@hviana/sema 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/dist/example/demo.d.ts +1 -0
  2. package/dist/example/demo.js +39 -0
  3. package/dist/example/train_base.d.ts +87 -0
  4. package/dist/example/train_base.js +2252 -0
  5. package/dist/src/alphabet.d.ts +7 -0
  6. package/dist/src/alphabet.js +33 -0
  7. package/dist/src/alu/src/alu.d.ts +185 -0
  8. package/dist/src/alu/src/alu.js +440 -0
  9. package/dist/src/alu/src/expr.d.ts +61 -0
  10. package/dist/src/alu/src/expr.js +318 -0
  11. package/dist/src/alu/src/index.d.ts +11 -0
  12. package/dist/src/alu/src/index.js +19 -0
  13. package/dist/src/alu/src/kernel-arith.d.ts +16 -0
  14. package/dist/src/alu/src/kernel-arith.js +264 -0
  15. package/dist/src/alu/src/kernel-bits.d.ts +19 -0
  16. package/dist/src/alu/src/kernel-bits.js +152 -0
  17. package/dist/src/alu/src/kernel-logic.d.ts +4 -0
  18. package/dist/src/alu/src/kernel-logic.js +60 -0
  19. package/dist/src/alu/src/kernel-nd.d.ts +3 -0
  20. package/dist/src/alu/src/kernel-nd.js +208 -0
  21. package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
  22. package/dist/src/alu/src/kernel-numeric.js +366 -0
  23. package/dist/src/alu/src/operation.d.ts +168 -0
  24. package/dist/src/alu/src/operation.js +189 -0
  25. package/dist/src/alu/src/parser.d.ts +221 -0
  26. package/dist/src/alu/src/parser.js +577 -0
  27. package/dist/src/alu/src/resonance.d.ts +55 -0
  28. package/dist/src/alu/src/resonance.js +126 -0
  29. package/dist/src/alu/src/text.d.ts +31 -0
  30. package/dist/src/alu/src/text.js +73 -0
  31. package/dist/src/alu/src/value.d.ts +109 -0
  32. package/dist/src/alu/src/value.js +300 -0
  33. package/dist/src/alu/test/alu.test.d.ts +1 -0
  34. package/dist/src/alu/test/alu.test.js +764 -0
  35. package/dist/src/bytes.d.ts +14 -0
  36. package/dist/src/bytes.js +59 -0
  37. package/dist/src/canon.d.ts +26 -0
  38. package/dist/src/canon.js +57 -0
  39. package/dist/src/config.d.ts +111 -0
  40. package/dist/src/config.js +91 -0
  41. package/dist/src/derive/src/deduction.d.ts +136 -0
  42. package/dist/src/derive/src/deduction.js +159 -0
  43. package/dist/src/derive/src/index.d.ts +8 -0
  44. package/dist/src/derive/src/index.js +11 -0
  45. package/dist/src/derive/src/priority-queue.d.ts +20 -0
  46. package/dist/src/derive/src/priority-queue.js +73 -0
  47. package/dist/src/derive/src/rewrite.d.ts +56 -0
  48. package/dist/src/derive/src/rewrite.js +100 -0
  49. package/dist/src/derive/src/trie.d.ts +90 -0
  50. package/dist/src/derive/src/trie.js +217 -0
  51. package/dist/src/derive/test/derive.test.d.ts +1 -0
  52. package/dist/src/derive/test/derive.test.js +122 -0
  53. package/dist/src/extension.d.ts +37 -0
  54. package/dist/src/extension.js +7 -0
  55. package/dist/src/geometry.d.ts +175 -0
  56. package/dist/src/geometry.js +823 -0
  57. package/dist/src/index.d.ts +17 -0
  58. package/dist/src/index.js +19 -0
  59. package/dist/src/ingest-cache.d.ts +41 -0
  60. package/dist/src/ingest-cache.js +165 -0
  61. package/dist/src/meter.d.ts +176 -0
  62. package/dist/src/meter.js +274 -0
  63. package/dist/src/mind/articulation.d.ts +6 -0
  64. package/dist/src/mind/articulation.js +99 -0
  65. package/dist/src/mind/attention.d.ts +414 -0
  66. package/dist/src/mind/attention.js +2082 -0
  67. package/dist/src/mind/bridge.d.ts +39 -0
  68. package/dist/src/mind/bridge.js +947 -0
  69. package/dist/src/mind/canonical.d.ts +34 -0
  70. package/dist/src/mind/canonical.js +93 -0
  71. package/dist/src/mind/graph-search.d.ts +294 -0
  72. package/dist/src/mind/graph-search.js +996 -0
  73. package/dist/src/mind/index.d.ts +9 -0
  74. package/dist/src/mind/index.js +5 -0
  75. package/dist/src/mind/junction.d.ts +137 -0
  76. package/dist/src/mind/junction.js +342 -0
  77. package/dist/src/mind/learning.d.ts +75 -0
  78. package/dist/src/mind/learning.js +270 -0
  79. package/dist/src/mind/match.d.ts +181 -0
  80. package/dist/src/mind/match.js +655 -0
  81. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  82. package/dist/src/mind/mechanisms/alu.js +36 -0
  83. package/dist/src/mind/mechanisms/cast.d.ts +89 -0
  84. package/dist/src/mind/mechanisms/cast.js +784 -0
  85. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  86. package/dist/src/mind/mechanisms/confluence.js +255 -0
  87. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  88. package/dist/src/mind/mechanisms/cover.js +227 -0
  89. package/dist/src/mind/mechanisms/extraction.d.ts +33 -0
  90. package/dist/src/mind/mechanisms/extraction.js +300 -0
  91. package/dist/src/mind/mechanisms/recall.d.ts +16 -0
  92. package/dist/src/mind/mechanisms/recall.js +364 -0
  93. package/dist/src/mind/mind.d.ts +337 -0
  94. package/dist/src/mind/mind.js +617 -0
  95. package/dist/src/mind/pipeline-mechanism.d.ts +172 -0
  96. package/dist/src/mind/pipeline-mechanism.js +465 -0
  97. package/dist/src/mind/pipeline.d.ts +49 -0
  98. package/dist/src/mind/pipeline.js +275 -0
  99. package/dist/src/mind/primitives.d.ts +66 -0
  100. package/dist/src/mind/primitives.js +306 -0
  101. package/dist/src/mind/rationale.d.ts +139 -0
  102. package/dist/src/mind/rationale.js +163 -0
  103. package/dist/src/mind/reasoning.d.ts +40 -0
  104. package/dist/src/mind/reasoning.js +280 -0
  105. package/dist/src/mind/recognition.d.ts +20 -0
  106. package/dist/src/mind/recognition.js +504 -0
  107. package/dist/src/mind/resonance.d.ts +23 -0
  108. package/dist/src/mind/resonance.js +0 -0
  109. package/dist/src/mind/trace.d.ts +15 -0
  110. package/dist/src/mind/trace.js +73 -0
  111. package/dist/src/mind/traverse.d.ts +126 -0
  112. package/dist/src/mind/traverse.js +650 -0
  113. package/dist/src/mind/types.d.ts +333 -0
  114. package/dist/src/mind/types.js +130 -0
  115. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  116. package/dist/src/rabitq-ivf/src/database.js +201 -0
  117. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  118. package/dist/src/rabitq-ivf/src/index.js +4 -0
  119. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  120. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  121. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  122. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  123. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  124. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  125. package/dist/src/sema.d.ts +31 -0
  126. package/dist/src/sema.js +63 -0
  127. package/dist/src/store-sqlite.d.ts +184 -0
  128. package/dist/src/store-sqlite.js +942 -0
  129. package/dist/src/store.d.ts +678 -0
  130. package/dist/src/store.js +1703 -0
  131. package/dist/src/vec.d.ts +31 -0
  132. package/dist/src/vec.js +109 -0
  133. package/package.json +1 -1
@@ -0,0 +1,364 @@
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
+ // Reuse recall's already-ranked proposals. Never scan every IVF cluster:
232
+ // exact co-occurrence and bounded anchor ascent are the bridge's structural
233
+ // proposal channels, while an exhaustive ANN call made every honest
234
+ // refusal cost hundreds of milliseconds regardless of k.
235
+ const wideIds = async () => {
236
+ // When the top resonance hit is below the concept threshold, the query
237
+ // gist has no concept-level match to any stored form — an exhaustive √N
238
+ // ANN would only score more vectors below the bar (profiled at 38K–40K
239
+ // annVectorReads per refusing query on a 325K-context store). The
240
+ // bridge's structural channels (junction walks, anchor climbs) are the
241
+ // correct proposal source for a query whose gist has no clean match;
242
+ // the ANN cannot propose what the gist cannot rank.
243
+ const marketScale = k * ctx.space.maxGroup;
244
+ if (corpusN(ctx) <= marketScale ** 3) {
245
+ const exhaustive = await ctx.store.resonate(queryGist, hubBound(ctx), true);
246
+ return exhaustive.map((h) => h.id);
247
+ }
248
+ return whole.map((h) => h.id);
249
+ };
250
+ const bridged = await substitutionBridge(ctx, query, wideIds);
251
+ if (bridged !== null) {
252
+ const g = await project(ctx, bridged.id, queryGist);
253
+ // A projection contained in a substituted candidate-side span is the
254
+ // substitution RESTATED as if it were knowledge — the exact failure
255
+ // observed live: "Darwin was born in England." bridged to the
256
+ // Einstein fact through " England." → " Germany." and would have
257
+ // voiced "Germany", an answer the substitution itself manufactured.
258
+ // The same principle as the restated-fragment guards above, extended
259
+ // to the bridge's own substitutions.
260
+ const cBytes = ctx.store.bytes(bridged.id);
261
+ const manufactured = g !== null &&
262
+ bridged.subs.some((s) => indexOf(cBytes.subarray(s.cs, s.ce), g, 0) >= 0);
263
+ if (g !== null && g.length > 0 && !restates(g) && !manufactured &&
264
+ !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
265
+ return ground(g, bridged.subs.length === 0
266
+ ? `identity bridge — a trained context IS this query, up to ` +
267
+ `scaffolding the corpus itself treats as filler`
268
+ : `substitution bridge — a trained context accounts for the ` +
269
+ `query up to ${bridged.subs.length} corroborated ` +
270
+ `substitution(s)`,
271
+ // WHAT THIS GROUNDING EXPLAINS — the spans its alignment covers,
272
+ // matched AND substituted, for BOTH tiers.
273
+ //
274
+ // A corroborated substitution is not a gap in the explanation; it is
275
+ // an explanation the mechanism PAID for, one CONCEPT each in `moves`
276
+ // just below. Leaving its span unaccounted charges the same act
277
+ // twice — once as a move, once as PASS-per-unexplained-byte — and the
278
+ // second charge is far the larger, so a bridge that matched 28 of 29
279
+ // bytes declared the whole query unexplained and lost to any
280
+ // mechanism with a smaller honest claim. Measured on test/49's
281
+ // paraphrase: it FOUND the trained fact through two corroborated case
282
+ // substitutions and was outbid 29011 to 1012 by a CAST comparison
283
+ // that voiced the wrong country.
284
+ //
285
+ // This tier DID once report `[]` for the substituted case, against
286
+ // the observation that "pricing the aligned spans outweighed
287
+ // extraction's correct answer in the grounding decider". That is no
288
+ // longer so and the suite is the witness: with the fold's regions
289
+ // content-defined and the junction tiers no longer consuming each
290
+ // other's candidates, full accounting here passes every test that
291
+ // refutation was recorded for. Reporting only the LITERALLY matched
292
+ // spans (accounted minus the substituted ones) was also implemented
293
+ // and is a strictly worse reading of the same ladder — it still
294
+ // double-charges, just less.
295
+ //
296
+ // An IDENTITY bridge (zero substitutions) substituted nothing, so
297
+ // there is nothing to be humble about: every accounted byte is a
298
+ // LITERAL match against a trained form, and the query is that form
299
+ // up to scaffolding. Reporting `[]` for it was actively wrong in
300
+ // two ways — it priced a full explanation at PASS-per-byte so junk
301
+ // outweighed it, and, because the honest-remainder test in think()
302
+ // reads the same spans, it left the whole query "unaccounted" and
303
+ // forced the multi-topic fusion gate open. Observed live: the
304
+ // correct "What is the process of photosynthesis?" grounding was
305
+ // fused away into an unrelated "Hello! How can I assist you
306
+ // today?" point of attention.
307
+ [...bridged.accounted], CONCEPT * bridged.subs.length + STEP, false,
308
+ // COMPLETE only for the identity tier: the query IS this trained
309
+ // context, so `g` is that context's own continuation — the whole
310
+ // read-out. A SUBSTITUTED bridge makes no such claim (it stood a
311
+ // different word in the query's place), so it stays extendable.
312
+ bridged.subs.length === 0);
313
+ }
314
+ }
315
+ }
316
+ // The refusal/echo decision. The echo returns a stored form's bytes AS
317
+ // the answer — a near-identity claim about the query — and identity-grade
318
+ // decisions are never made on an estimated score ("approximate scores may
319
+ // rank and propose; they may never decide", §6.2): the RaBitQ estimate
320
+ // overshooting the reach bar echoed a WRONG-entity neighbour ("capital of
321
+ // Zamunda?" echoed the Armenia fact, observed). The bytes are read
322
+ // anyway to be echoed, so the decision uses their EXACT fold: one river
323
+ // fold of the top hit, measured in the same query-relative,
324
+ // chance-corrected units as the tier above.
325
+ const topBytes = read(ctx, top.id);
326
+ const exact = topBytes.length > 0
327
+ ? cosine(queryGist, gistOf(ctx, topBytes))
328
+ : 0;
329
+ if (fracOfQuery(exact, topBytes.length) < reach) {
330
+ return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
331
+ }
332
+ // Echoing the query's own bytes back at it is not an echo of a RELATED
333
+ // form — it is the query restated, which answers nothing.
334
+ if (restates(topBytes)) {
335
+ return ground(null, "the nearest form IS the query itself — restating it answers nothing", [], 0);
336
+ }
337
+ // Honest echo.
338
+ return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
339
+ }
340
+ // ── Pipeline mechanism ──────────────────────────────────────────────────────
341
+ export const recallMechanism = {
342
+ name: "recall",
343
+ provenance: "recall",
344
+ // Recall's floor is free to state (one STEP-grade projection) and its run
345
+ // gates its own tiers — no expensive investment happens inside floor, so
346
+ // there is nothing to guard with worthRunning here: the pipeline's own
347
+ // check prunes run() against the incumbent.
348
+ async floor(_ctx, _query, _pre, _worthRunning) {
349
+ return STEP;
350
+ },
351
+ async run(ctx, query, pre) {
352
+ const r = await recallByResonance(ctx, query, pre);
353
+ if (!r)
354
+ return [];
355
+ return [{
356
+ bytes: r.bytes,
357
+ accounted: r.accounted,
358
+ moves: r.moves,
359
+ unexplained: r.unexplained,
360
+ provenance: r.echoed ? "recall-echo" : "recall",
361
+ ...(r.complete ? { complete: true } : {}),
362
+ }];
363
+ },
364
+ };