Skip to content

Language module Phase 7 — predecessor data import — implementation plan

Scope: the seventh phase of the language-module migration (docs/architecture/language-module-migration-v1.md, item 13 and the Phase 7 line): the predecessor’s lexeme identity, its paid-for machine-translation and dictionary-lookup caches, and ONE person’s learning data, moved into emergent once. Phases 3–5 are closed; nothing here reopens them.

Status: implemented 2026-09-10 — every slice is on develop (7a 6318b354, 7b 2bc35057, 7c with this status update). bun run --cwd packages/db predecessor:import --source-url … --person-email … --dry-run against the surviving copy reconciles every step: identity 239,344 en / 515,781 zh; frequency 53,467 / 98,733; bing-mt 161,531 / 515,522; bing-dict 239,344 / 515,781; google-mt 147,066; cards 26,410 graph / 26,409 sound / 26,409 meaning; node facts 41,358; day facts 508. Closes when the maintainer has run it against the rebuilt database (run order below) and parked it. Decisions taken while landing, in the commits: the identity reconcile reports graph collisions, legacy-id conflicts and empty graphs as counted skips; cache steps count distinct source words under the same join the write pass uses; the person steps resolve the person once per run and refuse if it already holds cards or facts; an efactor below the CHECK floor (the predecessor’s never-reviewed 0) becomes the default; the baseline evidence row is a change_audit_entry (source_operation_id = predecessor-import:v1), written only when all three person steps matched; and a ninth step, policy, derives the verdicts the imported facts imply through the projector’s person-scoped replayPolicyOnly — expecting the person’s userword rows with a first success, since for an imported fact counts_as_known reduces to first_success_at != null — and is the one person step the one-shot guard exempts, because it requires the rows that guard protects.

The standard for this phase, ruled by the maintainer: this is one-shot work over personal and reference data. The job prints what it expected and what it did; the maintainer reads, practises, looks at the stats, and parks it when they look roughly right. No integration lane, no seeded source fixture, no benchmark run, no re-import path. The same standard applies when it runs against the future production database.

Grounding (verified 2026-09-10 against the surviving copy)

Section titled “Grounding (verified 2026-09-10 against the surviving copy)”
  • Identity is the same contract on both sides. The predecessor’s word table is one row per (language, graph); word.id = bingapilookup.id for all 755,125 rows (zh-Hans 515,781, en 239,344). lang_lexeme is unique on (language_tag, graph) and carries a nullable legacy_id, unique per (language_tag, legacy_id), that nothing writes yet.
  • Three Bing caches, two consumed here. bingapitranslation 771k rows (666k match a word), bingapitransliteration 561k (515k match), bingapilookup 998k rows of the Dictionary Lookup API (755k match a word; the 243k orphans are mostly multi-word English fragments). Emergent’s pipeline speaks Translate and Transliterate only and caches in lang_dictionary_entry under the bing-mt system dictionary per pair, content {gloss, phones}, read before any live call.
  • providerdefinition (1.3M): fallback = the Translate gloss re-shaped; cedict = a CEDICT re-parse; google = 147k en→zh Google Translate sentences; abc = the ABC dictionaries in the predecessor’s parsed format (the maintainer holds the raw files and a licence).
  • word material: sound (pinyin) on 582k; synonyms (Bing back-translations) on 137k; metadata.frq (SUBTLEX words-per-million etc.) on 152,200 (zh 98,733, en 53,467; 381 with several POS rows); HSK tags on 11k. The predecessor ordered new words by frq.wcpm.
  • The surviving copy is dev-pg-transcrobes, database supabase, a 5 May 2026 snapshot on the pre-cutover model (card 539k, userword 133k, userlist 161, 155 auth users; last card and word activity 2026-04-29, last sign-in 2026-05-01). wordstate / research_event never held data in any surviving copy. Production is gone; the copy is backed by the Barman backup in ~/backups/db-backups-prod-transcrobes/supabase-cluster/, an external SSD and Garage on mac101.
  • The maintainer’s own rows (auth.users.email = anton@melser.org): 41,358 userword, 508 userday, 10 goals, 6 content, 1 list, plus cards in four types.
  • R1 — Reconcile by graph. The identity step is idempotent and order-independent: for every predecessor word it stamps legacy_id onto the lexeme that already exists for that (language, graph) or mints the lexeme with the id set. The list imports already run on the current database were not a mistake; the reconcile stamps them.
  • R2 — Every word row becomes a lexeme, both languages; the 243k lookup orphans are dropped.
  • R3 — Imported lexemes are UNPUBLISHED and the import never touches publication (ADR-0059’s 2026-08-01 amendment: the everything-public semantics are not ported). Publication stays with the curated paths (list import, CEDICT ingest). A predecessor learner’s obscure words reach their devices through the own-index shape; they are not platform-enumerable until a curated source names them.
  • R4 — The caches. Translate + Transliterate fold into one bing-mt entry per matched word and pair (gloss from the translation text, phones from the space-separated pinyin), zh→en and en→zh. Dictionary Lookup becomes a bing-dict system dictionary per pair: content stored as parts of speech with their glosses in Bing’s confidence order, back-translations kept as data, no pronunciation (Bing Lookup gives none). Implementation note: the client picks how to draw an entry from its stored shape, so bing-dict needs a small rendering addition in packages/domain/src/lang-dictionary-presentation.ts; google-mt reuses the MT shape and needs none.
  • R5 — providerdefinition. fallback and cedict dropped (the CEDICT importer runs on the fresh database from the source file). google carried as a google-mt en→zh system dictionary (gloss = sentences[].trans). abc EXCLUDED from this import: a separate project ports the two raw ABC files through two new DictionaryParser implementations (the transcrobes parser is a reference, not code to run), scoped org-licensed to personal-0 narrowed to the maintainer’s personal Offering; further licences become further rows.
  • R6 — word material. sound is the phones fallback where no transliteration row exists. synonyms and the HSK tags are dropped. frq is carried as ONE nullable numeric column on lang_lexeme (words per million, first frq row), present on BOTH identity shapes (shared published index and the per-person own index), consumed by the ordering policy’s frequency criterion log-scaled with the list rank as fallback where null. New minted words get null and score zero as unranked words do today. Refresh from the public corpora (SUBTLEX-CH, SUBTLEX-US) is a later job — backlog 0015.
  • R7 — Source of record is the dev-pg-transcrobes copy, read frozen over a second connection. It is on the old model, so the migration doc’s item 13 is corrected to card + userword + userlist; there is no research_event history to archive.
  • R8 — One person. Only anton@melser.org is ported. Nobody else is minted or imported.
  • R9 — The person is resolved at run time. The maintainer signs in once on the rebuilt database (which also mints the personal Offering); --person-email resolves exactly one active person holding that email and the job refuses on none or many. No pre-minted person, no new contact-point source, no dependence on identity linking.
  • R10 — Rows. card → lang_card (same SM-2 fields; word_id → lexeme via legacy_id); phrase cards are dropped (“the idea was good, the execution wasn’t”). userword → lang_node_fact (nb_seen → encounters, nb_checked + nb_translated → aid interactions, dates to their counterparts), minting the person’s nodes through the lazy-minting path and recording one provenance-marked baseline evidence row. Facts written directly fold no event, so nothing asks the projector what they mean: a final policy step therefore derives the verdicts they imply — lang_node_policy_output through the projector’s own person-scoped replayPolicyOnly, never a second interpreter — because the learner surface reads the verdicts, not the facts. (Superseded by the offline plan R1/R6 (2026-09-12): the learner surface computes the verdict from the lang_word_state row the import writes, and the policy step now derives the SERVER-side person_competency_state over it.) userday → lang_day_fact (seen → encounters, checked → lookups, success → successes, success + failures → grades). Goals, the personal list and content rows are dropped and recreated by hand (ADR-0061: content is never re-imported). (2026-09-17: the learner’s EXCLUSIONS are carried too — userworddetails.ignore (the May snapshot’s spelling; the predecessor’s own 2026-07-25 cutover renamed it wordstate.excluded) becomes lang_word_state.excluded, so the policy step’s expectation is “first success AND not ignored”. Without it, 8 930 words the maintainer deliberately put aside — 7 086 of them with a first success — would come back as words they are told they know.)
  • R11 — Mechanism. One job, packages/db/scripts/import-predecessor.ts (script predecessor:import): --source-url, --person-email, --dry-run default-safe like the other importers. Ordered, individually re-runnable, idempotent steps: identity reconcile → frequency → bing-mt → bing-dict → google-mt → cards → node facts → day facts → policy. The three person steps that write rows refuse if the person already has cards or facts; policy derives from those rows and is exempt. Batched writes. The source tables are declared as Drizzle objects inside the script module (the SQL-strings rule). Operator-run, never by an agent.
  • R12 — Entries are written directly as lang_dictionary_entry rows against the reconciled lexemes, never through runDictionaryIngest, whose door publishes every headword it touches.
  1. Migrations; the account — sign in once, or mint it with users:create — and its personal offering, minted by personal-offering:ensure (see The scripted form below).

  2. predecessor:import (all steps — the last, policy, derives the verdicts the imported facts imply, which is what makes them visible as “words you know”; --step policy re-derives verdicts for an already-imported person). (2026-09-17: --step takes a comma-separated list, always run in declared order, because the person half is ONE job — the recovery after a deliberate delete of that person’s rows is --step cards,node-facts,day-facts,policy in a single run.)

  3. The seven list imports (lists:import, --level for HSK 1–6). HSK 1–6 are the six hskhsk.com “HSK Official With Definitions 2012” files (zh_hsk1.txt … zh_hsk6.txt, in the tpd data directory) — tab-separated, and the importer takes the first column, so they go in as they are. The frequency list is cut from the published SUBTLEX-CH corpus file, which is already sorted by count: drop the header, take the first ten thousand data lines.

    Terminal window
    tail -n +2 "$SUBTLEX_ZH_FILE" | head -n 10000 > tmp/zh-frequency-top-10k.txt
    bun run --cwd packages/db lists:import --language zh --file tmp/zh-frequency-top-10k.txt --slug zh-frequency-top-10k --title "Chinese frequency, top 10 000"

    The parser collapses the corpus’s duplicate rows, so the list lands a few dozen graphs short of ten thousand — take a few hundred extra lines if the round number matters.

  4. The CEDICT import.

  5. The ABC dictionaries (backlog 0014) — one row each, licensed personally. The Chinese–English run is the one to do; the English–Chinese run waits for a learner who can reach an en row (0014 ruling 7).

    Terminal window
    bun run --cwd packages/db dict:import --provider abc-ce --file "$ABC_CE_FILE" --language zh --l1 en --scope org-licensed --organization personal-0 --offering <personal offering id>
    bun run --cwd packages/db dict:import --provider abc-ec --file "$ABC_EC_FILE" --language en --l1 zh --scope org-licensed --organization personal-0 --offering <personal offering id>

    The personal offering must exist first — its id is what the licence row points at. Step 1’s personal-offering:ensure mints it and prints the id as its last stdout line; the first write into the personal space (saving a text to Your library) mints it too. Signing in alone does not. DATABASE_URL must be in the shell for a real run — only the scripted block below loads .env.development.local, so export it first (set -a; source .env.development.local; set +a). A run that stops on a dropped connection has written a prefix; re-running the same command finishes it.

  6. frequency:refresh, once per language, over the PUBLISHED SUBTLEX files (backlog 0015) — it fills the frequency_wcpm of every word the predecessor’s own copy of those corpora did not cover, and leaves the figures step 2 carried alone unless --overwrite is given.

Predecessor first so the curated imports adopt already-stamped rows and do nothing but publish.

infra/utils/cloudnativepg/supabase/db/recreate-database.sh --with-real-user recreates the local database as usual and then runs every step above for the maintainer’s own account, in order. 1: it mints the auth user with users:create, grants platform_admin with roles:grant, waits for the api (supabase-functions) to roll out, and mints the personal offering with personal-offering:ensure, keeping its id. 2: predecessor:import against the frozen transcrobes copy. 3: the six HSK lists from HSK_LIST_DIR and the zh frequency list cut from SUBTLEX_ZH_FILE. 4: CC-CEDICT. 5: ABC Chinese–English, licensed to that offering, re-run once if it stops part-way (English–Chinese is not run — 0014 ruling 7). 6: frequency:refresh once per language, last, so the words the dictionaries minted receive figures too. An optional input that is absent is skipped with a message and listed again at the end; nothing is left to do in a browser.

Its inputs come from .env.development.local (documented in .env.development.local.example), which the block loads into the environment — a variable already set in the shell wins, the file fills the rest, and the bun jobs inherit it, so none of them takes an --env-file:

key what it is
EMERGENT_REAL_USER_EMAIL the account’s email — prompted when missing or blank
EMERGENT_REAL_USER_NAME its display name — prompted when missing or blank
EMERGENT_USER_PASSWORD its password — prompted silently when missing; never an argument
TRANSCROBES_DATABASE_URL the predecessor source. Required: no prompt and no fallback, because a wrong source is a silent bad import
SUBTLEX_ZH_FILE, SUBTLEX_EN_FILE the published SUBTLEX file per language (optional; ~ expanded; unset or missing on disk skips that language with a message). The zh file also gives the zh frequency list (step 3)
HSK_LIST_DIR the directory holding zh_hsk1.txt … zh_hsk6.txt (optional; ~ expanded; unset or not a directory skips the six lists, a missing file skips that level)
CEDICT_FILE the CC-CEDICT file (optional; ~ expanded; unset or missing skips step 4)
ABC_CE_FILE the ABC Chinese–English file (optional; ~ expanded; unset or missing skips step 5). Licensed: it never enters the repo

The file is read by a small loader, never sourced or evaled: it carries JSON values (the LTI JWKs) that are not shell-safe, and values are taken verbatim — one pair of surrounding quotes stripped, $(…) and ${…} left alone.

users:create is how the person is minted without a browser. Step 1 offers “sign in once” only because a sign-in is what mints an auth user, and handle_auth_user_created mints the person, their profile and their identity binding from it (ADR-0013). EMERGENT_USER_PASSWORD=… bun run --cwd packages/db users:create --email … --display-name "…" creates the same auth user through the Supabase admin API, so the person exists before predecessor:import has to resolve them. The password is read from that environment variable and never from an argument (an argument is visible in ps); --dry-run contacts no service at all; an email GoTrue already holds is reported and exits 0, so a re-run is harmless. It is an operator job — it mints ONE real person, and it writes no seeded mark.

personal-offering:ensure is how the personal offering is minted without a browser (ADR-0056 decision 3). EMERGENT_USER_PASSWORD=… bun run --cwd packages/db personal-offering:ensure --email … signs in as that account (GoTrue’s password grant) and POSTs to the api’s own provisioning route, /functions/v1/workflow/personal-offering — whose service, ensurePersonalOfferingWithDb, is the one learner-web’s first write into personal space runs too. It goes through the route, not the database: authority_level cannot be minted over the sync rail, and the provisioner’s one transaction lives in the api, where calling it exercises it as deployed. It is idempotent (a re-run answers the same offering and says whether it was created), the password comes from the environment only, --dry-run contacts nothing, and the offering id is its last stdout line, alone — which is how the scripted run hands it to the ABC import. Signing in is no longer a step.

The job prints the source counts it expects next to the counts it wrote — lexemes stamped per language, frequency set, entries per dictionary, cards per type, node facts, day facts, known verdicts — and exits non-zero if they differ. Nothing else.

  • 7a — identity and frequency. frequency_wcpm on lang_lexeme and on both identity shapes (revision bumps), the ordering policy’s criterion, the migration regeneration; the script skeleton with the identity and frequency steps.
  • 7b — caches. The bing-mt, bing-dict and google-mt steps; the bing-dict presentation addition.
  • 7c — the person. Cards, node facts, day facts; the run-order note in the runbook.
  • docs/architecture/language-module-migration-v1.md item 13 corrected (R7).
  • ADR-0059 consequence list: the frequency column (R6).
  • Backlog 0014 (ABC two-parser project, R5) and 0015 (frequency refresh from the corpora, R6).