agentmux_common/data_paths/isolation.rs
1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The isolated-auth and isolated-settings switches: whether a channel keeps its own
5//! accounts, credential folders and settings, or shares them.
6
7/// Isolated per-channel auth (identity accounts + OAuth credential dirs).
8/// Read directly at every call site rather than cached on `DataPaths` so
9/// `identities_dir()` behaves consistently regardless of whether the
10/// caller built its `DataPaths` via `resolve()` (launcher) or
11/// `from_env()` (downstream host/srv).
12///
13/// Resolution order:
14/// 1. `AGENTMUX_ISOLATED_AUTH=1` — isolate this channel's auth. Meant for
15/// testing the login and relogin paths from an empty store.
16/// 2. Any other value of `AGENTMUX_ISOLATED_AUTH` — shared (explicit opt-out).
17/// 3. Unset — **shared, on every channel**. A new build, a rebuilt branch,
18/// `task dev` and a release all read the same accounts, provider login
19/// folders, bundles, drones, native memory, Global Memory and MuxBus
20/// sign-in, so a new build needs no login
21/// (`SPEC_SHARED_AUTH_ACROSS_CHANNELS_2026_10_03.md`). Until 2026-10-03 every
22/// channel except `stable` defaulted to isolated
23/// (`SPEC_ISOLATED_AUTH_DEFAULT_BY_CHANNEL_2026_08_06.md`).
24///
25/// See `docs/specs/SPEC_ISOLATED_AUTH_DEV_TESTING_2026_07_27.md` (the
26/// underlying mechanism — channel-scoped store + credential dirs — this
27/// flag drives, still authoritative).
28pub fn isolated_auth_enabled() -> bool {
29 isolated_auth_reason().is_isolated()
30}
31
32/// Which rule decided [`isolated_auth_enabled`]'s result — for boot-time
33/// diagnostics (see `bootstrap/stores.rs`'s "shared store: attached" log line)
34/// so a developer staring at an empty Connectors → Accounts can tell at a glance whether
35/// that is an explicit choice, rather than re-deriving it from the env by
36/// hand. Callers that only need the boolean should use
37/// [`isolated_auth_enabled`] directly — this exists purely so the two never
38/// drift (one resolution, two views).
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum IsolatedAuthReason {
41 /// `AGENTMUX_ISOLATED_AUTH=1`.
42 ExplicitOptIn,
43 /// `AGENTMUX_ISOLATED_AUTH` is set to anything other than exactly `"1"`
44 /// (`"0"`, `""`, a typo like `"false"`, anything): shared. Fail-safe by
45 /// construction: a malformed opt-in can never silently isolate a channel
46 /// (#2431), and since 2026-10-03 the unset default is shared as well.
47 ExplicitOptOut,
48 /// No override: shared, on every channel.
49 DefaultShared,
50}
51
52impl IsolatedAuthReason {
53 pub fn is_isolated(self) -> bool {
54 matches!(self, Self::ExplicitOptIn)
55 }
56
57 /// Short, log-friendly label.
58 pub fn as_str(self) -> &'static str {
59 match self {
60 Self::ExplicitOptIn => "explicit opt-in",
61 Self::ExplicitOptOut => "explicit opt-out",
62 Self::DefaultShared => "default — shared",
63 }
64 }
65}
66
67pub fn isolated_auth_reason() -> IsolatedAuthReason {
68 match std::env::var("AGENTMUX_ISOLATED_AUTH") {
69 // Exactly "1" isolates; any other value the var is explicitly set to
70 // ("0", "", a typo) is an explicit opt-out.
71 Ok(v) if v == "1" => IsolatedAuthReason::ExplicitOptIn,
72 Ok(_) => IsolatedAuthReason::ExplicitOptOut,
73 Err(_) => IsolatedAuthReason::DefaultShared,
74 }
75}
76
77/// Isolated per-channel `settings.json`. Same shape and same reasoning as
78/// [`isolated_auth_enabled`] — see
79/// `docs/specs/SPEC_SETTINGS_ISOLATED_BY_CHANNEL_2026_08_19.md`.
80///
81/// Resolution order:
82/// 1. `AGENTMUX_ISOLATED_SETTINGS=1` / `=0` — explicit override, always wins.
83/// 2. Otherwise, defaults to isolated for every channel except `"stable"`.
84/// `stable` is the real release channel — the daily-driver install(s)
85/// this machine's actual work depends on — and keeps the old
86/// always-global behavior, so nobody's real window theme/pinned
87/// widgets/voice API key gets silently blanked by a channel-name
88/// coincidence. Every `task dev` branch and every `task package` local
89/// build now starts with a genuinely default `settings.json` — the
90/// motivating case was `network:lan_discovery` silently carrying
91/// `true` into a brand-new build from a decision made in an unrelated
92/// channel weeks earlier.
93/// 3. If `AGENTMUX_CHANNEL` isn't set yet, stays global — conservative
94/// default when channel context is unknown, not a guess.
95///
96/// Deliberately a SEPARATE flag from `AGENTMUX_ISOLATED_AUTH`, not a
97/// shared `AGENTMUX_ISOLATED` umbrella — see the spec's Open Questions
98/// §1 for why.
99pub fn isolated_settings_enabled() -> bool {
100 isolated_settings_reason().is_isolated()
101}
102
103/// Which rule decided [`isolated_settings_enabled`]'s result — for
104/// boot-time diagnostics, mirroring [`IsolatedAuthReason`] exactly (see
105/// that type's doc comment for the rationale: one resolution, two views,
106/// so the boolean and the log line can never drift apart).
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub enum IsolatedSettingsReason {
109 /// `AGENTMUX_ISOLATED_SETTINGS=1`.
110 ExplicitOptIn,
111 /// `AGENTMUX_ISOLATED_SETTINGS` is set to anything other than exactly
112 /// `"1"` — fail-safe by construction, same rule as
113 /// `IsolatedAuthReason::ExplicitOptOut` and for the same reason: a
114 /// typo'd opt-out attempt must not silently isolate a non-stable
115 /// channel instead of falling back to the safe (global) state.
116 ExplicitOptOut,
117 /// No override; `AGENTMUX_CHANNEL` is set and isn't `"stable"`.
118 ChannelDefaultIsolated,
119 /// No override; `AGENTMUX_CHANNEL` is `"stable"` or unset entirely.
120 ChannelDefaultGlobal,
121}
122
123impl IsolatedSettingsReason {
124 pub fn is_isolated(self) -> bool {
125 matches!(self, Self::ExplicitOptIn | Self::ChannelDefaultIsolated)
126 }
127
128 /// Short, log-friendly label.
129 pub fn as_str(self) -> &'static str {
130 match self {
131 Self::ExplicitOptIn => "explicit opt-in",
132 Self::ExplicitOptOut => "explicit opt-out",
133 Self::ChannelDefaultIsolated => "channel default — isolated",
134 Self::ChannelDefaultGlobal => "channel default — global",
135 }
136 }
137}
138
139/// Reads both inputs from the process env; the rule itself is
140/// [`crate::settings_path::isolated_settings_reason_for`].
141pub fn isolated_settings_reason() -> IsolatedSettingsReason {
142 let opt = std::env::var(crate::settings_path::ISOLATED_SETTINGS_ENV).ok();
143 crate::settings_path::isolated_settings_reason_for(opt.as_deref(), std::env::var("AGENTMUX_CHANNEL").ok().as_deref())
144}