agentmux_srv\reducer/
snapshot.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4use agentmux_common::ipc::Event;
5
6use crate::state::State;
7
8
9pub(super) fn handle_get_srv_snapshot(state: &mut State) -> Vec<Event> {
10    let v = state.bump_version();
11    let mut workspaces: Vec<(String, String)> = state
12        .workspaces
13        .values()
14        .map(|w| (w.workspace_id.clone(), w.name.clone()))
15        .collect();
16    // Stable ordering for diffability — reducer state is HashMap so
17    // iteration order is non-deterministic.
18    workspaces.sort_by(|a, b| a.0.cmp(&b.0));
19    let mut tabs: Vec<(String, String, String)> = state
20        .tabs
21        .values()
22        .map(|t| (t.tab_id.clone(), t.workspace_id.clone(), t.name.clone()))
23        .collect();
24    tabs.sort_by(|a, b| a.0.cmp(&b.0));
25    let mut active_tabs: Vec<(String, String)> = state
26        .workspaces
27        .values()
28        .filter_map(|w| {
29            w.active_tab_id
30                .as_ref()
31                .map(|t| (w.workspace_id.clone(), t.clone()))
32        })
33        .collect();
34    active_tabs.sort_by(|a, b| a.0.cmp(&b.0));
35    let mut blocks: Vec<(String, String)> = state
36        .blocks
37        .values()
38        .map(|b| (b.block_id.clone(), b.tab_id.clone()))
39        .collect();
40    blocks.sort_by(|a, b| a.0.cmp(&b.0));
41    vec![Event::SrvSnapshot {
42        version: v,
43        lifecycle: state.lifecycle,
44        workspaces,
45        tabs,
46        active_tabs,
47        blocks,
48    }]
49}