Files
Agent-Workbench/docs/model-selection-k-evidence/development/model-entry-k-install.mjs
T
2026-11-03 15:37:04 +08:00

84 lines
6.8 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import net from 'node:net';
import { spawn, execFileSync } from 'node:child_process';
import { publicSnapshot, compareOriginalState, hash } from './desktop-f-state.mjs';
import { inspectHostInstallation } from '../src/core/desktop/app-installation.mjs';
const root = path.resolve('.qa/model-entry-k/formal'); fs.mkdirSync(root, { recursive: true });
const read = file => JSON.parse(fs.readFileSync(file, 'utf8'));
const save = (name, value) => fs.writeFileSync(path.join(root, name), JSON.stringify(value, null, 2) + '\n', { flag: 'wx' });
const release = read('releases/latest.json'), sourceCommit = release.documentation.sourceCommit;
const manifest = read(path.join(release.portableRoot, 'MANIFEST.json')); assert.equal(manifest.version, '1.24.3');
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 mode = process.argv[2]; assert.ok(['before', 'install', 'final'].includes(mode));
assert.ok(!fs.existsSync(path.join(root, mode + '.json')), 'Keep prior receipts; do not retry unknown installation.');
const startedAt = new Date().toISOString();
async function api(route) {
const boot = await (await fetch(url + '/api/bootstrap', { signal: AbortSignal.timeout(20000) })).json();
const response = await fetch(url + '/api' + route, { headers: { 'X-Workbench-Token': boot.token, Connection: 'close' }, signal: AbortSignal.timeout(20000) });
assert.equal(response.status, 200, route); return response.json();
}
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 open.')); });
socket.once('error', error => { socket.destroy(); error.code === 'ECONNREFUSED' ? resolve() : reject(error); });
socket.once('timeout', () => { socket.destroy(); reject(Error('Inspector state unknown.')); });
});
}
let result;
try {
assert.equal(execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim(), sourceCommit);
assert.equal(execFileSync('git', ['status', '--porcelain'], { encoding: 'utf8' }).trim(), '');
await inspectorClosed();
const current = await publicSnapshot(url, dataRoot), host = await api('/host/status');
assert.equal(current.pid, 36552); assert.equal(current.hasKey, true); assert.deepEqual(current.active, []); assert.equal(host.activity.active, false);
if (mode === 'before') {
assert.equal(current.version, '1.24.2'); assert.equal(host.desktopWindows.count, 0);
assert.match(current.runtimeRoot.replaceAll('\\', '/'), /\/versions\/Agent-Workbench-1\.24\.2-win-x64-20261103042107173$/);
const archived = read('.qa/archives-validation.json'), portable = read('.qa/release-validation.json');
assert.equal(archived.success, true); assert.equal(portable.success, true); assert.equal(portable.appRoot, release.portableRoot);
save('private-before.json', current); save('pointer-before.json', read(path.join(installRoot, 'current.json'))); save('release.json', release);
result = { success: true, version: current.version, pid: current.pid, projects: current.projects,
sessions: current.database.sessionIds.length, tables: Object.fromEntries(Object.entries(current.database.tables).map(([name, table]) => [name, table.count])),
runtimeRoot: current.runtimeRoot, sourceCommit, hasKey: current.hasKey, active: current.active };
} else {
const before = read(path.join(root, 'private-before.json')); compareOriginalState(before, current);
assert.deepEqual(release, read(path.join(root, 'release.json')));
if (mode === 'install') {
assert.equal(current.version, '1.24.2'); assert.equal(host.desktopWindows.count, 0);
assert.deepEqual(read(path.join(installRoot, 'current.json')), read(path.join(root, 'pointer-before.json')));
assert.equal(hash(fs.readFileSync(release.installer.path)), release.installer.sha256);
const logFile = path.join(root, 'installer.log'), fd = fs.openSync(path.join(root, 'installer-process.log'), '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', fd, fd] });
child.once('error', reject); child.once('close', resolve);
});
} finally { fs.closeSync(fd); }
save('installer-exit.json', { code, finishedAt: new Date().toISOString() }); assert.equal(code, 0, 'Unknown/failed install is not automatically replayed.');
} else assert.equal(read(path.join(root, 'install.json')).success, true);
const after = await publicSnapshot(url, dataRoot), retention = compareOriginalState(before, after);
assert.equal(after.version, '1.24.3'); assert.equal(after.pid, before.pid); assert.equal(after.hasKey, true);
const candidate = path.join(installRoot, 'versions', path.basename(release.portableRoot));
assert.equal(fs.realpathSync.native(after.runtimeRoot), fs.realpathSync.native(candidate));
const identity = inspectHostInstallation(candidate), pointer = read(path.join(installRoot, 'current.json'));
assert.equal(pointer.active.directory, path.basename(candidate)); assert.equal(pointer.active.manifestSha256, identity.manifestSha256);
assert.equal(pointer.active.runtimeIdentity, identity.identitySha256);
const operation = fs.readdirSync(path.join(installRoot, '.installation')).filter(name => /^progress-.*\.json$/.test(name))
.map(name => read(path.join(installRoot, '.installation', name))).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))[0];
assert.equal(operation.state, 'completed'); assert.equal(operation.candidate.manifestSha256, identity.manifestSha256);
save(mode + '-private-after.json', after);
result = { success: true, version: after.version, pid: after.pid, runtimeRoot: after.runtimeRoot, projects: after.projects,
sessions: after.database.sessionIds.length, hasKey: after.hasKey, retention, pointer, operation,
desktopWindows: (await api('/host/status')).desktopWindows.count, inspectorClosed: true, sourceCommit };
}
} catch (error) { result = { success: false, error: error.stack }; process.exitCode = 1; }
result = { ...result, mode, startedAt, finishedAt: new Date().toISOString(), realModelRequests: 0,
scope: 'Public API/full original row/export/artifact hashes; installer retains original running host and Provider. No credential extraction or task replay.' };
save(mode + '.json', result); console.log(JSON.stringify(result));