From 0c1aef47670cfc252c032b4f2abd547605eae639 Mon Sep 17 00:00:00 2001 From: CTYQAQ <135084081+CTYQAQ@users.noreply.github.com> Date: Wed, 28 Oct 2026 23:34:31 +0800 Subject: [PATCH] refactor(tokens): own the letter-led compact token shape once The domain-state key validator and the ML-experiment public-token validator compiled the same body `^[A-Za-z][A-Za-z0-9_.:-]{0,79}$` under their own names since they were split out of one quality change. Both now import `public_safe_text.LETTER_LED_COMPACT_TOKEN_PATTERN`, the module that already hosts the sibling compact identifier shapes, and a census guard rejects a new local copy. Each consumer keeps its own field names and error text; the value and every acceptance/rejection decision are unchanged. Signed-off-by: CTYQAQ <135084081+CTYQAQ@users.noreply.github.com> --- loopx/domain_packs/ml_experiment.py | 4 +++- loopx/domain_state.py | 7 +++---- loopx/public_safe_text.py | 6 ++++++ .../test_compact_identifier_shape_owner.py | 21 ++++++++++++++++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/loopx/domain_packs/ml_experiment.py b/loopx/domain_packs/ml_experiment.py index bb3f87de8f..89bc9cfdfa 100644 --- a/loopx/domain_packs/ml_experiment.py +++ b/loopx/domain_packs/ml_experiment.py @@ -8,6 +8,9 @@ from ..control_plane.runtime.public_safety import REMOTE_LOCATION_SURFACE_PATTERN from ..domain_state import default_domain_state_file_path, upsert_domain_state_jsonl +from ..public_safe_text import ( + LETTER_LED_COMPACT_TOKEN_PATTERN as _TOKEN_RE, +) DOMAIN_PACK_CONTRACT_SCHEMA_VERSION = "domain_pack_contract_v0" @@ -35,7 +38,6 @@ "Unknown", ) -_TOKEN_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_.:-]{0,79}$") _ABSOLUTE_PATH_RE = re.compile( r"(^|[\s:=])(?:" + "/" + "Users/" diff --git a/loopx/domain_state.py b/loopx/domain_state.py index 3590e6739c..670bf1126f 100644 --- a/loopx/domain_state.py +++ b/loopx/domain_state.py @@ -2,15 +2,14 @@ import json import os -import re import tempfile from pathlib import Path from typing import Any, Callable from .file_lock import exclusive_file_lock - - -_DOMAIN_STATE_TOKEN_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_.:-]{0,79}$") +from .public_safe_text import ( + LETTER_LED_COMPACT_TOKEN_PATTERN as _DOMAIN_STATE_TOKEN_RE, +) def _domain_state_token(value: str, *, field: str) -> str: diff --git a/loopx/public_safe_text.py b/loopx/public_safe_text.py index 0527f03439..325ed01cfe 100644 --- a/loopx/public_safe_text.py +++ b/loopx/public_safe_text.py @@ -343,6 +343,12 @@ r"^[A-Za-z0-9][A-Za-z0-9._:/#-]{0,199}$" ) COMPACT_TOKEN_PATTERN = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$") +# One definition for "a compact token that must start with a letter". The +# domain-state key validator and the ML-experiment public-token validator each +# compiled this body under their own name; both now import this object, and +# tests/control_plane/test_compact_identifier_shape_owner.py rejects a new local +# copy. Each consumer keeps its own field names and error text. +LETTER_LED_COMPACT_TOKEN_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9_.:-]{0,79}$") # One definition for "a compact, path-safe opaque identifier". Four contracts each # compiled this body under their own name -- the Chat action surface, the Chat # action store, a Goal reference validator and the BotMux runtime -- and a fifth diff --git a/tests/control_plane/test_compact_identifier_shape_owner.py b/tests/control_plane/test_compact_identifier_shape_owner.py index ffe84a66cb..1ebfdfce46 100644 --- a/tests/control_plane/test_compact_identifier_shape_owner.py +++ b/tests/control_plane/test_compact_identifier_shape_owner.py @@ -6,7 +6,7 @@ import pytest -from loopx import chat_action_normalization, chat_actions, chat_action_store +from loopx import chat_action_normalization, chat_actions, chat_action_store, domain_state from loopx.capabilities.benchmark_toolkit import integrity from loopx.capabilities.decision_context import packets, profile, sources from loopx.capabilities.material_lifecycle import _validation @@ -20,6 +20,7 @@ registry, ) from loopx.capabilities.semantic_preference import contract +from loopx.domain_packs import ml_experiment from loopx.control_plane.goals import ( botmux_runtime, deletion_service, @@ -28,6 +29,7 @@ from loopx.extensions import presentation from loopx.public_safe_text import ( COMPACT_TOKEN_PATTERN, + LETTER_LED_COMPACT_TOKEN_PATTERN, OPAQUE_ID_PATTERN as PUBLIC_OPAQUE_ID, MODULE_QUALIFIED_SURFACE_PATTERN, PUBLIC_SAFE_REFERENCE_PATTERN, @@ -37,6 +39,7 @@ OWNER_MODULE = "loopx/public_safe_text.py" REFERENCE_SHAPE = "^[A-Za-z0-9][A-Za-z0-9._:/#-]{0,199}$" COMPACT_SHAPE = "^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$" +LETTER_LED_SHAPE = "^[A-Za-z][A-Za-z0-9_.:-]{0,79}$" SURFACE_SHAPE = "^[a-z][a-z0-9_-]*(?:\\.[a-z][a-z0-9_-]*)+$" OPAQUE_SHAPE = "^[A-Za-z0-9._:-]{1,200}$" @@ -45,6 +48,7 @@ # an entry whose site has already been converted, fails the census. OPEN_OPAQUE_SITES: dict[str, int] = {} OPEN_COMPACT_SITES: dict[str, int] = {} +OPEN_LETTER_LED_SITES: dict[str, int] = {} SHAPE_CONSUMERS = { @@ -125,6 +129,21 @@ def test_the_compact_token_body_is_compiled_only_by_the_owner_and_declared_sites assert found == {OWNER_MODULE: 1, **OPEN_COMPACT_SITES} +def test_the_letter_led_token_body_is_compiled_only_by_the_owner_and_declared_sites() -> None: + found: dict[str, int] = {} + for path in sorted(REPOSITORY_ROOT.glob("loopx/**/*.py")): + relative = path.relative_to(REPOSITORY_ROOT).as_posix() + count = path.read_text(encoding="utf-8").count(LETTER_LED_SHAPE) + if count: + found[relative] = count + assert found == {OWNER_MODULE: 1, **OPEN_LETTER_LED_SITES} + + +def test_domain_state_and_ml_experiment_share_the_letter_led_token_shape() -> None: + assert domain_state._DOMAIN_STATE_TOKEN_RE is LETTER_LED_COMPACT_TOKEN_PATTERN + assert ml_experiment._TOKEN_RE is LETTER_LED_COMPACT_TOKEN_PATTERN + + def test_each_contract_holds_the_owners_object_and_reports_its_own_field() -> None: assert chat_actions._OPAQUE_ID is PUBLIC_OPAQUE_ID assert chat_action_store._OPAQUE_ID is PUBLIC_OPAQUE_ID