Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105"""An interrupt or input cancel that arrives while a finished turn's end isstill being committed must say the run already ended: its outcome is fixed,and it is recorded as completed. Only the ledger writer, held at that committhrough the inspection node, opens this window on demand."""
import jsonimport threadingimport unittest
from harness import ROOT, Albedo, Provider, exclusive_class, textfrom inspect_support import Inspection
@exclusive_classclass SessionFinishTest(unittest.TestCase): def setUp(self): self.started = threading.Event() self.answer = threading.Event() self.addCleanup(self.answer.set)
def script(_): self.started.set() self.answer.wait(30) return text("finished answer")
self.provider = Provider(script) self.addCleanup(self.provider.close)
def prepare(self, app): app.daemon.env["ALBEDO_INSPECT"] = "1"
def start_turn(self, app): """A session whose turn waits on the provider, with that turn's snapshot and input id.""" session = app.session() with app.api(f"/sessions/{session}?tail=0") as response: response.read() with app.prompt(session, "answer once") as response: input_id = json.load(response)["id"] self.assertTrue(self.started.wait(20)) with app.api(f"/sessions/{session}?tail=0") as response: snapshot = json.load(response) return session, input_id, snapshot
def hold_finish(self, app): """Lets the turn return and holds the writer at the commit of its end.""" probe = Inspection(app, ROOT / "test/e2e/albedo_writer_step_probe.erl") self.assertEqual(probe.call("start"), "held") self.answer.set() self.assertEqual(probe.call("hold_before", '[<<"-finish_turn/">>]'), "held") return probe
def outcome(self, app, session, input_id): app.idle(session) with app.api(f"/sessions/{session}/inputs/{input_id}") as response: return json.load(response)["turn"]["state"]
def test_interrupt_during_the_finish_commit_reports_already_ended(self): with Albedo(self.provider, prepare=self.prepare) as app: session, input_id, snapshot = self.start_turn(app) replies = []
def interrupt(): with app.api( f"/sessions/{session}/interrupt", { "run_id": snapshot["status"]["run_id"], "through_input_order": snapshot["input_order"], }, ) as response: replies.append(json.load(response))
caller = threading.Thread(target=interrupt) probe = self.hold_finish(app) try: caller.start() # Its cancellation of queued inputs commits right behind the # run's end, so the session answers before the end lands. self.assertEqual( probe.call("queued", '[<<"cancel_through">>]'), "queued" ) finally: probe.call("release") caller.join(20) self.assertEqual([reply["state"] for reply in replies], ["already_ended"]) self.assertEqual(self.outcome(app, session, input_id), "completed") self.assertEqual(len(self.provider.requests), 1)
def test_input_cancel_during_the_finish_commit_is_not_pending(self): with Albedo(self.provider, prepare=self.prepare) as app: session, input_id, _ = self.start_turn(app) probe = self.hold_finish(app) try: with app.api( f"/sessions/{session}/inputs/{input_id}/cancel", {} ) as response: self.assertEqual(json.load(response)["result"], "not_pending") finally: probe.call("release") self.assertEqual(self.outcome(app, session, input_id), "completed")
if __name__ == "__main__": unittest.main()