pi-mega-compact 0.18.1 → 0.19.1
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/assets/vector-cortex/encoder-v1/manifest.json +1 -0
- package/assets/vector-cortex/encoder-v1/model-card.json +1 -0
- package/assets/vector-cortex/encoder-v1/model.onnx +0 -0
- package/assets/vector-cortex/encoder-v1/tokenizer.json +1 -0
- 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 +68 -0
- package/dist/extensions/dashboard-server/routes-vector-cortex.js +12 -285
- 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/encoder/calibrate.js +174 -0
- package/dist/src/vector-cortex/encoder/emit-vc2c.js +59 -0
- package/dist/src/vector-cortex/encoder/fallback.js +62 -0
- package/dist/src/vector-cortex/encoder/select.js +172 -0
- package/dist/src/vector-cortex/encoder/types.js +31 -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/encoder/calibrate.js +174 -0
- package/dist/vector-cortex/encoder/emit-vc2c.js +59 -0
- package/dist/vector-cortex/encoder/fallback.js +62 -0
- package/dist/vector-cortex/encoder/select.js +172 -0
- package/dist/vector-cortex/encoder/types.js +31 -0
- package/extensions/dashboard-client/dist/assets/{AreaChart-BQXFF7UI.js → AreaChart-B9KYKrUE.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{AreaChart-BQXFF7UI.js.map → AreaChart-B9KYKrUE.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{BarChart-BVtoz9Eg.js → BarChart-D1sWkR6j.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{BarChart-BVtoz9Eg.js.map → BarChart-D1sWkR6j.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{CacheTab-trI2TIGy.js → CacheTab-BR8MYcJq.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{CacheTab-trI2TIGy.js.map → CacheTab-BR8MYcJq.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{EventsTab-CNUbk9__.js → EventsTab-BRNpDMvP.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{EventsTab-CNUbk9__.js.map → EventsTab-BRNpDMvP.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{HealthTab-K-bd_CM-.js → HealthTab-rr5nXpjq.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{HealthTab-K-bd_CM-.js.map → HealthTab-rr5nXpjq.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CoEioKzx.js → MaintenanceTab-CtjPWnzN.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MaintenanceTab-CoEioKzx.js.map → MaintenanceTab-CtjPWnzN.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-CJo24ulv.js → MemoryMapTab-5SnFDIYc.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MemoryMapTab-CJo24ulv.js.map → MemoryMapTab-5SnFDIYc.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{MetricsTab-DCNlQWWP.js → MetricsTab-4MZfwRhq.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{MetricsTab-DCNlQWWP.js.map → MetricsTab-4MZfwRhq.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{OverviewTab-CQQlyIQ6.js → OverviewTab-BAKENnbk.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{OverviewTab-CQQlyIQ6.js.map → OverviewTab-BAKENnbk.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{ReposTab-Cqm82_yn.js → ReposTab-7-z_OmZo.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{ReposTab-Cqm82_yn.js.map → ReposTab-7-z_OmZo.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SessionsTab-A8xTAdET.js → SessionsTab-D_r2ki0y.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SessionsTab-A8xTAdET.js.map → SessionsTab-D_r2ki0y.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{SetupTab-CqaSgaZl.js → SetupTab-DPujtYgv.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{SetupTab-CqaSgaZl.js.map → SetupTab-DPujtYgv.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-C_f3q8GR.js → TimeSavedCard-CLHy30t6.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TimeSavedCard-C_f3q8GR.js.map → TimeSavedCard-CLHy30t6.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{TurnsTab-BEsafACo.js → TurnsTab-Coul6CHc.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{TurnsTab-BEsafACo.js.map → TurnsTab-Coul6CHc.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js +2 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-rqBpWc7A.js.map +1 -0
- package/extensions/dashboard-client/dist/assets/{WikiTab-Bf01M2_Z.js → WikiTab-hryG5oyY.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{WikiTab-Bf01M2_Z.js.map → WikiTab-hryG5oyY.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{button-BEJPTk_E.js → button-Dxo95lRI.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{button-BEJPTk_E.js.map → button-Dxo95lRI.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{card-DK1V382s.js → card-FXwhLF_u.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{card-DK1V382s.js.map → card-FXwhLF_u.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-CRtaPphL.js → generateCategoricalChart-M-GeRM8U.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-CRtaPphL.js.map → generateCategoricalChart-M-GeRM8U.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{index-CB6BslE4.js → index-BIZZXvoy.js} +3 -3
- package/extensions/dashboard-client/dist/assets/{index-CB6BslE4.js.map → index-BIZZXvoy.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/index-CYpzq9Lm.css +1 -0
- package/extensions/dashboard-client/dist/assets/{switch-C4FV6B9k.js → switch-C-d4tkN1.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{switch-C4FV6B9k.js.map → switch-C-d4tkN1.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{toggle-GBmeENMd.js → toggle-Bfmucdcl.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{toggle-GBmeENMd.js.map → toggle-Bfmucdcl.js.map} +1 -1
- package/extensions/dashboard-client/dist/assets/{useSSE-BOKZGSn1.js → useSSE-CxK_wdmk.js} +2 -2
- package/extensions/dashboard-client/dist/assets/{useSSE-BOKZGSn1.js.map → useSSE-CxK_wdmk.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 +36 -0
- package/extensions/dashboard-client/src/types/vector-cortex.ts +26 -0
- package/extensions/dashboard-server/api-contracts/vector-cortex.ts +34 -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 +83 -0
- package/extensions/dashboard-server/routes-vector-cortex.ts +12 -324
- package/extensions/dashboard-server/routes.ts +1 -0
- package/extensions/dashboard-server/server.ts +2 -0
- package/package.json +2 -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/encoder/calibrate.ts +225 -0
- package/src/vector-cortex/encoder/emit-vc2c.ts +70 -0
- package/src/vector-cortex/encoder/fallback.ts +99 -0
- package/src/vector-cortex/encoder/select.ts +237 -0
- package/src/vector-cortex/encoder/types.ts +116 -0
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DD1CMK80.js +0 -2
- package/extensions/dashboard-client/dist/assets/VectorCortexTab-DD1CMK80.js.map +0 -1
- package/extensions/dashboard-client/dist/assets/index-CfvziyBi.css +0 -1
|
@@ -12,10 +12,12 @@ import {
|
|
|
12
12
|
fetchVectorCortexEvaluation,
|
|
13
13
|
fetchVectorCortexHealth,
|
|
14
14
|
fetchVectorCortexLedger,
|
|
15
|
+
fetchVectorCortexTopology,
|
|
15
16
|
resetVectorCortexBreaker,
|
|
16
17
|
type VectorCortexEvaluationSummary,
|
|
17
18
|
type VectorCortexHealthCard,
|
|
18
19
|
type VectorCortexLedgerView,
|
|
20
|
+
type VectorCortexTopologyView,
|
|
19
21
|
} from "../api/vector-cortex";
|
|
20
22
|
import { Card, CardContent, CardHeader, CardTitle } from "../components/ui/card";
|
|
21
23
|
import { Badge } from "../components/ui/badge";
|
|
@@ -45,6 +47,7 @@ export default function VectorCortexTab(): React.ReactElement {
|
|
|
45
47
|
const [health, setHealth] = useState<VectorCortexHealthCard | null>(null);
|
|
46
48
|
const [resetMsg, setResetMsg] = useState<string | null>(null);
|
|
47
49
|
const [ledger, setLedger] = useState<VectorCortexLedgerView | null>(null);
|
|
50
|
+
const [topology, setTopology] = useState<VectorCortexTopologyView | null>(null);
|
|
48
51
|
|
|
49
52
|
const poll = useCallback(() => {
|
|
50
53
|
fetchVectorCortexEvaluation()
|
|
@@ -59,6 +62,9 @@ export default function VectorCortexTab(): React.ReactElement {
|
|
|
59
62
|
fetchVectorCortexLedger().then(setLedger).catch(() => {
|
|
60
63
|
/* ledger card is best-effort (VC1B) */
|
|
61
64
|
});
|
|
65
|
+
fetchVectorCortexTopology().then(setTopology).catch(() => {
|
|
66
|
+
/* topology card is best-effort (VC3A) */
|
|
67
|
+
});
|
|
62
68
|
}, []);
|
|
63
69
|
|
|
64
70
|
useEffect(() => {
|
|
@@ -203,6 +209,36 @@ export default function VectorCortexTab(): React.ReactElement {
|
|
|
203
209
|
}
|
|
204
210
|
/>
|
|
205
211
|
<Metric label="Spool lag" value={String(health.spoolLag)} />
|
|
212
|
+
<Metric label="Encoder mode" value={health.encoderMode} />
|
|
213
|
+
<Metric label="Encoder asset" value={health.encoderAssetDigest ? health.encoderAssetDigest.slice(0, 12) : "none"} />
|
|
214
|
+
</div>
|
|
215
|
+
)}
|
|
216
|
+
</CardContent>
|
|
217
|
+
</Card>
|
|
218
|
+
<Card>
|
|
219
|
+
<CardHeader>
|
|
220
|
+
<div className="flex items-center justify-between">
|
|
221
|
+
<CardTitle>Derived Cortex Store (VC3A)</CardTitle>
|
|
222
|
+
{topology?.enabled ? (
|
|
223
|
+
<Badge variant="success">ACTIVE</Badge>
|
|
224
|
+
) : (
|
|
225
|
+
<Badge variant="danger">OFF</Badge>
|
|
226
|
+
)}
|
|
227
|
+
</div>
|
|
228
|
+
</CardHeader>
|
|
229
|
+
<CardContent>
|
|
230
|
+
{!topology?.enabled ? (
|
|
231
|
+
<div className="vc-empty">Cortex store disabled (VC3A off).</div>
|
|
232
|
+
) : (
|
|
233
|
+
<div className="grid grid-cols-2 gap-4 md:grid-cols-3">
|
|
234
|
+
<Metric label="Generation" value={topology.generationId ?? "—"} />
|
|
235
|
+
<Metric label="Ordinal" value={topology.ordinal ?? "—"} />
|
|
236
|
+
<Metric label="Records" value={String(topology.recordCount)} />
|
|
237
|
+
<Metric label="Frontier (HW)" value={topology.sourceHighWater} />
|
|
238
|
+
<Metric
|
|
239
|
+
label="Root digest"
|
|
240
|
+
value={topology.rootDigest ? topology.rootDigest.slice(0, 16) : "—"}
|
|
241
|
+
/>
|
|
206
242
|
</div>
|
|
207
243
|
)}
|
|
208
244
|
</CardContent>
|
|
@@ -50,6 +50,17 @@ export interface VectorCortexHealthCard {
|
|
|
50
50
|
aggregate: string;
|
|
51
51
|
/** "ephemeral" = per-process/in-memory breaker (non-live until VC0D). */
|
|
52
52
|
stateSource: "ephemeral" | "live";
|
|
53
|
+
/**
|
|
54
|
+
* VC2C encoder asset digest — SHA-256 of the committed ModelManifestV1 bytes.
|
|
55
|
+
* null when no qualified encoder manifest is present on this host.
|
|
56
|
+
*/
|
|
57
|
+
encoderAssetDigest: string | null;
|
|
58
|
+
/**
|
|
59
|
+
* VC2C encoder triad mode: "A" when the committed asset verifies as a
|
|
60
|
+
* qualified learned asset on this host/platform, else "B" (trigram) or "C"
|
|
61
|
+
* (lexical).
|
|
62
|
+
*/
|
|
63
|
+
encoderMode: "A" | "B" | "C";
|
|
53
64
|
}
|
|
54
65
|
|
|
55
66
|
/** Admin reset result (VC0C, task 5). */
|
|
@@ -64,6 +75,21 @@ export interface VectorCortexResetResult {
|
|
|
64
75
|
updatedAt: string;
|
|
65
76
|
}
|
|
66
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Reader-only cortex topology view (VC3A, GET /api/vector-cortex/topology).
|
|
80
|
+
* Aggregate only — active generation identity, one root digest, derived frontier
|
|
81
|
+
* and record count. Never append/rebuild capability or raw record payloads.
|
|
82
|
+
*/
|
|
83
|
+
export interface VectorCortexTopologyView {
|
|
84
|
+
enabled: boolean;
|
|
85
|
+
generationId: string | null;
|
|
86
|
+
rootDigest: string | null;
|
|
87
|
+
sourceHighWater: string;
|
|
88
|
+
recordCount: number;
|
|
89
|
+
ordinal: string | null;
|
|
90
|
+
updatedAt: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
67
93
|
/**
|
|
68
94
|
* Occurrence-ledger identity view (VC1B, GET /api/vector-cortex/ledger).
|
|
69
95
|
* Reader-only: seq/eventId/kind/digest + high-water, never source payloads.
|
|
@@ -90,6 +90,17 @@ export interface VectorCortexHealthCard {
|
|
|
90
90
|
* only once the persistent breaker runtime + producer wiring land (VC0D).
|
|
91
91
|
*/
|
|
92
92
|
readonly stateSource: "ephemeral" | "live";
|
|
93
|
+
/**
|
|
94
|
+
* VC2C encoder asset digest — SHA-256 of the committed ModelManifestV1 bytes.
|
|
95
|
+
* null when no qualified encoder manifest is present on this host.
|
|
96
|
+
*/
|
|
97
|
+
readonly encoderAssetDigest: string | null;
|
|
98
|
+
/**
|
|
99
|
+
* VC2C encoder triad mode: "A" when the committed asset verifies as a
|
|
100
|
+
* qualified learned asset on this host/platform, else "B" (trigram) or "C"
|
|
101
|
+
* (lexical). Reader-only aggregate — the measured digest prefix, never bytes.
|
|
102
|
+
*/
|
|
103
|
+
readonly encoderMode: "A" | "B" | "C";
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
/**
|
|
@@ -108,6 +119,29 @@ export interface VectorCortexResetResult {
|
|
|
108
119
|
readonly updatedAt: string;
|
|
109
120
|
}
|
|
110
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Reader-only cortex topology view for GET /api/vector-cortex/topology (VC3A).
|
|
124
|
+
* Built on the CortexReader capability surface ONLY — it exposes the flagged
|
|
125
|
+
* enabled state, active generation identity, one root digest, derived frontier
|
|
126
|
+
* and record count, and NEVER append/rebuild capability or raw record payloads.
|
|
127
|
+
*/
|
|
128
|
+
export interface VectorCortexTopologyView {
|
|
129
|
+
/** Whether the VC3A cortex-store flag is enabled in this process. */
|
|
130
|
+
readonly enabled: boolean;
|
|
131
|
+
/** Active generation id, or null when none rebuilt yet. */
|
|
132
|
+
readonly generationId: string | null;
|
|
133
|
+
/** Active generation root digest, or null. */
|
|
134
|
+
readonly rootDigest: string | null;
|
|
135
|
+
/** Derived frontier (active generation sourceHighWater, else "0"). */
|
|
136
|
+
readonly sourceHighWater: string;
|
|
137
|
+
/** Accepted derived record count. */
|
|
138
|
+
readonly recordCount: number;
|
|
139
|
+
/** Monotonic rebuild ordinal, or null. */
|
|
140
|
+
readonly ordinal: string | null;
|
|
141
|
+
/** ISO timestamp of the snapshot. */
|
|
142
|
+
readonly updatedAt: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
111
145
|
/**
|
|
112
146
|
* Reader-only occurrence-ledger view for GET /api/vector-cortex/ledger (VC1B).
|
|
113
147
|
* Built on the LedgerReader capability surface. Exposes occurrence IDENTITY
|
|
@@ -325,6 +325,18 @@ export const SETTINGS: ReadonlyArray<{
|
|
|
325
325
|
"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.",
|
|
326
326
|
true,
|
|
327
327
|
),
|
|
328
|
+
boolDirect(
|
|
329
|
+
"MEGACOMPACT_VC2C",
|
|
330
|
+
"VC2C Encoder Qualification + Calibration",
|
|
331
|
+
"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.",
|
|
332
|
+
true,
|
|
333
|
+
),
|
|
334
|
+
boolDirect(
|
|
335
|
+
"MEGACOMPACT_VC3A",
|
|
336
|
+
"VC3A Cortex Store",
|
|
337
|
+
"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.",
|
|
338
|
+
true,
|
|
339
|
+
),
|
|
328
340
|
],
|
|
329
341
|
},
|
|
330
342
|
{
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
|
|
13
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
14
|
+
import type { RouteContext } from "./routes-core.js";
|
|
15
|
+
import { VC0A_ENABLED } from "../../src/config.js";
|
|
16
|
+
import { readEvalRows } from "../../src/vector-cortex/eval/persist.js";
|
|
17
|
+
import { summarizeEvalRows } from "../../src/vector-cortex/eval/reader.js";
|
|
18
|
+
import { sendJson } from "./routes-vector-cortex-shared.js";
|
|
19
|
+
import type { VectorCortexEvaluationSummary } from "./api-contracts/vector-cortex.js";
|
|
20
|
+
|
|
21
|
+
/** Reader-only aggregate GET /api/vector-cortex/evaluation. */
|
|
22
|
+
export function handleVectorCortexEvaluation(
|
|
23
|
+
req: IncomingMessage,
|
|
24
|
+
res: ServerResponse,
|
|
25
|
+
ctx: RouteContext,
|
|
26
|
+
): boolean {
|
|
27
|
+
const url = req.url ?? "";
|
|
28
|
+
if (url !== "/api/vector-cortex/evaluation") return false;
|
|
29
|
+
if (req.method !== "GET") {
|
|
30
|
+
// Reader-only: no mutation endpoints exist on this path.
|
|
31
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const enabled = VC0A_ENABLED();
|
|
36
|
+
const rows = enabled ? readEvalRows(ctx.stateDir) : [];
|
|
37
|
+
const summary = summarizeEvalRows(rows);
|
|
38
|
+
|
|
39
|
+
const body: VectorCortexEvaluationSummary = {
|
|
40
|
+
enabled,
|
|
41
|
+
mode: enabled ? "A" : "C",
|
|
42
|
+
samples: summary.samples,
|
|
43
|
+
byMode: {
|
|
44
|
+
A: summary.byMode.A,
|
|
45
|
+
B: summary.byMode.B,
|
|
46
|
+
C: summary.byMode.C,
|
|
47
|
+
},
|
|
48
|
+
histogram: {
|
|
49
|
+
edges: [...summary.histogram.edges],
|
|
50
|
+
cells: [...summary.histogram.cells],
|
|
51
|
+
overflow: summary.histogram.overflow,
|
|
52
|
+
total: summary.histogram.total,
|
|
53
|
+
},
|
|
54
|
+
rejects: [], // VC0C wires live breaker/reject telemetry here
|
|
55
|
+
updatedAt: new Date().toISOString(),
|
|
56
|
+
};
|
|
57
|
+
sendJson(res, 200, body);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
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
|
+
|
|
12
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { readFileSync, statSync } from "node:fs";
|
|
15
|
+
import { join, dirname } from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import type { RouteContext } from "./routes-core.js";
|
|
18
|
+
import { VC0C_ENABLED, VC2C_ENABLED } from "../../src/config.js";
|
|
19
|
+
import { createVectorCortexSafety } from "../mega-runtime/vector-cortex-safety.js";
|
|
20
|
+
import { readEncoderManifest, verifyEncoderAsset, detectPlatform } from "../../src/vector-cortex/encoder/asset.js";
|
|
21
|
+
import { sendJson, readJsonBody } from "./routes-vector-cortex-shared.js";
|
|
22
|
+
import type {
|
|
23
|
+
VectorCortexHealthCard,
|
|
24
|
+
VectorCortexResetResult,
|
|
25
|
+
} from "./api-contracts/vector-cortex.js";
|
|
26
|
+
|
|
27
|
+
/** Resolve the committed encoder-v1 asset dir by walking up to the repo root. */
|
|
28
|
+
function encoderAssetDir(): string | null {
|
|
29
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
30
|
+
const rel = join("assets", "vector-cortex", "encoder-v1");
|
|
31
|
+
for (let i = 0; i < 8; i++) {
|
|
32
|
+
const candidate = join(dir, rel);
|
|
33
|
+
try {
|
|
34
|
+
// guardrails-allow PREVENT-PI-004: local asset filesystem read (loopback)
|
|
35
|
+
readFileSync(join(candidate, "manifest.json"));
|
|
36
|
+
return candidate;
|
|
37
|
+
} catch {
|
|
38
|
+
/* keep walking */
|
|
39
|
+
}
|
|
40
|
+
const next = dirname(dir);
|
|
41
|
+
if (next === dir) break;
|
|
42
|
+
dir = next;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* VC2C encoder health facts (task 5): the SHA-256 of the committed qualified
|
|
49
|
+
* manifest (asset digest) and the encoder triad mode. Reader-only aggregate
|
|
50
|
+
* (digest prefix, no bytes). Gated on MEGACOMPACT_VC2C: when the flag is OFF
|
|
51
|
+
* (rollback), the card reports mode C with an absent digest — consistent with
|
|
52
|
+
* the rest of the card, whose enabled/mode fields reflect active (flag-gated)
|
|
53
|
+
* state. When ON: "A" when the committed asset verifies on this host, "B" when
|
|
54
|
+
* present but not verified (demotion), "C" when absent.
|
|
55
|
+
*
|
|
56
|
+
* The verification hashes the full ONNX + tokenizer bytes, which is expensive
|
|
57
|
+
* on a periodically-polled endpoint as the shipped asset grows. The result is
|
|
58
|
+
* memoized against a cheap key (manifest bytes + file sizes/mtimes + platform);
|
|
59
|
+
* any on-disk mutation invalidates the key, so the cache never goes stale while
|
|
60
|
+
* avoiding the repeated large-file hashing. A failed digest read returns null
|
|
61
|
+
* (an absent digest), never a misleading zero-sentinel.
|
|
62
|
+
*/
|
|
63
|
+
type EncoderHealthFacts = { assetDigest: string | null; mode: "A" | "B" | "C" };
|
|
64
|
+
|
|
65
|
+
interface HealthFactCache {
|
|
66
|
+
key: string;
|
|
67
|
+
facts: EncoderHealthFacts;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let healthCache: HealthFactCache | null = null;
|
|
71
|
+
|
|
72
|
+
/** Stable cache key: manifest bytes + (size,mtime) of the asset files + platform. */
|
|
73
|
+
function healthFactCacheKey(dir: string, manifestPath: string): string | null {
|
|
74
|
+
const platform = detectPlatform();
|
|
75
|
+
if (!platform) return null;
|
|
76
|
+
const parts: string[] = [platform];
|
|
77
|
+
try {
|
|
78
|
+
for (const p of [manifestPath, join(dir, "model.onnx"), join(dir, "tokenizer.json")]) {
|
|
79
|
+
const s = statSync(p);
|
|
80
|
+
parts.push(`${s.size}:${s.mtimeMs}`);
|
|
81
|
+
}
|
|
82
|
+
// guardrails-allow PREVENT-PI-004: local manifest filesystem read (loopback)
|
|
83
|
+
parts.push(createHash("sha256").update(readFileSync(manifestPath)).digest("hex"));
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return parts.join("|");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function computeEncoderHealthFacts(dir: string): EncoderHealthFacts | null {
|
|
91
|
+
const manifest = readEncoderManifest(dir);
|
|
92
|
+
if (manifest === null) return { assetDigest: null, mode: "C" };
|
|
93
|
+
const manifestPath = join(dir, "manifest.json");
|
|
94
|
+
const key = healthFactCacheKey(dir, manifestPath);
|
|
95
|
+
if (key === null) return null;
|
|
96
|
+
if (healthCache !== null && healthCache.key === key) return healthCache.facts;
|
|
97
|
+
let digest: string;
|
|
98
|
+
try {
|
|
99
|
+
// guardrails-allow PREVENT-PI-004: local manifest filesystem read (loopback)
|
|
100
|
+
digest = createHash("sha256").update(readFileSync(manifestPath)).digest("hex");
|
|
101
|
+
} catch {
|
|
102
|
+
return { assetDigest: null, mode: "B" };
|
|
103
|
+
}
|
|
104
|
+
const verify = verifyEncoderAsset(dir, manifest, detectPlatform());
|
|
105
|
+
const facts: EncoderHealthFacts = { assetDigest: digest, mode: verify.ok ? "A" : "B" };
|
|
106
|
+
healthCache = { key, facts };
|
|
107
|
+
return facts;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function encoderHealthFacts(): EncoderHealthFacts {
|
|
111
|
+
if (!VC2C_ENABLED()) {
|
|
112
|
+
// Rollback: MEGACOMPACT_VC2C=0 selects C — report the inactive (flag-gated)
|
|
113
|
+
// mode with no asset digest rather than an on-disk-computed A/B.
|
|
114
|
+
return { assetDigest: null, mode: "C" };
|
|
115
|
+
}
|
|
116
|
+
const dir = encoderAssetDir();
|
|
117
|
+
if (dir === null) return { assetDigest: null, mode: "C" };
|
|
118
|
+
const facts = computeEncoderHealthFacts(dir);
|
|
119
|
+
if (facts === null) return { assetDigest: null, mode: "C" };
|
|
120
|
+
return facts;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Reader-only GET /api/vector-cortex/health (VC0C).
|
|
125
|
+
*
|
|
126
|
+
* Aggregate health card: breaker state (window/probe/backoff), durable spool
|
|
127
|
+
* frontier/authority/lag, and a worst-state aggregate. Constructs the safety
|
|
128
|
+
* adapter over the state dir — durable spool state (frontier-frozen, lag) is
|
|
129
|
+
* read from disk, so it reflects restart. Purely read: no mutation, never
|
|
130
|
+
* payloads/prompts/ledger.
|
|
131
|
+
*/
|
|
132
|
+
export function handleVectorCortexHealth(
|
|
133
|
+
req: IncomingMessage,
|
|
134
|
+
res: ServerResponse,
|
|
135
|
+
ctx: RouteContext,
|
|
136
|
+
): boolean {
|
|
137
|
+
const url = req.url ?? "";
|
|
138
|
+
if (url !== "/api/vector-cortex/health") return false;
|
|
139
|
+
if (req.method !== "GET") {
|
|
140
|
+
// Reader-only path: no mutation endpoint lives at /health.
|
|
141
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const enabled = VC0C_ENABLED();
|
|
146
|
+
const safety = enabled ? createVectorCortexSafety({ stateDir: ctx.stateDir }) : null;
|
|
147
|
+
const card = safety ? safety.health() : null;
|
|
148
|
+
|
|
149
|
+
const enc = encoderHealthFacts();
|
|
150
|
+
|
|
151
|
+
const fallback: VectorCortexHealthCard = {
|
|
152
|
+
enabled: false,
|
|
153
|
+
mode: "C",
|
|
154
|
+
state: "CLOSED_A",
|
|
155
|
+
subsystem: "provider",
|
|
156
|
+
sinceMs: 0,
|
|
157
|
+
windowMs: 0,
|
|
158
|
+
probeCount: 0,
|
|
159
|
+
backoffDelayMs: 0,
|
|
160
|
+
frontierFrozen: false,
|
|
161
|
+
authorityOutage: false,
|
|
162
|
+
spoolLag: 0,
|
|
163
|
+
attempts: 0,
|
|
164
|
+
failures: 0,
|
|
165
|
+
p95Ms: 0,
|
|
166
|
+
failureRate: 0,
|
|
167
|
+
updatedAt: new Date().toISOString(),
|
|
168
|
+
aggregate: "CLOSED_A",
|
|
169
|
+
stateSource: "ephemeral",
|
|
170
|
+
encoderAssetDigest: enc.assetDigest,
|
|
171
|
+
encoderMode: enc.mode,
|
|
172
|
+
};
|
|
173
|
+
if (!card) {
|
|
174
|
+
sendJson(res, 200, fallback);
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const mode: "A" | "B" | "C" =
|
|
179
|
+
card.state === "CLOSED_A" ? "A" : card.state === "MANUAL_HALT" ? "C" : "B";
|
|
180
|
+
const aggregate =
|
|
181
|
+
card.state === "MANUAL_HALT"
|
|
182
|
+
? "MANUAL_HALT"
|
|
183
|
+
: card.state === "OPEN_C"
|
|
184
|
+
? "OPEN_C"
|
|
185
|
+
: card.state === "OPEN_B"
|
|
186
|
+
? "OPEN_B"
|
|
187
|
+
: card.state === "PROBE_B" || card.state === "PROBE_A"
|
|
188
|
+
? "PROBE"
|
|
189
|
+
: "CLOSED_A";
|
|
190
|
+
|
|
191
|
+
const body: VectorCortexHealthCard = {
|
|
192
|
+
enabled,
|
|
193
|
+
mode,
|
|
194
|
+
state: card.state,
|
|
195
|
+
subsystem: card.subsystem,
|
|
196
|
+
sinceMs: card.sinceMs,
|
|
197
|
+
reason: card.reason,
|
|
198
|
+
windowMs: card.windowMs,
|
|
199
|
+
probeCount: card.probeCount,
|
|
200
|
+
backoffDelayMs: card.backoffDelayMs,
|
|
201
|
+
frontierFrozen: card.frontierFrozen,
|
|
202
|
+
authorityOutage: card.authorityOutage,
|
|
203
|
+
spoolLag: card.spoolLag,
|
|
204
|
+
attempts: card.attempts,
|
|
205
|
+
failures: card.failures,
|
|
206
|
+
p95Ms: card.p95Ms,
|
|
207
|
+
failureRate: card.failureRate,
|
|
208
|
+
updatedAt: new Date().toISOString(),
|
|
209
|
+
aggregate,
|
|
210
|
+
stateSource: card.stateSource,
|
|
211
|
+
encoderAssetDigest: enc.assetDigest,
|
|
212
|
+
encoderMode: enc.mode,
|
|
213
|
+
};
|
|
214
|
+
sendJson(res, 200, body);
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Admin POST /api/vector-cortex/breakers/reset (VC0C).
|
|
220
|
+
*
|
|
221
|
+
* Explicit admin capability (separate from the reader GET). Clears a breaker's
|
|
222
|
+
* cooldown — or unwires a MANUAL_HALT — but NEVER evidence (attempts/failures
|
|
223
|
+
* are retained, per TRIAD_RESILIENCE). The subsystem is actor-supplied via the
|
|
224
|
+
* JSON body `{ subsystem }`. Every reset is documented in the returned record
|
|
225
|
+
* (state, retained evidence, updatedAt) — an auditable mutation surface; the
|
|
226
|
+
* GET path can never mutate.
|
|
227
|
+
*/
|
|
228
|
+
export function handleVectorCortexBreakersReset(
|
|
229
|
+
req: IncomingMessage,
|
|
230
|
+
res: ServerResponse,
|
|
231
|
+
ctx: RouteContext,
|
|
232
|
+
): boolean {
|
|
233
|
+
const url = req.url ?? "";
|
|
234
|
+
if (url !== "/api/vector-cortex/breakers/reset") return false;
|
|
235
|
+
if (req.method !== "POST") {
|
|
236
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
if (!VC0C_ENABLED()) {
|
|
240
|
+
sendJson(res, 409, { error: "vco_c_disabled" });
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
readJsonBody(req, (parsed) => {
|
|
245
|
+
if (!parsed.ok) {
|
|
246
|
+
sendJson(res, 400, { error: parsed.error });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const subsystem = parsed.value.subsystem;
|
|
250
|
+
if (typeof subsystem !== "string" || subsystem.length === 0) {
|
|
251
|
+
sendJson(res, 400, { error: "subsystem_required" });
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
const safety = createVectorCortexSafety({ stateDir: ctx.stateDir });
|
|
256
|
+
const rec = safety.reset(subsystem);
|
|
257
|
+
const body: VectorCortexResetResult = {
|
|
258
|
+
subsystem: rec.subsystem,
|
|
259
|
+
state: rec.state,
|
|
260
|
+
cooldownCleared: true,
|
|
261
|
+
attempts: rec.attempts,
|
|
262
|
+
failures: rec.failures,
|
|
263
|
+
probeCount: rec.probeCount,
|
|
264
|
+
manualReason: rec.manualReason,
|
|
265
|
+
updatedAt: rec.updatedAt,
|
|
266
|
+
};
|
|
267
|
+
sendJson(res, 200, body);
|
|
268
|
+
} catch {
|
|
269
|
+
sendJson(res, 500, { error: "reset_failed" });
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
|
|
15
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
16
|
+
import type { RouteContext } from "./routes-core.js";
|
|
17
|
+
import { VC1B_ENABLED } from "../../src/config.js";
|
|
18
|
+
import { createLedgerStore } from "../../src/vector-cortex/ledger/store.js";
|
|
19
|
+
import { sendJson } from "./routes-vector-cortex-shared.js";
|
|
20
|
+
import type { VectorCortexLedgerView } from "./api-contracts/vector-cortex.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reader-only GET /api/vector-cortex/ledger (VC1B).
|
|
24
|
+
*/
|
|
25
|
+
export function handleVectorCortexLedger(
|
|
26
|
+
req: IncomingMessage,
|
|
27
|
+
res: ServerResponse,
|
|
28
|
+
ctx: RouteContext,
|
|
29
|
+
): boolean {
|
|
30
|
+
const url = req.url ?? "";
|
|
31
|
+
const path = url.split("?")[0] ?? url;
|
|
32
|
+
if (path !== "/api/vector-cortex/ledger") return false;
|
|
33
|
+
if (req.method !== "GET") {
|
|
34
|
+
// Reader-only path: no mutation endpoint lives at /ledger.
|
|
35
|
+
sendJson(res, 405, { error: "method_not_allowed" });
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Parse the optional session query parameter.
|
|
40
|
+
let session = "default";
|
|
41
|
+
const qs = url.split("?")[1];
|
|
42
|
+
if (qs !== undefined) {
|
|
43
|
+
for (const pair of qs.split("&")) {
|
|
44
|
+
const [k, v] = pair.split("=");
|
|
45
|
+
if (k === "session" && v !== undefined && v.length > 0) {
|
|
46
|
+
session = decodeURIComponent(v);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const enabled = VC1B_ENABLED();
|
|
52
|
+
let active = enabled;
|
|
53
|
+
let highWater = "0";
|
|
54
|
+
let count = 0;
|
|
55
|
+
let occurrences: VectorCortexLedgerView["occurrences"] = [];
|
|
56
|
+
|
|
57
|
+
if (enabled) {
|
|
58
|
+
try {
|
|
59
|
+
const store = createLedgerStore({ stateDir: ctx.stateDir });
|
|
60
|
+
try {
|
|
61
|
+
const reader = store.reader();
|
|
62
|
+
highWater = reader.highWater(session).toString();
|
|
63
|
+
const rows = reader.readSession(session);
|
|
64
|
+
count = rows.length;
|
|
65
|
+
// Identity only: never sourceBytes or prompt text (reader-only rule).
|
|
66
|
+
occurrences = rows.slice(-500).map((occ) => ({
|
|
67
|
+
seq: occ.seq.toString(),
|
|
68
|
+
eventId: occ.eventId,
|
|
69
|
+
kind: occ.kind,
|
|
70
|
+
digest: occ.digest,
|
|
71
|
+
...(occ.toolCallId !== undefined ? { toolCallId: occ.toolCallId } : {}),
|
|
72
|
+
}));
|
|
73
|
+
} finally {
|
|
74
|
+
store.close();
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
// Non-fatal: a missing/corrupt ledger DB degrades to `enabled:false`.
|
|
78
|
+
active = false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const body: VectorCortexLedgerView = {
|
|
83
|
+
enabled: active,
|
|
84
|
+
session,
|
|
85
|
+
highWater,
|
|
86
|
+
count,
|
|
87
|
+
occurrences,
|
|
88
|
+
updatedAt: new Date().toISOString(),
|
|
89
|
+
};
|
|
90
|
+
sendJson(res, 200, body);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
|
|
13
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
14
|
+
|
|
15
|
+
export function sendJson(res: ServerResponse, status: number, body: unknown): void {
|
|
16
|
+
// guardrails-allow PREVENT-PI-004: loopback dashboard response (local)
|
|
17
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
18
|
+
res.end(JSON.stringify(body));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Parse a JSON request body with a size cap. `cb` receives either a validated
|
|
23
|
+
* plain object or an error reason. PREVENT-001: JSON.parse is guarded by a
|
|
24
|
+
* shape check (rejects null / arrays / non-objects).
|
|
25
|
+
*/
|
|
26
|
+
export function readJsonBody(
|
|
27
|
+
req: IncomingMessage,
|
|
28
|
+
cb: (
|
|
29
|
+
result: { ok: true; value: Record<string, unknown> } | { ok: false; error: string },
|
|
30
|
+
) => void,
|
|
31
|
+
): void {
|
|
32
|
+
let body = "";
|
|
33
|
+
let tooBig = false;
|
|
34
|
+
req.on("data", (chunk: Buffer) => {
|
|
35
|
+
if (body.length > 65536) {
|
|
36
|
+
tooBig = true;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
body += chunk.toString();
|
|
40
|
+
});
|
|
41
|
+
req.on("end", () => {
|
|
42
|
+
if (tooBig) return cb({ ok: false, error: "body_too_large" });
|
|
43
|
+
try {
|
|
44
|
+
const v = body ? JSON.parse(body) : {};
|
|
45
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) {
|
|
46
|
+
return cb({ ok: false, error: "invalid_object" });
|
|
47
|
+
}
|
|
48
|
+
cb({ ok: true, value: v as Record<string, unknown> });
|
|
49
|
+
} catch {
|
|
50
|
+
cb({ ok: false, error: "invalid_json" });
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|