@pi-unipi/compactor 0.1.1

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.
Files changed (65) hide show
  1. package/README.md +86 -0
  2. package/package.json +54 -0
  3. package/skills/compactor/SKILL.md +74 -0
  4. package/skills/compactor-doctor/SKILL.md +74 -0
  5. package/skills/compactor-ops/SKILL.md +65 -0
  6. package/skills/compactor-stats/SKILL.md +49 -0
  7. package/skills/compactor-tools/SKILL.md +120 -0
  8. package/src/commands/index.ts +248 -0
  9. package/src/compaction/brief.ts +334 -0
  10. package/src/compaction/build-sections.ts +77 -0
  11. package/src/compaction/content.ts +47 -0
  12. package/src/compaction/cut.ts +80 -0
  13. package/src/compaction/extract/commits.ts +52 -0
  14. package/src/compaction/extract/files.ts +58 -0
  15. package/src/compaction/extract/goals.ts +36 -0
  16. package/src/compaction/extract/preferences.ts +40 -0
  17. package/src/compaction/filter-noise.ts +46 -0
  18. package/src/compaction/format.ts +48 -0
  19. package/src/compaction/hooks.ts +145 -0
  20. package/src/compaction/merge.ts +113 -0
  21. package/src/compaction/normalize.ts +68 -0
  22. package/src/compaction/recall-scope.ts +32 -0
  23. package/src/compaction/sanitize.ts +12 -0
  24. package/src/compaction/search-entries.ts +101 -0
  25. package/src/compaction/sections.ts +15 -0
  26. package/src/compaction/summarize.ts +29 -0
  27. package/src/config/manager.ts +89 -0
  28. package/src/config/presets.ts +83 -0
  29. package/src/config/schema.ts +55 -0
  30. package/src/display/bash-display.ts +28 -0
  31. package/src/display/diff-presentation.ts +20 -0
  32. package/src/display/diff-renderer.ts +255 -0
  33. package/src/display/line-width-safety.ts +16 -0
  34. package/src/display/pending-diff-preview.ts +51 -0
  35. package/src/display/render-utils.ts +52 -0
  36. package/src/display/thinking-label.ts +18 -0
  37. package/src/display/tool-overrides.ts +136 -0
  38. package/src/display/user-message-box.ts +16 -0
  39. package/src/executor/executor.ts +242 -0
  40. package/src/executor/runtime.ts +125 -0
  41. package/src/index.ts +211 -0
  42. package/src/info-screen.ts +60 -0
  43. package/src/security/evaluator.ts +142 -0
  44. package/src/security/policy.ts +74 -0
  45. package/src/security/scanner.ts +65 -0
  46. package/src/session/db.ts +237 -0
  47. package/src/session/extract.ts +107 -0
  48. package/src/session/resume-inject.ts +25 -0
  49. package/src/session/snapshot.ts +326 -0
  50. package/src/store/chunking.ts +126 -0
  51. package/src/store/db-base.ts +79 -0
  52. package/src/store/index.ts +364 -0
  53. package/src/tools/compact.ts +20 -0
  54. package/src/tools/ctx-batch-execute.ts +53 -0
  55. package/src/tools/ctx-doctor.ts +78 -0
  56. package/src/tools/ctx-execute-file.ts +26 -0
  57. package/src/tools/ctx-execute.ts +21 -0
  58. package/src/tools/ctx-fetch-and-index.ts +37 -0
  59. package/src/tools/ctx-index.ts +42 -0
  60. package/src/tools/ctx-search.ts +23 -0
  61. package/src/tools/ctx-stats.ts +37 -0
  62. package/src/tools/register.ts +360 -0
  63. package/src/tools/vcc-recall.ts +64 -0
  64. package/src/tui/settings-overlay.ts +290 -0
  65. package/src/types.ts +269 -0
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # @pi-unipi/compactor
2
+
3
+ Context engine for Pi coding agent. Fuses zero-LLM compaction, session continuity, sandbox execution, FTS5 search, and tool display optimization into a single cohesive package.
4
+
5
+ ## Features
6
+
7
+ - **Zero-LLM Compaction** — 6-stage pipeline (normalize → filter → build sections → brief → format → merge) achieves 95%+ token reduction with zero API cost
8
+ - **Session Continuity** — XML resume snapshots survive compaction, preserving context across session boundaries
9
+ - **Sandbox Execution** — 11 languages with process isolation, security hardening, and output capping
10
+ - **FTS5 Search** — Full-text search over indexed content with auto-chunking
11
+ - **Tool Display** — Mode-aware rendering for read, grep, find, ls, bash, edit, write tools
12
+
13
+ ## Commands
14
+
15
+ | Command | Description |
16
+ |---------|-------------|
17
+ | `/unipi:compact` | Manual compaction with stats |
18
+ | `/unipi:compact-recall` | Search session history |
19
+ | `/unipi:compact-stats` | Context savings dashboard |
20
+ | `/unipi:compact-doctor` | Run diagnostics |
21
+ | `/unipi:compact-settings` | TUI settings overlay |
22
+ | `/unipi:compact-preset <name>` | Apply quick preset |
23
+ | `/unipi:compact-index` | Index current project |
24
+ | `/unipi:compact-search` | Search indexed content |
25
+ | `/unipi:compact-purge` | Wipe all indexed content |
26
+
27
+ ## Tools
28
+
29
+ | Tool | Description |
30
+ |------|-------------|
31
+ | `compact` | Trigger manual compaction |
32
+ | `vcc_recall` | BM25 session history search |
33
+ | `ctx_execute` | Run code, stdout enters context |
34
+ | `ctx_execute_file` | Process file via FILE_CONTENT |
35
+ | `ctx_batch_execute` | Atomic batch of commands + searches |
36
+ | `ctx_index` | Chunk content → FTS5 index |
37
+ | `ctx_search` | Query indexed content |
38
+ | `ctx_fetch_and_index` | Fetch URL → index |
39
+ | `ctx_stats` | Context savings dashboard |
40
+ | `ctx_doctor` | Diagnostics checklist |
41
+
42
+ ## Configuration
43
+
44
+ Config lives at `~/.unipi/config/compactor/config.json`. Each of the 9 strategies can be toggled on/off and cycled through modes.
45
+
46
+ ### Presets
47
+
48
+ | Preset | Description |
49
+ |--------|-------------|
50
+ | `opencode` | Maximal context preservation, minimal display |
51
+ | `balanced` | Moderate across all strategies |
52
+ | `verbose` | Everything visible, everything tracked |
53
+ | `minimal` | Only compaction + basic recall |
54
+
55
+ Apply via `/unipi:compact-preset <name>`.
56
+
57
+ ## Architecture
58
+
59
+ ```
60
+ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
61
+ │ Compaction Core │ │ Session Engine │ │ Display Engine │
62
+ │ (zero-LLM) │ │ (SQLite + XML) │ │ (mode-aware) │
63
+ └────────┬────────┘ └────────┬────────┘ └────────┬────────┘
64
+ │ │ │
65
+ └────────────────────┼────────────────────┘
66
+
67
+ ┌─────────────────────┐
68
+ │ Config Manager │
69
+ └─────────────────────┘
70
+ ```
71
+
72
+ ## Installation
73
+
74
+ Included in `@pi-unipi/unipi` metapackage. To use standalone:
75
+
76
+ ```json
77
+ {
78
+ "pi": {
79
+ "extensions": ["node_modules/@pi-unipi/compactor/src/index.ts"]
80
+ }
81
+ }
82
+ ```
83
+
84
+ ## License
85
+
86
+ MIT
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@pi-unipi/compactor",
3
+ "version": "0.1.1",
4
+ "description": "Context engine for Pi — zero-LLM compaction, session continuity, sandbox execution, FTS5 search, and tool display optimization",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Neuron Mr White",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/Neuron-Mr-White/unipi.git",
11
+ "directory": "packages/compactor"
12
+ },
13
+ "keywords": [
14
+ "pi-package",
15
+ "pi-extension",
16
+ "pi-coding-agent",
17
+ "unipi",
18
+ "compactor",
19
+ "context",
20
+ "sandbox",
21
+ "fts5"
22
+ ],
23
+ "files": [
24
+ "src/**/*.ts",
25
+ "skills/**/*",
26
+ "README.md"
27
+ ],
28
+ "pi": {
29
+ "extensions": [
30
+ "src/index.ts"
31
+ ],
32
+ "skills": [
33
+ "skills"
34
+ ]
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "@pi-unipi/core": "*"
41
+ },
42
+ "peerDependencies": {
43
+ "@mariozechner/pi-coding-agent": "*",
44
+ "@mariozechner/pi-tui": "*",
45
+ "@sinclair/typebox": "*"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^25.6.0",
49
+ "typescript": "^6.0.0"
50
+ },
51
+ "optionalDependencies": {
52
+ "better-sqlite3": ">=11.0.0"
53
+ }
54
+ }
@@ -0,0 +1,74 @@
1
+ ---
2
+ name: compactor
3
+ description: Routing decision tree for compactor — when to compact, search, index, or diagnose.
4
+ ---
5
+
6
+ # Compactor Routing
7
+
8
+ Use this skill to decide which compactor action to take.
9
+
10
+ ## Decision Tree
11
+
12
+ ```
13
+ User wants to...
14
+ ├── Reduce context / free tokens
15
+ │ └── /unipi:compact or `compact` tool
16
+
17
+ ├── Find something from earlier in the session
18
+ │ └── /unipi:compact-recall <query> or `vcc_recall` tool
19
+
20
+ ├── Run code safely
21
+ │ └── `ctx_execute` tool (single) or `ctx_batch_execute` (batch)
22
+
23
+ ├── Index project files for search
24
+ │ └── /unipi:compact-index or `ctx_index` tool
25
+
26
+ ├── Search indexed content
27
+ │ └── /unipi:compact-search <query> or `ctx_search` tool
28
+
29
+ ├── Fetch and index a URL
30
+ │ └── `ctx_fetch_and_index` tool
31
+
32
+ ├── View compactor stats
33
+ │ └── /unipi:compact-stats or `ctx_stats` tool
34
+
35
+ ├── Diagnose issues
36
+ │ └── /unipi:compact-doctor or `ctx_doctor` tool
37
+
38
+ ├── Change settings
39
+ │ └── /unipi:compact-settings (TUI overlay)
40
+ │ └── /unipi:compact-preset <name> (quick preset)
41
+
42
+ └── Wipe indexed content
43
+ └── /unipi:compact-purge
44
+ ```
45
+
46
+ ## When to Compact
47
+
48
+ - Context window > 80% full
49
+ - Session has > 100 messages
50
+ - Before starting a complex multi-step task
51
+ - When the agent starts repeating itself
52
+
53
+ ## When to Recall
54
+
55
+ - User references something from earlier
56
+ - Need to find a file path mentioned before
57
+ - Looking for a previous error or decision
58
+ - Searching for a specific code snippet
59
+
60
+ ## When to Index
61
+
62
+ - Starting work on a large codebase
63
+ - Need fast search across many files
64
+ - Documentation or reference material
65
+ - Before a research-heavy task
66
+
67
+ ## Presets
68
+
69
+ | Preset | Best For |
70
+ |--------|----------|
71
+ | `opencode` | Code-heavy work, minimal context waste |
72
+ | `balanced` | General use, good defaults |
73
+ | `verbose` | Maximum context preservation |
74
+ | `minimal` | Maximum token savings |
@@ -0,0 +1,74 @@
1
+ ---
2
+ name: compactor-doctor
3
+ description: Diagnostics — validate config, DB, FTS5, runtimes, troubleshoot issues.
4
+ ---
5
+
6
+ # Compactor Doctor
7
+
8
+ Run diagnostics and troubleshoot compactor issues.
9
+
10
+ ## Commands
11
+
12
+ - `/unipi:compact-doctor` — run all checks
13
+ - `ctx_doctor` tool — agent-callable diagnostics
14
+
15
+ ## Checks Performed
16
+
17
+ | Check | What It Validates |
18
+ |-------|-------------------|
19
+ | **Config file** | `~/.unipi/config/compactor/config.json` exists and is valid |
20
+ | **Session DB** | SQLite connection works, schema correct |
21
+ | **Content Store** | FTS5 index accessible, tables exist |
22
+ | **Runtime: node** | Node.js available for sandbox |
23
+ | **Runtime: python3** | Python 3 available for sandbox |
24
+ | **Runtime: bash** | Bash available for sandbox |
25
+
26
+ ## Status Icons
27
+
28
+ - ✅ **pass** — check succeeded
29
+ - ⚠️ **warn** — non-critical issue (e.g., optional runtime missing)
30
+ - ❌ **fail** — critical issue, feature may not work
31
+
32
+ ## Common Issues
33
+
34
+ ### "Config file: Using defaults"
35
+ - Normal on first run
36
+ - Config auto-created on next settings save
37
+ - Fix: `/unipi:compact-settings` → save
38
+
39
+ ### "Session DB: Connection failed"
40
+ - SQLite not available
41
+ - Check if `better-sqlite3` is installed
42
+ - Fix: `npm install better-sqlite3`
43
+
44
+ ### "Content Store: FTS5 error"
45
+ - SQLite FTS5 extension not available
46
+ - Requires SQLite 3.9+ with FTS5
47
+ - Fix: Update system SQLite
48
+
49
+ ### "Runtime: python3 Not found"
50
+ - Python not installed or not in PATH
51
+ - Only needed for Python sandbox execution
52
+ - Fix: Install Python 3 or ignore if not needed
53
+
54
+ ## Manual Diagnostics
55
+
56
+ ### Check config
57
+ ```bash
58
+ cat ~/.unipi/config/compactor/config.json
59
+ ```
60
+
61
+ ### Check DB files
62
+ ```bash
63
+ ls -la ~/.unipi/db/compactor/
64
+ ```
65
+
66
+ ### Check SQLite version
67
+ ```bash
68
+ sqlite3 --version
69
+ ```
70
+
71
+ ### Test FTS5
72
+ ```bash
73
+ sqlite3 ':memory:' "CREATE VIRTUAL TABLE t USING fts5(x); SELECT 1;"
74
+ ```
@@ -0,0 +1,65 @@
1
+ ---
2
+ name: compactor-ops
3
+ description: Engineering ops orchestration — batch operations, project indexing, maintenance.
4
+ ---
5
+
6
+ # Compactor Ops
7
+
8
+ Orchestrate batch operations, project indexing, and maintenance tasks.
9
+
10
+ ## Project Indexing Workflow
11
+
12
+ When starting work on a new codebase:
13
+
14
+ 1. **Index project files** — `/unipi:compact-index` or `ctx_index` for specific files
15
+ 2. **Verify index** — `/unipi:compact-stats` to check chunk count
16
+ 3. **Test search** — `/unipi:compact-search <term>` to verify quality
17
+
18
+ ## Batch Code Execution
19
+
20
+ Use `ctx_batch_execute` for atomic multi-step operations:
21
+
22
+ ```json
23
+ {
24
+ "items": [
25
+ { "type": "execute", "language": "shell", "code": "ls -la src/" },
26
+ { "type": "execute", "language": "python", "code": "import ast; print('OK')" },
27
+ { "type": "search", "query": "authentication module" }
28
+ ]
29
+ }
30
+ ```
31
+
32
+ ## Maintenance Tasks
33
+
34
+ ### Clear stale index
35
+ ```
36
+ /unipi:compact-purge
37
+ /unipi:compact-index
38
+ ```
39
+
40
+ ### Reset configuration
41
+ ```
42
+ /unipi:compact-preset balanced
43
+ ```
44
+
45
+ ### Full diagnostics
46
+ ```
47
+ /unipi:compact-doctor
48
+ ```
49
+
50
+ ## Integration Patterns
51
+
52
+ ### Before complex task
53
+ 1. `/unipi:compact` — free up context
54
+ 2. `/unipi:compact-index` — index relevant files
55
+ 3. Start work with fresh context + indexed search
56
+
57
+ ### After long session
58
+ 1. `/unipi:compact-stats` — check savings
59
+ 2. `/unipi:compact` — compact if needed
60
+ 3. `/unipi:compact-recall <topic>` — verify recall quality
61
+
62
+ ### Research workflow
63
+ 1. `ctx_fetch_and_index` — index reference docs
64
+ 2. `ctx_search` — find relevant sections
65
+ 3. `ctx_execute` — test hypotheses in sandbox
@@ -0,0 +1,49 @@
1
+ ---
2
+ name: compactor-stats
3
+ description: Stats display — context savings, session metrics, index health.
4
+ ---
5
+
6
+ # Compactor Stats
7
+
8
+ Display and interpret compactor statistics.
9
+
10
+ ## Commands
11
+
12
+ - `/unipi:compact-stats` — quick dashboard
13
+ - `ctx_stats` tool — detailed stats (agent-callable)
14
+
15
+ ## Metrics Explained
16
+
17
+ | Metric | Description |
18
+ |--------|-------------|
19
+ | **Session events** | Total events tracked in current session |
20
+ | **Compactions** | Number of times context was compacted |
21
+ | **Tokens saved** | Estimated tokens freed by compaction |
22
+ | **Indexed docs** | Number of files/sources in FTS5 index |
23
+ | **Indexed chunks** | Total searchable chunks |
24
+ | **Sandbox runs** | Code executions in sandbox |
25
+ | **Search queries** | FTS5 searches performed |
26
+
27
+ ## Health Indicators
28
+
29
+ - **Compactions > 0** on long sessions = working correctly
30
+ - **Indexed chunks > 0** = search available
31
+ - **Sandbox runs** = code execution active
32
+
33
+ ## Reading Stats
34
+
35
+ ```
36
+ 📊 Compactor Stats
37
+ Session events: 42
38
+ Compactions: 3
39
+ Tokens saved: 15000
40
+ Indexed docs: 12 (48 chunks)
41
+ Sandbox runs: 7
42
+ Search queries: 15
43
+ ```
44
+
45
+ This means:
46
+ - 42 tool calls/events tracked
47
+ - 3 compactions saved ~15K tokens
48
+ - 12 files indexed into 48 searchable chunks
49
+ - 7 code executions, 15 searches performed
@@ -0,0 +1,120 @@
1
+ ---
2
+ name: compactor-tools
3
+ description: Reference card for all compactor tools — parameters, usage, examples.
4
+ ---
5
+
6
+ # Compactor Tools Reference
7
+
8
+ ## compact
9
+
10
+ Trigger manual context compaction.
11
+
12
+ ```
13
+ compact()
14
+ ```
15
+
16
+ - Returns compaction stats (summarized, kept, tokens estimated)
17
+ - Actual work done by `session_before_compact` hook
18
+ - Zero LLM calls — pure regex/text processing
19
+
20
+ ## vcc_recall
21
+
22
+ Search session history using BM25 or regex.
23
+
24
+ ```
25
+ vcc_recall(query: string, mode?: "bm25" | "regex", limit?: number, offset?: number, expand?: boolean)
26
+ ```
27
+
28
+ - **query**: Search terms
29
+ - **mode**: `bm25` (default, ranked) or `regex` (pattern match)
30
+ - **limit**: Max results (default 10)
31
+ - **offset**: Pagination offset
32
+ - **expand**: Return full message content for hits
33
+
34
+ ## ctx_execute
35
+
36
+ Run code in a sandboxed environment.
37
+
38
+ ```
39
+ ctx_execute(language: string, code: string, timeout?: number)
40
+ ```
41
+
42
+ **Supported languages:** javascript, typescript, python, shell, ruby, go, rust, php, perl, r, elixir
43
+
44
+ - Only stdout enters context (stderr filtered)
45
+ - 100MB output cap with process kill
46
+ - 30s default timeout
47
+ - Auto-detects Bun for JS/TS
48
+
49
+ ## ctx_execute_file
50
+
51
+ Execute a file with content injected as `FILE_CONTENT`.
52
+
53
+ ```
54
+ ctx_execute_file(language: string, path: string, timeout?: number)
55
+ ```
56
+
57
+ ## ctx_batch_execute
58
+
59
+ Run multiple commands atomically.
60
+
61
+ ```
62
+ ctx_batch_execute(items: Array<ExecuteItem | SearchItem>)
63
+ ```
64
+
65
+ - Execute items: `{ type: "execute", language, code, timeout? }`
66
+ - Search items: `{ type: "search", query, limit? }`
67
+
68
+ ## ctx_index
69
+
70
+ Index content into FTS5 for fast search.
71
+
72
+ ```
73
+ ctx_index(label: string, content?: string, filePath?: string, contentType?: "markdown"|"json"|"plain", chunkSize?: number)
74
+ ```
75
+
76
+ - Provide either `content` or `filePath`
77
+ - Auto-chunks by content type
78
+ - Deduplicates by label
79
+
80
+ ## ctx_search
81
+
82
+ Query indexed content.
83
+
84
+ ```
85
+ ctx_search(query: string, limit?: number, offset?: number)
86
+ ```
87
+
88
+ - Returns ranked results with title, content, source, rank
89
+ - Supports pagination via offset/limit
90
+
91
+ ## ctx_fetch_and_index
92
+
93
+ Fetch URL → markdown → index.
94
+
95
+ ```
96
+ ctx_fetch_and_index(url: string, label?: string, chunkSize?: number)
97
+ ```
98
+
99
+ - Converts HTML to markdown
100
+ - Auto-indexes for later search
101
+
102
+ ## ctx_stats
103
+
104
+ Context savings dashboard.
105
+
106
+ ```
107
+ ctx_stats()
108
+ ```
109
+
110
+ Returns: session events, compactions, tokens saved, indexed docs, sandbox runs, search queries.
111
+
112
+ ## ctx_doctor
113
+
114
+ Diagnostics checklist.
115
+
116
+ ```
117
+ ctx_doctor()
118
+ ```
119
+
120
+ Checks: config file, session DB, content store, runtimes (node, python3, bash).