"""A daemon that dies right after any of a turn's commits keeps everything it had acknowledged, and the turn goes on after the restart without running a started cell twice. Each test arms the kill point through the inspection node: the VM halts the moment the named commit returns, which no timed kill can hit reliably. """ import http.client import json import time import unittest from harness import Albedo, Provider, ROOT, alive, exclusive_class, python, text from inspect_support import Inspection CONVERSATION = "'albedo@daemon@conversation'" CELLS = "'albedo@harness@extensions@python@cells'" @exclusive_class class CrashDurabilityTest(unittest.TestCase): def setUp(self): self.provider = Provider(self.reply) self.addCleanup(self.provider.close) def reply(self, request): messages = request.get("messages") or [] last_user = max( (i for i, m in enumerate(messages) if m.get("role") == "user"), default=0 ) calls = sum(1 for m in messages[last_user:] if m.get("role") == "tool") if "crash turn" in str(messages[last_user].get("content", "")) and calls < 2: # Each cell leaves one line per run, so a second run shows. return python( f"from pathlib import Path\n" f"with open('ran-{calls}', 'a') as out: out.write('ran\\n')\n" f"print('cell {calls} done')" ) return text("turn finished") def crash_at(self, module, function, nth): def prepare(app): app.daemon.env["ALBEDO_INSPECT"] = "1" with Albedo(self.provider, prepare=prepare) as app: probe = Inspection(app, ROOT / "test/e2e/albedo_crash_probe.erl") session = app.session() probe.call("arm", f"[{module}, {function}, {nth}]") app.prompt(session, "crash turn").close() seen, pid = [], app.daemon._pid deadline = time.monotonic() + 20 while alive(pid): self.assertLess( time.monotonic(), deadline, "the kill point never fired" ) try: seen = app.history(session)["items"] except (OSError, http.client.HTTPException): break app.restart() app.idle(session, timeout=60) after = {item["id"]: item for item in app.history(session)["items"]} for item in seen: self.assertIn(item["id"], after, f"acknowledged item lost: {item}") if item.get("content_complete"): self.assertEqual( json.dumps(item["content"], sort_keys=True), json.dumps(after[item["id"]]["content"], sort_keys=True), ) self.assertIn("turn finished", json.dumps(list(after.values())[-1])) for cell in (0, 1): ran = app.workspace / f"ran-{cell}" runs = ran.read_text().count("ran") if ran.exists() else 0 self.assertLessEqual(runs, 1, f"cell {cell} ran {runs} times") def test_after_the_response_that_calls_the_tool(self): # The first commit is the user input, the second the response. self.crash_at(CONVERSATION, "commit_with_letters", 2) def test_after_the_cell_is_marked_started(self): self.crash_at(CELLS, "mark_started", 1) def test_after_the_cell_result_is_saved(self): self.crash_at(CELLS, "settle", 1) def test_after_the_first_tool_result_commit(self): self.crash_at(CONVERSATION, "commit_with_letters", 3)