mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-16 20:12:20 +01:00
perf(ui): keep the live socket when a cached session is verified
A session restored from the saved query cache has no session id. When the first session read answered with one for the same user, the live-updates socket key changed, the socket reconnected, and that second open refreshed every active query, session and health included: the duplicate wave of reads about 1.5-2.5 s after a returning hard load. The socket key now adopts the verified session id for the same user without reconnecting. A different user, or a different session id once one is known, still reconnects. The initial catch-up read for events between the first reads and the subscription is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e01fe7f84a
commit
b7c41490cc
5 files changed
+251
-1
No files matched your search
@@ -61,6 +61,18 @@ navigation start to the first Mine row (two frames after mount) over
|
||||
a native scenario without request interception and a separate diagnostic scenario that delays
|
||||
workspace reads by 3 seconds. The timings are attached evidence, not budgets.
|
||||
|
||||
`shell-read-waves.spec.ts` hard loads Mine with every API response delayed
|
||||
300 ms and counts the shell and inbox reads that used to repeat about 1.5–2.5 s
|
||||
after load (session, health, company list, agents, inbox dismissals, user
|
||||
directory, approvals, execution workspaces). With the saved query cache
|
||||
(`paperclip:query-cache:v1:<user>:<company>`, primed by one earlier visit in
|
||||
the same browser context) each read happens at most once and the live socket
|
||||
opens once. Without it, the socket's catch-up read may repeat reads that
|
||||
answered before the subscription, but never the session or health read. The
|
||||
fixture (`returning-visit-fixture.ts`) is a company with 24 Mine tasks and
|
||||
three pending board approvals, retained in
|
||||
`test-results/collections/mine-returning-visit-fixture.json`.
|
||||
|
||||
`recent-task-open.spec.ts` covers opening a task from the sidebar's Recents.
|
||||
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
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import fs from "node:fs/promises";
|
||||
import { expect, type APIRequestContext, type Browser, type BrowserContext, type Page } from "@playwright/test";
|
||||
|
||||
// A company with Mine tasks and pending board approvals, for hard loads of
|
||||
// Mine with and without the saved query cache
|
||||
// (`paperclip:query-cache:v1:<user>:<company>`). Retained in
|
||||
// test-results/collections with the same 404 rule as the other fixtures.
|
||||
export type ReturningVisitFixture = {
|
||||
company: { id: string; issuePrefix: string };
|
||||
tasks: Array<{ id: string; identifier: string }>;
|
||||
approvalIds: string[];
|
||||
};
|
||||
const fixturePath = "test-results/collections/mine-returning-visit-fixture.json";
|
||||
const TASK_COUNT = 24;
|
||||
const APPROVAL_COUNT = 3;
|
||||
|
||||
export async function returningVisitFixture(request: APIRequestContext): Promise<ReturningVisitFixture> {
|
||||
const saved = await fs.readFile(fixturePath, "utf8").then(text => JSON.parse(text) as ReturningVisitFixture).catch(error => {
|
||||
if (error.code !== "ENOENT") throw error;
|
||||
return null;
|
||||
});
|
||||
if (saved) {
|
||||
const existing = await request.get(`/api/companies/${saved.company.id}`);
|
||||
if (existing.ok()) return saved;
|
||||
expect(existing.status(), await existing.text()).toBe(404);
|
||||
}
|
||||
const created = await request.post("/api/companies", { data: { name: "Mine returning visit fixture" } });
|
||||
expect(created.ok(), await created.text()).toBeTruthy();
|
||||
const company = await created.json() as ReturningVisitFixture["company"];
|
||||
const tasks: ReturningVisitFixture["tasks"] = [];
|
||||
for (let index = 0; index < TASK_COUNT; index++) {
|
||||
const response = await request.post(`/api/companies/${company.id}/issues`, { data: { title: `Returning visit task ${index}`, status: "todo" } });
|
||||
expect(response.ok(), await response.text()).toBeTruthy();
|
||||
const task = await response.json();
|
||||
tasks.push({ id: task.id, identifier: task.identifier });
|
||||
}
|
||||
const approvalIds: string[] = [];
|
||||
for (let index = 0; index < APPROVAL_COUNT; index++) {
|
||||
const response = await request.post(`/api/companies/${company.id}/approvals`, { data: {
|
||||
type: "request_board_approval", payload: { title: `Returning visit approval ${index}`, summary: "Synthetic decision context." },
|
||||
} });
|
||||
expect(response.ok(), await response.text()).toBeTruthy();
|
||||
approvalIds.push((await response.json()).id);
|
||||
}
|
||||
const fixture = { company, tasks, approvalIds };
|
||||
await fs.mkdir("test-results/collections", { recursive: true });
|
||||
await fs.writeFile(fixturePath, JSON.stringify(fixture));
|
||||
return fixture;
|
||||
}
|
||||
|
||||
export const minePath = (fixture: ReturningVisitFixture) => `/${fixture.company.issuePrefix}/issues?view=mine`;
|
||||
export const firstMineRow = (page: Page) => page.locator("[data-inbox-issue-link]").first();
|
||||
export type StorageState = Awaited<ReturnType<BrowserContext["storageState"]>>;
|
||||
export const freshContext = (browser: Browser, baseURL: string | undefined, storageState?: StorageState) =>
|
||||
browser.newContext({ baseURL, serviceWorkers: "block", viewport: { width: 1440, height: 900 }, storageState });
|
||||
|
||||
/** Visits Mine once and returns the browser storage that visit saved, including the query cache. */
|
||||
export async function savedQueryCache(browser: Browser, baseURL: string | undefined, fixture: ReturningVisitFixture): Promise<StorageState> {
|
||||
const context = await freshContext(browser, baseURL);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
await page.goto(minePath(fixture));
|
||||
await expect(firstMineRow(page)).toBeVisible({ timeout: 30_000 });
|
||||
await page.waitForFunction((companyId) => Object.keys(localStorage).some(key => key.startsWith("paperclip:query-cache:v1:")
|
||||
&& key.endsWith(`:${encodeURIComponent(companyId)}`) && (localStorage.getItem(key) ?? "").includes("mine-context")),
|
||||
fixture.company.id, { timeout: 15_000 });
|
||||
// Let the debounced writer settle after the last background read.
|
||||
await page.waitForTimeout(1_500);
|
||||
await page.evaluate(() => window.dispatchEvent(new Event("pagehide")));
|
||||
return await context.storageState();
|
||||
} finally { await context.close(); }
|
||||
}
|
||||
|
||||
/** Shell and inbox reads that a hard load of Mine used to repeat about 1.5–2.5 s after load. */
|
||||
export const SHELL_READS: ReadonlyArray<readonly [string, (url: URL) => boolean]> = [
|
||||
["auth/get-session", url => url.pathname === "/api/auth/get-session"],
|
||||
["health", url => url.pathname === "/api/health"],
|
||||
["companies?scope=accessible", url => url.pathname === "/api/companies" && url.searchParams.get("scope") === "accessible"],
|
||||
["agents", url => /^\/api\/companies\/[^/]+\/agents$/.test(url.pathname)],
|
||||
["inbox-dismissals", url => /^\/api\/companies\/[^/]+\/inbox-dismissals$/.test(url.pathname)],
|
||||
["user-directory", url => /^\/api\/companies\/[^/]+\/user-directory$/.test(url.pathname)],
|
||||
["approvals", url => /^\/api\/companies\/[^/]+\/approvals$/.test(url.pathname)],
|
||||
["execution-workspaces", url => /^\/api\/companies\/[^/]+\/execution-workspaces$/.test(url.pathname)],
|
||||
];
|
||||
|
||||
export type ApiRead = { path: string; startedAt: number; finishedAt: number | null };
|
||||
|
||||
/**
|
||||
* Delays every API response by `delayMs`, records each read, and marks the
|
||||
* first Mine row (two frames after it mounts) in page time.
|
||||
*/
|
||||
export async function observeMineLoad(page: Page, delayMs: number) {
|
||||
const reads: ApiRead[] = [];
|
||||
await page.route(url => url.pathname.startsWith("/api/"), async route => {
|
||||
const url = new URL(route.request().url());
|
||||
reads.push({ path: `${url.pathname}${url.search}`, startedAt: Date.now(), finishedAt: null });
|
||||
if (delayMs > 0) await new Promise(resolve => setTimeout(resolve, delayMs));
|
||||
// A closed page aborts outstanding routes; that read never finished.
|
||||
await route.continue().catch(() => {});
|
||||
});
|
||||
page.on("requestfinished", request => {
|
||||
const url = new URL(request.url());
|
||||
const read = reads.find(entry => entry.finishedAt === null && entry.path === `${url.pathname}${url.search}`);
|
||||
if (read) read.finishedAt = Date.now();
|
||||
});
|
||||
await page.addInitScript(() => {
|
||||
const state = window as unknown as { __mineRowsAt: number | null };
|
||||
state.__mineRowsAt = null;
|
||||
const observer = new MutationObserver(() => {
|
||||
if (!document.querySelector("[data-inbox-issue-link]")) return;
|
||||
observer.disconnect();
|
||||
requestAnimationFrame(() => requestAnimationFrame(() => { state.__mineRowsAt = performance.timeOrigin + performance.now(); }));
|
||||
});
|
||||
observer.observe(document, { subtree: true, childList: true });
|
||||
});
|
||||
const rowsAt = async () => {
|
||||
await page.waitForFunction(() => (window as unknown as { __mineRowsAt: number | null }).__mineRowsAt !== null, null, { timeout: 30_000 });
|
||||
return await page.evaluate(() => (window as unknown as { __mineRowsAt: number }).__mineRowsAt);
|
||||
};
|
||||
const shellReadCounts = () => Object.fromEntries(SHELL_READS.map(([name, matches]) => [name,
|
||||
reads.filter(read => matches(new URL(read.path, "http://local"))).length])) as Record<string, number>;
|
||||
return { reads, rowsAt, shellReadCounts };
|
||||
}
|
||||
|
||||
export const median = (values: number[]) => {
|
||||
const sorted = [...values].sort((a, b) => a - b);
|
||||
const middle = Math.floor(sorted.length / 2);
|
||||
return sorted.length % 2 ? sorted[middle]! : (sorted[middle - 1]! + sorted[middle]!) / 2;
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { freshContext, minePath, observeMineLoad, returningVisitFixture, savedQueryCache, type ReturningVisitFixture } from "./returning-visit-fixture";
|
||||
|
||||
// A hard load of Mine used to repeat about eight shell and inbox reads 1.5–2.5 s
|
||||
// after load. With the saved query cache, the restored session has no session
|
||||
// id; when the server answered with one, the live-updates socket reconnected,
|
||||
// and that reconnect refreshed every active query, session and health included.
|
||||
// Every API response is delayed 300 ms so the socket opens while reads are in
|
||||
// flight, as on a busy hosted instance.
|
||||
let fixture: ReturningVisitFixture;
|
||||
test.beforeAll(async ({ request }) => {
|
||||
test.setTimeout(120_000);
|
||||
fixture = await returningVisitFixture(request);
|
||||
});
|
||||
|
||||
test("a returning visit makes each shell read at most once and keeps one live socket", async ({ browser, baseURL }) => {
|
||||
const storageState = await savedQueryCache(browser, baseURL, fixture);
|
||||
const context = await freshContext(browser, baseURL, storageState);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
const sockets: string[] = [];
|
||||
page.on("websocket", socket => { if (socket.url().includes("/events/ws")) sockets.push(socket.url()); });
|
||||
const { rowsAt, shellReadCounts } = await observeMineLoad(page, 300);
|
||||
await page.goto(minePath(fixture));
|
||||
await rowsAt();
|
||||
await page.waitForTimeout(5_000);
|
||||
const counts = shellReadCounts();
|
||||
expect(Object.entries(counts).filter(([, count]) => count > 1), JSON.stringify(counts)).toEqual([]);
|
||||
expect(sockets, "The verified session reconnected the live socket").toHaveLength(1);
|
||||
} finally { await context.close(); }
|
||||
});
|
||||
|
||||
// Without a saved cache the socket can open only after the company list has
|
||||
// answered, so the catch-up read that covers events between a read and the
|
||||
// subscription may repeat reads that answered first. It never repeats the
|
||||
// session or health read, and the socket is opened once.
|
||||
test("a first visit never repeats the session or health read", async ({ browser, baseURL }) => {
|
||||
const context = await freshContext(browser, baseURL);
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
const sockets: string[] = [];
|
||||
page.on("websocket", socket => { if (socket.url().includes("/events/ws")) sockets.push(socket.url()); });
|
||||
const { rowsAt, shellReadCounts } = await observeMineLoad(page, 300);
|
||||
await page.goto(minePath(fixture));
|
||||
await rowsAt();
|
||||
await page.waitForTimeout(5_000);
|
||||
const counts = shellReadCounts();
|
||||
expect({ session: counts["auth/get-session"], health: counts.health }, JSON.stringify(counts)).toEqual({ session: 1, health: 1 });
|
||||
expect(Object.entries(counts).filter(([, count]) => count > 2), JSON.stringify(counts)).toEqual([]);
|
||||
expect(sockets).toHaveLength(1);
|
||||
} finally { await context.close(); }
|
||||
});
|
||||
@@ -278,6 +278,40 @@ describe("LiveUpdatesProvider connection recovery", () => {
|
||||
expect(pollRead).toHaveBeenCalledTimes(afterOpen);
|
||||
});
|
||||
|
||||
// A session restored from the saved query cache has no session id. Its first
|
||||
// verified read is the same sign-in, so the socket stays open and nothing is
|
||||
// refetched. Before, the new id reconnected the socket, and that open
|
||||
// refreshed every active query, session and health included.
|
||||
it("keeps the socket when the restored session is verified for the same user", async () => {
|
||||
vi.stubGlobal("WebSocket", Socket);
|
||||
client.setQueryData(queryKeys.auth.session, { user: { id: "viewer" }, session: { userId: "viewer" } });
|
||||
await renderPolling();
|
||||
expect(Socket.instances).toHaveLength(1);
|
||||
await act(async () => Socket.instances[0].onopen?.());
|
||||
expect(container.textContent).toBe("connected");
|
||||
const invalidate = vi.spyOn(client, "invalidateQueries");
|
||||
await act(async () => client.setQueryData(queryKeys.auth.session, { user: { id: "viewer" }, session: { id: "session", userId: "viewer" } }));
|
||||
await act(async () => vi.advanceTimersByTimeAsync(1));
|
||||
expect(Socket.instances).toHaveLength(1);
|
||||
expect(container.textContent).toBe("connected");
|
||||
expect(invalidate).not.toHaveBeenCalled();
|
||||
// A later replacement session still needs a new connection.
|
||||
await act(async () => client.setQueryData(queryKeys.auth.session, { user: { id: "viewer" }, session: { id: "replacement", userId: "viewer" } }));
|
||||
await act(async () => vi.advanceTimersByTimeAsync(1));
|
||||
expect(Socket.instances).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("reconnects when the verified session belongs to another user", async () => {
|
||||
vi.stubGlobal("WebSocket", Socket);
|
||||
client.setQueryData(queryKeys.auth.session, { user: { id: "viewer" }, session: { userId: "viewer" } });
|
||||
await renderPolling();
|
||||
await act(async () => Socket.instances[0].onopen?.());
|
||||
await act(async () => client.setQueryData(queryKeys.auth.session, { user: { id: "other" }, session: { id: "other-session", userId: "other" } }));
|
||||
await act(async () => vi.advanceTimersByTimeAsync(1));
|
||||
expect(Socket.instances).toHaveLength(2);
|
||||
expect(container.textContent).toBe("fallback");
|
||||
});
|
||||
|
||||
it.each(["DEMO-1", "issue-1"])("discovers and finishes runs with cache key %s on an unfocused non-task route", async queryRef => {
|
||||
vi.stubGlobal("WebSocket", Socket);
|
||||
vi.spyOn(document, "hasFocus").mockReturnValue(false);
|
||||
|
||||
@@ -2214,6 +2214,7 @@ export const __liveUpdatesTestUtils = {
|
||||
readRunLiveStatusPatchFromPayload,
|
||||
resolveLiveCompanyId,
|
||||
canUseLiveSession,
|
||||
nextSocketAuthScope,
|
||||
shouldDeferIssueRefetchForVisibleAgentActivity,
|
||||
shouldDeferVisibleIssueCommentActivity,
|
||||
shouldSuppressActivityToastForVisibleIssue,
|
||||
@@ -2221,6 +2222,26 @@ export const __liveUpdatesTestUtils = {
|
||||
shouldSuppressAgentStatusToastForVisibleIssue,
|
||||
};
|
||||
|
||||
type SocketAuthScope = { userId: string | null; sessionId: string | null; key: string };
|
||||
|
||||
/**
|
||||
* The socket's sign-in scope. A new key closes the socket, opens another and
|
||||
* refreshes every active query, because events are not replayed.
|
||||
*
|
||||
* A session restored from the saved query cache carries the user but no
|
||||
* session id. Its first verified read adds the id for the same user. That is
|
||||
* the same sign-in, so the key stays and the open socket is kept. A
|
||||
* different user, or a different session id once one is known, still
|
||||
* reconnects.
|
||||
*/
|
||||
function nextSocketAuthScope(previous: SocketAuthScope | null, userId: string | null, sessionId: string | null): SocketAuthScope {
|
||||
if (previous && userId !== null && previous.userId === userId) {
|
||||
if (previous.sessionId === sessionId) return previous;
|
||||
if (previous.sessionId === null) return { ...previous, sessionId };
|
||||
}
|
||||
return { userId, sessionId, key: sessionId ?? userId ?? "signed_out" };
|
||||
}
|
||||
|
||||
function canUseLiveSession(sessionStatus: string, hasSession: boolean, deploymentMode?: string) {
|
||||
return sessionStatus === "success" && (hasSession || deploymentMode === "local_trusted");
|
||||
}
|
||||
@@ -2248,7 +2269,9 @@ export function LiveUpdatesProvider({ children }: { children: ReactNode }) {
|
||||
const { data: health } = useQuery({ queryKey: queryKeys.health, queryFn: healthApi.get });
|
||||
const currentUserId = session?.user?.id ?? session?.session?.userId ??
|
||||
(health?.deploymentMode === "local_trusted" ? "local-board" : null);
|
||||
const socketAuthKey = session?.session?.id ?? currentUserId ?? "signed_out";
|
||||
const socketAuth = useRef<SocketAuthScope | null>(null);
|
||||
socketAuth.current = nextSocketAuthScope(socketAuth.current, currentUserId, session?.session?.id ?? null);
|
||||
const socketAuthKey = socketAuth.current.key;
|
||||
const [connectedScopeKey, setConnectedScopeKey] = useState<string | null>(null);
|
||||
const liveCompanyId = resolveLiveCompanyId(selectedCompanyId, selectedCompany?.id ?? null);
|
||||
const socketScopeKey = JSON.stringify([liveCompanyId, socketAuthKey]);
|
||||
|
||||
Reference in new issue
Block a user