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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(projects): keep the last-session offer inside the one-confirm guard (#848)
Erik Zwepfa9d29d62808
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex 83864267f..2048852f5 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -1697,7 +1697,7 @@ export function Canvas() {} | null>(null)// Issue #848 (opt-in): the project whose LAST session node the user just closed with ×, awaiting// "close the project too?". Declining only clears this; the session was already ended by ×.- const [lastSessionOffer, setLastSessionOffer] = useState<{ id: string; name: string } | null>(null)+ const [lastSessionOffer, setLastSessionOfferState] = useState<{ id: string; name: string } | null>(null)const [mergePush, setMergePush] = useState(false)const settings = useSettings((s) => s.settings)const gatewayModels = useModelGateway((s) => s.models)@@ -1762,29 +1762,6 @@ export function Canvas() {// onBrowserNewWindow effect can dedup repeat opens and rate-cap a flood of window.open calls.const browserPopupSpawnsRef = useRef<{ url: string; source: string; t: number }[]>([])const loadingRef = useRef(false)- // Issue #848: TerminalNode's × announces the user's own close (and nothing else does — not an- // exit, restart, hibernation, bulk delete, canvas-control close, project close or quit). Decided- // HERE against the live canvas while the closed node is still on it, and only for the project- // whose nodes `nodesRef` actually holds — see lib/lastSessionClose for what counts as a session.- useEffect(() => {- const onUserClosedSession = (e: Event): void => {- const nodeId = (e as CustomEvent<{ nodeId?: unknown }>).detail?.nodeId- if (typeof nodeId !== 'string') return- const store = useProjects.getState()- const projectId = nodesProjectIdRef.current- if (!projectId || projectId !== store.activeProjectId) return- const project = store.getProject(projectId)- const offer = shouldOfferProjectClose({- enabled: useSettings.getState().settings.offerCloseProjectOnLastSession,- closedNodeId: nodeId,- nodes: nodesRef.current,- project- })- if (offer && project) setLastSessionOffer({ id: project.id, name: project.name })- }- window.addEventListener(USER_CLOSED_SESSION_EVENT, onUserClosedSession)- return () => window.removeEventListener(USER_CLOSED_SESSION_EVENT, onUserClosedSession)- }, [])const flowWrapRef = useRef<HTMLDivElement>(null)// Glass terminals: while the camera moves, every glass node's backdrop changes each frame and// the 28px blur is re-rasterised for all of them. The class drops the blur (the tint stays, and@@ -2012,7 +1989,8 @@ export function Canvas() {peer: false,closeProject: false,deleteProject: false,- issueWorktree: false+ issueWorktree: false,+ lastSessionOffer: false})// Every confirm setter flips its flag AT CALL TIME. Assigning the mirror during RENDER (what this// used to do) is a tick too late: two agent verbs arriving in separate IPC events before React@@ -2112,9 +2090,44 @@ export function Canvas() {f.closeProject ||f.deleteProject ||f.issueWorktree ||+ f.lastSessionOffer ||removePendingRef.current)}, [])+ // Issue #848's offer is an actionable dialog like the rest, so it is in the same guard: an agent+ // `write`/`close` or a worktree removal must not stack over it (flag flipped at call time).+ const setLastSessionOffer = useCallback((v: { id: string; name: string } | null) => {+ confirmFlags.current.lastSessionOffer = !!v+ setLastSessionOfferState(v)+ }, [])+ // Issue #848: TerminalNode's × announces the user's own close (and nothing else does — not an+ // exit, restart, hibernation, bulk delete, canvas-control close, project close or quit). Decided+ // HERE against the live canvas while the closed node is still on it, and only for the project+ // whose nodes `nodesRef` actually holds — see lib/lastSessionClose for what counts as a session.+ useEffect(() => {+ const onUserClosedSession = (e: Event): void => {+ const nodeId = (e as CustomEvent<{ nodeId?: unknown }>).detail?.nodeId+ if (typeof nodeId !== 'string') return+ // One actionable dialog at a time: if any confirm is open — or being opened (the async gap+ // in requestRemoveWorktree) — SKIP the offer rather than queue it. It is a convenience tied+ // to this click; raised later it would no longer be about what the user just did (and the+ // project may have sessions again). Closing the project by hand remains one menu away.+ if (confirmBusy()) return+ const store = useProjects.getState()+ const projectId = nodesProjectIdRef.current+ if (!projectId || projectId !== store.activeProjectId) return+ const project = store.getProject(projectId)+ const offer = shouldOfferProjectClose({+ enabled: useSettings.getState().settings.offerCloseProjectOnLastSession,+ closedNodeId: nodeId,+ nodes: nodesRef.current,+ project+ })+ if (offer && project) setLastSessionOffer({ id: project.id, name: project.name })+ }+ window.addEventListener(USER_CLOSED_SESSION_EVENT, onUserClosedSession)+ return () => window.removeEventListener(USER_CLOSED_SESSION_EVENT, onUserClosedSession)+ }, [confirmBusy, setLastSessionOffer])const nodeTypes = useMemo(() => ({@@ -18430,9 +18443,13 @@ export function Canvas() {onConfirm={() => {// The existing close path (issue #848: reuse, don't reimplement). With no session// node left it closes silently; if an agent spawned one since, it gets the- // usual #442 confirm instead.- closeProject(lastSessionOffer.id)+ // usual #442 confirm instead — so the offer leaves the guard FIRST, or that+ // confirm would stack over it. Keyed by the id captured at raise time: after a+ // tab switch this still closes the project the offer named, and closeProject+ // commits the live canvas only when that project is the active one.+ const offer = lastSessionOffersetLastSessionOffer(null)+ closeProject(offer.id)}}onCancel={() => setLastSessionOffer(null)}/>diff --git a/src/renderer/canvas/canvas-wiring.test.tsx b/src/renderer/canvas/canvas-wiring.test.tsxindex 59db68875..13c4de155 100644--- a/src/renderer/canvas/canvas-wiring.test.tsx+++ b/src/renderer/canvas/canvas-wiring.test.tsx@@ -573,6 +573,51 @@ describe('the last-session close offer is wired to the × and to the existing clexpect(handler).toContain('useSettings.getState().settings.offerCloseProjectOnLastSession')expect(handler).toContain('nodes: nodesRef.current')const dialog = CANVAS_SRC.slice(CANVAS_SRC.indexOf('{lastSessionOffer && ('))- expect(dialog.slice(0, 1500)).toContain('closeProject(lastSessionOffer.id)')+ expect(dialog.slice(0, 2500)).toContain('closeProject(offer.id)')+ })+})++describe('the last-session offer joins the one-confirmation-at-a-time guard (issue #848 review)', () => {+ // `confirmBusy()` is what refuses a second actionable dialog (agent `write`/`close`, worktree+ // removal, …). An offer it cannot see lets another dialog stack over it, and an offer raised+ // while another dialog is open (or being opened) stacks over that one.+ const guard = CANVAS_SRC.slice(+ CANVAS_SRC.indexOf('const confirmFlags = useRef({'),+ CANVAS_SRC.indexOf('const nodeTypes = useMemo(')+ )++ it('has a synchronous flag, flipped at call time, that confirmBusy reads', () => {+ expect(guard).toContain('lastSessionOffer: false')+ expect(guard).toContain('confirmFlags.current.lastSessionOffer = !!v')+ expect(guard).toContain('f.lastSessionOffer ||')+ })++ it('skips the offer (never queues it) while another confirm is busy', () => {+ const handler = CANVAS_SRC.slice(+ CANVAS_SRC.indexOf('const onUserClosedSession = (e: Event): void => {'),+ CANVAS_SRC.indexOf('window.addEventListener(USER_CLOSED_SESSION_EVENT')+ )+ expect(handler).toContain('if (confirmBusy()) return')+ expect(indexOfPresent(handler, 'if (confirmBusy()) return')).toBeLessThan(+ indexOfPresent(handler, 'setLastSessionOffer(')+ )+ })++ it('accepting clears the offer BEFORE closing, so a follow-up #442 confirm is not refused or stacked', () => {+ const dialog = CANVAS_SRC.slice(CANVAS_SRC.indexOf('{lastSessionOffer && (')).slice(0, 2500)+ expect(indexOfPresent(dialog, 'setLastSessionOffer(null)')).toBeLessThan(+ indexOfPresent(dialog, 'closeProject(offer.id)')+ )+ })++ it('closes the project the offer NAMED even after a tab switch, touching the active one only when it is that project', () => {+ // The offer stores the project id at raise time; closeProject commits the live canvas only+ // when that id is the active project, and the store keeps activeProjectId when a background+ // project closes (pinned in state/projects.test.ts).+ const body = CANVAS_SRC.slice(+ CANVAS_SRC.indexOf('const closeProject = useCallback('),+ CANVAS_SRC.indexOf('// Right-click on a sidebar project header')+ )+ expect(body).toContain('if (id === store.activeProjectId) commitActiveToStore()')})})diff --git a/src/renderer/state/projects.test.ts b/src/renderer/state/projects.test.tsindex 0ef9f5c78..e6e5261f3 100644--- a/src/renderer/state/projects.test.ts+++ b/src/renderer/state/projects.test.ts@@ -330,3 +330,17 @@ describe('rebindNode', () => {expect(stored('p1', 'n1')?.accountId).toBe('a')})})++describe('closeProject on a background project (issue #848: the offer outlives a tab switch)', () => {+ it('closes only the named project and leaves the active one and its nodes alone', () => {+ const a = useProjects.getState().addProject('a')+ const b = useProjects.getState().addProject('b')+ useProjects.getState().setActive(b.id)+ const activeBefore = useProjects.getState().getProject(b.id)+ useProjects.getState().closeProject(a.id)+ const s = useProjects.getState()+ expect(s.activeProjectId).toBe(b.id)+ expect(s.getProject(a.id)?.closed).toBe(true)+ expect(s.getProject(b.id)).toBe(activeBefore)+ })+})