@hviana/sema 0.1.5 → 0.1.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.
Files changed (170) hide show
  1. package/AGENTS.md +6 -5
  2. package/CITATION.cff +49 -0
  3. package/HOW_IT_WORKS.md +11 -12
  4. package/README.md +7 -5
  5. package/dist/example/demo.js +25 -25
  6. package/dist/example/train_base.d.ts +19 -21
  7. package/dist/example/train_base.js +1700 -1952
  8. package/dist/src/alphabet.d.ts +3 -3
  9. package/dist/src/alphabet.js +24 -27
  10. package/dist/src/alu/src/alu.d.ts +169 -194
  11. package/dist/src/alu/src/alu.js +374 -414
  12. package/dist/src/alu/src/expr.d.ts +36 -43
  13. package/dist/src/alu/src/expr.js +252 -278
  14. package/dist/src/alu/src/index.d.ts +9 -98
  15. package/dist/src/alu/src/index.js +10 -64
  16. package/dist/src/alu/src/kernel-arith.d.ts +1 -5
  17. package/dist/src/alu/src/kernel-arith.js +195 -285
  18. package/dist/src/alu/src/kernel-bits.d.ts +1 -5
  19. package/dist/src/alu/src/kernel-bits.js +81 -115
  20. package/dist/src/alu/src/kernel-logic.js +33 -63
  21. package/dist/src/alu/src/kernel-nd.js +155 -195
  22. package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
  23. package/dist/src/alu/src/kernel-numeric.js +259 -331
  24. package/dist/src/alu/src/operation.d.ts +112 -132
  25. package/dist/src/alu/src/operation.js +149 -163
  26. package/dist/src/alu/src/parser.d.ts +168 -175
  27. package/dist/src/alu/src/parser.js +382 -421
  28. package/dist/src/alu/src/resonance.d.ts +16 -25
  29. package/dist/src/alu/src/resonance.js +49 -56
  30. package/dist/src/alu/src/text.d.ts +3 -7
  31. package/dist/src/alu/src/text.js +31 -37
  32. package/dist/src/alu/src/value.d.ts +14 -14
  33. package/dist/src/alu/src/value.js +150 -160
  34. package/dist/src/alu/test/alu.test.js +543 -656
  35. package/dist/src/bytes.d.ts +1 -5
  36. package/dist/src/bytes.js +33 -40
  37. package/dist/src/config.d.ts +99 -102
  38. package/dist/src/config.js +78 -83
  39. package/dist/src/derive/src/deduction.d.ts +60 -62
  40. package/dist/src/derive/src/deduction.js +103 -109
  41. package/dist/src/derive/src/index.d.ts +1 -7
  42. package/dist/src/derive/src/priority-queue.d.ts +8 -8
  43. package/dist/src/derive/src/priority-queue.js +57 -61
  44. package/dist/src/derive/src/rewrite.d.ts +15 -18
  45. package/dist/src/derive/src/rewrite.js +59 -67
  46. package/dist/src/derive/src/trie.d.ts +56 -56
  47. package/dist/src/derive/src/trie.js +175 -179
  48. package/dist/src/derive/test/derive.test.js +96 -100
  49. package/dist/src/extension.d.ts +13 -16
  50. package/dist/src/geometry.d.ts +29 -43
  51. package/dist/src/geometry.js +324 -268
  52. package/dist/src/index.d.ts +2 -17
  53. package/dist/src/index.js +2 -12
  54. package/dist/src/ingest-cache.d.ts +25 -30
  55. package/dist/src/ingest-cache.js +108 -127
  56. package/dist/src/mind/articulation.d.ts +1 -5
  57. package/dist/src/mind/articulation.js +77 -112
  58. package/dist/src/mind/attention.d.ts +40 -108
  59. package/dist/src/mind/attention.js +754 -872
  60. package/dist/src/mind/canonical.d.ts +4 -19
  61. package/dist/src/mind/canonical.js +27 -31
  62. package/dist/src/mind/graph-search.d.ts +201 -225
  63. package/dist/src/mind/graph-search.js +742 -821
  64. package/dist/src/mind/index.d.ts +2 -10
  65. package/dist/src/mind/junction.d.ts +31 -58
  66. package/dist/src/mind/junction.js +172 -237
  67. package/dist/src/mind/learning.d.ts +16 -52
  68. package/dist/src/mind/learning.js +143 -165
  69. package/dist/src/mind/match.d.ts +20 -69
  70. package/dist/src/mind/match.js +259 -318
  71. package/dist/src/mind/mechanisms/alu.js +16 -16
  72. package/dist/src/mind/mechanisms/cast.d.ts +9 -13
  73. package/dist/src/mind/mechanisms/cast.js +363 -456
  74. package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
  75. package/dist/src/mind/mechanisms/confluence.js +152 -183
  76. package/dist/src/mind/mechanisms/cover.d.ts +2 -8
  77. package/dist/src/mind/mechanisms/cover.js +148 -210
  78. package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
  79. package/dist/src/mind/mechanisms/extraction.js +234 -288
  80. package/dist/src/mind/mechanisms/recall.d.ts +6 -10
  81. package/dist/src/mind/mechanisms/recall.js +141 -185
  82. package/dist/src/mind/mind.d.ts +194 -155
  83. package/dist/src/mind/mind.js +392 -289
  84. package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
  85. package/dist/src/mind/pipeline-mechanism.js +161 -172
  86. package/dist/src/mind/pipeline.d.ts +4 -14
  87. package/dist/src/mind/pipeline.js +125 -189
  88. package/dist/src/mind/primitives.d.ts +20 -33
  89. package/dist/src/mind/primitives.js +124 -120
  90. package/dist/src/mind/rationale.d.ts +86 -98
  91. package/dist/src/mind/rationale.js +113 -121
  92. package/dist/src/mind/reasoning.d.ts +2 -13
  93. package/dist/src/mind/reasoning.js +129 -166
  94. package/dist/src/mind/recognition.d.ts +1 -4
  95. package/dist/src/mind/recognition.js +198 -210
  96. package/dist/src/mind/resonance.d.ts +5 -22
  97. package/dist/src/mind/resonance.js +0 -0
  98. package/dist/src/mind/trace.d.ts +6 -25
  99. package/dist/src/mind/trace.js +50 -58
  100. package/dist/src/mind/traverse.d.ts +29 -58
  101. package/dist/src/mind/traverse.js +344 -356
  102. package/dist/src/mind/types.d.ts +122 -127
  103. package/dist/src/mind/types.js +60 -69
  104. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  105. package/dist/src/rabitq-ivf/src/database.js +201 -0
  106. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  107. package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
  108. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  109. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  110. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  111. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  112. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  113. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  114. package/dist/src/sema.d.ts +9 -13
  115. package/dist/src/sema.js +26 -40
  116. package/dist/src/store-sqlite.d.ts +171 -166
  117. package/dist/src/store-sqlite.js +768 -662
  118. package/dist/src/store.d.ts +597 -630
  119. package/dist/src/store.js +1434 -1581
  120. package/dist/src/vec.d.ts +5 -9
  121. package/dist/src/vec.js +61 -73
  122. package/example/train_base.ts +33 -15
  123. package/index.html +65 -0
  124. package/package.json +1 -1
  125. package/src/alu/README.md +1 -1
  126. package/src/alu/src/index.ts +1 -1
  127. package/src/config.ts +19 -27
  128. package/src/geometry.ts +93 -1
  129. package/src/index.ts +6 -11
  130. package/src/mind/attention.ts +11 -6
  131. package/src/mind/mechanisms/recall.ts +29 -13
  132. package/src/mind/mind.ts +250 -19
  133. package/src/mind/primitives.ts +43 -6
  134. package/src/mind/recognition.ts +26 -8
  135. package/src/mind/traverse.ts +47 -0
  136. package/src/mind/types.ts +20 -21
  137. package/src/rabitq-ivf/README.md +56 -0
  138. package/src/rabitq-ivf/src/database.ts +276 -0
  139. package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
  140. package/src/rabitq-ivf/src/ivf.ts +1330 -0
  141. package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
  142. package/src/store-sqlite.ts +204 -9
  143. package/src/store.ts +27 -34
  144. package/test/08-storage.test.mjs +3 -3
  145. package/test/13-conversation.test.mjs +190 -20
  146. package/test/14-scaling.test.mjs +2 -2
  147. package/test/35-ivf.test.mjs +263 -0
  148. package/test/36-bloom.test.mjs +123 -0
  149. package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
  150. package/dist/src/rabitq-hnsw/src/database.js +0 -405
  151. package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
  152. package/dist/src/rabitq-hnsw/src/heap.js +0 -94
  153. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
  154. package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
  155. package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
  156. package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
  157. package/dist/src/rabitq-hnsw/src/prng.js +0 -38
  158. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
  159. package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
  160. package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
  161. package/dist/src/rabitq-hnsw/src/store.js +0 -916
  162. package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
  163. package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
  164. package/src/rabitq-hnsw/README.md +0 -303
  165. package/src/rabitq-hnsw/src/database.ts +0 -492
  166. package/src/rabitq-hnsw/src/heap.ts +0 -90
  167. package/src/rabitq-hnsw/src/hnsw.ts +0 -514
  168. package/src/rabitq-hnsw/src/store.ts +0 -994
  169. package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
  170. /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
@@ -10,28 +10,20 @@
10
10
  // Implementation split across src/mind/*.ts — this file assembles the Mind class.
11
11
  import { makeKeyring, rng, setVecConfig } from "../vec.js";
12
12
  import { Alphabet } from "../alphabet.js";
13
- import { reachThreshold } from "../geometry.js";
13
+ import { bytesToTreePyramid, reachThreshold, } from "../geometry.js";
14
14
  import { BoundedMap } from "../store.js";
15
15
  import { SQliteStore } from "../store-sqlite.js";
16
16
  import { resolveConfig } from "../config.js";
17
- import { bytesEqual } from "../bytes.js";
18
- import { GraphSearch } from "./graph-search.js";
17
+ import { bytesEqual, concat2 } from "../bytes.js";
18
+ import { GraphSearch, } from "./graph-search.js";
19
19
  import { Alu } from "../alu/src/index.js";
20
- import { decodeText, Rationale } from "./rationale.js";
21
- import {
22
- gistOf,
23
- inputBytes,
24
- perceive as perceiveImpl,
25
- resolve as resolveImpl,
26
- } from "./primitives.js";
20
+ import { decodeText, Rationale, } from "./rationale.js";
21
+ import { gistOf, inputBytes, latin1Key, perceive as perceiveImpl, resolve as resolveImpl, } from "./primitives.js";
27
22
  import { chooseNext, edgeAncestors as edgeAncestorsFn } from "./traverse.js";
28
23
  import { follow } from "./match.js";
29
24
  import { recognise, segment } from "./recognition.js";
30
25
  import { meaningOf } from "./resonance.js";
31
- import {
32
- climbAttention as climbAttentionFn,
33
- naturalBreak as naturalBreakFn,
34
- } from "./attention.js";
26
+ import { climbAttention as climbAttentionFn, naturalBreak as naturalBreakFn, } from "./attention.js";
35
27
  import { aluToMechanism, defaultMechanisms, think } from "./pipeline.js";
36
28
  import { articulate } from "./articulation.js";
37
29
  import { ingest } from "./learning.js";
@@ -40,291 +32,402 @@ import { rItem } from "./trace.js";
40
32
  // THE MIND
41
33
  // ═══════════════════════════════════════════════════════════════════════════
42
34
  export class Mind {
43
- space;
44
- alphabet;
45
- store;
46
- cfg;
47
- /** The lightest-derivation engine over the Sema graph. */
48
- search;
49
- /** The grounding mechanisms iterated by {@link think}. */
50
- mechanisms = [];
51
- /** The live rationale tracer for the inference currently in flight, or null. */
52
- trace = null;
53
- /** Per-response memo of the consensus climb. NOTE: this memo and
54
- * {@link recogniseMemo} are BYPASSED while a rationale trace is attached
55
- * (every mechanism must emit its own steps), so a traced respond re-pays
56
- * up to four consensus climbs plus repeat recognitions — that is where the
57
- * traced-vs-untraced latency multiple comes from, by design. */
58
- climbMemo = null;
59
- /** Per-response memo of recognise() — see {@link MindContext.recogniseMemo}. */
60
- recogniseMemo = null;
61
- /** Per-response memo of perceive() see {@link MindContext.perceiveMemo}. */
62
- perceiveMemo = null;
63
- /** The perceived gist of the query currently being answered. Set by `think`
64
- * before the graph search runs; `chooseNext` consults it as a gate (a null
65
- * guide means no query is in flight, so structural walkers keep plain
66
- * first-edge behaviour) and the reverse projection uses it for
67
- * reverse-recall disambiguation via `chooseAmong`. */
68
- _edgeGuide = null;
69
- /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
70
- * of a single response follows the SAME continuation for each ambiguous
71
- * context node. */
72
- _edgeChoice = new Map();
73
- /** Previous deposit's seen node ids for incremental change detection. */
74
- _prevSeen = null;
75
- /** Session cache of node-id perceived gist for candidate scoring — see
76
- * {@link MindContext._gistCache}. 32 MB 8K gists at D=1024; hub
77
- * candidate sets (√N at most) fit comfortably and recur across queries. */
78
- _gistCache = new BoundedMap(32_000_000, (v) => v.byteLength);
79
- // Deposit-path fold-pyramid cache (see MindContext) ENTRY-count
80
- // bounded: a pyramid costs ~KB per content byte (one D-float gist per
81
- // interior node), and only the few live conversation chains need to stay
82
- // warm, so 8 entries is the honest budget.
83
- _depositTrees = new BoundedMap(8);
84
- _depositLens = new Set();
85
- _internIds = new WeakMap();
86
- // ── GraphSearchHost implementation ─────────────────────────────────────
87
- /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
88
- resolve(bytes) {
89
- return resolveImpl(this, bytes);
90
- }
91
- // recogniseSpan wraps recognise
92
- recogniseSpan(bytes) {
93
- const r = recognise(this, bytes);
94
- return { sites: r.sites, leaves: r.leaves, splits: r.splits };
95
- }
96
- /** Disambiguate among multiple learnt continuations of the same context node.
97
- * Required by {@link GraphSearchHost} — the graph search calls this through the
98
- * host interface when a recognised form has more than one outgoing edge.
99
- * Delegates to the standalone {@link chooseNext} which picks the candidate
100
- * with the most distributional evidence (highest `prevOf` count — the
101
- * structural manifestation of its halo). When evidence is equal the
102
- * first-inserted edge wins. */
103
- chooseNext(node) {
104
- return chooseNext(this, node, this._edgeGuide);
105
- }
106
- constructor(optsOrCfg, storeArg, _fromStore) {
107
- let userMechanisms = [];
108
- let userFactories = [];
109
- if (_fromStore !== undefined) {
110
- this.cfg = resolveConfig(optsOrCfg);
111
- this.store = storeArg;
112
- } else {
113
- const {
114
- store: optsStore,
115
- mechanisms: userMechs,
116
- mechanismFactories: userFacts,
117
- ...rest
118
- } = optsOrCfg ?? {};
119
- this.cfg = resolveConfig(rest);
120
- this.store = optsStore ?? new SQliteStore({
121
- maxGroup: this.cfg.geometry.maxGroup,
122
- });
123
- userMechanisms = userMechs ?? [];
124
- userFactories = userFacts ?? [];
35
+ space;
36
+ alphabet;
37
+ store;
38
+ cfg;
39
+ /** The lightest-derivation engine over the Sema graph. */
40
+ search;
41
+ /** The grounding mechanisms iterated by {@link think}. */
42
+ mechanisms = [];
43
+ /** The live rationale tracer for the inference currently in flight, or null. */
44
+ trace = null;
45
+ /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
46
+ climbMemo = null;
47
+ /** Memo of recognise() content-keyed. See {@link MindContext.recogniseMemo}. */
48
+ recogniseMemo = null;
49
+ /** Memo of perceive() content-keyed. See {@link MindContext.perceiveMemo}. */
50
+ perceiveMemo = null;
51
+ /** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
52
+ _resolvedSubtrees = null;
53
+ /** The perceived gist of the query currently being answered. Set by `think`
54
+ * before the graph search runs; `chooseNext` consults it as a gate (a null
55
+ * guide means no query is in flight, so structural walkers keep plain
56
+ * first-edge behaviour) and the reverse projection uses it for
57
+ * reverse-recall disambiguation via `chooseAmong`. */
58
+ _edgeGuide = null;
59
+ /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
60
+ * of a single response follows the SAME continuation for each ambiguous
61
+ * context node. */
62
+ _edgeChoice = new Map();
63
+ /** Previous deposit's seen node ids for incremental change detection. */
64
+ _prevSeen = null;
65
+ /** Session cache of node-id perceived gist for candidate scoring see
66
+ * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
67
+ * candidate sets (√N at most) fit comfortably and recur across queries. */
68
+ _gistCache = new BoundedMap(32_000_000, (v) => v.byteLength);
69
+ // Deposit-path fold-pyramid cache (see MindContext) ENTRY-count
70
+ // bounded: a pyramid costs ~KB per content byte (one D-float gist per
71
+ // interior node), and only the few live conversation chains need to stay
72
+ // warm, so 8 entries is the honest budget.
73
+ _depositTrees = new BoundedMap(8);
74
+ _depositLens = new Set();
75
+ _internIds = new WeakMap();
76
+ // ── Conversation state ──────────────────────────────────────────────────
77
+ _nextConvId = 1;
78
+ _conversations = new Map();
79
+ // ── GraphSearchHost implementation ─────────────────────────────────────
80
+ /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
81
+ resolve(bytes) {
82
+ return resolveImpl(this, bytes);
125
83
  }
126
- setVecConfig({
127
- normalizeEpsilon: this.cfg.normalizeEpsilon,
128
- cosineEpsilon: this.cfg.cosineEpsilon,
129
- });
130
- const seedRand = rng((this.cfg.seed ^ 0x9e3779) >>> 0);
131
- const seats = makeKeyring(
132
- this.store.D,
133
- Math.max(8, this.cfg.geometry.maxGroup),
134
- seedRand,
135
- );
136
- this.space = {
137
- D: this.store.D,
138
- seats,
139
- rand: rng((this.cfg.seed ^ 0x51f15e) >>> 0),
140
- maxGroup: this.cfg.geometry.maxGroup,
141
- };
142
- this.alphabet = new Alphabet(
143
- this.cfg.seed,
144
- this.store.D,
145
- this.cfg.alphabet,
146
- );
147
- this.search = new GraphSearch(this.store, this.space.maxGroup, this);
148
- // Build the mechanism list: default grounding + ALU + user mechanisms.
149
- for (const m of defaultMechanisms) {
150
- this.mechanisms.push(m);
84
+ // recogniseSpan wraps recognise
85
+ recogniseSpan(bytes) {
86
+ const r = recognise(this, bytes);
87
+ return { sites: r.sites, leaves: r.leaves, splits: r.splits };
151
88
  }
152
- const host = this.extensionHost();
153
- if (this.cfg.alu.enabled) {
154
- const alu = new Alu({
155
- tol: this.cfg.alu.tol,
156
- maxIter: this.cfg.alu.maxIter,
157
- precision: this.cfg.alu.precision,
158
- }, host);
159
- this.mechanisms.push(aluToMechanism(alu));
89
+ /** Disambiguate among multiple learnt continuations of the same context node.
90
+ * Required by {@link GraphSearchHost} — the graph search calls this through the
91
+ * host interface when a recognised form has more than one outgoing edge.
92
+ * Delegates to the standalone {@link chooseNext} which picks the candidate
93
+ * with the most distributional evidence (highest `prevOf` count — the
94
+ * structural manifestation of its halo). When evidence is equal the
95
+ * first-inserted edge wins. */
96
+ chooseNext(node) {
97
+ return chooseNext(this, node, this._edgeGuide);
160
98
  }
161
- for (const m of userMechanisms) {
162
- this.mechanisms.push(m);
99
+ constructor(optsOrCfg, storeArg, _fromStore) {
100
+ let userMechanisms = [];
101
+ let userFactories = [];
102
+ if (_fromStore !== undefined) {
103
+ this.cfg = resolveConfig(optsOrCfg);
104
+ this.store = storeArg;
105
+ }
106
+ else {
107
+ const { store: optsStore, mechanisms: userMechs, mechanismFactories: userFacts, ...rest } = (optsOrCfg ?? {});
108
+ this.cfg = resolveConfig(rest);
109
+ this.store = optsStore ?? new SQliteStore({
110
+ maxGroup: this.cfg.geometry.maxGroup,
111
+ });
112
+ userMechanisms = userMechs ?? [];
113
+ userFactories = userFacts ?? [];
114
+ }
115
+ setVecConfig({
116
+ normalizeEpsilon: this.cfg.normalizeEpsilon,
117
+ cosineEpsilon: this.cfg.cosineEpsilon,
118
+ });
119
+ const seedRand = rng((this.cfg.seed ^ 0x9e3779) >>> 0);
120
+ const seats = makeKeyring(this.store.D, Math.max(8, this.cfg.geometry.maxGroup), seedRand);
121
+ this.space = {
122
+ D: this.store.D,
123
+ seats,
124
+ rand: rng((this.cfg.seed ^ 0x51f15e) >>> 0),
125
+ maxGroup: this.cfg.geometry.maxGroup,
126
+ };
127
+ this.alphabet = new Alphabet(this.cfg.seed, this.store.D, this.cfg.alphabet);
128
+ this.search = new GraphSearch(this.store, this.space.maxGroup, this);
129
+ // Build the mechanism list: default grounding + ALU + user mechanisms.
130
+ for (const m of defaultMechanisms)
131
+ this.mechanisms.push(m);
132
+ const host = this.extensionHost();
133
+ if (this.cfg.alu.enabled) {
134
+ const alu = new Alu({
135
+ tol: this.cfg.alu.tol,
136
+ maxIter: this.cfg.alu.maxIter,
137
+ precision: this.cfg.alu.precision,
138
+ }, host);
139
+ this.mechanisms.push(aluToMechanism(alu));
140
+ }
141
+ for (const m of userMechanisms)
142
+ this.mechanisms.push(m);
143
+ for (const f of userFactories)
144
+ this.mechanisms.push(f(host));
163
145
  }
164
- for (const f of userFactories) {
165
- this.mechanisms.push(f(host));
146
+ // ── Public API ───────────────────────────────────────────────────────────
147
+ /** Exposed for tests: the consensus climb over query sub-regions. */
148
+ climbAttention(query, k, mode = "inverse") {
149
+ return climbAttentionFn(this, query, k, mode);
166
150
  }
167
- }
168
- // ── Public API ───────────────────────────────────────────────────────────
169
- /** Exposed for tests: the consensus climb over query sub-regions. */
170
- climbAttention(query, k, mode = "inverse") {
171
- return climbAttentionFn(this, query, k, mode);
172
- }
173
- /** Exposed for tests: climb the structural DAG from a node to its
174
- * edge-bearing ancestor contexts. */
175
- edgeAncestors(id, contextCount) {
176
- return edgeAncestorsFn(this, id, contextCount);
177
- }
178
- /** Exposed for tests: find the natural break point in a sorted vote list. */
179
- naturalBreak(votes) {
180
- return naturalBreakFn(votes);
181
- }
182
- // ── respond ───────────────────────────────────────────────────────────
183
- /** Perceive input into a content-defined tree. Deterministic — identical
184
- * bytes always produce an identical tree. Public for ingest-cache. */
185
- perceive(input, leafAt, lookup) {
186
- return perceiveImpl(this, input, leafAt, lookup);
187
- }
188
- /** Open one response's transient state — the tracer and the per-response
189
- * memos. Paired with {@link endResponse}; the ONE place this state is
190
- * created, so adding a memo cannot forget its reset. */
191
- beginResponse(inspectRationale) {
192
- this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
193
- this.climbMemo = new WeakMap();
194
- this.recogniseMemo = new WeakMap();
195
- this.perceiveMemo = new Map();
196
- }
197
- /** Close one response's transient state — every per-response field, incl.
198
- * the edge guide/choices `think` sets mid-flight. */
199
- endResponse() {
200
- this.trace = null;
201
- this.climbMemo = null;
202
- this.recogniseMemo = null;
203
- this.perceiveMemo = null;
204
- this._edgeGuide = null;
205
- this._edgeChoice.clear();
206
- }
207
- async respond(input, inspectRationale) {
208
- this.beginResponse(inspectRationale);
209
- try {
210
- const inBytes = inputBytes(this, input);
211
- const top = this.trace?.enter("respond", [
212
- rItem(inBytes, "query"),
213
- ]);
214
- const thought = await think(this, inBytes, this.mechanisms);
215
- if (thought === null) {
216
- top?.done([], "nothing to perceive or an empty store — no answer");
217
- return { v: null, bytes: new Uint8Array(0) };
218
- }
219
- const voiced = await articulate(this, thought.bytes, inBytes);
220
- top?.done([
221
- rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined),
222
- ], "the answer, re-voiced in the asker's words");
223
- return {
224
- v: gistOf(this, voiced),
225
- bytes: voiced,
226
- provenance: thought.provenance,
227
- };
228
- } finally {
229
- this.endResponse();
151
+ /** Exposed for tests: climb the structural DAG from a node to its
152
+ * edge-bearing ancestor contexts. */
153
+ edgeAncestors(id, contextCount) {
154
+ return edgeAncestorsFn(this, id, contextCount);
230
155
  }
231
- }
232
- /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
233
- * decoding — they are structural padding in text answers. LOSSY for a
234
- * binary answer that legitimately contains NULs: use {@link respond} and
235
- * read `bytes` directly for binary/grid modalities. */
236
- async respondText(input, inspectRationale) {
237
- const r = await this.respond(input, inspectRationale);
238
- return decodeText(r.bytes);
239
- }
240
- async embedding(input) {
241
- return (await this.respond(input)).v;
242
- }
243
- /** Kinship note: the vector arm below is a miniature of recall's tier 3
244
- * (resonate → reach gate → read out the nearest form's bytes) — the
245
- * read-out direction of the same operation, without recall's grounding
246
- * ladder. If either side's acceptance rule changes, revisit the other. */
247
- async express(idOrV) {
248
- if (typeof idOrV === "number") {
249
- return this.store.bytes(idOrV);
156
+ /** Exposed for tests: find the natural break point in a sorted vote list. */
157
+ naturalBreak(votes) {
158
+ return naturalBreakFn(votes);
250
159
  }
251
- const [hit] = await this.store.resonate(idOrV, 1);
252
- // The same confidence floor recall uses: a vector whose nearest stored
253
- // form sits below the reach threshold relates to NOTHING in the store —
254
- // returning that form's bytes anyway would fabricate an answer from an
255
- // unrelated neighbour. Silence is the honest read-out.
256
- if (hit && hit.score >= reachThreshold(this.space.maxGroup)) {
257
- return this.store.bytes(hit.id);
160
+ // ── respond ───────────────────────────────────────────────────────────
161
+ /** Perceive input into a content-defined tree. Deterministic identical
162
+ * bytes always produce an identical tree. Public for ingest-cache. */
163
+ perceive(input, leafAt, lookup) {
164
+ return perceiveImpl(this, input, leafAt, lookup);
258
165
  }
259
- return new Uint8Array(0);
260
- }
261
- // ── Learning ─────────────────────────────────────────────────────────────
262
- async ingest(input, second) {
263
- return ingest(this, input, second);
264
- }
265
- // ── Extension Surface ────────────────────────────────────────────────────
266
- extensionHost() {
267
- const mind = this;
268
- return {
269
- meaningOf: (bytes, anchors) => meaningOf(this, bytes, anchors),
270
- continuation: (bytes) => this.groundedContinuation(bytes),
271
- segment: (bytes) =>
272
- segment(this, bytes).map((s) => ({ i: s.start, j: s.end })),
273
- get reach() {
274
- return mind.space.maxGroup;
275
- },
276
- };
277
- }
278
- async groundedContinuation(bytes) {
279
- const id = resolveImpl(this, bytes);
280
- if (id === null) {
281
- return null;
166
+ /** Open one response's transient state — the tracer and the per-response
167
+ * memos. Paired with {@link endResponse}; the ONE place this state is
168
+ * created, so adding a memo cannot forget its reset. */
169
+ beginResponse(inspectRationale) {
170
+ this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
171
+ this.climbMemo = new Map();
172
+ this.recogniseMemo = new Map();
173
+ this.perceiveMemo = new Map();
282
174
  }
283
- const grounded = await follow(this, id);
284
- if (grounded !== null && !bytesEqual(grounded, bytes)) {
285
- return grounded;
175
+ /** Close one response's transient state — every per-response field, incl.
176
+ * the edge guide/choices `think` sets mid-flight. */
177
+ endResponse() {
178
+ this.trace = null;
179
+ this.climbMemo = null;
180
+ this.recogniseMemo = null;
181
+ this.perceiveMemo = null;
182
+ this._edgeGuide = null;
183
+ this._edgeChoice.clear();
286
184
  }
287
- return null;
288
- }
289
- // ── Content-index repair ───────────────────────────────────────────────
290
- /** Re-index structurally-important nodes whose gists were evicted from the
291
- * pending cache before they reached the content index. See {@link
292
- * Store.repairContentIndex} for the contract; this method wires the
293
- * Mind's perception into the store's repair walk.
294
- *
295
- * Run this after training or at checkpoints to restore recall reach for
296
- * nodes that bridge experiences but were never indexed. A pure interior
297
- * node (no edges, no halo) is deliberately skipped — it is scaffolding,
298
- * not an experience root or bridge, and regenerating its gist would waste
299
- * I/O and index space for no recall benefit.
300
- *
301
- * @param minParents only repair nodes with ≥ this many structural parents
302
- * (default 2 structural bridges)
303
- * @returns number of nodes added to the content index */
304
- async repairContentIndex(minParents = 2) {
305
- return this.store.repairContentIndex(async (id) => {
306
- const bytes = this.store.bytes(id);
307
- if (bytes.length === 0) {
185
+ /** Shared response core — the one path from bytes to voiced answer.
186
+ * `respond` calls this directly; `respondTurn` has its own path
187
+ * with conversation-persistent memos and incremental perception. */
188
+ async _respondImpl(queryBytes, inspectRationale, traceLabel = "respond") {
189
+ this.beginResponse(inspectRationale);
190
+ try {
191
+ const top = this.trace?.enter(traceLabel, [
192
+ rItem(queryBytes, "query"),
193
+ ]);
194
+ const thought = await think(this, queryBytes, this.mechanisms);
195
+ if (thought === null) {
196
+ top?.done([], "nothing to perceive or an empty store no answer");
197
+ return { v: null, bytes: new Uint8Array(0) };
198
+ }
199
+ const voiced = await articulate(this, thought.bytes, queryBytes);
200
+ top?.done([rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined)], "the answer, re-voiced in the asker's words");
201
+ return {
202
+ v: gistOf(this, voiced),
203
+ bytes: voiced,
204
+ provenance: thought.provenance,
205
+ };
206
+ }
207
+ finally {
208
+ this.endResponse();
209
+ }
210
+ }
211
+ async respond(input, inspectRationale) {
212
+ return this._respondImpl(inputBytes(this, input), inspectRationale);
213
+ }
214
+ /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
215
+ * decoding — they are structural padding in text answers. LOSSY for a
216
+ * binary answer that legitimately contains NULs: use {@link respond} and
217
+ * read `bytes` directly for binary/grid modalities. */
218
+ async respondText(input, inspectRationale) {
219
+ const r = await this.respond(input, inspectRationale);
220
+ return decodeText(r.bytes);
221
+ }
222
+ // ── Conversation API ────────────────────────────────────────────────────
223
+ /** Begin a new conversation, optionally restoring from a previously-saved
224
+ * {@link ConversationState}. The returned handle is required for
225
+ * {@link respondTurn} and {@link endConversation}.
226
+ *
227
+ * Conversations are independent — a Mind can manage several concurrently.
228
+ * Each tracks the fold pyramid (accumulated internal processing) and
229
+ * turn-boundary offsets; the geometry never inspects content to guess
230
+ * where one turn ends and the next begins. */
231
+ beginConversation(state) {
232
+ const id = this._nextConvId++;
233
+ // Build the initial pyramid: from scratch for a new conversation,
234
+ // or from the saved context bytes when restoring.
235
+ const initBytes = state?.context ?? new Uint8Array(0);
236
+ const { pyramid } = bytesToTreePyramid(this.space, this.alphabet, initBytes);
237
+ this._conversations.set(id, {
238
+ pyramid,
239
+ bytes: initBytes,
240
+ boundaries: state?.boundaries ? [...state.boundaries] : [],
241
+ perceiveMemo: new Map(),
242
+ recogniseMemo: new Map(),
243
+ climbMemo: new Map(),
244
+ resolvedSubtrees: new WeakMap(),
245
+ });
246
+ return { id };
247
+ }
248
+ /** End a conversation, releasing its internal resources (accumulated
249
+ * context, boundary offsets, and the fold-pyramid cache). Idempotent. */
250
+ endConversation(conv) {
251
+ this._conversations.delete(conv.id);
252
+ }
253
+ /** The current serialisable state of an active conversation. Save this
254
+ * to resume the conversation later via {@link beginConversation}. */
255
+ conversationState(conv) {
256
+ const data = this._conversations.get(conv.id);
257
+ if (!data)
258
+ return null;
259
+ return {
260
+ context: data.bytes,
261
+ boundaries: [...data.boundaries],
262
+ };
263
+ }
264
+ /** Process one turn of a conversation.
265
+ *
266
+ * `turn` is the raw input for the latest turn — its bytes are appended
267
+ * to the accumulated context directly (raw concatenation). The Mind
268
+ * tracks the byte offset where each turn ends; no separator is ever
269
+ * inserted or inspected.
270
+ *
271
+ * Returns the response AND the updated {@link ConversationState} so the
272
+ * caller can persist it. The conversation handle's internal state is
273
+ * updated in place — the returned state is a snapshot for storage. */
274
+ async respondTurn(conv, turn, inspectRationale) {
275
+ const data = this._conversations.get(conv.id);
276
+ if (!data)
277
+ throw new Error(`Conversation ${conv.id} not found`);
278
+ const turnBytes = inputBytes(this, turn);
279
+ const prevLen = data.pyramid.bytes;
280
+ const prevBytes = data.bytes;
281
+ const newContext = prevLen > 0 ? concat2(prevBytes, turnBytes) : turnBytes;
282
+ data.bytes = newContext;
283
+ if (prevLen > 0)
284
+ data.boundaries.push(prevLen);
285
+ // Incremental perception — O(turn) instead of O(context).
286
+ const { tree, pyramid } = bytesToTreePyramid(this.space, this.alphabet, newContext, data.pyramid);
287
+ data.pyramid = pyramid;
288
+ // Swap in the conversation's persistent state so the inference
289
+ // pipeline does not re-process the prefix from scratch.
290
+ // perceiveMemo / recogniseMemo / climbMemo — content-keyed;
291
+ // the prefix's results from the previous turn are found by
292
+ // the current turn's sub-span calls.
293
+ // _resolvedSubtrees — Sema-node-keyed; when the pyramid reuses
294
+ // prefix subtrees (identical objects), foldTree returns their
295
+ // ids immediately — O(suffix) instead of O(context).
296
+ this.perceiveMemo = data.perceiveMemo;
297
+ this.recogniseMemo = data.recogniseMemo;
298
+ this.climbMemo = data.climbMemo;
299
+ this._resolvedSubtrees = data.resolvedSubtrees;
300
+ this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
301
+ try {
302
+ this.perceiveMemo.set(latin1Key(newContext), tree);
303
+ const top = this.trace?.enter("respondTurn", [
304
+ rItem(newContext, "query"),
305
+ ]);
306
+ const thought = await think(this, newContext, this.mechanisms);
307
+ if (thought === null) {
308
+ top?.done([], "nothing to perceive or an empty store — no answer");
309
+ return {
310
+ response: { v: null, bytes: new Uint8Array(0) },
311
+ state: this.conversationState(conv),
312
+ };
313
+ }
314
+ const voiced = await articulate(this, thought.bytes, newContext);
315
+ top?.done([rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined)], "the answer, re-voiced in the asker's words");
316
+ return {
317
+ response: {
318
+ v: gistOf(this, voiced),
319
+ bytes: voiced,
320
+ provenance: thought.provenance,
321
+ },
322
+ state: this.conversationState(conv),
323
+ };
324
+ }
325
+ finally {
326
+ // Save back to the conversation for the next turn.
327
+ data.perceiveMemo = this.perceiveMemo;
328
+ data.recogniseMemo = this.recogniseMemo;
329
+ data.climbMemo = this.climbMemo;
330
+ data.resolvedSubtrees = this._resolvedSubtrees;
331
+ // Clear Mind references — non-conversation respond() calls get
332
+ // fresh per-response memos.
333
+ this.trace = null;
334
+ this.perceiveMemo = null;
335
+ this.recogniseMemo = null;
336
+ this.climbMemo = null;
337
+ this._resolvedSubtrees = null;
338
+ this._edgeGuide = null;
339
+ this._edgeChoice.clear();
340
+ }
341
+ }
342
+ /** Text view of {@link respondTurn}. See {@link respondText} for the
343
+ * NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
344
+ * directly — this is a text-only convenience, like {@link respondText}. */
345
+ async respondTurnText(conv, turn, inspectRationale) {
346
+ const { response, state } = await this.respondTurn(conv, turn, inspectRationale);
347
+ return { response: decodeText(response.bytes), state };
348
+ }
349
+ async embedding(input) {
350
+ return (await this.respond(input)).v;
351
+ }
352
+ /** Kinship note: the vector arm below is a miniature of recall's tier 3
353
+ * (resonate → reach gate → read out the nearest form's bytes) — the
354
+ * read-out direction of the same operation, without recall's grounding
355
+ * ladder. If either side's acceptance rule changes, revisit the other. */
356
+ async express(idOrV) {
357
+ if (typeof idOrV === "number")
358
+ return this.store.bytes(idOrV);
359
+ const [hit] = await this.store.resonate(idOrV, 1);
360
+ // The same confidence floor recall uses: a vector whose nearest stored
361
+ // form sits below the reach threshold relates to NOTHING in the store —
362
+ // returning that form's bytes anyway would fabricate an answer from an
363
+ // unrelated neighbour. Silence is the honest read-out.
364
+ if (hit && hit.score >= reachThreshold(this.space.maxGroup)) {
365
+ return this.store.bytes(hit.id);
366
+ }
367
+ return new Uint8Array(0);
368
+ }
369
+ // ── Learning ─────────────────────────────────────────────────────────────
370
+ async ingest(input, second) {
371
+ return ingest(this, input, second);
372
+ }
373
+ // ── Extension Surface ────────────────────────────────────────────────────
374
+ extensionHost() {
375
+ const mind = this;
376
+ return {
377
+ meaningOf: (bytes, anchors) => meaningOf(this, bytes, anchors),
378
+ continuation: (bytes) => this.groundedContinuation(bytes),
379
+ segment: (bytes) => segment(this, bytes).map((s) => ({ i: s.start, j: s.end })),
380
+ get reach() {
381
+ return mind.space.maxGroup;
382
+ },
383
+ };
384
+ }
385
+ async groundedContinuation(bytes) {
386
+ const id = resolveImpl(this, bytes);
387
+ if (id === null)
388
+ return null;
389
+ const grounded = await follow(this, id);
390
+ if (grounded !== null && !bytesEqual(grounded, bytes))
391
+ return grounded;
308
392
  return null;
309
- }
310
- return gistOf(this, bytes);
311
- }, minParents);
312
- }
313
- // ── Persistence ──────────────────────────────────────────────────────────
314
- async save() {
315
- const meta = new TextEncoder().encode(JSON.stringify(this.cfg));
316
- await this.store.saveSnapshot(meta);
317
- return meta;
318
- }
319
- static async load(snapshot, store) {
320
- const cfg = JSON.parse(new TextDecoder().decode(snapshot));
321
- return new Mind(cfg, store, true);
322
- }
323
- static async loadFromStore(store) {
324
- const meta = await store.loadSnapshot();
325
- if (!meta) {
326
- throw new Error("no snapshot in store");
327
393
  }
328
- return Mind.load(meta, store);
329
- }
394
+ // ── Content-index repair ───────────────────────────────────────────────
395
+ /** Re-index structurally-important nodes whose gists were evicted from the
396
+ * pending cache before they reached the content index. See {@link
397
+ * Store.repairContentIndex} for the contract; this method wires the
398
+ * Mind's perception into the store's repair walk.
399
+ *
400
+ * Run this after training or at checkpoints to restore recall reach for
401
+ * nodes that bridge experiences but were never indexed. A pure interior
402
+ * node (no edges, no halo) is deliberately skipped — it is scaffolding,
403
+ * not an experience root or bridge, and regenerating its gist would waste
404
+ * I/O and index space for no recall benefit.
405
+ *
406
+ * @param minParents only repair nodes with ≥ this many structural parents
407
+ * (default 2 — structural bridges)
408
+ * @returns number of nodes added to the content index */
409
+ async repairContentIndex(minParents = 2) {
410
+ return this.store.repairContentIndex(async (id) => {
411
+ const bytes = this.store.bytes(id);
412
+ if (bytes.length === 0)
413
+ return null;
414
+ return gistOf(this, bytes);
415
+ }, minParents);
416
+ }
417
+ // ── Persistence ──────────────────────────────────────────────────────────
418
+ async save() {
419
+ const meta = new TextEncoder().encode(JSON.stringify(this.cfg));
420
+ await this.store.saveSnapshot(meta);
421
+ return meta;
422
+ }
423
+ static async load(snapshot, store) {
424
+ const cfg = JSON.parse(new TextDecoder().decode(snapshot));
425
+ return new Mind(cfg, store, true);
426
+ }
427
+ static async loadFromStore(store) {
428
+ const meta = await store.loadSnapshot();
429
+ if (!meta)
430
+ throw new Error("no snapshot in store");
431
+ return Mind.load(meta, store);
432
+ }
330
433
  }