diff --git a/server/src/__tests__/issue-runs-read-cost.test.ts b/server/src/__tests__/issue-runs-read-cost.test.ts new file mode 100644 index 00000000000..20ffdca4669 --- /dev/null +++ b/server/src/__tests__/issue-runs-read-cost.test.ts @@ -0,0 +1,81 @@ +import { randomUUID } from "node:crypto"; +import { sql } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; +import { activityLog, agents, applyPendingMigrations, companies, createDb, heartbeatRuns, issues } from "@paperclipai/db"; +import { activityService } from "../services/activity.js"; +import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; + +const external = resolveExternalTestDatabaseUrl("PAPERCLIP_ISSUE_RUNS_COST_TEST_DATABASE_URL"); +const support = external ? { supported: true } : await getEmbeddedPostgresTestSupport(); +type Plan = { "Relation Name"?: string; "Actual Rows": number; "Actual Loops": number; + "Rows Removed by Filter"?: number; "Rows Removed by Index Recheck"?: number; Plans?: Plan[] }; + +// The task chat reads a task's two latest runs before its first frame (the +// chat bootstrap and `/issues/:id/runs?view=recent`). That read must cost the +// task's own runs, not every run the company ever had. +describe.skipIf(!support.supported)("issue runs read cost", () => { + let temporary: Awaited> | undefined; + let db: ReturnType; + let capture = false; + const queries: Array<{ query: string; parameters: unknown[] }> = []; + beforeAll(async () => { + if (external) await applyPendingMigrations(external); + else temporary = await startEmbeddedPostgresTestDatabase("issue-runs-cost-"); + db = createDb(external ?? temporary!.connectionString, { maxConnections: 6, idleTimeoutSeconds: 0, maxLifetimeSeconds: 0 }); + db.$client.options.onnotice = () => {}; + db.$client.options.debug = (_connection, query, parameters) => { + if (capture && /from "heartbeat_runs"/.test(query)) queries.push({ query, parameters: [...parameters] }); + }; + }, 30_000); + afterEach(async () => { capture = false; queries.length = 0; await db.execute(sql`truncate companies cascade`); }); + afterAll(async () => { await db?.$client.end({ timeout: 1 }); await temporary?.cleanup(); }); + + it("reads a task's latest runs without walking the company's run history", async () => { + const companyId = randomUUID(), agentId = randomUUID(); + await db.insert(companies).values({ id: companyId, name: "Busy", issuePrefix: `BR${companyId.slice(0, 6)}` }); + await db.insert(agents).values({ id: agentId, companyId, name: "Worker", role: "engineer", status: "active", + adapterType: "codex_local", adapterConfig: {}, runtimeConfig: {}, permissions: {} }); + const [task, other] = await db.insert(issues).values([ + { companyId, title: "Opened task", status: "in_progress" }, + { companyId, title: "Other task", status: "in_progress" }, + ]).returning(); + // 20,000 runs of other work, newer than the task's own runs. + await db.execute(sql`insert into heartbeat_runs (company_id, agent_id, issue_id, status, created_at) + select ${companyId}::uuid, ${agentId}::uuid, ${other!.id}::uuid, 'succeeded', + '2026-06-01T00:00:00Z'::timestamptz + n * interval '1 second' from generate_series(1, 20000) n`); + // The task's runs: two linked directly, one only through its activity. + const [direct1, direct2, viaActivity] = await db.insert(heartbeatRuns).values([ + { companyId, agentId, issueId: task!.id, status: "succeeded", createdAt: new Date("2026-01-01T00:00:01Z") }, + { companyId, agentId, issueId: task!.id, status: "succeeded", createdAt: new Date("2026-01-01T00:00:02Z") }, + { companyId, agentId, status: "succeeded", createdAt: new Date("2026-01-01T00:00:03Z") }, + ]).returning(); + await db.insert(activityLog).values({ companyId, actorType: "agent", actorId: agentId, action: "issue.updated", + entityType: "issue", entityId: task!.id, runId: viaActivity!.id }); + await db.execute(sql`analyze heartbeat_runs`); + await db.execute(sql`analyze activity_log`); + + capture = true; + const recent = await activityService(db).runsForIssue(companyId, task!.id, null, 2, undefined, { skipLivenessBackfill: true }); + const all = await activityService(db).runsForIssue(companyId, task!.id, null, undefined, undefined, { skipLivenessBackfill: true }); + capture = false; + + expect(recent.map(run => run.runId)).toEqual([viaActivity!.id, direct2!.id]); + expect(all.map(run => run.runId)).toEqual([viaActivity!.id, direct2!.id, direct1!.id]); + + expect(queries.length).toBeGreaterThan(0); + let runRowsExamined = 0; + for (const statement of queries) { + const result = await db.$client.unsafe("EXPLAIN (ANALYZE, FORMAT JSON) " + statement.query, statement.parameters as never[]); + const visit = (node: Plan) => { + if (node["Relation Name"] === "heartbeat_runs") runRowsExamined += node["Actual Loops"] + * (node["Actual Rows"] + (node["Rows Removed by Filter"] ?? 0) + (node["Rows Removed by Index Recheck"] ?? 0)); + node.Plans?.forEach(visit); + }; + visit((result[0]!["QUERY PLAN"] as Array<{ Plan: Plan }>)[0]!.Plan); + } + process.stdout.write(JSON.stringify({ issueRunsReadCost: { companyRuns: 20003, runRowsExamined, queryCount: queries.length } }) + "\n"); + // Two reads of a three-run task. The other task's 20,000 runs must not be scanned. + expect(runRowsExamined).toBeLessThanOrEqual(100); + }, 120_000); +}); diff --git a/server/src/services/activity.ts b/server/src/services/activity.ts index 4fa79b84104..be5a8a7da75 100644 --- a/server/src/services/activity.ts +++ b/server/src/services/activity.ts @@ -575,17 +575,19 @@ export function activityService(db: Db) { or coalesce(${heartbeatRuns.nativeIssueId}::text, ${heartbeatRuns.issueId}::text, ${heartbeatRuns.contextSnapshot} ->> 'issueId', ${heartbeatRuns.contextSnapshot} ->> 'taskId') = ${issueId})`, ...(selectedRunIds ? [inArray(heartbeatRuns.id, selectedRunIds)] : []), - or( - eq(heartbeatRuns.issueId, issueId), - sql`exists ( - select 1 - from ${activityLog} - where ${activityLog.companyId} = ${companyId} - and ${activityLog.entityType} = 'issue' - and ${activityLog.entityId} = ${issueId} - and ${activityLog.runId} = ${heartbeatRuns.id} - )`, - ), + // The task's runs: linked directly, or through the task's activity. + // Collect the candidate ids first, each half by its own index. An + // OR with a per-run EXISTS walks every run of the company instead. + sql`${heartbeatRuns.id} in ( + select hr.id from ${heartbeatRuns} hr + where hr.company_id = ${companyId} and hr.issue_id = ${issueId} + union + select al.run_id from ${activityLog} al + where al.company_id = ${companyId} + and al.entity_type = 'issue' + and al.entity_id = ${issueId} + and al.run_id is not null + )`, ), ) .orderBy(desc(heartbeatRuns.createdAt), desc(heartbeatRuns.id)); diff --git a/ui/src/components/CoordinatorSidebar.tsx b/ui/src/components/CoordinatorSidebar.tsx index 6529af2ba2c..8895ac8150c 100644 --- a/ui/src/components/CoordinatorSidebar.tsx +++ b/ui/src/components/CoordinatorSidebar.tsx @@ -127,9 +127,9 @@ export function CoordinatorSidebar({ children }: { children?: ReactNode }) { const { collapsed, peeking, isMobile, setSidebarOpen } = useSidebar(); const rail = collapsed && !peeking; // Warm opens only while the Recents list is on screen. - useRecentTaskOpenWarmup({ companyId, userId, tasks: !rail && session.isSuccess ? recentTasks : NO_RECENT_TASKS }); - const { openNewIssue } = useDialogActions(); const location = useLocation(); + useRecentTaskOpenWarmup({ companyId, userId, tasks: !rail && session.isSuccess ? recentTasks : NO_RECENT_TASKS, navigationKey: location.pathname }); + const { openNewIssue } = useDialogActions(); const hierarchy = useMemo(() => recentTaskHierarchy(recentTasks, location.pathname), [recentTasks, location.pathname]); const attentionCount = useCoordinatorAttentionCount(companyId); const { data: projects } = useQuery({ ...companyProjectsQuery(companyId!), enabled: !!companyId }); diff --git a/ui/src/components/SidebarRecentTasks.tsx b/ui/src/components/SidebarRecentTasks.tsx index 9f7c2aeb217..5033771ea73 100644 --- a/ui/src/components/SidebarRecentTasks.tsx +++ b/ui/src/components/SidebarRecentTasks.tsx @@ -117,7 +117,7 @@ function RecentTasksList({ const [renameValue, setRenameValue] = useState(""); const [pendingAction, setPendingAction] = useState<"rename" | "archive" | "pause" | null>(null); const restartRetryStorageKey = restartWakeRetryStorageKey(companyId, userId); - useRecentTaskOpenWarmup({ companyId, userId, tasks: entries }); + useRecentTaskOpenWarmup({ companyId, userId, tasks: entries, navigationKey: pathname }); if (entries.length === 0) return null; diff --git a/ui/src/hooks/useRecentTaskOpenWarmup.test.tsx b/ui/src/hooks/useRecentTaskOpenWarmup.test.tsx index 3810c77d7d0..7a95e90ff96 100644 --- a/ui/src/hooks/useRecentTaskOpenWarmup.test.tsx +++ b/ui/src/hooks/useRecentTaskOpenWarmup.test.tsx @@ -8,8 +8,8 @@ import { TASK_COLLECTION_PAINTED_EVENT } from "@/lib/issue-detail-preload-events import type { Issue } from "@paperclipai/shared"; import { seedIssueChatBootstrap, type IssueChatBootstrapEntry } from "@/lib/issueChatBootstrapCache"; import { queryKeys } from "@/lib/queryKeys"; -import { resetIssueChatBootstrapsForTest } from "@/lib/issue-chat-bootstrap"; -import { resetRecentTaskChatPreloadForTests, useRecentTaskOpenWarmup, type RecentTaskOpenWarmupTask } from "./useRecentTaskOpenWarmup"; +import { CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS, resetIssueChatBootstrapsForTest } from "@/lib/issue-chat-bootstrap"; +import { useRecentTaskOpenWarmup, type RecentTaskOpenWarmupTask } from "./useRecentTaskOpenWarmup"; const chatBootstrap = vi.hoisted(() => vi.fn()); vi.mock("@/api/issues", () => ({ issuesApi: { chatBootstrap } })); @@ -41,23 +41,22 @@ let root: Root; let queryClient: QueryClient; let visibility: DocumentVisibilityState; -function Probe(props: { companyId: string | null; userId: string | null; tasks: RecentTaskOpenWarmupTask[] }) { +function Probe(props: { companyId: string | null; userId: string | null; tasks: RecentTaskOpenWarmupTask[]; navigationKey?: string }) { useRecentTaskOpenWarmup(props); return null; } -function render(props: { companyId?: string | null; userId?: string | null; tasks: RecentTaskOpenWarmupTask[] }) { +function render(props: { companyId?: string | null; userId?: string | null; tasks: RecentTaskOpenWarmupTask[]; navigationKey?: string }) { act(() => { root.render( - + , ); }); } beforeEach(() => { - resetRecentTaskChatPreloadForTests(); resetIssueChatBootstrapsForTest(); chatBootstrap.mockReset(); chatBootstrap.mockImplementation(async (_companyId: string, ids: string[]) => ({ @@ -72,6 +71,7 @@ beforeEach(() => { }); afterEach(() => { + vi.useRealTimers(); act(() => root.unmount()); container.remove(); queryClient.clear(); @@ -184,8 +184,8 @@ describe("useRecentTaskOpenWarmup", () => { expect(chatBootstrap).toHaveBeenCalledTimes(1); expect(chatBootstrap.mock.calls[0]![1]).toEqual(["id-2"]); - // Now all three are cached in full: no request at all. - resetRecentTaskChatPreloadForTests(); + // Now all three are cached in full: no request at all, even past the repeat window. + resetIssueChatBootstrapsForTest(); chatBootstrap.mockClear(); act(() => root.unmount()); root = createRoot(container); @@ -194,11 +194,50 @@ describe("useRecentTaskOpenWarmup", () => { expect(chatBootstrap).not.toHaveBeenCalled(); }); - it("preloads once per account per page load and never seeds another company's tasks", async () => { - render({ tasks: tasks(1) }); + it("sends nothing new on navigations within five minutes of a warm", async () => { + render({ tasks: tasks(2), navigationKey: "/PAP/inbox" }); + await settle(); + render({ tasks: tasks(2), navigationKey: "/PAP/issues/PAP-9" }); await settle(); act(() => root.unmount()); root = createRoot(container); + render({ tasks: tasks(2), navigationKey: "/PAP/dashboard" }); + await settle(); + expect(chatBootstrap).toHaveBeenCalledTimes(1); + }); + + it("warms Recents again on a navigation once the earlier warm may have been dropped", async () => { + // The sidebar stays mounted for hours; unread cache entries are dropped after five minutes. + vi.useFakeTimers({ toFake: ["Date"] }); + render({ tasks: tasks(2), navigationKey: "/PAP/inbox" }); + await settle(); + expect(chatBootstrap).toHaveBeenCalledTimes(1); + queryClient.clear(); + vi.setSystemTime(Date.now() + CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS + 1); + render({ tasks: tasks(2), navigationKey: "/PAP/dashboard" }); + await settle(); + expect(chatBootstrap).toHaveBeenCalledTimes(2); + expect(chatBootstrap.mock.calls[1]![1]).toEqual(["id-0", "id-1"]); + expect(queryClient.getQueryData([...queryKeys.issues.detail("PAP-1"), "core"])).toMatchObject({ id: "id-1" }); + }); + + it("preloads the same task again for another account signed in within five minutes", async () => { + queryClient.setQueryData(queryKeys.auth.session, { user: { id: "user-1" } }); + render({ tasks: tasks(1) }); + await settle(); + expect(chatBootstrap).toHaveBeenCalledTimes(1); + // A sign-out clears the cached tasks; the next account opens the same task. + queryClient.clear(); + queryClient.setQueryData(queryKeys.auth.session, { user: { id: "user-2" } }); + act(() => root.unmount()); + root = createRoot(container); + render({ userId: "user-2", tasks: tasks(1) }); + await settle(); + expect(chatBootstrap).toHaveBeenCalledTimes(2); + expect(queryClient.getQueryData([...queryKeys.issues.detail("PAP-0"), "core"])).toMatchObject({ id: "id-0" }); + }); + + it("never seeds another company's tasks", async () => { render({ tasks: tasks(1) }); await settle(); expect(chatBootstrap).toHaveBeenCalledTimes(1); diff --git a/ui/src/hooks/useRecentTaskOpenWarmup.ts b/ui/src/hooks/useRecentTaskOpenWarmup.ts index a135f25aa81..c7744d75b73 100644 --- a/ui/src/hooks/useRecentTaskOpenWarmup.ts +++ b/ui/src/hooks/useRecentTaskOpenWarmup.ts @@ -1,34 +1,36 @@ import { useEffect, useRef } from "react"; -import { useQueryClient, type QueryClient } from "@tanstack/react-query"; +import { useQueryClient } from "@tanstack/react-query"; import { ISSUE_CHAT_BOOTSTRAP_MAX_IDS } from "@paperclipai/shared"; import { loadIssueChatBootstraps } from "@/lib/issue-chat-bootstrap"; -import { hasFreshIssueChatFirstFrame } from "@/lib/issueChatBootstrapCache"; import { whenPageSettles } from "@/lib/when-page-settles"; import { useWarmTaskDetailCode } from "./useWarmTaskDetailCode"; export type RecentTaskOpenWarmupTask = { id: string; identifier: string | null }; -let preloadedScopes = new WeakMap>>(); - -export function resetRecentTaskChatPreloadForTests() { - preloadedScopes = new WeakMap(); -} - /** * Make opening a recent task from the sidebar nearly instant. * * Once Recents has painted, warm the task-detail code as task lists do. Then, * once the page is idle, read the chat's first frame for the most recent tasks * in one `chat-bootstrap` request and seed it into the task page's caches. + * + * The sidebar stays mounted for the whole session, so it repeats this after + * each navigation (`navigationKey`), as a task list does each time it is + * shown. Like a list's preload it skips tasks cached and fresh, loading, or + * preloaded in the last five minutes, so most navigations send nothing. A warm + * that is never repeated goes cold: unread cache entries are dropped after + * five minutes. */ export function useRecentTaskOpenWarmup({ companyId, userId, tasks, + navigationKey, }: { companyId: string | null | undefined; userId: string | null | undefined; tasks: readonly RecentTaskOpenWarmupTask[]; + navigationKey?: string; }) { const queryClient = useQueryClient(); const hasTasks = tasks.length > 0; @@ -39,28 +41,14 @@ export function useRecentTaskOpenWarmup({ latestTasks.current = tasks; useEffect(() => { if (!companyId || !hasTasks || typeof window.requestAnimationFrame !== "function") return; - const scope = `${companyId}:${userId ?? "__local_board__"}`; - let scopes = preloadedScopes.get(queryClient); - if (!scopes) preloadedScopes.set(queryClient, scopes = new Map()); - let preloaded = scopes.get(scope); - if (!preloaded) scopes.set(scope, preloaded = new Set()); - const remembered = preloaded; const controller = new AbortController(); const cancel = whenPageSettles(queryClient, () => { - // Skips tasks already cached or loading (a task list may have preloaded them). - const top = latestTasks.current.slice(0, ISSUE_CHAT_BOOTSTRAP_MAX_IDS).filter(task => !remembered.has(task.id)); - if (!top.length) return; - for (const task of top) remembered.add(task.id); - void loadIssueChatBootstraps(queryClient, companyId, top, { signal: controller.signal }).then(() => { - for (const task of top) { - const refs = [task.id, task.identifier].filter((ref): ref is string => !!ref); - if (!hasFreshIssueChatFirstFrame(queryClient, refs)) remembered.delete(task.id); - } - }); + const top = latestTasks.current.slice(0, ISSUE_CHAT_BOOTSTRAP_MAX_IDS); + void loadIssueChatBootstraps(queryClient, companyId, top, { signal: controller.signal, background: true }); }); return () => { cancel(); controller.abort(); }; - }, [companyId, hasTasks, queryClient, userId, topTasksKey]); + }, [companyId, hasTasks, queryClient, userId, topTasksKey, navigationKey]); } diff --git a/ui/src/lib/issue-chat-bootstrap.test.ts b/ui/src/lib/issue-chat-bootstrap.test.ts index 0bbcb4e3803..4ae25cd819e 100644 --- a/ui/src/lib/issue-chat-bootstrap.test.ts +++ b/ui/src/lib/issue-chat-bootstrap.test.ts @@ -16,6 +16,7 @@ import { } from "./issue-chat-bootstrap"; import { queryKeys } from "./queryKeys"; import { ISSUE_CHAT_BOOTSTRAP_FRESH_MS } from "./issueChatBootstrapCache"; +import { recentIssueActivityQueryKey } from "./issue-chat-history-queries"; const chatBootstrap = vi.hoisted(() => vi.fn()); vi.mock("@/api/issues", () => ({ issuesApi: { chatBootstrap } })); @@ -207,6 +208,39 @@ describe("startIssueChatBootstrap", () => { expect(read).toHaveBeenCalledOnce(); }); + it("opens a task on its own request instead of waiting for a slow preload of ten tasks", async () => { + // Recents' preload of ten tasks is still loading (each task costs the server time). + let answerBatch!: (value: unknown) => void; + chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answerBatch = resolve; })); + const batchRefs = Array.from({ length: 10 }, (_, index) => ({ id: idFor(index + 1), identifier: `ABC-${index + 1}` })); + void loadIssueChatBootstraps(client, "company-1", batchRefs); + await Promise.resolve(); + // The person opens one of them: the page reads by identifier, a later read by id. + startIssueChatBootstrap(client, "company-1", "ABC-4"); + const ownRead = vi.fn(); + const byIdentifier = readFromIssueChatBootstrap(client, ["unread", "ABC-4"], "ABC-4", (bootstrap) => bootstrap.recentActivity, ownRead); + const byId = readFromIssueChatBootstrap(client, ["unread", idFor(4)], idFor(4), (bootstrap) => bootstrap.issue.identifier, ownRead); + await expect(byIdentifier).resolves.toEqual([{ id: "event-4" }]); + await expect(byId).resolves.toBe("ABC-4"); + expect(ownRead).not.toHaveBeenCalled(); + expect(requestedRefs()).toEqual([batchRefs.map((task) => task.id).join(","), "ABC-4"]); + // The preload's later answer seeds the other tasks and does not replace the newer read. + answerBatch({ items: batchRefs.map((task) => item(task.id, { recentActivity: [{ id: "stale" }] as never })) }); + await vi.waitFor(() => expect(client.getQueryData([...queryKeys.issues.detail("ABC-5"), "core"])).toMatchObject({ identifier: "ABC-5" })); + expect(client.getQueryData(recentIssueActivityQueryKey("ABC-4"))).toEqual([{ id: "event-4" }]); + }); + + it("still shares a loading request that was made for this task alone", async () => { + let answer!: (value: unknown) => void; + chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; })); + startIssueChatBootstrap(client, "company-1", "ABC-7"); + await Promise.resolve(); + startIssueChatBootstrap(client, "company-1", "ABC-7"); + answer(respondWith(["ABC-7"])); + await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-7"], "ABC-7", (bootstrap) => bootstrap.issue.identifier, vi.fn())).resolves.toBe("ABC-7"); + expect(requestedRefs()).toEqual(["ABC-7"]); + }); + it("ignores an early fetch for another task and loads this one through the company endpoint", async () => { (window as unknown as Record)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-1", promise: Promise.resolve(item("ABC-1")) }; startIssueChatBootstrap(client, "company-1", "ABC-2"); diff --git a/ui/src/lib/issue-chat-bootstrap.ts b/ui/src/lib/issue-chat-bootstrap.ts index 7b5730dbedb..b9d8fd758ec 100644 --- a/ui/src/lib/issue-chat-bootstrap.ts +++ b/ui/src/lib/issue-chat-bootstrap.ts @@ -49,8 +49,12 @@ export const CHAT_BOOTSTRAP_SHARE_DEADLINE_MS = 5_000; */ type Owner = { account: string | null | undefined; revoked: boolean }; -/** One task's part of a bootstrap request. It is never used by another account. */ -type Entry = { promise: Promise; owner: Owner; startedAt: number; settledAt?: number }; +/** + * One task's part of a bootstrap request. It is never used by another account. + * `batched` marks a part of a request for several tasks: it settles only when + * the slowest of them has been read. + */ +type Entry = { promise: Promise; owner: Owner; startedAt: number; settledAt?: number; batched: boolean }; const entries = new Map(); function refKey(ref: string) { @@ -126,10 +130,10 @@ function forget(entry: Entry) { * same task twice, even while a request is in flight. Expired entries are * dropped, so answers do not stay in memory. */ -function track(queryClient: QueryClient, refs: readonly string[], owner: Owner, startedAt: number, promise: Promise) { +function track(queryClient: QueryClient, refs: readonly string[], owner: Owner, startedAt: number, promise: Promise, batched = false) { const now = Date.now(); for (const [key, entry] of entries) if (isExpired(entry, now)) entries.delete(key); - const entry: Entry = { startedAt, owner, promise }; + const entry: Entry = { startedAt, owner, promise, batched }; for (const ref of refs) entries.set(refKey(ref), entry); void promise.then((item) => { entry.settledAt = Date.now(); @@ -163,7 +167,7 @@ function startChunk(queryClient: QueryClient, companyId: string, wanted: readonl return byRef; }); for (const target of wanted) { - track(queryClient, target.refs, owner, requestedAt, request.then((byRef) => byRef.get(refKey(target.ref)), () => undefined)); + track(queryClient, target.refs, owner, requestedAt, request.then((byRef) => byRef.get(refKey(target.ref)), () => undefined), wanted.length > 1); } return request.then(() => undefined, () => undefined); } @@ -175,18 +179,40 @@ function startChunk(queryClient: QueryClient, companyId: string, wanted: readonl * does not need to preload the same task again every 30 seconds. */ export const CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS = 5 * 60_000; -const backgroundPreloadedAt = new Map(); +/** + * When each task was last preloaded in the background, per cache and per + * account: another account (after a sign-out) starts with an empty cache and + * must preload its own tasks. + */ +let backgroundPreloadedAt = new WeakMap>(); -function preloadedInBackgroundRecently(refs: readonly string[], now: number) { +function backgroundKey(queryClient: QueryClient, ref: string) { + return `${sessionAccount(queryClient) ?? ""}:${refKey(ref)}`; +} + +function backgroundPreloads(queryClient: QueryClient) { + let preloads = backgroundPreloadedAt.get(queryClient); + if (!preloads) backgroundPreloadedAt.set(queryClient, preloads = new Map()); + return preloads; +} + +function preloadedInBackgroundRecently(queryClient: QueryClient, refs: readonly string[], now: number) { + const preloads = backgroundPreloads(queryClient); return refs.some((ref) => { - const at = backgroundPreloadedAt.get(refKey(ref)); + const at = preloads.get(backgroundKey(queryClient, ref)); return at !== undefined && now - at < CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS; }); } -function rememberBackgroundPreload(refs: readonly string[], now: number) { - for (const [key, at] of backgroundPreloadedAt) if (now - at >= CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS) backgroundPreloadedAt.delete(key); - for (const ref of refs) backgroundPreloadedAt.set(refKey(ref), now); +function rememberBackgroundPreload(queryClient: QueryClient, refs: readonly string[], now: number) { + const preloads = backgroundPreloads(queryClient); + for (const [key, at] of preloads) if (now - at >= CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS) preloads.delete(key); + for (const ref of refs) preloads.set(backgroundKey(queryClient, ref), now); +} + +function forgetBackgroundPreload(queryClient: QueryClient, refs: readonly string[]) { + const preloads = backgroundPreloads(queryClient); + for (const ref of refs) preloads.delete(backgroundKey(queryClient, ref)); } /** A task to preload: a ref, or its id with the identifier it may also be cached under. */ @@ -217,15 +243,21 @@ export async function loadIssueChatBootstraps( if (!refs.length || wanted.has(refKey(refs[0]!))) continue; if (refs.some((ref) => currentEntry(queryClient, ref, now))) continue; if (hasFreshIssueChatFirstFrame(queryClient, refs, now)) continue; - if (options.background && preloadedInBackgroundRecently(refs, now)) continue; + if (options.background && preloadedInBackgroundRecently(queryClient, refs, now)) continue; wanted.set(refKey(refs[0]!), { ref: refs[0]!, refs }); } const ordered = [...wanted.values()]; for (let start = 0; start < ordered.length; start += ISSUE_CHAT_BOOTSTRAP_MAX_IDS) { if (options.signal?.aborted) return; const chunk = ordered.slice(start, start + ISSUE_CHAT_BOOTSTRAP_MAX_IDS); - if (options.background) rememberBackgroundPreload(chunk.flatMap((target) => target.refs), Date.now()); + if (options.background) rememberBackgroundPreload(queryClient, chunk.flatMap((target) => target.refs), Date.now()); await startChunk(queryClient, companyId, chunk); + // A failed or partial answer leaves those tasks cold: the next preload may retry them. + if (options.background) { + for (const target of chunk) { + if (!hasFreshIssueChatFirstFrame(queryClient, target.refs)) forgetBackgroundPreload(queryClient, target.refs); + } + } } } @@ -293,7 +325,8 @@ function startSingle(queryClient: QueryClient, ref: string) { /** * Start a single-task bootstrap for a cold open or an open intent, unless one * is loading, loaded recently, or the task's first frame is cached and fresh. - * Without a company it uses the single-task route. Resolves when the task's + * A task whose only request is a background preload of several tasks, still + * loading, gets its own request. Without a company it uses the single-task route. Resolves when the task's * bootstrap (new or already loading) settles; it never rejects. * * `aliases` are other refs the caller already knows the task by (a restored @@ -312,7 +345,21 @@ export function startIssueChatBootstrap( for (const alias of aliases) if (alias && !currentEntry(queryClient, alias)) entries.set(refKey(alias), entry); return entry.promise; }; - if (currentEntry(queryClient, ref)) return settled(); + const existing = currentEntry(queryClient, ref); + // A task being opened must not wait for the rest of a background preload + // (Recents or a list reads ten tasks in one request): it is read on its own, + // and its reads share that answer instead. The preload's later answer only + // fills what is still empty. + if (existing?.batched && existing.settledAt === undefined) { + if (companyId) void startChunk(queryClient, companyId, [{ ref, refs: [ref] }]); + else startSingle(queryClient, ref); + const single = currentEntry(queryClient, ref); + if (single && single !== existing) { + for (const [key, value] of entries) if (value === existing) entries.set(key, single); + } + return settled(); + } + if (existing) return settled(); if (adoptEarlyChatBootstrap(queryClient, companyId, ref)) return settled(); if (companyId) { void loadIssueChatBootstraps(queryClient, companyId, [ref]); @@ -367,7 +414,7 @@ export function sameIds(a: readonly string[], b: readonly string[]) { /** Test seam. */ export function resetIssueChatBootstrapsForTest() { entries.clear(); - backgroundPreloadedAt.clear(); + backgroundPreloadedAt = new WeakMap(); pendingOwners.clear(); stopWatchingSession?.(); stopWatchingSession = null;