@@ -86,6 +86,7 @@ export const runTask = async ({ run, task, publish, logger }: RunTaskOptions) =>
8686
8787 let taskStartedAt = Date . now ( )
8888 let taskFinishedAt : number | undefined
89+ let taskAbortedAt : number | undefined
8990 let taskMetricsId : number | undefined
9091 let rooTaskId : string | undefined
9192 let isClientDisconnected = false
@@ -162,9 +163,12 @@ export const runTask = async ({ run, task, publish, logger }: RunTaskOptions) =>
162163 await updateTaskMetrics ( taskMetricsId , { toolUsage } )
163164 }
164165
165- if ( eventName === RooCodeEventName . TaskAborted || eventName === RooCodeEventName . TaskCompleted ) {
166+ if ( eventName === RooCodeEventName . TaskAborted ) {
167+ taskAbortedAt = Date . now ( )
168+ }
169+
170+ if ( eventName === RooCodeEventName . TaskCompleted ) {
166171 taskFinishedAt = Date . now ( )
167- await updateTask ( task . id , { finishedAt : new Date ( ) } )
168172 }
169173 } )
170174
@@ -187,7 +191,10 @@ export const runTask = async ({ run, task, publish, logger }: RunTaskOptions) =>
187191 } )
188192
189193 try {
190- await pWaitFor ( ( ) => ! ! taskFinishedAt || isClientDisconnected , { interval : 1_000 , timeout : EVALS_TIMEOUT } )
194+ await pWaitFor ( ( ) => ! ! taskFinishedAt || ! ! taskAbortedAt || isClientDisconnected , {
195+ interval : 1_000 ,
196+ timeout : EVALS_TIMEOUT ,
197+ } )
191198 } catch ( _error ) {
192199 logger . error ( "time limit reached" )
193200
@@ -197,18 +204,27 @@ export const runTask = async ({ run, task, publish, logger }: RunTaskOptions) =>
197204 await new Promise ( ( resolve ) => setTimeout ( resolve , 5_000 ) ) // Allow some time for the task to cancel.
198205 }
199206
207+ taskFinishedAt = Date . now ( )
208+ }
209+
210+ if ( taskFinishedAt ) {
211+ logger . info ( "setting task finished at" )
200212 await updateTask ( task . id , { finishedAt : new Date ( ) } )
201213 }
202214
203- if ( isClientDisconnected ) {
215+ if ( ! taskFinishedAt && isClientDisconnected ) {
204216 logger . error ( "client disconnected before task finished" )
205- } else {
206- if ( rooTaskId ) {
207- logger . info ( "closing task" )
208- client . sendCommand ( { commandName : TaskCommandName . CloseTask , data : rooTaskId } )
209- await new Promise ( ( resolve ) => setTimeout ( resolve , 2_000 ) ) // Allow some time for the window to close.
210- }
217+ throw new Error ( "Client disconnected before task completion." )
218+ }
219+
220+ if ( rooTaskId && ! isClientDisconnected ) {
221+ logger . info ( "closing task" )
222+ client . sendCommand ( { commandName : TaskCommandName . CloseTask , data : rooTaskId } )
223+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2_000 ) ) // Allow some time for the window to close.
224+ }
211225
226+ if ( ! isClientDisconnected ) {
227+ logger . info ( "disconnecting client" )
212228 client . disconnect ( )
213229 }
214230
0 commit comments