Skip to content

API Reference parrot.tools.working_memory.task_memory.adapters

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

parrot.tools.working_memory.task_memory.adapters

View source

Typed result adapters for the invocation observer (FEAT-538).

The observer has to answer one question about every dispatch: what actually happened? This module answers it, and the whole design rests on a single prohibition from the specification:

**Never parse free text to infer success.**

A tool that returns the string "Error: the database is unreachable" returned a value. It succeeded. A tool that returns ToolResult(status="error") failed. The difference is the typed envelope, never the prose — and a classifier that reads prose will eventually mark a search result about outages as a failure, or miss a real failure whose message happens to read cheerfully.

What counts as a typed signal, in precedence order:

  1. A :class:~parrot.tools.abstract.ToolResult-shaped model — its success/status/error fields.
  2. A toolkit error dictionary: a mapping whose status key holds a recognised failure status. Business dictionaries are not envelopes: a mapping with no status, or with a status this module does not recognise as a failure, is an ordinary value and therefore a success.
  3. A plan execution manifest: failed nodes, or a partial/error artifact reference. A partial manifest is explicitly not valid completion evidence.
  4. A raised exception — a failure, except for cancellation.
  5. Anything else — an ordinary value, and therefore a success.

Dispatch outcomes that never ran a tool body

ToolManager.execute_tool returns early for an unknown tool (not_found), a guardrail/grant/resolver denial (forbidden) and an authorization requirement (authorization_required). Those are unsuccessful dispatch outcomes, not failed tools: they carry executed=False so nothing downstream can claim a tool body ran.

Both dispatch modes reach the same answer

Verified against the real dispatcher: in default mode the AbstractTool branch raises ValueError for a status == "error" envelope, while return_tool_result=True returns that envelope. This module classifies both to :data:CallOutcome.ERROR, so enabling full-result mode cannot change a step's recorded history.

Structural typing, deliberately

Envelopes and manifests are recognised by shape, not by isinstance against imported classes. Importing parrot.tools.abstract or parrot.bots.flows.plan.models here would couple the task-memory package to the tool and flow machinery for nothing — the fields are the contract. The test module verifies the duck-typing against the real ToolResult, ArtifactRef and ExecutionManifest classes, so the shapes cannot drift apart silently.

Attribute CANCELLED_STATUSES

Type: frozenset

Attribute DENIED_STATUSES

Type: frozenset

Class DispatchOutcome

What one physical dispatch attempt actually did.

Parameter Type Default Description
outcome CallOutcome
executed bool
error Optional[str] None
status Optional[str] None
partial bool False
source OutcomeSource OutcomeSource.ORDINARY_VALUE

Fields:

Field Type Default Description
outcome CallOutcome The typed outcome.
executed bool Whether a tool body actually ran. False only for the dispatcher's early returns.
error Optional[str] None Condensed error text taken from a typed field, never inferred from prose. Callers must still pass it through the redactor before it reaches a journal.
status Optional[str] None The envelope status observed, when there was one.
partial bool False Whether a plan manifest reported partial results. A partial manifest is not valid completion evidence.
source OutcomeSource OutcomeSource.ORDINARY_VALUE Which rule decided this outcome.

Attributes:

Attribute Type Description
succeeded bool Whether this attempt is a plain success.
is_resolved bool Whether this outcome settles the attempt's disposition.
tool_status ToolStatus The compaction status this outcome maps to. ToolStatus has only COMPLETED and ERROR and this module does not invent new members: the richer vocabulary (cancelled, unknown, denied, not-executed) lives on InvocationRecord and in journal events, while ToolInvocation stays the shared normalized payload it already is.

Attribute FAILURE_STATUSES

Type: frozenset

Attribute NOT_EXECUTED_STATUSES

Type: frozenset

Class OutcomeSource

Which rule decided a dispatch's outcome.

Recorded so an operator can tell why an attempt was classified the way it was — "the tool returned a typed envelope" and "the value was ordinary so we defaulted to success" are very different claims to make about a step's history.

Attribute UNRESOLVED_STATUSES

Type: frozenset

Function classify

Classify a dispatch from whichever of value/exception it produced.

classify(*, value: Any = None, exception: Optional[BaseException] = None) -> DispatchOutcome

Parameter Type Default Description
value Any None The returned value, when the dispatch returned.
exception Optional[BaseException] None The raised exception, when it raised.

Returns: DispatchOutcome — The dispatch outcome.

Function classify_exception

Classify an exception that propagated out of the dispatch.

Cancellation is neither a success nor a failure: the call was interrupted, so its disposition is unresolved until a later attempt or an explicit reconciliation records one.

classify_exception(exc: BaseException, *, executed: bool = True) -> DispatchOutcome

Parameter Type Default Description
exc BaseException The exception.
executed bool True Whether a tool body had already been entered. Pass False only when the dispatcher failed before execution.

Returns: DispatchOutcome — The dispatch outcome.

Function classify_result

Classify a value the dispatcher returned.

classify_result(value: Any) -> DispatchOutcome

Parameter Type Default Description
value Any Whatever execute_tool returned.

Returns: DispatchOutcome — The dispatch outcome. An ordinary value — including a string that reads like an error message — is a success, because it carries no typed failure signal.

Function looks_like_error_mapping

Whether value is a toolkit error dictionary.

A mapping is an error envelope only when its status is one this module recognises as a failure. A mapping with no status, or with an unrecognised one ({"status": "shipped"}), is ordinary business data.

looks_like_error_mapping(value: Any) -> bool

Parameter Type Default Description
value Any The candidate.

Returns: bool — True when it is a mapping carrying a recognised failure status.

Function looks_like_manifest

Whether value is a plan execution manifest.

looks_like_manifest(value: Any) -> bool

Parameter Type Default Description
value Any The candidate.

Returns: bool — True when it carries the manifest's aggregate fields.

Function looks_like_tool_result

Whether value is a ToolResult-shaped model.

Deliberately excludes mappings: a plain dict that happens to hold success/status/result keys is business data, and treating it as an envelope is exactly the "business dicts mistaken for envelopes" failure the acceptance criteria forbid.

looks_like_tool_result(value: Any) -> bool

Parameter Type Default Description
value Any The candidate.

Returns: bool — True when it carries the envelope's typed fields as attributes and is not a mapping.

Function to_tool_invocation

Build the canonical compaction payload for one attempt.

The observer produces this once and feeds both the journal derivation and ConversationTurn.tool_invocations, rather than letting the turn re-derive its own copy from AIMessage.tool_calls.

The result is not yet safe to persist: run it through :func:~parrot.tools.working_memory.task_memory.redaction.redact_invocation, which normalizes it with Stage 0 and then removes credentials.

to_tool_invocation(*, tool_name: str, arguments: Mapping[str, Any], outcome: DispatchOutcome, output: Optional[str] = None, elapsed_ms: Optional[int] = None, output_chars: Optional[int] = None, wm_key: Optional[str] = None) -> ToolInvocation

Parameter Type Default Description
tool_name str The dispatched tool.
arguments Mapping[str, Any] The call's arguments.
outcome DispatchOutcome The classified outcome.
output Optional[str] None Rendered output text, when there is one.
elapsed_ms Optional[int] None Wall-clock duration.
output_chars Optional[int] None Length of the original output, captured before any offload so a preview notice can report the true size.
wm_key Optional[str] None Working-memory tee key, when the result carried one.

Returns: ToolInvocation — A new :class:ToolInvocation.

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