Skip to content

Commit a5d583c

Browse files
committed
Upgrade notebook semantics to durable subject-oriented grounding
1 parent e55ca65 commit a5d583c

3 files changed

Lines changed: 65 additions & 47 deletions

File tree

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![MIT License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
66
![Status](https://img.shields.io/badge/status-active-brightgreen)
77

8-
**A Pi extension that gives the LLM tools to manage its own context.** `spawn`, `ledger`, and `handoff` let the agent actively isolate work, persist reusable knowledge, and restart clean — without platform compaction or manual copy-paste.
8+
**A Pi extension that gives the LLM tools to manage its own context.** `spawn`, `notebook`, and `handoff` let the agent actively isolate work, persist reusable knowledge, and restart clean — without platform compaction or manual copy-paste.
99

1010
---
1111

@@ -40,7 +40,7 @@ Then disable pi's built-in compaction so handoff stays in control:
4040
}
4141
```
4242

43-
That's it. Your agent now has `spawn`, `ledger_add`, `ledger_get`, `ledger_list`, and `handoff`. The status bar shows context usage and ledger count.
43+
That's it. Your agent now has `spawn`, `notebook_write`, `notebook_read`, `notebook_index`, and `handoff`. The status bar shows context usage and notebook count.
4444

4545
---
4646

@@ -49,10 +49,10 @@ That's it. Your agent now has `spawn`, `ledger_add`, `ledger_get`, `ledger_list`
4949
| Feature | What it looks like |
5050
|---------|-------------------|
5151
| **Context usage %** | `ctx 65%` in status bar — green < 30%, yellow < 50%, orange < 70%, red ≥ 70% |
52-
| **Ledger count** | 📒 `3` when entries exist, hidden when empty |
52+
| **Notebook count** | 📒 `3` when pages exist, dim `📒 0` when empty |
5353
| **`/handoff` command** | Instant pivot — agent drafts brief, compacts context, resumes |
54-
| **`/ledger` command** | Overlay showing all entries with previews |
55-
| **Auto-rehydration** | Ledger entries survive session restarts |
54+
| **`/notebook` command** | Overlay showing all notebook pages with previews |
55+
| **Auto-rehydration** | Notebook pages survive session restarts |
5656
| **Spawn transparency** | Watch child agents work in real time in the TUI |
5757
| **Token cost visibility** | Each spawn reports input/output tokens, cache hits, and cost |
5858
| **No polling** | Writes serialized via a process-local lock — no race conditions |
@@ -86,17 +86,17 @@ You: "Add OAuth to the backend"
8686
spawn("audit current auth code")
8787
8888
89-
ledger_add("oauth-decisions", "Flow: PKCE. Scope: read+write.")
89+
notebook_write("oauth-decisions", "Flow: PKCE. Scope: read+write.")
9090
9191
├── spawn("implement token endpoint")
9292
└── spawn("write tests")
9393
9494
9595
handoff("Wire OAuth routes into the middleware stack.
96-
Ledger 'oauth-decisions' holds the constraints.")
96+
Notebook page 'oauth-decisions' holds the constraints.")
9797
```
9898

99-
The agent decided to spawn research children, save reusable findings to the ledger, delegate implementation subtasks, and handoff when context got noisy. **You said one sentence.**
99+
The agent decided to spawn research children, save reusable findings to the notebook, delegate implementation subtasks, and handoff when context got noisy. **You said one sentence.**
100100

101101
---
102102

@@ -106,15 +106,15 @@ The agent decided to spawn research children, save reusable findings to the ledg
106106

107107
Delegate messy work to an isolated child agent with clean context. The child inherits the parent's model and tools, works independently, and returns only the condensed result. Siblings run in parallel; the parent stays focused on orchestration. Children cannot spawn grandchildren (explosive branch prevention).
108108

109-
### Ledger — Continuity Across Cuts
109+
### Notebook — Continuity Across Cuts
110110

111-
A sparse continuity cache the agent curates while working. After discovering something reusable — a fact, constraint, decision, or expensive finding — it saves a named entry. Later contexts fetch entries on demand instead of re-deriving the work. The ledger persists across handoffs, context resets, and session restarts.
111+
A sparse pocket notebook the agent curates while working. After discovering something reusable — a fact, constraint, decision, or expensive finding — it writes a named page. Later contexts read pages on demand instead of re-deriving the work. The notebook persists across handoffs, context resets, and session restarts.
112112

113113
### Handoff — Deliberate Compaction
114114

115-
When context degrades or the job changes, the agent saves reusable state to the ledger, writes a focused brief preserving what's still missing, and restarts clean. The new context starts with the brief front-and-center, all ledger entries accessible, and zero noise.
115+
When context degrades or the job changes, the agent saves reusable state to the notebook, writes a focused brief preserving what's still missing, and restarts clean. The new context starts with the brief front-and-center, all notebook pages accessible, and zero noise.
116116

117-
**Rule of thumb:** The ledger holds reusable learned knowledge. Handoff carries the remaining situational context.
117+
**Rule of thumb:** The notebook holds reusable learned knowledge. Handoff carries the remaining situational context.
118118

119119
---
120120

@@ -139,7 +139,7 @@ A single summary blob mixes durable knowledge with transient situational context
139139
| Operation | Primitive | What It Prevents |
140140
|-----------|-----------|-----------------|
141141
| **Isolate** | Spawn | Context pollution from noisy subtasks |
142-
| **Persist** | Ledger | Knowledge loss across resets and pivots |
142+
| **Persist** | Notebook | Knowledge loss across resets and pivots |
143143
| **Compact** | Handoff | Degradation from overstuffed context |
144144

145145
---
@@ -150,10 +150,10 @@ A single summary blob mixes durable knowledge with transient situational context
150150
|---|---|---|---|---|
151151
| **Compaction** | Runtime decides | User decides | Manual wipe + copy-paste | **Agent decides** |
152152
| **Subagents** | Pre-defined or manual trigger | None | None | **Agent spawns dynamically** |
153-
| **Persistent memory** | Background-generated (if at all) | None | None — gone on reset | **Ledger — agent-curated reusable continuity** |
153+
| **Persistent memory** | Background-generated (if at all) | None | None — gone on reset | **Notebook — agent-curated reusable continuity** |
154154
| **Context awareness** | Token count only | Token count only | None | **Primacy-zone heuristic (~30%)** |
155-
| **Cross-session continuity** | Rare (opt-in, background) | Manual copy-paste | Manual copy-paste | **Ledger persists across restarts** |
156-
| **Structured handoff** | No | No | No | **Yes — resets context while carrying forward non-ledger state explicitly** |
155+
| **Cross-session continuity** | Rare (opt-in, background) | Manual copy-paste | Manual copy-paste | **Notebook persists across restarts** |
156+
| **Structured handoff** | No | No | No | **Yes — resets context while carrying forward non-notebook state explicitly** |
157157

158158
---
159159

@@ -166,18 +166,18 @@ The extension hooks into pi's lifecycle:
166166

167167
| Hook | What it does |
168168
|------|-------------|
169-
| `before_agent_start` | Injects context management primer + live ledger listing into system prompt |
169+
| `before_agent_start` | Injects context management primer + live notebook index into system prompt |
170170
| `context` | Injects advisory watchdog reminders when context > 30% |
171-
| `session_start` | Rehydrates ledger from persisted entries; resets on `/new` |
172-
| `turn_end` | Updates TUI indicators (context %, ledger count) |
171+
| `session_start` | Rehydrates notebook pages from persisted entries; resets on `/new` |
172+
| `turn_end` | Updates TUI indicators (context %, notebook count) |
173173
| `agent_end` | Records last context usage percent |
174174
| `session_before_compact` | Consumes pending handoff task and sets it as compaction summary |
175175

176176
All state lives in a single `AgenticodingState` instance:
177177

178178
```typescript
179179
interface AgenticodingState {
180-
ledger: Map<string, string>
180+
notebookPages: Map<string, string>
181181
epoch: number
182182
lastContextPercent: number | null
183183
pendingHandoff: { task, source } | null
@@ -193,7 +193,7 @@ interface AgenticodingState {
193193
<details>
194194
<summary><strong>Deep dive → ARCHITECTURE.md</strong></summary>
195195

196-
See [ARCHITECTURE.md](ARCHITECTURE.md) for full module breakdown, tool schemas, lifecycle wiring, spawn child-session lifecycle, and ledger rehydration algorithm.
196+
See [ARCHITECTURE.md](ARCHITECTURE.md) for full module breakdown, tool schemas, lifecycle wiring, spawn child-session lifecycle, and notebook rehydration algorithm.
197197

198198
</details>
199199

handoff/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function registerHandoffCommand(pi: ExtensionAPI, state: AgenticodingStat
3737
}
3838

3939
pi.sendUserMessage(
40-
`Handoff direction: ${direction}\n\nPrepare a real handoff in the current session and current context. Before calling the handoff tool, capture any reusable state in the ledger if needed. Then complete the picture in a concise but sufficiently detailed handoff brief and call the handoff tool in this turn. Preserve the important knowledge that is still only present in the current context so the next clean context can start well without re-deriving it. Use any structure that makes the next work unambiguous. Include findings, current state, unresolved questions, failed paths worth avoiding, next steps, refs, constraints, and spawn ideas when useful. Reference ledger entries by name when relevant.`,
40+
`Handoff direction: ${direction}\n\nPrepare a handoff in the current session. First, save any durable reusable knowledge to the notebook: findings worth keeping, constraints discovered, decisions made, or other grounding future contexts will need. Then draft a concise but sufficiently detailed handoff brief capturing only the remaining situational context: current state, blockers, unresolved questions, failed paths worth avoiding, and next steps. The next context will read the notebook on demand, so do not duplicate notebook content in the brief. Use any structure that makes the next work unambiguous. Reference notebook pages by name when relevant.`,
4141
ctx.isIdle() ? undefined : { deliverAs: "followUp" },
4242
);
4343
},

system-prompt.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Context management system prompt primer.
33
*
44
* Injected via before_agent_start into the system prompt.
5-
* Teaches the LLM about spawn, ledger, and handoff primitives.
5+
* Teaches the LLM about spawn, notebook, and handoff primitives.
66
*/
77

88
export const CONTEXT_PRIMER = `
@@ -24,36 +24,54 @@ Delegate isolated work to child agents. They are trusted extensions of you,
2424
with their own context and the same authority. You receive only condensed
2525
results. Parent context stays at orchestration level. Siblings run in parallel.
2626
27-
### Ledger — sparse continuity cache
28-
Continuously maintain the ledger while you work. After meaningful reads,
29-
research, analysis, decisions, or milestones, either update/refine a ledger
30-
entry now or consciously skip because nothing reusable was learned. Prefer
31-
refining existing entries over creating many tiny ones. The current ledger
32-
listing is available above. Reference entries by name; fetch via ledger_get on
33-
demand. Never pre-load bodies.
27+
### Notebook — durable cross-context grounding
28+
Treat the notebook as durable grounding for future contexts. Each page covers
29+
one subject, thread, or subsystem. Prefer refining a few living pages organized
30+
by subject rather than workflow phase. Store only reusable knowledge worth
31+
carrying across resets: verified facts, architecture learned, decisions and
32+
rationale, constraints, expensive discoveries, and durable open questions.
3433
35-
### Handoff — complete the picture, then continue cleanly
34+
Treat notebook_index as the notebook index. Scan it at task start, after handoff,
35+
before replanning, or when stuck. Use notebook_read to open only relevant pages.
36+
Use them to ground a fresh context, avoid repeated work, and resume a subject
37+
quickly. Verify stale notes before relying on them. Avoid raw transcripts, logs,
38+
or large tool output. Reference pages by name; fetch on demand; never pre-load
39+
bodies.
40+
41+
### Active notebook topic — current semantic frame
42+
The active notebook topic names the current high-level frame for this session.
43+
If the current work still fits that topic, prefer spawn for isolated noisy
44+
subtasks so the parent stays focused. If the work no longer fits that topic,
45+
prefer handoff over dragging stale context forward. After handoff, assign a
46+
fresh topic again in the next context.
47+
48+
### Handoff — distilled next task
3649
When the job changes, or when context is noisy past the ~30% heuristic, use
3750
handoff to finish extracting what matters from the current context before the
38-
cut. Save reusable state to the ledger when useful, then draft a handoff brief
39-
that preserves the important knowledge still missing from the ledger.
40-
Handoff compacts the active session around that brief so the next turn starts
41-
in a clean context with the right picture already in view. Full history remains
42-
in the session file for the user.
51+
cut. Save durable reusable knowledge to the notebook first, then draft a
52+
handoff brief that carries only the situational context still missing: current
53+
state, blockers, unresolved questions, failed paths worth avoiding, and next
54+
steps. Handoff compacts the active session around that brief so the next turn
55+
starts in a clean context with the right direction already in view. Full history
56+
remains in the session file for the user.
4357
44-
Use any structure that keeps the next work unambiguous. Include the current
45-
state, important findings, unresolved questions, failed paths worth avoiding,
46-
next steps, refs, constraints, and spawn ideas when useful. The handoff should
47-
help the next context start well without re-deriving what you already learned.
58+
The next context should use the notebook for grounding and the handoff brief
59+
for direction. Reference notebook pages by name; do not duplicate their content
60+
in the brief. The handoff should help the next context start well without
61+
re-deriving what you already learned.
4862
4963
### Rules
50-
- Maintain the ledger as a constant working process; do not wait for handoff
51-
- Cache reusable state in the ledger; handoff should also capture the missing context that has not been recorded yet
52-
- Prefer refining existing entries over creating many tiny ones
53-
- After meaningful work, either update/refine the ledger or intentionally skip
54-
- Reference ledger entries by name when useful; fetch bodies on demand
64+
- Maintain the notebook deliberately; update it when you learn durable knowledge worth carrying across contexts
65+
- One page = one subject, thread, or subsystem
66+
- Prefer subject pages over workflow-phase pages
67+
- Use notebook_index as the index before starting, resuming, or replanning
68+
- Use notebook_read to open only relevant pages
69+
- Keep pages compact; avoid raw dumps, repeated tool output, scratch reasoning, and local task state
70+
- Use compact sections such as Facts / Architecture / Decisions / Constraints / Open questions when helpful
71+
- Separate facts, guesses, and decisions when useful
5572
- Use spawn to delegate isolated subtasks when it helps; parent orchestrates and merges results
73+
- Treat the active notebook topic as the current semantic frame: same topic → spawn bias, different topic → handoff bias
5674
- Call handoff at job boundaries: research→execution, planning→execution
57-
- Past ~30%, consider handoff when the phase is done or context noise is hurting focus
58-
- After handoff, ledger_get entries as needed — not all at once
75+
- Use handoff to pass the distilled next task and immediate starting state
76+
- After handoff, fetch only the pages you need and assign a fresh topic again
5977
`.trim();

0 commit comments

Comments
 (0)