@hviana/sema 0.1.6 → 0.1.8

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 (50) hide show
  1. package/dist/example/train_base.js +42 -9
  2. package/dist/src/alu/src/parser.d.ts +9 -0
  3. package/dist/src/alu/src/parser.js +110 -2
  4. package/dist/src/canon.d.ts +26 -0
  5. package/dist/src/canon.js +57 -0
  6. package/dist/src/geometry.d.ts +13 -2
  7. package/dist/src/geometry.js +101 -2
  8. package/dist/src/index.d.ts +1 -0
  9. package/dist/src/index.js +1 -0
  10. package/dist/src/mind/attention.js +11 -7
  11. package/dist/src/mind/learning.js +33 -1
  12. package/dist/src/mind/match.d.ts +4 -2
  13. package/dist/src/mind/match.js +30 -6
  14. package/dist/src/mind/mechanisms/cast.js +18 -4
  15. package/dist/src/mind/mechanisms/confluence.js +13 -1
  16. package/dist/src/mind/mechanisms/recall.js +115 -31
  17. package/dist/src/mind/mind.d.ts +138 -12
  18. package/dist/src/mind/mind.js +284 -22
  19. package/dist/src/mind/primitives.d.ts +22 -2
  20. package/dist/src/mind/primitives.js +95 -6
  21. package/dist/src/mind/recognition.js +31 -8
  22. package/dist/src/mind/traverse.d.ts +13 -0
  23. package/dist/src/mind/traverse.js +41 -0
  24. package/dist/src/mind/types.d.ts +33 -21
  25. package/dist/src/store-sqlite.d.ts +10 -0
  26. package/dist/src/store-sqlite.js +58 -0
  27. package/dist/src/store.d.ts +23 -0
  28. package/dist/src/store.js +13 -2
  29. package/example/train_base.ts +49 -9
  30. package/index.html +65 -0
  31. package/package.json +1 -1
  32. package/src/alu/src/parser.ts +105 -4
  33. package/src/canon.ts +65 -0
  34. package/src/geometry.ts +105 -1
  35. package/src/index.ts +1 -0
  36. package/src/mind/attention.ts +11 -6
  37. package/src/mind/learning.ts +39 -1
  38. package/src/mind/match.ts +29 -6
  39. package/src/mind/mechanisms/cast.ts +21 -6
  40. package/src/mind/mechanisms/confluence.ts +15 -1
  41. package/src/mind/mechanisms/recall.ts +132 -41
  42. package/src/mind/mind.ts +396 -22
  43. package/src/mind/primitives.ts +109 -7
  44. package/src/mind/recognition.ts +36 -8
  45. package/src/mind/traverse.ts +47 -0
  46. package/src/mind/types.ts +30 -21
  47. package/src/store-sqlite.ts +76 -0
  48. package/src/store.ts +46 -2
  49. package/test/13-conversation.test.mjs +240 -20
  50. package/test/35-prefix-edge.test.mjs +86 -0
@@ -262,7 +262,21 @@ export async function counterfactualTransfer(ctx, query, pre) {
262
262
  // corrupted-store read now falls back here instead of voicing a hollow
263
263
  // seat into the comparison — the same "empty bytes are no grounding"
264
264
  // invariant project() has always enforced.
265
- const seatOfNode = (id, fallback) => reverseContext(ctx, id, qv) ?? fallback;
265
+ // A node that only ever CONTINUES an edge SOURCE with no predecessor —
266
+ // is a learnt CONTEXT (a stored question-shaped frame), not an entity.
267
+ // Voicing it verbatim answers a question with a question (the observed
268
+ // cast echo: two stored questions concatenated as an "answer"). The
269
+ // context's role is established by what it LEADS TO, so its seat is its
270
+ // own continuation — the fact — with the reverse projection and the raw
271
+ // bytes as the ordinary fallbacks for genuine entities.
272
+ const seatOfNode = async (id, fallback) => {
273
+ if (ctx.store.prevCount(id) === 0 && ctx.store.hasNext(id)) {
274
+ const fwd = await follow(ctx, id, qv);
275
+ if (fwd !== null)
276
+ return fwd;
277
+ }
278
+ return reverseContext(ctx, id, qv) ?? fallback;
279
+ };
266
280
  const seatOf = (p) => seatOfNode(p.anchor, p.ctx);
267
281
  const analogs = [];
268
282
  for (const p of points) {
@@ -381,10 +395,10 @@ export async function counterfactualTransfer(ctx, query, pre) {
381
395
  rNode(ctx, dominant.anchor, "analog", bestSim),
382
396
  rNode(ctx, bestAnalog.anchor, "analog", bestSim),
383
397
  ], [], "the two structures keep distributional company beyond chance — genuine analogs");
384
- const a = seatOf(dominant);
398
+ const a = await seatOf(dominant);
385
399
  const b = bestAnalog.point !== null
386
- ? seatOf(bestAnalog.point)
387
- : seatOfNode(bestAnalog.anchor, read(ctx, bestAnalog.anchor));
400
+ ? await seatOf(bestAnalog.point)
401
+ : await seatOfNode(bestAnalog.anchor, read(ctx, bestAnalog.anchor));
388
402
  const answer = await joinWithBridge(ctx, a, b);
389
403
  record(answer, "analogical comparison — each analog voiced by the context that establishes its role", new Set([dominant.anchor, bestAnalog.anchor]),
390
404
  // A halo-mediated act (the analogy gate) plus two seat projections.
@@ -70,6 +70,18 @@ export async function confluenceJoin(ctx, query, pre) {
70
70
  // below.
71
71
  const queryWin = pre.queryWindows;
72
72
  const queryIds = new Set(queryWin.values());
73
+ // A constraint must bind a CONSTITUENT, not a shard. A genuinely shared
74
+ // form weaves a contiguous RUN of shared discriminative windows — its
75
+ // merged cover span is the form's own length, beyond one perception
76
+ // quantum (2W: the same "two quanta of structure" bar the conjunctive
77
+ // precondition `query.length < 2W` and CAST's weave live under). A long
78
+ // stored document holds thousands of W-windows and will hold a FEW of any
79
+ // query's by accident, but accidental sharing is one window (a merged
80
+ // span of W, at most ~W+overlap bytes: "ow ma", "ys a", "ías " —
81
+ // observed), never a run ("​ translucent", " featherlight" — the genuine
82
+ // constraints). Shard-bound streams are no constraints, and their meets
83
+ // are connective debris (". Sure,", "ngul" — observed).
84
+ const bindsAConstituent = (cover) => cover.some(([cs, ce]) => ce - cs >= 2 * W);
73
85
  const streams = [];
74
86
  const rankedCapped = ranked.length > pre.k ? ranked.slice(0, pre.k) : ranked;
75
87
  for (const cand of rankedCapped) {
@@ -96,7 +108,7 @@ export async function confluenceJoin(ctx, query, pre) {
96
108
  else
97
109
  cover.push(curC = [off, off + W]);
98
110
  }
99
- if (cover.length > 0) {
111
+ if (cover.length > 0 && bindsAConstituent(cover)) {
100
112
  streams.push({ anchor: cand.anchor, vote: cand.vote, ids, cover, held });
101
113
  }
102
114
  }
@@ -6,8 +6,9 @@
6
6
  import { cosine } from "../../vec.js";
7
7
  import { consensusFloor, identityBar, reachThreshold, significanceBar, } from "../../geometry.js";
8
8
  import { gistOf, read, resolve } from "../primitives.js";
9
+ import { bytesEqual, indexOf } from "../../bytes.js";
9
10
  import { corpusN, hubBound } from "../traverse.js";
10
- import { project, reverseContext } from "../match.js";
11
+ import { follow, project, reverseContext } from "../match.js";
11
12
  import { CONCEPT, STEP } from "../graph-search.js";
12
13
  import { unexplainedLabel } from "../rationale.js";
13
14
  import { rItem, rNode } from "../trace.js";
@@ -43,6 +44,44 @@ export async function recallByResonance(ctx, query, pre) {
43
44
  : "exact self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
44
45
  }
45
46
  }
47
+ // 0b. ARGUMENT BINDING (RC8): the query is not itself a stored form, but
48
+ // it CONTAINS a recognised constituent that is an edge SOURCE — a learnt
49
+ // pair's left side carried inside a wrapper ("How do you say 'thank you'
50
+ // in French?"). The wrapper is scaffolding; the argument is the span
51
+ // that LEADS somewhere, so its continuation — guided by the whole query's
52
+ // gist — is the answer. Matching the wrapper while ignoring the argument
53
+ // (the observed "good morning" template failure) is worse than silence,
54
+ // so anything short of ONE unambiguous binding falls through: the
55
+ // constituent bar is the same two-quanta (2W) reading confluence binds
56
+ // under, nested recognitions collapse to their MAXIMAL span, and two
57
+ // distinct maximal arguments mean the query asks about neither alone.
58
+ if (qId === null) {
59
+ const W2 = 2 * ctx.space.maxGroup;
60
+ const args = pre.rec.sites.filter((s) => s.end - s.start >= W2 &&
61
+ s.end - s.start < query.length &&
62
+ ctx.store.hasNext(s.payload));
63
+ // Maximal spans by one sorted sweep (starts ascending, ties longest
64
+ // first): every earlier span starts at or before s, so s is contained
65
+ // exactly when the running max end already covers it. O(m log m) — a
66
+ // long input recognises O(|input|) sites, and a pairwise scan here was
67
+ // quadratic in the input.
68
+ args.sort((a, b) => a.start - b.start || b.end - a.end);
69
+ const maximal = [];
70
+ let maxEnd = -1;
71
+ for (const s of args) {
72
+ if (s.end <= maxEnd)
73
+ continue;
74
+ maximal.push(s);
75
+ maxEnd = s.end;
76
+ }
77
+ if (maximal.length === 1) {
78
+ const arg = maximal[0];
79
+ const g = await follow(ctx, arg.payload, queryGist);
80
+ if (g !== null && g.length > 0) {
81
+ return ground(g, "argument binding — the query's sole edge-source constituent, continuation followed", [[arg.start, arg.end]], STEP);
82
+ }
83
+ }
84
+ }
46
85
  const whole = await ctx.store.resonate(queryGist, k);
47
86
  if (whole.length === 0) {
48
87
  return ground(null, "empty store — nothing to resonate with", [], 0);
@@ -55,9 +94,27 @@ export async function recallByResonance(ctx, query, pre) {
55
94
  // into bytes — at most one river window (see {@link identityBar}). A
56
95
  // fixed cosine bar let long queries claim "near-identical" while whole
57
96
  // windows — an answer word — differed.
58
- if (top.score >= identityBar(ctx.store.D, ctx.space.maxGroup, query.length)) {
97
+ // A hit RESTATES the query when its bytes are the query's own — exactly,
98
+ // or under the response's equivalence (a case/width twin). Restating
99
+ // hits may only conclude through disciplined reverse recall: voicing
100
+ // their bytes echoes the query back at itself (never an answer — the
101
+ // same principle that keeps cast from voicing stored questions), and
102
+ // projecting them forward is reverse recall's containment failure in the
103
+ // other direction — "whatever followed these bytes in some document".
104
+ const qKey = ctx.canon ? ctx.canon(query) : query;
105
+ const restates = (b) => bytesEqual(b, query) ||
106
+ (ctx.canon !== null && bytesEqual(ctx.canon(b), qKey));
107
+ const idBar = identityBar(ctx.store.D, ctx.space.maxGroup, query.length);
108
+ if (top.score >= idBar) {
59
109
  for (const h of whole) {
60
- if (h.id === qId || h.score >= 1.0) {
110
+ // The identity claim is PER HIT, not per tier: hits are ranked
111
+ // nearest-first, and grounding one below the bar under this tier's
112
+ // "near-identical" label would launder byte-overlap noise (observed:
113
+ // "merci" projecting through the unrelated near hit "meraih").
114
+ if (h.score < idBar)
115
+ break;
116
+ const own = read(ctx, h.id);
117
+ if (h.id === qId || restates(own)) {
61
118
  const rev = ctx.store.prevFirst(h.id, hubBound(ctx));
62
119
  const g = reverseContext(ctx, h.id, queryGist, rev);
63
120
  if (g !== null) {
@@ -65,10 +122,7 @@ export async function recallByResonance(ctx, query, pre) {
65
122
  ? "perfect self-match — reverse recall to the sole predecessor"
66
123
  : "perfect self-match — reverse recall to the best-resonating predecessor", nothing, STEP);
67
124
  }
68
- const own = read(ctx, h.id);
69
- if (own.length > 0) {
70
- return ground(own, "perfect self-match — the query IS this node", nothing, STEP);
71
- }
125
+ continue;
72
126
  }
73
127
  const g = await project(ctx, h.id, queryGist);
74
128
  if (g) {
@@ -76,8 +130,30 @@ export async function recallByResonance(ctx, query, pre) {
76
130
  }
77
131
  }
78
132
  }
133
+ // The query-relative grounding fraction, shared by tiers 2–4 — gated on
134
+ // the FRACTION OF THE QUERY the grounding explains, not the raw cosine.
135
+ // Root gists are unit vectors, but their magnitudes are recoverable from
136
+ // the byte lengths (‖·‖ = √len under the linear fold):
137
+ // cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
138
+ // The raw cosine punished honest containment — a query fully inside a
139
+ // longer grounded answer scored √(lenQ/lenG) and was refused — and let a
140
+ // long answer sharing only scaffolding pass; the query-relative fraction
141
+ // measures exactly what the reach bar means: how much of THE QUERY the
142
+ // store accounts for.
143
+ // Chance similarity survives the length conversion AMPLIFIED: the same
144
+ // √(lenG/lenQ) factor that converts an honest shared fraction into a
145
+ // query-relative one multiplies the estimator/chance floor too, so a long
146
+ // stored form (√(lenG/lenQ) ≈ 10 at 100×) lifted a noise-level cosine past
147
+ // the reach bar and grounded pure gibberish (observed). Only the
148
+ // ABOVE-CHANCE part of the similarity is evidence of shared content —
149
+ // subtract the significance bar (3/√D, §8.3) before converting. Derived
150
+ // from the existing bars; never tuned.
151
+ const sig = significanceBar(ctx.store.D);
152
+ const reach = reachThreshold(ctx.space.maxGroup);
153
+ const fracOfQuery = (cos, otherLen) => Math.min(1, Math.max(0, cos - sig) *
154
+ Math.sqrt(otherLen / Math.max(1, query.length)));
79
155
  // 2. Scaffolding-dominated.
80
- if (top.score >= significanceBar(ctx.store.D)) {
156
+ if (top.score >= sig) {
81
157
  const N = corpusN(ctx);
82
158
  const minVote = consensusFloor(N);
83
159
  // The committed points of attention ARE the shared climb's roots (same
@@ -86,44 +162,52 @@ export async function recallByResonance(ctx, query, pre) {
86
162
  const forest = (await pre.attention()).roots;
87
163
  if (forest.length > 0 && forest[0].vote >= minVote) {
88
164
  const g = await project(ctx, forest[0].anchor, queryGist);
89
- if (g) {
165
+ // The anchor cleared the consensus floor, but the floor prices the
166
+ // ANCHOR's evidence, not the projection's: a junk attractor can clear
167
+ // it and project a PIECE OF THE QUERY back at it (the observed
168
+ // "buenos días in English" → "English" fragment). A projection that
169
+ // is a proper byte-subspan of the query restates part of the question
170
+ // — never an answer (the same principle as `restates` above, extended
171
+ // to fragments). Genuine anchor groundings — longer than the query,
172
+ // or disjoint from it — pass untouched.
173
+ if (g && !(g.length < query.length && indexOf(query, g, 0) >= 0)) {
90
174
  return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
91
175
  }
92
176
  }
93
177
  }
94
- // 3. Last resort — gated on the FRACTION OF THE QUERY the grounding
95
- // explains, not the raw cosine. Root gists are unit vectors, but their
96
- // magnitudes are recoverable from the byte lengths (‖·‖ = √len under the
97
- // linear fold): cos = shared/√(lenQ·lenG), so shared/lenQ = cos·√(lenG/lenQ).
98
- // The raw cosine punished honest containment — a query fully inside a
99
- // longer grounded answer scored √(lenQ/lenG) and was refused — and let a
100
- // long answer sharing only scaffolding pass; the query-relative fraction
101
- // measures exactly what the reach bar means: how much of THE QUERY the
102
- // store accounts for.
103
- const fracOfQuery = (cos, otherLen) => Math.min(1, cos * Math.sqrt(otherLen / Math.max(1, query.length)));
178
+ // 3. Last resort — the nearest grounded whole-query hit, same gate.
104
179
  for (const h of whole) {
105
180
  const g = await project(ctx, h.id, queryGist);
106
181
  if (g) {
107
182
  if (fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
108
- reachThreshold(ctx.space.maxGroup)) {
183
+ reach) {
109
184
  return ground(g, "last resort: the nearest grounded whole-query hit", [], STEP);
110
185
  }
111
186
  }
112
187
  }
113
- // The refusal/echo decision, in the same query-relative units the top
114
- // hit's magnitude read from the store (contentLen: √bytes IS the linear
115
- // fold's gist norm), never from re-reading its bytes.
116
- // The magnitude read SATURATES at the decision point: frac reaches the
117
- // reach bar exactly when lenH = lenQ·(reach/score)², so the walk never
118
- // needs to see past that a huge conversation root costs a capped read,
119
- // and a clamped return decides "pass" identically (frac reach).
120
- const reach = reachThreshold(ctx.space.maxGroup);
121
- const lenCap = Math.ceil(query.length * (reach / Math.max(top.score, 1e-6)) ** 2) + 1;
122
- if (fracOfQuery(top.score, ctx.store.contentLen(top.id, lenCap)) < reach) {
188
+ // The refusal/echo decision. The echo returns a stored form's bytes AS
189
+ // the answer a near-identity claim about the query and identity-grade
190
+ // decisions are never made on an estimated score ("approximate scores may
191
+ // rank and propose; they may never decide", §6.2): the RaBitQ estimate
192
+ // overshooting the reach bar echoed a WRONG-entity neighbour ("capital of
193
+ // Zamunda?" echoed the Armenia fact, observed). The bytes are read
194
+ // anyway to be echoed, so the decision uses their EXACT fold: one river
195
+ // fold of the top hit, measured in the same query-relative,
196
+ // chance-corrected units as the tier above.
197
+ const topBytes = read(ctx, top.id);
198
+ const exact = topBytes.length > 0
199
+ ? cosine(queryGist, gistOf(ctx, topBytes))
200
+ : 0;
201
+ if (fracOfQuery(exact, topBytes.length) < reach) {
123
202
  return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
124
203
  }
204
+ // Echoing the query's own bytes back at it is not an echo of a RELATED
205
+ // form — it is the query restated, which answers nothing.
206
+ if (restates(topBytes)) {
207
+ return ground(null, "the nearest form IS the query itself — restating it answers nothing", [], 0);
208
+ }
125
209
  // Honest echo.
126
- return ground(read(ctx, top.id), "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
210
+ return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
127
211
  }
128
212
  // ── Pipeline mechanism ──────────────────────────────────────────────────────
129
213
  export const recallMechanism = {
@@ -1,9 +1,10 @@
1
1
  import { Vec } from "../vec.js";
2
2
  import { Sema, Space } from "../sema.js";
3
3
  import { Alphabet } from "../alphabet.js";
4
- import { Grid } from "../geometry.js";
4
+ import { type FoldPyramid, Grid } from "../geometry.js";
5
5
  import { BoundedMap, type Store } from "../store.js";
6
6
  import { type MindConfig } from "../config.js";
7
+ import { type Canon } from "../canon.js";
7
8
  import { GraphSearch, type Leaf, type Site } from "./graph-search.js";
8
9
  import type { ComputedSpan, ExtensionHost } from "../extension.js";
9
10
  export type { ComputedSpan, ExtensionHost };
@@ -19,6 +20,27 @@ export interface Response {
19
20
  * no answer. */
20
21
  provenance?: import("./pipeline.js").Provenance;
21
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
+ }
38
+ /** An active conversation handle. Opaque — interact through the Mind's
39
+ * conversation methods ({@link Mind.beginConversation},
40
+ * {@link Mind.respondTurn}, {@link Mind.endConversation}). */
41
+ export interface Conversation {
42
+ readonly id: number;
43
+ }
22
44
  import type { AttentionRead, MindContext, Recognition } from "./types.js";
23
45
  export interface MindOptions {
24
46
  seed?: number;
@@ -34,6 +56,12 @@ export interface MindOptions {
34
56
  mechanisms?: import("./pipeline-mechanism.js").PipelineMechanism[];
35
57
  /** Factories that receive the {@link ExtensionHost} and return mechanisms. */
36
58
  mechanismFactories?: ((host: import("../extension.js").ExtensionHost) => import("./pipeline-mechanism.js").PipelineMechanism)[];
59
+ /** Content canonicalizer applied to EVERY response (any modality) for
60
+ * equivalence-class resolution — see src/canon.ts. Text entry points
61
+ * ({@link Mind.respondText}, {@link Mind.respondTurnText}) inject the
62
+ * Unicode text canonicalizer automatically when this is unset; pass
63
+ * `false` to disable canonical resolution everywhere. */
64
+ canon?: Canon | false;
37
65
  }
38
66
  export declare class Mind implements MindContext {
39
67
  readonly space: Space;
@@ -46,16 +74,27 @@ export declare class Mind implements MindContext {
46
74
  readonly mechanisms: import("./pipeline-mechanism.js").PipelineMechanism[];
47
75
  /** The live rationale tracer for the inference currently in flight, or null. */
48
76
  trace: Rationale | null;
49
- /** Per-response memo of the consensus climb. NOTE: this memo and
50
- * {@link recogniseMemo} are BYPASSED while a rationale trace is attached
51
- * (every mechanism must emit its own steps), so a traced respond re-pays
52
- * up to four consensus climbs plus repeat recognitions — that is where the
53
- * traced-vs-untraced latency multiple comes from, by design. */
54
- climbMemo: WeakMap<Uint8Array, Map<string, AttentionRead>> | null;
55
- /** Per-response memo of recognise() see {@link MindContext.recogniseMemo}. */
56
- recogniseMemo: WeakMap<Uint8Array, Recognition> | null;
57
- /** Per-response memo of perceive() see {@link MindContext.perceiveMemo}. */
77
+ /** The content canonicalizer for the response in flight see
78
+ * {@link MindContext.canon}. Injected per response by the modality entry
79
+ * point; null when the response carries no equivalence. */
80
+ canon: Canon | null;
81
+ /** Per-response canonical-resolution memo see {@link MindContext.canonMemo}. */
82
+ canonMemo: Map<string, number | null> | null;
83
+ /** The Mind-level canon option: a canonicalizer to use for EVERY response,
84
+ * `false` to disable canonical resolution, or null to let each entry
85
+ * point decide (text entry points inject {@link textCanon}). */
86
+ private _canonOpt;
87
+ /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
88
+ climbMemo: Map<string, Map<string, AttentionRead>> | null;
89
+ /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
90
+ recogniseMemo: Map<string, Recognition> | null;
91
+ /** Memo of perceive() — content-keyed. See {@link MindContext.perceiveMemo}. */
58
92
  perceiveMemo: Map<string, import("../sema.js").Sema> | null;
93
+ /** Subtree-resolution cache. See {@link MindContext._resolvedSubtrees}. */
94
+ _resolvedSubtrees: WeakMap<import("../sema.js").Sema, {
95
+ id: number;
96
+ len: number;
97
+ }> | null;
59
98
  /** The perceived gist of the query currently being answered. Set by `think`
60
99
  * before the graph search runs; `chooseNext` consults it as a gate (a null
61
100
  * guide means no query is in flight, so structural walkers keep plain
@@ -72,9 +111,11 @@ export declare class Mind implements MindContext {
72
111
  * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
73
112
  * candidate sets (√N at most) fit comfortably and recur across queries. */
74
113
  _gistCache: BoundedMap<number, Vec>;
75
- _depositTrees: BoundedMap<string, import("../geometry.js").FoldPyramid>;
114
+ _depositTrees: BoundedMap<string, FoldPyramid>;
76
115
  _depositLens: Set<number>;
77
116
  _internIds: WeakMap<Sema, number>;
117
+ private _nextConvId;
118
+ private _conversations;
78
119
  /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
79
120
  resolve(bytes: Uint8Array): number | null;
80
121
  recogniseSpan(bytes: Uint8Array): {
@@ -106,15 +147,85 @@ export declare class Mind implements MindContext {
106
147
  * memos. Paired with {@link endResponse}; the ONE place this state is
107
148
  * created, so adding a memo cannot forget its reset. */
108
149
  private beginResponse;
150
+ /** The canonicalizer a response should carry: the Mind-level option when
151
+ * set (or none when explicitly disabled), else the entry point's own
152
+ * default — text entry points pass {@link textCanon}, binary ones null. */
153
+ private _canonFor;
109
154
  /** Close one response's transient state — every per-response field, incl.
110
155
  * the edge guide/choices `think` sets mid-flight. */
111
156
  private endResponse;
157
+ /** Shared response core — the one path from bytes to voiced answer.
158
+ * `respond` calls this directly; `respondTurn` has its own path
159
+ * with conversation-persistent memos and incremental perception. */
160
+ private _respondImpl;
112
161
  respond(input: Input, inspectRationale?: InspectRationale): Promise<Response>;
113
162
  /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
114
163
  * decoding — they are structural padding in text answers. LOSSY for a
115
164
  * binary answer that legitimately contains NULs: use {@link respond} and
116
- * read `bytes` directly for binary/grid modalities. */
165
+ * read `bytes` directly for binary/grid modalities.
166
+ *
167
+ * Injects the TEXT canonicalizer (src/canon.ts) so resolution treats
168
+ * every character variation of the same text — case, width, whitespace —
169
+ * as one form, provided the store's canon index is built
170
+ * ({@link buildCanonIndex}). */
117
171
  respondText(input: string, inspectRationale?: InspectRationale): Promise<string>;
172
+ /** Begin a new conversation, optionally restoring from a previously-saved
173
+ * {@link ConversationState}. The returned handle is required for
174
+ * {@link respondTurn} and {@link endConversation}.
175
+ *
176
+ * Conversations are independent — a Mind can manage several concurrently.
177
+ * Each tracks the fold pyramid (accumulated internal processing) and
178
+ * turn-boundary offsets; the geometry never inspects content to guess
179
+ * where one turn ends and the next begins. */
180
+ beginConversation(state?: ConversationState): Conversation;
181
+ /** End a conversation, releasing its internal resources (accumulated
182
+ * context, boundary offsets, and the fold-pyramid cache). Idempotent. */
183
+ endConversation(conv: Conversation): void;
184
+ /** The current serialisable state of an active conversation. Save this
185
+ * to resume the conversation later via {@link beginConversation}. */
186
+ conversationState(conv: Conversation): ConversationState | null;
187
+ /** Append a turn to a conversation's accumulated context WITHOUT
188
+ * responding — raw byte append plus a boundary offset, never a
189
+ * separator; the fold pyramid advances by O(turn).
190
+ *
191
+ * This is the primitive for turns the Mind should hear but not answer:
192
+ * replaying a transcript, feeding the OTHER speaker's line in a
193
+ * prediction harness, or restoring context piecewise. {@link
194
+ * respondTurn} = addTurn + think + its own reply appended the same way. */
195
+ addTurn(conv: Conversation, turn: Input): ConversationState;
196
+ /** Grow a conversation's accumulated context by one turn's bytes — raw
197
+ * append plus a boundary offset, pyramid advanced by O(turn), the grown
198
+ * context's tree seeded into the conversation's perceive memo. The ONE
199
+ * place a context grows ({@link addTurn} and {@link respondTurn} both
200
+ * come through here), so the append semantics cannot drift. */
201
+ private _growContext;
202
+ /** Process one turn of a conversation.
203
+ *
204
+ * `turn` is the raw input for the latest turn — its bytes are appended
205
+ * to the accumulated context directly (raw concatenation). The Mind
206
+ * tracks the byte offset where each turn ends; no separator is ever
207
+ * inserted or inspected.
208
+ *
209
+ * Returns the response AND the updated {@link ConversationState} so the
210
+ * caller can persist it. The conversation handle's internal state is
211
+ * updated in place — the returned state is a snapshot for storage.
212
+ *
213
+ * SINGLE FLIGHT: at most one respondTurn may be in flight per Mind. The
214
+ * conversation's memo caches are swapped into the Mind-level per-response
215
+ * pointers for the duration of the turn, so a concurrently-running
216
+ * respond()/respondTurn() on the SAME Mind would interleave state.
217
+ * Different Minds (or sequential awaits, as in every test) are safe. */
218
+ respondTurn(conv: Conversation, turn: Input, inspectRationale?: InspectRationale): Promise<{
219
+ response: Response;
220
+ state: ConversationState;
221
+ }>;
222
+ /** Text view of {@link respondTurn}. See {@link respondText} for the
223
+ * NUL-stripping caveat. For binary or grid turns use {@link respondTurn}
224
+ * directly — this is a text-only convenience, like {@link respondText}. */
225
+ respondTurnText(conv: Conversation, turn: string, inspectRationale?: InspectRationale): Promise<{
226
+ response: string;
227
+ state: ConversationState;
228
+ }>;
118
229
  embedding(input: Input): Promise<Vec | null>;
119
230
  /** Kinship note: the vector arm below is a miniature of recall's tier 3
120
231
  * (resonate → reach gate → read out the nearest form's bytes) — the
@@ -141,6 +252,21 @@ export declare class Mind implements MindContext {
141
252
  * (default 2 — structural bridges)
142
253
  * @returns number of nodes added to the content index */
143
254
  repairContentIndex(minParents?: number): Promise<number>;
255
+ /** Build (or incrementally refresh) the store's canonical-form index: for
256
+ * every content-bearing node, record the hash of its CANONICAL key so
257
+ * resolution can find stored forms across surface variation (case, width,
258
+ * whitespace — whatever `canon` equates; see src/canon.ts).
259
+ *
260
+ * Incremental and idempotent: the last indexed node id is remembered in
261
+ * store meta (`canon.upto`), so a refresh after further training scans
262
+ * only the new rows. Run once after training, and again after ingests —
263
+ * the same operational shape as {@link repairContentIndex}.
264
+ *
265
+ * @param canon the canonicalizer to index under — MUST be the same one
266
+ * queries will carry (text queries carry {@link textCanon}
267
+ * unless the Mind was constructed with its own)
268
+ * @returns number of index rows added */
269
+ buildCanonIndex(canon?: Canon): Promise<number>;
144
270
  save(): Promise<Uint8Array>;
145
271
  static load(snapshot: Uint8Array, store: Store): Promise<Mind>;
146
272
  static loadFromStore(store: Store): Promise<Mind>;