Skip to main content

vtcode_core/utils/
session_archive.rs

1mod archive;
2mod identifier;
3mod message;
4mod metadata;
5mod query;
6mod retention;
7mod search;
8mod settings;
9mod shrink;
10mod snapshot;
11mod transcript;
12
13/// Instruction supplied to the model when tool output is collapsed in the UI.
14pub const COLLAPSED_TOOL_OUTPUT_NOTICE: &str = "Only you see that command's output — the user's terminal shows at most a few lines of it, and routine tool failures stay hidden entirely. If the user needs to read any of it, put it in your reply; never reference hidden details as if the user saw them.";
15
16/// Identify persisted model-only context that should not appear in a resumed transcript.
17pub fn is_internal_session_context(
18    role: crate::llm::provider::MessageRole,
19    text: &str,
20    clear_at: Option<crate::llm::provider::MessageClearAt>,
21) -> bool {
22    role == crate::llm::provider::MessageRole::System
23        && (clear_at.is_some()
24            || text.starts_with(crate::prompts::FEW_SHOT_SECTION_HEADER)
25            || text.starts_with("## Active Editor Context")
26            || text == COLLAPSED_TOOL_OUTPUT_NOTICE)
27}
28
29pub use self::archive::SessionArchive;
30#[cfg(test)]
31pub(crate) use self::identifier::generate_unique_archive_path;
32pub use self::identifier::{generate_session_archive_identifier, reserve_session_archive_identifier};
33pub(crate) use self::query::resolve_sessions_dir_for_archive_writes;
34pub use self::query::{
35    delete_session_archive, find_session_by_id_or_name, find_session_by_identifier,
36    list_recent_session_summaries_in_workspace, list_recent_sessions, list_recent_sessions_in_workspace, sessions_dir,
37};
38#[cfg(test)]
39pub(crate) use self::retention::{SessionRetentionLimits, apply_session_retention_with_limits};
40#[cfg(test)]
41pub(crate) use self::shrink::{compact_snapshot_to_max_bytes, truncate_preview};
42#[cfg(test)]
43pub(crate) use self::transcript::clean_transcript_lines;
44
45pub use self::message::SessionMessage;
46pub use self::metadata::{
47    SessionArchiveMetadata, SessionContinuationExhaustionReason, SessionContinuationMetadata,
48    SessionContinuationRecommendedAction, SessionForkMode,
49};
50
51pub use self::search::{SearchResult, search_sessions};
52pub use self::settings::{
53    SESSION_DIR_ENV, SESSION_MAX_AGE_DAYS_ENV, SESSION_MAX_FILES_ENV, SESSION_MAX_SIZE_MB_ENV,
54    apply_session_history_config_from_vtcode, history_persistence_enabled,
55};
56#[cfg(test)]
57pub(crate) use self::settings::{
58    SessionHistorySettings, clear_sessions_dir_override_for_tests, clear_test_env_override,
59    override_sessions_dir_for_tests, session_history_settings, set_test_env_override_path,
60};
61pub use self::snapshot::{
62    SessionArchiveSummary, SessionListing, SessionProgress, SessionProgressArgs, SessionProgressPersistenceStatus,
63    SessionSnapshot, VerifiedSessionArchiveIdentifier, session_listing_matches_workspace, session_workspace_path,
64};
65
66const SESSION_FILE_PREFIX: &str = "session";
67const SESSION_FILE_EXTENSION: &str = "json";
68const DEFAULT_SESSION_MAX_FILES: usize = 100;
69const DEFAULT_SESSION_MAX_AGE_DAYS: u64 = 14;
70const DEFAULT_SESSION_MAX_SIZE_MB: u64 = 100;
71const BYTES_PER_MB: u64 = 1024 * 1024;
72
73#[cfg(test)]
74mod metadata_compat_tests {
75    use super::{SessionArchive, SessionArchiveMetadata};
76
77    #[test]
78    fn primary_agent_metadata_is_backward_compatible() {
79        let metadata = serde_json::from_value::<SessionArchiveMetadata>(serde_json::json!({
80            "workspace_label": "ws",
81            "workspace_path": "/tmp/ws",
82            "model": "gpt-5.6-sol",
83            "provider": "openai",
84            "theme": "test",
85            "reasoning_effort": "low"
86        }))
87        .expect("deserialize metadata without primary_agent");
88
89        assert_eq!(metadata.primary_agent, None);
90
91        let value = serde_json::to_value(&metadata).expect("serialize metadata");
92        assert!(value.get("primary_agent").is_none());
93    }
94
95    #[test]
96    fn set_primary_agent_records_mode_on_archive() {
97        let metadata = SessionArchiveMetadata::new("ws", "/tmp/ws", "gpt-5.6-sol", "openai", "test", "low");
98        let mut archive = tokio::runtime::Runtime::new()
99            .expect("runtime")
100            .block_on(SessionArchive::new_with_identifier(metadata, "ws-0000".to_string()))
101            .expect("create archive");
102        assert_eq!(archive.metadata().primary_agent, None);
103
104        archive.set_primary_agent("plan");
105        assert_eq!(archive.metadata().primary_agent.as_deref(), Some("plan"));
106    }
107}
108
109#[cfg(test)]
110#[path = "session_archive_tests/mod.rs"]
111mod tests;