11import type { Runtime , BackgroundHandle } from "@/node/runtime/Runtime" ;
22import { getErrorMessage } from "@/common/utils/errors" ;
33import { log } from "./log" ;
4- import * as fs from "fs/promises" ;
54import * as path from "path" ;
65
76/**
8- * Metadata persisted to meta.json for recovery across restarts
7+ * Metadata written to meta.json for bookkeeping
98 */
109export interface BackgroundProcessMeta {
1110 id : string ;
@@ -30,7 +29,7 @@ export interface BackgroundProcess {
3029 exitCode ?: number ; // Undefined if still running
3130 exitTime ?: number ; // Timestamp when exited (undefined if running)
3231 status : "running" | "exited" | "killed" | "failed" ;
33- handle : BackgroundHandle | null ; // For process interaction (null after recovery)
32+ handle : BackgroundHandle ; // For process interaction
3433}
3534
3635/**
@@ -125,13 +124,7 @@ export class BackgroundProcessManager {
125124 } ;
126125 const metaJson = JSON . stringify ( meta , null , 2 ) ;
127126
128- // Use handle if available, otherwise write directly to file
129- if ( proc . handle ) {
130- await proc . handle . writeMeta ( metaJson ) ;
131- } else {
132- const metaPath = path . join ( proc . outputDir , "meta.json" ) ;
133- await fs . writeFile ( metaPath , metaJson ) ;
134- }
127+ await proc . handle . writeMeta ( metaJson ) ;
135128 }
136129
137130 /**
@@ -144,7 +137,7 @@ export class BackgroundProcessManager {
144137 if ( ! proc ) return null ;
145138
146139 // Refresh status if still running (exit code null = still running)
147- if ( proc . status === "running" && proc . handle ) {
140+ if ( proc . status === "running" ) {
148141 const exitCode = await proc . handle . getExitCode ( ) ;
149142 if ( exitCode !== null ) {
150143 log . debug ( `Background process ${ proc . id } has exited` ) ;
@@ -179,12 +172,10 @@ export class BackgroundProcessManager {
179172 */
180173 private async refreshRunningStatuses ( ) : Promise < void > {
181174 const runningProcesses = Array . from ( this . processes . values ( ) ) . filter (
182- ( p ) => p . status === "running" && p . handle
175+ ( p ) => p . status === "running"
183176 ) ;
184177
185178 for ( const proc of runningProcesses ) {
186- if ( ! proc . handle ) continue ;
187-
188179 const exitCode = await proc . handle . getExitCode ( ) ;
189180 if ( exitCode !== null ) {
190181 log . debug ( `Background process ${ proc . id } has exited` ) ;
@@ -220,17 +211,6 @@ export class BackgroundProcessManager {
220211 return { success : true } ;
221212 }
222213
223- // Check if we have a valid handle
224- if ( ! proc . handle ) {
225- log . debug ( `Process ${ processId } has no handle, marking as failed` ) ;
226- proc . status = "failed" ;
227- proc . exitTime = Date . now ( ) ;
228- await this . updateMetaFile ( proc ) . catch ( ( err : unknown ) => {
229- log . debug ( `BackgroundProcessManager: Failed to update meta.json: ${ getErrorMessage ( err ) } ` ) ;
230- } ) ;
231- return { success : true } ;
232- }
233-
234214 try {
235215 await proc . handle . terminate ( ) ;
236216
@@ -260,9 +240,7 @@ export class BackgroundProcessManager {
260240 log . debug ( `BackgroundProcessManager: Failed to update meta.json: ${ getErrorMessage ( err ) } ` ) ;
261241 } ) ;
262242 // Ensure handle is cleaned up even on error
263- if ( proc . handle ) {
264- await proc . handle . dispose ( ) ;
265- }
243+ await proc . handle . dispose ( ) ;
266244 return { success : true } ;
267245 }
268246 }
0 commit comments