@hviana/sema 0.2.4 → 0.2.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 (142) hide show
  1. package/package.json +1 -1
  2. package/src/geometry.ts +52 -3
  3. package/src/mind/attention.ts +1469 -120
  4. package/src/mind/index.ts +16 -0
  5. package/src/mind/junction.ts +125 -16
  6. package/src/mind/mind.ts +15 -0
  7. package/src/mind/rationale.ts +12 -4
  8. package/src/mind/traverse.ts +75 -7
  9. package/src/mind/types.ts +25 -0
  10. package/test/51-structural-resonance-ladder.test.mjs +552 -0
  11. package/test/52-climb-consensus-instrumentation.test.mjs +324 -0
  12. package/test/53-cross-region-probe-instrumentation.test.mjs +356 -0
  13. package/dist/example/demo.d.ts +0 -1
  14. package/dist/example/demo.js +0 -39
  15. package/dist/example/train_base.d.ts +0 -87
  16. package/dist/example/train_base.js +0 -2252
  17. package/dist/src/alphabet.d.ts +0 -7
  18. package/dist/src/alphabet.js +0 -33
  19. package/dist/src/alu/src/alu.d.ts +0 -185
  20. package/dist/src/alu/src/alu.js +0 -440
  21. package/dist/src/alu/src/expr.d.ts +0 -61
  22. package/dist/src/alu/src/expr.js +0 -318
  23. package/dist/src/alu/src/index.d.ts +0 -11
  24. package/dist/src/alu/src/index.js +0 -19
  25. package/dist/src/alu/src/kernel-arith.d.ts +0 -16
  26. package/dist/src/alu/src/kernel-arith.js +0 -264
  27. package/dist/src/alu/src/kernel-bits.d.ts +0 -19
  28. package/dist/src/alu/src/kernel-bits.js +0 -152
  29. package/dist/src/alu/src/kernel-logic.d.ts +0 -4
  30. package/dist/src/alu/src/kernel-logic.js +0 -60
  31. package/dist/src/alu/src/kernel-nd.d.ts +0 -3
  32. package/dist/src/alu/src/kernel-nd.js +0 -208
  33. package/dist/src/alu/src/kernel-numeric.d.ts +0 -54
  34. package/dist/src/alu/src/kernel-numeric.js +0 -366
  35. package/dist/src/alu/src/operation.d.ts +0 -168
  36. package/dist/src/alu/src/operation.js +0 -189
  37. package/dist/src/alu/src/parser.d.ts +0 -221
  38. package/dist/src/alu/src/parser.js +0 -577
  39. package/dist/src/alu/src/resonance.d.ts +0 -55
  40. package/dist/src/alu/src/resonance.js +0 -126
  41. package/dist/src/alu/src/text.d.ts +0 -31
  42. package/dist/src/alu/src/text.js +0 -73
  43. package/dist/src/alu/src/value.d.ts +0 -109
  44. package/dist/src/alu/src/value.js +0 -300
  45. package/dist/src/alu/test/alu.test.d.ts +0 -1
  46. package/dist/src/alu/test/alu.test.js +0 -764
  47. package/dist/src/bytes.d.ts +0 -14
  48. package/dist/src/bytes.js +0 -59
  49. package/dist/src/canon.d.ts +0 -26
  50. package/dist/src/canon.js +0 -57
  51. package/dist/src/config.d.ts +0 -111
  52. package/dist/src/config.js +0 -91
  53. package/dist/src/derive/src/deduction.d.ts +0 -125
  54. package/dist/src/derive/src/deduction.js +0 -155
  55. package/dist/src/derive/src/index.d.ts +0 -7
  56. package/dist/src/derive/src/index.js +0 -11
  57. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  58. package/dist/src/derive/src/priority-queue.js +0 -73
  59. package/dist/src/derive/src/rewrite.d.ts +0 -56
  60. package/dist/src/derive/src/rewrite.js +0 -100
  61. package/dist/src/derive/src/trie.d.ts +0 -90
  62. package/dist/src/derive/src/trie.js +0 -217
  63. package/dist/src/derive/test/derive.test.d.ts +0 -1
  64. package/dist/src/derive/test/derive.test.js +0 -122
  65. package/dist/src/extension.d.ts +0 -37
  66. package/dist/src/extension.js +0 -7
  67. package/dist/src/geometry.d.ts +0 -176
  68. package/dist/src/geometry.js +0 -546
  69. package/dist/src/index.d.ts +0 -16
  70. package/dist/src/index.js +0 -18
  71. package/dist/src/ingest-cache.d.ts +0 -41
  72. package/dist/src/ingest-cache.js +0 -161
  73. package/dist/src/mind/articulation.d.ts +0 -6
  74. package/dist/src/mind/articulation.js +0 -99
  75. package/dist/src/mind/attention.d.ts +0 -88
  76. package/dist/src/mind/attention.js +0 -997
  77. package/dist/src/mind/bridge.d.ts +0 -30
  78. package/dist/src/mind/bridge.js +0 -569
  79. package/dist/src/mind/canonical.d.ts +0 -29
  80. package/dist/src/mind/canonical.js +0 -88
  81. package/dist/src/mind/graph-search.d.ts +0 -285
  82. package/dist/src/mind/graph-search.js +0 -956
  83. package/dist/src/mind/index.d.ts +0 -5
  84. package/dist/src/mind/index.js +0 -5
  85. package/dist/src/mind/junction.d.ts +0 -95
  86. package/dist/src/mind/junction.js +0 -262
  87. package/dist/src/mind/learning.d.ts +0 -47
  88. package/dist/src/mind/learning.js +0 -248
  89. package/dist/src/mind/match.d.ts +0 -126
  90. package/dist/src/mind/match.js +0 -441
  91. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  92. package/dist/src/mind/mechanisms/alu.js +0 -36
  93. package/dist/src/mind/mechanisms/cast.d.ts +0 -89
  94. package/dist/src/mind/mechanisms/cast.js +0 -716
  95. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  96. package/dist/src/mind/mechanisms/confluence.js +0 -225
  97. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  98. package/dist/src/mind/mechanisms/cover.js +0 -203
  99. package/dist/src/mind/mechanisms/extraction.d.ts +0 -67
  100. package/dist/src/mind/mechanisms/extraction.js +0 -387
  101. package/dist/src/mind/mechanisms/recall.d.ts +0 -13
  102. package/dist/src/mind/mechanisms/recall.js +0 -325
  103. package/dist/src/mind/mind.d.ts +0 -274
  104. package/dist/src/mind/mind.js +0 -572
  105. package/dist/src/mind/pipeline-mechanism.d.ts +0 -142
  106. package/dist/src/mind/pipeline-mechanism.js +0 -213
  107. package/dist/src/mind/pipeline.d.ts +0 -20
  108. package/dist/src/mind/pipeline.js +0 -217
  109. package/dist/src/mind/primitives.d.ts +0 -70
  110. package/dist/src/mind/primitives.js +0 -287
  111. package/dist/src/mind/rationale.d.ts +0 -134
  112. package/dist/src/mind/rationale.js +0 -162
  113. package/dist/src/mind/reasoning.d.ts +0 -34
  114. package/dist/src/mind/reasoning.js +0 -240
  115. package/dist/src/mind/recognition.d.ts +0 -20
  116. package/dist/src/mind/recognition.js +0 -390
  117. package/dist/src/mind/resonance.d.ts +0 -23
  118. package/dist/src/mind/resonance.js +0 -0
  119. package/dist/src/mind/trace.d.ts +0 -15
  120. package/dist/src/mind/trace.js +0 -73
  121. package/dist/src/mind/traverse.d.ts +0 -113
  122. package/dist/src/mind/traverse.js +0 -504
  123. package/dist/src/mind/types.d.ts +0 -271
  124. package/dist/src/mind/types.js +0 -130
  125. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  126. package/dist/src/rabitq-ivf/src/database.js +0 -201
  127. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  128. package/dist/src/rabitq-ivf/src/index.js +0 -4
  129. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  130. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  131. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  132. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  133. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  134. package/dist/src/rabitq-ivf/src/rabitq.js +0 -283
  135. package/dist/src/sema.d.ts +0 -31
  136. package/dist/src/sema.js +0 -63
  137. package/dist/src/store-sqlite.d.ts +0 -184
  138. package/dist/src/store-sqlite.js +0 -942
  139. package/dist/src/store.d.ts +0 -663
  140. package/dist/src/store.js +0 -1618
  141. package/dist/src/vec.d.ts +0 -31
  142. package/dist/src/vec.js +0 -109
@@ -1,325 +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) => {
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
- };
34
- };
35
- const k = pre.k;
36
- const queryGist = pre.guide;
37
- // 0. Exact self-match — content-addressed, deterministic.
38
- const qId = pre.queryResolved;
39
- if (qId !== null) {
40
- const rev = ctx.store.prevFirst(qId, hubBound(ctx));
41
- const g = reverseContext(ctx, qId, queryGist, rev);
42
- if (g !== null) {
43
- return ground(g, rev.length === 1
44
- ? "exact self-match — reverse recall to the sole predecessor"
45
- : "exact self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
46
- }
47
- }
48
- // 0b. ARGUMENT BINDING (RC8): the query is not itself a stored form, but
49
- // it CONTAINS a recognised constituent that is an edge SOURCE — a learnt
50
- // pair's left side carried inside a wrapper ("How do you say 'thank you'
51
- // in French?"). The wrapper is scaffolding; the argument is the span
52
- // that LEADS somewhere, so its continuation — guided by the whole query's
53
- // gist — is the answer. Matching the wrapper while ignoring the argument
54
- // (the observed "good morning" template failure) is worse than silence,
55
- // so anything short of ONE unambiguous binding falls through: the
56
- // constituent bar is the same two-quanta (2W) reading confluence binds
57
- // under, nested recognitions collapse to their MAXIMAL span, and two
58
- // distinct maximal arguments mean the query asks about neither alone.
59
- if (qId === null) {
60
- const W2 = 2 * ctx.space.maxGroup;
61
- const args = pre.rec.sites.filter((s) => s.end - s.start >= W2 &&
62
- s.end - s.start < query.length &&
63
- ctx.store.hasNext(s.payload));
64
- // Maximal spans by one sorted sweep (starts ascending, ties longest
65
- // first): every earlier span starts at or before s, so s is contained
66
- // exactly when the running max end already covers it. O(m log m) — a
67
- // long input recognises O(|input|) sites, and a pairwise scan here was
68
- // quadratic in the input.
69
- args.sort((a, b) => a.start - b.start || b.end - a.end);
70
- const maximal = [];
71
- let maxEnd = -1;
72
- for (const s of args) {
73
- if (s.end <= maxEnd)
74
- continue;
75
- maximal.push(s);
76
- maxEnd = s.end;
77
- }
78
- // The wrapper must actually BE scaffolding: RC8's own premise is "the
79
- // wrapper is scaffolding; the argument is the span that leads
80
- // somewhere" ("How do you say 'thank you' in French?" — everything
81
- // outside the argument is a small fixed template). When the query
82
- // instead has ANOTHER substantial recognised form (≥ W2, the same
83
- // constituent bar the argument itself must clear) sitting OUTSIDE the
84
- // chosen argument, the query is not one argument in a wrapper — it is
85
- // several complete, independently-meaningful pieces (a multi-turn
86
- // conversation's own accumulated turns are exactly this shape), and
87
- // binding to the argument's continuation would answer past content
88
- // the query itself already carries forward. Derived from the same W2
89
- // bar the argument itself is held to, never a separate tuned number.
90
- const hasSubstantialOutside = maximal.length === 1 &&
91
- pre.rec.sites.some((s) => s.end - s.start >= W2 &&
92
- (s.end <= maximal[0].start || s.start >= maximal[0].end));
93
- if (maximal.length === 1 && !hasSubstantialOutside) {
94
- const arg = maximal[0];
95
- const g = await follow(ctx, arg.payload, queryGist);
96
- // The same "no restated fragment" guard tier 2 applies below (§ "the
97
- // anchor cleared the consensus floor..."): a followed continuation
98
- // that is itself a proper byte-subspan of the QUERY restates part of
99
- // the question — never an answer. A multi-turn query's own later
100
- // turns are exact, content-addressed matches for exactly this reason
101
- // (each turn is its own previously-learnt form), so without this
102
- // guard the argument's OWN later restatement in the same
103
- // conversation reads as if it were the next thing to say.
104
- if (g !== null && g.length > 0 &&
105
- !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
106
- return ground(g, "argument binding — the query's sole edge-source constituent, continuation followed", [[arg.start, arg.end]], STEP);
107
- }
108
- }
109
- }
110
- const whole = await ctx.store.resonate(queryGist, k);
111
- if (whole.length === 0) {
112
- return ground(null, "empty store — nothing to resonate with", [], 0);
113
- }
114
- const top = whole[0];
115
- 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)`);
116
- // 1. Clean resonance — the scale-aware identity claim. The ANGLE
117
- // (top.score) carries the shared fraction; the query's MAGNITUDE (√len,
118
- // the linear fold's own norm) converts the tolerated foreign fraction
119
- // into bytes — at most one river window (see {@link identityBar}). A
120
- // fixed cosine bar let long queries claim "near-identical" while whole
121
- // windows — an answer word — differed.
122
- // A hit RESTATES the query when its bytes are the query's own — exactly,
123
- // or under the response's equivalence (a case/width twin). Restating
124
- // hits may only conclude through disciplined reverse recall: voicing
125
- // their bytes echoes the query back at itself (never an answer — the
126
- // same principle that keeps cast from voicing stored questions), and
127
- // projecting them forward is reverse recall's containment failure in the
128
- // other direction — "whatever followed these bytes in some document".
129
- const qKey = ctx.canon ? ctx.canon(query) : query;
130
- const restates = (b) => bytesEqual(b, query) ||
131
- (ctx.canon !== null && bytesEqual(ctx.canon(b), qKey));
132
- const idBar = identityBar(ctx.store.D, ctx.space.maxGroup, query.length);
133
- if (top.score >= idBar) {
134
- for (const h of whole) {
135
- // The identity claim is PER HIT, not per tier: hits are ranked
136
- // nearest-first, and grounding one below the bar under this tier's
137
- // "near-identical" label would launder byte-overlap noise (observed:
138
- // "merci" projecting through the unrelated near hit "meraih").
139
- if (h.score < idBar)
140
- break;
141
- const own = read(ctx, h.id);
142
- if (h.id === qId || restates(own)) {
143
- const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
144
- const g = reverseContext(ctx, h.id, queryGist, rev);
145
- if (g !== null) {
146
- return ground(g, rev.length === 1
147
- ? "perfect self-match — reverse recall to the sole predecessor"
148
- : "perfect self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
149
- }
150
- continue;
151
- }
152
- const g = await project(ctx, h.id, queryGist);
153
- if (g) {
154
- return ground(g, "clean whole-query resonance — ground the nearest hit", whole_, STEP);
155
- }
156
- }
157
- }
158
- // The query-relative grounding fraction, shared by tiers 2–4 — gated on
159
- // the FRACTION OF THE QUERY the grounding explains, not the raw cosine.
160
- // Root gists are unit vectors, but their magnitudes are recoverable from
161
- // the byte lengths (‖·‖ = √len under the linear fold):
162
- // cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
163
- // The raw cosine punished honest containment — a query fully inside a
164
- // longer grounded answer scored √(lenQ/lenG) and was refused — and let a
165
- // long answer sharing only scaffolding pass; the query-relative fraction
166
- // measures exactly what the reach bar means: how much of THE QUERY the
167
- // store accounts for.
168
- // Chance similarity survives the length conversion AMPLIFIED: the same
169
- // √(lenG/lenQ) factor that converts an honest shared fraction into a
170
- // query-relative one multiplies the estimator/chance floor too, so a long
171
- // stored form (√(lenG/lenQ) ≈ 10 at 100×) lifted a noise-level cosine past
172
- // the reach bar and grounded pure gibberish (observed). Only the
173
- // ABOVE-CHANCE part of the similarity is evidence of shared content —
174
- // subtract the significance bar (3/√D, §8.3) before converting. Derived
175
- // from the existing bars; never tuned.
176
- const sig = significanceBar(ctx.store.D);
177
- const reach = reachThreshold(ctx.space.maxGroup);
178
- const fracOfQuery = (cos, otherLen) => Math.min(1, Math.max(0, cos - sig) *
179
- Math.sqrt(otherLen / Math.max(1, query.length)));
180
- // 2. Scaffolding-dominated.
181
- if (top.score >= sig) {
182
- const N = corpusN(ctx);
183
- const minVote = consensusFloor(N);
184
- // The committed points of attention ARE the shared climb's roots (same
185
- // query, same k, same DF mode) — read them from Precomputed instead of
186
- // re-climbing, so even a traced response pays for the climb once.
187
- const forest = (await pre.attention()).roots;
188
- if (forest.length > 0 && forest[0].vote >= minVote) {
189
- const g = await project(ctx, forest[0].anchor, queryGist);
190
- // The anchor cleared the consensus floor, but the floor prices the
191
- // ANCHOR's evidence, not the projection's: a junk attractor can clear
192
- // it and project a PIECE OF THE QUERY back at it (the observed
193
- // "buenos días in English" → "English" fragment). A projection that
194
- // is a proper byte-subspan of the query restates part of the question
195
- // — never an answer (the same principle as `restates` above, extended
196
- // to fragments). Genuine anchor groundings — longer than the query,
197
- // or disjoint from it — pass untouched.
198
- if (g && !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
199
- return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
200
- }
201
- }
202
- }
203
- // 3. Last resort — the nearest grounded whole-query hit, same gate.
204
- for (const h of whole) {
205
- const g = await project(ctx, h.id, queryGist);
206
- if (g) {
207
- if (fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
208
- reach) {
209
- return ground(g, "last resort: the nearest grounded whole-query hit", [], STEP);
210
- }
211
- }
212
- }
213
- // 3b. Corroborated-substitution bridge — refusal-path only (bridge.ts).
214
- // Every gist-based tier has failed; before refusing, align the query
215
- // byte-for-byte against the trained contexts its own stored windows
216
- // anchor, accepting mismatches only as corpus-attested, concept-bar
217
- // substitutions. A bridged context grounds exactly like any hit —
218
- // projected through its learnt edges — under the same restated-fragment
219
- // guard tiers 0b/2 apply. Costs nothing on any answering path.
220
- {
221
- // The resonance hits already ranked above are handed to the bridge as
222
- // PROPOSED candidates alongside its own anchor climbs: on a corpus this
223
- // size a W-byte window is far too common for the clamped climb to
224
- // single out the right trained context, while the whole-query gist
225
- // already ranked it nearest (observed live: "what is the capital of
226
- // france" resonating straight to "What is the capital of France?" yet
227
- // refusing on the reach bar). Approximate scores propose; the bridge's
228
- // byte-exact alignment and attestation gates decide.
229
- //
230
- // The proposal breadth here is widened PAST `k` — first by requesting
231
- // hubBound(ctx) candidates instead of `k` (recall's own tiers above
232
- // stay at `k`; this re-resonates only on the refusal path, exactly
233
- // where the bridge itself already runs), AND by asking the index to
234
- // search EXHAUSTIVELY. Both matter: the IVF only ever probes
235
- // ⌈√clusters⌉ of them (store.ts's efFor) REGARDLESS of k — widening k
236
- // alone just returns more hits from the SAME already-probed clusters,
237
- // never a hit whose vector lives in an unprobed one. Measured live:
238
- // "What is the chemical symbol for water?" needs "What is the
239
- // chemical formula for water?", scoring only 0.58 against the
240
- // query's gist (a MIDDLE-of-string word swap perturbs the river-fold
241
- // tree hash far more than a same-length TAIL swap like the "carbon"/
242
- // "oxygen" neighbours that outrank it at 0.87+) — absent from the
243
- // resonance list even at k=5000, present and byte-exact-verified the
244
- // moment it's force-fed to the bridge directly. `exhaustive` is the
245
- // natural, tuning-free ceiling (probe every cluster) for a call that
246
- // is ALREADY refusal-path-only and must not miss a candidate hiding
247
- // behind an unlucky structural distance.
248
- const wide = k < hubBound(ctx)
249
- ? await ctx.store.resonate(queryGist, hubBound(ctx), true)
250
- : whole;
251
- const bridged = await substitutionBridge(ctx, query, wide.map((h) => h.id));
252
- if (bridged !== null) {
253
- const g = await project(ctx, bridged.id, queryGist);
254
- // A projection contained in a substituted candidate-side span is the
255
- // substitution RESTATED as if it were knowledge — the exact failure
256
- // observed live: "Darwin was born in England." bridged to the
257
- // Einstein fact through " England." → " Germany." and would have
258
- // voiced "Germany", an answer the substitution itself manufactured.
259
- // The same principle as the restated-fragment guards above, extended
260
- // to the bridge's own substitutions.
261
- const cBytes = ctx.store.bytes(bridged.id);
262
- const manufactured = g !== null &&
263
- bridged.subs.some((s) => indexOf(cBytes.subarray(s.cs, s.ce), g, 0) >= 0);
264
- if (g !== null && g.length > 0 && !restates(g) && !manufactured &&
265
- !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
266
- return ground(g, `substitution bridge — a trained context accounts for the query ` +
267
- `up to ${bridged.subs.length} corroborated substitution(s)`,
268
- // Accounted NOTHING — the same epistemic humility as the echo
269
- // tier below: a substitution-bridged grounding is a last resort
270
- // that must lose to ANY mechanism that actually explained the
271
- // query (observed: pricing the aligned spans here outweighed
272
- // extraction's correct answer in the grounding decider), while
273
- // still beating silence when everything else refused.
274
- [], CONCEPT * bridged.subs.length + STEP);
275
- }
276
- }
277
- }
278
- // The refusal/echo decision. The echo returns a stored form's bytes AS
279
- // the answer — a near-identity claim about the query — and identity-grade
280
- // decisions are never made on an estimated score ("approximate scores may
281
- // rank and propose; they may never decide", §6.2): the RaBitQ estimate
282
- // overshooting the reach bar echoed a WRONG-entity neighbour ("capital of
283
- // Zamunda?" echoed the Armenia fact, observed). The bytes are read
284
- // anyway to be echoed, so the decision uses their EXACT fold: one river
285
- // fold of the top hit, measured in the same query-relative,
286
- // chance-corrected units as the tier above.
287
- const topBytes = read(ctx, top.id);
288
- const exact = topBytes.length > 0
289
- ? cosine(queryGist, gistOf(ctx, topBytes))
290
- : 0;
291
- if (fracOfQuery(exact, topBytes.length) < reach) {
292
- return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
293
- }
294
- // Echoing the query's own bytes back at it is not an echo of a RELATED
295
- // form — it is the query restated, which answers nothing.
296
- if (restates(topBytes)) {
297
- return ground(null, "the nearest form IS the query itself — restating it answers nothing", [], 0);
298
- }
299
- // Honest echo.
300
- return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
301
- }
302
- // ── Pipeline mechanism ──────────────────────────────────────────────────────
303
- export const recallMechanism = {
304
- name: "recall",
305
- provenance: "recall",
306
- // Recall's floor is free to state (one STEP-grade projection) and its run
307
- // gates its own tiers — no expensive investment happens inside floor, so
308
- // there is nothing to guard with worthRunning here: the pipeline's own
309
- // check prunes run() against the incumbent.
310
- async floor(_ctx, _query, _pre, _worthRunning) {
311
- return STEP;
312
- },
313
- async run(ctx, query, pre) {
314
- const r = await recallByResonance(ctx, query, pre);
315
- if (!r)
316
- return [];
317
- return [{
318
- bytes: r.bytes,
319
- accounted: r.accounted,
320
- moves: r.moves,
321
- unexplained: r.unexplained,
322
- provenance: r.echoed ? "recall-echo" : "recall",
323
- }];
324
- },
325
- };
@@ -1,274 +0,0 @@
1
- import { Vec } from "../vec.js";
2
- import { Sema, Space } from "../sema.js";
3
- import { Alphabet } from "../alphabet.js";
4
- import { Grid } from "../geometry.js";
5
- import { BoundedMap, type Store } from "../store.js";
6
- import { type MindConfig } from "../config.js";
7
- import { type Canon } from "../canon.js";
8
- import { GraphSearch, type Leaf, type Site } from "./graph-search.js";
9
- import type { ComputedSpan, ExtensionHost } from "../extension.js";
10
- export type { ComputedSpan, ExtensionHost };
11
- import { type InspectRationale, Rationale } from "./rationale.js";
12
- export type { InspectRationale, RationaleItem, RationaleStep, } from "./rationale.js";
13
- export type Input = string | Uint8Array | Grid | Grid[];
14
- export interface Response {
15
- v: Vec | null;
16
- bytes: Uint8Array;
17
- /** How the answer was grounded (see {@link Provenance}). `"recall-echo"`
18
- * marks the last-resort fallback that returned the nearest stored form's
19
- * own bytes verbatim — an echo, NOT a grounded fact. Absent when there is
20
- * no answer. */
21
- provenance?: import("./pipeline.js").Provenance;
22
- }
23
- /** Serializable state of a conversation — can be saved and restored across
24
- * sessions. The Mind never interprets the bytes; it only tracks their
25
- * cumulative lengths so the caller can reconstruct turn boundaries later
26
- * without inspecting content. */
27
- export interface ConversationState {
28
- /** The accumulated context bytes — raw concatenation of every turn's
29
- * bytes in order. No separator is inserted; the boundary offsets
30
- * ({@link boundaries}) tell the caller where each turn ends. */
31
- context: Uint8Array;
32
- /** Cumulative byte length after each completed turn. Sorted, strictly
33
- * increasing, each {@code < context.length}. The first turn's length
34
- * is `boundaries[0]`; the second turn starts at that offset, and so
35
- * on. Empty for a single-turn or new conversation. */
36
- boundaries: number[];
37
- }
38
- /** An active conversation handle. Opaque — interact through the Mind's
39
- * conversation methods ({@link Mind.beginConversation},
40
- * {@link Mind.respondTurn}, {@link Mind.endConversation}). */
41
- export interface Conversation {
42
- readonly id: number;
43
- }
44
- import type { AttentionRead, MindContext, Recognition } from "./types.js";
45
- export interface MindOptions {
46
- seed?: number;
47
- recallQueryK?: number;
48
- haloQueryK?: number;
49
- normalizeEpsilon?: number;
50
- cosineEpsilon?: number;
51
- geometry?: Partial<import("../config.js").GeometryConfig>;
52
- alphabet?: Partial<import("../config.js").AlphabetConfig>;
53
- storeConfig?: Partial<import("../config.js").StoreConfig>;
54
- store?: Store;
55
- /** Additional grounding mechanisms (appended after the built-in defaults). */
56
- mechanisms?: import("./pipeline-mechanism.js").PipelineMechanism[];
57
- /** Factories that receive the {@link ExtensionHost} and return mechanisms. */
58
- mechanismFactories?: ((host: import("../extension.js").ExtensionHost) => import("./pipeline-mechanism.js").PipelineMechanism)[];
59
- /** Content canonicalizer applied to EVERY response (any modality) for
60
- * equivalence-class resolution — see src/canon.ts. Text entry points
61
- * ({@link Mind.respondText}, {@link Mind.respondTurnText}) inject the
62
- * Unicode text canonicalizer automatically when this is unset; pass
63
- * `false` to disable canonical resolution everywhere. */
64
- canon?: Canon | false;
65
- }
66
- export declare class Mind implements MindContext {
67
- readonly space: Space;
68
- readonly alphabet: Alphabet;
69
- readonly store: Store;
70
- readonly cfg: MindConfig;
71
- /** The lightest-derivation engine over the Sema graph. */
72
- readonly search: GraphSearch;
73
- /** The grounding mechanisms iterated by {@link think}. */
74
- readonly mechanisms: import("./pipeline-mechanism.js").PipelineMechanism[];
75
- /** The live rationale tracer for the inference currently in flight, or null. */
76
- trace: Rationale | null;
77
- /** The content canonicalizer for the response in flight — see
78
- * {@link MindContext.canon}. Injected per response by the modality entry
79
- * point; null when the response carries no equivalence. */
80
- canon: Canon | null;
81
- /** Per-response canonical-resolution memo — see {@link MindContext.canonMemo}. */
82
- canonMemo: Map<string, number | null> | null;
83
- /** The Mind-level canon option: a canonicalizer to use for EVERY response,
84
- * `false` to disable canonical resolution, or null to let each entry
85
- * point decide (text entry points inject {@link textCanon}). */
86
- private _canonOpt;
87
- /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
88
- climbMemo: Map<string, Map<string, AttentionRead>> | null;
89
- /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
90
- recogniseMemo: Map<string, Recognition> | null;
91
- /** Memo of perceive() — content-keyed. See {@link MindContext.perceiveMemo}. */
92
- perceiveMemo: Map<string, import("../sema.js").Sema> | null;
93
- /** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
94
- _resolvedSubtrees: WeakMap<import("../sema.js").Sema, {
95
- id: number;
96
- len: number;
97
- }> | null;
98
- /** The perceived gist of the query currently being answered. Set by `think`
99
- * before the graph search runs; `chooseNext` consults it as a gate (a null
100
- * guide means no query is in flight, so structural walkers keep plain
101
- * first-edge behaviour) and the reverse projection uses it for
102
- * reverse-recall disambiguation via `chooseAmong`. */
103
- _edgeGuide: Vec | null;
104
- /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
105
- * of a single response follows the SAME continuation for each ambiguous
106
- * context node. */
107
- _edgeChoice: Map<number, number>;
108
- /** Previous deposit's seen node ids for incremental change detection. */
109
- _prevSeen: Set<number> | null;
110
- /** Session cache of node-id → perceived gist for candidate scoring — see
111
- * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
112
- * candidate sets (√N at most) fit comfortably and recur across queries. */
113
- _gistCache: BoundedMap<number, Vec>;
114
- _depositTrees: BoundedMap<string, import("./types.js").DepositCacheEntry>;
115
- _depositLens: Set<number>;
116
- _internIds: WeakMap<Sema, number>;
117
- private _nextConvId;
118
- private _conversations;
119
- /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
120
- resolve(bytes: Uint8Array): number | null;
121
- recogniseSpan(bytes: Uint8Array): {
122
- sites: ReadonlyArray<Site>;
123
- leaves: ReadonlyArray<Leaf>;
124
- splits: ReadonlySet<number>;
125
- starts: ReadonlySet<number>;
126
- };
127
- /** Disambiguate among multiple learnt continuations of the same context node.
128
- * Required by {@link GraphSearchHost} — the graph search calls this through the
129
- * host interface when a recognised form has more than one outgoing edge.
130
- * Delegates to the standalone {@link chooseNext} which picks the candidate
131
- * with the most distributional evidence (highest `prevOf` count — the
132
- * structural manifestation of its halo). When evidence is equal the
133
- * first-inserted edge wins. */
134
- chooseNext(node: number): number | undefined;
135
- constructor(opts?: MindOptions);
136
- constructor(cfg: MindConfig, store: Store, _fromStore: true);
137
- /** Exposed for tests: the consensus climb over query sub-regions. */
138
- climbAttention(query: Uint8Array, k: number, mode?: import("./types.js").DFMode): Promise<import("./types.js").Attention[]>;
139
- /** Exposed for tests: climb the structural DAG from a node to its
140
- * edge-bearing ancestor contexts. */
141
- edgeAncestors(id: number, contextCount: number): import("./types.js").AncestorReach;
142
- /** Exposed for tests: find the natural break point in a sorted vote list. */
143
- naturalBreak(votes: number[]): number;
144
- /** Perceive input into a content-defined tree. Deterministic — identical
145
- * bytes always produce an identical tree. Public for ingest-cache. */
146
- perceive(input: Input, leafAt?: (i: number) => number | null, lookup?: (ids: number[]) => number | null): Sema;
147
- /** Open one response's transient state — the tracer and the per-response
148
- * memos. Paired with {@link endResponse}; the ONE place this state is
149
- * created, so adding a memo cannot forget its reset. */
150
- private beginResponse;
151
- /** The canonicalizer a response should carry: the Mind-level option when
152
- * set (or none when explicitly disabled), else the entry point's own
153
- * default — text entry points pass {@link textCanon}, binary ones null. */
154
- private _canonFor;
155
- /** Close one response's transient state — every per-response field, incl.
156
- * the edge guide/choices `think` sets mid-flight. */
157
- private endResponse;
158
- /** Shared response core — the one path from bytes to voiced answer.
159
- * `respond` calls this directly; `respondTurn` has its own path
160
- * with conversation-persistent memos and incremental perception. */
161
- private _respondImpl;
162
- respond(input: Input, inspectRationale?: InspectRationale): Promise<Response>;
163
- /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
164
- * decoding — they are structural padding in text answers. LOSSY for a
165
- * binary answer that legitimately contains NULs: use {@link respond} and
166
- * read `bytes` directly for binary/grid modalities.
167
- *
168
- * Injects the TEXT canonicalizer (src/canon.ts) so resolution treats
169
- * every character variation of the same text — case, width, whitespace —
170
- * as one form, provided the store's canon index is built
171
- * ({@link buildCanonIndex}). */
172
- respondText(input: string, inspectRationale?: InspectRationale): Promise<string>;
173
- /** Begin a new conversation, optionally restoring from a previously-saved
174
- * {@link ConversationState}. The returned handle is required for
175
- * {@link respondTurn} and {@link endConversation}.
176
- *
177
- * Conversations are independent — a Mind can manage several concurrently.
178
- * Each tracks the fold pyramid (accumulated internal processing) and
179
- * turn-boundary offsets; the geometry never inspects content to guess
180
- * where one turn ends and the next begins. */
181
- beginConversation(state?: ConversationState): Conversation;
182
- /** End a conversation, releasing its internal resources (accumulated
183
- * context, boundary offsets, and the fold-pyramid cache). Idempotent. */
184
- endConversation(conv: Conversation): void;
185
- /** The current serialisable state of an active conversation. Save this
186
- * to resume the conversation later via {@link beginConversation}. */
187
- conversationState(conv: Conversation): ConversationState | null;
188
- /** Append a turn to a conversation's accumulated context WITHOUT
189
- * responding — raw byte append plus a boundary offset, never a
190
- * separator; the fold pyramid advances by O(turn).
191
- *
192
- * This is the primitive for turns the Mind should hear but not answer:
193
- * replaying a transcript, feeding the OTHER speaker's line in a
194
- * prediction harness, or restoring context piecewise. {@link
195
- * respondTurn} = addTurn + think + its own reply appended the same way. */
196
- addTurn(conv: Conversation, turn: Input): ConversationState;
197
- /** Grow a conversation's accumulated context by one turn's bytes — raw
198
- * append plus a boundary offset, pyramid advanced by O(turn), the grown
199
- * context's tree seeded into the conversation's perceive memo. The ONE
200
- * place a context grows ({@link addTurn} and {@link respondTurn} both
201
- * come through here), so the append semantics cannot drift. */
202
- private _growContext;
203
- /** Process one turn of a conversation.
204
- *
205
- * `turn` is the raw input for the latest turn — its bytes are appended
206
- * to the accumulated context directly (raw concatenation). The Mind
207
- * tracks the byte offset where each turn ends; no separator is ever
208
- * inserted or inspected.
209
- *
210
- * Returns the response AND the updated {@link ConversationState} so the
211
- * caller can persist it. The conversation handle's internal state is
212
- * updated in place — the returned state is a snapshot for storage.
213
- *
214
- * SINGLE FLIGHT: at most one respondTurn may be in flight per Mind. The
215
- * conversation's memo caches are swapped into the Mind-level per-response
216
- * pointers for the duration of the turn, so a concurrently-running
217
- * respond()/respondTurn() on the SAME Mind would interleave state.
218
- * Different Minds (or sequential awaits, as in every test) are safe. */
219
- respondTurn(conv: Conversation, turn: Input, inspectRationale?: InspectRationale): Promise<{
220
- response: Response;
221
- state: ConversationState;
222
- }>;
223
- /** Text view of {@link respondTurn}. See {@link respondText} for the
224
- * NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
225
- * directly — this is a text-only convenience, like {@link respondText}. */
226
- respondTurnText(conv: Conversation, turn: string, inspectRationale?: InspectRationale): Promise<{
227
- response: string;
228
- state: ConversationState;
229
- }>;
230
- embedding(input: Input): Promise<Vec | null>;
231
- /** Kinship note: the vector arm below is a miniature of recall's tier 3
232
- * (resonate → reach gate → read out the nearest form's bytes) — the
233
- * read-out direction of the same operation, without recall's grounding
234
- * ladder. If either side's acceptance rule changes, revisit the other. */
235
- express(idOrV: number | Vec): Promise<Uint8Array>;
236
- ingest(input: Input | (Input | [Input, Input])[], second?: Input): Promise<(Sema & {
237
- id: number;
238
- }) | undefined>;
239
- private extensionHost;
240
- private groundedContinuation;
241
- /** Re-index structurally-important nodes whose gists were evicted from the
242
- * pending cache before they reached the content index. See {@link
243
- * Store.repairContentIndex} for the contract; this method wires the
244
- * Mind's perception into the store's repair walk.
245
- *
246
- * Run this after training or at checkpoints to restore recall reach for
247
- * nodes that bridge experiences but were never indexed. A pure interior
248
- * node (no edges, no halo) is deliberately skipped — it is scaffolding,
249
- * not an experience root or bridge, and regenerating its gist would waste
250
- * I/O and index space for no recall benefit.
251
- *
252
- * @param minParents only repair nodes with ≥ this many structural parents
253
- * (default 2 — structural bridges)
254
- * @returns number of nodes added to the content index */
255
- repairContentIndex(minParents?: number): Promise<number>;
256
- /** Build (or incrementally refresh) the store's canonical-form index: for
257
- * every content-bearing node, record the hash of its CANONICAL key so
258
- * resolution can find stored forms across surface variation (case, width,
259
- * whitespace — whatever `canon` equates; see src/canon.ts).
260
- *
261
- * Incremental and idempotent: the last indexed node id is remembered in
262
- * store meta (`canon.upto`), so a refresh after further training scans
263
- * only the new rows. Run once after training, and again after ingests —
264
- * the same operational shape as {@link repairContentIndex}.
265
- *
266
- * @param canon the canonicalizer to index under — MUST be the same one
267
- * queries will carry (text queries carry {@link textCanon}
268
- * unless the Mind was constructed with its own)
269
- * @returns number of index rows added */
270
- buildCanonIndex(canon?: Canon): Promise<number>;
271
- save(): Promise<Uint8Array>;
272
- static load(snapshot: Uint8Array, store: Store): Promise<Mind>;
273
- static loadFromStore(store: Store): Promise<Mind>;
274
- }