Skip to content

Timeline builds take quadratic time in a fileChange item's file count #7833

Closed

Description

@cleanunicorn

Summary

Building a timeline costs time quadratic in a fileChange item's file count. matchFileRows runs for every fileChange item event (started, updated, completed). For each change it scans all of the record's earlier file rows with findIndex + some + includes. A 2,000-file change takes 28 ms per timeline build instead of about 3 ms. A 4,000-file change takes 127 ms instead of about 6 ms. The server runs this build synchronously when it serves a thread timeline (timeline.ts#L1966), so the time blocks the event loop.

Versions and environment

  • main at df9619d8e8, run from source.
  • Linux 6.17, Node 24.14.1.
  • Found in a code survey of per-event costs. The steps below are self-contained and need no provider.

Steps to reproduce

Save this script as packages/thread-view/bench.ts in a checkout, then run pnpm install and pnpm exec tsx packages/thread-view/bench.ts from the repo root. It builds a timeline from turn/started, then item/started and item/completed for one fileChange item with N changes, and prints the best of 5 builds after a warm-up for each N.

import { turnScope } from "@bb/domain";
import { EMPTY_ACCEPTED_CLIENT_REQUEST_CONTEXT } from "./src/accepted-client-request-context.js";
import { buildThreadTimelineFromEvents } from "./src/index.js";
const changes = (n: number) => Array.from({ length: n }, (_, i) => ({ path: `src/dir-${i % 37}/file-${i}.ts`, kind: "update" as const }));
const ev = (seq: number, type: string, item?: object): any => ({ event: { type, threadId: "thread-1", providerThreadId: "p", scope: turnScope("turn-1"), ...(item ? { item } : {}) }, meta: { id: `e${seq}`, seq, createdAt: seq } });
const fc = (n: number, status: string, rev = false) => ({ type: "fileChange", id: "fc-1", changes: rev ? changes(n).reverse() : changes(n), status, approvalStatus: null });
const build = (events: any[]) => buildThreadTimelineFromEvents({ acceptedClientRequestContext: EMPTY_ACCEPTED_CLIENT_REQUEST_CONTEXT, contextWindowEvents: [], events, options: { completedTurnDisplay: "collapse", includeNestedRows: true, omitSettledDelegationChildRows: false, includeDiagnosticOperations: false, summarizeShellCommands: true, isLatestPage: true, threadStatus: "idle", threadName: "", workspaceRoot: null } } as any);
const out: any[] = [];
for (const n of [50, 200, 500, 1000, 2000, 4000]) {
  const events = [ev(0, "turn/started"), ev(1, "item/started", fc(n, "pending")), ev(2, "item/completed", fc(n, "completed"))];
  build(events);
  let best = Infinity;
  for (let i = 0; i < 5; i++) { const t0 = performance.now(); build(events); best = Math.min(best, performance.now() - t0); }
  out.push({ files: n, ms: +best.toFixed(2) });
}
console.log(JSON.stringify(out));

Expected vs actual

Expected: build time grows roughly linearly with N.

Actual: build time grows quadratically with N. The table uses the same change order in both events; reversing the order in item/completed costs more, since each lookup then scans past more claimed rows.

files build (ms)
50 0.67
200 1.46
500 4.24
1,000 8.56
2,000 28.21
4,000 127.36

Evidence

  • The scan over every earlier row, once per change: item-records.ts#L409-L449.
  • It runs for every fileChange item event, through applyItem → matchFileRows.
  • Timings: performance.now() around buildThreadTimelineFromEvents, best of 5 runs after a warm-up, Node 24.14.1.

What you ruled out

  • Typical edits touch only a few files. The cost matters for large edits: codemods, formatter runs, renames across a tree, or a generated directory.
  • Not a duplicate: no open or closed issue or PR covers matchFileRows. Prune duplicated command output and started file-change diffs #7806 touched file-change diffs, not row matching.
  • The matching rule has to stay as it is: each change takes the earliest unclaimed row that shares its path or movePath. Row identity across events depends on that rule.

Suggested priority and effort

  • Low priority: it costs time only on large file changes.
  • Small effort. Index the existing rows by match key once per event, and keep a cursor per key past claimed rows. That gives the same earliest-unclaimed-row result in O(N).

AGENT GENERATED

Activity

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

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority Low · Effort Low; labels threads, perf.
    • Report: Reproduction and root cause — REPRODUCED, high root-cause confidence; labeled confirmed-repro.
    • Root cause: Each incoming file repeatedly scans accumulated file rows during synchronous timeline projection, including rows already claimed by the same event.
    • Evidence: An independently authored regression fails on unchanged main in two clean checkouts: 16× more files costs approximately 183× and 182× with reversed ordering. It passes after the fix. All 545 package tests and 58 server tests pass, along with typecheck and type-aware lint; two history tests skip without their optional dataset.
    • Fix: Ready PR #7835 indexes historical paths while preserving row identity. 193 changed text lines across 3 files; not merged.
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