@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,78 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import type { D1Database, D1Sql } from './types.ts';
3
+ import { guard, sql } from './database.ts';
4
+ import { retainedSql } from './retained-values.ts';
5
+ import { changedLazy, putRow } from './expected-state.ts';
6
+
7
+ // JSON snapshots are split by UTF-8 size, never by source count alone. Each
8
+ // statement compares the original column values, not their recorded hashes.
9
+ export const SNAPSHOT_BYTES = 128 * 1024;
10
+ export const PACKED_JSON_BYTES = 1900000;
11
+ export function jsonChunks<T>(rows: T[], maxBytes = SNAPSHOT_BYTES): string[] {
12
+ const chunks: string[] = []; let parts: string[] = [], size = 2;
13
+ for (const row of rows) {
14
+ const value = JSON.stringify(row), bytes = Buffer.byteLength(value) + 1;
15
+ if (bytes + 1 > PACKED_JSON_BYTES) throw new Error('IMPORT_D1_VALUE_LIMIT');
16
+ if (parts.length && size + bytes > maxBytes) { chunks.push(`[${parts.join(',')}]`); parts = []; size = 2; }
17
+ parts.push(value); size += bytes;
18
+ }
19
+ if (parts.length) chunks.push(`[${parts.join(',')}]`);
20
+ return chunks;
21
+ }
22
+ export async function selectKeys(db: D1Database, table: string, key: string, values: string[], fence?:D1Sql): Promise<any[]> {
23
+ const rows: any[] = [], keys = [...new Set(values)];
24
+ for (const chunk of jsonChunks(keys)) {
25
+ // A singleton needs no JSON table. Keep the same size admission, result
26
+ // normalization and optional snapshot fence as the bounded set path.
27
+ const singleton = keys.length === 1;
28
+ const result = await db.prepare(`SELECT ${table === 'ik_originals' ? "*,CASE WHEN typeof(name)='text' THEN json_quote(name) ELSE NULL END AS name_json" : '*'} FROM ${table} WHERE ${key}${singleton ? '=?' : ' IN (SELECT value FROM json_each(?))'}${fence?` AND (${fence.sql})`:''}`).bind(singleton ? keys[0] : chunk,...fence?.params??[]).all();
29
+ rows.push(...result.results.map(row => table === 'ik_originals' ? originalRow(row) : row));
30
+ }
31
+ return rows;
32
+ }
33
+ /** Close a selected-key snapshot against additions, including empty owner sets.
34
+ * Pair these counts with exactRows for the existing rows' complete fields. */
35
+ export function selectedCounts(table: string, key: string, values: string[], rows: any[]): D1Sql[] {
36
+ const counts = new Map<string, number>();
37
+ for (const row of rows) counts.set(row[key], (counts.get(row[key]) ?? 0) + 1);
38
+ return jsonChunks([...new Set(values)]).map(chunk => {
39
+ const selected = JSON.parse(chunk);
40
+ const result = guard(sql(`(SELECT COUNT(*) FROM ${table} WHERE ${key} IN (SELECT value FROM json_each(?)))=?`, chunk, selected.reduce((n: number, value: string) => n + (counts.get(value) ?? 0), 0)));
41
+ result.expectedSnapshots = [{table,columns:[],rows:[],selection:{key,values:selected}}];
42
+ return result;
43
+ });
44
+ }
45
+ /** Full-field comparison plus a separate scoped cardinality fence. Callers using
46
+ * a selected-key closure guard that closure's counts, not unrelated rows. */
47
+ export function exactRows(table: string, columns: string[], rows: any[], scope?: D1Sql, maxBytes = SNAPSHOT_BYTES, retain = !['ik_originals','ik_d1_original_chunks'].includes(table), selection?: {key:string;values:string[]}): D1Sql[] {
48
+ const result: D1Sql[] = [];
49
+ if (scope) result.push(guard(sql(`(SELECT COUNT(*) FROM ${table} WHERE ${scope.sql})=?`, ...(scope.params ?? []), rows.length)));
50
+ // Very large old inline plans must not grow beyond D1's value ceiling merely
51
+ // because JSON encoding escapes them a second time.
52
+ const ordinary: any[] = [];
53
+ for (const row of rows) {
54
+ const picked = Object.fromEntries(columns.map(c => [c, row[c]]));
55
+ if (Buffer.byteLength(JSON.stringify(picked)) + 2 > maxBytes) {
56
+ const parts=columns.map((c,i)=>`${i?' AND ':`EXISTS(SELECT 1 FROM ${table} t WHERE `}t.${c} IS `).concat(')'), params=columns.map(c=>row[c]);
57
+ result.push(guard(retain?retainedSql(parts,params,params.flatMap((value,i)=>typeof value==='string'?[i]:[])):sql(parts.join('?'),...params)));
58
+ } else ordinary.push(picked);
59
+ }
60
+ for (const chunk of jsonChunks(ordinary, maxBytes)) {
61
+ const parts=['NOT EXISTS(SELECT 1 FROM json_each(',`) e WHERE NOT EXISTS(SELECT 1 FROM ${table} t WHERE ${columns.map(c => `t.${c} IS json_extract(e.value,'$.${c}')`).join(' AND ')}))`];
62
+ result.push(guard(retain?retainedSql(parts,[chunk],[0]):sql(parts.join('?'),chunk)));
63
+ }
64
+ if (result.length) result[0].expectedSnapshots = [{table,columns,rows,...(selection?{selection}:{})}];
65
+ return result;
66
+ }
67
+ export const jobColumns = ['id','destination','namespace','mapping_version','principal_id','fingerprint','operation_key','plan_json','state','checkpoint','total','fence','lease_token','lease_expires','error','reversal_policy','reversal_policy_hash','reversal_cursor','reversal_hash'];
68
+ export const provenanceColumns = ['id','job_id','ordinal','effect_ordinal','source_identity','destination','target_kind','target_id','reversed','record_json'];
69
+ export const claimColumns = ['identity','source_hash','mapping_version','effects_hash','job_id'];
70
+
71
+ /** One bound JSON value per statement; SQLite preserves strings as TEXT even
72
+ * in BLOB-affinity columns. Column names are internal constants only. */
73
+ export function insertJsonRows(table: string, columns: string[], rows: any[]): D1Sql[] {
74
+ return jsonChunks(rows, PACKED_JSON_BYTES).map(chunk => changedLazy(retainedSql(
75
+ [`INSERT INTO ${table}(${columns.join(',')}) SELECT ${columns.map(c => `json_extract(value,'$.${c}')`).join(',')} FROM json_each(`,')'],[chunk],['ik_originals','ik_d1_original_chunks'].includes(table)?[]:[0]),()=>JSON.parse(chunk).map((row:any)=>putRow(table,row))));
76
+ }
77
+
78
+ function originalRow(row:any):any{if(row.name_json===null)throw new Error('ORIGINAL_CUSTODY_MISMATCH');return {...row,name:JSON.parse(row.name_json)};}
@@ -0,0 +1,201 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import type { ImportPlan } from '../types.ts';
3
+ import type { D1ImportContext, D1Sql } from './types.ts';
4
+ import type { StoredPlan } from './plans.ts';
5
+ import { atomic, atomicCount, guard, prepare, sql } from './database.ts';
6
+ import { D1RequestLimitError, remaining } from './budget.ts';
7
+ import { stagingStatements } from './staging-statements.ts';
8
+ import { batchBytes } from './transport.ts';
9
+ import { bindingPredicate, stageClear, type StageBinding } from './staging-transport.ts';
10
+ import { beginStageWrites, discardStageWrites, stageRevisionWrites } from './staging-control.ts';
11
+ import { planIndex } from './erasure-index.ts';
12
+ import { custodyGuards } from './original-storage.ts';
13
+ import { retainOriginals } from './originals.ts';
14
+ import { originalsFor } from '../storage/originals.ts';
15
+ import { PACKED_JSON_BYTES } from './sets.ts';
16
+ import { erasedSourcePredicates } from './erasure-claims.ts';
17
+ import { policy, policyHash, EMPTY_REVERSAL_HASH } from './undo-state.ts';
18
+ import { canonicalJson, sha256 } from '../provenance/identity.ts';
19
+
20
+ /** Ordinary job creation of a large plan in bounded steps. The plan's chunks
21
+ * (holding a canonical plan's originals literally) are staged privately in
22
+ * the kit's existing staging tables, a few MiB per request, behind a hash-only
23
+ * privacy index that erasure and retirement already reach. One small final
24
+ * transaction then publishes the job by copying the staged rows inside D1: no
25
+ * request carries the whole plan or a whole original, and none repeats them in
26
+ * guards. A partly staged creation is never a job; a later call resumes from the
27
+ * rows already staged, or discards a generation that no longer matches. */
28
+ export const STAGED_BATCH_BYTES = 4 * 1024 * 1024;
29
+ /** Marks an ordinary job's stage; stream stages carry their page layout hash. */
30
+ export const ORDINARY_STAGE_LAYOUT = 'ordinary-job-v1';
31
+ const READ_ROWS = 16;
32
+ type Table = 'pages' | 'index' | 'originals' | 'chunks';
33
+ const columns: Record<Table, string[]> = {
34
+ pages: ['ordinal','job_id','total','fingerprint','plan_json','body_hash','body_size','chunks','identities','index_hash','policy_hash','inline_plan'],
35
+ index: ['page_ordinal','ordinal','source_identity','target_identity'],
36
+ originals: ['original_key','name','sha256','size'],
37
+ chunks: ['page_ordinal','ordinal','body','body_hash','body_size'],
38
+ };
39
+ const order: Record<Table, string> = { pages: 'ordinal', index: 'page_ordinal,ordinal,target_identity', originals: 'original_key', chunks: 'page_ordinal,ordinal' };
40
+ type Row = unknown[];
41
+ interface Layout { stage: StageBinding; jobId: string; rows: Record<Table, Row[]> }
42
+
43
+ /** The complete private layout of one creation: pages, privacy index, original
44
+ * names and payload chunks, in the order they are written and verified. */
45
+ function layout(plan: ImportPlan, context: D1ImportContext, operationKey: string, encoded: StoredPlan, stageId: string, generation: string, jobId: string): Layout {
46
+ const envelope = JSON.parse(encoded.body), selected = policy(context);
47
+ const index: Row[] = planIndex(plan, jobId).map(row => [0, row.ordinal, row.source_identity, row.target_identity]);
48
+ const pages: Row[] = [[0, jobId, plan.entries.length, plan.fingerprint, encoded.body, envelope.sha256, envelope.size, envelope.chunks, index.length, sha256(canonicalJson(index)), policyHash(jobId, plan.fingerprint, selected), 0]];
49
+ const chunks: Row[] = encoded.chunks.map(c => [0, c.ordinal, c.body, sha256(c.body), Buffer.byteLength(c.body)]);
50
+ const originals: Row[] = originalsFor(plan, jobId).map(o => [canonicalJson([o.kind, o.key]), o.name, o.sha256, o.size]);
51
+ const stage: StageBinding = { id: stageId, generation, destination: plan.destination, namespace: plan.namespace, mapping_version: plan.mappingVersion,
52
+ principal_id: plan.principalId, fingerprint: plan.fingerprint, operation_key: operationKey, total: plan.entries.length, pages: pages.length,
53
+ layout_hash: ORDINARY_STAGE_LAYOUT, reversal_policy: selected };
54
+ return { stage, jobId, rows: { pages, index, originals, chunks } };
55
+ }
56
+
57
+ /** A row's worst-case share of D1's batch encoding: its JSON text is bound as a
58
+ * string, so escaped again, then counted in UTF-8 or at two bytes per UTF-16
59
+ * unit once any wide character is present. */
60
+ const cost = (text: string): number => { const bound = JSON.stringify(text); return Math.max(Buffer.byteLength(bound), 2 * bound.length); };
61
+ const insert = (table: Table, stageId: string, body: string): D1Sql => sql(`INSERT INTO ik_d1_staging_${table}(stage_id,${columns[table].join(',')}) SELECT ?,${columns[table].map((_, i) => `json_extract(value,'$[${i}]')`).join(',')} FROM json_each(?)`, stageId, body);
62
+ const revisionIs = (stage: StageBinding, revision: number) => guard(sql('EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=?)', stage.id, revision));
63
+
64
+ /** Bounded write batches for `rows` of one table, each fenced on the stage's
65
+ * binding, its exact revision before and after, and the privacy tombstones. */
66
+ function writes(stage: StageBinding, table: Table, rows: Row[], start: number): { statements: D1Sql[]; count: number }[] {
67
+ const batches: { statements: D1Sql[]; count: number }[] = [], budget = STAGED_BATCH_BYTES - 512 * 1024;
68
+ let revision = start, statements: string[] = [], parts: string[] = [], bytes = 2, batchCost = 0, count = 0;
69
+ const statement = () => { if (parts.length) statements.push(`[${parts.join(',')}]`); parts = []; bytes = 2; };
70
+ const flush = () => {
71
+ statement();
72
+ if (!statements.length) return;
73
+ batches.push({ count, statements: [guard(bindingPredicate(stage)), revisionIs(stage, revision), guard(stageClear(stage)),
74
+ ...statements.map(body => insert(table, stage.id, body)),
75
+ guard(bindingPredicate(stage)), revisionIs(stage, revision + count), guard(stageClear(stage))] });
76
+ revision += count; statements = []; batchCost = 0; count = 0;
77
+ };
78
+ for (const row of rows) {
79
+ const text = JSON.stringify(row), size = Buffer.byteLength(text) + 1, weight = cost(text);
80
+ if (count && batchCost + weight > budget) flush();
81
+ if (parts.length && bytes + size > PACKED_JSON_BYTES) statement();
82
+ parts.push(text); bytes += size; batchCost += weight; count++;
83
+ }
84
+ flush();
85
+ return batches;
86
+ }
87
+
88
+ /** The single publishing transaction: every live row is copied from the sealed
89
+ * stage inside D1 and compared with it there; only identities, positions and
90
+ * metadata cross the transport. */
91
+ function publication(plan: ImportPlan, context: D1ImportContext, encoded: StoredPlan, l: Layout, revision: number): D1Sql[] {
92
+ const { stage, jobId: id } = l, selected = policy(context);
93
+ return [guard(bindingPredicate(stage)), guard(sql('EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=? AND sealed_revision=? AND registered_revision IS NOT NULL)', stage.id, revision, revision)), guard(stageClear(stage)),
94
+ ...erasedSourcePredicates(plan).map(guard),
95
+ sql("INSERT INTO ik_jobs(id,destination,namespace,mapping_version,principal_id,fingerprint,operation_key,plan_json,state,total,reversal_policy,reversal_policy_hash) VALUES(?,?,?,?,?,?,?,?,'queued',?,?,?)", id, plan.destination, plan.namespace, plan.mappingVersion, plan.principalId, plan.fingerprint, stage.operation_key, encoded.body, plan.entries.length, selected, policyHash(id, plan.fingerprint, selected)),
96
+ sql('INSERT INTO ik_d1_plan_chunks(job_id,ordinal,body) SELECT ?,ordinal,body FROM ik_d1_staging_chunks WHERE stage_id=? AND page_ordinal=0', id, stage.id),
97
+ // Canonical chunks hold each original: it is copied from them by position.
98
+ ...retainOriginals(plan, id, encoded),
99
+ sql('INSERT INTO ik_d1_plan_index(job_id,ordinal,source_identity,target_identity) SELECT ?,ordinal,source_identity,target_identity FROM ik_d1_staging_index WHERE stage_id=? AND page_ordinal=0', id, stage.id),
100
+ sql('INSERT INTO ik_d1_indexed_jobs(job_id,fingerprint) VALUES(?,?)', id, plan.fingerprint),
101
+ guard(sql('EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND destination=? AND namespace=? AND mapping_version=? AND principal_id=? AND fingerprint=? AND operation_key=? AND plan_json=? AND state=\'queued\' AND checkpoint=0 AND fence=0 AND total=? AND error IS NULL AND lease_token IS NULL AND lease_expires IS NULL AND reversal_policy=? AND reversal_policy_hash=? AND reversal_cursor=0 AND reversal_hash=?)', id, plan.destination, plan.namespace, plan.mappingVersion, plan.principalId, plan.fingerprint, stage.operation_key, encoded.body, plan.entries.length, selected, policyHash(id, plan.fingerprint, selected), EMPTY_REVERSAL_HASH)),
102
+ // Every live plan chunk, its creation capture and every index row equal the sealed stage's.
103
+ guard(sql('(SELECT count(*) FROM ik_d1_plan_chunks WHERE job_id=?)=? AND (SELECT count(*) FROM ik_d1_creation_plans WHERE job_id=? AND released=0)=? AND NOT EXISTS(SELECT 1 FROM ik_d1_staging_chunks c WHERE c.stage_id=? AND c.page_ordinal=0 AND (NOT EXISTS(SELECT 1 FROM ik_d1_plan_chunks p WHERE p.job_id=? AND p.ordinal=c.ordinal AND p.body IS c.body) OR NOT EXISTS(SELECT 1 FROM ik_d1_creation_plans p WHERE p.job_id=? AND p.ordinal=c.ordinal AND p.body IS c.body AND p.released=0)))', id, encoded.chunks.length, id, encoded.chunks.length, stage.id, id, id)),
104
+ guard(sql('(SELECT count(*) FROM ik_d1_plan_index WHERE job_id=?)=? AND NOT EXISTS(SELECT 1 FROM ik_d1_staging_index i WHERE i.stage_id=? AND NOT EXISTS(SELECT 1 FROM ik_d1_plan_index p WHERE p.job_id=? AND p.ordinal=i.ordinal AND p.source_identity=i.source_identity AND p.target_identity=i.target_identity))', id, l.rows.index.length, stage.id, id)),
105
+ ...custodyGuards(plan, id, encoded),
106
+ guard(sql('(SELECT COUNT(*) FROM ik_d1_reversal_reports WHERE job_id=?)=0', id)),
107
+ sql('UPDATE ik_d1_creation_plans SET released=1 WHERE job_id=?', id), sql('DELETE FROM ik_d1_creation_plans WHERE job_id=?', id), guard(sql('(SELECT COUNT(*) FROM ik_d1_creation_plans WHERE job_id=?)=0', id)),
108
+ sql('DELETE FROM ik_d1_staging WHERE id=? AND generation=?', stage.id, stage.generation), guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=?)', stage.id))];
109
+ }
110
+
111
+ async function readRows(context: D1ImportContext, table: Table, stageId: string): Promise<Row[]> {
112
+ const rows: Row[] = [];
113
+ for (;;) {
114
+ const { results } = await context.storage.database.prepare(`SELECT ${columns[table].join(',')} FROM ik_d1_staging_${table} WHERE stage_id=? ORDER BY ${order[table]} LIMIT ${READ_ROWS} OFFSET ?`).bind(stageId, rows.length).all<Record<string, unknown>>();
115
+ rows.push(...results.map(row => columns[table].map(c => row[c])));
116
+ if (results.length < READ_ROWS) return rows;
117
+ }
118
+ }
119
+ const same = (a: Row, b: Row) => a.length === b.length && a.every((v, i) => v === b[i]);
120
+ /** Index and original rows compare as sets: both sides in one deterministic order. */
121
+ const key = (row: Row) => JSON.stringify(row);
122
+ const sorted = (table: Table, rows: Row[]): Row[] => table === 'pages' || table === 'chunks' ? rows : [...rows].sort((a, b) => key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0);
123
+
124
+ /** How much of a prior generation can be kept: complete identical pages, index
125
+ * and originals, and an identical prefix of chunks. Anything else is discarded. */
126
+ async function resumable(context: D1ImportContext, prior: any, expected: Layout): Promise<{ revision: number; chunks: number } | null> {
127
+ if (prior.registered_revision === null || prior.registered_revision === undefined) return null;
128
+ let revision = 0;
129
+ for (const table of ['pages', 'index', 'originals'] as const) {
130
+ const found = sorted(table, await readRows(context, table, prior.id)), wanted = sorted(table, expected.rows[table]);
131
+ if (found.length !== wanted.length || found.some((row, i) => !same(row, wanted[i]))) return null;
132
+ revision += found.length;
133
+ }
134
+ if (prior.registered_revision !== expected.rows.pages.length + expected.rows.index.length) return null;
135
+ const found = await readRows(context, 'chunks', prior.id);
136
+ if (found.length > expected.rows.chunks.length || found.some((row, i) => !same(row, expected.rows.chunks[i]))) return null;
137
+ revision += found.length;
138
+ return prior.revision === revision ? { revision, chunks: found.length } : null;
139
+ }
140
+
141
+ /** Execute one bounded private batch: authority before and after, the stage's
142
+ * own fences, and never more than STAGED_BATCH_BYTES on the wire. */
143
+ async function stagedBatch(context: D1ImportContext, statements: D1Sql[]): Promise<void> {
144
+ const packed = stagingStatements(context, 'create', statements);
145
+ if (packed.length > remaining(context)) throw new D1RequestLimitError();
146
+ if (batchBytes(packed) > STAGED_BATCH_BYTES) throw new Error('IMPORT_STAGE_TRANSPORT_LIMIT');
147
+ await context.storage.database.batch(packed.map(s => prepare(context.storage.database, s)));
148
+ }
149
+
150
+ /** Above LARGE_PLAN_BYTES of stored plan a creation is staged. The decision
151
+ * depends on the reviewed plan alone, so a retry always takes the same path. */
152
+ export const stagedSize = (encoded: StoredPlan): number => encoded.chunks.reduce((n, c) => n + Buffer.byteLength(c.body), 0);
153
+ export interface StagedCreation { jobId: string; layout: Layout; batches: D1Sql[][]; publication: D1Sql[] }
154
+ /** Every remaining request of a creation, built before any is sent, so its
155
+ * whole cost is admitted against the request budget first. */
156
+ /** `encoded` is the plan's stored form; its body and chunk bodies do not depend
157
+ * on the job id, which only the staged page and the publication bind. */
158
+ export async function planStagedCreation(plan: ImportPlan, context: D1ImportContext, operationKey: string, encoded: StoredPlan, newIds: () => [string, string, string]): Promise<StagedCreation> {
159
+ const db = context.storage.database;
160
+ const prior = await db.prepare('SELECT * FROM ik_d1_staging WHERE destination=? AND principal_id=? AND operation_key=?').bind(context.destination, context.authorization.principalId, operationKey).first<any>();
161
+ if (prior && (prior.layout_hash !== ORDINARY_STAGE_LAYOUT || prior.fingerprint !== plan.fingerprint || prior.namespace !== plan.namespace || prior.mapping_version !== plan.mappingVersion)) throw new Error('OPERATION_KEY_CONFLICT');
162
+ const page = prior ? await db.prepare('SELECT job_id FROM ik_d1_staging_pages WHERE stage_id=? AND ordinal=0').bind(prior.id).first<{ job_id: string }>() : null;
163
+ if (prior && page) {
164
+ const expected = layout(plan, context, operationKey, encoded, prior.id, prior.generation, page.job_id);
165
+ const same = prior.reversal_policy === expected.stage.reversal_policy && prior.total === expected.stage.total && prior.pages === expected.stage.pages;
166
+ const kept = same ? await resumable(context, prior, expected) : null;
167
+ if (kept) return build(plan, context, encoded, expected, kept);
168
+ }
169
+ // A prior generation that cannot be continued is discarded before the new begin.
170
+ const [stageId, generation, jobId] = newIds();
171
+ return build(plan, context, encoded, layout(plan, context, operationKey, encoded, stageId, generation, jobId), undefined, prior ?? undefined);
172
+ }
173
+ /** Pure: every request of a creation from `kept` (or from nothing, discarding
174
+ * `discard`), built before any is sent so its whole cost is admitted first. */
175
+ function build(plan: ImportPlan, context: D1ImportContext, encoded: StoredPlan, l: Layout, kept?: { revision: number; chunks: number }, discard?: any): StagedCreation {
176
+ const batches: D1Sql[][] = [];
177
+ let revision = kept?.revision ?? 0;
178
+ if (!kept) {
179
+ if (discard) batches.push(discardStageWrites({ ...discard }));
180
+ batches.push(beginStageWrites(l.stage));
181
+ for (const table of ['pages', 'index'] as const) for (const batch of writes(l.stage, table, l.rows[table], revision)) { batches.push(batch.statements); revision += batch.count; }
182
+ batches.push(stageRevisionWrites(l.stage, revision, false));
183
+ for (const batch of writes(l.stage, 'originals', l.rows.originals, revision)) { batches.push(batch.statements); revision += batch.count; }
184
+ }
185
+ for (const batch of writes(l.stage, 'chunks', l.rows.chunks.slice(kept?.chunks ?? 0), revision)) { batches.push(batch.statements); revision += batch.count; }
186
+ batches.push(stageRevisionWrites(l.stage, revision, true));
187
+ return { jobId: l.jobId, layout: l, batches, publication: publication(plan, context, encoded, l, revision) };
188
+ }
189
+ /** Pure admission of a fresh staged creation, as preflight sizes it. */
190
+ export function preflightStagedCreation(plan: ImportPlan, context: D1ImportContext, operationKey: string, encoded: StoredPlan, jobId: string): StagedCreation {
191
+ return build(plan, context, encoded, layout(plan, context, operationKey, encoded, jobId, jobId, jobId));
192
+ }
193
+ /** Statements the creation spends in this request, excluding its return reads. */
194
+ export const stagedCreationCost = (context: D1ImportContext, creation: StagedCreation): number =>
195
+ creation.batches.reduce((n, batch) => n + stagingStatements(context, 'create', batch).length, 0) + atomicCount(context, 'create', creation.publication, 0);
196
+ export async function runStagedCreation(context: D1ImportContext, creation: StagedCreation, returnReads: number): Promise<string> {
197
+ if (stagedCreationCost(context, creation) + returnReads > remaining(context)) throw new D1RequestLimitError();
198
+ for (const batch of creation.batches) await stagedBatch(context, batch);
199
+ await atomic(context, 'create', creation.publication, returnReads);
200
+ return creation.jobId;
201
+ }
@@ -0,0 +1,45 @@
1
+ import { stagingStatements } from './staging-statements.ts';
2
+ import { Buffer } from 'node:buffer';
3
+ import type { ImportStreamPlan } from './stream-plan.ts';
4
+ import type { D1ImportContext } from './types.ts';
5
+ import { guard, sql } from './database.ts';
6
+ import { D1RequestLimitError, remaining } from './budget.ts';
7
+ import { bindingPredicate, stageClear, stagingBatchBytes, stagingChunks, STAGING_BATCH_BYTES, STAGING_ROWS_BYTES, type StageBinding } from './staging-transport.ts';
8
+ import { stagePublication, publishedStagePredicates } from './staging-publication.ts';
9
+ import { stagingPlan } from './staging-plan.ts';
10
+ import { stagingControlQueries } from './staging-control.ts';
11
+
12
+ /** Reserve all transactions before private writes. Greedy row batches are full
13
+ * to at least limit-largestRow, except their last batch. Counts include both
14
+ * current-authority checks, mutation guards, cleanup and public return reads. */
15
+ export function reserveStaging(stream: ImportStreamPlan, originals: unknown[][], context: D1ImportContext, stage: StageBinding, pages:{ordinal:number;job_id:string;total:number}[]): { executions: number; indexBatches: number } {
16
+ const batches = (bytes: number, largest: number) => {
17
+ if (largest + 2 > STAGING_ROWS_BYTES) throw new Error('IMPORT_STAGE_TRANSPORT_LIMIT');
18
+ return bytes ? Math.ceil(bytes / (STAGING_ROWS_BYTES - largest - 2)) : 0;
19
+ };
20
+ const indices = stream.header.counts.effects + stream.header.counts.unique;
21
+ if (!Number.isSafeInteger(indices) || indices < 0) throw new Error('IMPORT_STREAM_COUNTS_CHANGED');
22
+ let chunkBytes = 0, largestChunk = 0;
23
+ for (let page = 0; page < stream.pages.length; page++) {
24
+ let ordinal = 0;
25
+ for (const body of stagingChunks(stagingPlan(stream.pages[page], context.storage.targets.planCodec).body)) {
26
+ const bytes = Buffer.byteLength(JSON.stringify([page, ordinal++, body, '0'.repeat(64), Buffer.byteLength(body)])) + 1;
27
+ chunkBytes += bytes; largestChunk = Math.max(largestChunk, bytes);
28
+ }
29
+ }
30
+ let originalBytes = 0, largestOriginal = 0;
31
+ for (const row of originals) { const bytes = Buffer.byteLength(JSON.stringify(row)) + 1; originalBytes += bytes; largestOriginal = Math.max(largestOriginal, bytes); }
32
+ const sample = stagingStatements(context, 'create', [guard(bindingPredicate(stage)), guard(sql('EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=?)', stage.id, 0)), guard(stageClear(stage)),
33
+ sql('INSERT INTO ik_d1_staging_chunks(stage_id,page_ordinal,ordinal,body,body_hash,body_size) SELECT ?,json_extract(value,\'$[0]\'),json_extract(value,\'$[1]\'),json_extract(value,\'$[2]\'),json_extract(value,\'$[3]\'),json_extract(value,\'$[4]\') FROM json_each(?)', stage.id, ''),
34
+ guard(bindingPredicate(stage)), guard(sql('EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=?)', stage.id, Number.MAX_SAFE_INTEGER)), guard(stageClear(stage)), sql('SELECT page_ordinal,ordinal,body,body_hash,body_size FROM ik_d1_staging_chunks WHERE stage_id=? ORDER BY rowid LIMIT ? OFFSET ?', stage.id, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)]);
35
+ if (stagingBatchBytes(sample) + 2 * STAGING_ROWS_BYTES + 100000 > STAGING_BATCH_BYTES) throw new Error('IMPORT_STAGE_TRANSPORT_LIMIT');
36
+ const indexBatches = batches(indices * 200, 200);
37
+ const batchCount = indexBatches + batches(stream.pages.length * 1024, 1024) + batches(chunkBytes, largestChunk) + batches(originalBytes, largestOriginal);
38
+ const publication = stagingStatements(context, 'create', stagePublication(stage, Number.MAX_SAFE_INTEGER), [sql('DELETE FROM ik_d1_publication_fences WHERE stream_id=?', stage.id)], publishedStagePredicates(stage,pages));
39
+ if (stagingBatchBytes(publication) > STAGING_BATCH_BYTES) throw new Error('IMPORT_STAGE_TRANSPORT_LIMIT');
40
+ // Use the same packed control/receipt builders as execution, including
41
+ // generation cleanup and authorized concurrent-winner recovery.
42
+ const executions = batchCount * sample.length + indexBatches + publication.length + stagingControlQueries(context, stage);
43
+ if (!Number.isSafeInteger(executions) || executions > remaining(context)) throw new D1RequestLimitError();
44
+ return { executions, indexBatches };
45
+ }
@@ -0,0 +1,43 @@
1
+ import type { D1ImportContext } from './types.ts';
2
+ import { guard, sql } from './database.ts';
3
+ import { exactRows } from './sets.ts';
4
+ import { bindingPredicate, stageClear, type StageBinding } from './staging-transport.ts';
5
+ import { stageMembership } from './staging-publication.ts';
6
+ import { stagingStatements } from './staging-statements.ts';
7
+ import { publishedStreamReceipt } from './published-stream.ts';
8
+
9
+ export function beginStageWrites(stage: StageBinding, prior?: any) {
10
+ const before = prior ? exactRows('ik_d1_staging', Object.keys(prior), [prior]) : [guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging WHERE destination=? AND principal_id=? AND operation_key=?)', stage.destination, stage.principal_id, stage.operation_key))];
11
+ return [...before,
12
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_streams WHERE destination=? AND principal_id=? AND operation_key=?)', stage.destination, stage.principal_id, stage.operation_key)),
13
+ ...(prior ? [sql('DELETE FROM ik_d1_staging WHERE id=? AND generation=?', prior.id, prior.generation)] : []),
14
+ sql(`INSERT INTO ik_d1_staging(${Object.keys(stage).join(',')}) VALUES(${Object.keys(stage).map(() => '?').join(',')})`, ...Object.values(stage))];
15
+ }
16
+ export const discardStageWrites = (stage: StageBinding) => [sql('DELETE FROM ik_d1_staging WHERE id=? AND generation=? AND destination=? AND namespace=? AND mapping_version=? AND principal_id=? AND operation_key=? AND fingerprint=?', stage.id, stage.generation, stage.destination, stage.namespace, stage.mapping_version, stage.principal_id, stage.operation_key, stage.fingerprint)];
17
+
18
+ export function stageRevisionWrites(stage: StageBinding, revision: number, seal: boolean) {
19
+ return [guard(bindingPredicate(stage)), guard(sql(`EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=?${seal ? ' AND registered_revision IS NOT NULL' : ''})`, stage.id, revision)), guard(stageMembership(stage, seal)), guard(stageClear(stage)),
20
+ sql(`UPDATE ik_d1_staging SET ${seal ? 'sealed_revision' : 'registered_revision'}=revision WHERE id=?`, stage.id)];
21
+ }
22
+
23
+ /** Reserve the complete successful receipt plus the reachable failure path:
24
+ * generation-only discard, operation lookup, and another authorized receipt.
25
+ * Packing the actual statements also admits their binding and transport sizes. */
26
+ export function publicationReturnQueries(context: D1ImportContext, stage: StageBinding): number {
27
+ const receipt = publishedStreamReceipt(context, stage.id, stage.operation_key, stage.fingerprint).statements.length;
28
+ const discard = stagingStatements(context, 'create', discardStageWrites(stage)).length;
29
+ return receipt + discard + 1 + receipt;
30
+ }
31
+
32
+ /** Bounded fixed work outside the payload batches. Include the largest prior
33
+ * generation shape and the optional preparation lookup before its replacement. */
34
+ export function stagingControlQueries(context: D1ImportContext, stage: StageBinding): number {
35
+ const prior = { ...stage, revision: Number.MAX_SAFE_INTEGER, registered_revision: Number.MAX_SAFE_INTEGER, sealed_revision: Number.MAX_SAFE_INTEGER };
36
+ const packed = (statements: ReturnType<typeof stageRevisionWrites>) => stagingStatements(context, 'create', statements).length;
37
+ return 1 // Destination erasure-set admission read.
38
+ + 2 // Existing generation and optional preparation receipt.
39
+ + Math.max(packed(beginStageWrites(stage)), packed(beginStageWrites(stage, prior)))
40
+ + packed(stageRevisionWrites(stage, Number.MAX_SAFE_INTEGER, false))
41
+ + packed(stageRevisionWrites(stage, Number.MAX_SAFE_INTEGER, true))
42
+ + publicationReturnQueries(context, stage);
43
+ }
@@ -0,0 +1,28 @@
1
+ import type { D1ImportContext, D1Sql } from './types.ts';
2
+ import { guard, sql } from './database.ts';
3
+ import { jsonChunks } from './sets.ts';
4
+
5
+ /** Privacy authority belongs to the destination, including other importing
6
+ * principals. Only hash identities are read; no private payload is exposed. */
7
+ export async function stagedSourceIdentities(digest: string, context: D1ImportContext): Promise<string[]> {
8
+ const { results } = await context.storage.database.prepare('SELECT DISTINCT i.source_identity FROM ik_d1_staging_index i JOIN ik_d1_staging s ON s.id=i.stage_id WHERE s.destination=? AND i.target_identity=? UNION SELECT DISTINCT i.source_identity FROM ik_d1_native_staging_index i JOIN ik_d1_native_staging s ON s.id=i.stage_id WHERE s.destination=? AND i.target_identity=?').bind(context.destination, digest, context.destination, digest).all<{ source_identity: string }>();
9
+ return results.map(row => row.source_identity);
10
+ }
11
+ export function stageErasure(destination: string, digest: string, identities: string[], ownStage?:string): { before: D1Sql[]; after: D1Sql[]; final:D1Sql[] } {
12
+ // This closure guard catches an index registration or publication race that
13
+ // introduces a source not covered by the public-plan snapshot above.
14
+ const chunks = jsonChunks(identities, 1500000);
15
+ const listed = chunks.length ? chunks.map(() => 'i.source_identity IN (SELECT value FROM json_each(?))').join(' OR ') : '0';
16
+ const before:D1Sql[]=[],after:D1Sql[]=[],final:D1Sql[]=[];
17
+ for(const table of ['ik_d1_staging','ik_d1_native_staging']){
18
+ before.push(guard(sql(`NOT EXISTS(SELECT 1 FROM ${table}_index i JOIN ${table} s ON s.id=i.stage_id WHERE s.destination=? AND i.target_identity=? AND NOT (${listed}))`,destination,digest,...chunks)));
19
+ const exclude=ownStage&&table==='ik_d1_native_staging'?' AND id<>?':'', extra=exclude?[ownStage]:[];
20
+ for(const chunk of chunks){
21
+ after.push(sql(`DELETE FROM ${table} WHERE destination=? AND id IN (SELECT stage_id FROM ${table}_index WHERE source_identity IN (SELECT value FROM json_each(?)))${exclude}`,destination,chunk,...extra));
22
+ final.push(sql(`NOT EXISTS(SELECT 1 FROM ${table} s JOIN ${table}_index i ON i.stage_id=s.id WHERE s.destination=? AND i.source_identity IN (SELECT value FROM json_each(?)))`,destination,chunk));
23
+ }
24
+ after.push(sql(`DELETE FROM ${table} WHERE destination=? AND id IN (SELECT stage_id FROM ${table}_index WHERE target_identity=?)${exclude}`,destination,digest,...extra));
25
+ final.push(sql(`NOT EXISTS(SELECT 1 FROM ${table} s JOIN ${table}_index i ON i.stage_id=s.id WHERE s.destination=? AND i.target_identity=?)`,destination,digest));
26
+ }
27
+ return { before, after, final };
28
+ }
@@ -0,0 +1,49 @@
1
+ import type { D1ImportContext, D1Sql } from './types.ts';
2
+ import { authorize, sql } from './database.ts';
3
+ import { exactRows } from './sets.ts';
4
+ import { requestContext } from './budget.ts';
5
+ import { publishedStream } from './published-stream.ts';
6
+ import { beginStageWrites, discardStageWrites } from './staging-control.ts';
7
+ import { requireText } from '../provenance/identity.ts';
8
+ import { stagingAtomic, type StageBinding } from './staging-transport.ts';
9
+
10
+ export async function publishedStage(context: D1ImportContext, operation: string, fingerprint: string) {
11
+ const prior = await context.storage.database.prepare('SELECT id,fingerprint FROM ik_d1_streams WHERE destination=? AND principal_id=? AND operation_key=?').bind(context.destination, context.authorization.principalId, operation).first();
12
+ if (!prior) return undefined;
13
+ if (prior.fingerprint !== fingerprint) throw new Error('OPERATION_KEY_CONFLICT');
14
+ return publishedStream(context, prior.id, operation, fingerprint);
15
+ }
16
+
17
+ export async function beginStage(stage: StageBinding, context: D1ImportContext, preservePreparation = false, assertInput?: () => void): Promise<void> {
18
+ const prior = await context.storage.database.prepare('SELECT * FROM ik_d1_staging WHERE destination=? AND principal_id=? AND operation_key=?').bind(stage.destination, stage.principal_id, stage.operation_key).first();
19
+ if (prior && (prior.namespace !== stage.namespace || prior.mapping_version !== stage.mapping_version || prior.fingerprint !== stage.fingerprint)) throw new Error('OPERATION_KEY_CONFLICT');
20
+ if (prior && prior.reversal_policy !== stage.reversal_policy) throw new Error('REVERSAL_POLICY_CONFLICT');
21
+ if (prior && preservePreparation && prior.sealed_revision === prior.revision && await context.storage.database.prepare('SELECT stage_id FROM ik_d1_preparation_receipts WHERE stage_id=?').bind(prior.id).first()) throw new Error('IMPORT_PREPARATION_EXISTS');
22
+ assertInput?.();
23
+ await stagingAtomic(context, beginStageWrites(stage, prior));
24
+ }
25
+ export async function discardStage(stage: StageBinding, context: D1ImportContext): Promise<void> {
26
+ await stagingAtomic(context, discardStageWrites(stage));
27
+ }
28
+ /** Owner cancellation of private-only custody needs no native entity or job.
29
+ * It cannot erase another operator's generation or any published import. */
30
+ export async function cancelD1ImportStaging(operationKey: string, input: D1ImportContext): Promise<void> {
31
+ const context = requestContext(input); requireText(operationKey, 'operationKey');
32
+ await authorize(context, 'create');
33
+ // A preparation made in steps: its control row and every generation under its
34
+ // internal keys, recorded or left unrecorded by an interrupted step.
35
+ const steps = await context.storage.database.prepare('SELECT * FROM ik_d1_preparation_steps WHERE destination=? AND namespace=? AND principal_id=? AND operation_key=?').bind(context.destination, context.namespace, context.authorization.principalId, operationKey).first<any>();
36
+ if (steps) await stagingAtomic(context, [...exactRows('ik_d1_preparation_steps', Object.keys(steps), [steps]),
37
+ stepGenerations('DELETE FROM ik_d1_staging', steps), sql('DELETE FROM ik_d1_preparation_steps WHERE id=?', steps.id)]);
38
+ const stage = await context.storage.database.prepare('SELECT * FROM ik_d1_staging WHERE destination=? AND namespace=? AND principal_id=? AND operation_key=?').bind(context.destination, context.namespace, context.authorization.principalId, operationKey).first();
39
+ if (!stage) return;
40
+ if (context.reviewedFingerprint !== undefined && context.reviewedFingerprint !== stage.fingerprint) throw new Error('REVIEW_FINGERPRINT_MISMATCH');
41
+ await stagingAtomic(context, [...exactRows('ik_d1_staging', Object.keys(stage), [stage]), sql('DELETE FROM ik_d1_staging WHERE id=? AND generation=?', stage.id, stage.generation)]);
42
+ }
43
+
44
+ /** Every generation a preparation's steps staged: each has the internal key
45
+ * `<preparation id>:<step>` under the preparation's destination and principal. */
46
+ export function stepGenerations(statement: string, steps: { id: string; destination: string; principal_id: string }): D1Sql {
47
+ const prefix = `${steps.id}:`;
48
+ return sql(`${statement} WHERE destination=? AND principal_id=? AND substr(operation_key,1,?)=?`, steps.destination, steps.principal_id, prefix.length, prefix);
49
+ }
@@ -0,0 +1,15 @@
1
+ import type { D1PlanCodec } from './types.ts';
2
+ import { expandCompressedEnvelope } from './compressed-plan.ts';
3
+ import { canonicalJson } from '../provenance/identity.ts';
4
+ import { SNAPSHOT_BYTES } from './sets.ts';
5
+
6
+ /** Preserve existing public inline custody for small pages. The private stage
7
+ * still registers its complete privacy index before receiving these bytes. */
8
+ export function stagingPlan(body: string, codec?: D1PlanCodec): { body: string; inline: number } {
9
+ const envelope = JSON.parse(body);
10
+ const inline = Number((envelope.expandedSize ?? envelope.size) <= SNAPSHOT_BYTES);
11
+ return { body: inline && envelope.d1Plan !== 3 ? canonicalJson(expandCompressedEnvelope(body, envelope, codec)) : body, inline };
12
+ }
13
+
14
+ /** Only the bounded inline page is joined inside the publication transaction. */
15
+ export const publishedPlan = (page = 'p') => `CASE WHEN ${page}.inline_plan=1 THEN (SELECT group_concat(body,'') FROM (SELECT c.body FROM ik_d1_staging_chunks c WHERE c.stage_id=${page}.stage_id AND c.page_ordinal=${page}.ordinal ORDER BY c.ordinal)) ELSE ${page}.plan_json END`;
@@ -0,0 +1,22 @@
1
+ import { guardPredicate } from './guards.ts';
2
+ import type { D1ImportContext, D1Sql } from './types.ts';
3
+ import { authority, sql } from './database.ts';
4
+
5
+ /** A privacy observation and its current authority share one SQL snapshot.
6
+ * Denied reads never disclose whether a matching identity was erased. */
7
+ export async function stagingPrivacyClear(context: D1ImportContext, predicate: D1Sql): Promise<boolean> {
8
+ const authorized = guardPredicate(authority(context, 'create'))!;
9
+ const row = await context.storage.database.prepare(`WITH permission AS (SELECT CASE WHEN (${authorized.sql}) THEN 1 ELSE 0 END AS authorized) SELECT authorized,CASE WHEN authorized THEN (${predicate.sql}) ELSE NULL END AS clear FROM permission`)
10
+ .bind(...authorized.params ?? [], ...predicate.params ?? []).first();
11
+ if (!row || row.authorized !== 0 && row.authorized !== 1) throw new Error('IMPORT_D1_READ_RECEIPT_REQUIRED');
12
+ if (!row.authorized) throw new Error('IMPORT_ATOMIC_GUARD');
13
+ if (row.clear !== 0 && row.clear !== 1) throw new Error('IMPORT_D1_READ_RECEIPT_REQUIRED');
14
+ return row.clear === 1;
15
+ }
16
+
17
+ /** Complete admission separates already erased input from a later race.
18
+ * Every matching digest rejects; stageClear also fences the transaction. */
19
+ export async function assertStagingIdentitiesClear(context:D1ImportContext,destination:string,body:string,phase:'admission'|'staging'):Promise<void>{
20
+ const clear = await stagingPrivacyClear(context, sql("NOT EXISTS(SELECT 1 FROM json_each(?) i WHERE EXISTS(SELECT 1 FROM ik_d1_erased_sources s WHERE s.destination=? AND s.identity=json_extract(i.value,'$[2]')) OR EXISTS(SELECT 1 FROM ik_d1_erased_targets t WHERE t.destination=? AND t.identity=json_extract(i.value,'$[3]')))", body,destination,destination));
21
+ if(!clear)throw new Error(phase==='admission'?'IMPORT_SOURCE_ERASED':'IMPORT_ATOMIC_GUARD');
22
+ }
@@ -0,0 +1,79 @@
1
+ import { streamOriginsMatch } from './stream-origin.ts';
2
+ import type { D1Sql } from './types.ts';
3
+ import { guard, sql } from './database.ts';
4
+ import { bindingPredicate, stageClear, type StageBinding } from './staging-transport.ts';
5
+ import { reportHash } from './undo-state.ts';
6
+ import { publishedPlan } from './staging-plan.ts';
7
+
8
+ /** Membership and order are checked again at seal. Hashes were computed from
9
+ * reviewed bytes before insertion, then every stored column is read back and
10
+ * compared exactly. The expected revision fences those verified values. */
11
+ export function stageMembership(stage: StageBinding, payload: boolean): D1Sql {
12
+ return sql(`EXISTS(SELECT 1 FROM ik_d1_staging s WHERE s.id=? AND
13
+ (SELECT count(*) FROM ik_d1_staging_pages p WHERE p.stage_id=s.id)=s.pages AND
14
+ (SELECT coalesce(sum(total),0) FROM ik_d1_staging_pages p WHERE p.stage_id=s.id)=s.total AND
15
+ NOT EXISTS(SELECT 1 FROM ik_d1_staging_pages p WHERE p.stage_id=s.id AND
16
+ (p.ordinal<0 OR p.ordinal>=s.pages OR p.total<0 OR p.chunks<1 OR
17
+ json_extract(p.plan_json,'$.sha256') IS NOT p.body_hash OR json_extract(p.plan_json,'$.size') IS NOT p.body_size OR json_extract(p.plan_json,'$.chunks') IS NOT p.chunks OR
18
+ (SELECT count(*) FROM ik_d1_staging_index i WHERE i.stage_id=s.id AND i.page_ordinal=p.ordinal)<>p.identities OR
19
+ (SELECT count(DISTINCT ordinal) FROM ik_d1_staging_index i WHERE i.stage_id=s.id AND i.page_ordinal=p.ordinal)<>p.total
20
+ ${payload ? 'OR (SELECT count(*) FROM ik_d1_staging_chunks c WHERE c.stage_id=s.id AND c.page_ordinal=p.ordinal)<>p.chunks OR (SELECT coalesce(sum(body_size),0) FROM ik_d1_staging_chunks c WHERE c.stage_id=s.id AND c.page_ordinal=p.ordinal)<>p.body_size' : ''})) AND
21
+ NOT EXISTS(SELECT 1 FROM ik_d1_staging_index i LEFT JOIN ik_d1_staging_pages p ON p.stage_id=i.stage_id AND p.ordinal=i.page_ordinal WHERE i.stage_id=s.id AND (p.ordinal IS NULL OR i.ordinal<0 OR i.ordinal>=p.total))
22
+ ${payload ? 'AND NOT EXISTS(SELECT 1 FROM ik_d1_staging_chunks c LEFT JOIN ik_d1_staging_pages p ON p.stage_id=c.stage_id AND p.ordinal=c.page_ordinal WHERE c.stage_id=s.id AND (p.ordinal IS NULL OR c.ordinal<0 OR c.ordinal>=p.chunks OR c.body_size<>length(CAST(c.body AS BLOB))))' : ''})`, stage.id);
23
+ }
24
+
25
+ /** Small SQL-only publication: no plan/index body crosses the transport twice.
26
+ * Every public derived set is compared to its unchanged private exact source. */
27
+ export function stagePublication(stage: StageBinding, revision: number): D1Sql[] {
28
+ const id = stage.id;
29
+ return [guard(bindingPredicate(stage)), guard(sql('EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=? AND sealed_revision=? AND registered_revision IS NOT NULL)', id, revision, revision)), guard(stageMembership(stage, true)), guard(stageClear(stage)),
30
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_streams WHERE destination=? AND principal_id=? AND operation_key=?)', stage.destination, stage.principal_id, stage.operation_key)),
31
+ sql(`INSERT INTO ik_jobs(id,destination,namespace,mapping_version,principal_id,fingerprint,operation_key,plan_json,state,total,reversal_policy,reversal_policy_hash) SELECT p.job_id,s.destination,s.namespace,s.mapping_version,s.principal_id,p.fingerprint,s.id||':'||p.ordinal,${publishedPlan()},'queued',p.total,s.reversal_policy,p.policy_hash FROM ik_d1_staging_pages p JOIN ik_d1_staging s ON s.id=p.stage_id WHERE s.id=?`, id),
32
+ sql('INSERT INTO ik_d1_plan_chunks(job_id,ordinal,body) SELECT p.job_id,c.ordinal,c.body FROM ik_d1_staging_chunks c JOIN ik_d1_staging_pages p ON p.stage_id=c.stage_id AND p.ordinal=c.page_ordinal WHERE c.stage_id=? AND p.inline_plan=0', id),
33
+ sql('INSERT INTO ik_d1_plan_index(job_id,ordinal,source_identity,target_identity) SELECT p.job_id,i.ordinal,i.source_identity,i.target_identity FROM ik_d1_staging_index i JOIN ik_d1_staging_pages p ON p.stage_id=i.stage_id AND p.ordinal=i.page_ordinal WHERE i.stage_id=?', id),
34
+ sql("INSERT INTO ik_d1_streams(id,destination,namespace,mapping_version,principal_id,fingerprint,operation_key,total,layout_hash,state) SELECT id,destination,namespace,mapping_version,principal_id,fingerprint,operation_key,total,layout_hash,'applying' FROM ik_d1_staging WHERE id=?", id),
35
+ sql('INSERT INTO ik_d1_stream_pages(stream_id,ordinal,job_id,total) SELECT stage_id,ordinal,job_id,total FROM ik_d1_staging_pages WHERE stage_id=?', id),
36
+ sql("INSERT INTO ik_originals(job_id,kind,original_key,name,sha256,size) SELECT p.job_id,'artifact',o.original_key,o.name,o.sha256,o.size FROM ik_d1_staging_originals o JOIN ik_d1_staging_pages p ON p.stage_id=o.stage_id WHERE o.stage_id=?", id),
37
+ sql('INSERT INTO ik_original_sets(job_id,version) SELECT job_id,4 FROM ik_d1_staging_pages WHERE stage_id=?', id),
38
+ sql('INSERT INTO ik_d1_indexed_jobs(job_id,fingerprint) SELECT job_id,fingerprint FROM ik_d1_staging_pages WHERE stage_id=?', id),
39
+ sql("UPDATE ik_d1_streams SET state='applying' WHERE id=?", id),
40
+ sql('INSERT INTO ik_d1_publication_fences(stream_id,job_id) SELECT stage_id,job_id FROM ik_d1_staging_pages WHERE stage_id=?', id),
41
+ guard(sql('((SELECT count(*) FROM ik_d1_publication_fences WHERE stream_id=?)=(SELECT count(*) FROM ik_d1_staging_pages WHERE stage_id=?)) AND NOT EXISTS(SELECT 1 FROM ik_d1_staging_pages p WHERE p.stage_id=? AND NOT EXISTS(SELECT 1 FROM ik_d1_publication_fences f WHERE f.stream_id=p.stage_id AND f.job_id=p.job_id))', id, id, id)),
42
+ guard(sql(`NOT EXISTS(SELECT 1 FROM ik_d1_staging_pages p JOIN ik_d1_staging s ON s.id=p.stage_id WHERE s.id=? AND NOT EXISTS(SELECT 1 FROM ik_jobs j WHERE j.id=p.job_id AND j.destination=s.destination AND j.namespace=s.namespace AND j.mapping_version=s.mapping_version AND j.principal_id=s.principal_id AND j.fingerprint=p.fingerprint AND j.operation_key=s.id||':'||p.ordinal AND j.plan_json IS (${publishedPlan()}) AND j.total=p.total AND j.state='queued' AND j.checkpoint=0 AND j.fence=0 AND j.error IS NULL AND j.lease_token IS NULL AND j.lease_expires IS NULL AND j.reversal_policy=s.reversal_policy AND j.reversal_policy_hash=p.policy_hash AND j.reversal_cursor=0))`, id)),
43
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_jobs j JOIN ik_d1_staging_pages p ON p.job_id=j.id WHERE p.stage_id=? AND (j.reversal_hash<>? OR EXISTS(SELECT 1 FROM ik_d1_reversal_reports r WHERE r.job_id=j.id)))', id, reportHash([]))),
44
+ ...exactPublishedSets(id),
45
+ // The upstream capture trigger provides a second exact in-transaction copy.
46
+ // Release it only while its corresponding verified live chunk still exists.
47
+ sql('UPDATE ik_d1_creation_plans SET released=1 WHERE job_id IN (SELECT job_id FROM ik_d1_staging_pages WHERE stage_id=?)', id),
48
+ sql('DELETE FROM ik_d1_creation_plans WHERE job_id IN (SELECT job_id FROM ik_d1_staging_pages WHERE stage_id=?)', id),
49
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_creation_plans WHERE job_id IN (SELECT job_id FROM ik_d1_staging_pages WHERE stage_id=?))', id)),
50
+ sql('UPDATE ik_d1_custody_revisions SET seal=? WHERE job_id IN (SELECT job_id FROM ik_d1_staging_pages WHERE stage_id=?)',id,id),
51
+ guard(stageClear(stage)), guard(bindingPredicate(stage)), guard(sql('EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=? AND revision=? AND sealed_revision=?)', id, revision, revision)),
52
+ sql('DELETE FROM ik_d1_staging WHERE id=? AND generation=?', id, stage.generation),
53
+ // Public mutations (including cleanup-trigger effects) are prohibited by
54
+ // the exact identity fence until the final transaction authority check.
55
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=?)', id))];
56
+ }
57
+
58
+ function exactPublishedSets(id: string): D1Sql[] {
59
+ return [
60
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging_pages p WHERE p.stage_id=? AND ((SELECT count(*) FROM ik_d1_plan_chunks c WHERE c.job_id=p.job_id)<>CASE WHEN p.inline_plan=1 THEN 0 ELSE p.chunks END OR (SELECT count(*) FROM ik_d1_creation_plans c WHERE c.job_id=p.job_id AND c.released=0)<>CASE WHEN p.inline_plan=1 THEN 0 ELSE p.chunks END OR (SELECT count(*) FROM ik_d1_plan_index i WHERE i.job_id=p.job_id)<>p.identities))', id)),
61
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging_chunks c JOIN ik_d1_staging_pages p ON p.stage_id=c.stage_id AND p.ordinal=c.page_ordinal WHERE c.stage_id=? AND p.inline_plan=0 AND (NOT EXISTS(SELECT 1 FROM ik_d1_plan_chunks live WHERE live.job_id=p.job_id AND live.ordinal=c.ordinal AND live.body IS c.body) OR NOT EXISTS(SELECT 1 FROM ik_d1_creation_plans live WHERE live.job_id=p.job_id AND live.ordinal=c.ordinal AND live.body IS c.body AND live.released=0)))', id)),
62
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging_index i JOIN ik_d1_staging_pages p ON p.stage_id=i.stage_id AND p.ordinal=i.page_ordinal WHERE i.stage_id=? AND NOT EXISTS(SELECT 1 FROM ik_d1_plan_index live WHERE live.job_id=p.job_id AND live.ordinal=i.ordinal AND live.source_identity=i.source_identity AND live.target_identity=i.target_identity))', id)),
63
+ guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging_pages p WHERE p.stage_id=? AND ((SELECT count(*) FROM ik_originals o WHERE o.job_id=p.job_id)<>(SELECT count(*) FROM ik_d1_staging_originals o WHERE o.stage_id=p.stage_id) OR EXISTS(SELECT 1 FROM ik_d1_original_chunks c WHERE c.job_id=p.job_id) OR NOT EXISTS(SELECT 1 FROM ik_original_sets o WHERE o.job_id=p.job_id AND o.version=4) OR NOT EXISTS(SELECT 1 FROM ik_d1_indexed_jobs i WHERE i.job_id=p.job_id AND i.fingerprint=p.fingerprint)))', id)),
64
+ guard(sql("NOT EXISTS(SELECT 1 FROM ik_d1_staging_originals o JOIN ik_d1_staging_pages p ON p.stage_id=o.stage_id WHERE o.stage_id=? AND NOT EXISTS(SELECT 1 FROM ik_originals live WHERE live.job_id=p.job_id AND live.kind='artifact' AND live.original_key=o.original_key AND live.name IS o.name AND live.sha256=o.sha256 AND live.size=o.size AND live.bytes IS NULL))", id)),
65
+ guard(sql(`EXISTS(SELECT 1 FROM ik_d1_staging s JOIN ik_d1_streams live ON live.id=s.id WHERE s.id=? AND live.destination=s.destination AND live.namespace=s.namespace AND live.mapping_version=s.mapping_version AND live.principal_id=s.principal_id AND live.fingerprint=s.fingerprint AND live.operation_key=s.operation_key AND live.total=s.total AND live.layout_hash=s.layout_hash AND live.state='applying' AND ${streamOriginsMatch('live')} AND (SELECT count(*) FROM ik_d1_stream_pages WHERE stream_id=s.id)=s.pages AND NOT EXISTS(SELECT 1 FROM ik_d1_staging_pages p WHERE p.stage_id=s.id AND NOT EXISTS(SELECT 1 FROM ik_d1_stream_pages l WHERE l.stream_id=s.id AND l.ordinal=p.ordinal AND l.job_id=p.job_id AND l.total=p.total AND l.erased_records=0)))`, id)),
66
+ ];
67
+ }
68
+
69
+ /** Known receipts fence the already exact-checked public sets after all private
70
+ * bytes and publication fences are deleted. Nothing writes after these checks. */
71
+ export function publishedStagePredicates(stage:StageBinding,pages:{ordinal:number;job_id:string;total:number}[]):D1Sql[]{
72
+ const binding={id:stage.id,destination:stage.destination,namespace:stage.namespace,mapping_version:stage.mapping_version,principal_id:stage.principal_id,fingerprint:stage.fingerprint,operation_key:stage.operation_key,total:stage.total,layout_hash:stage.layout_hash,state:'applying'};
73
+ const rows=JSON.stringify(pages.map(({ordinal,job_id,total})=>[ordinal,job_id,total]));
74
+ return [sql(`EXISTS(SELECT 1 FROM ik_d1_streams live WHERE ${Object.keys(binding).map(key=>`live.${key} IS ?`).join(' AND ')} AND ${streamOriginsMatch('live')})`,...Object.values(binding)),
75
+ sql(`(SELECT count(*) FROM ik_d1_stream_pages WHERE stream_id=?)=? AND NOT EXISTS(SELECT 1 FROM json_each(?) expected WHERE NOT EXISTS(SELECT 1 FROM ik_d1_stream_pages p JOIN ik_d1_custody_revisions r ON r.job_id=p.job_id JOIN ik_d1_custody_states c ON c.job_id=p.job_id WHERE p.stream_id=? AND p.ordinal=json_extract(expected.value,'$[0]') AND p.job_id=json_extract(expected.value,'$[1]') AND p.total=json_extract(expected.value,'$[2]') AND p.erased_records=0 AND typeof(c.revision)='integer' AND c.revision>=0 AND r.seal=?))`,stage.id,pages.length,rows,stage.id,stage.id),
76
+ sql('NOT EXISTS(SELECT 1 FROM ik_d1_plan_index i JOIN ik_d1_stream_pages p ON p.job_id=i.job_id WHERE p.stream_id=? AND (EXISTS(SELECT 1 FROM ik_d1_erased_sources s WHERE s.destination=? AND s.identity=i.source_identity) OR EXISTS(SELECT 1 FROM ik_d1_erased_targets t WHERE t.destination=? AND t.identity=i.target_identity)))',stage.id,stage.destination,stage.destination),
77
+ sql('NOT EXISTS(SELECT 1 FROM ik_d1_creation_plans c JOIN ik_d1_stream_pages p ON p.job_id=c.job_id WHERE p.stream_id=?)',stage.id),
78
+ sql('NOT EXISTS(SELECT 1 FROM ik_d1_staging WHERE id=?) AND NOT EXISTS(SELECT 1 FROM ik_d1_publication_fences WHERE stream_id=?)',stage.id,stage.id)];
79
+ }