Skip to content

fix(usage): yield within indexed row decoding and aggregation - #8800

Merged
lidge-jun merged 1 commit into
lidge-jun:devfrom
luvs01:fix/usage-index-cooperative-row-processing
Nov 17, 2026
Merged

lidge-jun merged 1 commit into
lidge-jun:devfrom
luvs01:fix/usage-index-cooperative-row-processing

Conversation

@luvs01

@luvs01 luvs01 commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Filtered usage reports already page candidate offsets in groups of 2,000, but decoding and normalizing every row, then folding the whole page into the accumulator, still run synchronously. A page with multi-attempt usage rows can delay other requests and timer-delivered cancellation even though the complete offset set is bounded.

This follows up the indexed paging fix by making the remaining row decoding and aggregation work cooperative.

  • Apply the existing 8 ms cooperative slice inside indexed row decoding and callback work; one row remains atomic.
  • Fold rows directly into the private accumulator without retaining a parsed page. Callers without the optional callback retain the existing returned-array contract.
  • Preserve cancellation and callback errors, close descriptors on failure, clear the cached read window before verification, and reject identity, shrinkage, or revision changes during a yielding verification pass. Partial accumulators remain unpublished and are discarded by the existing retry paths.
  • Update the usage contracts and add twelve regression cases covering the reader and the production aggregate path.

Verification

Recorded local validation:

  • bun run test:changed tests/usage/usage-ledger-scanner.test.ts tests/usage/usage-filter-index.test.ts tests/usage/usage-filter-paging.test.ts tests/usage/usage-aggregate-cache.test.ts tests/server/api-usage.test.ts --timeout 30000 — 175 passed, 0 failed, 913 assertions across five files.
  • bun run typecheck — no type errors in 2,533 files.
  • bun run structure:check and bun run privacy:scan — passed.
  • bun test tests/ci-workflows/file-size-ratchet.test.ts --timeout 30000 — 9 passed, 0 failed.
  • git diff --cached --check — passed.

Posted-head CI 43812029790 passed, including the Typecheck step and test changed job. Catalog checks also passed. This is exact-head hosted evidence; the local results above are retained prior validation and were not rerun for this status change.

A source comparison against dev c36793a9336b70cfe5bc3b7f6e6f751e730fb6fc found no duplicate implementation or incompatible contract change requiring a rebase. Maintainer security review and installed/live application validation remain separate; review readiness does not claim merge or release acceptance.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

The security checkbox records implementation review; explicit maintainer security review remains required before merge.

Summary by CodeRabbit

  • Performance
    • Usage summaries remain responsive during larger scans, with processing yielding control periodically rather than monopolizing the application.
  • Bug Fixes
    • Usage results are checked for changes that occur during scanning, helping prevent incomplete or inconsistent summaries from being published.
  • Documentation
    • Updated usage-accounting documentation to reflect the latest processing and validation behavior.

Stream indexed rows into the private accumulator under the existing cooperative
slice and honor cancellation within a candidate page. Revalidate fresh bytes
and stable file identity after any yielding verification pass.

Add regression coverage for scheduling, aborts, compatibility, and file mutations.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The indexed ledger reader now supports callback-based row accumulation and cooperative yielding during candidate traversal. After detected mutations, it rereads selected content and validates file identity, size, and revisions before returning.

Changes: Indexed Usage Processing

Layer / File(s) Summary
Cooperative read and accumulation
src/usage/ledger-scanner.ts, src/server/management/usage-aggregate-cache.ts, tests/usage/usage-ledger-scanner.test.ts, tests/usage/usage-aggregate-cache.test.ts, structure/dashboard-and-usage.md
The indexed reader can send normalized rows to an onEntry callback instead of retaining them in entries. Candidate traversal and callback work share the cooperative time slice. The aggregate cache accumulates rows through the callback. Tests cover yielding, cancellation, callback errors, and result ordering.
Mutation verification
src/usage/ledger-scanner.ts, tests/usage/usage-ledger-scanner.test.ts, structure/usage-client-attribution.md
After detecting a mutation, the reader clears its cached row window and rereads candidates. It rejects identity, size, or revision changes during verification. Tests cover rewrites, appends, replacement, shrinkage, and cancellation.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant AggregateCache as accumulateCandidates
  participant LedgerReader as readUsageLedgerRowsAtOffsets
  participant Accumulator as Usage accumulator
  AggregateCache->>LedgerReader: Read indexed candidates with onEntry callback
  LedgerReader->>Accumulator: Pass each normalized row to callback
  LedgerReader-->>AggregateCache: Return after traversal and validation
Loading

Possibly related PRs

Merge Risk: Minimal · up to 1743a

This change makes indexed usage-report decoding and aggregation yield cooperatively, and it tightens the checks for file changes during verification. No concrete defect was found, and the change adds regression tests for the new behavior. The merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: making indexed usage row decoding and aggregation yield cooperatively. The raw summary and objectives confirm that the change adds an onEntry callback, a cooperative 8…
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.
Full details: Title check

Explanation

The title names the main change: making indexed usage row decoding and aggregation yield cooperatively. The raw summary and objectives confirm that the change adds an onEntry callback, a cooperative 8 ms slice in the candidate reader, and direct accumulation. The title is 61 characters and one sentence, uses a conventional fix(usage) prefix, and avoids vague terms. It does not cover the verification hardening, but that is expected for a short title.

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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 595de1e and 1743a93.

Files selected for processing: 6

  • src/server/management/usage-aggregate-cache.ts
  • src/usage/ledger-scanner.ts
  • structure/dashboard-and-usage.md
  • structure/usage-client-attribution.md
  • tests/usage/usage-aggregate-cache.test.ts
  • tests/usage/usage-ledger-scanner.test.ts

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

@github-actions github-actions Bot added the bug Something isn't working label Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@lidge-jun

Copy link
Copy Markdown
Owner

Maintainer integration into dev under MAINTAINERS.md (not an approving review), 2026-11-17 triage round.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants