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 manifest;
36pub mod migration;
37pub mod pack;
39pub mod progress;
40pub mod query;
41pub mod retention;
42
43pub use error::SessionStoreError;
44pub use event_log::{
45 DEFAULT_MAX_EVENTS, EvictionSummaryHook, SessionEventLog, SessionManifest, TurnIndex, TurnIndexEntry,
46};
47pub use migration::{MigrationReport, migrate_legacy};
48pub use pack::{
49 AUDIT_PACK_SCHEMA_VERSION, AuditPackEntry, AuditVerification, SessionAuditPack, audit_pack_path, create_audit_pack,
50 read_audit_pack, verify_audit_pack, write_audit_pack,
51};
52pub use progress::{
53 GoalClassifierVerdict, GoalEvent, GoalHistoryEntry, GoalOrchestration, GoalPauseReason, GoalPhase, GoalStatus,
54 GoalTracker, Milestone, MilestoneStatus, ProgressLedger, load_progress, progress_path, save_progress,
55};
56pub use query::{
57 FactRecord, MemorySearchResult, SessionMemoryView, SessionSummary, query_facts, recent_sessions, search_memory,
58 session_memory_facts, write_session_memory_view,
59};
60pub use retention::{
61 RETENTION_PIN_FILE, RetentionPolicy, apply_retention, apply_retention_preserving, evict_zero_turn_completed_store,
62 gc_legacy, mark_abandoned_active_sessions, pin_session_retention, retention_pinned_session_ids,
63 session_retention_pinned, unpin_session_retention,
64};
65
66use std::path::{Path, PathBuf};
67
68const SESSIONS_DIR: &str = ".vtcode/sessions";
70
71const DERIVED_DIR: &str = "derived";
73
74const SESSION_STORE_SCHEMA_VERSION: u32 = 1;
76
77#[must_use]
79pub(crate) fn sessions_root(workspace: &Path) -> PathBuf {
80 workspace.join(SESSIONS_DIR)
81}
82
83#[must_use]
85pub(crate) fn session_dir(workspace: &Path, session_id: &str) -> PathBuf {
86 sessions_root(workspace).join(sanitize_id(session_id))
87}
88
89#[must_use]
95pub fn session_directory(workspace: &Path, session_id: &str) -> PathBuf {
96 session_dir(workspace, session_id)
97}
98
99pub fn open(workspace: &Path, session_id: &str, max_events: usize) -> Result<SessionEventLog, SessionStoreError> {
106 SessionEventLog::open(workspace, session_id, max_events)
107}
108
109pub fn open_with_eviction_summary(
112 workspace: &Path,
113 session_id: &str,
114 max_events: usize,
115 eviction_summary_hook: EvictionSummaryHook,
116) -> Result<SessionEventLog, SessionStoreError> {
117 SessionEventLog::open_with_eviction_summary(workspace, session_id, max_events, eviction_summary_hook)
118}
119
120pub(crate) fn ensure_private_directory(path: &Path) -> Result<(), SessionStoreError> {
122 vtcode_commons::VtCodePaths::ensure_user_dir(path).map_err(|error| SessionStoreError::CreateDir {
123 path: path.to_path_buf(),
124 source: std::io::Error::other(error),
125 })?;
126
127 #[cfg(unix)]
128 {
129 use std::os::unix::fs::PermissionsExt;
130
131 std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))
132 .map_err(|error| SessionStoreError::io(path.to_path_buf(), error))?;
133 }
134
135 Ok(())
136}
137
138fn sanitize_id(id: &str) -> String {
140 let mut out = String::with_capacity(id.len());
141 for c in id.chars() {
142 if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' {
143 out.push(c);
144 } else {
145 out.push('_');
146 }
147 }
148 let out = out.trim_start_matches('.').to_string();
150 if out.is_empty() { "session".to_string() } else { out }
151}
152
153#[cfg(test)]
154mod tests;