Skip to content

fix(ai): report compacted model context - #312

Open
arhxam wants to merge 4 commits into
getopenscreen:mainfrom
arhxam:codex/accurate-compacted-context-budget
Open

fix(ai): report compacted model context#312
arhxam wants to merge 4 commits into
getopenscreen:mainfrom
arhxam:codex/accurate-compacted-context-budget

Conversation

@arhxam

@arhxam arhxam commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • feed the chat context pill from the existing native chatBudget endpoint, which measures the messages actually sent to the model
  • retain the renderer transcript estimate as an immediate/browser/error fallback
  • refresh usage whenever the active session's visible message array changes, including after manual compaction
  • ignore late budget responses from a previously selected session

This is a follow-up to #238: preserving the complete transcript was correct, but it meant the transcript-based meter no longer reflected the compacted model payload.

Related issue

Related to #238

Type of change

  • Bug fix
  • Feature
  • Enhancement
  • Documentation
  • Refactor / maintenance
  • Performance
  • Security

Release impact

  • Patch
  • Minor
  • Major / breaking change
  • No release note needed

Desktop impact

  • Windows
  • macOS
  • Linux
  • Installer / packaging
  • Not platform-specific

Screenshots / video

Not included; the context pill retains its existing UI and now receives the correct post-compaction value.

Testing

  • npx vitest --run src/components/ai-edition/useChatBudget.test.ts (3 passed)
    • renderer estimate before native usage arrives
    • stale response ignored after session switch
    • unchanged visible transcript refreshes from 500 to 40 model tokens after compaction
  • npm run test (1,680 passed, 1 skipped across 141 files)
  • npx tsc --noEmit
  • npx tsc -p tsconfig.test.json --noEmit
  • npx biome check on all four changed files
  • npm run docs:check
  • npm run i18n:check
  • npm run build-vite

Authored with Codex assistance and manually verified against the native modelMessages(session) budget path.

Summary by CodeRabbit

  • New Features

    • Chat context usage now uses the most accurate available budget information.
    • Added fallback estimates when native usage data is unavailable or delayed.
    • Budget information stays synchronized when switching sessions or updating transcripts.
  • Bug Fixes

    • Prevented outdated budget responses from affecting the active chat session.
  • Tests

    • Added coverage for fallback estimates, session changes, transcript updates, and native data errors.

@arhxam
arhxam requested a review from EtienneLescot as a code owner August 8, 2026 18:35
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@arhxam, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 40 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 31376764-ab40-4021-bcca-1f57a65fcae2

📥 Commits

Reviewing files that changed from the base of the PR and between 026d405 and 76331d6.

📒 Files selected for processing (1)
  • src/components/ai-edition/useChatBudget.test.ts
📝 Walkthrough

Walkthrough

The chat budget meter now uses useChatBudget to combine native session usage with renderer estimation. The hook handles asynchronous responses, stale sessions, missing data, and errors. Tests cover transcript and session updates.

Changes

Chat budget integration

Layer / File(s) Summary
Budget fallback contract
src/components/ai-edition/chatBudget.ts
The renderer helpers document fallback behavior, export RenderableChatMessage, and accept readonly message arrays.
Native budget hook
src/components/ai-edition/useChatBudget.ts
useChatBudget computes a fallback estimate, requests native usage, ignores stale responses, and returns fallback data when native data is unavailable.
Meter integration and validation
src/components/ai-edition/LeftPanel.tsx, src/components/ai-edition/useChatBudget.test.ts
LeftPanel uses the hook. Tests cover initial estimates, native updates, unavailable data, rejected requests, stale sessions, and transcript changes.

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

Sequence Diagram(s)

sequenceDiagram
  participant LeftPanel
  participant useChatBudget
  participant MainProcessChatBudget
  LeftPanel->>useChatBudget: Pass project, session, and messages
  useChatBudget->>useChatBudget: Compute renderer fallback
  useChatBudget->>MainProcessChatBudget: Request native budget
  MainProcessChatBudget-->>useChatBudget: Return usage or unavailable result
  useChatBudget-->>LeftPanel: Return native usage or fallback budget
Loading

Possibly related PRs

Suggested reviewers: etiennelescot

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main bug fix: reporting compacted model context in the AI context meter.
Description check ✅ Passed The description covers the summary, related issue, change type, release impact, desktop impact, screenshots, and detailed testing.
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 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.

@arhxam

arhxam commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

The earlier automated review was rate-limited; requesting a fresh substantive review now that the cooldown has elapsed.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

@arhxam I will perform a fresh substantive review of #312.

✅ 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 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/components/ai-edition/useChatBudget.test.ts`:
- Around line 27-105: Add tests in the useChatBudget suite covering the
no-session path, an undefined native budget result, and a rejected chatBudget
call. For each scenario, render useChatBudget with an appropriate setup and
assert usedTokens remains the renderer estimate, including handling the rejected
promise without replacing it with native usage.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: fe59ab43-48f5-4885-84be-fe1e5958669c

📥 Commits

Reviewing files that changed from the base of the PR and between 4e7a85b and e12bbab.

📒 Files selected for processing (4)
  • src/components/ai-edition/LeftPanel.tsx
  • src/components/ai-edition/chatBudget.ts
  • src/components/ai-edition/useChatBudget.test.ts
  • src/components/ai-edition/useChatBudget.ts

Comment thread src/components/ai-edition/useChatBudget.test.ts
@arhxam

arhxam commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the fresh review in 026d405: added no-session, undefined-native-budget, and rejected-native-budget fallback tests. Biome format/check and git diff --check pass; exact-head CI is running.

@arhxam

arhxam commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up test harness fix is now on exact head 76331d6: the rejection case first observes native usage, then verifies a rejected refresh falls back to the changed transcript estimate, so the catch path is awaited without leaving an artificial unhandled rejection. Targeted test: 6/6 passed locally. Exact-head CI is fully green, including the complete 1,705-test suite and all Rust/platform jobs.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant