Repository navigation
Description
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:
- Salvage what is safely recoverable from the raw fragment (e.g. a complete
filePathstring value) with a tolerant prefix parse - never guess or fabricate argument content that did not arrive. - 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).
- 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
- Successor of Give the model a useful nudge when a tool call is truncated/unparseable, instead of a bare JSON parse error #414 / feat: nudge the model when a tool call arrives unparseable #437 (the nudge) - this extends the same repair hook; do not regress the deferred-tool-discovery and lowercase-name repairs already in that hook.
- Doom-loop interplay (fix(opencode): doom_loop permission false-triggers on parallel/same-batch tool calls and non-failure statuses #725, fix in flight in fix(opencode): keep doom-loop guard to real failures across steps #728): a single truncated call plus its guided retry must not trip the doom-loop guard; verify against the guard's real-failure definition.
- Separate front from feat(session): auto-resume replay-safe after stream interruption with tool calls #730 / feat(session): auto-resume replay-safe after stream interruption (Refs #730) #733 (stream interruption auto-resume): different mechanism and locus (llm.ts repair hook vs processor/prompt); no conflicting patch.
- No silent blanket JSON auto-fix (e.g. brace-closing heuristics that could produce a corrupt or unintended write) and no partial content invention.
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.
Activity
fix(opencode): refuse ambiguous truncated-write targets and guide a s…
fix(opencode): recover truncated write calls into a guided continuati…
test(opencode): prove the doom-loop guard at runtime (Refs #736)