11import { Page } from 'playwright' ;
22
3- import { AgentRuntime , AttachOptions } from './agent-runtime' ;
3+ import { AgentRuntime , AssertionHandle , AttachOptions } from './agent-runtime' ;
44import { Predicate } from './verification' ;
55import { Tracer } from './tracing/tracer' ;
66
7+ class DebuggerAssertionHandle extends AssertionHandle {
8+ private dbg : SentienceDebugger ;
9+ private autoClose : boolean ;
10+ private openedStepId : string | null ;
11+
12+ constructor (
13+ dbg : SentienceDebugger ,
14+ predicate : Predicate ,
15+ label : string ,
16+ required : boolean ,
17+ autoClose : boolean ,
18+ openedStepId : string | null
19+ ) {
20+ super ( dbg . runtime , predicate , label , required ) ;
21+ this . dbg = dbg ;
22+ this . autoClose = autoClose ;
23+ this . openedStepId = openedStepId ;
24+ }
25+
26+ override once ( ) : boolean {
27+ const ok = super . once ( ) ;
28+ if ( this . autoClose && this . dbg . isAutoStepOpenFor ( this . openedStepId ) ) {
29+ void this . dbg . endStep ( ) ;
30+ }
31+ return ok ;
32+ }
33+
34+ override async eventually (
35+ options : Parameters < AssertionHandle [ 'eventually' ] > [ 0 ] = { }
36+ ) : Promise < boolean > {
37+ const ok = await super . eventually ( options ) ;
38+ if ( this . autoClose && this . dbg . isAutoStepOpenFor ( this . openedStepId ) ) {
39+ await this . dbg . endStep ( ) ;
40+ }
41+ return ok ;
42+ }
43+ }
44+
745export class SentienceDebugger {
846 readonly runtime : AgentRuntime ;
947 private stepOpen : boolean = false ;
1048 private autoStep : boolean = true ;
49+ private autoOpenedStep : boolean = false ;
50+ private autoOpenedStepId : string | null = null ;
1151
1252 constructor ( runtime : AgentRuntime , options ?: { autoStep ?: boolean } ) {
1353 this . runtime = runtime ;
@@ -19,13 +59,31 @@ export class SentienceDebugger {
1959 return new SentienceDebugger ( runtime ) ;
2060 }
2161
62+ isAutoStepOpenFor ( stepId : string | null ) : boolean {
63+ return Boolean (
64+ this . stepOpen &&
65+ this . autoOpenedStep &&
66+ this . autoOpenedStepId &&
67+ stepId &&
68+ this . autoOpenedStepId === stepId
69+ ) ;
70+ }
71+
2272 beginStep ( goal : string , stepIndex ?: number ) : string {
73+ // If we previously auto-opened a verification step, close it before starting a real step.
74+ if ( this . stepOpen && this . autoOpenedStep ) {
75+ void this . endStep ( ) ;
76+ this . autoOpenedStep = false ;
77+ this . autoOpenedStepId = null ;
78+ }
2379 this . stepOpen = true ;
2480 return this . runtime . beginStep ( goal , stepIndex ) ;
2581 }
2682
2783 async endStep ( opts : Parameters < AgentRuntime [ 'emitStepEnd' ] > [ 0 ] = { } ) : Promise < any > {
2884 this . stepOpen = false ;
85+ this . autoOpenedStep = false ;
86+ this . autoOpenedStepId = null ;
2987 // emitStepEnd is synchronous; wrap to satisfy async/await lint rules.
3088 return await Promise . resolve ( this . runtime . emitStepEnd ( opts ) ) ;
3189 }
@@ -43,7 +101,13 @@ export class SentienceDebugger {
43101 return this . runtime . snapshot ( options ) ;
44102 }
45103
104+ async recordAction ( action : string , url ?: string ) : Promise < void > {
105+ return await this . runtime . recordAction ( action , url ) ;
106+ }
107+
46108 check ( predicate : Predicate , label : string , required : boolean = false ) {
109+ let didAutoOpen = false ;
110+ let openedStepId : string | null = null ;
47111 if ( ! this . stepOpen ) {
48112 if ( ! this . autoStep ) {
49113 throw new Error (
@@ -53,7 +117,17 @@ export class SentienceDebugger {
53117 ) ;
54118 }
55119 this . beginStep ( `verify:${ label } ` ) ;
120+ didAutoOpen = true ;
121+ openedStepId = this . runtime . stepId ;
122+ this . autoOpenedStep = true ;
123+ this . autoOpenedStepId = openedStepId ;
124+ }
125+ const base = this . runtime . check ( predicate , label , required ) ;
126+ if ( ! didAutoOpen ) {
127+ return base ;
56128 }
57- return this . runtime . check ( predicate , label , required ) ;
129+ // Return an auto-closing handle for the common "casual" sidecar usage pattern.
130+ // We still call runtime.check(...) above to keep behavior consistent.
131+ return new DebuggerAssertionHandle ( this , predicate , label , required , true , openedStepId ) ;
58132 }
59133}
0 commit comments