@hviana/sema 0.1.5 → 0.1.7

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 (170) hide show
  1. package/AGENTS.md +6 -5
  2. package/CITATION.cff +49 -0
  3. package/HOW_IT_WORKS.md +11 -12
  4. package/README.md +7 -5
  5. package/dist/example/demo.js +25 -25
  6. package/dist/example/train_base.d.ts +19 -21
  7. package/dist/example/train_base.js +1700 -1952
  8. package/dist/src/alphabet.d.ts +3 -3
  9. package/dist/src/alphabet.js +24 -27
  10. package/dist/src/alu/src/alu.d.ts +169 -194
  11. package/dist/src/alu/src/alu.js +374 -414
  12. package/dist/src/alu/src/expr.d.ts +36 -43
  13. package/dist/src/alu/src/expr.js +252 -278
  14. package/dist/src/alu/src/index.d.ts +9 -98
  15. package/dist/src/alu/src/index.js +10 -64
  16. package/dist/src/alu/src/kernel-arith.d.ts +1 -5
  17. package/dist/src/alu/src/kernel-arith.js +195 -285
  18. package/dist/src/alu/src/kernel-bits.d.ts +1 -5
  19. package/dist/src/alu/src/kernel-bits.js +81 -115
  20. package/dist/src/alu/src/kernel-logic.js +33 -63
  21. package/dist/src/alu/src/kernel-nd.js +155 -195
  22. package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
  23. package/dist/src/alu/src/kernel-numeric.js +259 -331
  24. package/dist/src/alu/src/operation.d.ts +112 -132
  25. package/dist/src/alu/src/operation.js +149 -163
  26. package/dist/src/alu/src/parser.d.ts +168 -175
  27. package/dist/src/alu/src/parser.js +382 -421
  28. package/dist/src/alu/src/resonance.d.ts +16 -25
  29. package/dist/src/alu/src/resonance.js +49 -56
  30. package/dist/src/alu/src/text.d.ts +3 -7
  31. package/dist/src/alu/src/text.js +31 -37
  32. package/dist/src/alu/src/value.d.ts +14 -14
  33. package/dist/src/alu/src/value.js +150 -160
  34. package/dist/src/alu/test/alu.test.js +543 -656
  35. package/dist/src/bytes.d.ts +1 -5
  36. package/dist/src/bytes.js +33 -40
  37. package/dist/src/config.d.ts +99 -102
  38. package/dist/src/config.js +78 -83
  39. package/dist/src/derive/src/deduction.d.ts +60 -62
  40. package/dist/src/derive/src/deduction.js +103 -109
  41. package/dist/src/derive/src/index.d.ts +1 -7
  42. package/dist/src/derive/src/priority-queue.d.ts +8 -8
  43. package/dist/src/derive/src/priority-queue.js +57 -61
  44. package/dist/src/derive/src/rewrite.d.ts +15 -18
  45. package/dist/src/derive/src/rewrite.js +59 -67
  46. package/dist/src/derive/src/trie.d.ts +56 -56
  47. package/dist/src/derive/src/trie.js +175 -179
  48. package/dist/src/derive/test/derive.test.js +96 -100
  49. package/dist/src/extension.d.ts +13 -16
  50. package/dist/src/geometry.d.ts +29 -43
  51. package/dist/src/geometry.js +324 -268
  52. package/dist/src/index.d.ts +2 -17
  53. package/dist/src/index.js +2 -12
  54. package/dist/src/ingest-cache.d.ts +25 -30
  55. package/dist/src/ingest-cache.js +108 -127
  56. package/dist/src/mind/articulation.d.ts +1 -5
  57. package/dist/src/mind/articulation.js +77 -112
  58. package/dist/src/mind/attention.d.ts +40 -108
  59. package/dist/src/mind/attention.js +754 -872
  60. package/dist/src/mind/canonical.d.ts +4 -19
  61. package/dist/src/mind/canonical.js +27 -31
  62. package/dist/src/mind/graph-search.d.ts +201 -225
  63. package/dist/src/mind/graph-search.js +742 -821
  64. package/dist/src/mind/index.d.ts +2 -10
  65. package/dist/src/mind/junction.d.ts +31 -58
  66. package/dist/src/mind/junction.js +172 -237
  67. package/dist/src/mind/learning.d.ts +16 -52
  68. package/dist/src/mind/learning.js +143 -165
  69. package/dist/src/mind/match.d.ts +20 -69
  70. package/dist/src/mind/match.js +259 -318
  71. package/dist/src/mind/mechanisms/alu.js +16 -16
  72. package/dist/src/mind/mechanisms/cast.d.ts +9 -13
  73. package/dist/src/mind/mechanisms/cast.js +363 -456
  74. package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
  75. package/dist/src/mind/mechanisms/confluence.js +152 -183
  76. package/dist/src/mind/mechanisms/cover.d.ts +2 -8
  77. package/dist/src/mind/mechanisms/cover.js +148 -210
  78. package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
  79. package/dist/src/mind/mechanisms/extraction.js +234 -288
  80. package/dist/src/mind/mechanisms/recall.d.ts +6 -10
  81. package/dist/src/mind/mechanisms/recall.js +141 -185
  82. package/dist/src/mind/mind.d.ts +194 -155
  83. package/dist/src/mind/mind.js +392 -289
  84. package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
  85. package/dist/src/mind/pipeline-mechanism.js +161 -172
  86. package/dist/src/mind/pipeline.d.ts +4 -14
  87. package/dist/src/mind/pipeline.js +125 -189
  88. package/dist/src/mind/primitives.d.ts +20 -33
  89. package/dist/src/mind/primitives.js +124 -120
  90. package/dist/src/mind/rationale.d.ts +86 -98
  91. package/dist/src/mind/rationale.js +113 -121
  92. package/dist/src/mind/reasoning.d.ts +2 -13
  93. package/dist/src/mind/reasoning.js +129 -166
  94. package/dist/src/mind/recognition.d.ts +1 -4
  95. package/dist/src/mind/recognition.js +198 -210
  96. package/dist/src/mind/resonance.d.ts +5 -22
  97. package/dist/src/mind/resonance.js +0 -0
  98. package/dist/src/mind/trace.d.ts +6 -25
  99. package/dist/src/mind/trace.js +50 -58
  100. package/dist/src/mind/traverse.d.ts +29 -58
  101. package/dist/src/mind/traverse.js +344 -356
  102. package/dist/src/mind/types.d.ts +122 -127
  103. package/dist/src/mind/types.js +60 -69
  104. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  105. package/dist/src/rabitq-ivf/src/database.js +201 -0
  106. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  107. package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
  108. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  109. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  110. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  111. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  112. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  113. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  114. package/dist/src/sema.d.ts +9 -13
  115. package/dist/src/sema.js +26 -40
  116. package/dist/src/store-sqlite.d.ts +171 -166
  117. package/dist/src/store-sqlite.js +768 -662
  118. package/dist/src/store.d.ts +597 -630
  119. package/dist/src/store.js +1434 -1581
  120. package/dist/src/vec.d.ts +5 -9
  121. package/dist/src/vec.js +61 -73
  122. package/example/train_base.ts +33 -15
  123. package/index.html +65 -0
  124. package/package.json +1 -1
  125. package/src/alu/README.md +1 -1
  126. package/src/alu/src/index.ts +1 -1
  127. package/src/config.ts +19 -27
  128. package/src/geometry.ts +93 -1
  129. package/src/index.ts +6 -11
  130. package/src/mind/attention.ts +11 -6
  131. package/src/mind/mechanisms/recall.ts +29 -13
  132. package/src/mind/mind.ts +250 -19
  133. package/src/mind/primitives.ts +43 -6
  134. package/src/mind/recognition.ts +26 -8
  135. package/src/mind/traverse.ts +47 -0
  136. package/src/mind/types.ts +20 -21
  137. package/src/rabitq-ivf/README.md +56 -0
  138. package/src/rabitq-ivf/src/database.ts +276 -0
  139. package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
  140. package/src/rabitq-ivf/src/ivf.ts +1330 -0
  141. package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
  142. package/src/store-sqlite.ts +204 -9
  143. package/src/store.ts +27 -34
  144. package/test/08-storage.test.mjs +3 -3
  145. package/test/13-conversation.test.mjs +190 -20
  146. package/test/14-scaling.test.mjs +2 -2
  147. package/test/35-ivf.test.mjs +263 -0
  148. package/test/36-bloom.test.mjs +123 -0
  149. package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
  150. package/dist/src/rabitq-hnsw/src/database.js +0 -405
  151. package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
  152. package/dist/src/rabitq-hnsw/src/heap.js +0 -94
  153. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
  154. package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
  155. package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
  156. package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
  157. package/dist/src/rabitq-hnsw/src/prng.js +0 -38
  158. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
  159. package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
  160. package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
  161. package/dist/src/rabitq-hnsw/src/store.js +0 -916
  162. package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
  163. package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
  164. package/src/rabitq-hnsw/README.md +0 -303
  165. package/src/rabitq-hnsw/src/database.ts +0 -492
  166. package/src/rabitq-hnsw/src/heap.ts +0 -90
  167. package/src/rabitq-hnsw/src/hnsw.ts +0 -514
  168. package/src/rabitq-hnsw/src/store.ts +0 -994
  169. package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
  170. /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
@@ -4,95 +4,92 @@ import type { ComputedSpan } from "../extension.js";
4
4
  import type { Vec } from "../vec.js";
5
5
  import { type GradedRun } from "./match.js";
6
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(
20
- ctx: MindContext,
21
- query: Uint8Array,
7
+ readonly ctx: MindContext;
8
+ readonly query: Uint8Array;
22
9
  /** Recognition result (structural + canonical). */
23
- rec: Recognition,
10
+ readonly rec: Recognition;
24
11
  /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
25
- computed: ComputedSpan[],
12
+ readonly computed: ComputedSpan[];
26
13
  /** The query's gist — the response-wide disambiguation guide. */
27
- guide: Vec,
28
- );
29
- private _windows?;
30
- /** Content-addressed W-window identities for every position in the query
31
- * (offset node id). O(|query|) probes. */
32
- get queryWindows(): Map<number, number>;
33
- private _resolved?;
34
- /** The node id of the query itself, or null when it is not a stored form.
35
- * O(|query|) probes. */
36
- get queryResolved(): number | null;
37
- private _anchorWindows;
38
- /** Content-addressed W-window identities of one anchor's own bytes
39
- * (offset → node id), memoised per anchor. Confluence intersects these;
40
- * any future identity-based mechanism reads the same cache. */
41
- windowsOf(anchor: number): Map<number, number>;
42
- /** Shared memo for {@link reachOf} (structural-IDF reads): a window's
43
- * ancestor reach is a pure function of the read-only store, so one
44
- * response-scoped memo serves every mechanism that prices commonality. */
45
- readonly reachMemo: Map<number, AncestorReach>;
46
- private _attention?;
47
- /** The full consensus climb (roots + ranked anchors) — the query-level
48
- * evidence CAST, confluence, extraction, recall's scaffolding tier, and
49
- * fusion all share. Computed on first access; a query no mechanism
50
- * climbs for (e.g. one an extension decided outright) never pays for it. */
51
- attention(): Promise<AttentionRead>;
52
- private _weave?;
53
- /** Result of {@link alignGraded} for the first k ranked anchors —
54
- * O(k · |query| · |ctx|). Consumed by CAST; reusable by any future
55
- * mechanism doing analogical transfer. */
56
- weave(): Promise<WeaveInfo>;
57
- /** Span-shaped classification of one ranked anchor, memoised per anchor id
58
- * so repeated calls (extraction's own early-exit scan, any future
59
- * template-based mechanism) never redo the work. Deliberately NOT an
60
- * eager all-anchors map: `skillExemplar` is the expensive part of
61
- * extraction (capped fan-out reads plus an O(|ctx|) scan), and most
62
- * queries are answered by the FIRST ranked anchor that qualifies — paying
63
- * for every ranked anchor regardless of where the scan stops would turn
64
- * an early-exit lookup into full O(k) work on every query. */
65
- private _spanShaped;
66
- spanShapedOf(anchor: number): Promise<SkillInfo | null>;
67
- /** Every ranked anchor's classification at once, sharing the same
68
- * per-anchor cache as {@link spanShapedOf} for a mechanism that
69
- * genuinely needs the full picture (not an early-exit scan). Mixing
70
- * access patterns across mechanisms never duplicates work: whichever
71
- * anchors an early-exit consumer already asked for are reused here, and
72
- * whichever this computes first are reused by a later early-exit scan. */
73
- spanShapedAll(): Promise<Map<number, SkillInfo | null>>;
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>>;
74
71
  }
75
72
  /** The weave-local structural alignment, computed once and consumed by CAST
76
73
  * (and any future mechanism doing analogical transfer). */
77
74
  export interface WeaveInfo {
78
- /** Per-anchor alignment: context bytes, vote weight, and graded runs. */
79
- points: Array<{
80
- anchor: number;
81
- vote: number;
82
- ctx: Uint8Array;
83
- runs: GradedRun[];
84
- }>;
85
- /** Weighted depth at each query byte — sum of alignment weights.
86
- * `depth[i]` is the total evidence that byte i is shared among the
87
- * aligned structures. */
88
- depth: Float64Array;
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;
89
86
  }
90
87
  /** Span-shaped classification of one anchor — the structural information
91
88
  * extraction uses to decide whether a learned fact can serve as a template
92
89
  * for reading an analogous span out of the query. */
93
90
  export interface SkillInfo {
94
- contextBytes: Uint8Array;
95
- answerBytes: Uint8Array;
91
+ contextBytes: Uint8Array;
92
+ answerBytes: Uint8Array;
96
93
  }
97
94
  /** Raw result from a mechanism's `run()`. The pipeline computes the weight
98
95
  * from `moves` + `PASS * unaccounted(accounted)` — the mechanism does not
@@ -102,53 +99,44 @@ export interface SkillInfo {
102
99
  * computing `weigh(accounted, moves)`. This is for mechanisms whose cost
103
100
  * is derived externally (e.g. cover: the A*LD derivation's g-value). */
104
101
  export interface MechanismResult {
105
- bytes: Uint8Array;
106
- accounted: Array<[number, number]>;
107
- moves: number;
108
- used?: ReadonlySet<number>;
109
- unexplained: string;
110
- /** Explicit weight override. When absent, weight = moves + PASS·unaccounted. */
111
- weight?: number;
112
- /** Override the mechanism's default provenance for this result.
113
- * When absent, the pipeline uses `mech.provenance`. */
114
- provenance?: string;
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;
115
112
  }
116
113
  export interface PipelineMechanism {
117
- /** Stable identifier for trace/debug. */
118
- readonly name: string;
119
- /** Which provenance tag the pipeline attaches to this mechanism's answers. */
120
- readonly provenance: string;
121
- /** Parse authoritative spans BEFORE the grounding loop.
122
- * Only needed by computational mechanisms (e.g. ALU). Results from ALL
123
- * mechanisms that implement this are collected into `Precomputed.computed`
124
- * before any `floor()` or `run()` is called. */
125
- parse?(query: Uint8Array): Promise<ComputedSpan[]>;
126
- /** Admissible lower bound on this mechanism's weight.
127
- * Returns `null` when the mechanism structurally cannot fire.
128
- *
129
- * `worthRunning(cheapFloor)` reports whether the CURRENT incumbent
130
- * (established by mechanisms that already ran this response, cover being
131
- * first — see `defaultMechanisms`) could still be beaten by a floor no
132
- * tighter than `cheapFloor`. THE INVESTMENT DISCIPLINE: before
133
- * first-touching an expensive shared analysis (`pre.attention()`,
134
- * `pre.weave()`, …), check `worthRunning(bound)` with this mechanism's
135
- * cheapest possible bound — and when it fails, RETURN THE BOUND rather
136
- * than null. The bound is still admissible (it never overstates cost),
137
- * the pipeline's own check then prunes `run()` and records the truthful
138
- * "cannot beat incumbent" trace note, and no analysis was computed just
139
- * to be discarded. This is uniform: no mechanism asks what produced the
140
- * incumbent — a computed extension result and an ordinary cheap cover
141
- * prune the same way. */
142
- floor(
143
- ctx: MindContext,
144
- query: Uint8Array,
145
- pre: Precomputed,
146
- worthRunning: (floor: number) => boolean,
147
- ): Promise<number | null>;
148
- /** Produce candidate answers. */
149
- run(
150
- ctx: MindContext,
151
- query: Uint8Array,
152
- pre: Precomputed,
153
- ): Promise<MechanismResult[]>;
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[]>;
154
142
  }
@@ -36,189 +36,178 @@ import { skillExemplar } from "./mechanisms/extraction.js";
36
36
  // analysis, so lazy analyses are only ever computed for a mechanism that
37
37
  // could still win.
38
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(
49
- ctx,
50
- query,
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,
51
49
  /** Recognition result (structural + canonical). */
52
- rec,
50
+ rec,
53
51
  /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
54
- computed,
52
+ computed,
55
53
  /** The query's gist — the response-wide disambiguation guide. */
56
- guide,
57
- ) {
58
- this.ctx = ctx;
59
- this.query = query;
60
- this.rec = rec;
61
- this.computed = computed;
62
- this.guide = guide;
63
- this.k = ctx.cfg.recallQueryK * 2;
64
- }
65
- // ── Cheap lazy analyses ───────────────────────────────────────────────
66
- _windows;
67
- /** Content-addressed W-window identities for every position in the query
68
- * (offset → node id). O(|query|) probes. */
69
- get queryWindows() {
70
- return this._windows ??= windowIds(this.ctx, this.query);
71
- }
72
- _resolved;
73
- /** The node id of the query itself, or null when it is not a stored form.
74
- * O(|query|) probes. */
75
- get queryResolved() {
76
- if (this._resolved === undefined) {
77
- this._resolved = resolve(this.ctx, this.query);
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;
78
61
  }
79
- return this._resolved;
80
- }
81
- _anchorWindows = new Map();
82
- /** Content-addressed W-window identities of one anchor's own bytes
83
- * (offset → node id), memoised per anchor. Confluence intersects these;
84
- * any future identity-based mechanism reads the same cache. */
85
- windowsOf(anchor) {
86
- let w = this._anchorWindows.get(anchor);
87
- if (w === undefined) {
88
- w = windowIds(this.ctx, read(this.ctx, anchor));
89
- this._anchorWindows.set(anchor, w);
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);
90
68
  }
91
- return w;
92
- }
93
- /** Shared memo for {@link reachOf} (structural-IDF reads): a window's
94
- * ancestor reach is a pure function of the read-only store, so one
95
- * response-scoped memo serves every mechanism that prices commonality. */
96
- reachMemo = new Map();
97
- // ── Expensive lazy analyses ───────────────────────────────────────────
98
- //
99
- // Async, cached-by-promise: the first caller starts the computation, every
100
- // later caller (any mechanism, any phase) awaits the same promise. A
101
- // mechanism MUST check its cheap floor gates and the pipeline's
102
- // `worthRunning` predicate before first-touching one of these.
103
- _attention;
104
- /** The full consensus climb (roots + ranked anchors) — the query-level
105
- * evidence CAST, confluence, extraction, recall's scaffolding tier, and
106
- * fusion all share. Computed on first access; a query no mechanism
107
- * climbs for (e.g. one an extension decided outright) never pays for it. */
108
- attention() {
109
- return this._attention ??= climbAttentionAll(this.ctx, this.query, this.k);
110
- }
111
- _weave;
112
- /** Result of {@link alignGraded} for the first k ranked anchors —
113
- * O(k · |query| · |ctx|). Consumed by CAST; reusable by any future
114
- * mechanism doing analogical transfer. */
115
- weave() {
116
- return this._weave ??= this.attention().then((climb) =>
117
- computeWeave(this.ctx, this.query, this, climb)
118
- );
119
- }
120
- /** Span-shaped classification of one ranked anchor, memoised per anchor id
121
- * so repeated calls (extraction's own early-exit scan, any future
122
- * template-based mechanism) never redo the work. Deliberately NOT an
123
- * eager all-anchors map: `skillExemplar` is the expensive part of
124
- * extraction (capped fan-out reads plus an O(|ctx|) scan), and most
125
- * queries are answered by the FIRST ranked anchor that qualifies — paying
126
- * for every ranked anchor regardless of where the scan stops would turn
127
- * an early-exit lookup into full O(k) work on every query. */
128
- _spanShaped = new Map();
129
- spanShapedOf(anchor) {
130
- let p = this._spanShaped.get(anchor);
131
- if (p === undefined) {
132
- p = skillExemplar(this.ctx, anchor, this.guide);
133
- this._spanShaped.set(anchor, p);
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;
134
77
  }
135
- return p;
136
- }
137
- /** Every ranked anchor's classification at once, sharing the same
138
- * per-anchor cache as {@link spanShapedOf} for a mechanism that
139
- * genuinely needs the full picture (not an early-exit scan). Mixing
140
- * access patterns across mechanisms never duplicates work: whichever
141
- * anchors an early-exit consumer already asked for are reused here, and
142
- * whichever this computes first are reused by a later early-exit scan. */
143
- async spanShapedAll() {
144
- const { ranked } = await this.attention();
145
- const out = new Map();
146
- for (const cand of ranked) {
147
- if (out.has(cand.anchor)) {
148
- continue;
149
- }
150
- out.set(cand.anchor, await this.spanShapedOf(cand.anchor));
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;
151
89
  }
152
- return out;
153
- }
154
- }
155
- function computeWeave(ctx, query, pre, climb) {
156
- const quantum = ctx.space.maxGroup;
157
- const { ranked } = climb;
158
- const rankedCapped = ranked.length > pre.k ? ranked.slice(0, pre.k) : ranked;
159
- const depth = new Float64Array(query.length);
160
- const points = [];
161
- // WEAVE-SCALE anchors only: CAST transfers structure between things the
162
- // QUERY weaves together — query-scale structures. A context an order of
163
- // magnitude beyond the query is not woven BY the query (the query can at
164
- // most quote a fragment of it, and fragment-level evidence is exactly what
165
- // recognition and the cover already handle); CAST's own comparison gate
166
- // demands `ctx.length query.length` before it fires, and its
167
- // substitution seats sit within a quantum of a context's start. W is the
168
- // perceptual quantum the same scale multiplier the bridge's phrase-scale
169
- // contract uses. The prefix-capped read makes an oversized anchor cost a
170
- // bounded read instead of reconstructing (and then canonically
171
- // recognising) a corpus-sized deposit: profiled on a 17.7M-node store,
172
- // uncapped weaves spent 5–8s per query recognising conversation-length
173
- // anchors that could never form a weave point.
174
- const capBytes = query.length * quantum;
175
- for (const cand of rankedCapped) {
176
- const ctxBytes = read(ctx, cand.anchor, capBytes + 1);
177
- if (ctxBytes.length === 0 || ctxBytes.length > capBytes) {
178
- continue;
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);
179
107
  }
180
- const raw = alignGraded(ctx, query, ctxBytes, pre.rec.sites);
181
- if (raw.length === 0) {
182
- continue;
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));
183
114
  }
184
- for (const r of raw) {
185
- for (let i = r.qs; i < r.qe; i++) {
186
- depth[i] += r.weight;
187
- }
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;
188
131
  }
189
- const free = [];
190
- for (const r of raw) {
191
- let { qs, qe, cs, weight } = r;
192
- for (const p of points) {
193
- for (const o of p.runs) {
194
- if (qs >= qe) {
195
- break;
196
- }
197
- if (o.qe <= qs || o.qs >= qe) {
198
- continue;
199
- }
200
- const left = Math.max(0, o.qs - qs);
201
- const right = Math.max(0, qe - o.qe);
202
- if (left >= right) {
203
- qe = qs + left;
204
- } else {
205
- cs += qe - right - qs;
206
- qs = qe - right;
207
- }
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));
208
145
  }
209
- }
210
- if (qe - qs >= Math.min(quantum, ctxBytes.length)) {
211
- free.push({ qs, qe, cs, weight });
212
- }
146
+ return out;
213
147
  }
214
- if (free.length > 0) {
215
- points.push({
216
- anchor: cand.anchor,
217
- vote: cand.vote,
218
- ctx: ctxBytes,
219
- runs: free,
220
- });
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
+ }
221
211
  }
222
- }
223
- return { points, depth };
212
+ return { points, depth };
224
213
  }