a3s_code_core/agent_api.rs
1//! Agent Facade API
2//!
3//! High-level, ergonomic API for using A3S Code as an embedded library.
4//!
5//! ## Example
6//!
7//! ```rust,no_run
8//! use a3s_code_core::Agent;
9//!
10//! # async fn run() -> anyhow::Result<()> {
11//! let agent = Agent::new("agent.acl").await?;
12//! let session = agent.session("/my-project", None)?;
13//! let result = session.send("Explain the auth module", None).await?;
14//! println!("{}", result.text);
15//! # Ok(())
16//! # }
17//! ```
18
19use crate::agent::{AgentConfig, AgentEvent, AgentResult};
20use crate::commands::CommandRegistry;
21use crate::config::CodeConfig;
22use crate::error::Result;
23use crate::hitl::PendingConfirmationInfo;
24use crate::llm::{LlmClient, Message};
25use crate::prompts::{PlanningMode, SystemPromptSlots};
26use crate::queue::{
27 ExternalTask, ExternalTaskResult, LaneHandlerConfig, SessionLane, SessionQueueConfig,
28 SessionQueueStats,
29};
30use crate::tools::{ToolContext, ToolExecutor};
31use a3s_lane::{DeadLetter, MetricsSnapshot};
32use a3s_memory::MemoryStore;
33use std::collections::HashMap;
34use std::path::{Path, PathBuf};
35use std::sync::{Arc, RwLock};
36use tokio::sync::mpsc;
37use tokio::task::JoinHandle;
38mod agent_binding;
39mod agent_bootstrap;
40mod agent_loop_runtime;
41mod agent_sessions;
42mod capabilities;
43mod command_runtime;
44mod conversation_runtime;
45mod direct_tools;
46mod hook_control;
47mod run_lifecycle;
48mod runtime;
49mod runtime_events;
50mod session_builder;
51mod session_clock;
52mod session_close;
53mod session_commands;
54mod session_config;
55mod session_extensions;
56mod session_hitl;
57mod session_options;
58mod session_persistence;
59mod session_queue;
60mod session_runs;
61mod session_runtime;
62mod session_save;
63mod session_verification;
64mod session_view;
65use direct_tools::DirectToolRuntime;
66use hook_control::HookControl;
67use runtime_events::ActiveToolState;
68use session_close::SessionCloseHandle;
69use session_extensions::SessionExtensionRuntime;
70use session_hitl::HitlControl;
71use session_queue::QueueControl;
72use session_runs::RunControl;
73use session_verification::VerificationRuntime;
74use session_view::SessionView;
75
76/// Canonicalize a path, stripping the Windows `\\?\` UNC prefix to avoid
77/// polluting workspace strings throughout the system (prompts, session data, etc.).
78fn safe_canonicalize(path: &Path) -> PathBuf {
79 match std::fs::canonicalize(path) {
80 Ok(p) => strip_unc_prefix(p),
81 Err(_) => path.to_path_buf(),
82 }
83}
84
85/// Strip the Windows extended-length path prefix (`\\?\`) that `canonicalize()` adds.
86/// On non-Windows this is a no-op.
87fn strip_unc_prefix(path: PathBuf) -> PathBuf {
88 #[cfg(windows)]
89 {
90 let s = path.to_string_lossy();
91 if let Some(stripped) = s.strip_prefix(r"\\?\") {
92 return PathBuf::from(stripped);
93 }
94 }
95 path
96}
97
98// ============================================================================
99// ToolCallResult
100// ============================================================================
101
102/// Result of a direct tool execution (no LLM).
103#[derive(Debug, Clone)]
104pub struct ToolCallResult {
105 pub name: String,
106 pub output: String,
107 pub exit_code: i32,
108 pub metadata: Option<serde_json::Value>,
109 /// Structured discriminant for tool failures. `None` when the tool
110 /// either succeeded or failed without a typed reason (the message in
111 /// `output` is then the only diagnostic). Populated for known
112 /// kinds such as `VersionConflict` so SDK callers can branch on the
113 /// `type` field instead of regex-matching `output`.
114 pub error_kind: Option<crate::tools::ToolErrorKind>,
115}
116
117// ============================================================================
118// SessionOptions
119// ============================================================================
120
121/// Optional per-session overrides.
122#[derive(Clone, Default)]
123pub struct SessionOptions {
124 /// Override the default model. Format: `"provider/model"` (e.g., `"openai/gpt-4o"`).
125 pub model: Option<String>,
126 /// Extra directories to scan for agent files.
127 /// Merged with any global `agent_dirs` from [`CodeConfig`].
128 pub agent_dirs: Vec<PathBuf>,
129 /// Reproducible disposable workers registered for task delegation.
130 /// Explicit session workers override agents loaded from directories by name.
131 pub worker_agents: Vec<crate::subagent::WorkerAgentSpec>,
132 /// Optional queue configuration for lane-based tool execution.
133 ///
134 /// When set, enables priority-based tool scheduling with parallel execution
135 /// of read-only (Query-lane) tools, DLQ, metrics, and external task handling.
136 pub queue_config: Option<SessionQueueConfig>,
137 /// Optional security provider for taint tracking and output sanitization
138 pub security_provider: Option<Arc<dyn crate::security::SecurityProvider>>,
139 /// Optional host-supplied LLM client.
140 ///
141 /// When set, it is used directly, overriding the `provider/model`
142 /// factory resolution — the one Action-layer backend that was previously
143 /// only injectable in test code. Lets a host plug in a provider the
144 /// built-in factory does not cover, a deterministic record/replay client,
145 /// or an HTTP-layer proxy/audit wrapper. Mirrors `workspace_services`.
146 pub llm_client: Option<Arc<dyn crate::llm::LlmClient>>,
147 /// Optional context providers for RAG
148 pub context_providers: Vec<Arc<dyn crate::context::ContextProvider>>,
149 /// Optional confirmation manager for HITL
150 pub confirmation_manager: Option<Arc<dyn crate::hitl::ConfirmationProvider>>,
151 /// Optional confirmation policy (will be used to create ConfirmationManager if confirmation_manager is not set)
152 pub confirmation_policy: Option<crate::hitl::ConfirmationPolicy>,
153 /// Optional permission checker
154 pub permission_checker: Option<Arc<dyn crate::permissions::PermissionChecker>>,
155 /// Serializable permission policy used to build the checker, when available.
156 pub permission_policy: Option<crate::permissions::PermissionPolicy>,
157 /// Enable planning
158 pub planning_mode: PlanningMode,
159 /// Enable goal tracking
160 pub goal_tracking: bool,
161 /// Extra directories to scan for skill files (*.md).
162 /// Merged with any global `skill_dirs` from [`CodeConfig`].
163 pub skill_dirs: Vec<PathBuf>,
164 /// Optional skill registry for instruction injection
165 pub skill_registry: Option<Arc<crate::skills::SkillRegistry>>,
166 /// Whether active skill `allowed-tools` restrict ordinary session tool calls.
167 ///
168 /// Defaults to false so ordinary tools continue through permission policy,
169 /// hooks, HITL, and AHP. Set true to restore the legacy global active-skill
170 /// restriction behavior.
171 pub enforce_active_skill_tool_restrictions: Option<bool>,
172 /// Optional memory store for long-term memory persistence
173 pub memory_store: Option<Arc<dyn MemoryStore>>,
174 /// Deferred file memory directory — constructed async in `build_session()`
175 pub(crate) file_memory_dir: Option<PathBuf>,
176 /// Optional session store for persistence
177 pub session_store: Option<Arc<dyn crate::store::SessionStore>>,
178 /// Explicit session ID (auto-generated if not set)
179 pub session_id: Option<String>,
180 /// Multi-tenant identifier. Framework only transports this string;
181 /// the host decides what "tenant" means and how to
182 /// aggregate/bill on it. Emitted to hooks/traces, persisted in
183 /// `SessionData`, never interpreted by core.
184 pub tenant_id: Option<String>,
185 /// Identity of the principal that triggered this session (user id,
186 /// service account, etc). Treated as opaque.
187 pub principal: Option<String>,
188 /// Logical identifier of the agent template / definition the session
189 /// was instantiated from. Lets the host aggregate sessions by
190 /// "which agent recipe" independent of the concrete session id.
191 pub agent_template_id: Option<String>,
192 /// Distributed-trace correlation id. Propagated through hooks/traces
193 /// so a session's events join with upstream/downstream work in the
194 /// host's observability pipeline.
195 pub correlation_id: Option<String>,
196 /// Optional host-supplied budget / quota guard. The framework calls
197 /// into it before each LLM call (and reports actuals after) so the
198 /// host can refuse or rate-limit at the cluster level. Default is
199 /// `None` (no enforcement — equivalent to
200 /// [`NoopBudgetGuard`](crate::budget::NoopBudgetGuard)).
201 pub budget_guard: Option<Arc<dyn crate::budget::BudgetGuard>>,
202 /// Optional host-provided ID/Clock pair. Replaces the default
203 /// random-UUID + wall-clock pair, enabling deterministic replay
204 /// on another node. `None` keeps pre-P2 behaviour.
205 pub host_env: Option<Arc<crate::host_env::HostEnv>>,
206 /// Optional FIFO retention caps on the session's in-memory stores
207 /// (run records, run events, trace events, terminal subagent
208 /// tasks). `None` (default) keeps everything — fine for short
209 /// sessions, a memory leak for hours-long cluster workloads.
210 pub retention_limits: Option<crate::retention::SessionRetentionLimits>,
211 /// Auto-save after each completed `send()` or default-history `stream()` call.
212 pub auto_save: bool,
213 /// Optional artifact retention limits for large tool/program outputs.
214 pub artifact_store_limits: Option<crate::tools::ArtifactStoreLimits>,
215 /// Max consecutive parse errors before aborting (overrides default of 2).
216 /// `None` uses the `AgentConfig` default.
217 pub max_parse_retries: Option<u32>,
218 /// Per-tool execution timeout in milliseconds.
219 /// `None` = no timeout (default).
220 pub tool_timeout_ms: Option<u64>,
221 /// Circuit-breaker threshold: max consecutive LLM API failures before
222 /// aborting in non-streaming mode (overrides default of 3).
223 /// `None` uses the `AgentConfig` default.
224 pub circuit_breaker_threshold: Option<u32>,
225 /// Optional concrete sandbox implementation.
226 ///
227 /// When set, `bash` tool commands are routed through this sandbox instead
228 /// of `std::process::Command`. The host application constructs and owns
229 /// the implementation (e.g., an A3S Box–backed handle).
230 pub sandbox_handle: Option<Arc<dyn crate::sandbox::BashSandbox>>,
231 /// Optional host-provided workspace backend.
232 ///
233 /// When set, built-in tools such as `read`, `write`, `ls`, and `bash`
234 /// execute against these workspace capabilities instead of assuming the
235 /// server-local filesystem. This is the primary extension point for DFS,
236 /// browser, container, and remote workspace deployments.
237 pub workspace_services: Option<Arc<crate::workspace::WorkspaceServices>>,
238 /// Enable auto-compaction when context usage exceeds threshold.
239 pub auto_compact: bool,
240 /// Context usage percentage threshold for auto-compaction (0.0 - 1.0).
241 /// Default: 0.80 (80%).
242 pub auto_compact_threshold: Option<f32>,
243 /// Inject a continuation message when the LLM stops without completing the task.
244 /// `None` uses the `AgentConfig` default (true).
245 pub continuation_enabled: Option<bool>,
246 /// Maximum continuation injections per execution.
247 /// `None` uses the `AgentConfig` default (3).
248 pub max_continuation_turns: Option<u32>,
249 /// Maximum execution time in milliseconds.
250 /// `None` = no timeout (default).
251 /// When set, the execution loop will abort if it exceeds this duration.
252 pub max_execution_time_ms: Option<u64>,
253 /// Optional MCP manager for connecting to external MCP servers.
254 ///
255 /// When set, all tools from connected MCP servers are registered and
256 /// available during agent execution with names like `mcp__server__tool`.
257 pub mcp_manager: Option<Arc<crate::mcp::manager::McpManager>>,
258 /// Sampling temperature (0.0–1.0). Overrides the provider default.
259 pub temperature: Option<f32>,
260 /// Extended thinking budget in tokens (Anthropic only).
261 pub thinking_budget: Option<usize>,
262 /// Per-session tool round limit override.
263 ///
264 /// When set, overrides the agent-level `max_tool_rounds` for this session only.
265 /// Maps directly from [`AgentDefinition::max_steps`] when creating sessions
266 /// via [`Agent::session_for_agent`].
267 pub max_tool_rounds: Option<usize>,
268 /// Per-session parallel fan-out limit override.
269 ///
270 /// Applies to delegated `parallel_task`, plan wave execution, and safe
271 /// parallel write batches.
272 pub max_parallel_tasks: Option<usize>,
273 /// Per-session automatic subagent delegation override.
274 pub auto_delegation: Option<crate::config::AutoDelegationConfig>,
275 /// Per-session switch for model-visible manual child-agent tools.
276 ///
277 /// This overlays the effective automatic delegation config instead of
278 /// replacing it, so callers can hide `task` / `parallel_task` while
279 /// preserving other delegation settings.
280 pub manual_delegation_enabled: Option<bool>,
281 /// Per-session kill switch for automatic parallel child-agent fan-out.
282 ///
283 /// This overlays the effective automatic delegation config instead of
284 /// replacing it, so callers can disable auto fan-out without disabling
285 /// automatic delegation itself.
286 pub auto_parallel_delegation: Option<bool>,
287 /// Slot-based system prompt customization.
288 ///
289 /// When set, overrides the agent-level prompt slots for this session.
290 /// Users can customize role, guidelines, response style, and extra instructions
291 /// without losing the core agentic capabilities.
292 pub prompt_slots: Option<SystemPromptSlots>,
293 /// Optional external hook executor (e.g. an AHP harness server).
294 ///
295 /// When set, **replaces** the built-in `HookEngine` for this session.
296 /// All 11 lifecycle events are forwarded to the executor instead of being
297 /// dispatched locally. The executor is also propagated to sub-agents via
298 /// the sentinel hook mechanism.
299 pub hook_executor: Option<Arc<dyn crate::hooks::HookExecutor>>,
300}
301
302// ============================================================================
303// Agent
304// ============================================================================
305
306/// High-level agent facade.
307///
308/// Holds the LLM client and agent config. Workspace-independent.
309/// Use [`Agent::session()`] to bind to a workspace.
310pub struct Agent {
311 code_config: CodeConfig,
312 config: AgentConfig,
313 /// Global MCP manager loaded from config.mcp_servers
314 global_mcp: Option<Arc<crate::mcp::manager::McpManager>>,
315 /// Pre-fetched MCP tool definitions from global_mcp (cached at creation time).
316 /// Wrapped in Mutex so `refresh_mcp_tools()` can update the cache without `&mut self`.
317 global_mcp_tools: std::sync::Mutex<Vec<(String, crate::mcp::McpTool)>>,
318 /// Tracks every live session created by this agent via `Weak` refs so
319 /// the agent can enumerate and forcibly close them. Sessions register
320 /// themselves at construction and become dangling `Weak`s on drop —
321 /// `list_sessions()` / `close_session()` prune dead entries on access.
322 ///
323 /// Uses a synchronous lock so the sync `Agent::session()` factory can
324 /// insert without nesting tokio runtimes. The lock is only held for
325 /// brief insert/scan operations — async close work happens after the
326 /// lock is released.
327 sessions: Arc<std::sync::Mutex<HashMap<String, std::sync::Weak<SessionCloseHandle>>>>,
328 /// Set once `Agent::close()` has been called. Subsequent `session()` /
329 /// `resume_session()` calls fail fast with `CodeError::SessionClosed`.
330 closed: Arc<std::sync::atomic::AtomicBool>,
331}
332
333impl std::fmt::Debug for Agent {
334 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
335 f.debug_struct("Agent").finish()
336 }
337}
338
339impl Agent {
340 /// Create from a config file path or inline ACL-compatible string.
341 ///
342 /// Auto-detects `.acl` file paths vs inline ACL-compatible config.
343 pub async fn new(config_source: impl Into<String>) -> Result<Self> {
344 let config = agent_bootstrap::load_code_config(config_source.into())?;
345 Self::from_config(config).await
346 }
347
348 /// Create from a config file path or inline ACL-compatible string.
349 ///
350 /// Alias for [`Agent::new()`] — provides a consistent API with
351 /// the Python and Node.js SDKs.
352 pub async fn create(config_source: impl Into<String>) -> Result<Self> {
353 Self::new(config_source).await
354 }
355
356 /// Create from a [`CodeConfig`] struct.
357 pub async fn from_config(config: CodeConfig) -> Result<Self> {
358 agent_bootstrap::build_agent_from_config(config).await
359 }
360
361 /// Re-fetch tool definitions from all connected global MCP servers and
362 /// update the internal cache.
363 ///
364 /// Call this when an MCP server has added or removed tools since the
365 /// agent was created. The refreshed tools will be visible to all
366 /// **new** sessions created after this call; existing sessions are
367 /// unaffected (their `ToolExecutor` snapshot is already built).
368 pub async fn refresh_mcp_tools(&self) -> Result<()> {
369 agent_sessions::refresh_mcp_tools(self).await
370 }
371
372 /// Bind to a workspace directory, returning an [`AgentSession`].
373 ///
374 /// Pass `None` for defaults, or `Some(SessionOptions)` to override
375 /// the model, agent directories for this session.
376 pub fn session(
377 &self,
378 workspace: impl Into<String>,
379 options: Option<SessionOptions>,
380 ) -> Result<AgentSession> {
381 agent_sessions::create_session(self, workspace, options)
382 }
383
384 /// Create a session pre-configured from an [`AgentDefinition`].
385 ///
386 /// Maps the definition's `permissions`, `prompt`, `model`, and `max_steps`
387 /// directly into [`SessionOptions`], so markdown/YAML-defined subagents can
388 /// be used by delegation and advanced control-plane flows without manual wiring.
389 ///
390 /// The mapping follows the same logic as the built-in `task` tool:
391 /// - `permissions` → `permission_checker`
392 /// - `prompt` → `prompt_slots.extra`
393 /// - `max_steps` → `max_tool_rounds`
394 /// - `model` → `model` (as `"provider/model"` string)
395 ///
396 /// `extra` can supply additional overrides (e.g. `planning_enabled`) that
397 /// take precedence over the definition's values.
398 pub fn session_for_agent(
399 &self,
400 workspace: impl Into<String>,
401 def: &crate::subagent::AgentDefinition,
402 extra: Option<SessionOptions>,
403 ) -> Result<AgentSession> {
404 agent_sessions::create_session_for_agent(self, workspace, def, extra)
405 }
406
407 /// Create a session from a reproducible disposable worker recipe.
408 ///
409 /// This is the cattle-mode companion to [`Agent::session_for_agent`]: callers
410 /// provide a small [`WorkerAgentSpec`](crate::subagent::WorkerAgentSpec), and
411 /// A3S Code compiles it into the same runtime definition used by delegated agents.
412 pub fn session_for_worker(
413 &self,
414 workspace: impl Into<String>,
415 spec: crate::subagent::WorkerAgentSpec,
416 extra: Option<SessionOptions>,
417 ) -> Result<AgentSession> {
418 let def = spec.into_agent_definition();
419 self.session_for_agent(workspace, &def, extra)
420 }
421
422 /// Resume a previously saved session by ID.
423 ///
424 /// Loads the session data from the store, rebuilds the `AgentSession` with
425 /// the saved conversation history, and returns it ready for continued use.
426 ///
427 /// The `options` must include a `session_store` (or `with_file_session_store`)
428 /// that contains the saved session.
429 ///
430 /// The resumed session uses the **workspace stored in the snapshot**, not a
431 /// workspace from `options`. The store is therefore a trust boundary: its
432 /// contents drive the resumed workspace and the persisted runtime policies.
433 ///
434 /// Runtime: this loads the snapshot via `block_in_place`, so it must be called
435 /// on a multi-threaded Tokio runtime (it panics on a current-thread runtime).
436 pub fn resume_session(
437 &self,
438 session_id: &str,
439 options: SessionOptions,
440 ) -> Result<AgentSession> {
441 agent_sessions::resume_session(self, session_id, options)
442 }
443
444 /// Return the IDs of every live session created from this agent.
445 ///
446 /// "Live" means the caller still holds an [`AgentSession`] — sessions
447 /// that have been dropped are pruned lazily on each call. The list is
448 /// sorted to make output stable for tests/UIs.
449 pub async fn list_sessions(&self) -> Vec<String> {
450 agent_sessions::list_sessions(self).await
451 }
452
453 /// Close a specific live session by its session ID.
454 ///
455 /// Returns `true` when a live session with the given id was found and
456 /// transitioned from open to closed by this call; `false` when no live
457 /// session has that id, or when the session was already closed.
458 ///
459 /// This is the out-of-band counterpart to [`AgentSession::close`]: it
460 /// performs exactly the same cleanup but can be invoked without holding
461 /// a reference to the session itself — useful for control-plane code
462 /// that only knows the session ID.
463 pub async fn close_session(&self, session_id: &str) -> bool {
464 agent_sessions::close_session(self, session_id).await
465 }
466
467 /// Close every live session created from this agent and tear down
468 /// background resources owned by the agent (global MCP connections).
469 ///
470 /// After this call:
471 /// - Every live `AgentSession` is closed (same effect as calling
472 /// [`AgentSession::close`] on each).
473 /// - Subsequent [`Agent::session`] / [`Agent::resume_session`] calls
474 /// fail fast with [`CodeError::SessionClosed`](crate::error::CodeError::SessionClosed).
475 ///
476 /// Idempotent: subsequent calls are no-ops and are guaranteed not to
477 /// panic.
478 pub async fn close(&self) {
479 agent_sessions::close_agent(self).await
480 }
481
482 /// Return whether [`close`](Self::close) has been called on this agent.
483 pub fn is_closed(&self) -> bool {
484 self.closed.load(std::sync::atomic::Ordering::Acquire)
485 }
486
487 /// Disconnect every global MCP server whose last activity is older
488 /// than `idle_threshold_ms`. Returns the names of disconnected
489 /// servers (empty when there is no global MCP manager or when
490 /// nothing is idle).
491 ///
492 /// Hosts running thousands of long-lived sessions should call this
493 /// periodically (e.g. every 60s with a 5-min threshold) to release
494 /// file descriptors and background workers from quiet MCP servers
495 /// without losing the server's configuration. A subsequent tool
496 /// call on the same server will require an explicit reconnect.
497 pub async fn disconnect_idle_mcp(&self, idle_threshold_ms: u64) -> Vec<String> {
498 match &self.global_mcp {
499 Some(mcp) => mcp.disconnect_idle(idle_threshold_ms).await,
500 None => Vec::new(),
501 }
502 }
503
504 #[cfg(test)]
505 fn build_session(
506 &self,
507 workspace: String,
508 llm_client: Arc<dyn LlmClient>,
509 opts: &SessionOptions,
510 ) -> Result<AgentSession> {
511 session_builder::build_agent_session(self, workspace, llm_client, opts)
512 }
513}
514
515// ============================================================================
516// AgentSession
517// ============================================================================
518
519/// Workspace-bound session. All LLM and tool operations happen here.
520///
521/// History is automatically accumulated after each `send()` call and after
522/// `stream()` completes when no custom history is supplied.
523/// Use `history()` to retrieve the current conversation log.
524pub struct AgentSession {
525 llm_client: Arc<dyn LlmClient>,
526 tool_executor: Arc<ToolExecutor>,
527 tool_context: ToolContext,
528 config: AgentConfig,
529 workspace: PathBuf,
530 /// Unique session identifier.
531 session_id: String,
532 /// Internal conversation history, auto-updated after each `send()` and default-history `stream()`.
533 history: Arc<RwLock<Vec<Message>>>,
534 /// Optional lane queue for priority-based tool execution.
535 command_queue: Option<Arc<crate::session_lane_queue::SessionLaneQueue>>,
536 /// Optional long-term memory.
537 memory: Option<Arc<crate::memory::AgentMemory>>,
538 /// Optional session store for persistence.
539 session_store: Option<Arc<dyn crate::store::SessionStore>>,
540 /// Auto-save after each completed `send()` or default-history `stream()`.
541 auto_save: bool,
542 /// Hook engine for lifecycle event interception.
543 hook_engine: Arc<crate::hooks::HookEngine>,
544 /// Optional external hook executor (e.g. AHP harness). When set, replaces
545 /// `hook_engine` as the executor passed to each `AgentLoop`.
546 ahp_executor: Option<Arc<dyn crate::hooks::HookExecutor>>,
547 /// Deferred init warning: emitted as PersistenceFailed on first send() if set.
548 init_warning: Option<String>,
549 /// Slash command registry for `/command` dispatch.
550 /// Uses interior mutability so commands can be registered on a shared `Arc<AgentSession>`.
551 command_registry: std::sync::Mutex<CommandRegistry>,
552 /// Model identifier for display (e.g., "anthropic/claude-sonnet-4-20250514").
553 model_name: String,
554 /// Shared MCP manager — all add_mcp_server / remove_mcp_server calls go here.
555 mcp_manager: Arc<crate::mcp::manager::McpManager>,
556 /// Shared agent registry — populated at session creation; extended via register_agent_dir().
557 agent_registry: Arc<crate::subagent::AgentRegistry>,
558 /// Cancellation token for the current operation (send/stream).
559 /// Stored so that cancel() can abort ongoing LLM calls.
560 cancel_token: Arc<tokio::sync::Mutex<Option<tokio_util::sync::CancellationToken>>>,
561 /// ID of the run currently attached to the active cancellation token.
562 current_run_id: Arc<tokio::sync::Mutex<Option<String>>>,
563 /// In-memory run snapshots and event replay buffer for this session.
564 run_store: Arc<crate::run::InMemoryRunStore>,
565 /// Materialized view of delegated subagent task lifecycle, populated from runtime events.
566 subagent_tasks: Arc<crate::subagent_task_tracker::InMemorySubagentTaskTracker>,
567 /// Currently executing tools observed from runtime events.
568 active_tools: Arc<tokio::sync::RwLock<HashMap<String, ActiveToolState>>>,
569 /// Compact execution traces for this session.
570 trace_sink: crate::trace::InMemoryTraceSink,
571 /// Structured completion evidence collected from agent and explicit verification runs.
572 verification_reports: Arc<RwLock<Vec<crate::verification::VerificationReport>>>,
573 /// Set once `close()` has been called. Subsequent send/stream calls
574 /// fast-fail with [`crate::error::CodeError::SessionClosed`].
575 closed: Arc<std::sync::atomic::AtomicBool>,
576 /// Session-level parent cancellation token.
577 ///
578 /// Every in-flight run (blocking send, stream, delegated subagent task)
579 /// derives its per-operation token from this one via `child_token()`,
580 /// so `session_cancel.cancel()` cascades to all of them. `close()` fires
581 /// this token first, after which any new `child_token()` returns an
582 /// already-cancelled token (defending against close/spawn races).
583 pub(crate) session_cancel: tokio_util::sync::CancellationToken,
584 /// Shared `Arc`-handle used by both [`AgentSession::close`] and the
585 /// parent [`Agent`]'s registry. The handle bundles every field needed
586 /// to perform the close sequence so the two entry points cannot drift.
587 close_handle: Arc<SessionCloseHandle>,
588 /// Runtime-mutable override for the budget guard. When set, takes
589 /// precedence over `config.budget_guard` on the next agent-loop
590 /// build. Lets SDK callers (Node especially) install a host-side
591 /// guard after `session()` has returned without ever putting a
592 /// JS callable into `SessionOptions`.
593 runtime_budget_guard: std::sync::Mutex<Option<Arc<dyn crate::budget::BudgetGuard>>>,
594 /// Multi-tenant label. Framework only carries the string; semantics
595 /// belong to the host.
596 pub(crate) tenant_id: Option<String>,
597 /// Principal that triggered the session (user / service / etc.).
598 pub(crate) principal: Option<String>,
599 /// Logical identifier of the agent template the session was
600 /// instantiated from.
601 pub(crate) agent_template_id: Option<String>,
602 /// Distributed-trace correlation id propagated to hooks / traces.
603 pub(crate) correlation_id: Option<String>,
604}
605
606impl std::fmt::Debug for AgentSession {
607 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
608 f.debug_struct("AgentSession")
609 .field("session_id", &self.session_id)
610 .field("workspace", &self.workspace.display().to_string())
611 .field("auto_save", &self.auto_save)
612 .finish()
613 }
614}
615
616impl AgentSession {
617 /// Get a snapshot of command entries (name, description, optional usage).
618 ///
619 /// Acquires the command registry lock briefly and returns owned data.
620 pub fn command_registry(&self) -> std::sync::MutexGuard<'_, CommandRegistry> {
621 session_commands::registry(self)
622 }
623
624 /// Register a custom slash command.
625 ///
626 /// Takes `&self` so it can be called on a shared `Arc<AgentSession>`.
627 pub fn register_command(&self, cmd: Arc<dyn crate::commands::SlashCommand>) {
628 session_commands::register(self, cmd);
629 }
630
631 /// Return whether [`close`](Self::close) has been called on this session.
632 ///
633 /// Once closed, `send`/`stream` and their attachment variants fast-fail
634 /// with [`crate::error::CodeError::SessionClosed`] instead of starting a
635 /// new run.
636 pub fn is_closed(&self) -> bool {
637 self.closed.load(std::sync::atomic::Ordering::Acquire)
638 }
639
640 /// Clone the session-level [`CancellationToken`](tokio_util::sync::CancellationToken).
641 ///
642 /// All in-flight runs derive their per-operation token from this one via
643 /// `child_token()`, so embedders can:
644 ///
645 /// - Observe the token (e.g. wire it into a host-side `select!`) to
646 /// react to session shutdown without polling [`is_closed`](Self::is_closed);
647 /// - Call `.cancel()` on it to abort every operation in the session
648 /// without going through `close()` (no run-store / hook side effects).
649 ///
650 /// For graceful shutdown prefer [`close`](Self::close), which also marks
651 /// runs as cancelled in the store and fires AHP hooks.
652 pub fn session_cancel_token(&self) -> tokio_util::sync::CancellationToken {
653 self.session_cancel.clone()
654 }
655
656 /// Return the host-defined tenant id, if any.
657 ///
658 /// The framework only transports this string — it never interprets
659 /// or enforces tenant boundaries itself. Use this from custom
660 /// `HookExecutor` / `PermissionChecker` / `BudgetGuard` impls to
661 /// route logic by tenant.
662 pub fn tenant_id(&self) -> Option<&str> {
663 self.tenant_id.as_deref()
664 }
665
666 /// Return the principal that triggered the session, if any.
667 pub fn principal(&self) -> Option<&str> {
668 self.principal.as_deref()
669 }
670
671 /// Return the id of the agent template/definition the session was
672 /// instantiated from, if any.
673 pub fn agent_template_id(&self) -> Option<&str> {
674 self.agent_template_id.as_deref()
675 }
676
677 /// Return the distributed-trace correlation id propagated through
678 /// this session's events, if any.
679 pub fn correlation_id(&self) -> Option<&str> {
680 self.correlation_id.as_deref()
681 }
682
683 /// Install or replace a runtime budget guard. Takes effect on the
684 /// next `send` / `stream` call (the guard is consulted at agent-
685 /// loop build time, not on the live execution). Setting `None`
686 /// clears the override so `config.budget_guard` takes over again.
687 ///
688 /// This is the entry point SDKs use to wire a host-supplied guard
689 /// after the session has already been constructed — useful when
690 /// the guard's transport (e.g. a JS callable) cannot live inside
691 /// the value-typed `SessionOptions`.
692 pub fn set_budget_guard(&self, guard: Option<Arc<dyn crate::budget::BudgetGuard>>) {
693 let mut slot = self
694 .runtime_budget_guard
695 .lock()
696 .unwrap_or_else(|p| p.into_inner());
697 *slot = guard;
698 }
699
700 /// Return the currently-installed runtime budget guard, if any.
701 /// `None` means the loop falls back to `config.budget_guard`.
702 pub fn budget_guard(&self) -> Option<Arc<dyn crate::budget::BudgetGuard>> {
703 self.runtime_budget_guard
704 .lock()
705 .unwrap_or_else(|p| p.into_inner())
706 .clone()
707 }
708
709 /// Proactively close the session and release its in-flight work.
710 ///
711 /// On the first call this:
712 /// 1. flips the session into the **closed** state so further `send`/`stream`
713 /// calls fast-fail with [`crate::error::CodeError::SessionClosed`];
714 /// 2. fires the session-level cancellation token so every derived
715 /// run/subagent token cascades to cancelled;
716 /// 3. marks the active run `Cancelled` in the run store and fires AHP
717 /// hook side effects;
718 /// 4. cancels every still-running delegated subagent task spawned from
719 /// this session;
720 /// 5. cancels all pending human-in-the-loop tool confirmations.
721 ///
722 /// Subsequent calls are no-ops and are guaranteed not to panic.
723 pub async fn close(&self) {
724 // Delegate to the shared handle so this entry point and
725 // `Agent::close_session(id)` cannot drift in behaviour.
726 self.close_handle.close().await;
727 }
728
729 /// Send a prompt and wait for the complete response.
730 ///
731 /// When `history` is `None`, uses (and auto-updates) the session's
732 /// internal conversation history. When `Some`, uses the provided
733 /// history instead (the internal history is **not** modified).
734 ///
735 /// If the prompt starts with `/`, it is dispatched as a slash command
736 /// and the result is returned without calling the LLM.
737 pub async fn send(&self, prompt: &str, history: Option<&[Message]>) -> Result<AgentResult> {
738 conversation_runtime::send(self, prompt, history).await
739 }
740
741 /// Resume a previously-checkpointed run on this session.
742 ///
743 /// Loads the latest [`LoopCheckpoint`](crate::loop_checkpoint::LoopCheckpoint)
744 /// stored under `checkpoint_run_id` and replays the agent loop from
745 /// that boundary state. A **new** run id is allocated for the
746 /// resumed work; the relationship between the old and new run is
747 /// host-tracked — the framework does not interpret
748 /// it.
749 ///
750 /// Returns an error when no `SessionStore` is configured on this
751 /// session, or when no checkpoint exists for `checkpoint_run_id`.
752 pub async fn resume_run(&self, checkpoint_run_id: &str) -> Result<AgentResult> {
753 conversation_runtime::resume_run(self, checkpoint_run_id).await
754 }
755
756 /// Send a prompt with image attachments and wait for the complete response.
757 ///
758 /// Images are included as multi-modal content blocks in the user message.
759 /// Requires a vision-capable model (e.g., Claude Sonnet, GPT-4o).
760 pub async fn send_with_attachments(
761 &self,
762 prompt: &str,
763 attachments: &[crate::llm::Attachment],
764 history: Option<&[Message]>,
765 ) -> Result<AgentResult> {
766 conversation_runtime::send_with_attachments(self, prompt, attachments, history).await
767 }
768
769 /// Stream a prompt with image attachments.
770 ///
771 /// Images are included as multi-modal content blocks in the user message.
772 /// Requires a vision-capable model (e.g., Claude Sonnet, GPT-4o).
773 pub async fn stream_with_attachments(
774 &self,
775 prompt: &str,
776 attachments: &[crate::llm::Attachment],
777 history: Option<&[Message]>,
778 ) -> Result<(mpsc::Receiver<AgentEvent>, JoinHandle<()>)> {
779 conversation_runtime::stream_with_attachments(self, prompt, attachments, history).await
780 }
781
782 /// Send a prompt and stream events back.
783 ///
784 /// When `history` is `None`, uses the session's internal history
785 /// and updates it when the stream completes.
786 /// When `Some`, uses the provided history instead.
787 ///
788 /// If the prompt starts with `/`, it is dispatched as a slash command
789 /// and the result is emitted as a single `TextDelta` + `End` event.
790 pub async fn stream(
791 &self,
792 prompt: &str,
793 history: Option<&[Message]>,
794 ) -> Result<(mpsc::Receiver<AgentEvent>, JoinHandle<()>)> {
795 conversation_runtime::stream(self, prompt, history).await
796 }
797
798 /// Cancel the current ongoing operation (send/stream).
799 ///
800 /// If an operation is in progress, this will trigger cancellation of the LLM streaming
801 /// and tool execution. The operation will terminate as soon as possible.
802 ///
803 /// Returns `true` if an operation was cancelled, `false` if no operation was in progress.
804 pub async fn cancel(&self) -> bool {
805 RunControl::from_session(self).cancel_current().await
806 }
807
808 /// Cancel a specific run only if it is still the active run.
809 ///
810 /// This is useful for SDK callers that hold a previously observed run ID:
811 /// stale run IDs will not cancel a newer operation.
812 pub async fn cancel_run(&self, run_id: &str) -> bool {
813 RunControl::from_session(self).cancel_run(run_id).await
814 }
815
816 /// Return snapshots for runs recorded by this session.
817 pub async fn runs(&self) -> Vec<crate::run::RunSnapshot> {
818 RunControl::from_session(self).runs().await
819 }
820
821 /// Return a snapshot for a recorded run.
822 pub async fn run_snapshot(&self, run_id: &str) -> Option<crate::run::RunSnapshot> {
823 RunControl::from_session(self).run_snapshot(run_id).await
824 }
825
826 /// Return recorded runtime events for a run.
827 pub async fn run_events(&self, run_id: &str) -> Vec<crate::run::RunEventRecord> {
828 RunControl::from_session(self).run_events(run_id).await
829 }
830
831 /// Return a handle for the currently running operation, if any.
832 pub async fn current_run(&self) -> Option<crate::run::RunHandle> {
833 RunControl::from_session(self).current_run().await
834 }
835
836 /// Return active tool calls observed for the currently running operation.
837 pub async fn active_tools(&self) -> Vec<crate::run::ActiveToolSnapshot> {
838 SessionView::from_session(self).active_tools().await
839 }
840
841 /// Look up a delegated subagent task by id. Returns `None` if no such task
842 /// has been observed in this session.
843 pub async fn subagent_task(
844 &self,
845 task_id: &str,
846 ) -> Option<crate::subagent_task_tracker::SubagentTaskSnapshot> {
847 self.subagent_tasks.get(task_id).await
848 }
849
850 /// Return snapshots of every delegated subagent task observed in this
851 /// session (including completed and failed ones), oldest first.
852 pub async fn subagent_tasks(&self) -> Vec<crate::subagent_task_tracker::SubagentTaskSnapshot> {
853 self.subagent_tasks.list_for_parent(&self.session_id).await
854 }
855
856 /// Return snapshots of subagent tasks still in `Running` state.
857 pub async fn pending_subagent_tasks(
858 &self,
859 ) -> Vec<crate::subagent_task_tracker::SubagentTaskSnapshot> {
860 use crate::subagent_task_tracker::SubagentStatus;
861 self.subagent_tasks
862 .list_for_parent(&self.session_id)
863 .await
864 .into_iter()
865 .filter(|task| task.status == SubagentStatus::Running)
866 .collect()
867 }
868
869 /// Cancel an in-flight delegated subagent task by id. Returns `true`
870 /// when a cancellation token was found and fired, `false` when the
871 /// task id is unknown or the task has already finished. The eventual
872 /// `SubagentEnd` from the cancelled child loop won't downgrade the
873 /// terminal status — it stays `Cancelled`.
874 pub async fn cancel_subagent_task(&self, task_id: &str) -> bool {
875 self.subagent_tasks.cancel(task_id).await
876 }
877
878 /// Return a shared handle to the session's subagent task tracker.
879 ///
880 /// Advanced: embedders implementing a custom subagent execution path
881 /// (i.e. spawning child loops outside the built-in `task` tool) can use
882 /// this to register cancellation tokens and feed `AgentEvent`s into the
883 /// tracker so the standard
884 /// [`subagent_task`](Self::subagent_task) / [`pending_subagent_tasks`](Self::pending_subagent_tasks) /
885 /// [`cancel_subagent_task`](Self::cancel_subagent_task) APIs and
886 /// [`close`](Self::close) keep working uniformly across execution paths.
887 pub fn subagent_tracker(
888 &self,
889 ) -> Arc<crate::subagent_task_tracker::InMemorySubagentTaskTracker> {
890 Arc::clone(&self.subagent_tasks)
891 }
892
893 /// Return a snapshot of the session's conversation history.
894 pub fn history(&self) -> Vec<Message> {
895 SessionView::from_session(self).history()
896 }
897
898 /// Return pending HITL tool confirmations for this session.
899 pub async fn pending_confirmations(&self) -> Vec<PendingConfirmationInfo> {
900 HitlControl::from_session(self)
901 .pending_confirmations()
902 .await
903 }
904
905 /// Resolve a pending HITL tool confirmation.
906 ///
907 /// Returns `Ok(true)` when a pending confirmation was found and completed,
908 /// `Ok(false)` when the tool ID is not pending or HITL is not configured.
909 pub async fn confirm_tool_use(
910 &self,
911 tool_id: &str,
912 approved: bool,
913 reason: Option<String>,
914 ) -> Result<bool> {
915 HitlControl::from_session(self)
916 .confirm_tool_use(tool_id, approved, reason)
917 .await
918 }
919
920 /// Cancel all pending HITL confirmations for this session.
921 pub async fn cancel_confirmations(&self) -> usize {
922 HitlControl::from_session(self).cancel_confirmations().await
923 }
924
925 /// Return a reference to the session's memory, if configured.
926 pub fn memory(&self) -> Option<&Arc<crate::memory::AgentMemory>> {
927 SessionView::from_session(self).memory()
928 }
929
930 /// Return the session ID.
931 pub fn id(&self) -> &str {
932 SessionView::from_session(self).id()
933 }
934
935 /// Return the session workspace path.
936 pub fn workspace(&self) -> &std::path::Path {
937 SessionView::from_session(self).workspace()
938 }
939
940 /// Return any deferred init warning (e.g. memory store failed to initialize).
941 pub fn init_warning(&self) -> Option<&str> {
942 SessionView::from_session(self).init_warning()
943 }
944
945 /// Return the session ID.
946 pub fn session_id(&self) -> &str {
947 SessionView::from_session(self).id()
948 }
949
950 /// An [`AgentExecutor`](crate::orchestration::AgentExecutor) backed by this
951 /// session — runs each orchestrated step as a child agent on this node,
952 /// inheriting the session's agent registry, LLM client, workspace, MCP
953 /// tools, and subagent tracker.
954 ///
955 /// This is what the orchestration combinators
956 /// ([`execute_steps_parallel`](crate::orchestration::execute_steps_parallel),
957 /// [`execute_pipeline`](crate::orchestration::execute_pipeline),
958 /// [`execute_steps_parallel_resumable`](crate::orchestration::execute_steps_parallel_resumable))
959 /// run against; a host can instead supply its own executor to place steps
960 /// across a cluster.
961 pub fn agent_executor(&self) -> Arc<dyn crate::orchestration::AgentExecutor> {
962 Arc::new(self.build_task_executor(self.parent_run_context()))
963 }
964
965 /// Build the in-box [`TaskExecutor`](crate::tools::TaskExecutor) for this
966 /// session, applying `parent` as the child-run capability context. Shared by
967 /// [`agent_executor`](Self::agent_executor) and [`workflow`](Self::workflow)
968 /// so both wire children identically.
969 fn build_task_executor(
970 &self,
971 parent: crate::child_run::ChildRunContext,
972 ) -> crate::tools::TaskExecutor {
973 crate::tools::TaskExecutor::with_mcp(
974 Arc::clone(&self.agent_registry),
975 Arc::clone(&self.llm_client),
976 self.workspace.display().to_string(),
977 Arc::clone(&self.mcp_manager),
978 )
979 .with_parent_context(parent)
980 .with_subagent_tracker(Arc::clone(&self.subagent_tasks))
981 .with_max_parallel_tasks(self.config.max_parallel_tasks)
982 }
983
984 /// A programmable [`Workflow`](crate::orchestration::Workflow) bound to this
985 /// session.
986 ///
987 /// Pre-wired with this session's executor (inheriting the same governance as
988 /// model-driven delegation), persistence store (so each
989 /// [`phase`](crate::orchestration::Workflow::phase) is a resume boundary),
990 /// per-step event stream, and a session-derived stable root id. Control flow
991 /// is ordinary Rust: `await` a verb, inspect the outcomes, decide what runs
992 /// next.
993 pub fn workflow(&self) -> crate::orchestration::Workflow {
994 self.workflow_with_token_budget(None)
995 }
996
997 /// Like [`workflow`](Self::workflow) but with a hard token ceiling shared
998 /// across every step. The cap is a best-effort *soft* cost ceiling — under a
999 /// wide fan-out a few in-flight turns can race past it before the shared
1000 /// ledger catches up (see [`WorkflowBudget`](crate::orchestration::WorkflowBudget)).
1001 pub fn workflow_with_token_budget(
1002 &self,
1003 limit_tokens: Option<u64>,
1004 ) -> crate::orchestration::Workflow {
1005 use crate::budget::BudgetGuard;
1006
1007 // One shared ledger for the whole workflow, wrapping the session's own
1008 // budget guard (if any) so a host's per-tenant accounting keeps working.
1009 let mut budget = crate::orchestration::WorkflowBudget::new(limit_tokens);
1010 if let Some(inner) = self.config.budget_guard.clone() {
1011 budget = budget.with_inner(inner);
1012 }
1013 let budget = Arc::new(budget);
1014
1015 // Install the shared ledger as the child runs' budget guard so every
1016 // step's per-turn LLM accounting feeds it.
1017 let mut parent = self.parent_run_context();
1018 parent.budget_guard = Some(Arc::clone(&budget) as Arc<dyn BudgetGuard>);
1019 let executor: Arc<dyn crate::orchestration::AgentExecutor> =
1020 Arc::new(self.build_task_executor(parent));
1021
1022 let mut builder = crate::orchestration::Workflow::builder(executor)
1023 .with_root_id(format!("wf-{}", self.session_id))
1024 .with_budget(Arc::clone(&budget));
1025 if let Some(store) = self.session_store.clone() {
1026 builder = builder.with_store(store);
1027 }
1028 if let Some(step_events) = self.tool_context.agent_event_tx.clone() {
1029 builder = builder.with_step_events(step_events);
1030 }
1031 builder.build()
1032 }
1033
1034 /// Build the [`ChildRunContext`](crate::child_run::ChildRunContext) that
1035 /// orchestrated / delegated child runs inherit from this session.
1036 ///
1037 /// Mirrors the context the model-driven `task` / `parallel_task` path
1038 /// installs (see `register_task_capability` in `agent_api/capabilities.rs`)
1039 /// so a step run through [`agent_executor`](Self::agent_executor) carries the
1040 /// SAME governance — security provider, skill restrictions, confirmation,
1041 /// the shared workspace, and the safety limits — instead of weaker, ambient
1042 /// authority. Sourced from the session's resolved config; `hook_engine`
1043 /// stays `None` to match the model-driven path.
1044 pub(crate) fn parent_run_context(&self) -> crate::child_run::ChildRunContext {
1045 crate::child_run::ChildRunContext {
1046 security_provider: self.config.security_provider.clone(),
1047 hook_engine: None,
1048 skill_registry: self.config.skill_registry.clone(),
1049 tool_timeout_ms: self.config.tool_timeout_ms,
1050 max_parallel_tasks: Some(self.config.max_parallel_tasks),
1051 max_execution_time_ms: self.config.max_execution_time_ms,
1052 circuit_breaker_threshold: Some(self.config.circuit_breaker_threshold),
1053 confirmation_manager: self.config.confirmation_manager.clone(),
1054 workspace_services: Some(Arc::clone(&self.tool_context.workspace_services)),
1055 budget_guard: self.config.budget_guard.clone(),
1056 }
1057 }
1058
1059 /// The session's persistence store, if one is configured — needed by the
1060 /// resumable orchestration combinator to journal workflow progress.
1061 pub fn session_store(&self) -> Option<Arc<dyn crate::store::SessionStore>> {
1062 self.session_store.clone()
1063 }
1064
1065 /// Return the definitions of all tools currently registered in this session.
1066 ///
1067 /// The list reflects the live state of the tool executor — tools added via
1068 /// `add_mcp_server()` appear immediately; tools removed via
1069 /// `remove_mcp_server()` disappear immediately.
1070 pub fn tool_definitions(&self) -> Vec<crate::llm::ToolDefinition> {
1071 DirectToolRuntime::from_session(self).definitions()
1072 }
1073
1074 /// Return the names of all tools currently registered on this session.
1075 ///
1076 /// Equivalent to `tool_definitions().into_iter().map(|t| t.name).collect()`.
1077 /// Tools added via [`add_mcp_server`] appear immediately; tools removed via
1078 /// [`remove_mcp_server`] disappear immediately.
1079 pub fn tool_names(&self) -> Vec<String> {
1080 DirectToolRuntime::from_session(self).names()
1081 }
1082
1083 /// Return a stored tool artifact by URI, if it exists in this session.
1084 pub fn get_artifact(&self, artifact_uri: &str) -> Option<crate::tools::ToolArtifact> {
1085 DirectToolRuntime::from_session(self).artifact(artifact_uri)
1086 }
1087
1088 /// Return compact execution trace events recorded for this session.
1089 pub fn trace_events(&self) -> Vec<crate::trace::TraceEvent> {
1090 SessionView::from_session(self).trace_events()
1091 }
1092
1093 /// Return structured verification reports recorded for this session.
1094 pub fn verification_reports(&self) -> Vec<crate::verification::VerificationReport> {
1095 VerificationRuntime::from_session(self).reports()
1096 }
1097
1098 /// Return a structured summary of all verification reports recorded for this session.
1099 pub fn verification_summary(&self) -> crate::verification::VerificationSummary {
1100 VerificationRuntime::from_session(self).summary()
1101 }
1102
1103 /// Return a concise human-readable verification summary for this session.
1104 pub fn verification_summary_text(&self) -> String {
1105 VerificationRuntime::from_session(self).summary_text()
1106 }
1107
1108 /// Add externally produced verification reports to this session's completion evidence.
1109 pub fn record_verification_reports(
1110 &self,
1111 reports: impl IntoIterator<Item = crate::verification::VerificationReport>,
1112 ) {
1113 VerificationRuntime::from_session(self).record(reports);
1114 }
1115
1116 // ========================================================================
1117 // Hook API
1118 // ========================================================================
1119
1120 /// Register a hook for lifecycle event interception.
1121 pub fn register_hook(&self, hook: crate::hooks::Hook) {
1122 HookControl::from_session(self).register_hook(hook);
1123 }
1124
1125 /// Unregister a hook by ID.
1126 pub fn unregister_hook(&self, hook_id: &str) -> Option<crate::hooks::Hook> {
1127 HookControl::from_session(self).unregister_hook(hook_id)
1128 }
1129
1130 /// Register a handler for a specific hook.
1131 pub fn register_hook_handler(
1132 &self,
1133 hook_id: &str,
1134 handler: Arc<dyn crate::hooks::HookHandler>,
1135 ) {
1136 HookControl::from_session(self).register_hook_handler(hook_id, handler);
1137 }
1138
1139 /// Unregister a hook handler by hook ID.
1140 pub fn unregister_hook_handler(&self, hook_id: &str) {
1141 HookControl::from_session(self).unregister_hook_handler(hook_id);
1142 }
1143
1144 /// Get the number of registered hooks.
1145 pub fn hook_count(&self) -> usize {
1146 HookControl::from_session(self).hook_count()
1147 }
1148
1149 /// Save the session to the configured store.
1150 ///
1151 /// Returns `Ok(())` if saved successfully, or if no store is configured (no-op).
1152 pub async fn save(&self) -> Result<()> {
1153 session_save::save(self).await
1154 }
1155
1156 /// Read a file from the workspace.
1157 pub async fn read_file(&self, path: &str) -> Result<String> {
1158 DirectToolRuntime::from_session(self).read_file(path).await
1159 }
1160
1161 /// Write a file in the workspace.
1162 pub async fn write_file(&self, path: &str, content: &str) -> Result<ToolCallResult> {
1163 DirectToolRuntime::from_session(self)
1164 .write_file(path, content)
1165 .await
1166 }
1167
1168 /// List a directory in the workspace.
1169 pub async fn ls(&self, path: Option<&str>) -> Result<ToolCallResult> {
1170 DirectToolRuntime::from_session(self).ls(path).await
1171 }
1172
1173 /// Edit a file by replacing text in the workspace.
1174 pub async fn edit_file(
1175 &self,
1176 path: &str,
1177 old_string: &str,
1178 new_string: &str,
1179 replace_all: bool,
1180 ) -> Result<ToolCallResult> {
1181 DirectToolRuntime::from_session(self)
1182 .edit_file(path, old_string, new_string, replace_all)
1183 .await
1184 }
1185
1186 /// Apply a unified diff patch to a workspace file.
1187 pub async fn patch_file(&self, path: &str, diff: &str) -> Result<ToolCallResult> {
1188 DirectToolRuntime::from_session(self)
1189 .patch_file(path, diff)
1190 .await
1191 }
1192
1193 /// Execute a bash command in the workspace.
1194 ///
1195 /// When a sandbox handle is configured via
1196 /// [`SessionOptions::with_sandbox_handle()`], the command is routed through
1197 /// that sandbox.
1198 pub async fn bash(&self, command: &str) -> Result<String> {
1199 DirectToolRuntime::from_session(self).bash(command).await
1200 }
1201
1202 /// Run verification commands through the session's tool execution path.
1203 pub async fn verify_commands(
1204 &self,
1205 subject: &str,
1206 commands: &[crate::verification::VerificationCommand],
1207 ) -> Result<crate::verification::VerificationReport> {
1208 VerificationRuntime::from_session(self)
1209 .verify_commands(subject, commands)
1210 .await
1211 }
1212
1213 /// Return project-aware verification command presets for this workspace.
1214 pub fn verification_presets(&self) -> Vec<crate::verification::VerificationPreset> {
1215 VerificationRuntime::from_session(self).presets()
1216 }
1217
1218 /// Search for files matching a glob pattern.
1219 pub async fn glob(&self, pattern: &str) -> Result<Vec<String>> {
1220 DirectToolRuntime::from_session(self).glob(pattern).await
1221 }
1222
1223 /// Search file contents with a regex pattern.
1224 pub async fn grep(&self, pattern: &str) -> Result<String> {
1225 DirectToolRuntime::from_session(self).grep(pattern).await
1226 }
1227
1228 /// Execute a tool by name, bypassing the LLM.
1229 pub async fn tool(&self, name: &str, args: serde_json::Value) -> Result<ToolCallResult> {
1230 DirectToolRuntime::from_session(self).call(name, args).await
1231 }
1232
1233 // ========================================================================
1234 // Advanced optional Queue API
1235 // ========================================================================
1236
1237 /// Returns whether this session has an advanced lane queue configured.
1238 pub fn has_queue(&self) -> bool {
1239 QueueControl::from_session(self).has_queue()
1240 }
1241
1242 /// Configure a lane's handler mode for explicit external/hybrid dispatch.
1243 ///
1244 /// Only effective when a queue is configured via `SessionOptions::with_queue_config`.
1245 pub async fn set_lane_handler(&self, lane: SessionLane, config: LaneHandlerConfig) {
1246 QueueControl::from_session(self)
1247 .set_lane_handler(lane, config)
1248 .await;
1249 }
1250
1251 /// Complete an external queue task by ID.
1252 ///
1253 /// Returns `true` if the task was found and completed, `false` if not found.
1254 pub async fn complete_external_task(&self, task_id: &str, result: ExternalTaskResult) -> bool {
1255 QueueControl::from_session(self)
1256 .complete_external_task(task_id, result)
1257 .await
1258 }
1259
1260 /// Get pending external queue tasks awaiting completion by an external handler.
1261 pub async fn pending_external_tasks(&self) -> Vec<ExternalTask> {
1262 QueueControl::from_session(self)
1263 .pending_external_tasks()
1264 .await
1265 }
1266
1267 /// Get optional queue statistics (pending, active, external counts per lane).
1268 pub async fn queue_stats(&self) -> SessionQueueStats {
1269 QueueControl::from_session(self).stats().await
1270 }
1271
1272 /// Get a metrics snapshot from the optional queue (if metrics are enabled).
1273 pub async fn queue_metrics(&self) -> Option<MetricsSnapshot> {
1274 QueueControl::from_session(self).metrics().await
1275 }
1276
1277 /// Get dead letters from the optional queue's DLQ (if DLQ is enabled).
1278 pub async fn dead_letters(&self) -> Vec<DeadLetter> {
1279 QueueControl::from_session(self).dead_letters().await
1280 }
1281
1282 // ========================================================================
1283 // MCP API
1284 // ========================================================================
1285
1286 /// Register all agents found in a directory with the live session.
1287 ///
1288 /// Scans `dir` for `*.yaml`, `*.yml`, and `*.md` agent definition files,
1289 /// parses them, and adds each one to the shared `AgentRegistry` used by the
1290 /// `task` tool. New agents are immediately usable via `task(agent="…")` in
1291 /// the same session — no restart required.
1292 ///
1293 /// Returns the number of agents successfully loaded from the directory.
1294 pub fn register_agent_dir(&self, dir: &std::path::Path) -> usize {
1295 SessionExtensionRuntime::from_session(self).register_agent_dir(dir)
1296 }
1297
1298 /// Register a disposable worker agent with the live session.
1299 ///
1300 /// The returned definition is immediately available to the `task` tool by
1301 /// worker name, so callers can create many reproducible workers without
1302 /// writing temporary agent files or restarting the session.
1303 pub fn register_worker_agent(
1304 &self,
1305 spec: crate::subagent::WorkerAgentSpec,
1306 ) -> crate::subagent::AgentDefinition {
1307 SessionExtensionRuntime::from_session(self).register_worker_agent(spec)
1308 }
1309
1310 /// Register multiple disposable worker agents with the live session.
1311 pub fn register_worker_agents<I>(&self, specs: I) -> Vec<crate::subagent::AgentDefinition>
1312 where
1313 I: IntoIterator<Item = crate::subagent::WorkerAgentSpec>,
1314 {
1315 SessionExtensionRuntime::from_session(self).register_worker_agents(specs)
1316 }
1317
1318 /// Add an MCP server to this session.
1319 ///
1320 /// Registers, connects, and makes all tools immediately available for the
1321 /// agent to call. Tool names follow the convention `mcp__<name>__<tool>`.
1322 ///
1323 /// Returns the number of tools registered from the server.
1324 pub async fn add_mcp_server(
1325 &self,
1326 config: crate::mcp::McpServerConfig,
1327 ) -> crate::error::Result<usize> {
1328 SessionExtensionRuntime::from_session(self)
1329 .add_mcp_server(config)
1330 .await
1331 }
1332
1333 /// The session's tool executor, for installing agent-dir `tools/` entries
1334 /// (e.g. a `kind = "script"` tool) into the live registry. Internal seam used
1335 /// by [`serve::install_agent_dir_tools`](crate::serve::install_agent_dir_tools)
1336 /// (the only caller, hence the `serve` gate).
1337 #[cfg(feature = "serve")]
1338 pub(crate) fn tool_executor(&self) -> &Arc<crate::tools::ToolExecutor> {
1339 &self.tool_executor
1340 }
1341
1342 /// Remove an MCP server from this session.
1343 ///
1344 /// Disconnects the server and unregisters all its tools from the executor.
1345 /// No-op if the server was never added.
1346 pub async fn remove_mcp_server(&self, server_name: &str) -> crate::error::Result<()> {
1347 SessionExtensionRuntime::from_session(self)
1348 .remove_mcp_server(server_name)
1349 .await
1350 }
1351
1352 /// Return the connection status of all MCP servers registered with this session.
1353 pub async fn mcp_status(
1354 &self,
1355 ) -> std::collections::HashMap<String, crate::mcp::McpServerStatus> {
1356 SessionExtensionRuntime::from_session(self)
1357 .mcp_status()
1358 .await
1359 }
1360}
1361
1362// ============================================================================
1363// Tests
1364// ============================================================================
1365
1366#[cfg(test)]
1367mod tests;