Skip to content

fix(routing): tolerate malformed historical attempts - #1187

Draft
luvs01 wants to merge 2 commits into
lidge-jun:devfrom
luvs01:agent/fix-routing-analytics-malformed-attempts
Draft

fix(routing): tolerate malformed historical attempts#1187
luvs01 wants to merge 2 commits into
lidge-jun:devfrom
luvs01:agent/fix-routing-analytics-malformed-attempts

Conversation

@luvs01

@luvs01 luvs01 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • tolerate malformed legacy attempts values in routing analytics;
  • validate each attempt and its recoveryKinds array before reading it;
  • preserve explicit HTTP 429 classification even when historical attempt metadata is malformed;
  • add raw JSONL regressions for missing, non-array, null, mixed-type, and malformed-429 historical fields.

Why

The usage log is append-only history and older or partially malformed rows may not satisfy the current TypeScript shape. The analytics reader parsed each row and then called .some() on attempts and recoveryKinds without runtime validation. A malformed historical row could therefore throw and make the read-only analytics endpoint unavailable.

The writer format is unchanged. This patch makes only the historical reader defensive and treats malformed recovery metadata as non-triggering rather than inventing a cooldown signal. An explicit HTTP 429 remains cooldown-triggering independently of malformed attempt metadata.

Verification

  • Bun 1.3.14: tests/routing-analytics.test.ts 11/11 passed, 47 assertions.
  • Bun 1.4.0-canary.1 (5f65d3785): the same suite 11/11 passed, 47 assertions.
  • bun x tsc --noEmit: passed.
  • bun scripts/privacy-scan.ts: passed.
  • git diff --check: passed.
  • A full-suite run was not performed on this refreshed head; this PR does not claim a green full-suite result.

Checklist

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

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.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of malformed historical usage data.
    • Prevented invalid attempt records from affecting request totals or cooldown-triggering failure counts.
    • Preserved existing HTTP 429 handling behavior.
  • Tests

    • Added coverage for malformed usage records and their impact on routing analytics.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Routing analytics now validates historical attempts and recoveryKinds data before cooldown classification. Tests cover malformed entries and confirm that request totals and HTTP 429 handling remain unchanged.

Changes

Routing analytics hardening

Layer / File(s) Summary
Validate historical attempt data
src/routing/analytics.ts
At lines 14 and 147–155, analytics removes the unused import and validates attempt arrays and recovery-kind values before cooldown classification. HTTP 429 classification remains unchanged.
Cover malformed log entries
tests/routing-analytics.test.ts
At lines 2–10 and 228–253, tests write malformed log entries and verify five requests are counted while only the explicit HTTP 429 failure triggers cooldown classification.

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

Possibly related PRs

Suggested reviewers: wibias, ingwannu, lidge-jun

🚥 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: handling malformed historical attempts in routing analytics.
✨ 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 7, 2026

Copy link
Copy Markdown
Contributor

⏳ DRAFT

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

What to do

  • Tick all four boxes in the PR description once you're done (currently 1/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.

1/4 boxes ticked.

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

@github-actions

github-actions Bot commented Aug 7, 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 7, 2026
@luvs01

luvs01 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@lidge-jun
lidge-jun marked this pull request as ready for review August 8, 2026 09:16
@github-actions
github-actions Bot marked this pull request as draft August 8, 2026 09:17
@luvs01
luvs01 force-pushed the agent/fix-routing-analytics-malformed-attempts branch from 8b413ac to 36cffce Compare August 8, 2026 10:02
@luvs01

luvs01 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@lidge-jun

Copy link
Copy Markdown
Owner

Approved the pending Cross-platform CI run at this exact head so it can actually execute — action_required runs are invisible to gh pr checks, so from your side it just looked like nothing was happening.

To be clear about what that does and doesn't do: it only unblocks CI. It does not make this mergeable. The four-box readiness checklist is your attestation, not something a maintainer ticks for you, and the gate keeps the PR in draft until you complete it. Once CI comes back green, tick the boxes and it moves to ready on its own.

If the run comes back red, tell me and I'll dig into it rather than leaving you to guess.

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