Skip to content

sqlite-v2 terminology files: closed meta keys, requires, integrity facts, word-index facts - #469

Closed
jmandel wants to merge 5 commits into
HealthIntersections:mainfrom
jmandel:sqlite-v2-format-hardening
Closed

jmandel wants to merge 5 commits into
HealthIntersections:mainfrom
jmandel:sqlite-v2-format-hardening

Conversation

@jmandel

@jmandel jmandel commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

The sqlite-v2 terminology file (tx-terminology/1) states facts about terminology content. Until
now it also carried hints about how a server should present a system (behaviour.* keys), accepted
any unknown meta key silently, and had no way to tell a reader "you must understand X to read this
file", nor any way to notice a damaged or hand-edited file. This PR tightens the format without
changing its label (tx-terminology/1, PRAGMA user_version = 1); every file built before it still
opens and answers exactly as before.

What changes in the format

  • Meta is a closed list of keys. A reader knows every key (format, requires, integrity,
    languages, displayContexts, moduleDependencies, edition, titles, derived-by-importer,
    ix.ix_word.format / .stemmers / .scope, ix.ix_order.walk.reached, behaviour.grammar,
    behaviour.filters) and
    refuses a file with any other key, naming it. provenance.* stays open (informational, never
    read). A malformed value of a known key is refused, not ignored.
  • requires: a JSON list of the features beyond the base format that the file's content uses.
    A reader refuses a file that requires a feature it does not know. The one feature today is
    identifier-text: every identifier.value is TEXT, and readers probe identifiers with text.
    The builder writes requires = ["identifier-text"] in every file.
  • Files without requires (every file built before this change) are accepted as before; for them
    only, one function (tx/sqlite-v2/meta.js readFileMeta) allows: no integrity facts, mixed
    INTEGER/TEXT identifier values with the old probe typing, the stored word index used as before,
    and the retired keys (below) accepted and ignored.
  • Integrity facts (meta integrity = {items, schema, catalog}): the item count and SHA-256
    digests of sqlite_master and of the catalog tables (code_system, meta without provenance,
    property, property_member, item_use, value_label, each row rendered with SQLite's
    quote()). Written by the step that finishes a file; a reader checks a file with requires at
    open: file length = page_count * page_size, the two digests, concept ids 1..N with the active
    prefix, item ids N+1..N+M, and the cardinality of the stored all/active sets. The checks read the
    catalog and a few index pages: measured at about 13 ms (50 ms cold) for the whole check on the
    largest SNOMED CT edition, 1-6 ms on the others.
  • Word index facts: ix.ix_word.scope (which names the index holds: snomed, loinc,
    rxnorm) and ix.ix_word.stemmers (the stemmer of each language, e.g. natural:PorterStemmer).
    A reader uses the stored index only when they match its own text search; otherwise it builds the
    index in memory, as for a file without one (a stored stem from another stemmer would miss matches).
    The per-concept counts row is now written and read little-endian explicitly (it was host order).
  • behaviour.*: two keys remain, both facts about the content. behaviour.grammar ("snomed",
    "icd11", "cpt" or null) is the grammar of the codes; "cpt" means codes may be written
    <code>:<modifier>[:<modifier>...], with the modifier rules the reader's. behaviour.filters (null,
    or a list {code, operator, values?} as FHIR CodeSystem.filter) lists the only filters the system
    understands; code is a catalog property, inactive, or a filter the grammar defines. The others
    (expansionOrder, lookupName, description, versionAlgorithm, designationUses,
    distinctDesignationTexts, lookupSkipsMixedLinks, and the alias/preferred keys) told a server
    how to present the system; those rules now live in code by system (tx/sqlite-v2/dialects.js, and
    the grammar for the SNOMED CT mixed-link rule). The values every installed file carries equal the
    rules in code, so answers do not change. This server refuses, with a plain message, a file whose
    grammar it does not implement (icd11, cpt) and a file that carries a filter list (it does not
    apply one; a reader that serves such a file must).
  • Removed columns nobody read: property.n_rows, property.target_kind,
    code_system.release_date, .hierarchy_meaning, .case_sensitive (codes are always matched
    exactly; the provider's isCaseSensitive() returns true). ix.ix_key.normalForm.stats becomes
    provenance.normalFormStats.
  • Contracts the readers rely on are now stated in tx/sqlite-v2/README.md and schema.sql, and the
    builder checks them before it finishes a file (checkContracts: id ranges, one hierarchy, one list
    property, TEXT identifiers, ix_set key conventions and bitmap ranges, kind-4 sets without their
    anchor, ix_in/ix_by_subject row counts, ix_order block layout, the counts row length, the
    schema's indexes, sqlite_stat1 without sqlite_stat4), and runs the reader's meta check on what
    it writes.

Compatibility, and what to look at

  • Files built before this PR (no requires) open and answer as before; nothing needs rebuilding.
  • A file built after this PR should not be served by a server from before it: that reader does not
    know requires, and probes integer-looking identifiers as INTEGER, which no longer match the TEXT
    values. There is no released reader to protect yet, which is why this is done now and under the
    same format label.
  • This is a draft for discussion of the format rules. The parts most worth an opinion: the closed
    key list (anything else must be provenance.*), dropping the presentation keys in favour of rules
    in code, and whether the open-time digests are wanted at all or should be a check a tool runs.

Code

  • tx/sqlite-v2/meta.js (new): the key list, value checks, known features, retired keys,
    readFileMeta.
  • tx/sqlite-v2/integrity.js (new): the digests and the open checks, shared by the builder and
    the reader.
  • tx/sqlite-v2/store.js: meta and integrity checks at open (unfinished: true skips the
    finished-file checks for the index builder's and importers' reads of a file mid-build);
    identifier probes by requires; wordIndexState; presentation rules from dialects.js.
  • tx/importers/sqlite-v2/builder.js: finishFile (contracts, integrity, meta check) at the end of
    finish() and in vacuumFile(); text identifier values; removed columns. indexes.js: word-index
    facts, little-endian counts, node indexes.js <file> finishes a file with requires again after a
    rebuild.
  • tx/library/text-match.js: stemmerName(lang), from the stemmer table.
  • Importers stop writing the retired keys and removed columns.

Tests

tests/tx/sqlite-v2-format-integrity.test.js (new, 25 tests): unknown requires and unknown keys
refused, retired keys refused with requires and accepted without, each of the six open checks
tripping on a changed copy, identifier probes on mixed and text-only files, the little-endian counts
row, the in-memory fallback on a stemmer or scope mismatch, builder contract failures, grammars
icd11 and cpt refused, behaviour.filters accepted (with a grammar-defined filter code) and
checked, a malformed one refused. Existing sqlite-v2 tests updated for the removed columns and text identifier values. All
tests/tx/sqlite-v2-*.test.js and tests/tx/library-sqlite-v2.test.js pass.

Rebuilding a file with the importers gives a file with requires and integrity facts; files built
before keep working unchanged.

🤖 Generated with Claude Code

jmandel and others added 5 commits December 3, 2026 15:59
stemmerName(lang) gives the stemmer the text rules use for a language as
'natural:<class>' (the natural package's stemmer classes, from the same
table stemmerFor reads), or 'none' for a language without one, so a stored
word index can record which stemmer made its stems.

Co-Authored-By: Claude <noreply@anthropic.com>
The terminology file states facts about its content; a reader now checks
them instead of accepting whatever it finds.

- meta is a closed list of keys (tx/sqlite-v2/meta.js): an unknown key, a
  malformed value, or a required feature the reader does not know refuses
  the file; provenance.* stays open. `requires` lists the features the
  content uses; the one feature is identifier-text (identifier values are
  TEXT and probed as text). A file without requires (built before this) is
  accepted as before; readFileMeta is the one place that decides what such
  a file may hold (no integrity facts, INTEGER identifier values, the
  retired keys).
- meta integrity = {items, schema, catalog} (tx/sqlite-v2/integrity.js):
  SHA-256 of sqlite_master and of the catalog tables rendered with quote().
  The builder writes it when it finishes a file (finishFile, from finish()
  and vacuumFile(); indexes.js <file> after a rebuild); the store checks a
  file with requires at open: file length, both digests, concept and item
  id ranges, the all and active set cardinalities.
- The word index records its scope (the names it holds) and the stemmer of
  each language; the store uses it only when both are its own, else builds
  the index in memory. The counts row is written and read little-endian.
- behaviour.* keeps only grammar. The presentation rules the other keys
  carried (expansion order, lookup name, description, version algorithm,
  designation uses, distinct designation texts, mixed links) are rules in
  code by system (dialects.js; the grammar for mixed links), equal to the
  values every existing file carries. A grammar the reader does not
  implement (icd11) refuses the file.
- The builder checks the format's contracts before it finishes a file
  (checkContracts) and runs the reader's meta check on what it writes.
- Unread columns removed: property.n_rows, property.target_kind,
  code_system.release_date, hierarchy_meaning, case_sensitive (codes are
  always matched exactly). ix.ix_key.normalForm.stats becomes
  provenance.normalFormStats.

Co-Authored-By: Claude <noreply@anthropic.com>
Unknown required features and meta keys refused; retired keys refused in a
file with requires and ignored in one without; each integrity check at
open tripping on a changed copy; identifier probes on mixed and text-only
files; the little-endian counts row; the in-memory word index on a stemmer
or scope mismatch; the builder's contract checks; grammar icd11 refused.

Co-Authored-By: Claude <noreply@anthropic.com>
…acts

The format document states the strictness rule, requires and its known
features, the meta key table, the integrity facts and the checks at open,
the word-index facts, the contracts and which the builder checks, the
remaining behaviour key with where each retired key's rule now lives, and
the removed columns.

Co-Authored-By: Claude <noreply@anthropic.com>
behaviour.grammar gains "cpt": codes may be written
<code>:<modifier>[:<modifier>...], the modifier rules being the grammar's.
This reader does not implement it and refuses such a file, as for icd11.

behaviour.filters is a format key again (no longer retired), in files with
and without requires: null, or the only filters the system understands as
FHIR CodeSystem.filter [{code, operator, values?}], where code is a catalog
property, inactive, or a filter the file's grammar defines. A malformed
value is refused. This reader does not apply such a list, and the generic
provider could otherwise serve a file that carries one, so it refuses the
file; the README says a reader that serves such a file must apply it.

Co-Authored-By: Claude <noreply@anthropic.com>
@jmandel jmandel closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant