Skip to content

fix(agent-core-v2): preserve raw tool call argument bytes across resume - #5268

Merged
sailist merged 1 commit into
mainfrom
bug-428-11-23-resume-tool-args
Nov 23, 2026
Merged

sailist merged 1 commit into
mainfrom
bug-428-11-23-resume-tool-args

Conversation

@sailist

@sailist sailist commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

After resuming a session, assistant messages rebuilt from the wire journal carry tool call arguments re-serialized from parsed JSON instead of the provider's original bytes. Whitespace, number spellings (1e3 → 1000), large-integer precision, \uXXXX escapes, 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 in packages/agent-core-v2/test/agent/loop/loop.test.ts encodes exactly this scenario and fails without the fix (verified by temporarily reverting the fold change).

Root Cause

The loop journal parses toolCall.arguments into an object when writing tool.call loop events, dropping the original bytes at persistence time; both resume-side folds then rebuild arguments via JSON.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.call recorded loop events gain an optional argsRaw field carrying the provider's raw argument string. It is written whenever the assistant tool call's arguments is a string (including the empty string) and omitted for null/undefined. Both journal write points (the tool.started branch and the message.appended tool fallback) derive args and argsRaw from the same helper so the two fields cannot drift apart.
  • The LLM resume fold (contextMemory/loopEventFold) and the UI replay fold (replayBuilder/fold) both rebuild with event.argsRaw ?? (event.args === undefined ? null : JSON.stringify(event.args)), keeping the two reconstruction paths consistent.
  • The wire change is additive: args remains the parsed object, so existing consumers of tool.call events (kap-server conversation fold, vis, search extraction) are unaffected, and the persisted-event schema passes the new field through untouched.
  • Tests: fold unit tests for byte-exact reconstruction (extra whitespace, 1e3, large integers, \uXXXX escapes, duplicate keys), malformed/empty arguments, and legacy records without argsRaw; an end-to-end loop test asserting the wire record carries argsRaw and that a full persist → resume round-trip matches the live state; five existing wire snapshots gained the argsRaw field with no other changes.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Tool call arguments in assistant messages rebuilt on resume/replay Re-serialized from parsed JSON: formatting normalized, malformed strings double-encoded as string literals, empty string became '""' Byte-identical to the live session whenever the record carries argsRaw None — sessions persisted by older versions have no argsRaw and rebuild exactly as before N/A
tool.call wire event shape args (parsed object) only Optional argsRaw (raw string) added alongside Consumers read args, which is unchanged; the field is additive N/A

Affected 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

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6adbc13

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/agent-core-v2 Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T08:09:40.212691Z 6adbc13 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@6adbc13
npx https://pkg.pr.new/@moonshot-ai/kimi-code@6adbc13

commit: 6adbc13

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@sailist
sailist force-pushed the bug-428-11-23-resume-tool-args branch from b82725b to 6adbc13 Compare November 23, 2026 08:07
@sailist
sailist merged commit 3a43c68 into main Nov 23, 2026
24 checks passed
@sailist
sailist deleted the bug-428-11-23-resume-tool-args branch November 23, 2026 08:50
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
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.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
…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).
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