Skip to content

lanes: rest the provider refresh while nothing routes - #2010

Merged
AbirAbbas merged 2 commits into
app/v3from
perf/app-lane-refresh-idle
Oct 28, 2026
Merged

AbirAbbas merged 2 commits into
app/v3from
perf/app-lane-refresh-idle

Conversation

@AbirAbbas

@AbirAbbas AbirAbbas commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

An open engine (terminal or desktop) refreshed every remembered model's provider lanes every five minutes for as long as it ran: a ledger reload and provider requests for models nobody was using, all day on an idle machine. Now the refresh rests when nothing routes.

  • Demand is measured where routing happens. Every lane selection for a send or a probe calls Registry.RecordRouting (internal/lane/routing.go): one atomic load and a compare-and-swap, no lock, no wait on a refresh. Callers do not declare anything, so every route is covered: agent turns, tasks, automations, side errands, pool judging, factory triage, typing probes, run/do.
  • The beat (internal/lane/sheet.go) warms the sheet at startup exactly as before. It keeps its five-minute cadence while something has routed within the last interval, and stops its timer after a full interval with no routing.
  • Coming back from idle. The first selection after idle wakes the beat through a retained, coalesced catch-up request. The request is not lost even if the call ends before the beat goroutine runs. If the sheet is stale, one refresh runs in the background while the call routes on the existing beliefs, as it already does between beats today.
  • Unchanged: the interval, the rows refreshed, model joins (still fetched immediately), routing-off/direct-service/support gates, beat ownership. The exec and plan revise doors never had a beat and are left out.

How it got here (three adversarial review passes, read-only)

  1. A first version had callers hold demand. The review returned NOT SAFE: it missed pool judging, factory triage and typing probes, and could lose a short catch-up. It was replaced by this choke-point design.
  2. The second pass returned SAFE WITH FIXES, with three edge cases, all fixed with the reviewer's reproducers as tests:
    • a proxy's weak "no lane info" answer could never be re-learned once idle (demand is now recorded before that capability gate);
    • refused probes (routing off / direct services) counted as demand;
    • wall-clock steps could keep an idle beat refreshing. Demand is now measured on a per-registry monotonic origin.
  3. The third pass confirmed all three fixed. It found one more case: a provider's terminal preference refusal still woke a refresh, which can't help since a refusal is final (lanes.PrefsRefused, read-only, now gates the record). Regression test TestATerminalPreferenceRefusalLeavesTheBeatIdle: 2 fetches without the fix, 1 with it.

Validation contract → tests

Contract Test
Startup warms once; then an idle hour makes 0 refreshes TestTheIdleBeatDoesNoWorkForAnHour
Routing keeps today's cadence; the beat sleeps one interval after routing stops TestRoutingKeepsTheBeatUntilOneIdleInterval, TestRoutingStopsUnreadModelsAfterTheIdleThreshold
Stale routing → exactly one background refresh; the call does not wait (blocking refresh) TestStaleRoutingStartsOneRefreshWithoutWaiting, TestProbeRoutingDoesNotWaitForRefreshOrSend
A routing that ends before the beat runs keeps its catch-up; bursts coalesce TestRoutingBeforeBeatDispatchKeepsItsCatchup
A bare provider completion and a probe selection alone each count as routing TestBareProviderRoutingWakesTheBeat, TestColdProbeSelectionAloneWakesTheBeat, TestInstalledProbeChooserStillCountsRouting
Gates and predictions don't count; every sheet wakes; registration races TestProviderRoutingGatesLeaveTheBeatIdle, TestRoutingPredictionsLeaveTheBeatIdle, TestRoutingWakesEveryIndependentSheet, TestRoutingDuringBeatRegistrationReachesEverySheet

Checks

  • gofmt -l clean; go vet on internal/lane, internal/provider, internal/session, cmd/codeaf.
  • Named tests across those packages: 315 top-level + 47 subtests pass, 0 fail.
  • After the last fix: go vet on internal/lane and internal/provider, and the named lane/provider tests pass.
  • CI (dispatched) on the final head: PR gate run 40630020775 and full check run 40630022842, against app/v3 (1e4a7e1c3) baseline runs 40627876452 (gate) and 40627880992 (full).
    • Gate: 3 failures, all in the baseline's 11.
    • Full check: 3 failures outside the baseline. TestTheSameSentenceIsInTheJSONAnswer is a TempDir RemoveAll teardown race. TestAStoppedTaskResultCannotAutoStartItsRequestAgain already failed on app: bound the idle scan over closed chats (desktop idle CPU) #2007's run, which touched neither lanes nor sessions. TestAStopDuringProofKeepsTheControlledFailureEnding (internal/secaf) passes 10/10 locally on this head, as does the session test.
    • The previous head's full check (run 40629347579) had a different single extra (TestTheNodeDoorCommitsARunRowNotesKeyInTheStore, the same teardown race, 6/6 locally). Flakes.
  • Terminal behaviour: no control or copy changes. The only difference a terminal user can see is that an engine left idle for more than five minutes makes no lane refreshes until the next request, which then refreshes in the background.

Re-verified before merge (2026-10-28)

On today's app/v3 (2e649b4, after #2008) merged with this branch; no app/v3 change since the merge-base touches internal/lane, internal/provider or the session lane setup.

  • gofmt clean on the 9 changed Go files; go vet ./internal/lane/ ./internal/provider/ clean; whole packages go test ./internal/lane/ ./internal/provider/: 1,048 top-level tests (1,391 with subtests), 0 failures.
  • Live, old vs new side by side (codeaf app on fresh homes in a loopback-only network namespace, a local stub provider with a dummy key; one chat open, idle, one turn at 16 min, shutdown at 28 min):
Sheet fetches (s from start) Reply latency for the 16-min turn
before 0.6, 300, 600, 900, 1200, 1500 15.0 ms
after 0.6, 960 (one catch-up when the turn routed, answered 4 s late on purpose) 15.8 ms, before the held catch-up finished

After the turn the new engine rested again for the rest of the run. Both engines exited 0.

  • Terminal: codeaf plan run on the new binary with the same stub routed its one request and exited 0.
  • Desktop: the Windows desktop ran 8 real turns and 2 reply drafts through OpenRouter on a Windows build of this branch without a routing problem.

🤖 Generated with Claude Code

AbirAbbas added a commit that referenced this pull request Oct 28, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
AbirAbbas added a commit that referenced this pull request Oct 28, 2026
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@AbirAbbas
AbirAbbas force-pushed the perf/app-lane-refresh-idle branch from 937db19 to 9e72a20 Compare October 28, 2026 03:54
AbirAbbas and others added 2 commits October 28, 2026 00:02
Every open engine (terminal or desktop) refreshed every remembered model's
provider lanes every five minutes for as long as it ran, reloading the ledger
and reaching providers for models nobody was using.

Demand is now measured where routing happens: every lane selection for a send
or a probe stamps the registry's last-routed time (lock-free, no wait on a
refresh). The beat refreshes at startup as before and keeps its cadence while
something has routed within the last interval; after a full idle interval it
stops its timer. The first selection after that wakes it through a retained
catch-up request (never lost, even if the routing ends at once), and if the
sheet is stale one refresh runs in the background while the call routes on the
existing beliefs. Every caller is covered without declaring demand, including
pool judging, factory triage and typing probes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@AbirAbbas
AbirAbbas force-pushed the perf/app-lane-refresh-idle branch from 9e72a20 to e7d895f Compare October 28, 2026 04:02
@AbirAbbas
AbirAbbas marked this pull request as ready for review October 28, 2026 04:47
@AbirAbbas
AbirAbbas merged commit db5ed1f into app/v3 Oct 28, 2026
14 of 20 checks passed
@AbirAbbas
AbirAbbas deleted the perf/app-lane-refresh-idle branch October 28, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant