import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { createHash, randomUUID } from 'node:crypto'; import { chromium, expect } from 'playwright/test'; import { fixture, until } from '../tests/helpers.mjs'; import { workerHttp } from '../tests/fixtures/workers-http.mjs'; import { workerDefinition, ownerBytes, applyWorkerProposals, approveWorkerChecks } from '../tests/fixtures/workers-project.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-workers-ui.mjs — owned local fixture, real isolated Git/checks/review, desktop/390px, conflicts, stop, recovery and late source errors'); process.exit(0); } const version = JSON.parse(fs.readFileSync('package.json', 'utf8')).version, prefix = version === '1.20.1' ? 'task-control-workers' : version === '1.20.0' ? 'engineering-evals-workers' : version === '1.19.0' ? 'project-cache-workers' : version === '1.18.0' ? 'vision-inputs-workers' : version === '1.17.0' ? 'model-protocols-workers' : version === '1.16.0' ? 'mobile-companion-workers' : version === '1.15.0' ? 'desktop-host-workers' : version === '1.14.0' ? 'controlled-engine-workers' : version === '1.13.0' ? 'workers' : 'workers-development'; const directory = path.resolve('.qa', 'workers-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const disposers = [], t = { after: dispose => disposers.push(dispose) }, f = fixture(t), host = await workerHttp(f); assert.notEqual(new URL(host.url).port, '4318'); const hash = bytes => createHash('sha256').update(bytes).digest('hex'); function inputs() { const files = ['package.json', 'package-lock.json']; const walk = root => { for (const entry of fs.readdirSync(root, { withFileTypes: true })) { const file = path.join(root, entry.name); if (entry.isDirectory()) walk(file); else if (entry.isFile()) files.push(file); } }; for (const root of ['src', 'scripts', 'dist-client', 'dist-server']) walk(root); return files.sort().map(file => ({ path: file, sha256: hash(fs.readFileSync(file)) })); } const frozen = inputs(), pageErrors = [], requestFailures = [], assertions = [], screenshots = [], exports = [], groups = []; const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.on('pageerror', error => pageErrors.push(error.message)); page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText)); const panel = () => page.getByLabel('隔离协作控制台', { exact: true }), groupState = id => f.store.core.collaboration.get(id); const mark = text => { assertions.push(text); console.log('PASS ' + text); }; async function screenshot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push({ file, sha256: hash(fs.readFileSync(file)) }); } async function openWorkers() { await page.getByRole('button', { name: '隔离协作', exact: true }).click(); await expect(panel()).toBeVisible(); await expect(panel().getByRole('button', { name: '刷新', exact: true })).toBeEnabled(); } async function parentFromChild() { await openWorkers(); await panel().getByRole('button', { name: '返回父目标', exact: true }).click(); await expect.poll(() => page.evaluate(() => localStorage.getItem('awb-session'))).toBe(f.session.id); await openWorkers(); } async function confirmStart() { await panel().getByRole('button', { name: /开始两个子任务|继续未完成子任务/ }).click(); await page.getByRole('dialog', { name: '确认受限子执行', exact: true }).getByRole('button', { name: '确认开始', exact: true }).click(); } async function applyUi(count, prepared = false) { if (await page.getByRole('button', { name: '查看改动', exact: true }).isVisible()) await page.getByRole('button', { name: '查看改动', exact: true }).click(); else await page.locator('.inspector-tabs').getByRole('button', { name: /^改动/ }).click(); await page.getByRole('button', { name: prepared ? '审查变更集' : '组合审查', exact: true }).click(); if (!prepared) await page.getByRole('dialog', { name: '选择要一起审查的提案', exact: true }).getByRole('button', { name: '封存审查范围', exact: true }).click(); const review = page.getByRole('dialog', { name: '多文件变更集审查', exact: true }); await review.getByRole('button', { name: '确认应用全部 ' + count + ' 项', exact: true }).click(); await expect(review).toContainText('全部应用完成', { timeout: 10000 }); await page.keyboard.press('Escape'); } async function runFixture(definition) { const group = await host.manager.plan(f.session, definition); groups.push(group.id); await (await host.manager.prepare(f.session, group.id, group.fingerprint)).promise; await (await host.manager.start(f.session, group.id, group.fingerprint)).promise; await applyWorkerProposals(f, host.runtime, group); await approveWorkerChecks(f, host.runtime, group, host.manager.start(f.session, group.id, group.fingerprint)); assert.equal(group.status, 'ready_to_collect', group.summary); await (await host.manager.collect(f.session, group.id, group.fingerprint)).promise; return group; } let result, releaseHeld; try { await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id }); await page.goto(host.url); await page.waitForLoadState('networkidle'); await openWorkers(); await expect(panel().getByRole('button', { name: '封存分工计划', exact: true })).toBeDisabled(); await panel().getByLabel('父目标', { exact: true }).fill('两个独立模块经审查、父工程检查和本地 Git 后交付'); for (const [index, relative] of ['src/a.mjs', 'src/b.mjs'].entries()) { const lane = panel().locator('fieldset').nth(index); await lane.getByLabel('任务名称', { exact: true }).fill('worker-' + (index + 1)); await lane.getByLabel('独立子目标', { exact: true }).fill('实现并验证独立模块 ' + (index + 1)); await lane.getByLabel('允许修改的文件或目录', { exact: true }).fill(relative); } await page.setViewportSize({ width: 390, height: 844 }); await screenshot('mobile-worker-plan'); assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390)); await page.setViewportSize({ width: 1440, height: 1000 }); const initial = ownerBytes(f); await panel().getByRole('button', { name: '封存分工计划', exact: true }).click(); await expect(panel()).toContainText('计划已封存'); const group = [...f.store.core.collaboration.values()].at(-1); groups.push(group.id); assert.equal(host.requests.length, 0); assert.deepEqual(ownerBytes(f), initial); mark('desktop/390px editor seals the actual parent goal, two scopes and shared budget without model calls or source writes'); await panel().getByRole('button', { name: '创建两个工作区', exact: true }).click(); await page.keyboard.press('Escape'); assert.equal(group.status, 'planned'); await panel().getByRole('button', { name: '创建两个工作区', exact: true }).click(); await page.getByRole('dialog', { name: '确认创建隔离工作区', exact: true }).getByRole('button', { name: '确认创建', exact: true }).click(); await expect.poll(() => group.status, { timeout: 20000 }).toBe('ready'); await expect(panel().locator('.worker-lane')).toHaveCount(2); assert.deepEqual(ownerBytes(f), initial); mark('explicit workspace confirmation creates two distinct Git checkouts and keeps every original source/staging byte'); await confirmStart(); await expect.poll(() => group.status, { timeout: 15000 }).toBe('awaiting_review'); assert.equal(f.session.changes.length, 0); for (let index = 0; index < 2; index++) { await panel().locator('.worker-lane').nth(index).getByRole('button', { name: '审查子任务改动', exact: true }).click(); await expect.poll(() => page.evaluate(() => localStorage.getItem('awb-session'))).toBe(group.children[index].sessionId); for (const name of ['项目技能', '项目MCP工具', '本地代理引擎', 'REA 工件研究']) await expect(page.getByRole('button', { name, exact: true })).toBeDisabled(); await applyUi(1); assert.equal(f.store.session(group.children[index].sessionId).changes[0].status, 'applied'); await parentFromChild(); } assert.deepEqual(ownerBytes(f), initial); mark('each lane navigates to its original child task, hides unsupported actions and applies only an explicitly reviewed child batch'); await confirmStart(); await expect.poll(() => group.children.filter(child => f.store.session(child.sessionId).status === 'awaiting_approval').length, { timeout: 15000 }).toBe(2); for (let index = 0; index < 2; index++) { await panel().locator('.worker-lane').nth(index).getByRole('button', { name: '查看并审批检查', exact: true }).click(); await expect(page.locator('.approval-card')).toContainText('npm'); await page.locator('.approval-card').getByRole('button', { name: '确认执行', exact: true }).click(); await parentFromChild(); } await expect.poll(() => group.status, { timeout: 25000 }).toBe('ready_to_collect'); await panel().getByRole('button', { name: '核对成果', exact: true }).click(); await expect.poll(() => group.status, { timeout: 15000 }).toBe('collected'); await screenshot('desktop-workers-collected'); await panel().getByRole('button', { name: '查看原成果记录', exact: true }).click(); await expect(page.getByRole('dialog', { name: '原成果核对记录', exact: true })).toContainText('groupFingerprint'); await page.keyboard.press('Escape'); mark('both UI approvals run real three-test/build checks and collect the original hashed outputs before any parent write'); await panel().getByRole('button', { name: '准备合并审查', exact: true }).click(); await expect.poll(() => group.status, { timeout: 15000 }).toBe('merge_prepared'); await panel().getByRole('button', { name: '打开父工程变更集审查', exact: true }).click(); await applyUi(2, true); assert.deepEqual(ownerBytes(f), initial); const plan = await host.runtime.checkRunner().plan(f.session), check = await (await host.runtime.checkRunner().start(f.session, plan.id)).promise; assert.equal(check.status, 'passed', check.summary); const delivery = await host.runtime.gitDelivery().plan(f.session, { message: 'UI reviewed isolated workers' }); await (await host.runtime.gitDelivery().start(f.session, delivery.id)).promise; await openWorkers(); await panel().getByRole('button', { name: '核对检查与 Git 交付', exact: true }).click(); await expect.poll(() => group.status).toBe('completed'); assert.ok(group.delivery.commit); assert.deepEqual(ownerBytes(f).files, initial.files); mark('parent UI review applies a new two-file batch and accepts completion only after a fresh real parent check and local Git delivery'); const conflict = await runFixture({ ...workerDefinition(['src/shared.mjs', 'src/shared.mjs']), goal: '重叠修改必须保持具体冲突' }); assert.equal(conflict.status, 'conflict'); await panel().getByRole('button', { name: '刷新', exact: true }).click(); await panel().getByLabel('选择协作记录', { exact: true }).selectOption(conflict.id); await page.setViewportSize({ width: 390, height: 844 }); await expect(panel().locator('.worker-conflicts')).toContainText('src/shared.mjs'); assert.equal(fs.existsSync(path.join(f.projectRoot, 'src/shared.mjs')), false); assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390)); await panel().locator('.worker-review').scrollIntoViewIfNeeded(); await screenshot('mobile-workers-conflict'); await panel().getByRole('button', { name: '结束协作并保留现场', exact: true }).click(); await page.getByRole('dialog', { name: '确认保留现场', exact: true }).getByRole('button', { name: '确认保留现场结束', exact: true }).click(); await expect.poll(() => conflict.status).toBe('resolved_external'); mark('390px conflict review names the real overlapping path, leaves parent files untouched and explicitly retains the original child results'); const stopped = await host.manager.plan(f.session, { ...workerDefinition(['src/stop-a.mjs', 'src/stop-b.mjs']), goal: '停止和原输入核对保持任务归属' }); groups.push(stopped.id); await (await host.manager.prepare(f.session, stopped.id, stopped.fingerprint)).promise; const complete = host.provider.complete; let held = 0; host.provider.complete = async (config, messages, project, signal) => new Promise((resolve, reject) => { held++; signal.addEventListener('abort', () => reject(Object.assign(new Error('Owned held UI request stopped'), { name: 'AbortError' })), { once: true }); }); await panel().getByRole('button', { name: '刷新', exact: true }).click(); await panel().getByLabel('选择协作记录', { exact: true }).selectOption(stopped.id); await confirmStart(); await expect.poll(() => held).toBe(2); await panel().getByRole('button', { name: '停止全部', exact: true }).click(); await expect.poll(() => stopped.status, { timeout: 10000 }).toBe('paused'); host.provider.complete = complete; assert.equal(stopped.usage.unknownRequests, 2); assert.equal(host.runtime.hasActivity(), false); const deadline = stopped.deadline; mark('mobile parent stop waits for both held requests, preserves estimated unknown usage and retains the original deadline'); await panel().getByLabel('选择协作记录', { exact: true }).selectOption(group.id); let heldRoute; await page.route('**/api/sessions/' + f.session.id + '/collaborations/' + group.id + '/collection', async route => { heldRoute = route; await new Promise(resolve => { releaseHeld = resolve; }); await route.fulfill({ status: 500, contentType: 'application/json', body: '{"error":"Owned late previous-group error"}' }); }); await panel().getByRole('button', { name: '查看原成果记录', exact: true }).click(); await expect.poll(() => !!heldRoute).toBe(true); await panel().getByLabel('选择协作记录', { exact: true }).selectOption(stopped.id); releaseHeld(); releaseHeld = null; await page.unrouteAll({ behavior: 'wait' }); await expect(page.getByText('Owned late previous-group error', { exact: true })).toHaveCount(0); await expect(page.getByRole('dialog', { name: '原成果核对记录', exact: true })).toHaveCount(0); mark('switching historical groups quarantines a deliberately late previous-group error and its preview'); stopped.status = 'unknown'; stopped.requiresVerification = true; f.store.save(f.session); await panel().getByRole('button', { name: '刷新', exact: true }).click(); await panel().getByRole('button', { name: '核对原输入与现场', exact: true }).click(); await expect.poll(() => stopped.evidence?.sha256, { timeout: 10000 }).toBeTruthy(); await expect(panel().getByRole('button', { name: '确认核对并继续', exact: true })).toBeDisabled(); await panel().getByRole('button', { name: '查看核对记录', exact: true }).click(); await expect(page.getByRole('dialog', { name: '原输入与现场核对', exact: true })).toContainText('worker-recovery-observation'); await page.keyboard.press('Escape'); fs.appendFileSync(path.join(f.projectRoot, 'owner-unstaged.txt'), 'Owner changed after the observed UI evidence\n'); await panel().getByRole('button', { name: '确认核对并继续', exact: true }).click(); await page.getByRole('dialog', { name: '确认原结果核对', exact: true }).getByRole('button', { name: '确认核对并继续', exact: true }).click(); await expect(panel().getByRole('alert')).toContainText('现场在核对后发生变化'); assert.equal(stopped.requiresVerification, true); mark('recovery UI requires reading the original record and rejects changed source instead of acknowledging stale evidence'); const oldSha = stopped.evidence.sha256; await panel().getByRole('button', { name: '核对原输入与现场', exact: true }).click(); await expect.poll(() => stopped.evidence.sha256).not.toBe(oldSha); await panel().getByRole('button', { name: '查看核对记录', exact: true }).click(); await expect(page.getByRole('dialog', { name: '原输入与现场核对', exact: true })).toBeVisible(); await page.keyboard.press('Escape'); await panel().getByRole('button', { name: '确认核对并保留现场结束', exact: true }).click(); await page.getByRole('dialog', { name: '确认保留现场', exact: true }).getByRole('button', { name: '确认保留现场结束', exact: true }).click(); await expect.poll(() => stopped.status).toBe('resolved_external'); assert.equal(stopped.deadline, deadline); assert.match(fs.readFileSync(path.join(f.projectRoot, 'owner-unstaged.txt'), 'utf8'), /Owner changed/); await screenshot('mobile-workers-recovery-retained'); await panel().getByRole('button', { name: '创建下一父目标', exact: true }).click(); await expect(panel().getByLabel('父目标', { exact: true })).toBeEditable(); mark('explicit recovery retention preserves changed owner source and unknown usage, closes the original goal and unlocks the next plan'); for (const id of groups) { const record = groupState(id); for (const sessionId of [record.sessionId, ...record.children.map(child => child.sessionId)]) { const file = path.join(directory, 'session-' + sessionId + '-after-' + id + '.json'), value = await host.api('/sessions/' + sessionId + '/export'); fs.writeFileSync(file, JSON.stringify(value, null, 2)); exports.push({ file, sha256: hash(fs.readFileSync(file)) }); } } assert.deepEqual(inputs(), frozen); assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []); result = { passed: true, version, capabilityStage: '1.13 isolated workers', evidenceKind: 'owned fixture and real local tools/UI', realModelRequests: 0, assertions, screenshots, exports, groups, fixtureRequests: host.requests.length + held, inputs: frozen, pageErrors, requestFailures, url: host.url, sourceRoot: f.root, createdAt: new Date().toISOString() }; } catch (error) { result = { passed: false, version, error: error.stack, assertions, screenshots, pageErrors, requestFailures, sourceRoot: f.root }; await screenshot('workers-ui-failure').catch(() => {}); throw error; } finally { releaseHeld?.(); await browser.close(); await host.runtime.stopAll(); f.store.storage.database.exec('PRAGMA wal_checkpoint(TRUNCATE)'); const copy = path.join(directory, 'original-fixture'); fs.cpSync(f.root, copy, { recursive: true }); if (result) result.originalFixture = copy; fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); if (result?.passed) fs.writeFileSync(path.join('docs', prefix + '-ui-validation.json'), JSON.stringify(result, null, 2)); for (const dispose of disposers.reverse()) await dispose(); console.log('EVIDENCE ' + directory); }