Skip to content

backend_protocol

backend_protocol

The memory-backend/1.1 protocol and its per-call RequestContext.

A backend is anything that implements these nine operations. The gateway (conductor side) builds a :class:RequestContext for every call from the session it admitted, never from data the musician sent, and a backend trusts the context: principal, canonical scope, permission ceiling and deadline.

Mapping from GH #496's first sketch: remember is declare, export is stream_facts. consolidate is not part of the frozen protocol; a backend that cannot consolidate says so through its capability table.

Classes

RequestContext dataclass

RequestContext(principal, scope, permissions, deadline_at, dispatch_id=None, job_id=None, session_id=None)

The immutable per-call envelope (memory-backend/1.1).

deadline_at is a POSIX timestamp. A conforming backend clamps every lock wait to remaining_seconds and aborts with E10 before it mutates anything once the deadline has passed. scope is canonicalised on construction; permissions is the session's ceiling.

Attributes
remaining_seconds property
remaining_seconds

Seconds left before deadline_at; never negative.

Methods:
require
require(permission)

Raise E2 naming permission when the ceiling lacks it.

Source code in src/marianne/memory/backend_protocol.py
def require(self, permission: Permission) -> None:
    """Raise E2 naming ``permission`` when the ceiling lacks it."""
    if not self.has(permission):
        raise PermissionDenied(
            f"operation needs `{permission.value}`; session holds "
            f"{list(self.permissions) or 'no permissions'}",
            subtype="permission_denied",
        )
require_any
require_any(*permissions)

Raise E2 unless the ceiling holds at least one of permissions.

Source code in src/marianne/memory/backend_protocol.py
def require_any(self, *permissions: Permission) -> None:
    """Raise E2 unless the ceiling holds at least one of ``permissions``."""
    if not any(self.has(p) for p in permissions):
        names = " or ".join(f"`{p.value}`" for p in permissions)
        raise PermissionDenied(
            f"operation needs {names}; session holds "
            f"{list(self.permissions) or 'no permissions'}",
            subtype="permission_denied",
        )

MemoryBackend

Bases: Protocol

memory-backend/1.1: what every memory backend implements.

Every operation takes the gateway-built :class:RequestContext first. Failures raise a typed :class:~marianne.memory.errors.MemoryBackendError subclass; a backend never returns an error as a success-shaped dict.

Methods:
declare
declare(ctx, fact, scope, provenance=None, confidence=1.0, tags=None)

Remember fact with its evidence; idempotent per (claim, evidence).

Source code in src/marianne/memory/backend_protocol.py
def declare(
    self,
    ctx: RequestContext,
    fact: str,
    scope: str,
    provenance: Provenance | None = None,
    confidence: float = 1.0,
    tags: list[str] | None = None,
) -> DeclareResult:
    """Remember ``fact`` with its evidence; idempotent per (claim, evidence)."""
    ...
recall
recall(ctx, query, scope, limit=100, cursor=None, cap_bytes=None)

Facts relevant to query, within the per-response byte cap.

cap_bytes is an optional per-call ceiling the gateway sets to min(recall_cap_bytes, quota_remaining); the backend applies the smaller of it and its own cap, stops reading at it and returns its cursor. None keeps the backend's own cap (the 1.1 behaviour).

Source code in src/marianne/memory/backend_protocol.py
def recall(
    self,
    ctx: RequestContext,
    query: str,
    scope: str,
    limit: int = 100,
    cursor: str | None = None,
    cap_bytes: int | None = None,
) -> RecallResult:
    """Facts relevant to ``query``, within the per-response byte cap.

    ``cap_bytes`` is an optional per-call ceiling the gateway sets to
    ``min(recall_cap_bytes, quota_remaining)``; the backend applies the
    smaller of it and its own cap, stops reading at it and returns its
    cursor. ``None`` keeps the backend's own cap (the 1.1 behaviour).
    """
    ...
verify
verify(ctx, fact_id_or_hash, expected_evidence='')

Re-check a fact against its evidence; read-only.

Source code in src/marianne/memory/backend_protocol.py
def verify(
    self, ctx: RequestContext, fact_id_or_hash: str, expected_evidence: str = "",
) -> VerifyResult:
    """Re-check a fact against its evidence; read-only."""
    ...
bind
bind(ctx, fact_id, target_symbol, evidence_receipt=None)

Bind a freshly verified fact to an action; re-checks disk first.

Source code in src/marianne/memory/backend_protocol.py
def bind(
    self,
    ctx: RequestContext,
    fact_id: str,
    target_symbol: str,
    evidence_receipt: str | None = None,
) -> BindResult:
    """Bind a freshly verified fact to an action; re-checks disk first."""
    ...
forget
forget(ctx, fact_id, reason='')

Tombstone a fact (archive, never silent delete).

Source code in src/marianne/memory/backend_protocol.py
def forget(self, ctx: RequestContext, fact_id: str, reason: str = "") -> ForgetResult:
    """Tombstone a fact (archive, never silent delete)."""
    ...
fetch
fetch(ctx, unit_id, offset, length)

A bounded byte range of an oversized unit.

Source code in src/marianne/memory/backend_protocol.py
def fetch(self, ctx: RequestContext, unit_id: str, offset: int, length: int) -> FetchResult:
    """A bounded byte range of an oversized unit."""
    ...
stream_facts
stream_facts(ctx, scope, since_revision=None)

Export every live fact and tombstone as migration-facts/1 batches.

Source code in src/marianne/memory/backend_protocol.py
def stream_facts(
    self, ctx: RequestContext, scope: str, since_revision: int | None = None,
) -> Iterator[MigrationBatch]:
    """Export every live fact and tombstone as ``migration-facts/1`` batches."""
    ...
migration_ingest
migration_ingest(ctx, migration_id, envelope)

Staged import: begin -> batch... -> finalize | abort.

Source code in src/marianne/memory/backend_protocol.py
def migration_ingest(
    self, ctx: RequestContext, migration_id: str, envelope: MigrationEnvelope,
) -> MigrationResult:
    """Staged import: ``begin`` -> ``batch``... -> ``finalize`` | ``abort``."""
    ...
health
health()

A memory-health/1 record for this backend's binding.

Source code in src/marianne/memory/backend_protocol.py
def health(self) -> MemoryHealth:
    """A ``memory-health/1`` record for this backend's binding."""
    ...

Functions: