@hviana/sema 0.1.5 → 0.1.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 (160) hide show
  1. package/AGENTS.md +6 -5
  2. package/CITATION.cff +49 -0
  3. package/HOW_IT_WORKS.md +11 -12
  4. package/README.md +7 -5
  5. package/dist/example/demo.js +25 -25
  6. package/dist/example/train_base.d.ts +19 -21
  7. package/dist/example/train_base.js +1687 -1952
  8. package/dist/src/alphabet.d.ts +3 -3
  9. package/dist/src/alphabet.js +24 -27
  10. package/dist/src/alu/src/alu.d.ts +169 -194
  11. package/dist/src/alu/src/alu.js +374 -414
  12. package/dist/src/alu/src/expr.d.ts +36 -43
  13. package/dist/src/alu/src/expr.js +252 -278
  14. package/dist/src/alu/src/index.d.ts +9 -98
  15. package/dist/src/alu/src/index.js +10 -64
  16. package/dist/src/alu/src/kernel-arith.d.ts +1 -5
  17. package/dist/src/alu/src/kernel-arith.js +195 -285
  18. package/dist/src/alu/src/kernel-bits.d.ts +1 -5
  19. package/dist/src/alu/src/kernel-bits.js +81 -115
  20. package/dist/src/alu/src/kernel-logic.js +33 -63
  21. package/dist/src/alu/src/kernel-nd.js +155 -195
  22. package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
  23. package/dist/src/alu/src/kernel-numeric.js +259 -331
  24. package/dist/src/alu/src/operation.d.ts +112 -132
  25. package/dist/src/alu/src/operation.js +149 -163
  26. package/dist/src/alu/src/parser.d.ts +168 -175
  27. package/dist/src/alu/src/parser.js +382 -421
  28. package/dist/src/alu/src/resonance.d.ts +16 -25
  29. package/dist/src/alu/src/resonance.js +49 -56
  30. package/dist/src/alu/src/text.d.ts +3 -7
  31. package/dist/src/alu/src/text.js +31 -37
  32. package/dist/src/alu/src/value.d.ts +14 -14
  33. package/dist/src/alu/src/value.js +150 -160
  34. package/dist/src/alu/test/alu.test.js +543 -656
  35. package/dist/src/bytes.d.ts +1 -5
  36. package/dist/src/bytes.js +33 -40
  37. package/dist/src/config.d.ts +99 -102
  38. package/dist/src/config.js +78 -83
  39. package/dist/src/derive/src/deduction.d.ts +60 -62
  40. package/dist/src/derive/src/deduction.js +103 -109
  41. package/dist/src/derive/src/index.d.ts +1 -7
  42. package/dist/src/derive/src/priority-queue.d.ts +8 -8
  43. package/dist/src/derive/src/priority-queue.js +57 -61
  44. package/dist/src/derive/src/rewrite.d.ts +15 -18
  45. package/dist/src/derive/src/rewrite.js +59 -67
  46. package/dist/src/derive/src/trie.d.ts +56 -56
  47. package/dist/src/derive/src/trie.js +175 -179
  48. package/dist/src/derive/test/derive.test.js +96 -100
  49. package/dist/src/extension.d.ts +13 -16
  50. package/dist/src/geometry.d.ts +17 -42
  51. package/dist/src/geometry.js +235 -266
  52. package/dist/src/index.d.ts +2 -17
  53. package/dist/src/index.js +2 -12
  54. package/dist/src/ingest-cache.d.ts +25 -30
  55. package/dist/src/ingest-cache.js +108 -127
  56. package/dist/src/mind/articulation.d.ts +1 -5
  57. package/dist/src/mind/articulation.js +77 -112
  58. package/dist/src/mind/attention.d.ts +40 -108
  59. package/dist/src/mind/attention.js +749 -871
  60. package/dist/src/mind/canonical.d.ts +4 -19
  61. package/dist/src/mind/canonical.js +27 -31
  62. package/dist/src/mind/graph-search.d.ts +201 -225
  63. package/dist/src/mind/graph-search.js +742 -821
  64. package/dist/src/mind/index.d.ts +2 -10
  65. package/dist/src/mind/junction.d.ts +31 -58
  66. package/dist/src/mind/junction.js +172 -237
  67. package/dist/src/mind/learning.d.ts +16 -52
  68. package/dist/src/mind/learning.js +143 -165
  69. package/dist/src/mind/match.d.ts +20 -69
  70. package/dist/src/mind/match.js +259 -318
  71. package/dist/src/mind/mechanisms/alu.js +16 -16
  72. package/dist/src/mind/mechanisms/cast.d.ts +9 -13
  73. package/dist/src/mind/mechanisms/cast.js +363 -456
  74. package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
  75. package/dist/src/mind/mechanisms/confluence.js +152 -183
  76. package/dist/src/mind/mechanisms/cover.d.ts +2 -8
  77. package/dist/src/mind/mechanisms/cover.js +148 -210
  78. package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
  79. package/dist/src/mind/mechanisms/extraction.js +234 -288
  80. package/dist/src/mind/mechanisms/recall.d.ts +6 -10
  81. package/dist/src/mind/mechanisms/recall.js +126 -185
  82. package/dist/src/mind/mind.d.ts +129 -154
  83. package/dist/src/mind/mind.js +258 -288
  84. package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
  85. package/dist/src/mind/pipeline-mechanism.js +161 -172
  86. package/dist/src/mind/pipeline.d.ts +4 -14
  87. package/dist/src/mind/pipeline.js +125 -189
  88. package/dist/src/mind/primitives.d.ts +8 -32
  89. package/dist/src/mind/primitives.js +99 -117
  90. package/dist/src/mind/rationale.d.ts +86 -98
  91. package/dist/src/mind/rationale.js +113 -121
  92. package/dist/src/mind/reasoning.d.ts +2 -13
  93. package/dist/src/mind/reasoning.js +129 -166
  94. package/dist/src/mind/recognition.d.ts +1 -4
  95. package/dist/src/mind/recognition.js +183 -208
  96. package/dist/src/mind/resonance.d.ts +5 -22
  97. package/dist/src/mind/resonance.js +0 -0
  98. package/dist/src/mind/trace.d.ts +6 -25
  99. package/dist/src/mind/trace.js +50 -58
  100. package/dist/src/mind/traverse.d.ts +16 -58
  101. package/dist/src/mind/traverse.js +304 -357
  102. package/dist/src/mind/types.d.ts +120 -127
  103. package/dist/src/mind/types.js +60 -69
  104. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  105. package/dist/src/rabitq-ivf/src/database.js +201 -0
  106. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  107. package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
  108. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  109. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  110. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  111. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  112. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  113. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  114. package/dist/src/sema.d.ts +9 -13
  115. package/dist/src/sema.js +26 -40
  116. package/dist/src/store-sqlite.d.ts +169 -166
  117. package/dist/src/store-sqlite.js +762 -662
  118. package/dist/src/store.d.ts +586 -630
  119. package/dist/src/store.js +1423 -1581
  120. package/dist/src/vec.d.ts +5 -9
  121. package/dist/src/vec.js +61 -73
  122. package/example/train_base.ts +13 -10
  123. package/package.json +1 -1
  124. package/src/alu/README.md +1 -1
  125. package/src/alu/src/index.ts +1 -1
  126. package/src/config.ts +19 -27
  127. package/src/index.ts +6 -11
  128. package/src/rabitq-ivf/README.md +56 -0
  129. package/src/rabitq-ivf/src/database.ts +276 -0
  130. package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
  131. package/src/rabitq-ivf/src/ivf.ts +1330 -0
  132. package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
  133. package/src/store-sqlite.ts +196 -9
  134. package/src/store.ts +8 -32
  135. package/test/08-storage.test.mjs +3 -3
  136. package/test/14-scaling.test.mjs +2 -2
  137. package/test/35-ivf.test.mjs +263 -0
  138. package/test/36-bloom.test.mjs +123 -0
  139. package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
  140. package/dist/src/rabitq-hnsw/src/database.js +0 -405
  141. package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
  142. package/dist/src/rabitq-hnsw/src/heap.js +0 -94
  143. package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
  144. package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
  145. package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
  146. package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
  147. package/dist/src/rabitq-hnsw/src/prng.js +0 -38
  148. package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
  149. package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
  150. package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
  151. package/dist/src/rabitq-hnsw/src/store.js +0 -916
  152. package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
  153. package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
  154. package/src/rabitq-hnsw/README.md +0 -303
  155. package/src/rabitq-hnsw/src/database.ts +0 -492
  156. package/src/rabitq-hnsw/src/heap.ts +0 -90
  157. package/src/rabitq-hnsw/src/hnsw.ts +0 -514
  158. package/src/rabitq-hnsw/src/store.ts +0 -994
  159. package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
  160. /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
@@ -12,72 +12,36 @@ import type { Input, MindContext } from "./types.js";
12
12
  * O(new nodes) per deposit instead of O(context). Only the hit node
13
13
  * itself enters `ids`; descendants stay reachable via the memo (see
14
14
  * idOf in indexSubSpans and the changedNodes prune). */
15
- export declare function internTreeIds(
16
- ctx: MindContext,
17
- node: Sema,
18
- ids: Map<Sema, number>,
19
- ): Promise<number>;
15
+ export declare function internTreeIds(ctx: MindContext, node: Sema, ids: Map<Sema, number>): Promise<number>;
20
16
  /** Index flat branches for sub-spans of a deposit's byte stream, linked to
21
17
  * their structural chunks via durable CONTAINMENT edges. */
22
- export declare function indexSubSpans(
23
- ctx: MindContext,
24
- tree: Sema,
25
- ids: Map<Sema, number>,
26
- ): Promise<boolean>;
18
+ export declare function indexSubSpans(ctx: MindContext, tree: Sema, ids: Map<Sema, number>): Promise<boolean>;
27
19
  /** Perceive, intern, and index a single input. Returns the perceived tree,
28
20
  * root id, id map, and the changed (new) subtrees for halo reinforcement. */
29
- export declare function deposit(
30
- ctx: MindContext,
31
- input: Input,
32
- track: boolean,
33
- ): Promise<{
34
- tree: Sema;
35
- rootId: number;
36
- ids: Map<Sema, number>;
37
- changed: Sema[];
21
+ export declare function deposit(ctx: MindContext, input: Input, track: boolean): Promise<{
22
+ tree: Sema;
23
+ rootId: number;
24
+ ids: Map<Sema, number>;
25
+ changed: Sema[];
38
26
  }>;
39
27
  /** Ingest a single input (a bare experience, no continuation). */
40
- export declare function ingestOne(
41
- ctx: MindContext,
42
- input: Input,
43
- ): Promise<
44
- Sema & {
28
+ export declare function ingestOne(ctx: MindContext, input: Input): Promise<Sema & {
45
29
  id: number;
46
- }
47
- >;
30
+ }>;
48
31
  /** Ingest a pair (context, continuation) — learn an edge and pour halos. */
49
- export declare function ingestPair(
50
- ctx: MindContext,
51
- ctxInput: Input,
52
- cont: Input,
53
- ): Promise<void>;
32
+ export declare function ingestPair(ctx: MindContext, ctxInput: Input, cont: Input): Promise<void>;
54
33
  /** Dispatch the public ingest input shapes onto one-input / pair handlers —
55
34
  * THE one reading of ingest's polymorphic surface (scalar, (context,
56
35
  * continuation) pair, or a list mixing bare inputs and pairs). Both ingest
57
36
  * paths — the direct one below and {@link CachedIngest} — route through
58
37
  * this, so the shape-detection can never drift between them again (the
59
38
  * ingest cache once re-implemented it and drifted). */
60
- export declare function dispatchIngest(
61
- input: Input | (Input | [Input, Input])[],
62
- second: Input | undefined,
63
- onOne: (input: Input) => Promise<
64
- Sema & {
65
- id: number;
66
- }
67
- >,
68
- onPair: (ctxInput: Input, cont: Input) => Promise<void>,
69
- ): Promise<
70
- (Sema & {
39
+ export declare function dispatchIngest(input: Input | (Input | [Input, Input])[], second: Input | undefined, onOne: (input: Input) => Promise<Sema & {
40
+ id: number;
41
+ }>, onPair: (ctxInput: Input, cont: Input) => Promise<void>): Promise<(Sema & {
71
42
  id: number;
72
- }) | undefined
73
- >;
43
+ }) | undefined>;
74
44
  /** Ingest an input or array of inputs/pairs. The public ingest entry point. */
75
- export declare function ingest(
76
- ctx: MindContext,
77
- input: Input | (Input | [Input, Input])[],
78
- second?: Input,
79
- ): Promise<
80
- (Sema & {
45
+ export declare function ingest(ctx: MindContext, input: Input | (Input | [Input, Input])[], second?: Input): Promise<(Sema & {
81
46
  id: number;
82
- }) | undefined
83
- >;
47
+ }) | undefined>;
@@ -20,166 +20,152 @@ import { fold as foldVecs } from "../sema.js";
20
20
  * itself enters `ids`; descendants stay reachable via the memo (see
21
21
  * idOf in indexSubSpans and the changedNodes prune). */
22
22
  export async function internTreeIds(ctx, node, ids) {
23
- const known = ctx._internIds.get(node);
24
- if (known !== undefined) {
25
- ids.set(node, known);
26
- return known;
27
- }
28
- let id;
29
- if (node.kids === null) {
30
- id = await ctx.store.putLeaf(node.leaf ?? new Uint8Array(0), node.v);
31
- } else {
32
- const kds = [];
33
- for (const k of node.kids) {
34
- kds.push(await internTreeIds(ctx, k, ids));
23
+ const known = ctx._internIds.get(node);
24
+ if (known !== undefined) {
25
+ ids.set(node, known);
26
+ return known;
35
27
  }
36
- id = await ctx.store.putBranch(kds, node.v);
37
- }
38
- ids.set(node, id);
39
- ctx._internIds.set(node, id);
40
- return id;
28
+ let id;
29
+ if (node.kids === null) {
30
+ id = await ctx.store.putLeaf(node.leaf ?? new Uint8Array(0), node.v);
31
+ }
32
+ else {
33
+ const kds = [];
34
+ for (const k of node.kids)
35
+ kds.push(await internTreeIds(ctx, k, ids));
36
+ id = await ctx.store.putBranch(kds, node.v);
37
+ }
38
+ ids.set(node, id);
39
+ ctx._internIds.set(node, id);
40
+ return id;
41
41
  }
42
42
  /** Index flat branches for sub-spans of a deposit's byte stream, linked to
43
43
  * their structural chunks via durable CONTAINMENT edges. */
44
44
  export async function indexSubSpans(ctx, tree, ids) {
45
- const chunkOf = [];
46
- const streamIds = [];
47
- const streamVecs = [];
48
- const collect = (n) => {
49
- if (isChunk(n)) {
50
- // A chunk inside a memo-skipped shared subtree is absent from `ids`;
51
- // the intern memo still knows it (same object). A miss on both (the
52
- // WeakMap entry was collected) only forfeits the seenBefore skip.
53
- const chunkId = ids.get(n) ?? ctx._internIds.get(n);
54
- for (const k of n.kids) {
55
- const lid = k.leaf ? ctx.store.findLeaf(k.leaf) : null;
56
- if (lid === null) {
57
- return false;
45
+ const chunkOf = [];
46
+ const streamIds = [];
47
+ const streamVecs = [];
48
+ const collect = (n) => {
49
+ if (isChunk(n)) {
50
+ // A chunk inside a memo-skipped shared subtree is absent from `ids`;
51
+ // the intern memo still knows it (same object). A miss on both (the
52
+ // WeakMap entry was collected) only forfeits the seenBefore skip.
53
+ const chunkId = ids.get(n) ?? ctx._internIds.get(n);
54
+ for (const k of n.kids) {
55
+ const lid = k.leaf ? ctx.store.findLeaf(k.leaf) : null;
56
+ if (lid === null)
57
+ return false;
58
+ streamIds.push(lid);
59
+ streamVecs.push(k.v);
60
+ chunkOf.push(chunkId);
61
+ }
62
+ return true;
58
63
  }
59
- streamIds.push(lid);
60
- streamVecs.push(k.v);
61
- chunkOf.push(chunkId);
62
- }
63
- return true;
64
- }
65
- if (n.kids) {
66
- for (const k of n.kids) {
67
- if (!collect(k)) {
68
- return false;
64
+ if (n.kids) {
65
+ for (const k of n.kids)
66
+ if (!collect(k))
67
+ return false;
69
68
  }
70
- }
71
- }
72
- return true;
73
- };
74
- if (!collect(tree)) {
75
- return false;
76
- }
77
- const W = ctx.space.maxGroup; // write side of the canonical contract
78
- const prev = ctx._prevSeen;
79
- const seenBefore = (off, len) => {
80
- if (!prev) {
81
- return false;
82
- }
83
- for (let i = off; i < off + len; i++) {
84
- const c = chunkOf[i];
85
- if (c === undefined || !prev.has(c)) {
69
+ return true;
70
+ };
71
+ if (!collect(tree))
86
72
  return false;
87
- }
88
- }
89
- return true;
90
- };
91
- const lens = streamIds.length >= W ? canonicalWindows(W) : [streamIds.length];
92
- for (const len of lens) {
93
- if (len < 1) {
94
- continue;
95
- }
96
- for (let off = 0; off + len <= streamIds.length; off++) {
97
- if (seenBefore(off, len)) {
98
- continue;
99
- }
100
- const winIds = streamIds.slice(off, off + len);
101
- const flatId = ctx.store.findBranch(winIds) ??
102
- await ctx.store.putBranch(
103
- winIds,
104
- foldVecs(ctx.space, streamVecs.slice(off, off + len)),
105
- );
106
- for (let i = off; i < off + len; i++) {
107
- const c = chunkOf[i];
108
- if (c !== undefined) {
109
- ctx.store.addContainer(flatId, c);
73
+ const W = ctx.space.maxGroup; // write side of the canonical contract
74
+ const prev = ctx._prevSeen;
75
+ const seenBefore = (off, len) => {
76
+ if (!prev)
77
+ return false;
78
+ for (let i = off; i < off + len; i++) {
79
+ const c = chunkOf[i];
80
+ if (c === undefined || !prev.has(c))
81
+ return false;
82
+ }
83
+ return true;
84
+ };
85
+ const lens = streamIds.length >= W ? canonicalWindows(W) : [streamIds.length];
86
+ for (const len of lens) {
87
+ if (len < 1)
88
+ continue;
89
+ for (let off = 0; off + len <= streamIds.length; off++) {
90
+ if (seenBefore(off, len))
91
+ continue;
92
+ const winIds = streamIds.slice(off, off + len);
93
+ const flatId = ctx.store.findBranch(winIds) ??
94
+ await ctx.store.putBranch(winIds, foldVecs(ctx.space, streamVecs.slice(off, off + len)));
95
+ for (let i = off; i < off + len; i++) {
96
+ const c = chunkOf[i];
97
+ if (c !== undefined)
98
+ ctx.store.addContainer(flatId, c);
99
+ }
110
100
  }
111
- }
112
101
  }
113
- }
114
- return true;
102
+ return true;
115
103
  }
116
104
  /** Perceive, intern, and index a single input. Returns the perceived tree,
117
105
  * root id, id map, and the changed (new) subtrees for halo reinforcement. */
118
106
  export async function deposit(ctx, input, track) {
119
- const bytes = inputBytes(ctx, input);
120
- // Deposit-shaped perception: stable-prefix tree SEEDING (see
121
- // perceiveDeposit) — an accumulated context re-folds only its new suffix,
122
- // O(turn) instead of O(context) per conversation turn. Cache-only here
123
- // (no store-probe fallback): a knownPrefixLength scan on every novel fact
124
- // would cost O(n²) hashing, while conversation replays are always warm —
125
- // re-deposition replays from the first turn, rebuilding the cache as it
126
- // goes.
127
- const tree = perceiveDeposit(ctx, bytes);
128
- const ids = new Map();
129
- const rootId = await internTreeIds(ctx, tree, ids);
130
- const indexed = await indexSubSpans(ctx, tree, ids);
131
- const leafIds = leafIdPrefix(ctx, bytes);
132
- if (leafIds.length === bytes.length && leafIds.length >= 2) {
133
- await ctx.store.putBranch(leafIds, tree.v);
134
- }
135
- const changed = (track && ctx._prevSeen)
136
- ? changedNodes(tree, ids, ctx._prevSeen)
137
- : [tree];
138
- if (track) {
139
- ctx._prevSeen = indexed ? new Set(ids.values()) : null;
140
- }
141
- return { tree, rootId, ids, changed };
107
+ const bytes = inputBytes(ctx, input);
108
+ // Deposit-shaped perception: stable-prefix tree SEEDING (see
109
+ // perceiveDeposit) — an accumulated context re-folds only its new suffix,
110
+ // O(turn) instead of O(context) per conversation turn. Cache-only here
111
+ // (no store-probe fallback): a knownPrefixLength scan on every novel fact
112
+ // would cost O(n²) hashing, while conversation replays are always warm —
113
+ // re-deposition replays from the first turn, rebuilding the cache as it
114
+ // goes.
115
+ const tree = perceiveDeposit(ctx, bytes);
116
+ const ids = new Map();
117
+ const rootId = await internTreeIds(ctx, tree, ids);
118
+ const indexed = await indexSubSpans(ctx, tree, ids);
119
+ const leafIds = leafIdPrefix(ctx, bytes);
120
+ if (leafIds.length === bytes.length && leafIds.length >= 2) {
121
+ await ctx.store.putBranch(leafIds, tree.v);
122
+ }
123
+ const changed = (track && ctx._prevSeen)
124
+ ? changedNodes(tree, ids, ctx._prevSeen)
125
+ : [tree];
126
+ if (track)
127
+ ctx._prevSeen = indexed ? new Set(ids.values()) : null;
128
+ return { tree, rootId, ids, changed };
142
129
  }
143
130
  /** Ingest a single input (a bare experience, no continuation). */
144
131
  export async function ingestOne(ctx, input) {
145
- const { tree, rootId, ids } = await deposit(ctx, input, true);
146
- ctx.store.indexTarget(rootId);
147
- const parts = tree.kids ? tree.kids.map((k) => ids.get(k)) : [rootId];
148
- const stride = ctx.space.maxGroup;
149
- if (parts.length > stride) {
150
- for (let i = 0; i + stride < parts.length; i += stride) {
151
- await ctx.store.link(parts[i], parts[i + stride]);
152
- }
153
- if ((parts.length - 1) % stride !== 0) {
154
- const lastStart = Math.floor((parts.length - 1) / stride) * stride;
155
- if (lastStart < parts.length - 1) {
156
- await ctx.store.link(parts[lastStart], parts[parts.length - 1]);
157
- }
132
+ const { tree, rootId, ids } = await deposit(ctx, input, true);
133
+ ctx.store.indexTarget(rootId);
134
+ const parts = tree.kids
135
+ ? tree.kids.map((k) => ids.get(k))
136
+ : [rootId];
137
+ const stride = ctx.space.maxGroup;
138
+ if (parts.length > stride) {
139
+ for (let i = 0; i + stride < parts.length; i += stride) {
140
+ await ctx.store.link(parts[i], parts[i + stride]);
141
+ }
142
+ if ((parts.length - 1) % stride !== 0) {
143
+ const lastStart = Math.floor((parts.length - 1) / stride) * stride;
144
+ if (lastStart < parts.length - 1) {
145
+ await ctx.store.link(parts[lastStart], parts[parts.length - 1]);
146
+ }
147
+ }
158
148
  }
159
- } else {
160
- for (const id of parts) {
161
- ctx.store.indexTarget(id);
149
+ else {
150
+ for (const id of parts)
151
+ ctx.store.indexTarget(id);
162
152
  }
163
- }
164
- return Object.assign(tree, { id: rootId });
153
+ return Object.assign(tree, { id: rootId });
165
154
  }
166
155
  /** Ingest a pair (context, continuation) — learn an edge and pour halos. */
167
156
  export async function ingestPair(ctx, ctxInput, cont) {
168
- const c = await deposit(ctx, ctxInput, true);
169
- const cont_ = await deposit(ctx, cont, false);
170
- const ctxId = c.rootId, contId = cont_.rootId;
171
- await ctx.store.link(ctxId, contId);
172
- // Halos pour company SIGNATURES (identity), not gists (content) — see
173
- // companySignature in sema.ts.
174
- const contSeat = bindSeat(ctx.space, companySignature(ctx.space, contId), 1);
175
- for (const part of c.changed) {
176
- const partId = c.ids.get(part);
177
- await ctx.store.pourHalo(partId, contSeat);
178
- await ctx.store.pourHalo(
179
- contId,
180
- bindSeat(ctx.space, companySignature(ctx.space, partId), 0),
181
- );
182
- }
157
+ const c = await deposit(ctx, ctxInput, true);
158
+ const cont_ = await deposit(ctx, cont, false);
159
+ const ctxId = c.rootId, contId = cont_.rootId;
160
+ await ctx.store.link(ctxId, contId);
161
+ // Halos pour company SIGNATURES (identity), not gists (content) — see
162
+ // companySignature in sema.ts.
163
+ const contSeat = bindSeat(ctx.space, companySignature(ctx.space, contId), 1);
164
+ for (const part of c.changed) {
165
+ const partId = c.ids.get(part);
166
+ await ctx.store.pourHalo(partId, contSeat);
167
+ await ctx.store.pourHalo(contId, bindSeat(ctx.space, companySignature(ctx.space, partId), 0));
168
+ }
183
169
  }
184
170
  /** Dispatch the public ingest input shapes onto one-input / pair handlers —
185
171
  * THE one reading of ingest's polymorphic surface (scalar, (context,
@@ -188,36 +174,28 @@ export async function ingestPair(ctx, ctxInput, cont) {
188
174
  * this, so the shape-detection can never drift between them again (the
189
175
  * ingest cache once re-implemented it and drifted). */
190
176
  export async function dispatchIngest(input, second, onOne, onPair) {
191
- if (
192
- Array.isArray(input) && !(input instanceof Uint8Array) &&
193
- input.width === undefined
194
- ) {
195
- const arr = input;
196
- if (arr.length === 2 && !Array.isArray(arr[0]) && !Array.isArray(arr[1])) {
197
- await onPair(arr[0], arr[1]);
198
- return undefined;
199
- }
200
- for (const item of arr) {
201
- if (Array.isArray(item) && item.length === 2) {
202
- await onPair(item[0], item[1]);
203
- } else {
204
- await onOne(item);
205
- }
177
+ if (Array.isArray(input) && !(input instanceof Uint8Array) &&
178
+ input.width === undefined) {
179
+ const arr = input;
180
+ if (arr.length === 2 && !Array.isArray(arr[0]) && !Array.isArray(arr[1])) {
181
+ await onPair(arr[0], arr[1]);
182
+ return undefined;
183
+ }
184
+ for (const item of arr) {
185
+ if (Array.isArray(item) && item.length === 2) {
186
+ await onPair(item[0], item[1]);
187
+ }
188
+ else
189
+ await onOne(item);
190
+ }
191
+ return undefined;
206
192
  }
193
+ if (second === undefined)
194
+ return onOne(input);
195
+ await onPair(input, second);
207
196
  return undefined;
208
- }
209
- if (second === undefined) {
210
- return onOne(input);
211
- }
212
- await onPair(input, second);
213
- return undefined;
214
197
  }
215
198
  /** Ingest an input or array of inputs/pairs. The public ingest entry point. */
216
199
  export async function ingest(ctx, input, second) {
217
- return dispatchIngest(
218
- input,
219
- second,
220
- (i) => ingestOne(ctx, i),
221
- (a, b) => ingestPair(ctx, a, b),
222
- );
200
+ return dispatchIngest(input, second, (i) => ingestOne(ctx, i), (a, b) => ingestPair(ctx, a, b));
223
201
  }
@@ -14,27 +14,17 @@ import type { Site } from "./graph-search.js";
14
14
  * (gate: identityBar — scale-aware).
15
15
  *
16
16
  * Returns the absolute byte position, or −1. */
17
- export declare function locate(
18
- ctx: MindContext,
19
- haystack: Uint8Array,
20
- needle: Uint8Array,
21
- fromPos: number,
22
- sites?: ReadonlyArray<Site>,
23
- ): number;
17
+ export declare function locate(ctx: MindContext, haystack: Uint8Array, needle: Uint8Array, fromPos: number, sites?: ReadonlyArray<Site>): number;
24
18
  /** The ALIGNED matcher: maximal literal matching runs between `query` and
25
19
  * `ct` (a learned context's bytes), by seed-and-extend over
26
20
  * `space.maxGroup`-sized n-gram seeds. Where locate() finds ONE position of
27
21
  * a short frame, this finds EVERY run two whole structures share — the
28
22
  * matcher CAST detects a woven query with. Returns non-overlapping runs
29
23
  * sorted by query position. */
30
- export declare function alignRuns(
31
- ctx: MindContext,
32
- query: Uint8Array,
33
- ct: Uint8Array,
34
- ): Array<{
35
- qs: number;
36
- qe: number;
37
- cs: number;
24
+ export declare function alignRuns(ctx: MindContext, query: Uint8Array, ct: Uint8Array): Array<{
25
+ qs: number;
26
+ qe: number;
27
+ cs: number;
38
28
  }>;
39
29
  /** A run from {@link alignGraded} — the ALIGNED matcher extended with the
40
30
  * same graded-evidence ladder as {@link locate}. Literal runs carry
@@ -44,10 +34,10 @@ export declare function alignRuns(
44
34
  * kind, so the substitution/redirection schemas work unchanged on conceptual
45
35
  * alignment. */
46
36
  export interface GradedRun {
47
- qs: number;
48
- qe: number;
49
- cs: number;
50
- weight: number;
37
+ qs: number;
38
+ qe: number;
39
+ cs: number;
40
+ weight: number;
51
41
  }
52
42
  /** The GRADED alignment matcher: extends literal W-gram alignment
53
43
  * ({@link alignRuns}) with halo-matched recognised sites in query regions
@@ -59,42 +49,23 @@ export interface GradedRun {
59
49
  * `querySites` are the pre-computed recognition sites for the query
60
50
  * (optional — when absent, only literal alignment fires and graded degrades
61
51
  * to the original behaviour). Context sites are recognised internally. */
62
- export declare function alignGraded(
63
- ctx: MindContext,
64
- query: Uint8Array,
65
- contextBytes: Uint8Array,
66
- querySites?: ReadonlyArray<Site>,
67
- ): GradedRun[];
52
+ export declare function alignGraded(ctx: MindContext, query: Uint8Array, contextBytes: Uint8Array, querySites?: ReadonlyArray<Site>): GradedRun[];
68
53
  /** The IN-LIST halo matcher: the best halo-mate for `halo` among EXPLICIT
69
54
  * candidates, above the concept threshold — the list counterpart of
70
55
  * {@link haloSiblings}, which asks the halo INDEX for candidates instead.
71
56
  * Behind locate()'s halo step and articulation's voice matching; a third
72
57
  * "best halo among these" decision must come here, not inline. */
73
- export declare function bestHaloMate<T>(
74
- ctx: MindContext,
75
- halo: Vec,
76
- items: Iterable<T>,
77
- haloOf: (item: T) => Vec | null | undefined,
78
- ): {
79
- item: T;
80
- score: number;
58
+ export declare function bestHaloMate<T>(ctx: MindContext, halo: Vec, items: Iterable<T>, haloOf: (item: T) => Vec | null | undefined): {
59
+ item: T;
60
+ score: number;
81
61
  } | null;
82
- export declare function haloSiblings(
83
- ctx: MindContext,
84
- id: number,
85
- halo?: Vec | null,
86
- bar?: number,
87
- ): Promise<Hit[]>;
62
+ export declare function haloSiblings(ctx: MindContext, id: number, halo?: Vec | null, bar?: number): Promise<Hit[]>;
88
63
  /** The DISTRIBUTIONAL matcher between two nodes: mutual-nearest-neighbour
89
64
  * strength, not a pick. Returns the direct halo cosine, or failing that the
90
65
  * highest mutual-halo-sibling min-score (second-order analogy), or failing
91
66
  * that the SHARED-FRAME strength (below) — the gate CAST's comparison
92
67
  * schema validates genuine analogs with (bar: significanceBar). */
93
- export declare function analogyStrength(
94
- ctx: MindContext,
95
- a: number,
96
- b: number,
97
- ): Promise<number>;
68
+ export declare function analogyStrength(ctx: MindContext, a: number, b: number): Promise<number>;
98
69
  /** The STRUCTURAL analogy tier: two nodes are analogs when their byte
99
70
  * streams share a LEARNT frame — a content-addressed flat form of at least
100
71
  * one full river window (W bytes, the perception quantum) that occurs in
@@ -107,28 +78,17 @@ export declare function analogyStrength(
107
78
  * a fraction, comparable to the cosine tiers above. Derived: the window
108
79
  * is maxGroup, the same quantum differsByOneWindow and canonicalChunkId
109
80
  * measure by; no tuned constants. */
110
- export declare function sharedFrameStrength(
111
- ctx: MindContext,
112
- a: number,
113
- b: number,
114
- ): number;
81
+ export declare function sharedFrameStrength(ctx: MindContext, a: number, b: number): number;
115
82
  /** FORWARD through a synonym: the continuation an edge-less node borrows from
116
83
  * a concept (halo) sibling — resonate the node's halo, take the first
117
84
  * sibling above the concept threshold that itself has a direct edge. */
118
- export declare function conceptHop(
119
- ctx: MindContext,
120
- id: number,
121
- ): Promise<number | null>;
85
+ export declare function conceptHop(ctx: MindContext, id: number): Promise<number | null>;
122
86
  /** FORWARD projection: follow continuation edges from a node to its fixpoint.
123
87
  * The first hop may cross a concept (halo) link — a synonym. The rest
124
88
  * follow direct edges. Convergence is intrinsic: the seen set guards
125
89
  * against cycles. `guide` disambiguates multi-continuation nodes by
126
90
  * resonance. */
127
- export declare function follow(
128
- ctx: MindContext,
129
- id: number,
130
- guide?: Vec | null,
131
- ): Promise<Uint8Array | null>;
91
+ export declare function follow(ctx: MindContext, id: number, guide?: Vec | null): Promise<Uint8Array | null>;
132
92
  /** REVERSE projection: the context a learnt continuation follows, voiced as
133
93
  * bytes. A common continuation ("Yes.") follows MANY contexts; with a
134
94
  * `guide` the context whose gist resonates with the query wins (seat
@@ -143,18 +103,9 @@ export declare function follow(
143
103
  * context reads empty (a zero-length context is no grounding: an empty
144
104
  * Uint8Array is truthy, and returning it would flow a hollow "answer"
145
105
  * onward). */
146
- export declare function reverseContext(
147
- ctx: MindContext,
148
- id: number,
149
- guide?: Vec | null,
150
- rev?: readonly number[],
151
- ): Uint8Array | null;
106
+ export declare function reverseContext(ctx: MindContext, id: number, guide?: Vec | null, rev?: readonly number[]): Uint8Array | null;
152
107
  /** THE projection: ground a matched node to answer bytes — FORWARD to its
153
108
  * continuation fixpoint (which may cross a concept hop), else REVERSE to
154
109
  * the context it follows. This is the direction ladder every mechanism's
155
110
  * final grounding step reduces to. */
156
- export declare function project(
157
- ctx: MindContext,
158
- id: number,
159
- guide?: Vec | null,
160
- ): Promise<Uint8Array | null>;
111
+ export declare function project(ctx: MindContext, id: number, guide?: Vec | null): Promise<Uint8Array | null>;