import fs from 'node:fs'; import path from 'node:path'; import net from 'node:net'; import http from 'node:http'; import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import { chromium, _electron } from 'playwright'; import { expect } from 'playwright/test'; import { PNG } from 'pngjs'; import { fixture, compatibleFixture, completion, call, launchWorkbench, until } from '../tests/helpers.mjs'; import { Provider } from '../src/server/provider.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'; if (process.argv.includes('--help')) { console.log('node scripts/verify-unrestricted-ui.mjs [--desktop] — isolated actual passthrough, permissions, automatic writes/rollback, upstream errors and cache UI. Optional AGENT_WORKBENCH_QA_APP_ROOT selects the immutable app. No real-model keys.'); process.exit(0); } assert.ok(process.argv.slice(2).every(value => value === '--desktop')); const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.'); const directory = path.resolve('.qa/unrestricted-1.25.5', (desktop ? 'open-electron-' : 'open-web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true }); console.log('EVIDENCE ' + directory); const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true }), checks = [], screenshots = [], errors = []; let host, controller, browser, application, page, window, result, ownedFailure = false, browserUrl, browserTurn = 0, ownedTabId; const dialog = () => page.getByRole('dialog', { name: '模型中心', exact: true }); const mark = name => { checks.push(name); console.log('PASS ' + name); }; async function open() { if (!await dialog().count()) await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); } async function resize(width) { if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height: 1080 }); await until(() => page.evaluate(w => innerWidth === w, width)); } else await page.setViewportSize({ width, height: 1080 }); const compactControl = page.locator('.task-toolbar').getByRole('button', { name: '查看文件', exact: true }); if (width <= 940) await expect(compactControl).toBeVisible(); else await expect(compactControl).toHaveCount(0); } async function shot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file); fs.writeFileSync(path.join(directory, name + '.txt'), await page.locator('body').innerText()); } try { if (desktop) { const site = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); res.end(req.url === '/next' ? '
' + 'FULL-BROWSER-TEXT '.repeat(1500) + 'LONG-END
'); }); await new Promise(resolve => site.listen(0, '127.0.0.1', resolve)); browserUrl = 'http://127.0.0.1:' + site.address().port + '/'; disposers.push(() => new Promise(resolve => { site.closeAllConnections(); site.close(resolve); })); } const browserReply = body => { const turn = ++browserTurn, original = body.messages.findLast(row => row.role === 'tool'); const observed = original ? JSON.parse(original.content) : null; if (turn === 1) return completion([call('browser-open', 'open_browser_tab', { url: browserUrl })]); if (turn === 2) { ownedTabId = observed.id; return completion([call('browser-observe', 'observe_browser_tab', { tab_id: ownedTabId })]); } if (turn <= 5) { const label = turn === 3 ? '账户口令' : turn === 4 ? '运行方式' : '打开第二来源'; assert.ok(Array.isArray(observed?.controls), 'Original browser result at turn ' + turn + ': ' + JSON.stringify(observed)); const control = observed.controls.find(value => value.label.startsWith(label)); assert.ok(control, label); if (turn === 3) { assert.ok(observed.text.length > 16000); assert.ok(observed.controls.length > 120); assert.ok(observed.text.includes('LONG-END')); } return completion([call('browser-act-' + turn, 'act_browser_tab', { tab_id: ownedTabId, observationId: observed.id, revision: observed.revision, action: turn === 5 ? 'click' : 'fill', ref: control.ref, ...(turn === 3 ? { value: 'OWNED-PASSWORD' } : turn === 4 ? { value: 'full' } : {}) })]); } return completion([], 'Owned full browser flow complete.'); }; const model = await compatibleFixture(t, (url, body) => ownedFailure ? { status: 429, body: { error: { code: 'owned_rate', type: 'owned_original', message: 'UI-UPSTREAM-ORIGINAL', detail: '原始上游正文'.repeat(6000) } }, headers: { 'x-request-id': 'owned-ui-error-id' } } : url.endsWith('/models') ? { data: [{ id: 'owned-capacity', context_length: 131072, max_output_tokens: 32768, capabilities: { text: true, tools: true } }, ...Array.from({ length: 121 }, (_, index) => ({ id: 'owned-bulk-' + index, capabilities: { text: true, tools: true } }))] } : body.messages.some(row => row.role === 'user' && row.content.includes('FULL-BROWSER-FLOW')) ? browserReply(body) : body.messages.some(row => row.role === 'user' && row.content.includes('PERMISSION-FLOW')) ? completion(body.messages.some(row => row.role === 'tool') ? [] : [call('owned-permission', 'run_command', { command: process.platform === 'win32' ? 'Write-Output "OWNED-PERMISSION-MARKER"' : 'printf OWNED-PERMISSION-MARKER', reason: 'Verify permission switching in the actual UI.' })], 'Permission flow complete.') : body.messages.some(row => row.role === 'user' && row.content.includes('AUTO-WRITE-FLOW')) ? completion(body.messages.some(row => row.role === 'tool') ? [] : [call('owned-write', 'propose_file', { path: 'ui-auto.txt', content: 'OWNED-APPLIED\n', reason: 'Verify automatic writes and exact rollback.' })], 'File flow complete.') : { ...completion(body.messages.some(row => row.role === 'tool') ? [] : [call('owned-list', 'list_files', { path: '' })], 'Owned reference answer.'), usage: { prompt_tokens: 1000, completion_tokens: 20, prompt_tokens_details: { cached_tokens: 700 } } }); const hub = new Provider().modelHub(f.store), source = hub.upsert({ name: 'Owned source', model: 'source', baseUrl: model.baseUrl, requiresKey: false }); const preview = await hub.modelCatalog().fetch({ id: source.id, revision: source.revision }); const applied = hub.modelCatalog().apply({ previewId: preview.id, models: ['owned-capacity'], routing: 'manual', defaultModel: 'owned-capacity' }); const connection = hub.upsert({ ...hub.connection(applied.added[0]), contextLimit: 16000, maxOutputTokens: 2048 }); assert.equal(connection.upstreamModel.maxOutputTokens, 32768); f.store.close(); if (desktop) { 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); controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start(); host = { api: (route, input) => controller.api(route, input ? { method: 'POST', input } : {}), bootstrap: await controller.api('/bootstrap') }; const executable = desktopExecutableIdentity(appRoot).path; application = await _electron.launch({ executablePath: executable, args: [...(executable.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port)], env: projectEnvironment({ AGENT_WORKBENCH_QA: '1', AGENT_WORKBENCH_NODE: process.execPath }) }); page = await application.firstWindow(); window = await application.browserWindow(page); await page.waitForLoadState(); } else { host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage(); await page.goto(host.url); } page.on('pageerror', error => errors.push(error.message)); await page.waitForLoadState('networkidle'); await resize(1440); const permission = () => page.getByLabel('操作权限', { exact: true }); await expect(permission()).toHaveValue('ask'); for (const mode of ['auto', 'full', 'ask']) { await permission().selectOption(mode); await until(async () => (await host.api('/bootstrap')).config.approvalMode === mode); await page.reload(); await expect(permission()).toHaveValue(mode); } mark('three permission choices save immediately, survive page reload and start at request approval'); const permissionSession = await host.api('/sessions', { projectId: f.project.id }); await host.api('/sessions/' + permissionSession.id + '/messages', { content: 'PERMISSION-FLOW: run the owned marker command once.' }); await until(async () => (await host.api('/sessions/' + permissionSession.id)).status === 'awaiting_approval', 30000); await permission().selectOption('full'); await until(async () => (await host.api('/sessions/' + permissionSession.id)).status === 'completed', 30000); const allowed = await host.api('/sessions/' + permissionSession.id); assert.equal(allowed.approvals.length, 1); assert.equal(allowed.approvals[0].decisionSource, 'full-access-policy'); assert.equal(model.requests.filter(row => row.input).length, 2); for (const width of [1440, 390]) { await resize(width); await expect(permission()).toHaveValue('full'); await shot('permissions-full-' + width); } mark('selecting full access while a command waits releases that original command exactly once'); await resize(1440); await open(); await dialog().getByRole('button', { name: '运行预算', exact: true }).click(); const budgets = () => dialog().getByRole('form', { name: '运行预算设置', exact: true }); const originalSelection = (await host.api('/model-center')).selected; await budgets().getByLabel('额度策略', { exact: true }).selectOption('unrestricted'); await expect(budgets().getByLabel('输出上限', { exact: true })).toHaveCount(0); await budgets().getByRole('button', { name: '保存运行预算', exact: true }).click(); await expect(dialog().getByRole('status').filter({ hasText: '运行预算已保存' })).toBeVisible(); const expected = { executionMode:'unrestricted', capacityMode:'passthrough', historyPolicy:'preserve', maxSteps:0, maxRunDurationMs:0, maxRunTokens:0, maxTaskTokens:0, modelResponseTimeoutMs:0, modelStreamIdleTimeoutMs:0 }; for (const [key, value] of Object.entries(expected)) assert.equal((await host.api('/bootstrap')).config[key], value, key); assert.deepEqual((await host.api('/model-center')).selected, originalSelection); await page.reload(); await open(); await dialog().getByRole('button', { name: '运行预算', exact: true }).click(); await expect(budgets().getByLabel('额度策略', { exact: true })).toHaveValue('unrestricted'); for (const width of [1440,390]) { await resize(width); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)); await shot('open-settings-' + width); } mark('open defaults save as real zeros, survive reload, hide local quotas and retain model selection at 1440 and 390 pixels'); await resize(1440); await dialog().getByRole('button', { name: '等待时限', exact: true }).click(); const waits = () => dialog().getByRole('form', { name: '模型等待时限', exact: true }); await expect(waits().getByLabel('单次模型响应上限(秒)', { exact: true })).toHaveValue('0'); await expect(waits().getByLabel('流式无数据上限(秒)', { exact: true })).toHaveValue('0'); await waits().getByLabel('单次模型响应上限(秒)', { exact: true }).fill('960'); await waits().getByLabel('流式无数据上限(秒)', { exact: true }).fill('901'); await waits().getByRole('button', { name: '保存等待时限', exact: true }).click(); await expect(dialog().getByRole('status').filter({ hasText: '等待时限已保存' })).toBeVisible(); await page.reload(); await open(); await dialog().getByRole('button', { name: '等待时限', exact: true }).click(); await expect(waits().getByLabel('单次模型响应上限(秒)', { exact: true })).toHaveValue('960'); await expect(waits().getByLabel('流式无数据上限(秒)', { exact: true })).toHaveValue('901'); for (const label of ['单次模型响应上限(秒)', '流式无数据上限(秒)']) await waits().getByLabel(label, { exact: true }).fill('0'); await waits().getByRole('button', { name: '保存等待时限', exact: true }).click(); await expect(dialog().getByRole('status').filter({ hasText: '等待时限已保存' })).toBeVisible(); assert.deepEqual((await host.api('/model-center')).waitSettings, { modelResponseTimeoutMs: 0, modelStreamIdleTimeoutMs: 0 }); for (const width of [1440,390]) { await resize(width); await shot('unlimited-waits-' + width); } mark('wait settings accept zero and values beyond old ceilings, persist after reload and restore unlimited waits'); await resize(1440); await dialog().getByRole('button', { name: '模型连接', exact: true }).click(); await dialog().getByRole('button', { name: '从上游添加模型', exact: true }).click(); const catalog = () => dialog().getByRole('region', { name: '上游模型目录', exact: true }); await catalog().getByLabel('目录上游连接', { exact: true }).selectOption(source.id); await catalog().getByRole('button', { name: '获取上游模型', exact: true }).click(); await expect(catalog().getByLabel('上游模型列表', { exact: true }).getByRole('checkbox')).toHaveCount(122); await catalog().getByRole('button', { name: '选中当前筛选', exact: true }).click(); await catalog().getByLabel('模型使用方式', { exact: true }).selectOption('auto'); await expect(catalog().getByRole('button', { name: '保存所选 122 个模型', exact: true })).toBeEnabled(); await expect(catalog()).toContainText('目录、连接和自动池不设本地数量上限'); for (const width of [1440,390]) { await resize(width); await shot('open-model-selection-' + width); } await catalog().getByRole('button', { name: '关闭目录', exact: true }).click(); assert.deepEqual((await host.api('/model-center')).selected, originalSelection); await dialog().getByRole('navigation', { name: '模型中心分类', exact: true }).getByRole('button', { name: '连接诊断', exact: true }).click(); const network = () => dialog().getByRole('region', { name: '模型连接诊断', exact: true }); await network().getByLabel('诊断连接', { exact: true }).selectOption(source.id); await network().getByRole('button', { name: '开始直连检查', exact: true }).click(); await expect(network().getByRole('status')).toHaveText('直连握手通过'); await expect(network()).toContainText('保留全部检查记录'); await network().locator('details summary').click(); await expect(network().getByText('不限 / 不限', { exact: true })).toBeVisible(); const diagnostic = (await host.api('/model-center/network-diagnostics')).jobs[0]; assert.equal(diagnostic.status, 'passed'); assert.equal(diagnostic.budgetMs, 0); assert.equal(diagnostic.stageTimeoutMs, 0); assert.equal(diagnostic.httpRequests, 0); assert.equal(diagnostic.modelRequests, 0); for (const width of [1440,390]) { await resize(width); await shot('open-network-diagnostic-' + width); } mark('catalog UI selects 122 models and enables automatic routing; actual TCP diagnostics show unlimited waits and complete history'); await resize(1440); await dialog().getByRole('button', { name: '运行预算', exact: true }).click(); await budgets().getByLabel('额度策略', { exact: true }).selectOption('restricted'); await budgets().getByLabel('容量策略', { exact: true }).selectOption('manual'); await budgets().getByLabel('输出上限', { exact: true }).fill('32768'); await budgets().getByLabel('上下文预算', { exact: true }).fill('131072'); await budgets().getByRole('button', { name: '保存运行预算', exact: true }).click(); await expect(dialog().getByRole('status').filter({ hasText: '运行预算已保存' })).toBeVisible(); assert.equal((await host.api('/bootstrap')).config.maxOutputTokens,32768); await budgets().getByLabel('额度策略', { exact: true }).selectOption('unrestricted'); await budgets().getByRole('button', { name: '保存运行预算', exact: true }).click(); await expect(dialog().getByRole('status').filter({ hasText: '运行预算已保存' })).toBeVisible(); mark('explicit manual settings remain selectable and returning to open mode removes their enforcement'); await dialog().getByRole('button', { name:'关闭窗口', exact:true }).click(); await host.api('/sessions/'+f.session.id+'/messages',{content:'Inspect the directory once and answer only. Do not modify files or run checks.'}); let state; await until(async()=>{state=await host.api('/sessions/'+f.session.id);return !['running','awaiting_approval','stopping'].includes(state.status);},30000); assert.equal(state.status,'completed',JSON.stringify(state.failure)); assert.equal(model.requests.filter(row=>row.input).length,4); for(const row of model.requests.filter(row=>row.input)) for(const key of ['max_tokens','max_output_tokens','max_completion_tokens']) assert.equal(Object.hasOwn(row.input,key),false); async function selectProject() { const project=page.getByLabel('当前项目',{exact:true});if(!await project.isVisible())await page.getByRole('button',{name:'展开项目栏',exact:true}).click();await project.selectOption(f.project.id); } await page.reload(); await selectProject(); await page.getByRole('button',{name:'查看任务记录',exact:true}).click(); const context=page.getByLabel('请求上下文',{exact:true}); await expect(context).toContainText('700 tokens'); await expect(context).toContainText('不附加上限'); for(const width of [1440,390]){await resize(width);if(!await context.isVisible())await page.getByRole('button',{name:'查看任务记录',exact:true}).click();await context.scrollIntoViewIfNeeded();assert.ok(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+1));await shot('open-request-cache-'+width);} fs.writeFileSync(path.join(directory,'completed-session.json'),JSON.stringify(state,null,2)); mark('actual requests omit output limits and request records separately show complete history, unlimited budgets and reported cache usage'); ownedFailure=true; await host.api('/sessions/'+f.session.id+'/messages',{content:'Reply once and retain any upstream error.'}); await until(async()=>{state=await host.api('/sessions/'+f.session.id);return !['running','awaiting_approval','stopping'].includes(state.status);},30000); assert.equal(state.status,'failed');assert.equal(state.failure.httpStatus,429);assert.equal(state.failure.upstream.code,'owned_rate');assert.equal(state.failure.retryDecision,'passthrough'); assert.equal(model.requests.filter(row=>row.input).length,5); await page.reload();await selectProject(); const details=page.locator('details.model-error-details').filter({hasText:'owned-ui-error-id'}).first();await details.locator('summary').click();await expect(details).toContainText('上游模型服务');await expect(details).toContainText('UI-UPSTREAM-ORIGINAL'); await expect(details.getByRole('button',{name:'下载已保存的原文',exact:true})).toBeVisible(); for(const width of [1440,390]){await resize(width);if(!await details.isVisible())await page.getByRole('button',{name:'查看对话',exact:true}).click();await details.scrollIntoViewIfNeeded();await shot('original-upstream-error-'+width);} fs.writeFileSync(path.join(directory,'failed-session.json'),JSON.stringify(state,null,2));assert.deepEqual(errors,[]); mark('original upstream status, code, request ID, preview and complete-body download are visible without retry or failover'); ownedFailure = false; const writeSession = await host.api('/sessions', { projectId: f.project.id }); await host.api('/sessions/' + writeSession.id + '/messages', { content: 'AUTO-WRITE-FLOW: create the owned UI file.' }); await until(async () => (await host.api('/sessions/' + writeSession.id)).status === 'completed', 30000); const written = await host.api('/sessions/' + writeSession.id); assert.equal(written.changes[0].status, 'applied'); assert.equal(fs.readFileSync(path.join(f.projectRoot, 'ui-auto.txt'), 'utf8'), 'OWNED-APPLIED\n'); await resize(1440); await page.reload(); await selectProject(); await page.getByRole('button', { name: /AUTO-WRITE-FLOW/ }).first().click(); await page.locator('.inspector-tabs').getByRole('button', { name: '改动', exact: true }).click(); await expect(page.getByRole('button', { name: '回滚这项改动', exact: true })).toBeVisible(); await shot('automatically-applied-file'); await page.getByRole('button', { name: '回滚这项改动', exact: true }).click(); await until(async () => (await host.api('/sessions/' + writeSession.id)).changes[0].status === 'rolled_back'); assert.equal(fs.existsSync(path.join(f.projectRoot, 'ui-auto.txt')), false); await shot('rolled-back-file'); assert.equal(await page.getByText('项改动等待审查', { exact: false }).count(), 0); mark('an automatic file write has no review pause and its UI rollback restores the original absence without deleting history'); await page.getByRole('button', { name: '关闭检查器', exact: true }).click().catch(() => {}); const image=new PNG({width:32,height:16}),imageBytes=PNG.sync.write(image); await page.getByLabel('选择工程截图', { exact: true }).setInputFiles(Array.from({length:9},(_,index)=>({name:'owned-'+index+'.png',mimeType:'image/png',buffer:imageBytes}))); await expect(page.locator('.input-draft')).toHaveCount(9); await expect(page.locator('.input-draft input:checked')).toHaveCount(9); await page.locator('.input-remove').nth(1).click(); await expect(page.locator('.input-draft')).toHaveCount(8); await expect(page.locator('.input-draft input:checked')).toHaveCount(8); await resize(390);await shot('multiple-input-images-390');await resize(1440); mark('nine images upload together and removing one retains all other selected images without a one-image quota'); if (desktop) { const browserSession = await host.api('/sessions', { projectId: f.project.id }); await host.api('/sessions/' + browserSession.id + '/messages', { content: 'FULL-BROWSER-FLOW: open and operate the owned local site.' }); let browserState; await until(async () => { browserState = await host.api('/sessions/' + browserSession.id); return !['running', 'stopping', 'awaiting_approval'].includes(browserState.status); }, 45000); fs.writeFileSync(path.join(directory, 'full-browser-session.json'), JSON.stringify(browserState, null, 2)); assert.equal(browserState.status, 'completed', JSON.stringify(browserState.failure)); assert.equal(browserTurn, 6); assert.equal(browserState.approvals.length, 4); assert.ok(browserState.approvals.every(value => value.status === 'approved' && value.decisionSource === 'full-access-policy')); await until(() => application.evaluate(({ webContents }) => webContents.getAllWebContents().some(value => value.getURL().includes('localhost:') && value.getURL().endsWith('/next'))), 15000); fs.writeFileSync(path.join(directory, 'full-browser-session.json'), JSON.stringify(browserState, null, 2)); const png = await application.evaluate(async ({ webContents }) => (await webContents.getAllWebContents().find(value => value.getURL().includes('localhost:') && value.getURL().endsWith('/next')).capturePage()).toPNG().toString('base64')); const screenshot = path.join(directory, 'full-browser-second-origin.png'); fs.writeFileSync(screenshot, Buffer.from(png, 'base64')); screenshots.push(screenshot); await host.api('/sessions/' + browserSession.id + '/browsing/tabs/' + ownedTabId + '/close', { approved: true }); mark('actual Electron full access opens localhost, retains large DOM and 133 controls, fills password/select and navigates across origins without a manual approval'); } assert.deepEqual(errors, []); assert.equal(model.requests.filter(row => row.input).length, desktop ? 13 : 7); result={success:true,version:host.bootstrap.version,appRoot,actualElectron:desktop,checks,screenshots,errors,realModelRequests:0,ownedModelRequests:desktop ? 13 : 7,directoryGets:model.requests.filter(row => !row.input).length,officialServiceTouched:false}; } catch(error) {if(page)await shot('failure').catch(()=>{});result={success:false,appRoot,actualElectron:desktop,checks,screenshots,errors,error:error.stack,realModelRequests:0};} finally {if(application)await application.close();if(browser)await browser.close();if(controller&&await controller.health())await controller.stop();await host?.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}));if(!result.success)process.exitCode=1;