mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 13:01:05 +01:00
fix(ui): keep new Recents tasks ready to open from cache (#21904)
Track warmed tasks per query client, company, and account so tasks added to the top Recents rows receive the existing idle preload. Preserve the batch limit, cache ownership, and retry-on-revisit behavior. Add unit and browser regressions for later additions, held API responses, editor retention, and message position. Extend controlled performance coverage and document timing results. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
991f773d7f
commit
698c371b1b
4 files changed
+164
-33
No files matched your search
@@ -77,15 +77,24 @@ three pending board approvals, retained in
|
||||
It gives eight fixture tasks twelve saved comments each and seeds them as the
|
||||
account's Recents. An idle Projects page must make exactly one
|
||||
`issues/chat-bootstrap` request (at most ten tasks) and no per-task reads.
|
||||
When a task enters the ten top rows later, Recents warms that task after the
|
||||
page settles. Tasks already warmed for this account and query client are
|
||||
skipped. Reordering the same tasks sends no new read. A failed preload can
|
||||
be tried on the next visit or change; there is no retry timer.
|
||||
Clicking a preloaded task must show its latest comment without any of the
|
||||
chat's first-frame reads (core issue, recent comments, comment input facts,
|
||||
recent activity, recent runs, interactions, attachments, live and active run).
|
||||
The full detail read and the task page's other side reads still follow the
|
||||
first frame; the spec records them as an annotation. Its timing cases measure
|
||||
click to the latest comment on screen over at least 5 fresh browser contexts
|
||||
click to the latest comment on screen with the chat revealed and composer
|
||||
editable over at least 5 fresh browser contexts
|
||||
(empty HTTP cache and storage apart from Recents), natively and with 150 ms
|
||||
added to every API response. They also record the idle page's API request
|
||||
count. The timings are attached evidence, not budgets.
|
||||
count and resource timing waterfalls. Both the initial history and a task
|
||||
added from another tab after the initial preload have separate timing cases.
|
||||
Set `PAPERCLIP_COLLECTION_PERF_PROFILE=1` for CPU profiles; use a separate run
|
||||
without that flag for latency comparisons. The timings are attached evidence,
|
||||
not budgets.
|
||||
|
||||
`mine-returning-visit.spec.ts` hard loads Mine with the saved query cache
|
||||
(`paperclip:query-cache:v1:<user>:<company>`, primed by one earlier visit in
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import { expect, test, type APIRequestContext, type Page } from "@playwright/test";
|
||||
import { collectionFixture, type CollectionFixture } from "./fixture";
|
||||
|
||||
// Opening a task from the sidebar's Recents. A page load with Recents makes at
|
||||
// most one batched chat-bootstrap read once the page is idle, and no per-task
|
||||
// first-frame reads. Clicking a preloaded task then shows its chat from cache.
|
||||
|
||||
const RECENT_COUNT = 8;
|
||||
const COMMENTS_PER_TASK = 12;
|
||||
const IDLE_SETTLE_MS = 3_000;
|
||||
@@ -35,7 +32,7 @@ async function ensureRecentTasks(request: APIRequestContext): Promise<RecentTask
|
||||
return tasks;
|
||||
}
|
||||
|
||||
async function seedRecents(page: Page, request: APIRequestContext) {
|
||||
async function seedRecents(page: Page, request: APIRequestContext, tasks = recents) {
|
||||
const session = await (await request.get("/api/auth/get-session")).json().catch(() => null);
|
||||
const userId: string | null = session?.user?.id ?? session?.session?.userId ?? null;
|
||||
await page.addInitScript(({ companyId, userId, recents }) => {
|
||||
@@ -45,7 +42,20 @@ async function seedRecents(page: Page, request: APIRequestContext) {
|
||||
for (const owner of new Set([userId ?? "__local_board__", "__local_board__"])) {
|
||||
localStorage.setItem(`paperclip.recentTasks.v2:${companyId}:${owner}`, JSON.stringify(entries));
|
||||
}
|
||||
}, { companyId: fixture.company.id, userId, recents });
|
||||
}, { companyId: fixture.company.id, userId, recents: tasks });
|
||||
}
|
||||
|
||||
async function addRecent(page: Page, task: RecentTask) {
|
||||
await page.evaluate(({ companyId, task }) => {
|
||||
for (const key of Object.keys(localStorage).filter(key => key.startsWith(`paperclip.recentTasks.v2:${companyId}:`))) {
|
||||
const oldValue = localStorage.getItem(key);
|
||||
const entries = JSON.parse(oldValue ?? "[]").filter((entry: { id: string }) => entry.id !== task.id);
|
||||
const newValue = JSON.stringify([{ ...task, companyId, status: "backlog", recordedAt: Date.now() }, ...entries]);
|
||||
localStorage.setItem(key, newValue);
|
||||
window.dispatchEvent(new StorageEvent("storage", { key, oldValue, newValue, storageArea: localStorage }));
|
||||
}
|
||||
}, { companyId: fixture.company.id, task });
|
||||
await expect(page.getByRole("link", { name: task.title }).first()).toBeVisible();
|
||||
}
|
||||
|
||||
/** Per-task reads that gate the chat's first frame, plus the full detail read. */
|
||||
@@ -91,9 +101,13 @@ async function openRecent(page: Page, task: RecentTask) {
|
||||
state.__recentOpen = {};
|
||||
document.addEventListener("pointerdown", () => { state.__recentOpen!.clickedAt ??= performance.now(); }, { capture: true, once: true });
|
||||
const check = () => {
|
||||
const visible = [...document.querySelectorAll("main *")].some(element =>
|
||||
element.childElementCount === 0 && element.textContent?.trim() === marker && (element as HTMLElement).offsetParent !== null);
|
||||
if (visible) {
|
||||
const thread = document.querySelector('[data-testid="task-chat-thread"] > [aria-busy="false"]');
|
||||
const composer = document.querySelector('[data-testid="task-chat-composer-stack"] [contenteditable="true"]');
|
||||
const visible = thread && [...thread.querySelectorAll("p")].some(element => {
|
||||
const box = element.getBoundingClientRect();
|
||||
return element.textContent?.trim() === marker && box.bottom > 0 && box.top < innerHeight && box.height > 0;
|
||||
});
|
||||
if (visible && composer) {
|
||||
state.__recentOpen!.shownAt = performance.now();
|
||||
return;
|
||||
}
|
||||
@@ -145,11 +159,43 @@ test("clicking a preloaded recent task shows its chat without first-frame reads"
|
||||
expect(reads.filter(kind => FIRST_FRAME_READS.has(kind))).toEqual([]);
|
||||
});
|
||||
|
||||
test("a task added after the initial Recents preload opens without first-frame reads", async ({ page, request }) => {
|
||||
const added = recents.at(-1)!;
|
||||
await seedRecents(page, request, recents.slice(0, -1));
|
||||
await loadIdle(page);
|
||||
const requests = recordRequests(page);
|
||||
await addRecent(page, added);
|
||||
await expect.poll(() => requests.some(url => url.pathname.endsWith("/issues/chat-bootstrap")
|
||||
&& url.searchParams.get("ids")?.split(",").includes(added.id))).toBe(true);
|
||||
await page.waitForLoadState("networkidle");
|
||||
requests.length = 0;
|
||||
let release!: () => void;
|
||||
const held = new Promise<void>(resolve => { release = resolve; });
|
||||
await page.route(/\/api\/issues\//, async route => { await held; await route.continue(); });
|
||||
try {
|
||||
await openRecent(page, added);
|
||||
expect(requests.map(classifyTaskRead).filter(kind => kind && FIRST_FRAME_READS.has(kind))).toEqual([]);
|
||||
const editor = page.getByTestId("task-chat-composer-stack").locator('[contenteditable="true"]').first();
|
||||
await editor.fill("Keep this draft during background refresh");
|
||||
const savedEditor = await editor.elementHandle();
|
||||
const message = page.getByText(added.marker, { exact: true }).first();
|
||||
const before = await message.boundingBox();
|
||||
release();
|
||||
await page.waitForLoadState("networkidle");
|
||||
await expect(editor).toContainText("Keep this draft during background refresh");
|
||||
expect(await savedEditor!.evaluate(element => element.isConnected)).toBe(true);
|
||||
const after = await message.boundingBox();
|
||||
expect(Math.abs(after!.y - before!.y)).toBeLessThanOrEqual(2);
|
||||
await page.screenshot({ path: test.info().outputPath("added-recent-loaded.png") });
|
||||
} finally { release(); }
|
||||
});
|
||||
|
||||
// Native loopback, then a diagnostic scenario that adds latency to every API
|
||||
// response, as a slow hosted database would. Timings are evidence, not budgets.
|
||||
for (const apiDelayMs of [0, 150]) {
|
||||
test(`timing: click on a recent task to first chat frame (+${apiDelayMs} ms API latency)`, async ({ browser, request, baseURL }) => {
|
||||
const runs = Math.max(5, Number(process.env.PAPERCLIP_COLLECTION_PERF_TIMING_RUNS ?? 5));
|
||||
for (const scenario of ["initial", "added"] as const) for (const apiDelayMs of [0, 150]) {
|
||||
test(`timing: ${scenario} recent task to first chat frame (+${apiDelayMs} ms API latency)`, async ({ browser, request, baseURL }) => {
|
||||
const profile = process.env.PAPERCLIP_COLLECTION_PERF_PROFILE === "1";
|
||||
const runs = profile ? 1 : Math.max(5, Number(process.env.PAPERCLIP_COLLECTION_PERF_TIMING_RUNS ?? 5));
|
||||
const timings: number[] = [];
|
||||
const idleCounts: number[] = [];
|
||||
const afterClick: string[][] = [];
|
||||
@@ -165,19 +211,43 @@ for (const apiDelayMs of [0, 150]) {
|
||||
await route.fallback();
|
||||
});
|
||||
}
|
||||
await seedRecents(page, request);
|
||||
await seedRecents(page, request, scenario === "added" ? recents.slice(0, -1) : recents);
|
||||
const requests = recordRequests(page);
|
||||
await loadIdle(page);
|
||||
if (scenario === "added") {
|
||||
await addRecent(page, recents.at(-1)!);
|
||||
await page.waitForLoadState("networkidle");
|
||||
await page.waitForTimeout(IDLE_SETTLE_MS);
|
||||
}
|
||||
idleCounts.push(requests.length);
|
||||
timings.push(await openRecent(page, recents[(run % (RECENT_COUNT - 1)) + 1]!));
|
||||
const task = scenario === "added" ? recents.at(-1)! : recents[(run % (RECENT_COUNT - 1)) + 1]!;
|
||||
const cdp = await context.newCDPSession(page);
|
||||
if (profile) {
|
||||
await cdp.send("Profiler.enable");
|
||||
await cdp.send("Profiler.start");
|
||||
}
|
||||
timings.push(await openRecent(page, task));
|
||||
if (profile) {
|
||||
const result = await cdp.send("Profiler.stop");
|
||||
const file = test.info().outputPath(`cpu-${run}.cpuprofile`);
|
||||
await fs.writeFile(file, JSON.stringify(result.profile));
|
||||
await test.info().attach(`cpu-${run}`, { path: file, contentType: "application/json" });
|
||||
}
|
||||
const waterfall = await page.evaluate(() => performance.getEntriesByType("resource").filter(entry => entry.name.includes("/api/"))
|
||||
.map(entry => ({ ...entry.toJSON(), serverTiming: (entry as PerformanceResourceTiming).serverTiming.map(timing => timing.toJSON()) })));
|
||||
const file = test.info().outputPath(`waterfall-${run}.json`);
|
||||
await fs.writeFile(file, JSON.stringify(waterfall, null, 2));
|
||||
await test.info().attach(`waterfall-${run}`, { path: file, contentType: "application/json" });
|
||||
afterClick.push(requests.slice(idleCounts.at(-1)).map(classifyTaskRead).filter((kind): kind is string => !!kind));
|
||||
await context.close();
|
||||
}
|
||||
const summary = {
|
||||
apiDelayMs, runs, clickToFirstFrameMs: timings.map(Math.round), medianMs: Math.round(median(timings)),
|
||||
scenario, apiDelayMs, runs, profile, clickToFirstFrameMs: timings.map(Math.round), medianMs: Math.round(median(timings)),
|
||||
idleApiRequests: idleCounts, medianIdleApiRequests: median(idleCounts), taskReadsAfterClick: afterClick,
|
||||
};
|
||||
console.log(`recent-task-open timing ${JSON.stringify(summary)}`);
|
||||
await test.info().attach("recent-task-open-timing", { body: JSON.stringify(summary, null, 2), contentType: "application/json" });
|
||||
const file = test.info().outputPath("timings.json");
|
||||
await fs.writeFile(file, JSON.stringify(summary, null, 2));
|
||||
await test.info().attach("recent-task-open-timing", { path: file, contentType: "application/json" });
|
||||
});
|
||||
}
|
||||
@@ -78,6 +78,18 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("useRecentTaskOpenWarmup", () => {
|
||||
it("warms tasks added to Recents after the initial preload", async () => {
|
||||
render({ tasks: tasks(2) });
|
||||
await settle();
|
||||
expect(chatBootstrap).toHaveBeenCalledTimes(1);
|
||||
|
||||
render({ tasks: [tasks(3)[2]!, ...tasks(2)] });
|
||||
await settle();
|
||||
expect(chatBootstrap).toHaveBeenCalledTimes(2);
|
||||
expect(chatBootstrap.mock.calls[1]![1]).toEqual(["id-2"]);
|
||||
expect(queryClient.getQueryData([...queryKeys.issues.detail("PAP-2"), "core"])).toMatchObject({ id: "id-2" });
|
||||
});
|
||||
|
||||
it("reads the ten most recent tasks in one request once the page settles, and seeds them", async () => {
|
||||
render({ tasks: tasks(12) });
|
||||
expect(chatBootstrap).not.toHaveBeenCalled();
|
||||
@@ -86,12 +98,45 @@ describe("useRecentTaskOpenWarmup", () => {
|
||||
expect(chatBootstrap.mock.calls[0]![0]).toBe("company-1");
|
||||
expect(chatBootstrap.mock.calls[0]![1]).toEqual(tasks(10).map((task) => task.id));
|
||||
expect(queryClient.getQueryData([...queryKeys.issues.detail("PAP-3"), "core"])).toMatchObject({ id: "id-3" });
|
||||
// Reordering or re-rendering Recents never starts a second request.
|
||||
render({ tasks: [...tasks(12)].reverse() });
|
||||
render({ tasks: [...tasks(10)].reverse() });
|
||||
await settle();
|
||||
expect(chatBootstrap).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("warms only tasks that move into the ten top rows", async () => {
|
||||
render({ tasks: tasks(12) });
|
||||
await settle();
|
||||
render({ tasks: [...tasks(12)].reverse() });
|
||||
await settle();
|
||||
expect(chatBootstrap).toHaveBeenCalledTimes(2);
|
||||
expect(chatBootstrap.mock.calls[1]![1]).toEqual(["id-11", "id-10"]);
|
||||
});
|
||||
|
||||
it("does not consume a preload when rows change before the page settles", async () => {
|
||||
let finish!: (value: string) => void;
|
||||
void queryClient.fetchQuery({ queryKey: ["page-read"], queryFn: () => new Promise<string>((resolve) => { finish = resolve; }) });
|
||||
render({ tasks: tasks(2) });
|
||||
render({ tasks: [tasks(3)[2]!, ...tasks(2)] });
|
||||
await settle();
|
||||
expect(chatBootstrap).not.toHaveBeenCalled();
|
||||
await act(async () => { finish("done"); });
|
||||
await settle();
|
||||
expect(chatBootstrap).toHaveBeenCalledTimes(1);
|
||||
expect(chatBootstrap.mock.calls[0]![1]).toEqual(["id-2", "id-0", "id-1"]);
|
||||
});
|
||||
|
||||
it("allows a failed preload to retry on the next visit", async () => {
|
||||
chatBootstrap.mockRejectedValueOnce(new Error("offline"));
|
||||
render({ tasks: tasks(1) });
|
||||
await settle();
|
||||
act(() => root.unmount());
|
||||
root = createRoot(container);
|
||||
render({ tasks: tasks(1) });
|
||||
await settle();
|
||||
expect(chatBootstrap).toHaveBeenCalledTimes(2);
|
||||
expect(queryClient.getQueryData([...queryKeys.issues.detail("PAP-0"), "core"])).toMatchObject({ id: "id-0" });
|
||||
});
|
||||
|
||||
it("warms the task-detail code only when Recents has a task", async () => {
|
||||
const painted = vi.fn();
|
||||
window.addEventListener(TASK_COLLECTION_PAINTED_EVENT, painted);
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useQueryClient, type QueryClient } 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 };
|
||||
|
||||
/** Accounts (company and user) whose Recents were preloaded during this page load. */
|
||||
const preloadedScopes = new Set<string>();
|
||||
let preloadedScopes = new WeakMap<QueryClient, Map<string, Set<string>>>();
|
||||
|
||||
export function resetRecentTaskChatPreloadForTests() {
|
||||
preloadedScopes.clear();
|
||||
preloadedScopes = new WeakMap();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -20,9 +20,6 @@ export function resetRecentTaskChatPreloadForTests() {
|
||||
* 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.
|
||||
* At most one request per account per page load; none for an empty list, a
|
||||
* hidden tab or tasks whose first frame is already cached and fresh. Live
|
||||
* updates keep seeded entries fresh afterwards, one task at a time.
|
||||
*/
|
||||
export function useRecentTaskOpenWarmup({
|
||||
companyId,
|
||||
@@ -36,24 +33,34 @@ export function useRecentTaskOpenWarmup({
|
||||
const queryClient = useQueryClient();
|
||||
const hasTasks = tasks.length > 0;
|
||||
useWarmTaskDetailCode(hasTasks);
|
||||
const topTasksKey = JSON.stringify(tasks.slice(0, ISSUE_CHAT_BOOTSTRAP_MAX_IDS).map(task => [task.id, task.identifier]));
|
||||
// Read when the preload starts. A reorder must not cancel a request in flight.
|
||||
const latestTasks = useRef(tasks);
|
||||
latestTasks.current = tasks;
|
||||
useEffect(() => {
|
||||
if (!companyId || !hasTasks || typeof window.requestAnimationFrame !== "function") return;
|
||||
const scope = `${companyId}:${userId ?? "__local_board__"}`;
|
||||
if (preloadedScopes.has(scope)) return;
|
||||
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, () => {
|
||||
if (preloadedScopes.has(scope)) return;
|
||||
preloadedScopes.add(scope);
|
||||
// Skips tasks already cached or loading (a task list may have preloaded them).
|
||||
const top = latestTasks.current.slice(0, ISSUE_CHAT_BOOTSTRAP_MAX_IDS);
|
||||
void loadIssueChatBootstraps(queryClient, companyId, top, { signal: controller.signal });
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
return () => {
|
||||
cancel();
|
||||
controller.abort();
|
||||
};
|
||||
}, [companyId, hasTasks, queryClient, userId]);
|
||||
}, [companyId, hasTasks, queryClient, userId, topTasksKey]);
|
||||
}
|
||||
Reference in new issue
Block a user