Skip to content

Skill catalog resolution rereads and rehashes every file across all skill trees on every turn #5963

Closed

Description

@Tiberriver256

Summary

Every thread turn start builds runtime config and calls resolveSkillCatalog, which scans dataDir/skills, generated skills, and all plugin skill roots. For every accepted skill candidate, readSkillTreeManifest reads every file into memory synchronously via collectSkillTreeEntries, hashes the content to compute treeHash, and discards the read buffers. A second resolve on unchanged trees repeats all reads and hashes unconditionally without a freshness cache.

Versions and environment

  • Node v26.12.0. From source at 405592c0b71b1655a6ab2e1cc7498b5beeb78caa.
  • Linux x86_64, 4 cores.
  • Reproduction runs directly against repository sources with node --conditions=source --import tsx.

Steps to reproduce

Run this standalone reproduction script from the root of a BB checkout:

node --conditions=source --import tsx - <<'REP'
import fs from "node:fs";
import path from "node:path";
import { performance } from "node:perf_hooks";

const realReadFileSync = fs.readFileSync;
let reads = 0;
let bytes = 0;
fs.readFileSync = function (...args) {
  const res = realReadFileSync.apply(this, args);
  reads += 1;
  bytes += Buffer.isBuffer(res) ? res.byteLength : Buffer.byteLength(String(res));
  return res;
};

const { resolveSkillCatalogEntries, SkillTreeRegistry } = await import(
  "./apps/server/src/services/skills/injected-skills.ts"
);

const repoPlugins = path.resolve("./plugins");
const pluginSkillRoots = fs
  .readdirSync(repoPlugins, { withFileTypes: true })
  .filter((entry) => entry.isDirectory())
  .map((entry) => ({
    pluginId: entry.name,
    rootPath: path.join(repoPlugins, entry.name, "skills"),
  }))
  .filter((entry) => fs.existsSync(entry.rootPath));

const logger = { warn() {}, debug() {}, info() {}, error() {}, fatal() {}, trace() {}, child() { return logger; } };

function scan() {
  reads = 0;
  bytes = 0;
  const start = performance.now();
  const entries = resolveSkillCatalogEntries(logger, {
    additionalSkillsRootPaths: [`${process.env.HOME}/.bb/skills-generated`],
    dataDir: `${process.env.HOME}/.bb`,
    pluginSkillRoots,
    skillTreeRegistry: new SkillTreeRegistry(),
  });
  return { durationMs: performance.now() - start, entries: entries.length, reads, bytes };
}

const first = scan();
const second = scan();

console.log(JSON.stringify({
  firstCallMs: Number(first.durationMs.toFixed(2)),
  firstReads: first.reads,
  firstBytes: first.bytes,
  secondCallMs: Number(second.durationMs.toFixed(2)),
  secondReads: second.reads,
  secondBytes: second.bytes,
  identicalEntryCount: first.entries === second.entries,
}, null, 2));
REP

Expected vs actual

Expected:
When skill files and directory listings have not changed, resolveSkillCatalog reuses cached tree hashes without re-reading file contents from disk.

Actual output:

{
  "firstCallMs": 44.25,
  "firstReads": 287,
  "firstBytes": 1450161,
  "secondCallMs": 19.72,
  "secondReads": 287,
  "secondBytes": 1450161,
  "identicalEntryCount": true
}

The second resolve re-executes 287 readFileSync calls, re-reads 1,450,161 bytes into temporary Buffer objects, and recomputes all SHA-256 tree hashes synchronously on the server main thread.

Evidence

Source comparison

Measured cost

  • Benchmark: node --conditions=source --import tsx /home/tiberriver256/bb-perf-audit/bench/46/repro.mjs
  • Test machine: Linux, 4 cores, load average 0.31, 0.26, 0.17.
  • Catalog workload: resolveSkillCatalogEntries over ~/.bb/skills, skills-generated, and 28 repo plugin skill roots.
  • Timings: Warmup 67.640 ms (excluded from median). Five warm runs recorded median wall time 19.348 ms (range 16.299 ms to 20.338 ms).
  • Operation counts per warm run: 85 returned entries (identical names), 287 readFileSync calls, 1,450,161 bytes read, 344 lstatSync calls, 0 errors.
  • Unprofiled CPU split: User CPU ~13 to 24 ms, system CPU ~4 to 10 ms (warm cost is primarily CPU once page cache is warm).
  • Separate readSkillTreeManifest loop on 86 directories (hash-only.mjs, finished 2026-10-21T03:22:50Z): 5 warm runs, median 11.945 ms (range 10.480 to 17.450 ms), 201 files, 1,122,218 bytes, 0 errors.

Suggested fix and verification

  • Cache tree hashes keyed by skill root path, file set, and file mtimes.
  • Subsequent calls on unchanged trees reuse the cached hash and perform zero readFileSync calls.
  • A modified file, missing directory, or failed hash rebuilds the manifest entry.
  • Suggested test: Run resolveSkillCatalogEntries twice on a temporary skill directory fixture. Assert the second call returns identical hashes with 0 content readFileSync calls. Touch a file in the fixture and verify the subsequent call recalculates the updated tree hash.

What you ruled out

Suggested priority and effort

Medium priority, small effort. It introduces ~19 ms of synchronous disk I/O on the server main thread during every turn start, scaling with the number of installed plugins and skills.

AGENT GENERATED
Investigation thread: @thread:thr_j4ihwv2cuc (parent: @thread:thr_dzt4erif6e)

Activity

  1. added theissue type on Oct 21, 2026
  2. added
    threads
    confirmed-repro
    on Oct 21, 2026
  3. bb-slop-cop commented on Oct 21, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority Medium · Effort Low. Applied labels: threads, perf, confirmed-repro.
    • Report: https://get-bb.github.io/reports/issues/5963.html — REPRODUCED, high root-cause confidence.
    • Root cause: Catalog discovery retains hash-to-root mappings but no freshness cache, so unchanged candidates are reparsed and their entire trees synchronously reread and hashed.
    • Reproduction: An isolated two-file fixture makes 3 content reads on its unchanged second resolution. The zero-read regression fails on trusted main in two clean checkouts; it passes after the fix.
    • Fix: Cache unchanged skill catalog candidates #5966 — ready for review, not merged. 225 changed text lines across 2 files; 168 targeted tests, server typechecking, formatting, and diff checks pass.

    AGENT GENERATED

  4. added a commit that references this issue on Oct 29, 2026
    9ad1636
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-reproperfthreads

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions