@@ -81,6 +81,26 @@ 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.
86+ let writeMetadataFlushed = false
87+ // Journal payload builder shared by the success-path flush and the
88+ // catch-path flush for writes recorded before an unexpected throw.
89+ 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+ } ) )
84104
85105 try {
86106 // Validate required parameters
@@ -129,10 +149,12 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
129149 }
130150
131151 // Process each file change. The handlers report whether their file
132- // operation succeeded (which controls the whole-patch checkpoint) and
133- // whether it actually wrote a file (which controls the change journal
134- // — a no-op update must not be journaled). A rejected approval or a
135- // failed local write never gets checkpointed as a success.
152+ // operation succeeded (which controls the whole-patch checkpoint); each
153+ // write is recorded through the onWrote callback at the moment it lands
154+ // on disk, so a throw from post-write bookkeeping cannot lose the
155+ // journal entry and change card for a completed write (a no-op update
156+ // never calls the callback). A rejected approval or a failed local
157+ // write never gets checkpointed as a success.
136158 let patchSucceeded = true
137159 for ( const change of changes ) {
138160 const relPath = change . path
@@ -163,11 +185,9 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
163185 task ,
164186 callbacks ,
165187 isWriteProtected ,
188+ ( ) => successfulChanges . push ( change ) ,
166189 )
167190 patchSucceeded = addResult . succeeded && patchSucceeded
168- if ( addResult . wrote ) {
169- successfulChanges . push ( change )
170- }
171191 if ( addResult . resultMessage ) {
172192 bufferedResults . push ( addResult . resultMessage )
173193 }
@@ -180,11 +200,9 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
180200 task ,
181201 callbacks ,
182202 isWriteProtected ,
203+ ( ) => successfulChanges . push ( change ) ,
183204 )
184205 patchSucceeded = deleteResult . succeeded && patchSucceeded
185- if ( deleteResult . wrote ) {
186- successfulChanges . push ( change )
187- }
188206 if ( deleteResult . resultMessage ) {
189207 bufferedResults . push ( deleteResult . resultMessage )
190208 }
@@ -197,11 +215,9 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
197215 task ,
198216 callbacks ,
199217 isWriteProtected ,
218+ ( ) => successfulChanges . push ( change ) ,
200219 )
201220 patchSucceeded = updateResult . succeeded && patchSucceeded
202- if ( updateResult . wrote ) {
203- successfulChanges . push ( change )
204- }
205221 if ( updateResult . resultMessage ) {
206222 bufferedResults . push ( updateResult . resultMessage )
207223 }
@@ -226,6 +242,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
226242 // pushToolResult. A fully failed patch (nothing written) leaves no
227243 // checkpoint behind.
228244 if ( patchSucceeded || successfulChanges . length > 0 ) {
245+ writeMetadataFlushed = true
229246 const perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
230247 if ( perWriteCheckpoints !== false ) {
231248 // B2: one journal entry per file that was actually written by
@@ -239,25 +256,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
239256 // staging/commit/journal/change-card work. checkpointSave rejects with a
240257 // CheckpointPartialPersistenceError when a committed checkpoint's
241258 // journal update fails; the catch below reports it.
242- await checkpointSave (
243- task ,
244- false ,
245- true ,
246- successfulChanges . map ( ( change ) => ( {
247- path : change . movePath ?? change . path ,
248- operation : change . type === "add" ? "create" : change . type ,
249- ...( change . diffStats
250- ? {
251- diffStats : {
252- additions : change . diffStats . added ,
253- deletions : change . diffStats . removed ,
254- } ,
255- }
256- : { } ) ,
257- ...( change . diff ? { diff : change . diff } : { } ) ,
258- ...( change . autoApproved ? { autoApproved : true } : { } ) ,
259- } ) ) ,
260- )
259+ await checkpointSave ( task , false , true , journalEntries ( ) )
261260 }
262261 }
263262
@@ -270,6 +269,18 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
270269 pushToolResult ( message )
271270 }
272271 } catch ( error ) {
272+ // A handler that throws after recording its write (post-write
273+ // bookkeeping such as trackFileContext) skips the success-path flush
274+ // above. Persist the recorded writes here, awaited, before the generic
275+ // error handling: the writes are on disk and the journal must document
276+ // them. A flush already attempted above is not repeated, so a journal
277+ // failure keeps the documented drop-buffered-results behavior.
278+ if ( ! writeMetadataFlushed && successfulChanges . length > 0 ) {
279+ const perWriteCheckpoints = ( await task . providerRef ?. deref ( ) ?. getState ( ) ) ?. perWriteCheckpoints
280+ if ( perWriteCheckpoints !== false ) {
281+ await checkpointSave ( task , false , true , journalEntries ( ) ) . catch ( ( ) => { } )
282+ }
283+ }
273284 await handleError ( "apply patch" , error as Error )
274285 await task . diffViewProvider . reset ( )
275286 } finally {
@@ -291,6 +302,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
291302 task : Task ,
292303 callbacks : ToolCallbacks ,
293304 isWriteProtected : boolean ,
305+ onWrote ?: ( ) => void ,
294306 ) : Promise < ApplyPatchFileOpResult > {
295307 const { askApproval, pushToolResult } = callbacks
296308
@@ -381,6 +393,11 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
381393 await task . diffViewProvider . saveChanges ( diagnosticsEnabled , writeDelayMs )
382394 }
383395
396+ // Record the completed write before post-write bookkeeping can fail:
397+ // a throw from trackFileContext must not lose the journal entry (and
398+ // change card) for a file that is already on disk.
399+ onWrote ?.( )
400+
384401 // Track file edit operation
385402 await task . fileContextTracker . trackFileContext ( relPath , "roo_edited" as RecordSource )
386403 task . didEditFile = true
@@ -397,6 +414,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
397414 task : Task ,
398415 callbacks : ToolCallbacks ,
399416 isWriteProtected : boolean ,
417+ onWrote ?: ( ) => void ,
400418 ) : Promise < ApplyPatchFileOpResult > {
401419 const { askApproval, pushToolResult } = callbacks
402420
@@ -456,6 +474,10 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
456474 return { succeeded : false , wrote : false }
457475 }
458476
477+ // The deletion is complete; record it before the buffered result can be
478+ // dropped by a later failure.
479+ onWrote ?.( )
480+
459481 task . didEditFile = true
460482 return { succeeded : true , wrote : true , resultMessage : `Successfully deleted ${ relPath } ` }
461483 }
@@ -467,6 +489,7 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
467489 task : Task ,
468490 callbacks : ToolCallbacks ,
469491 isWriteProtected : boolean ,
492+ onWrote ?: ( ) => void ,
470493 ) : Promise < ApplyPatchFileOpResult > {
471494 const { askApproval, pushToolResult } = callbacks
472495
@@ -633,6 +656,10 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
633656 pushToolResult ( formatResponse . toolError ( errorMessage ) )
634657 }
635658
659+ // The destination write is on disk whether or not the source unlink
660+ // succeeded; record it before bookkeeping can fail.
661+ onWrote ?.( )
662+
636663 await task . fileContextTracker . trackFileContext ( change . movePath , "roo_edited" as RecordSource )
637664 } else {
638665 // Save changes to the same file
@@ -642,6 +669,9 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
642669 await task . diffViewProvider . saveChanges ( diagnosticsEnabled , writeDelayMs )
643670 }
644671
672+ // Record the completed write before post-write bookkeeping can fail.
673+ onWrote ?.( )
674+
645675 await task . fileContextTracker . trackFileContext ( relPath , "roo_edited" as RecordSource )
646676 }
647677
0 commit comments