@@ -4,7 +4,7 @@ import path from "path"
44
55import { afterEach , beforeEach , describe , expect , it , vi } from "vitest"
66
7- import { appendChange , journalPath , loadChanges , type ChangeJournalEntry } from "../changeJournal"
7+ import { appendChange , appendChanges , journalPath , loadChanges , type ChangeJournalEntry } from "../changeJournal"
88
99describe ( "changeJournal" , ( ) => {
1010 const taskId = "test-task"
@@ -72,22 +72,63 @@ describe("changeJournal", () => {
7272 expect ( raw . split ( "\n" ) . filter ( ( line ) => line !== "" ) ) . toHaveLength ( 2 )
7373 } )
7474
75- it ( "propagates a non-ENOENT tail-inspection failure instead of appending blind" , async ( ) => {
75+ it ( "syncs the parent directory after every journal replacement (POSIX)" , async ( ) => {
76+ const platformDesc = Object . getOwnPropertyDescriptor ( process , "platform" )
77+ Object . defineProperty ( process , "platform" , { value : "linux" , configurable : true } )
78+ const dirPath = path . dirname ( journalPath ( tmpRoot , taskId ) )
79+ const realOpen = fs . open
80+ const synced : string [ ] = [ ]
81+ const closed : string [ ] = [ ]
82+ // Windows cannot open directories, so the directory handle is doubled
83+ // here: the code under test only opens it on non-win32 platforms.
84+ const openSpy = vi . spyOn ( fs , "open" ) . mockImplementation ( async ( pathish , flags ) => {
85+ const openAs = realOpen as ( p : string | URL , f ?: string ) => Promise < fs . FileHandle >
86+ const p = typeof pathish === "string" ? pathish : String ( pathish )
87+ if ( p === dirPath && flags === "r" ) {
88+ return {
89+ sync : async ( ) => {
90+ synced . push ( p )
91+ } ,
92+ close : async ( ) => {
93+ closed . push ( p )
94+ } ,
95+ } as unknown as fs . FileHandle
96+ }
97+ return openAs ( p , flags as string | undefined )
98+ } )
99+ try {
100+ await appendChange ( tmpRoot , taskId , entry ( { checkpointId : "first" } ) )
101+ expect ( synced ) . toEqual ( [ dirPath ] )
102+ expect ( closed ) . toEqual ( [ dirPath ] )
103+ // Every replacement renames a new file into place, so the directory
104+ // entry is re-synced on each update.
105+ await appendChange ( tmpRoot , taskId , entry ( { checkpointId : "second" } ) )
106+ expect ( synced ) . toHaveLength ( 2 )
107+ } finally {
108+ openSpy . mockRestore ( )
109+ if ( platformDesc ) Object . defineProperty ( process , "platform" , platformDesc )
110+ }
111+ const result = await loadChanges ( tmpRoot , taskId )
112+ expect ( result . map ( ( e ) => e . checkpointId ) ) . toEqual ( [ "first" , "second" ] )
113+ } )
114+
115+ it ( "propagates a non-ENOENT journal-read failure instead of replacing blind" , async ( ) => {
76116 await appendChange ( tmpRoot , taskId , entry ( { checkpointId : "good" } ) )
77117 const filePath = journalPath ( tmpRoot , taskId )
78118 const raw = await fs . readFile ( filePath , "utf8" )
79119 await fs . writeFile ( filePath , raw + '{"path":"src/half.ts","operation":"upd' )
80- // The tail cannot be inspected (I/O error): appending would merge the
81- // new entry into the fragment, so appendChange must fail loudly.
82- const statSpy = vi
83- . spyOn ( fs , "stat" )
120+ // The current content cannot be read (I/O error): replacing would
121+ // discard whatever the unreadable journal really held, so appendChange
122+ // must fail loudly.
123+ const readSpy = vi
124+ . spyOn ( fs , "readFile" )
84125 . mockRejectedValueOnce ( Object . assign ( new Error ( "simulated I/O error" ) , { code : "EIO" } ) )
85126 try {
86127 await expect ( appendChange ( tmpRoot , taskId , entry ( { checkpointId : "after" } ) ) ) . rejects . toMatchObject ( {
87128 code : "EIO" ,
88129 } )
89130 } finally {
90- statSpy . mockRestore ( )
131+ readSpy . mockRestore ( )
91132 }
92133 // The failed append wrote nothing: the next append still repairs and loads.
93134 await appendChange ( tmpRoot , taskId , entry ( { checkpointId : "after" } ) )
@@ -101,7 +142,7 @@ describe("changeJournal", () => {
101142 const openSpy = vi . spyOn ( fs , "open" ) . mockImplementation ( async ( pathish , flags ) => {
102143 const openAs = realOpen as ( p : string | URL , f ?: string ) => Promise < fs . FileHandle >
103144 const handle = await openAs ( pathish as string | URL , flags as string | undefined )
104- if ( flags === "a " ) {
145+ if ( flags === "w " ) {
105146 const realWrite = handle . write . bind ( handle )
106147 let first = true
107148 handle . write = ( async ( buf : Uint8Array , off ?: number , len ?: number ) => {
@@ -124,6 +165,59 @@ describe("changeJournal", () => {
124165 const result = await loadChanges ( tmpRoot , taskId )
125166 expect ( result . map ( ( e ) => e . checkpointId ) ) . toEqual ( [ "first" , "second" ] )
126167 } )
168+
169+ it ( "serializes concurrent updates to the same task" , async ( ) => {
170+ await Promise . all ( [
171+ appendChange ( tmpRoot , taskId , entry ( { checkpointId : "c1" } ) ) ,
172+ appendChange ( tmpRoot , taskId , entry ( { checkpointId : "c2" } ) ) ,
173+ ] )
174+
175+ const result = await loadChanges ( tmpRoot , taskId )
176+ expect ( result . map ( ( e ) => e . checkpointId ) . sort ( ) ) . toEqual ( [ "c1" , "c2" ] )
177+ } )
178+
179+ it ( "leaves the journal untouched when the atomic rename fails mid-update" , async ( ) => {
180+ await appendChanges ( tmpRoot , taskId , [ entry ( { checkpointId : "kept" } ) ] )
181+ const renameSpy = vi . spyOn ( fs , "rename" ) . mockRejectedValueOnce ( new Error ( "simulated rename failure" ) )
182+ try {
183+ await expect (
184+ appendChanges ( tmpRoot , taskId , [
185+ entry ( { checkpointId : "m1" , path : "src/m1.ts" } ) ,
186+ entry ( { checkpointId : "m2" , path : "src/m2.ts" } ) ,
187+ ] ) ,
188+ ) . rejects . toThrow ( "simulated rename failure" )
189+ } finally {
190+ renameSpy . mockRestore ( )
191+ }
192+ // Neither of the two new entries survived: the update is all-or-nothing.
193+ const result = await loadChanges ( tmpRoot , taskId )
194+ expect ( result . map ( ( e ) => e . checkpointId ) ) . toEqual ( [ "kept" ] )
195+ // The failed temp file was cleaned up.
196+ const leftovers = ( await fs . readdir ( path . dirname ( journalPath ( tmpRoot , taskId ) ) ) ) . filter ( ( name ) =>
197+ name . endsWith ( ".tmp" ) ,
198+ )
199+ expect ( leftovers ) . toEqual ( [ ] )
200+ } )
201+
202+ it ( "recovers after a crash left a stale temp file and a torn tail" , async ( ) => {
203+ await appendChange ( tmpRoot , taskId , entry ( { checkpointId : "before" } ) )
204+ const dir = path . dirname ( journalPath ( tmpRoot , taskId ) )
205+ // A crash between the temp write and the rename leaves an orphan temp
206+ // file; a crash mid-write can also leave the tail unterminated.
207+ await fs . writeFile ( path . join ( dir , "changes.jsonl.4242.orphan.tmp" ) , "partial" )
208+ const raw = await fs . readFile ( journalPath ( tmpRoot , taskId ) , "utf8" )
209+ await fs . writeFile ( journalPath ( tmpRoot , taskId ) , raw + '{"path":"src/half.ts","operation":"upd' )
210+
211+ await appendChange ( tmpRoot , taskId , entry ( { checkpointId : "after" } ) )
212+
213+ const result = await loadChanges ( tmpRoot , taskId )
214+ expect ( result . map ( ( e ) => e . checkpointId ) ) . toEqual ( [ "before" , "after" ] )
215+ } )
216+
217+ it ( "does not create a journal for an empty update" , async ( ) => {
218+ await appendChanges ( tmpRoot , taskId , [ ] )
219+ await expect ( fs . stat ( journalPath ( tmpRoot , taskId ) ) ) . rejects . toMatchObject ( { code : "ENOENT" } )
220+ } )
127221 } )
128222
129223 describe ( "task ID containment" , ( ) => {
0 commit comments