Pi dcp

From Dicklesworthstone/pi_agent_rust · View interactive version

Pi dcp is an AI agent skill from the GitHub repository Dicklesworthstone/pi_agent_rust. Dynamic Context Pruning extension for pi. Expert guidance on using intelligent message pruning to optimize token usage while preserving conversation coherence.

How to use this skill

Skill file

Pi-DCP: Dynamic Context Pruning Expert

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.

Core Concepts

What Pi-DCP Does

Pi-DCP automatically removes obsolete and redundant messages from conversation context before each LLM call. This:

Workflow: Prepare > Process > Filter

  1. Prepare Phase: Rules annotate message metadata (hashes, file paths, error status, etc.)
  2. Process Phase: Rules make pruning decisions based on metadata
  3. Filter Phase: Messages marked for pruning are removed

Built-in Rules

Deduplication

Superseded Writes

Error Purging

Recency Protection

Usage Guidance

Commands

Tell users about these commands:

When to Use Debug Mode

Recommend /dcp-debug when:

When to Adjust Recency Threshold

Recommend changing keepRecentCount:

When to Disable

Recommend /dcp-toggle to disable when:

Custom Rules

Creating Custom Rules

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';
    }
  },
};

Rule Design Patterns

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)

Metadata Fields

Standard metadata fields:

Custom rules can add any fields.

Troubleshooting

"LLM forgot recent context"

  1. Check recency threshold: /dcp-recent 15 to increase
  2. Enable debug: /dcp-debug to see what's being pruned
  3. Check if important messages are within recency window

"Still using too many tokens"

  1. Check stats: /dcp-stats to see pruning rate
  2. Consider adding custom rules for domain-specific pruning
  3. Reduce recency threshold if safe: /dcp-recent 5

"Extension not working"

  1. Check if enabled: /dcp-toggle twice (off then on)
  2. Look for initialization message in logs
  3. Check for configuration errors in console

"Rule not found error"

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);

Best Practices

Configuration

Rule Order

Rules are applied in the order configured. Standard order:

  1. Deduplication
  2. Superseded Writes
  3. Error Purging
  4. Recency (should be last to override)

Recency should typically be last since it protects messages.

Performance

Example Scenarios

Scenario: Long File Editing Session

User has edited src/app.ts 20 times. Without DCP, all 20 write operations are in context.

Pi-DCP behavior:

Scenario: Debugging with Retries

User encountered an error, retried 5 times, finally succeeded.

Pi-DCP behavior:

Scenario: Repetitive Tool Calls

User ran ls 10 times in the same directory.

Pi-DCP behavior:

Advanced Topics

State Tracking

Rules 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).

Multi-Rule Coordination

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) {
    // ...
  }
}

Conditional Pruning

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;
  }
}

Integration with Pi

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.

Future Enhancements

Potential features to suggest:

Summary

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.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.