Repository navigation
fix(agent-core-v2): preserve raw tool call argument bytes across resume - #5268
Conversation
🦋 Changeset detectedLatest commit: 6adbc13 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b82725b876
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| "@moonshot-ai/agent-core-v2": patch | |||
There was a problem hiding this comment.
Add changesets for the affected products
This user-visible fix reaches the CLI, desktop app, and VS Code extension through their agent-core-v2/Kimi SDK runtimes, so users who resume sessions with tool calls will observe the corrected behavior. Listing only @moonshot-ai/agent-core-v2 leaves the product release notes without this fix; add separate patch changesets with user-facing wording for @moonshot-ai/kimi-code, kimi-code-app, and kimi-code.
AGENTS.md reference: AGENTS.md:L169-L170
Useful? React with 👍 / 👎.
b82725b to
6adbc13
Compare
Record the assistant message's raw argument string on the tool.call loop event and let the context and replay folds prefer it over re-encoding the parsed args (MoonshotAI#5268), so a resumed session rebuilds malformed, empty or duplicate-key arguments exactly as the live one did. The journal spreads {args, argsRaw}; the three context-fold cases, the replay-fold case and the updated wire snapshot pass (journal 7, loop 68). Ledger: 5257 equivalent.
…reconciling when idle Remove reconcileWhenIdle from the reminder service and the plugin session-start call; the SDK's swarm/tower (and the fork's hello-world) calls are gone and the tests drive a step hook instead (MoonshotAI#5235). Also carry argsRaw in the registered-user-tools snapshot missed by the MoonshotAI#5268 update. Ledger: 5320/5304 not-applicable (root e2e harness), 5078 equivalent (testkit already ported).
Requirement or Bug
After resuming a session, assistant messages rebuilt from the wire journal carry tool call
argumentsre-serialized from parsed JSON instead of the provider's original bytes. Whitespace, number spellings (1e3→1000), large-integer precision,\uXXXXescapes, and duplicate keys all drift, so the prompt bytes of OpenAI-style requests differ before and after a resume, breaking prompt-cache stability. Malformed or empty argument strings are worse: they get re-encoded as quoted string literals ({"command": "ls"becomes"{\"command\": \"ls\"",''becomes'""'), corrupting the arguments semantically.Bug Reproduction Steps
Run a turn whose tool call arguments contain formatting that a JSON round-trip normalizes (e.g.
{ "query": "moon", "limit": 1e3 }), let it persist, resume the session, and compare the rebuilt assistant message arguments against the live ones — the bytes differ before this fix. The new end-to-end test inpackages/agent-core-v2/test/agent/loop/loop.test.tsencodes exactly this scenario and fails without the fix (verified by temporarily reverting the fold change).Root Cause
The loop journal parses
toolCall.argumentsinto an object when writingtool.callloop events, dropping the original bytes at persistence time; both resume-side folds then rebuildargumentsviaJSON.stringify(event.args). This is a fundamental fix, not a workaround: the raw string is now persisted alongside the parsed object and preferred during reconstruction.Code Changes
tool.callrecorded loop events gain an optionalargsRawfield carrying the provider's raw argument string. It is written whenever the assistant tool call'sargumentsis a string (including the empty string) and omitted fornull/undefined. Both journal write points (thetool.startedbranch and themessage.appendedtool fallback) deriveargsandargsRawfrom the same helper so the two fields cannot drift apart.contextMemory/loopEventFold) and the UI replay fold (replayBuilder/fold) both rebuild withevent.argsRaw ?? (event.args === undefined ? null : JSON.stringify(event.args)), keeping the two reconstruction paths consistent.argsremains the parsed object, so existing consumers oftool.callevents (kap-server conversation fold, vis, search extraction) are unaffected, and the persisted-event schema passes the new field through untouched.1e3, large integers,\uXXXXescapes, duplicate keys), malformed/empty arguments, and legacy records withoutargsRaw; an end-to-end loop test asserting the wire record carriesargsRawand that a full persist → resume round-trip matches the live state; five existing wire snapshots gained theargsRawfield with no other changes.Behavior Changes and Affected Users
argumentsin assistant messages rebuilt on resume/replay'""'argsRawargsRawand rebuild exactly as beforetool.callwire event shapeargs(parsed object) onlyargsRaw(raw string) added alongsideargs, which is unchanged; the field is additiveAffected modules: agent-core-v2 loop journal and the two resume/replay folds. Coverage: new fold unit tests, the new end-to-end resume test, and the updated wire snapshots.
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.