@hviana/sema 0.2.3 → 0.2.5

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 (60) hide show
  1. package/dist/src/geometry.d.ts +26 -0
  2. package/dist/src/geometry.js +32 -3
  3. package/dist/src/mind/attention.d.ts +246 -7
  4. package/dist/src/mind/attention.js +771 -173
  5. package/dist/src/mind/bridge.d.ts +30 -0
  6. package/dist/src/mind/bridge.js +569 -0
  7. package/dist/src/mind/index.d.ts +2 -0
  8. package/dist/src/mind/junction.d.ts +30 -1
  9. package/dist/src/mind/junction.js +73 -18
  10. package/dist/src/mind/match.d.ts +15 -2
  11. package/dist/src/mind/match.js +3 -8
  12. package/dist/src/mind/mechanisms/cast.d.ts +54 -0
  13. package/dist/src/mind/mechanisms/cast.js +268 -37
  14. package/dist/src/mind/mechanisms/cover.js +16 -31
  15. package/dist/src/mind/mechanisms/recall.js +66 -0
  16. package/dist/src/mind/mind.d.ts +2 -0
  17. package/dist/src/mind/rationale.d.ts +7 -2
  18. package/dist/src/mind/rationale.js +6 -5
  19. package/dist/src/mind/reasoning.d.ts +11 -0
  20. package/dist/src/mind/reasoning.js +58 -2
  21. package/dist/src/mind/recognition.js +127 -7
  22. package/dist/src/mind/traverse.js +90 -25
  23. package/dist/src/mind/types.d.ts +57 -2
  24. package/dist/src/mind/types.js +38 -7
  25. package/dist/src/store.d.ts +12 -3
  26. package/dist/src/store.js +9 -3
  27. package/package.json +1 -1
  28. package/src/geometry.ts +52 -3
  29. package/src/mind/attention.ts +1199 -128
  30. package/src/mind/bridge.ts +596 -0
  31. package/src/mind/index.ts +16 -0
  32. package/src/mind/junction.ts +125 -16
  33. package/src/mind/match.ts +19 -5
  34. package/src/mind/mechanisms/cast.ts +290 -38
  35. package/src/mind/mechanisms/cover.ts +23 -36
  36. package/src/mind/mechanisms/recall.ts +79 -0
  37. package/src/mind/mind.ts +15 -0
  38. package/src/mind/rationale.ts +12 -4
  39. package/src/mind/reasoning.ts +71 -2
  40. package/src/mind/recognition.ts +132 -7
  41. package/src/mind/traverse.ts +91 -24
  42. package/src/mind/types.ts +95 -6
  43. package/src/store.ts +19 -5
  44. package/test/36-already-answered-fusion.test.mjs +128 -0
  45. package/test/37-cluster-dispersion-fusion.test.mjs +190 -0
  46. package/test/38-reason-restate-guard.test.mjs +94 -0
  47. package/test/39-cast-restate-guard.test.mjs +102 -0
  48. package/test/40-choosenext-scale-guard.test.mjs +75 -0
  49. package/test/41-seatofnode-direction.test.mjs +85 -0
  50. package/test/42-recognise-trace-idempotence.test.mjs +106 -0
  51. package/test/43-cast-analog-seat.test.mjs +244 -0
  52. package/test/44-recognise-edge-whitespace.test.mjs +63 -0
  53. package/test/45-liftanswer-restated-trim.test.mjs +60 -0
  54. package/test/46-recognise-multibyte-edge.test.mjs +85 -0
  55. package/test/47-cast-comparison-coverage.test.mjs +134 -0
  56. package/test/48-recognise-turn-connective.test.mjs +125 -0
  57. package/test/49-natural-units-synonym-bridge.test.mjs +175 -0
  58. package/test/50-cast-analog-consensus-floor.test.mjs +179 -0
  59. package/test/51-structural-resonance-ladder.test.mjs +552 -0
  60. package/test/52-climb-consensus-instrumentation.test.mjs +324 -0
@@ -12,6 +12,7 @@
12
12
  // adjacent ANSWER pieces) and cross-region attention (the joint CONTEXT of two
13
13
  // non-adjacent QUERY regions) ascend by the same disciplined, bounded walk.
14
14
 
15
+ import type { Hit } from "../store.js";
15
16
  import type { MindContext } from "./types.js";
16
17
  import { read, resolve } from "./primitives.js";
17
18
  import { windowIds } from "./canonical.js";
@@ -27,6 +28,52 @@ export interface Junction {
27
28
  interior: Uint8Array;
28
29
  }
29
30
 
31
+ /** Which relaxation produced a {@link SynonymJunction}: one side replaced by
32
+ * a distributional halo sibling (`single-synonym`), or both (`double-
33
+ * synonym`) — the two remaining rungs of the graded ladder below exact DAG
34
+ * containment (see the module doc atop {@link junctionSynonyms}). */
35
+ export type SynonymJunctionTier =
36
+ | "single-synonym"
37
+ | "double-synonym";
38
+
39
+ export interface SynonymJunction extends Junction {
40
+ tier: SynonymJunctionTier;
41
+ /** Sibling score for a single-synonym junction; min(left, right) sibling
42
+ * score for a double-synonym junction. */
43
+ confidence: number;
44
+ }
45
+
46
+ /** The exact node ids and halo siblings resolved for one junction call's two
47
+ * sides — computed ONCE and reused by every ladder rung that needs them
48
+ * (junctionSynonyms' two tiers, and the structural-resonance tier beyond
49
+ * it). A failed synonym junction means only "no common DAG container was
50
+ * proven" — it does NOT mean the loaded siblings stop being useful. */
51
+ export interface JunctionSynonymSides {
52
+ leftId: number | null;
53
+ rightId: number | null;
54
+ leftSiblings: Hit[];
55
+ rightSiblings: Hit[];
56
+ }
57
+
58
+ /** Resolve `left`/`right` to their exact node ids (when known) and load each
59
+ * resolved side's halo siblings once — deterministic (haloSiblings already
60
+ * ranks nearest-first) and shared by every ladder rung that consults
61
+ * siblings, so no ladder rung repeats a halo ANN query the previous one
62
+ * already paid for. */
63
+ export async function loadJunctionSynonymSides(
64
+ ctx: MindContext,
65
+ left: Uint8Array,
66
+ right: Uint8Array,
67
+ ): Promise<JunctionSynonymSides> {
68
+ const leftId = resolve(ctx, left);
69
+ const rightId = resolve(ctx, right);
70
+ const leftSiblings = leftId !== null ? await haloSiblings(ctx, leftId) : [];
71
+ const rightSiblings = rightId !== null
72
+ ? await haloSiblings(ctx, rightId)
73
+ : [];
74
+ return { leftId, rightId, leftSiblings, rightSiblings };
75
+ }
76
+
30
77
  /** Seed node ids to ascend from for one side of a junction: the side's own
31
78
  * node when it is a stored form, plus — when the node has no structural
32
79
  * parents — its canonical window ids. A non-W-aligned node may have no
@@ -292,20 +339,32 @@ export async function junctionSynonyms(
292
339
  right: Uint8Array,
293
340
  maxInterior: number,
294
341
  unordered = false,
295
- ): Promise<Junction[]> {
296
- const out: Junction[] = [];
297
-
298
- const lId = resolve(ctx, left);
299
- const rId = resolve(ctx, right);
300
- if (lId === null && rId === null) return out;
342
+ sides?: JunctionSynonymSides,
343
+ ): Promise<SynonymJunction[]> {
344
+ const s = sides ?? await loadJunctionSynonymSides(ctx, left, right);
345
+ if (s.leftId === null && s.rightId === null) return [];
301
346
 
302
- const budget = { n: hubBound(ctx) * ctx.space.maxGroup };
347
+ // ── Tier 2.5a: single-synonym one side replaced by a halo sibling ──────
348
+ // ONE shared expansion budget across BOTH directions of this tier.
349
+ const singleBudget = { n: hubBound(ctx) * ctx.space.maxGroup };
350
+ const singleOut = new Map<number, SynonymJunction>();
351
+ const keepBest = (
352
+ map: Map<number, SynonymJunction>,
353
+ j: Junction,
354
+ tier: SynonymJunctionTier,
355
+ confidence: number,
356
+ ) => {
357
+ const prev = map.get(j.id);
358
+ if (prev === undefined || confidence > prev.confidence) {
359
+ map.set(j.id, { ...j, tier, confidence });
360
+ }
361
+ };
303
362
 
304
363
  // Left-side synonyms: containers of sibling+right. `right`'s seeds are
305
364
  // FIXED across every sibling this loop tries.
306
- if (lId !== null) {
365
+ if (s.leftId !== null) {
307
366
  const rightSeeds = junctionSeeds(ctx, right);
308
- for (const sib of await haloSiblings(ctx, lId)) {
367
+ for (const sib of s.leftSiblings) {
309
368
  const sibBytes = read(ctx, sib.id, maxInterior + 1);
310
369
  if (sibBytes.length === 0 || sibBytes.length > maxInterior) continue;
311
370
  const containers = junctionContainersFrom(
@@ -315,18 +374,20 @@ export async function junctionSynonyms(
315
374
  sibBytes.length + right.length + maxInterior,
316
375
  junctionSeeds(ctx, sibBytes),
317
376
  rightSeeds,
318
- budget,
377
+ singleBudget,
319
378
  unordered,
320
379
  );
321
- for (const c of containers) out.push(c);
380
+ for (const c of containers) {
381
+ keepBest(singleOut, c, "single-synonym", sib.score);
382
+ }
322
383
  }
323
384
  }
324
385
 
325
386
  // Right-side synonyms: containers of left+sibling. `left`'s seeds are
326
387
  // likewise fixed across this loop.
327
- if (rId !== null) {
388
+ if (s.rightId !== null) {
328
389
  const leftSeeds = junctionSeeds(ctx, left);
329
- for (const sib of await haloSiblings(ctx, rId)) {
390
+ for (const sib of s.rightSiblings) {
330
391
  const sibBytes = read(ctx, sib.id, maxInterior + 1);
331
392
  if (sibBytes.length === 0 || sibBytes.length > maxInterior) continue;
332
393
  const containers = junctionContainersFrom(
@@ -336,12 +397,60 @@ export async function junctionSynonyms(
336
397
  left.length + sibBytes.length + maxInterior,
337
398
  leftSeeds,
338
399
  junctionSeeds(ctx, sibBytes),
339
- budget,
400
+ singleBudget,
340
401
  unordered,
341
402
  );
342
- for (const c of containers) out.push(c);
403
+ for (const c of containers) {
404
+ keepBest(singleOut, c, "single-synonym", sib.score);
405
+ }
343
406
  }
344
407
  }
345
408
 
346
- return out;
409
+ if (singleOut.size > 0) return [...singleOut.values()];
410
+
411
+ // ── Tier 2.5b: double-synonym — BOTH sides replaced, tried only when
412
+ // single-synonym found NOTHING. Every (leftSibling, rightSibling) pair,
413
+ // sorted deterministically, bounded to haloQueryK pairs total, ONE fresh
414
+ // shared budget for the whole tier. ─────────────────────────────────────
415
+ if (s.leftSiblings.length === 0 || s.rightSiblings.length === 0) return [];
416
+
417
+ const pairs: Array<{ l: Hit; r: Hit; confidence: number }> = [];
418
+ for (const l of s.leftSiblings) {
419
+ for (const r of s.rightSiblings) {
420
+ pairs.push({ l, r, confidence: Math.min(l.score, r.score) });
421
+ }
422
+ }
423
+ pairs.sort((a, b) =>
424
+ b.confidence - a.confidence ||
425
+ a.l.id - b.l.id ||
426
+ a.r.id - b.r.id
427
+ );
428
+
429
+ const doubleOut = new Map<number, SynonymJunction>();
430
+ const budget = { n: hubBound(ctx) * ctx.space.maxGroup };
431
+ const tries = Math.min(pairs.length, ctx.cfg.haloQueryK);
432
+ for (let i = 0; i < tries; i++) {
433
+ const { l, r, confidence } = pairs[i];
434
+ const lBytes = read(ctx, l.id, maxInterior + 1);
435
+ const rBytes = read(ctx, r.id, maxInterior + 1);
436
+ if (
437
+ lBytes.length === 0 || lBytes.length > maxInterior ||
438
+ rBytes.length === 0 || rBytes.length > maxInterior
439
+ ) continue;
440
+ const containers = junctionContainersFrom(
441
+ ctx,
442
+ lBytes,
443
+ rBytes,
444
+ lBytes.length + rBytes.length + maxInterior,
445
+ junctionSeeds(ctx, lBytes),
446
+ junctionSeeds(ctx, rBytes),
447
+ budget,
448
+ unordered,
449
+ );
450
+ for (const c of containers) {
451
+ keepBest(doubleOut, c, "double-synonym", confidence);
452
+ }
453
+ }
454
+
455
+ return [...doubleOut.values()];
347
456
  }
package/src/mind/match.ts CHANGED
@@ -325,18 +325,32 @@ export async function haloSiblings(
325
325
  * strength, not a pick. Returns the direct halo cosine, or failing that the
326
326
  * highest mutual-halo-sibling min-score (second-order analogy), or failing
327
327
  * that the SHARED-FRAME strength (below) — the gate CAST's comparison
328
- * schema validates genuine analogs with (bar: significanceBar). */
328
+ * schema validates genuine analogs with (bar: significanceBar).
329
+ *
330
+ * The result names its TIER alongside the score: `halo: true` means the
331
+ * score cleared a significanceBar-gated HALO tier (direct company cosine
332
+ * or mutual-sibling) — genuine distributional evidence; `halo: false`
333
+ * means only the structural shared-frame fallback matched, a coverage
334
+ * fraction with no bar of its own. CAST's comparison gate treats the two
335
+ * differently (see cast.ts): halo evidence stands alone, frame evidence
336
+ * needs the query to have named the analog or the climb root to be
337
+ * trusted. */
338
+ export interface AnalogyEvidence {
339
+ score: number;
340
+ halo: boolean;
341
+ }
342
+
329
343
  export async function analogyStrength(
330
344
  ctx: MindContext,
331
345
  a: number,
332
346
  b: number,
333
- ): Promise<number> {
347
+ ): Promise<AnalogyEvidence> {
334
348
  const ha = ctx.store.halo(a);
335
349
  const hb = ctx.store.halo(b);
336
350
  if (ha && hb) {
337
351
  const bar = significanceBar(ctx.store.D);
338
352
  const direct = cosine(ha, hb);
339
- if (direct >= bar) return direct;
353
+ if (direct >= bar) return { score: direct, halo: true };
340
354
  const sibsA = await haloSiblings(ctx, a, ha, bar);
341
355
  const sibsB = await haloSiblings(ctx, b, hb, bar);
342
356
  let best = 0;
@@ -347,9 +361,9 @@ export async function analogyStrength(
347
361
  best = Math.max(best, Math.min(x.score, y.score));
348
362
  }
349
363
  }
350
- if (best > 0) return best;
364
+ if (best > 0) return { score: best, halo: true };
351
365
  }
352
- return sharedFrameStrength(ctx, a, b);
366
+ return { score: sharedFrameStrength(ctx, a, b), halo: false };
353
367
  }
354
368
 
355
369
  /** The STRUCTURAL analogy tier: two nodes are analogs when their byte
@@ -12,8 +12,9 @@
12
12
  // juxtapose.
13
13
 
14
14
  import type { MindContext } from "../types.js";
15
+ import type { Vec } from "../../vec.js";
15
16
  import { read } from "../primitives.js";
16
- import { argmaxBy, hubBound } from "../traverse.js";
17
+ import { argmaxBy, corpusN, hubBound } from "../traverse.js";
17
18
  import {
18
19
  analogyStrength,
19
20
  follow,
@@ -22,11 +23,17 @@ import {
22
23
  reverseContext,
23
24
  } from "../match.js";
24
25
  import { joinWithBridge } from "../resonance.js";
26
+ import { restatesQuery } from "../reasoning.js";
25
27
  import { CONCEPT, STEP } from "../graph-search.js";
26
28
  import { concat2, indexOf } from "../../bytes.js";
27
- import { dominates } from "../../geometry.js";
28
- import { decodeText, unexplainedLabel } from "../rationale.js";
29
+ import { consensusFloor, dominates } from "../../geometry.js";
30
+ import {
31
+ decodeText,
32
+ unexplainedLabel,
33
+ unexplainedSpans,
34
+ } from "../rationale.js";
29
35
  import { rItem, rNode } from "../trace.js";
36
+ import { dismissedKnownContent } from "../bridge.js";
30
37
 
31
38
  // ── CAST gates ────────────────────────────────────────────────────────────
32
39
  //
@@ -80,6 +87,85 @@ export interface CastResult {
80
87
  unexplained: string;
81
88
  }
82
89
 
90
+ /** The seat that establishes a node's role in an analogical comparison:
91
+ * the REVERSE context (what leads to it) when a predecessor genuinely
92
+ * ESTABLISHES id — introduces or describes it by name — else the FORWARD
93
+ * continuation (what it leads to), else `fallback`.
94
+ *
95
+ * An earlier version gated this purely on `prevCount(id) > 0`: any
96
+ * predecessor at all was treated as proof of a genuine named ENTITY
97
+ * (seat it by what established it), while no predecessor meant a bare
98
+ * learnt CONTEXT (seat it by what it leads to, since voicing it verbatim
99
+ * would answer a question with a question). That test measured the wrong
100
+ * thing — a broad sample of this store's own question-shaped nodes showed
101
+ * the large majority (≈71%) have at least one predecessor, most of them a
102
+ * handful of generic, high-fan-out sentences that recur as an INCIDENTAL
103
+ * neighbour to dozens of otherwise-unrelated destinations (a SmolSent-
104
+ * style sentence-adjacency artifact, never naming or describing what
105
+ * follows). Traced live: "What is the capital of France?" — whose own
106
+ * forward edge unambiguously resolves to "The capital of France is
107
+ * Paris." — has exactly one such incidental predecessor ("Create an
108
+ * example of a types of questions a GPT model can answer.?"), wrongly
109
+ * read as disqualifying proof of "genuine entity."
110
+ *
111
+ * A plain forward-first swap (matching {@link project}'s universal
112
+ * priority) over-corrected: test/29's C2/C3 pin that a genuine entity
113
+ * analog (e.g. "Leonardo da Vinci", established by "The Mona Lisa was
114
+ * painted by Leonardo da Vinci.") must be seated by that establishing
115
+ * sentence, NOT by its own biography fact — voicing the bio leaks exactly
116
+ * what a comparison must keep out, and loses the embedded "Mona Lisa"
117
+ * term C3 relies on for a further hop.
118
+ *
119
+ * The distinguishing signal is content-addressed, not a count: a genuine
120
+ * establishing predecessor's bytes CONTAIN id's own bytes — it names or
121
+ * describes id ("...painted by Leonardo da Vinci." contains "Leonardo da
122
+ * Vinci"). An incidental adjacency predecessor never does — it merely
123
+ * preceded id in some unrelated document without ever mentioning it. No
124
+ * new tuned constant: containment is the same primitive `restatesQuery`
125
+ * and `dominates`-style checks already use throughout this codebase.
126
+ *
127
+ * `allowForward` (default true) gates the FORWARD branch specifically —
128
+ * see the call sites below: the DOMINANT is what the query is actually
129
+ * ASKING, so completing it forward is the whole point; an ANALOG is only
130
+ * being CITED for comparison; the query never asked about IT, so chasing
131
+ * its own further continuation drifts onto whatever coincidentally
132
+ * follows it in the corpus. Traced live: the analog "What is the capital
133
+ * of Japan?\nTokyo is the capital of Japan." is ALREADY a complete,
134
+ * self-answering unit (prevCount 0, so no establishing predecessor
135
+ * either) — its sole forward edge is "And what is the capital of the
136
+ * Moon?", an unrelated quiz question sharing nothing but corpus
137
+ * adjacency. With forward disallowed, an analog like this falls through
138
+ * to `fallback` — its own bytes, exactly the complete fact that made it a
139
+ * genuine analog in the first place. See
140
+ * test/41-seatofnode-direction.test.mjs and
141
+ * test/43-cast-analog-seat.test.mjs. */
142
+ export async function seatOfNode(
143
+ ctx: MindContext,
144
+ id: number,
145
+ guide: Vec | null | undefined,
146
+ fallback: Uint8Array,
147
+ allowForward = true,
148
+ ): Promise<Uint8Array> {
149
+ const rev = ctx.store.prevFirst(id, hubBound(ctx));
150
+ if (rev.length > 0) {
151
+ const own = read(ctx, id);
152
+ const establishing = rev.some((p) => indexOf(read(ctx, p), own, 0) >= 0);
153
+ if (establishing) {
154
+ const back = reverseContext(ctx, id, guide, rev);
155
+ if (back !== null) return back;
156
+ }
157
+ }
158
+ // The "last resort, non-establishing reverse" fallback below is itself a
159
+ // LESS CERTAIN projection (the same tier as forward) — an analog
160
+ // (allowForward: false) must stop at `fallback` (its own bytes) here
161
+ // rather than fall back to a predecessor that already failed the
162
+ // establishing check just above.
163
+ if (!allowForward) return fallback;
164
+ const fwd = await follow(ctx, id, guide);
165
+ if (fwd !== null) return fwd;
166
+ return reverseContext(ctx, id, guide, rev) ?? fallback;
167
+ }
168
+
83
169
  /** CAST's own entry gates, checked once here and reused by
84
170
  /** The main CAST entry point. Given a query and its pre-computed pre.rec.sites,
85
171
  * determine whether the query weaves together multiple independent learnt
@@ -297,7 +383,10 @@ export async function counterfactualTransfer(
297
383
  const tail = proj.ctx.subarray(seat.cs);
298
384
  let answer = await joinWithBridge(ctx, filler, tail);
299
385
  const fwd = await follow(ctx, proj.anchor, qv);
300
- if (fwd !== null && indexOf(answer, fwd, 0) < 0) {
386
+ if (
387
+ fwd !== null && indexOf(answer, fwd, 0) < 0 &&
388
+ !restatesQuery(query, fwd)
389
+ ) {
301
390
  answer = concat2(answer, fwd);
302
391
  }
303
392
  ctx.trace?.step(
@@ -375,36 +464,14 @@ export async function counterfactualTransfer(
375
464
  // seed-dependent failure where the climb ranks an exemplar above a
376
465
  // person node and the person node is excluded from points by run-
377
466
  // overlap trimming.
378
- // The seat that establishes a candidate's role: the REVERSE projection
379
- // (the context it follows), voiced by the query gist — falling back to the
380
- // candidate's own bytes when it follows nothing. DELIBERATE STRENGTHENING
381
- // over the pre-refactor code: reverseContext also returns null when the
382
- // picked context READS EMPTY (a dangling id degrades to zero bytes), so a
383
- // corrupted-store read now falls back here instead of voicing a hollow
384
- // seat into the comparison — the same "empty bytes are no grounding"
385
- // invariant project() has always enforced.
386
- // A node that only ever CONTINUES — an edge SOURCE with no predecessor —
387
- // is a learnt CONTEXT (a stored question-shaped frame), not an entity.
388
- // Voicing it verbatim answers a question with a question (the observed
389
- // cast echo: two stored questions concatenated as an "answer"). The
390
- // context's role is established by what it LEADS TO, so its seat is its
391
- // own continuation — the fact — with the reverse projection and the raw
392
- // bytes as the ordinary fallbacks for genuine entities.
393
- const seatOfNode = async (
394
- id: number,
395
- fallback: Uint8Array,
396
- ): Promise<Uint8Array> => {
397
- if (ctx.store.prevCount(id) === 0 && ctx.store.hasNext(id)) {
398
- const fwd = await follow(ctx, id, qv);
399
- if (fwd !== null) return fwd;
400
- }
401
- return reverseContext(ctx, id, qv) ?? fallback;
402
- };
403
- const seatOf = (p: Point): Promise<Uint8Array> => seatOfNode(p.anchor, p.ctx);
467
+ // The seat that establishes a candidate's role see {@link seatOfNode}.
468
+ const seatOf = (p: Point, allowForward = true): Promise<Uint8Array> =>
469
+ seatOfNode(ctx, p.anchor, qv, p.ctx, allowForward);
404
470
  interface AnalogCandidate {
405
471
  anchor: number;
406
472
  /** The point this candidate came from, or null when it is a nextOf
407
- * descendant — then seatOfNode supplies the seat. */
473
+ * descendant — then its own bytes ARE the seat (already one meaningful
474
+ * hop from `src`; see the comparison gate below). */
408
475
  point: Point | null;
409
476
  /** For a nextOf descendant: the aligned point whose continuation edge
410
477
  * named it. Its runs ARE the query evidence this analog rests on
@@ -447,8 +514,46 @@ export async function counterfactualTransfer(
447
514
  }
448
515
  let bestAnalog: AnalogCandidate | null = null;
449
516
  let bestSim = 0;
517
+ let bestHalo = false;
518
+ // Whether the query itself NAMES a candidate. A directly aligned point
519
+ // is named by construction — its runs ARE query bytes. A hop-reached
520
+ // candidate is named when its own bytes contain the query text of an
521
+ // aligned run of the point whose continuation edge reached it (that
522
+ // alignment IS the query evidence the hop rests on — the same reading
523
+ // cmpAccounted already prices): "William Shakespeare", reached off
524
+ // "Macbeth was written by William Shakespeare.", contains the src's
525
+ // 12-byte aligned run " Shakespeare" — test/29 C2/C3. The run must span
526
+ // at least TWO perception windows (2·W, the same two-quantum floor
527
+ // CAST's own entry gate holds the whole query to): a single shared
528
+ // W-window is exactly the frame tier's own evidence quantum — the level
529
+ // "half the corpus" shares — and stopword scraps (" the ", "he b",
530
+ // 4–5 bytes) never reach two windows, while a genuinely named entity
531
+ // does. NOT the weave's usable()/frame filter: weave depth counts every
532
+ // ranked exemplar, so a query's own named entity recurring across
533
+ // exemplars ("Shakespeare" in Hamlet+Macbeth+…) is wrongly classified as
534
+ // frame — measured live, it silently disqualified C3's genuine analog.
535
+ const namedByQuery = (c: AnalogCandidate): boolean => {
536
+ if (c.point !== null) return true;
537
+ const bytes = read(ctx, c.anchor);
538
+ return c.src.runs.some((r) =>
539
+ r.qe - r.qs >= 2 * quantum &&
540
+ indexOf(bytes, query.subarray(r.qs, r.qe), 0) >= 0
541
+ );
542
+ };
543
+ // Whether any committed root's consensus vote clears the SAME trust bar
544
+ // recallByResonance applies before grounding through a climb root:
545
+ // consensusFloor(N) = ln(N) + 1/2. The climb's FIRST root is
546
+ // deliberately floor-free (attention.ts: "the dominant one always
547
+ // grounds") — fine for ORIENTING mechanisms, not for voicing learnt
548
+ // content the query never asked about. Computed once here; both the
549
+ // hub fallback below and the comparison gate consume it.
550
+ const rootTrusted = roots.some((r) => r.vote >= consensusFloor(corpusN(ctx)));
450
551
  for (const c of analogs) {
451
- const sim = await analogyStrength(ctx, dominant.anchor, c.anchor);
552
+ const { score: sim, halo } = await analogyStrength(
553
+ ctx,
554
+ dominant.anchor,
555
+ c.anchor,
556
+ );
452
557
  ctx.trace?.step(
453
558
  "tryAnalog",
454
559
  [
@@ -456,11 +561,12 @@ export async function counterfactualTransfer(
456
561
  rNode(ctx, c.anchor, "candidate", sim),
457
562
  ],
458
563
  [],
459
- `analogy strength ${sim.toFixed(4)}`,
564
+ `analogy strength ${sim.toFixed(4)}${halo ? " (halo tier)" : ""}`,
460
565
  );
461
566
  if (sim > bestSim) {
462
567
  bestSim = sim;
463
568
  bestAnalog = c;
569
+ bestHalo = halo;
464
570
  }
465
571
  }
466
572
  // When every candidate fails the similarity gates (halo company — now
@@ -489,6 +595,17 @@ export async function counterfactualTransfer(
489
595
  let hubMass = -1;
490
596
  const fanClamp = hubBound(ctx) + 1;
491
597
  for (const c of analogs) {
598
+ // A fallback comparison carries NO similarity evidence at all. Its
599
+ // honesty rests on the grounding decider discounting it against
600
+ // richer candidates (the design note below) — an assumption that
601
+ // holds only when the climb itself settled on this query with real
602
+ // evidence. Under a root the consensus floor does not trust, an
603
+ // unnamed, hop-reached hub is pure corpus adjacency: refusing it is
604
+ // what kept the live wrong echo silent. A hub the query itself
605
+ // NAMED stays eligible either way (test/29 C2/C3's "William
606
+ // Shakespeare"); an unnamed one under a TRUSTED root stays eligible
607
+ // too (test/33 1b's deliberately weak second candidate).
608
+ if (!rootTrusted && !namedByQuery(c)) continue;
492
609
  // Evidence clamped at the hub bound: beyond √N + 1 the exact fan-out
493
610
  // no longer discriminates (every mega-hub ties at the clamp), and
494
611
  // counting it exactly would require the corpus-sized read.
@@ -538,10 +655,111 @@ export async function counterfactualTransfer(
538
655
  // climb's own forest, never tuned; substitution/redirection stay
539
656
  // unaffected — they orient around a displaced seat, not a whole-topic
540
657
  // analogy.
658
+ //
659
+ // roots.length <= 1 is a PROXY for "the query is about one thing" — it is
660
+ // only as good as the climb's own root-commitment, which depends on
661
+ // recognise() having found something to commit a root TO. When the
662
+ // query's newest content genuinely isn't recognised (not boundary noise —
663
+ // real, uncommitted content; see the session's own investigation of the
664
+ // France→Spain live trace), the climb under-commits roots and this proxy
665
+ // is fooled: comparison looks licensed to treat the query as one topic
666
+ // when it is not.
667
+ //
668
+ // The direct check is the SAME accounted spans comparison is about to
669
+ // cite as its evidence: unexplainedSpans (rationale.ts, the same gap
670
+ // computation the trace's own `unexplained` diagnostic uses) names every
671
+ // stretch of the query NEITHER the dominant NOR the analog's evidence
672
+ // touches. A short comparison query ("How is ice like steel?") legitimately
673
+ // accounts for only its two short entity spans — the surrounding "How is
674
+ // ... like ...?" framing is real but SHORT, split into several small gaps,
675
+ // none of them the bulk of the query. The live bug's shape is different in
676
+ // kind, not degree: ONE contiguous, substantial gap — a whole second
677
+ // question the query added that comparison's two spans never touch at all.
678
+ //
679
+ // Two bars, both derived, neither tuned:
680
+ // • the largest gap must not DOMINATE the whole query (the same
681
+ // predicate CAST's own frame gate uses) — rules out a gap that is
682
+ // most of the query outright;
683
+ // • the largest gap must be SMALLER than the dominant's own established
684
+ // context. A gap can't be dismissed as mere connective framing once
685
+ // it is at least as large as the topic being compared FROM — at that
686
+ // scale it isn't glue between two named things, it's substantial
687
+ // enough to be a second topic in its own right. This is what
688
+ // actually separates the live bug (a 47-byte gap against a 30-byte
689
+ // dominant — the ignored content is bigger than the topic itself)
690
+ // from ordinary short comparisons (a 9-byte gap against an 11-byte
691
+ // dominant — the gap is smaller than what's being compared): the two
692
+ // cases land on the same side of "half the query" often enough
693
+ // (both can exceed or clear it) that the query-relative bar alone
694
+ // does not reliably separate them — the topic-relative scale does.
695
+ const cmpAccounted: Array<[number, number]> = bestAnalog !== null
696
+ ? [...runSpans(dominant), ...runSpans(bestAnalog.point ?? bestAnalog.src)]
697
+ : [];
698
+ const cmpGaps = unexplainedSpans(query.length, cmpAccounted);
699
+ const cmpMaxGap = cmpGaps.reduce((n, [s, e]) => Math.max(n, e - s), 0);
700
+ // An analog that is not itself a directly ALIGNED point (point !== null —
701
+ // its own runs are query bytes, the query NAMED it) was only reached
702
+ // through a continuation hop or the structural-hub fallback. Voicing
703
+ // learnt content the query never named is the same act recallByResonance
704
+ // refuses to perform through a climb root whose consensus vote is below
705
+ // consensusFloor(N) = ln(N) + 1/2 (recall.ts's minVote), so comparison
706
+ // holds the climb to that SAME bar before citing a hop-reached analog:
707
+ // some committed root must clear the floor. The climb's FIRST root is
708
+ // deliberately floor-free (attention.ts: "the dominant one always
709
+ // grounds") — fine for ORIENTING mechanisms, not for transferring
710
+ // unnamed content through. The live bug this gates (real trained store,
711
+ // 325k edge sources, floor 13.2): the query's stopword scraps pooled a
712
+ // 1.92 vote that committed an unrelated haiku exemplar as the sole root,
713
+ // and comparison voiced that exemplar's continuation through a
714
+ // hop-reached analog while every other mechanism honestly refused. A
715
+ // directly aligned analog needs no floor — the query's own bytes are its
716
+ // evidence (test/29 C1's "Steel is hard" for "How is ice like steel?").
717
+ // See test/50-cast-analog-consensus-floor.
718
+ // A HALO-tier best analog needs neither: its similarity already cleared
719
+ // significanceBar-gated distributional company (analogyStrength's
720
+ // `halo`) — genuine evidence in its own right, the very case the halo
721
+ // gate exists for (test/33 1b's nickname-corroborated analog). Only a
722
+ // FRAME-tier or fallback analog — whose "similarity" is an unbarred
723
+ // coverage fraction or nothing — needs the query's naming or the climb's
724
+ // trust.
725
+ const analogNamed = bestAnalog !== null && namedByQuery(bestAnalog);
726
+ // NOTE — two further gates were tried here and empirically REFUTED,
727
+ // recorded so they are not re-tried:
728
+ // • dominant self-coverage (dominant's aligned runs must dominate its
729
+ // own ctx): legitimate dominants sit at the same coverage as junk
730
+ // ones ("The Mona Lisa was painted by…" 16/47 vs the live junk
731
+ // haiku ~10/54) — no separation.
732
+ // • denying the shared-frame similarity tier to hop-reached analogs:
733
+ // semantically right in isolation, but it merely promoted the next
734
+ // junk candidate — an ALIGNED scrap-matched point ("The affluence…",
735
+ // frame 0.157) — into bestAnalog on the live store, and the aligned
736
+ // configuration is byte-structurally IDENTICAL to test/29 C1's
737
+ // legitimate one ("Steel is hard", frame 0.364): every derived
738
+ // local separator measured (run length, site overlap, frame
739
+ // query-containment, weave-usable classification) falls on the same
740
+ // side for both. Only corpus-scale consensus separates them, which
741
+ // is exactly what `rootTrusted` prices.
742
+ // FRAME-tier evidence under an UNTRUSTED root is comparison's weakest
743
+ // licence (an unbarred coverage fraction, a climb the consensus floor
744
+ // does not trust). There it is additionally held to the IGNORED-KNOWN
745
+ // principle (dismissedKnownContent, bridge.ts): the two analogs' aligned
746
+ // runs must account for every STORED window of the query. This is the
747
+ // byte-structural separator the refuted-gates note below could not find
748
+ // locally: a legitimate small-corpus comparison ("How is ice like
749
+ // steel?") leaves only UNATTESTED spans ("How ", " like ") unexplained,
750
+ // while a scrap-matched junk pair leaves the query's own trained content
751
+ // ("…songs…times…", "…planet…sun.") dismissed as gaps. Halo-tier and
752
+ // trusted-root comparisons are exempt — their evidence already stands.
753
+ const cmpDismisses = !(bestHalo || rootTrusted) &&
754
+ dismissedKnownContent(ctx, query, cmpAccounted);
541
755
  if (
542
756
  bestAnalog !== null &&
757
+ (bestHalo || analogNamed || rootTrusted) &&
758
+ !cmpDismisses &&
543
759
  dominant.ctx.length <= query.length &&
544
- roots.length <= 1
760
+ roots.length <= 1 &&
761
+ !dominates(cmpMaxGap, query.length) &&
762
+ cmpMaxGap < dominant.ctx.length
545
763
  ) {
546
764
  ctx.trace?.step(
547
765
  "validateAnalogy",
@@ -553,9 +771,21 @@ export async function counterfactualTransfer(
553
771
  "the two structures keep distributional company beyond chance — genuine analogs",
554
772
  );
555
773
  const a = await seatOf(dominant);
774
+ // The analog is only being CITED for comparison — the query never asked
775
+ // about it — so its seat never chases a FORWARD continuation (see
776
+ // seatOfNode's `allowForward`): only reverse (if a predecessor genuinely
777
+ // establishes it) or its own bytes. A DIRECTLY aligned point
778
+ // (bestAnalog.point !== null) still goes through seatOfNode for that
779
+ // reverse check (a bare entity NAME like "Leonardo da Vinci" needs it —
780
+ // test/29's C2/C3). A nextOf DESCENDANT (point === null) was already
781
+ // reached by following ONE meaningful hop off another aligned point (the
782
+ // alignment loop above: "its nextOf is the hub... and the hub's own
783
+ // [...] context will be the seat") — its own bytes ARE that seat
784
+ // directly, with no predecessor to even check (it was found by a
785
+ // forward edge, not matched in the query).
556
786
  const b = bestAnalog.point !== null
557
- ? await seatOf(bestAnalog.point)
558
- : await seatOfNode(bestAnalog.anchor, read(ctx, bestAnalog.anchor));
787
+ ? await seatOf(bestAnalog.point, false)
788
+ : read(ctx, bestAnalog.anchor);
559
789
  const answer = await joinWithBridge(ctx, a, b);
560
790
  record(
561
791
  answer,
@@ -568,10 +798,32 @@ export async function counterfactualTransfer(
568
798
  // when it was an aligned point, else the source point whose
569
799
  // continuation edge reached it (that alignment IS the query evidence
570
800
  // the hop rests on).
801
+ cmpAccounted,
802
+ );
803
+ } else if (
804
+ bestAnalog !== null &&
805
+ dominant.ctx.length <= query.length &&
806
+ roots.length <= 1
807
+ ) {
808
+ ctx.trace?.step(
809
+ "validateAnalogy",
571
810
  [
572
- ...runSpans(dominant),
573
- ...runSpans(bestAnalog.point ?? bestAnalog.src),
811
+ rNode(ctx, dominant.anchor, "analog", bestSim),
812
+ rNode(ctx, bestAnalog.anchor, "analog", bestSim),
574
813
  ],
814
+ [],
815
+ !(bestHalo || analogNamed || rootTrusted)
816
+ ? `the best analog carries no halo-tier company evidence, was never ` +
817
+ `named by the query, and no committed root's consensus vote ` +
818
+ `clears the floor, so comparison refuses to voice it`
819
+ : cmpDismisses
820
+ ? `a frame-tier analog under an untrusted root dismisses stored ` +
821
+ `query content its alignment never accounted for — comparison ` +
822
+ `refuses to ignore what the store knows`
823
+ : `comparison's own accounted evidence leaves a ${cmpMaxGap}-byte gap in ` +
824
+ `a ${query.length}-byte query against a ${dominant.ctx.length}-byte ` +
825
+ `dominant — too large to be mere framing — so it refuses rather ` +
826
+ `than paper over it with an analog the query never asked about`,
575
827
  );
576
828
  }
577
829
  t?.done(