pi-mega-compact 0.19.2 → 0.19.3

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 (78) hide show
  1. package/dist/config/vector-cortex.js +12 -0
  2. package/dist/config.js +1 -1
  3. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +1 -0
  4. package/dist/extensions/dashboard-server/routes-vector-cortex-query.js +46 -0
  5. package/dist/extensions/dashboard-server/routes-vector-cortex.js +1 -0
  6. package/dist/extensions/dashboard-server/routes.js +1 -1
  7. package/dist/extensions/dashboard-server/server.js +3 -1
  8. package/dist/src/config/vector-cortex.js +12 -0
  9. package/dist/src/config.js +1 -1
  10. package/dist/src/tieredRouter.js +8 -0
  11. package/dist/src/vector-cortex/migrations/router-generation-v2.js +252 -0
  12. package/dist/src/vector-cortex/topology/query.js +342 -0
  13. package/dist/vector-cortex/migrations/router-generation-v2.js +252 -0
  14. package/dist/vector-cortex/topology/query.js +342 -0
  15. package/extensions/dashboard-client/dist/assets/{AreaChart-CSaJZXru.js → AreaChart-DqiFSnDx.js} +2 -2
  16. package/extensions/dashboard-client/dist/assets/{AreaChart-CSaJZXru.js.map → AreaChart-DqiFSnDx.js.map} +1 -1
  17. package/extensions/dashboard-client/dist/assets/{BarChart-CQouNj1g.js → BarChart-Cqg-PQMK.js} +2 -2
  18. package/extensions/dashboard-client/dist/assets/{BarChart-CQouNj1g.js.map → BarChart-Cqg-PQMK.js.map} +1 -1
  19. package/extensions/dashboard-client/dist/assets/{CacheTab-m_eiCYoL.js → CacheTab-BPNs0xIF.js} +2 -2
  20. package/extensions/dashboard-client/dist/assets/{CacheTab-m_eiCYoL.js.map → CacheTab-BPNs0xIF.js.map} +1 -1
  21. package/extensions/dashboard-client/dist/assets/{EventsTab-DRyOwjYN.js → EventsTab-BQf7OGkf.js} +2 -2
  22. package/extensions/dashboard-client/dist/assets/{EventsTab-DRyOwjYN.js.map → EventsTab-BQf7OGkf.js.map} +1 -1
  23. package/extensions/dashboard-client/dist/assets/{HealthTab-Dj9k40TJ.js → HealthTab-B14TaSPN.js} +2 -2
  24. package/extensions/dashboard-client/dist/assets/{HealthTab-Dj9k40TJ.js.map → HealthTab-B14TaSPN.js.map} +1 -1
  25. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-Bk9mhYfD.js → MaintenanceTab-D8lkRoa1.js} +2 -2
  26. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-Bk9mhYfD.js.map → MaintenanceTab-D8lkRoa1.js.map} +1 -1
  27. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-C7ve49VG.js → MemoryMapTab-BOWtGfpx.js} +2 -2
  28. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-C7ve49VG.js.map → MemoryMapTab-BOWtGfpx.js.map} +1 -1
  29. package/extensions/dashboard-client/dist/assets/{MetricsTab-DzjhqZA9.js → MetricsTab-7XI9cWaX.js} +2 -2
  30. package/extensions/dashboard-client/dist/assets/{MetricsTab-DzjhqZA9.js.map → MetricsTab-7XI9cWaX.js.map} +1 -1
  31. package/extensions/dashboard-client/dist/assets/{OverviewTab-CFQdfIm_.js → OverviewTab-C9KaGYvh.js} +2 -2
  32. package/extensions/dashboard-client/dist/assets/{OverviewTab-CFQdfIm_.js.map → OverviewTab-C9KaGYvh.js.map} +1 -1
  33. package/extensions/dashboard-client/dist/assets/{ReposTab-DyjwEFUr.js → ReposTab-BwESNhox.js} +2 -2
  34. package/extensions/dashboard-client/dist/assets/{ReposTab-DyjwEFUr.js.map → ReposTab-BwESNhox.js.map} +1 -1
  35. package/extensions/dashboard-client/dist/assets/{SessionsTab-njLIy7ab.js → SessionsTab-DKH55Rda.js} +2 -2
  36. package/extensions/dashboard-client/dist/assets/{SessionsTab-njLIy7ab.js.map → SessionsTab-DKH55Rda.js.map} +1 -1
  37. package/extensions/dashboard-client/dist/assets/{SetupTab-UHIs8-Lh.js → SetupTab-zDxf-nJd.js} +2 -2
  38. package/extensions/dashboard-client/dist/assets/{SetupTab-UHIs8-Lh.js.map → SetupTab-zDxf-nJd.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-inWHjCxg.js → TimeSavedCard-C9isog4L.js} +2 -2
  40. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-inWHjCxg.js.map → TimeSavedCard-C9isog4L.js.map} +1 -1
  41. package/extensions/dashboard-client/dist/assets/{TurnsTab-CXJYugBu.js → TurnsTab-ciI6ydvj.js} +2 -2
  42. package/extensions/dashboard-client/dist/assets/{TurnsTab-CXJYugBu.js.map → TurnsTab-ciI6ydvj.js.map} +1 -1
  43. package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js +2 -0
  44. package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js.map +1 -0
  45. package/extensions/dashboard-client/dist/assets/{WikiTab-BJ4A2oi9.js → WikiTab-DcYHc734.js} +2 -2
  46. package/extensions/dashboard-client/dist/assets/{WikiTab-BJ4A2oi9.js.map → WikiTab-DcYHc734.js.map} +1 -1
  47. package/extensions/dashboard-client/dist/assets/{button-BWl-moyn.js → button-DF8lx4bt.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{button-BWl-moyn.js.map → button-DF8lx4bt.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/{card-C58RVPOk.js → card-DIoDheSZ.js} +2 -2
  50. package/extensions/dashboard-client/dist/assets/{card-C58RVPOk.js.map → card-DIoDheSZ.js.map} +1 -1
  51. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-vgeOQU3L.js → generateCategoricalChart-BFRzeYX3.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-vgeOQU3L.js.map → generateCategoricalChart-BFRzeYX3.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/{index-lVrWQjpu.js → index-DSZON0Yi.js} +3 -3
  54. package/extensions/dashboard-client/dist/assets/{index-lVrWQjpu.js.map → index-DSZON0Yi.js.map} +1 -1
  55. package/extensions/dashboard-client/dist/assets/{switch-CSeplFQT.js → switch-l4qo22mU.js} +2 -2
  56. package/extensions/dashboard-client/dist/assets/{switch-CSeplFQT.js.map → switch-l4qo22mU.js.map} +1 -1
  57. package/extensions/dashboard-client/dist/assets/{toggle-CQRxgVRi.js → toggle-DPn6td1_.js} +2 -2
  58. package/extensions/dashboard-client/dist/assets/{toggle-CQRxgVRi.js.map → toggle-DPn6td1_.js.map} +1 -1
  59. package/extensions/dashboard-client/dist/assets/{useSSE-BUz6IOI-.js → useSSE-BLmolqTJ.js} +2 -2
  60. package/extensions/dashboard-client/dist/assets/{useSSE-BUz6IOI-.js.map → useSSE-BLmolqTJ.js.map} +1 -1
  61. package/extensions/dashboard-client/dist/index.html +1 -1
  62. package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
  63. package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +27 -0
  64. package/extensions/dashboard-client/src/types/vector-cortex.ts +12 -0
  65. package/extensions/dashboard-server/api-contracts/vector-cortex.ts +17 -0
  66. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +6 -0
  67. package/extensions/dashboard-server/routes-vector-cortex-query.ts +54 -0
  68. package/extensions/dashboard-server/routes-vector-cortex.ts +1 -0
  69. package/extensions/dashboard-server/routes.ts +1 -0
  70. package/extensions/dashboard-server/server.ts +2 -0
  71. package/package.json +1 -1
  72. package/src/config/vector-cortex.ts +13 -0
  73. package/src/config.ts +1 -0
  74. package/src/tieredRouter.ts +9 -0
  75. package/src/vector-cortex/migrations/router-generation-v2.ts +317 -0
  76. package/src/vector-cortex/topology/query.ts +485 -0
  77. package/extensions/dashboard-client/dist/assets/VectorCortexTab-DkS0Azx9.js +0 -2
  78. package/extensions/dashboard-client/dist/assets/VectorCortexTab-DkS0Azx9.js.map +0 -1
@@ -110,6 +110,18 @@ export const VC3A_ENABLED = () => sprintFlag("MEGACOMPACT_VC3A");
110
110
  * the topology build seam (build.ts) and the dashboard topology node/edge view.
111
111
  */
112
112
  export const VC3B_ENABLED = () => sprintFlag("MEGACOMPACT_VC3B");
113
+ /**
114
+ * VC3C — topology query + router invalidation (TopologyQueryV1 / RouterKeyV2,
115
+ * M6 router-generation-v2 migration).
116
+ * Default ON. `MEGACOMPACT_VC3C=0` disables and is byte-identical to the
117
+ * predecessor (mode C: no length-delimited router key, no generation
118
+ * invalidation, zero `vector_cortex_router_generation_invalidated` /
119
+ * `vector_cortex_topology_query_demoted` emissions; the predecessor tiered
120
+ * router keying is unchanged). The real consumers are the topology query seam
121
+ * (query.ts), the M6 router-generation-v2 migration seam, and the reader-only
122
+ * dashboard topology query diagnostic view.
123
+ */
124
+ export const VC3C_ENABLED = () => sprintFlag("MEGACOMPACT_VC3C");
113
125
  // ---------------------------------------------------------------------------
114
126
  // Breaker state machine constants (TRIAD_RESILIENCE.md §breaker).
115
127
  // Rolled numbers for one 60s window; VC0C consumes these at its breaker seam.
package/dist/config.js CHANGED
@@ -114,4 +114,4 @@ export const NEW_UI = () => ragEnabled("MEGACOMPACT_NEW_UI");
114
114
  // default ON, `=0`/`_DISABLED` off. Re-exported from src/config/vector-cortex.ts
115
115
  // so root consumers share one source of truth.
116
116
  // ---------------------------------------------------------------------------
117
- export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
117
+ export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, VC3C_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
@@ -145,6 +145,7 @@ export const SETTINGS = [
145
145
  boolDirect("MEGACOMPACT_VC2C", "VC2C Encoder Qualification + Calibration", "QualifiedEncoderV1/CalibrationV1: calibration fit on the calibration split only (held-out labels prohibited) + atomic selection across MODEL_ASSET and per-head EVALUATION thresholds (any field failure demotes all of A). OFF = mode C, no qualification/calibration selection, byte-identical predecessor.", true),
146
146
  boolDirect("MEGACOMPACT_VC3A", "VC3A Cortex Store", "Capability-gated derived cortex store (CortexReader/Writer/Admin + CortexRecordV1): additive, keyed (sourceHighWater, algorithmVersion, id), immutable records, deterministic generation rebuild + one root digest. OFF = mode C, no cortex records written, byte-identical predecessor.", true),
147
147
  boolDirect("MEGACOMPACT_VC3B", "VC3B Deterministic Topology", "Deterministic cortical topology (TopologyV1/EdgeV1): per-(source,head) top-k=16/head calibrated-threshold edges, stable score-desc/then-target-ID sort, dependency directed + contradiction symmetric paired records, one stable generation digest. OFF = mode C, no topology graph built/emitted, predecessor-precise topology view, byte-identical predecessor.", true),
148
+ boolDirect("MEGACOMPACT_VC3C", "VC3C Topology Query + Router Invalidation", "TopologyQueryV1/RouterKeyV2 structured keys (length-delimited, unsigned-byte order, no prefix ambiguity), exact (session,generation) invalidation, stale-generation rejection (TOP_GENERATION_STALE), and the M6 router-generation-v2 copy/validate/switch migration. OFF = mode C, no structured router key / generation invalidation, byte-identical predecessor.", true),
148
149
  ],
149
150
  },
150
151
  {
@@ -0,0 +1,46 @@
1
+ /**
2
+ * dashboard-server/routes-vector-cortex-query.ts — VC3C vector-cortex query-layer
3
+ * diagnostics dashboard route.
4
+ *
5
+ * Reader-only GET /api/vector-cortex/query. Purely a flag-status + structural
6
+ * diagnostic: the VC3C query index is in-memory (not durable), so this route
7
+ * reports whether the VC3C flag is enabled, the router-generation v2 version
8
+ * constant, and a snapshot timestamp. It NEVER exposes payloads or prompts.
9
+ * Non-fatal: a missing state dir (or any internal error) degrades to
10
+ * `enabled:false`.
11
+ *
12
+ * Guardrails: PREVENT-PI-004 (local flag/config read only, no network),
13
+ * PREVENT-011 (no `any`), reader-only aggregate (never payloads/prompts).
14
+ */
15
+ import { VC3C_ENABLED } from "../../src/config.js";
16
+ import { ROUTER_KEY_VERSION } from "../../src/vector-cortex/topology/query.js";
17
+ import { sendJson } from "./routes-vector-cortex-shared.js";
18
+ /**
19
+ * Reader-only GET /api/vector-cortex/query (VC3C query-layer diagnostics).
20
+ */
21
+ export function handleVectorCortexQuery(req, res, _ctx) {
22
+ const url = req.url ?? "";
23
+ const path = url.split("?")[0] ?? url;
24
+ if (path !== "/api/vector-cortex/query")
25
+ return false;
26
+ if (req.method !== "GET") {
27
+ // Reader-only path: no mutation endpoint lives at /query.
28
+ sendJson(res, 405, { error: "method_not_allowed" });
29
+ return true;
30
+ }
31
+ let enabled = false;
32
+ try {
33
+ enabled = VC3C_ENABLED();
34
+ }
35
+ catch {
36
+ // Non-fatal: a missing/unavailable flag degrades to `enabled:false`.
37
+ enabled = false;
38
+ }
39
+ const body = {
40
+ enabled,
41
+ routerVersion: ROUTER_KEY_VERSION,
42
+ updatedAt: new Date().toISOString(),
43
+ };
44
+ sendJson(res, 200, body);
45
+ return true;
46
+ }
@@ -17,3 +17,4 @@ export { handleVectorCortexEvaluation } from "./routes-vector-cortex-eval.js";
17
17
  export { handleVectorCortexHealth, handleVectorCortexBreakersReset } from "./routes-vector-cortex-health.js";
18
18
  export { handleVectorCortexLedger } from "./routes-vector-cortex-ledger.js";
19
19
  export { handleVectorCortexTopology } from "./routes-vector-cortex-topology.js";
20
+ export { handleVectorCortexQuery } from "./routes-vector-cortex-query.js";
@@ -23,4 +23,4 @@ export { handleRagSettings } from "./routes-rag-settings.js";
23
23
  export { handleRagMetrics } from "./routes-rag-metrics.js";
24
24
  export { handleModelThresholds } from "./routes-model-thresholds.js";
25
25
  export { handleWiki } from "./routes-wiki.js";
26
- export { handleVectorCortexEvaluation, handleVectorCortexHealth, handleVectorCortexBreakersReset, handleVectorCortexLedger, handleVectorCortexTopology, } from "./routes-vector-cortex.js";
26
+ export { handleVectorCortexEvaluation, handleVectorCortexHealth, handleVectorCortexBreakersReset, handleVectorCortexLedger, handleVectorCortexTopology, handleVectorCortexQuery, } from "./routes-vector-cortex.js";
@@ -13,7 +13,7 @@ import { fileURLToPath } from "node:url";
13
13
  import { createRequire } from "node:module";
14
14
  import { NEW_UI } from "../../src/config.js";
15
15
  import { log, setLogPath, setDashboardServerVersion } from "./state.js";
16
- import { buildRouteContext, handleIndex, handleRepoIndex, handleEvents, handleGameState, handleGameScores, handlePerf, handlePerfSamples, handleAchievements, handleSessions, handleTopics, handleTurns, handleMaintenance, handleProviderCache, handleCacheStripes, handleMemoryStatus, handleSetupStatus, handleSetupDetect, handleSetupConfigure, handleMemoryMap, handleRaptorTree, handleRaptorBuildHistory, handleContextHealth, handleCachePoison, handleHealthSettings, handleEmbedderHealth, handleRagSettings, handleRagMetrics, handleModelThresholds, handleWiki, handleVectorCortexEvaluation, handleVectorCortexHealth, handleVectorCortexBreakersReset, handleVectorCortexLedger, handleVectorCortexTopology, handleStatic, } from "./routes.js";
16
+ import { buildRouteContext, handleIndex, handleRepoIndex, handleEvents, handleGameState, handleGameScores, handlePerf, handlePerfSamples, handleAchievements, handleSessions, handleTopics, handleTurns, handleMaintenance, handleProviderCache, handleCacheStripes, handleMemoryStatus, handleSetupStatus, handleSetupDetect, handleSetupConfigure, handleMemoryMap, handleRaptorTree, handleRaptorBuildHistory, handleContextHealth, handleCachePoison, handleHealthSettings, handleEmbedderHealth, handleRagSettings, handleRagMetrics, handleModelThresholds, handleWiki, handleVectorCortexEvaluation, handleVectorCortexHealth, handleVectorCortexBreakersReset, handleVectorCortexLedger, handleVectorCortexTopology, handleVectorCortexQuery, handleStatic, } from "./routes.js";
17
17
  export async function launchDashboardServer(stateDir) {
18
18
  // Our own package version — exposed at /api/version so the launcher can
19
19
  // detect a stale server (started by an older build) and replace it on
@@ -251,6 +251,8 @@ export async function launchDashboardServer(stateDir) {
251
251
  return;
252
252
  if (handleVectorCortexTopology(req, res, ctx))
253
253
  return;
254
+ if (handleVectorCortexQuery(req, res, ctx))
255
+ return;
254
256
  handleStatic(req, res, ctx);
255
257
  });
256
258
  // Bind base + range are env-configurable so tests can use a private,
@@ -110,6 +110,18 @@ export const VC3A_ENABLED = () => sprintFlag("MEGACOMPACT_VC3A");
110
110
  * the topology build seam (build.ts) and the dashboard topology node/edge view.
111
111
  */
112
112
  export const VC3B_ENABLED = () => sprintFlag("MEGACOMPACT_VC3B");
113
+ /**
114
+ * VC3C — topology query + router invalidation (TopologyQueryV1 / RouterKeyV2,
115
+ * M6 router-generation-v2 migration).
116
+ * Default ON. `MEGACOMPACT_VC3C=0` disables and is byte-identical to the
117
+ * predecessor (mode C: no length-delimited router key, no generation
118
+ * invalidation, zero `vector_cortex_router_generation_invalidated` /
119
+ * `vector_cortex_topology_query_demoted` emissions; the predecessor tiered
120
+ * router keying is unchanged). The real consumers are the topology query seam
121
+ * (query.ts), the M6 router-generation-v2 migration seam, and the reader-only
122
+ * dashboard topology query diagnostic view.
123
+ */
124
+ export const VC3C_ENABLED = () => sprintFlag("MEGACOMPACT_VC3C");
113
125
  // ---------------------------------------------------------------------------
114
126
  // Breaker state machine constants (TRIAD_RESILIENCE.md §breaker).
115
127
  // Rolled numbers for one 60s window; VC0C consumes these at its breaker seam.
@@ -114,4 +114,4 @@ export const NEW_UI = () => ragEnabled("MEGACOMPACT_NEW_UI");
114
114
  // default ON, `=0`/`_DISABLED` off. Re-exported from src/config/vector-cortex.ts
115
115
  // so root consumers share one source of truth.
116
116
  // ---------------------------------------------------------------------------
117
- export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
117
+ export { VC0A_ENABLED, VC0B_ENABLED, VC1A_ENABLED, VC0C_ENABLED, VC1B_ENABLED, VC1C_ENABLED, VC2A_ENABLED, VC2B_ENABLED, VC2C_ENABLED, VC3A_ENABLED, VC3B_ENABLED, VC3C_ENABLED, BREAKER_WINDOW_MS, BREAKER_MIN_ATTEMPTS, BREAKER_PERF_FAILURES, BREAKER_PERF_FAILURE_RATE, BREAKER_CORRECTNESS_FAILURES, BREAKER_COOLDOWN_MS, BREAKER_PROBE_COUNT, BREAKER_RETRY_BASE_MS, BREAKER_RETRY_CAP_MS, BREAKER_RETRY_JITTER, BREAKER_HYSTERESIS_FAILURE_RATE, BREAKER_HYSTERESIS_BUDGET_P95_MS, BREAKER_MIN_HEALTHY_RESIDENCE_MS, } from "./config/vector-cortex.js";
@@ -15,6 +15,9 @@
15
15
  import { createHash } from "node:crypto";
16
16
  import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
17
17
  import { dirname, join } from "node:path";
18
+ import { VC3C_ENABLED } from "./config.js";
19
+ import { Logger } from "./log.js";
20
+ import { routerGenerationInvalidationSeam } from "./vector-cortex/topology/query.js";
18
21
  import { openStore } from "./store/sqlite/utils.js";
19
22
  import { fts5SearchScoped } from "./store/sqlite/fts5-search.js";
20
23
  import { listCheckpoints } from "./store/sqlite.js";
@@ -287,6 +290,11 @@ export class TieredRouter {
287
290
  this.cacheMap.clear();
288
291
  this.embeddingCache.clear();
289
292
  }
293
+ /** VC3C narrow delegate: exact (session,generation) RouterKeyV2 invalidation.
294
+ * No routing rewrite; `MEGACOMPACT_VC3C=0` is a strict no-op, byte-identical. */
295
+ invalidateGeneration(key) {
296
+ routerGenerationInvalidationSeam(key, VC3C_ENABLED(), (e, f) => new Logger().info(e, f));
297
+ }
290
298
  /** Snapshot of current metrics for dashboard / logging. */
291
299
  getMetrics() {
292
300
  const q = Math.max(this.totalQueries, 1);
@@ -0,0 +1,252 @@
1
+ /**
2
+ * vector-cortex/migrations/router-generation-v2.ts — M6 copy/validate/switch
3
+ * migration (VC3C).
4
+ *
5
+ * M6 versions the tiered router's query set: the predecessor routed on ad-hoc
6
+ * per-session keys (no generation identity); VC3C routes on the structured,
7
+ * length-delimited `RouterKeyV2`. M6 copies the old per-session query set into
8
+ * v2 `RouterGenV2Row`s BESIDE the legacy state, validates, and only then
9
+ * atomically switches the active version pointer — the same copy/validate/
10
+ * switch + resume contract as M4.
11
+ *
12
+ * - copy: resumable per (session, old key) — an interrupted run resumes
13
+ * without duplicate rows or active-pointer drift.
14
+ * - validate: every old query key has exactly one v2 row, every row's key is
15
+ * canonical + re-hashes, and — the VC3C-specific invariant — the new query
16
+ * set is partition-preserving: a row derived from an old key of session S
17
+ * MUST carry session S. A row whose session differs is rejected as
18
+ * cross-session eviction (`M6_CROSS_SESSION_EVICTION`): one session may
19
+ * never lose or steal another session's query identity.
20
+ * - switch: atomically flips the ACTIVE VERSION pointer from 1 (legacy) to 2
21
+ * (router-generation-v2) only after a verified copy; interruption keeps the
22
+ * old active pointer.
23
+ *
24
+ * Pure logic over an injected M6Host (deterministic + testable; no console).
25
+ * PREVENT-002/011/PI-004 honored.
26
+ */
27
+ import { createHash } from "node:crypto";
28
+ import { encodeRouterKeyV2, decodeRouterKeyV2, } from "../topology/query.js";
29
+ /** The active-version value the v2 router generation pointer is switched to. */
30
+ export const ROUTER_GEN_V2_VERSION = 2;
31
+ /** The legacy (predecessor) active-version value before any verified switch. */
32
+ export const ROUTER_GEN_LEGACY_VERSION = 1;
33
+ /** M6 failure codes. */
34
+ export const M6_FAIL = {
35
+ VERSION_MISMATCH: "M6_VERSION_MISMATCH",
36
+ COPY_PARTIAL: "M6_COPY_PARTIAL",
37
+ COUNT_MISMATCH: "M6_COUNT_MISMATCH",
38
+ DIGEST_MISMATCH: "M6_DIGEST_MISMATCH",
39
+ BAD_OLD_KEY: "M6_BAD_OLD_KEY",
40
+ CROSS_SESSION_EVICTION: "M6_CROSS_SESSION_EVICTION",
41
+ SWITCH_PRECONDITION: "M6_SWITCH_PRECONDITION",
42
+ };
43
+ /** Derive one v2 row from an old key (authoritative bytes). */
44
+ export function deriveRouterGenRow(host, session, oldKey) {
45
+ const parsed = host.parseOldKey(session, oldKey);
46
+ if (!parsed.ok)
47
+ return { ok: false, session, oldKey };
48
+ const key = parsed.key;
49
+ // Cross-session guard at COPY time: the structured key's session must equal
50
+ // the slot session. A key that claims a different session is never copied.
51
+ if (key.session !== session)
52
+ return { ok: false, session, oldKey };
53
+ const encoded = encodeRouterKeyV2(key);
54
+ return {
55
+ ok: true,
56
+ row: {
57
+ session,
58
+ generation: key.generation,
59
+ sourceStart: key.sourceStart,
60
+ sourceEnd: key.sourceEnd,
61
+ algorithm: key.algorithm,
62
+ key: encoded,
63
+ digest: sha256Hex(Buffer.from(encoded.slice(4), "hex")),
64
+ },
65
+ };
66
+ }
67
+ function sha256Hex(bytes) {
68
+ return createHash("sha256").update(bytes).digest("hex");
69
+ }
70
+ /**
71
+ * Copy the full old query set into v2 rows (resumable): rows already persisted
72
+ * with a matching canonical key + digest are left untouched (no duplicates);
73
+ * absent or corrupt (wrong session / wrong key / stale digest / bad old key)
74
+ * slots are (re)written from the authoritative derivation. Returns the delta
75
+ * written this call. The active pointer stays at the legacy version until a
76
+ * verified switch.
77
+ */
78
+ export function m6Copy(host) {
79
+ const existing = new Map(host.existingV2().map((r) => [`${r.session}::${r.key}`, r]));
80
+ const wanted = [];
81
+ for (const session of host.sessions()) {
82
+ for (const oldKey of host.oldKeysOf(session)) {
83
+ const derived = deriveRouterGenRow(host, session, oldKey);
84
+ if (!derived.ok)
85
+ continue; // bad old key surfaced by verify, not lost silently
86
+ const stored = existing.get(`${session}::${derived.row.key}`);
87
+ if (stored !== undefined && rowMatches(stored, derived.row))
88
+ continue; // healthy
89
+ wanted.push(derived.row);
90
+ }
91
+ }
92
+ if (wanted.length > 0)
93
+ host.putV2(wanted);
94
+ return { written: wanted, all: [...host.existingV2()] };
95
+ }
96
+ /** True when a persisted row already matches the authoritative derivation. */
97
+ function rowMatches(stored, fresh) {
98
+ return (stored.session === fresh.session &&
99
+ stored.generation === fresh.generation &&
100
+ stored.sourceStart === fresh.sourceStart &&
101
+ stored.sourceEnd === fresh.sourceEnd &&
102
+ stored.algorithm === fresh.algorithm &&
103
+ stored.key === fresh.key &&
104
+ stored.digest === fresh.digest);
105
+ }
106
+ /**
107
+ * Verify the migrated query set: every old query key across every session has a
108
+ * v2 row (exact-once), every row's key is canonical + re-hashes, and the new set
109
+ * is partition-preserving — every row's session is exactly the session of the
110
+ * old key it was derived from (rejects cross-session eviction). Never mutates.
111
+ *
112
+ * Comparison of old/new query sets is exact-by-identity (structured key bytes),
113
+ * never by string prefix.
114
+ */
115
+ export function m6Verify(host) {
116
+ const codes = [];
117
+ const stored = host.existingV2();
118
+ // Expected slot inventory: (session, canonical key) for every derivable old key.
119
+ const expectedSlots = [];
120
+ const badOldKeys = [];
121
+ for (const session of host.sessions()) {
122
+ for (const oldKey of host.oldKeysOf(session)) {
123
+ const parsed = host.parseOldKey(session, oldKey);
124
+ if (!parsed.ok) {
125
+ badOldKeys.push({ session, oldKey });
126
+ continue;
127
+ }
128
+ if (parsed.key.session !== session) {
129
+ // A structured key that claims a different session is a cross-session
130
+ // eviction attempt — never migrated, never evicted.
131
+ codes.push(M6_FAIL.CROSS_SESSION_EVICTION);
132
+ continue;
133
+ }
134
+ expectedSlots.push({ session, oldKey });
135
+ }
136
+ }
137
+ if (badOldKeys.length > 0)
138
+ codes.push(M6_FAIL.BAD_OLD_KEY);
139
+ // Map stored rows by their (session,key) identity.
140
+ const byIdentity = new Map();
141
+ const duplicateCheck = new Map();
142
+ for (const r of stored) {
143
+ const ident = `${r.session}::${r.key}`;
144
+ byIdentity.set(ident, r);
145
+ duplicateCheck.set(ident, (duplicateCheck.get(ident) ?? 0) + 1);
146
+ }
147
+ // Each expected slot must be present exactly once, with a healthy row.
148
+ for (const slot of expectedSlots) {
149
+ const parsed = host.parseOldKey(slot.session, slot.oldKey);
150
+ if (!parsed.ok)
151
+ continue;
152
+ const expectedKey = encodeRouterKeyV2(parsed.key);
153
+ const ident = `${slot.session}::${expectedKey}`;
154
+ const count = duplicateCheck.get(ident) ?? 0;
155
+ if (count === 0)
156
+ codes.push(M6_FAIL.COPY_PARTIAL);
157
+ if (count > 1)
158
+ codes.push(M6_FAIL.COUNT_MISMATCH);
159
+ }
160
+ // Row health: digest re-hashes + session partition-preserving.
161
+ for (const r of stored) {
162
+ if (!r.key.startsWith("rk2:"))
163
+ codes.push(M6_FAIL.DIGEST_MISMATCH);
164
+ const expectedDigest = sha256Hex(Buffer.from(r.key.slice(4), "hex"));
165
+ if (r.digest !== expectedDigest)
166
+ codes.push(M6_FAIL.DIGEST_MISMATCH);
167
+ // The row's own structured key must decode and agree with its declared fields.
168
+ const dec = decodeRow(r);
169
+ if (!dec)
170
+ codes.push(M6_FAIL.DIGEST_MISMATCH);
171
+ }
172
+ // No stored row may be an orphan (no corresponding old key anywhere).
173
+ const expectedIdentities = new Set();
174
+ for (const slot of expectedSlots) {
175
+ const parsed = host.parseOldKey(slot.session, slot.oldKey);
176
+ if (!parsed.ok)
177
+ continue;
178
+ expectedIdentities.add(`${slot.session}::${encodeRouterKeyV2(parsed.key)}`);
179
+ }
180
+ for (const ident of byIdentity.keys()) {
181
+ if (!expectedIdentities.has(ident))
182
+ codes.push(M6_FAIL.COUNT_MISMATCH);
183
+ }
184
+ const ok = codes.length === 0;
185
+ return { ok, codes: dedupe(codes) };
186
+ }
187
+ /**
188
+ * Self-consistent decode of a stored row: its canonical key decodes to a
189
+ * RouterKeyV2 whose five fields equal the row's declared fields. A row whose key
190
+ * doesn't round-trip against its own declared session/generation/range/algorithm
191
+ * is corrupt (cannot be trusted as the query set's identity).
192
+ */
193
+ function decodeRow(r) {
194
+ if (!r.key.startsWith("rk2:"))
195
+ return false;
196
+ const hex = r.key.slice(4);
197
+ if (!/^[0-9a-f]+$/.test(hex) || hex.length % 2 !== 0)
198
+ return false;
199
+ const dec = decodeRouterKeyV2(r.key);
200
+ if (!dec.ok)
201
+ return false;
202
+ return (dec.key.session === r.session &&
203
+ dec.key.generation === r.generation &&
204
+ dec.key.sourceStart === r.sourceStart &&
205
+ dec.key.sourceEnd === r.sourceEnd &&
206
+ dec.key.algorithm === r.algorithm);
207
+ }
208
+ /**
209
+ * Switch the active version to 2. Only call after `m6Verify` reports ok; a
210
+ * crash/return before switch leaves the legacy pointer active (interruption
211
+ * keeps old authority, resumable idempotently).
212
+ */
213
+ export function m6Switch(host) {
214
+ if (host.activeVersion() !== ROUTER_GEN_LEGACY_VERSION)
215
+ return;
216
+ host.switchToV2();
217
+ }
218
+ /** Full M6 lifecycle: copy -> verify -> switch; returns the verify result. */
219
+ export function migrateRouterGenerationV2(host) {
220
+ m6Copy(host);
221
+ const v = m6Verify(host);
222
+ if (v.ok)
223
+ m6Switch(host);
224
+ return v;
225
+ }
226
+ /** Registered M6 conformance ID range (M6-001..012). */
227
+ export const M6_IDS = [
228
+ "M6-001",
229
+ "M6-002",
230
+ "M6-003",
231
+ "M6-004",
232
+ "M6-005",
233
+ "M6-006",
234
+ "M6-007",
235
+ "M6-008",
236
+ "M6-009",
237
+ "M6-010",
238
+ "M6-011",
239
+ "M6-012",
240
+ ];
241
+ /** Registered named M6 conformance IDs. */
242
+ export const M6_NAMED_IDS = [
243
+ "M6-KEY-001",
244
+ "M6-STALE-002",
245
+ ];
246
+ function dedupe(codes) {
247
+ const out = [];
248
+ for (const c of codes)
249
+ if (!out.includes(c))
250
+ out.push(c);
251
+ return out;
252
+ }