@@ -65,6 +65,13 @@ const completedStreaming = deepFreeze(
6565)
6666const allEvents : RunEvent [ ] = [
6767 { tag : "initialized" } ,
68+ { tag : "turnStarted" } ,
69+ { tag : "firstChunkAwaitStarted" } ,
70+ { tag : "firstChunkAwaitEnded" } ,
71+ { tag : "toolRejected" } ,
72+ { tag : "toolUsed" } ,
73+ { tag : "toolFailed" } ,
74+ { tag : "streamReadCompleted" } ,
6875 { tag : "streamStarted" } ,
6976 { tag : "streamCleanupFinished" } ,
7077 { tag : "streamEnded" } ,
@@ -78,6 +85,112 @@ const allEvents: RunEvent[] = [
7885]
7986
8087describe ( "nextRunState" , ( ) => {
88+ describe ( "turn" , ( ) => {
89+ const allSet = {
90+ waitingForFirstChunk : true ,
91+ toolRejected : true ,
92+ toolUsed : true ,
93+ toolFailed : true ,
94+ streamReadComplete : true ,
95+ }
96+ const dirty = deepFreeze ( { ...running , turn : allSet } )
97+ const clean = deepFreeze ( { ...running , turn : { ...running . turn } } )
98+ const turnEvents = [
99+ [ "firstChunkAwaitStarted" , "waitingForFirstChunk" ] ,
100+ [ "toolRejected" , "toolRejected" ] ,
101+ [ "toolUsed" , "toolUsed" ] ,
102+ [ "toolFailed" , "toolFailed" ] ,
103+ [ "streamReadCompleted" , "streamReadComplete" ] ,
104+ ] as const
105+
106+ it . each ( turnEvents ) ( "%s sets only %s" , ( tag , field ) => {
107+ const next = accept ( clean , { tag } )
108+ expect ( next . turn ) . toEqual ( { ...clean . turn , [ field ] : true } )
109+ expect ( { ...next , turn : clean . turn } ) . toEqual ( clean )
110+ } )
111+
112+ it . each ( turnEvents ) ( "%s keeps the other four fields when they are set" , ( tag , field ) => {
113+ const base = deepFreeze ( { ...running , turn : { ...allSet , [ field ] : false } } )
114+ expect ( accept ( base , { tag } ) . turn ) . toEqual ( allSet )
115+ } )
116+
117+ it ( "firstChunkAwaitEnded clears only waitingForFirstChunk" , ( ) => {
118+ const state = run ( [ { tag : "firstChunkAwaitStarted" } , { tag : "toolUsed" } ] , running )
119+ expect ( accept ( state , { tag : "firstChunkAwaitEnded" } ) . turn ) . toEqual ( {
120+ ...state . turn ,
121+ waitingForFirstChunk : false ,
122+ } )
123+ } )
124+
125+ it ( "firstChunkAwaitEnded keeps the other four fields when they are set" , ( ) => {
126+ expect ( accept ( dirty , { tag : "firstChunkAwaitEnded" } ) . turn ) . toEqual ( {
127+ ...allSet ,
128+ waitingForFirstChunk : false ,
129+ } )
130+ } )
131+
132+ it . each ( [ "streamStarted" , "streamCleanupFinished" , "streamEnded" ] as const ) (
133+ "%s leaves the turn unchanged" ,
134+ ( tag ) => {
135+ const live = deepFreeze ( { ...( tag === "streamStarted" ? running : streaming ) , turn : allSet } )
136+ expect ( accept ( live , { tag } ) . turn ) . toEqual ( allSet )
137+ } ,
138+ )
139+
140+ it ( "initialized leaves the turn unchanged" , ( ) => {
141+ expect ( accept ( { ...initialRunState , turn : allSet } , { tag : "initialized" } ) . turn ) . toEqual ( allSet )
142+ } )
143+
144+ it ( "turnStarted resets the four per-request fields and keeps waitingForFirstChunk" , ( ) => {
145+ expect ( accept ( dirty , { tag : "turnStarted" } ) . turn ) . toEqual ( {
146+ ...running . turn ,
147+ waitingForFirstChunk : true ,
148+ } )
149+ } )
150+
151+ it . each ( [ initialRunState , completed , aborted ] ) ( "accepts every turn event when the phase is $phase" , ( state ) => {
152+ const turnEvents : RunEvent [ ] = [
153+ { tag : "turnStarted" } ,
154+ { tag : "firstChunkAwaitStarted" } ,
155+ { tag : "firstChunkAwaitEnded" } ,
156+ { tag : "toolRejected" } ,
157+ { tag : "toolUsed" } ,
158+ { tag : "toolFailed" } ,
159+ { tag : "streamReadCompleted" } ,
160+ ]
161+ for ( const event of turnEvents ) {
162+ expect ( nextRunState ( state , event ) ) . toHaveProperty ( "ok" )
163+ }
164+ } )
165+
166+ it ( "accepts tool writes after the stream ends" , ( ) => {
167+ const ended = run ( [ { tag : "streamEnded" } , { tag : "toolUsed" } , { tag : "toolFailed" } ] , streaming )
168+ expect ( ended . stream ) . toEqual ( { tag : "none" } )
169+ expect ( ended . turn ) . toMatchObject ( { toolUsed : true , toolFailed : true } )
170+ } )
171+
172+ it ( "clears the first-chunk wait when abort lands before the first chunk" , ( ) => {
173+ const state = run (
174+ [
175+ { tag : "turnStarted" } ,
176+ { tag : "streamStarted" } ,
177+ { tag : "firstChunkAwaitStarted" } ,
178+ { tag : "abortRequested" , abandoned : false } ,
179+ { tag : "firstChunkAwaitEnded" } ,
180+ ] ,
181+ running ,
182+ )
183+ expect ( state . latches . abort ) . toBe ( true )
184+ expect ( state . turn . waitingForFirstChunk ) . toBe ( false )
185+ expect ( state . turn . streamReadComplete ) . toBe ( false )
186+ } )
187+
188+ it ( "a turn started after an abort still resets the fields" , ( ) => {
189+ const state = run ( [ { tag : "toolFailed" } , { tag : "abortRequested" , abandoned : false } ] , running )
190+ expect ( accept ( state , { tag : "turnStarted" } ) . turn ) . toEqual ( running . turn )
191+ } )
192+ } )
193+
81194 describe ( "initialized" , ( ) => {
82195 it ( "moves uninitialized to running" , ( ) => {
83196 acceptOnly ( initialRunState , { tag : "initialized" } , { phase : "running" } )
@@ -414,9 +527,17 @@ describe("nextRunState", () => {
414527 generation : 0 ,
415528 stream : { tag : "none" } ,
416529 ask : { tag : "none" } ,
530+ turn : {
531+ waitingForFirstChunk : false ,
532+ toolRejected : false ,
533+ toolUsed : false ,
534+ toolFailed : false ,
535+ streamReadComplete : false ,
536+ } ,
417537 latches : { abort : false , abandoned : false , disposed : false } ,
418538 } )
419539 expect ( Object . isFrozen ( initialRunState ) ) . toBe ( true )
540+ expect ( Object . isFrozen ( initialRunState . turn ) ) . toBe ( true )
420541 expect ( Object . isFrozen ( initialRunState . stream ) ) . toBe ( true )
421542 expect ( Object . isFrozen ( initialRunState . ask ) ) . toBe ( true )
422543 expect ( Object . isFrozen ( initialRunState . latches ) ) . toBe ( true )
0 commit comments