@@ -18,6 +18,205 @@ const OPENCODE_CMD = "opencode";
1818const CREDENTIALS_PATH = "/root/.local/share/opencode/auth.json" ;
1919const 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+
21220function 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 }
0 commit comments