pi-mega-compact 0.16.3 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/dist/config/vector-cortex.js +81 -0
  2. package/dist/extensions/dashboard-server/api-contracts/endpoints/registry-ext.js +30 -0
  3. package/dist/extensions/dashboard-server/api-contracts/endpoints/registry.js +4 -14
  4. package/dist/extensions/dashboard-server/api-contracts/vector-cortex.js +12 -0
  5. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +10 -0
  6. package/dist/extensions/dashboard-server/routes-vector-cortex.js +292 -0
  7. package/dist/extensions/dashboard-server/routes.js +1 -0
  8. package/dist/extensions/dashboard-server/server.js +9 -1
  9. package/dist/extensions/mega-events/context-handler.js +11 -0
  10. package/dist/extensions/mega-runtime/vector-cortex-ledger.js +131 -0
  11. package/dist/extensions/mega-runtime/vector-cortex-safety.js +100 -0
  12. package/dist/src/config/vector-cortex.js +81 -0
  13. package/dist/src/config.js +6 -0
  14. package/dist/src/vector-cortex/eval/annotations.js +41 -0
  15. package/dist/src/vector-cortex/eval/metrics.js +86 -0
  16. package/dist/src/vector-cortex/eval/observer.js +61 -0
  17. package/dist/src/vector-cortex/eval/persist.js +67 -0
  18. package/dist/src/vector-cortex/eval/reader.js +29 -0
  19. package/dist/src/vector-cortex/eval/types.js +33 -0
  20. package/dist/src/vector-cortex/ledger/adapter.js +55 -0
  21. package/dist/src/vector-cortex/ledger/compat-journal.js +206 -0
  22. package/dist/src/vector-cortex/ledger/emit.js +53 -0
  23. package/dist/src/vector-cortex/ledger/event-codec.js +72 -0
  24. package/dist/src/vector-cortex/ledger/event-codecB.js +76 -0
  25. package/dist/src/vector-cortex/ledger/sqlite.js +200 -0
  26. package/dist/src/vector-cortex/ledger/store.js +184 -0
  27. package/dist/src/vector-cortex/ledger/types.js +52 -0
  28. package/dist/src/vector-cortex/ledger/validator.js +117 -0
  29. package/dist/src/vector-cortex/migrations/effective-cut-v2.js +103 -0
  30. package/dist/src/vector-cortex/migrations/occurrence-v2.js +139 -0
  31. package/dist/src/vector-cortex/replay/cut.js +123 -0
  32. package/dist/src/vector-cortex/replay/emit.js +58 -0
  33. package/dist/src/vector-cortex/replay/replay.js +260 -0
  34. package/dist/src/vector-cortex/replay/replayB.js +73 -0
  35. package/dist/src/vector-cortex/replay/types.js +52 -0
  36. package/dist/src/vector-cortex/resilience/breaker-core.js +359 -0
  37. package/dist/src/vector-cortex/resilience/breaker.js +12 -0
  38. package/dist/src/vector-cortex/resilience/emit.js +56 -0
  39. package/dist/src/vector-cortex/resilience/spool-core.js +347 -0
  40. package/dist/src/vector-cortex/resilience/spool.js +12 -0
  41. package/dist/src/vector-cortex/resilience/types.js +32 -0
  42. package/dist/vector-cortex/eval/annotations.js +41 -0
  43. package/dist/vector-cortex/eval/metrics.js +86 -0
  44. package/dist/vector-cortex/eval/observer.js +61 -0
  45. package/dist/vector-cortex/eval/persist.js +67 -0
  46. package/dist/vector-cortex/eval/reader.js +29 -0
  47. package/dist/vector-cortex/eval/types.js +33 -0
  48. package/dist/vector-cortex/ledger/adapter.js +55 -0
  49. package/dist/vector-cortex/ledger/compat-journal.js +206 -0
  50. package/dist/vector-cortex/ledger/emit.js +53 -0
  51. package/dist/vector-cortex/ledger/event-codec.js +72 -0
  52. package/dist/vector-cortex/ledger/event-codecB.js +76 -0
  53. package/dist/vector-cortex/ledger/sqlite.js +200 -0
  54. package/dist/vector-cortex/ledger/store.js +184 -0
  55. package/dist/vector-cortex/ledger/types.js +52 -0
  56. package/dist/vector-cortex/ledger/validator.js +117 -0
  57. package/dist/vector-cortex/migrations/effective-cut-v2.js +103 -0
  58. package/dist/vector-cortex/migrations/occurrence-v2.js +139 -0
  59. package/dist/vector-cortex/replay/cut.js +123 -0
  60. package/dist/vector-cortex/replay/emit.js +58 -0
  61. package/dist/vector-cortex/replay/replay.js +260 -0
  62. package/dist/vector-cortex/replay/replayB.js +73 -0
  63. package/dist/vector-cortex/replay/types.js +52 -0
  64. package/dist/vector-cortex/resilience/breaker-core.js +359 -0
  65. package/dist/vector-cortex/resilience/breaker.js +12 -0
  66. package/dist/vector-cortex/resilience/emit.js +56 -0
  67. package/dist/vector-cortex/resilience/spool-core.js +347 -0
  68. package/dist/vector-cortex/resilience/spool.js +12 -0
  69. package/dist/vector-cortex/resilience/types.js +32 -0
  70. package/extensions/dashboard-client/dist/assets/{AreaChart-Y_X0Bbjd.js → AreaChart-CAnWomhw.js} +2 -2
  71. package/extensions/dashboard-client/dist/assets/{AreaChart-Y_X0Bbjd.js.map → AreaChart-CAnWomhw.js.map} +1 -1
  72. package/extensions/dashboard-client/dist/assets/{BarChart-B9XDb-Ob.js → BarChart-lZ2-s71k.js} +2 -2
  73. package/extensions/dashboard-client/dist/assets/{BarChart-B9XDb-Ob.js.map → BarChart-lZ2-s71k.js.map} +1 -1
  74. package/extensions/dashboard-client/dist/assets/{CacheTab-BUGVb1G1.js → CacheTab-BL2Thh66.js} +2 -2
  75. package/extensions/dashboard-client/dist/assets/{CacheTab-BUGVb1G1.js.map → CacheTab-BL2Thh66.js.map} +1 -1
  76. package/extensions/dashboard-client/dist/assets/{EventsTab-dOqLYTsy.js → EventsTab-DEh2Z9fp.js} +2 -2
  77. package/extensions/dashboard-client/dist/assets/{EventsTab-dOqLYTsy.js.map → EventsTab-DEh2Z9fp.js.map} +1 -1
  78. package/extensions/dashboard-client/dist/assets/{HealthTab-c2vezKg1.js → HealthTab-DTwKclxk.js} +2 -2
  79. package/extensions/dashboard-client/dist/assets/{HealthTab-c2vezKg1.js.map → HealthTab-DTwKclxk.js.map} +1 -1
  80. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-bQYHIcdx.js → MaintenanceTab-DDfBEdbX.js} +2 -2
  81. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-bQYHIcdx.js.map → MaintenanceTab-DDfBEdbX.js.map} +1 -1
  82. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-2L8pH6y2.js → MemoryMapTab-Dgfmg-0Q.js} +2 -2
  83. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-2L8pH6y2.js.map → MemoryMapTab-Dgfmg-0Q.js.map} +1 -1
  84. package/extensions/dashboard-client/dist/assets/{MetricsTab-CzUx1jpo.js → MetricsTab-DOcMm0Tx.js} +2 -2
  85. package/extensions/dashboard-client/dist/assets/{MetricsTab-CzUx1jpo.js.map → MetricsTab-DOcMm0Tx.js.map} +1 -1
  86. package/extensions/dashboard-client/dist/assets/{OverviewTab-eCNoaclv.js → OverviewTab-DHT5aoHg.js} +2 -2
  87. package/extensions/dashboard-client/dist/assets/{OverviewTab-eCNoaclv.js.map → OverviewTab-DHT5aoHg.js.map} +1 -1
  88. package/extensions/dashboard-client/dist/assets/{ReposTab-tb0IW3NF.js → ReposTab-CGAmtR_c.js} +2 -2
  89. package/extensions/dashboard-client/dist/assets/{ReposTab-tb0IW3NF.js.map → ReposTab-CGAmtR_c.js.map} +1 -1
  90. package/extensions/dashboard-client/dist/assets/{SessionsTab-C-IoLw5h.js → SessionsTab-BIgx6iKT.js} +2 -2
  91. package/extensions/dashboard-client/dist/assets/{SessionsTab-C-IoLw5h.js.map → SessionsTab-BIgx6iKT.js.map} +1 -1
  92. package/extensions/dashboard-client/dist/assets/{SetupTab-BvmFi8gf.js → SetupTab-ADghKuEI.js} +2 -2
  93. package/extensions/dashboard-client/dist/assets/{SetupTab-BvmFi8gf.js.map → SetupTab-ADghKuEI.js.map} +1 -1
  94. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-DBH-cOlA.js → TimeSavedCard-C33UiiOM.js} +2 -2
  95. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-DBH-cOlA.js.map → TimeSavedCard-C33UiiOM.js.map} +1 -1
  96. package/extensions/dashboard-client/dist/assets/{TurnsTab-DBMgmvnX.js → TurnsTab-DdbL9wPN.js} +2 -2
  97. package/extensions/dashboard-client/dist/assets/{TurnsTab-DBMgmvnX.js.map → TurnsTab-DdbL9wPN.js.map} +1 -1
  98. package/extensions/dashboard-client/dist/assets/VectorCortexTab-Cl1pbEdR.js +2 -0
  99. package/extensions/dashboard-client/dist/assets/VectorCortexTab-Cl1pbEdR.js.map +1 -0
  100. package/extensions/dashboard-client/dist/assets/{WikiTab-1LcQrXk3.js → WikiTab-Ch00OFKs.js} +2 -2
  101. package/extensions/dashboard-client/dist/assets/{WikiTab-1LcQrXk3.js.map → WikiTab-Ch00OFKs.js.map} +1 -1
  102. package/extensions/dashboard-client/dist/assets/{button-DtEsZTUE.js → button-ByeEdo2g.js} +2 -2
  103. package/extensions/dashboard-client/dist/assets/{button-DtEsZTUE.js.map → button-ByeEdo2g.js.map} +1 -1
  104. package/extensions/dashboard-client/dist/assets/{card-DcfTmw-4.js → card-DesoLXvc.js} +2 -2
  105. package/extensions/dashboard-client/dist/assets/{card-DcfTmw-4.js.map → card-DesoLXvc.js.map} +1 -1
  106. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-BsIxUDyw.js → generateCategoricalChart-CCAB7Iz4.js} +2 -2
  107. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-BsIxUDyw.js.map → generateCategoricalChart-CCAB7Iz4.js.map} +1 -1
  108. package/extensions/dashboard-client/dist/assets/{index-B93cewiX.js → index-B6cgyAPr.js} +34 -29
  109. package/extensions/dashboard-client/dist/assets/index-B6cgyAPr.js.map +1 -0
  110. package/extensions/dashboard-client/dist/assets/index-CfvziyBi.css +1 -0
  111. package/extensions/dashboard-client/dist/assets/{switch-Bowd7jCD.js → switch-DIZC-oe-.js} +2 -2
  112. package/extensions/dashboard-client/dist/assets/{switch-Bowd7jCD.js.map → switch-DIZC-oe-.js.map} +1 -1
  113. package/extensions/dashboard-client/dist/assets/{toggle-Bo7kJZ4u.js → toggle-CIoWaH2e.js} +2 -2
  114. package/extensions/dashboard-client/dist/assets/{toggle-Bo7kJZ4u.js.map → toggle-CIoWaH2e.js.map} +1 -1
  115. package/extensions/dashboard-client/dist/assets/{useSSE-zNhb0c3u.js → useSSE-Bv4943l1.js} +2 -2
  116. package/extensions/dashboard-client/dist/assets/{useSSE-zNhb0c3u.js.map → useSSE-Bv4943l1.js.map} +1 -1
  117. package/extensions/dashboard-client/dist/index.html +2 -2
  118. package/extensions/dashboard-client/src/App.tsx +2 -0
  119. package/extensions/dashboard-client/src/api/vector-cortex.ts +54 -0
  120. package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +272 -0
  121. package/extensions/dashboard-client/src/tabs/registry.ts +4 -1
  122. package/extensions/dashboard-client/src/types/vector-cortex.ts +84 -0
  123. package/extensions/dashboard-server/api-contracts/endpoints/registry-ext.ts +67 -0
  124. package/extensions/dashboard-server/api-contracts/endpoints/registry.ts +4 -21
  125. package/extensions/dashboard-server/api-contracts/index.ts +3 -0
  126. package/extensions/dashboard-server/api-contracts/vector-cortex.ts +136 -0
  127. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +35 -0
  128. package/extensions/dashboard-server/routes-vector-cortex.ts +332 -0
  129. package/extensions/dashboard-server/routes.ts +6 -0
  130. package/extensions/dashboard-server/server.ts +8 -0
  131. package/extensions/mega-events/context-handler.ts +16 -0
  132. package/extensions/mega-runtime/vector-cortex-ledger.ts +165 -0
  133. package/extensions/mega-runtime/vector-cortex-safety.ts +181 -0
  134. package/package.json +2 -1
  135. package/src/config/vector-cortex.ts +87 -0
  136. package/src/config.ts +27 -0
  137. package/src/vector-cortex/eval/annotations.ts +70 -0
  138. package/src/vector-cortex/eval/metrics.ts +113 -0
  139. package/src/vector-cortex/eval/observer.ts +86 -0
  140. package/src/vector-cortex/eval/persist.ts +73 -0
  141. package/src/vector-cortex/eval/reader.ts +45 -0
  142. package/src/vector-cortex/eval/types.ts +89 -0
  143. package/src/vector-cortex/ledger/adapter.ts +71 -0
  144. package/src/vector-cortex/ledger/compat-journal.ts +234 -0
  145. package/src/vector-cortex/ledger/emit.ts +86 -0
  146. package/src/vector-cortex/ledger/event-codec.ts +79 -0
  147. package/src/vector-cortex/ledger/event-codecB.ts +87 -0
  148. package/src/vector-cortex/ledger/sqlite.ts +265 -0
  149. package/src/vector-cortex/ledger/store.ts +380 -0
  150. package/src/vector-cortex/ledger/types.ts +141 -0
  151. package/src/vector-cortex/ledger/validator.ts +124 -0
  152. package/src/vector-cortex/migrations/effective-cut-v2.ts +149 -0
  153. package/src/vector-cortex/migrations/occurrence-v2.ts +192 -0
  154. package/src/vector-cortex/replay/cut.ts +169 -0
  155. package/src/vector-cortex/replay/emit.ts +89 -0
  156. package/src/vector-cortex/replay/replay.ts +308 -0
  157. package/src/vector-cortex/replay/replayB.ts +97 -0
  158. package/src/vector-cortex/replay/types.ts +145 -0
  159. package/src/vector-cortex/resilience/breaker-core.ts +444 -0
  160. package/src/vector-cortex/resilience/breaker.ts +22 -0
  161. package/src/vector-cortex/resilience/emit.ts +99 -0
  162. package/src/vector-cortex/resilience/spool-core.ts +411 -0
  163. package/src/vector-cortex/resilience/spool.ts +22 -0
  164. package/src/vector-cortex/resilience/types.ts +158 -0
  165. package/extensions/dashboard-client/dist/assets/index-B93cewiX.js.map +0 -1
  166. package/extensions/dashboard-client/dist/assets/index-C_tCbOI0.css +0 -1
@@ -0,0 +1,33 @@
1
+ /**
2
+ * vector-cortex/eval/types.ts — MetricEventV1 / AnnotationV1 contract types
3
+ * and the registered EVAL conformance ID range (EVAL-001..010).
4
+ *
5
+ * Owned by VC0A (baseline observability). Consumes reviewer-accepted predecessor
6
+ * contracts and common contracts only. These are pure type/schema definitions —
7
+ * no runtime network, no side effects (PREVENT-PI-004 / PREVENT-011).
8
+ */
9
+ /** Units the evaluator understands. Unknown units reject as EVAL_UNIT_UNKNOWN. */
10
+ export const UNITS = ["ms", "bytes", "count", "ratio"];
11
+ /**
12
+ * Histogram bucket edges for latency, in milliseconds, inclusive on both
13
+ * boundaries (EVAL-BUCKET-001): values at exactly 1ms and 250ms land in
14
+ * those buckets. Overflow (value > largest edge) is kept separate.
15
+ */
16
+ export const LATENCY_BUCKETS = [1, 5, 10, 25, 50, 100, 250];
17
+ /**
18
+ * Registered EVAL conformance ID range (EVAL-001..010). The acceptance test
19
+ * reads these rows from the v2 manifest and asserts their expected bytes/results.
20
+ * Registered BEFORE evaluation logic so the manifest/fixture seam is stable.
21
+ */
22
+ export const EVAL_IDS = [
23
+ "EVAL-001",
24
+ "EVAL-002",
25
+ "EVAL-003",
26
+ "EVAL-004",
27
+ "EVAL-005",
28
+ "EVAL-006",
29
+ "EVAL-007",
30
+ "EVAL-008",
31
+ "EVAL-009",
32
+ "EVAL-010",
33
+ ];
@@ -0,0 +1,55 @@
1
+ /**
2
+ * vector-cortex/ledger/adapter.ts — EventV2 ledger adapter (VC1A, task 5).
3
+ *
4
+ * A thin, pi-agnostic adapter combining the Mode-A codec, the canonical
5
+ * validator, and the VC1A emit seam so `vector_cortex_event_decoded` and
6
+ * `vector_cortex_event_validation_failed` are emitted by REAL code paths (not a
7
+ * hand-invoked reporter), gated by `VC1A_ENABLED()` inside `createLedgerReporter`.
8
+ *
9
+ * The live producer (wiring this into `extensions/mega-compact.ts` /
10
+ * `src/engine.ts`) is deferred to VC1C/VC1B (mirroring VC0B-I08); this sprint
11
+ * ships the seam + adapter as the clean single surface, with the acceptance
12
+ * aggregator exercising the real gated-emission path.
13
+ *
14
+ * Pure orchestration — no storage, no network, no side effects (PREVENT-PI-004).
15
+ */
16
+ import { createEventCodec } from "./event-codec.js";
17
+ import { validateEvents } from "./validator.js";
18
+ import { createLedgerReporter } from "./emit.js";
19
+ /** Build the ledger adapter over an injected emit callback (optional). */
20
+ export function createLedgerAdapter(emit) {
21
+ const codec = createEventCodec();
22
+ const reporter = createLedgerReporter(emit);
23
+ return {
24
+ encode: (input) => codec.encode(input),
25
+ decode(event) {
26
+ const bytes = codec.decode(event);
27
+ reporter.eventDecoded({
28
+ session: event.sessionId,
29
+ seq: event.seq.toString(),
30
+ eventId: event.eventId,
31
+ bytes: bytes.length,
32
+ utf8Valid: event.utf8.valid,
33
+ });
34
+ return bytes;
35
+ },
36
+ validate(events) {
37
+ const result = validateEvents(events);
38
+ if (!result.ok) {
39
+ // Emit one event per failing occurrence with its REAL locator — a
40
+ // consumer of the feed can identify WHICH event failed (VC1A-I05).
41
+ for (const issue of result.issues) {
42
+ reporter.validationFailed({
43
+ session: issue.sessionId,
44
+ seq: issue.seq.toString(),
45
+ eventId: issue.eventId,
46
+ code: issue.code,
47
+ });
48
+ }
49
+ }
50
+ return result;
51
+ },
52
+ codec,
53
+ reporter,
54
+ };
55
+ }
@@ -0,0 +1,206 @@
1
+ /**
2
+ * vector-cortex/ledger/compat-journal.ts — CompatJournalV1 (VC1B, task 4).
3
+ *
4
+ * The downgrade-safety journal. From v2 activation every accepted v2 append
5
+ * atomically appends a record holding the original bytes, IDs, and a legacy
6
+ * projection or an explicit `unrepresentable` marker. The journal tracks its own
7
+ * lifetime state (prepared → copied → validated → switched) so the M2
8
+ * downgrade is resumable: a crash mid-journal never corrupts v2, and a restart
9
+ * resumes idempotently (the unique failure-injection contract).
10
+ *
11
+ * `active()` is true once ANY v2 append has been journaled — an old binary may
12
+ * only open the exported legacy copy, never v2 directly, once the journal is
13
+ * active (CONTRACTS §Store). Unrepresentable rows (e.g. invalid UTF-8 bytes have
14
+ * no lossless legacy projection) are listed on prepare and carried through the
15
+ * copy/validate/switch lifecycle.
16
+ *
17
+ * Durable in the same isolated occurrence-v2 SQLite DB as the ledger rows
18
+ * (append-only provenance: journal records are never UPDATE'd; only the
19
+ * singleton state row advances phase). PREVENT-002 parameterized; PREVENT-011
20
+ * no `any`; PREVENT-PI-004 local filesystem only; no console.log.
21
+ */
22
+ import { createHash } from "node:crypto";
23
+ /** Downgrade-phase validation failure codes (registered as conformance rows). */
24
+ export const MIG_DOWN_FAIL = {
25
+ NOT_ACTIVE: "MIG_DOWN_NOT_ACTIVE",
26
+ PHASE_UNREACHED: "MIG_DOWN_PHASE_UNREACHED",
27
+ SEQ_REGRESSION: "MIG_DOWN_SEQ_REGRESSION",
28
+ DIGEST_MISMATCH: "MIG_DOWN_DIGEST_MISMATCH",
29
+ SWITCH_UNSAFE: "MIG_DOWN_SWITCH_UNSAFE",
30
+ };
31
+ const SCHEMA = `
32
+ CREATE TABLE IF NOT EXISTS compat_journal_v1 (
33
+ session TEXT NOT NULL,
34
+ seq INTEGER NOT NULL,
35
+ event_id TEXT NOT NULL,
36
+ digest TEXT NOT NULL,
37
+ kind TEXT NOT NULL,
38
+ legacy_projection TEXT,
39
+ unrepresentable INTEGER NOT NULL DEFAULT 0,
40
+ PRIMARY KEY (session, seq)
41
+ ) STRICT;
42
+
43
+ CREATE TABLE IF NOT EXISTS compat_journal_state (
44
+ id INTEGER PRIMARY KEY CHECK (id = 1),
45
+ phase TEXT NOT NULL
46
+ ) STRICT;
47
+ `;
48
+ /** Ensure the journal tables exist and seed the singleton phase row. */
49
+ export function initCompatJournal(db) {
50
+ db.exec(SCHEMA);
51
+ db.prepare(`INSERT OR IGNORE INTO compat_journal_state (id, phase) VALUES (1, 'prepared')`).run();
52
+ }
53
+ function phaseOf(db) {
54
+ const row = db
55
+ .prepare(`SELECT phase FROM compat_journal_state WHERE id = 1`)
56
+ .get();
57
+ return row?.phase ?? "prepared";
58
+ }
59
+ /** Read the durable journal phase (for the M2 migration host seam). */
60
+ export function journalPhase(db) {
61
+ return phaseOf(db);
62
+ }
63
+ function setPhase(db, phase) {
64
+ db.prepare(`UPDATE compat_journal_state SET phase = @phase WHERE id = 1`).run({
65
+ "@phase": phase,
66
+ });
67
+ }
68
+ /**
69
+ * Verify a recorded digest by RECOMPUTING sha256 over the base64 source embedded
70
+ * in the legacy projection and comparing it to the recorded digest (Q03: real
71
+ * digest parity, not a `sha256:` prefix check). Returns false when the projection
72
+ * is absent or malformed, or the digest does not match the recomputed hash.
73
+ */
74
+ export function verifyLegacyDigest(digest, legacyProjection) {
75
+ if (legacyProjection === null)
76
+ return false;
77
+ let rec;
78
+ try {
79
+ rec = JSON.parse(legacyProjection);
80
+ }
81
+ catch {
82
+ return false;
83
+ }
84
+ const source = rec.source;
85
+ if (typeof source !== "string")
86
+ return false;
87
+ const recomputed = `sha256:${createHash("sha256").update(Buffer.from(source, "base64")).digest("hex")}`;
88
+ return recomputed === digest;
89
+ }
90
+ /**
91
+ * Build the CompatJournalV1 over the isolated occurrence-v2 DB. Call
92
+ * `initCompatJournal` once before use. All record writes are append-only and
93
+ * best-effort/non-fatal so the agent loop is never broken (PREVENT-PI-004).
94
+ */
95
+ export function createCompatJournal(db) {
96
+ return {
97
+ active() {
98
+ try {
99
+ const row = db
100
+ .prepare(`SELECT 1 AS hit FROM compat_journal_v1 LIMIT 1`)
101
+ .get();
102
+ return row !== undefined;
103
+ }
104
+ catch {
105
+ return false;
106
+ }
107
+ },
108
+ record(input) {
109
+ const { occurrence, legacyProjection } = input;
110
+ try {
111
+ const unrep = legacyProjection === null;
112
+ db.prepare(`INSERT INTO compat_journal_v1
113
+ (session, seq, event_id, digest, kind, legacy_projection, unrepresentable)
114
+ VALUES (@session, @seq, @event_id, @digest, @kind, @legacy, @unrep)
115
+ ON CONFLICT(session, seq) DO NOTHING`).run({
116
+ "@session": occurrence.session,
117
+ "@seq": Number(occurrence.seq),
118
+ "@event_id": occurrence.eventId,
119
+ "@digest": occurrence.digest,
120
+ "@kind": occurrence.kind,
121
+ // Q06: an unrepresentable row stores NO projection (legacy_projection
122
+ // stays NULL); we never write a base64 body we cannot round-trip.
123
+ "@legacy": legacyProjection,
124
+ "@unrep": unrep ? 1 : 0,
125
+ });
126
+ }
127
+ catch {
128
+ /* non-fatal journal write — never break the agent loop */
129
+ }
130
+ },
131
+ isUnrepresentable(eventId, digest) {
132
+ try {
133
+ const row = db
134
+ .prepare(`SELECT unrepresentable AS u FROM compat_journal_v1
135
+ WHERE event_id = @event_id AND digest = @digest
136
+ ORDER BY seq DESC LIMIT 1`)
137
+ .get({ "@event_id": eventId, "@digest": digest });
138
+ return row?.u === 1;
139
+ }
140
+ catch {
141
+ return false;
142
+ }
143
+ },
144
+ prepare() {
145
+ const rows = db
146
+ .prepare(`SELECT session, seq, event_id, digest, kind, legacy_projection, unrepresentable
147
+ FROM compat_journal_v1 ORDER BY session ASC, seq ASC`)
148
+ .all();
149
+ setPhase(db, "prepared");
150
+ return rows.filter((r) => r.unrepresentable === 1).map((r) => r.event_id);
151
+ },
152
+ copied() {
153
+ setPhase(db, "copied");
154
+ },
155
+ validate() {
156
+ const codes = [];
157
+ const phase = phaseOf(db);
158
+ if (!isRepresentableState(phase, "copied"))
159
+ codes.push(MIG_DOWN_FAIL.PHASE_UNREACHED);
160
+ if (!this.active())
161
+ codes.push(MIG_DOWN_FAIL.NOT_ACTIVE);
162
+ // Seq must be strictly increasing within a session (no regression).
163
+ const prev = new Map();
164
+ const rows = db
165
+ .prepare(`SELECT session, seq FROM compat_journal_v1 ORDER BY session ASC, seq ASC`)
166
+ .all();
167
+ for (const r of rows) {
168
+ const last = prev.get(r.session);
169
+ if (last !== undefined && r.seq <= last)
170
+ codes.push(MIG_DOWN_FAIL.SEQ_REGRESSION);
171
+ prev.set(r.session, r.seq);
172
+ }
173
+ // Digests must verify: recompute sha256 over the stored source and compare
174
+ // to the recorded digest (Q03 — real parity, not just a `sha256:` prefix).
175
+ for (const r of db
176
+ .prepare(`SELECT digest, legacy_projection FROM compat_journal_v1 WHERE unrepresentable = 0`)
177
+ .all()) {
178
+ if (r.legacy_projection === null)
179
+ continue;
180
+ if (!verifyLegacyDigest(r.digest, r.legacy_projection)) {
181
+ codes.push(MIG_DOWN_FAIL.DIGEST_MISMATCH);
182
+ }
183
+ }
184
+ const ok = codes.length === 0;
185
+ if (ok)
186
+ setPhase(db, "validated");
187
+ return { ok, codes: dedupe(codes) };
188
+ },
189
+ switched() {
190
+ if (this.active())
191
+ setPhase(db, "switched");
192
+ },
193
+ };
194
+ }
195
+ /** A phase has reached at least `required` in the prepare→…→switched order. */
196
+ function isRepresentableState(phase, required) {
197
+ const order = ["prepared", "copied", "validated", "switched"];
198
+ return order.indexOf(phase) >= order.indexOf(required);
199
+ }
200
+ function dedupe(codes) {
201
+ const out = [];
202
+ for (const c of codes)
203
+ if (!out.includes(c))
204
+ out.push(c);
205
+ return out;
206
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * vector-cortex/ledger/emit.ts — VC1A ledger emit seam.
3
+ *
4
+ * The single structured-event surface for the EventV2 ledger path. It mirrors
5
+ * the VC0B replay emit seam (`src/vector-cortex/replay/emit.ts`): the same
6
+ * non-fatal, structured-JSON `ts`+`event` contract (`src/log.ts` LogEntry),
7
+ * delivered through an injected emit callback so it stays pi-agnostic,
8
+ * deterministically testable, and network-free (PREVENT-PI-004).
9
+ *
10
+ * Carries only the two EventV2 events:
11
+ * - vector_cortex_event_decoded (a V2 occurrence was byte-decoded)
12
+ * - vector_cortex_event_validation_failed (a V2 conformity check failed)
13
+ *
14
+ * Flag gating: this is VC1A's single real consumer of `VC1A_ENABLED()`. When the
15
+ * flag is OFF the reporter emits NOTHING (zero observability writes) — the
16
+ * VC1A mode-C byte-identical behavior — while the codec/validator still operate.
17
+ * The flag is read per-call so a live toggle takes effect immediately.
18
+ */
19
+ import { VC1A_ENABLED } from "../../config/vector-cortex.js";
20
+ function safe(fn) {
21
+ return (event, fields) => {
22
+ try {
23
+ fn(event, fields);
24
+ }
25
+ catch {
26
+ /* non-fatal observability — never break the agent loop */
27
+ }
28
+ };
29
+ }
30
+ /**
31
+ * Build a typed reporter over an injected emit callback. Emission is gated on
32
+ * `VC1A_ENABLED()` — flag OFF means zero ledger observability writes (mode-C
33
+ * parity). `emit` is optional: absent (or flag off) degrades to a no-op.
34
+ */
35
+ export function createLedgerReporter(emit) {
36
+ const fire = safe((event, fields) => {
37
+ if (VC1A_ENABLED())
38
+ emit?.(event, fields);
39
+ });
40
+ return {
41
+ eventDecoded(fields) {
42
+ fire("vector_cortex_event_decoded", fields);
43
+ },
44
+ validationFailed(fields) {
45
+ fire("vector_cortex_event_validation_failed", fields);
46
+ },
47
+ };
48
+ }
49
+ /** Export the event names for consumers that match on them. */
50
+ export const LEDGER_EVENTS = [
51
+ "vector_cortex_event_decoded",
52
+ "vector_cortex_event_validation_failed",
53
+ ];
@@ -0,0 +1,72 @@
1
+ /**
2
+ * vector-cortex/ledger/event-codec.ts — EventV2 byte-authority codec, Mode A
3
+ * (VC1A, TRIAD_RESILIENCE).
4
+ *
5
+ * A retains `originalBytes` + a SHA-256 `bytesDigest` and classifies strict
6
+ * UTF-8 (success) vs invalid-byte content WITHOUT replacement decoding:
7
+ * `TextDecoder(..., {fatal:true})` either yields text or throws — invalid input
8
+ * is represented only as `{valid:false, base64}` and is NEVER lossy-replaced.
9
+ * `canonicalNfc` is computed ONLY as a derived field for valid UTF-8 and is
10
+ * NEVER used for identity, digest, or byte reconstruction — `decode` returns the
11
+ * authoritative `originalBytes` exactly.
12
+ *
13
+ * Mode B (`./event-codecB.ts`) is a genuinely independent implementation of the
14
+ * same byte rule; A and B share no subroutine and must agree byte-for-byte.
15
+ *
16
+ * Pure/deterministic — no console, no network, no side effects (PREVENT-PI-004).
17
+ */
18
+ import nodeCrypto from "node:crypto";
19
+ /** Mode-A internal SHA-256 helper (NOT shared with mode B). */
20
+ function digestSha256A(bytes) {
21
+ const hex = nodeCrypto.createHash("sha256").update(bytes).digest("hex");
22
+ return `sha256:${hex}`;
23
+ }
24
+ /** Mode-A internal base64 (NOT shared with mode B). */
25
+ function base64EncodeA(bytes) {
26
+ return Buffer.from(bytes).toString("base64");
27
+ }
28
+ /**
29
+ * Strict UTF-8 classification with FATAL decoding (never replaces invalid bytes
30
+ * with U+FFFD). Returns the decoded text, or `{valid:false, base64}`.
31
+ */
32
+ export function classifyUtf8(bytes) {
33
+ try {
34
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
35
+ return { valid: true, text };
36
+ }
37
+ catch {
38
+ return { valid: false, base64: base64EncodeA(bytes) };
39
+ }
40
+ }
41
+ /** Build the Mode-A byte-authority codec. */
42
+ export function createEventCodec() {
43
+ return {
44
+ encode(input) {
45
+ const bytes = input.bytes;
46
+ const bytesDigest = digestSha256A(bytes);
47
+ const utf8 = classifyUtf8(bytes);
48
+ const event = {
49
+ schema: "event-v2",
50
+ sessionId: input.sessionId,
51
+ seq: input.seq,
52
+ eventId: input.eventId,
53
+ role: input.role,
54
+ kind: input.kind,
55
+ originalBytes: bytes,
56
+ bytesDigest,
57
+ utf8,
58
+ occurredAtMs: input.occurredAtMs,
59
+ };
60
+ if (input.toolCallId !== undefined)
61
+ event.toolCallId = input.toolCallId;
62
+ // canonicalNfc is a DERIVED search key — valid UTF-8 only, never identity.
63
+ if (utf8.valid)
64
+ event.canonicalNfc = utf8.text.normalize("NFC");
65
+ return event;
66
+ },
67
+ decode(event) {
68
+ return event.originalBytes;
69
+ },
70
+ classifyUtf8,
71
+ };
72
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * vector-cortex/ledger/event-codecB.ts — EventV2 byte-authority codec, Mode B
3
+ * (VC1A, TRIAD_RESILIENCE).
4
+ *
5
+ * B is a GENUINELY independent raw-byte record implementation of the SAME byte
6
+ * rule as Mode A (`./event-codec.ts`). It shares NO subroutine with A: its own
7
+ * SHA-256 helper, its own base64 helper, its own strict-UTF-8 classifier, and
8
+ * its own raw record — the lesson from VC0B-I09 (a mode-B tested against code
9
+ * identical to A is a tautology). Because the digest of given bytes is a pure
10
+ * function, two independent implementations MUST agree byte-for-byte; the
11
+ * acceptance test asserts `bytesDigest` parity across the fixture corpus.
12
+ *
13
+ * B retains `originalBytes` + `bytesDigest` and classifies strict UTF-8 with no
14
+ * lossy replacement; `canonicalNfc` is derived for valid UTF-8 but never used
15
+ * for identity, digest, or reconstruction.
16
+ *
17
+ * Pure/deterministic — no console, no network, no side effects (PREVENT-PI-004).
18
+ */
19
+ import nodeCrypto from "node:crypto";
20
+ /** Mode-B internal SHA-256 helper (independent of A's — no shared subroutine). */
21
+ function digestSha256B(bytes) {
22
+ const d = nodeCrypto.createHash("sha256");
23
+ d.update(bytes);
24
+ const hex = d.digest("hex");
25
+ return `sha256:${hex}`;
26
+ }
27
+ /** Mode-B internal base64 (independent of A's). */
28
+ function base64EncodeB(bytes) {
29
+ let out = "";
30
+ const buf = Buffer.from(bytes);
31
+ for (let i = 0; i < buf.length; i += 3) {
32
+ const b0 = buf[i];
33
+ const b1 = i + 1 < buf.length ? buf[i + 1] : -1;
34
+ const b2 = i + 2 < buf.length ? buf[i + 2] : -1;
35
+ out += CHARS[(b0 >> 2) & 63];
36
+ out += CHARS[((b0 & 3) << 4) | (b1 < 0 ? 0 : (b1 >> 4) & 15)];
37
+ if (b1 >= 0)
38
+ out += CHARS[((b1 & 15) << 2) | (b2 < 0 ? 0 : (b2 >> 6) & 3)];
39
+ if (b2 >= 0)
40
+ out += CHARS[b2 & 63];
41
+ }
42
+ // Padding (independent reconstruction of standard base64).
43
+ const pad = (3 - (buf.length % 3)) % 3;
44
+ for (let i = 0; i < pad; i++)
45
+ out += "=";
46
+ return out;
47
+ }
48
+ const CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49
+ /** Mode-B strict UTF-8 classifier (fatal decode; no replacement). */
50
+ function classifyUtf8B(bytes) {
51
+ try {
52
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
53
+ return { valid: true, text };
54
+ }
55
+ catch {
56
+ return { valid: false, base64: base64EncodeB(bytes) };
57
+ }
58
+ }
59
+ /** Independently digest + classify raw bytes into a B record. */
60
+ export function recordRawBytesB(bytes) {
61
+ const bytesDigest = digestSha256B(bytes);
62
+ const utf8 = classifyUtf8B(bytes);
63
+ const record = { originalBytes: bytes, bytesDigest, utf8 };
64
+ if (utf8.valid) {
65
+ return { originalBytes: bytes, bytesDigest, utf8, canonicalNfc: utf8.text.normalize("NFC") };
66
+ }
67
+ return record;
68
+ }
69
+ /**
70
+ * Independent digest check: recompute the SHA-256 over an EventV2's
71
+ * `originalBytes` via B's OWN algorithm and compare to the stored digest.
72
+ * Returns the recomputed digest string (parity-checked by the caller).
73
+ */
74
+ export function digestCheckB(event) {
75
+ return digestSha256B(event.originalBytes);
76
+ }