@runeditrun/rer 0.2.99 → 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.
Files changed (72) hide show
  1. package/cli/README.md +88 -2
  2. package/cli/business/bounded-waves.d.mts +23 -0
  3. package/cli/business/bounded-waves.mjs +57 -0
  4. package/cli/business/catchup-task.mjs +9 -2
  5. package/cli/business/command.mjs +8 -3
  6. package/cli/business/export-inputs.mjs +12 -2
  7. package/cli/business/export-task.mjs +9 -2
  8. package/cli/business/legacy-package.mjs +117 -0
  9. package/cli/business/local-commands.mjs +10 -1
  10. package/cli/business/package-transfer.mjs +127 -0
  11. package/cli/business/state.mjs +3 -3
  12. package/cli/business/vendor-door.mjs +83 -11
  13. package/cli/business/vendors/README.md +41 -2
  14. package/cli/business/vendors/common.mjs +81 -0
  15. package/cli/business/vendors/index.mjs +12 -2
  16. package/cli/business/vendors/zendesk-attachments.mjs +57 -0
  17. package/cli/business/vendors/zendesk-encode.mjs +133 -0
  18. package/cli/business/vendors/zendesk-guide.mjs +34 -0
  19. package/cli/business/vendors/zendesk-package.mjs +28 -0
  20. package/cli/business/vendors/zendesk-pages.mjs +61 -0
  21. package/cli/business/vendors/zendesk-people.mjs +7 -0
  22. package/cli/business/vendors/zendesk-store.mjs +154 -0
  23. package/cli/business/vendors/zendesk-tickets.mjs +21 -0
  24. package/cli/business/vendors/zendesk-token.mjs +31 -0
  25. package/cli/business/vendors/zendesk-transport.mjs +48 -0
  26. package/cli/business/vendors/zendesk.mjs +61 -0
  27. package/cli/deploy-command.mjs +5 -4
  28. package/cli/deploy-inputs.mjs +1 -1
  29. package/cli/deploy.mjs +6 -4
  30. package/cli/production-auth.mjs +16 -3
  31. package/cli/production-mail.mjs +18 -1
  32. package/cli/site.mjs +1 -1
  33. package/global/CHANGELOG.md +21 -0
  34. package/modules/deploy-kit/README.md +10 -0
  35. package/modules/deploy-kit/provider.mjs +18 -2
  36. package/modules/import-kit/src/d1/jobs.ts +3 -1
  37. package/modules/import-kit/src/d1/retirement-scope.ts +2 -2
  38. package/modules/import-kit/src/d1/retirement-tally.ts +1 -0
  39. package/modules/import-kit/src/d1/retirement.ts +15 -5
  40. package/modules/import-kit/src/d1/types.ts +3 -0
  41. package/modules/import-kit/src/d1/wave-boundary.ts +33 -0
  42. package/modules/import-kit/src/d1/wave-retirement.ts +47 -0
  43. package/modules/import-kit/src/door/ADOPTING.md +37 -0
  44. package/modules/import-kit/src/door/README.md +66 -0
  45. package/modules/import-kit/src/door/admission.ts +13 -3
  46. package/modules/import-kit/src/door/authority.ts +1 -0
  47. package/modules/import-kit/src/door/failures.ts +1 -1
  48. package/modules/import-kit/src/door/grants.ts +19 -5
  49. package/modules/import-kit/src/door/handler.ts +12 -1
  50. package/modules/import-kit/src/door/index.ts +6 -0
  51. package/modules/import-kit/src/door/package.ts +121 -0
  52. package/modules/import-kit/src/door/planning.ts +44 -19
  53. package/modules/import-kit/src/door/protocol.ts +4 -2
  54. package/modules/import-kit/src/door/schema.ts +2 -0
  55. package/modules/import-kit/src/door/store.ts +2 -2
  56. package/modules/import-kit/src/door/types.ts +24 -1
  57. package/modules/import-kit/src/door/waves/README.md +158 -0
  58. package/modules/import-kit/src/door/waves/digests.ts +38 -0
  59. package/modules/import-kit/src/door/waves/inventory.ts +84 -0
  60. package/modules/import-kit/src/door/waves/journal.ts +19 -0
  61. package/modules/import-kit/src/door/waves/lifecycle.ts +242 -0
  62. package/modules/import-kit/src/door/waves/preparation.ts +190 -0
  63. package/modules/import-kit/src/door/waves/schema.ts +73 -0
  64. package/modules/import-kit/src/door/waves/store.ts +112 -0
  65. package/modules/import-kit/src/door/waves/types.ts +76 -0
  66. package/onboarding/adapter.mjs +7 -1
  67. package/onboarding/operator-installer.mjs +3 -1
  68. package/package.json +1 -1
  69. package/scripts/publish-check/README.md +20 -0
  70. package/scripts/publish-check/diagnostics.mjs +73 -0
  71. package/scripts/publish-check/slots.py +37 -7
  72. package/scripts/publish-check.mjs +64 -37
@@ -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});
@@ -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: Math.min(timeoutMs??60000,60000), ...(softAfterMs ? { softAfterMs } : {}), ...(intervalMs ? { intervalMs } : {}) });
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.99",
3
+ "version": "0.2.100",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -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
+ }
@@ -18,14 +18,17 @@ def notify(message):
18
18
  os.write(3, (message + '\n').encode())
19
19
 
20
20
 
21
- def main(*, wait_seconds=3600):
21
+ def hold_slot(*, wait_seconds):
22
22
  # wait_seconds is injectable by the test interpreter, never a user knob.
23
23
  raw = os.environ.get('RER_SCAN_SLOTS', '3')
24
24
  if not raw.isdecimal() or not 1 <= int(raw) <= 8:
25
25
  notify('scan.slot-configuration')
26
26
  return 2
27
27
  directory = Path.home() / '.cache/rer/scan-slots'
28
- directory.mkdir(mode=0o700, parents=True, exist_ok=True)
28
+ try:
29
+ directory.mkdir(mode=0o700, parents=True, exist_ok=True)
30
+ except OSError as error:
31
+ return failure('slot.directory', error)
29
32
  deadline = time.monotonic() + wait_seconds
30
33
  waiting = False
31
34
  while True:
@@ -33,27 +36,40 @@ def main(*, wait_seconds=3600):
33
36
  notify('scan.slot-timeout')
34
37
  return 2
35
38
  for number in range(int(raw)):
36
- fd = os.open(directory / f'slot-{number}', os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
39
+ try:
40
+ fd = os.open(directory / f'slot-{number}', os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
41
+ except OSError as error:
42
+ return failure('slot.open', error)
37
43
  try:
38
44
  fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
39
45
  except BlockingIOError:
40
46
  os.close(fd)
41
47
  continue
48
+ except OSError as error:
49
+ os.close(fd)
50
+ return failure('slot.lock', error)
42
51
  try:
43
52
  notify('acquired')
44
- worker = subprocess.Popen([sys.executable, '-B', str(Path(__file__).with_name('cli.py')),
45
- *sys.argv[1:]], stdin=subprocess.DEVNULL,
46
- stderr=subprocess.DEVNULL, pass_fds=(fd,))
53
+ try:
54
+ worker = subprocess.Popen([sys.executable, '-B', str(Path(__file__).with_name('cli.py')),
55
+ *sys.argv[1:]], stdin=subprocess.DEVNULL,
56
+ stderr=subprocess.DEVNULL, pass_fds=(fd,))
57
+ except OSError as error:
58
+ return failure('worker.launch', error)
47
59
  # Node's pipe closes even on SIGKILL. Stop only this private
48
60
  # group, so an orphan never consumes CPU or holds a slot.
49
61
  while worker.poll() is None:
50
62
  if parent_closed(0.2):
51
63
  os.killpg(os.getpgrp(), signal.SIGKILL)
64
+ if worker.returncode < 0:
65
+ notify(f'worker.signal:{-worker.returncode}')
66
+ else:
67
+ notify(f'worker.exit:{worker.returncode}')
52
68
  return worker.returncode
53
69
  finally:
54
70
  os.close(fd)
55
71
  if not waiting:
56
- print('waiting for a scan slot', file=sys.stderr, flush=True)
72
+ notify('waiting')
57
73
  waiting = True
58
74
  remaining = deadline - time.monotonic()
59
75
  if remaining <= 0:
@@ -63,5 +79,19 @@ def main(*, wait_seconds=3600):
63
79
  return 2
64
80
 
65
81
 
82
+ def failure(stage, error):
83
+ # Only an OS errno crosses fd 3, never paths, argv or exception messages.
84
+ number = error.errno if isinstance(error.errno, int) and 0 < error.errno < 1000 else 0
85
+ notify(f'{stage}:{number}')
86
+ return 2
87
+
88
+
89
+ def main(*, wait_seconds=3600):
90
+ try:
91
+ return hold_slot(wait_seconds=wait_seconds)
92
+ except OSError as error:
93
+ return failure('slot.wait', error)
94
+
95
+
66
96
  if __name__ == '__main__':
67
97
  raise SystemExit(main())