ChatObject — The Lifecycle Manager
Core Positioning
ChatObject is the core of AmritaCore — the basic unit of a dialogue. It is a lifecycle manager: it owns the workflow graph, the interpreter, the bidirectional stream, and every piece of runtime state (DI contexts) for one conversation.
Lifecycle
begin()schedules_entry()as a task, but only the first time — the guard ishasattr(self, "_task")._entry()itself re-checks_is_running/_is_doneand raisesRuntimeErrorif the object is already running or done.- On exit
_is_doneis set,set_queue_done()writes an EOF to the response channel,end_atis stamped, the object is dropped fromChatManager.running_chat_object_id2map, andChatManager.clean_obj()enforces a hard cap on retained objects per session. - Middleware (
middleware=...) can wrap the whole workflow; when set, the interpreter is bypassed and the middleware is awaited instead.
Workflow Selection
ChatObject runs a pre-compiled workflow. The default (used when workflow=None) is _workflow_rendered, the full shell:
LOAD_STATE -> NORMALIZE_MESSAGES -> (COMPACT) -> JINJA2_RENDER -> BUILD_MESSAGE -> _pre_runner -> _prepare_strategy -> agent branch -> LLM_COMPLETION -> _post_runner -> COMMIT_MEMORY
Its agent branch is the legacy single-call loop (AGENT_BLOCK: one single_execute per iteration, no DAG decomposition). Strategies that are not agent-category take the RUN_INLINE_STRATEGY branch instead. Both _pre_runner (which fires the PRECOMPLE breakpoint and the pre-completion matchers) and _post_runner are part of this default shell.
For the built-in step-driven ReAct loop (decompose -> Step -> summarize, with update_step plan revision), pass _step_workflow_rendered — the same shell with STEP_AGENT_BLOCK in place of AGENT_BLOCK:
from amrita_core.chatmanager import _step_workflow_rendered
from amrita_core.builtins.workflows import SIMPLE_STEP_REACT, SIMPLE_CHAT
# Default: full shell + legacy single-call agent loop
chat = ChatObject(train=..., user_input=..., session_id="s1")
# Native step loop: same shell, STEP_AGENT_BLOCK
chat = ChatObject(..., workflow=_step_workflow_rendered)
# Pre-composed pipelines from amrita_core.builtins.workflows
chat = ChatObject(..., workflow=SIMPLE_CHAT) # no agent branch: one LLM call
chat = ChatObject(..., workflow=SIMPLE_STEP_REACT) # step loop, component nodes onlyThe
amrita_core.builtins.workflowspipelines are assembled from component nodes only — they have no_pre_runner/_prepare_strategy/_post_runner. WithSIMPLE_CHATthe pre-completion matchers never fire, so thePRECOMPLEbreakpoint is unreachable.
workflowandarchived_nodesare mutually exclusive — passing both raisesValueError. The step-loop workflow is what enables thestepmetadata events (decompose/intro/leave) and theupdate_steptool — see The Step Loop.
Why "Lifecycle Manager" Matters
Strategies and hooks never own the lifecycle — they receive resources via DI fields (see Agent Strategy). ChatObject is the single place that wires everything together: that is why it is the unit of a dialogue rather than a thin wrapper.
Next
Configuration — how the runtime is configured.
