pi-agenticoding 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 Agentic Coding
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 ADDED
@@ -0,0 +1,240 @@
1
+ # pi-agenticoding
2
+
3
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
4
+ ![npm version](https://img.shields.io/badge/npm-0.1.0-blue)
5
+ ![Status](https://img.shields.io/badge/status-active-green)
6
+
7
+ **Context management for the pi coding agent.** Three primitives — spawn, ledger, handoff — that keep your agent focused, prevent context rot, and make complex multi-step tasks actually finish.
8
+
9
+ ---
10
+
11
+ ## Stop treating context like infinite RAM.
12
+
13
+ Every coding agent degrades as the conversation grows. The model "forgets" the beginning, hallucinates stale assumptions, and burns tokens re-deriving context it already knew. By the time you hit the token limit, performance has been degrading for a while — long before the error.
14
+
15
+ *pi-agenticoding* is the antidote. It gives your agent three concrete primitives for managing context deliberately, so it stays sharp across long sessions, survives session restarts, and can pivot between tasks without carrying dead weight.
16
+
17
+ > **What if your agent could hand off knowledge between sessions, isolate messy exploration to child contexts, and restart fresh without losing what it learned?**
18
+
19
+ That's what this does.
20
+
21
+ *This README was written by an agent, using the same primitives this extension teaches: spawn research subtasks, cache findings in a ledger, handoff to the next phase.*
22
+
23
+ ---
24
+
25
+ ## Table of Contents
26
+
27
+ - [Quick Start](#quick-start)
28
+ - [The Three Primitives](#the-three-primitives)
29
+ - [Spawn — isolate noise](#spawn--isolate-noise)
30
+ - [Ledger — continuity across cuts](#ledger--continuity-across-cuts)
31
+ - [Handoff — deliberate compaction](#handoff--deliberate-compaction)
32
+ - [What You Get](#what-you-get)
33
+ - [Under the Hood](#under-the-hood)
34
+ - [Why This Exists](#why-this-exists)
35
+ - [Contributing](#contributing)
36
+ - [License](#license)
37
+
38
+ ---
39
+
40
+ ## Quick Start
41
+
42
+ **1. Install**
43
+
44
+ ```bash
45
+ npm install pi-agenticoding
46
+ ```
47
+
48
+ **2. Enable in your pi configuration**
49
+
50
+ Add the extension to your `pi` config and restart:
51
+
52
+ ```json
53
+ {
54
+ "extensions": ["pi-agenticoding"]
55
+ }
56
+ ```
57
+
58
+ **3. You're done.**
59
+
60
+ Your agent now has access to `spawn`, `ledger_add`, `ledger_get`, `ledger_list`, and `handoff` tools. The status bar will show context usage and ledger entry count.
61
+
62
+ **Try it in 30 seconds:**
63
+
64
+ ```bash
65
+ # Tell your agent:
66
+ # "Use spawn to research what the current primacy-zone heuristic says
67
+ # about context degradation, then ledger_add a summary."
68
+ ```
69
+
70
+ The agent spawns a child with its own clean context, researches, and caches the result. You can now reference that knowledge across turns without re-deriving it.
71
+
72
+ ---
73
+
74
+ ## The Three Primitives
75
+
76
+ ### Spawn — isolate noise
77
+
78
+ Delegate messy, exploratory work to an isolated child agent. The child has its own clean context, inherits your model and active tools except `handoff`, and only gets `spawn` when depth allows it. The parent stays focused.
79
+
80
+ ```typescript
81
+ // The agent calls spawn — you never see the child's messy exploration
82
+ spawn({
83
+ prompt: "Research best error-handling patterns for async TypeScript.
84
+ Summarize top 3 with code snippets.",
85
+ })
86
+ // Returns: a concise summary. All intermediate noise stays in the child.
87
+ ```
88
+
89
+ - Max depth: 1 (parent → child only)
90
+ - Real-time TUI rendering of the child session
91
+ - Token cost and usage stats reported back
92
+ - Ledger writes from children are visible to parent (same shared state)
93
+
94
+ ### Ledger — continuity across cuts
95
+
96
+ A sparse key-value cache that survives context resets, handoffs, and session restarts. The agent saves compact reusable knowledge — facts, decisions, constraints, discoveries — and fetches it on demand.
97
+
98
+ ```typescript
99
+ // Save
100
+ ledger_add({
101
+ name: "architecture-decision",
102
+ content: "- We chose SQLite for local-first sync\n- Reason: offline-first requirement\n- Constraint: max 5 concurrent writers",
103
+ })
104
+
105
+ // Retrieve
106
+ ledger_get({ name: "architecture-decision" })
107
+ // → restores full body
108
+
109
+ // List
110
+ ledger_list()
111
+ // → architecture-decision: We chose SQLite for local-first sync
112
+ ```
113
+
114
+ - Entries persist in the session file — survive restarts
115
+ - Newest write wins on rehydration
116
+ - Children share the same ledger (one truth across contexts)
117
+ - Capped at 50KB / 2000 lines per entry
118
+
119
+ ### Handoff — deliberate compaction
120
+
121
+ When context gets noisy or the job changes, handoff replaces the active context with a clean restart brief. The agent captures what matters, inlines referenced ledger entries, and starts fresh.
122
+
123
+ ```bash
124
+ # User triggers a handoff:
125
+ /handoff Implement auth module
126
+
127
+ # The agent:
128
+ # 1. Saves reusable state to the ledger
129
+ # 2. Drafts a concise brief that completes the picture
130
+ # 3. Calls handoff() — context compacts, brief becomes the new top
131
+ # 4. Continues in a clean context with all knowledge preserved
132
+ ```
133
+
134
+ - `/handoff <direction>` command for user-triggered pivots
135
+ - `handoff()` tool for agent-initiated compaction
136
+ - Inlines referenced ledger entries (up to 3, 4000 chars) into the brief
137
+ - Full history preserved in session file — nothing lost
138
+
139
+ ---
140
+
141
+ ## What You Get
142
+
143
+ | Feature | What it looks like |
144
+ |---------|-------------------|
145
+ | **Context usage %** | `ctx 65%` in status bar — green < 30%, yellow < 50%, orange < 70%, red ≥ 70% |
146
+ | **Ledger count** | 📒 `3` when entries exist, hidden when empty |
147
+ | **`/handoff` command** | Instant pivot — agent drafts brief, compacts context, resumes |
148
+ | **`/ledger` command** | Overlay showing all entries with previews |
149
+ | **Auto-rehydration** | Ledger entries survive session restarts — determined by epoch |
150
+ | **Spawn transparency** | Watch child agents work in real time in the TUI |
151
+ | **Token cost visibility** | Each spawn reports input/output tokens, cache hits, and cost |
152
+ | **No polling** | writes are serialized via a process-local lock — no race conditions |
153
+
154
+ ---
155
+
156
+ ## Under the Hood
157
+
158
+ <details>
159
+ <summary><strong>Architecture overview</strong> — how the three primitives wire together</summary>
160
+
161
+ The extension hooks into pi's lifecycle at key points:
162
+
163
+ | Hook | What it does |
164
+ |------|-------------|
165
+ | `before_agent_start` | Injects context management primer + live ledger listing into system prompt |
166
+ | `context` | Injects advisory watchdog reminders when context > 30% |
167
+ | `session_start` | Rehydrates ledger from persisted entries; resets on `/new` |
168
+ | `turn_end` | Updates TUI indicators (context %, ledger count) |
169
+ | `agent_end` | Records last context usage percent |
170
+ | `session_before_compact` | Consumes pending handoff task and sets it as compaction summary |
171
+
172
+ All state lives in a single `AgenticodingState` instance:
173
+
174
+ ```typescript
175
+ interface AgenticodingState {
176
+ ledger: Map<string, string> // keyed by kebab-case name
177
+ epoch: number // set on first ledger_add, for rehydration
178
+ lastContextPercent: number | null // last reading from getContextUsage()
179
+ pendingHandoff: { task, source } | null
180
+ pendingRequestedHandoff: { direction, ... } | null
181
+ childSessions: Map<string, AgentSession> // render handoff queue keyed by toolCallId
182
+ liveChildSessions: Map<string, AgentSession> // live registry, including claimed sessions
183
+ childSessionEpoch: number // increments on /new to invalidate stale child updates
184
+ }
185
+ ```
186
+
187
+ </details>
188
+
189
+ <details>
190
+ <summary><strong>The primacy-zone heuristic</strong> — why 30% matters</summary>
191
+
192
+ LLMs use context unevenly. Performance degrades as context grows — even far from the token limit. The **first ~30%** is a practical heuristic for keeping the current job in active focus. Past that, the agent is nudged to consider handoff.
193
+
194
+ The watchdog never force-disengages — it's advisory only. Three tiers:
195
+
196
+ - **30–50%**: "Consider handoff if the phase is done or context is noisy"
197
+ - **50–70%**: "Well past the heuristic — consider a deliberate handoff"
198
+ - **≥70%**: "Deep in the degraded zone. Save state, draft a brief, call handoff"
199
+
200
+ </details>
201
+
202
+ <details>
203
+ <summary><strong>Architecture deep dive</strong> → <code>ARCHITECTURE.md</code></summary>
204
+
205
+ See [ARCHITECTURE.md](ARCHITECTURE.md) for:
206
+ - Full module breakdown (handoff/, ledger/, spawn/, state.ts, system-prompt.ts, watchdog.ts)
207
+ - Tool definitions and parameter schemas
208
+ - Lifecycle hook wiring details
209
+ - Spawn depth tracking and child session lifecycle
210
+ - Ledger rehydration algorithm and epoch mechanics
211
+
212
+ </details>
213
+
214
+ ---
215
+
216
+ ## Why This Exists
217
+
218
+ LLM context management is underspecified. Most developers discover it the hard way — their agent starts forgetting, hallucinating, or grinding to a halt mid-task. There's no built-in vocabulary for "save this discovery" or "start fresh with what I've learned."
219
+
220
+ pi-agenticoding provides that vocabulary, embedded directly into the agent's toolset. The agent learns to manage its own context because the system teaches it how.
221
+
222
+ The three primitives aren't arbitrary — they correspond to the three fundamental operations in any context management system:
223
+
224
+ | Operation | Primitive | What it prevents |
225
+ |-----------|-----------|-----------------|
226
+ | Isolate | **Spawn** | Context pollution from noisy subtasks |
227
+ | Persist | **Ledger** | Knowledge loss across resets and pivots |
228
+ | Compact | **Handoff** | Degradation from overstuffed context |
229
+
230
+ ---
231
+
232
+ ## Contributing
233
+
234
+ Contributions are welcome. Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
235
+
236
+ ---
237
+
238
+ ## License
239
+
240
+ MIT — see [LICENSE](LICENSE).