Skip to main content

a3s_code_core/store/
session_data.rs

1use crate::llm::{Message, TokenUsage, ToolDefinition};
2use crate::planning::Task;
3use crate::prompts::PlanningMode;
4use crate::queue::SessionQueueConfig;
5use serde::{Deserialize, Serialize};
6
7// ============================================================================
8// Serializable Session Data
9// ============================================================================
10
11/// Session state persisted with saved sessions.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
13pub enum SessionState {
14    #[default]
15    Unknown = 0,
16    Active = 1,
17    Paused = 2,
18    Completed = 3,
19    Error = 4,
20}
21
22/// Context usage statistics persisted with saved sessions.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct ContextUsage {
25    pub used_tokens: usize,
26    pub max_tokens: usize,
27    pub percent: f32,
28    pub turns: usize,
29}
30
31impl Default for ContextUsage {
32    fn default() -> Self {
33        Self {
34            used_tokens: 0,
35            max_tokens: 200_000,
36            percent: 0.0,
37            turns: 0,
38        }
39    }
40}
41
42/// Default auto-compact threshold (80% of context window).
43pub const DEFAULT_AUTO_COMPACT_THRESHOLD: f32 = 0.80;
44
45pub(crate) fn default_auto_compact_threshold() -> f32 {
46    DEFAULT_AUTO_COMPACT_THRESHOLD
47}
48
49fn is_false(value: &bool) -> bool {
50    !*value
51}
52
53/// Serializable session configuration.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct SessionConfig {
56    pub name: String,
57    pub workspace: String,
58    pub system_prompt: Option<String>,
59    pub max_context_length: u32,
60    pub auto_compact: bool,
61    /// Context usage percentage threshold to trigger auto-compaction (0.0 - 1.0).
62    /// Only used when `auto_compact` is true. Default: 0.80 (80%).
63    #[serde(default = "default_auto_compact_threshold")]
64    pub auto_compact_threshold: f32,
65    /// Storage type for this session.
66    #[serde(default)]
67    pub storage_type: crate::config::StorageBackend,
68    /// Optional advanced queue configuration.
69    ///
70    /// Queue infrastructure is initialized only when this is set. Ordinary
71    /// sessions stay queue-free.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub queue_config: Option<SessionQueueConfig>,
74    /// Confirmation policy (optional, uses defaults if None).
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub confirmation_policy: Option<crate::hitl::ConfirmationPolicy>,
77    /// Permission policy (optional, uses defaults if None).
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub permission_policy: Option<crate::permissions::PermissionPolicy>,
80    /// Whether active skill `allowed-tools` restrict ordinary session tools.
81    #[serde(default, skip_serializing_if = "is_false")]
82    pub enforce_active_skill_tool_restrictions: bool,
83    /// Maximum sibling branches/tools to run concurrently.
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub max_parallel_tasks: Option<usize>,
86    /// Automatic subagent delegation settings.
87    #[serde(default, skip_serializing_if = "Option::is_none")]
88    pub auto_delegation: Option<crate::config::AutoDelegationConfig>,
89    /// Parent session ID (for delegated child sessions).
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub parent_id: Option<String>,
92    /// Security configuration (optional, enables security features).
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub security_config: Option<crate::security::SecurityConfig>,
95    /// Shared hook engine for lifecycle events.
96    #[serde(skip)]
97    pub hook_engine: Option<std::sync::Arc<dyn crate::hooks::HookExecutor>>,
98    /// Enable planning phase before execution.
99    #[serde(default)]
100    pub planning_mode: PlanningMode,
101    /// Enable goal tracking.
102    #[serde(default)]
103    pub goal_tracking: bool,
104}
105
106impl Default for SessionConfig {
107    fn default() -> Self {
108        Self {
109            name: String::new(),
110            workspace: String::new(),
111            system_prompt: None,
112            max_context_length: 0,
113            auto_compact: false,
114            auto_compact_threshold: DEFAULT_AUTO_COMPACT_THRESHOLD,
115            storage_type: crate::config::StorageBackend::default(),
116            queue_config: None,
117            confirmation_policy: None,
118            permission_policy: None,
119            enforce_active_skill_tool_restrictions: false,
120            max_parallel_tasks: None,
121            auto_delegation: None,
122            parent_id: None,
123            security_config: None,
124            hook_engine: None,
125            planning_mode: PlanningMode::default(),
126            goal_tracking: false,
127        }
128    }
129}
130
131/// Serializable session data for persistence
132///
133/// Contains only the fields that can be serialized.
134/// Non-serializable fields (event_tx, command_queue, etc.) are rebuilt on load.
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct SessionData {
137    /// Session ID
138    pub id: String,
139
140    /// Session configuration
141    pub config: SessionConfig,
142
143    /// Current state
144    pub state: SessionState,
145
146    /// Conversation history
147    pub messages: Vec<Message>,
148
149    /// Context usage statistics
150    pub context_usage: ContextUsage,
151
152    /// Total token usage
153    pub total_usage: TokenUsage,
154
155    /// Cumulative dollar cost for this session
156    #[serde(default)]
157    pub total_cost: f64,
158
159    /// Model name for cost calculation
160    #[serde(skip_serializing_if = "Option::is_none")]
161    pub model_name: Option<String>,
162
163    /// LLM cost records for this session
164    #[serde(default)]
165    pub cost_records: Vec<crate::telemetry::LlmCostRecord>,
166
167    /// Tool definitions (names only, rebuilt from executor on load)
168    pub tool_names: Vec<String>,
169
170    /// Whether thinking mode is enabled
171    pub thinking_enabled: bool,
172
173    /// Thinking budget if set
174    pub thinking_budget: Option<usize>,
175
176    /// Creation timestamp (Unix epoch seconds)
177    pub created_at: i64,
178
179    /// Last update timestamp (Unix epoch seconds)
180    pub updated_at: i64,
181
182    /// LLM configuration for per-session client (if set)
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub llm_config: Option<LlmConfigData>,
185
186    /// Task list for tracking
187    #[serde(default, alias = "todos")]
188    pub tasks: Vec<Task>,
189
190    /// Parent session ID (for delegated child sessions)
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub parent_id: Option<String>,
193
194    /// Multi-tenant identifier. The framework only transports this string;
195    /// the host decides what "tenant" means and how to
196    /// aggregate/bill on it.
197    #[serde(default, skip_serializing_if = "Option::is_none")]
198    pub tenant_id: Option<String>,
199
200    /// Identity of the principal that triggered this session (user id,
201    /// service account, etc). Framework treats as opaque; emitted to
202    /// hooks/traces for accounting and audit.
203    #[serde(default, skip_serializing_if = "Option::is_none")]
204    pub principal: Option<String>,
205
206    /// Logical identifier of the agent template / definition the session
207    /// was instantiated from. Lets the host aggregate sessions by
208    /// "which agent recipe" independent of the concrete session id.
209    #[serde(default, skip_serializing_if = "Option::is_none")]
210    pub agent_template_id: Option<String>,
211
212    /// Distributed-trace correlation id. Propagated through hooks/traces
213    /// so a session's events can be joined with upstream/downstream work
214    /// in the host's observability pipeline.
215    #[serde(default, skip_serializing_if = "Option::is_none")]
216    pub correlation_id: Option<String>,
217}
218
219/// Serializable LLM configuration
220#[derive(Debug, Clone, Serialize, Deserialize)]
221pub struct LlmConfigData {
222    pub provider: String,
223    pub model: String,
224    /// API key is NOT stored - must be provided on session resume
225    #[serde(skip_serializing, default)]
226    pub api_key: Option<String>,
227    pub base_url: Option<String>,
228}
229
230impl SessionData {
231    /// Extract tool names from definitions
232    pub fn tool_names_from_definitions(tools: &[ToolDefinition]) -> Vec<String> {
233        tools.iter().map(|t| t.name.clone()).collect()
234    }
235}