pi-mega-compact 0.20.22 → 0.20.24

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 (104) hide show
  1. package/dist/extensions/dashboard-server/resolve-snapshot.js +42 -28
  2. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +2 -0
  3. package/dist/extensions/dashboard-server/routes-setup.js +4 -1
  4. package/dist/extensions/dashboard-server/routes-vector-cortex-crystals.js +11 -3
  5. package/dist/extensions/dashboard-server/routes-vector-cortex-diagnostics.js +7 -2
  6. package/dist/extensions/dashboard-server/routes-vector-cortex-economics.js +1 -0
  7. package/dist/extensions/dashboard-server/routes-vector-cortex-heal.js +18 -6
  8. package/dist/extensions/dashboard-server/routes-vector-cortex-policy.js +1 -0
  9. package/dist/extensions/dashboard-server/routes-vector-cortex-reconstruct.js +10 -3
  10. package/dist/extensions/dashboard-server/routes-vector-cortex-repair.js +13 -5
  11. package/dist/extensions/dashboard-server/routes-vector-cortex-shards.js +12 -4
  12. package/dist/extensions/dashboard-server/vc-event-counts.js +63 -0
  13. package/dist/extensions/mega-events/agent-handlers/turnEndHandler/cacheStripe.js +15 -4
  14. package/dist/extensions/mega-events/context-handler/afterCompact.js +57 -4
  15. package/dist/extensions/mega-events/context-handler/dbMirrorAppend.js +35 -7
  16. package/dist/extensions/mega-events/context-handler/pipelineRun.js +33 -0
  17. package/dist/extensions/mega-events/context-handler/tailResult.js +45 -12
  18. package/dist/extensions/mega-runtime/dashboard-snapshot.js +9 -6
  19. package/dist/extensions/mega-runtime/runtime-snapshot.js +2 -0
  20. package/dist/extensions/mega-runtime/vc-observer.js +98 -0
  21. package/dist/src/cache-stripe-impl.js +52 -214
  22. package/dist/src/cache-stripe-score.js +115 -0
  23. package/extensions/dashboard-client/dist/assets/{AreaChart-BkfB-LSQ.js → AreaChart-BzEedilo.js} +2 -2
  24. package/extensions/dashboard-client/dist/assets/{AreaChart-BkfB-LSQ.js.map → AreaChart-BzEedilo.js.map} +1 -1
  25. package/extensions/dashboard-client/dist/assets/{BarChart-Cs1kogko.js → BarChart-CzAVg1_J.js} +2 -2
  26. package/extensions/dashboard-client/dist/assets/{BarChart-Cs1kogko.js.map → BarChart-CzAVg1_J.js.map} +1 -1
  27. package/extensions/dashboard-client/dist/assets/{CacheTab-BZgaguGr.js → CacheTab-D2-rzTuX.js} +2 -2
  28. package/extensions/dashboard-client/dist/assets/{CacheTab-BZgaguGr.js.map → CacheTab-D2-rzTuX.js.map} +1 -1
  29. package/extensions/dashboard-client/dist/assets/{EventsTab-BFbkI_MO.js → EventsTab-DB7g1Szy.js} +2 -2
  30. package/extensions/dashboard-client/dist/assets/{EventsTab-BFbkI_MO.js.map → EventsTab-DB7g1Szy.js.map} +1 -1
  31. package/extensions/dashboard-client/dist/assets/{HealthTab-CvlOENCX.js → HealthTab-Cvkmi-R3.js} +2 -2
  32. package/extensions/dashboard-client/dist/assets/{HealthTab-CvlOENCX.js.map → HealthTab-Cvkmi-R3.js.map} +1 -1
  33. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtBuz2qL.js → MaintenanceTab-g_udcOO7.js} +2 -2
  34. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtBuz2qL.js.map → MaintenanceTab-g_udcOO7.js.map} +1 -1
  35. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-K4775S4d.js → MemoryMapTab-DAjBaf8P.js} +2 -2
  36. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-K4775S4d.js.map → MemoryMapTab-DAjBaf8P.js.map} +1 -1
  37. package/extensions/dashboard-client/dist/assets/{MetricsTab-DVGcFInz.js → MetricsTab-13dYYocB.js} +2 -2
  38. package/extensions/dashboard-client/dist/assets/{MetricsTab-DVGcFInz.js.map → MetricsTab-13dYYocB.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/{OverviewTab-4LmW5ZCl.js → OverviewTab-CLpCyLvx.js} +2 -2
  40. package/extensions/dashboard-client/dist/assets/{OverviewTab-4LmW5ZCl.js.map → OverviewTab-CLpCyLvx.js.map} +1 -1
  41. package/extensions/dashboard-client/dist/assets/{ReposTab-StebchgQ.js → ReposTab-EdAqUrwG.js} +2 -2
  42. package/extensions/dashboard-client/dist/assets/{ReposTab-StebchgQ.js.map → ReposTab-EdAqUrwG.js.map} +1 -1
  43. package/extensions/dashboard-client/dist/assets/{SessionsTab-C3D_XAkX.js → SessionsTab-C21ZuOL_.js} +2 -2
  44. package/extensions/dashboard-client/dist/assets/{SessionsTab-C3D_XAkX.js.map → SessionsTab-C21ZuOL_.js.map} +1 -1
  45. package/extensions/dashboard-client/dist/assets/SetupTab-CP7lE3L_.js +2 -0
  46. package/extensions/dashboard-client/dist/assets/SetupTab-CP7lE3L_.js.map +1 -0
  47. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CTPi_AAW.js → TimeSavedCard-DZ3QR8qt.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CTPi_AAW.js.map → TimeSavedCard-DZ3QR8qt.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/{TurnsTab-DSOGcY5Q.js → TurnsTab-CNchE5rD.js} +2 -2
  50. package/extensions/dashboard-client/dist/assets/{TurnsTab-DSOGcY5Q.js.map → TurnsTab-CNchE5rD.js.map} +1 -1
  51. package/extensions/dashboard-client/dist/assets/{VectorCortexTab-OMW8_RFf.js → VectorCortexTab-B3WuPeyn.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{VectorCortexTab-OMW8_RFf.js.map → VectorCortexTab-B3WuPeyn.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/{WikiTab-Db-qFDdg.js → WikiTab-BIe5Z9Nc.js} +2 -2
  54. package/extensions/dashboard-client/dist/assets/{WikiTab-Db-qFDdg.js.map → WikiTab-BIe5Z9Nc.js.map} +1 -1
  55. package/extensions/dashboard-client/dist/assets/{button-BF4UgBAD.js → button-BrVreSt5.js} +2 -2
  56. package/extensions/dashboard-client/dist/assets/{button-BF4UgBAD.js.map → button-BrVreSt5.js.map} +1 -1
  57. package/extensions/dashboard-client/dist/assets/{card-XmXoYQqB.js → card-D9F4wbqs.js} +2 -2
  58. package/extensions/dashboard-client/dist/assets/{card-XmXoYQqB.js.map → card-D9F4wbqs.js.map} +1 -1
  59. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-Be8hM1aU.js → generateCategoricalChart-mkdzCnMP.js} +2 -2
  60. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-Be8hM1aU.js.map → generateCategoricalChart-mkdzCnMP.js.map} +1 -1
  61. package/extensions/dashboard-client/dist/assets/{index-NXDgILxv.js → index-CtlmOlj1.js} +3 -3
  62. package/extensions/dashboard-client/dist/assets/{index-NXDgILxv.js.map → index-CtlmOlj1.js.map} +1 -1
  63. package/extensions/dashboard-client/dist/assets/{switch-kUZCrbia.js → switch-BXmp5iwA.js} +2 -2
  64. package/extensions/dashboard-client/dist/assets/{switch-kUZCrbia.js.map → switch-BXmp5iwA.js.map} +1 -1
  65. package/extensions/dashboard-client/dist/assets/{toggle-DEe5mCwF.js → toggle-BgawNwW9.js} +2 -2
  66. package/extensions/dashboard-client/dist/assets/{toggle-DEe5mCwF.js.map → toggle-BgawNwW9.js.map} +1 -1
  67. package/extensions/dashboard-client/dist/assets/{useSSE-BlCyLqge.js → useSSE-DZKX73Ea.js} +2 -2
  68. package/extensions/dashboard-client/dist/assets/{useSSE-BlCyLqge.js.map → useSSE-DZKX73Ea.js.map} +1 -1
  69. package/extensions/dashboard-client/dist/index.html +1 -1
  70. package/extensions/dashboard-client/src/tabs/SetupTab/CustomEndpointSection.tsx +65 -0
  71. package/extensions/dashboard-client/src/tabs/SetupTab/EmbedderSetup.tsx +19 -108
  72. package/extensions/dashboard-client/src/tabs/SetupTab/EmbedderSetupStyles.ts +69 -0
  73. package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +5 -3
  74. package/extensions/dashboard-server/api-contracts/setup.ts +4 -3
  75. package/extensions/dashboard-server/api-contracts/vector-cortex-cache.ts +5 -0
  76. package/extensions/dashboard-server/api-contracts/vector-cortex-diagnostics.ts +2 -0
  77. package/extensions/dashboard-server/api-contracts/vector-cortex-economics.ts +2 -0
  78. package/extensions/dashboard-server/api-contracts/vector-cortex-policy.ts +2 -0
  79. package/extensions/dashboard-server/resolve-snapshot.ts +42 -28
  80. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
  81. package/extensions/dashboard-server/routes-setup.ts +3 -1
  82. package/extensions/dashboard-server/routes-vector-cortex-crystals.ts +12 -3
  83. package/extensions/dashboard-server/routes-vector-cortex-diagnostics.ts +8 -2
  84. package/extensions/dashboard-server/routes-vector-cortex-economics.ts +1 -0
  85. package/extensions/dashboard-server/routes-vector-cortex-heal.ts +19 -6
  86. package/extensions/dashboard-server/routes-vector-cortex-policy.ts +1 -0
  87. package/extensions/dashboard-server/routes-vector-cortex-reconstruct.ts +11 -3
  88. package/extensions/dashboard-server/routes-vector-cortex-repair.ts +14 -5
  89. package/extensions/dashboard-server/routes-vector-cortex-shards.ts +13 -4
  90. package/extensions/dashboard-server/vc-event-counts.ts +67 -0
  91. package/extensions/mega-dashboard.ts +2 -0
  92. package/extensions/mega-events/agent-handlers/turnEndHandler/cacheStripe.ts +13 -4
  93. package/extensions/mega-events/context-handler/afterCompact.ts +69 -4
  94. package/extensions/mega-events/context-handler/dbMirrorAppend.ts +39 -7
  95. package/extensions/mega-events/context-handler/pipelineRun.ts +39 -0
  96. package/extensions/mega-events/context-handler/tailResult.ts +56 -15
  97. package/extensions/mega-runtime/dashboard-snapshot.ts +12 -6
  98. package/extensions/mega-runtime/runtime-snapshot.ts +6 -0
  99. package/extensions/mega-runtime/vc-observer.ts +120 -0
  100. package/package.json +1 -1
  101. package/src/cache-stripe-impl.ts +73 -277
  102. package/src/cache-stripe-score.ts +170 -0
  103. package/extensions/dashboard-client/dist/assets/SetupTab-BTBGyppF.js +0 -2
  104. package/extensions/dashboard-client/dist/assets/SetupTab-BTBGyppF.js.map +0 -1
@@ -11,12 +11,19 @@ import type { IncomingMessage, ServerResponse } from "node:http";
11
11
  import type { RouteContext } from "./routes-core.js";
12
12
  import { VC4C_ENABLED } from "../../src/config.js";
13
13
  import { sendJson } from "./routes-vector-cortex-shared.js";
14
+ import { countVcEvents, vcCount } from "./vc-event-counts.js";
14
15
  import type { VectorCortexReconstructView } from "./api-contracts/vector-cortex.js";
15
16
 
17
+ // Closure attempt/rejection events emitted by src/vector-cortex/heal/emit.ts.
18
+ const RECONSTRUCT_EVENTS = [
19
+ "vector_cortex_closure_optimized",
20
+ "vector_cortex_closure_proof_rejected",
21
+ ] as const;
22
+
16
23
  export function handleVectorCortexReconstruct(
17
24
  req: IncomingMessage,
18
25
  res: ServerResponse,
19
- _ctx: RouteContext,
26
+ ctx: RouteContext,
20
27
  ): boolean {
21
28
  const url = req.url ?? "";
22
29
  const path = url.split("?")[0] ?? url;
@@ -26,10 +33,11 @@ export function handleVectorCortexReconstruct(
26
33
  return true;
27
34
  }
28
35
  const enabled = VC4C_ENABLED();
36
+ const counts = countVcEvents(ctx.stateDir, RECONSTRUCT_EVENTS);
29
37
  const body: VectorCortexReconstructView = {
30
38
  enabled,
31
- closureAttempts: 0,
32
- closureRejections: 0,
39
+ closureAttempts: vcCount(counts, "vector_cortex_closure_optimized"),
40
+ closureRejections: vcCount(counts, "vector_cortex_closure_proof_rejected"),
33
41
  validatedCount: 0,
34
42
  invalidatedCount: 0,
35
43
  spanTotal: 0,
@@ -25,8 +25,16 @@ import type { IncomingMessage, ServerResponse } from "node:http";
25
25
  import type { RouteContext } from "./routes-core.js";
26
26
  import { VC6C_ENABLED } from "../../src/config.js";
27
27
  import { sendJson } from "./routes-vector-cortex-shared.js";
28
+ import { countVcEvents, vcCount } from "./vc-event-counts.js";
28
29
  import type { VectorCortexRepairView } from "./api-contracts/vector-cortex.js";
29
30
 
31
+ // Actual event names emitted by src/vector-cortex/heal/repair-emit.ts.
32
+ const REPAIR_EVENTS = [
33
+ "vector_cortex_repair_planned",
34
+ "vector_cortex_repair_pointer_switched",
35
+ "vector_cortex_repair_backoff",
36
+ ] as const;
37
+
30
38
  /**
31
39
  * Reader-only GET /api/vector-cortex/repair (VC6C).
32
40
  *
@@ -38,7 +46,7 @@ import type { VectorCortexRepairView } from "./api-contracts/vector-cortex.js";
38
46
  export function handleVectorCortexRepair(
39
47
  req: IncomingMessage,
40
48
  res: ServerResponse,
41
- _ctx: RouteContext,
49
+ ctx: RouteContext,
42
50
  ): boolean {
43
51
  const url = req.url ?? "";
44
52
  const path = url.split("?")[0] ?? url;
@@ -49,6 +57,7 @@ export function handleVectorCortexRepair(
49
57
  }
50
58
 
51
59
  const enabled = VC6C_ENABLED();
60
+ const counts = countVcEvents(ctx.stateDir, REPAIR_EVENTS);
52
61
  // Flag-off routes to mode C: with VC6C off no self-healing controller runs at
53
62
  // all, so neither the targeted rebuild (mode A) nor the full deterministic
54
63
  // rebuild (mode B) path is available. Mode C is exactly the spec's "derived
@@ -59,10 +68,10 @@ export function handleVectorCortexRepair(
59
68
  const body: VectorCortexRepairView = {
60
69
  enabled,
61
70
  mode,
62
- repairAttempts: 0,
63
- repairsPlanned: 0,
64
- pointersSwitched: 0,
65
- backoffs: 0,
71
+ repairAttempts: vcCount(counts, "vector_cortex_repair_planned"),
72
+ repairsPlanned: vcCount(counts, "vector_cortex_repair_planned"),
73
+ pointersSwitched: vcCount(counts, "vector_cortex_repair_pointer_switched"),
74
+ backoffs: vcCount(counts, "vector_cortex_repair_backoff"),
66
75
  lastBackoffMs: null,
67
76
  lastFailure: null,
68
77
  updatedAt: new Date().toISOString(),
@@ -22,15 +22,22 @@ import type { IncomingMessage, ServerResponse } from "node:http";
22
22
  import type { RouteContext } from "./routes-core.js";
23
23
  import { VC4A_ENABLED } from "../../src/config.js";
24
24
  import { sendJson } from "./routes-vector-cortex-shared.js";
25
+ import { countVcEvents, vcCount } from "./vc-event-counts.js";
25
26
  import type { VectorCortexShardsView } from "./api-contracts/vector-cortex.js";
26
27
 
28
+ // Actual events emitted by src/vector-cortex/shards/manifest.ts. VC4A has no
29
+ // per-tier (semantic/exact) write event — the manifest build is the single
30
+ // shard-write signal — so semanticCount is wired to it and exactCount stays 0
31
+ // until a per-tier write event exists.
32
+ const SHARD_EVENTS = ["vector_cortex_shard_manifest_built"] as const;
33
+
27
34
  /**
28
35
  * Reader-only GET /api/vector-cortex/shards (VC4A).
29
36
  */
30
37
  export function handleVectorCortexShards(
31
38
  req: IncomingMessage,
32
39
  res: ServerResponse,
33
- _ctx: RouteContext,
40
+ ctx: RouteContext,
34
41
  ): boolean {
35
42
  const url = req.url ?? "";
36
43
  const path = url.split("?")[0] ?? url;
@@ -42,11 +49,13 @@ export function handleVectorCortexShards(
42
49
  }
43
50
 
44
51
  const enabled = VC4A_ENABLED();
52
+ const counts = countVcEvents(ctx.stateDir, SHARD_EVENTS);
45
53
  const body: VectorCortexShardsView = {
46
54
  enabled,
47
- // No durable shard manifest is staged yet in this sprint (pure in-memory
48
- // partition), so the aggregates are truthfully zero.
49
- semanticCount: 0,
55
+ // The manifest-build event is the only VC4A shard-write signal; semanticCount
56
+ // reflects manifests built and exactCount stays 0 (no per-tier write event
57
+ // distinguishes exact shards in events.log yet).
58
+ semanticCount: vcCount(counts, "vector_cortex_shard_manifest_built"),
50
59
  exactCount: 0,
51
60
  byteTotal: 0,
52
61
  protectedByteTotal: 0,
@@ -0,0 +1,67 @@
1
+ /**
2
+ * dashboard-server/vc-event-counts.ts — lightweight events.log tail reader for
3
+ * the Vector Cortex dashboard routes.
4
+ *
5
+ * The VC event reporters in `src/vector-cortex/*` emit structured JSON events
6
+ * (one per line) into the per-repo `events.log`. Instead of the dashboard
7
+ * hardcoding zero for aggregate counters, this module reads the event tail and
8
+ * counts real occurrences of named events. It is the single shared seam so every
9
+ * route stays a small, thin consumer and the counting logic lives in one place
10
+ * (extensions/ 400-line soft-as-hard split trigger).
11
+ *
12
+ * NEVER throws: a missing/unreadable file degrades to an empty Map, malformed
13
+ * lines are skipped silently, and only the `.event` string field is inspected —
14
+ * no payload, bytes, span ids, digests, or ledger text ever leaves this module
15
+ * (SECURITY_PRIVACY, reader-only aggregate).
16
+ *
17
+ * Guardrails: PREVENT-PI-004 (local in-process file read only), PREVENT-011
18
+ * (no `any`).
19
+ */
20
+
21
+ import { join } from "node:path";
22
+ import { readFileSync } from "node:fs";
23
+
24
+ /**
25
+ * Count occurrences of the given named events within the last `maxLines` of the
26
+ * per-repo events.log adjacent to `stateDir`. Returns a Map keyed by event name;
27
+ * a missing/unreadable log or zero matching lines yields an empty Map. Best-effort
28
+ * and non-fatal by construction (matches the non-fatal-stores invariant).
29
+ */
30
+ export function countVcEvents(
31
+ stateDir: string,
32
+ eventNames: readonly string[],
33
+ maxLines = 10000,
34
+ ): Map<string, number> {
35
+ const counts = new Map<string, number>();
36
+ let raw: string;
37
+ try {
38
+ raw = readFileSync(join(stateDir, "events.log"), "utf-8");
39
+ } catch {
40
+ // Missing or unreadable log → nothing to count, never throw.
41
+ return counts;
42
+ }
43
+ // Walk only the tail: split into lines and keep the last `maxLines`.
44
+ const lines = raw.split("\n");
45
+ const start = Math.max(0, lines.length - maxLines);
46
+ const wanted = new Set(eventNames);
47
+ for (let i = start; i < lines.length; i++) {
48
+ const line = lines[i] as string;
49
+ if (line.length === 0) continue;
50
+ let ev: { event?: unknown } | null = null;
51
+ try {
52
+ ev = JSON.parse(line) as { event?: unknown };
53
+ } catch {
54
+ // Malformed line — skip silently, never break the reader.
55
+ continue;
56
+ }
57
+ if (typeof ev?.event === "string" && wanted.has(ev.event)) {
58
+ counts.set(ev.event, (counts.get(ev.event) ?? 0) + 1);
59
+ }
60
+ }
61
+ return counts;
62
+ }
63
+
64
+ /** Read a value from an event-count map, defaulting to 0 when absent. */
65
+ export function vcCount(counts: Map<string, number>, name: string): number {
66
+ return counts.get(name) ?? 0;
67
+ }
@@ -25,6 +25,8 @@ export interface DashboardSnapshot {
25
25
  presetTier: string;
26
26
  /** Live 0–1 pressure ratio (currentTokens / thresholdTokens). */
27
27
  pressure: number;
28
+ /** VC0A: count of eval-observer latency samples (0 when disabled/absent). */
29
+ vcObserverSamples: number;
28
30
  config: {
29
31
  fastGatePct: number;
30
32
  thresholdTokens: number;
@@ -28,12 +28,21 @@ export function cacheStripe(
28
28
  const embedder = defaultEmbedder();
29
29
 
30
30
  // (a) Extract text from the assistant's response for topic embedding.
31
+ // pi's assistant content may be a plain string OR an array of parts.
32
+ // Discriminate by type and only reach into `.text` after confirming the
33
+ // part is an object carrying a string `.text` — never assume shape.
31
34
  let textToEmbed = "";
32
35
  const msg = event.message;
33
- if (msg.role === "assistant" && Array.isArray(msg.content)) {
34
- for (const part of msg.content) {
35
- if ("text" in part && typeof part.text === "string") {
36
- textToEmbed += part.text + " ";
36
+ if (msg.role === "assistant") {
37
+ const c = msg.content;
38
+ if (typeof c === "string") {
39
+ textToEmbed = c;
40
+ } else if (Array.isArray(c)) {
41
+ for (const part of c) {
42
+ if (part && typeof part === "object" && "text" in part) {
43
+ const t = (part as { text?: unknown }).text;
44
+ if (typeof t === "string") textToEmbed += t + " ";
45
+ }
37
46
  }
38
47
  }
39
48
  }
@@ -28,12 +28,17 @@ import { assignNewMemoriesIncremental } from "../../../src/wiki/index.js";
28
28
  import { TrigramEmbedder } from "../../../src/embedder.js";
29
29
  import type { EmbeddedChunk } from "../../../src/topics/types.js";
30
30
  import type { MegaConfig } from "../../mega-config.js";
31
+ import { reportClosureOptimized } from "../../../src/vector-cortex/heal/emit.js";
32
+ import { reportRepairPlanned } from "../../../src/vector-cortex/heal/repair-emit.js";
33
+ import { VC6A_ENABLED, VC6C_ENABLED } from "../../../src/config/vector-cortex.js";
31
34
 
32
35
  /** Shape of the compact result consumed by the epoch/maintenance writes. */
33
36
  interface CompactResult {
34
37
  checkpointId?: string;
35
38
  compactedFrom: number;
36
39
  summary: string;
40
+ tokenEstimate?: number;
41
+ originalTokenEstimate?: number;
37
42
  }
38
43
 
39
44
  /**
@@ -45,9 +50,12 @@ export async function persistEpochAndMaintain(
45
50
  config: MegaConfig,
46
51
  ran: { result: CompactResult },
47
52
  ): Promise<void> {
48
- // S27 DB-mirror: write checkpoint_epoch with deterministic nonce.
49
- // This makes the cache key stable across identical compactions.
50
- if (config.dbMirror) {
53
+ // PLAN_V2 cache-striping epoch row. buildCacheOptimizedPrompt's stripe
54
+ // lookup reads the most-recent checkpoint_epochs row, so emit it whenever
55
+ // PLAN_V2 flags are live. Separate from the legacy dbMirror block below so
56
+ // PLAN_V2 can run with dbMirror OFF (flag-on is a genuine additive; flag-OFF
57
+ // = byte-identical to the predecessor, since this block no-ops then).
58
+ if (config.dbMirror || config.messageSeparation || config.cacheStriping) {
51
59
  try {
52
60
  const db = openStore(runtime.currentStateDir);
53
61
  const cpId = ran.result.checkpointId ?? `epoch-${Date.now()}`;
@@ -62,6 +70,22 @@ export async function persistEpochAndMaintain(
62
70
  createdAt: Date.now(),
63
71
  };
64
72
  writeCheckpointEpoch(db, epoch);
73
+ } catch (e) {
74
+ runtime.logger.warn("planv2-epoch-fail", { error: String(e) });
75
+ }
76
+ }
77
+
78
+ // S27 DB-mirror downstream: stamp turns, rebuild wiki, seed topics, dedup.
79
+ // Remains on `config.dbMirror` only — these are DB-mirror maintenance and
80
+ // must NOT run for PLAN_V2-only configurations.
81
+ if (config.dbMirror) {
82
+ try {
83
+ const db = openStore(runtime.currentStateDir);
84
+ const cpId = ran.result.checkpointId ?? `epoch-${Date.now()}`;
85
+ // epoch + writeCheckpointEpoch moved to the PLAN_V2 block above (it
86
+ // gates the wider set of flags); reuse epochIdFor(cpId) here so the
87
+ // stamps below land in the same epoch row.
88
+ const epochId = epochIdFor(cpId);
65
89
  // S50B: link this session's turns to the epoch that just compacted
66
90
  // them (compression-by-conversation-epoch metrics). Isolated-store
67
91
  // only; best-effort + non-fatal.
@@ -69,7 +93,7 @@ export async function persistEpochAndMaintain(
69
93
  stampTurnsEpochFor(
70
94
  config,
71
95
  runtime.rt.sessionId,
72
- epoch.epochId,
96
+ epochId,
73
97
  runtime.currentStateDir,
74
98
  );
75
99
  } catch {
@@ -253,4 +277,45 @@ export async function persistEpochAndMaintain(
253
277
  runtime.logger.warn("db-mirror-epoch-fail", { error: String(e) });
254
278
  }
255
279
  }
280
+
281
+ // VC6 Heal lifecycle emits (post-compact). Wiring stubs — dashboard graph +
282
+ // event counts move; real gap detection/rebuild is a future sprint.
283
+ // VC6A: closure-optimization savings from the compact token delta.
284
+ const savings = Math.max(
285
+ 0,
286
+ (ran.result.originalTokenEstimate ?? 0) - (ran.result.tokenEstimate ?? 0),
287
+ );
288
+ if (VC6A_ENABLED()) {
289
+ try {
290
+ reportClosureOptimized(
291
+ (name, payload) =>
292
+ runtime.appendEvent(name, payload as Record<string, unknown>),
293
+ {
294
+ sessionId: runtime.rt.sessionId,
295
+ removed: 0,
296
+ retained: ran.result.compactedFrom,
297
+ savings,
298
+ },
299
+ );
300
+ } catch {
301
+ /* non-fatal: VC6A heal emit never breaks compaction */
302
+ }
303
+ }
304
+ // VC6C: repair-planner placeholder (no real gap detection yet).
305
+ if (VC6C_ENABLED()) {
306
+ try {
307
+ reportRepairPlanned(
308
+ (name, payload) =>
309
+ runtime.appendEvent(name, payload as Record<string, unknown>),
310
+ {
311
+ subsystem: "post_compact",
312
+ generation: 1,
313
+ backoffMs: 0,
314
+ gapSize: 0,
315
+ },
316
+ );
317
+ } catch {
318
+ /* non-fatal: VC6C heal emit never breaks compaction */
319
+ }
320
+ }
256
321
  }
@@ -16,6 +16,21 @@ import type { MegaRuntime } from "../../mega-runtime.js";
16
16
  import type { MegaConfig } from "../../mega-config.js";
17
17
  import { messageContentText } from "./messageText.js";
18
18
 
19
+ /**
20
+ * Best-effort tool_call_id for the tool_results insert. The toolResult variant
21
+ * carries a top-level toolCallId (read via an `unknown`-narrowed cast — never
22
+ * reach into `.content`, which is variant-specific and requires narrowing).
23
+ * bashExecution has no toolCallId, so fall back to a stable synthetic id keyed
24
+ * on (turn, index) to satisfy the NOT NULL column. No `any` (PREVENT-011).
25
+ */
26
+ function toolCallIdOf(m: AgentMessage, fallback: string): string {
27
+ if (m.role === "toolResult") {
28
+ const id = (m as unknown as { toolCallId?: unknown }).toolCallId;
29
+ if (typeof id === "string" && id.length > 0) return id;
30
+ }
31
+ return fallback;
32
+ }
33
+
19
34
  /**
20
35
  * Append incoming messages to the DB mirror (raw_transcript + thread/tool
21
36
  * tables) and the v2 ledger. Gated on config.dbMirror for the mirror; the VC1B
@@ -49,25 +64,42 @@ export function appendMirrorAndLedger(
49
64
  // separation (buildSeparatedPrompt / buildCacheOptimizedPrompt in
50
65
  // tailResult) is sufficient for the prompt-construction path;
51
66
  // these DB writes persist the split for post-hoc analysis, dashboard
52
- // queries, and future readers. Non-fatal failure here never breaks
53
- // the agent loop (PREVENT-PI-004: zero network, local SQLite only).
54
- {
67
+ // queries, and future readers. Gated on (messageSeparation ||
68
+ // cacheStriping) so flag-OFF remains byte-identical to the
69
+ // predecessor — when both flags are OFF the live prompt is never
70
+ // separated, and growing these tables would be dead state.
71
+ // Non-fatal — failure here never breaks the agent loop
72
+ // (PREVENT-PI-004: zero network, local SQLite only).
73
+ if (config.messageSeparation || config.cacheStriping) {
55
74
  const sid = runtime.rt.sessionId;
56
75
  const turn = runtime.currentTurn;
57
76
  const now = Date.now();
58
77
  const threadStmt = db.prepare(
59
78
  "INSERT OR IGNORE INTO conversation_thread (conversation_id, role, content, turn_index, timestamp) VALUES (?, ?, ?, ?, ?)",
60
79
  );
80
+ // Schema (plan-v2.ts) is (conversation_id, tool_call_id,
81
+ // tool_result, turn_index, timestamp) — NOT role/content.
61
82
  const toolStmt = db.prepare(
62
- "INSERT OR IGNORE INTO tool_results (conversation_id, role, content, turn_index, timestamp) VALUES (?, ?, ?, ?, ?)",
83
+ "INSERT OR IGNORE INTO tool_results (conversation_id, tool_call_id, tool_result, turn_index, timestamp) VALUES (?, ?, ?, ?, ?)",
84
+ );
85
+ const toolHas = db.prepare(
86
+ "SELECT 1 FROM tool_results WHERE conversation_id = ? AND turn_index = ? AND tool_call_id = ? AND tool_result = ? LIMIT 1",
87
+ );
88
+ const threadHas = db.prepare(
89
+ "SELECT 1 FROM conversation_thread WHERE conversation_id = ? AND turn_index = ? AND role = ? AND content = ? LIMIT 1",
63
90
  );
64
- for (const m of messages) {
91
+ for (const [idx, m] of messages.entries()) {
65
92
  const role = m.role;
66
93
  const content = messageContentText(m);
67
94
  if (role === "user" || role === "assistant") {
68
- threadStmt.run(sid, role, content, turn, now);
95
+ if (threadHas.get(sid, turn, role, content) == null) {
96
+ threadStmt.run(sid, role, content, turn, now);
97
+ }
69
98
  } else if (role === "toolResult" || role === "bashExecution") {
70
- toolStmt.run(sid, role, content, turn, now);
99
+ const toolCallId = toolCallIdOf(m, `bash:${turn}:${idx}`);
100
+ if (toolHas.get(sid, turn, toolCallId, content) == null) {
101
+ toolStmt.run(sid, toolCallId, content, turn, now);
102
+ }
71
103
  }
72
104
  }
73
105
  }
@@ -19,6 +19,9 @@ import { pressureFromPct, pressureRatio } from "../../mega-config.js";
19
19
  import type { MegaRuntime } from "../../mega-runtime.js";
20
20
  import type { MegaConfig } from "../../mega-config.js";
21
21
  import type { TailResultFn } from "./gateCheck.js";
22
+ import { recordCompactLatency } from "../../mega-runtime/vc-observer.js";
23
+ import { decideLivePath } from "../../mega-runtime/vector-cortex-live.js";
24
+ import { VC5C_ENABLED } from "../../../src/config/vector-cortex.js";
22
25
 
23
26
  /** The non-skipped variant of the runCompact result. */
24
27
  export type RanResult = Extract<RunCompactResult, { skipped: false }>;
@@ -45,6 +48,32 @@ export function invokePipeline(
45
48
  tailResult: TailResultFn;
46
49
  },
47
50
  ): PipelineOutcome {
51
+ // VC5C: emit the rollout decision per compact event (observability seam).
52
+ // vcGate/vcHardFaults are not yet declared on MegaRuntime.rt — cast to the
53
+ // rollout shapes. Best-effort + non-fatal; decision does NOT gate behavior yet.
54
+ if (VC5C_ENABLED()) {
55
+ try {
56
+ const decision = decideLivePath(runtime.rt.sessionId, {
57
+ emit: runtime.appendEvent.bind(runtime),
58
+ currentGate:
59
+ (runtime.rt as { vcGate?: 0 | 1 | 2 | 3 | 4 }).vcGate ?? 0,
60
+ hardFaults:
61
+ (runtime.rt as { vcHardFaults?: import("../../../src/vector-cortex/rollout/types.js").RolloutHardFault[] })
62
+ .vcHardFaults ?? [],
63
+ });
64
+ runtime.appendEvent("vector_cortex_rollout_decision", {
65
+ sessionId: runtime.rt.sessionId,
66
+ vcActive: decision.vcActive,
67
+ forcedPreVc: decision.forcedPreVc,
68
+ mode: decision.mode,
69
+ bucket: decision.bucket,
70
+ gateIndex: decision.gateIndex,
71
+ promotionBlocked: decision.promotionBlocked,
72
+ });
73
+ } catch {
74
+ /* non-fatal: rollout decision emission never breaks compaction */
75
+ }
76
+ }
48
77
  // Adaptive compression (Fix E): scale compression strength + keepFrom depth
49
78
  // with how close we are to the model context limit. Null-safe: when the
50
79
  // token-fallback path ran (pct unavailable) use the token-basis pressure
@@ -53,9 +82,19 @@ export function invokePipeline(
53
82
  opts.pct != null
54
83
  ? pressureFromPct(opts.pct)
55
84
  : pressureRatio(opts.currentTokens, runtime.effectiveThreshold);
85
+ const t0 = Date.now();
56
86
  const ran = runCompact(pi, runtime, config, ctx, opts.messages, {
57
87
  compressionPressure: pressure,
58
88
  });
89
+ // VC0A: record compact latency on the eval observer (mode A) on every
90
+ // outcome so the dashboard histogram reflects real data. No-op when the
91
+ // observer is absent (flag off / construction failure).
92
+ recordCompactLatency(
93
+ runtime,
94
+ Date.now() - t0,
95
+ runtime.rt.sessionId,
96
+ runtime.rt.compactCount,
97
+ );
59
98
  // D.3: skip paths fall back to replay instead of returning empty.
60
99
  // If runCompact skipped and we have a valid trimCache, replay it
61
100
  // (free stability win) — otherwise defer to the next event.
@@ -12,6 +12,16 @@ import type { MegaConfig } from "../../mega-config.js";
12
12
  import { stagedForTail, withRecallTail } from "../recall-tail.js";
13
13
  import { buildSeparatedPrompt, buildCacheOptimizedPrompt } from "../separated-prompt.js";
14
14
  import { messageContentText } from "./messageText.js";
15
+ import { computeContentDigest } from "../../../src/dedup/digest.js";
16
+
17
+ // P2.5: per-runtime cross-turn prompt-prefix footprints for stable-prefix
18
+ // measurement. WeakMap keyed by MegaRuntime so the entry dies with the runtime
19
+ // (same lifecycle as a session boundary in practice — a new session re-creates
20
+ // the pipeline over the same runtime, but the prior turn's fingerprints must
21
+ // still be visible for cross-turn compare; a fresh runtime has an empty map and
22
+ // the sessionId guard below blocks cross-session compare).
23
+ type TurnPrefix = { sessionId: string; turn: number; fingerprints: string[] };
24
+ const lastTurnPrefix = new WeakMap<MegaRuntime, TurnPrefix>();
15
25
 
16
26
  /**
17
27
  * Build the tail injection closure. Returns undefined when nothing is staged
@@ -39,23 +49,54 @@ export function buildTailResult(
39
49
  } else if (config.messageSeparation) {
40
50
  result = buildSeparatedPrompt(result);
41
51
  }
42
- // P2.5: log prefix stability (fire-and-forget, non-fatal).
43
- // tailResult is sync, so use .then().catch() on the dynamic import.
52
+ // P2.5: log cross-turn stable-prefix length (cache-hit proxy). After the
53
+ // prompt is built (separated or cache-optimized), fingerprint each leading
54
+ // message and count how many are byte-identical to the previous turn's
55
+ // prompt, in order. A high stablePrefix = the provider KV-cache prefix is
56
+ // re-used (cache hit). Fire-and-forget + non-fatal.
57
+ //
58
+ // tailResult may run several times per turn (gate return / replay /
59
+ // debounce / live-trim), so the sessionId+turn guard measures once per
60
+ // turn — compare against the PREVIOUS turn's stored footprint, then store
61
+ // this turn's for the next comparison. Cross-session compares are skipped
62
+ // via the stored sessionId check (belt-and-suspenders; the WeakMap entry
63
+ // is scoped to this runtime, which owns the session).
44
64
  if (result.length > 1) {
45
- import("../../../src/cache-stripe-impl.js").then(({ computeStabilityScore }) => {
46
- const stableScore = computeStabilityScore(
47
- { content: messageContentText(result[0] ?? result[0]), chunkId: "prefix", accessCount: 0, lastAccessedAt: 0 },
48
- result.slice(0, 2).map((m) => ({ content: messageContentText(m), chunkId: "prefix", accessCount: 0, lastAccessedAt: 0 })),
49
- );
50
- runtime.logger.info("prefix_stability", {
51
- stableScore: Number.isFinite(stableScore) ? stableScore : 0,
52
- prefixMessages: result.length,
53
- separation: config.messageSeparation ? "v2" : "off",
54
- striping: config.cacheStriping ? "v3" : "off",
55
- });
56
- }).catch(() => {
65
+ try {
66
+ const sessionId = runtime.rt.sessionId;
67
+ const turn = runtime.currentTurn;
68
+ const prev = lastTurnPrefix.get(runtime);
69
+ const isNewTurn =
70
+ !prev || prev.sessionId !== sessionId || prev.turn !== turn;
71
+ if (isNewTurn) {
72
+ const fingerprints = result.map((m) =>
73
+ // Role prefix disambiguates identical text across variants
74
+ // (roles are a fixed enum — no realistic fingerprint collision).
75
+ computeContentDigest(`${m.role}|${messageContentText(m)}`).contentHash,
76
+ );
77
+ let stablePrefix = 0;
78
+ if (prev && prev.sessionId === sessionId) {
79
+ for (let i = 0; i < fingerprints.length; i++) {
80
+ if (
81
+ i >= prev.fingerprints.length ||
82
+ prev.fingerprints[i] !== fingerprints[i]
83
+ ) {
84
+ break;
85
+ }
86
+ stablePrefix++;
87
+ }
88
+ }
89
+ lastTurnPrefix.set(runtime, { sessionId, turn, fingerprints });
90
+ runtime.logger.info("prefix_stability", {
91
+ stablePrefix,
92
+ totalMessages: result.length,
93
+ separation: config.messageSeparation ? "v2" : "off",
94
+ striping: config.cacheStriping ? "v3" : "off",
95
+ });
96
+ }
97
+ } catch {
57
98
  // Non-fatal: stability logging is best-effort.
58
- });
99
+ }
59
100
  }
60
101
  return { messages: result };
61
102
  };
@@ -39,6 +39,9 @@ export interface SnapshotBuildContext {
39
39
  readonly pressureBand: string;
40
40
  readonly pressure: number;
41
41
  readonly effectiveThreshold: number;
42
+ /** VC0A: count of latency samples the eval observer histogram should show
43
+ * (0 when the observer is absent / flag off). */
44
+ readonly vcObserverSamples?: number;
42
45
  readonly statusKey: string | undefined;
43
46
  readonly lastCtxTokens: number | null;
44
47
  readonly lastCtxPercent: number | null;
@@ -76,6 +79,7 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
76
79
  tier: ctx.pressureBand,
77
80
  presetTier: ctx.config.tier,
78
81
  pressure: ctx.pressure,
82
+ vcObserverSamples: ctx.vcObserverSamples ?? 0,
79
83
  config: {
80
84
  fastGatePct: ctx.config.fastGatePct,
81
85
  thresholdTokens: ctx.effectiveThreshold,
@@ -132,17 +136,17 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
132
136
  repo: ctx.repo,
133
137
  compression: {
134
138
  session: {
135
- tokensIn: ctx.rt.tokensSaved + (ctx.st.totalTokenEstimate - ctx.st.originalTokens),
139
+ tokensIn: ctx.st.originalTokens,
136
140
  tokensOut: ctx.st.totalTokenEstimate,
137
141
  tokensFreed: ctx.rt.tokensSaved,
138
- compressionPct: ctx.rt.tokensSaved / Math.max(1, ctx.rt.tokensSaved + (ctx.st.totalTokenEstimate - ctx.st.originalTokens)),
142
+ compressionPct: ctx.rt.tokensSaved / Math.max(1, ctx.st.originalTokens),
139
143
  dedupPct: ctx.rt.dedupAttempts > 0 ? ctx.rt.dedupSkips / ctx.rt.dedupAttempts : 0,
140
144
  },
141
145
  repo: {
142
- tokensIn: ctx.repo.tokensSaved + (ctx.repo.totalTokenEstimate - ctx.repo.originalTokens),
146
+ tokensIn: ctx.repo.originalTokens,
143
147
  tokensOut: ctx.repo.totalTokenEstimate,
144
148
  tokensFreed: ctx.repo.tokensSaved,
145
- compressionPct: ctx.repo.tokensSaved / Math.max(1, ctx.repo.tokensSaved + (ctx.repo.totalTokenEstimate - ctx.repo.originalTokens)),
149
+ compressionPct: ctx.repo.tokensSaved / Math.max(1, ctx.repo.originalTokens),
146
150
  dedupPct: ctx.repo.dedupAttempts > 0 ? ctx.repo.dedupCollapsed / ctx.repo.dedupAttempts : 0,
147
151
  },
148
152
  },
@@ -155,7 +159,9 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
155
159
  // per-session in rt and there's no cumulative counter yet.
156
160
  total: ctx.repo.dedupCollapsed,
157
161
  sessionTokensSaved: ctx.rt.cacheHitTokens,
158
- totalTokensSaved: ctx.repo.dedupCollapsed > 0 ? ctx.repo.dedupCollapsed * 100 : 0,
162
+ // Cumulative tokens saved across all sessions in this repo —
163
+ // was a placeholder (dedupCollapsed * 100).
164
+ totalTokensSaved: ctx.repo.tokensSaved,
159
165
  },
160
166
  compacts: {
161
167
  session: ctx.rt.compactCount,
@@ -168,7 +174,7 @@ export function buildDashboardSnapshot(ctx: SnapshotBuildContext): DashboardSnap
168
174
  },
169
175
  cacheHit: {
170
176
  sessionSec: ctx.rt.cacheHitTokens / 1000,
171
- totalSec: (ctx.repo.dedupCollapsed * 100) / 1000,
177
+ totalSec: ctx.repo.tokensSaved / 1000,
172
178
  },
173
179
  },
174
180
  model: ctx.currentModel
@@ -42,6 +42,7 @@ import type { Dashboard } from "../mega-dashboard.js";
42
42
  import type { WidgetData } from "./widget.js";
43
43
  import { computeMegaSnapshot } from "./snapshot.js";
44
44
  import { buildDashboardSnapshot } from "./dashboard-snapshot.js";
45
+ import { getVcObserver } from "./vc-observer.js";
45
46
  import {
46
47
  type RuntimeHelpersContext,
47
48
  materialSigImpl,
@@ -65,6 +66,10 @@ export interface RuntimeSnapshotContext extends RuntimeHelpersContext {
65
66
  store: VectorStore;
66
67
  config: MegaConfig;
67
68
  dashboard: Dashboard;
69
+ // VC0A: appendEvent + currentStateDir are consumed by getVcObserver()
70
+ // (vc-observer.ts WeakMap cache) to surface how many latency samples the
71
+ // dashboard histogram should expect.
72
+ appendEvent(event: string, fields: Record<string, unknown>): void;
68
73
  currentStateDir: string;
69
74
  widgetData: WidgetData | null;
70
75
  /** v0.8.5: material-change signature; read + written by snapshot(). Public so
@@ -164,6 +169,7 @@ export function snapshotImpl(
164
169
  pressureBand: self.pressureBand,
165
170
  pressure: self.pressure,
166
171
  effectiveThreshold: self.effectiveThreshold,
172
+ vcObserverSamples: getVcObserver(self)?.rows().length ?? 0,
167
173
  statusKey: self.statusKey,
168
174
  lastCtxTokens: self.lastCtxTokens,
169
175
  lastCtxPercent: self.lastCtxPercent,