Skip to content

fix(hermes): hallucination guard — strip fabricated tool output - #1

Merged
zenc-cp merged 1 commit into
masterfrom
fix/hallucination-guard
Apr 6, 2026
Merged

fix(hermes): hallucination guard — strip fabricated tool output#1
zenc-cp merged 1 commit into
masterfrom
fix/hallucination-guard

Conversation

@zenc-cp

@zenc-cp zenc-cp commented Apr 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds a three-layer hallucination guard to api/streaming.py that detects and strips fabricated tool output ({"output": ..., "exit_code": ...}) from LLM responses
  • Streaming guard: Buffers suspicious tokens and pattern-matches against known hallucination patterns before forwarding to SSE stream
  • Post-run scrub: Scans all assistant messages after run_conversation() completes, replacing any fabricated output blocks
  • Anti-hallucination prompt: Injects an ephemeral system prompt that explicitly forbids writing fake tool execution results
  • Includes 25 unit tests for detection, stripping, and prompt validation

Ported from upstream hermes-webui PR nesquena#133.

Test plan

  • All 25 new tests pass (pytest tests/test_hallucination_guard.py -v)
  • Deploy to VM: cd ~/zen-console && git pull origin main && sudo systemctl restart zen-console
  • Verify real tool execution still works: send "run: echo REAL_$(date +%s)" and confirm real output
  • Verify hallucinated output patterns are stripped from streamed responses

🤖 Generated with Claude Code

Add a three-layer defense against LLM hallucinated tool output:

1. Streaming token buffer: detects and strips fake {"output": ..., "exit_code": ...}
   JSON blocks from the SSE token stream in real-time using pattern matching
2. Post-run scrub: scans all assistant messages after agent.run_conversation()
   and replaces any remaining fabricated output blocks
3. Anti-hallucination system prompt: ephemeral instruction injected into every
   agent run that explicitly forbids writing fake tool output

Includes 25 unit tests covering detection, stripping, and prompt validation.

Ported from upstream hermes-webui PR nesquena#133.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zenc-cp
zenc-cp merged commit 45a9f66 into master Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants