@hviana/sema 0.1.0

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 (110) hide show
  1. package/AGENTS.md +470 -0
  2. package/AUTHORS.md +12 -0
  3. package/COMMERCIAL-LICENSE.md +19 -0
  4. package/CONTRIBUTING.md +15 -0
  5. package/HOW_IT_WORKS.md +4210 -0
  6. package/LICENSE.md +117 -0
  7. package/README.md +290 -0
  8. package/TRADEMARKS.md +19 -0
  9. package/example/demo.ts +46 -0
  10. package/example/train_base.ts +2675 -0
  11. package/index.html +893 -0
  12. package/package.json +25 -0
  13. package/src/alphabet.ts +34 -0
  14. package/src/alu/README.md +332 -0
  15. package/src/alu/src/alu.ts +541 -0
  16. package/src/alu/src/expr.ts +339 -0
  17. package/src/alu/src/index.ts +115 -0
  18. package/src/alu/src/kernel-arith.ts +377 -0
  19. package/src/alu/src/kernel-bits.ts +199 -0
  20. package/src/alu/src/kernel-logic.ts +102 -0
  21. package/src/alu/src/kernel-nd.ts +235 -0
  22. package/src/alu/src/kernel-numeric.ts +466 -0
  23. package/src/alu/src/operation.ts +344 -0
  24. package/src/alu/src/parser.ts +574 -0
  25. package/src/alu/src/resonance.ts +161 -0
  26. package/src/alu/src/text.ts +83 -0
  27. package/src/alu/src/value.ts +327 -0
  28. package/src/alu/test/alu.test.ts +1004 -0
  29. package/src/bytes.ts +62 -0
  30. package/src/config.ts +227 -0
  31. package/src/derive/README.md +295 -0
  32. package/src/derive/src/deduction.ts +263 -0
  33. package/src/derive/src/index.ts +24 -0
  34. package/src/derive/src/priority-queue.ts +70 -0
  35. package/src/derive/src/rewrite.ts +127 -0
  36. package/src/derive/src/trie.ts +242 -0
  37. package/src/derive/test/derive.test.ts +137 -0
  38. package/src/extension.ts +42 -0
  39. package/src/geometry.ts +511 -0
  40. package/src/index.ts +38 -0
  41. package/src/ingest-cache.ts +224 -0
  42. package/src/mind/articulation.ts +137 -0
  43. package/src/mind/attention.ts +1061 -0
  44. package/src/mind/canonical.ts +107 -0
  45. package/src/mind/graph-search.ts +1100 -0
  46. package/src/mind/index.ts +18 -0
  47. package/src/mind/junction.ts +347 -0
  48. package/src/mind/learning.ts +246 -0
  49. package/src/mind/match.ts +507 -0
  50. package/src/mind/mechanisms/alu.ts +33 -0
  51. package/src/mind/mechanisms/cast.ts +568 -0
  52. package/src/mind/mechanisms/confluence.ts +268 -0
  53. package/src/mind/mechanisms/cover.ts +248 -0
  54. package/src/mind/mechanisms/extraction.ts +422 -0
  55. package/src/mind/mechanisms/recall.ts +241 -0
  56. package/src/mind/mind.ts +483 -0
  57. package/src/mind/pipeline-mechanism.ts +326 -0
  58. package/src/mind/pipeline.ts +300 -0
  59. package/src/mind/primitives.ts +201 -0
  60. package/src/mind/rationale.ts +275 -0
  61. package/src/mind/reasoning.ts +198 -0
  62. package/src/mind/recognition.ts +234 -0
  63. package/src/mind/resonance.ts +0 -0
  64. package/src/mind/trace.ts +108 -0
  65. package/src/mind/traverse.ts +556 -0
  66. package/src/mind/types.ts +281 -0
  67. package/src/rabitq-hnsw/README.md +303 -0
  68. package/src/rabitq-hnsw/src/database.ts +492 -0
  69. package/src/rabitq-hnsw/src/heap.ts +90 -0
  70. package/src/rabitq-hnsw/src/hnsw.ts +514 -0
  71. package/src/rabitq-hnsw/src/index.ts +15 -0
  72. package/src/rabitq-hnsw/src/prng.ts +39 -0
  73. package/src/rabitq-hnsw/src/rabitq.ts +308 -0
  74. package/src/rabitq-hnsw/src/store.ts +994 -0
  75. package/src/rabitq-hnsw/test/hnsw.test.ts +1213 -0
  76. package/src/store-sqlite.ts +928 -0
  77. package/src/store.ts +2148 -0
  78. package/src/vec.ts +124 -0
  79. package/test/00-extract.test.mjs +151 -0
  80. package/test/01-floor.test.mjs +20 -0
  81. package/test/02-roundtrip.test.mjs +83 -0
  82. package/test/03-recall.test.mjs +98 -0
  83. package/test/04-think.test.mjs +627 -0
  84. package/test/05-concepts.test.mjs +84 -0
  85. package/test/08-storage.test.mjs +948 -0
  86. package/test/09-edges.test.mjs +65 -0
  87. package/test/11-universality.test.mjs +180 -0
  88. package/test/13-conversation.test.mjs +228 -0
  89. package/test/14-scaling.test.mjs +503 -0
  90. package/test/15-decomposition-gap.test.mjs +0 -0
  91. package/test/16-bridge.test.mjs +250 -0
  92. package/test/17-intelligence.test.mjs +240 -0
  93. package/test/18-alu.test.mjs +209 -0
  94. package/test/19-nd.test.mjs +254 -0
  95. package/test/20-stability.test.mjs +489 -0
  96. package/test/21-partial.test.mjs +158 -0
  97. package/test/22-multihop.test.mjs +130 -0
  98. package/test/23-rationale.test.mjs +316 -0
  99. package/test/24-generalization.test.mjs +416 -0
  100. package/test/25-embedding.test.mjs +75 -0
  101. package/test/26-cooperative.test.mjs +89 -0
  102. package/test/27-saturation-drop.test.mjs +637 -0
  103. package/test/28-unknowable.test.mjs +88 -0
  104. package/test/29-counterfactual.test.mjs +476 -0
  105. package/test/30-conflict-resolution.test.mjs +166 -0
  106. package/test/31-audit.test.mjs +288 -0
  107. package/test/32-confluence.test.mjs +173 -0
  108. package/test/33-multi-candidate.test.mjs +222 -0
  109. package/test/34-cross-region.test.mjs +252 -0
  110. package/tsconfig.json +16 -0
@@ -0,0 +1,107 @@
1
+ // canonical.ts — THE canonical-segmentation convention, defined once.
2
+ //
3
+ // The store's canonical reading of a byte stream — "the id of each single-byte
4
+ // leaf, chained into flat branches" — is a WRITE/READ CONTRACT split across
5
+ // four consumers that previously each re-implemented the chaining loop:
6
+ //
7
+ // WRITE side (training):
8
+ // • learning.indexSubSpans — interns sliding windows of the two
9
+ // {@link canonicalWindows} lengths over the stream's leaf ids;
10
+ // • learning.deposit — interns the WHOLE stream as one flat branch
11
+ // (via {@link leafIdPrefix}) so every deposit has a byte-level identity.
12
+ // READ side (inference):
13
+ // • recognition's canonical pass — chains leaf ids from every position and
14
+ // probes each growing prefix as a flat branch, up to {@link chainReach};
15
+ // • attention.canonicalChunkId — reads a chunk's window of leaf ids
16
+ // (via {@link leafIdRun}) and probes its prefixes for the best anchor.
17
+ // (geometry.knownPrefixLength is the same convention below the store layer,
18
+ // expressed through injected `leafAt`/`lookup` capabilities — it cannot
19
+ // import this module without inverting the dependency spine, so its header
20
+ // cross-references this file instead.)
21
+ //
22
+ // If the write side's windows or the read side's reach ever change, they must
23
+ // change HERE — a drift between them silently makes recognition stop finding
24
+ // what training indexed, which no type checker catches.
25
+
26
+ import type { MindContext } from "./types.js";
27
+
28
+ /** The two sliding-window lengths the WRITE side interns over a stream's leaf
29
+ * ids: W−1 and W (the river's grouping quantum and its off-by-one neighbour,
30
+ * so a form straddling a group boundary is reachable from either cut). */
31
+ export function canonicalWindows(W: number): [number, number] {
32
+ return [W - 1, W];
33
+ }
34
+
35
+ /** The READ side's chain reach: how many leaf ids a canonical chain may grow
36
+ * to from one position — W², the deepest two-level composite the write side's
37
+ * windows can spell. */
38
+ export function chainReach(W: number): number {
39
+ return W * W;
40
+ }
41
+
42
+ /** The id of the single-byte leaf at position `p`, or null when that byte was
43
+ * never interned. */
44
+ export function leafIdAt(
45
+ ctx: MindContext,
46
+ bytes: Uint8Array,
47
+ p: number,
48
+ ): number | null {
49
+ return ctx.store.findLeaf(bytes.subarray(p, p + 1));
50
+ }
51
+
52
+ /** The leaf ids of the window `[from, to)`, or null the moment ANY byte in it
53
+ * is unknown — the all-or-nothing read {@link canonicalChunkId} anchors on. */
54
+ export function leafIdRun(
55
+ ctx: MindContext,
56
+ bytes: Uint8Array,
57
+ from: number,
58
+ to: number,
59
+ ): number[] | null {
60
+ const ids: number[] = [];
61
+ for (let i = from; i < to; i++) {
62
+ const lid = leafIdAt(ctx, bytes, i);
63
+ if (lid === null) return null;
64
+ ids.push(lid);
65
+ }
66
+ return ids;
67
+ }
68
+
69
+ /** The leaf ids of the LONGEST KNOWN PREFIX of `bytes` — stops at the first
70
+ * unknown byte and returns what it has (possibly empty). The caller decides
71
+ * what a partial prefix means (deposit only interns the whole-stream flat
72
+ * branch when the prefix covers everything). */
73
+ export function leafIdPrefix(
74
+ ctx: MindContext,
75
+ bytes: Uint8Array,
76
+ ): number[] {
77
+ const ids: number[] = [];
78
+ for (let i = 0; i < bytes.length; i++) {
79
+ const lid = leafIdAt(ctx, bytes, i);
80
+ if (lid === null) break;
81
+ ids.push(lid);
82
+ }
83
+ return ids;
84
+ }
85
+
86
+ /** The canonical W-window node ids of a byte stream, offset → id — the
87
+ * CONTENT-ADDRESSED IDENTITY of every W-sized slice, under which any content
88
+ * two deposits share IS the same node (hash-consing paid the comparison at
89
+ * write time). The read side of the {@link canonicalWindows} contract asked
90
+ * at every offset: a window over unknown bytes, or one never interned as a
91
+ * flat branch, has no identity and is skipped. Confluence's meet and CAST's
92
+ * frame detection both read shared/common content through this — never
93
+ * through a byte scan. */
94
+ export function windowIds(
95
+ ctx: MindContext,
96
+ bytes: Uint8Array,
97
+ ): Map<number, number> {
98
+ const W = ctx.space.maxGroup;
99
+ const out = new Map<number, number>();
100
+ for (let off = 0; off + W <= bytes.length; off++) {
101
+ const ids = leafIdRun(ctx, bytes, off, off + W);
102
+ if (ids === null) continue;
103
+ const wid = ctx.store.findBranch(ids);
104
+ if (wid !== null) out.set(off, wid);
105
+ }
106
+ return out;
107
+ }