@hviana/sema 0.1.9 → 0.2.1

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 (131) hide show
  1. package/dist/example/demo.d.ts +1 -0
  2. package/dist/example/demo.js +39 -0
  3. package/dist/example/train_base.d.ts +87 -0
  4. package/dist/example/train_base.js +2252 -0
  5. package/dist/src/alphabet.d.ts +7 -0
  6. package/dist/src/alphabet.js +33 -0
  7. package/dist/src/alu/src/alu.d.ts +185 -0
  8. package/dist/src/alu/src/alu.js +440 -0
  9. package/dist/src/alu/src/expr.d.ts +61 -0
  10. package/dist/src/alu/src/expr.js +318 -0
  11. package/dist/src/alu/src/index.d.ts +11 -0
  12. package/dist/src/alu/src/index.js +19 -0
  13. package/dist/src/alu/src/kernel-arith.d.ts +16 -0
  14. package/dist/src/alu/src/kernel-arith.js +264 -0
  15. package/dist/src/alu/src/kernel-bits.d.ts +19 -0
  16. package/dist/src/alu/src/kernel-bits.js +152 -0
  17. package/dist/src/alu/src/kernel-logic.d.ts +4 -0
  18. package/dist/src/alu/src/kernel-logic.js +60 -0
  19. package/dist/src/alu/src/kernel-nd.d.ts +3 -0
  20. package/dist/src/alu/src/kernel-nd.js +208 -0
  21. package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
  22. package/dist/src/alu/src/kernel-numeric.js +366 -0
  23. package/dist/src/alu/src/operation.d.ts +168 -0
  24. package/dist/src/alu/src/operation.js +189 -0
  25. package/dist/src/alu/src/parser.d.ts +221 -0
  26. package/dist/src/alu/src/parser.js +577 -0
  27. package/dist/src/alu/src/resonance.d.ts +55 -0
  28. package/dist/src/alu/src/resonance.js +126 -0
  29. package/dist/src/alu/src/text.d.ts +31 -0
  30. package/dist/src/alu/src/text.js +73 -0
  31. package/dist/src/alu/src/value.d.ts +109 -0
  32. package/dist/src/alu/src/value.js +300 -0
  33. package/dist/src/alu/test/alu.test.d.ts +1 -0
  34. package/dist/src/alu/test/alu.test.js +764 -0
  35. package/dist/src/bytes.d.ts +14 -0
  36. package/dist/src/bytes.js +59 -0
  37. package/dist/src/canon.d.ts +26 -0
  38. package/dist/src/canon.js +57 -0
  39. package/dist/src/config.d.ts +111 -0
  40. package/dist/src/config.js +91 -0
  41. package/dist/src/derive/src/deduction.d.ts +125 -0
  42. package/dist/src/derive/src/deduction.js +155 -0
  43. package/dist/src/derive/src/index.d.ts +7 -0
  44. package/dist/src/derive/src/index.js +11 -0
  45. package/dist/src/derive/src/priority-queue.d.ts +20 -0
  46. package/dist/src/derive/src/priority-queue.js +73 -0
  47. package/dist/src/derive/src/rewrite.d.ts +56 -0
  48. package/dist/src/derive/src/rewrite.js +100 -0
  49. package/dist/src/derive/src/trie.d.ts +90 -0
  50. package/dist/src/derive/src/trie.js +217 -0
  51. package/dist/src/derive/test/derive.test.d.ts +1 -0
  52. package/dist/src/derive/test/derive.test.js +122 -0
  53. package/dist/src/extension.d.ts +37 -0
  54. package/dist/src/extension.js +7 -0
  55. package/dist/src/geometry.d.ts +148 -0
  56. package/dist/src/geometry.js +511 -0
  57. package/dist/src/index.d.ts +16 -0
  58. package/dist/src/index.js +18 -0
  59. package/dist/src/ingest-cache.d.ts +41 -0
  60. package/dist/src/ingest-cache.js +161 -0
  61. package/dist/src/mind/articulation.d.ts +6 -0
  62. package/dist/src/mind/articulation.js +99 -0
  63. package/dist/src/mind/attention.d.ts +72 -0
  64. package/dist/src/mind/attention.js +898 -0
  65. package/dist/src/mind/canonical.d.ts +29 -0
  66. package/dist/src/mind/canonical.js +88 -0
  67. package/dist/src/mind/graph-search.d.ts +270 -0
  68. package/dist/src/mind/graph-search.js +927 -0
  69. package/dist/src/mind/index.d.ts +5 -0
  70. package/dist/src/mind/index.js +5 -0
  71. package/dist/src/mind/junction.d.ts +95 -0
  72. package/dist/src/mind/junction.js +262 -0
  73. package/dist/src/mind/learning.d.ts +47 -0
  74. package/dist/src/mind/learning.js +233 -0
  75. package/dist/src/mind/match.d.ts +113 -0
  76. package/dist/src/mind/match.js +446 -0
  77. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  78. package/dist/src/mind/mechanisms/alu.js +29 -0
  79. package/dist/src/mind/mechanisms/cast.d.ts +35 -0
  80. package/dist/src/mind/mechanisms/cast.js +461 -0
  81. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  82. package/dist/src/mind/mechanisms/confluence.js +225 -0
  83. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  84. package/dist/src/mind/mechanisms/cover.js +179 -0
  85. package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
  86. package/dist/src/mind/mechanisms/extraction.js +342 -0
  87. package/dist/src/mind/mechanisms/recall.d.ts +13 -0
  88. package/dist/src/mind/mechanisms/recall.js +235 -0
  89. package/dist/src/mind/mind.d.ts +273 -0
  90. package/dist/src/mind/mind.js +598 -0
  91. package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
  92. package/dist/src/mind/pipeline-mechanism.js +213 -0
  93. package/dist/src/mind/pipeline.d.ts +20 -0
  94. package/dist/src/mind/pipeline.js +185 -0
  95. package/dist/src/mind/primitives.d.ts +63 -0
  96. package/dist/src/mind/primitives.js +251 -0
  97. package/dist/src/mind/rationale.d.ts +134 -0
  98. package/dist/src/mind/rationale.js +162 -0
  99. package/dist/src/mind/reasoning.d.ts +15 -0
  100. package/dist/src/mind/reasoning.js +162 -0
  101. package/dist/src/mind/recognition.d.ts +20 -0
  102. package/dist/src/mind/recognition.js +246 -0
  103. package/dist/src/mind/resonance.d.ts +23 -0
  104. package/dist/src/mind/resonance.js +0 -0
  105. package/dist/src/mind/trace.d.ts +15 -0
  106. package/dist/src/mind/trace.js +73 -0
  107. package/dist/src/mind/traverse.d.ts +113 -0
  108. package/dist/src/mind/traverse.js +488 -0
  109. package/dist/src/mind/types.d.ts +186 -0
  110. package/dist/src/mind/types.js +84 -0
  111. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  112. package/dist/src/rabitq-ivf/src/database.js +201 -0
  113. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  114. package/dist/src/rabitq-ivf/src/index.js +4 -0
  115. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  116. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  117. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  118. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  119. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  120. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  121. package/dist/src/sema.d.ts +31 -0
  122. package/dist/src/sema.js +63 -0
  123. package/dist/src/store-sqlite.d.ts +184 -0
  124. package/dist/src/store-sqlite.js +942 -0
  125. package/dist/src/store.d.ts +654 -0
  126. package/dist/src/store.js +1612 -0
  127. package/dist/src/vec.d.ts +31 -0
  128. package/dist/src/vec.js +109 -0
  129. package/package.json +1 -1
  130. package/src/mind/graph-search.ts +128 -46
  131. package/src/mind/mind.ts +37 -0
@@ -0,0 +1,251 @@
1
+ // primitives.ts — Address + Read primitives (Section 1 of the mind).
2
+ //
3
+ // Address — bytes → node (perceive, foldTree, resolve)
4
+ // Read — node → bytes (read)
5
+ import { bytesToTree, bytesToTreePyramid, gridToTree, hilbertBytes, stackGrids, } from "../geometry.js";
6
+ import { canonHash } from "../canon.js";
7
+ import { bytesEqual } from "../bytes.js";
8
+ import { ALL } from "./types.js";
9
+ // ── Address: bytes → node ──────────────────────────────────────────────
10
+ /** The content key of a byte span — one latin1 char per byte, an exact,
11
+ * collision-free encoding. Spans on the perception path are query-scale
12
+ * (windows, regions, candidate spans), so key construction is far cheaper
13
+ * than the river fold it deduplicates. */
14
+ export function latin1Key(bytes) {
15
+ // Batched String.fromCharCode — avoids the O(n²) cost of repeated += on
16
+ // potentially-large query spans, and stays well under the ~65536 arg limit.
17
+ const n = bytes.length;
18
+ let s = "";
19
+ for (let i = 0; i < n; i += 4096) {
20
+ s += String.fromCharCode(...bytes.subarray(i, Math.min(i + 4096, n)));
21
+ }
22
+ return s;
23
+ }
24
+ /** Perceive input into a content-defined tree (the river fold).
25
+ * Deterministic — identical bytes always produce an identical tree.
26
+ *
27
+ * `boundaries` is an optional sorted list of proper byte offsets where the
28
+ * fold must split so that each prefix segment folds identically to how it
29
+ * folded when it was learned (§10.3 stable-prefix contract). Only the
30
+ * CALLER — who assembled the multi-turn context — knows where those
31
+ * boundaries are; the geometry never guesses them from the bytes. */
32
+ export function perceive(ctx, input, leafAt, lookup, boundaries) {
33
+ if (typeof input === "string" || input instanceof Uint8Array) {
34
+ const bytes = typeof input === "string"
35
+ ? new TextEncoder().encode(input)
36
+ : input;
37
+ if (leafAt === undefined && lookup === undefined) {
38
+ // Per-response memo (see MindContext.perceiveMemo): only the plain
39
+ // inference shape — raw bytes, no store capabilities — is memoised,
40
+ // keyed by CONTENT so byte-identical spans in fresh arrays still hit.
41
+ // The tree is shared by reference; Sema nodes are never mutated.
42
+ const memo = ctx.perceiveMemo;
43
+ if (memo) {
44
+ const key = latin1Key(bytes);
45
+ const hit = memo.get(key);
46
+ if (hit !== undefined)
47
+ return hit;
48
+ const tree = bytesToTree(ctx.space, ctx.alphabet, bytes, undefined, undefined, boundaries);
49
+ memo.set(key, tree);
50
+ return tree;
51
+ }
52
+ return bytesToTree(ctx.space, ctx.alphabet, bytes, undefined, undefined, boundaries);
53
+ }
54
+ return bytesToTree(ctx.space, ctx.alphabet, bytes, leafAt, lookup);
55
+ }
56
+ if (Array.isArray(input)) {
57
+ return gridToTree(ctx.space, ctx.alphabet, stackGrids(input));
58
+ }
59
+ return gridToTree(ctx.space, ctx.alphabet, input);
60
+ }
61
+ /** The DEPOSIT-shaped perceive: the PLAIN fold (bit-identical to inference
62
+ * perception — that structural train/inference agreement is load-bearing
63
+ * for exact recall), computed INCREMENTALLY via the fold's level pyramid
64
+ * ({@link bytesToTreePyramid}). An accumulated context (a conversation)
65
+ * grows by suffixes: the previous context's pyramid is cached by CONTENT
66
+ * (ctx._depositTrees), and this deposit refolds only the right edge of
67
+ * each level — O(turn) instead of O(context) per turn. Purely a cache:
68
+ * the produced tree never depends on cache state. */
69
+ export function perceiveDeposit(ctx, bytes) {
70
+ let prev;
71
+ // Longest cached PROPER prefix first.
72
+ const lens = [...ctx._depositLens]
73
+ .filter((L) => L >= 2 && L < bytes.length)
74
+ .sort((a, b) => b - a);
75
+ for (const L of lens) {
76
+ const hit = ctx._depositTrees.get(latin1Key(bytes.subarray(0, L)));
77
+ if (hit !== undefined) {
78
+ prev = hit;
79
+ break;
80
+ }
81
+ }
82
+ const { tree, pyramid } = bytesToTreePyramid(ctx.space, ctx.alphabet, bytes, prev);
83
+ if (bytes.length >= 2) {
84
+ // The lengths set drifts as the map evicts; past the probe budget the
85
+ // drift itself becomes the cost (each stale length is an O(len) key
86
+ // build), so both reset together — losing only warm-up on live chains.
87
+ if (ctx._depositLens.size > 64) {
88
+ ctx._depositLens.clear();
89
+ ctx._depositTrees.clear();
90
+ }
91
+ ctx._depositTrees.set(latin1Key(bytes), pyramid);
92
+ ctx._depositLens.add(bytes.length);
93
+ }
94
+ return tree;
95
+ }
96
+ /** The raw bytes of an input — modality-neutral conversion. */
97
+ export function inputBytes(ctx, input) {
98
+ if (typeof input === "string")
99
+ return new TextEncoder().encode(input);
100
+ if (input instanceof Uint8Array)
101
+ return input;
102
+ if (Array.isArray(input))
103
+ return hilbertBytes(stackGrids(input));
104
+ return hilbertBytes(input);
105
+ }
106
+ /** Convenience: the gist vector of a byte span. */
107
+ export function gistOf(ctx, bytes) {
108
+ return perceive(ctx, bytes).v;
109
+ }
110
+ /** Fold a perceived tree bottom-up against the store's content-addressed maps:
111
+ * every leaf is named by findLeaf, every branch by findBranch over its kids'
112
+ * ids (null the moment any child is unknown). `visit`, when given, sees each
113
+ * node with its byte span and resolved id. Returns the node's byte end and
114
+ * resolved id. */
115
+ export function foldTree(ctx, n, start, visit) {
116
+ // Fast path: subtree already resolved (from a previous conversation turn
117
+ // or an earlier recognition pass). The pyramid reuses prefix subtrees as
118
+ // identical Sema objects, so this cache turns foldTree into O(suffix)
119
+ // instead of O(context) for multi-turn recognition.
120
+ const cached = ctx._resolvedSubtrees?.get(n);
121
+ if (cached !== undefined) {
122
+ const end = start + cached.len;
123
+ visit?.(n, start, end, cached.id);
124
+ return { end, node: cached.id };
125
+ }
126
+ if (n.kids === null) {
127
+ const b = n.leaf ?? new Uint8Array(0);
128
+ const end = start + b.length;
129
+ const node = ctx.store.findLeaf(b);
130
+ visit?.(n, start, end, node);
131
+ if (node !== null && ctx._resolvedSubtrees) {
132
+ ctx._resolvedSubtrees.set(n, { id: node, len: b.length });
133
+ }
134
+ return { end, node };
135
+ }
136
+ let pos = start;
137
+ let known = true;
138
+ const kids = [];
139
+ for (const k of n.kids) {
140
+ const r = foldTree(ctx, k, pos, visit);
141
+ if (r.node === null)
142
+ known = false;
143
+ else if (known)
144
+ kids.push(r.node);
145
+ pos = r.end;
146
+ }
147
+ const node = known ? ctx.store.findBranch(kids) : null;
148
+ visit?.(n, start, pos, node);
149
+ if (node !== null && ctx._resolvedSubtrees) {
150
+ ctx._resolvedSubtrees.set(n, { id: node, len: pos - start });
151
+ }
152
+ return { end: pos, node };
153
+ }
154
+ /** The canonical node id of a byte span: perceive it in isolation — the way
155
+ * training did — and recover its root bottom-up. Returns null if any part is
156
+ * unknown. */
157
+ export function resolve(ctx, bytes) {
158
+ if (bytes.length === 0)
159
+ return null;
160
+ const exact = foldTree(ctx, perceive(ctx, bytes), 0).node;
161
+ if (exact !== null)
162
+ return exact;
163
+ return canonResolve(ctx, bytes);
164
+ }
165
+ /** Equivalence-class resolution: when the exact content-addressed lookup
166
+ * misses, find a stored node whose CANONICAL key equals the span's — the
167
+ * store's canon index proposes candidates by key hash, and each is verified
168
+ * by re-canonicalizing its bytes (hash-then-verify, like every content
169
+ * lookup). Among verified candidates, one that leads somewhere (has a
170
+ * continuation edge) is preferred; ties break to the lowest id — a corpus
171
+ * property, not a seed property. Null when the response carries no
172
+ * canonicalizer, the store has no canon index, or nothing verifies. */
173
+ export function canonResolve(ctx, bytes) {
174
+ const canon = ctx.canon;
175
+ const store = ctx.store;
176
+ if (canon === null || !store.canonFind)
177
+ return null;
178
+ if (bytes.length < 2)
179
+ return null;
180
+ const memo = ctx.canonMemo;
181
+ const memoKey = memo ? latin1Key(bytes) : "";
182
+ if (memo) {
183
+ const hit = memo.get(memoKey);
184
+ if (hit !== undefined)
185
+ return hit;
186
+ }
187
+ const set = (v) => {
188
+ memo?.set(memoKey, v);
189
+ return v;
190
+ };
191
+ const key = canon(bytes);
192
+ if (key.length === 0)
193
+ return set(null);
194
+ // A stored form that IS canonical is not in the index (buildCanonIndex
195
+ // skips identity rows) — the exact content-addressed lookup of the
196
+ // canonical bytes finds it directly.
197
+ if (key.length !== bytes.length || !bytesEqual(key, bytes)) {
198
+ const direct = foldTree(ctx, perceive(ctx, key), 0).node;
199
+ if (direct !== null)
200
+ return set(direct);
201
+ }
202
+ const candidates = store.canonFind(canonHash(key));
203
+ if (candidates.length === 0)
204
+ return set(null);
205
+ let best = null;
206
+ let bestLeads = false;
207
+ for (const id of candidates) {
208
+ const bytesOf = read(ctx, id);
209
+ const stored = canon(bytesOf);
210
+ if (stored.length !== key.length || !bytesEqual(stored, key))
211
+ continue;
212
+ // The index stores FLAT content twins; the id the exact path would have
213
+ // resolved for these bytes is their FOLD — the deposit-shaped node that
214
+ // carries the edges and halos. Re-folding the candidate's bytes lands
215
+ // on exactly the node the canonical-case query would have found.
216
+ const folded = foldTree(ctx, perceive(ctx, bytesOf), 0).node;
217
+ const use = folded ?? id;
218
+ const leads = store.hasNext(use) || store.haloMass(use) > 0;
219
+ if (best === null || (leads && !bestLeads) ||
220
+ (leads === bestLeads && use < best)) {
221
+ best = use;
222
+ bestLeads = leads;
223
+ }
224
+ }
225
+ return set(best);
226
+ }
227
+ /** Walk a perceived tree in POST-ORDER with byte offsets — children before
228
+ * their parent, `visit(node, start, end)` for every node including leaves.
229
+ * Returns the byte end. The one shared traversal the offset-carrying tree
230
+ * readers (recognition via foldTree's richer variant, attention's region
231
+ * collection, resonance's branch counting) build on, so each does not
232
+ * re-derive the offset bookkeeping. (recognition.segment keeps its own
233
+ * walk: its flush semantics need PRE-order decisions at leaf-parents, which
234
+ * a post-order visitor cannot express.) */
235
+ export function walkTree(n, start, visit) {
236
+ if (n.kids === null) {
237
+ const end = start + (n.leaf?.length ?? 0);
238
+ visit(n, start, end);
239
+ return end;
240
+ }
241
+ let pos = start;
242
+ for (const k of n.kids)
243
+ pos = walkTree(k, pos, visit);
244
+ visit(n, start, pos);
245
+ return pos;
246
+ }
247
+ // ── Read: node → bytes ──────────────────────────────────────────────────
248
+ /** Reconstruct a node's byte content from the DAG, up to `maxLen` bytes. */
249
+ export function read(ctx, id, maxLen = ALL) {
250
+ return ctx.store.bytesPrefix(id, maxLen);
251
+ }
@@ -0,0 +1,134 @@
1
+ import type { Vec } from "../vec.js";
2
+ /** One element of a step's input or output vector.
3
+ *
4
+ * Modality-neutral and deliberately partial: an element might be a byte span of
5
+ * the query, a resolved graph node, a resonance hit with its score, a spliced
6
+ * connector — so every descriptive field is optional and a mechanism fills only
7
+ * the ones that carry meaning for what it did. `text` is always present (the
8
+ * human-readable rendering); the rest is provenance a debugger can lean on. */
9
+ export interface RationaleItem {
10
+ /** Human-readable rendering — decoded text for a byte span, else a label like
11
+ * "‹none›" or an operator name. Always set, so a step always reads. */
12
+ text: string;
13
+ /** The graph node this element is, or resolved to, when known — the handle to
14
+ * point back at the exact stored fact in the content-addressed DAG. */
15
+ node?: number;
16
+ /** The `[start, end)` span this element occupies in its step's frame of
17
+ * reference (usually the query or the answer being composed). */
18
+ span?: [number, number];
19
+ /** The resonance / cosine score that selected this element, when it was chosen
20
+ * by similarity rather than by exact structure. */
21
+ score?: number;
22
+ /** A short role tag — "query", "leaf", "form", "hit", "connector", "answer",
23
+ * … — naming what KIND of element this is within the step. */
24
+ role?: string;
25
+ /** The gist vector, only when the element fundamentally IS a vector and a
26
+ * caller asked to carry it (off by default — a D-float array per item would
27
+ * bury the reasoning it is meant to explain). */
28
+ v?: Vec;
29
+ }
30
+ /** A single completed act of inference — one mechanism, run once.
31
+ *
32
+ * Steps are emitted in COMPLETION order (a sub-mechanism finishes, and is
33
+ * reported, before the mechanism that called it), while `index` is assigned in
34
+ * ENTRY order (a parent reserves its index before its children run). So a
35
+ * parent's `index` is always lower than its children's, and the two orderings
36
+ * together give a valid topological reading of the dependency graph. */
37
+ export interface RationaleStep {
38
+ /** This step's index, assigned when the mechanism was ENTERED — a strict,
39
+ * incremental ordering over the whole inference. */
40
+ index: number;
41
+ /** The mechanism and its enclosing mechanisms, outermost → innermost, e.g.
42
+ * `["respond", "think", "recognise"]`. The last entry is this step; the
43
+ * prefix is the nest of sub-mechanisms it ran inside. */
44
+ mechanism: string[];
45
+ /** The enclosing mechanism's step index, or -1 at the root — the NESTING edge
46
+ * of the dependency graph (which step this one is a part of). */
47
+ parent: number;
48
+ /** The earlier steps whose OUTPUTS became this step's inputs — the DATA-FLOW
49
+ * edges of the dependency graph. Defaults to the previous sibling (the step
50
+ * run just before this one inside the same mechanism), or the parent when
51
+ * this is the first sub-step; a mechanism that fuses several earlier results
52
+ * names them all explicitly. */
53
+ dependsOn: number[];
54
+ /** The vector of elements handed to the mechanism (one or more). */
55
+ inputs: RationaleItem[];
56
+ /** The vector of elements the mechanism produced (one or more) — longer than
57
+ * `inputs` when it decomposed, shorter when it combined. */
58
+ outputs: RationaleItem[];
59
+ /** A one-line, human account of what the mechanism did and why — the sentence
60
+ * that turns the data into an explanation. */
61
+ note?: string;
62
+ }
63
+ /** The callback {@link Mind.respond} / {@link Mind.respondText} accept. It is
64
+ * invoked once per completed step, AS the inference unfolds — never batched at
65
+ * the end — so a caller can stream the reasoning live or accumulate it. */
66
+ export type InspectRationale = (step: RationaleStep) => void;
67
+ /** Decode bytes to text for display, dropping the NUL padding the encoder uses
68
+ * (the same cleanup {@link Mind.respondText} does for its result). */
69
+ export declare function decodeText(bytes: Uint8Array): string;
70
+ /** The `[start, end)` gaps of `[0, queryLen)` NOT covered by `accounted` —
71
+ * the same union-of-spans reading think's grounding decider prices at PASS
72
+ * per byte, exposed here so a mechanism can turn it into a human label. */
73
+ export declare function unexplainedSpans(queryLen: number, accounted: ReadonlyArray<[number, number]>): Array<[number, number]>;
74
+ /** A human-readable label for the query bytes a mechanism's `accounted`
75
+ * spans leave unexplained — purely diagnostic (Task 2's negative evidence):
76
+ * it never changes a candidate's weight, only what the rationale trace
77
+ * says the mechanism left on the table. `""` when nothing is unexplained. */
78
+ export declare function unexplainedLabel(query: Uint8Array, accounted: ReadonlyArray<[number, number]>): string;
79
+ /** An open mechanism — the handle {@link Rationale.enter} returns. Hold it for
80
+ * the duration of the mechanism and call {@link Scope.done} with the outputs
81
+ * when it finishes; that emits the step and pops the nesting. */
82
+ export interface Scope {
83
+ /** The step index reserved for this mechanism at entry — pass it as an
84
+ * explicit dependency of a later step that consumes this one's output. */
85
+ readonly index: number;
86
+ /** Close the mechanism: emit its step with these outputs and pop it off the
87
+ * nesting stack. Idempotent — a second call is ignored, so a `finally` that
88
+ * closes after an early return is safe. */
89
+ done(outputs: RationaleItem[], note?: string): void;
90
+ }
91
+ /** The live tracer: a stack of open mechanisms over one {@link Mind.respond}.
92
+ *
93
+ * Sema's inference is single-threaded and strictly sequential — every async
94
+ * step is awaited before the next begins, and `respond` holds no two thoughts
95
+ * at once — so a plain stack exactly tracks the current nesting: {@link enter}
96
+ * pushes, {@link Scope.done} pops, and {@link step} (a mechanism with no
97
+ * sub-steps) is the two fused. The tracer never branches the control flow; it
98
+ * only records it. */
99
+ export declare class Rationale {
100
+ private readonly sink;
101
+ private next;
102
+ /** Open mechanisms, outermost first. Each frame remembers the last child it
103
+ * has spawned so the next sibling can default its data-flow edge to it. */
104
+ private readonly stack;
105
+ /** The most recent step index emitted under each mechanism name — the handle
106
+ * a later step uses to name an EARLIER mechanism as its data-flow producer
107
+ * (e.g. cover depends on the latest recognise / computeExtensions). One tracer is
108
+ * built per response and inference is sequential, so "most recent" is exactly
109
+ * "the one that produced the inputs I am about to consume". */
110
+ private readonly lastByName;
111
+ constructor(sink: InspectRationale);
112
+ /** The index of the most recent step with this mechanism name, or undefined if
113
+ * none has run. Used to wire an explicit producer edge into {@link
114
+ * Scope.done} / {@link step}'s `deps`. */
115
+ lastIndex(name: string): number | undefined;
116
+ /** The mechanism names currently open, outermost → innermost. */
117
+ private path;
118
+ /** The default data-flow edge for a step entering now: the previous sibling
119
+ * inside the current mechanism, else the enclosing mechanism, else nothing
120
+ * (the root). An explicit `deps` overrides this. */
121
+ private defaultDeps;
122
+ /** Reserve this step's index and register it as the current mechanism's most
123
+ * recent child (so the NEXT sibling chains to it) and as the most recent step
124
+ * of its own NAME (so a later mechanism can name it as a producer). */
125
+ private reserve;
126
+ private emit;
127
+ /** Enter a mechanism that has sub-steps. Captures its inputs and the nesting
128
+ * now; the matching {@link Scope.done} supplies the outputs when it finishes.
129
+ * `deps` overrides the default data-flow edge (previous sibling / parent). */
130
+ enter(name: string, inputs: RationaleItem[], deps?: number[]): Scope;
131
+ /** Record a mechanism that has no sub-steps — its inputs and outputs are both
132
+ * known at the call site. Returns its index, for a later step to depend on. */
133
+ step(name: string, inputs: RationaleItem[], outputs: RationaleItem[], note?: string, deps?: number[]): number;
134
+ }
@@ -0,0 +1,162 @@
1
+ // rationale.ts — the inference, told as it happens.
2
+ //
3
+ // Sema's edge over a weight matrix is that every answer is a DERIVATION over
4
+ // explicit facts, not a sample from an opaque distribution. This module turns
5
+ // that derivation into a stream a human (or a debugger) can read: as {@link
6
+ // Mind.respond} thinks, each inference MECHANISM it runs emits a {@link
7
+ // RationaleStep} the moment it completes — what it was handed, what it produced,
8
+ // where it sits in the nesting of mechanisms, and which earlier steps fed it.
9
+ //
10
+ // Nothing here drives the inference; it only WITNESSES it. When no
11
+ // `inspectRationale` callback is supplied the tracer is never constructed and
12
+ // the cost is exactly zero — every emit site in src/mind/mind.ts is guarded by `?.`, and
13
+ // optional-chaining short-circuits its arguments, so the items are not even
14
+ // built (see {@link Mind.respond}).
15
+ //
16
+ // The shape of a step mirrors how Sema reasons. A mechanism is rarely a 1→1
17
+ // map: {@link Mind.recognise} DECOMPOSES one query into many recognised forms;
18
+ // the cover COMBINES many forms back into one answer; resonance fans one gist
19
+ // out into a ranked list of hits. So a step's `inputs` and `outputs` are each a
20
+ // VECTOR — an ordered list of {@link RationaleItem}s, one per element — and the
21
+ // fan-out / fan-in is visible in their lengths.
22
+ /** Decode bytes to text for display, dropping the NUL padding the encoder uses
23
+ * (the same cleanup {@link Mind.respondText} does for its result). */
24
+ export function decodeText(bytes) {
25
+ return new TextDecoder().decode(bytes.filter((b) => b !== 0x00));
26
+ }
27
+ /** The `[start, end)` gaps of `[0, queryLen)` NOT covered by `accounted` —
28
+ * the same union-of-spans reading think's grounding decider prices at PASS
29
+ * per byte, exposed here so a mechanism can turn it into a human label. */
30
+ export function unexplainedSpans(queryLen, accounted) {
31
+ const sorted = accounted
32
+ .map(([s, e]) => [Math.max(0, s), Math.min(queryLen, e)])
33
+ .filter(([s, e]) => e > s)
34
+ .sort((a, b) => a[0] - b[0]);
35
+ const gaps = [];
36
+ let reach = 0;
37
+ for (const [s, e] of sorted) {
38
+ if (s > reach)
39
+ gaps.push([reach, s]);
40
+ if (e > reach)
41
+ reach = e;
42
+ }
43
+ if (reach < queryLen)
44
+ gaps.push([reach, queryLen]);
45
+ return gaps;
46
+ }
47
+ /** A human-readable label for the query bytes a mechanism's `accounted`
48
+ * spans leave unexplained — purely diagnostic (Task 2's negative evidence):
49
+ * it never changes a candidate's weight, only what the rationale trace
50
+ * says the mechanism left on the table. `""` when nothing is unexplained. */
51
+ export function unexplainedLabel(query, accounted) {
52
+ const gaps = unexplainedSpans(query.length, accounted);
53
+ if (gaps.length === 0)
54
+ return "";
55
+ return gaps.map(([s, e]) => decodeText(query.subarray(s, e))).join(" … ");
56
+ }
57
+ /** The live tracer: a stack of open mechanisms over one {@link Mind.respond}.
58
+ *
59
+ * Sema's inference is single-threaded and strictly sequential — every async
60
+ * step is awaited before the next begins, and `respond` holds no two thoughts
61
+ * at once — so a plain stack exactly tracks the current nesting: {@link enter}
62
+ * pushes, {@link Scope.done} pops, and {@link step} (a mechanism with no
63
+ * sub-steps) is the two fused. The tracer never branches the control flow; it
64
+ * only records it. */
65
+ export class Rationale {
66
+ sink;
67
+ next = 0;
68
+ /** Open mechanisms, outermost first. Each frame remembers the last child it
69
+ * has spawned so the next sibling can default its data-flow edge to it. */
70
+ stack = [];
71
+ /** The most recent step index emitted under each mechanism name — the handle
72
+ * a later step uses to name an EARLIER mechanism as its data-flow producer
73
+ * (e.g. cover depends on the latest recognise / computeExtensions). One tracer is
74
+ * built per response and inference is sequential, so "most recent" is exactly
75
+ * "the one that produced the inputs I am about to consume". */
76
+ lastByName = new Map();
77
+ constructor(sink) {
78
+ this.sink = sink;
79
+ }
80
+ /** The index of the most recent step with this mechanism name, or undefined if
81
+ * none has run. Used to wire an explicit producer edge into {@link
82
+ * Scope.done} / {@link step}'s `deps`. */
83
+ lastIndex(name) {
84
+ return this.lastByName.get(name);
85
+ }
86
+ /** The mechanism names currently open, outermost → innermost. */
87
+ path(leaf) {
88
+ const p = this.stack.map((f) => f.name);
89
+ p.push(leaf);
90
+ return p;
91
+ }
92
+ /** The default data-flow edge for a step entering now: the previous sibling
93
+ * inside the current mechanism, else the enclosing mechanism, else nothing
94
+ * (the root). An explicit `deps` overrides this. */
95
+ defaultDeps() {
96
+ const top = this.stack[this.stack.length - 1];
97
+ if (!top)
98
+ return [];
99
+ return [top.lastChild ?? top.index];
100
+ }
101
+ /** Reserve this step's index and register it as the current mechanism's most
102
+ * recent child (so the NEXT sibling chains to it) and as the most recent step
103
+ * of its own NAME (so a later mechanism can name it as a producer). */
104
+ reserve(name) {
105
+ const index = this.next++;
106
+ const top = this.stack[this.stack.length - 1];
107
+ if (top)
108
+ top.lastChild = index;
109
+ this.lastByName.set(name, index);
110
+ return index;
111
+ }
112
+ emit(index, mechanism, inputs, outputs, deps, note) {
113
+ this.sink({
114
+ index,
115
+ mechanism,
116
+ parent: this.stack.length > 0
117
+ ? this.stack[this.stack.length - 1].index
118
+ : -1,
119
+ dependsOn: deps ?? this.defaultDeps(),
120
+ inputs,
121
+ outputs,
122
+ note,
123
+ });
124
+ }
125
+ /** Enter a mechanism that has sub-steps. Captures its inputs and the nesting
126
+ * now; the matching {@link Scope.done} supplies the outputs when it finishes.
127
+ * `deps` overrides the default data-flow edge (previous sibling / parent). */
128
+ enter(name, inputs, deps) {
129
+ const mechanism = this.path(name);
130
+ const resolvedDeps = deps ?? this.defaultDeps();
131
+ const index = this.reserve(name);
132
+ this.stack.push({ index, name, lastChild: null });
133
+ let closed = false;
134
+ const emit = this.emit.bind(this);
135
+ const pop = () => {
136
+ // Pop down to and including this frame — tolerant of a sub-mechanism that
137
+ // forgot to close, so one missed `done` cannot desync the whole stack.
138
+ const at = this.stack.findIndex((f) => f.index === index);
139
+ if (at >= 0)
140
+ this.stack.length = at;
141
+ };
142
+ return {
143
+ index,
144
+ done: (outputs, note) => {
145
+ if (closed)
146
+ return;
147
+ closed = true;
148
+ pop();
149
+ emit(index, mechanism, inputs, outputs, resolvedDeps, note);
150
+ },
151
+ };
152
+ }
153
+ /** Record a mechanism that has no sub-steps — its inputs and outputs are both
154
+ * known at the call site. Returns its index, for a later step to depend on. */
155
+ step(name, inputs, outputs, note, deps) {
156
+ const mechanism = this.path(name);
157
+ const resolvedDeps = deps ?? this.defaultDeps();
158
+ const index = this.reserve(name);
159
+ this.emit(index, mechanism, inputs, outputs, resolvedDeps, note);
160
+ return index;
161
+ }
162
+ }
@@ -0,0 +1,15 @@
1
+ import type { MindContext } from "./types.js";
2
+ import type { Precomputed } from "./pipeline-mechanism.js";
3
+ /** Extend a grounded answer forward across facts (multi-hop reasoning).
4
+ * Pivots on the longest unconsumed learnt context each answer contains,
5
+ * then follows the pivot's continuation to the next fact. Repeats up
6
+ * to `cfg.recallQueryK` hops. `preConsumed` carries node ids already
7
+ * spoken for by the grounding stage (cover/extract/CAST). `pre` is the
8
+ * response's shared pre-computation — the post-grounding stages read the
9
+ * same container the mechanisms did. */
10
+ export declare function reason(ctx: MindContext, query: Uint8Array, answer: Uint8Array, preConsumed: ReadonlySet<number>, pre: Precomputed): Promise<Uint8Array>;
11
+ /** Fuse independent points of attention into one answer (multi-topic).
12
+ * When the consensus climb finds more than one dominant point, each
13
+ * independent point grounds its own answer; they are bridged together
14
+ * by any learnt connector the graph holds between them. */
15
+ export declare function fuseAttention(ctx: MindContext, query: Uint8Array, primary: Uint8Array, pre: Precomputed): Promise<Uint8Array>;