@hviana/sema 0.1.7 → 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 (42) hide show
  1. package/dist/example/train_base.js +24 -4
  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.js +12 -0
  7. package/dist/src/index.d.ts +1 -0
  8. package/dist/src/index.js +1 -0
  9. package/dist/src/mind/learning.js +33 -1
  10. package/dist/src/mind/match.d.ts +4 -2
  11. package/dist/src/mind/match.js +30 -6
  12. package/dist/src/mind/mechanisms/cast.js +18 -4
  13. package/dist/src/mind/mechanisms/confluence.js +13 -1
  14. package/dist/src/mind/mechanisms/recall.js +97 -28
  15. package/dist/src/mind/mind.d.ts +64 -2
  16. package/dist/src/mind/mind.js +152 -23
  17. package/dist/src/mind/primitives.d.ts +9 -0
  18. package/dist/src/mind/primitives.js +68 -1
  19. package/dist/src/mind/recognition.js +11 -1
  20. package/dist/src/mind/types.d.ts +10 -0
  21. package/dist/src/store-sqlite.d.ts +8 -0
  22. package/dist/src/store-sqlite.js +52 -0
  23. package/dist/src/store.d.ts +12 -0
  24. package/example/train_base.ts +29 -4
  25. package/package.json +1 -1
  26. package/src/alu/src/parser.ts +105 -4
  27. package/src/canon.ts +65 -0
  28. package/src/geometry.ts +12 -0
  29. package/src/index.ts +1 -0
  30. package/src/mind/learning.ts +39 -1
  31. package/src/mind/match.ts +29 -6
  32. package/src/mind/mechanisms/cast.ts +21 -6
  33. package/src/mind/mechanisms/confluence.ts +15 -1
  34. package/src/mind/mechanisms/recall.ts +116 -41
  35. package/src/mind/mind.ts +172 -29
  36. package/src/mind/primitives.ts +66 -1
  37. package/src/mind/recognition.ts +10 -0
  38. package/src/mind/types.ts +10 -0
  39. package/src/store-sqlite.ts +68 -0
  40. package/src/store.ts +27 -0
  41. package/test/13-conversation.test.mjs +77 -27
  42. package/test/35-prefix-edge.test.mjs +86 -0
@@ -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,25 +130,11 @@ export async function recallByResonance(ctx, query, pre) {
76
130
  }
77
131
  }
78
132
  }
79
- // 2. Scaffolding-dominated.
80
- if (top.score >= significanceBar(ctx.store.D)) {
81
- const N = corpusN(ctx);
82
- const minVote = consensusFloor(N);
83
- // The committed points of attention ARE the shared climb's roots (same
84
- // query, same k, same DF mode) — read them from Precomputed instead of
85
- // re-climbing, so even a traced response pays for the climb once.
86
- const forest = (await pre.attention()).roots;
87
- if (forest.length > 0 && forest[0].vote >= minVote) {
88
- const g = await project(ctx, forest[0].anchor, queryGist);
89
- if (g) {
90
- return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
91
- }
92
- }
93
- }
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).
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).
98
138
  // The raw cosine punished honest containment — a query fully inside a
99
139
  // longer grounded answer scored √(lenQ/lenG) and was refused — and let a
100
140
  // long answer sharing only scaffolding pass; the query-relative fraction
@@ -109,13 +149,38 @@ export async function recallByResonance(ctx, query, pre) {
109
149
  // subtract the significance bar (3/√D, §8.3) before converting. Derived
110
150
  // from the existing bars; never tuned.
111
151
  const sig = significanceBar(ctx.store.D);
152
+ const reach = reachThreshold(ctx.space.maxGroup);
112
153
  const fracOfQuery = (cos, otherLen) => Math.min(1, Math.max(0, cos - sig) *
113
154
  Math.sqrt(otherLen / Math.max(1, query.length)));
155
+ // 2. Scaffolding-dominated.
156
+ if (top.score >= sig) {
157
+ const N = corpusN(ctx);
158
+ const minVote = consensusFloor(N);
159
+ // The committed points of attention ARE the shared climb's roots (same
160
+ // query, same k, same DF mode) — read them from Precomputed instead of
161
+ // re-climbing, so even a traced response pays for the climb once.
162
+ const forest = (await pre.attention()).roots;
163
+ if (forest.length > 0 && forest[0].vote >= minVote) {
164
+ const g = await project(ctx, forest[0].anchor, queryGist);
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)) {
174
+ return ground(g, "scaffolding-dominated query — ground the consensus-climb anchor", [[forest[0].start, forest[0].end]], CONCEPT);
175
+ }
176
+ }
177
+ }
178
+ // 3. Last resort — the nearest grounded whole-query hit, same gate.
114
179
  for (const h of whole) {
115
180
  const g = await project(ctx, h.id, queryGist);
116
181
  if (g) {
117
182
  if (fracOfQuery(cosine(queryGist, gistOf(ctx, g)), g.length) >=
118
- reachThreshold(ctx.space.maxGroup)) {
183
+ reach) {
119
184
  return ground(g, "last resort: the nearest grounded whole-query hit", [], STEP);
120
185
  }
121
186
  }
@@ -129,7 +194,6 @@ export async function recallByResonance(ctx, query, pre) {
129
194
  // anyway to be echoed, so the decision uses their EXACT fold: one river
130
195
  // fold of the top hit, measured in the same query-relative,
131
196
  // chance-corrected units as the tier above.
132
- const reach = reachThreshold(ctx.space.maxGroup);
133
197
  const topBytes = read(ctx, top.id);
134
198
  const exact = topBytes.length > 0
135
199
  ? cosine(queryGist, gistOf(ctx, topBytes))
@@ -137,6 +201,11 @@ export async function recallByResonance(ctx, query, pre) {
137
201
  if (fracOfQuery(exact, topBytes.length) < reach) {
138
202
  return ground(null, "below reach threshold — nothing in the store relates to this query", [], 0);
139
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
+ }
140
209
  // Honest echo.
141
210
  return ground(topBytes, "last resort: the nearest resonant form's own bytes (echo, not grounded)", [], 0, true);
142
211
  }
@@ -4,6 +4,7 @@ import { Alphabet } from "../alphabet.js";
4
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 };
@@ -55,6 +56,12 @@ export interface MindOptions {
55
56
  mechanisms?: import("./pipeline-mechanism.js").PipelineMechanism[];
56
57
  /** Factories that receive the {@link ExtensionHost} and return mechanisms. */
57
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;
58
65
  }
59
66
  export declare class Mind implements MindContext {
60
67
  readonly space: Space;
@@ -67,6 +74,16 @@ export declare class Mind implements MindContext {
67
74
  readonly mechanisms: import("./pipeline-mechanism.js").PipelineMechanism[];
68
75
  /** The live rationale tracer for the inference currently in flight, or null. */
69
76
  trace: Rationale | null;
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;
70
87
  /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
71
88
  climbMemo: Map<string, Map<string, AttentionRead>> | null;
72
89
  /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
@@ -130,6 +147,10 @@ export declare class Mind implements MindContext {
130
147
  * memos. Paired with {@link endResponse}; the ONE place this state is
131
148
  * created, so adding a memo cannot forget its reset. */
132
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;
133
154
  /** Close one response's transient state — every per-response field, incl.
134
155
  * the edge guide/choices `think` sets mid-flight. */
135
156
  private endResponse;
@@ -141,7 +162,12 @@ export declare class Mind implements MindContext {
141
162
  /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
142
163
  * decoding — they are structural padding in text answers. LOSSY for a
143
164
  * binary answer that legitimately contains NULs: use {@link respond} and
144
- * 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}). */
145
171
  respondText(input: string, inspectRationale?: InspectRationale): Promise<string>;
146
172
  /** Begin a new conversation, optionally restoring from a previously-saved
147
173
  * {@link ConversationState}. The returned handle is required for
@@ -158,6 +184,21 @@ export declare class Mind implements MindContext {
158
184
  /** The current serialisable state of an active conversation. Save this
159
185
  * to resume the conversation later via {@link beginConversation}. */
160
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;
161
202
  /** Process one turn of a conversation.
162
203
  *
163
204
  * `turn` is the raw input for the latest turn — its bytes are appended
@@ -167,7 +208,13 @@ export declare class Mind implements MindContext {
167
208
  *
168
209
  * Returns the response AND the updated {@link ConversationState} so the
169
210
  * caller can persist it. The conversation handle's internal state is
170
- * updated in place — the returned state is a snapshot for storage. */
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. */
171
218
  respondTurn(conv: Conversation, turn: Input, inspectRationale?: InspectRationale): Promise<{
172
219
  response: Response;
173
220
  state: ConversationState;
@@ -205,6 +252,21 @@ export declare class Mind implements MindContext {
205
252
  * (default 2 — structural bridges)
206
253
  * @returns number of nodes added to the content index */
207
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>;
208
270
  save(): Promise<Uint8Array>;
209
271
  static load(snapshot: Uint8Array, store: Store): Promise<Mind>;
210
272
  static loadFromStore(store: Store): Promise<Mind>;
@@ -14,6 +14,7 @@ import { bytesToTreePyramid, reachThreshold, } from "../geometry.js";
14
14
  import { BoundedMap } from "../store.js";
15
15
  import { SQliteStore } from "../store-sqlite.js";
16
16
  import { resolveConfig } from "../config.js";
17
+ import { canonHash, textCanon } from "../canon.js";
17
18
  import { bytesEqual, concat2 } from "../bytes.js";
18
19
  import { GraphSearch, } from "./graph-search.js";
19
20
  import { Alu } from "../alu/src/index.js";
@@ -42,6 +43,16 @@ export class Mind {
42
43
  mechanisms = [];
43
44
  /** The live rationale tracer for the inference currently in flight, or null. */
44
45
  trace = null;
46
+ /** The content canonicalizer for the response in flight — see
47
+ * {@link MindContext.canon}. Injected per response by the modality entry
48
+ * point; null when the response carries no equivalence. */
49
+ canon = null;
50
+ /** Per-response canonical-resolution memo — see {@link MindContext.canonMemo}. */
51
+ canonMemo = null;
52
+ /** The Mind-level canon option: a canonicalizer to use for EVERY response,
53
+ * `false` to disable canonical resolution, or null to let each entry
54
+ * point decide (text entry points inject {@link textCanon}). */
55
+ _canonOpt = null;
45
56
  /** Memo of the consensus climb — content-keyed. See {@link MindContext.climbMemo}. */
46
57
  climbMemo = null;
47
58
  /** Memo of recognise() — content-keyed. See {@link MindContext.recogniseMemo}. */
@@ -104,7 +115,8 @@ export class Mind {
104
115
  this.store = storeArg;
105
116
  }
106
117
  else {
107
- const { store: optsStore, mechanisms: userMechs, mechanismFactories: userFacts, ...rest } = (optsOrCfg ?? {});
118
+ const { store: optsStore, mechanisms: userMechs, mechanismFactories: userFacts, canon: optsCanon, ...rest } = (optsOrCfg ?? {});
119
+ this._canonOpt = optsCanon ?? null;
108
120
  this.cfg = resolveConfig(rest);
109
121
  this.store = optsStore ?? new SQliteStore({
110
122
  maxGroup: this.cfg.geometry.maxGroup,
@@ -166,11 +178,21 @@ export class Mind {
166
178
  /** Open one response's transient state — the tracer and the per-response
167
179
  * memos. Paired with {@link endResponse}; the ONE place this state is
168
180
  * created, so adding a memo cannot forget its reset. */
169
- beginResponse(inspectRationale) {
181
+ beginResponse(inspectRationale, canon) {
170
182
  this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
171
183
  this.climbMemo = new Map();
172
184
  this.recogniseMemo = new Map();
173
185
  this.perceiveMemo = new Map();
186
+ this.canon = canon ?? null;
187
+ this.canonMemo = canon ? new Map() : null;
188
+ }
189
+ /** The canonicalizer a response should carry: the Mind-level option when
190
+ * set (or none when explicitly disabled), else the entry point's own
191
+ * default — text entry points pass {@link textCanon}, binary ones null. */
192
+ _canonFor(entryDefault) {
193
+ if (this._canonOpt === false)
194
+ return null;
195
+ return this._canonOpt ?? entryDefault;
174
196
  }
175
197
  /** Close one response's transient state — every per-response field, incl.
176
198
  * the edge guide/choices `think` sets mid-flight. */
@@ -179,14 +201,16 @@ export class Mind {
179
201
  this.climbMemo = null;
180
202
  this.recogniseMemo = null;
181
203
  this.perceiveMemo = null;
204
+ this.canon = null;
205
+ this.canonMemo = null;
182
206
  this._edgeGuide = null;
183
207
  this._edgeChoice.clear();
184
208
  }
185
209
  /** Shared response core — the one path from bytes to voiced answer.
186
210
  * `respond` calls this directly; `respondTurn` has its own path
187
211
  * with conversation-persistent memos and incremental perception. */
188
- async _respondImpl(queryBytes, inspectRationale, traceLabel = "respond") {
189
- this.beginResponse(inspectRationale);
212
+ async _respondImpl(queryBytes, inspectRationale, traceLabel = "respond", canon = null) {
213
+ this.beginResponse(inspectRationale, canon);
190
214
  try {
191
215
  const top = this.trace?.enter(traceLabel, [
192
216
  rItem(queryBytes, "query"),
@@ -209,12 +233,21 @@ export class Mind {
209
233
  }
210
234
  }
211
235
  async respond(input, inspectRationale) {
212
- return this._respondImpl(inputBytes(this, input), inspectRationale);
236
+ // A STRING input is text by nature: it carries the text equivalence even
237
+ // through the generic entry point. Raw bytes / grids carry only the
238
+ // Mind-level canon option, if any.
239
+ const canon = this._canonFor(typeof input === "string" ? textCanon : null);
240
+ return this._respondImpl(inputBytes(this, input), inspectRationale, "respond", canon);
213
241
  }
214
242
  /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
215
243
  * decoding — they are structural padding in text answers. LOSSY for a
216
244
  * binary answer that legitimately contains NULs: use {@link respond} and
217
- * read `bytes` directly for binary/grid modalities. */
245
+ * read `bytes` directly for binary/grid modalities.
246
+ *
247
+ * Injects the TEXT canonicalizer (src/canon.ts) so resolution treats
248
+ * every character variation of the same text — case, width, whitespace —
249
+ * as one form, provided the store's canon index is built
250
+ * ({@link buildCanonIndex}). */
218
251
  async respondText(input, inspectRationale) {
219
252
  const r = await this.respond(input, inspectRationale);
220
253
  return decodeText(r.bytes);
@@ -261,6 +294,47 @@ export class Mind {
261
294
  boundaries: [...data.boundaries],
262
295
  };
263
296
  }
297
+ /** Append a turn to a conversation's accumulated context WITHOUT
298
+ * responding — raw byte append plus a boundary offset, never a
299
+ * separator; the fold pyramid advances by O(turn).
300
+ *
301
+ * This is the primitive for turns the Mind should hear but not answer:
302
+ * replaying a transcript, feeding the OTHER speaker's line in a
303
+ * prediction harness, or restoring context piecewise. {@link
304
+ * respondTurn} = addTurn + think + its own reply appended the same way. */
305
+ addTurn(conv, turn) {
306
+ const data = this._conversations.get(conv.id);
307
+ if (!data)
308
+ throw new Error(`Conversation ${conv.id} not found`);
309
+ const turnBytes = inputBytes(this, turn);
310
+ this._growContext(data, turnBytes);
311
+ return this.conversationState(conv);
312
+ }
313
+ /** Grow a conversation's accumulated context by one turn's bytes — raw
314
+ * append plus a boundary offset, pyramid advanced by O(turn), the grown
315
+ * context's tree seeded into the conversation's perceive memo. The ONE
316
+ * place a context grows ({@link addTurn} and {@link respondTurn} both
317
+ * come through here), so the append semantics cannot drift. */
318
+ _growContext(data, turnBytes) {
319
+ const prevLen = data.pyramid.bytes;
320
+ // An empty turn neither grows the context nor marks a boundary —
321
+ // boundaries are documented strictly increasing, and a zero-length
322
+ // "turn" is no turn. The pyramid's zero-growth shortcut makes the
323
+ // refold below O(1), so the existing tree is returned unchanged.
324
+ const grow = turnBytes.length > 0;
325
+ const grown = !grow
326
+ ? data.bytes
327
+ : prevLen > 0
328
+ ? concat2(data.bytes, turnBytes)
329
+ : turnBytes;
330
+ if (grow && prevLen > 0)
331
+ data.boundaries.push(prevLen);
332
+ const { tree, pyramid } = bytesToTreePyramid(this.space, this.alphabet, grown, data.pyramid);
333
+ data.pyramid = pyramid;
334
+ data.bytes = grown;
335
+ data.perceiveMemo.set(latin1Key(grown), tree);
336
+ return tree;
337
+ }
264
338
  /** Process one turn of a conversation.
265
339
  *
266
340
  * `turn` is the raw input for the latest turn — its bytes are appended
@@ -270,21 +344,21 @@ export class Mind {
270
344
  *
271
345
  * Returns the response AND the updated {@link ConversationState} so the
272
346
  * caller can persist it. The conversation handle's internal state is
273
- * updated in place — the returned state is a snapshot for storage. */
347
+ * updated in place — the returned state is a snapshot for storage.
348
+ *
349
+ * SINGLE FLIGHT: at most one respondTurn may be in flight per Mind. The
350
+ * conversation's memo caches are swapped into the Mind-level per-response
351
+ * pointers for the duration of the turn, so a concurrently-running
352
+ * respond()/respondTurn() on the SAME Mind would interleave state.
353
+ * Different Minds (or sequential awaits, as in every test) are safe. */
274
354
  async respondTurn(conv, turn, inspectRationale) {
275
355
  const data = this._conversations.get(conv.id);
276
356
  if (!data)
277
357
  throw new Error(`Conversation ${conv.id} not found`);
278
358
  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
359
  // Incremental perception — O(turn) instead of O(context).
286
- const { tree, pyramid } = bytesToTreePyramid(this.space, this.alphabet, newContext, data.pyramid);
287
- data.pyramid = pyramid;
360
+ const tree = this._growContext(data, turnBytes);
361
+ const newContext = data.bytes;
288
362
  // Swap in the conversation's persistent state so the inference
289
363
  // pipeline does not re-process the prefix from scratch.
290
364
  // perceiveMemo / recogniseMemo / climbMemo — content-keyed;
@@ -298,8 +372,11 @@ export class Mind {
298
372
  this.climbMemo = data.climbMemo;
299
373
  this._resolvedSubtrees = data.resolvedSubtrees;
300
374
  this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
375
+ // A string turn is text by nature — carry the text equivalence, same as
376
+ // respond() (see _canonFor).
377
+ this.canon = this._canonFor(typeof turn === "string" ? textCanon : null);
378
+ this.canonMemo = this.canon ? new Map() : null;
301
379
  try {
302
- this.perceiveMemo.set(latin1Key(newContext), tree);
303
380
  const top = this.trace?.enter("respondTurn", [
304
381
  rItem(newContext, "query"),
305
382
  ]);
@@ -313,6 +390,14 @@ export class Mind {
313
390
  }
314
391
  const voiced = await articulate(this, thought.bytes, newContext);
315
392
  top?.done([rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined)], "the answer, re-voiced in the asker's words");
393
+ // The REPLY joins the accumulated context the same way a turn does
394
+ // ({@link addTurn}): raw byte append plus a boundary offset — never a
395
+ // separator. A conversation's context is the full exchange, exactly
396
+ // the cumulative continuous shape multi-turn training deposits, so a
397
+ // later turn can refer to what was ANSWERED ("which of those two…"),
398
+ // not only to what was asked.
399
+ if (voiced.length > 0)
400
+ this.addTurn(conv, voiced);
316
401
  return {
317
402
  response: {
318
403
  v: gistOf(this, voiced),
@@ -323,17 +408,19 @@ export class Mind {
323
408
  };
324
409
  }
325
410
  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.
411
+ // No save-back: the conversation's memo MAPS were mutated in place —
412
+ // `data.*` still points at them. Re-assigning from the Mind-level
413
+ // pointers here was a no-op in the single-flight case and, under a
414
+ // concurrently-started respond() (which swaps its own fresh maps into
415
+ // those pointers), would inject a FOREIGN response's memos into this
416
+ // conversation. Clear Mind references — non-conversation respond()
417
+ // calls get fresh per-response memos.
333
418
  this.trace = null;
334
419
  this.perceiveMemo = null;
335
420
  this.recogniseMemo = null;
336
421
  this.climbMemo = null;
422
+ this.canon = null;
423
+ this.canonMemo = null;
337
424
  this._resolvedSubtrees = null;
338
425
  this._edgeGuide = null;
339
426
  this._edgeChoice.clear();
@@ -414,6 +501,48 @@ export class Mind {
414
501
  return gistOf(this, bytes);
415
502
  }, minParents);
416
503
  }
504
+ // ── Canonical-form index ───────────────────────────────────────────────
505
+ /** Build (or incrementally refresh) the store's canonical-form index: for
506
+ * every content-bearing node, record the hash of its CANONICAL key so
507
+ * resolution can find stored forms across surface variation (case, width,
508
+ * whitespace — whatever `canon` equates; see src/canon.ts).
509
+ *
510
+ * Incremental and idempotent: the last indexed node id is remembered in
511
+ * store meta (`canon.upto`), so a refresh after further training scans
512
+ * only the new rows. Run once after training, and again after ingests —
513
+ * the same operational shape as {@link repairContentIndex}.
514
+ *
515
+ * @param canon the canonicalizer to index under — MUST be the same one
516
+ * queries will carry (text queries carry {@link textCanon}
517
+ * unless the Mind was constructed with its own)
518
+ * @returns number of index rows added */
519
+ async buildCanonIndex(canon) {
520
+ const c = canon ?? this._canonFor(textCanon);
521
+ const store = this.store;
522
+ if (c === null || !store.canonAdd || !store.eachContent)
523
+ return 0;
524
+ const from = Number(await store.getMeta("canon.upto") ?? 0);
525
+ let added = 0;
526
+ let maxId = from - 1;
527
+ store.eachContent((id, bytes) => {
528
+ if (id > maxId)
529
+ maxId = id;
530
+ const key = c(bytes);
531
+ if (key.length === 0)
532
+ return;
533
+ // Only index content whose canonical key DIFFERS from its raw bytes —
534
+ // an already-canonical span is found by the exact lookup (and by the
535
+ // fallback's own exact probe of the canonical bytes), so indexing it
536
+ // would only add rows.
537
+ if (bytesEqual(key, bytes))
538
+ return;
539
+ store.canonAdd(canonHash(key), id);
540
+ added++;
541
+ }, from);
542
+ await store.setMeta("canon.upto", String(maxId + 1));
543
+ store.commit();
544
+ return added;
545
+ }
417
546
  // ── Persistence ──────────────────────────────────────────────────────────
418
547
  async save() {
419
548
  const meta = new TextEncoder().encode(JSON.stringify(this.cfg));
@@ -41,6 +41,15 @@ export declare function foldTree(ctx: MindContext, n: Sema, start: number, visit
41
41
  * training did — and recover its root bottom-up. Returns null if any part is
42
42
  * unknown. */
43
43
  export declare function resolve(ctx: MindContext, bytes: Uint8Array): number | null;
44
+ /** Equivalence-class resolution: when the exact content-addressed lookup
45
+ * misses, find a stored node whose CANONICAL key equals the span's — the
46
+ * store's canon index proposes candidates by key hash, and each is verified
47
+ * by re-canonicalizing its bytes (hash-then-verify, like every content
48
+ * lookup). Among verified candidates, one that leads somewhere (has a
49
+ * continuation edge) is preferred; ties break to the lowest id — a corpus
50
+ * property, not a seed property. Null when the response carries no
51
+ * canonicalizer, the store has no canon index, or nothing verifies. */
52
+ export declare function canonResolve(ctx: MindContext, bytes: Uint8Array): number | null;
44
53
  /** Walk a perceived tree in POST-ORDER with byte offsets — children before
45
54
  * their parent, `visit(node, start, end)` for every node including leaves.
46
55
  * Returns the byte end. The one shared traversal the offset-carrying tree