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(workspace): an index that parses but is no index we know also marks the run as rebuilt

review08eb0ced6153
diff --git a/src/core/watch-link/service.workspace.test.ts b/src/core/watch-link/service.workspace.test.ts
index 4f1e1d30b..0301ea568 100644
--- a/src/core/watch-link/service.workspace.test.ts
+++ b/src/core/watch-link/service.workspace.test.ts
@@ -146,4 +146,47 @@ describe('an index rebuilt from nothing is never a complete read (R44)', () => {
await store.save(ws([]))
expect(workspaceNodeState(store, 'term-1')).toBe('unknown')
})
+
+ // NEW-1: a file that PARSES but is no index this build recognises falls through to an empty
+ // workspace just like an unparsable one — and its empty boot save must not read as complete either.
+ for (const [label, body] of [
+ ['{}', '{}'],
+ ['[]', '[]'],
+ ['null', 'null'],
+ ['a v2 without its projects list', '{"version":2}'],
+ ['a v3 without entries', '{"version":3}'],
+ ['a v3 whose entries are not objects', '{"version":3,"entries":[5]}'],
+ ['a newer build\'s index', '{"version":4,"entries":[]}']
+ ] as const) {
+ it(`a PARSABLE but unrecognised index (${label}) is the same: unknown after the empty boot save, and load() answers`, async () => {
+ const m = await machine()
+ await fs.writeFile(m.index, body)
+ const store = new WorkspaceStore()
+ await expect(store.load({ sideline: false })).resolves.toMatchObject({ projects: [] })
+ const links = linksOver(store)
+ await links.s.init()
+ await store.load()
+ await store.save(ws([]))
+ expect(workspaceNodeState(store, 'term-1')).toBe('unknown')
+ await settle()
+ expect(links.calls).toEqual([])
+ expect(links.s.list()).toHaveLength(1)
+ })
+ }
+
+ // …while an index that says, readably, that there is nothing IS a complete read.
+ for (const [label, body] of [
+ ['v3', '{"version":3,"entries":[]}'],
+ ['v2', '{"version":2,"activeProjectId":"","projects":[]}']
+ ] as const) {
+ it(`a genuinely empty ${label} index stays a complete read: the node is absent`, async () => {
+ const m = await machine()
+ await fs.writeFile(m.index, body)
+ const store = new WorkspaceStore()
+ await store.load()
+ await store.save(ws([]))
+ expect(store.knownNodeIds()).toEqual(new Set())
+ expect(workspaceNodeState(store, 'term-1')).toBe('absent')
+ })
+ }
})
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index aacbd3b43..628958729 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -2368,4 +2368,13 @@ describe('knownNodeIds — every node id in every project, or undefined when it
expect([...(next.knownNodeIds() ?? [])]).toEqual(['term-1'])
}
})
+ // Re-review NEW-1: an index of the right version that cannot be BUILT (loadV3 throws: a `cwd` that
+ // is not a string) keeps rejecting as before, and marks the run like any other unreadable index.
+ it('a v3 index whose build throws still rejects the load, and marks the run', async () => {
+ await fs.writeFile(path.join(userData, 'workspace.json'), JSON.stringify({ version: 3, entries: [{ id: 'p1', name: 'x', color: '#fff', cwd: 5 }] }))
+ const store = new WorkspaceStore()
+ await expect(store.load()).rejects.toThrow()
+ await store.save(ws([]))
+ expect(store.knownNodeIds()).toBeUndefined()
+ })
})
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index fac97885b..8518b09fe 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -327,7 +327,8 @@ export class WorkspaceStore {
onPersist?: () => void
/**
* Some load in THIS run found no readable workspace.json — missing (deleted, a first run, a crash
- * between the corrupt-file set-aside and the next write), unreadable, or unparsable — so whatever
+ * between the corrupt-file set-aside and the next write), unreadable, unparsable, or parsable but no
+ * index this build recognises (`{}`, a v3 without entries, a newer build's version) — so whatever
* index this run holds afterwards may have been rebuilt from NOTHING: the renderer's unconditional
* boot save writes an EMPTY index over the set-aside file while every project's own
* `.nodeterm/project.json` still holds its nodes. `knownNodeIds` then answers undefined for the rest
@@ -426,10 +427,23 @@ export class WorkspaceStore {
}
return EMPTY_WORKSPACE
}
- const anyParsed = parsed as { version?: number }
- if (anyParsed?.version === 3) return this.loadV3(parsed as WorkspaceIndexV3, sideline)
+ const anyParsed = parsed as { version?: number; entries?: unknown }
+ // Only a v3 index with an entry list of objects is one: `{"version":3}` alone used to reach loadV3
+ // and throw (`index.entries is not iterable`); now it falls through, like any unrecognised shape.
+ if (anyParsed?.version === 3 && Array.isArray(anyParsed.entries) && anyParsed.entries.every(isObjectEntry)) {
+ try {
+ return await this.loadV3(parsed as WorkspaceIndexV3, sideline)
+ } catch (e) {
+ this.indexRebuiltThisRun = true // R44: an index we could not build is not a read of it
+ throw e
+ }
+ }
// v1/v2: assemble in memory now; the first save() performs the actual migration.
const legacy = migrateLegacy(parsed)
+ // PARSED, but no index this build recognises (`{}`, `null`, `[]`, a v2 without its projects list,
+ // a v3 without entries, a newer build's version): the run's index is rebuilt from nothing exactly
+ // as for an unparsable file (R44 / re-review NEW-1). A readable EMPTY v2/v3 index is not this.
+ if (legacy === EMPTY_WORKSPACE) this.indexRebuiltThisRun = true
if (legacy.projects.length) this.pendingV2Backup = raw
return legacy
}
@@ -2728,7 +2742,11 @@ function unavailableProject(e: { id: string; name: string; color: string; closed
}
}
-/** Normalize legacy on-disk shapes (v1 single canvas, v2 projects) into a v2-shaped workspace. */
+const isObjectEntry = (e: unknown): boolean => typeof e === 'object' && e !== null && !Array.isArray(e)
+
+/** Normalize legacy on-disk shapes (v1 single canvas, v2 projects) into a v2-shaped workspace.
+ * Anything else answers `EMPTY_WORKSPACE` itself (by identity: `loadInner` reads that as "no index
+ * this build recognises"). */
function migrateLegacy(parsed: unknown): Workspace {
const ws = parsed as Partial<Workspace> & Partial<WorkspaceV1>
if (ws?.version === 2 && Array.isArray(ws.projects)) {