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(workspace): phone writers keep app-data projects in app data

Brandon Bridgesd8f4f535a92a
diff --git a/src/core/workspace-store.folder-data.test.ts b/src/core/workspace-store.folder-data.test.ts
index 04945da04..f649adcc4 100644
--- a/src/core/workspace-store.folder-data.test.ts
+++ b/src/core/workspace-store.folder-data.test.ts
@@ -15,6 +15,7 @@ import type { CanvasNodeState, Project, Workspace } from '../shared/types'
let userData: string
let folder: string
+let fake: ReturnType<typeof fakePlatform>
const node = (id: string, over: Partial<CanvasNodeState> = {}): CanvasNodeState => ({
id, kind: 'terminal', position: { x: 0, y: 0 }, size: { width: 1, height: 1 },
@@ -44,7 +45,8 @@ const foreignFile = (nodeId: string) => JSON.stringify({
beforeEach(async () => {
userData = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-folder-data-'))
folder = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-folder-data-repo-'))
- initPlatform(fakePlatform({ userDataDir: userData }))
+ fake = fakePlatform({ userDataDir: userData })
+ initPlatform(fake)
})
afterEach(async () => {
resetPlatformForTests()
@@ -169,6 +171,47 @@ describe('its side files', () => {
})
})
+describe('the phone writers', () => {
+ const changes = () => fake.sent.filter((m) => m.channel === 'workspace:external-change').map((m) => m.args[0])
+
+ it('a session registered from the phone lands in the data file, rooted in the folder', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ nodes: [] })]))
+ fake.sent.length = 0
+
+ expect(await store.appendRemoteNode('project-p1', { id: 'term-zz1-1', title: 'Mobile' })).toBe(true)
+ expect(await fs.readdir(folder)).toEqual([])
+ const added = (await readData('project-p1')).nodes.find((n: CanvasNodeState) => n.id === 'term-zz1-1')
+ // The append's portable '.' default is resolved: this file keeps absolute cwds.
+ expect(added.cwd).toBe(folder)
+ // The canvas hears about it with its folder and the flag, so the next save stays in app data.
+ expect(changes()).toHaveLength(1)
+ expect(changes()[0]).toMatchObject({ id: 'project-p1', cwd: folder, storeInAppData: true })
+
+ const loaded = (await new WorkspaceStore().load()).projects[0]
+ expect(ids(loaded.nodes)).toEqual(['term-zz1-1'])
+ expect(loaded.nodes[0].cwd).toBe(folder)
+ })
+
+ it('ending a session from the phone removes it from the data file', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project()]))
+ expect(await store.removeRemoteNode('term-1')).toBe(true)
+ expect((await readData('project-p1')).nodes).toEqual([])
+ expect(await fs.readdir(folder)).toEqual([])
+ expect(await store.removeRemoteNode('term-nowhere')).toBe(false)
+ })
+
+ it('the phone board lands in the data file', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project()]))
+ const columns = await store.ensureRemoteBoard('project-p1')
+ expect(columns?.length).toBeGreaterThan(0)
+ expect((await readData('project-p1')).kanban.columns).toHaveLength(columns!.length)
+ expect(await fs.readdir(folder)).toEqual([])
+ })
+})
+
describe('its project settings', () => {
it('live in its app-data root, still as the shared document', async () => {
const store = new WorkspaceStore()
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 709345985..28090c601 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -2558,6 +2558,11 @@ export class WorkspaceStore {
now: Date,
accountColor?: string
): Promise<boolean> {
+ const data = this.index?.entries.find((x) => x.id === projectId && isFolderDataRef(x))
+ if (data) {
+ return (await this.transformDataFile(data, (raw) => appendProjectNode(raw, input, now, accountColor)))
+ ?.written ?? false
+ }
const e = this.index?.entries.find((x) => x.id === projectId && folderFileCwd(x))
if (!e?.cwd) return false
const file = projectFilePath(e.cwd)
@@ -2621,6 +2626,13 @@ export class WorkspaceStore {
for (const e of this.index?.entries ?? []) {
// Local ref projects only, like appendRemoteNode: an ssh ref's file lives on another
// machine (and a relay `pty.attach` only ever reaches THIS machine's tmux anyway).
+ if (isFolderDataRef(e)) {
+ if (!e.project?.nodes.some((n) => n.id === nodeId)) continue
+ const done = await this.transformDataFile(e, (raw) => removeProjectNode(raw, nodeId, now))
+ if (done?.written) return true
+ if (!done) return false
+ continue
+ }
if (!folderFileCwd(e) || !e.cwd || e.ssh) continue
const file = projectFilePath(e.cwd)
let raw: string
@@ -2824,6 +2836,7 @@ export class WorkspaceStore {
return { file: parsed, written: true }
}
+ if (isFolderDataRef(e)) return this.transformDataFile(e, transform)
if (!folderFileCwd(e) || !e.cwd) return null
const file = projectFilePath(e.cwd)
let raw: string
@@ -2861,6 +2874,56 @@ export class WorkspaceStore {
return { file: parsed, written: true }
}
+ /**
+ * The phone writers' leg for a FOLDER-DATA-REF (issue #1145): the same read-transform-write the
+ * folder leg does on `<cwd>/.nodeterm/project.json`, on the project's data file instead — so a
+ * session registered or ended from the phone, or a board move, lands in app data and never
+ * recreates the folder file. Three differences, each because this file is the inline kind:
+ * - the write goes through `writeDataFile`, under its rev rule (a second instance may share it);
+ * - node cwds are absolute here, so a portable one the transform produced (the append copies a
+ * sibling's, or defaults to '.') is resolved against the project's folder before it lands;
+ * - the index `project` cache is put in step, since the sync readers consult it first.
+ * Null when the entry is not this kind or the file could not be written.
+ */
+ private async transformDataFile(
+ e: IndexEntryV3,
+ transform: (raw: string) => string | null
+ ): Promise<{ file: ProjectFileV1; written: boolean } | null> {
+ if (!isFolderDataRef(e) || !e.cwd || !isInlineProjectFileId(e.id)) return null
+ const read = await this.readDataFile(e.id, false)
+ if (!read) return null
+ const updated = transform(read.raw)
+ if (updated === null) return { file: read.file, written: false }
+ let parsed: ProjectFileV1
+ try {
+ parsed = JSON.parse(updated) as ProjectFileV1
+ } catch {
+ return { file: read.file, written: false }
+ }
+ const candidate = { ...parsed, nodes: resolveNodes(parsed.nodes, e.cwd) }
+ const landed = await this.writeDataFile(e.id, candidate)
+ if (landed !== 'written' && landed !== 'unchanged') return null
+ const file = this.parsedLastWritten(inlineFilePath(e.id)) ?? candidate
+ const project = fileToProject(file, {
+ id: e.id,
+ closed: e.closed,
+ closedAt: e.closedAt,
+ handedOffTo: e.handedOffTo,
+ viewport: e.viewport,
+ defaultAccountId: e.defaultAccountId,
+ breadcrumbs: e.breadcrumbs,
+ closedSessions: e.closedSessions,
+ layoutViewports: e.layoutViewports,
+ capabilityAck: e.capabilityAck,
+ localExec: e.localExec
+ })
+ e.project = project
+ // The data file is the record and wins at load, so a cache write that fails costs nothing lasting.
+ await this.writeIndexNow().catch(() => {})
+ platform().broadcast(IPC.workspaceExternalChange, { ...project, cwd: e.cwd, storeInAppData: true })
+ return { file, written: true }
+ }
+
/** Tell the renderer about a project file THIS store just rewrote outside of `save()`. Shared by
* the kanban verbs; the same payload `appendRemoteNode`/`removeRemoteNode` build by hand. */
private announceProjectFile(e: IndexEntryV3, file: ProjectFileV1): void {