Skip to content

Commit caca332

Browse files
Bound opencode's non-exiting provider inside the container
`opencode run` starts an in-process local server and never exits (anomalyco/opencode#17516). The lingering process inherits `docker exec`'s stdout as fd 1, so the host never sees EOF and no host-side deadline can shorten the wait — every dispatch to an opencode target burned its full deadline and was booked execution_timed_out even though the model had already produced the correct deliverable. Bound it where the observation is actually possible: a static dash supervisor wraps the provider in-container, watches captured output for N seconds of quiescence (SWITCHYARD_OPENCODE_IDLE_SECONDS, default 60), then TERM -> KILL with a 5s grace, sweeps survivors, and exits reserved code 75. The adapter/lifecycle boundary maps 75 to {success: true, idleTerminated: true}, so the captured diff still passes the integration gate. Idle termination requires at least one byte on *stdout*, not merely stream activity: opencode writes progress to stderr and its answer to stdout, so a stderr-only run has not finished anything and is left to the host deadline to classify as a timeout rather than being booked as a success. Result: 3 x execution_timed_out at bounds of 15m and 45m -> success in ~85-92s on both the sync and detached paths.
1 parent 2cab77a commit caca332

3 files changed

Lines changed: 496 additions & 47 deletions

File tree

‎src/switchyard/adapter/opencode.mjs‎

Lines changed: 232 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,205 @@ const OPENCODE_CMD = "opencode";
1818
const CREDENTIALS_PATH = "/root/.local/share/opencode/auth.json";
1919
const MIN_CREDENTIAL_BYTES = 16;
2020

21+
// `opencode run` starts an in-process local server (see its own `run --help`:
22+
// `--port` "port for the local server", `--attach` "attach to a running
23+
// opencode server") and never shuts it down, so the CLI parks in futex_wait
24+
// after finishing the task. There is no one-shot/`--exit` flag. Because the
25+
// lingering process holds `docker exec`'s stdout pipe as fd 1, the host never
26+
// observes EOF and NO host-side deadline can shorten the wait — the pipe stays
27+
// open until something inside the container kills the process. Upstream:
28+
// anomalyco/opencode#17516 (open, no maintainer response) and #32335 (the
29+
// non-exiting process retains 90-400 MB RSS per job).
30+
//
31+
// So the bound has to be enforced container-side. OPENCODE_SUPERVISOR runs
32+
// opencode as a background child, watches its captured output for quiescence,
33+
// and terminates it once it has gone silent for `idle` seconds. The wrapper
34+
// then closes the pipe by exiting, which is what actually unblocks the host.
35+
const IDLE_EXIT_CODE = 75;
36+
const DEFAULT_IDLE_SECONDS = 60;
37+
const MIN_IDLE_SECONDS = 5;
38+
const MAX_IDLE_SECONDS = 3600;
39+
const MAX_IDLE_STDERR_CHARS = 4000;
40+
const IDLE_TERMINATION_NOTE =
41+
"[switchyard] opencode stopped producing output and did not exit; " +
42+
"terminated by the container-side idle bound and the working tree was " +
43+
"captured as-is (upstream anomalyco/opencode#17516).";
44+
45+
// Invoked as: sh -c <script> sh <idleSeconds> opencode run [args...] <prompt>
46+
// The prompt and every provider argument arrive as positional parameters and
47+
// are never interpolated into this text, so no caller-controlled string is ever
48+
// parsed as shell source.
49+
const OPENCODE_SUPERVISOR = `set -u
50+
idle=$1
51+
shift
52+
cmd=$1
53+
base=/tmp/switchyard-opencode.$$
54+
out=$base.out
55+
err=$base.err
56+
: >"$out"
57+
: >"$err"
58+
"$@" >"$out" 2>"$err" &
59+
pid=$!
60+
last=0
61+
quiet=0
62+
elapsed=0
63+
killed=0
64+
while :; do
65+
st=$(awk '/^State:/{print $2}' "/proc/$pid/status" 2>/dev/null)
66+
if [ -z "$st" ] || [ "$st" = Z ]; then
67+
break
68+
fi
69+
osize=$(wc -c <"$out")
70+
size=$(( osize + $(wc -c <"$err") ))
71+
if [ "$size" -ne "$last" ]; then
72+
last=$size
73+
quiet=0
74+
else
75+
quiet=$(( quiet + 1 ))
76+
fi
77+
# Either stream counts as activity, but only stdout qualifies the run as
78+
# having produced a result. opencode writes progress to stderr and its answer
79+
# to stdout, so a stderr-only run that then goes silent has not finished
80+
# anything — it is left to the host deadline, which classifies it as a
81+
# timeout instead of booking it as a success.
82+
if [ "$osize" -gt 0 ] && [ "$quiet" -ge "$idle" ]; then
83+
killed=1
84+
break
85+
fi
86+
elapsed=$(( elapsed + 1 ))
87+
if [ $(( elapsed % 15 )) -eq 0 ]; then
88+
printf 'switchyard: opencode alive %ss, %s bytes captured, %ss idle\\n' "$elapsed" "$last" "$quiet" >&2
89+
fi
90+
sleep 1
91+
done
92+
if [ "$killed" -eq 1 ]; then
93+
kill -TERM "$pid" 2>/dev/null
94+
i=0
95+
while [ "$i" -lt 5 ]; do
96+
st=$(awk '/^State:/{print $2}' "/proc/$pid/status" 2>/dev/null)
97+
if [ -z "$st" ] || [ "$st" = Z ]; then
98+
break
99+
fi
100+
sleep 1
101+
i=$(( i + 1 ))
102+
done
103+
kill -KILL "$pid" 2>/dev/null
104+
wait "$pid" 2>/dev/null
105+
swept=0
106+
for p in /proc/[0-9]*; do
107+
q=$(basename "$p")
108+
if [ "$q" = "$$" ] || [ "$q" = 1 ]; then
109+
continue
110+
fi
111+
a0=$(tr '\\0' '\\n' <"$p/cmdline" 2>/dev/null | head -n 1)
112+
if [ -z "$a0" ]; then
113+
continue
114+
fi
115+
if [ "$(basename "$a0")" = "$cmd" ]; then
116+
kill -KILL "$q" 2>/dev/null
117+
swept=$(( swept + 1 ))
118+
fi
119+
done
120+
if [ "$swept" -gt 0 ]; then
121+
printf 'switchyard: swept %s surviving %s process(es)\\n' "$swept" "$cmd" >&2
122+
fi
123+
rc=${IDLE_EXIT_CODE}
124+
else
125+
wait "$pid" 2>/dev/null
126+
rc=$?
127+
fi
128+
cat "$out"
129+
cat "$err" >&2
130+
rm -f "$out" "$err"
131+
exit "$rc"
132+
`;
133+
134+
/**
135+
* Seconds of provider silence that count as "finished but not exited".
136+
* Overridable per run via SWITCHYARD_OPENCODE_IDLE_SECONDS; any malformed or
137+
* out-of-range value falls back to the default rather than failing the
138+
* dispatch, and the value is digits-only so it is safe as argv.
139+
*/
140+
function resolveIdleSeconds(env = process.env) {
141+
const raw = env.SWITCHYARD_OPENCODE_IDLE_SECONDS;
142+
if (typeof raw !== "string" || !/^\d+$/u.test(raw)) {
143+
return DEFAULT_IDLE_SECONDS;
144+
}
145+
const parsed = Number.parseInt(raw, 10);
146+
if (parsed < MIN_IDLE_SECONDS || parsed > MAX_IDLE_SECONDS) {
147+
return DEFAULT_IDLE_SECONDS;
148+
}
149+
return parsed;
150+
}
151+
152+
/**
153+
* Build the `docker exec` argv for a supervised `opencode run`. Throws on any
154+
* argument that fails the shared shell-safety checks so both the sync and async
155+
* entry points reject identically.
156+
*/
157+
function buildSupervisedArgs(
158+
workingContainerName,
159+
invocationArgs,
160+
model,
161+
guardedPrompt,
162+
) {
163+
validateIdentifier(workingContainerName, "workingContainerName");
164+
const args = [
165+
"exec",
166+
"-i",
167+
"-w",
168+
"/project",
169+
workingContainerName,
170+
"sh",
171+
"-c",
172+
OPENCODE_SUPERVISOR,
173+
"sh",
174+
String(resolveIdleSeconds()),
175+
OPENCODE_CMD,
176+
"run",
177+
// OpenCode variant argv is forwarded verbatim immediately after the
178+
// `run` subcommand and before the model selector.
179+
...invocationArgs,
180+
];
181+
if (model) {
182+
validateModelArg(model, "model");
183+
args.push("--model", model);
184+
}
185+
// `opencode run` consumes the task message as a positional argument.
186+
args.push(guardedPrompt);
187+
return args;
188+
}
189+
190+
/**
191+
* Keep the idle-termination reason in the durable provider transcript, along
192+
* with whatever the provider wrote to stderr. Stderr is normally dropped on a
193+
* successful exit, but an idle-terminated run reports success without the
194+
* provider ever having said it was finished — so its own diagnostics are the
195+
* only evidence of what it actually did, and discarding them would leave a
196+
* "completed" record that cannot be explained after the fact.
197+
*/
198+
function annotateIdleTermination(output, stderr) {
199+
const parts = [];
200+
const body = typeof output === "string" ? output : "";
201+
if (body !== "") parts.push(body.endsWith("\n") ? body.slice(0, -1) : body);
202+
parts.push(IDLE_TERMINATION_NOTE);
203+
// The supervisor's own heartbeat shares fd 2 with the provider; strip it so
204+
// the recorded diagnostic is the provider's words only.
205+
const diagnostic = (typeof stderr === "string" ? stderr : "")
206+
.split("\n")
207+
.filter((line) => !line.startsWith("switchyard: "))
208+
.join("\n")
209+
.trim();
210+
if (diagnostic !== "") {
211+
const bounded =
212+
diagnostic.length > MAX_IDLE_STDERR_CHARS
213+
? `${diagnostic.slice(0, MAX_IDLE_STDERR_CHARS)}… (truncated)`
214+
: diagnostic;
215+
parts.push(`[switchyard] provider stderr at termination:\n${bounded}`);
216+
}
217+
return `${parts.join("\n")}\n`;
218+
}
219+
21220
function hasNonTrivialCredential(containerName) {
22221
try {
23222
execFileSync(
@@ -53,47 +252,26 @@ export function execute(prompt, workingContainerName, options = {}) {
53252
const { model, timeoutMs = PROVIDER_EXECUTION_TIMEOUT_MS } = options;
54253
const guardedPrompt = addProviderPromptGuardrail(prompt);
55254

255+
let args;
56256
try {
257+
// Validated ahead of the descriptor so an unsafe container name is still
258+
// reported first, as it was before the supervisor wrapper.
57259
validateIdentifier(workingContainerName, "workingContainerName");
58-
} catch (error) {
59-
return { output: "", success: false, error: error.message };
60-
}
61-
let invocationArgs;
62-
try {
63-
invocationArgs = validateAdapterInvocation(options, {
260+
const invocationArgs = validateAdapterInvocation(options, {
64261
expectedHarness: "opencode",
65262
expectedTargetId: options.resolvedTargetId,
66263
expectedModel: model,
67264
});
265+
args = buildSupervisedArgs(
266+
workingContainerName,
267+
invocationArgs,
268+
model,
269+
guardedPrompt,
270+
);
68271
} catch (error) {
69272
return { output: "", success: false, error: error.message };
70273
}
71274

72-
const args = [
73-
"exec",
74-
"-i",
75-
"-w",
76-
"/project",
77-
workingContainerName,
78-
OPENCODE_CMD,
79-
"run",
80-
// OpenCode variant argv is forwarded verbatim immediately after the
81-
// `run` subcommand and before the model selector.
82-
...invocationArgs,
83-
];
84-
if (model) {
85-
try {
86-
validateModelArg(model, "model");
87-
} catch (error) {
88-
return { output: "", success: false, error: error.message };
89-
}
90-
args.push("--model", model);
91-
}
92-
// `opencode run` consumes the task message as a positional argument. Keep
93-
// stdin populated for compatibility with wrappers that still read it, but
94-
// do not rely on stdin as the only prompt transport.
95-
args.push(guardedPrompt);
96-
97275
try {
98276
const result = execFileSync("docker", args, {
99277
input: guardedPrompt,
@@ -105,6 +283,16 @@ export function execute(prompt, workingContainerName, options = {}) {
105283

106284
return { output: result, success: true };
107285
} catch (error) {
286+
// The supervisor terminated a finished-but-not-exiting provider. The work
287+
// is already in the working tree, so this is a success for the runner and
288+
// the diff still goes through the integration gate like any other.
289+
if (error.status === IDLE_EXIT_CODE) {
290+
return {
291+
output: annotateIdleTermination(error.stdout, error.stderr),
292+
success: true,
293+
idleTerminated: true,
294+
};
295+
}
108296
const timedOut = error.code === "ETIMEDOUT";
109297
if (timedOut) {
110298
killOrphanedProcesses(workingContainerName);
@@ -147,30 +335,28 @@ export async function executeAsync(prompt, workingContainerName, options = {}) {
147335
expectedTargetId: options.resolvedTargetId,
148336
expectedModel: model,
149337
});
150-
const args = [
151-
"exec",
152-
"-i",
153-
"-w",
154-
"/project",
338+
const args = buildSupervisedArgs(
155339
workingContainerName,
156-
OPENCODE_CMD,
157-
"run",
158-
...invocationArgs,
159-
];
160-
if (model) {
161-
validateModelArg(model, "model");
162-
args.push("--model", model);
163-
}
164-
args.push(guardedPrompt);
165-
return await executeProviderInvocation("docker", args, {
340+
invocationArgs,
341+
model,
342+
guardedPrompt,
343+
);
344+
const result = await executeProviderInvocation("docker", args, {
166345
...options,
167346
provider: "opencode",
168347
input: guardedPrompt,
169348
timeoutMs,
170349
signal,
171350
onPoll,
351+
idleExitCode: IDLE_EXIT_CODE,
172352
cleanup: () => killOrphanedProcessesAsync(workingContainerName),
173353
});
354+
if (!result.idleTerminated) return result;
355+
const { stderr, ...rest } = result;
356+
return {
357+
...rest,
358+
output: annotateIdleTermination(result.output, stderr),
359+
};
174360
} catch (error) {
175361
return { output: "", success: false, error: error.message };
176362
}

‎src/switchyard/adapter/provider-lifecycle.mjs‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,30 @@ function truncateDiagnostic(value, maxChars = DEFAULT_DIAGNOSTIC_CHARS) {
232232
* this boundary and remain bounded before callers can persist/report them.
233233
*/
234234
export async function executeProviderInvocation(command, args, options = {}) {
235-
const { provider, cleanup, ...lifecycleOptions } = options;
235+
const { provider, cleanup, idleExitCode, ...lifecycleOptions } = options;
236236
const result = await runProviderProcess(command, args, {
237237
...lifecycleOptions,
238238
cleanup,
239239
});
240240
if (result.success) return { output: result.output, success: true };
241+
// A provider whose container-side supervisor reports this reserved exit code
242+
// finished its work but could not exit on its own (see opencode.mjs). The
243+
// work is in the working tree, so it is mapped to success and the captured
244+
// diff still passes through the integration gate.
245+
if (
246+
typeof idleExitCode === "number" &&
247+
result.code === idleExitCode &&
248+
!result.timedOut &&
249+
!result.cancelled &&
250+
!result.error
251+
) {
252+
return {
253+
output: result.output,
254+
stderr: result.stderr,
255+
success: true,
256+
idleTerminated: true,
257+
};
258+
}
241259
if (result.timedOut) {
242260
return {
243261
output: result.output,

0 commit comments

Comments
 (0)