@@ -190,12 +190,28 @@ type RunState = {
190190 generation: number
191191 stream: Stream
192192 ask: Ask
193+ turn: Turn
193194 latches: Latches
194195 lastCompletionAskTs? : number
195196}
196197
198+ type Turn = {
199+ waitingForFirstChunk: boolean
200+ toolRejected: boolean
201+ toolUsed: boolean
202+ toolFailed: boolean
203+ streamReadComplete: boolean
204+ }
205+
197206type RunEvent =
198207 | { tag: " initialized" }
208+ | { tag: " turnStarted" }
209+ | { tag: " firstChunkAwaitStarted" }
210+ | { tag: " firstChunkAwaitEnded" }
211+ | { tag: " toolRejected" }
212+ | { tag: " toolUsed" }
213+ | { tag: " toolFailed" }
214+ | { tag: " streamReadCompleted" }
199215 | { tag: " streamStarted" }
200216 | { tag: " streamCleanupFinished" }
201217 | { tag: " streamEnded" }
@@ -229,25 +245,32 @@ Queued user input is out of scope for the kernel. The queued-input bugs (#1308,
2292457. Abort drain . The chunk loop sees ` abort ` . If the task is not abandoned , the loop calls ` abortStream ` (` Task.ts:3492 ` ). Only a non - abandoned abort reaches this call , for example a checkpoint - restore delete or a dispose . ` abortStream ` sets ` didFinishAbortingStream ` (` Task.ts:3225 ` ). The ` finally ` block clears ` isStreaming ` (` Task.ts:3756 ` ).
2302468. Stream failure . A stream error without ` abort ` also calls ` abortStream("streaming_failed") ` (` Task.ts:3714 ` ), which sets ` didFinishAbortingStream ` (` Task.ts:3225 ` ). Then the loop retries . It does not abort . The per - request reset block (` Task.ts:3228-3245 ` ) does not reset ` didFinishAbortingStream ` . Only ` resumeAfterDelegation ` resets it (` Task.ts:2884 ` ). So the field stays true across later requests (race R - 1 below ).
2312479. Completion . ` attempt_completion ` asks ` completion_result ` during the turn (` AttemptCompletionTool.ts:221 ` ). The ask is usually still pending after the stream ends . On accept the task ends . On feedback the loop continues .
232- 10. New instance . ` reopenParentFromDelegation ` builds a new instance and calls ` resumeAfterDelegation ` (` ClineProvider.ts:4357, 4404 ` ). Latches are per instance . No path reuses an aborted instance . The resets at ` Task.ts:2881-2886 ` write the default values on the new instance .
248+ 10. New instance . ` reopenParentFromDelegation ` builds a new instance and calls ` resumeAfterDelegation ` (` ClineProvider.ts:4357, 4404 ` ). Latches are per instance . No path reuses an aborted instance . ` resumeAfterDelegation ` no longer resets the latches . It resets only the stream fields ( ` Task.ts:2884-2886 ` ) .
233249
234250#### Transitions
235251
236252Each event has a guard and an effect . If the guard fails , the event returns ` rejected ` with the state unchanged . An effect changes only the fields it names .
237253
238- | Event | Guard | Effect |
239- | ---------------------- - | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------- - |
240- | ` initialized ` | phase is uninitialized | phase = running |
241- | ` streamStarted ` | phase is running , stream is none | generation + 1 , stream = live (generation , cleanupFinished false ), lastCompletionAskTs = empty |
242- | ` streamCleanupFinished ` | stream is live | stream .cleanupFinished = true |
243- | ` streamEnded ` | stream is live | stream = none |
244- | ` askStarted ` | phase is running , abort is false | ask = the new ask . lastCompletionAskTs = its askTs if it is a completion ask , else empty |
245- | ` askSettled ` | none | if ask .askTs equals the event askTs , ask = none . Else no change |
246- | ` completionAccepted ` | phase is running , abort is false , askTs equals lastCompletionAskTs | phase = completed , ask = none |
247- | ` reasonSet ` | none | if reason is empty , reason = the new reason . Else no change |
248- | ` abortRequested ` | none | abort = true . If the event has abandoned true , abandoned = true |
249- | ` abandonRequested ` | abort is true | abandoned = true |
250- | ` disposeRequested ` | none | abort = true , disposed = true |
254+ | Event | Guard | Effect |
255+ | ------------------------ | ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- |
256+ | ` initialized ` | phase is uninitialized | phase = running |
257+ | ` turnStarted ` | none | toolRejected , toolUsed , toolFailed , streamReadComplete = false . waitingForFirstChunk unchanged |
258+ | ` firstChunkAwaitStarted ` | none | turn .waitingForFirstChunk = true |
259+ | ` firstChunkAwaitEnded ` | none | turn .waitingForFirstChunk = false |
260+ | ` toolRejected ` | none | turn .toolRejected = true |
261+ | ` toolUsed ` | none | turn .toolUsed = true |
262+ | ` toolFailed ` | none | turn .toolFailed = true |
263+ | ` streamReadCompleted ` | none | turn .streamReadComplete = true |
264+ | ` streamStarted ` | phase is running , stream is none | generation + 1 , stream = live (generation , cleanupFinished false ), lastCompletionAskTs = empty |
265+ | ` streamCleanupFinished ` | stream is live | stream .cleanupFinished = true |
266+ | ` streamEnded ` | stream is live | stream = none |
267+ | ` askStarted ` | phase is running , abort is false | ask = the new ask . lastCompletionAskTs = its askTs if it is a completion ask , else empty |
268+ | ` askSettled ` | none | if ask .askTs equals the event askTs , ask = none . Else no change |
269+ | ` completionAccepted ` | phase is running , abort is false , askTs equals lastCompletionAskTs | phase = completed , ask = none |
270+ | ` reasonSet ` | none | if reason is empty , reason = the new reason . Else no change |
271+ | ` abortRequested ` | none | abort = true . If the event has abandoned true , abandoned = true |
272+ | ` abandonRequested ` | abort is true | abandoned = true |
273+ | ` disposeRequested ` | none | abort = true , disposed = true |
251274
252275Transition notes :
253276
@@ -324,6 +347,18 @@ The `didFinishAbortingStream` getter is false after the stream ends. The only re
324347
325348The turn sub - machine holds five within - turn fields : ` isWaitingForFirstChunk ` , ` didRejectTool ` , ` didAlreadyUseTool ` , ` didToolFailInCurrentTurn ` , and ` didCompleteReadingStream ` . A turn is one request iteration of the loop . It starts at the per - request reset block (` Task.ts:3228-3245 ` ), before ` streamStarted ` . It ends when the next iteration starts . It is independent of the stream region , because the loop writes these fields after the stream ends (` Task.ts:3768, 3810, 3830, 4029 ` ).
326349
350+ Turn events have no guard . The loop and the tools write these fields in every phase , after abort , and after the stream ends . No other event changes the turn . Event and field names map to the ` Task ` fields as follows .
351+
352+ | Kernel field (event ) | ` Task ` field |
353+ | ------------------------------------------------------ - | -------------------------- |
354+ | ` waitingForFirstChunk ` (` firstChunkAwaitStarted/Ended ` ) | ` isWaitingForFirstChunk ` |
355+ | ` toolRejected ` (` toolRejected ` ) | ` didRejectTool ` |
356+ | ` toolUsed ` (` toolUsed ` ) | ` didAlreadyUseTool ` |
357+ | ` toolFailed ` (` toolFailed ` ) | ` didToolFailInCurrentTurn ` |
358+ | ` streamReadComplete ` (` streamReadCompleted ` ) | ` didCompleteReadingStream ` |
359+
360+ ` turnStarted ` does not reset ` waitingForFirstChunk ` . The legacy reset block does not clear ` isWaitingForFirstChunk ` , and ` attemptApiRequest ` clears it . If the chunk loop breaks on the first chunk , the generator never resumes and the flag stays true . The kernel keeps that behavior , so RSK - 10 and RSK - 16 do not change graceful - abort timing at ` ClineProvider.ts:3641 ` .
361+
327362#### External writers
328363
329364` ClineProvider ` writes ` abortReason ` (` :3572 ` ) and ` abandoned ` (` :3588 ` ) directly . It calls ` abortTask ` at ` :631 ` and ` :1389 ` . ` checkpointRestoreHandler ` calls ` abortTask ` (` :34 ` ). RSK - 06 routes every one of these writes through events before any read flips .
0 commit comments