kimi-code/packages/agent-core/src/loop
Kai 063538744f
Some checks are pending
CI / test-windows (push) Waiting to run
CI / lint (push) Waiting to run
CI / build (push) Waiting to run
CI / test (push) Waiting to run
CI / typecheck (push) Waiting to run
Nix Build / Check flake.nix workspace sync (push) Waiting to run
Nix Build / nix build .#kimi-code (push) Blocked by required conditions
Release / Native release artifact (push) Blocked by required conditions
Release / Release (push) Waiting to run
Release / Deploy docs (push) Blocked by required conditions
Release / Publish native release assets (push) Blocked by required conditions
refactor(agent-core): align malformed tool args with schema validation (#1209)
* feat(agent-core): repair malformed tool args JSON

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.

* chore(nix): update pnpm deps hash

Update the fixed-output pnpmDeps hash after adding jsonrepair so the Nix build can fetch dependencies.

* refactor(agent-core): drop tool args JSON repair

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.

* chore: add changeset for tool args validation
2026-06-29 23:24:03 +08:00
..
errors.ts feat(agent-core): suggest update-config command in max-steps error (#1099) 2026-06-25 18:19:12 +08:00
events.ts feat: expose LLM stream timing events (#101) 2026-05-27 13:46:57 +08:00
index.ts refactor: simplify LLM request logging (#823) 2026-06-16 21:13:31 +08:00
llm.ts refactor: simplify LLM request logging (#823) 2026-06-16 21:13:31 +08:00
README.md Kimi For Coding 2026-05-22 15:54:50 +08:00
retry.ts refactor: simplify LLM request logging (#823) 2026-06-16 21:13:31 +08:00
run-turn.ts feat: pursue a goal autonomously (#270) 2026-06-02 22:52:23 +08:00
tool-access.ts feat: rework permission decision policies (#26) 2026-05-27 20:07:24 +08:00
tool-args-parse.ts refactor(agent-core): align malformed tool args with schema validation (#1209) 2026-06-29 23:24:03 +08:00
tool-call.ts refactor(agent-core): align malformed tool args with schema validation (#1209) 2026-06-29 23:24:03 +08:00
tool-scheduler.ts Kimi For Coding 2026-05-22 15:54:50 +08:00
turn-step.ts feat: pursue a goal autonomously (#270) 2026-06-02 22:52:23 +08:00
types.ts feat: add shell mode (!) to the CLI (#1079) 2026-06-25 21:24:53 +08:00

Loop

loop is the stateless agent loop. It does not own sessions, wire transport, compaction execution, permissions UI, or durable protocol bridging. Those are host-layer responsibilities.

Internal Owners

  • run-turn.ts owns turn-level convergence: abort and compaction safe points, max-step enforcement, usage aggregation, optional continuation after non-tool stops, and final TurnResult mapping.
  • turn-step.ts owns one provider step: pre/post step hooks, message construction, the atomic step envelope, LLM call, streaming callback wiring, and the handoff to the tool-call lifecycle.
  • tool-call.ts owns the tool-call batch lifecycle. Classification is pure; preparation dispatches recorded tool.call events in provider order; terminal tool.result events are recorded in provider order before step.end seals the step.
  • tool-scheduler.ts owns stateful tool execution scheduling: tasks with non-conflicting resource accesses may overlap, while conflicting tasks are serialized at provider-order boundaries.
  • llm.ts, events.ts, and types.ts define the narrow model, event/transcript, message, and tool surfaces that hosts provide to the loop.

Contracts

  • The core loop must not import from host-layer implementations.
  • LLM is the only source of model metadata, optional capability metadata, and system prompt.
  • buildMessages builds the latest model-visible messages per model step.
  • dispatchEvent is the only event path the loop writes to. The dispatcher records LoopRecordedEvents, publishes LoopLiveOnlyEvents, and routes shared events such as step.begin, step.end, tool.call, and tool.result to both transcript and live listeners.
  • Live event listener failures are contained by LoopEventDispatcher and must not affect the agent loop.
  • Provider usage is recorded immediately after LLM.chat returns, not after tool execution completes. Aborted tool execution must still report spent LLM usage.
  • The transcript step envelope is intentionally partial on provider abort: step.begin may exist without step.end.
  • Every dispatched tool.call must be followed by a matching tool.result unless the step is interrupted before the result dispatch point.

Test Boundaries

The main regression guards live in test/loop:

  • turn-lifecycle.e2e.test.ts covers turn convergence and usage aggregation.
  • transcript.e2e.test.ts covers step ordering and durable transcript linkage.
  • tool-call.e2e.test.ts and hooks.e2e.test.ts cover tool preparation, description, result finalization, and hook safety points.
  • abort.e2e.test.ts, error-paths.e2e.test.ts, and events.e2e.test.ts cover abort convergence, error propagation, and live event containment.
  • streaming.e2e.test.ts covers provider streaming callback wiring.