From e11750a4f61f7d813ef1eb621868aa41d8694515 Mon Sep 17 00:00:00 2001 From: viktorce Date: Thu, 19 Nov 2026 17:18:06 +0000 Subject: [PATCH 1/3] fix(web): the terminal button reopens the drawer after its last terminal closed The terminal store remembers which terminal ids the server last listed, and closes a local terminal that later drops off that list. A terminal the user closed stayed on it, because the drawer unmounts before it sees the server drop the id. Reopening the drawer creates the same id, and the first reconcile closed it again. A closed id now leaves the list when it closes, and stays off it while it is still closing. --- apps/web/src/terminalUiStateStore.test.ts | 19 +++++++++++++ apps/web/src/terminalUiStateStore.ts | 33 ++++++++++++++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/apps/web/src/terminalUiStateStore.test.ts b/apps/web/src/terminalUiStateStore.test.ts index 6b043606f564b..69ff11a1eabbc 100644 --- a/apps/web/src/terminalUiStateStore.test.ts +++ b/apps/web/src/terminalUiStateStore.test.ts @@ -244,6 +244,25 @@ describe("terminalUiStateStore actions", () => { expect(layout().terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID, "new"]); }); + it.each([ + ["unmounts right after the close", false], + ["sees the closing terminal once more before it unmounts", true], + ])("keeps a reopened terminal when the drawer %s", (_case, staleReconcile) => { + const store = useTerminalUiStateStore.getState(); + store.setTerminalOpen(THREAD_REF, true); + store.reconcileTerminalIds(THREAD_REF, [DEFAULT_THREAD_TERMINAL_ID], "manager-1"); + store.closeTerminal(THREAD_REF, DEFAULT_THREAD_TERMINAL_ID); + if (staleReconcile) { + store.reconcileTerminalIds(THREAD_REF, [DEFAULT_THREAD_TERMINAL_ID], "manager-1"); + } + // The drawer is gone now, so nothing reconciles the server dropping the terminal. + store.setTerminalOpen(THREAD_REF, true); + store.reconcileTerminalIds(THREAD_REF, [], "manager-1"); + + expect(layout().terminalOpen).toBe(true); + expect(layout().terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID]); + }); + it("keeps the surviving pane active when another client closes its split sibling", () => { const store = useTerminalUiStateStore.getState(); store.newTerminal(THREAD_REF, "first"); diff --git a/apps/web/src/terminalUiStateStore.ts b/apps/web/src/terminalUiStateStore.ts index 6e0d88e784d47..1592dd553c6d3 100644 --- a/apps/web/src/terminalUiStateStore.ts +++ b/apps/web/src/terminalUiStateStore.ts @@ -563,11 +563,28 @@ function removeRecordEntry(record: Record, key: string): Record, + threadKey: string, + terminalId: string, +): Record { + const observation = serverObservationsByThreadKey[threadKey]; + if (!observation?.ids.includes(terminalId)) { + return serverObservationsByThreadKey; + } + const ids = observation.ids.filter((id) => id !== terminalId); + if (ids.length === 0) { + return removeRecordEntry(serverObservationsByThreadKey, threadKey); + } + return { ...serverObservationsByThreadKey, [threadKey]: { ...observation, ids } }; +} + interface TerminalUiStateStoreState { serverObservationsByThreadKey: Record; terminalUiStateByThreadKey: Record; @@ -642,15 +659,24 @@ export const useTerminalUiStateStore = create()( suppression.suppressed, ) : state.suppressedTerminalIdsByThreadKey; + const nextServerObservationsByThreadKey = suppression?.suppressed + ? forgetObservedTerminalId( + state.serverObservationsByThreadKey, + terminalThreadKey(threadRef), + suppression.terminalId, + ) + : state.serverObservationsByThreadKey; if ( nextTerminalUiStateByThreadKey === state.terminalUiStateByThreadKey && - nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey + nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey && + nextServerObservationsByThreadKey === state.serverObservationsByThreadKey ) { return state; } return { terminalUiStateByThreadKey: nextTerminalUiStateByThreadKey, suppressedTerminalIdsByThreadKey: nextSuppressedTerminalIdsByThreadKey, + serverObservationsByThreadKey: nextServerObservationsByThreadKey, }; }); }; @@ -727,19 +753,20 @@ export const useTerminalUiStateStore = create()( const state = get(); const previous = state.serverObservationsByThreadKey[threadKey]; const suppressedIds = new Set(state.suppressedTerminalIdsByThreadKey[threadKey]); + const followedIds = serverIds.filter((id) => !suppressedIds.has(id)); const terminalUiStateByThreadKey = updateTerminalUiStateByThreadKey( state.terminalUiStateByThreadKey, threadRef, (terminalState) => reconcileThreadTerminalSessionIds( terminalState, - serverIds.filter((id) => !suppressedIds.has(id)), + followedIds, // A different manager can start empty before old sessions reattach. previous?.snapshotId === snapshotId ? (previous?.ids ?? []) : [], ), ); // Metadata is ordered by activity, which does not change which terminals were seen. - const observedIds = [...serverIds].sort(); + const observedIds = followedIds.toSorted(); let serverObservationsByThreadKey = state.serverObservationsByThreadKey; if (observedIds.length === 0) { serverObservationsByThreadKey = removeRecordEntry( From d8a42e0451913ec53b79d109d492400fb6536832 Mon Sep 17 00:00:00 2001 From: viktorce Date: Thu, 19 Nov 2026 17:51:53 +0000 Subject: [PATCH 2/3] fix(web): forget a reused terminal id when the client places it The store forgot a closed id at close time and kept it out of later reconciles. An id saved by an older build never went through either path, so its first reopen still took two clicks. Placing a fresh local id now drops it from the observed list instead, which covers the close, the close animation and the saved entry with one rule. --- apps/web/src/terminalUiStateStore.test.ts | 2 +- apps/web/src/terminalUiStateStore.ts | 28 ++++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/apps/web/src/terminalUiStateStore.test.ts b/apps/web/src/terminalUiStateStore.test.ts index 69ff11a1eabbc..b4030d453b41a 100644 --- a/apps/web/src/terminalUiStateStore.test.ts +++ b/apps/web/src/terminalUiStateStore.test.ts @@ -256,7 +256,7 @@ describe("terminalUiStateStore actions", () => { store.reconcileTerminalIds(THREAD_REF, [DEFAULT_THREAD_TERMINAL_ID], "manager-1"); } // The drawer is gone now, so nothing reconciles the server dropping the terminal. - store.setTerminalOpen(THREAD_REF, true); + store.ensureTerminal(THREAD_REF, DEFAULT_THREAD_TERMINAL_ID, { open: true }); store.reconcileTerminalIds(THREAD_REF, [], "manager-1"); expect(layout().terminalOpen).toBe(true); diff --git a/apps/web/src/terminalUiStateStore.ts b/apps/web/src/terminalUiStateStore.ts index 1592dd553c6d3..0c38e17b10312 100644 --- a/apps/web/src/terminalUiStateStore.ts +++ b/apps/web/src/terminalUiStateStore.ts @@ -563,7 +563,6 @@ function removeRecordEntry(record: Record, key: string): Record()( suppression.suppressed, ) : state.suppressedTerminalIdsByThreadKey; - const nextServerObservationsByThreadKey = suppression?.suppressed - ? forgetObservedTerminalId( - state.serverObservationsByThreadKey, - terminalThreadKey(threadRef), - suppression.terminalId, - ) - : state.serverObservationsByThreadKey; + // An observed id that is not local was closed here or saved by an older build, so it + // must not make reconcile close the fresh terminal that reuses it. + const nextServerObservationsByThreadKey = + suppression && + !suppression.suppressed && + !selectThreadTerminalUiState( + state.terminalUiStateByThreadKey, + threadRef, + ).terminalIds.includes(suppression.terminalId) + ? forgetObservedTerminalId( + state.serverObservationsByThreadKey, + terminalThreadKey(threadRef), + suppression.terminalId, + ) + : state.serverObservationsByThreadKey; if ( nextTerminalUiStateByThreadKey === state.terminalUiStateByThreadKey && nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey && @@ -753,20 +760,19 @@ export const useTerminalUiStateStore = create()( const state = get(); const previous = state.serverObservationsByThreadKey[threadKey]; const suppressedIds = new Set(state.suppressedTerminalIdsByThreadKey[threadKey]); - const followedIds = serverIds.filter((id) => !suppressedIds.has(id)); const terminalUiStateByThreadKey = updateTerminalUiStateByThreadKey( state.terminalUiStateByThreadKey, threadRef, (terminalState) => reconcileThreadTerminalSessionIds( terminalState, - followedIds, + serverIds.filter((id) => !suppressedIds.has(id)), // A different manager can start empty before old sessions reattach. previous?.snapshotId === snapshotId ? (previous?.ids ?? []) : [], ), ); // Metadata is ordered by activity, which does not change which terminals were seen. - const observedIds = followedIds.toSorted(); + const observedIds = [...serverIds].sort(); let serverObservationsByThreadKey = state.serverObservationsByThreadKey; if (observedIds.length === 0) { serverObservationsByThreadKey = removeRecordEntry( From b7ba5aac2288c81f69cad609412a0829a5a77f2e Mon Sep 17 00:00:00 2001 From: viktorce Date: Thu, 19 Nov 2026 18:05:07 +0000 Subject: [PATCH 3/3] fix(web): the reused-id comment states what the forget relies on --- apps/web/src/terminalUiStateStore.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/web/src/terminalUiStateStore.ts b/apps/web/src/terminalUiStateStore.ts index 0c38e17b10312..6bb939f795846 100644 --- a/apps/web/src/terminalUiStateStore.ts +++ b/apps/web/src/terminalUiStateStore.ts @@ -658,8 +658,8 @@ export const useTerminalUiStateStore = create()( suppression.suppressed, ) : state.suppressedTerminalIdsByThreadKey; - // An observed id that is not local was closed here or saved by an older build, so it - // must not make reconcile close the fresh terminal that reuses it. + // A listing from before this client placed the id is about an earlier terminal, so it + // must not make reconcile close the new one. const nextServerObservationsByThreadKey = suppression && !suppression.suppressed &&