Skip to main content

agentmux_srv/server/service/
workspace.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `workspace` service handler (workspace + tab lifecycle, drag-and-drop moves).
5//!
6//! The dispatcher below just routes each method to its handler; the
7//! handler bodies live in sibling modules:
8//! * [`super::workspace_lifecycle`] — `CreateWorkspace` / `GetWorkspace` /
9//!   `DeleteWorkspace` / `UpdateWorkspace`.
10//! * [`super::tab_lifecycle`] — `CreateTab` / `SetActiveTab` / `CloseTab` /
11//!   `UpdateTabIds` / `ReorderTab`.
12//! * [`super::tab_move`] — `MoveBlockToTab` / `PromoteBlockToTab` /
13//!   `MoveTabToWorkspace` / `RestoreTornOffTab`.
14//! * [`super::tear_off`] — `TearOffBlock` / `RedockFloatingPane` /
15//!   `TearOffTab`.
16
17use crate::backend::service::{WebCallType, WebReturnType};
18use crate::backend::wcore;
19
20use super::super::AppState;
21use super::tab_lifecycle::{
22    handle_close_tab, handle_create_tab, handle_reorder_tab, handle_set_active_tab,
23    handle_update_tab_ids,
24};
25use super::tab_move::{
26    handle_move_block_to_tab, handle_move_tab_to_workspace, handle_promote_block_to_tab,
27    handle_restore_torn_off_tab,
28};
29use super::pin::{handle_pin_block_to_window, handle_unpin_block_from_window};
30use super::tear_off::{handle_redock_floating_pane, handle_tear_off_block, handle_tear_off_tab};
31use super::workspace_lifecycle::{
32    handle_create_workspace, handle_delete_workspace, handle_get_workspace,
33    handle_update_workspace,
34};
35
36pub(super) async fn handle_workspace_service(state: &AppState, call: &WebCallType) -> WebReturnType {
37    let store = &state.mstore;
38    // Phase E.2c.2 — workspace lifecycle dispatches through the
39    // srv reducer for event emission (sagas / renderer / persist
40    // subscriber consume them) AND synchronously applies the
41    // emitted events to SQLite via the subscriber's apply path.
42    // Synchronous SQLite writes are required during the migration
43    // window because tab/block RPC still hits wcore directly and
44    // expects workspaces to be present in SQLite by the time the
45    // RPC reply returns (e.g., a CreateTab call right after
46    // CreateWorkspace would 404 on the workspace lookup if we
47    // only relied on the async subscriber). The subscriber later
48    // receives the same event on the broadcast bus and re-applies
49    // idempotently — safe because each apply arm checks SQLite
50    // state before writing. (Both reagent + codex flagged this
51    // race as P1 #615.)
52    //
53    // Reads (`GetWorkspace` / `ListWorkspaces`) stay on mstore
54    // until the tab + block RPC layers also migrate (E.2c.3 +
55    // E.2c.4). The reducer's `WorkspaceRecord` doesn't track
56    // `pinnedtabids` and its `tabids` / `activetabid` go stale
57    // immediately after any wcore-direct tab op — reading from
58    // it before tabs are migrated returns wrong data.
59    match call.method.as_str() {
60        "CreateWorkspace" => handle_create_workspace(state, call).await,
61        "GetWorkspace" => handle_get_workspace(state, call).await,
62        "DeleteWorkspace" => handle_delete_workspace(state, call).await,
63        "ListWorkspaces" => match wcore::list_workspaces(store) {
64            Ok(list) => WebReturnType::success(serde_json::to_value(&list).unwrap_or_default()),
65            Err(e) => WebReturnType::error(e.to_string()),
66        },
67        "CreateTab" => handle_create_tab(state, call).await,
68        "SetActiveTab" => handle_set_active_tab(state, call).await,
69        "CloseTab" => handle_close_tab(state, call).await,
70        "UpdateWorkspace" => handle_update_workspace(state, call).await,
71        "UpdateTabIds" => handle_update_tab_ids(state, call).await,
72        "MoveBlockToTab" => handle_move_block_to_tab(state, call).await,
73        "PinBlockToWindow" => handle_pin_block_to_window(state, call).await,
74        "UnpinBlockFromWindow" => handle_unpin_block_from_window(state, call).await,
75        "PromoteBlockToTab" => handle_promote_block_to_tab(state, call).await,
76        "ReorderTab" => handle_reorder_tab(state, call).await,
77        "MoveTabToWorkspace" => handle_move_tab_to_workspace(state, call).await,
78        "RestoreTornOffTab" => handle_restore_torn_off_tab(state, call).await,
79        "TearOffBlock" => handle_tear_off_block(state, call).await,
80        "RedockFloatingPane" => handle_redock_floating_pane(state, call).await,
81        "TearOffTab" => handle_tear_off_tab(state, call).await,
82        // The user's workspace, a named set of windows (`user_workspace`), not
83        // the window's contents the methods above act on.
84        "Flush" => super::user_workspace::handle_flush(state, call).await,
85        "RenameUserWorkspace" => super::user_workspace::handle_rename(state, call),
86        "ListUserWorkspaces" => super::user_workspace_switch::handle_list(state, call),
87        "CreateUserWorkspace" => super::user_workspace_switch::handle_create(state, call).await,
88        "SwitchUserWorkspace" => super::user_workspace_switch::handle_switch(state, call).await,
89        "DeleteUserWorkspace" => super::user_workspace_switch::handle_delete(state, call).await,
90        "UserWorkspaceAgents" => super::user_workspace_switch::handle_agents(state, call),
91        "ExportUserWorkspace" => super::user_workspace_files::handle_export(state, call).await,
92        "PreviewImport" => super::user_workspace_files::handle_preview(state, call).await,
93        "ImportUserWorkspace" => super::user_workspace_files::handle_import(state, call).await,
94        _ => WebReturnType::error(format!("unknown workspace method: {}", call.method)),
95    }
96}