Skip to main content

vtcode_memory/
error.rs

1//! Error type for the session store.
2
3use std::path::PathBuf;
4
5/// Errors produced by the session store.
6#[derive(Debug, thiserror::Error)]
7pub enum SessionStoreError {
8    /// IO error while reading or writing a session artifact.
9    #[error("session store IO error at {path}: {source}")]
10    Io {
11        /// Path that triggered the error.
12        path: PathBuf,
13        /// Underlying IO error.
14        source: std::io::Error,
15    },
16
17    /// A JSON (de)serialization error.
18    #[error("session store serialization error: {0}")]
19    Json(#[from] serde_json::Error),
20
21    /// The requested turn does not exist in the index.
22    #[error("turn {turn} not found in session {session}")]
23    TurnNotFound {
24        /// Session id.
25        session: String,
26        /// Missing turn number.
27        turn: u64,
28    },
29
30    /// The store directory could not be created.
31    #[error("failed to create session directory {path}: {source}")]
32    CreateDir {
33        /// Path that could not be created.
34        path: PathBuf,
35        /// Underlying IO error.
36        source: std::io::Error,
37    },
38
39    /// An audit pack is malformed or unsafe (bad schema, path traversal).
40    #[error("invalid audit pack: {0}")]
41    InvalidPack(String),
42
43    /// The session is already open for writing in another process.
44    #[error(
45        "session {session} is already open in another process; continue there or fork it with vtcode --fork-session {session}"
46    )]
47    SessionBusy {
48        /// Session id.
49        session: String,
50    },
51
52    /// A mailbox message failed validation.
53    #[error("invalid mail: {0}")]
54    InvalidMail(String),
55
56    /// The target session does not exist or is not live.
57    #[error("session {session} is not live; messages go to running sessions only")]
58    SessionNotLive {
59        /// Session id.
60        session: String,
61    },
62
63    /// The target inbox is full.
64    #[error("session {session} has too many unread messages; it must drain before accepting more")]
65    MailboxFull {
66        /// Session id.
67        session: String,
68    },
69}
70
71impl SessionStoreError {
72    /// Convenience constructor for an IO error at a path.
73    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
74        Self::Io { path: path.into(), source }
75    }
76
77    /// Convenience constructor for a mailbox validation failure.
78    pub(crate) fn invalid_mail(reason: impl Into<String>) -> Self {
79        Self::InvalidMail(reason.into())
80    }
81
82    /// Convenience constructor for a message to a non-live session.
83    pub(crate) fn session_not_live(session: impl Into<String>) -> Self {
84        Self::SessionNotLive { session: session.into() }
85    }
86
87    /// Convenience constructor for a send to a full inbox.
88    pub(crate) fn mailbox_full(session: impl Into<String>) -> Self {
89        Self::MailboxFull { session: session.into() }
90    }
91}