Say why the worker failed: the exit code, or exit 0 without a result event

This commit is contained in:
2026-09-25 17:58:04 +02:00
parent 2062e5c5f1
commit 5d9cb80912
4 changed files with 35 additions and 12 deletions
+22 -1
View File
@@ -275,6 +275,22 @@ else:
"""
class RunWorker(unittest.TestCase):
def test_says_whether_a_non_zero_exit_or_a_missing_result_event_failed_it(self):
with tempfile.TemporaryDirectory() as home:
stub = os.path.join(home, "claude")
scripts = {
"": 'print(json.dumps({"type": "result"}))',
"claude exited 0 without a result event": 'print(json.dumps({"type": "assistant", "message": {"content": [{"type": "text", "text": "worked"}]}}))',
"claude exited 3": "sys.exit(3)",
}
with unittest.mock.patch.dict(os.environ, {"PATH": f"{home}:{os.environ['PATH']}"}), unittest.mock.patch.object(sys, "stdout", io.StringIO()):
for failure, script in scripts.items():
open(stub, "w").write(f"#!/usr/bin/env python3\nimport json\nimport sys\n{script}\n")
os.chmod(stub, 0o700)
self.assertEqual(loop.run_worker([]), failure)
class RunCycle(unittest.TestCase):
def test_resumes_until_the_judge_calls_the_chunk_finished_and_rejudges_a_failed_or_unquoted_verdict(self):
with tempfile.TemporaryDirectory() as home:
@@ -287,7 +303,7 @@ class RunCycle(unittest.TestCase):
environment = {"HOME": home, "PATH": f"{home}:{os.environ['PATH']}", "STUB_CALLS": calls, "STUB_VERDICTS": verdicts}
with unittest.mock.patch.dict(os.environ, environment), unittest.mock.patch("time.sleep") as sleep, unittest.mock.patch("sys.stdin.isatty", return_value=False):
for _ in range(3):
self.assertTrue(loop.run_cycle("task", "premises", "A name", SESSION_ID))
self.assertEqual(loop.run_cycle("task", "premises", "A name", SESSION_ID), "")
open(verdicts, "w").write("ask")
with self.assertRaises(SystemExit) as stopped, unittest.mock.patch("sys.stderr") as stderr:
loop.run_cycle("task", "premises", "A name", SESSION_ID)
@@ -338,6 +354,11 @@ class Main(unittest.TestCase):
loop.main()
mocks[2].assert_called_once_with("do it", 3, 2)
self.assertIn("=== cycle 4 ===", sys.stdout.getvalue())
mocks[3].side_effect = ["claude exited 3"] * 3
stack.enter_context(unittest.mock.patch("time.sleep"))
with self.assertRaises(SystemExit) as stopped:
loop.main()
self.assertEqual(stopped.exception.code, "the worker failed three times running: claude exited 3")
if __name__ == "__main__":