pi-agent-browser-native 0.6.10 → 0.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/README.md +50 -7
- package/dist/extensions/agent-browser/index.js +416 -430
- package/dist/extensions/agent-browser/lib/argv-grammar.js +1 -1
- package/dist/extensions/agent-browser/lib/command-policy.js +41 -2
- package/dist/extensions/agent-browser/lib/input-modes/job.js +7 -7
- package/dist/extensions/agent-browser/lib/input-modes/params.js +1 -1
- package/dist/extensions/agent-browser/lib/input-modes/script.js +3 -2
- package/dist/extensions/agent-browser/lib/managed-session-restore.js +40 -10
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/diagnostics.js +3 -0
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/final-result.js +8 -2
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/index.js +1 -0
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/prepare/wait-timeouts.js +3 -2
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/prepare.js +25 -14
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/process-output.js +56 -10
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/recording-recovery.js +161 -0
- package/dist/extensions/agent-browser/lib/orchestration/browser-run/session-state.js +3 -3
- package/dist/extensions/agent-browser/lib/orchestration/input-plan.js +2 -4
- package/dist/extensions/agent-browser/lib/orchestration/native-session-defaults.js +68 -0
- package/dist/extensions/agent-browser/lib/orchestration/output-file.js +41 -6
- package/dist/extensions/agent-browser/lib/page-target-validation.js +9 -5
- package/dist/extensions/agent-browser/lib/playbook.js +10 -9
- package/dist/extensions/agent-browser/lib/process-environment.js +26 -8
- package/dist/extensions/agent-browser/lib/process.js +8 -5
- package/dist/extensions/agent-browser/lib/read-confirmation.js +59 -0
- package/dist/extensions/agent-browser/lib/recording-reservations.js +11 -1
- package/dist/extensions/agent-browser/lib/results/action-recommendations.js +8 -0
- package/dist/extensions/agent-browser/lib/results/artifact-manifest.js +6 -5
- package/dist/extensions/agent-browser/lib/results/categories.js +4 -2
- package/dist/extensions/agent-browser/lib/results/presentation/artifacts.js +60 -29
- package/dist/extensions/agent-browser/lib/results/presentation/batch.js +19 -8
- package/dist/extensions/agent-browser/lib/results/presentation/common.js +0 -20
- package/dist/extensions/agent-browser/lib/results/presentation/diagnostics.js +40 -38
- package/dist/extensions/agent-browser/lib/results/presentation/errors.js +1 -0
- package/dist/extensions/agent-browser/lib/results/presentation/navigation.js +3 -3
- package/dist/extensions/agent-browser/lib/results/presentation.js +38 -9
- package/dist/extensions/agent-browser/lib/results/recording.js +50 -0
- package/dist/extensions/agent-browser/lib/runtime.js +72 -20
- package/dist/extensions/agent-browser/lib/session-page-state.js +23 -7
- package/dist/extensions/agent-browser/lib/temp.js +4 -0
- package/docs/ARCHITECTURE.md +27 -11
- package/docs/COMMAND_REFERENCE.md +37 -17
- package/docs/RELEASE.md +3 -1
- package/docs/SUPPORT_MATRIX.md +10 -4
- package/docs/TOOL_CONTRACT.md +87 -25
- package/docs/platform-smoke.md +9 -1
- package/package.json +1 -1
- package/scripts/prepare.mjs +2 -4
|
@@ -14,13 +14,14 @@ export const QUICK_START_GUIDELINES = [
|
|
|
14
14
|
"Locator-first clicks/fills and native select changes without hand-building argv: { semanticAction: { action: \"click\", locator: \"text\", value: \"Close\" } }, { semanticAction: { action: \"fill\", locator: \"label\", value: \"Email\", text: \"user@example.com\" } }, direct current targets such as { semanticAction: { action: \"fill\", selector: \"@e1\", text: \"prompt\" } }, or { semanticAction: { action: \"select\", selector: \"#flavor\", value: \"chocolate\" } }; add semanticAction.session when targeting a named upstream browser session; details.compiledSemanticAction shows the semantic target, while details.effectiveArgs may show a resolved current @ref for active-session role/name click/check/fill actions to avoid hidden duplicate matches; semanticAction does not expose uncheck while upstream find ... uncheck is not runtime-supported, so use raw uncheck with a stable selector or current ref; selector-not-found failures may append bounded click try-*-candidate next actions or, for fill misses with current editable refs, details.richInputRecovery with focus/click actions that do not copy fill text; stale-ref failures can return retry-semantic-action-after-stale-ref for compiled find actions when retry safety is provable.",
|
|
15
15
|
`Common advanced calls: { args: ["batch", "--bail"], stdin: "[[\"open\",\"https://example.com\"],[\"snapshot\",\"-i\"]]" }, { job: { steps: [{ action: "open", url: "https://example.com" }, { action: "assertText", text: "Example Domain" }, { action: "screenshot", path: ".dogfood/example.png" }] } }, { qa: { url: "https://example.com", expectedText: "Example Domain", screenshotPath: ".dogfood/qa-example.png" } } (example.com smoke only; elsewhere match exact visible text from snapshot -i), { electron: { action: "list", query: "code" } }, { electron: { action: "launch", appName: "Visual Studio Code", handoff: "snapshot" } }, { electron: { action: "probe" } }, { qa: { attached: true, expectedText: "Explorer" } }, { args: ["eval", "--stdin"], stdin: "document.title", outputPath: "logs/page-title.json" }, { args: ["auth", "save", "name", "--password-stdin"], stdin: "<password from user-approved secret source>" }, { args: ["--profile", "Default", "open", "https://example.com/account"], sessionMode: "fresh" }, and { args: ["open", "--enable", "react-devtools", "https://example.com"], sessionMode: "fresh" }. For app pages with a native dropdown, job steps can include { action: "select", selector: "#flavor", value: "chocolate" } before the dependent assertion; for locator-friendly pages, job click/fill steps can use semantic locator fields such as { action: "fill", locator: "role", role: "searchbox", name: "Search", text: "agent browser" }; for human-paced input, job type steps can use { action: "type", selector: "#prompt", text: "hello", delayMs: 20, press: "Enter" }; delayed typing is capped at 200 characters per step, and generated per-character rows are compacted in visible batch prose while full rows remain in details.batchSteps.`,
|
|
16
16
|
"Constrained job navigation is explicit only: click (and select/submit flows that may navigate) does not prove the next page loaded; add an assertUrl that does not already match the starting page and/or assertText for new page state after navigation-prone steps before screenshot or later interactions. assertText takes only text, not selector or locator fields. Clicks can stale subsequent @refs: split the job and re-snapshot before using those refs. Keep jobs short around navigation, click, and rerender boundaries on dynamic React/product apps; avoid a whole checkout in one job. If a long job times out and details.timeoutPartialProgress shows a mutating incomplete step, inspect current page state and continue with a shorter job or single action instead of blindly retrying the mutating step. Example: { job: { steps: [{ action: \"open\", url: \"https://shop.example/checkout\" }, { action: \"fill\", selector: \"#email\", text: \"user@example.com\" }, { action: \"click\", selector: \"#continue\" }, { action: \"assertUrl\", url: \"**/shipping\" }, { action: \"assertText\", text: \"Shipping address\" }, { action: \"screenshot\", path: \".dogfood/shipping.png\" }] } }. Top-level click may add pageChangeSummary hints, but job never auto-inserts post-click asserts.",
|
|
17
|
-
"High-value command reference: click <selector> --new-tab opens link-like targets in a new tab; select <selector> <value...> changes native dropdown values; wrapper-handled scroll <dir> [px|percent] and scroll to end/top target document scrolling before upstream fallback, while scroll <selector> <dir> [px|percent] targets nested scrollers; download <selector> <path> saves a file triggered by a click; read [url] returns agent-readable text (explicit URLs
|
|
18
|
-
"For artifact-producing commands, read the visible artifact block and details.artifactVerification before using files: check requested path, absolute path, existence, size bytes, artifact kind, optional mediaType, status, optional limitation, and verified/missing/pending/unverified counts. details.artifacts contains per-file metadata; record start rows are pending/openRecording until record stop
|
|
17
|
+
"High-value command reference: click <selector> --new-tab opens link-like targets in a new tab; select <selector> <value...> changes native dropdown values; wrapper-handled scroll <dir> [px|percent] and scroll to end/top target document scrolling before upstream fallback, while scroll <selector> <dir> [px|percent] targets nested scrollers; download <selector> <path> saves a file triggered by a click; read [url] returns agent-readable text (explicit URLs and all-read batches skip browser helpers and managed replacement, including timeout recovery; omit the URL for verified active-tab DOM); get title/url need no selector; get text/html/value/count <selector> and get attr <selector> <name> read elements/page state (use body for whole-page text/html); screenshot [selector] [path] captures a page or element image; pdf <path> saves a PDF; tab list and tab <tab-id-or-label> inspect or recover the active tab; react tree, react inspect <fiberId>, react renders start/stop, and react suspense introspect React after --enable react-devtools; vitals [url] measures Core Web Vitals; pushstate <url> performs SPA navigation; tap <selector> and swipe <direction> [distance] support iOS/provider touch flows.",
|
|
18
|
+
"For artifact-producing commands, read the visible artifact block and details.artifactVerification before using files: check requested path, absolute path, existence, size bytes, artifact kind, optional mediaType, status, optional limitation, and verified/missing/pending/unverified counts. details.artifacts contains per-file metadata; record start rows are pending/openRecording until record stop finalizes the target. Recording receipts separate captured-frame rate/wall duration from nominal FPS and held/encoded frames; unknown native metrics stay unknown, and sparse or final-only frames cannot prove smoothness. A stop timeout gets one bounded session-info receipt query; follow its exact status/stop actions. Distinct outputPath exports retain failed attempts too. Current upstream records the active page unless a URL is supplied. To support older natives, the wrapper conservatively blocks prior @e… refs after every dispatched start attempt and URL-bearing restart, even on failure; this does not prove the page changed. Take a fresh snapshot before continuing. A restart with only --fps keeps the page and refs; --fps <n> selects 1–60 fps (default 30). The wrapper creates parent directories for direct artifact paths and can save simple loopback HTTP(S) anchor downloads directly to the requested path before upstream download fallback. Browser close does not delete explicit saved files; if close reports details.artifactCleanup, use host file tools to remove paths listed in explicitArtifactPaths (when non-empty) after inspection. If close fails with details.promptGuard.reason=requested-artifacts-missing-before-close, save the exact required artifact path before closing. A bare inbound image/video path is not a requested output artifact and does not block close. For annotated screenshots inside batch, put --annotate in top-level args (for example { args: [\"--annotate\", \"batch\"], stdin: \"[[\\\"screenshot\\\",\\\"/tmp/page.png\\\"]]\" }) rather than inside the screenshot step; if annotation labels crowd a dense page, use a scoped or non-annotated screenshot plus snapshot refs instead.",
|
|
19
|
+
"Use session info for one read-only preflight distinguishing daemon PID/activity from native browser liveness, Chrome PID, exact profile, tabs and native ownership; Pi cleanup ownership is separate and absent fields stay unknown. For policy-required URL reads, use the exact confirm/deny actions and session they return. Only native explicit-read provenance plus capabilities.readRequiresConfirmation enables browserless confirmation; legacy/DOM confirmations retain normal page checks.",
|
|
19
20
|
"When failure output shows Next actions, prefer those exact native agent_browser follow-up payloads over guessed commands. The same actions are available in details.nextActions to callers that expose structured details; short stdin is shown inline, while long stdin stays details-only.",
|
|
20
21
|
];
|
|
21
22
|
export const WEB_SEARCH_PROMPT_GUIDELINE = "Prefer agent_browser_web_search for current or external web facts and URL discovery over public search-engine forms that can hit anti-bot/CAPTCHA-gated pages. For research before implementation, pass searchType: deep-lite unless webSearch.defaultSearchType already does; omit it for everyday lookups so config/auto wins. Provider rank is not proof of authority: when correctness or version matters, prefer the vendor or project's primary current docs, inspect page-date and version clues, and constrain one follow-up after discovering the official domain (Exa includeDomains; Brave site: in query). Do not count URL aliases as independent sources. Use agent_browser after you have a target URL that needs interaction, screenshots, or DOM inspection.";
|
|
22
23
|
export const SHARED_BROWSER_PLAYBOOK_GUIDELINES = [
|
|
23
|
-
"Use top-level script only for one-shot loops, conditional page branches, or multi-page aggregation that would otherwise require several calls: await browser({ args, stdin?, timeoutMs? }), branch on its ok field, and emit one bounded JSON value. Script gets an isolated non-profile browser session that is always closed, cannot use caller session/namespace/lifecycle/attachment controls, inherited agent-browser launch/proxy settings, or host APIs, and is not a reusable named recipe. One top-level approval can authorize up to 25 inner calls, so inspect the full source before approval. Use args/job/qa for ordinary linear flows.",
|
|
24
|
+
"Use top-level script only for one-shot loops, conditional page branches, or multi-page aggregation that would otherwise require several calls: await browser({ args, stdin?, timeoutMs? }), branch on its ok field, and emit one bounded JSON value. Script gets an isolated non-profile browser session that is always closed, cannot use caller session/namespace/lifecycle/attachment controls, inherited agent-browser launch/proxy settings, user/project config or explicit --config, or host APIs, and is not a reusable named recipe. One top-level approval can authorize up to 25 inner calls, so inspect the full source before approval. Use args/job/qa for ordinary linear flows.",
|
|
24
25
|
"Standard workflow: open the page, snapshot -i, interact using current @refs from that snapshot, and re-snapshot after navigation, scrolling, rerendering, or other major DOM changes because refs are page-scoped; the wrapper fails mutation-prone stale/recycled refs before upstream can silently target a different current-page element. On dense pages, use wrapper-side snapshot -i --search <text> or snapshot -i --filter role=<role> to render matching refs while preserving the full ref map in details.refSnapshot, add snapshot --viewport when scroll position or above/below-fold context matters, and add snapshot --diff when a quick before/after ref-map delta would prevent reading a full spill file.",
|
|
25
26
|
"For ordinary forms from one snapshot, batch multiple fill @refs before the submit/click step to avoid serial tool calls; if a fill may autosubmit, navigate, or rerender later fields, split the flow and refresh refs first.",
|
|
26
27
|
"Do not use browser automation to drive public search-engine forms such as Google for discovery; headless jobs that type a query and press Enter can be redirected to anti-bot or CAPTCHA pages. Prefer agent_browser_web_search for live discovery, then agent_browser on a target URL. Do not attempt CAPTCHA bypass.",
|
|
@@ -29,7 +30,7 @@ export const SHARED_BROWSER_PLAYBOOK_GUIDELINES = [
|
|
|
29
30
|
"For desktop or host-controlled rich inputs, if semanticAction fill misses, refresh refs and prefer a current editable @ref from details.richInputRecovery or the latest snapshot; focus or click that ref, then use keyboard type for framework-controlled editors that require real key events. keyboard inserttext is paste-like and can change a DOM value without updating application state, so use it only when later application-state evidence proves the edit was accepted. Do not auto-submit with Enter or a submit button unless the user flow explicitly calls for it.",
|
|
30
31
|
"Do not assume Playwright selector dialects such as text=Close or button:has-text('Close') are supported wrapper syntax unless current upstream agent-browser behavior has been verified.",
|
|
31
32
|
"For authenticated or user-specific content explicitly requested by the user, such as feeds, inboxes, account pages, or private dashboards, use a real profile only when the user/config asks for it or profiles have been inspected; do not assume --profile Default exists on every machine. Do not use a real profile for public pages just because they are dashboards. Treat visible page content from real profiles as model-visible transcript data. On macOS, copied Chrome profiles may omit encrypted cookies, so profile selection alone is not proof of authentication; verify the target page and use a user-approved headed login once when needed. Use --auto-connect only if profile-based reuse is unavailable or the task is specifically about attaching to a running debug-enabled browser. If profile/user-data-dir resolution fails, stop retrying opens, run profiles and/or doctor through agent_browser, then report what the user needs to configure.",
|
|
32
|
-
"
|
|
33
|
+
"Use bare calls for a configured native shared session; the wrapper honors its session/namespace across Pi agents without owning quit cleanup or idle policy. Otherwise use the implicit session for routine work. Coordinate shared tabs and do not close another agent's browser.",
|
|
33
34
|
`When using launch-scoped flags (${LAUNCH_SCOPED_FLAG_LABEL}), put them on the first command for that session. If you intentionally use an explicit --session, keep using that same explicit session for follow-ups.`,
|
|
34
35
|
"Caller-owned explicit sessions are serialized per effective canonical namespace/session inside this extension while live URL checks, semantic-action snapshots, and the requested command run. For raw batches whose later content step depends on navigation, use exact batch --bail or split the calls; unsafe continue-after-navigation-failure shapes are rejected before the batch runs.",
|
|
35
36
|
"After a successful `connect`, `--cdp`, or enabled `--auto-connect` call, verify with get url and keep using the resulting session without repeating the attach flag. The wrapper remembers that attachment across active-branch reload/resume and live-checks the URL before later page reads/interactions because an attached browser can drift externally; caller config, file access, launch arguments, and environment pass through unchanged. A successful close clears the marker. When several named sessions share one Chrome, pass --pin-tab once (AGENT_BROWSER_PIN_TAB) so a closed bound tab fails as tab_gone instead of acting on a neighbor; recover with tab new or tab list. --no-pin-tab turns the sticky pin off. tab list includes each tab's CDP targetId, accepted as a tab ref.",
|
|
@@ -69,7 +70,7 @@ export const INSPECTION_TOOL_CALL_EXAMPLES = [
|
|
|
69
70
|
];
|
|
70
71
|
export const WRAPPER_TAB_RECOVERY_BEHAVIOR = [
|
|
71
72
|
"After open/goto/navigate calls with --profile, --restore, --session-name, or --state, agent_browser best-effort re-selects the tab whose URL matches the returned page when restored tabs steal focus during launch or reconnect.",
|
|
72
|
-
"After confirmed shutdown of an automatically restored managed session, the wrapper retains its complete recorded URL, including the fragment, until the first current-page operation (including get url and reload). Non-page calls such as tab list
|
|
73
|
+
"After confirmed shutdown of an automatically restored managed session, the wrapper retains its complete recorded URL, including the fragment, until the first current-page operation (including get url and reload). Non-page calls such as tab list may start a daemon without fulfilling that reopen; explicit URL reads leave the managed browser and pending reopen untouched. The wrapper uses native open once, verifies the observed tab, and discards old refs/frame scope; it does not restore unsaved forms, JavaScript memory, or history. Explicit navigation, caller-owned/attached sessions, and restore-disabled sessions are not auto-reopened.",
|
|
73
74
|
"For a still-live browser after tab drift or resume, the wrapper verifies/selects the intended tab before ref/semantic helpers and page commands; failed selection stops the call without navigating. Local commands, read <url>, URL a11y/vitals, diff url, window new, and explicit tab/navigation/connection/state recovery do not require the prior tab. Batch checks follow effective rows past non-page prefixes and stop at explicit context changes, preserving caller argv/stdin and continue-on-error behavior. Same-tab reselection is avoided because it clears refs. Use exact batch --bail for fail-fast, not --bail=<value>. Routine same-session calls skip tab-list preflights.",
|
|
74
75
|
"For sessions with observed tab-drift risk, after a successful command on a known target tab, agent_browser also best-effort restores that intended tab if a restored/background tab steals focus after the command completes. Routine same-session commands skip this post-command tab-list probe.",
|
|
75
76
|
"If a known session target unexpectedly reports about:blank, agent_browser best-effort re-selects the prior intended target when it still exists; if recovery fails, it records the observed about:blank target and reports exact recovery guidance instead of treating the prior page as active.",
|
|
@@ -79,7 +80,7 @@ export const WRAPPER_TAB_RECOVERY_BEHAVIOR = [
|
|
|
79
80
|
export const RUNTIME_PROMPT_GUIDELINES = [
|
|
80
81
|
"Use agent_browser with one input mode: script, args, semanticAction, job, qa, sourceLookup/networkSourceLookup, or electron. stdin: batch/eval/auth/wrapper batch only; electron rejects it; never pass --json.",
|
|
81
82
|
"For agent_browser, use open → snapshot -i → @refs; re-snapshot after changes. In authenticated unattended/auto-approved employee flows, ordinary requested non-destructive submissions may proceed. Honor explicit stops; require explicit authorization for purchases, production-control, destructive/irreversible, or account/security/privacy changes.",
|
|
82
|
-
"
|
|
83
|
+
"agent_browser honors native shared-session defaults on bare calls; do not relaunch/close a shared browser. Without a selected native session, use sessionMode=fresh for launch flags. Use requested/configured profiles only; verify auth. Profile content is model-visible; script is always disposable.",
|
|
83
84
|
"agent_browser: exact user paths; verify artifactVerification/artifacts before success claims. Save promptGuard-required files before close; ffmpeg before recording; close keeps files; waited:timeout proves nothing.",
|
|
84
85
|
"When agent_browser details.nextActions exists, use them. Check Omitted high-value controls in dense snapshots. Dashboards: verify scroll via screenshot/snapshot.",
|
|
85
86
|
"agent_browser: read <url> for docs/text or active DOM; get title/url; get text/html/value/count <selector>; get attr <selector> <name>. Batch 3+ getters; heed visibility warnings.",
|
|
@@ -87,15 +88,15 @@ export const RUNTIME_PROMPT_GUIDELINES = [
|
|
|
87
88
|
export function buildBrowserExecutablePathGuideline(executablePath) {
|
|
88
89
|
if (!executablePath)
|
|
89
90
|
return undefined;
|
|
90
|
-
return `agent_browser config sets browser.executablePath to ${JSON.stringify(executablePath)}; for fresh browser launches that should use that Chromium-compatible executable, add --executable-path ${JSON.stringify(executablePath)} with sessionMode:fresh. The upstream profiles command still lists Chrome profiles only; for non-Chrome Chromium login state, ask the user for an explicit profile/user-data directory path or inspect local setup with profiles/doctor before recommending a profile value.`;
|
|
91
|
+
return `agent_browser config sets browser.executablePath to ${JSON.stringify(executablePath)}; reuse a configured native shared session with bare calls; otherwise for fresh browser launches that should use that Chromium-compatible executable, add --executable-path ${JSON.stringify(executablePath)} with sessionMode:fresh. The upstream profiles command still lists Chrome profiles only; for non-Chrome Chromium login state, ask the user for an explicit profile/user-data directory path or inspect local setup with profiles/doctor before recommending a profile value.`;
|
|
91
92
|
}
|
|
92
93
|
export function buildBrowserDefaultProfileGuideline(profile) {
|
|
93
94
|
if (!profile || profile.policy === "explicit-only")
|
|
94
95
|
return undefined;
|
|
95
96
|
if (profile.policy === "always") {
|
|
96
|
-
return `agent_browser config sets browser.defaultProfile.name to ${JSON.stringify(profile.name)} with policy always; use --profile ${JSON.stringify(profile.name)} with sessionMode:fresh when a fresh browser launch should use the configured profile, and treat profile content as model-visible user data.`;
|
|
97
|
+
return `agent_browser config sets browser.defaultProfile.name to ${JSON.stringify(profile.name)} with policy always; reuse a configured native shared session with bare calls, otherwise use --profile ${JSON.stringify(profile.name)} with sessionMode:fresh when a fresh browser launch should use the configured profile, and treat profile content as model-visible user data.`;
|
|
97
98
|
}
|
|
98
|
-
return `agent_browser config sets browser.defaultProfile.name to ${JSON.stringify(profile.name)}; for signed-in/account-specific browser tasks, start with --profile ${JSON.stringify(profile.name)} plus sessionMode:fresh unless the user asks for a different profile.`;
|
|
99
|
+
return `agent_browser config sets browser.defaultProfile.name to ${JSON.stringify(profile.name)}; for signed-in/account-specific browser tasks, reuse a configured native shared session with bare calls, otherwise start with --profile ${JSON.stringify(profile.name)} plus sessionMode:fresh unless the user asks for a different profile.`;
|
|
99
100
|
}
|
|
100
101
|
export function buildToolPromptGuidelines(options) {
|
|
101
102
|
const browserDefaultProfileGuideline = buildBrowserDefaultProfileGuideline(options.browserDefaultProfile);
|
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { rm } from "node:fs/promises";
|
|
3
|
+
import { writeSecureTempFile } from "./temp.js";
|
|
2
4
|
const isolatedAgentBrowserEnvironment = new AsyncLocalStorage();
|
|
5
|
+
const agentBrowserProcessEnvironment = new AsyncLocalStorage();
|
|
3
6
|
const PROXY_ENV_NAMES = new Set(["ALL_PROXY", "HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY"]);
|
|
4
7
|
export function getAgentBrowserProcessEnvironment(baseEnv = process.env) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const isolatedConfig = isolatedAgentBrowserEnvironment.getStore();
|
|
9
|
+
if (isolatedConfig === undefined)
|
|
10
|
+
return { ...baseEnv, ...agentBrowserProcessEnvironment.getStore() };
|
|
11
|
+
return {
|
|
12
|
+
...Object.fromEntries(Object.entries(baseEnv).filter(([name]) => {
|
|
13
|
+
const normalizedName = name.toUpperCase();
|
|
14
|
+
return !normalizedName.startsWith("AGENT_BROWSER_") && !PROXY_ENV_NAMES.has(normalizedName);
|
|
15
|
+
})),
|
|
16
|
+
AGENT_BROWSER_CONFIG: isolatedConfig,
|
|
17
|
+
};
|
|
11
18
|
}
|
|
12
|
-
export function
|
|
13
|
-
return
|
|
19
|
+
export function withAgentBrowserProcessEnvironment(env, run) {
|
|
20
|
+
return agentBrowserProcessEnvironment.run({ ...agentBrowserProcessEnvironment.getStore(), ...env }, run);
|
|
21
|
+
}
|
|
22
|
+
export async function withIsolatedAgentBrowserEnvironment(run) {
|
|
23
|
+
if (isolatedAgentBrowserEnvironment.getStore() !== undefined)
|
|
24
|
+
return await run();
|
|
25
|
+
const path = await writeSecureTempFile({ content: "{}", prefix: "script-config", suffix: ".json" });
|
|
26
|
+
try {
|
|
27
|
+
return await isolatedAgentBrowserEnvironment.run(path, run);
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
await rm(path, { force: true });
|
|
31
|
+
}
|
|
14
32
|
}
|
|
@@ -286,10 +286,12 @@ export function buildAgentBrowserProcessEnv(baseEnv = processEnv, overrides = un
|
|
|
286
286
|
clampUpstreamDefaultTimeout(childEnv);
|
|
287
287
|
return childEnv;
|
|
288
288
|
}
|
|
289
|
-
function getManagedPreSpawnPolicyError(options, currentPageUrl, pageUrlUnknown = false) {
|
|
289
|
+
function getManagedPreSpawnPolicyError(options, currentPageUrl, pageUrlUnknown = false, browserIndependentReadConfirmation = false) {
|
|
290
290
|
if (!validateManagedSessionRestoreContextForSpawn(options)) {
|
|
291
291
|
return "Managed session restore policy, storage, or checkout identity changed after planning; refusing to start agent-browser.";
|
|
292
292
|
}
|
|
293
|
+
if (browserIndependentReadConfirmation)
|
|
294
|
+
return undefined;
|
|
293
295
|
return getPageTargetValidationError({
|
|
294
296
|
args: options.args,
|
|
295
297
|
currentPageUrl,
|
|
@@ -316,7 +318,7 @@ export async function runAgentBrowserProcess(options) {
|
|
|
316
318
|
restoreState: managedSessionRestoreState,
|
|
317
319
|
stdin,
|
|
318
320
|
};
|
|
319
|
-
const planningPolicyError = getManagedPreSpawnPolicyError(managedSessionRestoreOptions, managedStateCurrentPageUrl, managedStatePageUrlUnknown);
|
|
321
|
+
const planningPolicyError = getManagedPreSpawnPolicyError(managedSessionRestoreOptions, managedStateCurrentPageUrl, managedStatePageUrlUnknown, options.browserIndependentReadConfirmation);
|
|
320
322
|
if (planningPolicyError) {
|
|
321
323
|
return {
|
|
322
324
|
aborted: false,
|
|
@@ -331,7 +333,7 @@ export async function runAgentBrowserProcess(options) {
|
|
|
331
333
|
const managedSessionRestoreEnv = getManagedSessionRestoreEnv(managedSessionRestoreOptions);
|
|
332
334
|
const ownedManagedSessionCompatibilityEnv = getOwnedManagedSessionCompatibilityEnv(managedSessionRestoreOptions);
|
|
333
335
|
const processOverrides = {
|
|
334
|
-
[AGENT_BROWSER_IDLE_TIMEOUT_ENV]: String(getImplicitSessionIdleTimeoutMs()),
|
|
336
|
+
...(ownedManagedSession ? { [AGENT_BROWSER_IDLE_TIMEOUT_ENV]: String(getImplicitSessionIdleTimeoutMs()) } : {}),
|
|
335
337
|
...managedSessionRestoreEnv,
|
|
336
338
|
...env,
|
|
337
339
|
...getManagedSessionRestoreProtectedEnv(managedSessionRestoreOptions, managedSessionRestoreEnv),
|
|
@@ -340,7 +342,8 @@ export async function runAgentBrowserProcess(options) {
|
|
|
340
342
|
};
|
|
341
343
|
const explicitSocketDir = processOverrides[AGENT_BROWSER_SOCKET_DIR_ENV];
|
|
342
344
|
let effectiveEnv = explicitSocketDir === undefined ? { ...processOverrides, [AGENT_BROWSER_SOCKET_DIR_ENV]: undefined } : processOverrides;
|
|
343
|
-
const requestedSocketDir = explicitSocketDir ?? parentEnv[PI_AGENT_BROWSER_SOCKET_DIR_ENV]
|
|
345
|
+
const requestedSocketDir = explicitSocketDir ?? parentEnv[PI_AGENT_BROWSER_SOCKET_DIR_ENV]
|
|
346
|
+
?? (!ownedManagedSession ? parentEnv[AGENT_BROWSER_SOCKET_DIR_ENV] : undefined) ?? getAgentBrowserSocketDir();
|
|
344
347
|
if (requestedSocketDir !== undefined) {
|
|
345
348
|
const socketDirError = requestedSocketDir.length > 0
|
|
346
349
|
? await getAgentBrowserSocketDirValidationError(requestedSocketDir)
|
|
@@ -463,7 +466,7 @@ export async function runAgentBrowserProcess(options) {
|
|
|
463
466
|
});
|
|
464
467
|
};
|
|
465
468
|
const childEnv = buildAgentBrowserProcessEnv(parentEnv, effectiveEnv);
|
|
466
|
-
const spawnPolicyError = getManagedPreSpawnPolicyError(managedSessionRestoreOptions, managedStateCurrentPageUrl, managedStatePageUrlUnknown);
|
|
469
|
+
const spawnPolicyError = getManagedPreSpawnPolicyError(managedSessionRestoreOptions, managedStateCurrentPageUrl, managedStatePageUrlUnknown, options.browserIndependentReadConfirmation);
|
|
467
470
|
if (spawnPolicyError) {
|
|
468
471
|
resolve({ aborted: false, agentBrowserStarted: false, exitCode: 1, spawnError: new Error(spawnPolicyError), stderr: "", stdout: "", timedOut: false });
|
|
469
472
|
return;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { extractUpstreamCommandTokens } from "./argv-descriptor.js";
|
|
2
|
+
import { extractExplicitSessionName, getAgentBrowserSessionIdentityKey, resolveAgentBrowserNamespace, scanUpstreamGlobalFlagOccurrences } from "./argv-grammar.js";
|
|
3
|
+
import { getExplicitReadUrl } from "./command-policy.js";
|
|
4
|
+
import { isCloseCommand } from "./command-taxonomy.js";
|
|
5
|
+
import { isRecord } from "./parsing.js";
|
|
6
|
+
export function parseReadConfirmation(value) {
|
|
7
|
+
if (!isRecord(value) || value.source !== "native-explicit-url-read" || (value.state !== "pending" && value.state !== "cleared"))
|
|
8
|
+
return undefined;
|
|
9
|
+
if (typeof value.id !== "string" || !value.id || typeof value.sessionName !== "string" || !value.sessionName || (value.namespace !== undefined && typeof value.namespace !== "string"))
|
|
10
|
+
return undefined;
|
|
11
|
+
return { ...(isRecord(value.capabilities) && value.capabilities.readRequiresConfirmation === true ? { capabilities: { readRequiresConfirmation: true } } : {}), id: value.id, sessionName: value.sessionName, namespace: value.namespace, source: "native-explicit-url-read", state: value.state };
|
|
12
|
+
}
|
|
13
|
+
export function findReadConfirmation(args, confirmations, namespace) {
|
|
14
|
+
const tokens = extractUpstreamCommandTokens(args);
|
|
15
|
+
if (tokens.length !== 2 || !["confirm", "deny"].includes(tokens[0]))
|
|
16
|
+
return undefined;
|
|
17
|
+
const sessionName = extractExplicitSessionName(args);
|
|
18
|
+
const effectiveNamespace = resolveAgentBrowserNamespace(args, namespace);
|
|
19
|
+
const matches = [...confirmations].filter(value => value.state === "pending" && value.id === tokens[1]
|
|
20
|
+
&& (sessionName === undefined || getAgentBrowserSessionIdentityKey(sessionName, value.namespace) === getAgentBrowserSessionIdentityKey(value.sessionName, value.namespace))
|
|
21
|
+
&& (effectiveNamespace === undefined || getAgentBrowserSessionIdentityKey(value.sessionName, effectiveNamespace) === getAgentBrowserSessionIdentityKey(value.sessionName, value.namespace)));
|
|
22
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
23
|
+
}
|
|
24
|
+
export function scopeReadConfirmationArgs(args, confirmation) {
|
|
25
|
+
return [
|
|
26
|
+
...(scanUpstreamGlobalFlagOccurrences(args, "--namespace").length === 0 ? ["--namespace", confirmation.namespace ?? ""] : []),
|
|
27
|
+
...(extractExplicitSessionName(args) === undefined ? ["--session", confirmation.sessionName] : []),
|
|
28
|
+
...args,
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
export function nextReadConfirmation(options) {
|
|
32
|
+
const { commandTokens: tokens, current } = options;
|
|
33
|
+
const settlesRead = current?.state === "pending" && tokens.length === 2 && ["confirm", "deny"].includes(tokens[0]) && tokens[1] === current.id;
|
|
34
|
+
const confirmedResult = settlesRead && tokens[0] === "confirm" && isRecord(options.data) && options.data.confirmed === true && options.data.action === "read" && isRecord(options.data.result)
|
|
35
|
+
? options.data.result.data : options.data;
|
|
36
|
+
// Native control fields only. Never parse response content, snapshot text or nested page JSON as provenance.
|
|
37
|
+
if (isRecord(confirmedResult) && confirmedResult.confirmation_required === true && typeof confirmedResult.confirmation_id === "string" && confirmedResult.confirmation_id && !("content" in confirmedResult)) {
|
|
38
|
+
if (confirmedResult.action === "read" && (typeof getExplicitReadUrl(tokens) === "string" || settlesRead)) {
|
|
39
|
+
return { ...(isRecord(confirmedResult.capabilities) && confirmedResult.capabilities.readRequiresConfirmation === true ? { capabilities: { readRequiresConfirmation: true } } : {}), id: confirmedResult.confirmation_id, namespace: options.namespace, sessionName: options.sessionName, source: "native-explicit-url-read", state: "pending" };
|
|
40
|
+
}
|
|
41
|
+
if (current?.state === "pending")
|
|
42
|
+
return { ...current, state: "cleared" };
|
|
43
|
+
}
|
|
44
|
+
return options.succeeded && current?.state === "pending" && (settlesRead || isCloseCommand(tokens[0])) ? { ...current, state: "cleared" } : undefined;
|
|
45
|
+
}
|
|
46
|
+
export function buildReadConfirmationNextActions(confirmation, pendingResponse) {
|
|
47
|
+
if (confirmation.state === "cleared")
|
|
48
|
+
return [];
|
|
49
|
+
const prefix = ["--namespace", confirmation.namespace ?? "", "--session", confirmation.sessionName];
|
|
50
|
+
if (!pendingResponse)
|
|
51
|
+
return [{ id: "inspect-read-confirmation-session", tool: "agent_browser", params: { args: [...prefix, "session", "info"] }, reason: "Inspect the exact native session after the read confirmation failed; rerun the original URL read if its ID expired.", safety: "Read-only status, without browser launch or tab changes. Do not substitute a different pending confirmation ID." }];
|
|
52
|
+
return ["confirm", "deny"].map(command => ({
|
|
53
|
+
id: command === "confirm" ? "approve-confirmation" : "deny-confirmation", tool: "agent_browser", params: { args: [...prefix, command, confirmation.id] },
|
|
54
|
+
reason: `${command === "confirm" ? "Approve" : "Deny"} the native confirmation for this explicit URL read.`,
|
|
55
|
+
safety: confirmation.capabilities?.readRequiresConfirmation === true
|
|
56
|
+
? "Review the requested read first. The native capability proves ID matching; no DOM confirmation is implied."
|
|
57
|
+
: "Native ID matching/browser independence is unproven. The exact native session is preserved, but this confirmation retains normal page checks.",
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
@@ -14,6 +14,8 @@ function getArtifactReservation(artifact) {
|
|
|
14
14
|
cwd: artifact.cwd ?? process.cwd(),
|
|
15
15
|
namespace: artifact.namespace,
|
|
16
16
|
path: artifact.path,
|
|
17
|
+
...(artifact.recording?.recordingId ? { recordingId: artifact.recording.recordingId } : {}),
|
|
18
|
+
...(artifact.recordingStartedAtMs !== undefined ? { startedAtMs: artifact.recordingStartedAtMs } : {}),
|
|
17
19
|
sessionName: artifact.session,
|
|
18
20
|
};
|
|
19
21
|
}
|
|
@@ -43,7 +45,7 @@ export function applyRecordingArtifactsToReservations(reservations, artifacts) {
|
|
|
43
45
|
for (const [key, pending] of pendingBySession) {
|
|
44
46
|
const existing = reservations.get(key);
|
|
45
47
|
reservations.set(key, pending);
|
|
46
|
-
if (!existing || existing.absolutePath !== pending.absolutePath || existing.cwd !== pending.cwd) {
|
|
48
|
+
if (!existing || existing.absolutePath !== pending.absolutePath || existing.cwd !== pending.cwd || existing.recordingId !== pending.recordingId || existing.startedAtMs !== pending.startedAtMs) {
|
|
47
49
|
transitions.push({ reservation: pending, state: "active" });
|
|
48
50
|
}
|
|
49
51
|
}
|
|
@@ -63,6 +65,8 @@ export function appendRecordingReservationTransition(pi, transition) {
|
|
|
63
65
|
cwd: state === "active" ? reservation.cwd : undefined,
|
|
64
66
|
namespace: reservation.namespace,
|
|
65
67
|
path: state === "active" ? reservation.path : undefined,
|
|
68
|
+
recordingId: state === "active" ? reservation.recordingId : undefined,
|
|
69
|
+
startedAtMs: state === "active" ? reservation.startedAtMs : undefined,
|
|
66
70
|
sessionName: reservation.sessionName,
|
|
67
71
|
state,
|
|
68
72
|
version: 1,
|
|
@@ -81,6 +85,10 @@ function parseReservationTransition(data) {
|
|
|
81
85
|
state: "closed",
|
|
82
86
|
};
|
|
83
87
|
}
|
|
88
|
+
if (data.recordingId !== undefined && (typeof data.recordingId !== "string" || !data.recordingId))
|
|
89
|
+
return undefined;
|
|
90
|
+
if (data.startedAtMs !== undefined && (typeof data.startedAtMs !== "number" || !Number.isFinite(data.startedAtMs)))
|
|
91
|
+
return undefined;
|
|
84
92
|
if (typeof data.absolutePath !== "string" || !isAbsolute(data.absolutePath)
|
|
85
93
|
|| typeof data.cwd !== "string" || !isAbsolute(data.cwd) || typeof data.path !== "string")
|
|
86
94
|
return undefined;
|
|
@@ -90,6 +98,8 @@ function parseReservationTransition(data) {
|
|
|
90
98
|
cwd: data.cwd,
|
|
91
99
|
namespace: data.namespace,
|
|
92
100
|
path: data.path,
|
|
101
|
+
...(typeof data.recordingId === "string" ? { recordingId: data.recordingId } : {}),
|
|
102
|
+
...(typeof data.startedAtMs === "number" ? { startedAtMs: data.startedAtMs } : {}),
|
|
93
103
|
sessionName: data.sessionName,
|
|
94
104
|
},
|
|
95
105
|
state: "active",
|
|
@@ -197,6 +197,14 @@ export function buildAgentBrowserNextActions(options) {
|
|
|
197
197
|
break;
|
|
198
198
|
case "timeout":
|
|
199
199
|
{
|
|
200
|
+
if (options.command === "session" && options.subcommand === "info") {
|
|
201
|
+
actions.push(buildNextToolAction({
|
|
202
|
+
args: ["session", "info"],
|
|
203
|
+
id: "retry-session-info",
|
|
204
|
+
reason: "Retry the same session status check without opening a browser or inspecting its page.",
|
|
205
|
+
}));
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
200
208
|
const textAssertion = options.command === "wait" && options.args?.includes("--text") === true;
|
|
201
209
|
const urlAssertion = options.command === "wait" && options.args?.includes("--url") === true;
|
|
202
210
|
actions.push(buildNextToolAction({
|
|
@@ -4,7 +4,8 @@ export function isPendingRecordingCommand(command, subcommand, kind) {
|
|
|
4
4
|
return command === "record" && (subcommand === "start" || subcommand === "restart") && kind === "video";
|
|
5
5
|
}
|
|
6
6
|
export function isPendingRecordingArtifact(artifact) {
|
|
7
|
-
return
|
|
7
|
+
return artifact.recordingState === "openRecording" || artifact.status === "pending"
|
|
8
|
+
|| (artifact.status === undefined && isPendingRecordingCommand(artifact.command, artifact.subcommand, artifact.kind));
|
|
8
9
|
}
|
|
9
10
|
export const SESSION_ARTIFACT_MANIFEST_VERSION = 1;
|
|
10
11
|
export const SESSION_ARTIFACT_MANIFEST_MAX_ENTRIES_ENV = "PI_AGENT_BROWSER_SESSION_ARTIFACT_MANIFEST_MAX_ENTRIES";
|
|
@@ -98,10 +99,10 @@ export function retirePendingRecordingManifestEntries(manifest, sessionName, nam
|
|
|
98
99
|
|| !entry.session
|
|
99
100
|
|| getAgentBrowserSessionIdentityKey(entry.session, entry.namespace) !== sessionKey
|
|
100
101
|
|| entry.kind !== "video"
|
|
101
|
-
|| !
|
|
102
|
+
|| !isPendingRecordingArtifact(entry))
|
|
102
103
|
return entry;
|
|
103
104
|
changed = true;
|
|
104
|
-
return { ...entry,
|
|
105
|
+
return { ...entry, recordingState: undefined, status: "unverified", subcommand: "close-abandoned" };
|
|
105
106
|
});
|
|
106
107
|
if (!changed)
|
|
107
108
|
return manifest;
|
|
@@ -135,7 +136,7 @@ export function mergeSessionArtifactManifest(options) {
|
|
|
135
136
|
if (candidateKey !== key
|
|
136
137
|
&& sameRecordingSession
|
|
137
138
|
&& candidate.kind === "video"
|
|
138
|
-
&&
|
|
139
|
+
&& isPendingRecordingArtifact(candidate)) {
|
|
139
140
|
byPath.delete(candidateKey);
|
|
140
141
|
}
|
|
141
142
|
}
|
|
@@ -155,7 +156,7 @@ export function mergeSessionArtifactManifest(options) {
|
|
|
155
156
|
const leftTime = left.evictedAtMs ?? left.createdAtMs;
|
|
156
157
|
const rightTime = right.evictedAtMs ?? right.createdAtMs;
|
|
157
158
|
return rightTime - leftTime
|
|
158
|
-
|| Number(
|
|
159
|
+
|| Number(isPendingRecordingArtifact(right)) - Number(isPendingRecordingArtifact(left))
|
|
159
160
|
|| left.path.localeCompare(right.path);
|
|
160
161
|
})
|
|
161
162
|
.slice(0, maxEntries);
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { isPendingRecordingArtifact } from "./artifact-manifest.js";
|
|
2
2
|
function hasUnverifiedFileArtifact(artifacts) {
|
|
3
|
-
return (artifacts ?? []).some((artifact) => !isPendingRecordingArtifact(artifact) && artifact.exists !== true);
|
|
3
|
+
return (artifacts ?? []).some((artifact) => !isPendingRecordingArtifact(artifact) && (artifact.exists !== true || ["failed", "stale", "unverified"].includes(artifact.status ?? "")));
|
|
4
4
|
}
|
|
5
5
|
export function classifyAgentBrowserSuccessCategory(options) {
|
|
6
6
|
if (options.inspection)
|
|
7
7
|
return "inspection";
|
|
8
|
+
if (hasUnverifiedFileArtifact(options.artifacts))
|
|
9
|
+
return "artifact-unverified";
|
|
8
10
|
if ((options.artifacts ?? []).some(isPendingRecordingArtifact))
|
|
9
11
|
return "artifact-pending";
|
|
10
12
|
if ((options.artifacts ?? []).length > 0)
|
|
11
|
-
return
|
|
13
|
+
return "artifact-saved";
|
|
12
14
|
if (options.savedFile)
|
|
13
15
|
return "artifact-saved";
|
|
14
16
|
return "completed";
|
|
@@ -5,6 +5,7 @@ import { getExplicitArtifactDestination } from "../../orchestration/browser-run/
|
|
|
5
5
|
import { isRecord, parsePositiveInteger } from "../../parsing.js";
|
|
6
6
|
import { formatSessionArtifactRetentionSummary, getSessionArtifactManifestEntryKey, isPendingRecordingArtifact, isPendingRecordingCommand, mergeSessionArtifactManifest, } from "../artifact-manifest.js";
|
|
7
7
|
import { classifyAgentBrowserSuccessCategory } from "../categories.js";
|
|
8
|
+
import { formatRecordingReceipt, getRecordingReceipt } from "../recording.js";
|
|
8
9
|
const PNG_HEADER = Buffer.from("89504e470d0a1a0a0000000d49484452", "hex");
|
|
9
10
|
const INLINE_IMAGE_MAX_BYTES_ENV = "PI_AGENT_BROWSER_INLINE_IMAGE_MAX_BYTES";
|
|
10
11
|
const DEFAULT_INLINE_IMAGE_MAX_BYTES = 5 * 1_024 * 1_024;
|
|
@@ -159,24 +160,26 @@ async function buildFileArtifactMetadata(options) {
|
|
|
159
160
|
const absolutePath = options.artifactRequest?.absolutePath ?? resolve(options.cwd, options.path);
|
|
160
161
|
const displayPath = options.artifactRequest?.path ?? options.path;
|
|
161
162
|
const extension = extname(absolutePath || options.path).toLowerCase() || undefined;
|
|
162
|
-
const pendingRecording = isPendingRecordingCommand(options.commandInfo.command, options.commandInfo.subcommand, kind);
|
|
163
|
+
const pendingRecording = options.recordingPending === true || (isPendingRecordingCommand(options.commandInfo.command, options.commandInfo.subcommand, kind) && options.recordingOutcome !== false && options.recording?.success !== false);
|
|
164
|
+
const captureStartedAtMs = options.recording?.capture.startedAt ? Date.parse(options.recording.capture.startedAt) : NaN;
|
|
165
|
+
const recordingStartedAtMs = Number.isFinite(captureStartedAtMs) ? captureStartedAtMs : options.artifactMinUpdatedAtMs;
|
|
163
166
|
let exists;
|
|
164
167
|
let sizeBytes;
|
|
165
168
|
let mediaType;
|
|
166
169
|
let stale = false;
|
|
167
170
|
let updatedAtMs;
|
|
168
|
-
if (!pendingRecording) {
|
|
171
|
+
if (!pendingRecording || options.commandInfo.subcommand === "stop") {
|
|
169
172
|
try {
|
|
170
173
|
const fileStats = await stat(absolutePath);
|
|
171
|
-
exists =
|
|
174
|
+
exists = fileStats.isFile();
|
|
172
175
|
sizeBytes = fileStats.size;
|
|
173
176
|
updatedAtMs = fileStats.mtimeMs;
|
|
174
177
|
mediaType = fileStats.isFile() ? await getFileImageMimeType(absolutePath) : undefined;
|
|
175
178
|
const commandCreatesArtifact = !(options.commandInfo.command === "wait" && isDownloadWaitSubcommand(options.commandInfo.subcommand));
|
|
176
|
-
stale = commandCreatesArtifact && artifactMtimeIsOutsideCommandWindow(updatedAtMs, options.artifactMinUpdatedAtMs, options.artifactMaxUpdatedAtMs);
|
|
179
|
+
stale = commandCreatesArtifact && artifactMtimeIsOutsideCommandWindow(updatedAtMs, kind === "video" ? recordingStartedAtMs : options.artifactMinUpdatedAtMs, options.artifactMaxUpdatedAtMs);
|
|
177
180
|
}
|
|
178
|
-
catch {
|
|
179
|
-
exists = false;
|
|
181
|
+
catch (error) {
|
|
182
|
+
exists = ["ENOENT", "ENOTDIR"].includes(error.code ?? "") ? false : undefined;
|
|
180
183
|
}
|
|
181
184
|
}
|
|
182
185
|
return {
|
|
@@ -190,11 +193,17 @@ async function buildFileArtifactMetadata(options) {
|
|
|
190
193
|
mediaType,
|
|
191
194
|
namespace: options.namespace,
|
|
192
195
|
path: displayPath,
|
|
196
|
+
recording: options.recording,
|
|
197
|
+
recordingStartedAtMs: kind === "video" ? recordingStartedAtMs : undefined,
|
|
193
198
|
recordingState: pendingRecording ? "openRecording" : undefined,
|
|
194
199
|
requestedPath: options.artifactRequest?.path ?? getExplicitArtifactDestination(options.commandInfo.commandTokens ?? []),
|
|
195
200
|
session: options.sessionName,
|
|
196
201
|
sizeBytes,
|
|
197
|
-
status: pendingRecording ? "pending" : exists === false ? "missing" :
|
|
202
|
+
status: pendingRecording ? "pending" : exists !== true ? exists === false ? "missing" : "unverified" : stale ? "stale"
|
|
203
|
+
: kind === "video" && (options.recording?.success === false || options.recording?.output.encoderSucceeded === false || (options.recordingOutcome === false && options.recording?.success !== null)) ? "failed"
|
|
204
|
+
: kind === "video" && ((options.recording?.success !== true && options.recordingOutcome !== true)
|
|
205
|
+
|| (options.recording?.file.sizeBytes != null && options.recording.file.sizeBytes !== sizeBytes)) ? "unverified"
|
|
206
|
+
: options.artifactRequest?.status ?? "saved",
|
|
198
207
|
subcommand: options.commandInfo.subcommand,
|
|
199
208
|
tempPath: options.artifactRequest?.tempPath,
|
|
200
209
|
updatedAtMs,
|
|
@@ -204,6 +213,10 @@ async function buildFileArtifactMetadata(options) {
|
|
|
204
213
|
async function buildPreviousRestartRecordingArtifact(options) {
|
|
205
214
|
if (options.commandInfo.command !== "record" || options.commandInfo.subcommand !== "restart")
|
|
206
215
|
return undefined;
|
|
216
|
+
if (isRecord(options.data) && "previousRecording" in options.data) {
|
|
217
|
+
const recording = getRecordingReceipt(options.data.previousRecording);
|
|
218
|
+
return recording ? buildFileArtifactMetadata({ ...options, commandInfo: { command: "record", subcommand: "restart-previous" }, path: recording.path, recording }) : undefined;
|
|
219
|
+
}
|
|
207
220
|
const sessionKey = options.sessionName ? getAgentBrowserSessionIdentityKey(options.sessionName, options.namespace) : undefined;
|
|
208
221
|
const previousRecording = options.artifactManifest?.entries.find((entry) => (entry.command === "record" &&
|
|
209
222
|
(entry.subcommand === "start" || entry.subcommand === "restart") &&
|
|
@@ -233,18 +246,21 @@ async function buildPreviousRestartRecordingArtifact(options) {
|
|
|
233
246
|
exists: true,
|
|
234
247
|
mediaType: fileStats.isFile() ? await getFileImageMimeType(absolutePath) : undefined,
|
|
235
248
|
sizeBytes: fileStats.size,
|
|
236
|
-
status: stale ? "stale" : "
|
|
249
|
+
status: stale ? "stale" : "unverified",
|
|
237
250
|
updatedAtMs: fileStats.mtimeMs,
|
|
238
251
|
};
|
|
239
252
|
}
|
|
240
|
-
catch {
|
|
241
|
-
|
|
253
|
+
catch (error) {
|
|
254
|
+
const missing = ["ENOENT", "ENOTDIR"].includes(error.code ?? "");
|
|
255
|
+
return { ...base, exists: missing ? false : undefined, status: missing ? "missing" : "unverified" };
|
|
242
256
|
}
|
|
243
257
|
}
|
|
244
258
|
export async function extractFileArtifacts(options) {
|
|
245
259
|
const candidates = extractPathStrings(options.data);
|
|
246
|
-
const
|
|
247
|
-
const
|
|
260
|
+
const recording = options.commandInfo.command === "record" ? getRecordingReceipt(options.data, options.commandInfo.subcommand === "stop" ? options.recordingOutcome : undefined) : undefined;
|
|
261
|
+
const recordingPending = options.recordingPending ?? (recording?.success === null && isRecord(options.data) && isRecord(options.data.capture) && recording.capture.endedAt === null);
|
|
262
|
+
const currentArtifacts = (await Promise.all(candidates.map((path) => buildFileArtifactMetadata({ ...options, path, recording, recordingPending })))).filter((artifact) => artifact !== undefined);
|
|
263
|
+
const previousRestartRecordingArtifact = await buildPreviousRestartRecordingArtifact(options);
|
|
248
264
|
return previousRestartRecordingArtifact ? [previousRestartRecordingArtifact, ...currentArtifacts] : currentArtifacts;
|
|
249
265
|
}
|
|
250
266
|
export function buildManifestEntriesForFileArtifacts(artifacts, nowMs = Date.now()) {
|
|
@@ -259,6 +275,10 @@ export function buildManifestEntriesForFileArtifacts(artifacts, nowMs = Date.now
|
|
|
259
275
|
mediaType: artifact.mediaType,
|
|
260
276
|
namespace: artifact.namespace,
|
|
261
277
|
path: artifact.path,
|
|
278
|
+
recording: artifact.recording,
|
|
279
|
+
recordingStartedAtMs: artifact.recordingStartedAtMs,
|
|
280
|
+
recordingState: artifact.recordingState,
|
|
281
|
+
status: artifact.status,
|
|
262
282
|
requestedPath: artifact.requestedPath,
|
|
263
283
|
retentionState: artifact.exists === false || artifact.status === "stale" ? "missing" : "live",
|
|
264
284
|
session: artifact.session,
|
|
@@ -268,10 +288,7 @@ export function buildManifestEntriesForFileArtifacts(artifacts, nowMs = Date.now
|
|
|
268
288
|
}));
|
|
269
289
|
}
|
|
270
290
|
export function isManifestFileArtifact(artifact) {
|
|
271
|
-
|
|
272
|
-
return artifact.kind === "video" && artifact.command === "record" && artifact.subcommand === "restart-previous";
|
|
273
|
-
}
|
|
274
|
-
return artifact.kind === "video" && artifact.command === "record" ? true : !isPendingRecordingArtifact(artifact);
|
|
291
|
+
return artifact.kind === "video" && artifact.command === "record" ? true : artifact.status !== "stale" && !isPendingRecordingArtifact(artifact);
|
|
275
292
|
}
|
|
276
293
|
function getArtifactVerificationEntry(artifact) {
|
|
277
294
|
if (isPendingRecordingArtifact(artifact)) {
|
|
@@ -279,7 +296,9 @@ function getArtifactVerificationEntry(artifact) {
|
|
|
279
296
|
absolutePath: artifact.absolutePath,
|
|
280
297
|
exists: artifact.exists,
|
|
281
298
|
kind: artifact.kind,
|
|
282
|
-
limitation: "Recording output is pending until
|
|
299
|
+
limitation: "Recording output is pending until native finalization succeeds and the file is verified.",
|
|
300
|
+
recording: artifact.recording,
|
|
301
|
+
recordingStartedAtMs: artifact.recordingStartedAtMs,
|
|
283
302
|
mediaType: artifact.mediaType,
|
|
284
303
|
path: artifact.path,
|
|
285
304
|
recordingState: artifact.recordingState ?? "openRecording",
|
|
@@ -292,7 +311,7 @@ function getArtifactVerificationEntry(artifact) {
|
|
|
292
311
|
willExistOnStop: artifact.willExistOnStop ?? true,
|
|
293
312
|
};
|
|
294
313
|
}
|
|
295
|
-
const state = artifact.status
|
|
314
|
+
const state = ["failed", "stale", "unverified"].includes(artifact.status ?? "")
|
|
296
315
|
? "unverified"
|
|
297
316
|
: artifact.exists === true
|
|
298
317
|
? "verified"
|
|
@@ -303,13 +322,17 @@ function getArtifactVerificationEntry(artifact) {
|
|
|
303
322
|
absolutePath: artifact.absolutePath,
|
|
304
323
|
exists: artifact.exists,
|
|
305
324
|
kind: artifact.kind,
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
325
|
+
recording: artifact.recording,
|
|
326
|
+
recordingStartedAtMs: artifact.recordingStartedAtMs,
|
|
327
|
+
limitation: artifact.status === "failed" || artifact.status === "unverified"
|
|
328
|
+
? "File presence does not prove successful native recording finalization or encoding. Inspect the receipt and original failure."
|
|
329
|
+
: artifact.status === "stale"
|
|
330
|
+
? "The reported path's modification time fell outside this command's bounded artifact window. Treat the artifact as stale until regenerated."
|
|
331
|
+
: state === "missing"
|
|
332
|
+
? "The wrapper did not find the reported artifact at absolutePath. Treat the path as unverified until recovered or regenerated."
|
|
333
|
+
: state === "unverified"
|
|
334
|
+
? "The wrapper could not prove local filesystem existence for this artifact."
|
|
335
|
+
: undefined,
|
|
313
336
|
mediaType: artifact.mediaType,
|
|
314
337
|
path: artifact.path,
|
|
315
338
|
requestedPath: artifact.requestedPath,
|
|
@@ -415,6 +438,10 @@ function formatArtifactLabel(artifact) {
|
|
|
415
438
|
case "trace":
|
|
416
439
|
return "Saved trace";
|
|
417
440
|
case "video":
|
|
441
|
+
if (artifact.status === "failed")
|
|
442
|
+
return artifact.subcommand === "restart-previous" ? "Previous recording failed" : "Recording failed";
|
|
443
|
+
if (artifact.status === "unverified")
|
|
444
|
+
return artifact.subcommand === "restart-previous" ? "Previous recording unverified" : "Recording unverified";
|
|
418
445
|
if (artifact.command === "record" && artifact.subcommand === "restart-previous") {
|
|
419
446
|
if (artifact.status === "stale")
|
|
420
447
|
return "Previous recording stale";
|
|
@@ -423,7 +450,9 @@ function formatArtifactLabel(artifact) {
|
|
|
423
450
|
return "Previous recording saved";
|
|
424
451
|
}
|
|
425
452
|
if (!isPendingRecordingArtifact(artifact))
|
|
426
|
-
return "Saved recording";
|
|
453
|
+
return artifact.status === "saved" ? "Saved recording" : "Recording reported; file not verified";
|
|
454
|
+
if (artifact.subcommand === "stop")
|
|
455
|
+
return "Recording finalization pending";
|
|
427
456
|
return artifact.subcommand === "restart" ? "Recording restarted; output will be written on stop" : "Recording started; output will be written on stop";
|
|
428
457
|
}
|
|
429
458
|
}
|
|
@@ -440,7 +469,7 @@ export function formatArtifactSummary(artifacts) {
|
|
|
440
469
|
if (restartArtifact && previousRecordingArtifacts.length > 0) {
|
|
441
470
|
return [...previousRecordingArtifacts, restartArtifact].map((artifact) => `${formatArtifactLabel(artifact)}: ${artifact.path}`).join("\n");
|
|
442
471
|
}
|
|
443
|
-
return
|
|
472
|
+
return `${artifacts.every((artifact) => artifact.status === "saved") ? "Saved" : "Reported"} ${artifacts.length} artifacts: ${artifacts.map((artifact) => `${artifact.kind} ${artifact.path}`).join(", ")}`;
|
|
444
473
|
}
|
|
445
474
|
export function formatArtifactMetadataLines(artifacts) {
|
|
446
475
|
return artifacts.map((artifact, index) => {
|
|
@@ -450,12 +479,13 @@ export function formatArtifactMetadataLines(artifacts) {
|
|
|
450
479
|
`Artifact type: ${artifact.kind}`,
|
|
451
480
|
artifact.requestedPath ? `Requested path: ${artifact.requestedPath}` : undefined,
|
|
452
481
|
`Absolute path: ${artifact.absolutePath}`,
|
|
453
|
-
|
|
482
|
+
`Exists: ${artifact.exists ?? "pending until record stop"}`,
|
|
454
483
|
`Status: ${artifact.status ?? "pending"}`,
|
|
455
484
|
`Recording state: ${artifact.recordingState ?? "openRecording"}`,
|
|
456
485
|
`Will exist on stop: ${artifact.willExistOnStop !== false}`,
|
|
457
486
|
artifact.session ? `Session: ${artifact.session}` : undefined,
|
|
458
487
|
artifact.cwd ? `CWD: ${artifact.cwd}` : undefined,
|
|
488
|
+
artifact.recording ? formatRecordingReceipt(artifact.recording) : undefined,
|
|
459
489
|
`Machine data: details.artifacts[${index}]`,
|
|
460
490
|
].filter((item) => item !== undefined).join("\n");
|
|
461
491
|
}
|
|
@@ -464,7 +494,7 @@ export function formatArtifactMetadataLines(artifacts) {
|
|
|
464
494
|
`Artifact type: ${artifact.kind}`,
|
|
465
495
|
artifact.requestedPath ? `Requested path: ${artifact.requestedPath}` : undefined,
|
|
466
496
|
`Absolute path: ${artifact.absolutePath}`,
|
|
467
|
-
`Exists: ${artifact.exists
|
|
497
|
+
`Exists: ${artifact.exists ?? "unknown"}`,
|
|
468
498
|
artifact.exists === false ? "not found on disk" : undefined,
|
|
469
499
|
typeof artifact.sizeBytes === "number" ? `Size: ${formatByteCount(artifact.sizeBytes)}` : undefined,
|
|
470
500
|
typeof artifact.sizeBytes === "number" ? `Size bytes: ${artifact.sizeBytes}` : undefined,
|
|
@@ -473,6 +503,7 @@ export function formatArtifactMetadataLines(artifacts) {
|
|
|
473
503
|
artifact.mediaType ? `Media type: ${artifact.mediaType}` : undefined,
|
|
474
504
|
artifact.session ? `Session: ${artifact.session}` : undefined,
|
|
475
505
|
artifact.cwd ? `CWD: ${artifact.cwd}` : undefined,
|
|
506
|
+
artifact.recording ? formatRecordingReceipt(artifact.recording) : undefined,
|
|
476
507
|
`Machine data: details.artifacts[${index}]`,
|
|
477
508
|
].filter((item) => item !== undefined).join("\n");
|
|
478
509
|
});
|