Files
Agent-Workbench/docs/model-selection-k-evidence/source-bytes/tests/change-set-recovery.test.mjs
T
2026-11-03 15:37:04 +08:00

43 lines
3.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { fixture, until, workspace } from './helpers.mjs';
import { batchFixture, assertBatchBytes } from './fixtures/change-set-project.mjs';
import { Store } from '../src/server/store.mjs';
import { ChangeSetManager } from '../src/core/workspaces/change-sets.mjs';
for (const phase of ['prepared', 'application_started', 'write_intent', 'staged', 'replaced', 'file_recorded', 'finished',
'rollback_started', 'restore_intent', 'restore_staged', 'restored', 'restore_recorded', 'rollback_finished']) {
test('actual process exit at file-batch ' + phase + ' preserves durable identity and never replays writes', { timeout: 30000 }, async t => {
const f = fixture(t), b = batchFixture(f);
let set = phase === 'prepared' ? null : await b.manager.prepare(f.session, b.ids);
const direction = phase === 'prepared' ? 'prepare' : phase.startsWith('restore') || phase.startsWith('rollback') ? 'rollback' : 'apply';
if (direction === 'rollback') await (await b.manager.start(f.session, set.id, set.fingerprint)).promise;
f.store.close();
const child = spawn(process.execPath, [path.join(workspace, 'tests/fixtures/change-set-crash-worker.mjs'), f.data, f.session.id,
set?.id ?? '-', phase, direction, JSON.stringify(b.ids)], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
t.after(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
await until(() => child.exitCode !== null, 25000); assert.equal(child.exitCode, 73, errors);
const cut = JSON.parse(output.trim()), beforeRestart = {};
for (const relative of Object.keys(b.before)) { const target = path.join(f.projectRoot, relative); beforeRestart[relative] = fs.existsSync(target) ? fs.readFileSync(target) : null; }
const recovered = new Store(f.data); f.onCleanup(() => recovered.close());
const session = recovered.session(f.session.id), manager = new ChangeSetManager(recovered); set = recovered.owned('change_set', cut.setId, session.id);
await new Promise(resolve => setTimeout(resolve, 30)); assertBatchBytes(assert, f, beforeRestart);
assert.equal(set.taskId, session.taskId); assert.equal(set.runId, f.session.runId);
if (phase === 'prepared') {
assert.equal(set.status, 'prepared'); assert.equal(set.requiresVerification, false);
manager.discard(session, set.id, set.fingerprint); assertBatchBytes(assert, f, b.before); return;
}
if (!['finished', 'rollback_finished'].includes(phase)) { assert.equal(set.status, 'unknown'); assert.equal(set.requiresVerification, true); }
await assert.rejects(manager.start(session, set.id, set.fingerprint), { status: 409 });
const result = await manager.reconcile(session, set.id);
if (result.status === 'partial') { assert.equal(result.requiresVerification, true); await manager.rollback(session, set.id, set.fingerprint); }
assert.equal(set.requiresVerification, false);
assertBatchBytes(assert, f, phase === 'finished' ? b.after : b.before);
assert.equal(recovered.publicSession(session).execution.changeSets[0].id, cut.setId);
});
}