@hviana/sema 0.2.5 → 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 (133) hide show
  1. package/package.json +1 -1
  2. package/src/mind/attention.ts +355 -19
  3. package/test/53-cross-region-probe-instrumentation.test.mjs +356 -0
  4. package/dist/example/demo.d.ts +0 -1
  5. package/dist/example/demo.js +0 -39
  6. package/dist/example/train_base.d.ts +0 -87
  7. package/dist/example/train_base.js +0 -2252
  8. package/dist/src/alphabet.d.ts +0 -7
  9. package/dist/src/alphabet.js +0 -33
  10. package/dist/src/alu/src/alu.d.ts +0 -185
  11. package/dist/src/alu/src/alu.js +0 -440
  12. package/dist/src/alu/src/expr.d.ts +0 -61
  13. package/dist/src/alu/src/expr.js +0 -318
  14. package/dist/src/alu/src/index.d.ts +0 -11
  15. package/dist/src/alu/src/index.js +0 -19
  16. package/dist/src/alu/src/kernel-arith.d.ts +0 -16
  17. package/dist/src/alu/src/kernel-arith.js +0 -264
  18. package/dist/src/alu/src/kernel-bits.d.ts +0 -19
  19. package/dist/src/alu/src/kernel-bits.js +0 -152
  20. package/dist/src/alu/src/kernel-logic.d.ts +0 -4
  21. package/dist/src/alu/src/kernel-logic.js +0 -60
  22. package/dist/src/alu/src/kernel-nd.d.ts +0 -3
  23. package/dist/src/alu/src/kernel-nd.js +0 -208
  24. package/dist/src/alu/src/kernel-numeric.d.ts +0 -54
  25. package/dist/src/alu/src/kernel-numeric.js +0 -366
  26. package/dist/src/alu/src/operation.d.ts +0 -168
  27. package/dist/src/alu/src/operation.js +0 -189
  28. package/dist/src/alu/src/parser.d.ts +0 -221
  29. package/dist/src/alu/src/parser.js +0 -577
  30. package/dist/src/alu/src/resonance.d.ts +0 -55
  31. package/dist/src/alu/src/resonance.js +0 -126
  32. package/dist/src/alu/src/text.d.ts +0 -31
  33. package/dist/src/alu/src/text.js +0 -73
  34. package/dist/src/alu/src/value.d.ts +0 -109
  35. package/dist/src/alu/src/value.js +0 -300
  36. package/dist/src/alu/test/alu.test.d.ts +0 -1
  37. package/dist/src/alu/test/alu.test.js +0 -764
  38. package/dist/src/bytes.d.ts +0 -14
  39. package/dist/src/bytes.js +0 -59
  40. package/dist/src/canon.d.ts +0 -26
  41. package/dist/src/canon.js +0 -57
  42. package/dist/src/config.d.ts +0 -111
  43. package/dist/src/config.js +0 -91
  44. package/dist/src/derive/src/deduction.d.ts +0 -125
  45. package/dist/src/derive/src/deduction.js +0 -155
  46. package/dist/src/derive/src/index.d.ts +0 -7
  47. package/dist/src/derive/src/index.js +0 -11
  48. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  49. package/dist/src/derive/src/priority-queue.js +0 -73
  50. package/dist/src/derive/src/rewrite.d.ts +0 -56
  51. package/dist/src/derive/src/rewrite.js +0 -100
  52. package/dist/src/derive/src/trie.d.ts +0 -90
  53. package/dist/src/derive/src/trie.js +0 -217
  54. package/dist/src/derive/test/derive.test.d.ts +0 -1
  55. package/dist/src/derive/test/derive.test.js +0 -122
  56. package/dist/src/extension.d.ts +0 -37
  57. package/dist/src/extension.js +0 -7
  58. package/dist/src/geometry.d.ts +0 -202
  59. package/dist/src/geometry.js +0 -575
  60. package/dist/src/index.d.ts +0 -16
  61. package/dist/src/index.js +0 -18
  62. package/dist/src/ingest-cache.d.ts +0 -41
  63. package/dist/src/ingest-cache.js +0 -161
  64. package/dist/src/mind/articulation.d.ts +0 -6
  65. package/dist/src/mind/articulation.js +0 -99
  66. package/dist/src/mind/attention.d.ts +0 -315
  67. package/dist/src/mind/attention.js +0 -1549
  68. package/dist/src/mind/bridge.d.ts +0 -30
  69. package/dist/src/mind/bridge.js +0 -569
  70. package/dist/src/mind/canonical.d.ts +0 -29
  71. package/dist/src/mind/canonical.js +0 -88
  72. package/dist/src/mind/graph-search.d.ts +0 -285
  73. package/dist/src/mind/graph-search.js +0 -956
  74. package/dist/src/mind/index.d.ts +0 -7
  75. package/dist/src/mind/index.js +0 -5
  76. package/dist/src/mind/junction.d.ts +0 -124
  77. package/dist/src/mind/junction.js +0 -317
  78. package/dist/src/mind/learning.d.ts +0 -47
  79. package/dist/src/mind/learning.js +0 -248
  80. package/dist/src/mind/match.d.ts +0 -126
  81. package/dist/src/mind/match.js +0 -441
  82. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  83. package/dist/src/mind/mechanisms/alu.js +0 -36
  84. package/dist/src/mind/mechanisms/cast.d.ts +0 -89
  85. package/dist/src/mind/mechanisms/cast.js +0 -716
  86. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  87. package/dist/src/mind/mechanisms/confluence.js +0 -225
  88. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  89. package/dist/src/mind/mechanisms/cover.js +0 -203
  90. package/dist/src/mind/mechanisms/extraction.d.ts +0 -67
  91. package/dist/src/mind/mechanisms/extraction.js +0 -387
  92. package/dist/src/mind/mechanisms/recall.d.ts +0 -13
  93. package/dist/src/mind/mechanisms/recall.js +0 -325
  94. package/dist/src/mind/mind.d.ts +0 -276
  95. package/dist/src/mind/mind.js +0 -572
  96. package/dist/src/mind/pipeline-mechanism.d.ts +0 -142
  97. package/dist/src/mind/pipeline-mechanism.js +0 -213
  98. package/dist/src/mind/pipeline.d.ts +0 -20
  99. package/dist/src/mind/pipeline.js +0 -217
  100. package/dist/src/mind/primitives.d.ts +0 -70
  101. package/dist/src/mind/primitives.js +0 -287
  102. package/dist/src/mind/rationale.d.ts +0 -139
  103. package/dist/src/mind/rationale.js +0 -163
  104. package/dist/src/mind/reasoning.d.ts +0 -34
  105. package/dist/src/mind/reasoning.js +0 -240
  106. package/dist/src/mind/recognition.d.ts +0 -20
  107. package/dist/src/mind/recognition.js +0 -390
  108. package/dist/src/mind/resonance.d.ts +0 -23
  109. package/dist/src/mind/resonance.js +0 -0
  110. package/dist/src/mind/trace.d.ts +0 -15
  111. package/dist/src/mind/trace.js +0 -73
  112. package/dist/src/mind/traverse.d.ts +0 -113
  113. package/dist/src/mind/traverse.js +0 -568
  114. package/dist/src/mind/types.d.ts +0 -289
  115. package/dist/src/mind/types.js +0 -130
  116. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  117. package/dist/src/rabitq-ivf/src/database.js +0 -201
  118. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  119. package/dist/src/rabitq-ivf/src/index.js +0 -4
  120. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  121. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  122. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  123. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  124. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  125. package/dist/src/rabitq-ivf/src/rabitq.js +0 -283
  126. package/dist/src/sema.d.ts +0 -31
  127. package/dist/src/sema.js +0 -63
  128. package/dist/src/store-sqlite.d.ts +0 -184
  129. package/dist/src/store-sqlite.js +0 -942
  130. package/dist/src/store.d.ts +0 -663
  131. package/dist/src/store.js +0 -1618
  132. package/dist/src/vec.d.ts +0 -31
  133. 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,276 +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 type { AnchorRejectionReason, ClimbConsensusData, ConsensusAnchorTrace, ConsensusReachTrace, ConsensusRegionTrace, CrossRegionTier, JunctionVoteTrace, RegionOutcome, } from "./attention.js";
46
- export type { AncestorReach, SaturationReason, SaturationStop, } from "./types.js";
47
- export interface MindOptions {
48
- seed?: number;
49
- recallQueryK?: number;
50
- haloQueryK?: number;
51
- normalizeEpsilon?: number;
52
- cosineEpsilon?: number;
53
- geometry?: Partial<import("../config.js").GeometryConfig>;
54
- alphabet?: Partial<import("../config.js").AlphabetConfig>;
55
- storeConfig?: Partial<import("../config.js").StoreConfig>;
56
- store?: Store;
57
- /** Additional grounding mechanisms (appended after the built-in defaults). */
58
- mechanisms?: import("./pipeline-mechanism.js").PipelineMechanism[];
59
- /** Factories that receive the {@link ExtensionHost} and return mechanisms. */
60
- mechanismFactories?: ((host: import("../extension.js").ExtensionHost) => import("./pipeline-mechanism.js").PipelineMechanism)[];
61
- /** Content canonicalizer applied to EVERY response (any modality) for
62
- * equivalence-class resolution — see src/canon.ts. Text entry points
63
- * ({@link Mind.respondText}, {@link Mind.respondTurnText}) inject the
64
- * Unicode text canonicalizer automatically when this is unset; pass
65
- * `false` to disable canonical resolution everywhere. */
66
- canon?: Canon | false;
67
- }
68
- export declare class Mind implements MindContext {
69
- readonly space: Space;
70
- readonly alphabet: Alphabet;
71
- readonly store: Store;
72
- readonly cfg: MindConfig;
73
- /** The lightest-derivation engine over the Sema graph. */
74
- readonly search: GraphSearch;
75
- /** The grounding mechanisms iterated by {@link think}. */
76
- readonly mechanisms: import("./pipeline-mechanism.js").PipelineMechanism[];
77
- /** The live rationale tracer for the inference currently in flight, or null. */
78
- trace: Rationale | null;
79
- /** The content canonicalizer for the response in flight — see
80
- * {@link MindContext.canon}. Injected per response by the modality entry
81
- * point; null when the response carries no equivalence. */
82
- canon: Canon | null;
83
- /** Per-response canonical-resolution memo — see {@link MindContext.canonMemo}. */
84
- canonMemo: Map<string, number | null> | null;
85
- /** The Mind-level canon option: a canonicalizer to use for EVERY response,
86
- * `false` to disable canonical resolution, or null to let each entry
87
- * point decide (text entry points inject {@link textCanon}). */
88
- private _canonOpt;
89
- /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
90
- climbMemo: Map<string, Map<string, AttentionRead>> | null;
91
- /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
92
- recogniseMemo: Map<string, Recognition> | null;
93
- /** Memo of perceive() — content-keyed. See {@link MindContext.perceiveMemo}. */
94
- perceiveMemo: Map<string, import("../sema.js").Sema> | null;
95
- /** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
96
- _resolvedSubtrees: WeakMap<import("../sema.js").Sema, {
97
- id: number;
98
- len: number;
99
- }> | null;
100
- /** The perceived gist of the query currently being answered. Set by `think`
101
- * before the graph search runs; `chooseNext` consults it as a gate (a null
102
- * guide means no query is in flight, so structural walkers keep plain
103
- * first-edge behaviour) and the reverse projection uses it for
104
- * reverse-recall disambiguation via `chooseAmong`. */
105
- _edgeGuide: Vec | null;
106
- /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
107
- * of a single response follows the SAME continuation for each ambiguous
108
- * context node. */
109
- _edgeChoice: Map<number, number>;
110
- /** Previous deposit's seen node ids for incremental change detection. */
111
- _prevSeen: Set<number> | null;
112
- /** Session cache of node-id → perceived gist for candidate scoring — see
113
- * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
114
- * candidate sets (√N at most) fit comfortably and recur across queries. */
115
- _gistCache: BoundedMap<number, Vec>;
116
- _depositTrees: BoundedMap<string, import("./types.js").DepositCacheEntry>;
117
- _depositLens: Set<number>;
118
- _internIds: WeakMap<Sema, number>;
119
- private _nextConvId;
120
- private _conversations;
121
- /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
122
- resolve(bytes: Uint8Array): number | null;
123
- recogniseSpan(bytes: Uint8Array): {
124
- sites: ReadonlyArray<Site>;
125
- leaves: ReadonlyArray<Leaf>;
126
- splits: ReadonlySet<number>;
127
- starts: ReadonlySet<number>;
128
- };
129
- /** Disambiguate among multiple learnt continuations of the same context node.
130
- * Required by {@link GraphSearchHost} — the graph search calls this through the
131
- * host interface when a recognised form has more than one outgoing edge.
132
- * Delegates to the standalone {@link chooseNext} which picks the candidate
133
- * with the most distributional evidence (highest `prevOf` count — the
134
- * structural manifestation of its halo). When evidence is equal the
135
- * first-inserted edge wins. */
136
- chooseNext(node: number): number | undefined;
137
- constructor(opts?: MindOptions);
138
- constructor(cfg: MindConfig, store: Store, _fromStore: true);
139
- /** Exposed for tests: the consensus climb over query sub-regions. */
140
- climbAttention(query: Uint8Array, k: number, mode?: import("./types.js").DFMode): Promise<import("./types.js").Attention[]>;
141
- /** Exposed for tests: climb the structural DAG from a node to its
142
- * edge-bearing ancestor contexts. */
143
- edgeAncestors(id: number, contextCount: number): import("./types.js").AncestorReach;
144
- /** Exposed for tests: find the natural break point in a sorted vote list. */
145
- naturalBreak(votes: number[]): number;
146
- /** Perceive input into a content-defined tree. Deterministic — identical
147
- * bytes always produce an identical tree. Public for ingest-cache. */
148
- perceive(input: Input, leafAt?: (i: number) => number | null, lookup?: (ids: number[]) => number | null): Sema;
149
- /** Open one response's transient state — the tracer and the per-response
150
- * memos. Paired with {@link endResponse}; the ONE place this state is
151
- * created, so adding a memo cannot forget its reset. */
152
- private beginResponse;
153
- /** The canonicalizer a response should carry: the Mind-level option when
154
- * set (or none when explicitly disabled), else the entry point's own
155
- * default — text entry points pass {@link textCanon}, binary ones null. */
156
- private _canonFor;
157
- /** Close one response's transient state — every per-response field, incl.
158
- * the edge guide/choices `think` sets mid-flight. */
159
- private endResponse;
160
- /** Shared response core — the one path from bytes to voiced answer.
161
- * `respond` calls this directly; `respondTurn` has its own path
162
- * with conversation-persistent memos and incremental perception. */
163
- private _respondImpl;
164
- respond(input: Input, inspectRationale?: InspectRationale): Promise<Response>;
165
- /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
166
- * decoding — they are structural padding in text answers. LOSSY for a
167
- * binary answer that legitimately contains NULs: use {@link respond} and
168
- * read `bytes` directly for binary/grid modalities.
169
- *
170
- * Injects the TEXT canonicalizer (src/canon.ts) so resolution treats
171
- * every character variation of the same text — case, width, whitespace —
172
- * as one form, provided the store's canon index is built
173
- * ({@link buildCanonIndex}). */
174
- respondText(input: string, inspectRationale?: InspectRationale): Promise<string>;
175
- /** Begin a new conversation, optionally restoring from a previously-saved
176
- * {@link ConversationState}. The returned handle is required for
177
- * {@link respondTurn} and {@link endConversation}.
178
- *
179
- * Conversations are independent — a Mind can manage several concurrently.
180
- * Each tracks the fold pyramid (accumulated internal processing) and
181
- * turn-boundary offsets; the geometry never inspects content to guess
182
- * where one turn ends and the next begins. */
183
- beginConversation(state?: ConversationState): Conversation;
184
- /** End a conversation, releasing its internal resources (accumulated
185
- * context, boundary offsets, and the fold-pyramid cache). Idempotent. */
186
- endConversation(conv: Conversation): void;
187
- /** The current serialisable state of an active conversation. Save this
188
- * to resume the conversation later via {@link beginConversation}. */
189
- conversationState(conv: Conversation): ConversationState | null;
190
- /** Append a turn to a conversation's accumulated context WITHOUT
191
- * responding — raw byte append plus a boundary offset, never a
192
- * separator; the fold pyramid advances by O(turn).
193
- *
194
- * This is the primitive for turns the Mind should hear but not answer:
195
- * replaying a transcript, feeding the OTHER speaker's line in a
196
- * prediction harness, or restoring context piecewise. {@link
197
- * respondTurn} = addTurn + think + its own reply appended the same way. */
198
- addTurn(conv: Conversation, turn: Input): ConversationState;
199
- /** Grow a conversation's accumulated context by one turn's bytes — raw
200
- * append plus a boundary offset, pyramid advanced by O(turn), the grown
201
- * context's tree seeded into the conversation's perceive memo. The ONE
202
- * place a context grows ({@link addTurn} and {@link respondTurn} both
203
- * come through here), so the append semantics cannot drift. */
204
- private _growContext;
205
- /** Process one turn of a conversation.
206
- *
207
- * `turn` is the raw input for the latest turn — its bytes are appended
208
- * to the accumulated context directly (raw concatenation). The Mind
209
- * tracks the byte offset where each turn ends; no separator is ever
210
- * inserted or inspected.
211
- *
212
- * Returns the response AND the updated {@link ConversationState} so the
213
- * caller can persist it. The conversation handle's internal state is
214
- * updated in place — the returned state is a snapshot for storage.
215
- *
216
- * SINGLE FLIGHT: at most one respondTurn may be in flight per Mind. The
217
- * conversation's memo caches are swapped into the Mind-level per-response
218
- * pointers for the duration of the turn, so a concurrently-running
219
- * respond()/respondTurn() on the SAME Mind would interleave state.
220
- * Different Minds (or sequential awaits, as in every test) are safe. */
221
- respondTurn(conv: Conversation, turn: Input, inspectRationale?: InspectRationale): Promise<{
222
- response: Response;
223
- state: ConversationState;
224
- }>;
225
- /** Text view of {@link respondTurn}. See {@link respondText} for the
226
- * NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
227
- * directly — this is a text-only convenience, like {@link respondText}. */
228
- respondTurnText(conv: Conversation, turn: string, inspectRationale?: InspectRationale): Promise<{
229
- response: string;
230
- state: ConversationState;
231
- }>;
232
- embedding(input: Input): Promise<Vec | null>;
233
- /** Kinship note: the vector arm below is a miniature of recall's tier 3
234
- * (resonate → reach gate → read out the nearest form's bytes) — the
235
- * read-out direction of the same operation, without recall's grounding
236
- * ladder. If either side's acceptance rule changes, revisit the other. */
237
- express(idOrV: number | Vec): Promise<Uint8Array>;
238
- ingest(input: Input | (Input | [Input, Input])[], second?: Input): Promise<(Sema & {
239
- id: number;
240
- }) | undefined>;
241
- private extensionHost;
242
- private groundedContinuation;
243
- /** Re-index structurally-important nodes whose gists were evicted from the
244
- * pending cache before they reached the content index. See {@link
245
- * Store.repairContentIndex} for the contract; this method wires the
246
- * Mind's perception into the store's repair walk.
247
- *
248
- * Run this after training or at checkpoints to restore recall reach for
249
- * nodes that bridge experiences but were never indexed. A pure interior
250
- * node (no edges, no halo) is deliberately skipped — it is scaffolding,
251
- * not an experience root or bridge, and regenerating its gist would waste
252
- * I/O and index space for no recall benefit.
253
- *
254
- * @param minParents only repair nodes with ≥ this many structural parents
255
- * (default 2 — structural bridges)
256
- * @returns number of nodes added to the content index */
257
- repairContentIndex(minParents?: number): Promise<number>;
258
- /** Build (or incrementally refresh) the store's canonical-form index: for
259
- * every content-bearing node, record the hash of its CANONICAL key so
260
- * resolution can find stored forms across surface variation (case, width,
261
- * whitespace — whatever `canon` equates; see src/canon.ts).
262
- *
263
- * Incremental and idempotent: the last indexed node id is remembered in
264
- * store meta (`canon.upto`), so a refresh after further training scans
265
- * only the new rows. Run once after training, and again after ingests —
266
- * the same operational shape as {@link repairContentIndex}.
267
- *
268
- * @param canon the canonicalizer to index under — MUST be the same one
269
- * queries will carry (text queries carry {@link textCanon}
270
- * unless the Mind was constructed with its own)
271
- * @returns number of index rows added */
272
- buildCanonIndex(canon?: Canon): Promise<number>;
273
- save(): Promise<Uint8Array>;
274
- static load(snapshot: Uint8Array, store: Store): Promise<Mind>;
275
- static loadFromStore(store: Store): Promise<Mind>;
276
- }