@@ -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+
1935export 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