Solo para leer y verificar. No puedes copiar, usar, modificar ni redistribuir este código. Aviso completo en la página principal.
src/all_mcp/e2e_policy.py
| 1 | """The device's read of the ALL 2.0 policy table (contracts/policy.json, the contract). |
| 2 | |
| 3 | The 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 |
| 4 | the 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 |
| 5 | again: it asks for it by its dotted name, `value("surface.body_max_bytes")`, and the generator's lint refuses a copy. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import functools |
| 10 | import json |
| 11 | import re |
| 12 | from pathlib import Path |
| 13 | from typing import Any |
| 14 | |
| 15 | POLICY_FILE = Path(__file__).with_name("e2e_policy.json") |
| 16 | |
| 17 | |
| 18 | @functools.lru_cache(maxsize=1) |
| 19 | def load() -> dict[str, Any]: |
| 20 | return json.loads(POLICY_FILE.read_text(encoding="utf-8")) |
| 21 | |
| 22 | |
| 23 | def 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 | |
| 31 | def 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 | |
| 40 | def 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") |