@hviana/sema 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/package.json +1 -1
  2. package/src/mind/attention.ts +355 -19
  3. package/test/53-cross-region-probe-instrumentation.test.mjs +356 -0
  4. package/dist/example/demo.d.ts +0 -1
  5. package/dist/example/demo.js +0 -39
  6. package/dist/example/train_base.d.ts +0 -87
  7. package/dist/example/train_base.js +0 -2252
  8. package/dist/src/alphabet.d.ts +0 -7
  9. package/dist/src/alphabet.js +0 -33
  10. package/dist/src/alu/src/alu.d.ts +0 -185
  11. package/dist/src/alu/src/alu.js +0 -440
  12. package/dist/src/alu/src/expr.d.ts +0 -61
  13. package/dist/src/alu/src/expr.js +0 -318
  14. package/dist/src/alu/src/index.d.ts +0 -11
  15. package/dist/src/alu/src/index.js +0 -19
  16. package/dist/src/alu/src/kernel-arith.d.ts +0 -16
  17. package/dist/src/alu/src/kernel-arith.js +0 -264
  18. package/dist/src/alu/src/kernel-bits.d.ts +0 -19
  19. package/dist/src/alu/src/kernel-bits.js +0 -152
  20. package/dist/src/alu/src/kernel-logic.d.ts +0 -4
  21. package/dist/src/alu/src/kernel-logic.js +0 -60
  22. package/dist/src/alu/src/kernel-nd.d.ts +0 -3
  23. package/dist/src/alu/src/kernel-nd.js +0 -208
  24. package/dist/src/alu/src/kernel-numeric.d.ts +0 -54
  25. package/dist/src/alu/src/kernel-numeric.js +0 -366
  26. package/dist/src/alu/src/operation.d.ts +0 -168
  27. package/dist/src/alu/src/operation.js +0 -189
  28. package/dist/src/alu/src/parser.d.ts +0 -221
  29. package/dist/src/alu/src/parser.js +0 -577
  30. package/dist/src/alu/src/resonance.d.ts +0 -55
  31. package/dist/src/alu/src/resonance.js +0 -126
  32. package/dist/src/alu/src/text.d.ts +0 -31
  33. package/dist/src/alu/src/text.js +0 -73
  34. package/dist/src/alu/src/value.d.ts +0 -109
  35. package/dist/src/alu/src/value.js +0 -300
  36. package/dist/src/alu/test/alu.test.d.ts +0 -1
  37. package/dist/src/alu/test/alu.test.js +0 -764
  38. package/dist/src/bytes.d.ts +0 -14
  39. package/dist/src/bytes.js +0 -59
  40. package/dist/src/canon.d.ts +0 -26
  41. package/dist/src/canon.js +0 -57
  42. package/dist/src/config.d.ts +0 -111
  43. package/dist/src/config.js +0 -91
  44. package/dist/src/derive/src/deduction.d.ts +0 -125
  45. package/dist/src/derive/src/deduction.js +0 -155
  46. package/dist/src/derive/src/index.d.ts +0 -7
  47. package/dist/src/derive/src/index.js +0 -11
  48. package/dist/src/derive/src/priority-queue.d.ts +0 -20
  49. package/dist/src/derive/src/priority-queue.js +0 -73
  50. package/dist/src/derive/src/rewrite.d.ts +0 -56
  51. package/dist/src/derive/src/rewrite.js +0 -100
  52. package/dist/src/derive/src/trie.d.ts +0 -90
  53. package/dist/src/derive/src/trie.js +0 -217
  54. package/dist/src/derive/test/derive.test.d.ts +0 -1
  55. package/dist/src/derive/test/derive.test.js +0 -122
  56. package/dist/src/extension.d.ts +0 -37
  57. package/dist/src/extension.js +0 -7
  58. package/dist/src/geometry.d.ts +0 -202
  59. package/dist/src/geometry.js +0 -575
  60. package/dist/src/index.d.ts +0 -16
  61. package/dist/src/index.js +0 -18
  62. package/dist/src/ingest-cache.d.ts +0 -41
  63. package/dist/src/ingest-cache.js +0 -161
  64. package/dist/src/mind/articulation.d.ts +0 -6
  65. package/dist/src/mind/articulation.js +0 -99
  66. package/dist/src/mind/attention.d.ts +0 -315
  67. package/dist/src/mind/attention.js +0 -1549
  68. package/dist/src/mind/bridge.d.ts +0 -30
  69. package/dist/src/mind/bridge.js +0 -569
  70. package/dist/src/mind/canonical.d.ts +0 -29
  71. package/dist/src/mind/canonical.js +0 -88
  72. package/dist/src/mind/graph-search.d.ts +0 -285
  73. package/dist/src/mind/graph-search.js +0 -956
  74. package/dist/src/mind/index.d.ts +0 -7
  75. package/dist/src/mind/index.js +0 -5
  76. package/dist/src/mind/junction.d.ts +0 -124
  77. package/dist/src/mind/junction.js +0 -317
  78. package/dist/src/mind/learning.d.ts +0 -47
  79. package/dist/src/mind/learning.js +0 -248
  80. package/dist/src/mind/match.d.ts +0 -126
  81. package/dist/src/mind/match.js +0 -441
  82. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  83. package/dist/src/mind/mechanisms/alu.js +0 -36
  84. package/dist/src/mind/mechanisms/cast.d.ts +0 -89
  85. package/dist/src/mind/mechanisms/cast.js +0 -716
  86. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  87. package/dist/src/mind/mechanisms/confluence.js +0 -225
  88. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  89. package/dist/src/mind/mechanisms/cover.js +0 -203
  90. package/dist/src/mind/mechanisms/extraction.d.ts +0 -67
  91. package/dist/src/mind/mechanisms/extraction.js +0 -387
  92. package/dist/src/mind/mechanisms/recall.d.ts +0 -13
  93. package/dist/src/mind/mechanisms/recall.js +0 -325
  94. package/dist/src/mind/mind.d.ts +0 -276
  95. package/dist/src/mind/mind.js +0 -572
  96. package/dist/src/mind/pipeline-mechanism.d.ts +0 -142
  97. package/dist/src/mind/pipeline-mechanism.js +0 -213
  98. package/dist/src/mind/pipeline.d.ts +0 -20
  99. package/dist/src/mind/pipeline.js +0 -217
  100. package/dist/src/mind/primitives.d.ts +0 -70
  101. package/dist/src/mind/primitives.js +0 -287
  102. package/dist/src/mind/rationale.d.ts +0 -139
  103. package/dist/src/mind/rationale.js +0 -163
  104. package/dist/src/mind/reasoning.d.ts +0 -34
  105. package/dist/src/mind/reasoning.js +0 -240
  106. package/dist/src/mind/recognition.d.ts +0 -20
  107. package/dist/src/mind/recognition.js +0 -390
  108. package/dist/src/mind/resonance.d.ts +0 -23
  109. package/dist/src/mind/resonance.js +0 -0
  110. package/dist/src/mind/trace.d.ts +0 -15
  111. package/dist/src/mind/trace.js +0 -73
  112. package/dist/src/mind/traverse.d.ts +0 -113
  113. package/dist/src/mind/traverse.js +0 -568
  114. package/dist/src/mind/types.d.ts +0 -289
  115. package/dist/src/mind/types.js +0 -130
  116. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  117. package/dist/src/rabitq-ivf/src/database.js +0 -201
  118. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  119. package/dist/src/rabitq-ivf/src/index.js +0 -4
  120. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  121. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  122. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  123. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  124. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  125. package/dist/src/rabitq-ivf/src/rabitq.js +0 -283
  126. package/dist/src/sema.d.ts +0 -31
  127. package/dist/src/sema.js +0 -63
  128. package/dist/src/store-sqlite.d.ts +0 -184
  129. package/dist/src/store-sqlite.js +0 -942
  130. package/dist/src/store.d.ts +0 -663
  131. package/dist/src/store.js +0 -1618
  132. package/dist/src/vec.d.ts +0 -31
  133. package/dist/src/vec.js +0 -109
@@ -1,161 +0,0 @@
1
- // ingest-cache.ts — ingest-level cache for Mind.
2
- //
3
- // Wraps a Mind and memoises the expensive, content-determined half of ingestion:
4
- // turning ONE input (a context, a continuation, or a bare experience) into its
5
- // interned graph nodes. Perception (riverFold) and interning are a PURE function
6
- // of the input bytes — the same bytes always cut the same way and hash-cons to
7
- // the same node ids — so the result can be cached by content and reused wherever
8
- // those same bytes appear again, in ANY role.
9
- //
10
- // This is why the cache keys on the single input, not on the (context,
11
- // continuation) pair: real training corpora repeat content at the INPUT level far
12
- // more than at the pair level — a shared system/tool preamble in front of every
13
- // row, a common answer ("Yes.", a refusal) under many questions, an accumulated
14
- // dialogue prefix, an exactly-duplicated row. A pair-level key sees none of these
15
- // (the pair as a whole is almost always unique); the input-level key catches them
16
- // all, and a context that recurs as a continuation (or vice versa) hits too.
17
- //
18
- // On a cache hit only the cheap relational writes are replayed — the edge and the
19
- // two halo pours for a pair, or the part-chain links for a single — while the
20
- // perceive + internTree work (the dominant cost, and on a large store the dominant
21
- // VECTOR-INDEX cost too) is skipped entirely. The interned node ids are stable
22
- // across eviction: a re-perceive of evicted content reproduces the same ids by
23
- // the store's hash-cons, so correctness never depends on a hit.
24
- //
25
- // DEPOSITION IS THE MIND'S OWN: every miss goes through the same
26
- // {@link deposit} the direct Mind.ingest path uses — perception, interning,
27
- // sub-span/window indexing, durable CONTAINMENT edges, whole-stream flat
28
- // branch, changed-node tracking against the Mind's `_prevSeen` — so a store
29
- // trained through this cache is structurally IDENTICAL to one trained by
30
- // calling mind.ingest() directly (this class used to re-implement the deposit
31
- // and drifted: no indexSubSpans/addContainer, wrong part-link stride). Only
32
- // the memo layer lives here. Behaviour is identical to mind.ingest() (links
33
- // and halos are reinforced on every call), just faster when content repeats.
34
- import { deposit, dispatchIngest, ingestOne as depositOne, } from "./mind/learning.js";
35
- import { bindSeat, companySignature } from "./sema.js";
36
- import { BoundedMap } from "./store.js";
37
- /**
38
- * An ingest cache layered over a Mind.
39
- *
40
- * Usage (drop-in for mind.ingest in training loops):
41
- * const ci = new CachedIngest(mind);
42
- * await ci.ingest(ctx, cont); // caches both sides
43
- * await ci.ingest("some text"); // caches the single text
44
- * await ci.ingest([[ctx, cont]]); // array form
45
- *
46
- * All other Mind methods (respond, save, …) are accessed on `ci.mind`.
47
- */
48
- export class CachedIngest {
49
- mind;
50
- /** One unified, content-addressed memo of interned inputs. Each value holds
51
- * Float32 root vector (D·4 bytes) plus a small id array, so the byte budget
52
- * is spent on the inputs most worth remembering (LRU). */
53
- _memo;
54
- hits = 0;
55
- misses = 0;
56
- constructor(mind) {
57
- this.mind = mind;
58
- this._memo = this.newMemo();
59
- }
60
- newMemo() {
61
- return new BoundedMap(this.mind.cfg.store.ingestCacheBytes, (c) => c.rootV.byteLength + c.partIds.length * 4 + 8 + c.keyBytes);
62
- }
63
- // ── cache key ───────────────────────────────────────────────────────────
64
- // Content key for an input, or null when the input cannot be keyed reliably
65
- // — in which case it BYPASSES the cache (recomputed every time) rather than
66
- // risk a collision returning the wrong nodes. A string is its own key; raw
67
- // bytes hash by FNV-1a (+ length, so a hash collision still needs an equal
68
- // length to alias); a Grid / Grid[] has no cheap stable key, so it bypasses.
69
- keyOf(input) {
70
- if (typeof input === "string")
71
- return "S:" + input;
72
- if (input instanceof Uint8Array) {
73
- // FNV-1a — no spread onto the call stack, safe on large binary inputs.
74
- let h = 0x811c9dc5 >>> 0;
75
- for (let i = 0; i < input.length; i++) {
76
- h ^= input[i];
77
- h = Math.imul(h, 0x01000193) >>> 0;
78
- }
79
- return "B:" + (h >>> 0).toString(16) + ":" + input.length;
80
- }
81
- return null; // Grid / Grid[] — bypass the cache
82
- }
83
- // ── public API ────────────────────────────────────────────────────────
84
- async ingest(input, second) {
85
- // One shape-reading for both ingest paths — see {@link dispatchIngest}.
86
- return dispatchIngest(input, second, (i) => this.ingestOne(i), (a, b) => this.ingestPair(a, b));
87
- }
88
- // ── the one cached step: perceive + intern + index ONE input ──────────────
89
- /** Resolve an input to its interned nodes, from the memo when its content has
90
- * been seen before, else by the Mind's OWN untracked {@link deposit}
91
- * (perceive + intern + sub-span/containment indexing — identical to the
92
- * direct path; those writes are durable and idempotent, so a later memo hit
93
- * legitimately skips them). This is the single expensive operation both
94
- * ingest paths share; caching it here is what makes every repeated input —
95
- * in any role — cheap. */
96
- async resolveInput(input) {
97
- const key = this.keyOf(input);
98
- if (key !== null) {
99
- const cached = this._memo.get(key);
100
- if (cached) {
101
- this.hits++;
102
- return cached;
103
- }
104
- }
105
- this.misses++;
106
- // The full deposit, untracked (a continuation must not break the tracked
107
- // deposit chain). `partIds` are the root's immediate children, already
108
- // interned by the deposit — free to capture. Only the root vector and
109
- // ids are retained; the tree is GC'd.
110
- const { tree, rootId, ids } = await deposit(this.mind, input, false);
111
- const partIds = tree.kids
112
- ? tree.kids.map((k) => ids.get(k))
113
- : [rootId];
114
- const entry = {
115
- rootV: tree.v,
116
- rootId,
117
- partIds,
118
- // ~2 bytes per UTF-16 code unit — the key is retained by the map.
119
- keyBytes: key !== null ? key.length * 2 : 0,
120
- };
121
- if (key !== null)
122
- this._memo.set(key, entry);
123
- return entry;
124
- }
125
- // ── pair ingest ───────────────────────────────────────────────────────
126
- async ingestPair(ctx, cont) {
127
- // The CONTEXT side is TRACKED (it continues the Mind's deposit chain and
128
- // is the growing, unique-per-turn side — a cache miss anyway), via the
129
- // same {@link deposit} the direct path uses; the continuation resolves
130
- // through the memo.
131
- const c = await deposit(this.mind, ctx, true);
132
- const b = await this.resolveInput(cont);
133
- // EDGE on the FULL context (disambiguates a shared pivot). HALO only on the
134
- // CHANGED NODES — the coherent new subtrees this turn adds — so a recurring
135
- // shared prefix never collects halo mass. (See Mind.ingestPair.)
136
- await this.mind.store.link(c.rootId, b.rootId);
137
- // Halos pour company SIGNATURES (identity), not gists (content) — see
138
- // companySignature in sema.ts.
139
- const contSeat = bindSeat(this.mind.space, companySignature(this.mind.space, b.rootId), 1);
140
- for (const part of c.changed) {
141
- const partId = c.ids.get(part);
142
- await this.mind.store.pourHalo(partId, contSeat);
143
- await this.mind.store.pourHalo(b.rootId, bindSeat(this.mind.space, companySignature(this.mind.space, partId), 0));
144
- }
145
- }
146
- // ── one-side ingest ───────────────────────────────────────────────────
147
- async ingestOne(input) {
148
- // A bare experience is the direct path verbatim — tracked deposit, root
149
- // marked a resonance target, part chain linked at the maxGroup stride.
150
- // (The tracked side is a memo miss by design, so there is nothing for the
151
- // cache to add here.)
152
- return depositOne(this.mind, input);
153
- }
154
- // ── helpers ───────────────────────────────────────────────────────────
155
- clear() {
156
- this._memo = this.newMemo();
157
- }
158
- get size() {
159
- return this._memo.size;
160
- }
161
- }
@@ -1,6 +0,0 @@
1
- import type { MindContext } from "./types.js";
2
- /** Re-voice an answer in the asker's own words. For each recognised form
3
- * in the answer, find a concept-sibling in the query (by halo resonance)
4
- * and substitute the asker's wording. The search's own cover mechanism
5
- * splices the substitutes into the answer exactly where the forms sit. */
6
- export declare function articulate(ctx: MindContext, answer: Uint8Array, query: Uint8Array | null): Promise<Uint8Array>;
@@ -1,99 +0,0 @@
1
- // articulation.ts — re-voice an answer in the asker's wording (Section 5).
2
- //
3
- // articulate — substitute answer forms with the asker's synonyms,
4
- // using concept (halo) resonance to match the voices.
5
- import { spliceAll } from "./types.js";
6
- import { recognise } from "./recognition.js";
7
- import { contains } from "./traverse.js";
8
- import { bestHaloMate } from "./match.js";
9
- import { coverSequence } from "../derive/src/index.js";
10
- import { rItem, rNode, traceDerivation } from "./trace.js";
11
- /** Re-voice an answer in the asker's own words. For each recognised form
12
- * in the answer, find a concept-sibling in the query (by halo resonance)
13
- * and substitute the asker's wording. The search's own cover mechanism
14
- * splices the substitutes into the answer exactly where the forms sit. */
15
- export async function articulate(ctx, answer, query) {
16
- if (!query || query.length < 2 || answer.length < 2)
17
- return answer;
18
- const store = ctx.store;
19
- const t = ctx.trace?.enter("articulate", [
20
- rItem(answer, "answer"),
21
- rItem(query, "query"),
22
- ]);
23
- const keep = (note) => {
24
- t?.done([rItem(answer, "answer")], note);
25
- return answer;
26
- };
27
- const qCandidates = [];
28
- for (const s of recognise(ctx, query).sites) {
29
- // payload < 0 is a SINGLE-BYTE leaf (byte leaves occupy the implicit
30
- // negative id range −256…−1 — see store.ts). A one-byte form's halo is
31
- // promiscuous (it keeps company with everything it ever neighboured), so
32
- // admitting it as a voice or a revoicing target lets e.g. the digit "4"
33
- // be "re-voiced" into an unrelated digit the query happens to contain.
34
- // Articulation therefore operates on multi-byte forms only.
35
- if (s.payload < 0)
36
- continue;
37
- const h = store.halo(s.payload);
38
- if (!h)
39
- continue;
40
- qCandidates.push({
41
- start: s.start,
42
- end: s.end,
43
- payload: {
44
- halo: h,
45
- bytes: query.slice(s.start, s.end),
46
- node: s.payload,
47
- },
48
- });
49
- }
50
- if (qCandidates.length === 0) {
51
- return keep("no asker concept to revoice — answer unchanged");
52
- }
53
- if (coverSequence(query.length, qCandidates).spans.length === 0) {
54
- return keep("no asker concept to revoice — answer unchanged");
55
- }
56
- const voices = qCandidates.map((s) => s.payload);
57
- const ans = recognise(ctx, answer);
58
- const substitutions = new Map();
59
- for (const s of ans.sites) {
60
- // Same single-byte-leaf exclusion as the query loop above.
61
- if (s.payload < 0)
62
- continue;
63
- const h = store.halo(s.payload);
64
- if (!h)
65
- continue;
66
- const found = bestHaloMate(ctx, h, voices, (v) => v.halo);
67
- if (!found)
68
- continue;
69
- const voice = found.item;
70
- if (voice.node === s.payload || contains(ctx, voice.node, s.payload)) {
71
- continue;
72
- }
73
- substitutions.set(s.payload, voice.bytes);
74
- }
75
- if (substitutions.size === 0) {
76
- return keep("no answer form is a synonym of an asker concept");
77
- }
78
- ctx.trace?.step("substitute", [...substitutions.keys()].map((n) => rNode(ctx, n, "answer-form")), [...substitutions.values()].map((b) => rItem(b, "asker-voice")), `revoice ${substitutions.size} answer form(s) in the asker's own words (synonym splice)`);
79
- const voicedSites = ans.sites.filter((s) => substitutions.has(s.payload));
80
- const tArtCover = ctx.trace?.enter("cover", [
81
- ...voicedSites.map((s) => rItem(answer.subarray(s.start, s.end), "form", s.payload, [
82
- s.start,
83
- s.end,
84
- ])),
85
- ]);
86
- const solved = ctx.search.cover(answer.length, voicedSites, new Map(), ans.leaves, ans.splits, ans.starts, substitutions, undefined, undefined, ctx.trace ? (steps) => traceDerivation(ctx, steps) : undefined);
87
- const segs = solved && solved.segs;
88
- tArtCover?.done(segs === null
89
- ? []
90
- : segs.map((s) => rItem(s.bytes, s.rec ? "voiced" : "kept", s.node, [s.i, s.j])), segs === null
91
- ? "the revoiced cover did not compose"
92
- : "lightest derivation of the revoiced answer");
93
- const result = segs && spliceAll(segs);
94
- if (result) {
95
- t?.done([rItem(result, "voiced", ctx.store.findLeaf(result.subarray(0, 1)) ?? undefined)], "splice the asker's wording into the answer where the same concept appears");
96
- return result;
97
- }
98
- return keep("the revoiced cover did not compose — answer unchanged");
99
- }
@@ -1,315 +0,0 @@
1
- import type { DerivationStep } from "./graph-search.js";
2
- import type { AncestorReach, Attention, AttentionRead, DFMode, MindContext, Region, RegionVote, SaturationInfo, SaturationStop } from "./types.js";
3
- import { type StructuralPart } from "../geometry.js";
4
- import { type JunctionSynonymSides } from "./junction.js";
5
- /** How the newly-added graded junction ladder (junction.ts / attention.ts's
6
- * {@link CrossRegionTier}) is reported on a `junctionVotes` entry. The
7
- * instrumentation spec this implements predates that ladder and only knew
8
- * two tiers ("exact" | "synonym"); with the richer `CrossRegionTier` now
9
- * the real shape of a junction vote's provenance, `junctionVotes[].tier`
10
- * reports it DIRECTLY (the tier as-is: "exact" | "single-synonym" |
11
- * "double-synonym" | "structural-resonance") rather than collapsing every
12
- * non-exact tier into a lossy "synonym" bucket — the whole point of
13
- * exposing `tier` here is to let a debugger tell a halo-sibling
14
- * substitution apart from a structural-resonance ANN guess, which the
15
- * spec's original two-value type cannot do. */
16
- export type ClimbConsensusJunctionTier = CrossRegionTier;
17
- export type RegionOutcome = "voted" | "no-ann-hit" | "no-structural-reach" | "saturated-abstention" | "nonpositive-df-weight" | "contrastive-margin-rejection";
18
- export interface ConsensusRegionTrace {
19
- index: number;
20
- source: "perceived" | "recognised";
21
- span: [number, number];
22
- chunk: boolean;
23
- known: boolean;
24
- canonicalId?: number;
25
- canonicalUsable: boolean;
26
- canonicalFailed: boolean;
27
- annQueried: boolean;
28
- annHitsReturned: number;
29
- annHitsExamined: number;
30
- selected?: {
31
- source: "canonical" | "ann";
32
- node: number;
33
- rank?: number;
34
- score: number;
35
- fallback?: "orphan" | "saturated-tie";
36
- };
37
- reachNode?: number;
38
- outcome: RegionOutcome;
39
- idf?: number;
40
- dfWeight?: number;
41
- contrastiveMargin?: number;
42
- contrastiveNoiseFloor?: number;
43
- mutualWeight?: number;
44
- voteWeightPerRoot?: number;
45
- focusWeightPerRoot?: number;
46
- ordinaryVoteProduced: boolean;
47
- superseded: boolean;
48
- }
49
- export interface ConsensusReachTrace {
50
- node: number;
51
- roots: number[];
52
- contextsReached: number;
53
- saturated: boolean;
54
- saturation?: SaturationStop;
55
- }
56
- export type AnchorRejectionReason = "below-natural-break" | "below-consensus-floor" | "leading-saturation";
57
- export interface ConsensusAnchorTrace {
58
- anchor: number;
59
- rank: number;
60
- pooledVote: number;
61
- idfVote: number;
62
- candidateBreadth: number;
63
- contributingVotes: number;
64
- contributingEvidence: number;
65
- breadth: number;
66
- contributingSpans: Array<[number, number]>;
67
- clusters: number;
68
- commit: {
69
- status: "root" | "overlap" | "rejected";
70
- dominant: boolean;
71
- passesNaturalBreak?: boolean;
72
- passesConsensusFloor?: boolean;
73
- pastLeadingSaturation?: boolean;
74
- rejectionReasons: AnchorRejectionReason[];
75
- };
76
- }
77
- export interface JunctionVoteTrace {
78
- container: number;
79
- span: [number, number];
80
- roots: number[];
81
- sourceRegionIndices: number[];
82
- explainedAwayRegionIndices: number[];
83
- absorbed: number;
84
- tier?: ClimbConsensusJunctionTier;
85
- }
86
- export interface ClimbConsensusData {
87
- version: 1;
88
- cache: {
89
- hit: boolean;
90
- detailAvailable: boolean;
91
- };
92
- config: {
93
- annK: number;
94
- crossRegionProbeLimit: number;
95
- mode: DFMode;
96
- corpusN?: number;
97
- dimension?: number;
98
- hubBound?: number;
99
- estimatorNoise?: number;
100
- naturalBreak?: number;
101
- consensusFloor?: number;
102
- };
103
- candidates: {
104
- perceived: number;
105
- recognised: number;
106
- total: number;
107
- };
108
- regions?: ConsensusRegionTrace[];
109
- reaches?: ConsensusReachTrace[];
110
- crossRegion?: {
111
- eligibleRegions: number;
112
- maximalRegions: number;
113
- probeLimit: number;
114
- probesAttempted: number;
115
- junctionVotes: JunctionVoteTrace[];
116
- supersededOrdinaryVotes: number;
117
- };
118
- saturation?: {
119
- regionIntervals: Array<{
120
- start: number;
121
- end: number;
122
- }>;
123
- hasLeading: boolean;
124
- leadingEnd: number;
125
- };
126
- pooling?: {
127
- inputVotes: number;
128
- eligibleVotes: number;
129
- saturationMaskedVotes: number;
130
- };
131
- anchors?: ConsensusAnchorTrace[];
132
- result: AttentionRead;
133
- }
134
- /** The mutable collection buffers threaded through one traced consensus
135
- * climb — allocated exactly once, in {@link computeAttention}, only when
136
- * `ctx.trace` is set. Every field mirrors a `ClimbConsensusData` array/map,
137
- * built incrementally as the pipeline runs so commit-time decisions (in
138
- * particular) are recorded LIVE, not reconstructed afterward. */
139
- interface TraceDraft {
140
- perceivedCount: number;
141
- regions: ConsensusRegionTrace[];
142
- crossRegionJunctionVotes: JunctionVoteTrace[];
143
- crossRegionSummary?: {
144
- eligibleRegions: number;
145
- maximalRegions: number;
146
- probeLimit: number;
147
- probesAttempted: number;
148
- };
149
- supersededOrdinaryVotes: number;
150
- saturation?: {
151
- regionIntervals: Array<{
152
- start: number;
153
- end: number;
154
- }>;
155
- hasLeading: boolean;
156
- leadingEnd: number;
157
- };
158
- pooling?: {
159
- inputVotes: number;
160
- eligibleVotes: number;
161
- saturationMaskedVotes: number;
162
- };
163
- anchors: ConsensusAnchorTrace[];
164
- }
165
- /** The config/corpus context {@link traceAttention} needs to fill in
166
- * `ClimbConsensusData.config` and `.result` at whichever exit fires —
167
- * threaded down from {@link computeAttention} rather than re-derived, so
168
- * every emission point reports the SAME numbers the real climb used. */
169
- interface ClimbConsensusCfg {
170
- k: number;
171
- mode: DFMode;
172
- perceivedCount: number;
173
- totalRegions: number;
174
- N?: number;
175
- reachMemo?: ReadonlyMap<number, AncestorReach>;
176
- naturalBreak?: number;
177
- consensusFloor?: number;
178
- }
179
- /** Climb the query's perceived byte regions up the structural DAG via
180
- * resonance, pool the evidence, and return only the ROOT points of
181
- * attention — those that cleared commitVotes' significance floor. */
182
- export declare function climbAttention(ctx: MindContext, query: Uint8Array, k: number, mode?: DFMode): Promise<Attention[]>;
183
- /** Full read-out of one consensus climb: both the roots (dominant points of
184
- * attention) and the entire ranked list. Cached via ctx.climbMemo, ALWAYS —
185
- * see {@link recognise} for why this memo (and recognise()'s own) must
186
- * never be skipped while tracing: computeAttention's collectRegions walks
187
- * the query's perceived tree via the same foldTree whose subtree-resolution
188
- * fast path makes a second call on identical bytes non-idempotent once
189
- * ctx._resolvedSubtrees is warm (which a multi-turn conversation's shared
190
- * prefix subtrees guarantee by the second turn). A cache hit still emits
191
- * a trace step — abbreviated, since the full per-sub-region voting detail
192
- * {@link traceAttention} builds isn't preserved by the cached read-out —
193
- * so a traced response is never silently blacked out for a repeated
194
- * query. */
195
- export declare function climbAttentionAll(ctx: MindContext, query: Uint8Array, k: number, mode?: DFMode): Promise<AttentionRead>;
196
- export declare function computeAttention(ctx: MindContext, query: Uint8Array, k: number, mode: DFMode): Promise<AttentionRead>;
197
- export declare function collectRegions(ctx: MindContext, query: Uint8Array): Region[];
198
- export declare function voteRegions(ctx: MindContext, query: Uint8Array, regions: readonly Region[], k: number, mode: DFMode, N: number, reachMemo?: Map<number, AncestorReach>, td?: TraceDraft): Promise<{
199
- votes: RegionVote[];
200
- saturated: boolean[];
201
- voters: Array<{
202
- id: number;
203
- score: number;
204
- w: number;
205
- } | null>;
206
- }>;
207
- /** The consensus vote as EVIDENCE POOLING, not shortest path: each surviving
208
- * region is an axiom; it contributes to every root it climbed to (or, for a
209
- * terminal answer node, to the contexts that lead to it) by a `combine:
210
- * "sum"` rule, so independent regions corroborating the same anchor ADD
211
- * rather than compete to be the cheapest route (see {@link Rule.combine} in
212
- * derive/src/deduction.ts). Run through the very engine {@link
213
- * GraphSearch} covers with — `lightestDerivation` — so a pooled-evidence
214
- * decision is, like a followed edge or a spliced connector, one weighted
215
- * rule of the SAME deduction system, not a separate hand-rolled tally that
216
- * merely logs alongside it. `votesIdf`/`support` are the same two
217
- * read-outs {@link commitVotes} always gated on; only how they accumulate
218
- * changed. */
219
- export declare function poolVotes(ctx: MindContext, regionVotes: readonly RegionVote[], sat: SaturationInfo, N: number, td?: TraceDraft): {
220
- votes: Map<number, number>;
221
- votesIdf: Map<number, number>;
222
- support: Map<number, {
223
- start: number;
224
- end: number;
225
- w: number;
226
- }>;
227
- /** Per-anchor SCALE-INVARIANT support: Σ RegionVote.absorbed over the
228
- * distinct contributing regions — see Attention.breadth. */
229
- regionSupport: Map<number, number>;
230
- /** Per-anchor contributing region spans — see Attention.clusters. */
231
- regionSpans: Map<number, Array<[number, number]>>;
232
- steps: DerivationStep[];
233
- };
234
- export declare function commitVotes(ctx: MindContext, pooled: {
235
- votes: Map<number, number>;
236
- votesIdf: Map<number, number>;
237
- support: Map<number, {
238
- start: number;
239
- end: number;
240
- w: number;
241
- }>;
242
- regionSupport: Map<number, number>;
243
- regionSpans: Map<number, Array<[number, number]>>;
244
- steps: DerivationStep[];
245
- }, sat: SaturationInfo, regions: readonly Region[], regionVoter: ReadonlyArray<{
246
- id: number;
247
- score: number;
248
- w: number;
249
- } | null>, N: number, td?: TraceDraft, cfg?: ClimbConsensusCfg): AttentionRead;
250
- export declare function detectSaturated(ctx: MindContext, regions: ReadonlyArray<{
251
- start: number;
252
- end: number;
253
- chunk?: boolean;
254
- }>, saturated: ReadonlyArray<boolean>): SaturationInfo;
255
- export declare function canonicalChunkId(ctx: MindContext, regionBytes: Uint8Array, N: number, reachMemo?: Map<number, AncestorReach>): number | null;
256
- export declare function naturalBreak(votes: number[]): number;
257
- export type CrossRegionTier = "exact" | "single-synonym" | "double-synonym" | "structural-resonance";
258
- export interface StructuralVariant {
259
- left: StructuralPart;
260
- right: StructuralPart;
261
- kind: "exact-exact" | "left-synonym" | "right-synonym" | "double-synonym";
262
- semanticConfidence: number;
263
- leftSiblingId?: number;
264
- rightSiblingId?: number;
265
- }
266
- export interface StructuralResonanceProposal {
267
- id: number;
268
- annScore: number;
269
- semanticConfidence: number;
270
- effectiveScore: number;
271
- variant: StructuralVariant["kind"];
272
- leftSiblingId?: number;
273
- rightSiblingId?: number;
274
- }
275
- /** Build, bound and order every mandatory structural variant (§7-8): the
276
- * exact/exact composition is always kept; up to `ctx.cfg.haloQueryK`
277
- * synonym variants (single- and double-synonym combined, one shared
278
- * budget) are appended, ordered by confidence, then kind, then sibling id. */
279
- export declare function buildStructuralVariants(ctx: MindContext, ra: Region, rb: Region, sides: JunctionSynonymSides): {
280
- variants: StructuralVariant[];
281
- exactLeft: StructuralPart;
282
- exactRight: StructuralPart;
283
- };
284
- /** The final approximate tier: compose every retained structural variant,
285
- * ANN-query each, merge proposals by candidate id, and validate the winner
286
- * through the SAME structural gates every other tier answers to (saturation,
287
- * roots, IDF, contrastive margin). Returns null when nothing survives. */
288
- export declare function structuralResonance(ctx: MindContext, query: Uint8Array, ra: Region, rb: Region, sides: JunctionSynonymSides, k: number, N: number, reachMemo: Map<number, AncestorReach>,
289
- /** Each side's OWN individual climb roots (from voteRegions), when it cast
290
- * one — the self-evidence backstop structural-resonance needs and the
291
- * exact tier gets for free from literal byte containment (§11's whole
292
- * premise: recover a JOINT context neither side votes for alone). A
293
- * candidate whose reach is exactly one side's own conclusion is not new
294
- * evidence of a joint whole; it is that side's resonance rediscovering
295
- * itself through a synthetic gist still dominated by its own direction. */
296
- ownRootsA: readonly number[] | undefined, ownRootsB: readonly number[] | undefined): Promise<{
297
- proposal: StructuralResonanceProposal;
298
- reach: AncestorReach;
299
- idf: number;
300
- } | null>;
301
- /** Emit the "climbConsensus" step — the human-readable note this always
302
- * produced, now paired (when `ctx.trace` and `cfg` are both present) with
303
- * the structured {@link ClimbConsensusData} payload on the SAME step's
304
- * `data` field. Every exit of {@link computeAttention} funnels through
305
- * here, so instrumentation and the existing rationale text can never drift
306
- * apart — see the instrumentation spec's §9 "every exit path". */
307
- export declare function traceAttention(ctx: MindContext, regions: ReadonlyArray<{
308
- start: number;
309
- end: number;
310
- }>, regionVoter: ReadonlyArray<{
311
- id: number;
312
- score: number;
313
- w: number;
314
- } | null>, roots: ReadonlyArray<Attention>, steps?: ReadonlyArray<DerivationStep>, td?: TraceDraft, cfg?: ClimbConsensusCfg, ranked?: ReadonlyArray<Attention>): void;
315
- export {};