Skip to content

symfonic.memory.backends.postgres_lease_identity

postgres_lease_identity

Bounded native lease comparison without changing persisted capability keys.

This remains a transitional private adapter, not scalable native admission. Native paths begin with [; validated v0 paths cannot. Existing JSON rows remain authoritative through a canonical-root expression index.

native_overlap async

native_overlap(conn: Any, scope: MemoryScope, acquired_at: Any) -> bool

Compare at most 256 live identities under the complete kind-aware root.

Both queries use the digest/expiry index; equality rechecks the complete root before LIMIT. Unclassified live paths refuse globally, including old direct writes. Index assistance is not an absolute I/O bound under crafted digest collisions or dead tuples; the timeout remains the execution bound. The one-second budget covers both checks, after the tenant lock.

Source code in src/symfonic/memory/backends/postgres_lease_identity.py
async def native_overlap(conn: Any, scope: MemoryScope, acquired_at: Any) -> bool:
    """Compare at most 256 live identities under the complete kind-aware root.

    Both queries use the digest/expiry index; equality rechecks the complete
    root before LIMIT. Unclassified live paths refuse globally, including old
    direct writes. Index assistance is not an absolute I/O bound under crafted
    digest collisions or dead tuples; the timeout remains the execution bound.
    The one-second budget covers both checks, after the tenant lock.
    """
    try:
        async with asyncio.timeout(1.0):
            ambiguous = await conn.fetch(_NATIVE_AMBIGUOUS, acquired_at, timeout=1.0)
            if ambiguous:
                raise MemoryContractError("unclassified live native lease identity")
            rows = await conn.fetch(
                _NATIVE_LIVE, acquired_at, scope.ancestor(1).path, timeout=1.0
            )
    except TimeoutError as error:
        raise MemoryContractError("native lease identity check exceeded its time budget") from error
    if len(rows) > _NATIVE_LIMIT:
        raise MemoryContractError("native lease identity check exceeds 256 live rows")
    held = [scope_from_path(row["scope_path"]) for row in rows]
    return any(previous.covers(scope) or scope.covers(previous) for previous in held)

validated_lease

validated_lease(scope: MemoryScope, owner: str, ttl_seconds: float) -> Lease

Validate authority before acquiring any database connection.

Source code in src/symfonic/memory/backends/postgres_lease_identity.py
def validated_lease(scope: MemoryScope, owner: str, ttl_seconds: float) -> Lease:
    """Validate authority before acquiring any database connection."""
    if type(scope) is not MemoryScope:
        raise MemoryContractError("lease acquisition requires a validated MemoryScope")
    scope.validate()
    scope_from_path(scope.path)
    try:
        ttl = float(ttl_seconds)
    except (TypeError, ValueError, OverflowError) as error:
        raise MemoryContractError("lease ttl must be finite and positive") from error
    if not math.isfinite(ttl) or ttl <= 0:
        raise MemoryContractError("lease ttl must be finite and positive")
    if type(owner) is not str or not owner or "\x00" in owner:
        raise MemoryContractError("lease owner must be a nonempty string without NUL")
    try:
        owner.encode("utf-8")
    except UnicodeError as error:
        raise MemoryContractError("lease owner must be valid UTF-8") from error
    return Lease(scope_path=scope.path, owner=owner, ttl_seconds=ttl)