@hviana/sema 0.5.4 → 0.5.6

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 (140) hide show
  1. package/README.md +34 -7
  2. package/dist/example/demo.d.ts +1 -0
  3. package/dist/example/demo.js +39 -0
  4. package/dist/example/train_base.d.ts +93 -0
  5. package/dist/example/train_base.js +2269 -0
  6. package/dist/src/alphabet.d.ts +7 -0
  7. package/dist/src/alphabet.js +33 -0
  8. package/dist/src/alu/src/alu.d.ts +185 -0
  9. package/dist/src/alu/src/alu.js +440 -0
  10. package/dist/src/alu/src/expr.d.ts +61 -0
  11. package/dist/src/alu/src/expr.js +318 -0
  12. package/dist/src/alu/src/index.d.ts +11 -0
  13. package/dist/src/alu/src/index.js +19 -0
  14. package/dist/src/alu/src/kernel-arith.d.ts +16 -0
  15. package/dist/src/alu/src/kernel-arith.js +264 -0
  16. package/dist/src/alu/src/kernel-bits.d.ts +19 -0
  17. package/dist/src/alu/src/kernel-bits.js +152 -0
  18. package/dist/src/alu/src/kernel-logic.d.ts +4 -0
  19. package/dist/src/alu/src/kernel-logic.js +60 -0
  20. package/dist/src/alu/src/kernel-nd.d.ts +3 -0
  21. package/dist/src/alu/src/kernel-nd.js +208 -0
  22. package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
  23. package/dist/src/alu/src/kernel-numeric.js +366 -0
  24. package/dist/src/alu/src/operation.d.ts +168 -0
  25. package/dist/src/alu/src/operation.js +189 -0
  26. package/dist/src/alu/src/parser.d.ts +221 -0
  27. package/dist/src/alu/src/parser.js +577 -0
  28. package/dist/src/alu/src/resonance.d.ts +55 -0
  29. package/dist/src/alu/src/resonance.js +126 -0
  30. package/dist/src/alu/src/text.d.ts +31 -0
  31. package/dist/src/alu/src/text.js +73 -0
  32. package/dist/src/alu/src/value.d.ts +109 -0
  33. package/dist/src/alu/src/value.js +300 -0
  34. package/dist/src/alu/test/alu.test.d.ts +1 -0
  35. package/dist/src/alu/test/alu.test.js +764 -0
  36. package/dist/src/bytes.d.ts +14 -0
  37. package/dist/src/bytes.js +59 -0
  38. package/dist/src/canon.d.ts +45 -0
  39. package/dist/src/canon.js +85 -0
  40. package/dist/src/config.d.ts +111 -0
  41. package/dist/src/config.js +91 -0
  42. package/dist/src/derive/src/deduction.d.ts +136 -0
  43. package/dist/src/derive/src/deduction.js +159 -0
  44. package/dist/src/derive/src/index.d.ts +8 -0
  45. package/dist/src/derive/src/index.js +11 -0
  46. package/dist/src/derive/src/priority-queue.d.ts +20 -0
  47. package/dist/src/derive/src/priority-queue.js +73 -0
  48. package/dist/src/derive/src/rewrite.d.ts +56 -0
  49. package/dist/src/derive/src/rewrite.js +100 -0
  50. package/dist/src/derive/src/trie.d.ts +90 -0
  51. package/dist/src/derive/src/trie.js +217 -0
  52. package/dist/src/derive/test/derive.test.d.ts +1 -0
  53. package/dist/src/derive/test/derive.test.js +122 -0
  54. package/dist/src/extension.d.ts +37 -0
  55. package/dist/src/extension.js +7 -0
  56. package/dist/src/geometry.d.ts +233 -0
  57. package/dist/src/geometry.js +1089 -0
  58. package/dist/src/index.d.ts +17 -0
  59. package/dist/src/index.js +19 -0
  60. package/dist/src/ingest-cache.d.ts +41 -0
  61. package/dist/src/ingest-cache.js +165 -0
  62. package/dist/src/meter.d.ts +176 -0
  63. package/dist/src/meter.js +274 -0
  64. package/dist/src/mind/articulation.d.ts +6 -0
  65. package/dist/src/mind/articulation.js +99 -0
  66. package/dist/src/mind/attention.d.ts +430 -0
  67. package/dist/src/mind/attention.js +2418 -0
  68. package/dist/src/mind/bridge.d.ts +37 -0
  69. package/dist/src/mind/bridge.js +907 -0
  70. package/dist/src/mind/canonical.d.ts +34 -0
  71. package/dist/src/mind/canonical.js +93 -0
  72. package/dist/src/mind/graph-search.d.ts +294 -0
  73. package/dist/src/mind/graph-search.js +996 -0
  74. package/dist/src/mind/index.d.ts +9 -0
  75. package/dist/src/mind/index.js +5 -0
  76. package/dist/src/mind/junction.d.ts +137 -0
  77. package/dist/src/mind/junction.js +358 -0
  78. package/dist/src/mind/learning.d.ts +75 -0
  79. package/dist/src/mind/learning.js +265 -0
  80. package/dist/src/mind/match.d.ts +404 -0
  81. package/dist/src/mind/match.js +1039 -0
  82. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  83. package/dist/src/mind/mechanisms/alu.js +36 -0
  84. package/dist/src/mind/mechanisms/cast.d.ts +89 -0
  85. package/dist/src/mind/mechanisms/cast.js +1186 -0
  86. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  87. package/dist/src/mind/mechanisms/confluence.js +271 -0
  88. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  89. package/dist/src/mind/mechanisms/cover.js +232 -0
  90. package/dist/src/mind/mechanisms/extraction.d.ts +33 -0
  91. package/dist/src/mind/mechanisms/extraction.js +327 -0
  92. package/dist/src/mind/mechanisms/prefix-completion.d.ts +22 -0
  93. package/dist/src/mind/mechanisms/prefix-completion.js +243 -0
  94. package/dist/src/mind/mechanisms/recall.d.ts +16 -0
  95. package/dist/src/mind/mechanisms/recall.js +472 -0
  96. package/dist/src/mind/mechanisms/reference.d.ts +6 -0
  97. package/dist/src/mind/mechanisms/reference.js +296 -0
  98. package/dist/src/mind/mind.d.ts +386 -0
  99. package/dist/src/mind/mind.js +754 -0
  100. package/dist/src/mind/pipeline-mechanism.d.ts +238 -0
  101. package/dist/src/mind/pipeline-mechanism.js +622 -0
  102. package/dist/src/mind/pipeline.d.ts +49 -0
  103. package/dist/src/mind/pipeline.js +343 -0
  104. package/dist/src/mind/primitives.d.ts +85 -0
  105. package/dist/src/mind/primitives.js +333 -0
  106. package/dist/src/mind/rationale.d.ts +139 -0
  107. package/dist/src/mind/rationale.js +163 -0
  108. package/dist/src/mind/reasoning.d.ts +42 -0
  109. package/dist/src/mind/reasoning.js +305 -0
  110. package/dist/src/mind/recognition.d.ts +20 -0
  111. package/dist/src/mind/recognition.js +696 -0
  112. package/dist/src/mind/resonance.d.ts +35 -0
  113. package/dist/src/mind/resonance.js +365 -0
  114. package/dist/src/mind/trace.d.ts +15 -0
  115. package/dist/src/mind/trace.js +73 -0
  116. package/dist/src/mind/traverse.d.ts +196 -0
  117. package/dist/src/mind/traverse.js +806 -0
  118. package/dist/src/mind/types.d.ts +418 -0
  119. package/dist/src/mind/types.js +179 -0
  120. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  121. package/dist/src/rabitq-ivf/src/database.js +201 -0
  122. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  123. package/dist/src/rabitq-ivf/src/index.js +4 -0
  124. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  125. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  126. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  127. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  128. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  129. package/dist/src/rabitq-ivf/src/rabitq.js +313 -0
  130. package/dist/src/sema.d.ts +41 -0
  131. package/dist/src/sema.js +77 -0
  132. package/dist/src/store-sqlite.d.ts +184 -0
  133. package/dist/src/store-sqlite.js +942 -0
  134. package/dist/src/store.d.ts +762 -0
  135. package/dist/src/store.js +1823 -0
  136. package/dist/src/vec.d.ts +31 -0
  137. package/dist/src/vec.js +109 -0
  138. package/index.html +796 -688
  139. package/jsr.json +1 -1
  140. package/package.json +1 -1
@@ -0,0 +1,472 @@
1
+ // mechanisms/recall.ts — Recall by resonance (Grounding VI).
2
+ //
3
+ // The recall mechanism resonates the whole query's gist against the content
4
+ // index and grounds the nearest learned form. Four tiers, orderly degrading
5
+ // from exact self-match to honest echo.
6
+ import { cosine } from "../../vec.js";
7
+ import { consensusFloor, dominates, identityBar, reachThreshold, significanceBar, } from "../../geometry.js";
8
+ import { gistOf, read, resolve } from "../primitives.js";
9
+ import { bytesEqual, indexOf } from "../../bytes.js";
10
+ import { allWindowsAreScaffolding, corpusN, hubBound } from "../traverse.js";
11
+ import { follow, project, reverseContext, voicesDisplacedFiller, } from "../match.js";
12
+ import { CONCEPT, STEP } from "../graph-search.js";
13
+ import { unexplainedLabel } from "../rationale.js";
14
+ import { rItem, rNode } from "../trace.js";
15
+ import { substitutionBridge } from "../bridge.js";
16
+ /** Recall the answer by resonating the whole query against the content index. */
17
+ export async function recallByResonance(ctx, query, pre) {
18
+ const t = ctx.trace?.enter("recallByResonance", [
19
+ rItem(query, "query"),
20
+ ]);
21
+ const whole_ = [[0, query.length]];
22
+ const nothing = [];
23
+ const ground = (bytes, note, accounted, moves, echoed = false, complete = false) => {
24
+ t?.done(bytes === null
25
+ ? []
26
+ : [rItem(bytes, "answer", resolve(ctx, bytes) ?? undefined)], note);
27
+ return bytes === null ? null : {
28
+ bytes,
29
+ echoed,
30
+ accounted,
31
+ moves,
32
+ unexplained: unexplainedLabel(query, accounted),
33
+ ...(complete ? { complete } : {}),
34
+ };
35
+ };
36
+ const k = pre.k;
37
+ const queryGist = pre.guide;
38
+ // 0. Exact self-match — content-addressed, deterministic.
39
+ const qId = pre.queryResolved;
40
+ if (qId !== null) {
41
+ const rev = ctx.store.prevFirst(qId, hubBound(ctx));
42
+ const g = reverseContext(ctx, qId, queryGist, rev);
43
+ if (g !== null) {
44
+ return ground(g, rev.length === 1
45
+ ? "exact self-match — reverse recall to the sole predecessor"
46
+ : "exact self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
47
+ }
48
+ }
49
+ // 0b. ARGUMENT BINDING (RC8): the query is not itself a stored form, but
50
+ // it CONTAINS a recognised constituent that is an edge SOURCE — a learnt
51
+ // pair's left side carried inside a wrapper ("How do you say 'thank you'
52
+ // in French?"). The wrapper is scaffolding; the argument is the span
53
+ // that LEADS somewhere, so its continuation — guided by the whole query's
54
+ // gist — is the answer. Matching the wrapper while ignoring the argument
55
+ // (the observed "good morning" template failure) is worse than silence,
56
+ // so anything short of ONE unambiguous binding falls through: the
57
+ // constituent bar is the same two-quanta (2W) reading confluence binds
58
+ // under, nested recognitions collapse to their MAXIMAL span, and two
59
+ // distinct maximal arguments mean the query asks about neither alone.
60
+ if (qId === null) {
61
+ const W2 = 2 * ctx.space.maxGroup;
62
+ const args = pre.rec.sites.filter((s) => s.end - s.start >= W2 &&
63
+ s.end - s.start < query.length &&
64
+ ctx.store.hasNext(s.payload));
65
+ // Maximal spans by one sorted sweep (starts ascending, ties longest
66
+ // first): every earlier span starts at or before s, so s is contained
67
+ // exactly when the running max end already covers it. O(m log m) — a
68
+ // long input recognises O(|input|) sites, and a pairwise scan here was
69
+ // quadratic in the input.
70
+ args.sort((a, b) => a.start - b.start || b.end - a.end);
71
+ const maximal = [];
72
+ let maxEnd = -1;
73
+ for (const s of args) {
74
+ if (s.end <= maxEnd)
75
+ continue;
76
+ maximal.push(s);
77
+ maxEnd = s.end;
78
+ }
79
+ // The wrapper must actually BE scaffolding: RC8's own premise is "the
80
+ // wrapper is scaffolding; the argument is the span that leads
81
+ // somewhere" ("How do you say 'thank you' in French?" — everything
82
+ // outside the argument is a small fixed template). When the query
83
+ // instead has ANOTHER substantial recognised form (≥ W2, the same
84
+ // constituent bar the argument itself must clear) sitting OUTSIDE the
85
+ // chosen argument, the query is not one argument in a wrapper — it is
86
+ // several complete, independently-meaningful pieces (a multi-turn
87
+ // conversation's own accumulated turns are exactly this shape), and
88
+ // binding to the argument's continuation would answer past content
89
+ // the query itself already carries forward. Derived from the same W2
90
+ // bar the argument itself is held to, never a separate tuned number.
91
+ const hasSubstantialOutside = maximal.length === 1 &&
92
+ pre.rec.sites.some((s) => s.end - s.start >= W2 &&
93
+ (s.end <= maximal[0].start || s.start >= maximal[0].end));
94
+ if (maximal.length === 1 && !hasSubstantialOutside) {
95
+ const arg = maximal[0];
96
+ const g = await follow(ctx, arg.payload, queryGist);
97
+ // The same "no restated fragment" guard tier 2 applies below (§ "the
98
+ // anchor cleared the consensus floor..."): a followed continuation
99
+ // that is itself a proper byte-subspan of the QUERY restates part of
100
+ // the question — never an answer. A multi-turn query's own later
101
+ // turns are exact, content-addressed matches for exactly this reason
102
+ // (each turn is its own previously-learnt form), so without this
103
+ // guard the argument's OWN later restatement in the same
104
+ // conversation reads as if it were the next thing to say.
105
+ if (g !== null && g.length > 0 &&
106
+ !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
107
+ return ground(g, "argument binding — the query's sole edge-source constituent, continuation followed", [[arg.start, arg.end]], STEP);
108
+ }
109
+ }
110
+ }
111
+ // The response's ONE top-k read (Precomputed.resonance) — the same list the
112
+ // frame inventory is assembled from, so a query that reaches both pays for a
113
+ // single ANN query rather than two identical ones.
114
+ const whole = await pre.resonance();
115
+ if (whole.length === 0) {
116
+ return ground(null, "empty store — nothing to resonate with", [], 0);
117
+ }
118
+ const top = whole[0];
119
+ ctx.trace?.step("resonate", [rItem(query, "query-gist")], whole.map((h) => rNode(ctx, h.id, "hit", h.score)), `resonate the whole-query gist → ${whole.length} nearest learnt form(s)`);
120
+ // 1. Clean resonance — the scale-aware identity claim. The ANGLE
121
+ // (top.score) carries the shared fraction; the query's MAGNITUDE (√len,
122
+ // the linear fold's own norm) converts the tolerated foreign fraction
123
+ // into bytes — at most one river window (see {@link identityBar}). A
124
+ // fixed cosine bar let long queries claim "near-identical" while whole
125
+ // windows — an answer word — differed.
126
+ // A hit RESTATES the query when its bytes are the query's own — exactly,
127
+ // or under the response's equivalence (a case/width twin). Restating
128
+ // hits may only conclude through disciplined reverse recall: voicing
129
+ // their bytes echoes the query back at itself (never an answer — the
130
+ // same principle that keeps cast from voicing stored questions), and
131
+ // projecting them forward is reverse recall's containment failure in the
132
+ // other direction — "whatever followed these bytes in some document".
133
+ const qKey = ctx.canon ? ctx.canon(query) : query;
134
+ const restates = (b) => bytesEqual(b, query) ||
135
+ (ctx.canon !== null && bytesEqual(ctx.canon(b), qKey));
136
+ const idBar = identityBar(ctx.store.D, ctx.space.maxGroup, query.length);
137
+ if (top.score >= idBar) {
138
+ for (const h of whole) {
139
+ // The identity claim is PER HIT, not per tier: hits are ranked
140
+ // nearest-first, and grounding one below the bar under this tier's
141
+ // "near-identical" label would launder byte-overlap noise (observed:
142
+ // "merci" projecting through the unrelated near hit "meraih").
143
+ if (h.score < idBar)
144
+ break;
145
+ const own = read(ctx, h.id);
146
+ if (h.id === qId || restates(own)) {
147
+ const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
148
+ const g = reverseContext(ctx, h.id, queryGist, rev);
149
+ if (g !== null) {
150
+ return ground(g, rev.length === 1
151
+ ? "perfect self-match — reverse recall to the sole predecessor"
152
+ : "perfect self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
153
+ }
154
+ continue;
155
+ }
156
+ const g = await project(ctx, h.id, queryGist);
157
+ if (g) {
158
+ return ground(g, "clean whole-query resonance — ground the nearest hit", whole_, STEP);
159
+ }
160
+ }
161
+ }
162
+ // The query-relative grounding fraction, shared by tiers 2–4 — gated on
163
+ // the FRACTION OF THE QUERY the grounding explains, not the raw cosine.
164
+ // Root gists are unit vectors, but their magnitudes are recoverable from
165
+ // the byte lengths (‖·‖ = √len under the linear fold):
166
+ // cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
167
+ // The raw cosine punished honest containment — a query fully inside a
168
+ // longer grounded answer scored √(lenQ/lenG) and was refused — and let a
169
+ // long answer sharing only scaffolding pass; the query-relative fraction
170
+ // measures exactly what the reach bar means: how much of THE QUERY the
171
+ // store accounts for.
172
+ // Chance similarity survives the length conversion AMPLIFIED: the same
173
+ // √(lenG/lenQ) factor that converts an honest shared fraction into a
174
+ // query-relative one multiplies the estimator/chance floor too, so a long
175
+ // stored form (√(lenG/lenQ) ≈ 10 at 100×) lifted a noise-level cosine past
176
+ // the reach bar and grounded pure gibberish (observed). Only the
177
+ // ABOVE-CHANCE part of the similarity is evidence of shared content —
178
+ // subtract the significance bar (3/√D, §8.3) before converting. Derived
179
+ // from the existing bars; never tuned.
180
+ const sig = significanceBar(ctx.store.D);
181
+ const reach = reachThreshold(ctx.space.maxGroup);
182
+ const fracOfQuery = (cos, otherLen) => Math.min(1, Math.max(0, cos - sig) *
183
+ Math.sqrt(otherLen / Math.max(1, query.length)));
184
+ // 2. Scaffolding-dominated.
185
+ if (top.score >= sig) {
186
+ // The committed points of attention ARE the shared climb's roots (same
187
+ // query, same k, same DF mode) — read them from Precomputed instead of
188
+ // re-climbing, so even a traced response pays for the climb once.
189
+ const forest = (await pre.attention()).roots;
190
+ // TRUST THE ANCHOR ON ITS BREADTH, NOT ON ITS ABSOLUTE VOTE.
191
+ //
192
+ // This gate read `forest[0].vote >= consensusFloor(N)`. Attention.breadth's
193
+ // own contract (types.ts) says why that is the wrong quantity: the IDF vote
194
+ // is "an absolute, ln(N)-scaled quantity that means 'strong' on a small
195
+ // store and 'weak' on a large one for the SAME degree of genuine
196
+ // consensus", while breadth is the SCALE-INVARIANT reading — "a point whose
197
+ // breadth clears `dominates` (> half the query's regions corroborate it) is
198
+ // real consensus; one that does not is a coincidental single-region echo".
199
+ // Attention.peak's contract makes the same point from the other side:
200
+ // comparing a POOLED SUM against a floor that prices ONE region's evidence
201
+ // is a dimensional error.
202
+ //
203
+ // Measured on the 15.7M-node store (N=325,615, so the old floor was 13.19).
204
+ // The absolute vote cannot separate right from wrong at this scale, and the
205
+ // proof is a probe that must stay SILENT:
206
+ //
207
+ // anchor picked by the climb vote breadth correct?
208
+ // "What is the chemical formula …" 10.60 0.556 RIGHT
209
+ // "Qual é a capital de França?" 8.19 0.667 RIGHT
210
+ // "Who wrote the play Romeo …?" 8.25 0.833 RIGHT
211
+ // "How do you say "good morning" …" 10.77 0.800 RIGHT
212
+ // "What is the commercial capital …" 12.69 0.333 Zamunda — MUST be silent
213
+ // "Menene sunan ginin mafi tsayi …" 12.79 0.214 wrong (Hausa)
214
+ // "Today is the 5th of March …" 10.36 0.000 wrong
215
+ //
216
+ // Zamunda's junk attractor outvotes every correct anchor, so no vote
217
+ // threshold admits the right ones without admitting fabrication — while
218
+ // breadth > ½ admits exactly the four correct anchors and nothing else.
219
+ // The old floor was simply never cleared on a corpus this large: the tier
220
+ // was dead code here, which is why 12 probes fell through to silence.
221
+ //
222
+ // `dominates(breadth, 1)` is the SAME half-dominance predicate used
223
+ // throughout, applied to the fraction — no new constant, and the bar the
224
+ // breadth contract names. COST: none; breadth is already computed and
225
+ // carried on every Attention the climb returns.
226
+ //
227
+ // The two readings are ALTERNATIVES, never a substitution. REPLACING the
228
+ // vote test with the breadth test was tried and broke 7 tests: on a small
229
+ // store ln(N) is low, so the vote bar is the one that legitimately fires
230
+ // there, and — as Attention.clusters' own contract warns — "breadth starves
231
+ // a genuine, evenly-split multi-topic query, since no root in a real N-way
232
+ // split can exceed half the vote" (the two 3.1 two-topic fusion tests are
233
+ // exactly that shape). Each reading is sufficient on its own evidence: a
234
+ // vote that clears the absolute floor is strong enough wherever the corpus
235
+ // is small enough for that to mean something, and a breadth past ½ is real
236
+ // consensus at any scale. ORing them keeps every admission the floor
237
+ // already made and adds only the scale-invariant ones it could never see.
238
+ //
239
+ // BREADTH ALSO NEEDS DISCRIMINATIVENESS. Breadth asks how much of the
240
+ // query corroborates the anchor, never whether the anchor SAYS anything: on
241
+ // a one-context store every region trivially corroborates the only anchor
242
+ // there is, so breadth is 1 while the anchor's IDF is 0 — and test/31 A2
243
+ // ("explain quantum chromodynamics" against a lone cat fact) answered the
244
+ // cat, which is fabrication. A region's IDF contribution for an anchor
245
+ // reached through c of N contexts is ln(N/c), so requiring it to exceed
246
+ // ln 2 is requiring c·2 < N — the SAME half-dominance reading used
247
+ // everywhere, expressed in the IDF's own units rather than as a new bar.
248
+ // `peak` is that per-region contribution, and reading it here is what
249
+ // Attention.peak's contract asks of a consumer gating on this evidence.
250
+ //
251
+ // AND THE QUERY MUST SAY SOMETHING. Both readings above price the
252
+ // ANCHOR's evidence; neither asks whether the QUERY discriminates
253
+ // anything. A query that is entirely corpus-global scaffolding gives the
254
+ // corpus nothing to be held to, and this tier — which exists to serve
255
+ // scaffolding-DOMINATED queries — is exactly where that runs out.
256
+ // Measured on the trained store: "What is the capital " answered "Colombo
257
+ // is the commercial capital of Sri Lanka…" on breadth 0.667 / clusters 1,
258
+ // and every window it spells is a hub ("What":572). See
259
+ // allWindowsAreScaffolding for the full separation, including the probes
260
+ // this tier serves CORRECTLY, which all retain a discriminating window
261
+ // ("what is the capital of france" → "f fr":248).
262
+ //
263
+ // DISPERSION WAS TRIED HERE FIRST AND FALSIFIED — do not retry it: the
264
+ // fabrication and the no-punctuation robustness probe have the IDENTICAL
265
+ // profile (breadth 0.667, clusters 1), so requiring clusters >= 2 silenced
266
+ // "what is the capital of france" too and cost the battery a probe.
267
+ const minVote = consensusFloor(corpusN(ctx));
268
+ if (forest.length > 0 &&
269
+ !allWindowsAreScaffolding(ctx, query) &&
270
+ (forest[0].vote >= minVote ||
271
+ (dominates(forest[0].breadth, 1) && forest[0].peak > Math.LN2))) {
272
+ const g = await project(ctx, forest[0].anchor, queryGist);
273
+ // THE ANCHOR'S OCCUPANT IS NOT THE ASKER'S. This tier grounds an anchor
274
+ // the climb elected, and voices that anchor's continuation. When the
275
+ // query is the same structure with one position filled differently, the
276
+ // continuation speaks the ANCHOR's occupant of that position — a fluent,
277
+ // specific, wrong answer (see voicesDisplacedFiller). Refusing here is
278
+ // silence, not a redirection: putting the asker's referent in its place
279
+ // is a stronger claim that needs the corpus's own carriage evidence, and
280
+ // that is `reference`'s job.
281
+ const anchorBytes = read(ctx, forest[0].anchor, query.length * ctx.space.maxGroup + 1);
282
+ if (g !== null && voicesDisplacedFiller(ctx, query, anchorBytes, g)) {
283
+ ctx.trace?.step("displacedFiller", [rItem(query, "query"), rNode(ctx, forest[0].anchor, "anchor")], [rItem(g, "withheld")], "refused — the anchor's continuation speaks the anchor's own " +
284
+ "occupant of a position this query fills differently");
285
+ } // The anchor cleared the consensus floor, but the floor prices the
286
+ // ANCHOR's evidence, not the projection's: a junk attractor can clear
287
+ // it and project a PIECE OF THE QUERY back at it (the observed
288
+ // "buenos días in English" → "English" fragment). A projection that
289
+ // is a proper byte-subspan of the query restates part of the question
290
+ // — never an answer (the same principle as `restates` above, extended
291
+ // to fragments). Genuine anchor groundings — longer than the query,
292
+ // or disjoint from it — pass untouched.
293
+ else if (g && !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
294
+ return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
295
+ }
296
+ }
297
+ }
298
+ // 3. Last resort — the nearest grounded whole-query hit, same gate.
299
+ for (const h of whole) {
300
+ const g = await project(ctx, h.id, queryGist);
301
+ if (g) {
302
+ if (fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
303
+ reach) {
304
+ return ground(g, "last resort: the nearest grounded whole-query hit", [], STEP);
305
+ }
306
+ }
307
+ }
308
+ // 3b. Corroborated-substitution bridge — refusal-path only (bridge.ts).
309
+ // The WIDE candidate list every past-the-top-k mechanism reads lives on
310
+ // Precomputed (see wideResonance): shared across the whole response, so the
311
+ // exhaustive branch runs at most once whoever first-touches it.
312
+ const wideIds = () => pre.wideResonance();
313
+ // Every gist-based tier has failed; before refusing, align the query
314
+ // byte-for-byte against the trained contexts its own stored windows
315
+ // anchor, accepting mismatches only as corpus-attested, concept-bar
316
+ // substitutions. A bridged context grounds exactly like any hit —
317
+ // projected through its learnt edges — under the same restated-fragment
318
+ // guard tiers 0b/2 apply. Costs nothing on any answering path.
319
+ {
320
+ // The resonance hits already ranked above are handed to the bridge as
321
+ // PROPOSED candidates alongside its own anchor climbs: on a corpus this
322
+ // size a W-byte window is far too common for the clamped climb to
323
+ // single out the right trained context, while the whole-query gist
324
+ // already ranked it nearest (observed live: "what is the capital of
325
+ // france" resonating straight to "What is the capital of France?" yet
326
+ // refusing on the reach bar). Approximate scores propose; the bridge's
327
+ // byte-exact alignment and attestation gates decide.
328
+ //
329
+ // Reuse recall's already-ranked proposals. Never scan every IVF cluster:
330
+ // exact co-occurrence and bounded anchor ascent are the bridge's structural
331
+ // proposal channels, while an exhaustive ANN call made every honest
332
+ // refusal cost hundreds of milliseconds regardless of k.
333
+ const bridged = await substitutionBridge(ctx, query, wideIds);
334
+ if (bridged !== null) {
335
+ const g = await project(ctx, bridged.id, queryGist);
336
+ // A projection contained in a substituted candidate-side span is the
337
+ // substitution RESTATED as if it were knowledge — the exact failure
338
+ // observed live: "Darwin was born in England." bridged to the
339
+ // Einstein fact through " England." → " Germany." and would have
340
+ // voiced "Germany", an answer the substitution itself manufactured.
341
+ // The same principle as the restated-fragment guards above, extended
342
+ // to the bridge's own substitutions.
343
+ const cBytes = ctx.store.bytes(bridged.id);
344
+ const manufactured = g !== null &&
345
+ bridged.subs.some((s) => indexOf(cBytes.subarray(s.cs, s.ce), g, 0) >= 0);
346
+ // THE PREFIX TRAP IS NOT THIS TIER'S TO SPRING. With no substitutions
347
+ // the claim is "a trained context IS this query, up to filler". When
348
+ // the query is a STRICT BYTE PREFIX of that context, the claim is false
349
+ // in the one way that matters: the candidate's extra tail is precisely
350
+ // the DISCRIMINATING part, and dismissing it as filler asserts a
351
+ // specification the asker never made. Measured on a 4,300-fact fixture
352
+ // of "what is the value of <i>?": the query "what is the value of"
353
+ // bridged with subs [] to "what is the value of 0?" and answered "the
354
+ // value of 0 is 0" — one arbitrary pick from 4,300 equally-matching
355
+ // contexts, every one of which fits the query exactly as well.
356
+ //
357
+ // The engine ALREADY has the right machinery for this shape:
358
+ // prefixCompletion runs a few lines below and carries the three guards
359
+ // this tier lacks — unreadable-continuation veto, sub-quantum
360
+ // continuation, and UNIQUENESS (distinct continuations ⇒ refuse), which
361
+ // is exactly what 4,300 competing values must trip. So this is not a
362
+ // new rule and not a new threshold: it is deferring a prefix decision to
363
+ // the tier that owns it (§2.5, one factored machinery). Byte-strict on
364
+ // purpose — a candidate differing by case or punctuation ("what is the
365
+ // capital of france" → "What is the capital of France?") is NOT a byte
366
+ // prefix, keeps grounding here, and is unaffected.
367
+ const strictPrefix = g !== null &&
368
+ cBytes.length > query.length &&
369
+ indexOf(cBytes, query, 0) === 0;
370
+ if (g !== null && g.length > 0 && !restates(g) && !manufactured &&
371
+ !(bridged.subs.length === 0 && strictPrefix) &&
372
+ !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
373
+ return ground(g, bridged.subs.length === 0
374
+ ? `identity bridge — a trained context IS this query, up to ` +
375
+ `scaffolding the corpus itself treats as filler`
376
+ : `substitution bridge — a trained context accounts for the ` +
377
+ `query up to ${bridged.subs.length} corroborated ` +
378
+ `substitution(s)`,
379
+ // WHAT THIS GROUNDING EXPLAINS — the spans its alignment covers,
380
+ // matched AND substituted, for BOTH tiers.
381
+ //
382
+ // A corroborated substitution is not a gap in the explanation; it is
383
+ // an explanation the mechanism PAID for, one CONCEPT each in `moves`
384
+ // just below. Leaving its span unaccounted charges the same act
385
+ // twice — once as a move, once as PASS-per-unexplained-byte — and the
386
+ // second charge is far the larger, so a bridge that matched 28 of 29
387
+ // bytes declared the whole query unexplained and lost to any
388
+ // mechanism with a smaller honest claim. Measured on test/49's
389
+ // paraphrase: it FOUND the trained fact through two corroborated case
390
+ // substitutions and was outbid 29011 to 1012 by a CAST comparison
391
+ // that voiced the wrong country.
392
+ //
393
+ // This tier DID once report `[]` for the substituted case, against
394
+ // the observation that "pricing the aligned spans outweighed
395
+ // extraction's correct answer in the grounding decider". That is no
396
+ // longer so and the suite is the witness: with the fold's regions
397
+ // content-defined and the junction tiers no longer consuming each
398
+ // other's candidates, full accounting here passes every test that
399
+ // refutation was recorded for. Reporting only the LITERALLY matched
400
+ // spans (accounted minus the substituted ones) was also implemented
401
+ // and is a strictly worse reading of the same ladder — it still
402
+ // double-charges, just less.
403
+ //
404
+ // An IDENTITY bridge (zero substitutions) substituted nothing, so
405
+ // there is nothing to be humble about: every accounted byte is a
406
+ // LITERAL match against a trained form, and the query is that form
407
+ // up to scaffolding. Reporting `[]` for it was actively wrong in
408
+ // two ways — it priced a full explanation at PASS-per-byte so junk
409
+ // outweighed it, and, because the honest-remainder test in think()
410
+ // reads the same spans, it left the whole query "unaccounted" and
411
+ // forced the multi-topic fusion gate open. Observed live: the
412
+ // correct "What is the process of photosynthesis?" grounding was
413
+ // fused away into an unrelated "Hello! How can I assist you
414
+ // today?" point of attention.
415
+ [...bridged.accounted], CONCEPT * bridged.subs.length + STEP, false,
416
+ // COMPLETE only for the identity tier: the query IS this trained
417
+ // context, so `g` is that context's own continuation — the whole
418
+ // read-out. A SUBSTITUTED bridge makes no such claim (it stood a
419
+ // different word in the query's place), so it stays extendable.
420
+ bridged.subs.length === 0);
421
+ }
422
+ }
423
+ }
424
+ // The refusal/echo decision. The echo returns a stored form's bytes AS
425
+ // the answer — a near-identity claim about the query — and identity-grade
426
+ // decisions are never made on an estimated score ("approximate scores may
427
+ // rank and propose; they may never decide", §6.2): the RaBitQ estimate
428
+ // overshooting the reach bar echoed a WRONG-entity neighbour ("capital of
429
+ // Zamunda?" echoed the Armenia fact, observed). The bytes are read
430
+ // anyway to be echoed, so the decision uses their EXACT fold: one river
431
+ // fold of the top hit, measured in the same query-relative,
432
+ // chance-corrected units as the tier above.
433
+ const topBytes = read(ctx, top.id);
434
+ const exact = topBytes.length > 0
435
+ ? cosine(queryGist, gistOf(ctx, topBytes))
436
+ : 0;
437
+ if (fracOfQuery(exact, topBytes.length) < reach) {
438
+ return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
439
+ }
440
+ // Echoing the query's own bytes back at it is not an echo of a RELATED
441
+ // form — it is the query restated, which answers nothing.
442
+ if (restates(topBytes)) {
443
+ return ground(null, "the nearest form IS the query itself — restating it answers nothing", [], 0);
444
+ }
445
+ // Honest echo.
446
+ return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
447
+ }
448
+ // ── Pipeline mechanism ──────────────────────────────────────────────────────
449
+ export const recallMechanism = {
450
+ name: "recall",
451
+ provenance: "recall",
452
+ // Recall's floor is free to state (one STEP-grade projection) and its run
453
+ // gates its own tiers — no expensive investment happens inside floor, so
454
+ // there is nothing to guard with worthRunning here: the pipeline's own
455
+ // check prunes run() against the incumbent.
456
+ async floor(_ctx, _query, _pre, _worthRunning) {
457
+ return STEP;
458
+ },
459
+ async run(ctx, query, pre) {
460
+ const r = await recallByResonance(ctx, query, pre);
461
+ if (!r)
462
+ return [];
463
+ return [{
464
+ bytes: r.bytes,
465
+ accounted: r.accounted,
466
+ moves: r.moves,
467
+ unexplained: r.unexplained,
468
+ provenance: r.echoed ? "recall-echo" : "recall",
469
+ ...(r.complete ? { complete: true } : {}),
470
+ }];
471
+ },
472
+ };
@@ -0,0 +1,6 @@
1
+ import type { MindContext } from "../types.js";
2
+ import type { MechanismResult, PipelineMechanism, Precomputed } from "../pipeline-mechanism.js";
3
+ /** Voice the query's referents through their frame's own attested carriage, or
4
+ * null when the corpus does not attest one. */
5
+ export declare function bindReference(ctx: MindContext, query: Uint8Array, pre: Precomputed): Promise<MechanismResult | null>;
6
+ export declare const referenceMechanism: PipelineMechanism;