fix(tui): rank tabs for label metadata prefetch

Prefetch the current tab, tabs needing attention, recently visited tabs, then recently active sessions, with strip order as the tie-break.
This commit is contained in:
jlongster 2026-10-19 18:20:22 +00:00
parent 90544e6c04
commit 481bd700ce
2 changed files with 42 additions and 3 deletions

View file

@ -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]),
)

View file

@ -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")