Agent call flow
- User message → dispatcherDirectly to the loop
- Agent method body → runtime.execThrough AgentSession and LoopDriver
- agent_loopA tool body can call runtime.exec again: nesting
Entry points
| A · dispatcher | B · runtime.exec | agent_loop | |
|---|---|---|---|
| Job | Turn lifecycle | One LLM call | Call model, run tools, feed back, until plain text |
| Order | Create or load session · write user node · attach Runtime · resolve model (profile + override) · parse tools (channel, plan, approval) · loop · persist, title, auto-compact | Open llm node (timeout and retry outside) · select nodes with render_context · render messages and current turn · resolve toolset · assemble system and skills · create AgentSession · loop · close llm node | Before each round: convert_to_llm, memory prefetch, deferred-tool re-split |
| Recording | insert_placeholder, then persist_assistant_message: one ROLE_LLM node | _open_model_call_node, _close_model_call_node: one ROLE_LLM node | Tool code nodes' caller = the current llm node |
| Code | runtime/conversation/turn_runtime/ | runtime/agent/runtime/ | runtime/agent/loop/ |
One exec is one llm node: running on entry, reply and completed on exit. Tool rounds inside it are not extra nodes, so every two nested code nodes have an llm node between them. exec sits below the loop, which is what lets a function body call a model; that is also the source of self-recursion.
_call_id switches to the llm node once the prompt is built (test_tool_loop_subcall_attributes_to_llm_node). Sync and async paths match. agent_loop knows nothing about the DAG. tool.before fires only in _execute_tool_calls, so no path loses or doubles events.
Why two entries
Their surroundings do not overlap: session management, WebSocket broadcast, title and compaction against the llm node and AgentSession retry. Sending the dispatcher through exec fails for four reasons:
| Conflict | Detail |
|---|---|
| Model | The dispatcher's _resolve_model(agent_profile, req.model_override) differs from the attached runtime's auto-detected api_model |
| Context | The context engine's prep.agent_messages clashes with exec rendering from the DAG |
| Streaming | exec's flat on_stream dict against the dispatcher's WebUI envelope |
| Duplicate node | The dispatcher's reply already is the record; another llm node gives [user, assistant, assistant] (test_dispatcher_integration.py::test_real_loop_text_only) |
The loop driver LoopDriver sits below exec (exec → AgentSession → LoopDriver → agent_loop) and is built in one place; it is not a parallel path.
Callable path
Runtime(call=fn) wraps fn into a stand-in provider model (callable_model.py) and goes through _call_via_providers like any model: no legacy branch. The adapter converts messages back to content, calls fn with response_format, returns one assistant message, and has no tools or tool loop. stream_fn threads from exec by context variable to AgentSession (test_exec_stream_fn_injection).
Compared with other frameworks
| OpenCode | OpenClaw | Hermes | OpenProgram | |
|---|---|---|---|---|
| One LLM call | Step, tool loop inside | runId pairing | No hierarchy | One llm node, tool loop inside |
| Recording | Step.Started → Step.Ended | llm_input → llm_output hooks | Append-only | Write running, fill on close |
| Tool attribution | ToolPart in the Step | Nested under the runId | Peer message | Code node caller = llm node |
| Chat vs programmatic | One path | One hook system | One run_conversation | Two entries, one loop |
Implementation status
Done: paired llm-node write, callable path, stream_fn, usage, blocks and status on close. The record is written by two pairs of the same shape; one primitive is designed, not adopted by the dispatcher.
| Must survive | Needed by | Risk |
|---|---|---|
extra.blocks (order of thinking, text, tool cards) | Reply bubble, transcript/rows.py | Highest |
Token columns, token_model | Metering UI | High |
predecessor = user message | Branch, fork, rewind | High |
cancelled / completed | Partial output on stop | Medium |
- Give
_open_model_call_nodean optional role and predecessor;execunchanged. - Change
insert_placeholderto call it. - Change
persist_assistant_messageto close through it, keeping only field assembly. - Delete the duplicate writes in
turn_lifecycle.py(fold_error_into_placeholder). - Verify chat end to end: bubbles, cards, tokens, stop, fork.
The front end is unaffected: _node_to_msg (state/store/session/_msg_adapter.py) still returns assistant; storage has only user, llm, code. Keep the prompt-cache prefix stable, and _run_loop_blocking as the test seam.