Files
Agent-Workbench/tests/release-efficiency.test.mjs
T
Agent Workbench Dev 0a82636d16 fix(release): advance served APP_VERSION to 1.25.1 and pin it to package.json
The 1.25.0 literal in diagnostics.mjs lagged the package bump, so a hot upgrade served 1.25.0 and the installer correctly rolled back on HOST_VERSION_MISMATCH. Advance the constant, add a release-efficiency guard that fails when the constant or compiled bundles drift from package.json, and add the 1.25.1 formal-release driver that refuses a candidate whose compiled server version is stale.

No product behaviour change beyond the version string; the reservation and install-scan fixes are unchanged.
2026-11-04 18:38:54 +08:00

171 lines
15 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { randomUUID, createHash } from 'node:crypto';
import { fixture, workspace } from './helpers.mjs';
import { createDocumentationBundle, REQUIRED_REPORTS } from '../scripts/documentation-bundle.mjs';
import { InstallationManager, validateInstalledVersion, snapshotPersistentData } from '../src/core/desktop/install-manager.mjs';
import { InstallationProgress, readInstallationProgress } from '../src/core/desktop/installation-progress.mjs';
import { approvalPresentation, approvalKind } from '../src/shared/approval-presentation.mjs';
import { mobileSession } from '../src/core/companion/projection.mjs';
import { approvalBinding } from '../src/shared/contracts/companion.mjs';
const hash = value => createHash('sha256').update(value).digest('hex');
const json = file => JSON.parse(fs.readFileSync(file, 'utf8'));
function candidate(f, name = 'owned-a') {
const root = path.join(f.root, 'install', 'versions', name); fs.mkdirSync(root, { recursive: true });
for (const dir of ['dist-client', 'dist-server', 'dist-desktop']) fs.cpSync(path.join(workspace, dir), path.join(root, dir), { recursive: true });
for (const file of ['AgentWorkbench.exe', 'runtime/node.exe', 'desktop/AgentWorkbench.exe', 'desktop/resources/app/main.cjs', 'desktop/resources/app/package.json']) {
const target = path.join(root, file); fs.mkdirSync(path.dirname(target), { recursive: true }); fs.writeFileSync(target, 'owned-static-fixture-only');
}
const walk = (dir, prefix = '') => fs.readdirSync(dir, { withFileTypes: true }).flatMap(entry => {
const next = path.posix.join(prefix, entry.name), file = path.join(dir, entry.name);
return entry.isDirectory() ? walk(file, next) : [{ path: next, bytes: fs.statSync(file).size, sha256: hash(fs.readFileSync(file)) }];
});
fs.writeFileSync(path.join(root, 'MANIFEST.json'), JSON.stringify({ app: 'agent-workbench', kind: 'portable', platform: 'win32-x64', version: json(path.join(workspace, 'package.json')).version, files: walk(root) }));
return root;
}
function manager(f, root, extra = {}) {
const m = new InstallationManager({ installRoot: path.join(f.root, 'install'), dataRoot: f.data, candidateRoot: root, port: 49999, ...extra });
m.host = async () => null; return m;
}
function documentation(f) {
const root = path.join(f.root, 'source'), portableRoot = path.join(f.root, 'portable'), evidenceRoot = path.join(f.root, 'evidence');
fs.mkdirSync(path.join(root, 'docs', 'raw'), { recursive: true });
for (const file of REQUIRED_REPORTS) { fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); fs.writeFileSync(path.join(root, file), '# 原研究\r\n[原件](../raw/failed.json)\r\n'); }
fs.writeFileSync(path.join(root, 'docs', 'raw', 'failed.json'), '{"success":false,"original":"失败也保留"}\n');
fs.writeFileSync(path.join(root, 'docs', 'GUIDE.md'), '# 指南\n[原失败](raw/failed.json)\n[历史缺失](old-missing.json)\n[异常链接](bad%XX.json)\n');
fs.writeFileSync(path.join(root, 'README.md'), '# 工作台\n[证据](docs/raw/failed.json)\n');
return { root, portableRoot, evidenceRoot, archiveName: 'Agent-Workbench-evidence.zip', sourceCommit: 'a'.repeat(40) };
}
test('runtime guides map archived evidence while all original reports and successful/failed source bytes remain independently verifiable', t => {
const f = fixture(t), input = documentation(f), result = createDocumentationBundle(input);
assert.equal(result.originalFiles, 6);
for (const row of result.evidenceEntries) assert.equal(hash(fs.readFileSync(path.join(input.evidenceRoot, row.path))), row.sha256);
assert.equal(hash(fs.readFileSync(path.join(input.evidenceRoot, 'MANIFEST.json'))), result.evidenceManifestSha256);
for (const file of REQUIRED_REPORTS) assert.deepEqual(fs.readFileSync(path.join(input.portableRoot, file)), fs.readFileSync(path.join(input.root, file)));
assert.equal(fs.existsSync(path.join(input.portableRoot, 'docs/raw/failed.json')), false);
assert.match(fs.readFileSync(path.join(input.portableRoot, 'docs/GUIDE.md'), 'utf8'), /EVIDENCE-CATALOG\.md#e-[a-f0-9]{16}/);
assert.ok(result.references.some(row => row.disposition === 'historical-source-not-in-repository'));
assert.ok(result.references.some(row => row.disposition === 'malformed-historical-reference'));
assert.equal(json(path.join(input.evidenceRoot, 'docs/raw/failed.json')).success, false);
assert.throws(() => createDocumentationBundle(input), /new directory/);
});
test('missing required research reports fail packaging rather than creating a successful placeholder', t => {
const f = fixture(t), input = documentation(f); fs.unlinkSync(path.join(input.root, REQUIRED_REPORTS[0]));
assert.throws(() => createDocumentationBundle(input), /Required original research report missing/);
assert.equal(fs.existsSync(input.evidenceRoot), false);
});
test('full verification reports only verified bytes, complete declared scope and actual directory inventory; changed/deleted files still fail', t => {
const f = fixture(t), root = candidate(f), counts = [];
validateInstalledVersion(root, { onProgress: value => counts.push(value) });
const manifest = json(path.join(root, 'MANIFEST.json')), full = counts.filter(row => row.scope === 'full-manifest');
assert.equal(full[0].completedFiles, 0); assert.equal(full.at(-1).completedFiles, manifest.files.length);
assert.equal(full.at(-1).completedBytes, manifest.files.reduce((sum, row) => sum + row.bytes, 0));
assert.ok(full.every((row, index) => row.completedFiles === index));
assert.equal(counts.filter(row => row.scope === 'directory-inventory').at(-1).completedFiles, manifest.files.length + 1);
const file = path.join(root, 'dist-client/index.html'), original = fs.readFileSync(file);
fs.appendFileSync(file, 'altered'); assert.throws(() => validateInstalledVersion(root));
fs.writeFileSync(file, original); fs.unlinkSync(file); assert.throws(() => validateInstalledVersion(root));
});
test('preflight and activation continue one monotonic operation bound to installer hash; terminal replay and changed origin are rejected', async t => {
const f = fixture(t), root = candidate(f), operationId = randomUUID(), manifestSha = hash(fs.readFileSync(path.join(root, 'MANIFEST.json'))), samples = [];
const opts = { operationId, expectedManifestSha256: manifestSha, onProgress: value => samples.push(value) };
const first = await manager(f, root, opts).preflight(); assert.equal(json(first.progressFile).state, 'ready-to-copy');
const prior = json(first.progressFile), result = await manager(f, root, opts).activate();
assert.equal(result.operationId, operationId); assert.equal(result.progressFile, first.progressFile);
const progress = json(first.progressFile); assert.equal(progress.state, 'completed'); assert.ok(progress.sequence > prior.sequence); assert.ok(progress.elapsedMs >= prior.elapsedMs);
assert.equal(json(result.receiptFile).operationId, operationId); assert.equal(progress.detail.receiptFile, result.receiptFile);
assert.ok(samples.every((row, index) => !index || row.sequence > samples[index - 1].sequence));
await assert.rejects(manager(f, root, opts).activate(), /重放/);
await assert.rejects(manager(f, root, { ...opts, expectedManifestSha256: 'b'.repeat(64) }).activate(), /来源/);
});
test('preflight failures and fixed-candidate mismatches retain the actual reason without publishing a pointer', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root), unknown = path.join(m.installRoot, 'unrelated.txt'); fs.writeFileSync(unknown, 'owned-original');
await assert.rejects(m.preflight(), /未登记/);
let progress = fs.readdirSync(m.stateRoot).filter(name => name.startsWith('progress-')).map(name => json(path.join(m.stateRoot, name)));
assert.equal(progress[0].state, 'failed'); assert.match(progress[0].detail.error.message, /未登记/);
fs.unlinkSync(unknown);
const mismatch = manager(f, root, { expectedManifestSha256: 'b'.repeat(64) });
await assert.rejects(mismatch.activate(), /固定的原清单不同/); assert.equal(mismatch.current(), null);
});
test('cancel finishes only the original ready preparation, preserves data and pointer and refuses replay', async t => {
const f = fixture(t), root = candidate(f), operationId = randomUUID(), m = manager(f, root, { operationId }), before = snapshotPersistentData(f.data);
const first = await m.preflight(), result = await manager(f, root, { operationId }).cancelPreparation();
assert.equal(result.activated, false); assert.equal(json(first.progressFile).state, 'cancelled');
assert.equal(m.current(), null); assert.deepEqual(snapshotPersistentData(f.data), before);
await assert.rejects(manager(f, root, { operationId }).cancelPreparation(), /重放/);
await assert.rejects(manager(f, root, { operationId: randomUUID() }).cancelPreparation(), /没有可取消/);
});
test('the installation lock rejects concurrent same-id operations before they can overwrite original progress', async t => {
const f = fixture(t), root = candidate(f), operationId = randomUUID(), m = manager(f, root, { operationId });
let release, entered; const ready = new Promise(resolve => entered = resolve), held = new Promise(resolve => release = resolve);
m.host = async () => { entered(); await held; return null; };
const first = m.preflight(); await ready;
const file = path.join(m.stateRoot, 'progress-' + operationId + '.json'), before = fs.readFileSync(file);
await assert.rejects(manager(f, root, { operationId }).preflight(), /仍在运行/);
assert.deepEqual(fs.readFileSync(file), before); release(); await first;
});
test('original post-publication failure is linked to its rollback receipt and every original byte survives', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root), before = snapshotPersistentData(f.data);
await assert.rejects(m.activate({ afterPointer: () => { throw Error('owned-original-publication-failure'); } }), /owned-original-publication-failure/);
const progress = json(path.join(m.stateRoot, fs.readdirSync(m.stateRoot).find(name => name.startsWith('progress-'))));
assert.equal(progress.state, 'failed'); assert.match(progress.detail.error.message, /owned-original-publication-failure/);
assert.equal(json(progress.detail.receiptFile).state, 'failed'); assert.equal(m.current(), null); assert.deepEqual(snapshotPersistentData(f.data), before);
});
test('a candidate mutation at the publication boundary still fails even after an earlier successful full validation', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); await m.activate(); const original = fs.readFileSync(m.recordFile);
const next = candidate(f, 'owned-b'); m.candidateRoot = next;
await assert.rejects(m.activate({ beforePointer: () => fs.appendFileSync(path.join(next, 'dist-client/index.html'), 'changed-during-update') }));
assert.deepEqual(fs.readFileSync(m.recordFile), original);
});
test('public progress marks a lost process unknown without editing its original record or claiming completion', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root); await m.activate();
const p = new InstallationProgress({ stateRoot: m.stateRoot, installRoot: m.installRoot, dataRoot: m.dataRoot, command: 'preflight' });
const value = json(p.file); value.process.createdAt = 'owned-mismatched-process-time'; value.updatedAt = '2099-01-01T00:00:00.000Z'; fs.writeFileSync(p.file, JSON.stringify(value));
const before = fs.readFileSync(p.file), status = readInstallationProgress({ appRoot: root, dataRoot: f.data });
assert.equal(status.operations[0].state, 'unknown'); assert.equal(status.operations[0].interrupted, true); assert.deepEqual(fs.readFileSync(p.file), before);
assert.equal(readInstallationProgress({ appRoot: workspace, dataRoot: f.data }).installed, false);
});
test('throwing console/UI progress observers cannot decide the installation result', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root, { onProgress() { throw Error('owned-observer-error'); } });
const result = await m.activate(); assert.equal(result.phase, 'activated'); assert.equal(json(result.progressFile).state, 'completed');
});
test('a progress-only write failure after committed publication returns the authoritative receipt and a warning', async t => {
const f = fixture(t), root = candidate(f), m = manager(f, root);
const result = await m.activate({ afterPointer: () => { m.progress.finish = () => { throw Error('owned-status-storage-failure'); }; } });
assert.equal(result.phase, 'activated'); assert.equal(result.progressWarning, 'owned-status-storage-failure');
assert.equal(json(result.receiptFile).state, 'activated'); assert.equal(m.current().active.directory, path.basename(root));
});
test('browser and MCP approvals use actual tool categories; mobile exposes only category while keeping original fingerprint', t => {
const f = fixture(t), session = f.session; session.taskId = randomUUID(); session.runId = randomUUID();
for (const [field, kind] of [['browsingTabId', 'browsing'], ['mcpRegistrationId', 'mcp'], ['environmentPlanId', 'environment'], ['browserPlanId', 'page-check'], ['checkPlanId', 'check']]) {
const approval = { id: randomUUID(), taskId: session.taskId, runId: session.runId, projectId: session.projectId, [field]: randomUUID(), status: 'pending', command: 'owned fixed operation', browsingInput: { secret: 'not-projected' } };
session.approvals = [approval]; const original = structuredClone(approval), expected = approvalBinding(session, approval);
const projected = mobileSession(f.store, { waiters: new Map([[approval.id, { runId: session.runId }]]) }, session).approvals[0];
assert.equal(approvalKind(approval), kind); assert.equal(projected.kind, kind); assert.equal(projected.fingerprint, expected);
assert.equal(Object.hasOwn(projected, 'browsingInput'), false); assert.deepEqual(approval, original);
assert.equal(approvalPresentation(approval).title, approvalPresentation(projected).title);
}
assert.equal(approvalPresentation({ browsingTabId: 'owned' }).title, '确认浏览器操作');
assert.equal(approvalPresentation({ browsingTabId: 'owned' }).denied, '浏览器操作未执行。');
});
test('the compiled runtime version constant tracks package.json so a release bump cannot ship a stale served version', async () => {
// A 1.25.1 formal upgrade rolled back on HOST_VERSION_MISMATCH because the
// hardcoded APP_VERSION lagged package.json. Pin them together.
const declared = json(path.join(workspace, 'package.json')).version;
const source = fs.readFileSync(path.join(workspace, 'src', 'server', 'diagnostics.mjs'), 'utf8');
const match = /export const APP_VERSION = '([0-9]+\.[0-9]+\.[0-9]+)';/.exec(source);
assert.ok(match, 'diagnostics.mjs must export a literal APP_VERSION.');
assert.equal(match[1], declared, 'APP_VERSION must equal package.json version.');
for (const bundle of ['dist-server/server.mjs', 'dist-server/hot-upgrade.mjs']) {
const file = path.join(workspace, bundle);
if (!fs.existsSync(file)) continue;
const served = /APP_VERSION\s*=\s*["']([0-9]+\.[0-9]+\.[0-9]+)["']/.exec(fs.readFileSync(file, 'utf8'));
assert.equal(served?.[1], declared, bundle + ' must embed the current version; rebuild after a bump.');
}
});