1
0
Fork
You've already forked pi-compaction-aware
0
No description
  • TypeScript 100%
2026年06月26日 10:47:00 -03:00
src fix: support Pi 0.80 compaction API 2026年06月26日 10:47:00 -03:00
config.example.json compaction-aware: fallback to session model for compaction summaries 2026年05月20日 18:41:45 -03:00
package-lock.json fix: support Pi 0.80 compaction API 2026年06月26日 10:47:00 -03:00
package.json fix: support Pi 0.80 compaction API 2026年06月26日 10:47:00 -03:00
README.md compaction-aware: fallback to session model for compaction summaries 2026年05月20日 18:41:45 -03:00
tsconfig.json compaction-aware: fallback to session model for compaction summaries 2026年05月20日 18:41:45 -03:00

compaction-aware

Pi Coding Agent extension for small compaction tweaks while reusing Pi's own compaction implementation as much as possible.

It changes only:

  • summary model: uses a configured model for compaction instead of the active session model;
  • trigger point: calls Pi's native ctx.compact() when context usage crosses triggerPercent of the active model context window;
  • recent retention: converts keepRecentPercent into Pi's native keepRecentTokens and then calls Pi's own prepareCompaction() / compact() from @earendil-works/pi-agent-core.

No custom serializer, custom split algorithm, or token inflation hack.

Install local

# from this repo
cd /home/su3h7am/Projects/pi-compaction-plus
mkdir -p ~/.pi/agent/extensions
ln -sfn /home/su3h7am/Projects/pi-compaction-plus ~/.pi/agent/extensions/compaction-aware

Or run one session with:

pi -e /home/su3h7am/Projects/pi-compaction-plus

Config

The extension reads only this file:

~/.pi/agent/compaction-aware.json

Create it:

mkdir -p ~/.pi/agent
cp /home/su3h7am/Projects/pi-compaction-plus/config.example.json ~/.pi/agent/compaction-aware.json

Example:

{
 "enabled": true,
 "summaryModel": "google/gemini-2.5-flash",
 "triggerPercent": 75,
 "keepRecentPercent": 15,
 "notify": true,
 "cooldownMs": 15000
}

Fields:

  • summaryModel: provider/model-id used only during compaction summary generation.
  • triggerPercent: after a turn, if ctx.getContextUsage().percent crosses this value, the extension calls ctx.compact().
  • keepRecentPercent: converted to keepRecentTokens = activeContextWindow * keepRecentPercent / 100 before calling Pi's native prepareCompaction().
  • notify: show Pi UI notifications.
  • cooldownMs: avoid repeated trigger calls.

Design

Hooks used:

  • turn_end: checks ctx.getContextUsage() and triggers ctx.compact() when crossing triggerPercent.
  • session_before_compact: resolves summaryModel, adjusts only keepRecentTokens, then delegates to Pi SDK:
const preparationResult = prepareCompaction(event.branchEntries, settings);
const compactionResult = await compact(preparation, summaryModel, apiKey, headers, event.customInstructions, event.signal);

Fallback behavior: if the configured model/auth is unavailable, the hook returns undefined, allowing Pi's default compaction path to run.