@hviana/sema 0.4.0 → 0.4.2

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