@@ -195,6 +195,37 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
195195 expect ( mockTask . recordToolError ) . toHaveBeenCalledWith ( "apply_patch" )
196196 expect ( mockedCheckpointSave ) . not . toHaveBeenCalled ( )
197197 } )
198+
199+ it ( "awaits the post-patch checkpoint before processing queued messages" , async ( ) => {
200+ // A pending checkpoint must not let queued messages (and the
201+ // writes they trigger) start first: execute() awaits the
202+ // post-patch checkpointSave and only then processes queued
203+ // messages, so a queued write's own checkpoint cannot interleave
204+ // with this commit.
205+ const order : string [ ] = [ ]
206+ ; ( mockTask . processQueuedMessages as MockedFunction < ( ) => void > ) . mockImplementation ( ( ) => {
207+ order . push ( "processQueuedMessages" )
208+ } )
209+ mockedCheckpointSave . mockImplementationOnce ( ( ) => {
210+ order . push ( "checkpointSave:start" )
211+ return new Promise < void > ( ( resolve ) => {
212+ setTimeout ( ( ) => {
213+ order . push ( "checkpointSave:done" )
214+ resolve ( )
215+ } , 10 )
216+ } ) . then ( ( ) => undefined )
217+ } )
218+
219+ await tool . execute ( { patch : deletePatch } , mockTask as Task , {
220+ askApproval : mockAskApproval ,
221+ handleError : mockHandleError ,
222+ pushToolResult : mockPushToolResult ,
223+ } )
224+
225+ // The checkpoint fully completes before queued messages are
226+ // processed.
227+ expect ( order ) . toEqual ( [ "checkpointSave:start" , "checkpointSave:done" , "processQueuedMessages" ] )
228+ } )
198229 } )
199230
200231 describe ( "checkpoint only for fully successful patches (B1)" , ( ) => {
@@ -306,6 +337,11 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
306337
307338 expect ( mockPushToolResult ) . toHaveBeenCalledWith ( "File saved successfully" )
308339 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
340+ // B2: the journal entry for a successful add carries the
341+ // approval-diff statistics (two added lines, no removals).
342+ expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
343+ { path : "src/new.ts" , operation : "create" , diffStats : { additions : 2 , deletions : 0 } } ,
344+ ] )
309345 } )
310346
311347 it ( "does not record a checkpoint when the file to update does not exist" , async ( ) => {
@@ -486,6 +522,11 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
486522 "utf8" ,
487523 )
488524 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
525+ // B2: the journal entry for a successful move is keyed by the
526+ // destination path and carries the approval-diff statistics.
527+ expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
528+ { path : "src/moved.ts" , operation : "update" , diffStats : { additions : 1 , deletions : 1 } } ,
529+ ] )
489530 } )
490531
491532 it ( "records a checkpoint when the in-place update succeeds" , async ( ) => {
@@ -497,12 +538,18 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
497538
498539 expect ( mockPushToolResult ) . toHaveBeenCalledWith ( "File saved successfully" )
499540 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
541+ // B2: the journal entry for a successful update carries the
542+ // approval-diff statistics.
543+ expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
544+ { path : "src/test.ts" , operation : "update" , diffStats : { additions : 1 , deletions : 1 } } ,
545+ ] )
500546 } )
501547
502548 it ( "records one journal write per file change for a multi-file patch" , async ( ) => {
503549 // src/a.ts does not exist (add); src/b.ts does (update).
504550 mockedFileExistsAtPath . mockImplementation ( ( filePath : string ) =>
505- Promise . resolve ( ! String ( filePath ) . toLowerCase ( ) . endsWith ( "a.ts" ) ) )
551+ Promise . resolve ( ! String ( filePath ) . toLowerCase ( ) . endsWith ( "a.ts" ) ) ,
552+ )
506553 const multiPatch = [
507554 "*** Begin Patch" ,
508555 "*** Add File: src/a.ts" ,
@@ -521,9 +568,11 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
521568 } )
522569
523570 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
571+ // B2: each journal entry carries the approval-diff statistics of
572+ // its file change.
524573 expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
525- { path : "src/a.ts" , operation : "create" } ,
526- { path : "src/b.ts" , operation : "update" } ,
574+ { path : "src/a.ts" , operation : "create" , diffStats : { additions : 1 , deletions : 0 } } ,
575+ { path : "src/b.ts" , operation : "update" , diffStats : { additions : 1 , deletions : 1 } } ,
527576 ] )
528577 } )
529578
@@ -556,7 +605,7 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
556605 // written, even though the whole patch succeeded.
557606 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
558607 expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
559- { path : "src/new.ts" , operation : "create" } ,
608+ { path : "src/new.ts" , operation : "create" , diffStats : { additions : 1 , deletions : 0 } } ,
560609 ] )
561610 } )
562611
@@ -584,10 +633,11 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
584633
585634 // The failed operation is reported to the model...
586635 expect ( mockPushToolResult ) . toHaveBeenCalledWith ( expect . stringContaining ( "File already exists" ) )
587- // ...and the successful subset is checkpointed and journaled.
636+ // ...and the successful subset is checkpointed and journaled,
637+ // with the written file's diff statistics.
588638 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
589639 expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
590- { path : "src/second.ts" , operation : "create" } ,
640+ { path : "src/second.ts" , operation : "create" , diffStats : { additions : 1 , deletions : 0 } } ,
591641 ] )
592642 } )
593643
@@ -613,14 +663,22 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
613663 // reported as a failed tool error - but the destination write was
614664 // made on disk and must still be covered by the checkpoint/journal.
615665 expect ( mockedCheckpointSave ) . toHaveBeenCalledOnce ( )
616- expect ( mockedCheckpointSave ) . toHaveBeenCalledWith (
617- mockTask as Task ,
618- false ,
619- true ,
620- [ expect . objectContaining ( { path : "src/new-location.ts" , operation : "update" } ) ] ,
621- )
666+ expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
667+ expect . objectContaining ( { path : "src/new-location.ts" , operation : "update" } ) ,
668+ ] )
669+ // B2: even a failed move's destination write carries the
670+ // approval-diff statistics in the journal payload.
671+ expect ( mockedCheckpointSave ) . toHaveBeenCalledWith ( mockTask as Task , false , true , [
672+ {
673+ path : "src/new-location.ts" ,
674+ operation : "update" ,
675+ diffStats : { additions : 1 , deletions : 1 } ,
676+ } ,
677+ ] )
622678 expect ( mockTask . recordToolError ) . toHaveBeenCalledWith ( "apply_patch" )
623- expect ( mockPushToolResult ) . toHaveBeenCalledWith ( expect . stringContaining ( "could not delete the original file" ) )
679+ expect ( mockPushToolResult ) . toHaveBeenCalledWith (
680+ expect . stringContaining ( "could not delete the original file" ) ,
681+ )
624682 } )
625683 } )
626684} )
0 commit comments