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(whiteboard): write the pending strokes when the app quits

review775bed08ad0c
diff --git a/docs/whiteboard-node.md b/docs/whiteboard-node.md
index 3bab408b1..b34683bcd 100644
--- a/docs/whiteboard-node.md
+++ b/docs/whiteboard-node.md
@@ -68,8 +68,11 @@ could aim a read or an overwrite anywhere. A node id must be a `string` passing
- `boardRev` — epoch ms, bumped once per editing session that changed the board (never per
stroke), so strokes never reach the workspace save, canvas undo or team sync.
-Saves happen 1.5 s after the last change, when editing ends (with the picture), and on unmount
-(project switch, node closed). Whether there is anything to save is decided by comparing the
+Saves happen 1.5 s after the last change, when editing ends (with the picture), on unmount
+(project switch, node closed), and — on the desktop — when the app quits or restarts to update: an
+open editor registers its pending save with `lib/quitFlush.ts`, which Canvas's answer to main's
+flush request waits for (main caps that wait at ~1 s). A browser tab of the Server Edition has no
+such request, so closing it can lose the last ≤1.5 s of strokes. Whether there is anything to save is decided by comparing the
scene's version with the one this editor last saved, not by an `onChange` flag; the unmount flush
writes a snapshot kept from `onChange`, because by cleanup time Excalidraw's api answers an empty
scene. A board whose file does not parse is shown as unreadable and never opened, so an autosave
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 5842794e9..0f29ab558 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -328,6 +328,7 @@ import {
import { isTerminalTarget, KEYBOARD_OWNER_SELECTOR, type ContextElement } from '../lib/keyContext'
import { nextMdMode } from '../lib/markdownModeToggle'
import { installTerminalFocusMirror } from '../lib/terminalFocusMirror'
+import { runQuitFlushes } from '../lib/quitFlush'
import { nodeToRefocus } from '../lib/focusRestore'
import { openDialogCount } from '../components/dialog-stack'
import {
@@ -4190,10 +4191,15 @@ export function Canvas() {
// Quit / restart-to-update: main asks for the pending edits the debounce above still holds
// (src/main/renderer-flush.ts). Without this, the last ~800 ms of node moves / renames / camera
// changes were lost on every ⌘Q and every update restart. Same conflict rule as the autosave.
+ // Main takes ONE answer, so the other debounced writes (an open whiteboard's autosave) are
+ // flushed here too, through lib/quitFlush.ts, before this answers.
useEffect(
() =>
api.workspace.onFlushRequest(async () => {
- if (shouldFlushOnQuit(dirtyRef.current, !!conflictRef.current)) await persist()
+ await Promise.allSettled([
+ shouldFlushOnQuit(dirtyRef.current, !!conflictRef.current) ? persist() : undefined,
+ runQuitFlushes()
+ ])
}),
[api, persist]
)
diff --git a/src/renderer/lib/quitFlush.test.ts b/src/renderer/lib/quitFlush.test.ts
new file mode 100644
index 000000000..ba9699841
--- /dev/null
+++ b/src/renderer/lib/quitFlush.test.ts
@@ -0,0 +1,32 @@
+import { readFileSync } from 'fs'
+import { join } from 'path'
+import { describe, expect, it, vi } from 'vitest'
+import { addQuitFlush, runQuitFlushes } from './quitFlush'
+
+describe('quit flush registry', () => {
+ it('runs every registered flush, and one that fails does not stop the others', async () => {
+ const a = vi.fn(async () => {
+ throw new Error('disk full')
+ })
+ const b = vi.fn(async () => true)
+ const offA = addQuitFlush(a)
+ const offB = addQuitFlush(b)
+ await expect(runQuitFlushes()).resolves.toBeUndefined()
+ expect(a).toHaveBeenCalledTimes(1)
+ expect(b).toHaveBeenCalledTimes(1)
+ offA()
+ offB()
+ await runQuitFlushes()
+ expect(b).toHaveBeenCalledTimes(1)
+ })
+
+ // Main takes ONE answer to its flush request, so the registry only works if Canvas's single
+ // `onFlushRequest` handler waits for it before answering. Canvas is too large to mount here.
+ it('Canvas answers main\'s flush request only after the registered flushes ran', () => {
+ const src = readFileSync(join(__dirname, '../canvas/Canvas.tsx'), 'utf8').replace(/\r\n/g, '\n')
+ const at = src.indexOf('api.workspace.onFlushRequest(')
+ expect(at).toBeGreaterThan(-1)
+ expect(src.slice(at, at + 400)).toMatch(/await Promise\.allSettled\(\[[\s\S]*runQuitFlushes\(\)/)
+ expect(src.split('onFlushRequest(').length - 1).toBe(1)
+ })
+})
diff --git a/src/renderer/lib/quitFlush.ts b/src/renderer/lib/quitFlush.ts
new file mode 100644
index 000000000..88e2365de
--- /dev/null
+++ b/src/renderer/lib/quitFlush.ts
@@ -0,0 +1,26 @@
+// Pending writes that must land before the app quits or restarts to update, beyond the canvas
+// autosave Canvas flushes itself.
+//
+// Main asks the renderer ONCE (`workspace.onFlushRequest`, src/main/renderer-flush.ts) and takes
+// the first answer, capped at ~1 s. A second `onFlushRequest` subscriber would race Canvas's answer
+// and lose, so anything else with a debounced write registers here instead and Canvas's handler
+// waits for all of them before it answers. Today that is an open whiteboard editor
+// (whiteboard/WhiteboardEditor.tsx), whose autosave waits 1.5 s after the last stroke.
+//
+// Desktop only in effect: the Server Edition's bridge has no flush request (a browser tab cannot
+// hold its own close for an async write).
+
+const flushes = new Set<() => Promise<unknown>>()
+
+/** Register a pending-write flush; returns the unregister function. */
+export function addQuitFlush(flush: () => Promise<unknown>): () => void {
+ flushes.add(flush)
+ return () => {
+ flushes.delete(flush)
+ }
+}
+
+/** Run every registered flush. Never rejects: one that fails must not stop the others, nor the quit. */
+export async function runQuitFlushes(): Promise<void> {
+ await Promise.allSettled([...flushes].map((flush) => Promise.resolve().then(flush)))
+}
diff --git a/src/renderer/whiteboard/WhiteboardEditor.test.tsx b/src/renderer/whiteboard/WhiteboardEditor.test.tsx
index 897f6deb1..9c5d80078 100644
--- a/src/renderer/whiteboard/WhiteboardEditor.test.tsx
+++ b/src/renderer/whiteboard/WhiteboardEditor.test.tsx
@@ -182,3 +182,23 @@ describe('a read-only board (a hosted-team Viewer)', () => {
expect(onExit).toHaveBeenCalledWith(false)
})
})
+
+describe('quitting the app (or restarting to update) writes the pending strokes', () => {
+ it('an open editor\'s unsaved changes are written by the quit flush', async () => {
+ const { runQuitFlushes } = await import('../lib/quitFlush')
+ const onSave = vi.fn(async () => true)
+ mount({ onSave, onExit: vi.fn() })
+ draw()
+ await act(async () => runQuitFlushes())
+ expect(onSave).toHaveBeenCalledTimes(1)
+ })
+
+ it('a read-only board registers nothing to write', async () => {
+ const { runQuitFlushes } = await import('../lib/quitFlush')
+ const onSave = vi.fn(async () => true)
+ mount({ onSave, onExit: vi.fn(), readOnly: true })
+ draw()
+ await act(async () => runQuitFlushes())
+ expect(onSave).not.toHaveBeenCalled()
+ })
+})
diff --git a/src/renderer/whiteboard/WhiteboardEditor.tsx b/src/renderer/whiteboard/WhiteboardEditor.tsx
index 5e2f6049c..cd2130f5b 100644
--- a/src/renderer/whiteboard/WhiteboardEditor.tsx
+++ b/src/renderer/whiteboard/WhiteboardEditor.tsx
@@ -12,6 +12,7 @@ import {
import type { AppState, BinaryFiles, ExcalidrawImperativeAPI } from '@excalidraw/excalidraw/types'
import type { ExcalidrawElement } from '@excalidraw/excalidraw/element/types'
import { boardZoom, rescaleForCanvas, screenZoom } from './zoom'
+import { addQuitFlush } from '../lib/quitFlush'
/** After the last change, how long until the scene is written. Long enough that a burst of
* strokes is one write; short enough that a crash loses a few seconds, not a session. */
@@ -208,6 +209,10 @@ export default function WhiteboardEditor({
}
}, [controlRef, flush, onExit])
+ // Quitting the app or restarting to update unmounts nothing — the window just goes — so the
+ // strokes still inside the autosave delay are handed to main's quit flush (lib/quitFlush.ts).
+ useEffect(() => (readOnly ? undefined : addQuitFlush(() => flush(false))), [flush, readOnly])
+
// Leaving without an exit (a project switch, the node deleted): write whatever is unsaved. The
// api is still readable synchronously here; the write itself is async and the core finishes it
// after this component is gone.