"""A side question continues once, into a new session created over HTTP. The exchange's rules (replay, conflict, busy, cancel, dismissal, claims, restart, rewind, and that nothing reaches the parent's history) are checked in process by apps/albedo/test/albedo/session/side_question_model_test.gleam. This file keeps the seam only the assembled daemon has: `PUT /sessions/{id}` of kind `side_question` claims the exchange through the registry, copies the question and answer into the new session without a provider call, and settles the claim. """ import json import unittest import urllib.error from harness import ( Albedo, Provider, operation_id, text, wait_until, conversation, ) class SideQuestionTests(unittest.TestCase): def setUp(self): def script(request): if "side-question" in json.dumps(request["input"][-1]): return text("side answer") return text("parent answer") self.provider = Provider(script) self.addCleanup(self.provider.close) self.app = Albedo(self.provider, protocol="responses") self.app.__enter__() self.addCleanup(self.app.__exit__, None, None, None) self.session = self.app.session() def put(self, identity, question="side question"): with self.app.api( f"/sessions/{self.session}/side-questions/{identity}", {"question": question}, method="PUT", ) as response: return json.load(response)["side_question"] def snapshot(self): with self.app.api(f"/sessions/{self.session}?tail=0") as response: return json.load(response) def done(self, identity): observed = [] def finished(): observed[:] = [self.snapshot()["side_question"]] return ( observed[0] and observed[0]["id"] == identity and observed[0]["status"] != "running" ) wait_until(finished, 15, "side question did not settle") return observed[0] def prepared(self): self.app.prompt(self.session, "first message").close() self.app.idle(self.session) def refusal(self, code, action): with self.assertRaises(urllib.error.HTTPError) as caught: action() self.assertEqual(json.load(caught.exception)["code"], code) def test_continue_copies_question_answer_once_without_provider_call(self): self.prepared() identity = operation_id() self.put(identity) self.done(identity) self.app.prompt(self.session, "later parent message").close() self.app.idle(self.session) before = len(self.provider.requests) target = operation_id() body = { "kind": "side_question", "source_session_id": self.session, "side_question_id": identity, "name": None, } def create(session): return self.app.api( f"/sessions/{session}", body, method="PUT", headers={"If-None-Match": "*"}, ) # A refused target leaves the exchange free to continue. self.refusal("identity_invalid", lambda: create("not-a-uuid")) with self.assertRaises(urllib.error.HTTPError) as taken: create(self.app.session()) self.assertEqual(taken.exception.code, 412) with create(target) as response: self.assertEqual(json.load(response)["id"], target) self.addCleanup(self.app.delete_tree, target) self.assertEqual(len(self.provider.requests), before) history = json.dumps(self.app.history(target)) self.assertIn("side question", history) self.assertIn("side answer", history) self.assertNotIn("later parent message", history) with self.assertRaises(urllib.error.HTTPError) as retry: create(target) self.assertEqual(retry.exception.code, 412) self.refusal("side_question_continued", lambda: create(operation_id())) self.assertEqual( self.snapshot()["side_question"]["continuation_session_id"], target ) self.app.prompt(target, "follow up in continuation").close() self.app.idle(target) followup = json.dumps(conversation(self.provider.requests[-1]["request"])) self.assertIn("side question", followup) self.assertIn("side answer", followup)