@hviana/sema 0.4.2 → 0.4.3

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 +136 -0
  42. package/dist/src/derive/src/deduction.js +159 -0
  43. package/dist/src/derive/src/index.d.ts +8 -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 +175 -0
  56. package/dist/src/geometry.js +823 -0
  57. package/dist/src/index.d.ts +17 -0
  58. package/dist/src/index.js +19 -0
  59. package/dist/src/ingest-cache.d.ts +41 -0
  60. package/dist/src/ingest-cache.js +165 -0
  61. package/dist/src/meter.d.ts +176 -0
  62. package/dist/src/meter.js +274 -0
  63. package/dist/src/mind/articulation.d.ts +6 -0
  64. package/dist/src/mind/articulation.js +99 -0
  65. package/dist/src/mind/attention.d.ts +414 -0
  66. package/dist/src/mind/attention.js +2082 -0
  67. package/dist/src/mind/bridge.d.ts +39 -0
  68. package/dist/src/mind/bridge.js +947 -0
  69. package/dist/src/mind/canonical.d.ts +34 -0
  70. package/dist/src/mind/canonical.js +93 -0
  71. package/dist/src/mind/graph-search.d.ts +294 -0
  72. package/dist/src/mind/graph-search.js +996 -0
  73. package/dist/src/mind/index.d.ts +9 -0
  74. package/dist/src/mind/index.js +5 -0
  75. package/dist/src/mind/junction.d.ts +137 -0
  76. package/dist/src/mind/junction.js +342 -0
  77. package/dist/src/mind/learning.d.ts +75 -0
  78. package/dist/src/mind/learning.js +270 -0
  79. package/dist/src/mind/match.d.ts +181 -0
  80. package/dist/src/mind/match.js +655 -0
  81. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  82. package/dist/src/mind/mechanisms/alu.js +36 -0
  83. package/dist/src/mind/mechanisms/cast.d.ts +89 -0
  84. package/dist/src/mind/mechanisms/cast.js +784 -0
  85. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  86. package/dist/src/mind/mechanisms/confluence.js +255 -0
  87. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  88. package/dist/src/mind/mechanisms/cover.js +227 -0
  89. package/dist/src/mind/mechanisms/extraction.d.ts +33 -0
  90. package/dist/src/mind/mechanisms/extraction.js +300 -0
  91. package/dist/src/mind/mechanisms/recall.d.ts +16 -0
  92. package/dist/src/mind/mechanisms/recall.js +364 -0
  93. package/dist/src/mind/mind.d.ts +337 -0
  94. package/dist/src/mind/mind.js +617 -0
  95. package/dist/src/mind/pipeline-mechanism.d.ts +172 -0
  96. package/dist/src/mind/pipeline-mechanism.js +465 -0
  97. package/dist/src/mind/pipeline.d.ts +49 -0
  98. package/dist/src/mind/pipeline.js +275 -0
  99. package/dist/src/mind/primitives.d.ts +66 -0
  100. package/dist/src/mind/primitives.js +306 -0
  101. package/dist/src/mind/rationale.d.ts +139 -0
  102. package/dist/src/mind/rationale.js +163 -0
  103. package/dist/src/mind/reasoning.d.ts +40 -0
  104. package/dist/src/mind/reasoning.js +280 -0
  105. package/dist/src/mind/recognition.d.ts +20 -0
  106. package/dist/src/mind/recognition.js +504 -0
  107. package/dist/src/mind/resonance.d.ts +23 -0
  108. package/dist/src/mind/resonance.js +0 -0
  109. package/dist/src/mind/trace.d.ts +15 -0
  110. package/dist/src/mind/trace.js +73 -0
  111. package/dist/src/mind/traverse.d.ts +126 -0
  112. package/dist/src/mind/traverse.js +650 -0
  113. package/dist/src/mind/types.d.ts +333 -0
  114. package/dist/src/mind/types.js +130 -0
  115. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  116. package/dist/src/rabitq-ivf/src/database.js +201 -0
  117. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  118. package/dist/src/rabitq-ivf/src/index.js +4 -0
  119. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  120. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  121. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  122. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  123. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  124. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  125. package/dist/src/sema.d.ts +31 -0
  126. package/dist/src/sema.js +63 -0
  127. package/dist/src/store-sqlite.d.ts +184 -0
  128. package/dist/src/store-sqlite.js +942 -0
  129. package/dist/src/store.d.ts +678 -0
  130. package/dist/src/store.js +1703 -0
  131. package/dist/src/vec.d.ts +31 -0
  132. package/dist/src/vec.js +109 -0
  133. package/package.json +1 -1
@@ -0,0 +1,172 @@
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 memo
41
+ * serves every mechanism that prices commonality — AND the consensus
42
+ * climb, which is the largest consumer and used to build its own. The
43
+ * ONE definition of its lifetime lives in traverse.ts
44
+ * ({@link sharedReachMemo}): session-scoped between writes and always cold
45
+ * under a trace. */
46
+ private _reach?;
47
+ get reachMemo(): Map<number, AncestorReach>;
48
+ /** Charge a lazily-shared analysis to its OWN phase rather than to the
49
+ * mechanism that happened to first-touch it. Without this the profile
50
+ * reads as "cast.floor costs 2 s" when what actually cost 2 s is the
51
+ * consensus climb — which cast merely paid for on everyone's behalf, and
52
+ * which every later consumer then got free. Attribution must follow the
53
+ * work, not the caller. */
54
+ private shared;
55
+ private _attention?;
56
+ /** The full consensus climb (roots + ranked anchors) — the query-level
57
+ * evidence CAST, confluence, extraction, recall's scaffolding tier, and
58
+ * fusion all share. Computed on first access; a query no mechanism
59
+ * climbs for (e.g. one an extension decided outright) never pays for it. */
60
+ attention(): Promise<AttentionRead>;
61
+ private _weave?;
62
+ /** Result of {@link alignGraded} for the first k ranked anchors —
63
+ * O(k · |query| · |ctx|). Consumed by CAST; reusable by any future
64
+ * mechanism doing analogical transfer. */
65
+ weave(): Promise<WeaveInfo>;
66
+ /** Span-shaped classification of one ranked anchor, memoised per anchor id
67
+ * so repeated calls (extraction's own early-exit scan, any future
68
+ * template-based mechanism) never redo the work. Deliberately NOT an
69
+ * eager all-anchors map: `skillExemplar` is the expensive part of
70
+ * extraction (capped fan-out reads plus an O(|ctx|) scan), and most
71
+ * queries are answered by the FIRST ranked anchor that qualifies — paying
72
+ * for every ranked anchor regardless of where the scan stops would turn
73
+ * an early-exit lookup into full O(k) work on every query. */
74
+ private _spanShaped;
75
+ spanShapedOf(anchor: number): Promise<SkillInfo | null>;
76
+ /** Every ranked anchor's classification at once, sharing the same
77
+ * per-anchor cache as {@link spanShapedOf} — for a mechanism that
78
+ * genuinely needs the full picture (not an early-exit scan). Mixing
79
+ * access patterns across mechanisms never duplicates work: whichever
80
+ * anchors an early-exit consumer already asked for are reused here, and
81
+ * whichever this computes first are reused by a later early-exit scan. */
82
+ spanShapedAll(): Promise<Map<number, SkillInfo | null>>;
83
+ }
84
+ /** The weave-local structural alignment, computed once and consumed by CAST
85
+ * (and any future mechanism doing analogical transfer). */
86
+ export interface WeaveInfo {
87
+ /** Per-anchor alignment: context bytes, vote weight, and graded runs. */
88
+ points: Array<{
89
+ anchor: number;
90
+ vote: number;
91
+ ctx: Uint8Array;
92
+ runs: GradedRun[];
93
+ }>;
94
+ /** Weighted depth at each query byte — sum of alignment weights.
95
+ * `depth[i]` is the total evidence that byte i is shared among the
96
+ * aligned structures. */
97
+ depth: Float64Array;
98
+ }
99
+ /** Span-shaped classification of one anchor — the structural information
100
+ * extraction uses to decide whether a learned fact can serve as a template
101
+ * for reading an analogous span out of the query. */
102
+ export interface SkillInfo {
103
+ contextBytes: Uint8Array;
104
+ answerBytes: Uint8Array;
105
+ }
106
+ /** Raw result from a mechanism's `run()`. The pipeline computes the weight
107
+ * from `moves` + `PASS * unaccounted(accounted)` — the mechanism does not
108
+ * know about the cost ladder.
109
+ *
110
+ * When `weight` is present, the pipeline uses it directly instead of
111
+ * computing `weigh(accounted, moves)`. This is for mechanisms whose cost
112
+ * is derived externally (e.g. cover: the A*LD derivation's g-value). */
113
+ export interface MechanismResult {
114
+ bytes: Uint8Array;
115
+ accounted: Array<[number, number]>;
116
+ moves: number;
117
+ used?: ReadonlySet<number>;
118
+ unexplained: string;
119
+ /** Explicit weight override. When absent, weight = moves + PASS·unaccounted. */
120
+ weight?: number;
121
+ /** Override the mechanism's default provenance for this result.
122
+ * When absent, the pipeline uses `mech.provenance`. */
123
+ provenance?: string;
124
+ /** This grounding is a COMPLETE trained answer — post-grounding must not
125
+ * extend it. Declared by the mechanism about its own result, exactly like
126
+ * `accounted`/`used`/`unexplained`; the decider honours the property and
127
+ * never asks which mechanism set it, so the market stays uniform.
128
+ *
129
+ * Set it only when the answer is a stored form's OWN continuation reached
130
+ * through an identity claim about the query — i.e. the query IS some
131
+ * trained context, so its continuation is the whole read-out and a further
132
+ * multi-hop pivot would chain PAST the fact that produced the answer.
133
+ * That is the same reasoning `reason`'s echo guard already applies to a
134
+ * query that resolves exactly (see reasoning.ts); this carries the claim
135
+ * for the mechanisms that establish the identity by another route.
136
+ *
137
+ * Observed without it: the correct "What is the process of
138
+ * photosynthesis?" grounding was pivoted forward four times, out of the
139
+ * fact that answered it and into an unrelated "Hello! How can I assist you
140
+ * today?" conversational turn. */
141
+ complete?: boolean;
142
+ }
143
+ export interface PipelineMechanism {
144
+ /** Stable identifier for trace/debug. */
145
+ readonly name: string;
146
+ /** Which provenance tag the pipeline attaches to this mechanism's answers. */
147
+ readonly provenance: string;
148
+ /** Parse authoritative spans BEFORE the grounding loop.
149
+ * Only needed by computational mechanisms (e.g. ALU). Results from ALL
150
+ * mechanisms that implement this are collected into `Precomputed.computed`
151
+ * before any `floor()` or `run()` is called. */
152
+ parse?(query: Uint8Array): Promise<ComputedSpan[]>;
153
+ /** Admissible lower bound on this mechanism's weight.
154
+ * Returns `null` when the mechanism structurally cannot fire.
155
+ *
156
+ * `worthRunning(cheapFloor)` reports whether the CURRENT incumbent
157
+ * (established by mechanisms that already ran this response, cover being
158
+ * first — see `defaultMechanisms`) could still be beaten by a floor no
159
+ * tighter than `cheapFloor`. THE INVESTMENT DISCIPLINE: before
160
+ * first-touching an expensive shared analysis (`pre.attention()`,
161
+ * `pre.weave()`, …), check `worthRunning(bound)` with this mechanism's
162
+ * cheapest possible bound — and when it fails, RETURN THE BOUND rather
163
+ * than null. The bound is still admissible (it never overstates cost),
164
+ * the pipeline's own check then prunes `run()` and records the truthful
165
+ * "cannot beat incumbent" trace note, and no analysis was computed just
166
+ * to be discarded. This is uniform: no mechanism asks what produced the
167
+ * incumbent — a computed extension result and an ordinary cheap cover
168
+ * prune the same way. */
169
+ floor(ctx: MindContext, query: Uint8Array, pre: Precomputed, worthRunning: (floor: number) => boolean): Promise<number | null>;
170
+ /** Produce candidate answers. */
171
+ run(ctx: MindContext, query: Uint8Array, pre: Precomputed): Promise<MechanismResult[]>;
172
+ }
@@ -0,0 +1,465 @@
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 { indexOf } from "../bytes.js";
16
+ import { dominates } from "../geometry.js";
17
+ import { windowIds } from "./canonical.js";
18
+ import { read, resolve } from "./primitives.js";
19
+ import { alignGraded, skillExemplar } from "./match.js";
20
+ import { climbAttentionAll } from "./attention.js";
21
+ import { sharedReachMemo } from "./traverse.js";
22
+ // ── Precomputed ──────────────────────────────────────────────────────────────
23
+ //
24
+ // Precomputed is a LAZY container for structural analyses of the query — the
25
+ // ONE place a response's shared evidence lives, for inter-mechanism exchange
26
+ // and for analyses future mechanisms will want. Eager fields (rec, computed,
27
+ // guide) are populated by the pipeline before the mechanism loop; everything
28
+ // expensive is a lazily-cached method that computes on first access. A
29
+ // mechanism that never asks for an analysis pays nothing for it; two
30
+ // mechanisms asking for the same analysis pay once.
31
+ //
32
+ // This design serves THREE purposes:
33
+ // 1. SHARING — when two mechanisms need the same analysis, it's computed once
34
+ // (even under trace, where the ctx-level memos are deliberately bypassed).
35
+ // 2. EXTENSIBILITY — a new analysis is one method in one file.
36
+ // 3. DECLARATIVE COST — a mechanism's floor() checks its cheap gates and the
37
+ // pipeline's `worthRunning` predicate BEFORE first-touching an expensive
38
+ // analysis, so lazy analyses are only ever computed for a mechanism that
39
+ // could still win.
40
+ export class Precomputed {
41
+ ctx;
42
+ query;
43
+ rec;
44
+ computed;
45
+ guide;
46
+ /** The response's evidence-breadth constant: how many ranked candidates the
47
+ * resonance probes, the weave alignment, and the climb all consider.
48
+ * Derived once from config; every consumer reads it here. */
49
+ k;
50
+ constructor(ctx, query,
51
+ /** Recognition result (structural + canonical). */
52
+ rec,
53
+ /** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
54
+ computed,
55
+ /** The query's gist — the response-wide disambiguation guide. */
56
+ guide) {
57
+ this.ctx = ctx;
58
+ this.query = query;
59
+ this.rec = rec;
60
+ this.computed = computed;
61
+ this.guide = guide;
62
+ this.k = ctx.cfg.recallQueryK * 2;
63
+ }
64
+ // ── Cheap lazy analyses ───────────────────────────────────────────────
65
+ _windows;
66
+ /** Content-addressed W-window identities for every position in the query
67
+ * (offset → node id). O(|query|) probes. */
68
+ get queryWindows() {
69
+ return this._windows ??= windowIds(this.ctx, this.query);
70
+ }
71
+ _resolved;
72
+ /** The node id of the query itself, or null when it is not a stored form.
73
+ * O(|query|) probes. */
74
+ get queryResolved() {
75
+ if (this._resolved === undefined) {
76
+ this._resolved = resolve(this.ctx, this.query);
77
+ }
78
+ return this._resolved;
79
+ }
80
+ _anchorWindows = new Map();
81
+ /** Content-addressed W-window identities of one anchor's own bytes
82
+ * (offset → node id), memoised per anchor. Confluence intersects these;
83
+ * any future identity-based mechanism reads the same cache. */
84
+ windowsOf(anchor) {
85
+ let w = this._anchorWindows.get(anchor);
86
+ if (w === undefined) {
87
+ w = windowIds(this.ctx, read(this.ctx, anchor));
88
+ this._anchorWindows.set(anchor, w);
89
+ }
90
+ return w;
91
+ }
92
+ /** Shared memo for {@link reachOf} (structural-IDF reads): a window's
93
+ * ancestor reach is a pure function of the read-only store, so one memo
94
+ * serves every mechanism that prices commonality — AND the consensus
95
+ * climb, which is the largest consumer and used to build its own. The
96
+ * ONE definition of its lifetime lives in traverse.ts
97
+ * ({@link sharedReachMemo}): session-scoped between writes and always cold
98
+ * under a trace. */
99
+ _reach;
100
+ get reachMemo() {
101
+ return this._reach ??= sharedReachMemo(this.ctx);
102
+ }
103
+ // ── Expensive lazy analyses ───────────────────────────────────────────
104
+ //
105
+ // Async, cached-by-promise: the first caller starts the computation, every
106
+ // later caller (any mechanism, any phase) awaits the same promise. A
107
+ // mechanism MUST check its cheap floor gates and the pipeline's
108
+ // `worthRunning` predicate before first-touching one of these.
109
+ /** Charge a lazily-shared analysis to its OWN phase rather than to the
110
+ * mechanism that happened to first-touch it. Without this the profile
111
+ * reads as "cast.floor costs 2 s" when what actually cost 2 s is the
112
+ * consensus climb — which cast merely paid for on everyone's behalf, and
113
+ * which every later consumer then got free. Attribution must follow the
114
+ * work, not the caller. */
115
+ shared(phase, fn) {
116
+ const meter = this.ctx.meter;
117
+ return meter ? meter.time(phase, fn) : fn();
118
+ }
119
+ _attention;
120
+ /** The full consensus climb (roots + ranked anchors) — the query-level
121
+ * evidence CAST, confluence, extraction, recall's scaffolding tier, and
122
+ * fusion all share. Computed on first access; a query no mechanism
123
+ * climbs for (e.g. one an extension decided outright) never pays for it. */
124
+ attention() {
125
+ return this._attention ??= this.shared("attention", () => climbAttentionAll(this.ctx, this.query, this.k));
126
+ }
127
+ _weave;
128
+ /** Result of {@link alignGraded} for the first k ranked anchors —
129
+ * O(k · |query| · |ctx|). Consumed by CAST; reusable by any future
130
+ * mechanism doing analogical transfer. */
131
+ weave() {
132
+ return this._weave ??= this.attention().then((climb) => this.shared("weave", async () => computeWeave(this.ctx, this.query, this, climb)));
133
+ }
134
+ /** Span-shaped classification of one ranked anchor, memoised per anchor id
135
+ * so repeated calls (extraction's own early-exit scan, any future
136
+ * template-based mechanism) never redo the work. Deliberately NOT an
137
+ * eager all-anchors map: `skillExemplar` is the expensive part of
138
+ * extraction (capped fan-out reads plus an O(|ctx|) scan), and most
139
+ * queries are answered by the FIRST ranked anchor that qualifies — paying
140
+ * for every ranked anchor regardless of where the scan stops would turn
141
+ * an early-exit lookup into full O(k) work on every query. */
142
+ _spanShaped = new Map();
143
+ spanShapedOf(anchor) {
144
+ let p = this._spanShaped.get(anchor);
145
+ if (p === undefined) {
146
+ p = this.shared("spanShaped", () => skillExemplar(this.ctx, anchor, this.guide));
147
+ this._spanShaped.set(anchor, p);
148
+ }
149
+ return p;
150
+ }
151
+ /** Every ranked anchor's classification at once, sharing the same
152
+ * per-anchor cache as {@link spanShapedOf} — for a mechanism that
153
+ * genuinely needs the full picture (not an early-exit scan). Mixing
154
+ * access patterns across mechanisms never duplicates work: whichever
155
+ * anchors an early-exit consumer already asked for are reused here, and
156
+ * whichever this computes first are reused by a later early-exit scan. */
157
+ async spanShapedAll() {
158
+ const { ranked } = await this.attention();
159
+ const out = new Map();
160
+ for (const cand of ranked) {
161
+ if (out.has(cand.anchor))
162
+ continue;
163
+ out.set(cand.anchor, await this.spanShapedOf(cand.anchor));
164
+ }
165
+ return out;
166
+ }
167
+ }
168
+ function computeWeave(ctx, query, pre, climb) {
169
+ const quantum = ctx.space.maxGroup;
170
+ const { ranked } = climb;
171
+ const rankedCapped = ranked.length > pre.k ? ranked.slice(0, pre.k) : ranked;
172
+ const depth = new Float64Array(query.length);
173
+ const points = [];
174
+ const byAnchor = new Map();
175
+ // WEAVE-SCALE anchors only: CAST transfers structure between things the
176
+ // QUERY weaves together — query-scale structures. A context an order of
177
+ // magnitude beyond the query is not woven BY the query (the query can at
178
+ // most quote a fragment of it, and fragment-level evidence is exactly what
179
+ // recognition and the cover already handle); CAST's own comparison gate
180
+ // demands `ctx.length ≤ query.length` before it fires, and its
181
+ // substitution seats sit within a quantum of a context's start. W is the
182
+ // perceptual quantum — the same scale multiplier the bridge's phrase-scale
183
+ // contract uses. The prefix-capped read makes an oversized anchor cost a
184
+ // bounded read instead of reconstructing (and then canonically
185
+ // recognising) a corpus-sized deposit: profiled on a 17.7M-node store,
186
+ // uncapped weaves spent 5–8s per query recognising conversation-length
187
+ // anchors that could never form a weave point.
188
+ const askerBytes = query.length -
189
+ ctx.answeredSpans.reduce((n, [start, end]) => n + end - start, 0);
190
+ const capBytes = askerBytes * quantum;
191
+ // EXCLUSIVITY IS ARBITRATED BY THE CLIMB'S VOTE ORDER, DELIBERATELY. A query
192
+ // byte can only be independent evidence for ONE point, so points are built in
193
+ // ranked order and each new point's runs are trimmed against every point
194
+ // already accepted; a point left with no run of a full quantum drops out of
195
+ // the weave.
196
+ //
197
+ // That reads like first-come-wins — a point that merely ranked higher taking
198
+ // a span from the point that actually explains it — and arbitrating by LOCAL
199
+ // evidence instead (ownership of each byte to the longest covering run, then
200
+ // the heavier weight, then rank) was implemented and MEASURED: test/29 went
201
+ // 9/2 to 7/4, and the new failures name the reason. CAST requires the weave
202
+ // to touch a COMMITTED point of attention ("2 aligned structure(s), but none
203
+ // is one of the climb's 1 committed root(s)"), and it was precisely the vote
204
+ // order that kept the committed root's own point alive in the weave. Local
205
+ // run length knows nothing about what the climb settled on, so it evicted the
206
+ // root's evidence and left CAST refusing on its own consistency check.
207
+ //
208
+ // So the vote order here is not an accident of construction — it is what
209
+ // holds the weave and the climb to the same conclusion. Weave-local
210
+ // measures decide what is FRAME inside the weave (see the frame gates in
211
+ // cast.ts); which structures are in the weave at all stays the climb's call.
212
+ //
213
+ // TWO PASSES. `depth` — how much of the weave agrees on each query byte, and
214
+ // therefore what counts as FRAME — must be the whole weave's, not "whatever
215
+ // has been processed so far": read in one pass it made a candidate's own
216
+ // frame reading depend on its rank, and the proposed-run gate below needs the
217
+ // real thing.
218
+ const cands = [];
219
+ const querySegments = [];
220
+ let segmentStart = 0;
221
+ for (const [start, end] of ctx.answeredSpans) {
222
+ if (segmentStart < start)
223
+ querySegments.push([segmentStart, start]);
224
+ segmentStart = Math.max(segmentStart, end);
225
+ }
226
+ if (segmentStart < query.length) {
227
+ querySegments.push([segmentStart, query.length]);
228
+ }
229
+ const weaveLength = querySegments.reduce((n, [s, e]) => n + e - s, 0);
230
+ const weaveQuery = new Uint8Array(weaveLength);
231
+ const weaveMap = [];
232
+ let compactStart = 0;
233
+ for (const [start, end] of querySegments) {
234
+ weaveQuery.set(query.subarray(start, end), compactStart);
235
+ weaveMap.push({
236
+ compactStart,
237
+ originalStart: start,
238
+ length: end - start,
239
+ });
240
+ compactStart += end - start;
241
+ }
242
+ const segmentOf = (start, end) => {
243
+ let lo = 0;
244
+ let hi = weaveMap.length;
245
+ while (lo < hi) {
246
+ const mid = (lo + hi) >>> 1;
247
+ if (weaveMap[mid].originalStart <= start)
248
+ lo = mid + 1;
249
+ else
250
+ hi = mid;
251
+ }
252
+ const part = lo > 0 ? weaveMap[lo - 1] : undefined;
253
+ return part && end <= part.originalStart + part.length ? part : undefined;
254
+ };
255
+ const weaveSites = pre.rec.sites.flatMap((s) => {
256
+ const part = segmentOf(s.start, s.end);
257
+ return part
258
+ ? [{
259
+ ...s,
260
+ start: part.compactStart + s.start - part.originalStart,
261
+ end: part.compactStart + s.end - part.originalStart,
262
+ }]
263
+ : [];
264
+ });
265
+ for (const cand of rankedCapped) {
266
+ const ctxBytes = read(ctx, cand.anchor, capBytes + 1);
267
+ if (ctxBytes.length === 0 || ctxBytes.length > capBytes)
268
+ continue;
269
+ // CAST compares structures stated by the asker. Completed assistant turns
270
+ // remain available to recognition and the climb as conversation context,
271
+ // but aligning every candidate across their full prose makes weave work
272
+ // grow with answer length and lets the engine analogise against its own
273
+ // previous output. The compact asker stream is aligned once (so candidate
274
+ // windows are not rebuilt per turn), then every run is split back across
275
+ // the original turn segments so no evidence crosses an omitted boundary.
276
+ const raw = alignGraded(ctx, weaveQuery, ctxBytes, weaveSites).flatMap((r) => {
277
+ let lo = 0;
278
+ let hi = weaveMap.length;
279
+ while (lo < hi) {
280
+ const mid = (lo + hi) >>> 1;
281
+ if (weaveMap[mid].compactStart <= r.qs)
282
+ lo = mid + 1;
283
+ else
284
+ hi = mid;
285
+ }
286
+ const out = [];
287
+ for (let pi = Math.max(0, lo - 1); pi < weaveMap.length; pi++) {
288
+ const part = weaveMap[pi];
289
+ if (part.compactStart >= r.qe)
290
+ break;
291
+ const partEnd = part.compactStart + part.length;
292
+ const start = Math.max(r.qs, part.compactStart);
293
+ const end = Math.min(r.qe, partEnd);
294
+ if (start >= end)
295
+ continue;
296
+ out.push({
297
+ ...r,
298
+ qs: part.originalStart + start - part.compactStart,
299
+ qe: part.originalStart + end - part.compactStart,
300
+ cs: r.cs + start - r.qs,
301
+ });
302
+ }
303
+ return out;
304
+ });
305
+ if (raw.length === 0)
306
+ continue;
307
+ for (const r of raw) {
308
+ for (let i = r.qs; i < r.qe; i++)
309
+ depth[i] += r.weight;
310
+ }
311
+ cands.push({ cand, ctxBytes, raw });
312
+ }
313
+ for (const { cand, ctxBytes, raw } of cands) {
314
+ const free = [];
315
+ for (const r of raw) {
316
+ let { qs, qe, cs, weight } = r;
317
+ for (const p of points) {
318
+ for (const o of p.runs) {
319
+ if (qs >= qe)
320
+ break;
321
+ if (o.qe <= qs || o.qs >= qe)
322
+ continue;
323
+ const left = Math.max(0, o.qs - qs);
324
+ const right = Math.max(0, qe - o.qe);
325
+ if (left >= right)
326
+ qe = qs + left;
327
+ else {
328
+ cs += qe - right - qs;
329
+ qs = qe - right;
330
+ }
331
+ }
332
+ }
333
+ if (qe - qs >= Math.min(quantum, ctxBytes.length)) {
334
+ free.push({ qs, qe, cs, weight });
335
+ }
336
+ }
337
+ if (free.length > 0) {
338
+ const pt = {
339
+ anchor: cand.anchor,
340
+ vote: cand.vote,
341
+ ctx: ctxBytes,
342
+ runs: free,
343
+ };
344
+ byAnchor.set(cand.anchor, pt);
345
+ points.push(pt);
346
+ }
347
+ }
348
+ // A byte is FRAME when more than half the weave shares it, and a SPAN is
349
+ // frame when more than half its bytes are — the same two-level
350
+ // half-dominance reading cast.ts's own frame gate uses, over the same
351
+ // `depth`. Read against the accepted POINTS (as cast.ts does), so it is
352
+ // only meaningful once phase 1 has run.
353
+ const framed = (from, to) => {
354
+ let n = 0;
355
+ for (let i = from; i < to; i++)
356
+ if (dominates(depth[i], points.length))
357
+ n++;
358
+ return dominates(n, to - from);
359
+ };
360
+ // PHASE 2 — THE CLIMB'S OWN CONCLUSION IS AN ALIGNMENT THE LITERAL MATCHER
361
+ // CANNOT SEE. `alignRuns` seeds on W-grams, so two forms differing by a
362
+ // single byte share no run at all: on `How is ice like steel?` against a
363
+ // store holding `Ice is cold`, the query's `ice` and the stored `Ice` agree
364
+ // on only `ce ` — three bytes, never seeded — so that structure entered the
365
+ // weave carrying nothing but the ` is ` scaffolding every exemplar shares,
366
+ // lost it to the first point that claimed it, and vanished. The climb had
367
+ // ALREADY identified it: its resonance elected `Ice is cold` from the query
368
+ // span `ce l` and `Steel is hard` from `stee`, two disjoint spans each naming
369
+ // its own structure, weighed through the region's contrastive margin and its
370
+ // IDF — gates the aligner has no equivalent of.
371
+ //
372
+ // So the climb PROPOSES the pairing (which structure, which query span) and
373
+ // bytes DECIDE its terms (§2.3). Three gates, each one measured:
374
+ //
375
+ // • it may only take query bytes NO literal run claimed. Run inline with
376
+ // phase 1 this did the opposite of "exact decides" — a higher-ranked
377
+ // candidate's proposal trimmed a lower-ranked candidate's byte-for-byte
378
+ // match out of existence (`he W`, proposed for `a nickname meaning the
379
+ // divine one`, cut the literal `The ` out of `The Starry Night was
380
+ // painted by Vincent van Gogh.` and CAST's redirection lost its
381
+ // dominant — test/29 C4). Hence a second pass, after every literal run
382
+ // is placed.
383
+ // • the literal agreement must DOMINATE the span. A climb vote is not by
384
+ // itself an alignment: on `The Persistence of Memory was painted by
385
+ // Salvador Dali.` the climb elects `The Starry Night…` from the span
386
+ // ` Dali.`, which shares barely a byte with it — the resonance was
387
+ // carried by the frame those exemplars share. Admitting it let CAST
388
+ // weave points out of pure scaffolding and out-account the correct
389
+ // extraction (test/00, test/24). Where the proposal is real the
390
+ // agreement is overwhelming: both C1 spans agree on three of four bytes.
391
+ // • and the span must not be FRAME. Literal dominance alone is too weak
392
+ // at this scale — a 4-byte span agrees three-of-four with half the
393
+ // corpus by accident (`he W` against `a nickname meaning the divine
394
+ // one`). Frame is the weave-local measure of exactly that.
395
+ const claimed = new Uint8Array(query.length);
396
+ for (const p of points) {
397
+ for (const r of p.runs)
398
+ claimed.fill(1, r.qs, r.qe);
399
+ }
400
+ for (const { cand, ctxBytes } of cands) {
401
+ if (cand.end > cand.start) {
402
+ let qs = cand.start;
403
+ let qe = cand.end;
404
+ while (qs < qe && claimed[qs])
405
+ qs++;
406
+ while (qe > qs && claimed[qe - 1])
407
+ qe--;
408
+ let clear = true;
409
+ for (let i = qs; i < qe; i++)
410
+ if (claimed[i])
411
+ clear = false;
412
+ if (clear && qe - qs >= Math.min(quantum, ctxBytes.length)) {
413
+ // The gate only asks whether the agreement DOMINATES the span, so
414
+ // search DOWNWARD from the whole span and stop at the first hit: the
415
+ // first length found is both the longest agreement and, by
416
+ // construction, already past the dominance bar. At most O(W²) bounded
417
+ // substring probes — a span is one segment (≤ 2W) — where a full
418
+ // longest-common-substring scan would be O(|span|² · |ctx|) against a
419
+ // context that may be W× the query.
420
+ const span = query.subarray(qs, qe);
421
+ const bar = Math.floor(span.length / 2) + 1; // dominates(bar, length)
422
+ let bestLen = 0;
423
+ let bestCs = 0;
424
+ for (let len = span.length; len >= bar && bestLen === 0; len--) {
425
+ for (let off = 0; off + len <= span.length; off++) {
426
+ const at = indexOf(ctxBytes, span.subarray(off, off + len), 0);
427
+ if (at < 0)
428
+ continue;
429
+ bestLen = len;
430
+ // Where the span's FIRST byte lands, so `cs` means the same thing
431
+ // it does for a literal run: the context offset the run starts at.
432
+ bestCs = Math.max(0, at - off);
433
+ break;
434
+ }
435
+ }
436
+ if (bestLen > 0 && !framed(qs, qe)) {
437
+ const run = {
438
+ qs,
439
+ qe,
440
+ cs: bestCs,
441
+ weight: bestLen / (qe - qs),
442
+ proposed: true,
443
+ };
444
+ claimed.fill(1, qs, qe);
445
+ const pt = byAnchor.get(cand.anchor);
446
+ if (!pt) {
447
+ const made = {
448
+ anchor: cand.anchor,
449
+ vote: cand.vote,
450
+ ctx: ctxBytes,
451
+ runs: [run],
452
+ };
453
+ byAnchor.set(cand.anchor, made);
454
+ points.push(made);
455
+ }
456
+ else {
457
+ pt.runs.push(run);
458
+ pt.runs.sort((x, y) => x.qs - y.qs);
459
+ }
460
+ }
461
+ }
462
+ }
463
+ }
464
+ return { points, depth };
465
+ }