OpenProgram Docs

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

PhaseCallDoes
1–2prep.prepare_turnSession, history, user message persisted
3turn_context.TurnBindingsPer-turn ContextVars, released in finally
3bTurnWriter.open_placeholderAssistant placeholder row
4loop_runner.run_loop_blockingAgent loop, with reactive compaction retry on overflow
5persistence.persist_assistant_messageAssistant message
6finalize.finalize_turnHead and token bookkeeping, commit, eviction
7_process_turn_onceFinal result event and TurnResult
errorerror_path.handle_turn_errorFolds the error into the placeholder or writes an error node; classifies; returns an error TurnResult
Modules
__init__.pyOrchestrator: execute_turn, execute_continuation, _process_turn_once, public surface
types.pyTurnResult, event callback type. TurnRequest is in openprogram.runtime.agent.turn_request.
prep.pyHistory resolution (override, branch walk, fork), predecessor, memory prefetch, user message
turn_context.pyGraphStore, DAG runtime, turn id, worktree cwd, deferred tools; project auto-commit baseline
stream_tap.pyPersists each completed tool row as results stream by
loop_runner.pyBuilds AgentContext, compacts, runs agent_loop, drains its stream
persistence.pyAssistant message
finalize.pyContext-commit backfill, usage feedback, auto-title, session and project commit
error_path.pyFailed-turn path
turn_writer.pyTurnWriter, the mover of the session head
titles.pyDefault and automatic titles, trigger_compaction
forced_tool.pydispatch_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.pyturn.stop gate: a deny reason launches one more turn
next_prompt.pySuggests the next message for the composer
ModuleHead writes
turn_writer.pyWrites 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.pyWrites its own head on the forced single-tool path
slash_actions.pyRestores 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 blocksIn the text
NothingJoined 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 textA blank line, however many blocks came between. Nothing is added when either side of the boundary already has whitespace there.
Reasoning with only a signatureNothing. It is never drawn, and no stream frame names it.
ReaderGets the text from
Result frame, stored row, final_textloop_runner feeds every response's blocks, a delivered steer and the text a Stop kept into one ReplyText.
A reply resumed after a pauseThe 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 streamingstream_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 deltasThe 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 rowsA row with no content, and a failed row, read reply_text of its blocks.
Next model requestThe 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
HarnessText of a reply with a tool call in the middle
openclawEvery assistant text of the run, joined with a blank line
opencoderun prints each finished text part trimmed, on its own lines
pi-monoPrint mode writes the last message's text blocks, one newline after each
codex-cliexec 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__.py calls 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.py imports 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 turn when empty. Success, error and shadow-file commits share this rule. A failed session commit stays unacknowledged.

Activity and control

Last updated · 2026-11-25