-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-runtime.ts
More file actions
1328 lines (1222 loc) · 40 KB
/
agent-runtime.ts
File metadata and controls
1328 lines (1222 loc) · 40 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Agent runtime for verification loop support.
*
* This module provides a thin runtime wrapper that combines:
* 1. Browser session management
* 2. Snapshot/query helpers
* 3. Tracer for event emission
* 4. Assertion/verification methods
*
* The AgentRuntime is designed to be used in agent verification loops where
* you need to repeatedly take snapshots, execute actions, and verify results.
*
* @example
* ```typescript
* import { SentienceBrowser } from './browser';
* import { AgentRuntime } from './agent-runtime';
* import { urlMatches, exists } from './verification';
* import { Tracer, JsonlTraceSink } from './tracing';
*
* const browser = await SentienceBrowser.create();
* const page = await browser.newPage();
* await page.goto("https://example.com");
*
* const sink = new JsonlTraceSink("trace.jsonl");
* const tracer = new Tracer("test-run", sink);
*
* const runtime = new AgentRuntime(browser, page, tracer);
*
* // Take snapshot and run assertions
* await runtime.snapshot();
* runtime.assert(urlMatches(/example\.com/), "on_homepage");
* runtime.assert(exists("role=button"), "has_buttons");
*
* // Check if task is done
* if (runtime.assertDone(exists("text~'Success'"), "task_complete")) {
* console.log("Task completed!");
* }
* ```
*/
import * as fs from 'fs';
import * as path from 'path';
import { Page } from 'playwright';
import {
EvaluateJsRequest,
EvaluateJsResult,
BackendCapabilities,
Snapshot,
TabInfo,
TabListResult,
TabOperationResult,
} from './types';
import { AssertContext, Predicate } from './verification';
import { Tracer } from './tracing/tracer';
import { TraceEventBuilder } from './utils/trace-event-builder';
import { LLMProvider } from './llm-provider';
import { FailureArtifactBuffer, FailureArtifactsOptions } from './failure-artifacts';
import { SentienceBrowser } from './browser';
import type { ToolRegistry } from './tools/registry';
import {
CaptchaContext,
CaptchaHandlingError,
CaptchaOptions,
CaptchaResolution,
CaptchaSource,
} from './captcha/types';
// Define a minimal browser interface to avoid circular dependencies
interface BrowserLike {
snapshot(page: Page, options?: Record<string, any>): Promise<Snapshot>;
}
export interface AttachOptions {
apiKey?: string;
apiUrl?: string;
toolRegistry?: ToolRegistry;
browser?: BrowserLike;
}
const DEFAULT_CAPTCHA_OPTIONS: Required<Omit<CaptchaOptions, 'handler' | 'resetSession'>> = {
policy: 'abort',
minConfidence: 0.7,
timeoutMs: 120_000,
pollMs: 1_000,
maxRetriesNewSession: 1,
};
/**
* Assertion record for accumulation and step_end emission.
*/
export interface AssertionRecord {
label: string;
passed: boolean;
required: boolean;
reason: string;
details: Record<string, any>;
}
export interface EventuallyOptions {
timeoutMs?: number;
pollMs?: number;
snapshotOptions?: Record<string, any>;
/** If set, `.eventually()` will treat snapshots below this confidence as failures and resnapshot. */
minConfidence?: number;
/** Max number of snapshot attempts to get above minConfidence before declaring exhaustion. */
maxSnapshotAttempts?: number;
/** Optional: vision fallback provider used after snapshot exhaustion (last resort). */
visionProvider?: LLMProvider;
/** Optional: override vision system prompt (YES/NO only). */
visionSystemPrompt?: string;
/** Optional: override vision user prompt (YES/NO only). */
visionUserPrompt?: string;
}
export class AssertionHandle {
private runtime: AgentRuntime;
private predicate: Predicate;
private label: string;
private required: boolean;
constructor(runtime: AgentRuntime, predicate: Predicate, label: string, required: boolean) {
this.runtime = runtime;
this.predicate = predicate;
this.label = label;
this.required = required;
}
once(): boolean {
return this.runtime.assert(this.predicate, this.label, this.required);
}
async eventually(options: EventuallyOptions = {}): Promise<boolean> {
const timeoutMs = options.timeoutMs ?? 10_000;
const pollMs = options.pollMs ?? 250;
const snapshotOptions = options.snapshotOptions;
const minConfidence = options.minConfidence;
const maxSnapshotAttempts = options.maxSnapshotAttempts ?? 3;
const visionProvider = options.visionProvider;
const visionSystemPrompt = options.visionSystemPrompt;
const visionUserPrompt = options.visionUserPrompt;
const deadline = Date.now() + timeoutMs;
let attempt = 0;
let snapshotAttempt = 0;
let lastOutcome: ReturnType<Predicate> | null = null;
while (true) {
attempt += 1;
await this.runtime.snapshot(snapshotOptions);
snapshotAttempt += 1;
const diagnostics = this.runtime.lastSnapshot?.diagnostics;
const confidence = diagnostics?.confidence;
if (
typeof minConfidence === 'number' &&
typeof confidence === 'number' &&
Number.isFinite(confidence) &&
confidence < minConfidence
) {
lastOutcome = {
passed: false,
reason: `Snapshot confidence ${confidence.toFixed(3)} < minConfidence ${minConfidence.toFixed(3)}`,
details: {
reason_code: 'snapshot_low_confidence',
confidence,
min_confidence: minConfidence,
snapshot_attempt: snapshotAttempt,
diagnostics,
},
};
(this.runtime as any)._recordOutcome(
lastOutcome,
this.label,
this.required,
{ eventually: true, attempt, snapshot_attempt: snapshotAttempt, final: false },
false
);
if (snapshotAttempt >= maxSnapshotAttempts) {
// Optional: vision fallback after snapshot exhaustion (last resort).
// Keeps the assertion surface invariant; only perception changes.
if (visionProvider && visionProvider.supportsVision?.()) {
try {
const buf = (await (this.runtime.page as any).screenshot({ type: 'png' })) as Buffer;
const imageBase64 = Buffer.from(buf).toString('base64');
const sys =
visionSystemPrompt ?? 'You are a strict visual verifier. Answer only YES or NO.';
const user =
visionUserPrompt ??
`Given the screenshot, is the following condition satisfied?\n\n${this.label}\n\nAnswer YES or NO.`;
const resp = await visionProvider.generateWithImage(sys, user, imageBase64, {
temperature: 0.0,
});
const text = (resp.content || '').trim().toLowerCase();
const passed = text.startsWith('yes');
const finalOutcome = {
passed,
reason: passed ? 'vision_fallback_yes' : 'vision_fallback_no',
details: {
reason_code: passed ? 'vision_fallback_pass' : 'vision_fallback_fail',
vision_response: resp.content,
min_confidence: minConfidence,
snapshot_attempts: snapshotAttempt,
},
};
(this.runtime as any)._recordOutcome(
finalOutcome,
this.label,
this.required,
{
eventually: true,
attempt,
snapshot_attempt: snapshotAttempt,
final: true,
vision_fallback: true,
},
true
);
if (this.required && !passed) {
(this.runtime as any).persistFailureArtifacts(
`assert_eventually_failed:${this.label}`
);
}
return passed;
} catch {
// fall through to snapshot_exhausted
}
}
const finalOutcome = {
passed: false,
reason: `Snapshot exhausted after ${snapshotAttempt} attempt(s) below minConfidence ${minConfidence.toFixed(3)}`,
details: {
reason_code: 'snapshot_exhausted',
confidence,
min_confidence: minConfidence,
snapshot_attempts: snapshotAttempt,
diagnostics,
},
};
(this.runtime as any)._recordOutcome(
finalOutcome,
this.label,
this.required,
{
eventually: true,
attempt,
snapshot_attempt: snapshotAttempt,
final: true,
exhausted: true,
},
true
);
if (this.required) {
(this.runtime as any).persistFailureArtifacts(`assert_eventually_failed:${this.label}`);
}
return false;
}
if (Date.now() >= deadline) {
(this.runtime as any)._recordOutcome(
lastOutcome,
this.label,
this.required,
{
eventually: true,
attempt,
snapshot_attempt: snapshotAttempt,
final: true,
timeout: true,
},
true
);
if (this.required) {
(this.runtime as any).persistFailureArtifacts(
`assert_eventually_timeout:${this.label}`
);
}
return false;
}
await new Promise(resolve => setTimeout(resolve, pollMs));
continue;
}
lastOutcome = this.predicate((this.runtime as any).ctx());
// Emit attempt event (not recorded in step_end)
(this.runtime as any)._recordOutcome(
lastOutcome,
this.label,
this.required,
{ eventually: true, attempt, final: false },
false
);
if (lastOutcome.passed) {
// Record final success once
(this.runtime as any)._recordOutcome(
lastOutcome,
this.label,
this.required,
{ eventually: true, attempt, final: true },
true
);
return true;
}
if (Date.now() >= deadline) {
// Record final failure once
(this.runtime as any)._recordOutcome(
lastOutcome,
this.label,
this.required,
{ eventually: true, attempt, final: true, timeout: true },
true
);
if (this.required) {
(this.runtime as any).persistFailureArtifacts(`assert_eventually_timeout:${this.label}`);
}
return false;
}
await new Promise(resolve => setTimeout(resolve, pollMs));
}
}
}
/**
* Runtime wrapper for agent verification loops.
*
* Provides ergonomic methods for:
* - snapshot(): Take page snapshot
* - assert(): Evaluate assertion predicates
* - assertDone(): Assert task completion (required assertion)
*
* The runtime manages assertion state per step and emits verification events
* to the tracer for Studio timeline display.
*/
export class AgentRuntime {
/** Browser instance for taking snapshots */
readonly browser: BrowserLike;
/** Playwright Page for browser interaction */
page: Page;
/** Tracer for event emission */
readonly tracer: Tracer;
/** Optional ToolRegistry for LLM-callable tools */
readonly toolRegistry?: ToolRegistry;
/** Current step identifier */
stepId: string | null = null;
/** Current step index (0-based) */
stepIndex: number = 0;
/** Most recent snapshot (for assertion context) */
lastSnapshot: Snapshot | null = null;
private stepPreSnapshot: Snapshot | null = null;
private stepPreUrl: string | null = null;
/** Best-effort download records (Playwright downloads) */
private downloads: Array<Record<string, any>> = [];
/** Tab registry for tab operations */
private tabRegistry: Map<string, Page> = new Map();
private tabIds: WeakMap<Page, string> = new WeakMap();
/** Failure artifact buffer (Phase 1) */
private artifactBuffer: FailureArtifactBuffer | null = null;
private artifactTimer: NodeJS.Timeout | null = null;
/** Assertions accumulated during current step */
private assertionsThisStep: AssertionRecord[] = [];
private stepGoal: string | null = null;
private lastAction: string | null = null;
/** Task completion tracking */
private taskDone: boolean = false;
private taskDoneLabel: string | null = null;
/** CAPTCHA handling (optional, disabled by default) */
private captchaOptions: CaptchaOptions | null = null;
private captchaRetryCount: number = 0;
private static similarity(a: string, b: string): number {
const s1 = a.toLowerCase();
const s2 = b.toLowerCase();
if (!s1 || !s2) return 0;
if (s1 === s2) return 1;
// Bigram overlap (cheap, robust enough for suggestions)
const bigrams = (s: string): string[] => {
const out: string[] = [];
for (let i = 0; i < s.length - 1; i++) out.push(s.slice(i, i + 2));
return out;
};
const a2 = bigrams(s1);
const b2 = bigrams(s2);
const setB = new Set(b2);
let common = 0;
for (const g of a2) if (setB.has(g)) common += 1;
return (2 * common) / (a2.length + b2.length + 1e-9);
}
private static stringifyEvalValue(value: any): string {
if (value === null || value === undefined) {
return 'null';
}
if (Array.isArray(value) || typeof value === 'object') {
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
return String(value);
}
_recordOutcome(
outcome: ReturnType<Predicate>,
label: string,
required: boolean,
extra: Record<string, any> | null,
recordInStep: boolean
): void {
const details = { ...(outcome.details || {}) } as Record<string, any>;
// Failure intelligence: nearest matches for selector-driven assertions
if (!outcome.passed && this.lastSnapshot && typeof details.selector === 'string') {
const selector = details.selector;
const scored: Array<{ score: number; el: any }> = [];
for (const el of this.lastSnapshot.elements) {
const hay = el.name ?? el.text ?? '';
if (!hay) continue;
const score = AgentRuntime.similarity(selector, hay);
scored.push({ score, el });
}
scored.sort((x, y) => y.score - x.score);
details.nearest_matches = scored.slice(0, 3).map(({ score, el }) => ({
id: el.id,
role: el.role,
text: (el.text ?? '').toString().slice(0, 80),
name: (el.name ?? '').toString().slice(0, 80),
score: Math.round(score * 10_000) / 10_000,
}));
}
const record: AssertionRecord & Record<string, any> = {
label,
passed: outcome.passed,
required,
reason: outcome.reason,
details,
...(extra || {}),
};
if (recordInStep) {
this.assertionsThisStep.push(record);
}
this.tracer.emit(
'verification',
{
kind: 'assert',
...record,
},
this.stepId || undefined
);
}
check(predicate: Predicate, label: string, required: boolean = false): AssertionHandle {
return new AssertionHandle(this, predicate, label, required);
}
/**
* Create AgentRuntime from a raw Playwright Page (sidecar mode).
*/
static fromPlaywrightPage(page: Page, tracer: Tracer, options?: AttachOptions): AgentRuntime {
const browser =
options?.browser ??
((): BrowserLike => {
const sentienceBrowser = SentienceBrowser.fromPage(page, options?.apiKey, options?.apiUrl);
return {
snapshot: async (_page: Page, snapshotOptions?: Record<string, any>) =>
sentienceBrowser.snapshot(snapshotOptions),
};
})();
return new AgentRuntime(browser, page, tracer, options?.toolRegistry);
}
/**
* Sidecar alias for fromPlaywrightPage().
*/
static attach(page: Page, tracer: Tracer, options?: AttachOptions): AgentRuntime {
return AgentRuntime.fromPlaywrightPage(page, tracer, options);
}
/**
* Create a new AgentRuntime.
*
* @param browser - Browser instance for taking snapshots
* @param page - Playwright Page for browser interaction
* @param tracer - Tracer for emitting verification events
*/
constructor(browser: BrowserLike, page: Page, tracer: Tracer, toolRegistry?: ToolRegistry) {
this.browser = browser;
this.page = page;
this.tracer = tracer;
this.toolRegistry = toolRegistry;
// Best-effort download tracking (does not change behavior unless a download occurs).
try {
this.page.on('download', download => {
void this.trackDownload(download);
});
} catch {
// ignore
}
}
capabilities(): BackendCapabilities {
const hasTabs = typeof (this as any).listTabs === 'function';
const hasEval = typeof (this as any).evaluateJs === 'function';
const hasKeyboard = Boolean((this.page as any)?.keyboard);
const hasDownloads = this.downloads.length >= 0;
let hasPermissions = false;
try {
const context =
typeof (this.page as any)?.context === 'function' ? (this.page as any).context() : null;
hasPermissions =
Boolean(context) &&
typeof context.clearPermissions === 'function' &&
typeof context.grantPermissions === 'function';
} catch {
hasPermissions = false;
}
let hasFiles = false;
if (this.toolRegistry) {
hasFiles = Boolean(this.toolRegistry.get('read_file'));
}
return {
tabs: hasTabs,
evaluate_js: hasEval,
downloads: hasDownloads,
filesystem_tools: hasFiles,
keyboard: hasKeyboard,
permissions: hasPermissions,
};
}
can(name: keyof BackendCapabilities): boolean {
return Boolean(this.capabilities()[name]);
}
/**
* Configure CAPTCHA handling (disabled by default unless set).
*/
setCaptchaOptions(options: CaptchaOptions): void {
this.captchaOptions = {
...DEFAULT_CAPTCHA_OPTIONS,
...options,
};
this.captchaRetryCount = 0;
}
/**
* Build assertion context from current state.
*/
private ctx(): AssertContext {
let url: string | null = null;
if (this.lastSnapshot) {
url = this.lastSnapshot.url;
} else if (this.page) {
url = this.page.url();
}
return {
snapshot: this.lastSnapshot,
url,
stepId: this.stepId,
downloads: this.downloads,
};
}
private async trackDownload(download: any): Promise<void> {
const rec: Record<string, any> = {
status: 'started',
suggested_filename: download?.suggestedFilename?.() ?? download?.suggested_filename,
url: download?.url?.() ?? download?.url,
};
this.downloads.push(rec);
try {
const p = (await download.path?.()) as string | null;
rec.status = 'completed';
if (p) {
rec.path = p;
try {
// Best-effort size and mime type (no new deps).
rec.size_bytes = Number(fs.statSync(p).size);
const ext = String(path.extname(p) || '').toLowerCase();
const mimeByExt: Record<string, string> = {
'.pdf': 'application/pdf',
'.txt': 'text/plain',
'.csv': 'text/csv',
'.json': 'application/json',
'.zip': 'application/zip',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp',
};
if (mimeByExt[ext]) rec.mime_type = mimeByExt[ext];
} catch {
// ignore
}
}
} catch (e: any) {
rec.status = 'failed';
rec.error = String(e?.message ?? e);
}
}
/**
* Take a snapshot of the current page state.
*
* This updates lastSnapshot which is used as context for assertions.
*
* @param options - Options passed through to browser.snapshot()
* @returns Snapshot of current page state
*/
async snapshot(options?: Record<string, any>): Promise<Snapshot> {
const { _skipCaptchaHandling, ...snapshotOptions } = options || {};
this.lastSnapshot = await this.browser.snapshot(this.page, snapshotOptions);
if (this.lastSnapshot && !this.stepPreSnapshot) {
this.stepPreSnapshot = this.lastSnapshot;
this.stepPreUrl = this.lastSnapshot.url;
}
if (!_skipCaptchaHandling) {
await this.handleCaptchaIfNeeded(this.lastSnapshot, 'gateway');
}
return this.lastSnapshot;
}
/**
* Evaluate JavaScript in the page context.
*/
async evaluateJs(request: EvaluateJsRequest): Promise<EvaluateJsResult> {
try {
const value = await this.page.evaluate(request.code);
const text = AgentRuntime.stringifyEvalValue(value);
const maxChars = request.max_output_chars ?? 4000;
const truncate = request.truncate ?? true;
let truncated = false;
let finalText = text;
if (truncate && finalText.length > maxChars) {
finalText = `${finalText.slice(0, maxChars)}...`;
truncated = true;
}
return {
ok: true,
value,
text: finalText,
truncated,
};
} catch (err: any) {
return { ok: false, error: String(err?.message ?? err) };
}
}
/**
* List open tabs in the current browser context.
*/
async listTabs(): Promise<TabListResult> {
const context = (this.page as any)?.context?.();
if (!context || typeof context.pages !== 'function') {
return { ok: false, tabs: [], error: 'unsupported_capability' };
}
this.pruneTabs();
const pages: Page[] = context.pages();
const tabs: TabInfo[] = [];
for (const page of pages) {
const tab_id = this.ensureTabId(page);
let title: string | null = null;
try {
title = await page.title();
} catch {
title = null;
}
let url: string | null = null;
try {
url = page.url();
} catch {
url = null;
}
tabs.push({ tab_id, url, title, is_active: page === this.page });
}
return { ok: true, tabs };
}
/**
* Open a new tab and navigate to the URL.
*/
async openTab(url: string): Promise<TabOperationResult> {
const context = (this.page as any)?.context?.();
if (!context || typeof context.newPage !== 'function') {
return { ok: false, error: 'unsupported_capability' };
}
this.pruneTabs();
try {
const page = await context.newPage();
await page.goto(url);
this.page = page;
const tab_id = this.ensureTabId(page);
let title: string | null = null;
try {
title = await page.title();
} catch {
title = null;
}
return { ok: true, tab: { tab_id, url: page.url?.() ?? url, title, is_active: true } };
} catch (err: any) {
return { ok: false, error: String(err?.message ?? err) };
}
}
/**
* Switch to an existing tab by id.
*/
async switchTab(tab_id: string): Promise<TabOperationResult> {
this.pruneTabs();
const page = this.tabRegistry.get(tab_id);
if (!page) {
return { ok: false, error: `unknown tab_id: ${tab_id}` };
}
this.page = page;
try {
await page.bringToFront();
} catch {
// best-effort
}
let title: string | null = null;
try {
title = await page.title();
} catch {
title = null;
}
return {
ok: true,
tab: { tab_id, url: page.url?.() ?? null, title, is_active: true },
};
}
/**
* Close a tab by id.
*/
async closeTab(tab_id: string): Promise<TabOperationResult> {
this.pruneTabs();
const page = this.tabRegistry.get(tab_id);
if (!page) {
return { ok: false, error: `unknown tab_id: ${tab_id}` };
}
let title: string | null = null;
try {
title = await page.title();
} catch {
title = null;
}
const wasActive = page === this.page;
try {
await page.close();
} catch (err: any) {
return { ok: false, error: String(err?.message ?? err) };
}
this.tabRegistry.delete(tab_id);
if (wasActive) {
const context = (page as any)?.context?.();
const pages: Page[] = context?.pages?.() ?? [];
if (pages.length > 0) {
this.page = pages[0];
}
}
return {
ok: true,
tab: { tab_id, url: page.url?.() ?? null, title, is_active: wasActive },
};
}
private ensureTabId(page: Page): string {
const existing = this.tabIds.get(page);
if (existing) {
return existing;
}
const tab_id = `tab-${Date.now()}-${Math.random().toString(16).slice(2)}`;
this.tabIds.set(page, tab_id);
this.tabRegistry.set(tab_id, page);
return tab_id;
}
private pruneTabs(): void {
for (const [tab_id, page] of this.tabRegistry.entries()) {
try {
const isClosed = (page as any).isClosed?.();
if (isClosed) {
this.tabRegistry.delete(tab_id);
}
} catch {
// ignore
}
}
}
private isCaptchaDetected(snapshot: Snapshot): boolean {
const options = this.captchaOptions;
if (!options) {
return false;
}
const captcha = snapshot.diagnostics?.captcha;
if (!captcha || !captcha.detected) {
return false;
}
// Many pages load CAPTCHA libraries proactively. Only block when we have
// evidence it's actually present/active (iframe/url/text hits), otherwise
// interactive runs can "do nothing" and time out.
const evidence = captcha.evidence;
const iframeHits = evidence?.iframe_src_hits ?? [];
const urlHits = evidence?.url_hits ?? [];
const textHits = evidence?.text_hits ?? [];
if (iframeHits.length === 0 && urlHits.length === 0 && textHits.length === 0) {
return false;
}
const confidence = captcha.confidence ?? 0;
const minConfidence = options.minConfidence ?? DEFAULT_CAPTCHA_OPTIONS.minConfidence;
return confidence >= minConfidence;
}
private buildCaptchaContext(snapshot: Snapshot, source: CaptchaSource): CaptchaContext {
return {
runId: this.tracer.getRunId(),
stepIndex: this.stepIndex,
url: snapshot.url,
source,
captcha: snapshot.diagnostics?.captcha ?? null,
evaluateJs: async (code: string) => {
const result = await this.evaluateJs({ code });
if (!result.ok) {
throw new Error(result.error ?? 'evaluateJs failed');
}
return result.value;
},
};
}
private emitCaptchaEvent(reasonCode: string, details: Record<string, any> = {}): void {
this.tracer.emit(
'verification',
{
kind: 'captcha',
passed: false,
label: reasonCode,
details: { reason_code: reasonCode, ...details },
},
this.stepId || undefined
);
}
private async handleCaptchaIfNeeded(snapshot: Snapshot, source: CaptchaSource): Promise<void> {
if (!this.captchaOptions) {
return;
}
if (!this.isCaptchaDetected(snapshot)) {
return;
}
const options = this.captchaOptions;
const minConfidence = options.minConfidence ?? DEFAULT_CAPTCHA_OPTIONS.minConfidence;
const captcha = snapshot.diagnostics?.captcha ?? null;
this.emitCaptchaEvent('captcha_detected', { captcha, min_confidence: minConfidence });
let resolution: CaptchaResolution;
if (options.policy === 'callback') {
if (!options.handler) {
this.emitCaptchaEvent('captcha_handler_error');
throw new CaptchaHandlingError(
'captcha_handler_error',
'Captcha handler is required for policy="callback".'
);
}
try {
resolution = await options.handler(this.buildCaptchaContext(snapshot, source));
} catch (err: any) {
this.emitCaptchaEvent('captcha_handler_error', { error: String(err?.message || err) });
throw new CaptchaHandlingError('captcha_handler_error', 'Captcha handler failed.', {
error: String(err?.message || err),
});
}
if (!resolution || !resolution.action) {
this.emitCaptchaEvent('captcha_handler_error');
throw new CaptchaHandlingError(
'captcha_handler_error',
'Captcha handler returned an invalid resolution.'
);
}
} else {
resolution = { action: 'abort' };
}
await this.applyCaptchaResolution(resolution, snapshot, source);
}
private async applyCaptchaResolution(
resolution: CaptchaResolution,
snapshot: Snapshot,
source: CaptchaSource
): Promise<void> {
const options = this.captchaOptions || DEFAULT_CAPTCHA_OPTIONS;
if (resolution.action === 'abort') {
this.emitCaptchaEvent('captcha_policy_abort', { message: resolution.message });
throw new CaptchaHandlingError(
'captcha_policy_abort',
resolution.message || 'Captcha detected. Aborting per policy.'
);
}
if (resolution.action === 'retry_new_session') {
this.captchaRetryCount += 1;
this.emitCaptchaEvent('captcha_retry_new_session');
if (this.captchaRetryCount > (options.maxRetriesNewSession ?? 1)) {
this.emitCaptchaEvent('captcha_retry_exhausted');
throw new CaptchaHandlingError(
'captcha_retry_exhausted',
'Captcha retry_new_session exhausted.'
);
}
const resetSession = this.captchaOptions?.resetSession;
if (!resetSession) {
throw new CaptchaHandlingError(
'captcha_retry_new_session',
'resetSession callback is required for retry_new_session.'
);
}
await resetSession();
return;
}
if (resolution.action === 'wait_until_cleared') {
const timeoutMs =
resolution.timeoutMs ?? options.timeoutMs ?? DEFAULT_CAPTCHA_OPTIONS.timeoutMs;
const pollMs = resolution.pollMs ?? options.pollMs ?? DEFAULT_CAPTCHA_OPTIONS.pollMs;
await this.waitUntilCleared(timeoutMs, pollMs, snapshot, source);
this.emitCaptchaEvent('captcha_resumed');
}
}
private async waitUntilCleared(
timeoutMs: number,
pollMs: number,
snapshot: Snapshot,
source: CaptchaSource
): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (Date.now() <= deadline) {
await new Promise(res => setTimeout(res, pollMs));
const next = await this.snapshot({ _skipCaptchaHandling: true });
if (!this.isCaptchaDetected(next)) {
this.emitCaptchaEvent('captcha_cleared', { source });
return;
}
}
this.emitCaptchaEvent('captcha_wait_timeout', { timeout_ms: timeoutMs });
throw new CaptchaHandlingError('captcha_wait_timeout', 'Captcha wait_until_cleared timed out.');
}
/**
* Enable failure artifact buffer (Phase 1).
*/
enableFailureArtifacts(options: FailureArtifactsOptions = {}): void {
this.artifactBuffer = new FailureArtifactBuffer(this.tracer.getRunId(), options);
const fps = this.artifactBuffer.getOptions().fps;
if (fps && fps > 0) {
const intervalMs = Math.max(1, Math.floor(1000 / fps));
this.artifactTimer = setInterval(() => {
this.captureArtifactFrame().catch(() => {
// best-effort
});
}, intervalMs);
}
}
/**
* Disable failure artifact buffer and stop background capture.
*/
disableFailureArtifacts(): void {
if (this.artifactTimer) {
clearInterval(this.artifactTimer);
this.artifactTimer = null;
}
}