1The loop
Look for: run_turn _Turn _start _ask _run_tools _check Runtime
The file opens with the whole turn as pseudocode, and run_turn
follows it step by step, numbered 1 to 10 in the comments. The diagram uses the
same numbers. Each step with any detail is a helper one level down; no helper
calls another, and nothing recurses.
flowchart TD
start(["1 · record the prompt"]) --> safe["3 · safe point:
pause, /steer"]
safe --> caps{"4 · over a
cost cap?"}
caps -->|yes| budget(["budget_exceeded"])
caps -->|no| ask["5 · ask the model
_ask"]
ask --> calls{"6 · tool
calls?"}
calls -->|yes| run["run them in order
_run_tools"] --> safe
calls -->|no| steer{"7 · steer
pending?"}
steer -->|yes| safe
steer -->|no| acted{"8 · changed
something?"}
acted -->|"no, or no check"| done(["9 · completed"])
acted -->|yes| check["run the check
_check"]
check -->|passed| done
check -->|"failed, tries left"| safe
check -->|"failed, none left"| failed(["verification_failed"])
A turn ends one of four ways:
budget_exceeded— the turn hit a cost cap.completed— the model stopped talking and any check passed.verification_failed— the check failed and no tries were left.- A cancel, which raises. First
seal()gives every open tool call a result, so the transcript stays valid even mid-call.
Take with you: the safe point (step 3) is the only
place a pause or a /steer takes effect. The previous round always
ended on a complete call-and-result pair, which is what keeps the transcript valid
(stop 16).