@runeditrun/rer 0.2.76 → 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.
- package/README.md +32 -0
- package/cli/README.md +55 -0
- package/cli/contract.mjs +1 -1
- package/cli/deploy-command.mjs +4 -2
- package/cli/deploy-inputs.mjs +15 -3
- package/cli/deploy-installation-key.mjs +71 -0
- package/cli/deploy-secrets.mjs +2 -0
- package/cli/deploy.mjs +13 -2
- package/cli/deployment-record.mjs +2 -0
- package/cli/export.mjs +1 -1
- package/cli/main.mjs +1 -1
- package/cli/parse.mjs +3 -2
- package/cli/portability-migrations.mjs +35 -14
- package/cli/portability-worker.mjs +2 -2
- package/cli/portability.mjs +2 -2
- package/cli/runtime-front-door.mjs +1 -1
- package/global/CHANGELOG.md +14 -0
- package/global/README.md +32 -0
- package/modules/dependencies/README.md +71 -1
- package/modules/dependencies/auth.mjs +4 -4
- package/modules/dependencies/crypto.mjs +6 -1
- package/modules/dependencies/custody.mjs +16 -0
- package/modules/dependencies/index.d.mts +1 -1
- package/modules/dependencies/index.mjs +6 -5
- package/modules/dependencies/schema/keys-sealed.sql +4 -0
- package/modules/dependencies/sealing.mjs +55 -0
- package/modules/dependencies/store.mjs +62 -9
- package/modules/deploy-kit/README.md +1 -0
- package/modules/deploy-kit/owner-cli.mjs +19 -7
- package/modules/deploy-kit/owner-rekey.mjs +69 -0
- package/modules/deploy-kit/provider.mjs +9 -4
- package/modules/import-kit/src/d1/README.md +1322 -0
- package/modules/import-kit/src/d1/apply.ts +115 -0
- package/modules/import-kit/src/d1/budget.ts +89 -0
- package/modules/import-kit/src/d1/compressed-plan.ts +63 -0
- package/modules/import-kit/src/d1/creation.ts +95 -0
- package/modules/import-kit/src/d1/custody-fence.ts +27 -0
- package/modules/import-kit/src/d1/custody-state-schema.ts +67 -0
- package/modules/import-kit/src/d1/database.ts +163 -0
- package/modules/import-kit/src/d1/entry-locator.ts +61 -0
- package/modules/import-kit/src/d1/erased-artifacts.ts +38 -0
- package/modules/import-kit/src/d1/erased-custody.ts +36 -0
- package/modules/import-kit/src/d1/erased-plan.ts +47 -0
- package/modules/import-kit/src/d1/erased-reversal.ts +19 -0
- package/modules/import-kit/src/d1/erasure-bounded.ts +22 -0
- package/modules/import-kit/src/d1/erasure-claims.ts +27 -0
- package/modules/import-kit/src/d1/erasure-compiler.ts +67 -0
- package/modules/import-kit/src/d1/erasure-disposition.ts +16 -0
- package/modules/import-kit/src/d1/erasure-fragments.ts +86 -0
- package/modules/import-kit/src/d1/erasure-index.ts +53 -0
- package/modules/import-kit/src/d1/erasure-pages.ts +62 -0
- package/modules/import-kit/src/d1/erasure-progress-schema.ts +8 -0
- package/modules/import-kit/src/d1/erasure-progress.ts +61 -0
- package/modules/import-kit/src/d1/erasure-related.ts +124 -0
- package/modules/import-kit/src/d1/erasure-report.ts +31 -0
- package/modules/import-kit/src/d1/erasure-schema.ts +24 -0
- package/modules/import-kit/src/d1/erasure-selection.ts +77 -0
- package/modules/import-kit/src/d1/erasure-snapshot.ts +15 -0
- package/modules/import-kit/src/d1/erasure-stage.ts +146 -0
- package/modules/import-kit/src/d1/erasure-step.ts +12 -0
- package/modules/import-kit/src/d1/erasure-superseded.ts +98 -0
- package/modules/import-kit/src/d1/erasure-values.ts +83 -0
- package/modules/import-kit/src/d1/erasure-writes.ts +46 -0
- package/modules/import-kit/src/d1/erasure.ts +170 -0
- package/modules/import-kit/src/d1/evolutions/0001-preserved-undo.ts +13 -0
- package/modules/import-kit/src/d1/expected-state.ts +91 -0
- package/modules/import-kit/src/d1/field-redaction.ts +86 -0
- package/modules/import-kit/src/d1/guards.ts +37 -0
- package/modules/import-kit/src/d1/history.ts +71 -0
- package/modules/import-kit/src/d1/index.ts +88 -0
- package/modules/import-kit/src/d1/integrity.ts +127 -0
- package/modules/import-kit/src/d1/inventory-snapshot.ts +84 -0
- package/modules/import-kit/src/d1/jobs.ts +97 -0
- package/modules/import-kit/src/d1/native-set.ts +48 -0
- package/modules/import-kit/src/d1/native-staging-admission.ts +19 -0
- package/modules/import-kit/src/d1/native-staging-context.ts +44 -0
- package/modules/import-kit/src/d1/native-staging-lifecycle.ts +33 -0
- package/modules/import-kit/src/d1/native-staging-offset.ts +74 -0
- package/modules/import-kit/src/d1/native-staging-publication.ts +18 -0
- package/modules/import-kit/src/d1/native-staging-rows.ts +72 -0
- package/modules/import-kit/src/d1/native-staging-schema.ts +28 -0
- package/modules/import-kit/src/d1/native-staging-state.ts +22 -0
- package/modules/import-kit/src/d1/native-staging.ts +64 -0
- package/modules/import-kit/src/d1/original-copies.ts +53 -0
- package/modules/import-kit/src/d1/original-storage.ts +56 -0
- package/modules/import-kit/src/d1/originals.ts +74 -0
- package/modules/import-kit/src/d1/plan-dictionary.ts +80 -0
- package/modules/import-kit/src/d1/plans.ts +121 -0
- package/modules/import-kit/src/d1/preparation-fingerprint.ts +57 -0
- package/modules/import-kit/src/d1/preparation-header.ts +33 -0
- package/modules/import-kit/src/d1/preparation-reader.ts +98 -0
- package/modules/import-kit/src/d1/preparation-schema.ts +16 -0
- package/modules/import-kit/src/d1/preparation-state.ts +56 -0
- package/modules/import-kit/src/d1/preparation-step-combine.ts +80 -0
- package/modules/import-kit/src/d1/preparation-step-control.ts +48 -0
- package/modules/import-kit/src/d1/preparation-step-hash.ts +131 -0
- package/modules/import-kit/src/d1/preparation-step-pages.ts +77 -0
- package/modules/import-kit/src/d1/preparation-steps-schema.ts +11 -0
- package/modules/import-kit/src/d1/preparation-steps.ts +102 -0
- package/modules/import-kit/src/d1/preparations.ts +87 -0
- package/modules/import-kit/src/d1/preserved-preview.ts +89 -0
- package/modules/import-kit/src/d1/preserved-reverse.ts +91 -0
- package/modules/import-kit/src/d1/provenance-set.ts +87 -0
- package/modules/import-kit/src/d1/publication-fence.ts +22 -0
- package/modules/import-kit/src/d1/publication-receipt-schema.ts +15 -0
- package/modules/import-kit/src/d1/published-stream.ts +11 -0
- package/modules/import-kit/src/d1/read-fence.ts +64 -0
- package/modules/import-kit/src/d1/read-results.ts +16 -0
- package/modules/import-kit/src/d1/redacted-custody.ts +76 -0
- package/modules/import-kit/src/d1/redaction-step.ts +125 -0
- package/modules/import-kit/src/d1/referenced-plans.ts +136 -0
- package/modules/import-kit/src/d1/retained-values.ts +35 -0
- package/modules/import-kit/src/d1/retirement-bounded.ts +71 -0
- package/modules/import-kit/src/d1/retirement-door.ts +105 -0
- package/modules/import-kit/src/d1/retirement-schema.ts +39 -0
- package/modules/import-kit/src/d1/retirement-scope.ts +94 -0
- package/modules/import-kit/src/d1/retirement-steps.ts +76 -0
- package/modules/import-kit/src/d1/retirement-tally.ts +74 -0
- package/modules/import-kit/src/d1/retirement.ts +136 -0
- package/modules/import-kit/src/d1/reversal-conversion-record.ts +19 -0
- package/modules/import-kit/src/d1/reversal-conversion-schema.ts +11 -0
- package/modules/import-kit/src/d1/reversal-conversion.ts +40 -0
- package/modules/import-kit/src/d1/reversal-fence-schema.ts +14 -0
- package/modules/import-kit/src/d1/reversal-fence.ts +106 -0
- package/modules/import-kit/src/d1/reversal-journal.ts +24 -0
- package/modules/import-kit/src/d1/reverse.ts +103 -0
- package/modules/import-kit/src/d1/reviewed-source.ts +23 -0
- package/modules/import-kit/src/d1/sets.ts +78 -0
- package/modules/import-kit/src/d1/staged-creation.ts +201 -0
- package/modules/import-kit/src/d1/staging-admission.ts +45 -0
- package/modules/import-kit/src/d1/staging-control.ts +43 -0
- package/modules/import-kit/src/d1/staging-erasure.ts +28 -0
- package/modules/import-kit/src/d1/staging-lifecycle.ts +49 -0
- package/modules/import-kit/src/d1/staging-plan.ts +15 -0
- package/modules/import-kit/src/d1/staging-privacy.ts +22 -0
- package/modules/import-kit/src/d1/staging-publication.ts +79 -0
- package/modules/import-kit/src/d1/staging-schema.ts +37 -0
- package/modules/import-kit/src/d1/staging-statements.ts +8 -0
- package/modules/import-kit/src/d1/staging-transport.ts +70 -0
- package/modules/import-kit/src/d1/stream-boundary.ts +11 -0
- package/modules/import-kit/src/d1/stream-create.ts +55 -0
- package/modules/import-kit/src/d1/stream-identity-admission.ts +37 -0
- package/modules/import-kit/src/d1/stream-input.ts +18 -0
- package/modules/import-kit/src/d1/stream-layout.ts +43 -0
- package/modules/import-kit/src/d1/stream-origin.ts +44 -0
- package/modules/import-kit/src/d1/stream-page-integrity.ts +18 -0
- package/modules/import-kit/src/d1/stream-partition.ts +50 -0
- package/modules/import-kit/src/d1/stream-plan.ts +133 -0
- package/modules/import-kit/src/d1/stream-receipt.ts +45 -0
- package/modules/import-kit/src/d1/stream-revision-schema.ts +35 -0
- package/modules/import-kit/src/d1/stream-snapshot.ts +26 -0
- package/modules/import-kit/src/d1/stream-stage.ts +79 -0
- package/modules/import-kit/src/d1/stream-state.ts +25 -0
- package/modules/import-kit/src/d1/stream-working-set.ts +30 -0
- package/modules/import-kit/src/d1/streams.ts +39 -0
- package/modules/import-kit/src/d1/target-guards.ts +9 -0
- package/modules/import-kit/src/d1/target-identities.ts +17 -0
- package/modules/import-kit/src/d1/transport.ts +10 -0
- package/modules/import-kit/src/d1/types.ts +162 -0
- package/modules/import-kit/src/d1/undo-observations.ts +39 -0
- package/modules/import-kit/src/d1/undo-report.ts +22 -0
- package/modules/import-kit/src/d1/undo-snapshot.ts +87 -0
- package/modules/import-kit/src/d1/undo-state.ts +84 -0
- package/modules/import-kit/src/d1/undo-types.ts +110 -0
- package/modules/import-kit/src/journal/authority.ts +12 -0
- package/modules/import-kit/src/plan/budget.ts +60 -0
- package/modules/import-kit/src/plan/index.ts +123 -0
- package/modules/import-kit/src/plan/size.ts +122 -0
- package/modules/import-kit/src/provenance/canonical-hash.ts +130 -0
- package/modules/import-kit/src/provenance/canonical-stream.ts +85 -0
- package/modules/import-kit/src/provenance/credential-names.ts +131 -0
- package/modules/import-kit/src/provenance/credentials.ts +317 -0
- package/modules/import-kit/src/provenance/identity.ts +51 -0
- package/modules/import-kit/src/provenance/json-snapshot.ts +21 -0
- package/modules/import-kit/src/provenance/resumable-sha256.ts +75 -0
- package/modules/import-kit/src/provenance/validate-json.ts +19 -0
- package/modules/import-kit/src/storage/originals.ts +87 -0
- package/modules/import-kit/src/storage/provenance.ts +64 -0
- package/modules/import-kit/src/storage/schema.ts +42 -0
- package/modules/import-kit/src/types.ts +47 -0
- package/modules/operator-auth/docs/native-accounts.md +4 -1
- package/modules/operator-auth/identity/oidc/aes-gcm.mjs +21 -0
- package/modules/operator-auth/identity/oidc/crypto.mjs +5 -12
- package/modules/operator-auth/index.d.mts +1 -1
- package/modules/portability/README.md +98 -2
- package/modules/portability/backfills.mjs +42 -15
- package/modules/portability/ledgers.mjs +7 -1
- package/modules/portability/transient-tables.mjs +40 -0
- package/onboarding/local/environment.mjs +2 -2
- package/package.json +3 -1
- package/scripts/publish-check/reviewed.py +3 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { d1StreamOriginSchema } from './stream-origin.ts';
|
|
2
|
+
import { d1ReversalPolicyConversionSchema } from './reversal-conversion-schema.ts';
|
|
3
|
+
import { d1PreparationSchema } from './preparation-schema.ts';
|
|
4
|
+
import { d1PreparationStepsSchema } from './preparation-steps-schema.ts';
|
|
5
|
+
import { stageNativeTransaction } from './native-staging.ts';
|
|
6
|
+
import type { NativeStagingScope } from './native-staging-context.ts';
|
|
7
|
+
import { d1NativeStagingSchema } from './native-staging-schema.ts';
|
|
8
|
+
import { d1CustodyStateSchema, d1CustodyStateReplaceSchema } from './custody-state-schema.ts';
|
|
9
|
+
import { d1ReversalFenceSchema } from './reversal-fence-schema.ts';
|
|
10
|
+
import { batchBytes } from './transport.ts';
|
|
11
|
+
import { D1RequestLimitError, packGuards, remaining } from './budget.ts';
|
|
12
|
+
import { d1ErasureSchema } from './erasure-schema.ts';
|
|
13
|
+
import { d1RetirementSchema } from './retirement-schema.ts';
|
|
14
|
+
import { d1ErasureProgressSchema } from './erasure-progress-schema.ts';
|
|
15
|
+
import { schema } from '../storage/schema.ts';
|
|
16
|
+
import { requireText } from '../provenance/identity.ts';
|
|
17
|
+
import type { ImportAction } from '../types.ts';
|
|
18
|
+
import type { D1Database, D1ImportContext, D1Sql, D1Statement } from './types.ts';
|
|
19
|
+
import { guard, guardPredicate } from './guards.ts';
|
|
20
|
+
import { batchRows, checkBatchResults } from './read-results.ts';
|
|
21
|
+
import { streamBoundary } from './stream-boundary.ts';
|
|
22
|
+
import { jobLayoutGuards } from './stream-layout.ts';
|
|
23
|
+
export { guard } from './guards.ts';
|
|
24
|
+
import { preservedUndoEvolution } from './evolutions/0001-preserved-undo.ts';
|
|
25
|
+
import { d1StagingSchema, d1LegacyStreamReceiptUpgrade } from './staging-schema.ts';
|
|
26
|
+
import { ExpectedState } from './expected-state.ts';
|
|
27
|
+
import { custodyGuards } from './original-storage.ts';
|
|
28
|
+
import { planGuards } from './plans.ts';
|
|
29
|
+
import { planIndex,erasedIndexGuards } from './erasure-index.ts';
|
|
30
|
+
import { exactRows } from './sets.ts';
|
|
31
|
+
import { readBoundedGuards } from './read-fence.ts';
|
|
32
|
+
|
|
33
|
+
const baseSchema = [...schema.split(';').map(s => s.trim()).filter(Boolean),
|
|
34
|
+
'CREATE TABLE IF NOT EXISTS ik_d1_plan_chunks (job_id TEXT NOT NULL REFERENCES ik_jobs(id), ordinal INTEGER NOT NULL, body TEXT NOT NULL, PRIMARY KEY(job_id,ordinal))',
|
|
35
|
+
'CREATE TABLE IF NOT EXISTS ik_d1_original_chunks (job_id TEXT NOT NULL REFERENCES ik_jobs(id), kind TEXT NOT NULL, original_key TEXT NOT NULL, ordinal INTEGER NOT NULL, bytes BLOB NOT NULL, PRIMARY KEY(job_id,kind,original_key,ordinal))',
|
|
36
|
+
'CREATE TABLE IF NOT EXISTS ik_d1_history (id TEXT PRIMARY KEY, job_id TEXT NOT NULL REFERENCES ik_jobs(id), ordinal INTEGER NOT NULL, effect_ordinal INTEGER NOT NULL, source_identity TEXT NOT NULL, destination TEXT NOT NULL, target_kind TEXT NOT NULL, target_id TEXT NOT NULL, reversed INTEGER NOT NULL CHECK(reversed=0), record_json TEXT NOT NULL, successor_id TEXT NOT NULL UNIQUE)',
|
|
37
|
+
'CREATE INDEX IF NOT EXISTS ik_d1_history_job ON ik_d1_history(job_id)',
|
|
38
|
+
'CREATE TABLE IF NOT EXISTS ik_d1_entries (job_id TEXT NOT NULL REFERENCES ik_jobs(id), ordinal INTEGER NOT NULL, source_identity TEXT NOT NULL, owner_job_id TEXT NOT NULL REFERENCES ik_jobs(id), effects_hash TEXT NOT NULL, PRIMARY KEY(job_id,ordinal))',
|
|
39
|
+
'CREATE TABLE IF NOT EXISTS ik_d1_assertions (ok INTEGER NOT NULL CONSTRAINT IMPORT_ATOMIC_GUARD CHECK(ok=1))'];
|
|
40
|
+
export const d1Schema = [...baseSchema, ...preservedUndoEvolution];
|
|
41
|
+
export { preservedUndoEvolution };
|
|
42
|
+
export async function initializeD1ImportStorage(database: D1Database): Promise<void> {
|
|
43
|
+
await database.batch(baseSchema.map(sql => database.prepare(sql)));
|
|
44
|
+
const columns = await database.prepare('PRAGMA table_info(ik_jobs)').all();
|
|
45
|
+
const installed = ['reversal_policy','reversal_policy_hash','reversal_cursor','reversal_hash'].filter(name => columns.results.some(c => c.name === name));
|
|
46
|
+
const report = await database.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='ik_d1_reversal_reports'").first();
|
|
47
|
+
const creation = await database.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='ik_d1_creation_plans'").first();
|
|
48
|
+
const triggers = await database.prepare("SELECT COUNT(*) AS n FROM sqlite_master WHERE type='trigger' AND name IN ('ik_d1_creation_plan_capture','ik_d1_creation_plan_immutable','ik_d1_creation_plan_release')").first();
|
|
49
|
+
if (!installed.length && !report && !creation && triggers?.n === 0) await database.batch(preservedUndoEvolution.map(sql => database.prepare(sql)));
|
|
50
|
+
else if (installed.length !== 4 || !report || !creation || triggers?.n !== 3) throw new Error('IMPORT_SCHEMA_EVOLUTION_INCOMPLETE');
|
|
51
|
+
await database.batch(d1ErasureSchema.map(sql => database.prepare(sql)));
|
|
52
|
+
await database.batch([...d1StreamOriginSchema, ...d1LegacyStreamReceiptUpgrade].map(sql => database.prepare(sql)));
|
|
53
|
+
await database.batch(d1StagingSchema.map(sql => database.prepare(sql)));
|
|
54
|
+
await database.batch(d1NativeStagingSchema.map(sql => database.prepare(sql)));
|
|
55
|
+
await database.batch(d1CustodyStateSchema.map(sql => database.prepare(sql)));
|
|
56
|
+
await database.batch(d1ReversalFenceSchema.map(sql => database.prepare(sql)));
|
|
57
|
+
await database.batch(d1PreparationSchema.map(sql => database.prepare(sql)));
|
|
58
|
+
await database.batch(d1PreparationStepsSchema.map(sql => database.prepare(sql)));
|
|
59
|
+
await database.batch(d1RetirementSchema.map(sql => database.prepare(sql)));
|
|
60
|
+
await database.batch(d1CustodyStateReplaceSchema.map(sql => database.prepare(sql)));
|
|
61
|
+
await database.batch(d1ErasureProgressSchema.map(sql => database.prepare(sql)));
|
|
62
|
+
const conversionInstalled = columns.results.some(c => c.name === 'reversal_conversion_hash');
|
|
63
|
+
await database.batch(d1ReversalPolicyConversionSchema.slice(conversionInstalled ? 1 : 0).map(sql => database.prepare(sql)));
|
|
64
|
+
}
|
|
65
|
+
export const sql = (sql: string, ...params: any[]): D1Sql => ({ sql, params });
|
|
66
|
+
export const prepare = (db: D1Database, query: D1Sql): D1Statement => db.prepare(query.sql).bind(...(query.params ?? []));
|
|
67
|
+
|
|
68
|
+
export function authority(context: Pick<D1ImportContext, 'destination' | 'namespace' | 'mappingVersion' | 'authorization'>, action: ImportAction): D1Sql {
|
|
69
|
+
for (const name of ['destination', 'namespace', 'mappingVersion'] as const) requireText(context[name], name);
|
|
70
|
+
requireText(context.authorization.principalId, 'principalId');
|
|
71
|
+
const predicate = context.authorization.predicate(action, context.destination);
|
|
72
|
+
if (!predicate || typeof predicate.sql !== 'string' || !predicate.sql.trim()) throw new Error('AUTHORIZATION_PREDICATE_REQUIRED');
|
|
73
|
+
return guard(predicate);
|
|
74
|
+
}
|
|
75
|
+
/** Final checks are read-only: no assertion cleanup can mutate state afterward.
|
|
76
|
+
* SQLite evaluates only the selected CASE branch; malformed JSON aborts the
|
|
77
|
+
* whole batch when the exact predicate is false or NULL. */
|
|
78
|
+
export function finalGuard(predicate: D1Sql): D1Sql {
|
|
79
|
+
predicate=guardPredicate(predicate)??predicate;
|
|
80
|
+
const prefix="SELECT CASE WHEN (",suffix=") THEN 1 ELSE json('IMPORT_FINAL_GUARD') END";
|
|
81
|
+
const result=sql(prefix+predicate.sql+suffix,...predicate.params??[]);
|
|
82
|
+
if(predicate.retainedValues){const parts=[...predicate.retainedValues.parts];parts[0]=prefix+parts[0];parts[parts.length-1]+=suffix;result.retainedValues={...predicate.retainedValues,parts,indexes:[...predicate.retainedValues.indexes]};}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
export function finalAuthority(context: Parameters<typeof authority>[0], action: ImportAction): D1Sql {
|
|
86
|
+
authority(context,action); // Validate the authorization contract as usual.
|
|
87
|
+
return finalGuard(context.authorization.predicate(action,context.destination));
|
|
88
|
+
}
|
|
89
|
+
export const readonlyChecks = (guards:D1Sql[]):D1Sql[] => packGuards(guards).map(finalGuard);
|
|
90
|
+
export function atomicStatements(context: Parameters<typeof authority>[0] & Pick<D1ImportContext,'maxBatchBytes'>, action: ImportAction, statements: D1Sql[]): D1Sql[] {
|
|
91
|
+
return [...packGuards([authority(context, action), ...statements, authority(context, action), sql('DELETE FROM ik_d1_assertions')]),...(context.maxBatchBytes===undefined?[]:[finalAuthority(context,action)])];
|
|
92
|
+
}
|
|
93
|
+
export async function atomic(context: D1ImportContext, action: ImportAction, statements: D1Sql[], returnReads = 0, scope?: NativeStagingScope): Promise<void> {
|
|
94
|
+
const {packed,post,count}=atomicPreparation(context,action,statements,scope,returnReads);
|
|
95
|
+
if (count > remaining(context)) throw new D1RequestLimitError();
|
|
96
|
+
if (!withinBatchBytes(context, packed)) {
|
|
97
|
+
if (!scope) throw new Error('IMPORT_D1_TRANSPORT_LIMIT');
|
|
98
|
+
await stageNativeTransaction(context, action, statements, {...scope,postGuards:post}, returnReads); return;
|
|
99
|
+
}
|
|
100
|
+
await context.storage.database.batch(packed.map(s => prepare(context.storage.database, s)));
|
|
101
|
+
}
|
|
102
|
+
function postConditions(context:Pick<D1ImportContext,'maxBatchBytes'>,statements:D1Sql[],scope?:NativeStagingScope,action:ImportAction='read'):D1Sql[]{
|
|
103
|
+
const stream = scope ? [...jobLayoutGuards(scope.job), ...streamBoundary(scope.job.id, action)] : [];
|
|
104
|
+
if(context.maxBatchBytes===undefined)return [...stream,...statements.flatMap(statement=>statement.finalChecks??[])];
|
|
105
|
+
const readOnly=statements.every(s=>guardPredicate(s));
|
|
106
|
+
const post=[...stream,...(readOnly?statements:new ExpectedState(statements).guards()),...statements.flatMap(statement=>statement.finalChecks??[])];
|
|
107
|
+
if(scope){
|
|
108
|
+
const plans=new Map([[scope.job.id,scope.plan]]);
|
|
109
|
+
for(const statement of statements){const values=statement.retainedValues;if(values?.privacyPlan&&values.privacyJobId&&!plans.has(values.privacyJobId))plans.set(values.privacyJobId,values.privacyPlan);}
|
|
110
|
+
for(const [id,plan] of plans)post.push(...erasedIndexGuards(plan),...custodyGuards(plan,id),...planGuards(plan,id),...exactRows('ik_d1_plan_index',['job_id','ordinal','source_identity','target_identity'],planIndex(plan,id),sql('job_id=?',id),undefined,true),guard(sql('EXISTS(SELECT 1 FROM ik_d1_indexed_jobs WHERE job_id=? AND fingerprint=?)',id,plan.fingerprint)));
|
|
111
|
+
post.push(...scope.postGuards);
|
|
112
|
+
}
|
|
113
|
+
return post;
|
|
114
|
+
}
|
|
115
|
+
export function withinBatchBytes(context: D1ImportContext, packed: D1Sql[]): boolean {
|
|
116
|
+
const limit = context.maxBatchBytes;
|
|
117
|
+
if (limit === undefined) return true;
|
|
118
|
+
if (!Number.isSafeInteger(limit) || limit < 1) throw new Error('INVALID_TRANSPORT_LIMIT');
|
|
119
|
+
return batchBytes(packed) <= limit;
|
|
120
|
+
}
|
|
121
|
+
/** Includes both current-authority predicates and the eventual progress write. */
|
|
122
|
+
export const fitsBatch = (context:D1ImportContext,action:ImportAction,statements:D1Sql[],scope?:NativeStagingScope):boolean => context.maxBatchBytes===undefined||withinBatchBytes(context,preparedAtomic(context,action,statements,scope));
|
|
123
|
+
export function preparedAtomic(context:Parameters<typeof authority>[0]&Pick<D1ImportContext,'maxBatchBytes'>,action:ImportAction,statements:D1Sql[],scope?:NativeStagingScope):D1Sql[]{
|
|
124
|
+
return atomicPreparation(context,action,statements,scope).packed;
|
|
125
|
+
}
|
|
126
|
+
/** The statements one atomic() request spends: the packed writes and guards,
|
|
127
|
+
* authority, post-conditions with the scope's guards, final authority and the
|
|
128
|
+
* return reads. Planners size a batch with this same count before sending it. */
|
|
129
|
+
export const atomicCount = (context:Parameters<typeof authority>[0]&Pick<D1ImportContext,'maxBatchBytes'>,action:ImportAction,statements:D1Sql[],returnReads:number,scope?:NativeStagingScope):number => atomicPreparation(context,action,statements,scope,returnReads).count;
|
|
130
|
+
function atomicPreparation(context:Parameters<typeof authority>[0]&Pick<D1ImportContext,'maxBatchBytes'>,action:ImportAction,statements:D1Sql[],scope?:NativeStagingScope,returnReads=0){
|
|
131
|
+
const post=postConditions(context,statements,scope,action);
|
|
132
|
+
const packed=[...atomicStatements(context,action,statements),...readonlyChecks(post),...(post.length?[finalAuthority(context,action)]:[])];
|
|
133
|
+
return {post,packed,count:packed.length+returnReads};
|
|
134
|
+
}
|
|
135
|
+
/** SELECT-only fence: inspect every packed scalar before publishing snapshots.
|
|
136
|
+
* Mutations continue to use atomic() and SQL CHECK rollback, never this path. */
|
|
137
|
+
export async function readAtomic(context: D1ImportContext, guards: D1Sql[], scope?: NativeStagingScope, action: ImportAction = 'read'): Promise<void> {
|
|
138
|
+
const checks = [...guards, ...postConditions(context, guards, scope, action)];
|
|
139
|
+
const packed = packGuards([authority(context, action), ...checks, authority(context, action)], true);
|
|
140
|
+
const limit = context.maxBatchBytes;
|
|
141
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 1)) throw new Error('INVALID_TRANSPORT_LIMIT');
|
|
142
|
+
if (limit !== undefined && !withinBatchBytes({...context,maxBatchBytes:Math.min(limit,2*1024*1024)}, packed)) {
|
|
143
|
+
if (!scope) throw new Error('IMPORT_D1_TRANSPORT_LIMIT');
|
|
144
|
+
await readBoundedGuards(context, checks, scope, Math.min(limit,2*1024*1024), action);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (packed.length > remaining(context)) throw new D1RequestLimitError();
|
|
148
|
+
const results = await context.storage.database.batch(packed.map(s => prepare(context.storage.database, s)));
|
|
149
|
+
checkBatchResults(results, packed.length, 'IMPORT_ATOMIC_GUARD');
|
|
150
|
+
for (const result of results) {
|
|
151
|
+
const rows = batchRows(result, 'IMPORT_ATOMIC_GUARD');
|
|
152
|
+
if (rows.length !== 1 || rows[0].ok !== 1) throw new Error('IMPORT_ATOMIC_GUARD');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export async function authorize(context: D1ImportContext, action: ImportAction): Promise<void> {
|
|
156
|
+
if (action === 'read' || action === 'plan') await readAtomic(context, [], undefined, action);
|
|
157
|
+
else await atomic(context, action, []);
|
|
158
|
+
}
|
|
159
|
+
export function limits(context: D1ImportContext): { batchSize: number; maxBatches: number } {
|
|
160
|
+
const batchSize = context.batchSize ?? 100, maxBatches = context.maxBatches ?? Number.MAX_SAFE_INTEGER;
|
|
161
|
+
if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000 || !Number.isSafeInteger(maxBatches) || maxBatches < 1) throw new Error('INVALID_BATCH_LIMIT');
|
|
162
|
+
return { batchSize, maxBatches };
|
|
163
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type { ImportPlan } from '../types.ts';
|
|
3
|
+
import { canonicalSize } from '../provenance/canonical-stream.ts';
|
|
4
|
+
|
|
5
|
+
/** Where a chunked plan's entries sit in its stored chunks, kept in its d1Plan 1
|
|
6
|
+
* envelope. `starts` names, for each chunk where an entry begins, the first such
|
|
7
|
+
* entry: [chunk, byte offset of its "{", ordinal]. `end` is the entries array's
|
|
8
|
+
* closing "]". A run of entries from one start to the next is complete JSON
|
|
9
|
+
* once wrapped in brackets, so a reader can parse the entries it needs inside D1
|
|
10
|
+
* without joining the whole plan. `header` repeats the plan's identity fields. */
|
|
11
|
+
export interface EntryLocator { count: number; end: [number, number]; header: [number, string, string, string, string, string]; starts: [number, number, number][] }
|
|
12
|
+
|
|
13
|
+
export function entryLocator(plan: ImportPlan, chunks: { body: string }[]): EntryLocator {
|
|
14
|
+
// Byte offset of the entries array in the canonical text: '{' then each sorted key before it.
|
|
15
|
+
let offset = 1;
|
|
16
|
+
for (const key of Object.keys(plan).sort()) {
|
|
17
|
+
if (key === 'entries') break;
|
|
18
|
+
offset += canonicalSize(key) + 1 + canonicalSize((plan as any)[key]) + 1;
|
|
19
|
+
}
|
|
20
|
+
offset += canonicalSize('entries') + 2;
|
|
21
|
+
const ends: number[] = [];
|
|
22
|
+
let total = 0;
|
|
23
|
+
for (const chunk of chunks) ends.push(total += Buffer.byteLength(chunk.body));
|
|
24
|
+
let chunk = 0;
|
|
25
|
+
const at = (position: number): [number, number] => {
|
|
26
|
+
while (chunk < ends.length && ends[chunk] <= position) chunk++;
|
|
27
|
+
if (chunk === ends.length) throw new Error('STORED_PLAN_CHANGED');
|
|
28
|
+
return [chunk, position - (chunk ? ends[chunk - 1] : 0)];
|
|
29
|
+
};
|
|
30
|
+
const starts: [number, number, number][] = [];
|
|
31
|
+
plan.entries.forEach((entry, ordinal) => {
|
|
32
|
+
const [where, within] = at(offset);
|
|
33
|
+
if (!starts.length || starts[starts.length - 1][0] !== where) starts.push([where, within, ordinal]);
|
|
34
|
+
offset += canonicalSize(entry) + 1;
|
|
35
|
+
});
|
|
36
|
+
return { count: plan.entries.length, end: at(plan.entries.length ? offset - 1 : offset), header: [plan.version, plan.destination, plan.namespace, plan.mappingVersion, plan.principalId, plan.fingerprint], starts };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const position = (value: unknown, chunks: number): value is [number, number] => Array.isArray(value) && value.length === 2 && Number.isSafeInteger(value[0]) && value[0] >= 0 && value[0] < chunks && Number.isSafeInteger(value[1]) && value[1] >= 0;
|
|
40
|
+
/** A stored locator's shape, before any of its positions is used. */
|
|
41
|
+
export function validLocator(value: any, chunks: number): value is EntryLocator {
|
|
42
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).sort().join() !== 'count,end,header,starts') return false;
|
|
43
|
+
const { count, end, header, starts } = value;
|
|
44
|
+
if (!Number.isSafeInteger(count) || count < 0 || !position(end, chunks) || !Array.isArray(header) || header.length !== 6 || !Array.isArray(starts) || !count !== !starts.length) return false;
|
|
45
|
+
for (let i = 0; i < starts.length; i++) {
|
|
46
|
+
const start = starts[i], prior = starts[i - 1];
|
|
47
|
+
if (!Array.isArray(start) || start.length !== 3 || !position(start.slice(0, 2), chunks) || !Number.isSafeInteger(start[2]) || start[2] >= count) return false;
|
|
48
|
+
if (prior ? start[0] <= prior[0] || start[2] <= prior[2] : start[2] !== 0) return false;
|
|
49
|
+
}
|
|
50
|
+
const last = starts[starts.length - 1];
|
|
51
|
+
return !last || end[0] > last[0] || end[0] === last[0] && end[1] > last[1];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The run of whole entries holding each ordinal: [first chunk, its offset,
|
|
55
|
+
* last chunk, the offset where the run stops, first ordinal of the run]. */
|
|
56
|
+
export function entryRun(locator: EntryLocator, ordinal: number): [number, number, number, number, number] {
|
|
57
|
+
let low = 0, high = locator.starts.length;
|
|
58
|
+
while (low + 1 < high) { const mid = (low + high) >>> 1; if (locator.starts[mid][2] <= ordinal) low = mid; else high = mid; }
|
|
59
|
+
const [chunk, offset, first] = locator.starts[low], next = locator.starts[low + 1] ?? locator.end;
|
|
60
|
+
return [chunk, offset, next[0], next[1], first];
|
|
61
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ImportPlan, OriginalErasurePolicy, SourceLocation, SourceRecord } from '../types.ts';
|
|
2
|
+
import { canonicalJson, sha256, sourceIdentity } from '../provenance/identity.ts';
|
|
3
|
+
|
|
4
|
+
const artifactKey = (name: string, hash: string) => canonicalJson([name, hash]);
|
|
5
|
+
/** Lineage, including duplicate source occurrences and byte-identical artifacts,
|
|
6
|
+
* selects complete originals. Names and pointers can themselves contain PII. */
|
|
7
|
+
export function withdrawArtifacts(plan: ImportPlan, next: ImportPlan, identities: Set<string>, policy?: OriginalErasurePolicy, originals?: Set<string>): void {
|
|
8
|
+
if (policy === undefined) return;
|
|
9
|
+
if (!['withdraw-affected-artifacts', 'withdraw-all-job-artifacts'].includes(policy)) throw new Error('INVALID_ORIGINAL_ERASURE_POLICY');
|
|
10
|
+
if (plan.erasure?.originalPolicy && plan.erasure.originalPolicy !== policy) throw new Error('IMPORT_ORIGINAL_ERASURE_POLICY_CONFLICT');
|
|
11
|
+
const hashes = new Set([...plan.source.records.filter(record => identities.has(sourceIdentity(plan.destination, plan.namespace, record.kind, record.sourceId))).map(record => record.artifactHash), ...originals ?? []]);
|
|
12
|
+
const names = new Set<string>(), replacements = new Map<string, string>();
|
|
13
|
+
next.source.artifacts = plan.source.artifacts.map(artifact => {
|
|
14
|
+
if (policy !== 'withdraw-all-job-artifacts' && !hashes.has(artifact.sha256)) return artifact;
|
|
15
|
+
names.add(artifact.name);
|
|
16
|
+
const key = artifactKey(artifact.name, artifact.sha256);
|
|
17
|
+
const name = artifact.original?.status === 'unavailable' && artifact.original.reason === 'unavailable-after-erasure' ? artifact.name : `erased-artifact:${sha256(key)}`;
|
|
18
|
+
replacements.set(key, name);
|
|
19
|
+
return { name, sha256: artifact.sha256, size: artifact.size, original: { status: 'unavailable' as const, reason: 'unavailable-after-erasure' as const, policy } };
|
|
20
|
+
});
|
|
21
|
+
if (!replacements.size) return;
|
|
22
|
+
next.erasure = { ...next.erasure!, originalPolicy: policy };
|
|
23
|
+
const source = (record: SourceRecord): SourceRecord => {
|
|
24
|
+
const name = replacements.get(artifactKey(record.location.artifact, record.artifactHash));
|
|
25
|
+
return name ? { ...record, location: { artifact: name } } : record;
|
|
26
|
+
};
|
|
27
|
+
next.source.records = next.source.records.map(source);
|
|
28
|
+
next.entries = next.entries.map(entry => ({ ...entry, source: source(entry.source) }));
|
|
29
|
+
// Artifact-scoped or unscoped text may contain filenames, URLs or wrapper PII.
|
|
30
|
+
const keep = (d: ImportPlan['diagnostics'][number]) => d.location ? !names.has(d.location.artifact) : !!d.kind && !!d.sourceId && !identities.has(sourceIdentity(plan.destination, plan.namespace, d.kind, d.sourceId));
|
|
31
|
+
next.diagnostics = next.diagnostics.filter(keep);
|
|
32
|
+
next.source.diagnostics = next.source.diagnostics.filter(keep);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function withdrawnLocation(plan: ImportPlan, next: ImportPlan, location: SourceLocation, hash: string): SourceLocation {
|
|
36
|
+
const index = plan.source.artifacts.findIndex(a => a.name === location.artifact && a.sha256 === hash);
|
|
37
|
+
return index >= 0 && plan.source.artifacts[index].name !== next.source.artifacts[index].name ? { artifact: next.source.artifacts[index].name } : location;
|
|
38
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ImportPlan } from '../types.ts';
|
|
2
|
+
import type { D1Sql } from './types.ts';
|
|
3
|
+
import type { InventorySnapshot } from './inventory-snapshot.ts';
|
|
4
|
+
import { canonicalJson } from '../provenance/identity.ts';
|
|
5
|
+
import { sql } from './database.ts';
|
|
6
|
+
import { originalStorage, originalVersions } from './original-storage.ts';
|
|
7
|
+
import { insertJsonRows } from './sets.ts';
|
|
8
|
+
import { changed, deleteRows } from './expected-state.ts';
|
|
9
|
+
import { recordWrites } from './erasure-writes.ts';
|
|
10
|
+
import { withdrawnLocation } from './erased-artifacts.ts';
|
|
11
|
+
|
|
12
|
+
/** Original bytes are only deleted, never rewritten. Unaffected custody remains
|
|
13
|
+
* byte-for-byte in its existing storage version; postguards prove the new set. */
|
|
14
|
+
export function erasedCustody(plan: ImportPlan, next: ImportPlan, id: string, snapshot: InventorySnapshot): D1Sql[] {
|
|
15
|
+
const version = originalVersions.get(plan)!;
|
|
16
|
+
originalVersions.set(next, version);
|
|
17
|
+
const after = originalStorage(next, id, version), writes: D1Sql[] = [];
|
|
18
|
+
for (const [index, artifact] of plan.source.artifacts.entries()) {
|
|
19
|
+
if (artifact.name === next.source.artifacts[index].name) continue;
|
|
20
|
+
const key = canonicalJson([artifact.name, artifact.sha256]);
|
|
21
|
+
for (const table of ['ik_d1_original_chunks', 'ik_originals']) writes.push(changed(sql(`DELETE FROM ${table} WHERE job_id=? AND kind='artifact' AND original_key=?`, id, key), deleteRows(table, row => row.job_id === id && row.kind === 'artifact' && row.original_key === key)));
|
|
22
|
+
const replacement = after.rows.find(row => row.kind === 'artifact' && row.name === next.source.artifacts[index].name)!;
|
|
23
|
+
writes.push(...insertJsonRows('ik_originals', ['job_id','kind','original_key','name','sha256','size','bytes'], [replacement]));
|
|
24
|
+
}
|
|
25
|
+
// Peers can share an artifact whose name contains the erased person's data.
|
|
26
|
+
// Preserve attribution and before-images, changing only that location metadata.
|
|
27
|
+
for (const table of ['ik_provenance', 'ik_d1_history']) {
|
|
28
|
+
const rows: { id: string; body: string }[] = [];
|
|
29
|
+
for (const row of snapshot.rows(table, 'job_id', [id])) {
|
|
30
|
+
const record = JSON.parse(row.record_json), location = withdrawnLocation(plan, next, record.location, record.artifactHash);
|
|
31
|
+
if (location !== record.location) rows.push({ id: row.id, body: canonicalJson({ ...record, location }) });
|
|
32
|
+
}
|
|
33
|
+
writes.push(...recordWrites(table, rows));
|
|
34
|
+
}
|
|
35
|
+
return writes;
|
|
36
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { targetIdentity } from './erasure-index.ts';
|
|
2
|
+
import { withdrawArtifacts } from './erased-artifacts.ts';
|
|
3
|
+
import type { ImportPlan, OriginalErasurePolicy } from '../types.ts';
|
|
4
|
+
import { canonicalJson, sha256, sourceIdentity } from '../provenance/identity.ts';
|
|
5
|
+
import { planFingerprint } from '../plan/index.ts';
|
|
6
|
+
|
|
7
|
+
/** Related records hold the erased person's data but map to other targets or none:
|
|
8
|
+
* their effects are severed and they are bound to the erased target. */
|
|
9
|
+
export interface RelatedPlanErasure { identities: Set<string>; target: string; originals?: Set<string> }
|
|
10
|
+
|
|
11
|
+
/** Preserve source identity and ordinal while removing its entire personal record.
|
|
12
|
+
* A permanent zero-effect decision prevents later replay, including after undo. */
|
|
13
|
+
export function erasePlanRecords(plan: ImportPlan, identities: Set<string>, policy?: OriginalErasurePolicy, related?: RelatedPlanErasure): ImportPlan {
|
|
14
|
+
const next: ImportPlan = { ...plan, entries: [...plan.entries], source: { ...plan.source, records: [...plan.source.records], diagnostics: [...plan.source.diagnostics] }, counts: { ...plan.counts }, diagnostics: [...plan.diagnostics] };
|
|
15
|
+
next.erasure ??= { reviewedFingerprint: plan.fingerprint };
|
|
16
|
+
for (const [ordinal, original] of plan.entries.entries()) {
|
|
17
|
+
if (!identities.has(original.identity) || original.erased) continue;
|
|
18
|
+
const entry = { ...original, source: { ...original.source } };
|
|
19
|
+
next.entries[ordinal] = entry;
|
|
20
|
+
const severed = related?.identities.has(original.identity) ?? false;
|
|
21
|
+
if (!severed && entry.effects.length !== 1) throw new Error('IMPORT_ERASURE_SHARED_SOURCE');
|
|
22
|
+
if (entry.effects.some(effect => effect.attachment) || (policy === undefined && next.source.artifacts.some(a => a.original?.status === 'available'))) throw new Error('IMPORT_ERASURE_RETAINED_ORIGINAL');
|
|
23
|
+
const reason = 'Personal data erased by an operator; this source cannot be replayed.';
|
|
24
|
+
const concernsRecord = (item: ImportPlan['diagnostics'][number]) => (!!item.kind && !!item.sourceId && identities.has(sourceIdentity(plan.destination, plan.namespace, item.kind, item.sourceId)))
|
|
25
|
+
|| (!!item.location && canonicalJson(item.location) === canonicalJson(entry.source.location));
|
|
26
|
+
next.diagnostics = next.diagnostics.filter(item => !concernsRecord(item));
|
|
27
|
+
next.source.diagnostics = next.source.diagnostics.filter(item => !concernsRecord(item));
|
|
28
|
+
next.counts.effects -= entry.effects.length;
|
|
29
|
+
if (entry.effects.length) next.counts.mapped--;
|
|
30
|
+
if (entry.rejection) next.counts.rejected--;
|
|
31
|
+
if (!entry.unmapped.length) next.counts.unmapped++;
|
|
32
|
+
delete entry.rejection;
|
|
33
|
+
const previousId = entry.source.sourceId;
|
|
34
|
+
entry.source.sourceId = `erased:${entry.identity}`;
|
|
35
|
+
entry.source.location = { artifact: entry.source.location.artifact };
|
|
36
|
+
entry.source.payload = { erased: true };
|
|
37
|
+
entry.source.payloadHash = sha256(canonicalJson(entry.source.payload));
|
|
38
|
+
next.source.records = next.source.records.map(record => record.namespace === entry.source.namespace && record.kind === entry.source.kind && record.sourceId === previousId ? { ...entry.source } : record);
|
|
39
|
+
entry.erasedTargetIdentity = severed ? related!.target : targetIdentity(plan.destination,entry.effects[0].targetKind,entry.effects[0].targetId);
|
|
40
|
+
entry.effects = []; entry.unmapped = [{ reason }]; entry.erased = true;
|
|
41
|
+
next.diagnostics.push({ severity: 'warning', code: 'ERASED_SOURCE', message: reason, kind: entry.source.kind, sourceId: entry.source.sourceId, location: entry.source.location });
|
|
42
|
+
}
|
|
43
|
+
if (related?.originals?.size && policy === undefined) throw new Error('IMPORT_ERASURE_RETAINED_ORIGINAL');
|
|
44
|
+
withdrawArtifacts(plan, next, identities, policy, related?.originals);
|
|
45
|
+
next.fingerprint = planFingerprint(next);
|
|
46
|
+
return next;
|
|
47
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ImportPlan } from '../types.ts';
|
|
2
|
+
import type { D1ImportContext, D1Sql } from './types.ts';
|
|
3
|
+
import { loadPlans } from './plans.ts';
|
|
4
|
+
import { guard, sql } from './database.ts';
|
|
5
|
+
import { eraseReport } from './erasure-report.ts';
|
|
6
|
+
|
|
7
|
+
/** Ordinary unbounded-policy calls retain their existing atomic behavior.
|
|
8
|
+
* Bounded consumers visit reports one page at a time in erasure-fragments. */
|
|
9
|
+
export async function eraseReversalEvidence(context: D1ImportContext, changedPlans: Map<string, ImportPlan>): Promise<{ before: D1Sql[]; after: D1Sql[] }> {
|
|
10
|
+
const db=context.storage.database;
|
|
11
|
+
const {results:jobs}=await db.prepare("SELECT * FROM ik_jobs WHERE destination=? AND reversal_policy='preserve-conflicts' AND EXISTS(SELECT 1 FROM ik_d1_reversal_reports r WHERE r.job_id=ik_jobs.id)").bind(context.destination).all();
|
|
12
|
+
const before=[guard(sql("(SELECT count(*) FROM ik_jobs j WHERE j.destination=? AND j.reversal_policy='preserve-conflicts' AND EXISTS(SELECT 1 FROM ik_d1_reversal_reports r WHERE r.job_id=j.id))=?",context.destination,jobs.length))],after:D1Sql[]=[];
|
|
13
|
+
for(const raw of jobs){
|
|
14
|
+
const plan=(await loadPlans([raw],db,context.storage.targets.planCodec)).get(raw.id)!;
|
|
15
|
+
const report=await eraseReport(raw,plan,changedPlans.get(raw.id)??plan,context);
|
|
16
|
+
before.push(...report.before);after.push(...report.after);
|
|
17
|
+
}
|
|
18
|
+
return {before,after};
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import type { D1ImportContext, D1Sql } from './types.ts';
|
|
3
|
+
import { copyStatement } from './guards.ts';
|
|
4
|
+
import { prepareErasureSelection } from './erasure-selection.ts';
|
|
5
|
+
import { executeErasureStage, stageD1ImportErasure } from './erasure-stage.ts';
|
|
6
|
+
import { canonicalJson } from '../provenance/identity.ts';
|
|
7
|
+
import type { D1ErasureStep, D1ErasureStepOptions } from './erasure-progress.ts';
|
|
8
|
+
import { erasureStatementBinding } from './erasure-progress.ts';
|
|
9
|
+
|
|
10
|
+
/** With an operation key, resumes only the journal's verified offset. Without
|
|
11
|
+
* options the complete operation must still fit this single invocation. */
|
|
12
|
+
export function eraseBounded(kind: string, targetId: string, nativeStatements: D1Sql[], context: D1ImportContext): Promise<{ nativeResults: unknown[]; erasedRecords: number }>;
|
|
13
|
+
export function eraseBounded(kind: string, targetId: string, nativeStatements: D1Sql[], context: D1ImportContext, options: D1ErasureStepOptions): Promise<D1ErasureStep>;
|
|
14
|
+
export async function eraseBounded(kind: string, targetId: string, nativeStatements: D1Sql[], context: D1ImportContext, options?: D1ErasureStepOptions): Promise<D1ErasureStep | { nativeResults: unknown[]; erasedRecords: number }> {
|
|
15
|
+
const keys = nativeStatements.map(() => Symbol('native erasure result'));
|
|
16
|
+
const statements = nativeStatements.map((row, i) => Object.assign(copyStatement(row), { resultKey: keys[i] }));
|
|
17
|
+
const build = async (id: string, ctx: D1ImportContext) => ({ ...await prepareErasureSelection({ target: { kind, id: targetId } }, statements, ctx, id), resultKeys: keys });
|
|
18
|
+
if (options) return stageD1ImportErasure(context, options, canonicalJson([kind, targetId, erasureStatementBinding(nativeStatements)]), build);
|
|
19
|
+
const id = randomUUID(), result = await executeErasureStage(context, id, await build(id, context));
|
|
20
|
+
if (result.phase !== 'complete') throw new Error('IMPORT_ERASURE_INCOMPLETE');
|
|
21
|
+
return { nativeResults: result.nativeResults, erasedRecords: result.erasedRecords };
|
|
22
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ImportPlan } from '../types.ts';
|
|
2
|
+
import type { D1Database, D1Sql } from './types.ts';
|
|
3
|
+
import { jsonChunks } from './sets.ts';
|
|
4
|
+
import { guard, sql } from './database.ts';
|
|
5
|
+
import { targetIdentity } from './erasure-index.ts';
|
|
6
|
+
|
|
7
|
+
/** Tombstones survive every job's undo. Both source and native target digests
|
|
8
|
+
* fence a stale reviewed plan, including one with a new source namespace. */
|
|
9
|
+
export function erasedSourcePredicates(plan: ImportPlan): D1Sql[] {
|
|
10
|
+
const predicates: D1Sql[] = [];
|
|
11
|
+
const live = plan.entries.filter(entry => !entry.erased);
|
|
12
|
+
const sources = live.map(entry => entry.identity);
|
|
13
|
+
const targets = live.flatMap(entry => entry.effects.map(effect => targetIdentity(plan.destination, effect.targetKind, effect.targetId)));
|
|
14
|
+
for (const [table, identities] of [['ik_d1_erased_sources', sources], ['ik_d1_erased_targets', targets]] as const) {
|
|
15
|
+
for (const chunk of jsonChunks([...new Set(identities)])) {
|
|
16
|
+
const predicate = sql(`NOT EXISTS(SELECT 1 FROM ${table} WHERE destination=? AND identity IN (SELECT value FROM json_each(?)))`, plan.destination, chunk);
|
|
17
|
+
predicates.push(predicate);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return predicates;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function guardErasedSources(plan: ImportPlan, db: D1Database): Promise<D1Sql[]> {
|
|
24
|
+
const predicates = erasedSourcePredicates(plan);
|
|
25
|
+
for (const predicate of predicates) if (!(await db.prepare(`SELECT ${predicate.sql} AS clear`).bind(...predicate.params!).first())?.clear) throw new Error('IMPORT_SOURCE_ERASED');
|
|
26
|
+
return predicates.map(guard);
|
|
27
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { guardPredicate } from './guards.ts';
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import type {D1ImportContext,D1Sql} from './types.ts';
|
|
4
|
+
import type {ErasureFragment} from './erasure-fragments.ts';
|
|
5
|
+
import {erasureBaselines} from './erasure-fragments.ts';
|
|
6
|
+
import {bare,prepareErasureFragment,privacyRows,valueRows,ErasureRowsAdmission} from './erasure-values.ts';
|
|
7
|
+
import {erasedIndexGuards} from './erasure-index.ts';
|
|
8
|
+
import {jobColumns} from './sets.ts';
|
|
9
|
+
import {batchBytes} from './transport.ts';
|
|
10
|
+
import {canonicalJson} from '../provenance/identity.ts';
|
|
11
|
+
import {remaining} from './budget.ts';
|
|
12
|
+
import {nativeStageRowsBytes} from './native-staging-rows.ts';
|
|
13
|
+
|
|
14
|
+
type Parts={before:D1Sql[];writes:D1Sql[];post:D1Sql[]};
|
|
15
|
+
const parts=():Parts=>({before:[],writes:[],post:[]});
|
|
16
|
+
const headers=(fragment:ErasureFragment)=>fragment.jobs.map(row=>Object.fromEntries([...jobColumns.filter(c=>c!=='plan_json'),'state_revision'].map(column=>[column,row[column]])));
|
|
17
|
+
export class ErasureCompiler {
|
|
18
|
+
readonly compiled=parts();direct:Parts|undefined=parts();
|
|
19
|
+
readonly indexes:ErasureRowsAdmission;readonly payload:ErasureRowsAdmission;
|
|
20
|
+
readonly jobs=new Map<string,any>();readonly fingerprints:string[]=[];
|
|
21
|
+
readonly erasedFinal:D1Sql[]=[];
|
|
22
|
+
values=0;erased=0;readQueries=0;unsafeOriginals=false;
|
|
23
|
+
readonly context:D1ImportContext;readonly id:string;readonly fragments:()=>AsyncGenerator<ErasureFragment>;
|
|
24
|
+
constructor(context:D1ImportContext,id:string,fragments:()=>AsyncGenerator<ErasureFragment>){this.context=context;this.id=id;this.fragments=fragments;this.indexes=new ErasureRowsAdmission(nativeStageRowsBytes(context));this.payload=new ErasureRowsAdmission(nativeStageRowsBytes(context));}
|
|
25
|
+
async inspect(){
|
|
26
|
+
const before=remaining(this.context),iterator=this.fragments();
|
|
27
|
+
while(true){
|
|
28
|
+
let item:IteratorResult<ErasureFragment>|undefined=await iterator.next();if(item.done)break;
|
|
29
|
+
this.collect(item.value);item=undefined;
|
|
30
|
+
}
|
|
31
|
+
this.readQueries=before-remaining(this.context);
|
|
32
|
+
}
|
|
33
|
+
private collect(fragment:ErasureFragment){
|
|
34
|
+
if(fragment.plans.some(plan=>plan.destination!==this.context.destination))throw new Error('PLAN_BINDING_MISMATCH');
|
|
35
|
+
const prepared=prepareErasureFragment(fragment,this.id,this.values),hash=createHash('sha256');
|
|
36
|
+
hash.update(prepared.hash);hash.update(canonicalJson(headers(fragment)));
|
|
37
|
+
for(const row of privacyRows(fragment,this.indexes.count)){this.indexes.append(row);hash.update(JSON.stringify(row)+'\n');}
|
|
38
|
+
for(const row of valueRows(prepared.values,this.values))this.payload.append(row);
|
|
39
|
+
this.values+=prepared.values.length;this.erased+=fragment.erased;
|
|
40
|
+
this.unsafeOriginals ||= fragment.plans.some(plan=>(this.context.originalErasurePolicy===undefined&&plan.source.artifacts.some(a=>a.original?.status==='available'))||plan.entries.some(e=>e.effects.some(effect=>effect.attachment!==undefined)));
|
|
41
|
+
for(const row of headers(fragment)){const prior=this.jobs.get(row.id);if(prior&&canonicalJson(prior)!==canonicalJson(row))throw new Error('IMPORT_CUSTODY_STATE_CHANGED');this.jobs.set(row.id,row);}
|
|
42
|
+
this.fingerprints.push(hash.digest('hex'));
|
|
43
|
+
// Only digest predicates survive the page; tombstones have no seal trigger.
|
|
44
|
+
for(const plan of fragment.plans)this.erasedFinal.push(...erasedIndexGuards(plan).map(query=>guardPredicate(query)!));
|
|
45
|
+
for(const key of ['before','writes','post'] as const){this.compiled[key].push(...prepared[key]);this.direct?.[key].push(...fragment[key].map(bare));}
|
|
46
|
+
// The direct alternative is retained only while its complete values fit.
|
|
47
|
+
// Prepared references and numeric admission data remain bounded separately.
|
|
48
|
+
if(this.direct&&batchBytes([...this.direct.before,...this.direct.writes,...this.direct.post])>this.context.maxBatchBytes!)this.direct=undefined;
|
|
49
|
+
if(batchBytes([...this.compiled.before,...this.compiled.writes,...this.compiled.post])>this.context.maxBatchBytes!)throw new Error('IMPORT_D1_TRANSPORT_LIMIT');
|
|
50
|
+
}
|
|
51
|
+
baseline():D1Sql[]{return erasureBaselines([...this.jobs.values()]);}
|
|
52
|
+
/** Both later passes must reproduce the validated instructions, exact value
|
|
53
|
+
* hashes and complete privacy membership under the same monotonic fences. */
|
|
54
|
+
async replay(accept:(fragment:ErasureFragment,values:string[],valueStart:number,indexStart:number)=>Promise<void>){
|
|
55
|
+
let ordinal=0,valueStart=0,indexStart=0;const iterator=this.fragments();
|
|
56
|
+
while(true){
|
|
57
|
+
let item:IteratorResult<ErasureFragment>|undefined=await iterator.next();if(item.done)break;
|
|
58
|
+
let prepared:ReturnType<typeof prepareErasureFragment>|undefined=prepareErasureFragment(item.value,this.id,valueStart);
|
|
59
|
+
const hash=createHash('sha256');hash.update(prepared.hash);hash.update(canonicalJson(headers(item.value)));
|
|
60
|
+
let count=0;for(const row of privacyRows(item.value,indexStart)){hash.update(JSON.stringify(row)+'\n');count++;}
|
|
61
|
+
if(hash.digest('hex')!==this.fingerprints[ordinal++])throw new Error('IMPORT_CUSTODY_STATE_CHANGED');
|
|
62
|
+
await accept(item.value,prepared.values,valueStart,indexStart);
|
|
63
|
+
valueStart+=prepared.values.length;indexStart+=count;prepared=undefined;item=undefined;
|
|
64
|
+
}
|
|
65
|
+
if(ordinal!==this.fingerprints.length||valueStart!==this.values||indexStart!==this.indexes.count)throw new Error('IMPORT_CUSTODY_STATE_CHANGED');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { D1UndoDisposition, D1UndoEvidence } from './undo-types.ts';
|
|
2
|
+
import { canonicalJson } from '../provenance/identity.ts';
|
|
3
|
+
|
|
4
|
+
const emptyEvidence = (): D1UndoEvidence => ({ revision: null, valueHash: null, attachmentHash: null, ownedHash: null, ownedValue: null });
|
|
5
|
+
export function erasedDisposition(d: Pick<D1UndoDisposition, 'jobId'|'effectId'|'disposition'|'reason'>, identity: string): D1UndoDisposition {
|
|
6
|
+
return { jobId: d.jobId, effectId: d.effectId, targetKind: 'erased', targetId: d.effectId, entity: { table: 'erased', recordId: d.effectId }, disposition: d.disposition, reason: d.reason, expected: emptyEvidence(), observed: emptyEvidence(), protectedBy: [], evidence: null, redacted: 'erased', sourceIdentity: identity };
|
|
7
|
+
}
|
|
8
|
+
export function redactDisposition(d: D1UndoDisposition): D1UndoDisposition {
|
|
9
|
+
if (d.redacted === 'erased') return d;
|
|
10
|
+
return { jobId: d.jobId, effectId: d.effectId, targetKind: 'redacted', targetId: d.effectId, entity: { table: 'redacted', recordId: d.effectId }, disposition: d.disposition, reason: d.reason,
|
|
11
|
+
expected: emptyEvidence(), observed: emptyEvidence(),
|
|
12
|
+
protectedBy: d.reason === 'dependency' ? [{ requiredEffectId: d.effectId, cause: { table: 'redacted', recordId: d.effectId }, evidence: null }] : [], evidence: null, redacted: 'evidence' };
|
|
13
|
+
}
|
|
14
|
+
export function redactedEvidenceMatches(d: D1UndoDisposition): boolean {
|
|
15
|
+
return d.redacted === 'evidence' && canonicalJson(d) === canonicalJson(redactDisposition(d));
|
|
16
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { erasedCustody } from './erased-custody.ts';
|
|
2
|
+
import type { ImportPlan } from '../types.ts';
|
|
3
|
+
import type { D1FieldRedaction, D1ImportContext, D1Sql } from './types.ts';
|
|
4
|
+
import { canonicalJson,sha256 } from '../provenance/identity.ts';
|
|
5
|
+
import { sql,guard } from './database.ts';
|
|
6
|
+
import { exactRows,selectedCounts,jsonChunks,jobColumns,insertJsonRows } from './sets.ts';
|
|
7
|
+
import { retainPlan,planGuards } from './plans.ts';
|
|
8
|
+
import { decode } from './jobs.ts';
|
|
9
|
+
import { readOriginals } from './originals.ts';
|
|
10
|
+
import { originalVersions,custodyGuards } from './original-storage.ts';
|
|
11
|
+
import { inventoryGuards,releaseInventory } from './integrity.ts';
|
|
12
|
+
import { attachReversal,loadReversalState,snapshotReversalGuards,policyHash } from './undo-state.ts';
|
|
13
|
+
import { erasePlanRecords,type RelatedPlanErasure } from './erased-plan.ts';
|
|
14
|
+
import { erasureWrites,type ErasedDecision,type ErasedJob } from './erasure-writes.ts';
|
|
15
|
+
import { eraseReport } from './erasure-report.ts';
|
|
16
|
+
import { changed,deleteRows,putRow,ExpectedState } from './expected-state.ts';
|
|
17
|
+
import type { ErasurePage } from './erasure-pages.ts';
|
|
18
|
+
import { readErasureSnapshot } from './erasure-snapshot.ts';
|
|
19
|
+
import { planIndex,erasedIndexGuards } from './erasure-index.ts';
|
|
20
|
+
import { redactPlanFields } from './field-redaction.ts';
|
|
21
|
+
import { redactedCustody } from './redacted-custody.ts';
|
|
22
|
+
|
|
23
|
+
export interface ErasureFragment {
|
|
24
|
+
before:D1Sql[]; writes:D1Sql[]; post:D1Sql[];
|
|
25
|
+
plans:ImportPlan[]; jobs:any[]; erased:number;
|
|
26
|
+
}
|
|
27
|
+
export interface ErasureSelection {
|
|
28
|
+
ids:string[]; reports:string[]; identities:Set<string>;kind:string;targetId:string;
|
|
29
|
+
severed?:RelatedPlanErasure; originals?:Map<string,Set<string>>;
|
|
30
|
+
fields?:readonly D1FieldRedaction[]; redactedJobs?:ReadonlySet<string>; retiring?:ReadonlySet<string>;
|
|
31
|
+
}
|
|
32
|
+
/** Bulk reads and set writes for small custody; large pages stay isolated.
|
|
33
|
+
* No decoded plan or inventory body survives advancing this iterator. */
|
|
34
|
+
export async function* erasureFragments(selection:ErasureSelection,context:D1ImportContext,pages:ErasurePage[]):AsyncGenerator<ErasureFragment>{
|
|
35
|
+
const {database:db,targets}=context.storage,affected=new Set(selection.ids),reports=new Set(selection.reports),retiring=selection.retiring??new Set<string>(),fields=selection.fields??[],changedJobs=new Set([...selection.ids,...selection.reports].filter(id=>!retiring.has(id)));
|
|
36
|
+
for(const page of pages){
|
|
37
|
+
const {ids}=page;
|
|
38
|
+
const snapshot=await readErasureSnapshot(page,db,targets.planCodec),before:D1Sql[]=[],payload:D1Sql[]=[],reportsAfter:D1Sql[]=[],post:D1Sql[]=[],jobs:ErasedJob[]=[],decisions:ErasedDecision[]=[],redactions:ErasedDecision[]=[];
|
|
39
|
+
const group=new Set(ids),changedIds=ids.filter(id=>affected.has(id)&&!retiring.has(id));
|
|
40
|
+
const release=jsonChunks(changedIds).flatMap(chunk=>[sql('UPDATE ik_d1_creation_plans SET released=1 WHERE job_id IN (SELECT value FROM json_each(?))',chunk),sql('DELETE FROM ik_d1_creation_plans WHERE job_id IN (SELECT value FROM json_each(?))',chunk)]);
|
|
41
|
+
const remove=jsonChunks(changedIds).map(chunk=>{const selected=new Set(JSON.parse(chunk));return changed(sql('DELETE FROM ik_d1_plan_chunks WHERE job_id IN (SELECT value FROM json_each(?))',chunk),deleteRows('ik_d1_plan_chunks',row=>selected.has(row.job_id)));});
|
|
42
|
+
for(const id of ids){
|
|
43
|
+
const raw=snapshot.rows('ik_jobs','id',[id])[0],plan=snapshot.plans.get(id)!;if(!raw)throw new Error('IMPORT_JOB_NOT_FOUND');
|
|
44
|
+
if(plan.destination!==context.destination)throw new Error('PLAN_BINDING_MISMATCH');
|
|
45
|
+
const job=decode(raw),reversal=await loadReversalState(raw,plan,context,false,snapshot);attachReversal(job,reversal);
|
|
46
|
+
await readOriginals(plan,id,context,snapshot);
|
|
47
|
+
const pages=snapshot.rows('ik_d1_stream_pages','job_id',[id]);
|
|
48
|
+
before.push(...snapshotReversalGuards(raw,reversal),...custodyGuards(plan,id),...await inventoryGuards(job,plan,context,snapshot),...exactRows('ik_d1_stream_pages',['stream_id','ordinal','job_id','total','erased_records'],pages),...selectedCounts('ik_d1_stream_pages','job_id',[id],pages));
|
|
49
|
+
if(retiring.has(id)){
|
|
50
|
+
post.push(...planGuards(plan,id),...custodyGuards(plan,id));
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
let next=plan;
|
|
54
|
+
if(affected.has(id)&&!retiring.has(id)){
|
|
55
|
+
const originals=selection.originals?.get(id);
|
|
56
|
+
if(!originals&&!selection.redactedJobs?.has(id)&&!plan.entries.some(entry=>selection.identities.has(entry.identity)))throw new Error('IMPORT_ERASURE_INDEX_MISMATCH');
|
|
57
|
+
if(plan.entries.some(entry=>selection.identities.has(entry.identity)&&!selection.severed?.identities.has(entry.identity)&&entry.effects.some(effect=>effect.targetKind!==selection.kind||effect.targetId!==selection.targetId)))throw new Error('IMPORT_ERASURE_SHARED_SOURCE');
|
|
58
|
+
next=erasePlanRecords(plan,selection.identities,context.originalErasurePolicy,{identities:selection.severed?.identities??new Set(),target:selection.severed?.target??'',originals});
|
|
59
|
+
if(fields.length)next=redactPlanFields(next,fields,context.originalErasurePolicy).plan;
|
|
60
|
+
originalVersions.set(next,originalVersions.get(plan)!);
|
|
61
|
+
const retained=retainPlan(next,id,next.source.artifacts.some(a=>a.original?.status==='available')?undefined:targets.planCodec);
|
|
62
|
+
decisions.push(...next.entries.flatMap((entry,ordinal)=>selection.identities.has(entry.identity)?[{job_id:id,ordinal,identity:entry.identity,source_hash:entry.source.payloadHash,effects_hash:sha256(canonicalJson(entry.effects))}]:[]));
|
|
63
|
+
redactions.push(...next.entries.flatMap((entry,ordinal)=>!entry.erased&&(entry.source.payloadHash!==plan.entries[ordinal].source.payloadHash||canonicalJson(entry.effects)!==canonicalJson(plan.entries[ordinal].effects))?[{job_id:id,ordinal,identity:entry.identity,source_hash:entry.source.payloadHash,effects_hash:sha256(canonicalJson(entry.effects))}]:[]));
|
|
64
|
+
jobs.push({id,body:retained.body,fingerprint:next.fingerprint,erasedRecords:next.entries.filter(entry=>entry.erased).length,policyHash:reversal.binding===null?null:policyHash(id,next.fingerprint,reversal.policy)});
|
|
65
|
+
payload.push(...erasedCustody(plan,next,id,snapshot),...retained.statements,guard(sql('(SELECT count(*) FROM ik_d1_creation_plans WHERE job_id=? AND released=0)=? AND (SELECT count(*) FROM ik_d1_plan_chunks WHERE job_id=?)=?',id,retained.encoded.chunks.length,id,retained.encoded.chunks.length)));
|
|
66
|
+
if(fields.length)payload.push(...redactedCustody(plan,next,id,snapshot,fields));
|
|
67
|
+
// Rewriting the index drops the job's indexed fingerprint (a schema trigger); retain it again.
|
|
68
|
+
if(canonicalJson(planIndex(next,id))!==canonicalJson(planIndex(plan,id)))payload.push(changed(sql('DELETE FROM ik_d1_plan_index WHERE job_id=?',id),deleteRows('ik_d1_plan_index',row=>row.job_id===id)),...insertJsonRows('ik_d1_plan_index',['job_id','ordinal','source_identity','target_identity'],planIndex(next,id)),
|
|
69
|
+
changed(sql('INSERT INTO ik_d1_indexed_jobs(job_id,fingerprint) VALUES(?,?)',id,next.fingerprint),putRow('ik_d1_indexed_jobs',{job_id:id,fingerprint:next.fingerprint})));
|
|
70
|
+
post.push(...exactRows('ik_d1_plan_chunks',['job_id','ordinal','body'],retained.encoded.chunks,sql('job_id=?',id)));
|
|
71
|
+
}else post.push(...planGuards(plan,id));
|
|
72
|
+
if(reports.has(id)&&!retiring.has(id)){const report=await eraseReport(raw,plan,next,context,snapshot);before.push(...report.before);reportsAfter.push(...report.after);}
|
|
73
|
+
post.push(...erasedIndexGuards(next),...custodyGuards(next,id),...exactRows('ik_d1_plan_index',['job_id','ordinal','source_identity','target_identity'],planIndex(next,id),sql('job_id=?',id)),guard(sql('EXISTS(SELECT 1 FROM ik_d1_indexed_jobs WHERE job_id=? AND fingerprint=?)',id,next.fingerprint)),guard(sql('NOT EXISTS(SELECT 1 FROM ik_d1_creation_plans WHERE job_id=?)',id)));
|
|
74
|
+
}
|
|
75
|
+
const writes=[...release,...remove,...payload,...release,...erasureWrites(jobs,decisions,redactions),...reportsAfter];
|
|
76
|
+
const excluded=new Set([...changedJobs].filter(id=>!group.has(id)));
|
|
77
|
+
post.unshift(...new ExpectedState([...before,...writes]).guards(excluded));
|
|
78
|
+
releaseInventory(context);
|
|
79
|
+
yield {before,writes,post,plans:[...snapshot.plans.values()],jobs:snapshot.tables.get('ik_jobs')!,erased:decisions.length};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function erasureBaselines(jobs:any[]):D1Sql[]{
|
|
84
|
+
for(const row of jobs)if(!Number.isSafeInteger(row.state_revision))throw new Error('IMPORT_CUSTODY_STATE_REQUIRED');
|
|
85
|
+
return [...exactRows('ik_jobs',jobColumns.filter(column=>column!=='plan_json'),jobs),...jsonChunks(jobs.map(row=>[row.id,row.state_revision])).map(body=>guard(sql("NOT EXISTS(SELECT 1 FROM json_each(?) e WHERE NOT EXISTS(SELECT 1 FROM ik_d1_custody_states s WHERE s.job_id=json_extract(e.value,'$[0]') AND s.revision=json_extract(e.value,'$[1]')))",body)))];
|
|
86
|
+
}
|