Answer stability round 1: 0 🔴, 3 🟡 (0 misread); catch only Unreadable around the decode, test each required field and the id list's type, reword the decoders' docstrings as the prose round asked

This commit is contained in:
2026-11-08 22:45:39 +01:00
parent 897d93d357
commit 2956997794
2 changed files with 22 additions and 8 deletions
+9 -6
View File
@@ -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")}}