ALL

For reading and verification only. You may not copy, use, modify or redistribute this code. Full notice on the main page.

src/all_mcp/e2e_policy.py

SHA-256: 87b9c654892a90327033d06d1f3044e597599d48d7cda7c7ae8b9a8a41704d7d
43 lines · 1898 bytes · View on GitHub

1"""The device's read of the ALL 2.0 policy table (contracts/policy.json, the contract).
2
3The device payload holds only `src/`, so it cannot read `contracts/` at run time: `e2e_policy.json` next to this file is a byte-identical copy that
4the generator's `sync` command writes and its `sync --check` compares (the contract test suite). Code never types a number or a pattern of the table
5again: it asks for it by its dotted name, `value("surface.body_max_bytes")`, and the generator's lint refuses a copy.
6"""
7from __future__ import annotations
8
9import functools
10import json
11import re
12from pathlib import Path
13from typing import Any
14
15POLICY_FILE = Path(__file__).with_name("e2e_policy.json")
16
17
18@functools.lru_cache(maxsize=1)
19def load() -> dict[str, Any]:
20 return json.loads(POLICY_FILE.read_text(encoding="utf-8"))
21
22
23def value(dotted: str) -> Any:
24 """`value("protocol.line_max_bytes")`: the one way this device reads a number, a word or a list of the table."""
25 node: Any = load()
26 for part in dotted.split("."):
27 node = node[part]
28 return node
29
30
31def anchored(dotted: str) -> re.Pattern[str]:
32 """The table's `^...$` pattern as a compiled one to use with `fullmatch` (Python's `$` also matches before a final newline, so the anchors are
33 dropped, the way the contract verifiers do)."""
34 pattern = value(dotted)
35 if not (pattern.startswith("^") and pattern.endswith("$")):
36 raise ValueError("policy patterns are anchored: " + dotted)
37 return re.compile(pattern[1:-1])
38
39
40def host_pattern(zone: str) -> re.Pattern[str]:
41 """The detector this device builds for ITS OWN address in a zone: the table's label pattern, one dot, the zone. Anchored with \\A and \\Z, so
42 match, search and fullmatch give the verdict the Go and TypeScript detectors give."""
43 return re.compile(r"\A" + value("identity.label_regex")[1:-1] + r"\." + re.escape(zone) + r"\Z")