@runeditrun/rer 0.2.98 → 0.2.100

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 (78) hide show
  1. package/README.md +1 -1
  2. package/cli/README.md +88 -2
  3. package/cli/business/api.mjs +4 -2
  4. package/cli/business/bounded-waves.d.mts +23 -0
  5. package/cli/business/bounded-waves.mjs +57 -0
  6. package/cli/business/catchup-task.mjs +9 -2
  7. package/cli/business/command.mjs +16 -4
  8. package/cli/business/connect.mjs +39 -33
  9. package/cli/business/export-inputs.mjs +12 -2
  10. package/cli/business/export-task.mjs +9 -2
  11. package/cli/business/legacy-package.mjs +117 -0
  12. package/cli/business/local-commands.mjs +10 -1
  13. package/cli/business/package-transfer.mjs +127 -0
  14. package/cli/business/questions.mjs +2 -2
  15. package/cli/business/state.mjs +47 -5
  16. package/cli/business/supervisor.mjs +46 -7
  17. package/cli/business/vendor-door.mjs +86 -13
  18. package/cli/business/vendors/README.md +41 -2
  19. package/cli/business/vendors/common.mjs +81 -0
  20. package/cli/business/vendors/index.mjs +12 -2
  21. package/cli/business/vendors/zendesk-attachments.mjs +57 -0
  22. package/cli/business/vendors/zendesk-encode.mjs +133 -0
  23. package/cli/business/vendors/zendesk-guide.mjs +34 -0
  24. package/cli/business/vendors/zendesk-package.mjs +28 -0
  25. package/cli/business/vendors/zendesk-pages.mjs +61 -0
  26. package/cli/business/vendors/zendesk-people.mjs +7 -0
  27. package/cli/business/vendors/zendesk-store.mjs +154 -0
  28. package/cli/business/vendors/zendesk-tickets.mjs +21 -0
  29. package/cli/business/vendors/zendesk-token.mjs +31 -0
  30. package/cli/business/vendors/zendesk-transport.mjs +48 -0
  31. package/cli/business/vendors/zendesk.mjs +61 -0
  32. package/cli/deploy-command.mjs +5 -4
  33. package/cli/deploy-inputs.mjs +1 -1
  34. package/cli/deploy.mjs +6 -4
  35. package/cli/production-auth.mjs +16 -3
  36. package/cli/production-mail.mjs +18 -1
  37. package/cli/site.mjs +1 -1
  38. package/global/CHANGELOG.md +42 -0
  39. package/global/README.md +1 -1
  40. package/modules/deploy-kit/README.md +10 -0
  41. package/modules/deploy-kit/provider.mjs +18 -2
  42. package/modules/import-kit/src/d1/jobs.ts +3 -1
  43. package/modules/import-kit/src/d1/retirement-scope.ts +2 -2
  44. package/modules/import-kit/src/d1/retirement-tally.ts +1 -0
  45. package/modules/import-kit/src/d1/retirement.ts +15 -5
  46. package/modules/import-kit/src/d1/types.ts +3 -0
  47. package/modules/import-kit/src/d1/wave-boundary.ts +33 -0
  48. package/modules/import-kit/src/d1/wave-retirement.ts +47 -0
  49. package/modules/import-kit/src/door/ADOPTING.md +37 -0
  50. package/modules/import-kit/src/door/README.md +66 -0
  51. package/modules/import-kit/src/door/admission.ts +13 -3
  52. package/modules/import-kit/src/door/authority.ts +1 -0
  53. package/modules/import-kit/src/door/failures.ts +1 -1
  54. package/modules/import-kit/src/door/grants.ts +19 -5
  55. package/modules/import-kit/src/door/handler.ts +12 -1
  56. package/modules/import-kit/src/door/index.ts +6 -0
  57. package/modules/import-kit/src/door/package.ts +121 -0
  58. package/modules/import-kit/src/door/planning.ts +44 -19
  59. package/modules/import-kit/src/door/protocol.ts +4 -2
  60. package/modules/import-kit/src/door/schema.ts +2 -0
  61. package/modules/import-kit/src/door/store.ts +2 -2
  62. package/modules/import-kit/src/door/types.ts +24 -1
  63. package/modules/import-kit/src/door/waves/README.md +158 -0
  64. package/modules/import-kit/src/door/waves/digests.ts +38 -0
  65. package/modules/import-kit/src/door/waves/inventory.ts +84 -0
  66. package/modules/import-kit/src/door/waves/journal.ts +19 -0
  67. package/modules/import-kit/src/door/waves/lifecycle.ts +242 -0
  68. package/modules/import-kit/src/door/waves/preparation.ts +190 -0
  69. package/modules/import-kit/src/door/waves/schema.ts +73 -0
  70. package/modules/import-kit/src/door/waves/store.ts +112 -0
  71. package/modules/import-kit/src/door/waves/types.ts +76 -0
  72. package/onboarding/adapter.mjs +7 -1
  73. package/onboarding/operator-installer.mjs +3 -1
  74. package/package.json +1 -1
  75. package/scripts/publish-check/README.md +20 -0
  76. package/scripts/publish-check/diagnostics.mjs +73 -0
  77. package/scripts/publish-check/slots.py +37 -7
  78. package/scripts/publish-check.mjs +64 -37
@@ -0,0 +1,47 @@
1
+ import type { D1Database, D1ImportPerson, D1PlanCodec, D1Sql } from './types.ts';
2
+ import { targetIdentity } from './erasure-index.ts';
3
+ import { sql } from './database.ts';
4
+ import { doorLineage } from './retirement-door.ts';
5
+ import { canonicalJson, sha256 } from '../provenance/identity.ts';
6
+
7
+ /** Wave snapshots are indivisible custody. Retire their actual child journals
8
+ * whole before ordinary erasure tries to rewrite an attachment-bearing entry.
9
+ * The caller still deletes only the selected person's native data. Peer native
10
+ * records remain; this is withdrawal, never successful Undo or reconciliation. */
11
+ export async function waveRetirementSeeds(db: D1Database, destination: string, person: D1ImportPerson, codec?: D1PlanCodec): Promise<{jobs:string[];roots:string[];tombstones:{installation:string;namespace:string;identity:string}[];guards:D1Sql[]}> {
12
+ const empty={jobs:[],roots:[],tombstones:[],guards:[]};
13
+ if (!person.target && !person.aliases?.length) return empty;
14
+ if (!await db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_wave_children'").first()) {
15
+ if (person.aliases?.length) throw new Error('IMPORT_WAVE_SCHEMA_REQUIRED');
16
+ return empty;
17
+ }
18
+ if (!person.aliases?.length && !await db.prepare('SELECT 1 FROM ik_door_wave_roots WHERE destination=? LIMIT 1').bind(destination).first()) return empty;
19
+ const lineage=await doorLineage(person,db,destination,codec);
20
+ const tombstones=[...lineage.sources.map(source=>{
21
+ const [installation,namespace,kind,id]=JSON.parse(source);
22
+ return {installation,namespace,identity:sha256(canonicalJson([kind,id]))};
23
+ }),...(person.aliases ?? []).map(a=>({installation:a.installationId,namespace:a.namespace,identity:sha256(canonicalJson([a.kind,a.sourceId]))}))];
24
+ const scopes=JSON.stringify(tombstones);
25
+ const selection=`WITH RECURSIVE affected(parent_id) AS (
26
+ SELECT DISTINCT c.parent_id FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id
27
+ JOIN ik_jobs j ON j.id=p.job_id OR j.operation_key='door:'||p.id JOIN ik_d1_plan_index i ON i.job_id=j.id
28
+ WHERE j.destination=? AND i.target_identity=?
29
+ UNION SELECT m.parent_id FROM ik_door_wave_members m JOIN ik_door_wave_roots r ON r.id=m.parent_id JOIN json_each(?) s
30
+ ON r.installation_id=json_extract(s.value,'$.installation') AND r.namespace=json_extract(s.value,'$.namespace')
31
+ AND m.identity=json_extract(s.value,'$.identity') WHERE r.destination=? AND m.type IN ('source','alias')
32
+ UNION SELECT r.id FROM ik_door_wave_roots r JOIN affected a ON r.previous_id=a.parent_id JOIN ik_door_wave_roots parent ON parent.id=a.parent_id WHERE r.destination=? AND r.installation_id=parent.installation_id AND r.namespace=parent.namespace
33
+ ) SELECT parent_id FROM affected`;
34
+ const digest=person.target ? targetIdentity(destination,person.target.kind,person.target.id) : null;
35
+ const parameters=[destination,digest,scopes,destination,destination];
36
+ const roots=(await db.prepare(selection).bind(...parameters).all<{parent_id:string}>()).results.map(row=>row.parent_id);
37
+ if (!roots.length) return {jobs:[],roots:[],tombstones,guards:[]};
38
+ const rootJson=JSON.stringify(roots);
39
+ const members=`SELECT j.id FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id
40
+ JOIN ik_jobs j ON j.id=p.job_id OR j.operation_key='door:'||p.id WHERE j.destination=? AND c.parent_id IN (SELECT value FROM json_each(?))`;
41
+ const jobs=(await db.prepare(members).bind(destination,rootJson).all<{id:string}>()).results.map(row=>row.id);
42
+ return {jobs,roots,tombstones,guards:[
43
+ ...lineage.fence,
44
+ sql(`NOT EXISTS(SELECT 1 FROM (${selection}) WHERE parent_id NOT IN (SELECT value FROM json_each(?)))`,...parameters,rootJson),
45
+ sql(`NOT EXISTS(SELECT 1 FROM (${members}) WHERE id NOT IN (SELECT value FROM json_each(?)))`,destination,rootJson,JSON.stringify(jobs)),
46
+ ]};
47
+ }
@@ -484,3 +484,40 @@ For a concrete grant client and harness see Spool's `tests/business-import.mjs`;
484
484
  its `grant`, `doorRequest`, `nativeSnapshot`, `startTestWorker` and `signIn`
485
485
  provide those network edges. Run with `nice -n 15 node <existing-test-journey>`
486
486
  after the seed's normal build. No `rer check` extension is required.
487
+
488
+ ### Package-aware sources
489
+
490
+ Re-vendor the door together with its ordinary dependency closure, including
491
+ `door/package.ts`, `planning.ts`, `types.ts`, `handler.ts` and `admission.ts`.
492
+ There is no package schema or migration-ledger change: segments use existing
493
+ upload/chunk storage. Include the static `package` declaration in each source
494
+ allow-list result on both mounts. An undeclared namespace remains refused before
495
+ any grant row is saved. See [Logical packages](README.md#logical-packages-across-upload-grants)
496
+ for the stream/index contract and an accessor's bounds.
497
+
498
+ Provide `package.formats`, `maxBytes`, `maxSegments` and `read`. Keep readers
499
+ responsible for format semantics, metadata, per-table counts, attachment bindings
500
+ and explained omissions. Base verifies transport custody and aggregate record
501
+ authority; a reader must return the entire snapshot in one call. It must not
502
+ split a snapshot into independently authorized plans. The legacy CLI deliberately
503
+ declares `records.total: null`: it chooses no record units or waves. HappyToHelp
504
+ must supply D-CAP (package capacity), D-LEG (legacy/delta reader) and D-WAVES
505
+ (aggregate bounded-wave authority) before real oversized legacy imports.
506
+
507
+ The agent registers a locally prepared legacy database export with:
508
+
509
+ ```sh
510
+ rer business legacy-package add DIR
511
+ rer business legacy-package status DIR
512
+ rer business legacy-package remove DIR
513
+ ```
514
+
515
+ Registration verifies all declared file sizes and digests, row chunk/context
516
+ counts, per-table totals, object identities and attachment counts. JSON files pass
517
+ the outbound detector before upload; customer-original media are exempt. It
518
+ stores a private local registration and prints only counts and the manifest SHA.
519
+ A leased `happytohelp-legacy` export checks that registration again, pauses and
520
+ names the local package if it changed, then sends the exact package files through
521
+ the logical package protocol. It needs no vendor key and makes no request to a
522
+ live HappyToHelp account. Disconnect removes the registration and transfer
523
+ journal/spool; removing registration leaves the source directory intact.
@@ -552,3 +552,69 @@ approval. It refuses gone, conflicting, rejected or unmapped records. Additions
552
552
  plus replacements divided by baseline source count must be strictly below 5%;
553
553
  the server derives these counts. A refusal leaves Business to present a new
554
554
  proposal for owner review. No caller-supplied totals relax this boundary.
555
+
556
+ ## Logical packages across upload grants
557
+
558
+ A source may opt in to manifests with `format: "rer-door-package/1"` and
559
+ `package: {formats, maxBytes, maxSegments, read}` on its resolved source. The
560
+ same declaration belongs on the operator and grant mounts. Other sources keep
561
+ their existing readers and upload limits. No additional schema migration or
562
+ larger grant is required. The declaration cannot be combined with `intake`.
563
+
564
+ The sender uploads `rer-package.json` first, within one ordinary upload grant,
565
+ then calls `plan` for that manifest's upload id. The manifest has:
566
+
567
+ ```json
568
+ {
569
+ "format": "rer-door-package/1",
570
+ "source": {"format": "example-snapshot-v1", "version": 1, "origin": "legacy-database", "exportedAt": null, "synthetic": true, "adapter": "example"},
571
+ "mode": "snapshot",
572
+ "stream": {"bytes": 0, "sha256": "<SHA-256 of the entire stream>"},
573
+ "segments": [{"bytes": 2097152, "sha256": "<segment SHA-256>"}],
574
+ "index": {"path": "index.ndjson", "bytes": 0, "sha256": "<index SHA-256>"},
575
+ "records": {"total": null, "byKind": {}},
576
+ "totals": {}
577
+ }
578
+ ```
579
+
580
+ The zero sizes and digest placeholders above describe fields, not an accepted
581
+ manifest. `source.format` must be listed by the seed. Both the index and manifest
582
+ fit one grant. The stream starts with `index.ndjson`, then the source files in
583
+ index order, with no padding or encoding. Each index line is
584
+ `{path, bytes, sha256, offset}`; offsets start at zero immediately after the index.
585
+ Paths are relative, unique, and contain no empty, `.` or `..` components. Source
586
+ files may be record NDJSON, `blobs/<sha256>`, census/tombstone/Guide sidecars, or
587
+ all files of a legacy package byte-for-byte. All stream bytes belong to indexed
588
+ files. Nonfinal segments are exactly the grant's `maxBytes`; the final segment
589
+ may be shorter. Upload each as `rer-segment` in ordinary 64 KiB chunks.
590
+
591
+ Before any plan exists, an aware door returns 409 `package_segment_missing`
592
+ with `details.missing: [{bytes, sha256}]`. Missing, incomplete, altered, wrong-size
593
+ or foreign segments have that same response. Only complete uploads named
594
+ `rer-segment` in the same installation and namespace resolve. The sender stops
595
+ with `SEED_PACKAGE_UNSUPPORTED` if a fresh manifest probe answers otherwise;
596
+ it sends no segments. A resumed, previously accepted probe may find the complete
597
+ package and return its plan. The CLI's private journal saves upload ids before
598
+ requests, uses door inventories to skip durable chunks, and restarts an upload
599
+ whose inventory returns 404. Each request uses a fresh single-use grant.
600
+
601
+ The reader receives `{manifest, files, read(path, maxBytes?)}`. The accessor checks
602
+ segment custody, index layout and each requested file's digest, and enforces the
603
+ declared package bound, the source's `limits.maxBytes` file bound (64 MiB by
604
+ default), and any tighter requested file bound. Read record files
605
+ with a tighter bound where practical. Return the whole snapshot as inert parsed
606
+ records. The door counts the returned aggregate against the current plan grant's
607
+ `maxRecords` before mapping or saving a plan, including legacy packages whose
608
+ `records.total` is null. A numeric declared total and its `byKind` counts must match the returned records.
609
+ A package is one plan, with one lineage comparison and one manifest digest; it
610
+ never authorizes record waves or increases record authority per segment.
611
+
612
+ The package path records manifest/file digest metadata in the plan; it does not
613
+ claim those files as kit-retained originals. Seeds needing durable original
614
+ custody must provide that through their reader/product contract. Grant and D1
615
+ request limits still apply; an oversized read/plan is a refusal, never a partial
616
+ import. HappyToHelp adoption still needs its own package reader, capacity and
617
+ legacy delta semantics. Legacy snapshots above the unchanged 100,000-record
618
+ grant require a separate bounded-wave authority decision before real imports.
619
+
620
+ The opt-in [complete-parent bounded-wave protocol](waves/README.md) uses separately approved children and the existing D1 journal. Ordinary package planning keeps its aggregate refusal.
@@ -1,7 +1,8 @@
1
+ import {importDoorBoundedWavesCheck} from './waves/schema.ts';
1
2
  import type { ImportDoorOptions, ImportDoorRuntime, DoorRequest } from './types.ts';
2
3
  import { runtimeOptions } from './handler.ts';
3
4
  import { DoorError, verifyImportGrant, type DoorAction } from './grants.ts';
4
- import { doorFence, erasures, sql, transact } from './store.ts';
5
+ import { authorized, doorFence, erasures, sql, transact } from './store.ts';
5
6
  import { importDoorSchemaCheck } from './schema.ts';
6
7
 
7
8
  /** The storage key; grants are still verified against `claim.installationId`. */
@@ -21,7 +22,7 @@ async function source(options: ImportDoorOptions, namespace: string): Promise<Im
21
22
  if (!options.source) return runtimeOptions(options);
22
23
  const listed = await options.source(namespace);
23
24
  if (!listed) throw new DoorError('unknown_import_source');
24
- return runtimeOptions({...options, parse: listed.parse, mapping: listed.mapping, intake: listed.intake,
25
+ return runtimeOptions({...options, parse: listed.parse, mapping: listed.mapping, intake: listed.intake, package: listed.package, bounded: listed.bounded,
25
26
  format: listed.format ?? options.format, readRaw: 'readRaw' in listed ? listed.readRaw : undefined,
26
27
  ownerPrincipalId: listed.ownerPrincipalId ?? options.ownerPrincipalId});
27
28
  }
@@ -43,5 +44,14 @@ export async function admit(options: ImportDoorOptions, request: Request, action
43
44
  throw new DoorError('grant_replayed_revoked_or_expired');
44
45
  }
45
46
  const authority = sql(`(${basic.sql}) AND EXISTS(SELECT 1 FROM ik_door_grants WHERE installation_id=? AND jti=? AND action=? AND namespace=? AND kid=? AND expires=?)`, ...basic.params ?? [], key, grant.jti, action, grant.namespace, kid, grant.exp);
46
- return {options: runtime, access: {...grant, installationId: key}, authority, via:'grant', epoch, nonce: {jti: grant.jti, kid}};
47
+ if (grant.bounded) {
48
+ if ((await options.database.prepare(importDoorBoundedWavesCheck).first())?.current!==1) throw new DoorError('bounded_waves_schema_outdated',503);
49
+ if (!runtime.bounded) throw new DoorError('bounded_waves_not_adopted',409);
50
+ const external=runtime.bounded.authority(grant.bounded,action);
51
+ authority.sql=`(${authority.sql}) AND (${external.sql})`;
52
+ authority.params=[...authority.params ?? [],...external.params ?? []];
53
+ }
54
+ const admitted: DoorRequest = {options: runtime, access: {...grant, installationId: key}, authority, via:'grant', epoch, nonce: {jti: grant.jti, kid}};
55
+ if (grant.bounded) await authorized(admitted);
56
+ return admitted;
47
57
  }
@@ -7,6 +7,7 @@ import { approvalCaption } from './failures.ts';
7
7
  import { reserveReturn } from '../d1/budget.ts';
8
8
 
9
9
  const actions: Record<DoorAction, ImportAction[]> = {
10
+ prepare: [], 'seal-parent': [], 'review-parent': [], 'apply-child': [], 'wave-status': [], 'finalize-parent': [], 'reverse-parent': [],
10
11
  uploads: [], plan: ['plan', 'read'], apply: ['create', 'apply', 'read'],
11
12
  status: ['read'], reconcile: ['read'], undo: ['read', 'undo', 'rollback'],
12
13
  cancel: [],
@@ -14,7 +14,7 @@ const transient = new RegExp([
14
14
  ].join('|'),'i');
15
15
  /** Only persist allowlisted classifications, never adapter/SQL messages that
16
16
  * may contain customer records. Providers can wrap SQLite errors in causes. */
17
- function reasonCode(error: unknown): string {
17
+ export function reasonCode(error: unknown): string {
18
18
  const seen = new Set<unknown>();
19
19
  for (let cause = error; cause && typeof cause === 'object' && !seen.has(cause);) {
20
20
  seen.add(cause);
@@ -9,8 +9,8 @@ import type { DoorGrant } from './protocol.ts';
9
9
  function fail(): never { throw new DoorError('invalid_grant'); }
10
10
  const object = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === 'object' && !Array.isArray(value);
11
11
  const requiredKeys = ['v', 'iss', 'aud', 'installationId', 'email', 'jti', 'iat', 'exp', 'action', 'namespace', 'maxBytes', 'maxChunks', 'maxRecords'];
12
- const optionalKeys = ['fingerprint', 'approvalRecordId', 'approvedAt', 'catchup'];
13
- const actions: readonly string[] = ['uploads', 'plan', 'apply', 'status', 'reconcile', 'undo', 'cancel', 'archive', 'restore-check'];
12
+ const optionalKeys = ['fingerprint', 'approvalRecordId', 'approvedAt', 'catchup', 'bounded'];
13
+ const actions: readonly string[] = ['uploads', 'plan', 'apply', 'status', 'reconcile', 'undo', 'cancel', 'archive', 'restore-check', 'prepare', 'seal-parent', 'review-parent', 'apply-child', 'wave-status', 'finalize-parent', 'reverse-parent'];
14
14
 
15
15
  function decode(segment: string): Uint8Array<ArrayBuffer> {
16
16
  if (!/^[A-Za-z0-9_-]+$/.test(segment)) fail();
@@ -80,14 +80,28 @@ function validateGrant(value: Record<string, unknown>, config: Readonly<OwnerCla
80
80
  || !quota(value.maxBytes, 300_000_000) || !quota(value.maxChunks, 1024) || !quota(value.maxRecords, 1_000_000)) fail();
81
81
 
82
82
  const catchup = Object.hasOwn(value, 'catchup');
83
- if (catchup && (value.catchup !== 'bounded' || (value.action !== 'plan' && value.action !== 'apply'))) fail();
83
+ if (catchup && (value.catchup !== 'bounded' || (value.action !== 'plan' && value.action !== 'apply' && !(value.bounded && ['prepare','apply-child'].includes(value.action as string))))) fail();
84
84
  if (catchup && !approved(value)) fail();
85
- if (value.action === 'apply') {
85
+ const waveActions=['prepare','seal-parent','review-parent','apply-child','wave-status','finalize-parent','reverse-parent'];
86
+ const wave=waveActions.includes(value.action as string);
87
+ if (wave !== Object.hasOwn(value,'bounded')) fail();
88
+ if (wave) {
89
+ const b=value.bounded;
90
+ if (!object(b) || b.protocol!=='rer-bounded-waves/1' || !['migration','revision','policy'].every(k=>typeof b[k]==='string' && /^[A-Za-z0-9_-]{1,128}$/.test(b[k] as string))
91
+ || Object.keys(b).some(k=>!['protocol','migration','revision','policy','parentFingerprint','childFingerprint','parentApprovalRecordId','parentApprovedAt'].includes(k))
92
+ || ['parentFingerprint','childFingerprint'].some(k=>Object.hasOwn(b,k) && (typeof b[k]!=='string' || !/^[a-f0-9]{64}$/.test(b[k] as string)))) fail();
93
+ if (['apply-child','finalize-parent','reverse-parent'].includes(value.action as string)
94
+ && (!b.parentFingerprint || typeof b.parentApprovalRecordId!=='string' || !b.parentApprovalRecordId.trim() || b.parentApprovalRecordId.length>256
95
+ || typeof b.parentApprovedAt!=='string' || !Number.isFinite(Date.parse(b.parentApprovedAt)) || new Date(b.parentApprovedAt).toISOString()!==b.parentApprovedAt)) fail();
96
+ if (value.action==='apply-child' && (!b.childFingerprint || value.fingerprint!==b.childFingerprint)) fail();
97
+ if (value.action==='review-parent' && (value.fingerprint!==b.parentFingerprint || value.approvalRecordId!==b.parentApprovalRecordId || value.approvedAt!==b.parentApprovedAt)) fail();
98
+ }
99
+ if (value.action === 'apply' || ['review-parent','apply-child'].includes(value.action as string)) {
86
100
  if (typeof value.fingerprint !== 'string' || !/^[0-9a-f]{64}$/.test(value.fingerprint) || !approved(value)) fail();
87
101
  } else {
88
102
  if (Object.hasOwn(value, 'fingerprint')) fail();
89
103
  const hasApproval = Object.hasOwn(value, 'approvalRecordId') || Object.hasOwn(value, 'approvedAt');
90
- if (hasApproval && (value.action !== 'plan' || !catchup || !approved(value))) fail();
104
+ if (hasApproval && (!['plan','prepare'].includes(value.action as string) || !catchup || !approved(value))) fail();
91
105
  }
92
106
  return Object.freeze(value) as unknown as Readonly<DoorGrant>;
93
107
  }
@@ -11,7 +11,10 @@ import { archive, restoreCheck } from './archive.ts';
11
11
  import { streamUpload } from './intake-upload.ts';
12
12
  import { intakeIdentity } from './intake-policy.ts';
13
13
 
14
- const routes = {uploads,plan,apply,status,reconcile,undo,cancel,archive,'restore-check':restoreCheck};
14
+ import { prepare } from './waves/preparation.ts';
15
+ import { seal, reviewParent, applyChild, waveStatus, finalize, reverseParent } from './waves/lifecycle.ts';
16
+
17
+ const routes = {prepare,'seal-parent':seal,'review-parent':reviewParent,'apply-child':applyChild,'wave-status':waveStatus,'finalize-parent':finalize,'reverse-parent':reverseParent,uploads,plan,apply,status,reconcile,undo,cancel,archive,'restore-check':restoreCheck};
15
18
  const headers = {'Cache-Control':'no-store','Content-Type':'application/json','X-Content-Type-Options':'nosniff'};
16
19
  async function readInput(request: Request): Promise<Record<string,unknown>> {
17
20
  if (request.headers.get('Content-Type')?.split(';')[0] !== 'application/json' || !request.body) throw new DoorError('json_body_required',400);
@@ -46,6 +49,12 @@ export function runtimeOptions<T extends RuntimeConfiguration>(options: T): T &
46
49
  || (options.format === 'raw' ? typeof options.readRaw !== 'function' || options.intake !== undefined
47
50
  : typeof options.parse !== 'function')) throw new DoorError('invalid_configuration',503);
48
51
  if (options.intake !== undefined) intakeIdentity(options.intake);
52
+ const pkg = options.package;
53
+ if (pkg && (options.intake || !Array.isArray(pkg.formats) || !pkg.formats.length || pkg.formats.some(f=>typeof f!=='string' || !f.trim())
54
+ || !Number.isSafeInteger(pkg.maxBytes) || pkg.maxBytes < 1 || !Number.isSafeInteger(pkg.maxSegments) || pkg.maxSegments < 1 || typeof pkg.read !== 'function')) throw new DoorError('invalid_configuration',503);
55
+ if (options.bounded && (!pkg || options.bounded.protocol !== 'rer-bounded-waves/1'
56
+ || options.bounded.batchSize!==undefined && (!Number.isSafeInteger(options.bounded.batchSize) || options.bounded.batchSize<1 || options.bounded.batchSize>20)
57
+ || ['describe','page','native','authority'].some(key=>typeof (options.bounded as any)[key]!=='function'))) throw new DoorError('invalid_configuration',503);
49
58
  return options as T & ImportDoorRuntime;
50
59
  }
51
60
  /** Server-side mount only. A null response belongs to another dispatcher. Every
@@ -78,6 +87,8 @@ export function createDoorHandler(prefix: string, database: D1Database,
78
87
  const input = await readInput(request);
79
88
  admitted = await review(admitted,input);
80
89
  if (['status','reconcile','undo','cancel'].includes(action) && Object.keys(input).some(k=>k!=='planId')) throw new DoorError('invalid_input',400);
90
+ if (['seal-parent','review-parent','wave-status','finalize-parent','reverse-parent'].includes(action) && Object.keys(input).some(k=>k!=='parentId')
91
+ || action==='apply-child' && Object.keys(input).some(k=>!['parentId','ordinal','reviewedFingerprint'].includes(k))) throw new DoorError('invalid_input',400);
81
92
  const result = await routes[action](admitted,input);
82
93
  await authorized(admitted);
83
94
  return Response.json(result,{headers});
@@ -18,3 +18,9 @@ export function createImportDoorHandler(options: ImportDoorOptions): (request: R
18
18
  return createDoorHandler('/api/rer/import/', options.database, (request, action, database) => admit({...options, database}, request, action),
19
19
  undefined, async database => !!options.claim?.publicKeys && Object.keys(options.claim.publicKeys).length > 0 && await enabled({...options, database}));
20
20
  }
21
+
22
+ export { importDoorBoundedWavesMigration } from './waves/schema.ts';
23
+ export type * from './waves/types.ts';
24
+ export { revokeImportDoorWaveApproval } from './waves/store.ts';
25
+ export { waveInventoryHash, inventoryMember } from './waves/digests.ts';
26
+ export type {WaveInventoryInput, WaveInventoryMember} from './waves/digests.ts';
@@ -0,0 +1,121 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { createHash } from 'node:crypto';
3
+ import { sha256 } from '../provenance/identity.ts';
4
+ import { DEFAULT_LIMITS, limitsFor, RecordCollector, safeValue } from '../readers/records.ts';
5
+ import type { ParsedRecord, SourceBundle } from '../types.ts';
6
+ import type { DoorPackageFile, DoorPackageManifest, DoorRequest, ImportDoorPackageUpload } from './types.ts';
7
+ import { DoorError } from './protocol.ts';
8
+ import { bundleBytes, CHUNK_BYTES, type UploadRow } from './uploads.ts';
9
+
10
+ const positive = (n: unknown): n is number => Number.isSafeInteger(n) && (n as number) > 0;
11
+ const count = (n: unknown): n is number => Number.isSafeInteger(n) && (n as number) >= 0;
12
+ const digest = (s: unknown): s is string => typeof s === 'string' && /^[a-f0-9]{64}$/.test(s);
13
+ const filename = (s: unknown): s is string => typeof s === 'string' && /^[A-Za-z0-9_][A-Za-z0-9_./-]*$/.test(s) && !s.split('/').some(p=>!p || p==='.' || p==='..');
14
+ const invalid = (): never => { throw new DoorError('invalid_package',422); };
15
+ const missing = (segments: {bytes: number; sha256: string}[]): never => { throw new DoorError('package_segment_missing',409,{missing:segments}); };
16
+ const json = (bytes: Uint8Array): any => { try { return JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(bytes)); } catch { return invalid(); } };
17
+
18
+ /** Resolve only verified complete segments in this installation AND namespace. No schema or grant changes. */
19
+ export async function packageUpload(request: DoorRequest, upload: UploadRow, resolved?: (rows: readonly UploadRow[]) => void): Promise<ImportDoorPackageUpload> {
20
+ const declaration = request.options.package;
21
+ if (!declaration) throw new DoorError('seed_package_unsupported',415);
22
+ const raw = await bundleBytes(request,upload), value = json(raw);
23
+ safeValue(value);
24
+ if (value?.format!=='rer-door-package/1' || !declaration.formats.includes(value.source?.format) || typeof value.mode!=='string'
25
+ || !positive(value.stream?.bytes) || value.stream.bytes > declaration.maxBytes || !digest(value.stream.sha256)
26
+ || !Array.isArray(value.segments) || !value.segments.length || value.segments.length > declaration.maxSegments
27
+ || value.index?.path!=='index.ndjson' || !positive(value.index.bytes) || value.index.bytes > request.access.maxBytes || !digest(value.index.sha256)
28
+ || !value.records || !(value.records.total===null || count(value.records.total)) || !value.records.byKind || typeof value.records.byKind!=='object' || Array.isArray(value.records.byKind)
29
+ || Object.values(value.records.byKind).some(n=>!count(n))) invalid();
30
+ const manifest = value as DoorPackageManifest;
31
+ let total = 0;
32
+ for (const [i,segment] of manifest.segments.entries()) {
33
+ if (!positive(segment?.bytes) || segment.bytes > request.access.maxBytes || Math.ceil(segment.bytes / CHUNK_BYTES) > request.access.maxChunks
34
+ || i < manifest.segments.length - 1 && segment.bytes !== request.access.maxBytes || !digest(segment.sha256)) invalid();
35
+ total += segment.bytes;
36
+ }
37
+ if (!Number.isSafeInteger(total) || total!==manifest.stream.bytes || manifest.index.bytes > total) invalid();
38
+ const rows: UploadRow[] = [], absent: {bytes:number;sha256:string}[] = [];
39
+ const streamHash = createHash('sha256');
40
+ for (const segment of manifest.segments) {
41
+ const candidates = await request.options.database.prepare(`SELECT * FROM ik_door_uploads WHERE installation_id=? AND namespace=? AND name='rer-segment' AND bundle_sha256=? AND total_bytes=? ORDER BY id`)
42
+ .bind(request.access.installationId,request.access.namespace,segment.sha256,segment.bytes).all<UploadRow>();
43
+ let complete: UploadRow | undefined, bytes: Uint8Array | undefined;
44
+ for (const row of candidates.results) {
45
+ if (row.total_chunks!==Math.ceil(segment.bytes / CHUNK_BYTES) || row.intake_json) continue;
46
+ try { bytes = await bundleBytes(request,row); complete = row; break; }
47
+ catch (error) { if (!(error instanceof DoorError) || !['upload_incomplete','upload_custody_mismatch','bundle_hash_mismatch'].includes(error.code)) throw error; }
48
+ }
49
+ if (!complete || !bytes) absent.push(segment);
50
+ else { rows.push(complete); streamHash.update(bytes); }
51
+ }
52
+ if (absent.length) missing(absent);
53
+ if (streamHash.digest('hex')!==manifest.stream.sha256) invalid();
54
+ // Planning owns these verified custody identities even if the index or reader refuses.
55
+ resolved?.(rows);
56
+ // Range reads materialize at most one segment plus the bounded requested file.
57
+ async function range(start: number, size: number): Promise<Uint8Array> {
58
+ const result = new Uint8Array(size); let offset = 0, written = 0;
59
+ for (let i = 0; i < rows.length && written < size; i++) {
60
+ const end = offset + manifest.segments[i].bytes;
61
+ if (end > start && offset < start + size) {
62
+ let bytes: Uint8Array;
63
+ try { bytes = await bundleBytes(request,rows[i]); } catch (error) { if (error instanceof DoorError && ['upload_incomplete','upload_custody_mismatch','bundle_hash_mismatch'].includes(error.code)) missing([manifest.segments[i]]); throw error; }
64
+ const from = Math.max(start,offset), to = Math.min(start+size,end);
65
+ result.set(bytes.subarray(from-offset,to-offset),from-start); written += to-from;
66
+ }
67
+ offset = end;
68
+ }
69
+ if (written!==size) invalid();
70
+ return result;
71
+ }
72
+ const indexBytes = await range(0,manifest.index.bytes);
73
+ if (sha256(indexBytes)!==manifest.index.sha256) invalid();
74
+ let indexText: string;
75
+ try { indexText = new TextDecoder('utf-8',{fatal:true}).decode(indexBytes); } catch { return invalid(); }
76
+ if (!indexText.endsWith('\n')) invalid();
77
+ const files: DoorPackageFile[] = [], names = new Set<string>(); let offset = 0;
78
+ for (const line of indexText.slice(0,-1).split('\n')) {
79
+ const row = json(Buffer.from(line));
80
+ if (!filename(row?.path) || row.path==='index.ndjson' || names.has(row.path) || !count(row.bytes) || !digest(row.sha256) || row.offset!==offset) invalid();
81
+ offset += row.bytes;
82
+ if (!Number.isSafeInteger(offset) || offset > total - manifest.index.bytes) invalid();
83
+ names.add(row.path); files.push(Object.freeze({path:row.path,bytes:row.bytes,sha256:row.sha256,offset:row.offset}));
84
+ }
85
+ if (!files.length || offset + manifest.index.bytes!==total) invalid();
86
+ // Detach the accessor's custody data from callback-owned metadata.
87
+ const exposed = JSON.parse(JSON.stringify(manifest)) as DoorPackageManifest;
88
+ const fileLimit = Math.min(declaration.maxBytes,request.options.limits?.maxBytes ?? DEFAULT_LIMITS.maxBytes);
89
+ return {manifest:exposed,files:Object.freeze(files),async read(path, maxBytes = fileLimit) {
90
+ const file = files.find(f=>f.path===path);
91
+ if (!file) throw new DoorError('package_file_missing',422);
92
+ if (!count(maxBytes) || file.bytes > Math.min(maxBytes,fileLimit)) throw new DoorError('package_file_exceeds_limit',413);
93
+ const bytes = await range(manifest.index.bytes+file.offset,file.bytes);
94
+ if (sha256(bytes)!==file.sha256) throw new DoorError('package_file_hash_mismatch',409);
95
+ return bytes;
96
+ }};
97
+ }
98
+
99
+ export async function readPackageSource(request: DoorRequest, upload: UploadRow, namespace: string): Promise<SourceBundle> {
100
+ const pkg = await packageUpload(request,upload), declaration = request.options.package!;
101
+ const limits = limitsFor({...request.options.limits,maxBytes:Math.min(declaration.maxBytes,request.options.limits?.maxBytes ?? DEFAULT_LIMITS.maxBytes),
102
+ maxRecords:Math.min(request.options.limits?.maxRecords ?? request.access.maxRecords,request.access.maxRecords)});
103
+ const expectedTotal = pkg.manifest.records.total, expectedKinds = {...pkg.manifest.records.byKind};
104
+ let records: ParsedRecord[];
105
+ try { records = await declaration.read(pkg,Object.freeze({...limits})); }
106
+ catch (error) { if (error instanceof DoorError) throw error; throw new DoorError('package_reader_failed',422); }
107
+ if (!Array.isArray(records)) invalid();
108
+ // Count the reader's aggregate before mapping or saving ANY plan, including when total is null (legacy).
109
+ if (records.length > request.access.maxRecords) throw new DoorError('plan_exceeds_grant_quota');
110
+ if (expectedTotal!==null && records.length!==expectedTotal) throw new DoorError('package_record_count_mismatch',422);
111
+ if (expectedTotal!==null) {
112
+ const actual: Record<string,number> = Object.create(null);
113
+ for (const record of records) actual[record.kind] = (actual[record.kind] ?? 0) + 1;
114
+ if (Object.keys(actual).length!==Object.keys(expectedKinds).length || Object.entries(actual).some(([kind,n])=>expectedKinds[kind]!==n)) throw new DoorError('package_record_count_mismatch',422);
115
+ }
116
+ const collector = new RecordCollector(namespace,limits);
117
+ collector.add(records,{artifact:upload.name},upload.bundle_sha256);
118
+ collector.bundle.artifacts.push({name:upload.name,sha256:upload.bundle_sha256,size:upload.total_bytes},
119
+ ...pkg.files.map(f=>({name:f.path,sha256:f.sha256,size:f.bytes})));
120
+ return collector.bundle;
121
+ }
@@ -1,3 +1,4 @@
1
+ import { packageUpload, readPackageSource } from './package.ts';
1
2
  import { readRawSource } from './raw.ts';
2
3
  import { randomUUID } from 'node:crypto';
3
4
  import { canonicalJson, sha256 } from '../provenance/identity.ts';
@@ -24,26 +25,36 @@ export function preview(snapshot: DoorSnapshot, created: boolean) {
24
25
  export async function plan(request: DoorRequest, input: Record<string, unknown>) {
25
26
  if (Object.keys(input).some(k=>!['uploadId','catchup'].includes(k)) || (input.catchup !== undefined && typeof input.catchup !== 'boolean')) throw new DoorError('invalid_plan_input',400);
26
27
  const upload = await uploadRow(request,text(input.uploadId,'upload_id'));
27
- try { return await planUpload(request,input,upload); }
28
+ const segments: UploadRow[] = [];
29
+ try {
30
+ if (upload.name === 'rer-package.json') await packageUpload(request,upload,rows=>segments.push(...rows));
31
+ return await planUpload(request,input,upload,segments);
32
+ }
28
33
  catch (error) {
29
34
  // A reader or bundle-check refusal answers a named 4xx, never a bare conflict.
30
35
  const refused = error instanceof DoorError ? error : sourceRefusal(error);
31
36
  // An upload holding a credential the reader cannot remove, or a value it
32
37
  // cannot inspect for one, never plans, so it is not kept.
33
38
  const unreadable = refused?.code === 'source_credential' || refused?.code === 'source_invalid_or_credential_source_data';
34
- if (unreadable) await forget(request,upload);
35
- if (unreadable || refused?.code === 'source_erased') throw refused;
39
+ if (unreadable || refused?.code === 'source_erased') await forget(request,upload,segments);
40
+ if (unreadable || refused?.code === 'source_erased' || refused?.code === 'package_segment_missing') throw refused;
36
41
  // Whatever refuses this plan first (a pending import, interrupted staging, a
37
42
  // withdrawn snapshot, the catch-up shape, a bound or the source itself), an
38
43
  // upload holding an erased record is refused for that instead, and goes. A
39
44
  // failing screen leaves the original refusal standing. No refused plan keeps
40
45
  // credential values the reader removes: their uploaded bytes go.
41
- const withheld = await screen(request,upload).catch(screened => { if (refused && screened instanceof DoorError && screened.code === 'source_erased') throw screened; return false; });
42
- if (withheld) await dropChunks(request,upload);
46
+ const withheld = await screen(request,upload,segments).catch(async screened => {
47
+ if (refused && screened instanceof DoorError && screened.code === 'source_erased') {
48
+ await forget(request,upload,segments);
49
+ throw screened;
50
+ }
51
+ return false;
52
+ });
53
+ if (withheld) await dropChunks(request,upload,segments);
43
54
  throw refused ?? error;
44
55
  }
45
56
  }
46
- async function planUpload(request: DoorRequest, input: Record<string, unknown>, upload: UploadRow) {
57
+ async function planUpload(request: DoorRequest, input: Record<string, unknown>, upload: UploadRow, segments: UploadRow[]) {
47
58
  const current = await lineage(request);
48
59
  // A plan whose apply failed, or whose import was undone or rolled back, has
49
60
  // left the lineage: the same bundle plans afresh instead of reusing it. A
@@ -67,7 +78,7 @@ async function planUpload(request: DoorRequest, input: Record<string, unknown>,
67
78
  if (snapshot.sourceCount > request.access.maxRecords) throw new DoorError('plan_exceeds_grant_quota');
68
79
  if (snapshot.plan.mappingVersion !== request.options.mapping.version) throw new DoorError('snapshot_mapping_changed_use_new_namespace',409);
69
80
  if (request.access.catchup === 'bounded' && (!snapshot.standingApproval || snapshot.standingApproval.id !== request.access.approvalRecordId || snapshot.standingApproval.at !== request.access.approvedAt)) throw new DoorError('standing_approval_mismatch');
70
- await dropCredentialBytes(request,snapshot,upload);
81
+ await dropCredentialBytes(request,snapshot,upload,segments);
71
82
  return preview(snapshot,false);
72
83
  }
73
84
  if (current.pending_id) throw new DoorError('namespace_has_pending_import',409);
@@ -85,7 +96,7 @@ async function planUpload(request: DoorRequest, input: Record<string, unknown>,
85
96
  if (!previous && (input.catchup === true || request.access.catchup === 'bounded')) throw new DoorError('catchup_requires_baseline',409);
86
97
  // A catch-up reads the destination's whole native set, which a large destination
87
98
  // refuses (413); an upload holding an erased record is refused and goes first.
88
- if (previous) await screen(request,upload);
99
+ if (previous) await screen(request,upload,segments);
89
100
  const planId = randomUUID(), options = request.options, {kitNamespace,source,retainedFrom} = await readUpload(request,upload,planId);
90
101
  const prior = new Map((previous?.records ?? []).map(r=>[recordKey(r),r]));
91
102
  const revisions = new Map<string,{revision:string;matches:boolean}>();
@@ -135,10 +146,10 @@ async function planUpload(request: DoorRequest, input: Record<string, unknown>,
135
146
  snapshot.records.push({...old,explanation:'Absent from source snapshot; retained in the seed. No deletion proposed.'});
136
147
  }
137
148
  if (request.access.catchup === 'bounded') assertBounded(snapshot,previous!);
138
- await refuseErased(request,upload,snapshot.plan);
149
+ await refuseErased(request,upload,snapshot.plan,segments);
139
150
  if (retired) await retire(request,retired,snapshot.plan);
140
151
  await saveSnapshot(request,snapshot,upload,retainedFrom);
141
- await dropCredentialBytes(request,snapshot,upload);
152
+ await dropCredentialBytes(request,snapshot,upload,segments);
142
153
  return preview(snapshot,true);
143
154
  }
144
155
  /** Retire an erasure-withdrawn live import as the namespace's baseline. The kit
@@ -160,11 +171,24 @@ async function retire(request: DoorRequest, head: DoorPlanRow, plan: ImportPlan)
160
171
  /** The reader removed credential values: the saved plan holds none and withholds
161
172
  * the original, so the uploaded bytes that still hold them go once it is saved.
162
173
  * The upload's header stays, so repeating this plan finds the saved one. */
163
- async function dropCredentialBytes(request: DoorRequest, snapshot: DoorSnapshot, upload: UploadRow): Promise<void> {
164
- if (withheldCredentials(snapshot.plan.source)) await dropChunks(request,upload);
174
+ async function dropCredentialBytes(request: DoorRequest, snapshot: DoorSnapshot, upload: UploadRow, segments: UploadRow[]): Promise<void> {
175
+ if (withheldCredentials(snapshot.plan.source)) await dropChunks(request,upload,segments);
165
176
  }
166
177
  const withheldCredentials = (source: SourceBundle) => source.artifacts.some(a=>a.original?.status==='unavailable' && a.original.reason==='credentials-removed');
167
- const dropChunks = (request: DoorRequest, upload: UploadRow) => transact(request.options.database,request.authority,[sql('DELETE FROM ik_door_chunks WHERE upload_id=?',upload.id)]);
178
+ /** Shared segment bytes cannot outlive a privacy withdrawal through another
179
+ * upload id. Match all equivalent copies in the authorized scope; manifests
180
+ * sharing them must request missing segments again. Foreign scopes stay intact. */
181
+ function cleanup(request: DoorRequest, upload: UploadRow, segments: readonly UploadRow[], headers: boolean) {
182
+ const {installationId,namespace} = request.access;
183
+ const matches = [sql('id=? AND installation_id=? AND namespace=?',upload.id,installationId,namespace),
184
+ ...segments.map(segment=>sql("installation_id=? AND namespace=? AND name='rer-segment' AND bundle_sha256=? AND total_bytes=?",installationId,namespace,segment.bundle_sha256,segment.total_bytes))];
185
+ return matches.flatMap(match=>[
186
+ sql(`DELETE FROM ik_door_chunks WHERE upload_id IN (SELECT id FROM ik_door_uploads WHERE ${match.sql})`,...match.params ?? []),
187
+ ...(headers ? [sql(`DELETE FROM ik_door_uploads WHERE ${match.sql}`,...match.params ?? [])] : []),
188
+ ]);
189
+ }
190
+ const dropChunks = (request: DoorRequest, upload: UploadRow, segments: readonly UploadRow[] = []) =>
191
+ transact(request.options.database,request.authority,cleanup(request,upload,segments,false));
168
192
  /** Each plan has its own kit namespace, so a replan after a failed apply never
169
193
  * meets the failed job's kit namespace. */
170
194
  async function readUpload(request: DoorRequest, upload: UploadRow, planId: string) {
@@ -174,6 +198,7 @@ async function readUpload(request: DoorRequest, upload: UploadRow, planId: strin
174
198
  const retained = await retainedSource(request,upload);
175
199
  if (retained) return {kitNamespace,retainedFrom:retained.planId,source:{...retained.source,records:retained.source.records.map(record=>({...record,namespace:kitNamespace}))}};
176
200
  // Credential values are removed per record, never imported, and never refuse a record.
201
+ if (upload.name === 'rer-package.json') return {kitNamespace,source:await readPackageSource(request,upload,kitNamespace),retainedFrom:undefined};
177
202
  const bytes = await bundleBytes(request,upload);
178
203
  const limits = {...options.limits,maxBytes:Math.min(options.limits?.maxBytes ?? request.access.maxBytes,request.access.maxBytes),maxRecords:Math.min(options.limits?.maxRecords ?? request.access.maxRecords,request.access.maxRecords)};
179
204
  const source = options.format === 'raw'
@@ -206,20 +231,20 @@ async function retainedSource(request: DoorRequest, upload: UploadRow): Promise<
206
231
  }
207
232
  /** The kit never stages an erased record, and neither does the door: an upload
208
233
  * holding one can never be planned, so it goes too. The epoch fences a later erasure. */
209
- export async function refuseErased(request: DoorRequest, upload: UploadRow, plan: ImportPlan): Promise<void> {
234
+ export async function refuseErased(request: DoorRequest, upload: UploadRow, plan: ImportPlan, segments: readonly UploadRow[] = []): Promise<void> {
210
235
  const db = request.options.database;
211
236
  for (const clear of erasedSourcePredicates(plan)) if ((await statement(db, sql(`SELECT ${clear.sql} AS clear`, ...clear.params ?? [])).first())?.clear !== 1) {
212
- await forget(request, upload);
237
+ await forget(request,upload,segments);
213
238
  throw new DoorError('source_erased', 409);
214
239
  }
215
240
  }
216
- const forget = (request: DoorRequest, upload: UploadRow) => transact(request.options.database, request.authority,
217
- [sql('DELETE FROM ik_door_chunks WHERE upload_id=?', upload.id), sql('DELETE FROM ik_door_uploads WHERE id=?', upload.id)]);
241
+ export const forget = (request: DoorRequest, upload: UploadRow, segments: readonly UploadRow[] = []) =>
242
+ transact(request.options.database,request.authority,cleanup(request,upload,segments,true));
218
243
  /** The upload planned on its own with the plain mapping: one holding an erased
219
244
  * record is refused and goes. Answers whether the reader removed credential values. */
220
- async function screen(request: DoorRequest, upload: UploadRow): Promise<boolean> {
245
+ async function screen(request: DoorRequest, upload: UploadRow, segments: readonly UploadRow[] = []): Promise<boolean> {
221
246
  const id = randomUUID(), {kitNamespace,source} = await readUpload(request,upload,id), mapping = request.options.mapping;
222
- await refuseErased(request,upload,await planD1Import(source,mapping,kitContext(request,{id,kitNamespace,plan:{mappingVersion:mapping.version} as ImportPlan})));
247
+ await refuseErased(request,upload,await planD1Import(source,mapping,kitContext(request,{id,kitNamespace,plan:{mappingVersion:mapping.version} as ImportPlan})),segments);
223
248
  return withheldCredentials(source);
224
249
  }
225
250
  export function assertBounded(snapshot: DoorSnapshot, previous: DoorSnapshot): void {
@@ -1,6 +1,7 @@
1
- export type DoorAction = 'uploads' | 'plan' | 'apply' | 'status' | 'reconcile' | 'undo' | 'cancel' | 'archive' | 'restore-check';
1
+ import type { WaveBinding } from './waves/types.ts';
2
+ export type DoorAction = 'uploads' | 'plan' | 'apply' | 'status' | 'reconcile' | 'undo' | 'cancel' | 'archive' | 'restore-check' | 'prepare' | 'seal-parent' | 'review-parent' | 'apply-child' | 'wave-status' | 'finalize-parent' | 'reverse-parent';
2
3
  /** Grant-only actions: the owner-session mount answers them 404. */
3
- export const GRANT_ONLY_ACTIONS: readonly DoorAction[] = ['cancel', 'archive', 'restore-check'];
4
+ export const GRANT_ONLY_ACTIONS: readonly DoorAction[] = ['cancel', 'archive', 'restore-check', 'prepare', 'seal-parent', 'review-parent', 'apply-child', 'wave-status', 'finalize-parent', 'reverse-parent'];
4
5
 
5
6
  export interface DoorGrant {
6
7
  v: 1;
@@ -20,6 +21,7 @@ export interface DoorGrant {
20
21
  approvalRecordId?: string;
21
22
  approvedAt?: string;
22
23
  catchup?: 'bounded';
24
+ bounded?: WaveBinding;
23
25
  }
24
26
 
25
27
  export class DoorError extends Error {
@@ -1,3 +1,4 @@
1
+ import { importDoorBoundedWavesMigration } from './waves/schema.ts';
1
2
  function planTables(suffix = ''): string[] {
2
3
  return [
3
4
  `CREATE TABLE IF NOT EXISTS ik_door_plans${suffix} (
@@ -98,4 +99,5 @@ export const importDoorSchema = [
98
99
  ...failureSchema,
99
100
  ...importDoorScopesMigration,
100
101
  ...importDoorArchiveMigration,
102
+ ...importDoorBoundedWavesMigration,
101
103
  ];
@@ -8,8 +8,8 @@ import { importDoorSchemaCheck } from './schema.ts';
8
8
  export const sql = (sql: string, ...params: unknown[]): D1Sql => ({ sql, params });
9
9
  export const statement = (db: D1Database, query: D1Sql) => db.prepare(query.sql).bind(...query.params ?? []);
10
10
  export const assertion = (query: D1Sql) => sql(`INSERT INTO ik_d1_assertions(ok) SELECT CASE WHEN (${query.sql}) THEN 1 ELSE 0 END`, ...query.params ?? []);
11
- export async function transact(db: D1Database, authority: D1Sql, queries: D1Sql[]): Promise<void> {
12
- await db.batch([assertion(authority), ...queries, assertion(authority), sql('DELETE FROM ik_d1_assertions')].map(q => statement(db, q)));
11
+ export async function transact(db: D1Database, authority: D1Sql, queries: D1Sql[], final: D1Sql[] = []): Promise<void> {
12
+ await db.batch([assertion(authority), ...queries, assertion(authority), sql('DELETE FROM ik_d1_assertions'), ...[authority,...final].map(predicate=>sql(`SELECT CASE WHEN (${predicate.sql}) THEN 1 ELSE json('IMPORT_ATOMIC_GUARD') END`,...predicate.params ?? []))].map(q => statement(db, q)));
13
13
  }
14
14
  export async function authorized(request: DoorRequest): Promise<void> {
15
15
  const row = await statement(request.options.database, sql(`SELECT CASE WHEN (${request.authority.sql}) THEN 1 ELSE 0 END AS ok`, ...request.authority.params ?? [])).first();