Skip to content

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

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

parrot.tools.working_memory.task_memory.store.memory

View source

Process-local task-memory store (FEAT-538, Delivery A).

.. warning::

This backend is not durable (D2). It holds journals and projections in Python dictionaries: nothing survives a process restart, a crash, or a second pod. It exists for tests and for single-process compatibility deployments. Durable multi-pod continuity is Delivery B's PostgreSQL store, and only that store may be described as recoverable.

What this module is responsible for is being behaviourally identical to the durable backend (AC2). It achieves that by owning as little judgement as possible:

  • Scope checks, opaque cursors, redelivery classification and page bounding come from :mod:.._base.
  • The projection comes from :func:..reducer.reduce. This store never computes task state itself; if it did, the two backends could disagree about what a journal means.

The one thing it genuinely owns is atomicity, and the shape of that is worth stating plainly:

  1. Take the task's own lock. Appends to the same task serialize; different tasks proceed concurrently, because the lock is per task and not global.
  2. Classify the batch against committed events — before any sequence is allocated and before the revision is checked, so an exact redelivery is a no-op rather than a conflict.
  3. Check expected_revision.
  4. Check capacity.
  5. Reduce every event into a local variable, allocating contiguous sequences as it goes.
  6. Only once the whole batch has reduced successfully, publish it to the record.

Steps 2–5 mutate nothing. That is what makes "a rejected command changes nothing" true by construction rather than by careful cleanup: there is no partially-applied intermediate to roll back.

Class InMemoryTaskMemoryStore

Process-local implementation of the task-memory store contract.

Not durable — see the module docstring.

Parameter Type Default Description
config Optional[TaskMemoryConfig] None Capacity and retention configuration. Defaults to :class:~..config.TaskMemoryConfig's own defaults.

Method InMemoryTaskMemoryStore.create_task

Create a task and append its first batch of events atomically.

There is no window in which a task exists with an empty journal: the record is only published once its whole creation batch has reduced successfully.

Creation is idempotent. Redelivering an identical creation batch for an existing task returns a no-op result rather than raising, for the same reason redelivering an append does.

create_task(scope: TaskScope, *, goal: str, events: Sequence[JournalEvent], transaction: Optional[Transaction] = None) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope the task belongs to.
goal str The task's goal. Stamped onto the task_started event when that event does not already carry it.
events Sequence[JournalEvent] Initial events, beginning with task_started.
transaction Optional[Transaction] None Accepted for contract parity. This backend has no database to enlist in; its atomicity comes from the per-task lock and stage-then-publish.

Returns: AppendResult — The append result, including the reduced projection.

Raises:

  • LimitExceeded — If the scope already holds the maximum number of open tasks.
  • ScopeViolation — If the task id already exists in a different scope.
  • ReducerError — If the batch is malformed.
  • PlanValidationError — If the initial plan is invalid.

Method InMemoryTaskMemoryStore.append_events

Append a batch of events under an optimistic revision check.

append_events(scope: TaskScope, task_id: str, events: Sequence[JournalEvent], *, expected_revision: Optional[int] = None, transaction: Optional[Transaction] = None) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The task to append to.
events Sequence[JournalEvent] The batch, in order.
expected_revision Optional[int] None Revision the caller believes is current. None skips the check.
transaction Optional[Transaction] None Accepted for contract parity; unused here.

Returns: AppendResult — The append result.

Raises:

  • ScopeViolation — If the task does not exist in this scope.
  • RevisionConflict — If the revision is stale and the batch is not a pure redelivery. Nothing is mutated.
  • ReducerError — If an id is reused with a different payload, or an event is malformed.
  • LimitExceeded — If the journal's capacity is exhausted.

Method InMemoryTaskMemoryStore.load_snapshot

Load one task's projection consistently.

load_snapshot(scope: TaskScope, task_id: str) -> Optional[TaskSnapshot]

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The task to load.

Returns: Optional[TaskSnapshot] — The snapshot, or None when no such task exists in this scope.

Method InMemoryTaskMemoryStore.list_tasks

List tasks in a scope, newest activity first.

Ordering is (updated_at desc, task_id asc). The task id tiebreak is what makes the order a total one, so keyset pagination cannot skip or repeat a row when two tasks share a timestamp.

list_tasks(scope: TaskScope, *, statuses: Optional[Sequence[TaskStatus]] = None, limit: int = 20, cursor: Optional[str] = None) -> TaskPage

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
statuses Optional[Sequence[TaskStatus]] None Restrict to these statuses; None means open (non-terminal) tasks only.
limit int 20 Bounded page size.
cursor Optional[str] None Opaque cursor from a previous page.

Returns: TaskPage — A bounded page of summaries.

Raises:

  • CursorError — If the cursor is malformed, out of bounds, or was issued for a different scope or query.

Method InMemoryTaskMemoryStore.list_events

Page a task's journal in ascending sequence order.

Reads never append.

list_events(scope: TaskScope, task_id: str, *, after_seq: int = 0, limit: int = 50, as_of_seq: Optional[int] = None) -> EventPage

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The task to read.
after_seq int 0 Return events strictly after this sequence.
limit int 50 Bounded page size.
as_of_seq Optional[int] None Fence the read at this sequence.

Returns: EventPage — A bounded page of events.

Raises:

  • ScopeViolation — If the task does not exist in this scope.

Method InMemoryTaskMemoryStore.count_events

Return how many events a task's journal holds.

count_events(scope: TaskScope, task_id: str) -> int

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The task to measure.

Returns: int — The event count, or 0 when the task does not exist in this scope.

Method InMemoryTaskMemoryStore.transaction

Open a transaction this store and an artifact store can share.

transaction() -> AsyncIterator[Transaction]

Returns: AsyncIterator[Transaction]

Method InMemoryTaskMemoryStore.close

Drop every journal and projection held in memory.

close() -> None

Class NoOpTransaction

A :class:~parrot.interfaces.task_memory.Transaction for a store with no database.

The contract is about atomicity, not about SQL. This backend gets its atomicity from the per-task lock plus the stage-then-publish discipline in :meth:InMemoryTaskMemoryStore.append_events, so a transaction handle here is a real object with a real lifecycle but no work to do.

It is not a lie: rollback() genuinely closes it, and an already-closed transaction reports is_active is False.

Attributes:

Attribute Type Description
is_active bool Whether this transaction is still open.

Method NoOpTransaction.rollback

Close the transaction without committing anything.

rollback() -> None

Method NoOpTransaction.commit

Close the transaction, accepting whatever was already published.

commit() -> None

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