Skip to main content

wire/
session.rs

1//! Multi-session wire on one machine (v0.5.16).
2//!
3//! Problem: multiple Claude Code (or any agent harness) sessions on the
4//! same machine share a single `WIRE_HOME`, which means they share the
5//! same DID, same relay slot, same inbox JSONL, and same daemon. Peers
6//! have no way to address a specific session, and the operator can't
7//! tell which session sent what.
8//!
9//! Solution: a `wire session` subcommand that bootstraps **isolated**
10//! per-session `WIRE_HOME` trees. Each session gets its own identity,
11//! handle, relay slot, daemon, and inbox/outbox. Sessions pair with each
12//! other through the public relay (`wireup.net`) like any other peer —
13//! no protocol changes. The bilateral-pair gate from v0.5.14 still
14//! applies in both directions.
15//!
16//! Storage layout:
17//!
18//! ```text
19//! ~/.local/state/wire/sessions/
20//!   registry.json                — cwd → session_name map
21//!   <session-name>/               — full WIRE_HOME tree per session
22//!     config/wire/...
23//!     state/wire/...
24//! ```
25//!
26//! Naming: derived from `basename(cwd)` so re-opening the same project
27//! reuses the same session identity. Collisions across two different
28//! paths with the same basename get a 4-char SHA-256 path-hash suffix.
29
30use anyhow::{Context, Result, anyhow};
31use serde::{Deserialize, Serialize};
32use serde_json::Value;
33use sha2::{Digest, Sha256};
34use std::collections::HashMap;
35use std::path::{Path, PathBuf};
36
37use crate::endpoints::{Endpoint, EndpointScope, self_endpoints};
38
39/// Root directory under which all session WIRE_HOMEs live.
40///
41/// Honors `WIRE_HOME` for testing (sessions root becomes
42/// `$WIRE_HOME/sessions/`); otherwise:
43///   - Linux: `$XDG_STATE_HOME/wire/sessions/` (typically
44///     `~/.local/state/wire/sessions/`).
45///   - macOS / other Unix without XDG: falls back to
46///     `dirs::data_local_dir() / wire / sessions /`, which on macOS is
47///     `~/Library/Application Support/wire/sessions/`. This mirrors
48///     `config::state_dir`'s fallback so the two surfaces resolve to
49///     compatible roots on every platform.
50pub fn sessions_root() -> Result<PathBuf> {
51    if let Ok(home_str) = std::env::var("WIRE_HOME") {
52        let home = PathBuf::from(&home_str);
53        let direct = home.join("sessions");
54        if direct.exists() {
55            return Ok(direct);
56        }
57        // v0.6.4: inside-session fallback. When WIRE_HOME is set by the
58        // MCP auto-detect or `wire session env`, it points at one
59        // session's home (`<root>/sessions/<name>`) — *not* the root
60        // holding every session. Without this fallback, `wire mesh
61        // status` / `mesh role list` / `mesh broadcast` invoked from
62        // inside a session see zero sister sessions even though the
63        // operator can plainly see them with `wire session list`.
64        //
65        // The check is tight on purpose: only short-circuit when the
66        // immediate parent dir is named `sessions`. Anything else (a
67        // plain test WIRE_HOME, a custom location) keeps the v0.6.3
68        // behavior of returning `<WIRE_HOME>/sessions/` so the caller
69        // can populate it.
70        if let Some(parent) = home.parent()
71            && parent.file_name().and_then(|s| s.to_str()) == Some("sessions")
72        {
73            return Ok(parent.to_path_buf());
74        }
75        return Ok(direct);
76    }
77    let state = dirs::state_dir()
78        .or_else(dirs::data_local_dir)
79        .ok_or_else(|| {
80            anyhow!(
81                "could not resolve XDG_STATE_HOME (or platform-equivalent local data dir) — \
82                 set WIRE_HOME or run on a platform with `dirs` support"
83            )
84        })?;
85    Ok(state.join("wire").join("sessions"))
86}
87
88/// Full filesystem path for the named session's WIRE_HOME root.
89/// Inside this dir the standard wire layout applies: `config/wire/...`
90/// and `state/wire/...`.
91pub fn session_dir(name: &str) -> Result<PathBuf> {
92    Ok(sessions_root()?.join(sanitize_name(name)))
93}
94
95/// Registry tracks `cwd → session_name` so repeated `wire session new`
96/// from the same project reuses the same identity instead of creating
97/// a fresh one each time. Lives at `<sessions_root>/registry.json`.
98pub fn registry_path() -> Result<PathBuf> {
99    Ok(sessions_root()?.join("registry.json"))
100}
101
102#[derive(Debug, Clone, Default, Serialize, Deserialize)]
103pub struct SessionRegistry {
104    /// `cwd_absolute_path → session_name`. Absent if cwd has not been
105    /// associated with a session yet.
106    #[serde(default)]
107    pub by_cwd: HashMap<String, String>,
108}
109
110pub fn read_registry() -> Result<SessionRegistry> {
111    let path = registry_path()?;
112    if !path.exists() {
113        return Ok(SessionRegistry::default());
114    }
115    let bytes =
116        std::fs::read(&path).with_context(|| format!("reading session registry {path:?}"))?;
117    serde_json::from_slice(&bytes).with_context(|| format!("parsing session registry {path:?}"))
118}
119
120pub fn write_registry(reg: &SessionRegistry) -> Result<()> {
121    let path = registry_path()?;
122    if let Some(parent) = path.parent() {
123        std::fs::create_dir_all(parent).with_context(|| format!("creating {parent:?}"))?;
124    }
125    let body = serde_json::to_vec_pretty(reg)?;
126    std::fs::write(&path, body).with_context(|| format!("writing session registry {path:?}"))?;
127    Ok(())
128}
129
130/// Sanitize an arbitrary string to a session-name-safe form: lowercase
131/// ASCII alphanumeric + `-` + `_`, replace other chars with `-`,
132/// dedupe consecutive dashes, trim leading/trailing dashes, max 32 chars.
133pub fn sanitize_name(raw: &str) -> String {
134    let mut out = String::with_capacity(raw.len());
135    let mut prev_dash = false;
136    for c in raw.chars() {
137        let ok = c.is_ascii_alphanumeric() || c == '-' || c == '_';
138        let ch = if ok { c.to_ascii_lowercase() } else { '-' };
139        if ch == '-' {
140            if !prev_dash && !out.is_empty() {
141                out.push('-');
142            }
143            prev_dash = true;
144        } else {
145            out.push(ch);
146            prev_dash = false;
147        }
148    }
149    let trimmed = out.trim_matches('-').to_string();
150    if trimmed.is_empty() {
151        return "wire-session".to_string();
152    }
153    if trimmed.len() > 32 {
154        return trimmed[..32].trim_end_matches('-').to_string();
155    }
156    trimmed
157}
158
159/// Short hash suffix derived from the full absolute path of the cwd.
160/// Used to disambiguate two different projects whose basenames collide
161/// (e.g. `~/Source/wire` and `~/Archive/wire`).
162fn path_hash_suffix(cwd: &Path) -> String {
163    let bytes = cwd.as_os_str().to_string_lossy().into_owned();
164    let mut h = Sha256::new();
165    h.update(bytes.as_bytes());
166    let digest = h.finalize();
167    hex::encode(&digest[..2]) // 4 hex chars
168}
169
170/// Derive a stable session name for the given cwd. Resolution order:
171///
172/// 1. If the registry already maps this cwd → name, return that name.
173/// 2. Else: candidate = sanitize(basename(cwd)). If the candidate is
174///    already mapped to a DIFFERENT cwd in the registry, append a
175///    4-char path-hash suffix to avoid collision.
176/// 3. If still a collision: append a numeric suffix `-2`, `-3`, ...
177///    until unique.
178pub fn derive_name_from_cwd(cwd: &Path, registry: &SessionRegistry) -> String {
179    let cwd_key = cwd.to_string_lossy().into_owned();
180    if let Some(existing) = registry.by_cwd.get(&cwd_key) {
181        return existing.clone();
182    }
183    let base = cwd
184        .file_name()
185        .and_then(|s| s.to_str())
186        .map(sanitize_name)
187        .unwrap_or_else(|| "wire-session".to_string());
188    let occupied: std::collections::HashSet<String> = registry.by_cwd.values().cloned().collect();
189    if !occupied.contains(&base) {
190        return base;
191    }
192    let with_hash = format!("{}-{}", base, path_hash_suffix(cwd));
193    if !occupied.contains(&with_hash) {
194        return with_hash;
195    }
196    // Highly unlikely (would require a SHA-256 prefix collision plus an
197    // existing entry to claim it). Numeric tiebreaker as final fallback.
198    for n in 2..1000 {
199        let candidate = format!("{base}-{n}");
200        if !occupied.contains(&candidate) {
201            return candidate;
202        }
203    }
204    // Pathological fallback — every numbered slot is taken.
205    format!("{base}-{}-overflow", path_hash_suffix(cwd))
206}
207
208/// Summary of one on-disk session for `wire session list`.
209#[derive(Debug, Clone, Serialize)]
210pub struct SessionInfo {
211    pub name: String,
212    /// First cwd associated with this session in the registry. `None`
213    /// if the session was created without registry tracking (manual
214    /// `wire session new <name>`).
215    pub cwd: Option<String>,
216    pub home_dir: PathBuf,
217    pub did: Option<String>,
218    pub handle: Option<String>,
219    /// True if a `daemon.pid` file exists AND the recorded PID is
220    /// actually a live process (best-effort, not POSIX-portable but
221    /// matches the existing `wire status` / `wire doctor` checks).
222    pub daemon_running: bool,
223}
224
225/// Enumerate every on-disk session by reading `sessions_root()`. Cross-
226/// references the registry so each entry's `cwd` is filled in when known.
227pub fn list_sessions() -> Result<Vec<SessionInfo>> {
228    let root = sessions_root()?;
229    if !root.exists() {
230        return Ok(Vec::new());
231    }
232    let registry = read_registry().unwrap_or_default();
233    // Reverse lookup: name → cwd. Used to annotate each SessionInfo.
234    let mut name_to_cwd: HashMap<String, String> = HashMap::new();
235    for (cwd, name) in &registry.by_cwd {
236        name_to_cwd.insert(name.clone(), cwd.clone());
237    }
238
239    let mut out = Vec::new();
240    for entry in std::fs::read_dir(&root)?.flatten() {
241        let path = entry.path();
242        if !path.is_dir() {
243            continue;
244        }
245        let name = match path.file_name().and_then(|s| s.to_str()) {
246            Some(s) => s.to_string(),
247            None => continue,
248        };
249        // Skip the registry sidecar.
250        if name == "registry.json" {
251            continue;
252        }
253        let card_path = path.join("config").join("wire").join("agent-card.json");
254        let (did, handle) = read_card_identity(&card_path);
255        let daemon_running = check_daemon_live(&path);
256        out.push(SessionInfo {
257            name: name.clone(),
258            cwd: name_to_cwd.get(&name).cloned(),
259            home_dir: path,
260            did,
261            handle,
262            daemon_running,
263        });
264    }
265    out.sort_by(|a, b| a.name.cmp(&b.name));
266    Ok(out)
267}
268
269fn read_card_identity(card_path: &Path) -> (Option<String>, Option<String>) {
270    let bytes = match std::fs::read(card_path) {
271        Ok(b) => b,
272        Err(_) => return (None, None),
273    };
274    let v: serde_json::Value = match serde_json::from_slice(&bytes) {
275        Ok(v) => v,
276        Err(_) => return (None, None),
277    };
278    let did = v.get("did").and_then(|x| x.as_str()).map(str::to_string);
279    let handle = v
280        .get("handle")
281        .and_then(|x| x.as_str())
282        .map(str::to_string)
283        .or_else(|| {
284            did.as_ref()
285                .map(|d| crate::agent_card::display_handle_from_did(d).to_string())
286        });
287    (did, handle)
288}
289
290fn check_daemon_live(session_home: &Path) -> bool {
291    // Pidfile lives at <session_home>/state/wire/daemon.pid. Use the
292    // existing ensure_up reader by temporarily pointing at the path; we
293    // can't change env mid-process race-free, so re-implement the pid
294    // extraction directly here from the JSON structure.
295    let pidfile = session_home.join("state").join("wire").join("daemon.pid");
296    let bytes = match std::fs::read(&pidfile) {
297        Ok(b) => b,
298        Err(_) => return false,
299    };
300    // Try the structured form first.
301    let pid_opt: Option<u32> = if let Ok(v) = serde_json::from_slice::<serde_json::Value>(&bytes) {
302        v.get("pid").and_then(|p| p.as_u64()).map(|p| p as u32)
303    } else {
304        // Legacy integer form.
305        String::from_utf8_lossy(&bytes).trim().parse::<u32>().ok()
306    };
307    let pid = match pid_opt {
308        Some(p) => p,
309        None => return false,
310    };
311    is_process_live(pid)
312}
313
314fn is_process_live(pid: u32) -> bool {
315    #[cfg(target_os = "linux")]
316    {
317        std::path::Path::new(&format!("/proc/{pid}")).exists()
318    }
319    #[cfg(not(target_os = "linux"))]
320    {
321        std::process::Command::new("kill")
322            .args(["-0", &pid.to_string()])
323            .output()
324            .map(|o| o.status.success())
325            .unwrap_or(false)
326    }
327}
328
329/// Read a session's `relay.json` and return its `self.endpoints[]`
330/// array (v0.5.17 dual-slot). Empty Vec on any read/parse error — this
331/// is a best-effort discovery helper, not a verification tool. A pre-
332/// v0.5.17 session writes only the legacy flat fields; `self_endpoints`
333/// promotes those to a federation-only Endpoint, so the result is
334/// still meaningful for legacy sessions.
335///
336/// v0.5.20 BUG FIX: this used to join `relay-state.json`, which is
337/// not the canonical filename (`config::relay_state_path` returns
338/// `relay.json`). The mis-named read silently no-op'd and
339/// `list-local` always returned an empty `local` map as a result.
340/// Companion to the `cli.rs::try_allocate_local_slot` filename fix
341/// in the same release — that helper had the symmetric write-side
342/// bug, so the local endpoint never got persisted in the first place.
343pub fn read_session_endpoints(session_home: &Path) -> Vec<Endpoint> {
344    let path = session_home.join("config").join("wire").join("relay.json");
345    let bytes = match std::fs::read(&path) {
346        Ok(b) => b,
347        Err(_) => return Vec::new(),
348    };
349    let val: Value = match serde_json::from_slice(&bytes) {
350        Ok(v) => v,
351        Err(_) => return Vec::new(),
352    };
353    self_endpoints(&val)
354}
355
356/// Stripped view of a Local endpoint for tooling output. Drops
357/// `slot_token` because it is a bearer credential — exposing it
358/// through `wire session list-local --json` would risk accidental
359/// leak via logs, screenshots, or piped output. Routing code uses
360/// the full `Endpoint` from `relay.json` directly; this type
361/// is for human/JSON observation only.
362#[derive(Debug, Clone, Serialize)]
363pub struct LocalEndpointView {
364    pub relay_url: String,
365    pub slot_id: String,
366}
367
368/// One row of `wire session list-local` output: a session that has a
369/// Local-scope endpoint plus metadata to render it.
370#[derive(Debug, Clone, Serialize)]
371pub struct LocalSessionView {
372    pub name: String,
373    pub handle: Option<String>,
374    pub did: Option<String>,
375    pub cwd: Option<String>,
376    pub home_dir: PathBuf,
377    pub daemon_running: bool,
378    /// All Local-scope endpoints this session advertises (token redacted).
379    /// Most sessions have exactly one; multiple is permitted for multi-
380    /// relay setups.
381    pub local_endpoints: Vec<LocalEndpointView>,
382}
383
384/// Sessions with no Local endpoint — shown separately so the operator
385/// knows they exist but are federation-only.
386#[derive(Debug, Clone, Serialize)]
387pub struct FederationOnlySessionView {
388    pub name: String,
389    pub handle: Option<String>,
390    pub cwd: Option<String>,
391}
392
393/// Result shape for `wire session list-local`. `local` is grouped by
394/// the local-relay URL so output can render each cluster of mutually-
395/// reachable sister sessions together. `federation_only` lists the rest.
396#[derive(Debug, Clone, Serialize)]
397pub struct LocalSessionListing {
398    pub local: HashMap<String, Vec<LocalSessionView>>,
399    pub federation_only: Vec<FederationOnlySessionView>,
400}
401
402/// Build the listing for `wire session list-local` from current on-disk
403/// state. Read-only; no daemon contact, no relay probe.
404pub fn list_local_sessions() -> Result<LocalSessionListing> {
405    let sessions = list_sessions()?;
406    let mut local: HashMap<String, Vec<LocalSessionView>> = HashMap::new();
407    let mut federation_only: Vec<FederationOnlySessionView> = Vec::new();
408
409    for s in sessions {
410        let endpoints = read_session_endpoints(&s.home_dir);
411        let local_eps: Vec<Endpoint> = endpoints
412            .into_iter()
413            .filter(|e| matches!(e.scope, EndpointScope::Local))
414            .collect();
415        if local_eps.is_empty() {
416            federation_only.push(FederationOnlySessionView {
417                name: s.name.clone(),
418                handle: s.handle.clone(),
419                cwd: s.cwd.clone(),
420            });
421            continue;
422        }
423        // Redacted view: drop slot_token before exposing through CLI.
424        let redacted: Vec<LocalEndpointView> = local_eps
425            .iter()
426            .map(|e| LocalEndpointView {
427                relay_url: e.relay_url.clone(),
428                slot_id: e.slot_id.clone(),
429            })
430            .collect();
431        // Group by relay_url. A session with two Local endpoints (rare —
432        // would mean two loopback relays) appears under each.
433        for ep in &local_eps {
434            local
435                .entry(ep.relay_url.clone())
436                .or_default()
437                .push(LocalSessionView {
438                    name: s.name.clone(),
439                    handle: s.handle.clone(),
440                    did: s.did.clone(),
441                    cwd: s.cwd.clone(),
442                    home_dir: s.home_dir.clone(),
443                    daemon_running: s.daemon_running,
444                    local_endpoints: redacted.clone(),
445                });
446        }
447    }
448    // Sort each group by session name so output is deterministic.
449    for group in local.values_mut() {
450        group.sort_by(|a, b| a.name.cmp(&b.name));
451    }
452    federation_only.sort_by(|a, b| a.name.cmp(&b.name));
453    Ok(LocalSessionListing {
454        local,
455        federation_only,
456    })
457}
458
459/// v0.6.7: cwd → session WIRE_HOME lookup. Read-only.
460///
461/// When `WIRE_HOME` isn't set in env, look up `cwd` in the session
462/// registry. If a session is registered for this cwd AND its home
463/// directory still exists, return that home dir; otherwise None.
464///
465/// Used by both `wire mcp` (v0.6.1) and the CLI entry point (v0.6.7)
466/// so a `wire whoami` / `wire monitor` invocation from a project cwd
467/// adopts that project's session identity automatically, instead of
468/// silently falling back to the machine default. The CLI parity is
469/// load-bearing: without it, the user-visible identity diverges
470/// between MCP and the terminal, and monitors pull machine-wide
471/// inboxes when the operator expected a per-session view.
472pub fn detect_session_wire_home(cwd: &std::path::Path) -> Option<PathBuf> {
473    let registry = read_registry().ok()?;
474    let cwd_str = cwd.to_string_lossy().into_owned();
475    let session_name = registry.by_cwd.get(&cwd_str)?;
476    let session_home = session_dir(session_name).ok()?;
477    if !session_home.exists() {
478        return None;
479    }
480    Some(session_home)
481}
482
483/// v0.6.7: apply `detect_session_wire_home` for the current process.
484///
485/// If `WIRE_HOME` is unset and the current cwd maps to an existing
486/// session, set `WIRE_HOME` for the rest of this process and emit a
487/// one-liner to stderr so the operator knows which identity is in
488/// use. Noop when `WIRE_HOME` is already set (explicit override wins).
489///
490/// `label` distinguishes the caller in the stderr line (`mcp` vs
491/// `cli`). Set `WIRE_QUIET_AUTOSESSION=1` to suppress the stderr line
492/// while keeping the env-var application active.
493///
494/// MUST be called BEFORE any worker thread or async task spawns —
495/// `env::set_var` is unsafe in Rust 2024 because of thread-safety
496/// guarantees, and our use is safe only at process entry.
497pub fn maybe_adopt_session_wire_home(label: &str) {
498    if std::env::var("WIRE_HOME").is_ok() {
499        return;
500    }
501    let cwd = match std::env::current_dir() {
502        Ok(c) => c,
503        Err(_) => return,
504    };
505    let home = match detect_session_wire_home(&cwd) {
506        Some(h) => h,
507        None => return,
508    };
509    if std::env::var("WIRE_QUIET_AUTOSESSION").is_err() {
510        eprintln!(
511            "wire {label}: auto-detected session for cwd `{}` → WIRE_HOME=`{}`",
512            cwd.display(),
513            home.display()
514        );
515    }
516    // SAFETY: caller contract is "before any thread spawn." All
517    // production sites (cli::run, mcp::run) call this as the first
518    // step in their respective entry points.
519    unsafe {
520        std::env::set_var("WIRE_HOME", &home);
521    }
522}
523
524#[cfg(test)]
525mod tests {
526    use super::*;
527
528    #[test]
529    fn sanitize_handles_unicode_and_long_names() {
530        assert_eq!(sanitize_name("paul-mac"), "paul-mac");
531        assert_eq!(sanitize_name("Paul Mac!"), "paul-mac");
532        assert_eq!(sanitize_name("ünìcødë"), "n-c-d"); // ascii-only fallback
533        assert_eq!(sanitize_name(""), "wire-session");
534        assert_eq!(sanitize_name("---"), "wire-session");
535        let long: String = "a".repeat(100);
536        assert_eq!(sanitize_name(&long).len(), 32);
537    }
538
539    #[test]
540    fn derive_name_returns_basename_when_no_collision() {
541        let reg = SessionRegistry::default();
542        assert_eq!(
543            derive_name_from_cwd(Path::new("/Users/paul/Source/wire"), &reg),
544            "wire"
545        );
546        assert_eq!(
547            derive_name_from_cwd(Path::new("/Users/paul/Source/slancha-mesh"), &reg),
548            "slancha-mesh"
549        );
550    }
551
552    #[test]
553    fn derive_name_returns_stored_name_when_cwd_already_registered() {
554        let mut reg = SessionRegistry::default();
555        reg.by_cwd.insert(
556            "/Users/paul/Source/wire".to_string(),
557            "wire-special".to_string(),
558        );
559        assert_eq!(
560            derive_name_from_cwd(Path::new("/Users/paul/Source/wire"), &reg),
561            "wire-special"
562        );
563    }
564
565    #[test]
566    fn read_session_endpoints_handles_missing_relay_state() {
567        let tmp = tempfile::tempdir().unwrap();
568        // No relay.json under <home>/config/wire/ — should yield empty.
569        let endpoints = read_session_endpoints(tmp.path());
570        assert!(endpoints.is_empty());
571    }
572
573    #[test]
574    fn read_session_endpoints_parses_dual_slot_form() {
575        let tmp = tempfile::tempdir().unwrap();
576        let cfg = tmp.path().join("config").join("wire");
577        std::fs::create_dir_all(&cfg).unwrap();
578        let body = serde_json::json!({
579            "self": {
580                "relay_url": "https://wireup.net",
581                "slot_id": "fed-slot",
582                "slot_token": "fed-tok",
583                "endpoints": [
584                    {
585                        "relay_url": "https://wireup.net",
586                        "slot_id": "fed-slot",
587                        "slot_token": "fed-tok",
588                        "scope": "federation"
589                    },
590                    {
591                        "relay_url": "http://127.0.0.1:8771",
592                        "slot_id": "loop-slot",
593                        "slot_token": "loop-tok",
594                        "scope": "local"
595                    }
596                ]
597            }
598        });
599        std::fs::write(cfg.join("relay.json"), serde_json::to_vec(&body).unwrap()).unwrap();
600        let endpoints = read_session_endpoints(tmp.path());
601        assert_eq!(endpoints.len(), 2);
602        let local_count = endpoints
603            .iter()
604            .filter(|e| matches!(e.scope, EndpointScope::Local))
605            .count();
606        assert_eq!(local_count, 1);
607        let local = endpoints
608            .iter()
609            .find(|e| matches!(e.scope, EndpointScope::Local))
610            .unwrap();
611        assert_eq!(local.relay_url, "http://127.0.0.1:8771");
612        assert_eq!(local.slot_id, "loop-slot");
613    }
614
615    // NOTE: list_local_sessions is integration-tested via tests/cli.rs
616    // using a subprocess that sets WIRE_HOME per-process. We do not test
617    // it in-module because env mutation races other parallel unit tests
618    // (Rust 2024 marks std::env::set_var unsafe for that reason). The
619    // grouping logic is straightforward enough that the integration
620    // test plus the read_session_endpoints unit tests above provide
621    // adequate coverage.
622
623    #[test]
624    fn derive_name_appends_path_hash_when_basename_collides() {
625        let mut reg = SessionRegistry::default();
626        reg.by_cwd
627            .insert("/Users/paul/Source/wire".to_string(), "wire".to_string());
628        // Different cwd, same basename → must get a hash suffix.
629        let name = derive_name_from_cwd(Path::new("/Users/paul/Archive/wire"), &reg);
630        assert!(name.starts_with("wire-"));
631        assert_eq!(name.len(), "wire-".len() + 4); // 4 hex chars
632        assert_ne!(name, "wire");
633    }
634}