@@ -71,10 +71,34 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
7171 const { patch } = params
7272 const { handleError, pushToolResult } = callbacks
7373
74- // Declared outside the try so the catch can persist write metadata
75- // recorded before an unexpected throw.
74+ // Set once the success-path flush actually proceeds to checkpointSave, so
75+ // the catch below does not persist the same writes twice while a
76+ // settings-read rejection before that decision leaves the catch flush
77+ // armed with default-on semantics.
7678 let writeMetadataFlushed = false
79+ // Cached perWriteCheckpoints lookup shared by the success-path flush and
80+ // the catch flush: the catch must not call getState() again, because a
81+ // rejecting settings read must not skip persisting writes already on disk.
82+ let perWriteCheckpoints : boolean | undefined
7783 const successfulChanges : { change : ApplyPatchFileChange ; diffStats ?: DiffStats } [ ] = [ ]
84+ // Journal payload builder shared by the success-path flush and the
85+ // catch-path flush for writes recorded before an unexpected throw.
86+ const journalEntries = ( ) =>
87+ successfulChanges . flatMap ( ( { change, diffStats } ) => {
88+ const written = {
89+ path : change . movePath ?? change . path ,
90+ operation : change . type === "add" ? ( "create" as const ) : change . type ,
91+ diffStats : diffStats ? { additions : diffStats . added , deletions : diffStats . removed } : undefined ,
92+ }
93+ // A completed move also DELETES its source: journal that deletion
94+ // after the destination write so a step rollback restores the
95+ // removed file from its pre-step state. A move whose source unlink
96+ // failed left the source on disk and gets no deletion entry.
97+ if ( change . movePath && change . moveSourceDeleted ) {
98+ return [ written , { path : change . path , operation : "delete" as const , diffStats : undefined } ]
99+ }
100+ return [ written ]
101+ } )
78102
79103 try {
80104 // Validate required parameters
@@ -206,31 +230,21 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
206230 // pushToolResult. A fully failed patch (nothing written) leaves no
207231 // checkpoint behind.
208232 if ( patchSucceeded || successfulChanges . length > 0 ) {
209- writeMetadataFlushed = true
210- const perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
233+ perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
211234 if ( perWriteCheckpoints !== false ) {
235+ writeMetadataFlushed = true
212236 // B2: one journal entry per file that was actually written by
213237 // the patch (the simplest correct design for multi-file patches),
214238 // all referencing the single checkpoint above. A no-op update
215239 // contributes no entry because nothing was written. `movePath`,
216- // when present, is the file's final location. Each entry
217- // carries the approval-diff statistics the handler computed.
240+ // when present, is the file's final location, and a completed move
241+ // additionally journals its source deletion. Each entry carries the
242+ // approval-diff statistics the handler computed.
218243 // Await so the checkpoint (staging + commit + journal)
219244 // completes before queued messages are processed below;
220245 // otherwise a queued write's own checkpoint could interleave
221246 // with this commit.
222- await checkpointSave (
223- task ,
224- false ,
225- true ,
226- successfulChanges . map ( ( { change, diffStats } ) => ( {
227- path : change . movePath ?? change . path ,
228- operation : change . type === "add" ? "create" : change . type ,
229- diffStats : diffStats
230- ? { additions : diffStats . added , deletions : diffStats . removed }
231- : undefined ,
232- } ) ) ,
233- ) . catch ( ( ) => { } )
247+ await checkpointSave ( task , false , true , journalEntries ( ) ) . catch ( ( ) => { } )
234248 }
235249 }
236250
@@ -249,24 +263,27 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
249263 // the generic error handling: the writes are on disk and the journal
250264 // must document them. A flush that already ran (or was skipped by the
251265 // setting) is not attempted twice.
266+ let reportedError : unknown = error
252267 if ( ! writeMetadataFlushed && successfulChanges . length > 0 ) {
253- const perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
254268 if ( perWriteCheckpoints !== false ) {
255- await checkpointSave (
256- task ,
257- false ,
258- true ,
259- successfulChanges . map ( ( { change, diffStats } ) => ( {
260- path : change . movePath ?? change . path ,
261- operation : change . type === "add" ? "create" : change . type ,
262- diffStats : diffStats
263- ? { additions : diffStats . added , deletions : diffStats . removed }
264- : undefined ,
265- } ) ) ,
266- ) . catch ( ( ) => { } )
269+ let flushError : unknown
270+ await checkpointSave ( task , false , true , journalEntries ( ) ) . catch ( ( flushFailure ) => {
271+ flushError = flushFailure
272+ } )
273+ if ( flushError !== undefined ) {
274+ // The completed write lacks rollback metadata: report the
275+ // persistence failure alongside the bookkeeping error without
276+ // replacing it as the reported failure.
277+ const flushMessage = flushError instanceof Error ? flushError . message : String ( flushError )
278+ const baseMessage = error instanceof Error ? error . message : String ( error )
279+ console . error ( "[ApplyPatchTool] catch-path checkpoint flush failed:" , flushError )
280+ reportedError = new Error ( `${ baseMessage } (checkpoint flush also failed: ${ flushMessage } )` , {
281+ cause : error ,
282+ } )
283+ }
267284 }
268285 }
269- await handleError ( "apply patch" , error as Error )
286+ await handleError ( "apply patch" , reportedError as Error )
270287 await task . diffViewProvider . reset ( )
271288 }
272289 }
@@ -584,6 +601,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
584601 // checkpointed and journaled as a completed move.
585602 try {
586603 await fs . unlink ( absolutePath )
604+ change . moveSourceDeleted = true
587605 } catch ( error ) {
588606 moveSucceeded = false
589607 console . error ( `Failed to delete original file after move: ${ error } ` )
0 commit comments