Skip to content

Serve small host files with one daemon round trip - #8117

Merged
ymichael merged 2 commits into
mainfrom
bb/small-host-files-one-round-trip-thr_ayhvwekcsv
Nov 24, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/small-host-files-one-round-trip-thr_ayhvwekcsv

Conversation

@ymichael

@ymichael ymichael commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

serveDaemonFileStream (apps/server/src/services/hosts/daemon-file-stream.ts) always sent a host.read_file_chunk with length: 0 to get metadata, then createFileStreamResponse read the first chunk with a second RPC. Every un-ref'd raw host file request therefore cost two serial daemon round trips: image previews, the file viewer's 64 KiB sample, thread-storage files and project/environment files. Only a 304 took one. This is finding #7 of the server→daemon RPC audit. I checked the call pattern in the code and on a running server before changing anything: main makes 2 RPCs per small file.

What changed

  • Server (daemon-file-stream.ts): the first read is offset 0, length 64 KiB. The daemon returns the metadata together with min(64 KiB, size) bytes, and the stream source uses those bytes as its first chunk when the response starts at byte 0. A file up to 64 KiB, or a preview sample of any file, now takes one RPC. Files over 64 KiB save one RPC but still take two waves (first chunk, then the rest in parallel).
    • The first read stays metadata-only (length: 0) for HEAD, If-None-Match: *, and ranges that don't start at byte 0 (video seeks, suffix ranges), so those requests don't ship 64 KiB that would be thrown away.
    • The prefetched bytes are used only when they cover the stream's first read exactly. Any mismatch falls back to a normal chunk read, so the choice of first read can cost an extra RPC but can't change the response.
  • Wire change, HOST_DAEMON_PROTOCOL_VERSION 281 → 282: host.read_file_chunk gains a required knownRevisions: revision[] (max 16). The server fills it from the request's If-None-Match tags (W/"file-<revision>"). When the file's current revision is in the list, the daemon returns the metadata with no bytes, and the server answers 304 from that same read.
    • Without this field, every image revalidation would ship up to 64 KiB for nothing. The alternative, probing with length: 0 whenever If-None-Match is present, would bring back the second RPC for files that changed.
    • Chunk reads after the first send [].
  • Daemon (file-read.ts): reads 0 bytes when the revision is known. The revision check and the read are otherwise unchanged.
  • Docs: updated the streaming paragraph in docs/debugging-and-qa.md.

How you verified

Real running servers, main vs this branch, same inputs. I ran pnpm start:worktree (its own dev data dir, not ~/.bb) on main, then rebuilt and restarted it on this branch. There were two hosts:

  • the local primary daemon;
  • a second dev daemon enrolled via POST /hosts/join-codes, whose BB_SERVER_URL pointed at a TCP proxy that delays each direction by 100 ms (~200 ms RTT).

I counted host RPCs on the remote daemon with an --import preload that logs every fs.open of the test directory. Each host.read_file_chunk opens the file exactly once. Each cell below is the median of 8 requests to GET /api/v1/hosts/:id/files/<path>.

Remote host (200 ms RTT):

request file main RPCs branch RPCs main ms branch ms
GET 2.7 KB txt 2 1 411 208
GET 12 KB png 2 1 411 206
GET 48 KB png 2 1 411 206
GET 300 KB jpg 6 5 412 412
file-viewer sample Range: bytes=0-65535 12 KB png 2 1 410 207
file-viewer sample Range: bytes=0-65535 300 KB jpg 2 1 410 208
revalidation, stale ETag (file changed) → 200 48 KB png 2 1 410 206
revalidation, matching ETag → 304 all 1 1 ~206 ~206
HEAD all 1 1 ~206 ~208
seek Range: bytes=100000- 300 KB jpg 5 5 414 421
  • Per action: opening an image in the file viewer is the 64 KiB sample request plus the image GET (loadFilePreview in apps/app/src/lib/api.ts). For a ≤64 KiB image that drops from 4 RPCs / ~820 ms to 2 RPCs / ~410 ms on the remote host. Each image rendered from the file URL saves ~200 ms.
  • Local host: every request is 2–5 ms on both builds, with no measurable change.
  • Correctness on both hosts: full and ranged bodies are byte-identical to the files on disk (sha1 compared). A file appended between requests revalidates to a full 200 with the new bytes in one RPC.

Tests:

  • New route test in apps/server/test/files/file-content-routes.test.ts. It asserts the daemon command sequence (offset, length, knownRevisions) for GET, the preview Range, matching and stale If-None-Match, HEAD, a multi-chunk file (no duplicate offset-0 read), and a seek. It fails on main, which sends a length: 0 probe and then re-reads offset 0.
  • New daemon test in host-files.test.ts: the first chunk comes back with metadata, and a known revision returns no bytes.
  • Ran locally: turbo run typecheck --filter=@bb/server --filter=@bb/host-daemon. Vitest for file-content-routes, host-file-routes, daemon-file-stream, file-stream-response (77 passed), and host-files plus server-connection in the daemon (60 passed). packages/host-daemon-contract/test/contract.test.ts now pins protocol 282 and rejects a missing, malformed or over-long knownRevisions (56 passed). CI runs the full suites.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 24, 2026 12:10
The raw file stream made a metadata-only host.read_file_chunk probe and then
read the first chunk, so every host file preview cost two serial RPCs. The
first read now returns the metadata with the first 64 KiB, and the stream uses
those bytes. It also carries the If-None-Match revisions as knownRevisions, so
an unchanged file returns no bytes and answers 304 from the same read.

Bumps HOST_DAEMON_PROTOCOL_VERSION to 282 for the new required field.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit bdfbd3d into main Nov 24, 2026
53 checks passed
@ymichael
ymichael deleted the bb/small-host-files-one-round-trip-thr_ayhvwekcsv branch November 24, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant