Skip to main content

a3s_code_core/agent_api/
session_options.rs

1//! Session option builder interface.
2//!
3//! `SessionOptions` is the host-facing capability configuration for a session.
4//! Keeping the builder implementation here lets `agent_api.rs` keep the type
5//! shape visible while moving option construction behavior behind this module.
6
7use super::SessionOptions;
8use crate::prompts::{PlanningMode, SystemPromptSlots};
9use crate::queue::SessionQueueConfig;
10use crate::subagent::WorkerAgentSpec;
11use a3s_memory::MemoryStore;
12use std::path::PathBuf;
13use std::sync::Arc;
14
15impl std::fmt::Debug for SessionOptions {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("SessionOptions")
18            .field("model", &self.model)
19            .field("agent_dirs", &self.agent_dirs)
20            .field("worker_agents", &self.worker_agents.len())
21            .field("skill_dirs", &self.skill_dirs)
22            .field("queue_config", &self.queue_config)
23            .field("security_provider", &self.security_provider.is_some())
24            .field("llm_client", &self.llm_client.is_some())
25            .field("context_providers", &self.context_providers.len())
26            .field("confirmation_manager", &self.confirmation_manager.is_some())
27            .field("permission_checker", &self.permission_checker.is_some())
28            .field("permission_policy", &self.permission_policy.is_some())
29            .field("planning_mode", &self.planning_mode)
30            .field("goal_tracking", &self.goal_tracking)
31            .field(
32                "skill_registry",
33                &self
34                    .skill_registry
35                    .as_ref()
36                    .map(|r| format!("{} skills", r.len())),
37            )
38            .field(
39                "enforce_active_skill_tool_restrictions",
40                &self.enforce_active_skill_tool_restrictions,
41            )
42            .field("memory_store", &self.memory_store.is_some())
43            .field("session_store", &self.session_store.is_some())
44            .field("session_id", &self.session_id)
45            .field("rl_trajectory", &self.rl_trajectory)
46            .field("llm_logprobs", &self.llm_logprobs)
47            .field("llm_top_logprobs", &self.llm_top_logprobs)
48            .field("auto_save", &self.auto_save)
49            .field("artifact_store_limits", &self.artifact_store_limits)
50            .field("max_parse_retries", &self.max_parse_retries)
51            .field("tool_timeout_ms", &self.tool_timeout_ms)
52            .field("llm_api_timeout_ms", &self.llm_api_timeout_ms)
53            .field("circuit_breaker_threshold", &self.circuit_breaker_threshold)
54            .field("sandbox_handle", &self.sandbox_handle.is_some())
55            .field("workspace_services", &self.workspace_services.is_some())
56            .field("auto_compact", &self.auto_compact)
57            .field("auto_compact_threshold", &self.auto_compact_threshold)
58            .field("continuation_enabled", &self.continuation_enabled)
59            .field("max_continuation_turns", &self.max_continuation_turns)
60            .field("mcp_manager", &self.mcp_manager.is_some())
61            .field("temperature", &self.temperature)
62            .field("thinking_budget", &self.thinking_budget)
63            .field("max_tool_rounds", &self.max_tool_rounds)
64            .field("max_parallel_tasks", &self.max_parallel_tasks)
65            .field("auto_delegation", &self.auto_delegation)
66            .field("manual_delegation_enabled", &self.manual_delegation_enabled)
67            .field("auto_parallel_delegation", &self.auto_parallel_delegation)
68            .field("prompt_slots", &self.prompt_slots.is_some())
69            .finish()
70    }
71}
72
73impl SessionOptions {
74    pub fn new() -> Self {
75        Self::default()
76    }
77
78    pub fn with_model(mut self, model: impl Into<String>) -> Self {
79        self.model = Some(model.into());
80        self
81    }
82
83    pub fn with_agent_dir(mut self, dir: impl Into<PathBuf>) -> Self {
84        self.agent_dirs.push(dir.into());
85        self
86    }
87
88    /// Register a cattle-style worker with this session's task delegation registry.
89    pub fn with_worker_agent(mut self, spec: WorkerAgentSpec) -> Self {
90        self.worker_agents.push(spec);
91        self
92    }
93
94    /// Register multiple cattle-style workers with this session.
95    pub fn with_worker_agents<I>(mut self, specs: I) -> Self
96    where
97        I: IntoIterator<Item = WorkerAgentSpec>,
98    {
99        self.worker_agents.extend(specs);
100        self
101    }
102
103    pub fn with_queue_config(mut self, config: SessionQueueConfig) -> Self {
104        self.queue_config = Some(config);
105        self
106    }
107
108    /// Enable default security provider with taint tracking and output sanitization
109    pub fn with_default_security(mut self) -> Self {
110        self.security_provider = Some(Arc::new(crate::security::DefaultSecurityProvider::new()));
111        self
112    }
113
114    /// Set a custom security provider
115    pub fn with_security_provider(
116        mut self,
117        provider: Arc<dyn crate::security::SecurityProvider>,
118    ) -> Self {
119        self.security_provider = Some(provider);
120        self
121    }
122
123    /// Provide a custom LLM client for this session.
124    ///
125    /// When set, this client is used directly, overriding the `provider/model`
126    /// factory resolution. Use it to plug in a provider the built-in factory
127    /// does not cover, a deterministic record/replay client for tests, or an
128    /// HTTP-layer proxy/audit wrapper. Mirrors [`Self::with_workspace_backend`];
129    /// the `provider/model` config path remains the default when unset.
130    pub fn with_llm_client(mut self, client: Arc<dyn crate::llm::LlmClient>) -> Self {
131        self.llm_client = Some(client);
132        self
133    }
134
135    /// Add a file system context provider for simple RAG
136    pub fn with_fs_context(mut self, root_path: impl Into<PathBuf>) -> Self {
137        let config = crate::context::FileSystemContextConfig::new(root_path);
138        self.context_providers
139            .push(Arc::new(crate::context::FileSystemContextProvider::new(
140                config,
141            )));
142        self
143    }
144
145    /// Add a custom context provider
146    pub fn with_context_provider(
147        mut self,
148        provider: Arc<dyn crate::context::ContextProvider>,
149    ) -> Self {
150        self.context_providers.push(provider);
151        self
152    }
153
154    /// Set a confirmation manager for HITL
155    pub fn with_confirmation_manager(
156        mut self,
157        manager: Arc<dyn crate::hitl::ConfirmationProvider>,
158    ) -> Self {
159        self.confirmation_manager = Some(manager);
160        self
161    }
162
163    /// Set a confirmation policy for HITL
164    ///
165    /// The policy will be used to create a ConfirmationManager when the session is built.
166    /// This is the preferred way to configure HITL from the Node SDK.
167    pub fn with_confirmation_policy(mut self, policy: crate::hitl::ConfirmationPolicy) -> Self {
168        self.confirmation_policy = Some(policy);
169        self
170    }
171
172    /// Set a serializable permission policy for tool execution.
173    pub fn with_permission_policy(mut self, policy: crate::permissions::PermissionPolicy) -> Self {
174        self.permission_checker = Some(Arc::new(policy.clone()));
175        self.permission_policy = Some(policy);
176        self
177    }
178
179    /// Set a permission checker
180    pub fn with_permission_checker(
181        mut self,
182        checker: Arc<dyn crate::permissions::PermissionChecker>,
183    ) -> Self {
184        self.permission_checker = Some(checker);
185        self
186    }
187
188    /// Set planning mode
189    pub fn with_planning_mode(mut self, mode: PlanningMode) -> Self {
190        self.planning_mode = mode;
191        self
192    }
193
194    /// Enable planning (shortcut for `with_planning_mode(PlanningMode::Enabled)`)
195    pub fn with_planning(mut self, enabled: bool) -> Self {
196        self.planning_mode = if enabled {
197            PlanningMode::Enabled
198        } else {
199            PlanningMode::Disabled
200        };
201        self
202    }
203
204    /// Enable goal tracking
205    pub fn with_goal_tracking(mut self, enabled: bool) -> Self {
206        self.goal_tracking = enabled;
207        self
208    }
209
210    /// Add a skill registry with built-in skills
211    pub fn with_builtin_skills(mut self) -> Self {
212        self.skill_registry = Some(Arc::new(crate::skills::SkillRegistry::with_builtins()));
213        self
214    }
215
216    /// Add a custom skill registry
217    pub fn with_skill_registry(mut self, registry: Arc<crate::skills::SkillRegistry>) -> Self {
218        self.skill_registry = Some(registry);
219        self
220    }
221
222    /// Enable or disable legacy global active-skill `allowed-tools` restrictions.
223    ///
224    /// The default is disabled: active skills do not block ordinary session
225    /// tools before the host permission/AHP/HITL approval chain runs.
226    pub fn with_active_skill_tool_restrictions(mut self, enabled: bool) -> Self {
227        self.enforce_active_skill_tool_restrictions = Some(enabled);
228        self
229    }
230
231    /// Add skill directories to scan for skill files (*.md).
232    /// Merged with any global `skill_dirs` from [`CodeConfig`] at session build time.
233    pub fn with_skill_dirs(mut self, dirs: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
234        self.skill_dirs.extend(dirs.into_iter().map(Into::into));
235        self
236    }
237
238    /// Load skills from a directory (eager — scans immediately into a registry).
239    pub fn with_skills_from_dir(mut self, dir: impl AsRef<std::path::Path>) -> Self {
240        let registry = self
241            .skill_registry
242            .unwrap_or_else(|| Arc::new(crate::skills::SkillRegistry::new()));
243        if let Err(e) = registry.load_from_dir(&dir) {
244            tracing::warn!(
245                dir = %dir.as_ref().display(),
246                error = %e,
247                "Failed to load skills from directory — continuing without them"
248            );
249        }
250        self.skill_registry = Some(registry);
251        self
252    }
253
254    /// Set a custom memory store override.
255    ///
256    /// Sessions resolve a default memory store when no override is provided.
257    pub fn with_memory(mut self, store: Arc<dyn MemoryStore>) -> Self {
258        self.memory_store = Some(store);
259        self
260    }
261
262    /// Use a file-based memory store at the given directory instead of the default.
263    ///
264    /// The store is created lazily when the session is built (requires async).
265    /// This stores the directory path; `FileMemoryStore::new()` is called during
266    /// session construction.
267    pub fn with_file_memory(mut self, dir: impl Into<PathBuf>) -> Self {
268        self.file_memory_dir = Some(dir.into());
269        self
270    }
271
272    /// Set a session store for persistence
273    pub fn with_session_store(mut self, store: Arc<dyn crate::store::SessionStore>) -> Self {
274        self.session_store = Some(store);
275        self
276    }
277
278    /// Use a file-based session store at the given directory
279    pub fn with_file_session_store(mut self, dir: impl Into<PathBuf>) -> Self {
280        let dir = dir.into();
281        match tokio::runtime::Handle::try_current() {
282            Ok(handle) => {
283                match tokio::task::block_in_place(|| {
284                    handle.block_on(crate::store::FileSessionStore::new(dir))
285                }) {
286                    Ok(store) => {
287                        self.session_store =
288                            Some(Arc::new(store) as Arc<dyn crate::store::SessionStore>);
289                    }
290                    Err(e) => {
291                        tracing::warn!("Failed to create file session store: {}", e);
292                    }
293                }
294            }
295            Err(_) => {
296                tracing::warn!(
297                    "No async runtime available for file session store — persistence disabled"
298                );
299            }
300        }
301        self
302    }
303
304    /// Set an explicit session ID (auto-generated UUID if not set)
305    pub fn with_session_id(mut self, id: impl Into<String>) -> Self {
306        self.session_id = Some(id.into());
307        self
308    }
309
310    /// Tag the session with a host-defined tenant id. Opaque to the
311    /// framework — propagated to `SessionData`, hooks, and traces.
312    pub fn with_tenant_id(mut self, tenant: impl Into<String>) -> Self {
313        self.tenant_id = Some(tenant.into());
314        self
315    }
316
317    /// Tag the session with the id of the principal (user / service
318    /// account / etc.) that triggered it.
319    pub fn with_principal(mut self, principal: impl Into<String>) -> Self {
320        self.principal = Some(principal.into());
321        self
322    }
323
324    /// Tag the session with the id of the agent template / definition it
325    /// was instantiated from.
326    pub fn with_agent_template_id(mut self, template_id: impl Into<String>) -> Self {
327        self.agent_template_id = Some(template_id.into());
328        self
329    }
330
331    /// Attach a distributed-trace correlation id so this session's events
332    /// can be joined with upstream/downstream work.
333    pub fn with_correlation_id(mut self, corr: impl Into<String>) -> Self {
334        self.correlation_id = Some(corr.into());
335        self
336    }
337
338    /// Install a host-supplied [`BudgetGuard`](crate::budget::BudgetGuard).
339    ///
340    /// The guard is consulted before every LLM call (and after, for
341    /// usage accounting). When unset, no budget enforcement happens.
342    pub fn with_budget_guard(mut self, guard: Arc<dyn crate::budget::BudgetGuard>) -> Self {
343        self.budget_guard = Some(guard);
344        self
345    }
346
347    /// Install a host-provided [`HostEnv`](crate::host_env::HostEnv) for
348    /// deterministic ID generation and time. Replaces the framework
349    /// default of `uuid::Uuid::new_v4()` + wall clock — used by
350    /// host replay infrastructure to recreate a run bit-identical on
351    /// another node.
352    pub fn with_host_env(mut self, env: Arc<crate::host_env::HostEnv>) -> Self {
353        self.host_env = Some(env);
354        self
355    }
356
357    /// Install FIFO retention caps for the session's in-memory stores.
358    ///
359    /// Without these caps the in-memory run store, trace sink, and
360    /// subagent task tracker grow unboundedly across long-running
361    /// sessions. Hosts running thousands of long-lived sessions per
362    /// node should set sensible caps (e.g. retain the last 100 runs,
363    /// 5000 events per run, 10000 trace events, 1000 terminal subagent
364    /// tasks). When unset, the framework keeps every record — the
365    /// pre-existing behaviour.
366    pub fn with_retention_limits(
367        mut self,
368        limits: crate::retention::SessionRetentionLimits,
369    ) -> Self {
370        self.retention_limits = Some(limits);
371        self
372    }
373
374    /// Enable structured JSONL trajectory capture for this session.
375    ///
376    /// This is the preferred programmatic path for RL training and deployed
377    /// service data collection. Environment-only deployments can instead set
378    /// `A3S_CODE_TRAJECTORY_PATH`.
379    pub fn with_rl_trajectory(mut self, config: crate::rl_trajectory::RlTrajectoryConfig) -> Self {
380        self.rl_trajectory = Some(config);
381        self
382    }
383
384    /// Request token-level log probabilities from compatible LLM providers.
385    pub fn with_llm_logprobs(mut self, enabled: bool) -> Self {
386        self.llm_logprobs = Some(enabled);
387        self
388    }
389
390    /// Request up to `top_logprobs` alternative logprobs per generated token.
391    pub fn with_llm_top_logprobs(mut self, top_logprobs: usize) -> Self {
392        self.llm_logprobs = Some(true);
393        self.llm_top_logprobs = Some(top_logprobs);
394        self
395    }
396
397    /// Enable auto-save after each `send()` call
398    pub fn with_auto_save(mut self, enabled: bool) -> Self {
399        self.auto_save = enabled;
400        self
401    }
402
403    /// Set artifact retention limits for this session.
404    pub fn with_artifact_store_limits(mut self, limits: crate::tools::ArtifactStoreLimits) -> Self {
405        self.artifact_store_limits = Some(limits);
406        self
407    }
408
409    /// Set the maximum number of consecutive malformed-tool-args errors before
410    /// the agent loop bails.
411    ///
412    /// Default: 2 (the LLM gets two chances to self-correct before the session
413    /// is aborted).
414    pub fn with_parse_retries(mut self, max: u32) -> Self {
415        self.max_parse_retries = Some(max);
416        self
417    }
418
419    /// Set a per-tool execution timeout.
420    ///
421    /// When set, each tool execution is wrapped in `tokio::time::timeout`.
422    /// A timeout produces an error message that is fed back to the LLM
423    /// (the session continues).
424    pub fn with_tool_timeout(mut self, timeout_ms: u64) -> Self {
425        self.tool_timeout_ms = Some(timeout_ms);
426        self
427    }
428
429    /// Set a per-model API HTTP timeout.
430    ///
431    /// This is separate from [`with_tool_timeout`](Self::with_tool_timeout):
432    /// tool calls may need long-running process limits while model API calls
433    /// should use provider/network-specific deadlines.
434    pub fn with_llm_api_timeout(mut self, timeout_ms: u64) -> Self {
435        self.llm_api_timeout_ms = Some(timeout_ms);
436        self
437    }
438
439    /// Set the circuit-breaker threshold.
440    ///
441    /// In non-streaming mode, the agent retries transient LLM API failures up
442    /// to this many times (with exponential backoff) before aborting.
443    /// Default: 3 attempts.
444    pub fn with_circuit_breaker(mut self, threshold: u32) -> Self {
445        self.circuit_breaker_threshold = Some(threshold);
446        self
447    }
448
449    /// Enable all resilience defaults with sensible values:
450    ///
451    /// - `max_parse_retries = 2`
452    /// - `tool_timeout_ms = 120_000` (2 minutes)
453    /// - `circuit_breaker_threshold = 3`
454    pub fn with_resilience_defaults(self) -> Self {
455        self.with_parse_retries(2)
456            .with_tool_timeout(120_000)
457            .with_circuit_breaker(3)
458    }
459
460    /// Provide a concrete [`BashSandbox`] implementation for this session.
461    ///
462    /// When set, `bash` tool commands are routed through the given sandbox
463    /// instead of `std::process::Command`. The host application is responsible
464    /// for constructing and lifecycle-managing the sandbox.
465    ///
466    /// [`BashSandbox`]: crate::sandbox::BashSandbox
467    pub fn with_sandbox_handle(mut self, handle: Arc<dyn crate::sandbox::BashSandbox>) -> Self {
468        self.sandbox_handle = Some(handle);
469        self
470    }
471
472    /// Provide a workspace backend for this session.
473    ///
474    /// Built-in tools keep their stable names and schemas, while their backing
475    /// implementation can target a DFS, browser workspace, remote runner, or
476    /// any other host-provided backend.
477    pub fn with_workspace_backend(
478        mut self,
479        services: Arc<crate::workspace::WorkspaceServices>,
480    ) -> Self {
481        self.workspace_services = Some(services);
482        self
483    }
484
485    /// Enable auto-compaction when context usage exceeds threshold.
486    ///
487    /// When enabled, the agent loop automatically prunes large tool outputs
488    /// and summarizes old messages when context usage exceeds the threshold.
489    pub fn with_auto_compact(mut self, enabled: bool) -> Self {
490        self.auto_compact = enabled;
491        self
492    }
493
494    /// Set the auto-compact threshold (0.0 - 1.0). Default: 0.80 (80%).
495    pub fn with_auto_compact_threshold(mut self, threshold: f32) -> Self {
496        self.auto_compact_threshold = Some(threshold.clamp(0.0, 1.0));
497        self
498    }
499
500    /// Enable or disable continuation injection (default: enabled).
501    ///
502    /// When enabled, the loop injects a continuation message when the LLM stops
503    /// calling tools before the task appears complete, nudging it to keep working.
504    pub fn with_continuation(mut self, enabled: bool) -> Self {
505        self.continuation_enabled = Some(enabled);
506        self
507    }
508
509    /// Set the maximum number of continuation injections per execution (default: 3).
510    pub fn with_max_continuation_turns(mut self, turns: u32) -> Self {
511        self.max_continuation_turns = Some(turns);
512        self
513    }
514
515    /// Set an MCP manager to connect to external MCP servers.
516    ///
517    /// All tools from connected servers will be available during execution
518    /// with names like `mcp__<server>__<tool>`.
519    pub fn with_mcp(mut self, manager: Arc<crate::mcp::manager::McpManager>) -> Self {
520        self.mcp_manager = Some(manager);
521        self
522    }
523
524    pub fn with_temperature(mut self, temperature: f32) -> Self {
525        self.temperature = Some(temperature);
526        self
527    }
528
529    pub fn with_thinking_budget(mut self, budget: usize) -> Self {
530        self.thinking_budget = Some(budget);
531        self
532    }
533
534    /// Override the maximum number of tool execution rounds for this session.
535    ///
536    /// Useful when binding a markdown-defined subagent to a session —
537    /// pass the agent definition's `max_steps` value here to enforce its step budget.
538    pub fn with_max_tool_rounds(mut self, rounds: usize) -> Self {
539        self.max_tool_rounds = Some(rounds);
540        self
541    }
542
543    /// Override the maximum number of sibling parallel branches for this session.
544    pub fn with_max_parallel_tasks(mut self, tasks: usize) -> Self {
545        self.max_parallel_tasks = Some(tasks.max(1));
546        self
547    }
548
549    /// Override automatic subagent delegation for this session.
550    pub fn with_auto_delegation(mut self, config: crate::config::AutoDelegationConfig) -> Self {
551        self.auto_delegation = Some(config);
552        self
553    }
554
555    /// Enable or disable automatic subagent delegation for this session.
556    pub fn with_auto_delegation_enabled(mut self, enabled: bool) -> Self {
557        let mut config = self.auto_delegation.take().unwrap_or_default();
558        config.enabled = enabled;
559        self.auto_delegation = Some(config);
560        self
561    }
562
563    /// Enable or disable model-visible manual child-agent tools for this session.
564    ///
565    /// When false, `task` and `parallel_task` are not registered in the session
566    /// tool surface. Worker agents remain registered for introspection and hosts
567    /// that manage them directly. This is for cost control or debugging; it is
568    /// not a security sandbox for the parent agent.
569    pub fn with_manual_delegation_enabled(mut self, enabled: bool) -> Self {
570        if let Some(config) = &mut self.auto_delegation {
571            config.allow_manual_delegation = enabled;
572        }
573        self.manual_delegation_enabled = Some(enabled);
574        self
575    }
576
577    /// Globally enable or disable automatic parallel child-agent fan-out.
578    ///
579    /// Manual `parallel_task` calls remain available when this is false.
580    pub fn with_auto_parallel_delegation(mut self, enabled: bool) -> Self {
581        if let Some(config) = &mut self.auto_delegation {
582            config.auto_parallel = enabled;
583        }
584        self.auto_parallel_delegation = Some(enabled);
585        self
586    }
587
588    /// Set slot-based system prompt customization for this session.
589    ///
590    /// Allows customizing role, guidelines, response style, and extra instructions
591    /// without overriding the core agentic capabilities.
592    pub fn with_prompt_slots(mut self, slots: SystemPromptSlots) -> Self {
593        self.prompt_slots = Some(slots);
594        self
595    }
596
597    /// Replace the built-in hook engine with an external hook executor.
598    ///
599    /// Use this to attach an AHP harness server (or any custom `HookExecutor`)
600    /// to the session. All lifecycle events will be forwarded to the executor
601    /// instead of the in-process `HookEngine`.
602    pub fn with_hook_executor(mut self, executor: Arc<dyn crate::hooks::HookExecutor>) -> Self {
603        self.hook_executor = Some(executor);
604        self
605    }
606}