Skip to content

refactor(agent-core): align malformed tool args with schema validation#1209

Merged
RealKai42 merged 5 commits into
mainfrom
kaiyi/lahore
Jun 29, 2026
Merged

refactor(agent-core): align malformed tool args with schema validation#1209
RealKai42 merged 5 commits into
mainfrom
kaiyi/lahore

Conversation

@RealKai42

@RealKai42 RealKai42 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add a parseToolCallArguments helper that falls back to {} when tool call arguments are not valid JSON, then continues through normal AJV validation.
  • Remove the JSON repair path so invalid args are surfaced as schema validation errors instead of malformed JSON errors.
  • Preserve valid arguments for unknown tools in the transcript.

Notes

  • No dependency, lockfile, or Nix hash changes in this PR.
  • Includes a changeset for @moonshot-ai/agent-core and @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.ts
  • pnpm --filter @moonshot-ai/agent-core run typecheck
  • pnpm 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

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-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3037783

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/agent-core Patch
@moonshot-ai/kimi-code 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

@github-actions

Copy link
Copy Markdown
Contributor

❌ Nix build failed

Hash mismatch in pnpmDeps:

Hash
specified sha256-w/mEQrb5gNn4S5jZ95vO9uy4u/JB3wFbUfIZDcWqTXU=
got sha256-qSJmNAzgPQj5RkZ2VLxE6IhCEhZyl5oMLBFtJt9NbIY=

Please update flake.nix with the got hash.

@pkg-pr-new

pkg-pr-new Bot commented Jun 29, 2026

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

commit: 3037783

@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: 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: {},

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 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.

@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: 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".

Comment thread packages/agent-core/package.json Outdated
"chokidar": "^4.0.3",
"ignore": "^5.3.2",
"js-yaml": "^4.1.1",
"jsonrepair": "^3.14.1",

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 the required changeset

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.

@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: 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: {},

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

@RealKai42 RealKai42 changed the title feat(agent-core): repair malformed tool args JSON refactor(agent-core): align malformed tool args with schema validation Jun 29, 2026
@RealKai42 RealKai42 merged commit 0635387 into main Jun 29, 2026
9 checks passed
@RealKai42 RealKai42 deleted the kaiyi/lahore branch June 29, 2026 15:24
@github-actions github-actions Bot mentioned this pull request Jun 29, 2026
RealKai42 added a commit that referenced this pull request Jun 30, 2026
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.
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.

1 participant