Files
Agent-Workbench/scripts/verify-desktop-install.mjs
2026-11-06 06:21:59 +08:00

190 lines
22 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import http from 'node:http';
import { spawn, execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { createHash, randomUUID } from 'node:crypto';
import { _electron } from 'playwright';
import { planDesktopBundle, copyDesktopBundle, buildNativeLauncher } from './bundle-desktop.mjs';
import { buildWindowsInstaller } from './build-installer.mjs';
import { HostController } from '../src/core/desktop/host-controller.mjs';
import { InstallationManager, snapshotPersistentData } from '../src/core/desktop/install-manager.mjs';
import { observeWindowsProcess } from '../src/core/desktop/process-identity.mjs';
import { projectEnvironment } from '../src/core/process-environment.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-desktop-install.mjs — isolated actual user-level setup/update/uninstall, full data retention, owned DPAPI/HTTP fixture, active-window and request guards, fault rollback and original PID retention. No configured-model credentials.'); process.exit(0); }
assert.equal(process.argv.length, 2);
const appRoot = process.cwd(), root = path.resolve('.qa', 'desktop-install-' + randomUUID()), installRoot = path.join(root, 'program'), dataRoot = path.join(root, 'user-data');
const packagedSource = process.env.AGENT_WORKBENCH_QA_APP_ROOT ? fs.realpathSync.native(process.env.AGENT_WORKBENCH_QA_APP_ROOT) : null;
const version = JSON.parse(fs.readFileSync('package.json')).version, results = [], appId = randomUUID(), hash = value => createHash('sha256').update(value).digest('hex');
fs.mkdirSync(root, { recursive: true });
const reserved = net.createServer(); await new Promise(resolve => reserved.listen(0, '127.0.0.1', resolve)); const port = reserved.address().port; await new Promise(resolve => reserved.close(resolve));
const record = (name, detail = {}) => { results.push({ name, passed: true, ...detail }); console.log('PASS ' + name); };
const until = async (action, timeout = 60000) => { const end = Date.now() + timeout; while (!await action()) { if (Date.now() > end) throw Error('Timeout: ' + stage); await new Promise(resolve => setTimeout(resolve, 100)); } };
const run = promisify(execFile), env = projectEnvironment();
let stage = 'prepare owned full releases', controller, application, ownedModel, report, heldRequest, releaseRequest, original, installedUI = null;
const launcher = path.resolve('.qa/native-launcher/AgentWorkbench.exe'); buildNativeLauncher(appRoot, launcher); const desktop = planDesktopBundle(appRoot);
function payload(name) {
const directory = path.join(root, name); fs.mkdirSync(directory);
if (packagedSource) {
const original = JSON.parse(fs.readFileSync(path.join(packagedSource, 'MANIFEST.json')));
assert.equal(original.kind, 'portable'); assert.equal(original.version, version);
for (const entry of original.files) { const file = path.join(packagedSource, entry.path); assert.equal(hash(fs.readFileSync(file)), entry.sha256, entry.path); }
fs.cpSync(packagedSource, directory, { recursive: true }); fs.unlinkSync(path.join(directory, 'MANIFEST.json'));
} else {
for (const item of ['dist-client', 'dist-server', 'dist-desktop', 'public', 'examples']) fs.cpSync(path.join(appRoot, item), path.join(directory, item), { recursive: true });
fs.mkdirSync(path.join(directory, 'scripts')); fs.copyFileSync('scripts/python-check-runner.py', path.join(directory, 'scripts/python-check-runner.py'));
fs.mkdirSync(path.join(directory, 'runtime')); fs.copyFileSync(process.execPath, path.join(directory, 'runtime/node.exe'));
fs.cpSync(path.join(path.dirname(process.execPath), 'node_modules/npm'), path.join(directory, 'runtime/node_modules/npm'), { recursive: true });
copyDesktopBundle(appRoot, directory, desktop, { launcher });
}
fs.writeFileSync(path.join(directory, 'owned-release.txt'), name + '\n');
// A valid PE overlay explicitly models a future owned native bootstrap with
// different bytes. The updated root entry must follow it and roll back with it.
if (name.startsWith('owned-b-')) fs.appendFileSync(path.join(directory, 'AgentWorkbench.exe'), '\nowned-native-bootstrap-update-fixture\n');
const walk = relative => fs.readdirSync(path.join(directory, relative), { withFileTypes: true }).flatMap(entry => {
const next = path.posix.join(relative, entry.name), file = path.join(directory, next); return entry.isDirectory() ? walk(next) : [{ path: next, bytes: fs.statSync(file).size, sha256: hash(fs.readFileSync(file)) }];
});
fs.writeFileSync(path.join(directory, 'MANIFEST.json'), JSON.stringify({ app: 'agent-workbench', kind: 'portable', version, platform: 'win32-x64', nodeVersion: process.version, files: walk('') }, null, 2)); return directory;
}
async function setup(installer, label, expected = 0) {
const log = path.join(root, label + '-setup.log');
let code = 0, output = '';
try { const result = await run(installer.path, ['/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/LOG=' + log, '/DIR=' + installRoot, '/DATA_ROOT=' + dataRoot, '/PORT=' + port], { windowsHide: true, timeout: 600000, maxBuffer: 1024 * 1024, env }); output = result.stdout + result.stderr; }
catch (error) { code = typeof error.code === 'number' ? error.code : -1; output = String(error.stdout ?? '') + String(error.stderr ?? ''); }
fs.writeFileSync(path.join(root, label + '-process.txt'), output);
if (expected === 0) assert.equal(code, 0, 'Actual installer failed; inspect ' + log); else assert.notEqual(code, 0, 'Actual installer unexpectedly accepted the rejected condition.');
const logText = fs.readFileSync(log, 'utf8'); assert.match(logText, /AWBPROGRESS /, 'Actual installer must receive original stage output.');
const records = fs.readdirSync(path.join(installRoot, '.installation')).filter(name => /^progress-.*\.json$/.test(name)).map(name => JSON.parse(fs.readFileSync(path.join(installRoot, '.installation', name)))).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
assert.equal(records[0].state, expected === 0 ? 'completed' : 'failed');
if (expected === 0) { assert.ok(records[0].history.some(row => row.stage === 'verify-candidate' && row.scope === 'full-manifest' && row.counts.completedFiles === row.counts.totalFiles)); assert.match(logText, /AWBFILES \d+\/\d+/); }
return { code, log, sha256: hash(fs.readFileSync(log)) };
}
async function launchElectron() {
const current = JSON.parse(fs.readFileSync(path.join(installRoot, 'current.json'))), directory = path.join(installRoot, 'versions', current.active.directory);
const client = await _electron.launch({ executablePath: path.join(directory, 'desktop/AgentWorkbench.exe'), args: ['--app-root', directory, '--data-root', dataRoot, '--port', String(port), '--hidden-window'], cwd: directory, env, timeout: 60000 });
const page = await client.firstWindow(); await page.locator('.connection.connected').waitFor({ timeout: 30000 }); return client;
}
function ownedManager(candidateRoot) { return new InstallationManager({ installRoot, dataRoot, candidateRoot, port }); }
try {
const a = payload('owned-a-' + version), b = payload('owned-b-' + version), fault = payload('owned-fault-' + version);
stage = 'compile isolated actual installers';
const firstSetup = buildWindowsInstaller({ root: appRoot, portableRoot: a, outputRoot: root, qaFixture: { appId } });
const secondSetup = buildWindowsInstaller({ root: appRoot, portableRoot: b, outputRoot: root, qaFixture: { appId } });
const faultSetup = buildWindowsInstaller({ root: appRoot, portableRoot: fault, outputRoot: root, qaFixture: { appId, fault: 'corrupt-installed-candidate' } });
stage = 'actual clean user-level installation'; const installed = await setup(firstSetup, 'clean');
const activeA = path.join(installRoot, 'versions', path.basename(a)); controller = new HostController({ appRoot: activeA, dataRoot, port, nodePath: process.execPath });
original = (await controller.start()).identity; application = await launchElectron();
assert.equal((await controller.status()).desktopWindows.count, 1); record(stage, { installer: firstSetup, ...installed, hostPid: original.pid, electronPid: await application.evaluate(() => process.pid) });
stage = 'actual installer rejects an open attested desktop window'; const windowRejection = await setup(secondSetup, 'window-block', 1);
assert.equal(ownedManager(null).current().active.directory, path.basename(a)); assert.equal((await controller.connect()).identity.pid, original.pid); record(stage, windowRejection);
await application.close(); application = null; await until(async () => (await controller.status()).desktopWindows.count === 0);
const key = 'owned-install-credential-' + randomUUID(); let authenticated = 0, hold = false;
ownedModel = http.createServer(async (request, response) => {
const chunks = []; for await (const bytes of request) chunks.push(bytes);
assert.equal(request.headers.authorization, 'Bearer ' + key); authenticated++;
if (hold) { heldRequest = true; await new Promise(resolve => { releaseRequest = resolve; request.on('close', resolve); }); }
response.writeHead(200, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ choices: [{ message: { role: 'assistant', content: 'OK' }, finish_reason: 'stop' }], usage: { prompt_tokens: 4, completion_tokens: 1, total_tokens: 5 } }));
}); await new Promise(resolve => ownedModel.listen(0, '127.0.0.1', resolve));
const connection = await controller.api('/model-center/connections', { method: 'POST', input: { name: 'Owned desktop install credential', model: 'owned', baseUrl: 'http://127.0.0.1:' + ownedModel.address().port + '/v1', apiKey: key, persistKey: true } });
const connectionId = connection.id ?? connection.connection?.id ?? connection.center?.connections.at(-1).id;
assert.ok(connectionId); await controller.api('/model-center/probe', { method: 'POST', input: { id: connectionId } }); assert.equal(authenticated, 1);
stage = 'actual update rejects a pending owned model request'; hold = true;
// Manifest verification is synchronous and can occupy this fixture's event
// loop. Keep the explicitly held local response pending across that work;
// this is the QA client's deadline, not a product/model timeout override.
const pendingStarted = Date.now();
const pending = controller.api('/model-center/probe', { method: 'POST', input: { id: connectionId }, timeoutMs: 300000 }); pending.catch(() => {});
await until(() => heldRequest); await assert.rejects(ownedManager(null).preflight(), /任务|请求/);
assert.equal((await controller.connect()).identity.pid, original.pid);
assert.equal(authenticated, 2, 'Exactly the initial and explicitly held owned requests; no retry.');
record(stage, { fixture: 'Actual owned HTTP response held until full manifest/preflight and original PID verification settle. No configured-model call or product timeout change.', elapsedMs: Date.now() - pendingStarted });
releaseRequest?.(); hold = false; assert.equal((await pending).ok, true);
stage = 'actual installer rejects a persistent original task approval';
const projectRoot = path.join(root, 'approval-project'); fs.mkdirSync(projectRoot); fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Owned installer approval project\n');
const project = await controller.api('/projects', { method: 'POST', input: { root: projectRoot } }), session = await controller.api('/sessions', { method: 'POST', input: { projectId: project.id } });
await controller.api('/sessions/' + session.id + '/messages', { method: 'POST', input: { content: 'Read this owned project and prepare the offline demonstration proposal.' } });
await until(async () => (await controller.api('/sessions/' + session.id)).approvals.some(value => value.status === 'pending'));
const approval = (await controller.api('/sessions/' + session.id)).approvals.find(value => value.status === 'pending');
const approvalRejection = await setup(secondSetup, 'approval-block', 1); assert.equal((await controller.api('/sessions/' + session.id)).approvals.find(value => value.status === 'pending').id, approval.id);
await controller.api('/sessions/' + session.id + '/stop', { method: 'POST', input: {} }); record(stage, { ...approvalRejection, approvalId: approval.id });
stage = 'actual manifest fault installer preserves original pointer, PID and all data'; const snapshot = snapshotPersistentData(dataRoot), pointer = fs.readFileSync(path.join(installRoot, 'current.json'));
const faultResult = await setup(faultSetup, 'candidate-fault', 1);
assert.deepEqual(fs.readFileSync(path.join(installRoot, 'current.json')), pointer); assert.deepEqual(snapshotPersistentData(dataRoot), snapshot); assert.equal((await controller.connect()).identity.pid, original.pid); assert.ok(fs.existsSync(path.join(installRoot, 'AgentWorkbench.exe'))); record(stage, faultResult);
stage = 'actual pointer-published fault rolls original host and pointer back';
const originalBootstrap = fs.readFileSync(path.join(installRoot, 'AgentWorkbench.exe'));
const faultCandidate = path.join(installRoot, 'versions', 'owned-pointer-fault'); fs.cpSync(b, faultCandidate, { recursive: true });
await assert.rejects(ownedManager(faultCandidate).activate({ afterPointer: () => { throw Error('owned-post-pointer-fault'); } }), /owned-post-pointer-fault/);
assert.deepEqual(fs.readFileSync(path.join(installRoot, 'current.json')), pointer); assert.deepEqual(snapshotPersistentData(dataRoot), snapshot); assert.equal((await controller.connect()).identity.pid, original.pid); record(stage, { fixture: 'Explicit in-process afterPointer error with real retained host; original pointer bytes, database rows and artifact bytes checked.' });
assert.deepEqual(fs.readFileSync(path.join(installRoot, 'AgentWorkbench.exe')), originalBootstrap);
stage = 'actual update retains original PID, instance, DPAPI ciphertext and every persistent byte';
const updated = await setup(secondSetup, 'update'); const activeB = path.join(installRoot, 'versions', path.basename(b));
controller = new HostController({ appRoot: activeB, dataRoot, port, nodePath: process.execPath }); const after = (await controller.connect()).identity;
assert.equal(after.pid, original.pid); assert.equal(after.instanceId, original.instanceId); assert.deepEqual(snapshotPersistentData(dataRoot), snapshot); record(stage, { ...updated, hostPid: after.pid, instanceId: after.instanceId, data: snapshot });
assert.notDeepEqual(fs.readFileSync(path.join(installRoot, 'AgentWorkbench.exe')), originalBootstrap);
assert.deepEqual(fs.readFileSync(path.join(installRoot, 'AgentWorkbench.exe')), fs.readFileSync(path.join(activeB, 'AgentWorkbench.exe')));
stage = 'installed native bootstrap verifies and opens the registered release';
const nativeLauncher = spawn(path.join(installRoot, 'AgentWorkbench.exe'), ['--hidden-window'], { windowsHide: true, stdio: 'ignore', env });
await new Promise((resolve, reject) => { nativeLauncher.once('error', reject); nativeLauncher.once('exit', code => code === 0 ? resolve() : reject(Error('Owned native bootstrap failed (' + code + ')'))); });
await until(async () => (await controller.status()).desktopWindows.count === 1);
const nativeWindow = (await controller.status()).desktopWindows.entries[0], nativeProcess = observeWindowsProcess(nativeWindow.pid); assert.ok(nativeProcess); assert.equal((await controller.connect()).identity.pid, original.pid);
// A controlled WM_CLOSE to the owned main window is delegated to Electron by
// a separate automation connection below; no unknown process is terminated.
const child = spawn(path.join(activeB, 'desktop/AgentWorkbench.exe'), ['--app-root', activeB, '--data-root', dataRoot, '--port', String(port), '--hidden-window'], { cwd: activeB, env, windowsHide: true, stdio: 'ignore' });
await new Promise(resolve => child.once('exit', resolve)); assert.equal((await controller.status()).desktopWindows.count, 1);
record(stage, { electronPid: nativeWindow.pid, hostPid: original.pid, repeatLaunchReusedWindow: true });
// The native process has no debugger. Its owned renderer closes itself through
// the product's browser window action using a localhost, attested stop below.
stage = 'native desktop remains registered before explicit owned cleanup';
// Closing only this exactly observed test Electron tree after the launch check
// models a desktop crash. Host/persistent results remain; stale registration is
// removed through the normal identity check, without signalling a reused PID.
const observed = observeWindowsProcess(nativeWindow.pid); assert.equal(observed.createdAt, nativeWindow.createdAt);
await run('taskkill.exe', ['/PID', String(observed.pid), '/T', '/F'], { windowsHide: true, timeout: 15000, env });
await until(async () => (await controller.status()).desktopWindows.count === 0); assert.equal((await controller.connect()).identity.pid, original.pid);
record(stage, { fixture: 'Explicit owned Electron crash after exact actual PID/creation check; independent host retained.' });
stage = 'real host restart restores the owned saved credential and actual HTTP authentication';
const beforeRestart = snapshotPersistentData(dataRoot); await controller.stop(); const restarted = (await controller.start()).identity;
assert.notEqual(restarted.pid, original.pid); assert.deepEqual(snapshotPersistentData(dataRoot), beforeRestart);
await controller.api('/model-center/probe', { method: 'POST', input: { id: connectionId } }); assert.equal(authenticated, 3); record(stage, { originalPid: original.pid, restartedPid: restarted.pid, authenticatedRequests: authenticated });
if (['1.24.0', '1.24.1', '1.24.2', '1.24.3', '1.24.4', '1.24.5', '1.24.6', '1.24.7', '1.24.8', '1.24.9', '1.25.0', '1.25.1', '1.25.2', '1.25.3', '1.25.5'].includes(version)) {
stage = 'actual installed current results and saved HTML user paths';
const pointer = JSON.parse(fs.readFileSync(path.join(installRoot, 'current.json')));
const installedRoot = fs.realpathSync.native(path.join(installRoot, 'versions', pointer.active.directory)), proofs = [];
const suites = [
['verify-file-results-ui.mjs', [], 16], ['verify-local-files.mjs', [], 9],
...(['1.24.9', '1.25.0', '1.25.1', '1.25.2', '1.25.3', '1.25.5'].includes(version) ? [['verify-requirements-ui.mjs', [], 7], ['verify-requirements-desktop.mjs', [], 3], ['verify-turn-progress-ui.mjs', [], 9], ['verify-turn-progress-ui.mjs', ['--desktop'], 9]] : []),
...(['1.25.0', '1.25.1', '1.25.2', '1.25.3', '1.25.5'].includes(version) ? [['verify-task-agreement-ui.mjs', [], 6], ['verify-task-agreement-ui.mjs', ['--desktop'], 6], ['verify-task-follow-up-ui.mjs', [], 5], ['verify-task-follow-up-ui.mjs', ['--desktop'], 5]] : []),
...(['1.25.3', '1.25.5'].includes(version) ? [['verify-reasoning-settings-ui.mjs', [], 6], ['verify-reasoning-settings-ui.mjs', ['--desktop'], 6], ['verify-model-catalog-ui.mjs', [], 8], ['verify-model-catalog-ui.mjs', ['--desktop'], 8]] : []),
...(version === '1.25.5' ? [['verify-model-capacity-ui.mjs', [], 3], ['verify-model-capacity-ui.mjs', ['--desktop'], 3], ['verify-unrestricted-ui.mjs', [], 10], ['verify-unrestricted-ui.mjs', ['--desktop'], 11]] : []),
];
for (const [script, args, expected] of suites) {
const output = await run(process.execPath, [path.join(appRoot, 'scripts', script), ...args], { cwd: appRoot,
env: { ...env, AGENT_WORKBENCH_QA_APP_ROOT: installedRoot }, windowsHide: true, timeout: 180000, maxBuffer: 1024 * 1024 });
const log = path.join(root, 'installed-' + script + (args.includes('--desktop') ? '-desktop' : '') + '.log'); fs.writeFileSync(log, output.stdout + output.stderr);
const summary = output.stdout.trim().split(/\r?\n/).filter(line => line.startsWith('{')).map(line => { try { return JSON.parse(line); } catch { return null; } }).findLast(row => row?.success !== undefined);
const proofRoot = summary.directory ?? summary.root; assert.ok(proofRoot && path.resolve(proofRoot).startsWith(path.join(appRoot, '.qa') + path.sep));
const proof = JSON.parse(fs.readFileSync(path.join(proofRoot, 'result.json'), 'utf8'));
assert.equal(proof.success, true); assert.equal(proof.version, version); assert.equal(fs.realpathSync.native(proof.appRoot), installedRoot);
assert.equal((proof.checks ?? proof.passed).length, expected); assert.deepEqual(proof.errors, []);
assert.equal(proof.realModelRequests, 0); proofs.push({ script, proof, log, sha256: hash(fs.readFileSync(log)) });
}
installedUI = { success: true, version, installedRoot, packagedSource, proofs, realModelRequests: 0 };
fs.writeFileSync('.qa/existing-results-installed-ui.json', JSON.stringify(installedUI, null, 2)); record(stage, installedUI);
}
stage = 'actual uninstall closes only the idle owned host and preserves user data';
const uninstallSnapshot = snapshotPersistentData(dataRoot), uninstallLog = path.join(root, 'uninstall.log');
await run(path.join(installRoot, 'unins000.exe'), ['/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/LOG=' + uninstallLog], { windowsHide: true, timeout: 180000, env });
assert.equal(observeWindowsProcess(restarted.pid), null); assert.deepEqual(snapshotPersistentData(dataRoot), uninstallSnapshot); assert.equal(fs.existsSync(path.join(installRoot, 'AgentWorkbench.exe')), false); assert.equal(fs.existsSync(path.join(installRoot, 'current.json')), false);
record(stage, { log: uninstallLog, originalDataRoot: dataRoot, data: uninstallSnapshot });
report = { success: true, version, root, installRoot, dataRoot, port, appId, packagedSource, results, installedUI, realModelRequests: 0, scope: 'Actual isolated user-level Windows installers and shipped Electron/Node runtimes; own credentials and HTTP fixture only. QA appId, corruption, pointer-fault and crash fixtures explicitly recorded.' };
} catch (error) { report = { success: false, version, root, installRoot, dataRoot, port, stage, error: error.stack, results, realModelRequests: 0 }; throw error; }
finally {
releaseRequest?.(); await application?.close().catch(() => {});
if (controller) { try { const status = await controller.status(); if (!status.desktopWindows.count) await controller.stop(); } catch {} }
if (ownedModel) { ownedModel.closeAllConnections(); await new Promise(resolve => ownedModel.close(resolve)); }
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(report, null, 2)); fs.writeFileSync('.qa/desktop-install-location.json', JSON.stringify({ root, result: path.join(root, 'result.json') })); console.log('EVIDENCE ' + root);
}