@hviana/sema 0.9.1 → 0.9.3

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 (42) hide show
  1. package/dist/src/meter.d.ts +19 -0
  2. package/dist/src/meter.js +19 -0
  3. package/dist/src/mind/evidence.d.ts +30 -0
  4. package/dist/src/mind/evidence.js +102 -0
  5. package/dist/src/mind/mechanisms/cast.d.ts +1 -1
  6. package/dist/src/mind/mechanisms/cast.js +73 -12
  7. package/dist/src/mind/mechanisms/cover.js +25 -3
  8. package/dist/src/mind/mechanisms/recall.js +93 -9
  9. package/dist/src/mind/mechanisms/reference.js +25 -0
  10. package/dist/src/mind/mind.d.ts +7 -0
  11. package/dist/src/mind/mind.js +3 -0
  12. package/dist/src/mind/pipeline-mechanism.js +15 -1
  13. package/dist/src/mind/pipeline.js +51 -8
  14. package/dist/src/mind/reasoning.js +65 -18
  15. package/dist/src/mind/resonance.d.ts +3 -2
  16. package/dist/src/mind/resonance.js +3 -2
  17. package/dist/src/mind/traverse.d.ts +94 -2
  18. package/dist/src/mind/traverse.js +611 -7
  19. package/dist/src/mind/types.d.ts +12 -0
  20. package/docs/INDEX.md +18 -17
  21. package/docs/INVARIANTS.md +1 -0
  22. package/docs/architecture/evidence.md +256 -0
  23. package/docs/architecture/exact-vs-approximate.md +10 -9
  24. package/jsr.json +1 -1
  25. package/package.json +1 -1
  26. package/src/meter.ts +19 -0
  27. package/src/mind/evidence.ts +125 -0
  28. package/src/mind/mechanisms/cast.ts +77 -3
  29. package/src/mind/mechanisms/cover.ts +42 -3
  30. package/src/mind/mechanisms/recall.ts +123 -11
  31. package/src/mind/mechanisms/reference.ts +24 -1
  32. package/src/mind/mind.ts +6 -0
  33. package/src/mind/pipeline-mechanism.ts +15 -1
  34. package/src/mind/pipeline.ts +64 -8
  35. package/src/mind/reasoning.ts +82 -17
  36. package/src/mind/resonance.ts +3 -2
  37. package/src/mind/traverse.ts +652 -7
  38. package/src/mind/types.ts +10 -0
  39. package/test/154-the-question-names-the-step.test.mjs +368 -0
  40. package/test/155-the-relation-read-off-another-instance.test.mjs +376 -0
  41. package/test/29-counterfactual.test.mjs +21 -11
  42. package/test/76-reference-binding.test.mjs +32 -0
@@ -6,14 +6,16 @@
6
6
  // The PROJECTIONS built on these walks (follow, conceptHop, reverseContext,
7
7
  // project) live in match.ts — the elementary match-and-project operation.
8
8
  import { cosine } from "../vec.js";
9
- import { gistOf, read } from "./primitives.js";
10
- import { canonicalWindows, leafIdPrefix, leafIdRun } from "./canonical.js";
9
+ import { gistOf, read, resolve } from "./primitives.js";
10
+ import { canonicalWindows, chainReach, leafIdPrefix, leafIdRun, } from "./canonical.js";
11
11
  // Imported at the TOP, where every other import is. They used to sit 800 lines
12
12
  // down under a note claiming the position mattered ("before trace module is
13
13
  // loaded") — it does not: an ES module's static imports are HOISTED, so the
14
14
  // file's line order never decides load order. The note described an intention
15
15
  // the runtime does not honour; the imports move and the claim goes.
16
16
  import { decodeText } from "./rationale.js";
17
+ import { concatBytes, indexOf, latin1 } from "../bytes.js";
18
+ import { windowIndex, witness } from "./evidence.js";
17
19
  //
18
20
  // Budgeted on the same terms as the reach memo below (caches.md): these three
19
21
  // maps are cleared on every write, but a long read-only session over a large
@@ -588,7 +590,9 @@ export function guidedNext(ctx, node) {
588
590
  // policy climbMemo and recogniseMemo follow (every mechanism must emit its
589
591
  // own steps; a memo hit would swallow the repeat's `disambiguate` step).
590
592
  // Consistency does not need the memo: chooseNext is a pure function of the
591
- // (read-only) store and the guide, so recomputation yields the same pick.
593
+ // (read-only) store, the guide and the consensus climb's points once they
594
+ // exist (the memo is cleared when they arrive), so recomputation yields the
595
+ // same pick.
592
596
  if (!ctx.trace) {
593
597
  const memo = ctx._edgeChoice.get(node);
594
598
  if (memo !== undefined)
@@ -599,8 +603,10 @@ export function guidedNext(ctx, node) {
599
603
  ctx._edgeChoice.set(node, pick ?? -1);
600
604
  return pick;
601
605
  }
602
- /** Disambiguate among a node's learnt continuations by distributional
603
- * support. NOTE the `guide` contract: its VALUE is deliberately unused —
606
+ /** Disambiguate among a node's learnt continuations: first by the question's
607
+ * own witness of an establishing context (the exact tier, see
608
+ * {@link askedContinuations}), then by distributional support. NOTE the
609
+ * `guide` contract: its VALUE is deliberately unused —
604
610
  * only its PRESENCE gates disambiguation (a null guide means no query is in
605
611
  * flight, so structural walkers keep plain first-edge behaviour). The
606
612
  * gist-cosine of short answer candidates against a query guide is dominated
@@ -621,13 +627,30 @@ export function chooseNext(ctx, id, guide) {
621
627
  return undefined;
622
628
  if (nx.length === 1 || !guide)
623
629
  return nx[0];
630
+ // THE EXACT TIER — the continuation the QUESTION names. Every other
631
+ // disambiguation below reads popularity, and is right to refuse the gist (see
632
+ // the doc above); but the corpus also wrote down, for each continuation,
633
+ // WHICH QUESTIONS IT ANSWERS — its establishing contexts — and a question is
634
+ // not a gist. When one of them is witnessed by the asker's bytes plus the
635
+ // node's own, that continuation is the one being asked for. Measured on the
636
+ // 31.7M-node store: `Who is the father of Frederick II?` answered the
637
+ // citizenship fact (the most-poured of eight) while `Frederick II father` —
638
+ // one of the father fact's own establishing contexts — lay wholly inside the
639
+ // question. Exact, so it ranks first (exact-vs-approximate.md); when nothing
640
+ // is witnessed the ladder below decides exactly as before.
641
+ const asked = ctx._edgeAsked;
642
+ const named = asked === null ? null : askedContinuations(ctx, id, nx, asked);
643
+ if (named !== null && named.length === 1)
644
+ return named[0];
624
645
  // Cap candidates at √N — the same bound the original chooseAmong used.
625
646
  // A hub context can accumulate thousands of continuations; the best-fit
626
647
  // one is among the first √N by insertion order (edges are never deleted,
627
648
  // so the oldest are the most established). A strongly-supported edge
628
649
  // inserted beyond the cap is invisible here — the deliberate trade
629
- // against paying O(fan-out) count reads on every disambiguation.
630
- const capped = nx; // already the hub-capped prefix, by the read above
650
+ // against paying O(fan-out) count reads on every disambiguation. Several
651
+ // continuations named EQUALLY by the question are told apart by the same
652
+ // ladder, over them alone.
653
+ const capped = named ?? nx; // already the hub-capped prefix, by the read above
631
654
  // Distributional-evidence disambiguation, consulting BOTH read-outs of the
632
655
  // evidence the training poured:
633
656
  // 1. prevCount — how many DISTINCT contexts predict this candidate (one
@@ -691,6 +714,509 @@ export function chooseNext(ctx, id, guide) {
691
714
  }
692
715
  return best;
693
716
  }
717
+ /** The response canonicalizer's reading of `bytes` when it keeps every offset
718
+ * — so a window found in the canonical bytes sits at the same place in the
719
+ * asker's — else the bytes themselves. Text canon is offset-preserving on
720
+ * ASCII without interior whitespace runs; where it is not, the raw bytes are
721
+ * read and a case-variant window simply does not match. */
722
+ export function offsetCanon(ctx, bytes) {
723
+ if (ctx.canon === null)
724
+ return bytes;
725
+ const c = ctx.canon(bytes);
726
+ return c.length === bytes.length ? c : bytes;
727
+ }
728
+ /** A FRAGMENT ANSWERS OTHER QUESTIONS. A recognised form that sits inside
729
+ * other forms (it has structural parents or containers) holds continuations
730
+ * because the forms it is a piece of were asked — suffix inheritance gives
731
+ * `f death` the answer of every `… place of death` question, and `director`
732
+ * the answer of every `… director` one. Choosing one of several such
733
+ * continuations by popularity voices some other question's answer. A
734
+ * fragment with several continuations leads somewhere FOR THIS QUESTION only
735
+ * when the question names one (the exact tier below). A form that is
736
+ * (nearly) the whole question is not a piece of it: the question says
737
+ * nothing beyond it, so its continuations answer THIS question. Read by
738
+ * every mechanism that projects through a recognised site (cover's sites,
739
+ * recall's argument binding). */
740
+ export function answersOtherQuestions(ctx, id, queryLen, siteLen) {
741
+ const asked = ctx._edgeAsked;
742
+ if (asked === null || queryLen - siteLen < ctx.space.maxGroup)
743
+ return false;
744
+ if (!(ctx.store.hasParents(id) || ctx.store.hasContainers(id)))
745
+ return false;
746
+ if (ctx.store.nextFirst(id, 2).length < 2)
747
+ return false;
748
+ return namedContinuations(ctx, id, asked) === null;
749
+ }
750
+ /** The continuations of `id` that `asked` NAMES (see {@link
751
+ * askedContinuations}) — for a caller holding material other than the whole
752
+ * question: the multi-hop walk asks with what of the question no product has
753
+ * restated yet. null when none is named. */
754
+ export function namedContinuations(ctx, id, asked) {
755
+ const nx = ctx.store.nextFirst(id, hubBound(ctx));
756
+ return nx.length === 0 ? null : askedContinuations(ctx, id, nx, asked);
757
+ }
758
+ /** The continuations of `id` (among `nx`) that the question NAMES: one of
759
+ * their establishing contexts — a predecessor other than `id` itself — is
760
+ * wholly witnessed by the question plus `id`'s own bytes (evidence.ts), with
761
+ * the question supplying at least one window the node does not. Ranked by
762
+ * how much of the question witnesses it; null when none is named.
763
+ *
764
+ * THE NODE'S OWN BYTES ARE MATERIAL because a derivation stands on them. On
765
+ * the second hop of `Where was the place of death of the director of film
766
+ * Beat Girl?` the node is `Edmond T. Gréville` — reached, never written — and
767
+ * its fact's establishing context `Edmond T. Gréville place of death` is held
768
+ * by neither the question nor the node, only by both. Measured over 5,236
769
+ * held-out 2Wiki questions: such a context is wholly witnessed by the
770
+ * question alone 69 times, by the question and the first hop 2,153 times.
771
+ *
772
+ * BOUNDED: predecessor reads share one √N budget across the candidates (see
773
+ * the loop below), asked cheapest first; past it the tier abstains for the
774
+ * rest, metered, and the ladder decides as before.
775
+ * Each form is read at most to the material's length — a form longer than
776
+ * everything at hand cannot be wholly witnessed without repeating it. */
777
+ function askedContinuations(ctx, id, nx, asked) {
778
+ return askedEntry(ctx, id, nx, asked).named;
779
+ }
780
+ /** The question spans that NAMED `pick` among `id`'s continuations — the
781
+ * evidence a projection through that pick stands on, so a mechanism can
782
+ * account for what the question said about it (mechanism-market.md:
783
+ * evidence travels). Empty when the question names no continuation of `id`
784
+ * or names others. */
785
+ export function askedEvidence(ctx, id, pick) {
786
+ const asked = ctx._edgeAsked;
787
+ if (asked === null)
788
+ return [];
789
+ const nx = ctx.store.nextFirst(id, hubBound(ctx));
790
+ const entry = askedEntry(ctx, id, nx, asked);
791
+ return entry.named?.includes(pick) ? entry.spans.get(pick) ?? [] : [];
792
+ }
793
+ function askedEntry(ctx, id, nx, asked) {
794
+ let memo = askedMemo.get(asked);
795
+ if (memo === undefined)
796
+ askedMemo.set(asked, memo = new Map());
797
+ const hit = memo.get(id);
798
+ if (hit !== undefined && !ctx.trace)
799
+ return hit;
800
+ const entry = askedContinuationsImpl(ctx, id, nx, asked);
801
+ // Nothing named before the climb has run is provisional: a co-instance
802
+ // read from its points may still name one.
803
+ if (entry.named !== null || ctx._edgeAsked?.points !== undefined) {
804
+ memo.set(id, entry);
805
+ }
806
+ return entry;
807
+ }
808
+ /** One pick per node per question — every mechanism of a response asks the
809
+ * same node about the same question (the guided-pick memo's own reason). */
810
+ const askedMemo = new WeakMap();
811
+ function askedContinuationsImpl(ctx, id, nx, asked) {
812
+ const none = { named: null, spans: new Map() };
813
+ const W = ctx.space.maxGroup;
814
+ // A SATURATED READ IS NOT A CANDIDATE SET. When the continuations came back
815
+ // at the √N cap the read may have cut the named one off, so "none of these is
816
+ // named" and "this is the named one" are both unfounded — and this is exactly
817
+ // where witnessing would read most. The tier abstains, metered, and the
818
+ // distributional ladder decides as it always has.
819
+ if (nx.length >= hubBound(ctx)) {
820
+ if (ctx.meter)
821
+ ctx.meter.askedReadsSaturated++;
822
+ return none;
823
+ }
824
+ const cache = getStructCache(ctx);
825
+ const ownCap = asked.bytes.length * W;
826
+ const own = offsetCanon(ctx, read(ctx, id, ownCap));
827
+ const ownIndex = windowIndex(own, W);
828
+ // Naming needs the question to say at least one window the node does not:
829
+ // when the node already holds every window of the question (the question IS
830
+ // this context, or a piece of it), nothing can be named, and nothing is read.
831
+ let beyond = false;
832
+ for (const key of asked.index.keys()) {
833
+ if (!ownIndex.has(key)) {
834
+ beyond = true;
835
+ break;
836
+ }
837
+ }
838
+ if (!beyond)
839
+ return none;
840
+ const indexes = [asked.index, ownIndex];
841
+ const formCap = asked.bytes.length + own.length;
842
+ // BOUNDED READS (bounded-reads.md): the decision reads at most √N
843
+ // establishing contexts — floored at the write side's own arity `chainReach(W)`
844
+ // so a store too small for √N to cover one fact's questions still decides.
845
+ // Candidates are asked CHEAPEST FIRST (fewest establishing contexts): a common
846
+ // reply established by hundreds of contexts would otherwise spend the whole
847
+ // allowance alone. The order changes what is READ, never what wins: scores
848
+ // are compared afterwards in the continuations' own order.
849
+ const allowance = Math.max(hubBound(ctx), chainReach(W));
850
+ let budget = allowance;
851
+ const order = nx
852
+ .map((n, at) => ({ n, at, support: cachedPrevCount(ctx, n, cache) }))
853
+ .filter((c) => c.support >= 2) // only `id` establishes the rest
854
+ .sort((a, b) => a.support - b.support || a.at - b.at);
855
+ const scored = [];
856
+ for (const { n, at, support } of order) {
857
+ if (support > budget) {
858
+ if (ctx.meter)
859
+ ctx.meter.askedReadsSaturated++;
860
+ break;
861
+ }
862
+ budget -= support;
863
+ if (ctx.meter)
864
+ ctx.meter.askedPredecessorReads += support;
865
+ let score = 0;
866
+ let by = -1;
867
+ let spans = [];
868
+ for (const c of ctx.store.prevFirst(n, support)) {
869
+ if (c === id)
870
+ continue;
871
+ // The form's FIRST window decides most refusals: one short prefix read
872
+ // before the whole form is reconstructed (a conversation-length
873
+ // predecessor would otherwise be read in full to fail on its opening).
874
+ const head = offsetCanon(ctx, read(ctx, c, W));
875
+ if (head.length < W)
876
+ continue;
877
+ if (!indexes.some((ix) => ix.has(latin1(head))))
878
+ continue;
879
+ // AN ESTABLISHING CONTEXT IS A DEPOSITED ONE. A span interned inside
880
+ // bigger forms inherits their edges (the fragment ` born?` leads to the
881
+ // fact of every `Where was … born?` question), so witnessing it names
882
+ // every one of them — measured on the 2Wiki fixture with one-hop
883
+ // questions deposited, ` born?` named nine strangers' birthplaces. The
884
+ // structural predicate pivotInto already reads: a form with parents or
885
+ // containers was never a context on its own.
886
+ if (cachedHasParents(ctx, c, cache) || ctx.store.hasContainers(c))
887
+ continue;
888
+ const form = read(ctx, c, formCap + 1);
889
+ if (form.length < W || form.length > formCap)
890
+ continue;
891
+ const w = witness(offsetCanon(ctx, form), indexes, W);
892
+ if (!w.complete || w.bytes < W)
893
+ continue;
894
+ if (w.bytes > score) {
895
+ score = w.bytes;
896
+ by = c;
897
+ spans = w.spans;
898
+ }
899
+ }
900
+ if (score > 0)
901
+ scored.push({ n, at, score, by, spans });
902
+ }
903
+ scored.sort((a, b) => a.at - b.at);
904
+ let best = [];
905
+ let bestBytes = 0;
906
+ let witnessed = null;
907
+ for (const { n, score, by } of scored) {
908
+ if (score > bestBytes) {
909
+ best = [n];
910
+ bestBytes = score;
911
+ witnessed = by;
912
+ }
913
+ else if (score === bestBytes)
914
+ best.push(n);
915
+ }
916
+ if (best.length === 0) {
917
+ return byCoInstance(ctx, id, nx, asked, formCap, allowance);
918
+ }
919
+ if (ctx.meter)
920
+ ctx.meter.askedContinuations++;
921
+ if (ctx.trace && witnessed !== null) {
922
+ ctx.trace.step("askedContinuation", [rItemShort(ctx, id, "node"), rItemShort(ctx, witnessed, "asked")], best.map((n) => rItemShort(ctx, n, "named")), `${nx.length} continuations — the question witnesses ` +
923
+ `${best.length === 1 ? "one's" : `${best.length}'`} own establishing ` +
924
+ `context (${bestBytes} question byte(s) beyond the node)`);
925
+ }
926
+ const evidence = new Map();
927
+ for (const c of scored)
928
+ if (best.includes(c.n))
929
+ evidence.set(c.n, c.spans);
930
+ return { named: best, spans: evidence };
931
+ }
932
+ /** The climb's points the question HOLDS under the response's equivalence —
933
+ * stored forms whose canonical bytes occur in the question's, as sites. The
934
+ * recognition walk matches bytes and the fold's own boundaries, so a stored
935
+ * `Man at Bath` inside `… of film Man At Bath work at?` is no site; the
936
+ * consensus climb reaches it through the windows the two spellings share, and
937
+ * containment under the canon (offset-preserving, so the span is the
938
+ * question's own) decides. Approximate proposes, exact decides. Read off
939
+ * the `chainReach(W)` most corroborated points; empty before the climb. */
940
+ export function canonHeldPoints(ctx, minLen) {
941
+ const asked = ctx._edgeAsked;
942
+ if (asked === null || asked.points === undefined || ctx.canon === null) {
943
+ return [];
944
+ }
945
+ const W = ctx.space.maxGroup;
946
+ const out = [];
947
+ for (const id of asked.points.slice(0, chainReach(W))) {
948
+ const raw = read(ctx, id, asked.bytes.length);
949
+ if (raw.length < minLen || raw.length >= asked.bytes.length)
950
+ continue;
951
+ const form = ctx.canon(raw);
952
+ if (form.length !== raw.length)
953
+ continue;
954
+ const at = indexOf(asked.bytes, form, 0);
955
+ if (at >= 0)
956
+ out.push({ start: at, end: at + raw.length, payload: id });
957
+ }
958
+ return out;
959
+ }
960
+ /** A CO-INSTANCE of the question: a stored form that shares the question's
961
+ * FRAME — the same opening and the same close, byte for byte under the
962
+ * response's equivalence — around ONE different filler. `Where was Peter
963
+ * Jackson born?` against `Where was the director of film Beat Girl born?`
964
+ * shares `Where was ` and ` born?`, and leaves `Peter Jackson` against `the
965
+ * director of film Beat Girl`: another instance of the same question, never
966
+ * this one. It is the ordered limit of witnessing: a frame is not a bag of
967
+ * windows (an order-free reading lets `Lyon is a city in France` pass for an
968
+ * instance of `what if the capital of France were Lyon?`, which NAMES Lyon;
969
+ * and a coincidental window inside a filler splits it).
970
+ *
971
+ * The frame must reach one window, every window inside it must be held by
972
+ * the material (so on the walk, a frame a product already said cannot name a
973
+ * second step), and the form's filler must be a thing the corpus knows
974
+ * (below) — a record that differs from the question only inside the frame
975
+ * by a description (`converts sunlight into chemical energy` against
976
+ * `works`) is the same question about the frame's own subject. (Requiring
977
+ * the two fillers to share no window as well was measured redundant with
978
+ * this and cost two answers on the 2Wiki fixture.) Returns the form's filler span in `raw` and the
979
+ * question spans of the frame, or null. One reading for every consumer: the
980
+ * exact tier transfers the relation through it, and the readers that would
981
+ * VOICE such a form's own continuation refuse it. */
982
+ export function coInstanceFiller(ctx, raw, indexes, question) {
983
+ const W = ctx.space.maxGroup;
984
+ const form = offsetCanon(ctx, raw);
985
+ const max = Math.min(form.length, question.length);
986
+ let a = 0;
987
+ while (a < max && form[a] === question[a])
988
+ a++;
989
+ let b = 0;
990
+ while (b < max - a &&
991
+ form[form.length - 1 - b] === question[question.length - 1 - b])
992
+ b++;
993
+ const fe = form.length - b;
994
+ if (a + b < W || fe <= a || question.length - b <= a)
995
+ return null;
996
+ // Scaffolding is nobody's evidence, so a frame window that is a hub need not
997
+ // be unsaid (` is ` in `Which country Leo Mittler is from?`, which the first
998
+ // hop's `… is Arshad Khan.` already said).
999
+ const hub = hubWindows(ctx, raw);
1000
+ const held = (o) => {
1001
+ if (hub[o])
1002
+ return true;
1003
+ const key = latin1(form.subarray(o, o + W));
1004
+ return indexes.some((ix) => ix.has(key));
1005
+ };
1006
+ for (let o = 0; o + W <= a; o++)
1007
+ if (!held(o))
1008
+ return null;
1009
+ for (let o = fe; o + W <= form.length; o++)
1010
+ if (!held(o))
1011
+ return null;
1012
+ // THE FILLER IS A THING THE CORPUS KNOWS: a stored context with
1013
+ // continuations of its own, opening where the slot opens (up to one window
1014
+ // earlier — a filler byte can sit in a window the question holds by chance:
1015
+ // the `T` of `Taika` inside `as t` of `was the`). `Peter Jackson` is one;
1016
+ // `converts sunlight into chemical energy`, the slot of `Explain how
1017
+ // photosynthesis …`, is a description of the frame's own subject, and the
1018
+ // record answers the question. The longest such context is the filler.
1019
+ // One content-addressed probe per byte (the `keyEnds` reading); only a run
1020
+ // some deposit spelled whole is looked up.
1021
+ const ids = leafIdPrefix(ctx, raw);
1022
+ let best = null;
1023
+ const cache = getStructCache(ctx);
1024
+ const reach = Math.min(ids.length, fe + W - 1);
1025
+ for (let st = Math.max(0, a - W + 1); st <= a; st++) {
1026
+ const run = [];
1027
+ for (let en = st + 1; en <= reach; en++) {
1028
+ run.push(ids[en - 1]);
1029
+ if (en - st < W)
1030
+ continue;
1031
+ // The flat branch over the whole run exists only where some deposit
1032
+ // spelled exactly these bytes — the cheap filter; the context it names
1033
+ // is then looked up by content.
1034
+ if (ctx.store.findBranch(run) === null)
1035
+ continue;
1036
+ if (best !== null && en - st <= best[1] - best[0])
1037
+ continue;
1038
+ const id = resolve(ctx, raw.subarray(st, en));
1039
+ if (id !== null && cachedHasNext(ctx, id, cache))
1040
+ best = [st, en];
1041
+ }
1042
+ }
1043
+ if (best === null)
1044
+ return null;
1045
+ const spans = [];
1046
+ if (a > 0)
1047
+ spans.push([0, a]);
1048
+ if (b > 0)
1049
+ spans.push([question.length - b, question.length]);
1050
+ return { span: best, spans };
1051
+ }
1052
+ /** THE RELATION, READ OFF ANOTHER INSTANCE — the exact tier's second reading,
1053
+ * asked only when no establishing context of `id`'s continuations is
1054
+ * witnessed outright.
1055
+ *
1056
+ * `When was the director of film Jinpa born?` reaches `Pema Tseden`, whose
1057
+ * date-of-birth fact is established by `Pema Tseden date of birth`: the
1058
+ * question says `born`, the corpus says `date of birth`, and no window joins
1059
+ * them. What can join them is ANOTHER INSTANCE of the same question. A
1060
+ * stored context the question witnesses in every byte but ONE contiguous span
1061
+ * (`When was Peter Jackson born?`, residue `Peter Jackson`) is the question's
1062
+ * own frame around a different filler — a CO-INSTANCE. Its continuation is
1063
+ * established by other contexts too (`Peter Jackson date of birth`), and one
1064
+ * that holds the filler is the relation spelled the way the corpus spells it.
1065
+ * Put this node where the filler was and look the result up by content:
1066
+ * `Pema Tseden date of birth` exists, and its continuation is named.
1067
+ *
1068
+ * Nothing here is stored as a unit — not `born` ≈ `date of birth`, not the
1069
+ * frame — and nothing is approximate: the co-instance is witnessed, the
1070
+ * substitution is bytes, the target is an exact lookup that either exists
1071
+ * with this node's continuation or does not. A frame the question shares only
1072
+ * partly (`Where was … born?` against `When was …`) leaves two residues and
1073
+ * is no co-instance.
1074
+ *
1075
+ * BOUNDED: the frames are a property of the question and are read once per
1076
+ * question (`relationFrames`); co-instances are proposed by the edge-bearing
1077
+ * ancestors of its windows (`edgeAncestors`, the climb's own memoised reach;
1078
+ * a saturated window proposes nothing), rarest first, at most `allowance` of
1079
+ * them, each read once. A node pays one exact lookup per frame. */
1080
+ function byCoInstance(ctx, id, nx, asked, formCap, allowance) {
1081
+ const none = { named: null, spans: new Map() };
1082
+ const frames = relationFrames(ctx, asked, allowance);
1083
+ if (frames.length === 0)
1084
+ return none;
1085
+ const node = read(ctx, id, formCap);
1086
+ const support = new Map();
1087
+ const spans = new Map();
1088
+ const via = new Map();
1089
+ for (const fr of frames) {
1090
+ const t = resolve(ctx, concatBytes([fr.prefix, node, fr.suffix]));
1091
+ if (t === null || t === id)
1092
+ continue;
1093
+ for (const n of ctx.store.nextFirst(t, allowance)) {
1094
+ if (!nx.includes(n))
1095
+ continue;
1096
+ let by = support.get(n);
1097
+ if (by === undefined)
1098
+ support.set(n, by = new Set());
1099
+ for (const q of fr.by)
1100
+ by.add(q);
1101
+ if (!spans.has(n)) {
1102
+ spans.set(n, fr.spans);
1103
+ via.set(n, { q: fr.by[0], t });
1104
+ }
1105
+ }
1106
+ }
1107
+ let best = [];
1108
+ let most = 0;
1109
+ for (const n of nx) {
1110
+ const k = support.get(n)?.size ?? 0;
1111
+ if (k === 0)
1112
+ continue;
1113
+ if (k > most) {
1114
+ best = [n];
1115
+ most = k;
1116
+ }
1117
+ else if (k === most)
1118
+ best.push(n);
1119
+ }
1120
+ if (best.length === 0)
1121
+ return none;
1122
+ if (ctx.meter)
1123
+ ctx.meter.coInstanceNamings++;
1124
+ const first = via.get(best[0]);
1125
+ if (ctx.trace && first !== undefined) {
1126
+ ctx.trace.step("askedByCoInstance", [
1127
+ rItemShort(ctx, id, "node"),
1128
+ rItemShort(ctx, first.q, "co-instance"),
1129
+ rItemShort(ctx, first.t, "asked"),
1130
+ ], best.map((n) => rItemShort(ctx, n, "named")), `${nx.length} continuations — no establishing context is witnessed; ` +
1131
+ `${most} co-instance(s) of the question carry the relation to ` +
1132
+ `${best.length === 1 ? "one" : best.length} of them`);
1133
+ }
1134
+ const evidence = new Map();
1135
+ for (const n of best)
1136
+ evidence.set(n, spans.get(n) ?? []);
1137
+ return { named: best, spans: evidence };
1138
+ }
1139
+ /** The relation frames the question's co-instances carry — a property of the
1140
+ * QUESTION, not of the node asked about, so it is read once per question
1141
+ * (and once per walk material) and every node only pays the exact lookups.
1142
+ * A question the corpus holds verbatim is its own instance and reads none. */
1143
+ function relationFrames(ctx, asked, allowance) {
1144
+ // A co-instance is the question's frame around ONE other filler, so it is
1145
+ // read at most to twice the question's length.
1146
+ const formCap = 2 * asked.bytes.length;
1147
+ const hit = frameMemo.get(asked);
1148
+ if (hit !== undefined && !ctx.trace)
1149
+ return hit;
1150
+ const whole = ctx._edgeAsked;
1151
+ // THE PROPOSALS ARE THE CLIMB'S. The consensus climb already scored the
1152
+ // stored forms the question's regions reach — measured on the 2Wiki
1153
+ // fixture with one-hop questions, the co-instances were among its points —
1154
+ // so the tier reads co-instances from those and climbs nothing of its own.
1155
+ // Before the climb has run there is nothing to read, and nothing is
1156
+ // remembered: a later ask, after the climb, reads its points.
1157
+ const points = whole?.points;
1158
+ if (points === undefined)
1159
+ return [];
1160
+ const frames = [];
1161
+ frameMemo.set(asked, frames);
1162
+ if (whole !== null && resolve(ctx, whole.bytes) !== null)
1163
+ return frames;
1164
+ const W = ctx.space.maxGroup;
1165
+ const cache = getStructCache(ctx);
1166
+ const deposited = (c) => !cachedHasParents(ctx, c, cache) && !ctx.store.hasContainers(c);
1167
+ const spelled = new Map();
1168
+ // Most corroborated first (the climb's own ranking), at most
1169
+ // `chainReach(W)` read — the exact tier's own floor.
1170
+ const proposals = points.filter((c) => cachedHasNext(ctx, c, cache))
1171
+ .slice(0, Math.min(allowance, chainReach(W)));
1172
+ for (const q of proposals) {
1173
+ if (!deposited(q))
1174
+ continue;
1175
+ const raw = read(ctx, q, formCap + 1);
1176
+ if (raw.length > formCap)
1177
+ continue;
1178
+ if (ctx.meter)
1179
+ ctx.meter.coInstanceReads++;
1180
+ const co = coInstanceFiller(ctx, raw, [asked.index], asked.bytes);
1181
+ if (co === null)
1182
+ continue;
1183
+ const [fs, fe] = co.span;
1184
+ const filler = raw.subarray(fs, fe);
1185
+ for (const f of ctx.store.nextFirst(q, W)) {
1186
+ for (const c of ctx.store.prevFirst(f, allowance)) {
1187
+ if (c === q || !deposited(c))
1188
+ continue;
1189
+ const cb = read(ctx, c, formCap + 1);
1190
+ const at = indexOf(cb, filler, 0);
1191
+ if (at < 0)
1192
+ continue;
1193
+ const prefix = cb.subarray(0, at);
1194
+ const suffix = cb.subarray(at + filler.length);
1195
+ if (prefix.length + suffix.length === 0)
1196
+ continue;
1197
+ const key = latin1(prefix) + "\u0000" + latin1(suffix);
1198
+ const known = spelled.get(key);
1199
+ if (known === undefined) {
1200
+ spelled.set(key, { by: [q], prefix, suffix, spans: co.spans });
1201
+ }
1202
+ else if (!known.by.includes(q))
1203
+ known.by.push(q);
1204
+ }
1205
+ }
1206
+ }
1207
+ // ONE INSTANCE AGREES WITH NOTHING. A relation is read off the corpus only
1208
+ // where two co-instances spell it the same way — the agreement `reference`
1209
+ // demands of a frame (MIN_INSTANCES), for the same reason: a single
1210
+ // coincidental alignment is not evidence of what the frame means. Measured
1211
+ // on the trained store, long dialogue turns proposed hundreds of one-off
1212
+ // "frames", each an exact lookup for every node asked about.
1213
+ for (const fr of spelled.values())
1214
+ if (fr.by.length >= 2)
1215
+ frames.push(fr);
1216
+ frames.sort((a, b) => b.by.length - a.by.length);
1217
+ return frames;
1218
+ }
1219
+ const frameMemo = new WeakMap();
694
1220
  /** The perceived gist of a candidate node, through the session gist cache.
695
1221
  * Re-gisting a candidate is a full river fold of its bytes — the measured
696
1222
  * recall bottleneck (a hub context offers up to √N continuations, EACH
@@ -776,6 +1302,84 @@ export function allWindowsAreScaffolding(ctx, query) {
776
1302
  }
777
1303
  return sawOne;
778
1304
  }
1305
+ /** Per offset of `bytes`: 1 when the W-window there is a stored form contained
1306
+ * in more than √N places (corpus-global scaffolding; see the floor below),
1307
+ * else 0. Memoised per
1308
+ * byte array for the life of the store's read-only response. */
1309
+ export function hubWindows(ctx, bytes) {
1310
+ const hit = hubWindowMemo.get(bytes);
1311
+ if (hit !== undefined)
1312
+ return hit;
1313
+ const W = ctx.space.maxGroup;
1314
+ // Floored at the write side's arity: inside ONE deposit's fold a window is
1315
+ // already contained by up to `chainReach(W)` chunks and branches, so on a
1316
+ // store of a few facts the √N reading would call every window frame — that
1317
+ // is fold structure, not corpus commonality.
1318
+ const bound = Math.max(hubBound(ctx), chainReach(W));
1319
+ const hub = new Uint8Array(Math.max(0, bytes.length - W + 1));
1320
+ for (let o = 0; o < hub.length; o++) {
1321
+ const ids = leafIdRun(ctx, bytes, o, o + W);
1322
+ const id = ids === null ? null : ctx.store.findBranch(ids);
1323
+ if (id !== null && ctx.store.containersSlice(id, bound, 1).length > 0) {
1324
+ hub[o] = 1;
1325
+ }
1326
+ }
1327
+ hubWindowMemo.set(bytes, hub);
1328
+ return hub;
1329
+ }
1330
+ const hubWindowMemo = new WeakMap();
1331
+ /** The query's SCAFFOLDING CORE, as spans: the bytes every W-window over which
1332
+ * is a hub (see {@link hubWindows}) — what is nothing but frame, where
1333
+ * {@link scaffoldExtents} is what a frame window reaches. */
1334
+ export function scaffoldSpans(ctx, query) {
1335
+ const W = ctx.space.maxGroup;
1336
+ const hub = hubWindows(ctx, query);
1337
+ const n = hub.length;
1338
+ if (n <= 0)
1339
+ return [];
1340
+ const spans = [];
1341
+ let start = -1;
1342
+ for (let i = 0; i < query.length; i++) {
1343
+ let all = true;
1344
+ for (let o = Math.max(0, i - W + 1); o <= Math.min(i, n - 1); o++) {
1345
+ if (!hub[o]) {
1346
+ all = false;
1347
+ break;
1348
+ }
1349
+ }
1350
+ if (all && start < 0)
1351
+ start = i;
1352
+ if (!all && start >= 0) {
1353
+ spans.push([start, i]);
1354
+ start = -1;
1355
+ }
1356
+ }
1357
+ if (start >= 0)
1358
+ spans.push([start, query.length]);
1359
+ return spans;
1360
+ }
1361
+ /** The EXTENTS of the query's SCAFFOLDING windows, merged: every byte some
1362
+ * W-window reaches that is a stored form contained in more than √N places —
1363
+ * corpus-global commonality (commonality.md), the same "hub" reading as
1364
+ * {@link allWindowsAreScaffolding} and the bridge's `explainedSpan`. A window
1365
+ * the store never saw is NOT scaffolding. The extent, not the core, is what
1366
+ * a coverage test needs: a span that still holds one hub window can be
1367
+ * "carried" by any fact that holds that window. */
1368
+ export function scaffoldExtents(ctx, query) {
1369
+ const W = ctx.space.maxGroup;
1370
+ const hub = hubWindows(ctx, query);
1371
+ const spans = [];
1372
+ for (let o = 0; o < hub.length; o++) {
1373
+ if (!hub[o])
1374
+ continue;
1375
+ const last = spans[spans.length - 1];
1376
+ if (last !== undefined && o <= last[1])
1377
+ last[1] = o + W;
1378
+ else
1379
+ spans.push([o, o + W]);
1380
+ }
1381
+ return spans;
1382
+ }
779
1383
  // ── THE PREFIX SUPPLY ───────────────────────────────────────────────────────
780
1384
  //
781
1385
  // A RETRIEVAL capability, not a grounding one: "which trained forms does this