Files
Agent-Workbench/docs/release-efficiency-1.23-evidence/receipts/release-efficiency-formal.mjs
T
2026-11-02 11:58:42 +08:00

95 lines
8.1 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { publicSnapshot, compareOriginalState, hash } from './desktop-f-state.mjs';
import { inspectHostInstallation } from '../src/core/desktop/app-installation.mjs';
const mode = process.argv[2]; assert.ok(['before', 'install', 'final'].includes(mode));
const prefix = '.qa/release-efficiency-formal-', output = prefix + mode + '.json';
assert.equal(fs.existsSync(output), false, 'An original phase result cannot be overwritten.');
const read = file => JSON.parse(fs.readFileSync(file, 'utf8'));
const save = (file, value) => fs.writeFileSync(file, JSON.stringify(value, null, 2) + '\n', { flag: 'wx' });
const url = 'http://127.0.0.1:4318', installRoot = 'C:/Users/wangy/AppData/Local/Programs/AgentWorkbench';
const dataRoot = 'C:/Users/wangy/AppData/Local/Packages/OpenAI.Codex_2p2nqsd0c76g0/LocalCache/Local/AgentWorkbench';
const release = read('releases/latest.json'), candidateName = path.basename(release.portableRoot);
assert.equal(candidateName, 'Agent-Workbench-1.23.0-win-x64-20261102025721172');
assert.equal(release.documentation.sourceCommit, '93e8bc27c4dee22a3b0c99a34c81f27fe6178c34');
const startedAt = new Date().toISOString(); let result;
async function inspectorClosed() {
await new Promise((resolve, reject) => {
const socket = net.createConnection({ host: '127.0.0.1', port: 9229 }); socket.setTimeout(1500);
socket.once('connect', () => { socket.destroy(); reject(Error('Inspector is already open.')); });
socket.once('error', error => { socket.destroy(); error.code === 'ECONNREFUSED' ? resolve() : reject(error); });
socket.once('timeout', () => { socket.destroy(); reject(Error('Inspector state is unknown.')); });
});
}
async function status() {
const bootstrap = await (await fetch(url + '/api/bootstrap')).json();
const response = await fetch(url + '/api/host/status', { headers: { 'X-Workbench-Token': bootstrap.token, Connection: 'close' } });
assert.equal(response.status, 200); return response.json();
}
const receipt = file => { const bytes = fs.readFileSync(file); return { file, bytes: bytes.length, sha256: hash(bytes) }; };
try {
await inspectorClosed();
if (mode === 'before') {
const source = read('.qa/source-package-validation.json'); assert.equal(source.success, true); assert.equal(source.version, '1.23.0');
assert.equal(path.basename(source.sourceRoot), path.basename(release.sourceRoot));
const portable = read('.qa/release-validation.json'); assert.equal(portable.success, true); assert.equal(portable.appRoot, release.portableRoot);
const installed = read(read('.qa/desktop-install-location.json').result); assert.equal(installed.success, true);
const binding = read('.qa/release-efficiency-candidate-binding.json'); assert.equal(binding.success, true); assert.equal(binding.finalSource, release.portableRoot);
assert.equal(installed.packagedSource, binding.originalSource); assert.equal(hash(fs.readFileSync(binding.originalResult)), binding.originalResultSha256);
const before = await publicSnapshot(url, dataRoot), host = await status();
assert.equal(before.version, '1.22.1'); assert.equal(before.pid, 36552); assert.deepEqual(before.active, []);
assert.equal(host.activity.active, false); assert.equal(host.desktopWindows.count, 0);
const pointer = read(path.join(installRoot, 'current.json')); assert.equal(pointer.active.version, '1.22.1');
save(prefix + 'private-before.json', before); save(prefix + 'original-pointer.json', pointer);
result = { success: true, mode, version: before.version, pid: before.pid, hasKey: before.hasKey,
originalState: receipt(prefix + 'private-before.json'), originalPointer: receipt(prefix + 'original-pointer.json'),
candidate: release.portableRoot, sourceCommit: release.documentation.sourceCommit,
sourceResult: receipt('.qa/source-package-validation.json'), portableResult: receipt('.qa/release-validation.json'), installationResult: receipt(read('.qa/desktop-install-location.json').result),
scope: 'Read-only full original state before actual final-product installer; private payloads stay local.' };
} else {
assert.equal(read(prefix + 'before.json').success, true);
const before = read(prefix + 'private-before.json'), current = await publicSnapshot(url, dataRoot), host = await status();
compareOriginalState(before, current); assert.equal(host.activity.active, false); assert.deepEqual(current.active, []);
if (mode === 'install') {
assert.equal(host.desktopWindows.count, 0); assert.equal(current.version, '1.22.1');
assert.deepEqual(read(path.join(installRoot, 'current.json')), read(prefix + 'original-pointer.json'));
assert.equal(hash(fs.readFileSync(release.installer.path)), release.installer.sha256);
const logFile = path.resolve(prefix + 'installer.log'), processFile = path.resolve(prefix + 'installer-process.txt');
const log = fs.openSync(processFile, 'wx');
let code;
try {
code = await new Promise((resolve, reject) => {
const child = spawn(release.installer.path, ['/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/LOG=' + logFile,
'/DIR=' + installRoot, '/DATA_ROOT=' + dataRoot, '/PORT=4318'], { windowsHide: true, stdio: ['ignore', log, log] });
// A lost observer must not kill an installation or trigger an automatic replay.
child.once('error', reject); child.once('close', resolve);
});
} finally { fs.closeSync(log); }
save(prefix + 'installer-exit.json', { code, logFile, installer: release.installer, finishedAt: new Date().toISOString() });
assert.equal(code, 0, 'Preserve original installer log and inspect the reported failure.');
} else { assert.equal(read(prefix + 'install.json').success, true); assert.equal(host.desktopWindows.count, 1, 'The reviewed native desktop must remain registered.'); }
const after = await publicSnapshot(url, dataRoot), retained = compareOriginalState(before, after);
assert.equal(after.version, '1.23.0'); assert.equal(after.pid, 36552);
const candidate = path.join(installRoot, 'versions', candidateName), identity = inspectHostInstallation(candidate);
assert.equal(fs.realpathSync.native(after.runtimeRoot), fs.realpathSync.native(candidate));
const pointer = read(path.join(installRoot, 'current.json')); assert.equal(pointer.active.directory, candidateName);
assert.equal(pointer.active.manifestSha256, identity.manifestSha256); assert.equal(pointer.active.runtimeIdentity, identity.identitySha256);
const progressFiles = fs.readdirSync(path.join(installRoot, '.installation')).filter(name => name.startsWith('progress-') && name.endsWith('.json'));
const operations = progressFiles.map(name => read(path.join(installRoot, '.installation', name))).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
assert.equal(operations[0].state, 'completed'); assert.equal(operations[0].candidate.manifestSha256, identity.manifestSha256);
const stages = operations[0].history.map(value => value.stage); assert.ok(stages.includes('verify-candidate')); assert.ok(stages.includes('publish-pointer'));
await inspectorClosed(); save(prefix + 'private-after-' + mode + '.json', after);
result = { success: true, mode, version: after.version, candidate, pid: after.pid, hasKey: after.hasKey, retained,
counts: { projects: after.projects, sessions: after.database.sessionIds.length, records: after.database.tables.records.count,
events: after.database.tables.events.count, artifacts: after.persistent.artifacts.length },
identity, pointer, operation: operations[0], privateState: receipt(prefix + 'private-after-' + mode + '.json'), inspectorClosed: true,
desktopWindows: (await status()).desktopWindows.count, realModelRequests: 0 };
}
} catch (error) { result = { success: false, mode, error: error.stack, realModelRequests: 0 }; process.exitCode = 1; }
save(output, { ...result, startedAt, finishedAt: new Date().toISOString() });
console.log(JSON.stringify({ success: result.success, mode, output, version: result.version, counts: result.counts, error: result.error }));