feat(tasks): review sourced requirements against current original evidence

This commit is contained in:
Agent Workbench Dev
2026-11-04 10:36:50 +08:00
parent ce3afb2f0d
commit 785dd0c54d
70 changed files with 6103 additions and 7 deletions
+53
View File
@@ -0,0 +1,53 @@
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
const [directory, ...options] = process.argv.slice(2);
if (!directory || options.some((value, index) => value !== '--index' && value !== '--source-ref' && options[index - 1] !== '--source-ref'))
throw new Error('Usage: node scripts/verify-adoption-evidence.mjs <sealed-directory> [--index] [--source-ref <commit|:>]');
const root = path.resolve(directory), gitRoot = process.cwd(), useIndex = options.includes('--index');
const sourceRef = options.includes('--source-ref') ? options[options.indexOf('--source-ref') + 1] : null;
if (sourceRef) assert.ok(sourceRef === ':' || /^[a-f0-9]{7,40}$/.test(sourceRef));
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
const git = object => execFileSync('git', ['show', object], { cwd: gitRoot, windowsHide: true, maxBuffer: 16 * 1024 * 1024 });
const contained = rel => {
assert.ok(typeof rel === 'string' && rel && !rel.includes('\\') && !path.isAbsolute(rel) && !rel.split('/').some(part => !part || part === '..'));
const file = path.resolve(root, rel); assert.ok(file.startsWith(root + path.sep)); assert.ok(fs.lstatSync(file).isFile()); return file;
};
const manifestBytes = fs.readFileSync(path.join(root, 'manifest.json')), manifest = JSON.parse(manifestBytes);
assert.equal(manifest.kind, 'adoption-stage-evidence'); assert.equal(manifest.realModelRequests, 0); assert.equal(manifest.officialServiceChanged, false);
const names = new Set(); let bytes = 0;
for (const row of manifest.files) {
assert.ok(!names.has(row.path)); names.add(row.path); const data = fs.readFileSync(contained(row.path));
assert.equal(data.length, row.bytes, row.path); assert.equal(sha(data), row.sha256, row.path); bytes += data.length;
if (row.path.endsWith('.png')) assert.equal(data.subarray(0, 8).toString('hex'), '89504e470d0a1a0a');
if (useIndex) assert.equal(sha(git(':' + path.relative(gitRoot, path.join(root, row.path)).replaceAll('\\', '/'))), row.sha256);
}
assert.equal(bytes, manifest.bytes);
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(row => {
const file = path.join(dir, row.name); assert.equal(fs.lstatSync(file).isSymbolicLink(), false);
return row.isDirectory() ? walk(file) : [path.relative(root, file).replaceAll('\\', '/')];
});
assert.deepEqual(walk(root).filter(name => name !== 'manifest.json').sort(), [...names].sort());
if (useIndex) assert.equal(sha(git(':' + path.relative(gitRoot, path.join(root, 'manifest.json')).replaceAll('\\', '/'))), sha(manifestBytes));
for (const check of manifest.testRuns ?? []) {
const log = fs.readFileSync(contained(check.path), 'utf8');
for (const [label, count] of Object.entries({ tests: check.tests, pass: check.passed, fail: check.failed }))
assert.match(log, new RegExp('(?:ℹ |# )' + label + ' ' + count + '(?:\\r?\\n|$)'));
}
for (const check of manifest.uiRuns ?? []) {
const result = JSON.parse(fs.readFileSync(contained(check.path), 'utf8'));
assert.equal(result.success, check.success); assert.equal(result.passed.length, check.passed); assert.equal(result.realModelRequests, 0);
}
if (sourceRef) for (const row of manifest.sourceFiles) {
assert.ok(names.has(row.snapshot)); assert.ok(row.path && !row.path.includes('..') && !path.isAbsolute(row.path));
const original = fs.readFileSync(contained(row.snapshot)); const recorded = git(sourceRef === ':' ? ':' + row.path : sourceRef + ':' + row.path);
// Source snapshots preserve original checkout bytes. The repository's text
// attributes store LF; accept only that declared text representation change.
const gitText = Buffer.from(original.toString('utf8').replaceAll('\r\n', '\n'));
assert.equal(sha(recorded), sha(original) === sha(recorded) ? sha(original) : sha(gitText), 'Source ref differs: ' + row.path);
}
console.log(JSON.stringify({ ok: true, stage: manifest.stage, files: names.size, bytes, testRuns: manifest.testRuns?.length ?? 0,
uiRuns: manifest.uiRuns?.length ?? 0, sourceFiles: manifest.sourceFiles.length, indexChecked: useIndex, sourceRef, realModelRequests: 0 }));
+54
View File
@@ -0,0 +1,54 @@
import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { _electron } from 'playwright';
import { fixture, until } from '../tests/helpers.mjs';
import { beginPlanTask, completeRead } from '../tests/fixtures/task-plan-fixture.mjs';
import { HostController } from '../src/core/desktop/host-controller.mjs';
import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs';
import { projectEnvironment } from '../src/core/process-environment.mjs';
const root = path.resolve('.qa/requirements-progress', 'electron-' + randomUUID()); fs.mkdirSync(root, { recursive: true });
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t);
beginPlanTask(f, '解释当前工程依据,保留用户原文和原读取。'); completeRead(f); f.session.title = 'Electron要求证据原任务'; f.store.save(f.session); f.store.close();
const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve));
assert.notEqual(port, 4318);
const controller = new HostController({ appRoot: process.cwd(), dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 });
const passed = [], errors = [], screenshots = []; let application, result;
const mark = name => { passed.push(name); console.log('PASS ' + name); };
try {
await controller.start(); assert.equal((await controller.api('/bootstrap')).config.hasKey, false);
const executable = desktopExecutableIdentity(process.cwd()).path;
application = await _electron.launch({ executablePath: executable,
args: [...(executable.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.resolve('dist-desktop/main.cjs')]), '--app-root', process.cwd(), '--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window'],
cwd: process.cwd(), env: projectEnvironment(), timeout: 60000 });
const page = await application.firstWindow(); page.on('pageerror', error => errors.push(error.message));
const window = await application.browserWindow(page); await window.evaluate(win => win.setSize(1440, 1080));
await page.locator('.connection.connected').waitFor(); assert.equal(await page.evaluate(() => typeof window.process), 'undefined');
await page.getByLabel('当前项目').selectOption(f.project.id); await page.locator('.session-list button').filter({ hasText: f.session.title }).click();
await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
const panel = page.getByLabel('要求与证据', { exact: true }); await until(() => panel.getByRole('button', { name: '记录要求', exact: true }).isEnabled());
await panel.getByRole('button', { name: '记录要求', exact: true }).click();
const editor = page.getByRole('dialog', { name: '记录用户要求', exact: true });
await editor.getByLabel('要求名称', { exact: true }).fill('原生窗口核对工程说明'); await editor.getByLabel('要求验收条件', { exact: true }).fill('实际读取当前文件,保留原来源');
await editor.getByRole('button', { name: '保存要求', exact: true }).click(); await editor.waitFor({ state: 'hidden' }); mark('sandboxed actual Electron saves a sourced requirement');
const row = panel.locator('.requirement-list > li').first(); await row.getByRole('combobox').selectOption({ index: 1 });
await row.getByRole('button', { name: '复核所选证据', exact: true }).click(); await until(async () => (await row.locator('.requirement-state').textContent()) === '所选证据已核对');
mark('actual Electron verifies the original tool result');
await page.reload(); await page.locator('.connection.connected').waitFor();
if (!await panel.isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await until(async () => (await row.locator('.requirement-state').textContent()) === '所选证据已核对');
await window.evaluate(win => { win.showInactive(); win.setContentSize(1440, 1080); });
await until(() => page.evaluate(() => innerWidth === 1440));
await panel.scrollIntoViewIfNeeded(); const screenshot = path.join(root, 'verified.png'); await page.screenshot({ path: screenshot }); screenshots.push(screenshot);
mark('renderer reload restores the same Task and requirement proof'); assert.deepEqual(errors, []);
result = { success: true, passed, errors, screenshots, actualElectron: true, installationValidated: false, realModelRequests: 0, officialServiceTouched: false };
} catch (error) { result = { success: false, passed, errors, screenshots, error: error.stack, realModelRequests: 0 }; }
finally {
if (application) await application.close(); if (await controller.health()) await controller.stop();
for (const dispose of disposers.reverse()) await dispose();
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2));
}
console.log(JSON.stringify({ root, ...result })); if (!result.success) process.exitCode = 1;
+84
View File
@@ -0,0 +1,84 @@
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { chromium } from 'playwright';
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
import { beginPlanTask, completeRead } from '../tests/fixtures/task-plan-fixture.mjs';
const directory = path.resolve('.qa/requirements-progress', 'ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t);
beginPlanTask(f, '研究现有README.md,说明原工程依据,不修改参考文件。'); const read = completeRead(f);
f.session.title = '要求复核原工程'; f.store.save(f.session);
const other = f.store.createSession(f.project.id), originalSession = f.session; f.session = other; beginPlanTask(f, '另一个独立研究目标');
other.title = '另一任务的独立目标'; f.store.save(other); f.session = originalSession; f.store.close();
const host = await launchWorkbench(t, f.data); assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false);
const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1080 } });
const passed = [], screenshots = [], errors = [];
page.on('pageerror', error => errors.push(error.message));
const panel = () => page.getByLabel('要求与证据', { exact: true }), row = () => panel().locator('.requirement-list > li').first();
const mark = name => { passed.push(name); console.log('PASS ' + name); };
const shot = async name => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); };
async function select(session) {
if (!await page.getByLabel('当前项目').isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
await page.getByLabel('当前项目').selectOption(f.project.id);
await page.locator('.session-list button').filter({ hasText: session.title }).click();
if (!await panel().isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await panel().getByRole('button', { name: '记录要求', exact: true }).waitFor();
await until(async () => await panel().getByRole('button', { name: '记录要求', exact: true }).isEnabled());
}
async function verify() {
await row().getByRole('combobox').selectOption(read.id);
await row().getByRole('button', { name: '复核所选证据', exact: true }).click();
await until(async () => (await row().locator('.requirement-state').textContent()) === '所选证据已核对');
}
let result;
try {
await page.goto(host.url); await select(f.session);
await panel().getByRole('button', { name: '记录要求', exact: true }).click();
const editor = page.getByRole('dialog', { name: '记录用户要求', exact: true });
await editor.getByLabel('要求名称', { exact: true }).fill('解释当前工程的实际依据');
await editor.getByLabel('要求验收条件', { exact: true }).fill('保留README原读取和可核对结论,参考文件保持原字节');
await editor.getByRole('button', { name: '保存要求', exact: true }).click(); await editor.waitFor({ state: 'hidden' });
assert.equal(await row().locator('.requirement-state').textContent(), '待复核'); mark('manual source selection creates pending requirement, never automatic acceptance');
await verify(); mark('actual original read proof verified through UI');
await panel().scrollIntoViewIfNeeded(); await shot('desktop-verified');
await page.reload(); await select(f.session); await until(async () => (await row().locator('.requirement-state').textContent()) === '所选证据已核对');
mark('saved requirement and proof survive page refresh');
await row().getByRole('button', { name: '修订此要求', exact: true }).click();
await editor.getByLabel('要求验收条件', { exact: true }).fill('追加核对当前说明,旧复核记录保留');
await editor.getByRole('button', { name: '保存要求', exact: true }).click(); await editor.waitFor({ state: 'hidden' });
await until(async () => (await row().locator('.requirement-state').textContent()) === '待复核');
assert.match(await panel().textContent(), /原复核历史.*1 条/s); mark('requirement revision invalidates its proof and preserves original history');
await verify();
const file = path.join(f.projectRoot, 'README.md'), original = fs.readFileSync(file); fs.appendFileSync(file, '\nChanged externally in owned fixture\n');
await panel().getByRole('button', { name: '刷新复核', exact: true }).click();
await until(async () => (await row().locator('.requirement-state').textContent()) === '依据已失效');
await row().getByRole('button', { name: '复核所选证据', exact: true }).click();
await until(async () => (await panel().textContent()).includes('原读取对应旧文件SHA'));
await panel().scrollIntoViewIfNeeded(); await shot('desktop-stale'); mark('source mutation and stale original read are visible failures with preserved history');
fs.writeFileSync(file, original);
await page.setViewportSize({ width: 390, height: 844 });
if (!await panel().isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await panel().scrollIntoViewIfNeeded(); await shot('narrow-stale');
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true); mark('390px viewport keeps requirement controls usable without page overflow');
await page.setViewportSize({ width: 1440, height: 1080 });
let release = null, held = false;
await page.route('**/requirements?taskId=' + f.session.taskId, async route => {
held = true; await new Promise(resolve => { release = resolve; });
try { await route.continue(); } catch (error) { if (!/Route is already handled|Invalid InterceptionId/.test(error.message)) errors.push(error.message); }
});
await panel().getByRole('button', { name: '刷新复核', exact: true }).click(); await until(() => held);
await select(other); release(); await page.unroute('**/requirements?taskId=' + f.session.taskId);
await until(async () => (await panel().textContent()).includes('尚未逐项记录要求'));
assert.ok(!(await panel().textContent()).includes('解释当前工程的实际依据')); mark('late response for original Task cannot replace the newly selected task');
assert.deepEqual(errors, []);
const requirements = await host.api('/sessions/' + f.session.id + '/requirements?taskId=' + f.session.taskId);
result = { success: true, passed, screenshots, errors, requirements, realModelRequests: 0, officialServiceTouched: false, companionWritePermissionsAdded: false };
} catch (error) {
await shot('failure').catch(() => {}); result = { success: false, passed, screenshots, errors, error: error.stack, realModelRequests: 0 };
} finally {
await browser.close(); for (const dispose of disposers.reverse()) await dispose();
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2));
}
console.log(JSON.stringify({ directory, ...result, requirements: undefined })); if (!result.success) process.exitCode = 1;