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    // v0.7.0-alpha.8 (review-fix #7): atomic write via tmp+rename so
127    // concurrent unflocked readers (detect_session_wire_home,
128    // list_sessions, cmd_peers) never observe a 0-byte / truncated
129    // registry mid-write. Pre-alpha.8 used std::fs::write which
130    // truncates first — race window where readers saw empty JSON and
131    // fell back to default identity for the write duration.
132    let tmp = path.with_extension("json.tmp");
133    std::fs::write(&tmp, body).with_context(|| format!("writing tmp session registry {tmp:?}"))?;
134    std::fs::rename(&tmp, &path).with_context(|| format!("atomic rename {tmp:?} → {path:?}"))?;
135    Ok(())
136}
137
138/// v0.7.0-alpha.3: flock'd read-modify-write of the session registry.
139///
140/// `write_registry` alone is not safe under concurrency — multiple MCP
141/// processes auto-initing in parallel each read an old snapshot, mutate
142/// their copy, and write back, losing N-1 updates. This helper acquires
143/// an exclusive flock on a sibling lockfile, re-reads inside the lock,
144/// applies the caller's modifier, writes atomically, and releases.
145///
146/// Modeled on `config::update_relay_state`. Lock contention is bounded:
147/// modifications are pure HashMap operations, write is whole-file at
148/// roughly the registry size (KBs, not MBs).
149pub fn update_registry<F>(modifier: F) -> Result<()>
150where
151    F: FnOnce(&mut SessionRegistry) -> Result<()>,
152{
153    use fs2::FileExt;
154    let path = registry_path()?;
155    if let Some(parent) = path.parent() {
156        std::fs::create_dir_all(parent).with_context(|| format!("creating {parent:?}"))?;
157    }
158    let lock_path = path.with_extension("lock");
159    let lock_file = std::fs::OpenOptions::new()
160        .create(true)
161        .truncate(false)
162        .read(true)
163        .write(true)
164        .open(&lock_path)
165        .with_context(|| format!("opening {lock_path:?}"))?;
166    lock_file
167        .lock_exclusive()
168        .with_context(|| format!("flock {lock_path:?}"))?;
169    // Re-read INSIDE the lock — any prior snapshot would race.
170    let mut reg = read_registry().unwrap_or_default();
171    let result = modifier(&mut reg);
172    let write_result = if result.is_ok() {
173        write_registry(&reg)
174    } else {
175        Ok(())
176    };
177    let _ = fs2::FileExt::unlock(&lock_file);
178    result?;
179    write_result?;
180    Ok(())
181}
182
183/// Sanitize an arbitrary string to a session-name-safe form: lowercase
184/// ASCII alphanumeric + `-` + `_`, replace other chars with `-`,
185/// dedupe consecutive dashes, trim leading/trailing dashes, max 32 chars.
186pub fn sanitize_name(raw: &str) -> String {
187    let mut out = String::with_capacity(raw.len());
188    let mut prev_dash = false;
189    for c in raw.chars() {
190        let ok = c.is_ascii_alphanumeric() || c == '-' || c == '_';
191        let ch = if ok { c.to_ascii_lowercase() } else { '-' };
192        if ch == '-' {
193            if !prev_dash && !out.is_empty() {
194                out.push('-');
195            }
196            prev_dash = true;
197        } else {
198            out.push(ch);
199            prev_dash = false;
200        }
201    }
202    let trimmed = out.trim_matches('-').to_string();
203    if trimmed.is_empty() {
204        return "wire-session".to_string();
205    }
206    if trimmed.len() > 32 {
207        return trimmed[..32].trim_end_matches('-').to_string();
208    }
209    trimmed
210}
211
212/// Short hash suffix derived from the full absolute path of the cwd.
213/// Used to disambiguate two different projects whose basenames collide
214/// (e.g. `~/Source/wire` and `~/Archive/wire`).
215fn path_hash_suffix(cwd: &Path) -> String {
216    let bytes = cwd.as_os_str().to_string_lossy().into_owned();
217    let mut h = Sha256::new();
218    h.update(bytes.as_bytes());
219    let digest = h.finalize();
220    hex::encode(&digest[..2]) // 4 hex chars
221}
222
223/// Derive a stable session name for the given cwd. Resolution order:
224///
225/// 1. If the registry already maps this cwd → name, return that name.
226/// 2. Else: candidate = sanitize(basename(cwd)). If the candidate is
227///    already mapped to a DIFFERENT cwd in the registry, append a
228///    4-char path-hash suffix to avoid collision.
229/// 3. If still a collision: append a numeric suffix `-2`, `-3`, ...
230///    until unique.
231pub fn derive_name_from_cwd(cwd: &Path, registry: &SessionRegistry) -> String {
232    let cwd_key = cwd.to_string_lossy().into_owned();
233    if let Some(existing) = registry.by_cwd.get(&cwd_key) {
234        return existing.clone();
235    }
236    let base = cwd
237        .file_name()
238        .and_then(|s| s.to_str())
239        .map(sanitize_name)
240        .unwrap_or_else(|| "wire-session".to_string());
241    let occupied: std::collections::HashSet<String> = registry.by_cwd.values().cloned().collect();
242    if !occupied.contains(&base) {
243        return base;
244    }
245    let with_hash = format!("{}-{}", base, path_hash_suffix(cwd));
246    if !occupied.contains(&with_hash) {
247        return with_hash;
248    }
249    // Highly unlikely (would require a SHA-256 prefix collision plus an
250    // existing entry to claim it). Numeric tiebreaker as final fallback.
251    for n in 2..1000 {
252        let candidate = format!("{base}-{n}");
253        if !occupied.contains(&candidate) {
254            return candidate;
255        }
256    }
257    // Pathological fallback — every numbered slot is taken.
258    format!("{base}-{}-overflow", path_hash_suffix(cwd))
259}
260
261/// Summary of one on-disk session for `wire session list`.
262#[derive(Debug, Clone, Serialize)]
263pub struct SessionInfo {
264    pub name: String,
265    /// First cwd associated with this session in the registry. `None`
266    /// if the session was created without registry tracking (manual
267    /// `wire session new <name>`).
268    pub cwd: Option<String>,
269    pub home_dir: PathBuf,
270    pub did: Option<String>,
271    pub handle: Option<String>,
272    /// True if a `daemon.pid` file exists AND the recorded PID is
273    /// actually a live process (best-effort, not POSIX-portable but
274    /// matches the existing `wire status` / `wire doctor` checks).
275    pub daemon_running: bool,
276    /// Display character (nickname + emoji + color palette) derived from
277    /// the session's DID. `None` when the session has no agent-card yet
278    /// (pre-init). Lazy-computed at read time; never persisted to disk.
279    pub character: Option<crate::character::Character>,
280}
281
282/// Enumerate every on-disk session by reading `sessions_root()`. Cross-
283/// references the registry so each entry's `cwd` is filled in when known.
284pub fn list_sessions() -> Result<Vec<SessionInfo>> {
285    let root = sessions_root()?;
286    if !root.exists() {
287        return Ok(Vec::new());
288    }
289    let registry = read_registry().unwrap_or_default();
290    // Reverse lookup: name → cwd. Used to annotate each SessionInfo.
291    let mut name_to_cwd: HashMap<String, String> = HashMap::new();
292    for (cwd, name) in &registry.by_cwd {
293        name_to_cwd.insert(name.clone(), cwd.clone());
294    }
295
296    let mut out = Vec::new();
297    for entry in std::fs::read_dir(&root)?.flatten() {
298        let path = entry.path();
299        if !path.is_dir() {
300            continue;
301        }
302        let name = match path.file_name().and_then(|s| s.to_str()) {
303            Some(s) => s.to_string(),
304            None => continue,
305        };
306        // Skip the registry sidecar.
307        if name == "registry.json" {
308            continue;
309        }
310        let card_path = path.join("config").join("wire").join("agent-card.json");
311        let (did, handle) = read_card_identity(&card_path);
312        let daemon_running = check_daemon_live(&path);
313        // v0.7.0-alpha.3: read this session's display.json for any
314        // operator-chosen nickname/emoji overrides.
315        let display_overrides_path = path.join("config").join("wire").join("display.json");
316        let overrides =
317            crate::config::read_display_overrides_at(&display_overrides_path).unwrap_or_default();
318        let character = did.as_deref().map(|d| {
319            crate::character::Character::from_did_with_override(
320                d,
321                overrides.nickname.as_deref(),
322                overrides.emoji.as_deref(),
323            )
324        });
325        out.push(SessionInfo {
326            name: name.clone(),
327            cwd: name_to_cwd.get(&name).cloned(),
328            home_dir: path,
329            did,
330            handle,
331            daemon_running,
332            character,
333        });
334    }
335    out.sort_by(|a, b| a.name.cmp(&b.name));
336    Ok(out)
337}
338
339fn read_card_identity(card_path: &Path) -> (Option<String>, Option<String>) {
340    let bytes = match std::fs::read(card_path) {
341        Ok(b) => b,
342        Err(_) => return (None, None),
343    };
344    let v: serde_json::Value = match serde_json::from_slice(&bytes) {
345        Ok(v) => v,
346        Err(_) => return (None, None),
347    };
348    let did = v.get("did").and_then(|x| x.as_str()).map(str::to_string);
349    let handle = v
350        .get("handle")
351        .and_then(|x| x.as_str())
352        .map(str::to_string)
353        .or_else(|| {
354            did.as_ref()
355                .map(|d| crate::agent_card::display_handle_from_did(d).to_string())
356        });
357    (did, handle)
358}
359
360fn check_daemon_live(session_home: &Path) -> bool {
361    // Pidfile lives at <session_home>/state/wire/daemon.pid. Use the
362    // existing ensure_up reader by temporarily pointing at the path; we
363    // can't change env mid-process race-free, so re-implement the pid
364    // extraction directly here from the JSON structure.
365    let pidfile = session_home.join("state").join("wire").join("daemon.pid");
366    let bytes = match std::fs::read(&pidfile) {
367        Ok(b) => b,
368        Err(_) => return false,
369    };
370    // Try the structured form first.
371    let pid_opt: Option<u32> = if let Ok(v) = serde_json::from_slice::<serde_json::Value>(&bytes) {
372        v.get("pid").and_then(|p| p.as_u64()).map(|p| p as u32)
373    } else {
374        // Legacy integer form.
375        String::from_utf8_lossy(&bytes).trim().parse::<u32>().ok()
376    };
377    let pid = match pid_opt {
378        Some(p) => p,
379        None => return false,
380    };
381    is_process_live(pid)
382}
383
384fn is_process_live(pid: u32) -> bool {
385    #[cfg(target_os = "linux")]
386    {
387        std::path::Path::new(&format!("/proc/{pid}")).exists()
388    }
389    #[cfg(not(target_os = "linux"))]
390    {
391        std::process::Command::new("kill")
392            .args(["-0", &pid.to_string()])
393            .output()
394            .map(|o| o.status.success())
395            .unwrap_or(false)
396    }
397}
398
399/// Read a session's `relay.json` and return its `self.endpoints[]`
400/// array (v0.5.17 dual-slot). Empty Vec on any read/parse error — this
401/// is a best-effort discovery helper, not a verification tool. A pre-
402/// v0.5.17 session writes only the legacy flat fields; `self_endpoints`
403/// promotes those to a federation-only Endpoint, so the result is
404/// still meaningful for legacy sessions.
405///
406/// v0.5.20 BUG FIX: this used to join `relay-state.json`, which is
407/// not the canonical filename (`config::relay_state_path` returns
408/// `relay.json`). The mis-named read silently no-op'd and
409/// `list-local` always returned an empty `local` map as a result.
410/// Companion to the `cli.rs::try_allocate_local_slot` filename fix
411/// in the same release — that helper had the symmetric write-side
412/// bug, so the local endpoint never got persisted in the first place.
413pub fn read_session_endpoints(session_home: &Path) -> Vec<Endpoint> {
414    let path = session_home.join("config").join("wire").join("relay.json");
415    let bytes = match std::fs::read(&path) {
416        Ok(b) => b,
417        Err(_) => return Vec::new(),
418    };
419    let val: Value = match serde_json::from_slice(&bytes) {
420        Ok(v) => v,
421        Err(_) => return Vec::new(),
422    };
423    self_endpoints(&val)
424}
425
426/// Stripped view of a Local endpoint for tooling output. Drops
427/// `slot_token` because it is a bearer credential — exposing it
428/// through `wire session list-local --json` would risk accidental
429/// leak via logs, screenshots, or piped output. Routing code uses
430/// the full `Endpoint` from `relay.json` directly; this type
431/// is for human/JSON observation only.
432#[derive(Debug, Clone, Serialize)]
433pub struct LocalEndpointView {
434    pub relay_url: String,
435    pub slot_id: String,
436}
437
438/// One row of `wire session list-local` output: a session that has a
439/// Local-scope endpoint plus metadata to render it.
440#[derive(Debug, Clone, Serialize)]
441pub struct LocalSessionView {
442    pub name: String,
443    pub handle: Option<String>,
444    pub did: Option<String>,
445    pub cwd: Option<String>,
446    pub home_dir: PathBuf,
447    pub daemon_running: bool,
448    /// All Local-scope endpoints this session advertises (token redacted).
449    /// Most sessions have exactly one; multiple is permitted for multi-
450    /// relay setups.
451    pub local_endpoints: Vec<LocalEndpointView>,
452}
453
454/// Sessions with no Local endpoint — shown separately so the operator
455/// knows they exist but are federation-only.
456#[derive(Debug, Clone, Serialize)]
457pub struct FederationOnlySessionView {
458    pub name: String,
459    pub handle: Option<String>,
460    pub cwd: Option<String>,
461}
462
463/// Result shape for `wire session list-local`. `local` is grouped by
464/// the local-relay URL so output can render each cluster of mutually-
465/// reachable sister sessions together. `federation_only` lists the rest.
466#[derive(Debug, Clone, Serialize)]
467pub struct LocalSessionListing {
468    pub local: HashMap<String, Vec<LocalSessionView>>,
469    pub federation_only: Vec<FederationOnlySessionView>,
470}
471
472/// Build the listing for `wire session list-local` from current on-disk
473/// state. Read-only; no daemon contact, no relay probe.
474pub fn list_local_sessions() -> Result<LocalSessionListing> {
475    let sessions = list_sessions()?;
476    let mut local: HashMap<String, Vec<LocalSessionView>> = HashMap::new();
477    let mut federation_only: Vec<FederationOnlySessionView> = Vec::new();
478
479    for s in sessions {
480        let endpoints = read_session_endpoints(&s.home_dir);
481        let local_eps: Vec<Endpoint> = endpoints
482            .into_iter()
483            .filter(|e| matches!(e.scope, EndpointScope::Local))
484            .collect();
485        if local_eps.is_empty() {
486            federation_only.push(FederationOnlySessionView {
487                name: s.name.clone(),
488                handle: s.handle.clone(),
489                cwd: s.cwd.clone(),
490            });
491            continue;
492        }
493        // Redacted view: drop slot_token before exposing through CLI.
494        let redacted: Vec<LocalEndpointView> = local_eps
495            .iter()
496            .map(|e| LocalEndpointView {
497                relay_url: e.relay_url.clone(),
498                slot_id: e.slot_id.clone(),
499            })
500            .collect();
501        // Group by relay_url. A session with two Local endpoints (rare —
502        // would mean two loopback relays) appears under each.
503        for ep in &local_eps {
504            local
505                .entry(ep.relay_url.clone())
506                .or_default()
507                .push(LocalSessionView {
508                    name: s.name.clone(),
509                    handle: s.handle.clone(),
510                    did: s.did.clone(),
511                    cwd: s.cwd.clone(),
512                    home_dir: s.home_dir.clone(),
513                    daemon_running: s.daemon_running,
514                    local_endpoints: redacted.clone(),
515                });
516        }
517    }
518    // Sort each group by session name so output is deterministic.
519    for group in local.values_mut() {
520        group.sort_by(|a, b| a.name.cmp(&b.name));
521    }
522    federation_only.sort_by(|a, b| a.name.cmp(&b.name));
523    Ok(LocalSessionListing {
524        local,
525        federation_only,
526    })
527}
528
529/// v0.6.7: cwd → session WIRE_HOME lookup. Read-only.
530///
531/// When `WIRE_HOME` isn't set in env, look up `cwd` in the session
532/// registry. If a session is registered for this cwd AND its home
533/// directory still exists, return that home dir; otherwise None.
534///
535/// Used by both `wire mcp` (v0.6.1) and the CLI entry point (v0.6.7)
536/// so a `wire whoami` / `wire monitor` invocation from a project cwd
537/// adopts that project's session identity automatically, instead of
538/// silently falling back to the machine default. The CLI parity is
539/// load-bearing: without it, the user-visible identity diverges
540/// between MCP and the terminal, and monitors pull machine-wide
541/// inboxes when the operator expected a per-session view.
542pub fn detect_session_wire_home(cwd: &std::path::Path) -> Option<PathBuf> {
543    let registry = read_registry().ok()?;
544    // v0.7.0-alpha.2: walk up parent dirs. Subdirs of a registered cwd
545    // inherit their parent's wire identity (e.g.
546    // `~/Source/slancha-business/tools/recon` → `slancha-business` session).
547    // Without this, subdirs all fell back to the machine-wide default
548    // identity, which silently collapsed multiple Claude sessions onto the
549    // same DID + character.
550    let mut probe: Option<&std::path::Path> = Some(cwd);
551    while let Some(path) = probe {
552        let path_str = path.to_string_lossy().into_owned();
553        if let Some(session_name) = registry.by_cwd.get(&path_str) {
554            let session_home = session_dir(session_name).ok()?;
555            if session_home.exists() {
556                return Some(session_home);
557            }
558        }
559        probe = path.parent();
560    }
561    None
562}
563
564/// v0.6.10: warn at MCP/CLI startup if another `wire mcp` process is
565/// already running with the same effective `WIRE_HOME`. Closes the
566/// "two Claudes in same cwd silently share an identity" failure mode
567/// that wasted hours of operator debugging time: today the collision
568/// is invisible (both Claudes resolve to the same wire session via
569/// v0.6.7 auto-detect, race the inbox cursor, "look identical" from
570/// the operator's view). This surfaces it explicitly with a clear
571/// remediation path.
572///
573/// Best-effort: any subprocess / env-read failure is silent (the
574/// collision check should never block startup). Cross-platform via
575/// `ps -E -p <pid>` on macOS, `/proc/<pid>/environ` on Linux. Windows
576/// returns empty (no collision detected).
577pub fn warn_on_identity_collision(self_pid: u32) {
578    let our_wire_home = match std::env::var("WIRE_HOME") {
579        Ok(h) => h,
580        Err(_) => return,
581    };
582
583    let pgrep_out = match std::process::Command::new("pgrep")
584        .args(["-f", "wire mcp"])
585        .output()
586    {
587        Ok(o) if o.status.success() => o,
588        _ => return,
589    };
590
591    let other_pids: Vec<u32> = String::from_utf8_lossy(&pgrep_out.stdout)
592        .split_whitespace()
593        .filter_map(|s| s.parse::<u32>().ok())
594        .filter(|&p| p != self_pid)
595        .collect();
596
597    let mut colliders: Vec<u32> = Vec::new();
598    for pid in &other_pids {
599        if let Some(their_home) = read_wire_home_from_pid(*pid)
600            && their_home == our_wire_home
601        {
602            colliders.push(*pid);
603        }
604    }
605
606    if colliders.is_empty() {
607        return;
608    }
609
610    eprintln!(
611        "wire mcp: WARNING — {} other wire mcp process(es) already using WIRE_HOME=`{}` (pid {})",
612        colliders.len(),
613        our_wire_home,
614        colliders
615            .iter()
616            .map(|p| p.to_string())
617            .collect::<Vec<_>>()
618            .join(", ")
619    );
620    eprintln!(
621        "  Multiple agents sharing one identity will race the inbox cursor; messages may be lost."
622    );
623    eprintln!("  To use a separate identity:");
624    eprintln!("    1. Close the other agent(s), OR");
625    eprintln!("    2. `wire session new <name> --local-only` to create a fresh identity, then");
626    eprintln!(
627        "    3. Restart THIS agent's launcher with `export WIRE_HOME=<path printed by step 2>`"
628    );
629}
630
631/// Best-effort cross-platform read of another process's `WIRE_HOME`.
632/// Linux: parses `/proc/<pid>/environ` (NUL-separated KEY=VAL).
633/// macOS: `ps -E -p <pid>` (whitespace-separated KEY=VAL prefix).
634/// Windows / other: returns `None` (collision detection no-ops).
635fn read_wire_home_from_pid(pid: u32) -> Option<String> {
636    #[cfg(target_os = "linux")]
637    {
638        let path = format!("/proc/{pid}/environ");
639        let bytes = std::fs::read(&path).ok()?;
640        for entry in bytes.split(|&b| b == 0) {
641            let s = match std::str::from_utf8(entry) {
642                Ok(s) => s,
643                Err(_) => continue,
644            };
645            if let Some(val) = s.strip_prefix("WIRE_HOME=") {
646                return Some(val.to_string());
647            }
648        }
649        None
650    }
651
652    #[cfg(target_os = "macos")]
653    {
654        let output = std::process::Command::new("ps")
655            .args(["-E", "-p", &pid.to_string(), "-o", "command="])
656            .output()
657            .ok()?;
658        let s = String::from_utf8_lossy(&output.stdout);
659        for tok in s.split_whitespace() {
660            if let Some(val) = tok.strip_prefix("WIRE_HOME=") {
661                return Some(val.to_string());
662            }
663        }
664        None
665    }
666
667    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
668    {
669        let _ = pid;
670        None
671    }
672}
673
674/// v0.6.7: apply `detect_session_wire_home` for the current process.
675///
676/// If `WIRE_HOME` is unset and the current cwd maps to an existing
677/// session, set `WIRE_HOME` for the rest of this process and emit a
678/// one-liner to stderr so the operator knows which identity is in
679/// use. Noop when `WIRE_HOME` is already set (explicit override wins).
680///
681/// `label` distinguishes the caller in the stderr line (`mcp` vs
682/// `cli`). Set `WIRE_QUIET_AUTOSESSION=1` to suppress the stderr line
683/// while keeping the env-var application active.
684///
685/// MUST be called BEFORE any worker thread or async task spawns —
686/// `env::set_var` is unsafe in Rust 2024 because of thread-safety
687/// guarantees, and our use is safe only at process entry.
688pub fn maybe_adopt_session_wire_home(label: &str) {
689    if std::env::var("WIRE_HOME").is_ok() {
690        return;
691    }
692    let cwd = match std::env::current_dir() {
693        Ok(c) => c,
694        Err(_) => return,
695    };
696    let home = match detect_session_wire_home(&cwd) {
697        Some(h) => h,
698        None => return,
699    };
700    if std::env::var("WIRE_QUIET_AUTOSESSION").is_err() {
701        eprintln!(
702            "wire {label}: auto-detected session for cwd `{}` → WIRE_HOME=`{}`",
703            cwd.display(),
704            home.display()
705        );
706    }
707    // SAFETY: caller contract is "before any thread spawn." All
708    // production sites (cli::run, mcp::run) call this as the first
709    // step in their respective entry points.
710    unsafe {
711        std::env::set_var("WIRE_HOME", &home);
712    }
713}
714
715#[cfg(test)]
716mod tests {
717    use super::*;
718
719    #[test]
720    fn sanitize_handles_unicode_and_long_names() {
721        assert_eq!(sanitize_name("paul-mac"), "paul-mac");
722        assert_eq!(sanitize_name("Paul Mac!"), "paul-mac");
723        assert_eq!(sanitize_name("ünìcødë"), "n-c-d"); // ascii-only fallback
724        assert_eq!(sanitize_name(""), "wire-session");
725        assert_eq!(sanitize_name("---"), "wire-session");
726        let long: String = "a".repeat(100);
727        assert_eq!(sanitize_name(&long).len(), 32);
728    }
729
730    #[test]
731    fn derive_name_returns_basename_when_no_collision() {
732        let reg = SessionRegistry::default();
733        assert_eq!(
734            derive_name_from_cwd(Path::new("/Users/paul/Source/wire"), &reg),
735            "wire"
736        );
737        assert_eq!(
738            derive_name_from_cwd(Path::new("/Users/paul/Source/slancha-mesh"), &reg),
739            "slancha-mesh"
740        );
741    }
742
743    #[test]
744    fn derive_name_returns_stored_name_when_cwd_already_registered() {
745        let mut reg = SessionRegistry::default();
746        reg.by_cwd.insert(
747            "/Users/paul/Source/wire".to_string(),
748            "wire-special".to_string(),
749        );
750        assert_eq!(
751            derive_name_from_cwd(Path::new("/Users/paul/Source/wire"), &reg),
752            "wire-special"
753        );
754    }
755
756    #[test]
757    fn read_session_endpoints_handles_missing_relay_state() {
758        let tmp = tempfile::tempdir().unwrap();
759        // No relay.json under <home>/config/wire/ — should yield empty.
760        let endpoints = read_session_endpoints(tmp.path());
761        assert!(endpoints.is_empty());
762    }
763
764    #[test]
765    fn read_session_endpoints_parses_dual_slot_form() {
766        let tmp = tempfile::tempdir().unwrap();
767        let cfg = tmp.path().join("config").join("wire");
768        std::fs::create_dir_all(&cfg).unwrap();
769        let body = serde_json::json!({
770            "self": {
771                "relay_url": "https://wireup.net",
772                "slot_id": "fed-slot",
773                "slot_token": "fed-tok",
774                "endpoints": [
775                    {
776                        "relay_url": "https://wireup.net",
777                        "slot_id": "fed-slot",
778                        "slot_token": "fed-tok",
779                        "scope": "federation"
780                    },
781                    {
782                        "relay_url": "http://127.0.0.1:8771",
783                        "slot_id": "loop-slot",
784                        "slot_token": "loop-tok",
785                        "scope": "local"
786                    }
787                ]
788            }
789        });
790        std::fs::write(cfg.join("relay.json"), serde_json::to_vec(&body).unwrap()).unwrap();
791        let endpoints = read_session_endpoints(tmp.path());
792        assert_eq!(endpoints.len(), 2);
793        let local_count = endpoints
794            .iter()
795            .filter(|e| matches!(e.scope, EndpointScope::Local))
796            .count();
797        assert_eq!(local_count, 1);
798        let local = endpoints
799            .iter()
800            .find(|e| matches!(e.scope, EndpointScope::Local))
801            .unwrap();
802        assert_eq!(local.relay_url, "http://127.0.0.1:8771");
803        assert_eq!(local.slot_id, "loop-slot");
804    }
805
806    // NOTE: list_local_sessions is integration-tested via tests/cli.rs
807    // using a subprocess that sets WIRE_HOME per-process. We do not test
808    // it in-module because env mutation races other parallel unit tests
809    // (Rust 2024 marks std::env::set_var unsafe for that reason). The
810    // grouping logic is straightforward enough that the integration
811    // test plus the read_session_endpoints unit tests above provide
812    // adequate coverage.
813
814    #[test]
815    fn derive_name_appends_path_hash_when_basename_collides() {
816        let mut reg = SessionRegistry::default();
817        reg.by_cwd
818            .insert("/Users/paul/Source/wire".to_string(), "wire".to_string());
819        // Different cwd, same basename → must get a hash suffix.
820        let name = derive_name_from_cwd(Path::new("/Users/paul/Archive/wire"), &reg);
821        assert!(name.starts_with("wire-"));
822        assert_eq!(name.len(), "wire-".len() + 4); // 4 hex chars
823        assert_ne!(name, "wire");
824    }
825}