Repository navigation
Conversation
`yarn fb` called the committed flatc.exe, so the schema could only be regenerated on Windows. flatbuffers/flatc.mjs resolves flatc 25.2.10 (the version of the runtime dependency and of that exe): FLATC from the environment, the committed exe on Windows, otherwise the official release binary, downloaded once into .tmp/ and checked against a pinned sha256. Regenerating src/Schema with it reproduces the committed files byte for byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A .frag written by an older version of the package can now be brought up to date instead of being misread. Every change to the files is a directory of src/Migrations/list named by a UTC timestamp, and a file records the id of the newest one its writer knew as `metadata.revision`. - `migrate(bytes)` runs what a file is behind on and stamps it. `getRevision(bytes)` tells where a file is and what is pending. `applyMigration(model, migration)` runs an app's own edit step. - A migration has a `layout` step (handlers for the fields a schema change leaves open), an `edit` step (edit requests), or both. They share a `ctx`, and `report(ctx)` is what the app is told about the file. Layout steps all run before edit steps, so edit steps only ever see the current layout and no old generated code has to be kept. - An old layout is data: `schema.json` describes the binary layout of one version of the schema, and a generic copier rewrites a file from one layout to another. It matches fields by name and copies vectors of scalars and of unchanged structs as bytes. - `yarn migration:new <slug>` snapshots index.fbs, diffs it against the previous snapshot and scaffolds a migration with one typed handler per decision the change leaves open. `yarn migration:check` fails when the schema changed without one. The first migration is the one the history needed. 3.2 removed `alignments` and `geometries` from the middle of the root table, which moved `unique_attributes` and `relation_names` two slots down, and nothing in a file tells the two layouts apart. On a file converted with 3.1.7 the current code reads the old `geometries` table as 22 relation names. The migration recognizes such a file by its layout, drops the two fields (counted in the report) and assigns the geometry ids 3.2 introduced the way its importer does: the migrated file has the same ids as the one the current importer writes for the same IFC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…atOpen#316) Every file the package writes now records the revision it is at, next to the generator and version of ThatOpen#273: `getProvenanceMetadata()` adds `revision`, the id of the newest migration. The second migration is an edit step. A file that names no generator or version (anything written before ThatOpen#273) gets "unknown" for what is missing, through an ordinary UPDATE_METADATA request, and reports which keys it filled in. Nobody can say any more what wrote such a file, so the stamp says so; from this revision on every file has the three keys. It is also the migration files with no revision are measured against. One that already names its generator was written between ThatOpen#273 and the revision stamp and has nothing to catch up on, so it is left alone and loading it costs one look at its metadata. Only the ones that name none are behind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`load()` now brings a file an older version of the package wrote up to
date before reading it. The worker does it right after inflating, which
is where the bytes are and the only point where a stale file can still be
told from a current one.
- `settings.autoMigrate` (default true) turns it off; a load overrides it
with `virtualModelConfig.migrate`.
- `onModelMigrated` tells the app what was done: `{ modelId, from, to,
applied }`, each applied migration with its report. The model is the
migrated one, so `getBuffer()` is what to store in place of the file.
- With migrating off, a file that is only behind on content loads as it
is. One whose layout changed cannot be read, so `load()` rejects with a
`StaleFragmentsError`. A file newer than the package rejects with an
`UnknownRevisionError` either way. The worker answers both as data and
the main thread throws the error, because an error thrown in the worker
arrives as a string.
- Loading reports a "migrating" progress stage when a migration ran.
The single-threaded model imports its types from the module and no longer
from the `model` barrel, so the worker, which now uses it to run edit
steps, does not reach the main-thread entry. The migrations add 41 kB to
worker.min.mjs (9 kB gzipped).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The provenance backfill was an edit step. Measured on a 64 MB .frag (192 MB inflated), that is 12.1 s: 6.2 s to build the model the step reads from and 5.2 s for `edit()` to rewrite the file. For scale, building a model from that file in Node and waiting for its setup takes 6.6 s. The backfill only changes `Model.metadata`, so it is now a handler for that one field, applied while the copier copies the file: 0.7 s on the same model. It also no longer has the side effects of an edit, which recomputes the unique attributes and relation names of the file. A handler that only changes content does not make a file unreadable, so a pending step reports `rewrite` only when the schema itself broke. With migrating turned off such a file still loads as it is. The copier also returns what the builder wrote as a view when the file fills most of the buffer, instead of copying it out, and sizes the buffer so the file starts on an aligned byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI runs `migration:check` before building. It fails when index.fbs changed since the newest migration, when a generated file (registry, diff, types) is stale, when a migration is still its scaffold, and, on a pull request, when a new migration is older than one on the base branch: files are stamped with the newest id their writer knew, so every file written in between would skip it. The check compares layouts itself and does not rely on `flatc --conform` alone, which accepts a field added to a struct although that changes the size of every vector of it. CONTRIBUTING.md gets a section on changing what a .frag holds, and the changelog an entry for the feature. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pen#316) Until now a schema change that left a decision without a handler was caught by the types of the migration and by its tests. The check now verifies it directly: for each migration it has the copier plan a copy from the previous layout to the new one with the handlers of the migration. The copier plans a whole copy before it touches a file and refuses a field it cannot carry and has no handler for, so this is the code that runs on a file, and it needs no fixture. A handler for a field of neither layout fails too. For each schema change the check also prints what became of every field: decided by a handler, carried by the copier, or new and left for older files to lack. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hatOpen#316) An edit step was handed a model built up front, and its requests were applied through that model. On a 192 MB inflated file building the model is 6.2 s of the 12.1 s the step took. The step now gets a model that is built the first time it is read from, so one that works from its ctx, or has nothing to do, does not pay for it. The requests are applied to the bytes with `edit()` directly, with the ids and the max local id handled the way a model's `edit()` and `save()` handle them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ThatOpen#316) 3.2 keeps an alignment as an item of category ThatOpenAlignment whose `data` attribute is the JSON of its curves. The migration from the 3.1 layout dropped `Model.alignments` and `Model.geometries` and reported the count. It now converts them: the handler of the removed field reads the curves while the file is still in its old layout and leaves them in ctx, and the edit step creates the items once the file is in the new one. The curves are written as the importer has written them since 3.2, which is not how 3.1 stored them. Its importer stored the curves of an alignment last curve first, the points of a curve last point first, and the curve types against the wrong curves: over the samples of an alignment the types were written vertical, horizontal, absolute, while the samples run absolute, horizontal, vertical. So 3.1 drew an alignment backwards and showed an absolute curve with the type of a vertical one. The two enums also number the arc and the clothoid the other way round. Checked against what web-ifc gave the 3.1.7 importer, recorded during the conversion and laid out the way the current importer does: on a hand-written IFC with two alignments (resources/ifc/alignment.ifc, whose 3.1.7 conversion and expected curves are the fixtures) and on three buildingSMART IFC 4.3 samples, every curve has the same type and every coordinate is equal as a 32-bit float. The samples are CC BY-ND and were only used locally. A file written by 3.0 lays its civil geometry out another way that no descriptor here covers. Its alignments are still dropped, and counted in the report as `droppedAlignments`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pen#316) The worker carried all of it: the copier, every layout and both migrations, 42 kB of worker.min.mjs that an app whose files are current never runs. It now holds a gate and nothing else, and the rest is behind a dynamic import. - The gate (`gate.ts`, generated next to the registry) is the steps by id, whether each has work to do on an older file, and the probes that place a file with no revision. A probe is now a `probe.ts` next to its migration instead of a property of it, so the gate can have it without the migration. - The worker asks the gate. A file that is behind is answered with no model and handed back, inflated and transferred, not copied. - The main thread then imports the migration code, runs the copies, and sends the file again together with what the edit steps ask for. The worker applies that, because it has the editor, and reads the file. - `migrate()` does the same in Node, and `getRevision()` answers from the gate alone, naming migrations by their slug. A plain `import()` inside the worker was not an option: the worker has to stay one file. `getWorker()` runs it from a blob URL, where a relative chunk cannot resolve. An edit step is now `edit(ctx)`, no longer `edit(model, ctx)`. Handing it a model meant the main thread had to load and build the whole reader of the package, which a viewer does not have, and no migration used it: what a step needs of the file, a handler can read while the file is copied. `applyMigration` is unchanged for apps, whose edit reads the model they hold. For a viewer bundled by esbuild the entry grows by 2 kB and the migration code is a 36 kB chunk loaded on demand; the worker grows by 3 kB instead of 42. The three single-file builds inline the import, so each stays one file. An abort that lands while the main thread is migrating is kept by the worker and stops the load when the file comes back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pen#316) The generated files spelled their imports as literals that only made sense from where each file lands. The generator now resolves every module it imports from its own location and writes the path relative to the file it is generating, so a module that moved fails the generator instead of leaving a broken import. The generated files are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pen#316) A file that was behind went back to the main thread, which imported the migration code, ran the copies there and sent the file to the worker a second time with the edits to apply. The worker now does all of it: the main thread sends the file once and gets back what was done. - The migration code is built as a file of its own next to the worker, `dist/Worker/migrations.mjs` (and `.min.mjs`), exported as `@thatopen/fragments/migrations`. It imports nothing and uses nothing of Node, a page or a worker, so it runs in all three. - The worker still carries only the gate. For a file that is behind it imports that file from a URL, because a worker that runs from a blob URL cannot resolve a path of its own, and checks that it is from the same version of the package. - The main thread sends the URLs with the load: `migrations.mjs` next to the worker, which is where a worker from `getWorker()` finds it on unpkg, and then the one the package bundles. The new `migrationsURL` option of FragmentsModels replaces both. - `run(bytes, { apply })` in `core.ts` is the whole of a migration and the one call `migrate()` and the worker both make. The editor is handed to it, so it is not bundled into the file a second time. - When the code cannot be loaded, a file that is only behind on content is read as it is, with a warning, and one whose layout changed rejects with the new `MigrationsUnavailableError`. A migration that fails now reaches `load()` as a `MigrationError` instead of a string. The handing back is gone with it: `transferBack` on the connection, the `migrating` set of the worker and what the abort and delete handlers did for it. On a 192 MB (inflated) file that is behind on content, the longest the main thread of Chrome stalls during `load()` is 6 ms, the same as with migrating off. The copy it used to run there takes 0.53 s. worker.min.mjs grows by 0.7 kB, migrations.min.mjs is 47 kB (12 kB gzipped), and a viewer bundled by esbuild starts with 1.1 kB more: the URLs and the new error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ThatOpen#316) The worker was sent a list of places to look for the migration code and tried them in order, and a file that was only behind on content loaded unmigrated, with a warning, when none of them answered. Both are gone. - The main thread resolves one URL and the worker imports that one or fails. It is the `migrationsURL` option, or the URL that goes with a worker the package located itself: the bundled one when no worker URL is given, the one on unpkg for a worker from `getWorker()`. Nothing is guessed from a worker URL the app passes. - With migrating on and no URL, `load()` rejects with a `MigrationsUnavailableError` before anything is sent to the worker, whatever the file. So an app that passes a worker URL of its own now has to pass `migrationsURL` too, or turn `autoMigrate` off. - A file the worker cannot import the code for is refused with the same error, naming the URL, whether its layout changed or only its content lags. `MigrationsUnavailableError` carries that one `url` in place of the revision and the list of sources. The three dev pages that run the worker from source pass the option, pointing at `core.ts`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
A POC for #316. A
.fragwritten by an older version of the package is brought up to date when it is loaded, instead of being misread. Files record the revision of the format they are at (metadata.revision).load()has its worker run the migrations a file is behind on before reading it, andonModelMigratedtells the app what was done so it can store the result.migrate(bytes)does the same in Node, a page or a worker. A file that is current costs one look at its metadata, and an app whose files are all current downloads none of the migration code.Two migrations ship, both for files that exist today:
alignmentsandgeometriesfrom the middle of the root table, which movedunique_attributesandrelation_namestwo slots down, and nothing in a file tells the two layouts apart. On a file converted with 3.1.7,mainreads the oldgeometriestable as 22 relation names. The migration rewrites such a file to the current layout, assigns the geometry ids 3.2 introduced the way its importer does, and turns the alignments of a 3.1 file into theThatOpenAlignmentitems they have been since 3.2."unknown"for what is missing, so from this revision on every file has all three keys.To test it
yarn testfrom the repo root: 240 tests. The ones for this PR are undersrc/Migrations, plusload-migration.test.tsandthread-model-creator.test.ts.yarn workspace @thatopen/fragments migration:check, which is what CI now runs. It prints what became of every field of each schema change.packages/fragments:yarn tsx -e 'import { readFileSync } from "fs"; import { getRevision, migrate } from "./src/Migrations"; const file = new Uint8Array(readFileSync("src/Migrations/list/20251023113211_remove-alignments-and-geometries/fixtures/alignment.3.1.7.frag")); console.log(getRevision(file)); migrate(file).then((result) => console.log(result.applied, getRevision(result.bytes).pending));'It prints the two pending migrations, then what each one did to the file, then an empty pending list. There is no example page yet (see Review).
What an app sees
settings.autoMigrate(defaulttrue) turns it off, and a load overrides it withvirtualModelConfig.migrate. With migrating off, a file that is only behind on content loads as it is, and one whose layout changed rejects with aStaleFragmentsError.UnknownRevisionErrorfor a file newer than the package,MigrationErrorwhen a migration fails on a file (the file is left as it was), andMigrationsUnavailableError(below)."migrating"stage.load():migrate(bytes)returns the migrated file and what was applied,getRevision(bytes)tells where a file is and what is pending without changing it, andapplyMigration(model, migration)runs an edit migration an app wrote for its own files.Where the migration code is
The worker carries only a gate: the ids of the steps, whether each has work to do, and the probes that place a file with no revision. Everything else (layouts, copier, migrations) is one self-contained file next to the worker,
dist/Worker/migrations.mjs, exported as@thatopen/fragments/migrations. The worker imports it from a URL the first time it is given a file that is behind, copies and edits the file in place, and reads it. Nothing goes back to the main thread but the report.It has to be a URL because the worker often runs from a blob (
getWorker()), where a path relative to it leads nowhere. The main thread resolves that one URL and the worker tries nothing else:migrations.mjsgetWorker(), also throughtoClassicWorker()migrationsURLoption, which is then requiredThis is a breaking change for apps that pass their own worker URL. With migrating on and no URL,
load()rejects with aMigrationsUnavailableErrorbefore anything is sent to the worker, whatever the file. The fix is to servemigrations.mjsand passmigrationsURL, or to turnautoMigrateoff. When the worker has a URL and cannot import from it, the load of a file that is behind rejects with the same error, naming the URL. In@thatopen/components3.4.6,FragmentsManager.getWorker()delegates here and keeps working;init(workerURL, options)passes its options through but types them as{ classicWorker?: boolean }, so it needs widening.Writing a migration
Every change to what a
.fragholds is a directory ofsrc/Migrations/list, named by a UTC timestamp and not by a version, because a migration is written before its version exists.yarn workspace @thatopen/fragments migration:new <slug>snapshotsindex.fbs, diffs it against the previous snapshot and scaffolds the directory. This is the second migration, less its two constants:layoutgives the value of single fields while the file is copied to the new layout. The copier carries everything it can by name; a handler is needed for each field the schema change leaves open, and the generatedtypes.tsmakes a missing one a compile error.editreturns ordinary edit requests, for changes that need the editor, like creating items. The two share actx, solayoutcan keep what it removes foreditto put back in another form, which is how the alignments are converted.schema.jsondescribes the binary layout of one version of the schema, and a generic copier rewrites a file from one layout to another. Nothing ofsrc/Schemais kept for old files.migration:checkcompares layouts itself instead of relying onflatc --conform, which accepts a field added to a struct. It also plans a copy with each migration's handlers, so a field left undecided fails without a fixture, and on a pull request it fails a migration older than one on the base branch.CONTRIBUTING.md has the full description.
Costs
mainmainmigrations.min.mjs, loaded only for a file that is behind.frag(192 MB inflated)The two size rows compare bundles made with esbuild from source. The edit cost is why the provenance backfill is a
layouthandler and not an edit step, and only a file that has alignments to convert pays it.Review
79 files, +8,665 −27, of which about a quarter is generated:
src/Migrations, hand-writtenflatbuffers/tooling (migration:new,check,sync, pinned flatc)src(load path, worker, options).fragfiles and the expected curves of oneRead the final state, not commit by commit. The load path changed three times on this branch: the worker first carried all the code, then handed stale files back to the main thread, and now imports the code itself (
cf8d450,c2fc917,e65eaf0,092bd30). A reading order:types.ts: what a migration is.core.ts:prepareandrun, the whole of a migration, the same call inmigrate()and in the worker.layout/copier.ts: the generic copy between two layouts. The largest hand-written file.gate.ts,thread-model-creator.tsandurl.ts: what a load does.flatbuffers/migration.ts, the generator.The first commit,
42a397d, makesyarn fbrun a pinned flatc on every platform instead of the committedflatc.exe. The generator needs it, but it stands on its own and can be a PR of its own.Decisions to weigh in on
migrationsURLfor a worker the app serves, described above. The alternative is a default guess (next to the worker, or the bundled one) that fails only when a stale file arrives.metadata.revision, and a file with none is placed by probes that look at its layout and metadata.Not done or not checked
migrate()on the page, in headless Chrome) was a local script againstdist.migrations.mjsfrom thenew URL(..., import.meta.url)literal that the bundled worker relies on. Firefox and Safari were not tried.getWorker()can only run once a version with this file is published.One conflict with
main, inCHANGELOG.md.🤖 Generated with Claude Code