@pi-unipi/compactor 2.0.3 → 2.0.4
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/README.md +290 -73
- package/package.json +4 -1
- package/skills/compactor/SKILL.md +2 -3
- package/skills/compactor-detail/SKILL.md +49 -64
- package/skills/compactor-doctor/SKILL.md +28 -31
- package/skills/compactor-stats/SKILL.md +22 -20
- package/src/commands/index.ts +4 -1
- package/src/compaction/auto-trigger.ts +306 -0
- package/src/config/manager.ts +1 -0
- package/src/config/presets.ts +26 -0
- package/src/config/schema.ts +7 -0
- package/src/index.ts +74 -1
- package/src/tools/context-budget.ts +18 -2
- package/src/tools/register.ts +19 -11
- package/src/tui/settings-overlay.ts +142 -3
- package/src/types.ts +17 -0
package/README.md
CHANGED
|
@@ -1,101 +1,318 @@
|
|
|
1
1
|
# @pi-unipi/compactor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Deterministic context management for Pi/UniPi. The compactor keeps long coding sessions usable by replacing large conversation history with a structured, zero-LLM summary, while preserving session recall, continuity snapshots, stats, sandbox helpers, and safer tool display.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## What it does
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- **Zero-LLM compaction:** Pi asks for compaction; UniPi builds the summary locally instead of spending model tokens on a summarization call.
|
|
8
|
+
- **Session continuity:** important goals, changed files, commits, blockers, preferences, and a recent transcript survive compaction.
|
|
9
|
+
- **Optional percentage trigger:** UniPi can compact at a configured context percentage before Pi's fixed reserve-token safety net fires.
|
|
10
|
+
- **Recall:** agents can search the append-only session branch, including messages that no longer fit in live LLM context.
|
|
11
|
+
- **Runtime helpers:** sandbox execution, context budget checks, diagnostics, and stats.
|
|
12
|
+
- **Display guards:** tool output/diff rendering is clamped and summarized to avoid noisy or terminal-breaking output.
|
|
8
13
|
|
|
9
|
-
|
|
10
|
-
|---------|-------------|
|
|
11
|
-
| `/unipi:lossless-compact` | Immediate zero-LLM compaction with structured summary |
|
|
12
|
-
| `/unipi:session-recall` | Search session history (BM25 or regex) |
|
|
13
|
-
| `/unipi:content-index` | Index current project into FTS5 |
|
|
14
|
-
| `/unipi:content-search` | Search indexed content |
|
|
15
|
-
| `/unipi:content-purge` | Wipe all indexed content |
|
|
16
|
-
| `/unipi:compact-stats` | Context savings dashboard |
|
|
17
|
-
| `/unipi:compact-doctor` | Run diagnostics |
|
|
18
|
-
| `/unipi:compact-settings` | TUI settings overlay |
|
|
19
|
-
| `/unipi:compact-preset <name>` | Apply quick preset |
|
|
20
|
-
| `/unipi:compact-help` | Show detailed documentation |
|
|
14
|
+
The compaction pipeline is deterministic and runs locally. It does not call an LLM during compaction.
|
|
21
15
|
|
|
22
|
-
|
|
16
|
+
---
|
|
23
17
|
|
|
24
|
-
##
|
|
18
|
+
## Quick start
|
|
25
19
|
|
|
26
|
-
|
|
20
|
+
```text
|
|
21
|
+
/unipi:compact-settings
|
|
22
|
+
```
|
|
27
23
|
|
|
28
|
-
|
|
24
|
+
1. Choose a preset in **Presets**.
|
|
25
|
+
2. Optionally enable **Auto → Percentage Trigger**.
|
|
26
|
+
3. Press **Enter** to save.
|
|
27
|
+
4. Use `/unipi:lossless-compact` when you want to compact immediately.
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
Config files:
|
|
31
30
|
|
|
32
|
-
|
|
|
33
|
-
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
-
| `sandbox` | sandbox | Run code in sandbox (11 languages) |
|
|
37
|
-
| `sandbox_file` | sandbox | Execute file via FILE_CONTENT |
|
|
38
|
-
| `sandbox_batch` | sandbox | Atomic batch of commands + searches |
|
|
39
|
-
| `content_index` | content | Chunk content to FTS5 index |
|
|
40
|
-
| `content_search` | content | Query indexed content |
|
|
41
|
-
| `content_fetch` | content | Fetch URL and index |
|
|
42
|
-
| `compactor_stats` | compactor | Context savings dashboard |
|
|
43
|
-
| `compactor_doctor` | compactor | Diagnostics checklist |
|
|
44
|
-
| `context_budget` | compactor | Estimate remaining context window |
|
|
31
|
+
| Scope | Path | Notes |
|
|
32
|
+
|---|---|---|
|
|
33
|
+
| Global | `~/.unipi/config/compactor/config.json` | Used by default |
|
|
34
|
+
| Per project | `<project>/.unipi/config/compactor.json` | Enabled from settings with **Project Override** |
|
|
45
35
|
|
|
46
|
-
|
|
36
|
+
Per-project config is merged over global config when the current project directory is known.
|
|
47
37
|
|
|
48
|
-
|
|
49
|
-
- **Tier 2** (`compactor-detail`): On-demand. Full tool reference, anti-patterns, sandbox languages, FTS5 modes, workflows.
|
|
38
|
+
---
|
|
50
39
|
|
|
51
|
-
##
|
|
40
|
+
## User-facing slash commands
|
|
52
41
|
|
|
53
|
-
|
|
42
|
+
| Command | What it does | Notes |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `/unipi:lossless-compact` | Immediately calls Pi compaction with UniPi's zero-LLM sentinel. | Main manual compaction command. |
|
|
45
|
+
| `/unipi:compact` | Deprecated alias for `/unipi:lossless-compact`. | Kept for backward compatibility. |
|
|
46
|
+
| `/unipi:session-recall <query>` | Searches current session history with BM25/regex recall. | Uses the append-only session branch when available, so pre-compaction messages can still be found. |
|
|
47
|
+
| `/unipi:compact-recall <query>` | Deprecated alias for `/unipi:session-recall`. | Kept for backward compatibility. |
|
|
48
|
+
| `/unipi:compact-stats` | Shows session events, compaction count, token savings, sandbox runs, and search counts. | Stats are DB-backed with runtime fallbacks. |
|
|
49
|
+
| `/unipi:compact-doctor` | Runs diagnostics for config, SQLite/session DB, and runtimes. | Useful when stats/recall/sandbox look broken. |
|
|
50
|
+
| `/unipi:compact-settings` | Opens the TUI settings overlay. | Tabs: Presets, Strategies, Auto, Pipeline. |
|
|
51
|
+
| `/unipi:compact-preset <name>` | Applies a preset globally. | Names: `precise`, `balanced`, `thorough`, `lean`. Old names map to new ones: `opencode→precise`, `verbose→thorough`, `minimal→lean`. |
|
|
52
|
+
| `/unipi:compact-help` | Shows compact help inside Pi. | Quick command reference. |
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
Content/project indexing is handled by `@pi-unipi/cocoindex`, not this package. Use `/unipi:cocoindex-init`, `/unipi:cocoindex-update`, and `cocoindex_search` for indexed project search.
|
|
56
55
|
|
|
57
|
-
|
|
58
|
-
|--------|-------------|
|
|
59
|
-
| `precise` | Code-heavy, minimal waste — compaction: full, pipeline: 2/6 on |
|
|
60
|
-
| `balanced` | Daily use (default) — all strategies moderate, pipeline: all on |
|
|
61
|
-
| `thorough` | Debug/audit — everything on, full transcript |
|
|
62
|
-
| `lean` | Quick fixes — compaction only, pipeline: all off |
|
|
56
|
+
---
|
|
63
57
|
|
|
64
|
-
|
|
58
|
+
## User-facing settings
|
|
65
59
|
|
|
66
|
-
|
|
60
|
+
Open settings with `/unipi:compact-settings`.
|
|
67
61
|
|
|
68
|
-
|
|
69
|
-
|---------|-------------|---------|
|
|
70
|
-
| TTL Cache | Cache with time-based expiry | On Compaction |
|
|
71
|
-
| Auto Injection | Inject behavioral state after compaction | On Compaction |
|
|
72
|
-
| MMap Pragma | Use mmap for SQLite I/O | On Compaction |
|
|
73
|
-
| Proximity Reranking | Rerank search results by proximity | On Search |
|
|
74
|
-
| Timeline Sort | Sort session events chronologically | On Search |
|
|
75
|
-
| Progressive Throttling | Slow down indexing for large projects | On Index |
|
|
62
|
+
Keyboard:
|
|
76
63
|
|
|
77
|
-
|
|
64
|
+
| Key | Action |
|
|
65
|
+
|---|---|
|
|
66
|
+
| `Tab` | Switch tabs |
|
|
67
|
+
| `↑/↓` | Move selection |
|
|
68
|
+
| `Space` | Cycle/toggle selected value |
|
|
69
|
+
| `/` | Search inside the Strategies tab |
|
|
70
|
+
| `Enter` | Save and close |
|
|
71
|
+
| `Esc` | Cancel |
|
|
78
72
|
|
|
79
|
-
|
|
80
|
-
- `/` key opens search filter in Strategies tab
|
|
81
|
-
- Preset selection shows 3-line preview
|
|
82
|
-
- Per-project override checkbox (`o` key)
|
|
83
|
-
- Keyboard: left/right cycle modes, Space toggle, `s` save, Esc cancel
|
|
73
|
+
### Presets tab
|
|
84
74
|
|
|
85
|
-
|
|
75
|
+
Presets are profiles that set strategies, pipeline switches, sandbox mode, and display mode.
|
|
86
76
|
|
|
77
|
+
| Preset | Intended use | Important effects |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| `precise` | Code-heavy work, minimal waste | Full compaction sections, safe-only sandbox, opencode display, Pipeline: `ttlCache` + `mmapPragma` on. |
|
|
80
|
+
| `balanced` | Daily/default profile | Compact transcript, balanced display, FTS mode auto, sandbox all, Pipeline: all six switches on. |
|
|
81
|
+
| `thorough` | Debug/audit sessions | Full transcript, verbose display, sandbox all, Pipeline: all six switches on. |
|
|
82
|
+
| `lean` | Quick fixes or short sessions | Brief/minimal compaction sections, no sandbox, no session continuity, Pipeline: all off. |
|
|
83
|
+
|
|
84
|
+
The **Project Override** row controls where settings are saved:
|
|
85
|
+
|
|
86
|
+
- `disabled` → save global config.
|
|
87
|
+
- `enabled` → save `<project>/.unipi/config/compactor.json` for only this project.
|
|
88
|
+
|
|
89
|
+
### Strategies tab
|
|
90
|
+
|
|
91
|
+
These settings control what is extracted into compaction summaries or how related compactor features behave.
|
|
92
|
+
|
|
93
|
+
| Setting | Values | Meaning |
|
|
94
|
+
|---|---|---|
|
|
95
|
+
| `Verbose Debug` | `on`, `off` | Enables internal debug state. Console debug output is intentionally muted to avoid TUI rendering issues. |
|
|
96
|
+
| `Session Goals` | `full`, `brief`, `off` | Extracts the user's active goals/request history into `[Session Goal]`. |
|
|
97
|
+
| `Files & Changes` | `all`, `modified-only`, `off` | Tracks read, created, edited, and written files in `[Files And Changes]`. |
|
|
98
|
+
| `Commits` | `full`, `brief`, `off` | Captures git commit hashes/messages detected in the conversation. |
|
|
99
|
+
| `Outstanding Context` | `full`, `critical-only`, `off` | Keeps blockers, TODOs, pending decisions, and follow-ups. |
|
|
100
|
+
| `User Preferences` | `all`, `recent-only`, `off` | Preserves user preferences learned during the session. |
|
|
101
|
+
| `Brief Transcript` | `full`, `compact`, `minimal`, `off` | Keeps a rolling recent transcript after the structured sections. |
|
|
102
|
+
| `Session Continuity` | `full`, `essential-only`, `off` | Controls XML-style resume snapshots that help the next turn continue after compaction. |
|
|
103
|
+
| `Sandbox Execution` | `all`, `safe-only`, `off` | Controls agent-facing sandbox availability/intent. Safe-only is intended for less risky execution. |
|
|
104
|
+
| `Tool Display` | `opencode`, `balanced`, `verbose`, `custom` | Controls built-in tool output rendering style and diff display behavior. |
|
|
105
|
+
|
|
106
|
+
Config-only legacy field: `fts5Index` remains in the schema for compatibility, but project indexing has moved to `@pi-unipi/cocoindex`.
|
|
107
|
+
|
|
108
|
+
### Auto tab
|
|
109
|
+
|
|
110
|
+
These settings control UniPi-managed percentage auto-compaction. They are separate from Pi core's own `compaction.reserveTokens` behavior.
|
|
111
|
+
|
|
112
|
+
| Setting | Default | Meaning |
|
|
113
|
+
|---|---:|---|
|
|
114
|
+
| `Percentage Trigger` | `off` | When on, UniPi checks context usage at `turn_end` and can call compaction when the threshold is reached. Disabled by default for backward compatibility. |
|
|
115
|
+
| `Threshold` | `80%` | Trigger point using Pi's live `ctx.getContextUsage().percent`. |
|
|
116
|
+
| `Cooldown` | `60s` | Minimum time between UniPi-triggered compaction attempts. Prevents loops. |
|
|
117
|
+
| `Repeat Growth` | `4k` | If usage is still above threshold after compaction, require this many new tokens before triggering again. |
|
|
118
|
+
| `Notifications` | `on` | Shows user-visible notifications for UniPi-triggered compaction attempts/results. |
|
|
119
|
+
|
|
120
|
+
Example config:
|
|
121
|
+
|
|
122
|
+
```json
|
|
123
|
+
{
|
|
124
|
+
"autoCompaction": {
|
|
125
|
+
"enabled": true,
|
|
126
|
+
"thresholdPercent": 80,
|
|
127
|
+
"cooldownMs": 60000,
|
|
128
|
+
"repeatMinGrowthTokens": 4000,
|
|
129
|
+
"notify": true
|
|
130
|
+
}
|
|
131
|
+
}
|
|
87
132
|
```
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
133
|
+
|
|
134
|
+
### Pipeline tab
|
|
135
|
+
|
|
136
|
+
Pipeline switches are low-level feature flags used by presets and advanced users.
|
|
137
|
+
|
|
138
|
+
| Setting | Preset behavior | Current effect |
|
|
139
|
+
|---|---|---|
|
|
140
|
+
| `TTL Cache` | On in `precise`, `balanced`, `thorough`; off in `lean` | Reserved/compatibility switch for cache behavior. Search has an internal cache, but this toggle is not currently required for it. |
|
|
141
|
+
| `Auto Injection` | On in `balanced`, `thorough`; off in `precise`, `lean` | Active. Adds behavioral/session state to the resume snapshot after compaction. |
|
|
142
|
+
| `MMap Pragma` | On in `precise`, `balanced`, `thorough`; off in `lean` | Reserved/compatibility switch for SQLite mmap tuning. |
|
|
143
|
+
| `Proximity Reranking` | On in `balanced`, `thorough`; off in `precise`, `lean` | Reserved/compatibility switch for future recall/search ranking. |
|
|
144
|
+
| `Timeline Sort` | On in `balanced`, `thorough`; off in `precise`, `lean` | Reserved/compatibility switch for future chronological recall sorting. |
|
|
145
|
+
| `Progressive Throttling` | On in `balanced`, `thorough`; off in `precise`, `lean` | Reserved/compatibility switch for future large-project throttling. |
|
|
146
|
+
|
|
147
|
+
If all Pipeline values stay `off` after choosing a preset, that means the running version is not applying preset pipeline values correctly. Choose a preset, press **Enter** to save, and verify you are on a version containing the preset pipeline fix.
|
|
148
|
+
|
|
149
|
+
Config-only pipeline field:
|
|
150
|
+
|
|
151
|
+
| Field | Meaning |
|
|
152
|
+
|---|---|
|
|
153
|
+
| `customNoisePatterns` | Extra regex/string patterns used by session recall filtering to remove noisy user blocks. |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## What the agent gets
|
|
158
|
+
|
|
159
|
+
### Agent tools
|
|
160
|
+
|
|
161
|
+
The extension registers tools during `session_start` after the session DB is initialized.
|
|
162
|
+
|
|
163
|
+
| Tool | Purpose |
|
|
164
|
+
|---|---|
|
|
165
|
+
| `compact` | Agent-facing compaction request; supports `dryRun: true` preview. Slash command `/unipi:lossless-compact` is the user-facing immediate compaction path. |
|
|
166
|
+
| `session_recall` | Search current session history with BM25 or regex. |
|
|
167
|
+
| `vcc_recall` | Deprecated alias for `session_recall`. |
|
|
168
|
+
| `sandbox` | Run code in a sandboxed environment. Languages: JavaScript, TypeScript, Python, shell, Ruby, Go, Rust, PHP, Perl, R, Elixir. |
|
|
169
|
+
| `sandbox_file` | Execute a file with its content injected as `FILE_CONTENT`. |
|
|
170
|
+
| `sandbox_batch` | Run multiple sandbox execution items in one atomic response. |
|
|
171
|
+
| `ctx_execute` | Deprecated alias for `sandbox`. |
|
|
172
|
+
| `ctx_execute_file` | Deprecated alias for `sandbox_file`. |
|
|
173
|
+
| `ctx_batch_execute` | Deprecated alias for `sandbox_batch`. |
|
|
174
|
+
| `compactor_stats` | Agent-readable stats dashboard. |
|
|
175
|
+
| `ctx_stats` | Deprecated alias for `compactor_stats`. |
|
|
176
|
+
| `compactor_doctor` | Agent-readable diagnostics. |
|
|
177
|
+
| `ctx_doctor` | Deprecated alias for `compactor_doctor`. |
|
|
178
|
+
| `context_budget` | Uses live Pi context usage when available and returns compaction guidance. |
|
|
179
|
+
|
|
180
|
+
### Agent skills
|
|
181
|
+
|
|
182
|
+
| Skill | Purpose |
|
|
183
|
+
|---|---|
|
|
184
|
+
| `compactor` | Small always-loadable routing skill: when to compact, recall, check budget, or use sandbox. |
|
|
185
|
+
| `compactor-detail` | On-demand full tool reference and workflow guidance. |
|
|
186
|
+
| `compactor-stats` | Help interpreting compactor stats. |
|
|
187
|
+
| `compactor-doctor` | Help diagnosing config/DB/runtime issues. |
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## How the system works
|
|
192
|
+
|
|
193
|
+
### Chronological hook flow
|
|
194
|
+
|
|
195
|
+
Typical session order:
|
|
196
|
+
|
|
197
|
+
1. **Extension load**
|
|
198
|
+
- Registers compaction hooks immediately with lazy dependencies.
|
|
199
|
+
- Creates in-memory runtime counters/state.
|
|
200
|
+
|
|
201
|
+
2. **`session_start`**
|
|
202
|
+
- Scaffolds config if needed.
|
|
203
|
+
- Initializes `SessionDB` and sandbox executor.
|
|
204
|
+
- Computes the current session ID, including worktree suffix.
|
|
205
|
+
- Registers agent tools and slash commands.
|
|
206
|
+
- Registers the info-screen Compactor group.
|
|
207
|
+
- Emits UniPi `MODULE_READY`.
|
|
208
|
+
|
|
209
|
+
3. **`before_agent_start`**
|
|
210
|
+
- Reloads config for the active project.
|
|
211
|
+
- Applies runtime `autoDetect` behavior, such as disabling commit extraction outside git repos.
|
|
212
|
+
- Rebuilds cached recall blocks from Pi's append-only session branch.
|
|
213
|
+
- If a resume snapshot exists from a prior compaction, injects it into session context and marks it consumed.
|
|
214
|
+
|
|
215
|
+
4. **`input`**
|
|
216
|
+
- Performs advisory security checks for shell/network/file patterns.
|
|
217
|
+
- Can block some known-dangerous network shell commands.
|
|
218
|
+
|
|
219
|
+
5. **Tool execution → `tool_result`**
|
|
220
|
+
- Extracts file/change/search/sandbox events into the session DB.
|
|
221
|
+
- Updates runtime byte/tool counters.
|
|
222
|
+
- Applies display overrides and width-safe diff clamping before output reaches the TUI.
|
|
223
|
+
|
|
224
|
+
6. **`turn_end`**
|
|
225
|
+
- If percentage auto-compaction is enabled, reads `ctx.getContextUsage()`.
|
|
226
|
+
- Runs cooldown/repeat-growth safeguards.
|
|
227
|
+
- Calls `ctx.compact({ customInstructions: COMPACTOR_INSTRUCTION })` when eligible.
|
|
228
|
+
|
|
229
|
+
7. **Pi decides to compact**
|
|
230
|
+
- This can be from `/unipi:lossless-compact`, UniPi percentage auto-compaction, or Pi core's reserve-token fallback.
|
|
231
|
+
|
|
232
|
+
8. **`session_before_compact`**
|
|
233
|
+
- Pi provides `preparation`, `branchEntries`, and optional custom instructions.
|
|
234
|
+
- If compactor should handle it, UniPi:
|
|
235
|
+
1. chooses the safe cut point (`buildOwnCut`),
|
|
236
|
+
2. converts messages to LLM-like input,
|
|
237
|
+
3. runs the zero-LLM compile pipeline,
|
|
238
|
+
4. persists stats/snapshots,
|
|
239
|
+
5. returns `{ compaction: { summary, details, tokensBefore, firstKeptEntryId } }` to Pi.
|
|
240
|
+
|
|
241
|
+
9. **`session_compact`**
|
|
242
|
+
- Pi commits the compaction entry.
|
|
243
|
+
- UniPi increments compaction counters and persists token/character savings.
|
|
244
|
+
|
|
245
|
+
10. **Next `before_agent_start` after compaction**
|
|
246
|
+
- UniPi injects the resume snapshot so the agent gets continuity without needing the full old transcript in context.
|
|
247
|
+
|
|
248
|
+
11. **`session_shutdown`**
|
|
249
|
+
- Cleans old sessions.
|
|
250
|
+
- Cleans sandbox/background processes.
|
|
251
|
+
- Closes DB handles.
|
|
252
|
+
|
|
253
|
+
### Zero-LLM compile pipeline
|
|
254
|
+
|
|
255
|
+
When UniPi handles `session_before_compact`, it transforms old messages through these deterministic stages:
|
|
256
|
+
|
|
257
|
+
1. **Normalize** — flatten user/assistant/tool/thinking messages into normalized blocks.
|
|
258
|
+
2. **Filter** — remove noise and apply `customNoisePatterns`.
|
|
259
|
+
3. **Build sections** — extract goals, files, commits, outstanding context, preferences, and transcript entries.
|
|
260
|
+
4. **Brief** — cap and condense transcript lines.
|
|
261
|
+
5. **Format** — emit a structured markdown summary.
|
|
262
|
+
6. **Merge** — merge with the previous summary when Pi provides one.
|
|
263
|
+
|
|
264
|
+
The resulting summary is optimized for continuity, not for perfect archival fidelity. Use `session_recall` when the agent needs details from the raw session branch.
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## Benchmarks
|
|
269
|
+
|
|
270
|
+
Synthetic compile-only benchmark, run on this repository with Node 24 using `compile()` directly. Token counts use the common `chars / 4` estimate. Real compaction also keeps a recent live tail, so final live-context size can be larger than the summary-only number below.
|
|
271
|
+
|
|
272
|
+
| Synthetic session | Input chars | Approx input tokens | Summary chars | Approx summary tokens | Reduction | Compile time |
|
|
273
|
+
|---:|---:|---:|---:|---:|---:|---:|
|
|
274
|
+
| 100 turns / 400 messages | 356,913 | 89,228 | 36,451 | 9,113 | 89.8% | 12.5 ms |
|
|
275
|
+
| 250 turns / 1,000 messages | 894,063 | 223,516 | 36,585 | 9,146 | 95.9% | 16.6 ms |
|
|
276
|
+
| 500 turns / 2,000 messages | 1,789,313 | 447,328 | 36,605 | 9,151 | 98.0% | 35.8 ms |
|
|
277
|
+
|
|
278
|
+
Practical interpretation:
|
|
279
|
+
|
|
280
|
+
- Larger sessions compress better because the structured summary has fixed caps.
|
|
281
|
+
- Runtime is local and usually small relative to model/tool latency.
|
|
282
|
+
- Actual savings reported to users should come from `/unipi:compact-stats`, because Pi supplies real `tokensBefore` during compaction when available.
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
## Troubleshooting
|
|
287
|
+
|
|
288
|
+
### "Whatever preset I choose, Pipeline is always off"
|
|
289
|
+
|
|
290
|
+
That means preset application is not updating `config.pipeline`. The intended behavior is:
|
|
291
|
+
|
|
292
|
+
- `precise`: `ttlCache` + `mmapPragma` on.
|
|
293
|
+
- `balanced`: all six pipeline switches on.
|
|
294
|
+
- `thorough`: all six pipeline switches on.
|
|
295
|
+
- `lean`: all pipeline switches off.
|
|
296
|
+
|
|
297
|
+
Make sure you are running a version with the preset pipeline fix, select the preset, and press **Enter** to save.
|
|
298
|
+
|
|
299
|
+
### "Compaction did not happen"
|
|
300
|
+
|
|
301
|
+
- For immediate user-triggered compaction, use `/unipi:lossless-compact`.
|
|
302
|
+
- For automatic percentage compaction, enable **Auto → Percentage Trigger**.
|
|
303
|
+
- Pi core still has its own reserve-token fallback; UniPi's percentage trigger is optional and disabled by default.
|
|
304
|
+
|
|
305
|
+
### "Stats show zero"
|
|
306
|
+
|
|
307
|
+
- Stats only increase after compaction or after tracked sandbox/search events.
|
|
308
|
+
- Run `/unipi:compact-doctor` to check DB/config health.
|
|
309
|
+
- Long sessions may show all-time DB fallbacks if current in-memory counters are zero.
|
|
310
|
+
|
|
311
|
+
### "Recall cannot find something"
|
|
312
|
+
|
|
313
|
+
- Use specific terms with `/unipi:session-recall <query>`.
|
|
314
|
+
- The recall path prefers Pi's append-only branch and falls back to cached normalized blocks.
|
|
315
|
+
- Extra noise filtering can be added with `pipeline.customNoisePatterns`.
|
|
99
316
|
|
|
100
317
|
## License
|
|
101
318
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-unipi/compactor",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Context engine for Pi — zero-LLM compaction, session continuity, sandbox execution, and tool display optimization",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"sandbox",
|
|
21
21
|
"fts5"
|
|
22
22
|
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "bun test tests"
|
|
25
|
+
},
|
|
23
26
|
"files": [
|
|
24
27
|
"index.ts",
|
|
25
28
|
"src/**/*.ts",
|
|
@@ -11,9 +11,8 @@ description: Context management — compact session, recall history, run code, s
|
|
|
11
11
|
- `compactor_stats` → check savings. `compactor_doctor` → diagnose.
|
|
12
12
|
|
|
13
13
|
## Finding Past Work
|
|
14
|
-
- `session_recall(query)` → search this session (BM25 or regex).
|
|
15
|
-
- `
|
|
16
|
-
→ Index first: `content_index` or `content_fetch(url)`.
|
|
14
|
+
- `session_recall(query)` → search this session (BM25 or regex), including raw messages that may no longer be in live context after compaction.
|
|
15
|
+
- For indexed project/file search, use the CocoIndex package (`cocoindex_search`) when installed; content indexing no longer lives in compactor.
|
|
17
16
|
|
|
18
17
|
## Running Code
|
|
19
18
|
- `sandbox(lang, code)` → single script. `sandbox_batch(items)` → atomic.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: compactor-detail
|
|
3
|
-
description: Full compactor reference — tool parameters, anti-patterns, sandbox languages,
|
|
3
|
+
description: Full compactor reference — tool parameters, anti-patterns, sandbox languages, context budget, workflows.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Compactor — Full Reference
|
|
@@ -9,70 +9,65 @@ description: Full compactor reference — tool parameters, anti-patterns, sandbo
|
|
|
9
9
|
|
|
10
10
|
### compact
|
|
11
11
|
```
|
|
12
|
-
compact()
|
|
12
|
+
compact(dryRun?: boolean)
|
|
13
13
|
```
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
Agent-facing compaction request. Use `dryRun: true` to preview estimated message reduction without compacting.
|
|
15
|
+
|
|
16
|
+
For user-triggered immediate compaction, prefer the slash command `/unipi:lossless-compact`.
|
|
16
17
|
|
|
17
18
|
### session_recall
|
|
18
19
|
```
|
|
19
20
|
session_recall(query: string, mode?: "bm25" | "regex", limit?: number, offset?: number, expand?: boolean)
|
|
20
21
|
```
|
|
21
|
-
Search session history. BM25 is default
|
|
22
|
+
Search current session history. BM25 is default; regex is fallback. The recall path prefers Pi's append-only session branch so it can find messages that are no longer present in live LLM context after compaction.
|
|
23
|
+
|
|
24
|
+
Deprecated alias: `vcc_recall`.
|
|
22
25
|
|
|
23
26
|
### sandbox
|
|
24
27
|
```
|
|
25
28
|
sandbox(language: string, code: string, timeout?: number)
|
|
26
29
|
```
|
|
27
|
-
Run code in sandboxed
|
|
30
|
+
Run code in a sandboxed environment. Only stdout enters context. 100MB output cap. 30s default timeout.
|
|
31
|
+
|
|
32
|
+
Deprecated alias: `ctx_execute`.
|
|
28
33
|
|
|
29
34
|
### sandbox_file
|
|
30
35
|
```
|
|
31
36
|
sandbox_file(language: string, path: string, timeout?: number)
|
|
32
37
|
```
|
|
33
|
-
Execute file.
|
|
38
|
+
Execute a file. File content is injected as `FILE_CONTENT`.
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
sandbox_batch(items: Array<{type: "execute", language, code} | {type: "search", query}>)
|
|
38
|
-
```
|
|
39
|
-
Atomic batch — all items run, results returned together.
|
|
40
|
+
Deprecated alias: `ctx_execute_file`.
|
|
40
41
|
|
|
41
|
-
###
|
|
42
|
+
### sandbox_batch
|
|
42
43
|
```
|
|
43
|
-
|
|
44
|
+
sandbox_batch(items: Array<{ type: "execute", language: string, code: string, timeout?: number }>)
|
|
44
45
|
```
|
|
45
|
-
|
|
46
|
+
Run multiple sandbox executions and return a single combined result.
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
content_search(query: string, limit?: number, offset?: number)
|
|
50
|
-
```
|
|
51
|
-
Search FTS5 index. Returns ranked results with title, content, source, rank.
|
|
52
|
-
|
|
53
|
-
### content_fetch
|
|
54
|
-
```
|
|
55
|
-
content_fetch(url: string, label?: string, chunkSize?: number)
|
|
56
|
-
```
|
|
57
|
-
Fetch URL → markdown → index. Auto-indexes for later search.
|
|
48
|
+
Deprecated alias: `ctx_batch_execute`.
|
|
58
49
|
|
|
59
50
|
### compactor_stats
|
|
60
51
|
```
|
|
61
52
|
compactor_stats()
|
|
62
53
|
```
|
|
63
|
-
Dashboard: session events, compactions, tokens saved,
|
|
54
|
+
Dashboard: session events, compactions, tokens saved, compression ratio, sandbox runs, and search queries.
|
|
55
|
+
|
|
56
|
+
Deprecated alias: `ctx_stats`.
|
|
64
57
|
|
|
65
58
|
### compactor_doctor
|
|
66
59
|
```
|
|
67
60
|
compactor_doctor()
|
|
68
61
|
```
|
|
69
|
-
Diagnostics: config file, session DB,
|
|
62
|
+
Diagnostics: config file, session DB, and available runtimes (node, python3, bash).
|
|
63
|
+
|
|
64
|
+
Deprecated alias: `ctx_doctor`.
|
|
70
65
|
|
|
71
66
|
### context_budget
|
|
72
67
|
```
|
|
73
68
|
context_budget()
|
|
74
69
|
```
|
|
75
|
-
Estimate remaining context tokens and
|
|
70
|
+
Estimate remaining context tokens and percent full. Uses Pi's live context usage when available and includes percentage auto-compaction guidance when enabled.
|
|
76
71
|
|
|
77
72
|
## Sandbox Language Reference
|
|
78
73
|
|
|
@@ -90,44 +85,34 @@ Estimate remaining context tokens and % full. Returns guidance on whether to com
|
|
|
90
85
|
| r | Rscript | 30s | - |
|
|
91
86
|
| elixir | elixir | 30s | - |
|
|
92
87
|
|
|
93
|
-
## FTS5 Search Modes
|
|
94
|
-
|
|
95
|
-
| Mode | When To Use |
|
|
96
|
-
|------|-------------|
|
|
97
|
-
| **porter** | Exact term matching with stemming |
|
|
98
|
-
| **trigram** | Fuzzy/spelling errors, partial matches |
|
|
99
|
-
| **rrf** | Best overall (Reciprocal Rank Fusion of porter+trigram) |
|
|
100
|
-
| **fuzzy** | Auto-correction of misspellings from vocabulary |
|
|
101
|
-
|
|
102
|
-
Default: `rrf` (best general-purpose).
|
|
103
|
-
|
|
104
88
|
## Anti-Patterns
|
|
105
89
|
|
|
106
|
-
1. **Don't
|
|
107
|
-
2. **Don't
|
|
108
|
-
3. **Don't use `sandbox` for file
|
|
109
|
-
4. **Don't use
|
|
110
|
-
5. **Don't
|
|
111
|
-
6. **Don't compact mid-task.** Wait for a natural break point.
|
|
90
|
+
1. **Don't compact in a tight loop.** Use `context_budget` first and compact at natural break points.
|
|
91
|
+
2. **Don't use `session_recall` for project-wide code search.** It searches the conversation/session. Use CocoIndex (`cocoindex_search`) for indexed files when installed.
|
|
92
|
+
3. **Don't use `sandbox` for file operations.** Use normal file tools for reads/writes; sandbox is for computation or quick scripts.
|
|
93
|
+
4. **Don't use empty or vague recall queries.** Search for specific filenames, issue numbers, commands, or decisions.
|
|
94
|
+
5. **Don't compact mid-thought if avoidable.** Finish the current step, then compact.
|
|
112
95
|
|
|
113
96
|
## Workflow Patterns
|
|
114
97
|
|
|
115
|
-
### Research → Index → Search → Test
|
|
116
|
-
1. `content_fetch(url)` — index reference docs
|
|
117
|
-
2. `content_search(query)` — find relevant sections
|
|
118
|
-
3. `sandbox(lang, code)` — test hypotheses
|
|
119
|
-
|
|
120
|
-
### Diagnose → Fix → Verify
|
|
121
|
-
1. `compactor_doctor` — check system health
|
|
122
|
-
2. Fix issues (install runtimes, rebuild index)
|
|
123
|
-
3. `compactor_stats` — verify metrics
|
|
124
|
-
|
|
125
98
|
### Before Complex Work
|
|
126
|
-
1. `
|
|
127
|
-
2. `
|
|
128
|
-
3. `
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
99
|
+
1. `context_budget` — check pressure.
|
|
100
|
+
2. `session_recall("goal files decisions")` — recover relevant prior context.
|
|
101
|
+
3. `compact(dryRun: true)` — preview if context is tight.
|
|
102
|
+
4. `/unipi:lossless-compact` or `compact` at a clean boundary.
|
|
103
|
+
|
|
104
|
+
### Diagnose Compactor Health
|
|
105
|
+
1. `compactor_doctor` — check config, DB, runtimes.
|
|
106
|
+
2. Fix reported issues.
|
|
107
|
+
3. `compactor_stats` — verify counters and savings.
|
|
108
|
+
|
|
109
|
+
### Long Session Recovery
|
|
110
|
+
1. `session_recall(query)` — find older details hidden by compaction.
|
|
111
|
+
2. `context_budget` — decide whether another compaction is needed.
|
|
112
|
+
3. `compactor_stats` — confirm compactions/savings are recorded.
|
|
113
|
+
|
|
114
|
+
### Project Search
|
|
115
|
+
Compactor no longer owns project content indexing. Use the CocoIndex package if installed:
|
|
116
|
+
1. `/unipi:cocoindex-init`
|
|
117
|
+
2. `/unipi:cocoindex-update`
|
|
118
|
+
3. `cocoindex_search(query)`
|