npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pi-compact-before-switch

v0.1.0

Published

Compact context with the outgoing model before switching to a smaller-window model.

Downloads

78

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:

  1. Source is /model (i.e. source === "set", not Ctrl+P cycling and not session restore)
  2. There was a previous model
  3. Not re-selecting the same (provider, id)
  4. New model's contextWindow < previous model's contextWindow (any narrowing)
  5. 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 /model works 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.