@hviana/sema 0.2.6 → 0.2.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 (133) 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 +202 -0
  56. package/dist/src/geometry.js +575 -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 +405 -0
  64. package/dist/src/mind/attention.js +1829 -0
  65. package/dist/src/mind/bridge.d.ts +30 -0
  66. package/dist/src/mind/bridge.js +569 -0
  67. package/dist/src/mind/canonical.d.ts +29 -0
  68. package/dist/src/mind/canonical.js +88 -0
  69. package/dist/src/mind/graph-search.d.ts +285 -0
  70. package/dist/src/mind/graph-search.js +956 -0
  71. package/dist/src/mind/index.d.ts +7 -0
  72. package/dist/src/mind/index.js +5 -0
  73. package/dist/src/mind/junction.d.ts +124 -0
  74. package/dist/src/mind/junction.js +317 -0
  75. package/dist/src/mind/learning.d.ts +47 -0
  76. package/dist/src/mind/learning.js +248 -0
  77. package/dist/src/mind/match.d.ts +126 -0
  78. package/dist/src/mind/match.js +441 -0
  79. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  80. package/dist/src/mind/mechanisms/alu.js +36 -0
  81. package/dist/src/mind/mechanisms/cast.d.ts +89 -0
  82. package/dist/src/mind/mechanisms/cast.js +716 -0
  83. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  84. package/dist/src/mind/mechanisms/confluence.js +225 -0
  85. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  86. package/dist/src/mind/mechanisms/cover.js +203 -0
  87. package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
  88. package/dist/src/mind/mechanisms/extraction.js +387 -0
  89. package/dist/src/mind/mechanisms/recall.d.ts +13 -0
  90. package/dist/src/mind/mechanisms/recall.js +325 -0
  91. package/dist/src/mind/mind.d.ts +276 -0
  92. package/dist/src/mind/mind.js +572 -0
  93. package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
  94. package/dist/src/mind/pipeline-mechanism.js +213 -0
  95. package/dist/src/mind/pipeline.d.ts +20 -0
  96. package/dist/src/mind/pipeline.js +217 -0
  97. package/dist/src/mind/primitives.d.ts +70 -0
  98. package/dist/src/mind/primitives.js +287 -0
  99. package/dist/src/mind/rationale.d.ts +139 -0
  100. package/dist/src/mind/rationale.js +163 -0
  101. package/dist/src/mind/reasoning.d.ts +34 -0
  102. package/dist/src/mind/reasoning.js +240 -0
  103. package/dist/src/mind/recognition.d.ts +20 -0
  104. package/dist/src/mind/recognition.js +390 -0
  105. package/dist/src/mind/resonance.d.ts +23 -0
  106. package/dist/src/mind/resonance.js +0 -0
  107. package/dist/src/mind/trace.d.ts +15 -0
  108. package/dist/src/mind/trace.js +73 -0
  109. package/dist/src/mind/traverse.d.ts +113 -0
  110. package/dist/src/mind/traverse.js +568 -0
  111. package/dist/src/mind/types.d.ts +289 -0
  112. package/dist/src/mind/types.js +130 -0
  113. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  114. package/dist/src/rabitq-ivf/src/database.js +201 -0
  115. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  116. package/dist/src/rabitq-ivf/src/index.js +4 -0
  117. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  118. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  119. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  120. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  121. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  122. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  123. package/dist/src/sema.d.ts +31 -0
  124. package/dist/src/sema.js +63 -0
  125. package/dist/src/store-sqlite.d.ts +184 -0
  126. package/dist/src/store-sqlite.js +942 -0
  127. package/dist/src/store.d.ts +663 -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
  132. package/src/mind/attention.ts +178 -63
  133. package/test/51-structural-resonance-ladder.test.mjs +6 -3
@@ -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>;
@@ -0,0 +1,217 @@
1
+ // pipeline.ts — the think pipeline (Section 5 of the mind).
2
+ //
3
+ // think() is the whole file's job: one lightest-derivation choice among
4
+ // UNIFORM mechanisms. The pipeline sees mechanisms through the
5
+ // PipelineMechanism interface only — it never imports a mechanism-specific
6
+ // type and never has a special-case branch for any mechanism. Adding a
7
+ // mechanism means registering one object; removing one means dropping it
8
+ // from the list. The mechanisms themselves live in mechanisms/ (one file
9
+ // each); the shared pre-computation they exchange lives in Precomputed
10
+ // (pipeline-mechanism.ts).
11
+ import { PASS, STEP } from "./graph-search.js";
12
+ import { gistOf, resolve } from "./primitives.js";
13
+ import { recognise } from "./recognition.js";
14
+ import { fuseAttention, reason } from "./reasoning.js";
15
+ import { unexplainedSpans } from "./rationale.js";
16
+ import { rItem } from "./trace.js";
17
+ import { Precomputed } from "./pipeline-mechanism.js";
18
+ import { coverMechanism } from "./mechanisms/cover.js";
19
+ import { castMechanism } from "./mechanisms/cast.js";
20
+ import { confluenceMechanism } from "./mechanisms/confluence.js";
21
+ import { extractionMechanism } from "./mechanisms/extraction.js";
22
+ import { recallMechanism } from "./mechanisms/recall.js";
23
+ // Re-exports: cover's pre-resolution helpers and the ALU adapter kept
24
+ // importable from the pipeline module (their historical home).
25
+ export { resolveConcepts, resolveConnectors } from "./mechanisms/cover.js";
26
+ export { aluToMechanism } from "./mechanisms/alu.js";
27
+ // ── Extension dispatch (pre-loop parse) ─────────────────────────────────────
28
+ async function collectComputed(mechanisms, query) {
29
+ const out = [];
30
+ for (const m of mechanisms) {
31
+ if (m.parse)
32
+ out.push(...await m.parse(query));
33
+ }
34
+ return out;
35
+ }
36
+ // ── Built-in mechanisms ─────────────────────────────────────────────────────
37
+ // ORDER MATTERS, but only through the uniform floor/worthRunning pruning —
38
+ // no mechanism is special-cased. Cover runs FIRST: when a computed
39
+ // extension result (e.g. ALU) exists, cover masks it in at near-zero cost
40
+ // (see mechanisms/cover.ts), which becomes `best` before any other mechanism
41
+ // invests in its own precomputation. CAST's and confluence's floors (2*STEP,
42
+ // 3*STEP) then fail `worthRunning` and are skipped by the SAME admissible-
43
+ // floor pruning every mechanism is already subject to — not by asking
44
+ // "is this an extension?". Grade TIES keep the earlier candidate, so this
45
+ // order is also the tie-break priority: cover, cast, confluence, extraction,
46
+ // recall.
47
+ export const defaultMechanisms = [
48
+ coverMechanism,
49
+ castMechanism,
50
+ confluenceMechanism,
51
+ extractionMechanism,
52
+ recallMechanism,
53
+ ];
54
+ /** Think: a single lightest-derivation exploration of the Sema graph.
55
+ *
56
+ * Every answer travels the same path:
57
+ * 1. Pre-computation — recognise, extension parse, guide; everything
58
+ * expensive stays lazy on Precomputed until a mechanism asks.
59
+ * 2. Grounding — every mechanism yields candidates weighed in the one
60
+ * cost ladder; the lightest grounding derivation wins.
61
+ * 3. Post-grounding — diagnostics (narrowDecision, thinGrounding),
62
+ * reasoning (multi-hop), fusion (multi-topic). */
63
+ export async function think(ctx, query, mechs) {
64
+ if (query.length === 0)
65
+ return null;
66
+ ctx._edgeGuide = gistOf(ctx, query);
67
+ ctx._edgeChoice.clear();
68
+ const t = ctx.trace?.enter("think", [rItem(query, "query")]);
69
+ const done = (answer, note) => {
70
+ t?.done(answer
71
+ ? [rItem(answer, "answer", resolve(ctx, answer) ?? undefined)]
72
+ : [], note);
73
+ return answer;
74
+ };
75
+ // ── Pre-computation ──────────────────────────────────────────────────
76
+ const mechanisms = mechs ?? defaultMechanisms;
77
+ const rec = recognise(ctx, query);
78
+ // Phase 1: collect computed spans from mechanisms that implement parse()
79
+ const computed = await collectComputed(mechanisms, query);
80
+ if (computed.length > 0) {
81
+ ctx.trace?.step("computeExtensions", [rItem(query, "query")], computed.map((u) => rItem(query.subarray(u.i, u.j), "operand", undefined, [u.i, u.j])), `extensions recognised and evaluated ${computed.length} computation(s)`);
82
+ for (const u of computed) {
83
+ ctx.trace?.step("evalComputation", [rItem(query.subarray(u.i, u.j), "expression", undefined, [u.i, u.j])], [rItem(u.bytes, "result", resolve(ctx, u.bytes) ?? undefined)], "evaluate the recognised operation to its authoritative result");
84
+ }
85
+ }
86
+ // Phase 2: the shared pre-computation container. Eager fields only
87
+ // (recognition, computed spans, guide) — every expensive analysis
88
+ // (consensus climb, weave, span-shape classification) is a lazily-cached
89
+ // method on Precomputed, first-touched by whichever mechanism's floor
90
+ // survives its cheap gates and the worthRunning check. A query no
91
+ // mechanism climbs for (e.g. one an extension decided) never climbs.
92
+ const pre = new Precomputed(ctx, query, rec, computed, ctx._edgeGuide);
93
+ const grade = (w) => Math.floor(w / STEP);
94
+ const unaccounted = (spans) => unexplainedSpans(query.length, spans)
95
+ .reduce((sum, [s, e]) => sum + (e - s), 0);
96
+ const weigh = (accounted, moves) => moves + PASS * unaccounted(accounted);
97
+ const candidates = [];
98
+ let best = null;
99
+ const consider = (c) => {
100
+ if (c.bytes.length === 0)
101
+ return;
102
+ candidates.push(c);
103
+ if (best === null || grade(c.weight) < grade(best.weight))
104
+ best = c;
105
+ };
106
+ const worthRunning = (floor) => best === null || grade(floor) < grade(best.weight);
107
+ // Phase 3: grounding loop
108
+ for (const mech of mechanisms) {
109
+ const floor = await mech.floor(ctx, query, pre, worthRunning);
110
+ if (floor === null) {
111
+ ctx.trace?.step("skipMechanism", [], [], `${mech.name} skipped — structural precondition failed`);
112
+ continue;
113
+ }
114
+ if (!worthRunning(floor)) {
115
+ ctx.trace?.step("skipMechanism", [], [], `${mech.name} skipped — floor ${floor} cannot beat incumbent (grade ${grade(best.weight)})`);
116
+ continue;
117
+ }
118
+ const results = await mech.run(ctx, query, pre);
119
+ for (const r of results) {
120
+ const weight = r.weight ?? weigh(r.accounted, r.moves);
121
+ consider({
122
+ bytes: r.bytes,
123
+ provenance: r.provenance ?? mech.provenance,
124
+ weight,
125
+ used: r.used,
126
+ accounted: r.accounted,
127
+ unexplained: r.unexplained,
128
+ });
129
+ }
130
+ }
131
+ // (TS cannot see the closure assignments into `best` and narrows it to its
132
+ // initial null, so the read-back needs the assertion.)
133
+ const decided = best;
134
+ if (candidates.length > 1) {
135
+ ctx.trace?.step("decideGrounding", candidates.map((c) => rItem(c.bytes, `${c.provenance} (weight ${c.weight.toFixed(3)}${c.unexplained ? `, unexplained: "${c.unexplained}"` : ""})`)), decided ? [rItem(decided.bytes, decided.provenance)] : [], "the lightest grounding derivation wins — every mechanism weighed in the one cost ladder");
136
+ if (decided !== null) {
137
+ let runnerUp = null;
138
+ for (const c of candidates) {
139
+ if (c === decided)
140
+ continue;
141
+ if (runnerUp === null || grade(c.weight) < grade(runnerUp.weight)) {
142
+ runnerUp = c;
143
+ }
144
+ }
145
+ if (runnerUp !== null) {
146
+ const margin = grade(runnerUp.weight) - grade(decided.weight);
147
+ if (margin <= 1) {
148
+ ctx.trace?.step("narrowDecision", [
149
+ rItem(decided.bytes, `${decided.provenance} (weight ${decided.weight.toFixed(3)})`),
150
+ ], [
151
+ rItem(runnerUp.bytes, `${runnerUp.provenance} (weight ${runnerUp.weight.toFixed(3)})`),
152
+ ], `margin ${margin} grade-unit(s) — the decision could change with one more training fact`);
153
+ }
154
+ }
155
+ }
156
+ }
157
+ if (decided === null) {
158
+ done(null, "no mechanism grounded an answer");
159
+ return null;
160
+ }
161
+ // Honesty density
162
+ {
163
+ const covered = query.length - unaccounted(decided.accounted);
164
+ const density = query.length > 0 ? covered / query.length : 1;
165
+ const thinBar = 1 / ctx.space.maxGroup;
166
+ if (density < thinBar) {
167
+ ctx.trace?.step("thinGrounding", [rItem(decided.bytes, decided.provenance)], [], `grounded but thin — density ${density.toFixed(3)} is below 1/W (${thinBar.toFixed(3)})`);
168
+ }
169
+ }
170
+ const answer = decided.bytes;
171
+ const provenance = decided.provenance;
172
+ const castUsed = decided.used ?? new Set();
173
+ // ── Post-grounding, gated by provenance ──────────────────────────────
174
+ const preConsumed = provenance === "cast" || provenance === "join"
175
+ ? castUsed
176
+ : provenance === "recall" || provenance === "recall-echo"
177
+ ? new Set()
178
+ : new Set(recognise(ctx, answer).sites.map((s) => s.payload));
179
+ const reasoned = await reason(ctx, query, answer, preConsumed, pre);
180
+ // Fuse only when the query has a genuine REMAINDER no mechanism's
181
+ // structural evidence touched at all. `decided.accounted` alone
182
+ // undercounts this: it is a COST-LADDER quantity (cover.ts prices its
183
+ // masked/computed spans at near-zero and deliberately leaves them out of
184
+ // `accounted` so PASS-bridged bytes are still charged), not a coverage
185
+ // one — a query fully explained by one computed span plus bridged
186
+ // connectors can report `accounted: []` while nothing is actually left
187
+ // unexplained. The genuine remainder is what NEITHER the winning
188
+ // candidate's accounted spans NOR any recognised extension's computed
189
+ // span (`pre.computed` — every mechanism's parse() output, ALU included)
190
+ // ever touched. A remainder under one river-fold quantum (W, the same
191
+ // floor cover.ts's restatedSpan and the honesty-density bar above both
192
+ // use) is bridging punctuation/whitespace, never a second topic —
193
+ // observed: a single space between two fully-computed arithmetic spans
194
+ // ("2+2 3+3") registered as "unaccounted" and pulled in an unrelated
195
+ // corpus fact, corrupting "4 6" into "4 63".
196
+ const explained = [
197
+ ...decided.accounted,
198
+ ...pre.computed.map((u) => [u.i, u.j]),
199
+ ];
200
+ const remainder = unaccounted(explained);
201
+ // Whether the winning candidate's entire recognised substance is
202
+ // COMPUTED — every accounted span exactly a pre.computed span, nothing
203
+ // from a genuinely recognised/climbed site. fuseAttention's lone-root
204
+ // shortcut assumes a single point of attention already IS primary's own
205
+ // source; that assumption is exactly backwards for a pure computation
206
+ // (an ALU result has no anchor of its own) — see fuseAttention's
207
+ // `unclimbed` parameter, gated there by Attention.breadth so a
208
+ // coincidental echo (which this flag alone cannot distinguish) is still
209
+ // rejected.
210
+ const unclimbed = decided.accounted.length > 0 &&
211
+ decided.accounted.every(([i, j]) => pre.computed.some((u) => u.i === i && u.j === j));
212
+ const fused = remainder >= ctx.space.maxGroup
213
+ ? await fuseAttention(ctx, query, reasoned, pre, unclimbed)
214
+ : reasoned;
215
+ done(fused, "grounded, reasoned forward, fused across points of attention");
216
+ return { bytes: fused, provenance };
217
+ }
@@ -0,0 +1,70 @@
1
+ import { Vec } from "../vec.js";
2
+ import { Sema } from "../sema.js";
3
+ import type { Input, MindContext } from "./types.js";
4
+ /** The content key of a byte span — one latin1 char per byte, an exact,
5
+ * collision-free encoding. Spans on the perception path are query-scale
6
+ * (windows, regions, candidate spans), so key construction is far cheaper
7
+ * than the river fold it deduplicates. */
8
+ export declare function latin1Key(bytes: Uint8Array): string;
9
+ /** Perceive input into a content-defined tree (the river fold).
10
+ * Deterministic — identical bytes always produce an identical tree.
11
+ *
12
+ * `boundaries` is an optional sorted list of proper byte offsets where the
13
+ * fold must split so that each prefix segment folds identically to how it
14
+ * folded when it was learned (§10.3 stable-prefix contract). Only the
15
+ * CALLER — who assembled the multi-turn context — knows where those
16
+ * boundaries are; the geometry never guesses them from the bytes. */
17
+ export declare function perceive(ctx: MindContext, input: Input, leafAt?: (i: number) => number | null, lookup?: (ids: number[]) => number | null, boundaries?: readonly number[]): Sema;
18
+ /** The DEPOSIT-shaped perceive. A FIRST-SEEN input takes the PLAIN fold
19
+ * (bit-identical to inference perception of a standalone query — that
20
+ * structural train/inference agreement is load-bearing for exact recall),
21
+ * computed incrementally via the fold's level pyramid
22
+ * ({@link bytesToTreePyramid}). An input that EXTENDS a previously
23
+ * deposited one is a conversation context grown by one turn — the cached
24
+ * prefix length IS the turn boundary (derived from the deposit sequence
25
+ * itself, never from content conventions) — and takes the STABLE-PREFIX
26
+ * fold over the accumulated boundaries, bit-identical to the boundary
27
+ * fold query-time conversation perception uses, so the trained context
28
+ * node and the query's context subtree are the SAME node. Segment folds
29
+ * reuse across deposits ({@link stablePrefixFoldIncremental}) — O(turn)
30
+ * instead of O(context) per turn. The fold state is purely a cache; the
31
+ * boundary accumulation is what an evicted chain loses (falling back to
32
+ * the plain fold, the pre-boundary shape — a warm replay restores it). */
33
+ export declare function perceiveDeposit(ctx: MindContext, bytes: Uint8Array, conversational?: boolean): Sema;
34
+ /** The raw bytes of an input — modality-neutral conversion. */
35
+ export declare function inputBytes(ctx: MindContext, input: Input): Uint8Array;
36
+ /** Convenience: the gist vector of a byte span. */
37
+ export declare function gistOf(ctx: MindContext, bytes: Uint8Array): Vec;
38
+ /** Fold a perceived tree bottom-up against the store's content-addressed maps:
39
+ * every leaf is named by findLeaf, every branch by findBranch over its kids'
40
+ * ids (null the moment any child is unknown). `visit`, when given, sees each
41
+ * node with its byte span and resolved id. Returns the node's byte end and
42
+ * resolved id. */
43
+ export declare function foldTree(ctx: MindContext, n: Sema, start: number, visit?: (n: Sema, start: number, end: number, node: number | null) => void): {
44
+ end: number;
45
+ node: number | null;
46
+ };
47
+ /** The canonical node id of a byte span: perceive it in isolation — the way
48
+ * training did — and recover its root bottom-up. Returns null if any part is
49
+ * unknown. */
50
+ export declare function resolve(ctx: MindContext, bytes: Uint8Array): number | null;
51
+ /** Equivalence-class resolution: when the exact content-addressed lookup
52
+ * misses, find a stored node whose CANONICAL key equals the span's — the
53
+ * store's canon index proposes candidates by key hash, and each is verified
54
+ * by re-canonicalizing its bytes (hash-then-verify, like every content
55
+ * lookup). Among verified candidates, one that leads somewhere (has a
56
+ * continuation edge) is preferred; ties break to the lowest id — a corpus
57
+ * property, not a seed property. Null when the response carries no
58
+ * canonicalizer, the store has no canon index, or nothing verifies. */
59
+ export declare function canonResolve(ctx: MindContext, bytes: Uint8Array): number | null;
60
+ /** Walk a perceived tree in POST-ORDER with byte offsets — children before
61
+ * their parent, `visit(node, start, end)` for every node including leaves.
62
+ * Returns the byte end. The one shared traversal the offset-carrying tree
63
+ * readers (recognition via foldTree's richer variant, attention's region
64
+ * collection, resonance's branch counting) build on, so each does not
65
+ * re-derive the offset bookkeeping. (recognition.segment keeps its own
66
+ * walk: its flush semantics need PRE-order decisions at leaf-parents, which
67
+ * a post-order visitor cannot express.) */
68
+ export declare function walkTree(n: Sema, start: number, visit: (node: Sema, start: number, end: number) => void): number;
69
+ /** Reconstruct a node's byte content from the DAG, up to `maxLen` bytes. */
70
+ export declare function read(ctx: MindContext, id: number, maxLen?: number): Uint8Array;