Skip to content

symfonic.memory.backends.postgres_conversation_forget

postgres_conversation_forget

Kind-aware transcript erase, in one transaction with its generation advance.

forget_native async

forget_native(pool: Any, scope: MemoryScope) -> int

Erase a kind-aware scope, its intents and receipts; advance its generation.

Source code in src/symfonic/memory/backends/postgres_conversation_forget.py
async def forget_native(pool: Any, scope: MemoryScope) -> int:
    """Erase a kind-aware scope, its intents and receipts; advance its generation."""
    joined = bound_connection(pool) is not None
    try:
        async with pool.transaction() as locked:
            # Hold cursor writers out from enumeration through deletion.
            await lock_fresh(locked, "SHARE ROW EXCLUSIVE", joined=joined)
            conn = await pool.acquire()
            try:
                # append_final takes its ROW EXCLUSIVE lock before its row lock.
                from symfonic.memory.backends.postgres_conversation_fork import forget_receipts

                await forget_receipts(conn, scope, _native_child_prefix(scope))
                await clear(conn, scope, _native_child_prefix(scope))
                # Retire the intent and stale every earlier turn atomically.
                await advance(conn, scope)
                candidates = await conn.fetch(
                    _NATIVE_CURSORS, scope.path, _native_child_prefix(scope)
                )
                paths = sorted(
                    {
                        row["scope_path"]
                        for row in candidates
                        if _native_covers(scope, row["scope_path"])
                    }
                )
                if not paths:
                    return 0
                rows = await conn.fetch(
                    "DELETE FROM native_conversation_cursors "
                    "WHERE scope_path = ANY($1::text[]) RETURNING scope_path",
                    paths,
                )
                return len(rows)
            finally:
                await pool.release(conn)
    except MemoryContractError:
        raise
    except Exception as exc:  # noqa: BLE001
        raise MemoryUnavailable("conversation history could not be erased") from exc