pi-preemptive-compact

Preemptive context compaction extension for Pi

Packages

Package details

extension

Install pi-preemptive-compact from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:pi-preemptive-compact
Package
pi-preemptive-compact
Version
0.1.1
Published
Jul 22, 2026
Downloads
279/mo · 279/wk
Author
pesciolindivino
License
MIT
Types
extension
Size
5.1 KB
Dependencies
0 dependencies · 1 peer
Pi manifest JSON
{
  "extensions": [
    "./preemptive-compact.ts"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

Pi Preemptive Compact

An ultra-light Pi extension that compacts context earlier, at a fixed token threshold you control.

After a successful compaction, it sends a hidden continuation message so the agent can resume the current task from Pi's summary.

Why compact earlier?

More context isn't always more useful. Old tool output, stale decisions, and abandoned paths can make the current task harder to follow. Compacting earlier may give an agent a smaller working set and help it stay focused.

The trade-off depends on the model and the task: compaction can also remove details that still matter. Treat the threshold as a value to tune, not a universal optimum.

Install

From npm:

pi install npm:pi-preemptive-compact

Or directly from GitHub:

pi install git:github.com/SannaMarcoDev/pi-preemptive-compact

Restart Pi after installation.

Configuration

The threshold is a source-level setting. Change this exported constant in preemptive-compact.ts:

export const COMPACT_THRESHOLD_TOKENS = 150_000;

The default is 150,000 tokens. Lower values compact sooner; higher values keep more context.

Behavior

On each turn_end, the extension reads Pi's current context usage. At or above the configured threshold, it:

  1. starts one compaction;
  2. ignores further turn-end events while that compaction is running;
  3. sends a hidden message with triggerTurn: true after success, which resumes the task from the summary.

If usage data is unavailable, it does nothing. If compaction fails, it clears its lock so a later turn can try again.

Pi's native compaction

Pi's built-in compaction remains active when enabled. It uses the context window and reserveTokens to compact near the model limit; this extension adds an earlier fixed threshold.

Keep native compaction enabled as a fallback if the extension hasn't run or an early compaction attempt fails:

{
  "compaction": {
    "enabled": true,
    "reserveTokens": 16384,
    "keepRecentTokens": 20000
  }
}

Put these settings in ~/.pi/agent/settings.json and adjust them for your setup.

Test

npm test

The test covers the threshold trigger and the hidden continuation sent after a successful compaction.

License

MIT. See LICENSE.