From 46ba9c81703fc6e7db7e623a607eeaab94fcd00f Mon Sep 17 00:00:00 2001 From: Shoubhit Dash Date: Fri, 13 Mar 2026 16:43:41 +0530 Subject: perf(app): use cursor session history loading (#17329) --- .../context/global-sync/session-prefetch.test.ts | 49 ++++++++++++++++++---- .../src/context/global-sync/session-prefetch.ts | 15 +++++++ 2 files changed, 56 insertions(+), 8 deletions(-) (limited to 'packages/app/src/context/global-sync') diff --git a/packages/app/src/context/global-sync/session-prefetch.test.ts b/packages/app/src/context/global-sync/session-prefetch.test.ts index f039b02ca..1ce833469 100644 --- a/packages/app/src/context/global-sync/session-prefetch.test.ts +++ b/packages/app/src/context/global-sync/session-prefetch.test.ts @@ -5,6 +5,7 @@ import { getSessionPrefetch, runSessionPrefetch, setSessionPrefetch, + shouldSkipSessionPrefetch, } from "./session-prefetch" describe("session prefetch", () => { @@ -16,11 +17,12 @@ describe("session prefetch", () => { directory: "/tmp/a", sessionID: "ses_1", limit: 200, + cursor: "abc", complete: false, at: 123, }) - expect(getSessionPrefetch("/tmp/a", "ses_1")).toEqual({ limit: 200, complete: false, at: 123 }) + expect(getSessionPrefetch("/tmp/a", "ses_1")).toEqual({ limit: 200, cursor: "abc", complete: false, at: 123 }) expect(getSessionPrefetch("/tmp/b", "ses_1")).toBeUndefined() clearSessionPrefetch("/tmp/a", ["ses_1"]) @@ -38,26 +40,57 @@ describe("session prefetch", () => { sessionID: "ses_2", task: async () => { calls += 1 - return { limit: 100, complete: true, at: 456 } + return { limit: 100, cursor: "next", complete: true, at: 456 } }, }) const [a, b] = await Promise.all([run(), run()]) expect(calls).toBe(1) - expect(a).toEqual({ limit: 100, complete: true, at: 456 }) - expect(b).toEqual({ limit: 100, complete: true, at: 456 }) + expect(a).toEqual({ limit: 100, cursor: "next", complete: true, at: 456 }) + expect(b).toEqual({ limit: 100, cursor: "next", complete: true, at: 456 }) }) test("clears a whole directory", () => { - setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_1", limit: 10, complete: true, at: 1 }) - setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_2", limit: 20, complete: false, at: 2 }) - setSessionPrefetch({ directory: "/tmp/e", sessionID: "ses_1", limit: 30, complete: true, at: 3 }) + setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_1", limit: 10, cursor: "a", complete: true, at: 1 }) + setSessionPrefetch({ directory: "/tmp/d", sessionID: "ses_2", limit: 20, cursor: "b", complete: false, at: 2 }) + setSessionPrefetch({ directory: "/tmp/e", sessionID: "ses_1", limit: 30, cursor: "c", complete: true, at: 3 }) clearSessionPrefetchDirectory("/tmp/d") expect(getSessionPrefetch("/tmp/d", "ses_1")).toBeUndefined() expect(getSessionPrefetch("/tmp/d", "ses_2")).toBeUndefined() - expect(getSessionPrefetch("/tmp/e", "ses_1")).toEqual({ limit: 30, complete: true, at: 3 }) + expect(getSessionPrefetch("/tmp/e", "ses_1")).toEqual({ limit: 30, cursor: "c", complete: true, at: 3 }) + }) + + test("refreshes stale first-page prefetched history", () => { + expect( + shouldSkipSessionPrefetch({ + message: true, + info: { limit: 200, cursor: "x", complete: false, at: 1 }, + chunk: 200, + now: 1 + 15_001, + }), + ).toBe(false) + }) + + test("keeps deeper or complete history cached", () => { + expect( + shouldSkipSessionPrefetch({ + message: true, + info: { limit: 400, cursor: "x", complete: false, at: 1 }, + chunk: 200, + now: 1 + 15_001, + }), + ).toBe(true) + + expect( + shouldSkipSessionPrefetch({ + message: true, + info: { limit: 120, complete: true, at: 1 }, + chunk: 200, + now: 1 + 15_001, + }), + ).toBe(true) }) }) diff --git a/packages/app/src/context/global-sync/session-prefetch.ts b/packages/app/src/context/global-sync/session-prefetch.ts index 10877b063..608561f85 100644 --- a/packages/app/src/context/global-sync/session-prefetch.ts +++ b/packages/app/src/context/global-sync/session-prefetch.ts @@ -4,10 +4,23 @@ export const SESSION_PREFETCH_TTL = 15_000 type Meta = { limit: number + cursor?: string complete: boolean at: number } +export function shouldSkipSessionPrefetch(input: { message: boolean; info?: Meta; chunk: number; now?: number }) { + if (input.message) { + if (!input.info) return true + if (input.info.complete) return true + if (input.info.limit > input.chunk) return true + } else { + if (!input.info) return false + } + + return (input.now ?? Date.now()) - input.info.at < SESSION_PREFETCH_TTL +} + const cache = new Map() const inflight = new Map>() const rev = new Map() @@ -53,11 +66,13 @@ export function setSessionPrefetch(input: { directory: string sessionID: string limit: number + cursor?: string complete: boolean at?: number }) { cache.set(key(input.directory, input.sessionID), { limit: input.limit, + cursor: input.cursor, complete: input.complete, at: input.at ?? Date.now(), }) -- cgit v1.2.3