pi-fold 0.0.1 → 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shane Conner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,18 +1,83 @@
1
1
  # pi-fold
2
2
 
3
- Lossless, agent-governed context folding for the [Pi coding agent](https://pi.dev).
3
+ pi-fold provides lossless, agent-governed context folding for Pi. It changes only the oldest eligible context while leaving the fresh window untouched.
4
4
 
5
- Long sessions never stop to compact and never reread what they already knew: tool
6
- results and closed chapters fold into content-addressed, SHA-verified briefs that the
7
- agent can expand back to the exact original bytes at any time. Only the oldest context
8
- ever changes — the fresh working window stays untouched, so work continues mid-stride.
5
+ ![pi-fold folding a live session](https://raw.githubusercontent.com/shaneconner/pi-fold/main/docs/demo.gif)
6
+
7
+ *A live session at 5.5× speed: the autonomous ladder folds a completed read batch mid-conversation — no command issued — and `/context` reports the fold.*
9
8
 
10
9
  ## Status
11
10
 
12
- **This version is a name reservation while the first release is finalized** — the
13
- runtime exists and is in production hardening (autonomous fold ladder, deterministic
14
- brief floor, expand leases, width consolidation, cadence-gated folding, a 18-gate
15
- verification suite, and multi-hour soak testing).
11
+ This package is pre-release. Its API may move until 0.1.0. Licensed under [MIT](./LICENSE).
12
+
13
+ ## Install
14
+
15
+ After the first 0.1.0 release:
16
+
17
+ ```sh
18
+ pi install npm:pi-fold
19
+ ```
20
+
21
+ ## How it works
22
+
23
+ pi-fold maps canonical session entries into a fold lattice. A fold replaces an eligible stale span with a short placeholder while retaining ordered references to the exact source entries. Expansion checks the stored SHA-256 identities before restoring that source.
24
+
25
+ An autonomous ladder first folds completed read-only tool batches, then refolds or consolidates existing folds, and finally folds structurally closed chapters. Cadence and fresh-tail rules prevent repeated or premature folding. Chapter briefs are generated by the session's model by default, using one request per fold with a bounded input span, bounded output, and no tool calls. Every summarizer failure falls back to a deterministic brief; set `summarizer: "deterministic"` to disable model calls entirely. The default spends provider tokens, as native compaction does. Advisory milestones describe eligible actions as measured context pressure rises, and the provider-window fence blocks an unsafe request if no lossless fold can make room.
26
+
27
+ By default, 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. Set `evidenceIngestion: false` to disable the hook and all evidence writes.
28
+
29
+ ## Configuration
30
+
31
+ The package entry calls `registerPiFold(pi)` with the defaults below. Hosts that call the named `registerPiFold` export directly may pass an options object. Replacement lists and sets replace the defaults; they are not additive.
32
+
33
+ | Option | Default | Effect |
34
+ | --- | --- | --- |
35
+ | `toolName` | `"active_context"` | Name of the registered context tool. |
36
+ | `toolLabel` | `"Active Context"` | Human-facing label for the registered context tool. |
37
+ | `brandNoun` | `"active-context"` | Short noun used in fold placeholders, advisory headings, fence text, and native-compaction notices. |
38
+ | `entryTypePrefix` | `"pi-fold-active-context"` | Prefix for durable state, fold-record, status, and advisory identifiers. Its namespace also derives measurement/compaction entry types, status source metadata, MCP evidence ownership, the evidence directory, and the unparseable MCP-server fallback. Changing it starts a separate state namespace. |
39
+ | `commandPrefix` | `""` | Prefix for `/context` and `/fold-context`; trailing hyphens are removed. |
40
+ | `commandNames` | `undefined` | Optional full-name overrides for the effective defaults `{ status: "context", fold: "fold-context" }`. Both names must be distinct kebab-case strings; supplied values take precedence over `commandPrefix`. |
41
+ | `summarizer` | `"session"` | Brief generator: the live session model, `"deterministic"` for no model calls, or `{ provider, model, effort? }` resolved through Pi's model registry. Cannot be combined with `summarizeContextSpan`. |
42
+ | `summarizeContextSpan` | `undefined` | Optional async brief generator. Results must include a useful bounded brief, provider/model/effort attribution, and `toolCalls: 0`; failure uses the deterministic brief. |
43
+ | `setProjectionProvider` | `undefined` | Optional host callback that receives the [projection-candidate provider](#projection-candidate-records). Normal Pi context events do not require it. |
44
+ | `toolActions` | all six actions | Replacement allowlist drawn from `status`, `fold`, `expand`, `refold`, `protect`, and `unprotect`. |
45
+ | `blockingTools` | `["Agent"]` | Replacement list of tool names that trigger one opportunistic stale-tool fold before the call. Use `[]` to disable it. |
46
+ | `readOnlyTools` | `new Set(["read", "grep", "find", "ls"])` | Replacement set of tool names whose completed batches may fold automatically. Defaults to Pi's built-in read-only tools. Pass a `ReadonlySet<string>`. |
47
+ | `isMcpTool` | `() => false` | Synchronous predicate enabling evidence ingestion for oversized structured MCP results. Names shaped as `mcp__<server>__<tool>` project the actual server; other names use the neutral namespace derived from `entryTypePrefix`. |
48
+ | `evidenceIngestion` | `true` | Set to `false` to register no evidence-ingestion hook and write no evidence artifacts. |
49
+
50
+ ### Projection candidate records
51
+
52
+ The provider emits one record per visible collapsed fold. The fields and current values are:
53
+
54
+ | Field | Emitted value |
55
+ | --- | --- |
56
+ | `version` | `1` |
57
+ | `key` | `"projection:<fold-id>"` |
58
+ | `kind` | `"projection"` |
59
+ | `domain` | `"system"` |
60
+ | `horizon` | `"working"` |
61
+ | `source_id` | Fold ID. |
62
+ | `source_version` | Exact source SHA-256. |
63
+ | `route` | `{ tool: <toolName>, arguments: { action: "expand", id: <fold-id> } }` |
64
+ | `token_cost` | Estimated replacement tokens, rounded up with a minimum of 1. |
65
+ | `expansion_cost` | Estimated exact-source tokens, rounded up with a minimum of 1. |
66
+ | `rank` | Zero-based position among emitted candidates. |
67
+ | `score` | Saved-byte fraction, capped at 1. |
68
+ | `raw_score` | Exact bytes saved by the fold. |
69
+ | `confidence` | `"exact"` |
70
+ | `freshness` | `"current"` |
71
+ | `locked_owner` | `false` |
72
+ | `collapse_key` | `"projection:<fold-id>"` |
73
+ | `generator` | `"projection-model"`, `"projection-deterministic"`, or `"projection-supplied"` from brief provenance. |
74
+ | `generator_version` | `"memory-slate-generators-v2"` |
75
+ | `recency` | `null` |
76
+
77
+ ## Development
16
78
 
17
- First release: days away, at [shaneconner/pi-fold](https://github.com/shaneconner/pi-fold),
18
- under an open-source license.
79
+ ```sh
80
+ npm ci
81
+ npm run lint
82
+ npm test
83
+ ```