@@ -81,26 +81,42 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
8181 // messages when a write succeeded but the post-patch checkpoint failed.
8282 const successfulChanges : ApplyPatchFileChange [ ] = [ ]
8383 const bufferedResults : string [ ] = [ ]
84- // Set once the success-path checkpoint/journal flush has been attempted
85- // so the catch below does not persist the same writes twice.
84+ // Set once the success-path flush actually proceeds to checkpointSave, so
85+ // the catch below does not persist the same writes twice while a
86+ // settings-read rejection before that decision leaves the catch flush
87+ // armed with default-on semantics.
8688 let writeMetadataFlushed = false
89+ // Cached perWriteCheckpoints lookup shared by the success-path flush and
90+ // the catch flush: the catch must not call getState() again, because a
91+ // rejecting settings read must not skip persisting writes already on disk.
92+ let perWriteCheckpoints : boolean | undefined
8793 // Journal payload builder shared by the success-path flush and the
8894 // catch-path flush for writes recorded before an unexpected throw.
8995 const journalEntries = ( ) =>
90- successfulChanges . map ( ( change ) => ( {
91- path : change . movePath ?? change . path ,
92- operation : change . type === "add" ? ( "create" as const ) : change . type ,
93- ...( change . diffStats
94- ? {
95- diffStats : {
96- additions : change . diffStats . added ,
97- deletions : change . diffStats . removed ,
98- } ,
99- }
100- : { } ) ,
101- ...( change . diff ? { diff : change . diff } : { } ) ,
102- ...( change . autoApproved ? { autoApproved : true } : { } ) ,
103- } ) )
96+ successfulChanges . flatMap ( ( change ) => {
97+ const written = {
98+ path : change . movePath ?? change . path ,
99+ operation : change . type === "add" ? ( "create" as const ) : change . type ,
100+ ...( change . diffStats
101+ ? {
102+ diffStats : {
103+ additions : change . diffStats . added ,
104+ deletions : change . diffStats . removed ,
105+ } ,
106+ }
107+ : { } ) ,
108+ ...( change . diff ? { diff : change . diff } : { } ) ,
109+ ...( change . autoApproved ? { autoApproved : true } : { } ) ,
110+ }
111+ // A completed move also DELETES its source: journal that deletion
112+ // after the destination write so a step rollback restores the
113+ // removed file from its pre-step state. A move whose source unlink
114+ // failed left the source on disk and gets no deletion entry.
115+ if ( change . movePath && change . moveSourceDeleted ) {
116+ return [ written , { path : change . path , operation : "delete" as const } ]
117+ }
118+ return [ written ]
119+ } )
104120
105121 try {
106122 // Validate required parameters
@@ -242,14 +258,15 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
242258 // pushToolResult. A fully failed patch (nothing written) leaves no
243259 // checkpoint behind.
244260 if ( patchSucceeded || successfulChanges . length > 0 ) {
245- writeMetadataFlushed = true
246- const perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
261+ perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
247262 if ( perWriteCheckpoints !== false ) {
263+ writeMetadataFlushed = true
248264 // B2: one journal entry per file that was actually written by
249265 // the patch (the simplest correct design for multi-file patches),
250266 // all referencing the single checkpoint above. A no-op update
251267 // contributes no entry because nothing was written. `movePath`,
252- // when present, is the file's final location. B3a: the per-file
268+ // when present, is the file's final location, and a completed move
269+ // additionally journals its source deletion. B3a: the per-file
253270 // approval diff/stats and auto-approval state, retained by the
254271 // handlers, feed the per-step change card.
255272 // Awaited: a later write must not interleave with this patch's
@@ -275,13 +292,27 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
275292 // error handling: the writes are on disk and the journal must document
276293 // them. A flush already attempted above is not repeated, so a journal
277294 // failure keeps the documented drop-buffered-results behavior.
295+ let reportedError : unknown = error
278296 if ( ! writeMetadataFlushed && successfulChanges . length > 0 ) {
279- const perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
280297 if ( perWriteCheckpoints !== false ) {
281- await checkpointSave ( task , false , true , journalEntries ( ) ) . catch ( ( ) => { } )
298+ let flushError : unknown
299+ await checkpointSave ( task , false , true , journalEntries ( ) ) . catch ( ( flushFailure ) => {
300+ flushError = flushFailure
301+ } )
302+ if ( flushError !== undefined ) {
303+ // The completed write lacks rollback metadata: report the
304+ // persistence failure alongside the bookkeeping error without
305+ // replacing it as the reported failure.
306+ const flushMessage = flushError instanceof Error ? flushError . message : String ( flushError )
307+ const baseMessage = error instanceof Error ? error . message : String ( error )
308+ console . error ( "[ApplyPatchTool] catch-path checkpoint flush failed:" , flushError )
309+ reportedError = new Error ( `${ baseMessage } (checkpoint flush also failed: ${ flushMessage } )` , {
310+ cause : error ,
311+ } )
312+ }
282313 }
283314 }
284- await handleError ( "apply patch" , error as Error )
315+ await handleError ( "apply patch" , reportedError as Error )
285316 await task . diffViewProvider . reset ( )
286317 } finally {
287318 // B2: deferred from the per-file handlers - a queued write must not
@@ -646,6 +677,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
646677 // checkpointed and journaled as a completed move.
647678 try {
648679 await fs . unlink ( absolutePath )
680+ change . moveSourceDeleted = true
649681 } catch ( error ) {
650682 moveSucceeded = false
651683 console . error ( `Failed to delete original file after move: ${ error } ` )
0 commit comments