Skip to content

symfonic.kernel.contracts.scope_path

scope_path

The one string a turn's stated scope binds its prompt and receipts under.

A turn's scope path is compared in four places: prompting compiles under it, the kernel checks each compiler admission against it, the model-port binding carries it, and a rendered selection is refused unless it names it. Each used str(scope.path). That is right for a MemoryScope, whose path is already its canonical string, and wrong for a typed scope, whose path is a tuple of ScopeLevel values: str() of that tuple matches no memory key, so a recall hydrated for the turn's own native scope was refused at compile time and the prompt carried no memory (CORE-171).

A typed path becomes the kind-aware envelope native memory keys by (MemoryScope.from_levels(...).path). The format is restated here, not imported, because kernel.contracts is standard library only; tests/kernel/test_core171_turn_scope_path.py pins the two strings equal. Every other value keeps its previous string.

scope_root

scope_root(scope_path: str) -> str

The root level's ID of a canonical scope path, or "".

A positional path is tenant/.... A native envelope is JSON, and its IDs may contain /, so splitting it on / named the whole envelope as the tenant.

Source code in src/symfonic/kernel/contracts/scope_path.py
def scope_root(scope_path: str) -> str:
    """The root level's ID of a canonical scope path, or ``""``.

    A positional path is ``tenant/...``. A native envelope is JSON, and its
    IDs may contain ``/``, so splitting it on ``/`` named the whole envelope
    as the tenant.
    """
    if not scope_path:
        return ""
    if scope_path.startswith("["):
        try:
            value = json.loads(scope_path)
            root = value[2][0][1]
        except (ValueError, TypeError, IndexError, KeyError):
            return ""
        if value[0] != _NATIVE_FORMAT or type(root) is not str:
            return ""
        return root
    return scope_path.split("/", 1)[0].strip()

turn_scope_path

turn_scope_path(scope: Any) -> str

"" when no scope was stated; else its canonical path string.

Source code in src/symfonic/kernel/contracts/scope_path.py
def turn_scope_path(scope: Any) -> str:
    """``""`` when no scope was stated; else its canonical path string."""
    if scope is None:
        return ""
    path = getattr(scope, "path", "")
    if not path:
        return ""
    if isinstance(path, str):
        return path
    levels = _typed_levels(path)
    if levels is None:
        return str(path)
    return json.dumps([_NATIVE_FORMAT, 1, levels], ensure_ascii=True, separators=(",", ":"))