1 -#
prune — Lossless Conversation Compactor 3 -> Trim conversation to essentials. Search everything.
7 -`prune` is a deterministic, lossless compaction extension for pi.
8 -It replaces the default LLM-based summarization with algorithmic extraction that
11 -1. **Brief transcript** — chronological flow with tool calls collapsed to
13 -2. **Key facts** — extracted goals, files, blockers, preferences
14 -3. **Lossless recall** — raw messages stay searchable via JSONL + `prune_recall`
16 -**Note on "lossless":** prune preserves original JSONL at the system level.
17 -Lossless refers to data integrity, not instant recall.
18 -The `prune_recall` tool (implemented as part of prune) provides programmatic
24 -┌─────────────────────────────────────────────────────────────┐
25 -│ session_before_compact │
28 -│ normalize(messages) → blocks │
31 -│ extract(blocks) → ExtractOutput │
34 -│ format(fresh ExtractOutput) → freshSummary │
37 -│ merge(previousSummary, freshSummary) → finalSummary │
38 -└─────────────────────────────────────────────────────────────┘
46 -- Also: update token refresh logic
49 -- Modified: session.ts, auth.test.ts
50 -- Created: middleware.ts
52 -[Outstanding Context]
53 -- lint check still failing on line 42
56 -- Prefer TypeScript strict mode
57 -- Never commit with debug logs
60 -- abc1234: Fix token refresh
63 -- Current request: Fix the auth bug (#5)
64 -- Last action: edit session.ts (#9)
65 -- Next: continue current request; use prune_recall before redoing old work.
68 -[user] Fix the auth bug (#5)
69 -[assistant] Root cause found
70 -* bash "bun test" (#7)
71 -* edit "session.ts" (#9)
72 -...(N earlier lines omitted)
74 -───────────────────────────────────────────────────────────────
76 - prune: 309 entries summarized; before 18.9k tok; raw kept 67 entries; split-prefix 0
78 -Use prune_recall to search for prior work, decisions, and context from before this summary.
79 -Do not redo work already completed.
84 -| Section | Description | Merge Policy |
85 -| ----------------------- | ---------------------------------------- | ------------------------------------------------------------------------ |
86 -| `[Session Goal]` | Goals + scope changes from user messages | Sticky (accumulate, dedup, cap 8) |
87 -| `[Files And Changes]` | Modified, created, read files | Union by category, dedup, cap 10 each |
88 -| `[Outstanding Context]` | Errors, blockers, pending questions | Fresh (replace) |
89 -| `[User Preferences]` | prefer/never/always patterns | Sticky (accumulate, cap 15) |
90 -| `[Commits]` | Extracted git commits with hashes | Sticky (keep recent 8) |
91 -| `[Continue Now]` | Resume block from latest transcript tail | Fresh (replace) |
92 -| `---` | Brief transcript | Append fresh after previous, then cap tail (older first in final output) |
98 -Map pi messages → blocks:
100 -- `user` — text content from user messages
101 -- `assistant` — text content from assistant messages
102 -- `tool_call` — tool name + args
103 -- `tool_result` — output, mark errors
106 -###
2. Build Sections 110 -Extracted from user blocks.
111 -Algorithm (based on pi-vcc + real session analysis):
119 -GOAL_VERB_RE
= /\
b(scan
|check
|fix
|implement
|add
|create
|build
|refactor
|debug
| 120 -investigate
|update
|remove
|delete|migrate
|deploy
|test
|write
|set up
| 121 -spin
|run
|execute
|verify
|review
|analyze
|explain
|describe
|explore
| 122 -call
|use
|switch
|clean
|lint
|format)\b
/i;
125 -SCOPE_CHANGE_RE
= /\
b(also
|instead
|actually
|change
of plan
|forget that
| 126 -new task
|switch to
|now I want
|pivot
|let
'?s do|stop .* and| 127 -while we
'?re at it|between|for now)\b/i; 130 -NOISE_SHORT_RE
= /^(ok
|yes
|no
|yep
|nope
|go
|hi
|hey
|thanks
?|thx
|k
|y
|n
| 131 -done
|go ahead
|looks good
|sounds good
|lgtm
|sounds right
| 132 -makes sense
|sure
|please
|thank)\s
*[.
!?]
*$
/i;
134 -NON_GOAL_RE
= /^\s
*[\[│├└─╭╰─]
|```|^\s*(=[A-Z_]+\(|function |const |let | 135 -var |import |export |class |async |await |interface |type )| 136 -^(https?:\/\/|file:|\/[a-z])|^\s*```|\s
*```\s*$|^\s*\| | 137 -^\s*For each\b|\bin full\b[^\n]*\b(comments?|issues?|PRs?|linked)\b| 140 -// Template signal (stop collecting at these): 141 -TEMPLATE_SIGNAL_RE = /^\s*(For each\b|Do NOT\b|Analyze and propose\b| 142 -If [Tt]ask|Output:\s*$|here'?s a|ticket:)/i; 145 -// 1. Truncate at template signal 146 -// 2. Filter noise lines 147 -// 3. Filter non-goal patterns 148 -// 4. Collect from first block, scope-change only from subsequent 151 -**Real session edge cases handled:**
154 - "yes", "done", "ok", "noop", "go" → filtered
156 - usually transcript-only; task questions like "can you fix X?" may become goals
158 - filtered via NON_GOAL_RE
161 - also do Y." → captures scope change via "also"
162 -- Command templates:
163 - stopped at TEMPLATE_SIGNAL
173 -extractFileFromArgs(toolName, args) → { modified, created, read }
174 -extractCommits(blocks) → [{ hash, message },
...]
182 -BLOCKER_RE
= /\
b(fail
|broken
|cannot
|can
't|won't work
|blocked
|blocker
| 183 -not (fixed
|resolved
|working)
|crash(es
|ed
|ing)
?|still (broken
|failing
|wrong))\b
/i;
188 -**Note:** Context is extracted fresh on each compaction, not merged.
189 -Old blockers are discarded when resolved.
197 - /\bprefer(?:s|red|ring)?\s+\w/i,
198 - /\bdon'?t want\b/i,
199 - /\balways (?:use|do|run|prefer|keep|make|format|write|add|set|put|prefix|start|include|append)\b/i,
200 - /\bnever (?:use|do|run|push|commit|write|ignore|add|set|put|remove|delete|include|deploy)\b/i,
201 - /\bplease (?:use|avoid|keep|make|don'?t|do not|format|write)\b/i,
202 - /\b(?:style|format|language|naming)\s*[:=]\s*\S/i,
212 -**Known limitations (false positive risk):**
214 -The preference extraction regex cannot distinguish user-authored instructions
217 -1. **Quoted tool output** — agent-generated examples copied into a user message
218 -2. **Pasted content** — user pasting code, logs, or external text
219 -3. **Previous compaction output** — quoted compaction summaries in new messages
220 -4. **Assistant suggestions** — content the assistant generated that user is
223 -**Mitigation attempts:**
225 -- Stricter position requirement (first 200 chars) — catches direct pastes but
227 -- Line length cap (200 chars) — filters some noise
228 -- Question filter — avoids interrogative patterns
230 -**200-char heuristic analysis (from real sessions):**
232 -- User messages in real sessions average <500 chars
233 -- Pasted content (skill files, code blocks) averages 2000+ chars
234 -- The heuristic catches most direct preference pastes
235 -- It does NOT catch content extracted incorrectly from tool output or prior
238 -**False positive example:**
241 -"POV: You Angered the Wrong Cat 😈🐱 (POV format = engagement)"
244 -This was in a compaction output and not found in user-authored messages scanned
247 -generated/test content became part of prior summary or quoted context, then
248 -preference regex preserved it.
249 -User later quoted it while reporting the false positive.
251 -**Acceptable tradeoff:** Preferences are hints, not facts.
252 -False positives in preferences are noisy but not harmful to compaction
255 -###
3. Build Transcript 275 -merge(previousSummary: string
| undefined, freshSummary: string): string {
287 -Implemented as part of prune.
288 -Two surfaces share one search implementation:
290 -1. `prune_recall` — registered tool, exposed to the LLM only after a prune
291 - compaction exists in the current branch
292 -2. `/prune-recall` — command for the human, always available
297 - scope:
'lineage' | 'all',
303 -Searches session JSONL for matching entries.
304 -Returns structured results with context.
308 -- `#N` = 1-based `sourceIndex` assigned during `normalize()` from current branch
310 -- Transcript refs and `expand` use same `#N`
312 - 'lineage'` searches current branch/lineage and preserves `#N` mapping
314 - 'all'` searches whole session file, including off-lineage branches; results
315 - include both `#N` when available and entry id
316 -- Refs are stable while branch history is append-only; export/import or tree
317 - navigation can change ordinal mapping
322 -prune: 309 entries summarized; before 18.9k tok; raw kept 67 entries; split-prefix 0
325 -- `entries summarized` = messages passed to deterministic extraction, including
326 - old raw-tail entries moved into summary
327 -- `before N tok` = compaction event `tokensBefore`, if available
328 -- `raw kept` = branch entries kept after returned `firstKeptEntryId`
329 -- `split-prefix` = `turnPrefixMessages.length`
333 -| Command | Description |
334 -| --------------------------- | ------------------------- |
335 -| `/prune` | Manual compaction trigger |
336 -| `/prune-recall <query>` | Search current lineage |
337 -| `/prune-recall <query> all` | Search all branches |
345 - "transcriptLimit":
60,
346 - "toolCapPerTurn":
8,
354 -###
Default tests (`bun test`) 356 -1. **Unit tests** — normalize(), extract(), format(), merge(), recall() in
358 -2. **Golden/edge tests** — minimized real-session messages and false positives
359 -3. **Harness tests** — `@marcfargas/pi-test-harness` verifies extension
360 - loading/hooks with mocked model
362 -Harness isolation rule:
365 -- pass absolute prune extension path
366 -- do not use repo cwd as harness cwd
367 -- assert only prune extension loaded
369 -###
Real MiniMax integration (`PRUNE_REAL=1 bun run test:real`) 371 -Use pi SDK or CLI, not test-harness.
372 -Test-harness replaces the model with playbook.
374 -Real integration must isolate resources:
380 - true` / CLI `--no-extensions`
381 -- only prune extension loaded
382 -- MiniMax provider only
383 -- tiny compaction thresholds to force compaction
385 -Assertions are structural, not exact model text:
387 -- compaction starts/ends
388 -- summary contains `[Session Goal]`, `---`, `Use prune_recall`
389 -- summary does not contain `vcc_recall`
390 -- `prune_recall` can find older raw message
401 -##
Comparison to pi-vcc 403 -| Aspect | pi-vcc | prune |
404 -| --------------------- | ------------------------------------- | ------------------------- |
405 -| Files | 15+ | ~8 |
406 -| Extractors | goals, files, prefs, commits, context | same |
407 -| Merge | header-based text parsing | simpler tested text merge |
408 -| Test coverage | minimal | comprehensive |
409 -| Real session analysis | none | yes |
410 -| Output metadata | yes | yes (same) |
411 -| prune_recall | external tool | built-in |
415 -- [x] Core extract functions (normalize, extract)
416 -- [x] merge() implementation
417 -- [x] format() implementation
418 -- [x] prune_recall tool implementation
419 -- [x] session_before_compact hook
420 -- [x] /prune command
422 -- [x] Integration tests
423 -- [x] Settings support