ADR 0002doc/{zh,en}/adr/0002-tool-contract.md

ADR 0002: The Tool Contract

Status: accepted, 2026-11-14.

Status: accepted, 2026-11-14.

Context

Coding is the foundational capability of the Agent Runtime, and its tools reach the model through one contract: ToolSpec, CallTool, ToolResult and the questions a call may ask. Several needs cannot be met inside plugins:

  • ◆Arguments are decoded by each plugin in its own way, so a schema the model sees does not bind what runs (review T03, T14).
  • ◆The agent cannot tell which tools only read, may run side by side, page their own output, or bring content from outside (needed by P27, P38, the loop guard and untrusted wrapping).
  • ◆A result cut by the agent is lost for good, so the model repeats calls with side effects to see the rest.
  • ◆Tool results are text only: an MCP image is delivered into the chat instead of shown to the model (D14), and read cannot show a screenshot.
  • ◆ask_user asks one free-text question, so choices come back as prose.
  • ◆A guard can allow or deny, but cannot require that a person approve one call.

Decision

  1. Arguments (P14). CallTool normalizes arguments against the pinned schema (null optionals removed, numeric and boolean strings converted, JSON strings for arrays or objects parsed, a single value wrapped where an array is wanted) and validates them before the guards, the ledger and the tool see them; failures return E2 or E3 and the tool does not run. The original execution schema is what is validated and pinned; a projection for a model never loosens it. A schema that does not compile is passed through unvalidated and logged tool.schema_invalid, keeping tools of imperfect MCP servers usable.
  2. Hints (P23). ToolSpec gains Output, Paged, Concurrent, ReadOnly, Destructive, Subject and Untrusted. They are facts for the runtime, never grants, never sent to the model, and part of the pin digest.
  3. The rest of a result (P24). RuntimeHost.ReadToolResult(key, offset, limit, pattern) returns lines of a recorded result of the caller's run, through the result guards again; the agent offers it as the builtin tool_result. Stored results are capped at 4 MiB (amended 2026-11-14: raised from 1 MiB when built, so long output can be read back whole; maxStoredResult in internal/host/broker/ledger.go).
  4. Parts (P25). ToolResult gains Parts []Part (text and image, the type user messages use). Parts go to the model only; people receive only Artifacts. Model plugins project parts per vendor, and models without image input get a text line naming the artifact.
  5. Structured questions (P26). Question gains Header, Options and MultiSelect; Answer gains Choices. A presentation without buttons shows numbered options and accepts their numbers or labels as text.
  6. Guard ask (P17). GuardDecision gains Ask and Approver: a guard can require that a person approve this call. The Host asks with its own key bound to the request digest (ADR-independent fix D17) and runs the call only when approved.

Consequences

  • ◆pkg/plugin types change additively; plugins that set none of the new fields behave as before. Process plugins carry the fields through the existing JSON.
  • ◆Runs pinned before the upgrade see tools whose hints changed as changed (the digest grows), as with D13.
  • ◆Amended 2026-12-04 (Feishu #424): a resumed attempt no longer keeps such tools hidden. Its first listing gives each tool whose pinned name and provider are unchanged its current definition (tool.repinned), so an upgrade during a run does not take the run's tools away; a tool whose provider changed stays hidden and the run is reminded once that it changed. Within one attempt the pin holds as before. No plugin contract changes (tools).
  • ◆Invalid arguments that some plugins tolerated are now refused before they run. The texts tell the model what to fix.
  • ◆Validation adds a schema compile per run and tool, cached for the run.

Design: tool results, tool catalog, extension points, proposals P14, P17, P23–P26.