Skip to content

fix(history): stream request-history index ingestion (#1189) - #1287

Merged
lidge-jun merged 1 commit into
devfrom
codex/260808-1189-history-stream-ingest
Aug 8, 2026
Merged

fix(history): stream request-history index ingestion (#1189)#1287
lidge-jun merged 1 commit into
devfrom
codex/260808-1189-history-stream-ingest

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Summary

Republishes @luvs01's #1189 on current dev. Their branch was 300 commits behind, so this is a maintainer rebase rather than a new change; the commit carries a Co-authored-by trailer and the fix is theirs.

The request-history indexer allocated size - indexedOffset bytes in one shot before parsing the appended tail, so a large append created a proportional transient allocation even though the SQLite index is a disposable projection. readCompleteTail is replaced by a reusable 64 KiB streaming reader that assembles complete JSONL records across chunk boundaries.

A complete record above 1 MiB is omitted from the projection only. usage.jsonl stays canonical and is never truncated or rewritten, indexedRows still counts successfully projected records, and the indexed offset only advances past a newline so a torn final record is re-read rather than skipped. insert.finalize() remains unconditional in finally — an unterminated prepared statement keeps the DB file busy on Windows after close.

Closes nothing on its own; supersedes #1189, which can be closed once this lands.

Verification

Rebased onto f5147cbc8 and re-verified after that rebase, not before it:

  • bun run test9991 pass / 7 skip / 0 fail across 625 files
  • bun test tests/request-history-index.test.ts — 20 pass / 0 fail
  • bun run typecheck — clean
  • Ablation: setting REQUEST_HISTORY_MAX_RECORD_BYTES to Number.MAX_SAFE_INTEGER fails exactly one test (19 pass / 1 fail); restoring the 1 MiB bound returns 20 pass. The bound is load-bearing, not decorative.
  • Contract check: rg 'ingestText|readCompleteTail' finds no caller outside src/routing/history/indexer.ts on dev, so removing them breaks nothing.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. (No user-facing behavior or configuration changed.)
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults. (No credential or request-body content is read or logged; the canonical log is never rewritten.)

Summary by CodeRabbit

  • Bug Fixes
    • Improved request history indexing for records split across read operations.
    • Large records are safely skipped while subsequent history entries continue to be processed.
    • Partial records are indexed only after they are complete, preventing duplicate ingestion.
    • Canonical usage logs remain unchanged during oversized-record handling.

Replaces the full-tail allocation in the request-history indexer with a
64 KiB streaming reader. The previous `readCompleteTail` allocated
`size - indexedOffset` bytes in one shot before parsing, so a large append
created a proportional transient allocation even though the SQLite index is
a disposable projection.

Records are now assembled across chunk boundaries, and a complete record
above 1 MiB is omitted from the projection only. `usage.jsonl` stays
canonical and is never truncated or rewritten; `indexedRows` still counts
successfully projected records, and the indexed offset only advances past a
newline so a torn final record is re-read rather than skipped.

`insert.finalize()` remains unconditional in `finally` — an unterminated
prepared statement keeps the DB file busy on Windows after close.

Republished from #1189 by luvs01, whose branch was 300 commits behind dev.
Rebased onto f5147cb with no conflicts; authorship preserved below.

Co-authored-by: luvs01 <27862058+luvs01@users.noreply.github.com>
@github-actions github-actions Bot added the bug Something isn't working label Aug 8, 2026
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 550a0991-9fd0-4629-bc9d-b1d898a5643f

📥 Commits

Reviewing files that changed from the base of the PR and between f5147cb and 02ec799.

📒 Files selected for processing (2)
  • src/routing/history/indexer.ts
  • tests/request-history-index.test.ts

📝 Walkthrough

Walkthrough

The request-history indexer now reads usage.jsonl in 64 KiB chunks, reconstructs records across chunk boundaries, skips complete records over 1 MiB, preserves byte offsets, and finalizes prepared statements during cleanup. Tests cover partial lines, oversized records, duplicate prevention, and canonical-log integrity.

Changes

Request-history streaming ingestion

Layer / File(s) Summary
Streaming limits and tail setup
src/routing/history/indexer.ts
The indexer exports chunk-size and maximum-record-size constants. It removes whole-tail loading and opens and stats the source directly before ingestion.
Chunk processing and validation
src/routing/history/indexer.ts, tests/request-history-index.test.ts
The indexer reads bounded buffers, reconstructs newline-delimited records, tracks byte-accurate offsets, skips oversized records, and finalizes prepared statements. Tests verify cross-chunk records, partial-line indexing, duplicate prevention, oversized-record handling, and canonical-log integrity.

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

Possibly related PRs

Suggested labels: bug

Suggested reviewers: ingwannu, wibias

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: streaming request-history index ingestion.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/260808-1189-history-stream-ingest

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02ec799fe1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +80 to +83
// The SQLite index is a disposable projection. Complete JSONL records above
// this bound are omitted from the projection; the canonical usage.jsonl is
// never truncated or rewritten by the indexer.
export const REQUEST_HISTORY_MAX_RECORD_BYTES = 1024 * 1024;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Disclose oversized records missing from request history

When usage.jsonl contains a complete valid record larger than 1 MiB, the indexer advances past it but permanently omits it from request history and routing analytics. This conflicts with docs-site/src/content/docs/reference/configuration/routing.md, which promises “full history,” and the returned metadata provides no truncation signal, so users can unknowingly make decisions from incomplete results. Document the limit and affected endpoints, or expose an explicit omission indicator.

AGENTS.md reference: src/AGENTS.md:L28-L28

Useful? React with 👍 / 👎.

@lidge-jun
lidge-jun merged commit 2cb8edd into dev Aug 8, 2026
23 checks passed
@lidge-jun
lidge-jun deleted the codex/260808-1189-history-stream-ingest branch August 8, 2026 13:23
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.

1 participant