@hviana/sema 0.1.4 → 0.1.5

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 (122) hide show
  1. package/dist/example/demo.js +25 -25
  2. package/dist/example/train_base.d.ts +21 -19
  3. package/dist/example/train_base.js +1952 -1684
  4. package/dist/src/alphabet.d.ts +3 -3
  5. package/dist/src/alphabet.js +27 -24
  6. package/dist/src/alu/src/alu.d.ts +194 -169
  7. package/dist/src/alu/src/alu.js +414 -374
  8. package/dist/src/alu/src/expr.d.ts +43 -36
  9. package/dist/src/alu/src/expr.js +278 -252
  10. package/dist/src/alu/src/index.d.ts +98 -9
  11. package/dist/src/alu/src/index.js +63 -9
  12. package/dist/src/alu/src/kernel-arith.d.ts +5 -1
  13. package/dist/src/alu/src/kernel-arith.js +285 -195
  14. package/dist/src/alu/src/kernel-bits.d.ts +5 -1
  15. package/dist/src/alu/src/kernel-bits.js +115 -81
  16. package/dist/src/alu/src/kernel-logic.js +63 -33
  17. package/dist/src/alu/src/kernel-nd.js +195 -155
  18. package/dist/src/alu/src/kernel-numeric.d.ts +80 -16
  19. package/dist/src/alu/src/kernel-numeric.js +331 -259
  20. package/dist/src/alu/src/operation.d.ts +132 -112
  21. package/dist/src/alu/src/operation.js +163 -149
  22. package/dist/src/alu/src/parser.d.ts +175 -168
  23. package/dist/src/alu/src/parser.js +421 -382
  24. package/dist/src/alu/src/resonance.d.ts +25 -16
  25. package/dist/src/alu/src/resonance.js +56 -49
  26. package/dist/src/alu/src/text.d.ts +7 -3
  27. package/dist/src/alu/src/text.js +37 -31
  28. package/dist/src/alu/src/value.d.ts +14 -14
  29. package/dist/src/alu/src/value.js +160 -150
  30. package/dist/src/alu/test/alu.test.js +656 -543
  31. package/dist/src/bytes.d.ts +5 -1
  32. package/dist/src/bytes.js +40 -33
  33. package/dist/src/config.d.ts +102 -102
  34. package/dist/src/config.js +83 -83
  35. package/dist/src/derive/src/deduction.d.ts +62 -60
  36. package/dist/src/derive/src/deduction.js +109 -103
  37. package/dist/src/derive/src/index.d.ts +7 -1
  38. package/dist/src/derive/src/priority-queue.d.ts +8 -8
  39. package/dist/src/derive/src/priority-queue.js +61 -57
  40. package/dist/src/derive/src/rewrite.d.ts +18 -15
  41. package/dist/src/derive/src/rewrite.js +67 -59
  42. package/dist/src/derive/src/trie.d.ts +56 -56
  43. package/dist/src/derive/src/trie.js +179 -175
  44. package/dist/src/derive/test/derive.test.js +100 -96
  45. package/dist/src/extension.d.ts +16 -13
  46. package/dist/src/geometry.d.ts +42 -17
  47. package/dist/src/geometry.js +266 -235
  48. package/dist/src/index.d.ts +17 -2
  49. package/dist/src/index.js +7 -1
  50. package/dist/src/ingest-cache.d.ts +30 -25
  51. package/dist/src/ingest-cache.js +127 -108
  52. package/dist/src/mind/articulation.d.ts +5 -1
  53. package/dist/src/mind/articulation.js +112 -77
  54. package/dist/src/mind/attention.d.ts +108 -40
  55. package/dist/src/mind/attention.js +871 -749
  56. package/dist/src/mind/canonical.d.ts +19 -4
  57. package/dist/src/mind/canonical.js +31 -27
  58. package/dist/src/mind/graph-search.d.ts +225 -201
  59. package/dist/src/mind/graph-search.js +821 -742
  60. package/dist/src/mind/index.d.ts +10 -2
  61. package/dist/src/mind/junction.d.ts +58 -31
  62. package/dist/src/mind/junction.js +237 -172
  63. package/dist/src/mind/learning.d.ts +52 -16
  64. package/dist/src/mind/learning.js +165 -143
  65. package/dist/src/mind/match.d.ts +69 -20
  66. package/dist/src/mind/match.js +318 -259
  67. package/dist/src/mind/mechanisms/alu.js +16 -16
  68. package/dist/src/mind/mechanisms/cast.d.ts +13 -9
  69. package/dist/src/mind/mechanisms/cast.js +456 -363
  70. package/dist/src/mind/mechanisms/confluence.d.ts +12 -8
  71. package/dist/src/mind/mechanisms/confluence.js +183 -152
  72. package/dist/src/mind/mechanisms/cover.d.ts +8 -2
  73. package/dist/src/mind/mechanisms/cover.js +210 -148
  74. package/dist/src/mind/mechanisms/extraction.d.ts +34 -8
  75. package/dist/src/mind/mechanisms/extraction.js +288 -234
  76. package/dist/src/mind/mechanisms/recall.d.ts +10 -6
  77. package/dist/src/mind/mechanisms/recall.js +185 -126
  78. package/dist/src/mind/mind.d.ts +154 -129
  79. package/dist/src/mind/mind.js +288 -258
  80. package/dist/src/mind/pipeline-mechanism.d.ts +124 -112
  81. package/dist/src/mind/pipeline-mechanism.js +172 -161
  82. package/dist/src/mind/pipeline.d.ts +14 -4
  83. package/dist/src/mind/pipeline.js +189 -125
  84. package/dist/src/mind/primitives.d.ts +32 -8
  85. package/dist/src/mind/primitives.js +117 -99
  86. package/dist/src/mind/rationale.d.ts +98 -86
  87. package/dist/src/mind/rationale.js +121 -113
  88. package/dist/src/mind/reasoning.d.ts +13 -2
  89. package/dist/src/mind/reasoning.js +166 -129
  90. package/dist/src/mind/recognition.d.ts +4 -1
  91. package/dist/src/mind/recognition.js +208 -183
  92. package/dist/src/mind/resonance.d.ts +22 -5
  93. package/dist/src/mind/resonance.js +0 -0
  94. package/dist/src/mind/trace.d.ts +25 -6
  95. package/dist/src/mind/trace.js +58 -50
  96. package/dist/src/mind/traverse.d.ts +58 -16
  97. package/dist/src/mind/traverse.js +357 -304
  98. package/dist/src/mind/types.d.ts +127 -120
  99. package/dist/src/mind/types.js +69 -60
  100. package/dist/src/rabitq-hnsw/src/database.d.ts +179 -177
  101. package/dist/src/rabitq-hnsw/src/database.js +359 -342
  102. package/dist/src/rabitq-hnsw/src/heap.d.ts +12 -12
  103. package/dist/src/rabitq-hnsw/src/heap.js +80 -75
  104. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +103 -103
  105. package/dist/src/rabitq-hnsw/src/hnsw.js +454 -428
  106. package/dist/src/rabitq-hnsw/src/index.d.ts +6 -1
  107. package/dist/src/rabitq-hnsw/src/prng.d.ts +10 -10
  108. package/dist/src/rabitq-hnsw/src/prng.js +27 -25
  109. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +69 -69
  110. package/dist/src/rabitq-hnsw/src/rabitq.js +262 -246
  111. package/dist/src/rabitq-hnsw/src/store.d.ts +132 -132
  112. package/dist/src/rabitq-hnsw/src/store.js +843 -752
  113. package/dist/src/rabitq-hnsw/test/hnsw.test.js +1054 -805
  114. package/dist/src/sema.d.ts +13 -9
  115. package/dist/src/sema.js +40 -26
  116. package/dist/src/store-sqlite.d.ts +166 -144
  117. package/dist/src/store-sqlite.js +660 -578
  118. package/dist/src/store.d.ts +630 -593
  119. package/dist/src/store.js +1581 -1440
  120. package/dist/src/vec.d.ts +9 -5
  121. package/dist/src/vec.js +73 -61
  122. package/package.json +2 -2
@@ -4,92 +4,95 @@ 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;
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,
9
22
  /** Recognition result (structural + canonical). */
10
- readonly rec: Recognition;
23
+ rec: Recognition,
11
24
  /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
12
- readonly computed: ComputedSpan[];
25
+ computed: ComputedSpan[],
13
26
  /** 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>>;
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>>;
71
74
  }
72
75
  /** The weave-local structural alignment, computed once and consumed by CAST
73
76
  * (and any future mechanism doing analogical transfer). */
74
77
  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;
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;
86
89
  }
87
90
  /** Span-shaped classification of one anchor — the structural information
88
91
  * extraction uses to decide whether a learned fact can serve as a template
89
92
  * for reading an analogous span out of the query. */
90
93
  export interface SkillInfo {
91
- contextBytes: Uint8Array;
92
- answerBytes: Uint8Array;
94
+ contextBytes: Uint8Array;
95
+ answerBytes: Uint8Array;
93
96
  }
94
97
  /** Raw result from a mechanism's `run()`. The pipeline computes the weight
95
98
  * from `moves` + `PASS * unaccounted(accounted)` — the mechanism does not
@@ -99,44 +102,53 @@ export interface SkillInfo {
99
102
  * computing `weigh(accounted, moves)`. This is for mechanisms whose cost
100
103
  * is derived externally (e.g. cover: the A*LD derivation's g-value). */
101
104
  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;
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;
112
115
  }
113
116
  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[]>;
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[]>;
142
154
  }
@@ -36,178 +36,189 @@ 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(ctx, 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(
49
+ ctx,
50
+ query,
49
51
  /** Recognition result (structural + canonical). */
50
- rec,
52
+ rec,
51
53
  /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
52
- computed,
54
+ computed,
53
55
  /** 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;
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);
61
78
  }
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);
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);
68
90
  }
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;
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);
89
134
  }
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);
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));
107
151
  }
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));
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;
114
179
  }
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;
180
+ const raw = alignGraded(ctx, query, ctxBytes, pre.rec.sites);
181
+ if (raw.length === 0) {
182
+ continue;
131
183
  }
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;
184
+ for (const r of raw) {
185
+ for (let i = r.qs; i < r.qe; i++) {
186
+ depth[i] += r.weight;
187
+ }
147
188
  }
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)
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) {
175
198
  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
- });
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
+ }
210
208
  }
209
+ }
210
+ if (qe - qs >= Math.min(quantum, ctxBytes.length)) {
211
+ free.push({ qs, qe, cs, weight });
212
+ }
213
+ }
214
+ if (free.length > 0) {
215
+ points.push({
216
+ anchor: cand.anchor,
217
+ vote: cand.vote,
218
+ ctx: ctxBytes,
219
+ runs: free,
220
+ });
211
221
  }
212
- return { points, depth };
222
+ }
223
+ return { points, depth };
213
224
  }