Skip to content
Source
Sign inSign up
pingdotgg/t3code

Default-on auto-settle sweep re-scans and decodes every thread every minute: 63 ms on a synthetic 1,400-thread database #20438

Closedbompus(GitHub profile, opens in a new tab) opened this issue 1 comment

What happened

With default settings, the server runs an auto-settle sweep every minute that scans every non-deleted thread and decodes every candidate's full payload, even when nothing has merged and nothing settles. On a synthetic 1,400-thread database one sweep query takes 63 ms on the writer connection; real thread payloads are larger, so real installs with thousands of threads pay more, every minute, plus one extra full sweep per pull-request merge.

Diagnosis

ThreadSettlementServiceV2 repeats its sweep every 1 minute (apps/server/src/orchestration-v2/ThreadSettlementService.ts) plus once per merged pull request and per relevant settings change. It is on by default: sidebarAutoSettleOnMerge decodes to true and sidebarAutoSettleAfterDays to a non-null default (packages/contracts/src/settings.ts), so autoSettlementConfigured() is true out of the box.

Each sweep calls getSettlementCandidates (ProjectionStore.ts, getSettlementCandidates) inside a transaction on the main writer SqlClient. The query scans all non-deleted threads, applies four json_extract payload filters per row (archivedAt, settledOverride, pinnedAt, autoSettleDisabledAt — archived also exists as a column), runs three correlated subqueries per thread (latest run, latest user message, latest user-authored message), then schema-decodes every returned thread payload. A merge event deliberately re-sweeps all candidates, not just the merged PR's threads. After the query the sweep also lists all project shells and runs per-candidate git/pull-request lookups.

Measured on the real ProjectionStore layer (production modules, file SQLite, Node v26.12.0), synthetic fixtures (1 completed run per thread, small payloads), 1 warmup + 5 timed repetitions, result counts verified:

ThreadsItems/threadgetSettlementCandidates medianCandidates returned
5002016.5 ms (range 14.1-19.3)500
1,4005063.3 ms (range 43.8-63.7)1,400

Neighboring sweeps on the same fixture, for scale: getThreadsWithPullRequests 19.1 ms at 1,400 threads (runs every 1-2 min plus per requested sweep), getShellSnapshot 94.3 ms. Those are separate queries; this issue is only the settlement sweep.

Possible directions (maintainer's choice): run the candidate query on the read-only client like other periodic reads; narrow it with existing columns/indexes (archived_at, settled state) instead of per-row json_extract; and/or scope merge-triggered sweeps and skip payload decode for threads that cannot settle yet (age/merge state known from columns and the latest-run join).

Limits: synthetic small payloads and one run per thread, warm page cache, query only (the sweep's later git/pull-request lookups were not timed). Absolute numbers on a real database will differ; the per-minute full scan plus decode comes from the query and cadence in code. Harness available on request.

Steps to reproduce

No end-user repro script is included; the numbers come from a harness that seeds synthetic threads through the real ProjectionStore and times getSettlementCandidates directly. From the app: run a server with default settings against a database with thousands of threads and watch the ThreadSettlementServiceV2.sweep span every minute. Harness available on request.

Version

upstream/main 6cecd22789 (measured at 2577baf8b0; ProjectionStore.ts byte-identical between the two)

Environment

Linux, Node v26.12.0

Evidence

{"label":"getSettlementCandidates","threads":500,"itemsPerThread":20,"medianMs":16.46,"rangeMs":[14.06,19.26],"result":500}
{"label":"getSettlementCandidates","threads":1400,"itemsPerThread":50,"medianMs":63.34,"rangeMs":[43.75,63.66],"result":1400}

None found for the settlement sweep (searched settlement/auto-settle on 2026-10-16). The 5-second usage-limit recovery sweep is a different query and is already tracked inside #17609.

Fix applied or workaround

None applied. I can open a pull request for this once triage confirms the direction. Not written yet; the approach I would measure is one of the directions in Diagnosis (read-only client and/or column-narrowed candidate query, keeping merge-settle correctness).

Filed by

Muse Wonder Full 1.0 via Muse Code in T3 Code, on behalf of bompus

  1. bompus(GitHub profile, opens in a new tab) added labelsvia-triage

  2. Note

    Grok responding on behalf of Julius.

    Thanks for the measurements. On main, ThreadSettlementServiceV2 does repeat its sweep with Schedule.spaced("1 minute"), and each sweep calls getSettlementCandidates in ProjectionStore.ts.

    We're not tracking this as a separate bug, though:

    • It's the same issue as #11220. That issue already covers the once-a-minute settlement and PR-discovery sweeps running when no client needs them. Making the candidate query cheaper is one way to address it, so the details belong there rather than in a new issue.
    • The cost is small, and the data was synthetic. 63 ms once a minute on a background loop, at 1,400 threads with small fake payloads, isn't a user-visible problem. The report doesn't show any writer contention, UI stall, or a real database where this matters.
    • What's being proposed is a perf enhancement, not a fix. Moving the query to the read-only client, filtering on columns instead of json_extract, and scoping merge sweeps are all design choices for the sweep. They aren't fixes for broken behavior. #19657 / #19665 (subagent threads as candidates) are already narrowing the candidate set too.

    I'm closing this as a duplicate of #11220. If you can show a real-world database where this sweep causes measurable writer contention or lag, please add those numbers to #11220.

  3. juliusmarminge added labelsduplicate

  4. juliusmarminge closed this

Sign in to join this conversation.