feat(companion): add task PWA and original execution controls

This commit is contained in:
Agent Workbench Dev
2026-10-31 17:53:15 +08:00
parent dfe2e40387
commit bc67e6e0a3
57 changed files with 1212 additions and 15 deletions
+42
View File
@@ -0,0 +1,42 @@
import fs from 'node:fs';
import path from 'node:path';
import { createHash } from 'node:crypto';
// Vite's dependency graph defines the complete mobile allowlist. Desktop
// entries, arbitrary files and future chunks are not implicitly exposed.
const root = path.resolve('dist-client'), manifest = JSON.parse(fs.readFileSync(path.join(root, '.vite/manifest.json'), 'utf8'));
const seen = new Set(), assets = new Set();
function visit(name) {
if (seen.has(name)) return; seen.add(name);
const entry = manifest[name]; if (!entry) throw new Error('Mobile build dependency is missing: ' + name);
for (const file of [entry.file, ...(entry.css ?? []), ...(entry.assets ?? [])]) {
if (!/^assets\/[A-Za-z0-9_.-]+\.(js|css)$/.test(file)) throw new Error('Unexpected mobile asset: ' + file);
if (!fs.statSync(path.join(root, file)).isFile()) throw new Error('Mobile asset is not a file: ' + file);
assets.add('/' + file);
}
for (const imported of [...(entry.imports ?? []), ...(entry.dynamicImports ?? [])]) visit(imported);
}
visit('mobile.html');
const list = [...assets].sort();
fs.writeFileSync(path.join(root, 'mobile-assets.json'), JSON.stringify(list, null, 2) + '\n');
// Reuse our existing raster mark for iPhone's home-screen icon; no generated
// media or network dependency is needed for this product asset.
const icon = fs.readFileSync(path.join(root, 'workbench-icon.png'));
fs.writeFileSync(path.join(root, 'mobile-touch-icon.png'), icon);
const version = createHash('sha256').update(fs.readFileSync(path.join(root, 'mobile.html'))).update(JSON.stringify(list)).digest('hex').slice(0, 20);
const files = ['/', '/mobile.webmanifest', '/mobile-icon.svg', '/mobile-touch-icon.png', ...list];
const worker = `const CACHE = 'workbench-mobile-${version}';
const FILES = ${JSON.stringify(files)};
self.addEventListener('install', event => event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(FILES)).then(() => self.skipWaiting())));
self.addEventListener('activate', event => event.waitUntil(caches.keys().then(names => Promise.all(names.filter(name => name.startsWith('workbench-mobile-') && name !== CACHE).map(name => caches.delete(name)))).then(() => self.clients.claim())));
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
if (event.request.method !== 'GET' || url.origin !== self.location.origin || url.search || !FILES.includes(url.pathname)) return;
event.respondWith(fetch(event.request).then(response => {
if (response.ok) { const copy = response.clone(); event.waitUntil(caches.open(CACHE).then(cache => cache.put(event.request, copy))); }
return response;
}).catch(async () => (await caches.match(event.request)) || Response.error()));
});
`;
fs.writeFileSync(path.join(root, 'mobile-sw.js'), worker);
console.log('Mobile shell built: ' + list.length + ' allowed assets; API and project data are never cached.');
+128
View File
@@ -0,0 +1,128 @@
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import https from 'node:https';
import { X509Certificate, randomUUID } from 'node:crypto';
import assert from 'node:assert/strict';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-companion-ui.mjs - isolated desktop and mobile UI via a loopback QA relay that verifies the actual gateway CA; does not modify certificate trust or substitute for iPhone Safari acceptance.'); process.exit(0); }
if (process.argv.length > 2) throw new Error('Unsupported arguments.');
const directory = path.resolve('.qa', 'companion-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: action => disposers.push(action) }, f = fixture(t);
fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ name: 'owned-companion-ui', private: true, type: 'module', scripts: { test: 'node --test fixture.test.mjs' } }));
fs.writeFileSync(path.join(f.projectRoot, 'fixture.test.mjs'), "import test from 'node:test';import assert from 'node:assert/strict';test('owned mobile fixture',()=>assert.equal(1+1,2));\n");
f.store.close();
const host = await launchWorkbench(t, f.data, { Path: path.dirname(process.execPath) + path.delimiter + process.env.Path });
await host.api('/sessions/' + f.session.id + '/messages', { content: '手机伴随端验收:读取项目、准备原差异并等待手机审批。明确为本地离线夹具。' });
await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'awaiting_approval');
const before = await host.api('/sessions/' + f.session.id), browser = await chromium.launch({ channel: 'msedge', headless: true });
const desktop = await browser.newPage({ viewport: { width: 1440, height: 1000 } }), errors = [], passed = [], screenshots = [];
desktop.on('pageerror', error => errors.push(error.message));
const mark = text => { passed.push(text); console.log('PASS ' + text); };
async function shot(page, name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: !(await page.locator(':modal').count()) }); screenshots.push(file); }
let mobile, relay, gatewayStatus, cookie = '', disconnected = false;
try {
await desktop.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id });
await desktop.goto(host.url); await desktop.getByRole('button', { name: '手机连接', exact: true }).click();
const panel = desktop.getByRole('dialog', { name: '手机连接', exact: true }); await expect(panel).toBeVisible();
await expect(panel).toContainText('默认关闭');
const initial = await host.api('/companion'); assert.ok(initial.addresses.length, 'Requires an actual owned private IPv4 interface.');
const reservation = http.createServer(); await new Promise(resolve => reservation.listen(0, initial.addresses[0].address, resolve));
const port = reservation.address().port; await new Promise(resolve => reservation.close(resolve));
await panel.getByLabel('手机连接端口', { exact: true }).fill(String(port));
await panel.getByRole('button', { name: '开启 HTTPS', exact: true }).click();
await expect(panel).toContainText('手机连接已开启', { timeout: 20000 });
await expect(panel).toContainText('证书信任设置'); await shot(desktop, 'desktop-certificate');
const downloadEvent = desktop.waitForEvent('download'); await panel.getByRole('button', { name: '下载手机根证书', exact: true }).click();
const download = await downloadEvent, certFile = path.join(directory, 'fixture-mobile-ca.cer'); await download.saveAs(certFile);
const ca = new X509Certificate(fs.readFileSync(certFile)).toString();
assert.equal(new X509Certificate(ca).ca, true); mark('desktop explicitly enables HTTPS and downloads the actual public CA without changing OS trust');
await panel.getByLabel('允许审批命令', { exact: true }).check(); await panel.getByLabel('允许停止任务', { exact: true }).check();
await panel.getByRole('button', { name: '生成一次性配对码', exact: true }).click(); await expect(panel.getByAltText('手机一次性配对二维码')).toBeVisible();
const pairUrl = await panel.locator('.companion-pair-url').textContent(); await shot(desktop, 'desktop-pairing');
gatewayStatus = await host.api('/companion');
// Browser rendering uses an owned loopback relay because this test must not
// silently install a root or bypass a TLS warning. The relay verifies the
// real gateway certificate for every request and holds only its fixture
// Secure cookie in memory. Native Safari cookie/trust/PWA remains device QA.
const target = new URL(gatewayStatus.origin);
relay = http.createServer((request, response) => {
if (disconnected && request.url.startsWith('/mobile-api/')) { response.writeHead(503, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: '自有断线夹具' })); return; }
const headers = { Host: target.host, Origin: target.origin, Connection: 'close', ...(cookie ? { Cookie: cookie } : {}),
...(request.headers['content-type'] ? { 'Content-Type': request.headers['content-type'] } : {}) };
const forwarded = https.request(new URL(request.url, target), { method: request.method, headers, ca, servername: '' }, incoming => {
const set = incoming.headers['set-cookie']; if (set?.length) cookie = set[0].split(';')[0];
const outgoing = { ...incoming.headers }; delete outgoing['set-cookie']; response.writeHead(incoming.statusCode, outgoing); incoming.pipe(response);
});
forwarded.on('error', () => { if (!response.headersSent) { response.writeHead(503, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: '自有TLS转发中断' })); } else response.destroy(); });
request.pipe(forwarded);
});
await new Promise(resolve => relay.listen(0, '127.0.0.1', resolve));
const relayUrl = 'http://127.0.0.1:' + relay.address().port;
const mobileContext = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true });
mobile = await mobileContext.newPage(); mobile.on('pageerror', error => errors.push(error.message));
await mobile.goto(relayUrl + '/#pair=' + new URLSearchParams(new URL(pairUrl).hash.slice(1)).get('pair'));
await expect(mobile.getByRole('heading', { name: '接续电脑上的任务', exact: true })).toBeVisible();
assert.equal(new URL(mobile.url()).hash, '');
await mobile.getByLabel('设备名称', { exact: true }).fill('验收 iPhone 观察窗口');
await mobile.getByRole('button', { name: '连接这台电脑', exact: true }).click();
await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); await shot(mobile, 'mobile-task-list');
await expect(panel).toContainText('验收 iPhone 观察窗口', { timeout: 10000 });
mark('390px real gateway pairing clears fragment, displays scoped task list and registers the same device in desktop');
await mobile.locator('.mobile-task-row').filter({ hasText: before.title }).click();
await expect(mobile.getByRole('heading', { name: '等待你的确认', exact: true })).toBeVisible();
await mobile.getByText('原任务与运行', { exact: true }).click(); await expect(mobile.locator('.mobile-task-title code')).toContainText(before.taskId);
assert.ok(await mobile.locator('body').evaluate(body => body.scrollWidth <= innerWidth)); await shot(mobile, 'mobile-original-approval');
await mobile.getByRole('button', { name: '核对并批准', exact: true }).click();
const confirmation = mobile.getByRole('dialog'); await expect(confirmation).toContainText(before.runId);
await confirmation.getByRole('button', { name: '确认', exact: true }).click();
await until(async () => ['needs_review', 'completed'].includes((await host.api('/sessions/' + f.session.id)).status));
await expect(mobile.locator('.mobile-receipt')).toContainText('已批准原请求');
const after = await host.api('/sessions/' + f.session.id); assert.equal(after.taskId, before.taskId); assert.equal(after.runId, before.runId);
assert.equal(after.events.filter(item => item.tool === 'run_command').length, 1); mark('phone UI confirms original Task/Run approval and the existing Runtime executes one command');
await mobile.getByRole('button', { name: /^文件差异/ }).click();
await mobile.getByRole('button', { name: /WORKBENCH-NOTES.md/ }).click(); await expect(mobile.getByRole('dialog')).toContainText('SHA-256 已核对');
await shot(mobile, 'mobile-original-diff'); await mobile.getByRole('button', { name: '返回任务', exact: true }).click();
await host.api('/sessions/' + f.session.id + '/changes/' + after.changes.at(-1).id + '/apply', {});
assert.equal(fs.existsSync(path.join(f.projectRoot, 'WORKBENCH-NOTES.md')), true);
await host.api('/sessions/' + f.session.id + '/check-plans', { names: ['test'] });
const planned = await host.api('/sessions/' + f.session.id); await host.api('/sessions/' + f.session.id + '/check-jobs', { planId: planned.execution.plans.at(-1).id });
await until(async () => ['passed', 'failed', 'unverified'].includes((await host.api('/sessions/' + f.session.id)).execution.checks.at(-1)?.status), 30000);
const checked = await host.api('/sessions/' + f.session.id); assert.equal(checked.execution.checks.at(-1).status, 'passed');
await host.api('/sessions/' + f.session.id + '/delivery-reports', {});
await mobile.getByRole('button', { name: '检查与报告', exact: true }).click();
await expect(mobile.locator('.mobile-checks')).toContainText('通过', { timeout: 12000 });
await mobile.getByRole('button', { name: /^交付报告 [\d,]+ bytes/ }).click(); await expect(mobile.getByRole('dialog')).toContainText('SHA-256 已核对');
await shot(mobile, 'mobile-original-report'); await mobile.getByRole('button', { name: '返回任务', exact: true }).click();
mark('original paginated diff and counted check/report display verified content from the same Task');
disconnected = true; await mobile.getByRole('button', { name: '刷新任务', exact: true }).click();
await expect(mobile.locator('.mobile-connection')).toContainText('连接中断'); await shot(mobile, 'mobile-disconnected');
disconnected = false; await mobile.getByRole('button', { name: '重新同步', exact: true }).click(); await expect(mobile.locator('.mobile-connection')).toContainText('已同步');
assert.equal((await host.api('/sessions/' + f.session.id)).events.filter(item => item.tool === 'run_command').length, 1);
await mobile.reload(); await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible();
mark('disconnect and refresh recover from persistent seq without submitting a new command or Run');
const cacheEntries = await mobile.evaluate(async () => { const names = await caches.keys(); return (await Promise.all(names.map(async name => (await (await caches.open(name)).keys()).map(request => new URL(request.url).pathname)))).flat(); });
assert.ok(cacheEntries.length > 0 && cacheEntries.every(route => !route.startsWith('/mobile-api/'))); mark('PWA service worker stores the shell and no API/project response');
await mobile.setViewportSize({ width: 768, height: 1024 }); await shot(mobile, 'tablet-task-list');
await desktop.setViewportSize({ width: 390, height: 844 }); await panel.evaluate(node => { node.scrollTop = 0; }); await shot(desktop, 'desktop-management-narrow');
assert.ok(await desktop.locator('body').evaluate(body => body.scrollWidth <= innerWidth));
await panel.getByRole('button', { name: '撤销', exact: true }).click();
await mobile.getByRole('button', { name: '刷新任务', exact: true }).click(); await expect(mobile.getByRole('heading', { name: '接续电脑上的任务', exact: true })).toBeVisible();
await expect(mobile.locator('.mobile-task-row')).toHaveCount(0); await shot(mobile, 'mobile-revoked');
assert.equal((await host.api('/sessions/' + f.session.id)).events.filter(item => item.tool === 'run_command').length, 1);
mark('desktop revocation removes mobile project content immediately on the next read while preserving the original completed tool');
assert.deepEqual(errors, []);
const result = { success: true, passed, screenshots, errors, viewports: [1440, 390, 768], browser: 'Microsoft Edge via Playwright; owned loopback QA relay with actual CA verification',
nativeSafariTrustCookiePWA: 'pending-real-iPhone', osTrustChanged: false, certificateWarningsBypassed: false, realModelRequests: 0, taskId: before.taskId, runId: before.runId };
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2));
} catch (error) {
if (mobile) await shot(mobile, 'failure-mobile').catch(() => {}); await shot(desktop, 'failure-desktop').catch(() => {});
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: false, error: error.message, stack: error.stack, passed, screenshots, errors, realModelRequests: 0 }, null, 2)); throw error;
} finally {
console.log('EVIDENCE ' + directory); await browser.close();
if (relay) await new Promise(resolve => { relay.closeAllConnections(); relay.close(resolve); });
await host.api('/companion/close', {}).catch(() => {});
for (const dispose of disposers.reverse()) await dispose();
}