@devries-cameron20 / t3code Mirror · read-only / commits / 2dfd6bf0ea
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 17:00 UTC

chore(web): measure renders before/after with render scenarios (#24980)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 25, 2026 at 15:26 UTC 2dfd6bf0ea04a57fd12dbe4fb23b9fbdb02aa4cb
8 files changed +1361 -1
apps/web/test/appScenarios.renders.tsx new
+282
@@ -0,0 +1,282 @@
1 +// Render scenarios that mount the whole app (AppRoot with the app's router) on a thread,
2 +// against the in-process environment in fakeEnvironment.ts. Counts cover ChatView, the
3 +// chat and settings routes, the sidebar and the right panel, through the same stores and
4 +// atoms the app reads. See renderScenarios.renders.tsx for the counting rules.
5 +import { createMemoryHistory } from "@tanstack/react-router";
6 +import type { ThreadId } from "@t3tools/contracts";
7 +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
8 +import type * as Context from "effect/Context";
9 +import { expect, it, vi } from "vite-plus/test";
10 +import { commands, page, userEvent } from "vite-plus/test/browser";
11 +
12 +import "~/index.css";
13 +
14 +import { AppRoot } from "~/AppRoot";
15 +import { __setClientSettingsForTests } from "~/hooks/useSettings";
16 +import { getRouter, type AppRouter } from "~/router";
17 +import {
18 + ENVIRONMENT_ID,
19 + assistantItem,
20 + emitThreadEvent,
21 + installFakeEnvironment,
22 + isThreadSubscribed,
23 + runningRun,
24 + threadWithHistory,
25 + userItem,
26 + type FakeDriver,
27 +} from "./fakeEnvironment";
28 +import { commitsSoFar, startCounting, stopCounting, type RenderReport } from "./renderCounter";
29 +import { act, fakeTimers, render } from "./render";
30 +
31 +// The socket is the only fake layer: client-runtime's driver hands every connection the
32 +// in-process session. A string path, since a typed import pulls client-runtime's sources
33 +// into this project, and imports inside, since the factory runs before this file's own.
34 +vi.mock("../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
35 + const [actual, { fakeDriver }, Layer] = await Promise.all([
36 + importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
37 + import("./fakeEnvironment"),
38 + import("effect/Layer"),
39 + ]);
40 + return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
41 +});
42 +
43 +// The fake server leaves most requests unanswered, which would raise the slow-request toast
44 +// 15s of wall clock after each one, at a different point in every run.
45 +vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
46 + ...(await original()),
47 + trackRpcRequestSent: () => {},
48 +}));
49 +
50 +const HISTORY_TURNS = 20;
51 +const STREAM_DELTAS = 30;
52 +const TYPED_TEXT = "fix the flaky tests!";
53 +// The stream scenario leaves a turn on its thread, so it streams into one no other scenario opens.
54 +const THREADS = ["thread-warm", "thread-a", "thread-b", "thread-live"].map((name) =>
55 + threadWithHistory(name, HISTORY_TURNS),
56 +);
57 +const [WARM, A, B, LIVE] = THREADS.map((thread) => thread.id) as [
58 + ThreadId,
59 + ThreadId,
60 + ThreadId,
61 + ThreadId,
62 +];
63 +
64 +installFakeEnvironment(THREADS);
65 +// Desktop-sized, so the sidebar, composer and right panel lay out as they do in the app.
66 +await page.viewport(1400, 900);
67 +// A returning user: the first-run gate would otherwise wait on onboarding evidence.
68 +__setClientSettingsForTests({
69 + ...DEFAULT_CLIENT_SETTINGS,
70 + onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
71 +});
72 +
73 +async function quiet() {
74 + let seen = -1;
75 + await vi.waitFor(
76 + () => {
77 + const now = commitsSoFar();
78 + const settled = now === seen;
79 + seen = now;
80 + expect(settled).toBe(true);
81 + },
82 + { interval: 100, timeout: 15_000 },
83 + );
84 +}
85 +
86 +// Relative times and durations re-render on minute and second ticks of the wall clock, so a
87 +// window that spans a tick counts extra renders. Only Date is frozen; timers run for real.
88 +const frozenClock = () =>
89 + fakeTimers({ toFake: ["Date"], now: new Date("2026-03-18T09:00:30.000Z") });
90 +
91 +const threadPath = (threadId: ThreadId) => `/${ENVIRONMENT_ID}/${threadId}`;
92 +const lastAnswer = (threadId: ThreadId) =>
93 + page.getByText(`Answer ${HISTORY_TURNS - 1} for ${threadId}.`);
94 +
95 +// The app owns its router, as main.tsx does, so render() adds none.
96 +async function mountApp(path: string) {
97 + const router = getRouter(createMemoryHistory({ initialEntries: [path] }));
98 + await router.load();
99 + const container = document.createElement("div");
100 + container.style.cssText = "height: 900px; width: 1400px;";
101 + const screen = await render(<AppRoot router={router} />, { container, router: false });
102 + return { screen, router };
103 +}
104 +
105 +async function openThread(threadId: ThreadId) {
106 + const app = await mountApp(threadPath(threadId));
107 + await expect.element(lastAnswer(threadId)).toBeVisible();
108 + await quiet();
109 + return app;
110 +}
111 +
112 +async function measure(action: () => Promise<void>): Promise<RenderReport> {
113 + await quiet();
114 + startCounting();
115 + await action();
116 + await quiet();
117 + return stopCounting();
118 +}
119 +
120 +// Commits are left out across several windows, and where frame batching splits a window's
121 +// commits differently from run to run while its renders stay identical.
122 +async function report(
123 + scenario: string,
124 + windows: ReadonlyArray<RenderReport>,
125 + { commits: countCommits = windows.length === 1 } = {},
126 +) {
127 + const components: Record<string, number> = {};
128 + for (const window of windows) {
129 + for (const [name, count] of Object.entries(window.components)) {
130 + components[name] = (components[name] ?? 0) + count;
131 + }
132 + }
133 + const total = windows.reduce((sum, window) => sum + window.renders, 0);
134 + const commits = countCommits ? windows[0]!.commits : null;
135 + await commands.writeRenderReport(
136 + scenario,
137 + JSON.stringify({ commits, renders: total, components }),
138 + );
139 +}
140 +
141 +// A first mount loads lazy route chunks, fonts and the connection at varying times. The diff
142 +// panel and settings load more chunks on first open (the T3 Connect sidebar, the terminal
143 +// preview's renderer), which can land after a window has gone quiet, so the warm-up opens
144 +// each once and lets it settle.
145 +it("warm up", async () => {
146 + using _clock = frozenClock();
147 + const { screen, router } = await openThread(WARM);
148 + await using _app = screen;
149 + await document.fonts.ready;
150 + await quiet();
151 + await toggleDiff("open")();
152 + await quiet();
153 + await toggleDiff("close")();
154 + await quiet();
155 + await openSettings(router, "/settings/general")();
156 + await openSettings(router, "/settings/appearance")();
157 + await quiet();
158 +});
159 +
160 +it("app-mount-thread", async () => {
161 + using _clock = frozenClock();
162 + startCounting();
163 + await using _app = (await mountApp(threadPath(A))).screen;
164 + await expect.element(lastAnswer(A)).toBeVisible();
165 + await quiet();
166 + await report("app-mount-thread", [stopCounting()]);
167 +});
168 +
169 +it("app-stream-assistant-turn", async () => {
170 + using _clock = frozenClock();
171 + await using _app = (await openThread(LIVE)).screen;
172 + expect(isThreadSubscribed(LIVE)).toBe(true);
173 + const live = runningRun(LIVE, "live-run", HISTORY_TURNS + 1, "live-user");
174 + await act(async () => {
175 + await emitThreadEvent(LIVE, { type: "run.created", payload: live });
176 + await emitThreadEvent(LIVE, {
177 + type: "turn-item.updated",
178 + payload: userItem(LIVE, live.id, "live-user", HISTORY_TURNS * 2, "Continue"),
179 + });
180 + });
181 + await expect.element(page.getByText("Continue", { exact: true })).toBeVisible();
182 + const counted = await measure(async () => {
183 + let text = "";
184 + for (let delta = 0; delta < STREAM_DELTAS; delta++) {
185 + text += `word${delta} `;
186 + await act(() =>
187 + emitThreadEvent(LIVE, {
188 + type: "turn-item.updated",
189 + payload: assistantItem(
190 + LIVE,
191 + live.id,
192 + "live-assistant",
193 + HISTORY_TURNS * 2 + 1,
194 + text,
195 + true,
196 + ),
197 + }),
198 + );
199 + }
200 + await expect
201 + .element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
202 + .toBeVisible();
203 + });
204 + await report("app-stream-assistant-turn", [counted], { commits: false });
205 +});
206 +
207 +it("app-type-in-composer", async () => {
208 + using _clock = frozenClock();
209 + await using _app = (await openThread(A)).screen;
210 + const editor = page.getByTestId("composer-editor");
211 + await userEvent.click(editor);
212 + const counted = await measure(async () => {
213 + await userEvent.type(editor, TYPED_TEXT);
214 + await expect.element(editor).toHaveTextContent(TYPED_TEXT);
215 + });
216 + await report("app-type-in-composer", [counted]);
217 +});
218 +
219 +it("app-switch-thread", async () => {
220 + using _clock = frozenClock();
221 + const { screen, router } = await openThread(A);
222 + await using _app = screen;
223 + const go = (threadId: ThreadId) => async () => {
224 + await act(() =>
225 + router.navigate({
226 + to: "/$environmentId/$threadId",
227 + params: { environmentId: ENVIRONMENT_ID, threadId },
228 + }),
229 + );
230 + await expect.element(lastAnswer(threadId)).toBeVisible();
231 + };
232 + await report("app-switch-thread", [await measure(go(B)), await measure(go(A))]);
233 +});
234 +
235 +// Through the diff.toggle shortcut, the same handler as the header's diff button.
236 +const toggleDiff = (to: "open" | "close") => async () => {
237 + await userEvent.keyboard("{Control>}d{/Control}");
238 + const diff = page.getByRole("group", { name: "Diff layout" });
239 + if (to === "open") await expect.element(diff).toBeVisible();
240 + else await expect.element(diff).not.toBeInTheDocument();
241 +};
242 +
243 +it("app-toggle-diff-panel", async () => {
244 + using _clock = frozenClock();
245 + await using _app = (await openThread(A)).screen;
246 + const open = await measure(toggleDiff("open"));
247 + const close = await measure(toggleDiff("close"));
248 + await report("app-toggle-diff-panel", [open, close]);
249 +});
250 +
251 +const openSettings =
252 + (router: AppRouter, to: "/settings/general" | "/settings/appearance") => async () => {
253 + await act(() => router.navigate({ to }));
254 + // A section title from each page, since neither page has a heading that names it.
255 + const name = to === "/settings/general" ? "Time format" : "Motion";
256 + await expect.element(page.getByRole("heading", { name, exact: true })).toBeVisible();
257 + // Appearance highlights its code font preview off-thread, then attaches it in a shadow root.
258 + if (to === "/settings/appearance") {
259 + await vi.waitFor(
260 + () =>
261 + expect([...document.querySelectorAll("div")].some((node) => node.shadowRoot)).toBe(true),
262 + { interval: 100, timeout: 15_000 },
263 + );
264 + }
265 + };
266 +
267 +it("app-open-settings", async () => {
268 + using _clock = frozenClock();
269 + const { screen, router } = await openThread(A);
270 + await using _app = screen;
271 + await report("app-open-settings", [await measure(openSettings(router, "/settings/general"))]);
272 +});
273 +
274 +it("app-switch-settings-section", async () => {
275 + using _clock = frozenClock();
276 + const { screen, router } = await openThread(A);
277 + await using _app = screen;
278 + await openSettings(router, "/settings/general")();
279 + await report("app-switch-settings-section", [
280 + await measure(openSettings(router, "/settings/appearance")),
281 + ]);
282 +});
apps/web/test/fakeEnvironment.ts new
+469
@@ -0,0 +1,469 @@
1 +// An in-process environment for app render scenarios: a fetch stub for its HTTP API and a
2 +// session that answers the shell, thread and config subscriptions from fixtures. Other RPCs
3 +// stay pending so nothing retries on timers. Everything above the wire is the app's own.
4 +import {
5 + AuthStandardClientScopes,
6 + EnvironmentId,
7 + EventId,
8 + ExecutionEnvironmentDescriptor,
9 + MessageId,
10 + ORCHESTRATION_PROTOCOL_VERSION,
11 + ORCHESTRATION_V2_WS_METHODS,
12 + ProjectId,
13 + ProviderInstanceId,
14 + RunId,
15 + ServerConfig,
16 + ThreadId,
17 + TurnItemId,
18 + WS_METHODS,
19 + WsRpcGroup,
20 + type OrchestrationProjectShell,
21 + type OrchestrationV2ProjectedTurnItem,
22 + type OrchestrationV2Run,
23 + type OrchestrationV2ShellStreamItem,
24 + type OrchestrationV2ThreadProjection,
25 + type OrchestrationV2ThreadShell,
26 + type OrchestrationV2ThreadStreamItem,
27 + type OrchestrationV2TurnItem,
28 +} from "@t3tools/contracts";
29 +import type {
30 + ConnectionAttemptError,
31 + ConnectionCatalogEntry,
32 + EnvironmentConnectionLease,
33 + RouteCheck,
34 +} from "@t3tools/client-runtime/connection";
35 +import type { WsRpcProtocolClient } from "@t3tools/client-runtime/rpc";
36 +import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
37 +import { APP_VERSION } from "~/branding";
38 +import * as DateTime from "effect/DateTime";
39 +import * as Effect from "effect/Effect";
40 +import * as Queue from "effect/Queue";
41 +import type * as Scope from "effect/Scope";
42 +import * as Schema from "effect/Schema";
43 +import * as Stream from "effect/Stream";
44 +import { RpcSchema } from "effect/rpc";
45 +
46 +export const ENVIRONMENT_ID = EnvironmentId.make("environment-render");
47 +const PROJECT_ID = ProjectId.make("project-render");
48 +const CODEX = ProviderInstanceId.make("codex");
49 +const MODEL = { instanceId: CODEX, model: "gpt-5.4" };
50 +const AT = DateTime.makeUnsafe("2026-03-17T19:12:28.000Z");
51 +
52 +const descriptor = {
53 + environmentId: ENVIRONMENT_ID,
54 + label: "Render scenarios",
55 + platform: { os: "linux", arch: "x64" },
56 + // A matching version, since a mismatch adds an update banner a typical user never sees.
57 + serverVersion: APP_VERSION,
58 + orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION,
59 + capabilities: { repositoryIdentity: true },
60 +};
61 +const auth = {
62 + policy: "loopback-browser",
63 + bootstrapMethods: ["one-time-token"],
64 + sessionMethods: ["browser-session-cookie"],
65 + sessionCookieName: "t3_session",
66 +};
67 +
68 +const serverConfig: ServerConfig = {
69 + ...Schema.decodeUnknownSync(ServerConfig)({
70 + environment: descriptor,
71 + auth,
72 + cwd: "/workspace/render",
73 + keybindingsConfigPath: "/workspace/.t3/keybindings.json",
74 + keybindings: [],
75 + issues: [],
76 + providers: [
77 + {
78 + instanceId: CODEX,
79 + driver: "codex",
80 + enabled: true,
81 + installed: true,
82 + version: "1.0.0",
83 + status: "ready",
84 + auth: { status: "authenticated" },
85 + checkedAt: "2026-03-17T19:12:28.000Z",
86 + models: [{ slug: "gpt-5.4", name: "GPT-5.4", isCustom: false, capabilities: null }],
87 + },
88 + ],
89 + availableEditors: [],
90 + observability: {
91 + logsDirectoryPath: "/workspace/.t3/logs",
92 + localTracingEnabled: false,
93 + otlpTracesEnabled: false,
94 + otlpMetricsEnabled: false,
95 + },
96 + settings: {},
97 + threadSnapshotPagination: true,
98 + threadResumeCompletionMarker: true,
99 + shellResumeCompletionMarker: true,
100 + }),
101 + keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
102 +};
103 +
104 +const project: OrchestrationProjectShell = {
105 + id: PROJECT_ID,
106 + title: "render",
107 + workspaceRoot: "/workspace/render",
108 + repositoryIdentity: null,
109 + defaultModelSelection: null,
110 + scripts: [],
111 + createdAt: "2026-03-17T19:12:28.000Z",
112 + updatedAt: "2026-03-17T19:12:28.000Z",
113 +};
114 +
115 +const base = (threadId: ThreadId, runId: RunId, id: string, ordinal: number) => ({
116 + id: TurnItemId.make(id),
117 + threadId,
118 + runId,
119 + nodeId: null,
120 + providerThreadId: null,
121 + providerTurnId: null,
122 + nativeItemRef: null,
123 + parentItemId: null,
124 + ordinal,
125 + status: "completed" as const,
126 + title: null,
127 + startedAt: AT,
128 + completedAt: AT,
129 + updatedAt: AT,
130 +});
131 +
132 +function userItem(threadId: ThreadId, runId: RunId, id: string, ordinal: number, text: string) {
133 + return {
134 + ...base(threadId, runId, id, ordinal),
135 + type: "user_message",
136 + messageId: MessageId.make(id),
137 + text,
138 + attachments: [],
139 + inputIntent: "turn_start",
140 + createdBy: "user",
141 + creationSource: "web",
142 + } satisfies OrchestrationV2TurnItem;
143 +}
144 +
145 +export function assistantItem(
146 + threadId: ThreadId,
147 + runId: RunId,
148 + id: string,
149 + ordinal: number,
150 + text: string,
151 + streaming = false,
152 +) {
153 + return {
154 + ...base(threadId, runId, id, ordinal),
155 + type: "assistant_message",
156 + messageId: MessageId.make(id),
157 + text,
158 + streaming,
159 + ...(streaming ? { status: "running" as const, completedAt: null } : {}),
160 + } satisfies OrchestrationV2TurnItem;
161 +}
162 +
163 +function run(threadId: ThreadId, id: RunId, ordinal: number, userMessageId: string) {
164 + return {
165 + id,
166 + threadId,
167 + ordinal,
168 + providerInstanceId: CODEX,
169 + modelSelection: MODEL,
170 + providerThreadId: null,
171 + userMessageId: MessageId.make(userMessageId),
172 + rootNodeId: null,
173 + activeAttemptId: null,
174 + status: "completed",
175 + requestedAt: AT,
176 + startedAt: AT,
177 + completedAt: AT,
178 + checkpointId: null,
179 + contextHandoffId: null,
180 + } satisfies OrchestrationV2Run;
181 +}
182 +
183 +export interface FakeThread {
184 + readonly id: ThreadId;
185 + readonly title: string;
186 + readonly runs: ReadonlyArray<OrchestrationV2Run>;
187 + readonly items: ReadonlyArray<OrchestrationV2TurnItem>;
188 +}
189 +
190 +/** A thread of `turns` user prompts, each answered in markdown with a list and inline code. */
191 +export function threadWithHistory(name: string, turns: number): FakeThread {
192 + const id = ThreadId.make(name);
193 + const runs: OrchestrationV2Run[] = [];
194 + const items: OrchestrationV2TurnItem[] = [];
195 + for (let turn = 0; turn < turns; turn++) {
196 + const runId = RunId.make(`${name}-run-${turn}`);
197 + runs.push(run(id, runId, turn + 1, `${name}-user-${turn}`));
198 + items.push(
199 + userItem(id, runId, `${name}-user-${turn}`, turn * 2, `${name}: question ${turn}`),
200 + assistantItem(
201 + id,
202 + runId,
203 + `${name}-assistant-${turn}`,
204 + turn * 2 + 1,
205 + `Answer ${turn} for ${name}.\n\n- checked \`Sidebar.tsx\`\n- ran the tests\n\n` +
206 + "The width is now `clamp(next, MIN_WIDTH, MAX_WIDTH)`, so the layout no longer jumps.",
207 + ),
208 + );
209 + }
210 + return { id, title: `Thread ${name}`, runs, items };
211 +}
212 +
213 +function shellOf(thread: FakeThread): OrchestrationV2ThreadShell {
214 + const latest = thread.runs.at(-1);
215 + return {
216 + id: thread.id,
217 + projectId: PROJECT_ID,
218 + title: thread.title,
219 + providerInstanceId: CODEX,
220 + modelSelection: MODEL,
221 + runtimeMode: "full-access",
222 + interactionMode: "default",
223 + branch: "main",
224 + worktreePath: null,
225 + activeProviderThreadId: null,
226 + lineage: { rootThreadId: thread.id, parentThreadId: null, relationshipToParent: null },
227 + forkedFrom: null,
228 + createdBy: "user",
229 + creationSource: "web",
230 + latestRunId: latest?.id ?? null,
231 + activeRunId: null,
232 + // Idle reads as waiting on the user, which shows a per-second duration in the sidebar.
233 + status: latest ? "completed" : "idle",
234 + pendingRuntimeRequest: null,
235 + latestVisibleMessage: null,
236 + latestUserMessageAt: AT,
237 + hasActionableProposedPlan: false,
238 + itemCount: thread.items.length,
239 + visibleItemCount: thread.items.length,
240 + createdAt: AT,
241 + updatedAt: AT,
242 + archivedAt: null,
243 + settledOverride: null,
244 + settledAt: null,
245 + lastVisitedAt: AT,
246 + deletedAt: null,
247 + };
248 +}
249 +
250 +function projectionOf(thread: FakeThread): OrchestrationV2ThreadProjection {
251 + const shell = shellOf(thread);
252 + const visibleTurnItems: OrchestrationV2ProjectedTurnItem[] = thread.items.map(
253 + (item, position) => ({
254 + position,
255 + visibility: "local",
256 + sourceThreadId: thread.id,
257 + sourceItemId: item.id,
258 + item,
259 + }),
260 + );
261 + return {
262 + thread: {
263 + id: shell.id,
264 + projectId: shell.projectId,
265 + title: shell.title,
266 + providerInstanceId: shell.providerInstanceId,
267 + modelSelection: shell.modelSelection,
268 + runtimeMode: shell.runtimeMode,
269 + interactionMode: shell.interactionMode,
270 + branch: shell.branch,
271 + worktreePath: shell.worktreePath,
272 + activeProviderThreadId: null,
273 + lineage: shell.lineage,
274 + forkedFrom: null,
275 + createdBy: "user",
276 + creationSource: "web",
277 + createdAt: AT,
278 + updatedAt: AT,
279 + archivedAt: null,
280 + settledOverride: null,
281 + settledAt: null,
282 + lastVisitedAt: AT,
283 + deletedAt: null,
284 + },
285 + runs: [...thread.runs],
286 + attempts: [],
287 + nodes: [],
288 + subagents: [],
289 + providerSessions: [],
290 + providerThreads: [],
291 + providerTurns: [],
292 + runtimeRequests: [],
293 + messages: [],
294 + plans: [],
295 + turnItems: [...thread.items],
296 + checkpointScopes: [],
297 + checkpoints: [],
298 + contextHandoffs: [],
299 + contextTransfers: [],
300 + visibleTurnItems,
301 + updatedAt: AT,
302 + };
303 +}
304 +
305 +const json = (body: unknown, status = 200) =>
306 + new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
307 +
308 +// Keyed by thread id: a live thread subscription's queue, for pushing events to it.
309 +const threadSubscribers = new Map<string, Queue.Queue<OrchestrationV2ThreadStreamItem>>();
310 +let threads: ReadonlyArray<FakeThread> = [];
311 +let sequence = 1;
312 +
313 +const subscribe = <A>(first: ReadonlyArray<A>, register?: (queue: Queue.Queue<A>) => () => void) =>
314 + Stream.unwrap(
315 + Effect.gen(function* () {
316 + const queue = yield* Queue.unbounded<A>();
317 + yield* Queue.offerAll(queue, first);
318 + if (register) {
319 + const unregister = register(queue);
320 + yield* Effect.addFinalizer(() => Effect.sync(unregister));
321 + }
322 + return Stream.fromQueue(queue);
323 + }),
324 + );
325 +
326 +const handlers: Record<string, (input: never) => unknown> = {
327 + [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () =>
328 + subscribe<OrchestrationV2ShellStreamItem>([
329 + {
330 + kind: "snapshot",
331 + snapshot: {
332 + schemaVersion: 1,
333 + snapshotSequence: sequence,
334 + projects: [project],
335 + threads: threads.map(shellOf),
336 + archivedThreads: [],
337 + },
338 + },
339 + { kind: "synchronized" },
340 + ]),
341 + [ORCHESTRATION_V2_WS_METHODS.subscribeThread]: (input: { readonly threadId: ThreadId }) => {
342 + const thread = threads.find((candidate) => candidate.id === input.threadId);
343 + if (!thread) return Stream.never;
344 + return subscribe<OrchestrationV2ThreadStreamItem>(
345 + [
346 + { kind: "snapshot", snapshotSequence: sequence, projection: projectionOf(thread) },
347 + { kind: "synchronized" },
348 + ],
349 + (queue) => {
350 + threadSubscribers.set(thread.id, queue);
351 +
352 + // A replaced subscription closes after its successor registers.
353 + return () => {
354 + if (threadSubscribers.get(thread.id) === queue) threadSubscribers.delete(thread.id);
355 + };
356 + },
357 + );
358 + },
359 + [WS_METHODS.subscribeServerConfig]: () =>
360 + subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig }]),
361 +};
362 +
363 +// Unanswered calls stay pending, like a server that has not replied yet.
364 +const client = new Proxy(handlers, {
365 + get: (target, method: string) =>
366 + target[method] ??
367 + (() => {
368 + const rpc = WsRpcGroup.requests.get(method);
369 + return rpc && RpcSchema.isStreamSchema(rpc.successSchema) ? Stream.never : Effect.never;
370 + }),
371 +});
372 +
373 +const session: EnvironmentConnectionLease["session"] = {
374 + client: client as unknown as WsRpcProtocolClient,
375 + initialConfig: Effect.succeed(serverConfig),
376 + subscribeServerConfig: () =>
377 + subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig }]),
378 + ready: Effect.void,
379 + probe: Effect.void,
380 + closed: Effect.never,
381 +};
382 +
383 +/** Stands in for client-runtime's ConnectionDriver: every connection opens the session above. */
384 +export const fakeDriver = {
385 + connect: (
386 + entry: ConnectionCatalogEntry,
387 + ): Effect.Effect<EnvironmentConnectionLease, ConnectionAttemptError, Scope.Scope> =>
388 + Effect.succeed({
389 + prepared: {
390 + environmentId: ENVIRONMENT_ID,
391 + label: descriptor.label,
392 + httpBaseUrl: window.location.origin,
393 + socketUrl: window.location.origin.replace(/^http/, "ws"),
394 + httpAuthorization: null,
395 + target: entry.target,
396 + },
397 + session,
398 + }),
399 + checkRoute: () => Effect.succeed<RouteCheck>("answered"),
400 + preflight: () => Effect.succeed(true),
401 +};
402 +export type FakeDriver = typeof fakeDriver;
403 +
404 +const realFetch = globalThis.fetch;
405 +const encodedDescriptor = Schema.encodeSync(ExecutionEnvironmentDescriptor)(
406 + Schema.decodeUnknownSync(ExecutionEnvironmentDescriptor)(descriptor),
407 +);
408 +
409 +/** Serves the environment's HTTP API and these threads until disposed. */
410 +export function installFakeEnvironment(fixtures: ReadonlyArray<FakeThread>): Disposable {
411 + threads = fixtures;
412 + globalThis.fetch = async (input, init) => {
413 + const url = new URL(input instanceof Request ? input.url : String(input), window.location.href);
414 + if (url.origin !== window.location.origin) return realFetch(input, init);
415 + if (url.pathname === "/.well-known/t3/environment") return json(encodedDescriptor);
416 + if (url.pathname === "/api/auth/session") {
417 + return json({
418 + authenticated: true,
419 + auth,
420 + scopes: AuthStandardClientScopes,
421 + sessionMethod: "browser-session-cookie",
422 + expiresAt: "2099-01-01T00:00:00.000Z",
423 + });
424 + }
425 + // Snapshot endpoints fall back to the socket, which serves the same fixtures.
426 + if (/^\/(api|oauth|\.well-known)\//.test(url.pathname)) return json({}, 404);
427 + return realFetch(input, init);
428 + };
429 + return {
430 + [Symbol.dispose]: () => {
431 + globalThis.fetch = realFetch;
432 + threads = [];
433 + },
434 + };
435 +}
436 +
437 +type ThreadEvent = Extract<OrchestrationV2ThreadStreamItem, { readonly kind: "event" }>["event"];
438 +
439 +/** Whether the app holds a live subscription to the thread, so events reach it. */
440 +export function isThreadSubscribed(threadId: ThreadId): boolean {
441 + return threadSubscribers.has(threadId);
442 +}
443 +
444 +/** Delivers one domain event, such as a run or turn-item update, to the thread's subscription. */
445 +export async function emitThreadEvent(
446 + threadId: ThreadId,
447 + event: Pick<ThreadEvent, "type" | "payload">,
448 +): Promise<void> {
449 + const queue = threadSubscribers.get(threadId);
450 + if (!queue) throw new Error(`No live subscription for ${threadId}`);
451 + sequence += 1;
452 + await Effect.runPromise(
453 + Queue.offer(queue, {
454 + kind: "event",
455 + sequence,
456 + event: { ...event, id: EventId.make(`event-${sequence}`), threadId, occurredAt: AT },
457 + } as OrchestrationV2ThreadStreamItem),
458 + );
459 +}
460 +
461 +export function runningRun(threadId: ThreadId, id: string, ordinal: number, userMessageId: string) {
462 + return {
463 + ...run(threadId, RunId.make(id), ordinal, userMessageId),
464 + status: "running" as const,
465 + completedAt: null,
466 + };
467 +}
468 +
469 +export { userItem };
apps/web/test/renderCounter.ts new
+108
@@ -0,0 +1,108 @@
1 +// Counts commits and component renders via the DevTools hook, so it loads before react-dom.
2 +// Same walk as apps/mobile/src/features/development/render-counter.ts: bailed-out subtrees
3 +// (child === alternate.child) keep stale PerformedWork flags and are skipped.
4 +
5 +interface Fiber {
6 + readonly tag: number;
7 + readonly type: unknown;
8 + readonly flags: number;
9 + readonly alternate: Fiber | null;
10 + readonly child: Fiber | null;
11 + readonly sibling: Fiber | null;
12 +}
13 +
14 +export interface RenderReport {
15 + readonly commits: number;
16 + readonly renders: number;
17 + readonly components: Readonly<Record<string, number>>;
18 +}
19 +
20 +// Function, class, forwardRef, memo and simple memo components.
21 +const COMPONENT_TAGS = new Set([0, 1, 11, 14, 15]);
22 +const PERFORMED_WORK = 1;
23 +
24 +function nameOf(fiber: Fiber): string | null {
25 + const type = fiber.type as {
26 + displayName?: string;
27 + name?: string;
28 + type?: { displayName?: string; name?: string };
29 + render?: { displayName?: string; name?: string };
30 + } | null;
31 + if (!type || (typeof type !== "object" && typeof type !== "function")) return null;
32 + const inner = type.type ?? type.render ?? type;
33 + const raw = type.displayName || inner.displayName || inner.name || "";
34 + return raw.replace(/^(Forget\(|Memo\()+|\)+$/g, "") || "Anonymous";
35 +}
36 +
37 +// The setup file and a test file can load this module as two instances. React reports to
38 +// whichever hook existed when react-dom initialized, so both share one hook and one state.
39 +const STATE = Symbol.for("t3code.renderCounter");
40 +const state = ((globalThis as { [STATE]?: CounterState })[STATE] ??= {
41 + on: false,
42 + commits: 0,
43 + everCommitted: 0,
44 + counts: new Map(),
45 +});
46 +
47 +interface CounterState {
48 + on: boolean;
49 + commits: number;
50 + everCommitted: number;
51 + counts: Map<string, number>;
52 +}
53 +
54 +/** Adds the components that rendered under `fiber` and returns how many did. */
55 +function visit(fiber: Fiber | null): number {
56 + let rendered = 0;
57 + for (let node = fiber; node; node = node.sibling) {
58 + const performed =
59 + COMPONENT_TAGS.has(node.tag) && (node.flags & PERFORMED_WORK) === PERFORMED_WORK;
60 + const name = performed ? nameOf(node) : null;
61 + if (name) {
62 + state.counts.set(name, (state.counts.get(name) ?? 0) + 1);
63 + rendered += 1;
64 + }
65 + const skipped = node.alternate !== null && node.child === node.alternate.child && !performed;
66 + if (node.child && !skipped) rendered += visit(node.child);
67 + }
68 + return rendered;
69 +}
70 +
71 +// React only reports commits to a hook that exists when react-dom initializes.
72 +if (!("__REACT_DEVTOOLS_GLOBAL_HOOK__" in globalThis))
73 + Object.assign(globalThis, {
74 + __REACT_DEVTOOLS_GLOBAL_HOOK__: {
75 + supportsFiber: true,
76 + renderers: new Map(),
77 + inject: () => 1,
78 + checkDCE: () => {},
79 + onCommitFiberUnmount: () => {},
80 + onPostCommitFiberRoot: () => {},
81 + onCommitFiberRoot: (_id: unknown, root: { current: Fiber }) => {
82 + state.everCommitted += 1;
83 + // Commits that render no component (layout-only batches) vary with frame timing.
84 + if (state.on && visit(root.current.child) > 0) state.commits += 1;
85 + },
86 + },
87 + });
88 +
89 +/** Every commit since load, counted or not, so a scenario can wait for the tree to go quiet. */
90 +export function commitsSoFar(): number {
91 + return state.everCommitted;
92 +}
93 +
94 +export function startCounting(): void {
95 + state.commits = 0;
96 + state.counts = new Map();
97 + state.on = true;
98 +}
99 +
100 +export function stopCounting(): RenderReport {
101 + state.on = false;
102 + const sorted = [...state.counts].sort(([a, x], [b, y]) => y - x || a.localeCompare(b));
103 + return {
104 + commits: state.commits,
105 + renders: sorted.reduce((total, [, count]) => total + count, 0),
106 + components: Object.fromEntries(sorted),
107 + };
108 +}
apps/web/test/renderScenarios.renders.tsx new
+283
@@ -0,0 +1,283 @@
1 +// Render scenarios for scripts/render-metrics.ts. Each one mounts real components on
2 +// fixed fixtures, counts React commits and component renders while it drives them,
3 +// and writes the counts through the writeRenderReport browser command. They mount the
4 +// components ChatView composes, not ChatView, which needs a live environment connection.
5 +import { EnvironmentId, MessageId } from "@t3tools/contracts";
6 +import type { LegendListRef } from "@legendapp/list/react";
7 +import { createRef, useRef, useState, type ComponentProps, type ReactNode } from "react";
8 +import { expect, it, vi } from "vite-plus/test";
9 +import { commands, userEvent } from "vite-plus/test/browser";
10 +
11 +import "~/index.css";
12 +
13 +import { MessagesTimeline } from "~/components/chat/MessagesTimeline";
14 +import {
15 + ComposerPromptEditor,
16 + type ComposerPromptEditorHandle,
17 +} from "~/components/ComposerPromptEditor";
18 +import { commitsSoFar, startCounting, stopCounting } from "./renderCounter";
19 +import { render } from "./render";
20 +
21 +declare module "vite-plus/test/browser" {
22 + interface BrowserCommands {
23 + /** Defined in vite.config.ts for the renders project. */
24 + writeRenderReport: (scenario: string, json: string) => Promise<void>;
25 + }
26 +}
27 +
28 +vi.mock(import("~/state/query"), async (original) => ({
29 + ...(await original()),
30 + useEnvironmentQuery: (() => ({
31 + data: { tasks: [] },
32 + dataUpdatedAt: 0,
33 + error: null,
34 + failure: null,
35 + isPending: false,
36 + isSuccess: true,
37 + refresh: () => {},
38 + })) as never,
39 +}));
40 +
41 +const ENVIRONMENT_ID = EnvironmentId.make("environment-local");
42 +const CREATED_AT = "2026-03-17T19:12:28.000Z";
43 +const HISTORY_TURNS = 20;
44 +const STREAM_DELTAS = 30;
45 +const TYPED_TEXT = "fix the flaky tests!";
46 +
47 +type TimelineProps = ComponentProps<typeof MessagesTimeline>;
48 +type Entry = TimelineProps["timelineEntries"][number];
49 +
50 +function message(id: string, role: "user" | "assistant", text: string, streaming = false): Entry {
51 + return {
52 + id: `entry-${id}`,
53 + kind: "message",
54 + createdAt: CREATED_AT,
55 + message: {
56 + id: MessageId.make(id),
57 + role,
58 + text,
59 + runId: null,
60 + createdAt: CREATED_AT,
61 + updatedAt: CREATED_AT,
62 + streaming,
63 + },
64 + } as Entry;
65 +}
66 +
67 +// A thread with history: user prompts and markdown answers with lists and inline code.
68 +// No fenced code: Shiki highlights it asynchronously, and whether a block first renders
69 +// its fallback varies run to run.
70 +function history(thread: string): Entry[] {
71 + return Array.from({ length: HISTORY_TURNS }, (_, turn) => [
72 + message(`${thread}-user-${turn}`, "user", `${thread}: question ${turn} about the sidebar`),
73 + message(
74 + `${thread}-assistant-${turn}`,
75 + "assistant",
76 + `Answer ${turn} for ${thread}.\n\n- checked \`Sidebar.tsx\`\n- ran the tests\n\n` +
77 + "The width is now `clamp(next, MIN_WIDTH, MAX_WIDTH)`, so the layout no longer jumps.",
78 + ),
79 + ]).flat();
80 +}
81 +
82 +// Props that some trees under comparison have dropped. A JSX spread is not checked for
83 +// excess properties, so this file type-checks on trees with and without them.
84 +const versionedProps = { onManualNavigation: () => {} };
85 +
86 +// One list handle per mounted timeline, as ChatView keeps one: the timeline compares the
87 +// handle across renders, and a new one per render would skip its scroll-to-end and settle work.
88 +function timelineFor() {
89 + const listRef = createRef<LegendListRef | null>();
90 + return (thread: string, entries: Entry[]) => (
91 + <MessagesTimeline
92 + timelineEntries={entries}
93 + isWorking={false}
94 + activeTurnInProgress={false}
95 + listRef={listRef}
96 + latestRun={null}
97 + turnDiffSummaries={[]}
98 + runs={[]}
99 + routeThreadKey={`environment-local:${thread}`}
100 + onOpenTurnDiff={() => {}}
101 + onOpenThread={() => {}}
102 + onForkFromRun={async () => {}}
103 + onRollbackCheckpoint={() => {}}
104 + supportsConversationRollback={false}
105 + onRevertToTurnCount={() => {}}
106 + isRevertingCheckpoint={false}
107 + onImageExpand={() => {}}
108 + activeThreadEnvironmentId={ENVIRONMENT_ID}
109 + markdownCwd={undefined}
110 + resolvedTheme="light"
111 + timestampFormat="locale"
112 + workspaceRoot={undefined}
113 + anchorMessageId={null}
114 + onAnchorReady={() => {}}
115 + onAnchorSizeChanged={() => {}}
116 + contentInsetEndAdjustment={0}
117 + emptyPlaceholderInset={0}
118 + liveFollowEnabled
119 + onIsAtEndChange={() => {}}
120 + {...versionedProps}
121 + />
122 + );
123 +}
124 +
125 +function sizedContainer() {
126 + const container = document.createElement("div");
127 + container.style.cssText = "height: 900px; width: 900px; display: flex; flex-direction: column;";
128 + return container;
129 +}
130 +
131 +// Legend List measures rows, and the minimap follows scroll, on later frames, and how
132 +// much of that lands inside a window varies run to run. Every window opens and closes
133 +// on a quiet tree: no commit across two consecutive checks.
134 +async function quiet() {
135 + let seen = -1;
136 + await vi.waitFor(
137 + () => {
138 + const now = commitsSoFar();
139 + const settled = now === seen;
140 + seen = now;
141 + expect(settled).toBe(true);
142 + },
143 + { interval: 100, timeout: 10_000 },
144 + );
145 +}
146 +
147 +// Off the guest page the connection runtime retries a missing environment on timers, and
148 +// its atom updates commit at varying times. On it, the runtime fails once and stays quiet.
149 +function onGuestPage(): Disposable {
150 + const previous = `${window.location.pathname}${window.location.search}`;
151 + window.history.replaceState(window.history.state, "", "/guest?share=render-scenarios");
152 + return {
153 + [Symbol.dispose]: () => window.history.replaceState(window.history.state, "", previous),
154 + };
155 +}
156 +
157 +// A first mount loads lazy modules, web fonts and syntax highlighting at varying times.
158 +// Each scenario mounts its tree once uncounted, so the counted run starts warm.
159 +async function warmUp(ui: ReactNode, container?: HTMLElement) {
160 + await using screen = await render(ui, container ? { container } : {});
161 + await document.fonts.ready;
162 + await quiet();
163 + expect(screen.container.childElementCount).toBeGreaterThan(0);
164 +}
165 +
166 +async function report(scenario: string) {
167 + await quiet();
168 + const counts = stopCounting();
169 + await commands.writeRenderReport(scenario, JSON.stringify(counts));
170 +}
171 +
172 +it("mount-thread", async () => {
173 + using _guest = onGuestPage();
174 + const timeline = timelineFor();
175 + await warmUp(timelineFor()("thread-warm", history("thread-warm")), sizedContainer());
176 + startCounting();
177 + await using screen = await render(timeline("thread-a", history("thread-a")), {
178 + container: sizedContainer(),
179 + });
180 + await expect.element(screen.getByText(`Answer ${HISTORY_TURNS - 1} for thread-a.`)).toBeVisible();
181 + await report("mount-thread");
182 +});
183 +
184 +it("stream-assistant-turn", async () => {
185 + using _guest = onGuestPage();
186 + const timeline = timelineFor();
187 + const base = history("thread-a");
188 + const streamed = (text: string) => [
189 + ...base,
190 + message("live-user", "user", "Continue"),
191 + message("live-assistant", "assistant", text, true),
192 + ];
193 + await warmUp(timelineFor()("thread-warm", streamed("warm")), sizedContainer());
194 + await using screen = await render(timeline("thread-a", streamed("")), {
195 + container: sizedContainer(),
196 + });
197 + await expect.element(screen.getByText("Continue")).toBeVisible();
198 + await quiet();
199 +
200 + startCounting();
201 + let text = "";
202 + for (let delta = 0; delta < STREAM_DELTAS; delta++) {
203 + text += `word${delta} `;
204 + await screen.rerender(timeline("thread-a", streamed(text)));
205 + }
206 + await expect
207 + .element(screen.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
208 + .toBeVisible();
209 + await report("stream-assistant-turn");
210 +});
211 +
212 +// Thread A to B and back, each switch counted until the tree is quiet again. Commits
213 +// are not reported: the minimap's follow-up renders sometimes split across two frames,
214 +// so the commit count varies by one while the renders stay identical.
215 +it("switch-thread", async () => {
216 + using _guest = onGuestPage();
217 + const timeline = timelineFor();
218 + const a = history("thread-a");
219 + const b = history("thread-b");
220 + const lastAnswer = (thread: string) =>
221 + screen.getByText(`Answer ${HISTORY_TURNS - 1} for ${thread}.`);
222 + await warmUp(timelineFor()("thread-warm", a), sizedContainer());
223 + await using screen = await render(timeline("thread-a", a), { container: sizedContainer() });
224 + await expect.element(lastAnswer("thread-a")).toBeVisible();
225 +
226 + await quiet();
227 + startCounting();
228 + await screen.rerender(timeline("thread-b", b));
229 + await expect.element(lastAnswer("thread-b")).toBeVisible();
230 + await quiet();
231 + const toB = stopCounting();
232 + startCounting();
233 + await screen.rerender(timeline("thread-a", a));
234 + await expect.element(lastAnswer("thread-a")).toBeVisible();
235 + await quiet();
236 + const toA = stopCounting();
237 +
238 + const components = { ...toB.components };
239 + for (const [name, count] of Object.entries(toA.components)) {
240 + components[name] = (components[name] ?? 0) + count;
241 + }
242 + await commands.writeRenderReport(
243 + "switch-thread",
244 + JSON.stringify({
245 + commits: null,
246 + renders: toB.renders + toA.renders,
247 + components,
248 + }),
249 + );
250 +});
251 +
252 +function Composer() {
253 + const [prompt, setPrompt] = useState({ value: "", cursor: 0 });
254 + const editorRef = useRef<ComposerPromptEditorHandle>(null);
255 + return (
256 + <ComposerPromptEditor
257 + editorRef={editorRef}
258 + value={prompt.value}
259 + cursor={prompt.cursor}
260 + richTextEnabled
261 + contextRecords={new Map()}
262 + skills={[]}
263 + disabled={false}
264 + placeholder="Ask anything"
265 + onChange={(value, cursor) => setPrompt({ value, cursor })}
266 + onPaste={() => {}}
267 + />
268 + );
269 +}
270 +
271 +it("type-in-composer", async () => {
272 + using _guest = onGuestPage();
273 + await warmUp(<Composer />);
274 + await using screen = await render(<Composer />);
275 + const editor = screen.getByRole("textbox");
276 + await userEvent.click(editor);
277 + await quiet();
278 +
279 + startCounting();
280 + await userEvent.type(editor, TYPED_TEXT);
281 + await expect.element(editor).toHaveTextContent(TYPED_TEXT);
282 + await report("type-in-composer");
283 +});
apps/web/vite.config.ts
+35 -1
@@ -1,3 +1,5 @@
1 +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Vite config runs before any Effect runtime.
2 +import * as NodeFSP from "node:fs/promises";
3 import * as NodeZlib from "node:zlib";
4
5 import react from "@vitejs/plugin-react";
@@ -122,6 +124,34 @@ const browserTestProject = {
124 },
125 } satisfies TestProjectInlineConfiguration;
126
127 +// Render scenarios count React commits; they run only through scripts/render-metrics.ts,
128 +// which reads the per-scenario reports this command writes.
129 +const renderScenariosProject = {
130 + ...browserTestProject,
131 + test: {
132 + ...browserTestProject.test,
133 + name: "renders",
134 + include: ["test/*.renders.tsx"],
135 + // App scenarios mount the whole app per test, which outlasts 15s on a loaded machine.
136 + hookTimeout: 60_000,
137 + testTimeout: 60_000,
138 + setupFiles: ["./test/renderCounter.ts", ...browserTestProject.test.setupFiles],
139 + browser: {
140 + ...browserTestProject.test.browser,
141 + // Vitest names each instance in place, so a shared array would clash with the browser project.
142 + instances: [{ browser: "chromium" }],
143 + commands: {
144 + ...browserTestProject.test.browser.commands,
145 + writeRenderReport: async (_context, scenario: string, json: string) => {
146 + const directory = process.env.T3CODE_RENDER_REPORT_DIR;
147 + if (!directory) throw new Error("T3CODE_RENDER_REPORT_DIR is not set");
148 + await NodeFSP.writeFile(`${directory}/${scenario}.json`, json);
149 + },
150 + },
151 + },
152 + },
153 +} satisfies TestProjectInlineConfiguration;
154 +
155 function resolveDevProxyTarget(
156 backendPort: string | undefined,
157 wsUrl: string | undefined,
@@ -296,7 +326,11 @@ export default defineConfig(() => {
326 sourcemap: buildSourcemap,
327 },
328 test: {
299 - projects: [defineProject(unitTestProject), defineProject(browserTestProject)],
329 + projects: [
330 + defineProject(unitTestProject),
331 + defineProject(browserTestProject),
332 + defineProject(renderScenariosProject),
333 + ],
334 },
335 };
336 });
package.json
+1
@@ -27,6 +27,7 @@
27 "typecheck": "vp run -r --cache --concurrency-limit 4 typecheck",
28 "tc": "vp run -r --cache --concurrency-limit 4 typecheck",
29 "check:changed": "node scripts/check-changed.ts",
30 + "report:renders": "node scripts/render-metrics.ts",
31 "lint": "vp lint --report-unused-disable-directives",
32 "knip": "knip --preprocessor ./scripts/knip-schemas.ts",
33 "knip:check": "knip --include files,dependencies --no-config-hints && knip --workspace apps/server --workspace apps/desktop --workspace apps/web --workspace packages/client-runtime --workspace packages/contracts --workspace packages/effect-acp --workspace packages/effect-codex-app-server --workspace packages/shared --workspace packages/ssh --workspace packages/tailscale --workspace packages/provider-core --workspace packages/provider-acp --workspace packages/provider-acp-registry --workspace packages/provider-testing --workspace packages/provider-pi --workspace packages/provider-muse --workspace packages/provider-opencode --workspace packages/provider-cursor --workspace packages/provider-grok --workspace packages/source-control-core --workspace packages/source-control-definitions --workspace packages/source-control-azure-devops --workspace packages/source-control-bitbucket --workspace packages/source-control-forgejo --workspace packages/source-control-gitcafe --workspace packages/source-control-github --workspace packages/source-control-gitlab --workspace packages/source-control-testing --exports --preprocessor ./scripts/knip-schemas.ts --no-config-hints",
scripts/render-metrics.test.ts new
+27
@@ -0,0 +1,27 @@
1 +import { describe, expect, it } from "vite-plus/test";
2 +
3 +import { nondeterministic, renderComparison } from "./render-metrics.ts";
4 +
5 +describe("nondeterministic", () => {
6 + it("names only the scenarios whose counts differ between runs", () => {
7 + const stable = { commits: 2, renders: 10, components: { Row: 10 } };
8 + expect(
9 + nondeterministic([
10 + { mount: stable, type: { commits: 1, renders: 4, components: { Editor: 4 } } },
11 + { mount: stable, type: { commits: 1, renders: 5, components: { Editor: 5 } } },
12 + ]),
13 + ).toEqual(["type"]);
14 + });
15 +});
16 +
17 +describe("renderComparison", () => {
18 + it("prints per-scenario deltas and the components that moved most", () => {
19 + const output = renderComparison(
20 + { stream: { commits: 40, renders: 400, components: { Row: 300, Header: 100 } } },
21 + { stream: { commits: 30, renders: 250, components: { Row: 150, Header: 100 } } },
22 + );
23 + expect(output).toContain("| stream | 40 → 30 (-10, -25%) | 400 → 250 (-150, -37%) |");
24 + expect(output).toContain("| `Row` | 300 → 150 (-150, -50%) |");
25 + expect(output).not.toContain("`Header`");
26 + });
27 +});
scripts/render-metrics.ts new
+156
@@ -0,0 +1,156 @@
1 +// @effect-diagnostics nodeBuiltinImport:off globalConsole:off - a synchronous wrapper around one vitest project.
2 +import * as NodeChildProcess from "node:child_process";
3 +import * as NodeFS from "node:fs";
4 +import * as NodeOS from "node:os";
5 +import * as NodePath from "node:path";
6 +
7 +const HELP = `Usage:
8 + node scripts/render-metrics.ts run [--out <file>] [--runs <n>]
9 + node scripts/render-metrics.ts compare <before.json> <after.json> [--top <n>]
10 +
11 +run Runs the web render scenarios (apps/web/test/renderScenarios.renders.tsx,
12 + vitest project "renders") on this checkout and writes one JSON object of
13 + { scenario: { commits, renders, components } }. With --runs above 1 (default 3)
14 + it fails unless every run produced identical counts.
15 +compare Prints a markdown table of commit and render deltas per scenario, plus the
16 + components whose counts changed most (default top 10).
17 +
18 +commits are React commits that rendered at least one component (null where frame
19 +timing splits them); renders are function and class component renders, counted from the
20 +fibers React worked on. Every scenario waits for the tree to go quiet before and after
21 +its counted window, so layout follow-ups are included.
22 +
23 +Comparing two refs needs both checkouts, since the scenarios run each tree's code:
24 + node scripts/render-metrics.ts run --out ../main-renders.json # on main
25 + node scripts/render-metrics.ts run --out ../branch-renders.json # on the branch
26 + node scripts/render-metrics.ts compare ../main-renders.json ../branch-renders.json
27 +A second checkout comes from \`git worktree add ../<name> main\`; copy the harness files
28 +in if that tree predates them.`;
29 +
30 +export interface ScenarioReport {
31 + /** null where a scenario's commit count depends on frame timing. */
32 + readonly commits: number | null;
33 + readonly renders: number;
34 + readonly components: Readonly<Record<string, number>>;
35 +}
36 +export type RenderMetrics = Readonly<Record<string, ScenarioReport>>;
37 +
38 +const root = NodePath.resolve(import.meta.dirname, "..");
39 +
40 +function runOnce(): RenderMetrics {
41 + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.homedir(), ".cache", "t3-renders-"));
42 + try {
43 + const result = NodeChildProcess.spawnSync("vp", ["test", "run", "--project", "renders"], {
44 + cwd: NodePath.join(root, "apps/web"),
45 + env: { ...process.env, T3CODE_RENDER_REPORT_DIR: directory },
46 + stdio: ["ignore", "inherit", "inherit"],
47 + });
48 + if (result.status !== 0) throw new Error(`render scenarios failed (exit ${result.status})`);
49 + const files = NodeFS.readdirSync(directory)
50 + .filter((file) => file.endsWith(".json"))
51 + .toSorted();
52 + return Object.fromEntries(
53 + files.map((file) => [
54 + file.replace(/\.json$/, ""),
55 + JSON.parse(NodeFS.readFileSync(NodePath.join(directory, file), "utf8")) as ScenarioReport,
56 + ]),
57 + );
58 + } finally {
59 + NodeFS.rmSync(directory, { recursive: true, force: true });
60 + }
61 +}
62 +
63 +/** Scenarios whose counts differ between runs, by name. */
64 +export function nondeterministic(runs: ReadonlyArray<RenderMetrics>): string[] {
65 + const [first, ...rest] = runs;
66 + if (!first) return [];
67 + return Object.keys(first).filter((scenario) =>
68 + rest.some((run) => JSON.stringify(run[scenario]) !== JSON.stringify(first[scenario])),
69 + );
70 +}
71 +
72 +const signed = (value: number) => (value > 0 ? `+${value}` : `${value}`);
73 +const change = (before: number | null, after: number | null) =>
74 + before === null || after === null
75 + ? "n/a"
76 + : before === after
77 + ? `${after}`
78 + : `${before} → ${after} (${signed(after - before)}${before ? `, ${signed(Math.round(((after - before) / before) * 100))}%` : ""})`;
79 +
80 +export function renderComparison(before: RenderMetrics, after: RenderMetrics, top = 10): string {
81 + const scenarios = [...new Set([...Object.keys(before), ...Object.keys(after)])].toSorted();
82 + const lines = [
83 + "### Render scenarios",
84 + "",
85 + "| scenario | commits | renders |",
86 + "| :-- | --: | --: |",
87 + ];
88 + const empty: ScenarioReport = { commits: 0, renders: 0, components: {} };
89 + for (const scenario of scenarios) {
90 + const a = before[scenario] ?? empty;
91 + const b = after[scenario] ?? empty;
92 + lines.push(
93 + `| ${scenario} | ${change(a.commits, b.commits)} | ${change(a.renders, b.renders)} |`,
94 + );
95 + }
96 + for (const scenario of scenarios) {
97 + const a = before[scenario]?.components ?? {};
98 + const b = after[scenario]?.components ?? {};
99 + const moved = [...new Set([...Object.keys(a), ...Object.keys(b)])]
100 + .map((name) => [name, a[name] ?? 0, b[name] ?? 0] as const)
101 + .filter(([, x, y]) => x !== y)
102 + .sort(([n, x, y], [m, p, q]) => Math.abs(q - p) - Math.abs(y - x) || n.localeCompare(m))
103 + .slice(0, top);
104 + if (moved.length === 0) continue;
105 + lines.push("", `<details><summary>${scenario}: components that changed most</summary>`, "");
106 + lines.push("| component | renders |", "| :-- | --: |");
107 + for (const [name, x, y] of moved) lines.push(`| \`${name}\` | ${change(x, y)} |`);
108 + lines.push("", "</details>");
109 + }
110 + return lines.join("\n");
111 +}
112 +
113 +function option(args: ReadonlyArray<string>, name: string): string | undefined {
114 + const index = args.indexOf(name);
115 + return index === -1 ? undefined : args[index + 1];
116 +}
117 +
118 +if (import.meta.main) {
119 + const [command, ...args] = process.argv.slice(2);
120 + if (command === "run") {
121 + const runs = Number(option(args, "--runs") ?? 3);
122 + const results = Array.from({ length: runs }, runOnce);
123 + const unstable = nondeterministic(results);
124 + if (unstable.length) {
125 + for (const scenario of unstable) {
126 + const counts = results.map((run) => `${run[scenario]?.commits}/${run[scenario]?.renders}`);
127 + console.error(`${scenario} differed between runs (commits/renders): ${counts.join(", ")}`);
128 + const first = results[0]?.[scenario]?.components ?? {};
129 + for (const [index, run] of results.entries()) {
130 + const other = run[scenario]?.components ?? {};
131 + const names = [...new Set([...Object.keys(first), ...Object.keys(other)])];
132 + const moved = names.filter((name) => first[name] !== other[name]);
133 + if (moved.length) {
134 + const detail = moved.map((name) => `${name} ${first[name] ?? 0}→${other[name] ?? 0}`);
135 + console.error(` run ${index + 1} vs run 1: ${detail.join(", ")}`);
136 + }
137 + }
138 + }
139 + process.exitCode = 1;
140 + } else {
141 + const json = JSON.stringify(results[0], null, 2);
142 + const out = option(args, "--out");
143 + if (out) NodeFS.writeFileSync(out, json + "\n");
144 + else console.log(json);
145 + console.error(`${runs} run(s) produced identical counts.`);
146 + }
147 + } else if (command === "compare" && args[0] && args[1]) {
148 + const read = (path: string) => JSON.parse(NodeFS.readFileSync(path, "utf8")) as RenderMetrics;
149 + console.log(
150 + renderComparison(read(args[0]), read(args[1]), Number(option(args, "--top") ?? 10)),
151 + );
152 + } else {
153 + console.log(HELP);
154 + if (command !== "--help" && command !== "-h" && command !== undefined) process.exitCode = 1;
155 + }
156 +}