Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(canvas): route node writes to the project that owns the node

Mina Sameh2eb0209e3253
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 364d35eb2..0dca21f06 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -113,6 +113,7 @@ import { Dock } from '../components/Dock'
import { TabBar } from '../components/TabBar'
import { ContextMenu, type MenuItem } from '../components/ContextMenu'
import { tidySeparators } from '../lib/tidySeparators'
+import { createNodeWriteRouter, type NodeWrites } from '../lib/nodeWriteRouter'
import {
buildAccountSwitchRows,
buildNodeActionItems,
@@ -8821,6 +8822,34 @@ export function Canvas() {
[setNodeIcon]
)
+ /** Writes to one node of `projectId`, wherever it lives right now: React Flow when it holds that
+ * project, else the projects store + disk (lib/nodeWriteRouter). Used by the kanban card menus
+ * and the Omni board, which act on nodes of projects that are not on the canvas. */
+ const nodeWritesFor = useCallback(
+ (projectId: string): NodeWrites =>
+ createNodeWriteRouter({
+ isLive: () =>
+ liveCanvasHolds(nodesProjectIdRef.current, useProjects.getState().activeProjectId, projectId),
+ live: { setColor: setNodesColor, setIcon: setNodeIcon, pickIcon: pickNodeIcon },
+ storedNode: (nodeId) =>
+ useProjects.getState().getProject(projectId)?.nodes.find((n) => n.id === nodeId),
+ recolorStored: (nodeId, color) => useProjects.getState().recolorNode(projectId, nodeId, color),
+ setStoredIcon: (nodeId, icon) =>
+ useProjects.setState((s) => ({
+ projects: s.projects.map((p) =>
+ p.id === projectId
+ ? { ...p, nodes: p.nodes.map((n) => (n.id === nodeId ? ({ ...n, icon } as never) : n)) }
+ : p
+ )
+ })),
+ persist: writeDisk,
+ iconDialog: nodeIconDialog,
+ toast: (message) =>
+ window.dispatchEvent(new CustomEvent('nodeterm:toast', { detail: { kind: 'error', message } }))
+ }),
+ [setNodesColor, setNodeIcon, pickNodeIcon, writeDisk]
+ )
+
const alignToGrid = useCallback(
(ids: string[]) => {
const g = useSettings.getState().settings.gridSize || GRID
@@ -15814,17 +15843,7 @@ export function Canvas() {
}
const onGlobalSetIcon = (e: CustomEvent<{ projectId: string; nodeId: string; icon: import('@shared/node-icon').NodeIcon | undefined }>) => {
const { projectId, nodeId, icon } = e.detail
- if (projectId === useProjects.getState().activeProjectId) {
- setNodes((ns) => ns.map((n) => (n.id === nodeId ? { ...n, data: { ...n.data, icon } } : n)))
- markDirty()
- } else {
- useProjects.setState((s) => ({
- projects: s.projects.map((p) =>
- p.id === projectId ? { ...p, nodes: p.nodes.map((n) => (n.id === nodeId ? { ...n, icon } as never : n)) } : p
- )
- }))
- void writeDisk()
- }
+ nodeWritesFor(projectId).setIcon(nodeId, icon)
}
window.addEventListener('nodeterm:global-rename' as never, onGlobalRename as never)
window.addEventListener('nodeterm:global-edit-sticky' as never, onGlobalEditSticky as never)
@@ -15838,7 +15857,7 @@ export function Canvas() {
window.removeEventListener('nodeterm:global-delete' as never, onGlobalDelete as never)
window.removeEventListener('nodeterm:global-set-icon' as never, onGlobalSetIcon as never)
}
- }, [renameSession, setNodes, markDirty, writeDisk, deleteNodeFromKanban, closeStoredNodes])
+ }, [renameSession, setNodes, markDirty, writeDisk, deleteNodeFromKanban, closeStoredNodes, nodeWritesFor])
// Sidebar "Name with AI": generate a title from the session's captured terminal output
// (same BYO-agent path as the terminal node's ✦), then apply it via renameSession.
diff --git a/src/renderer/lib/nodeWriteRouter.test.ts b/src/renderer/lib/nodeWriteRouter.test.ts
new file mode 100644
index 000000000..56c733d91
--- /dev/null
+++ b/src/renderer/lib/nodeWriteRouter.test.ts
@@ -0,0 +1,78 @@
+import { describe, expect, it, vi } from 'vitest'
+import type { CanvasNodeState } from '@shared/types'
+import {
+ createNodeWriteRouter,
+ NODE_GONE_MESSAGE,
+ SAVE_FAILED_MESSAGE,
+ type NodeWriteRouterDeps
+} from './nodeWriteRouter'
+
+const stored = (id: string): CanvasNodeState => ({ id, title: `T ${id}` }) as unknown as CanvasNodeState
+const flush = (): Promise<void> => new Promise((r) => setTimeout(r, 0))
+
+function deps(over: Partial<NodeWriteRouterDeps> = {}): NodeWriteRouterDeps {
+ return {
+ isLive: () => false,
+ live: { setColor: vi.fn(), setIcon: vi.fn(), pickIcon: vi.fn() },
+ storedNode: (id) => (id === 'n1' ? stored('n1') : undefined),
+ recolorStored: vi.fn(),
+ setStoredIcon: vi.fn(),
+ persist: vi.fn(async () => true),
+ iconDialog: vi.fn(async () => ({ type: 'emoji', value: '🚀' }) as never),
+ toast: vi.fn(),
+ ...over
+ }
+}
+
+describe('createNodeWriteRouter', () => {
+ it('the project on the canvas writes through the live funnels, never the store', () => {
+ const d = deps({ isLive: () => true })
+ const w = createNodeWriteRouter(d)
+ w.setColor(['n1'], '#0a84ff')
+ w.pickIcon('n1')
+ expect(d.live.setColor).toHaveBeenCalledWith(['n1'], '#0a84ff')
+ expect(d.live.pickIcon).toHaveBeenCalledWith('n1')
+ expect(d.recolorStored).not.toHaveBeenCalled()
+ expect(d.persist).not.toHaveBeenCalled()
+ })
+
+ it('a project off the canvas writes to the store, then to disk', async () => {
+ const d = deps()
+ createNodeWriteRouter(d).setColor(['n1'], '#0a84ff')
+ await flush()
+ expect(d.recolorStored).toHaveBeenCalledWith('n1', '#0a84ff')
+ expect(d.persist).toHaveBeenCalledTimes(1)
+ expect(d.toast).not.toHaveBeenCalled()
+ })
+
+ it('a node that is gone is reported, not silently skipped', () => {
+ const d = deps()
+ createNodeWriteRouter(d).setColor(['gone'], '#0a84ff')
+ expect(d.toast).toHaveBeenCalledWith(NODE_GONE_MESSAGE)
+ expect(d.recolorStored).not.toHaveBeenCalled()
+ expect(d.persist).not.toHaveBeenCalled()
+ })
+
+ it('a refused save is reported', async () => {
+ const d = deps({ persist: vi.fn(async () => false) })
+ createNodeWriteRouter(d).setIcon('n1', undefined)
+ await flush()
+ expect(d.setStoredIcon).toHaveBeenCalledWith('n1', undefined)
+ expect(d.toast).toHaveBeenCalledWith(SAVE_FAILED_MESSAGE)
+ })
+
+ it('the icon picker for a stored node writes its answer to the store', async () => {
+ const d = deps()
+ createNodeWriteRouter(d).pickIcon('n1')
+ await flush()
+ expect(d.iconDialog).toHaveBeenCalledWith({ nodeId: 'n1', title: 'T n1', icon: undefined })
+ expect(d.setStoredIcon).toHaveBeenCalledWith('n1', { type: 'emoji', value: '🚀' })
+ })
+
+ it('a cancelled picker writes nothing', async () => {
+ const d = deps({ iconDialog: vi.fn(async () => undefined) })
+ createNodeWriteRouter(d).pickIcon('n1')
+ await flush()
+ expect(d.setStoredIcon).not.toHaveBeenCalled()
+ })
+})
diff --git a/src/renderer/lib/nodeWriteRouter.ts b/src/renderer/lib/nodeWriteRouter.ts
new file mode 100644
index 000000000..94b838771
--- /dev/null
+++ b/src/renderer/lib/nodeWriteRouter.ts
@@ -0,0 +1,87 @@
+/**
+ * Writes to ONE node of ONE project, routed to wherever that node lives right now.
+ *
+ * React Flow is the source of truth only for the project it currently holds (`liveCanvasHolds`);
+ * every other project's nodes live in the projects store, and a write there must go to the store
+ * and then to disk, or it is lost (a live-canvas callback like `setNodesColor` simply finds no such
+ * node and does nothing). That is the trap the kanban card menus — the Omni board above all — walk
+ * into, because they act on nodes of projects that are not on the canvas.
+ *
+ * Never silent: a node that is no longer in its project, and a save that fails, each raise a
+ * `nodeterm:toast` (the caller's `toast`).
+ */
+import type { NodeIcon } from '@shared/node-icon'
+import type { CanvasNodeState } from '@shared/types'
+import type { NodeIconChoice } from '../components/NodeIconPicker'
+import { applyIconChoice } from './nodeIconChoice'
+
+export const NODE_GONE_MESSAGE = 'That session is no longer in its project — nothing was changed.'
+export const SAVE_FAILED_MESSAGE = 'The change could not be saved to disk.'
+
+export interface NodeWrites {
+ setColor: (ids: string[], color: string) => void
+ setIcon: (nodeId: string, icon: NodeIcon | undefined) => void
+ /** Open the icon picker for the node, then write the answer. */
+ pickIcon: (nodeId: string) => void
+}
+
+export interface NodeWriteRouterDeps {
+ /** React Flow holds this project right now. Asked on every write, never cached. */
+ isLive: () => boolean
+ /** The live canvas's own funnels (Canvas `setNodesColor` / `setNodeIcon` / `pickNodeIcon`). */
+ live: NodeWrites
+ /** The node in the project's stored copy, or undefined when it is gone. */
+ storedNode: (nodeId: string) => CanvasNodeState | undefined
+ recolorStored: (nodeId: string, color: string) => void
+ setStoredIcon: (nodeId: string, icon: NodeIcon | undefined) => void
+ /** Canvas `writeDisk`: false when the save was refused. */
+ persist: () => Promise<boolean>
+ iconDialog: (opts: { nodeId: string; title: string; icon?: NodeIcon }) => Promise<NodeIconChoice>
+ toast: (message: string) => void
+}
+
+export function createNodeWriteRouter(d: NodeWriteRouterDeps): NodeWrites {
+ const save = async (): Promise<void> => {
+ if (!(await d.persist())) d.toast(SAVE_FAILED_MESSAGE)
+ }
+ const setIcon = (nodeId: string, icon: NodeIcon | undefined): void => {
+ if (d.isLive()) {
+ d.live.setIcon(nodeId, icon)
+ return
+ }
+ if (!d.storedNode(nodeId)) {
+ d.toast(NODE_GONE_MESSAGE)
+ return
+ }
+ d.setStoredIcon(nodeId, icon)
+ void save()
+ }
+ return {
+ setColor(ids, color) {
+ if (d.isLive()) {
+ d.live.setColor(ids, color)
+ return
+ }
+ const present = ids.filter((id) => d.storedNode(id))
+ if (present.length < ids.length) d.toast(NODE_GONE_MESSAGE)
+ if (present.length === 0) return
+ for (const id of present) d.recolorStored(id, color)
+ void save()
+ },
+ setIcon,
+ pickIcon(nodeId) {
+ if (d.isLive()) {
+ d.live.pickIcon(nodeId)
+ return
+ }
+ const node = d.storedNode(nodeId)
+ if (!node) {
+ d.toast(NODE_GONE_MESSAGE)
+ return
+ }
+ void d
+ .iconDialog({ nodeId, title: node.title ?? '', icon: node.icon })
+ .then((choice) => applyIconChoice(choice, (icon) => setIcon(nodeId, icon)))
+ }
+ }
+}