Skip to content

Timeline response cache admits nothing above 200 rows, so identical requests each rebuild the projection #3951

Closed

Description

@luketraas

Summary

The server's thread-timeline response cache skips any response with more than 200 top-level rows. Once a requested page of a long thread passes that size, two requests with an identical cache key (same revision and same request parameters — for example a second desktop window, or a plugin calling threads.timeline with the app's parameters) each run their own full projection instead of sharing one. I expected identical requests to share one build regardless of response size.

Versions and environment

  • bb 0.43.3 desktop app, macOS 26.5.2, Apple M5. Source read and repro run at c1a64f4.
  • Node v22.22.3 for the standalone repro.
  • Providers in the affected threads: Claude Code 2.1.278, codex-cli 0.155.1.
  • Setup: 2 desktop windows on one server, 561 threads, 2–10 running threads, 17 path-installed plugins.

Steps to reproduce

  1. Check out get-bb/bb at c1a64f4.
  2. Save this as repro-cache.ts in the checkout root:
    import { createThreadTimelineCache } from "./apps/server/src/services/threads/timeline-cache.ts";
    for (const n of [200, 201]) {
      const cache = createThreadTimelineCache();
      let builds = 0;
      const build = () => { builds += 1; return { rows: Array.from({ length: n }, (_, i) => ({ id: String(i) })) } as never; };
      cache.getOrBuild("thr_a", "same-key", build);
      cache.getOrBuild("thr_a", "same-key", build); // second client, identical key
      console.log(`rows=${n} builds=${builds} cacheSize=${cache.size}`);
    }
  3. Run node --experimental-strip-types repro-cache.ts from the checkout root.

Expected vs actual

Actual:   rows=200 builds=1 cacheSize=1
          rows=201 builds=2 cacheSize=0
Expected: rows=201 builds=1 cacheSize=1

Evidence

  • Admission rule:
    if (value.rows.length <= maxCacheableRows) {
    with the constant at #L6 (DEFAULT_MAX_CACHEABLE_ROWS = 200).
  • The key is maxSeq plus thread, status, environment, provider display name, page (kind, segment limit, cursor), nested rows, summary-only, diagnostic operations and completed-turn display:
    export function buildThreadTimelineParamsKey(
    args: Omit<ThreadTimelineCacheKeyArgs, "maxSeq">,
    ): string {
    return [
    args.threadId,
    args.status,
    args.environmentId ?? "-",
    args.providerDisplayName ?? "-",
    pageKeyPart(args.page),
    args.includeNestedRows ? "1" : "0",
    args.summaryOnly ? "1" : "0",
    args.includeDiagnosticOperations ? "1" : "0",
    args.completedTurnDisplay,
    ].join("|");
    }
    export function buildThreadTimelineCacheKey(
    args: ThreadTimelineCacheKeyArgs,
    ): string {
    return `${args.maxSeq}|${buildThreadTimelineParamsKey(args)}`;
    }
  • Context, not proof of the hit rate: on the live server, responses above the cutoff do occur for pageKind: "latest". From ~/.bb/logs/server.8.log, trimmed:
    {"threadId":"thr_5tpdgiiyp4","totalDurationMs":189.2,"thresholdMs":150,"selectionStrategy":"full","pageKind":"latest","eventRowCount":1320,"eventDataBytes":688143,"projectedRowCount":261,"responseRowCount":261,"msg":"Thread timeline build blocked the event loop"}
    {"threadId":"thr_tddwe6esxx","totalDurationMs":224.4,"thresholdMs":150,"selectionStrategy":"standard-window","pageKind":"latest","eventRowCount":1130,"eventDataBytes":703094,"projectedRowCount":298,"responseRowCount":298,"msg":"Thread timeline build blocked the event loop"}
    
    With 2 running threads the bb server used 24% of a core, with 10 running threads 84% (CPU-time deltas from ps -o cputime over fixed 40 s windows). I did not measure how many of those builds were identical-key misses, so I am not claiming this cutoff explains that load — only that above 200 rows the cache cannot help at all.
  • Investigation: bb thread thr_tddwe6esxx on my machine (local, not shareable by link); I can paste the measurement notes and scripts here on request.

What you ruled out

Suggested priority and effort

Affects long threads viewed by more than one client with identical request parameters. No workaround, no data loss. Replacing the row cutoff needs a memory-policy decision (rows can carry large nested content), so I am not suggesting an effort level.

AGENT GENERATED

Activity

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

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Classification: Bug · Priority: Medium · Effort: Medium. Applied threads, perf, and confirmed-repro; no existing classifications were replaced.

    Reproduction report: REPRODUCED · high confidence. On trusted main, the 200-row admission gate leaves larger responses uncached, so identical keys invoke the projection builder again. In two clean checkouts, three requests built once at 199–200 rows and three times at 201–300 rows. The report includes the test and output; production CPU impact was not measured. The normal build was blocked by a broken local pnpm launcher.

    No PR: replacing the explicit admission policy requires a memory-budget decision, so it fails the automatic simple-fix criteria.

    AGENT GENERATED

  4. bborn commented on Sep 27, 2026

    @bborn

    One data point for the memory-budget question. Measured on main at a19b3ed with a 378-row thread on loopback: the build ran on every request (25 ms each on an M-series Mac; 150 to 360 ms per the slow-build log on a 4-core Linux host, blocking the event loop each time). Raising the admission cap alone would let 128 entries of several hundred KB each stay resident, so the prototype at bborn#1 (apps/server/src/services/threads/timeline-cache.ts) keeps the entry limit and adds a total-row budget: responses up to 2,000 rows are admitted, the cache evicts oldest-first once the sum of cached rows passes 12,000, and a single entry larger than the budget is still kept. At roughly 1.6 KB of JSON per row that bounds the cache near 20 MB of response data. After the change the same thread answers from cache in 8 to 10 ms, most of it JSON and brotli. Tests for admission, budget eviction, oversized single entries and invalidation are in timeline-cache.test.ts on that branch. Happy to adjust the numbers to whatever policy you prefer before opening a PR.

    AGENT GENERATED

  5. luketraas commented on Sep 29, 2026

    @luketraas
    ContributorAuthor

    I have a fix on a branch that replaces the 200-row cap with a byte budget: each admitted response is weighed once by its serialized size, the cache keeps its 128-entry cap, adds a 16 MiB budget with least-recently-used eviction, and skips any response above a quarter of the budget. Through the real route, 10 identical requests for a 450-row page went from 10 builds (18.3 ms p50 per repeat) to 1 build (about 1 ms per repeat); under pressure, 40 threads of 515 KiB responses kept 31 in cache at 15.6 MiB accounted and 18.1 MiB measured heap.

    Before opening a PR: is a byte budget the policy you want here, and is 16 MiB acceptable, or would you prefer another number or a shared budget with the delta rows cache? @bborn, I compared against your prototype; happy to credit it or defer if you are opening one.

    AGENT GENERATED

  6. hellno commented on Nov 5, 2026

    @hellno
    Contributor

    Looks like this was fixed by #7063, which replaced the 200-row limit with a byte budget (2 MiB per entry, 64 MiB total). Can this be closed?

  7. ymichael commented on Nov 17, 2026

    @ymichael
    Collaborator

    Fixed on main: #7063 replaced the 200-row cap with a byte budget, and #7245 set the limits. Large responses are now cached. Closing.

    AGENT GENERATED

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