Skip to content

feat(native): keep conversation history within a token budget - #79

Merged
rferrari merged 2 commits into
rferrari:fullnative-devfrom
bgrana75:feat/native-history-budget
Oct 10, 2026
Merged

rferrari merged 2 commits into
rferrari:fullnative-devfrom
bgrana75:feat/native-history-budget

Conversation

@bgrana75

Copy link
Copy Markdown

Keep conversation history within a token budget

Builds on #76 (CI); the last commit is this PR's. Finding 5 of the review.

ChatViewModel sent the last 6 messages verbatim (VERBATIM_MESSAGE_COUNT). With detailed answers (~500 tokens each) that reached ~1,700 tokens. The history sits after the retrieved sources in the prompt (RagPure.assembleChatMessages), and the sources change every question, so all of it was read again for every question. In the Expo app the same history took a follow-up's first word from 1.6 s (new chat) to 20–48 s (Pixel 6a, 1.5B, Oct 3).

HistoryBudget (engine, answer/) ports src/routing/historyBudget.ts from #72:

  • newest turns first, within 400 tokens;
  • questions kept whole, earlier answers cut to their opening sentences (80 tokens), with " …";
  • the latest exchange always kept, even over budget.

It's used in two places in ChatViewModel: the answer's history, and the summary's input (the older turns, which could be thousands of tokens).

Golden test. src/routing/historyBudget.ts and its vitest tests come from #72 unchanged, since fullnative-dev doesn't have them. scripts/export-native-golden-history.mjs runs the real TS and writes golden/history-budget.json:

  • 438 leadSentences cases: 60 corpus passages and 13 edge cases (abbreviations, decimals, quotes, newlines, Portuguese, one huge sentence), each at caps of 0–200 tokens;
  • 34 budgetHistory cases: 17 conversations at the default and at a tight budget, 8 of which drop turns.

HistoryBudgetGoldenTest checks the Kotlin output matches exactly. The Kotlin uses Compress.splitSentences and Compress.approxTokens, which port the same functions (the splitSentences in src/rag/compress.ts and src/routing/context.ts are character-for-character identical, as ResearchContext already relies on).

Not run locally. No Android toolchain here, so CI (#76) is the first place the Kotlin compiles and the golden test runs; the TS tests pass locally (7/7). Not measured on a phone. No CodeRabbit CLI review was run before pushing (cr isn't installed here).

Touches ChatViewModel.kt at the history and summary lines only, not at send()'s start (#78) or preload().

ChatViewModel sent the last 6 messages verbatim. With detailed answers that reached ~1,700 tokens,
read again for every question, since the history comes after the retrieved sources in the prompt.

HistoryBudget ports src/routing/historyBudget.ts from rferrari#72 (brought here with its vitest tests):
newest turns first within 400 tokens, questions whole, earlier answers cut to their opening
sentences (80 tokens), the latest exchange always kept. Used for the answer's history and for the
summary's input. Golden test against the TypeScript: scripts/export-native-golden-history.mjs
(438 leadSentences cases on corpus passages and edge cases, 34 conversations at two budgets).
@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Review in Change Stack →Review in Change Stack →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 421c7433-8ccc-472b-932d-1865a3d579cf

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR


🧪 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

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

@rferrari

Copy link
Copy Markdown
Owner

Approve. The golden file regenerates byte for byte from the TS, and HistoryBudgetGoldenTest
passes on the merged branch. One difference from #72: the budget applies to the last 6
messages rather than the whole history. That's stricter, and fine with the 400-token
budget. Bounding the summary's input too is right; a long detailed chat was the slow case.

@bgrana75

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

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.

@bgrana75

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Head commit changed.

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.

@rferrari
rferrari merged commit 498504d into rferrari:fullnative-dev Oct 10, 2026
3 checks passed
@bgrana75

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Pull request is closed.

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.

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.

2 participants