@hviana/sema 0.1.9 → 0.2.1
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.
- package/dist/example/demo.d.ts +1 -0
- package/dist/example/demo.js +39 -0
- package/dist/example/train_base.d.ts +87 -0
- package/dist/example/train_base.js +2252 -0
- package/dist/src/alphabet.d.ts +7 -0
- package/dist/src/alphabet.js +33 -0
- package/dist/src/alu/src/alu.d.ts +185 -0
- package/dist/src/alu/src/alu.js +440 -0
- package/dist/src/alu/src/expr.d.ts +61 -0
- package/dist/src/alu/src/expr.js +318 -0
- package/dist/src/alu/src/index.d.ts +11 -0
- package/dist/src/alu/src/index.js +19 -0
- package/dist/src/alu/src/kernel-arith.d.ts +16 -0
- package/dist/src/alu/src/kernel-arith.js +264 -0
- package/dist/src/alu/src/kernel-bits.d.ts +19 -0
- package/dist/src/alu/src/kernel-bits.js +152 -0
- package/dist/src/alu/src/kernel-logic.d.ts +4 -0
- package/dist/src/alu/src/kernel-logic.js +60 -0
- package/dist/src/alu/src/kernel-nd.d.ts +3 -0
- package/dist/src/alu/src/kernel-nd.js +208 -0
- package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
- package/dist/src/alu/src/kernel-numeric.js +366 -0
- package/dist/src/alu/src/operation.d.ts +168 -0
- package/dist/src/alu/src/operation.js +189 -0
- package/dist/src/alu/src/parser.d.ts +221 -0
- package/dist/src/alu/src/parser.js +577 -0
- package/dist/src/alu/src/resonance.d.ts +55 -0
- package/dist/src/alu/src/resonance.js +126 -0
- package/dist/src/alu/src/text.d.ts +31 -0
- package/dist/src/alu/src/text.js +73 -0
- package/dist/src/alu/src/value.d.ts +109 -0
- package/dist/src/alu/src/value.js +300 -0
- package/dist/src/alu/test/alu.test.d.ts +1 -0
- package/dist/src/alu/test/alu.test.js +764 -0
- package/dist/src/bytes.d.ts +14 -0
- package/dist/src/bytes.js +59 -0
- package/dist/src/canon.d.ts +26 -0
- package/dist/src/canon.js +57 -0
- package/dist/src/config.d.ts +111 -0
- package/dist/src/config.js +91 -0
- package/dist/src/derive/src/deduction.d.ts +125 -0
- package/dist/src/derive/src/deduction.js +155 -0
- package/dist/src/derive/src/index.d.ts +7 -0
- package/dist/src/derive/src/index.js +11 -0
- package/dist/src/derive/src/priority-queue.d.ts +20 -0
- package/dist/src/derive/src/priority-queue.js +73 -0
- package/dist/src/derive/src/rewrite.d.ts +56 -0
- package/dist/src/derive/src/rewrite.js +100 -0
- package/dist/src/derive/src/trie.d.ts +90 -0
- package/dist/src/derive/src/trie.js +217 -0
- package/dist/src/derive/test/derive.test.d.ts +1 -0
- package/dist/src/derive/test/derive.test.js +122 -0
- package/dist/src/extension.d.ts +37 -0
- package/dist/src/extension.js +7 -0
- package/dist/src/geometry.d.ts +148 -0
- package/dist/src/geometry.js +511 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +18 -0
- package/dist/src/ingest-cache.d.ts +41 -0
- package/dist/src/ingest-cache.js +161 -0
- package/dist/src/mind/articulation.d.ts +6 -0
- package/dist/src/mind/articulation.js +99 -0
- package/dist/src/mind/attention.d.ts +72 -0
- package/dist/src/mind/attention.js +898 -0
- package/dist/src/mind/canonical.d.ts +29 -0
- package/dist/src/mind/canonical.js +88 -0
- package/dist/src/mind/graph-search.d.ts +270 -0
- package/dist/src/mind/graph-search.js +927 -0
- package/dist/src/mind/index.d.ts +5 -0
- package/dist/src/mind/index.js +5 -0
- package/dist/src/mind/junction.d.ts +95 -0
- package/dist/src/mind/junction.js +262 -0
- package/dist/src/mind/learning.d.ts +47 -0
- package/dist/src/mind/learning.js +233 -0
- package/dist/src/mind/match.d.ts +113 -0
- package/dist/src/mind/match.js +446 -0
- package/dist/src/mind/mechanisms/alu.d.ts +4 -0
- package/dist/src/mind/mechanisms/alu.js +29 -0
- package/dist/src/mind/mechanisms/cast.d.ts +35 -0
- package/dist/src/mind/mechanisms/cast.js +461 -0
- package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
- package/dist/src/mind/mechanisms/confluence.js +225 -0
- package/dist/src/mind/mechanisms/cover.d.ts +6 -0
- package/dist/src/mind/mechanisms/cover.js +179 -0
- package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
- package/dist/src/mind/mechanisms/extraction.js +342 -0
- package/dist/src/mind/mechanisms/recall.d.ts +13 -0
- package/dist/src/mind/mechanisms/recall.js +235 -0
- package/dist/src/mind/mind.d.ts +273 -0
- package/dist/src/mind/mind.js +598 -0
- package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
- package/dist/src/mind/pipeline-mechanism.js +213 -0
- package/dist/src/mind/pipeline.d.ts +20 -0
- package/dist/src/mind/pipeline.js +185 -0
- package/dist/src/mind/primitives.d.ts +63 -0
- package/dist/src/mind/primitives.js +251 -0
- package/dist/src/mind/rationale.d.ts +134 -0
- package/dist/src/mind/rationale.js +162 -0
- package/dist/src/mind/reasoning.d.ts +15 -0
- package/dist/src/mind/reasoning.js +162 -0
- package/dist/src/mind/recognition.d.ts +20 -0
- package/dist/src/mind/recognition.js +246 -0
- package/dist/src/mind/resonance.d.ts +23 -0
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +15 -0
- package/dist/src/mind/trace.js +73 -0
- package/dist/src/mind/traverse.d.ts +113 -0
- package/dist/src/mind/traverse.js +488 -0
- package/dist/src/mind/types.d.ts +186 -0
- package/dist/src/mind/types.js +84 -0
- package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
- package/dist/src/rabitq-ivf/src/database.js +201 -0
- package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
- package/dist/src/rabitq-ivf/src/index.js +4 -0
- package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
- package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
- package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
- package/dist/src/rabitq-ivf/src/prng.js +36 -0
- package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
- package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
- package/dist/src/sema.d.ts +31 -0
- package/dist/src/sema.js +63 -0
- package/dist/src/store-sqlite.d.ts +184 -0
- package/dist/src/store-sqlite.js +942 -0
- package/dist/src/store.d.ts +654 -0
- package/dist/src/store.js +1612 -0
- package/dist/src/vec.d.ts +31 -0
- package/dist/src/vec.js +109 -0
- package/package.json +1 -1
- package/src/mind/graph-search.ts +128 -46
- package/src/mind/mind.ts +37 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// extraction.ts — Extraction (Skill) — Section 4 of the mind.
|
|
2
|
+
//
|
|
3
|
+
// Given a query and its consensus-ranked anchors, find the first span-shaped
|
|
4
|
+
// skill exemplar among the ranked anchors and read the analogous span of the
|
|
5
|
+
// query. A skill exemplar is a learnt fact whose context and answer together
|
|
6
|
+
// form a span-in-context pattern: the answer is a subsequence of the context
|
|
7
|
+
// (or one of its pieces is), and the context is the smallest spanning frame
|
|
8
|
+
// that contains it.
|
|
9
|
+
import { foldTree, perceive, read, resolve } from "../primitives.js";
|
|
10
|
+
import { follow, locate } from "../match.js";
|
|
11
|
+
import { chooseAmong, hubBound } from "../traverse.js";
|
|
12
|
+
import { concatBytes, indexOf } from "../../bytes.js";
|
|
13
|
+
import { decodeText, unexplainedLabel } from "../rationale.js";
|
|
14
|
+
import { CONCEPT, STEP } from "../graph-search.js";
|
|
15
|
+
import { rItem, rNode, traceFail } from "../trace.js";
|
|
16
|
+
// ── Extraction ────────────────────────────────────────────────────────────
|
|
17
|
+
/** Find the first span-shaped skill exemplar among the ranked anchors from
|
|
18
|
+
* climbAttentionAll and read the analogous span from the query. Returns
|
|
19
|
+
* the extracted bytes PLUS the query spans the skill ACCOUNTED FOR — the
|
|
20
|
+
* located frames AND any read span BOUNDED by located frames on both
|
|
21
|
+
* sides, the elementary evidence think's grounding decider weighs. A
|
|
22
|
+
* bounded read is explained: the skill located both its borders in the
|
|
23
|
+
* query and emitted exactly what sits between them. An OPEN-ENDED read
|
|
24
|
+
* (the exemplar's answer reaches the context's end, so the query is read
|
|
25
|
+
* to its own end with no located right border) remains a guess about where
|
|
26
|
+
* the span stops — it stays unaccounted, priced by exclusion like the
|
|
27
|
+
* cover's bridged bytes. (Accounting frames only — the earlier convention
|
|
28
|
+
* — let a CAST juxtaposition that merely echoed the query's exact site
|
|
29
|
+
* outweigh a correct bounded extraction: the same span counted as
|
|
30
|
+
* explained for one mechanism and not the other, and the asymmetry, not
|
|
31
|
+
* the answers' merits, decided the grounding.) Null when no skill
|
|
32
|
+
* applies. */
|
|
33
|
+
export async function extractBySkill(ctx, query, pre) {
|
|
34
|
+
const t = ctx.trace?.enter("extractBySkill", [
|
|
35
|
+
rItem(query, "query"),
|
|
36
|
+
]);
|
|
37
|
+
const fail = traceFail(t);
|
|
38
|
+
// Use climbAttentionAll to get the FULL ranked list, not just the
|
|
39
|
+
// roots that cleared commitVotes' significance floor. The floor
|
|
40
|
+
// gates further points of attention for fusion, but extraction only
|
|
41
|
+
// needs ONE anchor that IS a span-shaped skill exemplar — and on
|
|
42
|
+
// some seeds the top-voted anchor is not one (e.g. a concept-merge
|
|
43
|
+
// nickname outvotes the painting exemplars on shared substrings,
|
|
44
|
+
// while the exemplars' votes fall below the floor). Iterating the
|
|
45
|
+
// ranked list instead of just the roots lets extraction reach the
|
|
46
|
+
// first painting-exemplar anchor regardless of its floor status.
|
|
47
|
+
//
|
|
48
|
+
const { ranked } = await pre.attention();
|
|
49
|
+
if (ranked.length === 0) {
|
|
50
|
+
return fail("no consensus anchor — no skill to apply");
|
|
51
|
+
}
|
|
52
|
+
let exemplar = null;
|
|
53
|
+
let chosenAnchor = null;
|
|
54
|
+
let skipped = 0;
|
|
55
|
+
for (const cand of ranked) {
|
|
56
|
+
const ex = await pre.spanShapedOf(cand.anchor);
|
|
57
|
+
if (ex) {
|
|
58
|
+
exemplar = ex;
|
|
59
|
+
chosenAnchor = cand.anchor;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
skipped++;
|
|
63
|
+
}
|
|
64
|
+
if (exemplar === null) {
|
|
65
|
+
ctx.trace?.step("trySkillAnchors", [], [], `none of ${ranked.length} ranked anchor(s) is a span-shaped skill exemplar`);
|
|
66
|
+
return fail("no consensus root is a span-shaped skill exemplar");
|
|
67
|
+
}
|
|
68
|
+
if (skipped > 0) {
|
|
69
|
+
ctx.trace?.step("trySkillAnchors", [
|
|
70
|
+
rItem(query.subarray(0, 0), `skipped ${skipped}`),
|
|
71
|
+
rNode(ctx, chosenAnchor, "chosen"),
|
|
72
|
+
], [], `skipped ${skipped} anchor(s) that are not skill exemplars`);
|
|
73
|
+
}
|
|
74
|
+
const { contextBytes, answerBytes } = exemplar;
|
|
75
|
+
const ansCtxRuns = answerRunsInContext(ctx, contextBytes, answerBytes);
|
|
76
|
+
if (ansCtxRuns === null || ansCtxRuns.length === 0) {
|
|
77
|
+
return fail("answer is not a subsequence of the context");
|
|
78
|
+
}
|
|
79
|
+
if (ansCtxRuns.length > 1) {
|
|
80
|
+
ctx.trace?.step("decomposeAnswer", [rItem(answerBytes, "multi-piece-answer")], ansCtxRuns.map((r) => rItem(contextBytes.subarray(r.start, r.end), "piece", undefined, [
|
|
81
|
+
r.start,
|
|
82
|
+
r.end,
|
|
83
|
+
])), `answer splits into ${ansCtxRuns.length} piece(s) within the exemplar context`);
|
|
84
|
+
}
|
|
85
|
+
const pieces = [];
|
|
86
|
+
const accounted = [];
|
|
87
|
+
for (let ri = 0; ri < ansCtxRuns.length; ri++) {
|
|
88
|
+
const run = ansCtxRuns[ri];
|
|
89
|
+
const isLast = ri === ansCtxRuns.length - 1;
|
|
90
|
+
const framePreLen = Math.min(run.start, ctx.space.maxGroup);
|
|
91
|
+
const framePre = run.start > 0
|
|
92
|
+
? contextBytes.subarray(run.start - framePreLen, run.start)
|
|
93
|
+
: null;
|
|
94
|
+
const frames = [];
|
|
95
|
+
let start = 0;
|
|
96
|
+
if (framePre) {
|
|
97
|
+
const prePos = locate(ctx, query, framePre, 0, pre.rec.sites);
|
|
98
|
+
if (prePos < 0)
|
|
99
|
+
continue;
|
|
100
|
+
start = prePos + framePre.length;
|
|
101
|
+
frames.push([prePos, start]); // the located frame IS matched evidence
|
|
102
|
+
}
|
|
103
|
+
let end;
|
|
104
|
+
if (isLast) {
|
|
105
|
+
if (run.end < contextBytes.length) {
|
|
106
|
+
const framePostLen = Math.min(contextBytes.length - run.end, ctx.space.maxGroup);
|
|
107
|
+
const framePost = contextBytes.subarray(run.end, run.end + framePostLen);
|
|
108
|
+
const postPos = locate(ctx, query.subarray(start), framePost, 0, pre.rec.sites);
|
|
109
|
+
if (postPos < 0)
|
|
110
|
+
continue;
|
|
111
|
+
end = start + postPos;
|
|
112
|
+
frames.push([end, end + framePost.length]); // matched post-frame
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
end = query.length;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const nextRun = ansCtxRuns[ri + 1];
|
|
120
|
+
const nextPreLen = Math.min(nextRun.start, ctx.space.maxGroup);
|
|
121
|
+
const nextPre = contextBytes.subarray(nextRun.start - nextPreLen, nextRun.start);
|
|
122
|
+
const nextPos = locate(ctx, query.subarray(start), nextPre, 0, pre.rec.sites);
|
|
123
|
+
if (nextPos < 0) {
|
|
124
|
+
end = start + run.ansLen;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
end = start + nextPos;
|
|
128
|
+
frames.push([end, end + nextPre.length]); // matched next-frame
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (start >= end)
|
|
132
|
+
continue;
|
|
133
|
+
pieces.push(query.subarray(start, end));
|
|
134
|
+
accounted.push(...frames);
|
|
135
|
+
// Bounded on both sides ⇒ the read span itself is explained (see doc).
|
|
136
|
+
// frames carries the pre-border (when the answer is not at the context's
|
|
137
|
+
// start) and the located right border (post-frame or next piece's
|
|
138
|
+
// pre-frame); only when BOTH borders were located is the read bounded.
|
|
139
|
+
const preBounded = run.start === 0 || frames.some(([, e]) => e === start);
|
|
140
|
+
const postBounded = frames.some(([b]) => b === end);
|
|
141
|
+
if (preBounded && postBounded)
|
|
142
|
+
accounted.push([start, end]);
|
|
143
|
+
}
|
|
144
|
+
if (pieces.length === 0) {
|
|
145
|
+
return fail("no answer piece's frame located in the query");
|
|
146
|
+
}
|
|
147
|
+
const out = pieces.length === 1 ? pieces[0] : concatBytes(pieces);
|
|
148
|
+
t?.done([rItem(out, "extracted")], pieces.length === 1
|
|
149
|
+
? `apply a learnt extraction skill — read the analogous span of the query` +
|
|
150
|
+
` framed like "${decodeText(answerBytes)}" sits in its exemplar`
|
|
151
|
+
: `apply a learnt MULTI-PIECE skill — read ${pieces.length} analogous` +
|
|
152
|
+
` pieces of the query and synthesize them like "${decodeText(answerBytes)}"`);
|
|
153
|
+
return {
|
|
154
|
+
bytes: out,
|
|
155
|
+
accounted,
|
|
156
|
+
unexplained: unexplainedLabel(query, accounted),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
// ── The two span-shape readings: OPEN acceptance vs. STRONG decomposition ──
|
|
160
|
+
//
|
|
161
|
+
// isSpanShaped and answerRunsInContext read the SAME relation ("the answer is
|
|
162
|
+
// drawn from the context") at two deliberately different strengths, and they
|
|
163
|
+
// are NOT interchangeable:
|
|
164
|
+
//
|
|
165
|
+
// • isSpanShaped — the OPEN reading: any in-order embedding (a sparse
|
|
166
|
+
// subsequence, arbitrary gaps). O(|context|) byte scan. Used to ACCEPT
|
|
167
|
+
// an exemplar candidate.
|
|
168
|
+
// • answerRunsInContext — the STRONG reading: a greedy longest-run
|
|
169
|
+
// DECOMPOSITION into contiguous pieces. Greedy-longest is strictly
|
|
170
|
+
// stronger than subsequence (a long late match can consume context an
|
|
171
|
+
// earlier shorter choice needed), so an ACCEPTED exemplar can still fail
|
|
172
|
+
// to decompose — extractBySkill then fails with "answer is not a
|
|
173
|
+
// subsequence of the context" and think falls through to recall. That
|
|
174
|
+
// fall-through is BEHAVIOUR, pinned by the extraction suites: do not
|
|
175
|
+
// "unify" the two into one machine — replacing the open reading with the
|
|
176
|
+
// strong one silently rejects exemplars extraction today accepts, and
|
|
177
|
+
// replacing the strong one with a backtracking embedding changes which
|
|
178
|
+
// pieces are read out of the query.
|
|
179
|
+
/** Decompose an answer into substrings of its surrounding context, in order —
|
|
180
|
+
* the STRONG span-shape reading (see the section note above). Returns null
|
|
181
|
+
* when no greedy longest-run decomposition exists. Adjacent runs that
|
|
182
|
+
* connect contiguously are merged. */
|
|
183
|
+
export function answerRunsInContext(_ctx, context, answer) {
|
|
184
|
+
const pos = indexOf(context, answer, 0);
|
|
185
|
+
if (pos >= 0) {
|
|
186
|
+
return [{ start: pos, end: pos + answer.length, ansLen: answer.length }];
|
|
187
|
+
}
|
|
188
|
+
const runs = [];
|
|
189
|
+
let ai = 0;
|
|
190
|
+
let ci = 0;
|
|
191
|
+
while (ai < answer.length) {
|
|
192
|
+
// Longest match of the remaining answer at any position of the remaining
|
|
193
|
+
// context: one direct extend per context position — O(|ctx|·match) per
|
|
194
|
+
// run, replacing the previous longest-first indexOf countdown whose
|
|
195
|
+
// repeated scans were cubic on long sparse-subsequence answers.
|
|
196
|
+
let bestLen = 0;
|
|
197
|
+
let bestPos = -1;
|
|
198
|
+
for (let p = ci; p < context.length; p++) {
|
|
199
|
+
let l = 0;
|
|
200
|
+
const maxL = Math.min(context.length - p, answer.length - ai);
|
|
201
|
+
if (maxL <= bestLen)
|
|
202
|
+
break; // no later position can beat the best
|
|
203
|
+
while (l < maxL && context[p + l] === answer[ai + l])
|
|
204
|
+
l++;
|
|
205
|
+
if (l > bestLen) {
|
|
206
|
+
bestLen = l;
|
|
207
|
+
bestPos = p;
|
|
208
|
+
if (ai + l === answer.length)
|
|
209
|
+
break; // the whole remainder matched
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (bestLen === 0)
|
|
213
|
+
return null;
|
|
214
|
+
runs.push({ start: bestPos, end: bestPos + bestLen, ansLen: bestLen });
|
|
215
|
+
ai += bestLen;
|
|
216
|
+
ci = bestPos + bestLen;
|
|
217
|
+
}
|
|
218
|
+
const merged = [];
|
|
219
|
+
for (const r of runs) {
|
|
220
|
+
const last = merged[merged.length - 1];
|
|
221
|
+
if (last && r.start === last.end) {
|
|
222
|
+
last.end = r.end;
|
|
223
|
+
last.ansLen += r.ansLen;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
merged.push({ ...r });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return merged.length > 0 ? merged : null;
|
|
230
|
+
}
|
|
231
|
+
/** Check whether an anchor is a span-shaped skill exemplar: it represents a
|
|
232
|
+
* fact whose context and answer together form a span-in-context pattern.
|
|
233
|
+
* If the anchor has a nextOf continuation, that is the answer and the anchor
|
|
234
|
+
* itself is the context. Otherwise the anchor's prevOf parents provide
|
|
235
|
+
* candidate contexts, and the longest one whose span is span-shaped wins. */
|
|
236
|
+
export async function skillExemplar(ctx, anchor, guide) {
|
|
237
|
+
if (ctx.store.hasNext(anchor)) {
|
|
238
|
+
const contextBytes = read(ctx, anchor);
|
|
239
|
+
const answerBytes = await follow(ctx, anchor, guide);
|
|
240
|
+
if (answerBytes !== null && isSpanShaped(ctx, contextBytes, answerBytes)) {
|
|
241
|
+
return { contextBytes, answerBytes };
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
const answerBytes = read(ctx, anchor);
|
|
246
|
+
// Candidate contexts, capped at the hub bound (a common answer's reverse
|
|
247
|
+
// fan-in is corpus-sized).
|
|
248
|
+
const capped = ctx.store.prevFirst(anchor, hubBound(ctx));
|
|
249
|
+
const spanShaped = [];
|
|
250
|
+
for (const p of capped) {
|
|
251
|
+
const ctxB = read(ctx, p);
|
|
252
|
+
if (ctxB.length > 0 && isSpanShaped(ctx, ctxB, answerBytes)) {
|
|
253
|
+
spanShaped.push({ id: p, bytes: ctxB });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (spanShaped.length === 0)
|
|
257
|
+
return null;
|
|
258
|
+
// Among span-shaped contexts, the longest wins (the smallest spanning frame
|
|
259
|
+
// heuristic's dual: more frame to locate in the query); the query gist,
|
|
260
|
+
// when given, breaks LENGTH TIES via chooseAmong — the same reverse-regime
|
|
261
|
+
// disambiguator every context pick uses, whose gist cache spares the
|
|
262
|
+
// re-fold this block once paid per tied candidate. Same strict first-seen
|
|
263
|
+
// tie-break as the hand loop it replaces.
|
|
264
|
+
const maxLen = Math.max(...spanShaped.map((s) => s.bytes.length));
|
|
265
|
+
const longest = spanShaped.filter((s) => s.bytes.length === maxLen);
|
|
266
|
+
let contextBytes = longest[0].bytes;
|
|
267
|
+
if (guide && longest.length > 1) {
|
|
268
|
+
const pick = chooseAmong(ctx, longest.map((s) => s.id), guide).id;
|
|
269
|
+
contextBytes = longest.find((s) => s.id === pick).bytes;
|
|
270
|
+
}
|
|
271
|
+
return { contextBytes, answerBytes };
|
|
272
|
+
}
|
|
273
|
+
/** Whether the answer is a SPARSE subsequence of the context (bytes in
|
|
274
|
+
* order, arbitrary gaps) — the OPEN span-shape reading (see the section
|
|
275
|
+
* note above). This is what lets extraction validate a MULTI-PIECE
|
|
276
|
+
* exemplar whose answer is stitched from several context runs — but it is
|
|
277
|
+
* deliberately permissive, so it must never be used as evidence that one
|
|
278
|
+
* span was "drawn from" another (see {@link containsSpan} for that).
|
|
279
|
+
*
|
|
280
|
+
* There is deliberately NO containsSpan pre-check here: strict containment
|
|
281
|
+
* IMPLIES the subsequence embedding (a contiguous run, or a resolved node —
|
|
282
|
+
* whose content-addressed identity means its bytes occur contiguously — is
|
|
283
|
+
* an in-order embedding with zero gaps), so the scan below decides alone,
|
|
284
|
+
* with the same truth value. The old pre-check re-perceived the context
|
|
285
|
+
* (a full river fold) per CANDIDATE in skillExemplar's √N-capped loop —
|
|
286
|
+
* pure cost, no discrimination. */
|
|
287
|
+
export function isSpanShaped(_ctx, context, answer) {
|
|
288
|
+
let ai = 0;
|
|
289
|
+
for (let ci = 0; ci < context.length && ai < answer.length; ci++) {
|
|
290
|
+
if (context[ci] === answer[ai])
|
|
291
|
+
ai++;
|
|
292
|
+
}
|
|
293
|
+
return ai === answer.length;
|
|
294
|
+
}
|
|
295
|
+
/** STRICT containment: the answer's resolved node appears in the context's
|
|
296
|
+
* folded tree, or the answer occurs as one CONTIGUOUS byte run of the
|
|
297
|
+
* context. This is real evidence the answer was drawn from the context.
|
|
298
|
+
* Fusion gates on this — the sparse-subsequence reading of
|
|
299
|
+
* {@link isSpanShaped} is trivially satisfied by short answers over long
|
|
300
|
+
* queries ("cold" is a gap-tolerant subsequence of most sentences holding
|
|
301
|
+
* c…o…l…d in order), and gating fusion on it silently starved multi-topic
|
|
302
|
+
* queries of their further points of attention. */
|
|
303
|
+
export function containsSpan(ctx, context, answer) {
|
|
304
|
+
const ansId = resolve(ctx, answer);
|
|
305
|
+
if (ansId !== null) {
|
|
306
|
+
let found = false;
|
|
307
|
+
foldTree(ctx, perceive(ctx, context), 0, (_n, _s, _e, node) => {
|
|
308
|
+
if (node === ansId)
|
|
309
|
+
found = true;
|
|
310
|
+
});
|
|
311
|
+
if (found)
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
return indexOf(context, answer, 0) >= 0;
|
|
315
|
+
}
|
|
316
|
+
// ── Pipeline mechanism ──────────────────────────────────────────────────────
|
|
317
|
+
export const extractionMechanism = {
|
|
318
|
+
name: "extraction",
|
|
319
|
+
provenance: "extract",
|
|
320
|
+
async floor(_ctx, _query, pre, worthRunning) {
|
|
321
|
+
// Extraction's floor is always exactly CONCEPT+STEP when it exists —
|
|
322
|
+
// same investment discipline as CAST's (see cast.ts): when the bound
|
|
323
|
+
// already cannot beat the incumbent, return it UNINVESTED (never
|
|
324
|
+
// first-touch the climb just to be pruned).
|
|
325
|
+
if (!worthRunning(CONCEPT + STEP))
|
|
326
|
+
return CONCEPT + STEP;
|
|
327
|
+
if ((await pre.attention()).ranked.length === 0)
|
|
328
|
+
return null;
|
|
329
|
+
return CONCEPT + STEP;
|
|
330
|
+
},
|
|
331
|
+
async run(ctx, query, pre) {
|
|
332
|
+
const ex = await extractBySkill(ctx, query, pre);
|
|
333
|
+
if (!ex)
|
|
334
|
+
return [];
|
|
335
|
+
return [{
|
|
336
|
+
bytes: ex.bytes,
|
|
337
|
+
accounted: ex.accounted,
|
|
338
|
+
moves: CONCEPT + STEP * ex.accounted.length,
|
|
339
|
+
unexplained: ex.unexplained,
|
|
340
|
+
}];
|
|
341
|
+
},
|
|
342
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MindContext } from "../types.js";
|
|
2
|
+
import type { PipelineMechanism, Precomputed } from "../pipeline-mechanism.js";
|
|
3
|
+
/** A recall result. */
|
|
4
|
+
export interface RecallResult {
|
|
5
|
+
bytes: Uint8Array;
|
|
6
|
+
echoed: boolean;
|
|
7
|
+
accounted: Array<[number, number]>;
|
|
8
|
+
moves: number;
|
|
9
|
+
unexplained: string;
|
|
10
|
+
}
|
|
11
|
+
/** Recall the answer by resonating the whole query against the content index. */
|
|
12
|
+
export declare function recallByResonance(ctx: MindContext, query: Uint8Array, pre: Precomputed): Promise<RecallResult | null>;
|
|
13
|
+
export declare const recallMechanism: PipelineMechanism;
|
|
@@ -0,0 +1,235 @@
|
|
|
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
|
+
/** Recall the answer by resonating the whole query against the content index. */
|
|
16
|
+
export async function recallByResonance(ctx, query, pre) {
|
|
17
|
+
const t = ctx.trace?.enter("recallByResonance", [
|
|
18
|
+
rItem(query, "query"),
|
|
19
|
+
]);
|
|
20
|
+
const whole_ = [[0, query.length]];
|
|
21
|
+
const nothing = [];
|
|
22
|
+
const ground = (bytes, note, accounted, moves, echoed = false) => {
|
|
23
|
+
t?.done(bytes === null
|
|
24
|
+
? []
|
|
25
|
+
: [rItem(bytes, "answer", resolve(ctx, bytes) ?? undefined)], note);
|
|
26
|
+
return bytes === null ? null : {
|
|
27
|
+
bytes,
|
|
28
|
+
echoed,
|
|
29
|
+
accounted,
|
|
30
|
+
moves,
|
|
31
|
+
unexplained: unexplainedLabel(query, accounted),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
const k = pre.k;
|
|
35
|
+
const queryGist = pre.guide;
|
|
36
|
+
// 0. Exact self-match — content-addressed, deterministic.
|
|
37
|
+
const qId = pre.queryResolved;
|
|
38
|
+
if (qId !== null) {
|
|
39
|
+
const rev = ctx.store.prevFirst(qId, hubBound(ctx));
|
|
40
|
+
const g = reverseContext(ctx, qId, queryGist, rev);
|
|
41
|
+
if (g !== null) {
|
|
42
|
+
return ground(g, rev.length === 1
|
|
43
|
+
? "exact self-match — reverse recall to the sole predecessor"
|
|
44
|
+
: "exact self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// 0b. ARGUMENT BINDING (RC8): the query is not itself a stored form, but
|
|
48
|
+
// it CONTAINS a recognised constituent that is an edge SOURCE — a learnt
|
|
49
|
+
// pair's left side carried inside a wrapper ("How do you say 'thank you'
|
|
50
|
+
// in French?"). The wrapper is scaffolding; the argument is the span
|
|
51
|
+
// that LEADS somewhere, so its continuation — guided by the whole query's
|
|
52
|
+
// gist — is the answer. Matching the wrapper while ignoring the argument
|
|
53
|
+
// (the observed "good morning" template failure) is worse than silence,
|
|
54
|
+
// so anything short of ONE unambiguous binding falls through: the
|
|
55
|
+
// constituent bar is the same two-quanta (2W) reading confluence binds
|
|
56
|
+
// under, nested recognitions collapse to their MAXIMAL span, and two
|
|
57
|
+
// distinct maximal arguments mean the query asks about neither alone.
|
|
58
|
+
if (qId === null) {
|
|
59
|
+
const W2 = 2 * ctx.space.maxGroup;
|
|
60
|
+
const args = pre.rec.sites.filter((s) => s.end - s.start >= W2 &&
|
|
61
|
+
s.end - s.start < query.length &&
|
|
62
|
+
ctx.store.hasNext(s.payload));
|
|
63
|
+
// Maximal spans by one sorted sweep (starts ascending, ties longest
|
|
64
|
+
// first): every earlier span starts at or before s, so s is contained
|
|
65
|
+
// exactly when the running max end already covers it. O(m log m) — a
|
|
66
|
+
// long input recognises O(|input|) sites, and a pairwise scan here was
|
|
67
|
+
// quadratic in the input.
|
|
68
|
+
args.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
69
|
+
const maximal = [];
|
|
70
|
+
let maxEnd = -1;
|
|
71
|
+
for (const s of args) {
|
|
72
|
+
if (s.end <= maxEnd)
|
|
73
|
+
continue;
|
|
74
|
+
maximal.push(s);
|
|
75
|
+
maxEnd = s.end;
|
|
76
|
+
}
|
|
77
|
+
if (maximal.length === 1) {
|
|
78
|
+
const arg = maximal[0];
|
|
79
|
+
const g = await follow(ctx, arg.payload, queryGist);
|
|
80
|
+
if (g !== null && g.length > 0) {
|
|
81
|
+
return ground(g, "argument binding — the query's sole edge-source constituent, continuation followed", [[arg.start, arg.end]], STEP);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const whole = await ctx.store.resonate(queryGist, k);
|
|
86
|
+
if (whole.length === 0) {
|
|
87
|
+
return ground(null, "empty store — nothing to resonate with", [], 0);
|
|
88
|
+
}
|
|
89
|
+
const top = whole[0];
|
|
90
|
+
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)`);
|
|
91
|
+
// 1. Clean resonance — the scale-aware identity claim. The ANGLE
|
|
92
|
+
// (top.score) carries the shared fraction; the query's MAGNITUDE (√len,
|
|
93
|
+
// the linear fold's own norm) converts the tolerated foreign fraction
|
|
94
|
+
// into bytes — at most one river window (see {@link identityBar}). A
|
|
95
|
+
// fixed cosine bar let long queries claim "near-identical" while whole
|
|
96
|
+
// windows — an answer word — differed.
|
|
97
|
+
// A hit RESTATES the query when its bytes are the query's own — exactly,
|
|
98
|
+
// or under the response's equivalence (a case/width twin). Restating
|
|
99
|
+
// hits may only conclude through disciplined reverse recall: voicing
|
|
100
|
+
// their bytes echoes the query back at itself (never an answer — the
|
|
101
|
+
// same principle that keeps cast from voicing stored questions), and
|
|
102
|
+
// projecting them forward is reverse recall's containment failure in the
|
|
103
|
+
// other direction — "whatever followed these bytes in some document".
|
|
104
|
+
const qKey = ctx.canon ? ctx.canon(query) : query;
|
|
105
|
+
const restates = (b) => bytesEqual(b, query) ||
|
|
106
|
+
(ctx.canon !== null && bytesEqual(ctx.canon(b), qKey));
|
|
107
|
+
const idBar = identityBar(ctx.store.D, ctx.space.maxGroup, query.length);
|
|
108
|
+
if (top.score >= idBar) {
|
|
109
|
+
for (const h of whole) {
|
|
110
|
+
// The identity claim is PER HIT, not per tier: hits are ranked
|
|
111
|
+
// nearest-first, and grounding one below the bar under this tier's
|
|
112
|
+
// "near-identical" label would launder byte-overlap noise (observed:
|
|
113
|
+
// "merci" projecting through the unrelated near hit "meraih").
|
|
114
|
+
if (h.score < idBar)
|
|
115
|
+
break;
|
|
116
|
+
const own = read(ctx, h.id);
|
|
117
|
+
if (h.id === qId || restates(own)) {
|
|
118
|
+
const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
|
|
119
|
+
const g = reverseContext(ctx, h.id, queryGist, rev);
|
|
120
|
+
if (g !== null) {
|
|
121
|
+
return ground(g, rev.length === 1
|
|
122
|
+
? "perfect self-match — reverse recall to the sole predecessor"
|
|
123
|
+
: "perfect self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
|
|
124
|
+
}
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const g = await project(ctx, h.id, queryGist);
|
|
128
|
+
if (g) {
|
|
129
|
+
return ground(g, "clean whole-query resonance — ground the nearest hit", whole_, STEP);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// The query-relative grounding fraction, shared by tiers 2–4 — gated on
|
|
134
|
+
// the FRACTION OF THE QUERY the grounding explains, not the raw cosine.
|
|
135
|
+
// Root gists are unit vectors, but their magnitudes are recoverable from
|
|
136
|
+
// the byte lengths (‖·‖ = √len under the linear fold):
|
|
137
|
+
// cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
|
|
138
|
+
// The raw cosine punished honest containment — a query fully inside a
|
|
139
|
+
// longer grounded answer scored √(lenQ/lenG) and was refused — and let a
|
|
140
|
+
// long answer sharing only scaffolding pass; the query-relative fraction
|
|
141
|
+
// measures exactly what the reach bar means: how much of THE QUERY the
|
|
142
|
+
// store accounts for.
|
|
143
|
+
// Chance similarity survives the length conversion AMPLIFIED: the same
|
|
144
|
+
// √(lenG/lenQ) factor that converts an honest shared fraction into a
|
|
145
|
+
// query-relative one multiplies the estimator/chance floor too, so a long
|
|
146
|
+
// stored form (√(lenG/lenQ) ≈ 10 at 100×) lifted a noise-level cosine past
|
|
147
|
+
// the reach bar and grounded pure gibberish (observed). Only the
|
|
148
|
+
// ABOVE-CHANCE part of the similarity is evidence of shared content —
|
|
149
|
+
// subtract the significance bar (3/√D, §8.3) before converting. Derived
|
|
150
|
+
// from the existing bars; never tuned.
|
|
151
|
+
const sig = significanceBar(ctx.store.D);
|
|
152
|
+
const reach = reachThreshold(ctx.space.maxGroup);
|
|
153
|
+
const fracOfQuery = (cos, otherLen) => Math.min(1, Math.max(0, cos - sig) *
|
|
154
|
+
Math.sqrt(otherLen / Math.max(1, query.length)));
|
|
155
|
+
// 2. Scaffolding-dominated.
|
|
156
|
+
if (top.score >= sig) {
|
|
157
|
+
const N = corpusN(ctx);
|
|
158
|
+
const minVote = consensusFloor(N);
|
|
159
|
+
// The committed points of attention ARE the shared climb's roots (same
|
|
160
|
+
// query, same k, same DF mode) — read them from Precomputed instead of
|
|
161
|
+
// re-climbing, so even a traced response pays for the climb once.
|
|
162
|
+
const forest = (await pre.attention()).roots;
|
|
163
|
+
if (forest.length > 0 && forest[0].vote >= minVote) {
|
|
164
|
+
const g = await project(ctx, forest[0].anchor, queryGist);
|
|
165
|
+
// The anchor cleared the consensus floor, but the floor prices the
|
|
166
|
+
// ANCHOR's evidence, not the projection's: a junk attractor can clear
|
|
167
|
+
// it and project a PIECE OF THE QUERY back at it (the observed
|
|
168
|
+
// "buenos días in English" → "English" fragment). A projection that
|
|
169
|
+
// is a proper byte-subspan of the query restates part of the question
|
|
170
|
+
// — never an answer (the same principle as `restates` above, extended
|
|
171
|
+
// to fragments). Genuine anchor groundings — longer than the query,
|
|
172
|
+
// or disjoint from it — pass untouched.
|
|
173
|
+
if (g && !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
|
|
174
|
+
return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// 3. Last resort — the nearest grounded whole-query hit, same gate.
|
|
179
|
+
for (const h of whole) {
|
|
180
|
+
const g = await project(ctx, h.id, queryGist);
|
|
181
|
+
if (g) {
|
|
182
|
+
if (fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
|
|
183
|
+
reach) {
|
|
184
|
+
return ground(g, "last resort: the nearest grounded whole-query hit", [], STEP);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// The refusal/echo decision. The echo returns a stored form's bytes AS
|
|
189
|
+
// the answer — a near-identity claim about the query — and identity-grade
|
|
190
|
+
// decisions are never made on an estimated score ("approximate scores may
|
|
191
|
+
// rank and propose; they may never decide", §6.2): the RaBitQ estimate
|
|
192
|
+
// overshooting the reach bar echoed a WRONG-entity neighbour ("capital of
|
|
193
|
+
// Zamunda?" echoed the Armenia fact, observed). The bytes are read
|
|
194
|
+
// anyway to be echoed, so the decision uses their EXACT fold: one river
|
|
195
|
+
// fold of the top hit, measured in the same query-relative,
|
|
196
|
+
// chance-corrected units as the tier above.
|
|
197
|
+
const topBytes = read(ctx, top.id);
|
|
198
|
+
const exact = topBytes.length > 0
|
|
199
|
+
? cosine(queryGist, gistOf(ctx, topBytes))
|
|
200
|
+
: 0;
|
|
201
|
+
if (fracOfQuery(exact, topBytes.length) < reach) {
|
|
202
|
+
return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
|
|
203
|
+
}
|
|
204
|
+
// Echoing the query's own bytes back at it is not an echo of a RELATED
|
|
205
|
+
// form — it is the query restated, which answers nothing.
|
|
206
|
+
if (restates(topBytes)) {
|
|
207
|
+
return ground(null, "the nearest form IS the query itself — restating it answers nothing", [], 0);
|
|
208
|
+
}
|
|
209
|
+
// Honest echo.
|
|
210
|
+
return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
|
|
211
|
+
}
|
|
212
|
+
// ── Pipeline mechanism ──────────────────────────────────────────────────────
|
|
213
|
+
export const recallMechanism = {
|
|
214
|
+
name: "recall",
|
|
215
|
+
provenance: "recall",
|
|
216
|
+
// Recall's floor is free to state (one STEP-grade projection) and its run
|
|
217
|
+
// gates its own tiers — no expensive investment happens inside floor, so
|
|
218
|
+
// there is nothing to guard with worthRunning here: the pipeline's own
|
|
219
|
+
// check prunes run() against the incumbent.
|
|
220
|
+
async floor(_ctx, _query, _pre, _worthRunning) {
|
|
221
|
+
return STEP;
|
|
222
|
+
},
|
|
223
|
+
async run(ctx, query, pre) {
|
|
224
|
+
const r = await recallByResonance(ctx, query, pre);
|
|
225
|
+
if (!r)
|
|
226
|
+
return [];
|
|
227
|
+
return [{
|
|
228
|
+
bytes: r.bytes,
|
|
229
|
+
accounted: r.accounted,
|
|
230
|
+
moves: r.moves,
|
|
231
|
+
unexplained: r.unexplained,
|
|
232
|
+
provenance: r.echoed ? "recall-echo" : "recall",
|
|
233
|
+
}];
|
|
234
|
+
},
|
|
235
|
+
};
|