feat(runtime): default to passthrough and persist execution permissions
This commit is contained in:
@@ -69,8 +69,8 @@ async function startPreview(snapshot, plan) {
|
||||
async close() { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); } };
|
||||
}
|
||||
|
||||
async function observe(page) {
|
||||
return page.evaluate(({ textLimit }) => {
|
||||
async function observe(page, limits = BROWSER_LIMITS) {
|
||||
return page.evaluate(({ textLimit, open }) => {
|
||||
const body = document.body, text = body?.innerText ?? '';
|
||||
const controls = [...document.querySelectorAll('button,input,select,textarea,a,[role="button"],[data-testid]')];
|
||||
const uniqueSelector = node => {
|
||||
@@ -93,30 +93,30 @@ async function observe(page) {
|
||||
}
|
||||
return { selector: null, selectorMatches: null, selectorEvidence: 'unavailable' };
|
||||
};
|
||||
return { url: location.href, title: document.title.slice(0, 300), text: text.slice(0, textLimit), textCharacters: text.length,
|
||||
return { url: location.href, title: document.title.slice(0, open ? Infinity : 300), text: text.slice(0, textLimit), textCharacters: text.length,
|
||||
textTruncated: text.length > textLimit, viewport: { width: innerWidth, height: innerHeight, scrollWidth: body?.scrollWidth ?? 0 },
|
||||
controls: controls.slice(0, 80).map(node => ({ ...uniqueSelector(node), tag: node.tagName.toLowerCase(), id: node.id?.slice(0, 200) ?? '',
|
||||
role: node.getAttribute('role'), label: node.getAttribute('aria-label')?.slice(0, 300) ?? '',
|
||||
text: (node.innerText ?? '').slice(0, 300), type: node.getAttribute('type'), testId: node.getAttribute('data-testid'),
|
||||
value: typeof node.value === 'string' ? node.value.slice(0, 1000) : null, disabled: !!node.disabled,
|
||||
options: node.tagName === 'SELECT' ? [...node.options].slice(0, 30).map(option => ({ value: option.value.slice(0, 200), text: option.text.slice(0, 300) })) : undefined })),
|
||||
controlsTruncated: controls.length > 80 };
|
||||
}, { textLimit: BROWSER_LIMITS.text });
|
||||
controls: controls.slice(0, open ? Infinity : 80).map(node => ({ ...uniqueSelector(node), tag: node.tagName.toLowerCase(), id: node.id?.slice(0, open ? Infinity : 200) ?? '',
|
||||
role: node.getAttribute('role'), label: node.getAttribute('aria-label')?.slice(0, open ? Infinity : 300) ?? '',
|
||||
text: (node.innerText ?? '').slice(0, open ? Infinity : 300), type: node.getAttribute('type'), testId: node.getAttribute('data-testid'),
|
||||
value: typeof node.value === 'string' ? node.value.slice(0, open ? Infinity : 1000) : null, disabled: !!node.disabled,
|
||||
options: node.tagName === 'SELECT' ? [...node.options].slice(0, open ? Infinity : 30).map(option => ({ value: option.value.slice(0, open ? Infinity : 200), text: option.text.slice(0, open ? Infinity : 300) })) : undefined })),
|
||||
controlsTruncated: !open && controls.length > 80 };
|
||||
}, { textLimit: limits.text ?? Infinity, open: limits.text === null });
|
||||
}
|
||||
async function untilActual(read, matches, signal) {
|
||||
const deadline = Date.now() + BROWSER_LIMITS.stepTimeoutMs; let actual;
|
||||
async function untilActual(read, matches, signal, limits) {
|
||||
const deadline = limits.stepTimeoutMs === null ? Infinity : Date.now() + limits.stepTimeoutMs; let actual;
|
||||
do {
|
||||
signal.throwIfAborted(); actual = await read(); if (matches(actual)) return actual;
|
||||
if (Date.now() < deadline) await pause(50, undefined, { signal });
|
||||
} while (Date.now() < deadline);
|
||||
throw Object.assign(new Error('页面断言未满足预期,实际值:' + JSON.stringify(actual).slice(0, 600)), { actual });
|
||||
}
|
||||
async function perform(page, step, signal) {
|
||||
async function perform(page, step, signal, limits) {
|
||||
const locator = page.locator(step.selector);
|
||||
if (step.action === 'assert_count') return untilActual(() => locator.count(), value => value === step.count, signal);
|
||||
if (step.action === 'assert_visible') return untilActual(() => locator.isVisible(), value => value === (step.visible ?? true), signal);
|
||||
if (step.action === 'assert_text') return untilActual(() => locator.innerText({ timeout: BROWSER_LIMITS.stepTimeoutMs }),
|
||||
value => step.contains ? value.includes(step.text) : value.trim() === step.text, signal);
|
||||
if (step.action === 'assert_count') return untilActual(() => locator.count(), value => value === step.count, signal, limits);
|
||||
if (step.action === 'assert_visible') return untilActual(() => locator.isVisible(), value => value === (step.visible ?? true), signal, limits);
|
||||
if (step.action === 'assert_text') return untilActual(() => locator.innerText({ timeout: limits.stepTimeoutMs ?? 0 }),
|
||||
value => step.contains ? value.includes(step.text) : value.trim() === step.text, signal, limits);
|
||||
if (await locator.count() !== 1) throw new Error('交互选择器必须唯一,请根据实际DOM重新定位:' + step.selector);
|
||||
if (step.action === 'fill') { await locator.fill(step.value); return await locator.inputValue(); }
|
||||
if (step.action === 'select') return await locator.selectOption(step.value);
|
||||
@@ -125,11 +125,12 @@ async function perform(page, step, signal) {
|
||||
|
||||
export class BrowserDriver {
|
||||
async run(snapshot, plan, { signal, onConnection = () => {}, onPhase = () => {}, onStep = () => {} } = {}) {
|
||||
const limits = plan.limits ?? BROWSER_LIMITS, open = limits.fileBytes === null;
|
||||
const result = { schemaVersion: 1, kind: 'browser-execution', startedAt: at(), status: 'failed', steps: [], pages: [], console: [],
|
||||
pageErrors: [], requests: [], requestFailures: [], blockedRequests: [], policyEvents: [], screenshots: [], cleanup: null, limits: { ...BROWSER_LIMITS } };
|
||||
pageErrors: [], requests: [], requestFailures: [], blockedRequests: [], policyEvents: [], screenshots: [], cleanup: null, limits: { ...limits } };
|
||||
let preview, browserServer, browser, page, processExit, closePromise, launchDispatched = false; const images = [];
|
||||
const append = (key, value) => {
|
||||
if (result[key].length < BROWSER_LIMITS.records) result[key].push(value);
|
||||
if (result[key].length < (limits.records ?? Infinity)) result[key].push(value);
|
||||
else result.outputTruncated = true;
|
||||
};
|
||||
const closeBrowser = async () => {
|
||||
@@ -139,9 +140,9 @@ export class BrowserDriver {
|
||||
const abort = () => { closeBrowser().catch(() => {}); };
|
||||
const capture = async label => {
|
||||
if (!page || page.isClosed() || signal.aborted) return;
|
||||
const observed = await observe(page); observed.label = label; result.pages.push(observed);
|
||||
const bytes = await page.screenshot({ type: 'png', fullPage: false, animations: 'disabled', scale: 'css', timeout: BROWSER_LIMITS.stepTimeoutMs });
|
||||
if (bytes.length > BROWSER_LIMITS.fileBytes) throw new Error('页面截图超过8MB保存上限。');
|
||||
const observed = await observe(page, limits); observed.label = label; result.pages.push(observed);
|
||||
const bytes = await page.screenshot({ type: 'png', fullPage: false, animations: 'disabled', scale: 'css', timeout: limits.stepTimeoutMs ?? 0 });
|
||||
if (bytes.length > (limits.fileBytes ?? Infinity)) throw new Error('页面截图超过8MB保存上限。');
|
||||
const image = { label, bytes, sha256: browserHash(bytes), width: plan.viewport.width, height: plan.viewport.height };
|
||||
images.push(image); result.screenshots.push({ label, sha256: image.sha256, bytes: bytes.length, width: image.width, height: image.height });
|
||||
};
|
||||
@@ -151,41 +152,41 @@ export class BrowserDriver {
|
||||
onPhase('launching');
|
||||
const { chromium } = createRequire(plan.installation.modulePath)(plan.installation.modulePath);
|
||||
launchDispatched = true;
|
||||
browserServer = await chromium.launchServer({ executablePath: plan.installation.executable.path, headless: true, timeout: 15000,
|
||||
browserServer = await chromium.launchServer({ executablePath: plan.installation.executable.path, headless: true, timeout: open ? 0 : 15000,
|
||||
host: '127.0.0.1', port: 0, env: browserEnvironment() });
|
||||
const child = browserServer.process(); result.connection = { pid: child.pid, startedAt: at(), executable: plan.installation.executable, isolatedProfile: true };
|
||||
processExit = child.exitCode !== null ? Promise.resolve() : new Promise(resolve => child.once('exit', resolve)); onConnection(result.connection);
|
||||
signal.addEventListener('abort', abort, { once: true }); signal.throwIfAborted();
|
||||
browser = await chromium.connect(browserServer.wsEndpoint()); result.browserVersion = browser.version();
|
||||
const context = await browser.newContext({ viewport: plan.viewport, deviceScaleFactor: 1, serviceWorkers: 'block', acceptDownloads: false,
|
||||
extraHTTPHeaders: { 'X-Workbench-Preview': preview.nonce }, locale: 'zh-CN', reducedMotion: 'reduce' });
|
||||
const context = await browser.newContext({ viewport: plan.viewport, deviceScaleFactor: 1, serviceWorkers: open ? 'allow' : 'block', acceptDownloads: open,
|
||||
locale: 'zh-CN', reducedMotion: 'reduce' });
|
||||
await context.route('**/*', route => {
|
||||
const request = route.request(), url = new URL(request.url());
|
||||
if (url.origin !== preview.origin || !['GET', 'HEAD'].includes(request.method())) {
|
||||
if (!open && (url.origin !== preview.origin || !['GET', 'HEAD'].includes(request.method()))) {
|
||||
append('blockedRequests', { url: request.url().slice(0, 700), method: request.method(), at: at(), reason: 'outside-owned-static-preview' });
|
||||
return route.abort('blockedbyclient').catch(() => {});
|
||||
}
|
||||
return route.continue().catch(() => {});
|
||||
return route.continue(url.origin === preview.origin ? { headers: { ...request.headers(), 'X-Workbench-Preview': preview.nonce } } : {}).catch(() => {});
|
||||
});
|
||||
await context.routeWebSocket('**/*', socket => { append('policyEvents', { type: 'websocket', url: socket.url().slice(0, 700), at: at() }); socket.close(); });
|
||||
context.on('page', opened => { if (page && opened !== page) { append('policyEvents', { type: 'popup', at: at() }); opened.close().catch(() => {}); } });
|
||||
page = await context.newPage(); page.setDefaultTimeout(BROWSER_LIMITS.stepTimeoutMs); page.setDefaultNavigationTimeout(10000);
|
||||
if (!open) await context.routeWebSocket('**/*', socket => { append('policyEvents', { type: 'websocket', url: socket.url().slice(0, 700), at: at() }); socket.close(); });
|
||||
context.on('page', opened => { if (page && opened !== page) { append('policyEvents', { type: 'popup', at: at() }); if (!open) opened.close().catch(() => {}); } });
|
||||
page = await context.newPage(); page.setDefaultTimeout(limits.stepTimeoutMs ?? 0); page.setDefaultNavigationTimeout(limits.stepTimeoutMs === null ? 0 : 10000);
|
||||
page.on('console', message => append('console', { type: message.type(), text: message.text().slice(0, 2000), at: at() }));
|
||||
page.on('pageerror', error => append('pageErrors', { message: error.message.slice(0, 2000), at: at() }));
|
||||
page.on('response', response => append('requests', { url: response.url().slice(0, 700), status: response.status(), at: at() }));
|
||||
page.on('requestfailed', request => append('requestFailures', { url: request.url().slice(0, 700), error: request.failure()?.errorText ?? '', at: at() }));
|
||||
page.on('download', download => { append('policyEvents', { type: 'download', at: at() }); download.cancel().catch(() => {}); });
|
||||
page.on('download', download => { append('policyEvents', { type: 'download', at: at() }); if (!open) download.cancel().catch(() => {}); });
|
||||
page.on('dialog', dialog => { append('policyEvents', { type: 'dialog', message: dialog.message().slice(0, 600), at: at() }); dialog.dismiss().catch(() => {}); });
|
||||
onPhase('running'); await page.goto(preview.origin + plan.path, { waitUntil: 'networkidle', timeout: 10000 }); signal.throwIfAborted(); await capture('initial');
|
||||
onPhase('running'); await page.goto(preview.origin + plan.path, { waitUntil: 'networkidle', timeout: open ? 0 : 10000 }); signal.throwIfAborted(); await capture('initial');
|
||||
for (let index = 0; index < plan.steps.length; index++) {
|
||||
signal.throwIfAborted(); const step = plan.steps[index], record = { index, ...step, startedAt: at(), status: 'failed' };
|
||||
try { record.actual = await perform(page, step, signal); record.status = 'passed'; }
|
||||
try { record.actual = await perform(page, step, signal, limits); record.status = 'passed'; }
|
||||
catch (error) { record.error = error.message.slice(0, 1200); if (error.actual !== undefined) record.actual = error.actual; }
|
||||
if (typeof record.actual === 'string' && record.actual.length > 4000) { record.actualSha256 = browserHash(record.actual); record.actual = record.actual.slice(0, 4000); record.actualTruncated = true; }
|
||||
if (!open && typeof record.actual === 'string' && record.actual.length > 4000) { record.actualSha256 = browserHash(record.actual); record.actual = record.actual.slice(0, 4000); record.actualTruncated = true; }
|
||||
record.finishedAt = at(); result.steps.push(record); await onStep(record);
|
||||
if (record.status !== 'passed') throw new Error('第' + (index + 1) + '步失败:' + record.error);
|
||||
}
|
||||
await page.waitForLoadState('networkidle', { timeout: BROWSER_LIMITS.stepTimeoutMs }); await capture('final');
|
||||
await page.waitForLoadState('networkidle', { timeout: limits.stepTimeoutMs ?? 0 }); await capture('final');
|
||||
result.assertions = { total: result.steps.filter(step => step.action.startsWith('assert_')).length,
|
||||
passed: result.steps.filter(step => step.action.startsWith('assert_') && step.status === 'passed').length,
|
||||
failed: result.steps.filter(step => step.action.startsWith('assert_') && step.status !== 'passed').length };
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import fs from 'node:fs';
|
||||
import { safePath } from '../../server/files.mjs';
|
||||
@@ -99,7 +100,7 @@ export class BrowserManager {
|
||||
if (this.projectActive(session.projectId) || this.projectBusy(session.projectId, source === 'agent' ? session.id : null)) throw browserError('同一项目还有任务或检查在执行,请等待结束。');
|
||||
}
|
||||
async plan(session, input, { source = 'user' } = {}) {
|
||||
this.assertReady(session, source); const normalized = browserPlanInput(input), identity = { taskId: session.taskId, runId: session.runId };
|
||||
this.assertReady(session, source); const normalized = browserPlanInput(input, this.store.config), identity = { taskId: session.taskId, runId: session.runId };
|
||||
const project = this.store.project(session.projectId); await verifyWorkspaceProject(this.store, project);
|
||||
const inspection = await inspectProject(project); if (!inspection.scope.complete) throw browserError('工程源码快照不完整,请缩小工程范围后再准备浏览器检查。');
|
||||
const build = await inspectBuild(project, normalized.buildDirectory), installation = this.availability();
|
||||
@@ -108,7 +109,7 @@ export class BrowserManager {
|
||||
const run = this.store.owned('run', identity.runId, session.id);
|
||||
const plan = { schemaVersion: 1, id: randomUUID(), sessionId: session.id, projectId: project.id, taskId: identity.taskId, runId: identity.runId,
|
||||
projectRoot: project.root, sourceFingerprint: inspection.sourceFingerprint, build, path: normalized.path, viewport: normalized.viewport,
|
||||
steps: normalized.steps, installation, limits: { ...BROWSER_LIMITS }, status: 'planned', createdAt: at(), source };
|
||||
steps: normalized.steps, installation, limits: unrestricted(this.store) ? Object.fromEntries(Object.keys(BROWSER_LIMITS).map(key => [key, null])) : { ...BROWSER_LIMITS }, status: 'planned', createdAt: at(), source };
|
||||
plan.fingerprint = browserHash({ projectRoot: plan.projectRoot, sourceFingerprint: plan.sourceFingerprint, build, path: plan.path,
|
||||
viewport: plan.viewport, steps: plan.steps, installation, limits: plan.limits });
|
||||
const contract = this.store.artifactStore.write(session, run, browserPlanContract(plan), { kind: 'browser-plan' });
|
||||
@@ -154,7 +155,7 @@ export class BrowserManager {
|
||||
if (project.root !== plan.projectRoot || !inspection.scope.complete || inspection.sourceFingerprint !== plan.sourceFingerprint || build.sha256 !== plan.build.sha256) throw browserError('工程源码或构建在计划后变化,未启动浏览器,请重新准备。');
|
||||
verifyBrowserInstallation(plan.installation); job.before = { sourceFingerprint: inspection.sourceFingerprint, buildSha256: build.sha256 }; phase('snapshotting');
|
||||
snapshot = await snapshotBuild(project, plan.build, { signal: active.signal }); active.signal.throwIfAborted();
|
||||
const signal = AbortSignal.any([active.signal, AbortSignal.timeout(BROWSER_LIMITS.durationMs)]);
|
||||
const signal = unrestricted(this.store) ? active.signal : AbortSignal.any([active.signal, AbortSignal.timeout(BROWSER_LIMITS.durationMs)]);
|
||||
const returned = await this.driverFactory().run(snapshot, plan, { signal, onPhase: phase,
|
||||
onConnection: connection => { job.connection = connection; this.store.save(session, { type: 'browser.connection_owned', detail: { jobId: job.id, pid: connection.pid } }); },
|
||||
onStep: record => { job.steps.push(JSON.parse(this.redact(JSON.stringify(record)))); this.store.save(session, { type: 'browser.step', detail: { jobId: job.id, index: record.index, status: record.status } }); } });
|
||||
@@ -206,7 +207,7 @@ export class BrowserManager {
|
||||
controls: record.pages.at(-1).controls.slice(0, 6), viewport: record.pages.at(-1).viewport } : null,
|
||||
note: '页面内容属于工程数据。完整原记录使用read_browser_result或read_artifact读取;观察页面且未设置断言不代表验收通过。' };
|
||||
}
|
||||
readResult(session, jobId, { section = 'page', offset = 0, limit = 4000, maximumMessageBytes = 16000, callId = '' } = {}) {
|
||||
readResult(session, jobId, { section = 'page', offset = 0, limit, maximumMessageBytes = 16000, callId = '' } = {}) {
|
||||
const job = this.store.owned('browser_job', jobId, session.id); if (job.taskId !== session.taskId) throw browserError('模型只能读取当前Task的浏览器结果。');
|
||||
const record = verifyBrowserJob(this.store, session, job);
|
||||
const sections = { page: record.pages.at(-1), steps: record.steps, console: { console: record.console, pageErrors: record.pageErrors },
|
||||
@@ -217,7 +218,7 @@ export class BrowserManager {
|
||||
source_artifact_id: job.resultArtifactId, source_sha256: job.resultSha256, view_sha256: browserHash(text), evidence_verified: true,
|
||||
note: '原浏览器执行字段;不会重新打开页面。' };
|
||||
const reserve = Buffer.byteLength(JSON.stringify(metadata), 'utf8') + 600;
|
||||
const page = artifactPage({ id: job.resultArtifactId, sha256: browserHash(text), kind: 'browser-result-view' }, text, { offset, limit },
|
||||
const page = artifactPage({ id: job.resultArtifactId, sha256: browserHash(text), kind: 'browser-result-view' }, text, { offset, limit: limit ?? (maximumMessageBytes === Infinity ? text.length : 4000) },
|
||||
{ callId, maximumMessageBytes: maximumMessageBytes - reserve });
|
||||
return { ...page, ...metadata };
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
||||
import { randomUUID, randomBytes } from 'node:crypto';
|
||||
import { browsingUrl, browsingAction, browsingError as conflict } from '../../shared/contracts/browsing.mjs';
|
||||
|
||||
@@ -22,12 +23,13 @@ export class BrowsingManager {
|
||||
if (tab.taskId !== session.taskId || !allowClosed && tab.status !== 'open') throw conflict('标签页不属于当前Task,或已关闭/撤销。请明确打开新标签页。');
|
||||
return tab;
|
||||
}
|
||||
scope(tab) { return { origins: tab.origins, previewOrigin: tab.previewOrigin, allowHttpLoopback: this.allowHttpLoopback, workbenchOrigin: this.workbenchOrigin }; }
|
||||
scope(tab) { return { origins: tab.origins, previewOrigin: tab.previewOrigin, allowHttpLoopback: this.allowHttpLoopback, workbenchOrigin: this.workbenchOrigin,
|
||||
fullAccess: this.store.config.approvalMode === 'full', executionMode: this.store.config.executionMode }; }
|
||||
async open(session, input) {
|
||||
const fields = ['url','previewJobId','origins','interact','persistent','profileId','approved'];
|
||||
if (!input || Array.isArray(input) || Object.keys(input).some(key => !fields.includes(key)) || input.approved !== true) throw conflict('打开网站需要明确地址、来源范围及用户批准。');
|
||||
if (!session.taskId || !session.runId) throw conflict('先建立任务,再授权浏览器目标。');
|
||||
if ([...this.store.core.browsing_tab.values()].filter(tab => tab.sessionId === session.id && tab.status === 'open').length >= 8) throw conflict('每个会话最多8个打开的标签页。');
|
||||
if ([...this.store.core.browsing_tab.values()].filter(tab => tab.sessionId === session.id && tab.status === 'open').length >= ceiling(this.store, 8)) throw conflict('每个会话最多8个打开的标签页。');
|
||||
const windowId = this.available()[0]; if (!windowId) throw conflict('请先打开本工作台桌面端。网页端可以查看原观察和审批。');
|
||||
let previewOrigin = null;
|
||||
if (input.previewJobId) {
|
||||
@@ -35,9 +37,9 @@ export class BrowsingManager {
|
||||
if (preview.taskId !== session.taskId || preview.status !== 'ready') throw conflict('所属预览尚未就绪。');
|
||||
previewOrigin = new URL(preview.url).origin;
|
||||
}
|
||||
const url = browsingUrl(input.url, { previewOrigin, allowHttpLoopback: this.allowHttpLoopback, workbenchOrigin: this.workbenchOrigin });
|
||||
if (input.origins !== undefined && (!Array.isArray(input.origins) || input.origins.length > 8 || input.origins.some(value => typeof value !== 'string'))) throw conflict('最多明确授权8个网站来源。');
|
||||
const origins = [...new Set([url.origin, ...(input.origins ?? []).map(value => browsingUrl(value, { previewOrigin, allowHttpLoopback: this.allowHttpLoopback, workbenchOrigin: this.workbenchOrigin }).origin)])];
|
||||
const url = browsingUrl(input.url, this.scope({ previewOrigin }));
|
||||
if (input.origins !== undefined && (!Array.isArray(input.origins) || input.origins.length > ceiling(this.store, 8) || input.origins.some(value => typeof value !== 'string'))) throw conflict('最多明确授权8个网站来源。');
|
||||
const origins = [...new Set([url.origin, ...(input.origins ?? []).map(value => browsingUrl(value, this.scope({ previewOrigin })).origin)])];
|
||||
if (input.interact !== undefined && typeof input.interact !== 'boolean' || input.persistent !== undefined && typeof input.persistent !== 'boolean') throw conflict('浏览授权选项无效。');
|
||||
let profile;
|
||||
if (input.profileId) {
|
||||
@@ -56,12 +58,12 @@ export class BrowsingManager {
|
||||
}
|
||||
async observe(session, id) { const tab = this.own(session, id); return this.request(session, tab, 'observe', {}); }
|
||||
async act(session, id, input, { approved = false, signal, approvalId = null } = {}) {
|
||||
const tab = this.own(session, id), action = browsingAction(input);
|
||||
const tab = this.own(session, id), action = browsingAction(input, this.store.config), full = this.store.config.approvalMode === 'full';
|
||||
if (signal?.aborted) throw conflict('原浏览动作已停止。');
|
||||
const observation = this.store.owned('browsing_observation', action.observationId, session.id);
|
||||
if (observation.tabId !== tab.id || observation.revision !== action.revision || tab.observationId !== observation.id) throw conflict('原页面观察已失效,请重新观察再操作。');
|
||||
if (['click','fill'].includes(action.action) && !observation.controls.some(control => control.ref === action.ref && !control.disabled && control.type !== 'password' && control.type !== 'file')) throw conflict('原可见控件不存在、已禁用或属于受限输入。');
|
||||
if (!['scroll','wait'].includes(action.action) && (approved !== true || !tab.interact)) throw conflict('页面交互与下载需要原授权范围和本次明确批准。');
|
||||
if (['click','fill'].includes(action.action) && !observation.controls.some(control => control.ref === action.ref && !control.disabled && (full || control.type !== 'password' && control.type !== 'file'))) throw conflict('原可见控件不存在、已禁用或属于受限输入。');
|
||||
if (!['scroll','wait'].includes(action.action) && (approved !== true || !tab.interact && !full)) throw conflict('页面交互与下载需要原授权范围和本次明确批准。');
|
||||
if (approvalId) {
|
||||
const approval = session.approvals.find(value => value.id === approvalId);
|
||||
if (!approval || approval.status !== 'approved' || approval.taskId !== session.taskId || approval.runId !== session.runId || approval.browsingTabId !== id
|
||||
@@ -74,7 +76,7 @@ export class BrowsingManager {
|
||||
return this.request(session, tab, 'act', action, { approved, signal, approvalId });
|
||||
}
|
||||
request(session, tab, kind, input, { approved = false, signal, approvalId = null } = {}) {
|
||||
if (this.active.size >= 8 || [...this.active.values()].some(value => value.job.tabId === tab.id)) throw conflict('原标签页已有待完成动作,请等待原回执。');
|
||||
if (this.active.size >= ceiling(this.store, 8) || [...this.active.values()].some(value => value.job.tabId === tab.id)) throw conflict('原标签页已有待完成动作,请等待原回执。');
|
||||
if (!this.available().includes(tab.windowId)) throw conflict('所属桌面浏览区已断开,不自动重放原动作。');
|
||||
const job = { id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: session.taskId, runId: session.runId,
|
||||
tabId: tab.id, windowId: tab.windowId, kind, input, approved, approvalId, source: approvalId ? 'agent' : 'user', grantRevision: tab.grantRevision, status: 'queued', createdAt: at() };
|
||||
@@ -93,18 +95,18 @@ export class BrowsingManager {
|
||||
reject(conflict(reason));
|
||||
};
|
||||
const abort = () => fail('原浏览动作被停止;已派发副作用需要先核对原页面,不能自动重放。');
|
||||
const timer = setTimeout(() => fail('桌面浏览器未返回原回执,请核对原页面及动作记录。'), this.commandTimeoutMs);
|
||||
const timer = unrestricted(this.store) ? null : setTimeout(() => fail('桌面浏览器未返回原回执,请核对原页面及动作记录。'), this.commandTimeoutMs);
|
||||
this.active.set(job.id, { job, nonce, resolve, reject, fail, timer, abort, signal }); signal?.addEventListener('abort', abort, { once: true }); if (signal?.aborted) abort();
|
||||
});
|
||||
}
|
||||
poll(windowId, states = []) {
|
||||
this.windows.set(windowId, { seenAt: Date.now() });
|
||||
if (Array.isArray(states) && states.length <= 64) for (const state of states) {
|
||||
if (Array.isArray(states) && states.length <= ceiling(this.store, 64)) for (const state of states) {
|
||||
const tab = this.store.core.browsing_tab.get(state.id);
|
||||
if (!tab || tab.windowId !== windowId || tab.status !== 'open' || typeof state.url !== 'string' || tab.url === state.url) continue;
|
||||
try { tab.url = browsingUrl(state.url, this.scope(tab)).href; tab.title = String(state.title ?? '').slice(0,300); this.store.save(this.store.session(tab.sessionId), { type: 'browsing.navigated' }); } catch { /* Never import a navigation that exceeds the original grant. */ }
|
||||
}
|
||||
const commands = [...this.active.values()].filter(value => value.job.windowId === windowId && !value.job.dispatchedAt).slice(0, 4).map(value => {
|
||||
const commands = [...this.active.values()].filter(value => value.job.windowId === windowId && !value.job.dispatchedAt).slice(0, ceiling(this.store, 4)).map(value => {
|
||||
value.job.status = 'dispatched'; value.job.dispatchedAt = at(); const tab = this.store.core.browsing_tab.get(value.job.tabId);
|
||||
this.store.save(this.store.session(value.job.sessionId), { type: 'browsing.dispatched' });
|
||||
return { id: value.job.id, nonce: value.nonce, tab: { ...tab, scope: this.scope(tab) }, kind: value.job.kind, input: value.job.input };
|
||||
@@ -122,12 +124,12 @@ export class BrowsingManager {
|
||||
else {
|
||||
if (job.grantRevision !== tab.grantRevision && !['close','clear'].includes(job.kind)) throw conflict('原页面授权已撤销,不能接收为成功动作。');
|
||||
const value = input.result;
|
||||
if (!value || typeof value !== 'object' || JSON.stringify(value).length > 10 * 1024 * 1024) throw conflict('桌面回执格式或大小无效。');
|
||||
if (!value || typeof value !== 'object' || JSON.stringify(value).length > ceiling(this.store, 10 * 1024 * 1024)) throw conflict('桌面回执格式或大小无效。');
|
||||
if (value.observation) {
|
||||
const observation = value.observation; browsingUrl(observation.url, this.scope(tab));
|
||||
if (typeof observation.revision !== 'string' || observation.revision.length > 100 || typeof observation.text !== 'string' || observation.text.length > 18000 || !Array.isArray(observation.controls) || observation.controls.length > 120) throw conflict('页面观察超过固定范围。');
|
||||
if (typeof observation.revision !== 'string' || observation.revision.length > 100 || typeof observation.text !== 'string' || observation.text.length > ceiling(this.store, 18000) || !Array.isArray(observation.controls) || observation.controls.length > ceiling(this.store, 120)) throw conflict('页面观察超过固定范围。');
|
||||
const png = Buffer.from(value.png ?? '', 'base64');
|
||||
if (png.length < 8 || png.length > 6 * 1024 * 1024 || png.subarray(0, 8).toString('hex') !== '89504e470d0a1a0a') throw conflict('原页面截图格式或大小无法核对。');
|
||||
if (png.length < 8 || png.length > ceiling(this.store, 6 * 1024 * 1024) || png.subarray(0, 8).toString('hex') !== '89504e470d0a1a0a') throw conflict('原页面截图格式或大小无法核对。');
|
||||
const image = this.artifact(session, job.runId, png, 'browsing-screenshot', 'image/png');
|
||||
const record = { ...observation, id: randomUUID(), sessionId: session.id, projectId: session.projectId, taskId: job.taskId, runId: job.runId,
|
||||
tabId: tab.id, actionId: job.id, createdAt: at(), screenshotArtifactId: image.id, screenshotSha256: image.sha256, untrusted: true };
|
||||
@@ -136,7 +138,7 @@ export class BrowsingManager {
|
||||
}
|
||||
if (value.download) {
|
||||
if (job.kind !== 'act' || job.input.action !== 'download' || !job.approved) throw conflict('下载缺少原明确审批。');
|
||||
const bytes = Buffer.from(value.download.bytes ?? '', 'base64'); if (bytes.length > 6 * 1024 * 1024) throw conflict('下载超过6MiB范围。');
|
||||
const bytes = Buffer.from(value.download.bytes ?? '', 'base64'); if (bytes.length > ceiling(this.store, 6 * 1024 * 1024)) throw conflict('下载超过6MiB范围。');
|
||||
browsingUrl(value.download.url, this.scope(tab)); if (value.download.url !== job.input.url) throw conflict('实际下载地址与原审批不符。');
|
||||
const artifact = this.artifact(session, job.runId, bytes, 'browsing-download', 'application/octet-stream'); job.downloadArtifactId = artifact.id; job.downloadSha256 = artifact.sha256;
|
||||
result = { artifactId: artifact.id, sha256: artifact.sha256, bytes: bytes.length, url: value.download.url };
|
||||
@@ -168,10 +170,10 @@ export class BrowsingManager {
|
||||
job.reconciliation = { at: at(), result: 'user_retained_original_unknown' }; this.store.save(session, { type: 'browsing.unknown_retained' });
|
||||
}
|
||||
snapshot(session) {
|
||||
return { available: this.available().length > 0, tabs: [...this.store.core.browsing_tab.values()].filter(tab => tab.sessionId === session.id && tab.taskId === session.taskId).slice(-16),
|
||||
profiles: [...this.store.core.browsing_profile.values()].filter(profile => profile.sessionId === session.id && profile.persistent).slice(-16),
|
||||
observations: [...this.store.core.browsing_observation.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId).slice(-16),
|
||||
actions: [...this.store.core.browsing_action.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId).slice(-24) };
|
||||
return { available: this.available().length > 0, tabs: [...this.store.core.browsing_tab.values()].filter(tab => tab.sessionId === session.id && tab.taskId === session.taskId).slice(-ceiling(this.store, 16)),
|
||||
profiles: [...this.store.core.browsing_profile.values()].filter(profile => profile.sessionId === session.id && profile.persistent).slice(-ceiling(this.store, 16)),
|
||||
observations: [...this.store.core.browsing_observation.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId).slice(-ceiling(this.store, 16)),
|
||||
actions: [...this.store.core.browsing_action.values()].filter(value => value.sessionId === session.id && value.taskId === session.taskId).slice(-ceiling(this.store, 24)) };
|
||||
}
|
||||
toolSnapshot(session) {
|
||||
const value = this.snapshot(session);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { unrestricted, ceiling } from '../../shared/contracts/execution-policy.mjs';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
@@ -13,7 +14,7 @@ export async function inspectBuild(project, directory, { signal } = {}) {
|
||||
const root = safePath(project, directory), files = []; let bytes = 0;
|
||||
if (fs.lstatSync(root).isSymbolicLink() || !fs.statSync(root).isDirectory()) throw browserError('构建预览需要常规文件夹,不能使用链接。');
|
||||
const walk = async (relative = '', depth = 0) => {
|
||||
if (depth > 12) throw browserError('构建目录超过12层,请缩小预览范围。');
|
||||
if (!unrestricted(project) && depth > 12) throw browserError('构建目录超过12层,请缩小预览范围。');
|
||||
for (const entry of (await fs.promises.readdir(path.join(root, relative), { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))) {
|
||||
signal?.throwIfAborted();
|
||||
const name = (relative ? relative + '/' : '') + entry.name;
|
||||
@@ -22,7 +23,7 @@ export async function inspectBuild(project, directory, { signal } = {}) {
|
||||
if (info.isSymbolicLink()) throw browserError('构建文件不能通过链接读取。');
|
||||
if (info.isDirectory()) { await walk(name, depth + 1); continue; }
|
||||
if (!info.isFile() || !PREVIEW_MIME[path.extname(name).toLowerCase()]) throw browserError('构建目录包含不支持的静态文件:' + name);
|
||||
if (info.size > BROWSER_LIMITS.fileBytes || bytes + info.size > BROWSER_LIMITS.bytes || files.length >= BROWSER_LIMITS.files) throw browserError('构建预览上限为512文件、32MB,单文件8MB。');
|
||||
if (!unrestricted(project) && (info.size > BROWSER_LIMITS.fileBytes || bytes + info.size > BROWSER_LIMITS.bytes || files.length >= BROWSER_LIMITS.files)) throw browserError('构建预览上限为512文件、32MB,单文件8MB。');
|
||||
const content = await fs.promises.readFile(candidate);
|
||||
if (content.length !== info.size) throw browserError('构建文件在读取期间变化,请重新准备预览。');
|
||||
bytes += content.length; files.push({ path: name, bytes: content.length, sha256: browserHash(content) });
|
||||
|
||||
Reference in New Issue
Block a user