From 887a78e28082486b65a6cc4b4f4aaf6b1b6d91e6 Mon Sep 17 00:00:00 2001 From: Filip Hejmowski <34747899+neriousy@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:44:02 +0100 Subject: [PATCH] fix(tui): close a tab removed by another TUI to its neighbour (#64261) --- .../tui/src/context/session-tabs-model.ts | 8 +++-- packages/tui/src/context/session-tabs.tsx | 12 ++++++- .../test/context/session-tabs-model.test.ts | 8 +++++ .../tui/test/context/session-tabs.test.tsx | 33 +++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/packages/tui/src/context/session-tabs-model.ts b/packages/tui/src/context/session-tabs-model.ts index 856f9961e7f..433941c4a99 100644 --- a/packages/tui/src/context/session-tabs-model.ts +++ b/packages/tui/src/context/session-tabs-model.ts @@ -43,11 +43,13 @@ export function openSessionTab(tabs: SessionTab[], tab: SessionTab): SessionTab[ return tabs.map((item, position) => (position === index ? { ...item, title: tab.title } : item)) } -export function closeSessionTab(tabs: SessionTab[], sessionID: string) { +export function closeSessionTab(tabs: SessionTab[], sessionID: string, lastIndex?: number) { const index = tabs.findIndex((tab) => tab.sessionID === sessionID) // Like openSessionTab and moveSessionTab, a no-op returns the same reference so callers can - // detect it by identity. - if (index === -1) return { tabs, next: undefined } + // detect it by identity. Another TUI sharing the tab list may already have closed the tab, so + // its last known index still picks the neighbour that took its place. + if (index === -1) + return { tabs, next: lastIndex === undefined ? undefined : tabs[Math.min(lastIndex, tabs.length - 1)]?.sessionID } return { tabs: tabs.filter((tab) => tab.sessionID !== sessionID), next: tabs[index + 1]?.sessionID ?? tabs[index - 1]?.sessionID, diff --git a/packages/tui/src/context/session-tabs.tsx b/packages/tui/src/context/session-tabs.tsx index 45265f4e7c0..9f40cff42e9 100644 --- a/packages/tui/src/context/session-tabs.tsx +++ b/packages/tui/src/context/session-tabs.tsx @@ -150,6 +150,15 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp const sessionID = current() return sessionID !== undefined && !state().tabs.some((tab) => tab.sessionID === sessionID) }, false) + // Where the viewed session's tab last sat. Another TUI sharing the tab list can close it while + // this one keeps showing the session; closing it here then still moves to a neighbour. + const currentTab = createMemo<{ sessionID: string; index: number } | undefined>((previous) => { + const sessionID = current() + if (!sessionID) return + const index = state().tabs.findIndex((tab) => tab.sessionID === sessionID) + if (index !== -1) return { sessionID, index } + if (previous?.sessionID === sessionID) return previous + }) const status = (sessionID: string) => { const session = root(sessionID) const members = family(session) @@ -353,7 +362,8 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp scrollAnchors.delete(id) setExpandedGroups(id, undefined) }) - const closed = closeSessionTab(state().tabs, target) + const known = currentTab() + const closed = closeSessionTab(state().tabs, target, known?.sessionID === target ? known.index : undefined) const selected = navigate && current() === target if (closed.tabs === state().tabs && !selected) return const previous = selected diff --git a/packages/tui/test/context/session-tabs-model.test.ts b/packages/tui/test/context/session-tabs-model.test.ts index a744c2966e5..24b18f818a4 100644 --- a/packages/tui/test/context/session-tabs-model.test.ts +++ b/packages/tui/test/context/session-tabs-model.test.ts @@ -112,6 +112,14 @@ describe("session tabs", () => { expect(closeSessionTab(tabs, "missing").tabs).toBe(tabs) }) + test("closing an already removed tab picks the neighbour at its last index", () => { + const tabs = [{ sessionID: "a" }, { sessionID: "c" }] + expect(closeSessionTab(tabs, "b", 1)).toEqual({ tabs, next: "c" }) + expect(closeSessionTab(tabs, "d", 2).next).toBe("c") + expect(closeSessionTab(tabs, "b").next).toBeUndefined() + expect(closeSessionTab([], "b", 0).next).toBeUndefined() + }) + test("cycles through a filtered tab set in either direction", () => { const tabs = ["a", "c", "e"].map((sessionID) => ({ sessionID })) expect(cycleSessionTab(tabs, "c", 1)?.sessionID).toBe("e") diff --git a/packages/tui/test/context/session-tabs.test.tsx b/packages/tui/test/context/session-tabs.test.tsx index b308ee4d243..74059a98fee 100644 --- a/packages/tui/test/context/session-tabs.test.tsx +++ b/packages/tui/test/context/session-tabs.test.tsx @@ -728,6 +728,39 @@ test("closing a tab is not undone by another TUI viewing the same session", asyn } }) +test("closing a session whose tab another TUI closed selects its former neighbour", async () => { + await using temporary = await tmpdir() + const clients: Awaited>[] = [] + + try { + const first = await renderSessionTabs("one", { + state: temporary.path, + persisted: ["one", "two", "three", "four", "five"], + }) + clients.push(first) + const second = await renderSessionTabs("three", { state: temporary.path }) + clients.push(second) + await wait(() => second.tabs.tabs().length === 5, 2_000, "second client to load tabs") + + first.tabs.close("three") + await first.flush() + await wait( + () => !second.tabs.tabs().some((tab) => tab.sessionID === "three"), + 2_000, + "second client to observe close", + ) + // The other client's close leaves this view in place rather than switching sessions under the user. + expect(second.route.data).toEqual({ type: "session", sessionID: "three" }) + + second.tabs.close() + await wait(() => second.route.data.type !== "session" || second.route.data.sessionID !== "three") + expect(second.route.data).toEqual({ type: "session", sessionID: "four" }) + expect(second.tabs.tabs().map((tab) => tab.sessionID)).toEqual(["one", "two", "four", "five"]) + } finally { + await Promise.allSettled(clients.map((client) => client.destroy())) + } +}) + test("user prompt admissions pulse an already-busy background tab", async () => { const setup = await renderSessionTabs("background", { persisted: ["background"] })