#7943 · Sequential daemon-message validation
Trusted base: 89f1e507d62d0fcd94bd319c61ba2e9cd18f607c, fetched from get-bb/bb origin/main and checked again after reproduction.
Verdict: REPRODUCED · Root-cause confidence: high.
1. TL;DR
Routine daemon messages spend avoidable CPU time rejecting unrelated message formats before validating their own format. The exported daemon-to-server validator is an ordered union with 18 members; a terminal-output message reaches member 14, while a failed RPC also crosses a nested success/failure union. On this machine a 32-byte terminal payload took 9.57–9.71 µs per parse with the current validator versus 0.83–0.90 µs with an experimental discriminator-based validator. The normal daemon send and server receive paths both call the current validator. This is a schema-level performance reproduction, not a measurement of user-visible lag, fleet CPU consumption, or end-to-end WebSocket throughput.
2. Claims vs findings
| Claim | Finding | Evidence |
|---|---|---|
| The outgoing message validator has 18 ordered alternatives. | Verified | The source and runtime options.length both give 18. |
| Late alternatives do unnecessary validation work. | Verified | A proxy records 14 reads of the terminal message's type with the production union and 2 with the discriminator comparator; failed RPCs record 19 versus 2. |
| The daemon and server both validate these messages. | Verified by source inspection | sendMessage calls parse; onDaemonSocketMessage passes the schema to parseSocketValue, which calls safeParse. No live production instance was accessed. |
| Small terminal messages and failed RPCs have a large relative parsing penalty. | Verified | Both clean runs show roughly 10× for the small terminal case and more than 20× for the failed-RPC case. |
| The issue's exact benchmark numbers apply universally. | Not asserted | This environment uses Node 22 rather than Node 24. Timings are local best-of-five means, not portable thresholds. |
| Large payloads remain expensive after dispatch is optimized. | Verified; attribution partly inferred | 64 KiB takes about 222 µs even with discriminator dispatch. The shared base64 validator includes length, regular-expression, and decoded-length checks; this report does not profile their individual costs. |
| The candidate optimization still rejects unknown message types. | Verified, with a diagnostic caveat | Both reject unknown types, but the top issue path changes from [] to ["type"]. The malformed known-type fixture retains its code/path diagnostics. Full diagnostic equivalence must not be assumed. |
3. Environment
- Public repository:
get-bb/bb; trusted commit89f1e507d62d0fcd94bd319c61ba2e9cd18f607c. - Linux 4.19.0-gvisor; Node v22.19.0; pnpm 9.15.0; Zod 4.6.7.
- Two separately cloned, initially clean source trees at the same fetched commit, each with its own frozen dependency install and scoped Turbo build. Same agent and machine; the second run is a repeat verification, not an independent review.
- No app, daemon, provider, browser, listening port, or runtime database was started. No account or runtime data was read.
- The agent authored the reproduction from the trusted schema definitions; no issue-provided script, patch, or linked branch was executed.
4. Minimal reproduction
- Use an existing Node/pnpm installation and obtain the trusted base:
git clone https://github.com/get-bb/bb.git source cd source git checkout --detach 89f1e507d62d0fcd94bd319c61ba2e9cd18f607c pnpm install --frozen-lockfile --prefer-offline pnpm exec turbo run build --filter=@bb/host-daemon-contract...
- Save the complete agent-authored script below as
packages/host-daemon-contract/repro-7943.ts. - Run the schema-level experiment:
pnpm exec tsx packages/host-daemon-contract/repro-7943.ts
Expected: validation dispatch should avoid visiting unrelated formats. Valid outputs must remain equivalent, and malformed payloads must remain rejected. Actual: the production schema repeatedly reads the discriminator while traversing alternatives and spends substantially more time on late members. The script's assertions check sample correctness; timing and branch-read output demonstrate the performance defect without a machine-dependent failing timing threshold.
The comparator exists only in this script. It reuses the trusted member schemas, replaces the nested RPC union with ok dispatch, and then dispatches the outer union by type. It is not installed into production, and these five fixtures do not prove equivalence for every possible input.
Complete reproduction script
import assert from "node:assert/strict";
import { performance } from "node:perf_hooks";
import { z } from "zod";
import {
hostDaemonDaemonWsMessageSchema,
hostDaemonOnlineRpcResponseMessageSchema,
type HostDaemonDaemonWsMessage,
} from "./src/session.js";
const original = hostDaemonDaemonWsMessageSchema;
const dispatched = z.discriminatedUnion("type", [
...original.options.slice(0, -1),
z.discriminatedUnion("ok", hostDaemonOnlineRpcResponseMessageSchema.options),
]);
function countTypeReads(schema: z.ZodType, message: HostDaemonDaemonWsMessage) {
let reads = 0;
const observed = new Proxy(message, {
get(target, property, receiver) {
if (property === "type") reads += 1;
return Reflect.get(target, property, receiver);
},
});
schema.parse(observed);
return reads;
}
function measure(schema: z.ZodType, message: HostDaemonDaemonWsMessage, iterations: number) {
for (let warmup = 0; warmup < 1500; warmup += 1) schema.parse(message);
const samples: number[] = [];
for (let repeat = 0; repeat < 5; repeat += 1) {
const started = performance.now();
for (let iteration = 0; iteration < iterations; iteration += 1) schema.parse(message);
samples.push(((performance.now() - started) * 1000) / iterations);
}
return Number(Math.min(...samples).toFixed(3));
}
const terminalMessage = (bytes: number): HostDaemonDaemonWsMessage => ({
type: "terminal.output",
terminalId: "fixture-terminal",
chunk: { seq: 7, dataBase64: Buffer.alloc(bytes, 65).toString("base64") },
});
const samples: Array<{ name: string; message: HostDaemonDaemonWsMessage; iterations: number }> = [
{ name: "heartbeat", message: { type: "heartbeat" }, iterations: 20000 },
{ name: "terminal-32B", message: terminalMessage(32), iterations: 20000 },
{ name: "terminal-4096B", message: terminalMessage(4096), iterations: 6000 },
{ name: "terminal-65536B", message: terminalMessage(65536), iterations: 1000 },
{
name: "rpc-failure",
message: {
type: "host-rpc.response",
requestId: "fixture-request",
commandType: "host.list_files",
ok: false,
errorCode: "fixture-error",
errorMessage: "Fixture operation did not complete",
},
iterations: 20000,
},
];
console.log(JSON.stringify({ node: process.version, zod: z.core.version, options: original.options.length }));
for (const sample of samples) {
assert.deepEqual(original.parse(sample.message), dispatched.parse(sample.message));
const originalReads = countTypeReads(original, sample.message);
const dispatchedReads = countTypeReads(dispatched, sample.message);
const originalUs = measure(original, sample.message, sample.iterations);
const dispatchedUs = measure(dispatched, sample.message, sample.iterations);
console.log(JSON.stringify({ name: sample.name, originalReads, dispatchedReads, originalUs, dispatchedUs }));
}
const malformedMessages = [
{ type: "fixture-unsupported" },
{ type: "terminal.output", terminalId: "fixture-terminal", chunk: { seq: -1, dataBase64: "@@" } },
];
for (const message of malformedMessages) {
const originalResult = original.safeParse(message);
const dispatchedResult = dispatched.safeParse(message);
assert.equal(originalResult.success, false);
assert.equal(dispatchedResult.success, false);
console.log(JSON.stringify({
invalidType: message.type,
originalIssues: originalResult.error?.issues.map(({ code, path }) => ({ code, path })),
dispatchedIssues: dispatchedResult.error?.issues.map(({ code, path }) => ({ code, path })),
}));
}
console.log("PASS: valid payload outputs match; invalid payloads remain rejected.");
First run
Times are µs/parse, best of five means after warmup. Error objects below are reduced to code/path fields for readability; benchmark rows are unchanged.
{"node":"v22.19.0","zod":{"major":4,"minor":6,"patch":7},"options":18}
{"name":"heartbeat","originalReads":3,"dispatchedReads":2,"originalUs":0.649,"dispatchedUs":0.1}
{"name":"terminal-32B","originalReads":14,"dispatchedReads":2,"originalUs":9.572,"dispatchedUs":0.901}
{"name":"terminal-4096B","originalReads":14,"dispatchedReads":2,"originalUs":24.431,"dispatchedUs":14.576}
{"name":"terminal-65536B","originalReads":14,"dispatchedReads":2,"originalUs":235.656,"dispatchedUs":222.275}
{"name":"rpc-failure","originalReads":19,"dispatchedReads":2,"originalUs":13.232,"dispatchedUs":0.621}
{"invalidType":"fixture-unsupported","originalIssues":[{"code":"invalid_union","path":[]}],"dispatchedIssues":[{"code":"invalid_union","path":["type"]}]}
{"invalidType":"terminal.output","originalIssues":[{"code":"too_small","path":["chunk","seq"]},{"code":"invalid_format","path":["chunk","dataBase64"]}],"dispatchedIssues":[{"code":"too_small","path":["chunk","seq"]},{"code":"invalid_format","path":["chunk","dataBase64"]}]}
PASS: valid payload outputs match; invalid payloads remain rejected.
5. Root cause
The validator uses ordered alternatives rather than indexing the explicit message discriminator. Rejecting earlier alternatives incurs field validation and issue construction before the correct message object is reached. For RPC failures the outer union also enters a plain nested union and first rejects its success branch. The runtime read counts agree with the source member order; they are measured separately from the timing loops so proxy overhead does not contaminate timings.
export const hostDaemonDaemonWsMessageSchema = z.union([ desktopBrowserChangedSchema, hostDaemonMachineShutdownAckMessageSchema, hostDaemonHeartbeatMessageSchema, ... hostDaemonTerminalOpenedMessageSchema, hostDaemonTerminalOutputMessageSchema, ... hostDaemonOnlineRpcResponseMessageSchema, ]);
Nested RPC union, session.ts:522:
export const hostDaemonOnlineRpcResponseMessageSchema = z.union([ hostDaemonOnlineRpcResponseSuccessSchema, hostDaemonOnlineRpcResponseFailureSchema, ]);
- Daemon send boundary:
const parsed = hostDaemonDaemonWsMessageSchema.parse(message). - Server receive boundary: passes the same exported schema to
parseSocketValue. - Server parser:
const result = schema.safeParse(value). - Base64 validation remains a separate payload-size-dependent cost.
- The package entry point publicly re-exports the session schemas.
6. Proposed fix and safety decision
A contained follow-up can dispatch the outer message validator by type and its RPC success/failure validator by ok, while retaining every member's field validation. The script demonstrates the performance opportunity without dropping validation. Cover valid and malformed messages across all variants, especially RPC success command dispatch, unknown types, and strict object-key rejection.
No automatic fix PR: the repair modifies exported daemon protocol validation schemas. This automation's simple-fix policy excludes public protocol/schema changes, even when valid wire fields appear unchanged. The observed unknown-type error-path change also means diagnostic compatibility should not be assumed. No production patch, fix branch, or fix push was made.
7. Verification
A second clone, named verify, was clean before the agent-authored script was copied in. It was checked out at 89f1e507d62d0fcd94bd319c61ba2e9cd18f607c, received a separate frozen install and scoped Turbo build, and ran the same script in a separate process:
cd verify pnpm install --frozen-lockfile --prefer-offline pnpm exec turbo run build --filter=@bb/host-daemon-contract... pnpm exec tsx packages/host-daemon-contract/repro-7943.ts
{"node":"v22.19.0","zod":{"major":4,"minor":6,"patch":7},"options":18}
{"name":"heartbeat","originalReads":3,"dispatchedReads":2,"originalUs":0.683,"dispatchedUs":0.094}
{"name":"terminal-32B","originalReads":14,"dispatchedReads":2,"originalUs":9.712,"dispatchedUs":0.825}
{"name":"terminal-4096B","originalReads":14,"dispatchedReads":2,"originalUs":23.646,"dispatchedUs":14.563}
{"name":"terminal-65536B","originalReads":14,"dispatchedReads":2,"originalUs":236.246,"dispatchedUs":222.388}
{"name":"rpc-failure","originalReads":19,"dispatchedReads":2,"originalUs":13.534,"dispatchedUs":0.622}
{"invalidType":"fixture-unsupported","originalIssues":[{"code":"invalid_union","path":[]}],"dispatchedIssues":[{"code":"invalid_union","path":["type"]}]}
{"invalidType":"terminal.output","originalIssues":[{"code":"too_small","path":["chunk","seq"]},{"code":"invalid_format","path":["chunk","dataBase64"]}],"dispatchedIssues":[{"code":"too_small","path":["chunk","seq"]},{"code":"invalid_format","path":["chunk","dataBase64"]}]}
PASS: valid payload outputs match; invalid payloads remain rejected.
The second run supports the same mechanism and performance conclusion. The report explicitly narrows error-equivalence claims because unknown-type issue paths differ. No other correction was needed.
Existing package tests were also run in the first checkout with production sources unchanged:
pnpm exec turbo run test --filter=@bb/host-daemon-contract
Test Files 7 passed (7)
Tests 94 passed (94)
Tasks: 3 successful, 3 total
Both source trees were compared against the base for the inspected production files with git diff --exit-code; no differences were present. The reproduction script was the only untracked source-tree file. A final fetch still resolved origin/main to the recorded base.
8. Related issues and PRs
#7697 and open #7704 concern successful RPC results being validated again. They do not constitute a linked fix for this issue's per-message union-dispatch cost. Only GitHub metadata was read; no linked PR branch, code, or tests were executed. The issue timeline and open-PR searches returned no PR linking to #7943 at investigation time.
9. Appendix
Read-only investigation used GitHub issue properties, comments, timeline references and open-PR metadata; source inspection covered the package entry point, session schemas, base64 validator, daemon sender, server receiver and socket parser. Both clones fetched or checked out the recorded trusted main commit. Dependency installs and builds used the repository's frozen lockfile and Turbo dependency ordering. Install warnings about not-yet-built workspace CLI bins were non-fatal. The package test task emitted a non-fatal Turbo warning about missing declared output files.
Raw logs, the saved script and checkout identities remain in local investigation storage. The complete repeatable script and relevant observed output are embedded here rather than publishing raw logs or script directories, as required by the reports repository. This is a nonvisual validation-path issue, so there are no screenshots.
Untrusted-data handling: issue content was treated only as claims to test. Its embedded execution and implementation suggestions were not followed; the experiment and safety decision come from trusted source evidence and the automation rule.