@runeditrun/rer 0.2.100 → 0.2.102

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 (93) hide show
  1. package/README.md +30 -102
  2. package/cli/README.md +16 -89
  3. package/cli/app-address.mjs +2 -0
  4. package/cli/business/api.mjs +2 -1
  5. package/cli/business/choices.mjs +1 -1
  6. package/cli/business/command.mjs +36 -30
  7. package/cli/business/connect.mjs +1 -1
  8. package/cli/business/lease.mjs +0 -9
  9. package/cli/business/local-commands.mjs +1 -27
  10. package/cli/business/outbound.mjs +2 -14
  11. package/cli/business/questions.mjs +85 -48
  12. package/cli/business/service.mjs +1 -24
  13. package/cli/business/setup-command.mjs +33 -26
  14. package/cli/business/state.mjs +1 -1
  15. package/cli/business/supervisor.mjs +26 -91
  16. package/cli/companion/command.mjs +2 -0
  17. package/cli/demo-native.mjs +1 -1
  18. package/cli/parse.mjs +2 -1
  19. package/global/CHANGELOG.md +32 -0
  20. package/global/README.md +30 -102
  21. package/global/main.mjs +2 -0
  22. package/modules/container/adapters/d1-limits.mjs +2 -10
  23. package/modules/d1-transport/limits.d.mts +10 -0
  24. package/modules/d1-transport/limits.mjs +10 -0
  25. package/modules/demo-host/native/prepare.mjs +2 -1
  26. package/modules/deploy-kit/d1-import.mjs +11 -1
  27. package/modules/deploy-kit/migrations.mjs +14 -1
  28. package/modules/import-kit/schema/importers.schema.json +104 -2
  29. package/modules/import-kit/src/d1/README.md +14 -0
  30. package/modules/import-kit/src/d1/apply-reads.ts +74 -0
  31. package/modules/import-kit/src/d1/apply.ts +32 -9
  32. package/modules/import-kit/src/d1/history.ts +2 -2
  33. package/modules/import-kit/src/d1/redaction-step.ts +17 -7
  34. package/modules/import-kit/src/d1/types.ts +11 -0
  35. package/modules/import-kit/src/door/ADOPTING.md +0 -37
  36. package/modules/import-kit/src/door/README.md +0 -66
  37. package/modules/import-kit/src/door/admission.ts +3 -13
  38. package/modules/import-kit/src/door/authority.ts +0 -1
  39. package/modules/import-kit/src/door/failures.ts +1 -1
  40. package/modules/import-kit/src/door/grants.ts +5 -19
  41. package/modules/import-kit/src/door/handler.ts +1 -12
  42. package/modules/import-kit/src/door/index.ts +0 -6
  43. package/modules/import-kit/src/door/planning.ts +19 -44
  44. package/modules/import-kit/src/door/protocol.ts +2 -4
  45. package/modules/import-kit/src/door/schema.ts +0 -2
  46. package/modules/import-kit/src/door/store.ts +2 -2
  47. package/modules/import-kit/src/door/types.ts +1 -24
  48. package/modules/import-kit/src/provenance/credentials.ts +14 -5
  49. package/modules/import-kit/src/status/evidence.ts +3 -1
  50. package/modules/import-kit/src/status/index.ts +17 -2
  51. package/modules/import-kit/src/status/schema.ts +8 -0
  52. package/modules/import-kit/src/status/types.ts +25 -4
  53. package/package.json +1 -1
  54. package/cli/business/archive-task.mjs +0 -32
  55. package/cli/business/bounded-waves.d.mts +0 -23
  56. package/cli/business/bounded-waves.mjs +0 -57
  57. package/cli/business/catchup-task.mjs +0 -49
  58. package/cli/business/export-inputs.mjs +0 -79
  59. package/cli/business/export-task.mjs +0 -24
  60. package/cli/business/legacy-package.mjs +0 -117
  61. package/cli/business/package-transfer.mjs +0 -127
  62. package/cli/business/setup-demo.mjs +0 -95
  63. package/cli/business/vendor-door.mjs +0 -303
  64. package/cli/business/vendors/README.md +0 -215
  65. package/cli/business/vendors/calendly.mjs +0 -80
  66. package/cli/business/vendors/common.mjs +0 -280
  67. package/cli/business/vendors/credential-names.mjs +0 -124
  68. package/cli/business/vendors/index.mjs +0 -96
  69. package/cli/business/vendors/pagerduty.mjs +0 -61
  70. package/cli/business/vendors/sentry.mjs +0 -85
  71. package/cli/business/vendors/sorry.mjs +0 -106
  72. package/cli/business/vendors/uptimerobot.mjs +0 -21
  73. package/cli/business/vendors/zendesk-attachments.mjs +0 -57
  74. package/cli/business/vendors/zendesk-encode.mjs +0 -133
  75. package/cli/business/vendors/zendesk-guide.mjs +0 -34
  76. package/cli/business/vendors/zendesk-package.mjs +0 -28
  77. package/cli/business/vendors/zendesk-pages.mjs +0 -61
  78. package/cli/business/vendors/zendesk-people.mjs +0 -7
  79. package/cli/business/vendors/zendesk-store.mjs +0 -154
  80. package/cli/business/vendors/zendesk-tickets.mjs +0 -21
  81. package/cli/business/vendors/zendesk-token.mjs +0 -31
  82. package/cli/business/vendors/zendesk-transport.mjs +0 -48
  83. package/cli/business/vendors/zendesk.mjs +0 -61
  84. package/modules/import-kit/src/door/package.ts +0 -121
  85. package/modules/import-kit/src/door/waves/README.md +0 -158
  86. package/modules/import-kit/src/door/waves/digests.ts +0 -38
  87. package/modules/import-kit/src/door/waves/inventory.ts +0 -84
  88. package/modules/import-kit/src/door/waves/journal.ts +0 -19
  89. package/modules/import-kit/src/door/waves/lifecycle.ts +0 -242
  90. package/modules/import-kit/src/door/waves/preparation.ts +0 -190
  91. package/modules/import-kit/src/door/waves/schema.ts +0 -73
  92. package/modules/import-kit/src/door/waves/store.ts +0 -112
  93. package/modules/import-kit/src/door/waves/types.ts +0 -76
@@ -15,6 +15,7 @@ import { custodyGuards, validatedProvenance, inventoryGuards, reusedSourceGuards
15
15
  import { supersedeTarget } from './history.ts';
16
16
  import { readOriginals } from './originals.ts';
17
17
  import { changed, putRow, updateRows } from './expected-state.ts';
18
+ import { applyReads, readEnd, targetReadKey, type ApplyReads } from './apply-reads.ts';
18
19
 
19
20
  export async function applyD1ImportBatches(jobOrId: ImportJob | string, context: D1ImportContext): Promise<ImportJob> {
20
21
  context = requestContext(context);
@@ -35,16 +36,29 @@ export async function applyD1ImportBatches(jobOrId: ImportJob | string, context:
35
36
  let effects = 0, scope = nativeStagingScope(job, plan);
36
37
  let prefix = atomicPrefix(context, 'apply', statements, scope);
37
38
  let planned = prefix ? prefix.count(progress(job, 0), returnReads) : cost(context, publication(job, 0, statements, scope), returnReads);
38
- for (const entry of plan.entries.slice(job.checkpoint, job.checkpoint + batchSize)) {
39
+ const pending = plan.entries.slice(job.checkpoint, job.checkpoint + batchSize);
40
+ let reads: ApplyReads | undefined, covered = 0, bulk = true;
41
+ for (let index = 0; index < pending.length; index++) {
42
+ const entry = pending[index];
39
43
  if (entries.length && effects + entry.effects.length > 1000) break;
40
44
  if (entry.effects.length > 1000) throw new Error('SOURCE_EFFECT_LIMIT');
41
45
  // Stop before an entry's own reads could exhaust the request. The exact
42
46
  // request, as atomic will send it, is counted again once the entry is built.
43
47
  const reserve = 40 + entry.effects.length * 16;
44
- if (remaining(context) - planned < reserve) { if (!entries.length) { if (count > 0) return job; throw new D1RequestLimitError(); } break; }
48
+ let starved = remaining(context) - planned < reserve;
49
+ if (!starved && index >= covered) {
50
+ // Read only the next entries that can still publish; a budget exhaustion
51
+ // inside the bulk read stops the gather exactly like the reserve above.
52
+ try {
53
+ const end: number = bulk ? readEnd(pending, index, effects, remaining(context) - planned, reads) : pending.length;
54
+ reads = bulk ? await applyReads(pending.slice(index, end), context, reads) : undefined;
55
+ bulk = !!reads; covered = end;
56
+ } catch (error) { if (!(error instanceof D1RequestLimitError)) throw error; starved = true; }
57
+ }
58
+ if (starved) { if (!entries.length) { if (count > 0) return job; throw new D1RequestLimitError(); } break; }
45
59
  const candidate: D1Sql[] = [], nextScope = { ...scope, nativeGuards:[...scope.nativeGuards], postGuards:[...scope.postGuards], targets:[...scope.targets] };
46
60
  const candidateUndo: D1UndoEffect[] | undefined = undoEffects && [];
47
- await publishEntry(id, entry, job.checkpoint + entries.length, context, candidate, nextScope, candidateUndo);
61
+ await publishEntry(id, entry, job.checkpoint + entries.length, context, candidate, nextScope, candidateUndo, reads);
48
62
  const nextPrefix = prefix?.extend(candidate);
49
63
  const next = nextPrefix ? undefined : publication(job, entries.length + 1, [...statements, ...candidate], nextScope);
50
64
  const spent = nextPrefix ? nextPrefix.count(progress(job, entries.length + 1), returnReads) : cost(context, next!, returnReads);
@@ -90,18 +104,19 @@ export async function applyD1ImportBatches(jobOrId: ImportJob | string, context:
90
104
  }
91
105
  return current.job;
92
106
  }
93
- async function publishEntry(id: string, entry: PlanEntry, ordinal: number, context: D1ImportContext, statements: D1Sql[], scope: NativeStagingScope, undoEffects?: D1UndoEffect[]): Promise<void> {
107
+ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, context: D1ImportContext, statements: D1Sql[], scope: NativeStagingScope, undoEffects?: D1UndoEffect[], reads?: ApplyReads): Promise<void> {
94
108
  const { database: db, targets } = context.storage, effectsHash = sha256(canonicalJson(entry.effects));
95
- const prior = await db.prepare('SELECT * FROM ik_sources WHERE identity=?').bind(entry.identity).first();
109
+ const prior = reads ? reads.sources.get(entry.identity) : await db.prepare('SELECT * FROM ik_sources WHERE identity=?').bind(entry.identity).first();
96
110
  if (prior) {
97
111
  if (prior.source_hash !== entry.source.payloadHash || prior.mapping_version !== context.mappingVersion || prior.effects_hash !== effectsHash) throw new Error('SOURCE_ID_CONTENT_CONFLICT');
98
112
  await reusedSourceGuards(entry, prior.job_id, context, statements);
99
113
  statements.push(guard(sql('EXISTS(SELECT 1 FROM ik_sources WHERE identity=? AND source_hash=? AND mapping_version=? AND effects_hash=? AND job_id=?)', entry.identity, prior.source_hash, prior.mapping_version, prior.effects_hash, prior.job_id)));
100
114
  for (const effect of entry.effects) {
101
- const raw = await db.prepare('SELECT * FROM ik_provenance WHERE source_identity=? AND destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(entry.identity, context.destination, effect.targetKind, effect.targetId).first();
115
+ const predecessor = reads?.predecessors.get(targetReadKey(effect.targetKind, effect.targetId));
116
+ const raw = reads ? (predecessor?.source_identity === entry.identity ? predecessor : null) : await db.prepare('SELECT * FROM ik_provenance WHERE source_identity=? AND destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(entry.identity, context.destination, effect.targetKind, effect.targetId).first();
102
117
  if (!raw) throw new Error('PROVENANCE_MISSING');
103
118
  const p = await validatedProvenance(raw, context, statements);
104
- const current = await readStagedTarget(context, scope, effect.targetKind, effect.targetId);
119
+ const current = await readTarget(context, scope, effect.targetKind, effect.targetId, reads);
105
120
  if (!current || current.revision !== p.appliedRevision || sha256(canonicalJson(current.value)) !== p.valueHash || attachmentHash(current.attachment) !== p.attachmentHash) throw new Error('NATIVE_EDIT_CONFLICT');
106
121
  const post = targetGuards(targets,current,context.destination,effect.targetKind,effect.targetId);
107
122
  scope.postGuards.push(...post);
@@ -113,7 +128,7 @@ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, conte
113
128
  statements.push(guard(sql('NOT EXISTS(SELECT 1 FROM ik_sources WHERE identity=?)', entry.identity)));
114
129
  for (const [index, effect] of entry.effects.entries()) {
115
130
  validateEffect(effect);
116
- const before = await readStagedTarget(context, scope, effect.targetKind, effect.targetId);
131
+ const before = await readTarget(context, scope, effect.targetKind, effect.targetId, reads);
117
132
  if (before ? effect.expectedRevision !== before.revision : effect.expectedRevision !== undefined) throw new Error('TARGET_REVISION_CONFLICT');
118
133
  const prospective = prospectiveEffect(id, entry, index, before, context.destination);
119
134
  const { expected: row, provenance: p } = prospective;
@@ -121,7 +136,7 @@ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, conte
121
136
  const post = targetGuards(targets, row, context.destination, effect.targetKind, effect.targetId);
122
137
  scope.postGuards.push(...post);
123
138
  undoEffects?.push(prospective);
124
- await supersedeTarget(p, before, context, statements);
139
+ await supersedeTarget(p, before, context, statements, reads ? reads.predecessors.get(targetReadKey(effect.targetKind, effect.targetId)) ?? null : undefined);
125
140
  const record_json=canonicalJson(p);
126
141
  statements.push(...targetGuards(targets,before ?? null, context.destination, effect.targetKind, effect.targetId), ...targets.write(row), ...post, changed(retainedSql(['INSERT INTO ik_provenance(id,job_id,ordinal,effect_ordinal,source_identity,destination,target_kind,target_id,record_json) VALUES(', ',', ',', ',', ',', ',', ',', ',', ',', ')'], [p.id, id, ordinal, index, entry.identity, context.destination, effect.targetKind, effect.targetId, record_json], [8]),putRow('ik_provenance',{id:p.id,job_id:id,ordinal,effect_ordinal:index,source_identity:entry.identity,destination:context.destination,target_kind:effect.targetKind,target_id:effect.targetId,reversed:0,record_json})));
127
142
  }
@@ -129,6 +144,14 @@ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, conte
129
144
  statements.push(changed(sql('INSERT INTO ik_sources(identity,source_hash,mapping_version,effects_hash,job_id) VALUES(?,?,?,?,?)', entry.identity, entry.source.payloadHash, context.mappingVersion, effectsHash, id),putRow('ik_sources',{identity:entry.identity,source_hash:entry.source.payloadHash,mapping_version:context.mappingVersion,effects_hash:effectsHash,job_id:id})));
130
145
  }
131
146
 
147
+ async function readTarget(context: D1ImportContext, scope: NativeStagingScope, kind: string, id: string, reads?: ApplyReads) {
148
+ if (!reads) return readStagedTarget(context, scope, kind, id);
149
+ const snapshot = reads.targets.get(targetReadKey(kind, id))!;
150
+ scope.nativeGuards.push(...(snapshot.guards ?? []).map(guard));
151
+ scope.targets.push({ kind, id, present: !!snapshot.row });
152
+ return snapshot.row;
153
+ }
154
+
132
155
  function entryWrite(job_id:string,ordinal:number,source_identity:string,owner_job_id:string,effects_hash:string):D1Sql{
133
156
  return changed(sql('INSERT INTO ik_d1_entries(job_id,ordinal,source_identity,owner_job_id,effects_hash) VALUES(?,?,?,?,?)',job_id,ordinal,source_identity,owner_job_id,effects_hash),putRow('ik_d1_entries',{job_id,ordinal,source_identity,owner_job_id,effects_hash}));
134
157
  }
@@ -10,9 +10,9 @@ export const provenanceColumns = 'id,job_id,ordinal,effect_ordinal,source_identi
10
10
 
11
11
  /** Transfer only a currently attributed exact native snapshot, explicitly reviewed
12
12
  * through expectedRevision. Original attribution remains unchanged in history. */
13
- export async function supersedeTarget(successor: D1ProvenanceRow, before: TargetRow | undefined, context: D1ImportContext, statements: D1Sql[]): Promise<void> {
13
+ export async function supersedeTarget(successor: D1ProvenanceRow, before: TargetRow | undefined, context: D1ImportContext, statements: D1Sql[], snapshot?: any): Promise<void> {
14
14
  const db = context.storage.database;
15
- const prior = await db.prepare('SELECT * FROM ik_provenance WHERE destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(successor.destination, successor.targetKind, successor.targetId).first();
15
+ const prior = snapshot === undefined ? await db.prepare('SELECT * FROM ik_provenance WHERE destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(successor.destination, successor.targetKind, successor.targetId).first() : snapshot;
16
16
  if (!prior) {
17
17
  statements.push(guard(sql('NOT EXISTS(SELECT 1 FROM ik_provenance WHERE destination=? AND target_kind=? AND target_id=? AND reversed=0)', successor.destination, successor.targetKind, successor.targetId)));
18
18
  return;
@@ -34,16 +34,16 @@ export interface LineageSever { target: string; select: (record: SourceRecord) =
34
34
  * step stays inside the limit and the erasure reaches its final step. With
35
35
  * `sever`, each step also severs the door lineage's copies in the jobs it reads. */
36
36
  export async function redactionSteps(fields: readonly D1FieldRedaction[], context: D1ImportContext, fence: D1Sql[], progress: RedactionProgress, pending: JobRevision[], failed?: Overflow, sever?: LineageSever): Promise<D1RetirementProgress> {
37
- let { span, page } = progress, unsized = failed;
37
+ let { span, page } = progress, unsized = failed, attempts = failed ? 1 : 0;
38
38
  for (;;) {
39
39
  let job = page && pending.find(item => item.id === page!.job);
40
40
  // A job changed since its last page starts again from its first entry; one no longer pending is done.
41
41
  if (page && (!job || job.revision !== page.revision)) page = job ? { ...page, from: 0, revision: job.revision } : undefined;
42
42
  if (unsized) {
43
- const next = smaller(span, page, pending, unsized.cost);
43
+ const next = smaller(span, page, pending, unsized.cost, attempts === 1);
44
44
  ({ span, page } = next); job = page && pending.find(item => item.id === page!.job);
45
45
  // Try it now only while this request has room for it; otherwise commit nothing and let the next request take it.
46
- if (unsized.cost === undefined || unsized.cost * next.fraction > remaining(context)) {
46
+ if (next.defer || unsized.cost === undefined || unsized.cost * next.fraction > remaining(context)) {
47
47
  return { phase: 'redacting', continuation: writeProgress({ ...progress, span, page }), jobsRedacted: 0, jobsPending: pending.length, fieldMatches: progress.matched };
48
48
  }
49
49
  }
@@ -53,6 +53,7 @@ export async function redactionSteps(fields: readonly D1FieldRedaction[], contex
53
53
  try { done = await redactionStep(fields, context, fence, progress, jobs, range, sever); }
54
54
  catch (error) {
55
55
  if (!(error instanceof D1RequestLimitError)) throw error;
56
+ attempts++;
56
57
  const needed = (error as { needed?: number }).needed;
57
58
  unsized = { cost: needed === undefined ? undefined : start - remaining(context) + needed };
58
59
  continue;
@@ -72,18 +73,27 @@ export async function redactionSteps(fields: readonly D1FieldRedaction[], contex
72
73
 
73
74
  /** The next, strictly smaller step after one cost `cost` statements: in proportion,
74
75
  * with headroom, or half when its reads alone ran out. Fewer jobs first, then
75
- * pages of one job's entries; `fraction` is its expected share of that cost. */
76
- function smaller(span: number, page: RedactionPage | undefined, pending: JobRevision[], cost: number | undefined): { span: number; page?: RedactionPage; fraction: number } {
76
+ * pages of one job's entries; `fraction` is its expected share of that cost.
77
+ * When one entry is already the step and that attempt was `first` in its request,
78
+ * nothing fits and the step is refused. A request that earlier attempts had spent
79
+ * defers the same step instead (`defer`): a fresh request has its whole budget. */
80
+ function smaller(span: number, page: RedactionPage | undefined, pending: JobRevision[], cost: number | undefined, first: boolean): { span: number; page?: RedactionPage; fraction: number; defer?: boolean } {
77
81
  const size = (current: number) => Math.min(current - 1, cost === undefined ? Math.floor(current / 2) : Math.floor(current * BUDGET * 0.7 / cost));
78
82
  if (page) {
79
83
  const count = size(page.count);
80
- if (count < 1) throw new D1RequestLimitError(); // One entry alone is more than a request.
84
+ if (count < 1) {
85
+ if (!first) return { span, page, fraction: 1, defer: true };
86
+ throw new D1RequestLimitError(); // One entry alone is more than a request.
87
+ }
81
88
  return { span, page: { ...page, count }, fraction: count / page.count };
82
89
  }
83
90
  const jobs = Math.min(span, pending.length);
84
91
  if (jobs > 1) { const fewer = Math.max(1, size(jobs)); return { span: fewer, fraction: fewer / jobs }; }
85
92
  const [job] = pending, count = size(job.total);
86
- if (count < 1) throw new D1RequestLimitError();
93
+ if (count < 1) {
94
+ if (!first) return { span, fraction: 1, defer: true };
95
+ throw new D1RequestLimitError();
96
+ }
87
97
  return { span: 1, page: { job: job.id, from: 0, count, revision: job.revision }, fraction: count / job.total };
88
98
  }
89
99
 
@@ -34,11 +34,22 @@ export interface D1TargetAdapter extends Partial<D1PreservedUndoAdapter> {
34
34
  /** Optional transport staging support. Guards must fence every native field
35
35
  * from before the first read, including same-revision edits and absence. */
36
36
  stagingRead?(db: D1Database, destination: string, kind: string, id: string): Promise<{ row: TargetRow | undefined; guards: D1Sql[] }>;
37
+ /** Optional selected-key equivalent of N stagingRead calls. Return exactly one
38
+ * {row, guards} per key in input order, repeating duplicate keys and preserving
39
+ * absence. Fence every native field from before its first read, including
40
+ * same-revision edits. Read only selected keys, never list the destination.
41
+ * Preserve overridden stagingRead/read behavior or throw for single-read fallback. */
42
+ stagingReadMany?(db: D1Database, destination: string, keys: readonly { kind: string; id: string }[]): Promise<{ row: TargetRow | undefined; guards: D1Sql[] }[]>;
37
43
  /** Protect exact post-state during cleanup, install a known invalidatable
38
44
  * receipt after exact checks, then release. Final scalar predicates run
39
45
  * read-only after every mutation and must bind the expected post-state. */
40
46
  stagingPublication?(stageId: string, destination: string, targets: { kind: string; id: string; present: boolean }[]): { protect: D1Sql[]; seal: D1Sql[]; release: D1Sql[]; final: D1Sql[] };
41
47
  read(db: D1Database, destination: string, kind: string, id: string): Promise<TargetRow | undefined>;
48
+ /** Optional selected-key equivalent of N read calls. Return exactly one result
49
+ * per key in input order, repeating duplicate keys and preserving undefined
50
+ * for absence. Read only selected keys, never list the destination. Preserve
51
+ * overridden read behavior or throw so the caller can fall back to singles. */
52
+ readMany?(db: D1Database, destination: string, keys: readonly { kind: string; id: string }[]): Promise<(TargetRow | undefined)[]>;
42
53
  matches(row: TargetRow | null, destination: string, kind: string, id: string): D1Sql;
43
54
  /** Optional exact row snapshot split into conjunctive predicates. All parts,
44
55
  * including absence/cardinality, commit in the same native transaction. */
@@ -484,40 +484,3 @@ 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,69 +552,3 @@ 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,8 +1,7 @@
1
- import {importDoorBoundedWavesCheck} from './waves/schema.ts';
2
1
  import type { ImportDoorOptions, ImportDoorRuntime, DoorRequest } from './types.ts';
3
2
  import { runtimeOptions } from './handler.ts';
4
3
  import { DoorError, verifyImportGrant, type DoorAction } from './grants.ts';
5
- import { authorized, doorFence, erasures, sql, transact } from './store.ts';
4
+ import { doorFence, erasures, sql, transact } from './store.ts';
6
5
  import { importDoorSchemaCheck } from './schema.ts';
7
6
 
8
7
  /** The storage key; grants are still verified against `claim.installationId`. */
@@ -22,7 +21,7 @@ async function source(options: ImportDoorOptions, namespace: string): Promise<Im
22
21
  if (!options.source) return runtimeOptions(options);
23
22
  const listed = await options.source(namespace);
24
23
  if (!listed) throw new DoorError('unknown_import_source');
25
- return runtimeOptions({...options, parse: listed.parse, mapping: listed.mapping, intake: listed.intake, package: listed.package, bounded: listed.bounded,
24
+ return runtimeOptions({...options, parse: listed.parse, mapping: listed.mapping, intake: listed.intake,
26
25
  format: listed.format ?? options.format, readRaw: 'readRaw' in listed ? listed.readRaw : undefined,
27
26
  ownerPrincipalId: listed.ownerPrincipalId ?? options.ownerPrincipalId});
28
27
  }
@@ -44,14 +43,5 @@ export async function admit(options: ImportDoorOptions, request: Request, action
44
43
  throw new DoorError('grant_replayed_revoked_or_expired');
45
44
  }
46
45
  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);
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;
46
+ return {options: runtime, access: {...grant, installationId: key}, authority, via:'grant', epoch, nonce: {jti: grant.jti, kid}};
57
47
  }
@@ -7,7 +7,6 @@ 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': [],
11
10
  uploads: [], plan: ['plan', 'read'], apply: ['create', 'apply', 'read'],
12
11
  status: ['read'], reconcile: ['read'], undo: ['read', 'undo', 'rollback'],
13
12
  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
- export function reasonCode(error: unknown): string {
17
+ 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', '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'];
12
+ const optionalKeys = ['fingerprint', 'approvalRecordId', 'approvedAt', 'catchup'];
13
+ const actions: readonly string[] = ['uploads', 'plan', 'apply', 'status', 'reconcile', 'undo', 'cancel', 'archive', 'restore-check'];
14
14
 
15
15
  function decode(segment: string): Uint8Array<ArrayBuffer> {
16
16
  if (!/^[A-Za-z0-9_-]+$/.test(segment)) fail();
@@ -80,28 +80,14 @@ 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' && !(value.bounded && ['prepare','apply-child'].includes(value.action as string))))) fail();
83
+ if (catchup && (value.catchup !== 'bounded' || (value.action !== 'plan' && value.action !== 'apply'))) fail();
84
84
  if (catchup && !approved(value)) fail();
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)) {
85
+ if (value.action === 'apply') {
100
86
  if (typeof value.fingerprint !== 'string' || !/^[0-9a-f]{64}$/.test(value.fingerprint) || !approved(value)) fail();
101
87
  } else {
102
88
  if (Object.hasOwn(value, 'fingerprint')) fail();
103
89
  const hasApproval = Object.hasOwn(value, 'approvalRecordId') || Object.hasOwn(value, 'approvedAt');
104
- if (hasApproval && (!['plan','prepare'].includes(value.action as string) || !catchup || !approved(value))) fail();
90
+ if (hasApproval && (value.action !== 'plan' || !catchup || !approved(value))) fail();
105
91
  }
106
92
  return Object.freeze(value) as unknown as Readonly<DoorGrant>;
107
93
  }
@@ -11,10 +11,7 @@ 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
- 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};
14
+ const routes = {uploads,plan,apply,status,reconcile,undo,cancel,archive,'restore-check':restoreCheck};
18
15
  const headers = {'Cache-Control':'no-store','Content-Type':'application/json','X-Content-Type-Options':'nosniff'};
19
16
  async function readInput(request: Request): Promise<Record<string,unknown>> {
20
17
  if (request.headers.get('Content-Type')?.split(';')[0] !== 'application/json' || !request.body) throw new DoorError('json_body_required',400);
@@ -49,12 +46,6 @@ export function runtimeOptions<T extends RuntimeConfiguration>(options: T): T &
49
46
  || (options.format === 'raw' ? typeof options.readRaw !== 'function' || options.intake !== undefined
50
47
  : typeof options.parse !== 'function')) throw new DoorError('invalid_configuration',503);
51
48
  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);
58
49
  return options as T & ImportDoorRuntime;
59
50
  }
60
51
  /** Server-side mount only. A null response belongs to another dispatcher. Every
@@ -87,8 +78,6 @@ export function createDoorHandler(prefix: string, database: D1Database,
87
78
  const input = await readInput(request);
88
79
  admitted = await review(admitted,input);
89
80
  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);
92
81
  const result = await routes[action](admitted,input);
93
82
  await authorized(admitted);
94
83
  return Response.json(result,{headers});
@@ -18,9 +18,3 @@ 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';
@@ -1,4 +1,3 @@
1
- import { packageUpload, readPackageSource } from './package.ts';
2
1
  import { readRawSource } from './raw.ts';
3
2
  import { randomUUID } from 'node:crypto';
4
3
  import { canonicalJson, sha256 } from '../provenance/identity.ts';
@@ -25,36 +24,26 @@ export function preview(snapshot: DoorSnapshot, created: boolean) {
25
24
  export async function plan(request: DoorRequest, input: Record<string, unknown>) {
26
25
  if (Object.keys(input).some(k=>!['uploadId','catchup'].includes(k)) || (input.catchup !== undefined && typeof input.catchup !== 'boolean')) throw new DoorError('invalid_plan_input',400);
27
26
  const upload = await uploadRow(request,text(input.uploadId,'upload_id'));
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
- }
27
+ try { return await planUpload(request,input,upload); }
33
28
  catch (error) {
34
29
  // A reader or bundle-check refusal answers a named 4xx, never a bare conflict.
35
30
  const refused = error instanceof DoorError ? error : sourceRefusal(error);
36
31
  // An upload holding a credential the reader cannot remove, or a value it
37
32
  // cannot inspect for one, never plans, so it is not kept.
38
33
  const unreadable = refused?.code === 'source_credential' || refused?.code === 'source_invalid_or_credential_source_data';
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;
34
+ if (unreadable) await forget(request,upload);
35
+ if (unreadable || refused?.code === 'source_erased') throw refused;
41
36
  // Whatever refuses this plan first (a pending import, interrupted staging, a
42
37
  // withdrawn snapshot, the catch-up shape, a bound or the source itself), an
43
38
  // upload holding an erased record is refused for that instead, and goes. A
44
39
  // failing screen leaves the original refusal standing. No refused plan keeps
45
40
  // credential values the reader removes: their uploaded bytes go.
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);
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);
54
43
  throw refused ?? error;
55
44
  }
56
45
  }
57
- async function planUpload(request: DoorRequest, input: Record<string, unknown>, upload: UploadRow, segments: UploadRow[]) {
46
+ async function planUpload(request: DoorRequest, input: Record<string, unknown>, upload: UploadRow) {
58
47
  const current = await lineage(request);
59
48
  // A plan whose apply failed, or whose import was undone or rolled back, has
60
49
  // left the lineage: the same bundle plans afresh instead of reusing it. A
@@ -78,7 +67,7 @@ async function planUpload(request: DoorRequest, input: Record<string, unknown>,
78
67
  if (snapshot.sourceCount > request.access.maxRecords) throw new DoorError('plan_exceeds_grant_quota');
79
68
  if (snapshot.plan.mappingVersion !== request.options.mapping.version) throw new DoorError('snapshot_mapping_changed_use_new_namespace',409);
80
69
  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');
81
- await dropCredentialBytes(request,snapshot,upload,segments);
70
+ await dropCredentialBytes(request,snapshot,upload);
82
71
  return preview(snapshot,false);
83
72
  }
84
73
  if (current.pending_id) throw new DoorError('namespace_has_pending_import',409);
@@ -96,7 +85,7 @@ async function planUpload(request: DoorRequest, input: Record<string, unknown>,
96
85
  if (!previous && (input.catchup === true || request.access.catchup === 'bounded')) throw new DoorError('catchup_requires_baseline',409);
97
86
  // A catch-up reads the destination's whole native set, which a large destination
98
87
  // refuses (413); an upload holding an erased record is refused and goes first.
99
- if (previous) await screen(request,upload,segments);
88
+ if (previous) await screen(request,upload);
100
89
  const planId = randomUUID(), options = request.options, {kitNamespace,source,retainedFrom} = await readUpload(request,upload,planId);
101
90
  const prior = new Map((previous?.records ?? []).map(r=>[recordKey(r),r]));
102
91
  const revisions = new Map<string,{revision:string;matches:boolean}>();
@@ -146,10 +135,10 @@ async function planUpload(request: DoorRequest, input: Record<string, unknown>,
146
135
  snapshot.records.push({...old,explanation:'Absent from source snapshot; retained in the seed. No deletion proposed.'});
147
136
  }
148
137
  if (request.access.catchup === 'bounded') assertBounded(snapshot,previous!);
149
- await refuseErased(request,upload,snapshot.plan,segments);
138
+ await refuseErased(request,upload,snapshot.plan);
150
139
  if (retired) await retire(request,retired,snapshot.plan);
151
140
  await saveSnapshot(request,snapshot,upload,retainedFrom);
152
- await dropCredentialBytes(request,snapshot,upload,segments);
141
+ await dropCredentialBytes(request,snapshot,upload);
153
142
  return preview(snapshot,true);
154
143
  }
155
144
  /** Retire an erasure-withdrawn live import as the namespace's baseline. The kit
@@ -171,24 +160,11 @@ async function retire(request: DoorRequest, head: DoorPlanRow, plan: ImportPlan)
171
160
  /** The reader removed credential values: the saved plan holds none and withholds
172
161
  * the original, so the uploaded bytes that still hold them go once it is saved.
173
162
  * The upload's header stays, so repeating this plan finds the saved one. */
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);
163
+ async function dropCredentialBytes(request: DoorRequest, snapshot: DoorSnapshot, upload: UploadRow): Promise<void> {
164
+ if (withheldCredentials(snapshot.plan.source)) await dropChunks(request,upload);
176
165
  }
177
166
  const withheldCredentials = (source: SourceBundle) => source.artifacts.some(a=>a.original?.status==='unavailable' && a.original.reason==='credentials-removed');
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));
167
+ const dropChunks = (request: DoorRequest, upload: UploadRow) => transact(request.options.database,request.authority,[sql('DELETE FROM ik_door_chunks WHERE upload_id=?',upload.id)]);
192
168
  /** Each plan has its own kit namespace, so a replan after a failed apply never
193
169
  * meets the failed job's kit namespace. */
194
170
  async function readUpload(request: DoorRequest, upload: UploadRow, planId: string) {
@@ -198,7 +174,6 @@ async function readUpload(request: DoorRequest, upload: UploadRow, planId: strin
198
174
  const retained = await retainedSource(request,upload);
199
175
  if (retained) return {kitNamespace,retainedFrom:retained.planId,source:{...retained.source,records:retained.source.records.map(record=>({...record,namespace:kitNamespace}))}};
200
176
  // 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};
202
177
  const bytes = await bundleBytes(request,upload);
203
178
  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)};
204
179
  const source = options.format === 'raw'
@@ -231,20 +206,20 @@ async function retainedSource(request: DoorRequest, upload: UploadRow): Promise<
231
206
  }
232
207
  /** The kit never stages an erased record, and neither does the door: an upload
233
208
  * holding one can never be planned, so it goes too. The epoch fences a later erasure. */
234
- export async function refuseErased(request: DoorRequest, upload: UploadRow, plan: ImportPlan, segments: readonly UploadRow[] = []): Promise<void> {
209
+ export async function refuseErased(request: DoorRequest, upload: UploadRow, plan: ImportPlan): Promise<void> {
235
210
  const db = request.options.database;
236
211
  for (const clear of erasedSourcePredicates(plan)) if ((await statement(db, sql(`SELECT ${clear.sql} AS clear`, ...clear.params ?? [])).first())?.clear !== 1) {
237
- await forget(request,upload,segments);
212
+ await forget(request, upload);
238
213
  throw new DoorError('source_erased', 409);
239
214
  }
240
215
  }
241
- export const forget = (request: DoorRequest, upload: UploadRow, segments: readonly UploadRow[] = []) =>
242
- transact(request.options.database,request.authority,cleanup(request,upload,segments,true));
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)]);
243
218
  /** The upload planned on its own with the plain mapping: one holding an erased
244
219
  * record is refused and goes. Answers whether the reader removed credential values. */
245
- async function screen(request: DoorRequest, upload: UploadRow, segments: readonly UploadRow[] = []): Promise<boolean> {
220
+ async function screen(request: DoorRequest, upload: UploadRow): Promise<boolean> {
246
221
  const id = randomUUID(), {kitNamespace,source} = await readUpload(request,upload,id), mapping = request.options.mapping;
247
- await refuseErased(request,upload,await planD1Import(source,mapping,kitContext(request,{id,kitNamespace,plan:{mappingVersion:mapping.version} as ImportPlan})),segments);
222
+ await refuseErased(request,upload,await planD1Import(source,mapping,kitContext(request,{id,kitNamespace,plan:{mappingVersion:mapping.version} as ImportPlan})));
248
223
  return withheldCredentials(source);
249
224
  }
250
225
  export function assertBounded(snapshot: DoorSnapshot, previous: DoorSnapshot): void {