pi-mega-compact 0.11.5 → 0.11.6
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/dist/extensions/dashboard-server/api-contracts/cache-stripes.js +8 -0
- package/dist/extensions/dashboard-server/api-contracts/endpoints.js +13 -0
- package/dist/extensions/dashboard-server/api-contracts/memory-map.js +1 -0
- package/dist/extensions/dashboard-server/routes-cache.js +28 -0
- package/dist/extensions/dashboard-server/routes-game.js +3 -0
- package/dist/extensions/dashboard-server/routes-memory-map.js +50 -0
- package/dist/extensions/dashboard-server/routes-setup.js +105 -0
- package/dist/extensions/dashboard-server/routes.js +3 -1
- package/dist/extensions/dashboard-server/server.js +7 -1
- package/dist/extensions/mega-compact.js +5 -0
- package/dist/extensions/mega-config.js +5 -0
- package/dist/extensions/mega-events/context-handler.js +10 -2
- package/dist/extensions/mega-events/perf-handler.js +83 -1
- package/dist/extensions/mega-events/separated-prompt.js +248 -0
- package/dist/extensions/mega-runtime/env-loader.js +56 -0
- package/dist/extensions/mega-runtime/reset-runtime.js +1 -0
- package/dist/extensions/mega-runtime/runtime-snapshot.js +1 -0
- package/dist/extensions/mega-runtime/runtime.js +1 -0
- package/dist/extensions/mega-runtime/snapshot.js +1 -0
- package/dist/extensions/mega-runtime/widget.js +5 -0
- package/dist/src/cache-stripe-impl.js +288 -0
- package/dist/src/cache-stripe.js +11 -0
- package/dist/src/config/stopwords.js +24 -0
- package/dist/src/config.js +17 -0
- package/dist/src/memoryGraph.js +299 -0
- package/dist/src/monitoring.js +13 -0
- package/dist/src/queryExpansion.js +163 -0
- package/dist/src/queryReformulation/cache.js +123 -0
- package/dist/src/queryReformulation/rrf.js +29 -0
- package/dist/src/queryReformulation/tfidf.js +61 -0
- package/dist/src/queryReformulation/vagueness.js +33 -0
- package/dist/src/queryReformulation.js +225 -0
- package/dist/src/recallMetrics.js +258 -0
- package/dist/src/store/sqlite/cache-stripes.js +133 -0
- package/dist/src/store/sqlite/fts5-search.js +68 -0
- package/dist/src/store/sqlite/perf-samples.js +1 -0
- package/dist/src/store/sqlite/schema.js +60 -1
- package/dist/src/store/sqlite.js +1 -0
- package/dist/src/store/vectorIndex.js +29 -5
- package/dist/src/tiered-router/types.js +18 -0
- package/dist/src/tieredRouter.js +421 -0
- package/dist/src/vectorStore.js +8 -6
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-BoL7qBOJ.js → AchievementTiles-DR9cLGDL.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AchievementTiles-BoL7qBOJ.js.map → AchievementTiles-DR9cLGDL.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{AchievementsTab-BFDE3HYc.js → AchievementsTab-svNUgJ1e.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AchievementsTab-BFDE3HYc.js.map → AchievementsTab-svNUgJ1e.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/CacheTab-ASwhOMjR.js +2 -0
- package/extensions/dashboard-client/dist/assets/CacheTab-ASwhOMjR.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{ConfigTab-DDFE-ckA.js → ConfigTab-CmmWheuW.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ConfigTab-DDFE-ckA.js.map → ConfigTab-CmmWheuW.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-B6UxZ7PM.js → EventsTab-CFE7jLsa.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-B6UxZ7PM.js.map → EventsTab-CFE7jLsa.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/GameTab-CJAX_th_.js +2 -0
- package/extensions/dashboard-client/dist/assets/{GameTab-DehUX-KE.js.map → GameTab-CJAX_th_.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-0GWRt4wn.js → MaintenanceTab-P4SUsvMf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-0GWRt4wn.js.map → MaintenanceTab-P4SUsvMf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/MemoryMapTab-BBiH76eF.js +2 -0
- package/extensions/dashboard-client/dist/assets/MemoryMapTab-BBiH76eF.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{MetricsTab-C3rwMVsN.js → MetricsTab-CHQscixy.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-C3rwMVsN.js.map → MetricsTab-CHQscixy.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-39O9_x5j.js → OverviewTab-DBs4DJdd.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-39O9_x5j.js.map → OverviewTab-DBs4DJdd.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-Cfj6Q5sH.js → ReposTab-DeFz76L5.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-Cfj6Q5sH.js.map → ReposTab-DeFz76L5.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DXDTCi3r.js → SessionsTab-BWNsj1OB.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-DXDTCi3r.js.map → SessionsTab-BWNsj1OB.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/SetupTab-uS6A0xmw.js +2 -0
- package/extensions/dashboard-client/dist/assets/SetupTab-uS6A0xmw.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-Cw1dJ4e9.js → TimeSavedCard-CiqCOQuL.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-Cw1dJ4e9.js.map → TimeSavedCard-CiqCOQuL.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TopicsTab-BAw972BV.js → TopicsTab-BWWQxltv.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TopicsTab-BAw972BV.js.map → TopicsTab-BWWQxltv.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Cf5mGIdl.js → TurnsTab-CVz4f4-o.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Cf5mGIdl.js.map → TurnsTab-CVz4f4-o.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/index-CUNn22iV.css +1 -0
- package/extensions/dashboard-client/dist/assets/{index-4gQ7auad.js → index-DrktNOoT.js} +11 -11
- package/extensions/dashboard-client/dist/assets/index-DrktNOoT.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{useSSE-DNgMuyP6.js → useSSE-Bf5LOLSi.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-DNgMuyP6.js.map → useSSE-Bf5LOLSi.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +2 -2
- package/extensions/dashboard-client/src/App.tsx +24 -8
- package/extensions/dashboard-client/src/api/client.ts +15 -0
- package/extensions/dashboard-client/src/components/CacheHitRateTrendCard.tsx +100 -0
- package/extensions/dashboard-client/src/components/StripeDistributionCard.tsx +110 -0
- package/extensions/dashboard-client/src/components/TabBar.tsx +49 -4
- package/extensions/dashboard-client/src/main.tsx +1 -0
- package/extensions/dashboard-client/src/styles/base.css +30 -0
- package/extensions/dashboard-client/src/styles/cache.css +167 -0
- package/extensions/dashboard-client/src/tabs/CacheTab.tsx +75 -13
- package/extensions/dashboard-client/src/tabs/MemoryMapTab.tsx +440 -0
- package/extensions/dashboard-client/src/tabs/SetupTab.tsx +65 -2
- package/extensions/dashboard-server/api-contracts/cache-stripes.ts +49 -0
- package/extensions/dashboard-server/api-contracts/endpoints.ts +31 -1
- package/extensions/dashboard-server/api-contracts/index.ts +16 -1
- package/extensions/dashboard-server/api-contracts/memory-map.ts +50 -0
- package/extensions/dashboard-server/api-contracts/setup.ts +22 -0
- package/extensions/dashboard-server/routes-cache.ts +36 -1
- package/extensions/dashboard-server/routes-game.ts +3 -0
- package/extensions/dashboard-server/routes-memory-map.ts +105 -0
- package/extensions/dashboard-server/routes-setup.ts +112 -0
- package/extensions/dashboard-server/routes.ts +3 -1
- package/extensions/dashboard-server/server.ts +6 -0
- package/extensions/mega-compact.ts +5 -0
- package/extensions/mega-config.ts +17 -0
- package/extensions/mega-events/context-handler.ts +10 -2
- package/extensions/mega-events/perf-handler.ts +111 -1
- package/extensions/mega-events/separated-prompt.ts +329 -0
- package/extensions/mega-runtime/env-loader.ts +55 -0
- package/extensions/mega-runtime/helpers.ts +2 -0
- package/extensions/mega-runtime/reset-runtime.ts +1 -0
- package/extensions/mega-runtime/runtime-snapshot.ts +1 -0
- package/extensions/mega-runtime/runtime.ts +1 -0
- package/extensions/mega-runtime/snapshot.ts +6 -0
- package/extensions/mega-runtime/widget-types.ts +3 -0
- package/extensions/mega-runtime/widget.ts +5 -0
- package/package.json +1 -1
- package/src/cache-stripe-impl.ts +385 -0
- package/src/cache-stripe.ts +18 -0
- package/src/config/stopwords.ts +25 -0
- package/src/config.ts +26 -0
- package/src/memoryGraph.ts +438 -0
- package/src/monitoring.ts +35 -0
- package/src/queryExpansion.ts +206 -0
- package/src/queryReformulation/cache.ts +169 -0
- package/src/queryReformulation/rrf.ts +51 -0
- package/src/queryReformulation/tfidf.ts +79 -0
- package/src/queryReformulation/vagueness.ts +38 -0
- package/src/queryReformulation.ts +355 -0
- package/src/recallMetrics.ts +388 -0
- package/src/store/sqlite/cache-stripes.ts +196 -0
- package/src/store/sqlite/fts5-search.ts +90 -0
- package/src/store/sqlite/perf-samples.ts +3 -1
- package/src/store/sqlite/schema.ts +60 -1
- package/src/store/sqlite.ts +1 -0
- package/src/store/vectorIndex.ts +38 -7
- package/src/tiered-router/types.ts +64 -0
- package/src/tieredRouter.ts +489 -0
- package/src/vectorStore.ts +9 -5
- package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js +0 -2
- package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/GameTab-DehUX-KE.js +0 -2
- package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js +0 -2
- package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-4gQ7auad.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-BEdKlpAB.css +0 -1
|
@@ -15,7 +15,10 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
17
17
|
import { type MegaRuntime } from "../mega-runtime.js";
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
recordPerfSample,
|
|
20
|
+
readPerfSamples,
|
|
21
|
+
} from "../../src/store/sqlite.js";
|
|
19
22
|
import { classifyPrefixBreak, type EventTimestamps } from "../../src/prefix-break.js";
|
|
20
23
|
|
|
21
24
|
/** Structural view of an AssistantMessage usage block (no pi-ai import). */
|
|
@@ -42,6 +45,9 @@ function usageOf(
|
|
|
42
45
|
* classifyPrefixBreak with the three candidate event timestamps from the
|
|
43
46
|
* runtime and records a `prefix_break` sample. Best-effort + non-fatal.
|
|
44
47
|
*
|
|
48
|
+
* Also evaluates rolling cache health and emits a degradation event when the
|
|
49
|
+
* composite score falls below 0.6 (A3, PLAN_V2 Phase 4).
|
|
50
|
+
*
|
|
45
51
|
* PREVENT-PI-004: guardrails-allow PREVENT-PI-004: local SQLite write only.
|
|
46
52
|
* PREVENT-002: SQL is fully parameterized via recordPerfSample.
|
|
47
53
|
*/
|
|
@@ -74,11 +80,115 @@ function tryRecordPrefixBreak(
|
|
|
74
80
|
breakAt,
|
|
75
81
|
},
|
|
76
82
|
); // guardrails-allow PREVENT-PI-004: local SQLite write (loopback perf instrumentation)
|
|
83
|
+
|
|
84
|
+
// A3: compute rolling cache health score and emit alert on degradation.
|
|
85
|
+
tryComputeCacheHealth(stateDir, currHitPct, runtime);
|
|
77
86
|
} catch {
|
|
78
87
|
/* non-fatal: prefix-break detection never blocks the agent loop */
|
|
79
88
|
}
|
|
80
89
|
}
|
|
81
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Compute a rolling cache health score and emit a dashboard event when it
|
|
93
|
+
* falls below the degradation threshold (0.6 / "yellow").
|
|
94
|
+
*
|
|
95
|
+
* The score is a weighted composite of:
|
|
96
|
+
* - hitRateScore (0-1): recent cache_hit_pct sample average / 100
|
|
97
|
+
* - stabilityScore (0-1): 1 - recent prefix_break count / maxExpectedBreaks
|
|
98
|
+
* - churnScore (0-1): 1 - stripe re-assignment rate (derived from prefix_break classifications)
|
|
99
|
+
*
|
|
100
|
+
* Weights: hit rate 50%, prefix stability 30%, churn 20%.
|
|
101
|
+
*
|
|
102
|
+
* PREVENT-PI-004: guardrails-allow PREVENT-PI-004: local SQLite read, no network.
|
|
103
|
+
* PREVENT-001: readPerfSamples returns empty array when no data; handled.
|
|
104
|
+
* PREVENT-011: no `any` — all arrays are typed PerfSampleRow[].
|
|
105
|
+
*/
|
|
106
|
+
function tryComputeCacheHealth(
|
|
107
|
+
stateDir: string,
|
|
108
|
+
currHitPct: number,
|
|
109
|
+
runtime: MegaRuntime,
|
|
110
|
+
): void {
|
|
111
|
+
try {
|
|
112
|
+
const now = Date.now();
|
|
113
|
+
const windowMs = 300_000; // 5-minute lookback
|
|
114
|
+
const sinceTs = now - windowMs;
|
|
115
|
+
|
|
116
|
+
// Read recent cache_hit_pct and prefix_break samples.
|
|
117
|
+
const hitSamples = readPerfSamples(stateDir, sinceTs, "cache_hit_pct");
|
|
118
|
+
const breakSamples = readPerfSamples(stateDir, sinceTs, "prefix_break");
|
|
119
|
+
|
|
120
|
+
// hitRateScore: average of recent cache hit percentages / 100.
|
|
121
|
+
let hitRateAvg = currHitPct;
|
|
122
|
+
if (hitSamples.length > 0) {
|
|
123
|
+
const sum = hitSamples.reduce((a, r) => a + r.value, 0);
|
|
124
|
+
hitRateAvg = sum / hitSamples.length;
|
|
125
|
+
}
|
|
126
|
+
const hitRateScore = Math.max(0, Math.min(1, hitRateAvg / 100));
|
|
127
|
+
|
|
128
|
+
// stabilityScore: penalize for frequent prefix breaks.
|
|
129
|
+
// Assume at most 2 breaks in 5min is tolerable.
|
|
130
|
+
const maxExpectedBreaks = 2;
|
|
131
|
+
const stabilityScore = Math.max(
|
|
132
|
+
0,
|
|
133
|
+
1 - breakSamples.length / maxExpectedBreaks,
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
// churnScore: if recent breaks have varied causes, penalize.
|
|
137
|
+
const causeVariety = new Set(
|
|
138
|
+
breakSamples.map((r) => {
|
|
139
|
+
const m = typeof r.meta === "string" ? (JSON.parse(r.meta) as Record<string, unknown>) : null;
|
|
140
|
+
return typeof m?.cause === "string" ? m.cause : "other";
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
const churnPenalty = Math.min(1, causeVariety.size / 4); // 4 possible causes
|
|
144
|
+
const churnScore = 1 - churnPenalty;
|
|
145
|
+
|
|
146
|
+
// Composite: hitRate dominates.
|
|
147
|
+
const composite =
|
|
148
|
+
hitRateScore * 0.5 + stabilityScore * 0.3 + churnScore * 0.2;
|
|
149
|
+
|
|
150
|
+
// Emit degradation event when score < 0.6.
|
|
151
|
+
if (composite < 0.6) {
|
|
152
|
+
const prevScore = runtime.rt._lastCacheHealthScore ?? 1;
|
|
153
|
+
runtime.rt._lastCacheHealthScore = composite;
|
|
154
|
+
|
|
155
|
+
recordPerfSample(
|
|
156
|
+
stateDir,
|
|
157
|
+
"cache_health",
|
|
158
|
+
composite,
|
|
159
|
+
{
|
|
160
|
+
hitRateScore: Math.round(hitRateScore * 1000) / 1000,
|
|
161
|
+
stabilityScore: Math.round(stabilityScore * 1000) / 1000,
|
|
162
|
+
churnScore: Math.round(churnScore * 1000) / 1000,
|
|
163
|
+
breakCount: breakSamples.length,
|
|
164
|
+
hitSampleCount: hitSamples.length,
|
|
165
|
+
currHitPct,
|
|
166
|
+
trend: composite < prevScore ? "falling" : "stable",
|
|
167
|
+
},
|
|
168
|
+
); // guardrails-allow PREVENT-PI-004: local SQLite write (loopback dashboard event)
|
|
169
|
+
|
|
170
|
+
// Structured log line for the host to pick up.
|
|
171
|
+
const level = composite < 0.4 ? "error" : "warn";
|
|
172
|
+
// eslint-disable-next-line no-console
|
|
173
|
+
console.log(
|
|
174
|
+
JSON.stringify({
|
|
175
|
+
ts: now,
|
|
176
|
+
event: `cache_health_${level}`,
|
|
177
|
+
composite: Math.round(composite * 1000) / 1000,
|
|
178
|
+
hitRateAvg: Math.round(hitRateAvg * 10) / 10,
|
|
179
|
+
breakCount: breakSamples.length,
|
|
180
|
+
trend: composite < prevScore ? "falling" : "stable",
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
} else {
|
|
184
|
+
// Still healthy — just update the stored score.
|
|
185
|
+
runtime.rt._lastCacheHealthScore = composite;
|
|
186
|
+
}
|
|
187
|
+
} catch {
|
|
188
|
+
/* non-fatal: cache health computation never blocks */
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
82
192
|
/** Register perf instrumentation handlers + start the 5s cpu/mem interval. */
|
|
83
193
|
export function registerPerfHandler(
|
|
84
194
|
pi: ExtensionAPI,
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* extensions/mega-events/separated-prompt.ts — A1+A2 PLAN_V2 Phase 2+3
|
|
3
|
+
* Message Separation + Cache-Optimized Prompt Builder:
|
|
4
|
+
* - buildSeparatedPrompt(): Basic 4-layer message separation (Phase 2).
|
|
5
|
+
* - buildCacheOptimizedPrompt(): Insert Layer 2 (vector-optimized stable
|
|
6
|
+
* context) between summary (Layer 1) and thread (Layer 3), ordered by
|
|
7
|
+
* stripe stability DESC (Phase 3).
|
|
8
|
+
* - detectTopicShift(): Cosine-similarity-based topic-shift detection.
|
|
9
|
+
* - cosineSimilarity(): cosine similarity of two Float32Array embeddings.
|
|
10
|
+
*
|
|
11
|
+
* Layer order (Phase 3): 0 (system) -> 1 (summary) -> 2 (cache stripes) ->
|
|
12
|
+
* 3 (thread: user/assistant turns) -> 4 (tool results at tail).
|
|
13
|
+
*
|
|
14
|
+
* Feature-gated by MEGACOMPACT_CACHE_STRIPING (default OFF).
|
|
15
|
+
* MEGACOMPACT_MESSAGE_SEPARATION must also be ON for any layering to occur.
|
|
16
|
+
* Flag-OFF = byte-identical to pre-sprint — returns messages unchanged.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
20
|
+
import { openStore } from "../../src/store/sqlite/utils.js";
|
|
21
|
+
import { getStateDir } from "../../src/store.js";
|
|
22
|
+
|
|
23
|
+
// ─── Exported types ──────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
export interface SeparatedPromptOptions {
|
|
26
|
+
/** Path to the SQLite store directory. Defaults to getStateDir(). */
|
|
27
|
+
stateDir?: string;
|
|
28
|
+
/** Conversation/agent ID for stripe lookups. Defaults to 'main'. */
|
|
29
|
+
conversationId?: string;
|
|
30
|
+
/** Current turn index for embedding_cache lookups. Defaults to 0. */
|
|
31
|
+
turnIndex?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface CacheStripeRow {
|
|
35
|
+
chunk_id: string;
|
|
36
|
+
stripe: number;
|
|
37
|
+
stability: number;
|
|
38
|
+
assigned_at: number;
|
|
39
|
+
epoch_id: string | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface EmbeddingCacheRow {
|
|
43
|
+
content_hash: string;
|
|
44
|
+
embedding: Buffer;
|
|
45
|
+
computed_at: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ─── Constants ───────────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
/** Default stripe limit: top 10 most stable chunks. */
|
|
51
|
+
const DEFAULT_STRIPE_LIMIT = 10;
|
|
52
|
+
|
|
53
|
+
/** Topic-shift threshold: similarity below this triggers a stripe refresh. */
|
|
54
|
+
const TOPIC_SHIFT_THRESHOLD = 0.7;
|
|
55
|
+
|
|
56
|
+
// ─── Build separated prompt (Phase 2) ────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Reorganize messages into stable layers for better prompt caching.
|
|
60
|
+
*
|
|
61
|
+
* Layer order: 0 (system) -> 1 (summary) -> 3 (thread: user/assistant turns) ->
|
|
62
|
+
* 4 (tool results at tail).
|
|
63
|
+
*
|
|
64
|
+
* Feature gate: MEGACOMPACT_MESSAGE_SEPARATION env var. When OFF (default),
|
|
65
|
+
* returns `messages` unchanged (byte-identical to pre-sprint).
|
|
66
|
+
*/
|
|
67
|
+
export function buildSeparatedPrompt(
|
|
68
|
+
messages: AgentMessage[],
|
|
69
|
+
_opts?: SeparatedPromptOptions,
|
|
70
|
+
): AgentMessage[] {
|
|
71
|
+
const flag = process.env.MEGACOMPACT_MESSAGE_SEPARATION;
|
|
72
|
+
if (flag === "0" || flag === "false" || flag === undefined || flag === "") {
|
|
73
|
+
return messages;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// pi's AgentMessage union has no "system" role — the system prompt lives in
|
|
77
|
+
// AgentState.systemPrompt, separate from this array. The cache-relevant,
|
|
78
|
+
// low-risk transformation is moving volatile tool results/executions to the
|
|
79
|
+
// tail so the stable prefix (user/assistant/summaries/custom) stays contiguous.
|
|
80
|
+
// Discriminate by role only — never reach into `.content`/`.tool_calls`,
|
|
81
|
+
// which are variant-specific and require narrowing.
|
|
82
|
+
const main: AgentMessage[] = [];
|
|
83
|
+
const tail: AgentMessage[] = [];
|
|
84
|
+
for (const m of messages) {
|
|
85
|
+
if (m.role === "toolResult" || m.role === "bashExecution") {
|
|
86
|
+
tail.push(m);
|
|
87
|
+
} else {
|
|
88
|
+
main.push(m);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (tail.length === 0) return messages; // nothing to reorder — byte-identical
|
|
92
|
+
return [...main, ...tail];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ─── Cosine similarity ───────────────────────────────────────────────────────
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Compute cosine similarity between two Float32Array embeddings.
|
|
99
|
+
* Returns a value in [0, 1] where 1 = identical direction.
|
|
100
|
+
* Returns 0 if either vector is zero or inputs are mismatched.
|
|
101
|
+
*/
|
|
102
|
+
export function cosineSimilarity(a: Float32Array, b: Float32Array): number {
|
|
103
|
+
if (a.length !== b.length || a.length === 0) return 0;
|
|
104
|
+
|
|
105
|
+
let dot = 0;
|
|
106
|
+
let normA = 0;
|
|
107
|
+
let normB = 0;
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < a.length; i++) {
|
|
110
|
+
dot += a[i] * b[i];
|
|
111
|
+
normA += a[i] * a[i];
|
|
112
|
+
normB += b[i] * b[i];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const denom = Math.sqrt(normA) * Math.sqrt(normB);
|
|
116
|
+
if (denom === 0) return 0;
|
|
117
|
+
|
|
118
|
+
return dot / denom;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ─── Buffer ↔ Float32Array helpers ───────────────────────────────────────────
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Decode a Float32Array from a Buffer (little-endian float32 blob).
|
|
125
|
+
* Returns null on empty/undefined input.
|
|
126
|
+
*/
|
|
127
|
+
export function decodeEmbeddingBlob(buf: Buffer | undefined | null): Float32Array | null {
|
|
128
|
+
if (!buf || buf.length === 0) return null;
|
|
129
|
+
return new Float32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Encode a Float32Array into a Buffer for DB storage.
|
|
134
|
+
*/
|
|
135
|
+
export function encodeEmbeddingBlob(vec: Float32Array): Buffer {
|
|
136
|
+
return Buffer.from(vec.buffer, vec.byteOffset, vec.byteLength);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ─── Topic-shift detection ───────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Detect whether the conversation topic has shifted between the current turn
|
|
143
|
+
* and the previous turn. Uses cosine similarity between topic embeddings.
|
|
144
|
+
*
|
|
145
|
+
* Returns `true` when similarity < TOPIC_SHIFT_THRESHOLD (0.7), meaning the
|
|
146
|
+
* stable cache prefix may need reordering.
|
|
147
|
+
*
|
|
148
|
+
* Returns `false` (no shift) when either embedding is missing.
|
|
149
|
+
*/
|
|
150
|
+
export function detectTopicShift(
|
|
151
|
+
currentEmb: Float32Array | null | undefined,
|
|
152
|
+
previousEmb: Float32Array | null | undefined,
|
|
153
|
+
): boolean {
|
|
154
|
+
if (!currentEmb || !previousEmb) return false;
|
|
155
|
+
return cosineSimilarity(currentEmb, previousEmb) < TOPIC_SHIFT_THRESHOLD;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ─── Load embeddings from store ──────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Load the topic embedding for a given turn from the embedding_cache table.
|
|
162
|
+
* Returns null if no embedding is stored for that turn.
|
|
163
|
+
*/
|
|
164
|
+
export function loadTopicEmbedding(
|
|
165
|
+
stateDir: string,
|
|
166
|
+
conversationId: string,
|
|
167
|
+
turnIndex: number,
|
|
168
|
+
): Float32Array | null {
|
|
169
|
+
const db = openStore(stateDir);
|
|
170
|
+
try {
|
|
171
|
+
const row = db.prepare(
|
|
172
|
+
`SELECT embedding FROM embedding_cache WHERE content_hash = ?`,
|
|
173
|
+
).get(`topic:${conversationId}:${turnIndex}`) as { embedding: Buffer } | undefined;
|
|
174
|
+
|
|
175
|
+
if (!row) return null;
|
|
176
|
+
return decodeEmbeddingBlob(row.embedding);
|
|
177
|
+
} finally {
|
|
178
|
+
db.close();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Store a topic embedding for a given turn in the embedding_cache table.
|
|
184
|
+
* Idempotent (INSERT OR REPLACE).
|
|
185
|
+
*/
|
|
186
|
+
export function storeTopicEmbedding(
|
|
187
|
+
stateDir: string,
|
|
188
|
+
conversationId: string,
|
|
189
|
+
turnIndex: number,
|
|
190
|
+
embedding: Float32Array,
|
|
191
|
+
): void {
|
|
192
|
+
const db = openStore(stateDir);
|
|
193
|
+
try {
|
|
194
|
+
const contentHash = `topic:${conversationId}:${turnIndex}`;
|
|
195
|
+
const blob = encodeEmbeddingBlob(embedding);
|
|
196
|
+
db.prepare(
|
|
197
|
+
`INSERT OR REPLACE INTO embedding_cache (content_hash, embedding, computed_at) VALUES (?, ?, ?)`,
|
|
198
|
+
).run(contentHash, blob, Date.now());
|
|
199
|
+
} finally {
|
|
200
|
+
db.close();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// ─── Cache stripe management ─────────────────────────────────────────────────
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Refresh cache stripe assignments from the cache_stripes table for a given
|
|
208
|
+
* conversation/epoch. Returns rows ordered by stability DESC.
|
|
209
|
+
*
|
|
210
|
+
* This is called at the start of each turn (or on topic-shift) to ensure the
|
|
211
|
+
* stripe assignments reflect the latest stability scores.
|
|
212
|
+
*/
|
|
213
|
+
export function refreshStripeAssignments(
|
|
214
|
+
stateDir: string,
|
|
215
|
+
epochId: string,
|
|
216
|
+
limit = DEFAULT_STRIPE_LIMIT,
|
|
217
|
+
): CacheStripeRow[] {
|
|
218
|
+
const db = openStore(stateDir);
|
|
219
|
+
try {
|
|
220
|
+
const rows = db.prepare(
|
|
221
|
+
`SELECT chunk_id, stripe, stability, assigned_at, epoch_id
|
|
222
|
+
FROM cache_stripes
|
|
223
|
+
WHERE epoch_id = ?
|
|
224
|
+
ORDER BY stability DESC
|
|
225
|
+
LIMIT ?`,
|
|
226
|
+
).all(epochId, limit) as unknown as CacheStripeRow[];
|
|
227
|
+
|
|
228
|
+
return rows;
|
|
229
|
+
} finally {
|
|
230
|
+
db.close();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ─── Build cache-optimized prompt (Phase 3) ──────────────────────────────────
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Build a cache-optimized prompt with 5 layers:
|
|
238
|
+
* 0 (system) -> 1 (summary) -> 2 (cache stripes) -> 3 (thread) -> 4 (tool)
|
|
239
|
+
*
|
|
240
|
+
* Feature gate: MEGACOMPACT_CACHE_STRIPING (default OFF). When OFF, delegates
|
|
241
|
+
* to buildSeparatedPrompt (byte-identical to Phase 2 behavior).
|
|
242
|
+
*
|
|
243
|
+
* When ON but MEGACOMPACT_MESSAGE_SEPARATION is OFF, also delegates to
|
|
244
|
+
* buildSeparatedPrompt (which returns messages unchanged).
|
|
245
|
+
*/
|
|
246
|
+
export function buildCacheOptimizedPrompt(
|
|
247
|
+
messages: AgentMessage[],
|
|
248
|
+
opts?: SeparatedPromptOptions,
|
|
249
|
+
): AgentMessage[] {
|
|
250
|
+
const flag = process.env.MEGACOMPACT_CACHE_STRIPING;
|
|
251
|
+
if (flag === "0" || flag === "false" || flag === undefined || flag === "") {
|
|
252
|
+
// Flag OFF: delegate to buildSeparatedPrompt (byte-identical).
|
|
253
|
+
return buildSeparatedPrompt(messages, opts);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Build the base 4-layer structure first.
|
|
257
|
+
const base = buildSeparatedPrompt(messages, opts);
|
|
258
|
+
|
|
259
|
+
// If base equals messages, separation is OFF — return unchanged.
|
|
260
|
+
if (base === messages) return base;
|
|
261
|
+
|
|
262
|
+
const stateDir = opts?.stateDir ?? getStateDir();
|
|
263
|
+
|
|
264
|
+
// Layer 1 = leading branch/compaction summaries (the stable summary layer).
|
|
265
|
+
// There is no "system" role in pi's AgentMessage union, so Layer 0 is empty.
|
|
266
|
+
// Count leading summaries to find where to insert the cache-stripe layer.
|
|
267
|
+
let layer1End = 0;
|
|
268
|
+
for (let i = 0; i < base.length; i++) {
|
|
269
|
+
const r = base[i].role;
|
|
270
|
+
if (r === "branchSummary" || r === "compactionSummary") {
|
|
271
|
+
layer1End = i + 1;
|
|
272
|
+
} else {
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Lay out the result: Layer 1 (summaries) first.
|
|
278
|
+
const result: AgentMessage[] = base.slice(0, layer1End);
|
|
279
|
+
|
|
280
|
+
// Insert Layer 2: cache stripes (stable context ordered by stability DESC).
|
|
281
|
+
// Try to find the most recent epoch.
|
|
282
|
+
const db = openStore(stateDir);
|
|
283
|
+
let epochId: string | null = null;
|
|
284
|
+
try {
|
|
285
|
+
const epoch = db.prepare(
|
|
286
|
+
`SELECT epoch_id FROM checkpoint_epochs ORDER BY created_at DESC LIMIT 1`,
|
|
287
|
+
).get() as { epoch_id: string } | undefined;
|
|
288
|
+
epochId = epoch?.epoch_id ?? null;
|
|
289
|
+
} finally {
|
|
290
|
+
db.close();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (epochId) {
|
|
294
|
+
const stripes = refreshStripeAssignments(stateDir, epochId);
|
|
295
|
+
if (stripes.length > 0) {
|
|
296
|
+
// Build a single user message with concatenated stripe content.
|
|
297
|
+
const stripeBlocks: string[] = [];
|
|
298
|
+
for (const s of stripes) {
|
|
299
|
+
// Load chunk content from context_chunks table.
|
|
300
|
+
const db2 = openStore(stateDir);
|
|
301
|
+
try {
|
|
302
|
+
const chunk = db2.prepare(
|
|
303
|
+
`SELECT normalized_text FROM context_chunks WHERE id = ?`,
|
|
304
|
+
).get(s.chunk_id) as { normalized_text: string } | undefined;
|
|
305
|
+
if (chunk?.normalized_text) {
|
|
306
|
+
stripeBlocks.push(chunk.normalized_text);
|
|
307
|
+
}
|
|
308
|
+
} finally {
|
|
309
|
+
db2.close();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (stripeBlocks.length > 0) {
|
|
314
|
+
result.push({
|
|
315
|
+
role: "user",
|
|
316
|
+
content: stripeBlocks.join("\n\n---\n\n"),
|
|
317
|
+
timestamp: Date.now(),
|
|
318
|
+
} as AgentMessage);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Append remaining layers (3: thread, 4: tool results).
|
|
324
|
+
for (let i = layer1End; i < base.length; i++) {
|
|
325
|
+
result.push(base[i]);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mega-runtime/env-loader.ts — load `.mega-compact.env` from the state dir.
|
|
3
|
+
*
|
|
4
|
+
* The /mega-setup wizard and the dashboard Setup tab write a `.mega-compact.env`
|
|
5
|
+
* file with the user's chosen embedder configuration. Without a loader, that
|
|
6
|
+
* file is write-only dead weight — config is read from process.env at startup
|
|
7
|
+
* (mega-config.ts:loadConfig), so the file was never applied.
|
|
8
|
+
*
|
|
9
|
+
* This loader runs BEFORE loadConfig in the extension entry. It parses the
|
|
10
|
+
* file (KEY=VALUE lines, # comments, quoted values) and applies each key to
|
|
11
|
+
* process.env ONLY if it is not already set — so shell profile / inline env
|
|
12
|
+
* vars always win over the file (the user can override without editing it).
|
|
13
|
+
*
|
|
14
|
+
* Non-fatal: a missing or malformed file never breaks the extension.
|
|
15
|
+
*
|
|
16
|
+
* PREVENT-PI-004: reads a local file only — no network.
|
|
17
|
+
* PREVENT-001: no JSON here; line parser is null/empty-safe.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Load `.mega-compact.env` from `stateDir` into `process.env`.
|
|
25
|
+
* Keys already set in the environment win (file does not override shell).
|
|
26
|
+
*/
|
|
27
|
+
export function loadMegaEnv(stateDir: string): void {
|
|
28
|
+
const envPath = join(stateDir, ".mega-compact.env");
|
|
29
|
+
if (!existsSync(envPath)) return;
|
|
30
|
+
let content: string;
|
|
31
|
+
try {
|
|
32
|
+
content = readFileSync(envPath, "utf-8");
|
|
33
|
+
} catch {
|
|
34
|
+
return; // unreadable — non-fatal
|
|
35
|
+
}
|
|
36
|
+
for (const line of content.split("\n")) {
|
|
37
|
+
const trimmed = line.trim();
|
|
38
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
39
|
+
const eq = trimmed.indexOf("=");
|
|
40
|
+
if (eq < 0) continue;
|
|
41
|
+
const key = trimmed.slice(0, eq).trim();
|
|
42
|
+
if (!key) continue;
|
|
43
|
+
let val = trimmed.slice(eq + 1).trim();
|
|
44
|
+
if (
|
|
45
|
+
(val.startsWith('"') && val.endsWith('"')) ||
|
|
46
|
+
(val.startsWith("'") && val.endsWith("'"))
|
|
47
|
+
) {
|
|
48
|
+
val = val.slice(1, -1);
|
|
49
|
+
}
|
|
50
|
+
// Shell / inline env vars win over the file.
|
|
51
|
+
if (process.env[key] === undefined) {
|
|
52
|
+
process.env[key] = val;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -42,6 +42,8 @@ export interface SessionRuntime {
|
|
|
42
42
|
lastInjectAt: number | null; // last tail injection this session
|
|
43
43
|
// S53A: previous cache_hit_pct for drop detection.
|
|
44
44
|
_prevCacheHitPct: number | null;
|
|
45
|
+
// A3: rolling cache health score (0-1) cached between turns for trend detection.
|
|
46
|
+
_lastCacheHealthScore: number | undefined;
|
|
45
47
|
lastNativeCompactAt: number | null; // COMPACT-DEDUP FIX: wall-clock ms of the last NATIVE pi compaction (session_compact event) — used by the agent_end/legacy race guard to skip a redundant ctx.compact() that would throw "Already compacted".
|
|
46
48
|
// S25: live dashboard counters (reset on session_start, mirrored to SQLite).
|
|
47
49
|
compactCount: number; // compactions performed this session-instance
|
|
@@ -267,6 +267,7 @@ export function snapshotImpl(
|
|
|
267
267
|
armed,
|
|
268
268
|
modelSnap,
|
|
269
269
|
providerCachePct,
|
|
270
|
+
perTurnCacheHitPct: self.rt._prevCacheHitPct ?? undefined,
|
|
270
271
|
});
|
|
271
272
|
self.widgetData = _snapResult.widgetData;
|
|
272
273
|
// S33: consume the flare after copying it into widgetData so it fires
|
|
@@ -106,6 +106,11 @@ export interface SnapshotInput {
|
|
|
106
106
|
} | null;
|
|
107
107
|
|
|
108
108
|
lastActivityAt: number;
|
|
109
|
+
|
|
110
|
+
/** A3: per-turn cache hit percentage (most recent turn's ratio, separate
|
|
111
|
+
* from providerCachePct which is the running average). Optional — when null
|
|
112
|
+
* the cache-hit line is omitted from the widget. */
|
|
113
|
+
perTurnCacheHitPct?: number;
|
|
109
114
|
}
|
|
110
115
|
|
|
111
116
|
export interface SnapshotResult {
|
|
@@ -220,6 +225,7 @@ export function computeMegaSnapshot(p: SnapshotInput): SnapshotResult {
|
|
|
220
225
|
cachePct,
|
|
221
226
|
megaCacheFlare: p.megaCacheFlare,
|
|
222
227
|
megaCacheFlarePct: p.megaCacheFlarePct,
|
|
228
|
+
perTurnCacheHitPct: p.perTurnCacheHitPct ?? cachePct,
|
|
223
229
|
levelUpFlare: p.levelUpFlare,
|
|
224
230
|
achievementFlare: p.achievementFlare,
|
|
225
231
|
achievementFlareTitles: p.achievementFlareTitles,
|
|
@@ -77,4 +77,7 @@ export interface WidgetData {
|
|
|
77
77
|
startedAt: number;
|
|
78
78
|
durationMs: number;
|
|
79
79
|
} | null;
|
|
80
|
+
/** A3: per-turn cache hit percentage (most recent turn's hit/miss ratio,
|
|
81
|
+
* 0–100). Separate from cachePct (running average) for trend visibility. */
|
|
82
|
+
perTurnCacheHitPct?: number;
|
|
80
83
|
}
|
|
@@ -131,6 +131,11 @@ export function buildWidgetLines(
|
|
|
131
131
|
`${C.gray}mem${C.reset} ${wd.embedderName} · ${wd.chk} chunks · ${C.blue}comp ${wd.compStr}${C.reset}`,
|
|
132
132
|
`${C.gray}drift${C.reset} ${wd.driftStatus === "ok" ? C.green : C.amber}${wd.driftStatus}${C.reset}`,
|
|
133
133
|
`${C.gray}compact${C.reset} ${sinceCompactStr(wd.sinceCompact)}`,
|
|
134
|
+
...(wd.perTurnCacheHitPct != null
|
|
135
|
+
? [
|
|
136
|
+
`${C.gray}cache hit${C.reset} this turn ${C.green}${Math.round(wd.perTurnCacheHitPct)}%${C.reset}`,
|
|
137
|
+
]
|
|
138
|
+
: []),
|
|
134
139
|
].join(sep);
|
|
135
140
|
// Wrap to terminal width and pad each line
|
|
136
141
|
const wrapped = wrapLine(content, width - 2, panelBg); // 2-char indent
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-mega-compact",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.6",
|
|
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",
|