@@ -908,8 +908,15 @@ 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+
911918 private buildDelta ( id : string , cached : HistoryItem , incoming : Partial < HistoryItem > ) : Partial < HistoryItem > {
912- return { id, ...computeHistoryDelta ( cached , incoming ) }
919+ return { id, ...this . computeDelta ( cached , incoming ) }
913920 }
914921
915922 /**
@@ -947,9 +954,10 @@ export class TaskHistoryStore {
947954 } ,
948955 } )
949956 return written
957+ } else {
958+ await safeWriteJson ( filePath , item )
959+ return item
950960 }
951- await safeWriteJson ( filePath , item )
952- return item
953961 }
954962
955963 private async reconcileTaskCache ( taskId : string ) : Promise < void > {
@@ -1244,11 +1252,13 @@ export class TaskHistoryStore {
12441252 const firstFileLock =
12451253 suppliedFirstFileLock ??
12461254 ( holdFirstFileLock ? await lockJsonFile ( await this . getTaskFilePath ( firstId ) ) : undefined )
1255+ const ownsFirstFileLock = Boolean ( firstFileLock && ! suppliedFirstFileLock )
12471256
12481257 try {
12491258 let firstDiskSnapshot ! : TaskFilePreImage
1259+ const firstDiskGuard = options ?. firstDiskGuard
12501260 const firstDelta = this . buildDelta ( firstId , first , updatedFirst )
1251- const writtenFirst = await this . writeTaskFile ( mergedFirst , firstDelta , options ?. firstDiskGuard , {
1261+ const writtenFirst = await this . writeTaskFile ( mergedFirst , firstDelta , firstDiskGuard , {
12521262 heldLock : firstFileLock ,
12531263 capturePreImage : ( preImage ) => ( firstDiskSnapshot = preImage ) ,
12541264 } )
@@ -1265,13 +1275,11 @@ export class TaskHistoryStore {
12651275 const firstRestoration = [ firstId , firstDiskSnapshot , expectedFirst , firstFileLock ] as const
12661276 const restorations = Array . of < TaskFileRestoration > ( firstRestoration )
12671277 if ( secondDiskSnapshot ) {
1278+ const mergeSecond = mergeWithDisk ( secondDelta )
12681279 const secondPreImage = isValidTaskFilePreImage ( secondDiskSnapshot )
12691280 ? secondDiskSnapshot
12701281 : null
1271- const expectedSecond = mergeWithDisk ( secondDelta ) (
1272- secondPreImage ,
1273- mergedSecond ,
1274- ) as HistoryItem
1282+ const expectedSecond = mergeSecond ( secondPreImage , mergedSecond ) as HistoryItem
12751283 const persistedSecond = JSON . parse ( JSON . stringify ( expectedSecond ) ) as HistoryItem
12761284 const expectedSecondStates = expectedTaskFileStates ( persistedSecond , secondDiskSnapshot )
12771285 restorations . unshift ( [ secondId , secondDiskSnapshot , expectedSecondStates ] )
@@ -1315,12 +1323,12 @@ export class TaskHistoryStore {
13151323 }
13161324 }
13171325
1318- if ( compensationErrors . length )
1319- throw new AggregateError ( Array . of ( error , ... compensationErrors ) , "Pair compensation failed" )
1326+ const callbackErrors = Array . of ( error , ... compensationErrors )
1327+ if ( compensationErrors . length ) throw new AggregateError ( callbackErrors , "Pair compensation failed" )
13201328 throw error
13211329 }
13221330 } finally {
1323- if ( firstFileLock && ! suppliedFirstFileLock ) await firstFileLock ( )
1331+ if ( ownsFirstFileLock ) await firstFileLock ! ( )
13241332 }
13251333 }
13261334 return options ?. storeLockAcquired ? update ( ) : this . withLock ( update )
0 commit comments