mirror of
https://github.com/aiclientproxy/proxycast.git
synced 2026-11-27 18:38:05 +08:00
2658 lines
86 KiB
JavaScript
2658 lines
86 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import process from "node:process";
|
||
import electronPath from "electron";
|
||
import {
|
||
createBrowserFirstOpenResponses,
|
||
runBrowserFirstOpenScenario,
|
||
} from "./browser-runtime-electron-gate-b-first-open.mjs";
|
||
import { _electron as electron } from "playwright";
|
||
import { startOpenAiCompatibleFixtureServer } from "../lib/openai-compatible-fixture-server.mjs";
|
||
import { resolveElectronAppServerRuntimeEnv } from "../lib/electron-app-server-assets.mjs";
|
||
import { resolveDevAppServerBinary } from "../lib/electron-dev-sidecar.mjs";
|
||
import { ensureElectronFixtureBuild } from "../lib/electron-fixture-build.mjs";
|
||
import { APP_SERVER_METHOD_SESSION_READ } from "../lib/chat-fixture/constants.mjs";
|
||
import { runBrowserCancelScenario } from "./browser-runtime-electron-gate-b-cancel.mjs";
|
||
import {
|
||
installBrowserApprovalPage,
|
||
runBrowserApprovalScenario,
|
||
} from "./browser-runtime-electron-gate-b-approval.mjs";
|
||
import { runBrowserUserControlScenario } from "./browser-runtime-electron-gate-b-user-control.mjs";
|
||
import { runBrowserDisconnectScenario } from "./browser-runtime-electron-gate-b-disconnect.mjs";
|
||
import { runBrowserDownloadScenario } from "./browser-runtime-electron-gate-b-download.mjs";
|
||
import {
|
||
prepareBrowserArtifactDownload,
|
||
runBrowserArtifactScenario,
|
||
} from "./browser-runtime-electron-gate-b-artifact.mjs";
|
||
import { runBrowserPermissionScenario } from "./browser-runtime-electron-gate-b-permission.mjs";
|
||
import { runBrowserWindowCloseScenario } from "./browser-runtime-electron-gate-b-window-close.mjs";
|
||
import { runBrowserProjectionGateA } from "./browser-runtime-gate-a.mjs";
|
||
import { runBrowserHistoryScenario } from "./browser-runtime-electron-gate-b-history.mjs";
|
||
import {
|
||
prepareBrowserHistoryManagementSeed,
|
||
runBrowserHistoryManagementScenario,
|
||
} from "./browser-runtime-electron-gate-b-history-management.mjs";
|
||
import { runBrowserDataScenario } from "./browser-runtime-electron-gate-b-data.mjs";
|
||
import { runBrowserSitePermissionsScenario } from "./browser-runtime-electron-gate-b-site-permissions.mjs";
|
||
import { runBrowserDeviceScenario } from "./browser-runtime-electron-gate-b-device.mjs";
|
||
import { runBrowserAddressScenario } from "./browser-runtime-electron-gate-b-address.mjs";
|
||
import { runBrowserNewTabAddressScenario } from "./browser-runtime-electron-gate-b-new-tab-address.mjs";
|
||
import { runBrowserSettingsNavigationScenario } from "./browser-runtime-electron-gate-b-settings-navigation.mjs";
|
||
import { runBrowserDeviceAnnotationScenario } from "./browser-runtime-electron-gate-b-device-annotation.mjs";
|
||
import { runBrowserHistoryAccessScenario } from "./browser-runtime-electron-gate-b-history-access.mjs";
|
||
import { createBrowserHistoryAccessResponses } from "./browser-history-access-provider.mjs";
|
||
import { createBrowserCdpResponses } from "./browser-cdp-provider.mjs";
|
||
import { runBrowserCdpScenario } from "./browser-runtime-electron-gate-b-cdp.mjs";
|
||
import { runBrowserDownloadHistoryScenario } from "./browser-runtime-electron-gate-b-download-history.mjs";
|
||
import { setBrowserFixtureLocale } from "./browser-runtime-electron-gate-b-locale.mjs";
|
||
import { runBrowserAnnotationScenario } from "./browser-runtime-electron-gate-b-annotation.mjs";
|
||
import { runBrowserAnnotationDesignScenario } from "./browser-runtime-electron-gate-b-annotation-design.mjs";
|
||
import {
|
||
assertBrowserAnnotationModel,
|
||
createBrowserAnnotationActiveResponses,
|
||
} from "./browser-runtime-electron-gate-b-annotation-provider.mjs";
|
||
import { startBrowserFixturePages } from "./browser-runtime-electron-gate-b-pages.mjs";
|
||
import {
|
||
approveBrowserBrowse,
|
||
captureBrowserActionApproval,
|
||
configureBrowserFixturePermissions,
|
||
joinBrowserBrowseEvidence,
|
||
prepareBrowserBrowseApproval,
|
||
} from "./browser-runtime-electron-gate-b-browse.mjs";
|
||
import {
|
||
createBrowserReceiptResponses,
|
||
runBrowserReceiptTurns,
|
||
} from "./browser-runtime-electron-gate-b-thread.mjs";
|
||
import {
|
||
bindGuiWorkspaceAndModelPreferences,
|
||
ensureDefaultWorkspace,
|
||
initializeAppServer,
|
||
invokeAppServerFromPage,
|
||
waitForRendererReady,
|
||
} from "../lib/chat-fixture/rpc.mjs";
|
||
import {
|
||
createFixtureSession,
|
||
navigateGuiToWorkspaceScopedAgent,
|
||
openFixtureSessionFromSidebar,
|
||
waitForGuiSessionVisible,
|
||
} from "../lib/chat-fixture/session.mjs";
|
||
import { sendPromptFromGui } from "../lib/chat-fixture/gui-actions.mjs";
|
||
import { createTempRuntimeEnv } from "../lib/chat-fixture/backend-file.mjs";
|
||
import {
|
||
mergeInvokeTraceEvidence,
|
||
startInvokeTraceEvidenceCollector,
|
||
} from "../lib/chat-fixture/invoke-trace.mjs";
|
||
import {
|
||
assert,
|
||
cleanupTempRoot,
|
||
sanitizeJson,
|
||
sanitizeText,
|
||
sleep,
|
||
} from "../lib/chat-fixture/utils.mjs";
|
||
|
||
const LOG_PREFIX = "[smoke:browser-runtime-electron-gate-b]";
|
||
const MODEL_NAME = "fixture-browser-runtime";
|
||
const PROVIDER_API_KEY = "fixture-browser-runtime-key";
|
||
const FINAL_MARKER = "BROWSER_RUNTIME_GATE_B_DONE";
|
||
const DEFAULT_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-summary.json",
|
||
);
|
||
const DEFAULT_CANCEL_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-cancel-summary.json",
|
||
);
|
||
const DEFAULT_WINDOW_CLOSE_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-window-close-summary.json",
|
||
);
|
||
const DEFAULT_DISCONNECT_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-disconnect-summary.json",
|
||
);
|
||
const DEFAULT_PERMISSION_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-permission-summary.json",
|
||
);
|
||
const DEFAULT_DOWNLOAD_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-download-summary.json",
|
||
);
|
||
const DEFAULT_ARTIFACT_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-artifact-summary.json",
|
||
);
|
||
const DEFAULT_APPROVAL_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-approval-summary.json",
|
||
);
|
||
const DEFAULT_USER_CONTROL_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-user-control-summary.json",
|
||
);
|
||
const DEFAULT_PROJECTION_OUTPUT = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-gate-a/browser-runtime-gate-a-summary.json",
|
||
);
|
||
const SCENARIOS = new Set([
|
||
"first-open",
|
||
"first-open-approval",
|
||
"projection",
|
||
"history",
|
||
"browser-data",
|
||
"site-permissions",
|
||
"device",
|
||
"device-annotation",
|
||
"address-input",
|
||
"new-tab-address",
|
||
"settings-navigation",
|
||
"history-management",
|
||
"history-access",
|
||
"cdp",
|
||
"download-history",
|
||
"annotation",
|
||
"annotation-text",
|
||
"annotation-form-text",
|
||
"annotation-frames",
|
||
"annotation-design",
|
||
"lifecycle",
|
||
"approval",
|
||
"browse-approval",
|
||
"user-control",
|
||
"cancel",
|
||
"window-close",
|
||
"disconnect",
|
||
"permission",
|
||
"download",
|
||
"artifact",
|
||
]);
|
||
const TERMINAL_STATUSES = new Set([
|
||
"completed",
|
||
"failed",
|
||
"cancelled",
|
||
"canceled",
|
||
"interrupted",
|
||
]);
|
||
const SUPPORTED_LOCALES = new Set([
|
||
"zh-CN",
|
||
"zh-TW",
|
||
"en-US",
|
||
"ja-JP",
|
||
"ko-KR",
|
||
]);
|
||
|
||
export function parseArgs(argv) {
|
||
const options = {
|
||
output: DEFAULT_OUTPUT,
|
||
timeoutMs: 180_000,
|
||
intervalMs: 500,
|
||
appUrl: process.env.VITE_DEV_SERVER_URL?.trim() || "",
|
||
keepTemp: false,
|
||
scenario: "lifecycle",
|
||
locale: "zh-CN",
|
||
screenshotDir: "",
|
||
targetUrl: "",
|
||
};
|
||
let outputExplicit = false;
|
||
for (let index = 0; index < argv.length; index += 1) {
|
||
const arg = argv[index];
|
||
const next = argv[index + 1];
|
||
if (arg === "-h" || arg === "--help") {
|
||
options.help = true;
|
||
return options;
|
||
}
|
||
if (arg === "--output" && next) {
|
||
options.output = path.resolve(next);
|
||
outputExplicit = true;
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--scenario" && next) {
|
||
options.scenario = String(next).trim();
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--timeout-ms" && next) {
|
||
options.timeoutMs = Number(next);
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--interval-ms" && next) {
|
||
options.intervalMs = Number(next);
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--app-url" && next) {
|
||
options.appUrl = String(next).trim();
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--target-url" && next) {
|
||
const target = new URL(next);
|
||
if (!["http:", "https:"].includes(target.protocol))
|
||
throw new Error("--target-url requires HTTP(S)");
|
||
options.targetUrl = target.href;
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--keep-temp") {
|
||
options.keepTemp = true;
|
||
continue;
|
||
}
|
||
if (arg === "--locale" && next) {
|
||
options.locale = String(next).trim();
|
||
index += 1;
|
||
continue;
|
||
}
|
||
if (arg === "--screenshot-dir" && next) {
|
||
options.screenshotDir = path.resolve(next);
|
||
index += 1;
|
||
continue;
|
||
}
|
||
throw new Error(`未知参数: ${arg}`);
|
||
}
|
||
if (!Number.isFinite(options.timeoutMs) || options.timeoutMs < 30_000) {
|
||
throw new Error("--timeout-ms 必须 >= 30000");
|
||
}
|
||
if (!Number.isFinite(options.intervalMs) || options.intervalMs < 100) {
|
||
throw new Error("--interval-ms 必须 >= 100");
|
||
}
|
||
if (!SCENARIOS.has(options.scenario)) {
|
||
throw new Error(
|
||
`--scenario 不支持: ${options.scenario || "<empty>"};请查看 --help`,
|
||
);
|
||
}
|
||
if (!SUPPORTED_LOCALES.has(options.locale)) {
|
||
throw new Error(
|
||
`--locale 必须是 ${[...SUPPORTED_LOCALES].join(", ")},收到: ${options.locale || "<empty>"}`,
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "cancel") {
|
||
options.output = DEFAULT_CANCEL_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "window-close") {
|
||
options.output = DEFAULT_WINDOW_CLOSE_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "disconnect") {
|
||
options.output = DEFAULT_DISCONNECT_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "permission") {
|
||
options.output = DEFAULT_PERMISSION_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "download") {
|
||
options.output = DEFAULT_DOWNLOAD_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "artifact") {
|
||
options.output = DEFAULT_ARTIFACT_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "approval") {
|
||
options.output = DEFAULT_APPROVAL_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "browse-approval") {
|
||
options.output = DEFAULT_APPROVAL_OUTPUT.replace(
|
||
"approval-summary",
|
||
"browse-approval-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "user-control") {
|
||
options.output = DEFAULT_USER_CONTROL_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "projection") {
|
||
options.output = DEFAULT_PROJECTION_OUTPUT;
|
||
}
|
||
if (!outputExplicit && options.scenario === "history") {
|
||
options.output = DEFAULT_OUTPUT.replace("-summary", "-history-summary");
|
||
}
|
||
if (!outputExplicit && options.scenario === "browser-data") {
|
||
options.output = path.resolve(
|
||
".lime/qc/gui-evidence/browser-runtime-electron-gate-b/browser-runtime-electron-gate-b-data-summary.json",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "history-access") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-history-access-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "cdp") {
|
||
options.output = DEFAULT_OUTPUT.replace("-summary", "-cdp-summary");
|
||
}
|
||
if (!outputExplicit && options.scenario === "site-permissions") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-site-permissions-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "device") {
|
||
options.output = DEFAULT_OUTPUT.replace("-summary", "-device-summary");
|
||
}
|
||
if (!outputExplicit && options.scenario === "new-tab-address") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-new-tab-address-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "settings-navigation") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-settings-navigation-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "history-management") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-history-management-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "address-input") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-address-input-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "device-annotation") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-device-annotation-summary",
|
||
);
|
||
}
|
||
if (!outputExplicit && options.scenario === "download-history") {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
"-download-history-summary",
|
||
);
|
||
}
|
||
if (
|
||
!outputExplicit &&
|
||
[
|
||
"annotation",
|
||
"annotation-text",
|
||
"annotation-form-text",
|
||
"annotation-frames",
|
||
"annotation-design",
|
||
].includes(options.scenario)
|
||
) {
|
||
options.output = DEFAULT_OUTPUT.replace(
|
||
"-summary",
|
||
`-${options.scenario}-summary`,
|
||
);
|
||
}
|
||
return options;
|
||
}
|
||
|
||
async function captureBrowserScreenshot(page, options, state) {
|
||
if (!options.screenshotDir) return null;
|
||
fs.mkdirSync(options.screenshotDir, { recursive: true });
|
||
const filePath = path.join(
|
||
options.screenshotDir,
|
||
`${options.locale}-${options.scenario}-${state}.png`,
|
||
);
|
||
await page.screenshot({ path: filePath, fullPage: true });
|
||
return filePath;
|
||
}
|
||
|
||
export function extractBrowserState(value) {
|
||
const seen = new Set();
|
||
const visit = (candidate) => {
|
||
if (typeof candidate === "string") {
|
||
try {
|
||
return visit(JSON.parse(candidate));
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
if (!candidate || typeof candidate !== "object" || seen.has(candidate)) {
|
||
return null;
|
||
}
|
||
seen.add(candidate);
|
||
if (
|
||
typeof candidate.tabId === "string" &&
|
||
typeof candidate.browserSessionId === "string" &&
|
||
typeof candidate.url === "string"
|
||
) {
|
||
return candidate;
|
||
}
|
||
if (Array.isArray(candidate)) {
|
||
for (const item of candidate) {
|
||
const found = visit(item);
|
||
if (found) return found;
|
||
}
|
||
return null;
|
||
}
|
||
for (const item of Object.values(candidate)) {
|
||
const found = visit(item);
|
||
if (found) return found;
|
||
}
|
||
return null;
|
||
};
|
||
return visit(value);
|
||
}
|
||
|
||
export function extractBrowserObservation(value) {
|
||
const seen = new Set();
|
||
const visit = (candidate) => {
|
||
if (typeof candidate === "string") {
|
||
try {
|
||
return visit(JSON.parse(candidate));
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
if (!candidate || typeof candidate !== "object" || seen.has(candidate)) {
|
||
return null;
|
||
}
|
||
seen.add(candidate);
|
||
if (
|
||
typeof candidate.snapshotId === "string" &&
|
||
Number.isInteger(candidate.pageRevision)
|
||
) {
|
||
return candidate;
|
||
}
|
||
for (const item of Array.isArray(candidate)
|
||
? candidate
|
||
: Object.values(candidate)) {
|
||
const found = visit(item);
|
||
if (found) return found;
|
||
}
|
||
return null;
|
||
};
|
||
return visit(value);
|
||
}
|
||
|
||
function toolResultFromRequest(body) {
|
||
const messages = Array.isArray(body?.messages) ? body.messages : [];
|
||
const toolMessage = [...messages]
|
||
.reverse()
|
||
.find((message) => message?.role === "tool");
|
||
if (!toolMessage) return null;
|
||
const content = toolMessage.content;
|
||
if (typeof content !== "string") return content;
|
||
try {
|
||
return JSON.parse(content);
|
||
} catch {
|
||
return content;
|
||
}
|
||
}
|
||
|
||
async function createBrowserProviderFixture({
|
||
firstOpenScenario = false,
|
||
cdpScenario = false,
|
||
historyAccessScenario = false,
|
||
annotationScenario = false,
|
||
approvalScenario = false,
|
||
browseApprovalScenario = false,
|
||
connectionDiagnostics = false,
|
||
userControlScenario = false,
|
||
artifactScenario = false,
|
||
} = {}) {
|
||
let releaseUserNavigation;
|
||
let releaseFinalResponse;
|
||
const userNavigationCompleted = new Promise((resolve) => {
|
||
releaseUserNavigation = resolve;
|
||
});
|
||
const finalResponseAllowed = new Promise((resolve) => {
|
||
releaseFinalResponse = resolve;
|
||
});
|
||
const scenario = {
|
||
initial: null,
|
||
approvedMutation: null,
|
||
declinedMutationFailure: null,
|
||
secondObservation: null,
|
||
latestAfterUserNavigation: null,
|
||
recovered: null,
|
||
staleMutationFailure: null,
|
||
userControlFailure: null,
|
||
artifact: null,
|
||
uploadTarget: null,
|
||
copy: null,
|
||
writeClipboard: null,
|
||
readClipboard: null,
|
||
open: null,
|
||
reveal: null,
|
||
upload: null,
|
||
releaseUserNavigation: () => releaseUserNavigation?.(),
|
||
releaseFinalResponse: () => releaseFinalResponse?.(),
|
||
releaseAll: () => {
|
||
releaseUserNavigation?.();
|
||
releaseFinalResponse?.();
|
||
scenario.annotation?.releaseAll();
|
||
scenario.receiptTurns?.forEach((turn) => turn.release());
|
||
},
|
||
};
|
||
const scriptedResponses = firstOpenScenario
|
||
? createBrowserFirstOpenResponses(scenario, {
|
||
extractBrowserState,
|
||
toolResultFromRequest,
|
||
})
|
||
: cdpScenario
|
||
? createBrowserCdpResponses(scenario, {
|
||
extractBrowserState,
|
||
toolResultFromRequest,
|
||
})
|
||
: historyAccessScenario
|
||
? createBrowserHistoryAccessResponses(scenario, toolResultFromRequest)
|
||
: annotationScenario
|
||
? createBrowserAnnotationActiveResponses(scenario, {
|
||
extractBrowserState,
|
||
extractBrowserObservation,
|
||
toolResultFromRequest,
|
||
})
|
||
: approvalScenario
|
||
? createBrowserApprovalResponses(scenario, {
|
||
userControl: userControlScenario,
|
||
})
|
||
: artifactScenario
|
||
? createBrowserArtifactResponses(scenario)
|
||
: createBrowserLifecycleResponses(
|
||
scenario,
|
||
userNavigationCompleted,
|
||
finalResponseAllowed,
|
||
);
|
||
if (browseApprovalScenario)
|
||
scriptedResponses.push(
|
||
...createBrowserReceiptResponses(scenario, {
|
||
extractBrowserState,
|
||
extractBrowserObservation,
|
||
toolResultFromRequest,
|
||
}),
|
||
);
|
||
const server = await startOpenAiCompatibleFixtureServer({
|
||
model: MODEL_NAME,
|
||
apiKey: PROVIDER_API_KEY,
|
||
modelRuntimeFeatures: ["streaming", "tool_calling", "custom_tools"],
|
||
...(annotationScenario
|
||
? {
|
||
modelInputModalities: ["text", "image"],
|
||
maxRequestBodyBytes: 16 * 1024 * 1024,
|
||
}
|
||
: {}),
|
||
connectionDiagnostics,
|
||
scriptedResponses,
|
||
});
|
||
return Object.assign(server, { scenario });
|
||
}
|
||
|
||
function createBrowserLifecycleResponses(
|
||
scenario,
|
||
userNavigationCompleted,
|
||
finalResponseAllowed,
|
||
) {
|
||
let initialObserveRetries = 0;
|
||
let initialTabId = null;
|
||
const finishRecoveredObservation = (body) => {
|
||
const result = toolResultFromRequest(body);
|
||
const observed = extractBrowserState(result);
|
||
const observation = extractBrowserObservation(result);
|
||
if (!observed || !observation?.snapshotId) {
|
||
throw new Error("重新 observe 未返回 Browser state 与 snapshotId");
|
||
}
|
||
scenario.recovered = { observation, state: observed };
|
||
return finalResponseAllowed.then(() => ({
|
||
type: "text",
|
||
content: `${FINAL_MARKER}:${JSON.stringify({
|
||
activeTurnId: observed.activeTurnId,
|
||
browserSessionId: observed.browserSessionId,
|
||
initialPageRevision: scenario.initial?.observation?.pageRevision,
|
||
ownerWebContentsId: observed.ownerWebContentsId,
|
||
pageRevision: observed.pageRevision,
|
||
recoveredPageRevision: observation.pageRevision,
|
||
staleMutationRejected:
|
||
scenario.staleMutationFailure === "stale_snapshot_rejected",
|
||
tabId: observed.tabId,
|
||
threadId: observed.threadId,
|
||
title: observed.title,
|
||
url: observed.url,
|
||
viewId: observed.viewId,
|
||
webContentsId: observed.webContentsId,
|
||
windowId: observed.windowId,
|
||
})}`,
|
||
}));
|
||
};
|
||
return [
|
||
{
|
||
type: "tool_call",
|
||
id: "call-browser-open-tabs",
|
||
name: "browser__openTabs",
|
||
arguments: {},
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) {
|
||
throw new Error("openTabs 未返回可用于 claimTab 的 Browser state");
|
||
}
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-claim-tab",
|
||
name: "browser__claimTab",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
title: state.title,
|
||
url: state.url,
|
||
pageRevision: state.pageRevision,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) {
|
||
throw new Error("claimTab 未返回可用于 observe 的 Browser state");
|
||
}
|
||
initialTabId = state.tabId;
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-observe",
|
||
name: "browser__observe",
|
||
arguments: { tabId: state.tabId },
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const result = parseDynamicToolResult(toolResultFromRequest(body));
|
||
const observed = extractBrowserState(result);
|
||
const observation = extractBrowserObservation(result);
|
||
if (!observed || typeof observed.webContentsId !== "number") {
|
||
const failure = JSON.stringify(result);
|
||
if (
|
||
initialObserveRetries < 2 &&
|
||
/page changed|stale|loading|being observed/i.test(failure)
|
||
) {
|
||
initialObserveRetries += 1;
|
||
return {
|
||
type: "tool_call",
|
||
id: `call-browser-observe-retry-${initialObserveRetries}`,
|
||
name: "browser__observe",
|
||
arguments: { tabId: initialTabId },
|
||
};
|
||
}
|
||
throw new Error("observe 未返回带 webContentsId 的同页 Browser state");
|
||
}
|
||
if (!observation?.snapshotId) {
|
||
throw new Error("observe 未返回可用于 stale-control 的 snapshotId");
|
||
}
|
||
scenario.initial = { observation, state: observed };
|
||
return userNavigationCompleted.then(() => ({
|
||
type: "tool_call",
|
||
id: "call-browser-open-tabs-after-user-navigation",
|
||
name: "browser__openTabs",
|
||
arguments: {},
|
||
}));
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) {
|
||
throw new Error("用户导航后的 openTabs 未返回 Browser state");
|
||
}
|
||
scenario.latestAfterUserNavigation = state;
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-reclaim-tab",
|
||
name: "browser__claimTab",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
title: state.title,
|
||
url: state.url,
|
||
pageRevision: state.pageRevision,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const value = toolResultFromRequest(body);
|
||
const state = extractBrowserState(value);
|
||
if (!state) {
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-open-tabs-after-stale-claim",
|
||
name: "browser__openTabs",
|
||
arguments: {},
|
||
};
|
||
}
|
||
if (!scenario.initial?.observation?.snapshotId) {
|
||
throw new Error("重新 claim 后缺少 stale snapshot mutation 输入");
|
||
}
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-stale-press",
|
||
name: "browser__press",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
snapshotId: scenario.initial.observation.snapshotId,
|
||
key: "ArrowDown",
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const value = toolResultFromRequest(body);
|
||
const state = extractBrowserState(value);
|
||
if (state) {
|
||
scenario.latestAfterUserNavigation = state;
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-reclaim-tab-retry",
|
||
name: "browser__claimTab",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
title: state.title,
|
||
url: state.url,
|
||
pageRevision: state.pageRevision,
|
||
},
|
||
};
|
||
}
|
||
const failure = String(value || "");
|
||
if (!failure.includes("Browser page snapshot is stale")) {
|
||
throw new Error(`旧 snapshot mutation 未按预期拒绝: ${failure}`);
|
||
}
|
||
scenario.staleMutationFailure = "stale_snapshot_rejected";
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-observe-after-user-navigation",
|
||
name: "browser__observe",
|
||
arguments: {
|
||
tabId: scenario.latestAfterUserNavigation?.tabId,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const value = toolResultFromRequest(body);
|
||
const observation = extractBrowserObservation(value);
|
||
if (observation?.snapshotId) {
|
||
return finishRecoveredObservation(body);
|
||
}
|
||
const state = extractBrowserState(value);
|
||
if (!state || !scenario.initial?.observation?.snapshotId) {
|
||
throw new Error("重新 claim 重试后缺少 stale snapshot mutation 输入");
|
||
}
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-stale-press-retry",
|
||
name: "browser__press",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
snapshotId: scenario.initial.observation.snapshotId,
|
||
key: "ArrowDown",
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const failure = String(toolResultFromRequest(body) || "");
|
||
if (!failure.includes("Browser page snapshot is stale")) {
|
||
throw new Error(`旧 snapshot mutation 未按预期拒绝: ${failure}`);
|
||
}
|
||
scenario.staleMutationFailure = "stale_snapshot_rejected";
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-observe-after-user-navigation",
|
||
name: "browser__observe",
|
||
arguments: {
|
||
tabId: scenario.latestAfterUserNavigation?.tabId,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => finishRecoveredObservation(body),
|
||
];
|
||
}
|
||
|
||
function parseDynamicToolResult(value) {
|
||
if (Array.isArray(value)) {
|
||
const text = value.find((item) => item?.type === "inputText")?.text;
|
||
if (typeof text === "string") {
|
||
try {
|
||
return JSON.parse(text);
|
||
} catch {
|
||
return text;
|
||
}
|
||
}
|
||
}
|
||
if (value && typeof value === "object" && typeof value.text === "string") {
|
||
try {
|
||
return JSON.parse(value.text);
|
||
} catch {
|
||
return value.text;
|
||
}
|
||
}
|
||
return value;
|
||
}
|
||
|
||
function createBrowserArtifactResponses(scenario) {
|
||
return [
|
||
{
|
||
type: "tool_call",
|
||
id: "call-browser-artifact-open-tabs",
|
||
name: "browser__openTabs",
|
||
arguments: {},
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) throw new Error("artifact openTabs 未返回 Browser state");
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-artifact-claim-tab",
|
||
name: "browser__claimTab",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
title: state.title,
|
||
url: state.url,
|
||
pageRevision: state.pageRevision,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) throw new Error("artifact claimTab 未返回 Browser state");
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-artifact-observe-initial",
|
||
name: "browser__observe",
|
||
arguments: { tabId: state.tabId },
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const result = toolResultFromRequest(body);
|
||
const state = extractBrowserState(result);
|
||
const observation = extractBrowserObservation(result);
|
||
if (!state || !observation?.snapshotId) {
|
||
throw new Error("artifact 初次 observe 未返回完整 Browser state");
|
||
}
|
||
scenario.initial = { state, observation };
|
||
scenario.secondObservation = { state, observation };
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-artifact-copy-ref",
|
||
name: "browser__copyArtifactRef",
|
||
arguments: { artifactRef: scenario.artifact.artifactRef },
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const result = parseDynamicToolResult(toolResultFromRequest(body));
|
||
scenario.copy = result;
|
||
assertArtifactGrant(result, "artifact_ref_copy");
|
||
return {
|
||
type: "text",
|
||
content: `${FINAL_MARKER}:${JSON.stringify({
|
||
artifactRef: scenario.artifact.artifactRef,
|
||
copied: scenario.copy?.data?.copied === true,
|
||
})}`,
|
||
};
|
||
},
|
||
];
|
||
}
|
||
|
||
function assertArtifactGrant(result, actionKind) {
|
||
if (
|
||
result?.status !== "completed" ||
|
||
result?.data?.grantScope !== "turn" ||
|
||
result?.data?.evidence?.actionKind !== actionKind
|
||
) {
|
||
throw new Error(
|
||
`artifact ${actionKind} 未返回 turn-scoped grant/evidence: ${JSON.stringify(result)}`,
|
||
);
|
||
}
|
||
}
|
||
|
||
function createBrowserApprovalResponses(
|
||
scenario,
|
||
{ userControl = false } = {},
|
||
) {
|
||
let initialObserveRetries = 0;
|
||
let initialTabId = null;
|
||
const dangerousNode = (observation) =>
|
||
observation?.nodes?.find(
|
||
(node) =>
|
||
Number.isInteger(node?.backendNodeId) &&
|
||
String(node?.name || "").includes("Delete account"),
|
||
);
|
||
return [
|
||
{
|
||
type: "tool_call",
|
||
id: "call-browser-open-tabs",
|
||
name: "browser__openTabs",
|
||
arguments: {},
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) {
|
||
throw new Error("openTabs 未返回可用于 claimTab 的 Browser state");
|
||
}
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-claim-tab",
|
||
name: "browser__claimTab",
|
||
arguments: {
|
||
tabId: state.tabId,
|
||
title: state.title,
|
||
url: state.url,
|
||
pageRevision: state.pageRevision,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const state = extractBrowserState(toolResultFromRequest(body));
|
||
if (!state) {
|
||
throw new Error("claimTab 未返回可用于 observe 的 Browser state");
|
||
}
|
||
initialTabId = state.tabId;
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-observe",
|
||
name: "browser__observe",
|
||
arguments: { tabId: state.tabId },
|
||
};
|
||
},
|
||
async ({ body }) => {
|
||
const result = toolResultFromRequest(body);
|
||
const observed = extractBrowserState(result);
|
||
const observation = extractBrowserObservation(result);
|
||
if (!observed || typeof observed.webContentsId !== "number") {
|
||
const failure = JSON.stringify(result);
|
||
if (
|
||
initialObserveRetries < 2 &&
|
||
/page changed|stale|loading|being observed/i.test(failure)
|
||
) {
|
||
initialObserveRetries += 1;
|
||
return {
|
||
type: "tool_call",
|
||
id: `call-browser-observe-retry-${initialObserveRetries}`,
|
||
name: "browser__observe",
|
||
arguments: { tabId: initialTabId },
|
||
};
|
||
}
|
||
throw new Error("observe 未返回带 webContentsId 的同页 Browser state");
|
||
}
|
||
if (!observation?.snapshotId) {
|
||
throw new Error("observe 未返回可用于 stale-control 的 snapshotId");
|
||
}
|
||
const target = dangerousNode(observation);
|
||
if (!target) {
|
||
throw new Error("observe 未返回 Delete account 危险按钮 node identity");
|
||
}
|
||
scenario.initial = { observation, state: observed, target };
|
||
await scenario.beforeSensitiveAction?.();
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-sensitive-click-approved",
|
||
name: "browser__click",
|
||
arguments: {
|
||
backendNodeId: target.backendNodeId,
|
||
snapshotId: observation.snapshotId,
|
||
tabId: observed.tabId,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const result = toolResultFromRequest(body);
|
||
if (userControl) {
|
||
const failure = JSON.stringify(result);
|
||
if (!/stale|invalid|approval token/i.test(failure)) {
|
||
throw new Error(`Browser 用户接管后旧审批未 fail closed: ${failure}`);
|
||
}
|
||
scenario.userControlFailure = failure;
|
||
return {
|
||
type: "text",
|
||
content: `${FINAL_MARKER}:${JSON.stringify({
|
||
staleApprovalRejected: true,
|
||
tabId: scenario.initial?.state?.tabId,
|
||
threadId: scenario.initial?.state?.threadId,
|
||
webContentsId: scenario.initial?.state?.webContentsId,
|
||
})}`,
|
||
};
|
||
}
|
||
const state = extractBrowserState(result);
|
||
if (!state) {
|
||
throw new Error("批准后的 Browser click 未返回 completed state");
|
||
}
|
||
scenario.approvedMutation = { result, state };
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-observe-before-decline",
|
||
name: "browser__observe",
|
||
arguments: { tabId: state.tabId },
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const result = toolResultFromRequest(body);
|
||
const observed = extractBrowserState(result);
|
||
const observation = extractBrowserObservation(result);
|
||
const target = dangerousNode(observation);
|
||
if (!observed || !observation?.snapshotId || !target) {
|
||
throw new Error("批准后重新 observe 未返回危险按钮 fresh snapshot");
|
||
}
|
||
scenario.secondObservation = { observation, state: observed, target };
|
||
return {
|
||
type: "tool_call",
|
||
id: "call-browser-sensitive-click-declined",
|
||
name: "browser__click",
|
||
arguments: {
|
||
backendNodeId: target.backendNodeId,
|
||
snapshotId: observation.snapshotId,
|
||
tabId: observed.tabId,
|
||
},
|
||
};
|
||
},
|
||
({ body }) => {
|
||
const failure = JSON.stringify(toolResultFromRequest(body));
|
||
if (!/拒绝|declin|approval/i.test(failure)) {
|
||
throw new Error(
|
||
`Browser decline 未返回 canonical approval failure: ${failure}`,
|
||
);
|
||
}
|
||
scenario.declinedMutationFailure = failure;
|
||
return {
|
||
type: "text",
|
||
content: `${FINAL_MARKER}:${JSON.stringify({
|
||
approvalResumed: true,
|
||
declineTerminal: true,
|
||
tabId: scenario.secondObservation?.state?.tabId,
|
||
threadId: scenario.secondObservation?.state?.threadId,
|
||
webContentsId: scenario.secondObservation?.state?.webContentsId,
|
||
})}`,
|
||
};
|
||
},
|
||
];
|
||
}
|
||
|
||
function logStage(stage) {
|
||
console.log(`${LOG_PREFIX} stage=${stage}`);
|
||
}
|
||
|
||
async function closeFixtureElectron(app) {
|
||
if (!app) {
|
||
return;
|
||
}
|
||
const pid = app.process()?.pid ?? null;
|
||
await Promise.race([app.close().catch(() => undefined), sleep(5_000)]);
|
||
if (!pid) {
|
||
return;
|
||
}
|
||
try {
|
||
process.kill(pid, 0);
|
||
process.kill(pid, "SIGTERM");
|
||
await sleep(500);
|
||
try {
|
||
process.kill(pid, 0);
|
||
process.kill(pid, "SIGKILL");
|
||
} catch {
|
||
// The fixture exited after SIGTERM.
|
||
}
|
||
} catch {
|
||
// The fixture exited during app.close().
|
||
}
|
||
}
|
||
|
||
async function waitForBrowserSurface(page, options) {
|
||
const startedAt = Date.now();
|
||
let last = null;
|
||
while (Date.now() - startedAt < options.timeoutMs) {
|
||
last = await page.evaluate(() => {
|
||
const panel = document.querySelector(
|
||
'[data-testid="right-surface-browser-panel"]',
|
||
);
|
||
const workspace = document.querySelector(
|
||
'[data-testid="browser-workspace"]',
|
||
);
|
||
const rect = workspace?.getBoundingClientRect();
|
||
return {
|
||
panelVisible: Boolean(
|
||
panel && rect && rect.width > 200 && rect.height > 200,
|
||
),
|
||
sessionId: workspace?.getAttribute("data-browser-session-id") || null,
|
||
tabId: workspace?.getAttribute("data-browser-tab-id") || null,
|
||
threadId: workspace?.getAttribute("data-browser-thread-id") || null,
|
||
webContentsId:
|
||
Number(workspace?.getAttribute("data-browser-web-contents-id")) ||
|
||
null,
|
||
activeSurface:
|
||
document
|
||
.querySelector('[data-testid="workspace-right-surface-host"]')
|
||
?.getAttribute("data-surface") || null,
|
||
};
|
||
});
|
||
if (
|
||
last?.panelVisible &&
|
||
last.sessionId &&
|
||
last.tabId &&
|
||
last.threadId &&
|
||
last.webContentsId
|
||
) {
|
||
return last;
|
||
}
|
||
await sleep(options.intervalMs);
|
||
}
|
||
throw new Error(
|
||
`Browser workspace 未完成挂载: ${JSON.stringify(sanitizeJson(last))}`,
|
||
);
|
||
}
|
||
|
||
async function readBrowserWorkspaceState(page) {
|
||
return await page.evaluate(() => {
|
||
const workspace = document.querySelector(
|
||
'[data-testid="browser-workspace"]',
|
||
);
|
||
const address = workspace?.querySelector("form input");
|
||
return {
|
||
activeTurnId:
|
||
workspace?.getAttribute("data-browser-active-turn-id") || null,
|
||
address:
|
||
workspace?.getAttribute("data-browser-url") ||
|
||
(address instanceof HTMLInputElement ? address.value || null : null),
|
||
controlOwner:
|
||
workspace?.getAttribute("data-browser-control-owner") || null,
|
||
pageRevision:
|
||
Number(workspace?.getAttribute("data-browser-page-revision")) || 0,
|
||
sessionId: workspace?.getAttribute("data-browser-session-id") || null,
|
||
tabId: workspace?.getAttribute("data-browser-tab-id") || null,
|
||
threadId: workspace?.getAttribute("data-browser-thread-id") || null,
|
||
viewId: workspace?.getAttribute("data-browser-view-id") || null,
|
||
webContentsId:
|
||
Number(workspace?.getAttribute("data-browser-web-contents-id")) || null,
|
||
};
|
||
});
|
||
}
|
||
|
||
async function waitForBrowserWorkspaceState(
|
||
page,
|
||
options,
|
||
predicate,
|
||
description,
|
||
) {
|
||
const startedAt = Date.now();
|
||
let last = null;
|
||
while (Date.now() - startedAt < options.timeoutMs) {
|
||
last = await readBrowserWorkspaceState(page);
|
||
if (predicate(last)) return last;
|
||
await sleep(options.intervalMs);
|
||
}
|
||
throw new Error(`${description}: ${JSON.stringify(sanitizeJson(last))}`);
|
||
}
|
||
|
||
async function waitForScenarioValue(options, read, description) {
|
||
const startedAt = Date.now();
|
||
while (Date.now() - startedAt < options.timeoutMs) {
|
||
const value = read();
|
||
if (value) return value;
|
||
await sleep(options.intervalMs);
|
||
}
|
||
throw new Error(`${description} 超时`);
|
||
}
|
||
|
||
function summarizeProviderRequests(requests) {
|
||
return requests.map((request, index) => ({
|
||
index: index + 1,
|
||
path: request?.path || null,
|
||
responseKind: request?.responseKind || null,
|
||
responseToolName: request?.responseToolName || null,
|
||
responseError: request?.responseError || null,
|
||
lastMessageRole: request?.body?.messages?.at(-1)?.role || null,
|
||
lastToolResultPreview:
|
||
request?.body?.messages?.at(-1)?.role === "tool"
|
||
? String(request.body.messages.at(-1).content || "").slice(0, 800)
|
||
: null,
|
||
lastMessageToolNames: Array.isArray(
|
||
request?.body?.messages?.at(-1)?.tool_calls,
|
||
)
|
||
? request.body.messages
|
||
.at(-1)
|
||
.tool_calls.map((call) => String(call?.function?.name || "").trim())
|
||
: [],
|
||
requestedToolNames: Array.isArray(request?.body?.tools)
|
||
? request.body.tools
|
||
.map((tool) =>
|
||
String(tool?.function?.name || tool?.name || "").trim(),
|
||
)
|
||
.filter(Boolean)
|
||
: [],
|
||
}));
|
||
}
|
||
|
||
async function readBrowserDebuggerState(app, webContentsId) {
|
||
return await app.evaluate(({ webContents }, targetId) => {
|
||
const target = webContents.fromId(targetId);
|
||
return {
|
||
attached: Boolean(
|
||
target && !target.isDestroyed() && target.debugger.isAttached(),
|
||
),
|
||
exists: Boolean(target && !target.isDestroyed()),
|
||
webContentsId: target?.id ?? null,
|
||
};
|
||
}, webContentsId);
|
||
}
|
||
|
||
async function waitForNativeBrowserCommit(app, options, webContentsId, url) {
|
||
const deadline = Date.now() + options.timeoutMs;
|
||
let last = null;
|
||
while (Date.now() < deadline) {
|
||
last = await app.evaluate(({ webContents }, id) => {
|
||
const target = webContents.fromId(id);
|
||
return target && !target.isDestroyed()
|
||
? { url: target.getURL(), loading: target.isLoading() }
|
||
: null;
|
||
}, webContentsId);
|
||
if (last?.url === url && !last.loading) return;
|
||
await sleep(options.intervalMs);
|
||
}
|
||
throw new Error(
|
||
`Native Browser navigation did not commit: ${JSON.stringify(last)}`,
|
||
);
|
||
}
|
||
|
||
async function destroyBrowserWebContents(app, webContentsId) {
|
||
return await app.evaluate(({ webContents }, targetId) => {
|
||
const target = webContents.fromId(targetId);
|
||
if (!target) {
|
||
return { exists: false };
|
||
}
|
||
if (!target.isDestroyed()) {
|
||
// 延迟到 evaluate 返回后终止 native renderer,避免破坏 Playwright 的主进程响应。
|
||
setImmediate(() => {
|
||
if (!target.isDestroyed()) {
|
||
target.forcefullyCrashRenderer();
|
||
}
|
||
});
|
||
}
|
||
return { exists: true, trigger: "render-process-gone" };
|
||
}, webContentsId);
|
||
}
|
||
|
||
async function waitForTerminalThread(page, options, threadId, requestLog) {
|
||
const startedAt = Date.now();
|
||
let last = null;
|
||
while (Date.now() - startedAt < options.timeoutMs) {
|
||
const read = await invokeAppServerFromPage(
|
||
page,
|
||
APP_SERVER_METHOD_SESSION_READ,
|
||
{ threadId, includeTurns: true },
|
||
requestLog,
|
||
);
|
||
const turns = Array.isArray(read.result?.thread?.turns)
|
||
? read.result.thread.turns
|
||
: Array.isArray(read.result?.turns)
|
||
? read.result.turns
|
||
: [];
|
||
last = turns.at(-1) || null;
|
||
const status = String(last?.status || last?.state || "").toLowerCase();
|
||
if (TERMINAL_STATUSES.has(status)) return { turn: last, read: read.result };
|
||
await sleep(options.intervalMs);
|
||
}
|
||
throw new Error(
|
||
`Browser Gate B 回合未结束: ${JSON.stringify(sanitizeJson(last))}`,
|
||
);
|
||
}
|
||
|
||
async function navigateVisibleBrowserTab(page, options, tabId, targetUrl) {
|
||
const address = page
|
||
.locator('[data-testid="browser-workspace"] form input')
|
||
.first();
|
||
assert((await address.count()) === 1, "Browser workspace 地址栏不可用");
|
||
await address.fill(targetUrl);
|
||
await address.press("Enter");
|
||
|
||
const startedAt = Date.now();
|
||
let lastTrace = [];
|
||
while (Date.now() - startedAt < options.timeoutMs) {
|
||
lastTrace = await page.evaluate(() => {
|
||
try {
|
||
return JSON.parse(
|
||
window.localStorage.getItem("lime_invoke_trace_buffer_v1") || "[]",
|
||
);
|
||
} catch {
|
||
return [];
|
||
}
|
||
});
|
||
const navigation = summarizeInvokeTrace(lastTrace).find(
|
||
(entry) =>
|
||
entry.command === "browser_tab_navigate" &&
|
||
entry.status === "success" &&
|
||
entry.args?.tabId === tabId &&
|
||
entry.args?.url === targetUrl,
|
||
);
|
||
if (navigation) return { navigation, trace: lastTrace };
|
||
await sleep(options.intervalMs);
|
||
}
|
||
throw new Error(
|
||
`用户导航未命中同一 Browser tab: ${JSON.stringify(
|
||
sanitizeJson(summarizeInvokeTrace(lastTrace)),
|
||
)}`,
|
||
);
|
||
}
|
||
|
||
function summarizeInvokeTrace(trace) {
|
||
return (Array.isArray(trace) ? trace : [])
|
||
.filter((entry) => entry?.transport === "electron-ipc")
|
||
.filter((entry) =>
|
||
["browser_tab_mount", "browser_tab_navigate"].includes(entry?.command),
|
||
)
|
||
.map((entry) => ({
|
||
command: entry.command,
|
||
status: entry.status,
|
||
args: entry.args_preview || null,
|
||
}));
|
||
}
|
||
|
||
export function extractFinalBrowserState(value) {
|
||
const text = String(value || "");
|
||
const marker = `${FINAL_MARKER}:`;
|
||
const markerIndex = text.lastIndexOf(marker);
|
||
if (markerIndex < 0) return null;
|
||
const payload = text
|
||
.slice(markerIndex + marker.length)
|
||
.split("\n", 1)[0]
|
||
?.trim();
|
||
if (!payload) return null;
|
||
try {
|
||
const parsed = JSON.parse(payload);
|
||
return parsed && typeof parsed === "object" ? parsed : null;
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
export function isReclaimedBrowserWorkspaceState({
|
||
activeTurnId,
|
||
initialWebContentsId,
|
||
recovered,
|
||
state,
|
||
}) {
|
||
return (
|
||
state?.controlOwner === "agent" &&
|
||
state?.activeTurnId === activeTurnId &&
|
||
state?.webContentsId === initialWebContentsId &&
|
||
state?.pageRevision === recovered?.observation?.pageRevision
|
||
);
|
||
}
|
||
|
||
export function buildAssertions({
|
||
debuggerAfterTerminal,
|
||
debuggerBeforeTerminal,
|
||
destroyed,
|
||
gui,
|
||
initial,
|
||
latestAfterUserNavigation,
|
||
recovered,
|
||
released,
|
||
staleMutationFailure,
|
||
trace,
|
||
providerRequests,
|
||
identity,
|
||
turnId,
|
||
finalText,
|
||
}) {
|
||
const browserTrace = summarizeInvokeTrace(trace);
|
||
const mount = browserTrace.find(
|
||
(entry) =>
|
||
entry.command === "browser_tab_mount" && entry.status === "success",
|
||
);
|
||
const navigate = browserTrace.find(
|
||
(entry) =>
|
||
entry.command === "browser_tab_navigate" && entry.status === "success",
|
||
);
|
||
const providerBodies = (
|
||
Array.isArray(providerRequests) ? providerRequests : []
|
||
)
|
||
.filter((request) => request?.path === "/v1/chat/completions")
|
||
.map((request) => request.body);
|
||
const toolNames = providerBodies.flatMap((body) =>
|
||
Array.isArray(body?.tools)
|
||
? body.tools.map((tool) => tool?.function?.name || tool?.name || null)
|
||
: [],
|
||
);
|
||
const dynamicToolCallCount = providerBodies.filter(
|
||
(body) =>
|
||
Array.isArray(body?.messages) &&
|
||
body.messages.some((message) => message?.role === "tool"),
|
||
).length;
|
||
const observed = extractFinalBrowserState(finalText);
|
||
return {
|
||
electronBrowserMounted: Boolean(mount?.status === "success"),
|
||
browserSurfaceVisible:
|
||
gui?.panelVisible === true && gui?.activeSurface === "browser",
|
||
canonicalThreadBound: gui?.threadId === identity?.threadId,
|
||
sameTabAcrossMountAndNavigate:
|
||
Boolean(mount?.args?.tabId) &&
|
||
Boolean(navigate?.args?.tabId) &&
|
||
mount.args.tabId === navigate.args.tabId,
|
||
webContentsBound:
|
||
Number.isInteger(gui?.webContentsId) && gui.webContentsId > 0,
|
||
sameBrowserSessionAsAgent:
|
||
Boolean(gui?.sessionId) && gui.sessionId === observed?.browserSessionId,
|
||
sameTabAsAgent: Boolean(gui?.tabId) && gui.tabId === observed?.tabId,
|
||
sameThreadAsAgent:
|
||
Boolean(identity?.threadId) &&
|
||
identity.threadId === observed?.threadId &&
|
||
gui?.threadId === observed?.threadId,
|
||
sameTurnAsAgent: Boolean(turnId) && turnId === observed?.activeTurnId,
|
||
sameWebContentsAsAgent:
|
||
Number.isInteger(gui?.webContentsId) &&
|
||
gui.webContentsId === observed?.webContentsId,
|
||
nativeAgentRouteBound:
|
||
typeof observed?.viewId === "string" &&
|
||
observed.viewId.length > 0 &&
|
||
Number.isInteger(observed?.windowId) &&
|
||
observed.windowId > 0 &&
|
||
Number.isInteger(observed?.ownerWebContentsId) &&
|
||
observed.ownerWebContentsId > 0,
|
||
userNavigationRelinquishedAgentControl:
|
||
latestAfterUserNavigation?.controlOwner === "user" &&
|
||
latestAfterUserNavigation?.activeTurnId === null,
|
||
userNavigationInvalidatedSnapshot:
|
||
Number.isInteger(initial?.observation?.pageRevision) &&
|
||
Number.isInteger(latestAfterUserNavigation?.pageRevision) &&
|
||
latestAfterUserNavigation.pageRevision > initial.observation.pageRevision,
|
||
staleSnapshotMutationRejected:
|
||
staleMutationFailure === "stale_snapshot_rejected" &&
|
||
observed?.staleMutationRejected === true,
|
||
reclaimedSameTabAfterUserNavigation:
|
||
recovered?.state?.browserSessionId === gui?.sessionId &&
|
||
recovered?.state?.tabId === gui?.tabId &&
|
||
recovered?.state?.threadId === identity?.threadId &&
|
||
recovered?.state?.webContentsId === gui?.webContentsId &&
|
||
recovered?.state?.activeTurnId === turnId &&
|
||
recovered?.state?.controlOwner === "agent",
|
||
reobservedFreshSnapshot:
|
||
Boolean(initial?.observation?.snapshotId) &&
|
||
Boolean(recovered?.observation?.snapshotId) &&
|
||
initial.observation.snapshotId !== recovered.observation.snapshotId &&
|
||
recovered.observation.pageRevision === recovered?.state?.pageRevision &&
|
||
recovered.observation.pageRevision >=
|
||
latestAfterUserNavigation?.pageRevision,
|
||
debuggerAttachedDuringRecoveredObserve:
|
||
debuggerBeforeTerminal?.exists === true &&
|
||
debuggerBeforeTerminal?.attached === true &&
|
||
debuggerBeforeTerminal?.webContentsId === gui?.webContentsId,
|
||
terminalReleasedUserTab:
|
||
released?.controlOwner === "released" &&
|
||
released?.activeTurnId === null &&
|
||
released?.sessionId === gui?.sessionId &&
|
||
released?.tabId === gui?.tabId &&
|
||
released?.webContentsId === gui?.webContentsId,
|
||
terminalDetachedDebugger:
|
||
debuggerAfterTerminal?.exists === true &&
|
||
debuggerAfterTerminal?.attached === false &&
|
||
debuggerAfterTerminal?.webContentsId === gui?.webContentsId,
|
||
destroyedWebContentsClosesRoute:
|
||
destroyed?.tabId === null && destroyed?.webContentsId === null,
|
||
dynamicBrowserToolsAdvertised: toolNames.includes("browser__openTabs"),
|
||
dynamicBrowserToolReturned: dynamicToolCallCount >= 1,
|
||
dynamicBrowserRoundTripVisible: providerBodies.length >= 8,
|
||
finalAssistantVisible: String(finalText || "").includes(FINAL_MARKER),
|
||
hostResolvedNotificationHidden: !String(finalText || "").includes(
|
||
"serverRequest/resolved",
|
||
),
|
||
};
|
||
}
|
||
|
||
function printHelp() {
|
||
console.log(`
|
||
Browser Electron Gate B
|
||
|
||
用法:
|
||
node scripts/agent-runtime/browser-runtime-electron-gate-b.mjs [options]
|
||
|
||
选项:
|
||
--output <path> 证据 JSON 路径
|
||
--scenario <name> projection(Gate A)、history、browser-data、site-permissions、device(真实设备工具栏)、device-annotation(原生设备批注坐标)、address-input(地址编辑焦点与输入)、new-tab-address(新标签地址草稿生命周期)、settings-navigation(设置管理页导航)、history-management(日期/来源/批量历史管理)、history-access(后台 Agent 历史权限)、cdp(当前页面 CDP 执行/撤权)、download-history(真实 Electron 下载管理)、annotation、annotation-design(后台设计提交)、lifecycle(默认)、approval、browse-approval、user-control、cancel、window-close、disconnect、permission、download 或 artifact
|
||
--locale <locale> zh-CN、zh-TW、en-US、ja-JP 或 ko-KR
|
||
--screenshot-dir <dir> 采集 renderer/browser/agent/takeover/released/destroyed 截图
|
||
--app-url <url> 可选 renderer dev server
|
||
--timeout-ms <ms> 总超时,默认 180000
|
||
--interval-ms <ms> 轮询间隔,默认 500
|
||
--keep-temp 保留临时 fixture 目录
|
||
|
||
原生网页焦点验收需要前台窗口:
|
||
LIME_BROWSER_DESIGN_VISIBLE=1 显示 design host 场景的真实窗口;默认隐藏
|
||
`);
|
||
}
|
||
|
||
export async function run(options) {
|
||
const annotationScenario = [
|
||
"annotation",
|
||
"annotation-text",
|
||
"annotation-form-text",
|
||
"annotation-frames",
|
||
"annotation-design",
|
||
].includes(options.scenario);
|
||
ensureElectronFixtureBuild({
|
||
appUrl: options.appUrl,
|
||
logPrefix: LOG_PREFIX,
|
||
rootDir: process.cwd(),
|
||
});
|
||
fs.mkdirSync(path.dirname(options.output), { recursive: true });
|
||
const runtimeEnv = createTempRuntimeEnv();
|
||
runtimeEnv.writeFixtureConfig({ language: options.locale });
|
||
const providerFixture = await createBrowserProviderFixture({
|
||
firstOpenScenario: options.scenario.startsWith("first-open"),
|
||
cdpScenario: options.scenario === "cdp",
|
||
historyAccessScenario: options.scenario === "history-access",
|
||
annotationScenario,
|
||
browseApprovalScenario: options.scenario === "browse-approval",
|
||
approvalScenario: ["approval", "browse-approval", "user-control"].includes(
|
||
options.scenario,
|
||
),
|
||
userControlScenario: options.scenario === "user-control",
|
||
artifactScenario: options.scenario === "artifact",
|
||
connectionDiagnostics: [
|
||
"lifecycle",
|
||
"cancel",
|
||
"disconnect",
|
||
"permission",
|
||
"download",
|
||
"artifact",
|
||
].includes(options.scenario),
|
||
});
|
||
const appServerBinary = resolveDevAppServerBinary({
|
||
env: runtimeEnv.env,
|
||
repoRoot: process.cwd(),
|
||
});
|
||
const appServerEnv = resolveElectronAppServerRuntimeEnv({
|
||
env: { ...runtimeEnv.env, APP_SERVER_BIN: appServerBinary },
|
||
});
|
||
const requestLog = [];
|
||
const consoleErrors = [];
|
||
const pageErrors = [];
|
||
let disconnectMainDiagnosticCount = 0;
|
||
let app = null;
|
||
let invokeTraceCollector = null;
|
||
const fixturePages = await startBrowserFixturePages({
|
||
annotationFrames: options.scenario === "annotation-frames",
|
||
});
|
||
if (options.scenario === "cdp") {
|
||
providerFixture.scenario.cdp.websocketUrl = fixturePages.webSocketUrl;
|
||
providerFixture.scenario.cdp.webSocketMessages =
|
||
fixturePages.webSocketMessages;
|
||
}
|
||
try {
|
||
const historyManagementSeed =
|
||
options.scenario === "history-management"
|
||
? await prepareBrowserHistoryManagementSeed(
|
||
runtimeEnv.electronUserDataDir,
|
||
)
|
||
: null;
|
||
logStage("launch-electron");
|
||
app = await electron.launch({
|
||
executablePath: electronPath,
|
||
args: [
|
||
"--use-mock-keychain",
|
||
[
|
||
"annotation-design",
|
||
"history-access",
|
||
"cdp",
|
||
"site-permissions",
|
||
"device",
|
||
"device-annotation",
|
||
"address-input",
|
||
"new-tab-address",
|
||
"settings-navigation",
|
||
"history-management",
|
||
].includes(options.scenario)
|
||
? path.resolve("scripts/agent-runtime/browser-design-submit-host.mjs")
|
||
: ".",
|
||
],
|
||
cwd: process.cwd(),
|
||
env: {
|
||
...runtimeEnv.env,
|
||
...appServerEnv,
|
||
APP_SERVER_BACKEND_MODE: "runtime",
|
||
ELECTRON_E2E_USER_DATA_DIR: runtimeEnv.electronUserDataDir,
|
||
LIME_ELECTRON_E2E: "1",
|
||
LIME_ELECTRON_BRAND_DEV_APP: "0",
|
||
LIME_ELECTRON_CLEAR_RENDERER_CACHE: "1",
|
||
LIME_ELECTRON_DEV_HTTP_BRIDGE: "0",
|
||
...([
|
||
"annotation-design",
|
||
"history-access",
|
||
"cdp",
|
||
"site-permissions",
|
||
"device",
|
||
"device-annotation",
|
||
"address-input",
|
||
"new-tab-address",
|
||
"settings-navigation",
|
||
"history-management",
|
||
].includes(options.scenario)
|
||
? { LIME_BROWSER_DESIGN_ROOT: process.cwd() }
|
||
: {}),
|
||
...(options.appUrl ? { VITE_DEV_SERVER_URL: options.appUrl } : {}),
|
||
},
|
||
timeout: options.timeoutMs,
|
||
});
|
||
app.on("console", (message) => {
|
||
const messageText = sanitizeText(message.text());
|
||
if (message.type() === "error") consoleErrors.push(messageText);
|
||
if (
|
||
options.scenario === "disconnect" &&
|
||
messageText.includes("app-server") &&
|
||
disconnectMainDiagnosticCount < 40
|
||
) {
|
||
disconnectMainDiagnosticCount += 1;
|
||
console.log(
|
||
`${LOG_PREFIX} main-console type=${message.type()} text=${messageText}`,
|
||
);
|
||
}
|
||
});
|
||
const page = await app.firstWindow({ timeout: options.timeoutMs });
|
||
page.on("pageerror", (error) =>
|
||
pageErrors.push(sanitizeText(error.message)),
|
||
);
|
||
page.setDefaultTimeout(options.timeoutMs);
|
||
await (
|
||
await app.browserWindow(page)
|
||
).evaluate((window) => {
|
||
window.setContentSize(1440, 1000);
|
||
});
|
||
await captureBrowserScreenshot(page, options, "renderer-loading");
|
||
await waitForRendererReady(page, options, () => undefined);
|
||
await setBrowserFixtureLocale(page, options);
|
||
await captureBrowserScreenshot(page, options, "renderer-ready");
|
||
|
||
logStage("initialize-app-server");
|
||
await initializeAppServer(page, requestLog);
|
||
if (!annotationScenario)
|
||
await configureBrowserFixturePermissions(page, options, requestLog);
|
||
const workspace = await ensureDefaultWorkspace(page, requestLog);
|
||
const provider = await invokeAppServerFromPage(
|
||
page,
|
||
"modelProvider/create",
|
||
{
|
||
name: `Browser Gate B ${process.pid}`,
|
||
providerType: "openai",
|
||
apiHost: `${providerFixture.baseUrl}/v1`,
|
||
},
|
||
requestLog,
|
||
);
|
||
const providerId = String(provider.result?.provider?.id || "").trim();
|
||
assert(providerId, "Browser fixture provider 创建失败");
|
||
await invokeAppServerFromPage(
|
||
page,
|
||
"modelProvider/update",
|
||
{
|
||
providerId,
|
||
enabled: true,
|
||
sortOrder: 0,
|
||
models: [],
|
||
},
|
||
requestLog,
|
||
);
|
||
await invokeAppServerFromPage(
|
||
page,
|
||
"modelProviderKey/create",
|
||
{
|
||
providerId,
|
||
apiKey: PROVIDER_API_KEY,
|
||
alias: "browser-runtime-gate-b",
|
||
replaceExisting: true,
|
||
},
|
||
requestLog,
|
||
);
|
||
const fetched = await invokeAppServerFromPage(
|
||
page,
|
||
"modelProvider/fetchModels",
|
||
{ providerId },
|
||
requestLog,
|
||
);
|
||
assert(
|
||
Array.isArray(fetched.result?.models) &&
|
||
fetched.result.models.some((model) => model?.id === MODEL_NAME),
|
||
"Browser fixture provider 未返回可用模型",
|
||
);
|
||
if (annotationScenario)
|
||
await assertBrowserAnnotationModel(
|
||
page,
|
||
requestLog,
|
||
providerId,
|
||
MODEL_NAME,
|
||
);
|
||
await bindGuiWorkspaceAndModelPreferences(page, workspace.workspaceId, {
|
||
provider: providerId,
|
||
model: MODEL_NAME,
|
||
});
|
||
|
||
logStage("create-and-open-thread");
|
||
const created = await createFixtureSession(page, workspace, requestLog, {
|
||
provider: providerId,
|
||
model: MODEL_NAME,
|
||
});
|
||
const identity = created.identity;
|
||
const sessionOptions = {
|
||
...options,
|
||
sessionId: identity.sessionId,
|
||
threadId: identity.threadId,
|
||
};
|
||
await page.evaluate(
|
||
({ sessionId, workspaceId }) => {
|
||
window.dispatchEvent(
|
||
new CustomEvent("lime:agent-runtime-sessions-changed", {
|
||
detail: { reason: "external", sessionId, workspaceId },
|
||
}),
|
||
);
|
||
},
|
||
{ sessionId: identity.sessionId, workspaceId: workspace.workspaceId },
|
||
);
|
||
await navigateGuiToWorkspaceScopedAgent(
|
||
page,
|
||
sessionOptions,
|
||
workspace.workspaceId,
|
||
);
|
||
await waitForGuiSessionVisible(page, sessionOptions);
|
||
await openFixtureSessionFromSidebar(page, sessionOptions, requestLog);
|
||
|
||
logStage("request-and-mount-browser");
|
||
invokeTraceCollector = startInvokeTraceEvidenceCollector(page, {
|
||
intervalMs: options.intervalMs,
|
||
});
|
||
if (options.scenario.startsWith("first-open")) {
|
||
const evidence = await runBrowserFirstOpenScenario({
|
||
app,
|
||
page,
|
||
identity,
|
||
options,
|
||
providerFixture,
|
||
requestLog,
|
||
targetUrl: options.targetUrl || fixturePages.initialUrl,
|
||
readBrowserWorkspaceState,
|
||
waitForBrowserSurface,
|
||
waitForNativeBrowserCommit,
|
||
waitForScenarioValue,
|
||
waitForTerminalThread,
|
||
captureBrowserScreenshot,
|
||
readInvokeTrace: () => invokeTraceCollector.snapshot(),
|
||
});
|
||
evidence.diagnostics = { consoleErrors, pageErrors };
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(evidence, null, 2)}\n`,
|
||
);
|
||
assert(
|
||
evidence.status === "pass" &&
|
||
consoleErrors.length === 0 &&
|
||
pageErrors.length === 0,
|
||
"First-open renderer errors",
|
||
);
|
||
return evidence;
|
||
}
|
||
await invokeAppServerFromPage(
|
||
page,
|
||
"workspaceRightSurface/request",
|
||
{
|
||
workspaceId: workspace.workspaceId,
|
||
workspaceRoot: workspace.rootPath,
|
||
sessionId: identity.sessionId,
|
||
surfaceKind: "browser",
|
||
origin: "runtime",
|
||
priority: "foreground",
|
||
reason: "browser_runtime_gate_b",
|
||
candidateId: "browser-runtime-gate-b",
|
||
ttlMs: 120_000,
|
||
metadata: {
|
||
browser: {
|
||
launchUrl: fixturePages.initialUrl,
|
||
title: "Browser Runtime Gate B",
|
||
},
|
||
},
|
||
},
|
||
requestLog,
|
||
);
|
||
const guiBeforeTurn = {
|
||
...(await waitForBrowserSurface(page, options)),
|
||
rendererLocale: await page.evaluate(
|
||
() => document.documentElement.lang || null,
|
||
),
|
||
};
|
||
await waitForNativeBrowserCommit(
|
||
app,
|
||
options,
|
||
guiBeforeTurn.webContentsId,
|
||
fixturePages.initialUrl,
|
||
);
|
||
await captureBrowserScreenshot(page, options, "browser-loaded");
|
||
if (options.scenario === "projection") {
|
||
logStage("run-browser-projection-gate-a");
|
||
const gateAEvidence = await runBrowserProjectionGateA({
|
||
app,
|
||
guiBeforeTurn,
|
||
identity,
|
||
options,
|
||
page,
|
||
readBrowserWorkspaceState,
|
||
});
|
||
gateAEvidence.diagnostics = {
|
||
consoleErrors,
|
||
pageErrors,
|
||
};
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(gateAEvidence, null, 2)}\n`,
|
||
);
|
||
if (
|
||
gateAEvidence.failedAssertions.length > 0 ||
|
||
consoleErrors.length > 0 ||
|
||
pageErrors.length > 0
|
||
) {
|
||
throw new Error(
|
||
`Browser Gate A 断言失败: ${[
|
||
...gateAEvidence.failedAssertions,
|
||
...(consoleErrors.length > 0 ? ["consoleErrors"] : []),
|
||
...(pageErrors.length > 0 ? ["pageErrors"] : []),
|
||
].join(", ")}`,
|
||
);
|
||
}
|
||
return gateAEvidence;
|
||
}
|
||
if (
|
||
options.scenario === "history" ||
|
||
options.scenario === "browser-data" ||
|
||
options.scenario === "site-permissions" ||
|
||
options.scenario === "device" ||
|
||
options.scenario === "device-annotation" ||
|
||
options.scenario === "address-input" ||
|
||
options.scenario === "new-tab-address" ||
|
||
options.scenario === "settings-navigation" ||
|
||
options.scenario === "history-management" ||
|
||
options.scenario === "history-access" ||
|
||
options.scenario === "cdp" ||
|
||
options.scenario === "download-history"
|
||
) {
|
||
logStage(`run-browser-${options.scenario}`);
|
||
const historyEvidence = await (
|
||
options.scenario === "history-management"
|
||
? runBrowserHistoryManagementScenario
|
||
: options.scenario === "settings-navigation"
|
||
? runBrowserSettingsNavigationScenario
|
||
: options.scenario === "new-tab-address"
|
||
? runBrowserNewTabAddressScenario
|
||
: options.scenario === "address-input"
|
||
? runBrowserAddressScenario
|
||
: options.scenario === "device-annotation"
|
||
? runBrowserDeviceAnnotationScenario
|
||
: options.scenario === "device"
|
||
? runBrowserDeviceScenario
|
||
: options.scenario === "site-permissions"
|
||
? runBrowserSitePermissionsScenario
|
||
: options.scenario === "browser-data"
|
||
? runBrowserDataScenario
|
||
: options.scenario === "history-access"
|
||
? runBrowserHistoryAccessScenario
|
||
: options.scenario === "cdp"
|
||
? runBrowserCdpScenario
|
||
: options.scenario === "history"
|
||
? runBrowserHistoryScenario
|
||
: runBrowserDownloadHistoryScenario
|
||
)({
|
||
app,
|
||
page,
|
||
options,
|
||
identity,
|
||
workspaceId: workspace.workspaceId,
|
||
guiBeforeTurn,
|
||
historyManagementSeed,
|
||
providerFixture,
|
||
waitForTerminalThread,
|
||
readBrowserWorkspaceState,
|
||
readBrowserDebuggerState,
|
||
readInvokeTrace: () => invokeTraceCollector.snapshot(),
|
||
requestLog,
|
||
});
|
||
historyEvidence.diagnostics.harness = { consoleErrors, pageErrors };
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(historyEvidence, null, 2)}\n`,
|
||
);
|
||
assert(
|
||
historyEvidence.failedAssertions.length === 0 &&
|
||
consoleErrors.length === 0 &&
|
||
pageErrors.length === 0,
|
||
`Browser ${options.scenario} 断言失败: ${historyEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
return historyEvidence;
|
||
}
|
||
if (annotationScenario) {
|
||
logStage("run-browser-annotation");
|
||
const runAnnotation =
|
||
options.scenario === "annotation-design"
|
||
? runBrowserAnnotationDesignScenario
|
||
: runBrowserAnnotationScenario;
|
||
const annotationEvidence = await runAnnotation({
|
||
app,
|
||
page,
|
||
options,
|
||
identity,
|
||
workspaceId: workspace.workspaceId,
|
||
guiBeforeTurn,
|
||
readBrowserWorkspaceState,
|
||
readInvokeTrace: () => invokeTraceCollector.snapshot(),
|
||
providerFixture,
|
||
providerId,
|
||
requestLog,
|
||
});
|
||
annotationEvidence.diagnostics.harness = { consoleErrors, pageErrors };
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(annotationEvidence, null, 2)}\n`,
|
||
);
|
||
assert(
|
||
annotationEvidence.failedAssertions.length === 0 &&
|
||
consoleErrors.length === 0 &&
|
||
pageErrors.length === 0,
|
||
`Browser annotation 断言失败: ${annotationEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
return annotationEvidence;
|
||
}
|
||
if (
|
||
["approval", "browse-approval", "user-control"].includes(options.scenario)
|
||
) {
|
||
await installBrowserApprovalPage(app, guiBeforeTurn.webContentsId);
|
||
}
|
||
if (options.scenario === "artifact") {
|
||
const artifactDownload = await prepareBrowserArtifactDownload({
|
||
app,
|
||
page,
|
||
options,
|
||
webContentsId: guiBeforeTurn.webContentsId,
|
||
});
|
||
providerFixture.scenario.artifactTrigger = artifactDownload.trigger;
|
||
providerFixture.scenario.artifact = artifactDownload.artifact;
|
||
}
|
||
|
||
const browseApproval =
|
||
options.scenario === "browse-approval"
|
||
? prepareBrowserBrowseApproval({
|
||
page,
|
||
providerFixture,
|
||
requestLog,
|
||
origin: new URL(fixturePages.initialUrl).origin,
|
||
})
|
||
: null;
|
||
logStage("run-browser-dynamic-tools");
|
||
await sendPromptFromGui(
|
||
page,
|
||
sessionOptions,
|
||
"请使用当前 Browser 打开标签并观察页面,然后返回结果。",
|
||
{ expectedSessionId: identity.sessionId },
|
||
);
|
||
if (browseApproval) {
|
||
logStage("approve-initial-browser-browse");
|
||
await approveBrowserBrowse({
|
||
app,
|
||
page,
|
||
options,
|
||
record: browseApproval,
|
||
initial: true,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
});
|
||
}
|
||
logStage("wait-initial-agent-observation");
|
||
let initial;
|
||
try {
|
||
initial = await waitForScenarioValue(
|
||
options,
|
||
() => providerFixture.scenario.initial,
|
||
"Agent 初次 observe",
|
||
);
|
||
} catch (error) {
|
||
const diagnostics = await page.evaluate(() => ({
|
||
browserWorkspace: (() => {
|
||
const workspace = document.querySelector(
|
||
'[data-testid="browser-workspace"]',
|
||
);
|
||
return workspace
|
||
? {
|
||
activeTurnId: workspace.getAttribute(
|
||
"data-browser-active-turn-id",
|
||
),
|
||
controlOwner: workspace.getAttribute(
|
||
"data-browser-control-owner",
|
||
),
|
||
pageRevision: workspace.getAttribute(
|
||
"data-browser-page-revision",
|
||
),
|
||
tabId: workspace.getAttribute("data-browser-tab-id"),
|
||
webContentsId: workspace.getAttribute(
|
||
"data-browser-web-contents-id",
|
||
),
|
||
}
|
||
: null;
|
||
})(),
|
||
invokeTrace: (() => {
|
||
try {
|
||
const value = JSON.parse(
|
||
window.localStorage.getItem("lime_invoke_trace_buffer_v1") ||
|
||
"[]",
|
||
);
|
||
return value.slice(-20).map((entry) => ({
|
||
command: entry?.command || null,
|
||
status: entry?.status || null,
|
||
transport: entry?.transport || null,
|
||
error: entry?.error || null,
|
||
}));
|
||
} catch {
|
||
return [];
|
||
}
|
||
})(),
|
||
}));
|
||
console.error(
|
||
`${LOG_PREFIX} initial-observe-diagnostics ${JSON.stringify(
|
||
sanitizeJson({
|
||
error: sanitizeText(error),
|
||
providerRequests: summarizeProviderRequests(
|
||
providerFixture.requests,
|
||
),
|
||
requestLog: requestLog.slice(-20),
|
||
diagnostics,
|
||
}),
|
||
)}`,
|
||
);
|
||
throw error;
|
||
}
|
||
const activeTurnId = initial.state?.activeTurnId || null;
|
||
assert(activeTurnId, "初次 observe 未绑定 active turn");
|
||
const agentControlled = await waitForBrowserWorkspaceState(
|
||
page,
|
||
options,
|
||
(state) =>
|
||
state.controlOwner === "agent" &&
|
||
state.activeTurnId === activeTurnId &&
|
||
state.webContentsId === initial.state.webContentsId,
|
||
"Browser Workspace 未投影 Agent 控制态",
|
||
);
|
||
await captureBrowserScreenshot(page, options, "agent-controlled");
|
||
|
||
if (options.scenario === "approval" || browseApproval) {
|
||
if (browseApproval) {
|
||
logStage("approve-browse-after-policy-change");
|
||
await approveBrowserBrowse({
|
||
app,
|
||
page,
|
||
options,
|
||
record: browseApproval,
|
||
initial: false,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
});
|
||
}
|
||
let approvalEvidence = await runBrowserApprovalScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
onFirstPrompt: browseApproval
|
||
? (prompt) =>
|
||
captureBrowserActionApproval(app, prompt, browseApproval)
|
||
: undefined,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
requestLog,
|
||
waitForBrowserWorkspaceState,
|
||
waitForScenarioValue,
|
||
waitForTerminalThread,
|
||
});
|
||
if (browseApproval)
|
||
approvalEvidence = joinBrowserBrowseEvidence(
|
||
approvalEvidence,
|
||
browseApproval,
|
||
);
|
||
if (browseApproval && approvalEvidence.failedAssertions.length === 0)
|
||
approvalEvidence = await runBrowserReceiptTurns({
|
||
app,
|
||
page,
|
||
options,
|
||
sessionOptions,
|
||
identity,
|
||
providerFixture,
|
||
requestLog,
|
||
evidence: approvalEvidence,
|
||
consoleErrors,
|
||
pageErrors,
|
||
readBrowserWorkspaceState,
|
||
readBrowserDebuggerState,
|
||
readInvokeTrace: () => invokeTraceCollector.snapshot(),
|
||
waitForBrowserWorkspaceState,
|
||
waitForScenarioValue,
|
||
waitForTerminalThread,
|
||
logStage,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(approvalEvidence, null, 2)}\n`,
|
||
);
|
||
if (approvalEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser approval Gate B 断言失败: ${approvalEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return approvalEvidence;
|
||
}
|
||
|
||
if (options.scenario === "user-control") {
|
||
const userControlEvidence = await runBrowserUserControlScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
requestLog,
|
||
waitForBrowserWorkspaceState,
|
||
waitForScenarioValue,
|
||
waitForTerminalThread,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(userControlEvidence, null, 2)}\n`,
|
||
);
|
||
if (userControlEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser user-control Gate B 断言失败: ${userControlEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return userControlEvidence;
|
||
}
|
||
|
||
if (options.scenario === "cancel") {
|
||
const cancelEvidence = await runBrowserCancelScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
requestLog,
|
||
waitForBrowserWorkspaceState,
|
||
waitForTerminalThread,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(cancelEvidence, null, 2)}\n`,
|
||
);
|
||
if (cancelEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser cancel Gate B 断言失败: ${cancelEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return cancelEvidence;
|
||
}
|
||
|
||
if (options.scenario === "window-close") {
|
||
const windowCloseEvidence = await runBrowserWindowCloseScenario({
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(windowCloseEvidence, null, 2)}\n`,
|
||
);
|
||
if (windowCloseEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser window-close Gate B 断言失败: ${windowCloseEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return windowCloseEvidence;
|
||
}
|
||
|
||
if (options.scenario === "disconnect") {
|
||
const disconnectEvidence = await runBrowserDisconnectScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
waitForBrowserWorkspaceState,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(disconnectEvidence, null, 2)}\n`,
|
||
);
|
||
if (disconnectEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser disconnect Gate B 断言失败: ${disconnectEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return disconnectEvidence;
|
||
}
|
||
|
||
if (options.scenario === "permission") {
|
||
const permissionEvidence = await runBrowserPermissionScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
requestLog,
|
||
waitForBrowserWorkspaceState,
|
||
waitForTerminalThread,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(permissionEvidence, null, 2)}\n`,
|
||
);
|
||
if (permissionEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser permission Gate B 断言失败: ${permissionEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return permissionEvidence;
|
||
}
|
||
|
||
if (options.scenario === "download") {
|
||
const downloadEvidence = await runBrowserDownloadScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
requestLog,
|
||
waitForBrowserWorkspaceState,
|
||
waitForTerminalThread,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(downloadEvidence, null, 2)}\n`,
|
||
);
|
||
if (downloadEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser download Gate B 断言失败: ${downloadEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return downloadEvidence;
|
||
}
|
||
|
||
if (options.scenario === "artifact") {
|
||
const artifactEvidence = await runBrowserArtifactScenario({
|
||
activeTurnId,
|
||
agentControlled,
|
||
app,
|
||
consoleErrors,
|
||
finalMarker: FINAL_MARKER,
|
||
guiBeforeTurn,
|
||
identity,
|
||
initial,
|
||
logStage,
|
||
options,
|
||
page,
|
||
pageErrors,
|
||
providerFixture,
|
||
readBrowserDebuggerState,
|
||
readBrowserWorkspaceState,
|
||
requestLog,
|
||
waitForBrowserWorkspaceState,
|
||
waitForTerminalThread,
|
||
});
|
||
fs.writeFileSync(
|
||
options.output,
|
||
`${JSON.stringify(artifactEvidence, null, 2)}\n`,
|
||
);
|
||
if (artifactEvidence.failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser artifact Gate B 断言失败: ${artifactEvidence.failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return artifactEvidence;
|
||
}
|
||
|
||
logStage("user-navigation-during-active-turn");
|
||
const userNavigation = await navigateVisibleBrowserTab(
|
||
page,
|
||
options,
|
||
agentControlled.tabId,
|
||
fixturePages.userUrl,
|
||
);
|
||
const latestAfterUserNavigation = await waitForBrowserWorkspaceState(
|
||
page,
|
||
options,
|
||
(state) =>
|
||
state.controlOwner === "user" &&
|
||
state.activeTurnId === null &&
|
||
state.webContentsId === initial.state.webContentsId &&
|
||
state.pageRevision > initial.observation.pageRevision &&
|
||
state.address === fixturePages.userUrl,
|
||
"用户导航后 Browser Workspace 未释放 Agent 控制",
|
||
);
|
||
await captureBrowserScreenshot(page, options, "user-takeover");
|
||
await waitForNativeBrowserCommit(
|
||
app,
|
||
options,
|
||
initial.state.webContentsId,
|
||
fixturePages.userUrl,
|
||
);
|
||
providerFixture.scenario.releaseUserNavigation();
|
||
|
||
logStage("reclaim-reject-stale-and-reobserve");
|
||
let recovered;
|
||
let reclaimed;
|
||
try {
|
||
recovered = await waitForScenarioValue(
|
||
options,
|
||
() => providerFixture.scenario.recovered,
|
||
"Agent 重新 claim/observe",
|
||
);
|
||
reclaimed = await waitForBrowserWorkspaceState(
|
||
page,
|
||
options,
|
||
(state) =>
|
||
isReclaimedBrowserWorkspaceState({
|
||
activeTurnId,
|
||
initialWebContentsId: initial.state.webContentsId,
|
||
recovered,
|
||
state,
|
||
}),
|
||
"Browser Workspace 未投影重新 claim 状态",
|
||
);
|
||
} catch (error) {
|
||
const read = await invokeAppServerFromPage(
|
||
page,
|
||
APP_SERVER_METHOD_SESSION_READ,
|
||
{ threadId: identity.threadId, includeTurns: true },
|
||
requestLog,
|
||
).catch(() => null);
|
||
const turns = read?.result?.thread?.turns ?? read?.result?.turns ?? [];
|
||
const diagnostics = sanitizeJson({
|
||
initial: providerFixture.scenario.initial?.state,
|
||
latestAfterUserNavigation:
|
||
providerFixture.scenario.latestAfterUserNavigation,
|
||
recovered: providerFixture.scenario.recovered?.state,
|
||
staleMutationFailure: providerFixture.scenario.staleMutationFailure,
|
||
providerRequests: summarizeProviderRequests(providerFixture.requests),
|
||
connectionDiagnostics: providerFixture.connectionDiagnostics,
|
||
browserWorkspace: await readBrowserWorkspaceState(page),
|
||
pendingApprovals: await app.evaluate(
|
||
() => globalThis.__appServerE2E?.pendingBrowserApprovals?.() ?? [],
|
||
),
|
||
turns: turns.map((turn) => ({
|
||
id: turn.id ?? turn.turnId,
|
||
status: turn.status,
|
||
error: turn.error,
|
||
itemTypes: (turn.items ?? []).map((item) => ({
|
||
type: item.type,
|
||
status: item.status,
|
||
name: item.name,
|
||
})),
|
||
})),
|
||
});
|
||
const diagnosticPath = options.output.replace(
|
||
/\.json$/u,
|
||
"-failure.json",
|
||
);
|
||
fs.writeFileSync(
|
||
diagnosticPath,
|
||
`${JSON.stringify(diagnostics, null, 2)}\n`,
|
||
);
|
||
throw new Error(
|
||
`${error instanceof Error ? error.message : String(error)}; ` +
|
||
`diagnostics=${diagnosticPath}; ` +
|
||
`lastRequests=${JSON.stringify(diagnostics.providerRequests.slice(-2))}`,
|
||
);
|
||
}
|
||
const debuggerBeforeTerminal = await readBrowserDebuggerState(
|
||
app,
|
||
reclaimed.webContentsId,
|
||
);
|
||
providerFixture.scenario.releaseFinalResponse();
|
||
|
||
let terminal;
|
||
try {
|
||
terminal = await waitForTerminalThread(
|
||
page,
|
||
options,
|
||
identity.threadId,
|
||
requestLog,
|
||
);
|
||
} catch (error) {
|
||
const lastRead = requestLog
|
||
.filter((entry) => entry?.method === APP_SERVER_METHOD_SESSION_READ)
|
||
.at(-1);
|
||
throw new Error(
|
||
`${error instanceof Error ? error.message : String(error)}; ` +
|
||
`providerRequests=${JSON.stringify(
|
||
providerFixture.requests.map((request) => ({
|
||
path: request.path,
|
||
responseError: request.responseError || null,
|
||
lastMessage: request.body?.messages?.at(-1) || null,
|
||
})),
|
||
)}; ` +
|
||
`lastRead=${JSON.stringify(sanitizeJson(lastRead?.response || null))}`,
|
||
);
|
||
}
|
||
const terminalTurnId = terminal.turn?.id || terminal.turn?.turnId || null;
|
||
assert(
|
||
terminalTurnId === activeTurnId,
|
||
"Browser Gate B terminal turn 与 active turn 不一致",
|
||
);
|
||
const released = await waitForBrowserWorkspaceState(
|
||
page,
|
||
options,
|
||
(state) =>
|
||
state.controlOwner === "released" &&
|
||
state.activeTurnId === null &&
|
||
state.webContentsId === initial.state.webContentsId,
|
||
"turn terminal 后 Browser 用户 tab 未 release",
|
||
);
|
||
await captureBrowserScreenshot(page, options, "released");
|
||
const debuggerAfterTerminal = await readBrowserDebuggerState(
|
||
app,
|
||
released.webContentsId,
|
||
);
|
||
const guiShellAfterTurn = await page.evaluate(() => ({
|
||
bodyText: document.body?.innerText || "",
|
||
panelVisible: Boolean(
|
||
document.querySelector('[data-testid="right-surface-browser-panel"]'),
|
||
),
|
||
activeSurface:
|
||
document
|
||
.querySelector('[data-testid="workspace-right-surface-host"]')
|
||
?.getAttribute("data-surface") || null,
|
||
}));
|
||
const guiAfterTurn = {
|
||
...(await readBrowserWorkspaceState(page)),
|
||
...guiShellAfterTurn,
|
||
};
|
||
const finalText = guiAfterTurn.bodyText;
|
||
const { bodyText: _bodyText, ...guiAfterTurnEvidence } = guiAfterTurn;
|
||
const collectedInvokeTrace = await invokeTraceCollector.stop();
|
||
invokeTraceCollector = null;
|
||
const traceBeforeDestroyRaw = await page.evaluate(() => {
|
||
try {
|
||
return JSON.parse(
|
||
window.localStorage.getItem("lime_invoke_trace_buffer_v1") || "[]",
|
||
);
|
||
} catch {
|
||
return [];
|
||
}
|
||
});
|
||
const traceBeforeDestroy = mergeInvokeTraceEvidence(
|
||
collectedInvokeTrace,
|
||
traceBeforeDestroyRaw,
|
||
);
|
||
|
||
logStage("destroy-native-browser-webcontents");
|
||
const destruction = await destroyBrowserWebContents(
|
||
app,
|
||
released.webContentsId,
|
||
);
|
||
const guiAfterDestroyed = await waitForBrowserWorkspaceState(
|
||
page,
|
||
options,
|
||
(state) => state.tabId === null && state.webContentsId === null,
|
||
"native WebContents destroyed 后 Browser route 未关闭",
|
||
);
|
||
await captureBrowserScreenshot(page, options, "destroyed");
|
||
|
||
const traceAfterTurn = traceBeforeDestroy;
|
||
const assertions = buildAssertions({
|
||
debuggerAfterTerminal,
|
||
debuggerBeforeTerminal,
|
||
destroyed: guiAfterDestroyed,
|
||
gui: guiAfterTurn,
|
||
initial,
|
||
latestAfterUserNavigation,
|
||
recovered,
|
||
released,
|
||
staleMutationFailure: providerFixture.scenario.staleMutationFailure,
|
||
trace: traceAfterTurn,
|
||
providerRequests: providerFixture.requests,
|
||
identity,
|
||
turnId: terminalTurnId,
|
||
finalText,
|
||
});
|
||
const failedAssertions = Object.entries(assertions)
|
||
.filter(([, passed]) => !passed)
|
||
.map(([name]) => name);
|
||
const evidence = sanitizeJson({
|
||
schemaVersion: "lime.browser_runtime_electron_gate_b.v1",
|
||
status: failedAssertions.length === 0 ? "pass" : "fail",
|
||
generatedAt: new Date().toISOString(),
|
||
proofLevel: "Gate B",
|
||
claimBoundary:
|
||
"真实 Electron WebContentsView、preload IPC、App Server JSON-RPC、dynamic browser tool 和 current read model 的本地闭环;不代表 live provider 或跨平台打包验证。",
|
||
identity,
|
||
gui: {
|
||
beforeTurn: guiBeforeTurn,
|
||
afterTurn: {
|
||
...guiAfterTurnEvidence,
|
||
finalAssistantVisible: finalText.includes(FINAL_MARKER),
|
||
},
|
||
afterDestroyed: guiAfterDestroyed,
|
||
},
|
||
provider: {
|
||
model: MODEL_NAME,
|
||
requestCount: providerFixture.requests.length,
|
||
toolCalls: providerFixture.requests
|
||
.filter((request) => request?.path === "/v1/chat/completions")
|
||
.map(
|
||
(request) => request?.body?.messages?.at(-1)?.tool_calls || null,
|
||
),
|
||
},
|
||
terminal: {
|
||
status: terminal.turn?.status || terminal.turn?.state || null,
|
||
turnId: terminalTurnId,
|
||
},
|
||
observedBrowserState: extractFinalBrowserState(finalText),
|
||
lifecycle: {
|
||
initial: {
|
||
activeTurnId: initial.state.activeTurnId,
|
||
pageRevision: initial.observation.pageRevision,
|
||
tabId: initial.state.tabId,
|
||
webContentsId: initial.state.webContentsId,
|
||
},
|
||
userNavigation: {
|
||
...userNavigation.navigation,
|
||
activeTurnId: latestAfterUserNavigation.activeTurnId,
|
||
controlOwner: latestAfterUserNavigation.controlOwner,
|
||
pageRevision: latestAfterUserNavigation.pageRevision,
|
||
tabId: latestAfterUserNavigation.tabId,
|
||
webContentsId: latestAfterUserNavigation.webContentsId,
|
||
},
|
||
staleMutation: providerFixture.scenario.staleMutationFailure,
|
||
recovered: {
|
||
activeTurnId: recovered.state.activeTurnId,
|
||
controlOwner: recovered.state.controlOwner,
|
||
pageRevision: recovered.observation.pageRevision,
|
||
tabId: recovered.state.tabId,
|
||
webContentsId: recovered.state.webContentsId,
|
||
},
|
||
terminal: {
|
||
controlOwner: released.controlOwner,
|
||
debuggerAttachedBefore: debuggerBeforeTerminal.attached,
|
||
debuggerAttachedAfter: debuggerAfterTerminal.attached,
|
||
tabId: released.tabId,
|
||
webContentsId: released.webContentsId,
|
||
},
|
||
destroyed: {
|
||
browserTabId: guiAfterDestroyed.tabId,
|
||
webContentsId: guiAfterDestroyed.webContentsId,
|
||
panelVisible: guiAfterDestroyed.panelVisible,
|
||
trigger: destruction.trigger || null,
|
||
},
|
||
},
|
||
invokeTrace: summarizeInvokeTrace(traceAfterTurn),
|
||
assertions,
|
||
failedAssertions,
|
||
diagnostics: { consoleErrors, pageErrors },
|
||
});
|
||
fs.writeFileSync(options.output, `${JSON.stringify(evidence, null, 2)}\n`);
|
||
if (failedAssertions.length > 0) {
|
||
throw new Error(
|
||
`Browser Gate B 断言失败: ${failedAssertions.join(", ")}`,
|
||
);
|
||
}
|
||
return evidence;
|
||
} finally {
|
||
if (invokeTraceCollector) {
|
||
await invokeTraceCollector.stop().catch(() => undefined);
|
||
}
|
||
providerFixture.scenario.releaseAll();
|
||
await closeFixtureElectron(app);
|
||
await providerFixture.close().catch(() => undefined);
|
||
await fixturePages.close();
|
||
if (!options.keepTemp) cleanupTempRoot(runtimeEnv.tempRoot);
|
||
}
|
||
}
|
||
|
||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||
const options = parseArgs(process.argv.slice(2));
|
||
if (options.help) {
|
||
printHelp();
|
||
process.exit(0);
|
||
}
|
||
run(options)
|
||
.then(() => {
|
||
console.log(`${LOG_PREFIX} result=pass output=${options.output}`);
|
||
})
|
||
.catch((error) => {
|
||
console.error(`${LOG_PREFIX} result=fail error=${sanitizeText(error)}`);
|
||
process.exitCode = 1;
|
||
});
|
||
}
|