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(canvas): make the file manager's "could not read" and loading states reachable

albaneselorenzo7f417b48c96e
diff --git a/src/renderer/lib/filesNode.test.ts b/src/renderer/lib/filesNode.test.ts
index 1826bcce7..52a055d6c 100644
--- a/src/renderer/lib/filesNode.test.ts
+++ b/src/renderer/lib/filesNode.test.ts
@@ -1,5 +1,13 @@
import { describe, expect, it } from 'vitest'
-import { breadcrumbs, childPath, fileOpenTarget, filterEntries, folderTitle, parentDir } from './filesNode'
+import {
+ breadcrumbs,
+ childPath,
+ classifyEmptyListing,
+ fileOpenTarget,
+ filterEntries,
+ folderTitle,
+ parentDir
+} from './filesNode'
describe('breadcrumbs', () => {
it('walks a shallow path in full', () => {
@@ -92,3 +100,43 @@ describe('fileOpenTarget', () => {
expect(fileOpenTarget('/a/notes.md', { remote: true })).toBe('canvas')
})
})
+
+describe('classifyEmptyListing', () => {
+ const parent = [{ name: 'docs' }, { name: 'src' }, { name: 'README.md' }]
+
+ // The case the fourth empty state exists for, and could not reach before: a removed worktree.
+ // `listDir` answers [] for a directory that is gone, exactly as it does for an empty one.
+ it('says missing when the parent lists real entries and ours is not among them', () => {
+ expect(classifyEmptyListing('/repo/gone', parent)).toBe('missing')
+ })
+
+ it('says empty when the parent lists us — the folder is there and simply has nothing in it', () => {
+ expect(classifyEmptyListing('/repo/docs', parent)).toBe('empty')
+ expect(classifyEmptyListing('/repo/docs/', parent)).toBe('empty')
+ })
+
+ // "A failed read is never evidence of absence" (SshFs.readTextChecked). Under the same
+ // fail-open contract an empty PARENT means unreadable-or-gone, not childless — so we learned
+ // nothing and must not tell the user their folder was deleted.
+ it('says unknown when the parent itself came back empty', () => {
+ expect(classifyEmptyListing('/repo/docs', [])).toBe('unknown')
+ })
+
+ it('says unknown when the parent could not be asked at all', () => {
+ expect(classifyEmptyListing('/repo/docs', null)).toBe('unknown')
+ })
+
+ // Root has no parent to interrogate and always exists.
+ it('says empty at the root rather than interrogating a parent that does not exist', () => {
+ expect(classifyEmptyListing('/', null)).toBe('empty')
+ expect(classifyEmptyListing('/', [])).toBe('empty')
+ })
+
+ // A symlinked directory can list as a non-dir entry depending on the leg; matching on `dir`
+ // would call it missing. Name matching is what keeps that false alarm out.
+ it('matches by name, not by entry kind', () => {
+ // Real callers pass `DirEntry[]`; a symlinked directory can arrive with `dir: false`.
+ const listed: { name: string; dir: boolean }[] = [{ name: 'link', dir: false }]
+ expect(classifyEmptyListing('/repo/link', listed)).toBe('empty')
+ })
+})
diff --git a/src/renderer/lib/filesNode.ts b/src/renderer/lib/filesNode.ts
index 5eeb9a907..38479d2fc 100644
--- a/src/renderer/lib/filesNode.ts
+++ b/src/renderer/lib/filesNode.ts
@@ -45,6 +45,48 @@ export function folderTitle(path: string): string {
return path.split('/').filter(Boolean).pop() ?? '/'
}
+/**
+ * What an EMPTY listing actually means.
+ *
+ * `FsApi` is fail-open by contract — `core/fs-ops.listDir` and `SshFs.listDir` both end
+ * `catch { return [] }`, and the SSH IPC resolves `[]` even for a project whose ControlMaster is
+ * down. So a `.catch` on `list()` fires only when the transport itself rejects, and a directory
+ * that was deleted (a removed worktree, most obviously) comes back indistinguishable from one
+ * that is genuinely empty. That is why the node's "could not read" state was unreachable.
+ *
+ * We do not ask `fs.exists`. It is `stat`-based, so it answers TRUE for a directory you can stat
+ * but not `readdir`, and on SSH its `false` cannot separate "gone" from "the master died" — which
+ * `SshFs.readTextChecked` singles out precisely so it can refuse to conflate them: *a failed read
+ * is never evidence of absence*. Instead we ask the PARENT's listing, the same way
+ * `SshProjectDialog` and `file-links.ts`'s `makeDirListingLookup` already answer this question.
+ *
+ * Three answers, and the third is the point:
+ * - `missing` — the parent listed real entries and ours is not among them. Only this earns the
+ * error state.
+ * - `empty` — the parent lists us, so the directory is there and simply has nothing in it.
+ * Root takes this too: it has no parent to ask and always exists.
+ * - `unknown` — the parent itself came back empty, which under the same fail-open contract means
+ * the parent is unreadable or gone rather than childless. We learned nothing, so we claim
+ * nothing; the caller keeps saying "empty". Understating beats telling someone their folder
+ * was deleted because a network hiccup ate one `ls`.
+ *
+ * Matching is by NAME, not by `dir`, deliberately: a symlinked directory can list as a non-dir
+ * entry depending on the leg, and mistaking one for "missing" is the false alarm this exists to
+ * avoid.
+ */
+export type EmptyListingVerdict = 'empty' | 'missing' | 'unknown'
+
+export function classifyEmptyListing(
+ cwd: string,
+ /** The parent's entries, or `null` when the parent was not asked or could not be read. */
+ parentEntries: readonly { name: string }[] | null
+): EmptyListingVerdict {
+ const name = folderTitle(cwd)
+ if (name === '/' || !cwd.replace(/\/+$/, '')) return 'empty' // root: no parent, always exists
+ if (!parentEntries || parentEntries.length === 0) return 'unknown'
+ return parentEntries.some((e) => e.name === name) ? 'empty' : 'missing'
+}
+
/** Join a directory and an entry name. Trailing slashes on the dir are absorbed, so the root
* ('/') does not produce a doubled separator. */
export function childPath(dir: string, name: string): string {
diff --git a/src/renderer/nodes/FilesNode.tsx b/src/renderer/nodes/FilesNode.tsx
index bcff07352..9997c9086 100644
--- a/src/renderer/nodes/FilesNode.tsx
+++ b/src/renderer/nodes/FilesNode.tsx
@@ -24,7 +24,15 @@ import { NodeResizer, useReactFlow, type NodeProps } from '@xyflow/react'
import type { DirEntry } from '@shared/types'
import { NODE_MIN_SIZES } from '../lib/nodeSizing'
import { COLLAPSED_HEIGHT, NODE_COLORS, type CanvasNode } from '../state/workspace'
-import { breadcrumbs, childPath, fileOpenTarget, filterEntries, folderTitle, parentDir } from '../lib/filesNode'
+import {
+ breadcrumbs,
+ childPath,
+ classifyEmptyListing,
+ fileOpenTarget,
+ filterEntries,
+ folderTitle,
+ parentDir
+} from '../lib/filesNode'
import { ancestorDirs, createTargetDir, newEntryPath } from '../lib/explorerCreate'
import { sshFs } from '../terminal/ssh-fs'
import { useSession } from '../session/session'
@@ -56,7 +64,12 @@ export function FilesNode({ id, data, selected }: NodeProps<CanvasNode>) {
const [showColors, setShowColors] = useState(false)
const [editingTitle, setEditingTitle] = useState(false)
const [titleBefore, setTitleBefore] = useState('')
- const [entries, setEntries] = useState<DirEntry[] | null>(null)
+ /** Kept WITH the directory it belongs to. Deriving `entries` from a cwd match is what makes
+ * "Loading…" reachable: before this, nothing ever reset the list, so navigating showed the
+ * PREVIOUS folder's rows until the new promise resolved and the loading state existed only on
+ * the very first mount. A `version` bump (re-list after a create) deliberately keeps the rows,
+ * since that is the same directory being re-read. */
+ const [listing, setListing] = useState<{ cwd: string; entries: DirEntry[] } | null>(null)
const [error, setError] = useState('')
const [query, setQuery] = useState('')
const [menu, setMenu] = useState<{ x: number; y: number; items: MenuItem[] } | null>(null)
@@ -65,6 +78,9 @@ export function FilesNode({ id, data, selected }: NodeProps<CanvasNode>) {
const collapsed = !!data.collapsed
const cwd = (data.cwd as string) || '/'
+ /** Null whenever the rows on screen do not belong to the directory being shown — i.e. the
+ * "Loading…" state, which is now reachable on every navigation and not just the first mount. */
+ const entries = listing && listing.cwd === cwd ? listing.entries : null
const isSshFs = !!data.sshFs
const { api, source } = useSession()
const activeProjectId = useProjects((s) => s.activeProjectId)
@@ -78,16 +94,36 @@ export function FilesNode({ id, data, selected }: NodeProps<CanvasNode>) {
useEffect(() => {
let live = true
setError('')
- void fs
- .list(cwd)
- .then((list) => {
- if (live) setEntries(list)
- })
- .catch(() => {
+ void (async () => {
+ let list: DirEntry[]
+ try {
+ list = await fs.list(cwd)
+ } catch {
+ // Only a transport-level rejection lands here (a dropped ws/relay socket, an
+ // unsupported namespace). Every filesystem failure resolves `[]` instead — which is
+ // exactly what the empty branch below has to disambiguate.
if (!live) return
- setEntries([])
+ setListing({ cwd, entries: [] })
setError('Could not read this folder.')
- })
+ return
+ }
+ if (!live) return
+ setListing({ cwd, entries: list })
+ if (list.length > 0) return
+ // Empty is ambiguous under the fail-open `FsApi` contract, so ask the parent who is right.
+ // A second listing, no new IPC, and only a definite absence is allowed to raise the error.
+ const parent = parentDir(cwd)
+ let parentEntries: DirEntry[] | null = null
+ try {
+ if (parent !== cwd) parentEntries = await fs.list(parent)
+ } catch {
+ parentEntries = null // could not ask ⇒ claim nothing
+ }
+ if (!live) return
+ if (classifyEmptyListing(cwd, parentEntries) === 'missing') {
+ setError('Could not read this folder.')
+ }
+ })()
return () => {
live = false
}
@@ -97,6 +133,14 @@ export function FilesNode({ id, data, selected }: NodeProps<CanvasNode>) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cwd, isSshFs, activeProjectId, version])
+ // The filter belongs to the directory it was typed in, so ONE owner clears it — keyed on `cwd`
+ // rather than done inside `navigate`, because navigation is not the only way the cwd changes:
+ // a removed worktree re-points it through `resetDisplacedCwd`, which never calls `navigate`.
+ // A filter surviving that lands the user in a new folder showing "Nothing matches …".
+ useEffect(() => {
+ setQuery('')
+ }, [cwd])
+
const navigate = useCallback(
(to: string) => {
// The title tracks the folder ONLY while the user has not renamed the node by hand — the
@@ -106,7 +150,6 @@ export function FilesNode({ id, data, selected }: NodeProps<CanvasNode>) {
const patch: Record<string, unknown> = { cwd: to }
if (data.titleAuto !== false) patch.title = folderTitle(to)
updateNodeData(id, patch)
- setQuery('')
},
[id, updateNodeData, data.titleAuto]
)