@hviana/sema 0.1.0 → 0.1.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 +2216 -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 +212 -0
  26. package/dist/src/alu/src/parser.js +469 -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/config.d.ts +114 -0
  38. package/dist/src/config.js +96 -0
  39. package/dist/src/derive/src/deduction.d.ts +125 -0
  40. package/dist/src/derive/src/deduction.js +155 -0
  41. package/dist/src/derive/src/index.d.ts +7 -0
  42. package/dist/src/derive/src/index.js +11 -0
  43. package/dist/src/derive/src/priority-queue.d.ts +20 -0
  44. package/dist/src/derive/src/priority-queue.js +73 -0
  45. package/dist/src/derive/src/rewrite.d.ts +56 -0
  46. package/dist/src/derive/src/rewrite.js +100 -0
  47. package/dist/src/derive/src/trie.d.ts +90 -0
  48. package/dist/src/derive/src/trie.js +217 -0
  49. package/dist/src/derive/test/derive.test.d.ts +1 -0
  50. package/dist/src/derive/test/derive.test.js +122 -0
  51. package/dist/src/extension.d.ts +37 -0
  52. package/dist/src/extension.js +7 -0
  53. package/dist/src/geometry.d.ts +137 -0
  54. package/dist/src/geometry.js +430 -0
  55. package/dist/src/index.d.ts +15 -0
  56. package/dist/src/index.js +21 -0
  57. package/dist/src/ingest-cache.d.ts +41 -0
  58. package/dist/src/ingest-cache.js +161 -0
  59. package/dist/src/mind/articulation.d.ts +6 -0
  60. package/dist/src/mind/articulation.js +99 -0
  61. package/dist/src/mind/attention.d.ts +72 -0
  62. package/dist/src/mind/attention.js +894 -0
  63. package/dist/src/mind/canonical.d.ts +29 -0
  64. package/dist/src/mind/canonical.js +88 -0
  65. package/dist/src/mind/graph-search.d.ts +270 -0
  66. package/dist/src/mind/graph-search.js +847 -0
  67. package/dist/src/mind/index.d.ts +5 -0
  68. package/dist/src/mind/index.js +5 -0
  69. package/dist/src/mind/junction.d.ts +95 -0
  70. package/dist/src/mind/junction.js +262 -0
  71. package/dist/src/mind/learning.d.ts +47 -0
  72. package/dist/src/mind/learning.js +201 -0
  73. package/dist/src/mind/match.d.ts +111 -0
  74. package/dist/src/mind/match.js +422 -0
  75. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  76. package/dist/src/mind/mechanisms/alu.js +29 -0
  77. package/dist/src/mind/mechanisms/cast.d.ts +35 -0
  78. package/dist/src/mind/mechanisms/cast.js +447 -0
  79. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  80. package/dist/src/mind/mechanisms/confluence.js +213 -0
  81. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  82. package/dist/src/mind/mechanisms/cover.js +179 -0
  83. package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
  84. package/dist/src/mind/mechanisms/extraction.js +342 -0
  85. package/dist/src/mind/mechanisms/recall.d.ts +13 -0
  86. package/dist/src/mind/mechanisms/recall.js +151 -0
  87. package/dist/src/mind/mind.d.ts +147 -0
  88. package/dist/src/mind/mind.js +300 -0
  89. package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
  90. package/dist/src/mind/pipeline-mechanism.js +213 -0
  91. package/dist/src/mind/pipeline.d.ts +20 -0
  92. package/dist/src/mind/pipeline.js +185 -0
  93. package/dist/src/mind/primitives.d.ts +43 -0
  94. package/dist/src/mind/primitives.js +162 -0
  95. package/dist/src/mind/rationale.d.ts +134 -0
  96. package/dist/src/mind/rationale.js +162 -0
  97. package/dist/src/mind/reasoning.d.ts +15 -0
  98. package/dist/src/mind/reasoning.js +162 -0
  99. package/dist/src/mind/recognition.d.ts +20 -0
  100. package/dist/src/mind/recognition.js +223 -0
  101. package/dist/src/mind/resonance.d.ts +23 -0
  102. package/dist/src/mind/resonance.js +0 -0
  103. package/dist/src/mind/trace.d.ts +15 -0
  104. package/dist/src/mind/trace.js +73 -0
  105. package/dist/src/mind/traverse.d.ts +100 -0
  106. package/dist/src/mind/traverse.js +447 -0
  107. package/dist/src/mind/types.d.ts +174 -0
  108. package/dist/src/mind/types.js +84 -0
  109. package/dist/src/rabitq-hnsw/src/database.d.ts +200 -0
  110. package/dist/src/rabitq-hnsw/src/database.js +388 -0
  111. package/dist/src/rabitq-hnsw/src/heap.d.ts +22 -0
  112. package/dist/src/rabitq-hnsw/src/heap.js +89 -0
  113. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +125 -0
  114. package/dist/src/rabitq-hnsw/src/hnsw.js +474 -0
  115. package/dist/src/rabitq-hnsw/src/index.d.ts +10 -0
  116. package/dist/src/rabitq-hnsw/src/index.js +6 -0
  117. package/dist/src/rabitq-hnsw/src/prng.d.ts +19 -0
  118. package/dist/src/rabitq-hnsw/src/prng.js +36 -0
  119. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +95 -0
  120. package/dist/src/rabitq-hnsw/src/rabitq.js +283 -0
  121. package/dist/src/rabitq-hnsw/src/store.d.ts +162 -0
  122. package/dist/src/rabitq-hnsw/src/store.js +825 -0
  123. package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +1 -0
  124. package/dist/src/rabitq-hnsw/test/hnsw.test.js +948 -0
  125. package/dist/src/store-sqlite.d.ts +149 -0
  126. package/dist/src/store-sqlite.js +702 -0
  127. package/dist/src/store.d.ts +638 -0
  128. package/dist/src/store.js +1618 -0
  129. package/dist/src/vec.d.ts +31 -0
  130. package/dist/src/vec.js +109 -0
  131. package/package.json +1 -1
@@ -0,0 +1,300 @@
1
+ // mind/mind.ts — perceive, deposit, recall, think, express.
2
+ //
3
+ // Memory is a content-addressed node graph (see store.ts). Learning is
4
+ // DEPOSITION: perceive a stream into a tree and intern every node, so equal —
5
+ // and, by resonance, similar — subtrees collapse to one shared node. A fact is
6
+ // an EDGE between node ids; recall traverses edges; thinking completes the
7
+ // query's OWN tree, node by node, to a fixed point. No whole, no weights.
8
+ //
9
+ // Architecture: 4 primitives × 2 patterns = all inference.
10
+ // Implementation split across src/mind/*.ts — this file assembles the Mind class.
11
+ import { makeKeyring, rng, setVecConfig } from "../vec.js";
12
+ import { Alphabet } from "../alphabet.js";
13
+ import { reachThreshold, } from "../geometry.js";
14
+ import { BoundedMap } from "../store.js";
15
+ import { SQliteStore } from "../store-sqlite.js";
16
+ import { resolveConfig } from "../config.js";
17
+ import { bytesEqual } from "../bytes.js";
18
+ import { GraphSearch, } from "./graph-search.js";
19
+ import { Alu } from "../alu/src/index.js";
20
+ import { decodeText, Rationale, } from "./rationale.js";
21
+ import { gistOf, inputBytes, perceive as perceiveImpl, resolve as resolveImpl, } from "./primitives.js";
22
+ import { chooseNext, edgeAncestors as edgeAncestorsFn } from "./traverse.js";
23
+ import { follow } from "./match.js";
24
+ import { recognise, segment } from "./recognition.js";
25
+ import { meaningOf } from "./resonance.js";
26
+ import { climbAttention as climbAttentionFn, naturalBreak as naturalBreakFn, } from "./attention.js";
27
+ import { aluToMechanism, defaultMechanisms, think } from "./pipeline.js";
28
+ import { articulate } from "./articulation.js";
29
+ import { ingest } from "./learning.js";
30
+ import { rItem } from "./trace.js";
31
+ // ═══════════════════════════════════════════════════════════════════════════
32
+ // THE MIND
33
+ // ═══════════════════════════════════════════════════════════════════════════
34
+ export class Mind {
35
+ space;
36
+ alphabet;
37
+ store;
38
+ cfg;
39
+ /** The lightest-derivation engine over the Sema graph. */
40
+ search;
41
+ /** The grounding mechanisms iterated by {@link think}. */
42
+ mechanisms = [];
43
+ /** The live rationale tracer for the inference currently in flight, or null. */
44
+ trace = null;
45
+ /** Per-response memo of the consensus climb. NOTE: this memo and
46
+ * {@link recogniseMemo} are BYPASSED while a rationale trace is attached
47
+ * (every mechanism must emit its own steps), so a traced respond re-pays
48
+ * up to four consensus climbs plus repeat recognitions — that is where the
49
+ * traced-vs-untraced latency multiple comes from, by design. */
50
+ climbMemo = null;
51
+ /** Per-response memo of recognise() — see {@link MindContext.recogniseMemo}. */
52
+ recogniseMemo = null;
53
+ /** Per-response memo of perceive() — see {@link MindContext.perceiveMemo}. */
54
+ perceiveMemo = null;
55
+ /** The perceived gist of the query currently being answered. Set by `think`
56
+ * before the graph search runs; `chooseNext` consults it as a gate (a null
57
+ * guide means no query is in flight, so structural walkers keep plain
58
+ * first-edge behaviour) and the reverse projection uses it for
59
+ * reverse-recall disambiguation via `chooseAmong`. */
60
+ _edgeGuide = null;
61
+ /** Per-response memo of {@link chooseNext} picks — ensures every mechanism
62
+ * of a single response follows the SAME continuation for each ambiguous
63
+ * context node. */
64
+ _edgeChoice = new Map();
65
+ /** Previous deposit's seen node ids for incremental change detection. */
66
+ _prevSeen = null;
67
+ /** Session cache of node-id → perceived gist for candidate scoring — see
68
+ * {@link MindContext._gistCache}. 32 MB ≈ 8K gists at D=1024; hub
69
+ * candidate sets (√N at most) fit comfortably and recur across queries. */
70
+ _gistCache = new BoundedMap(32_000_000, (v) => v.byteLength);
71
+ // Deposit-path fold-pyramid cache (see MindContext) — ENTRY-count
72
+ // bounded: a pyramid costs ~KB per content byte (one D-float gist per
73
+ // interior node), and only the few live conversation chains need to stay
74
+ // warm, so 8 entries is the honest budget.
75
+ _depositTrees = new BoundedMap(8);
76
+ _depositLens = new Set();
77
+ _internIds = new WeakMap();
78
+ // ── GraphSearchHost implementation ─────────────────────────────────────
79
+ /** Canonical node id of a byte span. Required by GraphSearchHost & MindContext. */
80
+ resolve(bytes) {
81
+ return resolveImpl(this, bytes);
82
+ }
83
+ // recogniseSpan wraps recognise
84
+ recogniseSpan(bytes) {
85
+ const r = recognise(this, bytes);
86
+ return { sites: r.sites, leaves: r.leaves, splits: r.splits };
87
+ }
88
+ /** Disambiguate among multiple learnt continuations of the same context node.
89
+ * Required by {@link GraphSearchHost} — the graph search calls this through the
90
+ * host interface when a recognised form has more than one outgoing edge.
91
+ * Delegates to the standalone {@link chooseNext} which picks the candidate
92
+ * with the most distributional evidence (highest `prevOf` count — the
93
+ * structural manifestation of its halo). When evidence is equal the
94
+ * first-inserted edge wins. */
95
+ chooseNext(node) {
96
+ return chooseNext(this, node, this._edgeGuide);
97
+ }
98
+ constructor(optsOrCfg, storeArg, _fromStore) {
99
+ let userMechanisms = [];
100
+ let userFactories = [];
101
+ if (_fromStore !== undefined) {
102
+ this.cfg = resolveConfig(optsOrCfg);
103
+ this.store = storeArg;
104
+ }
105
+ else {
106
+ const { store: optsStore, mechanisms: userMechs, mechanismFactories: userFacts, ...rest } = (optsOrCfg ?? {});
107
+ this.cfg = resolveConfig(rest);
108
+ this.store = optsStore ?? new SQliteStore({
109
+ maxGroup: this.cfg.geometry.maxGroup,
110
+ });
111
+ userMechanisms = userMechs ?? [];
112
+ userFactories = userFacts ?? [];
113
+ }
114
+ setVecConfig({
115
+ normalizeEpsilon: this.cfg.normalizeEpsilon,
116
+ cosineEpsilon: this.cfg.cosineEpsilon,
117
+ });
118
+ const seedRand = rng((this.cfg.seed ^ 0x9e3779) >>> 0);
119
+ const seats = makeKeyring(this.store.D, Math.max(8, this.cfg.geometry.maxGroup), seedRand);
120
+ this.space = {
121
+ D: this.store.D,
122
+ seats,
123
+ rand: rng((this.cfg.seed ^ 0x51f15e) >>> 0),
124
+ maxGroup: this.cfg.geometry.maxGroup,
125
+ };
126
+ this.alphabet = new Alphabet(this.cfg.seed, this.store.D, this.cfg.alphabet);
127
+ this.search = new GraphSearch(this.store, this.space.maxGroup, this);
128
+ // Build the mechanism list: default grounding + ALU + user mechanisms.
129
+ for (const m of defaultMechanisms)
130
+ this.mechanisms.push(m);
131
+ const host = this.extensionHost();
132
+ if (this.cfg.alu.enabled) {
133
+ const alu = new Alu({
134
+ tol: this.cfg.alu.tol,
135
+ maxIter: this.cfg.alu.maxIter,
136
+ precision: this.cfg.alu.precision,
137
+ }, host);
138
+ this.mechanisms.push(aluToMechanism(alu));
139
+ }
140
+ for (const m of userMechanisms)
141
+ this.mechanisms.push(m);
142
+ for (const f of userFactories)
143
+ this.mechanisms.push(f(host));
144
+ }
145
+ // ── Public API ───────────────────────────────────────────────────────────
146
+ /** Exposed for tests: the consensus climb over query sub-regions. */
147
+ climbAttention(query, k, mode = "inverse") {
148
+ return climbAttentionFn(this, query, k, mode);
149
+ }
150
+ /** Exposed for tests: climb the structural DAG from a node to its
151
+ * edge-bearing ancestor contexts. */
152
+ edgeAncestors(id, contextCount) {
153
+ return edgeAncestorsFn(this, id, contextCount);
154
+ }
155
+ /** Exposed for tests: find the natural break point in a sorted vote list. */
156
+ naturalBreak(votes) {
157
+ return naturalBreakFn(votes);
158
+ }
159
+ // ── respond ───────────────────────────────────────────────────────────
160
+ /** Perceive input into a content-defined tree. Deterministic — identical
161
+ * bytes always produce an identical tree. Public for ingest-cache. */
162
+ perceive(input, leafAt, lookup) {
163
+ return perceiveImpl(this, input, leafAt, lookup);
164
+ }
165
+ /** Open one response's transient state — the tracer and the per-response
166
+ * memos. Paired with {@link endResponse}; the ONE place this state is
167
+ * created, so adding a memo cannot forget its reset. */
168
+ beginResponse(inspectRationale) {
169
+ this.trace = inspectRationale ? new Rationale(inspectRationale) : null;
170
+ this.climbMemo = new WeakMap();
171
+ this.recogniseMemo = new WeakMap();
172
+ this.perceiveMemo = new Map();
173
+ }
174
+ /** Close one response's transient state — every per-response field, incl.
175
+ * the edge guide/choices `think` sets mid-flight. */
176
+ endResponse() {
177
+ this.trace = null;
178
+ this.climbMemo = null;
179
+ this.recogniseMemo = null;
180
+ this.perceiveMemo = null;
181
+ this._edgeGuide = null;
182
+ this._edgeChoice.clear();
183
+ }
184
+ async respond(input, inspectRationale) {
185
+ this.beginResponse(inspectRationale);
186
+ try {
187
+ const inBytes = inputBytes(this, input);
188
+ const top = this.trace?.enter("respond", [
189
+ rItem(inBytes, "query"),
190
+ ]);
191
+ const thought = await think(this, inBytes, this.mechanisms);
192
+ if (thought === null) {
193
+ top?.done([], "nothing to perceive or an empty store — no answer");
194
+ return { v: null, bytes: new Uint8Array(0) };
195
+ }
196
+ const voiced = await articulate(this, thought.bytes, inBytes);
197
+ top?.done([rItem(voiced, "answer", resolveImpl(this, voiced) ?? undefined)], "the answer, re-voiced in the asker's words");
198
+ return {
199
+ v: gistOf(this, voiced),
200
+ bytes: voiced,
201
+ provenance: thought.provenance,
202
+ };
203
+ }
204
+ finally {
205
+ this.endResponse();
206
+ }
207
+ }
208
+ /** Text view of {@link respond}. NUL bytes (0x00) are stripped before
209
+ * decoding — they are structural padding in text answers. LOSSY for a
210
+ * binary answer that legitimately contains NULs: use {@link respond} and
211
+ * read `bytes` directly for binary/grid modalities. */
212
+ async respondText(input, inspectRationale) {
213
+ const r = await this.respond(input, inspectRationale);
214
+ return decodeText(r.bytes);
215
+ }
216
+ async embedding(input) {
217
+ return (await this.respond(input)).v;
218
+ }
219
+ /** Kinship note: the vector arm below is a miniature of recall's tier 3
220
+ * (resonate → reach gate → read out the nearest form's bytes) — the
221
+ * read-out direction of the same operation, without recall's grounding
222
+ * ladder. If either side's acceptance rule changes, revisit the other. */
223
+ async express(idOrV) {
224
+ if (typeof idOrV === "number")
225
+ return this.store.bytes(idOrV);
226
+ const [hit] = await this.store.resonate(idOrV, 1);
227
+ // The same confidence floor recall uses: a vector whose nearest stored
228
+ // form sits below the reach threshold relates to NOTHING in the store —
229
+ // returning that form's bytes anyway would fabricate an answer from an
230
+ // unrelated neighbour. Silence is the honest read-out.
231
+ if (hit && hit.score >= reachThreshold(this.space.maxGroup)) {
232
+ return this.store.bytes(hit.id);
233
+ }
234
+ return new Uint8Array(0);
235
+ }
236
+ // ── Learning ─────────────────────────────────────────────────────────────
237
+ async ingest(input, second) {
238
+ return ingest(this, input, second);
239
+ }
240
+ // ── Extension Surface ────────────────────────────────────────────────────
241
+ extensionHost() {
242
+ const mind = this;
243
+ return {
244
+ meaningOf: (bytes, anchors) => meaningOf(this, bytes, anchors),
245
+ continuation: (bytes) => this.groundedContinuation(bytes),
246
+ segment: (bytes) => segment(this, bytes).map((s) => ({ i: s.start, j: s.end })),
247
+ get reach() {
248
+ return mind.space.maxGroup;
249
+ },
250
+ };
251
+ }
252
+ async groundedContinuation(bytes) {
253
+ const id = resolveImpl(this, bytes);
254
+ if (id === null)
255
+ return null;
256
+ const grounded = await follow(this, id);
257
+ if (grounded !== null && !bytesEqual(grounded, bytes))
258
+ return grounded;
259
+ return null;
260
+ }
261
+ // ── Content-index repair ───────────────────────────────────────────────
262
+ /** Re-index structurally-important nodes whose gists were evicted from the
263
+ * pending cache before they reached the content index. See {@link
264
+ * Store.repairContentIndex} for the contract; this method wires the
265
+ * Mind's perception into the store's repair walk.
266
+ *
267
+ * Run this after training or at checkpoints to restore recall reach for
268
+ * nodes that bridge experiences but were never indexed. A pure interior
269
+ * node (no edges, no halo) is deliberately skipped — it is scaffolding,
270
+ * not an experience root or bridge, and regenerating its gist would waste
271
+ * I/O and index space for no recall benefit.
272
+ *
273
+ * @param minParents only repair nodes with ≥ this many structural parents
274
+ * (default 2 — structural bridges)
275
+ * @returns number of nodes added to the content index */
276
+ async repairContentIndex(minParents = 2) {
277
+ return this.store.repairContentIndex(async (id) => {
278
+ const bytes = this.store.bytes(id);
279
+ if (bytes.length === 0)
280
+ return null;
281
+ return gistOf(this, bytes);
282
+ }, minParents);
283
+ }
284
+ // ── Persistence ──────────────────────────────────────────────────────────
285
+ async save() {
286
+ const meta = new TextEncoder().encode(JSON.stringify(this.cfg));
287
+ await this.store.saveSnapshot(meta);
288
+ return meta;
289
+ }
290
+ static async load(snapshot, store) {
291
+ const cfg = JSON.parse(new TextDecoder().decode(snapshot));
292
+ return new Mind(cfg, store, true);
293
+ }
294
+ static async loadFromStore(store) {
295
+ const meta = await store.loadSnapshot();
296
+ if (!meta)
297
+ throw new Error("no snapshot in store");
298
+ return Mind.load(meta, store);
299
+ }
300
+ }
@@ -0,0 +1,142 @@
1
+ import type { AncestorReach, MindContext, Recognition } from "./types.js";
2
+ import type { AttentionRead } from "./types.js";
3
+ import type { ComputedSpan } from "../extension.js";
4
+ import type { Vec } from "../vec.js";
5
+ import { type GradedRun } from "./match.js";
6
+ export declare class Precomputed {
7
+ readonly ctx: MindContext;
8
+ readonly query: Uint8Array;
9
+ /** Recognition result (structural + canonical). */
10
+ readonly rec: Recognition;
11
+ /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
12
+ readonly computed: ComputedSpan[];
13
+ /** The query's gist — the response-wide disambiguation guide. */
14
+ readonly guide: Vec;
15
+ /** The response's evidence-breadth constant: how many ranked candidates the
16
+ * resonance probes, the weave alignment, and the climb all consider.
17
+ * Derived once from config; every consumer reads it here. */
18
+ readonly k: number;
19
+ constructor(ctx: MindContext, query: Uint8Array,
20
+ /** Recognition result (structural + canonical). */
21
+ rec: Recognition,
22
+ /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
23
+ computed: ComputedSpan[],
24
+ /** The query's gist — the response-wide disambiguation guide. */
25
+ guide: Vec);
26
+ private _windows?;
27
+ /** Content-addressed W-window identities for every position in the query
28
+ * (offset → node id). O(|query|) probes. */
29
+ get queryWindows(): Map<number, number>;
30
+ private _resolved?;
31
+ /** The node id of the query itself, or null when it is not a stored form.
32
+ * O(|query|) probes. */
33
+ get queryResolved(): number | null;
34
+ private _anchorWindows;
35
+ /** Content-addressed W-window identities of one anchor's own bytes
36
+ * (offset → node id), memoised per anchor. Confluence intersects these;
37
+ * any future identity-based mechanism reads the same cache. */
38
+ windowsOf(anchor: number): Map<number, number>;
39
+ /** Shared memo for {@link reachOf} (structural-IDF reads): a window's
40
+ * ancestor reach is a pure function of the read-only store, so one
41
+ * response-scoped memo serves every mechanism that prices commonality. */
42
+ readonly reachMemo: Map<number, AncestorReach>;
43
+ private _attention?;
44
+ /** The full consensus climb (roots + ranked anchors) — the query-level
45
+ * evidence CAST, confluence, extraction, recall's scaffolding tier, and
46
+ * fusion all share. Computed on first access; a query no mechanism
47
+ * climbs for (e.g. one an extension decided outright) never pays for it. */
48
+ attention(): Promise<AttentionRead>;
49
+ private _weave?;
50
+ /** Result of {@link alignGraded} for the first k ranked anchors —
51
+ * O(k · |query| · |ctx|). Consumed by CAST; reusable by any future
52
+ * mechanism doing analogical transfer. */
53
+ weave(): Promise<WeaveInfo>;
54
+ /** Span-shaped classification of one ranked anchor, memoised per anchor id
55
+ * so repeated calls (extraction's own early-exit scan, any future
56
+ * template-based mechanism) never redo the work. Deliberately NOT an
57
+ * eager all-anchors map: `skillExemplar` is the expensive part of
58
+ * extraction (capped fan-out reads plus an O(|ctx|) scan), and most
59
+ * queries are answered by the FIRST ranked anchor that qualifies — paying
60
+ * for every ranked anchor regardless of where the scan stops would turn
61
+ * an early-exit lookup into full O(k) work on every query. */
62
+ private _spanShaped;
63
+ spanShapedOf(anchor: number): Promise<SkillInfo | null>;
64
+ /** Every ranked anchor's classification at once, sharing the same
65
+ * per-anchor cache as {@link spanShapedOf} — for a mechanism that
66
+ * genuinely needs the full picture (not an early-exit scan). Mixing
67
+ * access patterns across mechanisms never duplicates work: whichever
68
+ * anchors an early-exit consumer already asked for are reused here, and
69
+ * whichever this computes first are reused by a later early-exit scan. */
70
+ spanShapedAll(): Promise<Map<number, SkillInfo | null>>;
71
+ }
72
+ /** The weave-local structural alignment, computed once and consumed by CAST
73
+ * (and any future mechanism doing analogical transfer). */
74
+ export interface WeaveInfo {
75
+ /** Per-anchor alignment: context bytes, vote weight, and graded runs. */
76
+ points: Array<{
77
+ anchor: number;
78
+ vote: number;
79
+ ctx: Uint8Array;
80
+ runs: GradedRun[];
81
+ }>;
82
+ /** Weighted depth at each query byte — sum of alignment weights.
83
+ * `depth[i]` is the total evidence that byte i is shared among the
84
+ * aligned structures. */
85
+ depth: Float64Array;
86
+ }
87
+ /** Span-shaped classification of one anchor — the structural information
88
+ * extraction uses to decide whether a learned fact can serve as a template
89
+ * for reading an analogous span out of the query. */
90
+ export interface SkillInfo {
91
+ contextBytes: Uint8Array;
92
+ answerBytes: Uint8Array;
93
+ }
94
+ /** Raw result from a mechanism's `run()`. The pipeline computes the weight
95
+ * from `moves` + `PASS * unaccounted(accounted)` — the mechanism does not
96
+ * know about the cost ladder.
97
+ *
98
+ * When `weight` is present, the pipeline uses it directly instead of
99
+ * computing `weigh(accounted, moves)`. This is for mechanisms whose cost
100
+ * is derived externally (e.g. cover: the A*LD derivation's g-value). */
101
+ export interface MechanismResult {
102
+ bytes: Uint8Array;
103
+ accounted: Array<[number, number]>;
104
+ moves: number;
105
+ used?: ReadonlySet<number>;
106
+ unexplained: string;
107
+ /** Explicit weight override. When absent, weight = moves + PASS·unaccounted. */
108
+ weight?: number;
109
+ /** Override the mechanism's default provenance for this result.
110
+ * When absent, the pipeline uses `mech.provenance`. */
111
+ provenance?: string;
112
+ }
113
+ export interface PipelineMechanism {
114
+ /** Stable identifier for trace/debug. */
115
+ readonly name: string;
116
+ /** Which provenance tag the pipeline attaches to this mechanism's answers. */
117
+ readonly provenance: string;
118
+ /** Parse authoritative spans BEFORE the grounding loop.
119
+ * Only needed by computational mechanisms (e.g. ALU). Results from ALL
120
+ * mechanisms that implement this are collected into `Precomputed.computed`
121
+ * before any `floor()` or `run()` is called. */
122
+ parse?(query: Uint8Array): Promise<ComputedSpan[]>;
123
+ /** Admissible lower bound on this mechanism's weight.
124
+ * Returns `null` when the mechanism structurally cannot fire.
125
+ *
126
+ * `worthRunning(cheapFloor)` reports whether the CURRENT incumbent
127
+ * (established by mechanisms that already ran this response, cover being
128
+ * first — see `defaultMechanisms`) could still be beaten by a floor no
129
+ * tighter than `cheapFloor`. THE INVESTMENT DISCIPLINE: before
130
+ * first-touching an expensive shared analysis (`pre.attention()`,
131
+ * `pre.weave()`, …), check `worthRunning(bound)` with this mechanism's
132
+ * cheapest possible bound — and when it fails, RETURN THE BOUND rather
133
+ * than null. The bound is still admissible (it never overstates cost),
134
+ * the pipeline's own check then prunes `run()` and records the truthful
135
+ * "cannot beat incumbent" trace note, and no analysis was computed just
136
+ * to be discarded. This is uniform: no mechanism asks what produced the
137
+ * incumbent — a computed extension result and an ordinary cheap cover
138
+ * prune the same way. */
139
+ floor(ctx: MindContext, query: Uint8Array, pre: Precomputed, worthRunning: (floor: number) => boolean): Promise<number | null>;
140
+ /** Produce candidate answers. */
141
+ run(ctx: MindContext, query: Uint8Array, pre: Precomputed): Promise<MechanismResult[]>;
142
+ }
@@ -0,0 +1,213 @@
1
+ // pipeline-mechanism.ts — the uniform grounding-mechanism interface.
2
+ //
3
+ // Every grounding mechanism (CAST, confluence, cover, extraction, recall, ALU,
4
+ // user extensions) implements this ONE interface. The pipeline (think()) sees
5
+ // a list of PipelineMechanism objects — it never imports a mechanism-specific
6
+ // type and never has a special-case branch for any mechanism.
7
+ //
8
+ // The four constraints of the free-will architecture (§14.5):
9
+ // 1. DECOUPLING — mechanisms import nothing from each other or from pipeline.
10
+ // 2. DECLARED COMPETENCE — floor() returns null when impossible, a number when
11
+ // possible. Binary, auditable, no learned scores.
12
+ // 3. VISIBLE BUDGET — every mechanism carries its own caps internally (√N, k).
13
+ // 4. TRAVELING EVIDENCE — run() returns MechanismResult with accounted, moves,
14
+ // and unexplained. The pipeline computes the weight.
15
+ import { windowIds } from "./canonical.js";
16
+ import { read, resolve } from "./primitives.js";
17
+ import { alignGraded } from "./match.js";
18
+ import { climbAttentionAll } from "./attention.js";
19
+ import { skillExemplar } from "./mechanisms/extraction.js";
20
+ // ── Precomputed ──────────────────────────────────────────────────────────────
21
+ //
22
+ // Precomputed is a LAZY container for structural analyses of the query — the
23
+ // ONE place a response's shared evidence lives, for inter-mechanism exchange
24
+ // and for analyses future mechanisms will want. Eager fields (rec, computed,
25
+ // guide) are populated by the pipeline before the mechanism loop; everything
26
+ // expensive is a lazily-cached method that computes on first access. A
27
+ // mechanism that never asks for an analysis pays nothing for it; two
28
+ // mechanisms asking for the same analysis pay once.
29
+ //
30
+ // This design serves THREE purposes:
31
+ // 1. SHARING — when two mechanisms need the same analysis, it's computed once
32
+ // (even under trace, where the ctx-level memos are deliberately bypassed).
33
+ // 2. EXTENSIBILITY — a new analysis is one method in one file.
34
+ // 3. DECLARATIVE COST — a mechanism's floor() checks its cheap gates and the
35
+ // pipeline's `worthRunning` predicate BEFORE first-touching an expensive
36
+ // analysis, so lazy analyses are only ever computed for a mechanism that
37
+ // could still win.
38
+ export class Precomputed {
39
+ ctx;
40
+ query;
41
+ rec;
42
+ computed;
43
+ guide;
44
+ /** The response's evidence-breadth constant: how many ranked candidates the
45
+ * resonance probes, the weave alignment, and the climb all consider.
46
+ * Derived once from config; every consumer reads it here. */
47
+ k;
48
+ constructor(ctx, query,
49
+ /** Recognition result (structural + canonical). */
50
+ rec,
51
+ /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
52
+ computed,
53
+ /** The query's gist — the response-wide disambiguation guide. */
54
+ guide) {
55
+ this.ctx = ctx;
56
+ this.query = query;
57
+ this.rec = rec;
58
+ this.computed = computed;
59
+ this.guide = guide;
60
+ this.k = ctx.cfg.recallQueryK * 2;
61
+ }
62
+ // ── Cheap lazy analyses ───────────────────────────────────────────────
63
+ _windows;
64
+ /** Content-addressed W-window identities for every position in the query
65
+ * (offset → node id). O(|query|) probes. */
66
+ get queryWindows() {
67
+ return this._windows ??= windowIds(this.ctx, this.query);
68
+ }
69
+ _resolved;
70
+ /** The node id of the query itself, or null when it is not a stored form.
71
+ * O(|query|) probes. */
72
+ get queryResolved() {
73
+ if (this._resolved === undefined) {
74
+ this._resolved = resolve(this.ctx, this.query);
75
+ }
76
+ return this._resolved;
77
+ }
78
+ _anchorWindows = new Map();
79
+ /** Content-addressed W-window identities of one anchor's own bytes
80
+ * (offset → node id), memoised per anchor. Confluence intersects these;
81
+ * any future identity-based mechanism reads the same cache. */
82
+ windowsOf(anchor) {
83
+ let w = this._anchorWindows.get(anchor);
84
+ if (w === undefined) {
85
+ w = windowIds(this.ctx, read(this.ctx, anchor));
86
+ this._anchorWindows.set(anchor, w);
87
+ }
88
+ return w;
89
+ }
90
+ /** Shared memo for {@link reachOf} (structural-IDF reads): a window's
91
+ * ancestor reach is a pure function of the read-only store, so one
92
+ * response-scoped memo serves every mechanism that prices commonality. */
93
+ reachMemo = new Map();
94
+ // ── Expensive lazy analyses ───────────────────────────────────────────
95
+ //
96
+ // Async, cached-by-promise: the first caller starts the computation, every
97
+ // later caller (any mechanism, any phase) awaits the same promise. A
98
+ // mechanism MUST check its cheap floor gates and the pipeline's
99
+ // `worthRunning` predicate before first-touching one of these.
100
+ _attention;
101
+ /** The full consensus climb (roots + ranked anchors) — the query-level
102
+ * evidence CAST, confluence, extraction, recall's scaffolding tier, and
103
+ * fusion all share. Computed on first access; a query no mechanism
104
+ * climbs for (e.g. one an extension decided outright) never pays for it. */
105
+ attention() {
106
+ return this._attention ??= climbAttentionAll(this.ctx, this.query, this.k);
107
+ }
108
+ _weave;
109
+ /** Result of {@link alignGraded} for the first k ranked anchors —
110
+ * O(k · |query| · |ctx|). Consumed by CAST; reusable by any future
111
+ * mechanism doing analogical transfer. */
112
+ weave() {
113
+ return this._weave ??= this.attention().then((climb) => computeWeave(this.ctx, this.query, this, climb));
114
+ }
115
+ /** Span-shaped classification of one ranked anchor, memoised per anchor id
116
+ * so repeated calls (extraction's own early-exit scan, any future
117
+ * template-based mechanism) never redo the work. Deliberately NOT an
118
+ * eager all-anchors map: `skillExemplar` is the expensive part of
119
+ * extraction (capped fan-out reads plus an O(|ctx|) scan), and most
120
+ * queries are answered by the FIRST ranked anchor that qualifies — paying
121
+ * for every ranked anchor regardless of where the scan stops would turn
122
+ * an early-exit lookup into full O(k) work on every query. */
123
+ _spanShaped = new Map();
124
+ spanShapedOf(anchor) {
125
+ let p = this._spanShaped.get(anchor);
126
+ if (p === undefined) {
127
+ p = skillExemplar(this.ctx, anchor, this.guide);
128
+ this._spanShaped.set(anchor, p);
129
+ }
130
+ return p;
131
+ }
132
+ /** Every ranked anchor's classification at once, sharing the same
133
+ * per-anchor cache as {@link spanShapedOf} — for a mechanism that
134
+ * genuinely needs the full picture (not an early-exit scan). Mixing
135
+ * access patterns across mechanisms never duplicates work: whichever
136
+ * anchors an early-exit consumer already asked for are reused here, and
137
+ * whichever this computes first are reused by a later early-exit scan. */
138
+ async spanShapedAll() {
139
+ const { ranked } = await this.attention();
140
+ const out = new Map();
141
+ for (const cand of ranked) {
142
+ if (out.has(cand.anchor))
143
+ continue;
144
+ out.set(cand.anchor, await this.spanShapedOf(cand.anchor));
145
+ }
146
+ return out;
147
+ }
148
+ }
149
+ function computeWeave(ctx, query, pre, climb) {
150
+ const quantum = ctx.space.maxGroup;
151
+ const { ranked } = climb;
152
+ const rankedCapped = ranked.length > pre.k ? ranked.slice(0, pre.k) : ranked;
153
+ const depth = new Float64Array(query.length);
154
+ const points = [];
155
+ // WEAVE-SCALE anchors only: CAST transfers structure between things the
156
+ // QUERY weaves together — query-scale structures. A context an order of
157
+ // magnitude beyond the query is not woven BY the query (the query can at
158
+ // most quote a fragment of it, and fragment-level evidence is exactly what
159
+ // recognition and the cover already handle); CAST's own comparison gate
160
+ // demands `ctx.length ≤ query.length` before it fires, and its
161
+ // substitution seats sit within a quantum of a context's start. W is the
162
+ // perceptual quantum — the same scale multiplier the bridge's phrase-scale
163
+ // contract uses. The prefix-capped read makes an oversized anchor cost a
164
+ // bounded read instead of reconstructing (and then canonically
165
+ // recognising) a corpus-sized deposit: profiled on a 17.7M-node store,
166
+ // uncapped weaves spent 5–8s per query recognising conversation-length
167
+ // anchors that could never form a weave point.
168
+ const capBytes = query.length * quantum;
169
+ for (const cand of rankedCapped) {
170
+ const ctxBytes = read(ctx, cand.anchor, capBytes + 1);
171
+ if (ctxBytes.length === 0 || ctxBytes.length > capBytes)
172
+ continue;
173
+ const raw = alignGraded(ctx, query, ctxBytes, pre.rec.sites);
174
+ if (raw.length === 0)
175
+ continue;
176
+ for (const r of raw) {
177
+ for (let i = r.qs; i < r.qe; i++)
178
+ depth[i] += r.weight;
179
+ }
180
+ const free = [];
181
+ for (const r of raw) {
182
+ let { qs, qe, cs, weight } = r;
183
+ for (const p of points) {
184
+ for (const o of p.runs) {
185
+ if (qs >= qe)
186
+ break;
187
+ if (o.qe <= qs || o.qs >= qe)
188
+ continue;
189
+ const left = Math.max(0, o.qs - qs);
190
+ const right = Math.max(0, qe - o.qe);
191
+ if (left >= right)
192
+ qe = qs + left;
193
+ else {
194
+ cs += qe - right - qs;
195
+ qs = qe - right;
196
+ }
197
+ }
198
+ }
199
+ if (qe - qs >= Math.min(quantum, ctxBytes.length)) {
200
+ free.push({ qs, qe, cs, weight });
201
+ }
202
+ }
203
+ if (free.length > 0) {
204
+ points.push({
205
+ anchor: cand.anchor,
206
+ vote: cand.vote,
207
+ ctx: ctxBytes,
208
+ runs: free,
209
+ });
210
+ }
211
+ }
212
+ return { points, depth };
213
+ }
@@ -0,0 +1,20 @@
1
+ import type { MindContext } from "./types.js";
2
+ import { type PipelineMechanism } from "./pipeline-mechanism.js";
3
+ export { resolveConcepts, resolveConnectors } from "./mechanisms/cover.js";
4
+ export { aluToMechanism } from "./mechanisms/alu.js";
5
+ export declare const defaultMechanisms: PipelineMechanism[];
6
+ export type Provenance = "cast" | "join" | "cover" | "extract" | "recall" | "recall-echo";
7
+ export interface Thought {
8
+ bytes: Uint8Array;
9
+ provenance: Provenance;
10
+ }
11
+ /** Think: a single lightest-derivation exploration of the Sema graph.
12
+ *
13
+ * Every answer travels the same path:
14
+ * 1. Pre-computation — recognise, extension parse, guide; everything
15
+ * expensive stays lazy on Precomputed until a mechanism asks.
16
+ * 2. Grounding — every mechanism yields candidates weighed in the one
17
+ * cost ladder; the lightest grounding derivation wins.
18
+ * 3. Post-grounding — diagnostics (narrowDecision, thinGrounding),
19
+ * reasoning (multi-hop), fusion (multi-topic). */
20
+ export declare function think(ctx: MindContext, query: Uint8Array, mechs?: readonly PipelineMechanism[]): Promise<Thought | null>;