@soulcraft/cor 3.0.9 → 3.0.10

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.
@@ -127,6 +127,24 @@ const FLUSH_FAILURE_BACKPRESSURE_THRESHOLD = 4;
127
127
  * to this ceiling. A successful consolidation resets to zero.
128
128
  */
129
129
  const CONSOLIDATION_BACKOFF_MAX_MS = 5 * 60_000;
130
+ /**
131
+ * Live-L0-segment count that triggers a loud warning: consolidation folds at
132
+ * {@link L0_COMPACT_THRESHOLD}, so accumulating this many segments means it
133
+ * has been failing for many cycles. Every query fans out across all live
134
+ * segments plus the base — memory-vm's incident reached 162 segments and
135
+ * degraded reads to 25–40 s. The warn is the early alarm; the hard cap below
136
+ * is the brake.
137
+ */
138
+ const L0_SEGMENT_WARN_THRESHOLD = 32;
139
+ /**
140
+ * Live-L0-segment count at which `addItem` REFUSES writes loudly
141
+ * (CORTEX-CONSOLIDATION-SILENT-DROP hardening). Segments grow past the warn
142
+ * threshold only while consolidation persistently fails; each one adds query
143
+ * fan-out and RAM until reads collapse and the process OOMs. Same posture as
144
+ * the flush-failure brake: a loud bounded stop beats an unbounded quiet
145
+ * degradation. Clears itself the moment one consolidation folds the segments.
146
+ */
147
+ const L0_SEGMENT_HARD_CAP = 256;
130
148
  /**
131
149
  * Storage key of the vector segment manifest (flat key — slashed keys trip
132
150
  * the storage adapters' unknown-key warning; same lesson as the metadata
@@ -434,6 +452,18 @@ export class NativeDiskAnnWrapper {
434
452
  * `rebuild()` call, which folds the delta into the main index.
435
453
  */
436
454
  async addItem(item) {
455
+ // Segment brake (CORTEX-CONSOLIDATION-SILENT-DROP): segments accumulate
456
+ // past the warn threshold only while consolidation persistently fails —
457
+ // the delta keeps draining to NEW segments (flush works), so the flush-
458
+ // failure brake never engages, yet fan-out and RAM grow without bound
459
+ // (162 segments degraded memory-vm's reads to 25–40 s en route to OOM).
460
+ // Refuse loudly at the cap; one successful consolidation clears it.
461
+ if (this.l0Segments.length >= L0_SEGMENT_HARD_CAP) {
462
+ throw new Error(`NativeDiskAnnWrapper: ${this.l0Segments.length} live L0 segments — ` +
463
+ `consolidation is persistently failing; refusing writes to prevent ` +
464
+ `unbounded segment growth (read collapse / OOM). See the ` +
465
+ `consolidation-failure log lines for the underlying cause.`);
466
+ }
437
467
  // item.vector.length is the authoritative width for THIS write; fall back to
438
468
  // it when the brain is zero-config (config.dimensions undefined). Otherwise
439
469
  // bytesPerVector is NaN and every `NaN >= cap` backpressure/fold check is
@@ -1523,13 +1553,33 @@ export class NativeDiskAnnWrapper {
1523
1553
  }
1524
1554
  prodLog.info(`NativeDiskAnnWrapper: flushed ${builtDelta.size} delta entries to L0 segment ${file} ` +
1525
1555
  `(${this.l0Segments.length} live segments)`);
1556
+ // Integrity probe (CORTEX-CONSOLIDATION-SILENT-DROP hardening): one O(1)
1557
+ // stat per flush catches a base deleted out from under the live handle in
1558
+ // SECONDS instead of at the next consolidation — the strand ran for hours
1559
+ // undetected on memory-vm. Detection here; the heal is the consolidation
1560
+ // below (runRebuild's self-heal drops the stale handle and rebuilds from
1561
+ // segments + canonical).
1562
+ const baseMissing = this.native !== null &&
1563
+ this.config.indexPath !== undefined &&
1564
+ !existsSync(this.config.indexPath);
1565
+ if (baseMissing) {
1566
+ prodLog.error(`NativeDiskAnnWrapper: durable base index missing under a live handle ` +
1567
+ `(${this.config.indexPath}) — triggering self-heal consolidation`);
1568
+ }
1569
+ if (this.l0Segments.length >= L0_SEGMENT_WARN_THRESHOLD) {
1570
+ prodLog.error(`NativeDiskAnnWrapper: ${this.l0Segments.length} live L0 segments ` +
1571
+ `(consolidation folds at ${L0_COMPACT_THRESHOLD}) — consolidation is ` +
1572
+ `failing; query fan-out is degrading and the write brake engages at ` +
1573
+ `${L0_SEGMENT_HARD_CAP}`);
1574
+ }
1526
1575
  // Tiered maintenance: enough small segments → one BACKGROUND consolidation
1527
1576
  // (the async rebuild folds base + L0s + delta into a fresh base off-thread).
1528
- // Guarded by an exponential backoff: a persistently-failing consolidation
1529
- // must not re-fire on every ~6 s flush (memory-vm hammered it for hours) —
1530
- // it backs off up to CONSOLIDATION_BACKOFF_MAX_MS, and a success resets it
1531
- // (see the tail of runRebuild). CORTEX-CONSOLIDATION-SILENT-DROP.
1532
- if (this.l0Segments.length >= L0_COMPACT_THRESHOLD &&
1577
+ // A missing base skips the segment-count threshold — heal now, not three
1578
+ // flushes from now. Guarded by an exponential backoff: a persistently-
1579
+ // failing consolidation must not re-fire on every ~6 s flush (memory-vm
1580
+ // hammered it for hours) — it backs off up to CONSOLIDATION_BACKOFF_MAX_MS,
1581
+ // and a success resets it (see the tail of runRebuild).
1582
+ if ((baseMissing || this.l0Segments.length >= L0_COMPACT_THRESHOLD) &&
1533
1583
  !this.rebuildInFlight &&
1534
1584
  Date.now() >= this.consolidationCooldownUntil) {
1535
1585
  this.rebuild().catch((error) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/cor",
3
- "version": "3.0.9",
3
+ "version": "3.0.10",
4
4
  "description": "Native Rust acceleration for Brainy — SIMD distance, vector quantization, zero-copy mmap, native embeddings. Free tier for storage, Pro license for compute acceleration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",