Skip to content

symfonic.memory.backends.postgres_lifecycle_deadline

postgres_lifecycle_deadline

Private graph-only lifecycle budget and commit outcome labels.

LifecycleCommitIndeterminateError

Bases: ForgetCommitIndeterminateError

COMMIT was not confirmed; reconcile durable state before retrying.

cycle_flush_join async

cycle_flush_join(pool: Any)

Permit only the selected exact HMS flush await to join its outer owner.

CycleWrites encloses one call to the exact GraphBackedHms.flush method; that implementation has no callback or other lifecycle verb. The permit is removed as soon as that await returns, before outer COMMIT.

Source code in src/symfonic/memory/backends/postgres_lifecycle_deadline.py
@asynccontextmanager
async def cycle_flush_join(pool: Any):
    """Permit only the selected exact HMS flush await to join its outer owner.

    CycleWrites encloses one call to the exact GraphBackedHms.flush method;
    that implementation has no callback or other lifecycle verb. The permit
    is removed as soon as that await returns, before outer COMMIT.
    """
    owner = _CYCLE_DEADLINE.get()
    if owner is None or owner[0] is not pool or owner[2] is not asyncio.current_task():
        raise MemoryContractError("native lifecycle flush has no owned cycle deadline")
    token = _FLUSH_JOIN.set((pool, asyncio.current_task()))
    try:
        yield
    finally:
        _FLUSH_JOIN.reset(token)

cycle_lifecycle_transaction async

cycle_lifecycle_transaction(pool: Any, *, deadline_at: float)

Own the native cycle's only transaction through its top-level COMMIT.

Source code in src/symfonic/memory/backends/postgres_lifecycle_deadline.py
@asynccontextmanager
async def cycle_lifecycle_transaction(pool: Any, *, deadline_at: float):
    """Own the native cycle's only transaction through its top-level COMMIT."""
    if bound_connection(pool) is not None:
        raise MemoryContractError(
            "native lifecycle requires an independent transaction for commit budget"
        )
    token = _CYCLE_DEADLINE.set((pool, deadline_at, asyncio.current_task()))
    try:
        async with lifecycle_transaction(pool, deadline_at=deadline_at) as conn:
            yield conn
    finally:
        _CYCLE_DEADLINE.reset(token)

lifecycle_transaction async

lifecycle_transaction(pool: Any, *, deadline_at: float)

Share forget's acquisition-to-COMMIT budget and ambient connection.

A confirmed precommit timeout rolls back. An unconfirmed COMMIT outcome keeps its distinct error because rollback cannot be asserted. An arbitrary ambient pool transaction cannot bound its outer COMMIT and is refused before mutation. Only the cycle-owned deadline may be joined.

Source code in src/symfonic/memory/backends/postgres_lifecycle_deadline.py
@asynccontextmanager
async def lifecycle_transaction(pool: Any, *, deadline_at: float):
    """Share forget's acquisition-to-COMMIT budget and ambient connection.

    A confirmed precommit timeout rolls back. An unconfirmed COMMIT outcome
    keeps its distinct error because rollback cannot be asserted.
    An arbitrary ambient pool transaction cannot bound its outer COMMIT and
    is refused before mutation. Only the cycle-owned deadline may be joined.
    """
    owned = _CYCLE_DEADLINE.get()
    permit = _FLUSH_JOIN.get()
    if bound_connection(pool) is not None and (
        owned is None or owned[0] is not pool or owned[1] > deadline_at
        or owned[2] is not asyncio.current_task()
        or permit != (pool, asyncio.current_task())
    ):
        raise MemoryContractError(
            "native lifecycle requires an independent transaction for commit budget"
        )
    if owned is not None and owned[0] is pool:
        deadline_at = owned[1]
    try:
        async with forget_transaction(pool, deadline_at=deadline_at) as conn:
            yield conn
    except ForgetCommitIndeterminateError as exc:
        raise LifecycleCommitIndeterminateError(
            "native lifecycle commit outcome indeterminate; "
            "reconcile before retry; connection discarded"
        ) from exc
    except MemoryContractError as exc:
        if str(exc) != "native subtree execution time budget exceeded":
            raise
        raise MemoryContractError(
            "native lifecycle execution time budget exceeded"
        ) from exc