Skip to content
Python

fix(proposer): reject OpenCode session errors before caching output (#13)

Rudy Mizrahi Celeklie9db57b1fc74
diff --git a/harness2/core/proposer.py b/harness2/core/proposer.py
index 6a688f1..0652863 100644
--- a/harness2/core/proposer.py
+++ b/harness2/core/proposer.py
@@ -292,10 +292,12 @@ class Proposer:
timeout=wall,
)
cost = _opencode_cost(p.stdout)
+ text, err = _opencode_stdout(p.stdout)
+ if err:
+ return "", cost, err
ans = out / "answer.md"
if ans.is_file() and ans.read_text(errors="replace").strip():
return ans.read_text(errors="replace"), cost, ""
- text, err = _opencode_stdout(p.stdout)
if text.strip():
return text, cost, ""
tail = _strip_oc_banner(p.stderr or "").strip()[-300:]
diff --git a/tests/test_proposer_session_errors.py b/tests/test_proposer_session_errors.py
new file mode 100644
index 0000000..7eef53d
--- /dev/null
+++ b/tests/test_proposer_session_errors.py
@@ -0,0 +1,92 @@
+# Copyright 2026 Google LLC
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Pinned OpenCode JSONL protocol stand-ins; no executor or model is contacted."""
+
+import json
+import tempfile
+import unittest
+from pathlib import Path
+from unittest.mock import patch
+
+from harness2 import config
+from harness2.core.proposer import Proposer, ProposerError
+from harness2.core.spec import ChannelSpec
+
+
+class ProposerSessionErrorsTests(unittest.TestCase):
+ def test_session_error_rejects_partial_output_and_preserves_cost(self):
+ for answer_file in (False, True):
+ with self.subTest(answer_file=answer_file):
+ self.run_session(answer_file=answer_file, fail=True)
+
+ def test_successful_file_and_stdout_answers_remain_usable(self):
+ for answer_file in (False, True):
+ with self.subTest(answer_file=answer_file):
+ self.run_session(answer_file=answer_file, fail=False)
+
+ def run_session(self, *, answer_file, fail):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary)
+ entry = root / "opencode/packages/opencode/src/index.ts"
+ entry.parent.mkdir(parents=True)
+ entry.write_text("")
+ events = [
+ {"type": "text", "part": {"type": "text", "text": "partial or final answer"}},
+ {"type": "step_finish", "part": {"type": "step-finish", "cost": 0.25}},
+ ]
+ if fail:
+ events.append(
+ {
+ "type": "error",
+ "error": {"name": "APIError", "data": {"message": "provider unavailable"}},
+ }
+ )
+ executable = root / "protocol-standin"
+ executable.write_text(
+ "#!/usr/bin/env python3\nfrom pathlib import Path\n"
+ + (
+ "Path('output/answer.md').write_text('partial or final answer')\n"
+ if answer_file
+ else ""
+ )
+ + "print("
+ + repr("\n".join(json.dumps(e) for e in events))
+ + ")\n"
+ )
+ executable.chmod(0o700)
+ audit = root / "audit"
+ proposer = Proposer(
+ ChannelSpec("opencode", "local/protocol-fixture"), audit_dir=audit, attempts=1
+ )
+ with (
+ patch.object(config, "BUN", executable),
+ patch.object(config, "OPENCODE_DIR", root / "opencode"),
+ ):
+ if fail:
+ with self.assertRaises(ProposerError) as caught:
+ proposer.call("Read the local protocol fixture", None, "fixture")
+ self.assertIn("provider unavailable", str(caught.exception))
+ self.assertEqual(caught.exception.cost_usd, 0.25)
+ self.assertFalse(proposer._calls[-1].ok)
+ self.assertEqual(proposer._calls[-1].text, "")
+ self.assertFalse(
+ any(p.read_text().strip() for p in audit.glob("*.response.md"))
+ )
+ else:
+ reply = proposer.call("Read the local protocol fixture", None, "fixture")
+ self.assertTrue(reply.ok)
+ self.assertEqual(reply.text, "partial or final answer")
+ self.assertEqual(reply.cost_usd, 0.25)