Skip to main content

agentmux_srv_core/backend/wcore/
block.rs

1#![allow(dead_code)]
2// Copyright 2025-2026, AgentMux Corp.
3// SPDX-License-Identifier: Apache-2.0
4
5//! Block CRUD operations.
6
7use uuid::Uuid;
8
9use crate::backend::storage::store::Store;
10use crate::backend::storage::StoreError;
11use crate::backend::obj::*;
12
13/// Create a new block in a tab.
14pub fn create_block(
15    store: &Store,
16    tab_id: &str,
17    meta: MetaMapType,
18) -> Result<Block, StoreError> {
19    let mut tab = store.must_get::<Tab>(tab_id)?;
20
21    let mut block = Block {
22        oid: Uuid::new_v4().to_string(),
23        parentoref: format!("tab:{}", tab_id),
24        meta,
25        ..Default::default()
26    };
27    store.insert(&mut block)?;
28
29    tab.blockids.push(block.oid.clone());
30    store.update(&mut tab)?;
31
32    Ok(block)
33}
34
35/// Delete a block's rows: remove it from its parent tab's `blockids` and
36/// delete the Block row.
37///
38/// SPEC_864 site #6 — the layout-tree prune that used to live here
39/// (wcore-direct `LayoutState` write, the last Path-B writer on the
40/// block-delete path) moved to the reducer: the `delete_block` saga
41/// dispatches `LayoutDeleteNodeByBlock` as its Step 2, and the persist
42/// subscriber writes the reducer's resulting tree from the
43/// `LayoutNodeDeleted{new_tree, tree_cleared}` event. This fn is row-ops
44/// only, keeping `db_layout` single-writer. SPEC_864 Phase 5 deleted the
45/// `prune_block_from_layout`/`heal_layout` backstop that used to live
46/// below — no Path-B writer remains to produce the orphans it swept.
47pub fn delete_block(
48    store: &Store,
49    tab_id: &str,
50    block_id: &str,
51) -> Result<(), StoreError> {
52    let mut tab = store.must_get::<Tab>(tab_id)?;
53    tab.blockids.retain(|id| id != block_id);
54    store.update(&mut tab)?;
55    store.delete::<Block>(block_id)?;
56    forget_block_state(block_id);
57    Ok(())
58}
59
60/// Delete a parked block's row: it is in no tab, so there is no tab to update.
61pub fn delete_parked_block(store: &Store, block_id: &str) -> Result<(), StoreError> {
62    store.delete::<Block>(block_id)?;
63    forget_block_state(block_id);
64    Ok(())
65}
66
67static BLOCK_GONE_HOOK: std::sync::OnceLock<fn(&str)> = std::sync::OnceLock::new();
68
69/// Sets what the rest of srv forgets about a block that is gone; the server calls it once at
70/// startup (`server::browser_pane_gone`), since this layer sits below what keeps that state.
71pub fn set_block_gone_hook(hook: fn(&str)) {
72    let _ = BLOCK_GONE_HOOK.set(hook);
73}
74
75/// What the rest of srv keeps about a block that is gone.
76fn forget_block_state(block_id: &str) {
77    if let Some(hook) = BLOCK_GONE_HOOK.get() {
78        hook(block_id);
79    }
80}
81
82/// Resolve a block ID from an 8-character prefix within a tab.
83pub fn resolve_block_id_from_prefix(
84    store: &Store,
85    tab_id: &str,
86    prefix: &str,
87) -> Result<String, StoreError> {
88    if prefix.len() != 8 {
89        return Err(StoreError::Other(
90            "block_id prefix must be 8 characters".to_string(),
91        ));
92    }
93    let tab = store.must_get::<Tab>(tab_id)?;
94    for block_id in &tab.blockids {
95        if block_id.starts_with(prefix) {
96            return Ok(block_id.clone());
97        }
98    }
99    Err(StoreError::NotFound)
100}