|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { constants } from 'node:fs'; |
| 3 | +import { mkdtemp, readFile, rm, utimes, writeFile } from 'node:fs/promises'; |
| 4 | +import { createRequire, syncBuiltinESMExports } from 'node:module'; |
| 5 | +import os from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | +import test from 'node:test'; |
| 8 | +import { workerSchema } from '../src/config/config.js'; |
| 9 | +import type { FilePayload, WorkContext } from '../src/core/contracts.js'; |
| 10 | +import { sha, writePrivate } from '../src/core/util.js'; |
| 11 | +import { SessionWorkspaces } from '../src/fabric/session-workspaces.js'; |
| 12 | +import { WorkerRuntime } from '../src/fabric/worker.js'; |
| 13 | +import { builtin, makeBundle } from '../src/lang/bundle.js'; |
| 14 | +import { agent } from './helpers.js'; |
| 15 | + |
| 16 | +const alias = 'outputs/result.txt'; |
| 17 | +const inputPath = 'inputs/attached/note.txt'; |
| 18 | +const signal = () => new AbortController().signal; |
| 19 | +const config = (dataDir: string) => workerSchema.parse({ dataDir, nodeId: 'cache-fixture' }); |
| 20 | +function file(id: string, text: string, workspacePath?: string): FilePayload { |
| 21 | + const bytes = Buffer.from(text); |
| 22 | + return { |
| 23 | + id, |
| 24 | + name: 'note.txt', |
| 25 | + content: bytes.toString('base64'), |
| 26 | + sha256: sha(bytes), |
| 27 | + workspacePath, |
| 28 | + }; |
| 29 | +} |
| 30 | +function context(files: FilePayload[]): WorkContext { |
| 31 | + return { files, owner: 'owner', sessionId: 'same', runId: 'resp_fixture', digest: 'fixture' }; |
| 32 | +} |
| 33 | + |
| 34 | +test('a new worker phase restores missing declared files without rebuilding the session or dropping its other files', async () => { |
| 35 | + const base = await mkdtemp(path.join(os.tmpdir(), 'oaa-prepared-missing-')); |
| 36 | + const worker = new WorkerRuntime(config(base)); |
| 37 | + const binding = builtin('read_file'); |
| 38 | + const bundle = makeBundle(agent({ tools: [binding] })); |
| 39 | + const manifest = context([ |
| 40 | + file('attached', 'input bytes'), |
| 41 | + file('prior_output', 'artifact bytes', alias), |
| 42 | + ]); |
| 43 | + const run = (name: string) => |
| 44 | + worker.execute( |
| 45 | + { |
| 46 | + kind: 'tool', |
| 47 | + binding, |
| 48 | + arguments: { path: name }, |
| 49 | + context: { ...manifest, digest: bundle.digest }, |
| 50 | + }, |
| 51 | + signal(), |
| 52 | + async () => {}, |
| 53 | + ); |
| 54 | + try { |
| 55 | + await worker.install(bundle); |
| 56 | + assert.deepEqual(await run(inputPath), { value: { text: 'input bytes' } }); |
| 57 | + const root = path.join(base, 'sessions', sha('owner').slice(0, 16), manifest.sessionId); |
| 58 | + await writeFile(path.join(root, 'unrelated.txt'), 'keep'); |
| 59 | + await rm(path.join(root, inputPath)); |
| 60 | + assert.deepEqual(await run(inputPath), { value: { text: 'input bytes' } }); |
| 61 | + await rm(path.join(root, alias)); |
| 62 | + assert.deepEqual(await run(alias), { value: { text: 'artifact bytes' } }); |
| 63 | + assert.equal(await readFile(path.join(root, 'unrelated.txt'), 'utf8'), 'keep'); |
| 64 | + } finally { |
| 65 | + await worker.close(); |
| 66 | + await rm(base, { recursive: true, force: true }); |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +test('unchanged files require no read handle; a metadata change verifies just that file', async (t) => { |
| 71 | + const base = await mkdtemp(path.join(os.tmpdir(), 'oaa-prepared-witness-')); |
| 72 | + const workspaces = new SessionWorkspaces(config(base)); |
| 73 | + const manifest = context([file('attached', 'first'), file('other', 'second')]); |
| 74 | + const fs = createRequire(import.meta.url)( |
| 75 | + 'node:fs/promises', |
| 76 | + ) as typeof import('node:fs/promises'); |
| 77 | + const original = fs.open; |
| 78 | + const reads: string[] = []; |
| 79 | + const mocked = t.mock.method(fs, 'open', (...args: Parameters<typeof original>) => { |
| 80 | + const [target, flags] = args; |
| 81 | + if (typeof flags === 'number' && !(flags & (constants.O_WRONLY | constants.O_RDWR))) |
| 82 | + reads.push(String(target)); |
| 83 | + return original(...args); |
| 84 | + }); |
| 85 | + syncBuiltinESMExports(); |
| 86 | + const activate = () => workspaces.use(manifest, signal(), async (root) => root); |
| 87 | + try { |
| 88 | + const root = await activate(); |
| 89 | + reads.length = 0; |
| 90 | + assert.equal(await activate(), root); |
| 91 | + assert.deepEqual(reads, []); |
| 92 | + const touched = path.join(root, inputPath); |
| 93 | + await utimes(touched, new Date(), new Date('2020-01-01T00:00:00Z')); |
| 94 | + await activate(); |
| 95 | + assert.deepEqual( |
| 96 | + reads.map((name) => path.basename(path.dirname(name))), |
| 97 | + ['attached'], |
| 98 | + ); |
| 99 | + reads.length = 0; |
| 100 | + await activate(); |
| 101 | + assert.deepEqual(reads, []); |
| 102 | + } finally { |
| 103 | + mocked.mock.restore(); |
| 104 | + syncBuiltinESMExports(); |
| 105 | + await workspaces.close(); |
| 106 | + await rm(base, { recursive: true, force: true }); |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +for (const target of ['attachment', 'artifact'] as const) |
| 111 | + test(`an edited ${target} cannot execute or be silently overwritten on repeated activations`, async () => { |
| 112 | + const base = await mkdtemp(path.join(os.tmpdir(), 'oaa-prepared-edit-')); |
| 113 | + const workspaces = new SessionWorkspaces(config(base)); |
| 114 | + const isAlias = target === 'artifact'; |
| 115 | + const manifest = context([file('attached', 'version-A', isAlias ? alias : undefined)]); |
| 116 | + try { |
| 117 | + const root = await workspaces.use(manifest, signal(), async (root) => root); |
| 118 | + const name = path.join(root, isAlias ? alias : inputPath); |
| 119 | + await writeFile(name, 'version-B'); |
| 120 | + await utimes(name, new Date(), new Date('2020-01-01T00:00:00Z')); |
| 121 | + for (let i = 0; i < 2; i++) |
| 122 | + await assert.rejects( |
| 123 | + workspaces.use(manifest, signal(), async () => assert.fail('unverified operation ran')), |
| 124 | + { code: 'file_integrity' }, |
| 125 | + ); |
| 126 | + assert.equal(await readFile(name, 'utf8'), 'version-B'); |
| 127 | + } finally { |
| 128 | + await workspaces.close(); |
| 129 | + await rm(base, { recursive: true, force: true }); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | +test('a new file at a missing alias is not overwritten while repair is opening the path', async () => { |
| 134 | + const base = await mkdtemp(path.join(os.tmpdir(), 'oaa-prepared-repair-race-')); |
| 135 | + let repairing = false; |
| 136 | + const workspaces = new SessionWorkspaces(config(base), async (...args) => { |
| 137 | + const [root, name, , exclusive] = args; |
| 138 | + if (repairing) { |
| 139 | + assert.equal(exclusive, true); |
| 140 | + await writePrivate(root, name, 'new on-disk bytes', true); |
| 141 | + } |
| 142 | + return writePrivate(...args); |
| 143 | + }); |
| 144 | + const manifest = context([file('attached', 'original', alias)]); |
| 145 | + try { |
| 146 | + const root = await workspaces.use(manifest, signal(), async (root) => root); |
| 147 | + await rm(path.join(root, alias)); |
| 148 | + repairing = true; |
| 149 | + await assert.rejects( |
| 150 | + workspaces.use(manifest, signal(), async () => assert.fail('replacement passed')), |
| 151 | + { code: 'file_integrity' }, |
| 152 | + ); |
| 153 | + assert.equal(await readFile(path.join(root, alias), 'utf8'), 'new on-disk bytes'); |
| 154 | + } finally { |
| 155 | + await workspaces.close(); |
| 156 | + await rm(base, { recursive: true, force: true }); |
| 157 | + } |
| 158 | +}); |
| 159 | + |
| 160 | +test('a materialized path is not acknowledged if its bytes change before staging completes', async () => { |
| 161 | + const base = await mkdtemp(path.join(os.tmpdir(), 'oaa-prepared-write-race-')); |
| 162 | + const workspaces = new SessionWorkspaces(config(base), async (...args) => { |
| 163 | + await writePrivate(...args); |
| 164 | + await writeFile(path.join(args[0], args[1]), 'different'); |
| 165 | + }); |
| 166 | + try { |
| 167 | + await assert.rejects( |
| 168 | + workspaces.use(context([file('attached', 'original')]), signal(), async () => |
| 169 | + assert.fail('changed bytes passed'), |
| 170 | + ), |
| 171 | + { code: 'file_integrity' }, |
| 172 | + ); |
| 173 | + } finally { |
| 174 | + await workspaces.close(); |
| 175 | + await rm(base, { recursive: true, force: true }); |
| 176 | + } |
| 177 | +}); |
0 commit comments