@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.
- package/dist/example/demo.js +25 -25
- package/dist/example/train_base.d.ts +21 -19
- package/dist/example/train_base.js +1952 -1684
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +27 -24
- package/dist/src/alu/src/alu.d.ts +194 -169
- package/dist/src/alu/src/alu.js +414 -374
- package/dist/src/alu/src/expr.d.ts +43 -36
- package/dist/src/alu/src/expr.js +278 -252
- package/dist/src/alu/src/index.d.ts +98 -9
- package/dist/src/alu/src/index.js +63 -9
- package/dist/src/alu/src/kernel-arith.d.ts +5 -1
- package/dist/src/alu/src/kernel-arith.js +285 -195
- package/dist/src/alu/src/kernel-bits.d.ts +5 -1
- package/dist/src/alu/src/kernel-bits.js +115 -81
- package/dist/src/alu/src/kernel-logic.js +63 -33
- package/dist/src/alu/src/kernel-nd.js +195 -155
- package/dist/src/alu/src/kernel-numeric.d.ts +80 -16
- package/dist/src/alu/src/kernel-numeric.js +331 -259
- package/dist/src/alu/src/operation.d.ts +132 -112
- package/dist/src/alu/src/operation.js +163 -149
- package/dist/src/alu/src/parser.d.ts +175 -168
- package/dist/src/alu/src/parser.js +421 -382
- package/dist/src/alu/src/resonance.d.ts +25 -16
- package/dist/src/alu/src/resonance.js +56 -49
- package/dist/src/alu/src/text.d.ts +7 -3
- package/dist/src/alu/src/text.js +37 -31
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +160 -150
- package/dist/src/alu/test/alu.test.js +656 -543
- package/dist/src/bytes.d.ts +5 -1
- package/dist/src/bytes.js +40 -33
- package/dist/src/config.d.ts +102 -102
- package/dist/src/config.js +83 -83
- package/dist/src/derive/src/deduction.d.ts +62 -60
- package/dist/src/derive/src/deduction.js +109 -103
- package/dist/src/derive/src/index.d.ts +7 -1
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +61 -57
- package/dist/src/derive/src/rewrite.d.ts +18 -15
- package/dist/src/derive/src/rewrite.js +67 -59
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +179 -175
- package/dist/src/derive/test/derive.test.js +100 -96
- package/dist/src/extension.d.ts +16 -13
- package/dist/src/geometry.d.ts +42 -17
- package/dist/src/geometry.js +266 -235
- package/dist/src/index.d.ts +17 -2
- package/dist/src/index.js +7 -1
- package/dist/src/ingest-cache.d.ts +30 -25
- package/dist/src/ingest-cache.js +127 -108
- package/dist/src/mind/articulation.d.ts +5 -1
- package/dist/src/mind/articulation.js +112 -77
- package/dist/src/mind/attention.d.ts +108 -40
- package/dist/src/mind/attention.js +871 -749
- package/dist/src/mind/canonical.d.ts +19 -4
- package/dist/src/mind/canonical.js +31 -27
- package/dist/src/mind/graph-search.d.ts +225 -201
- package/dist/src/mind/graph-search.js +821 -742
- package/dist/src/mind/index.d.ts +10 -2
- package/dist/src/mind/junction.d.ts +58 -31
- package/dist/src/mind/junction.js +237 -172
- package/dist/src/mind/learning.d.ts +52 -16
- package/dist/src/mind/learning.js +165 -143
- package/dist/src/mind/match.d.ts +69 -20
- package/dist/src/mind/match.js +318 -259
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +13 -9
- package/dist/src/mind/mechanisms/cast.js +456 -363
- package/dist/src/mind/mechanisms/confluence.d.ts +12 -8
- package/dist/src/mind/mechanisms/confluence.js +183 -152
- package/dist/src/mind/mechanisms/cover.d.ts +8 -2
- package/dist/src/mind/mechanisms/cover.js +210 -148
- package/dist/src/mind/mechanisms/extraction.d.ts +34 -8
- package/dist/src/mind/mechanisms/extraction.js +288 -234
- package/dist/src/mind/mechanisms/recall.d.ts +10 -6
- package/dist/src/mind/mechanisms/recall.js +185 -126
- package/dist/src/mind/mind.d.ts +154 -129
- package/dist/src/mind/mind.js +288 -258
- package/dist/src/mind/pipeline-mechanism.d.ts +124 -112
- package/dist/src/mind/pipeline-mechanism.js +172 -161
- package/dist/src/mind/pipeline.d.ts +14 -4
- package/dist/src/mind/pipeline.js +189 -125
- package/dist/src/mind/primitives.d.ts +32 -8
- package/dist/src/mind/primitives.js +117 -99
- package/dist/src/mind/rationale.d.ts +98 -86
- package/dist/src/mind/rationale.js +121 -113
- package/dist/src/mind/reasoning.d.ts +13 -2
- package/dist/src/mind/reasoning.js +166 -129
- package/dist/src/mind/recognition.d.ts +4 -1
- package/dist/src/mind/recognition.js +208 -183
- package/dist/src/mind/resonance.d.ts +22 -5
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +25 -6
- package/dist/src/mind/trace.js +58 -50
- package/dist/src/mind/traverse.d.ts +58 -16
- package/dist/src/mind/traverse.js +357 -304
- package/dist/src/mind/types.d.ts +127 -120
- package/dist/src/mind/types.js +69 -60
- package/dist/src/rabitq-hnsw/src/database.d.ts +179 -177
- package/dist/src/rabitq-hnsw/src/database.js +359 -342
- package/dist/src/rabitq-hnsw/src/heap.d.ts +12 -12
- package/dist/src/rabitq-hnsw/src/heap.js +80 -75
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +103 -103
- package/dist/src/rabitq-hnsw/src/hnsw.js +454 -428
- package/dist/src/rabitq-hnsw/src/index.d.ts +6 -1
- package/dist/src/rabitq-hnsw/src/prng.d.ts +10 -10
- package/dist/src/rabitq-hnsw/src/prng.js +27 -25
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +69 -69
- package/dist/src/rabitq-hnsw/src/rabitq.js +262 -246
- package/dist/src/rabitq-hnsw/src/store.d.ts +132 -132
- package/dist/src/rabitq-hnsw/src/store.js +843 -752
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +1054 -805
- package/dist/src/sema.d.ts +13 -9
- package/dist/src/sema.js +40 -26
- package/dist/src/store-sqlite.d.ts +166 -144
- package/dist/src/store-sqlite.js +660 -578
- package/dist/src/store.d.ts +630 -593
- package/dist/src/store.js +1581 -1440
- package/dist/src/vec.d.ts +9 -5
- package/dist/src/vec.js +73 -61
- 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
|
-
|
|
8
|
-
|
|
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
|
-
|
|
23
|
+
rec: Recognition,
|
|
11
24
|
/** Computed spans from mechanisms that implement `parse()` (e.g. ALU). */
|
|
12
|
-
|
|
25
|
+
computed: ComputedSpan[],
|
|
13
26
|
/** The query's gist — the response-wide disambiguation guide. */
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
92
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
-
|
|
222
|
+
}
|
|
223
|
+
return { points, depth };
|
|
213
224
|
}
|