@hviana/sema 0.2.6 → 0.2.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/dist/example/demo.d.ts +1 -0
- package/dist/example/demo.js +39 -0
- package/dist/example/train_base.d.ts +87 -0
- package/dist/example/train_base.js +2252 -0
- package/dist/src/alphabet.d.ts +7 -0
- package/dist/src/alphabet.js +33 -0
- package/dist/src/alu/src/alu.d.ts +185 -0
- package/dist/src/alu/src/alu.js +440 -0
- package/dist/src/alu/src/expr.d.ts +61 -0
- package/dist/src/alu/src/expr.js +318 -0
- package/dist/src/alu/src/index.d.ts +11 -0
- package/dist/src/alu/src/index.js +19 -0
- package/dist/src/alu/src/kernel-arith.d.ts +16 -0
- package/dist/src/alu/src/kernel-arith.js +264 -0
- package/dist/src/alu/src/kernel-bits.d.ts +19 -0
- package/dist/src/alu/src/kernel-bits.js +152 -0
- package/dist/src/alu/src/kernel-logic.d.ts +4 -0
- package/dist/src/alu/src/kernel-logic.js +60 -0
- package/dist/src/alu/src/kernel-nd.d.ts +3 -0
- package/dist/src/alu/src/kernel-nd.js +208 -0
- package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
- package/dist/src/alu/src/kernel-numeric.js +366 -0
- package/dist/src/alu/src/operation.d.ts +168 -0
- package/dist/src/alu/src/operation.js +189 -0
- package/dist/src/alu/src/parser.d.ts +221 -0
- package/dist/src/alu/src/parser.js +577 -0
- package/dist/src/alu/src/resonance.d.ts +55 -0
- package/dist/src/alu/src/resonance.js +126 -0
- package/dist/src/alu/src/text.d.ts +31 -0
- package/dist/src/alu/src/text.js +73 -0
- package/dist/src/alu/src/value.d.ts +109 -0
- package/dist/src/alu/src/value.js +300 -0
- package/dist/src/alu/test/alu.test.d.ts +1 -0
- package/dist/src/alu/test/alu.test.js +764 -0
- package/dist/src/bytes.d.ts +14 -0
- package/dist/src/bytes.js +59 -0
- package/dist/src/canon.d.ts +26 -0
- package/dist/src/canon.js +57 -0
- package/dist/src/config.d.ts +111 -0
- package/dist/src/config.js +91 -0
- package/dist/src/derive/src/deduction.d.ts +125 -0
- package/dist/src/derive/src/deduction.js +155 -0
- package/dist/src/derive/src/index.d.ts +7 -0
- package/dist/src/derive/src/index.js +11 -0
- package/dist/src/derive/src/priority-queue.d.ts +20 -0
- package/dist/src/derive/src/priority-queue.js +73 -0
- package/dist/src/derive/src/rewrite.d.ts +56 -0
- package/dist/src/derive/src/rewrite.js +100 -0
- package/dist/src/derive/src/trie.d.ts +90 -0
- package/dist/src/derive/src/trie.js +217 -0
- package/dist/src/derive/test/derive.test.d.ts +1 -0
- package/dist/src/derive/test/derive.test.js +122 -0
- package/dist/src/extension.d.ts +37 -0
- package/dist/src/extension.js +7 -0
- package/dist/src/geometry.d.ts +202 -0
- package/dist/src/geometry.js +575 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +18 -0
- package/dist/src/ingest-cache.d.ts +41 -0
- package/dist/src/ingest-cache.js +161 -0
- package/dist/src/mind/articulation.d.ts +6 -0
- package/dist/src/mind/articulation.js +99 -0
- package/dist/src/mind/attention.d.ts +400 -0
- package/dist/src/mind/attention.js +1763 -0
- package/dist/src/mind/bridge.d.ts +30 -0
- package/dist/src/mind/bridge.js +569 -0
- package/dist/src/mind/canonical.d.ts +29 -0
- package/dist/src/mind/canonical.js +88 -0
- package/dist/src/mind/graph-search.d.ts +285 -0
- package/dist/src/mind/graph-search.js +956 -0
- package/dist/src/mind/index.d.ts +7 -0
- package/dist/src/mind/index.js +5 -0
- package/dist/src/mind/junction.d.ts +124 -0
- package/dist/src/mind/junction.js +317 -0
- package/dist/src/mind/learning.d.ts +47 -0
- package/dist/src/mind/learning.js +248 -0
- package/dist/src/mind/match.d.ts +126 -0
- package/dist/src/mind/match.js +441 -0
- package/dist/src/mind/mechanisms/alu.d.ts +4 -0
- package/dist/src/mind/mechanisms/alu.js +36 -0
- package/dist/src/mind/mechanisms/cast.d.ts +89 -0
- package/dist/src/mind/mechanisms/cast.js +716 -0
- package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
- package/dist/src/mind/mechanisms/confluence.js +225 -0
- package/dist/src/mind/mechanisms/cover.d.ts +6 -0
- package/dist/src/mind/mechanisms/cover.js +203 -0
- package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
- package/dist/src/mind/mechanisms/extraction.js +387 -0
- package/dist/src/mind/mechanisms/recall.d.ts +13 -0
- package/dist/src/mind/mechanisms/recall.js +325 -0
- package/dist/src/mind/mind.d.ts +276 -0
- package/dist/src/mind/mind.js +572 -0
- package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
- package/dist/src/mind/pipeline-mechanism.js +213 -0
- package/dist/src/mind/pipeline.d.ts +20 -0
- package/dist/src/mind/pipeline.js +217 -0
- package/dist/src/mind/primitives.d.ts +70 -0
- package/dist/src/mind/primitives.js +287 -0
- package/dist/src/mind/rationale.d.ts +139 -0
- package/dist/src/mind/rationale.js +163 -0
- package/dist/src/mind/reasoning.d.ts +34 -0
- package/dist/src/mind/reasoning.js +240 -0
- package/dist/src/mind/recognition.d.ts +20 -0
- package/dist/src/mind/recognition.js +390 -0
- package/dist/src/mind/resonance.d.ts +23 -0
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +15 -0
- package/dist/src/mind/trace.js +73 -0
- package/dist/src/mind/traverse.d.ts +113 -0
- package/dist/src/mind/traverse.js +568 -0
- package/dist/src/mind/types.d.ts +289 -0
- package/dist/src/mind/types.js +130 -0
- 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-ivf/src/index.js +4 -0
- 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 +31 -0
- package/dist/src/sema.js +63 -0
- package/dist/src/store-sqlite.d.ts +184 -0
- package/dist/src/store-sqlite.js +942 -0
- package/dist/src/store.d.ts +663 -0
- package/dist/src/store.js +1618 -0
- package/dist/src/vec.d.ts +31 -0
- package/dist/src/vec.js +109 -0
- package/package.json +1 -1
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
// traverse.ts — Traverse primitives + disambiguation (Sections 1 & 6 of the mind).
|
|
2
|
+
//
|
|
3
|
+
// Traverse — node → nodes (edgeAncestors, nextOf, prevOf, contains,
|
|
4
|
+
// guidedNext, chooseNext, chooseAmong, hubCap)
|
|
5
|
+
//
|
|
6
|
+
// The PROJECTIONS built on these walks (follow, conceptHop, reverseContext,
|
|
7
|
+
// project) live in match.ts — the elementary match-and-project operation.
|
|
8
|
+
import { cosine } from "../vec.js";
|
|
9
|
+
import { gistOf, read } from "./primitives.js";
|
|
10
|
+
const structCaches = new WeakMap();
|
|
11
|
+
function getStructCache(ctx) {
|
|
12
|
+
if (ctx.climbMemo === null)
|
|
13
|
+
return null;
|
|
14
|
+
let c = structCaches.get(ctx.climbMemo);
|
|
15
|
+
if (c === undefined) {
|
|
16
|
+
structCaches.set(ctx.climbMemo, c = {
|
|
17
|
+
hasNext: new Map(),
|
|
18
|
+
prevCount: new Map(),
|
|
19
|
+
hasParents: new Map(),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return c;
|
|
23
|
+
}
|
|
24
|
+
/** Cached {@link Store.hasNext} — pure during one respond(). */
|
|
25
|
+
function cachedHasNext(ctx, id, cache) {
|
|
26
|
+
if (cache === null)
|
|
27
|
+
return ctx.store.hasNext(id);
|
|
28
|
+
let v = cache.hasNext.get(id);
|
|
29
|
+
if (v === undefined) {
|
|
30
|
+
v = ctx.store.hasNext(id);
|
|
31
|
+
cache.hasNext.set(id, v);
|
|
32
|
+
}
|
|
33
|
+
return v;
|
|
34
|
+
}
|
|
35
|
+
/** Cached {@link Store.prevCount} — pure during one respond(). */
|
|
36
|
+
function cachedPrevCount(ctx, id, cache) {
|
|
37
|
+
if (cache === null)
|
|
38
|
+
return ctx.store.prevCount(id);
|
|
39
|
+
let v = cache.prevCount.get(id);
|
|
40
|
+
if (v === undefined) {
|
|
41
|
+
v = ctx.store.prevCount(id);
|
|
42
|
+
cache.prevCount.set(id, v);
|
|
43
|
+
}
|
|
44
|
+
return v;
|
|
45
|
+
}
|
|
46
|
+
/** Cached {@link Store.hasParents} — pure during one respond(). */
|
|
47
|
+
function cachedHasParents(ctx, id, cache) {
|
|
48
|
+
if (cache === null)
|
|
49
|
+
return ctx.store.hasParents(id);
|
|
50
|
+
let v = cache.hasParents.get(id);
|
|
51
|
+
if (v === undefined) {
|
|
52
|
+
v = ctx.store.hasParents(id);
|
|
53
|
+
cache.hasParents.set(id, v);
|
|
54
|
+
}
|
|
55
|
+
return v;
|
|
56
|
+
}
|
|
57
|
+
// ── Graph climbing ───────────────────────────────────────────────────────
|
|
58
|
+
/** Climb the structural DAG from a node to its edge-bearing ancestor contexts.
|
|
59
|
+
* Ascent stops at hub nodes (parents > √N) — their reach is non-discriminative.
|
|
60
|
+
* When the start node has no structural parents, climbs from containment parents
|
|
61
|
+
* (sub-span flat branches inheriting their chunks' context).
|
|
62
|
+
*
|
|
63
|
+
* `memo`, when given, caches whole climbs by start id for the duration of ONE
|
|
64
|
+
* query (the store is read-only while a query is in flight, so a climb is a
|
|
65
|
+
* pure function of the id). The consensus pipeline climbs the SAME anchors
|
|
66
|
+
* repeatedly — regions sharing a chunk, and canonicalChunkId probing each
|
|
67
|
+
* chunk's prefixes — so without the memo every repeat re-pays the full
|
|
68
|
+
* fan-out reads. */
|
|
69
|
+
export function edgeAncestors(ctx, id, contextCount, memo) {
|
|
70
|
+
const hit = memo?.get(id);
|
|
71
|
+
if (hit !== undefined)
|
|
72
|
+
return hit;
|
|
73
|
+
// BYTE-ATOM COMMONALITY. A single-byte leaf (implicit negative id) has no
|
|
74
|
+
// structural parents BY CONSTRUCTION — atoms are never linked into the kid
|
|
75
|
+
// or contain tables — so this climb cannot observe its containment at all.
|
|
76
|
+
// The walk below would see only the atom's own direct edges and report
|
|
77
|
+
// contextsReached ≈ 1, turning the MOST common content in the store into
|
|
78
|
+
// the MOST discriminative voter (observed on a 325K-context store: every
|
|
79
|
+
// recognised single-letter site voted full ln N for the one fact whose
|
|
80
|
+
// continuation is that letter, and their pooled sum out-voted every
|
|
81
|
+
// genuine anchor). An unmeasurable containment must not default to
|
|
82
|
+
// "maximally rare": it is bounded below by the uniform expectation over
|
|
83
|
+
// the byte alphabet — N contexts, each at least one chunk of up to W of
|
|
84
|
+
// the 256 possible atoms, reach ≥ N·W/256 contexts per atom on average
|
|
85
|
+
// (see {@link atomReach}). When that floor itself exceeds the hub bound
|
|
86
|
+
// √N the atom is a hub at this corpus scale and the climb abstains
|
|
87
|
+
// (saturated) — the atom's own edges remain fully traversable (tier-0
|
|
88
|
+
// exact recall, chooseNext, project); only its say as a consensus voter
|
|
89
|
+
// is withdrawn. On a small store the floor stays ≤ √N and the atom
|
|
90
|
+
// climbs exactly as before, so single-letter facts keep working.
|
|
91
|
+
if (id < 0 && atomIsHub(ctx, contextCount)) {
|
|
92
|
+
const bound0 = Math.ceil(Math.sqrt(Math.max(2, contextCount)));
|
|
93
|
+
const reach = {
|
|
94
|
+
roots: [],
|
|
95
|
+
contextsReached: 0,
|
|
96
|
+
saturated: true,
|
|
97
|
+
...(ctx.trace
|
|
98
|
+
? {
|
|
99
|
+
saturation: {
|
|
100
|
+
reason: "byte-atom-commonality",
|
|
101
|
+
node: id,
|
|
102
|
+
observed: atomReach(ctx, contextCount),
|
|
103
|
+
limit: bound0,
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
: {}),
|
|
107
|
+
};
|
|
108
|
+
memo?.set(id, reach);
|
|
109
|
+
return reach;
|
|
110
|
+
}
|
|
111
|
+
const bound = Math.ceil(Math.sqrt(contextCount));
|
|
112
|
+
const roots = [];
|
|
113
|
+
const seen = new Set([id]);
|
|
114
|
+
const ctxSeen = new Set();
|
|
115
|
+
let saturated = false;
|
|
116
|
+
// Provenance of the FIRST decision that saturated this climb — allocated
|
|
117
|
+
// only when a trace is requested (see AncestorReach.saturation's doc); the
|
|
118
|
+
// climb itself never reads it back.
|
|
119
|
+
let satStop;
|
|
120
|
+
// EXPAND-UNTIL-DECIDED: a reach is consumed either as a VOTE (which needs
|
|
121
|
+
// contextsReached exactly, and only while ≤ √N — beyond that the region is
|
|
122
|
+
// non-discriminative) or as an ABSTENTION (saturated — whose roots and
|
|
123
|
+
// counts no consumer reads). So the climb may STOP the moment the answer
|
|
124
|
+
// is decided:
|
|
125
|
+
// • a node whose prev fan-in alone exceeds √N decides it (its
|
|
126
|
+
// predecessors are √N+ distinct contexts) — no read needed, prevCount
|
|
127
|
+
// is an indexed O(1);
|
|
128
|
+
// • distinct contexts crossing √N decides it;
|
|
129
|
+
// • a node with more than √N parents decides its own expansion (the
|
|
130
|
+
// classic hub guard; the walk aborts rather than continue, which no
|
|
131
|
+
// consumer can distinguish — saturated reaches are never voted).
|
|
132
|
+
// Below every decision threshold the walk is EXACT — identical roots and
|
|
133
|
+
// contexts to the unbounded climb — because prevFirst(√N) IS the full prev
|
|
134
|
+
// list and parentsFirst(√N+1) IS the full parent list whenever they do not
|
|
135
|
+
// decide. Work is bounded by √N contexts × the climb's local structure,
|
|
136
|
+
// never by the corpus.
|
|
137
|
+
const structCache = getStructCache(ctx);
|
|
138
|
+
// LATERAL-BRANCH ACCOUNT — the cumulative dual of the per-node hub guard.
|
|
139
|
+
// Within one deposit the ascent is a CHAIN (each node's first parent);
|
|
140
|
+
// every parent BEYOND a node's first is an entry into another containing
|
|
141
|
+
// structure (hash-consing: a shared subtree's extra parents are other
|
|
142
|
+
// deposits' chunks). The per-node guard already declares a node with more
|
|
143
|
+
// than √N parents non-discriminative; a climb whose ACCUMULATED lateral
|
|
144
|
+
// entries exceed √N has spread across just as many distinct containing
|
|
145
|
+
// structures — the same commonness, distributed along the cone instead of
|
|
146
|
+
// concentrated at one node — and is decided: saturated. A deep chain in
|
|
147
|
+
// ONE structure accrues no laterals, so legitimate deep scaffolding (a
|
|
148
|
+
// fragment far down a long cumulative context) still climbs to its root
|
|
149
|
+
// at any depth; what dies is the cross-structure drift that visited tens
|
|
150
|
+
// of thousands of edge-free interiors (profiled on a 17.7M-node store:
|
|
151
|
+
// ~20K distinct nodes per climb family, >95% unique — not memoisable)
|
|
152
|
+
// while the context account never decided.
|
|
153
|
+
let lateral = 0;
|
|
154
|
+
const visit = (x) => {
|
|
155
|
+
const hasNx = cachedHasNext(ctx, x, structCache);
|
|
156
|
+
const pc = cachedPrevCount(ctx, x, structCache);
|
|
157
|
+
if (hasNx || pc > 0) {
|
|
158
|
+
roots.push(x);
|
|
159
|
+
if (hasNx)
|
|
160
|
+
ctxSeen.add(x);
|
|
161
|
+
if (pc > bound) {
|
|
162
|
+
// decided: ≥ pc > √N distinct contexts
|
|
163
|
+
if (ctx.trace) {
|
|
164
|
+
satStop = {
|
|
165
|
+
reason: "predecessor-fan-in",
|
|
166
|
+
node: x,
|
|
167
|
+
observed: pc,
|
|
168
|
+
limit: bound,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
for (const p of ctx.store.prevFirst(x, bound))
|
|
174
|
+
ctxSeen.add(p);
|
|
175
|
+
if (ctxSeen.size > bound) {
|
|
176
|
+
// decided
|
|
177
|
+
if (ctx.trace) {
|
|
178
|
+
satStop = {
|
|
179
|
+
reason: "distinct-context-limit",
|
|
180
|
+
node: x,
|
|
181
|
+
observed: ctxSeen.size,
|
|
182
|
+
limit: bound,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const parents = ctx.store.parentsFirst(x, bound + 1);
|
|
189
|
+
if (parents.length > bound) {
|
|
190
|
+
// decided: hub
|
|
191
|
+
if (ctx.trace) {
|
|
192
|
+
satStop = {
|
|
193
|
+
reason: "parent-fan-out",
|
|
194
|
+
node: x,
|
|
195
|
+
observed: parents.length,
|
|
196
|
+
limit: bound,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
let fresh = 0;
|
|
202
|
+
for (const p of parents) {
|
|
203
|
+
if (!seen.has(p)) {
|
|
204
|
+
seen.add(p);
|
|
205
|
+
stack.push(p);
|
|
206
|
+
fresh++;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (fresh > 1) {
|
|
210
|
+
lateral += fresh - 1;
|
|
211
|
+
if (lateral > bound) {
|
|
212
|
+
// decided: cone-wide hub
|
|
213
|
+
if (ctx.trace) {
|
|
214
|
+
satStop = {
|
|
215
|
+
reason: "lateral-cone-limit",
|
|
216
|
+
node: x,
|
|
217
|
+
observed: lateral,
|
|
218
|
+
limit: bound,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return true;
|
|
225
|
+
};
|
|
226
|
+
const stack = [];
|
|
227
|
+
const containment = !cachedHasParents(ctx, id, structCache);
|
|
228
|
+
if (!containment)
|
|
229
|
+
stack.push(id);
|
|
230
|
+
// The containment seed is STREAMED in pages of √N: a distinctive window's
|
|
231
|
+
// containers (which converge on one or two contexts, however many chunks
|
|
232
|
+
// of one deposit repeat it) are walked IN FULL — exact — while a common
|
|
233
|
+
// window's corpus-sized container list is abandoned at the first decision
|
|
234
|
+
// above, after O(√N) pages at most (each page adds containers whose climbs
|
|
235
|
+
// add contexts; √N distinct contexts decide).
|
|
236
|
+
let containerOff = 0;
|
|
237
|
+
let containersExhausted = !containment;
|
|
238
|
+
climb: for (;;) {
|
|
239
|
+
if (stack.length === 0) {
|
|
240
|
+
if (containersExhausted)
|
|
241
|
+
break;
|
|
242
|
+
const page = ctx.store.containersSlice(id, containerOff, bound);
|
|
243
|
+
containerOff += page.length;
|
|
244
|
+
if (page.length < bound)
|
|
245
|
+
containersExhausted = true;
|
|
246
|
+
for (const c of page) {
|
|
247
|
+
if (!seen.has(c)) {
|
|
248
|
+
seen.add(c);
|
|
249
|
+
stack.push(c);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (stack.length === 0) {
|
|
253
|
+
if (containerOff === 0)
|
|
254
|
+
stack.push(id); // no containers at all
|
|
255
|
+
else
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
while (stack.length > 0) {
|
|
260
|
+
let x = stack.pop();
|
|
261
|
+
// TRANSPARENT-CHAIN HOP: a node with no edges in or out and exactly one
|
|
262
|
+
// parent contributes nothing here — no root, no context, no lateral
|
|
263
|
+
// entry — so the run to its first non-transparent ancestor is skipped
|
|
264
|
+
// in ONE store read (Store.chainRun) instead of three probes per node.
|
|
265
|
+
// The interior nodes still enter `seen`, exactly as a node-at-a-time
|
|
266
|
+
// ascent would have recorded them at push time, so sibling entries into
|
|
267
|
+
// the same chain keep identical fresh/lateral accounting; and if the
|
|
268
|
+
// terminal was already seen (another chain merged into this one first),
|
|
269
|
+
// it is not visited twice — the same dedup the push-time seen-check
|
|
270
|
+
// used to provide.
|
|
271
|
+
const run = ctx.store.chainRun(x);
|
|
272
|
+
if (run.length > 1) {
|
|
273
|
+
const top = run[run.length - 1];
|
|
274
|
+
const dup = seen.has(top);
|
|
275
|
+
for (let i = 1; i < run.length; i++)
|
|
276
|
+
seen.add(run[i]);
|
|
277
|
+
if (dup)
|
|
278
|
+
continue;
|
|
279
|
+
x = top;
|
|
280
|
+
}
|
|
281
|
+
if (!visit(x)) {
|
|
282
|
+
saturated = true;
|
|
283
|
+
break climb;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const reach = {
|
|
288
|
+
roots,
|
|
289
|
+
contextsReached: ctxSeen.size,
|
|
290
|
+
saturated,
|
|
291
|
+
...(saturated && satStop ? { saturation: satStop } : {}),
|
|
292
|
+
};
|
|
293
|
+
memo?.set(id, reach);
|
|
294
|
+
return reach;
|
|
295
|
+
}
|
|
296
|
+
/** Convenience: forward edges of a node. */
|
|
297
|
+
export function nextOf(ctx, id) {
|
|
298
|
+
return ctx.store.next(id);
|
|
299
|
+
}
|
|
300
|
+
/** Convenience: reverse edges of a node. */
|
|
301
|
+
export function prevOf(ctx, id) {
|
|
302
|
+
return ctx.store.prev(id);
|
|
303
|
+
}
|
|
304
|
+
/** The uniform-expectation floor on a byte atom's corpus commonality: N
|
|
305
|
+
* learnt contexts, each at least one perception chunk of up to W of the 256
|
|
306
|
+
* possible byte values, contain a given atom in ≥ N·W/256 contexts on
|
|
307
|
+
* average. An atom's TRUE containment is unmeasurable (atoms carry no
|
|
308
|
+
* kid/contain links by construction), so this floor is the honest stand-in:
|
|
309
|
+
* derived entirely from the corpus scale N, the perception window W, and
|
|
310
|
+
* the alphabet size — never tuned. */
|
|
311
|
+
export function atomReach(ctx, contextCount) {
|
|
312
|
+
return Math.max(1, Math.ceil((contextCount * ctx.space.maxGroup) / 256));
|
|
313
|
+
}
|
|
314
|
+
/** Whether a byte atom is a hub at this corpus scale — its commonality floor
|
|
315
|
+
* {@link atomReach} exceeds the hub bound √N. Below it (small stores) an
|
|
316
|
+
* atom votes and is recognised exactly as any stored form; above it the
|
|
317
|
+
* alphabet is scaffolding everywhere and abstains. */
|
|
318
|
+
export function atomIsHub(ctx, contextCount) {
|
|
319
|
+
return atomReach(ctx, contextCount) >
|
|
320
|
+
Math.ceil(Math.sqrt(Math.max(2, contextCount)));
|
|
321
|
+
}
|
|
322
|
+
/** Whether a node LEADS SOMEWHERE — it bears a continuation edge or a halo.
|
|
323
|
+
* The admission predicate recognition filters sites with (HOW_IT_WORKS
|
|
324
|
+
* §15.3): a form that leads nowhere contributes nothing to any derivation.
|
|
325
|
+
* Runs once per candidate span on the recognition hot path — `hasNext` is
|
|
326
|
+
* cached per response (the same flat-branch ids are probed across prefix
|
|
327
|
+
* variants by canonicalChunkId). `hasHalo` is not cached: it's a single
|
|
328
|
+
* indexed point probe per candidate, and the candidates that reach this
|
|
329
|
+
* check have already been filtered by hasNext above in edgeAncestors. */
|
|
330
|
+
export function leadsSomewhere(ctx, id) {
|
|
331
|
+
const memo = getStructCache(ctx);
|
|
332
|
+
if (cachedHasNext(ctx, id, memo))
|
|
333
|
+
return true;
|
|
334
|
+
return ctx.store.hasHalo(id);
|
|
335
|
+
}
|
|
336
|
+
/** The structural IDF read of ONE node: how many distinct learnt contexts
|
|
337
|
+
* its containment/edge climb reaches, or Infinity when it reaches none or
|
|
338
|
+
* saturates (no usable identity evidence). The number every
|
|
339
|
+
* discriminative-vs-scaffolding decision derives from — paired with the
|
|
340
|
+
* half-dominance convention (geometry.dominates(reach, N)): content
|
|
341
|
+
* reaching a corpus MINORITY of contexts discriminates (an entity, a
|
|
342
|
+
* filler); content reaching a majority is frame scaffolding. */
|
|
343
|
+
export function reachOf(ctx, id, contextCount, memo) {
|
|
344
|
+
const r = edgeAncestors(ctx, id, contextCount, memo);
|
|
345
|
+
if (r.saturated || r.roots.length === 0)
|
|
346
|
+
return Infinity;
|
|
347
|
+
return Math.max(1, r.contextsReached);
|
|
348
|
+
}
|
|
349
|
+
/** The corpus scale N — the count of DISTINCT learnt contexts, floored at 2
|
|
350
|
+
* so its derived readings (ln N in the consensus floor, √N in the hub bound)
|
|
351
|
+
* stay meaningful on a near-empty store. The one definition every consumer
|
|
352
|
+
* of "how big is this corpus?" reads. */
|
|
353
|
+
export function corpusN(ctx) {
|
|
354
|
+
return Math.max(2, ctx.store.edgeSourceCount());
|
|
355
|
+
}
|
|
356
|
+
/** The hub bound √N itself (≥ 2 always, since N is floored at 2) — for
|
|
357
|
+
* consumers that pass it to the store's LIMITed reads instead of capping a
|
|
358
|
+
* materialised list. {@link hubCap} is the list-side reading of the same
|
|
359
|
+
* convention. */
|
|
360
|
+
export function hubBound(ctx) {
|
|
361
|
+
return Math.ceil(Math.sqrt(corpusN(ctx)));
|
|
362
|
+
}
|
|
363
|
+
/** Cap a candidate list at the hub bound √N (insertion order) — the ONE
|
|
364
|
+
* fan-out convention every walk and disambiguation uses (see HOW_IT_WORKS
|
|
365
|
+
* §8.6). A node connected to more than √N others is a hub whose individual
|
|
366
|
+
* connections carry ~no discriminative information; materialising or scoring
|
|
367
|
+
* them all would make single decisions scale with the corpus. */
|
|
368
|
+
export function hubCap(ctx, ids) {
|
|
369
|
+
const bound = hubBound(ctx);
|
|
370
|
+
return ids.length > bound ? ids.slice(0, bound) : ids;
|
|
371
|
+
}
|
|
372
|
+
/** Whether `descendant` lies within `ancestor`'s subtree — a structural DAG
|
|
373
|
+
* relation read off the hash-consed `kids` lists, by a bounded explicit-stack
|
|
374
|
+
* descent. Used by articulation to keep a voice from revoicing a fragment
|
|
375
|
+
* OF that voice. */
|
|
376
|
+
export function contains(ctx, ancestor, descendant) {
|
|
377
|
+
if (ancestor === descendant)
|
|
378
|
+
return true;
|
|
379
|
+
const seen = new Set([ancestor]);
|
|
380
|
+
const stack = [ancestor];
|
|
381
|
+
while (stack.length > 0) {
|
|
382
|
+
const rec = ctx.store.get(stack.pop());
|
|
383
|
+
if (!rec?.kids)
|
|
384
|
+
continue;
|
|
385
|
+
for (const k of rec.kids) {
|
|
386
|
+
if (k === descendant)
|
|
387
|
+
return true;
|
|
388
|
+
if (!seen.has(k)) {
|
|
389
|
+
seen.add(k);
|
|
390
|
+
stack.push(k);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
// ── Edge disambiguation (Section 6) ──────────────────────────────────────
|
|
397
|
+
/** The best-scoring item by cosine against `query`, among items scoring at
|
|
398
|
+
* or above `threshold` — the shared arg-max every Pattern-A "which of these
|
|
399
|
+
* resonates best" decision reduces to. `strict` picks the tie-break a
|
|
400
|
+
* caller needs: `true` keeps the first-seen leader on a tie (`>`), the
|
|
401
|
+
* default lets a later equal score take it (`>=`). */
|
|
402
|
+
export function argmaxBy(items, scoreOf, threshold, strict = false) {
|
|
403
|
+
let best = null;
|
|
404
|
+
for (const item of items) {
|
|
405
|
+
const score = scoreOf(item);
|
|
406
|
+
const bar = best?.score ?? threshold;
|
|
407
|
+
if (strict ? score > bar : score >= bar)
|
|
408
|
+
best = { item, score };
|
|
409
|
+
}
|
|
410
|
+
return best;
|
|
411
|
+
}
|
|
412
|
+
export function argmaxCosine(query, items, vecOf, threshold, strict = false) {
|
|
413
|
+
return argmaxBy(items, (item) => {
|
|
414
|
+
const v = vecOf(item);
|
|
415
|
+
return v ? cosine(query, v) : -Infinity;
|
|
416
|
+
}, threshold, strict);
|
|
417
|
+
}
|
|
418
|
+
/** The guided-or-first continuation of a node, as answer-shaped bytes source:
|
|
419
|
+
* chooseNext under the response guide, falling back to the FIRST-inserted
|
|
420
|
+
* edge — the one no-guide convention chooseNext, project() and the search's
|
|
421
|
+
* formRules all share. undefined when the node has no continuation. */
|
|
422
|
+
export function guidedFirst(ctx, id) {
|
|
423
|
+
const pick = guidedNext(ctx, id);
|
|
424
|
+
if (pick !== undefined)
|
|
425
|
+
return pick;
|
|
426
|
+
// No guide in flight (or nothing chosen): the first-inserted edge, read
|
|
427
|
+
// with LIMIT 1 — never the full fan-out.
|
|
428
|
+
const nx = ctx.store.nextFirst(id, 1);
|
|
429
|
+
return nx.length > 0 ? nx[0] : undefined;
|
|
430
|
+
}
|
|
431
|
+
export function guidedNext(ctx, node) {
|
|
432
|
+
if (ctx._edgeGuide === null)
|
|
433
|
+
return undefined;
|
|
434
|
+
// The pick memo is BYPASSED while a rationale trace is attached — the same
|
|
435
|
+
// policy climbMemo and recogniseMemo follow (every mechanism must emit its
|
|
436
|
+
// own steps; a memo hit would swallow the repeat's `disambiguate` step).
|
|
437
|
+
// Consistency does not need the memo: chooseNext is a pure function of the
|
|
438
|
+
// (read-only) store and the guide, so recomputation yields the same pick.
|
|
439
|
+
if (!ctx.trace) {
|
|
440
|
+
const memo = ctx._edgeChoice.get(node);
|
|
441
|
+
if (memo !== undefined)
|
|
442
|
+
return memo === -1 ? undefined : memo;
|
|
443
|
+
}
|
|
444
|
+
const pick = chooseNext(ctx, node, ctx._edgeGuide);
|
|
445
|
+
if (!ctx.trace)
|
|
446
|
+
ctx._edgeChoice.set(node, pick ?? -1);
|
|
447
|
+
return pick;
|
|
448
|
+
}
|
|
449
|
+
/** Disambiguate among a node's learnt continuations by distributional
|
|
450
|
+
* support. NOTE the `guide` contract: its VALUE is deliberately unused —
|
|
451
|
+
* only its PRESENCE gates disambiguation (a null guide means no query is in
|
|
452
|
+
* flight, so structural walkers keep plain first-edge behaviour). The
|
|
453
|
+
* gist-cosine of short answer candidates against a query guide is dominated
|
|
454
|
+
* by accidental byte-pattern correlations, not semantic relatedness, so the
|
|
455
|
+
* evidence consulted is structural: each candidate's reverse-edge support
|
|
456
|
+
* count (see below). Contrast {@link chooseAmong}, the REVERSE-direction
|
|
457
|
+
* disambiguator, whose candidates are whole learnt contexts — long enough
|
|
458
|
+
* that their perceived gists ARE semantically meaningful — and which
|
|
459
|
+
* therefore scores by guide cosine. The two directions consult different
|
|
460
|
+
* halves of the evidence on purpose. */
|
|
461
|
+
export function chooseNext(ctx, id, guide) {
|
|
462
|
+
// CAPPED read: only the first √N continuations are ever candidates (the
|
|
463
|
+
// documented hub trade), so only they are read — a hub context's full
|
|
464
|
+
// fan-out is corpus-sized and must never be materialised. hubBound ≥ 2,
|
|
465
|
+
// so the single-continuation fast path below stays exact.
|
|
466
|
+
const nx = ctx.store.nextFirst(id, hubBound(ctx));
|
|
467
|
+
if (nx.length === 0)
|
|
468
|
+
return undefined;
|
|
469
|
+
if (nx.length === 1 || !guide)
|
|
470
|
+
return nx[0];
|
|
471
|
+
// Cap candidates at √N — the same bound the original chooseAmong used.
|
|
472
|
+
// A hub context can accumulate thousands of continuations; the best-fit
|
|
473
|
+
// one is among the first √N by insertion order (edges are never deleted,
|
|
474
|
+
// so the oldest are the most established). A strongly-supported edge
|
|
475
|
+
// inserted beyond the cap is invisible here — the deliberate trade
|
|
476
|
+
// against paying O(fan-out) count reads on every disambiguation.
|
|
477
|
+
const capped = nx; // already the hub-capped prefix, by the read above
|
|
478
|
+
// Distributional-evidence disambiguation, consulting BOTH read-outs of the
|
|
479
|
+
// evidence the training poured:
|
|
480
|
+
// 1. prevCount — how many DISTINCT contexts predict this candidate (one
|
|
481
|
+
// indexed COUNT; never a materialisation — a common continuation's
|
|
482
|
+
// reverse fan-in is corpus-sized). Diversity of independent evidence
|
|
483
|
+
// is the primary signal: three different formulations agreeing beat
|
|
484
|
+
// one formulation repeated.
|
|
485
|
+
// 2. haloMass — how many episode signatures were poured into the
|
|
486
|
+
// candidate's halo (repetition counts). The tie-break among equally
|
|
487
|
+
// diverse candidates: a fact reinforced across many episodes is more
|
|
488
|
+
// corroborated than one seen once, and this is the DIRECT measure of
|
|
489
|
+
// that — consulting only the structural count would leave poured
|
|
490
|
+
// evidence on the table.
|
|
491
|
+
// When both are equal, first-inserted wins (backward compatible).
|
|
492
|
+
let best = capped[0];
|
|
493
|
+
let bestSupport = ctx.store.prevCount(best);
|
|
494
|
+
let bestMass = ctx.store.haloMass(best);
|
|
495
|
+
for (let i = 1; i < capped.length; i++) {
|
|
496
|
+
const support = ctx.store.prevCount(capped[i]);
|
|
497
|
+
if (support < bestSupport)
|
|
498
|
+
continue;
|
|
499
|
+
const mass = ctx.store.haloMass(capped[i]);
|
|
500
|
+
if (support > bestSupport || mass > bestMass) {
|
|
501
|
+
best = capped[i];
|
|
502
|
+
bestSupport = support;
|
|
503
|
+
bestMass = mass;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// NO consensusFloor gate here (tried and reverted — see
|
|
507
|
+
// test/40-choosenext-scale-guard.test.mjs): that floor is calibrated for
|
|
508
|
+
// POOLED, IDF-weighted CLIMB VOTES (recallByResonance, commitVotes), where
|
|
509
|
+
// each corroborating region contributes at most ln N and the floor grows
|
|
510
|
+
// with N exactly as that per-region ceiling does (HOW_IT_WORKS.md §8.6).
|
|
511
|
+
// `bestSupport` here is a different kind of quantity — a raw prevCount of
|
|
512
|
+
// how many training contexts predicted ONE destination, bounded by how
|
|
513
|
+
// often that specific fact was retold, never by corpus size N. Gating an
|
|
514
|
+
// N-invariant count against an N-growing threshold guarantees failure
|
|
515
|
+
// once N is large enough, discarding genuinely, structurally dominant
|
|
516
|
+
// edges (observed: a fact corroborated 2-to-1-1-1 refused at N≈325K,
|
|
517
|
+
// falling back to a noisy concept-hop). The loop above already IS the
|
|
518
|
+
// "genuinely competing" test: a tie leaves first-inserted as the pick
|
|
519
|
+
// (test/30's own pinned behaviour); a strict winner is real evidence
|
|
520
|
+
// regardless of corpus scale. Matches HOW_IT_WORKS.md §25's own
|
|
521
|
+
// chooseNext pseudocode, which has no such floor.
|
|
522
|
+
// Trace is built lazily — the filter + map below only execute when a
|
|
523
|
+
// trace listener is attached, so the common (no-trace) path pays only
|
|
524
|
+
// for the prevCount calls in the loop above, never for extra rItemShort
|
|
525
|
+
// byte-reads.
|
|
526
|
+
if (ctx.trace) {
|
|
527
|
+
const others = capped.filter((c) => c !== best);
|
|
528
|
+
ctx.trace.step("disambiguate", [rItemShort(ctx, best, "halo-evidence", bestSupport)], others.map((c) => rItemShort(ctx, c, "candidate", ctx.store.prevCount(c))), `${capped.length} continuations — distributional evidence selects ` +
|
|
529
|
+
`the most corroborated (distinct contexts ${bestSupport}, ` +
|
|
530
|
+
`poured mass ${bestMass})`);
|
|
531
|
+
}
|
|
532
|
+
return best;
|
|
533
|
+
}
|
|
534
|
+
/** The perceived gist of a candidate node, through the session gist cache.
|
|
535
|
+
* Re-gisting a candidate is a full river fold of its bytes — the measured
|
|
536
|
+
* recall bottleneck (a hub context offers up to √N continuations, EACH
|
|
537
|
+
* re-perceived per pick). A node's bytes are immutable and perception is
|
|
538
|
+
* pure, so the cached gist is valid for the store's lifetime. Exported for
|
|
539
|
+
* every "score node ids against a guide" decision (chooseAmong here, the
|
|
540
|
+
* bridge's junction pick) so they share ONE cache and one convention. */
|
|
541
|
+
export function candidateGist(ctx, c) {
|
|
542
|
+
const hit = ctx._gistCache.get(c);
|
|
543
|
+
if (hit !== undefined)
|
|
544
|
+
return hit;
|
|
545
|
+
const b = read(ctx, c);
|
|
546
|
+
if (b.length === 0)
|
|
547
|
+
return null;
|
|
548
|
+
const g = gistOf(ctx, b);
|
|
549
|
+
ctx._gistCache.set(c, g);
|
|
550
|
+
return g;
|
|
551
|
+
}
|
|
552
|
+
export function chooseAmong(ctx, candidates, guide) {
|
|
553
|
+
const capped = hubCap(ctx, candidates);
|
|
554
|
+
const found = argmaxCosine(guide, capped, (c) => candidateGist(ctx, c), -Infinity, true);
|
|
555
|
+
return found
|
|
556
|
+
? { id: found.item, score: found.score }
|
|
557
|
+
: { id: candidates[0], score: -Infinity };
|
|
558
|
+
}
|
|
559
|
+
// ── Trace shim (used by chooseNext before trace module is loaded) ────────
|
|
560
|
+
import { decodeText } from "./rationale.js";
|
|
561
|
+
function rItemShort(ctx, id, role, score) {
|
|
562
|
+
return {
|
|
563
|
+
text: decodeText(read(ctx, id)),
|
|
564
|
+
node: id,
|
|
565
|
+
role,
|
|
566
|
+
score,
|
|
567
|
+
};
|
|
568
|
+
}
|