Skip to content

fix(harness/opencode): surface stderr Error: lines on exit 0#525

Merged
AbirAbbas merged 1 commit intomainfrom
fix/opencode-surface-stderr-errors-on-exit0
May 4, 2026
Merged

fix(harness/opencode): surface stderr Error: lines on exit 0#525
AbirAbbas merged 1 commit intomainfrom
fix/opencode-surface-stderr-errors-on-exit0

Conversation

@AbirAbbas
Copy link
Copy Markdown
Contributor

Summary

The opencode CLI sometimes prints a hard error to stderr but exits 0 — notably:

  • Error: Model not found: … (invalid model id)
  • AuthenticationError (bad/missing key)
  • APIError (provider failure)

OpenCodeProvider._execute_impl only flagged failures on non-zero exit, so these cases returned RawResult(is_error=False, result=None) — silently empty output. Downstream callers (the schema-output retry loop in agentfield.harness._runner) then surface the failure as the much less useful "agent failed to produce a valid result".

The bug is doubly hidden because clean_stderr is logged truncated to 800 chars, but opencode opens stderr with the SQLite migration prelude (Performing one time database migration…), pushing the real Error: line off the end of the truncation window. Operators saw the prelude in logs and assumed migration was the issue, not the model id.

This was the proximate cause of the SWE-AF Railway run run_1777925624226_f5a637ae failing every run_product_manager and run_git_init invocation in 8–10s with no useful diagnostic. A separate companion PR (Agent-Field/SWE-AF#55) fixes the upstream model-string bug; this PR ensures opencode failures of this shape are never invisible again.

Changes

  • sdk/python/agentfield/harness/providers/opencode.py

    • New _OPENCODE_STDERR_ERROR_PATTERNS and _extract_opencode_error() helper that pulls the meaningful failure line + a small context window out of opencode stderr, skipping the migration prelude.
    • Adds an explicit branch for returncode == 0 and result_text is None and stderr matches an error pattern → returns is_error=True, failure_type=CRASH with the extracted error message.
    • The existing non-zero-exit branch now uses _extract_opencode_error() too, so log truncation can no longer hide the cause there either.
  • sdk/python/tests/test_harness_provider_opencode.py — three new regression tests:

    1. Model not found exit 0 → is_error=True with the model id in the message.
    2. Migration-only stderr + real stdout → is_error=False (no false positive).
    3. Long migration prelude + AuthenticationError on non-zero exit → AuthenticationError surfaces (no truncation).

Test plan

  • pytest tests/test_harness_provider_opencode.py tests/test_harness_factory.py tests/test_harness_runner.py — 48/48 pass.
  • Bump SWE-AF's agentfield requirement to whatever version this lands in, redeploy, re-trigger github buddy implement on Agent-Field/github-buddy#22, confirm any future opencode misconfiguration shows up with the actual error in the Railway logs (not buried under the migration prelude).

Companion PR

Agent-Field/SWE-AF#55 — fixes the upstream cause (default model id was missing the openrouter/ provider prefix). Both PRs are needed to fully unblock the github-buddy → SWE-AF delegation flow on Railway.

🤖 Generated with Claude Code

The opencode CLI sometimes prints a hard error to stderr but exits 0,
notably "Error: Model not found: …", auth errors, and APIErrors. The
provider's previous logic only flagged failures on non-zero exit, so
these cases returned RawResult(is_error=False, result=None) — silently
empty output that downstream callers interpret as "agent failed to
produce a valid result".

The bug also hid behind the diagnostic: clean_stderr is logged truncated
to 800 chars, but opencode opens stderr with the SQLite migration
prelude ("Performing one time database migration..."), pushing the real
Error: line off the end of the truncation window. Operators saw the
prelude in logs and assumed migration was the issue.

This change:

- Adds explicit detection of error-shaped stderr (Error:, Model not
  found, AuthenticationError, Unauthorized, APIError) and treats them
  as CRASH failures even when returncode == 0.
- Extracts the meaningful error line + a small context window via
  _extract_opencode_error() so the surfaced message contains the real
  cause, not the migration prelude.
- Uses the same extractor for the existing non-zero-exit branch so log
  truncation can no longer hide the cause there either.

Tests: 3 new regression tests (Model not found exit 0, migration-only
stderr is not a failure, long-prelude + AuthenticationError on
non-zero exit). Full opencode + harness suite: 48/48.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@AbirAbbas AbirAbbas requested a review from a team as a code owner May 4, 2026 20:26
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

Performance

SDK Memory Δ Latency Δ Tests Status
Python 7.9 KB -13% 0.50 µs +43%

✓ No regressions detected

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

📊 Coverage gate

Thresholds from .coverage-gate.toml: per-surface ≥ 86%, aggregate ≥ 88%, max per-surface regression ≤ 1.0 pp, max aggregate regression ≤ 0.50 pp.

Surface Current Baseline Δ
control-plane 87.40% 87.30% ↑ +0.10 pp 🟡
sdk-go 91.80% 90.70% ↑ +1.10 pp 🟢
sdk-python 93.66% 93.63% ↑ +0.03 pp 🟢
sdk-typescript 92.63% 92.56% ↑ +0.07 pp 🟢
web-ui 89.69% 90.01% ↓ -0.32 pp 🟡
aggregate 88.85% 89.01% ↓ -0.16 pp 🟡

✅ Gate passed

No surface regressed past the allowed threshold and the aggregate stayed above the floor.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

📐 Patch coverage gate

Threshold: 80% on lines this PR touches vs origin/main (from .coverage-gate.toml:thresholds.min_patch).

Surface Touched lines Patch coverage Status
control-plane 0 ➖ no changes
sdk-go 0 ➖ no changes
sdk-python 0 ➖ no changes
sdk-typescript 0 ➖ no changes
web-ui 0 ➖ no changes

✅ Patch gate passed

Every surface whose lines were touched by this PR has patch coverage at or above the threshold.

@AbirAbbas AbirAbbas merged commit 720a48e into main May 4, 2026
32 checks passed
@AbirAbbas AbirAbbas deleted the fix/opencode-surface-stderr-errors-on-exit0 branch May 4, 2026 20:40
AbirAbbas added a commit to Agent-Field/SWE-AF that referenced this pull request May 4, 2026
Pulls in the OpenCodeProvider stderr-error-detection fix released in
agentfield 0.1.73 (Agent-Field/agentfield#525). Without this bump the
Docker image keeps resolving to the cached older agentfield, so the
companion SWE-AF model-prefix fix from #55 lands but the harness still
silently swallows opencode "Model not found" / auth errors at exit 0.

Updates the pin in:
- requirements-docker.txt (Docker build path)
- requirements.txt (bare-metal install path)
- pyproject.toml (was lagging at 0.1.9)

Verified agentfield-0.1.73-py3-none-any.whl ships
_extract_opencode_error and _OPENCODE_STDERR_ERROR_PATTERNS.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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