@hviana/sema 0.5.8 → 0.6.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 (108) hide show
  1. package/AGENTS.md +43 -4
  2. package/DATASETS.md +160 -0
  3. package/README.md +12 -0
  4. package/dist/example/train_base/cache.d.ts +35 -0
  5. package/dist/example/train_base/cache.js +211 -0
  6. package/dist/example/train_base/config.d.ts +21 -0
  7. package/dist/example/train_base/config.js +94 -0
  8. package/dist/example/train_base/corpora/aya.d.ts +19 -0
  9. package/dist/example/train_base/corpora/aya.js +76 -0
  10. package/dist/example/train_base/corpora/converted-parquet.d.ts +14 -0
  11. package/dist/example/train_base/corpora/converted-parquet.js +44 -0
  12. package/dist/example/train_base/corpora/genknow.d.ts +14 -0
  13. package/dist/example/train_base/corpora/genknow.js +83 -0
  14. package/dist/example/train_base/corpora/index.d.ts +29 -0
  15. package/dist/example/train_base/corpora/index.js +81 -0
  16. package/dist/example/train_base/corpora/massive.d.ts +7 -0
  17. package/dist/example/train_base/corpora/massive.js +98 -0
  18. package/dist/example/train_base/corpora/oasst2.d.ts +52 -0
  19. package/dist/example/train_base/corpora/oasst2.js +120 -0
  20. package/dist/example/train_base/corpora/smolsent.d.ts +23 -0
  21. package/dist/example/train_base/corpora/smolsent.js +156 -0
  22. package/dist/example/train_base/corpora/soda.d.ts +12 -0
  23. package/dist/example/train_base/corpora/soda.js +113 -0
  24. package/dist/example/train_base/corpora/taskmaster.d.ts +15 -0
  25. package/dist/example/train_base/corpora/taskmaster.js +144 -0
  26. package/dist/example/train_base/corpora/wiki2.d.ts +23 -0
  27. package/dist/example/train_base/corpora/wiki2.js +132 -0
  28. package/dist/example/train_base/corpus.d.ts +88 -0
  29. package/dist/example/train_base/corpus.js +65 -0
  30. package/dist/example/train_base/discovery.d.ts +48 -0
  31. package/dist/example/train_base/discovery.js +143 -0
  32. package/dist/example/train_base/http.d.ts +82 -0
  33. package/dist/example/train_base/http.js +219 -0
  34. package/dist/example/train_base/items.d.ts +46 -0
  35. package/dist/example/train_base/items.js +98 -0
  36. package/dist/example/train_base/main.d.ts +4 -0
  37. package/dist/example/train_base/main.js +207 -0
  38. package/dist/example/train_base/progress.d.ts +34 -0
  39. package/dist/example/train_base/progress.js +114 -0
  40. package/dist/example/train_base/readers.d.ts +125 -0
  41. package/dist/example/train_base/readers.js +391 -0
  42. package/dist/example/train_base/runtime.d.ts +115 -0
  43. package/dist/example/train_base/runtime.js +637 -0
  44. package/dist/example/train_base/stage.d.ts +3 -0
  45. package/dist/example/train_base/stage.js +246 -0
  46. package/dist/example/train_base/ui.d.ts +88 -0
  47. package/dist/example/train_base/ui.js +272 -0
  48. package/dist/src/geometry.d.ts +20 -0
  49. package/dist/src/geometry.js +22 -0
  50. package/dist/src/mind/attention.d.ts +6 -0
  51. package/dist/src/mind/attention.js +44 -4
  52. package/dist/src/mind/learning.js +134 -50
  53. package/dist/src/mind/mechanisms/cast.js +45 -1
  54. package/dist/src/mind/mind.d.ts +7 -2
  55. package/dist/src/mind/mind.js +15 -3
  56. package/dist/src/mind/reasoning.js +59 -5
  57. package/dist/src/mind/recognition.js +29 -3
  58. package/dist/src/mind/traverse.d.ts +16 -0
  59. package/dist/src/mind/traverse.js +18 -0
  60. package/dist/src/store-sqlite.d.ts +4 -0
  61. package/dist/src/store-sqlite.js +47 -0
  62. package/dist/src/store.d.ts +7 -0
  63. package/example/train_base/cache.ts +251 -0
  64. package/example/train_base/config.ts +128 -0
  65. package/example/train_base/corpora/aya.ts +106 -0
  66. package/example/train_base/corpora/converted-parquet.ts +64 -0
  67. package/example/train_base/corpora/genknow.ts +114 -0
  68. package/example/train_base/corpora/index.ts +88 -0
  69. package/example/train_base/corpora/massive.ts +111 -0
  70. package/example/train_base/corpora/oasst2.ts +163 -0
  71. package/example/train_base/corpora/smolsent.ts +203 -0
  72. package/example/train_base/corpora/soda.ts +130 -0
  73. package/example/train_base/corpora/taskmaster.ts +217 -0
  74. package/example/train_base/corpora/wiki2.ts +190 -0
  75. package/example/train_base/corpus.ts +150 -0
  76. package/example/train_base/discovery.ts +203 -0
  77. package/example/train_base/http.ts +284 -0
  78. package/example/train_base/items.ts +118 -0
  79. package/example/train_base/main.ts +240 -0
  80. package/example/train_base/progress.ts +149 -0
  81. package/example/train_base/readers.ts +505 -0
  82. package/example/train_base/runtime.ts +894 -0
  83. package/example/train_base/stage.ts +276 -0
  84. package/example/train_base/ui.ts +333 -0
  85. package/jsr.json +1 -1
  86. package/package.json +2 -4
  87. package/src/geometry.ts +23 -0
  88. package/src/mind/attention.ts +54 -1
  89. package/src/mind/learning.ts +137 -43
  90. package/src/mind/mechanisms/cast.ts +48 -1
  91. package/src/mind/mind.ts +13 -2
  92. package/src/mind/reasoning.ts +64 -5
  93. package/src/mind/recognition.ts +29 -3
  94. package/src/mind/traverse.ts +19 -0
  95. package/src/store-sqlite.ts +53 -0
  96. package/src/store.ts +28 -0
  97. package/test/13-conversation.test.mjs +1 -1
  98. package/test/29-counterfactual.test.mjs +43 -6
  99. package/test/77-company-saturation.test.mjs +302 -0
  100. package/test/78-atom-hub-recognition-cliff.test.mjs +135 -0
  101. package/test/84-composed-answer-honesty.test.mjs +137 -0
  102. package/test/85-answered-directly.test.mjs +126 -0
  103. package/test/86-cast-voices-committed.test.mjs +164 -0
  104. package/test/87-codominant-commitment.test.mjs +250 -0
  105. package/test/88-dependency-footprint.test.mjs +99 -0
  106. package/dist/example/train_base.d.ts +0 -93
  107. package/dist/example/train_base.js +0 -2269
  108. package/example/train_base.ts +0 -2735
@@ -0,0 +1,114 @@
1
+ // train_base/corpora/genknow.ts — MuskumPillerum/General-Knowledge Q&A
2
+ //
3
+ // Knobs, the row adapter, and the stage descriptor for ONE corpus, together
4
+ // with the evidence that fixed each default. A corpus file owns everything
5
+ // source-specific; the contract it fills is ../corpus.ts and the loop that runs
6
+ // it is ../stage.ts.
7
+
8
+ import { env } from "../config.js";
9
+ import { refineItems, type TrainingItem } from "../items.js";
10
+ import { jsonArray } from "../readers.js";
11
+ import { type Corpus, singleUnit } from "../corpus.js";
12
+
13
+ // ── MuskumPillerum/General-Knowledge (the fourth training stage, after oasst2) ──
14
+ // A ~37.6k-row general-knowledge Q&A set: each row is a single {Question, Answer}
15
+ // pair. A row is a pure RELATION (question → answer), so it becomes exactly ONE
16
+ // FACT, identical in shape to the Aya stage. It ships as a single JSON array
17
+ // file (output.json); we DOWNLOAD it and stream the array. GENKNOW_URL overrides
18
+ // the source.
19
+ //
20
+ // DISABLED BY DEFAULT ON LICENCE GROUNDS (2026-08-13). The HF repo carries NO
21
+ // licence tag and no licence in its card — an earlier header in this file
22
+ // claimed MIT without support — and its own dataset card states it "contains a
23
+ // subset of the alpaca dataset". Alpaca is CC BY-NC 4.0: NonCommercial, which
24
+ // conflicts with Sema's commercial licence. Because a Sema store retains its
25
+ // training text VERBATIM, an unlicensed corpus inside it makes the whole
26
+ // artifact undistributable. See DATASETS.md §3.2. GENKNOW=1 re-enables the
27
+ // stage for local, non-distributed experiments only.
28
+ const GENKNOW = env("GENKNOW", "0") !== "0";
29
+ const GENKNOW_URL = env(
30
+ "GENKNOW_URL",
31
+ "https://huggingface.co/datasets/MuskumPillerum/General-Knowledge/resolve/main/output.json",
32
+ );
33
+ // A Question/Answer longer than this is skipped (answers run to a few hundred
34
+ // chars; this only guards against a corrupt/runaway field).
35
+ const MAX_GENKNOW_CHARS = Math.max(
36
+ 4_000,
37
+ Math.floor(Number(env("MAX_GENKNOW_KB", "64")) * 1000) || 64_000,
38
+ );
39
+
40
+ // ═══════════════════════════════════════════════════════════════════════
41
+ // §6f General-Knowledge parsing — a {Question, Answer} row → SEMA fact
42
+ //
43
+ // Each row is a single general-knowledge question with one answer — a pure
44
+ // RELATION (question → answer), so it becomes exactly ONE FACT, like the Aya
45
+ // stage. No experience (a fact is possible) and no cumulative walk (a lone Q&A
46
+ // is not multi-turn). The source over-escapes newlines (a literal "\n" two-char
47
+ // sequence) and leaves trailing whitespace, so answers are un-escaped and
48
+ // trimmed to plain prose before deposit.
49
+ // ═══════════════════════════════════════════════════════════════════════
50
+
51
+ /** One normalized General-Knowledge row. */
52
+ export interface GenKnowRow {
53
+ question: string;
54
+ answer: string;
55
+ }
56
+
57
+ /** Turn a source value into clean prose: decode the literal "\n"/"\t"/"\r"
58
+ * two-character escapes the source JSON left in the text, collapse the runs of
59
+ * whitespace that creates, and trim. */
60
+ function unescapePlain(s: string): string {
61
+ return s
62
+ .replace(/\\r\\n|\\n|\\r/g, "\n")
63
+ .replace(/\\t/g, " ")
64
+ .replace(/[ \t]+/g, " ")
65
+ .replace(/\n{3,}/g, "\n\n")
66
+ .trim();
67
+ }
68
+
69
+ /** Normalize a raw datasets-server row into a GenKnowRow, or null when it lacks
70
+ * a usable question/answer or a side is implausibly large (corruption). */
71
+ export function toGenKnowRow(
72
+ row: unknown,
73
+ maxChars = MAX_GENKNOW_CHARS,
74
+ ): GenKnowRow | null {
75
+ if (!row || typeof row !== "object") return null;
76
+ const r = row as Record<string, unknown>;
77
+ const question = typeof r.Question === "string"
78
+ ? unescapePlain(r.Question)
79
+ : "";
80
+ const answer = typeof r.Answer === "string" ? unescapePlain(r.Answer) : "";
81
+ if (!question || !answer) return null;
82
+ if (
83
+ question.length > maxChars || answer.length > maxChars
84
+ ) return null;
85
+ return { question, answer };
86
+ }
87
+
88
+ /** Translate ONE General-Knowledge row into SEMA items: exactly one
89
+ * (question → answer) FACT. refineItems drops a degenerate question === answer. */
90
+ export function genKnowRowToItems(row: GenKnowRow): TrainingItem[] {
91
+ return refineItems([{ context: row.question, continuation: row.answer }]);
92
+ }
93
+
94
+ export const genknow: Corpus = {
95
+ id: "genknow",
96
+ label: "General-Knowledge",
97
+ kind: "Q&A facts",
98
+ enabled: GENKNOW,
99
+ read: jsonArray(),
100
+ toItems: (row) => {
101
+ const r = toGenKnowRow(row);
102
+ return r ? genKnowRowToItems(r) : null;
103
+ },
104
+ discover: singleUnit({
105
+ // Resume id "genknow::qa" — the string this store already records.
106
+ key: "qa",
107
+ label: "General-Knowledge",
108
+ display: "General-Knowledge",
109
+ url: GENKNOW_URL,
110
+ dest: "general_knowledge.json",
111
+ localMatch: [/general.*knowledge.*\.json$/i, /output\.json$/i],
112
+ localWhat: "General-Knowledge *.json",
113
+ }),
114
+ };
@@ -0,0 +1,88 @@
1
+ // train_base/corpora/index.ts — THE CURRICULUM.
2
+ //
3
+ // The order is the curriculum, and it is load-bearing: each stage runs only
4
+ // after the previous one finishes, and every stage records itself in the same
5
+ // completed-set, so a single store resumes the whole sequence.
6
+ //
7
+ // 1. SmolSent sentence-level TRANSLATION pairs across 100+ low-resource
8
+ // languages. Each pair is "two names for one meaning" → a
9
+ // foreign→English translation FACT, so every language's
10
+ // rendering of a meaning converges on ONE English node (cf.
11
+ // test/05-concepts.test.mjs).
12
+ // 2. Aya ~204k human prompt→completion pairs, 70+ languages → one
13
+ // (question → answer) FACT each.
14
+ // 3. oasst2 MULTI-TURN human↔assistant conversation trees → the
15
+ // accumulated-context walk (single-turn trees are skipped).
16
+ // 4. Taskmaster task-oriented DIALOGUE, the best-scoring corpora on the
17
+ // fold-unit recurrence benchmark that predicts halo health.
18
+ // 5. 2Wiki the `evidences` TRIPLES — the one stage aimed at
19
+ // COMPOSITION. Its Wikipedia passages and its composed
20
+ // questions are deliberately NOT read.
21
+ // 6. SODA social/commonsense DIALOGUE, budgeted.
22
+ // 7. MASSIVE short intent utterances → ONE bare experience each.
23
+ // DISABLED BY DEFAULT — edge-less content was measured to
24
+ // manufacture answers where the store should stay silent.
25
+ // 8. GenKnow ~37.6k {Question, Answer} pairs → one FACT each. DISABLED
26
+ // BY DEFAULT on licence grounds; see DATASETS.md §3.2.
27
+
28
+ import type { Corpus } from "../corpus.js";
29
+ import { smolsent } from "./smolsent.js";
30
+ import { aya } from "./aya.js";
31
+ import { oasst2 } from "./oasst2.js";
32
+ import { taskmaster } from "./taskmaster.js";
33
+ import { wiki2 } from "./wiki2.js";
34
+ import { soda } from "./soda.js";
35
+ import { massive } from "./massive.js";
36
+ import { genknow } from "./genknow.js";
37
+
38
+ export const CURRICULUM: Corpus[] = [
39
+ smolsent,
40
+ aya,
41
+ oasst2,
42
+ taskmaster,
43
+ wiki2,
44
+ soda,
45
+ massive,
46
+ genknow,
47
+ ];
48
+
49
+ /** The corpora this RUN will train, for the panel header — derived, so it can
50
+ * never go stale the way the hand-written string it replaces had. */
51
+ export const enabledLabels = (): string =>
52
+ CURRICULUM.filter((c) => c.enabled).map((c) => c.label).join("+");
53
+
54
+ /** The corpora a STORE contains: everything this run will train PLUS everything
55
+ * an earlier run already did, in curriculum order.
56
+ *
57
+ * Not the same question as `enabledLabels`, and conflating them wrote a false
58
+ * statement into every store that was ever resumed with a different set of
59
+ * stages enabled. Observed on a real store: `train.dataset` read
60
+ * "SmolSent+Aya+oasst2" while the tally recorded 37,623 General-Knowledge
61
+ * deposits sitting in it. That is not cosmetic — a Sema store retains its
62
+ * training text VERBATIM, so `train.dataset` is the record of whose licence
63
+ * terms travel with the artifact, and General-Knowledge is precisely the
64
+ * corpus DATASETS.md §3.2 disables on NonCommercial grounds.
65
+ *
66
+ * `trainedIds` are the corpus ids an earlier run deposited under (the keys of
67
+ * the per-corpus tally). An id no longer in the curriculum cannot be named and
68
+ * is dropped — the tally still carries it, which is where that evidence lives. */
69
+ export const storedLabels = (trainedIds: Iterable<string>): string => {
70
+ const trained = new Set(trainedIds);
71
+ return CURRICULUM
72
+ .filter((c) => c.enabled || trained.has(c.id))
73
+ .map((c) => c.label)
74
+ .join("+");
75
+ };
76
+
77
+ // One re-export per corpus, carrying the descriptor AND its adapters. The
78
+ // explicit `export { aya, genknow, … }` list that used to sit here named the
79
+ // eight descriptors a second time; the star exports below already provide them,
80
+ // and a hand-kept list of everything is exactly the thing that goes stale.
81
+ export * from "./smolsent.js";
82
+ export * from "./aya.js";
83
+ export * from "./oasst2.js";
84
+ export * from "./taskmaster.js";
85
+ export * from "./wiki2.js";
86
+ export * from "./soda.js";
87
+ export * from "./massive.js";
88
+ export * from "./genknow.js";
@@ -0,0 +1,111 @@
1
+ // train_base/corpora/massive.ts — AmazonScience/massive short intents
2
+ //
3
+ // Knobs, the row adapter, and the stage descriptor for ONE corpus, together
4
+ // with the evidence that fixed each default. A corpus file owns everything
5
+ // source-specific; the contract it fills is ../corpus.ts and the loop that runs
6
+ // it is ../stage.ts.
7
+
8
+ import { env } from "../config.js";
9
+ import { refineItems, type TrainingItem } from "../items.js";
10
+ import { parquet } from "../readers.js";
11
+ import type { Corpus } from "../corpus.js";
12
+ import { convertedParquetUnits } from "./converted-parquet.js";
13
+
14
+ // MASSIVE deposits BARE UTTERANCES — an experience, not an episode — and that
15
+ // is the only shape its data supports. Two richer shapes were considered and
16
+ // rejected on evidence:
17
+ // • Same-intent pairs as paraphrases. 49.1% of consecutive rows share
18
+ // (locale, intent), but they are NOT meaning-equivalent: intent 48 in mn-MN
19
+ // runs "wake me at nine on the fifth" next to "set an alarm two hours from
20
+ // now". Depositing that pair as an episode teaches a continuation that does
21
+ // not exist.
22
+ // • Same-id rows across locales. Those ARE translations of one another —
23
+ // which is exactly SmolSent's relation, and SmolSent scores worst of every
24
+ // corpus measured on fold-unit recurrence (23.2%) because cross-lingual
25
+ // pairs share no units.
26
+ // So the stage contributes recurring fold units and lexical coverage (65.1%
27
+ // recurring unit mass, median 29 B) and nothing relational. `annot_utt` carries
28
+ // slot markup ("[date : tavdahad] ...") and is never read.
29
+ // DISABLED BY DEFAULT, on evidence gathered after the stage was written. A bare
30
+ // experience deposits content with NO EDGE, and that cuts both ways. Measured on
31
+ // a three-pair dialogue store with and without six MASSIVE-style utterances:
32
+ //
33
+ // "set an alarm" without: "Sure, what size would you like?" (wrong)
34
+ // with: "set an alarm for seven" (better)
35
+ // "play music" without: "" (correct silence)
36
+ // with: "Yes, sweetened or unsweetened?" (wrong)
37
+ //
38
+ // So it displaces some wrong answers and manufactures others, INCLUDING turning
39
+ // a correct silence into a wrong answer — and honest silence is a stated
40
+ // property of this engine (AGENTS §2.13). On the mixed-curriculum store the
41
+ // same shape produced the fragment "nus" for "wake me up at nine am".
42
+ //
43
+ // That evidence is four probes on toy stores and is NOT conclusive; it is,
44
+ // however, the only evidence there is, and it points the wrong way. The stage
45
+ // stays implemented and one env var away. Turn it on (MASSIVE=1) once there is
46
+ // a real measurement showing the recurring fold units it contributes (72.3% of
47
+ // deposited unit mass) buy more than the spurious answers cost.
48
+ const MASSIVE = env("MASSIVE", "0") !== "0";
49
+ const MASSIVE_DATASET = env("MASSIVE_DATASET", "AmazonScience/massive");
50
+ // "all" is the config covering every locale in one set of shards.
51
+ const MASSIVE_CONFIG = env("MASSIVE_CONFIG", "all");
52
+ const MASSIVE_SPLITS = env("MASSIVE_SPLITS", "train")
53
+ .split(",").map((s) => s.trim()).filter(Boolean);
54
+ // 0 = every row (587,214 in `all`/train, ~17 MB of content).
55
+ const MASSIVE_MAX_ROWS = Math.max(
56
+ 0,
57
+ Math.floor(Number(env("MASSIVE_MAX_ROWS", "0"))) || 0,
58
+ );
59
+ const MAX_MASSIVE_UTT_CHARS = Math.max(
60
+ 100,
61
+ Math.floor(Number(env("MAX_MASSIVE_UTT_KB", "2")) * 1000) || 2_000,
62
+ );
63
+
64
+ // ═══════════════════════════════════════════════════════════════════════
65
+ // §6e⁗ MASSIVE parsing — one short utterance → ONE SEMA experience
66
+ //
67
+ // See the constants note for why this deposits a bare experience and not a
68
+ // relation: the two relational shapes this corpus appears to offer are both
69
+ // false (same-intent rows are not paraphrases; same-id rows across locales are
70
+ // translations, SmolSent's worst-scoring relation).
71
+ // ═══════════════════════════════════════════════════════════════════════
72
+
73
+ /** Translate ONE MASSIVE row into SEMA items: its bare utterance, as an
74
+ * experience. `annot_utt` (slot-annotated) is deliberately not used — its
75
+ * "[date : ...]" markup is not prose. Returns [] for an unusable row. */
76
+ export function massiveRowToItems(
77
+ row: unknown,
78
+ maxChars = MAX_MASSIVE_UTT_CHARS,
79
+ ): TrainingItem[] {
80
+ if (!row || typeof row !== "object") return [];
81
+ const utt = (row as Record<string, unknown>).utt;
82
+ const text = typeof utt === "string" ? utt.trim() : "";
83
+ if (!text || text.length > maxChars) return [];
84
+ return refineItems([text]);
85
+ }
86
+
87
+ export const massive: Corpus = {
88
+ id: "massive",
89
+ label: "MASSIVE",
90
+ kind: "short intents",
91
+ enabled: MASSIVE,
92
+ maxRows: MASSIVE_MAX_ROWS,
93
+ // One of ten columns — `annot_utt`, whose slot markup the note above rejects,
94
+ // is not decoded either. Measured on the converted train shard: 92 MB
95
+ // uncompressed across all ten, 36 MB for `utt` (38.7%).
96
+ read: parquet({ columns: ["utt"] }),
97
+ toItems: (row) => {
98
+ const items = massiveRowToItems(row);
99
+ return items.length ? items : null;
100
+ },
101
+ unitNoun: "shard(s)",
102
+ log: { rows: "row(s)" },
103
+ discover: convertedParquetUnits({
104
+ id: "massive",
105
+ label: "MASSIVE",
106
+ dataset: MASSIVE_DATASET,
107
+ config: MASSIVE_CONFIG,
108
+ splits: MASSIVE_SPLITS,
109
+ localSub: "massive",
110
+ }),
111
+ };
@@ -0,0 +1,163 @@
1
+ // train_base/corpora/oasst2.ts — OpenAssistant/oasst2 conversation trees
2
+ //
3
+ // Knobs, the row adapter, and the stage descriptor for ONE corpus, together
4
+ // with the evidence that fixed each default. A corpus file owns everything
5
+ // source-specific; the contract it fills is ../corpus.ts and the loop that runs
6
+ // it is ../stage.ts.
7
+
8
+ import { env } from "../config.js";
9
+ import { accumulate, refineItems, type TrainingItem } from "../items.js";
10
+ import { lines } from "../readers.js";
11
+ import { type Corpus, singleUnit } from "../corpus.js";
12
+
13
+ // ── OpenAssistant/oasst2 (the fourth training stage, after Aya) ──
14
+ // oasst2 is a corpus of human↔assistant conversation TREES. Its richest, most
15
+ // stream-friendly artifact is "<date>_oasst2_ready.trees.jsonl.gz": one JSON
16
+ // conversation tree PER LINE, gzip-compressed (a web standard — Decompression
17
+ // Stream("gzip")). Each tree is {message_tree_id, prompt:{role,text,replies:[…]}}
18
+ // where `replies` nests recursively and a prompt can have several ranked
19
+ // assistant replies (rank 0 = best). We follow the best-ranked, non-deleted
20
+ // reply at each step to get ONE linear, strictly-alternating conversation per
21
+ // tree, then keep only the MULTI-TURN ones (≥ OASST_MIN_TURNS messages, i.e. at
22
+ // least two full user→assistant exchanges) — single Q→A trees are skipped, by
23
+ // design. OASST=0 disables the stage; OASST_URL overrides the source.
24
+ const OASST = env("OASST", "1") !== "0";
25
+ const OASST_URL = env(
26
+ "OASST_URL",
27
+ "https://huggingface.co/datasets/OpenAssistant/oasst2/resolve/main/2023-11-05_oasst2_ready.trees.jsonl.gz",
28
+ );
29
+ // Multi-turn threshold: a conversation must have at least this many turns to be
30
+ // trained (4 = user→assistant→user→assistant, the smallest real multi-turn).
31
+ const OASST_MIN_TURNS = Math.max(
32
+ 2,
33
+ Math.floor(Number(env("OASST_MIN_TURNS", "4"))) || 4,
34
+ );
35
+ // Skip a tree whose decoded JSON line exceeds this (a pathological record); the
36
+ // real maximum is far smaller, so this only guards against corruption.
37
+ const MAX_OASST_LINE_CHARS = Math.max(
38
+ 100_000,
39
+ Math.floor(Number(env("MAX_OASST_LINE_MB", "8")) * 1_000_000) || 8_000_000,
40
+ );
41
+
42
+ // ═══════════════════════════════════════════════════════════════════════
43
+ // §6e OpenAssistant/oasst2 parsing — a conversation TREE → SEMA items
44
+ //
45
+ // Each tree is {prompt:{role,text,replies:[…]}}, replies nested recursively. A
46
+ // prompt can have several ranked assistant replies; we collapse the tree to ONE
47
+ // linear conversation by following the best-ranked (rank 0), non-deleted reply
48
+ // at each step. The result strictly alternates prompter/assistant. Only MULTI-
49
+ // TURN conversations (≥ OASST_MIN_TURNS messages) are kept — the explicit focus
50
+ // of this stage; single Q→A trees are dropped.
51
+ // ═══════════════════════════════════════════════════════════════════════
52
+
53
+ /** A single oasst2 message node (the fields we use; the tree nests via replies). */
54
+ interface OasstNode {
55
+ role?: string;
56
+ text?: string;
57
+ rank?: number | null;
58
+ deleted?: boolean;
59
+ replies?: OasstNode[];
60
+ }
61
+
62
+ /** One conversational turn extracted from a tree. */
63
+ export interface OasstTurn {
64
+ role: string; // "prompter" | "assistant"
65
+ text: string;
66
+ }
67
+
68
+ /** Collapse a conversation tree to ONE linear path: at each node, descend into
69
+ * its best-ranked, non-deleted reply (rank 0 preferred; unranked sorts last).
70
+ * Returns the ordered turns (already strictly alternating in this corpus). */
71
+ export function bestOasstPath(root: OasstNode): OasstTurn[] {
72
+ const turns: OasstTurn[] = [];
73
+ let node: OasstNode | undefined = root;
74
+ while (node) {
75
+ const text = typeof node.text === "string" ? node.text.trim() : "";
76
+ if (text) turns.push({ role: String(node.role ?? "?"), text });
77
+ const live: OasstNode[] = (node.replies ?? []).filter((r: OasstNode) =>
78
+ r && !r.deleted && typeof r.text === "string" && r.text.trim() !== ""
79
+ );
80
+ if (live.length === 0) break;
81
+ live.sort((a: OasstNode, b: OasstNode) =>
82
+ (a.rank ?? Number.MAX_SAFE_INTEGER) - (b.rank ?? Number.MAX_SAFE_INTEGER)
83
+ );
84
+ node = live[0];
85
+ }
86
+ return turns;
87
+ }
88
+
89
+ /** Translate ONE multi-turn oasst2 conversation into SEMA training items.
90
+ *
91
+ * This is the ONE stage where cumulative continuous context is truly necessary:
92
+ * the data is a real multi-turn dialogue, and what must be learned is how each
93
+ * turn follows from the WHOLE conversation so far — not from the previous turn
94
+ * alone. The conversation is emitted ONLY as the accumulated walk; standalone
95
+ * turn experiences and local adjacent-pair facts are NOT emitted (they are
96
+ * subsumed by it and would merely replicate the content).
97
+ *
98
+ * The walk is the pattern proven in test/13-conversation.test.mjs
99
+ * ("teachConversation"): each turn is the continuation of all prior turns,
100
+ * with BARE turn text — NO "User:/Assistant:" labels. The SHAPE is identical
101
+ * (cumulative context → next turn); the join string is not, and does not need
102
+ * to be — that file joins with nothing and this corpus joins with "\n" (see
103
+ * `accumulate`). Saying "byte-for-byte", as this comment used to, invites the
104
+ * reading that the two must agree on a separator. They must not agree,
105
+ * because there is nothing to agree about: turn boundaries are offsets, and
106
+ * the join string is just corpus text. Roles already
107
+ * alternate by position in an oasst2 best-path (the root is a prompter), so a
108
+ * label adds nothing the position does not, while a clean continuation matches
109
+ * the test's recall (predictNext queries bare prior turns) and lets a turn share
110
+ * its gist with the same text elsewhere (e.g. an Aya question stored bare).
111
+ *
112
+ * Returns [] for a conversation below the multi-turn threshold, so callers can
113
+ * simply skip empties. */
114
+ export function oasstConversationToItems(
115
+ turns: OasstTurn[],
116
+ minTurns = OASST_MIN_TURNS,
117
+ ): TrainingItem[] {
118
+ if (turns.length < minTurns) return []; // not multi-turn — skip
119
+ return refineItems(accumulate(turns.map((t) => t.text)));
120
+ }
121
+
122
+ /** The row adapter: ONE line of the tree dump → its deposits. Returns null for
123
+ * a tree with no prompt and for every single-turn tree — the latter is the
124
+ * stage's design, not a defect, which is why the reader counts it `unusable`
125
+ * rather than `skipped`. */
126
+ export function oasstTreeToItems(row: unknown): TrainingItem[] | null {
127
+ if (!row || typeof row !== "object") return null;
128
+ const tree = row as { prompt?: OasstNode };
129
+ if (!tree.prompt) return null;
130
+ const items = oasstConversationToItems(bestOasstPath(tree.prompt));
131
+ return items.length ? items : null;
132
+ }
133
+
134
+ export const oasst2: Corpus = {
135
+ id: "oasst2",
136
+ label: "oasst2",
137
+ kind: "multi-turn chat",
138
+ enabled: OASST,
139
+ read: lines({ gzip: true, maxLineChars: MAX_OASST_LINE_CHARS }),
140
+ toItems: oasstTreeToItems,
141
+ // The one corpus that KEEPS a cached file after a complete read: a copy left
142
+ // behind by a previous interrupted run is not this stage's to reclaim.
143
+ keepCached: true,
144
+ log: {
145
+ deposits: "examples",
146
+ // A tree that yields items is exactly a tree that cleared OASST_MIN_TURNS.
147
+ rows: "conversation(s)",
148
+ // The single-turn trees this stage drops BY DESIGN must not be reported as
149
+ // damage — only genuinely malformed lines are.
150
+ malformedOnly: true,
151
+ },
152
+ discover: singleUnit({
153
+ // Resume id "oasst2::trees" — the string this store already records.
154
+ key: "trees",
155
+ label: "oasst2",
156
+ display: "oasst2 (multi-turn)",
157
+ url: OASST_URL,
158
+ dest: "oasst2_ready.trees.jsonl.gz",
159
+ acquireLabel: "oasst2 trees",
160
+ localMatch: [/oasst.*trees.*\.jsonl\.gz$/i, /oasst.*\.jsonl\.gz$/i],
161
+ localWhat: "oasst2 *trees*.jsonl.gz",
162
+ }),
163
+ };