Session Actor: Thread, State & Cancellation Boundaries
Each Session runs a current-thread Tokio runtime and LocalSet on a dedicated OS thread. SessionActor coordinates turns, ChatStateActor serially owns the conversation state, and CancellationToken handles cooperative termination.
Be able to explain how thread isolation, Actor state ownership, and cancellation signals work together, and accurately describe the Agent's "effectively immutable" boundary.
SessionActor Coordination
run_sessionsimultaneously receives SessionCommand, ChatStateEvent, SessionEvent, and turn completion.maybe_start_running_taskstarts a pending turn.- After a turn completes, it handles completion, turn-end, and follow-up notification processing.
ChatStateActor Owns State
- Exclusively owns conversation, tokens, configuration, and persistence.
- Processes commands serially via
mpsc::UnboundedReceiver. - A cancelled token triggers exit; dropping all handles also ends the loop.
definitionAgentDefinition — defines identity, mode, and strategy inputs.
prompt_contextPromptContext supporting inspection, re-rendering, and serialization.
system_promptRendered and cached string from the prompt context.
tool_bridgeArc<ToolBridge> — bridge for tool registration and session context.
reminder_policySession-level reminder policy.
compaction_policyAuto-compaction, memory flush, and two-pass configuration.
hosted_toolsBackend-hosted tool definitions sent to the API.
backend_search_enabledServer-side search toggle at build time.
finalize_prompt(&mut self) to update the build timestamp and re-render the prompt, so it cannot be described as absolutely immutable..name(thread_name)
.stack_size(8 * 1024 * 1024)
.spawn(move || {
let rt = tokio::runtime::Builder
::new_current_thread().enable_all().build()?;
let local = tokio::task::LocalSet::new();
});
pub async fn finalize_prompt(&mut self) {
self.prompt_context.build_timestamp_utc =
chrono::Utc::now().to_rfc3339();
self.system_prompt = self.prompt_context
.render(&self.tool_bridge).await
.unwrap_or_default();
}
.git metadata, so no specific commit version is claimed.Assign a Sole Owner to Each State
Place conversation, system_prompt, tool registry, and sampling request into ChatStateActor, Agent, ToolBridge, and SamplerActor respectively. Then explain why cancellation token and message priority are different concepts, and that this codebase has no general "high-priority message at queue head" design.