Skip to content

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

Merged
canersoz-scaleai merged 4 commits into
scaleapi:mainfrom
canersoz-scaleai:canersoz/opencode-max-turns
Nov 16, 2026
Merged

canersoz-scaleai merged 4 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.

End to end with the real opencode-ai 1.17.18 CLI: I ran AgentEnvHub locally (hub backend, Temporal, workflow and activity workers) against the dev database, with this PR's OpenCode agent built and registered as a test agent and run in a local Docker sandbox. The prompt forced one shell command per step (echo 17, add 5, double it, reply with the result).

  • max_turns: 2: the run failed with Reached maximum number of turns (2). Its trajectory has two steps, both ending in tool-calls, and no third step started; no opencode process was left in the container.
  • max_turns: 10: the same prompt completed with 44 after four steps, the last ending in stop.

The local hub and workers ran the release scaleapi pins (0.9.1357) with this PR's agents/opencode/ folder in place, since the agent folder is its own build context.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no new actionable issue was established.

Summary

OpenCode now accepts max_turns and counts finished model steps. When the last allowed step requires another, the bridge kills the CLI and fails the task with the shared limit message.

  • OpenCode tasks stop after their configured number of model steps.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Task configuration: max_turns"] --> B["Run OpenCode"]
  B --> C["Read step_finish events"]
  C --> D{"Allowed steps exhausted<br/>and another step needed?"}
  D -- No --> E["Continue or return answer"]
  D -- Yes --> F["Kill CLI and child processes"]
  F --> G["Fail task with turn-limit message"]
Loading

Reviews (7) · Last reviewed commit: "Merge branch 'main' into canersoz/openco..." · 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
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@canersoz-scaleai
canersoz-scaleai enabled auto-merge (squash) November 16, 2026 23:15
@canersoz-scaleai
canersoz-scaleai merged commit bf4574c into scaleapi:main Nov 16, 2026
34 of 35 checks passed
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.

2 participants