@hviana/sema 0.1.7 → 0.1.9

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 (145) hide show
  1. package/example/train_base.ts +29 -4
  2. package/package.json +1 -1
  3. package/src/alu/src/parser.ts +105 -4
  4. package/src/canon.ts +65 -0
  5. package/src/geometry.ts +26 -30
  6. package/src/index.ts +1 -0
  7. package/src/mind/learning.ts +39 -1
  8. package/src/mind/match.ts +29 -6
  9. package/src/mind/mechanisms/cast.ts +33 -13
  10. package/src/mind/mechanisms/confluence.ts +15 -1
  11. package/src/mind/mechanisms/recall.ts +116 -41
  12. package/src/mind/mind.ts +172 -29
  13. package/src/mind/primitives.ts +66 -1
  14. package/src/mind/recognition.ts +10 -0
  15. package/src/mind/types.ts +10 -0
  16. package/src/store-sqlite.ts +68 -0
  17. package/src/store.ts +27 -0
  18. package/test/13-conversation.test.mjs +77 -27
  19. package/test/35-prefix-edge.test.mjs +86 -0
  20. package/dist/example/demo.d.ts +0 -1
  21. package/dist/example/demo.js +0 -39
  22. package/dist/example/train_base.d.ts +0 -87
  23. package/dist/example/train_base.js +0 -2232
  24. package/dist/src/alphabet.d.ts +0 -7
  25. package/dist/src/alphabet.js +0 -33
  26. package/dist/src/alu/src/alu.d.ts +0 -185
  27. package/dist/src/alu/src/alu.js +0 -440
  28. package/dist/src/alu/src/expr.d.ts +0 -61
  29. package/dist/src/alu/src/expr.js +0 -318
  30. package/dist/src/alu/src/index.d.ts +0 -11
  31. package/dist/src/alu/src/index.js +0 -19
  32. package/dist/src/alu/src/kernel-arith.d.ts +0 -16
  33. package/dist/src/alu/src/kernel-arith.js +0 -264
  34. package/dist/src/alu/src/kernel-bits.d.ts +0 -19
  35. package/dist/src/alu/src/kernel-bits.js +0 -152
  36. package/dist/src/alu/src/kernel-logic.d.ts +0 -4
  37. package/dist/src/alu/src/kernel-logic.js +0 -60
  38. package/dist/src/alu/src/kernel-nd.d.ts +0 -3
  39. package/dist/src/alu/src/kernel-nd.js +0 -208
  40. package/dist/src/alu/src/kernel-numeric.d.ts +0 -54
  41. package/dist/src/alu/src/kernel-numeric.js +0 -366
  42. package/dist/src/alu/src/operation.d.ts +0 -168
  43. package/dist/src/alu/src/operation.js +0 -189
  44. package/dist/src/alu/src/parser.d.ts +0 -212
  45. package/dist/src/alu/src/parser.js +0 -469
  46. package/dist/src/alu/src/resonance.d.ts +0 -55
  47. package/dist/src/alu/src/resonance.js +0 -126
  48. package/dist/src/alu/src/text.d.ts +0 -31
  49. package/dist/src/alu/src/text.js +0 -73
  50. package/dist/src/alu/src/value.d.ts +0 -109
  51. package/dist/src/alu/src/value.js +0 -300
  52. package/dist/src/alu/test/alu.test.d.ts +0 -1
  53. package/dist/src/alu/test/alu.test.js +0 -764
  54. package/dist/src/bytes.d.ts +0 -14
  55. package/dist/src/bytes.js +0 -59
  56. package/dist/src/config.d.ts +0 -111
  57. package/dist/src/config.js +0 -91
  58. package/dist/src/derive/src/deduction.d.ts +0 -125
  59. package/dist/src/derive/src/deduction.js +0 -155
  60. package/dist/src/derive/src/index.d.ts +0 -7
  61. package/dist/src/derive/src/index.js +0 -11
  62. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  63. package/dist/src/derive/src/priority-queue.js +0 -73
  64. package/dist/src/derive/src/rewrite.d.ts +0 -56
  65. package/dist/src/derive/src/rewrite.js +0 -100
  66. package/dist/src/derive/src/trie.d.ts +0 -90
  67. package/dist/src/derive/src/trie.js +0 -217
  68. package/dist/src/derive/test/derive.test.d.ts +0 -1
  69. package/dist/src/derive/test/derive.test.js +0 -122
  70. package/dist/src/extension.d.ts +0 -37
  71. package/dist/src/extension.js +0 -7
  72. package/dist/src/geometry.d.ts +0 -148
  73. package/dist/src/geometry.js +0 -517
  74. package/dist/src/index.d.ts +0 -15
  75. package/dist/src/index.js +0 -17
  76. package/dist/src/ingest-cache.d.ts +0 -41
  77. package/dist/src/ingest-cache.js +0 -161
  78. package/dist/src/mind/articulation.d.ts +0 -6
  79. package/dist/src/mind/articulation.js +0 -99
  80. package/dist/src/mind/attention.d.ts +0 -72
  81. package/dist/src/mind/attention.js +0 -898
  82. package/dist/src/mind/canonical.d.ts +0 -29
  83. package/dist/src/mind/canonical.js +0 -88
  84. package/dist/src/mind/graph-search.d.ts +0 -270
  85. package/dist/src/mind/graph-search.js +0 -847
  86. package/dist/src/mind/index.d.ts +0 -5
  87. package/dist/src/mind/index.js +0 -5
  88. package/dist/src/mind/junction.d.ts +0 -95
  89. package/dist/src/mind/junction.js +0 -262
  90. package/dist/src/mind/learning.d.ts +0 -47
  91. package/dist/src/mind/learning.js +0 -201
  92. package/dist/src/mind/match.d.ts +0 -111
  93. package/dist/src/mind/match.js +0 -422
  94. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  95. package/dist/src/mind/mechanisms/alu.js +0 -29
  96. package/dist/src/mind/mechanisms/cast.d.ts +0 -35
  97. package/dist/src/mind/mechanisms/cast.js +0 -447
  98. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  99. package/dist/src/mind/mechanisms/confluence.js +0 -213
  100. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  101. package/dist/src/mind/mechanisms/cover.js +0 -179
  102. package/dist/src/mind/mechanisms/extraction.d.ts +0 -67
  103. package/dist/src/mind/mechanisms/extraction.js +0 -342
  104. package/dist/src/mind/mechanisms/recall.d.ts +0 -13
  105. package/dist/src/mind/mechanisms/recall.js +0 -166
  106. package/dist/src/mind/mind.d.ts +0 -211
  107. package/dist/src/mind/mind.js +0 -433
  108. package/dist/src/mind/pipeline-mechanism.d.ts +0 -142
  109. package/dist/src/mind/pipeline-mechanism.js +0 -213
  110. package/dist/src/mind/pipeline.d.ts +0 -20
  111. package/dist/src/mind/pipeline.js +0 -185
  112. package/dist/src/mind/primitives.d.ts +0 -54
  113. package/dist/src/mind/primitives.js +0 -184
  114. package/dist/src/mind/rationale.d.ts +0 -134
  115. package/dist/src/mind/rationale.js +0 -162
  116. package/dist/src/mind/reasoning.d.ts +0 -15
  117. package/dist/src/mind/reasoning.js +0 -162
  118. package/dist/src/mind/recognition.d.ts +0 -20
  119. package/dist/src/mind/recognition.js +0 -236
  120. package/dist/src/mind/resonance.d.ts +0 -23
  121. package/dist/src/mind/resonance.js +0 -0
  122. package/dist/src/mind/trace.d.ts +0 -15
  123. package/dist/src/mind/trace.js +0 -73
  124. package/dist/src/mind/traverse.d.ts +0 -113
  125. package/dist/src/mind/traverse.js +0 -488
  126. package/dist/src/mind/types.d.ts +0 -176
  127. package/dist/src/mind/types.js +0 -84
  128. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  129. package/dist/src/rabitq-ivf/src/database.js +0 -201
  130. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  131. package/dist/src/rabitq-ivf/src/index.js +0 -4
  132. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  133. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  134. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  135. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  136. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  137. package/dist/src/rabitq-ivf/src/rabitq.js +0 -283
  138. package/dist/src/sema.d.ts +0 -31
  139. package/dist/src/sema.js +0 -63
  140. package/dist/src/store-sqlite.d.ts +0 -176
  141. package/dist/src/store-sqlite.js +0 -890
  142. package/dist/src/store.d.ts +0 -642
  143. package/dist/src/store.js +0 -1612
  144. package/dist/src/vec.d.ts +0 -31
  145. package/dist/src/vec.js +0 -109
@@ -1,211 +0,0 @@
1
- import { Vec } from "../vec.js";
2
- import { Sema, Space } from "../sema.js";
3
- import { Alphabet } from "../alphabet.js";
4
- import { type FoldPyramid, Grid } from "../geometry.js";
5
- import { BoundedMap, type Store } from "../store.js";
6
- import { type MindConfig } from "../config.js";
7
- import { GraphSearch, type Leaf, type Site } from "./graph-search.js";
8
- import type { ComputedSpan, ExtensionHost } from "../extension.js";
9
- export type { ComputedSpan, ExtensionHost };
10
- import { type InspectRationale, Rationale } from "./rationale.js";
11
- export type { InspectRationale, RationaleItem, RationaleStep, } from "./rationale.js";
12
- export type Input = string | Uint8Array | Grid | Grid[];
13
- export interface Response {
14
- v: Vec | null;
15
- bytes: Uint8Array;
16
- /** How the answer was grounded (see {@link Provenance}). `"recall-echo"`
17
- * marks the last-resort fallback that returned the nearest stored form's
18
- * own bytes verbatim — an echo, NOT a grounded fact. Absent when there is
19
- * no answer. */
20
- provenance?: import("./pipeline.js").Provenance;
21
- }
22
- /** Serializable state of a conversation — can be saved and restored across
23
- * sessions. The Mind never interprets the bytes; it only tracks their
24
- * cumulative lengths so the caller can reconstruct turn boundaries later
25
- * without inspecting content. */
26
- export interface ConversationState {
27
- /** The accumulated context bytes — raw concatenation of every turn's
28
- * bytes in order. No separator is inserted; the boundary offsets
29
- * ({@link boundaries}) tell the caller where each turn ends. */
30
- context: Uint8Array;
31
- /** Cumulative byte length after each completed turn. Sorted, strictly
32
- * increasing, each {@code < context.length}. The first turn's length
33
- * is `boundaries[0]`; the second turn starts at that offset, and so
34
- * on. Empty for a single-turn or new conversation. */
35
- boundaries: number[];
36
- }
37
- /** An active conversation handle. Opaque — interact through the Mind's
38
- * conversation methods ({@link Mind.beginConversation},
39
- * {@link Mind.respondTurn}, {@link Mind.endConversation}). */
40
- export interface Conversation {
41
- readonly id: number;
42
- }
43
- import type { AttentionRead, MindContext, Recognition } from "./types.js";
44
- export interface MindOptions {
45
- seed?: number;
46
- recallQueryK?: number;
47
- haloQueryK?: number;
48
- normalizeEpsilon?: number;
49
- cosineEpsilon?: number;
50
- geometry?: Partial<import("../config.js").GeometryConfig>;
51
- alphabet?: Partial<import("../config.js").AlphabetConfig>;
52
- storeConfig?: Partial<import("../config.js").StoreConfig>;
53
- store?: Store;
54
- /** Additional grounding mechanisms (appended after the built-in defaults). */
55
- mechanisms?: import("./pipeline-mechanism.js").PipelineMechanism[];
56
- /** Factories that receive the {@link ExtensionHost} and return mechanisms. */
57
- mechanismFactories?: ((host: import("../extension.js").ExtensionHost) => import("./pipeline-mechanism.js").PipelineMechanism)[];
58
- }
59
- export declare class Mind implements MindContext {
60
- readonly space: Space;
61
- readonly alphabet: Alphabet;
62
- readonly store: Store;
63
- readonly cfg: MindConfig;
64
- /** The lightest-derivation engine over the Sema graph. */
65
- readonly search: GraphSearch;
66
- /** The grounding mechanisms iterated by {@link think}. */
67
- readonly mechanisms: import("./pipeline-mechanism.js").PipelineMechanism[];
68
- /** The live rationale tracer for the inference currently in flight, or null. */
69
- trace: Rationale | null;
70
- /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
71
- climbMemo: Map<string, Map<string, AttentionRead>> | null;
72
- /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
73
- recogniseMemo: Map<string, Recognition> | null;
74
- /** Memo of perceive() — content-keyed. See {@link MindContext.perceiveMemo}. */
75
- perceiveMemo: Map<string, import("../sema.js").Sema> | null;
76
- /** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
77
- _resolvedSubtrees: WeakMap<import("../sema.js").Sema, {
78
- id: number;
79
- len: number;
80
- }> | null;
81
- /** The perceived gist of the query currently being answered. Set by `think`
82
- * before the graph search runs; `chooseNext` consults it as a gate (a null
83
- * guide means no query is in flight, so structural walkers keep plain
84
- * first-edge behaviour) and the reverse projection uses it for
85
- * reverse-recall disambiguation via `chooseAmong`. */
86
- _edgeGuide: Vec | null;
87
- /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
88
- * of a single response follows the SAME continuation for each ambiguous
89
- * context node. */
90
- _edgeChoice: Map<number, number>;
91
- /** Previous deposit's seen node ids for incremental change detection. */
92
- _prevSeen: Set<number> | null;
93
- /** Session cache of node-id → perceived gist for candidate scoring — see
94
- * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
95
- * candidate sets (√N at most) fit comfortably and recur across queries. */
96
- _gistCache: BoundedMap<number, Vec>;
97
- _depositTrees: BoundedMap<string, FoldPyramid>;
98
- _depositLens: Set<number>;
99
- _internIds: WeakMap<Sema, number>;
100
- private _nextConvId;
101
- private _conversations;
102
- /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
103
- resolve(bytes: Uint8Array): number | null;
104
- recogniseSpan(bytes: Uint8Array): {
105
- sites: ReadonlyArray<Site>;
106
- leaves: ReadonlyArray<Leaf>;
107
- splits: ReadonlySet<number>;
108
- };
109
- /** Disambiguate among multiple learnt continuations of the same context node.
110
- * Required by {@link GraphSearchHost} — the graph search calls this through the
111
- * host interface when a recognised form has more than one outgoing edge.
112
- * Delegates to the standalone {@link chooseNext} which picks the candidate
113
- * with the most distributional evidence (highest `prevOf` count — the
114
- * structural manifestation of its halo). When evidence is equal the
115
- * first-inserted edge wins. */
116
- chooseNext(node: number): number | undefined;
117
- constructor(opts?: MindOptions);
118
- constructor(cfg: MindConfig, store: Store, _fromStore: true);
119
- /** Exposed for tests: the consensus climb over query sub-regions. */
120
- climbAttention(query: Uint8Array, k: number, mode?: import("./types.js").DFMode): Promise<import("./types.js").Attention[]>;
121
- /** Exposed for tests: climb the structural DAG from a node to its
122
- * edge-bearing ancestor contexts. */
123
- edgeAncestors(id: number, contextCount: number): import("./types.js").AncestorReach;
124
- /** Exposed for tests: find the natural break point in a sorted vote list. */
125
- naturalBreak(votes: number[]): number;
126
- /** Perceive input into a content-defined tree. Deterministic — identical
127
- * bytes always produce an identical tree. Public for ingest-cache. */
128
- perceive(input: Input, leafAt?: (i: number) => number | null, lookup?: (ids: number[]) => number | null): Sema;
129
- /** Open one response's transient state — the tracer and the per-response
130
- * memos. Paired with {@link endResponse}; the ONE place this state is
131
- * created, so adding a memo cannot forget its reset. */
132
- private beginResponse;
133
- /** Close one response's transient state — every per-response field, incl.
134
- * the edge guide/choices `think` sets mid-flight. */
135
- private endResponse;
136
- /** Shared response core — the one path from bytes to voiced answer.
137
- * `respond` calls this directly; `respondTurn` has its own path
138
- * with conversation-persistent memos and incremental perception. */
139
- private _respondImpl;
140
- respond(input: Input, inspectRationale?: InspectRationale): Promise<Response>;
141
- /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
142
- * decoding — they are structural padding in text answers. LOSSY for a
143
- * binary answer that legitimately contains NULs: use {@link respond} and
144
- * read `bytes` directly for binary/grid modalities. */
145
- respondText(input: string, inspectRationale?: InspectRationale): Promise<string>;
146
- /** Begin a new conversation, optionally restoring from a previously-saved
147
- * {@link ConversationState}. The returned handle is required for
148
- * {@link respondTurn} and {@link endConversation}.
149
- *
150
- * Conversations are independent — a Mind can manage several concurrently.
151
- * Each tracks the fold pyramid (accumulated internal processing) and
152
- * turn-boundary offsets; the geometry never inspects content to guess
153
- * where one turn ends and the next begins. */
154
- beginConversation(state?: ConversationState): Conversation;
155
- /** End a conversation, releasing its internal resources (accumulated
156
- * context, boundary offsets, and the fold-pyramid cache). Idempotent. */
157
- endConversation(conv: Conversation): void;
158
- /** The current serialisable state of an active conversation. Save this
159
- * to resume the conversation later via {@link beginConversation}. */
160
- conversationState(conv: Conversation): ConversationState | null;
161
- /** Process one turn of a conversation.
162
- *
163
- * `turn` is the raw input for the latest turn — its bytes are appended
164
- * to the accumulated context directly (raw concatenation). The Mind
165
- * tracks the byte offset where each turn ends; no separator is ever
166
- * inserted or inspected.
167
- *
168
- * Returns the response AND the updated {@link ConversationState} so the
169
- * caller can persist it. The conversation handle's internal state is
170
- * updated in place — the returned state is a snapshot for storage. */
171
- respondTurn(conv: Conversation, turn: Input, inspectRationale?: InspectRationale): Promise<{
172
- response: Response;
173
- state: ConversationState;
174
- }>;
175
- /** Text view of {@link respondTurn}. See {@link respondText} for the
176
- * NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
177
- * directly — this is a text-only convenience, like {@link respondText}. */
178
- respondTurnText(conv: Conversation, turn: string, inspectRationale?: InspectRationale): Promise<{
179
- response: string;
180
- state: ConversationState;
181
- }>;
182
- embedding(input: Input): Promise<Vec | null>;
183
- /** Kinship note: the vector arm below is a miniature of recall's tier 3
184
- * (resonate → reach gate → read out the nearest form's bytes) — the
185
- * read-out direction of the same operation, without recall's grounding
186
- * ladder. If either side's acceptance rule changes, revisit the other. */
187
- express(idOrV: number | Vec): Promise<Uint8Array>;
188
- ingest(input: Input | (Input | [Input, Input])[], second?: Input): Promise<(Sema & {
189
- id: number;
190
- }) | undefined>;
191
- private extensionHost;
192
- private groundedContinuation;
193
- /** Re-index structurally-important nodes whose gists were evicted from the
194
- * pending cache before they reached the content index. See {@link
195
- * Store.repairContentIndex} for the contract; this method wires the
196
- * Mind's perception into the store's repair walk.
197
- *
198
- * Run this after training or at checkpoints to restore recall reach for
199
- * nodes that bridge experiences but were never indexed. A pure interior
200
- * node (no edges, no halo) is deliberately skipped — it is scaffolding,
201
- * not an experience root or bridge, and regenerating its gist would waste
202
- * I/O and index space for no recall benefit.
203
- *
204
- * @param minParents only repair nodes with ≥ this many structural parents
205
- * (default 2 — structural bridges)
206
- * @returns number of nodes added to the content index */
207
- repairContentIndex(minParents?: number): Promise<number>;
208
- save(): Promise<Uint8Array>;
209
- static load(snapshot: Uint8Array, store: Store): Promise<Mind>;
210
- static loadFromStore(store: Store): Promise<Mind>;
211
- }
@@ -1,433 +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 { bytesToTreePyramid, 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 { bytesEqual, concat2 } from "../bytes.js";
18
- import { GraphSearch, } from "./graph-search.js";
19
- import { Alu } from "../alu/src/index.js";
20
- import { decodeText, Rationale, } from "./rationale.js";
21
- import { gistOf, inputBytes, latin1Key, perceive as perceiveImpl, resolve as resolveImpl, } from "./primitives.js";
22
- import { chooseNext, edgeAncestors as edgeAncestorsFn } from "./traverse.js";
23
- import { follow } from "./match.js";
24
- import { recognise, segment } from "./recognition.js";
25
- import { meaningOf } from "./resonance.js";
26
- import { climbAttention as climbAttentionFn, naturalBreak as naturalBreakFn, } from "./attention.js";
27
- import { aluToMechanism, defaultMechanisms, think } from "./pipeline.js";
28
- import { articulate } from "./articulation.js";
29
- import { ingest } from "./learning.js";
30
- import { rItem } from "./trace.js";
31
- // ═══════════════════════════════════════════════════════════════════════════
32
- // THE MIND
33
- // ═══════════════════════════════════════════════════════════════════════════
34
- export class Mind {
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);
83
- }
84
- // recogniseSpan wraps recognise
85
- recogniseSpan(bytes) {
86
- const r = recognise(this, bytes);
87
- return { sites: r.sites, leaves: r.leaves, splits: r.splits };
88
- }
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);
98
- }
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));
145
- }
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);
150
- }
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);
155
- }
156
- /** Exposed for tests: find the natural break point in a sorted vote list. */
157
- naturalBreak(votes) {
158
- return naturalBreakFn(votes);
159
- }
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);
165
- }
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();
174
- }
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();
184
- }
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;
392
- return null;
393
- }
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
- }
433
- }