Skip to main content

agentmux_srv_core/registry/
paths.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Resolve the shared registry directory (`<shared_home>/agents/registry/`).
5
6use std::path::PathBuf;
7
8/// Resolve the GLOBAL `<home>/shared/agents/registry/` directory.
9///
10/// P0.3 re-roots the named-**instance** registry from the old
11/// channel-scoped `channels/<ch>/agents/registry/` (which walked
12/// `AGENTMUX_DATA_DIR` up three levels and so landed inside the current
13/// channel) to the channel-independent `~/.agentmux/shared/`. An agent
14/// named in one channel is then visible in every channel, exactly like the
15/// definition store ([`resolve_shared_definitions_dir`]) — the two are now
16/// siblings under `shared/agents/`.
17///
18/// The base directory that instance `working_directory` values are stored
19/// *relative to* is tracked separately (the Store's `registry_agents_base`,
20/// fed from `AGENTMUX_AGENTS_DIR`) — it must stay the current channel's
21/// agents dir even though the registry files are now global.
22///
23/// Returns `None` only if the global shared root can't be resolved — caller
24/// treats this as "registry disabled," no write attempts.
25pub fn resolve_shared_registry_dir() -> Option<PathBuf> {
26    resolve_global_shared_root().map(|h| h.join("agents").join("registry"))
27}
28
29/// Resolve the GLOBAL `<home>/shared/agents/definitions/` directory.
30///
31/// Sibling of [`resolve_shared_registry_dir`]: since P0.3 both the definition
32/// store and the instance registry are **channel-independent**, resolved via
33/// the same [`resolve_global_shared_root`] so an agent created/named in one
34/// channel is visible in every channel (cross-channel agent persistence,
35/// P0.2/P0.3). Resolves via the launcher-exported `AGENTMUX_SHARED_DIR`, with
36/// a test override and a `~/.agentmux/shared` fallback.
37pub fn resolve_shared_definitions_dir() -> Option<PathBuf> {
38    resolve_global_shared_root().map(|h| h.join("agents").join("definitions"))
39}
40
41/// Resolve `~/.agentmux/shared/store.db` — the global store for
42/// accounts, bundles, drone definitions, and MuxBus credentials.
43///
44/// Uses the same root as the agent registry/definitions so
45/// `AGENTMUX_HOME_OVERRIDE` and `AGENTMUX_SHARED_DIR` work consistently.
46/// Returns `None` only when the shared root itself can't be resolved.
47///
48/// When `agentmux_common::isolated_auth_enabled()` is set, resolves to
49/// `<instance_dir>/identity-store.db` instead — a channel-scoped store for
50/// destructive account testing (delete-account flows) that can never touch
51/// the real global store other channels/instances depend on. Falls back
52/// to the global path if `AGENTMUX_INSTANCE_DIR` isn't set (e.g. a bare
53/// `cargo run` outside the launcher) rather than failing outright — this
54/// function's existing `None`-on-unresolvable contract is preserved, just
55/// with an extra source to try first. See
56/// `docs/specs/SPEC_ISOLATED_AUTH_DEV_TESTING_2026_07_27.md`.
57///
58/// IMPORTANT: `MigrationContext.home` (`migrations/runner.rs`) must NEVER
59/// be derived from this function's return value (e.g. via
60/// `.parent().parent()`) — it needs the unconditional global root
61/// regardless of isolation, and must call `resolve_global_shared_root()`
62/// directly instead. See that module's comments.
63pub fn resolve_shared_store_path() -> Option<std::path::PathBuf> {
64    if agentmux_common::isolated_auth_enabled() {
65        if let Some(instance_dir) = std::env::var_os("AGENTMUX_INSTANCE_DIR") {
66            if !instance_dir.is_empty() {
67                return Some(std::path::PathBuf::from(instance_dir).join("identity-store.db"));
68            }
69        }
70    }
71    resolve_global_shared_root().map(|h| h.join("store.db"))
72}
73
74/// Resolve the GLOBAL `<home>/shared/agents/reactive/` directory — the
75/// cross-channel presence registry for live reactive-injection targets
76/// (agent_id -> local_url/block_id/pid/auth_key, one entry per channel),
77/// used by MuxBus Tier 2b same-host cross-channel delivery
78/// (issue #1916 / PR #2350) and by agent admission's other-instance probe.
79///
80/// Sibling of [`resolve_shared_registry_dir`] in *location* (both live
81/// under `shared/agents/`), but a different *concern*: that registry
82/// mirrors durable agent definitions/session-resume state, while this one
83/// tracks live, ephemeral reachability for message forwarding — entries
84/// here are written at reactive-register time and removed at unregister;
85/// any a crashed channel leaves behind are dropped by the startup sweep.
86///
87/// Returns `None` only if the global shared root can't be resolved —
88/// caller treats this as "cross-channel delivery disabled," falling back
89/// to same-channel-only Tier 2 (no behavior regression).
90pub fn resolve_shared_reactive_dir() -> Option<PathBuf> {
91    resolve_global_shared_root().map(|h| h.join("agents").join("reactive"))
92}
93
94/// Resolve the GLOBAL `<home>/shared/lan/instances/` directory: one file per
95/// LAN-enabled AgentMux instance on this host, so whichever instance holds
96/// the UDP probe port can list the others as `siblings`
97/// (SPEC_LAN_FLEET_FEED_2026_10_03.md §3). Beside `agents/`, not inside the
98/// reactive registry: these are instances, not agents.
99pub fn resolve_shared_lan_instances_dir() -> Option<PathBuf> {
100    resolve_global_shared_root().map(|h| h.join("lan").join("instances"))
101}
102
103/// Resolve the GLOBAL `<home>/shared/instances/` directory: one record per running srv on this
104/// machine, agents or not, so one instance can name and reach another
105/// (`backend::instance_registry`). Not the LAN sibling directory above, which lists only
106/// LAN-enabled instances and holds the LAN key instead of the auth key.
107pub fn resolve_shared_instances_dir() -> Option<PathBuf> {
108    resolve_global_shared_root().map(|h| h.join("instances"))
109}
110
111/// Resolve the GLOBAL `<home>/shared/agents/transcripts/` directory.
112///
113/// Sibling of [`resolve_shared_registry_dir`] / [`resolve_shared_definitions_dir`]:
114/// the agent's *conversation transcript* is the last per-channel agent surface
115/// (definitions, instances, workspaces, and auth all became global in
116/// #1387–#1396). Backing the `agent:<defId>:current` FileStore zone with a store
117/// rooted here makes a conversation load when you open the agent from *any*
118/// build/channel — the open path reads the same zone regardless of which channel
119/// wrote it. See `docs/analysis/ANALYSIS_CROSS_CHANNEL_CONVERSATION_HISTORY_2026_06_14.md`.
120///
121/// Returns `None` only if the global shared root can't be resolved — caller
122/// treats this as "global transcripts disabled" and falls back to the
123/// per-channel store.
124pub fn resolve_shared_transcripts_dir() -> Option<PathBuf> {
125    resolve_global_shared_root().map(|h| h.join("agents").join("transcripts"))
126}
127
128/// Resolve `~/.agentmux/shared/identity-store.db` — the permanently-global
129/// store for agent→account links, bundles, drone definitions,
130/// muxbus credentials, per-agent M2M credentials, native memory, and cron
131/// jobs. See `docs/specs/SPEC_IDENTITY_STORE_SPLIT_2026_08_17.md`.
132///
133/// Unlike [`resolve_shared_store_path`], this is **never** redirected by
134/// [`agentmux_common::isolated_auth_enabled`] — none of the tables this
135/// store holds have a legitimate per-channel-isolation use case (that
136/// audit is §2.3 of the design doc above). Uses the same
137/// `AGENTMUX_HOME_OVERRIDE`/`AGENTMUX_SHARED_DIR` resolution as every
138/// other `resolve_shared_*` function in this file, via
139/// [`resolve_global_shared_root`], so tests/overrides behave consistently.
140/// Returns `None` only when the shared root itself can't be resolved.
141pub fn resolve_identity_store_path() -> Option<std::path::PathBuf> {
142    resolve_global_shared_root().map(|h| h.join("identity-store.db"))
143}
144
145/// Where this channel keeps its own work store (the Kanban queue, Presence
146/// claims, coordination posts, the Jobs ledger and scheduled jobs), or `None`
147/// when it shares the host's: any build but a `task dev` one, or a dev build with
148/// `AGENTMUX_ISOLATED_WORK=0`. See [`crate::registry::work_store`].
149///
150/// The channel's instance dir, which outlives a version bump; `fallback_dir`
151/// (the version's db dir) only when a bare run has no instance dir, so an
152/// isolated channel never falls back to the shared files.
153pub fn resolve_isolated_work_store_path(fallback_dir: &std::path::Path) -> Option<std::path::PathBuf> {
154    if !crate::registry::work_store::isolated_work_enabled() {
155        return None;
156    }
157    let dir = std::env::var_os("AGENTMUX_INSTANCE_DIR")
158        .filter(|d| !d.is_empty())
159        .map(std::path::PathBuf::from)
160        .unwrap_or_else(|| fallback_dir.to_path_buf());
161    Some(dir.join("work-store.db"))
162}
163
164/// The GLOBAL `<home>/shared/container-agent-folders/` directory: one small file per folder a container
165/// agent has run in (`backend::mcp_folder_rule`). Every version and channel of an install reads the same
166/// marks, and an upgrade, the removal of an old version and the deletion of an agent leave them. A mark is
167/// plain text, written once and never changed, the same whichever srv writes it: so it is read and written
168/// by a srv that has given the shared stores up to a srv of another version too.
169pub fn resolve_shared_container_agent_folders_dir() -> Option<PathBuf> {
170    shared_root_ignoring_deferral().map(|h| h.join("container-agent-folders"))
171}
172
173/// The GLOBAL `<home>/shared/folder-overlap-choices/` directory: one small file per pair of agents whose folders
174/// overlap (`backend::folder_overlap`), written when the person goes ahead. Like the marks of
175/// [`resolve_shared_container_agent_folders_dir`] it outlives an upgrade and is bound into no container.
176pub fn resolve_shared_folder_overlap_choices_dir() -> Option<PathBuf> {
177    shared_root_ignoring_deferral().map(|h| h.join("folder-overlap-choices"))
178}
179
180/// The global, channel-independent `<home>/shared` root. Deliberately
181/// UNAFFECTED by `isolated_auth_enabled()` — callers that need the true
182/// `~/.agentmux` home (e.g. `migrations/runner.rs`'s `MigrationContext.home`)
183/// must call this directly rather than deriving it from
184/// `resolve_shared_store_path()`, which DOES vary with isolation.
185pub fn resolve_global_shared_root() -> Option<PathBuf> {
186    // A srv that gave the folder up to an earlier srv of another version runs as if there were none.
187    if crate::registry::shared_guard::deferred() {
188        return None;
189    }
190    shared_root_ignoring_deferral()
191}
192
193/// The same root while this srv has given the folder up: only for a plain file it reads and never
194/// writes (the jekt audit file), where an earlier srv's lines are still the account's record.
195pub fn shared_root_ignoring_deferral() -> Option<PathBuf> {
196    // `var_os` throughout: a path that isn't valid UTF-8 must not read as unset.
197    if let Some(s) = std::env::var_os("AGENTMUX_HOME_OVERRIDE") {
198        if !s.is_empty() {
199            // Consistent with data_paths everywhere else: the override is the
200            // ~/.agentmux root, with the shared dir at root/shared. (reagent
201            // P2 on #1385.)
202            return Some(PathBuf::from(s).join("shared"));
203        }
204    }
205    if let Some(s) = std::env::var_os("AGENTMUX_SHARED_DIR") {
206        if !s.is_empty() {
207            return Some(PathBuf::from(s));
208        }
209    }
210    // Fallible on purpose: this function documents "Returns None only when
211    // the shared root itself can't be resolved", and callers across
212    // migrations/runner, bootstrap, reactive, muxspect and native-memory rely
213    // on that None to disable cleanly ("No shared root (CI, a bare cargo
214    // run) — nothing to re-key"). An infallible resolver made every one of
215    // those branches unreachable.
216    crate::srv_info::agentmux_root().map(|root| root.join("shared"))
217}
218
219#[cfg(test)]
220mod tests {
221    use super::*;
222
223    // Process-global env access — shared with migrations::runner and
224    // migrations::m0011_shared_store_backfill's tests, which mutate the SAME
225    // AGENTMUX_ISOLATED_AUTH/AGENTMUX_INSTANCE_DIR vars. A module-local lock
226    // only serializes tests within this file; Cargo runs a crate's tests in
227    // one multi-threaded process, so a local-only lock still let this
228    // module's tests race against those two (reagent/codex on PR #2318).
229    use crate::test_support::ISOLATED_AUTH_ENV_LOCK as ENV_LOCK;
230
231    fn clear() {
232        std::env::remove_var("AGENTMUX_HOME_OVERRIDE");
233        std::env::remove_var("AGENTMUX_DATA_DIR");
234        std::env::remove_var("AGENTMUX_SHARED_DIR");
235        std::env::remove_var("AGENTMUX_ISOLATED_AUTH");
236        std::env::remove_var("AGENTMUX_ISOLATED_WORK");
237        std::env::remove_var("AGENTMUX_INSTANCE_DIR");
238        std::env::remove_var("AGENTMUX_CHANNEL");
239    }
240
241    #[test]
242    fn a_build_with_its_own_work_store_keeps_it_in_the_instance_dir() {
243        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
244        clear();
245        std::env::set_var("AGENTMUX_ISOLATED_WORK", "1");
246        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
247        let fallback = PathBuf::from("/tmp/test-home/dev/some-branch/v/db");
248        assert_eq!(
249            resolve_isolated_work_store_path(&fallback),
250            Some(PathBuf::from("/tmp/test-home/dev/some-branch/work-store.db"))
251        );
252        // A bare run with no instance dir still never falls back to the host's files.
253        std::env::remove_var("AGENTMUX_INSTANCE_DIR");
254        assert_eq!(resolve_isolated_work_store_path(&fallback), Some(fallback.join("work-store.db")));
255        clear();
256    }
257
258    #[test]
259    fn opting_out_shares_the_hosts_work_store() {
260        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
261        clear();
262        std::env::set_var("AGENTMUX_ISOLATED_WORK", "0");
263        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
264        assert_eq!(resolve_isolated_work_store_path(&PathBuf::from("/x")), None);
265        clear();
266    }
267
268    /// The marks of folders a container agent ran in are in the shared folder, wherever this version's data is.
269    #[test]
270    fn the_container_agent_folder_marks_are_in_the_shared_folder_not_in_a_versions_data() {
271        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
272        clear();
273        let saved = std::env::var_os("AGENTMUX_DATA_HOME");
274        std::env::set_var("AGENTMUX_SHARED_DIR", "/tmp/test-home/shared");
275        std::env::set_var("AGENTMUX_DATA_HOME", "/tmp/test-home/channels/stable/versions/1.0.0/data");
276        let one = resolve_shared_container_agent_folders_dir().unwrap();
277        std::env::set_var("AGENTMUX_DATA_HOME", "/tmp/test-home/channels/beta/versions/2.0.0/data");
278        let other = resolve_shared_container_agent_folders_dir().unwrap();
279        match saved {
280            Some(v) => std::env::set_var("AGENTMUX_DATA_HOME", v),
281            None => std::env::remove_var("AGENTMUX_DATA_HOME"),
282        }
283        clear();
284        assert_eq!(one, PathBuf::from("/tmp/test-home/shared/container-agent-folders"));
285        assert_eq!(one, other, "one folder for every version and channel");
286    }
287
288    #[test]
289    fn override_wins() {
290        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
291        clear();
292        // The override is the ~/.agentmux root; the global registry lives at
293        // root/shared/agents/registry (P0.3 re-root).
294        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
295        let r = resolve_shared_registry_dir().unwrap();
296        assert_eq!(
297            r,
298            PathBuf::from("/tmp/test-home/shared/agents/registry")
299        );
300        clear();
301    }
302
303    #[test]
304    fn registry_uses_shared_dir_and_ignores_data_dir() {
305        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
306        clear();
307        // The instance registry is GLOBAL now: it resolves from the shared
308        // root, NOT by walking up the per-version AGENTMUX_DATA_DIR (which
309        // would land in the current channel). Set both and confirm the
310        // channel-scoped data dir is ignored.
311        std::env::set_var("AGENTMUX_SHARED_DIR", "/home/user/.agentmux/shared");
312        std::env::set_var(
313            "AGENTMUX_DATA_DIR",
314            "/home/user/.agentmux/channels/stable/versions/0.44.2/data",
315        );
316        let r = resolve_shared_registry_dir().unwrap();
317        assert_eq!(
318            r,
319            PathBuf::from("/home/user/.agentmux/shared/agents/registry")
320        );
321        // Sibling of the definition store — both under shared/agents/.
322        let d = resolve_shared_definitions_dir().unwrap();
323        assert_eq!(r.parent(), d.parent());
324        clear();
325    }
326
327    #[test]
328    fn falls_back_to_home() {
329        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
330        clear();
331        // No env set — uses dirs::home_dir()/.agentmux/shared. Non-None.
332        assert!(resolve_shared_registry_dir().is_some());
333    }
334
335    #[test]
336    fn reactive_dir_uses_shared_dir() {
337        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
338        clear();
339        std::env::set_var("AGENTMUX_SHARED_DIR", "/home/user/.agentmux/shared");
340        let r = resolve_shared_reactive_dir().unwrap();
341        assert_eq!(
342            r,
343            PathBuf::from("/home/user/.agentmux/shared/agents/reactive")
344        );
345        clear();
346    }
347
348    #[test]
349    fn reactive_dir_override_wins() {
350        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
351        clear();
352        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
353        let r = resolve_shared_reactive_dir().unwrap();
354        assert_eq!(
355            r,
356            PathBuf::from("/tmp/test-home/shared/agents/reactive")
357        );
358        clear();
359    }
360
361    #[test]
362    fn reactive_dir_is_sibling_of_registry_dir() {
363        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
364        clear();
365        std::env::set_var("AGENTMUX_SHARED_DIR", "/home/user/.agentmux/shared");
366        let reactive = resolve_shared_reactive_dir().unwrap();
367        let registry = resolve_shared_registry_dir().unwrap();
368        assert_eq!(reactive.parent(), registry.parent());
369        clear();
370    }
371
372    #[test]
373    fn definitions_dir_uses_shared_dir() {
374        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
375        clear();
376        std::env::set_var("AGENTMUX_SHARED_DIR", "/home/user/.agentmux/shared");
377        let r = resolve_shared_definitions_dir().unwrap();
378        assert_eq!(
379            r,
380            PathBuf::from("/home/user/.agentmux/shared/agents/definitions")
381        );
382        clear();
383    }
384
385    #[test]
386    fn definitions_dir_override_wins() {
387        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
388        clear();
389        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
390        let r = resolve_shared_definitions_dir().unwrap();
391        // Override is the ~/.agentmux root; shared lives at root/shared.
392        assert_eq!(
393            r,
394            PathBuf::from("/tmp/test-home/shared/agents/definitions")
395        );
396        clear();
397    }
398
399    #[test]
400    fn shared_store_path_default_is_global_on_stable_channel() {
401        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
402        clear();
403        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
404        // Isolation flag unset on the real release channel, with an instance
405        // dir present: shared, as on every channel since
406        // SPEC_SHARED_AUTH_ACROSS_CHANNELS_2026_10_03.md.
407        std::env::set_var("AGENTMUX_CHANNEL", "stable");
408        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
409        let r = resolve_shared_store_path().unwrap();
410        assert_eq!(r, PathBuf::from("/tmp/test-home/shared/store.db"));
411        clear();
412    }
413
414    #[test]
415    fn shared_store_path_default_is_global_when_channel_unset() {
416        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
417        clear();
418        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
419        // No AGENTMUX_CHANNEL at all (e.g. bare `cargo test`) — stay
420        // global rather than guess. Isolation flag also unset.
421        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
422        let r = resolve_shared_store_path().unwrap();
423        assert_eq!(r, PathBuf::from("/tmp/test-home/shared/store.db"));
424        clear();
425    }
426
427    #[test]
428    fn shared_store_path_is_global_by_default_on_every_channel() {
429        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
430        clear();
431        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
432        // SPEC_SHARED_AUTH_ACROSS_CHANNELS_2026_10_03.md: no AGENTMUX_ISOLATED_AUTH
433        // set at all — a dev branch and a local-package build share the global
434        // store, like `stable`. Only an explicit `=1` isolates.
435        for channel in ["dev-some-branch", "local-main-b28b7a-051fbf53"] {
436            std::env::set_var("AGENTMUX_CHANNEL", channel);
437            std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
438            let r = resolve_shared_store_path().unwrap();
439            assert_eq!(
440                r,
441                PathBuf::from("/tmp/test-home/shared/store.db"),
442                "{channel}: channels share the global store by default"
443            );
444        }
445        clear();
446    }
447
448    #[test]
449    fn shared_store_path_explicit_opt_out_stays_global_on_non_stable_channel() {
450        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
451        clear();
452        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
453        std::env::set_var("AGENTMUX_CHANNEL", "dev-some-branch");
454        std::env::set_var("AGENTMUX_ISOLATED_AUTH", "0");
455        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
456        let r = resolve_shared_store_path().unwrap();
457        assert_eq!(
458            r,
459            PathBuf::from("/tmp/test-home/shared/store.db"),
460            "AGENTMUX_ISOLATED_AUTH=0 keeps the global store on a non-stable channel"
461        );
462        clear();
463    }
464
465    #[test]
466    fn shared_store_path_isolated_uses_instance_dir() {
467        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
468        clear();
469        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
470        std::env::set_var("AGENTMUX_ISOLATED_AUTH", "1");
471        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/dev/some-branch");
472        let r = resolve_shared_store_path().unwrap();
473        assert_eq!(
474            r,
475            PathBuf::from("/tmp/test-home/dev/some-branch/identity-store.db"),
476            "isolated shared store must live under the channel's own instance dir"
477        );
478        clear();
479    }
480
481    // SPEC_IDENTITY_STORE_SPLIT_2026_08_17.md — the regression class P3
482    // asks for: prove `resolve_identity_store_path` never varies by
483    // channel/isolation, the way `resolve_shared_store_path` deliberately
484    // does. This is the exact test the design doc's audit (§2.3) found
485    // missing for every non-account table in the old shared store.
486    #[test]
487    fn identity_store_path_is_global_on_stable_channel() {
488        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
489        clear();
490        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
491        std::env::set_var("AGENTMUX_CHANNEL", "stable");
492        let r = resolve_identity_store_path().unwrap();
493        assert_eq!(r, PathBuf::from("/tmp/test-home/shared/identity-store.db"));
494        clear();
495    }
496
497    #[test]
498    fn identity_store_path_stays_global_on_non_stable_channel() {
499        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
500        clear();
501        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
502        // The identity store is global whatever the channel or the isolation
503        // flag: a non-stable (local/dev/portable — i.e. every version bump)
504        // channel must NOT redirect this path anywhere per-channel.
505        std::env::set_var("AGENTMUX_CHANNEL", "local-somebranch-abcd1234-ef56789a");
506        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/channels/local-somebranch-abcd1234-ef56789a");
507        let r = resolve_identity_store_path().unwrap();
508        assert_eq!(
509            r,
510            PathBuf::from("/tmp/test-home/shared/identity-store.db"),
511            "identity store must stay global on a non-stable channel — this is the exact \
512             regression class that broke conversation continuity across a version bump \
513             (docs/specs/REPORT_HISTORY_CONTINUITY_ACROSS_VERSION_UPGRADE_2026_08_17.md)"
514        );
515        clear();
516    }
517
518    #[test]
519    fn identity_store_path_stays_global_even_with_isolated_auth_explicitly_on() {
520        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
521        clear();
522        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
523        std::env::set_var("AGENTMUX_CHANNEL", "local-somebranch-abcd1234-ef56789a");
524        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/channels/local-somebranch-abcd1234-ef56789a");
525        // Explicit isolation (the flag account testing relies on for
526        // db_accounts / identities_dir()) must have NO effect here — this
527        // store never had a legitimate isolation use case (design doc §2.3).
528        std::env::set_var("AGENTMUX_ISOLATED_AUTH", "1");
529        let r = resolve_identity_store_path().unwrap();
530        assert_eq!(r, PathBuf::from("/tmp/test-home/shared/identity-store.db"));
531        clear();
532    }
533
534    #[test]
535    fn identity_store_path_diverges_from_shared_store_path_under_isolation() {
536        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
537        clear();
538        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
539        std::env::set_var("AGENTMUX_CHANNEL", "local-somebranch-abcd1234-ef56789a");
540        std::env::set_var("AGENTMUX_INSTANCE_DIR", "/tmp/test-home/channels/local-somebranch-abcd1234-ef56789a");
541        std::env::set_var("AGENTMUX_ISOLATED_AUTH", "1");
542        // Deliberately proves the two resolvers now disagree under
543        // isolation — resolve_shared_store_path (accounts, still
544        // isolatable for account testing) redirects per-channel;
545        // resolve_identity_store_path (links/bundles/drones/muxbus) does
546        // not. If a future change accidentally re-couples them, this test
547        // fails immediately instead of silently reintroducing the bug.
548        let identity = resolve_identity_store_path().unwrap();
549        let shared = resolve_shared_store_path().unwrap();
550        assert_ne!(
551            identity, shared,
552            "identity store and shared store must resolve to DIFFERENT paths under isolation \
553             — same failure mode as identities_dir() vs identity_history_dir(), see \
554             docs/specs/SPEC_IDENTITY_STORE_SPLIT_2026_08_17.md §3.1"
555        );
556        clear();
557    }
558
559    #[test]
560    fn identity_store_path_default_is_global_when_channel_unset() {
561        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
562        clear();
563        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
564        // No AGENTMUX_CHANNEL at all — same as resolve_shared_store_path's
565        // own conservative-stays-global default, but here it's not even a
566        // "default": there's no isolation branch to fall into at all.
567        let r = resolve_identity_store_path().unwrap();
568        assert_eq!(r, PathBuf::from("/tmp/test-home/shared/identity-store.db"));
569        clear();
570    }
571
572    #[test]
573    fn identity_store_path_is_sibling_of_shared_store_path() {
574        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
575        clear();
576        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
577        std::env::set_var("AGENTMUX_CHANNEL", "stable");
578        // Both identity-store.db and store.db live directly under shared/ —
579        // NOT under shared/agents/ (that's resolve_shared_registry_dir's
580        // level, one directory deeper).
581        let identity = resolve_identity_store_path().unwrap();
582        let shared = resolve_shared_store_path().unwrap();
583        assert_eq!(identity.parent(), shared.parent());
584        clear();
585    }
586
587    #[test]
588    fn shared_store_path_isolated_without_instance_dir_falls_back() {
589        let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
590        clear();
591        std::env::set_var("AGENTMUX_HOME_OVERRIDE", "/tmp/test-home");
592        std::env::set_var("AGENTMUX_ISOLATED_AUTH", "1");
593        // AGENTMUX_INSTANCE_DIR deliberately left unset — e.g. a bare
594        // `cargo run` outside the launcher. Must degrade to the global
595        // path rather than returning None unnecessarily.
596        let r = resolve_shared_store_path().unwrap();
597        assert_eq!(r, PathBuf::from("/tmp/test-home/shared/store.db"));
598        clear();
599    }
600}