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): watch project files for outside edits

enes1396438af15c
diff --git a/src/core/workspace-watcher.test.ts b/src/core/workspace-watcher.test.ts
new file mode 100644
index 000000000..2fcbbe195
--- /dev/null
+++ b/src/core/workspace-watcher.test.ts
@@ -0,0 +1,56 @@
+import { describe, it, expect, beforeEach, afterEach } from 'vitest'
+import { promises as fs } from 'fs'
+import os from 'os'
+import path from 'path'
+import { WorkspaceWatcher } from './workspace-watcher'
+
+const wait = (ms: number) => new Promise((r) => setTimeout(r, ms))
+
+let dir: string
+let file: string
+beforeEach(async () => {
+ dir = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-watch-'))
+ file = path.join(dir, 'project.json')
+ await fs.writeFile(file, '{"v":1}')
+})
+afterEach(() => fs.rm(dir, { recursive: true, force: true }))
+
+describe('WorkspaceWatcher', () => {
+ it('fires for an outside edit, but not for a self-write', async () => {
+ const events: string[] = []
+ let self = ''
+ const w = new WorkspaceWatcher({
+ paths: () => [file],
+ isSelfWrite: (_p, content) => content === self,
+ onExternalChange: (p) => events.push(p),
+ debounceMs: 30
+ })
+ w.sync()
+ self = '{"v":2}'
+ await fs.writeFile(file, self) // simulated own save
+ await wait(120)
+ expect(events).toEqual([])
+ await fs.writeFile(file, '{"v":3}') // outside edit (git pull)
+ await wait(120)
+ expect(events).toEqual([file])
+ w.dispose()
+ })
+
+ it('drops watchers for paths that disappear from paths()', async () => {
+ const events: string[] = []
+ let paths = [file]
+ const w = new WorkspaceWatcher({
+ paths: () => paths,
+ isSelfWrite: () => false,
+ onExternalChange: (p) => events.push(p),
+ debounceMs: 30
+ })
+ w.sync()
+ paths = []
+ w.sync()
+ await fs.writeFile(file, '{"v":9}')
+ await wait(120)
+ expect(events).toEqual([])
+ w.dispose()
+ })
+})
diff --git a/src/core/workspace-watcher.ts b/src/core/workspace-watcher.ts
new file mode 100644
index 000000000..f4aebd0ee
--- /dev/null
+++ b/src/core/workspace-watcher.ts
@@ -0,0 +1,79 @@
+import { watch, type FSWatcher } from 'fs'
+import { promises as fs } from 'fs'
+
+interface Opts {
+ paths(): string[]
+ isSelfWrite(filePath: string, content: string): boolean
+ onExternalChange(filePath: string): void
+ debounceMs?: number
+}
+
+/**
+ * Watches each local ref's project.json for outside edits (git pull, file sync,
+ * a teammate's commit). Self-writes are recognized by exact content match against
+ * the store's last-written cache and ignored. Events are debounced per file —
+ * editors and git often touch a file several times in quick succession.
+ */
+export class WorkspaceWatcher {
+ private watchers = new Map<string, FSWatcher>()
+ private timers = new Map<string, ReturnType<typeof setTimeout>>()
+
+ constructor(private opts: Opts) {}
+
+ sync(): void {
+ const want = new Set(this.opts.paths())
+ for (const [p, w] of this.watchers) {
+ if (!want.has(p)) {
+ w.close()
+ this.watchers.delete(p)
+ }
+ }
+ for (const p of want) {
+ if (this.watchers.has(p)) continue
+ try {
+ const w = watch(p, () => this.schedule(p))
+ // A watcher error (file replaced by rename, dir removed) must not crash main.
+ w.on('error', () => {
+ w.close()
+ this.watchers.delete(p)
+ })
+ this.watchers.set(p, w)
+ } catch {
+ /* file missing right now → unavailable path; next sync() retries */
+ }
+ }
+ }
+
+ private schedule(p: string): void {
+ clearTimeout(this.timers.get(p))
+ this.timers.set(p, setTimeout(() => void this.check(p), this.opts.debounceMs ?? 300))
+ }
+
+ private async check(p: string): Promise<void> {
+ let content: string
+ try {
+ content = await fs.readFile(p, 'utf-8')
+ } catch {
+ return // transient (atomic rename in flight)
+ }
+ if (this.opts.isSelfWrite(p, content)) {
+ // Atomic saves replace the inode — rewatch so future outside edits still fire.
+ this.watchers.get(p)?.close()
+ this.watchers.delete(p)
+ this.sync()
+ return
+ }
+ // Atomic saves replace the inode — rewatch so future edits still fire.
+ this.watchers.get(p)?.close()
+ this.watchers.delete(p)
+ this.sync()
+ this.opts.onExternalChange(p)
+ }
+
+ dispose(): void {
+ for (const w of this.watchers.values()) w.close()
+ this.watchers.clear()
+ for (const t of this.timers.values()) clearTimeout(t)
+ this.timers.clear()
+ }
+}
diff --git a/src/main/index.ts b/src/main/index.ts
index 904a02a8a..db31d11cc 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -7,6 +7,7 @@ import { IPC } from '../shared/ipc'
import * as fsOps from '../core/fs-ops'
import { PtyManager } from '../core/pty-manager'
import { WorkspaceStore } from '../core/workspace-store'
+import { WorkspaceWatcher } from '../core/workspace-watcher'
import { SettingsStore } from '../core/settings-store'
import { SshStore } from './ssh-store'
import { GitService } from '../core/git-service'
@@ -98,6 +99,19 @@ const settingsStore = new SettingsStore()
const sshStore = new SshStore()
const ptyManager = new PtyManager()
const workspaceStore = new WorkspaceStore()
+// Watch each local ref's project.json for outside edits (git pull, a teammate's commit).
+// Self-writes match the store's last-written cache and are ignored. Re-synced after every
+// store load/save via onPersist; disposed on quit next to ptyManager.killAll().
+const workspaceWatcher = new WorkspaceWatcher({
+ paths: () => workspaceStore.localRefPaths(),
+ isSelfWrite: (p, c) => workspaceStore.isSelfWrite(p, c),
+ onExternalChange: (filePath) => {
+ void workspaceStore.readLocalRefByPath(filePath).then((changed) => {
+ if (changed) sendToMain(IPC.workspaceExternalChange, changed)
+ })
+ }
+})
+workspaceStore.onPersist = () => workspaceWatcher.sync()
const gitService = new GitService()
// One driver for all chat nodes. Resolves the live window at send time (getMainWindow) so
// pushes survive a macOS close→dock-reopen; disposed on quit next to ptyManager.killAll().
@@ -918,6 +932,7 @@ app.on('window-all-closed', () => {
let quitFlushed = false
app.on('before-quit', (e) => {
quitting = true // from here on, window close-events must NOT be turned into hide
+ workspaceWatcher.dispose()
sshProjectManager?.disconnectAll()
chatDriver.disposeAll() // tear down every chat node's SDK query (resume-based, so this is safe)
if (quitFlushed) return