diff --git a/loopx/capabilities/agent_turn_recall/runtime.py b/loopx/capabilities/agent_turn_recall/runtime.py index 5eaca95d6f..f384764e3d 100644 --- a/loopx/capabilities/agent_turn_recall/runtime.py +++ b/loopx/capabilities/agent_turn_recall/runtime.py @@ -3,7 +3,6 @@ import hashlib import json import os -import re import tempfile from collections.abc import Mapping from datetime import datetime, timezone @@ -12,6 +11,7 @@ from ...history import load_registry from ...materials import find_registry_goal, goal_repo +from ...paths import PATH_SEGMENT_UNSAFE_RE from ..context_providers.base import ContextProvider from ..reward_memory.experiment import ( resolve_reward_memory_experiment, @@ -27,11 +27,10 @@ AGENT_TURN_RECALL_RECEIPT_SCHEMA_VERSION = "agent_turn_recall_receipt_v1" -_SAFE_PATH_TOKEN = re.compile(r"[^A-Za-z0-9._-]+") def _safe_token(value: str, *, fallback: str) -> str: - readable = _SAFE_PATH_TOKEN.sub("-", value).strip("-") or fallback + readable = PATH_SEGMENT_UNSAFE_RE.sub("-", value).strip("-") or fallback digest = hashlib.sha256(value.encode("utf-8")).hexdigest()[:12] return f"{readable[:60]}-{digest}" diff --git a/loopx/capabilities/reward_memory/outcome_lifecycle.py b/loopx/capabilities/reward_memory/outcome_lifecycle.py index 7d37b96d76..9039f5b19d 100644 --- a/loopx/capabilities/reward_memory/outcome_lifecycle.py +++ b/loopx/capabilities/reward_memory/outcome_lifecycle.py @@ -3,7 +3,6 @@ import hashlib import json import os -import re import tempfile from collections.abc import Mapping from datetime import datetime, timezone @@ -13,6 +12,7 @@ from ...control_plane.runtime.public_safety import public_safe_compact_text from ...history import load_registry from ...materials import find_registry_goal, goal_repo +from ...paths import PATH_SEGMENT_UNSAFE_RE from ..context_providers.base import ContextProvider from .experiment import ( resolve_reward_memory_experiment, @@ -37,7 +37,6 @@ LEGACY_PENDING_MIGRATION_SCHEMA_VERSION = ( "turn_reward_memory_legacy_pending_migration_v0" ) -_SAFE_PATH_TOKEN = re.compile(r"[^A-Za-z0-9._-]+") _PENDING_PROVIDER_STATUSES = { "committed_pending", "provider_unavailable", @@ -92,7 +91,7 @@ def _base( def _identity_token(value: str, *, fallback: str) -> str: - readable = _SAFE_PATH_TOKEN.sub("-", str(value or "")).strip("-") or fallback + readable = PATH_SEGMENT_UNSAFE_RE.sub("-", str(value or "")).strip("-") or fallback digest = hashlib.sha256(str(value or "").encode("utf-8")).hexdigest()[:12] return f"{readable[:60]}-{digest}" diff --git a/loopx/capabilities/steward_memory/runtime.py b/loopx/capabilities/steward_memory/runtime.py index 3d9f1ce556..0abd96f7cf 100644 --- a/loopx/capabilities/steward_memory/runtime.py +++ b/loopx/capabilities/steward_memory/runtime.py @@ -3,7 +3,6 @@ import json import os -import re import shutil import subprocess import tempfile @@ -17,6 +16,7 @@ from ...control_plane.effect_runtime import EffectRuntimeRejected, effect_runtime_result from ...control_plane.runtime.public_safety import public_safe_compact_text +from ...paths import PATH_SEGMENT_UNSAFE_RE from ..context_providers.openviking_scope import classify_openviking_scope from .machine_defaults import ( DEFAULT_RECALL_TIMEOUT_SECONDS, @@ -35,7 +35,6 @@ SPEAKER_PREFERENCE_CHAR_LIMIT = 1500 EVENT_SNIPPET_CHAR_LIMIT = 600 COMMIT_TIMEOUT_SECONDS = 3.0 -_SAFE_TURN_SEGMENT_RE = re.compile(r"[^A-Za-z0-9._-]+") CommandRunner = Callable[..., subprocess.CompletedProcess[str]] @@ -572,7 +571,7 @@ def recall_steward_memory_for_turn( def _safe_event_filename(turn_id: str) -> str: - cleaned = _SAFE_TURN_SEGMENT_RE.sub("-", str(turn_id or "").strip()).strip("-.")[:64] + cleaned = PATH_SEGMENT_UNSAFE_RE.sub("-", str(turn_id or "").strip()).strip("-.")[:64] if not cleaned: cleaned = f"turn-{int(time.time())}" return f"{cleaned}.md" diff --git a/loopx/extensions/openviking_semantic_preference/history_export.py b/loopx/extensions/openviking_semantic_preference/history_export.py index 2d3721dc36..40146c1f10 100644 --- a/loopx/extensions/openviking_semantic_preference/history_export.py +++ b/loopx/extensions/openviking_semantic_preference/history_export.py @@ -33,6 +33,7 @@ from ...control_plane.runtime.public_safety import public_safe_compact_text from ...entrypoint import pin_utf8_stdio from ...history import collect_history, validate_goal_id_path_segment +from ...paths import PATH_SEGMENT_UNSAFE_RE from ...public_safe_text import IDENTITY_TOKEN_PATTERN as _PUBLIC_GOAL_ID_RE HISTORY_EXPORT_SCHEMA_VERSION = "loopx_history_conclusion_export_v0" @@ -42,7 +43,6 @@ _CORPUS_DIR = "history-conclusions" _MANIFEST_FILE = ".loopx-history-conclusion-export.json" _MANIFEST_SCHEMA_VERSION = "loopx_history_conclusion_export_manifest_v0" -_SLUG_RE = re.compile(r"[^A-Za-z0-9._-]+") # Bounded ISO-8601-ish timestamp: digits, T/space, colon, dot, +/- and Z only. _TIMESTAMP_RE = re.compile(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}[T ][0-9:.+\-]{1,20}Z?$") _SECRET_SHAPED_GOAL_ID_RE = re.compile( @@ -88,7 +88,7 @@ def _safe_generated_at(value: Any) -> str: def _slug(value: str) -> str: - slug = _SLUG_RE.sub("-", value).strip("-") + slug = PATH_SEGMENT_UNSAFE_RE.sub("-", value).strip("-") return slug[:80] or "run" diff --git a/loopx/paths.py b/loopx/paths.py index 420aed1588..d6b1e765f1 100644 --- a/loopx/paths.py +++ b/loopx/paths.py @@ -2,11 +2,16 @@ import ast import os +import re import stat from enum import StrEnum from pathlib import Path +# One alphabet for repository-relative file-name segments. Callers keep their +# own fallback, truncation and digest policy. +PATH_SEGMENT_UNSAFE_RE = re.compile(r"[^A-Za-z0-9._-]+") + DEFAULT_RUNTIME_ROOT = Path.home() / ".loopx" LEGACY_RUNTIME_ROOT = Path.home() / ".codex" / "loopx" DEFAULT_PROJECT_REGISTRY = Path(".loopx") / "registry.json" diff --git a/tests/architecture/test_path_segment_alphabet_owner.py b/tests/architecture/test_path_segment_alphabet_owner.py new file mode 100644 index 0000000000..4f132fed54 --- /dev/null +++ b/tests/architecture/test_path_segment_alphabet_owner.py @@ -0,0 +1,34 @@ +"""One owner for the safe path-segment alphabet.""" + +from __future__ import annotations + +import inspect + +from loopx import paths +from loopx.capabilities.agent_turn_recall import runtime as agent_turn_recall +from loopx.capabilities.reward_memory import outcome_lifecycle +from loopx.capabilities.steward_memory import runtime as steward_memory +from loopx.extensions.openviking_semantic_preference import history_export + +CONSUMERS = (agent_turn_recall, outcome_lifecycle, steward_memory, history_export) + + +def test_every_consumer_borrows_the_owner_alphabet() -> None: + owner = paths.PATH_SEGMENT_UNSAFE_RE + for module in CONSUMERS: + assert module.PATH_SEGMENT_UNSAFE_RE is owner, module.__name__ + + +def test_consumers_keep_allowed_punctuation_and_replace_the_rest() -> None: + value = "a.b_c-d/e f" + readable = "a.b_c-d-e-f" + assert agent_turn_recall._safe_token(value, fallback="fallback").startswith(f"{readable}-") + assert outcome_lifecycle._identity_token(value, fallback="fallback").startswith(f"{readable}-") + assert steward_memory._safe_event_filename(value) == f"{readable}.md" + assert history_export._slug(value) == readable + + +def test_consumers_do_not_restate_the_alphabet() -> None: + literal = r"[^A-Za-z0-9._-]+" + for module in CONSUMERS: + assert literal not in inspect.getsource(module), module.__name__