Skip to content

fix(opencode,kilocode): bound the CLIs' model request retries - #5913

Draft
yaoyu-33 wants to merge 1 commit into
mainfrom
fix/opencode-kilo-model-retries
Draft

yaoyu-33 wants to merge 1 commit into
mainfrom
fix/opencode-kilo-model-retries

Conversation

@yaoyu-33

@yaoyu-33 yaoyu-33 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What and why

The team's rule for harness-side model retries: resend a failed model request only after a 408, 425, 429, 5xx or a transport error, never after any other 4xx, and keep the total wait bounded.

OpenCode 1.17.11 (read from anomalyco/opencode at v1.17.11) wraps each model attempt in a retry policy with no attempt and no time limit. It resends anything the AI SDK marks retryable (408, 409, 429, ≥500), any 5xx, transport errors and rate-limit texts. It waits exactly as long as retry-after-ms/Retry-After says, uncapped, so Retry-After: 0 makes a hot loop. Against a persistent 503 it sent about 360 requests in 38 s until the run deadline, and the episode was graded incomplete instead of failing with the 503 and being retried. Kilo Code 7.4.15 (an OpenCode fork) did the same (about 330 requests). Its only setting is KILO_SESSION_RETRY_LIMIT, an attempt count that still resends a 409 and still follows any Retry-After. OpenCode has no setting at all.

So this adds a plugin, opencode_agent/model_retry_limit.mjs, shared with Kilo. Its config hook wraps the fetch of every provider in the config:

  • a 408, 425, 429 or 5xx reaches the CLI with retry-after-ms set to Retry-After capped at 10 s (else 1 s, then 2 s), so the CLI's own loop resends it;
  • the third such failure in a row, a 409, or a third failed connection ends the turn with NeMo Gym model retry limit: HTTP <status> after 3 attempts (or HTTP 409 is not sent again, or no model response after 3 attempts). It is a plain Error, because both CLIs resend every 5xx API error. Its text and its missing cause keep both CLIs from reading it as a rate limit or a lost connection, which they would also resend;
  • any other answer ends a run of failures; other 4xx (400, 401, 403, 404, 413, 422) reach the CLI unchanged and are not resent.

The CLIs save that error as an UnknownError. retry_limit_outcome (shared in opencode_agent/runtime.py) classifies it as model_request_error with the status, or model_transport_error. OpenCode's sandbox path treats it as a reported model error (HTTP 200 failed) as it does an API error, and Kilo's error event path does the same. So the Environment Server's outcome policy applies as for an API error.

Where it loads:

  • OpenCode: locally whenever a model_server defines Gym's provider, and in every sandbox session (uploaded beside task_environment.mjs, also for a continuation);
  • Kilo: in sandbox sessions. Both Kilo paths also set KILO_SESSION_RETRY_LIMIT=2, which is the only bound on the local run: it starts Kilo with --pure, which loads no plugins. There a 409 is still resent twice and a server Retry-After is followed in full (documented in the README).

Issue: no public issue; this implements the retry rule the team agreed for all harnesses. The other harness groups are in separate PRs (Claude Code; Codex; NOOA, Hermes and Stirrup). opencode_sandboxed_agent, which is being retired, is not changed.

Related: #5910 (NOOA, Hermes, Stirrup), #5911 (Claude Code), #5912 (Codex), #5913 (OpenCode, Kilo Code).

Score and compatibility impact

  • A persistent 503 (or 429, 408, 425) now ends the run after 3 requests with failed model_request_error and that status. The Environment Server retries it, instead of the run spinning until its deadline and being graded incomplete (OpenCode) or failing as unknown (Kilo).
  • A 409 is sent once.
  • A transient 5xx is still resent (twice), within 20 s of waiting.
  • A direct provider call (openai_base_url without model_server) defines no provider in the config and keeps OpenCode's own retries.

Validation

Linux workstation, each adapter's uv sync --frozen venv, Node 22.19.0:

  • opencode_agent/tests: 865 passed, 8 skipped. 1 failed, test_pinned.py::test_stage_executable_rejects_wrong_or_broken_bytes_without_installing[format], which this PR does not touch: the host's /bin/sh is dash, which reports an unexecutable file as exit 2 where the test expects 126.
  • kilocode_agent/tests: 256 passed, 7 skipped.
  • New test_model_retry_limit.py drives the plugin under Node:
    • 503, 500, 429, 408 and 425 each reach the CLI twice with a bounded retry-after-ms, then end the turn;
    • Retry-After: 60 gives 10 s, and no header gives 1 s then 2 s;
    • a 409 is not sent again; 400, 401, 403, 404, 413 and 422 pass through unchanged;
    • a success resets the count; refused connections end the turn on the third without a network cause;
    • a cancelled request is not counted, a second config call does not wrap twice, and the module exports only the plugin.
  • New session tests: the plugin's error reaches the Environment Server as failed model_request_error 503/409 and model_transport_error (OpenCode and Kilo). The host config loads the plugin with a model server, and Kilo's local environment sets the count. Tests that pinned the exact plugin list and uploads now include the plugin.
  • pre-commit run --files <changed files>: passed. Commit signed off.

Smoke evidence

Scripted-model smoke (no real model), Linux workstation, 2026-11-09, with the harness-conformance runner, one job at a time:

Harness Scenario A B
OpenCode model_503 372 × 503 over 39.5 s, until the run deadline → incomplete → verify (graded); P0 outcome.ladder, outcome.disposition, model.retries_bounded, model.terminal_error fail 3 × 503, 1.7 s → failed model_request_error 503 → retry; every P0 check passes
Kilo Code model_503 321 × 503 over 36.3 s → failed unknown → retry; the same four P0 checks fail 3 × 503, 1.1 s → failed model_request_error 503 → retry; every P0 check passes
both model_error, model_401 1 request → failed model_request_rejected → fail same
both retry_429, retry_500 completed → verify same (resent with the bounded wait)
both context_overflow 1 × 200, then 2 (OpenCode) or 3 (Kilo) different compaction requests → incomplete → verify same

OpenCode's B row is from a re-run after the plugin started keeping the server's error message in its final error; before that, model.terminal_error failed on the missing message.

Real-model rollouts: not run yet; this PR stays a draft until they are.

🤖 Generated with Claude Code

Harness-side model retries may cover only 408, 425, 429, 5xx and transport
errors, with a bounded total wait. OpenCode 1.17.11 resends a failed model
request with no attempt or time limit, also after a 409, and waits exactly
as long as Retry-After says. Against a persistent 503 with Retry-After: 0 it
sent about 360 requests in 38 s until the run deadline, so the episode was
graded as incomplete instead of failed and retried. Kilo Code 7.4.15 did the
same (about 330 requests); its only setting is an attempt count.

Neither CLI can be configured for this, so a plugin
(opencode_agent/model_retry_limit.mjs, shared with Kilo) wraps the fetch of
every configured provider: a 408, 425, 429 or 5xx reaches the CLI with
retry-after-ms set to Retry-After capped at 10 s (else 1 s, 2 s), and a 409
or the third failure in a row ends the turn with "NeMo Gym model retry
limit: HTTP <status> ...". The adapters classify that error as
model_request_error with the status (or model_transport_error), so the
Environment Server's outcome policy applies as for an API error.

OpenCode loads the plugin locally (with a model server) and in sandbox
sessions; Kilo loads it in sandbox sessions and also sets
KILO_SESSION_RETRY_LIMIT=2, the only bound on its local --pure run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 10, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 71e465a

This branch was previously deployed

1 inactive deployment
public — 71e465a1 Deployed Nov 10, 2026 by copy-pr-bot[bot] via release / finalize / notify #11286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent harnesses and Responses API agent behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants