@graphrefly/graphrefly 0.47.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.
- package/dist/{chunk-CGHORL6G.js → chunk-7ADWWI2T.js} +2 -2
- package/dist/{chunk-TNX5ZGDJ.js → chunk-B4AKFXGE.js} +4 -4
- package/dist/{chunk-FW23JYNQ.js → chunk-CEVNQ74M.js} +2 -2
- package/dist/{chunk-JGFRAFDL.js → chunk-FVINAAKA.js} +3 -3
- package/dist/{chunk-22SG74BD.js → chunk-J5WFUEO4.js} +2 -2
- package/dist/{chunk-GWRNLJNW.js → chunk-K7PDZYQE.js} +4 -4
- package/dist/{chunk-Z6EGP5D7.js → chunk-LDCSZ72P.js} +2 -2
- package/dist/{chunk-EHRRQ4IC.js → chunk-MTTRCEJT.js} +2 -2
- package/dist/{chunk-Q3EYOCZB.js → chunk-NPRP3MCV.js} +111 -2
- package/dist/chunk-NPRP3MCV.js.map +1 -0
- package/dist/{chunk-JKTC747G.js → chunk-RGMTUZCL.js} +3 -3
- package/dist/{chunk-ZVXXDWIB.js → chunk-U225SKB4.js} +455 -25
- package/dist/chunk-U225SKB4.js.map +1 -0
- package/dist/{chunk-ZT4WMQW4.js → chunk-V4Y3TM7U.js} +5 -5
- package/dist/{chunk-5IMMNARC.js → chunk-YXCPV26R.js} +2 -2
- package/dist/index.cjs +1374 -840
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +35 -13
- package/dist/index.js.map +1 -1
- package/dist/presets/ai/index.cjs.map +1 -1
- package/dist/presets/ai/index.js +6 -6
- package/dist/presets/harness/index.cjs.map +1 -1
- package/dist/presets/harness/index.js +9 -9
- package/dist/presets/index.cjs.map +1 -1
- package/dist/presets/index.js +13 -13
- package/dist/presets/inspect/index.cjs.map +1 -1
- package/dist/presets/inspect/index.js +4 -4
- package/dist/solutions/index.cjs.map +1 -1
- package/dist/solutions/index.js +10 -10
- package/dist/utils/ai/index.cjs.map +1 -1
- package/dist/utils/ai/index.js +5 -5
- package/dist/utils/index.cjs +931 -397
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +2 -2
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +28 -6
- package/dist/utils/inspect/index.cjs.map +1 -1
- package/dist/utils/inspect/index.js +2 -2
- package/dist/utils/memory/index.cjs +462 -37
- package/dist/utils/memory/index.cjs.map +1 -1
- package/dist/utils/memory/index.d.cts +591 -2
- package/dist/utils/memory/index.d.ts +591 -2
- package/dist/utils/memory/index.js +19 -1
- package/dist/utils/messaging/index.cjs +109 -0
- package/dist/utils/messaging/index.cjs.map +1 -1
- package/dist/utils/messaging/index.d.cts +115 -2
- package/dist/utils/messaging/index.d.ts +115 -2
- package/dist/utils/messaging/index.js +5 -1
- package/dist/utils/orchestration/index.cjs.map +1 -1
- package/dist/utils/orchestration/index.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-Q3EYOCZB.js.map +0 -1
- package/dist/chunk-ZVXXDWIB.js.map +0 -1
- /package/dist/{chunk-CGHORL6G.js.map → chunk-7ADWWI2T.js.map} +0 -0
- /package/dist/{chunk-TNX5ZGDJ.js.map → chunk-B4AKFXGE.js.map} +0 -0
- /package/dist/{chunk-FW23JYNQ.js.map → chunk-CEVNQ74M.js.map} +0 -0
- /package/dist/{chunk-JGFRAFDL.js.map → chunk-FVINAAKA.js.map} +0 -0
- /package/dist/{chunk-22SG74BD.js.map → chunk-J5WFUEO4.js.map} +0 -0
- /package/dist/{chunk-GWRNLJNW.js.map → chunk-K7PDZYQE.js.map} +0 -0
- /package/dist/{chunk-Z6EGP5D7.js.map → chunk-LDCSZ72P.js.map} +0 -0
- /package/dist/{chunk-EHRRQ4IC.js.map → chunk-MTTRCEJT.js.map} +0 -0
- /package/dist/{chunk-JKTC747G.js.map → chunk-RGMTUZCL.js.map} +0 -0
- /package/dist/{chunk-ZT4WMQW4.js.map → chunk-V4Y3TM7U.js.map} +0 -0
- /package/dist/{chunk-5IMMNARC.js.map → chunk-YXCPV26R.js.map} +0 -0
|
@@ -12,8 +12,8 @@ import {
|
|
|
12
12
|
} from "./chunk-BXGZFGZ4.js";
|
|
13
13
|
|
|
14
14
|
// src/utils/memory/index.ts
|
|
15
|
-
import { monotonicNs, NodeImpl, node as
|
|
16
|
-
import { fromTimer, keepalive as
|
|
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
|
|
@@ -500,6 +500,427 @@ function reactiveFactStore(config) {
|
|
|
500
500
|
return out;
|
|
501
501
|
}
|
|
502
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
|
+
|
|
503
924
|
// src/utils/memory/index.ts
|
|
504
925
|
var NS_PER_SEC = 1e9;
|
|
505
926
|
function memoryMeta(kind, extra) {
|
|
@@ -507,7 +928,7 @@ function memoryMeta(kind, extra) {
|
|
|
507
928
|
}
|
|
508
929
|
function toNode(v, name) {
|
|
509
930
|
if (v instanceof NodeImpl) return v;
|
|
510
|
-
return
|
|
931
|
+
return node9([], { initial: v, ...name ? { name } : void 0 });
|
|
511
932
|
}
|
|
512
933
|
function ageSeconds(now, lastNs) {
|
|
513
934
|
return (now - lastNs) / NS_PER_SEC;
|
|
@@ -576,8 +997,8 @@ function collection(name, opts = {}) {
|
|
|
576
997
|
let refreshTick;
|
|
577
998
|
if (ranked && decayRate > 0) {
|
|
578
999
|
const intervalMs = opts.refreshIntervalMs ?? Math.max(1, 1e3 * Math.LN2 / (10 * decayRate));
|
|
579
|
-
const tickCounter =
|
|
580
|
-
refreshTick =
|
|
1000
|
+
const tickCounter = fromTimer3(intervalMs, { period: intervalMs });
|
|
1001
|
+
refreshTick = node9(
|
|
581
1002
|
[tickCounter],
|
|
582
1003
|
(_batchData, actions) => {
|
|
583
1004
|
actions.emit(monotonicNs());
|
|
@@ -596,7 +1017,7 @@ function collection(name, opts = {}) {
|
|
|
596
1017
|
const rankedDeps = [items.entries];
|
|
597
1018
|
if (refreshTick) rankedDeps.push(refreshTick);
|
|
598
1019
|
if (scoreNode) rankedDeps.push(scoreNode);
|
|
599
|
-
rankedNode =
|
|
1020
|
+
rankedNode = node9(
|
|
600
1021
|
rankedDeps,
|
|
601
1022
|
(batchData, actions, ctx) => {
|
|
602
1023
|
const values = batchData.map(
|
|
@@ -633,7 +1054,7 @@ function collection(name, opts = {}) {
|
|
|
633
1054
|
);
|
|
634
1055
|
graph.add(rankedNode, { name: "ranked" });
|
|
635
1056
|
} else {
|
|
636
|
-
rankedNode =
|
|
1057
|
+
rankedNode = node9([], {
|
|
637
1058
|
initial: [],
|
|
638
1059
|
name: "ranked",
|
|
639
1060
|
describeKind: "state",
|
|
@@ -641,7 +1062,7 @@ function collection(name, opts = {}) {
|
|
|
641
1062
|
});
|
|
642
1063
|
graph.add(rankedNode, { name: "ranked" });
|
|
643
1064
|
}
|
|
644
|
-
const size =
|
|
1065
|
+
const size = node9(
|
|
645
1066
|
[items.entries],
|
|
646
1067
|
(batchData, actions, ctx) => {
|
|
647
1068
|
const data = batchData.map(
|
|
@@ -658,7 +1079,7 @@ function collection(name, opts = {}) {
|
|
|
658
1079
|
}
|
|
659
1080
|
);
|
|
660
1081
|
graph.add(size, { name: "size" });
|
|
661
|
-
graph.addDisposer(
|
|
1082
|
+
graph.addDisposer(keepalive6(size));
|
|
662
1083
|
const events = createAuditLog({
|
|
663
1084
|
name: "events",
|
|
664
1085
|
retainedLimit: 1024,
|
|
@@ -722,7 +1143,7 @@ function collection(name, opts = {}) {
|
|
|
722
1143
|
});
|
|
723
1144
|
function itemNode(id) {
|
|
724
1145
|
const idN = toNode(id, "id");
|
|
725
|
-
return
|
|
1146
|
+
return node9(
|
|
726
1147
|
[items.entries, idN],
|
|
727
1148
|
(batchData, actions, ctx) => {
|
|
728
1149
|
const data = batchData.map(
|
|
@@ -740,7 +1161,7 @@ function collection(name, opts = {}) {
|
|
|
740
1161
|
}
|
|
741
1162
|
function hasNode(id) {
|
|
742
1163
|
const idN = toNode(id, "id");
|
|
743
|
-
return
|
|
1164
|
+
return node9(
|
|
744
1165
|
[items.entries, idN],
|
|
745
1166
|
(batchData, actions, ctx) => {
|
|
746
1167
|
const data = batchData.map(
|
|
@@ -827,7 +1248,7 @@ function vectorIndex(opts = {}) {
|
|
|
827
1248
|
events.append({
|
|
828
1249
|
action: "evict",
|
|
829
1250
|
id: key,
|
|
830
|
-
t_ns:
|
|
1251
|
+
t_ns: wallClockNs3(),
|
|
831
1252
|
seq: bumpCursor(seqCursor)
|
|
832
1253
|
});
|
|
833
1254
|
}
|
|
@@ -835,7 +1256,7 @@ function vectorIndex(opts = {}) {
|
|
|
835
1256
|
} : {}
|
|
836
1257
|
});
|
|
837
1258
|
graph.add(entries.entries, { name: "entries" });
|
|
838
|
-
graph.addDisposer(
|
|
1259
|
+
graph.addDisposer(keepalive6(entries.entries));
|
|
839
1260
|
if (hnsw?.dispose) {
|
|
840
1261
|
const disposeAdapter = hnsw.dispose.bind(hnsw);
|
|
841
1262
|
graph.addDisposer(() => disposeAdapter());
|
|
@@ -908,7 +1329,7 @@ function vectorIndex(opts = {}) {
|
|
|
908
1329
|
});
|
|
909
1330
|
function searchNode(query, k = 5) {
|
|
910
1331
|
const kN = toNode(k, "k");
|
|
911
|
-
return
|
|
1332
|
+
return node9(
|
|
912
1333
|
[entries.entries, query, kN],
|
|
913
1334
|
(batchData, actions, ctx) => {
|
|
914
1335
|
const values = batchData.map(
|
|
@@ -1024,7 +1445,7 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1024
1445
|
});
|
|
1025
1446
|
graph.add(entitiesMap.entries, { name: "entities" });
|
|
1026
1447
|
graph.add(edgesMap.entries, { name: "edges" });
|
|
1027
|
-
const adjacencyOut =
|
|
1448
|
+
const adjacencyOut = node9(
|
|
1028
1449
|
[edgesMap.entries],
|
|
1029
1450
|
(batchData, actions, ctx) => {
|
|
1030
1451
|
const data = batchData.map(
|
|
@@ -1041,7 +1462,7 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1041
1462
|
meta: memoryMeta("adjacency_out")
|
|
1042
1463
|
}
|
|
1043
1464
|
);
|
|
1044
|
-
const adjacencyIn =
|
|
1465
|
+
const adjacencyIn = node9(
|
|
1045
1466
|
[edgesMap.entries],
|
|
1046
1467
|
(batchData, actions, ctx) => {
|
|
1047
1468
|
const data = batchData.map(
|
|
@@ -1060,9 +1481,9 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1060
1481
|
);
|
|
1061
1482
|
graph.add(adjacencyOut, { name: "adjacencyOut" });
|
|
1062
1483
|
graph.add(adjacencyIn, { name: "adjacencyIn" });
|
|
1063
|
-
graph.addDisposer(
|
|
1064
|
-
graph.addDisposer(
|
|
1065
|
-
const entityCount =
|
|
1484
|
+
graph.addDisposer(keepalive6(adjacencyOut));
|
|
1485
|
+
graph.addDisposer(keepalive6(adjacencyIn));
|
|
1486
|
+
const entityCount = node9(
|
|
1066
1487
|
[entitiesMap.entries],
|
|
1067
1488
|
(batchData, actions, ctx) => {
|
|
1068
1489
|
const data = batchData.map(
|
|
@@ -1073,7 +1494,7 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1073
1494
|
},
|
|
1074
1495
|
{ name: "entityCount", describeKind: "derived", initial: 0, meta: memoryMeta("entity_count") }
|
|
1075
1496
|
);
|
|
1076
|
-
const edgeCount =
|
|
1497
|
+
const edgeCount = node9(
|
|
1077
1498
|
[edgesMap.entries],
|
|
1078
1499
|
(batchData, actions, ctx) => {
|
|
1079
1500
|
const data = batchData.map(
|
|
@@ -1086,8 +1507,8 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1086
1507
|
);
|
|
1087
1508
|
graph.add(entityCount, { name: "entityCount" });
|
|
1088
1509
|
graph.add(edgeCount, { name: "edgeCount" });
|
|
1089
|
-
graph.addDisposer(
|
|
1090
|
-
graph.addDisposer(
|
|
1510
|
+
graph.addDisposer(keepalive6(entityCount));
|
|
1511
|
+
graph.addDisposer(keepalive6(edgeCount));
|
|
1091
1512
|
const events = createAuditLog({
|
|
1092
1513
|
name: "events",
|
|
1093
1514
|
retainedLimit: 1024,
|
|
@@ -1110,7 +1531,7 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1110
1531
|
events.append({
|
|
1111
1532
|
action: "orphanRemove",
|
|
1112
1533
|
id: candidate,
|
|
1113
|
-
t_ns:
|
|
1534
|
+
t_ns: wallClockNs3(),
|
|
1114
1535
|
seq: bumpCursor(seqCursor)
|
|
1115
1536
|
});
|
|
1116
1537
|
}
|
|
@@ -1205,7 +1626,7 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1205
1626
|
const idN = toNode(id, "id");
|
|
1206
1627
|
const relN = relation !== void 0 ? toNode(relation, "relation") : void 0;
|
|
1207
1628
|
const deps = relN ? [adjacencyOut, adjacencyIn, idN, relN] : [adjacencyOut, adjacencyIn, idN];
|
|
1208
|
-
return
|
|
1629
|
+
return node9(
|
|
1209
1630
|
deps,
|
|
1210
1631
|
(batchData, actions, ctx) => {
|
|
1211
1632
|
const values = batchData.map(
|
|
@@ -1274,9 +1695,18 @@ function knowledgeGraph(name, opts = {}) {
|
|
|
1274
1695
|
|
|
1275
1696
|
export {
|
|
1276
1697
|
reactiveFactStore,
|
|
1698
|
+
persistentReactiveFactStore,
|
|
1699
|
+
admissionLlmJudge,
|
|
1700
|
+
bitemporalQuery,
|
|
1701
|
+
consolidationRem,
|
|
1702
|
+
decayExponential,
|
|
1703
|
+
influenceAnalysis,
|
|
1704
|
+
invalidationTracer,
|
|
1705
|
+
scoringByOutcome,
|
|
1706
|
+
shardByTenant,
|
|
1277
1707
|
cosineSimilarity,
|
|
1278
1708
|
collection,
|
|
1279
1709
|
vectorIndex,
|
|
1280
1710
|
knowledgeGraph
|
|
1281
1711
|
};
|
|
1282
|
-
//# sourceMappingURL=chunk-
|
|
1712
|
+
//# sourceMappingURL=chunk-U225SKB4.js.map
|