@hviana/sema 0.4.1 → 0.4.2

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 (152) hide show
  1. package/package.json +1 -1
  2. package/src/ingest-cache.ts +4 -0
  3. package/src/meter.ts +5 -0
  4. package/src/mind/attention.ts +18 -1
  5. package/src/mind/bridge.ts +292 -54
  6. package/src/mind/junction.ts +21 -7
  7. package/src/mind/match.ts +92 -1
  8. package/src/mind/mechanisms/cast.ts +12 -0
  9. package/src/mind/mechanisms/confluence.ts +30 -1
  10. package/src/mind/mechanisms/cover.ts +36 -4
  11. package/src/mind/mechanisms/recall.ts +21 -44
  12. package/src/mind/mind.ts +39 -2
  13. package/src/mind/pipeline-mechanism.ts +86 -4
  14. package/src/mind/pipeline.ts +1 -1
  15. package/src/mind/reasoning.ts +15 -8
  16. package/src/mind/recognition.ts +40 -0
  17. package/src/mind/resonance.ts +0 -0
  18. package/src/mind/traverse.ts +17 -15
  19. package/src/mind/types.ts +10 -0
  20. package/test/49-natural-units-synonym-bridge.test.mjs +56 -15
  21. package/dist/example/demo.d.ts +0 -1
  22. package/dist/example/demo.js +0 -39
  23. package/dist/example/train_base.d.ts +0 -87
  24. package/dist/example/train_base.js +0 -2252
  25. package/dist/src/alphabet.d.ts +0 -7
  26. package/dist/src/alphabet.js +0 -33
  27. package/dist/src/alu/src/alu.d.ts +0 -185
  28. package/dist/src/alu/src/alu.js +0 -440
  29. package/dist/src/alu/src/expr.d.ts +0 -61
  30. package/dist/src/alu/src/expr.js +0 -318
  31. package/dist/src/alu/src/index.d.ts +0 -11
  32. package/dist/src/alu/src/index.js +0 -19
  33. package/dist/src/alu/src/kernel-arith.d.ts +0 -16
  34. package/dist/src/alu/src/kernel-arith.js +0 -264
  35. package/dist/src/alu/src/kernel-bits.d.ts +0 -19
  36. package/dist/src/alu/src/kernel-bits.js +0 -152
  37. package/dist/src/alu/src/kernel-logic.d.ts +0 -4
  38. package/dist/src/alu/src/kernel-logic.js +0 -60
  39. package/dist/src/alu/src/kernel-nd.d.ts +0 -3
  40. package/dist/src/alu/src/kernel-nd.js +0 -208
  41. package/dist/src/alu/src/kernel-numeric.d.ts +0 -54
  42. package/dist/src/alu/src/kernel-numeric.js +0 -366
  43. package/dist/src/alu/src/operation.d.ts +0 -168
  44. package/dist/src/alu/src/operation.js +0 -189
  45. package/dist/src/alu/src/parser.d.ts +0 -221
  46. package/dist/src/alu/src/parser.js +0 -577
  47. package/dist/src/alu/src/resonance.d.ts +0 -55
  48. package/dist/src/alu/src/resonance.js +0 -126
  49. package/dist/src/alu/src/text.d.ts +0 -31
  50. package/dist/src/alu/src/text.js +0 -73
  51. package/dist/src/alu/src/value.d.ts +0 -109
  52. package/dist/src/alu/src/value.js +0 -300
  53. package/dist/src/alu/test/alu.test.d.ts +0 -1
  54. package/dist/src/alu/test/alu.test.js +0 -764
  55. package/dist/src/bytes.d.ts +0 -14
  56. package/dist/src/bytes.js +0 -59
  57. package/dist/src/canon.d.ts +0 -26
  58. package/dist/src/canon.js +0 -57
  59. package/dist/src/config.d.ts +0 -111
  60. package/dist/src/config.js +0 -91
  61. package/dist/src/derive/src/deduction.d.ts +0 -136
  62. package/dist/src/derive/src/deduction.js +0 -159
  63. package/dist/src/derive/src/index.d.ts +0 -8
  64. package/dist/src/derive/src/index.js +0 -11
  65. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  66. package/dist/src/derive/src/priority-queue.js +0 -73
  67. package/dist/src/derive/src/rewrite.d.ts +0 -56
  68. package/dist/src/derive/src/rewrite.js +0 -100
  69. package/dist/src/derive/src/trie.d.ts +0 -90
  70. package/dist/src/derive/src/trie.js +0 -217
  71. package/dist/src/derive/test/derive.test.d.ts +0 -1
  72. package/dist/src/derive/test/derive.test.js +0 -122
  73. package/dist/src/extension.d.ts +0 -37
  74. package/dist/src/extension.js +0 -7
  75. package/dist/src/geometry.d.ts +0 -175
  76. package/dist/src/geometry.js +0 -823
  77. package/dist/src/index.d.ts +0 -17
  78. package/dist/src/index.js +0 -19
  79. package/dist/src/ingest-cache.d.ts +0 -41
  80. package/dist/src/ingest-cache.js +0 -161
  81. package/dist/src/meter.d.ts +0 -171
  82. package/dist/src/meter.js +0 -269
  83. package/dist/src/mind/articulation.d.ts +0 -6
  84. package/dist/src/mind/articulation.js +0 -99
  85. package/dist/src/mind/attention.d.ts +0 -414
  86. package/dist/src/mind/attention.js +0 -2065
  87. package/dist/src/mind/bridge.d.ts +0 -39
  88. package/dist/src/mind/bridge.js +0 -738
  89. package/dist/src/mind/canonical.d.ts +0 -34
  90. package/dist/src/mind/canonical.js +0 -93
  91. package/dist/src/mind/graph-search.d.ts +0 -294
  92. package/dist/src/mind/graph-search.js +0 -996
  93. package/dist/src/mind/index.d.ts +0 -9
  94. package/dist/src/mind/index.js +0 -5
  95. package/dist/src/mind/junction.d.ts +0 -134
  96. package/dist/src/mind/junction.js +0 -331
  97. package/dist/src/mind/learning.d.ts +0 -75
  98. package/dist/src/mind/learning.js +0 -270
  99. package/dist/src/mind/match.d.ts +0 -166
  100. package/dist/src/mind/match.js +0 -565
  101. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  102. package/dist/src/mind/mechanisms/alu.js +0 -36
  103. package/dist/src/mind/mechanisms/cast.d.ts +0 -89
  104. package/dist/src/mind/mechanisms/cast.js +0 -773
  105. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  106. package/dist/src/mind/mechanisms/confluence.js +0 -225
  107. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  108. package/dist/src/mind/mechanisms/cover.js +0 -203
  109. package/dist/src/mind/mechanisms/extraction.d.ts +0 -33
  110. package/dist/src/mind/mechanisms/extraction.js +0 -300
  111. package/dist/src/mind/mechanisms/recall.d.ts +0 -16
  112. package/dist/src/mind/mechanisms/recall.js +0 -388
  113. package/dist/src/mind/mind.d.ts +0 -330
  114. package/dist/src/mind/mind.js +0 -594
  115. package/dist/src/mind/pipeline-mechanism.d.ts +0 -172
  116. package/dist/src/mind/pipeline-mechanism.js +0 -382
  117. package/dist/src/mind/pipeline.d.ts +0 -49
  118. package/dist/src/mind/pipeline.js +0 -275
  119. package/dist/src/mind/primitives.d.ts +0 -66
  120. package/dist/src/mind/primitives.js +0 -306
  121. package/dist/src/mind/rationale.d.ts +0 -139
  122. package/dist/src/mind/rationale.js +0 -163
  123. package/dist/src/mind/reasoning.d.ts +0 -40
  124. package/dist/src/mind/reasoning.js +0 -272
  125. package/dist/src/mind/recognition.d.ts +0 -20
  126. package/dist/src/mind/recognition.js +0 -463
  127. package/dist/src/mind/resonance.d.ts +0 -23
  128. package/dist/src/mind/resonance.js +0 -0
  129. package/dist/src/mind/trace.d.ts +0 -15
  130. package/dist/src/mind/trace.js +0 -73
  131. package/dist/src/mind/traverse.d.ts +0 -124
  132. package/dist/src/mind/traverse.js +0 -649
  133. package/dist/src/mind/types.d.ts +0 -323
  134. package/dist/src/mind/types.js +0 -130
  135. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  136. package/dist/src/rabitq-ivf/src/database.js +0 -201
  137. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  138. package/dist/src/rabitq-ivf/src/index.js +0 -4
  139. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  140. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  141. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  142. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  143. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  144. package/dist/src/rabitq-ivf/src/rabitq.js +0 -283
  145. package/dist/src/sema.d.ts +0 -31
  146. package/dist/src/sema.js +0 -63
  147. package/dist/src/store-sqlite.d.ts +0 -184
  148. package/dist/src/store-sqlite.js +0 -942
  149. package/dist/src/store.d.ts +0 -678
  150. package/dist/src/store.js +0 -1703
  151. package/dist/src/vec.d.ts +0 -31
  152. package/dist/src/vec.js +0 -109
@@ -1,388 +0,0 @@
1
- // mechanisms/recall.ts — Recall by resonance (Grounding IV).
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, identityBar, reachThreshold, significanceBar, } from "../../geometry.js";
8
- import { gistOf, read, resolve } from "../primitives.js";
9
- import { bytesEqual, indexOf } from "../../bytes.js";
10
- import { corpusN, hubBound } from "../traverse.js";
11
- import { follow, project, reverseContext } 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
- const whole = await ctx.store.resonate(queryGist, k);
112
- if (whole.length === 0) {
113
- return ground(null, "empty store — nothing to resonate with", [], 0);
114
- }
115
- const top = whole[0];
116
- 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)`);
117
- // 1. Clean resonance — the scale-aware identity claim. The ANGLE
118
- // (top.score) carries the shared fraction; the query's MAGNITUDE (√len,
119
- // the linear fold's own norm) converts the tolerated foreign fraction
120
- // into bytes — at most one river window (see {@link identityBar}). A
121
- // fixed cosine bar let long queries claim "near-identical" while whole
122
- // windows — an answer word — differed.
123
- // A hit RESTATES the query when its bytes are the query's own — exactly,
124
- // or under the response's equivalence (a case/width twin). Restating
125
- // hits may only conclude through disciplined reverse recall: voicing
126
- // their bytes echoes the query back at itself (never an answer — the
127
- // same principle that keeps cast from voicing stored questions), and
128
- // projecting them forward is reverse recall's containment failure in the
129
- // other direction — "whatever followed these bytes in some document".
130
- const qKey = ctx.canon ? ctx.canon(query) : query;
131
- const restates = (b) => bytesEqual(b, query) ||
132
- (ctx.canon !== null && bytesEqual(ctx.canon(b), qKey));
133
- const idBar = identityBar(ctx.store.D, ctx.space.maxGroup, query.length);
134
- if (top.score >= idBar) {
135
- for (const h of whole) {
136
- // The identity claim is PER HIT, not per tier: hits are ranked
137
- // nearest-first, and grounding one below the bar under this tier's
138
- // "near-identical" label would launder byte-overlap noise (observed:
139
- // "merci" projecting through the unrelated near hit "meraih").
140
- if (h.score < idBar)
141
- break;
142
- const own = read(ctx, h.id);
143
- if (h.id === qId || restates(own)) {
144
- const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
145
- const g = reverseContext(ctx, h.id, queryGist, rev);
146
- if (g !== null) {
147
- return ground(g, rev.length === 1
148
- ? "perfect self-match — reverse recall to the sole predecessor"
149
- : "perfect self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
150
- }
151
- continue;
152
- }
153
- const g = await project(ctx, h.id, queryGist);
154
- if (g) {
155
- return ground(g, "clean whole-query resonance — ground the nearest hit", whole_, STEP);
156
- }
157
- }
158
- }
159
- // The query-relative grounding fraction, shared by tiers 2–4 — gated on
160
- // the FRACTION OF THE QUERY the grounding explains, not the raw cosine.
161
- // Root gists are unit vectors, but their magnitudes are recoverable from
162
- // the byte lengths (‖·‖ = √len under the linear fold):
163
- // cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
164
- // The raw cosine punished honest containment — a query fully inside a
165
- // longer grounded answer scored √(lenQ/lenG) and was refused — and let a
166
- // long answer sharing only scaffolding pass; the query-relative fraction
167
- // measures exactly what the reach bar means: how much of THE QUERY the
168
- // store accounts for.
169
- // Chance similarity survives the length conversion AMPLIFIED: the same
170
- // √(lenG/lenQ) factor that converts an honest shared fraction into a
171
- // query-relative one multiplies the estimator/chance floor too, so a long
172
- // stored form (√(lenG/lenQ) ≈ 10 at 100×) lifted a noise-level cosine past
173
- // the reach bar and grounded pure gibberish (observed). Only the
174
- // ABOVE-CHANCE part of the similarity is evidence of shared content —
175
- // subtract the significance bar (3/√D, §8.3) before converting. Derived
176
- // from the existing bars; never tuned.
177
- const sig = significanceBar(ctx.store.D);
178
- const reach = reachThreshold(ctx.space.maxGroup);
179
- const fracOfQuery = (cos, otherLen) => Math.min(1, Math.max(0, cos - sig) *
180
- Math.sqrt(otherLen / Math.max(1, query.length)));
181
- // 2. Scaffolding-dominated.
182
- if (top.score >= sig) {
183
- const N = corpusN(ctx);
184
- const minVote = consensusFloor(N);
185
- // The committed points of attention ARE the shared climb's roots (same
186
- // query, same k, same DF mode) — read them from Precomputed instead of
187
- // re-climbing, so even a traced response pays for the climb once.
188
- const forest = (await pre.attention()).roots;
189
- if (forest.length > 0 && forest[0].vote >= minVote) {
190
- const g = await project(ctx, forest[0].anchor, queryGist);
191
- // The anchor cleared the consensus floor, but the floor prices the
192
- // ANCHOR's evidence, not the projection's: a junk attractor can clear
193
- // it and project a PIECE OF THE QUERY back at it (the observed
194
- // "buenos días in English" → "English" fragment). A projection that
195
- // is a proper byte-subspan of the query restates part of the question
196
- // — never an answer (the same principle as `restates` above, extended
197
- // to fragments). Genuine anchor groundings — longer than the query,
198
- // or disjoint from it — pass untouched.
199
- if (g && !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
200
- return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
201
- }
202
- }
203
- }
204
- // 3. Last resort — the nearest grounded whole-query hit, same gate.
205
- for (const h of whole) {
206
- const g = await project(ctx, h.id, queryGist);
207
- if (g) {
208
- if (fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
209
- reach) {
210
- return ground(g, "last resort: the nearest grounded whole-query hit", [], STEP);
211
- }
212
- }
213
- }
214
- // 3b. Corroborated-substitution bridge — refusal-path only (bridge.ts).
215
- // Every gist-based tier has failed; before refusing, align the query
216
- // byte-for-byte against the trained contexts its own stored windows
217
- // anchor, accepting mismatches only as corpus-attested, concept-bar
218
- // substitutions. A bridged context grounds exactly like any hit —
219
- // projected through its learnt edges — under the same restated-fragment
220
- // guard tiers 0b/2 apply. Costs nothing on any answering path.
221
- {
222
- // The resonance hits already ranked above are handed to the bridge as
223
- // PROPOSED candidates alongside its own anchor climbs: on a corpus this
224
- // size a W-byte window is far too common for the clamped climb to
225
- // single out the right trained context, while the whole-query gist
226
- // already ranked it nearest (observed live: "what is the capital of
227
- // france" resonating straight to "What is the capital of France?" yet
228
- // refusing on the reach bar). Approximate scores propose; the bridge's
229
- // byte-exact alignment and attestation gates decide.
230
- //
231
- // The proposal breadth here is widened PAST `k` — first by requesting
232
- // hubBound(ctx) candidates instead of `k` (recall's own tiers above
233
- // stay at `k`; this re-resonates only on the refusal path, exactly
234
- // where the bridge itself already runs), AND by asking the index to
235
- // search EXHAUSTIVELY. Both matter: the IVF only ever probes
236
- // ⌈√clusters⌉ of them (store.ts's efFor) REGARDLESS of k — widening k
237
- // alone just returns more hits from the SAME already-probed clusters,
238
- // never a hit whose vector lives in an unprobed one. Measured live:
239
- // "What is the chemical symbol for water?" needs "What is the
240
- // chemical formula for water?", scoring only 0.58 against the
241
- // query's gist (a MIDDLE-of-string word swap perturbs the river-fold
242
- // tree hash far more than a same-length TAIL swap like the "carbon"/
243
- // "oxygen" neighbours that outrank it at 0.87+) — absent from the
244
- // resonance list even at k=5000, present and byte-exact-verified the
245
- // moment it's force-fed to the bridge directly. `exhaustive` is the
246
- // natural, tuning-free ceiling (probe every cluster) for a call that
247
- // is ALREADY refusal-path-only and must not miss a candidate hiding
248
- // behind an unlucky structural distance.
249
- //
250
- // MEASURED COST, AND WHY IT STAYS (17.9M vectors / 325K contexts):
251
- // this one call is ~570 ms and ~45% of all inference time on a refusing
252
- // query. The cost is entirely `exhaustive` (nprobe = every cluster),
253
- // NOT the widened k — timed on that store: k=571 exhaustive 632 ms,
254
- // k=24 exhaustive 536 ms, k=571 NON-exhaustive 12 ms. So narrowing k
255
- // buys nothing and the 50x is the whole-index scan itself.
256
- // It is load-bearing: over an 18-query battery the bridge produced a
257
- // winner 4 times, and ALL FOUR winners came from this proposal channel
258
- // — the anchor-climb channel won nothing on its own. Reordering the
259
- // channels (climb first, resonate only on failure) would therefore pay
260
- // the climb, fail, and pay this anyway. Do not weaken it without
261
- // re-running that measurement.
262
- // Handed to the bridge as a THUNK: this exhaustive whole-index probe is
263
- // the most expensive single act on the refusal path, and the bridge's
264
- // own cheap gates (query length, the O(|query|) stored-window anchor
265
- // scan) can refuse without any proposal at all. See substitutionBridge.
266
- const wideIds = async () => {
267
- const wide = k >= hubBound(ctx)
268
- ? whole
269
- : ctx.meter
270
- ? await ctx.meter.time("recall.exhaustiveResonate", () => ctx.store.resonate(queryGist, hubBound(ctx), true))
271
- : await ctx.store.resonate(queryGist, hubBound(ctx), true);
272
- return wide.map((h) => h.id);
273
- };
274
- const bridged = await substitutionBridge(ctx, query, wideIds);
275
- if (bridged !== null) {
276
- const g = await project(ctx, bridged.id, queryGist);
277
- // A projection contained in a substituted candidate-side span is the
278
- // substitution RESTATED as if it were knowledge — the exact failure
279
- // observed live: "Darwin was born in England." bridged to the
280
- // Einstein fact through " England." → " Germany." and would have
281
- // voiced "Germany", an answer the substitution itself manufactured.
282
- // The same principle as the restated-fragment guards above, extended
283
- // to the bridge's own substitutions.
284
- const cBytes = ctx.store.bytes(bridged.id);
285
- const manufactured = g !== null &&
286
- bridged.subs.some((s) => indexOf(cBytes.subarray(s.cs, s.ce), g, 0) >= 0);
287
- if (g !== null && g.length > 0 && !restates(g) && !manufactured &&
288
- !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
289
- return ground(g, bridged.subs.length === 0
290
- ? `identity bridge — a trained context IS this query, up to ` +
291
- `scaffolding the corpus itself treats as filler`
292
- : `substitution bridge — a trained context accounts for the ` +
293
- `query up to ${bridged.subs.length} corroborated ` +
294
- `substitution(s)`,
295
- // WHAT THIS GROUNDING EXPLAINS — the spans its alignment covers,
296
- // matched AND substituted, for BOTH tiers.
297
- //
298
- // A corroborated substitution is not a gap in the explanation; it is
299
- // an explanation the mechanism PAID for, one CONCEPT each in `moves`
300
- // just below. Leaving its span unaccounted charges the same act
301
- // twice — once as a move, once as PASS-per-unexplained-byte — and the
302
- // second charge is far the larger, so a bridge that matched 28 of 29
303
- // bytes declared the whole query unexplained and lost to any
304
- // mechanism with a smaller honest claim. Measured on test/49's
305
- // paraphrase: it FOUND the trained fact through two corroborated case
306
- // substitutions and was outbid 29011 to 1012 by a CAST comparison
307
- // that voiced the wrong country.
308
- //
309
- // This tier DID once report `[]` for the substituted case, against
310
- // the observation that "pricing the aligned spans outweighed
311
- // extraction's correct answer in the grounding decider". That is no
312
- // longer so and the suite is the witness: with the fold's regions
313
- // content-defined and the junction tiers no longer consuming each
314
- // other's candidates, full accounting here passes every test that
315
- // refutation was recorded for. Reporting only the LITERALLY matched
316
- // spans (accounted minus the substituted ones) was also implemented
317
- // and is a strictly worse reading of the same ladder — it still
318
- // double-charges, just less.
319
- //
320
- // An IDENTITY bridge (zero substitutions) substituted nothing, so
321
- // there is nothing to be humble about: every accounted byte is a
322
- // LITERAL match against a trained form, and the query is that form
323
- // up to scaffolding. Reporting `[]` for it was actively wrong in
324
- // two ways — it priced a full explanation at PASS-per-byte so junk
325
- // outweighed it, and, because the honest-remainder test in think()
326
- // reads the same spans, it left the whole query "unaccounted" and
327
- // forced the multi-topic fusion gate open. Observed live: the
328
- // correct "What is the process of photosynthesis?" grounding was
329
- // fused away into an unrelated "Hello! How can I assist you
330
- // today?" point of attention.
331
- [...bridged.accounted], CONCEPT * bridged.subs.length + STEP, false,
332
- // COMPLETE only for the identity tier: the query IS this trained
333
- // context, so `g` is that context's own continuation — the whole
334
- // read-out. A SUBSTITUTED bridge makes no such claim (it stood a
335
- // different word in the query's place), so it stays extendable.
336
- bridged.subs.length === 0);
337
- }
338
- }
339
- }
340
- // The refusal/echo decision. The echo returns a stored form's bytes AS
341
- // the answer — a near-identity claim about the query — and identity-grade
342
- // decisions are never made on an estimated score ("approximate scores may
343
- // rank and propose; they may never decide", §6.2): the RaBitQ estimate
344
- // overshooting the reach bar echoed a WRONG-entity neighbour ("capital of
345
- // Zamunda?" echoed the Armenia fact, observed). The bytes are read
346
- // anyway to be echoed, so the decision uses their EXACT fold: one river
347
- // fold of the top hit, measured in the same query-relative,
348
- // chance-corrected units as the tier above.
349
- const topBytes = read(ctx, top.id);
350
- const exact = topBytes.length > 0
351
- ? cosine(queryGist, gistOf(ctx, topBytes))
352
- : 0;
353
- if (fracOfQuery(exact, topBytes.length) < reach) {
354
- return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
355
- }
356
- // Echoing the query's own bytes back at it is not an echo of a RELATED
357
- // form — it is the query restated, which answers nothing.
358
- if (restates(topBytes)) {
359
- return ground(null, "the nearest form IS the query itself — restating it answers nothing", [], 0);
360
- }
361
- // Honest echo.
362
- return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
363
- }
364
- // ── Pipeline mechanism ──────────────────────────────────────────────────────
365
- export const recallMechanism = {
366
- name: "recall",
367
- provenance: "recall",
368
- // Recall's floor is free to state (one STEP-grade projection) and its run
369
- // gates its own tiers — no expensive investment happens inside floor, so
370
- // there is nothing to guard with worthRunning here: the pipeline's own
371
- // check prunes run() against the incumbent.
372
- async floor(_ctx, _query, _pre, _worthRunning) {
373
- return STEP;
374
- },
375
- async run(ctx, query, pre) {
376
- const r = await recallByResonance(ctx, query, pre);
377
- if (!r)
378
- return [];
379
- return [{
380
- bytes: r.bytes,
381
- accounted: r.accounted,
382
- moves: r.moves,
383
- unexplained: r.unexplained,
384
- provenance: r.echoed ? "recall-echo" : "recall",
385
- ...(r.complete ? { complete: true } : {}),
386
- }];
387
- },
388
- };