Skip to main content

agentmux_srv_core/reducer/layout/
heal.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Heal a layout tree loaded from disk. A row written by an earlier build
5//! can hold a leaf for a block the tab no longer owns (moved to another tab,
6//! or deleted); the window used to prune those itself after loading. Now
7//! srv does, once, at bootstrap. SPEC_STRONG_REDUCER_AUTHORITY_LAYOUT_2026_06_30.md ยง7, Phase 6.
8
9use agentmux_common::ipc::Event;
10
11use super::{clear_dangling_focus_magnify, report_layout_violations};
12use crate::backend::layout::intent::migrate_legacy_minimize;
13use crate::state::State;
14
15/// Migrate the pre-display-mode minimize fields of `tab_id`'s tree, remove
16/// the leaves whose block the tab doesn't own, then normalize the tree as a
17/// delete intent leaves it (balanced) and clear a focus/magnify id left
18/// pointing at nothing. A tree with nothing to migrate or remove is left
19/// exactly as loaded. The window no longer migrates a tree itself: every tree
20/// it is given has been through here or through an intent.
21///
22/// `prune` is false when the tab's block ids could not be trusted (the blocks
23/// could not be read): the minimize fields are still migrated, no leaf is removed.
24///
25/// Returns the events that persist the change (empty when nothing changed),
26/// with versions taken the way an intent's are.
27pub(crate) fn heal_loaded_layout(state: &mut State, tab_id: &str, prune: bool) -> Vec<Event> {
28    let Some(tab) = state.tabs.get_mut(tab_id) else { return Vec::new() };
29    let owned: std::collections::HashSet<String> = tab.block_ids.iter().cloned().collect();
30    let migrated = tab.rootnode.as_mut().map_or(0, migrate_legacy_minimize);
31    let pruned = if prune { crate::backend::layout::prune_dangling_block_refs(&mut tab.rootnode, &owned) } else { 0 };
32    if migrated == 0 && pruned == 0 {
33        return Vec::new();
34    }
35    if let Some(root) = tab.rootnode.as_mut() {
36        if let Err(e) = crate::backend::layout::balance_node(root) {
37            tracing::warn!(tab_id, error = %e, "layout heal: balance failed; keeping the healed tree");
38        }
39    }
40    let (was_focused, was_magnified) = clear_dangling_focus_magnify(tab);
41    tracing::warn!(tab_id, migrated, pruned, "layout heal: migrated legacy minimize fields, removed leaves for blocks the tab doesn't own");
42    report_layout_violations(tab_id, "bootstrap heal", &tab.rootnode);
43    let new_tree = tab.rootnode.clone();
44
45    let mut events = Vec::new();
46    let v = state.bump_version();
47    events.push(Event::LayoutTreeReplaced {
48        tab_id: tab_id.to_string(),
49        new_tree,
50        correlation_id: String::new(),
51        slices: None,
52        version: v,
53    });
54    if was_focused {
55        let v = state.bump_version();
56        events.push(Event::FocusedNodeChanged { tab_id: tab_id.to_string(), node_id: String::new(), version: v });
57    }
58    if was_magnified {
59        let v = state.bump_version();
60        events.push(Event::MagnifiedNodeChanged { tab_id: tab_id.to_string(), node_id: String::new(), version: v });
61    }
62    events
63}