Repository navigation
Fix: Replay the usage ring beside live traffic, not before it - #1519
Conversation
The startup replay of the session archive into the usage ring ran before any listener, so /healthz waited for it — up to its 5s budget. On a loaded laptop it reached that budget on both restarts of 2026-10-27, and agentop setup's 15s health wait expired and rolled the install back. The replay now starts before the listeners and finishes beside them: - archive.ReplaySources takes the segment list before any listener starts. A process never appends to a segment it did not open, so nothing live traffic records is read back and counted twice. - usage.Aggregator.BeginReplay hands out a private ring to feed, so the time-ordered feed keeps eviction and request pairing as they were. - FinishReplay merges it with mergeRing, first applying every clear, delete and rename the live ring took meanwhile, so an erased session does not come back. The budget is 60s now: it no longer delays readiness. Measured on a copy of a real archive under taskpolicy -b (launchd's Background ProcessType): healthy after 0.47s; the replay merged 3.9s later with 525 requests in LAST 1H. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 1 minute. View limit details
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Snapshot and read archive sources core/session/archive/replay.go, core/session/archive/replay_bench_test.go |
ReplaySet snapshots eligible segments and reads events from that snapshot. ReplaySince delegates to these methods. |
Track and merge private usage replay core/cost/usage/*.go, cmd/cortex/archive_replay_test.go |
The aggregator records live clears, deletes, and renames during replay. On completion, it merges replayed totals and session rings into live usage while enforcing the session cap. Tests cover the merge and concurrent live changes. |
Run replay alongside startup traffic cmd/cortex/archive.go, cmd/cortex/main.go, cmd/cortex/archive_replay_test.go, cmd/agentop/local_endpoint.go, cmd/agentop/local_endpoint_test.go, CLAUDE.md, docs/laptop-service.md |
Startup snapshots archive sources and runs replay asynchronously with a 60-second budget. It merges on success and abandons replay on error. Tests and documentation describe the updated flow. |
Priority:
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Sequence Diagram(s)
sequenceDiagram
participant Cortex as cmd/cortex
participant Archive
participant ReplaySet
participant Aggregator
Cortex->>Archive: ReplaySources(last six hours)
Cortex->>Aggregator: BeginReplay()
Cortex->>ReplaySet: Read(ctx, callback)
Cortex->>Aggregator: FinishReplay(private ring) on success
Possibly related issues
- Issue 1497: The change keeps replayed usage consistent with live clears, deletes, renames, and concurrent traffic.
- Issue 964: The change replays archived usage after restart and merges it with live usage.
Suggested reviewers: cwiklik, esnible, alan-cha
Merge Risk: 
Low · previous review 09a4a
-
The change is mergeable with awareness that a session renamed during replay may temporarily show too little historical usage.
Pre-merge checks: 
5
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 92.86% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 14 functions across 13 files. (2 skipped: 2 unsupported.)
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
Fixes review: the double-count test used a session with no archived segment, so it passed even if the archive reopened an old segment Fixes review: Read's comment said a segment erased mid-read lost nothing, but the totals an erase keeps come up short; now a shortcut comment naming that limit Files: - cmd/cortex/archive_replay_test.go - core/session/archive/replay.go Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Fixes review: laptop-service.md's restart paragraph said the proxy replays the archive (up to 5s) before it serves, which the background replay made false Files: - docs/laptop-service.md Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Fixes review: the rewritten restart line ran past the paragraph's wrap Files: - docs/laptop-service.md Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
evaline-ju
left a comment
There was a problem hiding this comment.
Moves the startup usage-ring replay off the readiness path. Correctness hinges on three things: segment list is taken before any listener starts; the replay feeds a private aggregator; the live aggregator logs clears, deletes and renames until FinishReplay applies them before merging.
Independent verification:
- Mutation check: stubbed out
FinishReplay's op-replay loop →TestStartUsageReplay_AnEraseOrRenameMeanwhileHoldsfails on all three subtests (delete/clear/rename) as the PR body claims. Restored, passes. - Tried a second mutation (moving
set := arch.ReplaySources(...)to afterstore.Append+store.Close) and the test still passed — suggesting the archive's publish/close timing happens to leave the live turn out of the index at that moment too, so the test's safety margin is wider than its comment ("the set must not") implies. Not a bug.
Diff is well-scoped, the replaying/replayOps field pair is written only under a.mu, and the user-visible change (empty-then-fills LAST 1H for a few seconds) is called out in docs.
Assisted-By: Claude (Anthropic AI) [email protected]
Problem
The startup usage replay ran before any listener, so
/healthzwaited for it.agentop setupwaits 15s for health. It ran out, somake dev-installrolled back.Two things make it slow, measured on a copy of a real archive:
ProcessType=Background: 1.1s at normal priority, 3.1–4.1s undertaskpolicy -b.ReplayCopydrops them straight away.Fix
Run the replay beside live traffic, not before it.
archive.ReplaySourcestakes the segment list before any listener starts. A process never appends to a segment it did not open, so nothing live traffic records is read back and counted twice.usage.Aggregator.BeginReplayreturns a private ring to feed. The time-ordered feed then keeps eviction and request pairing as they were.FinishReplaymerges it with the existingmergeRing. It first applies every clear, delete and rename the live ring took meanwhile, so an erased session stays erased.User-visible change: for a few seconds after a restart,
LAST 1Hcounts only live traffic, then fills in.Verification
Scratch Cortex on 49600–49604, a copy of a real archive, under
taskpolicy -b:/healthzLAST 1HNew tests:
startUsageReplayreturns at once and merges later.The first two were checked by mutation: each fails when the guard it covers is removed. core,
cmd/cortexandcmd/agentoppass with-race; vet and gofmt are clean.Not in this PR
ProcessType=Backgroundfrom the plist. It throttles every request, not only startup. Commit 0d936d2 added it with no stated reason.Assisted-By: Claude (Anthropic AI) [email protected]
Summary by CodeRabbit