@runeditrun/rer 0.2.99 → 0.2.101
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 +30 -102
- package/cli/README.md +38 -25
- package/cli/app-address.mjs +2 -0
- package/cli/business/api.mjs +2 -1
- package/cli/business/choices.mjs +1 -1
- package/cli/business/command.mjs +36 -25
- package/cli/business/connect.mjs +1 -1
- package/cli/business/lease.mjs +0 -9
- package/cli/business/local-commands.mjs +1 -18
- package/cli/business/outbound.mjs +2 -14
- package/cli/business/questions.mjs +85 -48
- package/cli/business/service.mjs +1 -24
- package/cli/business/setup-command.mjs +33 -26
- package/cli/business/state.mjs +2 -2
- package/cli/business/supervisor.mjs +26 -91
- package/cli/companion/command.mjs +2 -0
- package/cli/demo-native.mjs +1 -1
- package/cli/deploy-command.mjs +5 -4
- package/cli/deploy-inputs.mjs +1 -1
- package/cli/deploy.mjs +6 -4
- package/cli/parse.mjs +2 -1
- package/cli/production-auth.mjs +16 -3
- package/cli/production-mail.mjs +18 -1
- package/cli/site.mjs +1 -1
- package/global/CHANGELOG.md +45 -0
- package/global/README.md +30 -102
- package/global/main.mjs +2 -0
- package/modules/container/adapters/d1-limits.mjs +2 -10
- package/modules/d1-transport/limits.d.mts +10 -0
- package/modules/d1-transport/limits.mjs +10 -0
- package/modules/demo-host/native/prepare.mjs +2 -1
- package/modules/deploy-kit/README.md +10 -0
- package/modules/deploy-kit/provider.mjs +18 -2
- package/modules/import-kit/schema/importers.schema.json +104 -2
- package/modules/import-kit/src/d1/README.md +13 -0
- package/modules/import-kit/src/d1/apply-reads.ts +48 -0
- package/modules/import-kit/src/d1/apply.ts +21 -8
- package/modules/import-kit/src/d1/history.ts +2 -2
- package/modules/import-kit/src/d1/jobs.ts +3 -1
- package/modules/import-kit/src/d1/retirement-scope.ts +2 -2
- package/modules/import-kit/src/d1/retirement-tally.ts +1 -0
- package/modules/import-kit/src/d1/retirement.ts +15 -5
- package/modules/import-kit/src/d1/types.ts +14 -0
- package/modules/import-kit/src/d1/wave-boundary.ts +33 -0
- package/modules/import-kit/src/d1/wave-retirement.ts +47 -0
- package/modules/import-kit/src/provenance/credentials.ts +14 -5
- package/modules/import-kit/src/status/evidence.ts +3 -1
- package/modules/import-kit/src/status/index.ts +17 -2
- package/modules/import-kit/src/status/schema.ts +8 -0
- package/modules/import-kit/src/status/types.ts +25 -4
- package/onboarding/adapter.mjs +7 -1
- package/onboarding/operator-installer.mjs +3 -1
- package/package.json +1 -1
- package/scripts/publish-check/README.md +20 -0
- package/scripts/publish-check/diagnostics.mjs +73 -0
- package/scripts/publish-check/slots.py +37 -7
- package/scripts/publish-check.mjs +64 -37
- package/cli/business/archive-task.mjs +0 -32
- package/cli/business/catchup-task.mjs +0 -42
- package/cli/business/export-inputs.mjs +0 -69
- package/cli/business/export-task.mjs +0 -17
- package/cli/business/setup-demo.mjs +0 -95
- package/cli/business/vendor-door.mjs +0 -231
- package/cli/business/vendors/README.md +0 -176
- package/cli/business/vendors/calendly.mjs +0 -80
- package/cli/business/vendors/common.mjs +0 -199
- package/cli/business/vendors/credential-names.mjs +0 -124
- package/cli/business/vendors/index.mjs +0 -86
- package/cli/business/vendors/pagerduty.mjs +0 -61
- package/cli/business/vendors/sentry.mjs +0 -85
- package/cli/business/vendors/sorry.mjs +0 -106
- package/cli/business/vendors/uptimerobot.mjs +0 -21
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
"required": ["$schema", "schemaVersion", "importers"],
|
|
7
7
|
"properties": {
|
|
8
8
|
"$schema": { "const": "./schema/importers.schema.json" },
|
|
9
|
-
"schemaVersion": { "
|
|
9
|
+
"schemaVersion": { "enum": [1, 2] },
|
|
10
|
+
"format": { "$ref": "#/$defs/format" },
|
|
10
11
|
"importers": {
|
|
11
12
|
"type": "array",
|
|
12
13
|
"minItems": 1,
|
|
@@ -14,13 +15,21 @@
|
|
|
14
15
|
"oneOf": [
|
|
15
16
|
{ "$ref": "#/$defs/verifiedImporter" },
|
|
16
17
|
{ "$ref": "#/$defs/documentedImporter" },
|
|
17
|
-
{ "$ref": "#/$defs/noneImporter" }
|
|
18
|
+
{ "$ref": "#/$defs/noneImporter" },
|
|
19
|
+
{ "$ref": "#/$defs/moveIn" },
|
|
20
|
+
{ "$ref": "#/$defs/moveInVerifiedImporter" },
|
|
21
|
+
{ "$ref": "#/$defs/moveInDocumentedImporter" },
|
|
22
|
+
{ "$ref": "#/$defs/moveInNoneImporter" }
|
|
18
23
|
]
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
},
|
|
22
27
|
"additionalProperties": false,
|
|
23
28
|
"$defs": {
|
|
29
|
+
"format": { "type": "string", "minLength": 1, "maxLength": 300, "pattern": "^(?!.*(?:^|/)\\.\\.(?:/|$))[A-Za-z0-9._][A-Za-z0-9._/-]*$" },
|
|
30
|
+
"moveInPath": { "type": "string", "minLength": 1, "maxLength": 300, "pattern": "^/(?:[^/\\s][^\\s]*)?$" },
|
|
31
|
+
"accepts": { "type": "array", "minItems": 1, "maxItems": 3, "uniqueItems": true, "items": { "enum": ["file", "key", "address"] } },
|
|
32
|
+
"export": { "type": "string", "minLength": 1, "maxLength": 1000, "pattern": "^[^\\r\\n]+$" },
|
|
24
33
|
"artifact": {
|
|
25
34
|
"type": "object",
|
|
26
35
|
"required": ["link", "sha256", "size"],
|
|
@@ -102,6 +111,99 @@
|
|
|
102
111
|
}
|
|
103
112
|
},
|
|
104
113
|
"additionalProperties": false
|
|
114
|
+
},
|
|
115
|
+
"moveIn": {
|
|
116
|
+
"type": "object",
|
|
117
|
+
"required": ["incumbent", "product", "moveIn", "accepts", "export"],
|
|
118
|
+
"properties": {
|
|
119
|
+
"incumbent": { "type": "string", "minLength": 1 },
|
|
120
|
+
"product": { "type": "string", "minLength": 1 },
|
|
121
|
+
"moveIn": { "$ref": "#/$defs/moveInPath" },
|
|
122
|
+
"accepts": { "$ref": "#/$defs/accepts" },
|
|
123
|
+
"export": { "$ref": "#/$defs/export" }
|
|
124
|
+
},
|
|
125
|
+
"additionalProperties": false
|
|
126
|
+
},
|
|
127
|
+
"moveInVerifiedImporter": {
|
|
128
|
+
"type": "object",
|
|
129
|
+
"required": ["incumbent", "product", "moveIn", "accepts", "export", "status", "evidence"],
|
|
130
|
+
"properties": {
|
|
131
|
+
"incumbent": { "type": "string", "minLength": 1 },
|
|
132
|
+
"product": { "type": "string", "minLength": 1 },
|
|
133
|
+
"moveIn": { "$ref": "#/$defs/moveInPath" },
|
|
134
|
+
"accepts": { "$ref": "#/$defs/accepts" },
|
|
135
|
+
"export": { "$ref": "#/$defs/export" },
|
|
136
|
+
"status": { "const": "verified" },
|
|
137
|
+
"evidence": {
|
|
138
|
+
"type": "object",
|
|
139
|
+
"required": ["link", "sha256", "size", "research"],
|
|
140
|
+
"properties": {
|
|
141
|
+
"link": { "type": "string", "minLength": 1 },
|
|
142
|
+
"sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
|
|
143
|
+
"size": { "type": "integer", "minimum": 1 },
|
|
144
|
+
"research": { "$ref": "#/$defs/artifact" }
|
|
145
|
+
},
|
|
146
|
+
"additionalProperties": false
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
"additionalProperties": false
|
|
150
|
+
},
|
|
151
|
+
"moveInDocumentedImporter": {
|
|
152
|
+
"type": "object",
|
|
153
|
+
"required": ["incumbent", "product", "moveIn", "accepts", "export", "status", "evidence"],
|
|
154
|
+
"properties": {
|
|
155
|
+
"incumbent": { "type": "string", "minLength": 1 },
|
|
156
|
+
"product": { "type": "string", "minLength": 1 },
|
|
157
|
+
"moveIn": { "$ref": "#/$defs/moveInPath" },
|
|
158
|
+
"accepts": { "$ref": "#/$defs/accepts" },
|
|
159
|
+
"export": { "$ref": "#/$defs/export" },
|
|
160
|
+
"status": { "const": "documented format" },
|
|
161
|
+
"evidence": {
|
|
162
|
+
"type": "object",
|
|
163
|
+
"required": [
|
|
164
|
+
"link", "sha256", "size", "sourceRepository", "sourceRevision",
|
|
165
|
+
"sourcePath", "sourceSha256", "validation", "realExport"
|
|
166
|
+
],
|
|
167
|
+
"properties": {
|
|
168
|
+
"link": { "type": "string", "minLength": 1 },
|
|
169
|
+
"sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
|
|
170
|
+
"size": { "type": "integer", "minimum": 1 },
|
|
171
|
+
"sourceRepository": { "type": "string", "minLength": 1 },
|
|
172
|
+
"sourceRevision": { "type": "string", "pattern": "^[a-f0-9]{40,64}$" },
|
|
173
|
+
"sourcePath": { "type": "string", "minLength": 1 },
|
|
174
|
+
"sourceSha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
|
|
175
|
+
"validation": { "const": "synthetic-only" },
|
|
176
|
+
"realExport": { "const": "not-supplied" }
|
|
177
|
+
},
|
|
178
|
+
"additionalProperties": false
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
"additionalProperties": false
|
|
182
|
+
},
|
|
183
|
+
"moveInNoneImporter": {
|
|
184
|
+
"type": "object",
|
|
185
|
+
"required": ["incumbent", "product", "moveIn", "accepts", "export", "status", "evidence"],
|
|
186
|
+
"properties": {
|
|
187
|
+
"incumbent": { "type": "string", "minLength": 1 },
|
|
188
|
+
"product": { "type": "string", "minLength": 1 },
|
|
189
|
+
"moveIn": { "$ref": "#/$defs/moveInPath" },
|
|
190
|
+
"accepts": { "$ref": "#/$defs/accepts" },
|
|
191
|
+
"export": { "$ref": "#/$defs/export" },
|
|
192
|
+
"status": { "const": "none" },
|
|
193
|
+
"evidence": {
|
|
194
|
+
"type": "object",
|
|
195
|
+
"required": ["link", "sha256", "size", "reason", "checkedAt"],
|
|
196
|
+
"properties": {
|
|
197
|
+
"link": { "type": "string", "minLength": 1 },
|
|
198
|
+
"sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
|
|
199
|
+
"size": { "type": "integer", "minimum": 1 },
|
|
200
|
+
"reason": { "type": "string", "minLength": 1 },
|
|
201
|
+
"checkedAt": { "type": "string", "format": "date-time" }
|
|
202
|
+
},
|
|
203
|
+
"additionalProperties": false
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
"additionalProperties": false
|
|
105
207
|
}
|
|
106
208
|
}
|
|
107
209
|
}
|
|
@@ -153,6 +153,14 @@ are local runtime receipts, not hosted capacity evidence.
|
|
|
153
153
|
The product implements `D1TargetAdapter`:
|
|
154
154
|
|
|
155
155
|
- `read(database, destination, kind, id)` returns the complete native `TargetRow`.
|
|
156
|
+
- Optional `readMany(database, destination, keys)` returns the same complete rows
|
|
157
|
+
as individual reads, in key order, including duplicate keys and missing rows.
|
|
158
|
+
Query only selected keys and preserve native decoding. With private transport,
|
|
159
|
+
adapters that implement `stagingRead` can also provide `stagingReadMany`, with
|
|
160
|
+
the same exact before-read guards for each key. Missing bulk methods retain
|
|
161
|
+
individual reads; any bulk-read error discards the snapshot and uses that path.
|
|
162
|
+
An inherited bulk method must preserve an overridden single read or throw.
|
|
163
|
+
Bulk observations belong only to the current publication.
|
|
156
164
|
- `matches(rowOrNull, destination, kind, id)` returns an exact SQL boolean snapshot
|
|
157
165
|
predicate covering the native values, revision, attachment and related evidence,
|
|
158
166
|
or complete absence for null. This is the optimistic concurrency fence. Checking
|
|
@@ -194,6 +202,11 @@ per request. The port does not retry failed SQL, conflicts or revoked authority.
|
|
|
194
202
|
A failed batch leaves its prior committed checkpoint intact; the product can fix
|
|
195
203
|
the condition and explicitly call `resumeD1Import`, or `rollbackD1Import`.
|
|
196
204
|
|
|
205
|
+
Saved read budget can advance a budget-limited call's checkpoint or surface a
|
|
206
|
+
later entry's refusal one call earlier, with the same error code and text.
|
|
207
|
+
Such a refusal publishes none of the gathered entries and leaves the job at its
|
|
208
|
+
last consistent durable checkpoint.
|
|
209
|
+
|
|
197
210
|
Each batch guards the immutable plan, actor binding, checkpoint and durable fence
|
|
198
211
|
inside the same D1 transaction as native rows, originals, source ownership,
|
|
199
212
|
provenance and journal. Successful batches increment the fence. Unlike Node's
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { d1NativeLimits } from '../../../d1-transport/limits.mjs';
|
|
2
|
+
import type { PlanEntry, TargetRow } from '../types.ts';
|
|
3
|
+
import type { D1ImportContext, D1Sql } from './types.ts';
|
|
4
|
+
|
|
5
|
+
const key = (kind: string, id: string): string => JSON.stringify([kind, id]);
|
|
6
|
+
export interface ApplyReads {
|
|
7
|
+
sources: Map<string, any>;
|
|
8
|
+
predecessors: Map<string, any>;
|
|
9
|
+
targets: Map<string, { row: TargetRow | undefined; guards?: D1Sql[] }>;
|
|
10
|
+
}
|
|
11
|
+
export const targetReadKey = key;
|
|
12
|
+
|
|
13
|
+
/** Only this publication's candidate entries. No observation survives atomic().
|
|
14
|
+
* Failure discards the entire snapshot, including any earlier successful set. */
|
|
15
|
+
export async function applyReads(entries: PlanEntry[], context: D1ImportContext): Promise<ApplyReads | undefined> {
|
|
16
|
+
const { database: db, targets } = context.storage;
|
|
17
|
+
const staged = context.maxBatchBytes !== undefined && !!targets.stagingRead;
|
|
18
|
+
if (staged ? !targets.stagingReadMany : !targets.readMany) return;
|
|
19
|
+
try {
|
|
20
|
+
const sources = new Map<string, any>(), predecessors = new Map<string, any>();
|
|
21
|
+
const identities = [...new Set(entries.map(entry => entry.identity))];
|
|
22
|
+
for (let i = 0; i < identities.length; i += d1NativeLimits.variableNumber) {
|
|
23
|
+
const part = identities.slice(i, i + d1NativeLimits.variableNumber);
|
|
24
|
+
const { results } = await db.prepare(`SELECT * FROM ik_sources WHERE identity IN (${part.map(() => '?').join(',')})`).bind(...part).all();
|
|
25
|
+
for (const row of results) sources.set(row.identity, row);
|
|
26
|
+
}
|
|
27
|
+
const keys = [...new Map(entries.flatMap(entry => entry.effects.map(effect => [key(effect.targetKind, effect.targetId), { kind: effect.targetKind, id: effect.targetId }] as const))).values()];
|
|
28
|
+
const snapshots: { row: TargetRow | undefined; guards?: D1Sql[] }[] = staged
|
|
29
|
+
? await targets.stagingReadMany!(db, context.destination, keys)
|
|
30
|
+
: (await targets.readMany!(db, context.destination, keys)).map(row => ({ row }));
|
|
31
|
+
if (snapshots.length !== keys.length) throw new Error('IMPORT_NATIVE_STAGING_GUARD_REQUIRED');
|
|
32
|
+
const native = new Map<string, { row: TargetRow | undefined; guards?: D1Sql[] }>();
|
|
33
|
+
for (const [i, selected] of keys.entries()) {
|
|
34
|
+
const snapshot = snapshots[i];
|
|
35
|
+
if (staged && !snapshot.guards?.length) throw new Error('IMPORT_NATIVE_STAGING_GUARD_REQUIRED');
|
|
36
|
+
native.set(key(selected.kind, selected.id), snapshot);
|
|
37
|
+
}
|
|
38
|
+
const size = Math.floor((d1NativeLimits.variableNumber - 1) / 2);
|
|
39
|
+
for (let i = 0; i < keys.length; i += size) {
|
|
40
|
+
const part = keys.slice(i, i + size);
|
|
41
|
+
const { results } = await db.prepare(`SELECT * FROM ik_provenance WHERE destination=? AND (target_kind,target_id) IN (${part.map(() => '(?,?)').join(',')}) AND reversed=0`).bind(context.destination, ...part.flatMap(k => [k.kind, k.id])).all();
|
|
42
|
+
for (const row of results) predecessors.set(key(row.target_kind, row.target_id), row);
|
|
43
|
+
}
|
|
44
|
+
return { sources, predecessors, targets: native };
|
|
45
|
+
} catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -15,6 +15,7 @@ import { custodyGuards, validatedProvenance, inventoryGuards, reusedSourceGuards
|
|
|
15
15
|
import { supersedeTarget } from './history.ts';
|
|
16
16
|
import { readOriginals } from './originals.ts';
|
|
17
17
|
import { changed, putRow, updateRows } from './expected-state.ts';
|
|
18
|
+
import { applyReads, targetReadKey, type ApplyReads } from './apply-reads.ts';
|
|
18
19
|
|
|
19
20
|
export async function applyD1ImportBatches(jobOrId: ImportJob | string, context: D1ImportContext): Promise<ImportJob> {
|
|
20
21
|
context = requestContext(context);
|
|
@@ -35,16 +36,19 @@ export async function applyD1ImportBatches(jobOrId: ImportJob | string, context:
|
|
|
35
36
|
let effects = 0, scope = nativeStagingScope(job, plan);
|
|
36
37
|
let prefix = atomicPrefix(context, 'apply', statements, scope);
|
|
37
38
|
let planned = prefix ? prefix.count(progress(job, 0), returnReads) : cost(context, publication(job, 0, statements, scope), returnReads);
|
|
38
|
-
|
|
39
|
+
const pending = plan.entries.slice(job.checkpoint, job.checkpoint + batchSize);
|
|
40
|
+
let reads: ApplyReads | undefined, loaded = false;
|
|
41
|
+
for (const entry of pending) {
|
|
39
42
|
if (entries.length && effects + entry.effects.length > 1000) break;
|
|
40
43
|
if (entry.effects.length > 1000) throw new Error('SOURCE_EFFECT_LIMIT');
|
|
41
44
|
// Stop before an entry's own reads could exhaust the request. The exact
|
|
42
45
|
// request, as atomic will send it, is counted again once the entry is built.
|
|
43
46
|
const reserve = 40 + entry.effects.length * 16;
|
|
44
47
|
if (remaining(context) - planned < reserve) { if (!entries.length) { if (count > 0) return job; throw new D1RequestLimitError(); } break; }
|
|
48
|
+
if (!loaded) { reads = await applyReads(pending, context); loaded = true; }
|
|
45
49
|
const candidate: D1Sql[] = [], nextScope = { ...scope, nativeGuards:[...scope.nativeGuards], postGuards:[...scope.postGuards], targets:[...scope.targets] };
|
|
46
50
|
const candidateUndo: D1UndoEffect[] | undefined = undoEffects && [];
|
|
47
|
-
await publishEntry(id, entry, job.checkpoint + entries.length, context, candidate, nextScope, candidateUndo);
|
|
51
|
+
await publishEntry(id, entry, job.checkpoint + entries.length, context, candidate, nextScope, candidateUndo, reads);
|
|
48
52
|
const nextPrefix = prefix?.extend(candidate);
|
|
49
53
|
const next = nextPrefix ? undefined : publication(job, entries.length + 1, [...statements, ...candidate], nextScope);
|
|
50
54
|
const spent = nextPrefix ? nextPrefix.count(progress(job, entries.length + 1), returnReads) : cost(context, next!, returnReads);
|
|
@@ -90,18 +94,19 @@ export async function applyD1ImportBatches(jobOrId: ImportJob | string, context:
|
|
|
90
94
|
}
|
|
91
95
|
return current.job;
|
|
92
96
|
}
|
|
93
|
-
async function publishEntry(id: string, entry: PlanEntry, ordinal: number, context: D1ImportContext, statements: D1Sql[], scope: NativeStagingScope, undoEffects?: D1UndoEffect[]): Promise<void> {
|
|
97
|
+
async function publishEntry(id: string, entry: PlanEntry, ordinal: number, context: D1ImportContext, statements: D1Sql[], scope: NativeStagingScope, undoEffects?: D1UndoEffect[], reads?: ApplyReads): Promise<void> {
|
|
94
98
|
const { database: db, targets } = context.storage, effectsHash = sha256(canonicalJson(entry.effects));
|
|
95
|
-
const prior = await db.prepare('SELECT * FROM ik_sources WHERE identity=?').bind(entry.identity).first();
|
|
99
|
+
const prior = reads ? reads.sources.get(entry.identity) : await db.prepare('SELECT * FROM ik_sources WHERE identity=?').bind(entry.identity).first();
|
|
96
100
|
if (prior) {
|
|
97
101
|
if (prior.source_hash !== entry.source.payloadHash || prior.mapping_version !== context.mappingVersion || prior.effects_hash !== effectsHash) throw new Error('SOURCE_ID_CONTENT_CONFLICT');
|
|
98
102
|
await reusedSourceGuards(entry, prior.job_id, context, statements);
|
|
99
103
|
statements.push(guard(sql('EXISTS(SELECT 1 FROM ik_sources WHERE identity=? AND source_hash=? AND mapping_version=? AND effects_hash=? AND job_id=?)', entry.identity, prior.source_hash, prior.mapping_version, prior.effects_hash, prior.job_id)));
|
|
100
104
|
for (const effect of entry.effects) {
|
|
101
|
-
const
|
|
105
|
+
const predecessor = reads?.predecessors.get(targetReadKey(effect.targetKind, effect.targetId));
|
|
106
|
+
const raw = reads ? (predecessor?.source_identity === entry.identity ? predecessor : null) : await db.prepare('SELECT * FROM ik_provenance WHERE source_identity=? AND destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(entry.identity, context.destination, effect.targetKind, effect.targetId).first();
|
|
102
107
|
if (!raw) throw new Error('PROVENANCE_MISSING');
|
|
103
108
|
const p = await validatedProvenance(raw, context, statements);
|
|
104
|
-
const current = await
|
|
109
|
+
const current = await readTarget(context, scope, effect.targetKind, effect.targetId, reads);
|
|
105
110
|
if (!current || current.revision !== p.appliedRevision || sha256(canonicalJson(current.value)) !== p.valueHash || attachmentHash(current.attachment) !== p.attachmentHash) throw new Error('NATIVE_EDIT_CONFLICT');
|
|
106
111
|
const post = targetGuards(targets,current,context.destination,effect.targetKind,effect.targetId);
|
|
107
112
|
scope.postGuards.push(...post);
|
|
@@ -113,7 +118,7 @@ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, conte
|
|
|
113
118
|
statements.push(guard(sql('NOT EXISTS(SELECT 1 FROM ik_sources WHERE identity=?)', entry.identity)));
|
|
114
119
|
for (const [index, effect] of entry.effects.entries()) {
|
|
115
120
|
validateEffect(effect);
|
|
116
|
-
const before = await
|
|
121
|
+
const before = await readTarget(context, scope, effect.targetKind, effect.targetId, reads);
|
|
117
122
|
if (before ? effect.expectedRevision !== before.revision : effect.expectedRevision !== undefined) throw new Error('TARGET_REVISION_CONFLICT');
|
|
118
123
|
const prospective = prospectiveEffect(id, entry, index, before, context.destination);
|
|
119
124
|
const { expected: row, provenance: p } = prospective;
|
|
@@ -121,7 +126,7 @@ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, conte
|
|
|
121
126
|
const post = targetGuards(targets, row, context.destination, effect.targetKind, effect.targetId);
|
|
122
127
|
scope.postGuards.push(...post);
|
|
123
128
|
undoEffects?.push(prospective);
|
|
124
|
-
await supersedeTarget(p, before, context, statements);
|
|
129
|
+
await supersedeTarget(p, before, context, statements, reads ? reads.predecessors.get(targetReadKey(effect.targetKind, effect.targetId)) ?? null : undefined);
|
|
125
130
|
const record_json=canonicalJson(p);
|
|
126
131
|
statements.push(...targetGuards(targets,before ?? null, context.destination, effect.targetKind, effect.targetId), ...targets.write(row), ...post, changed(retainedSql(['INSERT INTO ik_provenance(id,job_id,ordinal,effect_ordinal,source_identity,destination,target_kind,target_id,record_json) VALUES(', ',', ',', ',', ',', ',', ',', ',', ',', ')'], [p.id, id, ordinal, index, entry.identity, context.destination, effect.targetKind, effect.targetId, record_json], [8]),putRow('ik_provenance',{id:p.id,job_id:id,ordinal,effect_ordinal:index,source_identity:entry.identity,destination:context.destination,target_kind:effect.targetKind,target_id:effect.targetId,reversed:0,record_json})));
|
|
127
132
|
}
|
|
@@ -129,6 +134,14 @@ async function publishEntry(id: string, entry: PlanEntry, ordinal: number, conte
|
|
|
129
134
|
statements.push(changed(sql('INSERT INTO ik_sources(identity,source_hash,mapping_version,effects_hash,job_id) VALUES(?,?,?,?,?)', entry.identity, entry.source.payloadHash, context.mappingVersion, effectsHash, id),putRow('ik_sources',{identity:entry.identity,source_hash:entry.source.payloadHash,mapping_version:context.mappingVersion,effects_hash:effectsHash,job_id:id})));
|
|
130
135
|
}
|
|
131
136
|
|
|
137
|
+
async function readTarget(context: D1ImportContext, scope: NativeStagingScope, kind: string, id: string, reads?: ApplyReads) {
|
|
138
|
+
if (!reads) return readStagedTarget(context, scope, kind, id);
|
|
139
|
+
const snapshot = reads.targets.get(targetReadKey(kind, id))!;
|
|
140
|
+
scope.nativeGuards.push(...(snapshot.guards ?? []).map(guard));
|
|
141
|
+
scope.targets.push({ kind, id, present: !!snapshot.row });
|
|
142
|
+
return snapshot.row;
|
|
143
|
+
}
|
|
144
|
+
|
|
132
145
|
function entryWrite(job_id:string,ordinal:number,source_identity:string,owner_job_id:string,effects_hash:string):D1Sql{
|
|
133
146
|
return changed(sql('INSERT INTO ik_d1_entries(job_id,ordinal,source_identity,owner_job_id,effects_hash) VALUES(?,?,?,?,?)',job_id,ordinal,source_identity,owner_job_id,effects_hash),putRow('ik_d1_entries',{job_id,ordinal,source_identity,owner_job_id,effects_hash}));
|
|
134
147
|
}
|
|
@@ -10,9 +10,9 @@ export const provenanceColumns = 'id,job_id,ordinal,effect_ordinal,source_identi
|
|
|
10
10
|
|
|
11
11
|
/** Transfer only a currently attributed exact native snapshot, explicitly reviewed
|
|
12
12
|
* through expectedRevision. Original attribution remains unchanged in history. */
|
|
13
|
-
export async function supersedeTarget(successor: D1ProvenanceRow, before: TargetRow | undefined, context: D1ImportContext, statements: D1Sql[]): Promise<void> {
|
|
13
|
+
export async function supersedeTarget(successor: D1ProvenanceRow, before: TargetRow | undefined, context: D1ImportContext, statements: D1Sql[], snapshot?: any): Promise<void> {
|
|
14
14
|
const db = context.storage.database;
|
|
15
|
-
const prior = await db.prepare('SELECT * FROM ik_provenance WHERE destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(successor.destination, successor.targetKind, successor.targetId).first();
|
|
15
|
+
const prior = snapshot === undefined ? await db.prepare('SELECT * FROM ik_provenance WHERE destination=? AND target_kind=? AND target_id=? AND reversed=0').bind(successor.destination, successor.targetKind, successor.targetId).first() : snapshot;
|
|
16
16
|
if (!prior) {
|
|
17
17
|
statements.push(guard(sql('NOT EXISTS(SELECT 1 FROM ik_provenance WHERE destination=? AND target_kind=? AND target_id=? AND reversed=0)', successor.destination, successor.targetKind, successor.targetId)));
|
|
18
18
|
return;
|
|
@@ -2,6 +2,7 @@ import { captureJobLayout, jobLayoutGuards, jobLayoutReads } from './stream-layo
|
|
|
2
2
|
import { needsStreamOrigin } from './stream-origin.ts';
|
|
3
3
|
import { nativeStagingScope } from './native-staging-context.ts';
|
|
4
4
|
import { streamBoundary } from './stream-boundary.ts';
|
|
5
|
+
import { captureWaveBoundary, waveBoundary } from './wave-boundary.ts';
|
|
5
6
|
import { requestContext } from './budget.ts';
|
|
6
7
|
import { loadPlans, storedPlanSnapshot, planBody, planGuards, planChunkReads, retainPlan, creationCodec, LARGE_PLAN_BYTES } from './plans.ts';
|
|
7
8
|
import { randomUUID } from 'node:crypto';
|
|
@@ -38,10 +39,11 @@ export async function stored(id: string, context: D1ImportContext, action: Impor
|
|
|
38
39
|
job.erasedRecords = plan.entries.filter(entry => entry.erased).length;
|
|
39
40
|
attachReversal(job, await loadReversalState(row, plan, context));
|
|
40
41
|
await captureJobLayout(job, row, context, action);
|
|
42
|
+
await captureWaveBoundary(job, context);
|
|
41
43
|
return { job, plan };
|
|
42
44
|
}
|
|
43
45
|
export function boundary(job: ImportJob, plan: ImportPlan, action: ImportAction = 'read'): D1Sql[] {
|
|
44
|
-
return [...jobLayoutGuards(job), ...streamBoundary(job.id, action), ...planGuards(plan, job.id), ...reversalGuards(job.id, reversalState(job)), guard(sql('EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND fence=? AND checkpoint=? AND state=? AND fingerprint=? AND plan_json=? AND principal_id=? AND destination=? AND namespace=? AND mapping_version=? AND total=? AND error IS ?)', job.id, job.fence, job.checkpoint, job.state, job.fingerprint, planBody(plan), job.principalId, job.destination, job.namespace, job.mappingVersion, job.total, job.error))];
|
|
46
|
+
return [...waveBoundary(job, action), ...jobLayoutGuards(job), ...streamBoundary(job.id, action), ...planGuards(plan, job.id), ...reversalGuards(job.id, reversalState(job)), guard(sql('EXISTS(SELECT 1 FROM ik_jobs WHERE id=? AND fence=? AND checkpoint=? AND state=? AND fingerprint=? AND plan_json=? AND principal_id=? AND destination=? AND namespace=? AND mapping_version=? AND total=? AND error IS ?)', job.id, job.fence, job.checkpoint, job.state, job.fingerprint, planBody(plan), job.principalId, job.destination, job.namespace, job.mappingVersion, job.total, job.error))];
|
|
45
47
|
}
|
|
46
48
|
export async function planD1Import(source: SourceBundle, mapping: ImportMapping, context: D1ImportContext): Promise<ImportPlan> {
|
|
47
49
|
context = requestContext(context);
|
|
@@ -21,8 +21,8 @@ export interface RetirementScope { jobs: string[]; streams: string[]; stages: st
|
|
|
21
21
|
/** Jobs, streams and private stages whose headers hold one of the exact values,
|
|
22
22
|
* closed over everything their deletion would break: whole streams, jobs whose
|
|
23
23
|
* entries reuse a retired job's sources, and preserved history in both directions. */
|
|
24
|
-
export async function retirementScope(db: D1Database, destination: string, values: string[]): Promise<RetirementScope> {
|
|
25
|
-
const jobs = new Set<string>(), streams = new Set<string>(), stages = new Set<string>(), anchors = new Set<string>(), preparations = new Set<string>();
|
|
24
|
+
export async function retirementScope(db: D1Database, destination: string, values: string[], seedJobs: string[] = []): Promise<RetirementScope> {
|
|
25
|
+
const jobs = new Set<string>(seedJobs), streams = new Set<string>(), stages = new Set<string>(), anchors = new Set<string>(), preparations = new Set<string>();
|
|
26
26
|
const exact = JSON.stringify(values);
|
|
27
27
|
const read = async (query: string, ...bound: unknown[]) => (await db.prepare(query).bind(...bound).all<{ id: string }>()).results.map(row => row.id);
|
|
28
28
|
if (values.length) {
|
|
@@ -5,6 +5,7 @@ import { doorRetirementStore } from './retirement-door.ts';
|
|
|
5
5
|
// Bookkeeping that every custody change bumps, plus the erasure's own records.
|
|
6
6
|
const SKIPPED = new Set(['ik_d1_assertions', 'ik_d1_custody_states', 'ik_d1_custody_revisions', 'ik_d1_publication_fences',
|
|
7
7
|
'ik_d1_erasure_tally', 'ik_d1_erasure_journal', 'ik_d1_erasure_progress', 'ik_d1_retirements', 'ik_d1_erased_sources', 'ik_d1_erased_targets', 'ik_targets',
|
|
8
|
+
'ik_door_wave_erased_aliases', 'ik_door_wave_origins', 'ik_door_wave_roots', 'ik_door_wave_children', 'ik_door_wave_members', 'ik_door_wave_approvals',
|
|
8
9
|
'ik_door_settings', // Installation opt-in only; no uploaded record content.
|
|
9
10
|
'ik_door_revoked_keys', // Persistent key revocations must survive erasure.
|
|
10
11
|
'ik_door_grants', // Grant nonce replay protection; no uploaded record content.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { waveRetirementSeeds } from './wave-retirement.ts';
|
|
1
2
|
import { randomUUID } from 'node:crypto';
|
|
2
3
|
import type { D1CustodyRetirement, D1ErasureJournalEntry, D1ImportContext, D1ImportPerson, D1RetirementOptions, D1RetirementProgress, D1Sql } from './types.ts';
|
|
3
4
|
import { requestContext, limitStatements, packGuards, remaining, D1RequestLimitError } from './budget.ts';
|
|
@@ -37,7 +38,14 @@ export function retireD1ImportCustody(person: D1ImportPerson, nativeStatements:
|
|
|
37
38
|
export function retireD1ImportCustody(person: D1ImportPerson, nativeStatements: D1Sql[], input: D1ImportContext, checkpoint: D1Sql[], options: D1ErasureStepOptions): Promise<D1CustodyRetirement | D1ErasurePending>;
|
|
38
39
|
export function retireD1ImportCustody(person: D1ImportPerson, nativeStatements: D1Sql[], input: D1ImportContext, checkpoint: D1Sql[], options: D1RetirementOptions): Promise<D1CustodyRetirement | D1RetirementProgress>;
|
|
39
40
|
export async function retireD1ImportCustody(person: D1ImportPerson, nativeStatements: D1Sql[], input: D1ImportContext, checkpoint: D1Sql[], options: D1RetirementOptions | D1ErasureStepOptions = {}): Promise<D1CustodyRetirement | D1RetirementProgress | D1ErasurePending> {
|
|
40
|
-
if ('operationKey' in options)
|
|
41
|
+
if ('operationKey' in options) {
|
|
42
|
+
const admission=requestContext(input);
|
|
43
|
+
await readAtomic(admission,[],undefined,'erase');
|
|
44
|
+
const db=admission.storage.database;
|
|
45
|
+
const installed=await db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_wave_roots'").first();
|
|
46
|
+
if (person.aliases?.length || installed && await db.prepare('SELECT 1 FROM ik_door_wave_roots WHERE destination=? LIMIT 1').bind(input.destination).first()) throw new Error('IMPORT_WAVE_JOURNAL_REQUIRED');
|
|
47
|
+
return retireD1ImportCustodyStep(person,nativeStatements,admission,checkpoint,options);
|
|
48
|
+
}
|
|
41
49
|
person = { ...person, related: person.related ? { ...person.related, fields: fieldRedactions(person.related.fields) } : undefined };
|
|
42
50
|
const context = limitStatements(requestContext({ ...input, originalErasurePolicy: input.originalErasurePolicy ?? 'withdraw-affected-artifacts' }), 1000);
|
|
43
51
|
if (context.maxBatchBytes !== undefined) throw new Error('IMPORT_RETIREMENT_OPERATION_KEY_REQUIRED');
|
|
@@ -45,16 +53,18 @@ export async function retireD1ImportCustody(person: D1ImportPerson, nativeStatem
|
|
|
45
53
|
for (const value of values) requireText(value, 'person value');
|
|
46
54
|
if (person.target) { requireText(person.target.kind, 'target kind'); requireText(person.target.id, 'target id'); }
|
|
47
55
|
const fields = person.related?.fields ?? [];
|
|
48
|
-
if (!person.target && !values.length && !fields.length) throw new Error('IMPORT_RETIREMENT_PERSON_REQUIRED');
|
|
56
|
+
if (!person.target && !values.length && !fields.length && !person.aliases?.length) throw new Error('IMPORT_RETIREMENT_PERSON_REQUIRED');
|
|
57
|
+
for (const alias of person.aliases ?? []) for (const value of [alias.installationId,alias.namespace,alias.kind,alias.sourceId]) requireText(value,'source alias');
|
|
49
58
|
// Without a native target only field redaction applies: whole-record erasure
|
|
50
59
|
// (`source`, `original`) binds what it severs to the erased target.
|
|
51
60
|
if (person.related && !person.target && (person.related.source || person.related.original || !fields.length)) throw new Error('IMPORT_RETIREMENT_TARGET_REQUIRED');
|
|
52
61
|
await readAtomic(context, [], undefined, 'erase');
|
|
53
62
|
const db = context.storage.database, destination = context.destination;
|
|
54
|
-
const
|
|
63
|
+
const wave = await waveRetirementSeeds(db,destination,person,context.storage.targets.planCodec);
|
|
64
|
+
const scope = await retirementScope(db, destination, values,wave.jobs), retiring = new Set(scope.jobs);
|
|
55
65
|
// Withdrawn door content is custody too: a values-only erasure that removes it is fenced.
|
|
56
66
|
let stores: Store[] | undefined;
|
|
57
|
-
const fenced = Boolean(person.target) || fields.length > 0 || scope.jobs.length + scope.streams.length + scope.stages.length + scope.anchors.length + scope.preparations.length > 0
|
|
67
|
+
const fenced = Boolean(person.target) || !!person.aliases?.length || fields.length > 0 || scope.jobs.length + scope.streams.length + scope.stages.length + scope.anchors.length + scope.preparations.length > 0
|
|
58
68
|
|| await doorCustodyInScope(db, destination, stores = await tallyStores(db));
|
|
59
69
|
if (fenced && !checkpoint.length) throw new Error('IMPORT_RETIREMENT_CHECKPOINT_REQUIRED');
|
|
60
70
|
const fence = fenced ? checkpoint : [];
|
|
@@ -94,7 +104,7 @@ export async function retireD1ImportCustody(person: D1ImportPerson, nativeStatem
|
|
|
94
104
|
touched.streams = [...new Set([...scope.anchors, ...await pageStreams(db, touched.jobs)])].sort();
|
|
95
105
|
stores ??= await tallyStores(db);
|
|
96
106
|
const entry = { recordedAt, recordsErased: erasure.decisions.length, jobsRetired: scope.jobs.length, streamsRetired: scope.anchors.length };
|
|
97
|
-
prefix = packGuards([authority(context, 'erase'), ...tallyBefore(stores, receiptId, touched), ...tallyWithdraw(stores, touched), ...erasure.before, ...scopeGuards(scope), ...lineage.fence.map(guard)]);
|
|
107
|
+
prefix = packGuards([authority(context, 'erase'), ...tallyBefore(stores, receiptId, touched), ...(wave.roots.length ? [sql("UPDATE ik_door_wave_roots SET state='withdrawn' WHERE id IN (SELECT value FROM json_each(?))",JSON.stringify(wave.roots))] : []), ...wave.tombstones.map(alias=>sql('INSERT OR IGNORE INTO ik_door_wave_erased_aliases(installation_id,namespace,destination,identity) VALUES(?,?,?,?)',alias.installation,alias.namespace,destination,alias.identity)), ...tallyWithdraw(stores, touched), ...erasure.before, ...scopeGuards(scope), ...wave.guards.map(guard), ...lineage.fence.map(guard)]);
|
|
98
108
|
suffix = [...packGuards([...erasure.after, ...retirementWrites(scope, destination, receiptId, recordedAt), ...headerAbsence(destination, values),
|
|
99
109
|
...tallyJournal(stores, receiptId, touched, entry, fenced), authority(context, 'erase'), sql('DELETE FROM ik_d1_assertions')]), ...readonlyChecks(erasure.final), finalAuthority(context, 'erase')];
|
|
100
110
|
// One read after the batch returns its journal row.
|
|
@@ -34,11 +34,22 @@ export interface D1TargetAdapter extends Partial<D1PreservedUndoAdapter> {
|
|
|
34
34
|
/** Optional transport staging support. Guards must fence every native field
|
|
35
35
|
* from before the first read, including same-revision edits and absence. */
|
|
36
36
|
stagingRead?(db: D1Database, destination: string, kind: string, id: string): Promise<{ row: TargetRow | undefined; guards: D1Sql[] }>;
|
|
37
|
+
/** Optional selected-key equivalent of N stagingRead calls. Return exactly one
|
|
38
|
+
* {row, guards} per key in input order, repeating duplicate keys and preserving
|
|
39
|
+
* absence. Fence every native field from before its first read, including
|
|
40
|
+
* same-revision edits. Read only selected keys, never list the destination.
|
|
41
|
+
* Preserve overridden stagingRead/read behavior or throw for single-read fallback. */
|
|
42
|
+
stagingReadMany?(db: D1Database, destination: string, keys: readonly { kind: string; id: string }[]): Promise<{ row: TargetRow | undefined; guards: D1Sql[] }[]>;
|
|
37
43
|
/** Protect exact post-state during cleanup, install a known invalidatable
|
|
38
44
|
* receipt after exact checks, then release. Final scalar predicates run
|
|
39
45
|
* read-only after every mutation and must bind the expected post-state. */
|
|
40
46
|
stagingPublication?(stageId: string, destination: string, targets: { kind: string; id: string; present: boolean }[]): { protect: D1Sql[]; seal: D1Sql[]; release: D1Sql[]; final: D1Sql[] };
|
|
41
47
|
read(db: D1Database, destination: string, kind: string, id: string): Promise<TargetRow | undefined>;
|
|
48
|
+
/** Optional selected-key equivalent of N read calls. Return exactly one result
|
|
49
|
+
* per key in input order, repeating duplicate keys and preserving undefined
|
|
50
|
+
* for absence. Read only selected keys, never list the destination. Preserve
|
|
51
|
+
* overridden read behavior or throw so the caller can fall back to singles. */
|
|
52
|
+
readMany?(db: D1Database, destination: string, keys: readonly { kind: string; id: string }[]): Promise<(TargetRow | undefined)[]>;
|
|
42
53
|
matches(row: TargetRow | null, destination: string, kind: string, id: string): D1Sql;
|
|
43
54
|
/** Optional exact row snapshot split into conjunctive predicates. All parts,
|
|
44
55
|
* including absence/cardinality, commit in the same native transaction. */
|
|
@@ -123,6 +134,9 @@ export interface D1ProvenanceRow extends ProvenanceRow { predecessorId?: string
|
|
|
123
134
|
* sit whole in a job or stream header, an original's name or a private stage.
|
|
124
135
|
* Nothing is ever matched as a substring. */
|
|
125
136
|
export interface D1ImportPerson {
|
|
137
|
+
/** Server-derived source subjects, including context/metadata subjects that
|
|
138
|
+
* never became native targets. Never accept this selector from a client. */
|
|
139
|
+
aliases?: readonly { installationId: string; namespace: string; kind: string; sourceId: string }[];
|
|
126
140
|
target?: { kind: string; id: string };
|
|
127
141
|
values?: string[];
|
|
128
142
|
related?: D1RelatedErasure;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ImportAction, ImportJob } from '../types.ts';
|
|
2
|
+
import type { D1ImportContext, D1Sql } from './types.ts';
|
|
3
|
+
import { guard, sql } from './database.ts';
|
|
4
|
+
|
|
5
|
+
const contexts = new WeakMap<D1ImportContext['authorization'], {parent:string;mode:string;database:D1ImportContext['storage']['database']}>();
|
|
6
|
+
const jobs = new WeakMap<ImportJob, {parent:string;ordinal:number;allowed:boolean;mode:string}>();
|
|
7
|
+
/** A direct ordinary apply/undo must not promote a prepared wave's child. */
|
|
8
|
+
export function bindWaveContext(context: D1ImportContext, parent: string, mode: string) {
|
|
9
|
+
contexts.set(context.authorization,{parent,mode,database:context.storage.database});
|
|
10
|
+
}
|
|
11
|
+
export async function captureWaveBoundary(job: ImportJob, context: D1ImportContext) {
|
|
12
|
+
if (!job.namespace.startsWith('door-wave:')) return;
|
|
13
|
+
const db=context.storage.database;
|
|
14
|
+
if (!await db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_wave_children'").first()) return;
|
|
15
|
+
const row=await db.prepare(`SELECT c.parent_id,c.ordinal FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id
|
|
16
|
+
WHERE p.job_id=? OR ('door:'||p.id)=(SELECT operation_key FROM ik_jobs WHERE id=?)`).bind(job.id,job.id).first<any>();
|
|
17
|
+
if (!row) {jobs.set(job,{parent:'',ordinal:-1,allowed:false,mode:''});return;}
|
|
18
|
+
const scope=contexts.get(context.authorization);
|
|
19
|
+
jobs.set(job,{parent:row.parent_id,ordinal:row.ordinal,allowed:scope?.parent===row.parent_id && scope?.database===db,mode:scope?.mode ?? ''});
|
|
20
|
+
}
|
|
21
|
+
export function waveBoundary(job: ImportJob, action: ImportAction): D1Sql[] {
|
|
22
|
+
const row=jobs.get(job);
|
|
23
|
+
if (!row || !['apply','undo','rollback'].includes(action)) return [];
|
|
24
|
+
const forward=action==='apply';
|
|
25
|
+
if (!row.allowed || row.mode!==(forward?'apply':'reverse')) return [guard(sql('0'))];
|
|
26
|
+
return [guard(sql(`EXISTS(SELECT 1 FROM ik_door_wave_roots r JOIN ik_door_wave_children c ON c.parent_id=r.id
|
|
27
|
+
JOIN ik_door_plans p ON p.id=c.plan_id WHERE r.id=? AND c.ordinal=? AND p.job_id=?
|
|
28
|
+
AND EXISTS(SELECT 1 FROM ik_door_wave_origins o WHERE o.job_id=p.job_id AND o.parent_id=r.id AND o.ordinal=c.ordinal AND o.plan_id=p.id AND o.fingerprint=p.fingerprint)
|
|
29
|
+
AND r.state IN (${forward?"'applying','completed'":"'reversing'"})
|
|
30
|
+
AND EXISTS(SELECT 1 FROM ik_door_wave_approvals a WHERE a.parent_id=r.id AND a.ordinal=-1 AND a.revoked=0 AND a.fingerprint=r.fingerprint)
|
|
31
|
+
AND NOT EXISTS(SELECT 1 FROM ik_door_wave_children peer JOIN ik_door_plans pp ON pp.id=peer.plan_id LEFT JOIN ik_jobs j ON j.id=pp.job_id
|
|
32
|
+
WHERE peer.parent_id=r.id AND peer.ordinal ${forward?'<':'>'} c.ordinal AND ${forward?"(j.id IS NULL OR j.state<>'completed')":"(j.id IS NOT NULL AND j.state NOT IN ('undone','rolled_back'))"}))`,row.parent,row.ordinal,job.id))];
|
|
33
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { D1Database, D1ImportPerson, D1PlanCodec, D1Sql } from './types.ts';
|
|
2
|
+
import { targetIdentity } from './erasure-index.ts';
|
|
3
|
+
import { sql } from './database.ts';
|
|
4
|
+
import { doorLineage } from './retirement-door.ts';
|
|
5
|
+
import { canonicalJson, sha256 } from '../provenance/identity.ts';
|
|
6
|
+
|
|
7
|
+
/** Wave snapshots are indivisible custody. Retire their actual child journals
|
|
8
|
+
* whole before ordinary erasure tries to rewrite an attachment-bearing entry.
|
|
9
|
+
* The caller still deletes only the selected person's native data. Peer native
|
|
10
|
+
* records remain; this is withdrawal, never successful Undo or reconciliation. */
|
|
11
|
+
export async function waveRetirementSeeds(db: D1Database, destination: string, person: D1ImportPerson, codec?: D1PlanCodec): Promise<{jobs:string[];roots:string[];tombstones:{installation:string;namespace:string;identity:string}[];guards:D1Sql[]}> {
|
|
12
|
+
const empty={jobs:[],roots:[],tombstones:[],guards:[]};
|
|
13
|
+
if (!person.target && !person.aliases?.length) return empty;
|
|
14
|
+
if (!await db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='ik_door_wave_children'").first()) {
|
|
15
|
+
if (person.aliases?.length) throw new Error('IMPORT_WAVE_SCHEMA_REQUIRED');
|
|
16
|
+
return empty;
|
|
17
|
+
}
|
|
18
|
+
if (!person.aliases?.length && !await db.prepare('SELECT 1 FROM ik_door_wave_roots WHERE destination=? LIMIT 1').bind(destination).first()) return empty;
|
|
19
|
+
const lineage=await doorLineage(person,db,destination,codec);
|
|
20
|
+
const tombstones=[...lineage.sources.map(source=>{
|
|
21
|
+
const [installation,namespace,kind,id]=JSON.parse(source);
|
|
22
|
+
return {installation,namespace,identity:sha256(canonicalJson([kind,id]))};
|
|
23
|
+
}),...(person.aliases ?? []).map(a=>({installation:a.installationId,namespace:a.namespace,identity:sha256(canonicalJson([a.kind,a.sourceId]))}))];
|
|
24
|
+
const scopes=JSON.stringify(tombstones);
|
|
25
|
+
const selection=`WITH RECURSIVE affected(parent_id) AS (
|
|
26
|
+
SELECT DISTINCT c.parent_id FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id
|
|
27
|
+
JOIN ik_jobs j ON j.id=p.job_id OR j.operation_key='door:'||p.id JOIN ik_d1_plan_index i ON i.job_id=j.id
|
|
28
|
+
WHERE j.destination=? AND i.target_identity=?
|
|
29
|
+
UNION SELECT m.parent_id FROM ik_door_wave_members m JOIN ik_door_wave_roots r ON r.id=m.parent_id JOIN json_each(?) s
|
|
30
|
+
ON r.installation_id=json_extract(s.value,'$.installation') AND r.namespace=json_extract(s.value,'$.namespace')
|
|
31
|
+
AND m.identity=json_extract(s.value,'$.identity') WHERE r.destination=? AND m.type IN ('source','alias')
|
|
32
|
+
UNION SELECT r.id FROM ik_door_wave_roots r JOIN affected a ON r.previous_id=a.parent_id JOIN ik_door_wave_roots parent ON parent.id=a.parent_id WHERE r.destination=? AND r.installation_id=parent.installation_id AND r.namespace=parent.namespace
|
|
33
|
+
) SELECT parent_id FROM affected`;
|
|
34
|
+
const digest=person.target ? targetIdentity(destination,person.target.kind,person.target.id) : null;
|
|
35
|
+
const parameters=[destination,digest,scopes,destination,destination];
|
|
36
|
+
const roots=(await db.prepare(selection).bind(...parameters).all<{parent_id:string}>()).results.map(row=>row.parent_id);
|
|
37
|
+
if (!roots.length) return {jobs:[],roots:[],tombstones,guards:[]};
|
|
38
|
+
const rootJson=JSON.stringify(roots);
|
|
39
|
+
const members=`SELECT j.id FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id
|
|
40
|
+
JOIN ik_jobs j ON j.id=p.job_id OR j.operation_key='door:'||p.id WHERE j.destination=? AND c.parent_id IN (SELECT value FROM json_each(?))`;
|
|
41
|
+
const jobs=(await db.prepare(members).bind(destination,rootJson).all<{id:string}>()).results.map(row=>row.id);
|
|
42
|
+
return {jobs,roots,tombstones,guards:[
|
|
43
|
+
...lineage.fence,
|
|
44
|
+
sql(`NOT EXISTS(SELECT 1 FROM (${selection}) WHERE parent_id NOT IN (SELECT value FROM json_each(?)))`,...parameters,rootJson),
|
|
45
|
+
sql(`NOT EXISTS(SELECT 1 FROM (${members}) WHERE id NOT IN (SELECT value FROM json_each(?)))`,destination,rootJson,JSON.stringify(jobs)),
|
|
46
|
+
]};
|
|
47
|
+
}
|
|
@@ -142,6 +142,10 @@ function memberKey(key: string): ReturnType<typeof credentialKey> {
|
|
|
142
142
|
const name = credentialMember(decodedCredentialText(key));
|
|
143
143
|
return name === undefined ? undefined : { name, holds: value => !emptyCredential(value) };
|
|
144
144
|
}
|
|
145
|
+
/** The parsed URL, or undefined for text that looks like one but does not parse. */
|
|
146
|
+
function parseUrl(text: string): URL | undefined {
|
|
147
|
+
try { return new URL(text, 'https://credential-check.invalid'); } catch { return undefined; }
|
|
148
|
+
}
|
|
145
149
|
const looksLikeUrl = (value: string) => /^(?:https?:\/\/|\/|\?)/i.test(decodedCredentialText(value));
|
|
146
150
|
|
|
147
151
|
export function assertNoCredentials(value: unknown, policy?: PaginationCursorPolicy): void {
|
|
@@ -167,14 +171,19 @@ export function assertNoCredentials(value: unknown, policy?: PaginationCursorPol
|
|
|
167
171
|
} else if (typeof item === 'string') {
|
|
168
172
|
// Relative pagination links receive the same check as absolute links.
|
|
169
173
|
if (!looksLikeUrl(item)) return;
|
|
170
|
-
|
|
174
|
+
// Free text that merely starts like a URL (`http:// see below`) does not parse: it is prose,
|
|
175
|
+
// with no user name to find. Its raw text still goes through the query-key scans below.
|
|
176
|
+
const url = parseUrl(item);
|
|
171
177
|
const structure = decodedCredentialText(credentialUrlStructure(item, policy).split('#')[0]);
|
|
172
|
-
const decodedUrl =
|
|
173
|
-
if (url
|
|
174
|
-
|
|
178
|
+
const decodedUrl = parseUrl(structure);
|
|
179
|
+
if (url?.username || url?.password || decodedUrl?.username || decodedUrl?.password) throw new CredentialError('CREDENTIAL_IN_DURABLE_URL', { kind: 'url-userinfo' });
|
|
180
|
+
const rawQueryStart = item.indexOf('?'), rawFragmentStart = item.indexOf('#');
|
|
181
|
+
const search = url ? url.search.slice(1)
|
|
182
|
+
: rawQueryStart < 0 || (rawFragmentStart >= 0 && rawFragmentStart < rawQueryStart) ? '' : item.slice(rawQueryStart + 1, rawFragmentStart < 0 ? item.length : rawFragmentStart);
|
|
183
|
+
for (const pair of search.split('&')) {
|
|
175
184
|
if (credentialQueryKey(pair.split('=')[0], policy)) throw new CredentialError('CREDENTIAL_IN_DURABLE_URL', { kind: 'url-query' });
|
|
176
185
|
}
|
|
177
|
-
for (const child of url.searchParams.values()) visit(child);
|
|
186
|
+
if (url) for (const child of url.searchParams.values()) visit(child);
|
|
178
187
|
// Encoded delimiters outside opaque values must not hide credential keys.
|
|
179
188
|
// Any key introduced by decoding has no exact-wire cursor exemption.
|
|
180
189
|
// A decoded fragment delimiter must not truncate these query checks either.
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
ImporterRecord,
|
|
9
9
|
ImporterValidationIssue,
|
|
10
10
|
ImporterValidationResult,
|
|
11
|
+
MoveInImporterRecord,
|
|
11
12
|
VerifiedImporter,
|
|
12
13
|
} from './types.ts';
|
|
13
14
|
|
|
@@ -23,7 +24,7 @@ const strayPercent = /%(?![0-9A-Fa-f]{2})/;
|
|
|
23
24
|
const nonAscii = /[^\x00-\x7f]/;
|
|
24
25
|
|
|
25
26
|
export async function validateImporterEvidence(
|
|
26
|
-
records: ImporterRecord
|
|
27
|
+
records: Array<ImporterRecord | MoveInImporterRecord>,
|
|
27
28
|
evidence: ImporterEvidencePort | undefined,
|
|
28
29
|
issues: ImporterValidationIssue[],
|
|
29
30
|
): Promise<ImporterValidationResult['evidence']> {
|
|
@@ -37,6 +38,7 @@ export async function validateImporterEvidence(
|
|
|
37
38
|
for (let index = 0; index < records.length; index += 1) {
|
|
38
39
|
const record = records[index]!;
|
|
39
40
|
const path = `$.importers[${index}]`;
|
|
41
|
+
if (!('evidence' in record)) continue;
|
|
40
42
|
const reference = record.evidence as ImporterEvidenceReference;
|
|
41
43
|
const receipt = await resolveAndCheckArtifact(reference, evidence, `${path}.evidence`, issues, counters);
|
|
42
44
|
if (!receipt) continue;
|