// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * Build per-script parquet files from the DeepSeek-generated transliteration jsonl and emit * the corpus-v0.4.0 manifest that combines them with the existing kryptonite + v0.3.0 files. * * This module sits beside `kryptonite.ts`. * Both modules use the same composition pattern: take a base manifest, * append new parquet files, write a combined manifest. * Differences specific to transliteration: * * - One jsonl contains rows from N target scripts (source = `deepseek-translit-`). * We bucket by `source` and write one parquet file per script so `audit.ts` can attribute * each file to its synthetic source without relying on filename-prefix inference. * - Each file is written to `train/part-translit-.parquet` * (distinct from kryptonite's `part-0000.parquet`, which v0.4.0's first builder already produced). * - Inherits the path-canonicalization fix flagged in Thread B's postmortem: * v0.3.0 file paths are rewritten from `$MAILWOMAN_DATA_ROOT/...` to `/data/...` in * the combined manifest so all paths share one container-friendly form. * * See docs/records/engineering/CORPUS_V0_4_0_GENERATION.mdx for prompts, model * and the reproducibility interface. * * Invoke via `mailwoman corpus recipe translit \ * --jsonl /data/corpus/versioned/v0.4.0/transliteration/canonical-transliteration.jsonl \ * --base-manifest /data/corpus/versioned/v0.4.0/corpus-v0.4.0/manifest.json \ * --out-dir /data/corpus/versioned/v0.4.0` */ import { sha256Hex } from "@mailwoman/core/crypto/hash" import { dataRootPath } from "@mailwoman/core/data-root" import { readLocalJSONFile } from "@mailwoman/core/fs/readers" import { pathExists, tryStat } from "@mailwoman/core/fs/readers/stat" import { writeLocalJSONFile, writeLocalTextFile, makeDirectories } from "@mailwoman/core/fs/writers" import { stringifyJSON } from "@mailwoman/core/values/json" import { PathBuilder, type PathBuilderLike } from "path-ts" import { JSONSpliterator } from "spliterator" import { moduleGraphHash } from "#build/code-hash" import { corpusDirectoryName } from "#directory" import type { FileDerivation } from "#parquet/record" import { PARQUET_COLUMNS, PARQUET_COMPRESSION, ROW_GROUP_SIZE, rowToParquet } from "#parquet/schema" import type { ParquetFileDescriptor, ParquetManifest } from "#parquet/writers" import { writeParquetFile } from "#parquet/writers" import { baseManifestFiles } from "#tools/overlay/manifest" import { type CanonicalRow, type LabeledRow, requireSurface } from "#types" import { alignRow } from "#utils" export interface TranslitOverlayOptions { jsonl: string baseManifest: string outDir: PathBuilderLike /** * @defaultValue `"0.4.0"` */ corpusVersion?: string /** * @defaultValue `"/data/"` */ canonicalPathPrefix?: string /** * Prefix currently used in the base manifest's file paths, to be rewritten * to {@link TranslitOverlayOptions.canonicalPathPrefix}. * * Pass it explicitly when translating a manifest generated under a different * `$MAILWOMAN_DATA_ROOT` than the one you're running with. * * @defaultValue `dataRootPath()` with a trailing slash — the root that wrote those paths. */ legacyPathPrefix?: string } function toCanonicalRow(raw: Record): CanonicalRow { return { raw: raw["raw"] as string, components: raw["components"] as Record, // country-branch: a transliteration row without a country reads as US. // The commit that added the fallback recorded no reason, and a row format // that requires `country` would replace it. country: (raw["country"] as string) ?? "US", locale: (raw["locale"] as string) ?? undefined, source: raw["source"] as string, source_id: raw["source_id"] as string, license: (raw["license"] as string) ?? "Synthetic (DeepSeek-v4-flash, AGPL-compatible)", register: (raw["register"] as string | null) ?? null, surface: requireSurface(raw, "translit"), } } /** * Write one parquet file for a single source slug. * * @returns The populated ParquetFileDescriptor + a list of quarantine reasons * for rows that failed alignment. */ async function writeOneFile( rows: readonly LabeledRow[], outPath: string, source: string, derivation: FileDerivation ): Promise { let firstSourceID = "" let lastSourceID = "" for (const row of rows) { if (firstSourceID === "") { firstSourceID = row.source_id } lastSourceID = row.source_id } const { sha256 } = await writeParquetFile(rows.map(rowToParquet), outPath, derivation) const fileStat = await tryStat(outPath) return { split: "train", path: outPath, format: "parquet", compression: PARQUET_COMPRESSION, rows: rows.length, bytes: fileStat?.size ?? 0, sha256, first_source_id: firstSourceID, last_source_id: lastSourceID, // Stamp source so audit.ts attributes the file without falling back to filename-prefix inference. source, } } function canonicalizeFilePath(path: string, legacyPrefix: string, canonicalPrefix: string): string { if (path.startsWith(legacyPrefix)) return canonicalPrefix + path.slice(legacyPrefix.length) return path } export async function buildTranslitOverlay( options: TranslitOverlayOptions, report?: (line: string) => void ): Promise { const corpusVersion = options.corpusVersion ?? "0.4.0" // This is a portable manifest namespace rather than a host filesystem default. const canonicalPathPrefix = options.canonicalPathPrefix ?? "/data/" const legacyPathPrefix = options.legacyPathPrefix ?? `${dataRootPath()}/` if (!(await pathExists(options.jsonl))) throw new Error(`jsonl not found: ${options.jsonl}`) if (!(await pathExists(options.baseManifest))) throw new Error(`base-manifest not found: ${options.baseManifest}`) const corpusDir = PathBuilder.from(options.outDir)(corpusDirectoryName(corpusVersion)) const trainDir = corpusDir("train") await makeDirectories(trainDir) // Bucket canonical rows by source. // Quarantined rows are logged. const buckets = new Map() const quarantine: string[] = [] let totalIn = 0 for await (const raw of JSONSpliterator.fromAsync>(options.jsonl)) { totalIn++ const canon = toCanonicalRow(raw) const result = alignRow(canon) if (result.kind !== "labeled") { quarantine.push(`${canon.source_id}\t${result.row.reason}`) continue } const bucket = buckets.get(canon.source) if (bucket) { bucket.push(result.row) } else { buckets.set(canon.source, [result.row]) } } report?.(`read ${totalIn} rows; ${quarantine.length} quarantined; ${buckets.size} script buckets`) const newFiles: ParquetFileDescriptor[] = [] const sortedKeys = [...buckets.keys()].toSorted() const codeHash = await moduleGraphHash([import.meta.filename]) for (const source of sortedKeys) { const rows = buckets.get(source)! const slug = source.startsWith("deepseek-translit-") ? source.slice("deepseek-translit-".length) : source // A string, because the manifest records it. const outPath = trainDir(`part-translit-${slug}.parquet`).toString() // The jsonl holds every script's rows, so a file's extract hash covers only the rows of its own source. const descriptor = await writeOneFile(rows, outPath, source, { extractHash: sha256Hex(rows.map((row) => stringifyJSON(row)).join("\n")), codeHash, config: { tool: "translit", source, inputSources: [source] }, }) newFiles.push(descriptor) report?.(` ${source}: ${descriptor.rows} rows → ${outPath} (${descriptor.bytes} bytes)`) } if (quarantine.length) { const qPath = corpusDir("quarantine-transliteration.tsv") await writeLocalTextFile(quarantine, qPath) report?.(`quarantine log → ${qPath} (${quarantine.length} rows)`) } // Compose final manifest: rewrite the base's file paths from the data root → /data/... // And append the new translit files. // The kryptonite file already lives in the base manifest (it was written there by Thread B). const base = await readLocalJSONFile(options.baseManifest) const rewrittenBase = baseManifestFiles<(typeof base)["files"][number]>(base).map((file) => ({ ...file, path: canonicalizeFilePath(file.path, legacyPathPrefix, canonicalPathPrefix), })) const newTrainRows = newFiles.reduce((sum, file) => sum + file.rows, 0) const combined: ParquetManifest = { corpus_version: corpusVersion, schema: PARQUET_COLUMNS, rows_per_file: base.rows_per_file, row_group_size: base.row_group_size ?? ROW_GROUP_SIZE, files: [...rewrittenBase, ...newFiles], counts: { train: base.counts.train + newTrainRows, val: base.counts.val, test: base.counts.test, }, total_rows: base.total_rows + newTrainRows, } const combinedPath = corpusDir("MANIFEST.json") await writeLocalJSONFile(combined, combinedPath) report?.(`wrote combined manifest → ${combinedPath}`) report?.(` total_rows=${combined.total_rows} (base=${base.total_rows}, added=${newTrainRows})`) report?.( ` files=${combined.files.length} (base=${baseManifestFiles<(typeof base)["files"][number]>(base).length}, added=${newFiles.length})` ) report?.(` compression=${PARQUET_COMPRESSION}`) const pathFix = rewrittenBase.filter( (s, i) => s.path !== baseManifestFiles<(typeof base)["files"][number]>(base)[i]!.path ).length if (pathFix > 0) { report?.(` path-canonicalized base files: ${pathFix} (legacy '${legacyPathPrefix}' → '${canonicalPathPrefix}')`) } }