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