pi-mega-compact 0.19.1 → 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.
- package/dist/config/vector-cortex.js +21 -0
- package/dist/config.js +1 -1
- package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +2 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-query.js +46 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-topology.js +145 -5
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +1 -0
- package/dist/extensions/dashboard-server/routes.js +1 -1
- package/dist/extensions/dashboard-server/server.js +3 -1
- package/dist/src/config/vector-cortex.js +21 -0
- package/dist/src/config.js +1 -1
- package/dist/src/tieredRouter.js +8 -0
- package/dist/src/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/src/vector-cortex/topology/build.js +232 -0
- package/dist/src/vector-cortex/topology/index.js +113 -0
- package/dist/src/vector-cortex/topology/query.js +342 -0
- package/dist/src/vector-cortex/topology/types.js +54 -0
- package/dist/src/vector-cortex/vc3b-support.js +125 -0
- package/dist/vector-cortex/migrations/router-generation-v2.js +252 -0
- package/dist/vector-cortex/topology/build.js +232 -0
- package/dist/vector-cortex/topology/index.js +113 -0
- package/dist/vector-cortex/topology/query.js +342 -0
- package/dist/vector-cortex/topology/types.js +54 -0
- package/dist/vector-cortex/vc3b-support.js +125 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-B9KYKrUE.js → AreaChart-DqiFSnDx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-B9KYKrUE.js.map → AreaChart-DqiFSnDx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-D1sWkR6j.js → BarChart-Cqg-PQMK.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-D1sWkR6j.js.map → BarChart-Cqg-PQMK.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-BR8MYcJq.js → CacheTab-BPNs0xIF.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-BR8MYcJq.js.map → CacheTab-BPNs0xIF.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-BRNpDMvP.js → EventsTab-BQf7OGkf.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-BRNpDMvP.js.map → EventsTab-BQf7OGkf.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-rr5nXpjq.js → HealthTab-B14TaSPN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-rr5nXpjq.js.map → HealthTab-B14TaSPN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtjPWnzN.js → MaintenanceTab-D8lkRoa1.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CtjPWnzN.js.map → MaintenanceTab-D8lkRoa1.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-5SnFDIYc.js → MemoryMapTab-BOWtGfpx.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-5SnFDIYc.js.map → MemoryMapTab-BOWtGfpx.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-4MZfwRhq.js → MetricsTab-7XI9cWaX.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-4MZfwRhq.js.map → MetricsTab-7XI9cWaX.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BAKENnbk.js → OverviewTab-C9KaGYvh.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-BAKENnbk.js.map → OverviewTab-C9KaGYvh.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-7-z_OmZo.js → ReposTab-BwESNhox.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-7-z_OmZo.js.map → ReposTab-BwESNhox.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-D_r2ki0y.js → SessionsTab-DKH55Rda.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-D_r2ki0y.js.map → SessionsTab-DKH55Rda.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-DPujtYgv.js → SetupTab-zDxf-nJd.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-DPujtYgv.js.map → SetupTab-zDxf-nJd.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CLHy30t6.js → TimeSavedCard-C9isog4L.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CLHy30t6.js.map → TimeSavedCard-C9isog4L.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Coul6CHc.js → TurnsTab-ciI6ydvj.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-Coul6CHc.js.map → TurnsTab-ciI6ydvj.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-qTZ7NWng.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-hryG5oyY.js → WikiTab-DcYHc734.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-hryG5oyY.js.map → WikiTab-DcYHc734.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-Dxo95lRI.js → button-DF8lx4bt.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-Dxo95lRI.js.map → button-DF8lx4bt.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-FXwhLF_u.js → card-DIoDheSZ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-FXwhLF_u.js.map → card-DIoDheSZ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-M-GeRM8U.js → generateCategoricalChart-BFRzeYX3.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-M-GeRM8U.js.map → generateCategoricalChart-BFRzeYX3.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-BIZZXvoy.js → index-DSZON0Yi.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-BIZZXvoy.js.map → index-DSZON0Yi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-C-d4tkN1.js → switch-l4qo22mU.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-C-d4tkN1.js.map → switch-l4qo22mU.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-Bfmucdcl.js → toggle-DPn6td1_.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-Bfmucdcl.js.map → toggle-DPn6td1_.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-CxK_wdmk.js → useSSE-BLmolqTJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-CxK_wdmk.js.map → useSSE-BLmolqTJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +1 -1
- package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +82 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +25 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +35 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
- package/extensions/dashboard-server/routes-vector-cortex-query.ts +54 -0
- package/extensions/dashboard-server/routes-vector-cortex-topology.ts +166 -5
- package/extensions/dashboard-server/routes-vector-cortex.ts +1 -0
- package/extensions/dashboard-server/routes.ts +1 -0
- package/extensions/dashboard-server/server.ts +2 -0
- package/package.json +1 -1
- package/src/config/vector-cortex.ts +23 -0
- package/src/config.ts +2 -0
- package/src/tieredRouter.ts +9 -0
- package/src/vector-cortex/migrations/router-generation-v2.ts +317 -0
- package/src/vector-cortex/topology/build.ts +259 -0
- package/src/vector-cortex/topology/index.ts +143 -0
- package/src/vector-cortex/topology/query.ts +485 -0
- package/src/vector-cortex/topology/types.ts +156 -0
- package/src/vector-cortex/vc3b-support.ts +142 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js.map +0 -1
|
@@ -101,6 +101,27 @@ export const VC2C_ENABLED = () => sprintFlag("MEGACOMPACT_VC2C");
|
|
|
101
101
|
* cortex rebuild/admin seam and the reader-only topology summary seam.
|
|
102
102
|
*/
|
|
103
103
|
export const VC3A_ENABLED = () => sprintFlag("MEGACOMPACT_VC3A");
|
|
104
|
+
/**
|
|
105
|
+
* VC3B — deterministic cortical topology (TopologyV1 / EdgeV1).
|
|
106
|
+
* Default ON. `MEGACOMPACT_VC3B=0` disables and is byte-identical to the
|
|
107
|
+
* predecessor (mode C: no topology graph is built or emitted, the dashboard
|
|
108
|
+
* topology view returns the VC3A-precise aggregation shape with no node/edge
|
|
109
|
+
* arrays, zero `vector_cortex_topology_*` emissions). The real consumers are
|
|
110
|
+
* the topology build seam (build.ts) and the dashboard topology node/edge view.
|
|
111
|
+
*/
|
|
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");
|
|
104
125
|
// ---------------------------------------------------------------------------
|
|
105
126
|
// Breaker state machine constants (TRIAD_RESILIENCE.md §breaker).
|
|
106
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, 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";
|
|
@@ -144,6 +144,8 @@ export const SETTINGS = [
|
|
|
144
144
|
boolDirect("MEGACOMPACT_VC2B", "VC2B Multi-Head Encoder", "VectorSetV1 five L2-normalized heads (384/128/128/64/32) with head-calibration draft + asset-free trigram B (512d) and lexical C fallbacks, plus the per-head emit seam. OFF = mode C, no per-head vectors emitted, byte-identical predecessor.", true),
|
|
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
|
+
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),
|
|
147
149
|
],
|
|
148
150
|
},
|
|
149
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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dashboard-server/routes-vector-cortex-topology.ts — VC3A vector-cortex
|
|
3
|
-
* dashboard route.
|
|
2
|
+
* dashboard-server/routes-vector-cortex-topology.ts — VC3A/VC3B vector-cortex
|
|
3
|
+
* topology dashboard route.
|
|
4
4
|
*
|
|
5
5
|
* Reader-only GET /api/vector-cortex/topology built ENTIRELY on the CortexReader
|
|
6
6
|
* capability: opens the isolated cortex DB for this repo's state dir and returns
|
|
@@ -10,14 +10,26 @@
|
|
|
10
10
|
* (no writer/admin leakage). Non-fatal: a missing or corrupt cortex DB degrades
|
|
11
11
|
* to `enabled:false`.
|
|
12
12
|
*
|
|
13
|
+
* VC3B adds the deterministic node/edge shapes (TopologyV1) when both VC3A and
|
|
14
|
+
* VC3B are enabled: the accepted derived records of kind "topology" that carry a
|
|
15
|
+
* canonical TopologyCandidate JSON payload are fed through the deterministic
|
|
16
|
+
* build (buildTopologyGraph), whose stable generation digest + sorted node/edge
|
|
17
|
+
* arrays are returned. Bad/unparseable records are skipped (PREVENT-001), and
|
|
18
|
+
* with VC3B off the node/edge fields are omitted entirely — byte-identical to
|
|
19
|
+
* the VC3A predecessor view.
|
|
20
|
+
*
|
|
13
21
|
* Guardrails: PREVENT-PI-004 (local filesystem read only), PREVENT-011 (no
|
|
14
22
|
* `any`), reader-only aggregate.
|
|
15
23
|
*/
|
|
16
|
-
import { VC3A_ENABLED } from "../../src/config.js";
|
|
24
|
+
import { VC3A_ENABLED, VC3B_ENABLED } from "../../src/config.js";
|
|
25
|
+
import { Logger } from "../../src/log.js";
|
|
17
26
|
import { createCortexStore } from "../../src/vector-cortex/cortex/store.js";
|
|
27
|
+
import { buildTopologyGraph } from "../../src/vector-cortex/topology/index.js";
|
|
18
28
|
import { sendJson } from "./routes-vector-cortex-shared.js";
|
|
29
|
+
/** Well-known cortex record kind that carries a canonical candidate payload. */
|
|
30
|
+
const TOPOLOGY_RECORD_KIND = "topology";
|
|
19
31
|
/**
|
|
20
|
-
* Reader-only GET /api/vector-cortex/topology (VC3A).
|
|
32
|
+
* Reader-only GET /api/vector-cortex/topology (VC3A + VC3B node/edge shapes).
|
|
21
33
|
*/
|
|
22
34
|
export function handleVectorCortexTopology(req, res, ctx) {
|
|
23
35
|
const url = req.url ?? "";
|
|
@@ -30,13 +42,18 @@ export function handleVectorCortexTopology(req, res, ctx) {
|
|
|
30
42
|
return true;
|
|
31
43
|
}
|
|
32
44
|
const enabled = VC3A_ENABLED();
|
|
45
|
+
const topologyEnabled = VC3B_ENABLED();
|
|
33
46
|
let active = enabled;
|
|
34
47
|
let summary = { generationId: null, rootDigest: null, sourceHighWater: "0", recordCount: 0, ordinal: null };
|
|
48
|
+
let nodes;
|
|
49
|
+
let edges;
|
|
50
|
+
let generationDigest;
|
|
35
51
|
if (enabled) {
|
|
36
52
|
try {
|
|
37
53
|
const store = createCortexStore({ stateDir: ctx.stateDir });
|
|
38
54
|
try {
|
|
39
|
-
const
|
|
55
|
+
const reader = store.reader();
|
|
56
|
+
const s = reader.topologySummary();
|
|
40
57
|
summary = {
|
|
41
58
|
generationId: s.generationId,
|
|
42
59
|
rootDigest: s.rootDigest,
|
|
@@ -44,6 +61,12 @@ export function handleVectorCortexTopology(req, res, ctx) {
|
|
|
44
61
|
recordCount: s.recordCount,
|
|
45
62
|
ordinal: s.ordinal,
|
|
46
63
|
};
|
|
64
|
+
if (topologyEnabled) {
|
|
65
|
+
const built = buildFromRecords(reader.readRecords());
|
|
66
|
+
nodes = built.nodes;
|
|
67
|
+
edges = built.edges;
|
|
68
|
+
generationDigest = built.generationDigest;
|
|
69
|
+
}
|
|
47
70
|
}
|
|
48
71
|
finally {
|
|
49
72
|
store.close();
|
|
@@ -61,8 +84,125 @@ export function handleVectorCortexTopology(req, res, ctx) {
|
|
|
61
84
|
sourceHighWater: summary.sourceHighWater,
|
|
62
85
|
recordCount: summary.recordCount,
|
|
63
86
|
ordinal: summary.ordinal,
|
|
87
|
+
...(topologyEnabled
|
|
88
|
+
? { nodes, edges, generationDigest }
|
|
89
|
+
: {}),
|
|
64
90
|
updatedAt: new Date().toISOString(),
|
|
65
91
|
};
|
|
66
92
|
sendJson(res, 200, body);
|
|
67
93
|
return true;
|
|
68
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Reconstruct the deterministic topology graph from the accepted derived records
|
|
97
|
+
* (reader-only, best-effort). Only records of kind "topology" whose payload is a
|
|
98
|
+
* canonical TopologyCandidate JSON are consumed; everything else is ignored.
|
|
99
|
+
* Returns empty arrays + null digest when no valid candidates are stored.
|
|
100
|
+
*
|
|
101
|
+
* The build seam is invoked with a logger-derived emit so the VC3B observability
|
|
102
|
+
* events (`vector_cortex_topology_built` / `vector_cortex_topology_edge_rejected`)
|
|
103
|
+
* fire on this production path — not just under unit tests. Best-effort and
|
|
104
|
+
* non-fatal: a failing emitter never breaks the agent loop.
|
|
105
|
+
*
|
|
106
|
+
* Q04 — DIVERGENCE RISK (documented, currently latent): the build is invoked
|
|
107
|
+
* with a hardcoded `threshold: 0` (plus `sessionId: "dashboard"` and
|
|
108
|
+
* `sourceHighWater: 0n`). This reflects the VC3B state where no production
|
|
109
|
+
* writer emits calibrated-threshold candidates yet, so retaining ALL stored
|
|
110
|
+
* "topology" records is correct for now. Once a real calibrated-threshold
|
|
111
|
+
* producer lands (VC3C), this dashboard digest will NOT derive from the
|
|
112
|
+
* authoritative threshold and can diverge from the canonical build for the same
|
|
113
|
+
* stored candidate set. The calibrated threshold must then be threaded through
|
|
114
|
+
* (e.g. sourced from the authority/config, not zero) before this route is treated
|
|
115
|
+
* as authoritative. Until then the route is explicitly best-effort display only.
|
|
116
|
+
*/
|
|
117
|
+
function buildFromRecords(records) {
|
|
118
|
+
const candidates = [];
|
|
119
|
+
for (const r of records) {
|
|
120
|
+
if (r.kind !== TOPOLOGY_RECORD_KIND)
|
|
121
|
+
continue;
|
|
122
|
+
const parsed = parseCandidatePayload(r);
|
|
123
|
+
if (parsed)
|
|
124
|
+
candidates.push(parsed);
|
|
125
|
+
}
|
|
126
|
+
const result = buildTopologyGraph({ sessionId: "dashboard", sourceHighWater: 0n, threshold: 0, candidates }, topologyEmit);
|
|
127
|
+
if (!result.ok)
|
|
128
|
+
return { nodes: [], edges: [], generationDigest: null };
|
|
129
|
+
return {
|
|
130
|
+
nodes: result.topology.nodes.map((n) => ({ id: n.id, kind: n.kind })),
|
|
131
|
+
edges: result.topology.edges.map((e) => ({
|
|
132
|
+
source: e.source,
|
|
133
|
+
target: e.target,
|
|
134
|
+
head: e.head,
|
|
135
|
+
score: e.score,
|
|
136
|
+
direction: e.direction,
|
|
137
|
+
})),
|
|
138
|
+
generationDigest: result.topology.generationDigest,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Logger-derived default emitter for the VC3B topology events. Mirrors the
|
|
143
|
+
* cortex store's `defaultEmitFor()` convention: a REAL structured-log producer so
|
|
144
|
+
* a caller that invokes the build seam without injecting an emitter still yields
|
|
145
|
+
* telemetry (best-effort, non-fatal). This is the production invoke point that
|
|
146
|
+
* makes the task-5 emission capable live (not dead under unit tests only).
|
|
147
|
+
*/
|
|
148
|
+
const topologyEmit = (event, fields) => {
|
|
149
|
+
new Logger().info(event, fields);
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* PREVENT-001: parse + shape-check a candidate payload before use. Returns
|
|
153
|
+
* undefined on any malformed/non-object input (best-effort, non-fatal).
|
|
154
|
+
*
|
|
155
|
+
* Q04 — FRAMING HARDENING: the canonical digest serialization in index.ts uses
|
|
156
|
+
* `|` (FIELD_SEP) and `~` (RECORD_SEP) as unambiguous delimiters between node
|
|
157
|
+
* ids/kinds and edge fields. That framing is collision-free only while id values
|
|
158
|
+
* (source/target/head) contain neither byte. Today ids are project-controlled, so
|
|
159
|
+
* this is latent, but a stored topology record could in principle carry a
|
|
160
|
+
* content-derived id containing those bytes, making the digest ambiguous
|
|
161
|
+
* (distinct graphs could collide) or the serialization injection-prone. To keep
|
|
162
|
+
* the digest well-defined for ANY input, reject candidates whose source/target/
|
|
163
|
+
* head contain the field/record separators; such a record is skipped (non-fatal)
|
|
164
|
+
* rather than fed into the digest.
|
|
165
|
+
*/
|
|
166
|
+
function parseCandidatePayload(r) {
|
|
167
|
+
let text;
|
|
168
|
+
try {
|
|
169
|
+
text = new TextDecoder().decode(r.payloadBytes);
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
const v = JSON.parse(text);
|
|
176
|
+
if (v === null ||
|
|
177
|
+
typeof v !== "object" ||
|
|
178
|
+
Array.isArray(v)) {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
const o = v;
|
|
182
|
+
if (typeof o.source !== "string" ||
|
|
183
|
+
typeof o.target !== "string" ||
|
|
184
|
+
typeof o.head !== "string" ||
|
|
185
|
+
typeof o.score !== "number" ||
|
|
186
|
+
(o.kind !== "dependency" && o.kind !== "contradiction")) {
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
// Q04: reject any id that would corrupt the `|`/`~` digest framing.
|
|
190
|
+
if (FRAMING_SEP.test(o.source) ||
|
|
191
|
+
FRAMING_SEP.test(o.target) ||
|
|
192
|
+
FRAMING_SEP.test(o.head)) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
source: o.source,
|
|
197
|
+
target: o.target,
|
|
198
|
+
head: o.head,
|
|
199
|
+
score: o.score,
|
|
200
|
+
kind: o.kind,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** Field (`|`) and record (`~`) separators used by the canonical digest framing. */
|
|
208
|
+
const FRAMING_SEP = /[|~]/;
|
|
@@ -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,
|
|
@@ -101,6 +101,27 @@ export const VC2C_ENABLED = () => sprintFlag("MEGACOMPACT_VC2C");
|
|
|
101
101
|
* cortex rebuild/admin seam and the reader-only topology summary seam.
|
|
102
102
|
*/
|
|
103
103
|
export const VC3A_ENABLED = () => sprintFlag("MEGACOMPACT_VC3A");
|
|
104
|
+
/**
|
|
105
|
+
* VC3B — deterministic cortical topology (TopologyV1 / EdgeV1).
|
|
106
|
+
* Default ON. `MEGACOMPACT_VC3B=0` disables and is byte-identical to the
|
|
107
|
+
* predecessor (mode C: no topology graph is built or emitted, the dashboard
|
|
108
|
+
* topology view returns the VC3A-precise aggregation shape with no node/edge
|
|
109
|
+
* arrays, zero `vector_cortex_topology_*` emissions). The real consumers are
|
|
110
|
+
* the topology build seam (build.ts) and the dashboard topology node/edge view.
|
|
111
|
+
*/
|
|
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");
|
|
104
125
|
// ---------------------------------------------------------------------------
|
|
105
126
|
// Breaker state machine constants (TRIAD_RESILIENCE.md §breaker).
|
|
106
127
|
// Rolled numbers for one 60s window; VC0C consumes these at its breaker seam.
|
package/dist/src/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, 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";
|
package/dist/src/tieredRouter.js
CHANGED
|
@@ -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
|
+
}
|