pi-compact-before-switch
Compact context with the outgoing model before switching to a smaller-window model.
Package details
Install pi-compact-before-switch from npm and Pi will load the resources declared by the package manifest.
$ pi install npm:pi-compact-before-switch- Package
pi-compact-before-switch- Version
0.1.0- Published
- Jun 15, 2026
- Downloads
- not available
- Author
- erans
- License
- MIT
- Types
- extension
- Size
- 35.5 KB
- Dependencies
- 0 dependencies · 1 peer
Security note
Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.
README
pi-compact-before-switch
A pi extension that prompts you to compact context using the outgoing model before switching to a smaller-window model via /model. Ctrl+P cycling and session restore pass through silently.
When you switch from an Opus-1M to a Haiku-200K with a long conversation, you'll see:
Compact before switching? Switching from claude-opus-4-5 (1,000,000 ctx) → claude-haiku-4-5 (200,000 ctx). Current context: 320,000 tokens — exceeds target window minus reserve. This will compact, then re-apply Haiku. Take a few seconds — no API key needed on Opus. (← confirm → cancel)
If you confirm, the extension reverts to Opus, runs pi's default compaction, then re-applies Haiku — all automatic. If you cancel, the switch is reverted and you stay on Opus.
Install
pi install npm:pi-compact-before-switch
That's it. The extension registers automatically on next session start.
Other install paths
pi install /path/to/pi-compact-before-switch # local path
pi install https://example.com/pi-compact-before-switch.tgz # URL
pi install -l . # project-local (commits .pi/npm/ path)
When does it trigger?
Conditions all met:
- Source is
/model(i.e.source === "set", not Ctrl+P cycling and not session restore) - There was a previous model
- Not re-selecting the same
(provider, id) - New model's
contextWindow< previous model'scontextWindow(any narrowing) currentTokens > targetContextWindow − 16,384(target fits, current doesn't)
If any condition is false, the switch is silent and immediate.
Behavior
| Scenario | Result |
|---|---|
/model from large → small, doesn't fit |
confirm → revert → compact on outgoing model → re-apply target |
/model from large → small, fits within reserve |
silent, switch happens immediately |
Widening (small → big) |
silent |
| Same model re-selected | silent |
Ctrl+P cycling |
silent |
| Session restore | silent |
| Old model has no API key (revert fails) | stay on target, error toast |
| Compact mid-flight fails | stay on outgoing model, error toast |
| Confirm canceled | revert to outgoing model, info toast |
| 30 s timer elapses | warning toast, guard cleared, next press re-prompts |
Minimal configuration
There is none. Run-time differences:
- Reserve tokens — 16,384 hardcoded. At or below threshold: switch is silent.
- Guard timeout — 30 s after a successful revert, the extension stops swallowing model_select events. If a compact hangs, the next press of
/modelworks again after that.
Why?
When you Ctrl+P through models you can land on one whose window is far below your current context. Many providers truncate silently. Compaction first avoids that.
Development
git clone https://github.com/erans/pi-compact-before-switch
cd pi-compact-before-switch
npm test
./scripts/smoke.sh # launches `pi -e extensions/compact-before-switch.ts`
Run tests:
./scripts/test.sh # or: npm test
31 tests cover the trigger filter (cycle/restore/no-previous/same-model/widening/overflow) and the orchestrator paths (cancel, revert failure, reentrancy guard, compact onError, guard recovery). Tests use Node 22+ built-in node:test with --experimental-strip-types. No transpile step.
Debug mode
Set PI_DEBUG_CBS=1 to make the extension emit a notification on every model_select event with the event shape and the trigger decision:
PI_DEBUG_CBS=1 pi
The toast will say:
CBS DEBUG | source=set | prev=anthropic/claude-opus-4-5 (1000000) | next=anthropic/claude-haiku-4-5 (200000) | tokens=12345 | threshold (next - 16384)=183616 | trigger=true
License
MIT © 2026 Eran Sandler. See LICENSE.
Source layout
pi-compact-before-switch/
├── extensions/
│ └── compact-before-switch.ts # the single extension file
├── tests/ # node:test, 31 tests
├── scripts/
│ ├── smoke.sh # launch pi -e ...
│ └── test.sh # npm test runner
├── verification/ # per-scenario outcomes
├── docs/ # spec + design + plan
├── package.json
├── README.md
└── LICENSE
When published, pi's convention auto-discovers extensions/*.ts and loads it as a single extension — no pi manifest needed.