pi-mega-compact 0.20.21 → 0.20.23

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 (95) hide show
  1. package/dist/extensions/dashboard-server/resolve-snapshot.js +42 -28
  2. package/dist/extensions/dashboard-server/routes-setup.js +4 -1
  3. package/dist/extensions/dashboard-server/routes-vector-cortex-crystals.js +11 -3
  4. package/dist/extensions/dashboard-server/routes-vector-cortex-diagnostics.js +7 -2
  5. package/dist/extensions/dashboard-server/routes-vector-cortex-economics.js +1 -0
  6. package/dist/extensions/dashboard-server/routes-vector-cortex-heal.js +18 -6
  7. package/dist/extensions/dashboard-server/routes-vector-cortex-policy.js +1 -0
  8. package/dist/extensions/dashboard-server/routes-vector-cortex-reconstruct.js +10 -3
  9. package/dist/extensions/dashboard-server/routes-vector-cortex-repair.js +13 -5
  10. package/dist/extensions/dashboard-server/routes-vector-cortex-shards.js +12 -4
  11. package/dist/extensions/dashboard-server/vc-event-counts.js +63 -0
  12. package/dist/extensions/mega-events/context-handler/afterCompact.js +34 -0
  13. package/dist/extensions/mega-events/context-handler/pipelineRun.js +33 -0
  14. package/dist/extensions/mega-runtime/dashboard-snapshot.js +9 -6
  15. package/dist/extensions/mega-runtime/runtime-snapshot.js +2 -0
  16. package/dist/extensions/mega-runtime/vc-observer.js +98 -0
  17. package/dist/src/vector-cortex/platform/_cross-language-fixture.js +57 -0
  18. package/dist/vector-cortex/platform/_cross-language-fixture.js +57 -0
  19. package/extensions/dashboard-client/dist/assets/{AreaChart-BkfB-LSQ.js → AreaChart-BzEedilo.js} +2 -2
  20. package/extensions/dashboard-client/dist/assets/{AreaChart-BkfB-LSQ.js.map → AreaChart-BzEedilo.js.map} +1 -1
  21. package/extensions/dashboard-client/dist/assets/{BarChart-Cs1kogko.js → BarChart-CzAVg1_J.js} +2 -2
  22. package/extensions/dashboard-client/dist/assets/{BarChart-Cs1kogko.js.map → BarChart-CzAVg1_J.js.map} +1 -1
  23. package/extensions/dashboard-client/dist/assets/{CacheTab-BZgaguGr.js → CacheTab-D2-rzTuX.js} +2 -2
  24. package/extensions/dashboard-client/dist/assets/{CacheTab-BZgaguGr.js.map → CacheTab-D2-rzTuX.js.map} +1 -1
  25. package/extensions/dashboard-client/dist/assets/{EventsTab-BFbkI_MO.js → EventsTab-DB7g1Szy.js} +2 -2
  26. package/extensions/dashboard-client/dist/assets/{EventsTab-BFbkI_MO.js.map → EventsTab-DB7g1Szy.js.map} +1 -1
  27. package/extensions/dashboard-client/dist/assets/{HealthTab-CvlOENCX.js → HealthTab-Cvkmi-R3.js} +2 -2
  28. package/extensions/dashboard-client/dist/assets/{HealthTab-CvlOENCX.js.map → HealthTab-Cvkmi-R3.js.map} +1 -1
  29. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtBuz2qL.js → MaintenanceTab-g_udcOO7.js} +2 -2
  30. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtBuz2qL.js.map → MaintenanceTab-g_udcOO7.js.map} +1 -1
  31. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-K4775S4d.js → MemoryMapTab-DAjBaf8P.js} +2 -2
  32. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-K4775S4d.js.map → MemoryMapTab-DAjBaf8P.js.map} +1 -1
  33. package/extensions/dashboard-client/dist/assets/{MetricsTab-DVGcFInz.js → MetricsTab-13dYYocB.js} +2 -2
  34. package/extensions/dashboard-client/dist/assets/{MetricsTab-DVGcFInz.js.map → MetricsTab-13dYYocB.js.map} +1 -1
  35. package/extensions/dashboard-client/dist/assets/{OverviewTab-4LmW5ZCl.js → OverviewTab-CLpCyLvx.js} +2 -2
  36. package/extensions/dashboard-client/dist/assets/{OverviewTab-4LmW5ZCl.js.map → OverviewTab-CLpCyLvx.js.map} +1 -1
  37. package/extensions/dashboard-client/dist/assets/{ReposTab-StebchgQ.js → ReposTab-EdAqUrwG.js} +2 -2
  38. package/extensions/dashboard-client/dist/assets/{ReposTab-StebchgQ.js.map → ReposTab-EdAqUrwG.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/{SessionsTab-C3D_XAkX.js → SessionsTab-C21ZuOL_.js} +2 -2
  40. package/extensions/dashboard-client/dist/assets/{SessionsTab-C3D_XAkX.js.map → SessionsTab-C21ZuOL_.js.map} +1 -1
  41. package/extensions/dashboard-client/dist/assets/SetupTab-CP7lE3L_.js +2 -0
  42. package/extensions/dashboard-client/dist/assets/SetupTab-CP7lE3L_.js.map +1 -0
  43. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CTPi_AAW.js → TimeSavedCard-DZ3QR8qt.js} +2 -2
  44. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CTPi_AAW.js.map → TimeSavedCard-DZ3QR8qt.js.map} +1 -1
  45. package/extensions/dashboard-client/dist/assets/{TurnsTab-DSOGcY5Q.js → TurnsTab-CNchE5rD.js} +2 -2
  46. package/extensions/dashboard-client/dist/assets/{TurnsTab-DSOGcY5Q.js.map → TurnsTab-CNchE5rD.js.map} +1 -1
  47. package/extensions/dashboard-client/dist/assets/{VectorCortexTab-OMW8_RFf.js → VectorCortexTab-B3WuPeyn.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{VectorCortexTab-OMW8_RFf.js.map → VectorCortexTab-B3WuPeyn.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/{WikiTab-Db-qFDdg.js → WikiTab-BIe5Z9Nc.js} +2 -2
  50. package/extensions/dashboard-client/dist/assets/{WikiTab-Db-qFDdg.js.map → WikiTab-BIe5Z9Nc.js.map} +1 -1
  51. package/extensions/dashboard-client/dist/assets/{button-BF4UgBAD.js → button-BrVreSt5.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{button-BF4UgBAD.js.map → button-BrVreSt5.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/{card-XmXoYQqB.js → card-D9F4wbqs.js} +2 -2
  54. package/extensions/dashboard-client/dist/assets/{card-XmXoYQqB.js.map → card-D9F4wbqs.js.map} +1 -1
  55. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-Be8hM1aU.js → generateCategoricalChart-mkdzCnMP.js} +2 -2
  56. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-Be8hM1aU.js.map → generateCategoricalChart-mkdzCnMP.js.map} +1 -1
  57. package/extensions/dashboard-client/dist/assets/{index-NXDgILxv.js → index-CtlmOlj1.js} +3 -3
  58. package/extensions/dashboard-client/dist/assets/{index-NXDgILxv.js.map → index-CtlmOlj1.js.map} +1 -1
  59. package/extensions/dashboard-client/dist/assets/{switch-kUZCrbia.js → switch-BXmp5iwA.js} +2 -2
  60. package/extensions/dashboard-client/dist/assets/{switch-kUZCrbia.js.map → switch-BXmp5iwA.js.map} +1 -1
  61. package/extensions/dashboard-client/dist/assets/{toggle-DEe5mCwF.js → toggle-BgawNwW9.js} +2 -2
  62. package/extensions/dashboard-client/dist/assets/{toggle-DEe5mCwF.js.map → toggle-BgawNwW9.js.map} +1 -1
  63. package/extensions/dashboard-client/dist/assets/{useSSE-BlCyLqge.js → useSSE-DZKX73Ea.js} +2 -2
  64. package/extensions/dashboard-client/dist/assets/{useSSE-BlCyLqge.js.map → useSSE-DZKX73Ea.js.map} +1 -1
  65. package/extensions/dashboard-client/dist/index.html +1 -1
  66. package/extensions/dashboard-client/src/tabs/SetupTab/CustomEndpointSection.tsx +65 -0
  67. package/extensions/dashboard-client/src/tabs/SetupTab/EmbedderSetup.tsx +19 -108
  68. package/extensions/dashboard-client/src/tabs/SetupTab/EmbedderSetupStyles.ts +69 -0
  69. package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +5 -3
  70. package/extensions/dashboard-server/api-contracts/setup.ts +4 -3
  71. package/extensions/dashboard-server/api-contracts/vector-cortex-cache.ts +5 -0
  72. package/extensions/dashboard-server/api-contracts/vector-cortex-diagnostics.ts +2 -0
  73. package/extensions/dashboard-server/api-contracts/vector-cortex-economics.ts +2 -0
  74. package/extensions/dashboard-server/api-contracts/vector-cortex-policy.ts +2 -0
  75. package/extensions/dashboard-server/resolve-snapshot.ts +42 -28
  76. package/extensions/dashboard-server/routes-setup.ts +3 -1
  77. package/extensions/dashboard-server/routes-vector-cortex-crystals.ts +12 -3
  78. package/extensions/dashboard-server/routes-vector-cortex-diagnostics.ts +8 -2
  79. package/extensions/dashboard-server/routes-vector-cortex-economics.ts +1 -0
  80. package/extensions/dashboard-server/routes-vector-cortex-heal.ts +19 -6
  81. package/extensions/dashboard-server/routes-vector-cortex-policy.ts +1 -0
  82. package/extensions/dashboard-server/routes-vector-cortex-reconstruct.ts +11 -3
  83. package/extensions/dashboard-server/routes-vector-cortex-repair.ts +14 -5
  84. package/extensions/dashboard-server/routes-vector-cortex-shards.ts +13 -4
  85. package/extensions/dashboard-server/vc-event-counts.ts +67 -0
  86. package/extensions/mega-dashboard.ts +2 -0
  87. package/extensions/mega-events/context-handler/afterCompact.ts +46 -0
  88. package/extensions/mega-events/context-handler/pipelineRun.ts +39 -0
  89. package/extensions/mega-runtime/dashboard-snapshot.ts +12 -6
  90. package/extensions/mega-runtime/runtime-snapshot.ts +6 -0
  91. package/extensions/mega-runtime/vc-observer.ts +120 -0
  92. package/package.json +1 -1
  93. package/src/vector-cortex/platform/_cross-language-fixture.ts +83 -0
  94. package/extensions/dashboard-client/dist/assets/SetupTab-BTBGyppF.js +0 -2
  95. 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,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
  /**
@@ -253,4 +258,45 @@ export async function persistEpochAndMaintain(
253
258
  runtime.logger.warn("db-mirror-epoch-fail", { error: String(e) });
254
259
  }
255
260
  }
261
+
262
+ // VC6 Heal lifecycle emits (post-compact). Wiring stubs — dashboard graph +
263
+ // event counts move; real gap detection/rebuild is a future sprint.
264
+ // VC6A: closure-optimization savings from the compact token delta.
265
+ const savings = Math.max(
266
+ 0,
267
+ (ran.result.originalTokenEstimate ?? 0) - (ran.result.tokenEstimate ?? 0),
268
+ );
269
+ if (VC6A_ENABLED()) {
270
+ try {
271
+ reportClosureOptimized(
272
+ (name, payload) =>
273
+ runtime.appendEvent(name, payload as Record<string, unknown>),
274
+ {
275
+ sessionId: runtime.rt.sessionId,
276
+ removed: 0,
277
+ retained: ran.result.compactedFrom,
278
+ savings,
279
+ },
280
+ );
281
+ } catch {
282
+ /* non-fatal: VC6A heal emit never breaks compaction */
283
+ }
284
+ }
285
+ // VC6C: repair-planner placeholder (no real gap detection yet).
286
+ if (VC6C_ENABLED()) {
287
+ try {
288
+ reportRepairPlanned(
289
+ (name, payload) =>
290
+ runtime.appendEvent(name, payload as Record<string, unknown>),
291
+ {
292
+ subsystem: "post_compact",
293
+ generation: 1,
294
+ backoffMs: 0,
295
+ gapSize: 0,
296
+ },
297
+ );
298
+ } catch {
299
+ /* non-fatal: VC6C heal emit never breaks compaction */
300
+ }
301
+ }
256
302
  }
@@ -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.
@@ -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,
@@ -0,0 +1,120 @@
1
+ /**
2
+ * vc-observer.ts — VC0A mode-A eval observer wiring for MegaRuntime.
3
+ *
4
+ * Bridges the pure in-memory EvalObserver (src/vector-cortex/eval/observer.ts)
5
+ * into the production runtime: every recorded compact/recall latency sample is
6
+ * both emitted to events.log (via MegaRuntime.appendEvent) and appended to the
7
+ * redacted evaluation.jsonl (via appendEvalRow) so the Vector Cortex dashboard
8
+ * "Observer" histogram reads real data instead of an empty file.
9
+ *
10
+ * All wiring is gated on VC0A_ENABLED() (MEGACOMPACT_VC0A). Flag-off and any
11
+ * construction/persistence error are non-fatal (PREVENT-PI-004: local FS only,
12
+ * no network) and degrade to a null observer — byte-identical to the
13
+ * predecessor (mode C). PREVENT-011: no `any` type.
14
+ *
15
+ * Follows the capture-model.ts / append-event.ts pattern: a context interface +
16
+ * free functions + a thin delegate. No import-time side effects — merely
17
+ * importing these functions is inert regardless of the flag.
18
+ */
19
+
20
+ import { VC0A_ENABLED } from "../../src/config.js";
21
+ import { createEvalObserver } from "../../src/vector-cortex/eval/observer.js";
22
+ import type { EvalObserver } from "../../src/vector-cortex/eval/observer.js";
23
+ import { appendEvalRow } from "../../src/vector-cortex/eval/persist.js";
24
+
25
+ // ---------------------------------------------------------------------- types
26
+
27
+ /** The slice of `MegaRuntime` the observer bridge reads/writes. */
28
+ export interface VcObserverContext {
29
+ appendEvent(event: string, fields: Record<string, unknown>): void;
30
+ readonly currentStateDir: string;
31
+ }
32
+
33
+ // ------------------------------------------------------------- construction
34
+
35
+ /**
36
+ * Build the production-wired eval observer (mode A) if the VC0A flag is on.
37
+ * Returns null when the flag is off or on any construction error (non-fatal).
38
+ * The returned observer is a thin, best-effort observability seam that never
39
+ * breaks the agent loop.
40
+ */
41
+ export function createVcObserver(self: VcObserverContext): EvalObserver | null {
42
+ if (!VC0A_ENABLED()) return null;
43
+ try {
44
+ return createEvalObserver({
45
+ emit: self.appendEvent.bind(self),
46
+ persist: (sample) => appendEvalRow(self.currentStateDir, [sample]),
47
+ });
48
+ } catch {
49
+ /* non-fatal observability — never break the agent loop */
50
+ return null;
51
+ }
52
+ }
53
+
54
+ // ---------------------------------------------------------------- weak cache
55
+
56
+ /**
57
+ * Module-level observer cache keyed by the runtime object. Avoids adding a
58
+ * `vcObserver` field to MegaRuntime (keeping runtime.ts under its soft limit
59
+ * — no field, no constructor line, no import bloat). One observer per runtime
60
+ * instance, lazily built on the first latency recording.
61
+ */
62
+ const observerCache = new WeakMap<object, EvalObserver | null>();
63
+
64
+ /** Lazily create + cache the observer for the given runtime context. */
65
+ export function getVcObserver(self: VcObserverContext): EvalObserver | null {
66
+ let obs = observerCache.get(self);
67
+ if (obs === undefined) {
68
+ obs = createVcObserver(self);
69
+ observerCache.set(self, obs);
70
+ }
71
+ return obs;
72
+ }
73
+
74
+ // ---------------------------------------------------------------- reporting
75
+
76
+ /** Record a compact latency sample on the observer (no-op when absent). */
77
+ export function recordCompactLatency(
78
+ self: VcObserverContext,
79
+ elapsedMs: number,
80
+ sessionId: string,
81
+ epoch: number,
82
+ ): void {
83
+ const obs = getVcObserver(self);
84
+ if (obs == null) return;
85
+ try {
86
+ obs.record({
87
+ session: sessionId,
88
+ seq: epoch,
89
+ event: "compact",
90
+ value: elapsedMs,
91
+ unit: "ms",
92
+ mode: "A",
93
+ });
94
+ } catch {
95
+ /* non-fatal observability */
96
+ }
97
+ }
98
+
99
+ /** Record a recall latency sample on the observer (no-op when absent). */
100
+ export function recordRecallLatency(
101
+ self: VcObserverContext,
102
+ elapsedMs: number,
103
+ sessionId: string,
104
+ epoch: number,
105
+ ): void {
106
+ const obs = getVcObserver(self);
107
+ if (obs == null) return;
108
+ try {
109
+ obs.record({
110
+ session: sessionId,
111
+ seq: epoch,
112
+ event: "recall",
113
+ value: elapsedMs,
114
+ unit: "ms",
115
+ mode: "A",
116
+ });
117
+ } catch {
118
+ /* non-fatal observability */
119
+ }
120
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.21",
3
+ "version": "0.20.23",
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,83 @@
1
+ /**
2
+ * platform/_cross-language-fixture.ts — VC8C cross-language fixture I/O.
3
+ *
4
+ * Reads conformance fixtures from the v2 `cross-language/` domain and decodes
5
+ * them into the REAL production types (`ParityReportV1`, `EngineAbiV1`,
6
+ * `NeutralRecord`) so `selectEngine` and the framing round-trip run against
7
+ * the committed corpus — no mocks, no stubs.
8
+ *
9
+ * Mirrors `_diagnostics-fixture.ts` (VC7C) and `_adaptive-fixture.ts` (VC8B).
10
+ */
11
+
12
+ import { readFileSync, existsSync } from "node:fs";
13
+ import { join, dirname } from "node:path";
14
+ import { fileURLToPath } from "node:url";
15
+
16
+ import type { ParityReportV1 } from "./types.js";
17
+
18
+ const here = dirname(fileURLToPath(import.meta.url));
19
+
20
+ function repoRoot(from: string): string {
21
+ let dir = from;
22
+ for (let i = 0; i < 8; i++) {
23
+ if (existsSync(join(dir, "conformance", "vector-cortex"))) return dir;
24
+ const next = dirname(dir);
25
+ if (next === dir) break;
26
+ dir = next;
27
+ }
28
+ throw new Error("conformance corpus not found above " + from);
29
+ }
30
+
31
+ const DIR = join(repoRoot(here), "conformance", "vector-cortex", "v2", "cross-language");
32
+
33
+ /** Read + parse one cross-language conformance fixture by ID. */
34
+ export function crossLanguageFixture(id: string): Record<string, unknown> {
35
+ const raw = readFileSync(join(DIR, `${id}.json`), "utf8");
36
+ return JSON.parse(raw);
37
+ }
38
+
39
+ // ── Cross-language golden / error fixture shape ───────────────────────────────
40
+
41
+ interface CrossLangFx {
42
+ id: string;
43
+ fixtureId: string;
44
+ kind: string;
45
+ assertion: string;
46
+ inputHex: string;
47
+ expected: { ok: boolean; code?: string };
48
+ expectedFailureCode: string | null;
49
+ expectedOutputHex: string;
50
+ }
51
+
52
+ /** Read a numbered or named cross-language fixture as a typed CrossLangFx. */
53
+ export function xlangFx(id: string): CrossLangFx {
54
+ return crossLanguageFixture(id) as unknown as CrossLangFx;
55
+ }
56
+
57
+ /** Build a ParityReportV1 whose matrix has exactly one failing row at `badIndex`. */
58
+ export function reportWithMatrixFailure(
59
+ badIndex: number,
60
+ code: string,
61
+ digest: string,
62
+ platform: string,
63
+ ): ParityReportV1 {
64
+ const matrix = Array.from({ length: 30 }, (_, i) => ({
65
+ fixtureId: `RUST-${String(i + 1).padStart(3, "0")}`,
66
+ ok: i === badIndex ? false : true,
67
+ code: i === badIndex ? code : null,
68
+ }));
69
+ return {
70
+ schema: "parity-report-v1",
71
+ artifactUrl: "file:///local/rad",
72
+ commit: "0".repeat(40),
73
+ cargoLockDigest: "a".repeat(64),
74
+ artifactCargoLockDigest: digest,
75
+ platform,
76
+ matrix,
77
+ };
78
+ }
79
+
80
+ /** Build a fully-qualified ParityReportV1 (all matrix rows ok). */
81
+ export function okReport(digest: string, platform: string): ParityReportV1 {
82
+ return reportWithMatrixFailure(-1, "", digest, platform);
83
+ }