Skip to content

symfonic.capabilities.memory.turn_scope

turn_scope

The scope a turn's memory stages act on, in their composition's identity.

A kernel turn may state its scope (TurnRequest.scope), and that scope wins over the one the capability was configured with: a bundle is folded once and serves many tenants. CORE-171 adds the half that was missing. The stated scope is converted in the identity version the capability was composed with, not always positionally.

  • A native v1 composition keeps every kind/ID pair of the stated scope, at any depth. A stated scope that cannot name its kinds is refused.
  • A positional v0 composition takes only a stated positional MemoryScope. A typed stated scope is refused: the turn's prompt compiles under the typed scope's native key (kernel.contracts.scope_path), so a positional recall could never render into it, and converting would have dropped recall from the prompt silently instead.
  • A MemoryScope of the other version is refused, never compared. The two versions never share visibility, so a mixed turn would read or write rows its composition never declared.

stated_turn_scope

stated_turn_scope(bound: Any, stated: Any) -> Any

The turn's scope if it stated one, else bound; same version as bound.

Source code in src/symfonic/capabilities/memory/turn_scope.py
def stated_turn_scope(bound: Any, stated: Any) -> Any:
    """The turn's scope if it stated one, else ``bound``; same version as ``bound``."""
    if not stated:
        return bound
    if not isinstance(bound, MemoryScope):
        return stated
    from symfonic.capabilities.memory.binding import as_native_memory_scope

    if isinstance(stated, MemoryScope):
        result = stated
    elif bound.identity_version == 1:
        result = as_native_memory_scope(stated)
    else:
        raise MemoryContractError(_TYPED_FOR_POSITIONAL)
    if result.identity_version != bound.identity_version:
        raise MemoryContractError(
            _MIXED.format(stated=result.identity_version, bound=bound.identity_version)
        )
    return result