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