1#![allow(
2 dead_code,
3 unused_imports,
4 reason = "Intentional compatibility, platform, or test-only suppression."
5)]
6#![expect(
7 unused_results,
8 clippy::let_underscore_must_use,
9 clippy::cast_possible_truncation,
10 clippy::cast_possible_wrap,
11 clippy::string_slice,
12 reason = "The memory store uses compact persisted counters, bounded timestamp conversions, and side-effect-only index maintenance."
13)]
14pub mod error;
32pub mod event_log;
33pub mod explanation;
34pub mod mailbox;
36pub mod manifest;
38pub mod matrix;
39pub mod migration;
40pub mod pack;
42pub mod progress;
43pub mod query;
44pub mod retention;
45mod session_guard;
46pub mod snapshot;
47
48pub use error::SessionStoreError;
49pub use event_log::{
50 DEFAULT_MAX_EVENTS, EvictionSummaryHook, SessionEventLog, SessionManifest, TurnIndex, TurnIndexEntry,
51};
52pub use mailbox::{
53 DEFAULT_TTL_SECS, InboxDrain, MAILBOX_DIR, MAILBOX_SCHEMA_VERSION, MAX_INBOX_DEPTH, MAX_MAIL_BYTES,
54 MAX_OPTION_DESCRIPTION_CHARS, MAX_OPTION_LABEL_CHARS, MAX_OPTIONS, MAX_QUESTION_CHARS, MAX_QUESTION_ID_CHARS,
55 MAX_QUESTIONS, MAX_TEXT_CHARS, MAX_TTL_SECS, MIN_TTL_SECS, MailKind, MailMessage, MailOption, MailQuestion,
56 MailReceipt, NewMail, ack_mail, drain_inbox, find_mail, read_mail, send_mail,
57};
58pub use migration::{MigrationReport, migrate_legacy};
59pub use pack::{
60 AUDIT_PACK_SCHEMA_VERSION, AuditPackEntry, AuditVerification, SessionAuditPack, audit_pack_path, create_audit_pack,
61 read_audit_pack, verify_audit_pack, write_audit_pack,
62};
63pub use progress::{
64 GoalClassifierVerdict, GoalEvent, GoalHistoryEntry, GoalOrchestration, GoalPauseReason, GoalPhase, GoalStatus,
65 GoalTracker, Milestone, MilestoneStatus, ProgressLedger, load_progress, progress_path, save_progress,
66};
67pub use query::{
68 FactRecord, MemorySearchResult, SessionMemoryView, SessionSummary, live_sessions, query_facts, recent_sessions,
69 search_memory, session_memory_facts, write_session_memory_view,
70};
71#[allow(deprecated, reason = "re-export keeps the deprecated API reachable until removal")]
72pub use retention::gc_legacy;
73pub use retention::{
74 RETENTION_PIN_FILE, RetentionPolicy, apply_retention, apply_retention_preserving, evict_zero_turn_completed_store,
75 mark_abandoned_active_sessions, pin_session_retention, retention_pinned_session_ids, session_retention_pinned,
76 unpin_session_retention,
77};
78
79use std::path::{Path, PathBuf};
80
81const SESSIONS_DIR: &str = ".vtcode/sessions";
83
84const DERIVED_DIR: &str = "derived";
86
87const SESSION_STORE_SCHEMA_VERSION: u32 = 1;
89
90#[must_use]
92pub(crate) fn sessions_root(workspace: &Path) -> PathBuf {
93 workspace.join(SESSIONS_DIR)
94}
95
96#[must_use]
98pub(crate) fn session_dir(workspace: &Path, session_id: &str) -> PathBuf {
99 sessions_root(workspace).join(sanitize_id(session_id))
100}
101
102#[must_use]
108pub fn session_directory(workspace: &Path, session_id: &str) -> PathBuf {
109 session_dir(workspace, session_id)
110}
111
112pub fn open(workspace: &Path, session_id: &str, max_events: usize) -> Result<SessionEventLog, SessionStoreError> {
119 SessionEventLog::open(workspace, session_id, max_events)
120}
121
122pub fn open_exclusive(
129 workspace: &Path,
130 session_id: &str,
131 max_events: usize,
132) -> Result<SessionEventLog, SessionStoreError> {
133 SessionEventLog::open_exclusive(workspace, session_id, max_events)
134}
135
136pub fn open_with_eviction_summary(
139 workspace: &Path,
140 session_id: &str,
141 max_events: usize,
142 eviction_summary_hook: EvictionSummaryHook,
143) -> Result<SessionEventLog, SessionStoreError> {
144 SessionEventLog::open_with_eviction_summary(workspace, session_id, max_events, eviction_summary_hook)
145}
146
147pub(crate) fn ensure_private_directory(path: &Path) -> Result<(), SessionStoreError> {
149 vtcode_commons::VtCodePaths::ensure_user_dir(path).map_err(|error| SessionStoreError::CreateDir {
150 path: path.to_path_buf(),
151 source: std::io::Error::other(error),
152 })?;
153
154 #[cfg(unix)]
155 {
156 use std::os::unix::fs::PermissionsExt;
157
158 std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))
159 .map_err(|error| SessionStoreError::io(path.to_path_buf(), error))?;
160 }
161
162 Ok(())
163}
164
165fn sanitize_id(id: &str) -> String {
167 let mut out = String::with_capacity(id.len());
168 for c in id.chars() {
169 if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' {
170 out.push(c);
171 } else {
172 out.push('_');
173 }
174 }
175 let out = out.trim_start_matches('.').to_string();
177 if out.is_empty() { "session".to_string() } else { out }
178}
179
180#[cfg(test)]
181mod tests;