Skip to content

fix(session): treat unknown finishReason as terminal in prompt loop - #19342

Closed
okuyam2y wants to merge 1 commit into
anomalyco:devfrom
okuyam2y:fix/unknown-finish-reason
Closed

okuyam2y wants to merge 1 commit into
anomalyco:devfrom
okuyam2y:fix/unknown-finish-reason

Conversation

@okuyam2y

Copy link
Copy Markdown

Issue for this PR

Closes #19339

Type of change

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

What does this PR do?

Providers like GLM-5 Turbo (via OpenAI-compatible gateways) return finishReason: "unknown" instead of "stop". The prompt loop treated "unknown" as non-terminal alongside "tool-calls", causing an infinite loop when no tool calls were present.

This PR removes "unknown" from the non-terminal list so only "tool-calls" keeps the loop running. The check is extracted into isTerminalFinishReason() to make the invariant explicit and testable.

Note: the TUI has a similar pattern in session/index.tsx — will address in a follow-up.

How did you verify your code works?

  • Reproduced the infinite loop with GLM-5 Turbo (finishReason: "unknown", step count reaching 100+)
  • After fix, session exits cleanly on first turn
  • bun test test/session/finish-reason.test.ts — 9 tests pass
  • bun typecheck passes

Checklist

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

Some providers (e.g. GLM-5 Turbo via OpenAI-compatible gateways)
return finishReason "unknown" instead of "stop" or "end_turn".
The prompt loop previously treated "unknown" as non-terminal alongside
"tool-calls", causing an infinite loop when no tool calls were present.

Extract isTerminalFinishReason() so the invariant — only "tool-calls"
keeps the loop running — is explicit and testable.
@okuyam2y
okuyam2y force-pushed the fix/unknown-finish-reason branch from 56319fa to 9e47961 Compare April 6, 2026 14:24
@okuyam2y

okuyam2y commented Apr 6, 2026

Copy link
Copy Markdown
Author

Rebased onto latest dev. 9 finish-reason tests pass, typecheck clean.

The upstream code still has !["tool-calls", "unknown"].includes(...) at one call site (line 1530 in current dev). This PR extracts isTerminalFinishReason() and replaces both sites, so "unknown" is treated as terminal — fixing the infinite loop reported in #19339.

@rekram1-node

Copy link
Copy Markdown
Contributor

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: infinite loop when provider returns finishReason "unknown"

2 participants