Skip to content

mnesis.compaction.pruner

pruner

Tool output pruner — reclaims context space by tombstoning stale tool outputs.

ToolOutputPruner

ToolOutputPruner(
    store: ImmutableStore,
    estimator: TokenEstimator,
    config: MnesisConfig,
)

Reclaims context space by tombstoning stale tool outputs.

The protect window ensures the most recent tool outputs (within the last prune_protect_tokens worth of content) are never pruned. Only completed tool calls outside this window are candidates.

No LLM call is required — this is entirely deterministic.

Example::

pruner = ToolOutputPruner(store, estimator, config)
result = await pruner.prune("sess_01JXYZ...")
print(f"Pruned {result.pruned_count} tool outputs ({result.pruned_tokens:,} tokens)")

prune async

prune(session_id: str) -> PruneResult

Run a prune pass for the given session.

Algorithm: 1. Fetch all messages (including summaries) in chronological order. 2. Walk backward, tracking user turn count. 3. Skip the most recent 2 user turns (protect recent content). 4. Stop at any is_summary message (compaction boundary). 5. For each completed, non-protected, non-already-pruned tool part: - Accumulate output tokens. - Once outside the protect window (>40K tokens), add to candidates. 6. If total prunable volume < minimum threshold (20K): no-op. 7. Apply tombstones in a batch.

Parameters:

Name Type Description Default
session_id str

The session to prune.

required

Returns:

Type Description
PruneResult

PruneResult with counts of pruned parts and tokens.

ToolOutputPrunerAsync

ToolOutputPrunerAsync(
    store: ImmutableStore,
    estimator: TokenEstimator,
    config: MnesisConfig,
)

Bases: ToolOutputPruner

Async version that properly resolves part IDs.

prune async

prune(session_id: str) -> PruneResult

Run prune pass with async part ID resolution.