Skip to main content

wire/
config.rs

1//! On-disk state for `wire`.
2//!
3//! Layout:
4//!   `$XDG_CONFIG_HOME/wire/` (defaults to `~/.config/wire/`)
5//!     - `private.key`     — 32-byte raw Ed25519 seed (mode 0600)
6//!     - `agent-card.json` — signed self-card (mode 0644, public)
7//!     - `trust.json`      — pinned peers + tiers
8//!     - `config.toml`     — relay URL, body cap, etc. (created lazily)
9//!
10//!   `$XDG_STATE_HOME/wire/` (defaults to `~/.local/state/wire/`)
11//!     - `inbox/<peer>.jsonl`  — verified inbound events
12//!     - `outbox/<peer>.jsonl` — agent-appended outbound events (daemon flushes)
13//!     - `spool/`              — daemon-internal staging
14//!
15//! All paths are configurable via `WIRE_HOME` env var (overrides both dirs to
16//! `$WIRE_HOME/{config,state}/`). Used by the test harness to keep tests
17//! isolated from the operator's real config.
18
19use anyhow::{Context, Result, anyhow};
20use serde_json::Value;
21use std::collections::HashMap;
22use std::fs;
23use std::io::Write;
24use std::path::{Path, PathBuf};
25use std::sync::{Arc, Mutex, OnceLock};
26
27/// Root configuration directory. Honors `WIRE_HOME` for testing.
28///
29/// With `WIRE_HOME=/tmp/foo`, returns `/tmp/foo/config/wire`.
30/// Without it, returns the XDG default (e.g. `~/.config/wire/`).
31pub fn config_dir() -> Result<PathBuf> {
32    if let Ok(home) = std::env::var("WIRE_HOME") {
33        return Ok(PathBuf::from(home).join("config").join("wire"));
34    }
35    dirs::config_dir()
36        .map(|d| d.join("wire"))
37        .ok_or_else(|| anyhow!("could not resolve XDG_CONFIG_HOME — set WIRE_HOME"))
38}
39
40/// Root state directory (rotating data — inbox/outbox/spool).
41///
42/// With `WIRE_HOME=/tmp/foo`, returns `/tmp/foo/state/wire`.
43pub fn state_dir() -> Result<PathBuf> {
44    if let Ok(home) = std::env::var("WIRE_HOME") {
45        return Ok(PathBuf::from(home).join("state").join("wire"));
46    }
47    dirs::state_dir()
48        .or_else(dirs::data_local_dir)
49        .map(|d| d.join("wire"))
50        .ok_or_else(|| anyhow!("could not resolve XDG_STATE_HOME — set WIRE_HOME"))
51}
52
53pub fn private_key_path() -> Result<PathBuf> {
54    Ok(config_dir()?.join("private.key"))
55}
56pub fn agent_card_path() -> Result<PathBuf> {
57    Ok(config_dir()?.join("agent-card.json"))
58}
59pub fn trust_path() -> Result<PathBuf> {
60    Ok(config_dir()?.join("trust.json"))
61}
62pub fn config_toml_path() -> Result<PathBuf> {
63    Ok(config_dir()?.join("config.toml"))
64}
65pub fn inbox_dir() -> Result<PathBuf> {
66    Ok(state_dir()?.join("inbox"))
67}
68pub fn outbox_dir() -> Result<PathBuf> {
69    Ok(state_dir()?.join("outbox"))
70}
71
72/// Per-outbox-path mutex registry. Serializes intra-process appends so that
73/// concurrent `wire_send` calls (e.g. multiple agents driving the same MCP
74/// server) cannot interleave bytes mid-line. POSIX `O_APPEND` is atomic only
75/// for writes ≤ PIPE_BUF (typically 4096 bytes); wire events can exceed that
76/// (per-event cap is 256 KiB).
77///
78/// **Inter-process scope (CLI vs MCP-server vs daemon):** v0.1 does not take
79/// an OS-level flock — the daemon only reads the outbox + a cursor file, and
80/// concurrent CLI `wire send` invocations against a running MCP server are
81/// rare enough we accept the risk for now. v0.2 BACKLOG: switch to
82/// `fs2::FileExt::lock_exclusive` for cross-process safety.
83static OUTBOX_LOCKS: OnceLock<Mutex<HashMap<PathBuf, Arc<Mutex<()>>>>> = OnceLock::new();
84
85fn outbox_lock(path: &Path) -> Arc<Mutex<()>> {
86    let registry = OUTBOX_LOCKS.get_or_init(|| Mutex::new(HashMap::new()));
87    let mut g = registry.lock().expect("OUTBOX_LOCKS poisoned");
88    g.entry(path.to_path_buf())
89        .or_insert_with(|| Arc::new(Mutex::new(())))
90        .clone()
91}
92
93/// Append a single JSONL record to the outbox for `peer`, holding the
94/// per-path mutex to keep concurrent appenders from interleaving lines.
95///
96/// `record_bytes` should be the full canonical JSON of the signed event,
97/// without trailing newline (the helper appends it). All bytes are written
98/// in one `write_all` while the lock is held.
99///
100/// The `peer` arg is normalized to its bare handle (`bob@relay.example` →
101/// `bob`) so the outbox filename is always `<bare_handle>.jsonl`. This is
102/// the canonical form the push enumerator and daemon reader expect; the
103/// normalization at this chokepoint guarantees correctness for every
104/// future caller, even if they forget to `bare_handle()` first. The
105/// original silent-fail of v0.5.11 was a caller that passed the FQDN
106/// form (issue #2 — 25-minute message-loss incident, surface fix in
107/// v0.5.13). This defense-in-depth makes the on-disk contract self-
108/// enforcing instead of caller-policed.
109pub fn append_outbox_record(peer: &str, record_bytes: &[u8]) -> Result<PathBuf> {
110    ensure_dirs()?;
111    let normalized = crate::agent_card::bare_handle(peer);
112    let path = outbox_dir()?.join(format!("{normalized}.jsonl"));
113    let lock = outbox_lock(&path);
114    let _g = lock.lock().expect("outbox per-path mutex poisoned");
115    let mut f = fs::OpenOptions::new()
116        .create(true)
117        .append(true)
118        .open(&path)
119        .with_context(|| format!("opening outbox {path:?}"))?;
120    let mut buf = Vec::with_capacity(record_bytes.len() + 1);
121    buf.extend_from_slice(record_bytes);
122    buf.push(b'\n');
123    f.write_all(&buf)
124        .with_context(|| format!("appending to {path:?}"))?;
125    Ok(path)
126}
127
128/// Whether `wire init` has already been run (private key + card both present).
129pub fn is_initialized() -> Result<bool> {
130    Ok(private_key_path()?.exists() && agent_card_path()?.exists())
131}
132
133/// Create directory tree with restrictive permissions on the config dir.
134pub fn ensure_dirs() -> Result<()> {
135    let cfg = config_dir()?;
136    fs::create_dir_all(&cfg).with_context(|| format!("creating {cfg:?}"))?;
137    fs::create_dir_all(state_dir()?)?;
138    fs::create_dir_all(inbox_dir()?)?;
139    fs::create_dir_all(outbox_dir()?)?;
140    set_dir_mode_0700(&cfg)?;
141    Ok(())
142}
143
144#[cfg(unix)]
145fn set_dir_mode_0700(path: &Path) -> Result<()> {
146    use std::os::unix::fs::PermissionsExt;
147    let mut perms = fs::metadata(path)?.permissions();
148    perms.set_mode(0o700);
149    fs::set_permissions(path, perms)?;
150    Ok(())
151}
152
153#[cfg(not(unix))]
154fn set_dir_mode_0700(_: &Path) -> Result<()> {
155    Ok(())
156}
157
158/// Write a private key file with mode 0600.
159pub fn write_private_key(seed: &[u8; 32]) -> Result<()> {
160    let path = private_key_path()?;
161    fs::write(&path, seed).with_context(|| format!("writing {path:?}"))?;
162    set_file_mode_0600(&path)?;
163    Ok(())
164}
165
166#[cfg(unix)]
167fn set_file_mode_0600(path: &Path) -> Result<()> {
168    use std::os::unix::fs::PermissionsExt;
169    let mut perms = fs::metadata(path)?.permissions();
170    perms.set_mode(0o600);
171    fs::set_permissions(path, perms)?;
172    Ok(())
173}
174
175#[cfg(not(unix))]
176fn set_file_mode_0600(_: &Path) -> Result<()> {
177    Ok(())
178}
179
180/// Read the saved private key seed (32 bytes).
181pub fn read_private_key() -> Result<[u8; 32]> {
182    let path = private_key_path()?;
183    let bytes = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
184    if bytes.len() != 32 {
185        return Err(anyhow!(
186            "private key file has wrong length ({} != 32)",
187            bytes.len()
188        ));
189    }
190    let mut seed = [0u8; 32];
191    seed.copy_from_slice(&bytes);
192    Ok(seed)
193}
194
195pub fn write_agent_card(card: &Value) -> Result<()> {
196    let path = agent_card_path()?;
197    let body = serde_json::to_vec_pretty(card)?;
198    // v0.7.0-alpha.8 (review-fix #7): atomic write via tmp+rename so
199    // a power-loss / SIGKILL mid-write doesn't leave a 0-byte agent-
200    // card that `is_initialized()` claims is fine but `read_agent_card`
201    // can't parse. `cmd_identity_rename` made this a hot path; the
202    // pre-existing fs::write pattern was a corruption risk every call.
203    let tmp = path.with_extension("json.tmp");
204    fs::write(&tmp, body).with_context(|| format!("writing tmp {tmp:?}"))?;
205    fs::rename(&tmp, &path).with_context(|| format!("atomic rename {tmp:?} → {path:?}"))?;
206    Ok(())
207}
208
209pub fn read_agent_card() -> Result<Value> {
210    let path = agent_card_path()?;
211    let body = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
212    Ok(serde_json::from_slice(&body)?)
213}
214
215// ---------- display overrides (v0.7.0-alpha.3) ----------
216
217/// Path to `display.json` — operator-chosen character nickname + emoji
218/// override. Sidecar to agent-card. NOT signed (display-only, local-only).
219///
220/// Format: `{"nickname": "foxtrot-meadow", "emoji": "🦊"}` — both fields
221/// optional, omitted means use the auto-derived value.
222pub fn display_overrides_path() -> Result<PathBuf> {
223    Ok(config_dir()?.join("display.json"))
224}
225
226#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
227pub struct DisplayOverrides {
228    #[serde(default, skip_serializing_if = "Option::is_none")]
229    pub nickname: Option<String>,
230    #[serde(default, skip_serializing_if = "Option::is_none")]
231    pub emoji: Option<String>,
232}
233
234pub fn read_display_overrides() -> Result<DisplayOverrides> {
235    read_display_overrides_at(&display_overrides_path()?)
236}
237
238pub fn read_display_overrides_at(path: &Path) -> Result<DisplayOverrides> {
239    if !path.exists() {
240        return Ok(DisplayOverrides::default());
241    }
242    let body = fs::read(path).with_context(|| format!("reading {path:?}"))?;
243    Ok(serde_json::from_slice(&body)?)
244}
245
246pub fn write_display_overrides(overrides: &DisplayOverrides) -> Result<()> {
247    let path = display_overrides_path()?;
248    if let Some(parent) = path.parent() {
249        fs::create_dir_all(parent).with_context(|| format!("creating {parent:?}"))?;
250    }
251    let body = serde_json::to_vec_pretty(overrides)?;
252    // v0.7.0-alpha.8 (review-fix #7): atomic write — consistent with
253    // write_agent_card now that they share the cmd_identity_rename
254    // call path.
255    let tmp = path.with_extension("json.tmp");
256    fs::write(&tmp, body).with_context(|| format!("writing tmp {tmp:?}"))?;
257    fs::rename(&tmp, &path).with_context(|| format!("atomic rename {tmp:?} → {path:?}"))?;
258    Ok(())
259}
260
261pub fn write_trust(trust: &Value) -> Result<()> {
262    let path = trust_path()?;
263    let body = serde_json::to_vec_pretty(trust)?;
264    fs::write(&path, body).with_context(|| format!("writing {path:?}"))?;
265    Ok(())
266}
267
268pub fn read_trust() -> Result<Value> {
269    let path = trust_path()?;
270    if !path.exists() {
271        return Ok(crate::trust::empty_trust());
272    }
273    let body = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
274    Ok(serde_json::from_slice(&body)?)
275}
276
277// ---------- relay binding state ----------
278
279/// Path to `relay.json` — holds our own slot binding and pinned peer slots.
280/// Contains slot-tokens, so always written mode 0600.
281pub fn relay_state_path() -> Result<PathBuf> {
282    Ok(config_dir()?.join("relay.json"))
283}
284
285pub fn read_relay_state() -> Result<Value> {
286    let path = relay_state_path()?;
287    if !path.exists() {
288        return Ok(serde_json::json!({"self": Value::Null, "peers": {}}));
289    }
290    let body = fs::read(&path).with_context(|| format!("reading {path:?}"))?;
291    Ok(serde_json::from_slice(&body)?)
292}
293
294/// Atomic, lock-serialized write of the full relay-state. Every direct caller
295/// (foreground `wire dial`, the background daemon, MCP) funnels through here,
296/// so a foreground write can neither TEAR nor lost-update against the daemon.
297/// Holds the same `relay.lock` flock as [`update_relay_state`] and writes via
298/// tmp+rename.
299///
300/// Bug #3 (v0.13.2): the old raw `fs::write` here was non-atomic and lockless.
301/// A foreground `wire dial` and the daemon both rewrote `relay.json`
302/// concurrently, interleaving bytes and leaving trailing garbage ("trailing
303/// characters at line N") that made the file unparseable — breaking all
304/// push/pull until hand-repaired. Surfaced on Windows (file-sharing
305/// semantics make the interleave easy to hit) but the race was cross-platform.
306pub fn write_relay_state(state: &Value) -> Result<()> {
307    use fs2::FileExt;
308    let lock_path = relay_state_lock_path()?;
309    if let Some(parent) = lock_path.parent() {
310        fs::create_dir_all(parent).with_context(|| format!("creating {parent:?}"))?;
311    }
312    let lock_file = fs::OpenOptions::new()
313        .create(true)
314        .truncate(false)
315        .read(true)
316        .write(true)
317        .open(&lock_path)
318        .with_context(|| format!("opening {lock_path:?}"))?;
319    lock_file
320        .lock_exclusive()
321        .with_context(|| format!("flock {lock_path:?}"))?;
322    let r = write_relay_state_unlocked(state);
323    let _ = fs2::FileExt::unlock(&lock_file);
324    r
325}
326
327/// Atomic relay-state write WITHOUT taking `relay.lock` — the caller must
328/// already hold it (only [`update_relay_state`], which writes inside its own
329/// locked transaction). tmp+rename so a concurrent reader sees either the old
330/// or new whole file, never a partial one.
331fn write_relay_state_unlocked(state: &Value) -> Result<()> {
332    let path = relay_state_path()?;
333    let body = serde_json::to_vec_pretty(state)?;
334    let tmp = path.with_extension("json.tmp");
335    fs::write(&tmp, &body).with_context(|| format!("writing tmp {tmp:?}"))?;
336    set_file_mode_0600(&tmp)?;
337    fs::rename(&tmp, &path).with_context(|| format!("atomic rename {tmp:?} → {path:?}"))?;
338    Ok(())
339}
340
341/// Path to the flock file that serialises concurrent read-modify-write
342/// transactions against `relay.json`. Separate file because flock on the
343/// data file itself races with file replacement (fs::write truncates +
344/// rewrites — atomic-ish but the lock identity disappears).
345fn relay_state_lock_path() -> Result<PathBuf> {
346    Ok(config_dir()?.join("relay.lock"))
347}
348
349/// Atomic read-modify-write against `relay.json`. Holds an exclusive
350/// `fs2::FileExt::lock_exclusive` for the whole transaction so concurrent
351/// `wire` processes (multiple daemons, CLI vs daemon, CLI vs MCP) cannot
352/// race the cursor or peer-pin entries.
353///
354/// P0.3 (0.5.11). Today's debug had three concurrent `wire` processes
355/// (stale 0.2.4 daemon, fresh 0.5.10 daemon, and the CLI) racing the
356/// `self.last_pulled_event_id` cursor — one would advance it past an
357/// event, another would later rewind via stale snapshot. flock makes
358/// that impossible.
359///
360/// Lock timeout: blocks indefinitely (well-behaved processes release in
361/// < 1ms). Use sparingly outside short RMW windows — long holds will
362/// stall every other `wire` process.
363pub fn update_relay_state<F>(modifier: F) -> Result<()>
364where
365    F: FnOnce(&mut Value) -> Result<()>,
366{
367    use fs2::FileExt;
368    let lock_path = relay_state_lock_path()?;
369    if let Some(parent) = lock_path.parent() {
370        fs::create_dir_all(parent).with_context(|| format!("creating {parent:?}"))?;
371    }
372    // Open / create the lock file. Holding a handle keeps the file
373    // alive for the lifetime of the transaction.
374    let lock_file = fs::OpenOptions::new()
375        .create(true)
376        .truncate(false)
377        .read(true)
378        .write(true)
379        .open(&lock_path)
380        .with_context(|| format!("opening {lock_path:?}"))?;
381    lock_file
382        .lock_exclusive()
383        .with_context(|| format!("flock {lock_path:?}"))?;
384
385    // Read fresh state INSIDE the lock — any prior snapshot would be a
386    // race window. Then run the modifier. Then write atomically.
387    let mut state = read_relay_state()?;
388    let result = modifier(&mut state);
389    let write_result = if result.is_ok() {
390        // We already hold relay.lock — use the unlocked writer to avoid
391        // re-acquiring the same flock (which would deadlock).
392        write_relay_state_unlocked(&state)
393    } else {
394        Ok(())
395    };
396    // RAII: drop releases the lock. Explicit unlock for clarity + to
397    // ensure unlock happens even if Drop ordering ever changes.
398    let _ = fs2::FileExt::unlock(&lock_file);
399    result?;
400    write_result?;
401    Ok(())
402}
403
404/// Test-only helpers. Lives outside `tests` mod so other modules' tests
405/// can share the same WIRE_HOME isolation. Tests run in-process and share
406/// process-wide env state, so all WIRE_HOME mutators must use this lock or
407/// they race each other.
408#[cfg(test)]
409pub(crate) mod test_support {
410    use std::sync::Mutex;
411
412    pub static ENV_LOCK: Mutex<()> = Mutex::new(());
413
414    pub fn with_temp_home<F: FnOnce()>(f: F) {
415        // Recover from poison so one failing test doesn't cascade-fail the rest.
416        let _guard = ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner());
417        let tmp = std::env::temp_dir().join(format!("wire-test-{}", rand::random::<u32>()));
418        // SAFETY: ENV_LOCK serializes all callers, so no concurrent env access.
419        unsafe { std::env::set_var("WIRE_HOME", &tmp) };
420        let _ = std::fs::remove_dir_all(&tmp);
421        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
422        unsafe { std::env::remove_var("WIRE_HOME") };
423        let _ = std::fs::remove_dir_all(&tmp);
424        if let Err(e) = result {
425            std::panic::resume_unwind(e);
426        }
427    }
428}
429
430#[cfg(test)]
431mod tests {
432    use super::*;
433    use serde_json::json;
434
435    fn with_temp_home<F: FnOnce()>(f: F) {
436        super::test_support::with_temp_home(f)
437    }
438
439    #[test]
440    fn config_dir_honors_wire_home() {
441        with_temp_home(|| {
442            let dir = config_dir().unwrap();
443            assert!(dir.ends_with("wire"), "got {dir:?}");
444            assert!(dir.to_string_lossy().contains("wire-test-"));
445        });
446    }
447
448    #[test]
449    fn ensure_dirs_creates_layout() {
450        with_temp_home(|| {
451            ensure_dirs().unwrap();
452            assert!(config_dir().unwrap().is_dir());
453            assert!(state_dir().unwrap().is_dir());
454            assert!(inbox_dir().unwrap().is_dir());
455            assert!(outbox_dir().unwrap().is_dir());
456        });
457    }
458
459    #[test]
460    fn private_key_roundtrip() {
461        with_temp_home(|| {
462            ensure_dirs().unwrap();
463            let seed = [42u8; 32];
464            write_private_key(&seed).unwrap();
465            let read_back = read_private_key().unwrap();
466            assert_eq!(seed, read_back);
467        });
468    }
469
470    #[test]
471    fn agent_card_roundtrip() {
472        with_temp_home(|| {
473            ensure_dirs().unwrap();
474            let card = json!({"did": "did:wire:paul", "name": "Paul"});
475            write_agent_card(&card).unwrap();
476            let read_back = read_agent_card().unwrap();
477            assert_eq!(card, read_back);
478        });
479    }
480
481    #[test]
482    fn trust_returns_empty_when_missing() {
483        with_temp_home(|| {
484            ensure_dirs().unwrap();
485            let t = read_trust().unwrap();
486            assert_eq!(t["version"], 1);
487            assert!(t["agents"].is_object());
488        });
489    }
490
491    #[test]
492    fn update_relay_state_writes_through_lock() {
493        // P0.3 smoke: update_relay_state runs the modifier and persists the
494        // result. Doesn't exercise concurrent flock contention (that needs
495        // multi-process orchestration; deferred to an e2e test) but at least
496        // proves the happy path works end-to-end through the new lock
497        // wrapper.
498        with_temp_home(|| {
499            ensure_dirs().unwrap();
500            // Seed initial state.
501            let initial = json!({"self": null, "peers": {}});
502            write_relay_state(&initial).unwrap();
503            // Run an update.
504            super::update_relay_state(|state| {
505                state["self"] = json!({
506                    "relay_url": "https://test",
507                    "slot_id": "abc",
508                    "slot_token": "tok",
509                });
510                Ok(())
511            })
512            .unwrap();
513            // Verify persisted.
514            let after = read_relay_state().unwrap();
515            assert_eq!(after["self"]["relay_url"], "https://test");
516            assert_eq!(after["self"]["slot_id"], "abc");
517        });
518    }
519
520    #[test]
521    fn write_relay_state_never_tears_under_concurrency() {
522        // Bug #3 regression: many writers hammering relay.json with
523        // alternating long/short bodies. With the old raw fs::write a
524        // concurrent reader caught torn bytes ("trailing characters") and
525        // failed to parse. The atomic tmp+rename + flock must guarantee every
526        // read sees a complete, parseable file. (Threads share one process +
527        // WIRE_HOME; the flock serializes them just as it would processes.)
528        with_temp_home(|| {
529            ensure_dirs().unwrap();
530            write_relay_state(&json!({"self": null, "peers": {}})).unwrap();
531            let handles: Vec<_> = (0..8)
532                .map(|w| {
533                    std::thread::spawn(move || {
534                        for j in 0..25 {
535                            let body = if j % 2 == 0 {
536                                json!({"self": {"w": w, "j": j, "pad": "x".repeat(2048)}})
537                            } else {
538                                json!({"self": {"w": w}})
539                            };
540                            write_relay_state(&body).unwrap();
541                            // Reader must ALWAYS parse — never a torn file.
542                            read_relay_state().expect("relay.json must always parse");
543                        }
544                    })
545                })
546                .collect();
547            for h in handles {
548                h.join().unwrap();
549            }
550            assert!(read_relay_state().unwrap().get("self").is_some());
551        });
552    }
553
554    #[test]
555    fn update_relay_state_modifier_error_does_not_clobber() {
556        // P0.3 contract: if the modifier returns Err, the state on disk
557        // must NOT be overwritten — partial work shouldn't half-land. The
558        // operator's prior state should survive the failed RMW.
559        with_temp_home(|| {
560            ensure_dirs().unwrap();
561            let initial = json!({"self": {"relay_url": "https://prior"}, "peers": {}});
562            write_relay_state(&initial).unwrap();
563            let result = super::update_relay_state(|state| {
564                // Trash the state mid-modifier...
565                state["self"] = json!({"relay_url": "https://NEVER_PERSIST"});
566                // ...then fail. Write must NOT happen.
567                anyhow::bail!("simulated mid-RMW error")
568            });
569            assert!(result.is_err());
570            let after = read_relay_state().unwrap();
571            assert_eq!(
572                after["self"]["relay_url"], "https://prior",
573                "state on disk must not reflect aborted modifier"
574            );
575        });
576    }
577
578    #[test]
579    fn is_initialized_true_only_after_both_files_written() {
580        with_temp_home(|| {
581            ensure_dirs().unwrap();
582            assert!(!is_initialized().unwrap());
583            write_private_key(&[0u8; 32]).unwrap();
584            assert!(!is_initialized().unwrap()); // card still missing
585            write_agent_card(&json!({"did": "did:wire:paul"})).unwrap();
586            assert!(is_initialized().unwrap());
587        });
588    }
589
590    #[cfg(unix)]
591    #[test]
592    fn append_outbox_record_normalizes_fqdn_to_bare_handle() {
593        // Regression for issue #2 (v0.5.11 silent-fail): if a caller
594        // passes the FQDN form (`bob@relay.example`), the file MUST
595        // still land at `bob.jsonl` so `wire push` enumerates it.
596        with_temp_home(|| {
597            let path_fqdn = append_outbox_record("bob@wireup.net", b"{\"kind\":1100}").unwrap();
598            let path_bare = append_outbox_record("bob", b"{\"kind\":1100}").unwrap();
599            // Both calls must land in the SAME file — the bare handle one.
600            assert_eq!(path_fqdn, path_bare, "FQDN form should normalize to bare");
601            assert!(
602                path_fqdn.file_name().unwrap().to_string_lossy() == "bob.jsonl",
603                "expected bob.jsonl, got {path_fqdn:?}"
604            );
605            // And the FQDN-named file MUST NOT exist.
606            let outbox = outbox_dir().unwrap();
607            assert!(
608                !outbox.join("bob@wireup.net.jsonl").exists(),
609                "FQDN-named file must not be created"
610            );
611            // The bare file should have BOTH writes.
612            let body = std::fs::read_to_string(&path_bare).unwrap();
613            assert_eq!(body.matches("kind").count(), 2, "got: {body}");
614        });
615    }
616
617    #[test]
618    fn private_key_is_mode_0600() {
619        use std::os::unix::fs::PermissionsExt;
620        with_temp_home(|| {
621            ensure_dirs().unwrap();
622            write_private_key(&[1u8; 32]).unwrap();
623            let mode = fs::metadata(private_key_path().unwrap())
624                .unwrap()
625                .permissions()
626                .mode();
627            assert_eq!(mode & 0o777, 0o600, "got {:o}", mode & 0o777);
628        });
629    }
630}