Skip to content

Commit 1376921

Browse files
matte250claude
andcommitted
fix: detect opencode completion via JSON event stream, not heuristics
Switch opencode invocation to --format json --dangerously-skip-permissions and parse the JSONL event stream from stdout. The terminal step_finish event (part.reason === "stop") is opencode's own "the agent is done" signal — far more reliable than guessing from idle time. When we see that event we proactively kill the process group, working around the upstream regression (anomalyco/opencode#3213, anomalyco/opencode#17516) where opencode 0.15+ doesn't exit on its own after completion. Hard timeout + deadman remain as belt-and-suspenders for genuinely stuck runs. Side benefits of the JSON stream: - step.tool / step.text / step.finish events log structured progress with token counts and tool inputs — much nicer journal output - --dangerously-skip-permissions removes any chance of headless permission prompts stalling us (opencode is already sandboxed to --dir) Also generalises generate.ts: trust validation as source of truth, treat exit code as a hint. opencode dying via our SIGTERM after completion is expected, so a non-zero exit with valid files on disk is a success. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 04c320b commit 1376921

3 files changed

Lines changed: 155 additions & 65 deletions

File tree

‎.env.example‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ VLLM_API_KEY=not-needed-for-local
77
OPENCODE_BIN=opencode
88
# Provider/model id as it appears in your opencode config (e.g. "vllm/gpt-oss-120b")
99
OPENCODE_MODEL=vllm/gpt-oss-120b
10-
# Per-generation timeout in milliseconds (each stage call gets this much)
10+
# Per-generation hard timeout in ms (each stage call gets this much)
1111
OPENCODE_TIMEOUT_MS=900000
1212

1313
# Quality vs. speed: 1 = implement only (~2 min), 2 = +critique (~5 min), 3 = +polish (~7-10 min)

‎src/generate.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,39 @@ async function runStage(
165165

166166
const prompt = stageDef.buildPrompt(idea, attempt, lastErrors);
167167
const oc = await runOpenCode({ sandboxDir: sandbox.dir, prompt });
168-
if (oc.exitCode !== 0) {
169-
lastErrors = [
170-
`opencode exited with code ${oc.exitCode}${oc.timedOut ? " (TIMED OUT)" : ""}`,
171-
oc.stderr.slice(-800),
172-
];
173-
log.warn("stage: opencode non-zero exit", { stage: stageNum, attempt, exitCode: oc.exitCode });
174-
continue;
175-
}
176168

169+
// Always validate, regardless of opencode's exit code. We proactively
170+
// kill opencode after seeing its terminal step_finish/stop event (workaround
171+
// for the upstream hang regression), so a non-zero exit is normal — what
172+
// matters is whether the files on disk are valid.
177173
lastValidation = await validate(sandbox.dir);
178174
if (lastValidation.ok && lastValidation.meta) {
179175
const durationMs = Date.now() - startedAt;
180-
log.info("stage: success", { stage: stageNum, name: stageDef.name, attempt, durationMs });
176+
log.info("stage: success", {
177+
stage: stageNum,
178+
name: stageDef.name,
179+
attempt,
180+
durationMs,
181+
exitCode: oc.exitCode,
182+
taskCompleted: oc.taskCompleted,
183+
timedOut: oc.timedOut,
184+
});
181185
return { ok: true, meta: lastValidation.meta, errors: [], attempts: attempt, durationMs };
182186
}
187+
183188
lastErrors = lastValidation.errors;
184-
log.warn("stage: validation failed", { stage: stageNum, attempt, errorCount: lastErrors.length });
189+
if (!oc.taskCompleted) {
190+
lastErrors = [
191+
`opencode did not signal task completion (exit ${oc.exitCode}${oc.timedOut ? ", HARD TIMEOUT" : ""})`,
192+
...lastErrors,
193+
];
194+
}
195+
log.warn("stage: validation failed", {
196+
stage: stageNum,
197+
attempt,
198+
exitCode: oc.exitCode,
199+
errorCount: lastErrors.length,
200+
});
185201
}
186202

187203
return {

‎src/opencode.ts‎

Lines changed: 128 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,38 @@ export type OpenCodeRunResult = {
1212
exitCode: number;
1313
stdout: string;
1414
stderr: string;
15+
/** True if the hard OPENCODE_TIMEOUT_MS deadline killed it. */
1516
timedOut: boolean;
17+
/** True if we observed opencode emit a step_finish event with reason "stop" (real done signal). */
18+
taskCompleted: boolean;
1619
durationMs: number;
1720
};
1821

22+
type OpenCodeEvent = {
23+
type?: string;
24+
sessionID?: string;
25+
part?: {
26+
type?: string;
27+
reason?: string;
28+
tool?: string;
29+
text?: string;
30+
state?: { input?: Record<string, unknown>; output?: string };
31+
tokens?: { total?: number; input?: number; output?: number; reasoning?: number };
32+
};
33+
};
34+
1935
export async function runOpenCode(opts: OpenCodeRunOptions): Promise<OpenCodeRunResult> {
2036
const cfg = loadConfig();
37+
// --format json: emit one JSON event per line on stdout (step_start, tool_use,
38+
// text, step_finish). Lets us detect actual completion via step_finish/stop.
39+
// --dangerously-skip-permissions: headless run; no human can answer prompts.
40+
// The "danger" is moot since opencode is sandboxed to opts.sandboxDir.
2141
const args = [
2242
"run",
23-
"--model",
24-
cfg.OPENCODE_MODEL,
25-
"--dir",
26-
opts.sandboxDir,
43+
"--format", "json",
44+
"--dangerously-skip-permissions",
45+
"--model", cfg.OPENCODE_MODEL,
46+
"--dir", opts.sandboxDir,
2747
opts.prompt,
2848
];
2949

@@ -39,45 +59,116 @@ export async function runOpenCode(opts: OpenCodeRunOptions): Promise<OpenCodeRun
3959
const child = spawn(cfg.OPENCODE_BIN, args, {
4060
cwd: opts.sandboxDir,
4161
stdio: ["ignore", "pipe", "pipe"],
42-
// Put opencode in its own process group so we can kill the whole tree
43-
// (opencode spawns subprocesses; a SIGTERM to the parent alone leaves
44-
// orphans holding stdio open, which prevents the 'close' event from
45-
// firing and hangs the loop forever).
62+
// Own process group: opencode spawns subprocesses; killing only the parent
63+
// leaves orphans holding stdio open, blocking the 'close' event.
4664
detached: true,
4765
});
4866

4967
const stdoutChunks: Buffer[] = [];
5068
const stderrChunks: Buffer[] = [];
5169
let timedOut = false;
70+
let taskCompleted = false;
5271
let settled = false;
5372
let lastActivityAt = Date.now();
73+
let stdoutBuf = "";
74+
let stderrBuf = "";
75+
let deadman: NodeJS.Timeout | null = null;
5476

55-
// Stream output line-by-line so the journal shows real-time progress
56-
// (opencode emits "→ Read foo", "← Write bar" markers per tool call).
57-
const streamer = (label: "stdout" | "stderr") => {
58-
let buf = "";
59-
return (b: Buffer) => {
60-
lastActivityAt = Date.now();
61-
buf += b.toString("utf-8");
62-
let i: number;
63-
while ((i = buf.indexOf("\n")) !== -1) {
64-
const line = buf.slice(0, i).trimEnd();
65-
buf = buf.slice(i + 1);
66-
if (line) log.info("opencode " + label, { line: line.slice(0, 500) });
77+
const killTree = (signal: "SIGTERM" | "SIGKILL") => {
78+
if (child.pid == null) return;
79+
try {
80+
process.kill(-child.pid, signal);
81+
} catch (e) {
82+
if ((e as NodeJS.ErrnoException).code !== "ESRCH") {
83+
log.warn("opencode.run kill failed", { signal, err: String(e) });
6784
}
68-
};
85+
}
6986
};
87+
88+
const armDeadman = (graceMs: number, on: "task-complete" | "hard-timeout") => {
89+
deadman = setTimeout(() => {
90+
if (!settled) {
91+
log.warn("opencode.run deadman fired (close event never arrived)", { on, graceMs });
92+
// exit 0 if we know the task completed, else -1
93+
settle(buildResult(on === "task-complete" ? 0 : -1));
94+
}
95+
}, graceMs);
96+
};
97+
98+
const handleEvent = (ev: OpenCodeEvent) => {
99+
switch (ev.type) {
100+
case "step_start":
101+
log.info("opencode step.start", { sessionID: ev.sessionID });
102+
break;
103+
case "tool_use": {
104+
const inputSummary: Record<string, string> = {};
105+
const input = ev.part?.state?.input ?? {};
106+
for (const [k, v] of Object.entries(input)) {
107+
const s = typeof v === "string" ? v : JSON.stringify(v);
108+
inputSummary[k] = s.length > 80 ? s.slice(0, 80) + "…" : s;
109+
}
110+
log.info("opencode step.tool", { tool: ev.part?.tool, input: inputSummary });
111+
break;
112+
}
113+
case "text":
114+
log.info("opencode step.text", { preview: ev.part?.text?.slice(0, 200) });
115+
break;
116+
case "step_finish":
117+
log.info("opencode step.finish", {
118+
reason: ev.part?.reason,
119+
tokensTotal: ev.part?.tokens?.total,
120+
tokensOutput: ev.part?.tokens?.output,
121+
reasoning: ev.part?.tokens?.reasoning,
122+
});
123+
// Terminal event: model decided to stop. Session is complete.
124+
// opencode 0.15+ has a regression where it doesn't exit on its own
125+
// (https://github.com/sst/opencode/issues/3213), so we kill the
126+
// process group ourselves and treat the exit as success.
127+
if (ev.part?.reason === "stop" && !taskCompleted) {
128+
taskCompleted = true;
129+
log.info("opencode.run task complete, killing process group", {
130+
elapsedMs: Date.now() - startedAt,
131+
});
132+
killTree("SIGTERM");
133+
setTimeout(() => killTree("SIGKILL"), 5_000);
134+
armDeadman(10_000, "task-complete");
135+
}
136+
break;
137+
default:
138+
log.debug("opencode event", { type: ev.type });
139+
}
140+
};
141+
70142
child.stdout.on("data", (b: Buffer) => {
143+
lastActivityAt = Date.now();
71144
stdoutChunks.push(b);
72-
streamer("stdout")(b);
145+
stdoutBuf += b.toString("utf-8");
146+
let i: number;
147+
while ((i = stdoutBuf.indexOf("\n")) !== -1) {
148+
const line = stdoutBuf.slice(0, i).trimEnd();
149+
stdoutBuf = stdoutBuf.slice(i + 1);
150+
if (!line) continue;
151+
try {
152+
handleEvent(JSON.parse(line) as OpenCodeEvent);
153+
} catch {
154+
log.warn("opencode stdout non-json", { line: line.slice(0, 300) });
155+
}
156+
}
73157
});
158+
74159
child.stderr.on("data", (b: Buffer) => {
160+
lastActivityAt = Date.now();
75161
stderrChunks.push(b);
76-
streamer("stderr")(b);
162+
stderrBuf += b.toString("utf-8");
163+
let i: number;
164+
while ((i = stderrBuf.indexOf("\n")) !== -1) {
165+
const line = stderrBuf.slice(0, i).trimEnd();
166+
stderrBuf = stderrBuf.slice(i + 1);
167+
if (line) log.info("opencode stderr", { line: line.slice(0, 300) });
168+
}
77169
});
78170

79-
// Heartbeat so the journal shows liveness even when the model is silent
80-
// (reasoning tokens don't stream, so a long think looks identical to a hang).
171+
// Heartbeat for journal liveness during long reasoning gaps.
81172
const heartbeat = setInterval(() => {
82173
const elapsedSec = Math.round((Date.now() - startedAt) / 1000);
83174
const idleSec = Math.round((Date.now() - lastActivityAt) / 1000);
@@ -86,31 +177,21 @@ export async function runOpenCode(opts: OpenCodeRunOptions): Promise<OpenCodeRun
86177
idleSec,
87178
stdoutBytes: stdoutChunks.reduce((n, b) => n + b.length, 0),
88179
stderrBytes: stderrChunks.reduce((n, b) => n + b.length, 0),
180+
taskCompleted,
89181
});
90182
}, 60_000);
91183

92-
const killTree = (signal: "SIGTERM" | "SIGKILL") => {
93-
if (child.pid == null) return;
94-
try {
95-
process.kill(-child.pid, signal);
96-
} catch (e) {
97-
// ESRCH = group already gone, fine
98-
if ((e as NodeJS.ErrnoException).code !== "ESRCH") {
99-
log.warn("opencode.run kill failed", { signal, err: String(e) });
100-
}
101-
}
102-
};
103-
104184
const settle = (result: OpenCodeRunResult) => {
105185
if (settled) return;
106186
settled = true;
107187
clearTimeout(timeout);
108-
clearTimeout(deadman);
188+
if (deadman) clearTimeout(deadman);
109189
clearInterval(heartbeat);
110190
opts.signal?.removeEventListener("abort", onAbort);
111191
log.info("opencode.run finished", {
112192
exitCode: result.exitCode,
113193
timedOut: result.timedOut,
194+
taskCompleted: result.taskCompleted,
114195
durationMs: result.durationMs,
115196
stdoutLen: result.stdout.length,
116197
stderrLen: result.stderr.length,
@@ -123,27 +204,16 @@ export async function runOpenCode(opts: OpenCodeRunOptions): Promise<OpenCodeRun
123204
stdout: Buffer.concat(stdoutChunks).toString("utf-8"),
124205
stderr: Buffer.concat(stderrChunks).toString("utf-8"),
125206
timedOut,
207+
taskCompleted,
126208
durationMs: Date.now() - startedAt,
127209
});
128210

129-
let deadman: NodeJS.Timeout = setTimeout(() => {}, 0);
130-
clearTimeout(deadman);
131-
132211
const timeout = setTimeout(() => {
133212
timedOut = true;
134-
log.warn("opencode.run timeout, killing process group", { ms: cfg.OPENCODE_TIMEOUT_MS });
213+
log.warn("opencode.run hard timeout, killing process group", { ms: cfg.OPENCODE_TIMEOUT_MS });
135214
killTree("SIGTERM");
136215
setTimeout(() => killTree("SIGKILL"), 5_000);
137-
// Last-resort: if 'close' never fires (orphan process holding stdio),
138-
// force-resolve so the loop can retry instead of hanging forever.
139-
deadman = setTimeout(() => {
140-
if (!settled) {
141-
log.error("opencode.run deadman fired (close event never arrived)", {
142-
graceMs: 15_000,
143-
});
144-
settle(buildResult(-1));
145-
}
146-
}, 15_000);
216+
armDeadman(15_000, "hard-timeout");
147217
}, cfg.OPENCODE_TIMEOUT_MS);
148218

149219
const onAbort = () => {
@@ -160,15 +230,19 @@ export async function runOpenCode(opts: OpenCodeRunOptions): Promise<OpenCodeRun
160230
if (settled) return;
161231
settled = true;
162232
clearTimeout(timeout);
163-
clearTimeout(deadman);
233+
if (deadman) clearTimeout(deadman);
164234
clearInterval(heartbeat);
165235
opts.signal?.removeEventListener("abort", onAbort);
166236
log.error("opencode.run spawn error", { err: String(err) });
167237
reject(err);
168238
});
169239

170240
child.on("close", (code) => {
171-
settle(buildResult(code ?? -1));
241+
// If our own SIGTERM after task-complete is what stopped opencode,
242+
// report exit 0 — the work was successful even though the signal made
243+
// the OS report the kill code.
244+
const exitCode = taskCompleted ? 0 : (code ?? -1);
245+
settle(buildResult(exitCode));
172246
});
173247
});
174248
}

0 commit comments

Comments
 (0)