@@ -106,9 +106,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
106106 const task = createMockTask ( )
107107
108108 await expect ( guardedWrite ( task , "existing.txt" , "hello" , "create" ) ) . rejects . toThrow (
109- "File already exists at " +
110- abs ( "existing.txt" ) +
111- " and was not read before this write -- read the file first, then retry." ,
109+ "file not read yet — read the file, then retry" ,
112110 )
113111 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
114112 } )
@@ -123,21 +121,22 @@ describe("guardedWrite (S4a, epic #1375)", () => {
123121 } )
124122 } )
125123
126- describe ( "deleted-after-read target" , ( ) => {
127- it ( "normalizes an ENOENT from the version token into the re- read remediation " , async ( ) => {
124+ describe ( "deleted-after-read target (observed + absent) " , ( ) => {
125+ it ( "creates the file when it vanished after the read" , async ( ) => {
128126 const reg = new ObservationRegistry ( )
129127 reg . observe ( abs ( "vanished.txt" ) , "v1" )
130128 const task = createMockTask ( { observationRegistry : reg } )
131129
132- // The file was deleted after the read: the token computation fails
133- // with a raw ENOENT, which the guard must convert into the standard
134- // re-read-then-retry contract.
130+ // Observed + absent is the create row of the #1399 decision table: the file the
131+ // caller read was deleted before this write, so the guard recreates it.
135132 mockedComputeVersionToken . mockRejectedValue ( { code : "ENOENT" } )
133+ mockedFsAccess . mockRejectedValue ( { code : "ENOENT" } )
136134
137- await expect ( guardedWrite ( task , "vanished.txt" , "next" , "update" ) ) . rejects . toThrow (
138- "File was deleted after it was read" ,
139- )
140- expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
135+ await guardedWrite ( task , "vanished.txt" , "next" , "update" )
136+
137+ expect ( mockedSafeWriteText ) . toHaveBeenCalledWith ( abs ( "vanished.txt" ) , "next" , {
138+ verifyBeforeCommit : expect . any ( Function ) ,
139+ } )
141140 } )
142141
143142 it ( "rethrows non-ENOENT token failures verbatim from replaceIfVersion" , async ( ) => {
@@ -165,9 +164,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
165164 const task = createMockTask ( )
166165
167166 await expect ( guardedWrite ( task , "existing.txt" , "hello" , "update" ) ) . rejects . toThrow (
168- "File already exists at " +
169- abs ( "existing.txt" ) +
170- " and was not read before this write -- read the file first, then retry." ,
167+ "file not read yet — read the file, then retry" ,
171168 )
172169 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
173170 } )
@@ -228,7 +225,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
228225 const task = createMockTask ( { observationRegistry : reg } )
229226
230227 await expect ( guardedWrite ( task , "kept.txt" , "rewritten" , "create" ) ) . rejects . toThrow (
231- "Stale version -- the file changed since you read it (expected v1, current v2); re-read the file, then retry. " ,
228+ "stale version — re-read the file, then retry" ,
232229 )
233230 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
234231 } )
@@ -241,7 +238,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
241238 const task = createMockTask ( { observationRegistry : reg } )
242239
243240 await expect ( guardedWrite ( task , "locked.txt" , "rewritten" , "create" ) ) . rejects . toThrow (
244- "Stale version -- the file changed since you read it (expected v1, current v2); re-read the file, then retry. " ,
241+ "stale version — re-read the file, then retry" ,
245242 )
246243 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
247244 } )
@@ -269,29 +266,25 @@ describe("guardedWrite (S4a, epic #1375)", () => {
269266 const task = createMockTask ( { observationRegistry : reg } )
270267
271268 await expect ( guardedWrite ( task , "doc.txt" , "new content" , "update" ) ) . rejects . toThrow (
272- "Stale version -- the file changed since you read it (expected v1, current v2); re-read the file, then retry. " ,
269+ "stale version — re-read the file, then retry" ,
273270 )
274271 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
275272 } )
276273
277- it ( "does not probe existence or recreate when an updated file vanished after the read" , async ( ) => {
274+ it ( "recreates when an updated file vanished after the read" , async ( ) => {
278275 const reg = new ObservationRegistry ( )
279276 reg . observe ( abs ( "doc.txt" ) , "v1" )
280277 const task = createMockTask ( { observationRegistry : reg } )
281- // The file vanished: the CAS token recomputation rejects with ENOENT,
282- // which the version guard reports as the deleted-file remediation .
278+ // Observed + absent is the create row of the #1399 decision table: the
279+ // file vanished between the read and this write, so the guard recreates it .
283280 mockedComputeVersionToken . mockRejectedValue ( { code : "ENOENT" } )
284281 mockedFsAccess . mockRejectedValue ( { code : "ENOENT" } )
285282
286- await expect ( guardedWrite ( task , "doc.txt" , "new content" , "update" ) ) . rejects . toThrow (
287- "File was deleted after it was read -- the version recorded at read time (v1) no longer exists; re-read the file, then retry." ,
288- )
283+ await guardedWrite ( task , "doc.txt" , "new content" , "update" )
289284
290- // The existence probe is a "create"-only step: an "update" must not run
291- // it (the branch condition short-circuits on kind) and must not recreate
292- // the vanished file - only the CAS branch may decide the write.
293- expect ( mockedFsAccess ) . not . toHaveBeenCalled ( )
294- expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
285+ expect ( mockedSafeWriteText ) . toHaveBeenCalledWith ( abs ( "doc.txt" ) , "new content" , {
286+ verifyBeforeCommit : expect . any ( Function ) ,
287+ } )
295288 } )
296289 } )
297290
@@ -300,7 +293,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
300293 const task = createMockTask ( )
301294
302295 await expect ( guardedWrite ( task , "any.txt" , "patched" , "edit" ) ) . rejects . toThrow (
303- "File not read yet -- read the file, then retry. " ,
296+ "file not read yet — read the file, then retry" ,
304297 )
305298 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
306299 expect ( mockedComputeVersionToken ) . not . toHaveBeenCalled ( )
@@ -327,7 +320,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
327320 const task = createMockTask ( { observationRegistry : reg } )
328321
329322 await expect ( guardedWrite ( task , "doc.txt" , "patched" , "edit" ) ) . rejects . toThrow (
330- "Stale version -- the file changed since you read it (expected v1, current v3); re-read the file, then retry. " ,
323+ "stale version — re-read the file, then retry" ,
331324 )
332325 expect ( mockedSafeWriteText ) . not . toHaveBeenCalled ( )
333326 } )
@@ -430,7 +423,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
430423 }
431424 expect ( mockedSafeWriteText ) . toHaveBeenCalledTimes ( 1 )
432425 expect ( r2 . reason . message ) . toBe (
433- "Stale version -- the file changed since you read it (expected v1, current v2); re-read the file, then retry. " ,
426+ "stale version — re-read the file, then retry" ,
434427 )
435428 } )
436429
@@ -468,7 +461,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
468461 // content derived from the v1 read over the v2 file. An execution-time
469462 // registry lookup would rescue it with the refreshed token and lose the
470463 // first write's update.
471- await expect ( second ) . rejects . toThrow ( "Stale version" )
464+ await expect ( second ) . rejects . toThrow ( "stale version" )
472465 expect ( publishes ) . toBe ( 1 )
473466 } )
474467
@@ -496,8 +489,8 @@ describe("guardedWrite (S4a, epic #1375)", () => {
496489 throw new Error ( "expected exactly one publish, got " + r1 . status + " / " + r2 . status )
497490 }
498491 expect ( publishes ) . toBe ( 1 )
499- expect ( r2 . reason . message ) . toContain ( "Stale version" )
500- expect ( r2 . reason . message ) . toContain ( "re-read the file, then retry. " )
492+ expect ( r2 . reason . message ) . toContain ( "stale version" )
493+ expect ( r2 . reason . message ) . toContain ( "re-read the file, then retry" )
501494 } )
502495
503496 it ( "the chain settles after a rejection - a later matching write still runs" , async ( ) => {
@@ -507,7 +500,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
507500 const task = createMockTask ( { observationRegistry : reg } )
508501
509502 const p1 = guardedWrite ( task , "settle.txt" , "first" , "update" )
510- await expect ( p1 ) . rejects . toThrow ( "Stale version" )
503+ await expect ( p1 ) . rejects . toThrow ( "stale version" )
511504
512505 // No resetChain: the rejected link must not wedge the chain. The
513506 // caller re-reads the file (observation refreshed to v2) and retries.
@@ -592,9 +585,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
592585 } )
593586
594587 await expect ( guardedWrite ( task , "raced.txt" , "mine" , "create" ) ) . rejects . toThrow (
595- "File already exists at " +
596- abs ( "raced.txt" ) +
597- " and was not read before this write -- read the file first, then retry." ,
588+ "file not read yet — read the file, then retry" ,
598589 )
599590 // nothing was published: the competing writer's file is preserved
600591 expect ( published ( ) ) . toBe ( false )
@@ -613,7 +604,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
613604 } )
614605
615606 await expect ( guardedWrite ( task , "raced.txt" , "mine" , "update" ) ) . rejects . toThrow (
616- "Stale version -- the file changed since you read it (expected v1, current v-external); re-read the file, then retry. " ,
607+ "stale version — re-read the file, then retry" ,
617608 )
618609 expect ( published ( ) ) . toBe ( false )
619610 } )
@@ -704,7 +695,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
704695 }
705696 expect ( mockedSafeWriteText ) . toHaveBeenCalledTimes ( 1 )
706697 expect ( r2 . reason . message ) . toBe (
707- "Stale version -- the file changed since you read it (expected v1, current v2); re-read the file, then retry. " ,
698+ "stale version — re-read the file, then retry" ,
708699 )
709700 } )
710701 } )
0 commit comments