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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316"""Strict inbound validation at the kernel and remote boundaries.
E2E drives valid frames through a real kernel, so it cannot show that amalformed frame is refused before the event loop, or that a frame the daemonnever sends (a non-boolean `snapshots`, an integer for a boolean) is rejectedinstead of being treated as truthy. These tests call the two public parsersin `albedo_protocol` directly, with valid wire frames and with the coercionsstrict validation exists to refuse."""
from pathlib import Pathimport subprocessimport sysimport unittest
ROOT = Path(__file__).resolve().parents[2]sys.path.insert(0, str(ROOT / "python"))
from albedo_protocol import parse_incoming, parse_remote # noqa: E402
VALID_INCOMING: list[dict[str, object]] = [ { "type": "execute", "id": "c1", "code": "x = 1", "durable": True, "snapshots": True, "max_edge": 100, "text_only": False, "timeout_ms": 1000, }, {"type": "execute", "id": "c1", "code": "x = 1"}, {"type": "reply", "id": "r1", "value": {"ok": True, "value": [1, 2]}}, { "type": "reply", "id": "r1", "value": {"ok": False, "code": "E", "message": "bad"}, }, {"type": "interrupt", "id": "c1", "reason": "deadline"}, {"type": "interrupt", "id": "c1"}, {"type": "shutdown"}, { "type": "invoke", "id": "i1", "name": "run", "args": [1], "kwargs": {"a": 1}, "await": True, }, {"type": "invoke", "id": "i1", "target": {"handle": "h1"}}, {"type": "invoke", "id": "i1", "target": {"pending": "p1"}}, { "type": "invoke", "id": "i1", "name": "run", "then": [ {"id": "i2", "name": "pipe", "target": {"pending": "i1"}}, {"id": "i3", "name": "pipe", "target": {"pending": "i2"}, "args": ["x"]}, ], }, {"type": "introspect", "id": "n1"}, {"type": "release", "handle": "h1"}, {"type": "snapshot", "id": "s1", "path": "/tmp/state"}, {"type": "restore", "id": "s1", "path": "/tmp/state", "originals": "source"}, {"type": "job_stop", "id": "j1"}, {"type": "job_list", "id": "j1"}, {"type": "job_read", "id": "j1", "job": "job1", "limit": 4000}, {"type": "job_read", "id": "j1", "job": "job1", "limit": 4000, "offset": 0}, {"type": "cell_claim", "id": "k1", "cell": "cell1"},]
VALID_REMOTE: list[dict[str, object]] = [ {"type": "ready", "pid": 10, "pgid": 10, "leader": None}, {"type": "ready", "pid": 10, "pgid": None, "leader": "token"}, {"type": "startup_error", "message": "could not start"}, {"type": "call", "id": "c1", "method": "jobs.completed", "args": {"id": "j1"}}, {"type": "invoked", "id": "i1", "ok": True, "value": 42}, {"type": "invoked", "id": "i1", "ok": True, "handle": "h1"}, { "type": "invoked", "id": "i1", "ok": False, "error": {"ename": "ValueError", "evalue": "bad", "traceback": ["line"]}, }, { "type": "introspected", "id": "n1", "names": ["run"], "handles": [{"id": "h1", "repr": "Job"}], }, { "type": "mirror", "handle": "h1", "job": None, "seen": 3, "tail": "out", "tail_bytes": 3, "exit_code": None, "timed_out": False, "duration": 1.5, }, {"type": "mirror", "handle": "h1", "duration": 2}, {"type": "cleanup", "module": "run"}, {"type": "job_stop", "id": "j1", "ok": True}, {"type": "job_list", "id": "j1", "ok": True, "jobs": []}, {"type": "job_read", "id": "j1", "ok": True, "output": "text"}, {"type": "cell_claim", "id": "k1", "ok": True, "done": {"status": "ok"}}, {"type": "job_start", "id": "j1", "service": True}, {"type": "job", "id": "j1"}, {"type": "trace", "id": "j1"}, { "type": "done", "id": "d1", "status": "ok", "output": "hi", "value": "", "truncated": False, "images": [], "duration": 0, }, {"type": "cells", "count": 2},]
class ParseIncomingTest(unittest.TestCase): def test_valid_frames_pass_unchanged(self) -> None: for message in VALID_INCOMING: with self.subTest(message=message): self.assertEqual(parse_incoming(message), message)
def test_snapshots_must_be_a_boolean(self) -> None: # Regression: `snapshots` was declared but never checked, so any # truthy value enabled trace snapshots. Strict validation refuses # every non-boolean, including an explicit null. base: dict[str, object] = {"type": "execute", "id": "c1", "code": "x = 1"} self.assertEqual( parse_incoming({**base, "snapshots": False}), {**base, "snapshots": False} ) for bad in (1, 0, "yes", "true", None, [], [True]): with self.subTest(snapshots=bad): with self.assertRaises(ValueError): parse_incoming({**base, "snapshots": bad})
def test_strict_types_are_not_coerced(self) -> None: bad_frames: list[object] = [ {"type": "execute", "id": "c1", "code": "x", "durable": 1}, {"type": "execute", "id": "c1", "code": "x", "text_only": "false"}, {"type": "execute", "id": "c1", "code": "x", "max_edge": True}, {"type": "execute", "id": "c1", "code": "x", "max_edge": 1.0}, {"type": "execute", "id": "c1", "code": "x", "timeout_ms": "1000"}, {"type": "execute", "id": "c1", "code": "x", "timeout_ms": None}, {"type": "execute", "id": 1, "code": "x"}, {"type": "execute", "id": "c1", "code": 1}, {"type": "execute", "id": "c1"}, {"type": "reply", "id": "r1", "value": {"ok": 1, "value": 1}}, {"type": "reply", "id": "r1", "value": {"ok": True}}, {"type": "reply", "id": "r1", "value": {"ok": False, "code": "E"}}, {"type": "interrupt", "id": "c1", "reason": "bogus"}, {"type": "invoke", "id": "i1", "await": 1}, {"type": "invoke", "id": "i1", "args": {"not": "a list"}}, {"type": "invoke", "id": "i1", "kwargs": {1: "x"}}, {"type": "job_read", "id": "j1", "job": "job1", "limit": True}, {"type": "unknown", "id": "x"}, { "type": "execute", "id": "c1", "code": "x", "unknown_type_field": 1, "max_edge": "wide", }, {}, {"id": "c1"}, [], "execute", None, ] for frame in bad_frames: with self.subTest(frame=frame): with self.assertRaises(ValueError): parse_incoming(frame)
def test_invoke_target_names_exactly_one_handle_or_pending(self) -> None: for target in ( {"handle": "h", "pending": "p"}, {"other": "h"}, {}, {"handle": 1}, "h", ): with self.subTest(target=target): with self.assertRaises(ValueError): parse_incoming({"type": "invoke", "id": "i1", "target": target})
def test_a_chained_call_follows_a_call_its_frame_carries(self) -> None: # The receiver makes each chained call on a result it already has; one # naming anything else could only wait, and the wait is the race the # chain exists to remove. call = {"id": "i2", "name": "pipe", "target": {"pending": "i1"}} for then in ( [{**call, "target": {"pending": "elsewhere"}}], [{**call, "target": {"handle": "i1"}}], [{**call, "id": "i3", "target": {"pending": "i2"}}, call], [{"id": "i2", "target": {"pending": "i1"}}], [{**call, "args": "x"}], ["i2"], call, ): with self.subTest(then=then): with self.assertRaises(ValueError): parse_incoming( {"type": "invoke", "id": "i1", "name": "run", "then": then} )
def test_validation_errors_do_not_leak_input_values(self) -> None: # A frame can carry cell source or a host token; the ValueError a # caller logs must name the field, never echo the value it held. secret = "input-value-that-must-not-leak" frames: list[object] = [ {"type": "execute", "id": "c1", "code": "x", "timeout_ms": secret}, {"type": "execute", "id": "c1", "code": [secret]}, {"type": "reply", "id": "r1", "value": {"ok": secret, "value": 1}}, ] for frame in frames: with self.subTest(frame=frame): with self.assertRaises(ValueError) as caught: parse_incoming(frame) self.assertNotIn(secret, str(caught.exception))
def test_unknown_fields_are_kept(self) -> None: message: dict[str, object] = { "type": "execute", "id": "c1", "code": "x", "future_field": {"a": 1}, } self.assertEqual(parse_incoming(message), message)
class LightweightValidationTest(unittest.TestCase): def test_validation_does_not_import_pydantic(self) -> None: # Every kernel pays for what its first frame imports. Validation must # stay lightweight: Pydantic is bundled for cells, not for the wire. code = ( "import sys\n" f"sys.path.insert(0, {str(ROOT / 'python')!r})\n" "from albedo_protocol import parse_incoming, parse_remote\n" "parse_incoming({'type': 'execute', 'id': 'c1', 'code': 'x = 1'})\n" "parse_remote({'type': 'ready', 'pid': 1, 'pgid': None, 'leader': None})\n" "assert 'pydantic' not in sys.modules, 'protocol validation imported pydantic'\n" "assert 'pydantic_core' not in sys.modules\n" ) result = subprocess.run( [sys.executable, "-c", code], capture_output=True, text=True ) self.assertEqual(result.returncode, 0, result.stderr)
class ParseRemoteTest(unittest.TestCase): def test_valid_frames_pass_unchanged(self) -> None: for message in VALID_REMOTE: with self.subTest(message=message): self.assertEqual(parse_remote(message), message)
def test_strict_types_are_not_coerced(self) -> None: bad_frames: list[object] = [ {"type": "ready", "pid": True, "pgid": None, "leader": None}, {"type": "ready", "pid": "10", "pgid": None, "leader": None}, {"type": "ready", "pid": 10, "pgid": None}, {"type": "call", "id": "c1", "method": "m", "args": []}, {"type": "invoked", "id": "i1", "ok": 1, "value": 1}, {"type": "invoked", "id": "i1", "ok": False}, {"type": "invoked", "id": "i1", "ok": True}, { "type": "invoked", "id": "i1", "ok": False, "error": {"ename": "E", "evalue": "v", "traceback": [1]}, }, {"type": "introspected", "id": "n1", "names": [1], "handles": []}, {"type": "introspected", "id": "n1", "names": [], "handles": [{"id": "h"}]}, {"type": "mirror", "handle": "h", "seen": "3"}, {"type": "mirror", "handle": "h", "duration": True}, {"type": "job_list", "id": "j1", "ok": 1}, {"type": "cell_claim", "ok": True}, {"type": "unknown", "id": "x"}, {}, None, ] for frame in bad_frames: with self.subTest(frame=frame): with self.assertRaises(ValueError): parse_remote(frame)
def test_validation_errors_do_not_leak_input_values(self) -> None: secret = "input-value-that-must-not-leak" with self.assertRaises(ValueError) as caught: parse_remote({"type": "ready", "pid": secret, "pgid": None, "leader": None}) self.assertNotIn(secret, str(caught.exception))
def test_done_and_cells_frames_keep_their_payload(self) -> None: done: dict[str, object] = { "type": "done", "id": "d1", "status": "ok", "output": "hi", "value": "", } self.assertEqual(parse_remote(done), done) self.assertEqual( parse_remote({"type": "cells", "count": 2}), {"type": "cells", "count": 2} )
if __name__ == "__main__": unittest.main()