From d883f3e19c1920ae1f178d7e391a7e6056be2e56 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Sun, 22 Nov 2026 22:08:21 -0800 Subject: [PATCH 1/3] feat(scripts): server metrics time the turn pipeline and opening a large thread Co-Authored-By: Claude Opus 5.5 --- scripts/server-metrics-report.ts | 25 ++++++ scripts/server-metrics.ts | 134 +++++++++++++++++++++++++++++-- 2 files changed, 154 insertions(+), 5 deletions(-) diff --git a/scripts/server-metrics-report.ts b/scripts/server-metrics-report.ts index 6e03db8ff84a3..e4703c6c7125c 100644 --- a/scripts/server-metrics-report.ts +++ b/scripts/server-metrics-report.ts @@ -62,6 +62,20 @@ export const Measured = Schema.Struct({ }), }), ), + /** 50 short turns back to back on one thread. */ + pipeline: Schema.optional( + Schema.Struct({ + /** Send to the agent's API request. */ + toRequestMs: Spread, + /** The reply's last byte to the CLI seeing a finished run: turn end and checkpoint. */ + turnEndMs: Spread, + /** Wall time per turn, the CLI's own start included. */ + msPerTurn: Schema.Number, + loopDelayP99Ms: Schema.Number, + loopDelayMaxMs: Schema.Number, + gcPauseMs: Schema.Number, + }), + ), /** A server on a copy of the developer's real database, its projects backed by clones. */ busy: Schema.optional( Schema.Struct({ @@ -72,6 +86,16 @@ export const Measured = Schema.Struct({ rest: Window, shellMs: Spread, shellKB: Schema.Number, + /** The seeded thread with the most messages, opened through its bounded HTTP snapshot. */ + largeThread: Schema.optional( + Schema.Struct({ + messages: Schema.Number, + openMs: Spread, + KB: Schema.Number, + /** Heap after GC once it was opened, less before. */ + heapMB: Schema.Number, + }), + ), /** Back-to-back turns in a clone, each ending in a checkpoint. */ git: Window, }), @@ -247,6 +271,7 @@ export function compareMetrics(before: ServerMetrics, after: ServerMetrics, thre metric === "busy.projects" || metric === "busy.ballastMB" || metric === "clients.storm.clients" || + metric === "busy.largeThread.messages" || metric.startsWith("scaling.points") ) { continue; diff --git a/scripts/server-metrics.ts b/scripts/server-metrics.ts index 4f0b67b66c83e..9785e8392db57 100644 --- a/scripts/server-metrics.ts +++ b/scripts/server-metrics.ts @@ -78,6 +78,8 @@ import { const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)); const MODEL = "claude-haiku-4-5"; const SLOW_MARKER = "[[stream-slowly]]"; +/** Sequential turns in the pipeline scenario. */ +const PIPELINE_TURNS = 50; /** Clients that drop and reconnect at once in the clients scenario. */ const STORM_CLIENTS = 20; /** Event-loop delay sampling interval; each sample wakes the loop once. */ @@ -120,6 +122,8 @@ interface ApiRequest { readonly agent: boolean; readonly stream: boolean; readonly marker: string | undefined; + /** When the reply's last event was produced; streamed replies only. */ + endedAt?: number; } const MessagesRequest = Schema.Struct({ @@ -147,13 +151,15 @@ const serveFakeAnthropic = Effect.fn("serverMetrics.serveFakeAnthropic")(functio Effect.orElseSucceed((): typeof MessagesRequest.Type => ({})), ); const streamed = body.stream === true; - requests.push({ + const entry: ApiRequest = { at: yield* nowMs, stream: streamed, // Title and branch-name jobs carry only their structured-output tool; the agent carries many. agent: streamed && (body.tools?.length ?? 0) > 1, - marker: /bench-run-(\d+)/.exec(raw)?.[0], - }); + // A follow-up carries the thread's earlier prompts too, so the newest marker is the turn's. + marker: [...raw.matchAll(/bench-run-\d+/g)].at(-1)?.[0], + }; + requests.push(entry); const model = body.model ?? MODEL; const id = `msg_bench_${requests.length}`; if (!streamed) { @@ -212,7 +218,14 @@ const serveFakeAnthropic = Effect.fn("serverMetrics.serveFakeAnthropic")(functio ), Stream.encodeText, ); - return HttpServerResponse.stream(events, { + const recorded = events.pipe( + Stream.ensuring( + Effect.map(nowMs, (now) => { + entry.endedAt = now; + }), + ), + ); + return HttpServerResponse.stream(recorded, { contentType: "text/event-stream", headers: { "cache-control": "no-cache" }, }); @@ -642,7 +655,14 @@ const seedBaseDir = Effect.fn("serverMetrics.seedBaseDir")(function* ( } const [runs] = yield* sql<{ n: number }>` SELECT COUNT(*) AS n FROM orchestration_v2_projection_runs`; - return { roots, runs: Number(runs?.n ?? 0) }; + const [largest] = yield* sql<{ thread_id: string; messages: number }>` + SELECT thread_id, COUNT(*) AS messages FROM orchestration_v2_projection_messages + GROUP BY thread_id ORDER BY messages DESC LIMIT 1`; + return { + roots, + runs: Number(runs?.n ?? 0), + largest: largest && { threadId: largest.thread_id, messages: Number(largest.messages) }, + }; }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: database }))); }); @@ -720,6 +740,7 @@ const startServer = Effect.fn("serverMetrics.startServer")(function* ( ? undefined : yield* seedBaseDir(options.seed, baseDir, directory, environment.checkout); const roots = seeded?.roots ?? []; + const largest = seeded?.largest; yield* fs.writeFileString( path.join(baseDir, "userdata/settings.json"), encodeJson({ textGenerationModelSelection: { instanceId: "claudeAgent", model: MODEL } }), @@ -912,6 +933,8 @@ const startServer = Effect.fn("serverMetrics.startServer")(function* ( }); } }), + /** The seeded thread with the most messages. */ + largest, measure, profile: devtools.profile, snapshot: devtools.snapshot, @@ -1026,6 +1049,40 @@ const loadShell = Effect.fn("serverMetrics.loadShell")(function* (server: Server return { ms: spread(elapsed), KB: Math.round(bytes / 1_000) }; }); +/** The bounded HTTP snapshot a client loads to open a thread, timed to its last byte. */ +const loadThread = Effect.fn("serverMetrics.loadThread")(function* ( + server: Server, + threadId: string, + times: number, +) { + const http = yield* HttpClient.HttpClient; + const token = yield* server.issueToken; + const request = HttpClientRequest.get( + `${server.origin}/api/orchestration/threads/${encodeURIComponent(threadId)}/bounded?compactTurnItems=1&dedupeBodies=1&compactIds=1&deferWork=1`, + ).pipe( + HttpClientRequest.bearerToken(token), + HttpClientRequest.setHeader(ORCHESTRATION_PROTOCOL_HEADER, ORCHESTRATION_PROTOCOL_VERSION_TEXT), + ); + const before = yield* server.heapAfterGc; + const elapsed: number[] = []; + let bytes = 0; + for (let index = 0; index < times; index++) { + const started = yield* nowMs; + const response = yield* http.execute(request); + const body = yield* response.arrayBuffer; + if (response.status !== 200) { + return yield* new MetricsError({ message: `thread snapshot answered ${response.status}` }); + } + elapsed.push((yield* nowMs) - started); + bytes = body.byteLength; + } + return { + ms: spread(elapsed), + KB: Math.round(bytes / 1_000), + heapMB: round(((yield* server.heapAfterGc) - before) / 1e6), + }; +}); + /** Short turns back to back in a clone, each ending in a checkpoint of the whole tree. */ const gitTraffic = Effect.fn("serverMetrics.gitTraffic")(function* (server: Server) { const fs = yield* FileSystem.FileSystem; @@ -1197,6 +1254,53 @@ const attachClient = Effect.fn("serverMetrics.attachClient")(function* ( }; }); +/** + * Short turns back to back on one thread: how long a send takes to reach the API, how long the + * end of the reply takes to become a finished run the CLI sees (turn end and checkpoint), the time + * per turn, and the event-loop delay across all of them. + */ +const pipelineTurns = Effect.fn("serverMetrics.pipelineTurns")(function* ( + environment: Environment, + server: Server, + turns: number, +) { + const [threadId] = yield* openThreads(server, 0, 1); + if (threadId === undefined) return yield* new MetricsError({ message: "no pipeline thread" }); + const toRequest: number[] = []; + const turnEnd: number[] = []; + yield* server.loopStart; + const first = yield* nowMs; + for (let turn = 0; turn < turns; turn++) { + const marker = `bench-run-${20_000 + turn}`; + const started = yield* nowMs; + const sent = yield* server.threadSend(threadId, `Short reply. ${marker}`); + const done = yield* nowMs; + if (sent.exitCode !== 0) { + return yield* new MetricsError({ + message: `pipeline turn ${turn} failed: ${sent.stderr.trim() || sent.stdout.trim()}`, + }); + } + const request = environment.requests.find( + (entry) => entry.agent && entry.marker === marker && entry.at >= started, + ); + if (request?.endedAt === undefined) { + return yield* new MetricsError({ message: `the agent request for ${marker} never ended` }); + } + toRequest.push(request.at - started); + turnEnd.push(done - request.endedAt); + } + const elapsed = (yield* nowMs) - first; + const loop = yield* server.loopRead; + return { + toRequestMs: spread(toRequest), + turnEndMs: spread(turnEnd), + msPerTurn: Math.round(elapsed / turns), + loopDelayP99Ms: round(loop.p99), + loopDelayMaxMs: round(loop.max), + gcPauseMs: round(loop.gcMs), + }; +}); + const SCENARIOS = [ "startup", "rest", @@ -1204,6 +1308,7 @@ const SCENARIOS = [ "quiet", "streaming", "clients", + "pipeline", "busy", ] as const; /** Scenarios that read the developer's real database, so they run only when named. */ @@ -1381,6 +1486,13 @@ const runOnce = Effect.fn("serverMetrics.runOnce")(function* (options: RunOption }), ); } + if (wanted("pipeline")) { + yield* withServer("pipeline", (server) => + Effect.gen(function* () { + measured.pipeline = yield* pipelineTurns(environment, server, PIPELINE_TURNS); + }), + ); + } if (wanted("streaming")) { yield* withServer("streaming", (server) => Effect.gen(function* () { @@ -1427,6 +1539,8 @@ const runOnce = Effect.fn("serverMetrics.runOnce")(function* (options: RunOption const rest = yield* server.measure(30); yield* server.assertIdle(server.startedAt); const shell = yield* loadShell(server, 5); + const largest = server.largest; + const thread = largest && (yield* loadThread(server, largest.threadId, 5)); const traffic = yield* gitTraffic(server); // The traffic only ends by failing, which then fails the window instead of thinning it. const git = yield* Effect.raceFirst( @@ -1445,6 +1559,16 @@ const runOnce = Effect.fn("serverMetrics.runOnce")(function* (options: RunOption rest, shellMs: shell.ms, shellKB: shell.KB, + ...(largest && thread + ? { + largeThread: { + messages: largest.messages, + openMs: thread.ms, + KB: thread.KB, + heapMB: thread.heapMB, + }, + } + : {}), git, }; }), From fd3d8d32f35e871adb94f37c75b552b7123e1cba Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:01:04 -0800 Subject: [PATCH 2/3] fix(scripts): server metrics stamp a reply's end before the client can see it Co-Authored-By: Claude Opus 5.5 --- scripts/server-metrics.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/scripts/server-metrics.ts b/scripts/server-metrics.ts index 9785e8392db57..8ab475f38a5ad 100644 --- a/scripts/server-metrics.ts +++ b/scripts/server-metrics.ts @@ -213,19 +213,20 @@ const serveFakeAnthropic = Effect.fn("serverMetrics.serveFakeAnthropic")(functio delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: words }, }), - event("message_stop", {}), ), ), - Stream.encodeText, - ); - const recorded = events.pipe( - Stream.ensuring( - Effect.map(nowMs, (now) => { - entry.endedAt = now; - }), + // Stamped as the last event is produced, so it is set before any client can see the end. + Stream.concat( + Stream.fromEffect( + Effect.map(nowMs, (now) => { + entry.endedAt = now; + return event("message_stop", {}); + }), + ), ), + Stream.encodeText, ); - return HttpServerResponse.stream(recorded, { + return HttpServerResponse.stream(events, { contentType: "text/event-stream", headers: { "cache-control": "no-cache" }, }); From 6d94115261a7ba3d1f49d7510742539736304a1d Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:07:08 -0800 Subject: [PATCH 3/3] fix(scripts): floor large-thread open times like the shell snapshot Co-Authored-By: Claude Opus 5.5 --- scripts/server-metrics-report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/server-metrics-report.ts b/scripts/server-metrics-report.ts index e4703c6c7125c..c11cfca1589a7 100644 --- a/scripts/server-metrics-report.ts +++ b/scripts/server-metrics-report.ts @@ -209,7 +209,7 @@ function noiseFloor(metric: string) { if (metric.endsWith("wakeupsPerSec")) return 20; if (metric.endsWith("MB")) return 15; if (metric.endsWith("KB")) return 20; - if (metric.includes(".shellMs.")) return 50; + if (metric.includes(".shellMs.") || metric.includes(".openMs.")) return 50; if (metric.endsWith("loopDelayP50Ms")) return 2; if (metric.endsWith("loopDelayP99Ms")) return 5; if (metric.endsWith("loopDelayMaxMs")) return 20;