@hviana/sema 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +6 -5
- package/CITATION.cff +49 -0
- package/HOW_IT_WORKS.md +11 -12
- package/README.md +7 -5
- package/dist/example/demo.js +25 -25
- package/dist/example/train_base.d.ts +19 -21
- package/dist/example/train_base.js +1700 -1952
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +24 -27
- package/dist/src/alu/src/alu.d.ts +169 -194
- package/dist/src/alu/src/alu.js +374 -414
- package/dist/src/alu/src/expr.d.ts +36 -43
- package/dist/src/alu/src/expr.js +252 -278
- package/dist/src/alu/src/index.d.ts +9 -98
- package/dist/src/alu/src/index.js +10 -64
- package/dist/src/alu/src/kernel-arith.d.ts +1 -5
- package/dist/src/alu/src/kernel-arith.js +195 -285
- package/dist/src/alu/src/kernel-bits.d.ts +1 -5
- package/dist/src/alu/src/kernel-bits.js +81 -115
- package/dist/src/alu/src/kernel-logic.js +33 -63
- package/dist/src/alu/src/kernel-nd.js +155 -195
- package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
- package/dist/src/alu/src/kernel-numeric.js +259 -331
- package/dist/src/alu/src/operation.d.ts +112 -132
- package/dist/src/alu/src/operation.js +149 -163
- package/dist/src/alu/src/parser.d.ts +168 -175
- package/dist/src/alu/src/parser.js +382 -421
- package/dist/src/alu/src/resonance.d.ts +16 -25
- package/dist/src/alu/src/resonance.js +49 -56
- package/dist/src/alu/src/text.d.ts +3 -7
- package/dist/src/alu/src/text.js +31 -37
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +150 -160
- package/dist/src/alu/test/alu.test.js +543 -656
- package/dist/src/bytes.d.ts +1 -5
- package/dist/src/bytes.js +33 -40
- package/dist/src/config.d.ts +99 -102
- package/dist/src/config.js +78 -83
- package/dist/src/derive/src/deduction.d.ts +60 -62
- package/dist/src/derive/src/deduction.js +103 -109
- package/dist/src/derive/src/index.d.ts +1 -7
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +57 -61
- package/dist/src/derive/src/rewrite.d.ts +15 -18
- package/dist/src/derive/src/rewrite.js +59 -67
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +175 -179
- package/dist/src/derive/test/derive.test.js +96 -100
- package/dist/src/extension.d.ts +13 -16
- package/dist/src/geometry.d.ts +29 -43
- package/dist/src/geometry.js +324 -268
- package/dist/src/index.d.ts +2 -17
- package/dist/src/index.js +2 -12
- package/dist/src/ingest-cache.d.ts +25 -30
- package/dist/src/ingest-cache.js +108 -127
- package/dist/src/mind/articulation.d.ts +1 -5
- package/dist/src/mind/articulation.js +77 -112
- package/dist/src/mind/attention.d.ts +40 -108
- package/dist/src/mind/attention.js +754 -872
- package/dist/src/mind/canonical.d.ts +4 -19
- package/dist/src/mind/canonical.js +27 -31
- package/dist/src/mind/graph-search.d.ts +201 -225
- package/dist/src/mind/graph-search.js +742 -821
- package/dist/src/mind/index.d.ts +2 -10
- package/dist/src/mind/junction.d.ts +31 -58
- package/dist/src/mind/junction.js +172 -237
- package/dist/src/mind/learning.d.ts +16 -52
- package/dist/src/mind/learning.js +143 -165
- package/dist/src/mind/match.d.ts +20 -69
- package/dist/src/mind/match.js +259 -318
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +9 -13
- package/dist/src/mind/mechanisms/cast.js +363 -456
- package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
- package/dist/src/mind/mechanisms/confluence.js +152 -183
- package/dist/src/mind/mechanisms/cover.d.ts +2 -8
- package/dist/src/mind/mechanisms/cover.js +148 -210
- package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
- package/dist/src/mind/mechanisms/extraction.js +234 -288
- package/dist/src/mind/mechanisms/recall.d.ts +6 -10
- package/dist/src/mind/mechanisms/recall.js +141 -185
- package/dist/src/mind/mind.d.ts +194 -155
- package/dist/src/mind/mind.js +392 -289
- package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
- package/dist/src/mind/pipeline-mechanism.js +161 -172
- package/dist/src/mind/pipeline.d.ts +4 -14
- package/dist/src/mind/pipeline.js +125 -189
- package/dist/src/mind/primitives.d.ts +20 -33
- package/dist/src/mind/primitives.js +124 -120
- package/dist/src/mind/rationale.d.ts +86 -98
- package/dist/src/mind/rationale.js +113 -121
- package/dist/src/mind/reasoning.d.ts +2 -13
- package/dist/src/mind/reasoning.js +129 -166
- package/dist/src/mind/recognition.d.ts +1 -4
- package/dist/src/mind/recognition.js +198 -210
- package/dist/src/mind/resonance.d.ts +5 -22
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +6 -25
- package/dist/src/mind/trace.js +50 -58
- package/dist/src/mind/traverse.d.ts +29 -58
- package/dist/src/mind/traverse.js +344 -356
- package/dist/src/mind/types.d.ts +122 -127
- package/dist/src/mind/types.js +60 -69
- package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
- package/dist/src/rabitq-ivf/src/database.js +201 -0
- package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
- package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
- package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
- package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
- package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
- package/dist/src/rabitq-ivf/src/prng.js +36 -0
- package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
- package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
- package/dist/src/sema.d.ts +9 -13
- package/dist/src/sema.js +26 -40
- package/dist/src/store-sqlite.d.ts +171 -166
- package/dist/src/store-sqlite.js +768 -662
- package/dist/src/store.d.ts +597 -630
- package/dist/src/store.js +1434 -1581
- package/dist/src/vec.d.ts +5 -9
- package/dist/src/vec.js +61 -73
- package/example/train_base.ts +33 -15
- package/index.html +65 -0
- package/package.json +1 -1
- package/src/alu/README.md +1 -1
- package/src/alu/src/index.ts +1 -1
- package/src/config.ts +19 -27
- package/src/geometry.ts +93 -1
- package/src/index.ts +6 -11
- package/src/mind/attention.ts +11 -6
- package/src/mind/mechanisms/recall.ts +29 -13
- package/src/mind/mind.ts +250 -19
- package/src/mind/primitives.ts +43 -6
- package/src/mind/recognition.ts +26 -8
- package/src/mind/traverse.ts +47 -0
- package/src/mind/types.ts +20 -21
- package/src/rabitq-ivf/README.md +56 -0
- package/src/rabitq-ivf/src/database.ts +276 -0
- package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
- package/src/rabitq-ivf/src/ivf.ts +1330 -0
- package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
- package/src/store-sqlite.ts +204 -9
- package/src/store.ts +27 -34
- package/test/08-storage.test.mjs +3 -3
- package/test/13-conversation.test.mjs +190 -20
- package/test/14-scaling.test.mjs +2 -2
- package/test/35-ivf.test.mjs +263 -0
- package/test/36-bloom.test.mjs +123 -0
- package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
- package/dist/src/rabitq-hnsw/src/database.js +0 -405
- package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
- package/dist/src/rabitq-hnsw/src/heap.js +0 -94
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
- package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
- package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
- package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
- package/dist/src/rabitq-hnsw/src/prng.js +0 -38
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
- package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
- package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
- package/dist/src/rabitq-hnsw/src/store.js +0 -916
- package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
- package/src/rabitq-hnsw/README.md +0 -303
- package/src/rabitq-hnsw/src/database.ts +0 -492
- package/src/rabitq-hnsw/src/heap.ts +0 -90
- package/src/rabitq-hnsw/src/hnsw.ts +0 -514
- package/src/rabitq-hnsw/src/store.ts +0 -994
- package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
- /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
|
@@ -2,60 +2,59 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Address — bytes → node (perceive, foldTree, resolve)
|
|
4
4
|
// Read — node → bytes (read)
|
|
5
|
-
import {
|
|
6
|
-
bytesToTree,
|
|
7
|
-
bytesToTreePyramid,
|
|
8
|
-
gridToTree,
|
|
9
|
-
hilbertBytes,
|
|
10
|
-
stackGrids,
|
|
11
|
-
} from "../geometry.js";
|
|
5
|
+
import { bytesToTree, bytesToTreePyramid, gridToTree, hilbertBytes, stackGrids, } from "../geometry.js";
|
|
12
6
|
import { ALL } from "./types.js";
|
|
13
7
|
// ── Address: bytes → node ──────────────────────────────────────────────
|
|
14
8
|
/** The content key of a byte span — one latin1 char per byte, an exact,
|
|
15
9
|
* collision-free encoding. Spans on the perception path are query-scale
|
|
16
10
|
* (windows, regions, candidate spans), so key construction is far cheaper
|
|
17
11
|
* than the river fold it deduplicates. */
|
|
18
|
-
function latin1Key(bytes) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
export function latin1Key(bytes) {
|
|
13
|
+
// Batched String.fromCharCode — avoids the O(n²) cost of repeated += on
|
|
14
|
+
// potentially-large query spans, and stays well under the ~65536 arg limit.
|
|
15
|
+
const n = bytes.length;
|
|
16
|
+
let s = "";
|
|
17
|
+
for (let i = 0; i < n; i += 4096) {
|
|
18
|
+
s += String.fromCharCode(...bytes.subarray(i, Math.min(i + 4096, n)));
|
|
19
|
+
}
|
|
20
|
+
return s;
|
|
27
21
|
}
|
|
28
22
|
/** Perceive input into a content-defined tree (the river fold).
|
|
29
|
-
* Deterministic — identical bytes always produce an identical tree.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
23
|
+
* Deterministic — identical bytes always produce an identical tree.
|
|
24
|
+
*
|
|
25
|
+
* `boundaries` is an optional sorted list of proper byte offsets where the
|
|
26
|
+
* fold must split so that each prefix segment folds identically to how it
|
|
27
|
+
* folded when it was learned (§10.3 stable-prefix contract). Only the
|
|
28
|
+
* CALLER — who assembled the multi-turn context — knows where those
|
|
29
|
+
* boundaries are; the geometry never guesses them from the bytes. */
|
|
30
|
+
export function perceive(ctx, input, leafAt, lookup, boundaries) {
|
|
31
|
+
if (typeof input === "string" || input instanceof Uint8Array) {
|
|
32
|
+
const bytes = typeof input === "string"
|
|
33
|
+
? new TextEncoder().encode(input)
|
|
34
|
+
: input;
|
|
35
|
+
if (leafAt === undefined && lookup === undefined) {
|
|
36
|
+
// Per-response memo (see MindContext.perceiveMemo): only the plain
|
|
37
|
+
// inference shape — raw bytes, no store capabilities — is memoised,
|
|
38
|
+
// keyed by CONTENT so byte-identical spans in fresh arrays still hit.
|
|
39
|
+
// The tree is shared by reference; Sema nodes are never mutated.
|
|
40
|
+
const memo = ctx.perceiveMemo;
|
|
41
|
+
if (memo) {
|
|
42
|
+
const key = latin1Key(bytes);
|
|
43
|
+
const hit = memo.get(key);
|
|
44
|
+
if (hit !== undefined)
|
|
45
|
+
return hit;
|
|
46
|
+
const tree = bytesToTree(ctx.space, ctx.alphabet, bytes, undefined, undefined, boundaries);
|
|
47
|
+
memo.set(key, tree);
|
|
48
|
+
return tree;
|
|
49
|
+
}
|
|
50
|
+
return bytesToTree(ctx.space, ctx.alphabet, bytes, undefined, undefined, boundaries);
|
|
46
51
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return bytesToTree(ctx.space, ctx.alphabet, bytes);
|
|
52
|
+
return bytesToTree(ctx.space, ctx.alphabet, bytes, leafAt, lookup);
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(input)) {
|
|
55
|
+
return gridToTree(ctx.space, ctx.alphabet, stackGrids(input));
|
|
52
56
|
}
|
|
53
|
-
return
|
|
54
|
-
}
|
|
55
|
-
if (Array.isArray(input)) {
|
|
56
|
-
return gridToTree(ctx.space, ctx.alphabet, stackGrids(input));
|
|
57
|
-
}
|
|
58
|
-
return gridToTree(ctx.space, ctx.alphabet, input);
|
|
57
|
+
return gridToTree(ctx.space, ctx.alphabet, input);
|
|
59
58
|
}
|
|
60
59
|
/** The DEPOSIT-shaped perceive: the PLAIN fold (bit-identical to inference
|
|
61
60
|
* perception — that structural train/inference agreement is load-bearing
|
|
@@ -66,53 +65,45 @@ export function perceive(ctx, input, leafAt, lookup) {
|
|
|
66
65
|
* each level — O(turn) instead of O(context) per turn. Purely a cache:
|
|
67
66
|
* the produced tree never depends on cache state. */
|
|
68
67
|
export function perceiveDeposit(ctx, bytes) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
let prev;
|
|
69
|
+
// Longest cached PROPER prefix first.
|
|
70
|
+
const lens = [...ctx._depositLens]
|
|
71
|
+
.filter((L) => L >= 2 && L < bytes.length)
|
|
72
|
+
.sort((a, b) => b - a);
|
|
73
|
+
for (const L of lens) {
|
|
74
|
+
const hit = ctx._depositTrees.get(latin1Key(bytes.subarray(0, L)));
|
|
75
|
+
if (hit !== undefined) {
|
|
76
|
+
prev = hit;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (ctx._depositLens.size > 64) {
|
|
92
|
-
ctx._depositLens.clear();
|
|
93
|
-
ctx._depositTrees.clear();
|
|
80
|
+
const { tree, pyramid } = bytesToTreePyramid(ctx.space, ctx.alphabet, bytes, prev);
|
|
81
|
+
if (bytes.length >= 2) {
|
|
82
|
+
// The lengths set drifts as the map evicts; past the probe budget the
|
|
83
|
+
// drift itself becomes the cost (each stale length is an O(len) key
|
|
84
|
+
// build), so both reset together — losing only warm-up on live chains.
|
|
85
|
+
if (ctx._depositLens.size > 64) {
|
|
86
|
+
ctx._depositLens.clear();
|
|
87
|
+
ctx._depositTrees.clear();
|
|
88
|
+
}
|
|
89
|
+
ctx._depositTrees.set(latin1Key(bytes), pyramid);
|
|
90
|
+
ctx._depositLens.add(bytes.length);
|
|
94
91
|
}
|
|
95
|
-
|
|
96
|
-
ctx._depositLens.add(bytes.length);
|
|
97
|
-
}
|
|
98
|
-
return tree;
|
|
92
|
+
return tree;
|
|
99
93
|
}
|
|
100
94
|
/** The raw bytes of an input — modality-neutral conversion. */
|
|
101
95
|
export function inputBytes(ctx, input) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return hilbertBytes(stackGrids(input));
|
|
110
|
-
}
|
|
111
|
-
return hilbertBytes(input);
|
|
96
|
+
if (typeof input === "string")
|
|
97
|
+
return new TextEncoder().encode(input);
|
|
98
|
+
if (input instanceof Uint8Array)
|
|
99
|
+
return input;
|
|
100
|
+
if (Array.isArray(input))
|
|
101
|
+
return hilbertBytes(stackGrids(input));
|
|
102
|
+
return hilbertBytes(input);
|
|
112
103
|
}
|
|
113
104
|
/** Convenience: the gist vector of a byte span. */
|
|
114
105
|
export function gistOf(ctx, bytes) {
|
|
115
|
-
|
|
106
|
+
return perceive(ctx, bytes).v;
|
|
116
107
|
}
|
|
117
108
|
/** Fold a perceived tree bottom-up against the store's content-addressed maps:
|
|
118
109
|
* every leaf is named by findLeaf, every branch by findBranch over its kids'
|
|
@@ -120,37 +111,51 @@ export function gistOf(ctx, bytes) {
|
|
|
120
111
|
* node with its byte span and resolved id. Returns the node's byte end and
|
|
121
112
|
* resolved id. */
|
|
122
113
|
export function foldTree(ctx, n, start, visit) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const kids = [];
|
|
133
|
-
for (const k of n.kids) {
|
|
134
|
-
const r = foldTree(ctx, k, pos, visit);
|
|
135
|
-
if (r.node === null) {
|
|
136
|
-
known = false;
|
|
137
|
-
} else if (known) {
|
|
138
|
-
kids.push(r.node);
|
|
114
|
+
// Fast path: subtree already resolved (from a previous conversation turn
|
|
115
|
+
// or an earlier recognition pass). The pyramid reuses prefix subtrees as
|
|
116
|
+
// identical Sema objects, so this cache turns foldTree into O(suffix)
|
|
117
|
+
// instead of O(context) for multi-turn recognition.
|
|
118
|
+
const cached = ctx._resolvedSubtrees?.get(n);
|
|
119
|
+
if (cached !== undefined) {
|
|
120
|
+
const end = start + cached.len;
|
|
121
|
+
visit?.(n, start, end, cached.id);
|
|
122
|
+
return { end, node: cached.id };
|
|
139
123
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
124
|
+
if (n.kids === null) {
|
|
125
|
+
const b = n.leaf ?? new Uint8Array(0);
|
|
126
|
+
const end = start + b.length;
|
|
127
|
+
const node = ctx.store.findLeaf(b);
|
|
128
|
+
visit?.(n, start, end, node);
|
|
129
|
+
if (node !== null && ctx._resolvedSubtrees) {
|
|
130
|
+
ctx._resolvedSubtrees.set(n, { id: node, len: b.length });
|
|
131
|
+
}
|
|
132
|
+
return { end, node };
|
|
133
|
+
}
|
|
134
|
+
let pos = start;
|
|
135
|
+
let known = true;
|
|
136
|
+
const kids = [];
|
|
137
|
+
for (const k of n.kids) {
|
|
138
|
+
const r = foldTree(ctx, k, pos, visit);
|
|
139
|
+
if (r.node === null)
|
|
140
|
+
known = false;
|
|
141
|
+
else if (known)
|
|
142
|
+
kids.push(r.node);
|
|
143
|
+
pos = r.end;
|
|
144
|
+
}
|
|
145
|
+
const node = known ? ctx.store.findBranch(kids) : null;
|
|
146
|
+
visit?.(n, start, pos, node);
|
|
147
|
+
if (node !== null && ctx._resolvedSubtrees) {
|
|
148
|
+
ctx._resolvedSubtrees.set(n, { id: node, len: pos - start });
|
|
149
|
+
}
|
|
150
|
+
return { end: pos, node };
|
|
145
151
|
}
|
|
146
152
|
/** The canonical node id of a byte span: perceive it in isolation — the way
|
|
147
153
|
* training did — and recover its root bottom-up. Returns null if any part is
|
|
148
154
|
* unknown. */
|
|
149
155
|
export function resolve(ctx, bytes) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return foldTree(ctx, perceive(ctx, bytes), 0).node;
|
|
156
|
+
if (bytes.length === 0)
|
|
157
|
+
return null;
|
|
158
|
+
return foldTree(ctx, perceive(ctx, bytes), 0).node;
|
|
154
159
|
}
|
|
155
160
|
/** Walk a perceived tree in POST-ORDER with byte offsets — children before
|
|
156
161
|
* their parent, `visit(node, start, end)` for every node including leaves.
|
|
@@ -161,20 +166,19 @@ export function resolve(ctx, bytes) {
|
|
|
161
166
|
* walk: its flush semantics need PRE-order decisions at leaf-parents, which
|
|
162
167
|
* a post-order visitor cannot express.) */
|
|
163
168
|
export function walkTree(n, start, visit) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return pos;
|
|
169
|
+
if (n.kids === null) {
|
|
170
|
+
const end = start + (n.leaf?.length ?? 0);
|
|
171
|
+
visit(n, start, end);
|
|
172
|
+
return end;
|
|
173
|
+
}
|
|
174
|
+
let pos = start;
|
|
175
|
+
for (const k of n.kids)
|
|
176
|
+
pos = walkTree(k, pos, visit);
|
|
177
|
+
visit(n, start, pos);
|
|
178
|
+
return pos;
|
|
175
179
|
}
|
|
176
180
|
// ── Read: node → bytes ──────────────────────────────────────────────────
|
|
177
181
|
/** Reconstruct a node's byte content from the DAG, up to `maxLen` bytes. */
|
|
178
182
|
export function read(ctx, id, maxLen = ALL) {
|
|
179
|
-
|
|
183
|
+
return ctx.store.bytesPrefix(id, maxLen);
|
|
180
184
|
}
|
|
@@ -7,25 +7,25 @@ import type { Vec } from "../vec.js";
|
|
|
7
7
|
* the ones that carry meaning for what it did. `text` is always present (the
|
|
8
8
|
* human-readable rendering); the rest is provenance a debugger can lean on. */
|
|
9
9
|
export interface RationaleItem {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
10
|
+
/** Human-readable rendering — decoded text for a byte span, else a label like
|
|
11
|
+
* "‹none›" or an operator name. Always set, so a step always reads. */
|
|
12
|
+
text: string;
|
|
13
|
+
/** The graph node this element is, or resolved to, when known — the handle to
|
|
14
|
+
* point back at the exact stored fact in the content-addressed DAG. */
|
|
15
|
+
node?: number;
|
|
16
|
+
/** The `[start, end)` span this element occupies in its step's frame of
|
|
17
|
+
* reference (usually the query or the answer being composed). */
|
|
18
|
+
span?: [number, number];
|
|
19
|
+
/** The resonance / cosine score that selected this element, when it was chosen
|
|
20
|
+
* by similarity rather than by exact structure. */
|
|
21
|
+
score?: number;
|
|
22
|
+
/** A short role tag — "query", "leaf", "form", "hit", "connector", "answer",
|
|
23
|
+
* … — naming what KIND of element this is within the step. */
|
|
24
|
+
role?: string;
|
|
25
|
+
/** The gist vector, only when the element fundamentally IS a vector and a
|
|
26
|
+
* caller asked to carry it (off by default — a D-float array per item would
|
|
27
|
+
* bury the reasoning it is meant to explain). */
|
|
28
|
+
v?: Vec;
|
|
29
29
|
}
|
|
30
30
|
/** A single completed act of inference — one mechanism, run once.
|
|
31
31
|
*
|
|
@@ -35,30 +35,30 @@ export interface RationaleItem {
|
|
|
35
35
|
* parent's `index` is always lower than its children's, and the two orderings
|
|
36
36
|
* together give a valid topological reading of the dependency graph. */
|
|
37
37
|
export interface RationaleStep {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
38
|
+
/** This step's index, assigned when the mechanism was ENTERED — a strict,
|
|
39
|
+
* incremental ordering over the whole inference. */
|
|
40
|
+
index: number;
|
|
41
|
+
/** The mechanism and its enclosing mechanisms, outermost → innermost, e.g.
|
|
42
|
+
* `["respond", "think", "recognise"]`. The last entry is this step; the
|
|
43
|
+
* prefix is the nest of sub-mechanisms it ran inside. */
|
|
44
|
+
mechanism: string[];
|
|
45
|
+
/** The enclosing mechanism's step index, or -1 at the root — the NESTING edge
|
|
46
|
+
* of the dependency graph (which step this one is a part of). */
|
|
47
|
+
parent: number;
|
|
48
|
+
/** The earlier steps whose OUTPUTS became this step's inputs — the DATA-FLOW
|
|
49
|
+
* edges of the dependency graph. Defaults to the previous sibling (the step
|
|
50
|
+
* run just before this one inside the same mechanism), or the parent when
|
|
51
|
+
* this is the first sub-step; a mechanism that fuses several earlier results
|
|
52
|
+
* names them all explicitly. */
|
|
53
|
+
dependsOn: number[];
|
|
54
|
+
/** The vector of elements handed to the mechanism (one or more). */
|
|
55
|
+
inputs: RationaleItem[];
|
|
56
|
+
/** The vector of elements the mechanism produced (one or more) — longer than
|
|
57
|
+
* `inputs` when it decomposed, shorter when it combined. */
|
|
58
|
+
outputs: RationaleItem[];
|
|
59
|
+
/** A one-line, human account of what the mechanism did and why — the sentence
|
|
60
|
+
* that turns the data into an explanation. */
|
|
61
|
+
note?: string;
|
|
62
62
|
}
|
|
63
63
|
/** The callback {@link Mind.respond} / {@link Mind.respondText} accept. It is
|
|
64
64
|
* invoked once per completed step, AS the inference unfolds — never batched at
|
|
@@ -70,29 +70,23 @@ export declare function decodeText(bytes: Uint8Array): string;
|
|
|
70
70
|
/** The `[start, end)` gaps of `[0, queryLen)` NOT covered by `accounted` —
|
|
71
71
|
* the same union-of-spans reading think's grounding decider prices at PASS
|
|
72
72
|
* per byte, exposed here so a mechanism can turn it into a human label. */
|
|
73
|
-
export declare function unexplainedSpans(
|
|
74
|
-
queryLen: number,
|
|
75
|
-
accounted: ReadonlyArray<[number, number]>,
|
|
76
|
-
): Array<[number, number]>;
|
|
73
|
+
export declare function unexplainedSpans(queryLen: number, accounted: ReadonlyArray<[number, number]>): Array<[number, number]>;
|
|
77
74
|
/** A human-readable label for the query bytes a mechanism's `accounted`
|
|
78
75
|
* spans leave unexplained — purely diagnostic (Task 2's negative evidence):
|
|
79
76
|
* it never changes a candidate's weight, only what the rationale trace
|
|
80
77
|
* says the mechanism left on the table. `""` when nothing is unexplained. */
|
|
81
|
-
export declare function unexplainedLabel(
|
|
82
|
-
query: Uint8Array,
|
|
83
|
-
accounted: ReadonlyArray<[number, number]>,
|
|
84
|
-
): string;
|
|
78
|
+
export declare function unexplainedLabel(query: Uint8Array, accounted: ReadonlyArray<[number, number]>): string;
|
|
85
79
|
/** An open mechanism — the handle {@link Rationale.enter} returns. Hold it for
|
|
86
80
|
* the duration of the mechanism and call {@link Scope.done} with the outputs
|
|
87
81
|
* when it finishes; that emits the step and pops the nesting. */
|
|
88
82
|
export interface Scope {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
83
|
+
/** The step index reserved for this mechanism at entry — pass it as an
|
|
84
|
+
* explicit dependency of a later step that consumes this one's output. */
|
|
85
|
+
readonly index: number;
|
|
86
|
+
/** Close the mechanism: emit its step with these outputs and pop it off the
|
|
87
|
+
* nesting stack. Idempotent — a second call is ignored, so a `finally` that
|
|
88
|
+
* closes after an early return is safe. */
|
|
89
|
+
done(outputs: RationaleItem[], note?: string): void;
|
|
96
90
|
}
|
|
97
91
|
/** The live tracer: a stack of open mechanisms over one {@link Mind.respond}.
|
|
98
92
|
*
|
|
@@ -103,44 +97,38 @@ export interface Scope {
|
|
|
103
97
|
* sub-steps) is the two fused. The tracer never branches the control flow; it
|
|
104
98
|
* only records it. */
|
|
105
99
|
export declare class Rationale {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
name: string,
|
|
141
|
-
inputs: RationaleItem[],
|
|
142
|
-
outputs: RationaleItem[],
|
|
143
|
-
note?: string,
|
|
144
|
-
deps?: number[],
|
|
145
|
-
): number;
|
|
100
|
+
private readonly sink;
|
|
101
|
+
private next;
|
|
102
|
+
/** Open mechanisms, outermost first. Each frame remembers the last child it
|
|
103
|
+
* has spawned so the next sibling can default its data-flow edge to it. */
|
|
104
|
+
private readonly stack;
|
|
105
|
+
/** The most recent step index emitted under each mechanism name — the handle
|
|
106
|
+
* a later step uses to name an EARLIER mechanism as its data-flow producer
|
|
107
|
+
* (e.g. cover depends on the latest recognise / computeExtensions). One tracer is
|
|
108
|
+
* built per response and inference is sequential, so "most recent" is exactly
|
|
109
|
+
* "the one that produced the inputs I am about to consume". */
|
|
110
|
+
private readonly lastByName;
|
|
111
|
+
constructor(sink: InspectRationale);
|
|
112
|
+
/** The index of the most recent step with this mechanism name, or undefined if
|
|
113
|
+
* none has run. Used to wire an explicit producer edge into {@link
|
|
114
|
+
* Scope.done} / {@link step}'s `deps`. */
|
|
115
|
+
lastIndex(name: string): number | undefined;
|
|
116
|
+
/** The mechanism names currently open, outermost → innermost. */
|
|
117
|
+
private path;
|
|
118
|
+
/** The default data-flow edge for a step entering now: the previous sibling
|
|
119
|
+
* inside the current mechanism, else the enclosing mechanism, else nothing
|
|
120
|
+
* (the root). An explicit `deps` overrides this. */
|
|
121
|
+
private defaultDeps;
|
|
122
|
+
/** Reserve this step's index and register it as the current mechanism's most
|
|
123
|
+
* recent child (so the NEXT sibling chains to it) and as the most recent step
|
|
124
|
+
* of its own NAME (so a later mechanism can name it as a producer). */
|
|
125
|
+
private reserve;
|
|
126
|
+
private emit;
|
|
127
|
+
/** Enter a mechanism that has sub-steps. Captures its inputs and the nesting
|
|
128
|
+
* now; the matching {@link Scope.done} supplies the outputs when it finishes.
|
|
129
|
+
* `deps` overrides the default data-flow edge (previous sibling / parent). */
|
|
130
|
+
enter(name: string, inputs: RationaleItem[], deps?: number[]): Scope;
|
|
131
|
+
/** Record a mechanism that has no sub-steps — its inputs and outputs are both
|
|
132
|
+
* known at the call site. Returns its index, for a later step to depend on. */
|
|
133
|
+
step(name: string, inputs: RationaleItem[], outputs: RationaleItem[], note?: string, deps?: number[]): number;
|
|
146
134
|
}
|