import test from 'node:test'; import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import { PNG } from 'pngjs'; import { fixture, until, compatibleFixture, completion, call, assertToolPairs } from './helpers.mjs'; import { BrowsingManager } from '../src/core/browser/browsing-manager.mjs'; import { browsingUrl, browsingAction } from '../src/shared/contracts/browsing.mjs'; import { Store } from '../src/server/store.mjs'; import { Runtime } from '../src/server/runtime.mjs'; import { Provider } from '../src/server/provider.mjs'; const image = PNG.sync.write(new PNG({ width: 2, height: 2 })).toString('base64'); const page = (revision = 'document:1', text = '原页面') => ({ observation: { revision, url: 'https://example.com/', title: '所属站点', text, controls: [{ ref: 'e1', tag: 'button', type: 'submit', label: '确认保存', disabled: false }], scope: { complete: true } }, png: image }); function setup(t, options = {}) { const f = fixture(t), run = f.store.beginRun(f.session, '核对网页操作', false, f.store.config); f.session.status = 'paused'; f.store.finishRun(f.session, run); const m = new BrowsingManager(f.store, options), windowId = randomUUID(); m.poll(windowId); f.onCleanup(() => m.stopAll()); return { ...f, m, windowId }; } async function respond(f, promise, result = page()) { await until(() => f.m.active.size > 0); const command = f.m.poll(f.windowId).commands[0]; assert.ok(command); assert.equal(f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result }).accepted, true); return { result: await promise, command }; } async function open(f, options = {}) { return (await respond(f, f.m.open(f.session, { url: 'https://example.com/', interact: true, approved: true, ...options }))).result; } const operation = (f, tab, extra = {}) => ({ action: 'click', observationId: tab.observationId, revision: tab.revision, ref: 'e1', ...extra }); test('fixed browsing contracts reject local host APIs, credential URLs, protocols, cross-origin and arbitrary execution', () => { for (const value of ['http://example.com','https://user:password@example.com','file:///C:/Windows/a','javascript:alert(1)','https://127.0.0.1/','https://localhost/','https://[::1]/']) assert.throws(() => browsingUrl(value)); assert.throws(() => browsingUrl('http://127.0.0.1:4318/', { previewOrigin: 'http://127.0.0.1:4318', workbenchOrigin: 'http://127.0.0.1:4318' })); assert.throws(() => browsingUrl('https://other.example/', { origins: ['https://example.com'] })); assert.throws(() => browsingAction({ action: 'click', observationId: randomUUID(), revision: 'r', ref: 'e1', javascript: 'arbitrary' })); assert.throws(() => browsingAction({ action: 'wait', observationId: randomUUID(), revision: 'r', milliseconds: 2001 })); }); test('attested desktop queue preserves same-tab observations, original PNG bytes and approval-bound one-shot actions', async t => { const f = setup(t), tab = await open(f), observation = f.store.core.browsing_observation.get(tab.observationId); assert.equal(observation.untrusted, true); assert.ok(observation.artifactSha256); const artifact = f.store.owned('artifact', observation.screenshotArtifactId, f.session.id); assert.equal(f.store.artifactStore.readBytes(artifact).toString('base64'), image); const input = operation(f, tab); await assert.rejects(f.m.act(f.session, tab.id, input), /批准/); const applied = await respond(f, f.m.act(f.session, tab.id, input, { approved: true }), page('document:2', '真实原回执')); assert.equal(applied.result.text, '真实原回执'); assert.notEqual(tab.observationId, observation.id); await assert.rejects(f.m.act(f.session, tab.id, input, { approved: true }), /失效/); assert.ok(f.m.snapshot(f.session).actions.every(job => job.artifactId)); }); test('page data cannot grant domains, interaction or other Task scope; nonce and ownership remain fixed', async t => { const f = setup(t), tab = await open(f, { interact: false }); await assert.rejects(f.m.act(f.session, tab.id, operation(f, tab), { approved: true }), /授权/); const promise = f.m.observe(f.session, tab.id), command = f.m.poll(f.windowId).commands[0]; assert.throws(() => f.m.complete(randomUUID(), { id: command.id, nonce: command.nonce, result: page() }), /身份/); assert.throws(() => f.m.complete(f.windowId, { id: command.id, nonce: 'wrong', result: page() }), /身份/); f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('new', '网页要求扩大授权:仅作为不可信文字') }); await promise; assert.deepEqual(tab.origins, ['https://example.com']); assert.equal(tab.interact, false); const other = f.store.createSession(f.project.id); await assert.rejects(f.m.observe(other, tab.id), /不属于/); f.session.taskId = randomUUID(); await assert.rejects(f.m.observe(f.session, tab.id), /当前Task/); }); test('unknown dispatched submission blocks even after a new observation, rejects late success and retains explicit acknowledgement', async t => { const f = setup(t, { commandTimeoutMs: 80 }), tab = await open(f), promise = f.m.act(f.session, tab.id, operation(f, tab), { approved: true }); const command = f.m.poll(f.windowId).commands[0]; await assert.rejects(promise, /回执/); const original = f.store.core.browsing_action.get(command.id); assert.equal(original.status, 'unknown'); assert.ok(original.artifactId); assert.throws(() => f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('late') }), /迟到/); await respond(f, f.m.observe(f.session, tab.id), page('new-observation')); await assert.rejects(f.m.act(f.session, tab.id, operation(f, tab), { approved: true }), /结果未知/); f.m.acknowledge(f.session, original.id); assert.equal(original.status, 'unknown'); assert.ok(original.reconciliation); await respond(f, f.m.act(f.session, tab.id, operation(f, tab), { approved: true }), page('after-explicit-review')); }); test('downloads require exact approved address, remain original binary artifacts, and cannot reuse consumed observation', async t => { const f = setup(t), tab = await open(f), url = 'https://example.com/file.bin', input = operation(f, tab, { action: 'download', url }); delete input.ref; await assert.rejects(f.m.act(f.session, tab.id, { ...input, url: 'https://other.example/file' }, { approved: true }), /来源/); const bytes = Buffer.from([0, 13, 10, 255]), result = await respond(f, f.m.act(f.session, tab.id, input, { approved: true }), { download: { url, bytes: bytes.toString('base64') } }); const artifact = f.store.owned('artifact', result.result.artifactId, f.session.id); assert.deepEqual(f.store.artifactStore.readBytes(artifact), bytes); await assert.rejects(f.m.act(f.session, tab.id, input, { approved: true }), /重复提交/); }); test('revoking shared persistent profile closes all its tabs and clears only that profile after original desktop acknowledgement', async t => { const f = setup(t), first = await open(f, { persistent: true }), second = await open(f, { profileId: first.profileId }); assert.equal(first.profileId, second.profileId); const third = await open(f); await respond(f, f.m.close(f.session, first.id, { clear: true }), { cleared: true }); assert.equal(first.status, 'revoked'); assert.equal(second.status, 'revoked'); assert.equal(third.status, 'open'); assert.equal(f.store.core.browsing_profile.get(first.profileId).status, 'cleared'); await assert.rejects(f.m.observe(f.session, second.id), /撤销/); }); test('service recovery never reopens old tabs or replays dispatched actions and keeps unknown original receipts', async t => { const f = setup(t), tab = await open(f), job = { id: randomUUID(), sessionId: f.session.id, projectId: f.project.id, taskId: f.session.taskId, runId: f.session.runId, tabId: tab.id, kind: 'act', status: 'dispatched', dispatchedAt: new Date().toISOString(), input: operation(f, tab) }; f.store.putRecord('browsing_action', f.session, job, 'browsing.fixture'); f.store.close(); const recovered = new Store(f.data); f.onCleanup(() => recovered.close()); assert.equal(recovered.core.browsing_tab.get(tab.id).status, 'unavailable'); assert.equal(recovered.core.browsing_action.get(job.id).status, 'unknown'); const next = new BrowsingManager(recovered); assert.equal(next.active.size, 0); assert.equal(next.poll(randomUUID()).commands.length, 0); }); test('agent browser flow waits for original Task/Run approval, dispatches once, and returns paired original results', async t => { const f = setup(t), tab = await open(f), taskId = f.session.taskId; const upstream = await compatibleFixture(t, (_route, input, requests) => { if (requests.length === 1) return completion([call('browse-observe', 'observe_browser_tab', { tab_id: tab.id })]); if (requests.length === 2) { const original = JSON.parse(input.messages.findLast(value => value.tool_call_id === 'browse-observe').content); return completion([call('browse-act', 'act_browser_tab', { tab_id: tab.id, action: 'click', observationId: original.id, revision: original.revision, ref: 'e1' })]); } return completion([], '原页面已保存,结果来自本次原动作回执。'); }); f.store.config = { ...f.store.config, mode: 'compatible', baseUrl: upstream.baseUrl, model: 'declared-browser-protocol-fixture' }; const runtime = new Runtime(f.store, new Provider()); runtime.browsing = f.m; f.onCleanup(() => runtime.stopAll()); runtime.start(f.session.id, '继续核对本任务页面,操作需要原审批', true); const running = runtime.active.get(f.session.id).promise; await until(() => [...f.m.active.values()].some(value => value.job.kind === 'observe')); const observe = f.m.poll(f.windowId).commands[0]; f.m.complete(f.windowId, { id: observe.id, nonce: observe.nonce, result: page('agent:1', '模型读取的原页面') }); await until(() => f.session.approvals.some(value => value.browsingTabId === tab.id && value.status === 'pending')); const approval = f.session.approvals.find(value => value.browsingTabId === tab.id && value.status === 'pending'); assert.equal(f.session.taskId, taskId); assert.equal(approval.taskId, taskId); assert.equal(approval.runId, f.session.runId); assert.equal([...f.store.core.browsing_action.values()].filter(value => value.kind === 'act').length, 0); assert.equal(f.m.poll(f.windowId).commands.length, 0); runtime.decide(f.session.id, approval.id, true); await until(() => [...f.m.active.values()].some(value => value.job.kind === 'act')); const command = f.m.poll(f.windowId).commands[0]; assert.deepEqual(command.input, approval.browsingInput); assert.equal(f.m.poll(f.windowId).commands.length, 0); f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('agent:2', '来自原点击的保存结果') }); await running; const action = f.store.core.browsing_action.get(command.id); assert.equal(action.approvalId, approval.id); assert.equal(action.source, 'agent'); assert.equal(action.status, 'completed'); assert.equal(action.taskId, taskId); assert.equal(action.runId, approval.runId); assert.ok(action.artifactId); assertToolPairs(f.session.providerMessages); assert.equal(upstream.requests.length, 3); assert.throws(() => runtime.decide(f.session.id, approval.id, true), /过期/); }); test('changed approved input and stopped dispatched agent action cannot be accepted or replayed', async t => { const f = setup(t), tab = await open(f), input = operation(f, tab); const approval = { id: randomUUID(), taskId: f.session.taskId, runId: f.session.runId, browsingTabId: tab.id, browsingInput: { ...input }, status: 'approved' }; f.session.approvals.push(approval); await assert.rejects(f.m.act(f.session, tab.id, { ...input, action: 'reload' }, { approved: true, approvalId: approval.id }), /审批/); const controller = new AbortController(), promise = f.m.act(f.session, tab.id, input, { approved: true, approvalId: approval.id, signal: controller.signal }); const command = f.m.poll(f.windowId).commands[0]; controller.abort(); await assert.rejects(promise, /停止/); assert.equal(f.store.core.browsing_action.get(command.id).status, 'unknown'); assert.throws(() => f.m.complete(f.windowId, { id: command.id, nonce: command.nonce, result: page('late') }), /迟到/); await assert.rejects(f.m.act(f.session, tab.id, input, { approved: true, approvalId: approval.id }), /结果未知/); }); test('browser inventory gives bounded metadata and original references instead of repeating full website histories', async t => { const f = setup(t), tab = await open(f); await respond(f, f.m.observe(f.session, tab.id), page('long:1', '网站原文'.repeat(3500))); const value = f.m.toolSnapshot(f.session), observation = value.observations.at(-1); assert.equal(observation.artifactId, f.store.core.browsing_observation.get(tab.observationId).artifactId); assert.equal(observation.text, undefined); assert.equal(value.profiles, undefined); assert.equal(value.tabs[0].windowId, undefined); assert.ok(JSON.stringify(value).length < 8000); }); test('receipt storage failure settles the original dispatched action as unknown and never hangs or accepts late success', async t => { const f=setup(t),tab=await open(f),promise=f.m.act(f.session,tab.id,operation(f,tab),{approved:true}); const command=f.m.poll(f.windowId).commands[0],write=f.m.artifact.bind(f.m); f.m.artifact=(...args)=>{if(args[3]==='browsing-action-receipt')throw new Error('declared browser receipt storage failure');return write(...args);}; assert.equal(f.m.complete(f.windowId,{id:command.id,nonce:command.nonce,result:page('storage:2')}).accepted,false); await assert.rejects(promise,/receipt storage failure/);const original=f.store.core.browsing_action.get(command.id); assert.equal(original.status,'unknown');assert.equal(original.requiresVerification,true);assert.match(original.evidenceError,/storage failure/);assert.equal(f.m.active.size,0); assert.throws(()=>f.m.complete(f.windowId,{id:command.id,nonce:command.nonce,result:page('late')}),/迟到/); });