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