Skip to content

API Reference parrot.tools.working_memory.task_memory.store._base

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.tools.working_memory.task_memory.store._base

View source

Shared machinery every task-memory store backend reuses (FEAT-538).

The in-memory and PostgreSQL stores must be behaviourally identical (AC2), which is hard to achieve if each re-derives the rules. This module owns the parts that have exactly one correct answer, so a backend implements only genuine storage:

  • Scope enforcement — :func:ensure_scope, and the deliberate choice to report a foreign task as absent rather than forbidden.
  • Opaque scoped cursors — :func:encode_cursor / :func:decode_cursor. A cursor is signed to its scope and its query, so it cannot be replayed in another scope or against a different filter.
  • Idempotent redelivery classification — :func:classify_events, which separates exact redeliveries (no-op, consume no sequence) from conflicting reuse of an event_id (rejected) and genuinely new events.
  • Bounded page sizes — :func:bounded_limit.

None of this touches a database. The module stays leaf-safe so the contract, the in-memory backend and the durable backend can all share it.

Class BaseTaskMemoryStore

Base class carrying the rules both backends share.

A backend subclasses this and implements only genuine storage. The shared helpers above are exposed as methods so a subclass never has to reimplement scope checks or cursor handling — the two places where an inconsistency between backends would be a security bug rather than a behaviour difference.

Attributes:

Attribute Type Description
default_task_page int
default_event_page int

Method BaseTaskMemoryStore.close

Release any resources the store holds.

close() -> None

Class EventClassification

How one batch of events splits against a task's existing journal.

Parameter Type Default Description
fresh Sequence[JournalEvent] The genuinely new events.
duplicates Sequence[str] Ids of exact redeliveries.

Attributes:

Attribute Type Description
fresh Tuple[JournalEvent, ...] Events that must consume new sequence numbers, in order.
duplicates Tuple[str, ...] Ids recognised as exact redeliveries. They consume no sequence number, which is what keeps sequences contiguous.
is_noop bool Whether the whole batch was already applied.

Attribute MAX_PAGE_LIMIT

Type: int

Function bounded_limit

Clamp a requested page size into range.

bounded_limit(limit: Optional[int], *, default: int, maximum: int = MAX_PAGE_LIMIT) -> int

Parameter Type Default Description
limit Optional[int] The caller's requested size, or None for the default.
default int Size to use when limit is None.
maximum int MAX_PAGE_LIMIT Hard ceiling.

Returns: int — The effective page size.

Raises:

  • CursorError — If limit is not positive. A zero or negative page is a caller bug, not an empty page.

Function classify_events

Split a batch into genuinely new events and exact redeliveries.

This runs before any sequence number is allocated, which is what makes redelivery a true no-op rather than a gap-producing skip. It also runs before the expected-revision check, because a caller retrying a batch that in fact committed has a legitimately stale revision — rejecting it as a conflict would be wrong.

classify_events(events: Sequence[JournalEvent], existing: Mapping[str, JournalEvent]) -> EventClassification

Parameter Type Default Description
events Sequence[JournalEvent] The batch, in order.
existing Mapping[str, JournalEvent] Already-committed events for this task, by event_id.

Returns: EventClassification — The classification.

Raises:

  • ReducerError — If an event_id is reused with a different payload — that is a bug or an attack, not a retry — or if the batch itself repeats an id.

Function decode_cursor

Decode and validate an opaque cursor.

decode_cursor(cursor: str, scope: TaskScope, query: Mapping[str, Any]) -> Dict[str, Any]

Parameter Type Default Description
cursor str The cursor from a previous page.
scope TaskScope The scope presenting it.
query Mapping[str, Any] The query parameters it must belong to.

Returns: Dict[str, Any] — The decoded position marker.

Raises:

  • CursorError — If the cursor is malformed, over-length, or was issued for a different scope or a different query. All three are the same answer to the caller: this cursor is not usable here.

Function encode_cursor

Encode an opaque cursor bound to its scope and query.

encode_cursor(scope: TaskScope, query: Mapping[str, Any], position: Mapping[str, Any]) -> str

Parameter Type Default Description
scope TaskScope The scope issuing the cursor.
query Mapping[str, Any] The query parameters it is valid for.
position Mapping[str, Any] The backend's position marker.

Returns: str — A URL-safe base64 string.

Raises:

  • CursorError — If the encoded cursor would exceed :data:Limits.MAX_CURSOR.

Function ensure_scope

Raise unless actual is exactly expected.

ensure_scope(expected: TaskScope, actual: TaskScope, *, subject: str) -> None

Parameter Type Default Description
expected TaskScope The trusted runtime scope of the caller.
actual TaskScope The scope stored on the row being touched.
subject str What is being accessed, for the message.

Raises:

  • ScopeViolation — If the scopes differ. The message deliberately names no identifiers from the foreign scope.

Function goal_preview

Return a bounded preview of a task's goal for a listing.

goal_preview(goal: str, *, chars: int) -> str

Parameter Type Default Description
goal str The full goal text.
chars int Maximum characters to keep.

Returns: str — The truncated goal. Truncation happens on characters, never on serialized JSON — this is display text, not a payload.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally