refactor(agent-core): align malformed tool args with schema validation#1209
Conversation
Attempt jsonrepair when tool call arguments fail JSON.parse, then continue schema validation. Return malformed JSON errors with a concise expected schema hint so the model can retry with corrected arguments.
🦋 Changeset detectedLatest commit: 3037783 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
|
❌ Nix build failed Hash mismatch in
Please update |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fd273db5f1
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| toolCall, | ||
| toolName, | ||
| args, | ||
| args: {}, |
There was a problem hiding this comment.
Preserve arguments for unknown tools
When the model calls an unavailable tool with valid JSON arguments, this branch now returns {} before parsing the provider arguments, so dispatchToolCall records {} and AgentContext.appendLoopEvent serializes {} back into the assistant tool_call transcript. Before this change, the parsed arguments were preserved even on the not-found path, which matters for transcript fidelity, auditing, and duplicate-call telemetry because distinct unknown-tool calls are now indistinguishable.
Useful? React with 👍 / 👎.
Update the fixed-output pnpmDeps hash after adding jsonrepair so the Nix build can fetch dependencies.
# Conflicts: # flake.nix
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 80930ae0f7
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "chokidar": "^4.0.3", | ||
| "ignore": "^5.3.2", | ||
| "js-yaml": "^4.1.1", | ||
| "jsonrepair": "^3.14.1", |
There was a problem hiding this comment.
This change adds a runtime dependency and changes agent-core behavior by repairing malformed tool-call arguments before validation/execution, but the commit does not add any .changeset/ entry. The root AGENTS.md workflow requires generating a changeset before submitting a PR, and without one the release tooling/changelog will not record the user-visible CLI behavior change.
Useful? React with 👍 / 👎.
Stop repairing malformed tool call arguments. Fall back to an empty object on JSON parse failure and let schema validation produce the retry error, while preserving valid arguments for unknown tools in the transcript.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c6ab18fb24
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } catch (error) { | ||
| return { | ||
| success: true, | ||
| data: {}, |
There was a problem hiding this comment.
Reject malformed args before executing zero-arg tools
When a non-empty malformed arguments string is caught, this now returns success with {}. Because preflightToolCall validates and executes parsedArgs.data, any tool whose schema accepts an empty object can run even though the model supplied invalid or extra arguments; for example EnterPlanMode has a zero-arg strict schema and immediately calls planMode.enter() (packages/agent-core/src/tools/builtin/planning/enter-plan-mode.ts:19,42-43). A malformed call such as {"unexpected":true,} is therefore treated as an approved empty call instead of producing the malformed-JSON tool error that previously stopped execution.
Useful? React with 👍 / 👎.
These were the only changes in this branch that touched agent-core. They persisted two previously live-only signals (step retries, tool progress) to the wire purely so the visualizer could display them — marginal features that did not justify modifying the core loop or extending the wire surface. Reverts the agent-core loop/type/export changes (restored to main, keeping #1209) and its changeset, and removes the vis-side rendering and types that consumed step.end.retries / tool.result.progress. The rest of vis is unchanged and reads only data agent-core already persists.
Summary
parseToolCallArgumentshelper that falls back to{}when tool call arguments are not valid JSON, then continues through normal AJV validation.Notes
@moonshot-ai/agent-coreand@moonshot-ai/kimi-code.Tests
pnpm exec vitest run packages/agent-core/test/loop/tool-args-parse.test.ts packages/agent-core/test/loop/tool-call.e2e.test.tspnpm --filter @moonshot-ai/agent-core run typecheckpnpm exec oxlint --type-aware packages/agent-core/src/loop/tool-args-parse.ts packages/agent-core/src/loop/tool-call.ts packages/agent-core/test/loop/tool-args-parse.test.ts packages/agent-core/test/loop/tool-call.e2e.test.ts