Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions loopx/capabilities/agent_turn_recall/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import hashlib
import json
import os
import re
import tempfile
from collections.abc import Mapping
from datetime import datetime, timezone
Expand All @@ -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,
Expand All @@ -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}"

Expand Down
5 changes: 2 additions & 3 deletions loopx/capabilities/reward_memory/outcome_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import hashlib
import json
import os
import re
import tempfile
from collections.abc import Mapping
from datetime import datetime, timezone
Expand All @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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}"

Expand Down
5 changes: 2 additions & 3 deletions loopx/capabilities/steward_memory/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import json
import os
import re
import shutil
import subprocess
import tempfile
Expand All @@ -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,
Expand All @@ -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]]

Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
Expand Down Expand Up @@ -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"


Expand Down
5 changes: 5 additions & 0 deletions loopx/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 34 additions & 0 deletions tests/architecture/test_path_segment_alphabet_owner.py
Original file line number Diff line number Diff line change
@@ -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__
Loading