Skip to content

fix: improve context budget and streaming#1605

Merged
zerob13 merged 1 commit intodevfrom
fix/content-length
May 11, 2026
Merged

fix: improve context budget and streaming#1605
zerob13 merged 1 commit intodevfrom
fix/content-length

Conversation

@zerob13
Copy link
Copy Markdown
Collaborator

@zerob13 zerob13 commented May 11, 2026

Summary

  • Add context-budget diagnostics to overflow errors and trigger recovery for unrecoverable preflight pressure.
  • Add an explicit smoothStreaming prop for markdown rendering and enable it only while message blocks are generating.
  • Document both fixes with SDD notes and expand main/renderer test coverage.

UI Structure

BEFORE

MessageBlockContent
└─ MarkdownRenderer
   └─ NodeRenderer streaming behavior not controlled by block status

AFTER

MessageBlockContent pending/loading
└─ MarkdownRenderer smoothStreaming=true

MessageBlockContent completed
└─ MarkdownRenderer smoothStreaming=false

Tests

  • pnpm run format
  • pnpm run i18n
  • pnpm run lint
  • commit hook: pnpm run typecheck

Summary by CodeRabbit

  • New Features
    • Enhanced error handling when requests exceed model context limits, providing actionable guidance to reduce input size or adjust output settings
    • Smooth streaming control for markdown rendering: completed messages display without animation while actively generating content streams smoothly

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 11, 2026

📝 Walkthrough

Walkthrough

This PR adds two independent features: (1) retry overflow hardening with context-budget diagnostics for requests that exceed the model window, redirecting unfittable preflight results through recovery before provider calls and formatting actionable error messages; and (2) markdown smooth-streaming control via a new smoothStreaming prop in MarkdownRenderer, enabled only for actively generating assistant blocks (pending/loading status) and disabled for completed content and preview modes.

Changes

Retry Overflow Hardening

Layer / File(s) Summary
Documentation
docs/issues/agent-tool-context-budget/plan.md, docs/issues/agent-tool-context-budget/spec.md, docs/issues/agent-tool-context-budget/tasks.md
Plan specifies unfittable preflight results route through existing recovery path once, protecting latest payloads and allowing history/prompt compaction and output-cap reduction; spec requires retry/resume calls to run recovery pass even with fewer output tokens, failing before provider calls if recovery cannot fit; tasks mark recovery and actionable diagnostics as complete.
Budget Diagnostics Type & Helpers
src/main/presenter/agentRuntimePresenter/contextBudget.ts
New RequestContextBudgetDiagnostics type captures usable context, input/tool/output token estimates, and remaining output room; buildRequestContextBudgetDiagnostics transforms preflight results into diagnostics shape; buildRequestContextOverflowErrorMessage formats user-facing overflow error with remediation guidance (shorten input, reduce tools/system prompt, lower max tokens, increase context length).
Presenter Recovery & Error Handling
src/main/presenter/agentRuntimePresenter/index.ts
Imports buildRequestContextOverflowErrorMessage; broadens context-pressure recovery trigger to run when preflight either requires recovery or request does not fit within context; after recovery re-run, throws formatted overflow error instead of generic string if request still cannot fit.
Tests & Test Helpers
test/main/presenter/agentRuntimePresenter/agentRuntimePresenter.test.ts, test/main/presenter/agentRuntimePresenter/contextBudget.test.ts
Adds factory helpers for constructing persisted DB message rows; introduces retry context overflow recovery test suite verifying recovery succeeds with conversation summary prompt and fails with stored budget-error when retry input itself cannot fit; validates diagnostics formatting and overflow error message contains remediation suggestions.

Markdown Smooth Streaming Control

Layer / File(s) Summary
Documentation
docs/issues/markdown-smooth-streaming-control/plan.md, docs/issues/markdown-smooth-streaming-control/spec.md, docs/issues/markdown-smooth-streaming-control/tasks.md
Plan specifies smoothStreaming prop defaults to false, forwarded to markstream-vue NodeRenderer, enabled only for chat assistant blocks with pending/loading status; spec defines user story for keeping completed markdown stable without streaming animation; tasks mark prop addition, state-specific behavior, and renderer tests as complete.
MarkdownRenderer Prop Definition
src/renderer/src/components/markdown/MarkdownRenderer.vue
Adds optional smoothStreaming prop (defaults to false via withDefaults); passes it to NodeRenderer via :smooth-streaming binding and explicitly sets :fade="false".
MessageBlockContent Integration
src/renderer/src/components/message/MessageBlockContent.vue
Computes shouldSmoothStream (true when props.block.status is 'pending' or 'loading'); binds it to MarkdownRenderer via :smooth-streaming="shouldSmoothStream" to control streaming animation based on message generation state.
Tests & Mocks
test/renderer/components/MarkdownRenderer.test.ts, test/renderer/components/message/MessageBlockContent.test.ts
Updates MarkdownRenderer and MessageBlockContent test mocks to expose smoothStreaming via data-smooth-streaming attribute; verifies smooth streaming is disabled by default, enabled when prop is true, disabled for completed blocks, and enabled for pending/loading blocks.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ThinkInAIXYZ/deepchat#1551: Introduces the foundational request preflight/fitting and presenter recovery logic that this PR builds upon with diagnostics and overflow error messaging.
  • ThinkInAIXYZ/deepchat#1600: Modifies the same context-budget and presenter flow to extend recovery and error handling with diagnostics and refined preflight-recovery trigger conditions.
  • ThinkInAIXYZ/deepchat#1602: Modifies markdown streaming behavior in the same MarkdownRenderer and MessageBlockContent components, affecting streaming control for message blocks.

Suggested reviewers

  • yyhhyyyyyy

Poem

🐰 A rabbit hops through context and streams so fine,
Overflow curves with diagnostics to guide the line.
Smooth markdown glows when pending, then rests when done,
Budget wisdom flows where recovery has begun! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

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.
Title check ❓ Inconclusive The title 'fix: improve context budget and streaming' is vague and overly broad, using non-descriptive terms like 'improve' that do not convey specific information about what was fixed or changed. Provide a more specific title that identifies the primary change, such as 'fix: add context budget diagnostics and control markdown streaming' or narrow to one main focus area.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 fix/content-length

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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/main/presenter/agentRuntimePresenter/contextBudget.ts (1)

35-43: ⚡ Quick win

Consider deriving RequestContextBudgetDiagnostics from RequestContextPreflightResult via Pick<>.

The new type is structurally a strict subset of RequestContextPreflightResult. Using Pick<> keeps the two in sync if preflight result fields are renamed or retyped later, and tightens buildRequestContextBudgetDiagnostics so the compiler enforces the mapping.

♻️ Proposed refactor
-export type RequestContextBudgetDiagnostics = {
-  usableContextLength: number
-  inputTokens: number
-  toolReserveTokens: number
-  requestedMaxTokens: number
-  effectiveMaxTokens: number
-  remainingOutputTokens: number
-  totalRequestTokens: number
-}
+export type RequestContextBudgetDiagnostics = Pick<
+  RequestContextPreflightResult,
+  | 'usableContextLength'
+  | 'inputTokens'
+  | 'toolReserveTokens'
+  | 'requestedMaxTokens'
+  | 'effectiveMaxTokens'
+  | 'remainingOutputTokens'
+  | 'totalRequestTokens'
+>
 export function buildRequestContextBudgetDiagnostics(
   preflight: RequestContextPreflightResult
 ): RequestContextBudgetDiagnostics {
-  return {
-    usableContextLength: preflight.usableContextLength,
-    inputTokens: preflight.inputTokens,
-    toolReserveTokens: preflight.toolReserveTokens,
-    requestedMaxTokens: preflight.requestedMaxTokens,
-    effectiveMaxTokens: preflight.effectiveMaxTokens,
-    remainingOutputTokens: preflight.remainingOutputTokens,
-    totalRequestTokens: preflight.totalRequestTokens
-  }
+  const {
+    usableContextLength,
+    inputTokens,
+    toolReserveTokens,
+    requestedMaxTokens,
+    effectiveMaxTokens,
+    remainingOutputTokens,
+    totalRequestTokens
+  } = preflight
+  return {
+    usableContextLength,
+    inputTokens,
+    toolReserveTokens,
+    requestedMaxTokens,
+    effectiveMaxTokens,
+    remainingOutputTokens,
+    totalRequestTokens
+  }
 }

Also applies to: 208-220

🤖 Prompt for 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.

In `@src/main/presenter/agentRuntimePresenter/contextBudget.ts` around lines 35 -
43, The RequestContextBudgetDiagnostics type should be derived from
RequestContextPreflightResult using TypeScript's Pick<> so it stays in sync and
the compiler enforces field mapping; replace the explicit object type for
RequestContextBudgetDiagnostics with a type alias like
Pick<RequestContextPreflightResult, 'usableContextLength' | 'inputTokens' |
'toolReserveTokens' | 'requestedMaxTokens' | 'effectiveMaxTokens' |
'remainingOutputTokens' | 'totalRequestTokens'> and update the corresponding
buildRequestContextBudgetDiagnostics mapping to reflect the new type; do the
same refactor for the similar type declared around lines 208-220 so both are
defined via Pick<> from RequestContextPreflightResult.
🤖 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.

Nitpick comments:
In `@src/main/presenter/agentRuntimePresenter/contextBudget.ts`:
- Around line 35-43: The RequestContextBudgetDiagnostics type should be derived
from RequestContextPreflightResult using TypeScript's Pick<> so it stays in sync
and the compiler enforces field mapping; replace the explicit object type for
RequestContextBudgetDiagnostics with a type alias like
Pick<RequestContextPreflightResult, 'usableContextLength' | 'inputTokens' |
'toolReserveTokens' | 'requestedMaxTokens' | 'effectiveMaxTokens' |
'remainingOutputTokens' | 'totalRequestTokens'> and update the corresponding
buildRequestContextBudgetDiagnostics mapping to reflect the new type; do the
same refactor for the similar type declared around lines 208-220 so both are
defined via Pick<> from RequestContextPreflightResult.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fdad6ea8-1e3b-4a1f-9b2a-53f93332a44b

📥 Commits

Reviewing files that changed from the base of the PR and between 106ac38 and f37c3c9.

📒 Files selected for processing (14)
  • docs/issues/agent-tool-context-budget/plan.md
  • docs/issues/agent-tool-context-budget/spec.md
  • docs/issues/agent-tool-context-budget/tasks.md
  • docs/issues/markdown-smooth-streaming-control/plan.md
  • docs/issues/markdown-smooth-streaming-control/spec.md
  • docs/issues/markdown-smooth-streaming-control/tasks.md
  • src/main/presenter/agentRuntimePresenter/contextBudget.ts
  • src/main/presenter/agentRuntimePresenter/index.ts
  • src/renderer/src/components/markdown/MarkdownRenderer.vue
  • src/renderer/src/components/message/MessageBlockContent.vue
  • test/main/presenter/agentRuntimePresenter/agentRuntimePresenter.test.ts
  • test/main/presenter/agentRuntimePresenter/contextBudget.test.ts
  • test/renderer/components/MarkdownRenderer.test.ts
  • test/renderer/components/message/MessageBlockContent.test.ts

@zerob13 zerob13 merged commit cf1fe6e into dev May 11, 2026
3 checks passed
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