@hviana/sema 0.5.2 → 0.5.4

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 (157) hide show
  1. package/AGENTS.md +115 -53
  2. package/HOW_IT_WORKS.md +291 -184
  3. package/jsr.json +6 -0
  4. package/package.json +1 -1
  5. package/src/mind/bridge.ts +10 -104
  6. package/src/mind/match.ts +497 -7
  7. package/src/mind/{prefix-completion.ts → mechanisms/prefix-completion.ts} +66 -92
  8. package/src/mind/mechanisms/recall.ts +38 -129
  9. package/src/mind/mechanisms/reference.ts +343 -0
  10. package/src/mind/mind.ts +12 -8
  11. package/src/mind/pipeline-mechanism.ts +120 -3
  12. package/src/mind/pipeline.ts +16 -2
  13. package/src/mind/traverse.ts +92 -1
  14. package/src/store.ts +13 -4
  15. package/test/33-multi-candidate.test.mjs +21 -11
  16. package/test/70-prefix-completion.test.mjs +1 -1
  17. package/test/72-prefix-candidate-supply.test.mjs +7 -9
  18. package/test/74-prefix-trap-not-sprung-early.test.mjs +1 -1
  19. package/test/76-reference-binding.test.mjs +543 -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 -93
  23. package/dist/example/train_base.js +0 -2269
  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 -221
  45. package/dist/src/alu/src/parser.js +0 -577
  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/canon.d.ts +0 -45
  57. package/dist/src/canon.js +0 -85
  58. package/dist/src/config.d.ts +0 -111
  59. package/dist/src/config.js +0 -91
  60. package/dist/src/derive/src/deduction.d.ts +0 -136
  61. package/dist/src/derive/src/deduction.js +0 -159
  62. package/dist/src/derive/src/index.d.ts +0 -8
  63. package/dist/src/derive/src/index.js +0 -11
  64. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  65. package/dist/src/derive/src/priority-queue.js +0 -73
  66. package/dist/src/derive/src/rewrite.d.ts +0 -56
  67. package/dist/src/derive/src/rewrite.js +0 -100
  68. package/dist/src/derive/src/trie.d.ts +0 -90
  69. package/dist/src/derive/src/trie.js +0 -217
  70. package/dist/src/derive/test/derive.test.d.ts +0 -1
  71. package/dist/src/derive/test/derive.test.js +0 -122
  72. package/dist/src/extension.d.ts +0 -37
  73. package/dist/src/extension.js +0 -7
  74. package/dist/src/geometry.d.ts +0 -233
  75. package/dist/src/geometry.js +0 -1089
  76. package/dist/src/index.d.ts +0 -17
  77. package/dist/src/index.js +0 -19
  78. package/dist/src/ingest-cache.d.ts +0 -41
  79. package/dist/src/ingest-cache.js +0 -165
  80. package/dist/src/meter.d.ts +0 -176
  81. package/dist/src/meter.js +0 -274
  82. package/dist/src/mind/articulation.d.ts +0 -6
  83. package/dist/src/mind/articulation.js +0 -99
  84. package/dist/src/mind/attention.d.ts +0 -430
  85. package/dist/src/mind/attention.js +0 -2418
  86. package/dist/src/mind/bridge.d.ts +0 -39
  87. package/dist/src/mind/bridge.js +0 -998
  88. package/dist/src/mind/canonical.d.ts +0 -34
  89. package/dist/src/mind/canonical.js +0 -93
  90. package/dist/src/mind/frame-filler.d.ts +0 -15
  91. package/dist/src/mind/frame-filler.js +0 -535
  92. package/dist/src/mind/graph-search.d.ts +0 -294
  93. package/dist/src/mind/graph-search.js +0 -996
  94. package/dist/src/mind/index.d.ts +0 -9
  95. package/dist/src/mind/index.js +0 -5
  96. package/dist/src/mind/junction.d.ts +0 -137
  97. package/dist/src/mind/junction.js +0 -358
  98. package/dist/src/mind/learning.d.ts +0 -75
  99. package/dist/src/mind/learning.js +0 -265
  100. package/dist/src/mind/match.d.ts +0 -192
  101. package/dist/src/mind/match.js +0 -666
  102. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  103. package/dist/src/mind/mechanisms/alu.js +0 -36
  104. package/dist/src/mind/mechanisms/cast.d.ts +0 -89
  105. package/dist/src/mind/mechanisms/cast.js +0 -1186
  106. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  107. package/dist/src/mind/mechanisms/confluence.js +0 -271
  108. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  109. package/dist/src/mind/mechanisms/cover.js +0 -232
  110. package/dist/src/mind/mechanisms/extraction.d.ts +0 -33
  111. package/dist/src/mind/mechanisms/extraction.js +0 -327
  112. package/dist/src/mind/mechanisms/recall.d.ts +0 -16
  113. package/dist/src/mind/mechanisms/recall.js +0 -558
  114. package/dist/src/mind/mind.d.ts +0 -386
  115. package/dist/src/mind/mind.js +0 -754
  116. package/dist/src/mind/pipeline-mechanism.d.ts +0 -183
  117. package/dist/src/mind/pipeline-mechanism.js +0 -521
  118. package/dist/src/mind/pipeline.d.ts +0 -49
  119. package/dist/src/mind/pipeline.js +0 -331
  120. package/dist/src/mind/prefix-completion.d.ts +0 -59
  121. package/dist/src/mind/prefix-completion.js +0 -270
  122. package/dist/src/mind/primitives.d.ts +0 -85
  123. package/dist/src/mind/primitives.js +0 -333
  124. package/dist/src/mind/rationale.d.ts +0 -139
  125. package/dist/src/mind/rationale.js +0 -163
  126. package/dist/src/mind/reasoning.d.ts +0 -42
  127. package/dist/src/mind/reasoning.js +0 -305
  128. package/dist/src/mind/recognition.d.ts +0 -20
  129. package/dist/src/mind/recognition.js +0 -696
  130. package/dist/src/mind/resonance.d.ts +0 -35
  131. package/dist/src/mind/resonance.js +0 -365
  132. package/dist/src/mind/trace.d.ts +0 -15
  133. package/dist/src/mind/trace.js +0 -73
  134. package/dist/src/mind/traverse.d.ts +0 -158
  135. package/dist/src/mind/traverse.js +0 -716
  136. package/dist/src/mind/types.d.ts +0 -418
  137. package/dist/src/mind/types.js +0 -179
  138. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  139. package/dist/src/rabitq-ivf/src/database.js +0 -201
  140. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  141. package/dist/src/rabitq-ivf/src/index.js +0 -4
  142. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  143. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  144. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  145. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  146. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  147. package/dist/src/rabitq-ivf/src/rabitq.js +0 -313
  148. package/dist/src/sema.d.ts +0 -41
  149. package/dist/src/sema.js +0 -77
  150. package/dist/src/store-sqlite.d.ts +0 -184
  151. package/dist/src/store-sqlite.js +0 -942
  152. package/dist/src/store.d.ts +0 -762
  153. package/dist/src/store.js +0 -1823
  154. package/dist/src/vec.d.ts +0 -31
  155. package/dist/src/vec.js +0 -109
  156. package/src/mind/frame-filler.ts +0 -604
  157. package/test/69-frame-filler.test.mjs +0 -115
@@ -1,386 +0,0 @@
1
- import { Vec } from "../vec.js";
2
- import { Sema, Space } from "../sema.js";
3
- import { Alphabet } from "../alphabet.js";
4
- import { Grid } from "../geometry.js";
5
- import { BoundedMap, type Store } from "../store.js";
6
- import { type MindConfig } from "../config.js";
7
- import { type Canon } from "../canon.js";
8
- import { GraphSearch, type Leaf, type Site } from "./graph-search.js";
9
- import type { ComputedSpan, ExtensionHost } from "../extension.js";
10
- export type { ComputedSpan, ExtensionHost };
11
- import { type InspectRationale, Rationale } from "./rationale.js";
12
- export type { InspectRationale, RationaleItem, RationaleStep, } from "./rationale.js";
13
- export type Input = string | Uint8Array | Grid | Grid[];
14
- export interface Response {
15
- v: Vec | null;
16
- bytes: Uint8Array;
17
- /** How the answer was grounded (see {@link Provenance}). `"recall-echo"`
18
- * marks the last-resort fallback that returned the nearest stored form's
19
- * own bytes verbatim — an echo, NOT a grounded fact. Absent when there is
20
- * no answer. */
21
- provenance?: import("./pipeline.js").Provenance;
22
- }
23
- /** Serializable state of a conversation — can be saved and restored across
24
- * sessions. The Mind never interprets the bytes; it only tracks their
25
- * cumulative lengths so the caller can reconstruct turn boundaries later
26
- * without inspecting content. */
27
- export interface ConversationState {
28
- /** The accumulated context bytes — raw concatenation of every turn's
29
- * bytes in order. No separator is inserted; the boundary offsets
30
- * ({@link boundaries}) tell the caller where each turn ends. */
31
- context: Uint8Array;
32
- /** Cumulative byte length after each completed turn. Sorted, strictly
33
- * increasing, each {@code < context.length}. The first turn's length
34
- * is `boundaries[0]`; the second turn starts at that offset, and so
35
- * on. Empty for a single-turn or new conversation. */
36
- boundaries: number[];
37
- /** Byte spans occupied by replies produced by this Mind. Unlike boundary
38
- * parity, this remains exact when a turn receives an empty reply. Optional
39
- * so states saved before the field existed remain restorable. */
40
- answeredSpans?: Array<[number, number]>;
41
- }
42
- /** An active conversation handle. Opaque — interact through the Mind's
43
- * conversation methods ({@link Mind.beginConversation},
44
- * {@link Mind.respondTurn}, {@link Mind.endConversation}). */
45
- export interface Conversation {
46
- readonly id: number;
47
- }
48
- import type { AttentionRead, MindContext, Recognition } from "./types.js";
49
- export type { AnchorRejectionReason, ClimbConsensusData, ConsensusAnchorTrace, ConsensusReachTrace, ConsensusRegionTrace, CrossRegionTier, JunctionVoteTrace, RegionOutcome, } from "./attention.js";
50
- export type { AncestorReach, SaturationReason, SaturationStop, } from "./types.js";
51
- import { type CostReport, Meter } from "../meter.js";
52
- export interface MindOptions {
53
- seed?: number;
54
- recallQueryK?: number;
55
- haloQueryK?: number;
56
- normalizeEpsilon?: number;
57
- cosineEpsilon?: number;
58
- geometry?: Partial<import("../config.js").GeometryConfig>;
59
- alphabet?: Partial<import("../config.js").AlphabetConfig>;
60
- storeConfig?: Partial<import("../config.js").StoreConfig>;
61
- store?: Store;
62
- /** Additional grounding mechanisms (appended after the built-in defaults). */
63
- mechanisms?: import("./pipeline-mechanism.js").PipelineMechanism[];
64
- /** Factories that receive the {@link ExtensionHost} and return mechanisms. */
65
- mechanismFactories?: ((host: import("../extension.js").ExtensionHost) => import("./pipeline-mechanism.js").PipelineMechanism)[];
66
- /** Measure the computational usage of every inference call — see
67
- * src/meter.ts. Off by default and free when off (one null check per
68
- * store read); on, each `respond`/`respondTurn` leaves a {@link
69
- * Mind.lastCost} report behind. Counters are deterministic, so two runs
70
- * of the same query on the same store are diffable; the millisecond
71
- * fields are not. Profiling NEVER changes an answer — but note that
72
- * attaching a RATIONALE does (traced responses bypass the ctx memos,
73
- * AGENTS §2.11), so profile without a trace. */
74
- profile?: boolean;
75
- /** Content canonicalizer applied to EVERY response (any modality) for
76
- * equivalence-class resolution — see src/canon.ts. Text entry points
77
- * ({@link Mind.respondText}, {@link Mind.respondTurnText}) inject the
78
- * Unicode text canonicalizer automatically when this is unset; pass
79
- * `false` to disable canonical resolution everywhere. */
80
- canon?: Canon | false;
81
- }
82
- export declare class Mind implements MindContext {
83
- readonly space: Space;
84
- readonly alphabet: Alphabet;
85
- readonly store: Store;
86
- readonly cfg: MindConfig;
87
- /** The lightest-derivation engine over the Sema graph. */
88
- readonly search: GraphSearch;
89
- /** The grounding mechanisms iterated by {@link think}. */
90
- readonly mechanisms: import("./pipeline-mechanism.js").PipelineMechanism[];
91
- /** The live rationale tracer for the inference currently in flight, or null. */
92
- trace: Rationale | null;
93
- /** The content canonicalizer for the response in flight — see
94
- * {@link MindContext.canon}. Injected per response by the modality entry
95
- * point; null when the response carries no equivalence. */
96
- canon: Canon | null;
97
- /** Per-response canonical-resolution memo — see {@link MindContext.canonMemo}. */
98
- canonMemo: Map<string, number | null> | null;
99
- /** The Mind-level canon option: a canonicalizer to use for EVERY response,
100
- * `false` to disable canonical resolution, or null to let each entry
101
- * point decide (text entry points inject {@link textCanon}). */
102
- private _canonOpt;
103
- /** The work accumulator for the inference call in flight — see
104
- * {@link MindContext.meter}. Non-null only between beginResponse and
105
- * endResponse, and only when the Mind was constructed with
106
- * `{ profile: true }`. */
107
- meter: Meter | null;
108
- /** Whether {@link MindOptions.profile} was set. */
109
- private _profile;
110
- /** The computational-usage report of the LAST completed inference call, or
111
- * null when profiling is off (or nothing has been asked yet). Overwritten
112
- * by every `respond`/`respondTurn`; copy it if you are aggregating. See
113
- * {@link import("../meter.js").CostReport} and `sumReports`/`formatReport`
114
- * for battery-level aggregation. */
115
- lastCost: CostReport | null;
116
- /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
117
- climbMemo: Map<string, Map<string, AttentionRead>> | null;
118
- _structMemoKey: object;
119
- /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
120
- recogniseMemo: Map<string, Recognition> | null;
121
- /** Memo of perceive() — content-keyed. See {@link MindContext.perceiveMemo}. */
122
- perceiveMemo: Map<string, import("../sema.js").Sema> | null;
123
- /** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
124
- _resolvedSubtrees: WeakMap<import("../sema.js").Sema, {
125
- id: number;
126
- len: number;
127
- }> | null;
128
- answeredSpans: ReadonlyArray<readonly [number, number]>;
129
- currentTurnStart: number;
130
- /** The perceived gist of the query currently being answered. Set by `think`
131
- * before the graph search runs; `chooseNext` consults it as a gate (a null
132
- * guide means no query is in flight, so structural walkers keep plain
133
- * first-edge behaviour) and the reverse projection uses it for
134
- * reverse-recall disambiguation via `chooseAmong`. */
135
- _edgeGuide: Vec | null;
136
- /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
137
- * of a single response follows the SAME continuation for each ambiguous
138
- * context node. */
139
- _edgeChoice: Map<number, number>;
140
- /** Previous deposit's seen node ids for incremental change detection. */
141
- _prevSeen: Set<number> | null;
142
- /** Session cache of node-id → perceived gist for candidate scoring — see
143
- * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
144
- * candidate sets (√N at most) fit comfortably and recur across queries. */
145
- _gistCache: BoundedMap<number, Vec>;
146
- _depositTrees: BoundedMap<string, import("./types.js").DepositCacheEntry>;
147
- _depositLens: Set<number>;
148
- _internIds: WeakMap<Sema, number>;
149
- private _nextConvId;
150
- private _conversations;
151
- /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
152
- resolve(bytes: Uint8Array): number | null;
153
- recogniseSpan(bytes: Uint8Array): {
154
- sites: ReadonlyArray<Site>;
155
- leaves: ReadonlyArray<Leaf>;
156
- splits: ReadonlySet<number>;
157
- starts: ReadonlySet<number>;
158
- };
159
- /** Disambiguate among multiple learnt continuations of the same context node.
160
- * Required by {@link GraphSearchHost} — the graph search calls this through the
161
- * host interface when a recognised form has more than one outgoing edge.
162
- * Delegates to the standalone {@link chooseNext} which picks the candidate
163
- * with the most distributional evidence (highest `prevOf` count — the
164
- * structural manifestation of its halo). When evidence is equal the
165
- * first-inserted edge wins. */
166
- chooseNext(node: number): number | undefined;
167
- constructor(opts?: MindOptions);
168
- constructor(cfg: MindConfig, store: Store, _fromStore: true);
169
- /** Exposed for tests: the consensus climb over query sub-regions. */
170
- climbAttention(query: Uint8Array, k: number, mode?: import("./types.js").DFMode): Promise<import("./types.js").Attention[]>;
171
- /** Exposed for tests: climb the structural DAG from a node to its
172
- * edge-bearing ancestor contexts. */
173
- edgeAncestors(id: number, contextCount: number): import("./types.js").AncestorReach;
174
- /** Exposed for tests: find the natural break point in a sorted vote list. */
175
- naturalBreak(votes: number[]): number;
176
- /** Perceive input into a content-defined tree. Deterministic — identical
177
- * bytes always produce an identical tree. Public for ingest-cache. */
178
- perceive(input: Input, leafAt?: (i: number) => number | null, lookup?: (ids: number[]) => number | null): Sema;
179
- /** Open one response's transient state — the tracer, the per-response
180
- * memos, the work meter. The ONE place this state is created, and it
181
- * serves BOTH entry points: `respond` takes fresh per-response memos,
182
- * `respondTurn` passes its conversation, whose memos persist across turns
183
- * (content-keyed, so the previous turn's results are found by this turn's
184
- * sub-span calls) and whose `resolvedSubtrees` spares foldTree the store
185
- * probes for every prefix subtree — and, for walks that pass no visitor,
186
- * the descent as well. respondTurn used to inline its own copy of this
187
- * and of {@link endResponse}; the two drifted (a memo added to one was
188
- * silently absent from the other), so there is exactly one pair now. */
189
- private beginResponse;
190
- /** Open (or leave closed) the response's work accumulator. Separate from
191
- * {@link beginResponse} because {@link respondTurn} keeps its own
192
- * conversation-scoped lifecycle and must not create fresh per-response
193
- * memos — but it DOES meter, through this same pair. */
194
- private _beginMeter;
195
- /** Close the accumulator and publish its report. Detaching from the store
196
- * matters: a Mind that shares a store with another Mind must not keep
197
- * charging that store's reads to a finished response. */
198
- private _endMeter;
199
- /** The canonicalizer a response should carry: the Mind-level option when
200
- * set (or none when explicitly disabled), else the entry point's own
201
- * default — text entry points pass {@link textCanon}, binary ones null. */
202
- private _canonFor;
203
- /** Close one response's transient state — every per-response field, incl.
204
- * the edge guide/choices `think` sets mid-flight, and the meter's report.
205
- *
206
- * A conversation's memo MAPS were mutated in place, so `data.*` still
207
- * points at them and there is nothing to save back. Clearing the Mind's
208
- * references is what matters: a concurrently-started `respond()` swaps its
209
- * own fresh maps into these pointers, and copying back from them here
210
- * would inject a foreign response's memos into the conversation. */
211
- private endResponse;
212
- /** Shared response core — the one path from bytes to voiced answer.
213
- * `respond` calls this directly; `respondTurn` has its own path
214
- * with conversation-persistent memos and incremental perception. */
215
- private _respondImpl;
216
- /** The ONE path from query bytes to a voiced answer: ground (think), then
217
- * re-voice in the asker's words (articulate). Both entry points run
218
- * exactly this — they differ only in the LIFECYCLE around it (fresh
219
- * per-response memos vs. a conversation's persistent ones) and in what
220
- * they do with the answer afterwards. It must be called between
221
- * {@link beginResponse} and {@link endResponse}. */
222
- private _groundAndVoice;
223
- /** Answer ONE self-contained input.
224
- *
225
- * A MULTI-TURN context is not that, and this is the wrong entry point for
226
- * it. `respond` folds the bytes it is handed with no boundary set, because
227
- * nothing in a flat byte string says where one turn ended — only the caller
228
- * who assembled it knows, which is the whole reason `boundaries` is a
229
- * parameter of {@link perceiveImpl} and never inferred from content. A
230
- * conversation deposited through {@link ingest} folds its contexts over
231
- * those turn boundaries, so a hand-concatenated transcript passed here
232
- * folds differently from the way it was learnt and reaches the trained
233
- * context node only by luck (measured on a 7-turn conversation: 5/7 here
234
- * against 7/7 through {@link respondTurn}, same bytes). Use
235
- * {@link beginConversation} + {@link respondTurn}, or {@link addTurn} to
236
- * replay turns the Mind should hear but not answer. */
237
- respond(input: Input, inspectRationale?: InspectRationale): Promise<Response>;
238
- /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
239
- * decoding — they are structural padding in text answers. LOSSY for a
240
- * binary answer that legitimately contains NULs: use {@link respond} and
241
- * read `bytes` directly for binary/grid modalities.
242
- *
243
- * Injects the TEXT canonicalizer (src/canon.ts) so resolution treats
244
- * every character variation of the same text — case, width, whitespace —
245
- * as one form, provided the store's canon index is built
246
- * ({@link buildCanonIndex}). */
247
- respondText(input: string, inspectRationale?: InspectRationale): Promise<string>;
248
- /** Begin a new conversation, optionally restoring from a previously-saved
249
- * {@link ConversationState}. The returned handle is required for
250
- * {@link respondTurn} and {@link endConversation}.
251
- *
252
- * Conversations are independent — a Mind can manage several concurrently.
253
- * Each tracks the fold pyramid (accumulated internal processing) and
254
- * turn-boundary offsets; the geometry never inspects content to guess
255
- * where one turn ends and the next begins. */
256
- beginConversation(state?: ConversationState): Conversation;
257
- /** End a conversation, releasing its internal resources (accumulated
258
- * context, boundary offsets, and the fold-pyramid cache). Idempotent. */
259
- endConversation(conv: Conversation): void;
260
- /** The current serialisable state of an active conversation. Save this
261
- * to resume the conversation later via {@link beginConversation}. */
262
- conversationState(conv: Conversation): ConversationState | null;
263
- /** Append a turn to a conversation's accumulated context WITHOUT
264
- * responding — raw byte append plus a boundary offset, never a
265
- * separator; the fold pyramid advances by O(turn).
266
- *
267
- * This is the primitive for turns the Mind should hear but not answer:
268
- * replaying a transcript, feeding the OTHER speaker's line in a
269
- * prediction harness, or restoring context piecewise. {@link
270
- * respondTurn} = addTurn + think + its own reply appended the same way.
271
- *
272
- * ── ON SEPARATORS: THERE IS NO SEPARATOR QUESTION ────────────────────
273
- *
274
- * "Never a separator" above says what this method DOES — it appends the
275
- * bytes you give it and records an OFFSET — not that separator bytes are
276
- * forbidden, unsupported, or something the engine must be taught about.
277
- * Sema is agnostic to them, and reviewers keep mistaking that agnosticism
278
- * for a constraint. To be explicit, because the mistake is easy:
279
- *
280
- * 1. A turn boundary is an OFFSET, held here, in `boundaries`. It is
281
- * never a character the geometry scans for. Nothing downstream asks
282
- * "what byte separates two turns?" because nothing downstream finds
283
- * boundaries by looking at content at all.
284
- * 2. A separator in a CORPUS is ordinary content. If a trainer joins
285
- * turns with "\n" (example/train_base.ts does), those newlines are
286
- * simply bytes inside the stream, folded like every other byte. They
287
- * are a property of that corpus, not of this API and not of the fold.
288
- * 3. This API can therefore reproduce ANY corpus exactly, with no
289
- * convention to agree on: replaying a "\n"-joined corpus means passing
290
- * `"\n" + turnText` as the turn. The separator rides along IN the
291
- * turn bytes, where it belongs. There is nothing to configure and no
292
- * mode to select.
293
- * 4. Inference is not exact-match anyway. Recognition works over
294
- * sub-spans, canonical equivalence and resonance, so a query that
295
- * differs from the trained bytes by punctuation or whitespace still
296
- * reaches the trained forms; it degrades, it does not fail closed.
297
- *
298
- * What follows from 1–4: differing separator bytes between a corpus and a
299
- * query is an ordinary CONTENT difference — the same kind as any other
300
- * wording difference — and it is measured the same way. It is NOT an
301
- * incompatibility between the trainer and this API, and it does NOT
302
- * require choosing a project-wide separator convention. A review that
303
- * concludes otherwise (this one did, before being corrected) has mistaken
304
- * its own harness feeding untrained bytes for an architectural defect. */
305
- addTurn(conv: Conversation, turn: Input): ConversationState;
306
- /** Grow a conversation's accumulated context by one turn's bytes — raw
307
- * append plus a boundary offset, pyramid advanced by O(turn), the grown
308
- * context's tree seeded into the conversation's perceive memo. The ONE
309
- * place a context grows ({@link addTurn} and {@link respondTurn} both
310
- * come through here), so the append semantics cannot drift. */
311
- private _growContext;
312
- /** Process one turn of a conversation.
313
- *
314
- * `turn` is the raw input for the latest turn — its bytes are appended
315
- * to the accumulated context directly (raw concatenation). The Mind
316
- * tracks the byte offset where each turn ends; no separator is ever
317
- * inserted or inspected.
318
- *
319
- * Returns the response AND the updated {@link ConversationState} so the
320
- * caller can persist it. The conversation handle's internal state is
321
- * updated in place — the returned state is a snapshot for storage.
322
- *
323
- * SINGLE FLIGHT: at most one respondTurn may be in flight per Mind. The
324
- * conversation's memo caches are swapped into the Mind-level per-response
325
- * pointers for the duration of the turn, so a concurrently-running
326
- * respond()/respondTurn() on the SAME Mind would interleave state.
327
- * Different Minds (or sequential awaits, as in every test) are safe. */
328
- respondTurn(conv: Conversation, turn: Input, inspectRationale?: InspectRationale): Promise<{
329
- response: Response;
330
- state: ConversationState;
331
- }>;
332
- /** Text view of {@link respondTurn}. See {@link respondText} for the
333
- * NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
334
- * directly — this is a text-only convenience, like {@link respondText}. */
335
- respondTurnText(conv: Conversation, turn: string, inspectRationale?: InspectRationale): Promise<{
336
- response: string;
337
- state: ConversationState;
338
- }>;
339
- embedding(input: Input): Promise<Vec | null>;
340
- /** Kinship note: the vector arm below is a miniature of recall's tier 3
341
- * (resonate → reach gate → read out the nearest form's bytes) — the
342
- * read-out direction of the same operation, without recall's grounding
343
- * ladder. If either side's acceptance rule changes, revisit the other. */
344
- express(idOrV: number | Vec): Promise<Uint8Array>;
345
- /** See {@link import("./learning.js").ingest} — `onDeposit`, when given,
346
- * reports each ingested item's deposited root node ids
347
- * ({@link DepositReport}); purely observational. */
348
- ingest(input: Input | (Input | [Input, Input])[], second?: Input, onDeposit?: (report: import("./learning.js").DepositReport) => void): Promise<(Sema & {
349
- id: number;
350
- }) | undefined>;
351
- private extensionHost;
352
- private groundedContinuation;
353
- /** Re-index structurally-important nodes whose gists were evicted from the
354
- * pending cache before they reached the content index. See {@link
355
- * Store.repairContentIndex} for the contract; this method wires the
356
- * Mind's perception into the store's repair walk.
357
- *
358
- * Run this after training or at checkpoints to restore recall reach for
359
- * nodes that bridge experiences but were never indexed. A pure interior
360
- * node (no edges, no halo) is deliberately skipped — it is scaffolding,
361
- * not an experience root or bridge, and regenerating its gist would waste
362
- * I/O and index space for no recall benefit.
363
- *
364
- * @param minParents only repair nodes with ≥ this many structural parents
365
- * (default 2 — structural bridges)
366
- * @returns number of nodes added to the content index */
367
- repairContentIndex(minParents?: number): Promise<number>;
368
- /** Build (or incrementally refresh) the store's canonical-form index: for
369
- * every content-bearing node, record the hash of its CANONICAL key so
370
- * resolution can find stored forms across surface variation (case, width,
371
- * whitespace — whatever `canon` equates; see src/canon.ts).
372
- *
373
- * Incremental and idempotent: the last indexed node id is remembered in
374
- * store meta (`canon.upto`), so a refresh after further training scans
375
- * only the new rows. Run once after training, and again after ingests —
376
- * the same operational shape as {@link repairContentIndex}.
377
- *
378
- * @param canon the canonicalizer to index under — MUST be the same one
379
- * queries will carry (text queries carry {@link textCanon}
380
- * unless the Mind was constructed with its own)
381
- * @returns number of index rows added */
382
- buildCanonIndex(canon?: Canon): Promise<number>;
383
- save(): Promise<Uint8Array>;
384
- static load(snapshot: Uint8Array, store: Store): Promise<Mind>;
385
- static loadFromStore(store: Store): Promise<Mind>;
386
- }