Skip to content

symfonic.memory.backends.mongo_lease_overlap

mongo_lease_overlap

Bounded, kind-aware live lease overlap checks for Mongo.

lease_covers_operation

lease_covers_operation(scope: MemoryScope, armed: TenantScope | None) -> bool

A v0 lease may fence only its own receipt scope or a descendant.

Source code in src/symfonic/memory/backends/mongo_lease_overlap.py
def lease_covers_operation(scope: MemoryScope, armed: TenantScope | None) -> bool:
    """A v0 lease may fence only its own receipt scope or a descendant."""
    if armed is None:
        return False
    from symfonic.capabilities.memory.graph_rows import tenant_scope

    prefix = tenant_scope(scope).path
    return len(prefix) <= len(armed.path) and armed.path[:len(prefix)] == prefix

live_overlap async

live_overlap(collection: Any, scope: MemoryScope, *, now: Any = None, session: Any = None) -> bool

Compare complete paths after a majority or owned snapshot read.

Source code in src/symfonic/memory/backends/mongo_lease_overlap.py
async def live_overlap(
    collection: Any,
    scope: MemoryScope,
    *,
    now: Any = None,
    session: Any = None,
) -> bool:
    """Compare complete paths after a majority or owned snapshot read."""
    query: dict[str, Any] = {"tenant": scope.tenant}
    if now is None:
        query["$expr"] = {"$gt": ["$expires_at", "$$NOW"]}
    else:
        query["expires_at"] = {"$gt": now}
    options = {"session": session} if session is not None else {}
    rows = await _settle(collection.find(query, **options).to_list(length=_MAX_ROWS + 1))
    if len(rows) > _MAX_ROWS:
        raise MemoryContractError("Mongo lease overlap exceeds 256 live rows")
    for row in rows:
        held = scope_from_path(row["_id"])
        if held.path != row["_id"] or held.tenant != row["tenant"]:
            raise MemoryContractError("unclassified live Mongo lease identity")
        if held.covers(scope) or scope.covers(held):
            return True
    return False