Skip to content

fix(cli): strip memory-context fence tags from resumed history display#23393

Closed
suparious wants to merge 1 commit into
NousResearch:mainfrom
suparious:fix/cli-memory-context-leak
Closed

fix(cli): strip memory-context fence tags from resumed history display#23393
suparious wants to merge 1 commit into
NousResearch:mainfrom
suparious:fix/cli-memory-context-leak

Conversation

@suparious

Copy link
Copy Markdown

Problem

Memory context blocks injected by memory providers (e.g., data-layer plugin, Honcho) via build_memory_context_block() use <memory-context> fence tags. These are stripped from live streaming output by StreamingContextScrubber but leak into the CLI's resumed history display (_display_resumed_history()).

Users see raw <memory-context>...</memory-context> blocks in their terminal when:

  • Resuming sessions with hermes --continue
  • Browsing session history

Additionally, these leaked XML-like tags can corrupt Rich Markdown parser state when backtick code spans interact with injected tags, causing response truncation in mid-sentence.

Root Cause

_display_resumed_history() at line 4282 calls str(content) on user messages without calling sanitize_context(), unlike the streaming path at line 7431 which runs every delta through StreamingContextScrubber.

Fix

Two lines added to cli.py:

  1. Import: from agent.memory_manager import sanitize_context
  2. Sanitize call: text = sanitize_context(text) after user message text is finalized but before display

This strips <memory-context> fence tags, system notes, and escapes from the display path, matching the behavior of the live streaming path.

Testing

  • Existing sanitize_context tests pass (2/2)
  • Existing StreamingContextScrubber tests pass (17/17)
  • Syntax check passes
  • Manual verification: memory context blocks no longer appear in resumed session display

Related

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cli CLI entry point, hermes_cli/, setup wizard tool/memory Memory tool and memory providers labels May 10, 2026
@suparious suparious force-pushed the fix/cli-memory-context-leak branch from fd5d227 to 7ade8bb Compare May 10, 2026 21:17
@suparious suparious force-pushed the fix/cli-memory-context-leak branch from 66508da to d02d58f Compare May 29, 2026 15:43
Two paths where memory-context fence tags leak into user-visible UI:

1. _display_resumed_history() (now in cli_agent_setup_mixin.py)
   renders user message content without sanitizing — raw
   <memory-context>...</memory-context> blocks appear in the
   resume recap panel.

2. chat() in cli.py stores result['messages'] directly
   into conversation_history without sanitizing user messages.
   Every turn, old blocks compound with new ones.

Fix: import sanitize_context from agent.memory_manager and
call it on user message text in both paths, matching the behavior
of the live streaming path which already scrubs via
StreamingContextScrubber.
@suparious suparious force-pushed the fix/cli-memory-context-leak branch from 92a2e5f to 23b2fef Compare June 9, 2026 02:50
@teknium1

Copy link
Copy Markdown
Contributor

This looks implemented on current main. Automated hermes-sweeper review found the same leak class is now handled at the shared session-replay layer rather than inside _display_resumed_history() itself.

Evidence:

  • hermes_state.py:2781 sanitizes string content for user and assistant messages inside SessionDB.get_messages_as_conversation().
  • hermes_cli/cli_agent_setup_mixin.py:478 loads hermes --continue resume history through get_messages_as_conversation() before assigning self.conversation_history and displaying the recap.
  • hermes_cli/cli_commands_mixin.py:698 uses the same sanitized replay path for interactive /resume, then calls _display_resumed_history() at hermes_cli/cli_commands_mixin.py:747.
  • tests/test_hermes_state.py:607 covers the regression: a stored message containing a full <memory-context>...</memory-context> block is replayed as only Visible answer.

Thanks for the focused fix here; main appears to have closed this via the shared replay chokepoint, which also covers sibling session-history consumers.

@teknium1 teknium1 closed this Jun 11, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 11, 2026
@suparious suparious deleted the fix/cli-memory-context-leak branch June 13, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants