agentmux_srv_core/state.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Phase E.1b — srv reducer state. Mirrors agentmux-launcher's State
5// pattern: plain data, mutated only by the pure reducer
6// (`crate::reducer::update`). Held inside Arc<Mutex<State>> by the
7// pipe IPC server; mutex held only during reducer dispatch
8// (sub-millisecond).
9//
10// Holds lifecycle, processes, workspaces, tabs (with their layout tree
11// and focus/magnify), blocks, and the window→workspace map. SQLite is a
12// projection: `persist::bootstrap_state_from_mstore` loads it at startup
13// and the persist subscriber writes each reducer event back.
14
15use std::collections::HashMap;
16
17pub use agentmux_common::ipc::LifecyclePhase;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum ProcessState {
21 Running,
22 Exited { code: i32 },
23}
24
25#[derive(Debug, Clone)]
26pub struct ProcessRecord {
27 pub state: ProcessState,
28 #[cfg(any(test, feature = "test-support"))]
29 pub version: String,
30}
31
32/// Phase E.2 — workspace as held by the srv reducer's canonical
33/// state. Mirrors the persistent `Workspace` struct in
34/// `agentmux_srv_core::backend::obj::Workspace` but with the reducer-
35/// canonical fields the cross-process events care about.
36///
37/// Phase E.2b extends the record with `tab_ids` (ordered) and
38/// `active_tab_id`. Tabs themselves live in `state.tabs` keyed by
39/// tab_id; the workspace owns the ordering.
40#[derive(Debug, Clone, PartialEq)]
41pub struct WorkspaceRecord {
42 pub workspace_id: String,
43 pub name: String,
44 /// Ordered list of tab ids in this workspace. Mirrors the
45 /// persistent `Workspace.tabids` field.
46 pub tab_ids: Vec<String>,
47 /// The active tab in this workspace, if any. `None` when the
48 /// workspace has no tabs or has not yet had one selected.
49 pub active_tab_id: Option<String>,
50 /// The workspace's pinned panes (`agentmux_common::pin`); each one's block is a pane of one of `tab_ids`.
51 pub pins: Vec<agentmux_common::PinRecord>,
52}
53
54impl WorkspaceRecord {
55 /// A record with no tabs and no pins.
56 pub fn new(workspace_id: String, name: String) -> Self {
57 Self { workspace_id, name, tab_ids: Vec::new(), active_tab_id: None, pins: Vec::new() }
58 }
59}
60
61/// Phase E.2b — tab as held by the srv reducer's canonical state.
62/// Tabs are owned by exactly one workspace; the workspace's
63/// `tab_ids` field gives the ordering. E.3 adds `block_ids` so the
64/// tab tracks which blocks live inside it.
65// `Eq` dropped in Phase E.4.B because `LayoutNode.size: f32` precludes
66// it. Nothing in the codebase relies on `TabRecord: Eq` (no HashSet
67// usage, no `==` comparisons in the reducer).
68#[derive(Debug, Clone, PartialEq, Default)]
69pub struct TabRecord {
70 pub tab_id: String,
71 pub workspace_id: String,
72 pub name: String,
73 /// Phase E.3 — ordered list of block ids in this tab. Mirrors
74 /// the persistent `Tab.blockids` field.
75 pub block_ids: Vec<String>,
76 /// Focused layout node id; empty when no pane in this tab is
77 /// focused. Mirrors `LayoutState.focusednodeid`.
78 pub focused_node_id: String,
79 /// Phase E.4 (Option A) — magnified layout node id. Empty when
80 /// no pane is magnified (toggle-off). Mirrors
81 /// `LayoutState.magnifiednodeid`.
82 pub magnified_node_id: String,
83 /// Layout tree root; `None` is an empty tree (no panes). The
84 /// reducer is its writer (`LayoutSetTree`, `LayoutDeleteNode*`, the
85 /// pane-tab stack arms); `LayoutState.rootnode` is persisted from
86 /// their events and bootstrap-loaded at startup.
87 pub rootnode: Option<agentmux_common::LayoutNode>,
88 /// Action ids of the latest layout intents applied, oldest first and at
89 /// most `reducer::layout::RECENT_INTENTS` of them: a repeat is a no-op, so a
90 /// resent intent can't apply twice. Not persisted.
91 pub recent_intent_ids: Vec<String>,
92}
93
94/// Phase E.3 — block as held by the srv reducer's canonical state.
95/// Blocks are owned by exactly one tab; the tab's `block_ids`
96/// field gives the ordering. Block content (view, meta, runtimeopts)
97/// is intentionally not yet tracked — E.3 ships block lifecycle
98/// only; metadata + view land in a follow-up.
99#[derive(Debug, Clone, PartialEq, Eq)]
100pub struct BlockRecord {
101 pub block_id: String,
102 pub tab_id: String,
103}
104
105/// Phase E.5 — window-to-workspace mapping as held by the srv
106/// reducer's canonical state. Mirrors the persistent
107/// `Window.workspaceid` field. Used by sagas (TearOff/Restore/
108/// CreateWindow/CloseWindow) that need to coordinate the
109/// window↔workspace lifecycle atomically.
110///
111/// Note: this is NOT the same as the launcher's `state::Window` —
112/// the launcher tracks CEF window ownership (label, kind, hwnd).
113/// The srv `WindowRecord` is purely "which workspace does this
114/// window currently point at." Both are valid orthogonal projections
115/// of the same on-disk Window row.
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct WindowRecord {
118 pub window_id: String,
119 pub workspace_id: String,
120}
121
122#[derive(Debug)]
123pub struct State {
124 pub lifecycle: LifecyclePhase,
125 pub processes: HashMap<u32, ProcessRecord>,
126 pub event_version: u64,
127 pub next_client_id: u64,
128 /// Workspaces canonical to the srv reducer. Bootstrapped from
129 /// SQLite at startup; subsequent transitions flow through `update`
130 /// and are written back by the persist subscriber.
131 pub workspaces: HashMap<String, WorkspaceRecord>,
132 /// Phase E.2b — tabs canonical to the srv reducer. Keyed by
133 /// `tab_id`; ordering within a workspace is held in
134 /// `WorkspaceRecord.tab_ids`. Bootstrap-loaded from SQLite at
135 /// startup alongside workspaces.
136 pub tabs: HashMap<String, TabRecord>,
137 /// Phase E.3 — blocks canonical to the srv reducer. Keyed by
138 /// `block_id`; ordering within a tab is held in
139 /// `TabRecord.block_ids`. Bootstrap-loaded from SQLite at startup
140 /// alongside workspaces and tabs.
141 pub blocks: HashMap<String, BlockRecord>,
142 /// Phase E.5 — window→workspace mapping. Bootstrap-loaded from
143 /// SQLite Window rows. Mutated by the saga-driven CreateWindow/
144 /// CloseWindow/SwitchWorkspace commands; sagas use it to keep
145 /// window+workspace lifecycle coherent.
146 pub windows: HashMap<String, WindowRecord>,
147}
148
149impl Default for State {
150 fn default() -> Self {
151 Self {
152 lifecycle: LifecyclePhase::Starting,
153 processes: HashMap::new(),
154 event_version: 0,
155 next_client_id: 0,
156 workspaces: HashMap::new(),
157 tabs: HashMap::new(),
158 blocks: HashMap::new(),
159 windows: HashMap::new(),
160 }
161 }
162}
163
164impl State {
165 /// Increment + return the monotonic event-version counter.
166 /// Every emitted Event carries a version; subscribers detect
167 /// gaps for resync (Phase D.3).
168 pub fn bump_version(&mut self) -> u64 {
169 self.event_version = self.event_version.saturating_add(1);
170 self.event_version
171 }
172
173 /// Allocate a fresh client_id for a new Register reply.
174 pub fn alloc_client_id(&mut self) -> u64 {
175 self.next_client_id = self.next_client_id.saturating_add(1);
176 self.next_client_id
177 }
178}