Skip to content

perf(server): turn starts no longer queue behind checkpoint captures - #42537

Closed
juliusmarminge wants to merge 2 commits into
mainfrom
t3/effect-worker-priority-scheduling
Closed

juliusmarminge wants to merge 2 commits into
mainfrom
t3/effect-worker-priority-scheduling

Conversation

@juliusmarminge

@juliusmarminge juliusmarminge commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

The orchestration effect worker had 4 slots handed out FIFO. With many active threads, a new message's provider request waited for a slot behind other threads' git-bound checkpoint captures. In the server-metrics working scenario at 20 threads that wait had a median of 6–7 s. Raising the pool size cut the wait, but captures got the extra concurrency too. Git throughput is the real limit, so turn ends got slower and throughput did not improve (−18% turns/min on 4-vCPU Windows at 8 slots).

Fix

Each effect type now has a priority. A satisfies Record means a new effect type has to pick one.

  • Interactive: turn start/restart/steer/interrupt, runtime-request.respond, rollback.
  • Normal: runtime continue, model/title apply, title generation, delegated-task stop.
  • Background: checkpoint capture, session detach, the cleanups.

How claims work now:

  • Claim order: the outbox claim query orders by level, FIFO within a level.
  • Pool size and caps: the pool grows to 6 slots. Two caps on running rows replace the old title-only cap:
    • Background ≤ 4, so captures keep exactly the concurrency they had.
    • Non-interactive ≤ 5, so one slot is always open to interactive work.
    • The caps count rows rather than slots because a row keeps running after its slot stops waiting at 30 s.
  • Starvation: I used aging. Work that has waited 10 s or more ranks with fresh interactive work and is claimed FIFO ahead of it, so a steady stream of turn starts cannot starve captures or cleanups. That is one CASE branch, it keeps FIFO within each level, and it covers normal work too. A cap on interactive concurrency would add a third limit and still let normal work starve.
  • Per-thread order: unchanged. The existing claim predicate still blocks a candidate behind any earlier pending or running effect on its thread, so the perf(server): runs finish when the provider turn ends, not after checkpoint capture #41362 hold is unchanged.
  • Priority inheritance: a job that its own thread's queued interactive work waits on is claimed at interactive priority. The usual case is the turn-end capture ahead of the user's next message. This is one EXISTS branch in the claim ORDER BY, computed from outbox rows at claim time, with no new state. The boosted job still counts toward the background cap of 4 and the non-interactive cap of 5, so git concurrency does not rise.

I also tried the broader rule "every capture whose run is held ranks with interactive work", since a held run looks "working" to the user. In a quick A/B against this PR's first head it did not help: working.20 turnEndMs median +4% (−269 to 266), and toRequestMs median got worse by 9% (−162 to 683). Captures are limited by the cap of 4 on git work, not by claim order, so ranking them higher only takes slots from turn starts. Ranking them as normal would change even less, because the working scenario has almost no normal work. So I kept the narrow rule: it costs nothing when no turn is queued and removes the wait when one is.

The tests are deterministic, use TestClock and have no sleeps:

  • Interactive work is claimed before normal, and normal before background, FIFO within each level.
  • A turn start is claimed at once while captures hold the background cap. The 5th capture waits for one to settle.
  • Aged work is claimed FIFO ahead of fresh interactive work.
  • A thread's turn start still waits for its own earlier capture, and that capture is claimed ahead of an older capture on another thread.
  • With interactive work from other threads running and the background cap not reached, a capture that blocks its own thread's queued turn start is claimed ahead of normal and background work. An unblocked capture stays background.
  • A daemon test: a turn starts while 4 captures are blocked. On main's pool this hangs.

Benchmark

Run on cups (16 cores, load 1.22 / 0.86 / 1.27 at start), server-metrics ab --rounds 3 --only working,pipeline. A is main 34ca872cbb6, B is cd0296518ed (this head).

metric A B change 95% interval
working.20 toRequestMs median 6205 5871 −5% −988 to −62
working.20 toRequestMs p90 7097 6708 −5% −623 to −23
working.20 turnEndMs median 955 1634 +71% 369 to 812
working.20 turnEndMs p90 1539 2036 +32% 42 to 853
working.20 turnMs median 9206 9287 +1% −208 to 365
working.20 turnMs p90 10223 10102 −1% −145 to 229
working.20 turnsPerMin 127.1 121.7 −4% −11.9 to 3.6
working.20 cpuPct 148 145.8 −1% −3.8 to 3
working.20 heapAfterGcMB 231.9 234.2 +1% 2.1 to 2.9
working.5 toRequestMs median 845 799 −5% −145 to 25
working.5 toRequestMs p90 1092 1113 +2% −198 to 192
working.5 turnEndMs median 154 158 +3% −26 to 12
working.5 turnEndMs p90 266 267 +0% −30 to 37
working.5 turnMs median 2862 2792 −2% −127 to 53
working.5 turnMs p90 3123 3143 +1% −192 to 256
working.5 turnsPerMin 103.6 101.5 −2% −8.1 to 5.5
working.5 cpuPct 119.8 124.5 +4% −3.8 to 10.1
working.5 heapAfterGcMB 225.9 225.1 0% −2.9 to 0.2
pipeline toRequestMs median 2335 2178 −7% −192 to 17
pipeline msPerTurn 2944 2771 −6% −403 to 124

Trade-off: at 20 threads, captures give way to other threads' turn starts, so turnEndMs rises (median +71%, p90 +32%), while whole-turn time turnMs stays flat. The inheritance rule does not show up in these numbers. Each working thread sends its next message only after its run ends, so a turn is never queued behind a capture. It applies when a user sends a message while the last turn's capture is still pending. In that case the capture no longer waits behind other threads' turn starts. With 5 threads and in pipeline nothing gets worse. working.heapMBPerThread goes from 0.38 to 0.58 (interval 0.14 to 0.37). The first head measured 0.48 → 0.56, so this looks like noise in a small per-thread fit.

Implemented by Claude Opus 5.5 in Claude Code, running in T3 Code.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅
Codex Live turn messages 1 1 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅
Claude Live turn messages 1 1 0 (0.0%) 8 ✅

Other scenarios

All 64 scenario measurements match main.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded 10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅
Codex short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn running, cold open HTTP decoded 281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅
Codex long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Codex long turn running, history page HTTP decoded 92.4 KiB 92.4 KiB 0 B (0.0%) 107.4 KiB ✅
Codex long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Codex long turn finished, cold open HTTP decoded 381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, socket cold open WebSocket decoded 381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, work page HTTP decoded 186.2 KiB 186.2 KiB 0 B (0.0%) 200.2 KiB ✅
Codex long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Codex idle thread resume WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell snapshot HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Codex fleet shell subscribe WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Codex fleet shell resume check rows 0 0 0 0 ✅
Codex stale shell reload HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex stale shell reload rows 27 27 0 (0.0%) 27 ✅
Codex stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Codex stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex stale todo list cold open HTTP decoded 72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅
Codex stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Codex stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅
Claude short turn resume WebSocket decoded 10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅
Claude short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn running, cold open HTTP decoded 283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅
Claude long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Claude long turn running, history page HTTP decoded 93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅
Claude long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Claude long turn finished, cold open HTTP decoded 384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, socket cold open WebSocket decoded 384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, work page HTTP decoded 188.1 KiB 188.1 KiB 0 B (0.0%) 200.2 KiB ✅
Claude long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Claude idle thread resume WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell snapshot HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Claude fleet shell subscribe WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Claude fleet shell resume check rows 0 0 0 0 ✅
Claude stale shell reload HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude stale shell reload rows 27 27 0 (0.0%) 27 ✅
Claude stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Claude stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude stale todo list cold open HTTP decoded 73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅
Claude stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Claude stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅

Baseline: c621d5f · PR result: cd02965 · Source CI: success

Scenario and decoded snapshot size

10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

  • Codex decoded thread snapshot: 108.5 KiB
  • Claude decoded thread snapshot: 108.8 KiB

Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The effect outbox now uses shared running-work limits and priority-based claim ordering. The worker defaults to six concurrent effects and supplies non-interactive and background caps. Tests cover priority, caps, aging, and per-thread ordering.

Changes: Effect scheduling

Layer / File(s) Summary
Outbox claim rules
apps/server/src/orchestration-v2/EffectOutbox.ts, apps/server/src/orchestration-v2/FoundationPersistence.test.ts, docs/internals/01_architecture.md
The outbox accepts limits for non-interactive and background work. It prioritizes aged and interactive work, and promotes work that blocks interactive work on its thread. Background work ranks last. Ties use availability time, creation time, and effect ID. Tests cover claim limits, ordering, aging, and per-thread ordering. The architecture documentation describes the scheduling rules.
Worker concurrency policy
apps/server/src/orchestration-v2/EffectWorker.ts, apps/server/src/orchestration-v2/FoundationPersistence.test.ts
The default concurrency increases from four to six. When concurrency exceeds one, the worker limits non-interactive work to concurrency minus one and background work to four. A worker test checks that a provider turn starts while four checkpoint captures occupy workers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant EffectWorkerV2
  participant EffectOutboxV2
  participant Database
  EffectWorkerV2->>EffectOutboxV2: claimNext with shared limits
  EffectOutboxV2->>Database: select claimable effects by caps and priority
  Database-->>EffectOutboxV2: return eligible effect
  EffectOutboxV2-->>EffectWorkerV2: return claimed effect
Loading

Possibly related issues

Possibly related PRs

  • pingdotgg/t3code#30669: Both changes adjust EffectOutbox.claimNext ordering for checkpoint captures and preserve the per-thread rule.

Suggested labels: bugfix

Suggested reviewers: only21mil, macroscopeapp

Merge Risk: Minimal · up to cd029

This change reorders effect scheduling so turn starts are no longer blocked by checkpoint captures. No concrete merge-blocking risk was found in the supplied changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "perf(server): turn starts no longer queue behind checkpoint captures" matches the main change: turn starts are no longer blocked by checkpoint captures from other threads, via priority-base…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the template's Problem, Change, Scope, and Verification sections, and it explains the problem, the fix, the tests, and a benchmark. It has no title-level issue and no explicit …
Full details: Title check

Explanation

The title "perf(server): turn starts no longer queue behind checkpoint captures" matches the main change: turn starts are no longer blocked by checkpoint captures from other threads, via priority-based effect claiming, slot pool growth, and running-row caps. It follows conventional commit format and is specific.

Full details: Description check

Explanation

The description follows the template's Problem, Change, Scope, and Verification sections, and it explains the problem, the fix, the tests, and a benchmark. It has no title-level issue and no explicit issue link in the Scope section. The Scope section says the PR is a bug fix, but it does not link an issue or explain why none is needed. The author gives a Claude attribution line, which the template asks for. The description is mostly complete, so the check passes. The Scope gap is a minor item and does not make the description incomplete.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4c791db and cd02965.

Files selected for processing: 3

  • apps/server/src/orchestration-v2/EffectOutbox.ts
  • apps/server/src/orchestration-v2/FoundationPersistence.test.ts
  • docs/internals/01_architecture.md
Files skipped from review as they are similar to previous changes: 1

  • docs/internals/01_architecture.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

… that turn's priority

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@juliusmarminge

Copy link
Copy Markdown
Member Author

Closing after the A/B in a new eager scenario: each thread sends its next message while its previous capture is still pending (100% of sends). Main 34ca872 vs cd02965, 3 rounds on cups.

  • No extra win from the priorities: at 20 threads, toRequestMs median improves by the same amount with or without a pending capture: −12% in eager, −11% in working.
  • turnEnd gets clearly worse: the median is +65% in eager (interval 605…661 ms) and +70% in working.
  • No gain overall: turnMs (send to run end) is flat in both scenarios, and turnsPerMin changes within noise.
  • heapMBPerThread is flagged worse again: 0.39 → 0.55 MB.

The priorities move the wait from the start of a turn to its end without shortening the turn, so this isn't worth the added scheduling complexity. Raw results are in the maintainer's ~/tempo-audit/eager-ab/ on cups. The scenario is on branch perf/bench-send-during-capture (898cf20).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant