Skip to content

fix(agent-memory): yaml-escape hint values in memory_observe and add max_hint_bytes config#1154

Open
ralf003 wants to merge 1 commit into
alibaba:mainfrom
ralf003:fix/agent-memory-hint-sanitization
Open

fix(agent-memory): yaml-escape hint values in memory_observe and add max_hint_bytes config#1154
ralf003 wants to merge 1 commit into
alibaba:mainfrom
ralf003:fix/agent-memory-hint-sanitization

Conversation

@ralf003

@ralf003 ralf003 commented Jun 26, 2026

Copy link
Copy Markdown

Problem

memory_observe writes the model-provided hint value into YAML frontmatter with minimal sanitization — only newlines are replaced with spaces. YAML special characters (# comment, : key-value separator, quotes, backslashes) can corrupt the frontmatter block. For example, hint: fix #123 and #456 is truncated by the YAML parser at fix because # starts a comment.

Additionally, there is no size limit on the hint field, unlike max_read_bytes/max_write_bytes/max_append_bytes for other MemoryConfig fields. A rogue or misconfigured model could inject arbitrarily long hints.

Changes (116 lines, 3 files)

src/tools/memory_observe.rs

  • Add yaml_escape_hint() function (~20 lines) that double-quotes hint values and escapes ", \, \n/\r, and ASCII control characters for safe YAML inclusion
  • Add max_hint_bytes enforcement (default 512) — hints exceeding the limit are rejected with InvalidArgument
  • Add 9 unit tests covering: # comment characters, colons, quotes, backslashes, multiline, empty strings, control chars, and normal text

src/config.rs

  • Add max_hint_bytes: u64 to MemoryConfig with default 512
  • Add default_max_hint_bytes() function
  • Add MEMORY_MAX_HINT_BYTES env var override in apply_env_overrides()

Service layer propagation

  • src/service/mod.rs: pass &self.config.memory through to tools::memory_observe (no signature change at service level)

Verification

  • All 147 unit tests pass on Linux (Alibaba Cloud ECS, x86_64, Rust 1.96)
  • 9 new yaml_escape_hint tests pass
  • Existing tests unchanged

…max_hint_bytes config

- Add yaml_escape_hint() to safely double-quote hint values in YAML
  frontmatter, preventing corruption from '#', ':', quotes, and control
  characters (previously only newlines were handled).
- Add max_hint_bytes (default 512) to MemoryConfig to cap hint length
  and prevent a rogue model from injecting oversized hints.
- Add MEMORY_MAX_HINT_BYTES env var override.
- Add 9 unit tests for yaml_escape_hint covering special chars,
  multiline, empty, control chars, and normal text.
- Update memory_observe signature to accept &MemoryConfig for limit
  enforcement; propagate config through MemoryService facade and MCP
  server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant