Skip to content

API Reference parrot.memory.compaction.compact

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

parrot.memory.compaction.compact

View source

compact_history — the pure three-tier retention pre-pass (FEAT-525).

This is the heart of the feature: a pure, synchronous function that turns a :class:~parrot.memory.abstract.ConversationHistory plus a :class:~parrot.memory.compaction.models.ContextBudget into :class:~parrot.memory.compaction.models.TurnView objects (RAW or PRUNED, with the assistant_suffix already materialized) and the list of :class:~parrot.memory.compaction.models.Omission s the bot must flush before rendering. Nothing here touches a store, the network, or mutates its inputs.

Alias CompactionResult

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

Alias ContextBudget

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

Alias ConversationHistory

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

Alias ConversationTurn

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

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.

Alias PrunePolicy

Re-exported from parrot.memory.compaction.policies.PrunePolicy.

Alias TokenCounter

Re-exported from parrot.memory.compaction.tokens.TokenCounter.

Alias TurnState

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

Alias TurnView

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

Function compact_history

Walk a history newest → oldest and classify each turn RAW / PRUNED / DROPPED.

Pure and synchronous (spec G1/G9): never mutates history, its turns, or their invocations; performs no I/O; the same inputs always produce an equal :class:CompactionResult.

Three tiers (spec G10), walked newest → oldest with a running calibrated token sum:

  1. Verbatim (RAW) while n_raw < budget.min_verbatim_turns or the cumulative size stays within both budget.verbatim_tokens and the high-watermark budget.
  2. Pruned while the cumulative size stays within the high watermark (or the turn is the newest kept turn — the newest is never dropped).
  3. Dropped beyond that — and every older turn is dropped too (tiers are contiguous); sets stage2_needed.

A persisted boundary_turn_id forces every turn at or before it to render PRUNED regardless of budget (monotonic: turns that once rendered PRUNED never render RAW again for this history). Any invocation whose output exceeds budget.oversize_tool_tokens is pruned in every turn but the newest, even inside the verbatim tier.

compact_history(history: ConversationHistory, budget: ContextBudget, *, policies: Optional[Dict[str, PrunePolicy]] = None, boundary_turn_id: Optional[str] = None, counter: Optional[TokenCounter] = None, calibration: float = 1.0, current_chatbot_id: Optional[str] = None, include_other_agents: bool = True) -> CompactionResult

Parameter Type Default Description
history ConversationHistory The history to compact.
budget ContextBudget The retention configuration.
policies Optional[Dict[str, PrunePolicy]] None Per-tool prune-policy overrides, consulted before the module registry.
boundary_turn_id Optional[str] None The previously persisted boundary, if any.
counter Optional[TokenCounter] None The token counter to size turns and rendered blocks with. Defaults to :func:~parrot.memory.compaction.tokens.get_default_counter.
calibration float 1.0 Multiplier applied to every calibrated size (the memory's EWMA of provider/estimate ratio).
current_chatbot_id Optional[str] None The agent doing the asking, for the foreign-turn rule (mirrors :func:~parrot.memory.render.render_history).
include_other_agents bool True When False, foreign turns are excluded from consideration entirely (not even counted as dropped).

Returns: CompactionResult — class:CompactionResult with views ordered oldest → newest, ready for :func:~parrot.memory.render.render_history.

Alias count_turn

Re-exported from parrot.memory.compaction.tokens.count_turn.

Alias format_invocation_parts

Re-exported from parrot.memory.compaction.policies.format_invocation_parts.

Alias get_default_counter

Re-exported from parrot.memory.compaction.tokens.get_default_counter.

Alias get_policy

Re-exported from parrot.memory.compaction.policies.get_policy.

Alias needs_recount

Re-exported from parrot.memory.compaction.tokens.needs_recount.

Alias prune_turn_parts

Re-exported from parrot.memory.compaction.policies.prune_turn_parts.

Alias render_parts_text

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

Function render_raw_parts

Build the RAW-view parts of a turn's tool activity.

Every invocation renders in full (name, status, elapsed, canonical input, output excerpt, error) except those whose index is in oversize — those go through their :class:PrunePolicy instead (the oversize rule applied inside the verbatim tier).

render_raw_parts(turn: ConversationTurn, limit: Limit, *, oversize: Collection[int] = (), policies: Optional[Dict[str, PrunePolicy]] = None, counter: TokenCounter) -> Tuple[Tuple[MessagePart, ...], Tuple[Omission, ...]]

Parameter Type Default Description
turn ConversationTurn The turn to render.
limit Limit Bounds on the block (max_invocations, max_output_chars, max_block_tokens).
oversize Collection[int] () Indices (into turn.tool_invocations) to render through their prune policy.
policies Optional[Dict[str, PrunePolicy]] None Consulted before the module registry for oversize invocations.
counter TokenCounter Used only to decide whether the whole block must be collapsed.

Returns: Tuple[Tuple[MessagePart, ...], Tuple[Omission, ...]] — (parts, omissions); parts is () when the turn has no invocations.

Function render_raw_view

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

render_raw_view(turn: ConversationTurn, limit: Limit, *, oversize: Collection[int] = (), policies: Optional[Dict[str, PrunePolicy]] = None, counter: TokenCounter) -> Tuple[str, Tuple[Omission, ...]]

Parameter Type Default Description
turn ConversationTurn The turn to render.
limit Limit Bounds on the block.
oversize Collection[int] () Indices rendered through their prune policy instead of as a full excerpt.
policies Optional[Dict[str, PrunePolicy]] None Consulted before the module registry for oversize invocations.
counter TokenCounter Used only to decide whether the whole block must be collapsed.

Returns: Tuple[str, Tuple[Omission, ...]] — (assistant_suffix, omissions); assistant_suffix is "" when the turn has no invocations.

Function render_tool_activity

Render the RAW-view <tool-activity> suffix, with no oversize exceptions.

Convenience wrapper over :func:render_raw_view for the common case (no oversize indices). Uses an internal, offline :class:~parrot.memory.compaction.tokens.HeuristicCounter for its own max_block_tokens collapsing check — callers that need the calibrated counter for the walk itself should call :func:render_raw_view directly.

render_tool_activity(turn: ConversationTurn, limit: Limit) -> str

Parameter Type Default Description
turn ConversationTurn The turn to render.
limit Limit Bounds on the block.

Returns: str — The rendered suffix, or "" when the turn has no invocations.

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