1+ import os from "node:os" ;
12import path from "node:path" ;
23import { WORKER_ID } from "./env" ;
34import { client , postInfo } from "./trpc" ;
@@ -6,6 +7,12 @@ import { jobLog } from "./logging";
67import { runPlanAgent } from "./plan" ;
78import { runOpencodeStreaming } from "./opencode" ;
89
10+ interface IndividualPlan {
11+ index : number ;
12+ planMd : string ;
13+ sessionId : string ;
14+ }
15+
916function buildSynthesisPrompt (
1017 job : Job ,
1118 plans : { index : number ; planMd : string } [ ] ,
@@ -52,31 +59,136 @@ async function waitForHardworkSelection(
5259 }
5360}
5461
62+ /** Parse the `hardworkIndividualPlans` JSON column into a typed array, tolerating bad/missing data. */
63+ function parseIndividualPlans ( raw : string | null | undefined ) : IndividualPlan [ ] {
64+ if ( ! raw ) return [ ] ;
65+ try {
66+ const parsed = JSON . parse ( raw ) ;
67+ if ( Array . isArray ( parsed ) ) {
68+ return parsed . filter ( ( p ) : p is IndividualPlan =>
69+ ! ! p && typeof p . index === "number" && typeof p . planMd === "string" ) ;
70+ }
71+ } catch {
72+ // ignore malformed JSON
73+ }
74+ return [ ] ;
75+ }
76+
77+ /** Persist a single completed plan-agent result immediately, so it survives a worker restart. */
78+ async function savePlanProgress ( jobId : number , plan : IndividualPlan ) : Promise < void > {
79+ await client . hardworkPlanProgress . mutate ( {
80+ jobId,
81+ index : plan . index ,
82+ planMd : plan . planMd ,
83+ sessionId : plan . sessionId ,
84+ } ) . catch ( ( err ) => {
85+ jobLog ( jobId , `Failed to persist hardwork plan ${ plan . index } progress (non-fatal): ${ err } ` ) ;
86+ } ) ;
87+ }
88+
89+ /**
90+ * Runs the full hardwork flow: N parallel plan agents followed by a
91+ * synthesis pass. Restart-safe — already-completed individual plans (whether
92+ * checkpointed in the DB via `hardworkIndividualPlans`, or recovered from
93+ * plan files left on disk by a previous run) are reused rather than re-run.
94+ */
5595async function runHardworkFlow (
5696 job : Job ,
5797 worktreePath : string ,
5898 issueNumber : number | null ,
5999 helperPath : string ,
60100) : Promise < { planMd : string ; sessionId : string } | null > {
61- await postInfo ( job . id , `Running ${ job . parallelPlanCount } parallel plan agents...` ) ;
101+ const total = job . parallelPlanCount ;
102+ const planDir = path . join ( worktreePath , ".opencode" , "plans" ) ;
62103
63- const planPromises = Array . from (
64- { length : job . parallelPlanCount } ,
65- ( _ , i ) => runPlanAgent ( job , worktreePath , issueNumber , helperPath , `hardwork-${ job . id } -${ i } ` , false ) ,
66- ) ;
104+ // ── Recover already-completed individual plans ─────────────────────────
105+ const completed = new Map < number , IndividualPlan > ( ) ;
106+ for ( const p of parseIndividualPlans ( job . hardworkIndividualPlans ) ) {
107+ completed . set ( p . index , p ) ;
108+ }
109+
110+ const recoveredFromDisk : number [ ] = [ ] ;
111+ for ( let i = 0 ; i < total ; i ++ ) {
112+ if ( completed . has ( i ) ) continue ;
113+
114+ // Fall back to plan files left on disk by a previous run that crashed
115+ // before its progress could be persisted to the DB (or before this
116+ // checkpointing existed at all).
117+ const planFilePath = path . join ( planDir , `plan-hardwork-${ job . id } -${ i } .md` ) ;
118+ const file = Bun . file ( planFilePath ) ;
119+ if ( await file . exists ( ) ) {
120+ const content = await file . text ( ) ;
121+ if ( content . trim ( ) . length > 0 ) {
122+ completed . set ( i , { index : i , planMd : content , sessionId : "" } ) ;
123+ recoveredFromDisk . push ( i ) ;
124+ }
125+ }
126+ }
127+
128+ if ( recoveredFromDisk . length > 0 ) {
129+ jobLog ( job . id , `Recovered ${ recoveredFromDisk . length } plan(s) from disk (indices: ${ recoveredFromDisk . join ( ", " ) } )` ) ;
130+ // Persist immediately so a future restart doesn't need to re-scan disk.
131+ await Promise . all ( recoveredFromDisk . map ( i => savePlanProgress ( job . id , completed . get ( i ) ! ) ) ) ;
132+ }
67133
68- const results = await Promise . allSettled ( planPromises ) ;
69- const successfulPlans = results
70- . filter ( ( r ) : r is PromiseFulfilledResult < { planMd : string ; sessionId : string } > => r . status === "fulfilled" )
71- . map ( ( r , i ) => ( { index : i , planMd : r . value . planMd , sessionId : r . value . sessionId } ) ) ;
134+ const pending : number [ ] = [ ] ;
135+ for ( let i = 0 ; i < total ; i ++ ) {
136+ if ( ! completed . has ( i ) ) pending . push ( i ) ;
137+ }
138+
139+ if ( pending . length === 0 && completed . size > 0 ) {
140+ jobLog ( job . id , `All ${ total } hardwork plan agents already completed in a previous run — skipping straight to synthesis` ) ;
141+ await postInfo ( job . id , `All ${ total } plan agents already completed in a previous run — proceeding to synthesis...` ) ;
142+ } else {
143+ if ( completed . size > 0 ) {
144+ jobLog ( job . id , `Resuming hardwork: ${ completed . size } /${ total } plan agent(s) already done, running remaining ${ pending . length } ` ) ;
145+ await postInfo ( job . id , `Resuming — ${ completed . size } /${ total } plan agents already done, running remaining ${ pending . length } ...` ) ;
146+ } else {
147+ await postInfo ( job . id , `Running ${ total } parallel plan agents...` ) ;
148+ }
72149
73- if ( successfulPlans . length === 0 ) {
150+ // Each parallel plan agent gets its own isolated opencode session
151+ // database. Running multiple `opencode run` processes concurrently
152+ // against the same project shares a single SQLite session DB, which
153+ // under contention can cause a process to hang silently (e.g. stuck
154+ // mid tool-call while writing session state for a `read`). Giving each
155+ // agent its own DB file avoids that contention entirely. These agents
156+ // are fire-and-forget (no later `--session --continue`), so isolation
157+ // is safe here.
158+ const tmpDir = os . tmpdir ( ) ;
159+ const results = await Promise . allSettled ( pending . map ( async ( i ) => {
160+ const dbPath = path . join ( tmpDir , `opencode-hardwork-${ job . id } -${ i } .db` ) ;
161+ const result = await runPlanAgent (
162+ job , worktreePath , issueNumber , helperPath , `hardwork-${ job . id } -${ i } ` , false ,
163+ { OPENCODE_DB : dbPath } ,
164+ ) ;
165+ const plan : IndividualPlan = { index : i , planMd : result . planMd , sessionId : result . sessionId } ;
166+ await savePlanProgress ( job . id , plan ) ;
167+ return plan ;
168+ } ) ) ;
169+
170+ for ( let idx = 0 ; idx < pending . length ; idx ++ ) {
171+ const i = pending [ idx ] ! ;
172+ const r = results [ idx ] ! ;
173+ if ( r . status === "fulfilled" ) {
174+ completed . set ( i , r . value ) ;
175+ } else {
176+ jobLog ( job . id , `Hardwork plan agent ${ i } failed: ${ r . reason instanceof Error ? r . reason . message : String ( r . reason ) } ` ) ;
177+ }
178+ }
179+ }
180+
181+ if ( completed . size === 0 ) {
74182 throw new Error ( "All parallel plan agents failed" ) ;
75183 }
76184
185+ const successfulPlans = [ ...completed . values ( ) ] . toSorted ( ( a , b ) => a . index - b . index ) ;
186+ if ( successfulPlans . length < total ) {
187+ jobLog ( job . id , `Proceeding to synthesis with ${ successfulPlans . length } /${ total } successful plan(s)` ) ;
188+ }
189+
77190 await postInfo ( job . id , "Synthesizing plans into final plan..." ) ;
78191
79- const planDir = path . join ( worktreePath , ".opencode" , "plans" ) ;
80192 const synthesisPlanFile = path . join ( planDir , `plan-synthesis-${ job . id } .md` ) ;
81193 const synthesisPrompt = buildSynthesisPrompt ( job , successfulPlans , issueNumber , synthesisPlanFile ) ;
82194
0 commit comments