feat(harness): OpenCode support with schema retry, error preservation, and project_dir routing#220
Open
santoshkumarradha wants to merge 8 commits intomainfrom
Open
feat(harness): OpenCode support with schema retry, error preservation, and project_dir routing#220santoshkumarradha wants to merge 8 commits intomainfrom
santoshkumarradha wants to merge 8 commits intomainfrom
Conversation
…mpts Add diagnose_output_failure() that classifies validation failures into specific categories: file missing, empty, invalid JSON, or schema mismatch with field-level diff. Enhance build_followup_prompt() to include schema file references and explicit rewrite instructions for the retry loop.
Replace single-shot _handle_schema_output() with _handle_schema_with_retry() that retries up to schema_max_retries times (default 2) when JSON validation fails. Each retry: - Diagnoses the specific failure via diagnose_output_failure() - Sends a follow-up prompt to the agent with error context - For Claude: passes resume=session_id to continue the conversation - For CLI providers: fresh call with the follow-up prompt - Accumulates cost, turns, and messages across all attempts This activates the previously dead-code build_followup_prompt() from _schema.py and adds resume_session_id support to the Claude Code provider.
Standalone script exercising the harness with 5 escalating schema levels:
- simple (2 fields), medium (lists + optionals), complex (13 nested fields),
deeply_nested (recursive TreeNode), massive (>4K tokens, file-based path)
Tested live with both claude-code and codex providers — all levels pass.
Includes manual retry test mode (--retry-test) to exercise the new retry loop.
- Rewrite opencode.py: auto-managed serve+attach pattern to bypass opencode v1.2.10-v1.2.16 'Session not found' bug - Add project_dir field to HarnessConfig (types.py) so coding agents explore the target repo instead of a temp working directory - Add output file placement inside project_dir (runner) so sandboxed Write tool can reach the output JSON - Pass server_url to OpenCodeProvider via factory - Clean up debug prints from runner and claude provider - Verified working with openrouter/moonshotai/kimi-k2.5 model
This was referenced Mar 4, 2026
Contributor
Performance
✓ No regressions detected |
1 task
Member
Author
|
Tracking issue for simplifying the OpenCode serve+attach workaround once upstream fixes headless mode: #221 |
Tests now pass server_url to skip auto-serve lifecycle in CI where opencode binary is not installed. Asserts updated to match --attach command structure.
opencode run --attach loses auto-approve because the serve process treats attached sessions as interactive, causing permission prompts to hang forever when the model tries to write files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Combines all post-merge harness improvements into a single PR. Supersedes #218 and #219.
What's in this PR
opencode serve+--attachpattern to bypass the "Session not found" bug in opencode v1.2.10–v1.2.16 (upstream issue)schema_max_retriestimes with diagnostic follow-up prompts and Claude session continuityproject_dirrouting — newHarnessConfig.project_dirfield so coding agents explore the target repo instead of a temp working directorydiagnose_output_failure()classifies failures into specific categories (file missing, empty, invalid JSON, schema mismatch with field-level diff)resume=session_idso Claude continues the same conversation~83% of the code in this PR (941 of 1,131 lines) exists because of OpenCode bugs and limitations.
The serve+attach pattern in
opencode.pyis a temporary workaround for opencode#13851 —opencode runreturns "Session not found" in headless mode. This workaround:opencode serveprocess on a random port (singleton, shared across provider instances)opencode run --attach <url>This is architecturally concerning because each harness call runs inside a FastAPI reasoner endpoint. Having the provider auto-spawn and manage background server processes within a web server process is fragile — it introduces process lifecycle coupling, port conflicts, and cleanup edge cases that don't belong in a request handler.
Once upstream fixes headless mode, this should be replaced with a simple
opencode runcall (matching the Codex/Gemini provider pattern). See tracking issue: #221OpenCode-specific code inventory
opencode.py(serve lifecycle)_runner.py(project_dir routing)--dirtypes.py(project_dir, opencode_server)_factory.py(server_url passthrough)Generic improvements (benefit all providers)
_runner.py(schema retry loop)_handle_schema_with_retry()replaces single-shot handler_schema.py(diagnosis + followup)diagnose_output_failure(), enhancedbuild_followup_prompt()_runner.py(error preservation)claude.py(resume + logging)Test code
debug_complex_json.pyFiles Changed (7 files, +1131/-37)
sdk/python/agentfield/harness/_runner.pyproject_diroutput routing, metrics accumulation, provider error contextsdk/python/agentfield/harness/_schema.pydiagnose_output_failure(), enhancedbuild_followup_prompt()with schema file refssdk/python/agentfield/harness/providers/opencode.py--dirand system prompt supportsdk/python/agentfield/harness/providers/claude.pyresume_session_idsupport, message counting, error loggingsdk/python/agentfield/harness/providers/_factory.pyserver_urlto OpenCodeProvidersdk/python/agentfield/types.pyproject_dir,opencode_serveronHarnessConfigsdk/python/tests/debug_complex_json.pyVerification
Closes #217. Supersedes #218 and #219.