diff --git a/packages/tui/src/context/session-tabs.tsx b/packages/tui/src/context/session-tabs.tsx index 49d1116a37f..8530940fc53 100644 --- a/packages/tui/src/context/session-tabs.tsx +++ b/packages/tui/src/context/session-tabs.tsx @@ -52,7 +52,7 @@ const empty = (): TabsState => ({ tabs: [], unread: {} }) // Deliberately after connect settles: the visible session's mount syncs win the first slots. const TAB_PREFETCH_DELAY = 300 // Tab labels read Location and VCS info, and serving either boots that Location on the server -// (config, plugins, MCP servers). Load the first tabs on connect; the rest load when opened. +// (config, plugins, MCP servers). Load the most relevant tabs on connect; the rest load when opened. const TAB_LOCATION_PREFETCH_LIMIT = 20 const VIEW_RETRY_DELAY = 250 const VIEW_RETRY_MAX_DELAY = 5_000 @@ -295,10 +295,29 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp void (async () => { await Promise.allSettled(sessionIDs.map((sessionID) => data.session.sync(sessionID, { children: true }))) if (stale) return + // Prefer the current tab, then tabs that need attention, then recently visited tabs (this + // client's history survives a service restart), then recent session activity. The stable + // sort keeps strip order as the final tie-break. + const visited = history.entries.toReversed() + const tier = (sessionID: string) => { + if (sessionID === current()) return 0 + const tab = status(sessionID) + return tab.busy || tab.attention || tab.unread ? 1 : 2 + } + const recency = (sessionID: string) => { + const index = visited.indexOf(sessionID) + return index === -1 ? visited.length : index + } + const activity = (sessionID: string) => { + const time = data.session.get(sessionID)?.time + return time ? Math.max(time.updated, time.idle ?? 0, time.viewed ?? 0) : 0 + } const locations = new Map( state() - .tabs.slice(0, TAB_LOCATION_PREFETCH_LIMIT) - .map((tab) => data.session.get(tab.sessionID)?.location) + .tabs.map((tab) => tab.sessionID) + .toSorted((a, b) => tier(a) - tier(b) || recency(a) - recency(b) || activity(b) - activity(a)) + .slice(0, TAB_LOCATION_PREFETCH_LIMIT) + .map((sessionID) => data.session.get(sessionID)?.location) .filter((location) => location !== undefined) .map((location) => [locationKey(location), location]), ) diff --git a/packages/tui/test/context/session-tabs.test.tsx b/packages/tui/test/context/session-tabs.test.tsx index 68f93dd26e9..4881f6ede4f 100644 --- a/packages/tui/test/context/session-tabs.test.tsx +++ b/packages/tui/test/context/session-tabs.test.tsx @@ -290,6 +290,26 @@ test("loads label metadata for the first 20 tabs and the rest when opened", asyn } }) +test("prefers the current, unread, and recently active tabs for label metadata", async () => { + const sessionIDs = Array.from({ length: 25 }, (_, index) => `tab${index}`) + const directories = Object.fromEntries(sessionIDs.map((sessionID) => [sessionID, `${directory}/${sessionID}`])) + const setup = await renderSessionTabs("tab24", { + persisted: sessionIDs, + sessionDirectories: directories, + sessionTimes: { tab23: { idle: 5 }, tab22: { idle: 10, viewed: 10 } }, + }) + + try { + // Current tab24, unread tab23, recently active tab22, then strip order fills the remaining 17. + const eager = ["tab24", "tab23", "tab22", ...sessionIDs.slice(0, 17)].map((sessionID) => directories[sessionID]) + await wait(() => eager.every((dir) => setup.locations.includes(dir) && setup.vcsLocations.includes(dir))) + await Bun.sleep(100) + expect(sessionIDs.slice(17, 22).some((sessionID) => setup.locations.includes(directories[sessionID]))).toBe(false) + } finally { + await setup.destroy() + } +}) + test("opens a background tab without changing the current session", async () => { const setup = await renderSessionTabs("first")