@graphrefly/graphrefly 0.47.0 → 0.47.2

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 (187) hide show
  1. package/dist/base/composition/index.cjs +24 -16
  2. package/dist/base/composition/index.cjs.map +1 -1
  3. package/dist/base/composition/index.js +6 -6
  4. package/dist/base/index.cjs +142 -86
  5. package/dist/base/index.cjs.map +1 -1
  6. package/dist/base/index.js +11 -11
  7. package/dist/base/io/index.cjs +114 -68
  8. package/dist/base/io/index.cjs.map +1 -1
  9. package/dist/base/io/index.js +5 -5
  10. package/dist/base/sources/browser/index.cjs +13 -9
  11. package/dist/base/sources/browser/index.cjs.map +1 -1
  12. package/dist/base/sources/browser/index.js +13 -9
  13. package/dist/base/sources/browser/index.js.map +1 -1
  14. package/dist/base/sources/event/index.cjs +1 -1
  15. package/dist/base/sources/event/index.cjs.map +1 -1
  16. package/dist/base/sources/event/index.js +1 -1
  17. package/dist/base/sources/index.cjs +21 -13
  18. package/dist/base/sources/index.cjs.map +1 -1
  19. package/dist/base/sources/index.js +3 -3
  20. package/dist/base/sources/node/index.cjs +43 -37
  21. package/dist/base/sources/node/index.cjs.map +1 -1
  22. package/dist/base/sources/node/index.js +43 -37
  23. package/dist/base/sources/node/index.js.map +1 -1
  24. package/dist/{chunk-VLAGJZSL.js → chunk-3O3NKZJW.js} +2 -2
  25. package/dist/{chunk-YJ4U2D2C.js → chunk-446I4EGD.js} +9 -7
  26. package/dist/chunk-446I4EGD.js.map +1 -0
  27. package/dist/{chunk-DKNHAICT.js → chunk-5GVURVIG.js} +14 -8
  28. package/dist/chunk-5GVURVIG.js.map +1 -0
  29. package/dist/{chunk-2OB3CEJS.js → chunk-6MRSX3YK.js} +2 -2
  30. package/dist/{chunk-EVYY4X5A.js → chunk-6ZLCPUXS.js} +2 -2
  31. package/dist/{chunk-ZVXXDWIB.js → chunk-7AVQIGF6.js} +514 -33
  32. package/dist/chunk-7AVQIGF6.js.map +1 -0
  33. package/dist/{chunk-7EGRP2VX.js → chunk-7BULJTL6.js} +2 -2
  34. package/dist/{chunk-7EGRP2VX.js.map → chunk-7BULJTL6.js.map} +1 -1
  35. package/dist/{chunk-FW23JYNQ.js → chunk-CEVNQ74M.js} +2 -2
  36. package/dist/{chunk-CGHORL6G.js → chunk-DDTS7F5O.js} +7 -5
  37. package/dist/chunk-DDTS7F5O.js.map +1 -0
  38. package/dist/{chunk-OCUDSN63.js → chunk-EL5VHUGK.js} +79 -47
  39. package/dist/chunk-EL5VHUGK.js.map +1 -0
  40. package/dist/{chunk-4GYMCUDZ.js → chunk-EP4WVQLX.js} +5 -5
  41. package/dist/{chunk-SOOKUYVM.js → chunk-F7EKHR32.js} +13 -9
  42. package/dist/chunk-F7EKHR32.js.map +1 -0
  43. package/dist/{chunk-JKTC747G.js → chunk-FQSQONOU.js} +4 -4
  44. package/dist/{chunk-JGFRAFDL.js → chunk-FVINAAKA.js} +3 -3
  45. package/dist/{chunk-RAGGHLCV.js → chunk-GUNIRPEJ.js} +8 -6
  46. package/dist/{chunk-RAGGHLCV.js.map → chunk-GUNIRPEJ.js.map} +1 -1
  47. package/dist/{chunk-BU3SEFA5.js → chunk-IOJDYUA7.js} +2 -2
  48. package/dist/{chunk-Y52CS6YA.js → chunk-JA67ZQG2.js} +2 -2
  49. package/dist/{chunk-Y52CS6YA.js.map → chunk-JA67ZQG2.js.map} +1 -1
  50. package/dist/{chunk-DM4OMPWK.js → chunk-KNU73RZW.js} +2 -2
  51. package/dist/{chunk-GWRNLJNW.js → chunk-KRFGO5QH.js} +19 -15
  52. package/dist/{chunk-GWRNLJNW.js.map → chunk-KRFGO5QH.js.map} +1 -1
  53. package/dist/{chunk-Z4YXAUDN.js → chunk-KUFXLAEY.js} +11 -7
  54. package/dist/{chunk-Z4YXAUDN.js.map → chunk-KUFXLAEY.js.map} +1 -1
  55. package/dist/{chunk-Z6EGP5D7.js → chunk-LDCSZ72P.js} +2 -2
  56. package/dist/{chunk-5IMMNARC.js → chunk-MS3WPRJR.js} +37 -25
  57. package/dist/chunk-MS3WPRJR.js.map +1 -0
  58. package/dist/{chunk-CXANAIZU.js → chunk-N65E26UL.js} +3 -3
  59. package/dist/{chunk-O3MT7DYI.js → chunk-N6MNJNHB.js} +2 -2
  60. package/dist/{chunk-Q3EYOCZB.js → chunk-NPRP3MCV.js} +111 -2
  61. package/dist/chunk-NPRP3MCV.js.map +1 -0
  62. package/dist/{chunk-A7KV5UK4.js → chunk-OXD5LFQP.js} +2 -2
  63. package/dist/{chunk-ZT4WMQW4.js → chunk-PTWADEH3.js} +9 -7
  64. package/dist/chunk-PTWADEH3.js.map +1 -0
  65. package/dist/{chunk-IHTWQEDR.js → chunk-QFE5BQH7.js} +2 -2
  66. package/dist/{chunk-IHTWQEDR.js.map → chunk-QFE5BQH7.js.map} +1 -1
  67. package/dist/{chunk-22SG74BD.js → chunk-R6ZCSXKX.js} +3 -3
  68. package/dist/{chunk-PZWISPIQ.js → chunk-S7HN5FHL.js} +17 -11
  69. package/dist/chunk-S7HN5FHL.js.map +1 -0
  70. package/dist/{chunk-RJOG4IJU.js → chunk-T7SP3EYR.js} +18 -12
  71. package/dist/chunk-T7SP3EYR.js.map +1 -0
  72. package/dist/{chunk-4S53H2KR.js → chunk-VAZXUK6G.js} +2 -2
  73. package/dist/{chunk-IJRR6YAI.js → chunk-VLDRAMP7.js} +18 -12
  74. package/dist/chunk-VLDRAMP7.js.map +1 -0
  75. package/dist/{chunk-TNX5ZGDJ.js → chunk-VNXAF2KE.js} +4 -4
  76. package/dist/{chunk-EHRRQ4IC.js → chunk-VP3TIUDF.js} +2 -2
  77. package/dist/{chunk-6XZYT4SW.js → chunk-WGDEBIP4.js} +5 -5
  78. package/dist/{chunk-E5OZPDIW.js → chunk-X7BA5PWG.js} +7 -5
  79. package/dist/chunk-X7BA5PWG.js.map +1 -0
  80. package/dist/compat/index.cjs +1 -1
  81. package/dist/compat/index.cjs.map +1 -1
  82. package/dist/compat/index.js +2 -2
  83. package/dist/compat/nestjs/index.cjs +1 -1
  84. package/dist/compat/nestjs/index.cjs.map +1 -1
  85. package/dist/compat/nestjs/index.js +2 -2
  86. package/dist/index.cjs +1657 -982
  87. package/dist/index.cjs.map +1 -1
  88. package/dist/index.d.cts +2 -2
  89. package/dist/index.d.ts +2 -2
  90. package/dist/index.js +58 -36
  91. package/dist/index.js.map +1 -1
  92. package/dist/presets/ai/index.cjs +42 -26
  93. package/dist/presets/ai/index.cjs.map +1 -1
  94. package/dist/presets/ai/index.js +11 -11
  95. package/dist/presets/harness/index.cjs +53 -33
  96. package/dist/presets/harness/index.cjs.map +1 -1
  97. package/dist/presets/harness/index.js +22 -22
  98. package/dist/presets/index.cjs +76 -48
  99. package/dist/presets/index.cjs.map +1 -1
  100. package/dist/presets/index.js +28 -28
  101. package/dist/presets/inspect/index.cjs.map +1 -1
  102. package/dist/presets/inspect/index.js +4 -4
  103. package/dist/presets/resilience/index.cjs +35 -23
  104. package/dist/presets/resilience/index.cjs.map +1 -1
  105. package/dist/presets/resilience/index.js +5 -5
  106. package/dist/solutions/index.cjs +71 -45
  107. package/dist/solutions/index.cjs.map +1 -1
  108. package/dist/solutions/index.js +24 -24
  109. package/dist/{timeout-U5O4ESK3.js → timeout-BEABACRP.js} +2 -2
  110. package/dist/utils/ai/browser.cjs.map +1 -1
  111. package/dist/utils/ai/browser.js +9 -9
  112. package/dist/utils/ai/index.cjs +41 -25
  113. package/dist/utils/ai/index.cjs.map +1 -1
  114. package/dist/utils/ai/index.js +18 -18
  115. package/dist/utils/ai/node.js +3 -3
  116. package/dist/utils/domain-templates/index.cjs +1 -1
  117. package/dist/utils/domain-templates/index.cjs.map +1 -1
  118. package/dist/utils/domain-templates/index.js +2 -2
  119. package/dist/utils/graphspec/index.cjs +1 -1
  120. package/dist/utils/graphspec/index.cjs.map +1 -1
  121. package/dist/utils/graphspec/index.js +2 -2
  122. package/dist/utils/harness/index.cjs +16 -10
  123. package/dist/utils/harness/index.cjs.map +1 -1
  124. package/dist/utils/harness/index.js +1 -1
  125. package/dist/utils/index.cjs +1069 -452
  126. package/dist/utils/index.cjs.map +1 -1
  127. package/dist/utils/index.d.cts +2 -2
  128. package/dist/utils/index.d.ts +2 -2
  129. package/dist/utils/index.js +45 -23
  130. package/dist/utils/inspect/index.cjs.map +1 -1
  131. package/dist/utils/inspect/index.js +2 -2
  132. package/dist/utils/memory/index.cjs +513 -37
  133. package/dist/utils/memory/index.cjs.map +1 -1
  134. package/dist/utils/memory/index.d.cts +641 -3
  135. package/dist/utils/memory/index.d.ts +641 -3
  136. package/dist/utils/memory/index.js +19 -1
  137. package/dist/utils/messaging/index.cjs +109 -0
  138. package/dist/utils/messaging/index.cjs.map +1 -1
  139. package/dist/utils/messaging/index.d.cts +115 -2
  140. package/dist/utils/messaging/index.d.ts +115 -2
  141. package/dist/utils/messaging/index.js +5 -1
  142. package/dist/utils/orchestration/index.cjs +5 -3
  143. package/dist/utils/orchestration/index.cjs.map +1 -1
  144. package/dist/utils/orchestration/index.js +2 -2
  145. package/dist/utils/process/index.js +2 -2
  146. package/dist/utils/reduction/index.cjs +1 -1
  147. package/dist/utils/reduction/index.cjs.map +1 -1
  148. package/dist/utils/reduction/index.js +1 -1
  149. package/dist/utils/resilience/index.cjs +35 -23
  150. package/dist/utils/resilience/index.cjs.map +1 -1
  151. package/dist/utils/resilience/index.js +4 -4
  152. package/dist/utils/surface/index.cjs +1 -1
  153. package/dist/utils/surface/index.cjs.map +1 -1
  154. package/dist/utils/surface/index.js +3 -3
  155. package/package.json +1 -1
  156. package/dist/chunk-5IMMNARC.js.map +0 -1
  157. package/dist/chunk-CGHORL6G.js.map +0 -1
  158. package/dist/chunk-DKNHAICT.js.map +0 -1
  159. package/dist/chunk-E5OZPDIW.js.map +0 -1
  160. package/dist/chunk-IJRR6YAI.js.map +0 -1
  161. package/dist/chunk-OCUDSN63.js.map +0 -1
  162. package/dist/chunk-PZWISPIQ.js.map +0 -1
  163. package/dist/chunk-Q3EYOCZB.js.map +0 -1
  164. package/dist/chunk-RJOG4IJU.js.map +0 -1
  165. package/dist/chunk-SOOKUYVM.js.map +0 -1
  166. package/dist/chunk-YJ4U2D2C.js.map +0 -1
  167. package/dist/chunk-ZT4WMQW4.js.map +0 -1
  168. package/dist/chunk-ZVXXDWIB.js.map +0 -1
  169. /package/dist/{chunk-VLAGJZSL.js.map → chunk-3O3NKZJW.js.map} +0 -0
  170. /package/dist/{chunk-2OB3CEJS.js.map → chunk-6MRSX3YK.js.map} +0 -0
  171. /package/dist/{chunk-EVYY4X5A.js.map → chunk-6ZLCPUXS.js.map} +0 -0
  172. /package/dist/{chunk-FW23JYNQ.js.map → chunk-CEVNQ74M.js.map} +0 -0
  173. /package/dist/{chunk-4GYMCUDZ.js.map → chunk-EP4WVQLX.js.map} +0 -0
  174. /package/dist/{chunk-JKTC747G.js.map → chunk-FQSQONOU.js.map} +0 -0
  175. /package/dist/{chunk-JGFRAFDL.js.map → chunk-FVINAAKA.js.map} +0 -0
  176. /package/dist/{chunk-BU3SEFA5.js.map → chunk-IOJDYUA7.js.map} +0 -0
  177. /package/dist/{chunk-DM4OMPWK.js.map → chunk-KNU73RZW.js.map} +0 -0
  178. /package/dist/{chunk-Z6EGP5D7.js.map → chunk-LDCSZ72P.js.map} +0 -0
  179. /package/dist/{chunk-CXANAIZU.js.map → chunk-N65E26UL.js.map} +0 -0
  180. /package/dist/{chunk-O3MT7DYI.js.map → chunk-N6MNJNHB.js.map} +0 -0
  181. /package/dist/{chunk-A7KV5UK4.js.map → chunk-OXD5LFQP.js.map} +0 -0
  182. /package/dist/{chunk-22SG74BD.js.map → chunk-R6ZCSXKX.js.map} +0 -0
  183. /package/dist/{chunk-4S53H2KR.js.map → chunk-VAZXUK6G.js.map} +0 -0
  184. /package/dist/{chunk-TNX5ZGDJ.js.map → chunk-VNXAF2KE.js.map} +0 -0
  185. /package/dist/{chunk-EHRRQ4IC.js.map → chunk-VP3TIUDF.js.map} +0 -0
  186. /package/dist/{chunk-6XZYT4SW.js.map → chunk-WGDEBIP4.js.map} +0 -0
  187. /package/dist/{timeout-U5O4ESK3.js.map → timeout-BEABACRP.js.map} +0 -0
@@ -12,12 +12,12 @@ import {
12
12
  } from "./chunk-BXGZFGZ4.js";
13
13
 
14
14
  // src/utils/memory/index.ts
15
- import { monotonicNs, NodeImpl, node as node2, wallClockNs as wallClockNs2 } from "@graphrefly/pure-ts/core";
16
- import { fromTimer, keepalive as keepalive2, reactiveMap } from "@graphrefly/pure-ts/extra";
15
+ import { monotonicNs as monotonicNs2, NodeImpl, node as node9, wallClockNs as wallClockNs3 } from "@graphrefly/pure-ts/core";
16
+ import { fromTimer as fromTimer3, keepalive as keepalive6, reactiveMap } from "@graphrefly/pure-ts/extra";
17
17
  import { Graph as Graph2 } from "@graphrefly/pure-ts/graph";
18
18
 
19
19
  // src/utils/memory/fact-store.ts
20
- import { node, wallClockNs } from "@graphrefly/pure-ts/core";
20
+ import { monotonicNs, node, wallClockNs } from "@graphrefly/pure-ts/core";
21
21
  import { keepalive, reactiveLog } from "@graphrefly/pure-ts/extra";
22
22
  import { Graph } from "@graphrefly/pure-ts/graph";
23
23
  var OVERFLOW_SAMPLE_SIZE = 8;
@@ -446,6 +446,57 @@ function reactiveFactStore(config) {
446
446
  );
447
447
  graph.add(consolidated, { name: "consolidated" });
448
448
  graph.addDisposer(keepalive(consolidated));
449
+ if (config.decayTrigger) {
450
+ const decayProcessor = node(
451
+ config.decay ? [config.decayTrigger, config.decay] : [config.decayTrigger],
452
+ (batchData, actions, ctx) => {
453
+ const policy = config.decay ? lastOf(batchData[1], ctx.prevData[1]) : void 0;
454
+ if (!policy) {
455
+ actions.emit([]);
456
+ return;
457
+ }
458
+ const now = BigInt(monotonicNs());
459
+ const changes = [];
460
+ for (const f of allFacts().values()) {
461
+ if (f.validTo !== void 0) continue;
462
+ const ageNs = now - f.t_ns;
463
+ const raw = policy(f.confidence, ageNs);
464
+ if (!Number.isFinite(raw)) continue;
465
+ const next = raw < 0 ? 0 : raw > 1 ? 1 : raw;
466
+ if (next === f.confidence) continue;
467
+ changes.push({ id: f.id, next });
468
+ }
469
+ const decayed = [];
470
+ for (const { id, next } of changes) {
471
+ const idx = findShardOf(id);
472
+ const fs = idx < 0 ? void 0 : shards[idx].cache;
473
+ const live = fs?.byId.get(id);
474
+ if (!live || live.validTo !== void 0) continue;
475
+ replaceFragment(
476
+ id,
477
+ (prev) => prev.validTo !== void 0 ? prev : { ...prev, confidence: next }
478
+ );
479
+ decayed.push({ ...live, confidence: next });
480
+ }
481
+ if (decayed.length > 0) {
482
+ events.append({
483
+ action: "decay",
484
+ t_ns: wallClockNs(),
485
+ seq: bumpCursor(seqCursor)
486
+ });
487
+ }
488
+ actions.emit(decayed);
489
+ },
490
+ {
491
+ name: "decay_processor",
492
+ describeKind: "derived",
493
+ initial: [],
494
+ meta: factMeta("decay")
495
+ }
496
+ );
497
+ graph.add(decayProcessor, { name: "decay_processor" });
498
+ graph.addDisposer(keepalive(decayProcessor));
499
+ }
449
500
  const ingestAudit = node(
450
501
  [extractOp],
451
502
  (batchData, actions, ctx) => {
@@ -500,6 +551,427 @@ function reactiveFactStore(config) {
500
551
  return out;
501
552
  }
502
553
 
554
+ // src/utils/memory/persistent-fact-store.ts
555
+ import { COMPLETE, node as node2 } from "@graphrefly/pure-ts/core";
556
+ import {
557
+ appendLogStorage,
558
+ bigintJsonCodecFor,
559
+ fromAny,
560
+ keepalive as keepalive2
561
+ } from "@graphrefly/pure-ts/extra";
562
+ function persistMeta(kind) {
563
+ return domainMeta("memory", kind);
564
+ }
565
+ function persistentReactiveFactStore(config) {
566
+ const persistName = config.persistName ?? "fact_store_ingest";
567
+ const codec = config.codec ?? bigintJsonCodecFor();
568
+ const tier = appendLogStorage(config.storage, {
569
+ name: persistName,
570
+ codec
571
+ });
572
+ const store = reactiveFactStore({ ...config, recordIngest: true });
573
+ const ingestLog = store.ingestLog;
574
+ async function* loadHistory() {
575
+ if (typeof tier.loadEntries !== "function") return;
576
+ const page = await tier.loadEntries();
577
+ for (const f of page.entries) yield f;
578
+ }
579
+ const replaySource = fromAny(loadHistory(), {
580
+ name: "_replay_source",
581
+ meta: persistMeta("persist_replay_source")
582
+ });
583
+ store.add(replaySource, { name: "_replay_source" });
584
+ let replayed = 0;
585
+ const replayPump = node2(
586
+ [replaySource],
587
+ (batchData, actions) => {
588
+ const b = batchData[0];
589
+ if (b != null && b.length > 0) {
590
+ for (const f of b) {
591
+ config.ingest.emit(f);
592
+ replayed += 1;
593
+ }
594
+ actions.emit(replayed);
595
+ }
596
+ },
597
+ {
598
+ name: "_replay_pump",
599
+ describeKind: "derived",
600
+ initial: 0,
601
+ meta: persistMeta("persist_replay_pump")
602
+ }
603
+ );
604
+ store.add(replayPump, { name: "_replay_pump" });
605
+ store.addDisposer(keepalive2(replayPump));
606
+ const attached = node2([], {
607
+ initial: false,
608
+ name: "_storage_attached",
609
+ describeKind: "state",
610
+ meta: persistMeta("persist_attached")
611
+ });
612
+ store.add(attached, { name: "_storage_attached" });
613
+ store.addDisposer(keepalive2(attached));
614
+ let detachStorage;
615
+ const replaySub = replaySource.subscribe((msgs) => {
616
+ for (const m of msgs) {
617
+ if (m[0] === COMPLETE && detachStorage === void 0) {
618
+ const sizeAtAttach = ingestLog.size;
619
+ detachStorage = ingestLog.attachStorage([tier]);
620
+ if (sizeAtAttach > replayed) {
621
+ const slice = [];
622
+ for (let i = replayed; i < sizeAtAttach; i += 1) {
623
+ const v = ingestLog.at(i);
624
+ if (v === void 0) {
625
+ throw new Error(
626
+ `persistentReactiveFactStore: ingestLog hole at index ${i} in reconciliation slice [${replayed}, ${sizeAtAttach}); pre-attach-live durability cannot be guaranteed.`
627
+ );
628
+ }
629
+ slice.push(v);
630
+ }
631
+ if (slice.length > 0) {
632
+ const r = tier.appendEntries(slice);
633
+ if (r instanceof Promise) r.catch(() => {
634
+ });
635
+ }
636
+ }
637
+ attached.emit(true);
638
+ }
639
+ }
640
+ });
641
+ store.addDisposer(() => {
642
+ replaySub();
643
+ detachStorage?.();
644
+ });
645
+ const position = node2(
646
+ [ingestLog.entries, attached],
647
+ (batchData, actions, ctx) => {
648
+ const eb = batchData[0];
649
+ const arr = eb != null && eb.length > 0 ? eb.at(-1) : ctx.prevData[0];
650
+ const ab = batchData[1];
651
+ const isAttached = ab != null && ab.length > 0 ? ab.at(-1) : ctx.prevData[1];
652
+ actions.emit(isAttached === true ? arr?.length ?? 0 : 0);
653
+ },
654
+ {
655
+ name: "_durable_position",
656
+ describeKind: "derived",
657
+ initial: 0,
658
+ meta: persistMeta("persist_position")
659
+ }
660
+ );
661
+ store.add(position, { name: "_durable_position" });
662
+ store.addDisposer(keepalive2(position));
663
+ const out = Object.assign(store, {
664
+ position,
665
+ replayedCount: replayPump,
666
+ tier,
667
+ async flush() {
668
+ await tier.flush?.();
669
+ }
670
+ });
671
+ return out;
672
+ }
673
+
674
+ // src/utils/memory/recipes/admission-llm-judge.ts
675
+ import { node as node3 } from "@graphrefly/pure-ts/core";
676
+ function admissionLlmJudge(verdicts, opts = {}) {
677
+ const dflt = opts.defaultVerdict ?? false;
678
+ const buildFilter = (m) => (f) => m.get(f.id) ?? dflt;
679
+ return node3(
680
+ [verdicts],
681
+ (batchData, actions, ctx) => {
682
+ const m = batchData[0]?.at(-1) ?? ctx.prevData[0] ?? /* @__PURE__ */ new Map();
683
+ actions.emit(buildFilter(m));
684
+ },
685
+ {
686
+ name: opts.name ?? "admission_llm_judge",
687
+ describeKind: "derived",
688
+ // Before any verdict arrives, apply the default policy.
689
+ initial: buildFilter(/* @__PURE__ */ new Map())
690
+ }
691
+ );
692
+ }
693
+
694
+ // src/utils/memory/recipes/bitemporal-query.ts
695
+ import { node as node4 } from "@graphrefly/pure-ts/core";
696
+
697
+ // src/utils/memory/recipes/_shared.ts
698
+ function lastOf2(batch, prev) {
699
+ return batch != null && batch.length > 0 ? batch.at(-1) : prev;
700
+ }
701
+ function validAt(f, asOf) {
702
+ if (asOf === void 0) return f.validTo === void 0;
703
+ if (f.validFrom !== void 0 && asOf < f.validFrom) return false;
704
+ if (f.validTo !== void 0 && asOf >= f.validTo) return false;
705
+ return true;
706
+ }
707
+
708
+ // src/utils/memory/recipes/bitemporal-query.ts
709
+ function bitemporalQuery(mem, asOf, opts = {}) {
710
+ const asOfOrNull = node4(
711
+ [asOf],
712
+ (b, a, c) => a.emit(lastOf2(b[0], c.prevData[0]) ?? null),
713
+ { name: `${opts.name ?? "bitemporal_query"}_asof`, describeKind: "derived", initial: null }
714
+ );
715
+ return node4(
716
+ [asOfOrNull, mem.factStore],
717
+ (batchData, actions, ctx) => {
718
+ const raw = lastOf2(batchData[0], ctx.prevData[0]);
719
+ const at = raw ?? void 0;
720
+ const fs = lastOf2(batchData[1], ctx.prevData[1]);
721
+ if (fs == null) {
722
+ actions.emit([]);
723
+ return;
724
+ }
725
+ const results = [...fs.byId.values()].filter((f) => {
726
+ if (!validAt(f, at)) return false;
727
+ if (opts.tags && opts.tags.length > 0 && !opts.tags.some((t) => f.tags.includes(t))) {
728
+ return false;
729
+ }
730
+ if (opts.minConfidence !== void 0 && f.confidence < opts.minConfidence) return false;
731
+ return true;
732
+ });
733
+ results.sort((a, b) => b.confidence - a.confidence || Number(b.t_ns - a.t_ns));
734
+ actions.emit(results);
735
+ },
736
+ {
737
+ name: opts.name ?? "bitemporal_query",
738
+ describeKind: "derived",
739
+ initial: []
740
+ }
741
+ );
742
+ }
743
+
744
+ // src/utils/memory/recipes/consolidation-rem.ts
745
+ import { fromTimer } from "@graphrefly/pure-ts/extra";
746
+ function consolidationRem(opts) {
747
+ const consolidateTrigger = fromTimer(opts.periodMs, { period: opts.periodMs });
748
+ const consolidate = (store) => {
749
+ const live = [...store.values()].filter((f) => f.validTo === void 0);
750
+ if (live.length === 0) return [];
751
+ let pool = live;
752
+ if (opts.recentWindowNs !== void 0) {
753
+ const newest = live.reduce((m, f) => f.t_ns > m ? f.t_ns : m, live[0].t_ns);
754
+ const cutoff = newest - opts.recentWindowNs;
755
+ pool = live.filter((f) => f.t_ns >= cutoff);
756
+ }
757
+ pool.sort((a, b) => b.confidence - a.confidence || Number(b.t_ns - a.t_ns));
758
+ const replayed = pool.slice(0, Math.max(0, opts.topK));
759
+ return replayed.length > 0 ? opts.summarize(replayed) : [];
760
+ };
761
+ return { consolidateTrigger, consolidate };
762
+ }
763
+
764
+ // src/utils/memory/recipes/decay-exponential.ts
765
+ import { node as node5, wallClockNs as wallClockNs2 } from "@graphrefly/pure-ts/core";
766
+ import { fromTimer as fromTimer2, keepalive as keepalive3 } from "@graphrefly/pure-ts/extra";
767
+ function decayExponential(mem, ingest, opts) {
768
+ const floor = opts.floor ?? 0;
769
+ const epsilon = Math.max(opts.epsilon ?? 1e-4, Number.EPSILON);
770
+ const half = Number(opts.halfLifeNs);
771
+ const lastTick = /* @__PURE__ */ new Map();
772
+ const timer = fromTimer2(opts.periodMs, { period: opts.periodMs });
773
+ const driver = node5(
774
+ [timer],
775
+ (_batchData, actions) => {
776
+ const fs = mem.factStore.cache;
777
+ if (!fs) {
778
+ actions.emit([]);
779
+ return;
780
+ }
781
+ const now = BigInt(wallClockNs2());
782
+ const decayed = [];
783
+ const liveIds = /* @__PURE__ */ new Set();
784
+ for (const f of fs.byId.values()) {
785
+ liveIds.add(f.id);
786
+ if (f.validTo !== void 0) continue;
787
+ if (f.confidence <= floor) continue;
788
+ const lt = lastTick.get(f.id);
789
+ const since = lt !== void 0 && lt >= f.t_ns ? lt : f.t_ns;
790
+ const elapsed = Number(now - since);
791
+ if (elapsed <= 0) continue;
792
+ const factor = 0.5 ** (half > 0 ? elapsed / half : 0);
793
+ if (!Number.isFinite(factor)) continue;
794
+ let next = f.confidence * factor;
795
+ if (next < floor) next = floor;
796
+ if (f.confidence - next < epsilon) continue;
797
+ const liveNow = mem.factStore.cache?.byId.get(f.id);
798
+ if (liveNow && liveNow.validTo !== void 0) continue;
799
+ lastTick.set(f.id, now);
800
+ const drifted = { ...f, confidence: next };
801
+ decayed.push(drifted);
802
+ ingest.emit(drifted);
803
+ }
804
+ if (lastTick.size > liveIds.size) {
805
+ for (const id of lastTick.keys()) if (!liveIds.has(id)) lastTick.delete(id);
806
+ }
807
+ actions.emit(decayed);
808
+ },
809
+ {
810
+ name: opts.name ?? "decay_exponential",
811
+ describeKind: "derived",
812
+ initial: []
813
+ }
814
+ );
815
+ mem.add(driver, { name: opts.name ?? "decay_exponential" });
816
+ mem.addDisposer(keepalive3(timer));
817
+ mem.addDisposer(keepalive3(driver));
818
+ return driver;
819
+ }
820
+
821
+ // src/utils/memory/recipes/influence-analysis.ts
822
+ import { node as node6 } from "@graphrefly/pure-ts/core";
823
+ import { keepalive as keepalive4 } from "@graphrefly/pure-ts/extra";
824
+ function closureOf(index, root) {
825
+ const seen = /* @__PURE__ */ new Set();
826
+ const queue = [root];
827
+ while (queue.length > 0) {
828
+ const cur = queue.shift();
829
+ for (const dep of index.get(cur) ?? []) {
830
+ if (seen.has(dep) || dep === root) continue;
831
+ seen.add(dep);
832
+ queue.push(dep);
833
+ }
834
+ }
835
+ return [...seen];
836
+ }
837
+ function influenceAnalysis(mem, opts = {}) {
838
+ const prefix = opts.name ?? "influence";
839
+ const maxRanked = Math.max(1, opts.maxRanked ?? 64);
840
+ const ranked = node6(
841
+ [mem.dependentsIndex],
842
+ (batchData, actions, ctx) => {
843
+ const index = lastOf2(batchData[0], ctx.prevData[0]);
844
+ if (index == null) {
845
+ actions.emit([]);
846
+ return;
847
+ }
848
+ const rows = [];
849
+ for (const key of index.keys()) {
850
+ rows.push({ factId: key, influence: closureOf(index, key).length });
851
+ }
852
+ rows.sort((a, b) => b.influence - a.influence);
853
+ actions.emit(rows.slice(0, maxRanked));
854
+ },
855
+ {
856
+ name: `${prefix}_ranked`,
857
+ describeKind: "derived",
858
+ initial: []
859
+ }
860
+ );
861
+ mem.add(ranked, { name: `${prefix}_ranked` });
862
+ mem.addDisposer(keepalive4(ranked));
863
+ const builtFor = /* @__PURE__ */ new Map();
864
+ function influenceOf(rootId) {
865
+ const existing = builtFor.get(rootId);
866
+ if (existing) return existing;
867
+ const n = node6(
868
+ [mem.dependentsIndex],
869
+ (batchData, actions, ctx) => {
870
+ const index = lastOf2(batchData[0], ctx.prevData[0]);
871
+ actions.emit(index == null ? [] : closureOf(index, rootId));
872
+ },
873
+ {
874
+ name: `${prefix}_of_${rootId}`,
875
+ describeKind: "derived",
876
+ initial: []
877
+ }
878
+ );
879
+ mem.add(n, { name: `${prefix}_of_${rootId}` });
880
+ mem.addDisposer(keepalive4(n));
881
+ builtFor.set(rootId, n);
882
+ return n;
883
+ }
884
+ return { influenceOf, ranked };
885
+ }
886
+
887
+ // src/utils/memory/recipes/invalidation-tracer.ts
888
+ import { node as node7 } from "@graphrefly/pure-ts/core";
889
+ import { keepalive as keepalive5 } from "@graphrefly/pure-ts/extra";
890
+ function invalidationTracer(mem, opts = {}) {
891
+ const limit = Math.max(1, opts.limit ?? 256);
892
+ const ring = [];
893
+ const push = (e) => {
894
+ ring.push(e);
895
+ if (ring.length > limit) ring.splice(0, ring.length - limit);
896
+ };
897
+ const tracer = node7(
898
+ [mem.cascade, mem.cascadeOverflow],
899
+ (batchData, actions) => {
900
+ const cascadeWaves = batchData[0] ?? [];
901
+ for (const wave of cascadeWaves) {
902
+ for (const ev of wave) {
903
+ push({
904
+ kind: "cascade",
905
+ factId: ev.factId,
906
+ rootFactId: ev.rootFactId,
907
+ reason: ev.reason,
908
+ iteration: ev.iteration,
909
+ causalReason: ev.causalReason
910
+ });
911
+ }
912
+ }
913
+ const overflows = batchData[1] ?? [];
914
+ for (const ov of overflows) {
915
+ if (ov == null) continue;
916
+ push({
917
+ kind: "overflow",
918
+ factId: ov.sample[0] ?? "",
919
+ rootFactId: ov.rootFactId,
920
+ reason: "overflow",
921
+ causalReason: `cascade overflow: ${ov.droppedCount} dropped (root ${ov.rootFactId})`
922
+ });
923
+ }
924
+ actions.emit([...ring]);
925
+ },
926
+ {
927
+ name: opts.name ?? "invalidation_tracer",
928
+ describeKind: "derived",
929
+ initial: []
930
+ }
931
+ );
932
+ mem.add(tracer, { name: opts.name ?? "invalidation_tracer" });
933
+ mem.addDisposer(keepalive5(tracer));
934
+ return tracer;
935
+ }
936
+
937
+ // src/utils/memory/recipes/scoring-by-outcome.ts
938
+ import { node as node8 } from "@graphrefly/pure-ts/core";
939
+ var clamp01 = (n) => n < 0 ? 0 : n > 1 ? 1 : n;
940
+ function scoringByOutcome(outcomes, opts = {}) {
941
+ const base = opts.base ?? ((f) => f.confidence);
942
+ const learningRate = opts.learningRate ?? 1;
943
+ const acc = /* @__PURE__ */ new Map();
944
+ const buildPolicy = () => (fragment) => clamp01(base(fragment) + learningRate * (acc.get(fragment.id) ?? 0));
945
+ return node8(
946
+ [outcomes],
947
+ (batchData, actions) => {
948
+ const wave = batchData[0] ?? [];
949
+ for (const sig of wave) acc.set(sig.factId, (acc.get(sig.factId) ?? 0) + sig.reward);
950
+ actions.emit(buildPolicy());
951
+ },
952
+ {
953
+ name: opts.name ?? "scoring_by_outcome",
954
+ describeKind: "derived",
955
+ // Usable scorer before any outcome arrives (base-only).
956
+ initial: buildPolicy()
957
+ }
958
+ );
959
+ }
960
+
961
+ // src/utils/memory/recipes/shard-by-tenant.ts
962
+ function shardByTenant(tenantOf, opts = {}) {
963
+ if (opts.tenants && opts.tenants.length > 0) {
964
+ const idx = new Map(opts.tenants.map((t, i) => [t, i]));
965
+ const overflow = opts.tenants.length;
966
+ return {
967
+ shardBy: (f) => idx.get(tenantOf(f)) ?? overflow,
968
+ shardCount: opts.tenants.length + 1
969
+ };
970
+ }
971
+ const shardCount = Math.max(1, opts.shardCount ?? 4);
972
+ return { shardBy: (f) => tenantOf(f), shardCount };
973
+ }
974
+
503
975
  // src/utils/memory/index.ts
504
976
  var NS_PER_SEC = 1e9;
505
977
  function memoryMeta(kind, extra) {
@@ -507,7 +979,7 @@ function memoryMeta(kind, extra) {
507
979
  }
508
980
  function toNode(v, name) {
509
981
  if (v instanceof NodeImpl) return v;
510
- return node2([], { initial: v, ...name ? { name } : void 0 });
982
+ return node9([], { initial: v, ...name ? { name } : void 0 });
511
983
  }
512
984
  function ageSeconds(now, lastNs) {
513
985
  return (now - lastNs) / NS_PER_SEC;
@@ -567,7 +1039,7 @@ function collection(name, opts = {}) {
567
1039
  return scoreInput;
568
1040
  };
569
1041
  const graph = new Graph2(name);
570
- const retentionScore = (_k, v) => ranked ? decay(v.baseScore, ageSeconds(monotonicNs(), v.lastAccessNs), decayRate, minScore) : v.lastAccessNs;
1042
+ const retentionScore = (_k, v) => ranked ? decay(v.baseScore, ageSeconds(monotonicNs2(), v.lastAccessNs), decayRate, minScore) : v.lastAccessNs;
571
1043
  const items = reactiveMap({
572
1044
  name: "items",
573
1045
  ...maxSize !== void 0 ? { retention: { score: retentionScore, maxSize } } : {}
@@ -576,16 +1048,16 @@ function collection(name, opts = {}) {
576
1048
  let refreshTick;
577
1049
  if (ranked && decayRate > 0) {
578
1050
  const intervalMs = opts.refreshIntervalMs ?? Math.max(1, 1e3 * Math.LN2 / (10 * decayRate));
579
- const tickCounter = fromTimer(intervalMs, { period: intervalMs });
580
- refreshTick = node2(
1051
+ const tickCounter = fromTimer3(intervalMs, { period: intervalMs });
1052
+ refreshTick = node9(
581
1053
  [tickCounter],
582
1054
  (_batchData, actions) => {
583
- actions.emit(monotonicNs());
1055
+ actions.emit(monotonicNs2());
584
1056
  },
585
1057
  {
586
1058
  name: "refresh_tick_ns",
587
1059
  describeKind: "derived",
588
- initial: monotonicNs(),
1060
+ initial: monotonicNs2(),
589
1061
  meta: memoryMeta("clock")
590
1062
  }
591
1063
  );
@@ -596,7 +1068,7 @@ function collection(name, opts = {}) {
596
1068
  const rankedDeps = [items.entries];
597
1069
  if (refreshTick) rankedDeps.push(refreshTick);
598
1070
  if (scoreNode) rankedDeps.push(scoreNode);
599
- rankedNode = node2(
1071
+ rankedNode = node9(
600
1072
  rankedDeps,
601
1073
  (batchData, actions, ctx) => {
602
1074
  const values = batchData.map(
@@ -606,9 +1078,9 @@ function collection(name, opts = {}) {
606
1078
  let now;
607
1079
  if (refreshTick) {
608
1080
  const tickValue = values[1];
609
- now = typeof tickValue === "number" ? tickValue : monotonicNs();
1081
+ now = typeof tickValue === "number" ? tickValue : monotonicNs2();
610
1082
  } else {
611
- now = monotonicNs();
1083
+ now = monotonicNs2();
612
1084
  }
613
1085
  if (!snapshot || snapshot.size === 0) {
614
1086
  actions.emit([]);
@@ -633,7 +1105,7 @@ function collection(name, opts = {}) {
633
1105
  );
634
1106
  graph.add(rankedNode, { name: "ranked" });
635
1107
  } else {
636
- rankedNode = node2([], {
1108
+ rankedNode = node9([], {
637
1109
  initial: [],
638
1110
  name: "ranked",
639
1111
  describeKind: "state",
@@ -641,7 +1113,7 @@ function collection(name, opts = {}) {
641
1113
  });
642
1114
  graph.add(rankedNode, { name: "ranked" });
643
1115
  }
644
- const size = node2(
1116
+ const size = node9(
645
1117
  [items.entries],
646
1118
  (batchData, actions, ctx) => {
647
1119
  const data = batchData.map(
@@ -658,7 +1130,7 @@ function collection(name, opts = {}) {
658
1130
  }
659
1131
  );
660
1132
  graph.add(size, { name: "size" });
661
- graph.addDisposer(keepalive2(size));
1133
+ graph.addDisposer(keepalive6(size));
662
1134
  const events = createAuditLog({
663
1135
  name: "events",
664
1136
  retainedLimit: 1024,
@@ -666,7 +1138,7 @@ function collection(name, opts = {}) {
666
1138
  });
667
1139
  const seqCursor = registerCursor(graph, "seq", 0);
668
1140
  const upsertImpl = (id, value, _opts) => {
669
- const now = monotonicNs();
1141
+ const now = monotonicNs2();
670
1142
  const prev = items.get(id);
671
1143
  const baseScore = _opts?.score ?? readScoreFn()(value);
672
1144
  items.set(id, {
@@ -722,7 +1194,7 @@ function collection(name, opts = {}) {
722
1194
  });
723
1195
  function itemNode(id) {
724
1196
  const idN = toNode(id, "id");
725
- return node2(
1197
+ return node9(
726
1198
  [items.entries, idN],
727
1199
  (batchData, actions, ctx) => {
728
1200
  const data = batchData.map(
@@ -740,7 +1212,7 @@ function collection(name, opts = {}) {
740
1212
  }
741
1213
  function hasNode(id) {
742
1214
  const idN = toNode(id, "id");
743
- return node2(
1215
+ return node9(
744
1216
  [items.entries, idN],
745
1217
  (batchData, actions, ctx) => {
746
1218
  const data = batchData.map(
@@ -827,7 +1299,7 @@ function vectorIndex(opts = {}) {
827
1299
  events.append({
828
1300
  action: "evict",
829
1301
  id: key,
830
- t_ns: wallClockNs2(),
1302
+ t_ns: wallClockNs3(),
831
1303
  seq: bumpCursor(seqCursor)
832
1304
  });
833
1305
  }
@@ -835,7 +1307,7 @@ function vectorIndex(opts = {}) {
835
1307
  } : {}
836
1308
  });
837
1309
  graph.add(entries.entries, { name: "entries" });
838
- graph.addDisposer(keepalive2(entries.entries));
1310
+ graph.addDisposer(keepalive6(entries.entries));
839
1311
  if (hnsw?.dispose) {
840
1312
  const disposeAdapter = hnsw.dispose.bind(hnsw);
841
1313
  graph.addDisposer(() => disposeAdapter());
@@ -852,7 +1324,7 @@ function vectorIndex(opts = {}) {
852
1324
  id,
853
1325
  vector: [...vector],
854
1326
  ...copiedMeta !== void 0 ? { meta: copiedMeta } : {},
855
- upsertedAtNs: monotonicNs()
1327
+ upsertedAtNs: monotonicNs2()
856
1328
  };
857
1329
  entries.set(id, record);
858
1330
  };
@@ -908,7 +1380,7 @@ function vectorIndex(opts = {}) {
908
1380
  });
909
1381
  function searchNode(query, k = 5) {
910
1382
  const kN = toNode(k, "k");
911
- return node2(
1383
+ return node9(
912
1384
  [entries.entries, query, kN],
913
1385
  (batchData, actions, ctx) => {
914
1386
  const values = batchData.map(
@@ -1024,7 +1496,7 @@ function knowledgeGraph(name, opts = {}) {
1024
1496
  });
1025
1497
  graph.add(entitiesMap.entries, { name: "entities" });
1026
1498
  graph.add(edgesMap.entries, { name: "edges" });
1027
- const adjacencyOut = node2(
1499
+ const adjacencyOut = node9(
1028
1500
  [edgesMap.entries],
1029
1501
  (batchData, actions, ctx) => {
1030
1502
  const data = batchData.map(
@@ -1041,7 +1513,7 @@ function knowledgeGraph(name, opts = {}) {
1041
1513
  meta: memoryMeta("adjacency_out")
1042
1514
  }
1043
1515
  );
1044
- const adjacencyIn = node2(
1516
+ const adjacencyIn = node9(
1045
1517
  [edgesMap.entries],
1046
1518
  (batchData, actions, ctx) => {
1047
1519
  const data = batchData.map(
@@ -1060,9 +1532,9 @@ function knowledgeGraph(name, opts = {}) {
1060
1532
  );
1061
1533
  graph.add(adjacencyOut, { name: "adjacencyOut" });
1062
1534
  graph.add(adjacencyIn, { name: "adjacencyIn" });
1063
- graph.addDisposer(keepalive2(adjacencyOut));
1064
- graph.addDisposer(keepalive2(adjacencyIn));
1065
- const entityCount = node2(
1535
+ graph.addDisposer(keepalive6(adjacencyOut));
1536
+ graph.addDisposer(keepalive6(adjacencyIn));
1537
+ const entityCount = node9(
1066
1538
  [entitiesMap.entries],
1067
1539
  (batchData, actions, ctx) => {
1068
1540
  const data = batchData.map(
@@ -1073,7 +1545,7 @@ function knowledgeGraph(name, opts = {}) {
1073
1545
  },
1074
1546
  { name: "entityCount", describeKind: "derived", initial: 0, meta: memoryMeta("entity_count") }
1075
1547
  );
1076
- const edgeCount = node2(
1548
+ const edgeCount = node9(
1077
1549
  [edgesMap.entries],
1078
1550
  (batchData, actions, ctx) => {
1079
1551
  const data = batchData.map(
@@ -1086,8 +1558,8 @@ function knowledgeGraph(name, opts = {}) {
1086
1558
  );
1087
1559
  graph.add(entityCount, { name: "entityCount" });
1088
1560
  graph.add(edgeCount, { name: "edgeCount" });
1089
- graph.addDisposer(keepalive2(entityCount));
1090
- graph.addDisposer(keepalive2(edgeCount));
1561
+ graph.addDisposer(keepalive6(entityCount));
1562
+ graph.addDisposer(keepalive6(edgeCount));
1091
1563
  const events = createAuditLog({
1092
1564
  name: "events",
1093
1565
  retainedLimit: 1024,
@@ -1110,7 +1582,7 @@ function knowledgeGraph(name, opts = {}) {
1110
1582
  events.append({
1111
1583
  action: "orphanRemove",
1112
1584
  id: candidate,
1113
- t_ns: wallClockNs2(),
1585
+ t_ns: wallClockNs3(),
1114
1586
  seq: bumpCursor(seqCursor)
1115
1587
  });
1116
1588
  }
@@ -1205,7 +1677,7 @@ function knowledgeGraph(name, opts = {}) {
1205
1677
  const idN = toNode(id, "id");
1206
1678
  const relN = relation !== void 0 ? toNode(relation, "relation") : void 0;
1207
1679
  const deps = relN ? [adjacencyOut, adjacencyIn, idN, relN] : [adjacencyOut, adjacencyIn, idN];
1208
- return node2(
1680
+ return node9(
1209
1681
  deps,
1210
1682
  (batchData, actions, ctx) => {
1211
1683
  const values = batchData.map(
@@ -1274,9 +1746,18 @@ function knowledgeGraph(name, opts = {}) {
1274
1746
 
1275
1747
  export {
1276
1748
  reactiveFactStore,
1749
+ persistentReactiveFactStore,
1750
+ admissionLlmJudge,
1751
+ bitemporalQuery,
1752
+ consolidationRem,
1753
+ decayExponential,
1754
+ influenceAnalysis,
1755
+ invalidationTracer,
1756
+ scoringByOutcome,
1757
+ shardByTenant,
1277
1758
  cosineSimilarity,
1278
1759
  collection,
1279
1760
  vectorIndex,
1280
1761
  knowledgeGraph
1281
1762
  };
1282
- //# sourceMappingURL=chunk-ZVXXDWIB.js.map
1763
+ //# sourceMappingURL=chunk-7AVQIGF6.js.map