@runeditrun/rer 0.2.77 → 0.2.78

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 (164) hide show
  1. package/README.md +32 -0
  2. package/cli/export.mjs +1 -1
  3. package/cli/portability-migrations.mjs +35 -14
  4. package/cli/portability-worker.mjs +2 -2
  5. package/cli/portability.mjs +2 -2
  6. package/global/CHANGELOG.md +8 -0
  7. package/global/README.md +32 -0
  8. package/modules/deploy-kit/owner-cli.mjs +15 -5
  9. package/modules/deploy-kit/provider.mjs +9 -4
  10. package/modules/import-kit/src/d1/README.md +1322 -0
  11. package/modules/import-kit/src/d1/apply.ts +115 -0
  12. package/modules/import-kit/src/d1/budget.ts +89 -0
  13. package/modules/import-kit/src/d1/compressed-plan.ts +63 -0
  14. package/modules/import-kit/src/d1/creation.ts +95 -0
  15. package/modules/import-kit/src/d1/custody-fence.ts +27 -0
  16. package/modules/import-kit/src/d1/custody-state-schema.ts +67 -0
  17. package/modules/import-kit/src/d1/database.ts +163 -0
  18. package/modules/import-kit/src/d1/entry-locator.ts +61 -0
  19. package/modules/import-kit/src/d1/erased-artifacts.ts +38 -0
  20. package/modules/import-kit/src/d1/erased-custody.ts +36 -0
  21. package/modules/import-kit/src/d1/erased-plan.ts +47 -0
  22. package/modules/import-kit/src/d1/erased-reversal.ts +19 -0
  23. package/modules/import-kit/src/d1/erasure-bounded.ts +22 -0
  24. package/modules/import-kit/src/d1/erasure-claims.ts +27 -0
  25. package/modules/import-kit/src/d1/erasure-compiler.ts +67 -0
  26. package/modules/import-kit/src/d1/erasure-disposition.ts +16 -0
  27. package/modules/import-kit/src/d1/erasure-fragments.ts +86 -0
  28. package/modules/import-kit/src/d1/erasure-index.ts +53 -0
  29. package/modules/import-kit/src/d1/erasure-pages.ts +62 -0
  30. package/modules/import-kit/src/d1/erasure-progress-schema.ts +8 -0
  31. package/modules/import-kit/src/d1/erasure-progress.ts +61 -0
  32. package/modules/import-kit/src/d1/erasure-related.ts +124 -0
  33. package/modules/import-kit/src/d1/erasure-report.ts +31 -0
  34. package/modules/import-kit/src/d1/erasure-schema.ts +24 -0
  35. package/modules/import-kit/src/d1/erasure-selection.ts +77 -0
  36. package/modules/import-kit/src/d1/erasure-snapshot.ts +15 -0
  37. package/modules/import-kit/src/d1/erasure-stage.ts +146 -0
  38. package/modules/import-kit/src/d1/erasure-step.ts +12 -0
  39. package/modules/import-kit/src/d1/erasure-superseded.ts +98 -0
  40. package/modules/import-kit/src/d1/erasure-values.ts +83 -0
  41. package/modules/import-kit/src/d1/erasure-writes.ts +46 -0
  42. package/modules/import-kit/src/d1/erasure.ts +170 -0
  43. package/modules/import-kit/src/d1/evolutions/0001-preserved-undo.ts +13 -0
  44. package/modules/import-kit/src/d1/expected-state.ts +91 -0
  45. package/modules/import-kit/src/d1/field-redaction.ts +86 -0
  46. package/modules/import-kit/src/d1/guards.ts +37 -0
  47. package/modules/import-kit/src/d1/history.ts +71 -0
  48. package/modules/import-kit/src/d1/index.ts +88 -0
  49. package/modules/import-kit/src/d1/integrity.ts +127 -0
  50. package/modules/import-kit/src/d1/inventory-snapshot.ts +84 -0
  51. package/modules/import-kit/src/d1/jobs.ts +97 -0
  52. package/modules/import-kit/src/d1/native-set.ts +48 -0
  53. package/modules/import-kit/src/d1/native-staging-admission.ts +19 -0
  54. package/modules/import-kit/src/d1/native-staging-context.ts +44 -0
  55. package/modules/import-kit/src/d1/native-staging-lifecycle.ts +33 -0
  56. package/modules/import-kit/src/d1/native-staging-offset.ts +74 -0
  57. package/modules/import-kit/src/d1/native-staging-publication.ts +18 -0
  58. package/modules/import-kit/src/d1/native-staging-rows.ts +72 -0
  59. package/modules/import-kit/src/d1/native-staging-schema.ts +28 -0
  60. package/modules/import-kit/src/d1/native-staging-state.ts +22 -0
  61. package/modules/import-kit/src/d1/native-staging.ts +64 -0
  62. package/modules/import-kit/src/d1/original-copies.ts +53 -0
  63. package/modules/import-kit/src/d1/original-storage.ts +56 -0
  64. package/modules/import-kit/src/d1/originals.ts +74 -0
  65. package/modules/import-kit/src/d1/plan-dictionary.ts +80 -0
  66. package/modules/import-kit/src/d1/plans.ts +121 -0
  67. package/modules/import-kit/src/d1/preparation-fingerprint.ts +57 -0
  68. package/modules/import-kit/src/d1/preparation-header.ts +33 -0
  69. package/modules/import-kit/src/d1/preparation-reader.ts +98 -0
  70. package/modules/import-kit/src/d1/preparation-schema.ts +16 -0
  71. package/modules/import-kit/src/d1/preparation-state.ts +56 -0
  72. package/modules/import-kit/src/d1/preparation-step-combine.ts +80 -0
  73. package/modules/import-kit/src/d1/preparation-step-control.ts +48 -0
  74. package/modules/import-kit/src/d1/preparation-step-hash.ts +131 -0
  75. package/modules/import-kit/src/d1/preparation-step-pages.ts +77 -0
  76. package/modules/import-kit/src/d1/preparation-steps-schema.ts +11 -0
  77. package/modules/import-kit/src/d1/preparation-steps.ts +102 -0
  78. package/modules/import-kit/src/d1/preparations.ts +87 -0
  79. package/modules/import-kit/src/d1/preserved-preview.ts +89 -0
  80. package/modules/import-kit/src/d1/preserved-reverse.ts +91 -0
  81. package/modules/import-kit/src/d1/provenance-set.ts +87 -0
  82. package/modules/import-kit/src/d1/publication-fence.ts +22 -0
  83. package/modules/import-kit/src/d1/publication-receipt-schema.ts +15 -0
  84. package/modules/import-kit/src/d1/published-stream.ts +11 -0
  85. package/modules/import-kit/src/d1/read-fence.ts +64 -0
  86. package/modules/import-kit/src/d1/read-results.ts +16 -0
  87. package/modules/import-kit/src/d1/redacted-custody.ts +76 -0
  88. package/modules/import-kit/src/d1/redaction-step.ts +125 -0
  89. package/modules/import-kit/src/d1/referenced-plans.ts +136 -0
  90. package/modules/import-kit/src/d1/retained-values.ts +35 -0
  91. package/modules/import-kit/src/d1/retirement-bounded.ts +71 -0
  92. package/modules/import-kit/src/d1/retirement-door.ts +105 -0
  93. package/modules/import-kit/src/d1/retirement-schema.ts +39 -0
  94. package/modules/import-kit/src/d1/retirement-scope.ts +94 -0
  95. package/modules/import-kit/src/d1/retirement-steps.ts +76 -0
  96. package/modules/import-kit/src/d1/retirement-tally.ts +74 -0
  97. package/modules/import-kit/src/d1/retirement.ts +136 -0
  98. package/modules/import-kit/src/d1/reversal-conversion-record.ts +19 -0
  99. package/modules/import-kit/src/d1/reversal-conversion-schema.ts +11 -0
  100. package/modules/import-kit/src/d1/reversal-conversion.ts +40 -0
  101. package/modules/import-kit/src/d1/reversal-fence-schema.ts +14 -0
  102. package/modules/import-kit/src/d1/reversal-fence.ts +106 -0
  103. package/modules/import-kit/src/d1/reversal-journal.ts +24 -0
  104. package/modules/import-kit/src/d1/reverse.ts +103 -0
  105. package/modules/import-kit/src/d1/reviewed-source.ts +23 -0
  106. package/modules/import-kit/src/d1/sets.ts +78 -0
  107. package/modules/import-kit/src/d1/staged-creation.ts +201 -0
  108. package/modules/import-kit/src/d1/staging-admission.ts +45 -0
  109. package/modules/import-kit/src/d1/staging-control.ts +43 -0
  110. package/modules/import-kit/src/d1/staging-erasure.ts +28 -0
  111. package/modules/import-kit/src/d1/staging-lifecycle.ts +49 -0
  112. package/modules/import-kit/src/d1/staging-plan.ts +15 -0
  113. package/modules/import-kit/src/d1/staging-privacy.ts +22 -0
  114. package/modules/import-kit/src/d1/staging-publication.ts +79 -0
  115. package/modules/import-kit/src/d1/staging-schema.ts +37 -0
  116. package/modules/import-kit/src/d1/staging-statements.ts +8 -0
  117. package/modules/import-kit/src/d1/staging-transport.ts +70 -0
  118. package/modules/import-kit/src/d1/stream-boundary.ts +11 -0
  119. package/modules/import-kit/src/d1/stream-create.ts +55 -0
  120. package/modules/import-kit/src/d1/stream-identity-admission.ts +37 -0
  121. package/modules/import-kit/src/d1/stream-input.ts +18 -0
  122. package/modules/import-kit/src/d1/stream-layout.ts +43 -0
  123. package/modules/import-kit/src/d1/stream-origin.ts +44 -0
  124. package/modules/import-kit/src/d1/stream-page-integrity.ts +18 -0
  125. package/modules/import-kit/src/d1/stream-partition.ts +50 -0
  126. package/modules/import-kit/src/d1/stream-plan.ts +133 -0
  127. package/modules/import-kit/src/d1/stream-receipt.ts +45 -0
  128. package/modules/import-kit/src/d1/stream-revision-schema.ts +35 -0
  129. package/modules/import-kit/src/d1/stream-snapshot.ts +26 -0
  130. package/modules/import-kit/src/d1/stream-stage.ts +79 -0
  131. package/modules/import-kit/src/d1/stream-state.ts +25 -0
  132. package/modules/import-kit/src/d1/stream-working-set.ts +30 -0
  133. package/modules/import-kit/src/d1/streams.ts +39 -0
  134. package/modules/import-kit/src/d1/target-guards.ts +9 -0
  135. package/modules/import-kit/src/d1/target-identities.ts +17 -0
  136. package/modules/import-kit/src/d1/transport.ts +10 -0
  137. package/modules/import-kit/src/d1/types.ts +162 -0
  138. package/modules/import-kit/src/d1/undo-observations.ts +39 -0
  139. package/modules/import-kit/src/d1/undo-report.ts +22 -0
  140. package/modules/import-kit/src/d1/undo-snapshot.ts +87 -0
  141. package/modules/import-kit/src/d1/undo-state.ts +84 -0
  142. package/modules/import-kit/src/d1/undo-types.ts +110 -0
  143. package/modules/import-kit/src/journal/authority.ts +12 -0
  144. package/modules/import-kit/src/plan/budget.ts +60 -0
  145. package/modules/import-kit/src/plan/index.ts +123 -0
  146. package/modules/import-kit/src/plan/size.ts +122 -0
  147. package/modules/import-kit/src/provenance/canonical-hash.ts +130 -0
  148. package/modules/import-kit/src/provenance/canonical-stream.ts +85 -0
  149. package/modules/import-kit/src/provenance/credential-names.ts +131 -0
  150. package/modules/import-kit/src/provenance/credentials.ts +317 -0
  151. package/modules/import-kit/src/provenance/identity.ts +51 -0
  152. package/modules/import-kit/src/provenance/json-snapshot.ts +21 -0
  153. package/modules/import-kit/src/provenance/resumable-sha256.ts +75 -0
  154. package/modules/import-kit/src/provenance/validate-json.ts +19 -0
  155. package/modules/import-kit/src/storage/originals.ts +87 -0
  156. package/modules/import-kit/src/storage/provenance.ts +64 -0
  157. package/modules/import-kit/src/storage/schema.ts +42 -0
  158. package/modules/import-kit/src/types.ts +47 -0
  159. package/modules/portability/README.md +98 -2
  160. package/modules/portability/backfills.mjs +42 -15
  161. package/modules/portability/ledgers.mjs +7 -1
  162. package/modules/portability/transient-tables.mjs +40 -0
  163. package/package.json +3 -1
  164. package/scripts/publish-check/reviewed.py +3 -1
@@ -0,0 +1,35 @@
1
+ /** Parent identity is retained even after parent/child deletion. Revisions never
2
+ * reset, so deleting and reconstructing the same IDs cannot recreate an earlier
3
+ * receipt. Only immutable layout changes advance layout_revision; child progress
4
+ * advances progress_revision and remains legal under a layout-only child fence. */
5
+ export const streamParentFields = ['destination','namespace','mapping_version','principal_id','fingerprint','operation_key','total','layout_hash'];
6
+ export const streamAnchorMatch = (s: string, r: string) => streamParentFields.map(field => `${s}.${field} IS ${r}.${field}`).join(' AND ');
7
+ const fields = streamParentFields.join(',');
8
+ const remember = (ref: string) => `INSERT INTO ik_d1_stream_revisions(stream_id,${fields}) SELECT ${ref}.id,${streamParentFields.map(field=>`${ref}.${field}`).join(',')} WHERE NOT EXISTS(SELECT 1 FROM ik_d1_stream_revisions WHERE stream_id=${ref}.id); UPDATE ik_d1_stream_revisions SET layout_revision=layout_revision+1,progress_revision=progress_revision+1 WHERE stream_id=${ref}.id;`;
9
+ const bump = (where: string, layout = true) => `UPDATE ik_d1_stream_revisions SET ${layout?'layout_revision=layout_revision+1,':''}progress_revision=progress_revision+1 WHERE ${where};`;
10
+ const member = (refs: string[]) => `stream_id IN (SELECT stream_id FROM ik_d1_stream_pages WHERE ${refs.map(ref=>`job_id=${ref}.id`).join(' OR ')} UNION SELECT stream_id FROM ik_d1_stream_origins WHERE ${refs.map(ref=>`job_id=${ref}.id`).join(' OR ')})`;
11
+ export const d1StreamRevisionSchema = [
12
+ `CREATE TABLE IF NOT EXISTS ik_d1_stream_revisions(stream_id TEXT PRIMARY KEY,${streamParentFields.map(field=>`${field} ${field==='total'?'INTEGER':'TEXT'} NOT NULL`).join(',')},layout_revision INTEGER NOT NULL DEFAULT 0 CHECK(typeof(layout_revision)='integer' AND layout_revision>=0),progress_revision INTEGER NOT NULL DEFAULT 0 CHECK(typeof(progress_revision)='integer' AND progress_revision>=0))`,
13
+ // This backfill runs only at schema installation; it does not bless a changed
14
+ // layout on later reads or reinitialization. Origins and canonical hashes must
15
+ // still validate before any public result or child effect.
16
+ `INSERT INTO ik_d1_stream_revisions(stream_id,${fields}) SELECT id,${fields} FROM ik_d1_streams WHERE NOT EXISTS(SELECT 1 FROM ik_d1_stream_revisions WHERE stream_id=ik_d1_streams.id)`,
17
+ "CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_insert BEFORE INSERT ON ik_d1_stream_revisions WHEN EXISTS(SELECT 1 FROM ik_d1_stream_revisions WHERE stream_id=NEW.stream_id) BEGIN SELECT RAISE(ABORT,'IMPORT_STREAM_REVISION_CHANGED'); END",
18
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_update BEFORE UPDATE ON ik_d1_stream_revisions WHEN NEW.stream_id IS NOT OLD.stream_id OR ${streamParentFields.map(field=>`NEW.${field} IS NOT OLD.${field}`).join(' OR ')} OR NEW.layout_revision<OLD.layout_revision OR NEW.progress_revision<=OLD.progress_revision BEGIN SELECT RAISE(ABORT,'IMPORT_STREAM_REVISION_CHANGED'); END`,
19
+ "CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_delete BEFORE DELETE ON ik_d1_stream_revisions BEGIN SELECT RAISE(ABORT,'IMPORT_STREAM_REVISION_CHANGED'); END",
20
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_parent_insert AFTER INSERT ON ik_d1_streams BEGIN ${remember('NEW')} END`,
21
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_parent_delete AFTER DELETE ON ik_d1_streams BEGIN ${bump('stream_id=OLD.id')} END`,
22
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_parent_update AFTER UPDATE ON ik_d1_streams BEGIN ${bump('stream_id=OLD.id OR stream_id=NEW.id',false)} END`,
23
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_parent_layout AFTER UPDATE OF id,${fields} ON ik_d1_streams BEGIN ${bump('stream_id=OLD.id OR stream_id=NEW.id')} END`,
24
+ ...['ik_d1_stream_pages','ik_d1_stream_origins'].flatMap(table=>['INSERT','UPDATE','DELETE'].map(event=> {
25
+ const refs = event==='INSERT'?'stream_id=NEW.stream_id':event==='DELETE'?'stream_id=OLD.stream_id':'stream_id=OLD.stream_id OR stream_id=NEW.stream_id';
26
+ const columns = table==='ik_d1_stream_pages' && event==='UPDATE'?' OF stream_id,ordinal,job_id,total':'';
27
+ return `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_${table}_${event.toLowerCase()} AFTER ${event}${columns} ON ${table} BEGIN ${bump(refs)} END`;
28
+ })),
29
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_page_erasure AFTER UPDATE OF erased_records ON ik_d1_stream_pages BEGIN ${bump('stream_id=OLD.stream_id OR stream_id=NEW.stream_id',false)} END`,
30
+ ...['INSERT','UPDATE','DELETE'].map(event=> {
31
+ const refs = event==='INSERT'?['NEW']:event==='DELETE'?['OLD']:['OLD','NEW'];
32
+ return `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_job_${event.toLowerCase()} AFTER ${event} ON ik_jobs BEGIN ${bump(member(refs),event!=='UPDATE')} END`;
33
+ }),
34
+ `CREATE TRIGGER IF NOT EXISTS ik_d1_stream_revision_job_layout AFTER UPDATE OF id,destination,namespace,mapping_version,principal_id,fingerprint,operation_key,total,reversal_policy,reversal_policy_hash ON ik_jobs BEGIN ${bump(member(['OLD','NEW']))} END`,
35
+ ];
@@ -0,0 +1,26 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { canonicalJson } from '../provenance/identity.ts';
3
+ import type { ImportJob } from '../types.ts';
4
+ import type { D1ImportContext } from './types.ts';
5
+
6
+ /** Feed canonical tuples individually: no second whole-layout JSON string. */
7
+ export function layoutHash(rows: any[]): string {
8
+ const hash = createHash('sha256'); hash.update('[');
9
+ for (let index = 0; index < rows.length; index++) {
10
+ const row = rows[index]; if (index) hash.update(',');
11
+ hash.update(canonicalJson({ ordinal: row.ordinal, job_id: row.job_id, total: row.total }));
12
+ }
13
+ hash.update(']'); return hash.digest('hex');
14
+ }
15
+ const natural = (value: unknown): value is number => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
16
+ /** Interpret only the three rowsets from the same checked SQL transaction. */
17
+ export function streamJob(row: any, pages: any[], jobs: any[], context: D1ImportContext): ImportJob {
18
+ if (!row) throw new Error('IMPORT_JOB_NOT_FOUND');
19
+ if (row.destination !== context.destination || row.namespace !== context.namespace || row.mapping_version !== context.mappingVersion || row.principal_id !== context.authorization.principalId) throw new Error('JOB_BINDING_MISMATCH');
20
+ if (!['applying','undoing'].includes(row.state) || !natural(row.total) || !natural(row.layout_revision) || !natural(row.progress_revision)) throw new Error('IMPORT_STREAM_LAYOUT_CHANGED');
21
+ if (!pages.length || layoutHash(pages) !== row.layout_hash || pages.some((p, i) => p.stream_id !== row.id || p.ordinal !== i || !natural(p.total) || !natural(p.erased_records) || p.erased_records > p.total) || pages.reduce((n, p) => n + p.total, 0) !== row.total) throw new Error('IMPORT_STREAM_LAYOUT_CHANGED');
22
+ if (jobs.length !== pages.length || jobs.some((j, i) => j.id !== pages[i].job_id || j.total !== pages[i].total || j.destination !== row.destination || j.namespace !== row.namespace || j.mapping_version !== row.mapping_version || j.principal_id !== row.principal_id || !natural(j.checkpoint) || j.checkpoint > j.total || !natural(j.fence) || !['queued','applying','completed','failed','undoing','undone','rolling_back','rolled_back'].includes(j.state))) throw new Error('IMPORT_STREAM_LAYOUT_CHANGED');
23
+ if (context.reversalPolicy !== undefined && jobs.some(j => j.reversal_policy !== context.reversalPolicy)) throw new Error('REVERSAL_POLICY_CONFLICT');
24
+ const state = row.state === 'undoing' ? jobs.every(j => ['undone','rolled_back'].includes(j.state)) ? 'undone' : 'undoing' : jobs.every(j => j.state === 'completed') ? 'completed' : 'applying';
25
+ return { id:row.id, destination:row.destination, namespace:row.namespace, mappingVersion:row.mapping_version, principalId:row.principal_id, fingerprint:row.fingerprint, total:row.total, checkpoint:jobs.reduce((n, j) => n+j.checkpoint,0), fence:jobs.reduce((n, j) => n+j.fence,0), state, error:jobs.find(j => j.error)?.error ?? null, erasedRecords:pages.reduce((n,p)=>n+p.erased_records,0) };
26
+ }
@@ -0,0 +1,79 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { Buffer } from 'node:buffer';
3
+ import type { D1ImportContext } from './types.ts';
4
+ import { canonicalHash } from '../provenance/canonical-stream.ts';
5
+ import { canonicalJson, sha256, requireText } from '../provenance/identity.ts';
6
+ import { originalsFor } from '../storage/originals.ts';
7
+ import { planIndex } from './erasure-index.ts';
8
+ import { expandCompressedPlan, type UnverifiedStreamPages } from './stream-plan.ts';
9
+ import { assertStreamPage } from './stream-page-integrity.ts';
10
+ import { layoutHash } from './stream-state.ts';
11
+ import { stagingAtomic, stagingChunks, StageWriter, type StageBinding } from './staging-transport.ts';
12
+ import { reserveStaging } from './staging-admission.ts';
13
+ import { stageRevisionWrites } from './staging-control.ts';
14
+ import { beginStage } from './staging-lifecycle.ts';
15
+ import { policy, policyHash } from './undo-state.ts';
16
+ import { stagingPlan } from './staging-plan.ts';
17
+ import { admitStreamIdentities } from './stream-identity-admission.ts';
18
+
19
+ /** Shared private writes; callers own exact generation cleanup and publication. */
20
+ export async function stageStreamPages(stream: UnverifiedStreamPages, fingerprint: string, context: D1ImportContext, own: (stage: StageBinding) => void, beforeSeal?: (stage: StageBinding, revision: number) => Promise<number>, assertInput?: () => void) {
21
+ const h = stream.header;
22
+ const originals = originalsFor({ ...h, source: { ...h.source, records: [] }, entries: [], fingerprint: fingerprint }, '');
23
+ const operation = context.operationKey ?? fingerprint;
24
+ requireText(operation, 'operationKey');
25
+ const id = randomUUID(), pages = stream.pages.map((_, ordinal) => ({ stream_id: id, ordinal, job_id: ordinal === 0 ? id : randomUUID(), total: stream.lengths[ordinal] }));
26
+ const stage: StageBinding = { id, generation: randomUUID(), destination: h.destination, namespace: h.namespace, mapping_version: h.mappingVersion, principal_id: h.principalId, fingerprint: fingerprint, operation_key: operation, total: h.counts.unique, pages: pages.length, layout_hash: layoutHash(pages), reversal_policy: policy(context) };
27
+ const originalRows = originals.map(o => [o.key, o.name, o.sha256, o.size]);
28
+ const reservation = reserveStaging({ ...stream, fingerprint }, originalRows, context, stage, pages);
29
+ await admitStreamIdentities(stream, context, reservation);
30
+ assertInput?.();
31
+ own(stage);
32
+ await beginStage(stage, context, !!beforeSeal, assertInput);
33
+ let revision = await registerPages(stream, pages, stage, context);
34
+ await stagingAtomic(context, stageRevisionWrites(stage, revision, false));
35
+ const chunks = new StageWriter(context, stage, 'chunks', ['page_ordinal','ordinal','body','body_hash','body_size']); chunks.revision = revision;
36
+ for (const page of pages) {
37
+ let ordinal = 0;
38
+ for (const body of stagingChunks(stagingPlan(stream.pages[page.ordinal], context.storage.targets.planCodec).body)) await chunks.append([page.ordinal, ordinal++, body, sha256(body), Buffer.byteLength(body)]);
39
+ }
40
+ await chunks.flush(); revision = chunks.revision;
41
+ const artifacts = new StageWriter(context, stage, 'originals', ['original_key','name','sha256','size']); artifacts.revision = revision;
42
+ for (const row of originalRows) await artifacts.append(row);
43
+ await artifacts.flush(); revision = artifacts.revision;
44
+ if (beforeSeal) revision = await beforeSeal(stage, revision);
45
+ await stagingAtomic(context, stageRevisionWrites(stage, revision, true));
46
+ return { stage, pages, revision };
47
+ }
48
+
49
+ async function registerPages(stream: UnverifiedStreamPages, pages: { ordinal: number; job_id: string; total: number }[], stage: StageBinding, context: D1ImportContext): Promise<number> {
50
+ const index = new StageWriter(context, stage, 'index', ['page_ordinal','ordinal','source_identity','target_identity']);
51
+ const metadata: unknown[][] = [];
52
+ const counts = { input: 0, unique: 0, duplicates: 0, mapped: 0, unmapped: 0, rejected: 0, effects: 0, byKind: Object.create(null) as Record<string, number> };
53
+ for (const page of pages) {
54
+ const originalBody = stream.pages[page.ordinal], plan = expandCompressedPlan(originalBody, context.storage.targets.planCodec);
55
+ const { body, inline } = stagingPlan(originalBody, context.storage.targets.planCodec);
56
+ assertStreamPage(stream, plan);
57
+ for (const key of ['input','unique','duplicates','mapped','unmapped','rejected','effects'] as const) counts[key] += plan.counts[key];
58
+ for (const [kind, count] of Object.entries(plan.counts.byKind)) counts.byKind[kind] = (counts.byKind[kind] ?? 0) + count;
59
+ if (plan.entries.some(entry => entry.effects.some(effect => effect.attachment)) || plan.source.artifacts.some(a => a.original?.status === 'available')) throw new Error('IMPORT_STREAM_NORMALIZED_SOURCE_REQUIRED');
60
+ if (plan.entries.length !== page.total) throw new Error('IMPORT_STREAM_PAGE_CHANGED');
61
+ const identities = planIndex(plan, page.job_id).map(row => [page.ordinal, row.ordinal, row.source_identity, row.target_identity]);
62
+ for (const row of identities) {
63
+ if (!/^[a-f0-9]{64}$/.test(String(row[2])) || row[3] !== '' && !/^[a-f0-9]{64}$/.test(String(row[3])) || Buffer.byteLength(JSON.stringify(row)) + 1 > 200) throw new Error('IMPORT_STREAM_PAGE_CHANGED');
64
+ await index.append(row);
65
+ }
66
+ let chunks = 0;
67
+ for (const _ of stagingChunks(body)) chunks++;
68
+ const hash = sha256(body), size = Buffer.byteLength(body);
69
+ const row = [page.ordinal, page.job_id, page.total, plan.fingerprint, canonicalJson({ d1Plan: 1, chunks, size, sha256: hash }), hash, size, chunks, identities.length, canonicalHash(identities), policyHash(page.job_id, plan.fingerprint, stage.reversal_policy), inline];
70
+ if (Buffer.byteLength(JSON.stringify(row)) + 1 > 1024) throw new Error('IMPORT_STAGE_TRANSPORT_LIMIT');
71
+ metadata.push(row);
72
+ }
73
+ if (canonicalHash(counts) !== canonicalHash(stream.header.counts)) throw new Error('IMPORT_STREAM_COUNTS_CHANGED');
74
+ await index.flush();
75
+ const records = new StageWriter(context, stage, 'pages', ['ordinal','job_id','total','fingerprint','plan_json','body_hash','body_size','chunks','identities','index_hash','policy_hash','inline_plan']); records.revision = index.revision;
76
+ for (const row of metadata) await records.append(row);
77
+ await records.flush();
78
+ return records.revision;
79
+ }
@@ -0,0 +1,25 @@
1
+ import type { ImportAction } from '../types.ts';
2
+ import type { D1ImportContext } from './types.ts';
3
+ import { sql, guard } from './database.ts';
4
+ import { streamSnapshotPredicate, rememberStreamContext } from './stream-layout.ts';
5
+ export { streamSnapshotPredicate } from './stream-layout.ts';
6
+ import { readStreamReceipt, streamReceipt } from './stream-receipt.ts';
7
+ export { layoutHash, streamJob } from './stream-snapshot.ts';
8
+
9
+ export async function streamState(id: string, context: D1ImportContext, action: ImportAction = 'read') {
10
+ const current = await readStreamReceipt(context, id, action);
11
+ const predicate = streamSnapshotPredicate(current.row);
12
+ const childContext: D1ImportContext = { ...context, reviewedFingerprint: undefined,
13
+ authorization: { ...context.authorization, predicate(childAction, destination) {
14
+ const outer = context.authorization.predicate(action, destination);
15
+ const child = childAction === action ? undefined : context.authorization.predicate(childAction, destination);
16
+ return sql(`(${outer.sql})${child?` AND (${child.sql})`:''} AND (${predicate.sql})`, ...outer.params??[], ...child?.params??[], ...predicate.params!);
17
+ } } };
18
+ rememberStreamContext(childContext, current.row.id, predicate);
19
+ return { ...current, guards: [guard(streamSnapshotPredicate(current.row, true))], childContext };
20
+ }
21
+ /** Count exactly the action-specific builder that will run after the child.
22
+ * It performs transport admission now, before native or private publication. */
23
+ export function streamReturnQueries(current: Pick<Awaited<ReturnType<typeof streamState>>, 'row'>, context: D1ImportContext, action: ImportAction = 'read'): number {
24
+ return streamReceipt(context, current.row.id, action).statements.length;
25
+ }
@@ -0,0 +1,30 @@
1
+ import type { ImportPlan } from '../types.ts';
2
+ import { canonicalSize } from '../provenance/canonical-stream.ts';
3
+ import { planFingerprint } from '../plan/index.ts';
4
+ import { dictionaryWeight } from './stream-partition.ts';
5
+
6
+ /** Long repeated values can make independent small dictionaries dominate total
7
+ * custody. Coalesce only those normalized pages, within a fixed working set.
8
+ * Ordinary imports retain their expanded-byte and 100-record boundaries. */
9
+ export function measurePlanWorkingSet(plan: ImportPlan): { bytes: number; expandedBytes: number; amplified: boolean } {
10
+ const expandedBytes=canonicalSize(plan),bytes=dictionaryWeight(plan);
11
+ return {bytes,expandedBytes,amplified:expandedBytes>4*bytes};
12
+ }
13
+ export function amplifiedPlan(plan: ImportPlan): boolean {
14
+ return measurePlanWorkingSet(plan).amplified;
15
+ }
16
+ export function mergePlanPages(pages: ImportPlan[]): ImportPlan {
17
+ if (pages.length === 1) return pages[0];
18
+ const first = pages[0], entries = pages.flatMap(page => page.entries);
19
+ const counts = { input: 0, unique: 0, duplicates: 0, mapped: 0, unmapped: 0, rejected: 0, effects: 0, byKind: Object.create(null) as Record<string, number> };
20
+ const diagnostics = [...first.diagnostics];
21
+ const artifactWarnings = first.source.artifacts.filter(artifact => artifact.original?.status === 'unavailable').length;
22
+ for (const [index, page] of pages.entries()) {
23
+ for (const key of ['input','unique','duplicates','mapped','unmapped','rejected','effects'] as const) counts[key] += page.counts[key];
24
+ for (const [kind, count] of Object.entries(page.counts.byKind)) counts.byKind[kind] = (counts.byKind[kind] ?? 0) + count;
25
+ if (index) diagnostics.push(...page.diagnostics.slice(artifactWarnings));
26
+ }
27
+ const plan = { ...first, counts, diagnostics, entries, source: { ...first.source, records: entries.map(entry => entry.source) } };
28
+ plan.fingerprint = planFingerprint(plan);
29
+ return plan;
30
+ }
@@ -0,0 +1,39 @@
1
+ import type { D1ImportContext } from './types.ts';
2
+ import { requestContext, reserveReturn } from './budget.ts';
3
+ import { atomic, guard, sql } from './database.ts';
4
+ import { streamState, streamReturnQueries, streamSnapshotPredicate } from './stream-state.ts';
5
+ import { applyD1ImportBatches } from './apply.ts';
6
+ import { reverseD1Import } from './reverse.ts';
7
+ export { planD1ImportStream, type ImportStreamPlan } from './stream-plan.ts';
8
+ export { createD1ImportStream } from './stream-create.ts';
9
+
10
+ export async function readD1ImportStream(id: string, input: D1ImportContext) {
11
+ return (await streamState(id, requestContext(input), 'read')).job;
12
+ }
13
+ /** One native bounded page invocation. Progress never needs the original input
14
+ * or mapper; all child pages were committed with the reviewed parent layout. */
15
+ export async function resumeD1ImportStream(id: string, input: D1ImportContext) {
16
+ const context = requestContext(input), current = await streamState(id, context, 'apply');
17
+ if (current.row.state === 'undoing') return undoD1ImportStream(id, current.childContext);
18
+ const pending = current.jobs.find(j => j.state !== 'completed');
19
+ if (pending) await applyD1ImportBatches(pending.id, { ...reserveReturn(current.childContext, streamReturnQueries(current, context, 'apply')), maxBatches: 1 });
20
+ return (await streamState(id, context, 'apply')).job;
21
+ }
22
+ export async function undoD1ImportStream(id: string, input: D1ImportContext) {
23
+ const context = requestContext(input);
24
+ let current = await streamState(id, context, 'undo');
25
+ const returns = streamReturnQueries(current, context, 'undo');
26
+ if (current.row.state !== 'undoing') {
27
+ // The only intended change is this parent direction. Any trigger mutation
28
+ // of layout, binding, child progress or direction changes these revisions
29
+ // and aborts before commit, including during assertion cleanup.
30
+ const after = { ...current.row, state: 'undoing', progress_revision: current.row.progress_revision + 1 };
31
+ const transition = sql("UPDATE ik_d1_streams SET state='undoing' WHERE id=?", id);
32
+ transition.finalChecks = [guard(streamSnapshotPredicate(after, true))];
33
+ await atomic(reserveReturn({ ...context, maxBatchBytes: Math.min(context.maxBatchBytes ?? 2 * 1024 * 1024, 2 * 1024 * 1024) }, returns * 2), 'undo', [...current.guards, transition]);
34
+ current = await streamState(id, context, 'undo');
35
+ }
36
+ const pending = current.jobs.findLast(j => !['undone','rolled_back'].includes(j.state));
37
+ if (pending) await reverseD1Import(pending.id, ['completed','undoing'].includes(pending.state) ? 'undo' : 'rollback', { ...reserveReturn(current.childContext, returns), maxBatches: 1 });
38
+ return (await streamState(id, context, 'undo')).job;
39
+ }
@@ -0,0 +1,9 @@
1
+ import type { TargetRow } from '../types.ts';
2
+ import type { D1TargetAdapter } from './types.ts';
3
+ import { guard } from './database.ts';
4
+ /** Every part is a required conjunct; an empty snapshot cannot authorize a write. */
5
+ export function targetGuards(targets: D1TargetAdapter, row: TargetRow | null, destination: string, kind: string, id: string) {
6
+ const parts = targets.matchesParts ? targets.matchesParts(row,destination,kind,id) : [targets.matches(row,destination,kind,id)];
7
+ if (!parts.length) throw new Error('IMPORT_NATIVE_ROW_GUARD_REQUIRED');
8
+ return parts.map(guard);
9
+ }
@@ -0,0 +1,17 @@
1
+ /** Exact target uniqueness without copying a shared destination and kind into
2
+ * every retained key. Identity components remain immutable strings; no hash or
3
+ * truncated surrogate stands in for the complete reviewed tuple. */
4
+ export class TargetIdentities {
5
+ private destinations = new Map<string, Map<string, Set<string>>>();
6
+ private count = 0;
7
+ get size(): number { return this.count; }
8
+ claim(destination: string, kind: string, id: string): boolean {
9
+ let kinds = this.destinations.get(destination);
10
+ if (!kinds) { kinds = new Map(); this.destinations.set(destination, kinds); }
11
+ let ids = kinds.get(kind);
12
+ if (!ids) { ids = new Set(); kinds.set(kind, ids); }
13
+ if (ids.has(id)) return false;
14
+ ids.add(id); this.count++; return true;
15
+ }
16
+ clear(): void { this.destinations.clear(); this.count = 0; }
17
+ }
@@ -0,0 +1,10 @@
1
+ import { rpcBytes, wireBatch } from '../../../d1-transport/index.mjs';
2
+ import type { D1Sql } from './types.ts';
3
+ import { checkSql } from './budget.ts';
4
+
5
+ /** Includes JSON escaping and SQL, including both actual authority predicates.
6
+ * Each member is encoded separately, never the complete import's transport. */
7
+ export function batchBytes(statements: D1Sql[]): number {
8
+ for (const statement of statements) checkSql(statement);
9
+ return rpcBytes(wireBatch(statements));
10
+ }
@@ -0,0 +1,162 @@
1
+ import type { ImportAction, ImportContext, TargetRow, ProvenanceRow, ImportPlan, Json } from '../types.ts';
2
+ import type { D1PreservedUndoAdapter, D1ReversalPolicy } from './undo-types.ts';
3
+ /** Structural subset of the Cloudflare D1 binding; no provider package required. */
4
+ export interface D1Statement {
5
+ bind(...values: any[]): D1Statement;
6
+ first<T = Record<string, any>>(): Promise<T | null>;
7
+ all<T = Record<string, any>>(): Promise<{ results: T[] }>;
8
+ }
9
+ export interface D1Database {
10
+ prepare(sql: string): D1Statement;
11
+ batch(statements: D1Statement[]): Promise<unknown[]>;
12
+ }
13
+ export interface D1Sql {
14
+ sql: string; params?: any[];
15
+ /** Internal exact preflight and maintained intended-state descriptions. */
16
+ finalChecks?: D1Sql[];
17
+ expectedSnapshots?: import('./expected-state.ts').ExpectedSnapshot[];
18
+ expectedChanges?: import('./expected-state.ts').ExpectedChange[] | (() => import('./expected-state.ts').ExpectedChange[]);
19
+ resultKey?: symbol;
20
+ /** Produced by retainedSql; never mark authorization or original-byte input. */
21
+ retainedValues?: { parts: string[]; indexes: number[]; privacyPlan?: ImportPlan; privacyJobId?: string };
22
+ }
23
+ /** Native SQL must be inert and confined to this database. matches must cover the
24
+ * entire row (including evidence, revision and attachments), not just an ID. */
25
+ export interface D1PlanCodec {
26
+ id: string;
27
+ /** Pure deterministic lossless representation. Both roundtrips preserve canonical
28
+ * bytes; unused encoded fields are refused when loading retained custody. */
29
+ encode(plan: ImportPlan): Json;
30
+ decode(payload: Json): ImportPlan;
31
+ }
32
+ export interface D1TargetAdapter extends Partial<D1PreservedUndoAdapter> {
33
+ planCodec?: D1PlanCodec;
34
+ /** Optional transport staging support. Guards must fence every native field
35
+ * from before the first read, including same-revision edits and absence. */
36
+ stagingRead?(db: D1Database, destination: string, kind: string, id: string): Promise<{ row: TargetRow | undefined; guards: D1Sql[] }>;
37
+ /** Protect exact post-state during cleanup, install a known invalidatable
38
+ * receipt after exact checks, then release. Final scalar predicates run
39
+ * read-only after every mutation and must bind the expected post-state. */
40
+ stagingPublication?(stageId: string, destination: string, targets: { kind: string; id: string; present: boolean }[]): { protect: D1Sql[]; seal: D1Sql[]; release: D1Sql[]; final: D1Sql[] };
41
+ read(db: D1Database, destination: string, kind: string, id: string): Promise<TargetRow | undefined>;
42
+ matches(row: TargetRow | null, destination: string, kind: string, id: string): D1Sql;
43
+ /** Optional exact row snapshot split into conjunctive predicates. All parts,
44
+ * including absence/cardinality, commit in the same native transaction. */
45
+ matchesParts?(row: TargetRow | null, destination: string, kind: string, id: string): D1Sql[];
46
+ /** Nonempty exact full-set predicates, including native cardinality and all
47
+ * evidence, even when rows is empty. */
48
+ matchesMany?(rows: TargetRow[], destination: string): D1Sql[];
49
+ /** Exact cardinality of the destination's native set. Export requires this
50
+ * with individual matches, or matchesMany with equivalent full-set coverage. */
51
+ matchesCount?(count: number, destination: string): D1Sql;
52
+ write(row: TargetRow): D1Sql[];
53
+ remove(destination: string, kind: string, id: string): D1Sql[];
54
+ list(db: D1Database, destination: string): Promise<TargetRow[]>;
55
+ /** Optional field-redaction scope, so its cost follows the affected rows, not
56
+ * the destination: `rows` are exactly the destination's rows whose native
57
+ * value holds one of `fields` (exact path and value). `holding` gives nonempty
58
+ * predicates that exactly the given rows hold one; `matches`, set predicates
59
+ * that each given row matches its snapshot; `write` rewrites existing rows. */
60
+ redaction?(db: D1Database, destination: string, fields: readonly D1FieldRedaction[]): Promise<D1RedactionScope>;
61
+ }
62
+ export interface D1RedactionScope {
63
+ rows: TargetRow[];
64
+ holding(rows: TargetRow[]): D1Sql[];
65
+ matches(rows: TargetRow[]): D1Sql[];
66
+ write(rows: TargetRow[]): D1Sql[];
67
+ }
68
+ /** Product recognition of an erased person's data outside the erased target's own
69
+ * sources: another record's mapped values, a rejected row, or an original's bytes.
70
+ * Called with decoded retained content only; must be pure and must not log it. */
71
+ export interface D1RelatedErasure {
72
+ /** Optional readD1ImportFieldMatches snapshot. Fences optional-selector
73
+ * discovery and cancels incomplete private preparations even with no fields. */
74
+ snapshot?: string;
75
+ /** True when this retained source record holds the erased person's data. */
76
+ source?(record: import('../types.ts').SourceRecord): boolean;
77
+ /** True when this retained original's exact bytes hold the erased person's data. */
78
+ original?(bytes: Uint8Array, original: { name: string; sha256: string }): boolean;
79
+ /** Remove exact string values at named paths in retained source payloads,
80
+ * effect/native values and reversal before-images. `*` visits array items.
81
+ * Matching array items are removed; matching object properties are deleted.
82
+ * Peer records and explicit aggregate counts remain. Each distinct path must
83
+ * match at least one exact leaf across all supplied values or erasure refuses. */
84
+ fields?: readonly D1FieldRedaction[];
85
+ }
86
+ export interface D1FieldRedaction {
87
+ path: readonly (string | number)[];
88
+ value: string;
89
+ /** An optional reference (an alternate identifier the person may not have):
90
+ * its path may match nothing. A path with a required selector must match. */
91
+ optional?: boolean;
92
+ }
93
+ /** Exact leaf occurrences per distinct selector path, never selected values. */
94
+ export interface D1FieldMatch {
95
+ path: readonly (string | number)[];
96
+ matches: number;
97
+ }
98
+ export interface D1ImportContext extends Pick<ImportContext, 'destination' | 'namespace' | 'mappingVersion' | 'reviewedFingerprint' | 'operationKey' | 'batchSize' | 'maxBatches'> {
99
+ /** Selected at creation. Omission on later calls uses the durable policy;
100
+ * an explicit different policy is an error. Legacy jobs remain strict. */
101
+ reversalPolicy?: D1ReversalPolicy;
102
+ /** Explicit privacy choice for this erasure; recorded in each changed plan.
103
+ * Whole affected artifacts become unavailable, including other records' bytes. */
104
+ originalErasurePolicy?: import('../types.ts').OriginalErasurePolicy;
105
+ /** Optional caller transport policy. SELECT fences always have a 2 MiB ceiling
106
+ * and honor a lower selected limit. Native mutations gather fewer whole entries
107
+ * to satisfy an explicitly selected bound. */
108
+ maxBatchBytes?: number;
109
+ storage: { database: D1Database; targets: D1TargetAdapter };
110
+ authorization: {
111
+ principalId: string;
112
+ /** Scalar SQL boolean consulting current session, actor, grant and expiry.
113
+ * Evaluated inside the same atomic batch, before AND after native effects. */
114
+ predicate(action: ImportAction, destination: string): D1Sql;
115
+ };
116
+ }
117
+
118
+ /** The predecessor marker stays in successor audit JSON even after reversal. */
119
+ export interface D1ProvenanceRow extends ProvenanceRow { predecessorId?: string }
120
+ /** A person named for erasure from import custody, by exact identifiers only.
121
+ * `target` is the native entity their records were imported into; `values` are
122
+ * exact strings (an email, a label the product built from the person) that may
123
+ * sit whole in a job or stream header, an original's name or a private stage.
124
+ * Nothing is ever matched as a substring. */
125
+ export interface D1ImportPerson {
126
+ target?: { kind: string; id: string };
127
+ values?: string[];
128
+ related?: D1RelatedErasure;
129
+ }
130
+ /** One erasure journal row: what was erased and where, never the values. */
131
+ export interface D1ErasureJournalEntry {
132
+ receiptId: string;
133
+ destination: string;
134
+ recordedAt: string;
135
+ recordsErased: number;
136
+ jobsRetired: number;
137
+ streamsRetired: number;
138
+ stores: { store: string; rows: number }[];
139
+ }
140
+ /** `resumable`: a redaction too large for one request commits bounded steps and
141
+ * returns `D1RetirementProgress`; call again in a new request with its
142
+ * `continuation` until the result's phase is `complete`. */
143
+ export interface D1RetirementOptions { resumable?: boolean; continuation?: string }
144
+ /** A committed step of a resumable redaction. `receiptId` names its journal row
145
+ * when it rewrote custody. Keep `continuation` server-side; it is bound to this
146
+ * person's selectors and destination and holds no selected value. */
147
+ export interface D1RetirementProgress {
148
+ phase: 'redacting';
149
+ continuation: string;
150
+ jobsRedacted: number;
151
+ jobsPending: number;
152
+ fieldMatches: D1FieldMatch[];
153
+ receiptId?: string;
154
+ }
155
+ export interface D1CustodyRetirement extends D1ErasureJournalEntry {
156
+ phase: 'complete';
157
+ fieldMatches: D1FieldMatch[];
158
+ /** Jobs and streams whose whole custody was retired: no undo or resume. */
159
+ retiredJobs: string[];
160
+ retiredStreams: string[];
161
+ nativeResults: unknown[];
162
+ }
@@ -0,0 +1,39 @@
1
+ import type { D1ImportContext, D1Sql } from './types.ts';
2
+ import type { D1UndoEffect, D1UndoSnapshot } from './undo-types.ts';
3
+ import { canonicalJson, targetIdentity } from '../provenance/identity.ts';
4
+ import { guard } from './database.ts';
5
+
6
+ /** A full native set can fence an imported subset, including absent targets.
7
+ * The supplied observations must first agree byte-for-byte with that set; an
8
+ * unrelated native row cannot be used as evidence for a selected effect. */
9
+ export function observationGuards(effects: D1UndoEffect[], snapshot: D1UndoSnapshot, context: D1ImportContext): D1Sql[] {
10
+ const { targets } = context.storage, guards: D1Sql[] = [];
11
+ const expected = new Map(effects.map(e => [e.provenance.id, e.expected]));
12
+ if (snapshot.nativeRows !== undefined) {
13
+ if (!Array.isArray(snapshot.nativeRows) || !targets.matchesMany) throw new Error('IMPORT_NATIVE_SET_GUARD_REQUIRED');
14
+ const native = new Map<string, string>();
15
+ for (const row of snapshot.nativeRows) {
16
+ if (!row || row.destination !== context.destination || typeof row.targetKind !== 'string' || !row.targetKind || typeof row.targetId !== 'string' || !row.targetId || typeof row.revision !== 'string') throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
17
+ const key = targetIdentity(row.destination, row.targetKind, row.targetId);
18
+ if (native.has(key)) throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
19
+ native.set(key, canonicalJson(row));
20
+ }
21
+ for (const observation of snapshot.observations) {
22
+ const effect = expected.get(observation.effectId);
23
+ if (!effect) throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
24
+ const encoded = native.get(targetIdentity(context.destination, effect.targetKind, effect.targetId));
25
+ if (encoded === undefined ? observation.current !== null : observation.current === null || canonicalJson(observation.current) !== encoded) throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
26
+ }
27
+ const predicates = targets.matchesMany(snapshot.nativeRows, context.destination);
28
+ if (!Array.isArray(predicates) || !predicates.length) throw new Error('IMPORT_NATIVE_SET_GUARD_REQUIRED');
29
+ guards.push(...predicates.map(guard));
30
+ }
31
+ for (const observation of snapshot.observations) {
32
+ const effect = expected.get(observation.effectId);
33
+ if (!effect) throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
34
+ // Domain-specific absence/ownership markers remain additional constraints.
35
+ if (observation.guard) guards.push(guard(observation.guard));
36
+ else if (snapshot.nativeRows === undefined) guards.push(guard(targets.matches(observation.current, context.destination, effect.targetKind, effect.targetId)));
37
+ }
38
+ return guards;
39
+ }
@@ -0,0 +1,22 @@
1
+ import type { D1ImportContext } from './types.ts';
2
+ import type { D1UndoReport } from './undo-types.ts';
3
+ import { requestContext } from './budget.ts';
4
+ import { authorize, readAtomic } from './database.ts';
5
+ import { stored, boundary } from './jobs.ts';
6
+ import { custodyGuards, inventoryGuards } from './integrity.ts';
7
+ import { readOriginals } from './originals.ts';
8
+ import { reversalState } from './undo-state.ts';
9
+ import { nativeStagingScope } from './native-staging-context.ts';
10
+
11
+ export async function readD1ImportUndoReport(id: string, context: D1ImportContext, page: { offset?: number; limit?: number } = {}): Promise<D1UndoReport> {
12
+ const offset = page.offset ?? 0, limit = page.limit ?? 100;
13
+ if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > 1000) throw new Error('INVALID_UNDO_REPORT_PAGE');
14
+ context = requestContext(context);
15
+ await authorize(context, 'read');
16
+ const { job, plan } = await stored(id, context);
17
+ await readOriginals(plan, id, context);
18
+ await readAtomic(context, [...boundary(job, plan), ...custodyGuards(plan, id), ...await inventoryGuards(job, plan, context)], nativeStagingScope(job, plan));
19
+ const state = reversalState(job);
20
+ const outcome = !['undone','rolled_back'].includes(job.state) ? 'pending' : state.dispositions.some(d => d.disposition === 'retained') ? 'compensated' : 'undone';
21
+ return { job, policy: state.policy, cursor: state.cursor, total: state.dispositions.length, nextOffset: offset + limit < state.dispositions.length ? offset + limit : null, dispositions: state.dispositions.slice(offset, offset + limit), outcome, ...(state.conversion ? { conversion: state.conversion } : {}) };
22
+ }
@@ -0,0 +1,87 @@
1
+ import type { D1ImportContext, D1Sql } from './types.ts';
2
+ import type { D1UndoEffect, D1UndoDisposition, D1UndoSnapshot } from './undo-types.ts';
3
+ import { canonicalJson, assertNoCredentials } from '../provenance/identity.ts';
4
+ import { undoEvidence } from './undo-state.ts';
5
+ import { guard } from './database.ts';
6
+ import { observationGuards } from './undo-observations.ts';
7
+
8
+ const validEntity = (e: any) => e && typeof e.table === 'string' && !!e.table && typeof e.recordId === 'string' && !!e.recordId && (e.field === undefined || typeof e.field === 'string') && (e.targetId === undefined || typeof e.targetId === 'string');
9
+ /** Ownership is evaluated in application code; every decision is fenced by the
10
+ * adapter's exact, complete current snapshot inside the publication transaction. */
11
+ export function classifyUndo(effects: D1UndoEffect[], snapshot: D1UndoSnapshot, context: D1ImportContext): { ordered: D1UndoDisposition[]; groups: D1UndoDisposition[][]; guards: D1Sql[]; observations: Map<string, D1UndoSnapshot['observations'][number]> } {
12
+ if (!snapshot || !Array.isArray(snapshot.observations) || !Array.isArray(snapshot.dependencies) || !Array.isArray(snapshot.guards) || !snapshot.guards.length) throw new Error('IMPORT_UNDO_SNAPSHOT_REQUIRED');
13
+ const expected = new Map(effects.map(e => [e.provenance.id, e]));
14
+ const observations = new Map(snapshot.observations.map(o => [o.effectId, o]));
15
+ if (expected.size !== effects.length || observations.size !== snapshot.observations.length || observations.size !== expected.size) throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
16
+ const guards = [...snapshot.guards.map(guard), ...observationGuards(effects, snapshot, context)], dispositions = new Map<string, D1UndoDisposition>(), retained = new Set<string>();
17
+ for (const o of snapshot.observations) {
18
+ const effect = expected.get(o.effectId), row = o.current;
19
+ if (!effect || !validEntity(o.entity) || (row !== null && (!row || row.destination !== context.destination || row.targetKind !== effect.expected.targetKind || row.targetId !== effect.expected.targetId || typeof row.revision !== 'string'))) throw new Error('IMPORT_UNDO_SNAPSHOT_MISMATCH');
20
+ const applied = undoEvidence(effect.expected, context), observed = undoEvidence(row, context);
21
+ // A replacement layer must restore the exact historical before image. An
22
+ // ownership projection cannot relax the predecessor's equality contract.
23
+ const strictReplacement = !!effect.provenance.before;
24
+ const unchanged = row !== null && applied.ownedHash === observed.ownedHash && (!strictReplacement || (applied.revision === observed.revision && applied.valueHash === observed.valueHash && applied.attachmentHash === observed.attachmentHash));
25
+ if (!unchanged || effect.retained) retained.add(o.effectId);
26
+ const d: D1UndoDisposition = { jobId: effect.provenance.jobId, effectId: o.effectId, targetKind: effect.expected.targetKind, targetId: effect.expected.targetId, entity: o.entity, disposition: unchanged ? 'reversed' : 'retained', reason: unchanged ? 'unchanged' : 'edited', expected: applied, observed, protectedBy: [], evidence: o.evidence ?? null };
27
+ assertNoCredentials(d); dispositions.set(o.effectId, d);
28
+ }
29
+ const dependencies = [...snapshot.dependencies];
30
+ const groupRoots = new Map(effects.map(e => [e.provenance.id, e.provenance.id]));
31
+ const root = (id: string): string => { while (groupRoots.get(id) !== id) id = groupRoots.get(id)!; return id; };
32
+ if (snapshot.atomicGroups !== undefined && !Array.isArray(snapshot.atomicGroups)) throw new Error('IMPORT_UNDO_GROUP_MISMATCH');
33
+ for (const group of snapshot.atomicGroups ?? []) {
34
+ if (!Array.isArray(group) || !group.length || new Set(group).size !== group.length || group.some(id => !expected.has(id))) throw new Error('IMPORT_UNDO_GROUP_MISMATCH');
35
+ for (const id of group) groupRoots.set(root(id), root(group[0]));
36
+ }
37
+ const units = new Map<string, string[]>();
38
+ for (const id of expected.keys()) { const key = root(id); units.set(key, [...(units.get(key) ?? []), id]); }
39
+ const keys = new Set<string>();
40
+ for (const edge of dependencies) {
41
+ if (!edge || !expected.has(edge.requiredEffectId) || (edge.dependentEffectId !== undefined && !expected.has(edge.dependentEffectId)) || !validEntity(edge.cause) || (edge.order !== undefined && edge.order !== false) || edge.evidence === undefined) throw new Error('IMPORT_UNDO_DEPENDENCY_MISMATCH');
42
+ const key = canonicalJson(edge);
43
+ if (keys.has(key)) throw new Error('IMPORT_UNDO_DEPENDENCY_MISMATCH');
44
+ keys.add(key); assertNoCredentials(edge);
45
+ if (edge.dependentEffectId === undefined) retained.add(edge.requiredEffectId);
46
+ }
47
+ let changed = true;
48
+ while (changed) {
49
+ changed = false;
50
+ for (const edge of dependencies) if ((edge.dependentEffectId === undefined || retained.has(edge.dependentEffectId)) && !retained.has(edge.requiredEffectId)) { retained.add(edge.requiredEffectId); changed = true; }
51
+ for (const unit of units.values()) {
52
+ const cause = unit.find(id => retained.has(id));
53
+ if (cause === undefined) continue;
54
+ for (const id of unit) if (!retained.has(id)) {
55
+ retained.add(id); changed = true;
56
+ dependencies.push({ requiredEffectId: id, dependentEffectId: cause, order: false, cause: dispositions.get(cause)!.entity, evidence: { atomicGroup: root(id), reason: 'atomic-reversal-unit' } });
57
+ }
58
+ }
59
+ }
60
+ for (const id of retained) {
61
+ const d = dispositions.get(id)!;
62
+ if (d.reason !== 'edited') d.reason = 'dependency';
63
+ d.disposition = 'retained';
64
+ d.protectedBy = dependencies.filter(e => e.requiredEffectId === id && (e.dependentEffectId === undefined || retained.has(e.dependentEffectId)));
65
+ }
66
+ const pending = effects.filter(e => !e.retained).map(e => e.provenance.id), groups: D1UndoDisposition[][] = [];
67
+ // Retentions are individually durable. Every removable atomic unit is
68
+ // topologically ordered both against other units and within its own effects.
69
+ for (const id of pending.filter(id => retained.has(id))) groups.push([dispositions.get(id)!]);
70
+ const removing = new Set(pending.filter(id => !retained.has(id)));
71
+ const orderMembers = (members: string[]) => {
72
+ const left = new Set(members), ordered: D1UndoDisposition[] = [];
73
+ while (left.size) {
74
+ const ready = members.find(id => left.has(id) && !dependencies.some(e => e.order !== false && e.requiredEffectId === id && e.dependentEffectId !== undefined && left.has(e.dependentEffectId)));
75
+ if (ready === undefined) throw new Error('IMPORT_UNDO_DEPENDENCY_CYCLE');
76
+ ordered.push(dispositions.get(ready)!); left.delete(ready);
77
+ }
78
+ return ordered;
79
+ };
80
+ while (removing.size) {
81
+ const ready = pending.find(id => removing.has(id) && !dependencies.some(e => e.order !== false && root(e.requiredEffectId) === root(id) && e.dependentEffectId !== undefined && removing.has(e.dependentEffectId) && root(e.dependentEffectId) !== root(id)));
82
+ if (ready === undefined) throw new Error('IMPORT_UNDO_DEPENDENCY_CYCLE');
83
+ const members = units.get(root(ready))!.filter(id => removing.has(id));
84
+ groups.push(orderMembers(members)); members.forEach(id => removing.delete(id));
85
+ }
86
+ return { ordered: groups.flat(), groups, guards, observations };
87
+ }