pi-mega-compact 0.11.13 → 0.12.0

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 (71) hide show
  1. package/dist/extensions/dashboard-server/routes-health.js +81 -0
  2. package/dist/extensions/dashboard-server/routes.js +1 -0
  3. package/dist/extensions/dashboard-server/server.js +5 -1
  4. package/dist/extensions/mega-config.js +5 -0
  5. package/dist/extensions/mega-events/agent-handlers.js +5 -0
  6. package/dist/extensions/mega-events/health-handler.js +156 -0
  7. package/dist/extensions/mega-runtime/runtime.js +6 -0
  8. package/dist/src/contextHealth/cachePoison.js +183 -0
  9. package/dist/src/contextHealth/drift.js +89 -0
  10. package/dist/src/contextHealth/outputQuality.js +112 -0
  11. package/dist/src/contextHealth.js +110 -0
  12. package/dist/src/store/sqlite/context-health.js +193 -0
  13. package/dist/src/store/sqlite/schema.js +43 -1
  14. package/dist/src/store/sqlite.js +1 -0
  15. package/extensions/dashboard-client/dist/assets/{AchievementTiles-9KQgKBEh.js → AchievementTiles-DeQ0qnBX.js} +2 -2
  16. package/extensions/dashboard-client/dist/assets/{AchievementTiles-9KQgKBEh.js.map → AchievementTiles-DeQ0qnBX.js.map} +1 -1
  17. package/extensions/dashboard-client/dist/assets/{AchievementsTab-B6xoQTEa.js → AchievementsTab-Dtk_RJfs.js} +2 -2
  18. package/extensions/dashboard-client/dist/assets/{AchievementsTab-B6xoQTEa.js.map → AchievementsTab-Dtk_RJfs.js.map} +1 -1
  19. package/extensions/dashboard-client/dist/assets/{CacheTab-Dkm9rawo.js → CacheTab-kWcQt3PI.js} +2 -2
  20. package/extensions/dashboard-client/dist/assets/{CacheTab-Dkm9rawo.js.map → CacheTab-kWcQt3PI.js.map} +1 -1
  21. package/extensions/dashboard-client/dist/assets/{ConfigTab-Bd_v_OUF.js → ConfigTab-UFlo73fR.js} +2 -2
  22. package/extensions/dashboard-client/dist/assets/{ConfigTab-Bd_v_OUF.js.map → ConfigTab-UFlo73fR.js.map} +1 -1
  23. package/extensions/dashboard-client/dist/assets/{EventsTab-CYWoFOdI.js → EventsTab-BENO2N0y.js} +2 -2
  24. package/extensions/dashboard-client/dist/assets/{EventsTab-CYWoFOdI.js.map → EventsTab-BENO2N0y.js.map} +1 -1
  25. package/extensions/dashboard-client/dist/assets/{GameTab-CH7lecNL.js → GameTab-BCJavOML.js} +2 -2
  26. package/extensions/dashboard-client/dist/assets/{GameTab-CH7lecNL.js.map → GameTab-BCJavOML.js.map} +1 -1
  27. package/extensions/dashboard-client/dist/assets/HealthTab-C7iw7rhx.js +2 -0
  28. package/extensions/dashboard-client/dist/assets/HealthTab-C7iw7rhx.js.map +1 -0
  29. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DNZ1IF4t.js → MaintenanceTab-C0rdAMWc.js} +2 -2
  30. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DNZ1IF4t.js.map → MaintenanceTab-C0rdAMWc.js.map} +1 -1
  31. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-tVRREYiG.js → MemoryMapTab-w5BZP3i-.js} +2 -2
  32. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-tVRREYiG.js.map → MemoryMapTab-w5BZP3i-.js.map} +1 -1
  33. package/extensions/dashboard-client/dist/assets/{MetricsTab-CHblYuTY.js → MetricsTab-BBCXa3xS.js} +2 -2
  34. package/extensions/dashboard-client/dist/assets/{MetricsTab-CHblYuTY.js.map → MetricsTab-BBCXa3xS.js.map} +1 -1
  35. package/extensions/dashboard-client/dist/assets/{OverviewTab-Dj0TgFvU.js → OverviewTab-CzABMSAQ.js} +2 -2
  36. package/extensions/dashboard-client/dist/assets/{OverviewTab-Dj0TgFvU.js.map → OverviewTab-CzABMSAQ.js.map} +1 -1
  37. package/extensions/dashboard-client/dist/assets/{ReposTab-BTJcfc1W.js → ReposTab-DFiM_Ubg.js} +2 -2
  38. package/extensions/dashboard-client/dist/assets/{ReposTab-BTJcfc1W.js.map → ReposTab-DFiM_Ubg.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/{SessionsTab-C8tU8K5u.js → SessionsTab-BtW4bees.js} +2 -2
  40. package/extensions/dashboard-client/dist/assets/{SessionsTab-C8tU8K5u.js.map → SessionsTab-BtW4bees.js.map} +1 -1
  41. package/extensions/dashboard-client/dist/assets/{SetupTab-BdxGMo6O.js → SetupTab-Duy1LJhg.js} +2 -2
  42. package/extensions/dashboard-client/dist/assets/{SetupTab-BdxGMo6O.js.map → SetupTab-Duy1LJhg.js.map} +1 -1
  43. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-BWZSARzT.js → TimeSavedCard-CC13EXij.js} +2 -2
  44. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-BWZSARzT.js.map → TimeSavedCard-CC13EXij.js.map} +1 -1
  45. package/extensions/dashboard-client/dist/assets/{TopicsTab-BCJwPYl8.js → TopicsTab-BrTB8uEK.js} +2 -2
  46. package/extensions/dashboard-client/dist/assets/{TopicsTab-BCJwPYl8.js.map → TopicsTab-BrTB8uEK.js.map} +1 -1
  47. package/extensions/dashboard-client/dist/assets/{TurnsTab-DQFTSDpz.js → TurnsTab-BJK7FIHu.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{TurnsTab-DQFTSDpz.js.map → TurnsTab-BJK7FIHu.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/{index-DrrDswj-.js → index-B8k19B9g.js} +11 -11
  50. package/extensions/dashboard-client/dist/assets/{index-DrrDswj-.js.map → index-B8k19B9g.js.map} +1 -1
  51. package/extensions/dashboard-client/dist/assets/{useSSE-o4tteeTs.js → useSSE-CWBB4Me7.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{useSSE-o4tteeTs.js.map → useSSE-CWBB4Me7.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/index.html +1 -1
  54. package/extensions/dashboard-client/src/App.tsx +5 -1
  55. package/extensions/dashboard-client/src/api/health.ts +64 -0
  56. package/extensions/dashboard-client/src/tabs/HealthTab.tsx +165 -0
  57. package/extensions/dashboard-server/routes-health.ts +104 -0
  58. package/extensions/dashboard-server/routes.ts +1 -0
  59. package/extensions/dashboard-server/server.ts +4 -0
  60. package/extensions/mega-config.ts +15 -0
  61. package/extensions/mega-events/agent-handlers.ts +6 -0
  62. package/extensions/mega-events/health-handler.ts +190 -0
  63. package/extensions/mega-runtime/runtime.ts +7 -0
  64. package/package.json +1 -1
  65. package/src/contextHealth/cachePoison.ts +243 -0
  66. package/src/contextHealth/drift.ts +107 -0
  67. package/src/contextHealth/outputQuality.ts +139 -0
  68. package/src/contextHealth.ts +191 -0
  69. package/src/store/sqlite/context-health.ts +342 -0
  70. package/src/store/sqlite/schema.ts +43 -1
  71. package/src/store/sqlite.ts +1 -0
@@ -0,0 +1,190 @@
1
+ /**
2
+ * health-handler.ts — Context Health turn_end hook (v0.12).
3
+ *
4
+ * Computes a composite 0-1 health score per turn from five sub-scores
5
+ * (drift, output quality, error rate, cache health, cache poison),
6
+ * persists it to the context_health SQLite table, and emits a dashboard
7
+ * event. All paths non-fatal — health monitoring never breaks the agent loop.
8
+ *
9
+ * PREVENT-PI-004: zero network (local TrigramEmbedder + SQLite only).
10
+ */
11
+
12
+ import type { MegaRuntime } from "../mega-runtime.js";
13
+ import type { MegaConfig } from "../mega-config.js";
14
+ import { defaultEmbedder } from "../../src/embedder.js";
15
+ import { computeOutputQuality } from "../../src/contextHealth/outputQuality.js";
16
+ import {
17
+ computeTopicDrift,
18
+ computeErrorEscalation,
19
+ computePrefixInstability,
20
+ computeDriftScore,
21
+ } from "../../src/contextHealth/drift.js";
22
+ import {
23
+ computePrefixHash,
24
+ checkPrefixHash,
25
+ evaluateCachePoison,
26
+ } from "../../src/contextHealth/cachePoison.js";
27
+ import {
28
+ computeHealthScore,
29
+ recordContextHealth,
30
+ type ContextHealthSubScores,
31
+ } from "../../src/contextHealth.js";
32
+ import {
33
+ recordCachePoisonEvent,
34
+ } from "../../src/store/sqlite/context-health.js";
35
+
36
+ const RING_MAX = 5;
37
+
38
+ /** Extract assistant text from the turn_end event message. */
39
+ function extractAssistantText(event: { message?: { role?: string; content?: unknown } }): string {
40
+ const msg = event.message;
41
+ if (!msg || msg.role !== "assistant") return "";
42
+ const c = msg.content;
43
+ if (typeof c === "string") return c;
44
+ if (Array.isArray(c)) {
45
+ return c
46
+ .map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text?: string }).text ?? "") : ""))
47
+ .join(" ");
48
+ }
49
+ return "";
50
+ }
51
+
52
+ /** Extract messages as string array for prefix hash (cache poison Layer 1). */
53
+ function extractMessageTexts(event: { message?: { content?: unknown } }): string[] {
54
+ const c = event?.message?.content;
55
+ if (typeof c === "string") return [c];
56
+ if (Array.isArray(c)) {
57
+ return c.map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text?: string }).text ?? "") : ""));
58
+ }
59
+ return [];
60
+ }
61
+
62
+ interface TurnEvent {
63
+ turnIndex: number;
64
+ message?: { role?: string; content?: unknown; stopReason?: string; usage?: { cacheRead?: number } };
65
+ }
66
+
67
+ /** Handle turn_end: compute + persist context health. Non-fatal. */
68
+ export function handleTurnEndHealth(
69
+ event: TurnEvent,
70
+ runtime: MegaRuntime,
71
+ config: MegaConfig,
72
+ ): void {
73
+ if (!config.contextHealth) return;
74
+ try {
75
+ const text = extractAssistantText(event);
76
+ const embedder = defaultEmbedder();
77
+ const emb = text.length > 0 ? embedder.embed(text) : [];
78
+
79
+ // Output quality sub-score
80
+ let outputQuality = 1.0;
81
+ let repetitionRatio = 0;
82
+ let coherenceScore = 1.0;
83
+ if (config.contextHealthOutputQuality && text.length > 0) {
84
+ const oq = computeOutputQuality(text, embedder);
85
+ outputQuality = oq.score;
86
+ repetitionRatio = oq.repetitionRatio;
87
+ coherenceScore = oq.coherenceScore;
88
+ }
89
+
90
+ // Drift sub-score
91
+ let drift = 1.0;
92
+ if (config.contextHealthDrift) {
93
+ const topic = computeTopicDrift(emb, runtime.recentTurnEmbeddings);
94
+ const error = computeErrorEscalation(runtime.recentErrorCategories);
95
+ const prefix = computePrefixInstability(0, 5);
96
+ drift = computeDriftScore(topic, error, prefix);
97
+ }
98
+
99
+ // Error rate sub-score (1 = no errors)
100
+ const errorScore = computeErrorEscalation(runtime.recentErrorCategories);
101
+
102
+ // Cache health (from perf-handler)
103
+ const cacheHealth = runtime.rt._lastCacheHealthScore ?? 1.0;
104
+
105
+ // Cache poison sub-score
106
+ let cachePoison = 1.0;
107
+ if (config.contextHealthCachePoison) {
108
+ const msgTexts = extractMessageTexts(event);
109
+ const currentHash = computePrefixHash(msgTexts);
110
+ const cacheRead = event.message?.usage?.cacheRead ?? 0;
111
+ const l1 = checkPrefixHash(currentHash, runtime.lastPrefixHash, cacheRead);
112
+ const result = evaluateCachePoison({
113
+ currentHash,
114
+ storedHash: runtime.lastPrefixHash,
115
+ cacheRead,
116
+ qualityByCacheHit: [],
117
+ qualityByCacheMiss: [],
118
+ errorRateCacheHit: 0,
119
+ errorRateCacheMiss: 0,
120
+ sampleCount: 0,
121
+ });
122
+ cachePoison = result.score;
123
+ if (l1.poisoned) {
124
+ try {
125
+ recordCachePoisonEvent(runtime.currentStateDir, {
126
+ ts: Date.now(),
127
+ turnIndex: event.turnIndex,
128
+ sessionId: runtime.rt.sessionId,
129
+ layer: 1,
130
+ detail: l1.detail,
131
+ severity: "warn",
132
+ });
133
+ } catch { /* non-fatal */ }
134
+ }
135
+ runtime.lastPrefixHash = currentHash;
136
+ }
137
+
138
+ // Composite
139
+ const sub: ContextHealthSubScores = {
140
+ drift,
141
+ outputQuality,
142
+ errorRate: errorScore,
143
+ cacheHealth,
144
+ cachePoison,
145
+ };
146
+ const composite = computeHealthScore(sub);
147
+
148
+ // Persist
149
+ recordContextHealth(runtime.currentStateDir, {
150
+ ts: Date.now(),
151
+ turnIndex: event.turnIndex,
152
+ sessionId: runtime.rt.sessionId,
153
+ driftScore: drift,
154
+ outputQuality,
155
+ errorScore,
156
+ cacheHealth,
157
+ cachePoison,
158
+ composite,
159
+ modelId: runtime.currentModel?.modelId,
160
+ repetitionRatio,
161
+ coherenceScore,
162
+ prefixHash: runtime.lastPrefixHash ?? undefined,
163
+ });
164
+
165
+ // Emit dashboard event
166
+ runtime.dashboard.event("context_health", {
167
+ composite,
168
+ drift,
169
+ outputQuality,
170
+ errorRate: errorScore,
171
+ cacheHealth,
172
+ cachePoison,
173
+ turnIndex: event.turnIndex,
174
+ });
175
+
176
+ // Update ring buffers
177
+ runtime.recentTurnEmbeddings.push(emb);
178
+ if (runtime.recentTurnEmbeddings.length > RING_MAX) {
179
+ runtime.recentTurnEmbeddings.shift();
180
+ }
181
+ runtime.recentErrorCategories.push(runtime.lastErrorCategory);
182
+ if (runtime.recentErrorCategories.length > RING_MAX) {
183
+ runtime.recentErrorCategories.shift();
184
+ }
185
+ } catch (e) {
186
+ runtime.logger?.error("context_health_failed", {
187
+ error: String(e instanceof Error ? e.message : e),
188
+ });
189
+ }
190
+ }
@@ -263,6 +263,13 @@ export class MegaRuntime {
263
263
  diagCtxCutNull = 0; // computeLiveTrimCut returned null (anchor/boundary)
264
264
  diagCtxThrown = 0; // live-trim try threw (caught)
265
265
 
266
+ // Context health instrumentation (v0.12): rolling ring buffers for
267
+ // drift detection + cache poison Layer 1 hash baseline.
268
+ recentTurnEmbeddings: number[][] = [];
269
+ recentErrorCategories: (string | null)[] = [];
270
+ lastPrefixHash: string | null = null;
271
+ lastErrorCategory: string | null = null;
272
+
266
273
  /**
267
274
  * S26 capture instrumentation: the "model_snapshots empty → $0.00 cost card"
268
275
  * bug was invisible because captureModel swallowed the DB write in a silent
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.11.13",
3
+ "version": "0.12.0",
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",
@@ -0,0 +1,243 @@
1
+ /**
2
+ * contextHealth/cachePoison.ts — tri-layer KV cache poison validator (v0.9.1).
3
+ *
4
+ * Three validation layers for detecting corrupted KV cache state. Each layer
5
+ * triggers independently; the composite score maps the number of triggered
6
+ * layers to a 0-1 health metric (1 = healthy, 0 = poisoned).
7
+ *
8
+ * Pure functions — zero I/O, zero SQLite, zero network. All inputs are
9
+ * primitive values or arrays passed in; this module makes no external calls
10
+ * and carries no state between invocations.
11
+ *
12
+ * Guardrails: no `any` (PREVENT-011), no un-null-checked JSON.parse
13
+ * (PREVENT-001), zero network (PREVENT-PI-004 trivially satisfied — this
14
+ * module never calls fetch or opens sockets).
15
+ */
16
+
17
+ /** FNV-1a 32-bit offset basis (must match TrigramEmbedder._embedRaw). */
18
+ const FNV_OFFSET_BASIS = 0x811c9dc5;
19
+ /** FNV-1a 32-bit prime (must match TrigramEmbedder._embedRaw). */
20
+ const FNV_PRIME = 0x01000193;
21
+ /** Max input characters for prefix hash (4 KB of UTF-16). */
22
+ const PREFIX_HASH_CAP = 4096;
23
+
24
+ // ─── Layer 1 — Hash validation ──────────────────────────────────────────────
25
+
26
+ /**
27
+ * Compute a stable 32-bit FNV-1a hash over the first 4 KB of the concatenated
28
+ * message text. Returns a lower-case hex string.
29
+ *
30
+ * The algorithm is identical to `TrigramEmbedder._embedRaw`'s internal hash
31
+ * (offset basis 0x811c9dc5, prime 0x01000193), so any change visible to the
32
+ * embedder is also visible here.
33
+ */
34
+ export function computePrefixHash(messages: string[]): string {
35
+ let combined = "";
36
+ for (let i = 0; i < messages.length; i++) {
37
+ combined += messages[i];
38
+ if (combined.length >= PREFIX_HASH_CAP) break;
39
+ }
40
+ const input = combined.length > PREFIX_HASH_CAP
41
+ ? combined.slice(0, PREFIX_HASH_CAP)
42
+ : combined;
43
+
44
+ let h = FNV_OFFSET_BASIS;
45
+ for (let i = 0; i < input.length; i++) {
46
+ h ^= input.charCodeAt(i);
47
+ h = Math.imul(h, FNV_PRIME);
48
+ }
49
+ return (h >>> 0).toString(16).padStart(8, "0");
50
+ }
51
+
52
+ /**
53
+ * Layer 1: detect when the KV cache prefix has been silently mutated.
54
+ *
55
+ * Trigger conditions:
56
+ * - cacheRead > 0 (cache was actually used)
57
+ * - storedHash !== null (we have a baseline from a prior turn)
58
+ * - currentHash !== storedHash (prefix text has drifted)
59
+ *
60
+ * Not triggered when: cache was never read, or no baseline exists (first turn).
61
+ */
62
+ export function checkPrefixHash(
63
+ currentHash: string,
64
+ storedHash: string | null,
65
+ cacheRead: number,
66
+ ): { poisoned: boolean; detail: string } {
67
+ if (cacheRead === 0) {
68
+ return { poisoned: false, detail: "no cache read" };
69
+ }
70
+ if (storedHash === null) {
71
+ return { poisoned: false, detail: "no stored hash baseline" };
72
+ }
73
+ if (currentHash !== storedHash) {
74
+ return {
75
+ poisoned: true,
76
+ detail:
77
+ `prefix hash mismatch: cached prefix changed without cache invalidation (current=${currentHash} stored=${storedHash})`,
78
+ };
79
+ }
80
+ return { poisoned: false, detail: "prefix hash matches baseline" };
81
+ }
82
+
83
+ // ─── Layer 2 — Semantic validation ──────────────────────────────────────────
84
+
85
+ /** Arithmetic mean of a non-empty number array. */
86
+ function mean(values: number[]): number {
87
+ let sum = 0;
88
+ for (const v of values) sum += v;
89
+ return sum / values.length;
90
+ }
91
+
92
+ /**
93
+ * Layer 2: detect when cache-hit outputs are semantically worse than cache-miss
94
+ * outputs.
95
+ *
96
+ * Trigger conditions:
97
+ * - both groups have at least 3 samples (statistical floor)
98
+ * - cache-hit mean quality is more than 0.15 below cache-miss mean quality
99
+ *
100
+ * Not triggered when: insufficient data in either group.
101
+ */
102
+ export function compareOutputQualityByCacheHit(
103
+ qualityByCacheHit: number[],
104
+ qualityByCacheMiss: number[],
105
+ ): { poisoned: boolean; detail: string } {
106
+ const hitN = qualityByCacheHit.length;
107
+ const missN = qualityByCacheMiss.length;
108
+
109
+ if (hitN < 3 || missN < 3) {
110
+ return {
111
+ poisoned: false,
112
+ detail: `insufficient quality samples: hit=${hitN} miss=${missN} (need >= 3 each)`,
113
+ };
114
+ }
115
+
116
+ const hitMean = mean(qualityByCacheHit);
117
+ const missMean = mean(qualityByCacheMiss);
118
+ const threshold = missMean - 0.15;
119
+
120
+ if (hitMean < threshold) {
121
+ return {
122
+ poisoned: true,
123
+ detail:
124
+ `cache-hit quality degraded: hitMean=${hitMean.toFixed(3)} missMean=${missMean.toFixed(3)} (threshold=${threshold.toFixed(3)})`,
125
+ };
126
+ }
127
+ return {
128
+ poisoned: false,
129
+ detail: `quality comparable: hitMean=${hitMean.toFixed(3)} missMean=${missMean.toFixed(3)}`,
130
+ };
131
+ }
132
+
133
+ // ─── Layer 3 — Behavioral validation ────────────────────────────────────────
134
+
135
+ /**
136
+ * Layer 3: detect when errors cluster on cache-hit turns (indicating the cache
137
+ * is returning corrupt state rather than fresh computation catching them).
138
+ *
139
+ * Trigger conditions (all three must hold):
140
+ * - sampleCount >= 5
141
+ * - errorRateCacheHit > errorRateCacheMiss * 2 (hit error rate is >2x miss rate)
142
+ * - errorRateCacheHit > 0.1 (absolute hit error rate is non-trivial)
143
+ *
144
+ * Not triggered when: insufficient observations.
145
+ */
146
+ export function correlateErrorsWithCacheHits(
147
+ errorRateCacheHit: number,
148
+ errorRateCacheMiss: number,
149
+ sampleCount: number,
150
+ ): { poisoned: boolean; detail: string } {
151
+ if (sampleCount < 5) {
152
+ return {
153
+ poisoned: false,
154
+ detail: `insufficient behavioral samples: n=${sampleCount} (need >= 5)`,
155
+ };
156
+ }
157
+ if (
158
+ errorRateCacheHit > errorRateCacheMiss * 2 &&
159
+ errorRateCacheHit > 0.1
160
+ ) {
161
+ return {
162
+ poisoned: true,
163
+ detail:
164
+ `error rate inflated on cache hits: hit=${errorRateCacheHit.toFixed(3)} miss=${errorRateCacheMiss.toFixed(3)} (2x breach + absolute floor)`,
165
+ };
166
+ }
167
+ return {
168
+ poisoned: false,
169
+ detail:
170
+ `error rates normal: hit=${errorRateCacheHit.toFixed(3)} miss=${errorRateCacheMiss.toFixed(3)}`,
171
+ };
172
+ }
173
+
174
+ // ─── Composite score ─────────────────────────────────────────────────────────
175
+
176
+ /**
177
+ * Compute a 0-1 composite cache poison score from layer triggers.
178
+ *
179
+ * | triggered layers | score |
180
+ * |-----------------|-------|
181
+ * | 0 | 1.0 |
182
+ * | 1 | 0.3 |
183
+ * | 2 | 0.15 |
184
+ * | 3 | 0.0 |
185
+ *
186
+ * Higher = healthier. Scores below ~0.3 warrant immediate cache invalidation.
187
+ */
188
+ export function computeCachePoisonScore(
189
+ l1: boolean,
190
+ l2: boolean,
191
+ l3: boolean,
192
+ ): number {
193
+ const triggers = (l1 ? 1 : 0) + (l2 ? 1 : 0) + (l3 ? 1 : 0);
194
+ switch (triggers) {
195
+ case 0: return 1.0;
196
+ case 1: return 0.3;
197
+ case 2: return 0.15;
198
+ case 3: return 0.0;
199
+ default: return 0.0; // should never reach
200
+ }
201
+ }
202
+
203
+ /** Result shape returned by `evaluateCachePoison`. */
204
+ export interface CachePoisonResult {
205
+ score: number;
206
+ layer1: { poisoned: boolean; detail: string };
207
+ layer2: { poisoned: boolean; detail: string };
208
+ layer3: { poisoned: boolean; detail: string };
209
+ }
210
+
211
+ /**
212
+ * Run all three validation layers and return a composite result.
213
+ *
214
+ * All arguments are primitive values; this function is pure and stateless.
215
+ */
216
+ export function evaluateCachePoison(args: {
217
+ currentHash: string;
218
+ storedHash: string | null;
219
+ cacheRead: number;
220
+ qualityByCacheHit: number[];
221
+ qualityByCacheMiss: number[];
222
+ errorRateCacheHit: number;
223
+ errorRateCacheMiss: number;
224
+ sampleCount: number;
225
+ }): CachePoisonResult {
226
+ const l1 = checkPrefixHash(args.currentHash, args.storedHash, args.cacheRead);
227
+ const l2 = compareOutputQualityByCacheHit(
228
+ args.qualityByCacheHit,
229
+ args.qualityByCacheMiss,
230
+ );
231
+ const l3 = correlateErrorsWithCacheHits(
232
+ args.errorRateCacheHit,
233
+ args.errorRateCacheMiss,
234
+ args.sampleCount,
235
+ );
236
+
237
+ return {
238
+ score: computeCachePoisonScore(l1.poisoned, l2.poisoned, l3.poisoned),
239
+ layer1: l1,
240
+ layer2: l2,
241
+ layer3: l3,
242
+ };
243
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * drift.ts — rolling-window context drift detectors.
3
+ *
4
+ * Pure functions; no I/O. All vectors are number[] (the Embedder output type).
5
+ * Cosine similarity is used throughout (PREVENT-PI-004 safe — no network).
6
+ */
7
+ import type { Vector } from "../embedder.js";
8
+ import { cosineSimilarity } from "../embedder.js";
9
+
10
+ // ─── Topic drift ────────────────────────────────────────────────────────────
11
+
12
+ /**
13
+ * Compute topic drift as cosine similarity between the current embedding and
14
+ * the centroid of recent embeddings.
15
+ *
16
+ * Returns 0–1: 1 = no drift (current topic matches the recent window exactly),
17
+ * 0 = completely unrelated. Empty recentEmbs returns 1.0 (baseline, no prior).
18
+ *
19
+ * Centroid: element-wise arithmetic mean of recentEmbs.
20
+ * cosineSimilarity normalizes internally; centroid need not be unit-normalized.
21
+ */
22
+ export function computeTopicDrift(currentEmb: Vector, recentEmbs: Vector[]): number {
23
+ if (recentEmbs.length === 0) return 1.0;
24
+
25
+ const dim = currentEmb.length;
26
+ // Element-wise mean of recent embeddings.
27
+ const centroid = new Array<number>(dim).fill(0);
28
+ for (const emb of recentEmbs) {
29
+ if (emb.length !== dim) continue; // defensive: skip mismatched-dim rows
30
+ for (let i = 0; i < dim; i++) {
31
+ centroid[i] += emb[i];
32
+ }
33
+ }
34
+ for (let i = 0; i < dim; i++) {
35
+ centroid[i] /= recentEmbs.length;
36
+ }
37
+
38
+ return cosineSimilarity(currentEmb, centroid);
39
+ }
40
+
41
+ // ─── Error escalation ───────────────────────────────────────────────────────
42
+
43
+ /**
44
+ * Compute error-rate score from recent error categories.
45
+ *
46
+ * `recentErrorCategories` is a list of error-class strings, or null for
47
+ * non-error turns. Returns 1.0 when the list is empty (no data → assume healthy).
48
+ * The score is 1 - (non-null / total), so an entirely-error list scores 0.
49
+ */
50
+ export function computeErrorEscalation(recentErrorCategories: (string | null)[]): number {
51
+ if (recentErrorCategories.length === 0) return 1.0;
52
+ let errors = 0;
53
+ for (const cat of recentErrorCategories) {
54
+ if (cat !== null) errors++;
55
+ }
56
+ return 1 - errors / recentErrorCategories.length;
57
+ }
58
+
59
+ // ─── Prefix instability ─────────────────────────────────────────────────────
60
+
61
+ /**
62
+ * Compute prefix-stability score using the cache-health formula from
63
+ * perf-handler.ts (tryComputeCacheHealth → stabilityScore).
64
+ *
65
+ * The stability formula from perf-handler.ts:
66
+ * instability = min(1, prefixBreakCount / (windowMinutes / 5 * 2))
67
+ * stabilityScore = max(0, 1 - breakSamples.length / maxExpectedBreaks)
68
+ *
69
+ * Here we expose a direct formula:
70
+ * instability = min(1, prefixBreakCount / (windowMinutes / 5 * 2))
71
+ * score = 1 - instability
72
+ * = max(0, 1 - min(1, prefixBreakCount / (windowMinutes / 5 * 2)))
73
+ *
74
+ * Returns 0–1: 1 = perfectly stable (no prefix breaks), 0 = maximally unstable.
75
+ * The window is expressed in minutes so the caller controls the lookback.
76
+ */
77
+ export function computePrefixInstability(
78
+ prefixBreakCount: number,
79
+ windowMinutes: number,
80
+ ): number {
81
+ if (prefixBreakCount <= 0) return 1.0;
82
+ if (windowMinutes <= 0) return 1.0; // defensive: avoid div-by-zero
83
+ const maxExpectedBreaks = (windowMinutes / 5) * 2;
84
+ const instability = Math.min(1, prefixBreakCount / maxExpectedBreaks);
85
+ return Math.max(0, 1 - instability);
86
+ }
87
+
88
+ // ─── Composite drift score ───────────────────────────────────────────────────
89
+
90
+ /**
91
+ * Weighted composite drift score from three sub-components.
92
+ *
93
+ * Weights are domain-informed:
94
+ * topic drift (40%) — the strongest signal of context-switching
95
+ * error rate (35%) — errors correlate strongly with confusion
96
+ * prefix stability (25%) — cache instability is secondary to content quality
97
+ *
98
+ * Returns 0–1: 1 = no drift / perfectly healthy, 0 = severe drift.
99
+ */
100
+ export function computeDriftScore(
101
+ topic: number,
102
+ error: number,
103
+ prefix: number,
104
+ ): number {
105
+ const raw = topic * 0.4 + error * 0.35 + prefix * 0.25;
106
+ return Math.max(0, Math.min(1, raw));
107
+ }
@@ -0,0 +1,139 @@
1
+ /**
2
+ * outputQuality.ts — pure assistant-output text quality detectors.
3
+ *
4
+ * No I/O beyond the injected Embedder (which is fully local / PREVENT-PI-004).
5
+ * All functions are deterministic and side-effect free.
6
+ */
7
+ import type { Embedder } from "../embedder.js";
8
+ import { cosineSimilarity } from "../embedder.js";
9
+
10
+ // ─── Helpers ────────────────────────────────────────────────────────────────
11
+
12
+ /** Tokenize on whitespace; skip empty tokens. */
13
+ function tokenizeWords(text: string): string[] {
14
+ return text.split(/\s+/).filter((w) => w.length > 0);
15
+ }
16
+
17
+ /** Split on sentence-terminating punctuation; skip empty. */
18
+ function splitSentences(text: string): string[] {
19
+ return text.split(/[.!?]+/).map((s) => s.trim()).filter((s) => s.length > 0);
20
+ }
21
+
22
+ // ─── Detectors ─────────────────────────────────────────────────────────────
23
+
24
+ /**
25
+ * Repetition ratio via 3-gram overlap.
26
+ *
27
+ * Builds sliding 3-word windows over the token stream. Returns the fraction
28
+ * of 3-grams that appear more than once (i.e. are repeated). Range 0–1;
29
+ * values above 0.30 signal likely garbled / looping output.
30
+ */
31
+ export function detectRepetition(text: string): number {
32
+ const words = tokenizeWords(text);
33
+ if (words.length < 3) return 0;
34
+ const count = new Map<string, number>();
35
+ for (let i = 0; i <= words.length - 3; i++) {
36
+ const gram = words.slice(i, i + 3).join(" ");
37
+ count.set(gram, (count.get(gram) ?? 0) + 1);
38
+ }
39
+ const total = words.length - 2;
40
+ if (total === 0) return 0;
41
+ let repeated = 0;
42
+ for (const n of count.values()) {
43
+ if (n > 1) repeated += n;
44
+ }
45
+ // Each duplicate occurrence beyond the first adds to the repeated count.
46
+ // Clamp total to total_3grams so the ratio stays in [0,1].
47
+ return Math.min(1, repeated / total);
48
+ }
49
+
50
+ /**
51
+ * Sentence-level coherence score via adjacent cosine similarity.
52
+ *
53
+ * Splits text into sentences, embeds each, and computes the cosine similarity
54
+ * between every consecutive pair. Returns the average. Range 0–1 (higher =
55
+ * more coherent); fewer than 2 sentences returns 1.0 (cannot measure).
56
+ */
57
+ export function detectCoherence(text: string, embedder: Embedder): number {
58
+ const sentences = splitSentences(text);
59
+ if (sentences.length < 2) return 1.0;
60
+
61
+ let sum = 0;
62
+ let pairCount = 0;
63
+ for (let i = 0; i < sentences.length - 1; i++) {
64
+ const a = embedder.embed(sentences[i]);
65
+ const b = embedder.embed(sentences[i + 1]);
66
+ sum += cosineSimilarity(a, b);
67
+ pairCount++;
68
+ }
69
+ return pairCount > 0 ? sum / pairCount : 1.0;
70
+ }
71
+
72
+ /**
73
+ * Token-salad detector.
74
+ *
75
+ * A word is "recognized" if it has length ≥ 2 AND contains at least one vowel
76
+ * (a / e / i / o / u, case-insensitive). Returns the fraction of unrecognized
77
+ * words. Values above 0.6 suggest scrambled / token-confused output.
78
+ */
79
+ export function detectTokenSalad(text: string): number {
80
+ const words = tokenizeWords(text);
81
+ if (words.length === 0) return 0;
82
+
83
+ const VOWELS = /[aeiou]/i;
84
+ let unrecognized = 0;
85
+ for (const w of words) {
86
+ if (w.length < 2 || !VOWELS.test(w)) unrecognized++;
87
+ }
88
+ return unrecognized / words.length;
89
+ }
90
+
91
+ /**
92
+ * True when the output is entirely absent or whitespace-only.
93
+ */
94
+ export function detectEmptyOutput(text: string): boolean {
95
+ return text.trim().length === 0;
96
+ }
97
+
98
+ // ─── Composite ─────────────────────────────────────────────────────────────
99
+
100
+ /**
101
+ * Compute the full output-quality profile for assistant output text.
102
+ *
103
+ * Returns a score 0–1 (1 = healthy) and the three sub-diagnostics.
104
+ *
105
+ * Penalty weights (sum to 1.0):
106
+ * repetition 40% — repeated 3-grams are the strongest corruption signal
107
+ * coherence 30% — incoherent sentence transitions
108
+ * salad 30% — garbage tokens
109
+ *
110
+ * Empty output is a hard 0.0 (nothing to evaluate).
111
+ */
112
+ export function computeOutputQuality(
113
+ text: string,
114
+ embedder: Embedder,
115
+ ): {
116
+ score: number;
117
+ repetitionRatio: number;
118
+ coherenceScore: number;
119
+ isEmpty: boolean;
120
+ } {
121
+ const isEmpty = detectEmptyOutput(text);
122
+ if (isEmpty) {
123
+ return { score: 0.0, repetitionRatio: 0, coherenceScore: 1.0, isEmpty: true };
124
+ }
125
+
126
+ const repetitionRatio = detectRepetition(text);
127
+ const coherenceScore = detectCoherence(text, embedder);
128
+ const saladRatio = detectTokenSalad(text);
129
+
130
+ const repetitionPenalty = repetitionRatio * 0.4;
131
+ const coherencePenalty = (1 - coherenceScore) * 0.3;
132
+ const saladPenalty = saladRatio * 0.3;
133
+
134
+ const score = Math.max(0, Math.min(1,
135
+ 1.0 - repetitionPenalty - coherencePenalty - saladPenalty,
136
+ ));
137
+
138
+ return { score, repetitionRatio, coherenceScore, isEmpty: false };
139
+ }