Skip to content

API Reference parrot.tools.working_memory.task_memory.reducer

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

parrot.tools.working_memory.task_memory.reducer

View source

The pure task-state reducer (FEAT-538, decision D6).

The journal is the source of truth; :class:TaskState is a derived, versioned projection of it. This module is that derivation, and it is deliberately the most boring code in the feature:

reduce(state: TaskState | None, event: JournalEvent) -> TaskState

Purity is the contract, not a style preference. During replay there are no wall-clock reads, no random identifiers, no validators, no file or network I/O, and no tool execution. Every timestamp comes from event.occurred_at; every identity comes from the event payload. Replaying the same sequence twice must produce byte-identical state, on any host, at any time — otherwise a projection rebuilt after a restart would silently disagree with the one it replaced.

Consequences that follow from that, and that this module enforces:

  • Validators do not run here. step_completed carries the recorded validator_results; the reducer consumes them. Re-running a validator during replay would make the projection depend on the world's current state rather than on the journal.
  • Already-applied sequences are ignored. A re-delivered event whose sequence the projection has already folded in returns the state unchanged. A forward gap raises: silently skipping an event would produce a projection that never existed.
  • A rejected event mutates nothing. Validation happens before any copy is made, and :class:TaskState is frozen, so there is no partially-applied intermediate.

Handler registry

:data:_HANDLERS maps an :class:EventType to its handler. Every member of :class:EventType is registered; an event type with no handler raises :class:ReducerError rather than silently no-op'ing, because an unhandled event that quietly changes nothing is exactly the bug that makes a projection drift from its journal.

Handled-but-immaterial events

Several event families are recorded in the journal but change nothing in the projection: a started call, a successful call, an artifact registration, a degradation marker, a retention intent. These are not "unhandled" — each has an explicit handler with a documented reason, and each returns the state object by identity to say so.

:func:reduce treats that identity return as "no material change" and does not advance revision (it still advances last_event_seq and updated_at, because the event was applied and the task was active). This matters practically: the agent's optimistic-concurrency API takes an expected_revision, and if every observed tool call bumped the revision, then every wm_update_step issued around a tool call would fail with a :class:RevisionConflict through no fault of the caller. Revision tracks material state, which is what an expected-revision check is actually about.

Attribute EVIDENCE_INVALIDATED

Type: str

Attribute MAX_ATTEMPTS

Type: int

Attribute MAX_ATTEMPTS_EXHAUSTED

Type: str

Attribute PLAN_INCOMPLETE

Type: str

Attribute REDUCER_VERSION

Type: int

Attribute UPSTREAM_REOPENED

Type: str

Function reduce

Fold one journal event into a task's projection.

Pure and deterministic: given the same (state, event, scope) it always returns the same result, with no clock, randomness, validator or I/O involved.

reduce(state: Optional[TaskState], event: JournalEvent, *, scope: Optional[TaskScope] = None) -> TaskState

Parameter Type Default Description
state Optional[TaskState] The projection so far, or None to begin a task. Only a task_started event may begin one.
event JournalEvent The event to apply. Its seq must be exactly state.last_event_seq + 1, or a value already folded in.
scope Optional[TaskScope] None The trusted scope to stamp on a new projection. Required when state is None and ignored otherwise. Scope lives on the task row, not on every event: the store owns it and has already checked it before the reducer runs.

Returns: TaskState — The new projection. When event was already applied, the same state object is returned unchanged. When the event was applied but changed nothing material, revision is left alone while last_event_seq and updated_at advance.

Raises:

  • ReducerError — If the event is malformed for the current state: a missing or duplicate task start, a task id mismatch, a forward sequence gap, an unsupported reducer version, an unhandled event family, an ordinary mutation of a terminal task, or a payload that contradicts its event type.
  • PlanValidationError — If a plan change would produce an invalid graph.

Function replay

Rebuild a projection by folding a whole journal.

This is the operation that must be reproducible: replaying the same events on any host, at any time, yields byte-identical state. It is what a lazy projection migration runs under a task lock when the stored reducer version is older than this build's.

replay(events: Sequence[JournalEvent], *, scope: TaskScope) -> Optional[TaskState]

Parameter Type Default Description
events Sequence[JournalEvent] The journal, in ascending sequence order.
scope TaskScope The trusted scope the task belongs to.

Returns: Optional[TaskState] — The final projection, or None for an empty journal.

Raises:

  • ReducerError — If the journal is malformed (see :func:reduce).
  • PlanValidationError — If a recorded plan change is invalid.

Function validate_plan_graph

Validate a plan's dependency graph.

validate_plan_graph(steps: Tuple[TaskStep, ...]) -> None

Parameter Type Default Description
steps Tuple[TaskStep, ...] The whole plan after a proposed change.

Raises:

  • PlanValidationError — If a step id repeats, a dependency names a step that does not exist, or the dependency graph contains a cycle. All three are rejected against the resulting plan, so a batch that would create a cycle only in combination is still caught.

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