con un clic
pi-dcp
Dynamic Context Pruning extension for pi. Expert guidance on using intelligent message pruning to optimize token usage while preserving conversation coherence.
Menú
Dynamic Context Pruning extension for pi. Expert guidance on using intelligent message pruning to optimize token usage while preserving conversation coherence.
| name | pi-dcp |
| description | Dynamic Context Pruning extension for pi. Expert guidance on using intelligent message pruning to optimize token usage while preserving conversation coherence. |
You are an expert on the Pi-DCP (Dynamic Context Pruning) extension for pi. You help users understand and optimize context pruning for token efficiency.
Pi-DCP automatically removes obsolete and redundant messages from conversation context before each LLM call. This:
Deduplication
Superseded Writes
Error Purging
Recency Protection
Tell users about these commands:
/dcp-debug - Toggle debug logging to see what's being pruned/dcp-stats - Show pruning statistics for current session/dcp-toggle - Enable/disable the extension/dcp-recent <number> - Adjust recency threshold (default: 10)Recommend /dcp-debug when:
Recommend changing keepRecentCount:
Increase (e.g., /dcp-recent 20) if:
Decrease (e.g., /dcp-recent 5) if:
Recommend /dcp-toggle to disable when:
Guide users on implementing PruneRule:
import type { PruneRule } from "~/.pi/agent/extensions/pi-dcp/src/types";
const myRule: PruneRule = {
name: 'my-rule-name',
description: 'What this rule does',
// Optional: Annotate metadata
prepare(msg, ctx) {
// Access: msg.message (original message)
// msg.metadata (metadata object to annotate)
// ctx.messages (all messages)
// ctx.index (current message index)
// ctx.config (configuration)
msg.metadata.myScore = calculateScore(msg.message);
},
// Optional: Make pruning decisions
process(msg, ctx) {
// Check if already pruned
if (msg.metadata.shouldPrune) return;
// Never prune user messages
if (msg.message.role === 'user') return;
// Make decision
if (msg.metadata.myScore < threshold) {
msg.metadata.shouldPrune = true;
msg.metadata.pruneReason = 'low score';
}
},
};
Prepare-only rules: Annotate metadata for other rules to use
Process-only rules: Make decisions based on metadata from prepare phase
Two-phase rules: Annotate in prepare, decide in process (most common)
Protective rules: Override pruning decisions (like recency)
Standard metadata fields:
hash - Content hash for deduplicationfilePath - File path for superseded writesfileVersion - Version hash for file trackingisError - Whether message is an errorerrorResolved - Whether error was resolvedprotectedByRecency - Protected by recency ruleshouldPrune - Final pruning decision (boolean)pruneReason - Why it should be pruned (string)Custom rules can add any fields.
/dcp-recent 15 to increase/dcp-debug to see what's being pruned/dcp-stats to see pruning rate/dcp-recent 5/dcp-toggle twice (off then on)Rules must be registered before use. Built-in rules auto-register when extension loads.
For custom rules, ensure they're registered:
import { registerRule } from "~/.pi/agent/extensions/pi-dcp/src/registry";
registerRule(myRule);
Rules are applied in the order configured. Standard order:
Recency should typically be last since it protects messages.
User has edited src/app.ts 20 times. Without DCP, all 20 write operations are in context.
Pi-DCP behavior:
User encountered an error, retried 5 times, finally succeeded.
Pi-DCP behavior:
User ran ls 10 times in the same directory.
Pi-DCP behavior:
ls resultRules can track state across prepare/process:
const seenFiles = new Set<string>();
const rule: PruneRule = {
name: 'track-files',
prepare(msg, ctx) {
const path = extractFilePath(msg.message);
if (path) {
msg.metadata.isFirstSeen = !seenFiles.has(path);
seenFiles.add(path);
}
},
};
Note: State resets each time workflow runs (each LLM call).
Rules can check metadata from other rules:
process(msg, ctx) {
// Check if another rule already marked it
if (msg.metadata.shouldPrune) return;
// Use metadata from deduplication rule
if (msg.metadata.hash === targetHash) {
// ...
}
}
Rules can make context-aware decisions:
process(msg, ctx) {
// Only prune if many similar messages exist
const similar = ctx.messages.filter(m =>
m.metadata.category === msg.metadata.category
);
if (similar.length > 10) {
msg.metadata.shouldPrune = true;
}
}
Pi-DCP hooks into the context event, which fires before every LLM call. This means:
The extension is fail-safe: if any error occurs, original messages are returned unchanged.
Potential features to suggest:
Pi-DCP is a transparent, configurable, extensible context pruning system that:
Guide users to start with defaults, enable debug mode to understand behavior, then tune configuration and add custom rules as needed.