Skip to main content

vtcode_desktop_protocol/
session_lock.rs

1//! Single live-session ownership: message + detection shared by service and app.
2//!
3//! The canonical event log is single-writer (exclusive flock on `session.lock`).
4//! Live sessions (CLI interactive, desktop service) open it with
5//! `open_exclusive` and fail with `SessionBusy` when another process holds it.
6//! Background readers (retention, query, inspector history) intentionally use
7//! shared `open` and stay available. This module is the one place that names
8//! the busy text so the service message and the desktop UI detection cannot drift.
9
10/// Stable substring present in every session-busy message.
11pub const SESSION_BUSY_MARKER: &str = "already open in another process";
12
13/// Full actionable busy message for `session_id`.
14#[must_use]
15pub fn session_busy_message(session_id: &str) -> String {
16    format!(
17        "session {session_id} is already open in another process; close it there and reload, or open a copy with vtcode --fork-session {session_id}"
18    )
19}
20
21/// Whether a backend error message reports session-busy contention.
22#[must_use]
23pub fn is_session_busy_message(message: &str) -> bool {
24    message.contains(SESSION_BUSY_MARKER)
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn busy_message_names_contention_session_and_fork_remedy() {
33        let message = session_busy_message("thread-9");
34        assert!(message.contains(SESSION_BUSY_MARKER));
35        assert!(message.contains("thread-9"));
36        assert!(message.contains("close it there and reload"));
37        assert!(message.contains("vtcode --fork-session thread-9"));
38    }
39
40    #[test]
41    fn busy_detection_matches_any_message_carrying_the_marker() {
42        assert!(is_session_busy_message(&session_busy_message("a")));
43        // Substring semantics, not a copy of another crate's wording: any
44        // present or future busy text carrying the marker must match.
45        assert!(is_session_busy_message(&format!("prefix {} suffix", SESSION_BUSY_MARKER)));
46        assert!(!is_session_busy_message("saved session was not found"));
47        assert!(!is_session_busy_message(""));
48    }
49}