@@ -908,15 +908,8 @@ export class TaskHistoryStore {
908908 }
909909 }
910910
911- /**
912- * Return only the fields in `incoming` that differ from `cached`.
913- */
914- private computeDelta ( cached : HistoryItem , incoming : Partial < HistoryItem > ) : Partial < HistoryItem > {
915- return computeHistoryDelta ( cached , incoming )
916- }
917-
918911 private buildDelta ( id : string , cached : HistoryItem , incoming : Partial < HistoryItem > ) : Partial < HistoryItem > {
919- return { id, ...this . computeDelta ( cached , incoming ) }
912+ return { id, ...computeHistoryDelta ( cached , incoming ) }
920913 }
921914
922915 /**
@@ -1245,20 +1238,17 @@ export class TaskHistoryStore {
12451238 // Merge with existing cache entries before writing, mirroring upsertCore.
12461239 const mergedFirst = { ...first , ...updatedFirst }
12471240 const mergedSecond = { ...second , ...updatedSecond }
1248- const holdFirstFileLock = Boolean (
1249- options ?. firstDiskGuard || options ?. rollbackBothOnCallbackFailure || options ?. whileFirstFileLocked ,
1250- )
1251- const suppliedFirstFileLock = options ?. firstFileLock
12521241 const firstFileLock =
1253- suppliedFirstFileLock ??
1254- ( holdFirstFileLock ? await lockJsonFile ( await this . getTaskFilePath ( firstId ) ) : undefined )
1255- const ownsFirstFileLock = Boolean ( firstFileLock && ! suppliedFirstFileLock )
1242+ options ?. firstFileLock ??
1243+ ( options ?. firstDiskGuard || options ?. rollbackBothOnCallbackFailure || options ?. whileFirstFileLocked
1244+ ? await lockJsonFile ( await this . getTaskFilePath ( firstId ) )
1245+ : undefined )
1246+ const ownsFirstFileLock = Boolean ( firstFileLock && ! options ?. firstFileLock )
12561247
12571248 try {
12581249 let firstDiskSnapshot ! : TaskFilePreImage
1259- const firstDiskGuard = options ?. firstDiskGuard
12601250 const firstDelta = this . buildDelta ( firstId , first , updatedFirst )
1261- const writtenFirst = await this . writeTaskFile ( mergedFirst , firstDelta , firstDiskGuard , {
1251+ const writtenFirst = await this . writeTaskFile ( mergedFirst , firstDelta , options ?. firstDiskGuard , {
12621252 heldLock : firstFileLock ,
12631253 capturePreImage : ( preImage ) => ( firstDiskSnapshot = preImage ) ,
12641254 } )
@@ -1271,18 +1261,26 @@ export class TaskHistoryStore {
12711261 } )
12721262 } catch ( error ) {
12731263 if ( options ?. rollbackBothOnCallbackFailure ) {
1274- const expectedFirst = Array . of ( JSON . parse ( JSON . stringify ( writtenFirst ) ) as HistoryItem )
1275- const firstRestoration = [ firstId , firstDiskSnapshot , expectedFirst , firstFileLock ] as const
1276- const restorations = Array . of < TaskFileRestoration > ( firstRestoration )
1264+ const restorations = Array . of < TaskFileRestoration > ( [
1265+ firstId ,
1266+ firstDiskSnapshot ,
1267+ Array . of ( JSON . parse ( JSON . stringify ( writtenFirst ) ) as HistoryItem ) ,
1268+ firstFileLock ,
1269+ ] )
12771270 if ( secondDiskSnapshot ) {
12781271 const mergeSecond = mergeWithDisk ( secondDelta )
1279- const secondPreImage = isValidTaskFilePreImage ( secondDiskSnapshot )
1280- ? secondDiskSnapshot
1281- : null
1282- const expectedSecond = mergeSecond ( secondPreImage , mergedSecond ) as HistoryItem
1283- const persistedSecond = JSON . parse ( JSON . stringify ( expectedSecond ) ) as HistoryItem
1284- const expectedSecondStates = expectedTaskFileStates ( persistedSecond , secondDiskSnapshot )
1285- restorations . unshift ( [ secondId , secondDiskSnapshot , expectedSecondStates ] )
1272+ const expectedSecond = mergeSecond (
1273+ isValidTaskFilePreImage ( secondDiskSnapshot ) ? secondDiskSnapshot : null ,
1274+ mergedSecond ,
1275+ ) as HistoryItem
1276+ restorations . unshift ( [
1277+ secondId ,
1278+ secondDiskSnapshot ,
1279+ expectedTaskFileStates (
1280+ JSON . parse ( JSON . stringify ( expectedSecond ) ) as HistoryItem ,
1281+ secondDiskSnapshot ,
1282+ ) ,
1283+ ] )
12861284 }
12871285 const rollbackErrors = await this . restoreTaskFilePreImages ( restorations )
12881286 if ( rollbackErrors . length )
@@ -1308,11 +1306,19 @@ export class TaskHistoryStore {
13081306 if ( ! options ?. rollbackBothOnCallbackFailure ) throw error
13091307
13101308 // Restore second before first, preserving the original compensation order.
1311- const expectedSecond = Array . of ( JSON . parse ( JSON . stringify ( writtenSecond ) ) as HistoryItem )
1312- const expectedFirst = Array . of ( JSON . parse ( JSON . stringify ( writtenFirst ) ) as HistoryItem )
13131309 const compensationErrors = await this . restoreTaskFilePreImages ( [
1314- [ secondId , secondDiskSnapshot ! , expectedSecond , undefined ] ,
1315- [ firstId , firstDiskSnapshot ! , expectedFirst , firstFileLock ] ,
1310+ [
1311+ secondId ,
1312+ secondDiskSnapshot ! ,
1313+ Array . of ( JSON . parse ( JSON . stringify ( writtenSecond ) ) as HistoryItem ) ,
1314+ undefined ,
1315+ ] ,
1316+ [
1317+ firstId ,
1318+ firstDiskSnapshot ! ,
1319+ Array . of ( JSON . parse ( JSON . stringify ( writtenFirst ) ) as HistoryItem ) ,
1320+ firstFileLock ,
1321+ ] ,
13161322 ] )
13171323
13181324 if ( this . onWrite ) {
@@ -1323,8 +1329,8 @@ export class TaskHistoryStore {
13231329 }
13241330 }
13251331
1326- const callbackErrors = Array . of ( error , ... compensationErrors )
1327- if ( compensationErrors . length ) throw new AggregateError ( callbackErrors , "Pair compensation failed" )
1332+ if ( compensationErrors . length )
1333+ throw new AggregateError ( Array . of ( error , ... compensationErrors ) , "Pair compensation failed" )
13281334 throw error
13291335 }
13301336 } finally {
0 commit comments