@hviana/sema 0.5.3 → 0.5.4

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 (143) hide show
  1. package/AGENTS.md +6 -6
  2. package/HOW_IT_WORKS.md +16 -0
  3. package/jsr.json +1 -1
  4. package/package.json +1 -1
  5. package/src/mind/match.ts +81 -0
  6. package/src/mind/mechanisms/recall.ts +29 -3
  7. package/test/76-reference-binding.test.mjs +72 -0
  8. package/dist/example/demo.d.ts +0 -1
  9. package/dist/example/demo.js +0 -39
  10. package/dist/example/train_base.d.ts +0 -93
  11. package/dist/example/train_base.js +0 -2269
  12. package/dist/src/alphabet.d.ts +0 -7
  13. package/dist/src/alphabet.js +0 -33
  14. package/dist/src/alu/src/alu.d.ts +0 -185
  15. package/dist/src/alu/src/alu.js +0 -440
  16. package/dist/src/alu/src/expr.d.ts +0 -61
  17. package/dist/src/alu/src/expr.js +0 -318
  18. package/dist/src/alu/src/index.d.ts +0 -11
  19. package/dist/src/alu/src/index.js +0 -19
  20. package/dist/src/alu/src/kernel-arith.d.ts +0 -16
  21. package/dist/src/alu/src/kernel-arith.js +0 -264
  22. package/dist/src/alu/src/kernel-bits.d.ts +0 -19
  23. package/dist/src/alu/src/kernel-bits.js +0 -152
  24. package/dist/src/alu/src/kernel-logic.d.ts +0 -4
  25. package/dist/src/alu/src/kernel-logic.js +0 -60
  26. package/dist/src/alu/src/kernel-nd.d.ts +0 -3
  27. package/dist/src/alu/src/kernel-nd.js +0 -208
  28. package/dist/src/alu/src/kernel-numeric.d.ts +0 -54
  29. package/dist/src/alu/src/kernel-numeric.js +0 -366
  30. package/dist/src/alu/src/operation.d.ts +0 -168
  31. package/dist/src/alu/src/operation.js +0 -189
  32. package/dist/src/alu/src/parser.d.ts +0 -221
  33. package/dist/src/alu/src/parser.js +0 -577
  34. package/dist/src/alu/src/resonance.d.ts +0 -55
  35. package/dist/src/alu/src/resonance.js +0 -126
  36. package/dist/src/alu/src/text.d.ts +0 -31
  37. package/dist/src/alu/src/text.js +0 -73
  38. package/dist/src/alu/src/value.d.ts +0 -109
  39. package/dist/src/alu/src/value.js +0 -300
  40. package/dist/src/alu/test/alu.test.d.ts +0 -1
  41. package/dist/src/alu/test/alu.test.js +0 -764
  42. package/dist/src/bytes.d.ts +0 -14
  43. package/dist/src/bytes.js +0 -59
  44. package/dist/src/canon.d.ts +0 -45
  45. package/dist/src/canon.js +0 -85
  46. package/dist/src/config.d.ts +0 -111
  47. package/dist/src/config.js +0 -91
  48. package/dist/src/derive/src/deduction.d.ts +0 -136
  49. package/dist/src/derive/src/deduction.js +0 -159
  50. package/dist/src/derive/src/index.d.ts +0 -8
  51. package/dist/src/derive/src/index.js +0 -11
  52. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  53. package/dist/src/derive/src/priority-queue.js +0 -73
  54. package/dist/src/derive/src/rewrite.d.ts +0 -56
  55. package/dist/src/derive/src/rewrite.js +0 -100
  56. package/dist/src/derive/src/trie.d.ts +0 -90
  57. package/dist/src/derive/src/trie.js +0 -217
  58. package/dist/src/derive/test/derive.test.d.ts +0 -1
  59. package/dist/src/derive/test/derive.test.js +0 -122
  60. package/dist/src/extension.d.ts +0 -37
  61. package/dist/src/extension.js +0 -7
  62. package/dist/src/geometry.d.ts +0 -233
  63. package/dist/src/geometry.js +0 -1089
  64. package/dist/src/index.d.ts +0 -17
  65. package/dist/src/index.js +0 -19
  66. package/dist/src/ingest-cache.d.ts +0 -41
  67. package/dist/src/ingest-cache.js +0 -165
  68. package/dist/src/meter.d.ts +0 -176
  69. package/dist/src/meter.js +0 -274
  70. package/dist/src/mind/articulation.d.ts +0 -6
  71. package/dist/src/mind/articulation.js +0 -99
  72. package/dist/src/mind/attention.d.ts +0 -430
  73. package/dist/src/mind/attention.js +0 -2418
  74. package/dist/src/mind/bridge.d.ts +0 -37
  75. package/dist/src/mind/bridge.js +0 -907
  76. package/dist/src/mind/canonical.d.ts +0 -34
  77. package/dist/src/mind/canonical.js +0 -93
  78. package/dist/src/mind/graph-search.d.ts +0 -294
  79. package/dist/src/mind/graph-search.js +0 -996
  80. package/dist/src/mind/index.d.ts +0 -9
  81. package/dist/src/mind/index.js +0 -5
  82. package/dist/src/mind/junction.d.ts +0 -137
  83. package/dist/src/mind/junction.js +0 -358
  84. package/dist/src/mind/learning.d.ts +0 -75
  85. package/dist/src/mind/learning.js +0 -265
  86. package/dist/src/mind/match.d.ts +0 -351
  87. package/dist/src/mind/match.js +0 -959
  88. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  89. package/dist/src/mind/mechanisms/alu.js +0 -36
  90. package/dist/src/mind/mechanisms/cast.d.ts +0 -89
  91. package/dist/src/mind/mechanisms/cast.js +0 -1186
  92. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  93. package/dist/src/mind/mechanisms/confluence.js +0 -271
  94. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  95. package/dist/src/mind/mechanisms/cover.js +0 -232
  96. package/dist/src/mind/mechanisms/extraction.d.ts +0 -33
  97. package/dist/src/mind/mechanisms/extraction.js +0 -327
  98. package/dist/src/mind/mechanisms/prefix-completion.d.ts +0 -22
  99. package/dist/src/mind/mechanisms/prefix-completion.js +0 -243
  100. package/dist/src/mind/mechanisms/recall.d.ts +0 -16
  101. package/dist/src/mind/mechanisms/recall.js +0 -460
  102. package/dist/src/mind/mechanisms/reference.d.ts +0 -6
  103. package/dist/src/mind/mechanisms/reference.js +0 -296
  104. package/dist/src/mind/mind.d.ts +0 -386
  105. package/dist/src/mind/mind.js +0 -754
  106. package/dist/src/mind/pipeline-mechanism.d.ts +0 -238
  107. package/dist/src/mind/pipeline-mechanism.js +0 -622
  108. package/dist/src/mind/pipeline.d.ts +0 -49
  109. package/dist/src/mind/pipeline.js +0 -343
  110. package/dist/src/mind/primitives.d.ts +0 -85
  111. package/dist/src/mind/primitives.js +0 -333
  112. package/dist/src/mind/rationale.d.ts +0 -139
  113. package/dist/src/mind/rationale.js +0 -163
  114. package/dist/src/mind/reasoning.d.ts +0 -42
  115. package/dist/src/mind/reasoning.js +0 -305
  116. package/dist/src/mind/recognition.d.ts +0 -20
  117. package/dist/src/mind/recognition.js +0 -696
  118. package/dist/src/mind/resonance.d.ts +0 -35
  119. package/dist/src/mind/resonance.js +0 -365
  120. package/dist/src/mind/trace.d.ts +0 -15
  121. package/dist/src/mind/trace.js +0 -73
  122. package/dist/src/mind/traverse.d.ts +0 -196
  123. package/dist/src/mind/traverse.js +0 -806
  124. package/dist/src/mind/types.d.ts +0 -418
  125. package/dist/src/mind/types.js +0 -179
  126. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  127. package/dist/src/rabitq-ivf/src/database.js +0 -201
  128. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  129. package/dist/src/rabitq-ivf/src/index.js +0 -4
  130. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  131. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  132. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  133. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  134. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  135. package/dist/src/rabitq-ivf/src/rabitq.js +0 -313
  136. package/dist/src/sema.d.ts +0 -41
  137. package/dist/src/sema.js +0 -77
  138. package/dist/src/store-sqlite.d.ts +0 -184
  139. package/dist/src/store-sqlite.js +0 -942
  140. package/dist/src/store.d.ts +0 -762
  141. package/dist/src/store.js +0 -1823
  142. package/dist/src/vec.d.ts +0 -31
  143. package/dist/src/vec.js +0 -109
@@ -1,959 +0,0 @@
1
- // match.ts — the ONE elementary operation behind every generalising mechanism:
2
- // MATCH a learned structure against bytes, then PROJECT along a learned
3
- // relation, gated by a derived threshold.
4
- //
5
- // Every grounding/generalisation mechanism in the mind is a configuration of
6
- // this single (matcher, direction, gate) operation:
7
- //
8
- // mechanism matcher direction gate
9
- // ─────────────────── ────────────────────────────── ───────────── ────────────────
10
- // cover follow-edge exact (content-addressed) forward —
11
- // concept hop halo sibling forward conceptThreshold
12
- // recall tier 0–1 identity / whole-query gist fwd/reverse identityBar
13
- // skill extraction locate() ladder (exact→halo→ read-out per-step gates
14
- // gist) on the exemplar's frames
15
- // CAST substitution alignGraded() (graded ladder: insert frame shapes
16
- // literal W-grams → halo sites)
17
- // CAST comparison analogyStrength() (halo, juxtapose significanceBar
18
- // direct or mutual-sibling)
19
- // multi-hop pivot byte containment forward —
20
- // articulation halo sibling substitute conceptThreshold
21
- // reference frameSlots() (the shared carry into carriesFillers
22
- // aligner, gaps contracted) the answer
23
- //
24
- // This module holds the shared vocabulary those configurations are built
25
- // from — the MATCHERS (locate, alignRuns, alignGraded, alignAround/frameSlots,
26
- // analogyStrength) and the PROJECTIONS (follow, conceptHop, reverseContext,
27
- // project) — so each mechanism file states only its configuration, never its
28
- // own copy of the machinery. Most gates live in geometry.ts (derived, never
29
- // tuned); the two STRUCTURAL gates that are byte predicates rather than
30
- // thresholds — isSpanShaped and carriesFillers — live here beside the matchers
31
- // they gate.
32
- import { addInto, cosine, dot, normalize, zeros } from "../vec.js";
33
- import { conceptThreshold, identityBar, significanceBar, } from "../geometry.js";
34
- import { bytesEqual, indexOf } from "../bytes.js";
35
- import { chainReach, leafIdRun } from "./canonical.js";
36
- import { foldTree, gistOf, perceive, read, resolve } from "./primitives.js";
37
- import { argmaxCosine, chooseAmong, chooseNext, corpusN, edgeAncestors, guidedFirst, hubBound, hubCap, sharedReachMemo, } from "./traverse.js";
38
- import { recognise, segment } from "./recognition.js";
39
- // ═══════════════════════════════════════════════════════════════════════════
40
- // MATCHERS — locating learned structure in/against bytes, by graded strictness
41
- // ═══════════════════════════════════════════════════════════════════════════
42
- /** The graded LOCATE ladder: find `needle` in `haystack` starting at
43
- * `fromPos`, strictest matcher first, relaxing only when the stricter one
44
- * fails. This is the read-out matcher skill extraction locates exemplar
45
- * frames with.
46
- *
47
- * 1. exact — literal byte match (the fast path).
48
- * 2. halo — the needle's distributional role matches a recognised query
49
- * form (gate: conceptThreshold).
50
- * 3. gist — the needle's perceived gist matches a query segment
51
- * (gate: identityBar — scale-aware).
52
- *
53
- * Returns the absolute byte position, or −1. */
54
- export function locate(ctx, haystack, needle, fromPos, sites) {
55
- // 1. Exact match — fast, preserves backward compatibility.
56
- const exact = indexOf(haystack.subarray(fromPos), needle, 0);
57
- if (exact >= 0)
58
- return fromPos + exact;
59
- // 2. Halo-based: the frame bytes' distributional role matches a query form.
60
- if (sites && sites.length > 0) {
61
- const frameId = resolve(ctx, needle);
62
- if (frameId !== null) {
63
- const frameHalo = ctx.store.halo(frameId);
64
- if (frameHalo) {
65
- const bestSite = bestHaloMate(ctx, frameHalo, sites.filter((s) => s.start >= fromPos), (s) => ctx.store.halo(s.payload));
66
- if (bestSite !== null)
67
- return bestSite.item.start;
68
- }
69
- }
70
- }
71
- // 3. Gist resonance: the frame's perceived gist against query segments.
72
- const frameGist = gistOf(ctx, needle);
73
- const segments = segment(ctx, haystack.subarray(fromPos));
74
- // The gist tier claims the WHOLE needle appears as a segment — an
75
- // identity claim over `needle.length` bytes, so its bar is the
76
- // scale-aware {@link identityBar} (one river window of tolerated foreign
77
- // bytes), not the fixed estimator floor. For quantum-sized frames the
78
- // two coincide; for long needles the fixed bar accepted segments that
79
- // differed by whole windows.
80
- const bestSeg = argmaxCosine(frameGist, segments, (s) => s.v, identityBar(ctx.store.D, ctx.space.maxGroup, needle.length), true);
81
- if (bestSeg !== null)
82
- return fromPos + bestSeg.item.start;
83
- return -1;
84
- }
85
- /** The ALIGNED matcher: maximal literal matching runs between `query` and
86
- * `ct` (a learned context's bytes), by seed-and-extend over
87
- * `space.maxGroup`-sized n-gram seeds. Where locate() finds ONE position of
88
- * a short frame, this finds EVERY run two whole structures share — the
89
- * matcher CAST detects a woven query with. Returns non-overlapping runs
90
- * sorted by query position. */
91
- export function alignRuns(ctx, query, ct) {
92
- if (ctx.meter) {
93
- ctx.meter.alignments++;
94
- // The alignment family's honest unit: the seed index is O(|query|) but
95
- // the run extension is O(|query|·|ct|) in the worst case, and a weave
96
- // that starts scanning conversation-length contexts shows up HERE long
97
- // before it shows up in a call count.
98
- ctx.meter.alignCells += query.length * ct.length;
99
- }
100
- // MEASURED AND REFUTED — seeding at the write side's unit floor W−1 instead
101
- // of W. `canonicalWindows` interns both lengths precisely so a form
102
- // straddling a group boundary is reachable from either cut, and the runs
103
- // found here are exactly such remnants: test/29 C1's query shares only `ce `
104
- // with `Ice is cold` — three bytes, never seeded at W, so that structure
105
- // enters the weave carrying nothing but the scaffolding run every exemplar
106
- // shares. But this is a byte MATCHER between two streams, not an index, and
107
- // at W−1 the corpus is dense with spurious 3-byte agreements: the extra runs
108
- // reshuffle which point claims which span, and test/29 A2 loses its analog.
109
- // C1 does not pass either way. The store's unit floor does not transfer to
110
- // the aligner's seed length.
111
- const quantum = Math.min(ctx.space.maxGroup, ct.length);
112
- if (quantum < 1 || query.length < quantum)
113
- return [];
114
- const gram = (b, at) => {
115
- let s = "";
116
- for (let i = 0; i < quantum; i++)
117
- s += String.fromCharCode(b[at + i]);
118
- return s;
119
- };
120
- const seeds = new Map();
121
- for (let i = 0; i + quantum <= query.length; i++) {
122
- const k2 = gram(query, i);
123
- const bucket = seeds.get(k2);
124
- if (bucket === undefined)
125
- seeds.set(k2, [i]);
126
- else
127
- bucket.push(i);
128
- }
129
- const found = [];
130
- for (let j = 0; j + quantum <= ct.length; j++) {
131
- const bucket = seeds.get(gram(ct, j));
132
- if (bucket === undefined)
133
- continue;
134
- for (const i of bucket) {
135
- if (i > 0 && j > 0 && query[i - 1] === ct[j - 1])
136
- continue;
137
- let len = quantum;
138
- while (i + len < query.length && j + len < ct.length &&
139
- query[i + len] === ct[j + len])
140
- len++;
141
- found.push({ qs: i, qe: i + len, cs: j, len });
142
- }
143
- }
144
- found.sort((a, b) => b.len - a.len);
145
- const runs = [];
146
- for (const r of found) {
147
- const clash = runs.some((o) => (r.qs < o.qe && o.qs < r.qe) ||
148
- (r.cs < o.cs + (o.qe - o.qs) && o.cs < r.cs + r.len));
149
- if (!clash)
150
- runs.push({ qs: r.qs, qe: r.qe, cs: r.cs });
151
- }
152
- return runs.sort((a, b) => a.qs - b.qs);
153
- }
154
- /** The GRADED alignment matcher: extends literal W-gram alignment
155
- * ({@link alignRuns}) with halo-matched recognised sites in query regions
156
- * that have no literal coverage. Same ladder as {@link locate}: literal
157
- * first, then distributional role (halo-matched sites, gate:
158
- * conceptThreshold, enforced by {@link bestHaloMate}). Returns weighted
159
- * runs sorted by query position.
160
- *
161
- * `querySites` are the pre-computed recognition sites for the query
162
- * (optional — when absent, only literal alignment fires and graded degrades
163
- * to the original behaviour). Context sites are recognised internally. */
164
- export function alignGraded(ctx, query, contextBytes, querySites) {
165
- const lit = alignRuns(ctx, query, contextBytes);
166
- const out = lit.map((r) => ({ ...r, weight: 1 }));
167
- if (!querySites || querySites.length === 0)
168
- return out;
169
- // Mark query positions ALREADY covered by literal runs — halo fills gaps.
170
- // If literal coverage is already complete, skip the halo step entirely
171
- // (recognise is O(|ctx|·W) — wasted when every byte is accounted for).
172
- const covered = new Uint8Array(query.length);
173
- let gaps = false;
174
- for (const r of lit) {
175
- for (let i = r.qs; i < r.qe; i++)
176
- covered[i] = 1;
177
- }
178
- for (let i = 0; i < query.length; i++) {
179
- if (!covered[i]) {
180
- gaps = true;
181
- break;
182
- }
183
- }
184
- if (!gaps)
185
- return out;
186
- // Recognise sites in the exemplar context — structural positions for halo
187
- // matching. (Circular import with recognition.ts is safe: recognise() is
188
- // called lazily, never at module load — the same pattern `segment` uses.)
189
- const ctxSites = recognise(ctx, contextBytes).sites;
190
- if (ctxSites.length === 0)
191
- return out;
192
- // Context sites with halos, hoisted: the same set serves every query site.
193
- const ctxCands = ctxSites.filter((cs) => ctx.store.hasHalo(cs.payload));
194
- if (ctxCands.length === 0)
195
- return out;
196
- // Candidate halos, also hoisted (lazily, first query site that needs them):
197
- // bestHaloMate consults every candidate's halo PER QUERY SITE, and sites
198
- // share the candidate set — without this memo the same few dozen halos were
199
- // re-fetched thousands of times per response. Distinct payloads can repeat
200
- // across sites, hence the map by payload id.
201
- const ctxHalos = new Map();
202
- const ctxHaloOf = (cs) => {
203
- let h = ctxHalos.get(cs.payload);
204
- if (h === undefined) {
205
- h = ctx.store.halo(cs.payload);
206
- ctxHalos.set(cs.payload, h);
207
- }
208
- return h;
209
- };
210
- for (const qs of querySites) {
211
- // Only sites that overlap UNCOVERED query regions add new evidence.
212
- let touchesGap = false;
213
- for (let i = qs.start; i < qs.end; i++) {
214
- if (!covered[i]) {
215
- touchesGap = true;
216
- break;
217
- }
218
- }
219
- if (!touchesGap)
220
- continue;
221
- const qHalo = ctx.store.halo(qs.payload);
222
- if (!qHalo)
223
- continue;
224
- // bestHaloMate already gates at conceptThreshold — no second check needed.
225
- const match = bestHaloMate(ctx, qHalo, ctxCands, ctxHaloOf);
226
- if (match === null)
227
- continue;
228
- out.push({
229
- qs: qs.start,
230
- qe: qs.end,
231
- cs: match.item.start,
232
- weight: match.score,
233
- });
234
- }
235
- out.sort((a, b) => a.qs - b.qs);
236
- return out;
237
- }
238
- /** Extend a seed match (query offset qo ↔ candidate offset co) to its maximal
239
- * common run, then walk outward in both directions collecting further common
240
- * runs of at least W bytes across bounded mismatch gaps (each side ≤
241
- * chainReach). Returns the matched query spans and the mismatch pairs
242
- * between consecutive runs.
243
- *
244
- * This is the SEEDED aligner, distinct from {@link alignRuns}: that one finds
245
- * every run two structures share anywhere (a weave), this one reads two
246
- * streams as ONE structure that diverges in bounded places (a frame with
247
- * slots).
248
- *
249
- * Gaps come back in SWEEP order (right sweep, then left), not query order,
250
- * and only the INTERIOR ones are reported — a consumer that needs the query's
251
- * unmatched head or tail derives it from `matched`. Both are the bridge's
252
- * contract, which prices its edges separately (see its matchStart/matchEnd
253
- * window test); {@link frameSlots} takes the other reading. */
254
- export function alignAround(ctx, q, c, qo, co) {
255
- const W = ctx.space.maxGroup;
256
- const reachCap = chainReach(W);
257
- // Maximal run around the seed.
258
- let qs = qo, ss = co;
259
- while (qs > 0 && ss > 0 && q[qs - 1] === c[ss - 1]) {
260
- qs--;
261
- ss--;
262
- }
263
- let qe = qo, se = co;
264
- while (qe < q.length && se < c.length && q[qe] === c[se]) {
265
- qe++;
266
- se++;
267
- }
268
- const matched = [[qs, qe]];
269
- const gaps = [];
270
- // The next common run of ≥ W bytes past (qi, si), with each side's gap
271
- // bounded by chainReach; smallest total gap wins (nearest continuation).
272
- const runLenAt = (qi, si) => {
273
- let n = 0;
274
- while (qi + n < q.length && si + n < c.length && q[qi + n] === c[si + n]) {
275
- n++;
276
- }
277
- return n;
278
- };
279
- // RIGHT sweep.
280
- let qi = qe, si = se;
281
- for (;;) {
282
- let found = false;
283
- for (let total = 1; total <= 2 * reachCap && !found; total++) {
284
- for (let gq = 0; gq <= Math.min(total, reachCap); gq++) {
285
- const gs = total - gq;
286
- if (gs > reachCap)
287
- continue;
288
- if (qi + gq >= q.length || si + gs >= c.length)
289
- continue;
290
- const n = runLenAt(qi + gq, si + gs);
291
- if (n >= W || qi + gq + n === q.length) {
292
- if (n === 0)
293
- continue;
294
- if (gq > 0 || gs > 0) {
295
- gaps.push({ qs: qi, qe: qi + gq, cs: si, ce: si + gs });
296
- }
297
- matched.push([qi + gq, qi + gq + n]);
298
- qi = qi + gq + n;
299
- si = si + gs + n;
300
- found = true;
301
- break;
302
- }
303
- }
304
- }
305
- if (!found)
306
- break;
307
- }
308
- // LEFT sweep (mirror).
309
- qi = qs;
310
- si = ss;
311
- for (;;) {
312
- let found = false;
313
- for (let total = 1; total <= 2 * reachCap && !found; total++) {
314
- for (let gq = 0; gq <= Math.min(total, reachCap); gq++) {
315
- const gs = total - gq;
316
- if (gs > reachCap)
317
- continue;
318
- if (qi - gq <= 0 || si - gs <= 0)
319
- continue;
320
- // Run ENDING at (qi - gq, si - gs).
321
- let n = 0;
322
- while (n < qi - gq && n < si - gs &&
323
- q[qi - gq - 1 - n] === c[si - gs - 1 - n]) {
324
- n++;
325
- }
326
- if (n >= W || n === qi - gq) {
327
- if (n === 0)
328
- continue;
329
- if (gq > 0 || gs > 0) {
330
- gaps.push({ qs: qi - gq, qe: qi, cs: si - gs, ce: si });
331
- }
332
- matched.push([qi - gq - n, qi - gq]);
333
- qi = qi - gq - n;
334
- si = si - gs - n;
335
- found = true;
336
- break;
337
- }
338
- }
339
- }
340
- if (!found)
341
- break;
342
- }
343
- return { matched, gaps };
344
- }
345
- /** Contract a gap to its VARYING CORE: strip the prefix and suffix the two
346
- * sides share. {@link alignAround} cannot match a shared affix shorter than
347
- * W, so that affix lands INSIDE the gap — measured, the slot of
348
- * `How do I compile main.c?` against `…hello.c?` comes back as
349
- * `main.c?`/`hello.c?`, three bytes of which (`.c?`) both sides hold.
350
- *
351
- * Splicing the uncontracted gap carries the query's own punctuation into the
352
- * answer; worse, it hides what actually VARIES, which is the only thing a
353
- * cohort can agree about. Returns null when nothing is left on either side —
354
- * a pure insertion or deletion, which names no slot. */
355
- export function contractGap(q, c, g) {
356
- let { qs, qe, cs, ce } = g;
357
- while (qs < qe && cs < ce && q[qs] === c[cs]) {
358
- qs++;
359
- cs++;
360
- }
361
- while (qe > qs && ce > cs && q[qe - 1] === c[ce - 1]) {
362
- qe--;
363
- ce--;
364
- }
365
- return qe > qs && ce > cs ? { qs, qe, cs, ce } : null;
366
- }
367
- /** THE SLOT MATCHER: read one query ↔ context pairing as one structure with
368
- * variable positions.
369
- *
370
- * IT REPORTS; IT DOES NOT JUDGE. This returns every gap the aligner found,
371
- * contracted to its varying core and tagged with its kind, plus the shared
372
- * coverage — and rejects nothing. That is the whole point of the split, and
373
- * it was got WRONG first: four VOICING gates (the frame must dominate the
374
- * query, each slot must reach one window on both sides, an insertion or
375
- * deletion disqualifies the pairing, fillers must be pairwise distinct) were
376
- * applied here, and every one of them is a requirement for SUBSTITUTING AND
377
- * SPEAKING, not for knowing where a pairing varies. With them in place the
378
- * shared reading was reference-shaped: measured over four real pairings, three
379
- * were hidden from every consumer —
380
- *
381
- * `What is the capital of the country where the Eiffel Tower is?`
382
- * against `What is the capital of France?` (covered 23/61) HIDDEN
383
- * `What is the capital of France, really?` (an insertion) HIDDEN
384
- * `What is the capital of Fran?` (sub-window) HIDDEN
385
- *
386
- * — including the case of the one consumer that most obviously needed it. A
387
- * shared layer with one usable consumer is private code at a public address.
388
- * Each gate now lives with the mechanism that needs it (see reference.ts).
389
- *
390
- * Seeded at the origin, because a frame is shared structure the query and its
391
- * instances both OPEN with: the maximal run around (0,0) is the frame's head
392
- * and the sweeps find the rest.
393
- *
394
- * Null only for a degenerate pairing (either side empty). */
395
- export function frameSlots(ctx, query, cand, id) {
396
- if (query.length === 0 || cand.length === 0)
397
- return null;
398
- const { matched, gaps } = alignAround(ctx, query, cand, 0, 0);
399
- const spans = [...matched].sort((a, b) => a[0] - b[0]);
400
- // Where the alignment RAN OUT on each side. Seeded at the origin there is
401
- // no leading gap, so both cursors are everything consumed so far: the
402
- // matched runs (equal length on both sides by construction) plus what each
403
- // interior gap ate of its own side. Counting only the runs reads the
404
- // candidate cursor short by exactly the fillers already seen, and invents a
405
- // trailing gap on every well-aligned instance.
406
- const all = [...gaps];
407
- let qEnd = 0, cEnd = 0;
408
- for (const [s, e] of spans) {
409
- cEnd += e - s;
410
- qEnd = Math.max(qEnd, e);
411
- }
412
- for (const g of gaps)
413
- cEnd += g.ce - g.cs;
414
- if (qEnd < query.length || cEnd < cand.length) {
415
- all.push({ qs: qEnd, qe: query.length, cs: cEnd, ce: cand.length });
416
- }
417
- const slots = [];
418
- for (const gap of all.sort((a, b) => a.qs - b.qs)) {
419
- if (gap.qe <= gap.qs && gap.ce <= gap.cs)
420
- continue;
421
- // Contract to the varying core. contractGap returns null when one side is
422
- // wholly shared with the other — a pure insertion or deletion, which is a
423
- // real variation and is reported AS ONE, not discarded.
424
- const core = contractGap(query, cand, gap);
425
- const g = core ?? gap;
426
- const kind = g.qe > g.qs && g.ce > g.cs
427
- ? "substitution"
428
- : g.qe > g.qs
429
- ? "insertion"
430
- : "deletion";
431
- slots.push({
432
- qs: g.qs,
433
- qe: g.qe,
434
- cs: g.cs,
435
- ce: g.ce,
436
- kind,
437
- filler: cand.slice(g.cs, g.ce),
438
- });
439
- }
440
- const covered = spans.reduce((n, [s, e]) => n + e - s, 0);
441
- return { id, slots, matched: spans, covered };
442
- }
443
- /** Whether every member is byte-distinct from the others. */
444
- export function distinct(items) {
445
- for (let i = 0; i < items.length; i++) {
446
- for (let j = i + 1; j < items.length; j++) {
447
- if (bytesEqual(items[i], items[j]))
448
- return false;
449
- }
450
- }
451
- return true;
452
- }
453
- /** Substitute every `needle -> repl` pair SIMULTANEOUSLY: one left-to-right
454
- * pass, longest needle first at each position, and a replacement is never
455
- * re-examined.
456
- *
457
- * SIMULTANEOUS IS NOT A DETAIL. Applying the pairs in sequence lets one
458
- * substitution's OUTPUT be another's input: with slots `gcc -> zig` and
459
- * `hello.c -> zig.c` a sequential pass rewrites bytes it had just written,
460
- * and the result depends on the order the slots happened to be found in.
461
- * Longest-first at each position makes the pass independent of pair order,
462
- * which is what keeps {@link carriesFillers} and the binding it licenses the
463
- * SAME operation — if they could disagree, the licence would not be testing
464
- * what is voiced. */
465
- export function substituteAll(hay, pairs) {
466
- const usable = pairs.filter((p) => p.needle.length > 0);
467
- if (usable.length === 0)
468
- return hay;
469
- // Longest needle first, so a needle that is a prefix of another can never
470
- // pre-empt it. Ties cannot arise: an instance whose fillers are not
471
- // pairwise distinct is refused by frameSlots.
472
- const order = [...usable].sort((a, b) => b.needle.length - a.needle.length);
473
- const out = [];
474
- let i = 0;
475
- let hit = false;
476
- outer: while (i < hay.length) {
477
- for (const p of order) {
478
- if (i + p.needle.length > hay.length)
479
- continue;
480
- let k = 0;
481
- while (k < p.needle.length && hay[i + k] === p.needle[k])
482
- k++;
483
- if (k < p.needle.length)
484
- continue;
485
- for (const b of p.repl)
486
- out.push(b);
487
- i += p.needle.length;
488
- hit = true;
489
- continue outer;
490
- }
491
- out.push(hay[i]);
492
- i++;
493
- }
494
- return hit ? Uint8Array.from(out) : hay;
495
- }
496
- /** THE CARRIAGE LICENCE — the gate that decides whether a slot may be VOICED
497
- * through. Given two instances of one frame and what each one continues to,
498
- * it asks one byte question:
499
- *
500
- * substituteAll(contA, fillersA -> fillersB) == contB
501
- *
502
- * When it holds, the corpus attests byte-exactly that the continuation is a
503
- * function of the fillers and nothing else, so putting a NEW occupant through
504
- * the same carriage is derivation rather than invention. No threshold, no
505
- * similarity, no new constant: the store's own instances decide, exactly as
506
- * the bridge's `unanimous` decides whether a frame is a value slot.
507
- *
508
- * Its FAILURE is what this is really for. A frame whose continuation carries
509
- * filler-DEPENDENT content — `What is the capital of X?` answering a different
510
- * city per X — fails it, and that failure is the only thing between a slot
511
- * and an invented fact. Measured on the trained 15.7M-node store (325,615
512
- * contexts): `What is the capital of Zamunda?` resonates to a PURE cohort,
513
- * every one of the top 14 hits an instance of that frame, with an unambiguous
514
- * slot; every structural gate passes and only this one refuses, on
515
- * `replace("Tokyo", "Japan" -> "France") != "Paris"`.
516
- *
517
- * With SEVERAL slots the test is unchanged, which is the point of testing the
518
- * whole substitution at once: a frame whose answer tracks one slot but
519
- * invents around another fails exactly as a single-slot value slot does. */
520
- export function carriesFillers(contA, fillersA, contB, fillersB) {
521
- if (fillersA.length !== fillersB.length)
522
- return false;
523
- const projected = substituteAll(contA, fillersA.map((needle, s) => ({ needle, repl: fillersB[s] })));
524
- return bytesEqual(projected, contB);
525
- }
526
- /** The IN-LIST halo matcher: the best halo-mate for `halo` among EXPLICIT
527
- * candidates, above the concept threshold — the list counterpart of
528
- * {@link haloSiblings}, which asks the halo INDEX for candidates instead.
529
- * Behind locate()'s halo step and articulation's voice matching; a third
530
- * "best halo among these" decision must come here, not inline. */
531
- export function bestHaloMate(ctx, halo, items, haloOf) {
532
- return argmaxCosine(halo, items, haloOf, conceptThreshold(ctx.store.D));
533
- }
534
- /** The HALO-SIBLING matcher: the nodes that keep the same distributional
535
- * company as `id`, nearest first — `resonateHalo` filtered to exclude the
536
- * node itself and everything below `bar` (default: the concept threshold).
537
- * `halo`, when the caller has already read the node's halo row, is reused
538
- * instead of refetched (one read per relation). Returns [] for a node with
539
- * no halo. The one sibling enumeration behind the concept hop, the
540
- * reasoning stage's synonym expansion, and the analogy matcher below. */
541
- const haloSiblingMemo = new WeakMap();
542
- export async function haloSiblings(ctx, id, halo, bar = conceptThreshold(ctx.store.D)) {
543
- // Per-response memo for the DEFAULT-ARGUMENT reading (the one the concept
544
- // hop, the bridge's synonym tier, and reasoning's synonym expansion all
545
- // use): the same node's siblings are asked for repeatedly within one
546
- // response (bridge pairs share sides), each a full halo-ANN query, and the
547
- // store is read-only while a response is in flight. Keyed by the response
548
- // lifecycle object (ctx.climbMemo — fresh per respond, nulled after).
549
- // Calls with an explicit halo or bar (analogyStrength's gated reading)
550
- // bypass the memo — their filter differs.
551
- const memoable = halo === undefined &&
552
- bar === conceptThreshold(ctx.store.D) && ctx.climbMemo !== null;
553
- let memo;
554
- if (memoable) {
555
- memo = haloSiblingMemo.get(ctx.climbMemo);
556
- if (memo === undefined) {
557
- haloSiblingMemo.set(ctx.climbMemo, memo = new Map());
558
- }
559
- const hit = memo.get(id);
560
- if (hit !== undefined)
561
- return hit;
562
- }
563
- const h = halo ?? ctx.store.halo(id);
564
- const out = h
565
- ? (await ctx.store.resonateHalo(h, ctx.cfg.haloQueryK))
566
- .filter((sib) => sib.id !== id && sib.score >= bar)
567
- : [];
568
- if (memo !== undefined)
569
- memo.set(id, out);
570
- return out;
571
- }
572
- /** Bundle the distributional company of every addressable W-window in a
573
- * byte span. This is the query-time counterpart of the write-side halo
574
- * pours: no lexical unit or storage row is invented; the span is represented
575
- * by VSA superposition of the window concepts the store already knows.
576
- *
577
- * Components are normalized before bundling so repetition mass remains
578
- * evidence about each stored node, not an accidental weight on one window
579
- * inside the composed phrase. Returns null when the corpus provides no
580
- * distributional evidence for the span. */
581
- export function spanHalo(ctx, bytes, from = 0, to = bytes.length) {
582
- const W = ctx.space.maxGroup;
583
- if (to - from < W)
584
- return null;
585
- if (ctx.meter)
586
- ctx.meter.spanHalos++;
587
- const out = zeros(ctx.store.D);
588
- let found = false;
589
- const added = new Set();
590
- const episodeRoots = [];
591
- const N = corpusN(ctx);
592
- const reachMemo = sharedReachMemo(ctx);
593
- const addHalo = (id) => {
594
- if (added.has(id))
595
- return;
596
- const halo = ctx.store.halo(id);
597
- if (halo === null)
598
- return;
599
- const norm = Math.sqrt(dot(halo, halo));
600
- if (norm === 0)
601
- return;
602
- added.add(id);
603
- addInto(out, halo, 1 / norm);
604
- found = true;
605
- };
606
- const windowCount = to - from - W + 1;
607
- const offsets = [];
608
- const samples = Math.min(W, windowCount);
609
- for (let i = 0; i < samples; i++) {
610
- const relative = samples === 1
611
- ? 0
612
- : Math.floor((i * (windowCount - 1)) / (samples - 1));
613
- const off = from + relative;
614
- if (offsets[offsets.length - 1] !== off)
615
- offsets.push(off);
616
- }
617
- for (const off of offsets) {
618
- if (ctx.meter)
619
- ctx.meter.spanHaloWindows++;
620
- const ids = leafIdRun(ctx, bytes, off, off + W);
621
- if (ids === null)
622
- continue;
623
- const id = ctx.store.findBranch(ids);
624
- if (id === null)
625
- continue;
626
- addHalo(id);
627
- // Canonical flat windows are retrieval addresses and normally carry no
628
- // halo themselves. Their bounded structural ascent reaches the learned
629
- // episode forms that contain them; bundling those forms' company is the
630
- // distributional meaning of the window, derived entirely from existing
631
- // containment and halo state.
632
- if (!added.has(id)) {
633
- episodeRoots.push(edgeAncestors(ctx, id, N, reachMemo).roots);
634
- }
635
- }
636
- for (let rank = 0; added.size < ctx.cfg.haloQueryK; rank++) {
637
- let any = false;
638
- for (const roots of episodeRoots) {
639
- if (rank >= roots.length)
640
- continue;
641
- any = true;
642
- addHalo(roots[rank]);
643
- if (added.size >= ctx.cfg.haloQueryK)
644
- break;
645
- }
646
- if (!any)
647
- break;
648
- }
649
- return found ? normalize(out) : null;
650
- }
651
- /** Distributional synonym evidence between arbitrary byte spans. Whole words
652
- * need not be independently interned: their stored W-window occurrences are
653
- * lifted to episode halos, bundled, and compared. The caller chooses the
654
- * derived gate appropriate to its claim (concept identity or analogy). */
655
- export function spanSynonymStrength(ctx, a, b) {
656
- const ah = spanHalo(ctx, a);
657
- const bh = spanHalo(ctx, b);
658
- if (ah === null || bh === null)
659
- return 0;
660
- return cosine(ah, bh);
661
- }
662
- export async function analogyStrength(ctx, a, b) {
663
- const ha = ctx.store.halo(a);
664
- const hb = ctx.store.halo(b);
665
- if (ha && hb) {
666
- const bar = significanceBar(ctx.store.D);
667
- const direct = cosine(ha, hb);
668
- if (direct >= bar)
669
- return { score: direct, halo: true };
670
- const sibsA = await haloSiblings(ctx, a, ha, bar);
671
- const sibsB = await haloSiblings(ctx, b, hb, bar);
672
- let best = 0;
673
- for (const x of sibsA) {
674
- if (x.id === b)
675
- continue;
676
- const y = sibsB.find((s) => s.id === x.id);
677
- if (y !== undefined) {
678
- best = Math.max(best, Math.min(x.score, y.score));
679
- }
680
- }
681
- if (best > 0)
682
- return { score: best, halo: true };
683
- }
684
- return { score: sharedFrameStrength(ctx, a, b), halo: false };
685
- }
686
- /** The STRUCTURAL analogy tier: two nodes are analogs when their byte
687
- * streams share a LEARNT frame — a content-addressed flat form of at least
688
- * one full river window (W bytes, the perception quantum) that occurs in
689
- * BOTH. This is what "playing the same role" means structurally: "Ice is
690
- * cold" and "Steel is hard" share the learnt " is " frame even though they
691
- * keep disjoint distributional company. Halos measure company by IDENTITY
692
- * (company signatures — see sema.ts), so unrelated-company analogs must be
693
- * validated by the frame itself, not by content leaking through halo
694
- * vectors. Strength is the shared learnt coverage of the SHORTER side —
695
- * a fraction, comparable to the cosine tiers above. Derived: the window
696
- * is maxGroup, the same quantum differsByOneWindow and canonicalChunkId
697
- * measure by; no tuned constants. */
698
- export function sharedFrameStrength(ctx, a, b) {
699
- return sharedFrameStrengthOf(ctx, read(ctx, a), read(ctx, b));
700
- }
701
- /** The same measure over BYTES, for callers holding a role-establishing
702
- * CONTEXT rather than the node whose role it establishes — CAST's comparison
703
- * reads the tier this way when two candidate analogs are fillers (bare entity
704
- * names) rather than frame-bearing structures themselves. A role is a
705
- * property of the context that establishes a filler, never of the filler's
706
- * own bytes: measured on test/29's corpus, "Michelangelo" against "Homer"
707
- * reads 0.000 while their establishing contexts ("The David was sculpted
708
- * by…" against "The Iliad was written by…") read 0.452, and a context in a
709
- * genuinely different frame ("Water boils at…") still reads 0.000 — the tier
710
- * discriminates, it was simply being asked about the wrong bytes. */
711
- export function sharedFrameStrengthOf(ctx, A, B) {
712
- const W = ctx.space.maxGroup;
713
- if (A.length < W || B.length < W)
714
- return 0;
715
- // Mark every byte of the shorter side covered by a learnt W-window that
716
- // also occurs in the longer side.
717
- const [s, l] = A.length <= B.length ? [A, B] : [B, A];
718
- const covered = new Uint8Array(s.length);
719
- for (let off = 0; off + W <= s.length; off++) {
720
- const win = s.subarray(off, off + W);
721
- // Learnt: the window resolves as a content-addressed flat form.
722
- const ids = leafIdRun(ctx, s, off, off + W);
723
- if (ids === null || ctx.store.findBranch(ids) === null)
724
- continue;
725
- if (indexOf(l, win, 0) < 0)
726
- continue;
727
- covered.fill(1, off, off + W);
728
- }
729
- let n = 0;
730
- for (let i = 0; i < s.length; i++)
731
- n += covered[i];
732
- return n >= W ? n / s.length : 0;
733
- }
734
- // ═══════════════════════════════════════════════════════════════════════════
735
- // PROJECTIONS — what a matched node is projected ALONG (the direction)
736
- // ═══════════════════════════════════════════════════════════════════════════
737
- /** FORWARD through a synonym: the continuation an edge-less node borrows from
738
- * a concept (halo) sibling — resonate the node's halo, take the first
739
- * sibling above the concept threshold that itself has a direct edge. */
740
- export async function conceptHop(ctx, id) {
741
- for (const sib of await haloSiblings(ctx, id)) {
742
- const hop = guidedFirst(ctx, sib.id);
743
- if (hop !== undefined)
744
- return hop;
745
- }
746
- return null;
747
- }
748
- /** FORWARD projection: follow continuation edges from a node to its fixpoint.
749
- * The first hop may cross a concept (halo) link — a synonym. The rest
750
- * follow direct edges. Convergence is intrinsic: the seen set guards
751
- * against cycles. `guide` disambiguates multi-continuation nodes by
752
- * resonance. */
753
- export async function follow(ctx, id, guide) {
754
- const seen = new Set([id]);
755
- // First hop: a direct edge, else a concept sibling's edge (the synonym).
756
- let next = chooseNext(ctx, id, guide);
757
- if (next === undefined) {
758
- const hop = await conceptHop(ctx, id);
759
- if (hop === null)
760
- return null;
761
- next = hop;
762
- }
763
- // Direct successors to the fixpoint. Only the FIXPOINT's bytes are
764
- // returned, so the walk tracks node ids and reads bytes exactly once at
765
- // the end — a K-hop chain used to pay K full reconstructions and discard
766
- // K−1 of them.
767
- while (!seen.has(next)) {
768
- seen.add(next);
769
- const fwd = chooseNext(ctx, next, guide);
770
- if (fwd === undefined || seen.has(fwd))
771
- break;
772
- next = fwd;
773
- }
774
- return read(ctx, next);
775
- }
776
- /** REVERSE projection: the context a learnt continuation follows, voiced as
777
- * bytes. A common continuation ("Yes.") follows MANY contexts; with a
778
- * `guide` the context whose gist resonates with the query wins (seat
779
- * symmetry) — without one, the most-corroborated context wins (poured halo
780
- * MASS, the direct measure of how many episodes established it), falling
781
- * back to first-learnt on equal mass. Among many predecessors RECIPROCAL
782
- * ones (mutual edges) are preferred when any exist (RC5). Callers that
783
- * HAVE a query gist must pass it, or they silently change disambiguation
784
- * regime.
785
- *
786
- * `rev`, when the caller has already materialised prevOf (one read per
787
- * relation — a hub's reverse fan-in is corpus-sized), is reused instead of
788
- * refetched. Returns null when there is no predecessor or the picked
789
- * context reads empty (a zero-length context is no grounding: an empty
790
- * Uint8Array is truthy, and returning it would flow a hollow "answer"
791
- * onward). */
792
- export function reverseContext(ctx, id, guide, rev) {
793
- // CAPPED default read: only the first √N predecessors are ever candidates
794
- // (hubCap below / in chooseAmong), so only they are read. hubBound ≥ 2
795
- // keeps the single-predecessor shortcut exact.
796
- const candidates = rev ?? ctx.store.prevFirst(id, hubBound(ctx));
797
- if (candidates.length === 0)
798
- return null;
799
- // RECIPROCAL PREFERENCE: among many predecessors, one that `id` also
800
- // continues TO (cand → id AND id → cand both learnt) is a mutually
801
- // established pairing — the strongest structural evidence a predecessor
802
- // can carry (bidirectional training deposits both directions of a genuine
803
- // pair). A bare predecessor is one episode's adjacency; guide-resonance
804
- // over bare predecessors favours whichever stored document merely
805
- // CONTAINS the query's bytes (the linear fold's cosine is byte overlap —
806
- // the observed "merci → unrelated French document" failure). One capped
807
- // forward read decides; when no reciprocal exists, behaviour is unchanged
808
- // — bare predecessors ARE the honest answer for a shared deposited
809
- // continuation (two questions → one answer; audited by 31-audit C1), and
810
- // this arm serves every mechanism's reverse projection, so abstaining
811
- // here starves far more than the one containment failure it would fix.
812
- let pool = candidates;
813
- if (candidates.length > 1) {
814
- const fwd = new Set(ctx.store.nextFirst(id, hubBound(ctx)));
815
- if (fwd.size > 0) {
816
- const mutual = candidates.filter((c) => fwd.has(c));
817
- if (mutual.length > 0)
818
- pool = mutual;
819
- }
820
- }
821
- const pick = pool.length === 1
822
- ? pool[0]
823
- : guide
824
- ? chooseAmong(ctx, pool, guide).id
825
- : pickByMass(ctx, pool);
826
- const g = read(ctx, pick);
827
- return g.length > 0 ? g : null;
828
- }
829
- /** The most-corroborated candidate by poured halo mass (first-seen wins a
830
- * tie). Capped at √N candidates by insertion order — the same hub bound
831
- * every fan-out walk uses. */
832
- function pickByMass(ctx, ids) {
833
- const capped = hubCap(ctx, ids);
834
- let best = capped[0];
835
- let bestMass = ctx.store.haloMass(best);
836
- for (let i = 1; i < capped.length; i++) {
837
- const mass = ctx.store.haloMass(capped[i]);
838
- if (mass > bestMass) {
839
- best = capped[i];
840
- bestMass = mass;
841
- }
842
- }
843
- return best;
844
- }
845
- /** THE projection: ground a matched node to answer bytes — FORWARD to its
846
- * continuation fixpoint (which may cross a concept hop), else REVERSE to
847
- * the context it follows. This is the direction ladder every mechanism's
848
- * final grounding step reduces to. */
849
- export async function project(ctx, id, guide) {
850
- const fc = await follow(ctx, id, guide);
851
- if (fc)
852
- return fc;
853
- return reverseContext(ctx, id, guide);
854
- }
855
- // ── The span-shape family ───────────────────────────────────────────────────
856
- //
857
- // "Is this answer drawn from this context?" has TWO formally distinct
858
- // readings, and the pair plus the anchor classifier built on them are SHARED
859
- // machinery — extraction proposes span-shaped exemplars with them, the
860
- // shared `Precomputed.spanShapedOf` container computes them, and fusion
861
- // (reasoning.ts) gates on the strict one. They lived inside
862
- // mechanisms/extraction.ts, so `pipeline-mechanism.ts` and `reasoning.ts`
863
- // both had to import back OUT of a specific mechanism — an inversion the
864
- // mechanism market forbids (AGENTS §2.6: the shared contract may not depend
865
- // on any one mechanism; §2.5: a shared matcher belongs to this family, never
866
- // to a mechanism's private helpers). Deleting extraction must not break the
867
- // shared container, so they live here.
868
- //
869
- // • isSpanShaped — the OPEN reading (sparse in-order embedding).
870
- // • containsSpan — the STRICT reading (contiguous run or resolved node).
871
- // • skillExemplar — classify one anchor into (context, answer) using them.
872
- //
873
- // The two readings are NOT interchangeable; AGENTS §2.5 pins the distinction
874
- // and each function's own doc states what breaks if it is substituted.
875
- /** Check whether an anchor is a span-shaped skill exemplar: it represents a
876
- * fact whose context and answer together form a span-in-context pattern.
877
- * If the anchor has a nextOf continuation, that is the answer and the anchor
878
- * itself is the context. Otherwise the anchor's prevOf parents provide
879
- * candidate contexts, and the longest one whose span is span-shaped wins. */
880
- export async function skillExemplar(ctx, anchor, guide) {
881
- if (ctx.store.hasNext(anchor)) {
882
- const contextBytes = read(ctx, anchor);
883
- const answerBytes = await follow(ctx, anchor, guide);
884
- if (answerBytes !== null && isSpanShaped(ctx, contextBytes, answerBytes)) {
885
- return { contextBytes, answerBytes };
886
- }
887
- return null;
888
- }
889
- const answerBytes = read(ctx, anchor);
890
- // Candidate contexts, capped at the hub bound (a common answer's reverse
891
- // fan-in is corpus-sized).
892
- const capped = ctx.store.prevFirst(anchor, hubBound(ctx));
893
- const spanShaped = [];
894
- for (const p of capped) {
895
- const ctxB = read(ctx, p);
896
- if (ctxB.length > 0 && isSpanShaped(ctx, ctxB, answerBytes)) {
897
- spanShaped.push({ id: p, bytes: ctxB });
898
- }
899
- }
900
- if (spanShaped.length === 0)
901
- return null;
902
- // Among span-shaped contexts, the longest wins (the smallest spanning frame
903
- // heuristic's dual: more frame to locate in the query); the query gist,
904
- // when given, breaks LENGTH TIES via chooseAmong — the same reverse-regime
905
- // disambiguator every context pick uses, whose gist cache spares the
906
- // re-fold this block once paid per tied candidate. Same strict first-seen
907
- // tie-break as the hand loop it replaces.
908
- const maxLen = Math.max(...spanShaped.map((s) => s.bytes.length));
909
- const longest = spanShaped.filter((s) => s.bytes.length === maxLen);
910
- let contextBytes = longest[0].bytes;
911
- if (guide && longest.length > 1) {
912
- const pick = chooseAmong(ctx, longest.map((s) => s.id), guide).id;
913
- contextBytes = longest.find((s) => s.id === pick).bytes;
914
- }
915
- return { contextBytes, answerBytes };
916
- }
917
- /** Whether the answer is a SPARSE subsequence of the context (bytes in
918
- * order, arbitrary gaps) — the OPEN span-shape reading (see the section
919
- * note above). This is what lets extraction validate a MULTI-PIECE
920
- * exemplar whose answer is stitched from several context runs — but it is
921
- * deliberately permissive, so it must never be used as evidence that one
922
- * span was "drawn from" another (see {@link containsSpan} for that).
923
- *
924
- * There is deliberately NO containsSpan pre-check here: strict containment
925
- * IMPLIES the subsequence embedding (a contiguous run, or a resolved node —
926
- * whose content-addressed identity means its bytes occur contiguously — is
927
- * an in-order embedding with zero gaps), so the scan below decides alone,
928
- * with the same truth value. The old pre-check re-perceived the context
929
- * (a full river fold) per CANDIDATE in skillExemplar's √N-capped loop —
930
- * pure cost, no discrimination. */
931
- export function isSpanShaped(_ctx, context, answer) {
932
- let ai = 0;
933
- for (let ci = 0; ci < context.length && ai < answer.length; ci++) {
934
- if (context[ci] === answer[ai])
935
- ai++;
936
- }
937
- return ai === answer.length;
938
- }
939
- /** STRICT containment: the answer's resolved node appears in the context's
940
- * folded tree, or the answer occurs as one CONTIGUOUS byte run of the
941
- * context. This is real evidence the answer was drawn from the context.
942
- * Fusion gates on this — the sparse-subsequence reading of
943
- * {@link isSpanShaped} is trivially satisfied by short answers over long
944
- * queries ("cold" is a gap-tolerant subsequence of most sentences holding
945
- * c…o…l…d in order), and gating fusion on it silently starved multi-topic
946
- * queries of their further points of attention. */
947
- export function containsSpan(ctx, context, answer) {
948
- const ansId = resolve(ctx, answer);
949
- if (ansId !== null) {
950
- let found = false;
951
- foldTree(ctx, perceive(ctx, context), 0, (_n, _s, _e, node) => {
952
- if (node === ansId)
953
- found = true;
954
- });
955
- if (found)
956
- return true;
957
- }
958
- return indexOf(context, answer, 0) >= 0;
959
- }