Skip to content

Reduce per-turn overhead with batched workspace reads and skill reuse - #5257

Merged
ymichael merged 4 commits into
mainfrom
bb/per-turn-overhead-v1-thr_bh24rimmh7
Oct 9, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/per-turn-overhead-v1-thr_bh24rimmh7

Conversation

@ymichael

@ymichael ymichael commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Every send on a warm thread repeated workspace-context RPCs, skill-file reads and hashing, and skill-store cleanup before the provider could start. This added avoidable latency, especially when the execution host was remote.

What changed

  • Add optional turn traces across server preparation, daemon submission, and first-output delivery, gated by the existing performance-diagnostics startup permission and experiment. Document the trace fields and limitations in the debugging guide.
  • Add host.read_workspace_agent_context { rootPath, sharedSkillRoots } to gather workspace instructions, project skill files, and shared skill discovery in one daemon round trip. The server retains instruction assembly and skill-selection policy. This reduces requests, not file-content payloads: workspace instructions and project SKILL.md contents still return to the server every turn.
  • Memoize immutable, content-addressed skill trees and file hashes in the daemon. Mutable host-local trees are freshly read each turn: file metadata alone cannot reliably detect rapid edits on Linux. The bounded, process-local cache is shared across threads. Schedule skill-store garbage collection without awaiting it on the turn path and coalesce pending cleanup requests.
  • Advance HOST_DAEMON_PROTOCOL_VERSION from 230 to 232 for the trace fields and new RPC. Bump the plugin SDK package to 0.6.34 because its generated declarations include the updated daemon contract. No database or thread/session lifecycle changes.

Workspace context remains freshly read each turn. Watcher-based caching was dropped because delayed change events could let a queued follow-up miss a file edit. Resume-context deduplication was also dropped because measurements showed little benefit. Oversized project SKILL.md files above the old 25 MiB transport cap now receive the same skip-and-warn treatment as other files over the 10 MiB project skill limit, instead of failing the send.

How you verified

  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/host-daemon --filter=@bb/host-daemon-contract passed (Turbo cache hits).
  • pnpm exec turbo run test --filter=@bb/server --filter=@bb/host-daemon --filter=@bb/host-daemon-contract -- <changed test paths> passed: 149 server tests, 112 daemon tests (2 skipped), and 49 contract tests across 18 files. Coverage includes trace gating/joining, workspace discovery, skill reuse, in-place edits with unchanged size and mtime, dispatch, and runtime configuration.
  • CI follow-up: the existing in-place-edit test exposed stale metadata-cache reuse on Linux. Removed that unsafe cache without weakening the test. Focused skill staging/install tests passed locally: 27 passed, 2 skipped. The npm publication guard passes with SDK 0.6.34.
  • git diff --check origin/main...HEAD passed.

Recorded implementation benchmarks used an instant echo provider, six project skills, workspace instructions, and 20 warm follow-ups per host. The remote daemon ran on an Intel Mac over a tailnet SSH reverse tunnel. These measure BB overhead; network jitter affects remote comparisons. Benchmarks were recorded during implementation and were not rerun while preparing this PR. These figures predate the CI correction that removed metadata-based caching for mutable host files, so they do not establish performance for the final revision. New-thread startup was not measured.

Median measurement Before After
Pre-submit RPC count 8 1
Local skill staging 19.5 ms 0.9 ms
Remote skill staging 53.2 ms 5.3 ms
Local send to first output notified 42.7 ms 22.6 ms
Remote send to first output notified 231.2 ms 107.7 ms

AGENT GENERATED

qa and others added 4 commits October 8, 2026 23:18
Adds an opt-in turn trace, recorded only while performance diagnostics are
permitted and the experiment is on. The server marks spans along the send
path, times every daemon RPC the send awaits, and joins the daemon's own
turn.submit spans (returned in the turn.submit result) with the first
output batch at event ingest. One `Turn trace` log line per traced send.

Bumps HOST_DAEMON_PROTOCOL_VERSION to 231 for the turn.submit result field.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds host.read_workspace_agent_context, which returns .bb/AGENTS.md, every
.bb/skills/<dir>/SKILL.md, and the shared skills in one reply. The server's
runtime configuration and the project command typeahead use it instead of
host.list_files followed by batched host.read_file and host.list_skills,
and keep assembling instructions and the skill catalog as before.

Discovery matches what host.list_files returned: direct, non-hidden,
non-symlink skill directories, read with the same workspace containment.
SKILL.md files over 10 MB are reported as oversized and skipped with the
existing warning, including files over the 25 MB read cap that previously
failed the send.

Bumps HOST_DAEMON_PROTOCOL_VERSION to 232.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
stageInjectedSkillSources re-read and re-hashed every skill tree and ran a
full skill-store GC once per tree source on every turn. Collected trees are
now remembered per source: tree sources by their content-addressed tree hash,
host and workspace path sources by an lstat fingerprint of the skill
directory (path, mode, size, inode, mtime, ctime), so an unchanged skill is
neither read nor hashed again and an edited one is restaged on the next turn.
File digests are computed once at collection.

Store GC runs once per staging call that pulled from the store (and once per
global skill install), in the background on the store's serial lane, instead
of after every tree inside the turn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 9f7c11e into main Oct 9, 2026
48 checks passed
@ymichael
ymichael deleted the bb/per-turn-overhead-v1-thr_bh24rimmh7 branch October 9, 2026 17:47
ymichael added a commit that referenced this pull request Oct 9, 2026
## Human comments

## What was wrong

The combined workspace-context reader introduced in #5257 could read up
to 1,000 project skill files concurrently without an aggregate content
budget. It also lost excluded-directory filtering and exact-case
filename matching on case-insensitive filesystems, and checked
containment only when reading individual files. Command lookup
unnecessarily read `.bb/AGENTS.md`, allowing an invalid instructions
file to break the command catalog.

## What changed

- Keep the combined RPC and supply read policy from the server: 1,000
directories, 10 MiB per file, and **32 MiB total JSON-encoded
project-skill content**. The aggregate allowance gives several large
skills room while bounding retained content; it is a pragmatic starting
policy, not a benchmark-derived optimum. Metadata, shared skill
discovery, and workspace instructions are outside this content budget,
so it is not a whole-response cap.
- Read skill files sequentially into bounded buffers, account for JSON
escaping, and skip over-budget content with server warnings. Smaller
later files can still fit.
- Restore excluded-directory filtering and exact-case `SKILL.md`
matching. Check root containment before enumeration and file containment
before reading; retain symlink skips.
- Make instruction reads explicit: command lookup skips them, while turn
preparation still reads instructions and reports errors.
- Bump `HOST_DAEMON_PROTOCOL_VERSION` from 232 to 233 for required
request fields and the budget-exceeded result variant; bump SDK
declarations to 0.6.35. Document policy and budget scope in the
debugging guide.

## How you verified

- Four regression tests failed against the merged implementation for the
intended reasons before the fixes: discovery filtering/case,
encoded-content budgeting, escaping empty roots, and unnecessary
instruction reads.
- Focused Turbo tests passed: 8 daemon tests, 46 server tests, and 49
contract tests. Coverage includes encoded-size accounting, admitting
smaller later skills, oversized files, symlinks, count limits, command
lookup, and runtime configuration.
- `pnpm exec turbo run test --filter=@bb/server --filter=@bb/host-daemon
--filter=@bb/host-daemon-contract --
src/command-handlers/workspace-agent-context.test.ts
test/public/public-project-commands.test.ts
test/threads/thread-runtime-config.test.ts test/contract.test.ts`
- `pnpm exec turbo run typecheck --filter=@bb/server
--filter=@bb/host-daemon --filter=@bb/host-daemon-contract`
- Formatting and `git diff --check` passed. No live UI or performance
benchmark was run for this follow-up.

> AGENT GENERATED

Co-authored-by: qa <qa@x>
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