-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_runtime.py
More file actions
1537 lines (1351 loc) · 57.1 KB
/
agent_runtime.py
File metadata and controls
1537 lines (1351 loc) · 57.1 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 (via BrowserBackend protocol)
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 usage with browser-use:
from browser_use import BrowserSession, BrowserProfile
from predicate import get_extension_dir
from predicate.backends import BrowserUseAdapter
from predicate.agent_runtime import AgentRuntime
from predicate.verification import url_matches, exists
from predicate.tracing import Tracer, JsonlTraceSink
# Setup browser-use with Sentience extension
profile = BrowserProfile(args=[f"--load-extension={get_extension_dir()}"])
session = BrowserSession(browser_profile=profile)
await session.start()
# Create adapter and backend
adapter = BrowserUseAdapter(session)
backend = await adapter.create_backend()
# Navigate using browser-use
page = await session.get_current_page()
await page.goto("https://example.com")
# Create runtime with backend
sink = JsonlTraceSink("trace.jsonl")
tracer = Tracer(run_id="test-run", sink=sink)
runtime = AgentRuntime(backend=backend, tracer=tracer)
# Take snapshot and run assertions
await runtime.snapshot()
runtime.assert_(url_matches(r"example\\.com"), label="on_homepage")
runtime.assert_(exists("role=button"), label="has_buttons")
# Check if task is done
if runtime.assert_done(exists("text~'Success'"), label="task_complete"):
print("Task completed!")
Example usage with AsyncSentienceBrowser (backward compatible):
from predicate import AsyncSentienceBrowser
from predicate.agent_runtime import AgentRuntime
async with AsyncSentienceBrowser() as browser:
page = await browser.new_page()
await page.goto("https://example.com")
runtime = await AgentRuntime.from_sentience_browser(
browser=browser,
page=page,
tracer=tracer,
)
await runtime.snapshot()
"""
from __future__ import annotations
import asyncio
import difflib
import hashlib
import time
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from .captcha import (
CaptchaContext,
CaptchaHandlingError,
CaptchaOptions,
CaptchaResolution,
PageControlHook,
)
from .failure_artifacts import FailureArtifactBuffer, FailureArtifactsOptions
from .models import (
EvaluateJsRequest,
EvaluateJsResult,
LLMStepData,
Snapshot,
SnapshotOptions,
TabInfo,
TabListResult,
TabOperationResult,
)
from .tools import BackendCapabilities, ToolRegistry
from .trace_event_builder import TraceEventBuilder
from .verification import AssertContext, AssertOutcome, Predicate
if TYPE_CHECKING:
from playwright.async_api import Page
from .backends.protocol import BrowserBackend
from .browser import AsyncSentienceBrowser
from .tracing import Tracer
class AgentRuntime:
"""
Runtime wrapper for agent verification loops.
Provides ergonomic methods for:
- snapshot(): Take page snapshot
- assert_(): Evaluate assertion predicates
- assert_done(): Assert task completion (required assertion)
The runtime manages assertion state per step and emits verification events
to the tracer for Studio timeline display.
Attributes:
backend: BrowserBackend instance for browser operations
tracer: Tracer for event emission
step_id: Current step identifier
step_index: Current step index (0-based)
last_snapshot: Most recent snapshot (for assertion context)
"""
def __init__(
self,
backend: BrowserBackend,
tracer: Tracer,
snapshot_options: SnapshotOptions | None = None,
predicate_api_key: str | None = None,
sentience_api_key: str | None = None,
tool_registry: ToolRegistry | None = None,
):
"""
Initialize agent runtime with any BrowserBackend-compatible browser.
Args:
backend: Any browser implementing BrowserBackend protocol.
Examples:
- CDPBackendV0 (for browser-use via BrowserUseAdapter)
- PlaywrightBackend (future, for direct Playwright)
tracer: Tracer for emitting verification events
snapshot_options: Default options for snapshots
predicate_api_key: Canonical API key parameter for Pro/Enterprise tier.
sentience_api_key: Backward-compatible API key alias (legacy name).
tool_registry: Optional ToolRegistry for LLM-callable tools
"""
self.backend = backend
self.tracer = tracer
self.tool_registry = tool_registry
# Build default snapshot options with API key if provided
default_opts = snapshot_options or SnapshotOptions()
effective_api_key = predicate_api_key or sentience_api_key
if effective_api_key:
default_opts.predicate_api_key = effective_api_key
default_opts.sentience_api_key = effective_api_key
if default_opts.use_api is None:
default_opts.use_api = True
self._snapshot_options = default_opts
# Step tracking
self.step_id: str | None = None
# 0-based step indexing (first auto-generated step_id is "step-0")
self.step_index: int = -1
# Snapshot state
self.last_snapshot: Snapshot | None = None
self._step_pre_snapshot: Snapshot | None = None
self._step_pre_url: str | None = None
# Failure artifacts (Phase 1)
self._artifact_buffer: FailureArtifactBuffer | None = None
self._artifact_timer_task: asyncio.Task | None = None
# Cached URL (updated on snapshot or explicit get_url call)
self._cached_url: str | None = None
# Assertions accumulated during current step
self._assertions_this_step: list[dict[str, Any]] = []
self._step_goal: str | None = None
self._last_action: str | None = None
self._last_action_error: str | None = None
self._last_action_outcome: str | None = None
self._last_action_duration_ms: int | None = None
self._last_action_success: bool | None = None
# Task completion tracking
self._task_done: bool = False
self._task_done_label: str | None = None
# CAPTCHA handling (optional, disabled by default)
self._captcha_options: CaptchaOptions | None = None
self._captcha_retry_count: int = 0
@classmethod
def from_playwright_page(
cls,
page: Page,
tracer: Tracer,
snapshot_options: SnapshotOptions | None = None,
predicate_api_key: str | None = None,
sentience_api_key: str | None = None,
tool_registry: ToolRegistry | None = None,
) -> AgentRuntime:
"""
Create AgentRuntime from a raw Playwright Page (sidecar mode).
Args:
page: Playwright Page for browser interaction
tracer: Tracer for emitting verification events
snapshot_options: Default options for snapshots
predicate_api_key: Canonical API key parameter for Pro/Enterprise tier.
sentience_api_key: Backward-compatible API key alias (legacy name).
tool_registry: Optional ToolRegistry for LLM-callable tools
Returns:
AgentRuntime instance
"""
from .backends.playwright_backend import PlaywrightBackend
backend = PlaywrightBackend(page)
return cls(
backend=backend,
tracer=tracer,
snapshot_options=snapshot_options,
predicate_api_key=predicate_api_key,
sentience_api_key=sentience_api_key,
tool_registry=tool_registry,
)
@classmethod
def attach(
cls,
page: Page,
tracer: Tracer,
snapshot_options: SnapshotOptions | None = None,
predicate_api_key: str | None = None,
sentience_api_key: str | None = None,
tool_registry: ToolRegistry | None = None,
) -> AgentRuntime:
"""
Sidecar alias for from_playwright_page().
"""
return cls.from_playwright_page(
page=page,
tracer=tracer,
snapshot_options=snapshot_options,
predicate_api_key=predicate_api_key,
sentience_api_key=sentience_api_key,
tool_registry=tool_registry,
)
@classmethod
async def from_sentience_browser(
cls,
browser: AsyncSentienceBrowser,
page: Page,
tracer: Tracer,
snapshot_options: SnapshotOptions | None = None,
predicate_api_key: str | None = None,
sentience_api_key: str | None = None,
) -> AgentRuntime:
"""
Create AgentRuntime from AsyncSentienceBrowser (backward compatibility).
This factory method wraps an AsyncSentienceBrowser + Page combination
into the new BrowserBackend-based AgentRuntime.
Args:
browser: AsyncSentienceBrowser instance
page: Playwright Page for browser interaction
tracer: Tracer for emitting verification events
snapshot_options: Default options for snapshots
predicate_api_key: Canonical API key parameter for Pro/Enterprise tier.
sentience_api_key: Backward-compatible API key alias (legacy name).
Returns:
AgentRuntime instance
"""
from .backends.playwright_backend import PlaywrightBackend
backend = PlaywrightBackend(page)
runtime = cls(
backend=backend,
tracer=tracer,
snapshot_options=snapshot_options,
predicate_api_key=predicate_api_key,
sentience_api_key=sentience_api_key,
)
# Store browser reference for snapshot() to use
runtime._legacy_browser = browser
runtime._legacy_page = page
return runtime
def _ctx(self) -> AssertContext:
"""
Build assertion context from current state.
Returns:
AssertContext with current snapshot and URL
"""
url = None
if self.last_snapshot is not None:
url = self.last_snapshot.url
elif self._cached_url:
url = self._cached_url
downloads = None
try:
downloads = getattr(self.backend, "downloads", None)
except Exception:
downloads = None
return AssertContext(
snapshot=self.last_snapshot, url=url, step_id=self.step_id, downloads=downloads
)
async def get_url(self) -> str:
"""
Get current page URL.
Returns:
Current page URL
"""
url = await self.backend.get_url()
self._cached_url = url
return url
async def snapshot(self, emit_trace: bool = True, **kwargs: Any) -> Snapshot:
"""
Take a snapshot of the current page state.
This updates last_snapshot which is used as context for assertions.
When emit_trace=True (default), automatically emits a 'snapshot' trace event
with screenshot_base64 for Sentience Studio visualization.
Args:
emit_trace: If True (default), emit a 'snapshot' trace event with screenshot.
Set to False to disable automatic trace emission.
**kwargs: Override default snapshot options for this call.
Common options:
- limit: Maximum elements to return
- goal: Task goal for ordinal support
- screenshot: Include screenshot
- show_overlay: Show visual overlay
Returns:
Snapshot of current page state
Example:
>>> # Default: snapshot with auto-emit trace event
>>> snapshot = await runtime.snapshot()
>>> # Disable auto-emit for manual control
>>> snapshot = await runtime.snapshot(emit_trace=False)
>>> # Later, manually emit if needed:
>>> tracer.emit_snapshot(snapshot, step_id=runtime.step_id)
"""
# Check if using legacy browser (backward compat)
if hasattr(self, "_legacy_browser") and hasattr(self, "_legacy_page"):
self.last_snapshot = await self._legacy_browser.snapshot(self._legacy_page, **kwargs)
if self.last_snapshot is not None:
self._cached_url = self.last_snapshot.url
if self._step_pre_snapshot is None:
self._step_pre_snapshot = self.last_snapshot
self._step_pre_url = self.last_snapshot.url
# Auto-emit trace for legacy path too
if emit_trace and self.last_snapshot is not None:
self._emit_snapshot_trace(self.last_snapshot)
return self.last_snapshot
# Use backend-agnostic snapshot
from .backends.snapshot import snapshot as backend_snapshot
# Merge default options with call-specific kwargs
skip_captcha_handling = bool(kwargs.pop("_skip_captcha_handling", False))
options_dict = self._snapshot_options.model_dump(exclude_none=True)
options_dict.update(kwargs)
options = SnapshotOptions(**options_dict)
self.last_snapshot = await backend_snapshot(self.backend, options=options)
if self.last_snapshot is not None:
self._cached_url = self.last_snapshot.url
if self._step_pre_snapshot is None:
self._step_pre_snapshot = self.last_snapshot
self._step_pre_url = self.last_snapshot.url
if not skip_captcha_handling:
await self._handle_captcha_if_needed(self.last_snapshot, source="gateway")
# Auto-emit snapshot trace event for Studio visualization
if emit_trace and self.last_snapshot is not None:
self._emit_snapshot_trace(self.last_snapshot)
return self.last_snapshot
def _emit_snapshot_trace(self, snapshot: Snapshot) -> None:
"""
Emit a snapshot trace event with screenshot for Studio visualization.
This is called automatically by snapshot() when emit_trace=True.
"""
if self.tracer is None:
return
try:
self.tracer.emit_snapshot(
snapshot=snapshot,
step_id=self.step_id,
step_index=self.step_index,
screenshot_format="jpeg",
)
except Exception:
# Best-effort: don't let trace emission errors break snapshot
pass
async def sampled_snapshot(
self,
*,
samples: int = 4,
scroll_delta_y: float | None = None,
settle_ms: int = 250,
union_limit: int | None = None,
restore_scroll: bool = True,
**kwargs: Any,
) -> Snapshot:
"""
Take multiple snapshots while scrolling and merge them into a "union snapshot".
Intended for analysis/extraction on long / virtualized pages where a single
viewport snapshot is insufficient.
IMPORTANT:
- The returned snapshot's element bboxes may not correspond to the current viewport.
Do NOT use it for clicking unless you also scroll to the right position.
- This method does NOT update `self.last_snapshot` (to avoid confusing verification
loops that depend on the current viewport).
"""
# Legacy browser path: fall back to a single snapshot (we can't rely on backend ops).
if hasattr(self, "_legacy_browser") and hasattr(self, "_legacy_page"):
return await self.snapshot(**kwargs)
from .backends.snapshot import sampled_snapshot as backend_sampled_snapshot
# Merge default options with call-specific kwargs
options_dict = self._snapshot_options.model_dump(exclude_none=True)
options_dict.update(kwargs)
options = SnapshotOptions(**options_dict)
snap = await backend_sampled_snapshot(
self.backend,
options=options,
samples=samples,
scroll_delta_y=scroll_delta_y,
settle_ms=settle_ms,
union_limit=union_limit,
restore_scroll=restore_scroll,
)
return snap
async def evaluate_js(self, request: EvaluateJsRequest) -> EvaluateJsResult:
"""
Evaluate JavaScript expression in the active backend.
Args:
request: EvaluateJsRequest with code and output limits.
Returns:
EvaluateJsResult with normalized text output.
"""
try:
value = await self.backend.eval(request.code)
except Exception as exc: # pragma: no cover - backend-specific errors
return EvaluateJsResult(ok=False, error=str(exc))
text = self._stringify_eval_value(value)
truncated = False
if request.truncate and len(text) > request.max_output_chars:
text = text[: request.max_output_chars] + "..."
truncated = True
return EvaluateJsResult(
ok=True,
value=value,
text=text,
truncated=truncated,
)
async def list_tabs(self) -> TabListResult:
backend = self._get_tab_backend()
if backend is None:
return TabListResult(ok=False, error="unsupported_capability")
try:
tabs = await backend.list_tabs()
except Exception as exc: # pragma: no cover - backend specific
return TabListResult(ok=False, error=str(exc))
return TabListResult(ok=True, tabs=tabs)
async def open_tab(self, url: str) -> TabOperationResult:
backend = self._get_tab_backend()
if backend is None:
return TabOperationResult(ok=False, error="unsupported_capability")
try:
tab = await backend.open_tab(url)
except Exception as exc: # pragma: no cover - backend specific
return TabOperationResult(ok=False, error=str(exc))
return TabOperationResult(ok=True, tab=tab)
async def switch_tab(self, tab_id: str) -> TabOperationResult:
backend = self._get_tab_backend()
if backend is None:
return TabOperationResult(ok=False, error="unsupported_capability")
try:
tab = await backend.switch_tab(tab_id)
except Exception as exc: # pragma: no cover - backend specific
return TabOperationResult(ok=False, error=str(exc))
return TabOperationResult(ok=True, tab=tab)
async def close_tab(self, tab_id: str) -> TabOperationResult:
backend = self._get_tab_backend()
if backend is None:
return TabOperationResult(ok=False, error="unsupported_capability")
try:
tab = await backend.close_tab(tab_id)
except Exception as exc: # pragma: no cover - backend specific
return TabOperationResult(ok=False, error=str(exc))
return TabOperationResult(ok=True, tab=tab)
def _get_tab_backend(self):
backend = getattr(self, "backend", None)
if backend is None:
return None
if not all(
hasattr(backend, attr) for attr in ("list_tabs", "open_tab", "switch_tab", "close_tab")
):
return None
return backend
def capabilities(self) -> BackendCapabilities:
backend = getattr(self, "backend", None)
if backend is None:
return BackendCapabilities()
has_eval = hasattr(backend, "eval")
has_keyboard = hasattr(backend, "type_text") or bool(
getattr(getattr(backend, "_page", None), "keyboard", None)
)
has_downloads = bool(getattr(backend, "downloads", None))
has_permissions = False
try:
context = None
legacy_browser = getattr(self, "_legacy_browser", None)
if legacy_browser is not None:
context = getattr(legacy_browser, "context", None)
if context is None:
page = getattr(backend, "_page", None) or getattr(backend, "page", None)
context = getattr(page, "context", None) if page is not None else None
if context is not None:
has_permissions = bool(
hasattr(context, "clear_permissions") and hasattr(context, "grant_permissions")
)
except Exception:
has_permissions = False
has_files = False
if self.tool_registry is not None:
try:
has_files = self.tool_registry.get("read_file") is not None
except Exception:
has_files = False
return BackendCapabilities(
tabs=self._get_tab_backend() is not None,
evaluate_js=bool(has_eval),
downloads=has_downloads,
filesystem_tools=has_files,
keyboard=bool(has_keyboard or has_eval),
permissions=has_permissions,
)
def can(self, capability: str) -> bool:
caps = self.capabilities()
return bool(getattr(caps, capability, False))
@staticmethod
def _stringify_eval_value(value: Any) -> str:
if value is None:
return "null"
if isinstance(value, (dict, list)):
try:
import json
return json.dumps(value, ensure_ascii=False)
except Exception:
return str(value)
return str(value)
def set_captcha_options(self, options: CaptchaOptions) -> None:
"""
Configure CAPTCHA handling (disabled by default unless set).
"""
self._captcha_options = options
self._captcha_retry_count = 0
def _is_captcha_detected(self, snapshot: Snapshot) -> bool:
if not self._captcha_options:
return False
captcha = getattr(snapshot.diagnostics, "captcha", None) if snapshot.diagnostics else None
if not captcha or not getattr(captcha, "detected", False):
return False
# IMPORTANT: Many sites load CAPTCHA libraries proactively. We only want to
# block execution when there's evidence it's actually *present/active*.
# If we block on low-signal detections (e.g. just a recaptcha script tag),
# interactive runs will “do nothing” and time out.
evidence = getattr(captcha, "evidence", None)
if evidence is not None:
def _list(name: str) -> list[str]:
try:
v = getattr(evidence, name, None)
except Exception:
v = None
if v is None and isinstance(evidence, dict):
v = evidence.get(name)
if not v:
return []
return [str(x) for x in v if x is not None]
iframe_hits = _list("iframe_src_hits")
url_hits = _list("url_hits")
text_hits = _list("text_hits")
selector_hits = _list("selector_hits")
# If we only saw selector/script hints, treat as non-blocking.
if not iframe_hits and not url_hits and not text_hits:
return False
# Heuristic: many sites include a passive reCAPTCHA badge (v3) that should NOT block.
# We only want to block when there's evidence of an interactive challenge.
hits_all = [*iframe_hits, *url_hits, *text_hits, *selector_hits]
hits_l = [str(x).lower() for x in hits_all if x]
strong_text = any(
k in " ".join(hits_l)
for k in (
"i'm not a robot",
"verify you are human",
"human verification",
"complete the security check",
"please verify",
)
)
strong_iframe = any(
any(k in h for k in ("api2/bframe", "hcaptcha", "turnstile"))
for h in hits_l
)
strong_selector = any(
any(
k in h
for k in (
"g-recaptcha-response",
"h-captcha-response",
"cf-turnstile-response",
"recaptcha-checkbox",
"hcaptcha-checkbox",
)
)
for h in hits_l
)
only_generic = (
not strong_text
and not strong_iframe
and not strong_selector
and all(("captcha" in h or "recaptcha" in h) for h in hits_l)
)
if only_generic:
return False
confidence = getattr(captcha, "confidence", 0.0)
return confidence >= self._captcha_options.min_confidence
def _build_captcha_context(self, snapshot: Snapshot, source: str) -> CaptchaContext:
captcha = getattr(snapshot.diagnostics, "captcha", None)
return CaptchaContext(
run_id=self.tracer.run_id,
step_index=self.step_index,
url=snapshot.url,
source=source, # type: ignore[arg-type]
captcha=captcha,
page_control=self._create_captcha_page_control(),
)
def _create_captcha_page_control(self) -> PageControlHook:
async def _eval(code: str) -> Any:
result = await self.evaluate_js(EvaluateJsRequest(code=code))
if not result.ok:
raise RuntimeError(result.error or "evaluate_js failed")
return result.value
return PageControlHook(evaluate_js=_eval)
def _emit_captcha_event(self, reason_code: str, details: dict[str, Any] | None = None) -> None:
payload = {
"kind": "captcha",
"passed": False,
"label": reason_code,
"details": {"reason_code": reason_code, **(details or {})},
}
self.tracer.emit("verification", data=payload, step_id=self.step_id)
async def _handle_captcha_if_needed(self, snapshot: Snapshot, source: str) -> None:
if not self._captcha_options:
return
if not self._is_captcha_detected(snapshot):
return
captcha = getattr(snapshot.diagnostics, "captcha", None)
self._emit_captcha_event(
"captcha_detected",
{"captcha": getattr(captcha, "model_dump", lambda: captcha)()},
)
resolution: CaptchaResolution
if self._captcha_options.policy == "callback":
if not self._captcha_options.handler:
self._emit_captcha_event("captcha_handler_error")
raise CaptchaHandlingError(
"captcha_handler_error",
'Captcha handler is required for policy="callback".',
)
try:
resolution = await self._captcha_options.handler(
self._build_captcha_context(snapshot, source)
)
except Exception as exc: # pragma: no cover - defensive
self._emit_captcha_event("captcha_handler_error", {"error": str(exc)})
raise CaptchaHandlingError(
"captcha_handler_error", "Captcha handler failed."
) from exc
else:
resolution = CaptchaResolution(action="abort")
await self._apply_captcha_resolution(resolution, snapshot, source)
async def _apply_captcha_resolution(
self,
resolution: CaptchaResolution,
snapshot: Snapshot,
source: str,
) -> None:
if resolution.action == "abort":
self._emit_captcha_event("captcha_policy_abort", {"message": resolution.message})
raise CaptchaHandlingError(
"captcha_policy_abort",
resolution.message or "Captcha detected. Aborting per policy.",
)
if resolution.action == "retry_new_session":
self._captcha_retry_count += 1
self._emit_captcha_event("captcha_retry_new_session")
if self._captcha_retry_count > self._captcha_options.max_retries_new_session:
self._emit_captcha_event("captcha_retry_exhausted")
raise CaptchaHandlingError(
"captcha_retry_exhausted",
"Captcha retry_new_session exhausted.",
)
if not self._captcha_options.reset_session:
raise CaptchaHandlingError(
"captcha_retry_new_session",
"reset_session callback is required for retry_new_session.",
)
await self._captcha_options.reset_session()
return
if resolution.action == "wait_until_cleared":
timeout_ms = resolution.timeout_ms or self._captcha_options.timeout_ms
poll_ms = resolution.poll_ms or self._captcha_options.poll_ms
await self._wait_until_cleared(timeout_ms=timeout_ms, poll_ms=poll_ms, source=source)
self._emit_captcha_event("captcha_resumed")
async def _wait_until_cleared(self, *, timeout_ms: int, poll_ms: int, source: str) -> None:
deadline = time.time() + timeout_ms / 1000.0
while time.time() <= deadline:
await asyncio.sleep(poll_ms / 1000.0)
snap = await self.snapshot(_skip_captcha_handling=True)
if not self._is_captcha_detected(snap):
self._emit_captcha_event("captcha_cleared", {"source": source})
return
self._emit_captcha_event("captcha_wait_timeout", {"timeout_ms": timeout_ms})
raise CaptchaHandlingError("captcha_wait_timeout", "Captcha wait_until_cleared timed out.")
async def enable_failure_artifacts(
self,
options: FailureArtifactsOptions | None = None,
) -> None:
"""
Enable failure artifact buffer (Phase 1).
"""
opts = options or FailureArtifactsOptions()
self._artifact_buffer = FailureArtifactBuffer(
run_id=self.tracer.run_id,
options=opts,
)
if opts.fps > 0:
self._artifact_timer_task = asyncio.create_task(self._artifact_timer_loop())
def disable_failure_artifacts(self) -> None:
"""
Disable failure artifact buffer and stop background capture.
"""
if self._artifact_timer_task:
self._artifact_timer_task.cancel()
self._artifact_timer_task = None
async def record_action(
self,
action: str,
*,
url: str | None = None,
) -> None:
"""
Record an action in the artifact timeline and capture a frame if enabled.
"""
self._last_action = action
if not self._artifact_buffer:
return
self._artifact_buffer.record_step(
action=action,
step_id=self.step_id,
step_index=self.step_index,
url=url,
)
if self._artifact_buffer.options.capture_on_action:
await self._capture_artifact_frame()
def _compute_snapshot_digest(self, snap: Snapshot | None) -> str | None:
if snap is None:
return None
try:
return "sha256:" + hashlib.sha256(f"{snap.url}{snap.timestamp}".encode()).hexdigest()
except Exception:
return None
async def emit_step_end(
self,
*,
action: str | None = None,
success: bool | None = None,
error: str | None = None,
outcome: str | None = None,
duration_ms: int | None = None,
attempt: int = 0,
verify_passed: bool | None = None,
verify_signals: dict[str, Any] | None = None,
post_url: str | None = None,
post_snapshot_digest: str | None = None,
llm_data: dict[str, Any] | LLMStepData | None = None,
) -> dict[str, Any]:
"""
Emit a step_end event using TraceEventBuilder.
Args:
action: Action name/type executed in this step
success: Whether the action execution succeeded
error: Error message if action failed
outcome: Outcome description of the action
duration_ms: Duration of action execution in milliseconds
attempt: Attempt number (0-based)
verify_passed: Whether verification passed
verify_signals: Additional verification signals
post_url: URL after action execution
post_snapshot_digest: Digest of post-action snapshot
llm_data: LLM interaction data for this step. Can be:
- LLMStepData: Structured model with response_text, response_hash, usage, model
- dict: Raw dict with response_text, response_hash, usage keys
- None: No LLM data (defaults to empty dict)
"""
goal = self._step_goal or ""
pre_snap = self._step_pre_snapshot or self.last_snapshot
pre_url = (
self._step_pre_url or (pre_snap.url if pre_snap else None) or self._cached_url or ""
)
if post_url is None:
try:
post_url = await self.get_url()
except Exception:
post_url = (
self.last_snapshot.url if self.last_snapshot else None
) or self._cached_url
post_url = post_url or pre_url
pre_digest = self._compute_snapshot_digest(pre_snap)
post_digest = post_snapshot_digest or self._compute_snapshot_digest(self.last_snapshot)
url_changed = bool(pre_url and post_url and str(pre_url) != str(post_url))
assertions_data = self.get_assertions_for_step_end()
assertions = assertions_data.get("assertions") or []
signals = dict(verify_signals or {})
signals.setdefault("url_changed", url_changed)
if error and "error" not in signals:
signals["error"] = error
passed = (
bool(verify_passed) if verify_passed is not None else self.required_assertions_passed()
)
exec_success = (
bool(success)
if success is not None
else bool(
self._last_action_success if self._last_action_success is not None else passed
)
)
exec_data: dict[str, Any] = {
"success": exec_success,
"action": action or self._last_action or "unknown",
"outcome": outcome or self._last_action_outcome or "",
}
if duration_ms is not None:
exec_data["duration_ms"] = int(duration_ms)
if error:
exec_data["error"] = error
verify_data = {
"passed": bool(passed),
"signals": signals,
}
# Convert LLMStepData to dict if needed
llm_data_dict: dict[str, Any]
if llm_data is None:
llm_data_dict = {}
elif isinstance(llm_data, LLMStepData):
llm_data_dict = llm_data.to_trace_dict()
else:
llm_data_dict = llm_data
step_end_data = TraceEventBuilder.build_step_end_event(
step_id=self.step_id or "",
step_index=int(self.step_index),
goal=goal,
attempt=int(attempt),
pre_url=str(pre_url or ""),
post_url=str(post_url or ""),
snapshot_digest=pre_digest,
llm_data=llm_data_dict,
exec_data=exec_data,
verify_data=verify_data,
pre_elements=None,
assertions=assertions,
post_snapshot_digest=post_digest,
)
self.tracer.emit("step_end", step_end_data, step_id=self.step_id)
return step_end_data
async def end_step(self, **kwargs: Any) -> dict[str, Any]:
"""
User-friendly alias for emit_step_end().
This keeps step lifecycle naming symmetric with begin_step().
"""
return await self.emit_step_end(**kwargs)
async def _capture_artifact_frame(self) -> None:
if not self._artifact_buffer:
return
try:
fmt = self._artifact_buffer.options.frame_format
if fmt == "jpeg":
image_bytes = await self.backend.screenshot_jpeg()
else:
image_bytes = await self.backend.screenshot_png()
except Exception:
return
self._artifact_buffer.add_frame(image_bytes, fmt=fmt)
async def _artifact_timer_loop(self) -> None:
if not self._artifact_buffer:
return
interval = 1.0 / max(0.001, self._artifact_buffer.options.fps)
try:
while True:
await self._capture_artifact_frame()
await asyncio.sleep(interval)
except asyncio.CancelledError:
return
def finalize_run(self, *, success: bool) -> None:
"""
Finalize artifact buffer at end of run.
"""
if not self._artifact_buffer:
return
if success:
if self._artifact_buffer.options.persist_mode == "always":
self._artifact_buffer.persist(
reason="success",
status="success",
snapshot=self.last_snapshot,
diagnostics=getattr(self.last_snapshot, "diagnostics", None),
metadata=self._artifact_metadata(),
)
self._artifact_buffer.cleanup()