@@ -448,17 +448,31 @@ export class SupervisorManager {
448448 updatedAt : Date . now ( ) ,
449449 } ;
450450
451+ const rollbackPatch = this . toSupervisorUpdatePatch ( current , Date . now ( ) ) ;
452+ let updated = this . attachCycles ( this . deps . supervisorRepo . update ( id , nextPatch ) ) ;
453+
451454 if ( objectiveChanged ) {
452- await this . deps . targetStore . resetTargetFiles ( workspace . path , {
453- targetId : current . targetId ,
454- sessionId : current . sessionId ,
455- workspaceId : current . workspaceId ,
456- objective : nextObjective ,
457- createdAt : nextPatch . updatedAt ?? Date . now ( ) ,
458- } ) ;
455+ try {
456+ await this . deps . targetStore . resetTargetFiles ( workspace . path , {
457+ targetId : current . targetId ,
458+ sessionId : current . sessionId ,
459+ workspaceId : current . workspaceId ,
460+ objective : nextObjective ,
461+ createdAt : nextPatch . updatedAt ?? Date . now ( ) ,
462+ } ) ;
463+ } catch ( error ) {
464+ try {
465+ this . deps . supervisorRepo . update ( id , rollbackPatch ) ;
466+ } catch ( rollbackError ) {
467+ this . logger . error (
468+ { err : rollbackError , supervisorId : id } ,
469+ "Failed to roll back supervisor after target reset failure"
470+ ) ;
471+ }
472+ throw error ;
473+ }
459474 }
460475
461- const updated = this . attachCycles ( this . deps . supervisorRepo . update ( id , nextPatch ) ) ;
462476 const enriched = await this . attachTargetState ( updated , workspace . path ) ;
463477
464478 this . storeSnapshot ( enriched ) ;
@@ -782,8 +796,7 @@ export class SupervisorManager {
782796 return finalized . cycle ;
783797 } catch ( error : unknown ) {
784798 if ( isSupervisorEvalAborted ( error ) ) {
785- const cancelled =
786- this . pendingPauses . has ( supervisorId ) || this . pendingObjectiveUpdates . has ( supervisorId ) ;
799+ const cancelled = this . isCancellationRequested ( supervisorId ) ;
787800 const abortedCycle = this . deps . cycleRepo . update ( activeCycle . id , {
788801 status : cancelled ? "cancelled" : "failed" ,
789802 errorReason : cancelled ? null : messageOf ( error , "Supervisor evaluator aborted" ) ,
@@ -1043,12 +1056,11 @@ export class SupervisorManager {
10431056 } ;
10441057 } catch ( error ) {
10451058 if ( isSupervisorEvalAborted ( error ) ) {
1059+ const cancelled = this . isCancellationRequested ( supervisor . id ) ;
10461060 this . deps . cycleAttemptRepo . update ( attempt . id , {
1047- status : this . pendingPauses . has ( supervisor . id ) ? "cancelled" : "failed" ,
1061+ status : cancelled ? "cancelled" : "failed" ,
10481062 completedAt : Date . now ( ) ,
1049- errorReason : this . pendingPauses . has ( supervisor . id )
1050- ? null
1051- : messageOf ( error , "Supervisor evaluator aborted" ) ,
1063+ errorReason : cancelled ? null : messageOf ( error , "Supervisor evaluator aborted" ) ,
10521064 } ) ;
10531065 throw error ;
10541066 }
@@ -1318,6 +1330,30 @@ export class SupervisorManager {
13181330 } ;
13191331 }
13201332
1333+ private isCancellationRequested ( supervisorId : string ) : boolean {
1334+ return this . pendingPauses . has ( supervisorId ) || this . pendingObjectiveUpdates . has ( supervisorId ) ;
1335+ }
1336+
1337+ private toSupervisorUpdatePatch (
1338+ supervisor : Supervisor ,
1339+ updatedAt : number
1340+ ) : Parameters < SupervisorRepo [ "update" ] > [ 1 ] {
1341+ return {
1342+ state : supervisor . state ,
1343+ objective : supervisor . objective ,
1344+ evaluatorProviderId : supervisor . evaluatorProviderId ,
1345+ evaluatorModel : supervisor . evaluatorModel ?? null ,
1346+ maxSupervisionCount : supervisor . maxSupervisionCount ,
1347+ completedSupervisionCount : supervisor . completedSupervisionCount ,
1348+ scheduledAt : supervisor . scheduledAt ?? null ,
1349+ stopReason : supervisor . stopReason ?? null ,
1350+ lastCycleAt : supervisor . lastCycleAt ?? null ,
1351+ lastEvaluatedTurnId : supervisor . lastEvaluatedTurnId ?? null ,
1352+ errorReason : supervisor . errorReason ?? null ,
1353+ updatedAt,
1354+ } ;
1355+ }
1356+
13211357 private applyEvaluationToTargetMemory (
13221358 memory : SupervisorTargetMemory ,
13231359 evaluation : Awaited < ReturnType < SupervisorEvaluator [ "evaluate" ] > > ,
0 commit comments