Skip to content

opencode: recover truncated write tool calls into a guided continuation instead of a bare invalid-call error #736

Closed

Description

@agenteinstinct1-max

Symptom

Reported by the project owner from a real TUI session: a model write tool call arrived with its arguments cut mid-JSON - the fragment contained the target file path and then ended (JSON Parse error: Expected '}'). The current behavior surfaces the "Invalid tool call" error and returns an invalid tool result with a retry-with-smaller-arguments nudge (the #437 / #414 fix). The owner asked for something stronger: recover the call and add an instruction for the model to continue from that call, instead of just returning an error.

Current mechanism (source at tip 3d4c83f0)

experimental_repairToolCall in packages/opencode/src/session/llm.ts (~L362) receives the failed call - including the raw truncated argument text and the tool name - and rewrites it to toolName: "invalid" with a generic nudge from repairToolCallMessage (~L43). The model must improvise the retry from scratch. Because the arguments never parsed, the write tool never executed: nothing was written, so there is no partial/corrupt file to reconcile.

Direction (owner-approved intent, cause still qualified)

In the repair path, when the tool is known and the failure is a JSON parse failure:

  1. Salvage what is safely recoverable from the raw fragment (e.g. a complete filePath string value) with a tolerant prefix parse - never guess or fabricate argument content that did not arrive.
  2. Return a recovery-oriented message that states exactly what was recovered ("the write to was cut off before any file content was generated"), states explicitly that THIS write call did not execute so it wrote nothing (scoped claim about this call only - it must NOT generalize to "nothing was written in the session", because any earlier tool calls may have had side effects the repair path knows nothing about), and instructs the model to re-issue the write for that path in smaller chunks (write the first chunk, then append the rest, each call well within the output budget).
  3. Keep the cause qualified: output-token-limit truncation is the likely cause but is NOT proven per occurrence (the fragment could also be genuinely malformed JSON); the message wording must not assert it.

Relations / constraints

Acceptance

  • Recovered-argument continuation message as above, with honest recovered/nothing-written statements.
  • Tests: prefix salvage of a truncated write call (filePath recovered, content not fabricated), recovery message contents, single-occurrence does not trip the doom-loop guard, existing repair behaviors (lowercase, deferred discovery) unchanged at equal strength.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions