@hviana/sema 0.1.3 → 0.1.5
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.js +25 -25
- package/dist/example/train_base.d.ts +21 -19
- package/dist/example/train_base.js +1952 -1684
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +27 -24
- package/dist/src/alu/src/alu.d.ts +194 -169
- package/dist/src/alu/src/alu.js +414 -374
- package/dist/src/alu/src/expr.d.ts +43 -36
- package/dist/src/alu/src/expr.js +278 -252
- package/dist/src/alu/src/index.d.ts +98 -9
- package/dist/src/alu/src/index.js +63 -9
- package/dist/src/alu/src/kernel-arith.d.ts +5 -1
- package/dist/src/alu/src/kernel-arith.js +285 -195
- package/dist/src/alu/src/kernel-bits.d.ts +5 -1
- package/dist/src/alu/src/kernel-bits.js +115 -81
- package/dist/src/alu/src/kernel-logic.js +63 -33
- package/dist/src/alu/src/kernel-nd.js +195 -155
- package/dist/src/alu/src/kernel-numeric.d.ts +80 -16
- package/dist/src/alu/src/kernel-numeric.js +331 -259
- package/dist/src/alu/src/operation.d.ts +132 -112
- package/dist/src/alu/src/operation.js +163 -149
- package/dist/src/alu/src/parser.d.ts +175 -168
- package/dist/src/alu/src/parser.js +421 -382
- package/dist/src/alu/src/resonance.d.ts +25 -16
- package/dist/src/alu/src/resonance.js +56 -49
- package/dist/src/alu/src/text.d.ts +7 -3
- package/dist/src/alu/src/text.js +37 -31
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +160 -150
- package/dist/src/alu/test/alu.test.js +656 -543
- package/dist/src/bytes.d.ts +5 -1
- package/dist/src/bytes.js +40 -33
- package/dist/src/config.d.ts +102 -102
- package/dist/src/config.js +83 -83
- package/dist/src/derive/src/deduction.d.ts +62 -60
- package/dist/src/derive/src/deduction.js +109 -103
- package/dist/src/derive/src/index.d.ts +7 -1
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +61 -57
- package/dist/src/derive/src/rewrite.d.ts +18 -15
- package/dist/src/derive/src/rewrite.js +67 -59
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +179 -175
- package/dist/src/derive/test/derive.test.js +100 -96
- package/dist/src/extension.d.ts +16 -13
- package/dist/src/geometry.d.ts +42 -17
- package/dist/src/geometry.js +266 -235
- package/dist/src/index.d.ts +17 -2
- package/dist/src/index.js +7 -1
- package/dist/src/ingest-cache.d.ts +30 -25
- package/dist/src/ingest-cache.js +127 -108
- package/dist/src/mind/articulation.d.ts +5 -1
- package/dist/src/mind/articulation.js +112 -77
- package/dist/src/mind/attention.d.ts +108 -40
- package/dist/src/mind/attention.js +871 -749
- package/dist/src/mind/canonical.d.ts +19 -4
- package/dist/src/mind/canonical.js +31 -27
- package/dist/src/mind/graph-search.d.ts +225 -201
- package/dist/src/mind/graph-search.js +821 -742
- package/dist/src/mind/index.d.ts +10 -2
- package/dist/src/mind/junction.d.ts +58 -31
- package/dist/src/mind/junction.js +237 -172
- package/dist/src/mind/learning.d.ts +52 -16
- package/dist/src/mind/learning.js +165 -143
- package/dist/src/mind/match.d.ts +69 -20
- package/dist/src/mind/match.js +318 -259
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +13 -9
- package/dist/src/mind/mechanisms/cast.js +456 -363
- package/dist/src/mind/mechanisms/confluence.d.ts +12 -8
- package/dist/src/mind/mechanisms/confluence.js +183 -152
- package/dist/src/mind/mechanisms/cover.d.ts +8 -2
- package/dist/src/mind/mechanisms/cover.js +210 -148
- package/dist/src/mind/mechanisms/extraction.d.ts +34 -8
- package/dist/src/mind/mechanisms/extraction.js +288 -234
- package/dist/src/mind/mechanisms/recall.d.ts +10 -6
- package/dist/src/mind/mechanisms/recall.js +185 -126
- package/dist/src/mind/mind.d.ts +154 -129
- package/dist/src/mind/mind.js +288 -258
- package/dist/src/mind/pipeline-mechanism.d.ts +124 -112
- package/dist/src/mind/pipeline-mechanism.js +172 -161
- package/dist/src/mind/pipeline.d.ts +14 -4
- package/dist/src/mind/pipeline.js +189 -125
- package/dist/src/mind/primitives.d.ts +32 -8
- package/dist/src/mind/primitives.js +117 -99
- package/dist/src/mind/rationale.d.ts +98 -86
- package/dist/src/mind/rationale.js +121 -113
- package/dist/src/mind/reasoning.d.ts +13 -2
- package/dist/src/mind/reasoning.js +166 -129
- package/dist/src/mind/recognition.d.ts +4 -1
- package/dist/src/mind/recognition.js +208 -183
- package/dist/src/mind/resonance.d.ts +22 -5
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +25 -6
- package/dist/src/mind/trace.js +58 -50
- package/dist/src/mind/traverse.d.ts +58 -16
- package/dist/src/mind/traverse.js +357 -304
- package/dist/src/mind/types.d.ts +127 -120
- package/dist/src/mind/types.js +69 -60
- package/dist/src/rabitq-hnsw/src/database.d.ts +179 -177
- package/dist/src/rabitq-hnsw/src/database.js +359 -342
- package/dist/src/rabitq-hnsw/src/heap.d.ts +12 -12
- package/dist/src/rabitq-hnsw/src/heap.js +80 -75
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +103 -103
- package/dist/src/rabitq-hnsw/src/hnsw.js +454 -428
- package/dist/src/rabitq-hnsw/src/index.d.ts +6 -1
- package/dist/src/rabitq-hnsw/src/prng.d.ts +10 -10
- package/dist/src/rabitq-hnsw/src/prng.js +27 -25
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +69 -69
- package/dist/src/rabitq-hnsw/src/rabitq.js +262 -246
- package/dist/src/rabitq-hnsw/src/store.d.ts +132 -132
- package/dist/src/rabitq-hnsw/src/store.js +843 -752
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +1054 -805
- package/dist/src/sema.d.ts +35 -0
- package/dist/src/sema.js +77 -0
- package/dist/src/store-sqlite.d.ts +166 -144
- package/dist/src/store-sqlite.js +660 -578
- package/dist/src/store.d.ts +630 -593
- package/dist/src/store.js +1581 -1440
- package/dist/src/vec.d.ts +9 -5
- package/dist/src/vec.js +73 -61
- package/package.json +2 -2
- package/src/sema.ts +102 -0
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
// index and grounds the nearest learned form. Four tiers, orderly degrading
|
|
5
5
|
// from exact self-match to honest echo.
|
|
6
6
|
import { cosine } from "../../vec.js";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
consensusFloor,
|
|
9
|
+
identityBar,
|
|
10
|
+
reachThreshold,
|
|
11
|
+
significanceBar,
|
|
12
|
+
} from "../../geometry.js";
|
|
8
13
|
import { gistOf, read, resolve } from "../primitives.js";
|
|
9
14
|
import { corpusN, hubBound } from "../traverse.js";
|
|
10
15
|
import { project, reverseContext } from "../match.js";
|
|
@@ -13,139 +18,193 @@ import { unexplainedLabel } from "../rationale.js";
|
|
|
13
18
|
import { rItem, rNode } from "../trace.js";
|
|
14
19
|
/** Recall the answer by resonating the whole query against the content index. */
|
|
15
20
|
export async function recallByResonance(ctx, query, pre) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
const t = ctx.trace?.enter("recallByResonance", [
|
|
22
|
+
rItem(query, "query"),
|
|
23
|
+
]);
|
|
24
|
+
const whole_ = [[0, query.length]];
|
|
25
|
+
const nothing = [];
|
|
26
|
+
const ground = (bytes, note, accounted, moves, echoed = false) => {
|
|
27
|
+
t?.done(
|
|
28
|
+
bytes === null
|
|
29
|
+
? []
|
|
30
|
+
: [rItem(bytes, "answer", resolve(ctx, bytes) ?? undefined)],
|
|
31
|
+
note,
|
|
32
|
+
);
|
|
33
|
+
return bytes === null ? null : {
|
|
34
|
+
bytes,
|
|
35
|
+
echoed,
|
|
36
|
+
accounted,
|
|
37
|
+
moves,
|
|
38
|
+
unexplained: unexplainedLabel(query, accounted),
|
|
32
39
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
};
|
|
41
|
+
const k = pre.k;
|
|
42
|
+
const queryGist = pre.guide;
|
|
43
|
+
// 0. Exact self-match — content-addressed, deterministic.
|
|
44
|
+
const qId = pre.queryResolved;
|
|
45
|
+
if (qId !== null) {
|
|
46
|
+
const rev = ctx.store.prevFirst(qId, hubBound(ctx));
|
|
47
|
+
const g = reverseContext(ctx, qId, queryGist, rev);
|
|
48
|
+
if (g !== null) {
|
|
49
|
+
return ground(
|
|
50
|
+
g,
|
|
51
|
+
rev.length === 1
|
|
52
|
+
? "exact self-match — reverse recall to the sole predecessor"
|
|
53
|
+
: "exact self-match — reverse recall to the best-resonating predecessor",
|
|
54
|
+
nothing,
|
|
55
|
+
STEP,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const whole = await ctx.store.resonate(queryGist, k);
|
|
60
|
+
if (whole.length === 0) {
|
|
61
|
+
return ground(null, "empty store — nothing to resonate with", [], 0);
|
|
62
|
+
}
|
|
63
|
+
const top = whole[0];
|
|
64
|
+
ctx.trace?.step(
|
|
65
|
+
"resonate",
|
|
66
|
+
[rItem(query, "query-gist")],
|
|
67
|
+
whole.map((h) => rNode(ctx, h.id, "hit", h.score)),
|
|
68
|
+
`resonate the whole-query gist → ${whole.length} nearest learnt form(s)`,
|
|
69
|
+
);
|
|
70
|
+
// 1. Clean resonance — the scale-aware identity claim. The ANGLE
|
|
71
|
+
// (top.score) carries the shared fraction; the query's MAGNITUDE (√len,
|
|
72
|
+
// the linear fold's own norm) converts the tolerated foreign fraction
|
|
73
|
+
// into bytes — at most one river window (see {@link identityBar}). A
|
|
74
|
+
// fixed cosine bar let long queries claim "near-identical" while whole
|
|
75
|
+
// windows — an answer word — differed.
|
|
76
|
+
if (top.score >= identityBar(ctx.store.D, ctx.space.maxGroup, query.length)) {
|
|
77
|
+
for (const h of whole) {
|
|
78
|
+
if (h.id === qId || h.score >= 1.0) {
|
|
79
|
+
const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
|
|
80
|
+
const g = reverseContext(ctx, h.id, queryGist, rev);
|
|
40
81
|
if (g !== null) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
82
|
+
return ground(
|
|
83
|
+
g,
|
|
84
|
+
rev.length === 1
|
|
85
|
+
? "perfect self-match — reverse recall to the sole predecessor"
|
|
86
|
+
: "perfect self-match — reverse recall to the best-resonating predecessor",
|
|
87
|
+
nothing,
|
|
88
|
+
STEP,
|
|
89
|
+
);
|
|
44
90
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// (top.score) carries the shared fraction; the query's MAGNITUDE (√len,
|
|
54
|
-
// the linear fold's own norm) converts the tolerated foreign fraction
|
|
55
|
-
// into bytes — at most one river window (see {@link identityBar}). A
|
|
56
|
-
// fixed cosine bar let long queries claim "near-identical" while whole
|
|
57
|
-
// windows — an answer word — differed.
|
|
58
|
-
if (top.score >= identityBar(ctx.store.D, ctx.space.maxGroup, query.length)) {
|
|
59
|
-
for (const h of whole) {
|
|
60
|
-
if (h.id === qId || h.score >= 1.0) {
|
|
61
|
-
const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
|
|
62
|
-
const g = reverseContext(ctx, h.id, queryGist, rev);
|
|
63
|
-
if (g !== null) {
|
|
64
|
-
return ground(g, rev.length === 1
|
|
65
|
-
? "perfect self-match — reverse recall to the sole predecessor"
|
|
66
|
-
: "perfect self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
|
|
67
|
-
}
|
|
68
|
-
const own = read(ctx, h.id);
|
|
69
|
-
if (own.length > 0) {
|
|
70
|
-
return ground(own, "perfect self-match — the query IS this node", nothing, STEP);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const g = await project(ctx, h.id, queryGist);
|
|
74
|
-
if (g) {
|
|
75
|
-
return ground(g, "clean whole-query resonance — ground the nearest hit", whole_, STEP);
|
|
76
|
-
}
|
|
91
|
+
const own = read(ctx, h.id);
|
|
92
|
+
if (own.length > 0) {
|
|
93
|
+
return ground(
|
|
94
|
+
own,
|
|
95
|
+
"perfect self-match — the query IS this node",
|
|
96
|
+
nothing,
|
|
97
|
+
STEP,
|
|
98
|
+
);
|
|
77
99
|
}
|
|
100
|
+
}
|
|
101
|
+
const g = await project(ctx, h.id, queryGist);
|
|
102
|
+
if (g) {
|
|
103
|
+
return ground(
|
|
104
|
+
g,
|
|
105
|
+
"clean whole-query resonance — ground the nearest hit",
|
|
106
|
+
whole_,
|
|
107
|
+
STEP,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
78
110
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
111
|
+
}
|
|
112
|
+
// 2. Scaffolding-dominated.
|
|
113
|
+
if (top.score >= significanceBar(ctx.store.D)) {
|
|
114
|
+
const N = corpusN(ctx);
|
|
115
|
+
const minVote = consensusFloor(N);
|
|
116
|
+
// The committed points of attention ARE the shared climb's roots (same
|
|
117
|
+
// query, same k, same DF mode) — read them from Precomputed instead of
|
|
118
|
+
// re-climbing, so even a traced response pays for the climb once.
|
|
119
|
+
const forest = (await pre.attention()).roots;
|
|
120
|
+
if (forest.length > 0 && forest[0].vote >= minVote) {
|
|
121
|
+
const g = await project(ctx, forest[0].anchor, queryGist);
|
|
122
|
+
if (g) {
|
|
123
|
+
return ground(
|
|
124
|
+
g,
|
|
125
|
+
"scaffolding-dominated query — ground the consensus-climb anchor",
|
|
126
|
+
[[forest[0].start, forest[0].end]],
|
|
127
|
+
CONCEPT,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
93
130
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
131
|
+
}
|
|
132
|
+
// 3. Last resort — gated on the FRACTION OF THE QUERY the grounding
|
|
133
|
+
// explains, not the raw cosine. Root gists are unit vectors, but their
|
|
134
|
+
// magnitudes are recoverable from the byte lengths (‖·‖ = √len under the
|
|
135
|
+
// linear fold): cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
|
|
136
|
+
// The raw cosine punished honest containment — a query fully inside a
|
|
137
|
+
// longer grounded answer scored √(lenQ/lenG) and was refused — and let a
|
|
138
|
+
// long answer sharing only scaffolding pass; the query-relative fraction
|
|
139
|
+
// measures exactly what the reach bar means: how much of THE QUERY the
|
|
140
|
+
// store accounts for.
|
|
141
|
+
const fracOfQuery = (cos, otherLen) =>
|
|
142
|
+
Math.min(1, cos * Math.sqrt(otherLen / Math.max(1, query.length)));
|
|
143
|
+
for (const h of whole) {
|
|
144
|
+
const g = await project(ctx, h.id, queryGist);
|
|
145
|
+
if (g) {
|
|
146
|
+
if (
|
|
147
|
+
fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
|
|
148
|
+
reachThreshold(ctx.space.maxGroup)
|
|
149
|
+
) {
|
|
150
|
+
return ground(
|
|
151
|
+
g,
|
|
152
|
+
"last resort: the nearest grounded whole-query hit",
|
|
153
|
+
[],
|
|
154
|
+
STEP,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
112
157
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
158
|
+
}
|
|
159
|
+
// The refusal/echo decision, in the same query-relative units — the top
|
|
160
|
+
// hit's magnitude read from the store (contentLen: √bytes IS the linear
|
|
161
|
+
// fold's gist norm), never from re-reading its bytes.
|
|
162
|
+
// The magnitude read SATURATES at the decision point: frac reaches the
|
|
163
|
+
// reach bar exactly when lenH = lenQ·(reach/score)², so the walk never
|
|
164
|
+
// needs to see past that — a huge conversation root costs a capped read,
|
|
165
|
+
// and a clamped return decides "pass" identically (frac ≥ reach).
|
|
166
|
+
const reach = reachThreshold(ctx.space.maxGroup);
|
|
167
|
+
const lenCap =
|
|
168
|
+
Math.ceil(query.length * (reach / Math.max(top.score, 1e-6)) ** 2) + 1;
|
|
169
|
+
if (fracOfQuery(top.score, ctx.store.contentLen(top.id, lenCap)) < reach) {
|
|
170
|
+
return ground(
|
|
171
|
+
null,
|
|
172
|
+
"below reach threshold — nothing in the store relates to this query",
|
|
173
|
+
[],
|
|
174
|
+
0,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
// Honest echo.
|
|
178
|
+
return ground(
|
|
179
|
+
read(ctx, top.id),
|
|
180
|
+
"last resort: the nearest resonant form's own bytes (echo, not grounded)",
|
|
181
|
+
[],
|
|
182
|
+
0,
|
|
183
|
+
true,
|
|
184
|
+
);
|
|
127
185
|
}
|
|
128
186
|
// ── Pipeline mechanism ──────────────────────────────────────────────────────
|
|
129
187
|
export const recallMechanism = {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
188
|
+
name: "recall",
|
|
189
|
+
provenance: "recall",
|
|
190
|
+
// Recall's floor is free to state (one STEP-grade projection) and its run
|
|
191
|
+
// gates its own tiers — no expensive investment happens inside floor, so
|
|
192
|
+
// there is nothing to guard with worthRunning here: the pipeline's own
|
|
193
|
+
// check prunes run() against the incumbent.
|
|
194
|
+
async floor(_ctx, _query, _pre, _worthRunning) {
|
|
195
|
+
return STEP;
|
|
196
|
+
},
|
|
197
|
+
async run(ctx, query, pre) {
|
|
198
|
+
const r = await recallByResonance(ctx, query, pre);
|
|
199
|
+
if (!r) {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
return [{
|
|
203
|
+
bytes: r.bytes,
|
|
204
|
+
accounted: r.accounted,
|
|
205
|
+
moves: r.moves,
|
|
206
|
+
unexplained: r.unexplained,
|
|
207
|
+
provenance: r.echoed ? "recall-echo" : "recall",
|
|
208
|
+
}];
|
|
209
|
+
},
|
|
151
210
|
};
|