Skip to content

fix(chat): reasoning_content parsing, image passthrough, model-change race - #104

Merged
ddutchie merged 3 commits into
mainfrom
ddutchie/qwenreasoning
Jul 30, 2026
Merged

ddutchie merged 3 commits into
mainfrom
ddutchie/qwenreasoning

Conversation

@ddutchie

@ddutchie ddutchie commented Jul 30, 2026 •

Copy link
Copy Markdown
Owner

What does this PR do?

Fixes three chat issues on desktop: (1) Qwen/DeepSeek/GLM-style reasoning is now parsed from reasoning_content and shown in the thinking panel; (2) attached images are always forwarded to the model instead of being stripped by a hardcoded vision allow-list; and (3) a stale-cache race that made a model change require clearing the chat twice.

Type of change

  • Bug fix
  • New feature
  • Refactor / code quality
  • Docs / changelog
  • Tests

Screenshots / recording

Not applicable — no UI changes.

Details

1. reasoning_content parsing (Qwen/DeepSeek/GLM)
The desktop stream parsers only read delta.reasoning, but these models emit thinking on delta.reasoning_content (and message.reasoning_content on non-streaming responses). Now reads reasoning_content ?? reasoning in:

  • electron/lib/chat-loop.ts — streaming + non-stream
  • electron/lib/pi-agent-loop.ts — streaming
  • electron/lib/chat-subagent-loop.ts — non-stream dispatch

Both reasoning and reasoning_content are stripped before assistant messages are re-sent (re-sending reasoning_content triggers 400s on DeepSeek). Matches the existing mobile behaviour in mobile/src/chat/providers/openai.ts.

2. Image passthrough
electron/ipc/chat.ts previously guessed vision support from a hardcoded model-name allow-list (gpt-4o, claude-3, gemini, …) and silently dropped images for anything else — including custom OpenAI-compatible endpoints and vision-capable on-device models like Gemma. The gate is removed; images are always forwarded and the model decides. The llama.cpp local router forwards content verbatim, so vision-capable on-device models get the image_url parts. Images now also flow through the subagent dispatch path, which previously dropped them.

3. Model-change race ("clear chat twice")
A refresh hydrateFromElectron (fired after write-tool turns and every db:changed) read the backend settings cache and layered it over localStorage. Because the model-change write is fire-and-forget, a refresh firing before it landed reverted the selection. On refresh hydrates the live in-memory aiConfig now takes precedence over the backend cache (src/store/index.ts); the initial hydrate still trusts the cache.

Checklist

  • npm run type-check:all passes
  • npm run lint passes
  • npm test passes (runs npm run compile first so electron/bundle-guard.test.ts actually executes — npm run test:bundle to run just that)
  • npm run test:e2e passes (run before merging UI changes or cutting a release)
  • If this ships a major, user-facing feature: N/A — bug fixes only, no "What's New" entry
  • No hardcoded colours — CSS variables only (no styling changes)
  • No text-[Npx] pixel font classes (no styling changes)
  • New IPC handlers wrapped in handle() and return IpcResult<T> — N/A, no new handlers
  • New DB migrations appended (not edited) in schema.ts — N/A
  • New SQL goes in electron/db/queries.ts — N/A
  • New dependencies/devDependencies — N/A
  • New MCP tools registered — N/A
  • --external:<pkg> compile flags — unchanged

Notes for reviewer

  • The vision gate is now fully permissive for all providers, including on-device. A text-only model will just respond that it can't see the image rather than Cairn pre-filtering it. This is intentional so custom endpoints and vision-capable on-device models (Gemma) work.
  • The model-change fix changes refresh-hydrate precedence: the in-memory session config now beats the backend cache on refresh. This means a settings change made in another instance won't be picked up mid-session by a db:changed refresh — an acceptable trade for fixing the revert bug, since the initial hydrate still reads the cache.
  • npm test / npm run test:e2e not run locally in this session — please confirm in CI.

Summary by CodeRabbit

  • Bug Fixes

    • Image attachments are now sent reliably in chats and subagent interactions.
    • Switching chat models now applies to the next message without reverting to older settings.
    • Reasoning and “thinking” text from more AI models now appears in the desktop thinking panel.
    • Prevented reasoning metadata from causing message submission errors.
    • Improved token counting for image attachments, preventing large image data from inflating context usage.
  • Tests

    • Added coverage for accurate token accounting with image and text content.

… race

- Parse delta.reasoning_content ?? delta.reasoning in the desktop chat loop,
  coding-agent loop, and subagent dispatch loop (streaming + non-stream), and
  strip reasoning_content before resend so Qwen/DeepSeek/GLM thinking shows in
  the panel and doesn't trigger 400s on resend.
- Always forward attached images to the model instead of guessing vision
  support from a hardcoded model-name allow-list; images now also flow through
  the subagent dispatch path. Fixes images being silently dropped for custom
  endpoints and vision-capable on-device models (e.g. Gemma).
- Prefer the live in-memory aiConfig over the backend cache on refresh
  hydrates so a model change applies to the next message (no "clear twice").
@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

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

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

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: fda124de-2301-43d1-b2fa-6cff8f601165

📥 Commits

Reviewing files that changed from the base of the PR and between 4ca5be5 and 599118f.

📒 Files selected for processing (1)
  • electron/lib/token-breakdown-images.test.ts
📝 Walkthrough

Walkthrough

Changes

Chat runtime fixes

Layer / File(s) Summary
Forward images and count multimodal content
electron/ipc/chat.ts, electron/lib/chat-subagent-loop.ts, electron/lib/llm.ts, electron/lib/token-breakdown-images.test.ts, changelogs/v2.5.17.md
Chat and subagent paths forward image attachments, while token accounting uses fixed image estimates and continues counting text parts with dedicated tests.
Handle provider reasoning output
electron/lib/chat-loop.ts, electron/lib/chat-subagent-loop.ts, electron/lib/pi-agent-loop.ts
Reasoning is read from reasoning_content or reasoning, emitted to thinking displays, and removed before assistant messages are resent.
Prefer live configuration during refresh
src/store/index.ts
Refresh hydration incorporates the current in-memory AI configuration when merging persisted settings.

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

Possibly related PRs

  • ddutchie/cairn#63: Earlier changes to multimodal image forwarding in the chat IPC path.
  • ddutchie/cairn#90: Earlier subagent and tool-loop changes in the chat runtime paths.
  • ddutchie/cairn#91: Related AI configuration hydration and refresh-merging changes.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly captures the three main fixes: reasoning_content parsing, image passthrough, and the model-change refresh race.
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 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ddutchie/qwenreasoning

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.

calculatePromptBreakdown stringified the whole multimodal message content,
tokenising the image's base64 data URL and inflating the conversation count /
context ring by orders of magnitude. Count only text parts and add a flat
per-image estimate instead, matching mobile's messageText() behaviour.
@ddutchie

Copy link
Copy Markdown
Owner Author

@coderabbitai please review

@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

@ddutchie I’ll review the changes in #104, with particular attention to provider-response compatibility, image content propagation, and refresh-state precedence.

✅ 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

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 `@electron/lib/token-breakdown-images.test.ts`:
- Around line 51-64: Update the test around calculatePromptBreakdown to compare
the image-bearing message with an equivalent text-only message, and assert that
the image-bearing conversation count is greater by the expected nonzero flat
image cost. Preserve the existing payload-size comparison so the test verifies
both nonzero contribution and independence from base64 payload length.
🪄 Autofix (Beta)

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: 76555151-886e-4b78-b1b9-96c65067790d

📥 Commits

Reviewing files that changed from the base of the PR and between c47b807 and 4ca5be5.

📒 Files selected for processing (8)
  • changelogs/v2.5.17.md
  • electron/ipc/chat.ts
  • electron/lib/chat-loop.ts
  • electron/lib/chat-subagent-loop.ts
  • electron/lib/llm.ts
  • electron/lib/pi-agent-loop.ts
  • electron/lib/token-breakdown-images.test.ts
  • src/store/index.ts

Comment thread electron/lib/token-breakdown-images.test.ts Outdated
@ddutchie
ddutchie merged commit 3dd34a7 into main Jul 30, 2026
6 checks passed
@ddutchie
ddutchie deleted the ddutchie/qwenreasoning branch July 30, 2026 13:07
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