@hviana/sema 0.2.6 → 0.2.7
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 +202 -0
- package/dist/src/geometry.js +575 -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 +400 -0
- package/dist/src/mind/attention.js +1763 -0
- package/dist/src/mind/bridge.d.ts +30 -0
- package/dist/src/mind/bridge.js +569 -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 +285 -0
- package/dist/src/mind/graph-search.js +956 -0
- package/dist/src/mind/index.d.ts +7 -0
- package/dist/src/mind/index.js +5 -0
- package/dist/src/mind/junction.d.ts +124 -0
- package/dist/src/mind/junction.js +317 -0
- package/dist/src/mind/learning.d.ts +47 -0
- package/dist/src/mind/learning.js +248 -0
- package/dist/src/mind/match.d.ts +126 -0
- package/dist/src/mind/match.js +441 -0
- package/dist/src/mind/mechanisms/alu.d.ts +4 -0
- package/dist/src/mind/mechanisms/alu.js +36 -0
- package/dist/src/mind/mechanisms/cast.d.ts +89 -0
- package/dist/src/mind/mechanisms/cast.js +716 -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 +203 -0
- package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
- package/dist/src/mind/mechanisms/extraction.js +387 -0
- package/dist/src/mind/mechanisms/recall.d.ts +13 -0
- package/dist/src/mind/mechanisms/recall.js +325 -0
- package/dist/src/mind/mind.d.ts +276 -0
- package/dist/src/mind/mind.js +572 -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 +217 -0
- package/dist/src/mind/primitives.d.ts +70 -0
- package/dist/src/mind/primitives.js +287 -0
- package/dist/src/mind/rationale.d.ts +139 -0
- package/dist/src/mind/rationale.js +163 -0
- package/dist/src/mind/reasoning.d.ts +34 -0
- package/dist/src/mind/reasoning.js +240 -0
- package/dist/src/mind/recognition.d.ts +20 -0
- package/dist/src/mind/recognition.js +390 -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 +568 -0
- package/dist/src/mind/types.d.ts +289 -0
- package/dist/src/mind/types.js +130 -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 +663 -0
- package/dist/src/store.js +1618 -0
- package/dist/src/vec.d.ts +31 -0
- package/dist/src/vec.js +109 -0
- package/package.json +1 -1
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
// mind/mind.ts — perceive, deposit, recall, think, express.
|
|
2
|
+
//
|
|
3
|
+
// Memory is a content-addressed node graph (see store.ts). Learning is
|
|
4
|
+
// DEPOSITION: perceive a stream into a tree and intern every node, so equal —
|
|
5
|
+
// and, by resonance, similar — subtrees collapse to one shared node. A fact is
|
|
6
|
+
// an EDGE between node ids; recall traverses edges; thinking completes the
|
|
7
|
+
// query's OWN tree, node by node, to a fixed point. No whole, no weights.
|
|
8
|
+
//
|
|
9
|
+
// Architecture: 4 primitives × 2 patterns = all inference.
|
|
10
|
+
// Implementation split across src/mind/*.ts — this file assembles the Mind class.
|
|
11
|
+
import { makeKeyring, rng, setVecConfig } from "../vec.js";
|
|
12
|
+
import { Alphabet } from "../alphabet.js";
|
|
13
|
+
import { bytesToTree, reachThreshold, } from "../geometry.js";
|
|
14
|
+
import { BoundedMap } from "../store.js";
|
|
15
|
+
import { SQliteStore } from "../store-sqlite.js";
|
|
16
|
+
import { resolveConfig } from "../config.js";
|
|
17
|
+
import { canonHash, textCanon } from "../canon.js";
|
|
18
|
+
import { bytesEqual, concat2 } from "../bytes.js";
|
|
19
|
+
import { GraphSearch, } from "./graph-search.js";
|
|
20
|
+
import { Alu } from "../alu/src/index.js";
|
|
21
|
+
import { decodeText, Rationale, } from "./rationale.js";
|
|
22
|
+
import { gistOf, inputBytes, latin1Key, perceive as perceiveImpl, resolve as resolveImpl, } from "./primitives.js";
|
|
23
|
+
import { chooseNext, edgeAncestors as edgeAncestorsFn } from "./traverse.js";
|
|
24
|
+
import { follow } from "./match.js";
|
|
25
|
+
import { recognise, segment } from "./recognition.js";
|
|
26
|
+
import { meaningOf } from "./resonance.js";
|
|
27
|
+
import { climbAttention as climbAttentionFn, naturalBreak as naturalBreakFn, } from "./attention.js";
|
|
28
|
+
import { aluToMechanism, defaultMechanisms, think } from "./pipeline.js";
|
|
29
|
+
import { articulate } from "./articulation.js";
|
|
30
|
+
import { ingest } from "./learning.js";
|
|
31
|
+
import { rItem } from "./trace.js";
|
|
32
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
33
|
+
// THE MIND
|
|
34
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
35
|
+
export class Mind {
|
|
36
|
+
space;
|
|
37
|
+
alphabet;
|
|
38
|
+
store;
|
|
39
|
+
cfg;
|
|
40
|
+
/** The lightest-derivation engine over the Sema graph. */
|
|
41
|
+
search;
|
|
42
|
+
/** The grounding mechanisms iterated by {@link think}. */
|
|
43
|
+
mechanisms = [];
|
|
44
|
+
/** The live rationale tracer for the inference currently in flight, or null. */
|
|
45
|
+
trace = null;
|
|
46
|
+
/** The content canonicalizer for the response in flight — see
|
|
47
|
+
* {@link MindContext.canon}. Injected per response by the modality entry
|
|
48
|
+
* point; null when the response carries no equivalence. */
|
|
49
|
+
canon = null;
|
|
50
|
+
/** Per-response canonical-resolution memo — see {@link MindContext.canonMemo}. */
|
|
51
|
+
canonMemo = null;
|
|
52
|
+
/** The Mind-level canon option: a canonicalizer to use for EVERY response,
|
|
53
|
+
* `false` to disable canonical resolution, or null to let each entry
|
|
54
|
+
* point decide (text entry points inject {@link textCanon}). */
|
|
55
|
+
_canonOpt = null;
|
|
56
|
+
/** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
|
|
57
|
+
climbMemo = null;
|
|
58
|
+
/** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
|
|
59
|
+
recogniseMemo = null;
|
|
60
|
+
/** Memo of perceive() — content-keyed. See {@link MindContext.perceiveMemo}. */
|
|
61
|
+
perceiveMemo = null;
|
|
62
|
+
/** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
|
|
63
|
+
_resolvedSubtrees = null;
|
|
64
|
+
/** The perceived gist of the query currently being answered. Set by `think`
|
|
65
|
+
* before the graph search runs; `chooseNext` consults it as a gate (a null
|
|
66
|
+
* guide means no query is in flight, so structural walkers keep plain
|
|
67
|
+
* first-edge behaviour) and the reverse projection uses it for
|
|
68
|
+
* reverse-recall disambiguation via `chooseAmong`. */
|
|
69
|
+
_edgeGuide = null;
|
|
70
|
+
/** Per-response memo of {@link chooseNext} picks — ensures every mechanism
|
|
71
|
+
* of a single response follows the SAME continuation for each ambiguous
|
|
72
|
+
* context node. */
|
|
73
|
+
_edgeChoice = new Map();
|
|
74
|
+
/** Previous deposit's seen node ids for incremental change detection. */
|
|
75
|
+
_prevSeen = null;
|
|
76
|
+
/** Session cache of node-id → perceived gist for candidate scoring — see
|
|
77
|
+
* {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
|
|
78
|
+
* candidate sets (√N at most) fit comfortably and recur across queries. */
|
|
79
|
+
_gistCache = new BoundedMap(32_000_000, (v) => v.byteLength);
|
|
80
|
+
// Deposit-path fold-pyramid cache (see MindContext) — ENTRY-count
|
|
81
|
+
// bounded: a pyramid costs ~KB per content byte (one D-float gist per
|
|
82
|
+
// interior node), and only the few live conversation chains need to stay
|
|
83
|
+
// warm, so 8 entries is the honest budget.
|
|
84
|
+
_depositTrees = new BoundedMap(8);
|
|
85
|
+
_depositLens = new Set();
|
|
86
|
+
_internIds = new WeakMap();
|
|
87
|
+
// ── Conversation state ──────────────────────────────────────────────────
|
|
88
|
+
_nextConvId = 1;
|
|
89
|
+
_conversations = new Map();
|
|
90
|
+
// ── GraphSearchHost implementation ─────────────────────────────────────
|
|
91
|
+
/** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
|
|
92
|
+
resolve(bytes) {
|
|
93
|
+
return resolveImpl(this, bytes);
|
|
94
|
+
}
|
|
95
|
+
// recogniseSpan wraps recognise
|
|
96
|
+
recogniseSpan(bytes) {
|
|
97
|
+
const r = recognise(this, bytes);
|
|
98
|
+
return {
|
|
99
|
+
sites: r.sites,
|
|
100
|
+
leaves: r.leaves,
|
|
101
|
+
splits: r.splits,
|
|
102
|
+
starts: r.starts,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Disambiguate among multiple learnt continuations of the same context node.
|
|
106
|
+
* Required by {@link GraphSearchHost} — the graph search calls this through the
|
|
107
|
+
* host interface when a recognised form has more than one outgoing edge.
|
|
108
|
+
* Delegates to the standalone {@link chooseNext} which picks the candidate
|
|
109
|
+
* with the most distributional evidence (highest `prevOf` count — the
|
|
110
|
+
* structural manifestation of its halo). When evidence is equal the
|
|
111
|
+
* first-inserted edge wins. */
|
|
112
|
+
chooseNext(node) {
|
|
113
|
+
return chooseNext(this, node, this._edgeGuide);
|
|
114
|
+
}
|
|
115
|
+
constructor(optsOrCfg, storeArg, _fromStore) {
|
|
116
|
+
let userMechanisms = [];
|
|
117
|
+
let userFactories = [];
|
|
118
|
+
if (_fromStore !== undefined) {
|
|
119
|
+
this.cfg = resolveConfig(optsOrCfg);
|
|
120
|
+
this.store = storeArg;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const { store: optsStore, mechanisms: userMechs, mechanismFactories: userFacts, canon: optsCanon, ...rest } = (optsOrCfg ?? {});
|
|
124
|
+
this._canonOpt = optsCanon ?? null;
|
|
125
|
+
this.cfg = resolveConfig(rest);
|
|
126
|
+
this.store = optsStore ?? new SQliteStore({
|
|
127
|
+
maxGroup: this.cfg.geometry.maxGroup,
|
|
128
|
+
});
|
|
129
|
+
userMechanisms = userMechs ?? [];
|
|
130
|
+
userFactories = userFacts ?? [];
|
|
131
|
+
}
|
|
132
|
+
setVecConfig({
|
|
133
|
+
normalizeEpsilon: this.cfg.normalizeEpsilon,
|
|
134
|
+
cosineEpsilon: this.cfg.cosineEpsilon,
|
|
135
|
+
});
|
|
136
|
+
const seedRand = rng((this.cfg.seed ^ 0x9e3779) >>> 0);
|
|
137
|
+
const seats = makeKeyring(this.store.D, Math.max(8, this.cfg.geometry.maxGroup), seedRand);
|
|
138
|
+
this.space = {
|
|
139
|
+
D: this.store.D,
|
|
140
|
+
seats,
|
|
141
|
+
rand: rng((this.cfg.seed ^ 0x51f15e) >>> 0),
|
|
142
|
+
maxGroup: this.cfg.geometry.maxGroup,
|
|
143
|
+
};
|
|
144
|
+
this.alphabet = new Alphabet(this.cfg.seed, this.store.D, this.cfg.alphabet);
|
|
145
|
+
this.search = new GraphSearch(this.store, this.space.maxGroup, this);
|
|
146
|
+
// Build the mechanism list: default grounding + ALU + user mechanisms.
|
|
147
|
+
for (const m of defaultMechanisms)
|
|
148
|
+
this.mechanisms.push(m);
|
|
149
|
+
const host = this.extensionHost();
|
|
150
|
+
if (this.cfg.alu.enabled) {
|
|
151
|
+
const alu = new Alu({
|
|
152
|
+
tol: this.cfg.alu.tol,
|
|
153
|
+
maxIter: this.cfg.alu.maxIter,
|
|
154
|
+
precision: this.cfg.alu.precision,
|
|
155
|
+
}, host);
|
|
156
|
+
this.mechanisms.push(aluToMechanism(alu));
|
|
157
|
+
}
|
|
158
|
+
for (const m of userMechanisms)
|
|
159
|
+
this.mechanisms.push(m);
|
|
160
|
+
for (const f of userFactories)
|
|
161
|
+
this.mechanisms.push(f(host));
|
|
162
|
+
}
|
|
163
|
+
// ── Public API ───────────────────────────────────────────────────────────
|
|
164
|
+
/** Exposed for tests: the consensus climb over query sub-regions. */
|
|
165
|
+
climbAttention(query, k, mode = "inverse") {
|
|
166
|
+
return climbAttentionFn(this, query, k, mode);
|
|
167
|
+
}
|
|
168
|
+
/** Exposed for tests: climb the structural DAG from a node to its
|
|
169
|
+
* edge-bearing ancestor contexts. */
|
|
170
|
+
edgeAncestors(id, contextCount) {
|
|
171
|
+
return edgeAncestorsFn(this, id, contextCount);
|
|
172
|
+
}
|
|
173
|
+
/** Exposed for tests: find the natural break point in a sorted vote list. */
|
|
174
|
+
naturalBreak(votes) {
|
|
175
|
+
return naturalBreakFn(votes);
|
|
176
|
+
}
|
|
177
|
+
// ── respond ───────────────────────────────────────────────────────────
|
|
178
|
+
/** Perceive input into a content-defined tree. Deterministic — identical
|
|
179
|
+
* bytes always produce an identical tree. Public for ingest-cache. */
|
|
180
|
+
perceive(input, leafAt, lookup) {
|
|
181
|
+
return perceiveImpl(this, input, leafAt, lookup);
|
|
182
|
+
}
|
|
183
|
+
/** Open one response's transient state — the tracer and the per-response
|
|
184
|
+
* memos. Paired with {@link endResponse}; the ONE place this state is
|
|
185
|
+
* created, so adding a memo cannot forget its reset. */
|
|
186
|
+
beginResponse(inspectRationale, canon) {
|
|
187
|
+
this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
|
|
188
|
+
this.climbMemo = new Map();
|
|
189
|
+
this.recogniseMemo = new Map();
|
|
190
|
+
this.perceiveMemo = new Map();
|
|
191
|
+
this.canon = canon ?? null;
|
|
192
|
+
this.canonMemo = canon ? new Map() : null;
|
|
193
|
+
}
|
|
194
|
+
/** The canonicalizer a response should carry: the Mind-level option when
|
|
195
|
+
* set (or none when explicitly disabled), else the entry point's own
|
|
196
|
+
* default — text entry points pass {@link textCanon}, binary ones null. */
|
|
197
|
+
_canonFor(entryDefault) {
|
|
198
|
+
if (this._canonOpt === false)
|
|
199
|
+
return null;
|
|
200
|
+
return this._canonOpt ?? entryDefault;
|
|
201
|
+
}
|
|
202
|
+
/** Close one response's transient state — every per-response field, incl.
|
|
203
|
+
* the edge guide/choices `think` sets mid-flight. */
|
|
204
|
+
endResponse() {
|
|
205
|
+
this.trace = null;
|
|
206
|
+
this.climbMemo = null;
|
|
207
|
+
this.recogniseMemo = null;
|
|
208
|
+
this.perceiveMemo = null;
|
|
209
|
+
this.canon = null;
|
|
210
|
+
this.canonMemo = null;
|
|
211
|
+
this._edgeGuide = null;
|
|
212
|
+
this._edgeChoice.clear();
|
|
213
|
+
}
|
|
214
|
+
/** Shared response core — the one path from bytes to voiced answer.
|
|
215
|
+
* `respond` calls this directly; `respondTurn` has its own path
|
|
216
|
+
* with conversation-persistent memos and incremental perception. */
|
|
217
|
+
async _respondImpl(queryBytes, inspectRationale, traceLabel = "respond", canon = null) {
|
|
218
|
+
this.beginResponse(inspectRationale, canon);
|
|
219
|
+
try {
|
|
220
|
+
const top = this.trace?.enter(traceLabel, [
|
|
221
|
+
rItem(queryBytes, "query"),
|
|
222
|
+
]);
|
|
223
|
+
const thought = await think(this, queryBytes, this.mechanisms);
|
|
224
|
+
if (thought === null) {
|
|
225
|
+
top?.done([], "nothing to perceive or an empty store — no answer");
|
|
226
|
+
return { v: null, bytes: new Uint8Array(0) };
|
|
227
|
+
}
|
|
228
|
+
const voiced = await articulate(this, thought.bytes, queryBytes);
|
|
229
|
+
top?.done([rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined)], "the answer, re-voiced in the asker's words");
|
|
230
|
+
return {
|
|
231
|
+
v: gistOf(this, voiced),
|
|
232
|
+
bytes: voiced,
|
|
233
|
+
provenance: thought.provenance,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
this.endResponse();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async respond(input, inspectRationale) {
|
|
241
|
+
// A STRING input is text by nature: it carries the text equivalence even
|
|
242
|
+
// through the generic entry point. Raw bytes / grids carry only the
|
|
243
|
+
// Mind-level canon option, if any.
|
|
244
|
+
const canon = this._canonFor(typeof input === "string" ? textCanon : null);
|
|
245
|
+
return this._respondImpl(inputBytes(this, input), inspectRationale, "respond", canon);
|
|
246
|
+
}
|
|
247
|
+
/** Text view of {@link respond}. NUL bytes (0x00) are stripped before
|
|
248
|
+
* decoding — they are structural padding in text answers. LOSSY for a
|
|
249
|
+
* binary answer that legitimately contains NULs: use {@link respond} and
|
|
250
|
+
* read `bytes` directly for binary/grid modalities.
|
|
251
|
+
*
|
|
252
|
+
* Injects the TEXT canonicalizer (src/canon.ts) so resolution treats
|
|
253
|
+
* every character variation of the same text — case, width, whitespace —
|
|
254
|
+
* as one form, provided the store's canon index is built
|
|
255
|
+
* ({@link buildCanonIndex}). */
|
|
256
|
+
async respondText(input, inspectRationale) {
|
|
257
|
+
const r = await this.respond(input, inspectRationale);
|
|
258
|
+
return decodeText(r.bytes);
|
|
259
|
+
}
|
|
260
|
+
// ── Conversation API ────────────────────────────────────────────────────
|
|
261
|
+
/** Begin a new conversation, optionally restoring from a previously-saved
|
|
262
|
+
* {@link ConversationState}. The returned handle is required for
|
|
263
|
+
* {@link respondTurn} and {@link endConversation}.
|
|
264
|
+
*
|
|
265
|
+
* Conversations are independent — a Mind can manage several concurrently.
|
|
266
|
+
* Each tracks the fold pyramid (accumulated internal processing) and
|
|
267
|
+
* turn-boundary offsets; the geometry never inspects content to guess
|
|
268
|
+
* where one turn ends and the next begins. */
|
|
269
|
+
beginConversation(state) {
|
|
270
|
+
const id = this._nextConvId++;
|
|
271
|
+
const initBytes = state?.context ?? new Uint8Array(0);
|
|
272
|
+
const initBoundaries = state?.boundaries ? [...state.boundaries] : [];
|
|
273
|
+
const tree = bytesToTree(this.space, this.alphabet, initBytes, undefined, undefined, initBoundaries.length > 0 ? initBoundaries : undefined);
|
|
274
|
+
this._conversations.set(id, {
|
|
275
|
+
tree,
|
|
276
|
+
bytes: initBytes,
|
|
277
|
+
boundaries: initBoundaries,
|
|
278
|
+
perceiveMemo: new Map(),
|
|
279
|
+
recogniseMemo: new Map(),
|
|
280
|
+
climbMemo: new Map(),
|
|
281
|
+
resolvedSubtrees: new WeakMap(),
|
|
282
|
+
});
|
|
283
|
+
return { id };
|
|
284
|
+
}
|
|
285
|
+
/** End a conversation, releasing its internal resources (accumulated
|
|
286
|
+
* context, boundary offsets, and the fold-pyramid cache). Idempotent. */
|
|
287
|
+
endConversation(conv) {
|
|
288
|
+
this._conversations.delete(conv.id);
|
|
289
|
+
}
|
|
290
|
+
/** The current serialisable state of an active conversation. Save this
|
|
291
|
+
* to resume the conversation later via {@link beginConversation}. */
|
|
292
|
+
conversationState(conv) {
|
|
293
|
+
const data = this._conversations.get(conv.id);
|
|
294
|
+
if (!data)
|
|
295
|
+
return null;
|
|
296
|
+
return {
|
|
297
|
+
context: data.bytes,
|
|
298
|
+
boundaries: [...data.boundaries],
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/** Append a turn to a conversation's accumulated context WITHOUT
|
|
302
|
+
* responding — raw byte append plus a boundary offset, never a
|
|
303
|
+
* separator; the fold pyramid advances by O(turn).
|
|
304
|
+
*
|
|
305
|
+
* This is the primitive for turns the Mind should hear but not answer:
|
|
306
|
+
* replaying a transcript, feeding the OTHER speaker's line in a
|
|
307
|
+
* prediction harness, or restoring context piecewise. {@link
|
|
308
|
+
* respondTurn} = addTurn + think + its own reply appended the same way. */
|
|
309
|
+
addTurn(conv, turn) {
|
|
310
|
+
const data = this._conversations.get(conv.id);
|
|
311
|
+
if (!data)
|
|
312
|
+
throw new Error(`Conversation ${conv.id} not found`);
|
|
313
|
+
const turnBytes = inputBytes(this, turn);
|
|
314
|
+
this._growContext(data, turnBytes);
|
|
315
|
+
return this.conversationState(conv);
|
|
316
|
+
}
|
|
317
|
+
/** Grow a conversation's accumulated context by one turn's bytes — raw
|
|
318
|
+
* append plus a boundary offset, pyramid advanced by O(turn), the grown
|
|
319
|
+
* context's tree seeded into the conversation's perceive memo. The ONE
|
|
320
|
+
* place a context grows ({@link addTurn} and {@link respondTurn} both
|
|
321
|
+
* come through here), so the append semantics cannot drift. */
|
|
322
|
+
_growContext(data, turnBytes) {
|
|
323
|
+
const prevLen = data.bytes.length;
|
|
324
|
+
// An empty turn neither grows the context nor marks a boundary —
|
|
325
|
+
// boundaries are documented strictly increasing, and a zero-length
|
|
326
|
+
// "turn" is no turn. Nothing changed, so the existing tree stands.
|
|
327
|
+
const grow = turnBytes.length > 0;
|
|
328
|
+
if (!grow)
|
|
329
|
+
return data.tree;
|
|
330
|
+
const grown = prevLen > 0 ? concat2(data.bytes, turnBytes) : turnBytes;
|
|
331
|
+
if (prevLen > 0)
|
|
332
|
+
data.boundaries.push(prevLen);
|
|
333
|
+
const tree = bytesToTree(this.space, this.alphabet, grown, undefined, undefined, data.boundaries.length > 0 ? data.boundaries : undefined);
|
|
334
|
+
data.tree = tree;
|
|
335
|
+
data.bytes = grown;
|
|
336
|
+
data.perceiveMemo.set(latin1Key(grown), tree);
|
|
337
|
+
return tree;
|
|
338
|
+
}
|
|
339
|
+
/** Process one turn of a conversation.
|
|
340
|
+
*
|
|
341
|
+
* `turn` is the raw input for the latest turn — its bytes are appended
|
|
342
|
+
* to the accumulated context directly (raw concatenation). The Mind
|
|
343
|
+
* tracks the byte offset where each turn ends; no separator is ever
|
|
344
|
+
* inserted or inspected.
|
|
345
|
+
*
|
|
346
|
+
* Returns the response AND the updated {@link ConversationState} so the
|
|
347
|
+
* caller can persist it. The conversation handle's internal state is
|
|
348
|
+
* updated in place — the returned state is a snapshot for storage.
|
|
349
|
+
*
|
|
350
|
+
* SINGLE FLIGHT: at most one respondTurn may be in flight per Mind. The
|
|
351
|
+
* conversation's memo caches are swapped into the Mind-level per-response
|
|
352
|
+
* pointers for the duration of the turn, so a concurrently-running
|
|
353
|
+
* respond()/respondTurn() on the SAME Mind would interleave state.
|
|
354
|
+
* Different Minds (or sequential awaits, as in every test) are safe. */
|
|
355
|
+
async respondTurn(conv, turn, inspectRationale) {
|
|
356
|
+
const data = this._conversations.get(conv.id);
|
|
357
|
+
if (!data)
|
|
358
|
+
throw new Error(`Conversation ${conv.id} not found`);
|
|
359
|
+
const turnBytes = inputBytes(this, turn);
|
|
360
|
+
// Incremental perception — O(turn) instead of O(context).
|
|
361
|
+
const tree = this._growContext(data, turnBytes);
|
|
362
|
+
const newContext = data.bytes;
|
|
363
|
+
// Swap in the conversation's persistent state so the inference
|
|
364
|
+
// pipeline does not re-process the prefix from scratch.
|
|
365
|
+
// perceiveMemo / recogniseMemo / climbMemo — content-keyed;
|
|
366
|
+
// the prefix's results from the previous turn are found by
|
|
367
|
+
// the current turn's sub-span calls.
|
|
368
|
+
// _resolvedSubtrees — Sema-node-keyed; when the pyramid reuses
|
|
369
|
+
// prefix subtrees (identical objects), foldTree returns their
|
|
370
|
+
// ids immediately — O(suffix) instead of O(context).
|
|
371
|
+
this.perceiveMemo = data.perceiveMemo;
|
|
372
|
+
this.recogniseMemo = data.recogniseMemo;
|
|
373
|
+
this.climbMemo = data.climbMemo;
|
|
374
|
+
this._resolvedSubtrees = data.resolvedSubtrees;
|
|
375
|
+
this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
|
|
376
|
+
// A string turn is text by nature — carry the text equivalence, same as
|
|
377
|
+
// respond() (see _canonFor).
|
|
378
|
+
this.canon = this._canonFor(typeof turn === "string" ? textCanon : null);
|
|
379
|
+
this.canonMemo = this.canon ? new Map() : null;
|
|
380
|
+
try {
|
|
381
|
+
// No recognise-memo pre-seeding here: that used to be necessary
|
|
382
|
+
// because the flat/positional fold lost visibility into an earlier
|
|
383
|
+
// turn's own structure once later bytes shifted its position
|
|
384
|
+
// (foldTree no longer visited the turn's root node). The
|
|
385
|
+
// STABLE-PREFIX fold (see {@link ConversationData}) makes every
|
|
386
|
+
// turn's subtree independent of what follows it by construction, so
|
|
387
|
+
// recognise() finds it correctly on its own, first-touch, exactly
|
|
388
|
+
// once per turn — no workaround needed, and none pre-empting
|
|
389
|
+
// recognise()'s own memo with a partial result.
|
|
390
|
+
const top = this.trace?.enter("respondTurn", [
|
|
391
|
+
rItem(newContext, "query"),
|
|
392
|
+
]);
|
|
393
|
+
const thought = await think(this, newContext, this.mechanisms);
|
|
394
|
+
if (thought === null) {
|
|
395
|
+
top?.done([], "nothing to perceive or an empty store — no answer");
|
|
396
|
+
return {
|
|
397
|
+
response: { v: null, bytes: new Uint8Array(0) },
|
|
398
|
+
state: this.conversationState(conv),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const voiced = await articulate(this, thought.bytes, newContext);
|
|
402
|
+
top?.done([rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined)], "the answer, re-voiced in the asker's words");
|
|
403
|
+
// The REPLY joins the accumulated context the same way a turn does
|
|
404
|
+
// ({@link addTurn}): raw byte append plus a boundary offset — never a
|
|
405
|
+
// separator. A conversation's context is the full exchange, exactly
|
|
406
|
+
// the cumulative continuous shape multi-turn training deposits, so a
|
|
407
|
+
// later turn can refer to what was ANSWERED ("which of those two…"),
|
|
408
|
+
// not only to what was asked.
|
|
409
|
+
if (voiced.length > 0)
|
|
410
|
+
this.addTurn(conv, voiced);
|
|
411
|
+
return {
|
|
412
|
+
response: {
|
|
413
|
+
v: gistOf(this, voiced),
|
|
414
|
+
bytes: voiced,
|
|
415
|
+
provenance: thought.provenance,
|
|
416
|
+
},
|
|
417
|
+
state: this.conversationState(conv),
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
finally {
|
|
421
|
+
// No save-back: the conversation's memo MAPS were mutated in place —
|
|
422
|
+
// `data.*` still points at them. Re-assigning from the Mind-level
|
|
423
|
+
// pointers here was a no-op in the single-flight case and, under a
|
|
424
|
+
// concurrently-started respond() (which swaps its own fresh maps into
|
|
425
|
+
// those pointers), would inject a FOREIGN response's memos into this
|
|
426
|
+
// conversation. Clear Mind references — non-conversation respond()
|
|
427
|
+
// calls get fresh per-response memos.
|
|
428
|
+
this.trace = null;
|
|
429
|
+
this.perceiveMemo = null;
|
|
430
|
+
this.recogniseMemo = null;
|
|
431
|
+
this.climbMemo = null;
|
|
432
|
+
this.canon = null;
|
|
433
|
+
this.canonMemo = null;
|
|
434
|
+
this._resolvedSubtrees = null;
|
|
435
|
+
this._edgeGuide = null;
|
|
436
|
+
this._edgeChoice.clear();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/** Text view of {@link respondTurn}. See {@link respondText} for the
|
|
440
|
+
* NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
|
|
441
|
+
* directly — this is a text-only convenience, like {@link respondText}. */
|
|
442
|
+
async respondTurnText(conv, turn, inspectRationale) {
|
|
443
|
+
const { response, state } = await this.respondTurn(conv, turn, inspectRationale);
|
|
444
|
+
return { response: decodeText(response.bytes), state };
|
|
445
|
+
}
|
|
446
|
+
async embedding(input) {
|
|
447
|
+
return (await this.respond(input)).v;
|
|
448
|
+
}
|
|
449
|
+
/** Kinship note: the vector arm below is a miniature of recall's tier 3
|
|
450
|
+
* (resonate → reach gate → read out the nearest form's bytes) — the
|
|
451
|
+
* read-out direction of the same operation, without recall's grounding
|
|
452
|
+
* ladder. If either side's acceptance rule changes, revisit the other. */
|
|
453
|
+
async express(idOrV) {
|
|
454
|
+
if (typeof idOrV === "number")
|
|
455
|
+
return this.store.bytes(idOrV);
|
|
456
|
+
const [hit] = await this.store.resonate(idOrV, 1);
|
|
457
|
+
// The same confidence floor recall uses: a vector whose nearest stored
|
|
458
|
+
// form sits below the reach threshold relates to NOTHING in the store —
|
|
459
|
+
// returning that form's bytes anyway would fabricate an answer from an
|
|
460
|
+
// unrelated neighbour. Silence is the honest read-out.
|
|
461
|
+
if (hit && hit.score >= reachThreshold(this.space.maxGroup)) {
|
|
462
|
+
return this.store.bytes(hit.id);
|
|
463
|
+
}
|
|
464
|
+
return new Uint8Array(0);
|
|
465
|
+
}
|
|
466
|
+
// ── Learning ─────────────────────────────────────────────────────────────
|
|
467
|
+
async ingest(input, second) {
|
|
468
|
+
return ingest(this, input, second);
|
|
469
|
+
}
|
|
470
|
+
// ── Extension Surface ────────────────────────────────────────────────────
|
|
471
|
+
extensionHost() {
|
|
472
|
+
const mind = this;
|
|
473
|
+
return {
|
|
474
|
+
meaningOf: (bytes, anchors) => meaningOf(this, bytes, anchors),
|
|
475
|
+
continuation: (bytes) => this.groundedContinuation(bytes),
|
|
476
|
+
segment: (bytes) => segment(this, bytes).map((s) => ({ i: s.start, j: s.end })),
|
|
477
|
+
get reach() {
|
|
478
|
+
return mind.space.maxGroup;
|
|
479
|
+
},
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
async groundedContinuation(bytes) {
|
|
483
|
+
const id = resolveImpl(this, bytes);
|
|
484
|
+
if (id === null)
|
|
485
|
+
return null;
|
|
486
|
+
const grounded = await follow(this, id);
|
|
487
|
+
if (grounded !== null && !bytesEqual(grounded, bytes))
|
|
488
|
+
return grounded;
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
// ── Content-index repair ───────────────────────────────────────────────
|
|
492
|
+
/** Re-index structurally-important nodes whose gists were evicted from the
|
|
493
|
+
* pending cache before they reached the content index. See {@link
|
|
494
|
+
* Store.repairContentIndex} for the contract; this method wires the
|
|
495
|
+
* Mind's perception into the store's repair walk.
|
|
496
|
+
*
|
|
497
|
+
* Run this after training or at checkpoints to restore recall reach for
|
|
498
|
+
* nodes that bridge experiences but were never indexed. A pure interior
|
|
499
|
+
* node (no edges, no halo) is deliberately skipped — it is scaffolding,
|
|
500
|
+
* not an experience root or bridge, and regenerating its gist would waste
|
|
501
|
+
* I/O and index space for no recall benefit.
|
|
502
|
+
*
|
|
503
|
+
* @param minParents only repair nodes with ≥ this many structural parents
|
|
504
|
+
* (default 2 — structural bridges)
|
|
505
|
+
* @returns number of nodes added to the content index */
|
|
506
|
+
async repairContentIndex(minParents = 2) {
|
|
507
|
+
return this.store.repairContentIndex(async (id) => {
|
|
508
|
+
const bytes = this.store.bytes(id);
|
|
509
|
+
if (bytes.length === 0)
|
|
510
|
+
return null;
|
|
511
|
+
return gistOf(this, bytes);
|
|
512
|
+
}, minParents);
|
|
513
|
+
}
|
|
514
|
+
// ── Canonical-form index ───────────────────────────────────────────────
|
|
515
|
+
/** Build (or incrementally refresh) the store's canonical-form index: for
|
|
516
|
+
* every content-bearing node, record the hash of its CANONICAL key so
|
|
517
|
+
* resolution can find stored forms across surface variation (case, width,
|
|
518
|
+
* whitespace — whatever `canon` equates; see src/canon.ts).
|
|
519
|
+
*
|
|
520
|
+
* Incremental and idempotent: the last indexed node id is remembered in
|
|
521
|
+
* store meta (`canon.upto`), so a refresh after further training scans
|
|
522
|
+
* only the new rows. Run once after training, and again after ingests —
|
|
523
|
+
* the same operational shape as {@link repairContentIndex}.
|
|
524
|
+
*
|
|
525
|
+
* @param canon the canonicalizer to index under — MUST be the same one
|
|
526
|
+
* queries will carry (text queries carry {@link textCanon}
|
|
527
|
+
* unless the Mind was constructed with its own)
|
|
528
|
+
* @returns number of index rows added */
|
|
529
|
+
async buildCanonIndex(canon) {
|
|
530
|
+
const c = canon ?? this._canonFor(textCanon);
|
|
531
|
+
const store = this.store;
|
|
532
|
+
if (c === null || !store.canonAdd || !store.eachContent)
|
|
533
|
+
return 0;
|
|
534
|
+
const from = Number(await store.getMeta("canon.upto") ?? 0);
|
|
535
|
+
let added = 0;
|
|
536
|
+
let maxId = from - 1;
|
|
537
|
+
store.eachContent((id, bytes) => {
|
|
538
|
+
if (id > maxId)
|
|
539
|
+
maxId = id;
|
|
540
|
+
const key = c(bytes);
|
|
541
|
+
if (key.length === 0)
|
|
542
|
+
return;
|
|
543
|
+
// Only index content whose canonical key DIFFERS from its raw bytes —
|
|
544
|
+
// an already-canonical span is found by the exact lookup (and by the
|
|
545
|
+
// fallback's own exact probe of the canonical bytes), so indexing it
|
|
546
|
+
// would only add rows.
|
|
547
|
+
if (bytesEqual(key, bytes))
|
|
548
|
+
return;
|
|
549
|
+
store.canonAdd(canonHash(key), id);
|
|
550
|
+
added++;
|
|
551
|
+
}, from);
|
|
552
|
+
await store.setMeta("canon.upto", String(maxId + 1));
|
|
553
|
+
store.commit();
|
|
554
|
+
return added;
|
|
555
|
+
}
|
|
556
|
+
// ── Persistence ──────────────────────────────────────────────────────────
|
|
557
|
+
async save() {
|
|
558
|
+
const meta = new TextEncoder().encode(JSON.stringify(this.cfg));
|
|
559
|
+
await this.store.saveSnapshot(meta);
|
|
560
|
+
return meta;
|
|
561
|
+
}
|
|
562
|
+
static async load(snapshot, store) {
|
|
563
|
+
const cfg = JSON.parse(new TextDecoder().decode(snapshot));
|
|
564
|
+
return new Mind(cfg, store, true);
|
|
565
|
+
}
|
|
566
|
+
static async loadFromStore(store) {
|
|
567
|
+
const meta = await store.loadSnapshot();
|
|
568
|
+
if (!meta)
|
|
569
|
+
throw new Error("no snapshot in store");
|
|
570
|
+
return Mind.load(meta, store);
|
|
571
|
+
}
|
|
572
|
+
}
|