pi-agent-browser-native 0.2.34 → 0.2.36

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +25 -15
  3. package/docs/ARCHITECTURE.md +19 -13
  4. package/docs/COMMAND_REFERENCE.md +274 -44
  5. package/docs/ELECTRON.md +3 -3
  6. package/docs/RELEASE.md +11 -11
  7. package/docs/REQUIREMENTS.md +5 -5
  8. package/docs/SUPPORT_MATRIX.md +43 -24
  9. package/docs/TOOL_CONTRACT.md +50 -30
  10. package/extensions/agent-browser/index.ts +518 -2402
  11. package/extensions/agent-browser/lib/argv-descriptor.ts +90 -0
  12. package/extensions/agent-browser/lib/argv-grammar.ts +128 -0
  13. package/extensions/agent-browser/lib/command-policy.ts +71 -0
  14. package/extensions/agent-browser/lib/command-taxonomy.ts +336 -0
  15. package/extensions/agent-browser/lib/electron/cleanup.ts +1 -0
  16. package/extensions/agent-browser/lib/executable-path.ts +19 -0
  17. package/extensions/agent-browser/lib/input-modes/params.ts +6 -6
  18. package/extensions/agent-browser/lib/orchestration/batch-stdin.ts +65 -0
  19. package/extensions/agent-browser/lib/orchestration/browser-run/browser-action-model.ts +154 -0
  20. package/extensions/agent-browser/lib/orchestration/browser-run/click-dispatch.ts +149 -0
  21. package/extensions/agent-browser/lib/orchestration/browser-run/diagnostics.ts +56 -30
  22. package/extensions/agent-browser/lib/orchestration/browser-run/final-result.ts +13 -3
  23. package/extensions/agent-browser/lib/orchestration/browser-run/index.ts +33 -27
  24. package/extensions/agent-browser/lib/orchestration/browser-run/prepare.ts +48 -22
  25. package/extensions/agent-browser/lib/orchestration/browser-run/process-output.ts +39 -10
  26. package/extensions/agent-browser/lib/orchestration/browser-run/prompt-guards.ts +93 -0
  27. package/extensions/agent-browser/lib/orchestration/browser-run/session-state.ts +98 -124
  28. package/extensions/agent-browser/lib/orchestration/browser-run/types.ts +40 -1
  29. package/extensions/agent-browser/lib/orchestration/electron-host/index.ts +860 -0
  30. package/extensions/agent-browser/lib/playbook.ts +10 -10
  31. package/extensions/agent-browser/lib/prompt-policy.ts +122 -0
  32. package/extensions/agent-browser/lib/results/action-recommendations.ts +3 -23
  33. package/extensions/agent-browser/lib/results/presentation/navigation.ts +2 -34
  34. package/extensions/agent-browser/lib/runtime.ts +93 -227
  35. package/extensions/agent-browser/lib/session-page-state.ts +31 -14
  36. package/extensions/agent-browser/lib/temp.ts +148 -23
  37. package/package.json +4 -4
  38. package/scripts/agent-browser-capability-baseline.mjs +198 -1
@@ -6,10 +6,9 @@
6
6
  * Invariants/Assumptions: agent-browser is installed separately on PATH, the wrapper targets the current locally installed upstream version only, and no backward-compatibility shims are provided.
7
7
  */
8
8
 
9
- import { constants as fsConstants } from "node:fs";
10
9
  import type { ChildProcess } from "node:child_process";
11
- import { access, copyFile, mkdir, readFile, rm, stat } from "node:fs/promises";
12
- import { delimiter, dirname, extname, isAbsolute, join, resolve } from "node:path";
10
+ import { readFile } from "node:fs/promises";
11
+ import { dirname, join, resolve } from "node:path";
13
12
  import { fileURLToPath } from "node:url";
14
13
 
15
14
  import {
@@ -18,89 +17,40 @@ import {
18
17
  keyHint,
19
18
  type AgentToolResult,
20
19
  type ExtensionAPI,
20
+ type ExtensionContext,
21
21
  type Theme,
22
22
  type ToolResultEvent,
23
23
  } from "@earendil-works/pi-coding-agent";
24
24
  import { Text } from "@earendil-works/pi-tui";
25
- import {
26
- discoverElectronApps,
27
- ELECTRON_DISCOVERY_DEFAULT_MAX_RESULTS,
28
- ELECTRON_DISCOVERY_MAX_RESULTS,
29
- type ElectronDiscoveryResult,
30
- } from "./lib/electron/discovery.js";
31
- import {
32
- cleanupElectronLaunchResources,
33
- inspectElectronLaunchStatus,
34
- type ElectronCleanupResult,
35
- type ElectronLaunchStatus,
36
- } from "./lib/electron/cleanup.js";
37
- import {
38
- launchElectronApp,
39
- type ElectronCdpTarget,
40
- type ElectronLaunchFailure,
41
- type ElectronLaunchRecord,
42
- type ElectronLaunchSuccess,
43
- } from "./lib/electron/launch.js";
44
25
  import {
45
26
  PROJECT_RULE_PROMPT,
46
27
  buildToolPromptGuidelines,
47
28
  } from "./lib/playbook.js";
48
- import { SAFE_AGENT_BROWSER_OPERATION_TIMEOUT_MS, runAgentBrowserProcess } from "./lib/process.js";
49
29
  import {
50
- buildAgentBrowserNextActions,
51
- buildAgentBrowserResultCategoryDetails,
52
30
  buildToolPresentation,
53
- getAgentBrowserErrorText,
54
- parseAgentBrowserEnvelope,
55
- type AgentBrowserBatchResult,
56
31
  type AgentBrowserEnvelope,
57
- type AgentBrowserNextAction,
58
32
  type AgentBrowserPageChangeSummary,
59
33
  } from "./lib/results.js";
60
- import {
61
- SessionPageState,
62
- buildNoActivePageRefSnapshotInvalidation,
63
- commandExplicitlyTargetsAboutBlank,
64
- deriveSessionTabTarget,
65
- extractLatestRefSnapshotStateFromBatchResults,
66
- extractRefSnapshotFromData,
67
- extractSessionTabTargetFromBatchResults,
68
- extractSessionTabTargetFromCommandData,
69
- isAboutBlankSessionTabTarget,
70
- isAboutBlankUrl,
71
- isNoActivePageSnapshotFailure,
72
- normalizeComparableUrl,
73
- normalizeSessionTabTarget,
74
- targetsMatch,
75
- type SessionRefSnapshot,
76
- type SessionRefSnapshotInvalidation,
77
- type SessionTabTarget,
78
- } from "./lib/session-page-state.js";
34
+ import { SessionPageState } from "./lib/session-page-state.js";
79
35
  import {
80
36
  buildExecutionPlan,
81
- buildPromptPolicy,
82
- chooseOpenResultTabCorrection,
83
37
  createEphemeralSessionSeed,
84
38
  createFreshSessionName,
85
39
  createImplicitSessionName,
86
40
  extractCommandTokens,
87
41
  getImplicitSessionCloseTimeoutMs,
88
42
  getImplicitSessionIdleTimeoutMs,
89
- getLatestUserPrompt,
90
43
  hasLaunchScopedTabCorrectionFlag,
91
44
  hasUsableBraveApiKey,
92
45
  extractExplicitSessionName,
93
46
  redactInvocationArgs,
94
- redactSensitiveText,
95
- redactSensitiveValue,
96
47
  restoreManagedSessionStateFromBranch,
97
48
  resolveManagedSessionState,
98
- shouldAppendBrowserSystemPrompt,
99
49
  validateToolArgs,
100
- type CommandInfo,
101
50
  type CompatibilityWorkaround,
102
- type OpenResultTabCorrection,
103
51
  } from "./lib/runtime.js";
52
+ import { buildPromptPolicy, getLatestUserPrompt, shouldAppendBrowserSystemPrompt } from "./lib/prompt-policy.js";
53
+ import { isCloseCommand } from "./lib/command-taxonomy.js";
104
54
  import {
105
55
  cleanupSecureTempArtifacts,
106
56
  type PersistentSessionArtifactEviction,
@@ -135,7 +85,17 @@ import {
135
85
  type CompiledAgentBrowserSemanticAction,
136
86
  type CompiledAgentBrowserSourceLookup,
137
87
  } from "./lib/input-modes.js";
138
- import { runAgentBrowserTool, type BrowserRunState } from "./lib/orchestration/browser-run.js";
88
+ import { closeManagedSession, runAgentBrowserTool, type BrowserRunState, type TraceOwner } from "./lib/orchestration/browser-run.js";
89
+ import { findElectronLaunchRecordForSession, getActiveElectronRecords } from "./lib/orchestration/browser-run/session-state.js";
90
+ import { parseBatchStdinJsonArray } from "./lib/orchestration/batch-stdin.js";
91
+ import {
92
+ ELECTRON_POST_COMMAND_STATUS_SETTLE_MS,
93
+ ELECTRON_PROFILE_ISOLATION_DETAILS,
94
+ cleanupActiveElectronHostLaunches,
95
+ handleElectronHostInput,
96
+ restoreElectronLaunchRecordsFromBranch,
97
+ type ElectronLaunchRecord,
98
+ } from "./lib/orchestration/electron-host/index.js";
139
99
  import { buildValidationFailureResult, resolveAgentBrowserInput } from "./lib/orchestration/input-plan.js";
140
100
  import type { SessionArtifactManifest } from "./lib/results/contracts.js";
141
101
  import {
@@ -157,99 +117,12 @@ import {
157
117
  type RichInputRecoveryDiagnostic,
158
118
  type VisibleRefFallbackDiagnostic,
159
119
  } from "./lib/results/selector-recovery.js";
160
- import {
161
- AgentBrowserNextActionCollector,
162
- alignPageChangeSummaryNextActionIds,
163
- appendUniqueAgentBrowserNextActions,
164
- isStandaloneSnapshotNextAction,
165
- withOptionalSessionArgs,
166
- } from "./lib/results/next-actions.js";
167
- import {
168
- buildConnectedSessionNextActions,
169
- buildNoActivePageNextActions,
170
- buildSessionAwareStaleRefNextActions,
171
- buildSessionTabRecoveryNextActions,
172
- } from "./lib/results/recovery-next-actions.js";
120
+ import { withOptionalSessionArgs } from "./lib/results/next-actions.js";
173
121
 
174
122
  const DEFAULT_SESSION_MODE = "auto" as const;
175
123
  const DIRECT_AGENT_BROWSER_BASH_BYPASS_ENV = "PI_AGENT_BROWSER_ALLOW_DIRECT_BASH";
176
124
  const PACKAGE_NAME = "pi-agent-browser-native";
177
125
 
178
- const ELECTRON_PROFILE_ISOLATION_NOTE = "Profile note: electron.launch starts an isolated temporary profile; it does not reuse the app's normal signed-in profile or attach to an already-running authenticated app.";
179
- const ELECTRON_EXISTING_AUTH_GUIDANCE = "For already-authenticated desktop app content, do not stop here: if host tools are allowed and the app is not running, launch the normal app with --remote-debugging-port=<port>, verify the port, then run agent_browser connect <port>; if it is already running without a debug port, ask before relaunching it.";
180
- const ELECTRON_PROFILE_ISOLATION_DETAILS = {
181
- attachesToAlreadyRunningApp: false,
182
- existingAuthenticatedAppGuidance: ELECTRON_EXISTING_AUTH_GUIDANCE,
183
- hostDebugLaunchExample: "macOS: open -a <App Name> --args --remote-debugging-port=9222 --remote-allow-origins='*'; then agent_browser connect 9222 with sessionMode=fresh",
184
- isolatedLaunch: true,
185
- note: ELECTRON_PROFILE_ISOLATION_NOTE,
186
- reusesExistingSignedInProfile: false,
187
- } as const;
188
- const ELECTRON_PROBE_MAX_TABS = 6;
189
- const ELECTRON_PROBE_MAX_REF_IDS = 20;
190
- const ELECTRON_PROBE_MAX_SNAPSHOT_LINES = 12;
191
- const ELECTRON_PROBE_MAX_SNAPSHOT_CHARS = 1_600;
192
- const ELECTRON_POST_COMMAND_STATUS_SETTLE_MS = 250;
193
- const ELECTRON_FILL_VERIFICATION_TIMEOUT_MS = 2_000;
194
-
195
- interface ScrollPositionSnapshot {
196
- containerCount: number;
197
- containers: Array<{ id: string; scrollLeft: number; scrollTop: number }>;
198
- innerHeight: number;
199
- innerWidth: number;
200
- scrollHeight: number;
201
- scrollWidth: number;
202
- scrollX: number;
203
- scrollY: number;
204
- }
205
-
206
- interface ScrollNoopDiagnostic {
207
- after: ScrollPositionSnapshot;
208
- before: ScrollPositionSnapshot;
209
- message: string;
210
- reason: "no-observed-scroll-position-change";
211
- recommendations: string[];
212
- }
213
-
214
- interface ComboboxFocusDiagnostic {
215
- activeElement: {
216
- expanded?: string;
217
- hasPopup?: string;
218
- name?: string;
219
- role?: string;
220
- tagName?: string;
221
- };
222
- message: string;
223
- reason: "focused-combobox-without-visible-options";
224
- recommendations: string[];
225
- visibleListboxCount: number;
226
- visibleOptionCount: number;
227
- }
228
-
229
- interface RecordingDependencyWarning {
230
- command: "record start" | "record restart";
231
- dependency: "ffmpeg";
232
- message: string;
233
- reason: "ffmpeg-missing-for-recording";
234
- recommendations: string[];
235
- }
236
-
237
-
238
-
239
- const SEMANTIC_ACTION_CANDIDATE_ACTION_IDS = new Set([
240
- "try-button-name-candidate",
241
- "try-link-name-candidate",
242
- ]);
243
-
244
-
245
-
246
-
247
- interface SemanticActionVisibleRefResolution {
248
- args: string[];
249
- snapshot: SessionRefSnapshot;
250
- }
251
-
252
-
253
126
  const TUI_COLLAPSED_OUTPUT_MAX_LINES = 10;
254
127
  const TUI_INVOCATION_PREVIEW_MAX_CHARS = 120;
255
128
  const ANSI_CONTROL_SEQUENCE_PATTERN = /\x1B(?:\][^\x07\x1B]*(?:\x07|\x1B\\)|\[[0-?]*[ -/]*[@-~]|P[^\x1B]*(?:\x1B\\)|_[^\x1B]*(?:\x1B\\)|\^[^\x1B]*(?:\x1B\\)|[@-Z\\-_])/g;
@@ -393,6 +266,20 @@ type AgentBrowserToolResultPatch = {
393
266
  isError?: boolean;
394
267
  };
395
268
 
269
+ type OwnedManagedSession = {
270
+ branchOwned: boolean;
271
+ cwd: string;
272
+ };
273
+
274
+ // Event ranks are local to the branch being restored. Keep them out of owned-resource
275
+ // state so branch switches never compare unrelated branch histories.
276
+ interface BranchManagedResourceEvents {
277
+ electronLaunchActiveRanks: Map<string, number>;
278
+ electronLaunchCleanupRanks: Map<string, number>;
279
+ managedSessionActiveRanks: Map<string, number>;
280
+ managedSessionCloseRanks: Map<string, number>;
281
+ }
282
+
396
283
  function agentBrowserToolResultRequestedJson(event: ToolResultEvent): boolean {
397
284
  const details = isRecord(event.details) ? event.details : undefined;
398
285
  const detailArgs = Array.isArray(details?.args) ? details.args : undefined;
@@ -672,332 +559,20 @@ async function isDirectAgentBrowserBashAllowed(cwd: string): Promise<boolean> {
672
559
  return isTruthyEnvValue(process.env[DIRECT_AGENT_BROWSER_BASH_BYPASS_ENV]) || await isPackageDevelopmentCwd(cwd);
673
560
  }
674
561
 
675
- const NAVIGATION_SUMMARY_COMMANDS = new Set(["back", "click", "dblclick", "forward", "reload"]);
676
- const NAVIGATION_SUMMARY_EVAL = `({ title: document.title, url: location.href })`;
677
- // These commands can expose URLs for inspected resources (request URLs, cookie/storage scope, or log sources),
678
- // but they do not navigate the active tab and must not poison page-scoped ref guards.
679
- const READ_ONLY_DIAGNOSTIC_SESSION_TARGET_COMMANDS = new Set(["console", "cookies", "errors", "network", "storage"]);
680
-
681
- interface NavigationSummary {
682
- title?: string;
683
- url?: string;
684
- }
685
-
686
- interface OverlayBlockerCandidate {
687
- args: string[];
688
- name?: string;
689
- reason: string;
690
- ref: string;
691
- role?: string;
692
- }
693
-
694
- interface OverlayBlockerDiagnostic {
695
- candidates: OverlayBlockerCandidate[];
696
- snapshot: SessionRefSnapshot;
697
- summary: string;
698
- }
699
-
700
- interface SelectorTextVisibilityDiagnostic {
701
- firstMatchVisible?: boolean;
702
- firstVisibleTextPreview?: string;
703
- matchCount: number;
704
- selector: string;
705
- summary: string;
706
- visibleCount: number;
707
- }
708
-
709
- interface ElectronBroadGetTextScopeDiagnostic {
710
- electronContext: {
711
- launchId?: string;
712
- sessionName?: string;
713
- url?: string;
714
- };
715
- selector: string;
716
- summary: string;
717
- }
718
-
719
- interface QaAttachedTarget {
720
- error?: string;
721
- sessionName: string;
722
- title?: string;
723
- url?: string;
724
- }
725
-
726
- interface TimeoutArtifactEvidence {
727
- absolutePath: string;
728
- exists: boolean;
729
- path: string;
730
- sizeBytes?: number;
731
- stepIndex: number;
732
- }
733
-
734
- interface TimeoutPartialProgress {
735
- artifacts: TimeoutArtifactEvidence[];
736
- currentPage?: {
737
- title?: string;
738
- url?: string;
739
- };
740
- steps?: Array<{ args: string[]; index: number }>;
741
- summary: string;
742
- }
743
-
744
- interface EvalStdinHint {
745
- reason: string;
746
- suggestion: string;
747
- }
748
-
749
- interface ArtifactCleanupGuidance {
750
- explicitArtifactPaths: string[];
751
- note: string;
752
- owner: "host-file-tools";
753
- summary: string;
754
- }
755
-
756
- interface ManagedSessionOutcome {
757
- activeAfter: boolean;
758
- activeBefore: boolean;
759
- attemptedSessionName?: string;
760
- currentSessionName: string;
761
- previousSessionName: string;
762
- replacedSessionName?: string;
763
- sessionMode: "auto" | "fresh";
764
- status: "abandoned" | "closed" | "created" | "preserved" | "replaced" | "unchanged";
765
- succeeded: boolean;
766
- summary: string;
767
- }
768
-
769
562
  function isRecord(value: unknown): value is Record<string, unknown> {
770
563
  return typeof value === "object" && value !== null;
771
564
  }
772
565
 
773
- const SCREENSHOT_VALUE_FLAGS = new Set(["--screenshot-dir", "--screenshot-format", "--screenshot-quality"]);
774
- const SCREENSHOT_IMAGE_EXTENSIONS = new Set([".jpeg", ".jpg", ".png", ".webp"]);
775
-
776
- interface ScreenshotPathRequest {
777
- absolutePath: string;
778
- path: string;
779
- }
780
-
781
- interface PreparedAgentBrowserArgs {
782
- args: string[];
783
- batchScreenshotPathRequests?: Array<ScreenshotPathRequest | undefined>;
784
- screenshotPathRequest?: ScreenshotPathRequest;
785
- stdin?: string;
786
- }
787
-
788
- interface ScreenshotArtifactRequest extends ScreenshotPathRequest {
789
- status?: "missing" | "repaired-from-temp" | "saved" | "upstream-temp-only";
790
- tempPath?: string;
791
- }
792
-
793
- type TraceOwner = "profiler" | "trace";
794
-
795
- function isImagePathToken(token: string): boolean {
796
- const extension = extname(token).toLowerCase();
797
- return SCREENSHOT_IMAGE_EXTENSIONS.has(extension);
798
- }
799
-
800
- function getScreenshotPathTokenIndex(commandTokens: string[]): number | undefined {
801
- if (commandTokens[0] !== "screenshot") {
802
- return undefined;
803
- }
804
-
805
- const positionalIndices: number[] = [];
806
- for (let index = 1; index < commandTokens.length; index += 1) {
807
- const token = commandTokens[index];
808
- if (token === "--") {
809
- for (let positionalIndex = index + 1; positionalIndex < commandTokens.length; positionalIndex += 1) {
810
- positionalIndices.push(positionalIndex);
811
- }
812
- break;
813
- }
814
- if (token.startsWith("-")) {
815
- const normalizedToken = token.split("=", 1)[0] ?? token;
816
- if (SCREENSHOT_VALUE_FLAGS.has(normalizedToken) && !token.includes("=")) {
817
- index += 1;
818
- }
819
- continue;
820
- }
821
- positionalIndices.push(index);
822
- }
823
-
824
- if (positionalIndices.length === 0) {
825
- return undefined;
826
- }
827
- const candidateIndex = positionalIndices[positionalIndices.length - 1];
828
- const candidate = commandTokens[candidateIndex];
829
- if (positionalIndices.length >= 2 || isImagePathToken(candidate) || isAbsolute(candidate) || candidate.startsWith("./") || candidate.startsWith("../")) {
830
- return candidateIndex;
831
- }
832
- return undefined;
833
- }
834
-
835
- async function normalizeScreenshotPathInTokens(commandTokens: string[], cwd: string): Promise<{
836
- request?: ScreenshotPathRequest;
837
- tokens: string[];
838
- }> {
839
- const screenshotPathTokenIndex = getScreenshotPathTokenIndex(commandTokens);
840
- if (screenshotPathTokenIndex === undefined) {
841
- return { tokens: commandTokens };
842
- }
843
-
844
- const requestedPath = commandTokens[screenshotPathTokenIndex];
845
- const absolutePath = resolve(cwd, requestedPath);
846
- await mkdir(dirname(absolutePath), { recursive: true });
847
-
848
- const tokens = [...commandTokens];
849
- tokens[screenshotPathTokenIndex] = absolutePath;
850
- const terminatorIndex = tokens.indexOf("--");
851
- if (terminatorIndex >= 0) {
852
- tokens.splice(terminatorIndex, 1);
853
- }
854
-
855
- return {
856
- request: {
857
- absolutePath,
858
- path: requestedPath,
859
- },
860
- tokens,
861
- };
862
- }
863
-
864
- async function prepareBatchScreenshotPaths(args: string[], stdin: string | undefined, cwd: string): Promise<PreparedAgentBrowserArgs | undefined> {
865
- const commandTokens = extractCommandTokens(args);
866
- if (commandTokens[0] !== "batch" || stdin === undefined) {
867
- return undefined;
868
- }
869
- let steps: unknown;
870
- try {
871
- steps = JSON.parse(stdin);
872
- } catch {
873
- return undefined;
874
- }
875
- if (!Array.isArray(steps)) {
876
- return undefined;
877
- }
878
-
879
- let changed = false;
880
- const batchScreenshotPathRequests: Array<ScreenshotPathRequest | undefined> = [];
881
- const preparedSteps = await Promise.all(steps.map(async (step, index) => {
882
- if (!Array.isArray(step) || !step.every((item) => typeof item === "string") || step[0] !== "screenshot") {
883
- return step;
884
- }
885
- const normalized = await normalizeScreenshotPathInTokens(step, cwd);
886
- batchScreenshotPathRequests[index] = normalized.request;
887
- if (normalized.request) {
888
- changed = true;
889
- }
890
- return normalized.tokens;
891
- }));
892
-
893
- return changed
894
- ? {
895
- args,
896
- batchScreenshotPathRequests,
897
- stdin: JSON.stringify(preparedSteps),
898
- }
899
- : undefined;
900
- }
901
-
902
- function parseMillisecondsToken(token: string | undefined): number | undefined {
903
- if (token === undefined || !/^\d+$/.test(token)) {
904
- return undefined;
905
- }
906
- const parsed = Number(token);
907
- return Number.isSafeInteger(parsed) ? parsed : undefined;
908
- }
909
-
910
- function findWaitTimeoutMs(commandTokens: string[]): { timeoutMs: number; source: string } | undefined {
911
- if (commandTokens[0] !== "wait") {
912
- return undefined;
913
- }
914
- for (let index = 1; index < commandTokens.length; index += 1) {
915
- const token = commandTokens[index];
916
- if (token === "--timeout") {
917
- const timeoutMs = parseMillisecondsToken(commandTokens[index + 1]);
918
- return timeoutMs === undefined ? undefined : { source: "wait --timeout", timeoutMs };
919
- }
920
- if (token.startsWith("--timeout=")) {
921
- const timeoutMs = parseMillisecondsToken(token.slice("--timeout=".length));
922
- return timeoutMs === undefined ? undefined : { source: "wait --timeout", timeoutMs };
923
- }
924
- if (!token.startsWith("-")) {
925
- const timeoutMs = parseMillisecondsToken(token);
926
- if (timeoutMs !== undefined) {
927
- return { source: "wait", timeoutMs };
928
- }
929
- }
930
- }
931
- return undefined;
932
- }
933
-
934
- function buildIpcUnsafeWaitError(source: string, timeoutMs: number, batchStep?: number): string {
935
- const location = batchStep === undefined ? source : `batch step ${batchStep + 1} (${source})`;
936
- return `${location} requests ${timeoutMs}ms, but upstream agent-browser CLI calls must stay under its 30s IPC read timeout. Use ${SAFE_AGENT_BROWSER_OPERATION_TIMEOUT_MS}ms or less per wait, split long waits into multiple tool calls, or use a page-specific shorter condition.`;
937
- }
938
-
939
-
940
-
941
- async function pathExists(path: string): Promise<boolean> {
942
- try {
943
- await stat(path);
944
- return true;
945
- } catch {
946
- return false;
947
- }
948
- }
949
-
950
- async function repairScreenshotData(options: {
951
- cwd: string;
952
- data: Record<string, unknown>;
953
- request: ScreenshotPathRequest;
954
- }): Promise<{ data: Record<string, unknown>; request: ScreenshotArtifactRequest }> {
955
- const { cwd, data, request } = options;
956
- const reportedPath = typeof data.path === "string" ? data.path : undefined;
957
- const reportedAbsolutePath = reportedPath ? resolve(cwd, reportedPath) : undefined;
958
- let status: ScreenshotArtifactRequest["status"] = await pathExists(request.absolutePath) ? "saved" : "missing";
959
- let tempPath: string | undefined;
960
-
961
- if (reportedAbsolutePath && reportedAbsolutePath !== request.absolutePath) {
962
- tempPath = reportedAbsolutePath;
963
- if (status === "missing" && await pathExists(reportedAbsolutePath)) {
964
- await mkdir(dirname(request.absolutePath), { recursive: true });
965
- await copyFile(reportedAbsolutePath, request.absolutePath);
966
- status = "repaired-from-temp";
967
- }
968
- }
969
-
970
- return {
971
- data: {
972
- ...data,
973
- path: request.absolutePath,
974
- },
975
- request: {
976
- ...request,
977
- status,
978
- tempPath,
979
- },
980
- };
981
- }
982
-
983
-
984
-
985
-
986
566
  function getBatchAnnotateValidationError(args: string[], stdin: string | undefined): string | undefined {
987
567
  const commandTokens = extractCommandTokens(args);
988
568
  if (commandTokens[0] !== "batch" || stdin === undefined) {
989
569
  return undefined;
990
570
  }
991
- let steps: unknown;
992
- try {
993
- steps = JSON.parse(stdin);
994
- } catch {
995
- return undefined;
996
- }
997
- if (!Array.isArray(steps)) {
571
+ const parsed = parseBatchStdinJsonArray(stdin);
572
+ if (parsed.error || parsed.steps === undefined) {
998
573
  return undefined;
999
574
  }
1000
- const badStepIndex = steps.findIndex((step) => Array.isArray(step) && step[0] === "screenshot" && step.includes("--annotate"));
575
+ const badStepIndex = parsed.steps.findIndex((step) => Array.isArray(step) && step[0] === "screenshot" && step.includes("--annotate"));
1001
576
  if (badStepIndex < 0) {
1002
577
  return undefined;
1003
578
  }
@@ -1007,70 +582,6 @@ function getBatchAnnotateValidationError(args: string[], stdin: string | undefin
1007
582
  ].join("\n");
1008
583
  }
1009
584
 
1010
- function getTraceOwner(command: string | undefined): TraceOwner | undefined {
1011
- return command === "trace" || command === "profiler" ? command : undefined;
1012
- }
1013
-
1014
-
1015
-
1016
-
1017
- function extractStringResultField(data: unknown, fieldName: "result" | "title" | "url" | "value"): string | undefined {
1018
- if (typeof data === "string") {
1019
- if (fieldName === "value") return data;
1020
- const text = data.trim();
1021
- return text.length > 0 ? text : undefined;
1022
- }
1023
- if (!isRecord(data) || typeof data[fieldName] !== "string") {
1024
- return undefined;
1025
- }
1026
- if (fieldName === "value") return data[fieldName];
1027
- const text = data[fieldName].trim();
1028
- return text.length > 0 ? text : undefined;
1029
- }
1030
-
1031
- function extractNavigationSummaryFromData(data: unknown): NavigationSummary | undefined {
1032
- const result = isRecord(data) && isRecord(data.result) ? data.result : data;
1033
- const title = extractStringResultField(result, "title");
1034
- const url = extractStringResultField(result, "url");
1035
- return title || url ? { title, url } : undefined;
1036
- }
1037
-
1038
- const SESSION_TAB_PINNING_EXCLUDED_COMMANDS = new Set(["close", "goto", "navigate", "open", "session", "tab"]);
1039
- const SESSION_TAB_POST_COMMAND_CORRECTION_EXCLUDED_COMMANDS = new Set(["batch", "close", "session", "tab"]);
1040
-
1041
- type PinnedBatchUnwrapMode = "single-command" | "user-batch";
1042
-
1043
- type AgentBrowserToolResult = AgentToolResult<unknown> & { isError?: boolean };
1044
-
1045
- type BatchCommandStep = [string, ...string[]];
1046
-
1047
- interface PinnedBatchPlan {
1048
- includeNavigationSummary: boolean;
1049
- steps: BatchCommandStep[];
1050
- unwrapMode: PinnedBatchUnwrapMode;
1051
- }
1052
-
1053
- interface StaleRefPreflight {
1054
- message: string;
1055
- refIds: string[];
1056
- snapshot?: SessionRefSnapshot;
1057
- snapshotInvalidation?: SessionRefSnapshotInvalidation;
1058
- }
1059
-
1060
- interface AboutBlankSessionMismatch {
1061
- activeUrl: "about:blank";
1062
- recoveryApplied: boolean;
1063
- recoveryHint: string;
1064
- targetTitle?: string;
1065
- targetUrl: string;
1066
- }
1067
-
1068
-
1069
-
1070
- function extractBatchResultCommand(item: Record<string, unknown>): string[] {
1071
- return Array.isArray(item.command) ? item.command.filter((token): token is string => typeof token === "string") : [];
1072
- }
1073
-
1074
585
  function restoreArtifactManifestFromBranch(branch: unknown[]): SessionArtifactManifest | undefined {
1075
586
  let restoredManifest: SessionArtifactManifest | undefined;
1076
587
  for (const entry of branch) {
@@ -1085,1869 +596,354 @@ function restoreArtifactManifestFromBranch(branch: unknown[]): SessionArtifactMa
1085
596
  return restoredManifest;
1086
597
  }
1087
598
 
1088
- function isPasswordStdinAuthSave(options: { command?: string; commandTokens: string[] }): boolean {
1089
- return options.command === "auth" && options.commandTokens[1] === "save" && options.commandTokens.includes("--password-stdin");
599
+ function trackOwnedManagedSession(
600
+ sessions: Map<string, OwnedManagedSession>,
601
+ sessionName: string | undefined,
602
+ cwd: string,
603
+ options: { branchOwned?: boolean } = {},
604
+ ): void {
605
+ if (!sessionName) return;
606
+ const existing = sessions.get(sessionName);
607
+ const branchOwned = existing && !existing.branchOwned ? false : options.branchOwned === true;
608
+ sessions.set(sessionName, { branchOwned, cwd });
609
+ }
610
+
611
+ function untrackOwnedManagedSession(sessions: Map<string, OwnedManagedSession>, sessionName: string | undefined): void {
612
+ if (sessionName) sessions.delete(sessionName);
613
+ }
614
+
615
+ function untrackOwnedManagedSessionFromBranchClose(
616
+ sessions: Map<string, OwnedManagedSession>,
617
+ sessionName: string | undefined,
618
+ activeBranchRank: number | undefined,
619
+ closeBranchRank: number | undefined,
620
+ ): void {
621
+ if (!sessionName || closeBranchRank === undefined) return;
622
+ const ownedSession = sessions.get(sessionName);
623
+ if (!ownedSession?.branchOwned) return;
624
+ if (activeBranchRank !== undefined && closeBranchRank <= activeBranchRank) return;
625
+ sessions.delete(sessionName);
626
+ }
627
+
628
+ function syncOwnedManagedSessionsFromResult(sessions: Map<string, OwnedManagedSession>, result: AgentToolResult<unknown>, cwd: string): void {
629
+ const details = isRecord(result.details) ? result.details : undefined;
630
+ const outcome = isRecord(details?.managedSessionOutcome) ? details.managedSessionOutcome : undefined;
631
+ if (!outcome) return;
632
+ const succeeded = outcome.succeeded === true;
633
+ const status = typeof outcome.status === "string" ? outcome.status : undefined;
634
+ const currentSessionName = typeof outcome.currentSessionName === "string" ? outcome.currentSessionName : undefined;
635
+ const attemptedSessionName = typeof outcome.attemptedSessionName === "string" ? outcome.attemptedSessionName : undefined;
636
+ if (succeeded && outcome.activeAfter === true && (status === "created" || status === "replaced" || status === "unchanged")) {
637
+ trackOwnedManagedSession(sessions, currentSessionName, cwd);
638
+ }
639
+ if (succeeded && status === "closed") {
640
+ untrackOwnedManagedSession(sessions, attemptedSessionName ?? currentSessionName);
641
+ }
642
+ }
643
+
644
+ function getTouchedElectronLaunchIds(sessionName: string | undefined, records: Map<string, ElectronLaunchRecord>): Set<string> | undefined {
645
+ const record = findElectronLaunchRecordForSession(sessionName, records);
646
+ return record ? new Set([record.launchId]) : undefined;
647
+ }
648
+
649
+ function mergeActiveElectronLaunchRecords(
650
+ target: Map<string, ElectronLaunchRecord>,
651
+ source: Map<string, ElectronLaunchRecord>,
652
+ options: {
653
+ branchOwnedLaunchIds?: Set<string>;
654
+ markBranchOwned?: boolean;
655
+ touchedLaunchIds?: Set<string>;
656
+ } = {},
657
+ ): void {
658
+ for (const record of getActiveElectronRecords(source)) {
659
+ const alreadyRuntimeOwned = target.has(record.launchId) && options.branchOwnedLaunchIds?.has(record.launchId) === false;
660
+ target.set(record.launchId, record);
661
+ if (options.branchOwnedLaunchIds) {
662
+ if (alreadyRuntimeOwned) {
663
+ // Already runtime-owned from a prior live result; keep it that way.
664
+ } else if (options.markBranchOwned === true) {
665
+ options.branchOwnedLaunchIds.add(record.launchId);
666
+ } else if (options.touchedLaunchIds?.has(record.launchId)) {
667
+ options.branchOwnedLaunchIds.delete(record.launchId);
668
+ }
669
+ }
670
+ }
1090
671
  }
1091
672
 
1092
-
1093
- function redactExactSensitiveText(text: string, sensitiveValues: string[]): string {
1094
- let redacted = text;
1095
- for (const value of sensitiveValues) {
1096
- redacted = redacted.split(value).join("[REDACTED]");
673
+ function removeInactiveOwnedElectronLaunchRecords(
674
+ target: Map<string, ElectronLaunchRecord>,
675
+ branchOwnedLaunchIds: Set<string>,
676
+ source: Map<string, ElectronLaunchRecord>,
677
+ activeBranchRanks: Map<string, number>,
678
+ cleanupBranchRanks: Map<string, number>,
679
+ ): void {
680
+ const activeLaunchIds = new Set(getActiveElectronRecords(source).map((record) => record.launchId));
681
+ const launchIds = new Set([...source.keys(), ...cleanupBranchRanks.keys()]);
682
+ for (const launchId of launchIds) {
683
+ if (!target.has(launchId) || !branchOwnedLaunchIds.has(launchId)) continue;
684
+ const activeBranchRank = activeBranchRanks.get(launchId);
685
+ const cleanupBranchRank = cleanupBranchRanks.get(launchId);
686
+ const restoredInactiveRecord = source.has(launchId) && !activeLaunchIds.has(launchId);
687
+ const cleanupIsLatest = cleanupBranchRank !== undefined && (activeBranchRank === undefined || cleanupBranchRank > activeBranchRank);
688
+ if (!restoredInactiveRecord && !cleanupIsLatest) continue;
689
+ target.delete(launchId);
690
+ branchOwnedLaunchIds.delete(launchId);
691
+ }
692
+ }
693
+
694
+ function mergeElectronLaunchRecordMaps(...maps: Array<Map<string, ElectronLaunchRecord>>): Map<string, ElectronLaunchRecord> {
695
+ const merged = new Map<string, ElectronLaunchRecord>();
696
+ for (const map of maps) {
697
+ for (const [launchId, record] of map) merged.set(launchId, record);
698
+ }
699
+ return merged;
700
+ }
701
+
702
+ function replaceWithActiveElectronLaunchRecords(
703
+ target: Map<string, ElectronLaunchRecord>,
704
+ source: Map<string, ElectronLaunchRecord>,
705
+ branchOwnedLaunchIds?: Set<string>,
706
+ cleanedLaunchIds?: Set<string>,
707
+ ): void {
708
+ target.clear();
709
+ if (branchOwnedLaunchIds) {
710
+ if (cleanedLaunchIds) {
711
+ for (const launchId of cleanedLaunchIds) branchOwnedLaunchIds.delete(launchId);
712
+ } else {
713
+ branchOwnedLaunchIds.clear();
714
+ }
1097
715
  }
1098
- return redacted;
716
+ mergeActiveElectronLaunchRecords(target, source, branchOwnedLaunchIds ? { branchOwnedLaunchIds } : {});
1099
717
  }
1100
718
 
1101
- function redactExactSensitiveValue(value: unknown, sensitiveValues: string[]): unknown {
1102
- if (sensitiveValues.length === 0) {
1103
- return value;
1104
- }
1105
- if (typeof value === "string") {
1106
- return redactExactSensitiveText(value, sensitiveValues);
719
+ function shouldSerializeElectronHostInput(compiledElectron: CompiledAgentBrowserElectron | undefined): boolean {
720
+ return compiledElectron?.action === "status" || compiledElectron?.action === "probe" || compiledElectron?.action === "cleanup";
721
+ }
722
+
723
+ function getElectronHostLaunchRecordsForInput(options: {
724
+ branchRecords: Map<string, ElectronLaunchRecord>;
725
+ compiledElectron: CompiledAgentBrowserElectron | undefined;
726
+ ownedRecords: Map<string, ElectronLaunchRecord>;
727
+ }): Map<string, ElectronLaunchRecord> {
728
+ if (
729
+ options.compiledElectron?.action === "status" ||
730
+ options.compiledElectron?.action === "cleanup" ||
731
+ (options.compiledElectron?.action === "probe" && options.compiledElectron.launchId)
732
+ ) {
733
+ return mergeElectronLaunchRecordMaps(options.branchRecords, options.ownedRecords);
1107
734
  }
1108
- if (Array.isArray(value)) {
1109
- return value.map((item) => redactExactSensitiveValue(item, sensitiveValues));
735
+ return options.branchRecords;
736
+ }
737
+
738
+ function getCleanupResultClosedManagedSessionNames(result: unknown): string[] {
739
+ if (!isRecord(result) || !Array.isArray(result.steps)) return [];
740
+ const closedSessionNames = new Set<string>();
741
+ const record = isRecord(result.record) ? result.record : undefined;
742
+ for (const step of result.steps) {
743
+ if (!isRecord(step) || step.resource !== "managed-session") continue;
744
+ if (step.state !== "removed" && step.state !== "already-gone") continue;
745
+ const sessionName = typeof step.sessionName === "string"
746
+ ? step.sessionName
747
+ : typeof record?.sessionName === "string" ? record.sessionName : undefined;
748
+ if (sessionName) closedSessionNames.add(sessionName);
1110
749
  }
1111
- if (!isRecord(value)) {
1112
- return value;
750
+ return [...closedSessionNames];
751
+ }
752
+
753
+ function getCleanupResultsClosedManagedSessionNames(cleanupResults: unknown[]): string[] {
754
+ const closedSessionNames = new Set<string>();
755
+ for (const result of cleanupResults) {
756
+ for (const sessionName of getCleanupResultClosedManagedSessionNames(result)) closedSessionNames.add(sessionName);
1113
757
  }
1114
- return Object.fromEntries(Object.entries(value).map(([key, entryValue]) => [key, redactExactSensitiveValue(entryValue, sensitiveValues)]));
758
+ return [...closedSessionNames];
1115
759
  }
1116
760
 
1117
- function redactToolDetails(details: Record<string, unknown>, sensitiveValues: string[]): Record<string, unknown> {
1118
- return redactSensitiveValue(redactExactSensitiveValue(details, sensitiveValues)) as Record<string, unknown>;
761
+ function isElectronLaunchRecord(value: unknown): value is ElectronLaunchRecord {
762
+ if (!isRecord(value)) return false;
763
+ return value.version === 1
764
+ && value.launchedByWrapper === true
765
+ && typeof value.launchId === "string"
766
+ && typeof value.appName === "string"
767
+ && typeof value.executablePath === "string"
768
+ && typeof value.userDataDir === "string"
769
+ && typeof value.port === "number"
770
+ && typeof value.createdAtMs === "number";
1119
771
  }
1120
772
 
1121
- function formatElectronListVisibleText(result: ElectronDiscoveryResult): string {
1122
- const visibleApps = result.apps.slice(0, 10);
1123
- const visibleOmittedCount = Math.max(0, result.apps.length - visibleApps.length);
1124
- const header = result.omittedCount > 0
1125
- ? `Electron apps (${result.apps.length} shown, ${result.omittedCount} omitted):`
1126
- : `Electron apps (${result.apps.length} found):`;
1127
- const lines = [header];
1128
- if (visibleApps.length === 0) {
1129
- lines.push(result.query ? `No Electron apps matched query "${result.query}".` : "No Electron apps found in the supported scan locations.");
1130
- } else {
1131
- for (const app of visibleApps) {
1132
- const identifier = app.bundleId ?? app.desktopId;
1133
- const path = app.appPath ?? app.executablePath;
1134
- const sensitivity = app.sensitivity ? ` [likely sensitive: ${app.sensitivity.categories.join(", ")}]` : "";
1135
- lines.push(`- ${app.name}${identifier ? ` (${identifier})` : ""}${sensitivity} — ${path}`);
1136
- }
1137
- }
1138
- if (visibleOmittedCount > 0) {
1139
- lines.push(`${visibleOmittedCount} additional app(s) omitted from visible output; see details.electron.apps.`);
1140
- }
1141
- if (result.omittedCount > 0) {
1142
- lines.push(`${result.omittedCount} app(s) omitted by maxResults=${result.maxResults}.`);
1143
- }
1144
- if (result.apps.some((app) => app.sensitivity?.level === "likely-sensitive")) {
1145
- lines.push("Review likely-sensitive apps and use caller-owned allow/deny policy before launch.");
1146
- lines.push(ELECTRON_PROFILE_ISOLATION_NOTE);
1147
- lines.push(ELECTRON_EXISTING_AUTH_GUIDANCE);
1148
- }
1149
- return lines.join("\n");
773
+ function getCleanupResultsElectronRecords(cleanupResults: unknown[]): ElectronLaunchRecord[] {
774
+ return cleanupResults
775
+ .map((result) => isRecord(result) ? result.record : undefined)
776
+ .filter(isElectronLaunchRecord);
1150
777
  }
1151
778
 
1152
- function buildElectronListSuccessResult(compiledElectron: CompiledAgentBrowserElectron, discovery: ElectronDiscoveryResult): AgentBrowserToolResult {
1153
- const text = redactSensitiveText(formatElectronListVisibleText(discovery));
1154
- const sensitiveAppCount = discovery.apps.filter((app) => app.sensitivity?.level === "likely-sensitive").length;
1155
- const details = {
1156
- args: [] as string[],
1157
- compiledElectron,
1158
- electron: {
1159
- action: "list" as const,
1160
- apps: discovery.apps,
1161
- maxResults: discovery.maxResults,
1162
- omittedCount: discovery.omittedCount || undefined,
1163
- platform: discovery.platform,
1164
- profileIsolation: ELECTRON_PROFILE_ISOLATION_DETAILS,
1165
- query: discovery.query,
1166
- sensitiveAppCount: sensitiveAppCount || undefined,
1167
- skippedCount: discovery.skippedCount,
1168
- status: "succeeded" as const,
1169
- },
1170
- ...buildAgentBrowserResultCategoryDetails({ args: [], succeeded: true }),
1171
- summary: discovery.omittedCount > 0
1172
- ? `Electron app discovery found ${discovery.apps.length} app(s) and omitted ${discovery.omittedCount}.`
1173
- : `Electron app discovery found ${discovery.apps.length} app(s).`,
1174
- };
1175
- return {
1176
- content: [{ type: "text", text }],
1177
- details: redactToolDetails(details, []),
1178
- isError: false,
1179
- };
779
+ function mergeElectronCleanupRecords(target: Map<string, ElectronLaunchRecord>, cleanupResults: unknown[]): void {
780
+ for (const record of getCleanupResultsElectronRecords(cleanupResults)) {
781
+ target.set(record.launchId, record);
782
+ }
1180
783
  }
1181
784
 
1182
- function buildElectronListFailureResult(compiledElectron: CompiledAgentBrowserElectron | undefined, error: unknown): AgentBrowserToolResult {
1183
- const errorText = error instanceof Error ? error.message : String(error);
1184
- const text = redactSensitiveText(`Electron app discovery failed: ${errorText}`);
1185
- const details = {
1186
- args: [] as string[],
1187
- compiledElectron,
1188
- electron: {
1189
- action: "list" as const,
1190
- error: errorText,
1191
- status: "failed" as const,
1192
- },
1193
- ...buildAgentBrowserResultCategoryDetails({ args: [], errorText, succeeded: false }),
1194
- summary: "Electron app discovery failed.",
1195
- };
1196
- return {
1197
- content: [{ type: "text", text }],
1198
- details: redactToolDetails(details, []),
1199
- isError: true,
1200
- };
785
+ function getManagedSessionOutcome(details: Record<string, unknown>): Record<string, unknown> | undefined {
786
+ return isRecord(details.managedSessionOutcome) ? details.managedSessionOutcome : undefined;
1201
787
  }
1202
788
 
1203
- interface ElectronHandoffSummary {
1204
- error?: string;
1205
- handoff: "connect" | "snapshot" | "tabs";
1206
- refSnapshot?: SessionRefSnapshot;
1207
- snapshot?: unknown;
1208
- snapshotRetryCount?: number;
1209
- tabs?: unknown;
789
+ function getSuccessfulToolResult(details: Record<string, unknown>, message: Record<string, unknown>): boolean {
790
+ const messageIsError = typeof message.isError === "boolean" ? message.isError : undefined;
791
+ const exitCode = typeof details.exitCode === "number" ? details.exitCode : undefined;
792
+ return messageIsError === undefined ? exitCode === undefined || exitCode === 0 : !messageIsError;
1210
793
  }
1211
794
 
1212
- function isElectronLaunchRecord(value: unknown): value is ElectronLaunchRecord {
1213
- if (!isRecord(value)) return false;
1214
- return value.version === 1 &&
1215
- value.launchedByWrapper === true &&
1216
- typeof value.launchId === "string" &&
1217
- typeof value.appName === "string" &&
1218
- typeof value.executablePath === "string" &&
1219
- typeof value.userDataDir === "string" &&
1220
- typeof value.port === "number" &&
1221
- typeof value.createdAtMs === "number";
795
+ function setBranchRankForString(map: Map<string, number>, value: unknown, rank: number): void {
796
+ if (typeof value === "string" && value.length > 0) map.set(value, rank);
1222
797
  }
1223
798
 
1224
- function restoreElectronLaunchRecordsFromBranch(branch: unknown[]): Map<string, ElectronLaunchRecord> {
1225
- const records = new Map<string, ElectronLaunchRecord>();
799
+ function collectBranchManagedResourceEvents(branch: unknown[]): BranchManagedResourceEvents {
800
+ const events: BranchManagedResourceEvents = {
801
+ electronLaunchActiveRanks: new Map<string, number>(),
802
+ electronLaunchCleanupRanks: new Map<string, number>(),
803
+ managedSessionActiveRanks: new Map<string, number>(),
804
+ managedSessionCloseRanks: new Map<string, number>(),
805
+ };
806
+ let eventRank = 0;
1226
807
  for (const entry of branch) {
1227
808
  if (!isRecord(entry) || entry.type !== "message") continue;
1228
809
  const message = isRecord(entry.message) ? entry.message : undefined;
1229
810
  if (!message || message.toolName !== "agent_browser") continue;
1230
811
  const details = isRecord(message.details) ? message.details : undefined;
1231
- const electron = isRecord(details?.electron) ? details.electron : undefined;
1232
- if (!electron) continue;
1233
- const launch = isElectronLaunchRecord(electron.launch) ? electron.launch : undefined;
1234
- if (launch) records.set(launch.launchId, launch);
1235
- const cleanupRecords = isRecord(electron.cleanup) && Array.isArray(electron.cleanup.records) ? electron.cleanup.records : [];
812
+ if (!details) continue;
813
+ eventRank += 1;
814
+ const succeeded = getSuccessfulToolResult(details, message);
815
+ const args = Array.isArray(details.args) && details.args.every((arg) => typeof arg === "string") ? details.args : [];
816
+ const command = typeof details.command === "string" ? details.command : extractCommandTokens(args)[0];
817
+ const sessionName = typeof details.sessionName === "string" ? details.sessionName : undefined;
818
+ const sessionMode = details.sessionMode === "fresh" || details.sessionMode === "auto" ? details.sessionMode : undefined;
819
+ const usedImplicitSession = details.usedImplicitSession === true;
820
+ const explicitSessionName = extractExplicitSessionName(args);
821
+ const outcome = getManagedSessionOutcome(details);
822
+ const outcomeSucceeded = outcome?.succeeded === true;
823
+ const outcomeStatus = typeof outcome?.status === "string" ? outcome.status : undefined;
824
+ const outcomeCurrentSessionName = typeof outcome?.currentSessionName === "string" ? outcome.currentSessionName : undefined;
825
+ const outcomeAttemptedSessionName = typeof outcome?.attemptedSessionName === "string" ? outcome.attemptedSessionName : undefined;
826
+ if (outcomeSucceeded && outcome.activeAfter === true && (outcomeStatus === "created" || outcomeStatus === "replaced" || outcomeStatus === "unchanged")) {
827
+ setBranchRankForString(events.managedSessionActiveRanks, outcomeCurrentSessionName, eventRank);
828
+ }
829
+ if (outcomeSucceeded && outcomeStatus === "closed") {
830
+ setBranchRankForString(events.managedSessionCloseRanks, outcomeAttemptedSessionName ?? outcomeCurrentSessionName ?? sessionName, eventRank);
831
+ }
832
+ if (outcomeSucceeded && outcomeStatus === "replaced") {
833
+ setBranchRankForString(events.managedSessionCloseRanks, outcome.replacedSessionName, eventRank);
834
+ }
835
+ if (succeeded && !isCloseCommand(command) && sessionName && (usedImplicitSession || sessionMode === "fresh")) {
836
+ events.managedSessionActiveRanks.set(sessionName, eventRank);
837
+ }
838
+ if (succeeded && isCloseCommand(command)) {
839
+ setBranchRankForString(events.managedSessionCloseRanks, explicitSessionName ?? sessionName ?? outcomeAttemptedSessionName ?? outcomeCurrentSessionName, eventRank);
840
+ }
841
+
842
+ const electron = isRecord(details.electron) ? details.electron : undefined;
843
+ const launch = electron && isElectronLaunchRecord(electron.launch) ? electron.launch : undefined;
844
+ if (launch && getActiveElectronRecords(new Map([[launch.launchId, launch]])).length > 0) {
845
+ events.electronLaunchActiveRanks.set(launch.launchId, eventRank);
846
+ }
847
+ const cleanup = isRecord(electron?.cleanup) ? electron.cleanup : undefined;
848
+ const cleanupRecords = Array.isArray(cleanup?.records) ? cleanup.records : [];
1236
849
  for (const cleanupRecord of cleanupRecords) {
1237
- if (isElectronLaunchRecord(cleanupRecord)) records.set(cleanupRecord.launchId, cleanupRecord);
850
+ if (isElectronLaunchRecord(cleanupRecord)) events.electronLaunchCleanupRanks.set(cleanupRecord.launchId, eventRank);
851
+ }
852
+ const cleanupResults = Array.isArray(cleanup?.results) ? cleanup.results : [];
853
+ for (const cleanupResult of cleanupResults) {
854
+ if (isRecord(cleanupResult) && isElectronLaunchRecord(cleanupResult.record)) {
855
+ events.electronLaunchCleanupRanks.set(cleanupResult.record.launchId, eventRank);
856
+ }
857
+ for (const closedSessionName of getCleanupResultClosedManagedSessionNames(cleanupResult)) {
858
+ events.managedSessionCloseRanks.set(closedSessionName, eventRank);
859
+ }
1238
860
  }
1239
861
  }
1240
- return records;
862
+ return events;
1241
863
  }
1242
864
 
1243
- function getActiveElectronRecords(records: Map<string, ElectronLaunchRecord>): ElectronLaunchRecord[] {
1244
- return [...records.values()].filter((record) => record.cleanupState === "active" || record.cleanupState === "dead" || record.cleanupState === "partial" || record.cleanupState === "failed");
1245
- }
1246
-
1247
- function selectElectronRecords(compiledElectron: Extract<CompiledAgentBrowserElectron, { action: "cleanup" | "status" }>, records: Map<string, ElectronLaunchRecord>): { error?: string; records?: ElectronLaunchRecord[] } {
1248
- if (compiledElectron.launchId) {
1249
- const record = records.get(compiledElectron.launchId);
1250
- return record ? { records: [record] } : { error: `No wrapper-tracked Electron launch found for launchId ${compiledElectron.launchId}.` };
865
+ function getCleanupResultsPreservedUserDataDirs(cleanupResults: unknown[]): string[] {
866
+ const userDataDirs = new Set<string>();
867
+ for (const result of cleanupResults) {
868
+ if (!isRecord(result) || !Array.isArray(result.steps) || !isElectronLaunchRecord(result.record)) continue;
869
+ const userDataDirStep = result.steps.find((step) => isRecord(step) && step.resource === "user-data-dir");
870
+ if (!isRecord(userDataDirStep)) continue;
871
+ if (userDataDirStep.state === "skipped" || userDataDirStep.state === "failed") userDataDirs.add(result.record.userDataDir);
1251
872
  }
1252
- if (compiledElectron.all) return { records: getActiveElectronRecords(records) };
1253
- const activeRecords = getActiveElectronRecords(records);
1254
- if (activeRecords.length === 0) return { records: [] };
1255
- if (activeRecords.length > 1) return { error: "Multiple wrapper-tracked Electron launches are active; pass electron.launchId or electron.all." };
1256
- return { records: activeRecords };
873
+ return [...userDataDirs];
1257
874
  }
1258
875
 
1259
- function formatElectronTargetLines(targets: ElectronCdpTarget[], limit = 8): string[] {
1260
- const shownTargets = targets.slice(0, limit);
1261
- const lines = shownTargets.map((target) => {
1262
- const label = [target.type, target.title].filter(Boolean).join(" ") || target.id || "target";
1263
- return `- ${label}${target.url ? ` — ${target.url}` : ""}`;
1264
- });
1265
- if (targets.length > shownTargets.length) lines.push(`- ... ${targets.length - shownTargets.length} more target(s) omitted`);
1266
- return lines;
876
+ function syncElectronCleanupManagedSessions(sessions: Map<string, OwnedManagedSession>, cleanupResults: unknown[]): void {
877
+ for (const sessionName of getCleanupResultsClosedManagedSessionNames(cleanupResults)) {
878
+ untrackOwnedManagedSession(sessions, sessionName);
879
+ }
1267
880
  }
1268
881
 
1269
- function extractTargetsFromStatus(statuses: ElectronLaunchStatus[]): ElectronCdpTarget[] {
1270
- return statuses.flatMap((status) => status.targets);
882
+ async function closeOwnedManagedSessionsExcept(sessions: Map<string, OwnedManagedSession>, keepSessionName: string | undefined, timeoutMs: number): Promise<void> {
883
+ for (const [sessionName, owner] of [...sessions]) {
884
+ if (sessionName === keepSessionName) continue;
885
+ const error = await closeManagedSession({ cwd: owner.cwd, sessionName, timeoutMs });
886
+ if (!error) sessions.delete(sessionName);
887
+ }
1271
888
  }
1272
889
 
1273
- interface ElectronManagedSessionTarget {
1274
- error?: string;
1275
- sessionName: string;
1276
- title?: string;
1277
- url?: string;
890
+ async function closeOwnedManagedSessions(sessions: Map<string, OwnedManagedSession>, timeoutMs: number): Promise<void> {
891
+ await closeOwnedManagedSessionsExcept(sessions, undefined, timeoutMs);
1278
892
  }
1279
893
 
1280
- type ElectronSessionMismatchReason =
1281
- | "launch-session-not-current"
1282
- | "managed-session-about-blank-while-launch-target-live"
1283
- | "managed-session-target-not-in-launch-status";
1284
-
1285
- interface ElectronSessionMismatch {
1286
- launchId: string;
1287
- liveTarget?: ElectronCdpTarget;
1288
- managedSession: ElectronManagedSessionTarget;
1289
- nextActionIds: string[];
1290
- reason: ElectronSessionMismatchReason;
1291
- sessionName?: string;
1292
- statusTargets: ElectronCdpTarget[];
1293
- summary: string;
894
+ function getOffBranchOwnedElectronLaunchRecords(ownedRecords: Map<string, ElectronLaunchRecord>, branchRecords: Map<string, ElectronLaunchRecord>): Map<string, ElectronLaunchRecord> {
895
+ const activeBranchLaunchIds = new Set(getActiveElectronRecords(branchRecords).map((record) => record.launchId));
896
+ const offBranchRecords = new Map<string, ElectronLaunchRecord>();
897
+ for (const record of getActiveElectronRecords(ownedRecords)) {
898
+ if (!activeBranchLaunchIds.has(record.launchId)) offBranchRecords.set(record.launchId, record);
899
+ }
900
+ return offBranchRecords;
1294
901
  }
1295
902
 
1296
- type ElectronPostCommandHealthReason = "about-blank-no-live-target" | "debug-port-dead" | "process-dead";
1297
-
1298
- interface ElectronPostCommandHealthDiagnostic {
1299
- appName: string;
1300
- command?: string;
1301
- launchId: string;
1302
- nextActionIds: string[];
1303
- reason: ElectronPostCommandHealthReason;
1304
- sessionName?: string;
1305
- status: ElectronLaunchStatus;
1306
- summary: string;
1307
- target?: SessionTabTarget;
903
+ function shouldSerializeBrowserCommand(options: {
904
+ explicitSessionName?: string;
905
+ managedSessionName: string;
906
+ ownedElectronLaunchRecords: Map<string, ElectronLaunchRecord>;
907
+ ownedManagedSessions: Map<string, OwnedManagedSession>;
908
+ }): boolean {
909
+ if (!options.explicitSessionName) return true;
910
+ if (options.explicitSessionName === options.managedSessionName) return true;
911
+ if (options.ownedManagedSessions.has(options.explicitSessionName)) return true;
912
+ return getActiveElectronRecords(options.ownedElectronLaunchRecords).some((record) => record.sessionName === options.explicitSessionName);
1308
913
  }
1309
914
 
1310
- interface FillVerificationDiagnostic {
1311
- actual?: string;
1312
- expected: string;
1313
- nextActionIds: string[];
1314
- selector: string;
1315
- status: "mismatch";
1316
- summary: string;
1317
- }
915
+ // Serializes managed-session read/modify/write work so overlapping tool calls cannot promote stale state or close an in-use session.
916
+ class AsyncExecutionQueue {
917
+ private tail: Promise<void> = Promise.resolve();
1318
918
 
1319
- interface ElectronRefFreshnessDiagnostic {
1320
- command?: string;
1321
- launchId: string;
1322
- nextActionIds: string[];
1323
- sessionName?: string;
1324
- summary: string;
1325
- }
919
+ run<T>(work: () => Promise<T>): Promise<T> {
920
+ const previous = this.tail;
921
+ let release!: () => void;
922
+ this.tail = new Promise<void>((resolve) => {
923
+ release = resolve;
924
+ });
1326
925
 
1327
- interface ElectronProbeContext {
1328
- launchId?: string;
1329
- mode: "current-managed-session" | "launchId";
1330
- note?: string;
1331
- sessionName: string;
926
+ return (async () => {
927
+ await previous;
928
+ try {
929
+ return await work();
930
+ } finally {
931
+ release();
932
+ }
933
+ })();
934
+ }
1332
935
  }
1333
936
 
1334
- function isLiveElectronRendererTarget(target: ElectronCdpTarget): boolean {
1335
- const normalizedUrl = normalizeComparableUrl(target.url);
1336
- if (!normalizedUrl || normalizedUrl === "about:blank" || normalizedUrl.startsWith("devtools://")) return false;
1337
- return target.type === undefined || target.type === "page" || target.type === "webview";
937
+ function getInstalledDocsPaths(): { readmePath: string; commandReferencePath: string; toolContractPath: string } {
938
+ const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
939
+ return {
940
+ readmePath: join(packageRoot, "README.md"),
941
+ commandReferencePath: join(packageRoot, "docs", "COMMAND_REFERENCE.md"),
942
+ toolContractPath: join(packageRoot, "docs", "TOOL_CONTRACT.md"),
943
+ };
1338
944
  }
1339
945
 
1340
- function getLiveElectronRendererTargets(targets: ElectronCdpTarget[]): ElectronCdpTarget[] {
1341
- return targets.filter(isLiveElectronRendererTarget);
1342
- }
1343
946
 
1344
- function electronTargetLabel(target: ElectronCdpTarget | undefined): string {
1345
- if (!target) return "unknown target";
1346
- return [target.title, target.url, target.id].find((value) => typeof value === "string" && value.trim().length > 0) ?? "unknown target";
1347
- }
1348
-
1349
- function findElectronLaunchRecordForSession(sessionName: string | undefined, records: Map<string, ElectronLaunchRecord>): ElectronLaunchRecord | undefined {
1350
- if (!sessionName) return undefined;
1351
- return getActiveElectronRecords(records).find((record) => record.sessionName === sessionName);
1352
- }
1353
-
1354
- function findUnambiguousActiveElectronLaunchRecord(records: Map<string, ElectronLaunchRecord>): ElectronLaunchRecord | undefined {
1355
- const activeRecords = getActiveElectronRecords(records);
1356
- return activeRecords.length === 1 ? activeRecords[0] : undefined;
1357
- }
1358
-
1359
- function buildElectronReattachNextAction(record: ElectronLaunchRecord, liveTarget?: ElectronCdpTarget): AgentBrowserNextAction {
1360
- const endpoint = liveTarget?.webSocketDebuggerUrl ?? record.webSocketDebuggerUrl ?? String(record.port);
1361
- return {
1362
- id: "reattach-electron-launch",
1363
- params: { args: ["connect", endpoint], sessionMode: "fresh" },
1364
- reason: "Attach a fresh managed session to the same wrapper-tracked Electron debug endpoint when the current session no longer matches the live renderer.",
1365
- safety: "Creates a new managed browser session; it does not mutate the Electron app. Keep the launchId for later status and cleanup.",
1366
- tool: "agent_browser",
1367
- };
1368
- }
1369
-
1370
- function buildElectronMismatchNextActions(record: ElectronLaunchRecord, liveTarget?: ElectronCdpTarget): AgentBrowserNextAction[] {
1371
- const baseActions = buildAgentBrowserNextActions({
1372
- electron: { launchId: record.launchId, sessionName: record.sessionName, status: record.cleanupState },
1373
- resultCategory: "success",
1374
- successCategory: "completed",
1375
- }) ?? [];
1376
- const reattachAction = buildElectronReattachNextAction(record, liveTarget);
1377
- const actions: AgentBrowserNextAction[] = [];
1378
- for (const action of baseActions) {
1379
- actions.push(action);
1380
- if (action.id === "probe-electron-launch") actions.push(reattachAction);
1381
- }
1382
- if (!actions.some((action) => action.id === reattachAction.id)) actions.push(reattachAction);
1383
- return actions;
1384
- }
1385
-
1386
- function buildElectronSessionMismatch(options: {
1387
- managedSession: ElectronManagedSessionTarget;
1388
- record: ElectronLaunchRecord;
1389
- statusTargets: ElectronCdpTarget[];
1390
- }): ElectronSessionMismatch | undefined {
1391
- const liveTargets = getLiveElectronRendererTargets(options.statusTargets);
1392
- if (liveTargets.length === 0) return undefined;
1393
- const managedUrl = normalizeComparableUrl(options.managedSession.url);
1394
- const matchingLiveTarget = managedUrl
1395
- ? liveTargets.find((target) => normalizeComparableUrl(target.url) === managedUrl)
1396
- : undefined;
1397
- if (matchingLiveTarget) return undefined;
1398
-
1399
- const liveTarget = liveTargets[0];
1400
- let reason: ElectronSessionMismatchReason | undefined;
1401
- if (isAboutBlankUrl(options.managedSession.url)) {
1402
- reason = "managed-session-about-blank-while-launch-target-live";
1403
- } else if (options.record.sessionName && options.record.sessionName !== options.managedSession.sessionName) {
1404
- reason = "launch-session-not-current";
1405
- } else if (managedUrl) {
1406
- reason = "managed-session-target-not-in-launch-status";
1407
- }
1408
- if (!reason) return undefined;
1409
-
1410
- const managedDescription = options.managedSession.url ?? options.managedSession.title ?? options.managedSession.sessionName;
1411
- const liveDescription = electronTargetLabel(liveTarget);
1412
- const summary = reason === "launch-session-not-current"
1413
- ? `Electron session mismatch: current managed session ${options.managedSession.sessionName} is not the wrapper launch session ${options.record.sessionName ?? "unknown"}, while launch ${options.record.launchId} still has live target ${liveDescription}.`
1414
- : `Electron session mismatch: managed session ${options.managedSession.sessionName} is on ${managedDescription}, but launch ${options.record.launchId} still has live target ${liveDescription}.`;
1415
- const nextActions = buildElectronMismatchNextActions(options.record, liveTarget);
1416
- return {
1417
- launchId: options.record.launchId,
1418
- liveTarget,
1419
- managedSession: options.managedSession,
1420
- nextActionIds: nextActions.map((action) => action.id),
1421
- reason,
1422
- sessionName: options.record.sessionName,
1423
- statusTargets: options.statusTargets,
1424
- summary,
1425
- };
1426
- }
1427
-
1428
- async function collectManagedSessionCommandData(options: {
1429
- args: string[];
1430
- cwd: string;
1431
- sessionName: string;
1432
- signal?: AbortSignal;
1433
- timeoutMs?: number;
1434
- }): Promise<{ data?: unknown; error?: string }> {
1435
- try {
1436
- return { data: await runSessionCommandData(options) };
1437
- } catch (error) {
1438
- return { error: error instanceof Error ? error.message : String(error) };
1439
- }
1440
- }
1441
-
1442
- async function collectElectronManagedSessionTarget(options: {
1443
- cwd: string;
1444
- sessionName?: string;
1445
- signal?: AbortSignal;
1446
- timeoutMs?: number;
1447
- }): Promise<ElectronManagedSessionTarget | undefined> {
1448
- if (!options.sessionName) return undefined;
1449
- const [titleResult, urlResult] = await Promise.all([
1450
- collectManagedSessionCommandData({ args: ["get", "title"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, timeoutMs: options.timeoutMs }),
1451
- collectManagedSessionCommandData({ args: ["get", "url"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, timeoutMs: options.timeoutMs }),
1452
- ]);
1453
- const title = boundElectronProbeString(extractStringResultField(titleResult.data, "result") ?? extractStringResultField(titleResult.data, "title"), 160);
1454
- const url = boundElectronProbeString(extractStringResultField(urlResult.data, "result") ?? extractStringResultField(urlResult.data, "url"), 300);
1455
- const errors = [titleResult.error, urlResult.error].filter((value): value is string => value !== undefined);
1456
- return { sessionName: options.sessionName, title, url, ...(errors.length > 0 ? { error: errors.join("; ") } : {}) };
1457
- }
1458
-
1459
- function formatElectronSessionMismatchText(mismatch: ElectronSessionMismatch): string {
1460
- return `${mismatch.summary}\nNext: run electron.status/electron.probe with launchId ${mismatch.launchId}, reattach with the reattach-electron-launch nextAction if needed, or cleanup when finished.`;
1461
- }
1462
-
1463
- const ELECTRON_POST_COMMAND_HEALTH_COMMANDS = new Set([
1464
- "back",
1465
- "check",
1466
- "click",
1467
- "dblclick",
1468
- "fill",
1469
- "find",
1470
- "forward",
1471
- "keyboard",
1472
- "mouse",
1473
- "press",
1474
- "reload",
1475
- "select",
1476
- "type",
1477
- "uncheck",
1478
- ]);
1479
-
1480
- function shouldInspectElectronPostCommandHealth(command: string | undefined): boolean {
1481
- return command !== undefined && ELECTRON_POST_COMMAND_HEALTH_COMMANDS.has(command);
1482
- }
1483
-
1484
- function buildElectronLifecycleNextActions(record: ElectronLaunchRecord): AgentBrowserNextAction[] {
1485
- return buildAgentBrowserNextActions({
1486
- electron: { launchId: record.launchId, sessionName: record.sessionName, status: record.cleanupState },
1487
- resultCategory: "success",
1488
- successCategory: "completed",
1489
- }) ?? [];
1490
- }
1491
-
1492
-
1493
-
1494
- function buildElectronIdentifiers(record: ElectronLaunchRecord): { appName: string; launchId: string; sessionName?: string } {
1495
- return { appName: record.appName, launchId: record.launchId, sessionName: record.sessionName };
1496
- }
1497
-
1498
- function formatElectronStatusVisibleText(statuses: ElectronLaunchStatus[], records: ElectronLaunchRecord[], mismatches: ElectronSessionMismatch[] = [], managedSessions: ElectronManagedSessionTarget[] = []): string {
1499
- if (statuses.length === 0) return "Electron status: no active wrapper-tracked launches.";
1500
- const recordsByLaunchId = new Map(records.map((record) => [record.launchId, record]));
1501
- const managedSessionsByName = new Map(managedSessions.map((managedSession) => [managedSession.sessionName, managedSession]));
1502
- const lines = [`Electron status: ${statuses.length} wrapper-tracked launch(es).`];
1503
- for (const status of statuses) {
1504
- const record = recordsByLaunchId.get(status.launchId);
1505
- const sessionName = record?.sessionName;
1506
- const appName = record?.appName ?? "Electron launch";
1507
- const sessionText = sessionName ? `, sessionName ${sessionName}` : "";
1508
- lines.push(`- ${status.launchId}: ${appName}${sessionText}; ${status.portAlive ? "debug port alive" : "debug port dead"}${status.pidAlive === undefined ? "" : status.pidAlive ? ", pid alive" : ", pid dead"} (port ${status.port})`);
1509
- lines.push(` Identifiers: launchId ${status.launchId}; sessionName ${sessionName ?? "not attached"}.`);
1510
- for (const targetLine of formatElectronTargetLines(status.targets, 4)) lines.push(` ${targetLine}`);
1511
- const managedSession = sessionName ? managedSessionsByName.get(sessionName) : undefined;
1512
- if (managedSession?.error) lines.push(` Managed session warning: ${managedSession.error}`);
1513
- }
1514
- for (const mismatch of mismatches) lines.push("", formatElectronSessionMismatchText(mismatch));
1515
- return lines.join("\n");
1516
- }
1517
-
1518
- function buildElectronStatusResult(options: {
1519
- compiledElectron: CompiledAgentBrowserElectron;
1520
- managedSessions?: ElectronManagedSessionTarget[];
1521
- mismatches?: ElectronSessionMismatch[];
1522
- records: ElectronLaunchRecord[];
1523
- statuses: ElectronLaunchStatus[];
1524
- }): AgentBrowserToolResult {
1525
- const baseNextActions = options.records.flatMap((record) => buildAgentBrowserNextActions({
1526
- electron: { launchId: record.launchId, sessionName: record.sessionName, status: record.cleanupState },
1527
- resultCategory: "success",
1528
- successCategory: "completed",
1529
- }) ?? []);
1530
- const mismatchNextActions = (options.mismatches ?? []).flatMap((mismatch) => {
1531
- const record = options.records.find((candidate) => candidate.launchId === mismatch.launchId);
1532
- return record ? buildElectronMismatchNextActions(record, mismatch.liveTarget) : [];
1533
- });
1534
- const nextActions = options.mismatches?.length
1535
- ? appendUniqueAgentBrowserNextActions([...mismatchNextActions], baseNextActions)
1536
- : appendUniqueAgentBrowserNextActions([...baseNextActions], mismatchNextActions);
1537
- const details = {
1538
- args: [] as string[],
1539
- compiledElectron: options.compiledElectron,
1540
- electron: {
1541
- action: "status" as const,
1542
- identifierList: options.records.length > 1 ? options.records.map(buildElectronIdentifiers) : undefined,
1543
- identifiers: options.records.length === 1 && options.records[0] ? buildElectronIdentifiers(options.records[0]) : undefined,
1544
- launches: options.records,
1545
- managedSession: options.managedSessions?.length === 1 ? options.managedSessions[0] : undefined,
1546
- managedSessions: options.managedSessions && options.managedSessions.length > 0 ? options.managedSessions : undefined,
1547
- sessionMismatch: options.mismatches?.length === 1 ? options.mismatches[0] : undefined,
1548
- sessionMismatches: options.mismatches && options.mismatches.length > 1 ? options.mismatches : undefined,
1549
- status: "succeeded" as const,
1550
- statuses: options.statuses,
1551
- targets: extractTargetsFromStatus(options.statuses),
1552
- },
1553
- nextActions: nextActions.length > 0 ? nextActions : undefined,
1554
- ...buildAgentBrowserResultCategoryDetails({ args: [], succeeded: true }),
1555
- summary: options.statuses.length === 0 ? "Electron status found no active wrapper-tracked launches." : `Electron status inspected ${options.statuses.length} launch(es).`,
1556
- };
1557
- return { content: [{ type: "text", text: redactSensitiveText(formatElectronStatusVisibleText(options.statuses, options.records, options.mismatches, options.managedSessions)) }], details: redactToolDetails(details, []), isError: false };
1558
- }
1559
-
1560
- function formatElectronCleanupVisibleText(results: ElectronCleanupResult[]): string {
1561
- if (results.length === 0) return "Electron cleanup: no active wrapper-tracked launches.";
1562
- const lines = [`Electron cleanup: ${results.filter((result) => !result.partial).length}/${results.length} launch(es) fully cleaned.`];
1563
- for (const result of results) {
1564
- lines.push(`- ${result.summary}`);
1565
- for (const step of result.steps) lines.push(` - ${step.resource}: ${step.state}${step.error ? ` (${step.error})` : ""}`);
1566
- }
1567
- return lines.join("\n");
1568
- }
1569
-
1570
- function buildElectronCleanupResult(compiledElectron: CompiledAgentBrowserElectron, cleanupResults: ElectronCleanupResult[]): AgentBrowserToolResult {
1571
- const partial = cleanupResults.some((result) => result.partial);
1572
- const records = cleanupResults.map((result) => result.record);
1573
- const nextActions = cleanupResults.flatMap((result) => buildAgentBrowserNextActions({
1574
- electron: { launchId: result.launchId, sessionName: result.record.sessionName, status: result.record.cleanupState },
1575
- failureCategory: partial ? "cleanup-failed" : undefined,
1576
- resultCategory: partial ? "failure" : "success",
1577
- successCategory: partial ? undefined : "completed",
1578
- }) ?? []);
1579
- const errorText = partial ? cleanupResults.map((result) => result.summary).join("\n") : undefined;
1580
- const details = {
1581
- args: [] as string[],
1582
- compiledElectron,
1583
- electron: {
1584
- action: "cleanup" as const,
1585
- cleanup: { partial, records, results: cleanupResults },
1586
- status: partial ? "partial" as const : "succeeded" as const,
1587
- },
1588
- nextActions: nextActions.length > 0 ? nextActions : undefined,
1589
- ...buildAgentBrowserResultCategoryDetails({ args: [], errorText, failureCategory: partial ? "cleanup-failed" : undefined, succeeded: !partial }),
1590
- summary: partial ? "Electron cleanup was partial." : "Electron cleanup completed.",
1591
- };
1592
- return { content: [{ type: "text", text: redactSensitiveText(formatElectronCleanupVisibleText(cleanupResults)) }], details: redactToolDetails(details, []), isError: partial };
1593
- }
1594
-
1595
- function formatElectronLaunchFailureDiagnostics(failure: ElectronLaunchFailure | undefined): string | undefined {
1596
- const diagnostics = failure?.diagnostics;
1597
- if (!diagnostics) return undefined;
1598
- const lines = ["Electron launch diagnostics:"];
1599
- if (diagnostics.pid !== undefined) {
1600
- const pidState = diagnostics.pidAlive === undefined ? "state unknown" : diagnostics.pidAlive ? "alive before cleanup" : "not alive before cleanup";
1601
- lines.push(`- PID: ${diagnostics.pid} (${pidState}).`);
1602
- }
1603
- if (diagnostics.exitCode !== undefined || diagnostics.exitSignal !== undefined) {
1604
- const exitParts = [diagnostics.exitCode !== undefined ? `code ${diagnostics.exitCode}` : undefined, diagnostics.exitSignal ? `signal ${diagnostics.exitSignal}` : undefined].filter(Boolean).join(", ");
1605
- lines.push(`- Process exit: ${exitParts || "not observed before cleanup"}.`);
1606
- }
1607
- if (diagnostics.userDataDir) lines.push(`- Wrapper profile: ${diagnostics.userDataDir}`);
1608
- if (diagnostics.devToolsActivePort) {
1609
- const activePort = diagnostics.devToolsActivePort;
1610
- const state = activePort.port
1611
- ? `found port ${activePort.port}`
1612
- : activePort.found
1613
- ? `found but invalid${activePort.error ? ` (${activePort.error})` : ""}`
1614
- : `missing${activePort.error ? ` (${activePort.error})` : ""}`;
1615
- lines.push(`- DevToolsActivePort: ${state} at ${activePort.path}.`);
1616
- }
1617
- if (diagnostics.cdpVersionReached === false) lines.push("- CDP /json/version: did not return a valid payload before timeout.");
1618
- if (diagnostics.timeoutMs !== undefined || diagnostics.elapsedMs !== undefined) {
1619
- lines.push(`- Timing: ${diagnostics.elapsedMs ?? "unknown"}ms elapsed${diagnostics.timeoutMs !== undefined ? ` of ${diagnostics.timeoutMs}ms timeout` : ""}.`);
1620
- }
1621
- if (diagnostics.outputCaptured === false) lines.push("- App stdout/stderr: not captured by this wrapper launch path.");
1622
- lines.push("Retry guidance: increase electron.timeoutMs, try targetType:'any', pass an explicit appPath/executablePath, quit any already-running singleton instance, then retry launch.");
1623
- return lines.join("\n");
1624
- }
1625
-
1626
- function buildElectronHostFailureResult(options: {
1627
- compiledElectron: CompiledAgentBrowserElectron;
1628
- errorText: string;
1629
- failureCategory?: "cleanup-failed" | "policy-blocked" | "timeout" | "upstream-error" | "validation-error";
1630
- launchFailure?: ElectronLaunchFailure;
1631
- managedSessionOutcome?: ManagedSessionOutcome;
1632
- status?: string;
1633
- }): AgentBrowserToolResult {
1634
- const text = [
1635
- options.errorText,
1636
- formatElectronLaunchFailureDiagnostics(options.launchFailure),
1637
- options.launchFailure?.cleanupError ? `Electron launch cleanup warning: ${options.launchFailure.cleanupError}` : undefined,
1638
- ].filter((item): item is string => item !== undefined && item.length > 0).join("\n");
1639
- const details = {
1640
- args: [] as string[],
1641
- compiledElectron: options.compiledElectron,
1642
- electron: {
1643
- action: options.compiledElectron.action,
1644
- error: options.errorText,
1645
- failure: options.launchFailure,
1646
- status: options.status ?? "failed",
1647
- },
1648
- managedSessionOutcome: options.managedSessionOutcome,
1649
- ...buildAgentBrowserResultCategoryDetails({ args: [], errorText: options.errorText, failureCategory: options.failureCategory, succeeded: false, timedOut: options.failureCategory === "timeout" }),
1650
- summary: options.errorText,
1651
- };
1652
- return { content: [{ type: "text", text: redactSensitiveText(text) }], details: redactToolDetails(details, []), isError: true };
1653
- }
1654
-
1655
-
1656
- function sleepMs(ms: number): Promise<void> {
1657
- return new Promise((resolve) => setTimeout(resolve, ms));
1658
- }
1659
-
1660
-
1661
- interface ElectronProbeFocusedElement {
1662
- ariaLabel?: string;
1663
- id?: string;
1664
- isContentEditable?: boolean;
1665
- name?: string;
1666
- placeholder?: string;
1667
- role?: string;
1668
- tagName?: string;
1669
- textLength?: number;
1670
- textPreview?: string;
1671
- title?: string;
1672
- type?: string;
1673
- valueLength?: number;
1674
- }
1675
-
1676
- interface ElectronProbeTab {
1677
- active?: boolean;
1678
- index?: number;
1679
- tabId?: string;
1680
- title?: string;
1681
- type?: string;
1682
- url?: string;
1683
- }
1684
-
1685
- interface ElectronProbeSnapshotSummary {
1686
- lineCount: number;
1687
- omittedLineCount?: number;
1688
- omittedRefCount?: number;
1689
- refCount: number;
1690
- refIds: string[];
1691
- text?: string;
1692
- }
1693
-
1694
- interface ElectronProbeResult {
1695
- activeTab?: ElectronProbeTab;
1696
- errors?: string[];
1697
- focusedElement?: ElectronProbeFocusedElement;
1698
- refSnapshot?: SessionRefSnapshot;
1699
- sessionName: string;
1700
- snapshot?: ElectronProbeSnapshotSummary;
1701
- status: "partial" | "succeeded";
1702
- summary: string;
1703
- tabs?: {
1704
- omittedCount?: number;
1705
- shown: ElectronProbeTab[];
1706
- total: number;
1707
- };
1708
- title?: string;
1709
- url?: string;
1710
- }
1711
-
1712
- const ELECTRON_FOCUSED_ELEMENT_EVAL = `(() => {
1713
- const clean = (value, max = 80) => {
1714
- if (typeof value !== "string") return undefined;
1715
- const normalized = value.replace(/\\s+/g, " ").trim();
1716
- if (!normalized) return undefined;
1717
- return normalized.length > max ? normalized.slice(0, max - 3) + "..." : normalized;
1718
- };
1719
- const describeElement = (element) => {
1720
- if (!element || !(element instanceof Element)) return undefined;
1721
- const tagName = element.tagName.toLowerCase();
1722
- const inputLike = element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement;
1723
- const contentEditable = element instanceof HTMLElement && element.isContentEditable;
1724
- const containerLike = tagName === "body" || tagName === "html";
1725
- const rawText = element.textContent || "";
1726
- const exposeText = !inputLike && !contentEditable && !containerLike;
1727
- const text = exposeText ? clean(rawText) : undefined;
1728
- return {
1729
- tagName: clean(tagName, 40),
1730
- role: clean(element.getAttribute("role") || "", 60),
1731
- name: clean(element.getAttribute("aria-label") || element.getAttribute("title") || text || "", 80),
1732
- id: clean(element.id || "", 80),
1733
- type: clean(element.getAttribute("type") || "", 40),
1734
- placeholder: clean(element.getAttribute("placeholder") || "", 80),
1735
- ariaLabel: clean(element.getAttribute("aria-label") || "", 80),
1736
- title: clean(element.getAttribute("title") || "", 80),
1737
- textLength: !exposeText && rawText ? rawText.length : undefined,
1738
- textPreview: text,
1739
- valueLength: inputLike && typeof element.value === "string" ? element.value.length : undefined,
1740
- isContentEditable: contentEditable || undefined,
1741
- };
1742
- };
1743
- return { focusedElement: describeElement(document.activeElement) };
1744
- })()`;
1745
-
1746
- function boundElectronProbeString(value: string | undefined, maxLength = 240): string | undefined {
1747
- const trimmed = value?.trim();
1748
- if (!trimmed) return undefined;
1749
- return trimmed.length > maxLength ? `${trimmed.slice(0, Math.max(0, maxLength - 3))}...` : trimmed;
1750
- }
1751
-
1752
- function getTrimmedString(value: unknown): string | undefined {
1753
- return typeof value === "string" ? boundElectronProbeString(value) : undefined;
1754
- }
1755
-
1756
- function getOptionalBoolean(value: unknown): boolean | undefined {
1757
- return typeof value === "boolean" ? value : undefined;
1758
- }
1759
-
1760
- function getOptionalNumber(value: unknown): number | undefined {
1761
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
1762
- }
1763
-
1764
- function extractElectronFocusedElement(data: unknown): ElectronProbeFocusedElement | undefined {
1765
- const payload = isRecord(data) && isRecord(data.result) ? data.result : data;
1766
- const rawFocusedElement = isRecord(payload) && isRecord(payload.focusedElement) ? payload.focusedElement : isRecord(payload) ? payload : undefined;
1767
- if (!rawFocusedElement) return undefined;
1768
- const focusedElement: ElectronProbeFocusedElement = {
1769
- ariaLabel: getTrimmedString(rawFocusedElement.ariaLabel),
1770
- id: getTrimmedString(rawFocusedElement.id),
1771
- isContentEditable: getOptionalBoolean(rawFocusedElement.isContentEditable),
1772
- name: getTrimmedString(rawFocusedElement.name),
1773
- placeholder: getTrimmedString(rawFocusedElement.placeholder),
1774
- role: getTrimmedString(rawFocusedElement.role),
1775
- tagName: getTrimmedString(rawFocusedElement.tagName),
1776
- textLength: getOptionalNumber(rawFocusedElement.textLength),
1777
- textPreview: getTrimmedString(rawFocusedElement.textPreview),
1778
- title: getTrimmedString(rawFocusedElement.title),
1779
- type: getTrimmedString(rawFocusedElement.type),
1780
- valueLength: getOptionalNumber(rawFocusedElement.valueLength),
1781
- };
1782
- return Object.values(focusedElement).some((value) => value !== undefined) ? focusedElement : undefined;
1783
- }
1784
-
1785
- function extractElectronProbeTabs(data: unknown): { activeTab?: ElectronProbeTab; tabs?: ElectronProbeResult["tabs"] } {
1786
- const rawTabs = isRecord(data) && Array.isArray(data.tabs) ? data.tabs : Array.isArray(data) ? data : [];
1787
- const allTabs = rawTabs.filter(isRecord).map((tab, index): ElectronProbeTab => ({
1788
- active: getOptionalBoolean(tab.active),
1789
- index: typeof tab.index === "number" && Number.isInteger(tab.index) ? tab.index : index,
1790
- tabId: getTrimmedString(tab.tabId) ?? getTrimmedString(tab.id),
1791
- title: getTrimmedString(tab.title) ?? getTrimmedString(tab.label),
1792
- type: getTrimmedString(tab.type),
1793
- url: getTrimmedString(tab.url),
1794
- }));
1795
- if (allTabs.length === 0) return {};
1796
- const shown = allTabs.slice(0, ELECTRON_PROBE_MAX_TABS);
1797
- return {
1798
- activeTab: allTabs.find((tab) => tab.active) ?? allTabs[0],
1799
- tabs: {
1800
- omittedCount: allTabs.length > shown.length ? allTabs.length - shown.length : undefined,
1801
- shown,
1802
- total: allTabs.length,
1803
- },
1804
- };
1805
- }
1806
-
1807
- function truncateElectronProbeSnapshotText(snapshotText: string | undefined): { lineCount: number; omittedLineCount?: number; text?: string } {
1808
- if (!snapshotText) return { lineCount: 0 };
1809
- const lines = snapshotText.split(/\r?\n/);
1810
- const shownLines: string[] = [];
1811
- let usedChars = 0;
1812
- for (const line of lines) {
1813
- if (shownLines.length >= ELECTRON_PROBE_MAX_SNAPSHOT_LINES) break;
1814
- const nextLength = usedChars + line.length + (shownLines.length > 0 ? 1 : 0);
1815
- if (nextLength > ELECTRON_PROBE_MAX_SNAPSHOT_CHARS) {
1816
- if (shownLines.length === 0) shownLines.push(`${line.slice(0, ELECTRON_PROBE_MAX_SNAPSHOT_CHARS - 3)}...`);
1817
- break;
1818
- }
1819
- shownLines.push(line);
1820
- usedChars = nextLength;
1821
- }
1822
- return {
1823
- lineCount: lines.length,
1824
- omittedLineCount: lines.length > shownLines.length ? lines.length - shownLines.length : undefined,
1825
- text: shownLines.length > 0 ? shownLines.join("\n") : undefined,
1826
- };
1827
- }
1828
-
1829
- function summarizeElectronProbeSnapshot(data: unknown): { refSnapshot?: SessionRefSnapshot; snapshot?: ElectronProbeSnapshotSummary } {
1830
- const refSnapshot = extractRefSnapshotFromData(data);
1831
- const rawSnapshotText = isRecord(data) ? getTrimmedString(data.snapshot) : undefined;
1832
- const truncatedText = truncateElectronProbeSnapshotText(rawSnapshotText);
1833
- const refIds = refSnapshot?.refIds ?? [];
1834
- const shownRefIds = refIds.slice(0, ELECTRON_PROBE_MAX_REF_IDS);
1835
- const snapshot = refSnapshot || truncatedText.text
1836
- ? {
1837
- lineCount: truncatedText.lineCount,
1838
- omittedLineCount: truncatedText.omittedLineCount,
1839
- omittedRefCount: refIds.length > shownRefIds.length ? refIds.length - shownRefIds.length : undefined,
1840
- refCount: refIds.length,
1841
- refIds: shownRefIds,
1842
- text: truncatedText.text,
1843
- }
1844
- : undefined;
1845
- return { refSnapshot, snapshot };
1846
- }
1847
-
1848
- function getElectronProbeSummary(probe: Omit<ElectronProbeResult, "summary">): string {
1849
- const parts = [
1850
- probe.title ? `title "${probe.title}"` : undefined,
1851
- probe.url ? `url ${probe.url}` : undefined,
1852
- probe.focusedElement ? "focused element" : undefined,
1853
- probe.tabs ? `${probe.tabs.total} tab(s)` : undefined,
1854
- probe.snapshot ? `${probe.snapshot.refCount} ref(s)` : undefined,
1855
- ].filter((item): item is string => item !== undefined);
1856
- return parts.length > 0 ? `Electron probe collected ${parts.join(", ")}.` : "Electron probe did not return current session state.";
1857
- }
1858
-
1859
- async function runElectronProbeCommandData(options: {
1860
- args: string[];
1861
- cwd: string;
1862
- sessionName: string;
1863
- signal?: AbortSignal;
1864
- stdin?: string;
1865
- timeoutMs?: number;
1866
- }): Promise<{ data?: unknown; error?: string }> {
1867
- try {
1868
- return { data: await runSessionCommandData(options) };
1869
- } catch (error) {
1870
- return { error: error instanceof Error ? error.message : String(error) };
1871
- }
1872
- }
1873
-
1874
- async function collectElectronProbe(options: {
1875
- cwd: string;
1876
- sessionName: string;
1877
- signal?: AbortSignal;
1878
- timeoutMs?: number;
1879
- }): Promise<ElectronProbeResult> {
1880
- const titleResult = await runElectronProbeCommandData({ args: ["get", "title"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, timeoutMs: options.timeoutMs });
1881
- const urlResult = await runElectronProbeCommandData({ args: ["get", "url"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, timeoutMs: options.timeoutMs });
1882
- const focusedResult = await runElectronProbeCommandData({ args: ["eval", "--stdin"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, stdin: ELECTRON_FOCUSED_ELEMENT_EVAL, timeoutMs: options.timeoutMs });
1883
- const tabsResult = await runElectronProbeCommandData({ args: ["tab", "list"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, timeoutMs: options.timeoutMs });
1884
- const snapshotResult = await runElectronProbeCommandData({ args: ["snapshot", "-i"], cwd: options.cwd, sessionName: options.sessionName, signal: options.signal, timeoutMs: options.timeoutMs });
1885
- const errors = [
1886
- titleResult.error ? `get title: ${titleResult.error}` : undefined,
1887
- urlResult.error ? `get url: ${urlResult.error}` : undefined,
1888
- focusedResult.error ? `focused element: ${focusedResult.error}` : undefined,
1889
- tabsResult.error ? `tab list: ${tabsResult.error}` : undefined,
1890
- snapshotResult.error ? `snapshot: ${snapshotResult.error}` : undefined,
1891
- ].filter((item): item is string => item !== undefined).map((error) => boundElectronProbeString(error, 240) ?? "probe command failed");
1892
- const title = boundElectronProbeString(extractStringResultField(titleResult.data, "result") ?? extractStringResultField(titleResult.data, "title"), 160);
1893
- const url = boundElectronProbeString(extractStringResultField(urlResult.data, "result") ?? extractStringResultField(urlResult.data, "url"), 300);
1894
- const focusedElement = extractElectronFocusedElement(focusedResult.data);
1895
- const { activeTab, tabs } = extractElectronProbeTabs(tabsResult.data);
1896
- const { refSnapshot, snapshot } = summarizeElectronProbeSnapshot(snapshotResult.data);
1897
- const probeWithoutSummary = {
1898
- activeTab,
1899
- focusedElement,
1900
- errors: errors.length > 0 ? errors : undefined,
1901
- refSnapshot,
1902
- sessionName: options.sessionName,
1903
- snapshot,
1904
- status: errors.length === 0 && (title || url || focusedElement || tabs || snapshot) ? "succeeded" as const : "partial" as const,
1905
- tabs,
1906
- title,
1907
- url,
1908
- };
1909
- return { ...probeWithoutSummary, summary: getElectronProbeSummary(probeWithoutSummary) };
1910
- }
1911
-
1912
- function formatElectronProbeFocusedElement(focusedElement: ElectronProbeFocusedElement | undefined): string | undefined {
1913
- if (!focusedElement) return undefined;
1914
- const label = focusedElement.name ?? focusedElement.textPreview ?? focusedElement.placeholder ?? focusedElement.ariaLabel ?? focusedElement.title;
1915
- const descriptor = [focusedElement.role, focusedElement.tagName].filter(Boolean).join("/") || "element";
1916
- const suffix = [
1917
- focusedElement.id ? `#${focusedElement.id}` : undefined,
1918
- focusedElement.type ? `type=${focusedElement.type}` : undefined,
1919
- focusedElement.valueLength !== undefined ? `valueLength=${focusedElement.valueLength}` : undefined,
1920
- focusedElement.textLength !== undefined ? `textLength=${focusedElement.textLength}` : undefined,
1921
- ].filter((item): item is string => item !== undefined).join(", ");
1922
- return `Focused: ${descriptor}${label ? ` "${label}"` : ""}${suffix ? ` (${suffix})` : ""}`;
1923
- }
1924
-
1925
- function formatElectronProbeContextText(context: ElectronProbeContext): string {
1926
- if (context.mode === "launchId") {
1927
- return `Probe context: wrapper launch ${context.launchId} session ${context.sessionName}.`;
1928
- }
1929
- if (context.note) {
1930
- return `Probe context: current managed session ${context.sessionName}; ${context.note}`;
1931
- }
1932
- if (context.launchId) {
1933
- return `Probe context: current managed session ${context.sessionName} maps to Electron launch ${context.launchId}.`;
1934
- }
1935
- return `Probe context: current managed session ${context.sessionName} only; pass electron.probe.launchId to compare wrapper-tracked launch status.`;
1936
- }
1937
-
1938
- function formatElectronProbeLaunchStatusText(status: ElectronLaunchStatus | undefined, probe: ElectronProbeResult): string | undefined {
1939
- if (!status) return undefined;
1940
- const lines = [`Launch status: ${status.portAlive ? "debug port alive" : "debug port dead"}${status.pidAlive === undefined ? "" : status.pidAlive ? ", pid alive" : ", pid dead"}; ${status.targets.length} CDP target(s).`];
1941
- if (isAboutBlankUrl(probe.url) && (!status.portAlive || status.pidAlive === false || getLiveElectronRendererTargets(status.targets).length === 0)) {
1942
- lines.push("Electron lifecycle warning: the browser session is on about:blank and the wrapper launch has no live renderer target to reattach. Run electron.status, cleanup if dead, or relaunch the app.");
1943
- }
1944
- return lines.join("\n");
1945
- }
1946
-
1947
- function formatElectronProbeVisibleText(options: {
1948
- context?: ElectronProbeContext;
1949
- mismatch?: ElectronSessionMismatch;
1950
- probe: ElectronProbeResult;
1951
- status?: ElectronLaunchStatus;
1952
- }): string {
1953
- const { context, mismatch, probe, status } = options;
1954
- const page = [probe.title, probe.url].filter(Boolean).join(" — ");
1955
- const lines = [`Electron probe: ${page || probe.sessionName}`];
1956
- if (context) lines.push(formatElectronProbeContextText(context));
1957
- const launchStatusText = formatElectronProbeLaunchStatusText(status, probe);
1958
- if (launchStatusText) lines.push(launchStatusText);
1959
- if (mismatch) lines.push(formatElectronSessionMismatchText(mismatch));
1960
- const focusedLine = formatElectronProbeFocusedElement(probe.focusedElement);
1961
- if (focusedLine) lines.push(focusedLine);
1962
- if (probe.tabs) {
1963
- const active = probe.activeTab;
1964
- lines.push(`Tabs: ${probe.tabs.total} total${probe.tabs.omittedCount ? ` (${probe.tabs.omittedCount} omitted)` : ""}${active ? `; active ${active.index ?? "?"}: ${[active.title, active.url].filter(Boolean).join(" — ") || active.tabId || "tab"}` : ""}`);
1965
- }
1966
- if (probe.snapshot) {
1967
- lines.push(`Snapshot: ${probe.snapshot.refCount} interactive ref(s)${probe.snapshot.omittedRefCount ? ` (${probe.snapshot.omittedRefCount} ref id(s) omitted)` : ""}.`);
1968
- if (probe.snapshot.text) lines.push(probe.snapshot.text);
1969
- if (probe.snapshot.omittedLineCount) lines.push(`... ${probe.snapshot.omittedLineCount} snapshot line(s) omitted`);
1970
- }
1971
- if (probe.status === "partial") lines.push("Some probe commands did not return data; use raw agent_browser commands for deeper diagnostics.");
1972
- if (probe.errors && probe.errors.length > 0) lines.push(`Probe warning: ${probe.errors.slice(0, 2).join("; ")}${probe.errors.length > 2 ? "; ..." : ""}`);
1973
- return lines.join("\n");
1974
- }
1975
-
1976
- function buildElectronProbeResult(options: {
1977
- compiledElectron: CompiledAgentBrowserElectron;
1978
- mismatch?: ElectronSessionMismatch;
1979
- probe: ElectronProbeResult;
1980
- probeContext: ElectronProbeContext;
1981
- record?: ElectronLaunchRecord;
1982
- sessionTabTarget?: SessionTabTarget;
1983
- status?: ElectronLaunchStatus;
1984
- }): AgentBrowserToolResult {
1985
- const { refSnapshot: _refSnapshot, ...boundedProbe } = options.probe;
1986
- const baseNextActions = options.record ? buildAgentBrowserNextActions({
1987
- electron: { launchId: options.record.launchId, sessionName: options.record.sessionName, status: options.record.cleanupState },
1988
- resultCategory: "success",
1989
- successCategory: "completed",
1990
- }) ?? [] : [];
1991
- const mismatchNextActions = options.mismatch && options.record ? buildElectronMismatchNextActions(options.record, options.mismatch.liveTarget) : [];
1992
- const nextActions = options.mismatch
1993
- ? appendUniqueAgentBrowserNextActions([...mismatchNextActions], baseNextActions)
1994
- : appendUniqueAgentBrowserNextActions([...baseNextActions], mismatchNextActions);
1995
- const details = {
1996
- args: [] as string[],
1997
- compiledElectron: options.compiledElectron,
1998
- electron: {
1999
- action: "probe" as const,
2000
- identifiers: options.record ? buildElectronIdentifiers(options.record) : undefined,
2001
- probe: boundedProbe,
2002
- probeContext: options.probeContext,
2003
- sessionMismatch: options.mismatch,
2004
- status: options.probe.status,
2005
- statusTargets: options.status?.targets,
2006
- launchStatus: options.status,
2007
- },
2008
- nextActions: nextActions.length > 0 ? nextActions : undefined,
2009
- ...buildAgentBrowserResultCategoryDetails({ args: [], succeeded: true }),
2010
- sessionName: options.probe.sessionName,
2011
- sessionTabTarget: options.sessionTabTarget,
2012
- summary: options.mismatch?.summary ?? options.probe.summary,
2013
- usedImplicitSession: options.probeContext.mode === "current-managed-session",
2014
- };
2015
- return {
2016
- content: [{ type: "text", text: redactSensitiveText(formatElectronProbeVisibleText({ context: options.probeContext, mismatch: options.mismatch, probe: options.probe, status: options.status })) }],
2017
- details: redactToolDetails(details, []),
2018
- isError: false,
2019
- };
2020
- }
2021
-
2022
-
2023
-
2024
- function supportsPinnedStdinCommand(options: { command?: string; commandTokens: string[]; stdin?: string }): boolean {
2025
- if (options.command === "batch") {
2026
- return options.stdin !== undefined;
2027
- }
2028
- if (options.stdin === undefined) {
2029
- return true;
2030
- }
2031
- if (options.command === "eval") {
2032
- return options.commandTokens.includes("--stdin");
2033
- }
2034
- return false;
2035
- }
2036
-
2037
-
2038
- function validateUserBatchStep(
2039
- step: unknown,
2040
- index: number,
2041
- ):
2042
- | { ok: true; step: BatchCommandStep }
2043
- | { ok: false; error: string } {
2044
- if (!Array.isArray(step)) {
2045
- return {
2046
- ok: false,
2047
- error: `agent_browser batch stdin step ${index} must be a non-empty array of string command tokens.`,
2048
- };
2049
- }
2050
- if (step.length === 0) {
2051
- return {
2052
- ok: false,
2053
- error: `agent_browser batch stdin step ${index} must not be empty.`,
2054
- };
2055
- }
2056
- const invalidTokenIndex = step.findIndex((token) => typeof token !== "string");
2057
- if (invalidTokenIndex !== -1) {
2058
- return {
2059
- ok: false,
2060
- error: `agent_browser batch stdin step ${index} token ${invalidTokenIndex} must be a string.`,
2061
- };
2062
- }
2063
- return { ok: true, step: step as BatchCommandStep };
2064
- }
2065
-
2066
- function parseUserBatchStdin(stdin: string | undefined): { error?: string; steps?: BatchCommandStep[] } {
2067
- if (stdin === undefined) {
2068
- return { steps: [] };
2069
- }
2070
- try {
2071
- const parsed = JSON.parse(stdin) as unknown;
2072
- if (!Array.isArray(parsed)) {
2073
- return { error: "agent_browser batch stdin must be a JSON array of command steps." };
2074
- }
2075
- const steps: BatchCommandStep[] = [];
2076
- for (const [index, rawStep] of parsed.entries()) {
2077
- const validated = validateUserBatchStep(rawStep, index);
2078
- if (!validated.ok) {
2079
- return { error: validated.error };
2080
- }
2081
- steps.push(validated.step);
2082
- }
2083
- return { steps };
2084
- } catch (error) {
2085
- const message = error instanceof Error ? error.message : String(error);
2086
- return { error: `agent_browser batch stdin could not be parsed as JSON: ${message}` };
2087
- }
2088
- }
2089
-
2090
- const REF_INVALIDATING_BATCH_COMMANDS = new Set([
2091
- "back",
2092
- "check",
2093
- "click",
2094
- "dblclick",
2095
- "drag",
2096
- "forward",
2097
- "goto",
2098
- "keyboard",
2099
- "mouse",
2100
- "navigate",
2101
- "open",
2102
- "press",
2103
- "reload",
2104
- "select",
2105
- "type",
2106
- "uncheck",
2107
- "upload",
2108
- ]);
2109
-
2110
- const REF_GUARDED_COMMANDS = new Set([
2111
- "check",
2112
- "click",
2113
- "dblclick",
2114
- "download",
2115
- "drag",
2116
- "fill",
2117
- "focus",
2118
- "hover",
2119
- "keyboard",
2120
- "mouse",
2121
- "press",
2122
- "scrollintoview",
2123
- "select",
2124
- "type",
2125
- "uncheck",
2126
- "upload",
2127
- ]);
2128
-
2129
-
2130
- function collectRefsFromTokens(tokens: string[]): string[] {
2131
- return tokens.filter((token) => /^@e\d+\b/.test(token)).map((token) => token.slice(1));
2132
- }
2133
-
2134
- function getGuardedRefUsage(commandTokens: string[], stdin?: string, options: { includeRefsAfterBatchSnapshot?: boolean } = {}): string[] {
2135
- const collectFromStep = (step: string[]) => REF_GUARDED_COMMANDS.has(step[0] ?? "") ? collectRefsFromTokens(step) : [];
2136
- if (commandTokens[0] !== "batch" || stdin === undefined) {
2137
- return collectFromStep(commandTokens);
2138
- }
2139
- const parsed = parseUserBatchStdin(stdin);
2140
- if (parsed.error || parsed.steps === undefined) {
2141
- return collectFromStep(commandTokens);
2142
- }
2143
- const refsBeforeInBatchSnapshot: string[] = [];
2144
- for (const step of parsed.steps) {
2145
- if (!options.includeRefsAfterBatchSnapshot && (step[0] ?? "") === "snapshot") break;
2146
- refsBeforeInBatchSnapshot.push(...collectFromStep(step));
2147
- }
2148
- return refsBeforeInBatchSnapshot;
2149
- }
2150
-
2151
- function getBatchRefInvalidationMessage(commandTokens: string[], stdin?: string): string | undefined {
2152
- if (commandTokens[0] !== "batch" || stdin === undefined) return undefined;
2153
- const parsed = parseUserBatchStdin(stdin);
2154
- if (parsed.error || parsed.steps === undefined) return undefined;
2155
- let priorStepInvalidatesRefs = false;
2156
- for (const step of parsed.steps) {
2157
- if ((step[0] ?? "") === "snapshot") {
2158
- priorStepInvalidatesRefs = false;
2159
- }
2160
- const refIds = collectRefsFromTokens(step);
2161
- if (refIds.length > 0 && REF_GUARDED_COMMANDS.has(step[0] ?? "") && priorStepInvalidatesRefs) {
2162
- return `Batch step ${step[0]} uses page-scoped ref ${refIds.map((refId) => `@${refId}`).join(", ")} after an earlier batch step can navigate or mutate the page. Split the batch, run snapshot -i after the page-changing step, then retry with current refs.`;
2163
- }
2164
- if (REF_INVALIDATING_BATCH_COMMANDS.has(step[0] ?? "")) {
2165
- priorStepInvalidatesRefs = true;
2166
- }
2167
- }
2168
- return undefined;
2169
- }
2170
-
2171
-
2172
-
2173
-
2174
- function selectSessionTargetTab(options: {
2175
- tabs: Array<{ active?: boolean; index?: number; label?: string; tabId?: string; title?: string; url?: string }>;
2176
- target: SessionTabTarget;
2177
- }): OpenResultTabCorrection | undefined {
2178
- return chooseOpenResultTabCorrection({
2179
- tabs: options.tabs,
2180
- targetTitle: options.target.title,
2181
- targetUrl: options.target.url,
2182
- });
2183
- }
2184
-
2185
-
2186
- async function runSessionCommandData(options: {
2187
- args: string[];
2188
- cwd: string;
2189
- sessionName?: string;
2190
- signal?: AbortSignal;
2191
- stdin?: string;
2192
- timeoutMs?: number;
2193
- }): Promise<unknown | undefined> {
2194
- const { args, cwd, sessionName, signal, stdin, timeoutMs } = options;
2195
- if (!sessionName) return undefined;
2196
-
2197
- const processResult = await runAgentBrowserProcess({
2198
- args: ["--json", "--session", sessionName, ...args],
2199
- cwd,
2200
- signal,
2201
- stdin,
2202
- timeoutMs,
2203
- });
2204
- try {
2205
- if (processResult.aborted || processResult.spawnError || processResult.exitCode !== 0) {
2206
- return undefined;
2207
- }
2208
- const parsed = await parseAgentBrowserEnvelope({
2209
- stdout: processResult.stdout,
2210
- stdoutPath: processResult.stdoutSpillPath,
2211
- });
2212
- if (parsed.parseError || parsed.envelope?.success === false) {
2213
- return undefined;
2214
- }
2215
- return parsed.envelope?.data;
2216
- } finally {
2217
- if (processResult.stdoutSpillPath) {
2218
- await rm(processResult.stdoutSpillPath, { force: true }).catch(() => undefined);
2219
- }
2220
- }
2221
- }
2222
-
2223
- function getTopLevelFillInvocation(commandTokens: string[]): { expected: string; selector: string } | undefined {
2224
- if (commandTokens[0] !== "fill" || commandTokens.length < 3) return undefined;
2225
- const selector = commandTokens[1];
2226
- const expected = commandTokens.slice(2).join(" ");
2227
- if (!selector || expected.length === 0) return undefined;
2228
- return { expected, selector };
2229
- }
2230
-
2231
- function buildFillVerificationNextActions(diagnostic: FillVerificationDiagnostic, sessionName: string | undefined): AgentBrowserNextAction[] {
2232
- return [
2233
- {
2234
- id: "inspect-after-fill-verification",
2235
- params: { args: withOptionalSessionArgs(sessionName, ["snapshot", "-i"]) },
2236
- reason: "Refresh the UI after a fill that reported success but did not appear to update the input value.",
2237
- safety: "Read-only snapshot; use current refs before retrying.",
2238
- tool: "agent_browser",
2239
- },
2240
- {
2241
- id: "verify-filled-value",
2242
- params: { args: withOptionalSessionArgs(sessionName, ["get", "value", diagnostic.selector]) },
2243
- reason: "Check the target input value directly before submitting or creating files.",
2244
- safety: "Read-only value check; selector may still be stale if the Electron UI rerendered.",
2245
- tool: "agent_browser",
2246
- },
2247
- ];
2248
- }
2249
-
2250
- function extractFillVerificationValue(data: unknown): string | undefined {
2251
- if (typeof data === "string") return data;
2252
- if (!isRecord(data)) return undefined;
2253
- if (typeof data.value === "string") return data.value;
2254
- if (typeof data.result === "string") return data.result;
2255
- return undefined;
2256
- }
2257
-
2258
-
2259
-
2260
- function buildElectronRefFreshnessNextActions(sessionName: string | undefined): AgentBrowserNextAction[] {
2261
- return [{
2262
- id: "refresh-electron-refs-after-rerender",
2263
- params: { args: withOptionalSessionArgs(sessionName, ["snapshot", "-i"]) },
2264
- reason: "Electron UIs often rerender without changing URL; refresh refs before using old @e handles again.",
2265
- safety: "Read-only snapshot; avoids stale same-URL refs after quick-pick, modal, theme, or editor rerenders.",
2266
- tool: "agent_browser",
2267
- }];
2268
- }
2269
-
2270
-
2271
-
2272
-
2273
- function extractScrollPositionSnapshot(data: unknown): ScrollPositionSnapshot | undefined {
2274
- const result = isRecord(data) && isRecord(data.result) ? data.result : data;
2275
- if (!isRecord(result)) return undefined;
2276
- const scrollX = typeof result.scrollX === "number" ? result.scrollX : undefined;
2277
- const scrollY = typeof result.scrollY === "number" ? result.scrollY : undefined;
2278
- const innerHeight = typeof result.innerHeight === "number" ? result.innerHeight : undefined;
2279
- const innerWidth = typeof result.innerWidth === "number" ? result.innerWidth : undefined;
2280
- const scrollHeight = typeof result.scrollHeight === "number" ? result.scrollHeight : undefined;
2281
- const scrollWidth = typeof result.scrollWidth === "number" ? result.scrollWidth : undefined;
2282
- if (scrollX === undefined || scrollY === undefined || innerHeight === undefined || innerWidth === undefined || scrollHeight === undefined || scrollWidth === undefined) return undefined;
2283
- const containers = Array.isArray(result.containers)
2284
- ? result.containers.flatMap((entry, index): ScrollPositionSnapshot["containers"] => {
2285
- if (!isRecord(entry)) return [];
2286
- const rawId = typeof entry.id === "string" ? entry.id : undefined;
2287
- const id = rawId && /^\d+:[a-z][a-z0-9-]*(?:\[role=[a-z-]+\])?$/i.test(rawId) ? rawId : `sample-${index}`;
2288
- const scrollTop = typeof entry.scrollTop === "number" ? entry.scrollTop : undefined;
2289
- const scrollLeft = typeof entry.scrollLeft === "number" ? entry.scrollLeft : undefined;
2290
- return scrollTop !== undefined && scrollLeft !== undefined ? [{ id, scrollLeft, scrollTop }] : [];
2291
- })
2292
- : [];
2293
- return {
2294
- containerCount: typeof result.containerCount === "number" ? result.containerCount : containers.length,
2295
- containers,
2296
- innerHeight,
2297
- innerWidth,
2298
- scrollHeight,
2299
- scrollWidth,
2300
- scrollX,
2301
- scrollY,
2302
- };
2303
- }
2304
-
2305
- const SCROLL_POSITION_EVAL = `(() => {
2306
- const viewport = {
2307
- scrollX: window.scrollX,
2308
- scrollY: window.scrollY,
2309
- innerHeight: window.innerHeight,
2310
- innerWidth: window.innerWidth,
2311
- scrollHeight: Math.max(document.documentElement?.scrollHeight || 0, document.body?.scrollHeight || 0),
2312
- scrollWidth: Math.max(document.documentElement?.scrollWidth || 0, document.body?.scrollWidth || 0),
2313
- };
2314
- const describe = (element, index) => {
2315
- const role = element.getAttribute("role") || "";
2316
- const id = element.tagName.toLowerCase();
2317
- return {
2318
- id: String(index) + ":" + id + (role ? "[role=" + role + "]" : ""),
2319
- scrollTop: element.scrollTop,
2320
- scrollLeft: element.scrollLeft,
2321
- area: element.clientWidth * element.clientHeight,
2322
- };
2323
- };
2324
- const containers = Array.from(document.querySelectorAll("body *"))
2325
- .filter((element) => element instanceof HTMLElement && (element.scrollHeight > element.clientHeight + 1 || element.scrollWidth > element.clientWidth + 1))
2326
- .map(describe)
2327
- .sort((left, right) => right.area - left.area)
2328
- .slice(0, 10)
2329
- .map(({ area, ...entry }) => entry);
2330
- return { ...viewport, containerCount: containers.length, containers };
2331
- })()`;
2332
-
2333
-
2334
- function sameScrollPositionSnapshot(left: ScrollPositionSnapshot, right: ScrollPositionSnapshot): boolean {
2335
- if (
2336
- left.scrollX !== right.scrollX ||
2337
- left.scrollY !== right.scrollY ||
2338
- left.scrollHeight !== right.scrollHeight ||
2339
- left.scrollWidth !== right.scrollWidth ||
2340
- left.containers.length !== right.containers.length
2341
- ) {
2342
- return false;
2343
- }
2344
- return left.containers.every((container, index) => {
2345
- const other = right.containers[index];
2346
- return other?.id === container.id && other.scrollTop === container.scrollTop && other.scrollLeft === container.scrollLeft;
2347
- });
2348
- }
2349
-
2350
-
2351
-
2352
-
2353
-
2354
- const COMBOBOX_FOCUS_EVAL = `(() => {
2355
- const isVisible = (element) => {
2356
- if (!(element instanceof HTMLElement)) return false;
2357
- const style = window.getComputedStyle(element);
2358
- if (style.display === "none" || style.visibility === "hidden" || Number(style.opacity) === 0) return false;
2359
- return element.getClientRects().length > 0;
2360
- };
2361
- const active = document.activeElement instanceof HTMLElement ? document.activeElement : null;
2362
- const role = active?.getAttribute("role") || undefined;
2363
- const hasPopup = active?.getAttribute("aria-haspopup") || undefined;
2364
- const expanded = active?.getAttribute("aria-expanded") || undefined;
2365
- const tagName = active?.tagName.toLowerCase();
2366
- const name = (active?.getAttribute("aria-label") || active?.getAttribute("placeholder") || active?.getAttribute("title") || active?.textContent || "").trim().slice(0, 80) || undefined;
2367
- const visibleListboxCount = Array.from(document.querySelectorAll('[role="listbox"], [role="menu"]')).filter(isVisible).length;
2368
- const visibleOptionCount = Array.from(document.querySelectorAll('[role="option"], option, [role="menuitem"]')).filter(isVisible).length;
2369
- const comboboxLike = role === "combobox" || hasPopup === "listbox" || hasPopup === "menu" || tagName === "select" || active?.getAttribute("aria-autocomplete") !== null;
2370
- return { activeElement: active ? { expanded, hasPopup, name, role, tagName } : undefined, comboboxLike, visibleListboxCount, visibleOptionCount };
2371
- })()`;
2372
-
2373
- function extractComboboxFocusDiagnostic(data: unknown): ComboboxFocusDiagnostic | undefined {
2374
- const result = isRecord(data) && isRecord(data.result) ? data.result : data;
2375
- if (!isRecord(result) || result.comboboxLike !== true || !isRecord(result.activeElement)) return undefined;
2376
- const visibleListboxCount = typeof result.visibleListboxCount === "number" ? result.visibleListboxCount : 0;
2377
- const visibleOptionCount = typeof result.visibleOptionCount === "number" ? result.visibleOptionCount : 0;
2378
- const expanded = typeof result.activeElement.expanded === "string" ? result.activeElement.expanded : undefined;
2379
- if ((expanded !== "false" && expanded !== "true") || visibleListboxCount > 0 || visibleOptionCount > 0) return undefined;
2380
- return {
2381
- activeElement: {
2382
- expanded,
2383
- hasPopup: typeof result.activeElement.hasPopup === "string" ? result.activeElement.hasPopup : undefined,
2384
- name: typeof result.activeElement.name === "string" ? redactSensitiveText(result.activeElement.name) : undefined,
2385
- role: typeof result.activeElement.role === "string" ? result.activeElement.role : undefined,
2386
- tagName: typeof result.activeElement.tagName === "string" ? result.activeElement.tagName : undefined,
2387
- },
2388
- message: "A combobox-like control is focused, but no listbox or option elements are visibly open.",
2389
- reason: "focused-combobox-without-visible-options",
2390
- recommendations: [
2391
- "Run snapshot -i to inspect whether options appeared under a different role or portal.",
2392
- "Try ArrowDown or Enter to open the option list before selecting, or use select/visible option refs when available.",
2393
- ],
2394
- visibleListboxCount,
2395
- visibleOptionCount,
2396
- };
2397
- }
2398
-
2399
- function isComboboxFocusDiagnosticCommand(command: string | undefined, commandTokens: string[]): boolean {
2400
- const explicitlyTargetsCombobox = commandTokens.some((token) => /^(?:combobox|listbox)$/i.test(token));
2401
- if (!explicitlyTargetsCombobox) return false;
2402
- if (command === "click" || command === "fill") return true;
2403
- return command === "find" && commandTokens.some((token) => ["click", "fill"].includes(token));
2404
- }
2405
-
2406
- function getCompiledSemanticActionRoleValue(compiled: CompiledAgentBrowserSemanticAction): string | undefined {
2407
- if (compiled.locator !== "role") return undefined;
2408
- const findIndex = compiled.args.indexOf("find");
2409
- if (findIndex < 0 || compiled.args[findIndex + 1] !== "role") return undefined;
2410
- return compiled.args[findIndex + 2];
2411
- }
2412
-
2413
- function isComboboxFocusDiagnosticSemanticAction(compiled: CompiledAgentBrowserSemanticAction | undefined): boolean {
2414
- if (!compiled || !["click", "fill"].includes(compiled.action)) return false;
2415
- return /^(?:combobox|listbox)$/i.test(getCompiledSemanticActionRoleValue(compiled) ?? "");
2416
- }
2417
-
2418
-
2419
-
2420
-
2421
- function getRecordStartLikeCommand(command: string | undefined, commandTokens: string[]): RecordingDependencyWarning["command"] | undefined {
2422
- if (command !== "record") return undefined;
2423
- const subcommand = commandTokens[1]?.toLowerCase();
2424
- if (subcommand === "start") return "record start";
2425
- if (subcommand === "restart") return "record restart";
2426
- return undefined;
2427
- }
2428
-
2429
- async function executableExistsOnPath(command: string): Promise<boolean> {
2430
- const pathValue = process.env.PATH ?? "";
2431
- const extensions = process.platform === "win32"
2432
- ? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean)
2433
- : [""];
2434
- for (const directory of pathValue.split(delimiter).filter(Boolean)) {
2435
- for (const extension of extensions) {
2436
- try {
2437
- const candidate = join(directory, `${command}${extension}`);
2438
- await access(candidate, fsConstants.X_OK);
2439
- if ((await stat(candidate)).isFile()) return true;
2440
- } catch {
2441
- // Try the next candidate.
2442
- }
2443
- }
2444
- }
2445
- return false;
2446
- }
2447
-
2448
-
2449
-
2450
- function getSnapshotRefRecord(data: unknown): Record<string, unknown> | undefined {
2451
- return isRecord(data) && isRecord(data.refs) ? data.refs : undefined;
2452
- }
2453
-
2454
- const OVERLAY_CLOSE_NAME_PATTERN = /(?:\b(?:close|dismiss|no thanks|not now|maybe later|hide|skip|continue without|x)\b|^\s*×\s*$)/i;
2455
- const OVERLAY_CONTEXT_ROLES = new Set(["alertdialog", "dialog"]);
2456
- const OVERLAY_ACTION_ROLES = new Set(["button", "link", "menuitem"]);
2457
- const OVERLAY_BLOCKER_CANDIDATE_LIMIT = 3;
2458
-
2459
- function getOverlayBlockerCandidates(snapshotData: unknown): OverlayBlockerCandidate[] {
2460
- const refs = getSnapshotRefRecord(snapshotData);
2461
- if (!refs) return [];
2462
- const hasOverlayContext = Object.values(refs).some((entry) => {
2463
- if (!isRecord(entry)) return false;
2464
- const role = typeof entry.role === "string" ? entry.role : "";
2465
- return OVERLAY_CONTEXT_ROLES.has(role.toLowerCase());
2466
- });
2467
- if (!hasOverlayContext) return [];
2468
- const candidates: OverlayBlockerCandidate[] = [];
2469
- for (const [ref, entry] of Object.entries(refs)) {
2470
- if (!/^e\d+$/.test(ref) || !isRecord(entry)) continue;
2471
- const role = typeof entry.role === "string" ? entry.role : undefined;
2472
- const name = typeof entry.name === "string" ? entry.name : undefined;
2473
- if (!role || !OVERLAY_ACTION_ROLES.has(role.toLowerCase()) || !name || !OVERLAY_CLOSE_NAME_PATTERN.test(name)) continue;
2474
- candidates.push({
2475
- args: ["click", `@${ref}`],
2476
- name,
2477
- reason: `Visible ${role} ${JSON.stringify(name)} appears in a snapshot that also contains overlay/banner/dialog context.`,
2478
- ref: `@${ref}`,
2479
- role,
2480
- });
2481
- if (candidates.length >= OVERLAY_BLOCKER_CANDIDATE_LIMIT) break;
2482
- }
2483
- return candidates;
2484
- }
2485
-
2486
-
2487
-
2488
- function buildVisibleTextProbeScript(selector: string): string {
2489
- return `(() => {\n const selector = ${JSON.stringify(selector)};\n const isVisible = (element) => {\n const style = window.getComputedStyle(element);\n if (!style || style.display === 'none' || style.visibility === 'hidden' || style.visibility === 'collapse' || Number(style.opacity) === 0) return false;\n return Array.from(element.getClientRects()).some((rect) => rect.width > 0 && rect.height > 0);\n };\n let matches = [];\n try {\n matches = Array.from(document.querySelectorAll(selector));\n } catch (error) {\n return JSON.stringify({ selector, error: error instanceof Error ? error.message : String(error) });\n }\n const visible = matches.filter(isVisible);\n const trim = (value) => typeof value === 'string' ? value.trim().replace(/\\s+/g, ' ').slice(0, 200) : undefined;\n return JSON.stringify({\n selector,\n matchCount: matches.length,\n visibleCount: visible.length,\n firstMatchVisible: matches[0] ? isVisible(matches[0]) : undefined,\n firstTextPreview: trim(matches[0]?.textContent),\n firstVisibleTextPreview: trim(visible[0]?.textContent),\n });\n})()`;
2490
- }
2491
-
2492
- function parseSelectorTextVisibilityProbe(data: unknown, selector: string): Omit<SelectorTextVisibilityDiagnostic, "summary"> | undefined {
2493
- const result = extractStringResultField(data, "result");
2494
- if (!result) return undefined;
2495
- let parsed: unknown;
2496
- try {
2497
- parsed = JSON.parse(result);
2498
- } catch {
2499
- return undefined;
2500
- }
2501
- if (!isRecord(parsed) || typeof parsed.error === "string") return undefined;
2502
- const matchCount = typeof parsed.matchCount === "number" ? parsed.matchCount : undefined;
2503
- const visibleCount = typeof parsed.visibleCount === "number" ? parsed.visibleCount : undefined;
2504
- if (matchCount === undefined || visibleCount === undefined) return undefined;
2505
- return {
2506
- firstMatchVisible: typeof parsed.firstMatchVisible === "boolean" ? parsed.firstMatchVisible : undefined,
2507
- firstVisibleTextPreview: typeof parsed.firstVisibleTextPreview === "string" && parsed.firstVisibleTextPreview.length > 0 ? redactSensitiveText(parsed.firstVisibleTextPreview) : undefined,
2508
- matchCount,
2509
- selector,
2510
- visibleCount,
2511
- };
2512
- }
2513
-
2514
- function selectorMayExposeSensitiveLiteral(selector: string): boolean {
2515
- return redactSensitiveText(selector) !== selector || /\[[^\]]*[~|^$*]?=\s*(?:"[^"]*"|'[^']*'|[^\]\s]+)\s*(?:[is]\s*)?\]/.test(selector);
2516
- }
2517
-
2518
- async function collectSelectorTextVisibilityDiagnosticForSelector(options: {
2519
- cwd: string;
2520
- selector: string | undefined;
2521
- sessionName?: string;
2522
- signal?: AbortSignal;
2523
- }): Promise<SelectorTextVisibilityDiagnostic | undefined> {
2524
- const { selector } = options;
2525
- if (!selector || /^@e\d+$/.test(selector) || selectorMayExposeSensitiveLiteral(selector)) return undefined;
2526
- const probe = await runSessionCommandData({
2527
- args: ["eval", "--stdin"],
2528
- cwd: options.cwd,
2529
- sessionName: options.sessionName,
2530
- signal: options.signal,
2531
- stdin: buildVisibleTextProbeScript(selector),
2532
- });
2533
- const parsed = parseSelectorTextVisibilityProbe(probe, selector);
2534
- if (!parsed || parsed.matchCount <= 1 && parsed.firstMatchVisible !== false) return undefined;
2535
- if (parsed.visibleCount === 0) return undefined;
2536
- const visibleMatchNoun = `visible match${parsed.visibleCount === 1 ? "" : "es"}`;
2537
- const visibleMatchVerb = parsed.visibleCount === 1 ? "exists" : "exist";
2538
- const summary = parsed.firstMatchVisible === false
2539
- ? `Selector ${JSON.stringify(selector)} matched ${parsed.matchCount} elements; the first match is hidden while ${parsed.visibleCount} ${visibleMatchNoun} ${visibleMatchVerb}.`
2540
- : `Selector ${JSON.stringify(selector)} matched ${parsed.matchCount} elements; get text reads the first upstream match, which may not be the intended visible tab/panel.`;
2541
- return { ...parsed, summary };
2542
- }
2543
-
2544
- function getBatchGetTextSelectors(data: unknown): string[] {
2545
- if (!Array.isArray(data)) return [];
2546
- return data.flatMap((item) => {
2547
- if (!isRecord(item) || item.success === false) return [];
2548
- const [command, subcommand, selector] = extractBatchResultCommand(item);
2549
- return command === "get" && subcommand === "text" && selector ? [selector] : [];
2550
- });
2551
- }
2552
-
2553
- function getSuccessfulGetTextSelectors(options: { commandInfo: CommandInfo; commandTokens: string[]; data: unknown }): string[] {
2554
- return options.commandInfo.command === "get" && options.commandInfo.subcommand === "text"
2555
- ? [options.commandTokens[2]].filter((selector): selector is string => typeof selector === "string" && selector.length > 0)
2556
- : options.commandInfo.command === "batch"
2557
- ? getBatchGetTextSelectors(options.data)
2558
- : [];
2559
- }
2560
-
2561
-
2562
-
2563
- function isElectronLikeRendererUrl(url: string | undefined): boolean {
2564
- if (!url) return false;
2565
- return /^(?:app|file|vscode-file|vscode|chrome-extension):/i.test(url);
2566
- }
2567
-
2568
- function normalizeSelectorForScopeHeuristic(selector: string): string {
2569
- return selector.trim().replace(/\s+/g, " ").toLowerCase();
2570
- }
2571
-
2572
- function isBroadGetTextSelector(selector: string | undefined): selector is string {
2573
- if (!selector || /^@e\d+$/.test(selector) || selectorMayExposeSensitiveLiteral(selector)) return false;
2574
- const normalized = normalizeSelectorForScopeHeuristic(selector);
2575
- return normalized === "body" ||
2576
- normalized === "html" ||
2577
- normalized === ":root" ||
2578
- normalized === "*" ||
2579
- normalized === "main" ||
2580
- normalized === "div" ||
2581
- normalized === "section" ||
2582
- normalized === "article" ||
2583
- /^\[role=(?:"application"|'application'|application)\]$/i.test(normalized);
2584
- }
2585
-
2586
- function getElectronTextScopeContext(options: {
2587
- currentTarget?: SessionTabTarget;
2588
- electronLaunchRecords: Map<string, ElectronLaunchRecord>;
2589
- priorTarget?: SessionTabTarget;
2590
- sessionName?: string;
2591
- }): ElectronBroadGetTextScopeDiagnostic["electronContext"] | undefined {
2592
- const record = findElectronLaunchRecordForSession(options.sessionName, options.electronLaunchRecords);
2593
- const url = options.currentTarget?.url ?? options.priorTarget?.url;
2594
- if (record) return { launchId: record.launchId, sessionName: record.sessionName ?? options.sessionName, url };
2595
- if (isElectronLikeRendererUrl(url)) return { sessionName: options.sessionName, url };
2596
- return undefined;
2597
- }
2598
-
2599
-
2600
-
2601
-
2602
-
2603
-
2604
- function looksLikeFunctionEvalStdin(stdin: string | undefined): boolean {
2605
- const trimmed = stdin?.trim();
2606
- if (!trimmed) return false;
2607
- return /^(?:async\s+)?function\b/.test(trimmed) || /^(?:async\s*)?\([^)]*\)\s*=>/.test(trimmed) || /^(?:async\s+)?[A-Za-z_$][\w$]*\s*=>/.test(trimmed);
2608
- }
2609
-
2610
- function isPlainEmptyObject(value: unknown): boolean {
2611
- if (!isRecord(value) || Array.isArray(value)) return false;
2612
- const prototype = Object.getPrototypeOf(value);
2613
- return (prototype === Object.prototype || prototype === null) && Object.keys(value).length === 0;
2614
- }
2615
-
2616
-
2617
-
2618
-
2619
-
2620
-
2621
-
2622
-
2623
- function getTimeoutProgressSteps(compiledJob: CompiledAgentBrowserJob | undefined, command: string | undefined, stdin: string | undefined): Array<{ args: string[]; index: number }> {
2624
- if (compiledJob) return compiledJob.steps.map((step, index) => ({ args: step.args, index: index + 1 }));
2625
- if (command !== "batch" || !stdin) return [];
2626
- try {
2627
- const parsed = JSON.parse(stdin) as unknown;
2628
- if (!Array.isArray(parsed)) return [];
2629
- return parsed.flatMap((step, index) => Array.isArray(step) && step.every((token) => typeof token === "string") ? [{ args: step as string[], index: index + 1 }] : []);
2630
- } catch {
2631
- return [];
2632
- }
2633
- }
2634
-
2635
- function getLastPositionalToken(args: string[], startIndex = 1): string | undefined {
2636
- for (let index = args.length - 1; index >= startIndex; index -= 1) {
2637
- const token = args[index];
2638
- if (token && !token.startsWith("-")) return token;
2639
- }
2640
- return undefined;
2641
- }
2642
-
2643
- function getTimeoutStepArtifactPath(args: string[]): string | undefined {
2644
- const [command] = args;
2645
- if (command === "screenshot") {
2646
- const index = getScreenshotPathTokenIndex(args);
2647
- return index === undefined ? undefined : args[index];
2648
- }
2649
- if (command === "pdf") return getLastPositionalToken(args);
2650
- if (command === "download") return getLastPositionalToken(args, 2);
2651
- if (command === "wait") {
2652
- const inlineDownload = args.find((token) => token.startsWith("--download="));
2653
- if (inlineDownload) return inlineDownload.slice("--download=".length) || undefined;
2654
- const downloadIndex = args.indexOf("--download");
2655
- const downloadPath = downloadIndex >= 0 ? args[downloadIndex + 1] : undefined;
2656
- if (downloadPath && !downloadPath.startsWith("-")) return downloadPath;
2657
- }
2658
- return undefined;
2659
- }
2660
-
2661
- async function collectTimeoutArtifactEvidence(cwd: string, steps: Array<{ args: string[]; index: number }>): Promise<TimeoutArtifactEvidence[]> {
2662
- const evidence: TimeoutArtifactEvidence[] = [];
2663
- for (const step of steps) {
2664
- const path = getTimeoutStepArtifactPath(step.args);
2665
- if (!path) continue;
2666
- const absolutePath = isAbsolute(path) ? path : resolve(cwd, path);
2667
- try {
2668
- const stats = await stat(absolutePath);
2669
- evidence.push({ absolutePath, exists: true, path, sizeBytes: stats.size, stepIndex: step.index });
2670
- } catch {
2671
- evidence.push({ absolutePath, exists: false, path, stepIndex: step.index });
2672
- }
2673
- }
2674
- return evidence;
2675
- }
2676
-
2677
- function getPlannedCurrentPageUrl(steps: Array<{ args: string[]; index: number }>): string | undefined {
2678
- for (let index = steps.length - 1; index >= 0; index -= 1) {
2679
- const args = steps[index]?.args ?? [];
2680
- if (args[0] === "open" || args[0] === "navigate" || args[0] === "pushstate") {
2681
- return getLastPositionalToken(args);
2682
- }
2683
- }
2684
- return undefined;
2685
- }
2686
-
2687
-
2688
- function redactSensitivePathSegmentsForDiagnostic(path: string): string {
2689
- return path.split(/([/\\]+)/).map((segment) => {
2690
- if (segment === "/" || segment === "\\" || /^[/\\]+$/.test(segment)) return segment;
2691
- return redactSensitiveText(segment) !== segment || /(?:secret|token|password|passwd|credential|auth|api[-_]?key|bearer)/i.test(segment) ? "[REDACTED]" : segment;
2692
- }).join("");
2693
- }
2694
-
2695
- function sanitizeCurrentPageUrlForTimeoutDiagnostic(url: string): string {
2696
- try {
2697
- const parsedUrl = new URL(url);
2698
- parsedUrl.pathname = parsedUrl.pathname.split("/").map((segment) => redactSensitivePathSegmentsForDiagnostic(segment)).join("/");
2699
- for (const [key, value] of parsedUrl.searchParams.entries()) {
2700
- if (redactSensitiveText(key) !== key || redactSensitiveText(value) !== value || /(?:secret|token|password|passwd|credential|auth|api[-_]?key|bearer)/i.test(`${key} ${value}`)) {
2701
- parsedUrl.searchParams.set(key, "[REDACTED]");
2702
- }
2703
- }
2704
- if (parsedUrl.hash) {
2705
- parsedUrl.hash = redactSensitivePathSegmentsForDiagnostic(redactSensitiveText(parsedUrl.hash));
2706
- }
2707
- return redactSensitiveText(parsedUrl.toString());
2708
- } catch {
2709
- return redactSensitivePathSegmentsForDiagnostic(redactSensitiveText(url));
2710
- }
2711
- }
2712
-
2713
-
2714
-
2715
-
2716
-
2717
-
2718
-
2719
-
2720
-
2721
-
2722
-
2723
-
2724
- // Serializes managed-session read/modify/write work so overlapping tool calls cannot promote stale state or close an in-use session.
2725
- class AsyncExecutionQueue {
2726
- private tail: Promise<void> = Promise.resolve();
2727
-
2728
- run<T>(work: () => Promise<T>): Promise<T> {
2729
- const previous = this.tail;
2730
- let release!: () => void;
2731
- this.tail = new Promise<void>((resolve) => {
2732
- release = resolve;
2733
- });
2734
-
2735
- return (async () => {
2736
- await previous;
2737
- try {
2738
- return await work();
2739
- } finally {
2740
- release();
2741
- }
2742
- })();
2743
- }
2744
- }
2745
-
2746
- async function closeManagedSession(options: { cwd: string; sessionName: string; timeoutMs: number }): Promise<string | undefined> {
2747
- const controller = new AbortController();
2748
- const timer = setTimeout(() => controller.abort(), options.timeoutMs);
2749
- let stdoutSpillPath: string | undefined;
2750
- const closeArgs = ["--session", options.sessionName, "close"];
2751
- try {
2752
- const processResult = await runAgentBrowserProcess({
2753
- args: closeArgs,
2754
- cwd: options.cwd,
2755
- signal: controller.signal,
2756
- });
2757
- stdoutSpillPath = processResult.stdoutSpillPath;
2758
- return getAgentBrowserErrorText({
2759
- aborted: processResult.aborted,
2760
- command: "close",
2761
- effectiveArgs: redactInvocationArgs(closeArgs),
2762
- exitCode: processResult.exitCode,
2763
- plainTextInspection: false,
2764
- spawnError: processResult.spawnError,
2765
- stderr: processResult.stderr,
2766
- timedOut: processResult.timedOut,
2767
- timeoutMs: processResult.timeoutMs,
2768
- });
2769
- } catch (error) {
2770
- return error instanceof Error ? error.message : String(error);
2771
- } finally {
2772
- clearTimeout(timer);
2773
- if (stdoutSpillPath) {
2774
- await rm(stdoutSpillPath, { force: true }).catch(() => undefined);
2775
- }
2776
- }
2777
- }
2778
-
2779
- function getInstalledDocsPaths(): { readmePath: string; commandReferencePath: string; toolContractPath: string } {
2780
- const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
2781
- return {
2782
- readmePath: join(packageRoot, "README.md"),
2783
- commandReferencePath: join(packageRoot, "docs", "COMMAND_REFERENCE.md"),
2784
- toolContractPath: join(packageRoot, "docs", "TOOL_CONTRACT.md"),
2785
- };
2786
- }
2787
-
2788
-
2789
-
2790
- async function handleElectronHostInput(options: {
2791
- cleanupTrackedElectronLaunches: (records: ElectronLaunchRecord[], cwd: string, timeoutMs?: number) => Promise<ElectronCleanupResult[]>;
2792
- compiledElectron?: CompiledAgentBrowserElectron;
2793
- cwd: string;
2794
- electronLaunchRecords: Map<string, ElectronLaunchRecord>;
2795
- implicitSessionCloseTimeoutMs: number;
2796
- managedSessionActive: boolean;
2797
- managedSessionExecutionQueue: { run<T>(task: () => Promise<T>): Promise<T> };
2798
- managedSessionName: string;
2799
- redactedCompiledElectron?: CompiledAgentBrowserElectron;
2800
- sessionPageState: SessionPageState;
2801
- signal?: AbortSignal;
2802
- }): Promise<AgentBrowserToolResult | undefined> {
2803
- const {
2804
- cleanupTrackedElectronLaunches,
2805
- compiledElectron,
2806
- cwd,
2807
- electronLaunchRecords,
2808
- implicitSessionCloseTimeoutMs,
2809
- managedSessionActive,
2810
- managedSessionExecutionQueue,
2811
- managedSessionName,
2812
- redactedCompiledElectron,
2813
- sessionPageState,
2814
- signal,
2815
- } = options;
2816
- if (compiledElectron?.action === "list") {
2817
- try {
2818
- const discovery = await discoverElectronApps({ maxResults: compiledElectron.maxResults, query: compiledElectron.query });
2819
- return buildElectronListSuccessResult(redactedCompiledElectron ?? compiledElectron, discovery);
2820
- } catch (error) {
2821
- return buildElectronListFailureResult(redactedCompiledElectron ?? compiledElectron, error);
2822
- }
2823
- }
2824
- if (compiledElectron?.action === "status") {
2825
- return managedSessionExecutionQueue.run(async () => {
2826
- const selection = selectElectronRecords(compiledElectron, electronLaunchRecords);
2827
- if (selection.error) return buildElectronHostFailureResult({ compiledElectron: redactedCompiledElectron ?? compiledElectron, errorText: selection.error, failureCategory: "validation-error" });
2828
- const records = selection.records ?? [];
2829
- const statuses = await Promise.all(records.map((record) => inspectElectronLaunchStatus(record)));
2830
- const managedSessions = (await Promise.all(records.map((record) => collectElectronManagedSessionTarget({
2831
- cwd,
2832
- sessionName: record.sessionName,
2833
- signal,
2834
- timeoutMs: compiledElectron.timeoutMs,
2835
- })))).filter((managedSession): managedSession is ElectronManagedSessionTarget => managedSession !== undefined);
2836
- const mismatches = managedSessions
2837
- .map((managedSession) => {
2838
- const record = records.find((candidate) => candidate.sessionName === managedSession.sessionName);
2839
- const status = record ? statuses.find((candidate) => candidate.launchId === record.launchId) : undefined;
2840
- return record && status ? buildElectronSessionMismatch({ managedSession, record, statusTargets: status.targets }) : undefined;
2841
- })
2842
- .filter((mismatch): mismatch is ElectronSessionMismatch => mismatch !== undefined);
2843
- return buildElectronStatusResult({
2844
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2845
- managedSessions,
2846
- mismatches,
2847
- records,
2848
- statuses,
2849
- });
2850
- });
2851
- }
2852
- if (compiledElectron?.action === "probe") {
2853
- return managedSessionExecutionQueue.run(async () => {
2854
- const launchRecord = compiledElectron.launchId
2855
- ? electronLaunchRecords.get(compiledElectron.launchId)
2856
- : findElectronLaunchRecordForSession(managedSessionName, electronLaunchRecords) ?? findUnambiguousActiveElectronLaunchRecord(electronLaunchRecords);
2857
- if (compiledElectron.launchId && !launchRecord) {
2858
- return buildElectronHostFailureResult({
2859
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2860
- errorText: `No wrapper-tracked Electron launch found for launchId ${compiledElectron.launchId}.`,
2861
- failureCategory: "validation-error",
2862
- });
2863
- }
2864
- if (compiledElectron.launchId && !launchRecord?.sessionName) {
2865
- return buildElectronHostFailureResult({
2866
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2867
- errorText: `electron.probe launchId ${compiledElectron.launchId} has no attached managed sessionName; reattach with connect or run electron.launch again.`,
2868
- failureCategory: "validation-error",
2869
- });
2870
- }
2871
- if (!compiledElectron.launchId && !managedSessionActive) {
2872
- return buildElectronHostFailureResult({
2873
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2874
- errorText: "electron.probe requires an active attached session. Run electron.launch or connect to an Electron debug port first.",
2875
- failureCategory: "validation-error",
2876
- });
2877
- }
2878
- const probeSessionName = compiledElectron.launchId ? launchRecord?.sessionName : managedSessionName;
2879
- if (!probeSessionName) {
2880
- return buildElectronHostFailureResult({
2881
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2882
- errorText: "electron.probe could not resolve a managed session to inspect.",
2883
- failureCategory: "validation-error",
2884
- });
2885
- }
2886
- try {
2887
- const status = launchRecord ? await inspectElectronLaunchStatus(launchRecord) : undefined;
2888
- const probe = await collectElectronProbe({ cwd, sessionName: probeSessionName, signal, timeoutMs: compiledElectron.timeoutMs });
2889
- const managedSession: ElectronManagedSessionTarget = {
2890
- sessionName: probe.sessionName,
2891
- title: probe.title ?? probe.activeTab?.title,
2892
- url: probe.url ?? probe.activeTab?.url,
2893
- };
2894
- const sessionMismatch = launchRecord && status
2895
- ? buildElectronSessionMismatch({ managedSession, record: launchRecord, statusTargets: status.targets })
2896
- : undefined;
2897
- const probeContextNote = !launchRecord
2898
- ? "No wrapper-tracked Electron launch matched this current managed session."
2899
- : !compiledElectron.launchId && launchRecord.sessionName && launchRecord.sessionName !== probe.sessionName
2900
- ? `single active Electron launch ${launchRecord.launchId} uses wrapper session ${launchRecord.sessionName}; pass electron.probe.launchId to inspect that launch session directly.`
2901
- : undefined;
2902
- const probeContext: ElectronProbeContext = {
2903
- launchId: launchRecord?.launchId,
2904
- mode: compiledElectron.launchId ? "launchId" : "current-managed-session",
2905
- note: probeContextNote,
2906
- sessionName: probe.sessionName,
2907
- };
2908
- const sessionTabTarget = normalizeSessionTabTarget({
2909
- title: probe.title ?? probe.activeTab?.title ?? probe.refSnapshot?.target?.title,
2910
- url: probe.url ?? probe.activeTab?.url ?? probe.refSnapshot?.target?.url,
2911
- });
2912
- const pageStateUpdate = sessionPageState.beginUpdate();
2913
- if (sessionTabTarget) {
2914
- sessionPageState.applyTabTarget({ sessionName: probe.sessionName, target: sessionTabTarget, update: pageStateUpdate });
2915
- }
2916
- if (probe.refSnapshot) {
2917
- sessionPageState.applyRefSnapshot({
2918
- fallbackTarget: sessionTabTarget,
2919
- sessionName: probe.sessionName,
2920
- snapshot: probe.refSnapshot,
2921
- update: pageStateUpdate,
2922
- });
2923
- }
2924
- return buildElectronProbeResult({
2925
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2926
- mismatch: sessionMismatch,
2927
- probe,
2928
- probeContext,
2929
- record: launchRecord,
2930
- sessionTabTarget,
2931
- status,
2932
- });
2933
- } catch (error) {
2934
- const errorText = error instanceof Error ? error.message : String(error);
2935
- return buildElectronHostFailureResult({
2936
- compiledElectron: redactedCompiledElectron ?? compiledElectron,
2937
- errorText: `Electron probe failed: ${errorText}`,
2938
- failureCategory: "upstream-error",
2939
- });
2940
- }
2941
- });
2942
- }
2943
- if (compiledElectron?.action === "cleanup") {
2944
- const selection = selectElectronRecords(compiledElectron, electronLaunchRecords);
2945
- if (selection.error) return buildElectronHostFailureResult({ compiledElectron: redactedCompiledElectron ?? compiledElectron, errorText: selection.error, failureCategory: "validation-error" });
2946
- const cleanupResults = await cleanupTrackedElectronLaunches(selection.records ?? [], cwd, compiledElectron.timeoutMs ?? implicitSessionCloseTimeoutMs);
2947
- return buildElectronCleanupResult(redactedCompiledElectron ?? compiledElectron, cleanupResults);
2948
- }
2949
- return undefined;
2950
- }
2951
947
 
2952
948
  export default function agentBrowserExtension(pi: ExtensionAPI) {
2953
949
  const ephemeralSessionSeed = createEphemeralSessionSeed();
@@ -2964,63 +960,118 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
2964
960
  let traceOwners = new Map<string, TraceOwner>();
2965
961
  let artifactManifest: SessionArtifactManifest | undefined;
2966
962
  let electronLaunchRecords = new Map<string, ElectronLaunchRecord>();
963
+ let ownedElectronLaunchRecords = new Map<string, ElectronLaunchRecord>();
964
+ let branchOwnedElectronLaunchIds = new Set<string>();
2967
965
  let electronChildProcesses = new Map<string, ChildProcess>();
966
+ const ownedManagedSessions = new Map<string, OwnedManagedSession>();
2968
967
  const managedSessionExecutionQueue = new AsyncExecutionQueue();
968
+ let branchStateGeneration = 0;
2969
969
 
2970
- const cleanupTrackedElectronLaunches = async (records: ElectronLaunchRecord[], cwd: string, timeoutMs = implicitSessionCloseTimeoutMs): Promise<ElectronCleanupResult[]> => {
2971
- const results: ElectronCleanupResult[] = [];
2972
- for (const record of records) {
2973
- const managedSessionCloseError = record.sessionName
2974
- ? await closeManagedSession({ cwd, sessionName: record.sessionName, timeoutMs })
2975
- : undefined;
2976
- const cleanupResult = await cleanupElectronLaunchResources({
2977
- child: electronChildProcesses.get(record.launchId),
2978
- record,
2979
- timeoutMs,
2980
- });
2981
- const result: ElectronCleanupResult = managedSessionCloseError
2982
- ? {
2983
- ...cleanupResult,
2984
- partial: true,
2985
- record: { ...cleanupResult.record, cleanupState: "partial" },
2986
- remainingResources: [...new Set(["managed-session", ...cleanupResult.remainingResources])],
2987
- steps: [{ error: managedSessionCloseError, resource: "managed-session", state: "failed" }, ...cleanupResult.steps],
2988
- summary: `Electron cleanup for ${record.launchId} is partial; managed session close failed.`,
2989
- }
2990
- : cleanupResult;
2991
- results.push(result);
2992
- electronLaunchRecords.set(record.launchId, result.record);
2993
- if (!result.partial) electronChildProcesses.delete(record.launchId);
2994
- }
2995
- return results;
2996
- };
2997
-
2998
- pi.on("session_start", async (_event, ctx) => {
970
+ const restoreBranchBackedState = (ctx: ExtensionContext, options: { resetRuntimeOwnership: boolean }): void => {
971
+ branchStateGeneration += 1;
972
+ const previousManagedSessionActive = managedSessionActive;
973
+ const previousManagedSessionName = managedSessionName;
974
+ const previousFreshSessionOrdinal = freshSessionOrdinal;
2999
975
  managedSessionBaseName = createImplicitSessionName(ctx.sessionManager.getSessionId(), ctx.cwd, ephemeralSessionSeed);
3000
- const restoredState = restoreManagedSessionStateFromBranch(ctx.sessionManager.getBranch(), managedSessionBaseName);
976
+ const branch = ctx.sessionManager.getBranch();
977
+ const branchResourceEvents = collectBranchManagedResourceEvents(branch);
978
+ const restoredState = restoreManagedSessionStateFromBranch(branch, managedSessionBaseName);
3001
979
  managedSessionActive = restoredState.active;
3002
- managedSessionName = restoredState.sessionName;
980
+ const restoredFreshSessionOrdinal = options.resetRuntimeOwnership
981
+ ? restoredState.freshSessionOrdinal
982
+ : Math.max(previousFreshSessionOrdinal, restoredState.freshSessionOrdinal);
983
+ const shouldReservePostCloseSession = !restoredState.active && restoredState.closedSessionName === restoredState.sessionName;
984
+ const alreadyReservedPostCloseSession = shouldReservePostCloseSession
985
+ && !options.resetRuntimeOwnership
986
+ && !previousManagedSessionActive
987
+ && previousFreshSessionOrdinal > restoredState.freshSessionOrdinal
988
+ && previousFreshSessionOrdinal === restoredFreshSessionOrdinal
989
+ && previousManagedSessionName === createFreshSessionName(managedSessionBaseName, ephemeralSessionSeed, restoredFreshSessionOrdinal);
990
+ const nextFreshSessionOrdinal = shouldReservePostCloseSession && !alreadyReservedPostCloseSession
991
+ ? restoredFreshSessionOrdinal + 1
992
+ : restoredFreshSessionOrdinal;
993
+ managedSessionName = shouldReservePostCloseSession
994
+ ? alreadyReservedPostCloseSession
995
+ ? previousManagedSessionName
996
+ : createFreshSessionName(managedSessionBaseName, ephemeralSessionSeed, nextFreshSessionOrdinal)
997
+ : restoredState.sessionName;
3003
998
  managedSessionCwd = ctx.cwd;
3004
- freshSessionOrdinal = restoredState.freshSessionOrdinal;
3005
- const branch = ctx.sessionManager.getBranch();
999
+ freshSessionOrdinal = nextFreshSessionOrdinal;
3006
1000
  sessionPageState = SessionPageState.fromBranch(branch);
1001
+ traceOwners = new Map<string, TraceOwner>();
3007
1002
  artifactManifest = restoreArtifactManifestFromBranch(branch);
3008
- electronLaunchRecords = restoreElectronLaunchRecordsFromBranch(ctx.sessionManager.getBranch());
1003
+ electronLaunchRecords = restoreElectronLaunchRecordsFromBranch(branch);
1004
+ if (options.resetRuntimeOwnership) {
1005
+ ownedManagedSessions.clear();
1006
+ ownedElectronLaunchRecords = new Map<string, ElectronLaunchRecord>();
1007
+ branchOwnedElectronLaunchIds = new Set<string>();
1008
+ } else {
1009
+ for (const [sessionName, closeRank] of branchResourceEvents.managedSessionCloseRanks) {
1010
+ untrackOwnedManagedSessionFromBranchClose(
1011
+ ownedManagedSessions,
1012
+ sessionName,
1013
+ branchResourceEvents.managedSessionActiveRanks.get(sessionName),
1014
+ closeRank,
1015
+ );
1016
+ }
1017
+ removeInactiveOwnedElectronLaunchRecords(
1018
+ ownedElectronLaunchRecords,
1019
+ branchOwnedElectronLaunchIds,
1020
+ electronLaunchRecords,
1021
+ branchResourceEvents.electronLaunchActiveRanks,
1022
+ branchResourceEvents.electronLaunchCleanupRanks,
1023
+ );
1024
+ }
1025
+ if (restoredState.active) {
1026
+ trackOwnedManagedSession(ownedManagedSessions, restoredState.sessionName, ctx.cwd, { branchOwned: true });
1027
+ }
1028
+ mergeActiveElectronLaunchRecords(ownedElectronLaunchRecords, electronLaunchRecords, {
1029
+ branchOwnedLaunchIds: branchOwnedElectronLaunchIds,
1030
+ markBranchOwned: true,
1031
+ });
1032
+ };
1033
+
1034
+ pi.on("session_start", async (_event, ctx) => {
1035
+ restoreBranchBackedState(ctx, { resetRuntimeOwnership: true });
3009
1036
  electronChildProcesses = new Map<string, ChildProcess>();
3010
1037
  });
3011
1038
 
1039
+ pi.on("session_tree", async (_event, ctx) => {
1040
+ await managedSessionExecutionQueue.run(async () => {
1041
+ restoreBranchBackedState(ctx, { resetRuntimeOwnership: false });
1042
+ });
1043
+ });
1044
+
3012
1045
  pi.on("session_shutdown", async (event, ctx) => {
1046
+ let preservedElectronProfileDirs: string[] = [];
3013
1047
  await managedSessionExecutionQueue.run(async () => {
3014
- const activeElectronRecords = getActiveElectronRecords(electronLaunchRecords);
3015
- if (activeElectronRecords.length > 0) {
3016
- await cleanupTrackedElectronLaunches(activeElectronRecords, ctx?.cwd ?? managedSessionCwd);
3017
- }
3018
- if (event?.reason === "quit" && managedSessionActive) {
3019
- await closeManagedSession({
3020
- cwd: managedSessionCwd,
3021
- sessionName: managedSessionName,
3022
- timeoutMs: implicitSessionCloseTimeoutMs,
3023
- });
1048
+ const shutdownCwd = ctx?.cwd ?? managedSessionCwd;
1049
+ const quitting = event?.reason === "quit";
1050
+ preservedElectronProfileDirs = quitting
1051
+ ? []
1052
+ : getActiveElectronRecords(electronLaunchRecords).map((record) => record.userDataDir);
1053
+ const electronRecordsToCleanup = quitting
1054
+ ? ownedElectronLaunchRecords
1055
+ : getOffBranchOwnedElectronLaunchRecords(ownedElectronLaunchRecords, electronLaunchRecords);
1056
+ const electronCleanupResults = await cleanupActiveElectronHostLaunches({
1057
+ cwd: shutdownCwd,
1058
+ electronChildProcesses,
1059
+ electronLaunchRecords: electronRecordsToCleanup,
1060
+ timeoutMs: implicitSessionCloseTimeoutMs,
1061
+ });
1062
+ preservedElectronProfileDirs = [...new Set([
1063
+ ...preservedElectronProfileDirs,
1064
+ ...getCleanupResultsPreservedUserDataDirs(electronCleanupResults),
1065
+ ])];
1066
+ syncElectronCleanupManagedSessions(ownedManagedSessions, electronCleanupResults);
1067
+ if (quitting) {
1068
+ await closeOwnedManagedSessions(ownedManagedSessions, implicitSessionCloseTimeoutMs);
1069
+ } else {
1070
+ await closeOwnedManagedSessionsExcept(
1071
+ ownedManagedSessions,
1072
+ managedSessionActive ? managedSessionName : undefined,
1073
+ implicitSessionCloseTimeoutMs,
1074
+ );
3024
1075
  }
3025
1076
  });
3026
1077
  managedSessionActive = false;
@@ -3028,8 +1079,11 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
3028
1079
  traceOwners = new Map<string, TraceOwner>();
3029
1080
  artifactManifest = undefined;
3030
1081
  electronLaunchRecords = new Map<string, ElectronLaunchRecord>();
1082
+ ownedElectronLaunchRecords = new Map<string, ElectronLaunchRecord>();
1083
+ branchOwnedElectronLaunchIds = new Set<string>();
3031
1084
  electronChildProcesses = new Map<string, ChildProcess>();
3032
- await cleanupSecureTempArtifacts();
1085
+ ownedManagedSessions.clear();
1086
+ await cleanupSecureTempArtifacts({ preservePaths: preservedElectronProfileDirs });
3033
1087
  });
3034
1088
 
3035
1089
  pi.on("before_agent_start", async (event) => {
@@ -3081,6 +1135,7 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
3081
1135
  return component;
3082
1136
  },
3083
1137
  async execute(_toolCallId, params, signal, onUpdate, ctx) {
1138
+ const promptPolicy = buildPromptPolicy(getLatestUserPrompt(ctx.sessionManager.getBranch()));
3084
1139
  const resolvedInput = resolveAgentBrowserInput({
3085
1140
  getBatchAnnotateValidationError,
3086
1141
  managedSessionActive,
@@ -3092,27 +1147,73 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
3092
1147
  const { toolArgs } = resolvedInput;
3093
1148
  const compiledElectron = resolvedInput.kind === "electron" ? resolvedInput.compiledElectron : undefined;
3094
1149
  const redactedCompiledElectron = resolvedInput.kind === "electron" ? resolvedInput.redactedCompiledElectron : undefined;
3095
- const electronHostResult = await handleElectronHostInput({
3096
- cleanupTrackedElectronLaunches,
3097
- compiledElectron,
3098
- cwd: ctx.cwd,
3099
- electronLaunchRecords,
3100
- implicitSessionCloseTimeoutMs,
3101
- managedSessionActive,
3102
- managedSessionExecutionQueue,
3103
- managedSessionName,
3104
- redactedCompiledElectron,
3105
- sessionPageState,
3106
- signal,
3107
- });
1150
+ const runElectronHostInput = async () => {
1151
+ const electronHostLaunchRecords = getElectronHostLaunchRecordsForInput({
1152
+ branchRecords: electronLaunchRecords,
1153
+ compiledElectron,
1154
+ ownedRecords: ownedElectronLaunchRecords,
1155
+ });
1156
+ const electronHostResult = await handleElectronHostInput({
1157
+ compiledElectron,
1158
+ cwd: ctx.cwd,
1159
+ electronChildProcesses,
1160
+ electronLaunchRecords: electronHostLaunchRecords,
1161
+ implicitSessionCloseTimeoutMs,
1162
+ managedSessionActive,
1163
+ managedSessionName,
1164
+ redactedCompiledElectron,
1165
+ sessionPageState,
1166
+ signal,
1167
+ });
1168
+ if (electronHostResult && compiledElectron?.action === "cleanup") {
1169
+ branchStateGeneration += 1;
1170
+ const cleanupRecords = isRecord(electronHostResult.details)
1171
+ && isRecord(electronHostResult.details.electron)
1172
+ && isRecord(electronHostResult.details.electron.cleanup)
1173
+ && Array.isArray(electronHostResult.details.electron.cleanup.results)
1174
+ ? electronHostResult.details.electron.cleanup.results
1175
+ : [];
1176
+ const cleanedLaunchIds = new Set<string>();
1177
+ for (const cleanupResult of cleanupRecords) {
1178
+ if (isRecord(cleanupResult) && isElectronLaunchRecord(cleanupResult.record)) {
1179
+ cleanedLaunchIds.add(cleanupResult.record.launchId);
1180
+ }
1181
+ }
1182
+ replaceWithActiveElectronLaunchRecords(ownedElectronLaunchRecords, electronHostLaunchRecords, branchOwnedElectronLaunchIds, cleanedLaunchIds);
1183
+ mergeElectronCleanupRecords(electronLaunchRecords, cleanupRecords);
1184
+ const closedSessionNames = getCleanupResultsClosedManagedSessionNames(cleanupRecords);
1185
+ syncElectronCleanupManagedSessions(ownedManagedSessions, cleanupRecords);
1186
+ for (const closedSessionName of closedSessionNames) {
1187
+ sessionPageState.clearSession(closedSessionName);
1188
+ if (closedSessionName === managedSessionName) {
1189
+ managedSessionActive = false;
1190
+ freshSessionOrdinal += 1;
1191
+ managedSessionName = createFreshSessionName(managedSessionBaseName, ephemeralSessionSeed, freshSessionOrdinal);
1192
+ }
1193
+ }
1194
+ }
1195
+ return electronHostResult;
1196
+ };
1197
+ const electronHostResult = shouldSerializeElectronHostInput(compiledElectron)
1198
+ ? await managedSessionExecutionQueue.run(runElectronHostInput)
1199
+ : await runElectronHostInput();
3108
1200
  if (electronHostResult) {
3109
1201
  return electronHostResult;
3110
1202
  }
3111
1203
 
3112
- const sessionPageStateUpdate = sessionPageState.beginUpdate();
1204
+ const explicitSessionName = extractExplicitSessionName(toolArgs);
1205
+ const serializeBrowserCommand = shouldSerializeBrowserCommand({
1206
+ explicitSessionName,
1207
+ managedSessionName,
1208
+ ownedElectronLaunchRecords,
1209
+ ownedManagedSessions,
1210
+ });
3113
1211
  const runBrowserCommand = async () => {
1212
+ const generationAtStart = branchStateGeneration;
1213
+ const sessionPageStateUpdate = sessionPageState.beginUpdate();
3114
1214
  const browserRunState: BrowserRunState = {
3115
1215
  artifactManifest,
1216
+ closedManagedSessionNames: new Set<string>(),
3116
1217
  electronChildProcesses,
3117
1218
  electronLaunchRecords,
3118
1219
  ephemeralSessionSeed,
@@ -3134,21 +1235,36 @@ export default function agentBrowserExtension(pi: ExtensionAPI) {
3134
1235
  input: resolvedInput,
3135
1236
  onUpdate,
3136
1237
  params,
1238
+ promptPolicy,
3137
1239
  sessionPageStateUpdate,
3138
1240
  signal,
3139
1241
  state: browserRunState,
3140
1242
  });
3141
- artifactManifest = browserRunState.artifactManifest;
3142
- freshSessionOrdinal = browserRunState.freshSessionOrdinal;
3143
- managedSessionActive = browserRunState.managedSessionActive;
3144
- managedSessionCwd = browserRunState.managedSessionCwd;
3145
- managedSessionName = browserRunState.managedSessionName;
1243
+ const branchStateStillCurrent = generationAtStart === branchStateGeneration;
1244
+ if (serializeBrowserCommand || branchStateStillCurrent) {
1245
+ artifactManifest = browserRunState.artifactManifest;
1246
+ freshSessionOrdinal = Math.max(freshSessionOrdinal, browserRunState.freshSessionOrdinal);
1247
+ managedSessionActive = browserRunState.managedSessionActive;
1248
+ managedSessionCwd = browserRunState.managedSessionCwd;
1249
+ managedSessionName = browserRunState.managedSessionName;
1250
+ for (const closedSessionName of browserRunState.closedManagedSessionNames) {
1251
+ untrackOwnedManagedSession(ownedManagedSessions, closedSessionName);
1252
+ }
1253
+ syncOwnedManagedSessionsFromResult(ownedManagedSessions, result, browserRunState.managedSessionCwd);
1254
+ mergeActiveElectronLaunchRecords(ownedElectronLaunchRecords, electronLaunchRecords, {
1255
+ branchOwnedLaunchIds: branchOwnedElectronLaunchIds,
1256
+ touchedLaunchIds: !result.isError
1257
+ ? getTouchedElectronLaunchIds(explicitSessionName ?? browserRunState.managedSessionName, electronLaunchRecords)
1258
+ : undefined,
1259
+ });
1260
+ if (serializeBrowserCommand) branchStateGeneration += 1;
1261
+ }
3146
1262
  return result;
3147
1263
  };
3148
1264
 
3149
- return extractExplicitSessionName(toolArgs)
3150
- ? runBrowserCommand()
3151
- : managedSessionExecutionQueue.run(runBrowserCommand);
1265
+ return serializeBrowserCommand
1266
+ ? managedSessionExecutionQueue.run(runBrowserCommand)
1267
+ : runBrowserCommand();
3152
1268
  },
3153
1269
  });
3154
1270
  }