@hviana/sema 0.1.9 → 0.2.1

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 (131) hide show
  1. package/dist/example/demo.d.ts +1 -0
  2. package/dist/example/demo.js +39 -0
  3. package/dist/example/train_base.d.ts +87 -0
  4. package/dist/example/train_base.js +2252 -0
  5. package/dist/src/alphabet.d.ts +7 -0
  6. package/dist/src/alphabet.js +33 -0
  7. package/dist/src/alu/src/alu.d.ts +185 -0
  8. package/dist/src/alu/src/alu.js +440 -0
  9. package/dist/src/alu/src/expr.d.ts +61 -0
  10. package/dist/src/alu/src/expr.js +318 -0
  11. package/dist/src/alu/src/index.d.ts +11 -0
  12. package/dist/src/alu/src/index.js +19 -0
  13. package/dist/src/alu/src/kernel-arith.d.ts +16 -0
  14. package/dist/src/alu/src/kernel-arith.js +264 -0
  15. package/dist/src/alu/src/kernel-bits.d.ts +19 -0
  16. package/dist/src/alu/src/kernel-bits.js +152 -0
  17. package/dist/src/alu/src/kernel-logic.d.ts +4 -0
  18. package/dist/src/alu/src/kernel-logic.js +60 -0
  19. package/dist/src/alu/src/kernel-nd.d.ts +3 -0
  20. package/dist/src/alu/src/kernel-nd.js +208 -0
  21. package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
  22. package/dist/src/alu/src/kernel-numeric.js +366 -0
  23. package/dist/src/alu/src/operation.d.ts +168 -0
  24. package/dist/src/alu/src/operation.js +189 -0
  25. package/dist/src/alu/src/parser.d.ts +221 -0
  26. package/dist/src/alu/src/parser.js +577 -0
  27. package/dist/src/alu/src/resonance.d.ts +55 -0
  28. package/dist/src/alu/src/resonance.js +126 -0
  29. package/dist/src/alu/src/text.d.ts +31 -0
  30. package/dist/src/alu/src/text.js +73 -0
  31. package/dist/src/alu/src/value.d.ts +109 -0
  32. package/dist/src/alu/src/value.js +300 -0
  33. package/dist/src/alu/test/alu.test.d.ts +1 -0
  34. package/dist/src/alu/test/alu.test.js +764 -0
  35. package/dist/src/bytes.d.ts +14 -0
  36. package/dist/src/bytes.js +59 -0
  37. package/dist/src/canon.d.ts +26 -0
  38. package/dist/src/canon.js +57 -0
  39. package/dist/src/config.d.ts +111 -0
  40. package/dist/src/config.js +91 -0
  41. package/dist/src/derive/src/deduction.d.ts +125 -0
  42. package/dist/src/derive/src/deduction.js +155 -0
  43. package/dist/src/derive/src/index.d.ts +7 -0
  44. package/dist/src/derive/src/index.js +11 -0
  45. package/dist/src/derive/src/priority-queue.d.ts +20 -0
  46. package/dist/src/derive/src/priority-queue.js +73 -0
  47. package/dist/src/derive/src/rewrite.d.ts +56 -0
  48. package/dist/src/derive/src/rewrite.js +100 -0
  49. package/dist/src/derive/src/trie.d.ts +90 -0
  50. package/dist/src/derive/src/trie.js +217 -0
  51. package/dist/src/derive/test/derive.test.d.ts +1 -0
  52. package/dist/src/derive/test/derive.test.js +122 -0
  53. package/dist/src/extension.d.ts +37 -0
  54. package/dist/src/extension.js +7 -0
  55. package/dist/src/geometry.d.ts +148 -0
  56. package/dist/src/geometry.js +511 -0
  57. package/dist/src/index.d.ts +16 -0
  58. package/dist/src/index.js +18 -0
  59. package/dist/src/ingest-cache.d.ts +41 -0
  60. package/dist/src/ingest-cache.js +161 -0
  61. package/dist/src/mind/articulation.d.ts +6 -0
  62. package/dist/src/mind/articulation.js +99 -0
  63. package/dist/src/mind/attention.d.ts +72 -0
  64. package/dist/src/mind/attention.js +898 -0
  65. package/dist/src/mind/canonical.d.ts +29 -0
  66. package/dist/src/mind/canonical.js +88 -0
  67. package/dist/src/mind/graph-search.d.ts +270 -0
  68. package/dist/src/mind/graph-search.js +927 -0
  69. package/dist/src/mind/index.d.ts +5 -0
  70. package/dist/src/mind/index.js +5 -0
  71. package/dist/src/mind/junction.d.ts +95 -0
  72. package/dist/src/mind/junction.js +262 -0
  73. package/dist/src/mind/learning.d.ts +47 -0
  74. package/dist/src/mind/learning.js +233 -0
  75. package/dist/src/mind/match.d.ts +113 -0
  76. package/dist/src/mind/match.js +446 -0
  77. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  78. package/dist/src/mind/mechanisms/alu.js +29 -0
  79. package/dist/src/mind/mechanisms/cast.d.ts +35 -0
  80. package/dist/src/mind/mechanisms/cast.js +461 -0
  81. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  82. package/dist/src/mind/mechanisms/confluence.js +225 -0
  83. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  84. package/dist/src/mind/mechanisms/cover.js +179 -0
  85. package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
  86. package/dist/src/mind/mechanisms/extraction.js +342 -0
  87. package/dist/src/mind/mechanisms/recall.d.ts +13 -0
  88. package/dist/src/mind/mechanisms/recall.js +235 -0
  89. package/dist/src/mind/mind.d.ts +273 -0
  90. package/dist/src/mind/mind.js +598 -0
  91. package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
  92. package/dist/src/mind/pipeline-mechanism.js +213 -0
  93. package/dist/src/mind/pipeline.d.ts +20 -0
  94. package/dist/src/mind/pipeline.js +185 -0
  95. package/dist/src/mind/primitives.d.ts +63 -0
  96. package/dist/src/mind/primitives.js +251 -0
  97. package/dist/src/mind/rationale.d.ts +134 -0
  98. package/dist/src/mind/rationale.js +162 -0
  99. package/dist/src/mind/reasoning.d.ts +15 -0
  100. package/dist/src/mind/reasoning.js +162 -0
  101. package/dist/src/mind/recognition.d.ts +20 -0
  102. package/dist/src/mind/recognition.js +246 -0
  103. package/dist/src/mind/resonance.d.ts +23 -0
  104. package/dist/src/mind/resonance.js +0 -0
  105. package/dist/src/mind/trace.d.ts +15 -0
  106. package/dist/src/mind/trace.js +73 -0
  107. package/dist/src/mind/traverse.d.ts +113 -0
  108. package/dist/src/mind/traverse.js +488 -0
  109. package/dist/src/mind/types.d.ts +186 -0
  110. package/dist/src/mind/types.js +84 -0
  111. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  112. package/dist/src/rabitq-ivf/src/database.js +201 -0
  113. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  114. package/dist/src/rabitq-ivf/src/index.js +4 -0
  115. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  116. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  117. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  118. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  119. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  120. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  121. package/dist/src/sema.d.ts +31 -0
  122. package/dist/src/sema.js +63 -0
  123. package/dist/src/store-sqlite.d.ts +184 -0
  124. package/dist/src/store-sqlite.js +942 -0
  125. package/dist/src/store.d.ts +654 -0
  126. package/dist/src/store.js +1612 -0
  127. package/dist/src/vec.d.ts +31 -0
  128. package/dist/src/vec.js +109 -0
  129. package/package.json +1 -1
  130. package/src/mind/graph-search.ts +128 -46
  131. package/src/mind/mind.ts +37 -0
@@ -0,0 +1,37 @@
1
+ /** A half-open byte span of a query an extension computed, and the canonical
2
+ * result bytes it is authoritative for. */
3
+ export interface ComputedSpan {
4
+ i: number;
5
+ j: number;
6
+ bytes: Uint8Array;
7
+ }
8
+ /** The generic capabilities the mind lends every extension — nothing here
9
+ * names any particular extension, and every member is machinery the mind
10
+ * already has:
11
+ *
12
+ * MEANING (resonance and grounding — the same mechanisms recall uses):
13
+ * • meaningOf — which of some labelled forms does a span mean? Pure gist
14
+ * nearness; the extension supplies its own vocabulary and gives the answer
15
+ * its own reading.
16
+ * • continuation — the grounded form the corpus continues a form to (the
17
+ * continuation fixpoint recall grounds answers with), or null when the
18
+ * form leads nowhere. The mind answers only "where does this form lead?";
19
+ * what that continuation MEANS is the extension's business.
20
+ *
21
+ * GEOMETRY (the perception tree's own structure):
22
+ * • segment — coherent runs by the alphabet-space merge the perception tree
23
+ * uses, so an extension's notion of "separator" is the learnt geometry's.
24
+ * • reach — the river's grouping capacity (maxGroup), bounding how far apart
25
+ * two spans may sit and still be read as one construction. */
26
+ export interface ExtensionHost {
27
+ meaningOf(bytes: Uint8Array, anchors: ReadonlyArray<{
28
+ name: string;
29
+ form: Uint8Array;
30
+ }>): Promise<string | null>;
31
+ continuation(bytes: Uint8Array): Promise<Uint8Array | null>;
32
+ segment(bytes: Uint8Array): Array<{
33
+ i: number;
34
+ j: number;
35
+ }>;
36
+ reach: number;
37
+ }
@@ -0,0 +1,7 @@
1
+ // extension.ts — shared types for computational extensions.
2
+ //
3
+ // Extensions (ALU, user computation packs) implement the PipelineMechanism
4
+ // interface (pipeline-mechanism.ts) — this file holds only the types that
5
+ // both the extension author and the pipeline need, without coupling either
6
+ // to a specific mechanism.
7
+ export {};
@@ -0,0 +1,148 @@
1
+ import { Sema, Space } from "./sema.js";
2
+ import { Alphabet } from "./alphabet.js";
3
+ /** The store's geometric identity bar: cosine ≥ 1 − 1/√D is the similarity at
4
+ * which `intern` already treats two gists as the SAME node. Recall reuses it
5
+ * to accept a near-identical query, and the climb to accept a containing form —
6
+ * one derived constant, never a tuned threshold. NOTE: this fixed bar is
7
+ * the ESTIMATOR floor of an identity claim; a whole-span claim over a span
8
+ * longer than the perception quantum must use the scale-aware
9
+ * {@link identityBar}, which converts the tolerated fraction into bytes. */
10
+ export declare function mergeThreshold(D: number): number;
11
+ /** The scale-aware IDENTITY bar for a whole-span resonance claim over a span
12
+ * of `len` bytes. Under the linear fold a cosine reads "fraction of aligned
13
+ * shared bytes", so a FIXED cosine bar admits a byte budget that grows with
14
+ * the span: 1 − 1/√D over a 4·√D-byte span tolerates four whole river
15
+ * windows of foreign content while still claiming "near-identical". An
16
+ * identity claim may tolerate at most ONE river window W — the perception
17
+ * quantum, the same single-window budget near-dedup's differsByOneWindow
18
+ * grants — so the bar is 1 − W/len, floored at mergeThreshold(D), below
19
+ * which the RaBitQ estimator cannot certify identity anyway. This is the
20
+ * angle+magnitude form of the identity test: the ANGLE carries the shared
21
+ * fraction, the span's MAGNITUDE (√len, the linear fold's own norm) converts
22
+ * the tolerated fraction into tolerated bytes. Derived from W, D and the
23
+ * span; never tuned. */
24
+ export declare function identityBar(D: number, maxGroup: number, len: number): number;
25
+ /** The reach bar: half a river quantum, derived from the fold's own geometry.
26
+ * A branch folds up to `maxGroup` children, so two forms that differ in ONE
27
+ * whole child — the smallest distinction perception can mean — sit at cosine
28
+ * ≈ 1 − 1/maxGroup. Half that quantum, 1 − 1/(2·maxGroup), is closer than any
29
+ * single-child difference can be: a positional echo of the same content.
30
+ *
31
+ * Recall uses this as its confidence floor: a query whose nearest resonant
32
+ * form sits below this bar is structurally unrelated to everything in the store
33
+ * — further than any single-child variant — and the system returns null rather
34
+ * than fabricate an answer from an unrelated form. Derived, never tuned. */
35
+ export declare function reachThreshold(maxGroup: number): number;
36
+ /** The estimator's own noise floor: 1/√D — ONE standard deviation of the
37
+ * cosine between two independent random vectors in D dimensions (the same σ
38
+ * {@link significanceBar} takes three of). It is the smallest difference in
39
+ * cosine that is distinguishable from the rotation-uniformised RaBitQ
40
+ * estimation error (see the MEASUREMENT CAVEAT above): a contrastive margin
41
+ * below it is quantisation noise, not evidence. The consensus climb gates a
42
+ * region's vote on its discriminative margin clearing this floor — the
43
+ * minimal "above noise" bar, one σ, not the stricter 3σ relatedness bar.
44
+ * Derived, never tuned. */
45
+ export declare function estimatorNoise(D: number): number;
46
+ /** The statistical-significance bar for whole-query resonance: 3/√D.
47
+ * In D dimensions the expected cosine of two independent random vectors is 0
48
+ * with standard deviation 1/√D. A cosine ≥ 3/√D is three standard deviations
49
+ * above chance — the query is statistically related to the store, not merely
50
+ * sharing random byte noise. Below this bar the consensus climb (which trusts
51
+ * sub-region resonance) is skipped: there is no evidence the query belongs to
52
+ * the same distribution as the stored content. Derived, never tuned. */
53
+ export declare function significanceBar(D: number): number;
54
+ /** The concept (halo) threshold: the cosine above which two nodes share a
55
+ * distributional concept. A halo is a superposition of episode signatures in
56
+ * D-dimensional space, so the expected cosine between two unrelated halos is 0
57
+ * with standard deviation 1/√D. The structural midpoint 0.5 separates "more
58
+ * similar than not" from noise; the +0.5/√D term adds one half-sigma margin
59
+ * that vanishes as D → ∞, accounting for the wider noise band at lower D
60
+ * without inventing a tuned constant. At D=1024 this gives 0.516, within
61
+ * 3% of 0.5 — existing behavior is preserved while threshold and D move
62
+ * together. Derived, never tuned. */
63
+ export declare function conceptThreshold(D: number): number;
64
+ /** The HALF-DOMINANCE predicate: whether a part covering `partLen` of a
65
+ * whole of `wholeLen` covers STRICTLY more than half of it. A span that
66
+ * dominates its whole can no longer discriminate the whole's own content —
67
+ * the one test behind liftAnswer's keep-the-frame rule, collectRegions'
68
+ * wrapper exclusion, and CAST's frame-depth majority (each cites this).
69
+ * CAST's frame-FRACTION gate is the deliberately CLOSED variant (≥ ½ is
70
+ * already unusable there) and stays inline where it is documented.
71
+ * Derived from the structural midpoint, never tuned. */
72
+ export declare function dominates(partLen: number, wholeLen: number): boolean;
73
+ /** The consensus-vote significance floor: ln(N) + 1/2, where N is the number
74
+ * of learnt contexts (edge sources). A single region's IDF-weighted vote for
75
+ * an anchor reached through c contexts is at most ln(N/c) ≤ ln(N); the +1/2
76
+ * demands the pooled vote exceed what ONE maximally-specific region could
77
+ * contribute by half a unit — i.e. genuine corroboration beyond a lone
78
+ * region's echo at this corpus scale. The ONE floor both consumers gate on:
79
+ * recallByResonance trusting a climb anchor, and commitVotes admitting a
80
+ * further point of attention. Defined once here so the two can never
81
+ * drift apart. Derived from N, never tuned. */
82
+ export declare function consensusFloor(N: number): number;
83
+ /** The coverage bar for the reach (interior) index, when vector-similarity
84
+ * gating is used. Returns the concept threshold — the structural midpoint
85
+ * (~0.5 at D=1024) where two forms are "more similar than not."
86
+ *
87
+ * Currently UNUSED in the hot training path: interior nodes are indexed
88
+ * unconditionally (hash-cons dedup bounds the index naturally).
89
+ * Post-hoc structural compaction ({@link Store.compactContentIndex})
90
+ * replaces runtime coverage gating with a batch pass that removes
91
+ * structurally-isolated entries. Derived, never tuned. */
92
+ export declare function coverageBar(_maxGroup: number, D: number): number;
93
+ export interface Grid {
94
+ width: number;
95
+ height: number;
96
+ channels: number;
97
+ data: Uint8Array;
98
+ dims?: number[];
99
+ }
100
+ /** Find the longest prefix of `bytes` whose leaf-id signature matches a
101
+ * known branch via `lookup`. Returns the byte-length of that prefix, or 0. */
102
+ export declare function knownPrefixLength(bytes: Uint8Array, leafAt: (i: number) => number | null, lookup: (leafIds: number[]) => number | null): number;
103
+ /** Bytes → Sema tree. `leafAt` and `lookup` are store capabilities for
104
+ * detecting previously-stored prefixes so the river can split at the
105
+ * correct boundary. Pass them through from `perceive`; the geometry
106
+ * computes the stable prefix internally.
107
+ *
108
+ * `boundaries` is the CALLER-computed stable-prefix boundary set (§10.3):
109
+ * strictly-increasing proper byte offsets, each the length of a prefix that
110
+ * is already a stored whole-stream form. When given, the fold splits into
111
+ * the segments between consecutive boundaries — each folded independently,
112
+ * exactly as it folded when it was learned — and the segment roots join
113
+ * LEFT-NESTED (((s₀·s₁)·s₂)…), so every learnt cumulative-context root
114
+ * reappears as an identical subtree (and, by hash-consing, the very same
115
+ * node) inside the grown stream. This is what lets a conversation's next
116
+ * turn extend perception instead of refolding it: identical prefixes
117
+ * produce identical subtrees regardless of what follows them. */
118
+ export declare function bytesToTree(space: Space, alphabet: Alphabet, bytes: Uint8Array, leafAt?: (i: number) => number | null, lookup?: (leafIds: number[]) => number | null, boundaries?: readonly number[]): Sema;
119
+ /** The PLAIN fold's full level pyramid — every level's item list, bottom
120
+ * (leaves) to top (root). Left-grouped folding is RADIX-ALIGNED: the item
121
+ * at level L, index i, covers exactly bytes [i·mg^L, (i+1)·mg^L) whenever
122
+ * it is a FULL block, and a full block folds bit-identically in ANY byte
123
+ * string that contains it at that offset. So a string extended by a
124
+ * suffix (a conversation's accumulated context) reuses every full block of
125
+ * its prefix's pyramid and refolds only the right edge of each level —
126
+ * O(suffix + depth·mg) per extension instead of O(whole), with the
127
+ * produced tree BIT-IDENTICAL to a from-scratch plain fold (same nodes,
128
+ * same FP ops; reused subtrees are shared objects, and Sema nodes are
129
+ * never mutated). Purely an implementation cache: structure and numerics
130
+ * never depend on whether a pyramid was available. */
131
+ export interface FoldPyramid {
132
+ levels: Array<Array<{
133
+ tree: Sema;
134
+ len: number;
135
+ }>>;
136
+ bytes: number;
137
+ }
138
+ /** Plain bytes→tree (identical to capability-less {@link bytesToTree}) that
139
+ * also RETURNS its pyramid, reusing `prev` — the pyramid of a PROPER
140
+ * prefix of `bytes` (caller guarantees content match and
141
+ * prev.bytes < bytes.length). */
142
+ export declare function bytesToTreePyramid(space: Space, alphabet: Alphabet, bytes: Uint8Array, prev?: FoldPyramid): {
143
+ tree: Sema;
144
+ pyramid: FoldPyramid;
145
+ };
146
+ export declare function hilbertBytes(grid: Grid): Uint8Array;
147
+ export declare function gridToTree(space: Space, alphabet: Alphabet, grid: Grid): Sema;
148
+ export declare function stackGrids(frames: Grid[]): Grid;