pi-fold 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -211
- package/extensions/active-context.ts +483 -244
- package/extensions/evidence.js +1 -5
- package/extensions/index.js +4 -3
- package/extensions/json.ts +23 -1
- package/extensions/lib/canonical.ts +34 -0
- package/extensions/lib/curation.ts +46 -9
- package/extensions/lib/folding.ts +181 -17
- package/extensions/lib/instrumentation.ts +0 -6
- package/extensions/lib/measurement.ts +37 -4
- package/extensions/lib/persistence.ts +212 -9
- package/extensions/lib/policy.ts +103 -29
- package/extensions/lib/scheduling.ts +105 -18
- package/extensions/lib/selection.ts +112 -39
- package/extensions/lib/tool-surface.ts +37 -6
- package/extensions/lib/transcript.ts +65 -14
- package/extensions/settings.ts +11 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
# pi-fold
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/pi-fold) [](https://doi.org/10.5281/zenodo.22142456)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
The full write-up, with interactive versions of every mechanism on this page and the complete measurement story, is at [shaneconner.com/projects/pi-fold](https://shaneconner.com/projects/pi-fold/).
|
|
5
|
+
**Rotate stale text out of the context window without losing it, and spend the prefix cache once per commit instead of continuously.** The oldest part of a session collapses into short briefs while the exact originals stay on disk, one tool call away. Folding is automatic and needs no agent participation. The shipped shape is deterministic briefs paired with a durable memory store, which is the cheapest configuration measured that kept recall intact.
|
|
8
6
|
|
|
9
7
|

|
|
10
8
|
|
|
11
|
-
*
|
|
9
|
+
*Marks outline spans as they complete and move nothing; the next commit applies every standing mark in one rewrite. Expanding a brief brings the exact messages back byte for byte. Illustrative animation drawn on the 2.x surface, where the agent laid the marks; since 3.0 the runtime lays them.*
|
|
12
10
|
|
|
13
11
|
## Install
|
|
14
12
|
|
|
@@ -16,256 +14,125 @@ The full write-up, with interactive versions of every mechanism on this page and
|
|
|
16
14
|
pi install npm:pi-fold
|
|
17
15
|
```
|
|
18
16
|
|
|
19
|
-
Node 22 or later, Pi 0.83 or later.
|
|
20
|
-
|
|
21
|
-
pi-fold serves the working tier of a four-tier memory stack. [pi-canon](https://github.com/shaneconner/pi-canon) is a separate, optional package serving the long-term tiers, the journal episodic and the canon semantic; the two compose but neither requires the other.
|
|
22
|
-
|
|
23
|
-
## What it is
|
|
24
|
-
|
|
25
|
-
pi-fold registers one tool, `pi_fold_context`, and gives the session's own agent nine actions over its transcript: `status`, `peek`, `brief`, `expand`, `refold`, `pin`, `unpin`, `reboundary`, and `unmark`. It also registers `/fold`, `/fold-editor`, `/fold-status` and `/fold-settings` for the human.
|
|
26
|
-
|
|
27
|
-
A fold takes a contiguous span of session entries and replaces it, in the window only, with a short brief. The entries themselves go to a fold store, byte for byte, addressed by their SHA-256 hash. The brief carries a handle, the handle resolves to the original, and expansion restores the exact bytes after verifying the hash. A fold is therefore a claim the runtime can check, not a claim the reader has to trust.
|
|
28
|
-
|
|
29
|
-
Everything else follows from one invariant, and it is the reason several otherwise reasonable features are not here.
|
|
30
|
-
|
|
31
|
-
**A provider prefix cache is positional.** It replays the longest byte-identical prefix of the prompt, so one changed byte at offset K discards everything cached after K. The cost of a mutation has nothing to do with its size: deleting a single character in the middle of a large window and rewriting a hundred thousand tokens near the end of it bill the same way, because both invalidate the same suffix.
|
|
32
|
-
|
|
33
|
-
**So the runtime may move a byte in the window only at a moment it is already rewriting the window**, which is a commit or a fold. Everything else is a pure append at the tail, and an appended byte is never later altered, shortened, removed, or repositioned. Stated plainly: never mutate the window, and never show something and then take it back. The second half is the one that costs features. Anything that displays state and then updates it in place is out, however useful the display was, because updating in place is a mid-prefix write with better manners.
|
|
34
|
-
|
|
35
|
-
Pinning is the other half of governing a window. `pin` holds entries raw through every fold, automatic or marked, so it is how an agent keeps a span expanded on purpose. It is tracked as a claim beside the marks and reported with them, and it frees nothing: the commit still takes its drop from whatever is left, so pinned mass makes the rest of the window fold sooner. A pinned share cap stops that from becoming a window nothing can reclaim.
|
|
36
|
-
|
|
37
|
-
**Marks are free, because a mark moves nothing.** The runtime stages the marks itself: the moment a tool batch or a chapter closes, the frontier cuts it into a pending fold mark, event by event, up to eight cuts per pass, stalest first. A mark lives in durable session state, outside the window; the projection stays byte-identical and the cached prefix survives. What the agent owns is the words. Once enough cut folds are waiting, one bounded notice names them, and `brief` writes the agent's own sentence onto a pending fold for free, because the brief reaches the window only when the commit writes that placeholder for the first time. A supplied brief is kept verbatim; a fold left unbriefed commits with a deterministic brief the runtime writes from the span itself, and the provenance records which happened.
|
|
38
|
-
|
|
39
|
-
Curation is correctable until the commit. `reboundary` with ids re-cuts a span into exactly one fold, merging adjacent cuts when the span covers several and splitting one when the span sits inside it; with a single id it returns that fold's span to raw. `unmark` withdraws a pending mark entirely, and `brief` may be rewritten on a pending fold any number of times at no cost. A standing fold's brief is never rewritten, because rewriting it rewrites the projection at that fold's placeholder and costs the whole prefix cache from there on.
|
|
40
|
-
|
|
41
|
-
**A commit is the one moment bytes move, and every accumulated decision lands at once.** It goes through the preparation and commit machinery every fold has always used, so a committed mark produces the ordinary fold record. Committing is the runtime's call, not a verb the agent can spend: measured 2026-08-07, an agent given the verb called it twice and the runtime correctly held it both times, which is surface without function.
|
|
42
|
-
|
|
43
|
-
**Folds are lossless, and there are two ways back in.** Folds form a DAG whose leaves are the exact session entries, and every fold record keeps ordered references down to those leaves. `peek` is the cheap read: it returns one fold's exact SHA-256-verified source as a tool result, at any depth, with the ancestors still collapsed, without touching the projection. It takes `offset` and `bytes` for a bounded slice, and any child fold id is peekable, so a large fold has a narrow read. A peek is ephemeral by default: the bytes ride the window until the model's next message, then their place holds a one-line placeholder and the reply that used them is the surviving trace, so an agent extracts what it needs into that reply. Peeking again is lossless and costs one append, and `ephemeral: false` keeps a result standing when the bytes are needed across several turns. The default follows what agents actually chose: on the sealed run measured for this, six of seven peeks asked for ephemeral at first exposure, and the one durable read was the probe that needed the bytes to persist. `expand` is the commitment, restoring that source in place until the fold is refolded, outside-in and one level at a time. Looking is not the same as taking, and most of the time an agent needs to check one detail rather than reopen an hour of work.
|
|
44
|
-
|
|
45
|
-
When the agent never annotates, the same folds still happen with deterministic briefs: the cuts are the runtime's either way, and only the words on them change. Pinned and blacklisted material never folds, and the fresh end of the window is protected structurally, because an incomplete unit is never proposable and the commit cuts stalest first and stops at its aim. A session that never calls the tool degrades gracefully into lossless hierarchical compaction.
|
|
46
|
-
|
|
47
|
-
## Folding against compaction
|
|
48
|
-
|
|
49
|
-
Compaction does not continue a session, it starts a new one. Pi's native compaction summarizes the transcript, discards the originals, and begins again from the summary. In the run measured below that is literal rather than rhetorical: native compacted three times, and the request immediately after each one carried a prompt of exactly zero tokens, 369.0k to 0k, then 364.6k to 0k, then 363.0k to 0k. A continuation does not restart at zero. The handoff is also written before the end, so the freshest material is exactly what is missing from it, and what a summarizer drops as bookkeeping is what becomes unrecoverable.
|
|
50
|
-
|
|
51
|
-
Folding keeps the turn. Occupancy crosses a threshold, spans fold in place, and the session continues holding a verified copy of everything it folded. The window stays inside a band instead of sweeping the whole allotment: the same measured run put native between 0k and 369.0k across 117 requests, with 12 of them served above 300k, while the folding arm ran between 1.1k and 236.9k across 82 requests and never entered that zone. Roughly the same mean window, 140.9k against 138.8k; what differs is the range. That matters because behavior is not constant across it. You are not running one system at two sizes; you are running a different system at 40k than at 400k.
|
|
52
|
-
|
|
53
|
-
## Measured results
|
|
54
|
-
|
|
55
|
-
The first campaign behind these tables contains two experiments. The first, here, is pi-fold against Pi's native compaction. The second, further down under [epoch fold scheduling](#epoch-fold-scheduling), is pi-fold against itself with fold scheduling as the only variable. A later transcript-only hidden-mass campaign is reported separately below, with its own protocol and claim limits.
|
|
56
|
-
|
|
57
|
-
One 64-stage staged assignment over the curl C repository: one Pi session per run, one user message, and the agent calls a `repo_stage` tool 64 times inside a single agentic turn. Model gpt-5.6-sol at xhigh effort, provider openai-codex. The 272,000-token figure in these tables is the transport's per-request input descriptor, which is what pi-fold budgeted against; the Codex catalog actually serves gpt-5.6 at a 372,000-token cap, which is where the native arm lived, peaking at 369.0k before each compaction. One arm runs pi-fold, the other runs Pi's native compaction.
|
|
58
|
-
|
|
59
|
-
There is one run per arm, and native did not finish: it ended its turn at stage 56 of 64. That leaves two pairings, and both are reported here rather than the flattering one.
|
|
17
|
+
Node 22 or later, Pi 0.83 or later. MIT licensed.
|
|
60
18
|
|
|
61
|
-
|
|
19
|
+
Folding does not require a memory store, but every best result measured came from pairing it with one. The campaigns used [pi-canon](https://github.com/shaneconner/pi-canon), installed separately. The two began as a single working setup and were split into separate extensions for modularity, and the experiments are what established that they work better as complements than either does alone. Only that pairing has been measured. Any durable long-term memory store is expected to compose the same way, though that is a suspicion rather than a result.
|
|
62
20
|
|
|
63
|
-
|
|
21
|
+
## What it does
|
|
64
22
|
|
|
65
|
-
|
|
23
|
+
Compaction replaces the transcript with a summary and discards the originals. Folding replaces stale spans with briefs and keeps the originals retrievable.
|
|
66
24
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
| Cache read | 8,309,760 | 14,602,240 | 0.57x |
|
|
72
|
-
| Output | 38,951 | 86,390 | 0.45x |
|
|
73
|
-
| Billed calls | 69 | 114 | 0.61x |
|
|
74
|
-
| Cost, Pi's own ledger | $11.57 | $21.00 | 0.55x, a 44.9% saving |
|
|
75
|
-
| Pooled cache share | 0.869 | 0.917 | inverted, see below |
|
|
76
|
-
| Peak request context | 236,861 | 369,024 | threshold 272,000 |
|
|
77
|
-
| Long-context surcharge | $0.00 | $4.50 on 17 requests | 21.4% of native's bill |
|
|
78
|
-
| Compactions | 0 | 3 | |
|
|
79
|
-
| Agent file reads | 27, 0.06 MB | 117, 0.93 MB | 4.3x the reads, 15x the bytes |
|
|
80
|
-
| Wall clock | 25.2 min | 42.7 min | 0.59x |
|
|
25
|
+
<picture>
|
|
26
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/shaneconner/pi-fold/main/media/fold-vs-compaction-dark.svg">
|
|
27
|
+
<img alt="Compaction turns the transcript into a summary and discards the originals, so the session starts over. pi-fold turns it into a brief and keeps the exact originals on disk, one peek or expand away, so the session keeps going." src="https://raw.githubusercontent.com/shaneconner/pi-fold/main/media/fold-vs-compaction-light.svg">
|
|
28
|
+
</picture>
|
|
81
29
|
|
|
82
|
-
|
|
30
|
+
A fold takes a contiguous span of session entries and replaces it, in the window only, with a short brief. The entries go to a fold store byte for byte, addressed by their SHA-256 hash. The brief carries a handle, the handle resolves to the original, and expansion restores the exact bytes after verifying the hash.
|
|
83
31
|
|
|
84
|
-
|
|
32
|
+
The package registers one tool, `pi_fold_context`, with nine actions over the transcript: `status`, `peek`, `brief`, `expand`, `refold`, `pin`, `unpin`, `reboundary` and `unmark`. A session that never calls the tool still folds; it degrades into lossless hierarchical compaction.
|
|
85
33
|
|
|
86
|
-
|
|
87
|
-
| --- | ---: | ---: | :--- |
|
|
88
|
-
| Total tokens | 11,435,313 | 16,970,754 | 0.67x |
|
|
89
|
-
| Fresh input | 1,489,599 | 1,407,978 | 1.06x |
|
|
90
|
-
| Cache read | 9,895,936 | 15,510,016 | 0.64x |
|
|
91
|
-
| Output | 49,778 | 52,760 | 0.94x |
|
|
92
|
-
| Billed calls | 82 | 108 | 0.76x |
|
|
93
|
-
| Cost, Pi's own ledger | $13.89 | $21.18 | 0.66x, a 34.4% saving |
|
|
94
|
-
| Pooled cache share | 0.869 | 0.917 | inverted, see below |
|
|
95
|
-
| Peak request context | 236,861 | 368,639 | threshold 272,000 |
|
|
96
|
-
| Long-context surcharge | $0.00 | $4.81 on 20 requests | |
|
|
97
|
-
| Fold events / compactions | 6 fold events, 0 compactions | 3 compactions | |
|
|
98
|
-
| Agent file reads | 39, 3.84 MB | 108, 4.62 MB | 2.8x the reads |
|
|
99
|
-
| Wall clock | 34.2 min | 32.4 min | 1.06x, pi-fold slower |
|
|
34
|
+
## Results
|
|
100
35
|
|
|
101
|
-
|
|
36
|
+
One 64-stage assignment over the curl C repository, run as a single agentic turn. Same model, same 251,520-token serving budget, every run. After all 64 stages landed, one withheld message asked sixteen questions about material from across the whole session.
|
|
102
37
|
|
|
103
|
-
|
|
38
|
+
| Condition | Correct of 16 | Wrong | Cost | Fresh input | Cached input | Wall clock |
|
|
39
|
+
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
|
|
40
|
+
| Compaction | 4 | 3 | $151.24 | 7.64M | 160.2M | 6.9h |
|
|
41
|
+
| Compaction + memory store | 14 | 0 | $201.70 | 9.65M | 231.7M | 9.0h |
|
|
42
|
+
| pi-fold, deterministic briefs | 11.3 avg | 3.5 avg | $93.65 | 5.74M | 91.2M | 4.6h |
|
|
43
|
+
| **pi-fold + memory store** | **14** | **0** | **$100.85** | **5.57M** | **102.2M** | **5.2h** |
|
|
44
|
+
| pi-fold, agent-written briefs | 3 | 9 | $111.96 | 7.12M | 108.8M | 5.4h |
|
|
104
45
|
|
|
105
|
-
|
|
106
|
-
- **No wall-clock claim on the complete pairing.** pi-fold was 5.6% slower there, 34.2 minutes against 32.4. The wall-clock win belongs to the stage-56 pairing only.
|
|
107
|
-
- **The cache share runs backwards, and that is the useful finding.** Native holds the higher pooled share, 0.917 against 0.869, while spending 67% more tokens and billing 82% more dollars, because cache share measures how cheap each prompt was rather than how much prompt was needed.
|
|
108
|
-
- **Part of the cost gap is a pricing tier, not token volume.** At flat base rates native's stage-56 bill would be $16.51, a 29.9% saving rather than 44.9%, and its complete-run bill $16.38, a 15.2% saving. The tier is real money a native run really pays, but quote the flat-rate figures when the argument is about tokens rather than price schedules.
|
|
109
|
-
- **One run per arm.** Native never finished; the supervisor lost the worker at the stage-57 request, so the stage-56 cut excludes native's final 6 messages and $0.57 of ledger cost. The complete pairing ran on different days. The stage-56 pairing is same-day and is the stronger claim.
|
|
110
|
-
- **The agent never called the tool.** All 62 folds in that run were the automatic ladder's; every commit records zero agent marks. So these are the fallback's numbers, produced by the part of the design that is closest to prior work, with agent curation, the distinguishing claim, unexercised.
|
|
111
|
-
- **The reread story is a count, not a duplicate-payload metric.** It rests on file read count and bytes from disk, both in the tables above.
|
|
46
|
+
*Fresh input is what the provider had to read for the first time; cached input is what it replayed. The deterministic-briefs row averages four runs that scored 14, 14, 9 and 8.*
|
|
112
47
|
|
|
113
|
-
|
|
48
|
+
Compare the two store-paired rows. Both scored 14 correct with none wrong, and folding cost half. That gap is request count rather than price: compaction needed roughly twice the provider requests for the same work, plus summarizer calls that never show up in its own ledger.
|
|
114
49
|
|
|
115
|
-
|
|
50
|
+
Compaction alone abstained nine times and was wrong three, so its dominant failure was declining rather than inventing, but it did invent. And the fold's deterministic runs are bimodal rather than average: two answered 14 with nothing wrong, two lost old material, and cost did not predict which. The store was tried once on each arm and both came back with nothing wrong, which is suggestive, not a rate.
|
|
116
51
|
|
|
117
|
-
|
|
52
|
+
Agent-written briefs scored worst of anything measured, which is why that invitation ships off.
|
|
118
53
|
|
|
119
|
-
|
|
120
|
-
| --- | ---: | ---: |
|
|
121
|
-
| Completed 64 stages | 2 of 2 | 0 of 2 |
|
|
122
|
-
| Join endpoints recorded correctly | 8 of 8 | 0 of 8; all eight recorded `unknown` |
|
|
123
|
-
| Ordinary probes | 40 of 42 | not available from two incomplete runs |
|
|
124
|
-
| Withheld end-block cells | 60 of 60 | not reached |
|
|
54
|
+
Four papers with figure sources, redacted per-request ledgers and campaign logs are on Zenodo: [design and trace evaluation](https://doi.org/10.5281/zenodo.21856873), [working memory under context shedding](https://doi.org/10.5281/zenodo.21980746), [ephemeral retrieval](https://doi.org/10.5281/zenodo.22142454), and [rotation and retention](https://doi.org/10.5281/zenodo.22142456), which is the campaign above. Narrative versions on [Medium](https://medium.com/@shane.conner/a-compaction-summary-is-one-record-doing-a-stores-job-3f46212ef059), interactive versions at [shaneconner.com](https://shaneconner.com/projects/pi-fold/), and the full chronology in [the experiment log](https://github.com/shaneconner/pi-fold/blob/main/docs/fold_vs_compaction/experiment-log.md).
|
|
125
55
|
|
|
126
|
-
|
|
56
|
+
## Limits
|
|
127
57
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
58
|
+
- **Native compaction is strong and is the right default for most sessions.** It needs no installation and no tool surface. This is an alternative for workflows where the numbers above matter, not a replacement.
|
|
59
|
+
- **The benchmark is narrow by construction.** It was built to reproduce specific things compaction handles badly, one to four runs per condition, and it says nothing about production behavior.
|
|
60
|
+
- **Folding is not a memory store.** It rotates transcript source out of the window; durable placement of what a session learned is a separate job, and the pairing is where the measured value sat.
|
|
131
61
|
|
|
132
62
|
## How it works
|
|
133
63
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
The cutting is the runtime's. As units complete, the frontier stages each one as a pending fold mark, at the level of events rather than turns: a completed tool batch is proposable the moment its results land, and an incomplete one never is. The agent's actions ride on top: it briefs the cut folds in its own words, expands any fold whose detail is needed again, dissolves a mis-cut boundary with `reboundary`, withdraws a mark with `unmark`, and pins entries that must stay raw. Curation the agent cannot fix afterwards is curation it will not risk making, which is why `reboundary` is part of the ordinary surface rather than an extra.
|
|
137
|
-
|
|
138
|
-
Every fold obeys the same structural rules. Folds nest rather than partially overlap. Chapter folds align to closed user, assistant and tool-batch units, consolidations take two or more adjacent folds, and pinned or blacklisted evidence never folds. The frontier keeps its own cuts bite-sized, because nobody vouched for a bigger boundary, and a fold that hides more than it says is a fold nobody reads back. A span someone names, through `reboundary` or the editor, is taken whole at any size: a person or the agent cut that boundary on purpose, and a large fold stays cheap to read because peek takes an offset and a byte count, states the middle it omitted, and reads any child fold by id.
|
|
139
|
-
|
|
140
|
-
The automatic law folds only completed tool batches and structurally closed chapters, absorbs only the tool-result folds inside a chapter's own span, and treats fold placeholders as ordinary span material once the window carries `consolidateAfter` unpinned folds, which keeps the brief index tidy long before pressure matters. Briefs are bounded at 2,000 characters each, one cap for supplied and runtime-written briefs alike, so the visible index normally costs a low single-digit share of the window. Automatic foldability is membership, not position: a span folds because it is a completed tool batch or older material a chapter can compose, and what holds it back is the agent's pins and the blacklist. Openness is structural and event-level: an incomplete tool batch is never proposable, and the routine commit cuts stalest first and stops at its aim, so the newest events stay raw for exactly as long as there is room for them.
|
|
141
|
-
|
|
142
|
-
Below `maxTarget` occupancy of the serving budget, 80% by default, the window is quiet: marks accumulate but nothing commits, because a commit is an epoch transition that risks the whole prefix cache, and the cadence has to be the fewest commits that still keep the window inside its budget. The remaining fifth of the budget is the runway the commit itself spends. Every occupancy number in this document is a share of that one serving budget: there is a single denominator, `capacityAccounting.budgetTokens`, and no threshold is stated against the raw provider window.
|
|
143
|
-
|
|
144
|
-
Every brief is deterministic, written by the runtime from the span itself with no model call: a tool fold carries the call, its arguments, the head and tail of the result, and the agent's own notes from the batch; a chapter carries its opening text and every assistant note in span order; a consolidation indexes each child. A model-written brief generator shipped through 1.0.x and was deleted on its own measurement: across 15 sealed runs it wrote 1,186 upgraded briefs, 75 percent of which were never consulted by the agent or never became visible before the session ended, and a build with the generator removed scored the same recall for less money. The agent that wants better words on a fold writes them itself: `brief` puts them on the pending fold and a supplied brief is kept verbatim. There is no way to rewrite a standing fold's brief, because doing so rewrites the projection at that fold's placeholder and costs the whole prefix cache from there on. Sessions already holding model-written briefs keep them on load.
|
|
145
|
-
|
|
146
|
-
The serving budget is what the deployment states it may fill, because the per-request max-input descriptor a provider advertises assumes a full output reservation and understates the real ceiling. With no `providerInputBudget` supplied the runtime falls back to the transport's descriptor and estimates a reservation out of it, which is the conservative default for a model it has no budget fact for; the descriptor is reported in status either way, so the gap stays auditable. A provider-window fence aborts an unsafe request before transmission if no lossless fold can make room, and says so out of band rather than by writing into the window.
|
|
147
|
-
|
|
148
|
-
Pi's automatic compaction stays enabled underneath pi-fold; there is no setting to turn off. The runtime intercepts every compaction pass and answers it by kind. A threshold pass is cancelled outright, because the fold commit at `maxTarget` fires first and folding remains authoritative. An overflow pass, the one Pi starts when the provider rejects a request as too large and is prepared to retry, becomes a recovery in the shape of `/tree`, automated: the failed path is labeled with its lineage, the session branches back to the last message before the error, and the retried pass folds and commits before it transmits, so the re-issued message fits inside a window that kept its folds and its exact originals. The agent gets one notice that a rollback and commit happened. A trailing assistant message still holding unanswered tool calls is not replayed, and the notice says so. Manual `/compact` passes through untouched as the user's escape hatch. Turning Pi's automatic compaction off removes the hook this lane arms on: the runtime still classifies an overflow when it appears, but reports that it cannot roll back rather than guessing at a tree whose agent state disagrees with it.
|
|
149
|
-
|
|
150
|
-
Evidence ingestion writes read-only artifact files under the session directory's `pi-fold-evidence/`, sets them to mode `0444`, and enforces a 512 MB session cap. These immutable files provide exact-recovery anchors for oversized tool results. It is always on and there is no setting to turn it off: the artifacts are what an oversized result folds against, so a deployment that stopped writing them would keep its folds and lose what makes them lossless.
|
|
151
|
-
|
|
152
|
-
## Epoch fold scheduling
|
|
153
|
-
|
|
154
|
-
Folding is cheap to decide and expensive to apply, and the expense has nothing to do with how much a fold saves. The bill is set by how often the projection changes, not by how many tokens each change reclaims. A session that folds once per turn re-sends its prefix once per turn; a session that folds three times in total re-sends it three times.
|
|
64
|
+
### The cache invariant
|
|
155
65
|
|
|
156
|
-
|
|
66
|
+
**A provider prefix cache is positional.** It replays the longest byte-identical prefix of the prompt, so one changed byte at offset K discards everything cached after K. The cost of a mutation has nothing to do with its size.
|
|
157
67
|
|
|
158
|
-
|
|
68
|
+
**So the runtime may move a byte in the window only at a moment it is already rewriting the window**, which is a commit or a fold. Everything else is a pure append, and an appended byte is never later altered, shortened, removed or repositioned. Never mutate the window, and never show something and then take it back. The second half is the one that costs features: anything that displays state and then updates it in place is out, because updating in place is a mid-prefix write with better manners.
|
|
159
69
|
|
|
160
|
-
|
|
70
|
+
### Marks and commits
|
|
161
71
|
|
|
162
|
-
|
|
72
|
+
<picture>
|
|
73
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/shaneconner/pi-fold/main/media/marks-and-commits-dark.svg">
|
|
74
|
+
<img alt="A completed unit becomes a pending mark that leaves the window and its cached prefix untouched. Marks repeat for free below maxTarget; at maxTarget one commit applies every standing mark in a single rewrite. From the brief, peek appends the exact bytes and expand restores them in place, both after verifying the SHA-256." src="https://raw.githubusercontent.com/shaneconner/pi-fold/main/media/marks-and-commits-light.svg">
|
|
75
|
+
</picture>
|
|
163
76
|
|
|
164
|
-
|
|
77
|
+
A mark moves nothing. The moment a tool batch or chapter closes, the runtime cuts it into a pending mark, up to eight cuts per pass, stalest first. The mark lives in durable state outside the window, so the projection stays byte-identical and the cached prefix survives. Marks accumulate for free until one commit applies all of them in a single rewrite.
|
|
165
78
|
|
|
166
|
-
|
|
79
|
+
What the agent owns is the words. `brief` writes its own sentence onto a pending fold for free, because that brief reaches the window only when the commit writes the placeholder for the first time. A supplied brief is kept verbatim; an unbriefed fold commits with the deterministic brief the runtime writes from the span itself, and the provenance records which happened. Curation stays correctable until the commit: `reboundary` re-cuts a span, `unmark` withdraws a pending mark, and a pending brief can be rewritten any number of times at no cost. A standing fold's brief is never rewritten, because that rewrites the projection at its placeholder.
|
|
167
80
|
|
|
168
|
-
|
|
81
|
+
### The band
|
|
169
82
|
|
|
170
|
-
|
|
83
|
+
A commit fires on one condition: occupancy reaching `maxTarget` of the serving budget. It applies marks stalest first and stops at `minTarget`, so the newest events stay raw and a mark past the bound waits for the next epoch. Below `maxTarget` the window is quiet, because a commit is an epoch transition that risks the whole prefix cache and the right cadence is the fewest commits that keep the window inside its budget.
|
|
171
84
|
|
|
172
|
-
|
|
85
|
+
```
|
|
86
|
+
272,000 the transport's descriptor
|
|
87
|
+
─────────────────────────────────────────────────────
|
|
88
|
+
~250k peak: inflow continues while the epoch computes
|
|
89
|
+
217,600 maxTarget 0.80 ← the commit fires here
|
|
90
|
+
⋮ marks accumulate, free, quiet
|
|
91
|
+
54,400 minTarget 0.20 ← the commit stops here
|
|
92
|
+
─────────────────────────────────────────────────────
|
|
93
|
+
```
|
|
173
94
|
|
|
174
|
-
|
|
95
|
+
Every commit re-reads roughly `minTarget` of the budget uncached, so a shallower cut buys less runway for nearly the same bill: the rewrite tax is `minTarget / (maxTarget - minTarget)`, 0.33 at 0.80/0.20 against 1.0 at 0.80/0.40.
|
|
175
96
|
|
|
176
|
-
|
|
97
|
+
### Peek and expand
|
|
177
98
|
|
|
178
|
-
|
|
99
|
+
`peek` returns a fold's exact SHA-256-verified source as a tool result at any depth, with the ancestors still collapsed. It appends and never edits the prefix, so checking what a fold holds costs nothing a commit has to pay for. It takes `offset` and `bytes` for a bounded slice and reads any child fold by id, so a large fold has a narrow read.
|
|
179
100
|
|
|
180
|
-
|
|
101
|
+
A peek is ephemeral by default: the bytes ride the window until the model's next message, then their place holds a one-line placeholder and the reply that used them is the surviving trace. Peeking again is lossless and costs one append. The default follows what the agent chose: on the run measured for it, six of seven peeks asked for ephemeral at first exposure. `expand` is the commitment, restoring the source in place until the fold is refolded. Looking is not the same as taking.
|
|
181
102
|
|
|
182
|
-
|
|
103
|
+
### Clipping, pins and the compaction path
|
|
183
104
|
|
|
184
|
-
|
|
105
|
+
Tool results get a second, lighter treatment at the same commit. Results in the oldest `toolFoldThreshold` share of the projected window, half of it by default, are clipped in view: the identifying head stays, the rest becomes a marker naming the entry id that peeks it back, and the stored bytes are untouched, so a later fold of that span still encodes the full result.
|
|
185
106
|
|
|
186
|
-
|
|
107
|
+
Briefs are bounded at 2,000 characters each, so the visible index normally costs a low single-digit share of the window. Folds nest rather than partially overlap, and collapsed folds double as a browsable index: `status` with `detail: "tree"` lists every fold in transcript order with its depth and parent. `pin` holds entries raw through every fold and frees nothing, so pinned mass makes the rest of the window fold sooner; a pinned-share cap stops that from becoming a window nothing can reclaim.
|
|
187
108
|
|
|
188
|
-
|
|
109
|
+
Pi's automatic compaction stays enabled underneath and is intercepted by kind. A threshold pass is cancelled, because the fold commit fires first. An overflow pass becomes an automated recovery: the session branches back to the last message before the error, and the retried pass folds and commits before transmitting. Manual `/compact` passes through untouched as the escape hatch.
|
|
189
110
|
|
|
190
|
-
|
|
111
|
+
Evidence ingestion writes read-only artifacts under the session directory's `pi-fold-evidence/` at mode `0444`, under a 512 MB session cap. It is always on: the artifacts are what an oversized result folds against, so a deployment that stopped writing them would keep its folds and lose what makes them lossless.
|
|
191
112
|
|
|
192
|
-
##
|
|
113
|
+
## Configuration
|
|
193
114
|
|
|
194
|
-
pi
|
|
115
|
+
`registerPiFold(pi)` works with no arguments, and the defaults are the configuration the campaign above actually ran: deterministic briefs, the invitation off, the tool-call diet at half the window, and a 0.80/0.20 band. Nothing here needs tuning.
|
|
195
116
|
|
|
196
|
-
|
|
117
|
+
The one thing worth adding is a long-term memory store. Rotation and retention are different jobs: folding gets stale material out of the window, a store keeps what the session learned. A handoff summary tries to do both in one record, written once under time pressure, bounded by the window it has to fit in. Folding is the rotation half and only that half.
|
|
197
118
|
|
|
198
|
-
|
|
119
|
+
Six options, and every other name is refused.
|
|
199
120
|
|
|
200
|
-
|
|
121
|
+
| Option | Default | Effect |
|
|
122
|
+
| --- | --- | --- |
|
|
123
|
+
| `thresholds` | `{ maxTarget: 0.80, minTarget: 0.20, consolidateAfter: 10, minFoldChars: 8000 }` | The band, set whole or not at all. The first two are proportions of the serving budget. `consolidateAfter` is how many visible roots a parent is owed. `minFoldChars` is the size a fold has to reach to be worth making, and the floor a gap between folds has to clear to stay raw. Validated atomically; an impossible policy is refused by name rather than clamped. |
|
|
124
|
+
| `providerInputBudget` | the transport's descriptor | The tokens this deployment may actually put in a request, already net of any output reservation. Every ratio, fence and budget on this page is a share of this one number. |
|
|
125
|
+
| `blacklistAutoFoldTools` | `new Set()` | The exception list. Every completed tool batch is foldable without an agent mark; name a tool here and its results stay raw. |
|
|
126
|
+
| `toolFoldThreshold` | `0.50` | The tool-call diet: a share in `[0, 1)` naming the oldest fraction of the window whose tool results are clipped in view. `0` turns it off. |
|
|
127
|
+
| `postFoldNotice` | `false` | The invitation switch. False is the shape that won the campaign above: no carrier invites a brief and every fold ships the runtime's own words. `true` appends a standing invitation to improve a pending brief. |
|
|
128
|
+
| `workingMemory` | off | An in-window keyed dictionary the agent maintains beside the fold index, with `remember` and `recall` added to the tool. Offered to one run, which never called it. |
|
|
201
129
|
|
|
202
|
-
|
|
130
|
+
Four commands ship for the human: `/fold-status`, `/fold` (commit every staged mark now), `/fold-editor` (the interactive map of the window) and `/fold-settings`.
|
|
203
131
|
|
|
204
|
-
|
|
132
|
+
Upgrading from 2.x: the `fold` action is gone from the tool surface, since the runtime stages every completed unit itself; `guidance` is refused by name and `postFoldNotice` replaces it; `thresholds.freshTail` is gone, and a settings file carrying it is migrated with every tuned value kept. Existing sessions stay readable, folds and briefs included.
|
|
205
133
|
|
|
206
|
-
|
|
134
|
+
## Prior work
|
|
207
135
|
|
|
208
|
-
|
|
209
|
-
| --- | --- | --- |
|
|
210
|
-
| `thresholds` | `{ maxTarget: 0.80, minTarget: 0.40, consolidateAfter: 10, minFoldChars: 8000 }` | The band, set whole or not at all. The first two are proportions of the serving budget: fold at `maxTarget`, fold down to `minTarget`. Against a 272,000-token descriptor the defaults hold the session in roughly a 100k to 250k band: the trigger fires at 80% and the session runs one epoch's inflow past it before the commit lands, and the aim leaves about 100k of the freshest context raw across the whole cycle. `consolidateAfter` is a positive integer count of visible roots per parent owed. `minFoldChars` is the size in characters a fold has to reach to be worth making, and the same number is the floor a gap between two folds has to clear to stay raw: anything smaller is absorbed by the fold beside it. Characters rather than tokens because a token means a different amount of text on every wire. Validated atomically at registration, and an impossible policy is refused by name rather than clamped. User policy only: no agent action can read it back as a mutable surface, though status reports the values. |
|
|
211
|
-
| `providerInputBudget` | the transport's descriptor | Machine surface, off the `/fold-settings` screen as of 2.1: the tokens this deployment may actually put into a request, already net of whatever output reservation it holds back. Every ratio, fence and budget divides by it directly, and pi-fold subtracts nothing from a declared value. It exists for a harness that needs runs comparable across descriptor changes. Shares otherwise apply to the model's own window: the runtime falls back to the transport's descriptor and estimates the reservation out of it, and the descriptor is reported in status either way, so the gap stays auditable. |
|
|
212
|
-
| `blacklistAutoFoldTools` | `new Set()` | The exception list. Every completed tool batch is foldable by the runtime on its own, without an agent mark; name a tool here and its results stay raw instead. Empty by default, because foldability is not the protection: pins are, and they apply to a blacklisted tool and an ordinary one alike. Pass a `ReadonlySet<string>`. Blacklisting is not free: the mass stays in the window until a chapter fold takes it, and a closed unit that alone exceeds the chapter cap is reclaimed whole rather than in pieces. |
|
|
213
|
-
|
|
214
|
-
### Migrating from 2.x
|
|
215
|
-
|
|
216
|
-
Version 3.0 moves the cutting from the agent to the runtime, on the first live session's measurement: a session that never closes its turn never handed the old design a boundary to fold at, so the guard that waited for one held everything while occupancy climbed. Boundaries are now events, not turns.
|
|
217
|
-
|
|
218
|
-
- The `fold` action is deleted from the tool surface. The runtime stages every completed unit itself; the agent annotates with `brief`, corrects with `reboundary` and `unmark`, and protects with `pin`.
|
|
219
|
-
- Remove `guidance`. The option is refused by name: it switched the copy that taught the agent to choose spans, and the agent no longer chooses spans. The post-fold notice that replaced it is not optional, because a fold the agent is never told about is one it can never brief.
|
|
220
|
-
- `thresholds.freshTail` is gone: fresh-tail protection is deleted, because openness is structural per event and the commit's depth bound already stops at the aim. A settings file carrying the key is migrated by dropping it, with every tuned value kept.
|
|
221
|
-
- The default `thresholds` are `maxTarget: 0.80` and `minTarget: 0.40`, which against a 272,000-token descriptor holds a session in roughly a 100k to 250k band.
|
|
222
|
-
- Existing sessions remain readable, folds and briefs included.
|
|
223
|
-
|
|
224
|
-
### Migrating from 1.x
|
|
225
|
-
|
|
226
|
-
Version 2.0 makes the measured deletions explicit instead of accepting options that no longer control the runtime.
|
|
227
|
-
|
|
228
|
-
- Remove `summarizer` and `summarizeContextSpan`. The model brief generator is deleted; every new fold carries a deterministic or agent-supplied brief. Passing either name is refused with the deletion reason.
|
|
229
|
-
- Remove `guidance.thresholdNotices`. `guidance` now contains only `actionResponses`.
|
|
230
|
-
- The default `thresholds.minTarget` is now `0.40`. A host supplying the whole threshold object keeps its declared value.
|
|
231
|
-
- Existing sessions remain readable. Fold records already carrying model-authored briefs keep those briefs and their provenance on load.
|
|
232
|
-
|
|
233
|
-
### Projection candidate records
|
|
234
|
-
|
|
235
|
-
`registerPiFold` returns `{ projectionCandidates }`, a provider a host may call with a Pi context. It emits one record per visible collapsed fold. The fields and current values are:
|
|
236
|
-
|
|
237
|
-
| Field | Emitted value |
|
|
238
|
-
| --- | --- |
|
|
239
|
-
| `version` | `1` |
|
|
240
|
-
| `key` | `"projection:<fold-id>"` |
|
|
241
|
-
| `kind` | `"projection"` |
|
|
242
|
-
| `domain` | `"system"` |
|
|
243
|
-
| `horizon` | `"working"` |
|
|
244
|
-
| `source_id` | Fold ID. |
|
|
245
|
-
| `source_version` | Exact source SHA-256. |
|
|
246
|
-
| `route` | `{ tool: "pi_fold_context", arguments: { action: "expand", id: <fold-id> } }` |
|
|
247
|
-
| `token_cost` | Estimated replacement tokens, rounded up with a minimum of 1. |
|
|
248
|
-
| `expansion_cost` | Estimated exact-source tokens, rounded up with a minimum of 1. |
|
|
249
|
-
| `rank` | Zero-based position among emitted candidates. |
|
|
250
|
-
| `score` | Saved-byte fraction, capped at 1. |
|
|
251
|
-
| `raw_score` | Exact bytes saved by the fold. |
|
|
252
|
-
| `confidence` | `"exact"` |
|
|
253
|
-
| `freshness` | `"current"` |
|
|
254
|
-
| `locked_owner` | `false` |
|
|
255
|
-
| `collapse_key` | `"projection:<fold-id>"` |
|
|
256
|
-
| `generator` | `"projection-model"`, `"projection-deterministic"`, or `"projection-supplied"` from brief provenance. |
|
|
257
|
-
| `generator_version` | `"memory-slate-generators-v2"` |
|
|
258
|
-
| `recency` | `null` |
|
|
259
|
-
|
|
260
|
-
## Status
|
|
261
|
-
|
|
262
|
-
Stable. The option surface is settled and the append-only projection invariant is enforced by the gate suite, which is the contract: `tests/verify.mjs` covers the runtime and `scripts/verify_pi_context_experiment.mjs` covers the experiment harness. New behavior lands with its gate.
|
|
263
|
-
|
|
264
|
-
## Development
|
|
136
|
+
Two recent lines inform this one. Lossless hierarchical compaction, exemplified by LCM ([arXiv:2605.04050](https://arxiv.org/abs/2605.04050)) with a walkthrough at [losslesscontext.ai](https://www.losslesscontext.ai/), builds a summary DAG over older messages with lossless pointers to every original. Agentic context management, exemplified by [Letta](https://github.com/letta-ai/letta) and by [arXiv:2510.11967](https://arxiv.org/abs/2510.11967), gives the agent tools over its own memory. See also [arXiv:2607.00692](https://arxiv.org/abs/2607.00692).
|
|
265
137
|
|
|
266
|
-
|
|
267
|
-
npm ci
|
|
268
|
-
npm run lint
|
|
269
|
-
npm test
|
|
270
|
-
node scripts/verify_pi_context_experiment.mjs
|
|
271
|
-
```
|
|
138
|
+
What this one adds is an efficiency result, arrived at by running the mechanism repeatedly and following the bill. Rotation is the part every approach here shares. The cost sits in where the rewrites land, because a prefix cache charges by position rather than by size, so each fold is staged as a mark that moves nothing and one commit spends the cache for all of them at once. The same campaigns settled the recommended shape: deterministic briefs paired with a durable memory store, which held recall at the lowest cost and wall clock measured. Agent control over boundaries and brief text stays available, and the invitation to use it ships off.
|