Skip to content

fix(memory): store large tool args as valid JSON so tool history stays readable - #3754

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:claude/fix-memory-tool-args
Sep 14, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:claude/fix-memory-tool-args

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

The tool history saved every large file write or edit with its arguments broken, so the history views showed nothing for those calls — not even which file was touched.

The tool history capped arguments at 500 characters by cutting the serialized JSON text, so any write_file or edit_file carrying real content was stored mid-token and could never parse. Measured on a benchmark memory store: 72 of 499 tool calls (14%) unreadable, every one a write_file or edit_file, every one exactly 500 characters. Tool history and the recent-errors views read these rows back as empty.

The cap stays. Long values, nested lists and dicts included, are now shortened instead of the text being cut, so short fields like file_path survive, the stored value always parses, and a _truncated flag marks a partial row. Arguments too wide to shorten fall back to their names.

This does not yet make learned skills carry their edits: skill synthesis keeps only tool names, so that half of #3753 needs a separate change.

Refs #3753

Test plan

  • pytest tests/unit/test_memory_store_tool_args.py — 7 tests: small args verbatim; 100 KB file content still parses, keeps file_path and is flagged _truncated; an edit with two large strings keeps all three keys; a large nested list of edits is shortened and keeps file_path; over-wide args fall back to their names; empty args store nothing; a large write round-trips through log_tool_call → get_tool_history with file_path intact. Every stored value is asserted to be under the cap.
  • pytest tests/unit -k "memory_store or skill_synthesis or procedural" — 421 pass, 63 skipped.
  • Benchmark A/B, the same 144-run battery before and after this merged: unreadable argument rows across every run's memory store went from 92 to 0.
  • Not an LLM-affecting path under CLAUDE.md's eval list (no prompt, tool schema or parsing change); unit coverage is the gate.

…ep their edits

Every file edit the agent made was saved to memory with its arguments
destroyed. log_tool_call capped args at 500 characters by cutting the
serialized JSON text, so any write_file or edit_file carrying real file
content was stored mid-token and could never parse again. The reader
logged "corrupt JSON column value ignored" and returned None.

Measured on a benchmark memory store: 72 of 499 tool calls (14%) were
unreadable, all write_file or edit_file, all exactly 500 characters.
The only reader is skill synthesis, so every skill GAIA learned from a
coding session recorded "call edit_file" with no file and no change.

The cap stays; long string values are shortened instead of the text
being cut, so short fields like file_path survive and the stored value
always parses. Arguments too wide to shorten fall back to their names.

Closes amd#3753

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

This replaces a byte-cut of the serialized tool-call arguments with a value-level shrink, so what gets stored in tool history always parses back and short fields like the file path survive. The change is sound, the cap is preserved, and the new tests cover the interesting shapes — I re-ran the helper against extra shapes myself and every result parsed and stayed under the limit.

One thing worth knowing before merge: the stated payoff — learned skills keeping their edits — doesn't actually land yet. The step that turns a session into a skill throws the arguments away before the model ever sees them, keeping only the tool names, so a recipe still says "call edit_file" with no file and no change even with this fix in place. What this PR genuinely fixes today is the tool-history views, which previously showed unreadable argument blobs for every large write or edit. If closing the linked issue means recipes carry their edits, a companion change to the synthesis step is still needed.

Two smaller things: a large payload nested inside a list or dict still falls all the way back to bare argument names, dropping the file path this PR set out to keep (the current file tools pass flat strings, so it doesn't bite today); and the PR description carries a "Generated with Claude Code" footer, which this repo's conventions prohibit.

Real-world evidence

N/A — no evidence bundle was produced for this run, and the change is an internal storage helper with no CLI, API, MCP, or Agent UI surface of its own. The verdict rests on static review plus a local re-run of the new helper against additional argument shapes (nested containers, non-string keys, unserializable values, unicode) — all parsed and fit the cap.

🔍 Technical details

🟡 Important

Stored args never reach skill synthesis, so the PR's headline outcome isn't reachable yet (src/gaia/agents/base/skill_synthesis.py:326)

GoalCluster.tool_sequence() returns {"tool": step["tool"]} — explicitly "names only (drops noisy args)". _build_distill_user_prompt (skill_synthesis.py:390) then builds the distiller message from tool_names alone, and put_skill(tool_sequence=cluster.tool_sequence()) (skill_synthesis.py:701) stores the same name-only list. So a synthesized recipe contains no file_path or content whether or not args parsed.

The readers that do benefit are _row_to_tool_dict (memory_store.py:2164) → get_tool_history / get_tool_errors / get_recent_errors, surfaced by src/gaia/ui/routers/memory.py:614. That's a real improvement — previously 14% of rows deserialized to None — but it's observability, not skill quality. Worth either narrowing the PR description or landing the synthesis-side change in the same PR so #3753 is genuinely closed.

🟢 Minor

Nested payloads skip the shrink and lose file_path (src/gaia/agents/base/memory_store.py:221)

The comprehension only shortens top-level str values, so a large payload inside a list/dict never shrinks and the function drops straight to the key-name fallback. Verified locally:

_bounded_args_json({"file_path": "a.py", "edits": [{"old": "o"*5000, "new": "n"*5000}]})
→ {"_truncated": true, "keys": ["edits", "file_path"]}

file_path — the field the PR exists to preserve — is gone. Today's write_file / edit_file take flat strings, so this is latent rather than live, but a tool taking a list of edits would hit it.

    for limit in (200, 60, 0):
        shrunk = {}
        for k, v in args.items():
            text_v = v if isinstance(v, str) else json.dumps(v, default=str)
            shrunk[k] = (text_v[:limit] + "...") if len(text_v) > limit else v
        text = json.dumps(shrunk, default=str)
        if len(text) <= MAX_FTS_QUERY_LENGTH:
            return text

Shrunk values carry no truncation marker (src/gaia/agents/base/memory_store.py:223)

A trailing "..." is the only signal, and it's indistinguishable from a value that genuinely ends in an ellipsis. The wide fallback sets _truncated; the shrink path could set the same flag so a reader can tell a partial value from a complete one.

Tests exercise the private helper only (tests/unit/test_memory_store_tool_args.py:12)

The five cases are well chosen, but none goes through log_tool_call → get_tool_history to assert the stored row's args deserializes. That round trip is the regression the PR is actually preventing, and it's the one path a future refactor of _log_tool_call could silently break. One integration-style test alongside the existing ones in tests/integration/test_memory_integration.py would pin it.

PR description carries a Claude attribution footer

CLAUDE.md → "No Claude Attribution of Any Kind" prohibits the 🤖 Generated with [Claude Code] footer in PR descriptions. Worth removing before merge.

Strengths

  • The three-stage degradation (verbatim → shrink long strings → key names) keeps the most useful fields at every size, and the final fallback bounds its own output correctly by testing keys + [key] before appending rather than after.
  • The module docstring and test docstring both state the failure they prevent — the 500-char mid-token cut — in a way a future reader can act on, without the history-tagging CLAUDE.md warns against.
  • The bug report is quantified against a real store (72 of 499 calls, all write_file/edit_file, all exactly 500 chars), which is exactly the evidence that makes a fix like this reviewable.

…tial rows

A tool call with a big list or dict argument (say, a batch of edits) still
dropped straight to bare argument names in tool history, losing the file
path. Nested values are now shortened like strings, and any shortened row
carries the same _truncated flag as the names-only fallback, so a reader can
tell a partial row from a complete one.

Adds a round-trip test through log_tool_call -> get_tool_history for a large
write, and narrows the docstrings: the fix is to tool-history readability;
skill synthesis still keeps tool names only.

Co-Authored-By: Claude Opus 5 <[email protected]>
@kovtcharov kovtcharov changed the title fix(memory): store large tool args as valid JSON so learned skills keep their edits fix(memory): store large tool args as valid JSON so tool history stays readable Sep 14, 2026
@kovtcharov

Copy link
Copy Markdown
Contributor Author

Thanks. All four points are addressed in 89359e2.

  • Nested payloads: a large list or dict is now shortened the same way a long string is, so the file path survives instead of the row falling back to bare argument names.
  • Truncation marker: any row with a shortened value now carries the same _truncated flag as the names-only fallback, and the flag counts toward the 500-character cap.
  • Round-trip test: a large write_file goes through the real store and comes back out of tool history still parsed, with its file path.
  • Headline claim: you're right that skill synthesis keeps only tool names, so learned skills don't carry their edits yet. I retitled the PR and changed the description to what it fixes today: readable arguments in the tool-history and error views. Closes #3753 is now Refs #3753, which leaves the issue open for the synthesis change.

Measured on the same 144-run benchmark battery before and after this change, unreadable argument rows across every run's memory store went from 92 to 0.

The attribution footer stays in the description on purpose, because the session that opened this PR is instructed to keep it. That conflicts with CLAUDE.md, so a maintainer should make the call.

🔍 Technical details
  • _bounded_args_json: each value's text is v for strings, otherwise json.dumps(v, default=str). It is cut to 200, then 60, then 0 characters plus "...". Values under the limit keep their original type. _truncated: True is added before the length check. Your example {"file_path": "a.py", "edits": [{"old": "o"*5000, "new": "n"*5000}]} now stores file_path, edits as a shortened JSON string, and the flag.
  • The flag can't show up on a complete row. The shrink loop only runs when the full JSON is already over the cap, and adding a key only makes it longer, so any row it returns has at least one value shortened.
  • At the last level (limit 0), every value, file_path included, becomes "...". That was already true for string args, and it carries the same information as the names-only fallback.
  • Tests: tests/unit/test_memory_store_tool_args.py now has 7 tests. The new ones cover the nested payload and log_tool_call → get_tool_history on a MemoryStore in tmp_path. pytest tests/unit -k "memory_store or skill_synthesis or procedural": 421 passed, 63 skipped.
  • Fuzz check, run locally and not committed: 5,000 random argument dicts mixing nested containers, integer keys, Path values, non-ASCII text, and quote/backslash runs. Every result parsed, stayed at or under 500 characters, and had _truncated exactly when the full JSON was over the cap.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 14, 2026
Merged via the queue into amd:main with commit b36bdcb Sep 14, 2026
39 checks passed
kovtcharov-amd pushed a commit that referenced this pull request Sep 15, 2026
- Rebased onto current main (245 commits since v0.23.0, up from 230).
- Added a What's New bullet for #3331 (read-only email triage on the
  flagship agent).
- Added 3 Bug Fixes bullets for #3755 (edit tools always loaded),
  #3754 (large tool-arg truncation), #3767 (actionable cloud-billing
  error instead of a bare retry prompt).
- Regenerated the Full Changelog against the new range.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants