Skip to content

API Reference parrot.tools.working_memory.task_memory.service

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

parrot.tools.working_memory.task_memory.service

View source

The task-memory command service (FEAT-538).

Everything above this layer speaks in commands — begin a task, revise a plan, record a decision, set a hint, pause or cancel. Everything below it speaks in events. This module is the translation, and it owns the three jobs that translation implies:

  1. Minting runtime identities. Step, constraint and decision ids are created here, never accepted from a caller. A model-authored id would let one task's command name another task's step.
  2. Resolving request-local labels. An initial plan cannot reference runtime step ids, because none exist yet. Labels are resolved to ids before anything is appended, so a malformed plan is rejected without mutating the task.
  3. Validating whole batches before appending. Dependencies, cycles, duplicate ids and illegal statuses are checked against the resulting plan. A rejected command leaves the journal and the projection exactly as they were.

Scope is runtime-owned. Every method takes a trusted :class:TaskScope as its first argument, and an explicit task_id is only ever a selector within that scope — never a way to reach across one. A task belonging to another scope reads as absent, not as forbidden, because an existence oracle is itself a leak.

Out of scope for this module, deliberately: LLM-facing tool schemas (TASK-2989) and completion validators (TASK-2980). update_step here records a transition; it does not decide whether the evidence justifies one.

Alias CompletionValidationError

Re-exported from parrot.tools.working_memory.task_memory.validators.CompletionValidationError.

Alias EvidenceMutated

Re-exported from parrot.tools.working_memory.task_memory.validators.EvidenceMutated.

Class TaskMemoryService

Command and read facade over an injected task store.

The service is stateless apart from its injected collaborators, so a toolkit may share one instance across turns. Concurrency safety comes from the store's per-task serialization plus the optimistic expected_revision check, not from a lock held here.

Parameter Type Default Description
store TaskMemoryStore The task store to command. Any implementation of the contract will do — the in-memory one and the PostgreSQL one are behaviourally identical by construction (AC2).
config Optional[TaskMemoryConfig] None Capacity and retention configuration.
artifacts Optional['ArtifactStore'] None Artifact store used to validate evidence-bound completions. Optional: without it, complete_step refuses rather than completing unvalidated. Refusing is the safe default — silently accepting a completion whose evidence was never checked is exactly the failure this feature exists to prevent.

Method TaskMemoryService.begin_task

Create a task, with its initial constraints and plan.

The whole initial plan is validated and its labels resolved before anything is appended, and the task_started and plan_updated events are committed together — so there is no window in which a task exists with a half-built plan.

begin_task(scope: TaskScope, *, goal: str, constraints: Sequence[str] = (), steps: Sequence[InitialStepSpec] = (), plan_complete: bool = False, turn_id: Optional[str] = None) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
goal str What the task is trying to achieve.
constraints Sequence[str] () Standing constraints.
steps Sequence[InitialStepSpec] () The initial plan, addressed by request-local labels.
plan_complete bool False Whether the plan is already complete.
turn_id Optional[str] None Conversation turn.

Returns: AppendResult — The append result, whose state carries the runtime ids the caller now needs.

Raises:

  • LimitExceeded — If the scope already holds the maximum number of open tasks.
  • PlanValidationError — If the initial plan is invalid — a duplicate or unknown label, or a dependency cycle.

Method TaskMemoryService.update_plan

Apply a batch of plan changes under an optimistic revision check.

The whole batch is validated against the resulting plan before a single event is written, so a batch that would produce a cycle only in combination is still rejected — and rejected without mutating anything.

update_plan(scope: TaskScope, task_id: str, *, expected_revision: int, changes: PlanChanges, reason: Optional[str] = None, turn_id: Optional[str] = None) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The task to revise.
expected_revision int The revision the caller believes is current.
changes PlanChanges The operations to apply, in order.
reason Optional[str] None Why the plan is changing.
turn_id Optional[str] None Conversation turn.

Returns: AppendResult — The append result.

Raises:

  • TaskNotFound — If the task is not in this scope.
  • RevisionConflict — If the revision is stale. Nothing changes.
  • PlanValidationError — If the batch is invalid. Nothing changes.

Method TaskMemoryService.update_step

Record a step transition.

This module records what was decided; it does not decide whether the evidence justifies a completion. That judgement is the completion validators' (TASK-2980), which supply completion_source and validator_results here.

update_step(scope: TaskScope, task_id: str, step_id: str, *, expected_revision: int, status: StepStatus, evidence_refs: Sequence[EvidenceRef] = (), note: Optional[str] = None, reason: Optional[str] = None, completion_source: Optional[CompletionSource] = None, validator_results: Sequence[Tuple[str, bool]] = (), turn_id: Optional[str] = None) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The owning task.
step_id str The step to transition.
expected_revision int The revision the caller believes is current.
status StepStatus The status to move to.
evidence_refs Sequence[EvidenceRef] () Exact artifact versions to bind.
note Optional[str] None Completion or transition note.
reason Optional[str] None Why it blocked or failed.
completion_source Optional[CompletionSource] None How it was completed, when it was.
validator_results Sequence[Tuple[str, bool]] () Recorded validator outcomes, consumed by replay rather than re-run.
turn_id Optional[str] None Conversation turn.

Returns: AppendResult — The append result.

Raises:

  • TaskNotFound — If the task is not in this scope.
  • RevisionConflict — If the revision is stale.
  • PlanValidationError — If the step is unknown, the status is not a legal target, or a bare alias was passed as evidence.

Method TaskMemoryService.update_task

Move a task through its lifecycle.

Pause, resume, block, fail, cancel and complete all go through the same event rules — completion in particular is still gated by the reducer, which refuses it while the plan is incomplete or a required step is unfinished.

update_task(scope: TaskScope, task_id: str, *, expected_revision: int, status: TaskStatus, reason: Optional[str] = None, turn_id: Optional[str] = None, actor: Actor = Actor.AGENT) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The task to transition.
expected_revision int The revision the caller believes is current.
status TaskStatus The status to move to.
reason Optional[str] None Why.
turn_id Optional[str] None Conversation turn.
actor Actor Actor.AGENT Who is acting — the sweeper uses this for retention transitions.

Returns: AppendResult — The append result.

Raises:

  • TaskNotFound — If the task is not in this scope.
  • RevisionConflict — If the revision is stale.
  • PlanValidationError — If the task is already terminal.

Method TaskMemoryService.record_decision

Record a decision against a task.

record_decision(scope: TaskScope, task_id: str, *, text: str, reason: str = '', affected_step_ids: Sequence[str] = (), expected_revision: Optional[int] = None, turn_id: Optional[str] = None) -> Tuple[AppendResult, str]

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The owning task.
text str What was decided.
reason str '' Why.
affected_step_ids Sequence[str] () Steps the decision bears on.
expected_revision Optional[int] None Optional optimistic check. A decision does not conflict with concurrent work, so this is optional where a plan change's is not.
turn_id Optional[str] None Conversation turn.

Returns: Tuple[AppendResult, str] — A (result, decision_id) pair.

Raises:

  • TaskNotFound — If the task is not in this scope.
  • RevisionConflict — If a supplied revision is stale.
  • PlanValidationError — If an affected step is unknown.

Method TaskMemoryService.set_resume_hint

Record what to do next.

The hint's stale flag is derived by the reducer from later events; it is never authored here.

set_resume_hint(scope: TaskScope, task_id: str, *, next_action: str, step_id: Optional[str] = None, turn_id: Optional[str] = None) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The owning task.
next_action str The next action.
step_id Optional[str] None The step it refers to.
turn_id Optional[str] None Conversation turn.

Returns: AppendResult — The append result.

Raises:

  • TaskNotFound — If the task is not in this scope.
  • PlanValidationError — If step_id is unknown.

Method TaskMemoryService.complete_step

Complete a step, but only if its evidence supports it.

This is the evidence-bound path. Validation runs outside the store's lock — validators may do real work, and holding a lock across them would serialize the whole task on the slowest check. The result is then committed against the same task revision and the same evidence fingerprints it validated; if either moved, the attempt is discarded and retried rather than committing a stale completion.

complete_step(scope: TaskScope, task_id: str, step_id: str, *, expected_revision: int, evidence_refs: Sequence[EvidenceRef] = (), note: Optional[str] = None, turn_id: Optional[str] = None, max_attempts: int = 2) -> AppendResult

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
task_id str The owning task.
step_id str The step to complete.
expected_revision int The revision the caller believes is current.
evidence_refs Sequence[EvidenceRef] () Exact artifact versions offered as evidence.
note Optional[str] None The completion note.
turn_id Optional[str] None Conversation turn.
max_attempts int 2 How many times to revalidate when the task moves underneath the validation.

Returns: AppendResult — The append result.

Raises:

  • CompletionValidationError — If the evidence does not support the completion. The step is left exactly as it was.
  • EvidenceMutated — If content changed behind a bound version. This is not an ordinary refusal — anything already completed against that version must be reopened.
  • UnknownValidatorError — If the policy names an unregistered validator.
  • RevisionConflict — If the task kept moving across every attempt.

Method TaskMemoryService.get_task

Load one task's projection.

get_task(scope: TaskScope, task_id: str) -> TaskSnapshot

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

Returns: TaskSnapshot — The snapshot.

Raises:

  • TaskNotFound — If the task is not in this scope.

Method TaskMemoryService.list_open_tasks

List the scope's open tasks.

list_open_tasks(scope: TaskScope, *, limit: int = 20) -> TaskPage

Parameter Type Default Description
scope TaskScope Trusted runtime scope.
limit int 20 Bounded page size.

Returns: TaskPage — A bounded page of summaries.

Method TaskMemoryService.compact_state

Return the small state summary a command result carries.

Deliberately tiny: a command's acknowledgement should let the caller issue its next command, not re-deliver the whole task.

compact_state(scope: TaskScope, task_id: str) -> Dict[str, Any]

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

Returns: Dict[str, Any] — A JSON-safe summary.

Raises:

  • TaskNotFound — If the task is not in this scope.

Class TaskNotFound

No such task exists in the caller's scope.

Deliberately indistinguishable from "exists but belongs to someone else": distinguishing them would turn this error into an existence oracle for other users' task ids.

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