Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
366f0a9
test(opencode): prove Windows owner-death cleanup with real processes…
arena-agent Oct 31, 2026
34d8f92
test(opencode): give the stall test's post-bound phases fresh deadlines
arena-agent Oct 31, 2026
2edb1d9
test(opencode): make the stall test verify cleanup even when its firs…
arena-agent Oct 31, 2026
6c4e760
test(opencode): return the stall test to the accepted budget envelope…
arena-agent Oct 31, 2026
9bbe7bd
test(opencode): MUTATION CONTROL - disable the job binding (reverted …
arena-agent Oct 31, 2026
bd64297
Revert "test(opencode): MUTATION CONTROL - disable the job binding (r…
agenteinstinct1-max Oct 31, 2026
05fe976
test(opencode): give the happy path fresh per-phase deadlines too
arena-agent Oct 31, 2026
a6f57ff
feat(opencode): recover truncated write calls into a guided continuat…
agenteinstinct1-max Oct 31, 2026
1a12cca
Merge remote-tracking branch 'origin/dev-vibecoded' into arena/217651…
agenteinstinct1-max Oct 31, 2026
95d982c
fix(opencode): refuse ambiguous truncated-write targets and guide a s…
agenteinstinct1-max Oct 31, 2026
abd6b5e
Merge exact head 95d982c (Refs #736) into arena/d3070f89-silvercode
arena-agent Oct 31, 2026
27d2ce2
fix(opencode): reject ambiguous and malformed truncated-write prefixes
arena-agent Oct 31, 2026
b2f6e46
chore(opencode): drop inherited Windows owner-death fixtures
arena-agent Oct 31, 2026
1c0075d
Merge commit 'b2f6e4667a77bc481de31ddd05fbdea3c726393b' into arena/48…
agenteinstinct1-max Oct 31, 2026
69645ef
test(opencode): restore accepted CLI retry coverage
agenteinstinct1-max Oct 31, 2026
d90aae0
Merge remote-tracking branch 'origin/dev-vibecoded' into arena/15b45c…
agenteinstinct1-max Nov 1, 2026
0dda801
Merge commit '69645ef4b1efbc1c6fae884bdd5ea16d5f704342' into arena/15…
agenteinstinct1-max Nov 1, 2026
5b52d75
test(opencode): clarify doom-loop evidence
agenteinstinct1-max Nov 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 229 additions & 9 deletions packages/opencode/src/session/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,236 @@ import { RepetitionGuard } from "./llm/repetition-guard"
import { SystemPrompt } from "./system"
import { ToolCatalog } from "@/tool/catalog"

// A truncated `write` call is recoverable in a narrow, honest sense: the file path
// is usually emitted before the (large) content body, so a prefix of the arguments
// can name the target even though the payload never parsed. Only the path is
// salvaged - never a partial body - and nothing here reconstructs the call.
export type TruncatedWriteRecovery = {
tool: "write"
filePath: string
}

function isJsonParseFailure(error: unknown): boolean {
const message = error instanceof Error ? error.message : String(error)
const cause = error instanceof Error && error.cause instanceof Error ? error.cause.message : ""
return /json parse|unterminated|unexpected end of json|unexpected end of input|syntaxerror/i.test(
`${message} ${cause}`,
)
}

type StringRead = { ok: true; value: string; next: number } | { ok: false; reason: "truncated" | "invalid" }

// Reads one JSON string literal whose opening quote sits at `from`. A fragment that ends
// inside the literal is `truncated` (the value never finished arriving); a literal that
// closes but does not decode is `invalid`. Neither is a value we may report.
function readStringLiteral(raw: string, from: number): StringRead {
for (let index = from + 1; index < raw.length; index++) {
const char = raw[index]
if (char === "\\") {
index += 1
continue
}
if (char !== '"') continue
try {
return { ok: true, value: JSON.parse(raw.slice(from, index + 1)), next: index + 1 }
} catch {
return { ok: false, reason: "invalid" }
}
}
return { ok: false, reason: "truncated" }
}

type ValueSkip = { ok: true; next: number; truncated: boolean } | { ok: false; reason: "invalid" }

const JSON_TOKEN = /^(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)$/

// Skips a non-string JSON value starting at `from`, tracking nesting and string literals
// so a `}` or `,` inside a nested string can never be mistaken for structure.
function skipValue(raw: string, from: number): ValueSkip {
const char = raw[from]
if (char === '"') {
const literal = readStringLiteral(raw, from)
if (!literal.ok) {
return literal.reason === "truncated" ? { ok: true, next: raw.length, truncated: true } : { ok: false, reason: "invalid" }
}
return { ok: true, next: literal.next, truncated: false }
}
if (char === "{" || char === "[") {
// Track the opening brackets so a closing one only balances a value when it
// matches: `{]` and `[}` are not prefixes of well-formed JSON, and treating
// them as balanced would let a malformed fragment look like a completed one.
const open: string[] = [char]
let index = from + 1
while (index < raw.length) {
const current = raw[index]
if (current === '"') {
const literal = readStringLiteral(raw, index)
if (!literal.ok) {
return literal.reason === "truncated"
? { ok: true, next: raw.length, truncated: true }
: { ok: false, reason: "invalid" }
}
index = literal.next
continue
}
if (current === "{" || current === "[") open.push(current)
else if (current === "}" || current === "]") {
if (open[open.length - 1] !== (current === "}" ? "{" : "[")) return { ok: false, reason: "invalid" }
open.pop()
if (open.length === 0) return { ok: true, next: index + 1, truncated: false }
}
index += 1
}
return { ok: true, next: raw.length, truncated: true }
}
let index = from
while (index < raw.length && !/[\s,}\]]/.test(raw[index])) index += 1
const token = raw.slice(from, index)
if (index >= raw.length) {
// A literal cut off at the end is unknown, not a token we can validate - and it is
// not the value we are looking for either, so the scan simply stops here.
return { ok: true, next: raw.length, truncated: true }
}
if (!JSON_TOKEN.test(token)) return { ok: false, reason: "invalid" }
return { ok: true, next: index, truncated: false }
}

/**
* Tolerant prefix parse of one top-level string member out of a possibly truncated JSON
* object. The fragment must be a structural prefix of a single object: members are read
* in order as `"key": value` with real separators, so key-shaped text inside a string, a
* nested value, or a top-level array is never mistaken for the member. A duplicate of the
* requested key makes the target ambiguous - a complete payload could resolve it to a
* different value - so it yields nothing rather than a guess, and a value is returned
* only when its closing quote actually arrived.
*/
export function salvageStringArgument(raw: string, key: string): string | undefined {
let index = 0
const skipWhitespace = () => {
while (index < raw.length && /\s/.test(raw[index])) index += 1
}

skipWhitespace()
// Anything but an object start cannot carry this member meaningfully; a leading `[`
// can still contain the key shape textually, which is exactly the mistake to avoid.
if (raw[index] !== "{") return undefined
index += 1

let found: string | undefined
let seen = false

while (true) {
skipWhitespace()
// A fragment ending at a member boundary is a valid prefix: keep what was completed.
if (index >= raw.length) break
if (raw[index] === "}") break
if (raw[index] !== '"') return undefined

const name = readStringLiteral(raw, index)
if (!name.ok) return undefined
index = name.next

const target = name.value === key
// A repeated target key makes the target ambiguous even when the fragment ends
// before the second member's colon: a complete payload could still resolve the
// duplicate to a different value, so refuse rather than report the first. The check
// therefore happens as soon as the name is read, not after the colon.
if (target) {
if (seen) return undefined
seen = true
}

skipWhitespace()
// Truncated between a key and its colon: the member's value is simply unknown, and
// nothing after it can exist, so the members read so far stay trustworthy.
if (index >= raw.length) break
if (raw[index] !== ":") return undefined
index += 1
skipWhitespace()

// The fragment ended between a member's key and its value: unknown, but a member we
// never needed, so the members already read stay valid.
if (index >= raw.length) {
if (target) return undefined
break
}

if (raw[index] === '"' && target) {
const value = readStringLiteral(raw, index)
if (!value.ok) return undefined
if (value.value.length === 0) return undefined
found = value.value
index = value.next
} else {
const value = skipValue(raw, index)
if (!value.ok) return undefined
index = value.next
// The wanted member exists but its value is not a complete string, or it was cut
// off: there is nothing recoverable and a later duplicate could still change it.
if (target) return undefined
if (value.truncated) break
}

skipWhitespace()
if (index >= raw.length) break
if (raw[index] === ",") {
index += 1
continue
}
if (raw[index] === "}") break
// No separator between members: the fragment is not a prefix of well-formed JSON,
// so its structure cannot be trusted.
return undefined
}

return found
}

/**
* What can be honestly said about a failed `write` call: the target path, when the
* prefix carried a complete `filePath` value. Only the `write` tool is handled - its
* two arguments are independent, so a path alone is meaningful - and only when the
* raw arguments genuinely failed to parse (a schema rejection of well-formed JSON is
* a different failure with a different message).
*/
export function truncatedWriteRecovery(toolName: string, rawArguments: unknown): TruncatedWriteRecovery | undefined {
if (toolName.toLowerCase() !== "write") return undefined
if (typeof rawArguments !== "string" || rawArguments.length === 0) return undefined
try {
JSON.parse(rawArguments)
return undefined
} catch {
// Expected: the fragment did not parse, which is why the repair hook is running.
}
const filePath = salvageStringArgument(rawArguments, "filePath")
if (!filePath) return undefined
return { tool: "write", filePath }
}

// A tool call whose arguments never parsed as JSON is almost always a response
// cut off at the output-token limit mid-payload (or genuinely malformed JSON).
// Name the likely cause and the recovery instead of relaying the bare parser
// error, so the model retries with smaller arguments instead of improvising
// chunking schemes. A nudge only: the model remains free to retry the same call.
export function repairToolCallMessage(error: unknown): string {
// With a recovery, the nudge becomes a guided continuation for that one path.
export function repairToolCallMessage(error: unknown, recovery?: TruncatedWriteRecovery): string {
const message = error instanceof Error ? error.message : String(error)
const cause = error instanceof Error && error.cause instanceof Error ? error.cause.message : ""
const combined = `${message} ${cause}`
if (!/json parse|unterminated|unexpected end of json|unexpected end of input|syntaxerror/i.test(combined)) {
return message
}
return [
if (!isJsonParseFailure(error)) return message
const head = [
`The tool-call arguments were not valid JSON (${message}).`,
"This usually means the response was cut off at the output-token limit while the arguments were being generated.",
"Retry with smaller arguments. When writing a large file, split the content across several smaller tool calls or append it in chunks instead of sending one large payload, and keep each individual tool call well within the output budget.",
]
if (!recovery) {
return [
...head,
"Retry with smaller arguments. When writing a large file, split the content across several smaller tool calls or append it in chunks instead of sending one large payload, and keep each individual tool call well within the output budget.",
].join(" ")
}
return [
...head,
`Recovered from the partial arguments: the write call that failed to parse targeted "${recovery.filePath}". The file content for that call is not recovered - whatever content did or did not arrive in the fragment is unknown - so no content is assumed and none is inferred.`,
`That call did not execute, so this specific write call wrote nothing and changed nothing on disk. This is a statement about this one call only: earlier tool calls in this session may already have run and had effects, including on this same path, which this repair cannot see.`,
`Continue by inspecting "${recovery.filePath}" first: check whether it exists and what it currently holds. If it does not exist, send one write call whose content is only the first chunk of the file. Then append the remaining chunks with edit calls whose oldString is a unique tail of the content already on disk and whose newString is that same tail followed by the next chunk. If the path already exists, do not start over with write - write replaces the entire file and would discard what is already there and anything earlier calls wrote - append the remaining chunks with those same edit calls instead, or write the complete content in one call if it fits. Keep every single call well within the output budget.`,
].join(" ")
}

Expand Down Expand Up @@ -386,11 +600,17 @@ const live: Layer.Layer<
}
}
}
// Known to this request, a truncated write call can name its target from
// the raw fragment. The call itself still never executed: it is routed to
// `invalid` exactly as before, now with a guided continuation.
const recovery = Object.hasOwn(prepared.tools, failed.toolCall.toolName)
? truncatedWriteRecovery(failed.toolCall.toolName, failed.toolCall.input)
: undefined
return {
...failed.toolCall,
input: JSON.stringify({
tool: failed.toolCall.toolName,
error: repairToolCallMessage(failed.error),
error: repairToolCallMessage(failed.error, recovery),
}),
toolName: "invalid",
}
Expand Down
14 changes: 14 additions & 0 deletions packages/opencode/test/lib/llm-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,16 @@ export class Reply {
return this.toolCalls()
}

/**
* Emits a tool call whose argument text is sent verbatim, so a test can stream a
* payload that is not valid JSON (e.g. the truncated `write` call in #736).
*/
toolRaw(name: string, args: string) {
const id = this.#id()
this.#tail = [...this.#tail, toolStartLine(id, name), toolArgsLine(args)]
return this.toolCalls()
}

pendingTool(name: string, input: unknown) {
const id = this.#id()
const args = JSON.stringify(input)
Expand Down Expand Up @@ -816,6 +826,7 @@ namespace TestLLMServer {
readonly toolMatch: (match: Match, name: string, input: unknown) => Effect.Effect<void>
readonly text: (value: string, opts?: { usage?: Usage }) => Effect.Effect<void>
readonly tool: (name: string, input: unknown) => Effect.Effect<void>
readonly toolRaw: (name: string, args: string) => Effect.Effect<void>
readonly toolHang: (name: string, input: unknown) => Effect.Effect<void>
readonly reason: (value: string, opts?: { text?: string; usage?: Usage }) => Effect.Effect<void>
readonly fail: (message?: unknown) => Effect.Effect<void>
Expand Down Expand Up @@ -968,6 +979,9 @@ export class TestLLMServer extends Context.Service<TestLLMServer, TestLLMServer.
tool: Effect.fn("TestLLMServer.tool")(function* (name: string, input: unknown) {
queue(reply().tool(name, input).item())
}),
toolRaw: Effect.fn("TestLLMServer.toolRaw")(function* (name: string, args: string) {
queue(reply().toolRaw(name, args).item())
}),
toolHang: Effect.fn("TestLLMServer.toolHang")(function* (name: string, input: unknown) {
queue(reply().pendingTool(name, input).hang().item())
}),
Expand Down
Loading
Loading