From ca0e83337b7eb49cd825d3001e66728cb2764348 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Sun, 25 Oct 2026 03:44:18 -0500 Subject: [PATCH] test: correct B1 fixture contract cleanup and sweep observation --- agents/magica/work/queue-331/BUILD.md | 9 +++ .../work/queue-331/b1-expected-red-r2.json | 77 +++++++++++++++++++ .../work/queue-331/b1-preparation-r2.md | 26 +++++++ .../queue-331/b1-prepared-r2-manifest.json | 69 +++++++++++++++++ packages/bus/tests/model-restart.test.mjs | 72 +++++++++++------ 5 files changed, 229 insertions(+), 24 deletions(-) create mode 100644 agents/magica/work/queue-331/b1-expected-red-r2.json create mode 100644 agents/magica/work/queue-331/b1-preparation-r2.md create mode 100644 agents/magica/work/queue-331/b1-prepared-r2-manifest.json diff --git a/agents/magica/work/queue-331/BUILD.md b/agents/magica/work/queue-331/BUILD.md index af55bbd9a..c80bf1590 100644 --- a/agents/magica/work/queue-331/BUILD.md +++ b/agents/magica/work/queue-331/BUILD.md @@ -639,3 +639,12 @@ exit because proc reads conflate failure. Strict host identity/exit confirmation and prior-run no-launch preservation required; reuse existing proxy-stop-failed. No productfix before intended RED, no new public/schema/role/mount/authority change. Await CLEAR; old results and narrow r5 approval remain scoped, no landing claim. + + +2026-10-25 — sage-4489 pre-RED fixture correction (new test only): PUT statusok; +early guarded resource cleanup/own ChildProcess only; original sweep awaited and +completion observed in finally, restored aftereach test; separate live/exit phases. +Uses established5s/20ms bounded wait (host-run.test:47), production1s timer intact. +Old7c563ece9/proposal/hash retained as preparation; amended b1-preparation-r2.md +supersedes1250ms observation. Expected10RED/2GREEN exactcase map, still not observed. +No oldtest/product/assertion change or Node/test/proxy launch. Await CLEAR. diff --git a/agents/magica/work/queue-331/b1-expected-red-r2.json b/agents/magica/work/queue-331/b1-expected-red-r2.json new file mode 100644 index 000000000..156b04f8f --- /dev/null +++ b/agents/magica/work/queue-331/b1-expected-red-r2.json @@ -0,0 +1,77 @@ +{ + "status": "expected only; no tests run", + "tests": [ + { + "name": "B1 restart before grace discovers persisted old-key run without bindLaunch", + "expected": "RED", + "why": "empty bound map retires live persisted key before grace", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 no-launch restart past grace neither signals nor ends a prior session", + "expected": "RED", + "why": "prior session deliberately never bound, so old key removed", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 normal recovery gap retains boot inventory when Registry clears before rebinding", + "expected": "RED", + "why": "empty map and cleared registry discard surviving run", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 malformed persisted discovery never authorizes retirement", + "expected": "RED", + "why": "old runtime never reads registry for retirement", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 missing persisted discovery never authorizes retirement", + "expected": "RED", + "why": "old runtime never reads registry for retirement", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 persisted identity disagreement in startTime retains key and record", + "expected": "RED", + "why": "old retirement has no persisted identity discovery", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 persisted identity disagreement in runnerStartTime retains key and record", + "expected": "RED", + "why": "old retirement has no persisted identity discovery", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 persisted identity disagreement in modelKeyId retains key and record", + "expected": "RED", + "why": "old retirement has no persisted identity discovery", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 discovery failure after boot retains the cached live old-key inventory", + "expected": "RED", + "why": "old runtime map-only callback ignores failed registry discovery", + "assertion": "old key stays remotely installed" + }, + { + "name": "B1 bound run with unauthorized runner stop retains key and retiring record", + "expected": "GREEN", + "why": "existing stopSession refuses scratch non-runner; map is populated", + "assertion": "existing positive control" + }, + { + "name": "B1 confirmed persisted runner exit permits old-key retirement after grace", + "expected": "GREEN", + "why": "old code removes key, consistent with positively exited scratch process", + "assertion": "existing positive control" + }, + { + "name": "B1 an observed persisted exit after restart permits retirement, never an empty registry alone", + "expected": "RED", + "why": "first live-retention phase fails before subsequent confirmed-exit phase", + "assertion": "old key stays remotely installed" + } + ] +} diff --git a/agents/magica/work/queue-331/b1-preparation-r2.md b/agents/magica/work/queue-331/b1-preparation-r2.md new file mode 100644 index 000000000..a72d2ff00 --- /dev/null +++ b/agents/magica/work/queue-331/b1-preparation-r2.md @@ -0,0 +1,26 @@ +# B1 preparation r2 amendment — sage-4489 + +Supersedes only the fixture setup/observation description in b1-proposal.md. +7c563ece9 and original manifest b205b127 remain preparation provenance, never RED. +Product code still4948ece0e. No Node/test/proxy/sandbox process launched. + +New test-only fixes: PUT response now exact JSON {status:'ok'} as ManagementClient +requires. Guarded t.after registered immediately after root allocation, before +server/credentials/child/runtime/launcher acquisition. All acquired resources are +closed even if another close throws; prototype restored and scratch root removed. +The cleanup exit uses only the recorded ChildProcess handle, not any registry PID. + +ModelKeys.prototype.sweep wrapper calls and AWAITS the saved ORIGINAL method; +completion increments in finally after original success/refusal. It changes neither +runtime timer nor sweep/HTTP/filesystem work and is restored after each test. +Per-phase observation waits for a NEW completion using5s budget and20ms polling, +the established pattern in packages/bus/tests/host-run.test.mjs:47-54. Removed +1250ms sleep. Live-to-exit observes two distinct completions. Original remote key, +local retiring record, no removal, process/no-launch assertions unchanged. + +Exact RED list remains ONE whole packages/bus/tests/model-restart.test.mjs. +No automatic fix/run after it. Expected failure map in b1-expected-red-r2.json is +expectation only; no observed RED/GREEN. No product-only seam, production interval +change, old assertion/deadline change, or scope expansion. Production proposal and +later7-file focus remain as prepared; implementation still requires intendedRED +and owner ruling. Waiting CLEAR after this corrected preparation inspection. diff --git a/agents/magica/work/queue-331/b1-prepared-r2-manifest.json b/agents/magica/work/queue-331/b1-prepared-r2-manifest.json new file mode 100644 index 000000000..f333a5ab5 --- /dev/null +++ b/agents/magica/work/queue-331/b1-prepared-r2-manifest.json @@ -0,0 +1,69 @@ +{ + "status": "corrected static preparation only; no intendedRED observed", + "priorPreparation": "7c563ece9", + "productCommit": "4948ece0e5c760943128ece963a30ab20c23c615", + "artifacts": [ + { + "path": "packages/bus/tests/model-restart.test.mjs", + "sha256": "daf5997cdb367388c1b79ff9392c33ce203d16bec2dc5b351dcb18497fdeb656", + "bytes": 10810 + }, + { + "path": "agents/magica/work/queue-331/b1-proposal.md", + "sha256": "0b98c9d87bccf70d73f46a790aeb888ef9966591d6b9543829e47d27c739d79b", + "bytes": 7228 + }, + { + "path": "agents/magica/work/queue-331/b1-preparation-r2.md", + "sha256": "e0ca08a1226b5167d7159c2fa8c03641f620c6e42443fc1219eef171d18936e1", + "bytes": 1730 + }, + { + "path": "agents/magica/work/queue-331/b1-expected-red-r2.json", + "sha256": "3fffa7848a8dcbd100df3c1808cab8f39c697b14d75a84f75e29ed24acc47dd5", + "bytes": 3133 + }, + { + "path": "agents/magica/work/queue-331/b1-red-files.txt", + "sha256": "4f0881da05f0d61a4dda4c7d14ad13608caad8f8325c03c758ce614560823c37", + "bytes": 42 + }, + { + "path": "agents/magica/work/queue-331/b1-proposed-focused-files.txt", + "sha256": "1f6fc01f5cb411107cf1940af6b696018fc119db3261eeadb6b190f81d884522", + "bytes": 274 + }, + { + "path": "agents/magica/work/queue-331/b1-static-dependencies.json", + "sha256": "860c6a13c6f4c878f8216b6695d6c9ec792eaf3b69c0cd92d55395390a674f3c", + "bytes": 1658 + }, + { + "path": "agents/magica/work/queue-331/b1-prepared-manifest.json", + "sha256": "b205b12705ff678b2b9854486672d12e39c132dad4e764b6385b679beb36aea6", + "bytes": 1849 + } + ], + "unchangedProduct": [ + { + "path": "packages/bus/src/runtime.mjs", + "sha256": "50f279fa4b4ffbe58faedcad3035e7e0dda905ea4046a92a2c1c96417597712b" + }, + { + "path": "packages/bus/src/model-keys.mjs", + "sha256": "ad5b37f3078f60299f1f629f4da1a75e934f90bc352e4be56e31cd4c2a1d8c65" + }, + { + "path": "packages/cli/src/launcher.mjs", + "sha256": "0272cc7c8e2ccbca9d92e4d3a34293cedc8a9bb1ec45fed88917050bd9bdede7" + }, + { + "path": "packages/seat/src/session.mjs", + "sha256": "c37a6ef6f6d2b4e61443e77d800656bf31e38913bf7dbdc4ca955f4a83b13a41" + }, + { + "path": "packages/seat/src/proc.mjs", + "sha256": "382cbf7e0ff336911e288ce858bdbfbec693bc2b69879720d1f0b4c7d8455198" + } + ] +} diff --git a/packages/bus/tests/model-restart.test.mjs b/packages/bus/tests/model-restart.test.mjs index 58e88af5c..e322cb862 100644 --- a/packages/bus/tests/model-restart.test.mjs +++ b/packages/bus/tests/model-restart.test.mjs @@ -21,11 +21,40 @@ import { startTimeOf } from '../../seat/src/proc.mjs'; import { Registry, SESSIONS_VERSION, sessionsFile } from '../../seat/src/session.mjs'; import { createLauncher } from '../../cli/src/launcher.mjs'; const id = value => createHash('sha256').update(value).digest('hex').slice(0, 12); -// The production sweep interval stays 1000ms. New tests observe it without a shorter timer seam. -const observeSweep = () => wait(1250); async function fixture(t, { overdue = false, exited = false } = {}) { const root = fs.mkdtempSync(join(tmpdir(), 'b1-')); + let server, credentials, child, childClosed, bus, launcher; + let completed = 0; + const originalSweep = ModelKeys.prototype.sweep; + const exit = async () => { + // Only the ChildProcess this fixture spawned: never use a registry-supplied PID. + if (child?.pid && child.exitCode === null && child.signalCode === null) child.kill('SIGTERM'); + if (childClosed) await childClosed; + }; + t.after(async () => { + const errors = []; + for (const close of [() => launcher?.close(), () => bus?.close(), () => credentials?.close(), exit, + async () => { server?.closeAllConnections(); if (server?.listening) await new Promise(resolve => server.close(resolve)); }]) { + try { await close(); } catch (error) { errors.push(error); } + } + ModelKeys.prototype.sweep = originalSweep; + fs.rmSync(root, { recursive: true, force: true }); + if (errors.length) throw new AggregateError(errors, 'scratch fixture cleanup failed'); + }); + // Await the ORIGINAL sweep, including its real HTTP/filesystem work. Count both + // fulfilled and rejected completions; the runtime still handles its own refusal. + ModelKeys.prototype.sweep = async function (...args) { + try { return await originalSweep.apply(this, args); } + finally { completed++; } + }; + const observeSweep = async () => { + const before = completed, deadline = Date.now() + 5000; + // Same five-second bounded wait budget as bus/tests/host-run.test.mjs:47. + // Production interval remains 1000ms; no clock or scheduler is replaced. + while (completed <= before && Date.now() < deadline) await wait(20); + assert.ok(completed > before, 'original runtime sweep completed within five seconds'); + }; const configDir = join(root, 'config'), dataRoot = join(root, 'data'); fs.mkdirSync(dataRoot, { mode: 0o700 }); const config = systemConfig(root), system = systemFor(root), rolesDir = rolesCopy(root); @@ -37,41 +66,36 @@ async function fixture(t, { overdue = false, exited = false } = {}) { fs.writeFileSync(join(configDir, 'secrets', 'proxy-management.key'), 'fx647-manage', { mode: 0o600 }); const prefix = `msk-${id(configDir)}-`; const state = { keys: [prefix + 'sentinel-' + randomBytes(32).toString('base64url')], put: 0, removed: 0 }; - const server = http.createServer(async (req, res) => { + server = http.createServer(async (req, res) => { if (req.url === '/v8/management/config/access/api-keys') { if (req.headers.authorization !== 'Bearer fx647-manage') { res.writeHead(401); res.end('{}'); return; } if (req.method === 'GET') { res.end(JSON.stringify(state.keys)); return; } const chunks = []; for await (const chunk of req) chunks.push(chunk); const next = JSON.parse(Buffer.concat(chunks)); state.removed += state.keys.filter(key => !next.includes(key)).length; - state.keys = next; state.put++; res.end('{}'); return; + state.keys = next; state.put++; res.end(JSON.stringify({ status: 'ok' })); return; } res.writeHead(state.keys.includes(req.headers.authorization?.slice(7)) ? 200 : 401); res.end('{}'); }); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); const baseUrl = `http://127.0.0.1:${server.address().port}`; - const credentials = new Credentials({ storeRoot, dataRoot, modelAgents: ['demo/chief'] }); + credentials = new Credentials({ storeRoot, dataRoot, modelAgents: ['demo/chief'] }); const manager = new ModelKeys({ credentials, configDir, baseUrl, client: new ManagementClient({ baseUrl, key: 'fx647-manage' }) }); await manager.reconcile(); const first = await manager.ensure('demo/chief'); await manager.rotate('demo/chief'); const record = credentials.modelRecord('demo/chief'); credentials.writeModel('demo/chief', { ...record, retiring: record.retiring.map(r => ({ ...r, retireAt: new Date(Date.now() + (overdue ? -60_000 : 3_600_000)).toISOString() })) }); - credentials.close(); state.put = 0; state.removed = 0; + credentials.close(); credentials = null; state.put = 0; state.removed = 0; // A harmless scratch-owned process supplies real /proc identity. It is deliberately // not a session runner, so stopSession must refuse rather than signal this stand-in. - const child = spawn('/usr/bin/sleep', ['60'], { stdio: 'ignore' }); + child = spawn('/usr/bin/sleep', ['60'], { stdio: 'ignore' }); + childClosed = new Promise(resolve => child.once('close', resolve)); await once(child, 'spawn'); const ticks = startTimeOf(child.pid); assert.equal(typeof ticks, 'string'); const entry = { business: 'demo', instance: 'chief', run: 'r647-old', harness: 'pi', provider: 'fake', model: 'fake', pid: child.pid, startTime: ticks, runnerPid: child.pid, runnerStartTime: ticks, modelKeyId: id(first.key), launchedBy: 'chief' }; const writeSessions = entries => writeJson(sessionsFile(dataRoot), { sessionsVersion: SESSIONS_VERSION, sessions: entries }); writeSessions([entry]); - const exit = async () => { if (child.exitCode === null && child.signalCode === null) { const ended = once(child, 'close'); child.kill('SIGTERM'); await ended; } }; if (exited) await exit(); - let bus = null, launcher = null; - t.after(async () => { - await launcher?.close(); await bus?.close(); await exit(); - server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); fs.rmSync(root, { recursive: true, force: true }); - }); const boot = async () => { bus = await startBroker({ dataRoot, businesses: { demo: busBusiness(business, resolved) }, organization: { dir: configDir, rolesDir, system }, proxy: { port: server.address().port, binary: '/fixture-unused' } }); @@ -89,46 +113,46 @@ async function fixture(t, { overdue = false, exited = false } = {}) { assert.equal(bus.credentials.modelRecord('demo/chief').retiring.some(r => r.keyId === entry.modelKeyId), true, 'retiring id stays locally recorded'); assert.equal(state.removed, 0, 'no remote key removal'); }; - return { root, dataRoot, state, entry, writeSessions, boot, noLaunch, exit, retained, + return { root, dataRoot, state, entry, writeSessions, boot, noLaunch, exit, retained, observeSweep, alive: () => startTimeOf(child.pid) === ticks, absent: () => { assert.equal(state.keys.includes(first.key), false); assert.equal(bus.credentials.modelRecord('demo/chief').retiring.length, 0); }, bind: () => bus.bindLaunch({ business: entry.business, role: entry.instance, run: entry.run, harness: entry.harness, pid: entry.pid, startTime: entry.startTime, modelKeyId: entry.modelKeyId }) }; } test('B1 restart before grace discovers persisted old-key run without bindLaunch', async t => { - const f = await fixture(t); await f.boot(); await observeSweep(); f.retained(); assert.equal(f.alive(), true); + const f = await fixture(t); await f.boot(); await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); }); test('B1 no-launch restart past grace neither signals nor ends a prior session', async t => { const f = await fixture(t, { overdue: true }); const before = fs.readFileSync(sessionsFile(f.dataRoot), 'utf8'); - await f.boot(); const ops = await f.noLaunch(); await observeSweep(); + await f.boot(); const ops = await f.noLaunch(); await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); assert.equal(ops.includes('bindLaunch') || ops.includes('endLaunch'), false); assert.equal(fs.readFileSync(sessionsFile(f.dataRoot), 'utf8'), before); }); test('B1 normal recovery gap retains boot inventory when Registry clears before rebinding', async t => { - const f = await fixture(t); await f.boot(); new Registry(f.dataRoot); await observeSweep(); f.retained(); assert.equal(f.alive(), true); + const f = await fixture(t); await f.boot(); new Registry(f.dataRoot); await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); }); for (const corruption of ['malformed', 'missing']) test(`B1 ${corruption} persisted discovery never authorizes retirement`, async t => { const f = await fixture(t, { overdue: true }); if (corruption === 'malformed') fs.writeFileSync(sessionsFile(f.dataRoot), '{'); else fs.unlinkSync(sessionsFile(f.dataRoot)); - await f.boot(); await observeSweep(); f.retained(); assert.equal(f.alive(), true); + await f.boot(); await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); }); for (const field of ['startTime', 'runnerStartTime', 'modelKeyId']) test(`B1 persisted identity disagreement in ${field} retains key and record`, async t => { const f = await fixture(t, { overdue: true }); f.writeSessions([{ ...f.entry, [field]: field === 'modelKeyId' ? 'fedcba987654' : '0' }]); - await f.boot(); await observeSweep(); f.retained(); assert.equal(f.alive(), true); + await f.boot(); await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); }); test('B1 discovery failure after boot retains the cached live old-key inventory', async t => { const f = await fixture(t, { overdue: true }); await f.boot(); fs.writeFileSync(sessionsFile(f.dataRoot), '{'); - await observeSweep(); f.retained(); assert.equal(f.alive(), true); + await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); }); test('B1 bound run with unauthorized runner stop retains key and retiring record', async t => { - const f = await fixture(t, { overdue: true }); await f.boot(); f.bind(); await observeSweep(); f.retained(); assert.equal(f.alive(), true); + const f = await fixture(t, { overdue: true }); await f.boot(); f.bind(); await f.observeSweep(); f.retained(); assert.equal(f.alive(), true); }); test('B1 confirmed persisted runner exit permits old-key retirement after grace', async t => { - const f = await fixture(t, { overdue: true, exited: true }); await f.boot(); await observeSweep(); f.absent(); + const f = await fixture(t, { overdue: true, exited: true }); await f.boot(); await f.observeSweep(); f.absent(); }); test('B1 an observed persisted exit after restart permits retirement, never an empty registry alone', async t => { - const f = await fixture(t, { overdue: true }); await f.boot(); await observeSweep(); f.retained(); - await f.exit(); await observeSweep(); f.absent(); + const f = await fixture(t, { overdue: true }); await f.boot(); await f.observeSweep(); f.retained(); + await f.exit(); await f.observeSweep(); f.absent(); });