pi-fold: Agent-Governed Lossless Context Folding. Design, and a single-workload trace evaluation against native compaction

Read full text
Authors: Shane Conner
Cited by: 2

Abstract

Long agent sessions end in compaction: the transcript is summarized, the originals leave the model's view, and the session restarts from the summary. pi-fold is a context-folding runtime for the Pi coding agent that takes a different position: finished spans of the transcript collapse in place into short briefs, the exact original entries move to a SHA-256-addressed store, and two in-band verbs, peek and expand, return them byte for byte, verified against their hashes. The design gives the session agent the folding verbs, so cuts can follow the work rather than position; an automatic ladder underneath folds conservatively when the agent stays silent. One measured campaign compared this runtime with Pi's native compaction on a 64-stage repository-comprehension workload built to force context shedding: one run per arm, same seed, same plan hash, same model, costs taken from Pi's own per-request ledger and reconciled to the microdollar. The two pairings reuse one folding run, so every number is a description of the runs that happened, not an estimate. Cut at the last common stage, folding spent 0.60x the total tokens and 0.55x the dollars of compaction (0.70x at the provider's flat base rates); on completed runs from different days, 0.67x the tokens and 0.66x the dollars (0.85x flat). Two mechanism findings came out of the reconciliation: pooled cache-hit share ranked the arms opposite to cost, and a long-context tier at 272,000 tokens billed over a fifth of the compaction arm's total while the folding arm's budget kept it below the boundary. The folding arm's wire history is 76 pure appends and six attributed batched rewrites. Completed runs on both arms delivered all four work products. Recall probes are reported and rated as a weak instrument: the probed facts can be re-derived by re-reading the repository, so the probes reward re-reading as much as retention, and folding scored slightly worse on them. The distinguishing design claim went unexercised: the comparison run's agent never marked, peeked, or expanded, so the headline numbers are the automatic fallback's, and neither agent-directed curation nor in-band exact recovery has measurement behind it yet. The data archive alongside the paper carries the figure sources, the redacted per-request ledgers for the cited runs, and the full campaign log; SHA256SUMS covers every file. The pi-fold software itself is MIT licensed at github.com/shaneconner/pi-fold. The interactive version of every figure, and the live demo of the mechanism, are at shaneconner.com/projects/pi-fold.

Fields of Study & Subjects

Logic programming and type systems Parallel Computing and Optimization Techniques Software Engineering Research

Publication details

Journal / source
Zenodo (CERN European Organization for Nuclear Research)
Publication date
9 August 2026
Document type
Preprint