11import assert from 'node:assert/strict' ;
22import { constants } from 'node:fs' ;
3- import { mkdtemp , readFile , rm , utimes , writeFile } from 'node:fs/promises' ;
3+ import { mkdir , mkdtemp , readFile , rename , rm , symlink , utimes , writeFile } from 'node:fs/promises' ;
44import { createRequire , syncBuiltinESMExports } from 'node:module' ;
55import os from 'node:os' ;
66import path from 'node:path' ;
@@ -39,6 +39,8 @@ test('a new worker phase restores missing declared files without rebuilding the
3939 const manifest = context ( [
4040 file ( 'attached' , 'input bytes' ) ,
4141 file ( 'prior_output' , 'artifact bytes' , alias ) ,
42+ file ( 'companion' , 'companion bytes' ) ,
43+ file ( 'second_output' , 'another artifact' , 'outputs/another.txt' ) ,
4244 ] ) ;
4345 const run = ( name : string ) =>
4446 worker . execute (
@@ -70,25 +72,45 @@ test('a new worker phase restores missing declared files without rebuilding the
7072test ( 'unchanged files require no read handle; a metadata change verifies just that file' , async ( t ) => {
7173 const base = await mkdtemp ( path . join ( os . tmpdir ( ) , 'oaa-prepared-witness-' ) ) ;
7274 const workspaces = new SessionWorkspaces ( config ( base ) ) ;
73- const manifest = context ( [ file ( 'attached' , 'first' ) , file ( 'other' , 'second' ) ] ) ;
75+ const manifest = context ( [
76+ file ( 'attached' , 'first' ) ,
77+ file ( 'other' , 'second' ) ,
78+ file ( 'output' , 'third' , alias ) ,
79+ file ( 'output2' , 'fourth' , 'outputs/second.txt' ) ,
80+ ] ) ;
7481 const fs = createRequire ( import . meta. url ) (
7582 'node:fs/promises' ,
7683 ) as typeof import ( 'node:fs/promises' ) ;
7784 const original = fs . open ;
85+ const realpath = fs . realpath ;
86+ const mkdir = fs . mkdir ;
87+ let roots = 0 ;
88+ let creates = 0 ;
7889 const reads : string [ ] = [ ] ;
7990 const mocked = t . mock . method ( fs , 'open' , ( ...args : Parameters < typeof original > ) => {
8091 const [ target , flags ] = args ;
8192 if ( typeof flags === 'number' && ! ( flags & ( constants . O_WRONLY | constants . O_RDWR ) ) )
8293 reads . push ( String ( target ) ) ;
8394 return original ( ...args ) ;
8495 } ) ;
96+ const resolved = t . mock . method ( fs , 'realpath' , ( ...args : Parameters < typeof realpath > ) => {
97+ roots ++ ;
98+ return realpath ( ...args ) ;
99+ } ) ;
100+ const made = t . mock . method ( fs , 'mkdir' , ( ...args : Parameters < typeof mkdir > ) => {
101+ creates ++ ;
102+ return mkdir ( ...args ) ;
103+ } ) ;
85104 syncBuiltinESMExports ( ) ;
86105 const activate = ( ) => workspaces . use ( manifest , signal ( ) , async ( root ) => root ) ;
87106 try {
88107 const root = await activate ( ) ;
89108 reads . length = 0 ;
109+ roots = creates = 0 ;
90110 assert . equal ( await activate ( ) , root ) ;
91111 assert . deepEqual ( reads , [ ] ) ;
112+ assert . equal ( roots , 2 , 'resolve the root once, then recheck it' ) ;
113+ assert . equal ( creates , 1 , 'do not repeat root preparation for each path' ) ;
92114 const touched = path . join ( root , inputPath ) ;
93115 await utimes ( touched , new Date ( ) , new Date ( '2020-01-01T00:00:00Z' ) ) ;
94116 await activate ( ) ;
@@ -101,12 +123,76 @@ test('unchanged files require no read handle; a metadata change verifies just th
101123 assert . deepEqual ( reads , [ ] ) ;
102124 } finally {
103125 mocked . mock . restore ( ) ;
126+ resolved . mock . restore ( ) ;
127+ made . mock . restore ( ) ;
104128 syncBuiltinESMExports ( ) ;
105129 await workspaces . close ( ) ;
106130 await rm ( base , { recursive : true , force : true } ) ;
107131 }
108132} ) ;
109133
134+ const batchFiles = ( ) =>
135+ Array . from ( { length : 4 } , ( _ , i ) =>
136+ file ( `output_${ i } ` , `fixture-${ i } ` , `outputs/nested/note-${ i } .txt` ) ,
137+ ) ;
138+
139+ test ( 'a parent replaced after the batch has seen its final leaf invalidates the shared observation' , async ( t ) => {
140+ const base = await mkdtemp ( path . join ( os . tmpdir ( ) , 'oaa-prepared-parent-change-' ) ) ;
141+ const workspaces = new SessionWorkspaces ( config ( base ) ) ;
142+ const manifest = context ( batchFiles ( ) ) ;
143+ const fs = createRequire ( import . meta. url ) (
144+ 'node:fs/promises' ,
145+ ) as typeof import ( 'node:fs/promises' ) ;
146+ const stat = fs . lstat ;
147+ let changed = false ;
148+ const root = await workspaces . use ( manifest , signal ( ) , async ( root ) => root ) ;
149+ const directory = path . join ( root , 'outputs' , 'nested' ) ;
150+ const mocked = t . mock . method ( fs , 'lstat' , async ( ...args : Parameters < typeof stat > ) => {
151+ const observed = await stat ( ...args ) ;
152+ if ( ! changed && String ( args [ 0 ] ) . endsWith ( path . join ( 'outputs' , 'nested' , 'note-3.txt' ) ) ) {
153+ changed = true ;
154+ await rename ( directory , directory + '-saved' ) ;
155+ await mkdir ( directory ) ;
156+ await writeFile ( path . join ( directory , 'note-0.txt' ) , 'new local bytes' ) ;
157+ }
158+ return observed ;
159+ } ) ;
160+ syncBuiltinESMExports ( ) ;
161+ try {
162+ await assert . rejects (
163+ workspaces . use ( manifest , signal ( ) , async ( ) => assert . fail ( 'old directory snapshot passed' ) ) ,
164+ { code : 'file_integrity' } ,
165+ ) ;
166+ assert . equal ( changed , true ) ;
167+ assert . equal ( await readFile ( path . join ( directory , 'note-0.txt' ) , 'utf8' ) , 'new local bytes' ) ;
168+ } finally {
169+ mocked . mock . restore ( ) ;
170+ syncBuiltinESMExports ( ) ;
171+ await workspaces . close ( ) ;
172+ await rm ( base , { recursive : true , force : true } ) ;
173+ }
174+ } ) ;
175+
176+ test ( 'batched metadata checks do not follow a symbolic parent even to matching fixture files' , async ( ) => {
177+ const base = await mkdtemp ( path . join ( os . tmpdir ( ) , 'oaa-prepared-link-parent-' ) ) ;
178+ const workspaces = new SessionWorkspaces ( config ( base ) ) ;
179+ const manifest = context ( batchFiles ( ) ) ;
180+ try {
181+ const root = await workspaces . use ( manifest , signal ( ) , async ( root ) => root ) ;
182+ const directory = path . join ( root , 'outputs' , 'nested' ) ;
183+ const saved = path . join ( base , 'outside-session' ) ;
184+ await rename ( directory , saved ) ;
185+ await symlink ( saved , directory , process . platform === 'win32' ? 'junction' : 'dir' ) ;
186+ await assert . rejects (
187+ workspaces . use ( manifest , signal ( ) , async ( ) => assert . fail ( 'linked parent passed' ) ) ,
188+ { code : 'invalid_path' } ,
189+ ) ;
190+ } finally {
191+ await workspaces . close ( ) ;
192+ await rm ( base , { recursive : true , force : true } ) ;
193+ }
194+ } ) ;
195+
110196for ( const target of [ 'attachment' , 'artifact' ] as const )
111197 test ( `an edited ${ target } cannot execute or be silently overwritten on repeated activations` , async ( ) => {
112198 const base = await mkdtemp ( path . join ( os . tmpdir ( ) , 'oaa-prepared-edit-' ) ) ;
0 commit comments