@hviana/sema 0.2.6 → 0.2.8

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/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 +202 -0
  56. package/dist/src/geometry.js +575 -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 +405 -0
  64. package/dist/src/mind/attention.js +1829 -0
  65. package/dist/src/mind/bridge.d.ts +30 -0
  66. package/dist/src/mind/bridge.js +569 -0
  67. package/dist/src/mind/canonical.d.ts +29 -0
  68. package/dist/src/mind/canonical.js +88 -0
  69. package/dist/src/mind/graph-search.d.ts +285 -0
  70. package/dist/src/mind/graph-search.js +956 -0
  71. package/dist/src/mind/index.d.ts +7 -0
  72. package/dist/src/mind/index.js +5 -0
  73. package/dist/src/mind/junction.d.ts +124 -0
  74. package/dist/src/mind/junction.js +317 -0
  75. package/dist/src/mind/learning.d.ts +47 -0
  76. package/dist/src/mind/learning.js +248 -0
  77. package/dist/src/mind/match.d.ts +126 -0
  78. package/dist/src/mind/match.js +441 -0
  79. package/dist/src/mind/mechanisms/alu.d.ts +4 -0
  80. package/dist/src/mind/mechanisms/alu.js +36 -0
  81. package/dist/src/mind/mechanisms/cast.d.ts +89 -0
  82. package/dist/src/mind/mechanisms/cast.js +716 -0
  83. package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
  84. package/dist/src/mind/mechanisms/confluence.js +225 -0
  85. package/dist/src/mind/mechanisms/cover.d.ts +6 -0
  86. package/dist/src/mind/mechanisms/cover.js +203 -0
  87. package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
  88. package/dist/src/mind/mechanisms/extraction.js +387 -0
  89. package/dist/src/mind/mechanisms/recall.d.ts +13 -0
  90. package/dist/src/mind/mechanisms/recall.js +325 -0
  91. package/dist/src/mind/mind.d.ts +276 -0
  92. package/dist/src/mind/mind.js +572 -0
  93. package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
  94. package/dist/src/mind/pipeline-mechanism.js +213 -0
  95. package/dist/src/mind/pipeline.d.ts +20 -0
  96. package/dist/src/mind/pipeline.js +217 -0
  97. package/dist/src/mind/primitives.d.ts +70 -0
  98. package/dist/src/mind/primitives.js +287 -0
  99. package/dist/src/mind/rationale.d.ts +139 -0
  100. package/dist/src/mind/rationale.js +163 -0
  101. package/dist/src/mind/reasoning.d.ts +34 -0
  102. package/dist/src/mind/reasoning.js +240 -0
  103. package/dist/src/mind/recognition.d.ts +20 -0
  104. package/dist/src/mind/recognition.js +390 -0
  105. package/dist/src/mind/resonance.d.ts +23 -0
  106. package/dist/src/mind/resonance.js +0 -0
  107. package/dist/src/mind/trace.d.ts +15 -0
  108. package/dist/src/mind/trace.js +73 -0
  109. package/dist/src/mind/traverse.d.ts +113 -0
  110. package/dist/src/mind/traverse.js +568 -0
  111. package/dist/src/mind/types.d.ts +289 -0
  112. package/dist/src/mind/types.js +130 -0
  113. package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
  114. package/dist/src/rabitq-ivf/src/database.js +201 -0
  115. package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
  116. package/dist/src/rabitq-ivf/src/index.js +4 -0
  117. package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
  118. package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
  119. package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
  120. package/dist/src/rabitq-ivf/src/prng.js +36 -0
  121. package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
  122. package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
  123. package/dist/src/sema.d.ts +31 -0
  124. package/dist/src/sema.js +63 -0
  125. package/dist/src/store-sqlite.d.ts +184 -0
  126. package/dist/src/store-sqlite.js +942 -0
  127. package/dist/src/store.d.ts +663 -0
  128. package/dist/src/store.js +1618 -0
  129. package/dist/src/vec.d.ts +31 -0
  130. package/dist/src/vec.js +109 -0
  131. package/package.json +1 -1
  132. package/src/mind/attention.ts +178 -63
  133. package/test/51-structural-resonance-ladder.test.mjs +6 -3
@@ -0,0 +1,23 @@
1
+ import type { MindContext } from "./types.js";
2
+ /** The connector that belongs BETWEEN two adjacent results — the graded
3
+ * junction ladder described in the module note above. Returns null when
4
+ * the graph holds no evidence that the two ever ran together. */
5
+ export declare function bridge(ctx: MindContext, left: Uint8Array, right: Uint8Array, interiorAllowance?: number): Promise<Uint8Array | null>;
6
+ /** Join two spans with the learnt connector between them, when one exists —
7
+ * the composition step every out-of-search assembly (multi-topic fusion,
8
+ * CAST's substitution and comparison) shares. A miss joins the pieces BARE
9
+ * and is never silent: it emits the same `bridgeMiss` trace step everywhere,
10
+ * so a degraded join is visible in the rationale regardless of which
11
+ * mechanism paid it. (The in-search connector splice in graph-search.ts is
12
+ * the same concept inside the deduction, where the join is a costed rule.) */
13
+ export declare function joinWithBridge(ctx: MindContext, left: Uint8Array, right: Uint8Array): Promise<Uint8Array>;
14
+ /** The pivot a produced answer bridges through: the longest UNCONSUMED learnt
15
+ * CONTEXT (a node bearing a continuation edge) whose bytes `answer` literally
16
+ * contains. Candidates are gathered by resonating the answer's sub-regions
17
+ * (breadth-first, leaves skipped, probes capped by branch count), then
18
+ * confirmed by exact byte containment — a near-resonance alone never hops. */
19
+ export declare function pivotInto(ctx: MindContext, answer: Uint8Array, consumed: ReadonlySet<number>): Promise<number | null>;
20
+ export declare function meaningOf(ctx: MindContext, bytes: Uint8Array, anchors: ReadonlyArray<{
21
+ name: string;
22
+ form: Uint8Array;
23
+ }>): Promise<string | null>;
Binary file
@@ -0,0 +1,15 @@
1
+ import type { MindContext } from "./types.js";
2
+ import type { DerivationStep } from "./graph-search.js";
3
+ import type { RationaleItem } from "./rationale.js";
4
+ export declare function rItem(bytes: Uint8Array, role?: string, node?: number, span?: [number, number]): RationaleItem;
5
+ export declare function rNode(ctx: MindContext, id: number, role?: string, score?: number): RationaleItem;
6
+ export declare function rDeriv(ctx: MindContext, it: DerivationStep["conclusion"], role?: string): RationaleItem;
7
+ /** The standard FALL-THROUGH closer every self-gating mechanism ends with:
8
+ * close the open scope with no outputs and the reason, and return null so
9
+ * the caller can `return fail("…")` in one expression. `t` is the scope an
10
+ * enclosing `ctx.trace?.enter(...)` returned (undefined when not tracing). */
11
+ export declare function traceFail(t: {
12
+ done(outputs: RationaleItem[], note?: string): void;
13
+ } | undefined): (note: string) => null;
14
+ export declare const MOVE_NOTE: Record<string, string>;
15
+ export declare function traceDerivation(ctx: MindContext, steps: ReadonlyArray<DerivationStep>): void;
@@ -0,0 +1,73 @@
1
+ // trace.ts — trace instrumentation + persistence (Section 9 of the mind).
2
+ //
3
+ // rItem, rNode, rDeriv — build RationaleItems from bytes/nodes/derivations
4
+ // traceDerivation — trace a full derivation proof tree
5
+ // MOVE_NOTE — human-readable names for each derivation move
6
+ import { read } from "./primitives.js";
7
+ import { decodeText } from "./rationale.js";
8
+ export function rItem(bytes, role, node, span) {
9
+ return {
10
+ text: decodeText(bytes),
11
+ role,
12
+ node: node ?? undefined,
13
+ span,
14
+ };
15
+ }
16
+ export function rNode(ctx, id, role, score) {
17
+ return {
18
+ text: decodeText(read(ctx, id)),
19
+ node: id,
20
+ role,
21
+ score,
22
+ };
23
+ }
24
+ export function rDeriv(ctx, it, role) {
25
+ const text = it.bytes
26
+ ? decodeText(it.bytes)
27
+ : it.node !== undefined
28
+ ? decodeText(read(ctx, it.node))
29
+ : it.kind === "cover"
30
+ ? `cover@${it.span[0]}`
31
+ : `[${it.span[0]},${it.span[1]})`;
32
+ return { text, role: role ?? it.kind, node: it.node, span: it.span };
33
+ }
34
+ /** The standard FALL-THROUGH closer every self-gating mechanism ends with:
35
+ * close the open scope with no outputs and the reason, and return null so
36
+ * the caller can `return fail("…")` in one expression. `t` is the scope an
37
+ * enclosing `ctx.trace?.enter(...)` returned (undefined when not tracing). */
38
+ export function traceFail(t) {
39
+ return (note) => {
40
+ t?.done([], note);
41
+ return null;
42
+ };
43
+ }
44
+ export const MOVE_NOTE = {
45
+ "follow-edge": "follow a learned continuation edge — 'what follows what'",
46
+ "concept-hop": "jump a concept (halo) link — a synonym's edge",
47
+ "voice": "emit the asker's own wording for this form (articulation)",
48
+ "ground": "a chain reached its terminal answer",
49
+ "splice-connector": "splice a learnt connector between two rewrites",
50
+ "split": "cut a span at a sub-leaf form boundary so a form can be reached",
51
+ "fuse": "fuse adjacent fragments toward a deeper learned form",
52
+ "recompose": "recompose fused parts into a learned whole that leads on",
53
+ "bridge": "advance the cover frontier across this span",
54
+ "pool-vote": "pool independent regions' evidence for a shared anchor (sum, not shortest path)",
55
+ "axiom": "a seed: a perceived leaf, recognised form, or computed result",
56
+ "step": "a derivation step",
57
+ };
58
+ export function traceDerivation(ctx, steps) {
59
+ const t = ctx.trace;
60
+ if (!t)
61
+ return;
62
+ const indexOfOrder = new Map();
63
+ for (const s of steps) {
64
+ const note = MOVE_NOTE[s.move] ?? s.move;
65
+ const deps = s.producers
66
+ .map((o) => indexOfOrder.get(o))
67
+ .filter((x) => x !== undefined);
68
+ const premises = s.premises.map((p) => rDeriv(ctx, p));
69
+ const conclusion = [rDeriv(ctx, s.conclusion)];
70
+ const index = t.step(s.move, premises, conclusion, s.cost > 0 ? `${note} (cost ${s.cost})` : note, deps.length > 0 ? deps : undefined);
71
+ indexOfOrder.set(s.order, index);
72
+ }
73
+ }
@@ -0,0 +1,113 @@
1
+ import { Vec } from "../vec.js";
2
+ import type { AncestorReach, MindContext } from "./types.js";
3
+ /** Climb the structural DAG from a node to its edge-bearing ancestor contexts.
4
+ * Ascent stops at hub nodes (parents > √N) — their reach is non-discriminative.
5
+ * When the start node has no structural parents, climbs from containment parents
6
+ * (sub-span flat branches inheriting their chunks' context).
7
+ *
8
+ * `memo`, when given, caches whole climbs by start id for the duration of ONE
9
+ * query (the store is read-only while a query is in flight, so a climb is a
10
+ * pure function of the id). The consensus pipeline climbs the SAME anchors
11
+ * repeatedly — regions sharing a chunk, and canonicalChunkId probing each
12
+ * chunk's prefixes — so without the memo every repeat re-pays the full
13
+ * fan-out reads. */
14
+ export declare function edgeAncestors(ctx: MindContext, id: number, contextCount: number, memo?: Map<number, AncestorReach>): AncestorReach;
15
+ /** Convenience: forward edges of a node. */
16
+ export declare function nextOf(ctx: MindContext, id: number): number[];
17
+ /** Convenience: reverse edges of a node. */
18
+ export declare function prevOf(ctx: MindContext, id: number): number[];
19
+ /** The uniform-expectation floor on a byte atom's corpus commonality: N
20
+ * learnt contexts, each at least one perception chunk of up to W of the 256
21
+ * possible byte values, contain a given atom in ≥ N·W/256 contexts on
22
+ * average. An atom's TRUE containment is unmeasurable (atoms carry no
23
+ * kid/contain links by construction), so this floor is the honest stand-in:
24
+ * derived entirely from the corpus scale N, the perception window W, and
25
+ * the alphabet size — never tuned. */
26
+ export declare function atomReach(ctx: MindContext, contextCount: number): number;
27
+ /** Whether a byte atom is a hub at this corpus scale — its commonality floor
28
+ * {@link atomReach} exceeds the hub bound √N. Below it (small stores) an
29
+ * atom votes and is recognised exactly as any stored form; above it the
30
+ * alphabet is scaffolding everywhere and abstains. */
31
+ export declare function atomIsHub(ctx: MindContext, contextCount: number): boolean;
32
+ /** Whether a node LEADS SOMEWHERE — it bears a continuation edge or a halo.
33
+ * The admission predicate recognition filters sites with (HOW_IT_WORKS
34
+ * §15.3): a form that leads nowhere contributes nothing to any derivation.
35
+ * Runs once per candidate span on the recognition hot path — `hasNext` is
36
+ * cached per response (the same flat-branch ids are probed across prefix
37
+ * variants by canonicalChunkId). `hasHalo` is not cached: it's a single
38
+ * indexed point probe per candidate, and the candidates that reach this
39
+ * check have already been filtered by hasNext above in edgeAncestors. */
40
+ export declare function leadsSomewhere(ctx: MindContext, id: number): boolean;
41
+ /** The structural IDF read of ONE node: how many distinct learnt contexts
42
+ * its containment/edge climb reaches, or Infinity when it reaches none or
43
+ * saturates (no usable identity evidence). The number every
44
+ * discriminative-vs-scaffolding decision derives from — paired with the
45
+ * half-dominance convention (geometry.dominates(reach, N)): content
46
+ * reaching a corpus MINORITY of contexts discriminates (an entity, a
47
+ * filler); content reaching a majority is frame scaffolding. */
48
+ export declare function reachOf(ctx: MindContext, id: number, contextCount: number, memo?: Map<number, AncestorReach>): number;
49
+ /** The corpus scale N — the count of DISTINCT learnt contexts, floored at 2
50
+ * so its derived readings (ln N in the consensus floor, √N in the hub bound)
51
+ * stay meaningful on a near-empty store. The one definition every consumer
52
+ * of "how big is this corpus?" reads. */
53
+ export declare function corpusN(ctx: MindContext): number;
54
+ /** The hub bound √N itself (≥ 2 always, since N is floored at 2) — for
55
+ * consumers that pass it to the store's LIMITed reads instead of capping a
56
+ * materialised list. {@link hubCap} is the list-side reading of the same
57
+ * convention. */
58
+ export declare function hubBound(ctx: MindContext): number;
59
+ /** Cap a candidate list at the hub bound √N (insertion order) — the ONE
60
+ * fan-out convention every walk and disambiguation uses (see HOW_IT_WORKS
61
+ * §8.6). A node connected to more than √N others is a hub whose individual
62
+ * connections carry ~no discriminative information; materialising or scoring
63
+ * them all would make single decisions scale with the corpus. */
64
+ export declare function hubCap<T>(ctx: MindContext, ids: readonly T[]): readonly T[];
65
+ /** Whether `descendant` lies within `ancestor`'s subtree — a structural DAG
66
+ * relation read off the hash-consed `kids` lists, by a bounded explicit-stack
67
+ * descent. Used by articulation to keep a voice from revoicing a fragment
68
+ * OF that voice. */
69
+ export declare function contains(ctx: MindContext, ancestor: number, descendant: number): boolean;
70
+ /** The best-scoring item by cosine against `query`, among items scoring at
71
+ * or above `threshold` — the shared arg-max every Pattern-A "which of these
72
+ * resonates best" decision reduces to. `strict` picks the tie-break a
73
+ * caller needs: `true` keeps the first-seen leader on a tie (`>`), the
74
+ * default lets a later equal score take it (`>=`). */
75
+ export declare function argmaxBy<T>(items: Iterable<T>, scoreOf: (item: T) => number, threshold: number, strict?: boolean): {
76
+ item: T;
77
+ score: number;
78
+ } | null;
79
+ export declare function argmaxCosine<T>(query: Vec, items: Iterable<T>, vecOf: (item: T) => Vec | null | undefined, threshold: number, strict?: boolean): {
80
+ item: T;
81
+ score: number;
82
+ } | null;
83
+ /** The guided-or-first continuation of a node, as answer-shaped bytes source:
84
+ * chooseNext under the response guide, falling back to the FIRST-inserted
85
+ * edge — the one no-guide convention chooseNext, project() and the search's
86
+ * formRules all share. undefined when the node has no continuation. */
87
+ export declare function guidedFirst(ctx: MindContext, id: number): number | undefined;
88
+ export declare function guidedNext(ctx: MindContext, node: number): number | undefined;
89
+ /** Disambiguate among a node's learnt continuations by distributional
90
+ * support. NOTE the `guide` contract: its VALUE is deliberately unused —
91
+ * only its PRESENCE gates disambiguation (a null guide means no query is in
92
+ * flight, so structural walkers keep plain first-edge behaviour). The
93
+ * gist-cosine of short answer candidates against a query guide is dominated
94
+ * by accidental byte-pattern correlations, not semantic relatedness, so the
95
+ * evidence consulted is structural: each candidate's reverse-edge support
96
+ * count (see below). Contrast {@link chooseAmong}, the REVERSE-direction
97
+ * disambiguator, whose candidates are whole learnt contexts — long enough
98
+ * that their perceived gists ARE semantically meaningful — and which
99
+ * therefore scores by guide cosine. The two directions consult different
100
+ * halves of the evidence on purpose. */
101
+ export declare function chooseNext(ctx: MindContext, id: number, guide?: Vec | null): number | undefined;
102
+ /** The perceived gist of a candidate node, through the session gist cache.
103
+ * Re-gisting a candidate is a full river fold of its bytes — the measured
104
+ * recall bottleneck (a hub context offers up to √N continuations, EACH
105
+ * re-perceived per pick). A node's bytes are immutable and perception is
106
+ * pure, so the cached gist is valid for the store's lifetime. Exported for
107
+ * every "score node ids against a guide" decision (chooseAmong here, the
108
+ * bridge's junction pick) so they share ONE cache and one convention. */
109
+ export declare function candidateGist(ctx: MindContext, c: number): Vec | null;
110
+ export declare function chooseAmong(ctx: MindContext, candidates: readonly number[], guide: Vec): {
111
+ id: number;
112
+ score: number;
113
+ };