@graphrefly/graphrefly 0.46.0 → 0.47.1

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 (99) hide show
  1. package/dist/base/composition/index.cjs +69 -15
  2. package/dist/base/composition/index.cjs.map +1 -1
  3. package/dist/base/composition/index.d.cts +1 -2
  4. package/dist/base/composition/index.d.ts +1 -2
  5. package/dist/base/composition/index.js +1 -1
  6. package/dist/base/index.cjs +69 -15
  7. package/dist/base/index.cjs.map +1 -1
  8. package/dist/base/index.d.cts +1 -2
  9. package/dist/base/index.d.ts +1 -2
  10. package/dist/base/index.js +1 -1
  11. package/dist/{chunk-CGHORL6G.js → chunk-7ADWWI2T.js} +2 -2
  12. package/dist/{chunk-RGL53X5G.js → chunk-B4AKFXGE.js} +4 -4
  13. package/dist/{chunk-FW23JYNQ.js → chunk-CEVNQ74M.js} +2 -2
  14. package/dist/{chunk-JGFRAFDL.js → chunk-FVINAAKA.js} +3 -3
  15. package/dist/{chunk-WKSWLSCX.js → chunk-J5WFUEO4.js} +2 -2
  16. package/dist/{chunk-HULCUY35.js → chunk-K7PDZYQE.js} +4 -4
  17. package/dist/{chunk-Z6EGP5D7.js → chunk-LDCSZ72P.js} +2 -2
  18. package/dist/{chunk-KIIXR252.js → chunk-MTTRCEJT.js} +2 -2
  19. package/dist/{chunk-Q3EYOCZB.js → chunk-NPRP3MCV.js} +111 -2
  20. package/dist/chunk-NPRP3MCV.js.map +1 -0
  21. package/dist/{chunk-5THCXDWY.js → chunk-RGMTUZCL.js} +3 -3
  22. package/dist/{chunk-FR6RGA3B.js → chunk-U225SKB4.js} +472 -37
  23. package/dist/chunk-U225SKB4.js.map +1 -0
  24. package/dist/{chunk-GBCENOLN.js → chunk-V4Y3TM7U.js} +5 -5
  25. package/dist/{chunk-LBAJK24K.js → chunk-VLAGJZSL.js} +11 -3
  26. package/dist/chunk-VLAGJZSL.js.map +1 -0
  27. package/dist/{chunk-OO5BM6CJ.js → chunk-YXCPV26R.js} +2 -2
  28. package/dist/chunk-Z65DVDEQ.js +146 -0
  29. package/dist/chunk-Z65DVDEQ.js.map +1 -0
  30. package/dist/compat/index.cjs +156 -93
  31. package/dist/compat/index.cjs.map +1 -1
  32. package/dist/compat/index.d.cts +3 -3
  33. package/dist/compat/index.d.ts +3 -3
  34. package/dist/compat/index.js +2 -2
  35. package/dist/compat/nestjs/index.cjs +156 -93
  36. package/dist/compat/nestjs/index.cjs.map +1 -1
  37. package/dist/compat/nestjs/index.d.cts +4 -4
  38. package/dist/compat/nestjs/index.d.ts +4 -4
  39. package/dist/compat/nestjs/index.js +4 -5
  40. package/dist/{index-5SU_O78r.d.cts → index-B_p8tnvf.d.cts} +19 -3
  41. package/dist/{index-CEXCtYYJ.d.ts → index-_HDSmPyp.d.ts} +19 -3
  42. package/dist/index.cjs +1449 -856
  43. package/dist/index.cjs.map +1 -1
  44. package/dist/index.d.cts +3 -4
  45. package/dist/index.d.ts +3 -4
  46. package/dist/index.js +36 -14
  47. package/dist/index.js.map +1 -1
  48. package/dist/observable-B25XqCbZ.d.cts +59 -0
  49. package/dist/observable-B25XqCbZ.d.ts +59 -0
  50. package/dist/presets/ai/index.cjs.map +1 -1
  51. package/dist/presets/ai/index.js +6 -6
  52. package/dist/presets/harness/index.cjs.map +1 -1
  53. package/dist/presets/harness/index.js +9 -9
  54. package/dist/presets/index.cjs.map +1 -1
  55. package/dist/presets/index.js +13 -13
  56. package/dist/presets/inspect/index.cjs.map +1 -1
  57. package/dist/presets/inspect/index.js +4 -4
  58. package/dist/solutions/index.cjs.map +1 -1
  59. package/dist/solutions/index.js +10 -10
  60. package/dist/utils/ai/index.cjs.map +1 -1
  61. package/dist/utils/ai/index.js +5 -5
  62. package/dist/utils/index.cjs +939 -400
  63. package/dist/utils/index.cjs.map +1 -1
  64. package/dist/utils/index.d.cts +2 -2
  65. package/dist/utils/index.d.ts +2 -2
  66. package/dist/utils/index.js +28 -6
  67. package/dist/utils/inspect/index.cjs.map +1 -1
  68. package/dist/utils/inspect/index.js +2 -2
  69. package/dist/utils/memory/index.cjs +470 -40
  70. package/dist/utils/memory/index.cjs.map +1 -1
  71. package/dist/utils/memory/index.d.cts +669 -2
  72. package/dist/utils/memory/index.d.ts +669 -2
  73. package/dist/utils/memory/index.js +19 -1
  74. package/dist/utils/messaging/index.cjs +109 -0
  75. package/dist/utils/messaging/index.cjs.map +1 -1
  76. package/dist/utils/messaging/index.d.cts +115 -2
  77. package/dist/utils/messaging/index.d.ts +115 -2
  78. package/dist/utils/messaging/index.js +5 -1
  79. package/dist/utils/orchestration/index.cjs.map +1 -1
  80. package/dist/utils/orchestration/index.js +2 -2
  81. package/package.json +1 -5
  82. package/dist/chunk-3QZY5BI7.js +0 -92
  83. package/dist/chunk-3QZY5BI7.js.map +0 -1
  84. package/dist/chunk-FR6RGA3B.js.map +0 -1
  85. package/dist/chunk-LBAJK24K.js.map +0 -1
  86. package/dist/chunk-Q3EYOCZB.js.map +0 -1
  87. package/dist/observable-BXQoW1P-.d.cts +0 -36
  88. package/dist/observable-BXQoW1P-.d.ts +0 -36
  89. /package/dist/{chunk-CGHORL6G.js.map → chunk-7ADWWI2T.js.map} +0 -0
  90. /package/dist/{chunk-RGL53X5G.js.map → chunk-B4AKFXGE.js.map} +0 -0
  91. /package/dist/{chunk-FW23JYNQ.js.map → chunk-CEVNQ74M.js.map} +0 -0
  92. /package/dist/{chunk-JGFRAFDL.js.map → chunk-FVINAAKA.js.map} +0 -0
  93. /package/dist/{chunk-WKSWLSCX.js.map → chunk-J5WFUEO4.js.map} +0 -0
  94. /package/dist/{chunk-HULCUY35.js.map → chunk-K7PDZYQE.js.map} +0 -0
  95. /package/dist/{chunk-Z6EGP5D7.js.map → chunk-LDCSZ72P.js.map} +0 -0
  96. /package/dist/{chunk-KIIXR252.js.map → chunk-MTTRCEJT.js.map} +0 -0
  97. /package/dist/{chunk-5THCXDWY.js.map → chunk-RGMTUZCL.js.map} +0 -0
  98. /package/dist/{chunk-GBCENOLN.js.map → chunk-V4Y3TM7U.js.map} +0 -0
  99. /package/dist/{chunk-OO5BM6CJ.js.map → chunk-YXCPV26R.js.map} +0 -0
@@ -12,13 +12,13 @@ import {
12
12
  } from "./chunk-BXGZFGZ4.js";
13
13
 
14
14
  // src/utils/memory/index.ts
15
- import { monotonicNs as monotonicNs2, 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, 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 { monotonicNs, node, wallClockNs } from "@graphrefly/pure-ts/core";
21
- import { keepalive } from "@graphrefly/pure-ts/extra";
20
+ import { node, wallClockNs } from "@graphrefly/pure-ts/core";
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;
24
24
  function factMeta(kind, extra) {
@@ -65,6 +65,8 @@ function reactiveFactStore(config) {
65
65
  graph
66
66
  });
67
67
  const seqCursor = registerCursor(graph, "seq", 0);
68
+ const ingestLog = config.recordIngest ? reactiveLog([], { name: "ingest_log" }) : void 0;
69
+ if (ingestLog) graph.addDisposer(() => ingestLog.dispose());
68
70
  const emptyStore = () => ({ byId: /* @__PURE__ */ new Map() });
69
71
  const shards = [];
70
72
  for (let s = 0; s < shardCount; s += 1) {
@@ -220,6 +222,8 @@ function reactiveFactStore(config) {
220
222
  factId: dep,
221
223
  rootFactId: f.id,
222
224
  reason: "obsolete",
225
+ // `obsolete` guard above guarantees `f.validTo` is set.
226
+ rootValidTo: f.validTo,
223
227
  iteration: cascadeIteration + 1,
224
228
  causalReason: `dependentsIndex[${f.id}] \u2192 ${dep} (obsolete: validTo set)`
225
229
  });
@@ -292,11 +296,10 @@ function reactiveFactStore(config) {
292
296
  actions.emit([]);
293
297
  return;
294
298
  }
295
- const now = monotonicNs();
296
- for (const [id] of byId) {
299
+ for (const [id, e] of byId) {
297
300
  replaceFragment(
298
301
  id,
299
- (prev) => prev.validTo !== void 0 ? prev : { ...prev, validTo: BigInt(now) }
302
+ (prev) => prev.validTo !== void 0 ? prev : { ...prev, validTo: e.rootValidTo }
300
303
  );
301
304
  }
302
305
  actions.emit([...byId.values()]);
@@ -454,6 +457,7 @@ function reactiveFactStore(config) {
454
457
  t_ns: wallClockNs(),
455
458
  seq: bumpCursor(seqCursor)
456
459
  });
460
+ ingestLog?.append(f);
457
461
  }
458
462
  actions.emit(f ?? null);
459
463
  },
@@ -490,11 +494,433 @@ function reactiveFactStore(config) {
490
494
  review,
491
495
  consolidated,
492
496
  events,
497
+ ...ingestLog ? { ingestLog } : {},
493
498
  itemNode
494
499
  });
495
500
  return out;
496
501
  }
497
502
 
503
+ // src/utils/memory/persistent-fact-store.ts
504
+ import { COMPLETE, node as node2 } from "@graphrefly/pure-ts/core";
505
+ import {
506
+ appendLogStorage,
507
+ bigintJsonCodecFor,
508
+ fromAny,
509
+ keepalive as keepalive2
510
+ } from "@graphrefly/pure-ts/extra";
511
+ function persistMeta(kind) {
512
+ return domainMeta("memory", kind);
513
+ }
514
+ function persistentReactiveFactStore(config) {
515
+ const persistName = config.persistName ?? "fact_store_ingest";
516
+ const codec = config.codec ?? bigintJsonCodecFor();
517
+ const tier = appendLogStorage(config.storage, {
518
+ name: persistName,
519
+ codec
520
+ });
521
+ const store = reactiveFactStore({ ...config, recordIngest: true });
522
+ const ingestLog = store.ingestLog;
523
+ async function* loadHistory() {
524
+ if (typeof tier.loadEntries !== "function") return;
525
+ const page = await tier.loadEntries();
526
+ for (const f of page.entries) yield f;
527
+ }
528
+ const replaySource = fromAny(loadHistory(), {
529
+ name: "_replay_source",
530
+ meta: persistMeta("persist_replay_source")
531
+ });
532
+ store.add(replaySource, { name: "_replay_source" });
533
+ let replayed = 0;
534
+ const replayPump = node2(
535
+ [replaySource],
536
+ (batchData, actions) => {
537
+ const b = batchData[0];
538
+ if (b != null && b.length > 0) {
539
+ for (const f of b) {
540
+ config.ingest.emit(f);
541
+ replayed += 1;
542
+ }
543
+ actions.emit(replayed);
544
+ }
545
+ },
546
+ {
547
+ name: "_replay_pump",
548
+ describeKind: "derived",
549
+ initial: 0,
550
+ meta: persistMeta("persist_replay_pump")
551
+ }
552
+ );
553
+ store.add(replayPump, { name: "_replay_pump" });
554
+ store.addDisposer(keepalive2(replayPump));
555
+ const attached = node2([], {
556
+ initial: false,
557
+ name: "_storage_attached",
558
+ describeKind: "state",
559
+ meta: persistMeta("persist_attached")
560
+ });
561
+ store.add(attached, { name: "_storage_attached" });
562
+ store.addDisposer(keepalive2(attached));
563
+ let detachStorage;
564
+ const replaySub = replaySource.subscribe((msgs) => {
565
+ for (const m of msgs) {
566
+ if (m[0] === COMPLETE && detachStorage === void 0) {
567
+ const sizeAtAttach = ingestLog.size;
568
+ detachStorage = ingestLog.attachStorage([tier]);
569
+ if (sizeAtAttach > replayed) {
570
+ const slice = [];
571
+ for (let i = replayed; i < sizeAtAttach; i += 1) {
572
+ const v = ingestLog.at(i);
573
+ if (v === void 0) {
574
+ throw new Error(
575
+ `persistentReactiveFactStore: ingestLog hole at index ${i} in reconciliation slice [${replayed}, ${sizeAtAttach}); pre-attach-live durability cannot be guaranteed.`
576
+ );
577
+ }
578
+ slice.push(v);
579
+ }
580
+ if (slice.length > 0) {
581
+ const r = tier.appendEntries(slice);
582
+ if (r instanceof Promise) r.catch(() => {
583
+ });
584
+ }
585
+ }
586
+ attached.emit(true);
587
+ }
588
+ }
589
+ });
590
+ store.addDisposer(() => {
591
+ replaySub();
592
+ detachStorage?.();
593
+ });
594
+ const position = node2(
595
+ [ingestLog.entries, attached],
596
+ (batchData, actions, ctx) => {
597
+ const eb = batchData[0];
598
+ const arr = eb != null && eb.length > 0 ? eb.at(-1) : ctx.prevData[0];
599
+ const ab = batchData[1];
600
+ const isAttached = ab != null && ab.length > 0 ? ab.at(-1) : ctx.prevData[1];
601
+ actions.emit(isAttached === true ? arr?.length ?? 0 : 0);
602
+ },
603
+ {
604
+ name: "_durable_position",
605
+ describeKind: "derived",
606
+ initial: 0,
607
+ meta: persistMeta("persist_position")
608
+ }
609
+ );
610
+ store.add(position, { name: "_durable_position" });
611
+ store.addDisposer(keepalive2(position));
612
+ const out = Object.assign(store, {
613
+ position,
614
+ replayedCount: replayPump,
615
+ tier,
616
+ async flush() {
617
+ await tier.flush?.();
618
+ }
619
+ });
620
+ return out;
621
+ }
622
+
623
+ // src/utils/memory/recipes/admission-llm-judge.ts
624
+ import { node as node3 } from "@graphrefly/pure-ts/core";
625
+ function admissionLlmJudge(verdicts, opts = {}) {
626
+ const dflt = opts.defaultVerdict ?? false;
627
+ const buildFilter = (m) => (f) => m.get(f.id) ?? dflt;
628
+ return node3(
629
+ [verdicts],
630
+ (batchData, actions, ctx) => {
631
+ const m = batchData[0]?.at(-1) ?? ctx.prevData[0] ?? /* @__PURE__ */ new Map();
632
+ actions.emit(buildFilter(m));
633
+ },
634
+ {
635
+ name: opts.name ?? "admission_llm_judge",
636
+ describeKind: "derived",
637
+ // Before any verdict arrives, apply the default policy.
638
+ initial: buildFilter(/* @__PURE__ */ new Map())
639
+ }
640
+ );
641
+ }
642
+
643
+ // src/utils/memory/recipes/bitemporal-query.ts
644
+ import { node as node4 } from "@graphrefly/pure-ts/core";
645
+
646
+ // src/utils/memory/recipes/_shared.ts
647
+ function lastOf2(batch, prev) {
648
+ return batch != null && batch.length > 0 ? batch.at(-1) : prev;
649
+ }
650
+ function validAt(f, asOf) {
651
+ if (asOf === void 0) return f.validTo === void 0;
652
+ if (f.validFrom !== void 0 && asOf < f.validFrom) return false;
653
+ if (f.validTo !== void 0 && asOf >= f.validTo) return false;
654
+ return true;
655
+ }
656
+
657
+ // src/utils/memory/recipes/bitemporal-query.ts
658
+ function bitemporalQuery(mem, asOf, opts = {}) {
659
+ const asOfOrNull = node4(
660
+ [asOf],
661
+ (b, a, c) => a.emit(lastOf2(b[0], c.prevData[0]) ?? null),
662
+ { name: `${opts.name ?? "bitemporal_query"}_asof`, describeKind: "derived", initial: null }
663
+ );
664
+ return node4(
665
+ [asOfOrNull, mem.factStore],
666
+ (batchData, actions, ctx) => {
667
+ const raw = lastOf2(batchData[0], ctx.prevData[0]);
668
+ const at = raw ?? void 0;
669
+ const fs = lastOf2(batchData[1], ctx.prevData[1]);
670
+ if (fs == null) {
671
+ actions.emit([]);
672
+ return;
673
+ }
674
+ const results = [...fs.byId.values()].filter((f) => {
675
+ if (!validAt(f, at)) return false;
676
+ if (opts.tags && opts.tags.length > 0 && !opts.tags.some((t) => f.tags.includes(t))) {
677
+ return false;
678
+ }
679
+ if (opts.minConfidence !== void 0 && f.confidence < opts.minConfidence) return false;
680
+ return true;
681
+ });
682
+ results.sort((a, b) => b.confidence - a.confidence || Number(b.t_ns - a.t_ns));
683
+ actions.emit(results);
684
+ },
685
+ {
686
+ name: opts.name ?? "bitemporal_query",
687
+ describeKind: "derived",
688
+ initial: []
689
+ }
690
+ );
691
+ }
692
+
693
+ // src/utils/memory/recipes/consolidation-rem.ts
694
+ import { fromTimer } from "@graphrefly/pure-ts/extra";
695
+ function consolidationRem(opts) {
696
+ const consolidateTrigger = fromTimer(opts.periodMs, { period: opts.periodMs });
697
+ const consolidate = (store) => {
698
+ const live = [...store.values()].filter((f) => f.validTo === void 0);
699
+ if (live.length === 0) return [];
700
+ let pool = live;
701
+ if (opts.recentWindowNs !== void 0) {
702
+ const newest = live.reduce((m, f) => f.t_ns > m ? f.t_ns : m, live[0].t_ns);
703
+ const cutoff = newest - opts.recentWindowNs;
704
+ pool = live.filter((f) => f.t_ns >= cutoff);
705
+ }
706
+ pool.sort((a, b) => b.confidence - a.confidence || Number(b.t_ns - a.t_ns));
707
+ const replayed = pool.slice(0, Math.max(0, opts.topK));
708
+ return replayed.length > 0 ? opts.summarize(replayed) : [];
709
+ };
710
+ return { consolidateTrigger, consolidate };
711
+ }
712
+
713
+ // src/utils/memory/recipes/decay-exponential.ts
714
+ import { node as node5, wallClockNs as wallClockNs2 } from "@graphrefly/pure-ts/core";
715
+ import { fromTimer as fromTimer2, keepalive as keepalive3 } from "@graphrefly/pure-ts/extra";
716
+ function decayExponential(mem, ingest, opts) {
717
+ const floor = opts.floor ?? 0;
718
+ const epsilon = Math.max(opts.epsilon ?? 1e-4, Number.EPSILON);
719
+ const half = Number(opts.halfLifeNs);
720
+ const lastTick = /* @__PURE__ */ new Map();
721
+ const timer = fromTimer2(opts.periodMs, { period: opts.periodMs });
722
+ const driver = node5(
723
+ [timer],
724
+ (_batchData, actions) => {
725
+ const fs = mem.factStore.cache;
726
+ if (!fs) {
727
+ actions.emit([]);
728
+ return;
729
+ }
730
+ const now = BigInt(wallClockNs2());
731
+ const decayed = [];
732
+ const liveIds = /* @__PURE__ */ new Set();
733
+ for (const f of fs.byId.values()) {
734
+ liveIds.add(f.id);
735
+ if (f.validTo !== void 0) continue;
736
+ if (f.confidence <= floor) continue;
737
+ const lt = lastTick.get(f.id);
738
+ const since = lt !== void 0 && lt >= f.t_ns ? lt : f.t_ns;
739
+ const elapsed = Number(now - since);
740
+ if (elapsed <= 0) continue;
741
+ const factor = 0.5 ** (half > 0 ? elapsed / half : 0);
742
+ if (!Number.isFinite(factor)) continue;
743
+ let next = f.confidence * factor;
744
+ if (next < floor) next = floor;
745
+ if (f.confidence - next < epsilon) continue;
746
+ const liveNow = mem.factStore.cache?.byId.get(f.id);
747
+ if (liveNow && liveNow.validTo !== void 0) continue;
748
+ lastTick.set(f.id, now);
749
+ const drifted = { ...f, confidence: next };
750
+ decayed.push(drifted);
751
+ ingest.emit(drifted);
752
+ }
753
+ if (lastTick.size > liveIds.size) {
754
+ for (const id of lastTick.keys()) if (!liveIds.has(id)) lastTick.delete(id);
755
+ }
756
+ actions.emit(decayed);
757
+ },
758
+ {
759
+ name: opts.name ?? "decay_exponential",
760
+ describeKind: "derived",
761
+ initial: []
762
+ }
763
+ );
764
+ mem.add(driver, { name: opts.name ?? "decay_exponential" });
765
+ mem.addDisposer(keepalive3(timer));
766
+ mem.addDisposer(keepalive3(driver));
767
+ return driver;
768
+ }
769
+
770
+ // src/utils/memory/recipes/influence-analysis.ts
771
+ import { node as node6 } from "@graphrefly/pure-ts/core";
772
+ import { keepalive as keepalive4 } from "@graphrefly/pure-ts/extra";
773
+ function closureOf(index, root) {
774
+ const seen = /* @__PURE__ */ new Set();
775
+ const queue = [root];
776
+ while (queue.length > 0) {
777
+ const cur = queue.shift();
778
+ for (const dep of index.get(cur) ?? []) {
779
+ if (seen.has(dep) || dep === root) continue;
780
+ seen.add(dep);
781
+ queue.push(dep);
782
+ }
783
+ }
784
+ return [...seen];
785
+ }
786
+ function influenceAnalysis(mem, opts = {}) {
787
+ const prefix = opts.name ?? "influence";
788
+ const maxRanked = Math.max(1, opts.maxRanked ?? 64);
789
+ const ranked = node6(
790
+ [mem.dependentsIndex],
791
+ (batchData, actions, ctx) => {
792
+ const index = lastOf2(batchData[0], ctx.prevData[0]);
793
+ if (index == null) {
794
+ actions.emit([]);
795
+ return;
796
+ }
797
+ const rows = [];
798
+ for (const key of index.keys()) {
799
+ rows.push({ factId: key, influence: closureOf(index, key).length });
800
+ }
801
+ rows.sort((a, b) => b.influence - a.influence);
802
+ actions.emit(rows.slice(0, maxRanked));
803
+ },
804
+ {
805
+ name: `${prefix}_ranked`,
806
+ describeKind: "derived",
807
+ initial: []
808
+ }
809
+ );
810
+ mem.add(ranked, { name: `${prefix}_ranked` });
811
+ mem.addDisposer(keepalive4(ranked));
812
+ const builtFor = /* @__PURE__ */ new Map();
813
+ function influenceOf(rootId) {
814
+ const existing = builtFor.get(rootId);
815
+ if (existing) return existing;
816
+ const n = node6(
817
+ [mem.dependentsIndex],
818
+ (batchData, actions, ctx) => {
819
+ const index = lastOf2(batchData[0], ctx.prevData[0]);
820
+ actions.emit(index == null ? [] : closureOf(index, rootId));
821
+ },
822
+ {
823
+ name: `${prefix}_of_${rootId}`,
824
+ describeKind: "derived",
825
+ initial: []
826
+ }
827
+ );
828
+ mem.add(n, { name: `${prefix}_of_${rootId}` });
829
+ mem.addDisposer(keepalive4(n));
830
+ builtFor.set(rootId, n);
831
+ return n;
832
+ }
833
+ return { influenceOf, ranked };
834
+ }
835
+
836
+ // src/utils/memory/recipes/invalidation-tracer.ts
837
+ import { node as node7 } from "@graphrefly/pure-ts/core";
838
+ import { keepalive as keepalive5 } from "@graphrefly/pure-ts/extra";
839
+ function invalidationTracer(mem, opts = {}) {
840
+ const limit = Math.max(1, opts.limit ?? 256);
841
+ const ring = [];
842
+ const push = (e) => {
843
+ ring.push(e);
844
+ if (ring.length > limit) ring.splice(0, ring.length - limit);
845
+ };
846
+ const tracer = node7(
847
+ [mem.cascade, mem.cascadeOverflow],
848
+ (batchData, actions) => {
849
+ const cascadeWaves = batchData[0] ?? [];
850
+ for (const wave of cascadeWaves) {
851
+ for (const ev of wave) {
852
+ push({
853
+ kind: "cascade",
854
+ factId: ev.factId,
855
+ rootFactId: ev.rootFactId,
856
+ reason: ev.reason,
857
+ iteration: ev.iteration,
858
+ causalReason: ev.causalReason
859
+ });
860
+ }
861
+ }
862
+ const overflows = batchData[1] ?? [];
863
+ for (const ov of overflows) {
864
+ if (ov == null) continue;
865
+ push({
866
+ kind: "overflow",
867
+ factId: ov.sample[0] ?? "",
868
+ rootFactId: ov.rootFactId,
869
+ reason: "overflow",
870
+ causalReason: `cascade overflow: ${ov.droppedCount} dropped (root ${ov.rootFactId})`
871
+ });
872
+ }
873
+ actions.emit([...ring]);
874
+ },
875
+ {
876
+ name: opts.name ?? "invalidation_tracer",
877
+ describeKind: "derived",
878
+ initial: []
879
+ }
880
+ );
881
+ mem.add(tracer, { name: opts.name ?? "invalidation_tracer" });
882
+ mem.addDisposer(keepalive5(tracer));
883
+ return tracer;
884
+ }
885
+
886
+ // src/utils/memory/recipes/scoring-by-outcome.ts
887
+ import { node as node8 } from "@graphrefly/pure-ts/core";
888
+ var clamp01 = (n) => n < 0 ? 0 : n > 1 ? 1 : n;
889
+ function scoringByOutcome(outcomes, opts = {}) {
890
+ const base = opts.base ?? ((f) => f.confidence);
891
+ const learningRate = opts.learningRate ?? 1;
892
+ const acc = /* @__PURE__ */ new Map();
893
+ const buildPolicy = () => (fragment) => clamp01(base(fragment) + learningRate * (acc.get(fragment.id) ?? 0));
894
+ return node8(
895
+ [outcomes],
896
+ (batchData, actions) => {
897
+ const wave = batchData[0] ?? [];
898
+ for (const sig of wave) acc.set(sig.factId, (acc.get(sig.factId) ?? 0) + sig.reward);
899
+ actions.emit(buildPolicy());
900
+ },
901
+ {
902
+ name: opts.name ?? "scoring_by_outcome",
903
+ describeKind: "derived",
904
+ // Usable scorer before any outcome arrives (base-only).
905
+ initial: buildPolicy()
906
+ }
907
+ );
908
+ }
909
+
910
+ // src/utils/memory/recipes/shard-by-tenant.ts
911
+ function shardByTenant(tenantOf, opts = {}) {
912
+ if (opts.tenants && opts.tenants.length > 0) {
913
+ const idx = new Map(opts.tenants.map((t, i) => [t, i]));
914
+ const overflow = opts.tenants.length;
915
+ return {
916
+ shardBy: (f) => idx.get(tenantOf(f)) ?? overflow,
917
+ shardCount: opts.tenants.length + 1
918
+ };
919
+ }
920
+ const shardCount = Math.max(1, opts.shardCount ?? 4);
921
+ return { shardBy: (f) => tenantOf(f), shardCount };
922
+ }
923
+
498
924
  // src/utils/memory/index.ts
499
925
  var NS_PER_SEC = 1e9;
500
926
  function memoryMeta(kind, extra) {
@@ -502,7 +928,7 @@ function memoryMeta(kind, extra) {
502
928
  }
503
929
  function toNode(v, name) {
504
930
  if (v instanceof NodeImpl) return v;
505
- return node2([], { initial: v, ...name ? { name } : void 0 });
931
+ return node9([], { initial: v, ...name ? { name } : void 0 });
506
932
  }
507
933
  function ageSeconds(now, lastNs) {
508
934
  return (now - lastNs) / NS_PER_SEC;
@@ -562,7 +988,7 @@ function collection(name, opts = {}) {
562
988
  return scoreInput;
563
989
  };
564
990
  const graph = new Graph2(name);
565
- const retentionScore = (_k, v) => ranked ? decay(v.baseScore, ageSeconds(monotonicNs2(), v.lastAccessNs), decayRate, minScore) : v.lastAccessNs;
991
+ const retentionScore = (_k, v) => ranked ? decay(v.baseScore, ageSeconds(monotonicNs(), v.lastAccessNs), decayRate, minScore) : v.lastAccessNs;
566
992
  const items = reactiveMap({
567
993
  name: "items",
568
994
  ...maxSize !== void 0 ? { retention: { score: retentionScore, maxSize } } : {}
@@ -571,16 +997,16 @@ function collection(name, opts = {}) {
571
997
  let refreshTick;
572
998
  if (ranked && decayRate > 0) {
573
999
  const intervalMs = opts.refreshIntervalMs ?? Math.max(1, 1e3 * Math.LN2 / (10 * decayRate));
574
- const tickCounter = fromTimer(intervalMs, { period: intervalMs });
575
- refreshTick = node2(
1000
+ const tickCounter = fromTimer3(intervalMs, { period: intervalMs });
1001
+ refreshTick = node9(
576
1002
  [tickCounter],
577
1003
  (_batchData, actions) => {
578
- actions.emit(monotonicNs2());
1004
+ actions.emit(monotonicNs());
579
1005
  },
580
1006
  {
581
1007
  name: "refresh_tick_ns",
582
1008
  describeKind: "derived",
583
- initial: monotonicNs2(),
1009
+ initial: monotonicNs(),
584
1010
  meta: memoryMeta("clock")
585
1011
  }
586
1012
  );
@@ -591,7 +1017,7 @@ function collection(name, opts = {}) {
591
1017
  const rankedDeps = [items.entries];
592
1018
  if (refreshTick) rankedDeps.push(refreshTick);
593
1019
  if (scoreNode) rankedDeps.push(scoreNode);
594
- rankedNode = node2(
1020
+ rankedNode = node9(
595
1021
  rankedDeps,
596
1022
  (batchData, actions, ctx) => {
597
1023
  const values = batchData.map(
@@ -601,9 +1027,9 @@ function collection(name, opts = {}) {
601
1027
  let now;
602
1028
  if (refreshTick) {
603
1029
  const tickValue = values[1];
604
- now = typeof tickValue === "number" ? tickValue : monotonicNs2();
1030
+ now = typeof tickValue === "number" ? tickValue : monotonicNs();
605
1031
  } else {
606
- now = monotonicNs2();
1032
+ now = monotonicNs();
607
1033
  }
608
1034
  if (!snapshot || snapshot.size === 0) {
609
1035
  actions.emit([]);
@@ -628,7 +1054,7 @@ function collection(name, opts = {}) {
628
1054
  );
629
1055
  graph.add(rankedNode, { name: "ranked" });
630
1056
  } else {
631
- rankedNode = node2([], {
1057
+ rankedNode = node9([], {
632
1058
  initial: [],
633
1059
  name: "ranked",
634
1060
  describeKind: "state",
@@ -636,7 +1062,7 @@ function collection(name, opts = {}) {
636
1062
  });
637
1063
  graph.add(rankedNode, { name: "ranked" });
638
1064
  }
639
- const size = node2(
1065
+ const size = node9(
640
1066
  [items.entries],
641
1067
  (batchData, actions, ctx) => {
642
1068
  const data = batchData.map(
@@ -653,7 +1079,7 @@ function collection(name, opts = {}) {
653
1079
  }
654
1080
  );
655
1081
  graph.add(size, { name: "size" });
656
- graph.addDisposer(keepalive2(size));
1082
+ graph.addDisposer(keepalive6(size));
657
1083
  const events = createAuditLog({
658
1084
  name: "events",
659
1085
  retainedLimit: 1024,
@@ -661,7 +1087,7 @@ function collection(name, opts = {}) {
661
1087
  });
662
1088
  const seqCursor = registerCursor(graph, "seq", 0);
663
1089
  const upsertImpl = (id, value, _opts) => {
664
- const now = monotonicNs2();
1090
+ const now = monotonicNs();
665
1091
  const prev = items.get(id);
666
1092
  const baseScore = _opts?.score ?? readScoreFn()(value);
667
1093
  items.set(id, {
@@ -717,7 +1143,7 @@ function collection(name, opts = {}) {
717
1143
  });
718
1144
  function itemNode(id) {
719
1145
  const idN = toNode(id, "id");
720
- return node2(
1146
+ return node9(
721
1147
  [items.entries, idN],
722
1148
  (batchData, actions, ctx) => {
723
1149
  const data = batchData.map(
@@ -735,7 +1161,7 @@ function collection(name, opts = {}) {
735
1161
  }
736
1162
  function hasNode(id) {
737
1163
  const idN = toNode(id, "id");
738
- return node2(
1164
+ return node9(
739
1165
  [items.entries, idN],
740
1166
  (batchData, actions, ctx) => {
741
1167
  const data = batchData.map(
@@ -822,7 +1248,7 @@ function vectorIndex(opts = {}) {
822
1248
  events.append({
823
1249
  action: "evict",
824
1250
  id: key,
825
- t_ns: wallClockNs2(),
1251
+ t_ns: wallClockNs3(),
826
1252
  seq: bumpCursor(seqCursor)
827
1253
  });
828
1254
  }
@@ -830,7 +1256,7 @@ function vectorIndex(opts = {}) {
830
1256
  } : {}
831
1257
  });
832
1258
  graph.add(entries.entries, { name: "entries" });
833
- graph.addDisposer(keepalive2(entries.entries));
1259
+ graph.addDisposer(keepalive6(entries.entries));
834
1260
  if (hnsw?.dispose) {
835
1261
  const disposeAdapter = hnsw.dispose.bind(hnsw);
836
1262
  graph.addDisposer(() => disposeAdapter());
@@ -847,7 +1273,7 @@ function vectorIndex(opts = {}) {
847
1273
  id,
848
1274
  vector: [...vector],
849
1275
  ...copiedMeta !== void 0 ? { meta: copiedMeta } : {},
850
- upsertedAtNs: monotonicNs2()
1276
+ upsertedAtNs: monotonicNs()
851
1277
  };
852
1278
  entries.set(id, record);
853
1279
  };
@@ -903,7 +1329,7 @@ function vectorIndex(opts = {}) {
903
1329
  });
904
1330
  function searchNode(query, k = 5) {
905
1331
  const kN = toNode(k, "k");
906
- return node2(
1332
+ return node9(
907
1333
  [entries.entries, query, kN],
908
1334
  (batchData, actions, ctx) => {
909
1335
  const values = batchData.map(
@@ -1019,7 +1445,7 @@ function knowledgeGraph(name, opts = {}) {
1019
1445
  });
1020
1446
  graph.add(entitiesMap.entries, { name: "entities" });
1021
1447
  graph.add(edgesMap.entries, { name: "edges" });
1022
- const adjacencyOut = node2(
1448
+ const adjacencyOut = node9(
1023
1449
  [edgesMap.entries],
1024
1450
  (batchData, actions, ctx) => {
1025
1451
  const data = batchData.map(
@@ -1036,7 +1462,7 @@ function knowledgeGraph(name, opts = {}) {
1036
1462
  meta: memoryMeta("adjacency_out")
1037
1463
  }
1038
1464
  );
1039
- const adjacencyIn = node2(
1465
+ const adjacencyIn = node9(
1040
1466
  [edgesMap.entries],
1041
1467
  (batchData, actions, ctx) => {
1042
1468
  const data = batchData.map(
@@ -1055,9 +1481,9 @@ function knowledgeGraph(name, opts = {}) {
1055
1481
  );
1056
1482
  graph.add(adjacencyOut, { name: "adjacencyOut" });
1057
1483
  graph.add(adjacencyIn, { name: "adjacencyIn" });
1058
- graph.addDisposer(keepalive2(adjacencyOut));
1059
- graph.addDisposer(keepalive2(adjacencyIn));
1060
- const entityCount = node2(
1484
+ graph.addDisposer(keepalive6(adjacencyOut));
1485
+ graph.addDisposer(keepalive6(adjacencyIn));
1486
+ const entityCount = node9(
1061
1487
  [entitiesMap.entries],
1062
1488
  (batchData, actions, ctx) => {
1063
1489
  const data = batchData.map(
@@ -1068,7 +1494,7 @@ function knowledgeGraph(name, opts = {}) {
1068
1494
  },
1069
1495
  { name: "entityCount", describeKind: "derived", initial: 0, meta: memoryMeta("entity_count") }
1070
1496
  );
1071
- const edgeCount = node2(
1497
+ const edgeCount = node9(
1072
1498
  [edgesMap.entries],
1073
1499
  (batchData, actions, ctx) => {
1074
1500
  const data = batchData.map(
@@ -1081,8 +1507,8 @@ function knowledgeGraph(name, opts = {}) {
1081
1507
  );
1082
1508
  graph.add(entityCount, { name: "entityCount" });
1083
1509
  graph.add(edgeCount, { name: "edgeCount" });
1084
- graph.addDisposer(keepalive2(entityCount));
1085
- graph.addDisposer(keepalive2(edgeCount));
1510
+ graph.addDisposer(keepalive6(entityCount));
1511
+ graph.addDisposer(keepalive6(edgeCount));
1086
1512
  const events = createAuditLog({
1087
1513
  name: "events",
1088
1514
  retainedLimit: 1024,
@@ -1105,7 +1531,7 @@ function knowledgeGraph(name, opts = {}) {
1105
1531
  events.append({
1106
1532
  action: "orphanRemove",
1107
1533
  id: candidate,
1108
- t_ns: wallClockNs2(),
1534
+ t_ns: wallClockNs3(),
1109
1535
  seq: bumpCursor(seqCursor)
1110
1536
  });
1111
1537
  }
@@ -1200,7 +1626,7 @@ function knowledgeGraph(name, opts = {}) {
1200
1626
  const idN = toNode(id, "id");
1201
1627
  const relN = relation !== void 0 ? toNode(relation, "relation") : void 0;
1202
1628
  const deps = relN ? [adjacencyOut, adjacencyIn, idN, relN] : [adjacencyOut, adjacencyIn, idN];
1203
- return node2(
1629
+ return node9(
1204
1630
  deps,
1205
1631
  (batchData, actions, ctx) => {
1206
1632
  const values = batchData.map(
@@ -1269,9 +1695,18 @@ function knowledgeGraph(name, opts = {}) {
1269
1695
 
1270
1696
  export {
1271
1697
  reactiveFactStore,
1698
+ persistentReactiveFactStore,
1699
+ admissionLlmJudge,
1700
+ bitemporalQuery,
1701
+ consolidationRem,
1702
+ decayExponential,
1703
+ influenceAnalysis,
1704
+ invalidationTracer,
1705
+ scoringByOutcome,
1706
+ shardByTenant,
1272
1707
  cosineSimilarity,
1273
1708
  collection,
1274
1709
  vectorIndex,
1275
1710
  knowledgeGraph
1276
1711
  };
1277
- //# sourceMappingURL=chunk-FR6RGA3B.js.map
1712
+ //# sourceMappingURL=chunk-U225SKB4.js.map