Skip to content

fix(usage): bound startup hydration tail reads - #1256

Draft
luvs01 wants to merge 1 commit into
lidge-jun:devfrom
luvs01:agent/fix-usage-tail-bound
Draft

fix(usage): bound startup hydration tail reads#1256
luvs01 wants to merge 1 commit into
lidge-jun:devfrom
luvs01:agent/fix-usage-tail-bound

Conversation

@luvs01

@luvs01 luvs01 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • cap readRecentUsageEntries() startup hydration at a 64 MiB tail window;
  • preserve the existing adaptive growth within that bound;
  • stop cleanly when the bounded tail contains no complete line instead of expanding to the whole file;
  • add a sparse oversized-ledger regression proving rows outside the bounded tail are not allocated or parsed.

Why

readRecentUsageEntries() starts with a small tail window and multiplies it until enough valid rows are found. Before this change, the upper bound was the full usage.jsonl size. A large or malformed persisted row could therefore make every startup allocate and parse the complete ledger even though the caller only requests a small recent set.

The management/history paths already use separately bounded readers. Startup hydration should have the same finite-memory property.

Behavior

The reader still returns the newest valid entries that fit inside the retained window and still expands for trace-sized rows. It now stops at 64 MiB. If that bounded tail contains no newline, the function returns an empty recent set rather than attempting a whole-file read. The append-only ledger itself is not modified.

Verification

  • Bun 1.3.14: tests/usage-log.test.ts 30/30 passed.
  • Bun 1.4.0-canary.1 (b22e0e6d0): the same suite 30/30 passed.
  • bun x tsc --noEmit: passed.
  • bun scripts/privacy-scan.ts: passed.
  • git diff --check: passed.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Tests cover the bounded behavior and existing usage-log semantics.
  • No credential, routing, or user-facing configuration contract changes.

Ready for review; upstream CI and maintainer review remain pending.

Summary by CodeRabbit

  • Bug Fixes

    • Improved startup usage-log loading by limiting reads to a 64 MiB window.
    • Ensures the newest usage entries are returned without scanning excessively large logs.
  • Tests

    • Added regression coverage for bounded reads when older data exists beyond the limit.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • All CI tests are green on my local testing.

  • I pushed my PR to the latest dev commit.

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

readRecentUsageEntries now limits adaptive startup reads to 64 MiB. A regression test covers oversized sparse logs where valid data lies outside the bounded tail.

Changes

Recent usage read bounding

Layer / File(s) Summary
Cap adaptive usage-log reads
src/usage/log.ts
Adds a 64 MiB maximum and applies it to initial and expanded read windows. The function returns the newest parsed entries when the cap is reached.
Validate bounded-tail behavior
tests/usage-log.test.ts
Adds a regression test for an oversized sparse log with valid data outside the capped tail.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: lidge-jun, harryzhou2000, wibias

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: bounding startup usage hydration tail reads.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 8, 2026
@luvs01
luvs01 force-pushed the agent/fix-usage-tail-bound branch 2 times, most recently from 5011789 to 86f8160 Compare August 8, 2026 06:03
@luvs01
luvs01 marked this pull request as ready for review August 8, 2026 06:20
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@Ingwannu Ingwannu left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 86f81604. Startup hydration is now capped to a 64 MiB tail instead of expanding to an unbounded whole-file read, while preserving newest-valid-row parsing within that window. Focused coverage passed locally (30 tests), git diff --check passed, and typecheck passed. Core Linux/macOS/keyring/storage shards are green; the remaining npm-global/gates jobs are still queued, so do not merge until the exact head is fully green.

@luvs01
luvs01 force-pushed the agent/fix-usage-tail-bound branch from 86f8160 to f2ce1ea Compare August 8, 2026 07:02

@Ingwannu Ingwannu left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed rebased exact head f2ce1ea5. The PR diff is unchanged in scope from the approved bounded-tail patch. Exact-head focused tests passed 30/30 and typecheck passed. Do not merge until the newly triggered exact-head CI is green.

@luvs01
luvs01 force-pushed the agent/fix-usage-tail-bound branch from f2ce1ea to 7c40ac6 Compare August 8, 2026 09:04
@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

⏳ DRAFT

  • review readiness checklist open (0/4 boxes ticked).

What to do

  • Tick all four boxes in the PR description once you're done (currently 0/4).

Review readiness checklist

  • ⬜ All CI tests are green on my local testing.
  • ⬜ I pushed my PR to the latest dev commit.
  • ⬜ I resolved all correct Codex and CodeRabbit findings.
  • ⬜ My PR is ready for review.

0/4 boxes ticked.

This PR stays in draft until every box above is ticked.

@github-actions
github-actions Bot marked this pull request as draft August 8, 2026 09:04

luvs01 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Rebased exact head 7c40ac63 onto current dev (517f4460) without changing the two-file patch.

Focused verification on the rebased state:

  • Bun 1.3.14: tests/usage-log.test.ts 30/30 passed.
  • Bun 1.4.0-canary.1: 30/30 passed.
  • Typecheck, privacy scan, and git diff --check passed.

The branch is now exactly one commit ahead and zero behind dev; fresh exact-head upstream CI is required because the previous green run covered the prior base.

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