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 { DatabaseSync } from 'node:sqlite'; import { chromium, _electron } from 'playwright'; import { expect } from 'playwright/test'; import { fixture, launchWorkbench, until } from '../tests/helpers.mjs'; import { Provider } from '../src/server/provider.mjs'; import { HostController } from '../src/core/desktop/host-controller.mjs'; import { desktopLaunchCommand } from './desktop-launch.mjs'; import { projectEnvironment } from '../src/core/process-environment.mjs'; import { usageSamples, usageModelId, cacheUsageFixture } from '../tests/fixtures/cache-usage-model.mjs'; import { historicalAttempts, inspectExport, hash } from '../tests/fixtures/request-ledger-data.mjs'; import { openWorkbenchTool } from './ui-navigation.mjs'; assert.ok(process.argv.slice(2).every(value => value === '--desktop')); assert.ok(process.env.AGENT_WORKBENCH_QA_APP_ROOT, 'Use a frozen independent candidate'); const desktop = process.argv.includes('--desktop'), appRoot = fs.realpathSync.native(process.env.AGENT_WORKBENCH_QA_APP_ROOT); const directory = path.resolve('.qa/m2-request-ledger-2026-11-07', (desktop ? 'electron-' : 'web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true }); console.log('EVIDENCE ' + directory); const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true }); const model = await cacheUsageFixture(t, { ...usageSamples.chat, streaming: true }); const hub = Object.assign(Object.create(Provider.prototype), { apiKey: '' }).modelHub(f.store); const connection = hub.upsert({ name: '自有账本验证 · Chat', model: usageModelId, protocol: 'chat-completions', streaming: true, baseUrl: model.baseUrl, requiresKey: false, modelIdentityPolicy: 'require-match' }); hub.select({ type: 'connection', id: connection.id }); // Config setup uses source contracts; the candidate itself receives an old // schema-1 database with the complete original array and performs migration. assert.equal(hub.ledger.rollbackInitialization(), true); const historical = historicalAttempts(); historical.at(-1).status = 'running'; delete historical.at(-1).finishedAt; historical.at(-1).usageStatus = 'unknown'; historical.at(-1).usage = null; f.store.storage.setMeta('modelAttempts', historical); const originalMetadata = f.store.storage.database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get().value; fs.writeFileSync(path.join(directory, 'legacy-metadata.json'), originalMetadata); f.store.close(); const checks = [], screenshots = [], downloads = [], errors = [], externalBlocked = [], pages = []; let host, controller, browser, app, page, window, port, result, desktopStdout = '', desktopStderr = '', beforeRestart, current; const mark = value => { checks.push(value); console.log('PASS ' + value); }; const center = () => page.getByRole('dialog', { name: '模型中心', exact: true }); const ledger = () => center().getByRole('region', { name: '完整请求账本', exact: true }); const entries = () => ledger().locator('.mc-attempts article'); const pager = () => ledger().getByRole('navigation', { name: '请求账本分页 · 列表上方', exact: true }); async function openLedger() { await openWorkbenchTool(page, '打开模型中心'); await center().getByRole('button', { name: '请求账本', exact: true }).click(); await expect(ledger()).toHaveAttribute('aria-busy', 'false'); } async function resize(width, height = 960) { if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height }); await until(() => page.evaluate(w => innerWidth === w, width)); } else await page.setViewportSize({ width, height }); } async function shot(name, scope) { await scope.scrollIntoViewIfNeeded(); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'No unintended page overflow'); const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push({ file: path.basename(file), sha256: hash(fs.readFileSync(file)) }); fs.writeFileSync(path.join(directory, name + '.txt'), await scope.innerText()); } async function download(name, action) { const file = path.join(directory, name); let native = null; if (app) { const id = randomUUID(); await app.evaluate(({ session }, { id, file }) => { globalThis.workbenchQaDownloads ??= {}; globalThis.workbenchQaDownloads[id] = { state: 'waiting' }; session.defaultSession.once('will-download', (_event, item) => { const value = globalThis.workbenchQaDownloads[id]; value.suggestedFilename = item.getFilename(); value.state = 'started'; item.setSavePath(file); item.once('done', (_event, state) => { value.state = state; value.bytes = item.getReceivedBytes(); value.totalBytes = item.getTotalBytes(); }); }); }, { id, file }); await action(); await expect.poll(() => app.evaluate((_electron, id) => globalThis.workbenchQaDownloads[id].state, id), { timeout: 30000 }).toBe('completed'); native = await app.evaluate((_electron, id) => globalThis.workbenchQaDownloads[id], id); assert.equal(native.bytes, native.totalBytes); assert.equal(native.bytes, fs.statSync(file).size); } else { const pending = page.waitForEvent('download'); await action(); await (await pending).saveAs(file); } const bytes = fs.readFileSync(file); downloads.push({ file: name, bytes: bytes.length, sha256: hash(bytes), ...(native ? { native, scope: 'Actual Electron DownloadItem; QA selects owned path, Windows save dialog not verified' } : { scope: 'Actual browser download' }) }); return bytes; } async function start() { if (desktop) { if (!port) { const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); 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 = { url: controller.url, api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) }; app = await _electron.launch({ ...desktopLaunchCommand(appRoot, ['--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window']), cwd: appRoot, env: projectEnvironment(), timeout: 60000 }); app.process().stdout?.on('data', bytes => { desktopStdout += bytes.toString('utf8'); }); app.process().stderr?.on('data', bytes => { desktopStderr += bytes.toString('utf8'); }); page = await app.firstWindow(); window = await app.browserWindow(page); await window.evaluate(win => win.showInactive()); } 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({ acceptDownloads: true }); } page.on('pageerror', error => errors.push(error.message)); await page.route('**/*', route => { const url = new URL(route.request().url()); if (url.origin === new URL(host.url).origin || ['data:', 'blob:'].includes(url.protocol)) return route.continue(); externalBlocked.push(url.origin); return route.abort(); }); await page.addInitScript(id => localStorage.setItem('awb-project', id), f.project.id); if (desktop) { // main.mjs awaits its initial loadURL. Replacing that navigation early // rejects startup and closes the actual Electron app. await page.waitForURL(url => url.origin === new URL(host.url).origin && url.searchParams.get('desktop') === '1'); await page.waitForLoadState('networkidle'); await page.reload(); } else await page.goto(host.url); await page.waitForLoadState('networkidle'); } async function stop() { await app?.close(); await browser?.close(); if (controller && await controller.health()) await controller.stop(); await host?.close?.(); app = null; browser = null; page = null; window = null; } async function filter(query = '', status = '', connectionId = '') { await ledger().getByLabel('搜索请求账本', { exact: true }).fill(query); await ledger().getByLabel('筛选账本连接', { exact: true }).selectOption(connectionId); await ledger().getByLabel('筛选账本结果', { exact: true }).selectOption(status); await ledger().getByRole('button', { name: '筛选记录', exact: true }).click(); await expect(ledger()).toHaveAttribute('aria-busy', 'false'); } function originalState() { const database = new DatabaseSync(path.join(f.data, 'workbench.sqlite'), { readOnly: true }); try { const original = database.prepare("SELECT value FROM metadata WHERE key='modelAttempts'").get().value; assert.equal(original, originalMetadata); return { originalMetadataSha256: hash(original), databaseVersion: database.prepare('PRAGMA user_version').get().user_version }; } finally { database.close(); } } try { await start(); const initial = await host.api('/model-center/attempts?limit=2000'); assert.equal(initial.total, 1107); assert.equal(initial.items.length, 1107); const unknown = await host.api('/model-center/attempts/1107'); assert.equal(unknown.item.status, 'unknown'); assert.deepEqual(unknown.initial.item, historical.at(-1)); assert.equal(originalState().databaseVersion, 2); assert.equal((await host.api('/bootstrap')).storage.schemaVersion, 2); assert.equal(model.requests.length, 0); fs.writeFileSync(path.join(directory, 'initial-page.json'), JSON.stringify(initial, null, 2)); await openLedger(); await resize(1280); await expect(entries()).toHaveCount(50); await expect(ledger()).toContainText('1,107 条已保存'); await shot('ledger-controls-1280', ledger().locator('.mc-ledger-filter')); const unknownRow = entries().first(); await unknownRow.locator('.mc-ledger-source > summary').focus(); await page.keyboard.press('Enter'); const sourceBytes = await download('legacy-unknown-source.json', () => unknownRow.getByRole('button', { name: '下载本条与初始记录', exact: true }).click()); assert.deepEqual(JSON.parse(sourceBytes), unknown); await resize(390, 844); await shot('unknown-initial-source-390', unknownRow.locator('.mc-ledger-source')); mark('Candidate migrates all 1107 old receipts, keeps metadata bytes, renders recovered unknown and downloads its unchanged initial record with keyboard expansion'); await resize(1280); const sequences = []; while (true) { const values = await entries().evaluateAll(rows => rows.map(row => Number(row.dataset.ledgerSequence))); pages.push(values); sequences.push(...values); if (await pager().getByRole('button', { name: '下一页', exact: true }).isDisabled()) break; await pager().getByRole('button', { name: '下一页', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', String(values.at(-1) - 1)); } assert.deepEqual(sequences, Array.from({ length: 1107 }, (_, index) => 1107 - index)); assert.equal(pages.length, 23); await pager().getByRole('button', { name: '上一页', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '57'); await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '1107'); fs.writeFileSync(path.join(directory, 'ui-page-sequences.json'), JSON.stringify(pages)); mark('All 23 UI pages cross old 100/1000 boundaries exactly once; previous page and refresh retain complete ordered history without model dispatch'); await filter('owned-history', 'failed', 'owned-beta'); const filtered = historical.filter(value => value.connectionId === 'owned-beta' && value.status === 'failed'); await expect(pager()).toContainText('当前范围 ' + filtered.length + ' 条'); const filteredBytes = await download('filtered-complete.ndjson', () => ledger().getByRole('button', { name: '导出当前范围全部记录', exact: true }).click()); assert.deepEqual(inspectExport(filteredBytes).records.map(value => value.item), filtered); await shot('historical-filter-1280', ledger().locator('.mc-ledger-filter')); await filter('owned-no-match', 'failed', 'owned-beta'); await expect(ledger()).toContainText('当前范围没有请求记录'); const emptyBytes = await download('empty-complete.ndjson', () => ledger().getByRole('button', { name: '导出当前范围全部记录', exact: true }).click()); assert.equal(inspectExport(emptyBytes).records.length, 0); let injected = 0; const pattern = '**/api/model-center/attempts?*'; await page.route(pattern, route => { injected++; return route.fulfill({ status: 400, contentType: 'application/json', body: JSON.stringify({ error: '自有账本读取失败,原记录保留,请刷新核对。' }) }); }); await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(ledger().getByRole('alert')).toContainText('自有账本读取失败'); await expect(ledger().getByLabel('筛选账本连接', { exact: true })).toHaveValue('owned-beta'); await expect(ledger().getByLabel('搜索请求账本', { exact: true })).toHaveValue('owned-no-match'); await expect(ledger().getByLabel('筛选账本结果', { exact: true })).toHaveValue('failed'); await shot('owned-read-error-1280', ledger().getByRole('alert')); await page.unroute(pattern); assert.equal(injected, 1); await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(ledger()).toContainText('当前范围没有请求记录'); await filter(); await expect(entries()).toHaveCount(50); assert.equal(model.requests.length, 0); await resize(390, 844); await shot('ledger-controls-390', ledger().locator('.mc-ledger-filter')); mark('Deleted historical connection/status/text filters and complete/empty exports work; a separately injected local read failure recovers without changing history or contacting a model'); current = await host.api('/sessions', { projectId: f.project.id }); await host.api('/sessions/' + current.id + '/messages', { content: '自有账本增量验证:只回复已收到,不修改文件或调用工具。' }); await until(async () => (await host.api('/sessions/' + current.id)).status === 'completed', 30000); assert.equal(model.requests.length, 1); for (const field of ['max_tokens', 'max_completion_tokens', 'max_output_tokens']) assert.equal(field in model.requests[0].input, false); await ledger().getByRole('button', { name: '刷新记录', exact: true }).click(); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '1108'); const row = entries().first(); await row.locator('.model-usage-details > summary').click(); await expect(row).toContainText('300 tokens'); const responseBytes = await download('actual-local-response.sse', () => row.getByRole('button', { name: '下载已保存的响应', exact: true }).click()); assert.equal(hash(responseBytes), hash(model.responses[0])); await resize(1280); await shot('new-incremental-receipt-1280', row.locator('.model-usage-details')); const allBytes = await download('all-1108-complete.ndjson', () => ledger().getByRole('button', { name: '导出当前范围全部记录', exact: true }).click()); const full = inspectExport(allBytes); assert.equal(full.records.length, 1108); assert.deepEqual(full.records.slice(0, 1107).map(value => value.initial.item), historical); for (let index = 0; index < 1106; index++) assert.deepEqual(full.records[index].item, historical[index]); assert.equal(full.records[1106].item.status, 'unknown'); assert.equal(full.records[1107].item.sessionId, current.id); assert.equal(full.records[1107].initial.item.status, 'running'); beforeRestart = await host.api('/model-center/attempts?limit=2000'); fs.writeFileSync(path.join(directory, 'before-restart.json'), JSON.stringify(beforeRestart, null, 2)); mark('One actual local SSE request writes the new source-bound receipt, preserves I10 usage, downloads exact response bytes and exports all 1108 current/initial records with count and line SHA'); await stop(); await start(); const restored = await host.api('/model-center/attempts?limit=2000'); assert.deepEqual(restored, beforeRestart); await openLedger(); await resize(1280); await expect(entries().first()).toHaveAttribute('data-ledger-sequence', '1108'); await expect(ledger()).toContainText('1,108 条已保存'); await shot('restart-preserved-1280', ledger().locator('.mc-ledger-filter')); assert.equal(model.requests.length, 1); assert.equal(originalState().databaseVersion, 2); const config = (await host.api('/bootstrap')).config; assert.equal(config.executionMode, 'unrestricted'); assert.equal(config.capacityMode, 'passthrough'); assert.equal(config.historyPolicy, 'preserve'); assert.equal((await host.api('/bootstrap')).storage.schemaVersion, 2); const finalCenter = await host.api('/model-center'); assert.equal(finalCenter.attemptCount, 1108); assert.equal(finalCenter.attempts.length, 100); assert.equal(finalCenter.storage.attemptsRetained, null); fs.writeFileSync(path.join(directory, 'final-model-center.json'), JSON.stringify(finalCenter, null, 2)); fs.writeFileSync(path.join(directory, 'after-restart.json'), JSON.stringify(restored, null, 2)); assert.deepEqual(errors, []); assert.deepEqual(externalBlocked, []); mark('Owned host restart retains current/initial SHA, unknown/error history and default passthrough permissions exactly, with zero replay and a complete saved count'); result = { success: true, appRoot, actualElectron: desktop, checks, screenshots, downloads, pages: pages.length, ...originalState(), errors, externalBlocked, syntheticRequests: model.requests.length, realModelRequests: 0, realCredentialsRead: 0, productionWrites: 0, viewportWidths: [1280, 390] }; } catch (cause) { if (page) { await page.screenshot({ path: path.join(directory, 'failure.png') }).catch(() => {}); fs.writeFileSync(path.join(directory, 'failure.txt'), await page.locator('body').innerText().catch(() => '')); } result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, downloads, errors, externalBlocked, error: cause.stack, syntheticRequests: model.requests.length, realModelRequests: 0 }; } finally { fs.writeFileSync(path.join(directory, 'requests.json'), JSON.stringify(model.requests, null, 2)); await stop(); fs.cpSync(f.data, path.join(directory, 'retained-owned-data'), { recursive: true, errorOnExist: true }); for (const dispose of disposers.reverse()) await dispose(); if (desktop) { fs.writeFileSync(path.join(directory, 'desktop-stdout.txt'), desktopStdout); fs.writeFileSync(path.join(directory, 'desktop-stderr.txt'), desktopStderr); } fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); } console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1;