pi-fold

Lossless context folding for the Pi coding agent: only the oldest context changes; the fresh window stays untouched.

Packages

Package details

extension

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

$ pi install npm:pi-fold
Package
pi-fold
Version
4.3.0
Published
Oct 26, 2026
Downloads
1,946/mo · 326/wk
Author
shaneconner
License
MIT
Types
extension
Size
816.5 KB
Dependencies
0 dependencies · 2 peers
Pi manifest JSON
{
  "extensions": [
    "./extensions"
  ]
}

Security note

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

README

pi-fold

npm DOI

Long sessions without forgetting. pi-fold rotates stale context out of the window and into short briefs, while the exact originals stay on disk one tool call away. It runs automatically, keeps your prefix cache warm, and never throws anything away.

pi-fold folding a session in place: marks outline completed spans as the session works, and the marks commit together at a fold event

Install

pi install npm:pi-fold

Requires Node 22+ and Pi 0.83+ (Pi 1.0 needs Node 22.19+). MIT licensed.

For the best results, pair it with a long-term memory store such as pi-canon. Folding keeps the window lean; the store keeps what the session learned. Together they were the strongest setup in every benchmark.

Why fold instead of compact

Compaction replaces the transcript with a summary and discards the originals. Folding replaces old spans with briefs and keeps the originals retrievable, so nothing is ever lost.

On a 64-stage coding assignment over the curl codebase, followed by a 16-question recall test about the whole session:

Setup Recall Cost Wall clock
Compaction 4 / 16 $151 6.9h
Compaction + memory store 14 / 16 $202 9.0h
pi-fold + memory store 14 / 16 $101 5.2h

Same recall, half the cost, and hours faster. The savings come from the cache: folding batches its rewrites so the provider replays the cached prefix instead of rereading the session from scratch, and roughly 95% of input in the pi-fold run was served from cache.

Full write-ups, data and logs are on Zenodo (design, working memory, retrieval, rotation and retention), with friendlier versions on Medium and shaneconner.com.

What you get

  • Automatic folding. Completed work is marked as it finishes and folded when the window fills up. The agent doesn't have to do anything.
  • Lossless recall. Every brief links back to the original messages, verified by hash. The agent can peek at them or expand them back in place.
  • A cache-friendly cadence. Marks cost nothing. All of them land together in one commit, so the cache is spent once per commit instead of on every change.
  • A clean chat. Folded spans collapse into a single muted row in the chat, so your screen matches what the model actually sees.
  • The fold bar. A compact usage bar above Pi's footer shows how full the window is and what's in it, from deeply folded to still raw.

Commands

Command What it does
/fold-status Detailed token and fold figures
/fold Commit every pending mark now
/fold-editor Interactive map of the window, for reading inside folds
/fold-settings Edit options live; saved to ~/.config/pi-fold/settings.json

The agent gets one tool, pi_fold_context, with actions to status, peek, expand, refold, pin and unpin content, plus brief, reboundary and unmark for curating pending folds. It's all optional; a session that never calls the tool still folds.

Configuration

The defaults are the benchmarked setup, so there's nothing you need to tune.

Option Default What it controls
thresholds { maxTarget: 0.80, minTarget: 0.20, consolidateAfter: 10, minFoldChars: 8000 } When a commit fires (maxTarget), how far it folds down (minTarget), when folds consolidate, and the smallest span worth folding
providerInputBudget from Pi's context window The token budget folding works within
blacklistAutoFoldTools new Set() Tools whose results should never be auto-folded
toolFoldThreshold 0.50 Oldest share of the window where tool results are clipped in view (0 turns it off)
preCommitNotice false Tell the agent when a commit is approaching
noticeLeadShare 0.10 How early that notice arrives
palette { map: "batlow", start: 0, end: 1 } Fold bar colours: batlow, viridis, plasma, magma or cividis

How it works

Prefix caches are positional. A provider replays the longest byte-identical prefix of a prompt, so changing one early byte invalidates everything after it. pi-fold only rewrites the window at a commit; everything else is a pure append.

Marks, then commits. As each tool batch or chapter finishes, the runtime marks it as a fold candidate. A mark lives outside the window, so it changes nothing and costs nothing. When the window reaches maxTarget, a single commit folds the stalest marks into briefs until it's back down to minTarget, leaving recent work raw.

Peek and expand. peek appends a verified, plain-text view of a fold's originals (without thinking blocks or provider metadata) for one turn, then steps aside. expand restores the original messages in place until the fold is refolded.

Plays well with Pi. Pi's own compaction stays enabled underneath. Threshold compaction is skipped because folding gets there first, overflow is recovered by folding and retrying, and manual /compact still works as an escape hatch.

Prior work

pi-fold builds on lossless hierarchical compaction (LCM, arXiv:2605.04050) and agentic context management (Letta, arXiv:2510.11967); see also arXiv:2607.00692. Its contribution is the cache-aware commit: stage every fold as a free mark, then pay for the rewrite once.