agentmux_srv_core/registry/work_store.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Whether a build keeps its own work store: the Kanban queue and what hangs
5//! off it (cards, events, Presence claims, coordination posts, the Jobs run
6//! ledger, scheduled jobs).
7//!
8//! Those tables live in files every build on the host opens: the identity
9//! store and the shared store. They are shared on purpose, so the agents of
10//! every build the operator runs, `stable` or a `task package` one, work from
11//! one queue. A `task dev` build is the exception: it is for testing, and
12//! reading the operator's real queue meant a task it created, claimed or
13//! completed was the operator's own. So a source-tree build gets a
14//! `work-store.db` of its own by default.
15
16/// `1` gives the build its own work store, any other value shares the host's.
17pub const ISOLATED_WORK_ENV: &str = "AGENTMUX_ISOLATED_WORK";
18
19/// Why the build does or doesn't have a work store of its own.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum IsolatedWorkReason {
22 /// `AGENTMUX_ISOLATED_WORK=1`.
23 ExplicitOptIn,
24 /// `AGENTMUX_ISOLATED_WORK` is set to anything else: shared. A typo can't
25 /// isolate by accident, the rule `AGENTMUX_ISOLATED_AUTH` follows.
26 ExplicitOptOut,
27 /// No override; this is a source-tree (`task dev`) build.
28 DevBuild,
29 /// No override; an installed or `task package` build: shared.
30 SharedBuild,
31}
32
33impl IsolatedWorkReason {
34 pub fn is_isolated(self) -> bool {
35 matches!(self, Self::ExplicitOptIn | Self::DevBuild)
36 }
37
38 /// Short, log-friendly label.
39 pub fn as_str(self) -> &'static str {
40 match self {
41 Self::ExplicitOptIn => "explicit opt-in",
42 Self::ExplicitOptOut => "explicit opt-out",
43 Self::DevBuild => "dev build — isolated",
44 Self::SharedBuild => "default — shared",
45 }
46 }
47}
48
49/// The rule, with its inputs passed in: `opt` is `AGENTMUX_ISOLATED_WORK` (if
50/// set) and `dev_build` whether this binary is a source-tree build.
51pub fn isolated_work_reason_for(opt: Option<&str>, dev_build: bool) -> IsolatedWorkReason {
52 match (opt, dev_build) {
53 (Some("1"), _) => IsolatedWorkReason::ExplicitOptIn,
54 (Some(_), _) => IsolatedWorkReason::ExplicitOptOut,
55 (None, true) => IsolatedWorkReason::DevBuild,
56 (None, false) => IsolatedWorkReason::SharedBuild,
57 }
58}
59
60/// Reads the env, and asks the binary's own path whether it is a dev build
61/// ([`agentmux_common::runtime_mode::is_dev_self`]), not `AGENTMUX_RUNTIME_MODE`: a
62/// packaged build started from a pane inside a dev instance inherits that.
63pub fn isolated_work_reason() -> IsolatedWorkReason {
64 let opt = std::env::var(ISOLATED_WORK_ENV).ok();
65 isolated_work_reason_for(opt.as_deref(), agentmux_common::runtime_mode::is_dev_self())
66}
67
68/// Whether this build keeps its own work store.
69pub fn isolated_work_enabled() -> bool {
70 isolated_work_reason().is_isolated()
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn a_dev_build_keeps_its_own() {
79 assert_eq!(isolated_work_reason_for(None, true), IsolatedWorkReason::DevBuild);
80 assert!(isolated_work_reason_for(None, true).is_isolated());
81 }
82
83 /// `stable` and the `local-*` builds agents run on share one queue.
84 #[test]
85 fn installed_and_packaged_builds_share_the_hosts() {
86 assert!(!isolated_work_reason_for(None, false).is_isolated());
87 }
88
89 #[test]
90 fn the_override_wins_either_way() {
91 assert!(isolated_work_reason_for(Some("1"), false).is_isolated());
92 assert!(!isolated_work_reason_for(Some("0"), true).is_isolated());
93 assert!(!isolated_work_reason_for(Some(""), true).is_isolated());
94 // A typo shares rather than isolates.
95 assert!(!isolated_work_reason_for(Some("true"), false).is_isolated());
96 }
97}