@runeditrun/rer 0.2.75 → 0.2.77

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 (72) hide show
  1. package/README.md +7 -2
  2. package/cli/README.md +55 -0
  3. package/cli/contract.mjs +1 -1
  4. package/cli/deploy-command.mjs +4 -2
  5. package/cli/deploy-inputs.mjs +15 -3
  6. package/cli/deploy-installation-key.mjs +71 -0
  7. package/cli/deploy-secrets.mjs +2 -0
  8. package/cli/deploy.mjs +13 -2
  9. package/cli/deployment-record.mjs +2 -0
  10. package/cli/listing.mjs +20 -0
  11. package/cli/main.mjs +2 -1
  12. package/cli/parse.mjs +5 -3
  13. package/cli/portability-migrations.mjs +46 -0
  14. package/cli/portability-worker.mjs +7 -20
  15. package/cli/portability.mjs +5 -4
  16. package/cli/runtime-front-door.mjs +1 -1
  17. package/cli/server-registry.mjs +43 -18
  18. package/cli/server-resources.mjs +38 -9
  19. package/global/CHANGELOG.md +14 -0
  20. package/global/README.md +7 -2
  21. package/global/launch/state.mjs +5 -4
  22. package/global/launch/stop.mjs +30 -8
  23. package/global/main.mjs +2 -1
  24. package/modules/dependencies/README.md +71 -1
  25. package/modules/dependencies/auth.mjs +4 -4
  26. package/modules/dependencies/crypto.mjs +6 -1
  27. package/modules/dependencies/custody.mjs +16 -0
  28. package/modules/dependencies/index.d.mts +1 -1
  29. package/modules/dependencies/index.mjs +6 -5
  30. package/modules/dependencies/schema/keys-sealed.sql +4 -0
  31. package/modules/dependencies/sealing.mjs +55 -0
  32. package/modules/dependencies/store.mjs +62 -9
  33. package/modules/deploy-kit/README.md +1 -0
  34. package/modules/deploy-kit/owner-cli.mjs +4 -2
  35. package/modules/deploy-kit/owner-rekey.mjs +69 -0
  36. package/modules/job-queue/command.mjs +8 -3
  37. package/modules/operator-auth/docs/native-accounts.md +4 -1
  38. package/modules/operator-auth/identity/oidc/aes-gcm.mjs +21 -0
  39. package/modules/operator-auth/identity/oidc/crypto.mjs +5 -12
  40. package/modules/operator-auth/index.d.mts +1 -1
  41. package/modules/portability/README.md +40 -11
  42. package/modules/portability/backfills.mjs +29 -16
  43. package/modules/portability/bridge.mjs +24 -29
  44. package/modules/portability/import.mjs +1 -2
  45. package/modules/portability/index.d.mts +1 -1
  46. package/modules/portability/ledgers.mjs +22 -0
  47. package/modules/portability/migration-history.mjs +2 -2
  48. package/modules/release/README.md +135 -3
  49. package/modules/release/catalogue.mjs +6 -2
  50. package/modules/release/listing.mjs +64 -0
  51. package/modules/release/look.mjs +26 -0
  52. package/modules/release/previous-listing.mjs +44 -0
  53. package/modules/release/produce.mjs +3 -0
  54. package/modules/release/screenshots.mjs +51 -0
  55. package/modules/release/signature.mjs +3 -2
  56. package/modules/release/source.mjs +19 -6
  57. package/modules/release/statement.mjs +8 -4
  58. package/onboarding/local/environment.mjs +2 -2
  59. package/onboarding/process-cleanup.mjs +5 -5
  60. package/onboarding/process-scope.mjs +1 -1
  61. package/onboarding/process-table.mjs +6 -4
  62. package/onboarding/process-tree.mjs +4 -4
  63. package/onboarding/schema/json-schema.mjs +4 -1
  64. package/package.json +1 -1
  65. package/scripts/publish-check/README.md +14 -0
  66. package/scripts/publish-check/assignment.py +48 -0
  67. package/scripts/publish-check/credential_assignments.mjs +75 -0
  68. package/scripts/publish-check/credential_assignments.py +70 -0
  69. package/scripts/publish-check/credential_literals.mjs +66 -0
  70. package/scripts/publish-check/credential_syntax.mjs +29 -0
  71. package/scripts/publish-check/env_values.py +14 -1
  72. package/scripts/publish-check/history_scan.py +9 -2
@@ -1,18 +1,71 @@
1
1
  import { keyPair } from './crypto.mjs';
2
- export function dependencyStore(db) {
2
+ import { requireInstallationKey, installationKeyId, sealDependencyKey, openDependencyKey } from './sealing.mjs';
3
+ import { AuthError } from '../operator-auth/errors.mjs';
4
+
5
+ async function sealingState(db, config, row, { retiredKeys = [], report } = {}) {
6
+ requireInstallationKey(config);
7
+ const state = row ?? await db.prepare('SELECT keys_sealed,key_ids FROM rer_dependency_installation WHERE singleton=1').first();
8
+ const ids = JSON.parse(state?.key_ids ?? '[]'), current = await installationKeyId(config.INSTALLATION_KEY);
9
+ const previous = config.INSTALLATION_KEY_PREVIOUS ? await installationKeyId(config.INSTALLATION_KEY_PREVIOUS) : null;
10
+ const available = [current, previous, ...await Promise.all(retiredKeys.map(installationKeyId))];
11
+ report?.({keyIds:ids, tail:ids.at(-1) ?? null, custody:ids.map(kid => ({kid, available:available.includes(kid)}))});
12
+ if (ids.length && !available.includes(ids.at(-1))) throw new AuthError('installation_key_unavailable', 503);
13
+ return { current, ids, options: { allowPlaintext: !state?.keys_sealed, knownKeyIds: ids.length ? ids : undefined, retiredKeys } };
14
+ }
15
+ // Closing the legacy branch is atomic with the last per-row upgrade. This SQL
16
+ // checks envelope shape only; reads never decrypt a sibling interface's key.
17
+ const finishUpgrade = (db, current) => db.prepare(`UPDATE rer_dependency_installation SET keys_sealed=1,key_ids=CASE WHEN json_array_length(key_ids)=0 THEN ? ELSE key_ids END WHERE singleton=1 AND NOT EXISTS(SELECT 1 FROM rer_dependency_keys WHERE (CASE WHEN json_valid(private_key) THEN json_extract(private_key,'$.version')=1 AND json_type(private_key,'$.kid')='text' AND json_type(private_key,'$.iv')='text' AND json_type(private_key,'$.ciphertext')='text' ELSE 0 END) IS NOT 1)`).bind(JSON.stringify([current]));
18
+
19
+ // Rekey alone opens the entire table, then checks the complete current set
20
+ // (including concurrent replacements) before authorizing key retirement.
21
+ export async function resealDependencyKeys(db, config = {}, custody = {}) {
22
+ const state = await sealingState(db, config, undefined, custody);
23
+ const rows = (await db.prepare('SELECT interface,private_key FROM rer_dependency_keys').all()).results;
24
+ const updates = [];
25
+ for (const row of rows) {
26
+ const value = await openDependencyKey(config, row.interface, row.private_key, state.options);
27
+ if (value.reseal) updates.push(db.prepare('UPDATE rer_dependency_keys SET private_key=? WHERE interface=? AND private_key=?').bind(await sealDependencyKey(config, row.interface, value.key), row.interface, row.private_key));
28
+ }
29
+ // Keep all historical IDs, with the current ID last, including a reopen.
30
+ const ids = [...state.ids.filter(id => id !== state.current), state.current];
31
+ await db.batch([...updates, db.prepare('UPDATE rer_dependency_installation SET keys_sealed=1,key_ids=? WHERE singleton=1').bind(JSON.stringify(ids))]);
32
+ for (const row of (await db.prepare('SELECT interface,private_key FROM rer_dependency_keys').all()).results) await openDependencyKey(config, row.interface, row.private_key, { currentOnly: true, knownKeyIds: ids });
33
+ return { resealed: updates.length };
34
+ }
35
+ export function dependencyStore(db, config = {}) {
3
36
  const first=(sql,...args)=>db.prepare(sql).bind(...args).first();
4
37
  const run=(sql,...args)=>db.prepare(sql).bind(...args).run();
38
+ async function identity() {
39
+ requireInstallationKey(config);
40
+ const ids = JSON.stringify([await installationKeyId(config.INSTALLATION_KEY)]);
41
+ await run('INSERT INTO rer_dependency_installation(singleton,sector_id,keys_sealed,key_ids) VALUES(1,?,1,?) ON CONFLICT DO NOTHING',crypto.randomUUID(),ids);
42
+ // An archive retains the sector but omits signing keys. A fresh relink can
43
+ // establish destination custody only while no private row needs an old key.
44
+ await run('UPDATE rer_dependency_installation SET keys_sealed=1,key_ids=? WHERE singleton=1 AND NOT EXISTS(SELECT 1 FROM rer_dependency_keys)',ids);
45
+ return first('SELECT * FROM rer_dependency_installation WHERE singleton=1');
46
+ }
47
+ async function readKey(contract = 'oidc-provider@1') {
48
+ requireInstallationKey(config);
49
+ const row = await first('SELECT k.private_key,i.keys_sealed,i.key_ids FROM rer_dependency_keys k LEFT JOIN rer_dependency_installation i ON i.singleton=1 WHERE k.interface=?',contract);
50
+ if (!row) return null;
51
+ const state = await sealingState(db, config, row);
52
+ const value = await openDependencyKey(config, contract, row.private_key, state.options);
53
+ if (!value.reseal) return { key: value.key, sealedKey: row.private_key };
54
+ await db.batch([
55
+ db.prepare('UPDATE rer_dependency_keys SET private_key=? WHERE interface=? AND private_key=?').bind(await sealDependencyKey(config, contract, value.key), contract, row.private_key),
56
+ finishUpgrade(db, state.current),
57
+ ]);
58
+ // Use the persisted winner if unlink/replacement raced our CAS.
59
+ return readKey(contract);
60
+ }
5
61
  return {
6
- first,run,
62
+ first,run,readKey,identity,
7
63
  async installation(contract = 'oidc-provider@1') {
8
- await run('INSERT INTO rer_dependency_installation(singleton,sector_id) VALUES(1,?) ON CONFLICT DO NOTHING',crypto.randomUUID());
9
- const row=await first('SELECT * FROM rer_dependency_installation WHERE singleton=1');
10
- let key=await first('SELECT private_key FROM rer_dependency_keys WHERE interface=?',contract);
11
- if(!key) {
12
- await run('INSERT INTO rer_dependency_keys(interface,private_key) VALUES(?,?) ON CONFLICT DO NOTHING',contract,JSON.stringify(await keyPair()));
13
- key=await first('SELECT private_key FROM rer_dependency_keys WHERE interface=?',contract);
64
+ const row=await identity();
65
+ if(!await first('SELECT 1 FROM rer_dependency_keys WHERE interface=?',contract)) {
66
+ await run('INSERT INTO rer_dependency_keys(interface,private_key) VALUES(?,?) ON CONFLICT DO NOTHING',contract,await sealDependencyKey(config,contract,await keyPair()));
14
67
  }
15
- return {...row,key:JSON.parse(key.private_key)};
68
+ return {...row,...await readKey(contract)};
16
69
  },
17
70
  async link(contract = 'oidc-provider@1') { const row=await first("SELECT * FROM rer_dependency_link WHERE interface=?",contract);return row?{...row,metadata:JSON.parse(row.metadata)}:null; },
18
71
  };
@@ -123,6 +123,7 @@ A transport failure carries `providerTransport`: the operation, sanitized endpoi
123
123
  - **Hosted owner.** After deploy and health pass, rer claims the setup owner on the hosted app: it POSTs the setup token to the seed's standard owner step (`hooks.owner`, by default `/auth/bootstrap`). 303 means claimed; `409 already_initialized` means an owner was already claimed and is kept. Then rer deletes the token secret from the Worker, records the new Worker version in the state file, and lists the token in `retiredSecrets`, so later plans no longer declare it. The build fingerprint still names it, and the recorded claim stays as recorded, so an untouched repeat deploy writes nothing. A Worker that still holds a token rer did not upload in this run (an older deploy) first gets a fresh token through the same recorded secret change. Dependency approvals that need the token run before the claim. A seed with an owner step function, an identity-provider claim, or no declared token secret claims its owner its own way.
124
124
  - **Several secrets at once.** `rer deploy --set-secret MAIL_API_KEY --set-secret MAIL_FROM` or `rer deploy --set-secret MAIL_API_KEY,MAIL_FROM` (likewise `--unset-secret A,B`) changes all of them in one new Worker version or none: a group of secrets is never half set. Every named secret must be declared and, for `--set-secret`, have its value in the environment or `--env-file`; otherwise rer names each missing one and changes nothing. One secret uses Cloudflare's secret call. Several are one copy of the live version through the Versions API (its own modules, assets and compatibility settings, every other binding inherited), then deployed at 100%: Cloudflare's per-secret calls each make a version, and its settings call can add secrets but not remove them (measured 17 October 2026). The copy carries a marker rer records first, so a copy whose answer was lost is recognised, and one uploaded but not deployed is deployed by the same command run again.
125
125
  - **Secret changes.** `rer deploy --set-secret NAME` (value from the environment or `--env-file`, under the declaration's environment name), `--unset-secret NAME` and `--rotate-secret NAME[,NAME]` (a fresh value for a secret rer generates, kept in `.rer/deploy/<environment>.secrets.env` once set) change declared secrets of the recorded Worker without a build. Before writing, rer records the starting version and the exact live names of all other secrets, never their values. Accepted or ambiguous writes retain that intent until readback confirms the change; explicit refusals clear it. An ambiguous failure, or a later command's preflight, can clear an unapplied intent only when the live deployment still matches the receipt and the newest listed version matches it too. A surviving intent blocks further mutations until consistency returns; retry the same command then. Recovery requires the recorded immediate successor (or the recorded confirming rotation's successor), the receipt's script etag and fingerprint, the intended binding's presence, and an unchanged set of other secret names. Ambiguous rotations require a confirming PUT before reporting success. Older intents without names may recover only from the exact starting version's historical bindings, with its id, etag and fingerprint verified; unavailable or malformed historical bindings refuse adoption. Each historical binding must have an exact provider type supported by this kit; unrecognized types cannot prove that a name was not a secret. Added, removed or renamed other secrets refuse deploy and removal before writes. Values remain opaque: these checks cannot distinguish an otherwise identical outside change to the pending binding.
126
+ - **Installation key custody.** The setup manifest generates `INSTALLATION_KEY` independently of `SESSION_SECRET`. Hosted deployment keeps it in the same private, Git-ignored `.rer/deploy/<environment>.secrets.env`, before its first upload; `INSTALLATION_KEY_PREVIOUS` is optional and never generated by setup. `rer deploy --rotate-secret INSTALLATION_KEY` first saves a new/current and old/previous pair there, publishes both bindings in one Worker version, then reseals every dependency-key row through the recorded D1 database and verifies that every row opens with the current key. Only then does it remove the previous Worker binding. Routine rotation uses only the current/previous pair; older retired keys require explicit reopen. By default, every retired key remains in the same private file as `SESSION_SECRET`, in `INSTALLATION_KEY_RETIRED=<kid>:<value>[,<kid>:<value>...]`; retired keys never enter routine Worker bindings or archives. Resealing uses the [D1 transactional batch API](https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/query/). Worker receipt and D1 identity checks precede database access. Any failure retains both saved values; rerun the same rotation command to resume with that exact pair. A D1 backup or Time Travel restore older than a rotation needs the corresponding retired key: `rer deploy --reopen-installation-key KID` publishes current plus only that retired key as previous, reseals and verifies every row through the same recorded D1, then removes the previous binding while retaining all retired custody. CLI-side reopen rekey uses every validated retired key in private custody to open mixed mid-rotation snapshots; only the selected pair is published to the Worker. Before opening rows, rekey reports the D1 `key_ids` tail and custody availability for every history ID. A whole pre-rotation restore with an unavailable tail reports `installation_key_unavailable`; a known row ID without its key reports `installation_key_version_unknown` only after the tail custody check succeeds. Failures retain the reopen marker and exact pair; repeat that command to finish. Missing retired custody refuses with `installation_key_unavailable` before provider writes: recover the private file before reopening. Back up private custody securely for as long as older database recovery points exist. Retain each retired key until no D1 Time Travel recovery point or exported backup predates the completed rotation that retired it or otherwise needs that key. After any rotation or reopen has finished, edit `.rer/deploy/<environment>.secrets.env` and remove only its `<kid>:<value>` entry from `INSTALLATION_KEY_RETIRED`, keeping valid comma separators; remove the assignment if no entries remain. Preserve the current key, all other entries and the file's owned 0600 permissions. There is no per-key removal command; reopening a removed ID refuses before provider writes unless its custody is restored. Base before #367 cannot open sealed keys; roll forward to sealing-capable code. The CLI has neither a Base-version capability receipt nor a code rollback operation, so out-of-band provider rollbacks cannot be checked here; current Base rejects sealed envelopes passed to plaintext signing with `installation_key_sealed_rollback`. Other deployment changes wait for completion. Manual installation-key set/unset, a conflicting supplied current key and a supplied previous key are refused. Without the current private key, restore custody from the deploying checkout before rotating. Neither keys nor database ciphertext appear in output.
126
127
  - **Removal.** `rer deploy --remove --yes` deletes what the state file says rer created for this environment: the Worker (schedules first; its Durable Objects go with it) and each D1, R2, KV and queue that is still the one rer created, after checking all of them before the first delete. A replaced or uncertain resource refuses the whole removal, except a D1 or KV whose create answer was lost: under this environment's derived name it is deleted once confirmed empty (a D1 with no tables, a KV namespace with no keys); one holding data is refused as `uncertain-resource-not-empty`, and an uncertain queue, whose backlog rer cannot read, as `uncertain-resource-unverifiable` (delete it by hand if it is the empty one from the interrupted run).
127
128
 
128
129
  R2 removal requires a saved `created` identity (the bucket's creation date). Pending buckets or buckets missing that identity are refused as `uncertain-resource-create`, and same-named replacements as `owned-resource-replaced`, even if empty. For a confirmed bucket, removal automatically lists and deletes its objects before deleting the bucket, checking its identity again before every object delete and the bucket delete. Serialize outside bucket deletion and recreation with removal: the provider cannot make a delete conditional on that identity.
@@ -13,11 +13,12 @@ import { activeWorker, secretBindings } from './worker.mjs';
13
13
  import { ownerSecret } from './owner-secret.mjs';
14
14
  import { ownerRemove } from './owner-remove.mjs';
15
15
  import { ownerSignin } from './owner-signin.mjs';
16
+ import { ownerRekey } from './owner-rekey.mjs';
16
17
  import { refuseLocalModels } from './local-only.mjs';
17
18
  import { publicOrigin } from './public-origin.mjs';
18
19
 
19
20
  export const cronNotice = 'This Worker has a Cron Trigger. Cloudflare may take up to about two hours to start its first scheduled run on some accounts.';
20
- const usage = 'usage: owner-cli.mjs deploy|dry-run|health|remove|inspect|record-owner|signin --declaration <file> --environment <name> [--product <dir>] [--state <file>] [--outcome claimed|kept] | secret ... --set NAME [--set NAME]|--unset NAME [--unset NAME]';
21
+ const usage = 'usage: owner-cli.mjs deploy|dry-run|health|remove|inspect|record-owner|signin|rekey --declaration <file> --environment <name> [--product <dir>] [--state <file>] [--outcome claimed|kept] | secret ... --set NAME [--set NAME]|--unset NAME [--unset NAME]';
21
22
 
22
23
  /** The owner's committed source and existing local build. Run the seed's build first. */
23
24
  async function ownerArtifact(root, product) {
@@ -33,7 +34,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
33
34
  try { parsed = parseArgs({args:argv,allowPositionals:true,strict:true,options:{declaration:{type:'string'},environment:{type:'string'},product:{type:'string',default:''},state:{type:'string'},set:{type:'string',multiple:true},unset:{type:'string',multiple:true},outcome:{type:'string'}}}); }
34
35
  catch { throw new Error('usage'); }
35
36
  const {positionals:[command,...rest],values} = parsed;
36
- if (!['deploy','dry-run','health','secret','remove','inspect','record-owner','signin'].includes(command) || rest.length || !values.declaration || !values.environment) throw new Error('usage');
37
+ if (!['deploy','dry-run','health','secret','remove','inspect','record-owner','signin','rekey'].includes(command) || rest.length || !values.declaration || !values.environment) throw new Error('usage');
37
38
  if ((command === 'secret') !== (values.set !== undefined || values.unset !== undefined) || values.set !== undefined && values.unset !== undefined) throw new Error('usage');
38
39
  if (values.outcome !== undefined && command !== 'record-owner' || command === 'record-owner' && !['claimed','kept'].includes(values.outcome)) throw new Error('usage');
39
40
  await refuseLocalModels(resolve(cwd));
@@ -71,6 +72,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
71
72
  : await ownerTarget(api,{project:declaration.project,environment:values.environment});
72
73
  const statePath = values.state ? resolve(root,values.state) : join(root,'.rer','deploy',`${target.environment}.json`);
73
74
  if (command === 'remove') return {ok:true,...await ownerRemove({api,target,statePath}).catch(error => { throw api.annotateFailure(error,'remove'); })};
75
+ if (command === 'rekey') return ownerRekey({api,target,statePath,declaration,env,notice});
74
76
  if (command === 'secret') {
75
77
  // Every named secret is changed in one Worker version, or none is.
76
78
  const named = [...new Set(values.set ?? values.unset)], declared = Array.isArray(declaration.secrets) ? declaration.secrets : [];
@@ -0,0 +1,69 @@
1
+ import { assert, uuid } from './validation.mjs';
2
+ import { activeWorker } from './worker.mjs';
3
+ import { checkResourceIdentity, readOwnerState, sameDeployment, withStateLock, ownerResourceName } from './owner.mjs';
4
+ import { retiredInstallationKeys } from '../dependencies/custody.mjs';
5
+
6
+ /** The D1 REST batch is one transaction, matching the Worker's db.batch contract.
7
+ * Nothing from query responses/errors is allowed into deployment diagnostics. */
8
+ function database(api, id) {
9
+ const execute = async body => {
10
+ try {
11
+ const rows = await api.request(`/d1/database/${id}/query`, { method: 'POST', body });
12
+ assert(Array.isArray(rows) && rows.every(row => row?.success === true && Array.isArray(row.results)), 'dependency-rekey-invalid-result');
13
+ return rows;
14
+ } catch { throw new Error('dependency-rekey-query-failed'); }
15
+ };
16
+ const prepare = (sql, params = []) => ({
17
+ sql, params,
18
+ bind(...values) { return prepare(sql, values); },
19
+ async all() { return (await execute({sql, params}))[0]; },
20
+ async first() { return (await this.all()).results[0] ?? null; },
21
+ async run() { return (await execute({sql, params}))[0]; },
22
+ });
23
+ return { prepare, batch: statements => statements.length ? execute({batch:statements.map(({sql,params}) => ({sql,params}))}) : Promise.resolve([]) };
24
+ }
25
+
26
+ /** Reseal the dependency rows in the declared DB only while the receipt still
27
+ * proves both the Worker and its recorded D1 resource belong to this deployment. */
28
+ export async function ownerRekey({ api, target, statePath, declaration, env, notice = line => console.error(line) }) {
29
+ return withStateLock(statePath, async () => {
30
+ const state = await readOwnerState(statePath, target);
31
+ assert(state.receipt && !state.pendingUpload && !state.pendingSecret, 'missing-owner-receipt');
32
+ const owned = async () => {
33
+ const live = await activeWorker(api, target.worker);
34
+ assert(live && sameDeployment(live, state.receipt.provider), 'owned-resource-replaced');
35
+ return live;
36
+ };
37
+ const live = await owned();
38
+ const binding = declaration.d1?.find(item => item.binding === 'DB');
39
+ assert(binding, 'dependency-rekey-missing-database');
40
+ const name = ownerResourceName(target.worker, binding.binding), entry = state.owned.d1[name];
41
+ assert(entry?.status === 'created' && uuid(entry.id), 'dependency-rekey-missing-database');
42
+ const resource = await api.request(`/d1/database/${entry.id}`);
43
+ checkResourceIdentity('d1', name, resource, entry);
44
+ assert(resource?.name === name, 'owned-resource-replaced');
45
+ assert(live.settings?.bindings?.some(item => item.name === binding.binding && item.type === 'd1' && item.id === entry.id), 'dependency-rekey-database-mismatch');
46
+ const config = {};
47
+ for (const key of ['INSTALLATION_KEY', 'INSTALLATION_KEY_PREVIOUS']) {
48
+ const secret = declaration.secrets?.find(item => item.binding === key);
49
+ assert(secret && typeof env[secret.environment] === 'string' && env[secret.environment].trim(), 'dependency-rekey-missing-key');
50
+ config[key] = env[secret.environment];
51
+ assert(live.settings?.bindings?.some(item => item.name === key && item.type === 'secret_text'), 'dependency-rekey-missing-key');
52
+ }
53
+ let result;
54
+ try {
55
+ const { resealDependencyKeys } = await import('../dependencies/store.mjs');
56
+ const retired = await retiredInstallationKeys(env.INSTALLATION_KEY_RETIRED);
57
+ result = await resealDependencyKeys(database(api, entry.id), config, {
58
+ retiredKeys:[...retired.values()],
59
+ report: status => {
60
+ // D1 is untrusted: only well-formed public key IDs enter diagnostics.
61
+ const publicId = kid => /^[A-Za-z0-9_-]{43}$/.test(kid) ? kid : '(invalid key ID)';
62
+ notice(`D1 key_ids tail: ${status.tail === null ? '(empty)' : publicId(status.tail)}; custody: ${status.custody.map(({kid,available}) => `${publicId(kid)}=${available ? 'in custody' : 'unavailable'}`).join(', ') || '(empty)'}`);
63
+ },
64
+ });
65
+ } catch { throw new Error('dependency-rekey-failed'); }
66
+ await owned();
67
+ return { ok: true, resealed: result.resealed };
68
+ });
69
+ }
@@ -51,13 +51,18 @@ export async function execute(command, job, config, adopt, signal, onStarted, op
51
51
  } finally {
52
52
  signal.removeEventListener('abort', abort);
53
53
  try {
54
- if (owner) await stopGroup(owner, config.killGraceMs);
55
- else child.kill('SIGKILL'); // The supervisor has not been given GO.
54
+ // Stop escaped descendants through custody first, then finish the group
55
+ // this run created even if custody cleanup fails. The ready owner's start
56
+ // time and unique marker protect against a replacement group leader.
57
+ try { await options.cleanup?.(child,outcome); }
58
+ finally {
59
+ if (owner) await stopGroup(owner, config.killGraceMs);
60
+ else child.kill('SIGKILL'); // The supervisor has not been given GO.
61
+ }
56
62
  // Its group is stopped; output still arriving on a dead supervisor's pipes
57
63
  // comes from escaped descendants, which the caller's cleanup stops.
58
64
  if (outcome?.supervisorEnded) { child.stdout?.destroy(); child.stderr?.destroy(); }
59
65
  await exited;
60
- await options.cleanup?.(child,outcome);
61
66
  if (owner?.socket) await unlink(owner.socket).catch(error => { if (error.code !== 'ENOENT') throw error; });
62
67
  } catch (error) { error.keepLease = true; throw error; }
63
68
  }
@@ -208,7 +208,10 @@ trust/configuration change also changes the transaction fingerprint.
208
208
 
209
209
  Apply **only** `identity/oidc/schema/oidc.sql` for shared identity storage, and
210
210
  `modules/dependencies/schema/dependencies.sql` followed by
211
- `modules/dependencies/schema/approval-attempt.sql` if dependency linking is enabled.
211
+ `modules/dependencies/schema/approval-attempt.sql` and
212
+ `modules/dependencies/schema/keys-sealed.sql` if dependency linking is enabled.
213
+ Existing adopters append `keys-sealed.sql` as a new numbered migration; it
214
+ records completion of legacy sealing and nonsecret installation key versions.
212
215
  Existing native adopters add `approval-attempt.sql` as a new numbered migration;
213
216
  it invalidates old approvals, which must be renewed through local owner proof.
214
217
  Their `oa_oidc_*`, `oa_identity_providers` and provider-health names are identity
@@ -0,0 +1,21 @@
1
+ import { check } from './errors.mjs';
2
+ export const utf8 = new TextEncoder();
3
+ export function encode(bytes) { return btoa(String.fromCharCode(...new Uint8Array(bytes))).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', ''); }
4
+ export function decode(value) {
5
+ check(typeof value === 'string' && /^[A-Za-z0-9_-]+$/.test(value));
6
+ const bytes = Uint8Array.from(atob(value.replaceAll('-', '+').replaceAll('_', '/')), c => c.charCodeAt(0));
7
+ check(encode(bytes) === value);
8
+ return bytes;
9
+ }
10
+ // Both short-lived OIDC transactions and installation data use these primitives.
11
+ // Callers own key derivation and the storage envelope, preserving the OIDC format.
12
+ export async function encryptJson(key, aad, value) {
13
+ const iv = crypto.getRandomValues(new Uint8Array(12));
14
+ const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: utf8.encode(aad), tagLength: 128 }, key, utf8.encode(JSON.stringify(value)));
15
+ return { iv: encode(iv), ciphertext: encode(ciphertext) };
16
+ }
17
+ export async function decryptJson(key, aad, value) {
18
+ const iv = decode(value.iv);
19
+ check(iv.length === 12);
20
+ return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(await crypto.subtle.decrypt({ name: 'AES-GCM', iv, additionalData: utf8.encode(aad), tagLength: 128 }, key, decode(value.ciphertext))));
21
+ }
@@ -1,12 +1,6 @@
1
1
  import { check } from './errors.mjs';
2
- export const utf8 = new TextEncoder();
3
- export function encode(bytes) { return btoa(String.fromCharCode(...new Uint8Array(bytes))).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', ''); }
4
- export function decode(value) {
5
- check(typeof value === 'string' && /^[A-Za-z0-9_-]+$/.test(value));
6
- const bytes = Uint8Array.from(atob(value.replaceAll('-', '+').replaceAll('_', '/')), c => c.charCodeAt(0));
7
- check(encode(bytes) === value);
8
- return bytes;
9
- }
2
+ import { utf8, encode, encryptJson, decryptJson } from './aes-gcm.mjs';
3
+ export { utf8, encode, decode } from './aes-gcm.mjs';
10
4
  export function random() { return encode(crypto.getRandomValues(new Uint8Array(32))); }
11
5
  export function isRandom(value) { return typeof value === 'string' && /^[A-Za-z0-9_-]{43}$/.test(value); }
12
6
  export async function digest(value) { return encode(await crypto.subtle.digest('SHA-256', utf8.encode(value))); }
@@ -18,12 +12,11 @@ async function encryptionKey(secret) {
18
12
  return crypto.subtle.importKey('raw', await crypto.subtle.digest('SHA-256', utf8.encode(`oidc-v1:encryption\0${secret}`)), 'AES-GCM', false, ['encrypt', 'decrypt']);
19
13
  }
20
14
  export async function seal(secret, stateHash, value) {
21
- const iv = crypto.getRandomValues(new Uint8Array(12));
22
- const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: utf8.encode(stateHash) }, await encryptionKey(secret), utf8.encode(JSON.stringify(value)));
23
- return `${encode(iv)}.${encode(ciphertext)}`;
15
+ const { iv, ciphertext } = await encryptJson(await encryptionKey(secret), stateHash, value);
16
+ return `${iv}.${ciphertext}`;
24
17
  }
25
18
  export async function unseal(secret, stateHash, value) {
26
19
  const [iv, ciphertext, extra] = value.split('.');
27
20
  check(!extra);
28
- return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(await crypto.subtle.decrypt({ name: 'AES-GCM', iv: decode(iv), additionalData: utf8.encode(stateHash) }, await encryptionKey(secret), decode(ciphertext))));
21
+ return decryptJson(await encryptionKey(secret), stateHash, { iv, ciphertext });
29
22
  }
@@ -43,7 +43,7 @@ export interface AuthConfig {
43
43
  dependencies?: boolean;
44
44
  /** Name: 1-80 characters; accent: #rrggbb; signInPath: same-origin absolute path. */
45
45
  branding?: AuthBranding;
46
- origin: string; OWNER_EMAIL: string; SESSION_SECRET: string; SETUP_TOKEN?: string; MAIL_FROM?: string;
46
+ origin: string; OWNER_EMAIL: string; SESSION_SECRET: string; INSTALLATION_KEY?: string; INSTALLATION_KEY_PREVIOUS?: string; SETUP_TOKEN?: string; MAIL_FROM?: string;
47
47
  /** Local HTTP is allowed only for literal localhost, 127.0.0.1 or [::1] with matching Host. */
48
48
  local?: boolean; allowList?: string[]; sessionSeconds?: number; linkSeconds?: number;
49
49
  /** Invitation lifetime in seconds, up to seven days (the default); independent of sign-in/step-up links. */
@@ -110,6 +110,27 @@ it continues to accept serializable object policies. Malformed primary or later
110
110
  journal JSON is reported once as `archive_json` with exit 2, before local data
111
111
  migrations, without echoing archive content or parser diagnostics.
112
112
 
113
+ An installation migrated by Platform has `rer_migrations(name, sha256,
114
+ applied_at)` instead of Wrangler's ledger. Local transfers match each entry to
115
+ exactly one migration file by SHA-256 and preserve its application order. They
116
+ verify all local files are recorded and skip Wrangler; a pending file refuses
117
+ as `migration_ledger:unapplied:<file>` until Platform applies it. Unknown,
118
+ ambiguous or conflicting entries refuse by name, without replaying SQL or
119
+ creating a second ledger.
120
+
121
+ For old archives containing only Platform's migration names, the seed adapter
122
+ may declare `platformMigrations: {prefix, dataOnly}` on `localPortability`.
123
+ Read `prefix` from the seed's own deploymentSpec packaging `migrationPrefix`
124
+ for this binding's migrations directory. It declares an alias `prefix + file`
125
+ for each file; it does not strip arbitrary prefixes. `dataOnly` is an explicit
126
+ list of installation-specific Platform seed entries without schema files.
127
+ Declare only SQL that seeds data and changes no schema: these names are omitted
128
+ from schema replay. This does not authorize replacing their rows; the archive
129
+ policy still controls that. Archive SQL and names never supply these declarations.
130
+ Custom replay runners can supply each plan entry's `aliases` and pass
131
+ `{dataOnly}` as `migrationReplay`'s third argument. Verified archive ledger
132
+ hashes take precedence over name-only placement where available.
133
+
113
134
  When a policy includes operator-auth tables, local dev identity still never
114
135
  travels, and no policy option changes that: export drops `oa_sessions` rows of
115
136
  kind `dev`, every credential recorded in `oa_dev_credentials` (and that table's
@@ -322,7 +343,7 @@ literals, numbers and comments never count. Text Base cannot read to a
322
343
  statement boundary, such as an unclosed block comment, is refused. An archived table the archive holds no
323
344
  rows of restores empty, so an `UPDATE` or `DELETE` of it changes nothing and is
324
345
  allowed regardless of what it reads: no row can change or fire a trigger. An
325
- ordinary `INSERT` into it remains refused. For archives without a migration record,
346
+ `INSERT` into it must qualify for replay below. For archives without a migration record,
326
347
  every migration after the earliest prefix that gives the archive's schema is
327
348
  checked, since the archive may predate any of them.
328
349
 
@@ -337,11 +358,18 @@ destination definition, so a row rejected by its CHECK fails the whole atomic
337
358
  restore; no row is skipped or coerced. Other statements still face the usual
338
359
  data classifier, and a proven preserving block is not a competing backfill writer.
339
360
 
340
- A backfill of a catalogue table the archive does not carry (one a later
341
- migration adds, restored empty) is **replayed** instead of refused: an
342
- `INSERT ... SELECT` (or `INSERT ... VALUES`) into that table. Finalize empties
343
- the table (resetting its `AUTOINCREMENT` counter, as the live upgrade created
344
- it) and runs each of its later writes in migration order, exactly as written,
361
+ A backfill of a catalogue table is **replayed** instead of refused when it is
362
+ an eligible `INSERT ... SELECT` (or `INSERT ... VALUES`) or `REPLACE`. A table
363
+ the archive carries keeps its archived rows, including when it carries none:
364
+ the replay runs over the rows the live migration would have found there. It
365
+ must name its target columns and be the table's only later writer. For a table
366
+ with `AUTOINCREMENT`, carried-table replay is refused: the archive does not
367
+ retain `sqlite_sequence`'s high-water mark, so generated keys could differ.
368
+ Reads of a keyed table's unarchived hidden `rowid`, `_rowid_` or `oid` also
369
+ refuse, including through views: restoring its keys can change physical rowids.
370
+ For a table the archive lacks (one a later migration adds), finalize empties the table
371
+ (resetting its `AUTOINCREMENT` counter, as the live upgrade created it).
372
+ Finalize runs the eligible writes in migration order, exactly as written,
345
373
  after every archived row is inserted and while the destination's triggers are
346
374
  still dropped, in the same atomic D1 transaction as the rest of the restore. The
347
375
  table then holds what the live upgrade gave it. Time functions such as
@@ -349,8 +377,7 @@ table then holds what the live upgrade gave it. Time functions such as
349
377
  `INSERT ... SELECT` without `ORDER BY` assigns new keys in the order SQLite
350
378
  scans the restored rows. Rows the restore normalizes (import safety) are read
351
379
  as restored. The restore is refused with
352
- `schema_mismatch:migration_data:<migration>` instead when the archive carries
353
- the table (even with no rows), when a write to it is not a plain `INSERT` or
380
+ `schema_mismatch:migration_data:<migration>` instead when a write is not an `INSERT` or
354
381
  `REPLACE`, reads any table other than archived ones and itself (including through
355
382
  a view resolved from applied migration history), selects `*` or
356
383
  a secret column, when a trigger on it or a foreign key action from it reaches a
@@ -366,7 +393,8 @@ an INSERT reading a view must still resolve entirely to carried tables.
366
393
 
367
394
  The same check runs when the archive's schema identity equals the destination's:
368
395
  a later migration that only purges or backfills rows leaves the schema unchanged,
369
- yet the archive's rows never passed through it. Such an archive is refused with
396
+ yet the archive's rows never passed through it. Eligible carried-table backfills
397
+ replay here too; other rewrites are refused with
370
398
  `schema_mismatch:migration_data:<migration>` (local `rer import` exit 4, naming
371
399
  the migration) before any staging or import job; one whose later migrations only
372
400
  add indexes or touch excluded tables restores as usual. Without a replayer, an
@@ -690,11 +718,12 @@ all existing migrations and their ledger. Wire package.json:
690
718
  Before copying, capture the source checkout's actual origin with
691
719
  `git remote get-url origin` and its full revision with `git rev-parse HEAD`.
692
720
  Record this component in `edition.json`, replacing `ORIGIN` and `PIN` with those
693
- values. Keep the real source provenance in the edition's component and source
721
+ values and `MODULE_ID` with the component's existing edition identifier. Keep
722
+ the real source provenance in the edition's component and source
694
723
  lock rather than embedding a repository address in the copied module:
695
724
 
696
725
  ```json
697
- {"id":"runeditrun/portability","role":"module","source":{"kind":"local","paths":["vendor/base/modules/portability"],"origin":{"repository":"ORIGIN","revision":"PIN","paths":["modules/portability"]}}}
726
+ {"id":"MODULE_ID","role":"module","source":{"kind":"local","paths":["vendor/base/modules/portability"],"origin":{"repository":"ORIGIN","revision":"PIN","paths":["modules/portability"]}}}
698
727
  ```
699
728
 
700
729
  Follow the seed's edition-authoring source-lock workflow, then prove the copy:
@@ -11,9 +11,9 @@ import { replayViews, viewReads } from './replay-views.mjs';
11
11
  // restorable catalogue (an excluded, derived or platform table) and whose
12
12
  // writes reach no catalogue table through a trigger or foreign key action
13
13
  // (reachingTables) changes no restored row, whatever it reads: admitted.
14
- // - One whose target is a catalogue table the archive does not carry (restored
15
- // empty) is replayed at finalize when it inserts seed or backfill rows.
16
- // Every later write to that table must then be a
14
+ // - An eligible seed or backfill of a catalogue table replays at finalize.
15
+ // A carried table keeps its archived rows and admits only one later writer,
16
+ // naming its target columns. An absent table is reset first; each later write is a
17
17
  // replayable INSERT: they run in migration order, exactly as written, after
18
18
  // the archived rows are inserted and with the destination's triggers still
19
19
  // dropped, so the table holds what the live upgrade gave it. A replay may read
@@ -33,7 +33,7 @@ import { replayViews, viewReads } from './replay-views.mjs';
33
33
  export function laterData({applied,unapplied,replay,catalogue,archived,empty,tables,secrets,manifest}) {
34
34
  const split=name=>replay.statements(name).flatMap(splitStatements),fold=list=>list.map(folded);
35
35
  const statements=applied.flatMap(split);
36
- const restorable=fold(catalogue),carried=fold(archived),absent=restorable.filter(name=>!carried.includes(name));
36
+ const restorable=fold(catalogue),carried=fold(archived);
37
37
  const reached=reachingTables(statements,carried),writable=reachingTables(statements,restorable);
38
38
  const later=unapplied.flatMap(migration=>split(migration).map((sql,index)=>({migration,index,sql,data:dataStatement(sql)})));
39
39
  const {admitted,tables:rebuilt}=preservingRebuilds(later,manifest,tables,statements),views=replayViews(statements);
@@ -46,14 +46,14 @@ export function laterData({applied,unapplied,replay,catalogue,archived,empty,tab
46
46
  for(const s of later) {
47
47
  if(outside.includes(s) || admitted.has(s))continue;
48
48
  s.today=changesData(s.sql,[...reached,...tainted],empty);
49
- if(s.data && absent.includes(s.data.target)){if(!writes.has(s.data.target))writes.set(s.data.target,[]);writes.get(s.data.target).push(s);}
49
+ if(s.data && restorable.includes(s.data.target)){if(!writes.has(s.data.target))writes.set(s.data.target,[]);writes.get(s.data.target).push(s);}
50
50
  else if(s.today)refused.add(s);
51
51
  }
52
52
  const replays=[];
53
53
  for(const [table,list] of writes) {
54
54
  // Seed INSERTs into a new table replay too: the rows already present in a
55
55
  // fresh destination must be reset and derived from the restored installation.
56
- const bad=list.filter(s=>!replayable(s,{table,statements,restorable,writable,carried,tables:known,secrets,views}));
56
+ const bad=carried.includes(table) && list.length>1?list:list.filter(s=>!replayable(s,{table,statements,restorable,writable,carried,tables:known,secrets,views,manifest}));
57
57
  // Writes confined to tables created after the archive retain their
58
58
  // admission. Replay eligible seeds, but do not turn a new-table-only
59
59
  // upgrade into a refused backfill. View reads always require resolution.
@@ -66,11 +66,24 @@ export function laterData({applied,unapplied,replay,catalogue,archived,empty,tab
66
66
  const first=later.find(s=>refused.has(s));
67
67
  insist(!first,'schema_mismatch:migration_data:'+first?.migration);
68
68
  const named=Object.fromEntries(catalogue.map(name=>[folded(name),name]));
69
- return {rebuilt,replays:later.filter(s=>replays.includes(s)).map(s=>({migration:s.migration,statement:s.index,table:named[s.data.target],sql:s.sql}))};
69
+ return {rebuilt,replays:later.filter(s=>replays.includes(s)).map(s=>({migration:s.migration,statement:s.index,table:named[s.data.target],sql:s.sql,...(carried.includes(s.data.target)?{carried:true}:{})}))};
70
70
  }
71
- function replayable({data},{table,statements,restorable,writable,carried,tables,secrets,views}) {
71
+ function replayable({data},{table,statements,restorable,writable,carried,tables,secrets,views,manifest}) {
72
72
  if(!['insert','replace'].includes(data.verb) || data.star)return false;
73
- if(!viewReads(data.names,data.words,{views,tables,carried,table,secrets}))return false;
73
+ if(carried.includes(table)) {
74
+ // sqlite_sequence's high-water mark is not archived, so replay cannot
75
+ // reproduce a carried AUTOINCREMENT table's live key allocation.
76
+ if(!data.columns || autoincrement(manifest.find(t=>folded(t.name)===table).sql))return false;
77
+ // Keyed tables carry their key columns, not their hidden physical rowids.
78
+ // Treat unarchived aliases as unreadable cells, including through views.
79
+ secrets={...secrets};
80
+ for(const t of manifest)if(!t.rowidAlias) {
81
+ const hidden=['rowid','_rowid_','oid'].filter(name=>!t.columns.some(c=>folded(c.name)===name));
82
+ secrets[t.name]=[...(secrets[t.name]??[]),...hidden];
83
+ }
84
+ }
85
+ // A carried target participates in secret checks, even through a view.
86
+ if(!viewReads(data.names,data.words,{views,tables,carried,table:carried.includes(table)?undefined:table,secrets}))return false;
74
87
  if(triggerBodies(statements,table).some(name=>writable.includes(name)))return false;
75
88
  // Foreign key actions: a row the replay deletes or rekeys (REPLACE, upsert) must reach no other catalogue table.
76
89
  return !reachingTables(statements,restorable.filter(name=>name!==table)).includes(table);
@@ -85,18 +98,18 @@ function writesAnother(s,table,reach,replays) {
85
98
  return !['create table','create index','drop index','create view','drop view','trigger'].includes(shape);
86
99
  }
87
100
  // Finalize statements for the job's replays, to run after the archived rows are
88
- // inserted and before the triggers are recreated: each replayed table is
89
- // emptied (and its AUTOINCREMENT counter reset, as the live upgrade created it)
90
- // and the statements run in migration order. Each records the rows it wrote in
91
- // the job's bridge record (`replayed[i].rows`), in the same transaction.
101
+ // inserted and before the triggers are recreated. Only absent tables are emptied
102
+ // (and their AUTOINCREMENT counters reset, as the live upgrade created them).
103
+ // Carried tables keep their archived rows. Statements run in migration order,
104
+ // recording rows in the job's bridge record (`replayed[i].rows`) in the same transaction.
92
105
  export function replayStatements(db,row) {
93
106
  const replays=row.metadata.replays??[];
94
- const reset=[...new Set(replays.map(r=>r.table))].flatMap(name=>{
107
+ const reset=[...new Set(replays.filter(r=>!r.carried).map(r=>r.table))].flatMap(name=>{
95
108
  const target=row.metadata.targets.find(t=>t.name===name);
96
109
  insist(target,'schema_mismatch:replay_table:'+name);
97
- const sequenced=tokens(target.sql).some(t=>!t.quoted && folded(t.text)==='autoincrement');
98
- return [statement(db,`DELETE FROM ${quote(name)}`),...(sequenced?[statement(db,'DELETE FROM sqlite_sequence WHERE name=?',name)]:[])];
110
+ return [statement(db,`DELETE FROM ${quote(name)}`),...(autoincrement(target.sql)?[statement(db,'DELETE FROM sqlite_sequence WHERE name=?',name)]:[])];
99
111
  });
100
112
  return [...reset,...replays.flatMap((r,i)=>[db.prepare(r.sql),
101
113
  statement(db,`UPDATE rer_portability_jobs SET metadata=json_set(metadata,'$.bridge.replayed[${i}].rows',changes()) WHERE id=?`,row.id)])];
102
114
  }
115
+ const autoincrement=sql=>tokens(sql).some(t=>!t.quoted && folded(t.text)==='autoincrement');
@@ -2,7 +2,7 @@ import { all, quote, insist } from './storage.mjs';
2
2
  import { canonicalJson } from './hash.mjs';
3
3
  import { tokens, identifier, folded } from './generated.mjs';
4
4
  import { laterData } from './backfills.mjs';
5
- import { destinationLedger } from './ledgers.mjs';
5
+ import { destinationLedger, matchMigrationLedger } from './ledgers.mjs';
6
6
  import { checkRebuild } from './rebuilds.mjs';
7
7
 
8
8
  // Applied product migrations in the ledger's own order (ledgers.mjs). A
@@ -22,9 +22,15 @@ export async function appliedMigrations(db) {
22
22
  // rewrites restorable data is refused, whether or not it changes the schema,
23
23
  // unless its statement is replayed at finalize (backfills.mjs).
24
24
  async function placement(db,manifest,cat,replay,ledger,history) {
25
- const applied=ledger.map(m=>m.name);
25
+ const files=replay.names.map(name=>({name,sha256:replay.sha256?.(name),aliases:replay.aliases?.(name)}));
26
+ // An aligned replayer keeps its existing contract: hashes are optional.
27
+ // Resolve destination aliases only when its ledger uses different names.
28
+ let applied=ledger.map(m=>m.name);
29
+ if(canonicalJson(replay.names)!==canonicalJson(applied))applied=matchMigrationLedger(ledger,files,replay);
26
30
  insist(canonicalJson(replay.names)===canonicalJson(applied) && await replay.schemaAfter(applied)===cat.identity,'schema_mismatch');
27
- const recorded=manifest.migrations ?? archivedApplied(history?.(),ledger,replay);
31
+ const archived=history?.();
32
+ const historyEntries=manifest.migrations?.map(name=>archived?.find(entry=>entry.name===name) ?? {name}) ?? archived;
33
+ const recorded=historyEntries && matchMigrationLedger(historyEntries,files,replay);
28
34
  let earlier,unapplied;
29
35
  if(recorded) {
30
36
  earlier=applied.filter(name=>recorded.includes(name));
@@ -48,26 +54,12 @@ async function placement(db,manifest,cat,replay,ledger,history) {
48
54
  // restorable. Trigger DDL itself changes no row, nor does an UPDATE or DELETE
49
55
  // of a table the archive holds no rows of (migration-statements.mjs). A data
50
56
  // statement that writes only tables outside the catalogue is admitted
51
- // whatever it reads; a backfill of a table the archive lacks is replayed.
52
- const catalogue=cat.tables.map(t=>t.name),archived=catalogue.filter(name=>manifest.tables.some(t=>t.name===name));
53
- const empty=archived.filter(name=>!manifest.tables.find(t=>t.name===name).count);
57
+ // whatever it reads; eligible catalogue backfills are replayed.
58
+ const catalogue=cat.tables.map(t=>t.name),archivedTables=catalogue.filter(name=>manifest.tables.some(t=>t.name===name));
59
+ const empty=archivedTables.filter(name=>!manifest.tables.find(t=>t.name===name).count);
54
60
  const tables=(await all(db,"SELECT name FROM sqlite_master WHERE type IN ('table','view')")).map(t=>t.name);
55
- const {replays,rebuilt}=laterData({applied,unapplied,replay,catalogue,archived,empty,tables,secrets:manifest.excludedColumns??{},manifest:manifest.tables});
56
- return {earlier,replays,rebuilt};
57
- }
58
- // An archive without a migration record that carries its source's migration
59
- // ledger (an old archive from before that ledger became bookkeeping) records
60
- // exactly which migrations its rows passed through. Each must be one the
61
- // destination applied, with the same file hash where the ledger records one:
62
- // the destination ledger's, else the replayed file's. The ledger itself is
63
- // never restored (bookkeeping.mjs). history: [{name,sha256?}] from
64
- // ledgers.mjs archivedHistory, undefined when the archive carries no ledger.
65
- function archivedApplied(history,ledger,replay) {
66
- for(const m of history??[]) {
67
- const applied=ledger.find(x=>x.name===m.name);
68
- insist(applied && (m.sha256===undefined || m.sha256===(applied.sha256??replay.sha256?.(m.name))),'schema_mismatch:migration_history:'+m.name);
69
- }
70
- return history?.map(m=>m.name);
61
+ const {replays,rebuilt}=laterData({applied,unapplied,replay,catalogue,archived:archivedTables,empty,tables,secrets:manifest.excludedColumns??{},manifest:manifest.tables});
62
+ return {applied,earlier,replays,rebuilt};
71
63
  }
72
64
  // An archive with the destination's schema identity can still predate later
73
65
  // migrations that changed only data (a purge or a backfill) or added an index.
@@ -79,10 +71,14 @@ function archivedApplied(history,ledger,replay) {
79
71
  export async function sameSchemaMigrations(db,manifest,cat,migrations,history) {
80
72
  const ledger=await destinationLedger(db),applied=ledger?.map(m=>m.name);
81
73
  if(!applied)return;
82
- insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
83
- if(!migrations){insist(!manifest.migrations || applied.every(name=>manifest.migrations.includes(name)),'schema_mismatch:migrations');return;}
84
- // Same schema: every catalogue table is archived, so nothing is replayed.
85
- await placement(db,manifest,cat,await migrations(),ledger,history);
74
+ if(!migrations){
75
+ if(manifest.migrations)matchMigrationLedger(manifest.migrations.map(name=>({name})),ledger);
76
+ insist(!manifest.migrations || applied.every(name=>manifest.migrations.includes(name)),'schema_mismatch:migrations');return;
77
+ }
78
+ // A data-only migration leaves the schema unchanged but still needs its
79
+ // carried-table replay plan retained for finalize.
80
+ const bridge=await additiveBridge(db,manifest,cat,migrations,history);
81
+ return bridge.replays?.length?bridge:undefined;
86
82
  }
87
83
  // An archive made before later migrations restores only when those migrations
88
84
  // are additive, or a proven row-preserving CHECK rebuild: new tables, new
@@ -90,9 +86,8 @@ export async function sameSchemaMigrations(db,manifest,cat,migrations,history) {
90
86
  // Anything else stays a schema_mismatch refusal.
91
87
  export async function additiveBridge(db,manifest,cat,migrations,history) {
92
88
  insist(migrations,'schema_mismatch');
93
- const replay=await migrations(),ledger=await destinationLedger(db)??[],applied=ledger.map(m=>m.name);
94
- insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
95
- const {earlier,replays,rebuilt}=await placement(db,manifest,cat,replay,ledger,history);
89
+ const replay=await migrations(),ledger=await destinationLedger(db)??[];
90
+ const {applied,earlier,replays,rebuilt}=await placement(db,manifest,cat,replay,ledger,history);
96
91
  const columns={};
97
92
  for(const table of manifest.tables) {
98
93
  const target=cat.tables.find(t=>t.name===table.name),label=':'+table.name;
@@ -28,8 +28,7 @@ export async function createImport(context,manifest,actor='direct',legacyRecover
28
28
  const skippedTables=original.tables.filter(table=>platformTable(table.name)).map(table=>table.name);
29
29
  const cat=await catalogue(db,config),upgrade=config.acceptedSchemas[original.schema];
30
30
  const same=manifest.schema===cat.identity,history=()=>archivedHistory(original,ledgers);
31
- const {replays,...bridge}=same || upgrade?{}:await additiveBridge(db,manifest,cat,context.migrations,history);
32
- if(same && !upgrade)await sameSchemaMigrations(db,manifest,cat,context.migrations,history);
31
+ const {replays,...bridge}=upgrade?{}:same?(await sameSchemaMigrations(db,manifest,cat,context.migrations,history)??{}):await additiveBridge(db,manifest,cat,context.migrations,history);
33
32
  if(!bridge.fromSchema)insist(canonicalJson(manifest.tables.map(x=>x.name))===canonicalJson(cat.tables.map(x=>x.name)),'table_inventory');
34
33
  if(!upgrade && !bridge.fromSchema)for(const table of manifest.tables) {
35
34
  const target=cat.tables.find(t=>t.name===table.name);