Skip to content

Commit 0e8eb54

Browse files
tanzhongyanclaude
andauthored
perf: cut background function invocations, stop MCP listen streams, fix OG image gate (#596)
* perf: cut background function invocations and fix OG image gate Vercel usage shows /api/auth/[...nextauth] (36K/day) and /api/assistant/status (18K/day) among the top routes, driven by requests the browser makes without any user action: - SessionProvider re-fetched /api/auth/session on every tab focus. The session is a JWT checked server-side on every request, so turn focus refetching off; sign-in and sign-out still update it. - React Query refetched every stale query on window focus, adding /api/trpc batches. Default it off; mutations already invalidate what they change. userBids.listMine opts back in because the assistant and MCP agents can change bids from another tab. - AssistantProvider fetched /api/assistant/status on every route change. Fetch on auth status changes instead (this still covers the client-side login it was written for); the open-refetch re-asks before each use. The course and professor OG images are cached publicly by the CDN for a week (OG_IMAGE_HEADERS), but gated on the viewer's session. With a domain set private, an image drawn for a signed-in viewer was then served to anonymous viewers. Gate on isDataPublic alone, since the cache key carries no cookies. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M83Ub5YoyGHcFYWpB8nscP * fix(assistant): ignore stale status responses after an auth change The status fetch now runs on auth status changes only. A signed-out request still in flight at login could resolve after the signed-in one and overwrite it, and no later route change would repair it. Drop responses from an effect run that has been superseded. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M83Ub5YoyGHcFYWpB8nscP * perf: stop prefetching course and professor links in long lists Production logs show /professor/[slug] and /course/[code] requests marked Prefetch: Yes with an _rsc param and the home page as referer, two per link, as review cards scroll into view. Together the two routes were 62K of 172K daily invocations. Turn prefetching off on the review card course and professor links and on search result links. The routes' loading.tsx already covers the wait on click, and a prefetch of a dynamic route only fetched that loading state anyway. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M83Ub5YoyGHcFYWpB8nscP * fix(assistant): let only the latest status request write The open-refetch response was unguarded, so a signed-out response still in flight at login could overwrite the signed-in status from the auth change refetch. Share one latest-request counter between both effects. Also scope the list-link prefetch invariant to every ProgressLink in each file instead of any occurrence of the text. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M83Ub5YoyGHcFYWpB8nscP * fix(mcp): stop subscriptions/listen streams holding functions open Vercel logs show 2026-07-28 clients (claude-code 2.1.30x) opening a subscriptions/listen stream that runs until the 300s maxDuration kill, then reconnecting: 40-64% of /api/mcp invocations ended that way. mcp-use advertises listChanged: true unconditionally, the TypeScript client auto-opens listen when it sees that flag, and the SDK keeps the SSE stream alive with keep-alives. The in-process event bus cannot reach other instances and the catalog is static, so those streams never delivered a notification. Follow the Python SDK's opt-out (modelcontextprotocol/python-sdk#3626) in the route's POST wrapper: - server/discover reports listChanged and subscribe as false, so the client skips auto-opening a listen stream. - A subscriptions/listen that still arrives is answered at once with JSON-RPC -32601 over HTTP 404, which the client reports and moves past. Every other request passes through unchanged. Tracked upstream in modelcontextprotocol/typescript-sdk#2873. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M83Ub5YoyGHcFYWpB8nscP * test(mcp): make the save-bids parity test pick its window deterministically save-bids reads its term from the current bid window: the first active window ordered by opensAt. The parity seed opened windows 9101 (T1) and 9102 (T2) at the same instant, so the pick fell to Postgres row order. When 9102 came first, every bid resolved against AY202425T2-PARITY and the golden comparison failed. It failed on this PR's CI twice and on an unrelated Renovate PR, and one in three full local integration runs. Open 9102 a day after 9101 so 9101 is always the current window. The golden output does not include window dates, and the 9102 failure case (class not in the window's term) is unchanged. Five full local integration runs pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01M83Ub5YoyGHcFYWpB8nscP --------- Co-authored-by: Claude <[email protected]>
1 parent 0b616c4 commit 0e8eb54

17 files changed

Lines changed: 427 additions & 23 deletions

File tree

‎src/app/(school)/(reviews)/@reviews/course/[code]/opengraph-image.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
toOgStatItems,
1010
} from "@/modules/opengraph/functions/ogFigures";
1111
import { getCoursePageData } from "@/modules/reviews/functions/getCoursePageData";
12-
import { auth } from "@/server/auth";
13-
import { canViewData } from "@/server/ecfg/public-data";
12+
import { isDataPublic } from "@/server/ecfg/public-data";
1413

1514
export const runtime = "nodejs";
1615

@@ -62,7 +61,10 @@ export default async function Image({
6261
const courseCode = code.toUpperCase();
6362

6463
// The figures on the image are course, professor and review data.
65-
if (!(await canViewData(await auth(), "courses", "professors", "reviews"))) {
64+
// The CDN caches this image publicly (OG_IMAGE_HEADERS) without keying on
65+
// cookies, so the gate ignores the session: an image drawn for a signed-in
66+
// viewer would otherwise be served to anonymous viewers.
67+
if (!(await isDataPublic("courses", "professors", "reviews"))) {
6668
return null;
6769
}
6870

‎src/app/(school)/(reviews)/@reviews/professor/[slug]/opengraph-image.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
toOgStatItems,
1010
} from "@/modules/opengraph/functions/ogFigures";
1111
import { getProfessorPageData } from "@/modules/reviews/functions/getProfessorPageData";
12-
import { auth } from "@/server/auth";
13-
import { canViewData } from "@/server/ecfg/public-data";
12+
import { isDataPublic } from "@/server/ecfg/public-data";
1413

1514
export const runtime = "nodejs";
1615

@@ -57,7 +56,10 @@ export default async function Image({
5756
const { slug } = await params;
5857

5958
// The figures on the image are professor, course and review data.
60-
if (!(await canViewData(await auth(), "professors", "courses", "reviews"))) {
59+
// The CDN caches this image publicly (OG_IMAGE_HEADERS) without keying on
60+
// cookies, so the gate ignores the session: an image drawn for a signed-in
61+
// viewer would otherwise be served to anonymous viewers.
62+
if (!(await isDataPublic("professors", "courses", "reviews"))) {
6163
return null;
6264
}
6365

‎src/app/api/mcp/[[...path]]/embedded.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,66 @@ describe("embedded /api/mcp views", () => {
257257
// ~15 RPCs + asset GETs; generous budget for full-suite contention.
258258
}, 120_000);
259259
});
260+
261+
// Wire contract with the real mcp-use server behind the route wrapper (see
262+
// src/mcp/subscriptions.ts): a 2026-07-28 client reads the change flags from
263+
// server/discover to decide whether to open a listen stream, and a listen
264+
// that still arrives must not hold the function open.
265+
describe("embedded /api/mcp subscriptions opt-out", () => {
266+
const modern = (method: string, id: string, params: object = {}) =>
267+
new Request("http://localhost/api/mcp", {
268+
method: "POST",
269+
headers: {
270+
"content-type": "application/json",
271+
accept: "application/json, text/event-stream",
272+
"mcp-method": method,
273+
"mcp-protocol-version": "2026-07-28",
274+
},
275+
body: JSON.stringify({
276+
jsonrpc: "2.0",
277+
id,
278+
method,
279+
params: {
280+
_meta: {
281+
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
282+
"io.modelcontextprotocol/clientInfo": { name: "t", version: "1" },
283+
"io.modelcontextprotocol/clientCapabilities": {},
284+
},
285+
...params,
286+
},
287+
}),
288+
});
289+
290+
it("server/discover advertises no change notifications", async () => {
291+
vi.stubEnv("MCP_DEV_BYPASS", "true");
292+
const route = await import("./route");
293+
const res = await route.POST(modern("server/discover", "d1"));
294+
expect(res.status).toBe(200);
295+
const payload = parseRpcBody(await res.text()) as {
296+
result?: { capabilities?: Record<string, Record<string, boolean>> };
297+
};
298+
const caps = payload.result?.capabilities;
299+
expect(caps?.tools?.listChanged).toBe(false);
300+
expect(caps?.prompts?.listChanged).toBe(false);
301+
expect(caps?.resources?.listChanged).toBe(false);
302+
expect(caps?.resources?.subscribe).toBe(false);
303+
vi.unstubAllEnvs();
304+
});
305+
306+
it("subscriptions/listen is declined at once instead of streaming", async () => {
307+
vi.stubEnv("MCP_DEV_BYPASS", "true");
308+
const route = await import("./route");
309+
const res = await route.POST(
310+
modern("subscriptions/listen", "listen:0", {
311+
notifications: { toolsListChanged: true },
312+
}),
313+
);
314+
expect(res.status).toBe(404);
315+
expect(res.headers.get("content-type")).not.toContain("text/event-stream");
316+
expect(await res.json()).toMatchObject({
317+
id: "listen:0",
318+
error: { code: -32601 },
319+
});
320+
vi.unstubAllEnvs();
321+
});
322+
});

‎src/app/api/mcp/[[...path]]/route.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createNextHandler } from "mcp-use/next";
22

33
import { buildRouteServer } from "@/mcp/route-server";
4+
import { declineSubscriptions } from "@/mcp/subscriptions";
45

56
export const runtime = "nodejs";
67
// 12-round tool chains need the Vercel Pro ceiling (same pin as
@@ -12,6 +13,19 @@ export const maxDuration = 300;
1213
// passes through untouched. The unbound-method disable is a false positive:
1314
// createNextHandler returns closures over `handle`, not prototype methods
1415
// needing `this` (see node_modules/mcp-use/dist/next/index.js).
15-
// eslint-disable-next-line @typescript-eslint/unbound-method -- handlers are this-independent closures, not methods
16-
export const { GET, POST, DELETE, OPTIONS } =
17-
createNextHandler(buildRouteServer());
16+
/* eslint-disable @typescript-eslint/unbound-method -- handlers are this-independent closures, not methods */
17+
const {
18+
GET,
19+
POST: mcpPost,
20+
DELETE,
21+
OPTIONS,
22+
} = createNextHandler(buildRouteServer());
23+
/* eslint-enable @typescript-eslint/unbound-method */
24+
25+
// POST declines `subscriptions/listen` and hides the change-notification
26+
// flags from `server/discover`: a listen stream otherwise holds the function
27+
// until the maxDuration kill (see src/mcp/subscriptions.ts). The wrapper
28+
// forwards every other argument, so the handler contract is unchanged.
29+
const POST = declineSubscriptions(mcpPost);
30+
31+
export { GET, POST, DELETE, OPTIONS };

‎src/common/performance-invariants.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,50 @@ describe("performance invariants", () => {
258258
);
259259
});
260260
});
261+
262+
describe("background function invocations", () => {
263+
it("does not refetch the session on every tab focus", () => {
264+
expect(read("src/common/providers/AuthProvider.tsx")).toContain(
265+
"<SessionProvider refetchOnWindowFocus={false}>",
266+
);
267+
});
268+
269+
it("fetches assistant status on auth changes, not on every route change", () => {
270+
const provider = read("src/modules/assistant/assistant-provider.tsx");
271+
expect(provider).toContain("}, [authStatus]);");
272+
expect(provider).not.toContain("}, [pathname]);");
273+
});
274+
});
275+
276+
describe("list link prefetching", () => {
277+
// Review cards and search results render many course and professor
278+
// links. Prefetching each one as it scrolls into view runs the dynamic
279+
// route's function (twice per link in production logs), while the
280+
// route's loading.tsx already covers the wait on click.
281+
it.each([
282+
"src/modules/reviews/components/ReviewItem/RevieweeGroup/RevieweeGroup.tsx",
283+
"src/modules/reviews/components/ReviewItem/RevieweeGroup/RevieweeCourse.tsx",
284+
"src/modules/search/components/SearchResult/SearchResultItem/SearchResultItem.tsx",
285+
])("%s does not prefetch", (file) => {
286+
// Every link element in the file opts out, not just one of them.
287+
const links = read(file).match(/<ProgressLink\b[\s\S]*?\n\s*>/g) ?? [];
288+
expect(links.length).toBeGreaterThan(0);
289+
for (const link of links) expect(link).toContain("prefetch={false}");
290+
});
291+
});
292+
293+
describe("publicly cached OG images", () => {
294+
// The CDN cache key carries no cookies, so a session-dependent gate
295+
// would serve an image drawn for a member to anonymous viewers.
296+
it.each([
297+
"src/app/(school)/(reviews)/@reviews/course/[code]/opengraph-image.tsx",
298+
"src/app/(school)/(reviews)/@reviews/professor/[slug]/opengraph-image.tsx",
299+
])("%s gates on public data alone", (file) => {
300+
const source = read(file);
301+
expect(source).toContain("OG_IMAGE_HEADERS");
302+
expect(source).toContain("await isDataPublic(");
303+
expect(source).not.toContain("canViewData");
304+
expect(source).not.toContain("@/server/auth");
305+
});
306+
});
261307
});

‎src/common/providers/AuthProvider.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ import { SessionProvider } from "next-auth/react";
44
import { type ReactNode } from "react";
55

66
export default function AuthProvider({ children }: { children: ReactNode }) {
7-
return <SessionProvider>{children}</SessionProvider>;
7+
// The session is a JWT the server checks on every request, so re-fetching
8+
// /api/auth/session on each tab focus only adds function invocations.
9+
// Sign-in and sign-out still update the session, across tabs too.
10+
return (
11+
<SessionProvider refetchOnWindowFocus={false}>{children}</SessionProvider>
12+
);
813
}

‎src/common/tools/trpc/query-client.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { shouldRetryQuery } from "./query-client";
2+
import { createQueryClient, shouldRetryQuery } from "./query-client";
33

44
const trpcError = (code: string) => ({ data: { code } });
55

@@ -23,3 +23,10 @@ describe("shouldRetryQuery", () => {
2323
expect(shouldRetryQuery(0, null)).toBe(true);
2424
});
2525
});
26+
27+
describe("createQueryClient", () => {
28+
it("does not refetch queries on window focus by default", () => {
29+
const queries = createQueryClient().getDefaultOptions().queries;
30+
expect(queries?.refetchOnWindowFocus).toBe(false);
31+
});
32+
});

‎src/common/tools/trpc/query-client.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const createQueryClient = () =>
2727
// With SSR, we usually want to set some default staleTime
2828
// above 0 to avoid refetching immediately on the client
2929
staleTime: 30 * 1000,
30+
// Every focus refetch of a stale query is a /api/trpc function
31+
// invocation on Vercel. Mutations invalidate what they change, so
32+
// queries that must follow outside changes opt back in per query.
33+
refetchOnWindowFocus: false,
3034
retry: shouldRetryQuery,
3135
},
3236
dehydrate: {

‎src/mcp/subscriptions.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { declineSubscriptions } from "./subscriptions";
4+
5+
const rpc = (method: string, id: unknown = 1, headers?: HeadersInit) =>
6+
new Request("http://localhost/api/mcp", {
7+
method: "POST",
8+
headers: { "content-type": "application/json", ...headers },
9+
body: JSON.stringify({ jsonrpc: "2.0", id, method, params: {} }),
10+
});
11+
12+
const capabilities = {
13+
logging: {},
14+
tools: { listChanged: true },
15+
prompts: { listChanged: true },
16+
resources: { listChanged: true, subscribe: true },
17+
};
18+
19+
const discoverResult = (id: unknown) => ({
20+
jsonrpc: "2.0",
21+
id,
22+
result: { supportedVersions: ["2026-07-28"], capabilities },
23+
});
24+
25+
const offCapabilities = {
26+
logging: {},
27+
tools: { listChanged: false },
28+
prompts: { listChanged: false },
29+
resources: { listChanged: false, subscribe: false },
30+
};
31+
32+
describe("declineSubscriptions", () => {
33+
it("answers subscriptions/listen with -32601 over 404 without opening a stream", async () => {
34+
const post = vi.fn();
35+
const res = await declineSubscriptions(post)(
36+
rpc("subscriptions/listen", "listen:0"),
37+
);
38+
expect(post).not.toHaveBeenCalled();
39+
expect(res.status).toBe(404);
40+
expect(res.headers.get("content-type")).toContain("application/json");
41+
expect(await res.json()).toEqual({
42+
jsonrpc: "2.0",
43+
id: "listen:0",
44+
error: {
45+
code: -32601,
46+
message: "Method not found: subscriptions/listen",
47+
},
48+
});
49+
});
50+
51+
it("recognises listen from the Mcp-Method header", async () => {
52+
const post = vi.fn();
53+
const res = await declineSubscriptions(post)(
54+
new Request("http://localhost/api/mcp", {
55+
method: "POST",
56+
headers: { "mcp-method": "subscriptions/listen" },
57+
body: "not json",
58+
}),
59+
);
60+
expect(post).not.toHaveBeenCalled();
61+
expect(res.status).toBe(404);
62+
});
63+
64+
it("turns the change-notification flags off in a JSON discover result", async () => {
65+
const post = vi.fn(async () => Response.json(discoverResult(7)));
66+
const res = await declineSubscriptions(post)(rpc("server/discover", 7));
67+
expect(res.status).toBe(200);
68+
const body = (await res.json()) as {
69+
result: { capabilities: unknown; supportedVersions: string[] };
70+
};
71+
expect(body.result.capabilities).toEqual(offCapabilities);
72+
expect(body.result.supportedVersions).toEqual(["2026-07-28"]);
73+
});
74+
75+
it("turns the flags off in an SSE discover result", async () => {
76+
const post = vi.fn(
77+
async () =>
78+
new Response(
79+
`event: message\ndata: ${JSON.stringify(discoverResult(8))}\n\n`,
80+
{ headers: { "content-type": "text/event-stream" } },
81+
),
82+
);
83+
const res = await declineSubscriptions(post)(rpc("server/discover", 8));
84+
const text = await res.text();
85+
expect(text.startsWith("event: message\ndata: ")).toBe(true);
86+
const frame = text.split("\n").find((line) => line.startsWith("data:"));
87+
const body = JSON.parse(frame!.slice("data:".length)) as {
88+
result: { capabilities: unknown };
89+
};
90+
expect(body.result.capabilities).toEqual(offCapabilities);
91+
});
92+
93+
it("passes every other method through untouched, with its arguments", async () => {
94+
const response = new Response("ok");
95+
const post = vi.fn(async (_request: Request, _context: { n: number }) =>
96+
Promise.resolve(response),
97+
);
98+
const request = rpc("tools/call");
99+
const context = { n: 1 };
100+
const res = await declineSubscriptions(post)(request, context);
101+
expect(res).toBe(response);
102+
expect(post).toHaveBeenCalledWith(request, context);
103+
// The wrapper reads a clone, so the handler can still read the body.
104+
expect(await request.json()).toMatchObject({ method: "tools/call" });
105+
});
106+
107+
it("leaves a failed discover response as it is", async () => {
108+
const response = new Response("nope", { status: 401 });
109+
const post = vi.fn(async () => response);
110+
const res = await declineSubscriptions(post)(rpc("server/discover"));
111+
expect(res).toBe(response);
112+
});
113+
});

0 commit comments

Comments
 (0)