From 2956997794b32292852ca5d7ee988be15eb4019a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sun, 8 Nov 2026 22:45:39 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=203=20=F0=9F=9F=A1=20(0=20misread);=20catch=20onl?= =?UTF-8?q?y=20Unreadable=20around=20the=20decode,=20test=20each=20require?= =?UTF-8?q?d=20field=20and=20the=20id=20list's=20type,=20reword=20the=20de?= =?UTF-8?q?coders'=20docstrings=20as=20the=20prose=20round=20asked?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/loop_claude/loop/reload.py | 15 +++++++++------ bin/loop_claude/test_loop_claude.py | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bin/loop_claude/loop/reload.py b/bin/loop_claude/loop/reload.py index b4f39ac5..9092d3cb 100644 --- a/bin/loop_claude/loop/reload.py +++ b/bin/loop_claude/loop/reload.py @@ -34,23 +34,26 @@ class Unreadable(Exception): def decode_handed(handed: str) -> LoopRun | None: - """The LoopRun the JSON holds; None for text that isn't one. A field of LoopRun or Route with a default may be missing, so a field added since the running script started doesn't stop the loop.""" + """The LoopRun the JSON holds; None for text that isn't one. A field of LoopRun or Route with a default may be missing, so a field the sending script didn't know yet doesn't stop the loop; an effort missing from a Rung is empty.""" try: fields = json.loads(handed) + except ValueError: + return None + try: return LoopRun(**decoded_fields(fields, LOOP_RUN_FIELDS, "cycle_number", "root", "task", "unfinished_evidence")) - except (ValueError, Unreadable): + except Unreadable: return None def decoded_fields(value: object, decoders: dict[str, typing.Callable[[typing.Any], typing.Any]], *required: str) -> dict[str, typing.Any]: - """Each field of the JSON object through its decoder; raises Unreadable unless the object holds every required field and none the decoders don't name.""" + """Each field of the JSON object through its decoder. Raises Unreadable for a value that isn't an object, an object missing a required field, and an object holding a field that has no decoder.""" if not isinstance(value, dict) or not set(required) <= value.keys() <= decoders.keys(): raise Unreadable return {name: decoders[name](field) for name, field in value.items()} def exactly(kind: type) -> typing.Callable[[typing.Any], typing.Any]: - """A decoder of a value of exactly this type: type(), not isinstance(), since JSON true is no integer.""" + """A decoder of a value of exactly this type. It checks type(), since isinstance(True, int) holds and a JSON true must not pass as an integer.""" def decode(value: object) -> object: if type(value) is not kind: raise Unreadable @@ -69,7 +72,7 @@ def decoded_rung(value: object) -> Rung: def decoded_route(value: object) -> Route | None: - """A reload from before Route dropped a field hands a route that field names in Route.RETIRED: it decodes as None, and the chunk routes afresh.""" + """The Route the JSON holds; None for null, or for a route holding a key Route.RETIRED names, so the chunk routes afresh.""" if value is None or (isinstance(value, dict) and value.keys() & Route.RETIRED): return None fields = decoded_fields(value, ROUTE_FIELDS, "rung") @@ -78,7 +81,7 @@ def decoded_route(value: object) -> Route | None: return Route(**fields) -# One decoder per field, which tests hold to the dataclass's fields: a field missing here stops a loop at the reload after it joined. +# One decoder per field of LoopRun and of Route, which tests hold to those dataclasses' fields: a field missing here stops a loop at the reload after it joined. LOOP_RUN_FIELDS = {"cycle_number": exactly(int), "root": exactly(str), "task": exactly(str), "unfinished_evidence": exactly(str), "route": decoded_route, "unfinished_run": exactly(int), "items_taken_in": decoded_ids} ROUTE_FIELDS = {"rung": decoded_rung, **{name: exactly(int) for name in ("item", "likelihood", "escape_and_impact", "unfinished", "escape_and_impact_taken_in", "escalated_at")}} diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index 920bf8b8..1b368491 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -1778,7 +1778,7 @@ class LoopRuns(unittest.TestCase): self.assertEqual(set(reload.LOOP_RUN_FIELDS), {field.name for field in dataclasses.fields(main.LoopRun)}) self.assertEqual(set(reload.ROUTE_FIELDS), {field.name for field in dataclasses.fields(Route)}) - def test_rejects_a_field_of_the_state_or_its_route_of_another_type(self): + def test_rejects_a_field_of_another_type_or_an_unknown_field(self): state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-opus-5-5", "high"), 3, 4, 5, 1, 7, 9), unfinished_run=2)) for path, wrong in ((("unfinished_run",), "2"), (("unfinished_run",), True), (("route", "unfinished"), 1.0), (("route", "escalated_at"), "9"), (("route", "item"), True), (("route", "rung"), "claude-opus-5-5"), (("route", "rung", "model"), None), (("route",), "r"), (("route", "extra"), 1)): with self.subTest(path=path, wrong=wrong): @@ -1789,6 +1789,17 @@ class LoopRuns(unittest.TestCase): target[path[-1]] = wrong self.assertIsNone(reload.decode_handed(json.dumps(handed))) + def test_rejects_a_state_or_rung_missing_a_required_field(self): + state = dataclasses.asdict(main.LoopRun(cycle_number=2, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("claude-opus-5-5", "high")))) + for path in (("cycle_number",), ("root",), ("task",), ("unfinished_evidence",), ("route", "rung"), ("route", "rung", "model")): + with self.subTest(path=path): + handed = json.loads(json.dumps(state)) + target = handed + for key in path[:-1]: + target = target[key] + del target[path[-1]] + self.assertIsNone(reload.decode_handed(json.dumps(handed))) + def test_decodes_a_rung_without_an_effort(self): state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", route=Route(Rung("some-model"))) self.assertEqual(reload.decode_handed(json.dumps({**dataclasses.asdict(state), "route": {"rung": {"model": "some-model"}}})), state) @@ -1830,7 +1841,7 @@ class LoopRuns(unittest.TestCase): def test_decodes_the_items_taken_in_as_a_tuple_of_ids(self): state = main.LoopRun(cycle_number=1, root="/r", task="do it", unfinished_evidence="", items_taken_in=(12, 14)) self.assertEqual(reload.decode_handed(json.dumps(dataclasses.asdict(state))), state) - for taken_in in ([12, "14"], [True], "12", {"12": 1}): + for taken_in in ([12, "14"], [True], "12", {"12": 1}, "", {}): with self.subTest(taken_in=taken_in): self.assertIsNone(reload.decode_handed(json.dumps(dataclasses.asdict(state) | {"items_taken_in": taken_in})))