pi-mega-compact 0.16.2 → 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 (168) 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/httpEmbedder.js +136 -1
  15. package/dist/src/vector-cortex/eval/annotations.js +41 -0
  16. package/dist/src/vector-cortex/eval/metrics.js +86 -0
  17. package/dist/src/vector-cortex/eval/observer.js +61 -0
  18. package/dist/src/vector-cortex/eval/persist.js +67 -0
  19. package/dist/src/vector-cortex/eval/reader.js +29 -0
  20. package/dist/src/vector-cortex/eval/types.js +33 -0
  21. package/dist/src/vector-cortex/ledger/adapter.js +55 -0
  22. package/dist/src/vector-cortex/ledger/compat-journal.js +206 -0
  23. package/dist/src/vector-cortex/ledger/emit.js +53 -0
  24. package/dist/src/vector-cortex/ledger/event-codec.js +72 -0
  25. package/dist/src/vector-cortex/ledger/event-codecB.js +76 -0
  26. package/dist/src/vector-cortex/ledger/sqlite.js +200 -0
  27. package/dist/src/vector-cortex/ledger/store.js +184 -0
  28. package/dist/src/vector-cortex/ledger/types.js +52 -0
  29. package/dist/src/vector-cortex/ledger/validator.js +117 -0
  30. package/dist/src/vector-cortex/migrations/effective-cut-v2.js +103 -0
  31. package/dist/src/vector-cortex/migrations/occurrence-v2.js +139 -0
  32. package/dist/src/vector-cortex/replay/cut.js +123 -0
  33. package/dist/src/vector-cortex/replay/emit.js +58 -0
  34. package/dist/src/vector-cortex/replay/replay.js +260 -0
  35. package/dist/src/vector-cortex/replay/replayB.js +73 -0
  36. package/dist/src/vector-cortex/replay/types.js +52 -0
  37. package/dist/src/vector-cortex/resilience/breaker-core.js +359 -0
  38. package/dist/src/vector-cortex/resilience/breaker.js +12 -0
  39. package/dist/src/vector-cortex/resilience/emit.js +56 -0
  40. package/dist/src/vector-cortex/resilience/spool-core.js +347 -0
  41. package/dist/src/vector-cortex/resilience/spool.js +12 -0
  42. package/dist/src/vector-cortex/resilience/types.js +32 -0
  43. package/dist/vector-cortex/eval/annotations.js +41 -0
  44. package/dist/vector-cortex/eval/metrics.js +86 -0
  45. package/dist/vector-cortex/eval/observer.js +61 -0
  46. package/dist/vector-cortex/eval/persist.js +67 -0
  47. package/dist/vector-cortex/eval/reader.js +29 -0
  48. package/dist/vector-cortex/eval/types.js +33 -0
  49. package/dist/vector-cortex/ledger/adapter.js +55 -0
  50. package/dist/vector-cortex/ledger/compat-journal.js +206 -0
  51. package/dist/vector-cortex/ledger/emit.js +53 -0
  52. package/dist/vector-cortex/ledger/event-codec.js +72 -0
  53. package/dist/vector-cortex/ledger/event-codecB.js +76 -0
  54. package/dist/vector-cortex/ledger/sqlite.js +200 -0
  55. package/dist/vector-cortex/ledger/store.js +184 -0
  56. package/dist/vector-cortex/ledger/types.js +52 -0
  57. package/dist/vector-cortex/ledger/validator.js +117 -0
  58. package/dist/vector-cortex/migrations/effective-cut-v2.js +103 -0
  59. package/dist/vector-cortex/migrations/occurrence-v2.js +139 -0
  60. package/dist/vector-cortex/replay/cut.js +123 -0
  61. package/dist/vector-cortex/replay/emit.js +58 -0
  62. package/dist/vector-cortex/replay/replay.js +260 -0
  63. package/dist/vector-cortex/replay/replayB.js +73 -0
  64. package/dist/vector-cortex/replay/types.js +52 -0
  65. package/dist/vector-cortex/resilience/breaker-core.js +359 -0
  66. package/dist/vector-cortex/resilience/breaker.js +12 -0
  67. package/dist/vector-cortex/resilience/emit.js +56 -0
  68. package/dist/vector-cortex/resilience/spool-core.js +347 -0
  69. package/dist/vector-cortex/resilience/spool.js +12 -0
  70. package/dist/vector-cortex/resilience/types.js +32 -0
  71. package/extensions/dashboard-client/dist/assets/{AreaChart-Y_X0Bbjd.js → AreaChart-CAnWomhw.js} +2 -2
  72. package/extensions/dashboard-client/dist/assets/{AreaChart-Y_X0Bbjd.js.map → AreaChart-CAnWomhw.js.map} +1 -1
  73. package/extensions/dashboard-client/dist/assets/{BarChart-B9XDb-Ob.js → BarChart-lZ2-s71k.js} +2 -2
  74. package/extensions/dashboard-client/dist/assets/{BarChart-B9XDb-Ob.js.map → BarChart-lZ2-s71k.js.map} +1 -1
  75. package/extensions/dashboard-client/dist/assets/{CacheTab-BUGVb1G1.js → CacheTab-BL2Thh66.js} +2 -2
  76. package/extensions/dashboard-client/dist/assets/{CacheTab-BUGVb1G1.js.map → CacheTab-BL2Thh66.js.map} +1 -1
  77. package/extensions/dashboard-client/dist/assets/{EventsTab-dOqLYTsy.js → EventsTab-DEh2Z9fp.js} +2 -2
  78. package/extensions/dashboard-client/dist/assets/{EventsTab-dOqLYTsy.js.map → EventsTab-DEh2Z9fp.js.map} +1 -1
  79. package/extensions/dashboard-client/dist/assets/{HealthTab-c2vezKg1.js → HealthTab-DTwKclxk.js} +2 -2
  80. package/extensions/dashboard-client/dist/assets/{HealthTab-c2vezKg1.js.map → HealthTab-DTwKclxk.js.map} +1 -1
  81. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-bQYHIcdx.js → MaintenanceTab-DDfBEdbX.js} +2 -2
  82. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-bQYHIcdx.js.map → MaintenanceTab-DDfBEdbX.js.map} +1 -1
  83. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-2L8pH6y2.js → MemoryMapTab-Dgfmg-0Q.js} +2 -2
  84. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-2L8pH6y2.js.map → MemoryMapTab-Dgfmg-0Q.js.map} +1 -1
  85. package/extensions/dashboard-client/dist/assets/{MetricsTab-CzUx1jpo.js → MetricsTab-DOcMm0Tx.js} +2 -2
  86. package/extensions/dashboard-client/dist/assets/{MetricsTab-CzUx1jpo.js.map → MetricsTab-DOcMm0Tx.js.map} +1 -1
  87. package/extensions/dashboard-client/dist/assets/{OverviewTab-eCNoaclv.js → OverviewTab-DHT5aoHg.js} +2 -2
  88. package/extensions/dashboard-client/dist/assets/{OverviewTab-eCNoaclv.js.map → OverviewTab-DHT5aoHg.js.map} +1 -1
  89. package/extensions/dashboard-client/dist/assets/{ReposTab-tb0IW3NF.js → ReposTab-CGAmtR_c.js} +2 -2
  90. package/extensions/dashboard-client/dist/assets/{ReposTab-tb0IW3NF.js.map → ReposTab-CGAmtR_c.js.map} +1 -1
  91. package/extensions/dashboard-client/dist/assets/{SessionsTab-C-IoLw5h.js → SessionsTab-BIgx6iKT.js} +2 -2
  92. package/extensions/dashboard-client/dist/assets/{SessionsTab-C-IoLw5h.js.map → SessionsTab-BIgx6iKT.js.map} +1 -1
  93. package/extensions/dashboard-client/dist/assets/{SetupTab-BvmFi8gf.js → SetupTab-ADghKuEI.js} +2 -2
  94. package/extensions/dashboard-client/dist/assets/{SetupTab-BvmFi8gf.js.map → SetupTab-ADghKuEI.js.map} +1 -1
  95. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-DBH-cOlA.js → TimeSavedCard-C33UiiOM.js} +2 -2
  96. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-DBH-cOlA.js.map → TimeSavedCard-C33UiiOM.js.map} +1 -1
  97. package/extensions/dashboard-client/dist/assets/{TurnsTab-DBMgmvnX.js → TurnsTab-DdbL9wPN.js} +2 -2
  98. package/extensions/dashboard-client/dist/assets/{TurnsTab-DBMgmvnX.js.map → TurnsTab-DdbL9wPN.js.map} +1 -1
  99. package/extensions/dashboard-client/dist/assets/VectorCortexTab-Cl1pbEdR.js +2 -0
  100. package/extensions/dashboard-client/dist/assets/VectorCortexTab-Cl1pbEdR.js.map +1 -0
  101. package/extensions/dashboard-client/dist/assets/{WikiTab-1LcQrXk3.js → WikiTab-Ch00OFKs.js} +2 -2
  102. package/extensions/dashboard-client/dist/assets/{WikiTab-1LcQrXk3.js.map → WikiTab-Ch00OFKs.js.map} +1 -1
  103. package/extensions/dashboard-client/dist/assets/{button-DtEsZTUE.js → button-ByeEdo2g.js} +2 -2
  104. package/extensions/dashboard-client/dist/assets/{button-DtEsZTUE.js.map → button-ByeEdo2g.js.map} +1 -1
  105. package/extensions/dashboard-client/dist/assets/{card-DcfTmw-4.js → card-DesoLXvc.js} +2 -2
  106. package/extensions/dashboard-client/dist/assets/{card-DcfTmw-4.js.map → card-DesoLXvc.js.map} +1 -1
  107. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-BsIxUDyw.js → generateCategoricalChart-CCAB7Iz4.js} +2 -2
  108. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-BsIxUDyw.js.map → generateCategoricalChart-CCAB7Iz4.js.map} +1 -1
  109. package/extensions/dashboard-client/dist/assets/{index-B93cewiX.js → index-B6cgyAPr.js} +34 -29
  110. package/extensions/dashboard-client/dist/assets/index-B6cgyAPr.js.map +1 -0
  111. package/extensions/dashboard-client/dist/assets/index-CfvziyBi.css +1 -0
  112. package/extensions/dashboard-client/dist/assets/{switch-Bowd7jCD.js → switch-DIZC-oe-.js} +2 -2
  113. package/extensions/dashboard-client/dist/assets/{switch-Bowd7jCD.js.map → switch-DIZC-oe-.js.map} +1 -1
  114. package/extensions/dashboard-client/dist/assets/{toggle-Bo7kJZ4u.js → toggle-CIoWaH2e.js} +2 -2
  115. package/extensions/dashboard-client/dist/assets/{toggle-Bo7kJZ4u.js.map → toggle-CIoWaH2e.js.map} +1 -1
  116. package/extensions/dashboard-client/dist/assets/{useSSE-zNhb0c3u.js → useSSE-Bv4943l1.js} +2 -2
  117. package/extensions/dashboard-client/dist/assets/{useSSE-zNhb0c3u.js.map → useSSE-Bv4943l1.js.map} +1 -1
  118. package/extensions/dashboard-client/dist/index.html +2 -2
  119. package/extensions/dashboard-client/src/App.tsx +2 -0
  120. package/extensions/dashboard-client/src/api/vector-cortex.ts +54 -0
  121. package/extensions/dashboard-client/src/tabs/VectorCortexTab.tsx +272 -0
  122. package/extensions/dashboard-client/src/tabs/registry.ts +4 -1
  123. package/extensions/dashboard-client/src/types/vector-cortex.ts +84 -0
  124. package/extensions/dashboard-server/api-contracts/endpoints/registry-ext.ts +67 -0
  125. package/extensions/dashboard-server/api-contracts/endpoints/registry.ts +4 -21
  126. package/extensions/dashboard-server/api-contracts/index.ts +3 -0
  127. package/extensions/dashboard-server/api-contracts/vector-cortex.ts +136 -0
  128. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +35 -0
  129. package/extensions/dashboard-server/routes-vector-cortex.ts +332 -0
  130. package/extensions/dashboard-server/routes.ts +6 -0
  131. package/extensions/dashboard-server/server.ts +8 -0
  132. package/extensions/mega-events/context-handler.ts +16 -0
  133. package/extensions/mega-runtime/vector-cortex-ledger.ts +165 -0
  134. package/extensions/mega-runtime/vector-cortex-safety.ts +181 -0
  135. package/package.json +2 -1
  136. package/src/config/vector-cortex.ts +87 -0
  137. package/src/config.ts +27 -0
  138. package/src/httpEmbedder.ts +134 -1
  139. package/src/vector-cortex/eval/annotations.ts +70 -0
  140. package/src/vector-cortex/eval/metrics.ts +113 -0
  141. package/src/vector-cortex/eval/observer.ts +86 -0
  142. package/src/vector-cortex/eval/persist.ts +73 -0
  143. package/src/vector-cortex/eval/reader.ts +45 -0
  144. package/src/vector-cortex/eval/types.ts +89 -0
  145. package/src/vector-cortex/ledger/adapter.ts +71 -0
  146. package/src/vector-cortex/ledger/compat-journal.ts +234 -0
  147. package/src/vector-cortex/ledger/emit.ts +86 -0
  148. package/src/vector-cortex/ledger/event-codec.ts +79 -0
  149. package/src/vector-cortex/ledger/event-codecB.ts +87 -0
  150. package/src/vector-cortex/ledger/sqlite.ts +265 -0
  151. package/src/vector-cortex/ledger/store.ts +380 -0
  152. package/src/vector-cortex/ledger/types.ts +141 -0
  153. package/src/vector-cortex/ledger/validator.ts +124 -0
  154. package/src/vector-cortex/migrations/effective-cut-v2.ts +149 -0
  155. package/src/vector-cortex/migrations/occurrence-v2.ts +192 -0
  156. package/src/vector-cortex/replay/cut.ts +169 -0
  157. package/src/vector-cortex/replay/emit.ts +89 -0
  158. package/src/vector-cortex/replay/replay.ts +308 -0
  159. package/src/vector-cortex/replay/replayB.ts +97 -0
  160. package/src/vector-cortex/replay/types.ts +145 -0
  161. package/src/vector-cortex/resilience/breaker-core.ts +444 -0
  162. package/src/vector-cortex/resilience/breaker.ts +22 -0
  163. package/src/vector-cortex/resilience/emit.ts +99 -0
  164. package/src/vector-cortex/resilience/spool-core.ts +411 -0
  165. package/src/vector-cortex/resilience/spool.ts +22 -0
  166. package/src/vector-cortex/resilience/types.ts +158 -0
  167. package/extensions/dashboard-client/dist/assets/index-B93cewiX.js.map +0 -1
  168. package/extensions/dashboard-client/dist/assets/index-C_tCbOI0.css +0 -1
@@ -0,0 +1,200 @@
1
+ /**
2
+ * vector-cortex/ledger/sqlite.ts — occurrence-v2 SQLite store, Mode A (VC1B).
3
+ *
4
+ * A self-contained, isolated SQLite store (`node:sqlite` `DatabaseSync`) over its
5
+ * OWN database file — NOT the host `sqlite.db` — so the neutral v2 ledger is a
6
+ * true independent authority, not a host table. Holds the canonical occurrence
7
+ * rows plus per-session monotonic seq and the durable contiguous high-water.
8
+ *
9
+ * The defect M2 fixes: the host `raw_transcript` table keys on
10
+ * `(content_hash, session_id)`, so repeated equal bytes at a later turn are
11
+ * silently dropped. Here the ledger keys occurrences by `(event_id, digest)`
12
+ * ONLY — equal bytes at a distinct seq/event-id are preserved as separate
13
+ * occurrences (M2-DUP-001).
14
+ *
15
+ * Append enforces, per occurrence: (1) strictly monotonic per-session seq
16
+ * (EVT_SEQ_REGRESSION otherwise); (2) a tool RESULT names exactly one earlier
17
+ * call (EVT_TOOL_CALL_MISSING otherwise); (3) exact `(event_id,digest)`
18
+ * re-appends are acknowledged idempotently, never duplicated.
19
+ *
20
+ * Mode A of the VC1B triad: transactional SQLite append. Mode B (`./spool.ts`)
21
+ * is an independent fsync spool; mode C leaves the host transcript unchanged.
22
+ *
23
+ * PREVENT-002: every query is parameterized. PREVENT-011: no `any`.
24
+ * PREVENT-PI-004: local filesystem only, no network. No console.log.
25
+ */
26
+ import { DatabaseSync } from "node:sqlite";
27
+ import { existsSync, mkdirSync } from "node:fs";
28
+ import { dirname } from "node:path";
29
+ import { createHash } from "node:crypto";
30
+ const SCHEMA = `
31
+ CREATE TABLE IF NOT EXISTS occurrence_v2 (
32
+ session TEXT NOT NULL,
33
+ seq INTEGER NOT NULL,
34
+ event_id TEXT NOT NULL,
35
+ digest TEXT NOT NULL,
36
+ kind TEXT NOT NULL,
37
+ tool_call_id TEXT,
38
+ source_bytes BLOB,
39
+ PRIMARY KEY (session, seq),
40
+ UNIQUE (session, event_id, digest)
41
+ ) STRICT;
42
+
43
+ CREATE TABLE IF NOT EXISTS ledger_high_water (
44
+ session TEXT PRIMARY KEY,
45
+ seq INTEGER NOT NULL
46
+ ) STRICT;
47
+ `;
48
+ /** Default digest for a row when the caller omits one (sha256 over bytes). */
49
+ export function ledgerDigest(bytes) {
50
+ const hex = createHash("sha256").update(bytes).digest("hex");
51
+ return `sha256:${hex}`;
52
+ }
53
+ /** Open (or reuse) the isolated occurrence-v2 ledger DB handle. */
54
+ export function openOccurrenceStore(dbPath) {
55
+ const dir = dirname(dbPath);
56
+ if (!existsSync(dir))
57
+ mkdirSync(dir, { recursive: true });
58
+ const db = new DatabaseSync(dbPath);
59
+ db.exec("PRAGMA journal_mode = WAL");
60
+ db.exec("PRAGMA foreign_keys = ON");
61
+ db.exec(SCHEMA);
62
+ return db;
63
+ }
64
+ function rowToOccurrence(row) {
65
+ return {
66
+ session: row.session,
67
+ seq: BigInt(row.seq),
68
+ eventId: row.event_id,
69
+ digest: row.digest,
70
+ kind: row.kind,
71
+ toolCallId: row.tool_call_id ?? undefined,
72
+ sourceBytes: row.source_bytes ? Buffer.from(row.source_bytes) : new Uint8Array(0),
73
+ };
74
+ }
75
+ /** Current durable contiguous high-water for a session (0 when none). */
76
+ export function ledgerHighWater(db, session) {
77
+ const row = db
78
+ .prepare(`SELECT seq FROM ledger_high_water WHERE session = @session`)
79
+ .get({ "@session": session });
80
+ return row ? BigInt(row.seq) : 0n;
81
+ }
82
+ /** Read all accepted occurrences for a session in ascending (seq,eventId). */
83
+ export function readSessionOccurrences(db, session) {
84
+ const rows = db
85
+ .prepare(`SELECT session, seq, event_id, digest, kind, tool_call_id, source_bytes
86
+ FROM occurrence_v2 WHERE session = @session
87
+ ORDER BY seq ASC, event_id ASC`)
88
+ .all({ "@session": session });
89
+ return rows.map(rowToOccurrence);
90
+ }
91
+ /** Occurrences at or above `fromSeq` (inclusive), ascending. */
92
+ export function readFromSeq(db, session, fromSeq) {
93
+ const rows = db
94
+ .prepare(`SELECT session, seq, event_id, digest, kind, tool_call_id, source_bytes
95
+ FROM occurrence_v2 WHERE session = @session AND seq >= @from
96
+ ORDER BY seq ASC, event_id ASC`)
97
+ .all({ "@session": session, "@from": Number(fromSeq) });
98
+ return rows.map(rowToOccurrence);
99
+ }
100
+ /** Count accepted occurrences for a session. */
101
+ export function countOccurrences(db, session) {
102
+ const row = db
103
+ .prepare(`SELECT COUNT(*) AS cnt FROM occurrence_v2 WHERE session = @session`)
104
+ .get({ "@session": session });
105
+ return row.cnt;
106
+ }
107
+ /** Whether a `(event_id, digest)` pair is already accepted in a session. */
108
+ export function hasOccurrence(db, session, eventId, digest) {
109
+ const row = db
110
+ .prepare(`SELECT 1 AS hit FROM occurrence_v2
111
+ WHERE session = @session AND event_id = @event_id AND digest = @digest LIMIT 1`)
112
+ .get({ "@session": session, "@event_id": eventId, "@digest": digest });
113
+ return row !== undefined;
114
+ }
115
+ /** The stored occurrence matching a `(event_id, digest)` pair, or undefined. */
116
+ function findOccurrence(db, session, eventId, digest) {
117
+ const row = db
118
+ .prepare(`SELECT session, seq, event_id, digest, kind, tool_call_id, source_bytes
119
+ FROM occurrence_v2
120
+ WHERE session = @session AND event_id = @event_id AND digest = @digest LIMIT 1`)
121
+ .get({ "@session": session, "@event_id": eventId, "@digest": digest });
122
+ return row ? rowToOccurrence(row) : undefined;
123
+ }
124
+ /** Whether a prior call row with the given eventId exists in the session. */
125
+ function hasCallRef(db, session, eventId) {
126
+ const row = db
127
+ .prepare(`SELECT 1 AS hit FROM occurrence_v2
128
+ WHERE session = @session AND event_id = @event_id LIMIT 1`)
129
+ .get({ "@session": session, "@event_id": eventId });
130
+ return row !== undefined;
131
+ }
132
+ /**
133
+ * Append one occurrence within a transaction. Enforces monotonic seq, tool-call
134
+ * reference completeness, and `(event_id,digest)` idempotent ack. Returns the
135
+ * accepted row or a deterministic failure code.
136
+ */
137
+ export function appendOccurrence(db, input) {
138
+ const digest = input.digest ?? ledgerDigest(input.sourceBytes);
139
+ const occurrence = {
140
+ session: input.session,
141
+ seq: input.seq,
142
+ eventId: input.eventId,
143
+ digest,
144
+ kind: input.kind,
145
+ toolCallId: input.toolCallId,
146
+ sourceBytes: input.sourceBytes,
147
+ };
148
+ // (3) exact (event_id,digest) re-append is acknowledged idempotently, WITHOUT
149
+ // inserting a new row. Return the EXISTING stored occurrence (its real seq) —
150
+ // not the caller's input — so a journal record written from this result carries
151
+ // the existing seq, which always corresponds to a real occurrence_v2 row (Q01:
152
+ // never a phantom journal row at a seq with no backing occurrence).
153
+ const existing = findOccurrence(db, input.session, input.eventId, digest);
154
+ if (existing) {
155
+ return { ok: true, occurrence: existing };
156
+ }
157
+ // (1) monotonic seq: must be exactly the next expected seq (highWater+1).
158
+ const hw = ledgerHighWater(db, input.session);
159
+ if (input.seq !== hw + 1n) {
160
+ return { ok: false, code: "EVT_SEQ_REGRESSION", rejected: occurrence };
161
+ }
162
+ // (2) a tool RESULT names exactly one earlier call in the same session.
163
+ if (input.toolCallId !== undefined && input.toolCallId !== "") {
164
+ if (!hasCallRef(db, input.session, input.toolCallId)) {
165
+ return { ok: false, code: "EVT_TOOL_CALL_MISSING", rejected: occurrence };
166
+ }
167
+ }
168
+ // Accept: insert the row and advance the per-session high-water atomically.
169
+ db.exec("SAVEPOINT mc_ledger");
170
+ try {
171
+ db.prepare(`INSERT INTO occurrence_v2 (session, seq, event_id, digest, kind, tool_call_id, source_bytes)
172
+ VALUES (@session, @seq, @event_id, @digest, @kind, @tool_call_id, @source_bytes)`).run({
173
+ "@session": input.session,
174
+ "@seq": Number(input.seq),
175
+ "@event_id": input.eventId,
176
+ "@digest": digest,
177
+ "@kind": input.kind,
178
+ "@tool_call_id": input.toolCallId ?? null,
179
+ "@source_bytes": Buffer.from(input.sourceBytes),
180
+ });
181
+ db.prepare(`INSERT INTO ledger_high_water (session, seq) VALUES (@session, @seq)
182
+ ON CONFLICT(session) DO UPDATE SET seq = excluded.seq`).run({ "@session": input.session, "@seq": Number(input.seq) });
183
+ db.exec("RELEASE mc_ledger");
184
+ }
185
+ catch (e) {
186
+ db.exec("ROLLBACK TO mc_ledger");
187
+ db.exec("RELEASE mc_ledger");
188
+ throw e;
189
+ }
190
+ return { ok: true, occurrence };
191
+ }
192
+ /** Append a batch; each occurrence reports its own result. */
193
+ export function appendOccurrenceBatch(db, inputs) {
194
+ return inputs.map((input) => appendOccurrence(db, input));
195
+ }
196
+ /** Diagnostic: total accepted occurrences across all sessions. */
197
+ export function countAllOccurrences(db) {
198
+ const row = db.prepare(`SELECT COUNT(*) AS cnt FROM occurrence_v2`).get();
199
+ return row.cnt;
200
+ }
@@ -0,0 +1,184 @@
1
+ /**
2
+ * vector-cortex/ledger/store.ts — VC1B capability-separated ledger contracts.
3
+ *
4
+ * Owns `LedgerReader` / `LedgerWriter` / `LedgerAdmin` and `CompatJournalV1`.
5
+ * The ledger is append-only, seq is monotonic per session, a tool RESULT names
6
+ * exactly one earlier call in the same session, and duplicates are identified by
7
+ * `(eventId, digest)` only (two occurrences may share bytes at distinct seq).
8
+ *
9
+ * Capability gating mirrors the host `asReader/asWriter/asAdmin` pattern: a
10
+ * consumer receives ONLY what it needs. Dashboard GET receives `LedgerReader`;
11
+ * only ingestion receives `LedgerWriter`; only the migration coordinator
12
+ * receives `LedgerAdmin`. This keeps the neutral byte-authority ledger closed to
13
+ * anything but the sanctioned channels (CONTRACTS §Store and migration
14
+ * contracts).
15
+ *
16
+ * Pure type/schema definitions + small pure predicates. No storage, no console,
17
+ * no network, no side effects (PREVENT-PI-004 / PREVENT-011).
18
+ */
19
+ import { VC1B_ENABLED } from "../../config/vector-cortex.js";
20
+ import { openOccurrenceStore, appendOccurrence, ledgerHighWater, readSessionOccurrences, readFromSeq, countOccurrences, hasOccurrence, } from "./sqlite.js";
21
+ import { initCompatJournal, createCompatJournal, journalPhase, } from "./compat-journal.js";
22
+ import { m2Copy, m2Validate, m2Switch, } from "../migrations/occurrence-v2.js";
23
+ /** The token that gates closures to a single capability (PREVENT-011-free). */
24
+ const _capability = Symbol("mc-ledger-capability");
25
+ /**
26
+ * Create the capability-separated occurrence-v2 ledger over its OWN isolated
27
+ * SQLite DB. `emit` is optional; occurrence-appended and compat-switch-committed
28
+ * events are only emitted when VC1B_ENABLED() and an emitter is supplied (mode-C
29
+ * parity: flag OFF / no emitter => zero observability writes).
30
+ *
31
+ * Normalization: `stateDir` gives the standard daemon location
32
+ * `<stateDir>/vector-cortex/occurrence-v2.db`; a bare `dbPath` overrides it
33
+ * (tests/rehearsal isolate the ledger). The compat journal shares the same DB
34
+ * so a v2 append + its journal record commit atomically.
35
+ */
36
+ export function createLedgerStore(opts, emit) {
37
+ const dbPath = "dbPath" in opts
38
+ ? opts.dbPath
39
+ : `${opts.stateDir}/vector-cortex/occurrence-v2.db`;
40
+ const db = openOccurrenceStore(dbPath);
41
+ initCompatJournal(db);
42
+ const compat = createCompatJournal(db);
43
+ const fire = (event, fields) => {
44
+ if (!VC1B_ENABLED())
45
+ return;
46
+ try {
47
+ emit?.(event, fields);
48
+ }
49
+ catch {
50
+ /* non-fatal observability — never break the agent loop */
51
+ }
52
+ };
53
+ const asReader = () => ({
54
+ kind: "LedgerReader",
55
+ [_capability]: "reader",
56
+ highWater: (session) => ledgerHighWater(db, session),
57
+ readSession: (session) => readSessionOccurrences(db, session),
58
+ readFrom: (session, fromSeq) => readFromSeq(db, session, fromSeq),
59
+ count: (session) => countOccurrences(db, session),
60
+ hasOccurrence: (session, eventId, digest) => hasOccurrence(db, session, eventId, digest),
61
+ });
62
+ const asWriter = () => ({
63
+ kind: "LedgerWriter",
64
+ [_capability]: "writer",
65
+ append(input) {
66
+ // The occurrence insert and its compat-journal record commit atomically
67
+ // inside one nested savepoint (CONTRACTS §Store: atomically appends).
68
+ db.exec("SAVEPOINT mc_ledger_app");
69
+ try {
70
+ const result = appendOccurrence(db, input);
71
+ if (result.ok) {
72
+ compat.record({
73
+ occurrence: result.occurrence,
74
+ legacyProjection: legacyProjectionOf(result.occurrence),
75
+ });
76
+ }
77
+ if (result.ok) {
78
+ db.exec("RELEASE mc_ledger_app");
79
+ fire("vector_cortex_occurrence_appended", {
80
+ session: result.occurrence.session,
81
+ seq: result.occurrence.seq.toString(),
82
+ eventId: result.occurrence.eventId,
83
+ digest: result.occurrence.digest,
84
+ kind: result.occurrence.kind,
85
+ });
86
+ }
87
+ else {
88
+ db.exec("ROLLBACK TO mc_ledger_app");
89
+ db.exec("RELEASE mc_ledger_app");
90
+ }
91
+ return result;
92
+ }
93
+ catch (e) {
94
+ db.exec("ROLLBACK TO mc_ledger_app");
95
+ db.exec("RELEASE mc_ledger_app");
96
+ throw e;
97
+ }
98
+ },
99
+ appendBatch(inputs) {
100
+ const out = [];
101
+ for (const input of inputs)
102
+ out.push(this.append(input));
103
+ return out;
104
+ },
105
+ });
106
+ const asAdmin = () => ({
107
+ kind: "LedgerAdmin",
108
+ [_capability]: "admin",
109
+ compat,
110
+ migrateOccurrenceV2() {
111
+ // copy => validate => switch; resumable & idempotent. The M2 host wraps
112
+ // the live DB + journal so every phase transition advances the SAME
113
+ // singleton journal state machine (prepared→copied→validated→switched).
114
+ const unrepresentable = compat.prepare().length;
115
+ const mig = migrateHost(db, compat);
116
+ m2Copy(mig);
117
+ const v = m2Validate(mig);
118
+ if (v.ok) {
119
+ m2Switch(mig);
120
+ fire("vector_cortex_compat_switch_committed", {
121
+ session: "all",
122
+ unrepresentable,
123
+ });
124
+ }
125
+ return { ok: v.ok, codes: v.codes };
126
+ },
127
+ });
128
+ return { reader: asReader, writer: asWriter, admin: asAdmin, close: () => db.close() };
129
+ }
130
+ /**
131
+ * Lossless legacy projection for a v2 occurrence: its identity envelope plus
132
+ * base64 source bytes. Returns null (→ journal `unrepresentable` marker) when the
133
+ * source bytes do not form valid UTF-8, which the legacy pipeline cannot round-trip
134
+ * losslessly — those rows are listed rather than silently coerced (MIG-DOWN-003).
135
+ */
136
+ function legacyProjectionOf(occ) {
137
+ const source = Buffer.from(occ.sourceBytes);
138
+ try {
139
+ new TextDecoder("utf-8", { fatal: true }).decode(source);
140
+ }
141
+ catch {
142
+ return null;
143
+ }
144
+ return JSON.stringify({
145
+ session: occ.session,
146
+ eventId: occ.eventId,
147
+ kind: occ.kind,
148
+ digest: occ.digest,
149
+ source: source.toString("base64"),
150
+ });
151
+ }
152
+ /** Build an M2Host over the live ledger DB + journal (for migrateOccurrenceV2). */
153
+ function migrateHost(db, journal) {
154
+ let staged = null;
155
+ return {
156
+ db,
157
+ phase: () => journalPhase(db),
158
+ journalActive: () => journal.active(),
159
+ journalRows: () => journalRowsOf(db),
160
+ writeStagedLegacy: (rows) => {
161
+ staged = rows;
162
+ journal.copied();
163
+ },
164
+ stagedLegacy: () => staged,
165
+ validateStaged: () => journal.validate(),
166
+ switchLegacy: () => journal.switched(),
167
+ };
168
+ }
169
+ /** Read the journaled occurrences as legacy-export rows. */
170
+ function journalRowsOf(db) {
171
+ const rows = db
172
+ .prepare(`SELECT session, seq, event_id, digest, kind, legacy_projection, unrepresentable
173
+ FROM compat_journal_v1 ORDER BY session ASC, seq ASC`)
174
+ .all();
175
+ return rows.map((r) => ({
176
+ session: r.session,
177
+ seq: BigInt(r.seq),
178
+ eventId: r.event_id,
179
+ digest: r.digest,
180
+ kind: r.kind,
181
+ legacyProjection: r.legacy_projection,
182
+ unrepresentable: r.unrepresentable === 1,
183
+ }));
184
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * vector-cortex/ledger/types.ts — EventV2 / EventCodec byte-authority contract
3
+ * types, validation result codes, and the registered EVT conformance ID range.
4
+ *
5
+ * Owned by VC1A (canonical byte events). Consumes only reviewer-accepted
6
+ * predecessor contracts and [common contracts](../../CONTRACTS.md §EventV2),
7
+ * which are NORMATIVE here. `originalBytes` and its SHA-256 digest are the byte
8
+ * authority; strict UTF-8 classification is never lossy; `canonicalNfc` is a
9
+ * DERIVED comparison/search key only (never identity, digest, or reconstruction).
10
+ *
11
+ * Pure type/schema definitions + a short pure digest/classify predicate runtime
12
+ * (validator codes). No network, no side effects (PREVENT-PI-004 / PREVENT-011).
13
+ */
14
+ /**
15
+ * Registered EVT conformance ID range (EVT-001..030). The acceptance test reads
16
+ * these rows from the v2 manifest and asserts each returns its manifest bytes or
17
+ * exactly its listed failure code. Mirrors CUT_IDS / M3_IDS in replay/types.ts.
18
+ * EVT-001..015 are owned by VC1A (codec/validator); EVT-016..030 are owned by
19
+ * VC1B (occurrence ledger, see ledger/store.ts).
20
+ */
21
+ export const EVT_IDS = [
22
+ "EVT-001",
23
+ "EVT-002",
24
+ "EVT-003",
25
+ "EVT-004",
26
+ "EVT-005",
27
+ "EVT-006",
28
+ "EVT-007",
29
+ "EVT-008",
30
+ "EVT-009",
31
+ "EVT-010",
32
+ "EVT-011",
33
+ "EVT-012",
34
+ "EVT-013",
35
+ "EVT-014",
36
+ "EVT-015",
37
+ "EVT-016",
38
+ "EVT-017",
39
+ "EVT-018",
40
+ "EVT-019",
41
+ "EVT-020",
42
+ "EVT-021",
43
+ "EVT-022",
44
+ "EVT-023",
45
+ "EVT-024",
46
+ "EVT-025",
47
+ "EVT-026",
48
+ "EVT-027",
49
+ "EVT-028",
50
+ "EVT-029",
51
+ "EVT-030",
52
+ ];
@@ -0,0 +1,117 @@
1
+ /**
2
+ * vector-cortex/ledger/validator.ts — EventV2 canonical validation (VC1A).
3
+ *
4
+ * Canonical ordering is `(sessionId, seq, eventId bytewise UTF-8)`: the sort is
5
+ * by the eventId's UTF-8 BYTES (unsigned) — NOT by JS string code-unit order —
6
+ * so multi-byte eventIds sort bytewise. Deterministic failure detection with a
7
+ * fixed priority order:
8
+ * 1. EVT_DIGEST_MISMATCH — sha256(originalBytes) !== bytesDigest
9
+ * 2. EVT_UTF8_TAG_INVALID — stored `utf8` discriminant contradicting a strict
10
+ * fatal re-classification of originalBytes
11
+ * 3. EVT_DUPLICATE_ID — duplicate (sessionId, seq, eventId) occurrence
12
+ *
13
+ * The unique failure injection requirement: flip ONE stored byte while retaining
14
+ * the SHA-256 signature — the digest recomputation diverges and the validator
15
+ * returns EVT_DIGEST_MISMATCH (never a lossy replacement text, which is not
16
+ * produced anywhere in this module).
17
+ *
18
+ * Pure/deterministic — no console, no network, no side effects (PREVENT-PI-004).
19
+ */
20
+ import nodeCrypto from "node:crypto";
21
+ import { classifyUtf8 } from "./event-codec.js";
22
+ /** Mode-A digest recompute (validator uses the codec's canonical digest rule). */
23
+ function digestOf(bytes) {
24
+ const hex = nodeCrypto.createHash("sha256").update(bytes).digest("hex");
25
+ return `sha256:${hex}`;
26
+ }
27
+ /** Bytewise UTF-8 comparator for eventId tiebreak (unsigned bytes, not code units). */
28
+ function compareEventIdBytes(a, b) {
29
+ const ab = Buffer.from(a, "utf8");
30
+ const bb = Buffer.from(b, "utf8");
31
+ const n = Math.min(ab.length, bb.length);
32
+ for (let i = 0; i < n; i++) {
33
+ if (ab[i] !== bb[i])
34
+ return ab[i] < bb[i] ? -1 : 1;
35
+ }
36
+ if (ab.length !== bb.length)
37
+ return ab.length < bb.length ? -1 : 1;
38
+ return 0;
39
+ }
40
+ /** Comparator: `(sessionId, seq, eventId bytewise UTF-8)`. */
41
+ export function compareEvents(a, b) {
42
+ if (a.sessionId !== b.sessionId)
43
+ return a.sessionId < b.sessionId ? -1 : 1;
44
+ if (a.seq !== b.seq)
45
+ return a.seq < b.seq ? -1 : 1;
46
+ return compareEventIdBytes(a.eventId, b.eventId);
47
+ }
48
+ /** Ascending (sessionId, seq, eventId-bytes) sort. Returns a new array. */
49
+ export function sortEvents(events) {
50
+ return [...events].sort(compareEvents);
51
+ }
52
+ /** Recompute the digest and compare to the stored one (authority corruption). */
53
+ function digestMismatch(event) {
54
+ return digestOf(event.originalBytes) !== event.bytesDigest;
55
+ }
56
+ /** The stored utf8 discriminant must match a strict fatal re-classification. */
57
+ function utf8TagMismatch(event) {
58
+ const cls = classifyUtf8(event.originalBytes);
59
+ const stored = event.utf8;
60
+ if (stored.valid !== cls.valid)
61
+ return true;
62
+ if (!stored.valid) {
63
+ // Both invalid: never compare decoded text; only the (valid, base64) shape.
64
+ return false;
65
+ }
66
+ if (!cls.valid)
67
+ return true; // unreachable given `stored.valid === cls.valid`
68
+ // Both valid: compare the strict-decoded TEXT (no NFC — the tag stores raw text).
69
+ return stored.text !== cls.text;
70
+ }
71
+ /**
72
+ * Validate a batch of events. Returns `{ok:true, ordered}` when every event
73
+ * passes digest + UTF-8-tag consistency and no duplicate occurrence exists;
74
+ * otherwise `{ok:false, codes}` with the failure codes in fixed priority order,
75
+ * deduplicated, and an explicit issue list for diagnostics.
76
+ */
77
+ export function validateEvents(events) {
78
+ const issues = [];
79
+ // Fixed priority order: DIGEST_MISMATCH, UTF8_TAG_INVALID, DUPLICATE_ID.
80
+ for (const e of events) {
81
+ if (digestMismatch(e)) {
82
+ issues.push({ code: "EVT_DIGEST_MISMATCH", sessionId: e.sessionId, seq: e.seq, eventId: e.eventId });
83
+ }
84
+ }
85
+ for (const e of events) {
86
+ if (utf8TagMismatch(e)) {
87
+ issues.push({ code: "EVT_UTF8_TAG_INVALID", sessionId: e.sessionId, seq: e.seq, eventId: e.eventId });
88
+ }
89
+ }
90
+ // Unambiguous (sessionId, seq, eventId) occurrence key (seq stringified — a
91
+ // bigint cannot be JSON-serialized directly).
92
+ const key = (e) => JSON.stringify([e.sessionId, e.seq.toString(), e.eventId]);
93
+ const seen = new Set();
94
+ for (const e of sortEvents(events)) {
95
+ const k = key(e);
96
+ if (seen.has(k)) {
97
+ issues.push({ code: "EVT_DUPLICATE_ID", sessionId: e.sessionId, seq: e.seq, eventId: e.eventId });
98
+ }
99
+ seen.add(k);
100
+ }
101
+ if (issues.length === 0) {
102
+ return { ok: true, ordered: sortEvents(events) };
103
+ }
104
+ // Surface the per-occurrence issues (real locators) alongside the deduped
105
+ // codes so the adapter/observability feed can name WHICH event failed.
106
+ return { ok: false, codes: dedupe(issues.map((i) => i.code)), issues };
107
+ }
108
+ function dedupe(codes) {
109
+ const out = [];
110
+ for (const c of codes) {
111
+ if (!out.includes(c))
112
+ out.push(c);
113
+ }
114
+ return out;
115
+ }
116
+ /** Export the canonical ordering comparator for consumers (replay etc.). */
117
+ export { compareEventIdBytes };
@@ -0,0 +1,103 @@
1
+ /**
2
+ * vector-cortex/migrations/effective-cut-v2.ts — M3 copy/validate/switch
3
+ * migration (VC0B).
4
+ *
5
+ * Freezes the v2 effective replay cut: `min(boundarySafeSeq, committedSeq,
6
+ * capturedHighWater)` with pair retreat and anchor floor. The migration copies
7
+ * the candidate into the staged slot, validates the STAGED pointer against the
8
+ * minima and pair/anchor invariants (plus copy-match), and ONLY THEN atomically
9
+ * switches the active pointer. The three phases are exposed separately so a
10
+ * crash between validate and switch retains the OLD pointer and a restart
11
+ * resumes idempotently (the unique failure-injection contract).
12
+ *
13
+ * This is an internal developer seam: no dashboard/API change is necessary. It
14
+ * operates against the EXISTING host state via an injected M3Host interface —
15
+ * the future v2 compat-journal is owned by VC1B and is not introduced here.
16
+ */
17
+ import { computeEffectiveCutV2 } from "../replay/cut.js";
18
+ /** Migration failure codes (registered in M3-001..010 as conformance rows). */
19
+ export const M3_FAIL = {
20
+ MISSING_HOST: "M3_HOST_MISSING",
21
+ INVALID_MINIMA: "M3_MINIMA_VIOLATED",
22
+ PAIR_SPLIT: "M3_PAIR_SPLIT",
23
+ ANCHOR_CROSSED: "M3_ANCHOR_CROSSED",
24
+ COPY_MISMATCH: "M3_COPY_MISMATCH",
25
+ };
26
+ /** Pure effective-cut computation (min-of-three + retreat + floor). */
27
+ export function m3Compute(input) {
28
+ return computeEffectiveCutV2(input).cut.effectiveSeq;
29
+ }
30
+ /**
31
+ * Phase 1 — copy: compute the candidate effective cut and stage it via the host
32
+ * (without activating). Returns the staged seq.
33
+ */
34
+ export function m3Copy(host, input) {
35
+ const effective = m3Compute(input);
36
+ host.writeStaged(effective);
37
+ return effective;
38
+ }
39
+ /**
40
+ * Phase 2 — validate: the host's STAGED pointer must equal the freshly computed
41
+ * effective cut (copy-match), respect the minima, not split a pair, and never
42
+ * fall below the anchor floor. Failure codes:
43
+ * M3_HOST_MISSING — no staged pointer at all
44
+ * M3_COPY_MISMATCH — staged != freshly computed effective
45
+ * M3_MINIMA_VIOLATED — effective exceeds a source minimum (or requested)
46
+ * M3_PAIR_SPLIT — effective splits a tool call/result pair
47
+ * M3_ANCHOR_CROSSED — effective is below the anchor floor
48
+ */
49
+ export function m3Validate(host, input) {
50
+ const codes = [];
51
+ const { cut, retreats } = computeEffectiveCutV2(input);
52
+ const expected = cut.effectiveSeq;
53
+ const staged = host.stagedPointer();
54
+ if (staged === null) {
55
+ codes.push(M3_FAIL.MISSING_HOST);
56
+ }
57
+ else {
58
+ if (staged !== expected)
59
+ codes.push(M3_FAIL.COPY_MISMATCH);
60
+ if (staged > input.boundarySafeSeq ||
61
+ staged > host.committedSeq() ||
62
+ staged > input.capturedHighWater ||
63
+ staged > input.requestedSeq) {
64
+ codes.push(M3_FAIL.INVALID_MINIMA);
65
+ }
66
+ for (const p of input.pairs) {
67
+ if (p.callSeq <= staged && staged < p.resultSeq) {
68
+ codes.push(M3_FAIL.PAIR_SPLIT);
69
+ break;
70
+ }
71
+ }
72
+ if (staged < input.anchorFloor)
73
+ codes.push(M3_FAIL.ANCHOR_CROSSED);
74
+ }
75
+ return {
76
+ ok: codes.length === 0,
77
+ codes,
78
+ retreats,
79
+ effectiveSeq: expected,
80
+ };
81
+ }
82
+ /**
83
+ * Phase 3 — switch: atomic activation of the staged pointer. Only call after
84
+ * m3Validate reports ok; the host performs the durable pointer flip.
85
+ */
86
+ export function m3Switch(host, input) {
87
+ const effective = m3Compute(input);
88
+ host.switchPointer(effective);
89
+ return effective;
90
+ }
91
+ /**
92
+ * Run the full copy-validate-switch migration against a host. On validation
93
+ * failure the switch is NOT performed and the host retains its previous pointer
94
+ * (crash-safety / copy-validate-switch).
95
+ */
96
+ export function migrateEffectiveCutV2(host, input) {
97
+ m3Copy(host, input);
98
+ const v = m3Validate(host, input);
99
+ if (!v.ok)
100
+ return { ok: false, codes: v.codes, effectiveSeq: v.effectiveSeq };
101
+ m3Switch(host, input);
102
+ return { ok: true, codes: [], effectiveSeq: v.effectiveSeq };
103
+ }