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.

fix(worktree): a displaced editor/diff node shows a notice, not a dead read

enes25f62e5ccb85
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 40e2b39ea..e69622113 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -2043,14 +2043,20 @@ export function Canvas() {
/**
* Move every node that was living in a worktree directory OFF that (now dead) path — back to the
* group's own cwd, else the project's. `displacedByWorktree` decides who those are: descendants
- * (nested groups included) whose cwd is inside the worktree; anyone else is left alone.
+ * (nested groups included) whose cwd is inside the worktree, PLUS any editor/diff node anywhere
+ * on the canvas whose file was inside it.
*
* Leaving a dead `data.cwd` behind is the trap this whole task exists to remove — it is persisted
* to project.json, tmux hides it (a warm reattach ignores cwd), and the next machine reboot cold-
* starts the terminal into a directory that no longer exists, where pty-manager silently falls
* back to $HOME while the dead path stays in the project file forever.
*
- * `respawn` separates the two callers:
+ * Editor/diff nodes get different treatment: unlike a terminal's cwd, there is no fallback to
+ * re-point a dead `filePath` AT — the file itself no longer exists — so they are marked
+ * `data.fileMissing` instead of rewritten. The node stays on the canvas (never auto-closed: it
+ * may hold unsaved Monaco edits the user hasn't copied out yet) and renders a persistent notice.
+ *
+ * `respawn` separates the two callers (terminal/chat only — editor/diff has no session to touch):
* - Remove (true): the directory is being deleted under live sessions, so their tmux sessions are
* destroyed and the terminals respawn straight into the fallback cwd.
* - Stale Unbind (false): unbind touches no process, by definition. The dead path is corrected on
@@ -2077,20 +2083,22 @@ export function Canvas() {
}
}
setNodes((ns) =>
- ns.map((n) =>
- displaced.has(n.id)
- ? {
- ...n,
- data: {
- ...n.data,
- cwd: fallbackCwd,
- ...(respawn && n.type === 'terminal'
- ? { respawnNonce: ((n.data.respawnNonce as number | undefined) ?? 0) + 1 }
- : {})
- }
- }
- : n
- )
+ ns.map((n) => {
+ if (!displaced.has(n.id)) return n
+ if (n.type === 'editor' || n.type === 'diff') {
+ return { ...n, data: { ...n.data, fileMissing: true } }
+ }
+ return {
+ ...n,
+ data: {
+ ...n.data,
+ cwd: fallbackCwd,
+ ...(respawn && n.type === 'terminal'
+ ? { respawnNonce: ((n.data.respawnNonce as number | undefined) ?? 0) + 1 }
+ : {})
+ }
+ }
+ })
)
markDirty()
},
@@ -2104,10 +2112,11 @@ export function Canvas() {
* through it, so none of them can quietly skip the two duties below again.
*
* For a STALE binding (the worktree directory was deleted behind git's back) that means:
- * a. reset the children's `data.cwd` off the dead path (`resetDisplacedCwd`, no respawn — nothing
- * here ends a process). Left behind, that dead path is persisted to project.json and tmux
- * hides it until the next machine reboot cold-starts the terminal into a directory that is not
- * there; and
+ * a. displace the children (`resetDisplacedCwd`, no respawn — nothing here ends a process):
+ * terminals/chats get `data.cwd` off the dead path (left behind, that dead path is persisted
+ * to project.json and tmux hides it until the next machine reboot cold-starts the terminal
+ * into a directory that is not there); editor/diff nodes get `data.fileMissing` instead,
+ * since there is nothing to re-point a dead `filePath` at; and
* b. prune git's stale REGISTRATION, or a later `git worktree add` at the same path fails with
* git's raw "missing but already registered worktree". `pruneOnly` guarantees a directory that
* still EXISTS is never touched, so a wrongly-stale group can never delete a live checkout.
@@ -2526,19 +2535,23 @@ export function Canvas() {
setNotice({ kind: 'error', text: res.message })
return // sessions untouched: nothing was removed.
}
- // 3) The directory is gone. Two things must happen to every node that was living in it —
- // and "every node" means ALL DESCENDANTS, not just direct children (a terminal inside a
- // nested group was missed), and chat nodes as well as terminals (a chat's cwd is just as
- // dead):
+ // 3) The directory is gone. Every node that was living in it owes a cleanup — and "every node"
+ // means ALL DESCENDANTS, not just direct children (a terminal inside a nested group was
+ // missed), plus editor/diff nodes anywhere on the canvas whose file was inside it:
// a. terminals: end the tmux session, which is now sitting in a directory that no longer
// exists;
// b. terminals AND chats: reset `data.cwd` off the deleted path. Leaving it there is the
// exact trap this whole task exists to remove — on the next mount the node spawns into
// a path that is gone, pty-manager silently falls back to $HOME, and the dead cwd is
// persisted forever — only reached through the SANCTIONED Remove path.
+ // c. editor/diff: mark `data.fileMissing`. There is no fallback path to re-point a dead
+ // `filePath` at — the file is genuinely gone — so unlike terminals/chats these are
+ // flagged, not rewritten, and the node shows a persistent notice instead of silently
+ // opening blank or failing a `git show`.
// The respawn (nonce bump) puts the terminal straight back in the fallback cwd rather than
// leaving a dead pane behind; its session was destroyed a line earlier either way.
- // Nodes whose cwd was NOT inside the worktree are left alone: they were never affected.
+ // Nodes whose cwd/filePath was NOT inside the worktree are left alone: they were never
+ // affected.
resetDisplacedCwd(t.groupId, wt.path, true)
clearWorktreeBinding(t.groupId)
setNotice({ kind: res.ok ? 'info' : 'error', text: res.message })
diff --git a/src/renderer/nodes/DiffNode.tsx b/src/renderer/nodes/DiffNode.tsx
index 6a272d7d6..16ca20029 100644
--- a/src/renderer/nodes/DiffNode.tsx
+++ b/src/renderer/nodes/DiffNode.tsx
@@ -14,15 +14,35 @@ import { tooLargeSize, formatBytes } from '@shared/fsLimits'
export function DiffNode({ id, data, selected }: NodeProps<CanvasNode>) {
const { deleteElements } = useReactFlow()
const bodyRef = useRef<HTMLDivElement>(null)
+ const editorRef = useRef<monaco.editor.IStandaloneDiffEditor | null>(null)
+ const originalRef = useRef<monaco.editor.ITextModel | null>(null)
+ const modifiedRef = useRef<monaco.editor.ITextModel | null>(null)
const [loadError, setLoadError] = useState('')
const cwd = (data.cwd as string) ?? ''
const rel = (data.filePath as string) ?? ''
const staged = !!data.diffStaged
const commitOid = (data.commitOid as string | undefined) || ''
+ // Set by a worktree removal sweep (`displacedByWorktree` / `resetDisplacedCwd` in Canvas.tsx):
+ // the repo this diff was scoped to no longer exists, and there is nothing to re-point it at.
+ const fileMissing = !!data.fileMissing
+
+ // A worktree removal can mark `fileMissing` on a node that is ALREADY mounted (open, live diff
+ // editor). The main effect below only runs once on mount, so it can't react to that — free the
+ // editor/models here instead.
+ useEffect(() => {
+ if (!fileMissing) return
+ editorRef.current?.dispose()
+ originalRef.current?.dispose()
+ modifiedRef.current?.dispose()
+ editorRef.current = null
+ originalRef.current = null
+ modifiedRef.current = null
+ }, [fileMissing])
useEffect(() => {
const el = bodyRef.current
- if (!el || !cwd || !rel) return
+ // fileMissing: the repo this diff was scoped to is gone — nothing to `git show` or read.
+ if (!el || !cwd || !rel || fileMissing) return
let disposed = false
let editor: monaco.editor.IStandaloneDiffEditor | null = null
let original: monaco.editor.ITextModel | null = null
@@ -63,6 +83,8 @@ export function DiffNode({ id, data, selected }: NodeProps<CanvasNode>) {
const s = useSettings.getState().settings
original = monaco.editor.createModel(orig, undefined, base.with({ fragment: `${id}-o` }))
modified = monaco.editor.createModel(mod, undefined, base.with({ fragment: `${id}-m` }))
+ originalRef.current = original
+ modifiedRef.current = modified
editor = monaco.editor.createDiffEditor(el, {
theme: 'vs-dark',
readOnly: true,
@@ -74,6 +96,7 @@ export function DiffNode({ id, data, selected }: NodeProps<CanvasNode>) {
fontSize: s.fontSize,
fontFamily: s.fontFamily
})
+ editorRef.current = editor
editor.setModel({ original, modified })
})
@@ -82,6 +105,9 @@ export function DiffNode({ id, data, selected }: NodeProps<CanvasNode>) {
editor?.dispose()
original?.dispose()
modified?.dispose()
+ editorRef.current = null
+ originalRef.current = null
+ modifiedRef.current = null
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
@@ -108,7 +134,15 @@ export function DiffNode({ id, data, selected }: NodeProps<CanvasNode>) {
</button>
</div>
- {loadError ? (
+ {fileMissing ? (
+ <div className="editor-node__body nodrag">
+ <div className="editor-node__image">
+ <span className="editor-node__loading">
+ This file’s worktree was removed — it no longer exists.
+ </span>
+ </div>
+ </div>
+ ) : loadError ? (
<div className="editor-node__body nodrag">
<div className="editor-node__image">
<span className="editor-node__loading">{loadError}</span>
diff --git a/src/renderer/nodes/EditorNode.tsx b/src/renderer/nodes/EditorNode.tsx
index 001174ba4..c0839b6ab 100644
--- a/src/renderer/nodes/EditorNode.tsx
+++ b/src/renderer/nodes/EditorNode.tsx
@@ -31,6 +31,7 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
const { deleteElements } = useReactFlow()
const bodyRef = useRef<HTMLDivElement>(null)
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null)
+ const modelRef = useRef<monaco.editor.ITextModel | null>(null)
const savedRef = useRef<string>('')
const hoveredRef = useRef(false)
const [dirty, setDirty] = useState(false)
@@ -41,6 +42,9 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
const [imageError, setImageError] = useState('')
const [loadError, setLoadError] = useState('')
const filePath = (data.filePath as string) ?? ''
+ // Set by a worktree removal sweep (`displacedByWorktree` / `resetDisplacedCwd` in Canvas.tsx):
+ // this file no longer exists, and unlike a terminal's cwd there is nothing to re-point it at.
+ const fileMissing = !!data.fileMissing
// Backend pick (the `FsApi` shape is identical across all three, so the rest of the component is
// unchanged): a relay session operates on the HOST's filesystem via the relay; an SSH-project
// editor (`data.sshFs`) on the project's remote fs over the ControlMaster; otherwise the local fs.
@@ -68,7 +72,7 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
const save = () => {
const editor = editorRef.current
- if (!editor) return
+ if (!editor || fileMissing) return
const value = editor.getValue()
fs.write(filePath, value).then((ok) => {
if (ok) {
@@ -80,8 +84,24 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
const saveRef = useRef(save)
saveRef.current = save
+ // A worktree removal can mark `fileMissing` on a node that is ALREADY mounted (open, live
+ // Monaco instance). The main effect below only runs once on mount, so it can't react to that —
+ // free the editor/model here instead. The div swaps out of the JSX below too, but React does not
+ // dispose Monaco's own resources just because its container left the tree.
useEffect(() => {
- if (!filePath) return
+ if (!fileMissing) return
+ editorRef.current?.dispose()
+ modelRef.current?.dispose()
+ editorRef.current = null
+ modelRef.current = null
+ }, [fileMissing])
+
+ useEffect(() => {
+ // Nothing to read: no path, or the file is already known gone (a worktree sweep can mark a
+ // node before it ever mounts too — e.g. a reload after the removal). Attempting the read would
+ // either open a silently-blank buffer (text) or chase a "couldn't read" error we already know
+ // the reason for (image).
+ if (!filePath || fileMissing) return
// Images: load as a data URL and render an <img> preview (no Monaco).
if (isImage) {
@@ -127,6 +147,7 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
// Unique model per node (fragment), language still inferred from the path extension.
const uri = monaco.Uri.file(filePath).with({ fragment: id })
model = monaco.editor.getModel(uri) ?? monaco.editor.createModel(content, undefined, uri)
+ modelRef.current = model
editor = monaco.editor.create(el, {
model,
theme: 'vs-dark',
@@ -149,6 +170,7 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
editor?.dispose()
model?.dispose()
editorRef.current = null
+ modelRef.current = null
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
@@ -181,7 +203,7 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
{!isImage && dirty ? ' ●' : ''}
</span>
<span className="term-node__spacer" />
- {isImage ? (
+ {fileMissing ? null : isImage ? (
imageDims && <span className="editor-node__dims">{imageDims}</span>
) : (
<>
@@ -212,7 +234,13 @@ export function EditorNode({ id, data, selected }: NodeProps<CanvasNode>) {
</div>
<div className="editor-node__body">
- {isImage ? (
+ {fileMissing ? (
+ <div className="editor-node__image nodrag">
+ <span className="editor-node__loading">
+ This file’s worktree was removed — it no longer exists.
+ </span>
+ </div>
+ ) : isImage ? (
<div className="editor-node__image nodrag nowheel">
{imageSrc ? (
<img
diff --git a/src/renderer/state/workspace.ts b/src/renderer/state/workspace.ts
index be1ecb5a5..cfa02b757 100644
--- a/src/renderer/state/workspace.ts
+++ b/src/renderer/state/workspace.ts
@@ -62,6 +62,15 @@ export interface NodeData {
cwd?: string
text?: string
filePath?: string
+ /**
+ * editor/diff-only: true once this node's `filePath` was confirmed gone — e.g. a worktree
+ * that contained it was removed (`displacedByWorktree` in @shared/worktree sweeps these up
+ * alongside terminal/chat cwds). Unlike a terminal's cwd, there is nothing to re-point an
+ * editor/diff node AT — the file itself no longer exists — so instead of silently opening
+ * blank (editor) or failing a `git show` (diff), the node shows a persistent notice. Persisted:
+ * the fact is durable, not a one-shot event like `respawnNonce`.
+ */
+ fileMissing?: boolean
/** web-only: live URL to load in the web (webview) node. */
url?: string
diffStaged?: boolean
@@ -918,6 +927,7 @@ export function nodeStatesToFlow(states: CanvasNodeState[]): CanvasNode[] {
cwd: n.cwd,
text: n.text,
filePath: n.filePath,
+ fileMissing: n.fileMissing,
url: n.url,
diffStaged: n.diffStaged,
commitOid: n.commitOid,
@@ -985,6 +995,7 @@ export function flowToNodeStates(nodes: CanvasNode[]): CanvasNodeState[] {
cwd: n.data.cwd,
text: n.data.text,
filePath: n.data.filePath,
+ fileMissing: n.data.fileMissing,
url: n.data.url,
diffStaged: n.data.diffStaged,
commitOid: n.data.commitOid,
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 5b7d4228e..66a86ca35 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -128,6 +128,12 @@ export interface CanvasNodeState {
highScore?: number
// editor / diff
filePath?: string
+ /**
+ * editor/diff-only: true once `filePath` was confirmed gone (e.g. its worktree was removed —
+ * see `displacedByWorktree` in `./worktree.ts`). There is nothing to re-point the node at, so
+ * it shows a persistent notice instead of silently opening blank / failing a `git show`.
+ */
+ fileMissing?: boolean
/** web-only: when set, the web node loads this live URL (else it loads `filePath` as local html). */
url?: string
/** diff-only: true = staged diff (HEAD vs index), false = unstaged (index vs working). */