Skip to content

symfonic.memory.backends.postgres_erase_vector_proof

postgres_erase_vector_proof

Bounded vector owner and physical-absence proof for native tenant retirement.

Vector admission requires the durable phase. Eraser startup attests exact grants.

preflight async

preflight(conn: Any, schema: str, scope: Any) -> None

Bound distinct owners, refusing v0, null, malformed and crossing rows.

Source code in src/symfonic/memory/backends/postgres_erase_vector_proof.py
async def preflight(conn: Any, schema: str, scope: Any) -> None:
    """Bound distinct owners, refusing v0, null, malformed and crossing rows."""
    from symfonic.memory.backends.postgres_native_subtree import _descends

    table = await require_ready(conn, schema)
    tenant, prefix = scope.tenant_id, scope.subtree_prefix
    async with index_probes(conn, ordered=True, presorted=True):
        if await conn.fetchval(
            f"SELECT COALESCE((SELECT scope_path IS NULL FROM {table} "
            'WHERE tenant_id COLLATE "C">=$1 AND tenant_id COLLATE "C"<=$1 '
            'ORDER BY tenant_id COLLATE "C" DESC, scope_path COLLATE "C" DESC LIMIT 1), false)',
            tenant,
        ):
            raise MemoryContractError("native retirement residual tenant data: memory_vectors")
        owned = await conn.fetch(owners_sql(table, own=True), tenant)
        crossed = await conn.fetch(owners_sql(table), prefix, prefix[:-1] + "0")
    if len(owned) > BUDGET or len(crossed) > BUDGET:
        raise MemoryContractError("native retirement vector ownership budget exceeded")
    if any(not _descends(row["scope_path"], scope) for row in owned) or any(
        row["tenant_id"] != tenant or not _descends(row["scope_path"], scope)
        for row in crossed
    ):
        raise MemoryContractError("native retirement residual tenant data: memory_vectors")

require_empty async

require_empty(conn: Any, schema: str, scope: Any) -> None

Independently prove physical absence through indexed equality and range seeks.

Source code in src/symfonic/memory/backends/postgres_erase_vector_proof.py
async def require_empty(conn: Any, schema: str, scope: Any) -> None:
    """Independently prove physical absence through indexed equality and range seeks."""
    table = await require_ready(conn, schema)
    prefix = scope.subtree_prefix
    async with index_probes(conn, ordered=True, presorted=True):
        owned = await conn.fetchval(
            f"SELECT (SELECT jsonb_build_array(scope_path) FROM {table} "
            'WHERE tenant_id COLLATE "C">=$1 AND tenant_id COLLATE "C"<=$1 '
            'ORDER BY tenant_id COLLATE "C", scope_path COLLATE "C" LIMIT 1) IS NOT NULL',
            scope.tenant_id,
        )
        crossed = await conn.fetchval(range_sql(table), prefix, prefix[:-1] + "0")
    if owned or crossed:
        raise MemoryContractError("native retirement residual tenant data: memory_vectors")

require_ready async

require_ready(conn: Any, schema: str) -> str

Pin the vector relation and attest same-schema fences and exact C indexes.

Source code in src/symfonic/memory/backends/postgres_erase_vector_proof.py
async def require_ready(conn: Any, schema: str) -> str:
    """Pin the vector relation and attest same-schema fences and exact C indexes."""
    from symfonic.memory.backends.postgres_vector_retirement import authority

    table = await conn.fetchval("SELECT format('%I.memory_vectors', $1::text)", schema)
    await conn.execute(f"LOCK TABLE {table} IN SHARE UPDATE EXCLUSIVE MODE")
    if not await conn.fetchval(_TABLE_CHECK, table):
        raise MemoryContractError(UNREADY)
    if await authority(conn, schema) is None:
        raise MemoryContractError(UNREADY)
    for name, columns in INDEXES:
        rows = await conn.fetch(_INDEX_CHECK, table, list(columns), name)
        if len(rows) != 1 or rows[0]["valid"] is not True:
            raise MemoryContractError(UNREADY)
    return table