Chat turn runtime
openprogram/runtime/conversation/turn_runtime/ is the execution path of a chat turn. Agent.run_turn and Agent.resume_turn own the public contract; process_user_turn and process_agent_continuation are entries that route through ChatAgent.
Phases
| Phase | Call | Does |
|---|---|---|
| 1–2 | prep.prepare_turn | Session, history, user message persisted |
| 3 | turn_context.TurnBindings | Per-turn ContextVars, released in finally |
| 3b | TurnWriter.open_placeholder | Assistant placeholder row |
| 4 | loop_runner.run_loop_blocking | Agent loop, with reactive compaction retry on overflow |
| 5 | persistence.persist_assistant_message | Assistant message |
| 6 | finalize.finalize_turn | Head and token bookkeeping, commit, eviction |
| 7 | _process_turn_once | Final result event and TurnResult |
| error | error_path.handle_turn_error | Folds the error into the placeholder or writes an error node; classifies; returns an error TurnResult |
Modules
__init__.py | Orchestrator: execute_turn, execute_continuation, _process_turn_once, public surface |
|---|---|
types.py | TurnResult, event callback type. TurnRequest is in openprogram.runtime.agent.turn_request. |
prep.py | History resolution (override, branch walk, fork), predecessor, memory prefetch, user message |
turn_context.py | GraphStore, DAG runtime, turn id, worktree cwd, deferred tools; project auto-commit baseline |
stream_tap.py | Persists each completed tool row as results stream by |
loop_runner.py | Builds AgentContext, compacts, runs agent_loop, drains its stream |
persistence.py | Assistant message |
finalize.py | Context-commit backfill, usage feedback, auto-title, session and project commit |
error_path.py | Failed-turn path |
turn_writer.py | TurnWriter, the mover of the session head |
titles.py | Default and automatic titles, trigger_compaction |
forced_tool.py | dispatch_forced_tool_call |
runtime_attach.py | _wrap_agentic_runtime_block: an Agent method call as a runtime-block turn |
slash_actions.py | /spawn and /merge |
stop_hook.py | turn.stop gate: a deny reason launches one more turn |
next_prompt.py | Suggests the next message for the composer |
Head invariant
| Module | Head writes |
|---|---|
turn_writer.py | Writes the head on user persist and failure, and supplies head_for_finalize, which finalize.py stamps. Applies advance_head, so a same-session spawned turn cannot move the head. |
forced_tool.py | Writes its own head on the forced single-tool path |
slash_actions.py | Restores the previous head after a /spawn or /merge |
error_path.py picks the head after a failure and delegates the write to TurnWriter.record_failure. No other module originates a head_id.
A reply's text
A reply is a list of blocks: text, reasoning, tool calls, steering messages. The chat draws the blocks. The stored content, TurnResult.final_text, --print, a channel message, a search hit, a title and a copied message read one string, built by reply_text in state/reply_text.py and nowhere else.
| Between two text blocks | In the text |
|---|---|
| Nothing | Joined byte for byte. A provider cuts one stream of text into blocks for citations, and a response continued after its output limit goes on mid-sentence. |
| A tool call, a steering message, or reasoning with text | A blank line, however many blocks came between. Nothing is added when either side of the boundary already has whitespace there. |
| Reasoning with only a signature | Nothing. It is never drawn, and no stream frame names it. |
| Reader | Gets the text from |
|---|---|
Result frame, stored row, final_text | loop_runner feeds every response's blocks, a delivered steer and the text a Stop kept into one ReplyText. |
| A reply resumed after a pause | The same row goes on. loop_runner seeds the ReplyText with the whole trace shown before the pause, so the text is every passage of the reply, and the first passage after the pause opens with one blank line. |
| Row while streaming | stream_tap reads its blocks. The separator is part of the appended output_tail, so the log only adds to the text and a cold fold equals the live row. |
| Clients that add up deltas | The text frame that opens a passage carries separator; stream_text puts it before the delta. The page, channels, the OpenAI-compatible stream, --print partial messages and ACP use it. |
| Transcript rows | A row with no content, and a failed row, read reply_text of its blocks. |
| Next model request | The blocks, one response at a time (context/session_dag/render.py). The separator is in no block, so the provider is sent what it wrote. |
Why a blank line
| Harness | Text of a reply with a tool call in the middle |
|---|---|
| openclaw | Every assistant text of the run, joined with a blank line |
| opencode | run prints each finished text part trimmed, on its own lines |
| pi-mono | Print mode writes the last message's text blocks, one newline after each |
| codex-cli | exec keeps only the last agent message |
OpenProgram follows openclaw: a blank line is a paragraph break in Markdown and in a chat app, and a single newline is not. It keeps every passage, since the sentence before a tool call is often the only place a reply says what it did.
Consecutive text blocks stay as they are. None of the four needs this rule, because none stores one text stream as several blocks.
Test seam
Tests patch _resolve_model, _load_agent_profile, _run_loop_blocking and process_user_turn on the package object.
__init__.pycalls these as module globals, so a patch is seen at call time.- A sibling module never freezes one with a module-level import.
loop_runner.pyimports the package inside the function and calls_dispatcher._load_agent_profile(...). - A module outside the seam, such as
finalize.py, takes the resolved profile and model as arguments.
Public surface
_wrap_agentic_runtime_block, dispatch_forced_tool_call, execute_continuation, execute_turn, process_agent_continuation, process_user_turn, trigger_compaction
Conventions
- Heavy provider and context imports stay inside function bodies.
- The split leaves the turn lifecycle, error taxonomy, persistence schema and event payloads as they are, and adds no async to the blocking path.
- A completed turn's commit summary is the first nonempty trimmed prompt line, up to 60 characters, or
turnwhen empty. Success, error and shadow-file commits share this rule. A failed session commit stays unacknowledged.