Skip to content

API Reference parrot.memory.compaction.policies

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

parrot.memory.compaction.policies

View source

Per-tool prune policies for per-turn conversation compaction (FEAT-525).

A PRUNED view keeps the user message and assistant text intact and replaces each tool invocation's I/O with a one-line notice carrying enough to recover the bytes (id="om_…") or to re-run the tool. Errors are never omitted (spec C7).

All functions here are pure and synchronous; content ids are computed via :func:parrot.memory.compaction.omission.content_id, the same function the write-time offload (ConversationMemory.add_turn) uses, so ids match whether an output was offloaded at write time or pruned later at render time.

Alias ConversationTurn

Re-exported from parrot.memory.abstract.ConversationTurn.

Class DefaultPolicy

Fallback policy: the shared rule set, full in= arguments.

Method DefaultPolicy.prune

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation
turn_id str
limit Limit

Returns: PrunedInvocation

Class FileReadPolicy

Keeps the source path in in=; content is offloaded/omitted like the default.

Method FileReadPolicy.prune

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation
turn_id str
limit Limit

Returns: PrunedInvocation

Class FileWritePolicy

Keeps the target path in in=; content is offloaded/omitted like the default.

Method FileWritePolicy.prune

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation
turn_id str
limit Limit

Returns: PrunedInvocation

Alias Limit

Re-exported from parrot.memory.compaction.models.Limit.

Alias MessagePart

Re-exported from parrot.memory.parts.MessagePart.

Alias Omission

Re-exported from parrot.memory.compaction.models.Omission.

Attribute POLICY_VERSION

Type: str

Class PrunePolicy

A per-tool strategy for rendering one invocation's PRUNED-view line.

Attributes:

Attribute Type Description
name str

Method PrunePolicy.prune

Render inv for a PRUNED view.

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation The invocation to render.
turn_id str The owning turn's id (stamped on any new Omission).
limit Limit Bounds on the rendered line (only max_input_chars is used here).

Returns: PrunedInvocation — The rendered line plus any newly created omissions.

Class PrunedInvocation

One invocation's rendered PRUNED-view line, plus any new omissions.

parts are the tool_use + tool_result parts matching notice; a custom policy may leave it empty (the caller then wraps notice in one text part).

Parameter Type Default Description
notice str
omissions Tuple[Omission, ...]
parts Tuple[MessagePart, ...] ()

Fields:

Field Type Default Description
notice str
omissions Tuple[Omission, ...]
parts Tuple[MessagePart, ...] ()

Class QueryPolicy

Keeps the query/url in in=; adds rows=/hits= when derivable from the output.

Method QueryPolicy.prune

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation
turn_id str
limit Limit

Returns: PrunedInvocation

Class ShellPolicy

Keeps the command in in=; adds exit= when the output JSON carries one.

Method ShellPolicy.prune

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation
turn_id str
limit Limit

Returns: PrunedInvocation

Class SubAgentPolicy

Keeps only the first line of the delegated task/prompt in in=.

Method SubAgentPolicy.prune

prune(inv: ToolInvocation, *, turn_id: str, limit: Limit) -> PrunedInvocation

Parameter Type Default Description
inv ToolInvocation
turn_id str
limit Limit

Returns: PrunedInvocation

Alias ToolInvocation

Re-exported from parrot.memory.compaction.models.ToolInvocation.

Alias ToolStatus

Re-exported from parrot.memory.compaction.models.ToolStatus.

Alias content_id

Re-exported from parrot.memory.compaction.omission.content_id.

Function format_invocation_line

Build the shared "- {tool} {ok|error}[ {Ns}] in=... {body}" line.

Derived from :func:format_invocation_parts so a line and its parts cannot drift apart.

format_invocation_line(inv: ToolInvocation, *, limit: Limit, body: str) -> str

Parameter Type Default Description
inv ToolInvocation The invocation being rendered. Pass a copy with a narrowed input (e.g. via :func:dataclasses.replace) to show a restricted summary instead of the full arguments.
limit Limit limit.max_input_chars bounds the in= field.
body str Already-rendered notice / error fragment(s), appended after in=....

Returns: str — The formatted line.

Function format_invocation_parts

The tool_use header part and, when body is non-empty, the tool_result part of one line.

format_invocation_parts(inv: ToolInvocation, *, limit: Limit, body: str) -> Tuple[MessagePart, ...]

Parameter Type Default Description
inv ToolInvocation The invocation being rendered (its input is canonicalized and truncated for in=).
limit Limit limit.max_input_chars bounds the in= field.
body str Already-rendered notice / error fragment(s), joined with a single space.

Returns: Tuple[MessagePart, ...] — (tool_use,) or (tool_use, tool_result).

Function get_policy

Resolve the policy for a tool name.

Lookup order: the :func:register_policy registry (exact name), then the built-in alias table, then :class:DefaultPolicy.

get_policy(tool_name: str) -> PrunePolicy

Parameter Type Default Description
tool_name str The tool name to resolve.

Returns: PrunePolicy — The resolved policy.

Function omission_notice

Build the <tool-output-omitted .../> notice for one field.

omission_notice(inv: ToolInvocation, content_id_: str, chars: int) -> str

Parameter Type Default Description
inv ToolInvocation The invocation the omission belongs to (for tool_name and wm_key).
content_id_ str The id the content is (or will be) stored under.
chars int The original content's character length.

Returns: str — <tool-output-omitted tool="..." chars="..." id="..."[ wm="..."]/>; the wm= attribute appears only when inv.wm_key is set.

Function prune_turn

Render the PRUNED-view <tool-activity> suffix for a turn (derived from :func:prune_turn_parts).

prune_turn(turn: ConversationTurn, *, limit: Limit = Limit(), policies: Optional[Dict[str, PrunePolicy]] = None) -> Tuple[str, Tuple[Omission, ...]]

Parameter Type Default Description
turn ConversationTurn The turn to render.
limit Limit Limit() Bounds on each rendered line.
policies Optional[Dict[str, PrunePolicy]] None Consulted before the module registry for each tool name.

Returns: Tuple[str, Tuple[Omission, ...]] — (assistant_suffix, omissions); assistant_suffix is "" for a turn with no invocations.

Function prune_turn_parts

PRUNED-view parts of a turn (() for a turn with no invocations) plus new omissions.

Pure: does not read context_used and does not mutate turn. A custom policy that returns no parts contributes one text part holding its rendered line. When at least one <tool-output-omitted notice was emitted, a trailing recovery-hint text part is appended.

prune_turn_parts(turn: ConversationTurn, *, limit: Limit = Limit(), policies: Optional[Dict[str, PrunePolicy]] = None) -> Tuple[Tuple[MessagePart, ...], Tuple[Omission, ...]]

Parameter Type Default Description
turn ConversationTurn The turn to render.
limit Limit Limit() Bounds on each rendered line.
policies Optional[Dict[str, PrunePolicy]] None When given, consulted before the module registry / built-in alias table.

Returns: Tuple[Tuple[MessagePart, ...], Tuple[Omission, ...]] — (parts, omissions).

Function register_policy

Register (or override) the policy used for tool_name.

register_policy(tool_name: str, policy: PrunePolicy) -> None

Parameter Type Default Description
tool_name str The exact tool name to match.
policy PrunePolicy The policy to use for it.

Alias render_parts_text

Re-exported from parrot.memory.parts.render_parts_text.

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