Skip to content

feat(agents): opencode stops a task at its max_turns - #706

Open
canersoz-scaleai wants to merge 2 commits into
scaleapi:mainfrom
canersoz-scaleai:canersoz/opencode-max-turns
Open

canersoz-scaleai wants to merge 2 commits into
scaleapi:mainfrom
canersoz-scaleai:canersoz/opencode-max-turns

Conversation

@canersoz-scaleai

@canersoz-scaleai canersoz-scaleai commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

A task's max_turns never reached the OpenCode agent: OpenCodeConfig had no such field, so prompt_agent's negotiation dropped it and OpenCode ran uncapped. OpenCode v1's own steps limit can't stand in: it only asks the model to stop and still sends every tool, which upstream confirmed and won't fix in v1 (anomalyco/opencode#54806, #55921). We pin v1 (1.17.18).

OpenCodeConfig now takes max_turns, and the bridge counts the task's steps (OpenCode's model requests, from its step_finish events) as Claude Code's --max-turns counts turns. Once the last allowed step asks for another, the bridge kills the CLI and fails the task with Reached maximum number of turns (N), the message Claude Code and codex use. Parallel tool calls in one step count as one turn. The limit is decided when a step ends, so how the CLI's output splits across reads can't change the outcome.

How it was tested

pytest tst/unit/agents/opencode: 137 passed, 7 skipped (existing Linux/GNU-tar-only skips); ruff check clean. The new tests cover the limit under four ways of splitting stdout into reads, parallel tool calls, negotiation against the real card, and a real subprocess that must be killed. Not run against the real opencode-ai binary.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the previous final-answer cutoff is addressed.

Summary

OpenCode now accepts max_turns through its Agent Card and passes it to the bridge.

  • OpenCode stops a task when its last allowed model step asks for another.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Task supplies max_turns] --> B[Start OpenCode CLI]
  B --> C[Read finished model step]
  C --> D{Needs a step past the limit?}
  D -->|Yes| E[Kill CLI and fail task]
  D -->|No| F{Answer finished?}
  F -->|Yes| G[Return answer]
  F -->|No| C
Loading

Reviews (2) · Last reviewed commit: "fix(agents): opencode's max_turns counts..." · Reviewed by Greptile

OpenCodeConfig had no max_turns, so prompt_agent's negotiation dropped the
field and an OpenCode task ran without a step cap. The CLI has no limit it
enforces: its agent `steps` only asks the model to stop. The bridge now
counts the task's finished tool calls as the CLI prints them, kills the CLI
once max_turns have finished, and fails the task with Claude Code's and
codex's "Reached maximum number of turns (N)".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@canersoz-scaleai
canersoz-scaleai requested a review from a team as a code owner November 16, 2026 05:15
project_id: str | None = None
task_id: str | None = None
timeout_seconds: int = Field(default=1800, gt=0)
# Tool calls a turn may make; see run_opencode.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

let's remove this comment, not necessary

A turn is one model request, an OpenCode step, as Claude Code's --max-turns
counts them; a step's parallel tool calls are one turn, which counting tool
calls charged as several. The limit is now decided at a step's end: the turn
fails once more steps than max_turns finished, or the last one it allows
asked for another. Before, a turn that answered right after its last allowed
tool call passed or failed depending on whether its final step_finish came in
the same stdout read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@canersoz-scaleai canersoz-scaleai changed the title feat(agents): opencode stops a task at its max_turns, as codex does feat(agents): opencode stops a task at its max_turns Nov 16, 2026
project_id: str | None = None
task_id: str | None = None
timeout_seconds: int = Field(default=1800, gt=0)
# Steps, model requests, a turn may make; see run_opencode.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

the line of code is self explanatory. Remove the comment

This branch has not been deployed

No deployments
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