pi-loop-compact
Compact Pi context between tool batches of a long run, skip compactions that cannot help, and resume the interrupted task
Package details
Install pi-loop-compact from npm and Pi will load the resources declared by the package manifest.
$ pi install npm:pi-loop-compact- Package
pi-loop-compact- Version
0.1.0- Published
- Oct 8, 2026
- Downloads
- 286/mo · 286/wk
- Author
- jiuai233
- License
- MIT
- Types
- extension
- Size
- 22.7 KB
- Dependencies
- 0 dependencies · 1 peer
Pi manifest JSON
{
"extensions": [
"./src/index.ts"
]
}Security note
Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.
README
pi-loop-compact
Compact Pi's context between tool batches of a long run, skip compactions that cannot help, and resume the task afterwards.
npm · Install · Commands · Configuration · 简体中文
- Compaction between tool batches: Pi's own threshold compaction waits for the run to end, so a run with dozens of tool calls can fill the context first. This extension checks usage after every complete tool batch and compacts at the threshold.
- Check before interrupting: Pi's compaction aborts the current request first. The extension predicts Pi's cut point and skips, with an explanation, when Pi has nothing to summarize or when the tail Pi must keep is already above the threshold on its own. The task keeps running.
- Resume afterwards: if the task was running, a continuation message follows the compaction, unless you already sent input during it.
Compaction itself stays with Pi: the cut point, summary format and compaction model (for example pi-compaction-model) are unchanged.
Install
Requires Node.js 24+ and Pi 1.1.0+.
pi install npm:pi-loop-compact
Run /reload in Pi. GitHub works too: pi install git:github.com/jiuai233/pi-loop-compact; use one source, not both. Automatic compaction is on by default at 85%. Disable other mid-run compaction extensions such as pi-midrun-compact to avoid double triggers.
Commands
| Command | Action |
|---|---|
/loop-compact, /loop-compact status |
Show switch, threshold, current usage and the latest skip or failure reason |
/loop-compact now |
Compact now; when there is nothing to compact, only explain why without interrupting |
Configuration
~/.pi/agent/extensions/pi-loop-compact.json; missing fields use the defaults:
{
"version": 1,
"enabled": true,
"threshold": 85,
"resume": true
}
| Field | Meaning |
|---|---|
enabled |
Compact automatically. /loop-compact now works either way |
threshold |
Context usage percentage that triggers compaction, between 0 and 100 |
resume |
Send a continuation message after a mid-run compaction |
The file is re-read on each new session and on /loop-compact status. An invalid file pauses automatic compaction and reports the error.
When compaction is skipped
Pi cuts only before user or assistant messages, keeps the most recent keepRecentTokens (20000 by default), and always keeps a tool batch together with the assistant message that issued it. Compaction cannot help when:
- everything since the last compaction belongs to that final block, for example a first message with hundreds of thousands of tokens; Pi reports "Nothing to compact";
- the final block (one huge message or tool batch) is above the threshold by itself, so usage stays above it after compacting.
The extension skips both, reports each cut point once, and lets the task continue. Once enough new content moves the cut point, it tries again. If the context really overflows, Pi's own overflow recovery still applies; a new session, or saving large inputs to disk for the model to read selectively, works better.
When compaction fails (for example, the compaction model request errors), the extension reports it, sends the continuation so the task proceeds uncompacted, and does not retry automatically at the same cut point.
Development
npm install
npm run check
The RPC tests start the local pi (override with PI_BINARY) with a mock model and never contact real services.
Inspired by pi-midrun-compact; the code is an independent implementation.