@runeditrun/rer 0.2.98 → 0.2.100
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 +1 -1
- package/cli/README.md +88 -2
- package/cli/business/api.mjs +4 -2
- package/cli/business/bounded-waves.d.mts +23 -0
- package/cli/business/bounded-waves.mjs +57 -0
- package/cli/business/catchup-task.mjs +9 -2
- package/cli/business/command.mjs +16 -4
- package/cli/business/connect.mjs +39 -33
- package/cli/business/export-inputs.mjs +12 -2
- package/cli/business/export-task.mjs +9 -2
- package/cli/business/legacy-package.mjs +117 -0
- package/cli/business/local-commands.mjs +10 -1
- package/cli/business/package-transfer.mjs +127 -0
- package/cli/business/questions.mjs +2 -2
- package/cli/business/state.mjs +47 -5
- package/cli/business/supervisor.mjs +46 -7
- package/cli/business/vendor-door.mjs +86 -13
- package/cli/business/vendors/README.md +41 -2
- package/cli/business/vendors/common.mjs +81 -0
- package/cli/business/vendors/index.mjs +12 -2
- package/cli/business/vendors/zendesk-attachments.mjs +57 -0
- package/cli/business/vendors/zendesk-encode.mjs +133 -0
- package/cli/business/vendors/zendesk-guide.mjs +34 -0
- package/cli/business/vendors/zendesk-package.mjs +28 -0
- package/cli/business/vendors/zendesk-pages.mjs +61 -0
- package/cli/business/vendors/zendesk-people.mjs +7 -0
- package/cli/business/vendors/zendesk-store.mjs +154 -0
- package/cli/business/vendors/zendesk-tickets.mjs +21 -0
- package/cli/business/vendors/zendesk-token.mjs +31 -0
- package/cli/business/vendors/zendesk-transport.mjs +48 -0
- package/cli/business/vendors/zendesk.mjs +61 -0
- package/cli/deploy-command.mjs +5 -4
- package/cli/deploy-inputs.mjs +1 -1
- package/cli/deploy.mjs +6 -4
- package/cli/production-auth.mjs +16 -3
- package/cli/production-mail.mjs +18 -1
- package/cli/site.mjs +1 -1
- package/global/CHANGELOG.md +42 -0
- package/global/README.md +1 -1
- package/modules/deploy-kit/README.md +10 -0
- package/modules/deploy-kit/provider.mjs +18 -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 +3 -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/door/ADOPTING.md +37 -0
- package/modules/import-kit/src/door/README.md +66 -0
- package/modules/import-kit/src/door/admission.ts +13 -3
- package/modules/import-kit/src/door/authority.ts +1 -0
- package/modules/import-kit/src/door/failures.ts +1 -1
- package/modules/import-kit/src/door/grants.ts +19 -5
- package/modules/import-kit/src/door/handler.ts +12 -1
- package/modules/import-kit/src/door/index.ts +6 -0
- package/modules/import-kit/src/door/package.ts +121 -0
- package/modules/import-kit/src/door/planning.ts +44 -19
- package/modules/import-kit/src/door/protocol.ts +4 -2
- package/modules/import-kit/src/door/schema.ts +2 -0
- package/modules/import-kit/src/door/store.ts +2 -2
- package/modules/import-kit/src/door/types.ts +24 -1
- package/modules/import-kit/src/door/waves/README.md +158 -0
- package/modules/import-kit/src/door/waves/digests.ts +38 -0
- package/modules/import-kit/src/door/waves/inventory.ts +84 -0
- package/modules/import-kit/src/door/waves/journal.ts +19 -0
- package/modules/import-kit/src/door/waves/lifecycle.ts +242 -0
- package/modules/import-kit/src/door/waves/preparation.ts +190 -0
- package/modules/import-kit/src/door/waves/schema.ts +73 -0
- package/modules/import-kit/src/door/waves/store.ts +112 -0
- package/modules/import-kit/src/door/waves/types.ts +76 -0
- 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
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import {inventoryMember, type WaveInventoryMember} from './digests.ts';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
import type { DoorRequest, DoorSnapshot } from '../types.ts';
|
|
5
|
+
import type { MappingResult, SourceBundle } from '../../types.ts';
|
|
6
|
+
import { limitsFor, RecordCollector } from '../../readers/records.ts';
|
|
7
|
+
import { planD1Import } from '../../d1/jobs.ts';
|
|
8
|
+
import { packageUpload } from '../package.ts';
|
|
9
|
+
import { bundleBytes, uploadRow, text } from '../uploads.ts';
|
|
10
|
+
import { DoorError } from '../protocol.ts';
|
|
11
|
+
import { sourceRefusal } from '../refusals.ts';
|
|
12
|
+
import { assertion, lineage, sql, transact } from '../store.ts';
|
|
13
|
+
import { forget, refuseErased } from '../planning.ts';
|
|
14
|
+
import { recordKey, importedTargets } from '../inventory.ts';
|
|
15
|
+
import { BOUNDED_WAVES, totalKeys, zeroTotals, type WaveCensus, type WaveDescriptor, type WaveMetadata, type WaveRoot } from './types.ts';
|
|
16
|
+
import { authority, children, hash, opaque, revision, rootFence, rootRow, saveChild } from './store.ts';
|
|
17
|
+
import { prior, snapshotRecords } from './inventory.ts';
|
|
18
|
+
|
|
19
|
+
const count = (n: unknown): n is number => Number.isSafeInteger(n) && (n as number)>=0;
|
|
20
|
+
const digest = (s: unknown): s is string => typeof s === 'string' && /^[a-f0-9]{64}$/.test(s);
|
|
21
|
+
const byKind = (v: Record<string,number>) => v && typeof v==='object' && !Array.isArray(v) && Object.entries(v).every(([k,n])=>k.length>0 && count(n));
|
|
22
|
+
function census(value: WaveCensus): WaveCensus {
|
|
23
|
+
if (!value || Object.keys(value).some(k=>![...totalKeys,'children','byKind','nativeByTable','inventoryHash','relationshipsHash','baselineHash','sourceMode','highWater','accountingVersion'].includes(k)) || !totalKeys.every(k=>count(value[k])) || !count(value.children) || !value.children || value.sourceMode !== 'complete'
|
|
24
|
+
|| !byKind(value.byKind) || !byKind(value.nativeByTable) || Object.values(value.byKind).reduce((a,b)=>a+b,0)!==value.source
|
|
25
|
+
|| Object.values(value.nativeByTable).reduce((a,b)=>a+b,0)!==value.native
|
|
26
|
+
|| ![value.inventoryHash,value.relationshipsHash,value.baselineHash].every(digest) || !opaque(value.accountingVersion) || !opaque(value.highWater)) throw new DoorError('invalid_complete_census',422);
|
|
27
|
+
return JSON.parse(JSON.stringify(value));
|
|
28
|
+
}
|
|
29
|
+
export function planningContext(request: DoorRequest, namespace: string) {
|
|
30
|
+
return {destination:request.options.destination,namespace,mappingVersion:request.options.mapping.version,
|
|
31
|
+
storage:{database:request.options.database,targets:request.options.targets},authorization:{principalId:request.options.ownerPrincipalId,predicate:()=>authority(request)}};
|
|
32
|
+
}
|
|
33
|
+
async function begin(request: DoorRequest, input: Record<string,unknown>) {
|
|
34
|
+
const b = revision(request), upload = await uploadRow(request,text(input.uploadId,'upload_id'));
|
|
35
|
+
const pkg = await packageUpload(request,upload), totals = census(await request.options.bounded!.describe(pkg));
|
|
36
|
+
if (pkg.manifest.records.total!==null && (totals.source!==pkg.manifest.records.total || hash(totals.byKind)!==hash(pkg.manifest.records.byKind))) throw new DoorError('package_record_count_mismatch',422);
|
|
37
|
+
const binding = {protocol:BOUNDED_WAVES,migration:b.migration,revision:b.revision,policy:b.policy};
|
|
38
|
+
const priorRoot = await request.options.database.prepare("SELECT * FROM ik_door_wave_roots WHERE installation_id=? AND namespace=? AND json_extract(binding_json,'$.migration')=? AND json_extract(binding_json,'$.revision')=?")
|
|
39
|
+
.bind(request.access.installationId,request.access.namespace,b.migration,b.revision).first<WaveRoot>();
|
|
40
|
+
if (priorRoot) {
|
|
41
|
+
if (priorRoot.bundle_sha256!==upload.bundle_sha256 || priorRoot.binding_json!==JSON.stringify(binding) || priorRoot.state==='cancelled') throw new DoorError('parent_revision_immutable',409);
|
|
42
|
+
return rootRow(request,{parentId:priorRoot.id});
|
|
43
|
+
}
|
|
44
|
+
const current = await lineage(request);
|
|
45
|
+
if (current.pending_id || current.head_id) {
|
|
46
|
+
const held=await request.options.database.prepare('SELECT state,epoch FROM ik_door_wave_roots WHERE id=?').bind(current.pending_id ?? current.head_id).first<any>();
|
|
47
|
+
if (held && (held.state==='withdrawn' || held.epoch!==request.epoch)) throw new DoorError('source_erased',410);
|
|
48
|
+
}
|
|
49
|
+
if (current.pending_id) throw new DoorError('namespace_has_pending_import',409);
|
|
50
|
+
if (!!current.head_id !== (input.catchup === true)) throw new DoorError(current.head_id?'catchup_required':'catchup_requires_baseline',409);
|
|
51
|
+
const root: WaveRoot = {id:randomUUID(),installation_id:request.access.installationId,namespace:request.access.namespace,destination:request.options.destination,
|
|
52
|
+
owner:request.access.email,mapping_version:request.options.mapping.version,bundle_sha256:upload.bundle_sha256,upload_id:upload.id,previous_id:current.head_id,
|
|
53
|
+
epoch:request.epoch,binding_json:JSON.stringify(binding),census_json:JSON.stringify(totals),fingerprint:null,state:'preparing',pages:0,chain:hash([]),catchup:input.catchup===true?(request.access.catchup==='bounded'?1:2):0};
|
|
54
|
+
// No implicit conversion of an ordinary or withdrawn baseline.
|
|
55
|
+
if (root.previous_id) {
|
|
56
|
+
await prior(request,root);
|
|
57
|
+
const previous=await request.options.database.prepare('SELECT binding_json FROM ik_door_wave_roots WHERE id=?').bind(root.previous_id).first<any>();
|
|
58
|
+
if (!previous || JSON.parse(previous.binding_json).migration!==b.migration) throw new DoorError('migration_lineage_changed',409);
|
|
59
|
+
}
|
|
60
|
+
await transact(request.options.database,authority(request),[
|
|
61
|
+
sql('INSERT OR IGNORE INTO ik_door_namespaces(installation_id,namespace) VALUES(?,?)',root.installation_id,root.namespace),
|
|
62
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_namespaces WHERE installation_id=? AND namespace=? AND head_id IS ? AND pending_id IS NULL)',root.installation_id,root.namespace,root.previous_id)),
|
|
63
|
+
assertion(sql("? IS NULL OR EXISTS(SELECT 1 FROM ik_door_wave_roots WHERE id=? AND state='completed' AND fingerprint=? AND epoch=?)",root.previous_id,root.previous_id,totals.baselineHash,root.epoch)),
|
|
64
|
+
sql(`INSERT INTO ik_door_wave_roots(id,installation_id,namespace,destination,owner,mapping_version,bundle_sha256,upload_id,previous_id,epoch,binding_json,census_json,fingerprint,state,pages,chain,catchup) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,...Object.values(root)),
|
|
65
|
+
sql('UPDATE ik_door_namespaces SET pending_id=? WHERE installation_id=? AND namespace=?',root.id,root.installation_id,root.namespace),
|
|
66
|
+
]);
|
|
67
|
+
return root;
|
|
68
|
+
}
|
|
69
|
+
export async function prepare(request: DoorRequest, input: Record<string,unknown>) {
|
|
70
|
+
if (input.cancel===true) {
|
|
71
|
+
const root=await rootRow(request,input);
|
|
72
|
+
const clear=sql("NOT EXISTS(SELECT 1 FROM ik_door_wave_approvals WHERE parent_id=?) AND NOT EXISTS(SELECT 1 FROM ik_door_wave_children c JOIN ik_jobs j ON j.operation_key='door:'||c.plan_id WHERE c.parent_id=?)",root.id,root.id);
|
|
73
|
+
await transact(request.options.database,authority(request,clear),[
|
|
74
|
+
assertion(rootFence(root)),
|
|
75
|
+
sql('DELETE FROM ik_door_plan_chunks WHERE plan_id IN (SELECT plan_id FROM ik_door_wave_children WHERE parent_id=?)',root.id),
|
|
76
|
+
sql("UPDATE ik_door_wave_roots SET state='cancelled' WHERE id=?",root.id),
|
|
77
|
+
sql('UPDATE ik_door_namespaces SET pending_id=NULL WHERE installation_id=? AND namespace=? AND pending_id=?',root.installation_id,root.namespace,root.id),
|
|
78
|
+
]);
|
|
79
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,state:'cancelled',aggregateSuccess:false};
|
|
80
|
+
}
|
|
81
|
+
try { return await preparePage(request,input); }
|
|
82
|
+
catch (caught) {
|
|
83
|
+
const refused=caught instanceof DoorError ? caught : sourceRefusal(caught);
|
|
84
|
+
if (refused && ['source_erased','source_credential','source_invalid_or_credential_source_data'].includes(refused.code)) {
|
|
85
|
+
const row=input.parentId ? await request.options.database.prepare('SELECT upload_id,id FROM ik_door_wave_roots WHERE id=? AND installation_id=? AND namespace=?').bind(input.parentId,request.access.installationId,request.access.namespace).first<any>() : null;
|
|
86
|
+
const upload=await uploadRow(request,text(row?.upload_id ?? input.uploadId,'upload_id'));
|
|
87
|
+
const segments: import('../uploads.ts').UploadRow[]=[];
|
|
88
|
+
try { await packageUpload(request,upload,rows=>segments.push(...rows)); } catch { /* Already missing custody stays missing. */ }
|
|
89
|
+
await forget(request,upload,segments);
|
|
90
|
+
const root=row ?? await request.options.database.prepare('SELECT id FROM ik_door_wave_roots WHERE upload_id=? AND installation_id=? AND namespace=?').bind(upload.id,request.access.installationId,request.access.namespace).first<any>();
|
|
91
|
+
if (root) await transact(request.options.database,authority(request),[
|
|
92
|
+
sql('DELETE FROM ik_door_plan_chunks WHERE plan_id IN (SELECT plan_id FROM ik_door_wave_children WHERE parent_id=?)',root.id),
|
|
93
|
+
sql("UPDATE ik_door_wave_roots SET state='withdrawn' WHERE id=?",root.id),
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
throw refused ?? caught;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async function preparePage(request: DoorRequest, input: Record<string,unknown>) {
|
|
100
|
+
if (Object.keys(input).some(k=>!['parentId','uploadId','ordinal','catchup'].includes(k)) || !count(input.ordinal) || input.catchup!==undefined && typeof input.catchup!=='boolean') throw new DoorError('invalid_prepare_input',400);
|
|
101
|
+
const root = input.parentId ? await rootRow(request,input) : await begin(request,input);
|
|
102
|
+
const expected = JSON.parse(root.census_json) as WaveCensus;
|
|
103
|
+
if (input.ordinal < root.pages) {
|
|
104
|
+
const child = (await children(request,root))[input.ordinal];
|
|
105
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,state:root.state,next:root.pages,child:{ordinal:child.ordinal,fingerprint:child.fingerprint}};
|
|
106
|
+
}
|
|
107
|
+
if (root.state !== 'preparing' || input.ordinal !== root.pages || root.pages >= expected.children) throw new DoorError('preparation_cursor_mismatch',409);
|
|
108
|
+
const upload = await uploadRow(request,root.upload_id), segments: any[]=[];
|
|
109
|
+
const pkg = await packageUpload(request,upload,rows=>segments.push(...rows));
|
|
110
|
+
if (hash(census(await request.options.bounded!.describe(pkg))) !== hash(expected)) throw new DoorError('preparation_census_changed',409);
|
|
111
|
+
const limits = limitsFor({...request.options.limits,maxRecords:Math.min(request.access.maxRecords,request.options.limits?.maxRecords ?? request.access.maxRecords)});
|
|
112
|
+
const unit = await request.options.bounded!.page(pkg,root.pages,Object.freeze({...limits}));
|
|
113
|
+
if (!unit || Object.keys(unit).some(k=>!['id','dependsOn','records','files','aliases','relationshipsHash'].includes(k)) || !opaque(unit.id) || !Array.isArray(unit.dependsOn) || unit.dependsOn.some(d=>!opaque(d)) || new Set(unit.dependsOn).size!==unit.dependsOn.length
|
|
114
|
+
|| !Array.isArray(unit.aliases) || unit.aliases.some(a=>!a || Object.keys(a).some(k=>!['kind','sourceId','role','files'].includes(k)) || typeof a.kind!=='string' || !a.kind || typeof a.sourceId!=='string' || !a.sourceId || !['context','archive','rejected','metadata'].includes(a.role) || !Array.isArray(a.files) || a.files.some(path=>typeof path!=='string' || !pkg.files.some(f=>f.path===path)))
|
|
115
|
+
|| !Array.isArray(unit.records) || !unit.records.length || unit.records.some(r=>!r || Object.keys(r).some(k=>!['kind','sourceId','payload','pointer'].includes(k))) || !Array.isArray(unit.files) || unit.files.some(f=>!f || Object.keys(f).some(k=>!['path','role','logicalBytes'].includes(k))) || !digest(unit.relationshipsHash)) throw new DoorError('invalid_native_work_unit',422);
|
|
116
|
+
if (unit.records.length>limits.maxRecords) throw new DoorError('child_exceeds_source_quota',413,{source:unit.records.length,limit:limits.maxRecords});
|
|
117
|
+
const metadata={id:unit.id,dependsOn:[...unit.dependsOn],relationshipsHash:unit.relationshipsHash,files:unit.files.map(f=>({...f})),aliases:unit.aliases.map(a=>({...a,files:[...a.files]}))},sourceCount=unit.records.length;
|
|
118
|
+
const previousChildren = await children(request,root), dependsOn = metadata.dependsOn.map(hash);
|
|
119
|
+
if (dependsOn.some(dep=>!previousChildren.some(child=>child.unit_hash===dep))) throw new DoorError('child_dependency_unprepared',409);
|
|
120
|
+
const namespace = 'door-wave:' + hash([root.installation_id,root.namespace,root.destination,JSON.parse(root.binding_json).migration,JSON.parse(root.binding_json).revision]);
|
|
121
|
+
const collector = new RecordCollector(namespace,limits);
|
|
122
|
+
collector.add(unit.records,{artifact:'rer-package.json'},root.bundle_sha256);
|
|
123
|
+
const totals = zeroTotals(), members: WaveInventoryMember[] = [];
|
|
124
|
+
const source: SourceBundle = collector.bundle;
|
|
125
|
+
source.artifacts.push({name:'rer-package.json',sha256:root.bundle_sha256,size:upload.total_bytes,original:{status:'available' as const,base64:Buffer.from(await bundleBytes(request,upload)).toString('base64')}});
|
|
126
|
+
const fileRoles = new Set<string>();
|
|
127
|
+
for (const file of metadata.files) {
|
|
128
|
+
const declared = pkg.files.find(f=>f.path===file.path);
|
|
129
|
+
if (!declared || fileRoles.has(file.path) || !['source','object','context'].includes(file.role) || !count(file.logicalBytes)) throw new DoorError('invalid_work_unit_file',422);
|
|
130
|
+
fileRoles.add(file.path);
|
|
131
|
+
const bytes = await pkg.read(file.path);
|
|
132
|
+
source.artifacts.push({name:file.path,sha256:declared.sha256,size:bytes.length,original:{status:'available',base64:Buffer.from(bytes).toString('base64')}});
|
|
133
|
+
members.push(inventoryMember('file',file.path,[declared.sha256,declared.bytes,file.role,file.logicalBytes]));
|
|
134
|
+
totals.storedBytes+=bytes.length; totals.logicalBytes+=file.logicalBytes;
|
|
135
|
+
if (file.role==='object') totals.objects++;
|
|
136
|
+
if (file.role==='context') totals.contextBytes+=bytes.length;
|
|
137
|
+
}
|
|
138
|
+
const old = await prior(request,root), id = randomUUID();
|
|
139
|
+
const differences: DoorSnapshot['differences'] = {additions:[],replacements:[],unchanged:[],gone:[],conflicts:[]};
|
|
140
|
+
const mapper = {version:root.mapping_version,map(record: SourceBundle['records'][number]): MappingResult {
|
|
141
|
+
const key=recordKey(record), before=old.get(key);
|
|
142
|
+
if (before && before.sourceHash===hash([record.payloadHash,metadata.relationshipsHash,root.mapping_version])) {
|
|
143
|
+
differences.unchanged.push(key); return {effects:[],unmapped:[{reason:'Unchanged source; exact earlier journal retained.'}]};
|
|
144
|
+
}
|
|
145
|
+
const mapped=request.options.mapping.map(record);
|
|
146
|
+
if (before?.targets.length && !mapped.rejection) {
|
|
147
|
+
const keys=mapped.effects.map(e=>hash([e.targetKind,e.targetId])).sort(), previous=before.targets.map(t=>hash([t.kind,t.id])).sort();
|
|
148
|
+
if (hash(keys)!==hash(previous)) throw new DoorError('changed_target_set_requires_proposal',409);
|
|
149
|
+
}
|
|
150
|
+
if (mapped.rejection) differences.conflicts.push(key);
|
|
151
|
+
else if (mapped.effects.length) (before?.targets.length?differences.replacements:differences.additions).push(key);
|
|
152
|
+
return mapped;
|
|
153
|
+
}};
|
|
154
|
+
// Establish earlier ownership before deriving replacement revisions.
|
|
155
|
+
const oldTargets=[...old.values()].flatMap(r=>r.targets), found=await importedTargets(request,oldTargets);
|
|
156
|
+
if (found.some(row=>!row.matches)) throw new DoorError('native_edit_conflict',409);
|
|
157
|
+
const revisions=new Map(oldTargets.map((t,i)=>[hash([t.kind,t.id]),found[i].provenance.appliedRevision]));
|
|
158
|
+
const mapping={...mapper,map(record:typeof source.records[number]) {const mapped=mapper.map(record);return {...mapped,effects:mapped.effects.map(e=>revisions.has(hash([e.targetKind,e.targetId]))?{...e,expectedRevision:revisions.get(hash([e.targetKind,e.targetId]))}:e)};}};
|
|
159
|
+
const plan = await planD1Import(source,mapping,planningContext(request,namespace));
|
|
160
|
+
if (plan.counts.duplicates || plan.entries.length!==sourceCount) throw new DoorError('duplicate_source_membership',422);
|
|
161
|
+
await refuseErased(request,upload,plan,segments);
|
|
162
|
+
const suppliedNative = request.options.bounded!.native(plan);
|
|
163
|
+
const native = Array.isArray(suppliedNative) ? suppliedNative.map(row=>({...row})) : suppliedNative;
|
|
164
|
+
if (!Array.isArray(native) || native.some(n=>!n || Object.keys(n).some(k=>!['table','key','valueHash','storedBytes','blobBytes'].includes(k)) || typeof n.table!=='string' || !n.table || typeof n.key!=='string' || !n.key || !digest(n.valueHash) || !count(n.storedBytes) || !count(n.blobBytes))
|
|
165
|
+
|| native.length<plan.counts.effects) throw new DoorError('invalid_native_accounting',422);
|
|
166
|
+
totals.source=plan.counts.unique; totals.native=native.length;
|
|
167
|
+
const nativeByTable:Record<string,number>=Object.create(null);
|
|
168
|
+
for (const row of native) { nativeByTable[row.table]=(nativeByTable[row.table] ?? 0)+1; totals.blobBytes+=row.blobBytes;totals.nativeBytes+=row.storedBytes; members.push(inventoryMember('native',[row.table,row.key],row)); }
|
|
169
|
+
totals.aliases=metadata.aliases.length;
|
|
170
|
+
for (const alias of metadata.aliases) members.push(inventoryMember('alias',[alias.kind,alias.sourceId],alias));
|
|
171
|
+
const stable=[...plan.entries.map(entry=>hash([entry.source.kind,entry.source.sourceId])),...metadata.aliases.map(a=>hash([a.kind,a.sourceId]))];
|
|
172
|
+
const withdrawn=await request.options.database.prepare("SELECT 1 FROM ik_door_wave_members m JOIN ik_door_wave_roots r ON r.id=m.parent_id WHERE r.installation_id=? AND r.namespace=? AND r.destination=? AND r.state='withdrawn' AND r.epoch<? AND m.type IN ('source','alias') AND m.identity IN (SELECT value FROM json_each(?)) LIMIT 1").bind(root.installation_id,root.namespace,root.destination,root.epoch,JSON.stringify(stable)).first();
|
|
173
|
+
const erasedAlias=await request.options.database.prepare('SELECT 1 FROM ik_door_wave_erased_aliases WHERE installation_id=? AND namespace=? AND destination=? AND identity IN (SELECT value FROM json_each(?)) LIMIT 1').bind(root.installation_id,root.namespace,root.destination,JSON.stringify(stable)).first();
|
|
174
|
+
if (withdrawn || erasedAlias) throw new DoorError('source_erased',410);
|
|
175
|
+
for (const entry of plan.entries) members.push(inventoryMember('source',[entry.source.kind,entry.source.sourceId],entry.source.payloadHash));
|
|
176
|
+
if (totals.native>limits.maxRecords) throw new DoorError('child_exceeds_native_quota',413,{native:totals.native,limit:limits.maxRecords});
|
|
177
|
+
for (const effect of plan.entries.flatMap(e=>e.effects)) if (effect.attachment && !source.artifacts.some(a=>a.sha256===effect.attachment!.sha256)) throw new DoorError('attachment_original_not_accounted',422);
|
|
178
|
+
const baseline=[];
|
|
179
|
+
for (const effect of plan.entries.flatMap(e=>e.effects)) baseline.push({kind:effect.targetKind,id:effect.targetId,row:await request.options.targets.read(request.options.database,root.destination,effect.targetKind,effect.targetId) ?? null});
|
|
180
|
+
if (totalKeys.some(k=>!count(totals[k]))) throw new DoorError('native_accounting_overflow',413);
|
|
181
|
+
const descriptor: WaveDescriptor = {...totals,ordinal:root.pages,unitHash:hash(metadata.id),dependsOn,membershipHash:hash(members),planFingerprint:plan.fingerprint,
|
|
182
|
+
relationshipsHash:metadata.relationshipsHash,nativeByTable,baseline,projections:native,fingerprint:''};
|
|
183
|
+
const {fingerprint:_fingerprint,...reviewed}=descriptor;
|
|
184
|
+
descriptor.fingerprint=hash(reviewed);
|
|
185
|
+
const snapshot: DoorSnapshot & {wave:WaveMetadata} = {id,namespace:root.namespace,bundleSha256:root.bundle_sha256,kitNamespace:namespace,
|
|
186
|
+
previous:root.previous_id,records:[],sourceCount:totals.source,plan,differences,standingApproval:null,wave:{unit:metadata,descriptor}};
|
|
187
|
+
snapshot.records=snapshotRecords(snapshot,old,metadata.relationshipsHash);
|
|
188
|
+
await saveChild(request,root,snapshot,members,hash([root.chain,descriptor.fingerprint]));
|
|
189
|
+
return {protocol:BOUNDED_WAVES,parentId:root.id,state:'preparing',next:root.pages+1,child:{ordinal:root.pages,fingerprint:descriptor.fingerprint,totals}};
|
|
190
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Apply in a NEW numbered edition migration after importDoorSchema. No
|
|
2
|
+
* existing table or ledger row is changed. Omitted feature mounts keep v1. */
|
|
3
|
+
export const importDoorBoundedWavesMigration = [
|
|
4
|
+
`CREATE TABLE IF NOT EXISTS ik_door_wave_roots (
|
|
5
|
+
id TEXT PRIMARY KEY, installation_id TEXT NOT NULL, namespace TEXT NOT NULL,
|
|
6
|
+
destination TEXT NOT NULL, owner TEXT NOT NULL, mapping_version TEXT NOT NULL,
|
|
7
|
+
bundle_sha256 TEXT NOT NULL, upload_id TEXT NOT NULL, previous_id TEXT,
|
|
8
|
+
epoch INTEGER NOT NULL, binding_json TEXT NOT NULL, census_json TEXT NOT NULL,
|
|
9
|
+
fingerprint TEXT, state TEXT NOT NULL, pages INTEGER NOT NULL DEFAULT 0,
|
|
10
|
+
chain TEXT NOT NULL, catchup INTEGER NOT NULL DEFAULT 0, content_revision INTEGER NOT NULL DEFAULT 0,
|
|
11
|
+
UNIQUE(installation_id,namespace,upload_id,binding_json))`,
|
|
12
|
+
"CREATE UNIQUE INDEX IF NOT EXISTS ik_door_wave_revision ON ik_door_wave_roots(installation_id,namespace,json_extract(binding_json,'$.migration'),json_extract(binding_json,'$.revision'))",
|
|
13
|
+
`CREATE TABLE IF NOT EXISTS ik_door_wave_children (
|
|
14
|
+
parent_id TEXT NOT NULL REFERENCES ik_door_wave_roots(id), ordinal INTEGER NOT NULL,
|
|
15
|
+
plan_id TEXT NOT NULL UNIQUE REFERENCES ik_door_plans(id), unit_hash TEXT NOT NULL,
|
|
16
|
+
fingerprint TEXT NOT NULL, descriptor_hash TEXT NOT NULL,
|
|
17
|
+
source INTEGER NOT NULL, native INTEGER NOT NULL, aliases INTEGER NOT NULL, objects INTEGER NOT NULL,
|
|
18
|
+
logicalBytes INTEGER NOT NULL, storedBytes INTEGER NOT NULL, nativeBytes INTEGER NOT NULL, blobBytes INTEGER NOT NULL,
|
|
19
|
+
contextBytes INTEGER NOT NULL, PRIMARY KEY(parent_id,ordinal), UNIQUE(parent_id,unit_hash))`,
|
|
20
|
+
`CREATE TABLE IF NOT EXISTS ik_door_wave_members (
|
|
21
|
+
parent_id TEXT NOT NULL REFERENCES ik_door_wave_roots(id), type TEXT NOT NULL,
|
|
22
|
+
identity TEXT NOT NULL, digest TEXT NOT NULL, ordinal INTEGER NOT NULL,
|
|
23
|
+
PRIMARY KEY(parent_id,type,identity))`,
|
|
24
|
+
'CREATE INDEX IF NOT EXISTS ik_door_wave_digest ON ik_door_wave_members(parent_id,digest)',
|
|
25
|
+
`CREATE TABLE IF NOT EXISTS ik_door_wave_approvals (
|
|
26
|
+
parent_id TEXT NOT NULL REFERENCES ik_door_wave_roots(id), ordinal INTEGER NOT NULL,
|
|
27
|
+
approval_id TEXT NOT NULL, approved_at TEXT NOT NULL, owner TEXT NOT NULL,
|
|
28
|
+
fingerprint TEXT NOT NULL, revoked INTEGER NOT NULL DEFAULT 0,
|
|
29
|
+
PRIMARY KEY(parent_id,ordinal))`,
|
|
30
|
+
`CREATE TABLE IF NOT EXISTS ik_door_wave_erased_aliases (installation_id TEXT NOT NULL, namespace TEXT NOT NULL, destination TEXT NOT NULL, identity TEXT NOT NULL, PRIMARY KEY(installation_id,namespace,destination,identity))`,
|
|
31
|
+
`CREATE TABLE IF NOT EXISTS ik_door_wave_origins (
|
|
32
|
+
job_id TEXT PRIMARY KEY REFERENCES ik_jobs(id) ON DELETE CASCADE,
|
|
33
|
+
parent_id TEXT NOT NULL, ordinal INTEGER NOT NULL, plan_id TEXT NOT NULL, fingerprint TEXT NOT NULL)`,
|
|
34
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_origin_update BEFORE UPDATE ON ik_door_wave_origins
|
|
35
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_ORIGIN_CHANGED'); END`,
|
|
36
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_origin_delete BEFORE DELETE ON ik_door_wave_origins
|
|
37
|
+
WHEN EXISTS(SELECT 1 FROM ik_jobs WHERE id=old.job_id)
|
|
38
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_ORIGIN_CHANGED'); END`,
|
|
39
|
+
...['INSERT','UPDATE'].map(event=>`CREATE TRIGGER IF NOT EXISTS ik_door_wave_epoch_${event.toLowerCase()} BEFORE ${event} ON ik_door_wave_roots
|
|
40
|
+
WHEN new.state NOT IN ('withdrawn','cancelled') AND new.epoch<(SELECT count(*) FROM ik_d1_erasure_journal WHERE destination=new.destination)
|
|
41
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_ERASURE_FENCE'); END`),
|
|
42
|
+
...['ik_door_wave_children','ik_door_wave_members'].flatMap(table=>['UPDATE','DELETE'].map(event=>`CREATE TRIGGER IF NOT EXISTS ${table}_${event.toLowerCase()} BEFORE ${event} ON ${table}
|
|
43
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_MEMBERSHIP_IMMUTABLE'); END`)),
|
|
44
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_root_binding BEFORE UPDATE ON ik_door_wave_roots
|
|
45
|
+
WHEN ${['id','installation_id','namespace','destination','owner','mapping_version','bundle_sha256','upload_id','previous_id','epoch','binding_json','census_json','catchup'].map(k=>`new.${k} IS NOT old.${k}`).join(' OR ')}
|
|
46
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_BINDING_IMMUTABLE'); END`,
|
|
47
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_root_layout BEFORE UPDATE ON ik_door_wave_roots
|
|
48
|
+
WHEN old.state<>'preparing' AND (new.pages<>old.pages OR new.chain<>old.chain OR new.fingerprint IS NOT old.fingerprint)
|
|
49
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_MEMBERSHIP_IMMUTABLE'); END`,
|
|
50
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_approval_update BEFORE UPDATE ON ik_door_wave_approvals
|
|
51
|
+
WHEN ${['parent_id','ordinal','approval_id','approved_at','owner','fingerprint'].map(k=>`new.${k} IS NOT old.${k}`).join(' OR ')} OR new.revoked<old.revoked
|
|
52
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_APPROVAL_IMMUTABLE'); END`,
|
|
53
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_approval_delete BEFORE DELETE ON ik_door_wave_approvals
|
|
54
|
+
BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_APPROVAL_IMMUTABLE'); END`,
|
|
55
|
+
...['ik_door_wave_children','ik_door_wave_members'].map(table=>`CREATE TRIGGER IF NOT EXISTS ${table}_revision AFTER INSERT ON ${table}
|
|
56
|
+
BEGIN UPDATE ik_door_wave_roots SET content_revision=content_revision+1 WHERE id=new.parent_id; END`),
|
|
57
|
+
...['INSERT','UPDATE','DELETE'].map(event=>`CREATE TRIGGER IF NOT EXISTS ik_door_wave_content_${event.toLowerCase()} AFTER ${event} ON ik_door_plan_chunks
|
|
58
|
+
BEGIN UPDATE ik_door_wave_roots SET content_revision=content_revision+1${event==='DELETE'?",state='withdrawn'":''}
|
|
59
|
+
WHERE id IN (SELECT parent_id FROM ik_door_wave_children WHERE plan_id=${event==='DELETE'?'old':'new'}.plan_id); END`),
|
|
60
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_content_monotonic BEFORE UPDATE ON ik_door_wave_roots
|
|
61
|
+
WHEN new.content_revision<old.content_revision BEGIN SELECT RAISE(ABORT,'IMPORT_WAVE_CONTENT_CHANGED'); END`,
|
|
62
|
+
// The existing erasure transaction withdraws opaque snapshots and package
|
|
63
|
+
// copies in its destination. This fence also withdraws an unfinished root:
|
|
64
|
+
// it can never regain authority by re-uploading the same or corrected bytes.
|
|
65
|
+
`CREATE TRIGGER IF NOT EXISTS ik_door_wave_withdraw AFTER DELETE ON ik_door_plan_chunks
|
|
66
|
+
BEGIN UPDATE ik_door_wave_roots SET state='withdrawn' WHERE id IN
|
|
67
|
+
(SELECT parent_id FROM ik_door_wave_children WHERE plan_id=old.plan_id)
|
|
68
|
+
AND state NOT IN ('reversed','withdrawn'); END`,
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
/** A partial migration never enables execution without its immutable bindings. */
|
|
72
|
+
const objects=importDoorBoundedWavesMigration.map(query=>/^CREATE (?:UNIQUE )?(TABLE|TRIGGER|INDEX) IF NOT EXISTS (\w+)/.exec(query.trim())).filter((match):match is RegExpExecArray=>!!match);
|
|
73
|
+
export const importDoorBoundedWavesCheck=`SELECT count(*)=${objects.length} AS current FROM sqlite_master WHERE `+objects.map(match=>`(type='${match[1].toLowerCase()}' AND name='${match[2]}')`).join(' OR ');
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {canonicalHash} from '../../provenance/canonical-stream.ts';
|
|
2
|
+
import type {WaveInventoryMember} from './digests.ts';
|
|
3
|
+
import { canonicalJson } from '../../provenance/identity.ts';
|
|
4
|
+
import type { DoorRequest, DoorSnapshot } from '../types.ts';
|
|
5
|
+
import { DoorError } from '../protocol.ts';
|
|
6
|
+
import { assertion, erasures, loadSnapshot, sql, statement, transact } from '../store.ts';
|
|
7
|
+
import { BOUNDED_WAVES, totalKeys, type WaveChild, type WaveMetadata, type WaveRoot, type WaveBinding } from './types.ts';
|
|
8
|
+
import { text } from '../uploads.ts';
|
|
9
|
+
|
|
10
|
+
export const hash = canonicalHash;
|
|
11
|
+
export const opaque = (value: unknown): value is string => typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
|
12
|
+
export function revision(request: DoorRequest): WaveBinding {
|
|
13
|
+
const binding = request.access.bounded;
|
|
14
|
+
if (request.via !== 'grant' || !binding || binding.protocol !== BOUNDED_WAVES
|
|
15
|
+
|| !request.options.bounded) throw new DoorError('bounded_waves_not_adopted',409);
|
|
16
|
+
return binding;
|
|
17
|
+
}
|
|
18
|
+
export async function rootRow(request: DoorRequest, input: Record<string,unknown>, allowWithdrawn = false): Promise<WaveRoot> {
|
|
19
|
+
revision(request);
|
|
20
|
+
const row = await request.options.database.prepare('SELECT * FROM ik_door_wave_roots WHERE id=? AND installation_id=? AND namespace=? AND destination=? AND owner=?')
|
|
21
|
+
.bind(text(input.parentId,'parent_id'),request.access.installationId,request.access.namespace,request.options.destination,request.access.email).first<WaveRoot>();
|
|
22
|
+
if (!row) throw new DoorError('parent_not_found',404);
|
|
23
|
+
const b = revision(request), saved = JSON.parse(row.binding_json) as WaveBinding;
|
|
24
|
+
if (['migration','revision','policy'].some(k=>b[k as keyof WaveBinding] !== saved[k as keyof WaveBinding])
|
|
25
|
+
|| row.mapping_version !== request.options.mapping.version) throw new DoorError('parent_revision_changed',409);
|
|
26
|
+
if (row.state === 'withdrawn' || row.epoch !== await erasures(request.options.database,row.destination)) {
|
|
27
|
+
if (!allowWithdrawn) throw new DoorError('parent_withdrawn',410);
|
|
28
|
+
}
|
|
29
|
+
if (row.fingerprint && parentFingerprint(row)!==row.fingerprint) throw new DoorError('parent_layout_changed',409);
|
|
30
|
+
if (b.parentFingerprint !== undefined && b.parentFingerprint !== row.fingerprint) throw new DoorError('parent_fingerprint_mismatch');
|
|
31
|
+
return row;
|
|
32
|
+
}
|
|
33
|
+
export function rootFence(row: WaveRoot) {
|
|
34
|
+
const settled=row.state==='completed' || row.state==='reversed';
|
|
35
|
+
return sql(`EXISTS(SELECT 1 FROM ik_door_wave_roots WHERE id=? AND state=? AND pages=? AND chain=? AND fingerprint IS ? AND epoch=? AND content_revision=?)
|
|
36
|
+
AND (SELECT count(*) FROM ik_d1_erasure_journal WHERE destination=?)=?
|
|
37
|
+
AND EXISTS(SELECT 1 FROM ik_door_namespaces WHERE installation_id=? AND namespace=? AND pending_id IS ?
|
|
38
|
+
AND (head_id IS ? OR (?=1 AND head_id=?)))`,row.id,row.state,row.pages,row.chain,row.fingerprint,row.epoch,row.content_revision ?? 0,row.destination,row.epoch,
|
|
39
|
+
row.installation_id,row.namespace,settled?null:row.id,row.state==='completed'?row.id:row.previous_id,row.state==='reversing'?1:0,row.id);
|
|
40
|
+
}
|
|
41
|
+
export function activeParent(request: DoorRequest, root: WaveRoot) {
|
|
42
|
+
const b = revision(request);
|
|
43
|
+
if (b.parentFingerprint !== root.fingerprint || !b.parentApprovalRecordId || !b.parentApprovedAt) throw new DoorError('parent_approval_required');
|
|
44
|
+
return sql(`EXISTS(SELECT 1 FROM ik_door_wave_approvals WHERE parent_id=? AND ordinal=-1 AND revoked=0 AND fingerprint=? AND approval_id=? AND approved_at=? AND owner=?)`,
|
|
45
|
+
root.id,root.fingerprint,b.parentApprovalRecordId,b.parentApprovedAt,request.access.email);
|
|
46
|
+
}
|
|
47
|
+
export function authority(request: DoorRequest, extra = sql('1')) {
|
|
48
|
+
const b = revision(request), upstream = request.options.bounded!.authority(b,request.access.action);
|
|
49
|
+
if (!upstream?.sql?.trim()) throw new DoorError('bounded_authority_unavailable',503);
|
|
50
|
+
return sql(`(${request.authority.sql}) AND (${upstream.sql}) AND (${extra.sql})`,...request.authority.params ?? [],...upstream.params ?? [],...extra.params ?? []);
|
|
51
|
+
}
|
|
52
|
+
export async function children(request: DoorRequest, root: WaveRoot): Promise<WaveChild[]> {
|
|
53
|
+
const {results} = await request.options.database.prepare('SELECT c.*,p.job_id FROM ik_door_wave_children c JOIN ik_door_plans p ON p.id=c.plan_id WHERE c.parent_id=? ORDER BY c.ordinal').bind(root.id).all<WaveChild>();
|
|
54
|
+
if (results.length !== root.pages || results.some((c,i)=>c.ordinal !== i)) throw new DoorError('parent_layout_changed',409);
|
|
55
|
+
let chain=hash([]);for(const child of results)chain=hash([chain,child.fingerprint]);
|
|
56
|
+
if (chain!==root.chain) throw new DoorError('parent_layout_changed',409);
|
|
57
|
+
return results;
|
|
58
|
+
}
|
|
59
|
+
export async function childSnapshot(request: DoorRequest, child: WaveChild) {
|
|
60
|
+
const row = await request.options.database.prepare('SELECT * FROM ik_door_plans WHERE id=? AND installation_id=? AND namespace=? AND complete=1')
|
|
61
|
+
.bind(child.plan_id,request.access.installationId,request.access.namespace).first<any>();
|
|
62
|
+
if (!row) throw new DoorError('child_custody_missing',410);
|
|
63
|
+
const snapshot = await loadSnapshot(request,row) as DoorSnapshot & {wave: WaveMetadata};
|
|
64
|
+
if (!snapshot.wave || hash(snapshot.wave.descriptor) !== child.descriptor_hash || snapshot.wave.descriptor.fingerprint !== child.fingerprint) throw new DoorError('child_membership_changed',409);
|
|
65
|
+
const {fingerprint,...descriptor}=snapshot.wave.descriptor;
|
|
66
|
+
if (totalKeys.some(k=>descriptor[k]!==child[k]) || descriptor.ordinal!==child.ordinal || descriptor.unitHash!==child.unit_hash || descriptor.source!==snapshot.plan.counts.unique || hash(descriptor)!==fingerprint || descriptor.planFingerprint!==snapshot.plan.fingerprint || hash(snapshot.wave.unit.id)!==descriptor.unitHash) throw new DoorError('child_membership_changed',409);
|
|
67
|
+
return snapshot;
|
|
68
|
+
}
|
|
69
|
+
/** Bounded snapshot chunks reuse the door's existing erasure and restore
|
|
70
|
+
* inventory. They are not another apply journal or byte store. */
|
|
71
|
+
export async function saveChild(request: DoorRequest, root: WaveRoot, snapshot: DoorSnapshot & {wave: WaveMetadata}, members: WaveInventoryMember[], nextChain: string) {
|
|
72
|
+
const db = request.options.database, d = snapshot.wave.descriptor, body = canonicalJson(snapshot);
|
|
73
|
+
const rows: {ordinal:number;body:string}[] = [];
|
|
74
|
+
for (let start = 0; start < body.length;) {
|
|
75
|
+
let end = Math.min(start+24_000,body.length);
|
|
76
|
+
if (end<body.length && /[\uD800-\uDBFF]/.test(body[end-1])) end--;
|
|
77
|
+
rows.push({ordinal:rows.length,body:body.slice(start,end)}); start=end;
|
|
78
|
+
}
|
|
79
|
+
// A work unit is bounded by the existing plan/value/request limits, including
|
|
80
|
+
// its originals. An indivisible unit that cannot fit is refused, never split.
|
|
81
|
+
await transact(db,authority(request),[
|
|
82
|
+
assertion(rootFence(root)),
|
|
83
|
+
sql('INSERT INTO ik_door_plans(id,installation_id,namespace,bundle_sha256,fingerprint,previous_id,snapshot_hash,complete) VALUES(?,?,?,?,?,?,?,1)',snapshot.id,root.installation_id,root.namespace,root.bundle_sha256,snapshot.plan.fingerprint,root.previous_id,hash(snapshot)),
|
|
84
|
+
...rows.map(row=>sql('INSERT INTO ik_door_plan_chunks(plan_id,ordinal,body) VALUES(?,?,?)',snapshot.id,row.ordinal,row.body)),
|
|
85
|
+
sql(`INSERT INTO ik_door_wave_children(parent_id,ordinal,plan_id,unit_hash,fingerprint,descriptor_hash,source,native,aliases,objects,logicalBytes,storedBytes,nativeBytes,blobBytes,contextBytes) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,root.id,d.ordinal,snapshot.id,d.unitHash,d.fingerprint,hash(d),d.source,d.native,d.aliases,d.objects,d.logicalBytes,d.storedBytes,d.nativeBytes,d.blobBytes,d.contextBytes),
|
|
86
|
+
sql(`INSERT INTO ik_door_wave_members(parent_id,type,identity,digest,ordinal) SELECT ?,json_extract(value,'$.type'),json_extract(value,'$.identity'),json_extract(value,'$.digest'),? FROM json_each(?)`,root.id,d.ordinal,JSON.stringify(members)),
|
|
87
|
+
sql('UPDATE ik_door_wave_roots SET pages=pages+1,chain=? WHERE id=?',nextChain,root.id),
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
/** Provisioning revocation is server-only, checked in every execution batch. */
|
|
91
|
+
export async function revokeImportDoorWaveApproval(request: DoorRequest, parentId: string, ordinal: number) {
|
|
92
|
+
const root = await rootRow(request,{parentId});
|
|
93
|
+
await transact(request.options.database,authority(request),[
|
|
94
|
+
sql('UPDATE ik_door_wave_approvals SET revoked=1 WHERE parent_id=? AND ordinal=?',root.id,ordinal),
|
|
95
|
+
]);
|
|
96
|
+
}
|
|
97
|
+
export async function assertPredicate(request: DoorRequest, predicate: ReturnType<typeof sql>, code: string) {
|
|
98
|
+
if ((await statement(request.options.database,sql(`SELECT (${predicate.sql}) AS ok`,...predicate.params ?? [])).first())?.ok !== 1) throw new DoorError(code);
|
|
99
|
+
}
|
|
100
|
+
export async function approve(request: DoorRequest, root: WaveRoot, ordinal: number, fingerprint: string) {
|
|
101
|
+
const g=request.access;
|
|
102
|
+
if (g.fingerprint !== fingerprint || !g.approvalRecordId || !g.approvedAt) throw new DoorError('exact_approval_required');
|
|
103
|
+
await transact(request.options.database,authority(request,rootFence(root)),[
|
|
104
|
+
sql('INSERT OR IGNORE INTO ik_door_wave_approvals(parent_id,ordinal,approval_id,approved_at,owner,fingerprint) VALUES(?,?,?,?,?,?)',root.id,ordinal,g.approvalRecordId,g.approvedAt,g.email,fingerprint),
|
|
105
|
+
assertion(sql('EXISTS(SELECT 1 FROM ik_door_wave_approvals WHERE parent_id=? AND ordinal=? AND approval_id=? AND approved_at=? AND owner=? AND fingerprint=? AND revoked=0)',root.id,ordinal,g.approvalRecordId,g.approvedAt,g.email,fingerprint)),
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function parentFingerprint(root: WaveRoot) {
|
|
110
|
+
return hash({protocol:BOUNDED_WAVES,binding:JSON.parse(root.binding_json),installation:root.installation_id,destination:root.destination,owner:root.owner,
|
|
111
|
+
namespace:root.namespace,mapping:root.mapping_version,package:root.bundle_sha256,previous:root.previous_id,census:JSON.parse(root.census_json),children:root.chain,epoch:root.epoch,catchup:root.catchup});
|
|
112
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { D1Sql } from '../../d1/types.ts';
|
|
2
|
+
import type { ImportPlan, ParsedRecord, ReaderLimits, TargetRow } from '../../types.ts';
|
|
3
|
+
import type { ImportDoorPackageUpload } from '../types.ts';
|
|
4
|
+
|
|
5
|
+
export const BOUNDED_WAVES = 'rer-bounded-waves/1' as const;
|
|
6
|
+
/** Opaque Business identifiers. These never select the destination. */
|
|
7
|
+
export interface WaveRevision { migration: string; revision: string; policy: string }
|
|
8
|
+
export interface WaveTotals {
|
|
9
|
+
source: number; native: number; aliases: number; objects: number; logicalBytes: number;
|
|
10
|
+
storedBytes: number; nativeBytes: number; blobBytes: number; contextBytes: number;
|
|
11
|
+
}
|
|
12
|
+
/** Adapter's independently enumerated COMPLETE inventory, not its first page. */
|
|
13
|
+
export interface WaveCensus extends WaveTotals {
|
|
14
|
+
children: number; byKind: Record<string, number>; nativeByTable: Record<string, number>;
|
|
15
|
+
inventoryHash: string; relationshipsHash: string; baselineHash: string;
|
|
16
|
+
sourceMode: 'complete'; highWater: string; accountingVersion: string;
|
|
17
|
+
}
|
|
18
|
+
export interface WaveFile {
|
|
19
|
+
path: string; role: 'source' | 'object' | 'context'; logicalBytes: number;
|
|
20
|
+
}
|
|
21
|
+
export interface WaveNativeRow {
|
|
22
|
+
table: string; key: string; valueHash: string; storedBytes: number; blobBytes: number;
|
|
23
|
+
}
|
|
24
|
+
/** The adapter coalesces cycles/publication closures BEFORE returning this unit.
|
|
25
|
+
* All primary rows, aliases and media must occur in exactly one unit. */
|
|
26
|
+
export interface WaveAlias {
|
|
27
|
+
kind: string; sourceId: string; role: 'context'|'archive'|'rejected'|'metadata'; files: string[];
|
|
28
|
+
}
|
|
29
|
+
export interface WaveUnit {
|
|
30
|
+
id: string; dependsOn: string[]; records: ParsedRecord[]; files: WaveFile[]; aliases: WaveAlias[];
|
|
31
|
+
/** Changes to relationship evidence must change this digest. */
|
|
32
|
+
relationshipsHash: string;
|
|
33
|
+
}
|
|
34
|
+
export interface WaveBinding extends WaveRevision {
|
|
35
|
+
protocol: typeof BOUNDED_WAVES;
|
|
36
|
+
parentFingerprint?: string; childFingerprint?: string;
|
|
37
|
+
parentApprovalRecordId?: string; parentApprovedAt?: string;
|
|
38
|
+
}
|
|
39
|
+
/** Edition-owned, server-only integration. No source arrays are read by the
|
|
40
|
+
* ordinary package reader; one bounded native work unit is prepared per call. */
|
|
41
|
+
export interface ImportDoorBoundedSource {
|
|
42
|
+
protocol: typeof BOUNDED_WAVES;
|
|
43
|
+
batchSize?: number;
|
|
44
|
+
describe(upload: ImportDoorPackageUpload): Promise<WaveCensus>;
|
|
45
|
+
page(upload: ImportDoorPackageUpload, ordinal: number, limits: Readonly<Required<ReaderLimits>>): Promise<WaveUnit>;
|
|
46
|
+
/** Full expansion into the actual installed schema, including joins, origin,
|
|
47
|
+
* hold and native blob chunks. Kit journal rows are reported separately. */
|
|
48
|
+
native(plan: ImportPlan): WaveNativeRow[];
|
|
49
|
+
/** Current installation/owner/claim, migration revision, policy and active
|
|
50
|
+
* exact parent/child approvals in the same database. Never return a cached
|
|
51
|
+
* boolean. Business and the edition must install these typed approval rows. */
|
|
52
|
+
authority(binding: Readonly<WaveBinding>, action: string): D1Sql;
|
|
53
|
+
}
|
|
54
|
+
export interface WaveDescriptor extends WaveTotals {
|
|
55
|
+
ordinal: number; unitHash: string; dependsOn: string[]; membershipHash: string;
|
|
56
|
+
planFingerprint: string; fingerprint: string; relationshipsHash: string;
|
|
57
|
+
nativeByTable: Record<string, number>;
|
|
58
|
+
baseline: {kind: string; id: string; row: TargetRow | null}[];
|
|
59
|
+
projections: WaveNativeRow[];
|
|
60
|
+
}
|
|
61
|
+
export interface WaveRoot {
|
|
62
|
+
id: string; installation_id: string; namespace: string; destination: string;
|
|
63
|
+
owner: string; mapping_version: string; bundle_sha256: string; upload_id: string;
|
|
64
|
+
previous_id: string | null; epoch: number; binding_json: string; census_json: string;
|
|
65
|
+
fingerprint: string | null; state: string; pages: number; chain: string;
|
|
66
|
+
catchup: number; content_revision?: number;
|
|
67
|
+
}
|
|
68
|
+
export interface WaveChild {
|
|
69
|
+
parent_id: string; ordinal: number; plan_id: string; unit_hash: string;
|
|
70
|
+
fingerprint: string; descriptor_hash: string; source: number; native: number; aliases: number;
|
|
71
|
+
objects: number; logicalBytes: number; storedBytes: number; nativeBytes: number; blobBytes: number;
|
|
72
|
+
contextBytes: number; job_id: string | null;
|
|
73
|
+
}
|
|
74
|
+
export type WaveMetadata = { descriptor: WaveDescriptor; unit: Omit<WaveUnit,'records'> };
|
|
75
|
+
export const totalKeys = ['source','native','aliases','objects','logicalBytes','storedBytes','nativeBytes','blobBytes','contextBytes'] as const;
|
|
76
|
+
export const zeroTotals = (): WaveTotals => ({source:0,native:0,aliases:0,objects:0,logicalBytes:0,storedBytes:0,nativeBytes:0,blobBytes:0,contextBytes:0});
|
package/onboarding/adapter.mjs
CHANGED
|
@@ -233,6 +233,7 @@ async function start(context, hooks) {
|
|
|
233
233
|
try{return await listenerPorts(child.pid);}
|
|
234
234
|
catch(error){throw installationFailure('command_failed',{step:'start',diagnostic:error});}
|
|
235
235
|
};
|
|
236
|
+
const readinessTimeoutMs=timeoutMs??60000,readinessStarted=performance.now();
|
|
236
237
|
try {
|
|
237
238
|
await store.waitForReady(readinessLabel, child, async probeSignal => {
|
|
238
239
|
if(runtimePortHandshake){
|
|
@@ -267,10 +268,15 @@ async function start(context, hooks) {
|
|
|
267
268
|
}
|
|
268
269
|
}
|
|
269
270
|
return result;
|
|
270
|
-
}, { signal, timeoutMs:
|
|
271
|
+
}, { signal, timeoutMs: readinessTimeoutMs, ...(softAfterMs ? { softAfterMs } : {}), ...(intervalMs ? { intervalMs } : {}) });
|
|
271
272
|
} catch (error) {
|
|
272
273
|
if (signal?.aborted) throw error;
|
|
273
274
|
if(detectedMismatch)throw installationFailure('command_failed',{step:'start',diagnostic:error});
|
|
275
|
+
if(runtimePortHandshake&&!announcement){
|
|
276
|
+
const prior=approvedFailure(error);
|
|
277
|
+
const message=`No managed listener was announced after ${Math.round(performance.now()-readinessStarted)} ms (allowed ${readinessTimeoutMs} ms). Check the runtime startup output in the log below, correct the startup error, then run rer ${context.verb??'setup'} again.`;
|
|
278
|
+
throw installationFailure('command_failed',{step:'start',diagnostic:commandFailure(`${message}\n${prior?.message??readinessLabel}`,prior??{})});
|
|
279
|
+
}
|
|
274
280
|
const ports=await ownedPorts();
|
|
275
281
|
if(ports.length){
|
|
276
282
|
let diagnosis;
|
|
@@ -30,6 +30,8 @@ export function operatorInstaller({ database, features, prepare, prepareData })
|
|
|
30
30
|
return {...lifecycle,localPrerequisites:async({root,vars,demo})=>vars.AUTH_LOCAL==='true'&&vars.MAIL_PROVIDER==='development'&&Boolean(vars.SESSION_SECRET)&&(demo||await operatorPrerequisites(root)),install:(root,input,store,onApp,signal)=>installOperator(root,input,store,onApp,signal,lifecycle.hooks)};
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
// Setup and dev give the real Vite listener one finite fifteen-minute startup
|
|
34
|
+
// budget, including its announcement and health check; no second readiness wait.
|
|
33
35
|
// Only local runtimes receive the dev key; vite.config.ts passes it to `vite dev` alone.
|
|
34
36
|
// No runtime rer starts serves the Local Explorer: it reads and writes the store
|
|
35
37
|
// without the key. trafficProxy also refuses /cdn-cgi/* on the public listener.
|
|
@@ -38,6 +40,6 @@ const developmentEnvironment = () => { const { RER_MODELS_SERVICE, RER_MODELS_KE
|
|
|
38
40
|
// A demo's Vite persists to its own folder (the seed's vite.config reads RER_STATE_DIR)
|
|
39
41
|
// and its Worker sees RER_DEMO=1 and the demo's end (modules/demo).
|
|
40
42
|
const demoBindings = demo => demo ? { RER_DEMO: '1', RER_DEMO_EXPIRES: demo.ends, RER_STATE_DIR: demo.state, RER_DEMO_RUN_URL: demo.runUrl } : {};
|
|
41
|
-
export const developmentTarget = ({ root, origin, devKey, modelsLive, demo }) => [process.execPath, [fileURLToPath(new URL('./development-runtime.mjs',import.meta.url)), 'dev', '--host', '127.0.0.1', '--port', '0', '--strictPort'], { runtimePortHandshake: true, stdio: ['ignore','pipe','pipe','ipc'], env: { ...developmentEnvironment(), PATH: [process.env.PATH, join(root, 'node_modules', '.bin')].filter(value => value !== undefined).join(delimiter), RER_DEV_ORIGIN: origin, WRANGLER_SEND_METRICS: 'false', X_LOCAL_EXPLORER: 'false', ...devBindings(devKey), ...(modelsLive ? { RER_MODELS_SERVICE: modelsLive.baseUrl, RER_MODELS_KEY: modelsLive.apiKey } : {}), ...demoBindings(demo) } }];
|
|
43
|
+
export const developmentTarget = ({ root, origin, devKey, modelsLive, demo }) => [process.execPath, [fileURLToPath(new URL('./development-runtime.mjs',import.meta.url)), 'dev', '--host', '127.0.0.1', '--port', '0', '--strictPort'], { timeoutMs: 900000, runtimePortHandshake: true, stdio: ['ignore','pipe','pipe','ipc'], env: { ...developmentEnvironment(), PATH: [process.env.PATH, join(root, 'node_modules', '.bin')].filter(value => value !== undefined).join(delimiter), RER_DEV_ORIGIN: origin, WRANGLER_SEND_METRICS: 'false', X_LOCAL_EXPLORER: 'false', ...devBindings(devKey), ...(modelsLive ? { RER_MODELS_SERVICE: modelsLive.baseUrl, RER_MODELS_KEY: modelsLive.apiKey } : {}), ...demoBindings(demo) } }];
|
|
42
44
|
|
|
43
45
|
export const previewTarget = ({ origin, devKey, scheduled }) => [process.execPath, [fileURLToPath(new URL('./production-runtime.mjs', import.meta.url))], { runtimePortHandshake: true, stdio: ['ignore','pipe','pipe','ipc'], env: { ...process.env, RER_DEV_ORIGIN: origin, RER_RUNTIME_HOST: '127.0.0.1', RER_RUNTIME_PORT: '0', WRANGLER_SEND_METRICS: 'false', CLOUDFLARE_INCLUDE_PROCESS_ENV: 'false', X_LOCAL_EXPLORER: 'false', RER_RUNTIME_TRIGGERS: scheduled ? '1' : '0', ...(devKey ? { RER_DEV_KEY: devKey } : {}) } }];
|
package/package.json
CHANGED
|
@@ -54,6 +54,26 @@ its worker inherits the lock so it cannot outlive its slot. Queueing has its
|
|
|
54
54
|
own one-hour cap. `timeout.json` supplies the shared default and upper bound
|
|
55
55
|
to Python and the Node/release validator.
|
|
56
56
|
|
|
57
|
+
Process failures keep the v3 INCOMPLETE/exit-2 refusal. Its optional
|
|
58
|
+
`diagnostics` array identifies worker or slot launch errors, exits and signals,
|
|
59
|
+
report read/empty/parse/shape failures, version-metadata failures and scratch
|
|
60
|
+
cleanup failures. Cleanup cannot replace the original process/report failure. Completed worker reports
|
|
61
|
+
keep their existing status, exit and coverage fields when cleanup succeeds.
|
|
62
|
+
Cleanup failure preserves an existing refusal and its diagnostics; if the
|
|
63
|
+
worker succeeded, cleanup failure returns INCOMPLETE/exit 2 instead of PASS.
|
|
64
|
+
Version stamping also preserves earlier failure facts and refuses on failure.
|
|
65
|
+
|
|
66
|
+
Only fixed stage/code strings, OS errno names, signal names and numeric exit
|
|
67
|
+
codes cross this diagnostic boundary. No exception message, traceback, command
|
|
68
|
+
argument, path, report fragment or child stderr is retained or forwarded.
|
|
69
|
+
Both holder and worker stderr are discarded; queue notices are fixed text
|
|
70
|
+
rendered by Node from the holder's private fd-3 protocol. The worker does not
|
|
71
|
+
inherit that descriptor. Protocol lines retain at most 128 characters and
|
|
72
|
+
public reports contain at most eight diagnostic facts. Scratch remains private,
|
|
73
|
+
and cleanup removes only the directory this invocation created. If removal
|
|
74
|
+
fails, the report records its errno without exposing the scratch path; there
|
|
75
|
+
is no retained-stderr file or diagnostic log to collect.
|
|
76
|
+
|
|
57
77
|
Git message records carry their kind explicitly. Only commit records enable the
|
|
58
78
|
bounded public coauthor address rule described in the command guide; a file named
|
|
59
79
|
`@commit-message` has no special status. `public_hygiene.py` matches complete raw
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Failure facts are an allowlisted protocol, never child stderr or exception text.
|
|
2
|
+
import { constants } from 'node:os';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
const errnos = new Map(Object.entries(constants.errno).map(([name, number]) => [number, name]));
|
|
6
|
+
const signals = new Set(Object.keys(constants.signals));
|
|
7
|
+
export const diagnosticLimit = 8;
|
|
8
|
+
export const controlLineLimit = 128;
|
|
9
|
+
|
|
10
|
+
export function osFailure(stage, code, error) {
|
|
11
|
+
return { stage, code, errno: errnos.get(Math.abs(error?.errno)) ?? 'UNKNOWN' };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function processFact(stage, { error, exitCode, signal }) {
|
|
15
|
+
if (error) return osFailure(stage, 'process.launch-error', error);
|
|
16
|
+
if (signal) return { stage, code: 'process.signal', signal: signals.has(signal) ? signal : 'UNKNOWN' };
|
|
17
|
+
return { stage, code: 'process.exit', exitCode: Number.isInteger(exitCode) ? exitCode : null };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// fd 3 is private to the slot holder; it is not inherited by the scan worker.
|
|
21
|
+
// Still bound framing and accept only fixed tokens and typed numeric fields.
|
|
22
|
+
export function controlReader(onMessage) {
|
|
23
|
+
let pending = '', overflow = false;
|
|
24
|
+
return data => {
|
|
25
|
+
for (const character of data) {
|
|
26
|
+
if (character === '\n') {
|
|
27
|
+
if (!overflow) onMessage(pending);
|
|
28
|
+
pending = ''; overflow = false;
|
|
29
|
+
} else if (!overflow) {
|
|
30
|
+
if (pending.length < controlLineLimit) pending += character;
|
|
31
|
+
else { pending = ''; overflow = true; }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function workerFact(message) {
|
|
38
|
+
const match = /^(worker\.(exit|signal|launch)|slot\.(directory|open|lock|wait)):(\d{1,3})$/.exec(message);
|
|
39
|
+
if (!match) return;
|
|
40
|
+
const number = Number(match[4]);
|
|
41
|
+
if (match[2] === 'exit' && number <= 255) return { stage: 'worker', code: 'process.exit', exitCode: number };
|
|
42
|
+
if (match[2] === 'signal' && number > 0 && number < 128) {
|
|
43
|
+
const signal = Object.entries(constants.signals).find(([, value]) => value === number)?.[0] ?? 'UNKNOWN';
|
|
44
|
+
return { stage: 'worker', code: 'process.signal', signal };
|
|
45
|
+
}
|
|
46
|
+
if (match[2] === 'launch') return { stage: 'worker', code: 'process.launch-error', errno: errnos.get(number) ?? 'UNKNOWN' };
|
|
47
|
+
if (match[3]) return { stage: 'slot-' + match[3], code: 'os.error', errno: errnos.get(number) ?? 'UNKNOWN' };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function readReport(path) {
|
|
51
|
+
let text, report;
|
|
52
|
+
try { text = readFileSync(path, 'utf8'); }
|
|
53
|
+
catch (error) { return { diagnostic: osFailure('report', 'report.read-error', error) }; }
|
|
54
|
+
if (!text.trim()) return { diagnostic: { stage: 'report', code: 'report.empty' } };
|
|
55
|
+
try { report = JSON.parse(text); }
|
|
56
|
+
catch { return { diagnostic: { stage: 'report', code: 'report.parse-error' } }; }
|
|
57
|
+
if (!report || typeof report !== 'object' || Array.isArray(report) ||
|
|
58
|
+
!(typeof report.help === 'string' || (Array.isArray(report.findings) &&
|
|
59
|
+
report.findings.every(finding => finding && typeof finding.rule === 'string')))) {
|
|
60
|
+
return { diagnostic: { stage: 'report', code: 'report.shape-error' } };
|
|
61
|
+
}
|
|
62
|
+
return { report };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function incompleteReport(reason) {
|
|
66
|
+
return {
|
|
67
|
+
schema: 'rer.publish-check/v3', status: 'INCOMPLETE', complete: false, source: null,
|
|
68
|
+
findings: [{ rule: reason, path: '.', commit: null, scope: 'newly-public' }],
|
|
69
|
+
preExisting: [], warnings: [], fixtures: [], reviewed: [], info: [], thirdPartyNotices: [],
|
|
70
|
+
history: { commits: 0, trees: 0, blobs: 0 },
|
|
71
|
+
guarantees: { historyScanned: false, visualRecognition: false, environmentChecked: false },
|
|
72
|
+
};
|
|
73
|
+
}
|