pi-mega-compact 0.19.0 → 0.19.2
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 +19 -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-eval.js +49 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-health.js +233 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-ledger.js +82 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-shared.js +46 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex-topology.js +208 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +12 -373
- 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 +19 -0
- package/dist/src/config.js +1 -1
- package/dist/src/vector-cortex/cortex/sqlite.js +393 -0
- package/dist/src/vector-cortex/cortex/store.js +139 -0
- package/dist/src/vector-cortex/cortex/types.js +44 -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/types.js +54 -0
- package/dist/src/vector-cortex/vc3b-support.js +125 -0
- package/dist/vector-cortex/cortex/sqlite.js +393 -0
- package/dist/vector-cortex/cortex/store.js +139 -0
- package/dist/vector-cortex/cortex/types.js +44 -0
- package/dist/vector-cortex/topology/build.js +232 -0
- package/dist/vector-cortex/topology/index.js +113 -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-DywU5SSt.js → AreaChart-CSaJZXru.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-DywU5SSt.js.map → AreaChart-CSaJZXru.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-But7WN6K.js → BarChart-CQouNj1g.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-But7WN6K.js.map → BarChart-CQouNj1g.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-7aZWa1DR.js → CacheTab-m_eiCYoL.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-7aZWa1DR.js.map → CacheTab-m_eiCYoL.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-Bf_boNG2.js → EventsTab-DRyOwjYN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-Bf_boNG2.js.map → EventsTab-DRyOwjYN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-AbGc1nXP.js → HealthTab-Dj9k40TJ.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-AbGc1nXP.js.map → HealthTab-Dj9k40TJ.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-BfHHhn5F.js → MaintenanceTab-Bk9mhYfD.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-BfHHhn5F.js.map → MaintenanceTab-Bk9mhYfD.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-DrcWti0-.js → MemoryMapTab-C7ve49VG.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-DrcWti0-.js.map → MemoryMapTab-C7ve49VG.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-ALiglXQq.js → MetricsTab-DzjhqZA9.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-ALiglXQq.js.map → MetricsTab-DzjhqZA9.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-nnaJdNyR.js → OverviewTab-CFQdfIm_.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-nnaJdNyR.js.map → OverviewTab-CFQdfIm_.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-BTK0nofM.js → ReposTab-DyjwEFUr.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-BTK0nofM.js.map → ReposTab-DyjwEFUr.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-RXPNqO6x.js → SessionsTab-njLIy7ab.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-RXPNqO6x.js.map → SessionsTab-njLIy7ab.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-Dab0AVfs.js → SetupTab-UHIs8-Lh.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-Dab0AVfs.js.map → SetupTab-UHIs8-Lh.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-DIVG8hdt.js → TimeSavedCard-inWHjCxg.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-DIVG8hdt.js.map → TimeSavedCard-inWHjCxg.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-9uz2fBwx.js → TurnsTab-CXJYugBu.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-9uz2fBwx.js.map → TurnsTab-CXJYugBu.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DkS0Azx9.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DkS0Azx9.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-CpcgaggS.js → WikiTab-BJ4A2oi9.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-CpcgaggS.js.map → WikiTab-BJ4A2oi9.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-B0Q2fuKY.js → button-BWl-moyn.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-B0Q2fuKY.js.map → button-BWl-moyn.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-Cnik3TFr.js → card-C58RVPOk.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-Cnik3TFr.js.map → card-C58RVPOk.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-DrSwgn1V.js → generateCategoricalChart-vgeOQU3L.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-DrSwgn1V.js.map → generateCategoricalChart-vgeOQU3L.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/index-CYpzq9Lm.css +1 -0
- package/extensions/dashboard-client/dist/assets/{index-DraytcOY.js → index-lVrWQjpu.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-DraytcOY.js.map → index-lVrWQjpu.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{switch-DVQ1aB9b.js → switch-CSeplFQT.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-DVQ1aB9b.js.map → switch-CSeplFQT.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-Dkppx-49.js → toggle-CQRxgVRi.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-Dkppx-49.js.map → toggle-CQRxgVRi.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-CPkjU4Gx.js → useSSE-BUz6IOI-.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-CPkjU4Gx.js.map → useSSE-BUz6IOI-.js.map} +1 -1
- package/extensions/dashboard-client/dist/index.html +2 -2
- package/extensions/dashboard-client/src/api/vector-cortex.ts +9 -0
- package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +89 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +28 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +41 -0
- package/extensions/dashboard-server/routes-rag-settings-helpers.ts +12 -0
- package/extensions/dashboard-server/routes-vector-cortex-eval.ts +59 -0
- package/extensions/dashboard-server/routes-vector-cortex-health.ts +273 -0
- package/extensions/dashboard-server/routes-vector-cortex-ledger.ts +92 -0
- package/extensions/dashboard-server/routes-vector-cortex-shared.ts +53 -0
- package/extensions/dashboard-server/routes-vector-cortex-topology.ts +244 -0
- package/extensions/dashboard-server/routes-vector-cortex.ts +12 -431
- 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 +21 -0
- package/src/config.ts +2 -0
- package/src/vector-cortex/cortex/sqlite.ts +489 -0
- package/src/vector-cortex/cortex/store.ts +202 -0
- package/src/vector-cortex/cortex/types.ts +194 -0
- package/src/vector-cortex/topology/build.ts +259 -0
- package/src/vector-cortex/topology/index.ts +143 -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-DnKY61EJ.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DnKY61EJ.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-CfvziyBi.css +0 -1
|
@@ -91,6 +91,25 @@ export const VC2B_ENABLED = () => sprintFlag("MEGACOMPACT_VC2B");
|
|
|
91
91
|
* the encoder-qualification emit seam and the calibrate/select seams.
|
|
92
92
|
*/
|
|
93
93
|
export const VC2C_ENABLED = () => sprintFlag("MEGACOMPACT_VC2C");
|
|
94
|
+
/**
|
|
95
|
+
* VC3A — capability-gated cortex store (CortexReader/Writer/Admin, CortexRecordV1).
|
|
96
|
+
* Default ON. `MEGACOMPACT_VC3A=0` disables and is byte-identical to the
|
|
97
|
+
* predecessor (mode C: the derived cortex store is absent, mode B in-memory is
|
|
98
|
+
* not consulted, zero `vector_cortex_record_append_failed` /
|
|
99
|
+
* `vector_cortex_generation_rebuilt` emissions; the predecessor derived pointer
|
|
100
|
+
* and goldens are unchanged). The real consumers are the cortex append seam, the
|
|
101
|
+
* cortex rebuild/admin seam and the reader-only topology summary seam.
|
|
102
|
+
*/
|
|
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");
|
|
94
113
|
// ---------------------------------------------------------------------------
|
|
95
114
|
// Breaker state machine constants (TRIAD_RESILIENCE.md §breaker).
|
|
96
115
|
// 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, 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, 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";
|
|
@@ -143,6 +143,8 @@ export const SETTINGS = [
|
|
|
143
143
|
boolDirect("MEGACOMPACT_VC2A", "VC2A Offline Model Runtime", "ModelManifestV1 digest-before-load ONNX runtime (opset17/batch1/max512) + asset-free trigram demotion. Asset path assets/vector-cortex/encoder-v1 is immutable/digest-pinned. OFF = mode C, byte-identical to predecessor.", true),
|
|
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
|
+
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),
|
|
146
148
|
],
|
|
147
149
|
},
|
|
148
150
|
{
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard-server/routes-vector-cortex-eval.ts — VC0A vector-cortex evaluation
|
|
3
|
+
* dashboard route.
|
|
4
|
+
*
|
|
5
|
+
* A single reader-only GET /api/vector-cortex/evaluation that aggregates redacted
|
|
6
|
+
* evaluation rows (persisted JSONL under the state dir) via the reader. No
|
|
7
|
+
* writable capability — no reset/mutation here.
|
|
8
|
+
*
|
|
9
|
+
* Guardrails: PREVENT-PI-004 (local filesystem read only), PREVENT-011 (no
|
|
10
|
+
* `any`), reader-only aggregate (never payloads/prompts/ledger — EVAL-REDACT-002).
|
|
11
|
+
*/
|
|
12
|
+
import { VC0A_ENABLED } from "../../src/config.js";
|
|
13
|
+
import { readEvalRows } from "../../src/vector-cortex/eval/persist.js";
|
|
14
|
+
import { summarizeEvalRows } from "../../src/vector-cortex/eval/reader.js";
|
|
15
|
+
import { sendJson } from "./routes-vector-cortex-shared.js";
|
|
16
|
+
/** Reader-only aggregate GET /api/vector-cortex/evaluation. */
|
|
17
|
+
export function handleVectorCortexEvaluation(req, res, ctx) {
|
|
18
|
+
const url = req.url ?? "";
|
|
19
|
+
if (url !== "/api/vector-cortex/evaluation")
|
|
20
|
+
return false;
|
|
21
|
+
if (req.method !== "GET") {
|
|
22
|
+
// Reader-only: no mutation endpoints exist on this path.
|
|
23
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
const enabled = VC0A_ENABLED();
|
|
27
|
+
const rows = enabled ? readEvalRows(ctx.stateDir) : [];
|
|
28
|
+
const summary = summarizeEvalRows(rows);
|
|
29
|
+
const body = {
|
|
30
|
+
enabled,
|
|
31
|
+
mode: enabled ? "A" : "C",
|
|
32
|
+
samples: summary.samples,
|
|
33
|
+
byMode: {
|
|
34
|
+
A: summary.byMode.A,
|
|
35
|
+
B: summary.byMode.B,
|
|
36
|
+
C: summary.byMode.C,
|
|
37
|
+
},
|
|
38
|
+
histogram: {
|
|
39
|
+
edges: [...summary.histogram.edges],
|
|
40
|
+
cells: [...summary.histogram.cells],
|
|
41
|
+
overflow: summary.histogram.overflow,
|
|
42
|
+
total: summary.histogram.total,
|
|
43
|
+
},
|
|
44
|
+
rejects: [], // VC0C wires live breaker/reject telemetry here
|
|
45
|
+
updatedAt: new Date().toISOString(),
|
|
46
|
+
};
|
|
47
|
+
sendJson(res, 200, body);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard-server/routes-vector-cortex-health.ts — VC0C vector-cortex breaker
|
|
3
|
+
* health + reset routes (plus the VC2C encoder-health facts they surface).
|
|
4
|
+
*
|
|
5
|
+
* GET /api/vector-cortex/health — reader-only aggregate health card
|
|
6
|
+
* POST /api/vector-cortex/breakers/reset — explicit admin capability
|
|
7
|
+
*
|
|
8
|
+
* Guardrails: PREVENT-PI-004 (local filesystem read only), PREVENT-011 (no
|
|
9
|
+
* `any`), reader-only aggregate (never payloads/prompts/ledger — EVAL-REDACT-002).
|
|
10
|
+
*/
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { readFileSync, statSync } from "node:fs";
|
|
13
|
+
import { join, dirname } from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
import { VC0C_ENABLED, VC2C_ENABLED } from "../../src/config.js";
|
|
16
|
+
import { createVectorCortexSafety } from "../mega-runtime/vector-cortex-safety.js";
|
|
17
|
+
import { readEncoderManifest, verifyEncoderAsset, detectPlatform } from "../../src/vector-cortex/encoder/asset.js";
|
|
18
|
+
import { sendJson, readJsonBody } from "./routes-vector-cortex-shared.js";
|
|
19
|
+
/** Resolve the committed encoder-v1 asset dir by walking up to the repo root. */
|
|
20
|
+
function encoderAssetDir() {
|
|
21
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
const rel = join("assets", "vector-cortex", "encoder-v1");
|
|
23
|
+
for (let i = 0; i < 8; i++) {
|
|
24
|
+
const candidate = join(dir, rel);
|
|
25
|
+
try {
|
|
26
|
+
// guardrails-allow PREVENT-PI-004: local asset filesystem read (loopback)
|
|
27
|
+
readFileSync(join(candidate, "manifest.json"));
|
|
28
|
+
return candidate;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
/* keep walking */
|
|
32
|
+
}
|
|
33
|
+
const next = dirname(dir);
|
|
34
|
+
if (next === dir)
|
|
35
|
+
break;
|
|
36
|
+
dir = next;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
let healthCache = null;
|
|
41
|
+
/** Stable cache key: manifest bytes + (size,mtime) of the asset files + platform. */
|
|
42
|
+
function healthFactCacheKey(dir, manifestPath) {
|
|
43
|
+
const platform = detectPlatform();
|
|
44
|
+
if (!platform)
|
|
45
|
+
return null;
|
|
46
|
+
const parts = [platform];
|
|
47
|
+
try {
|
|
48
|
+
for (const p of [manifestPath, join(dir, "model.onnx"), join(dir, "tokenizer.json")]) {
|
|
49
|
+
const s = statSync(p);
|
|
50
|
+
parts.push(`${s.size}:${s.mtimeMs}`);
|
|
51
|
+
}
|
|
52
|
+
// guardrails-allow PREVENT-PI-004: local manifest filesystem read (loopback)
|
|
53
|
+
parts.push(createHash("sha256").update(readFileSync(manifestPath)).digest("hex"));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return parts.join("|");
|
|
59
|
+
}
|
|
60
|
+
function computeEncoderHealthFacts(dir) {
|
|
61
|
+
const manifest = readEncoderManifest(dir);
|
|
62
|
+
if (manifest === null)
|
|
63
|
+
return { assetDigest: null, mode: "C" };
|
|
64
|
+
const manifestPath = join(dir, "manifest.json");
|
|
65
|
+
const key = healthFactCacheKey(dir, manifestPath);
|
|
66
|
+
if (key === null)
|
|
67
|
+
return null;
|
|
68
|
+
if (healthCache !== null && healthCache.key === key)
|
|
69
|
+
return healthCache.facts;
|
|
70
|
+
let digest;
|
|
71
|
+
try {
|
|
72
|
+
// guardrails-allow PREVENT-PI-004: local manifest filesystem read (loopback)
|
|
73
|
+
digest = createHash("sha256").update(readFileSync(manifestPath)).digest("hex");
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return { assetDigest: null, mode: "B" };
|
|
77
|
+
}
|
|
78
|
+
const verify = verifyEncoderAsset(dir, manifest, detectPlatform());
|
|
79
|
+
const facts = { assetDigest: digest, mode: verify.ok ? "A" : "B" };
|
|
80
|
+
healthCache = { key, facts };
|
|
81
|
+
return facts;
|
|
82
|
+
}
|
|
83
|
+
function encoderHealthFacts() {
|
|
84
|
+
if (!VC2C_ENABLED()) {
|
|
85
|
+
// Rollback: MEGACOMPACT_VC2C=0 selects C — report the inactive (flag-gated)
|
|
86
|
+
// mode with no asset digest rather than an on-disk-computed A/B.
|
|
87
|
+
return { assetDigest: null, mode: "C" };
|
|
88
|
+
}
|
|
89
|
+
const dir = encoderAssetDir();
|
|
90
|
+
if (dir === null)
|
|
91
|
+
return { assetDigest: null, mode: "C" };
|
|
92
|
+
const facts = computeEncoderHealthFacts(dir);
|
|
93
|
+
if (facts === null)
|
|
94
|
+
return { assetDigest: null, mode: "C" };
|
|
95
|
+
return facts;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Reader-only GET /api/vector-cortex/health (VC0C).
|
|
99
|
+
*
|
|
100
|
+
* Aggregate health card: breaker state (window/probe/backoff), durable spool
|
|
101
|
+
* frontier/authority/lag, and a worst-state aggregate. Constructs the safety
|
|
102
|
+
* adapter over the state dir — durable spool state (frontier-frozen, lag) is
|
|
103
|
+
* read from disk, so it reflects restart. Purely read: no mutation, never
|
|
104
|
+
* payloads/prompts/ledger.
|
|
105
|
+
*/
|
|
106
|
+
export function handleVectorCortexHealth(req, res, ctx) {
|
|
107
|
+
const url = req.url ?? "";
|
|
108
|
+
if (url !== "/api/vector-cortex/health")
|
|
109
|
+
return false;
|
|
110
|
+
if (req.method !== "GET") {
|
|
111
|
+
// Reader-only path: no mutation endpoint lives at /health.
|
|
112
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
const enabled = VC0C_ENABLED();
|
|
116
|
+
const safety = enabled ? createVectorCortexSafety({ stateDir: ctx.stateDir }) : null;
|
|
117
|
+
const card = safety ? safety.health() : null;
|
|
118
|
+
const enc = encoderHealthFacts();
|
|
119
|
+
const fallback = {
|
|
120
|
+
enabled: false,
|
|
121
|
+
mode: "C",
|
|
122
|
+
state: "CLOSED_A",
|
|
123
|
+
subsystem: "provider",
|
|
124
|
+
sinceMs: 0,
|
|
125
|
+
windowMs: 0,
|
|
126
|
+
probeCount: 0,
|
|
127
|
+
backoffDelayMs: 0,
|
|
128
|
+
frontierFrozen: false,
|
|
129
|
+
authorityOutage: false,
|
|
130
|
+
spoolLag: 0,
|
|
131
|
+
attempts: 0,
|
|
132
|
+
failures: 0,
|
|
133
|
+
p95Ms: 0,
|
|
134
|
+
failureRate: 0,
|
|
135
|
+
updatedAt: new Date().toISOString(),
|
|
136
|
+
aggregate: "CLOSED_A",
|
|
137
|
+
stateSource: "ephemeral",
|
|
138
|
+
encoderAssetDigest: enc.assetDigest,
|
|
139
|
+
encoderMode: enc.mode,
|
|
140
|
+
};
|
|
141
|
+
if (!card) {
|
|
142
|
+
sendJson(res, 200, fallback);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
const mode = card.state === "CLOSED_A" ? "A" : card.state === "MANUAL_HALT" ? "C" : "B";
|
|
146
|
+
const aggregate = card.state === "MANUAL_HALT"
|
|
147
|
+
? "MANUAL_HALT"
|
|
148
|
+
: card.state === "OPEN_C"
|
|
149
|
+
? "OPEN_C"
|
|
150
|
+
: card.state === "OPEN_B"
|
|
151
|
+
? "OPEN_B"
|
|
152
|
+
: card.state === "PROBE_B" || card.state === "PROBE_A"
|
|
153
|
+
? "PROBE"
|
|
154
|
+
: "CLOSED_A";
|
|
155
|
+
const body = {
|
|
156
|
+
enabled,
|
|
157
|
+
mode,
|
|
158
|
+
state: card.state,
|
|
159
|
+
subsystem: card.subsystem,
|
|
160
|
+
sinceMs: card.sinceMs,
|
|
161
|
+
reason: card.reason,
|
|
162
|
+
windowMs: card.windowMs,
|
|
163
|
+
probeCount: card.probeCount,
|
|
164
|
+
backoffDelayMs: card.backoffDelayMs,
|
|
165
|
+
frontierFrozen: card.frontierFrozen,
|
|
166
|
+
authorityOutage: card.authorityOutage,
|
|
167
|
+
spoolLag: card.spoolLag,
|
|
168
|
+
attempts: card.attempts,
|
|
169
|
+
failures: card.failures,
|
|
170
|
+
p95Ms: card.p95Ms,
|
|
171
|
+
failureRate: card.failureRate,
|
|
172
|
+
updatedAt: new Date().toISOString(),
|
|
173
|
+
aggregate,
|
|
174
|
+
stateSource: card.stateSource,
|
|
175
|
+
encoderAssetDigest: enc.assetDigest,
|
|
176
|
+
encoderMode: enc.mode,
|
|
177
|
+
};
|
|
178
|
+
sendJson(res, 200, body);
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Admin POST /api/vector-cortex/breakers/reset (VC0C).
|
|
183
|
+
*
|
|
184
|
+
* Explicit admin capability (separate from the reader GET). Clears a breaker's
|
|
185
|
+
* cooldown — or unwires a MANUAL_HALT — but NEVER evidence (attempts/failures
|
|
186
|
+
* are retained, per TRIAD_RESILIENCE). The subsystem is actor-supplied via the
|
|
187
|
+
* JSON body `{ subsystem }`. Every reset is documented in the returned record
|
|
188
|
+
* (state, retained evidence, updatedAt) — an auditable mutation surface; the
|
|
189
|
+
* GET path can never mutate.
|
|
190
|
+
*/
|
|
191
|
+
export function handleVectorCortexBreakersReset(req, res, ctx) {
|
|
192
|
+
const url = req.url ?? "";
|
|
193
|
+
if (url !== "/api/vector-cortex/breakers/reset")
|
|
194
|
+
return false;
|
|
195
|
+
if (req.method !== "POST") {
|
|
196
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
if (!VC0C_ENABLED()) {
|
|
200
|
+
sendJson(res, 409, { error: "vco_c_disabled" });
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
readJsonBody(req, (parsed) => {
|
|
204
|
+
if (!parsed.ok) {
|
|
205
|
+
sendJson(res, 400, { error: parsed.error });
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const subsystem = parsed.value.subsystem;
|
|
209
|
+
if (typeof subsystem !== "string" || subsystem.length === 0) {
|
|
210
|
+
sendJson(res, 400, { error: "subsystem_required" });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const safety = createVectorCortexSafety({ stateDir: ctx.stateDir });
|
|
215
|
+
const rec = safety.reset(subsystem);
|
|
216
|
+
const body = {
|
|
217
|
+
subsystem: rec.subsystem,
|
|
218
|
+
state: rec.state,
|
|
219
|
+
cooldownCleared: true,
|
|
220
|
+
attempts: rec.attempts,
|
|
221
|
+
failures: rec.failures,
|
|
222
|
+
probeCount: rec.probeCount,
|
|
223
|
+
manualReason: rec.manualReason,
|
|
224
|
+
updatedAt: rec.updatedAt,
|
|
225
|
+
};
|
|
226
|
+
sendJson(res, 200, body);
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
sendJson(res, 500, { error: "reset_failed" });
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard-server/routes-vector-cortex-ledger.ts — VC1B vector-cortex ledger
|
|
3
|
+
* dashboard route.
|
|
4
|
+
*
|
|
5
|
+
* Reader-only GET /api/vector-cortex/ledger built on the LedgerReader capability:
|
|
6
|
+
* opens the occurrence-v2 ledger for this repo's state dir and returns the
|
|
7
|
+
* session's occurrence IDENTITY rows (seq/eventId/kind/digest/toolCallId) plus
|
|
8
|
+
* high-water/count. NEVER ships sourceBytes or prompt text (reader-only
|
|
9
|
+
* no-ledger-text rule). Optional `?session=<id>` query selects the session.
|
|
10
|
+
*
|
|
11
|
+
* Guardrails: PREVENT-PI-004 (local filesystem read only), PREVENT-011 (no
|
|
12
|
+
* `any`), reader-only aggregate/identity only.
|
|
13
|
+
*/
|
|
14
|
+
import { VC1B_ENABLED } from "../../src/config.js";
|
|
15
|
+
import { createLedgerStore } from "../../src/vector-cortex/ledger/store.js";
|
|
16
|
+
import { sendJson } from "./routes-vector-cortex-shared.js";
|
|
17
|
+
/**
|
|
18
|
+
* Reader-only GET /api/vector-cortex/ledger (VC1B).
|
|
19
|
+
*/
|
|
20
|
+
export function handleVectorCortexLedger(req, res, ctx) {
|
|
21
|
+
const url = req.url ?? "";
|
|
22
|
+
const path = url.split("?")[0] ?? url;
|
|
23
|
+
if (path !== "/api/vector-cortex/ledger")
|
|
24
|
+
return false;
|
|
25
|
+
if (req.method !== "GET") {
|
|
26
|
+
// Reader-only path: no mutation endpoint lives at /ledger.
|
|
27
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
// Parse the optional session query parameter.
|
|
31
|
+
let session = "default";
|
|
32
|
+
const qs = url.split("?")[1];
|
|
33
|
+
if (qs !== undefined) {
|
|
34
|
+
for (const pair of qs.split("&")) {
|
|
35
|
+
const [k, v] = pair.split("=");
|
|
36
|
+
if (k === "session" && v !== undefined && v.length > 0) {
|
|
37
|
+
session = decodeURIComponent(v);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const enabled = VC1B_ENABLED();
|
|
42
|
+
let active = enabled;
|
|
43
|
+
let highWater = "0";
|
|
44
|
+
let count = 0;
|
|
45
|
+
let occurrences = [];
|
|
46
|
+
if (enabled) {
|
|
47
|
+
try {
|
|
48
|
+
const store = createLedgerStore({ stateDir: ctx.stateDir });
|
|
49
|
+
try {
|
|
50
|
+
const reader = store.reader();
|
|
51
|
+
highWater = reader.highWater(session).toString();
|
|
52
|
+
const rows = reader.readSession(session);
|
|
53
|
+
count = rows.length;
|
|
54
|
+
// Identity only: never sourceBytes or prompt text (reader-only rule).
|
|
55
|
+
occurrences = rows.slice(-500).map((occ) => ({
|
|
56
|
+
seq: occ.seq.toString(),
|
|
57
|
+
eventId: occ.eventId,
|
|
58
|
+
kind: occ.kind,
|
|
59
|
+
digest: occ.digest,
|
|
60
|
+
...(occ.toolCallId !== undefined ? { toolCallId: occ.toolCallId } : {}),
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
store.close();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Non-fatal: a missing/corrupt ledger DB degrades to `enabled:false`.
|
|
69
|
+
active = false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const body = {
|
|
73
|
+
enabled: active,
|
|
74
|
+
session,
|
|
75
|
+
highWater,
|
|
76
|
+
count,
|
|
77
|
+
occurrences,
|
|
78
|
+
updatedAt: new Date().toISOString(),
|
|
79
|
+
};
|
|
80
|
+
sendJson(res, 200, body);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard-server/routes-vector-cortex-shared.ts — shared HTTP helpers for the
|
|
3
|
+
* per-feature vector-cortex route modules.
|
|
4
|
+
*
|
|
5
|
+
* Homes the small request/response helpers used across the VC0A/VC0C/VC1B/VC3A
|
|
6
|
+
* handler modules (`sendJson`, `readJsonBody`) so the feature modules stay small
|
|
7
|
+
* and within extension line limits.
|
|
8
|
+
*
|
|
9
|
+
* Guardrails: PREVENT-PI-004 (loopback dashboard response only), PREVENT-011 (no
|
|
10
|
+
* `any`), PREVENT-001 (JSON.parse guarded by an object/shape check).
|
|
11
|
+
*/
|
|
12
|
+
export function sendJson(res, status, body) {
|
|
13
|
+
// guardrails-allow PREVENT-PI-004: loopback dashboard response (local)
|
|
14
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
15
|
+
res.end(JSON.stringify(body));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parse a JSON request body with a size cap. `cb` receives either a validated
|
|
19
|
+
* plain object or an error reason. PREVENT-001: JSON.parse is guarded by a
|
|
20
|
+
* shape check (rejects null / arrays / non-objects).
|
|
21
|
+
*/
|
|
22
|
+
export function readJsonBody(req, cb) {
|
|
23
|
+
let body = "";
|
|
24
|
+
let tooBig = false;
|
|
25
|
+
req.on("data", (chunk) => {
|
|
26
|
+
if (body.length > 65536) {
|
|
27
|
+
tooBig = true;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
body += chunk.toString();
|
|
31
|
+
});
|
|
32
|
+
req.on("end", () => {
|
|
33
|
+
if (tooBig)
|
|
34
|
+
return cb({ ok: false, error: "body_too_large" });
|
|
35
|
+
try {
|
|
36
|
+
const v = body ? JSON.parse(body) : {};
|
|
37
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) {
|
|
38
|
+
return cb({ ok: false, error: "invalid_object" });
|
|
39
|
+
}
|
|
40
|
+
cb({ ok: true, value: v });
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
cb({ ok: false, error: "invalid_json" });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard-server/routes-vector-cortex-topology.ts — VC3A/VC3B vector-cortex
|
|
3
|
+
* topology dashboard route.
|
|
4
|
+
*
|
|
5
|
+
* Reader-only GET /api/vector-cortex/topology built ENTIRELY on the CortexReader
|
|
6
|
+
* capability: opens the isolated cortex DB for this repo's state dir and returns
|
|
7
|
+
* the topology summary (enabled, active generation identity, one root digest,
|
|
8
|
+
* derived frontier, record count, ordinal). It is query-only — never
|
|
9
|
+
* append/rebuild/switch. No writer or admin capability is reachable on this path
|
|
10
|
+
* (no writer/admin leakage). Non-fatal: a missing or corrupt cortex DB degrades
|
|
11
|
+
* to `enabled:false`.
|
|
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
|
+
*
|
|
21
|
+
* Guardrails: PREVENT-PI-004 (local filesystem read only), PREVENT-011 (no
|
|
22
|
+
* `any`), reader-only aggregate.
|
|
23
|
+
*/
|
|
24
|
+
import { VC3A_ENABLED, VC3B_ENABLED } from "../../src/config.js";
|
|
25
|
+
import { Logger } from "../../src/log.js";
|
|
26
|
+
import { createCortexStore } from "../../src/vector-cortex/cortex/store.js";
|
|
27
|
+
import { buildTopologyGraph } from "../../src/vector-cortex/topology/index.js";
|
|
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";
|
|
31
|
+
/**
|
|
32
|
+
* Reader-only GET /api/vector-cortex/topology (VC3A + VC3B node/edge shapes).
|
|
33
|
+
*/
|
|
34
|
+
export function handleVectorCortexTopology(req, res, ctx) {
|
|
35
|
+
const url = req.url ?? "";
|
|
36
|
+
const path = url.split("?")[0] ?? url;
|
|
37
|
+
if (path !== "/api/vector-cortex/topology")
|
|
38
|
+
return false;
|
|
39
|
+
if (req.method !== "GET") {
|
|
40
|
+
// Reader-only path: no mutation endpoint lives at /topology.
|
|
41
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
const enabled = VC3A_ENABLED();
|
|
45
|
+
const topologyEnabled = VC3B_ENABLED();
|
|
46
|
+
let active = enabled;
|
|
47
|
+
let summary = { generationId: null, rootDigest: null, sourceHighWater: "0", recordCount: 0, ordinal: null };
|
|
48
|
+
let nodes;
|
|
49
|
+
let edges;
|
|
50
|
+
let generationDigest;
|
|
51
|
+
if (enabled) {
|
|
52
|
+
try {
|
|
53
|
+
const store = createCortexStore({ stateDir: ctx.stateDir });
|
|
54
|
+
try {
|
|
55
|
+
const reader = store.reader();
|
|
56
|
+
const s = reader.topologySummary();
|
|
57
|
+
summary = {
|
|
58
|
+
generationId: s.generationId,
|
|
59
|
+
rootDigest: s.rootDigest,
|
|
60
|
+
sourceHighWater: s.sourceHighWater,
|
|
61
|
+
recordCount: s.recordCount,
|
|
62
|
+
ordinal: s.ordinal,
|
|
63
|
+
};
|
|
64
|
+
if (topologyEnabled) {
|
|
65
|
+
const built = buildFromRecords(reader.readRecords());
|
|
66
|
+
nodes = built.nodes;
|
|
67
|
+
edges = built.edges;
|
|
68
|
+
generationDigest = built.generationDigest;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
store.close();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Non-fatal: a missing/corrupt cortex DB degrades to `enabled:false`.
|
|
77
|
+
active = false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const body = {
|
|
81
|
+
enabled: active,
|
|
82
|
+
generationId: summary.generationId,
|
|
83
|
+
rootDigest: summary.rootDigest,
|
|
84
|
+
sourceHighWater: summary.sourceHighWater,
|
|
85
|
+
recordCount: summary.recordCount,
|
|
86
|
+
ordinal: summary.ordinal,
|
|
87
|
+
...(topologyEnabled
|
|
88
|
+
? { nodes, edges, generationDigest }
|
|
89
|
+
: {}),
|
|
90
|
+
updatedAt: new Date().toISOString(),
|
|
91
|
+
};
|
|
92
|
+
sendJson(res, 200, body);
|
|
93
|
+
return true;
|
|
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 = /[|~]/;
|