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,347 @@
1
+ /**
2
+ * vector-cortex/resilience/spool-core.ts — VC0C durable spool implementation (VC0C).
3
+ *
4
+ * Implementation file behind the `spool.ts` factory barrel. See spool.ts for the
5
+ * public surface; this file owns the TRIAD_RESILIENCE §spool append/fsync/ack
6
+ * protocol.
7
+ *
8
+ * Implements TRIAD_RESILIENCE.md §Spool protocol as the mode-B independent,
9
+ * deterministic, local disk spool that derives directly from authority:
10
+ *
11
+ * - One spool file per session: `spool-<session>.spool` under a state dir.
12
+ * - Header records schema, session, first sequence, and the prior durable
13
+ * contiguous high-water.
14
+ * - Frames are length-prefixed binary records carrying seq + eventId +
15
+ * original bytes + SHA-256 + CRC32C (Castagnoli). A frame is FSYNC'd to disk
16
+ * before the writer acknowledges `SPOOLED`; only a durable ledger commit
17
+ * (after `drain` inserts into authority and the ledger is fsynced) returns
18
+ * `SPOOL_COMMITTED` and advances the contiguous high-water.
19
+ * - `drain` strictly sorts frames by `(seq, eventId)`, rejects gaps/conflicts,
20
+ * inserts idempotently by `(eventId, digest)`, appends an fsynced ack frame,
21
+ * then advances contiguous high-water. A duplicate with the same id+digest is
22
+ * acknowledged (`SPOOL_IDEMPOTENT_ACK`); the same id with a DIFFERENT digest
23
+ * is `SPOOL_MANUAL_HALT`.
24
+ * - Crash before an ack safely replays: on reopen, frames after the last ack
25
+ * are re-drained (the ack frame carries the committed seq).
26
+ * - A derived builder may never read beyond the durable contiguous authority
27
+ * high-water; during an authority outage the high-water freezes (drain cannot
28
+ * commit) even while the spool keeps accepting frames.
29
+ *
30
+ * LOCAL ONLY: filesystem + node:crypto, zero network (PREVENT-PI-004); no `any`
31
+ * (PREVENT-011); synchronous durable writes (fsyncSync) per the spec.
32
+ */
33
+ import { createHash } from "node:crypto";
34
+ import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, writeSync } from "node:fs";
35
+ import { basename, join } from "node:path";
36
+ export const SPOOL_SCHEMA = "spool-v1";
37
+ /** Sentinel eventId of an acknowledgement frame (carries the committed seq). */
38
+ export const ACK_EVENT_ID = "__ack__";
39
+ /** Fixed on-disk width of the digest field: "sha256:" + 64 hex chars = 71 bytes. */
40
+ export const DIGEST_FIELD_LEN = "sha256:".length + 64;
41
+ /** CRC32C (Castagnoli) table — seeded once. */
42
+ const CRC_TABLE = (() => {
43
+ const t = new Array(256);
44
+ for (let n = 0; n < 256; n++) {
45
+ let c = n;
46
+ for (let k = 0; k < 8; k++)
47
+ c = c & 1 ? 0x82f63b78 ^ (c >>> 1) : c >>> 1;
48
+ t[n] = c >>> 0;
49
+ }
50
+ return t;
51
+ })();
52
+ export function crc32c(bytes) {
53
+ let c = 0xffffffff;
54
+ for (let i = 0; i < bytes.length; i++) {
55
+ c = CRC_TABLE[(c ^ bytes[i]) & 0xff] ^ (c >>> 8);
56
+ }
57
+ return (c ^ 0xffffffff) >>> 0;
58
+ }
59
+ export function sha256Hex(bytes) {
60
+ return createHash("sha256").update(bytes).digest("hex");
61
+ }
62
+ export function createSpool(opts) {
63
+ mkdirSync(opts.dir, { recursive: true });
64
+ const bySession = new Map();
65
+ return {
66
+ session(session) {
67
+ let s = bySession.get(session);
68
+ if (!s) {
69
+ s = new SessionSpoolImpl(join(opts.dir, `spool-${basename(session)}.spool`), session, opts);
70
+ bySession.set(session, s);
71
+ }
72
+ return s;
73
+ },
74
+ };
75
+ }
76
+ /**
77
+ * Durably manages one session's spool file. Fully synchronous: every append is
78
+ * fsynced before acknowledging; every committed drain appends+fsyncs an ack
79
+ * frame. Reopen parses the header + frames, stops at a torn trailing frame
80
+ * (crash mid-write => unacknowledged tail), and drains only frames strictly
81
+ * beyond the recovered contiguous high-water — crash before ack safely replays.
82
+ */
83
+ class SessionSpoolImpl {
84
+ session;
85
+ file;
86
+ outage;
87
+ reporter;
88
+ header;
89
+ frames = [];
90
+ ackedSeq;
91
+ frontierFrozen = false;
92
+ constructor(file, session, opts) {
93
+ this.file = file;
94
+ this.session = session;
95
+ this.outage = opts.authorityOutage ?? (() => false);
96
+ this.reporter = opts.reporter;
97
+ mkdirSync(join(file, ".."), { recursive: true });
98
+ if (existsSync(file)) {
99
+ const parsed = this.readExisting();
100
+ this.header = parsed.header;
101
+ this.frames = parsed.frames;
102
+ this.ackedSeq = parsed.highWater;
103
+ }
104
+ else {
105
+ this.header = { schema: SPOOL_SCHEMA, session, firstSeq: 1, priorHighWater: "0" };
106
+ this.ackedSeq = 0n;
107
+ this.writeHeader();
108
+ }
109
+ }
110
+ writeHeader() {
111
+ const fd = openSync(this.file, "a");
112
+ try {
113
+ writeSync(fd, Buffer.from(encodeHeader(this.header), "utf8"));
114
+ fsyncSync(fd);
115
+ }
116
+ finally {
117
+ closeSync(fd);
118
+ }
119
+ }
120
+ /** Reopen: parse header + frames; recover the last contiguous ack high-water. */
121
+ readExisting() {
122
+ const raw = readFileSync(this.file);
123
+ const nl = raw.indexOf(10);
124
+ if (nl < 0) {
125
+ return { header: { schema: SPOOL_SCHEMA, session: this.session, firstSeq: 1, priorHighWater: "0" }, frames: [], highWater: 0n };
126
+ }
127
+ const header = parseHeader(raw.subarray(0, nl).toString("utf8"));
128
+ let highWater = BigInt(header.priorHighWater);
129
+ const frames = [];
130
+ let off = nl + 1;
131
+ for (;;) {
132
+ if (off >= raw.length)
133
+ break;
134
+ const f = tryParseFrame(raw.subarray(off));
135
+ if (!f)
136
+ break; // torn/unacknowledged tail — stop, replay from last ack
137
+ const frame = { ...f, session: this.session };
138
+ if (frame.eventId === ACK_EVENT_ID) {
139
+ highWater = frame.seq; // ack frame carries the committed seq
140
+ }
141
+ else {
142
+ frames.push(frame);
143
+ }
144
+ const len = raw.readUInt32LE(off);
145
+ off += 4 + len;
146
+ }
147
+ // Only frames strictly beyond the recovered high-water need re-drain.
148
+ const unacked = frames.filter((f) => f.seq > highWater);
149
+ return { header, frames: unacked, highWater };
150
+ }
151
+ append(input) {
152
+ const digest = input.digest ?? `sha256:${sha256Hex(input.bytes)}`;
153
+ const frame = {
154
+ session: this.session,
155
+ seq: input.seq,
156
+ eventId: input.eventId,
157
+ digest,
158
+ bytes: input.bytes,
159
+ crc32c: crc32c(input.bytes),
160
+ };
161
+ const framed = encodeFrame(frame);
162
+ // Fsync BEFORE acknowledging SPOOLED.
163
+ const fd = openSync(this.file, "a");
164
+ try {
165
+ writeSync(fd, framed, 0, framed.length);
166
+ fsyncSync(fd);
167
+ }
168
+ finally {
169
+ closeSync(fd);
170
+ }
171
+ this.frames.push(frame);
172
+ return { verdict: "SPOOLED", seq: input.seq };
173
+ }
174
+ drain(insert) {
175
+ const tail = this.frames;
176
+ if (tail.length === 0)
177
+ return { verdict: "SPOOL_COMMITTED", committedSeq: this.ackedSeq };
178
+ // Strictly sort by (seq, eventId). NOTE: we do NOT clear this.frames up front
179
+ // (VC0C-Q04) — the queue is only cleared once every insert has succeeded and
180
+ // the ack is durably appended. If any insert throws (rather than returning a
181
+ // conflict), the unacked tail must be retained for a later retry, not dropped.
182
+ const sorted = [...tail].sort(compareFrames);
183
+ let committed = this.ackedSeq;
184
+ for (const f of sorted) {
185
+ if (f.seq !== committed + 1n) {
186
+ // Gap: requeue everything beyond the last committed seq and halt.
187
+ this.frames = sorted.filter((x) => x.seq > committed);
188
+ return { verdict: "SPOOL_MANUAL_HALT", committedSeq: committed, reason: `TRI_SPOOL_GAP expected ${committed + 1n} got ${f.seq}` };
189
+ }
190
+ let outcome;
191
+ try {
192
+ outcome = insert(this.session, f.seq, f.eventId, f.digest, f.bytes);
193
+ }
194
+ catch {
195
+ // Authority insert THREW (e.g. a transient authority write error). This is
196
+ // NON-FATAL: requeue the unacked tail and halt, mirroring the manual-halt
197
+ // path, instead of propagating (a propagating throw would lose the frames
198
+ // and leave a retry seeing SPOOL_COMMITTED-with-nothing-processed).
199
+ this.frames = sorted.filter((x) => x.seq > committed);
200
+ return { verdict: "SPOOL_MANUAL_HALT", committedSeq: committed, reason: "TRI_SPOOL_INSERT_THROW" };
201
+ }
202
+ if (outcome === "committed" || outcome === "idempotent") {
203
+ committed = f.seq;
204
+ }
205
+ else {
206
+ this.frames = sorted.filter((x) => x.seq > committed);
207
+ return { verdict: "SPOOL_MANUAL_HALT", committedSeq: committed, reason: "TRI_SPOOL_CONFLICT conflicting digest" };
208
+ }
209
+ }
210
+ // All frames committed: clear the in-memory queue, then durably commit by
211
+ // appending+fsyncing an ack frame and advancing the high-water.
212
+ this.frames = [];
213
+ this.appendAck(committed);
214
+ this.ackedSeq = committed;
215
+ return { verdict: "SPOOL_COMMITTED", committedSeq: committed };
216
+ }
217
+ appendAck(seq) {
218
+ const frame = {
219
+ session: this.session,
220
+ seq,
221
+ eventId: ACK_EVENT_ID,
222
+ digest: `sha256:${sha256Hex(new Uint8Array(0))}`,
223
+ bytes: new Uint8Array(0),
224
+ crc32c: crc32c(new Uint8Array(0)),
225
+ };
226
+ const framed = encodeFrame(frame);
227
+ const fd = openSync(this.file, "a");
228
+ try {
229
+ writeSync(fd, framed, 0, framed.length);
230
+ fsyncSync(fd);
231
+ }
232
+ finally {
233
+ closeSync(fd);
234
+ }
235
+ }
236
+ highWater() {
237
+ // The contiguous authority high-water advances ONLY on a durable ack
238
+ // (appendAck after a successful drain commit). During authority outage drain
239
+ // cannot commit, so ackedSeq naturally freezes here — an explicit outage
240
+ // branch would return the same value and was dead code (VC0C-Q05). The
241
+ // freeze/outage SIGNAL is the `frozen()`/`frontierFrozen` flag, not this
242
+ // scalar; frames appended during outage remain in the unacked tail.
243
+ return this.ackedSeq;
244
+ }
245
+ frozen() {
246
+ return this.frontierFrozen || this.outage();
247
+ }
248
+ freezeFrontier() {
249
+ const firstFreeze = !this.frontierFrozen && !this.outage();
250
+ this.frontierFrozen = true;
251
+ // Emit vector_cortex_frontier_frozen only on an actual freeze transition
252
+ // (once per outage; flag-gated + non-fatal inside the reporter). No event on
253
+ // an already-frozen or authority-outage-covered frontier. flag OFF => no-op.
254
+ if (firstFreeze) {
255
+ this.reporter?.frontierFrozen({
256
+ session: this.session,
257
+ committedSeq: String(this.ackedSeq),
258
+ frozenHighWater: String(this.ackedSeq),
259
+ });
260
+ }
261
+ return this.ackedSeq;
262
+ }
263
+ }
264
+ function compareFrames(a, b) {
265
+ if (a.seq !== b.seq)
266
+ return a.seq < b.seq ? -1 : 1;
267
+ return a.eventId < b.eventId ? -1 : a.eventId > b.eventId ? 1 : 0;
268
+ }
269
+ function encodeHeader(h) {
270
+ return `${JSON.stringify(h)}\n`;
271
+ }
272
+ function parseHeader(line) {
273
+ const raw = JSON.parse(line);
274
+ if (raw.schema !== SPOOL_SCHEMA)
275
+ throw new Error("TRI_SPOOL_SCHEMA");
276
+ return raw;
277
+ }
278
+ /**
279
+ * Encode one binary frame: [u32 len][seq i64][eventId UTF-8 NUL][bytes][NUL][digest][u32 crc32c].
280
+ * The digest field is DIGEST_FIELD_LEN bytes ("sha256:" + 64 hex = 71) — sizing it
281
+ * to fit the FULL digest string is load-bearing for read-side verification
282
+ * (VC0C-Q03): a 64-byte cap silently truncated Buffer.write, so the stored digest
283
+ * could never match the recomputed value and every frame would be rejected on read.
284
+ * body length = 8 + eventId.length + 1 + bytes.length + 1 + DIGEST_FIELD_LEN + 4.
285
+ */
286
+ function encodeFrame(f) {
287
+ const idBuf = Buffer.from(f.eventId, "utf8");
288
+ const bodyLen = 8 + idBuf.length + 1 + f.bytes.length + 1 + DIGEST_FIELD_LEN + 4;
289
+ const body = Buffer.alloc(bodyLen);
290
+ let o = 0;
291
+ body.writeBigInt64LE(BigInt(f.seq), o);
292
+ o += 8;
293
+ idBuf.copy(body, o);
294
+ o += idBuf.length;
295
+ body[o] = 0;
296
+ o += 1;
297
+ Buffer.from(f.bytes).copy(body, o);
298
+ o += f.bytes.length;
299
+ body[o] = 0;
300
+ o += 1;
301
+ body.write(f.digest, o, "utf8");
302
+ o += DIGEST_FIELD_LEN;
303
+ body.writeUInt32LE(f.crc32c, o);
304
+ o += 4;
305
+ const lenBuf = Buffer.alloc(4);
306
+ lenBuf.writeUInt32LE(bodyLen, 0);
307
+ return Buffer.concat([lenBuf, body]);
308
+ }
309
+ /**
310
+ * Parse a length-prefixed frame; returns null for a torn/truncated tail OR a
311
+ * CRYPTOGRAPHIC INTEGRITY MISMATCH (VC0C-Q03). The stored SHA-256 digest and
312
+ * CRC32C are recomputed over the parsed bytes and compared — a same-length bit
313
+ * flip must not survive reopen and be drained/committed. A mismatch is treated
314
+ * like a torn tail: `readExisting` stops there and the frame is NOT accepted —
315
+ * it remains unacknowledged, so replay/requeue preserves it rather than
316
+ * silently committing corrupted bytes.
317
+ */
318
+ function tryParseFrame(buf) {
319
+ if (buf.length < 4)
320
+ return null;
321
+ const len = buf.readUInt32LE(0);
322
+ if (buf.length < 4 + len)
323
+ return null; // torn
324
+ const body = buf.subarray(4, 4 + len);
325
+ let o = 0;
326
+ const seq = body.readBigInt64LE(o);
327
+ o += 8;
328
+ const idEnd = body.indexOf(0, o);
329
+ if (idEnd < 0)
330
+ return null;
331
+ const eventId = body.subarray(o, idEnd).toString("utf8");
332
+ o = idEnd + 1;
333
+ const bytesStart = o;
334
+ const nul2 = body.indexOf(0, o);
335
+ if (nul2 < 0)
336
+ return null;
337
+ const bytes = new Uint8Array(body.subarray(bytesStart, nul2));
338
+ o = nul2 + 1;
339
+ const digest = body.subarray(o, o + DIGEST_FIELD_LEN).toString("utf8");
340
+ const storedCrc = body.readUInt32LE(o + DIGEST_FIELD_LEN);
341
+ // Recompute and verify — write-only integrity would let a same-length bit flip
342
+ // drain/commit silently. Return null (treat as torn/corrupt) on mismatch.
343
+ if (digest !== `sha256:${sha256Hex(bytes)}` || storedCrc !== crc32c(bytes)) {
344
+ return null;
345
+ }
346
+ return { session: "", seq, eventId, digest, bytes, crc32c: storedCrc };
347
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * vector-cortex/resilience/spool.ts — VC0C durable spool barrel (VC0C).
3
+ *
4
+ * Thin barrel over the append/fsync/ack spool implementation in `spool-core.ts`
5
+ * (delegate-shell + impl-file split keeps the frequently-touched file small).
6
+ * Re-exports the public `createSpool` factory, the mode-B spool/session types,
7
+ * and the SHA-256 / CRC32C helpers for the safety adapter and tests.
8
+ *
9
+ * Full protocol semantics live in spool-core.ts (TRIAD_RESILIENCE §spool).
10
+ * Local-only, no network (PREVENT-PI-004), no `any` (PREVENT-011).
11
+ */
12
+ export { createSpool, SPOOL_SCHEMA, ACK_EVENT_ID, crc32c, sha256Hex } from "./spool-core.js";
@@ -0,0 +1,32 @@
1
+ /**
2
+ * vector-cortex/resilience/types.ts — VC0C breaker/triad contracts (VC0C).
3
+ *
4
+ * Owns `Mode`, `TriadResult<T>`, `BreakerRecord`, `KillDecision`, and the
5
+ * `Breaker` circuit-breaker seam from CONTRACTS.md §TriadResult and breaker
6
+ * seam, plus the durable spool record shapes. Pure type/schema definitions +
7
+ * small pure predicates (no storage, no console, no network, no side effects —
8
+ * PREVENT-PI-004 / PREVENT-011). The live state machine lives in
9
+ * `breaker.ts`/`spool.ts`; the safety adapter selects A→B→C in
10
+ * `extensions/mega-runtime/vector-cortex-safety.ts`.
11
+ *
12
+ * Triad semantics (TRIAD_RESILIENCE.md): A is optimized/learned; B is
13
+ * deterministic/local and derives directly from authority; C is continuity, not
14
+ * semantic completeness — it uses only the exact current transcript and reports
15
+ * that limitation.
16
+ */
17
+ /**
18
+ * Registered TRI conformance ID range (TRI-001..030). The acceptance test reads
19
+ * these rows from the v2 `resilience/` domain and asserts each returns its
20
+ * manifest bytes or exactly its listed failure code. TRI-001..015 are owned by
21
+ * VC0C (window/probe/hysteresis/cooldown transitions); TRI-016..030 span the
22
+ * spool protocol (frame, drain, gap, digest conflict, ack crash, frozen
23
+ * frontier). Mirrors EVT_IDS / CUT_IDS / M3_IDS.
24
+ */
25
+ export const TRI_IDS = [
26
+ "TRI-001", "TRI-002", "TRI-003", "TRI-004", "TRI-005",
27
+ "TRI-006", "TRI-007", "TRI-008", "TRI-009", "TRI-010",
28
+ "TRI-011", "TRI-012", "TRI-013", "TRI-014", "TRI-015",
29
+ "TRI-016", "TRI-017", "TRI-018", "TRI-019", "TRI-020",
30
+ "TRI-021", "TRI-022", "TRI-023", "TRI-024", "TRI-025",
31
+ "TRI-026", "TRI-027", "TRI-028", "TRI-029", "TRI-030",
32
+ ];
@@ -0,0 +1,41 @@
1
+ /**
2
+ * vector-cortex/eval/annotations.ts — redaction metadata + JSONL serialization.
3
+ *
4
+ * Payload bytes, prompts, and exact ledger text are NEVER serialized. Before a
5
+ * JSONL line is written, each such field is replaced by a SHA-256 digest + byte
6
+ * count + redaction kind (EVAL-REDACT-002: prompt bytes never appear in JSONL).
7
+ * The redaction journal is itself the AnnotationV1 output.
8
+ *
9
+ * Uses node:crypto for the digest (pure local math, PREVENT-PI-004 safe).
10
+ */
11
+ import { createHash } from "node:crypto";
12
+ /** SHA-256 over bytes, unpadded base64 (conformance canonical binary form). */
13
+ export function digestBytes(bytes) {
14
+ return createHash("sha256").update(bytes).digest("base64");
15
+ }
16
+ /** Replace every raw content field with its digest/count metadata. */
17
+ export function redactAnnotation(itemId, contents) {
18
+ const redactions = contents.map((c) => ({
19
+ field: c.field,
20
+ digest: digestBytes(c.bytes),
21
+ bytes: c.bytes.byteLength,
22
+ kind: c.kind,
23
+ }));
24
+ return {
25
+ itemId,
26
+ redactions,
27
+ redactedCount: redactions.length,
28
+ };
29
+ }
30
+ /**
31
+ * Serialize one set of raw contents to a single redacted JSONL line. Returns
32
+ * the annotation with its digest metadata and the JSONL text; the raw bytes
33
+ * never appear in the returned string.
34
+ */
35
+ export function serializeRedactedJsonl(itemId, contents) {
36
+ const annotation = redactAnnotation(itemId, contents);
37
+ return {
38
+ annotation,
39
+ jsonl: `${JSON.stringify(annotation)}\n`,
40
+ };
41
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * vector-cortex/eval/metrics.ts — canonical metric ordering + fixed histogram.
3
+ *
4
+ * 1. Stable sort rows by `(session, seq, event)` — session string byte order,
5
+ * then integer seq ascending, then event-name byte order as tiebreak
6
+ * (EVAL-ORDER-003: equal seq rows use event-name order).
7
+ * 2. Bucket latency values at exactly 1/5/10/25/50/100/250ms with inclusive
8
+ * boundaries (EVAL-BUCKET-001); values past 250ms kept in a separate
9
+ * overflow bucket. Histogram totals are permutation-stable.
10
+ *
11
+ * Pure/no side effects (PREVENT-PI-004).
12
+ */
13
+ import { LATENCY_BUCKETS } from "./types.js";
14
+ /** Inclusive-boundary bucket edges, with a sentinel past the largest edge. */
15
+ const EDGES = LATENCY_BUCKETS;
16
+ const OVERFLOW_INDEX = EDGES.length;
17
+ /** Histogram cell counts aligned to EDGES, plus a trailing overflow cell. */
18
+ export function emptyHistogram() {
19
+ return new Array(EDGES.length + 1).fill(0);
20
+ }
21
+ /** True when a sample carries a latency (`unit === "ms"`) datum. */
22
+ export function isLatencySample(m) {
23
+ return m.unit === "ms";
24
+ }
25
+ /** Bucket a latency ms value into a cell index (0..EDGES.length, last=overflow). */
26
+ export function bucketIndex(value) {
27
+ if (!Number.isFinite(value) || value < 0)
28
+ return OVERFLOW_INDEX;
29
+ for (let i = 0; i < EDGES.length; i++) {
30
+ if (value <= EDGES[i])
31
+ return i; // inclusive upper boundary
32
+ }
33
+ return OVERFLOW_INDEX;
34
+ }
35
+ /**
36
+ * Bucket latency sample values across all rows into the fixed histogram.
37
+ * Overflow is kept separate (counted in `overflow` and the trailing cell).
38
+ */
39
+ export function bucketHistogram(rows) {
40
+ const cells = emptyHistogram();
41
+ let overflow = 0;
42
+ for (const m of rows) {
43
+ if (!isLatencySample(m))
44
+ continue;
45
+ const idx = bucketIndex(m.value);
46
+ cells[idx] += 1;
47
+ if (idx === EDGES.length)
48
+ overflow += 1;
49
+ }
50
+ return {
51
+ cells,
52
+ edges: [...EDGES],
53
+ overflow,
54
+ total: cells.reduce((a, b) => a + b, 0),
55
+ };
56
+ }
57
+ /**
58
+ * Stable sort MetricEventV1 rows by `(session, seq, event)`:
59
+ * - session: UTF-8 / JS string byte order;
60
+ * - seq: ascending numeric;
61
+ * - event: ascending string byte order (tiebreak for equal seq).
62
+ * Equal content stays separate occurrences (never deduplicated here).
63
+ */
64
+ export function sortCanonical(rows) {
65
+ const out = [...rows];
66
+ out.sort((a, b) => {
67
+ const s = a.session < b.session ? -1 : a.session > b.session ? 1 : 0;
68
+ if (s !== 0)
69
+ return s;
70
+ if (a.seq !== b.seq)
71
+ return a.seq - b.seq;
72
+ return a.event < b.event ? -1 : a.event > b.event ? 1 : 0;
73
+ });
74
+ return out;
75
+ }
76
+ /**
77
+ * Assisted sort plus histogram, returning both under one result.
78
+ * The observer (mode A) calls this before JSONL serialization.
79
+ */
80
+ export function buildMetrics(rows) {
81
+ return {
82
+ rows: sortCanonical(rows),
83
+ histogram: bucketHistogram(rows),
84
+ rejects: [],
85
+ };
86
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * vector-cortex/eval/observer.ts — VC0A structured observer (triad mode A).
3
+ *
4
+ * Connects to the evaluation reader: accepts MetricEventV1 samples, maintains
5
+ * the canonical `(session, seq, event)` ordered set plus the fixed latency
6
+ * histogram, and emits two structured local events:
7
+ * - vector_cortex_eval_sample_recorded (one per accepted sample)
8
+ * - vector_cortex_eval_redaction_rejected (when a redaction fails)
9
+ *
10
+ * Mode C (observer absent) is the responsibility of the caller: no observes(),
11
+ * no writes, byte-identical to the predecessor. This module never touches the
12
+ * network (PREVENT-PI-004) — it only manipulates in-memory rows and emits
13
+ * structured log lines through an injected logging callback so it stays
14
+ * pi-agnostic and deterministically testable.
15
+ */
16
+ import { buildMetrics, sortCanonical } from "./metrics.js";
17
+ /**
18
+ * Build a structured observer (mode A). Best-effort: emitting never throws
19
+ * into the extension path (non-fatal observability).
20
+ */
21
+ export function createEvalObserver(opts) {
22
+ const rows = [];
23
+ function emit(event, fields) {
24
+ try {
25
+ opts.emit(event, fields);
26
+ }
27
+ catch {
28
+ /* non-fatal observability — never break the agent loop */
29
+ }
30
+ }
31
+ return {
32
+ record(sample) {
33
+ rows.push(sample);
34
+ if (opts.persist) {
35
+ try {
36
+ opts.persist(sample);
37
+ }
38
+ catch {
39
+ /* non-fatal observability */
40
+ }
41
+ }
42
+ emit("vector_cortex_eval_sample_recorded", {
43
+ session: sample.session,
44
+ seq: sample.seq,
45
+ event: sample.event,
46
+ unit: sample.unit,
47
+ mode: sample.mode,
48
+ });
49
+ },
50
+ rejectRedaction(field, reason) {
51
+ emit("vector_cortex_eval_redaction_rejected", { field, reason });
52
+ },
53
+ rows() {
54
+ return sortCanonical(rows);
55
+ },
56
+ };
57
+ }
58
+ /** Aggregate metrics result from an observer's rows (canonical + histogram). */
59
+ export function observerMetrics(result) {
60
+ return buildMetrics(result.rows());
61
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * vector-cortex/eval/persist.ts — redacted eval JSONL persistence (VC0A).
3
+ *
4
+ * Bridges the in-memory observer (mode A) and the dashboard reader (separate
5
+ * process) via a disk JSONL under the state dir. Writes are append-only and
6
+ * hold only MetricEventV1 metric rows plus redaction metadata — never payloads,
7
+ * prompts, or exact ledger text (EVAL-REDACT-002). Best-effort and non-fatal
8
+ * (PREVENT-PI-004: local filesystem only; no network).
9
+ *
10
+ * PREVENT-011: no `any` type.
11
+ */
12
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
13
+ import { join } from "node:path";
14
+ /** Location of the redacted evaluation JSONL under a state dir. */
15
+ export function evalJsonlPath(stateDir) {
16
+ return join(stateDir, "vector-cortex", "evaluation.jsonl");
17
+ }
18
+ /** Append one redacted metric row as a canonical JSON line (append-only). */
19
+ export function appendEvalRow(stateDir, row) {
20
+ try {
21
+ const path = evalJsonlPath(stateDir);
22
+ mkdirSync(join(stateDir, "vector-cortex"), { recursive: true, mode: 0o700 });
23
+ // PER_EVAL_PERMISSION: 0600 file inherits state-dir permission policy.
24
+ writeFileSync(path, row.map((r) => `${JSON.stringify(r)}\n`).join(""), {
25
+ flag: "a",
26
+ mode: 0o600,
27
+ });
28
+ // guardrails-allow PREVENT-PI-004: local filesystem eval JSONL (no network)
29
+ }
30
+ catch {
31
+ /* non-fatal observability — never break the agent loop */
32
+ }
33
+ }
34
+ /**
35
+ * Read every redacted metric row from the JSONL. Malformed or truncated final
36
+ * lines are skipped (callers surface EVAL_JSONL_TRUNCATED); the caller decides
37
+ * whether that is fatal. Returns an empty array when the file is absent.
38
+ */
39
+ export function readEvalRows(stateDir) {
40
+ const out = [];
41
+ let raw;
42
+ try {
43
+ raw = readFileSync(evalJsonlPath(stateDir), "utf8");
44
+ }
45
+ catch {
46
+ return out;
47
+ }
48
+ for (const line of raw.split("\n")) {
49
+ if (line.trim().length === 0)
50
+ continue;
51
+ try {
52
+ const parsed = JSON.parse(line);
53
+ if (typeof parsed.session === "string" &&
54
+ typeof parsed.seq === "number" &&
55
+ typeof parsed.event === "string" &&
56
+ typeof parsed.value === "number" &&
57
+ typeof parsed.unit === "string" &&
58
+ (parsed.mode === "A" || parsed.mode === "B" || parsed.mode === "C")) {
59
+ out.push(parsed);
60
+ }
61
+ }
62
+ catch {
63
+ /* malformed line — skip; EVAL_JSONL_TRUNCATED surfaced by caller */
64
+ }
65
+ }
66
+ return out;
67
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * vector-cortex/eval/reader.ts — evaluation reader (aggregate-only).
3
+ *
4
+ * Reader-only capability: `summarizeEvalRows` collapses a set of MetricEventV1
5
+ * rows into an aggregate summary for the dashboard `GET /api/vector-cortex/
6
+ * evaluation` endpoint. No writable capability — the observer writes via its
7
+ * own path; this reader never mutates. Predecessor (flag-off / mode C) emits
8
+ * an empty summary byte-identical to "no data".
9
+ *
10
+ * Pure + local, zero network (PREVENT-PI-004).
11
+ */
12
+ import { buildMetrics } from "./metrics.js";
13
+ /** Aggregate rows into a reader-only summary (histogram + per-mode counts). */
14
+ export function summarizeEvalRows(rows) {
15
+ const byMode = { A: 0, B: 0, C: 0 };
16
+ for (const r of rows)
17
+ byMode[r.mode] += 1;
18
+ const metrics = buildMetrics(rows);
19
+ return {
20
+ samples: rows.length,
21
+ byMode,
22
+ histogram: {
23
+ edges: metrics.histogram.edges,
24
+ cells: metrics.histogram.cells,
25
+ overflow: metrics.histogram.overflow,
26
+ total: metrics.histogram.total,
27
+ },
28
+ };
29
+ }