@runeditrun/rer 0.2.78 → 0.2.80

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 (116) hide show
  1. package/README.md +17 -3
  2. package/cli/README.md +25 -13
  3. package/cli/authoring.mjs +14 -0
  4. package/cli/contract.mjs +25 -5
  5. package/cli/demo-host.mjs +4 -2
  6. package/cli/dev-identity.mjs +2 -2
  7. package/cli/export.mjs +1 -1
  8. package/cli/inspect.mjs +38 -2
  9. package/cli/main.mjs +8 -2
  10. package/cli/managed-server.mjs +1 -1
  11. package/cli/parse.mjs +7 -6
  12. package/cli/portability-worker.mjs +2 -1
  13. package/cli/portability.mjs +6 -2
  14. package/cli/production-auth.mjs +2 -1
  15. package/cli/production-mail.mjs +36 -9
  16. package/cli/project.mjs +5 -14
  17. package/cli/release.mjs +2 -1
  18. package/cli/server-proxy.mjs +30 -8
  19. package/cli/site-preview.mjs +4 -1
  20. package/cli/site.mjs +3 -2
  21. package/cli/upstream-agent.mjs +14 -0
  22. package/edition-authoring/adopt.mjs +39 -0
  23. package/edition-authoring/base-acknowledgements.mjs +24 -0
  24. package/edition-authoring/base-copies.mjs +418 -0
  25. package/edition-authoring/cli.mjs +85 -0
  26. package/edition-authoring/copy-checkout.mjs +69 -0
  27. package/edition-authoring/files.mjs +15 -2
  28. package/edition-authoring/lock.mjs +48 -0
  29. package/edition-authoring/offline-git.mjs +28 -0
  30. package/edition-authoring/parent-comparison.mjs +77 -0
  31. package/edition-authoring/projection-lock.mjs +38 -0
  32. package/edition-authoring/release.mjs +48 -0
  33. package/edition-authoring/sources.mjs +114 -0
  34. package/edition-authoring/updates.mjs +105 -0
  35. package/global/CHANGELOG.md +17 -0
  36. package/global/README.md +17 -3
  37. package/global/cli/install.mjs +3 -1
  38. package/global/main.mjs +9 -12
  39. package/modules/container/build.mjs +1 -1
  40. package/modules/d1-transport/index.mjs +68 -21
  41. package/modules/demo/README.md +8 -2
  42. package/modules/demo/declaration.mjs +21 -0
  43. package/modules/demo/input-path.mjs +18 -0
  44. package/modules/demo/label.d.mts +1 -1
  45. package/modules/demo/label.mjs +10 -5
  46. package/modules/demo/navigation.mjs +32 -0
  47. package/modules/demo/run-url.mjs +9 -0
  48. package/modules/demo/vite.d.mts +7 -2
  49. package/modules/demo/vite.mjs +17 -2
  50. package/modules/demo-host/README.md +5 -3
  51. package/modules/demo-host/native/visitor.mjs +1 -1
  52. package/modules/demo-host/page-style.mjs +2 -2
  53. package/modules/demo-host/pages.mjs +3 -3
  54. package/modules/demo-host/plan.mjs +6 -4
  55. package/modules/demo-host/theme.mjs +5 -2
  56. package/modules/deploy-kit/README.md +19 -15
  57. package/modules/deploy-kit/owner-cli.mjs +1 -1
  58. package/modules/deploy-kit/readiness-window.mjs +3 -3
  59. package/modules/erasure/README.md +10 -5
  60. package/modules/import-kit/src/d1/README.md +15 -9
  61. package/modules/import-kit/src/d1/apply.ts +10 -6
  62. package/modules/import-kit/src/d1/atomic-prefix.ts +64 -0
  63. package/modules/import-kit/src/d1/budget.ts +48 -26
  64. package/modules/import-kit/src/d1/compressed-plan.ts +9 -2
  65. package/modules/import-kit/src/d1/database.ts +2 -1
  66. package/modules/import-kit/src/d1/jobs.ts +2 -1
  67. package/modules/import-kit/src/d1/plans.ts +4 -2
  68. package/modules/import-kit/src/d1/verified-plan.ts +63 -0
  69. package/modules/integrations/core/README.md +2 -2
  70. package/modules/mail/README.md +4 -3
  71. package/modules/mail/pnpm-lock.yaml +16 -0
  72. package/modules/operator-auth/accounts/native.mjs +3 -1
  73. package/modules/operator-auth/accounts/provider-control.mjs +7 -4
  74. package/modules/operator-auth/accounts/provider-setup.mjs +34 -0
  75. package/modules/operator-auth/accounts/settings-page.mjs +13 -4
  76. package/modules/operator-auth/conformance/README.md +1 -1
  77. package/modules/operator-auth/dev/index.mjs +22 -4
  78. package/modules/operator-auth/dev/refusal.mjs +19 -0
  79. package/modules/operator-auth/docs/README.md +6 -5
  80. package/modules/operator-auth/docs/tanstack-start.md +1 -1
  81. package/modules/operator-auth/docs/vite-spa.md +5 -6
  82. package/modules/operator-auth/index.d.mts +1 -1
  83. package/modules/operator-auth/page.mjs +14 -0
  84. package/modules/operator-auth/representation.mjs +83 -0
  85. package/modules/operator-auth/routes.mjs +25 -2
  86. package/modules/portability/README.md +91 -14
  87. package/modules/portability/bookkeeping.mjs +3 -3
  88. package/modules/portability/bridge.mjs +5 -4
  89. package/modules/portability/catalogue.mjs +12 -8
  90. package/modules/portability/cli.mjs +5 -1
  91. package/modules/portability/export.mjs +5 -11
  92. package/modules/portability/format.mjs +7 -1
  93. package/modules/portability/http-errors.mjs +51 -0
  94. package/modules/portability/http.mjs +4 -1
  95. package/modules/portability/import-lease.mjs +13 -2
  96. package/modules/portability/import.mjs +10 -0
  97. package/modules/portability/index.d.mts +3 -0
  98. package/modules/portability/references.mjs +20 -0
  99. package/modules/portability/schema-identity.mjs +13 -0
  100. package/modules/portability/schema-legacy.mjs +21 -0
  101. package/modules/portability/sqlite-tokens.mjs +121 -0
  102. package/modules/release/README.md +20 -9
  103. package/modules/release/github-verify.mjs +30 -9
  104. package/modules/site/docs-search/search.css +1 -1
  105. package/onboarding/README.md +16 -4
  106. package/onboarding/adapter.mjs +5 -1
  107. package/onboarding/schema/SETUP.schema.json +26 -1
  108. package/onboarding/schema/business-field-inputs.mjs +37 -0
  109. package/onboarding/schema/json-schema.mjs +6 -2
  110. package/onboarding/schema/setup-validation.mjs +20 -3
  111. package/onboarding/server.mjs +1 -1
  112. package/onboarding/setup-manifest.mjs +2 -0
  113. package/onboarding/start-origin-diagnostic.mjs +24 -0
  114. package/onboarding/ui/style.css +9 -9
  115. package/package.json +1 -1
  116. package/scripts/publish-check/README.md +1 -1
@@ -40,7 +40,7 @@ The maintained default-seed `/health` returns the existing `status: 'ok'` plus t
40
40
  } }
41
41
  ```
42
42
 
43
- Assets upload with `run_worker_first: true` (`RUN_WORKER_FIRST` in `render.mjs`). Every request reaches the Worker first, so the seed's routing, origin admission, `no-store` document policy and private-path refusals decide what is served, and the Base delivery producer accepts no other setting. A built file therefore reaches the browser only through the Worker: seed entries call `staticAssetResponse(request, env)` from `foundation/runtime/static-assets.ts` first. After the deploy identity passes, health requests one built file (the first artifact asset that is neither an HTML document nor under a dot path) without credentials, retries a 5xx or transport failure once within the existing readiness deadline, and fails with `asset-http-failed` unless it answers 200; the result is recorded as `health.asset`. `rer verify` serves the local build with the same setting (`cli/hosted-output.mjs`).
43
+ Assets upload with `run_worker_first: true` (`RUN_WORKER_FIRST` in `render.mjs`). Every request reaches the Worker first, so the seed's routing, origin admission, `no-store` document policy and private-path refusals decide what is served, and the Base delivery producer accepts no other setting. A built file therefore reaches the browser only through the Worker: seed entries call `staticAssetResponse(request, env)` from `foundation/runtime/static-assets.ts` first. After the deploy identity passes, health requests one built file (the first artifact asset that is neither an HTML document nor under a dot path) without credentials, polls a missing asset after upload within the smaller of 60 seconds and the remaining readiness window, replays 5xx and transport failures at most once, and fails with `asset-http-failed` unless it answers 200; the result is recorded as `health.asset`. `rer verify` serves the local build with the same setting (`cli/hosted-output.mjs`).
44
44
 
45
45
  The controller supplies the two public variables and Cloudflare version-metadata binding. The parent owns default-seed source/types, package scripts, shared manifests and workflows. Keep the staged-deploy descriptor blocked until the three-stage isolation and actual build/health integration pass; the original deploy-then-health executor cannot safely run installation/build with its credentials.
46
46
 
@@ -48,7 +48,7 @@ The controller supplies the two public variables and Cloudflare version-metadata
48
48
 
49
49
  `deploy({api,plan,artifact,artifactSha256,previousReceipt,env,fetch})` is the shared implementation for deploy and stage. Plans are controller-owned and validated; do not derive a secret-bearing plan from executable project configuration. `defaultStagePlan` demonstrates the full schema. Optional R2 `{binding,name}`, Queue `{binding,name,consumer?}`, DO `{binding,className,storage:'sqlite'|'classic'}`, and secret `{binding,environment}` declarations are supported. New DOs must use SQLite; existing classic namespaces are reusable. Namespace creation occurs through Worker upload migrations, with old/new migration tags, then authenticated namespace readback. No namespace create endpoint is invented. Cron and queue consumers reconcile after Worker creation. Conflicting consumers and resource identities fail; resources/data are never deleted.
50
50
 
51
- Cron reads require the documented `result: {schedules: [...]}` envelope, including valid unique Cron strings; bare arrays and partial/malformed results fail. Before changing an existing Worker, deployment reads its schedules and rejects any Cron outside the requested plan with `cron-drift`, before resource writes, SQL migrations or Worker upload. Trigger reconciliation repeats that read before any trigger write. This applies to ordinary changed-source upgrades with or without a prior receipt. The kit may add requested schedules; it never intentionally removes existing schedules. Removing a Cron from the plan while it still exists remotely requires a separate reviewed removal first. Every changed upload reapplies its nonempty declared Cron set after workers.dev activation, even when the Worker-scoped list already matches. The kit reads the exact schedule set back after the write and checks that the active deployment identity remained stable. A declared Cron also requires the authenticated active version to report a `scheduled` handler; absent detection fails with `missing-scheduled-handler`. These checks establish provider configuration, not proof that a scheduled invocation has executed. Dispatch is the provider's: on the RunEditRun account (11 October 2026) a correctly registered Cron started only at the account scheduler's next refresh, observed 60 to 106 minutes apart, so a first tick can arrive well after Cloudflare's documented 15 minutes; a cleared schedule keeps running, and a deleted Worker keeps being invoked (as `internalError`), until a later refresh. A redeploy keeps the existing dispatch: the first run after it executes the new version. Judge first-deploy Cron by the seed's own scheduled-run record, not by a short watch. Unchanged deployment and health require the exact desired schedule set and perform no repairs. Callers must serialize deployments and out-of-band schedule edits for a Worker: the provider schedule replacement API offers no conditional update in this interface.
51
+ Cron reads require the documented `result: {schedules: [...]}` envelope, including valid unique Cron strings; bare arrays and partial/malformed results fail. Before changing an existing Worker, deployment reads its schedules and rejects any Cron outside the requested plan with `cron-drift`, before resource writes, SQL migrations or Worker upload. Trigger reconciliation repeats that read before any trigger write. This applies to ordinary changed-source upgrades with or without a prior receipt. The kit may add requested schedules; it never intentionally removes existing schedules. Removing a Cron from the plan while it still exists remotely requires a separate reviewed removal first. Every changed upload reapplies its nonempty declared Cron set after workers.dev activation, even when the Worker-scoped list already matches. The kit reads the exact schedule set back after the write and checks that the active deployment identity remained stable. A declared Cron also requires the authenticated active version to report a `scheduled` handler; absent detection fails with `missing-scheduled-handler`. These checks establish provider configuration, not proof that a scheduled invocation has executed. Dispatch is the provider's. The following account-specific observation is historical and unverified here; no independent receipt is retained in this repository. On the RunEditRun account (11 October 2026) a correctly registered Cron started only at the account scheduler's next refresh, observed 60 to 106 minutes apart, so a first tick can arrive well after Cloudflare's documented 15 minutes; a cleared schedule keeps running, and a deleted Worker keeps being invoked (as `internalError`), until a later refresh. A redeploy keeps the existing dispatch: the first run after it executes the new version. Judge first-deploy Cron by the seed's own scheduled-run record, not by a short watch. Unchanged deployment and health require the exact desired schedule set and perform no repairs. Callers must serialize deployments and out-of-band schedule edits for a Worker: the provider schedule replacement API offers no conditional update in this interface.
52
52
 
53
53
  `workerConsumerScript(consumer)` (also exported from `index.mjs`) decodes Worker consumer identities from either documented `script_name` or the observed GET response string `script`. It returns the exact name using the plan's resource-name grammar, or `null` for missing, malformed, conflicting, or non-Worker identities. If both fields are present, each must be valid and both must agree; a malformed alias is never ignored. Reconciliation checks every consumer on queues with a requested consumer before any trigger write and rejects foreign or duplicate consumers. Writes continue to use the documented `script_name` request field. Read-only health and retained-receipt repeats accept either response variant without repairs.
54
54
 
@@ -94,7 +94,7 @@ A declaration may also bind another owner-deployed Worker and may keep its own w
94
94
  "workersDev": false }
95
95
  ```
96
96
 
97
- A service's Worker name is derived from its `project` and this deploy's environment, so the example above binds `egress-service-production`. `allowedHosts` becomes the binding's `props` (`{allowedHosts}`), which the bound Worker reads as `ctx.props`. The binding is uploaded and read back like every other binding, so a changed `props` is reported as `binding-readback-mismatch`. Before any write, deploy reads each bound Worker. If it is absent, deploy refuses with `service-worker-missing` and resource `service:<worker>`; deploy that Worker first. `workersDev: false` keeps the route off: an enabled route is switched off on upload, the receipt's `origin` is `null`, and health uses provider readback only, because there is no public `/health` to request. [modules/egress-service](../egress-service/README.md) is the Worker this exists for.
97
+ A service's Worker name is derived from its `project` and this deploy's environment, so the example above binds `egress-service-production`. `allowedHosts` becomes the binding's `props` (`{allowedHosts}`), which the bound Worker reads as `ctx.props`. The binding is uploaded and read back like every other binding, so a changed `props` is reported as `binding-readback-mismatch`. Before any write, deploy reads each bound Worker. If it is absent, deploy refuses with `service-worker-missing` and resource `service:<worker>`; deploy that Worker first. `workersDev: false` keeps the route off: an enabled route is switched off on upload, the receipt's `origin` is `null`, and health uses provider readback only, because there is no public `/health` to request. [modules/egress-service](https://github.com/runeditrun/base/blob/main/modules/egress-service/README.md) is the Worker this exists for.
98
98
 
99
99
  `ownerTarget`, `ownerPlan`, `ownerDeploy` and `ownerHealth` (exported from `index.mjs`) are the same steps for a seed's own script. A seed script can compute vars before calling `ownerPlan`.
100
100
 
@@ -104,8 +104,8 @@ Ownership lives in a state file, by default `.rer/deploy/<environment>.json` und
104
104
  - **Re-run.** The same build writes nothing: no create, upload, migration, trigger or secret write, and the state file stays byte-identical. A later release that declares a new resource creates only that resource.
105
105
  - **Partial failure and resume.** The kit records each create as `pending` before the request and `created` with the provider id after it. A failed create leaves `pending`. On the next run an absent pending name is created again, and a present one is refused as `uncertain-resource-create`, because a lost response cannot prove who created it. Inspect it: if it is the empty resource from the interrupted run, delete it and re-run. A Worker upload is recorded as created when the upload succeeds, so a failure after it resumes with one fresh upload. If the response to the very first upload is lost, the Worker exists but is still `pending`. The kit records the upload's fingerprint before sending it. When an upload's response or its readback is lost (a first upload or a redeploy), the next run accepts the live version as this deploy's own only when it carries that fingerprint both as its `RER_DEPLOY_FINGERPRINT` binding and as its `rer-deploy:<fingerprint>` annotation: it records the live version id, deployment id and etag, then resumes with one fresh upload. Any other live version is refused, as `uncertain-resource-create` with no prior receipt and as `owned-resource-replaced` with one. If the uncertain Worker cannot be authenticated, inspect it and delete it only after confirming that it is the kit-created Worker and that its Durable Object data is disposable. Back up any needed data before deleting it. Migration claims keep the rules under Migration safety.
106
106
  - **Drift.** A created resource that has gone is refused as `owned-resource-missing`; one replaced under the same name is refused as `owned-resource-replaced`. The kit never deletes or recreates them. This covers D1, KV, Queues and R2 (a bucket is identified by its creation date; a state file written before that was recorded keeps working and is not checked for that bucket). For the Worker, the live deployment (version id, deployment id and etag) must be the last receipt's before any write; someone else's upload under the same name is refused as `owned-resource-replaced` and never overwritten. Immediately before the Worker PUT, the kit rechecks every saved resource identity, including resources omitted from the current plan, then the Worker identity. A replacement during provisioning or asset upload is refused before the upload intent or receipt is saved. An interrupted successful upload may resume only when the live version id, deployment id and etag match the identity saved immediately after that upload, or, when its readback was lost before that identity was saved, when the live version carries the fingerprint saved immediately before that upload (binding and annotation). A receipt's historical fingerprint never overrides a receipt mismatch.
107
- - **Lost upgrade response.** If an upgrade response is lost and the live identity no longer matches the receipt, the kit refuses rather than guessing ownership. A [Cloudflare rollback](https://developers.cloudflare.com/workers/versions-and-deployments/rollbacks/) creates a new deployment, so it does not restore the recorded identity. Recover with a new environment and a fresh state path, or back up the state and any needed data, confirm ownership, and follow the cleanup under "If the state file is lost" with a fresh `--state` path.
108
- - **Settling listings.** Right after a create or delete, an account listing can briefly disagree with its own page counts (`provider-partial-list`: on 13 October 2026 D1's `total_count` lagged its rows by about 7 seconds after a create). Owner-mode deploy, health and removal discard such a snapshot and list again from the start for up to 30 seconds with a short backoff; a partial listing is never accepted.
107
+ - **Lost upgrade response.** An interrupted upload can resume when its saved upload intent authenticates the live version by the pending fingerprint binding and annotation, or by its saved provider identity. Otherwise deployment refuses the mismatch. A [Cloudflare rollback](https://developers.cloudflare.com/workers/versions-and-deployments/rollbacks/) creates a new deployment, so it does not restore the recorded identity.
108
+ - **Settling listings.** Right after a create or delete, an account listing can briefly disagree with its own page counts (`provider-partial-list`: on 13 October 2026 D1's `total_count` lagged its rows by about 7 seconds after a create). Owner-mode deploy, health and removal discard such a snapshot and list again from the start for up to 120 seconds with a short backoff; a partial listing is never accepted.
109
109
  - **One run at a time.** A deploy takes an exclusive lock file, `<state file>.lock`, for the whole run and removes it at the end, even after a failure. A second run on the same state file is refused as `owner-state-locked` before any provider request. If a run was killed (power loss, `kill -9`) and no deploy is running, delete the lock file by hand.
110
110
 
111
111
  The CLI prints JSON: refusal codes and resource names only, never inputs, tokens or provider bodies.
@@ -348,10 +348,12 @@ the standalone window: 2, 2, 4, 8, 10, 10 ... seconds. Only transport failures,
348
348
  names another Worker version (the edge still serving the previous version just
349
349
  after the upload). A redirect, any other status, a malformed or unhealthy body
350
350
  and a wrong identity from the uploaded version itself fail at once. The one
351
- built file then requested must answer 200; after an upload its 404, 5xx and
352
- transport failures are retried for up to 60 seconds, since an edge still serving
353
- the previous version does not have the new build's hashed files (seen live on
354
- 13 October 2026).
351
+ built file then requested must answer 200. After an upload, only asset 404
352
+ propagation receives repeated polling within the smaller of 60 seconds and the
353
+ remaining readiness window; asset 5xx and transport failures receive at most one
354
+ replay. A historical, unverified 13 October 2026 observation attributed missing
355
+ hashed files to an edge still serving the previous build; no independent receipt
356
+ is retained here.
355
357
  Each request is capped by both the 30-second request limit and the remaining
356
358
  budget, and no request starts at the deadline. An unchanged repeat uses the
357
359
  existing library window and does not record first-upload readiness attempts.
@@ -370,9 +372,10 @@ retains the existing window, retrying only 404, 503 and network failures for
370
372
  Custom-hook `rer deploy` finalization gets a separate 90-second `/health` wait
371
373
  after the hook returns, using the current recorded Worker version when available.
372
374
  The hook can already have claimed the owner and retired its token, which creates
373
- another version and can exhaust the original upload window. First hosted Slate
374
- and Pencil deploys on 15 October 2026 failed this final gate even though health
375
- named the right commit seconds later. Reusing the existing 90-second propagation
375
+ another version and can exhaust the original upload window. A historical,
376
+ unverified report says first hosted Slate and Pencil deploys on 15 October 2026
377
+ failed this final gate even though health named the right commit seconds later;
378
+ no independent receipt is retained here. Reusing the existing 90-second propagation
376
379
  budget gives this final observation the same bounded allowance, without spending
377
380
  it on the hook's earlier work. Transport failures and healthy identity mismatches wait;
378
381
  permanent HTTP or malformed responses still fail. The deadline includes request
@@ -415,8 +418,9 @@ untrusted response prefixes are never written into that record.
415
418
 
416
419
  ## Read-only teardown verification
417
420
 
418
- The kit does not perform deletes. After a seed has validated its receipt and
419
- removed only its authenticated disposable resources, call
421
+ `verifyTeardown` performs no deletes. Explicit owner-account removal deletes
422
+ authenticated owned resources and purges owned R2 objects. After a seed has
423
+ validated its receipt and removed only its authenticated disposable resources, call
420
424
  `verifyTeardown({api, plan, receipt})`. The default CLI exposes
421
425
  `node modules/deploy-kit/cli.mjs verify-teardown` with the same controller inputs.
422
426
  The target must be the exact receipt Worker with a `rer-seedproof-` prefix or
@@ -479,7 +483,7 @@ Only the ordinary named service binding is uploaded. This supports service-owned
479
483
  narrowing where Cloudflare omits service-binding `props` from settings/version
480
484
  readback. Ordinary `allowedHosts` still maps to `props` and retains strict
481
485
  readback; missing props are never silently accepted. See the
482
- [egress service guide](../egress-service/README.md).
486
+ [egress service guide](https://github.com/runeditrun/base/blob/main/modules/egress-service/README.md).
483
487
 
484
488
  ### List completion and deployment failures
485
489
 
@@ -68,7 +68,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
68
68
  if (!requestWindow?.active) return undefined;
69
69
  const remaining = requestWindow.remaining();
70
70
  assert(remaining > 0, 'deployment-readiness-timeout');
71
- return AbortSignal.timeout(Math.max(1, Math.ceil(remaining)));
71
+ return requestWindow.timeout(Math.max(1, Math.ceil(remaining)));
72
72
  };
73
73
  fetch = (input, init = {}) => {
74
74
  const deadline = requestSignal();
@@ -3,11 +3,11 @@ import { assert } from './validation.mjs';
3
3
 
4
4
  // A single, in-memory deadline travels with this upload. It is deliberately not
5
5
  // persisted: an old receipt is not evidence of a fresh propagation window.
6
- export function readinessWindow({ totalMs = 90000, clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) } = {}) {
6
+ export function readinessWindow({ totalMs = 90000, clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve, ms)), timeout = ms => AbortSignal.timeout(ms) } = {}) {
7
7
  assert(Number.isFinite(totalMs) && totalMs > 0 && totalMs <= 90000, 'invalid-health-bound');
8
8
  let start;
9
9
  return {
10
- clock, sleep,
10
+ clock, sleep, timeout,
11
11
  get active() { return start !== undefined; },
12
12
  start() { start ??= clock(); },
13
13
  remaining() { return start === undefined ? totalMs : Math.max(0, totalMs - (clock() - start)); },
@@ -26,7 +26,7 @@ export function readinessAPI(api, window, failure = 'deployment-readiness-timeou
26
26
  return {...api, request(path, options = {}) {
27
27
  const remaining = window.remaining();
28
28
  assert(remaining > 0, failure);
29
- const deadline = AbortSignal.timeout(Math.max(1, Math.ceil(remaining)));
29
+ const deadline = window.timeout(Math.max(1, Math.ceil(remaining)));
30
30
  return api.request(path, {...options, signal:options.signal ? AbortSignal.any([options.signal,deadline]) : deadline});
31
31
  }};
32
32
  }
@@ -188,6 +188,9 @@ the seed retires them in its own migration.
188
188
  retires imports whose headers hold the person's exact identifiers and writes
189
189
  the import erasure journal (import-kit `src/d1/README.md`).
190
190
 
191
+ If a required retained row disappears, erasure refuses with `retained_row_missing`
192
+ and leaves the work pending.
193
+
191
194
  ## Adopting
192
195
 
193
196
  Copy these files byte for byte to a folder the Worker build serves, such as
@@ -207,6 +210,7 @@ modules/erasure/scope.mjs
207
210
  modules/erasure/completion.mjs
208
211
  modules/erasure/verification.mjs
209
212
  modules/erasure/cascade.mjs
213
+ modules/erasure/queue.mjs
210
214
  modules/erasure/blobs.mjs
211
215
  modules/erasure/audit.mjs
212
216
  modules/erasure/receipt.mjs
@@ -229,11 +233,12 @@ archives; the blob work list does not.
229
233
 
230
234
  ## Historical measurements before the review fixes
231
235
 
232
- Through the default seed's route in a local `rer dev` Worker, a subject with
233
- 10,000 dependent rows (9,500 notes and 500 file rows) and 500 R2 blobs was
234
- erased in 10.7 s in one run and 15.0 s in another. That is one request: 20
235
- bounded D1 steps, then the blob phase, with no queue continuation needed. The
236
- full proof table is kept with the consolidation evidence for this change.
236
+ A historical, unverified report through the default seed's route in a local
237
+ `rer dev` Worker recorded erasing a subject with 10,000 dependent rows (9,500
238
+ notes and 500 file rows) and 500 R2 blobs in 10.7 s in one run and 15.0 s in
239
+ another, in one request: 20 bounded D1 steps, then the blob phase, with no queue continuation needed. The cited
240
+ consolidation proof table is outside this repository; no corresponding benchmark
241
+ receipt is retained here.
237
242
  These pre-fix measurements do not establish the performance of the corrected
238
243
  helper. They are local Miniflare measurements, not production capacity claims. Real D1
239
244
  statement, CPU and request limits decide `rows` per step and the request budget.
@@ -13,7 +13,7 @@ use Undo from the same history when needed. Base's reference accepts people JSON
13
13
  here, not installation `.rer` archives. Editions adopt this same door
14
14
  with their own supported readers and native adapters.
15
15
 
16
- For seed integration, follow [the door adoption guide](../door/ADOPTING.md)
16
+ For seed integration, follow [the door adoption guide](https://github.com/runeditrun/base/blob/main/modules/import-kit/src/door/ADOPTING.md)
17
17
  first. The D1 primitives below back that shared pipeline; do not mount separate
18
18
  job creation/apply routes beside it. Keep native readers and target adapters.
19
19
 
@@ -29,6 +29,10 @@ provenance and inert-effect policy through an asynchronous native SQL port.
29
29
  When copying Base source directly, copy the shared [D1 transport](../../../d1-transport/index.mjs) and its
30
30
  [types](../../../d1-transport/index.d.mts), preserving their sibling location
31
31
  beside the import-kit directory.
32
+ Reading codec-dependent stored plans without the matching codec ID refuses with
33
+ `IMPORT_PLAN_CODEC_REQUIRED`; encoding a codec whose round trip changes the plan
34
+ fingerprint refuses with `IMPORT_PLAN_CODEC_MISMATCH`.
35
+
32
36
  The D1 entry uses this shared transport automatically. It accounts for actual
33
37
  UTF-8 bytes and workerd's whole-call two-byte strings, escapes sparse wide text
34
38
  for SQLite to decode, and keeps each mutation in its original transaction.
@@ -44,7 +48,9 @@ and D1 engine for the same jobs: each engine owns its checkpoint protocol.
44
48
  For reviewed reversal-policy conversion, also apply the additive
45
49
  `d1ReversalPolicyConversionSchema` after these existing schemas in a new
46
50
  product-owned numbered migration. `initializeD1ImportStorage` installs it
47
- automatically. Do not rewrite an applied migration.
51
+ automatically. Do not rewrite an applied migration. Partial preserved-undo schema
52
+ evolution refuses with `IMPORT_SCHEMA_EVOLUTION_INCOMPLETE`; complete the additive
53
+ evolution before continuing.
48
54
 
49
55
  Apply stream-origin backfill and legacy receipt conversion in one transaction.
50
56
  Origins record actual page, parent and job bindings and remain immutable until
@@ -64,10 +70,10 @@ evaluates the expression at both ends of every atomic mutation batch. A false
64
70
  predicate raises `IMPORT_ATOMIC_GUARD` and rolls back every native and kit write.
65
71
  Read operations also require current authority. Jobs remain bound to their actor.
66
72
  The plan and its fingerprint carry that `principalId`, so a job can never be rebound
67
- to another one, and an archive carries the jobs but not the operators who created
68
- them: after an export, reset, import and setup the owner is a new operator row. A
69
- product that wants restored jobs to stay usable passes a principal that survives a
70
- restore, such as the operator's role (`'role:' + role`), and lets `predicate` check
73
+ to another one. Identity retention depends on the seed's archive policy: a
74
+ restored job remains bound to its recorded principal, so verify whether that
75
+ principal survives the selected policy. A product that wants restored jobs to
76
+ stay usable passes a principal that survives a restore, such as the operator's role (`'role:' + role`), and lets `predicate` check
71
77
  that the acting operator is still active in that role. A product that passes the
72
78
  operator's row id keeps its jobs bound to that operator for good.
73
79
 
@@ -932,9 +938,9 @@ the new conversion and preview operations refuse with the `schema` reason until
932
938
  their additive migration is installed. Updating kit source does not silently
933
939
  adopt conversion or require an edit to an existing seed migration.
934
940
 
935
- Current rer 0.2.77 lacks this API. A seed adopts a reviewed vendored Base revision
936
- containing the capability, or a future rer release that supplies it, and proves
937
- its migration, current authority, conversion and later reversal through its own
941
+ Historically, rer 0.2.77 lacked this API. A seed adopts a reviewed vendored Base
942
+ revision containing the capability, or a pinned rer release that supplies it,
943
+ and proves its migration, current authority, conversion and later reversal through its own
938
944
  public door. Base's tests do not prove that adoption. Until then, existing strict
939
945
  jobs retain their policy and the seed's compensation-unavailable refusal remains
940
946
  in force. Adoption, seed release and deployment are separate from this Base
@@ -7,7 +7,7 @@ import { canonicalJson, sha256, attachmentHash } from '../provenance/identity.ts
7
7
  import { validateEffect } from '../plan/index.ts';
8
8
  import { effectIdentity } from '../storage/provenance.ts';
9
9
  import type { D1ImportContext, D1Sql } from './types.ts';
10
- import { atomic, atomicCount, authorize, guard, limits, sql, fitsBatch } from './database.ts';
10
+ import { atomic, atomicCount, atomicPrefix, authorize, guard, limits, sql, fitsBatch } from './database.ts';
11
11
  import { stored, boundary, storedReads } from './jobs.ts';
12
12
  import { custodyGuards, validatedProvenance, inventoryGuards, reusedSourceGuards } from './integrity.ts';
13
13
  import { supersedeTarget } from './history.ts';
@@ -27,7 +27,9 @@ export async function applyD1ImportBatches(jobOrId: ImportJob | string, context:
27
27
  if (job.state === 'completed') { await atomic(context, 'apply', [...boundary(job, plan, 'apply'), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], 0, nativeStagingScope(job, plan)); return job; }
28
28
  if (!['queued', 'applying', 'failed'].includes(job.state)) throw new Error('IMPORT_ALREADY_REVERSING');
29
29
  const statements: D1Sql[] = [...boundary(job, plan, 'apply'), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], entries: PlanEntry[] = [], returnReads = storedReads(job);
30
- let effects = 0, scope = nativeStagingScope(job, plan), planned = cost(context, publication(job, 0, statements, scope), returnReads);
30
+ let effects = 0, scope = nativeStagingScope(job, plan);
31
+ let prefix = atomicPrefix(context, 'apply', statements, scope);
32
+ let planned = prefix ? prefix.count(progress(job, 0), returnReads) : cost(context, publication(job, 0, statements, scope), returnReads);
31
33
  for (const entry of plan.entries.slice(job.checkpoint, job.checkpoint + batchSize)) {
32
34
  if (entries.length && effects + entry.effects.length > 1000) break;
33
35
  if (entry.effects.length > 1000) throw new Error('SOURCE_EFFECT_LIMIT');
@@ -35,13 +37,15 @@ export async function applyD1ImportBatches(jobOrId: ImportJob | string, context:
35
37
  // request, as atomic will send it, is counted again once the entry is built.
36
38
  const reserve = 40 + entry.effects.length * 16;
37
39
  if (remaining(context) - planned < reserve) { if (!entries.length) { if (count > 0) return job; throw new D1RequestLimitError(); } break; }
38
- const candidate = [...statements], nextScope = { ...scope, nativeGuards:[...scope.nativeGuards], postGuards:[...scope.postGuards], targets:[...scope.targets] };
40
+ const candidate: D1Sql[] = [], nextScope = { ...scope, nativeGuards:[...scope.nativeGuards], postGuards:[...scope.postGuards], targets:[...scope.targets] };
39
41
  await publishEntry(id, entry, job.checkpoint + entries.length, context, candidate, nextScope);
40
- const next = publication(job, entries.length + 1, candidate, nextScope), spent = cost(context, next, returnReads);
42
+ const nextPrefix = prefix?.extend(candidate);
43
+ const next = nextPrefix ? undefined : publication(job, entries.length + 1, [...statements, ...candidate], nextScope);
44
+ const spent = nextPrefix ? nextPrefix.count(progress(job, entries.length + 1), returnReads) : cost(context, next!, returnReads);
41
45
  if (spent > remaining(context)) { if (entries.length) break; if (count > 0) return job; throw new D1RequestLimitError(); }
42
- const fits = fitsBatch(context, 'apply', next.statements, next.scope);
46
+ const fits = nextPrefix ? true : fitsBatch(context, 'apply', next!.statements, next!.scope);
43
47
  if (!fits && entries.length) break;
44
- statements.splice(0, statements.length, ...candidate);
48
+ statements.push(...candidate); prefix = nextPrefix;
45
49
  scope = nextScope; entries.push(entry); effects += entry.effects.length; planned = spent;
46
50
  if (!fits) break;
47
51
  }
@@ -0,0 +1,64 @@
1
+ import type { ImportAction } from '../types.ts';
2
+ import type { D1ImportContext, D1Sql } from './types.ts';
3
+ import type { NativeStagingScope } from './native-staging-context.ts';
4
+ import { GuardPacking, valueBytes } from './budget.ts';
5
+ import { authority, finalAuthority, postConditions, sql } from './database.ts';
6
+
7
+ /** Unbounded publication has append-only SQL and final checks. Bounded native
8
+ * staging derives an ExpectedState from the complete transaction instead. */
9
+ export function atomicPrefix(context: D1ImportContext, action: ImportAction, statements: D1Sql[], scope: NativeStagingScope) {
10
+ if (context.maxBatchBytes !== undefined) return undefined;
11
+ type Part = { statements: D1Sql[]; unchanged: () => boolean; prior?: Part };
12
+ const observe = (statement: D1Sql): (() => boolean) => {
13
+ const text = statement.sql, params = [...statement.params ?? []], sizes = params.map(p => p instanceof ArrayBuffer || ArrayBuffer.isView(p) ? valueBytes(p) : undefined);
14
+ const checks = [...statement.finalChecks ?? []], unchanged = checks.map(observe);
15
+ return () => statement.sql === text && (statement.params?.length ?? 0) === params.length
16
+ && params.every((p, i) => p === statement.params![i] && (sizes[i] === undefined || sizes[i] === valueBytes(p)))
17
+ && (statement.finalChecks?.length ?? 0) === checks.length && checks.every((check, i) => check === statement.finalChecks![i] && unchanged[i]());
18
+ };
19
+ const part = (statements: D1Sql[], prior?: Part): Part => {
20
+ const checks = statements.map(observe);
21
+ return { statements: [...statements], prior, unchanged: () => checks.every(check => check()) };
22
+ };
23
+ const same = (a: D1Sql, b: D1Sql) => a.sql === b.sql && (a.params?.length ?? 0) === (b.params?.length ?? 0) && (a.params ?? []).every((p, i) => p === b.params![i]);
24
+ const collect = (part: Part): D1Sql[] => {
25
+ const parts: D1Sql[][] = [];
26
+ for (let current: Part | undefined = part; current; current = current.prior) parts.push(current.statements);
27
+ return parts.reverse().flat();
28
+ };
29
+ class Prefix {
30
+ private writes?: GuardPacking;
31
+ private first?: D1Sql;
32
+ private firstUnchanged?: () => boolean;
33
+ private part: Part;
34
+ private post: GuardPacking;
35
+ private postLength: number;
36
+ constructor(part: Part, post: GuardPacking, postLength: number, prior?: Prefix) {
37
+ this.part = part; this.post = post; this.postLength = postLength;
38
+ if (prior) { this.writes = prior.writes?.fork().append(part.statements); this.first = prior.first; this.firstUnchanged = prior.firstUnchanged; }
39
+ }
40
+ extend(added: D1Sql[]): Prefix {
41
+ const checks = added.flatMap(s => s.finalChecks ?? []);
42
+ return new Prefix(part(added, this.part), this.post.fork().append(checks), this.postLength + checks.length, this);
43
+ }
44
+ count(tail: D1Sql[], returnReads: number): number {
45
+ // Authority is evaluated afresh even when its SQL remains equivalent.
46
+ const first = authority(context, action);
47
+ let changed = false;
48
+ for (let current: Part | undefined = this.part; current; current = current.prior) changed ||= !current.unchanged();
49
+ if (changed) {
50
+ const post = postConditions(context, collect(this.part), scope, action);
51
+ this.post = new GuardPacking().append(post); this.postLength = post.length;
52
+ }
53
+ if (changed || !this.writes || !this.first || !same(first, this.first) || !this.firstUnchanged?.()) {
54
+ this.writes = new GuardPacking().append([first, ...collect(this.part)]); this.first = first; this.firstUnchanged = observe(first);
55
+ }
56
+ const writes = this.writes.count([...tail, authority(context, action), sql('DELETE FROM ik_d1_assertions')]);
57
+ const checks = tail.flatMap(s => s.finalChecks ?? []), post = this.post.count(checks);
58
+ if (this.postLength + checks.length) finalAuthority(context, action);
59
+ return writes + post + Number(this.postLength + checks.length > 0) + returnReads;
60
+ }
61
+ }
62
+ const post = postConditions(context, statements, scope, action);
63
+ return new Prefix(part(statements), new GuardPacking().append(post), post.length);
64
+ }
@@ -57,33 +57,55 @@ function conjunction(predicates: string[], start = 0, end = predicates.length):
57
57
  }
58
58
  /** Adjacent assertions can share one CHECK without changing transactional order.
59
59
  * Native statements remain between their original pre/postconditions. */
60
- export function packGuards(statements: D1Sql[], readOnly = false): D1Sql[] {
61
- const prefix = readOnly ? 'SELECT CASE WHEN (' : 'INSERT INTO ik_d1_assertions(ok) SELECT CASE WHEN (';
62
- const suffix = readOnly ? ') THEN 1 ELSE 0 END AS ok' : ') THEN 1 ELSE 0 END';
63
- const result: D1Sql[] = []; const seen = new Map<string, D1Sql[]>(); let predicates: string[] = [], params: any[] = [], bytes = 0;
64
- const flush = () => {
65
- if (predicates.length) {
66
- // A flat AND list becomes a left-deep SQLite expression tree. Keep the
67
- // added depth logarithmic without changing predicates or binding order.
68
- const text=conjunction(predicates),packed = readOnly ? { sql: `${prefix}${text}${suffix}`, params } : guard({sql:text,params});
69
- checkSql(packed); result.push(packed);
60
+ /** Append-only state for the exact same packing algorithm. Count-only forks
61
+ * retain the open guard run, never copy or rebuild the already packed prefix. */
62
+ export class GuardPacking {
63
+ private seen = new Map<string, D1Sql[]>();
64
+ private predicates: string[] = [];
65
+ private params: any[] = [];
66
+ private bytes = 0;
67
+ private values = 0;
68
+ private completed = 0;
69
+ private output: D1Sql[] | undefined;
70
+ private readOnly: boolean;
71
+ constructor(readOnly = false, collect = false) { this.readOnly = readOnly; if (collect) this.output = []; }
72
+ fork(): GuardPacking {
73
+ const next = new GuardPacking(this.readOnly);
74
+ next.seen = new Map([...this.seen].map(([key, value]) => [key, [...value]]));
75
+ next.predicates = [...this.predicates]; next.params = [...this.params];
76
+ next.bytes = this.bytes; next.values = this.values; next.completed = this.completed;
77
+ return next;
78
+ }
79
+ private flush(): void {
80
+ if (this.predicates.length) {
81
+ const text = conjunction(this.predicates), packed = this.readOnly
82
+ ? { sql: `SELECT CASE WHEN (${text}) THEN 1 ELSE 0 END AS ok`, params: this.params }
83
+ : guard({ sql: text, params: this.params });
84
+ checkSql(packed); this.output?.push(packed); this.completed++;
70
85
  }
71
- predicates = []; params = []; bytes = 0;
72
- };
73
- for (const statement of statements) {
74
- checkSql(statement);
75
- const predicate = guardPredicate(statement);
76
- if (!predicate) {
77
- if (readOnly) throw new Error('IMPORT_READ_GUARD_REQUIRED');
78
- flush(); seen.clear(); result.push(statement); continue;
86
+ this.predicates = []; this.params = []; this.bytes = this.values = 0;
87
+ }
88
+ append(statements: D1Sql[]): this {
89
+ for (const statement of statements) {
90
+ checkSql(statement);
91
+ const predicate = guardPredicate(statement);
92
+ if (!predicate) {
93
+ if (this.readOnly) throw new Error('IMPORT_READ_GUARD_REQUIRED');
94
+ this.flush(); this.seen.clear(); this.output?.push(statement); this.completed++; continue;
95
+ }
96
+ const prior = this.seen.get(statement.sql) ?? [];
97
+ if (prior.some(s => (s.params?.length ?? 0) === (statement.params?.length ?? 0) && (s.params ?? []).every((p, i) => p === statement.params![i]))) continue;
98
+ prior.push(statement); this.seen.set(statement.sql, prior);
99
+ const params = predicate.params ?? [], text = `(${predicate.sql})`;
100
+ const bytes = Buffer.byteLength(text), values = params.reduce((n, p) => n + valueBytes(p), 0);
101
+ if (this.predicates.length && (this.params.length + params.length > 100 || this.bytes + bytes > 90000 || this.values + values > 1500000 || this.predicates.length >= 50)) this.flush();
102
+ this.predicates.push(text); this.params.push(...params); this.bytes += bytes; this.values += values;
79
103
  }
80
- const prior = seen.get(statement.sql) ?? [];
81
- if (prior.some(s => (s.params?.length ?? 0) === (statement.params?.length ?? 0) && (s.params ?? []).every((p, i) => p === statement.params![i]))) continue;
82
- prior.push(statement); seen.set(statement.sql, prior);
83
- const p = predicate.params ?? [], text = `(${predicate.sql})`;
84
- const nextBytes = Buffer.byteLength(text);
85
- if (predicates.length && (params.length + p.length > 100 || bytes + nextBytes > 90000 || params.reduce((n, p) => n + valueBytes(p), 0) + p.reduce((n, p) => n + valueBytes(p), 0) > 1500000 || predicates.length >= 50)) flush();
86
- predicates.push(text); params.push(...p); bytes += nextBytes;
104
+ return this;
87
105
  }
88
- flush(); return result;
106
+ count(tail: D1Sql[] = []): number { const next = this.fork().append(tail); next.flush(); return next.completed; }
107
+ finish(): D1Sql[] { this.flush(); return this.output!; }
108
+ }
109
+ export function packGuards(statements: D1Sql[], readOnly = false): D1Sql[] {
110
+ return new GuardPacking(readOnly, true).append(statements).finish();
89
111
  }
@@ -6,6 +6,7 @@ import type { D1PlanCodec } from './types.ts';
6
6
  import { planFingerprint } from '../plan/index.ts';
7
7
  import { canonicalHash, canonicalSize, canonicalChunks, cloneJson } from '../provenance/canonical-stream.ts';
8
8
  import { canonicalJson, sha256 } from '../provenance/identity.ts';
9
+ import { ownDecodedPlan, reusedExpansion, retainVerifiedExpansion, type PlanReadScope } from './verified-plan.ts';
9
10
 
10
11
  function validateRoundTrip(plan: ImportPlan, payload: Json, codec: D1PlanCodec): void {
11
12
  const roundtrip = codec.decode(cloneJson(payload));
@@ -25,7 +26,7 @@ export function expandCompressedPlan(body: string, codec?: D1PlanCodec): ImportP
25
26
  }
26
27
  /** Custody loading already parses the envelope to distinguish storage formats.
27
28
  * Reuse that object, still comparing its complete canonical bytes with body. */
28
- export function expandCompressedEnvelope(body: string, value: any, codec?: D1PlanCodec): ImportPlan {
29
+ export function expandCompressedEnvelope(body: string, value: any, codec?: D1PlanCodec, scope?: PlanReadScope): ImportPlan {
29
30
  if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('STORED_PLAN_CHANGED');
30
31
  if (canonicalHash(value) !== sha256(body)) throw new Error('STORED_PLAN_CHANGED');
31
32
  if (![2,3].includes(value.d1Plan) || !Number.isSafeInteger(value.size) || value.size < 0) throw new Error('STORED_PLAN_CHANGED');
@@ -35,12 +36,18 @@ export function expandCompressedEnvelope(body: string, value: any, codec?: D1Pla
35
36
  if (value.compression !== undefined && value.compression !== 'brotli') throw new Error('STORED_PLAN_CHANGED');
36
37
  if (value.d1Plan === 3 && (!Number.isSafeInteger(value.expandedSize) || value.expandedSize < 0)) throw new Error('STORED_PLAN_CHANGED');
37
38
  if (value.d1Plan === 3 && value.dictionary === 2 && (!Number.isSafeInteger(value.payloadSize) || value.payloadSize < 0)) throw new Error('STORED_PLAN_CHANGED');
39
+ // loadPlans has freshly checked the complete manifest and chunk bytes. Only
40
+ // this exact request/body/codec can reuse its already verified expansion.
41
+ const prior = scope && reusedExpansion(scope, body, codec);
42
+ if (prior) return prior;
38
43
  const payload = decodePayload(value, codec);
39
44
  const payloadHash = value.d1Plan === 3 ? canonicalHash(payload) : undefined;
40
- const plan = (value.d1Plan === 3 ? codec!.decode(payload) : payload) as ImportPlan;
45
+ const decoded = (value.d1Plan === 3 ? codec!.decode(payload) : payload) as ImportPlan;
46
+ const plan = scope ? ownDecodedPlan(decoded) : decoded;
41
47
  if (value.d1Plan === 3 && canonicalHash(codec!.encode(plan)) !== payloadHash) throw new Error('STORED_PLAN_CHANGED');
42
48
  if (value.d1Plan === 3 && plan.fingerprint !== value.fingerprint) throw new Error('STORED_PLAN_CHANGED');
43
49
  if (value.d1Plan === 3 && canonicalSize(plan) !== value.expandedSize) throw new Error('STORED_PLAN_CHANGED');
50
+ if (scope) return retainVerifiedExpansion(scope, body, plan, codec);
44
51
  if (planFingerprint(plan) !== plan.fingerprint) throw new Error('STORED_PLAN_CHANGED');
45
52
  return plan;
46
53
  }
@@ -99,7 +99,7 @@ export async function atomic(context: D1ImportContext, action: ImportAction, sta
99
99
  }
100
100
  await context.storage.database.batch(packed.map(s => prepare(context.storage.database, s)));
101
101
  }
102
- function postConditions(context:Pick<D1ImportContext,'maxBatchBytes'>,statements:D1Sql[],scope?:NativeStagingScope,action:ImportAction='read'):D1Sql[]{
102
+ export function postConditions(context:Pick<D1ImportContext,'maxBatchBytes'>,statements:D1Sql[],scope?:NativeStagingScope,action:ImportAction='read'):D1Sql[]{
103
103
  const stream = scope ? [...jobLayoutGuards(scope.job), ...streamBoundary(scope.job.id, action)] : [];
104
104
  if(context.maxBatchBytes===undefined)return [...stream,...statements.flatMap(statement=>statement.finalChecks??[])];
105
105
  const readOnly=statements.every(s=>guardPredicate(s));
@@ -132,6 +132,7 @@ function atomicPreparation(context:Parameters<typeof authority>[0]&Pick<D1Import
132
132
  const packed=[...atomicStatements(context,action,statements),...readonlyChecks(post),...(post.length?[finalAuthority(context,action)]:[])];
133
133
  return {post,packed,count:packed.length+returnReads};
134
134
  }
135
+ export { atomicPrefix } from './atomic-prefix.ts';
135
136
  /** SELECT-only fence: inspect every packed scalar before publishing snapshots.
136
137
  * Mutations continue to use atomic() and SQL CHECK rollback, never this path. */
137
138
  export async function readAtomic(context: D1ImportContext, guards: D1Sql[], scope?: NativeStagingScope, action: ImportAction = 'read'): Promise<void> {
@@ -16,6 +16,7 @@ import { guardErasedSources } from './erasure-claims.ts';
16
16
  import { readOriginals } from './originals.ts';
17
17
  import { retiredError } from './retirement.ts';
18
18
  import { attachReversal, loadReversalState, reversalGuards, reversalState } from './undo-state.ts';
19
+ import { verifiedPlanFingerprint } from './verified-plan.ts';
19
20
 
20
21
  export function decode(row: any): ImportJob {
21
22
  return { id: row.id, fingerprint: row.fingerprint, destination: row.destination, namespace: row.namespace, mappingVersion: row.mapping_version, principalId: row.principal_id, state: row.state, checkpoint: row.checkpoint, total: row.total, fence: row.fence, error: row.error };
@@ -33,7 +34,7 @@ export async function stored(id: string, context: D1ImportContext, action: Impor
33
34
  planReads.set(job, planChunkReads(row.plan_json, fenced));
34
35
  if (job.destination !== context.destination || job.namespace !== context.namespace || job.mappingVersion !== context.mappingVersion || job.principalId !== context.authorization.principalId) throw new Error('JOB_BINDING_MISMATCH');
35
36
  if (context.reviewedFingerprint !== undefined && context.reviewedFingerprint !== job.fingerprint) throw new Error('REVIEW_FINGERPRINT_MISMATCH');
36
- if (planFingerprint(plan) !== job.fingerprint || plan.fingerprint !== job.fingerprint || plan.destination !== job.destination || plan.namespace !== job.namespace || plan.mappingVersion !== job.mappingVersion || plan.principalId !== job.principalId || plan.entries.length !== job.total) throw new Error('STORED_PLAN_CHANGED');
37
+ if ((verifiedPlanFingerprint(plan) ?? planFingerprint(plan)) !== job.fingerprint || plan.fingerprint !== job.fingerprint || plan.destination !== job.destination || plan.namespace !== job.namespace || plan.mappingVersion !== job.mappingVersion || plan.principalId !== job.principalId || plan.entries.length !== job.total) throw new Error('STORED_PLAN_CHANGED');
37
38
  job.erasedRecords = plan.entries.filter(entry => entry.erased).length;
38
39
  attachReversal(job, await loadReversalState(row, plan, context));
39
40
  await captureJobLayout(job, row, context, action);
@@ -2,6 +2,7 @@ import { retainedSql } from './retained-values.ts';
2
2
  import { changedLazy, putRow } from './expected-state.ts';
3
3
  import { compressedPlan, storedStreamPage } from './stream-plan.ts';
4
4
  import { expandCompressedEnvelope } from './compressed-plan.ts';
5
+ import { planReadScope } from './verified-plan.ts';
5
6
  import { Buffer } from 'node:buffer';
6
7
  import { createHash } from 'node:crypto';
7
8
  import type { ImportPlan } from '../types.ts';
@@ -79,6 +80,7 @@ export async function loadPlans(rows: any[], db: D1Database, codec?: D1PlanCodec
79
80
  }
80
81
  }
81
82
  for (const row of rows) {
83
+ const scope = planReadScope(db, row.id, row.plan_json, codec);
82
84
  const parsed = JSON.parse(row.plan_json), parts = chunks.filter(c => c.job_id === row.id);
83
85
  let plan: ImportPlan, compressed = false;
84
86
  if (parsed?.d1Plan === 1) {
@@ -86,11 +88,11 @@ export async function loadPlans(rows: any[], db: D1Database, codec?: D1PlanCodec
86
88
  const body = parts.map(c => c.body).join('');
87
89
  if (Buffer.byteLength(body) !== parsed.size || sha256(body) !== parsed.sha256) throw new Error('STORED_PLAN_CHANGED');
88
90
  const payload = JSON.parse(body);
89
- if ([2,3].includes(payload?.d1Plan)) { if (parsed.entries !== undefined) throw new Error('STORED_PLAN_CHANGED'); plan = expandCompressedEnvelope(body,payload,codec); compressed = true; }
91
+ if ([2,3].includes(payload?.d1Plan)) { if (parsed.entries !== undefined) throw new Error('STORED_PLAN_CHANGED'); plan = expandCompressedEnvelope(body,payload,codec,scope); compressed = true; }
90
92
  else { if (canonicalHash(payload) !== parsed.sha256) throw new Error('STORED_PLAN_CHANGED'); plan = payload; }
91
93
  // The entry locator a referenced read trusts must describe these exact chunks.
92
94
  if (parsed.entries !== undefined && canonicalJson(entryLocator(plan, parts)) !== canonicalJson(parsed.entries)) throw new Error('STORED_PLAN_CHANGED');
93
- } else plan = [2,3].includes(parsed?.d1Plan) ? expandCompressedEnvelope(row.plan_json,parsed,codec) : parsed;
95
+ } else plan = [2,3].includes(parsed?.d1Plan) ? expandCompressedEnvelope(row.plan_json,parsed,codec,scope) : parsed;
94
96
  const captured = fences.get(row.id);
95
97
  snapshots.set(plan, { body: row.plan_json, chunks: parts, ...(row.custody_revision === undefined ? {} : {revision: row.custody_revision}), ...(compressed ? {compressed} : {}), ...(captured === undefined ? {} : {fence: captured}) }); result.set(row.id, plan);
96
98
  }
@@ -0,0 +1,63 @@
1
+ import type { ImportPlan } from '../types.ts';
2
+ import { planFingerprint } from '../plan/index.ts';
3
+ import { cloneJson } from '../provenance/canonical-stream.ts';
4
+ import type { D1Database, D1PlanCodec } from './types.ts';
5
+
6
+ export interface PlanReadScope { database: D1Database; jobId: string; storedBody: string }
7
+ interface VerifiedExpansion {
8
+ jobId: string; storedBody: string; body: string; codecId?: string;
9
+ encode?: D1PlanCodec['encode']; decode?: D1PlanCodec['decode'];
10
+ plan: ImportPlan; fingerprint: string;
11
+ }
12
+ // One active child per request database bounds retention. This contains no job
13
+ // progress, custody fence or authority result; those are read on every load.
14
+ const expansions = new WeakMap<D1Database, VerifiedExpansion>();
15
+ const owned = new WeakSet<ImportPlan>();
16
+ const fingerprints = new WeakMap<ImportPlan, string>();
17
+
18
+ /** Visit ordinary plans too: changing away from compressed custody invalidates
19
+ * its previous receipt even if a later read returns to those old bytes. */
20
+ export function planReadScope(database: D1Database, jobId: string, storedBody: string, codec?: D1PlanCodec): PlanReadScope {
21
+ const prior = expansions.get(database);
22
+ if (prior && (prior.jobId !== jobId || prior.storedBody !== storedBody || prior.codecId !== codec?.id || prior.encode !== codec?.encode || prior.decode !== codec?.decode)) expansions.delete(database);
23
+ return { database, jobId, storedBody };
24
+ }
25
+
26
+ /** Codec callbacks may retain aliases. Own every container before protecting
27
+ * the graph whose verified fingerprint will survive awaited database reads. */
28
+ export function ownDecodedPlan(plan: ImportPlan): ImportPlan {
29
+ const result = cloneJson(plan);
30
+ const freeze = (value: unknown): void => {
31
+ if (value === null || typeof value !== 'object' || Object.isFrozen(value)) return;
32
+ for (const child of Object.values(value)) freeze(child);
33
+ Object.freeze(value);
34
+ };
35
+ freeze(result); owned.add(result);
36
+ return result;
37
+ }
38
+
39
+ /** A fresh root keeps plans.ts custody snapshots independent even when their
40
+ * immutable content is shared by overlapping reads of the same request. */
41
+ function readView(expansion: VerifiedExpansion): ImportPlan {
42
+ const plan = Object.freeze({ ...expansion.plan });
43
+ fingerprints.set(plan, expansion.fingerprint);
44
+ return plan;
45
+ }
46
+ export function reusedExpansion(scope: PlanReadScope, body: string, codec?: D1PlanCodec): ImportPlan | undefined {
47
+ const prior = expansions.get(scope.database);
48
+ if (prior?.jobId === scope.jobId && prior.storedBody === scope.storedBody && prior.body === body && prior.codecId === codec?.id && prior.encode === codec?.encode && prior.decode === codec?.decode) return readView(prior);
49
+ // A changed body, job or codec must never recover an earlier receipt later.
50
+ expansions.delete(scope.database);
51
+ return undefined;
52
+ }
53
+ export function retainVerifiedExpansion(scope: PlanReadScope, body: string, plan: ImportPlan, codec?: D1PlanCodec): ImportPlan {
54
+ if (!owned.has(plan)) throw new Error('IMPORT_PLAN_VERIFICATION_REQUIRED');
55
+ const fingerprint = planFingerprint(plan);
56
+ if (fingerprint !== plan.fingerprint) throw new Error('STORED_PLAN_CHANGED');
57
+ const expansion = { jobId: scope.jobId, storedBody: scope.storedBody, body, codecId: codec?.id, encode: codec?.encode, decode: codec?.decode, plan, fingerprint };
58
+ expansions.set(scope.database, expansion);
59
+ return readView(expansion);
60
+ }
61
+ /** Only owned immutable decoded graphs receive this receipt. Caller-owned
62
+ * creation plans and ordinary mutable decoded plans still need a full hash. */
63
+ export const verifiedPlanFingerprint = (plan: ImportPlan): string | undefined => fingerprints.get(plan);