@@ -246,28 +246,25 @@ export class LocalAgentManager {
246246 } ) ) ;
247247 }
248248
249- const updated = this . store . updateResult ( record . id , {
250- status : "running" ,
249+ const begun = this . store . beginTurnResult ( record . id , {
250+ prompt ,
251251 model : overrides . model ?? record . model ,
252252 effort : overrides . effort ?? record . effort ,
253- latestResponse : undefined ,
254- error : undefined ,
255- errorCode : undefined ,
256- errorRetryable : undefined ,
257253 } ) ;
258- if ( updated . isErr ( ) ) return updated ;
254+ if ( begun . isErr ( ) ) return begun ;
259255 // Defer invocation until after the tracking entry is visible. This keeps
260256 // cleanup correct even if runTurn later gains a synchronous completion path.
261257 const turn = Promise . resolve ( ) . then ( ( ) => (
262- this . runTurn ( updated . value , prompt , overrides , workspaceId )
258+ this . runTurn ( begun . value . agent , begun . value . turn . id , prompt , overrides , workspaceId )
263259 ) ) ;
264260 this . activeTurns . set ( record . id , turn ) ;
265261 void turn . catch ( ( ) => undefined ) ;
266- return updated ;
262+ return Result . ok ( begun . value . agent ) ;
267263 }
268264
269265 private async runTurn (
270266 record : LocalAgentRecord ,
267+ turnId : number ,
271268 prompt : string ,
272269 overrides : RunOverrides ,
273270 workspaceId ?: string ,
@@ -281,7 +278,7 @@ export class LocalAgentManager {
281278 try {
282279 const authorized = this . authorizeWorkspace ( record . workspaceRoot , workspaceId , "run" ) ;
283280 if ( authorized . isErr ( ) ) {
284- this . persistRunError ( record , authorized . error , startedAt ) ;
281+ this . persistRunError ( record , turnId , authorized . error , startedAt ) ;
285282 return ;
286283 }
287284 const workspaceRoot = authorized . value ;
@@ -290,22 +287,22 @@ export class LocalAgentManager {
290287 : { ...record , workspaceRoot } ;
291288 const profiles = await this . loadProfilesResult ( workspaceRoot , record . profileName ) ;
292289 if ( profiles . isErr ( ) ) {
293- this . persistRunError ( record , profiles . error , startedAt ) ;
290+ this . persistRunError ( record , turnId , profiles . error , startedAt ) ;
294291 return ;
295292 }
296293 const profile = this . profileForRecordResult ( record , profiles . value ) ;
297294 if ( profile . isErr ( ) ) {
298- this . persistRunError ( record , profile . error , startedAt ) ;
295+ this . persistRunError ( record , turnId , profile . error , startedAt ) ;
299296 return ;
300297 }
301298 const input = this . buildRunInputResult ( authorizedRecord , profile . value , prompt , overrides ) ;
302299 if ( input . isErr ( ) ) {
303- this . persistRunError ( record , input . error , startedAt ) ;
300+ this . persistRunError ( record , turnId , input . error , startedAt ) ;
304301 return ;
305302 }
306303 const driver = this . driverResult ( record . provider , "run" , record . id ) ;
307304 if ( driver . isErr ( ) ) {
308- this . persistRunError ( record , driver . error , startedAt ) ;
305+ this . persistRunError ( record , turnId , driver . error , startedAt ) ;
309306 return ;
310307 }
311308 const context : LocalAgentRuntimeContext = {
@@ -329,20 +326,17 @@ export class LocalAgentManager {
329326 } ;
330327 const result = await this . pool . run ( driver . value , context , input . value , callbacks ) ;
331328 if ( result . isErr ( ) ) {
332- this . persistRunError ( record , result . error , startedAt ) ;
329+ this . persistRunError ( record , turnId , result . error , startedAt ) ;
333330 return ;
334331 }
335332 const runResult = result . value ;
336333 const current = this . store . getByIdResult ( record . id ) ;
337334 if ( current . isErr ( ) ) throw current . error ;
338335 if ( ! current . value ) return ;
339- const updated = this . store . updateResult ( record . id , {
336+ const updated = this . store . finishTurnResult ( record . id , turnId , {
340337 providerSessionId : runResult . providerSessionId ?? current . value . providerSessionId ,
341- status : "idle" ,
342- latestResponse : runResult . finalResponse ,
343- error : undefined ,
344- errorCode : undefined ,
345- errorRetryable : undefined ,
338+ status : "completed" ,
339+ response : runResult . finalResponse ,
346340 } ) ;
347341 if ( updated . isErr ( ) ) throw updated . error ;
348342 this . log ( "info" , "agent_run_completed" , {
@@ -353,11 +347,11 @@ export class LocalAgentManager {
353347 } ) ;
354348 } catch ( error ) {
355349 if ( isLocalAgentError ( error ) ) {
356- this . persistRunError ( record , error , startedAt ) ;
350+ this . persistRunError ( record , turnId , error , startedAt ) ;
357351 return ;
358352 }
359- const persisted = this . store . updateResult ( record . id , {
360- status : "error " ,
353+ const persisted = this . store . finishTurnResult ( record . id , turnId , {
354+ status : "failed " ,
361355 error : "Unexpected internal subagent failure." ,
362356 errorCode : "AGENT_INTERNAL_ERROR" ,
363357 errorRetryable : false ,
@@ -379,11 +373,12 @@ export class LocalAgentManager {
379373
380374 private persistRunError (
381375 record : LocalAgentRecord ,
376+ turnId : number ,
382377 error : LocalAgentError ,
383378 startedAt : number ,
384379 ) : void {
385- const persisted = this . store . updateResult ( record . id , {
386- status : "error " ,
380+ const persisted = this . store . finishTurnResult ( record . id , turnId , {
381+ status : "failed " ,
387382 error : error . message ,
388383 errorCode : error . code ,
389384 errorRetryable : error . retryable ,
0 commit comments