pi-mega-compact 0.12.3 → 0.12.5

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 CHANGED
@@ -12,12 +12,13 @@ A local context compressor for the [pi coding agent](https://github.com/earendil
12
12
  - **Cross-repo recall** — doors you close in one repo don't reopen when you move to another. A decision stored while hacking repo A is a recall hit the next time you're in repo B.
13
13
  - **Durable memory** — on a cadence the store auto-reviews and safe-keeps decisions, facts, and preferences as first-class RAG memories, so long-running projects remember what mattered.
14
14
  - **Prompt-cache optimization** — message separation + cache striping (default OFF, opt-in). Targets 82-90% cache hit rate by structuring context around provider cache boundaries. Enables with `MEGACOMPACT_MESSAGE_SEPARATION` and `MEGACOMPACT_CACHE_STRIPING`.
15
+ - **Context health + KV cache poison validation** (v0.12) — a real-time composite 0-1 health score per turn from five sub-scores (drift, output quality, error rate, cache health, cache poison). Catches garbled/hallucinated output and provider-side KV-cache corruption *before* they waste tokens — the failure mode where a large-context model (e.g. DeepSeek V4 Flash, 1M window) degrades at <1% usage. Tri-layer cache poison validation: prefix hash (L1 FNV-1a), output-quality-by-cache-hit (L2 semantic), error-rate correlation (L3 behavioral). The dashboard **Health tab** shows a gauge, sparkline, sub-score bars, alerts, and per-model breakdown. Auto-mitigation (force compaction on degraded context, prefix break on poisoned cache) is default OFF — toggle it in the Maintenance tab.
15
16
  - **RAG suite** — query reformulation (TF-IDF + RRF), tiered routing (L0 cache -> L1 FTS5 -> L2 PGlite), and recall-quality metrics (CRAG). All default OFF, opt-in via flags `MEGACOMPACT_QUERY_REFORMULATION`, `MEGACOMPACT_TIERED_ROUTER`, `MEGACOMPACT_RECALL_METRICS`.
16
17
  - **Stacked memory graph** — the dashboard shows memory composition over time from 3 content sources (turns, durable memories, wiki) with a 9-gate validation system and a graph-health indicator. Per-model provider cache breakdown in the Cache tab.
17
18
  - **Debug bundle** — Maintenance tab in the dashboard has a **Gather Debug Logs** button that collects events, config, and store state into a shareable archive for bug reports.
18
19
  - **Fully local** — node:sqlite + trigram embeddings by default. Bring your own localhost embedder (ONNX, Ollama, TEI) for better semantic matches. Zero calls off your machine except the optional, localhost-only dashboard.
19
20
  - **Team-run aware** — fine-grained durable trim fires at agent settle during sub-agent runs, so long multi-agent work doesn't just collapse at the end.
20
- - **Multi-pi dashboard** — one dashboard tab per active pi process with the context stack, per-repo stats, and a live SSE feed across all of them. The React SPA has lazy-loaded tabs: Overview, Repos, Events, Config, Metrics, Cache, Game, Achievements, Sessions, Topics (wiki), Turns (per-turn memory + recall + rewind), and Maintenance (debug bundle).
21
+ - **Multi-pi dashboard** — one dashboard tab per active pi process with the context stack, per-repo stats, and a live SSE feed across all of them. The React SPA has 15 lazy-loaded tabs — **Overview**, **Cache**, **Sessions**, **Turns** (per-turn memory + recall + rewind), and **Health** (context-health gauge + cache-poison alerts) are primary; **Repos**, **Events**, **Config**, **Setup** (embedding wizard), **Metrics** (perf latency/TPS/CPU), **Game**, **Achievements**, **Topics** (wiki), **Memory Map** (D3 graph), and **Maintenance** (debug bundle) are advanced.
21
22
  - **Auto-categorizing wiki.** Every 3 compactions (seeds from turns before that), the store clusters your real memory embeddings (k-means) and labels each cluster with its most discriminative terms (TF-IDF) — no LLM, no Ollama, fully local. The dashboard **Wiki tab** browses topics, searches by label or term, and drills down into the member memories of each cluster.
22
23
 
23
24
  ## Install
@@ -76,17 +77,23 @@ Set env vars before starting pi. Defaults are in `src/config/dedup.ts`.
76
77
  | `MEGACOMPACT_MEMORY_GRAPH_SEED_TURNS` | `true` | Seed memory graph from turns (default ON) |
77
78
  | `MEGACOMPACT_WIKI_SEED_FROM_TURNS` | `true` | Seed wiki from turns (default ON) |
78
79
  | `MEGACOMPACT_RAPTOR_INCREMENTAL` | `true` | Incremental RAPTOR updates (default ON) |
80
+ | `MEGACOMPACT_CONTEXT_HEALTH` | `true` | Master switch: per-turn context-health composite score |
81
+ | `MEGACOMPACT_CONTEXT_HEALTH_DRIFT` | `true` | Sub-score: topic drift + error escalation + prefix instability |
82
+ | `MEGACOMPACT_CONTEXT_HEALTH_OUTPUT_QUALITY` | `true` | Sub-score: repetition, coherence, token-salad detection |
83
+ | `MEGACOMPACT_CONTEXT_HEALTH_CACHE_POISON` | `true` | Sub-score: tri-layer KV cache poison validation |
84
+ | `MEGACOMPACT_CONTEXT_HEALTH_MITIGATE` | `false` | Opt-in: auto-compaction on degraded context, prefix break on poisoned cache |
79
85
 
80
86
  Full config reference: [`docs/CONFIGURATION.md`](docs/CONFIGURATION.md)
81
87
 
82
88
  ## Architecture
83
89
 
84
90
  ```
85
- extensions/ Pi entry points (mega-compact, mega-trim, dashboard)
86
- src/engine.ts Trident pipeline (supersede -> collapse -> cluster)
91
+ extensions/ Pi entry points (mega-compact, mega-trim, dashboard)
92
+ src/engine.ts Trident pipeline (supersede -> collapse -> cluster)
87
93
  src/vectorStore.ts Local vector DB (add/search/dedupe)
88
94
  src/compact.ts Summarize / merge / auto-compact
89
95
  src/memory.ts Durable memories + auto-review
96
+ src/contextHealth.ts Context health composite + KV cache poison validation
90
97
  src/store/sqlite.ts node:sqlite store (Node >=22.13)
91
98
  src/store/vectorIndex.ts PGlite/HNSW cross-repo index
92
99
  ```
@@ -97,7 +104,7 @@ Detailed architecture: [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md)
97
104
 
98
105
  ```bash
99
106
  npm run build # TypeScript compile
100
- npm test # Build + 1112 tests
107
+ npm test # Build + 1088 tests
101
108
  npm run lint # Type check + guardrails scan
102
109
  ```
103
110
 
@@ -51,7 +51,21 @@ export function buildDashboardSnapshot(ctx) {
51
51
  tierPct: ctx.config.tierPct,
52
52
  effectiveThresholdPct: ctx.config.tierPct != null ? ctx.config.tierPct * 100 : null,
53
53
  },
54
- store: ctx.st,
54
+ store: {
55
+ checkpointCount: ctx.st.checkpointCount,
56
+ totalTokenEstimate: ctx.st.totalTokenEstimate,
57
+ originalTokens: ctx.st.originalTokens,
58
+ tokensSaved: ctx.st.tokensSaved,
59
+ injectedCount: ctx.st.injectedCount,
60
+ dedupHitRate: ctx.st.dedupHitRate,
61
+ // Session-scoped dedup stats from runtime counters, NOT the
62
+ // cumulative meta table (which is repo-wide and belongs in the
63
+ // repo card below). rt.dedupSkips/dedupAttempts reset on session
64
+ // restart so the session card shows only this session's dedup.
65
+ storageDedupRate: ctx.rt.dedupAttempts > 0 ? ctx.rt.dedupSkips / ctx.rt.dedupAttempts : 0,
66
+ dedupAttempts: ctx.rt.dedupAttempts,
67
+ dedupCollapsed: ctx.rt.dedupSkips,
68
+ },
55
69
  crew: {
56
70
  activeAgents: ctx.activeAgents,
57
71
  currentTurn: ctx.currentTurn,
@@ -76,13 +90,17 @@ export function buildDashboardSnapshot(ctx) {
76
90
  integrity: ctx.di,
77
91
  cacheHits: {
78
92
  session: ctx.rt.dedupSkips + ctx.rt.recallInjections,
79
- total: ctx.st.dedupCollapsed + ctx.st.injectedCount,
93
+ // Total = cumulative dedup collapses (meta table) + session
94
+ // recall injections. The meta table is the authoritative
95
+ // cumulative counter for dedup; recall injections are tracked
96
+ // per-session in rt and there's no cumulative counter yet.
97
+ total: ctx.repo.dedupCollapsed,
80
98
  sessionTokensSaved: ctx.rt.cacheHitTokens,
81
- totalTokensSaved: ctx.st.dedupCollapsed > 0 ? ctx.st.dedupCollapsed * 100 : 0,
99
+ totalTokensSaved: ctx.repo.dedupCollapsed > 0 ? ctx.repo.dedupCollapsed * 100 : 0,
82
100
  },
83
101
  compacts: {
84
102
  session: ctx.rt.compactCount,
85
- total: ctx.st.checkpointCount,
103
+ total: ctx.repo.checkpointCount,
86
104
  },
87
105
  timeSaved: {
88
106
  compact: {
@@ -91,7 +109,7 @@ export function buildDashboardSnapshot(ctx) {
91
109
  },
92
110
  cacheHit: {
93
111
  sessionSec: ctx.rt.cacheHitTokens / 1000,
94
- totalSec: (ctx.st.dedupCollapsed * 100) / 1000,
112
+ totalSec: (ctx.repo.dedupCollapsed * 100) / 1000,
95
113
  },
96
114
  },
97
115
  model: ctx.currentModel
@@ -110,7 +110,21 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
110
110
  tierPct: ctx.config.tierPct,
111
111
  effectiveThresholdPct: ctx.config.tierPct != null ? ctx.config.tierPct * 100 : null,
112
112
  },
113
- store: ctx.st,
113
+ store: {
114
+ checkpointCount: ctx.st.checkpointCount,
115
+ totalTokenEstimate: ctx.st.totalTokenEstimate,
116
+ originalTokens: ctx.st.originalTokens,
117
+ tokensSaved: ctx.st.tokensSaved,
118
+ injectedCount: ctx.st.injectedCount,
119
+ dedupHitRate: ctx.st.dedupHitRate,
120
+ // Session-scoped dedup stats from runtime counters, NOT the
121
+ // cumulative meta table (which is repo-wide and belongs in the
122
+ // repo card below). rt.dedupSkips/dedupAttempts reset on session
123
+ // restart so the session card shows only this session's dedup.
124
+ storageDedupRate: ctx.rt.dedupAttempts > 0 ? ctx.rt.dedupSkips / ctx.rt.dedupAttempts : 0,
125
+ dedupAttempts: ctx.rt.dedupAttempts,
126
+ dedupCollapsed: ctx.rt.dedupSkips,
127
+ },
114
128
  crew: {
115
129
  activeAgents: ctx.activeAgents,
116
130
  currentTurn: ctx.currentTurn,
@@ -135,13 +149,17 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
135
149
  integrity: ctx.di,
136
150
  cacheHits: {
137
151
  session: ctx.rt.dedupSkips + ctx.rt.recallInjections,
138
- total: ctx.st.dedupCollapsed + ctx.st.injectedCount,
152
+ // Total = cumulative dedup collapses (meta table) + session
153
+ // recall injections. The meta table is the authoritative
154
+ // cumulative counter for dedup; recall injections are tracked
155
+ // per-session in rt and there's no cumulative counter yet.
156
+ total: ctx.repo.dedupCollapsed,
139
157
  sessionTokensSaved: ctx.rt.cacheHitTokens,
140
- totalTokensSaved: ctx.st.dedupCollapsed > 0 ? ctx.st.dedupCollapsed * 100 : 0,
158
+ totalTokensSaved: ctx.repo.dedupCollapsed > 0 ? ctx.repo.dedupCollapsed * 100 : 0,
141
159
  },
142
160
  compacts: {
143
161
  session: ctx.rt.compactCount,
144
- total: ctx.st.checkpointCount,
162
+ total: ctx.repo.checkpointCount,
145
163
  },
146
164
  timeSaved: {
147
165
  compact: {
@@ -150,7 +168,7 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
150
168
  },
151
169
  cacheHit: {
152
170
  sessionSec: ctx.rt.cacheHitTokens / 1000,
153
- totalSec: (ctx.st.dedupCollapsed * 100) / 1000,
171
+ totalSec: (ctx.repo.dedupCollapsed * 100) / 1000,
154
172
  },
155
173
  },
156
174
  model: ctx.currentModel
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.12.3",
3
+ "version": "0.12.5",
4
4
  "description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
5
5
  "type": "module",
6
6
  "license": "BSD-3-Clause",