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: sanitize closedSessions on the inline-project load path too

Mina Sameh498a79b301a2
diff --git a/src/core/workspace-store.closed-history.test.ts b/src/core/workspace-store.closed-history.test.ts
index e2bad9250..5a9e9f48d 100644
--- a/src/core/workspace-store.closed-history.test.ts
+++ b/src/core/workspace-store.closed-history.test.ts
@@ -5,6 +5,7 @@ import path from 'path'
import { initPlatform, resetPlatformForTests } from './platform'
import { fakePlatform } from './platform-fake'
import { WorkspaceStore } from './workspace-store'
+import { CLOSED_SESSIONS_CAP } from '../shared/types'
import type { Project, Workspace } from '../shared/types'
let userData: string
@@ -53,3 +54,90 @@ describe('closedAt round trip (machine-local index)', () => {
expect(index.entries[0].closedAt).toBeUndefined()
})
})
+
+/**
+ * An INLINE (cwd-less) project is stored verbatim in workspace.json and never passes through
+ * `fileToProject`, so it bypasses every guard that lives there. The branch already re-applies
+ * `validKanban` and `sanitizeNodeTriggers` for exactly this reason ("workspace.json is
+ * hand-editable input too") — `closedSessions` owes the same, or a malformed value reaches
+ * `mergeClosedHistory`, which iterates it (a non-array throws and takes the sidebar render down)
+ * and hands each node to React Flow.
+ */
+describe('inline (cwd-less) projects sanitize closedSessions on load', () => {
+ const writeIndex = async (entries: unknown[]): Promise<void> => {
+ await fs.writeFile(
+ path.join(userData, 'workspace.json'),
+ JSON.stringify({ version: 3, activeProjectId: 'p1', entries }),
+ 'utf-8'
+ )
+ }
+ const inline = (closedSessions: unknown) => [
+ {
+ id: 'p1',
+ name: 'foo',
+ color: '#7aa2f7',
+ project: {
+ id: 'p1', name: 'foo', color: '#7aa2f7',
+ viewport: { x: 0, y: 0, zoom: 1 }, nodes: [],
+ closedSessions
+ }
+ }
+ ]
+
+ it('drops a non-array closedSessions rather than letting it reach the sidebar', async () => {
+ await writeIndex(inline({ not: 'an array' }))
+ const loaded = await new WorkspaceStore().load()
+ expect(loaded.projects[0].closedSessions).toBeUndefined()
+ })
+
+ it('drops entries with no position data (the recreate-time crash shape)', async () => {
+ await writeIndex(
+ inline([
+ {
+ id: 'e1', closedAt: 1,
+ node: { id: 'n1', kind: 'terminal', title: 't', color: '#fff', group: null }
+ }
+ ])
+ )
+ const loaded = await new WorkspaceStore().load()
+ expect(loaded.projects[0].closedSessions).toBeUndefined()
+ })
+
+ it('keeps a well-formed entry but strips a trigger spec off its node, same as live nodes', async () => {
+ await writeIndex(
+ inline([
+ {
+ id: 'e1', closedAt: 1,
+ absolutePosition: { x: 0, y: 0 },
+ node: {
+ id: 'n1', kind: 'terminal', position: { x: 0, y: 0 },
+ size: { width: 1, height: 1 }, title: 't', color: '#fff', group: null,
+ // A spec on a NON-trigger node — sanitizeNodeTriggers drops it outright.
+ trigger: { kind: 'cron', expr: '* * * * *' }
+ }
+ }
+ ])
+ )
+ const loaded = await new WorkspaceStore().load()
+ expect(loaded.projects[0].closedSessions).toHaveLength(1)
+ expect(loaded.projects[0].closedSessions?.[0].node.trigger).toBeUndefined()
+ expect(loaded.projects[0].closedSessions?.[0].node.title).toBe('t')
+ })
+
+ it('caps an oversized inline history', async () => {
+ await writeIndex(
+ inline(
+ Array.from({ length: CLOSED_SESSIONS_CAP + 5 }, (_, i) => ({
+ id: `e${i}`, closedAt: i,
+ absolutePosition: { x: 0, y: 0 },
+ node: {
+ id: `n${i}`, kind: 'terminal', position: { x: 0, y: 0 },
+ size: { width: 1, height: 1 }, title: 't', color: '#fff', group: null
+ }
+ }))
+ )
+ )
+ const loaded = await new WorkspaceStore().load()
+ expect(loaded.projects[0].closedSessions).toHaveLength(CLOSED_SESSIONS_CAP)
+ })
+})
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 36de232f9..fee3e4600 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -5,12 +5,12 @@ import { renameAtomic, writeFileAtomic } from './fs-atomic'
import { IPC } from '../shared/ipc'
import { platform } from './platform'
import {
- DEFAULT_PROJECT_ID, EMPTY_WORKSPACE,
+ CLOSED_SESSIONS_CAP, DEFAULT_PROJECT_ID, EMPTY_WORKSPACE,
type BridgeLink, type CanvasNodeState, type Project, type Workspace, type WorkspaceV1
} from '../shared/types'
import {
PROJECT_DIR, PROJECT_FILE, fileToProject, projectToFile, resolveNodes, sameProjectContent,
- sanitizeNodeTriggers, serializeProjectFile, splitWorkspace, validKanban,
+ sanitizeNodeTriggers, serializeProjectFile, splitWorkspace, validClosedSessions, validKanban,
type IndexEntryV3, type ProjectFileV1, type WorkspaceIndexV3
} from './workspace-files'
import { readProjectSettingsFile, writeProjectSettingsFile } from './project-settings-files'
@@ -264,9 +264,27 @@ export class WorkspaceStore {
// Inline projects are stored verbatim in the index (no fileToProject pass), so apply the
// same kanban shape guard here — a v1/hand-edited board would otherwise crash the render —
// and the same trigger shape rule (workspace.json is hand-editable input too).
- const { kanban, ...rest } = e.project
- const base = validKanban(kanban) ? e.project : rest
- built.push({ entry: e, project: { ...base, nodes: sanitizeNodeTriggers(base.nodes) } })
+ // `rest` drops BOTH guarded fields; each is added back below only if it passes its guard.
+ const { kanban, closedSessions, ...rest } = e.project
+ const base = validKanban(kanban) ? { ...rest, kanban } : rest
+ // Same treatment for `closedSessions`, and for the same reason: a malformed value here
+ // reaches `mergeClosedHistory`, which iterates it (a non-array throws and takes the whole
+ // sidebar render down) and hands each entry's node to React Flow. Dropped when it fails
+ // the shape rule, capped like every other path, and each surviving node gets the same
+ // trigger normalization `nodes` gets.
+ const history = validClosedSessions(closedSessions)
+ ? closedSessions
+ .slice(0, CLOSED_SESSIONS_CAP)
+ .map((c) => ({ ...c, node: sanitizeNodeTriggers([c.node])[0] }))
+ : undefined
+ built.push({
+ entry: e,
+ project: {
+ ...base,
+ nodes: sanitizeNodeTriggers(base.nodes),
+ ...(history ? { closedSessions: history } : {})
+ }
+ })
} else if (e.cwd) {
if (sideline) await sweepStaleTmp(projectFilePath(e.cwd))
const read = await this.readProjectFile(e.cwd, sideline)