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 manifest;
35pub mod migration;
36pub mod progress;
37pub mod query;
38pub mod retention;
39
40pub use error::SessionStoreError;
41pub use event_log::{
42 DEFAULT_MAX_EVENTS, EvictionSummaryHook, SessionEventLog, SessionManifest, TurnIndex, TurnIndexEntry,
43};
44pub use migration::{MigrationReport, migrate_legacy};
45pub use progress::{
46 GoalClassifierVerdict, GoalEvent, GoalHistoryEntry, GoalOrchestration, GoalPauseReason, GoalPhase, GoalStatus,
47 GoalTracker, Milestone, MilestoneStatus, ProgressLedger, load_progress, progress_path, save_progress,
48};
49pub use query::{FactRecord, MemorySearchResult, SessionSummary, query_facts, recent_sessions, search_memory};
50pub use retention::{RetentionPolicy, apply_retention, apply_retention_preserving, gc_legacy};
51
52use std::path::{Path, PathBuf};
53
54const SESSIONS_DIR: &str = ".vtcode/sessions";
56
57const DERIVED_DIR: &str = "derived";
59
60const SESSION_STORE_SCHEMA_VERSION: u32 = 1;
62
63#[must_use]
65pub(crate) fn sessions_root(workspace: &Path) -> PathBuf {
66 workspace.join(SESSIONS_DIR)
67}
68
69#[must_use]
71pub(crate) fn session_dir(workspace: &Path, session_id: &str) -> PathBuf {
72 sessions_root(workspace).join(sanitize_id(session_id))
73}
74
75#[must_use]
81pub fn session_directory(workspace: &Path, session_id: &str) -> PathBuf {
82 session_dir(workspace, session_id)
83}
84
85pub fn open(workspace: &Path, session_id: &str, max_events: usize) -> Result<SessionEventLog, SessionStoreError> {
92 SessionEventLog::open(workspace, session_id, max_events)
93}
94
95pub fn open_with_eviction_summary(
98 workspace: &Path,
99 session_id: &str,
100 max_events: usize,
101 eviction_summary_hook: EvictionSummaryHook,
102) -> Result<SessionEventLog, SessionStoreError> {
103 SessionEventLog::open_with_eviction_summary(workspace, session_id, max_events, eviction_summary_hook)
104}
105
106pub(crate) fn ensure_private_directory(path: &Path) -> Result<(), SessionStoreError> {
108 vtcode_commons::VtCodePaths::ensure_user_dir(path).map_err(|error| SessionStoreError::CreateDir {
109 path: path.to_path_buf(),
110 source: std::io::Error::other(error),
111 })?;
112
113 #[cfg(unix)]
114 {
115 use std::os::unix::fs::PermissionsExt;
116
117 std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))
118 .map_err(|error| SessionStoreError::io(path.to_path_buf(), error))?;
119 }
120
121 Ok(())
122}
123
124fn sanitize_id(id: &str) -> String {
126 let mut out = String::with_capacity(id.len());
127 for c in id.chars() {
128 if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' {
129 out.push(c);
130 } else {
131 out.push('_');
132 }
133 }
134 let out = out.trim_start_matches('.').to_string();
136 if out.is_empty() { "session".to_string() } else { out }
137}
138
139#[cfg(test)]
140mod tests;