Skip to content

fix: merge per-token reasoning chunks into single part for GLM models - #22240

Closed
bamboodew wants to merge 1 commit into
anomalyco:devfrom
bamboodew:fix/reasoning-per-token-merge
Closed

bamboodew wants to merge 1 commit into
anomalyco:devfrom
bamboodew:fix/reasoning-per-token-merge

Conversation

@bamboodew

@bamboodew bamboodew commented Apr 13, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #22241

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Some models (e.g., zhipu/glm) emit a separate reasoning-start → reasoning-delta → reasoning-end cycle for every single token during reasoning/thinking output. This causes each token to create an independent reasoning Part with a unique partID, resulting in the TUI rendering each token as a separate Thinking: line:

Thinking: The
Thinking: user
Thinking: said
Thinking: hello

Root cause: The AI SDK adapter for these models wraps each token in its own reasoning lifecycle event, unlike models such as DeepSeek which emit one reasoning-start, many reasoning-deltas, and one reasoning-end.

Fix in processor.ts: Track the last reasoning part via _lastReasoningEntry. When a new reasoning-start arrives and a previous reasoning part exists, reuse it instead of creating a new one. reasoning-end no longer finalizes the part — it just cleans up the map entry. The merged part is finalized when text-start or finish-step fires (via finalizeReasoning()).

Defense-in-depth in index.tsx: Normalize newlines in ReasoningPart's createMemo — collapse single \n (token boundaries) to spaces while preserving \n\n (paragraph breaks).

How did you verify your code works?

  • Tested with zhipu/glm-5 via Mify gateway — thinking content now renders as a continuous paragraph
  • Three-layer debug logging (processor → sync → render) confirmed all deltas now share the same partID
  • DeepSeek thinking output unaffected (single start/end cycle works as before)

Screenshots / recordings

N/A — terminal behavior fix, not UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Apr 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Apr 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Some models (e.g., zhipu/glm) emit a separate reasoning-start/delta/end
cycle for every single token, causing each token to render as an
independent 'Thinking:' line in the TUI.

Fix by tracking the last reasoning part and reusing it when consecutive
reasoning-start events arrive, instead of creating a new part each time.
The merged part is finalized at text-start or finish-step.

Also normalize newlines in ReasoningPart render as defense-in-depth.
@rekram1-node

Copy link
Copy Markdown
Collaborator

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TUI renders each reasoning token on a separate line for models with per-token reasoning cycles

2 participants