From ee6922f7055e333b2eb30b5124e75125f5170aba Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:26:22 -0600 Subject: [PATCH] perf(tasks): open recent tasks without waiting on a slow ten-task preload (#21939) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A person opens a task from the sidebar's Recent Tasks, the Inbox or the Tasks list, and the task chat shows its first frame from a `chat-bootstrap` read > - On staging, a task opened from Recent Tasks soon after a page load took 15.3 s to show its chat. The same task from the Inbox took 0.19 s > - Two defects cause this. Every task's bootstrap paid a fixed ~1.8 s for its latest runs. Recent Tasks preloaded ten tasks in one request (17.7 s), and an open waited for that whole request > - Recent Tasks also warmed its tasks only once per page load. Unread cache entries are dropped after five minutes, so its tasks were cold for the rest of the session > - This pull request makes the runs read use indexes, lets an open read its own task instead of waiting for a ten-task preload, and re-warms Recent Tasks on navigation > - The benefit is that a task from Recent Tasks opens as fast as from a task list ## Linked Issues or Issue Description Refs #19818, Refs #16713 (earlier proposals of the same `runsForIssue` rewrite; this PR adds a query-plan regression test and the client fixes). **What happened?** On staging, a click on a Recent Tasks link about 6 s after a page load took 15.3 s until the chat thread showed. The Inbox opened the same task in 0.19 s. **Steps to reproduce** 1. Load a page with a populated Recent Tasks sidebar. 2. Within a few seconds, click a recent task. 3. The chat stays on its loading state for many seconds. **Expected behavior** The task opens about as fast as from the Inbox or the Tasks list. **Root cause (measured on staging)** - `GET /issues/:id/runs?view=recent` took 1.7–1.9 s of server time for tasks with 1 to 24 runs. Every other first-frame read took 80–350 ms. The query filtered with `issue_id = $1 OR EXISTS (activity_log … run_id = heartbeat_runs.id)`, so Postgres walked every run of the company. - The bootstrap reads two tasks at a time, so the Recents preload of ten tasks took 17.7 s. Each task in a batch settles only with the whole request, and the page's reads wait for it (up to the 5 s share deadline, then read again under load). - `useRecentTaskOpenWarmup` remembered preloaded task ids for the life of the page and never preloaded them again. ## What Changed - `activityService.runsForIssue`: select the task's runs by `id IN (runs with issue_id = task UNION run ids from the task's activity)`. Each half uses its own index. The results are the same. - `startIssueChatBootstrap`: when the only request for a task is a background batch that is still loading, send a request for that task alone. All refs of the task point to the new request. The later batch answer only fills keys that are still empty or older. - `loadIssueChatBootstraps` (background): a task whose answer did not arrive is not held back for five minutes, so the next preload can retry it. - `useRecentTaskOpenWarmup`: use the background preload (five-minute repeat, the same as the cache GC time) and run again after each navigation (`navigationKey`). It no longer keeps a per-page-load set. `SidebarRecentTasks` and `CoordinatorSidebar` pass the pathname. ## Verification - `server/src/__tests__/issue-runs-read-cost.test.ts` (new): a company with 20,003 runs, and a task with 3 runs. EXPLAIN ANALYZE of the runs reads examined **40,011** run rows before the fix and **15** after (red/green). The test also checks the run order and the run that is linked only through activity. - `ui/src/lib/issue-chat-bootstrap.test.ts`: a new test opens a task while a ten-task preload is still loading. Before the fix it timed out waiting on the batch. After the fix it reads its own request. A second new test checks that a single-task request is still shared. - `ui/src/hooks/useRecentTaskOpenWarmup.test.tsx`: a new test re-warms Recents on a navigation after five minutes (red on the old hook). Another new test sends nothing for navigations inside five minutes. - Existing suites pass: activity service/routes, task cold open, agent conversations (197 tests); sidebar, Recents, navigation prefetch, issue detail cache, IssueDetail (341 tests). - `tsc --noEmit` is clean for `server` and `ui` (apart from the pre-existing Storybook index errors). - `pnpm verify` exits 2 (an opaque server setup dependency), so CI supplies the broad coverage. - After deploy: on staging, time a Recent Tasks click a few seconds after a cold load, and time `runs?view=recent`. ## Risks - Low risk. The SQL change keeps the same filter. It changes only how Postgres finds the candidate runs. The existing activity tests cover retry, credential and conversation cases. - An open during a background preload now sends one more bootstrap request for that task. This happens only on an open or a resting hover, never for each row. - Recents now sends at most one preload per five minutes while the person navigates. It sends nothing while the tab is idle or hidden. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, browser measurement on staging, code execution). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- .../__tests__/issue-runs-read-cost.test.ts | 81 +++++++++++++++++++ server/src/services/activity.ts | 24 +++--- ui/src/components/CoordinatorSidebar.tsx | 4 +- ui/src/components/SidebarRecentTasks.tsx | 2 +- ui/src/hooks/useRecentTaskOpenWarmup.test.tsx | 59 +++++++++++--- ui/src/hooks/useRecentTaskOpenWarmup.ts | 38 +++------ ui/src/lib/issue-chat-bootstrap.test.ts | 34 ++++++++ ui/src/lib/issue-chat-bootstrap.ts | 79 ++++++++++++++---- 8 files changed, 256 insertions(+), 65 deletions(-) create mode 100644 server/src/__tests__/issue-runs-read-cost.test.ts 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;