@hviana/sema 0.3.0 → 0.4.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 (79) hide show
  1. package/AGENTS.md +71 -5
  2. package/CONTRIBUTING.md +92 -10
  3. package/LICENSE.md +2 -2
  4. package/dist/src/derive/src/deduction.d.ts +12 -1
  5. package/dist/src/derive/src/deduction.js +5 -1
  6. package/dist/src/derive/src/index.d.ts +1 -0
  7. package/dist/src/geometry.d.ts +3 -30
  8. package/dist/src/geometry.js +330 -82
  9. package/dist/src/index.d.ts +1 -0
  10. package/dist/src/index.js +1 -0
  11. package/dist/src/meter.d.ts +171 -0
  12. package/dist/src/meter.js +269 -0
  13. package/dist/src/mind/attention.d.ts +0 -4
  14. package/dist/src/mind/attention.js +251 -23
  15. package/dist/src/mind/bridge.d.ts +10 -1
  16. package/dist/src/mind/bridge.js +179 -10
  17. package/dist/src/mind/canonical.d.ts +6 -1
  18. package/dist/src/mind/canonical.js +6 -1
  19. package/dist/src/mind/graph-search.d.ts +9 -0
  20. package/dist/src/mind/graph-search.js +59 -19
  21. package/dist/src/mind/junction.d.ts +10 -0
  22. package/dist/src/mind/junction.js +14 -0
  23. package/dist/src/mind/match.d.ts +40 -0
  24. package/dist/src/mind/match.js +125 -1
  25. package/dist/src/mind/mechanisms/cast.js +63 -6
  26. package/dist/src/mind/mechanisms/extraction.d.ts +0 -34
  27. package/dist/src/mind/mechanisms/extraction.js +1 -88
  28. package/dist/src/mind/mechanisms/recall.d.ts +3 -0
  29. package/dist/src/mind/mechanisms/recall.js +77 -14
  30. package/dist/src/mind/mind.d.ts +55 -4
  31. package/dist/src/mind/mind.js +110 -91
  32. package/dist/src/mind/pipeline-mechanism.d.ts +33 -3
  33. package/dist/src/mind/pipeline-mechanism.js +179 -10
  34. package/dist/src/mind/pipeline.js +52 -10
  35. package/dist/src/mind/primitives.d.ts +11 -15
  36. package/dist/src/mind/primitives.js +47 -28
  37. package/dist/src/mind/reasoning.d.ts +7 -1
  38. package/dist/src/mind/reasoning.js +40 -8
  39. package/dist/src/mind/recognition.js +93 -20
  40. package/dist/src/mind/traverse.d.ts +11 -0
  41. package/dist/src/mind/traverse.js +58 -5
  42. package/dist/src/mind/types.d.ts +28 -5
  43. package/dist/src/store.d.ts +15 -0
  44. package/dist/src/store.js +91 -6
  45. package/package.json +1 -1
  46. package/src/derive/src/deduction.ts +15 -0
  47. package/src/derive/src/index.ts +1 -0
  48. package/src/geometry.ts +350 -122
  49. package/src/index.ts +1 -0
  50. package/src/meter.ts +333 -0
  51. package/src/mind/attention.ts +268 -31
  52. package/src/mind/bridge.ts +187 -10
  53. package/src/mind/canonical.ts +6 -1
  54. package/src/mind/graph-search.ts +60 -21
  55. package/src/mind/junction.ts +12 -0
  56. package/src/mind/match.ts +146 -1
  57. package/src/mind/mechanisms/cast.ts +62 -6
  58. package/src/mind/mechanisms/extraction.ts +2 -103
  59. package/src/mind/mechanisms/recall.ts +84 -17
  60. package/src/mind/mind.ts +139 -98
  61. package/src/mind/pipeline-mechanism.ts +203 -13
  62. package/src/mind/pipeline.ts +65 -8
  63. package/src/mind/primitives.ts +49 -33
  64. package/src/mind/reasoning.ts +39 -7
  65. package/src/mind/recognition.ts +89 -19
  66. package/src/mind/traverse.ts +59 -5
  67. package/src/mind/types.ts +28 -5
  68. package/src/store.ts +75 -6
  69. package/test/14-scaling.test.mjs +17 -7
  70. package/test/31-audit.test.mjs +4 -1
  71. package/test/33-multi-candidate.test.mjs +13 -1
  72. package/test/36-already-answered-fusion.test.mjs +10 -3
  73. package/test/46-recognise-multibyte-edge.test.mjs +3 -3
  74. package/test/53-cross-region-probe-instrumentation.test.mjs +36 -6
  75. package/test/55-cost-meter.test.mjs +284 -0
  76. package/test/56-bridge-identity-admission.test.mjs +209 -0
  77. package/test/57-fusion-order.test.mjs +104 -0
  78. package/test/58-subquantum-sites.test.mjs +112 -0
  79. package/test/59-fold-invariance.test.mjs +226 -0
@@ -37,7 +37,12 @@ import {
37
37
  import { foldTree, gistOf, latin1Key, perceive, read } from "./primitives.js";
38
38
  import { recognise } from "./recognition.js";
39
39
  import { leafIdRun } from "./canonical.js";
40
- import { corpusN, edgeAncestors, hubBound } from "./traverse.js";
40
+ import {
41
+ corpusN,
42
+ edgeAncestors,
43
+ hubBound,
44
+ sharedReachMemo,
45
+ } from "./traverse.js";
41
46
  import {
42
47
  cachedRead,
43
48
  type Junction,
@@ -475,6 +480,7 @@ export async function climbAttentionAll(
475
480
  }
476
481
  const hit = byRead.get(modeKey);
477
482
  if (hit !== undefined) {
483
+ if (ctx.meter) ctx.meter.climbHits++;
478
484
  // Cache-hit exit (spec §9): the abbreviated payload shape — only what
479
485
  // is actually stored in the cached AttentionRead is reported. No
480
486
  // candidate, reach, saturation, pooling or anchor detail is fabricated
@@ -514,6 +520,7 @@ export async function computeAttention(
514
520
  k: number,
515
521
  mode: DFMode,
516
522
  ): Promise<AttentionRead> {
523
+ if (ctx.meter) ctx.meter.climbs++;
517
524
  const regions = collectRegions(ctx, query);
518
525
  const perceivedCount = regions.length;
519
526
 
@@ -531,6 +538,22 @@ export async function computeAttention(
531
538
  v: gistOf(ctx, query.subarray(s.start, s.end)),
532
539
  start: s.start,
533
540
  end: s.end,
541
+ // NOT a chunk — a precondition, not a judgement about the evidence.
542
+ // `chunk` admits a region into the saturated-INTERVAL builder (see
543
+ // crossRegionVotes), which walks regions as a SEQUENCE and merges
544
+ // neighbouring saturated ones into runs. Its own contract requires the
545
+ // regions it reads to be DISJOINT and in byte order — true of
546
+ // leaf-parents, false of sites, which overlap each other and the chunks
547
+ // ("red", "circle" and "red circle" are all present at once).
548
+ //
549
+ // Admitting them was measured both ways and is unprincipled in each
550
+ // direction: a saturated site EXTENDS a run and masks votes that should
551
+ // have won (test/37 lost all three — roots became "red " and "hat"
552
+ // instead of "red circle" and "2"), while a non-saturated one BREAKS a
553
+ // run and unmasks votes that should have been dropped, which is the only
554
+ // reason it appeared to fix test/34. Either way the outcome turns on
555
+ // where an overlapping span happens to fall in the array — the same
556
+ // positional accident this work exists to remove.
534
557
  chunk: false,
535
558
  known: true, // a recognised site IS a stored form
536
559
  });
@@ -557,25 +580,30 @@ export async function computeAttention(
557
580
  const N = corpusN(ctx);
558
581
  // One climb per distinct anchor for the WHOLE query: regions sharing a
559
582
  // chunk, and canonicalChunkId's prefix probes, all hit this memo instead of
560
- // re-reading the anchor's full edge fan-out from the store.
561
- const reachMemo = new Map<number, AncestorReach>();
562
- const rvs = await voteRegions(ctx, query, regions, k, mode, N, reachMemo, td);
583
+ // re-reading the anchor's full edge fan-out from the store. The memo is
584
+ // the SHARED one (traverse.ts) — response-scoped for respond(),
585
+ // conversation-scoped across turns, and the same map confluence prices
586
+ // commonality against; it used to be a private per-climb Map, so a
587
+ // conversation re-climbed its own repeated regions from cold on every
588
+ // turn. A traced response still gets a fresh one — see sharedReachMemo.
589
+ const reachMemo = sharedReachMemo(ctx);
590
+ const rvs = ctx.meter
591
+ ? await ctx.meter.time(
592
+ "climb.voteRegions",
593
+ () => voteRegions(ctx, query, regions, k, mode, N, reachMemo, td),
594
+ )
595
+ : await voteRegions(ctx, query, regions, k, mode, N, reachMemo, td);
563
596
 
564
597
  // ── Cross-region: DIRECT region-to-region interaction ─────────────────
565
598
  // Two regions whose individual climbs land on DIFFERENT contexts leave
566
599
  // their JOINT context — the learnt whole that contains BOTH — with no
567
600
  // vote. crossRegionVotes recovers it by the bridge's content-addressed
568
601
  // junction ascent (see the note above the function).
569
- const cross = await crossRegionVotes(
570
- ctx,
571
- query,
572
- regions,
573
- rvs,
574
- k,
575
- N,
576
- reachMemo,
577
- td,
578
- );
602
+ const crossArgs = () =>
603
+ crossRegionVotes(ctx, query, regions, rvs, k, N, reachMemo, td);
604
+ const cross = ctx.meter
605
+ ? await ctx.meter.time("climb.crossRegion", crossArgs)
606
+ : await crossArgs();
579
607
  // A vote SUPERSEDED by exact joint evidence (its bytes literally live
580
608
  // inside the joint container, yet it climbed elsewhere — grid aliasing)
581
609
  // is dropped, not down-weighted: the joint container explains it away.
@@ -635,7 +663,19 @@ export function collectRegions(ctx: MindContext, query: Uint8Array): Region[] {
635
663
  // node, the same lookups a deposit pays.
636
664
  foldTree(ctx, perceive(ctx, query), 0, (n, start, end, node) => {
637
665
  if (n.kids === null) return;
638
- if (!dominates(end - start, query.length) || regions.length === 0) {
666
+ // The dominance filter is about WRAPPERS, not about size. A chunk is the
667
+ // smallest grouped unit — it wraps no other region — so it can never be the
668
+ // "broad, non-discriminative wrapper" this rule exists to exclude, however
669
+ // much of a short query it happens to cover. Testing it by span alone was
670
+ // safe only while chunks were exactly W bytes: content-defined segments run
671
+ // up to the keyring's seat count, so on a 15-byte query the 8-byte segment
672
+ // "is frigi" counted as dominant and was discarded, leaving CAST one point
673
+ // of attention where it needs two (test/29 D1/D2). Composites are still
674
+ // filtered exactly as before.
675
+ if (
676
+ isChunk(n) || !dominates(end - start, query.length) ||
677
+ regions.length === 0
678
+ ) {
639
679
  regions.push({
640
680
  v: n.v,
641
681
  start,
@@ -644,6 +684,25 @@ export function collectRegions(ctx: MindContext, query: Uint8Array): Region[] {
644
684
  known: node !== null,
645
685
  });
646
686
  }
687
+ // MEASURED AND REFUTED — subdividing a long segment into W-scale tiles.
688
+ // A content segment runs from W−1 up to the keyring's seat count (2W) and
689
+ // folds FLAT, so its only sub-units are single bytes; the grid's regions
690
+ // were always exactly W. Offering each segment's W-byte tiles as extra
691
+ // regions (gists only, no stored nodes, anchored on the segment's own
692
+ // content-defined start so invariance is kept) does restore that finer
693
+ // grain: on `How is ice like steel?` the climb went from ONE ranked anchor
694
+ // to three, and test/33's own CAST-candidate spread was recovered.
695
+ //
696
+ // It is still wrong, and net worse (measured: test/29 went 9/2 to 7/4).
697
+ // canonicalWindows governs EXACT identity lookup, which recognition
698
+ // already probes at every offset — it says nothing about the grain of an
699
+ // approximate gist, so "the write side's unit scale" was two machineries
700
+ // conflated. What the tiles actually do is reintroduce a fixed stride
701
+ // inside the segment, and the extra votes reorder the climb: on
702
+ // `How is Shakespeare like Leonardo da Vinci?` the short name deposits
703
+ // outranked the exemplar sentences, claimed their aligned runs first, and
704
+ // left the sentences CAST needs with no free run at all (C2, C3). A
705
+ // region must come from the fold, not from a stride over it.
647
706
  });
648
707
  return regions;
649
708
  }
@@ -662,13 +721,18 @@ export async function voteRegions(
662
721
  saturated: boolean[];
663
722
  voters: Array<{ id: number; score: number; w: number } | null>;
664
723
  }> {
724
+ if (ctx.meter) ctx.meter.climbRegions += regions.length;
665
725
  const regionSaturated: boolean[] = new Array(regions.length).fill(false);
666
726
  const regionVotes: RegionVote[] = [];
667
727
  const regionVoter: Array<{ id: number; score: number; w: number } | null> =
668
728
  ctx.trace ? regions.map(() => null) : [];
669
729
 
730
+ const W = ctx.space.maxGroup;
670
731
  for (let ri = 0; ri < regions.length; ri++) {
671
- const { v, start, end, chunk, known } = regions[ri];
732
+ // `v`/`start`/`end` are rebindable: a long approximate segment may vote
733
+ // with the sub-span that actually carries its evidence — see below.
734
+ let { v, start, end } = regions[ri];
735
+ const { chunk, known } = regions[ri];
672
736
  // Trace-only bookkeeping for this region — allocated only under `td`
673
737
  // (i.e. only when ctx.trace is set); see ConsensusRegionTrace/
674
738
  // RegionOutcome (spec §4). `examinedIds` tracks distinct ANN hits
@@ -712,10 +776,10 @@ export async function voteRegions(
712
776
  // the resonate() call for most exact regions — the single largest
713
777
  // remaining inference sink — with the anchor choice unchanged (the
714
778
  // canonical branch already ignored hits[0]).
715
- const canonicalId = chunk
779
+ let canonicalId = chunk
716
780
  ? canonicalChunkId(ctx, query.subarray(start, end), N, reachMemo)
717
781
  : null;
718
- const canonicalUsable = canonicalId !== null &&
782
+ let canonicalUsable = canonicalId !== null &&
719
783
  (ctx.store.hasParents(canonicalId) ||
720
784
  ctx.store.hasContainers(canonicalId));
721
785
  let hits: readonly Hit[] | null = null;
@@ -727,6 +791,80 @@ export async function voteRegions(
727
791
  return hits;
728
792
  };
729
793
 
794
+ // A DILUTED SEGMENT VOTES WITH THE SPAN THAT CARRIES ITS EVIDENCE.
795
+ //
796
+ // A content segment runs up to the keyring's seat count and folds FLAT, so
797
+ // its gist superposes every one of its bytes: an entity inside a longer
798
+ // segment is averaged together with whatever scaffolding shares the
799
+ // segment, and the resonance reads the average. Measured on
800
+ // `How is ice like steel?` against a store holding `Steel is hard`: the
801
+ // segment `ike stee` resonates to `Ice is c` at 0.297 — the WRONG deposit —
802
+ // with `Steel ` fourth at 0.123, while the sub-span `stee` resonates to
803
+ // `Steel ` at 0.627. The evidence is there; the whole-segment read cannot
804
+ // see it, and `Steel is hard` received no vote at all (test/29 C1).
805
+ //
806
+ // Entered only after the EXACT path has already failed — a chunk with a
807
+ // usable canonical identity has a content-addressed handle on its own bytes
808
+ // and needs no estimator at all — so this is honest degradation, not extra
809
+ // work on regions that already resolved. The candidates are the segment's
810
+ // two EDGE sub-spans at the write side's own unit scale (W) — the scale
811
+ // `canonicalWindows` interns, and the only one at which a sub-span could
812
+ // carry a stored identity; a segment of W or less has no interior at all.
813
+ // Edges because a content cut lands INSIDE a unit, so the remnant it split
814
+ // sits against the cut: `steel` is cut after `stee`. Offering every
815
+ // interior offset instead was measured and is worse — it re-anchors
816
+ // segments on spans no boundary ever separated, and broke three of
817
+ // test/17's vote-distribution and root-count assertions (428/1 → 424/5).
818
+ //
819
+ // Selection is by the SAME quantity the region's vote is weighted by —
820
+ // score² · idf — never by score alone: the scaffolding window `is i`
821
+ // resonates at 0.832, far above `stee`, and is worth nothing because its
822
+ // reach is the whole corpus. Nothing new is being measured here; the
823
+ // choice the code did not previously make is made with the criterion it
824
+ // already uses. The region's SPAN narrows with its gist, so breadth,
825
+ // clusters and cross-region pairing all see where the evidence really sits.
826
+ if (!canonicalUsable && chunk && !known && end - start > W) {
827
+ const weigh = (h: Hit): { w: number; id: number } | null => {
828
+ const r = edgeAncestors(ctx, h.id, N, reachMemo);
829
+ if (r.saturated || r.roots.length === 0) return null;
830
+ const idf = Math.log(N / Math.max(1, r.contextsReached));
831
+ if (idf <= 0) return null;
832
+ return { w: h.score * h.score * idf, id: h.id };
833
+ };
834
+ // The whole-segment candidate reuses the ranking the region needs
835
+ // anyway, so only the two edge probes are new work.
836
+ const scoreOf = async (
837
+ gist: Vec,
838
+ ): Promise<{ w: number; id: number } | null> => {
839
+ const h = await ctx.store.resonate(gist, 1);
840
+ return h.length === 0 ? null : weigh(h[0]);
841
+ };
842
+ const h0 = await ensureHits();
843
+ let best = h0.length > 0 ? weigh(h0[0]) : null;
844
+ let bestSpan: [number, number, Vec] | null = null;
845
+ for (const s0 of [start, end - W]) {
846
+ const sub = gistOf(ctx, query.subarray(s0, s0 + W));
847
+ const cand = await scoreOf(sub);
848
+ if (cand !== null && (best === null || cand.w > best.w)) {
849
+ best = cand;
850
+ bestSpan = [s0, s0 + W, sub];
851
+ }
852
+ }
853
+ if (bestSpan !== null) {
854
+ [start, end, v] = bestSpan;
855
+ hits = null; // the whole-segment ranking no longer describes this span
856
+ canonicalId = canonicalChunkId(
857
+ ctx,
858
+ query.subarray(start, end),
859
+ N,
860
+ reachMemo,
861
+ );
862
+ canonicalUsable = canonicalId !== null &&
863
+ (ctx.store.hasParents(canonicalId) ||
864
+ ctx.store.hasContainers(canonicalId));
865
+ }
866
+ }
867
+
730
868
  const canonicalFailed = chunk && canonicalId === null;
731
869
  let voterId: number;
732
870
  let score: number;
@@ -1023,6 +1161,9 @@ export function poolVotes(
1023
1161
  };
1024
1162
  }
1025
1163
 
1164
+ // The one hub bound (traverse.ts) — N here IS corpusN, threaded down from
1165
+ // computeAttention. Read once, not per rule application.
1166
+ const bound = hubBound(ctx);
1026
1167
  const key = (it: AItem) =>
1027
1168
  it.kind === "region"
1028
1169
  ? `r${it.ri}`
@@ -1053,13 +1194,14 @@ export function poolVotes(
1053
1194
  // region's vote across its FULL corpus-sized fan-in yields O(corpus)
1054
1195
  // rule applications per region and near-zero per-target weight anyway.
1055
1196
  // Cap the redistribution at the first √N contexts (insertion order,
1056
- // the same convention chooseNext caps by).
1057
- const hubBound = Math.ceil(Math.sqrt(N));
1197
+ // the same convention chooseNext caps by). Hoisted out of the
1198
+ // generator: `rules` is invoked once per popped item, and this used to
1199
+ // re-derive the bound on every one of them.
1058
1200
  for (const r of rv.roots) {
1059
1201
  // CAPPED read: only the first hubBound targets are ever credited, so
1060
1202
  // only they are read — a common continuation's full reverse fan-in
1061
1203
  // is corpus-sized and is never materialised.
1062
- const pv = ctx.store.prevFirst(r, hubBound);
1204
+ const pv = ctx.store.prevFirst(r, bound);
1063
1205
  const isAnswer = pv.length > 0 && !ctx.store.hasNext(r);
1064
1206
  const targets = isAnswer ? pv : [r];
1065
1207
  for (const t of targets) {
@@ -1106,7 +1248,12 @@ export function poolVotes(
1106
1248
  const rv = regionVotes[p0.ri];
1107
1249
  breadthSum += rv.absorbed ?? 1;
1108
1250
  premises.push({ kind: "form", span: [rv.start, rv.end] });
1109
- spans.push([rv.start, rv.end]);
1251
+ // A vote knows where its own evidence sits: `parts` when it stands on
1252
+ // several separate places (a joint binding), the merged span
1253
+ // otherwise. See RegionVote.parts.
1254
+ if (rv.parts !== undefined) {
1255
+ for (const [s, e] of rv.parts) spans.push([s, e]);
1256
+ } else spans.push([rv.start, rv.end]);
1110
1257
  }
1111
1258
  regionSupport.set(pc.item.id, breadthSum);
1112
1259
  regionSpans.set(pc.item.id, spans);
@@ -1411,13 +1558,38 @@ export function canonicalChunkId(
1411
1558
  reachMemo?: Map<number, AncestorReach>,
1412
1559
  ): number | null {
1413
1560
  const len = Math.min(regionBytes.length, ctx.space.maxGroup);
1561
+ // WHICH window anchors a region is decided by reach, not by position. This
1562
+ // used to return at the FIRST offset that matched, which was indistinguishable
1563
+ // from correct while every region was exactly W bytes — there was only one
1564
+ // offset. A content-defined segment is longer, and its first window is
1565
+ // whatever happens to start it: for "is frigi" that is " is ", pure
1566
+ // scaffolding, which reaches every context, saturates, and makes the whole
1567
+ // region ABSTAIN. The region's own content ("frigi") never got a say, and
1568
+ // CAST lost a point of attention it needed (test/29 D1/D2).
1569
+ //
1570
+ // So scan every offset and prefer an anchor that still discriminates: not
1571
+ // saturated, and among those the one reaching the FEWEST contexts (§2.7,
1572
+ // corpus-global). Only when every window in the region saturates does the
1573
+ // old generalising choice stand — there is then no discriminative anchor to
1574
+ // find, and abstaining is the honest outcome.
1575
+ let discId: number | null = null;
1576
+ let discReached = Infinity;
1577
+ let fallback: number | null = null;
1414
1578
  for (let off = 0; off + len <= regionBytes.length; off++) {
1415
1579
  const ids = leafIdRun(ctx, regionBytes, off, off + len);
1416
- if (ids === null) return null;
1580
+ // An unknown byte disqualifies THIS window, not the region. This used to
1581
+ // abandon the whole region on the first unseen byte, which was
1582
+ // indistinguishable from correct while regions were exactly W bytes — there
1583
+ // was one window, so failing it was failing the region. A content-defined
1584
+ // segment holds several windows, and a single unknown byte near its start
1585
+ // was silently costing the region its anchor entirely.
1586
+ if (ids === null) continue;
1417
1587
  const flatId = ctx.store.findBranch(ids);
1418
1588
  if (flatId === null) continue;
1419
1589
  if (len < 2) return flatId;
1420
1590
 
1591
+ // Within one window, the widest reach is still the right CANONICAL
1592
+ // identity — a chunk's anchor should be its most general stable form.
1421
1593
  let bestId = flatId;
1422
1594
  let bestReach = edgeAncestors(ctx, flatId, N, reachMemo);
1423
1595
  for (let k2 = 1; k2 < len; k2++) {
@@ -1433,9 +1605,16 @@ export function canonicalChunkId(
1433
1605
  bestReach = shortReach;
1434
1606
  }
1435
1607
  }
1436
- return bestId;
1608
+ if (fallback === null) fallback = bestId;
1609
+ if (!bestReach.saturated && bestReach.contextsReached < discReached) {
1610
+ discId = bestId;
1611
+ discReached = bestReach.contextsReached;
1612
+ // Nothing can discriminate better than reaching ONE context, so the scan
1613
+ // stops there rather than pricing the rest of the segment's windows.
1614
+ if (discReached <= 1) break;
1615
+ }
1437
1616
  }
1438
- return null;
1617
+ return discId ?? fallback;
1439
1618
  }
1440
1619
 
1441
1620
  export function naturalBreak(votes: number[]): number {
@@ -1794,6 +1973,24 @@ function betterProposal(
1794
1973
  * ANN-query each, merge proposals by candidate id, and validate the winner
1795
1974
  * through the SAME structural gates every other tier answers to (saturation,
1796
1975
  * roots, IDF, contrastive margin). Returns null when nothing survives. */
1976
+ /** {@link structuralResonance}, charged to its own profiling phase — it is
1977
+ * the halo-mediated arm of the cross-region ladder and the one part of it
1978
+ * that resonates. */
1979
+ async function meteredStructuralResonance(
1980
+ ...args: Parameters<typeof structuralResonance>
1981
+ ): Promise<
1982
+ ReturnType<typeof structuralResonance> extends Promise<infer R> ? R
1983
+ : never
1984
+ > {
1985
+ const ctx = args[0];
1986
+ return ctx.meter
1987
+ ? await ctx.meter.time(
1988
+ "climb.structuralResonance",
1989
+ () => structuralResonance(...args),
1990
+ )
1991
+ : await structuralResonance(...args);
1992
+ }
1993
+
1797
1994
  export async function structuralResonance(
1798
1995
  ctx: MindContext,
1799
1996
  query: Uint8Array,
@@ -1995,7 +2192,19 @@ async function crossRegionVotes(
1995
2192
  // one side is always individually discriminative.
1996
2193
  //
1997
2194
  // Only MAXIMAL spans compose: a span contained in another candidate is a
1998
- // fragment of that candidate's evidence, never independent of it.
2195
+ // fragment of that candidate's evidence, never independent of it — but
2196
+ // containment alone does not establish that relation. An APPROXIMATE
2197
+ // container (a fold segment whose gist merely resonated) does not hold the
2198
+ // evidence of an EXACT one (a recognised site, content-addressed): its
2199
+ // bytes straddle the site rather than explain it, so calling the site a
2200
+ // fragment of it discards the only exact reading of those bytes. Measured:
2201
+ // on `blue then square` the segment `blue t` swallowed the site `blue`,
2202
+ // leaving one candidate and no pair, while on `red then circle` the
2203
+ // segment happened to end at `red `'s edge and the same query shape
2204
+ // composed — the outcome turned on where a cut fell. This is the same
2205
+ // discipline the between-region gate below already states: an approximate
2206
+ // region climbing "somewhere" is ordinary noise, not evidence.
2207
+ //
1999
2208
  // Shared across every cross-region probe in this climb: a sibling
2000
2209
  // successfully reconstructed while probing one pair must not be read and
2001
2210
  // perceived again while probing another pair in the same climb.
@@ -2019,7 +2228,8 @@ async function crossRegionVotes(
2019
2228
  y !== x &&
2020
2229
  regions[y].start <= regions[x].start &&
2021
2230
  regions[x].end <= regions[y].end &&
2022
- regions[y].end - regions[y].start > regions[x].end - regions[x].start
2231
+ regions[y].end - regions[y].start > regions[x].end - regions[x].start &&
2232
+ (regions[y].known || !regions[x].known)
2023
2233
  )
2024
2234
  );
2025
2235
  const none = { votes: [], superseded: new Set<RegionVote>() };
@@ -2285,7 +2495,7 @@ async function crossRegionVotes(
2285
2495
  const ownRootsB = rvs.votes.find((v) =>
2286
2496
  v.start === rb.start && v.end === rb.end
2287
2497
  )?.roots;
2288
- structuralPick = await structuralResonance(
2498
+ structuralPick = await meteredStructuralResonance(
2289
2499
  ctx,
2290
2500
  query,
2291
2501
  ra,
@@ -2462,9 +2672,26 @@ async function crossRegionVotes(
2462
2672
  spanStart = Math.min(spanStart, regions[ei].start);
2463
2673
  spanEnd = Math.max(spanEnd, regions[ei].end);
2464
2674
  }
2465
- consumed.add(cand[a]);
2466
- consumed.add(cand[b]);
2467
- for (const ei of bestExtras) consumed.add(ei);
2675
+ // CONSUMPTION IS FOR CONTAINER-BACKED EVIDENCE ONLY. Consuming a
2676
+ // candidate says "its evidence is already composed at full joint
2677
+ // strength, re-pairing it would vote the same container twice" — a
2678
+ // claim only a real container can make. A structural-resonance pick
2679
+ // has none (see above: it is NOT a Junction), so consuming its
2680
+ // endpoints locks up candidates on the strength of an ANN guess and
2681
+ // stops genuine evidence from ever composing them. Measured: on
2682
+ // `greet reply-greet then red then circle` the pair
2683
+ // `reply-greet` ▸ `red` resonated to `red square` and consumed `red`,
2684
+ // after which `red` ▸ `circle` was never probed and the exact junction
2685
+ // `red circle` — a stored whole, sitting right there — went unfound.
2686
+ // This is spec §15's asymmetry (only exact DAG evidence may explain
2687
+ // ordinary votes away) applied to the other way a tier can silence
2688
+ // evidence. Both votes now stand and pooling decides between them,
2689
+ // which is what the mechanism market is for.
2690
+ if (structuralPick === null) {
2691
+ consumed.add(cand[a]);
2692
+ consumed.add(cand[b]);
2693
+ for (const ei of bestExtras) consumed.add(ei);
2694
+ }
2468
2695
 
2469
2696
  // EXPLAINING AWAY — see the block comment above the function. Byte
2470
2697
  // containment in the joint container is the relatedness test (the
@@ -2514,6 +2741,16 @@ async function crossRegionVotes(
2514
2741
  w,
2515
2742
  wFocus: w,
2516
2743
  absorbed: 1 + explainedAway,
2744
+ // The places this junction actually stands on — its two endpoints and
2745
+ // any N-ary extras, NOT the merged span [spanStart, spanEnd], which
2746
+ // swallows the gap and reads as one neighbourhood. See
2747
+ // RegionVote.parts.
2748
+ parts: [cand[a], cand[b], ...bestExtras]
2749
+ .map((ri): readonly [number, number] => [
2750
+ regions[ri].start,
2751
+ regions[ri].end,
2752
+ ])
2753
+ .sort((x, y) => x[0] - y[0]),
2517
2754
  });
2518
2755
  pushProbe("accepted");
2519
2756
  if (td) {