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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320"""Every input a session will read waits in its inbox, where people see,reorder and drop it before the session takes it.
The queue's order under posts, reorders, drops, edits, and takes, and thebatches a turn or step takes, are checked in process byapps/albedo/test/albedo/session/queue_test.gleam. These cases keep the HTTPjourney and holds, which live in the session actor and survive a crash."""
import base64import jsonimport threadingimport timeimport unittestimport urllib.error
from harness import ( Albedo, Provider, exclusive, operation_id, python, text, wait_until, conversation,)from image_limits_test import png
class QueueTests(unittest.TestCase): def setUp(self): self.gate = threading.Event() self.started = threading.Event() self.first = True
def reply(_): if self.first: self.first = False self.started.set() self.gate.wait(40) return text("done")
self.provider = Provider(reply) self.addCleanup(self.provider.close) self.addCleanup(self.gate.set) self.app = Albedo(self.provider, protocol="responses") self.app.__enter__() self.addCleanup(self.app.__exit__, None, None, None)
def put_input(self, session, intent): input_id = operation_id() with self.app.api( f"/sessions/{session}/inputs/{input_id}", intent, method="PUT" ) as response: self.assertEqual(json.load(response)["delivery"], "pending") return input_id
def queue(self, session, path="", body=None, method="GET"): with self.app.api( f"/extensions/mail/sessions/{session}/queue{path}", body, method=method ) as response: return json.load(response)
def input_action(self, session, input_id, action, body=None, method="POST"): with self.app.api( f"/sessions/{session}/inputs/{input_id}/{action}", body, method=method ) as response: raw = response.read() return json.loads(raw) if raw else None
def phase(self, session): with self.app.api(f"/sessions/{session}?tail=0") as response: return json.load(response)["status"]["phase"]
def said(self): return [ json.dumps(conversation(record["request"])) for record in self.provider.requests ]
def test_waiting_inputs_are_listed_reordered_and_dropped(self): session = self.app.session() self.app.prompt(session, "hold the turn").close() self.assertTrue(self.started.wait(10)) first = self.put_input(session, {"kind": "message", "text": "first waiting"}) image = self.put_input( session, { "kind": "message", "text": "with an image", "images": [ { "mime_type": "image/png", "data": base64.b64encode(png(2, 2)).decode(), } ], }, ) last = self.put_input(session, {"kind": "message", "text": "last waiting"})
queue = self.queue(session) self.assertEqual([item["id"] for item in queue["items"]], [first, image, last]) self.assertEqual(queue["total"], 3) self.assertEqual( {(item["kind"], item["from"], item["delivery"]) for item in queue["items"]}, {("chat", "you", "steer")}, ) self.assertEqual([len(item["images"]) for item in queue["items"]], [0, 1, 0]) self.assertEqual( [item["editable"] for item in queue["items"]], [True, False, True] ) self.assertEqual( [row["id"] for row in queue["page"]["rows"]], [first, image, last] ) self.assertEqual( [action["id"] for action in queue["page"]["actions"]], ["drop", "first", "clear"], )
moved = self.queue(session, f"/{last}/first", {}, method="POST") self.assertEqual([item["id"] for item in moved["items"]], [last, first, image]) dropped = self.queue(session, f"/{image}", method="DELETE") self.assertEqual([item["id"] for item in dropped["items"]], [last, first]) with self.app.api(f"/sessions/{session}/inputs/{image}") as response: self.assertEqual(json.load(response)["delivery"], "cancelled") with self.assertRaises(urllib.error.HTTPError) as missing: self.queue(session, f"/{image}", method="DELETE") self.assertEqual(missing.exception.code, 404)
self.gate.set() self.app.idle(session) self.assertEqual(self.queue(session)["items"], []) said = [ json.dumps(conversation(record["request"])) for record in self.provider.requests ] later = next(request for request in said if "first waiting" in request) self.assertLess(later.index("last waiting"), later.index("first waiting")) self.assertFalse(any("with an image" in request for request in said))
def test_a_held_message_waits_for_its_edit_and_keeps_its_place(self): def script(request): if len(self.provider.requests) == 1: self.started.set() self.gate.wait(40) return python("print('one more step')") return text("done")
self.provider.script = script session = self.app.session() self.app.prompt(session, "hold the turn").close() self.assertTrue(self.started.wait(10)) typo = self.put_input( session, {"kind": "message", "text": "use the wrnog branch"} ) behind = self.put_input(session, {"kind": "message", "text": "then push it"}) held = self.input_action(session, typo, "hold", {}) self.assertGreater(held["lease_ms"], 0)
# The next step and the end of the turn go without the held message, # and without what waits behind it. self.gate.set() wait_until( lambda: len(self.provider.requests) >= 2 and self.phase(session) == "idle", 20, "the turn did not end", ) time.sleep(0.5) self.assertEqual(len(self.provider.requests), 2) self.assertFalse( any("wrnog" in said or "then push it" in said for said in self.said()) ) self.assertEqual( [item["id"] for item in self.queue(session)["items"]], [typo, behind] )
edited = self.input_action( session, typo, "edit", {"kind": "message", "text": "use the right branch"} ) self.assertEqual(edited["input"]["id"], typo) self.app.idle(session) said = self.said() self.assertFalse(any("wrnog" in request for request in said)) later = next(request for request in said if "use the right branch" in request) self.assertLess( later.index("use the right branch"), later.index("then push it") ) with self.app.api(f"/sessions/{session}/inputs/{typo}") as response: self.assertEqual(json.load(response)["delivery"], "committed")
with self.assertRaises(urllib.error.HTTPError) as read: self.input_action( session, typo, "edit", {"kind": "message", "text": "too late"} ) self.assertEqual(read.exception.code, 409) self.assertEqual(json.load(read.exception)["code"], "input_not_waiting")
def test_a_released_hold_reads_the_message_as_it_was(self): session = self.app.session() self.app.prompt(session, "hold the turn").close() self.assertTrue(self.started.wait(10)) waiting = self.put_input(session, {"kind": "message", "text": "as it was"}) self.input_action(session, waiting, "hold", {}) self.gate.set() wait_until(lambda: self.phase(session) == "idle", 20, "the turn did not end") time.sleep(0.5) self.assertFalse(any("as it was" in said for said in self.said()))
self.assertIsNone(self.input_action(session, waiting, "hold", method="DELETE")) self.app.idle(session) self.assertTrue(any("as it was" in said for said in self.said()))
@exclusiveclass QueueRecoveryTests(unittest.TestCase): def test_held_input_survives_daemon_crash(self): gate = threading.Event() started = threading.Event() self.addCleanup(gate.set) first = True
def reply(_): nonlocal first if first: first = False started.set() gate.wait(20) return text("done")
provider = Provider(reply) self.addCleanup(provider.close) with Albedo(provider, protocol="responses") as app: session = app.session() app.prompt(session, "hold the turn").close() self.assertTrue(started.wait(10)) waiting = operation_id() app.api( f"/sessions/{session}/inputs/{waiting}", {"kind": "message", "text": "held across crash"}, method="PUT", ).close() app.api( f"/sessions/{session}/inputs/{waiting}/hold", {}, method="POST", ).close() app.restart(crash=True) gate.set() wait_until( lambda: ( json.load(app.api(f"/sessions/{session}?tail=0"))["status"]["phase"] == "idle" ), 20, "session did not return to idle", ) time.sleep(0.5) self.assertFalse( any("held across crash" in json.dumps(r) for r in provider.requests) ) app.api( f"/sessions/{session}/inputs/{waiting}/hold", method="DELETE", ).close() wait_until( lambda: any( "held across crash" in json.dumps(r) for r in provider.requests ), 15, "held input did not run after release", )
def test_restored_hold_lapses_without_client_release(self): gate = threading.Event() started = threading.Event() self.addCleanup(gate.set) first = True
def reply(_): nonlocal first if first: first = False started.set() gate.wait(20) return text("done")
provider = Provider(reply) self.addCleanup(provider.close) with Albedo( provider, protocol="responses", prepare=lambda app: app.env.update(ALBEDO_TEST_TIME_SCALE="10"), ) as app: session = app.session() app.prompt(session, "hold the turn").close() self.assertTrue(started.wait(10)) waiting = operation_id() app.api( f"/sessions/{session}/inputs/{waiting}", {"kind": "message", "text": "lapses after crash"}, method="PUT", ).close() app.api( f"/sessions/{session}/inputs/{waiting}/hold", {}, method="POST", ).close() app.restart(crash=True) gate.set() wait_until( lambda: any( "lapses after crash" in json.dumps(r) for r in provider.requests ), 15, "restored hold never lapsed on its own", )
if __name__ == "__main__": unittest.main()