@hviana/sema 0.1.8 → 0.1.9

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/package.json +1 -1
  2. package/src/geometry.ts +14 -30
  3. package/src/mind/mechanisms/cast.ts +12 -7
  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 -148
  59. package/dist/src/geometry.js +0 -529
  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 -72
  67. package/dist/src/mind/attention.js +0 -898
  68. package/dist/src/mind/canonical.d.ts +0 -29
  69. package/dist/src/mind/canonical.js +0 -88
  70. package/dist/src/mind/graph-search.d.ts +0 -270
  71. package/dist/src/mind/graph-search.js +0 -847
  72. package/dist/src/mind/index.d.ts +0 -5
  73. package/dist/src/mind/index.js +0 -5
  74. package/dist/src/mind/junction.d.ts +0 -95
  75. package/dist/src/mind/junction.js +0 -262
  76. package/dist/src/mind/learning.d.ts +0 -47
  77. package/dist/src/mind/learning.js +0 -233
  78. package/dist/src/mind/match.d.ts +0 -113
  79. package/dist/src/mind/match.js +0 -446
  80. package/dist/src/mind/mechanisms/alu.d.ts +0 -4
  81. package/dist/src/mind/mechanisms/alu.js +0 -29
  82. package/dist/src/mind/mechanisms/cast.d.ts +0 -35
  83. package/dist/src/mind/mechanisms/cast.js +0 -461
  84. package/dist/src/mind/mechanisms/confluence.d.ts +0 -24
  85. package/dist/src/mind/mechanisms/confluence.js +0 -225
  86. package/dist/src/mind/mechanisms/cover.d.ts +0 -6
  87. package/dist/src/mind/mechanisms/cover.js +0 -179
  88. package/dist/src/mind/mechanisms/extraction.d.ts +0 -67
  89. package/dist/src/mind/mechanisms/extraction.js +0 -342
  90. package/dist/src/mind/mechanisms/recall.d.ts +0 -13
  91. package/dist/src/mind/mechanisms/recall.js +0 -235
  92. package/dist/src/mind/mind.d.ts +0 -273
  93. package/dist/src/mind/mind.js +0 -562
  94. package/dist/src/mind/pipeline-mechanism.d.ts +0 -142
  95. package/dist/src/mind/pipeline-mechanism.js +0 -213
  96. package/dist/src/mind/pipeline.d.ts +0 -20
  97. package/dist/src/mind/pipeline.js +0 -185
  98. package/dist/src/mind/primitives.d.ts +0 -63
  99. package/dist/src/mind/primitives.js +0 -251
  100. package/dist/src/mind/rationale.d.ts +0 -134
  101. package/dist/src/mind/rationale.js +0 -162
  102. package/dist/src/mind/reasoning.d.ts +0 -15
  103. package/dist/src/mind/reasoning.js +0 -162
  104. package/dist/src/mind/recognition.d.ts +0 -20
  105. package/dist/src/mind/recognition.js +0 -246
  106. package/dist/src/mind/resonance.d.ts +0 -23
  107. package/dist/src/mind/resonance.js +0 -0
  108. package/dist/src/mind/trace.d.ts +0 -15
  109. package/dist/src/mind/trace.js +0 -73
  110. package/dist/src/mind/traverse.d.ts +0 -113
  111. package/dist/src/mind/traverse.js +0 -488
  112. package/dist/src/mind/types.d.ts +0 -186
  113. package/dist/src/mind/types.js +0 -84
  114. package/dist/src/rabitq-ivf/src/database.d.ts +0 -113
  115. package/dist/src/rabitq-ivf/src/database.js +0 -201
  116. package/dist/src/rabitq-ivf/src/index.d.ts +0 -7
  117. package/dist/src/rabitq-ivf/src/index.js +0 -4
  118. package/dist/src/rabitq-ivf/src/ivf.d.ts +0 -200
  119. package/dist/src/rabitq-ivf/src/ivf.js +0 -1165
  120. package/dist/src/rabitq-ivf/src/prng.d.ts +0 -19
  121. package/dist/src/rabitq-ivf/src/prng.js +0 -36
  122. package/dist/src/rabitq-ivf/src/rabitq.d.ts +0 -95
  123. package/dist/src/rabitq-ivf/src/rabitq.js +0 -283
  124. package/dist/src/sema.d.ts +0 -31
  125. package/dist/src/sema.js +0 -63
  126. package/dist/src/store-sqlite.d.ts +0 -184
  127. package/dist/src/store-sqlite.js +0 -942
  128. package/dist/src/store.d.ts +0 -654
  129. package/dist/src/store.js +0 -1612
  130. package/dist/src/vec.d.ts +0 -31
  131. package/dist/src/vec.js +0 -109
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hviana/sema",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Sema: a non-parametric, instance-based reasoning system.",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",
package/src/geometry.ts CHANGED
@@ -189,8 +189,13 @@ export interface Grid {
189
189
  /** Fold `items[start .. start+count)` in groups of `mg` into `out`.
190
190
  *
191
191
  * With `force`, the trailing incomplete group (2..mg-1 items) is folded as
192
- * well — only a lone singleton passes through guaranteeing progress when
193
- * the caller detected a stall. */
192
+ * well — only a lone singleton passes through. The river always folds with
193
+ * force: every level contracts by ~mg, so the tree's DEPTH is a function of
194
+ * ceil(log_mg(n)) alone. Letting leftovers pass through unfolded made depth
195
+ * depend on the exact byte count (39 bytes folded in 3 levels, 41 in 4), and
196
+ * each extra level applies another seat permutation to the whole gist —
197
+ * near-identical inputs straddling such a cliff read as orthogonal
198
+ * (measured: 33-byte-identical prefixes at cos ≈ 0). */
194
199
  function foldSlice(
195
200
  space: Space,
196
201
  items: Folded[],
@@ -251,19 +256,12 @@ function riverFold(space: Space, row: Folded[], stableBytes: number): Folded {
251
256
  const next: Folded[] = [];
252
257
  if (boundary < level.length) {
253
258
  // Prefix folds independently of the suffix — structural stability.
254
- foldSlice(space, level, 0, boundary, next, false);
255
- foldSlice(space, level, boundary, level.length - boundary, next, false);
259
+ foldSlice(space, level, 0, boundary, next, true);
260
+ foldSlice(space, level, boundary, level.length - boundary, next, true);
256
261
  } else {
257
- foldSlice(space, level, 0, level.length, next, false);
258
- }
259
- if (next.length === level.length) {
260
- // Stuck — every group was incomplete. Force-fold to break the stall.
261
- const forced: Folded[] = [];
262
- foldSlice(space, next, 0, next.length, forced, true);
263
- level = forced;
264
- } else {
265
- level = next;
262
+ foldSlice(space, level, 0, level.length, next, true);
266
263
  }
264
+ level = next;
267
265
  }
268
266
  // LINEAR fold — this root normalize is the ONLY normalize of the entire
269
267
  // fold; every intermediate gist stays unnormalized (see the folding
@@ -413,14 +411,8 @@ function riverFoldRaw(space: Space, row: Folded[]): Folded {
413
411
  let level = row;
414
412
  while (level.length > 1) {
415
413
  const next: Folded[] = [];
416
- foldSlice(space, level, 0, level.length, next, false);
417
- if (next.length === level.length) {
418
- const forced: Folded[] = [];
419
- foldSlice(space, next, 0, next.length, forced, true);
420
- level = forced;
421
- } else {
422
- level = next;
423
- }
414
+ foldSlice(space, level, 0, level.length, next, true);
415
+ level = next;
424
416
  }
425
417
  return level[0];
426
418
  }
@@ -509,16 +501,8 @@ export function bytesToTreePyramid(
509
501
  reused * mg,
510
502
  level.length - reused * mg,
511
503
  next,
512
- false,
504
+ true,
513
505
  );
514
- if (next.length === level.length) {
515
- // Stuck — same force-fold as riverFold (reuse cannot cause it: any
516
- // reused block replaces a whole folded group, so reused > 0 implies
517
- // progress).
518
- const forced: Folded[] = [];
519
- foldSlice(space, next, 0, next.length, forced, true);
520
- next = forced;
521
- }
522
506
  levels.push(next);
523
507
  level = next;
524
508
  L++;
@@ -368,6 +368,11 @@ export async function counterfactualTransfer(
368
368
  /** The point this candidate came from, or null when it is a nextOf
369
369
  * descendant — then seatOfNode supplies the seat. */
370
370
  point: Point | null;
371
+ /** For a nextOf descendant: the aligned point whose continuation edge
372
+ * named it. Its runs ARE the query evidence this analog rests on
373
+ * (the hop was reached THROUGH that alignment), so the comparison
374
+ * schema accounts them. */
375
+ src: Point;
371
376
  }
372
377
  const analogs: AnalogCandidate[] = [];
373
378
  for (const p of points) {
@@ -380,7 +385,7 @@ export async function counterfactualTransfer(
380
385
  indexOf(p.ctx, dominant.ctx, 0) < 0 &&
381
386
  indexOf(query, p.ctx, 0) < 0
382
387
  ) {
383
- analogs.push({ anchor: p.anchor, point: p });
388
+ analogs.push({ anchor: p.anchor, point: p, src: p });
384
389
  }
385
390
  // Reach through to the point's continuation targets regardless
386
391
  // of the point's own context length: when the point is a leaf
@@ -399,7 +404,7 @@ export async function counterfactualTransfer(
399
404
  indexOf(nctx, dominant.ctx, 0) >= 0 ||
400
405
  indexOf(query, nctx, 0) >= 0
401
406
  ) continue;
402
- analogs.push({ anchor: nid, point: null });
407
+ analogs.push({ anchor: nid, point: null, src: p });
403
408
  }
404
409
  }
405
410
  let bestAnalog: AnalogCandidate | null = null;
@@ -521,13 +526,13 @@ export async function counterfactualTransfer(
521
526
  // A halo-mediated act (the analogy gate) plus two seat projections.
522
527
  CONCEPT + STEP + STEP,
523
528
  // What comparison READ: the dominant's own aligned runs, plus the
524
- // analog's aligned runs when it was itself an aligned point (a nextOf
525
- // descendant was never aligned to the query directly, so it
526
- // contributes no accounted span its seat is graph-reached, not
527
- // query-matched).
529
+ // aligned runs of the point that named the analog the analog itself
530
+ // when it was an aligned point, else the source point whose
531
+ // continuation edge reached it (that alignment IS the query evidence
532
+ // the hop rests on).
528
533
  [
529
534
  ...runSpans(dominant),
530
- ...(bestAnalog.point !== null ? runSpans(bestAnalog.point) : []),
535
+ ...runSpans(bestAnalog.point ?? bestAnalog.src),
531
536
  ],
532
537
  );
533
538
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,39 +0,0 @@
1
- // demo.ts — one short session that drives the WHOLE pipeline from one memory.
2
- //
3
- // We give Sema a handful of plain notes, then ask things that no single note
4
- // answers. The headline query is the third one: from three worked examples Sema
5
- // learns the shape of "X was painted by Y", lifts the painter out of a sentence
6
- // it has NEVER seen, and then — in the same pass — reasons forward to a separate
7
- // fact about that painter. The reply contains no word from the question. That is
8
- // retrieval, generalization, and reasoning composing as a single act, with every
9
- // step traceable back to the notes behind it.
10
- import { Mind } from "../src/index.js";
11
- import { SQliteStore } from "../src/store-sqlite.js";
12
- async function main() {
13
- const mind = new Mind({ store: new SQliteStore({ path: ":memory:" }) });
14
- const ask = async (q) => (await mind.respondText(q)).trim();
15
- // ── Jot down what we know. Each line is just (context → what follows). ──
16
- await mind.ingest([
17
- // One relation, shown three times — a pattern taught purely by example:
18
- ["The Mona Lisa was painted by Leonardo da Vinci.", "Leonardo da Vinci"],
19
- ["The Starry Night was painted by Vincent van Gogh.", "Vincent van Gogh"],
20
- [
21
- "The Night Watch was painted by Rembrandt van Rijn.",
22
- "Rembrandt van Rijn",
23
- ],
24
- // One stray fact, keyed on a name none of the examples mention:
25
- ["Pablo Picasso", "Pablo Picasso co-founded the Cubist movement"],
26
- ]);
27
- // 1) GENERALIZE — apply the learned pattern to an unseen sentence and read out
28
- // the painter. "Pablo Picasso" was never given as an answer; Sema locates it
29
- // by analogy to the three examples.
30
- console.log(await ask("The Weeping Woman was painted by Pablo Picasso."));
31
- // → "Pablo Picasso co-founded the Cubist movement"
32
- // …and, having found the painter, it KEEPS GOING: the name bridges into the
33
- // one fact it holds about him. The answer appears in no word of the question.
34
- // 2) COMPUTE — exact arithmetic, grounded right where the notes go silent.
35
- console.log(await ask("a museum charges 12*4 for a family ticket"));
36
- // → "48"
37
- await mind.store.close();
38
- }
39
- main();
@@ -1,87 +0,0 @@
1
- export interface Episode {
2
- context: string;
3
- continuation: string;
4
- }
5
- export type TrainingItem = string | Episode;
6
- /** Dedup + trim a concept's items: drop empty/degenerate pairs and exact
7
- * repeats so a concept never deposits the same form twice. */
8
- export declare function refineItems(items: TrainingItem[]): TrainingItem[];
9
- /** One normalized SmolSent row. */
10
- export interface SmolSentRow {
11
- src: string;
12
- trg: string;
13
- sl: string;
14
- tl: string;
15
- }
16
- /** Normalize a raw datasets-server row into a SmolSentRow, or null when it lacks
17
- * both sides or a side is implausibly large (a dump, not a sentence). */
18
- export declare function toSmolSentRow(row: unknown): SmolSentRow | null;
19
- /** Translate ONE SmolSent pair into SEMA facts: the two sentences are one
20
- * meaning in two languages, so bind them BOTH ways. refineItems drops the
21
- * degenerate case where src === trg. */
22
- export declare function smolSentRowToItems(row: SmolSentRow): TrainingItem[];
23
- /** One normalized Aya row. */
24
- export interface AyaRow {
25
- inputs: string;
26
- targets: string;
27
- language: string;
28
- }
29
- /** Normalize a raw datasets-server row object into an AyaRow, or null when it
30
- * lacks a usable prompt/answer or a field is implausibly large (a dump, not a
31
- * cognitive example). Trims surrounding whitespace; keeps inner text verbatim
32
- * (human prose, possibly multi-paragraph). */
33
- export declare function toAyaRow(row: unknown): AyaRow | null;
34
- /** Translate ONE Aya row into SEMA training items. A row is a single human
35
- * (question → answer) exchange — exactly one FACT, the (inputs → targets) edge.
36
- * No standalone-answer experience and no one-exchange "cumulative" walk: a lone
37
- * Q→A is not multi-turn, and both would only replicate the same edge. */
38
- export declare function ayaRowToItems(row: AyaRow): TrainingItem[];
39
- /** A single oasst2 message node (the fields we use; the tree nests via replies). */
40
- interface OasstNode {
41
- role?: string;
42
- text?: string;
43
- rank?: number | null;
44
- deleted?: boolean;
45
- replies?: OasstNode[];
46
- }
47
- /** One conversational turn extracted from a tree. */
48
- export interface OasstTurn {
49
- role: string;
50
- text: string;
51
- }
52
- /** Collapse a conversation tree to ONE linear path: at each node, descend into
53
- * its best-ranked, non-deleted reply (rank 0 preferred; unranked sorts last).
54
- * Returns the ordered turns (already strictly alternating in this corpus). */
55
- export declare function bestOasstPath(root: OasstNode): OasstTurn[];
56
- /** Translate ONE multi-turn oasst2 conversation into SEMA training items.
57
- *
58
- * This is the ONE stage where cumulative continuous context is truly necessary:
59
- * the data is a real multi-turn dialogue, and what must be learned is how each
60
- * turn follows from the WHOLE conversation so far — not from the previous turn
61
- * alone. The conversation is emitted ONLY as the accumulated walk; standalone
62
- * turn experiences and local adjacent-pair facts are NOT emitted (they are
63
- * subsumed by it and would merely replicate the content).
64
- *
65
- * The walk is byte-for-byte the pattern proven in test/13-conversation.test.mjs
66
- * ("teachConversation"): each turn is the continuation of all prior turns joined
67
- * by "\n", with BARE turn text — NO "User:/Assistant:" labels. Roles already
68
- * alternate by position in an oasst2 best-path (the root is a prompter), so a
69
- * label adds nothing the position does not, while a clean continuation matches
70
- * the test's recall (predictNext queries bare prior turns) and lets a turn share
71
- * its gist with the same text elsewhere (e.g. an Aya question stored bare).
72
- *
73
- * Returns [] for a conversation below the multi-turn threshold, so callers can
74
- * simply skip empties. */
75
- export declare function oasstConversationToItems(turns: OasstTurn[]): TrainingItem[];
76
- /** One normalized General-Knowledge row. */
77
- export interface GenKnowRow {
78
- question: string;
79
- answer: string;
80
- }
81
- /** Normalize a raw datasets-server row into a GenKnowRow, or null when it lacks
82
- * a usable question/answer or a side is implausibly large (corruption). */
83
- export declare function toGenKnowRow(row: unknown): GenKnowRow | null;
84
- /** Translate ONE General-Knowledge row into SEMA items: exactly one
85
- * (question → answer) FACT. refineItems drops a degenerate question === answer. */
86
- export declare function genKnowRowToItems(row: GenKnowRow): TrainingItem[];
87
- export {};