@runeditrun/rer 0.2.100 → 0.2.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/README.md +30 -102
  2. package/cli/README.md +16 -89
  3. package/cli/app-address.mjs +2 -0
  4. package/cli/business/api.mjs +2 -1
  5. package/cli/business/choices.mjs +1 -1
  6. package/cli/business/command.mjs +36 -30
  7. package/cli/business/connect.mjs +1 -1
  8. package/cli/business/lease.mjs +0 -9
  9. package/cli/business/local-commands.mjs +1 -27
  10. package/cli/business/outbound.mjs +2 -14
  11. package/cli/business/questions.mjs +85 -48
  12. package/cli/business/service.mjs +1 -24
  13. package/cli/business/setup-command.mjs +33 -26
  14. package/cli/business/state.mjs +1 -1
  15. package/cli/business/supervisor.mjs +26 -91
  16. package/cli/companion/command.mjs +2 -0
  17. package/cli/demo-native.mjs +1 -1
  18. package/cli/parse.mjs +2 -1
  19. package/global/CHANGELOG.md +24 -0
  20. package/global/README.md +30 -102
  21. package/global/main.mjs +2 -0
  22. package/modules/container/adapters/d1-limits.mjs +2 -10
  23. package/modules/d1-transport/limits.d.mts +10 -0
  24. package/modules/d1-transport/limits.mjs +10 -0
  25. package/modules/demo-host/native/prepare.mjs +2 -1
  26. package/modules/import-kit/schema/importers.schema.json +104 -2
  27. package/modules/import-kit/src/d1/README.md +13 -0
  28. package/modules/import-kit/src/d1/apply-reads.ts +48 -0
  29. package/modules/import-kit/src/d1/apply.ts +21 -8
  30. package/modules/import-kit/src/d1/history.ts +2 -2
  31. package/modules/import-kit/src/d1/types.ts +11 -0
  32. package/modules/import-kit/src/door/ADOPTING.md +0 -37
  33. package/modules/import-kit/src/door/README.md +0 -66
  34. package/modules/import-kit/src/door/admission.ts +3 -13
  35. package/modules/import-kit/src/door/authority.ts +0 -1
  36. package/modules/import-kit/src/door/failures.ts +1 -1
  37. package/modules/import-kit/src/door/grants.ts +5 -19
  38. package/modules/import-kit/src/door/handler.ts +1 -12
  39. package/modules/import-kit/src/door/index.ts +0 -6
  40. package/modules/import-kit/src/door/planning.ts +19 -44
  41. package/modules/import-kit/src/door/protocol.ts +2 -4
  42. package/modules/import-kit/src/door/schema.ts +0 -2
  43. package/modules/import-kit/src/door/store.ts +2 -2
  44. package/modules/import-kit/src/door/types.ts +1 -24
  45. package/modules/import-kit/src/provenance/credentials.ts +14 -5
  46. package/modules/import-kit/src/status/evidence.ts +3 -1
  47. package/modules/import-kit/src/status/index.ts +17 -2
  48. package/modules/import-kit/src/status/schema.ts +8 -0
  49. package/modules/import-kit/src/status/types.ts +25 -4
  50. package/package.json +1 -1
  51. package/cli/business/archive-task.mjs +0 -32
  52. package/cli/business/bounded-waves.d.mts +0 -23
  53. package/cli/business/bounded-waves.mjs +0 -57
  54. package/cli/business/catchup-task.mjs +0 -49
  55. package/cli/business/export-inputs.mjs +0 -79
  56. package/cli/business/export-task.mjs +0 -24
  57. package/cli/business/legacy-package.mjs +0 -117
  58. package/cli/business/package-transfer.mjs +0 -127
  59. package/cli/business/setup-demo.mjs +0 -95
  60. package/cli/business/vendor-door.mjs +0 -303
  61. package/cli/business/vendors/README.md +0 -215
  62. package/cli/business/vendors/calendly.mjs +0 -80
  63. package/cli/business/vendors/common.mjs +0 -280
  64. package/cli/business/vendors/credential-names.mjs +0 -124
  65. package/cli/business/vendors/index.mjs +0 -96
  66. package/cli/business/vendors/pagerduty.mjs +0 -61
  67. package/cli/business/vendors/sentry.mjs +0 -85
  68. package/cli/business/vendors/sorry.mjs +0 -106
  69. package/cli/business/vendors/uptimerobot.mjs +0 -21
  70. package/cli/business/vendors/zendesk-attachments.mjs +0 -57
  71. package/cli/business/vendors/zendesk-encode.mjs +0 -133
  72. package/cli/business/vendors/zendesk-guide.mjs +0 -34
  73. package/cli/business/vendors/zendesk-package.mjs +0 -28
  74. package/cli/business/vendors/zendesk-pages.mjs +0 -61
  75. package/cli/business/vendors/zendesk-people.mjs +0 -7
  76. package/cli/business/vendors/zendesk-store.mjs +0 -154
  77. package/cli/business/vendors/zendesk-tickets.mjs +0 -21
  78. package/cli/business/vendors/zendesk-token.mjs +0 -31
  79. package/cli/business/vendors/zendesk-transport.mjs +0 -48
  80. package/cli/business/vendors/zendesk.mjs +0 -61
  81. package/modules/import-kit/src/door/package.ts +0 -121
  82. package/modules/import-kit/src/door/waves/README.md +0 -158
  83. package/modules/import-kit/src/door/waves/digests.ts +0 -38
  84. package/modules/import-kit/src/door/waves/inventory.ts +0 -84
  85. package/modules/import-kit/src/door/waves/journal.ts +0 -19
  86. package/modules/import-kit/src/door/waves/lifecycle.ts +0 -242
  87. package/modules/import-kit/src/door/waves/preparation.ts +0 -190
  88. package/modules/import-kit/src/door/waves/schema.ts +0 -73
  89. package/modules/import-kit/src/door/waves/store.ts +0 -112
  90. package/modules/import-kit/src/door/waves/types.ts +0 -76
@@ -47,14 +47,35 @@ export interface NoImporter {
47
47
  evidence: NoneEvidence;
48
48
  }
49
49
 
50
+ /** A version 1 row: the evidence-bound status of an importer. */
50
51
  export type ImporterRecord = VerifiedImporter | DocumentedImporter | NoImporter;
51
52
 
52
- export interface ImportersDocument {
53
- $schema: './schema/importers.schema.json';
54
- schemaVersion: 1;
55
- importers: ImporterRecord[];
53
+ export type MoveInAccepts = 'file' | 'key' | 'address';
54
+
55
+ /** What a seed's Move in page takes and how to get it out of the old tool (version 2). */
56
+ export interface MoveInDeclaration {
57
+ /** The seed's Move in path; starts with `/`. */
58
+ moveIn: string;
59
+ /** A non-empty subset of file, key and address. */
60
+ accepts: MoveInAccepts[];
61
+ /** One short paragraph of plain text, at most 1,000 characters. */
62
+ export: string;
56
63
  }
57
64
 
65
+ /** A version 2 row; `status` and `evidence` appear together or not at all. */
66
+ export type MoveInImporterRecord =
67
+ | ({ incumbent: string; product: string } & MoveInDeclaration)
68
+ | (VerifiedImporter & MoveInDeclaration)
69
+ | (DocumentedImporter & MoveInDeclaration)
70
+ | (NoImporter & MoveInDeclaration);
71
+
72
+ export type ImportersDocument = {
73
+ $schema: './schema/importers.schema.json';
74
+ } & (
75
+ | { schemaVersion: 1; importers: ImporterRecord[]; format?: undefined }
76
+ | { schemaVersion: 2; importers: MoveInImporterRecord[]; format?: string }
77
+ );
78
+
58
79
  /** A resolver must return the bytes it actually resolved and the final URL. */
59
80
  export interface ResolvedEvidenceArtifact {
60
81
  url: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.100",
3
+ "version": "0.2.101",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,32 +0,0 @@
1
- import { withUploadedBundle, door, DOOR_ID, count, plural } from './vendor-door.mjs';
2
-
3
- // Key order is not part of the door's counts; compare them as values.
4
- const stable = value => JSON.stringify(value, (_, item) => item && typeof item === 'object' && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item);
5
-
6
- /**
7
- * Perform a claimed switchoff.archive: the vendor's final export uploaded into
8
- * the app's import door, kept there by the door's `archive` action as the tool's
9
- * archive in the app's own storage (`business-archive/<tool>/<takenAt>`, the tool
10
- * being the vendor's namespace), then read back by the door's `restore-check`,
11
- * which parses it and writes nothing native. Run checks that the archive and its
12
- * test-restore are exactly this bundle, then completes with the archive_id and
13
- * the bundle's SHA-256; Business runs its own restore-check before the task is
14
- * done. Returns the line to say.
15
- */
16
- export function runArchive({ lease, task, grant, directory, say, ownerPage }) {
17
- const takenAt = new Date().toISOString();
18
- return withUploadedBundle({ lease, task, grant, directory, say,
19
- doing: (name, origin, keyed) => `Archiving the final ${name} export through the shipped read-only adapter. ${keyed ? 'The key stays on this computer; the archive' : 'The archive'} goes only to the app's import door at ${origin} and stays in the app's own storage.` },
20
- async ({ vendor, name, origin, uploadId, bundle, fresh }) => {
21
- const kept = await door(await fresh('archive'), { uploadId, tool: vendor, takenAt }, lease.signal);
22
- const [input, unique, rejected] = ['input', 'unique', 'rejected'].map(field => count(kept.counts?.[field]));
23
- if (typeof kept.archiveId !== 'string' || !DOOR_ID.test(kept.archiveId) || kept.key !== `business-archive/${vendor}/${takenAt}` || kept.tool !== vendor
24
- || kept.takenAt !== takenAt || kept.bundleSha256 !== bundle.sha256 || kept.bytes !== bundle.size || [input, unique, rejected].includes(null)) throw Error("The app's import door returned an invalid archive answer.");
25
- // The door reads its own copy back and parses it as a restore would, with no native writes.
26
- const restored = await door(await fresh('restore-check'), { archiveId: kept.archiveId }, lease.signal);
27
- if (restored.archiveId !== kept.archiveId || restored.key !== kept.key || restored.bundleSha256 !== bundle.sha256 || restored.bytes !== bundle.size
28
- || stable(restored.counts) !== stable(kept.counts) || !Array.isArray(restored.rejected)) throw Error(`The app's test-restore of the ${name} archive does not match the exported file. The archive was not reported.`);
29
- return { outputs: { archive_id: kept.archiveId, bundle_sha256: bundle.sha256 },
30
- said: `Archive kept in ${origin} as ${kept.key}: ${plural(bundle.size, 'byte')}, ${plural(unique, 'source record')}, test-restored with no writes (${rejected} rejected). Business test-restores it again itself; cancelling ${name} stays the owner's step in the browser: ${ownerPage}` };
31
- });
32
- }
@@ -1,23 +0,0 @@
1
- import type { WaveTotals, WaveBinding } from '../../modules/import-kit/src/door/waves/types.ts';
2
- export interface BoundedApproval { id: string; at: string }
3
- export interface BoundedArrangement {
4
- protocol: 'rer-bounded-waves/1'; migration: string; revision: string; policy: string;
5
- catchup?: boolean; parentId?: string;
6
- parentApproval?: BoundedApproval & { fingerprint: string };
7
- children?: { ordinal: number; fingerprint: string; approval?: BoundedApproval }[];
8
- }
9
- export interface BoundedAggregate {
10
- protocol: 'rer-bounded-waves/1'; parentId: string; fingerprint: string;
11
- aggregateSuccess: true; nativeSuccess: boolean; completeHead: string; counts: WaveTotals;
12
- goneButRetained: { kind: string; sourceId: string }[]; children: number;
13
- }
14
- export interface BoundedPending {
15
- parentId: string; state: 'awaiting_parent_approval' | 'awaiting_child_approval' | 'partial';
16
- aggregateSuccess: false; fingerprint?: string; ordinal?: number; children?: unknown[];
17
- }
18
- export function sendBoundedWaves(options: {
19
- arrangement: BoundedArrangement; uploadId: string; signal: AbortSignal;
20
- fresh(action: string, extra: { bounded: WaveBinding }): Promise<unknown>;
21
- request(grant: unknown, body: Record<string, unknown>, signal: AbortSignal): Promise<any>;
22
- checkpoint?(progress: Record<string, string | number | boolean | undefined>): Promise<void>;
23
- }): Promise<BoundedAggregate | BoundedPending>;
@@ -1,57 +0,0 @@
1
- const PROTOCOL = 'rer-bounded-waves/1';
2
- const digest = value => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value);
3
- const id = value => typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value);
4
- /** Business supplies a typed arrangement and fresh grants. The sender cannot
5
- * choose a destination or manufacture any parent/child approval. */
6
- export async function sendBoundedWaves({ arrangement, uploadId, fresh, request, signal, checkpoint = async () => {} }) {
7
- if (arrangement?.protocol !== PROTOCOL || !['migration','revision','policy'].every(k => id(arrangement[k]))) throw Error('BOUNDED_TASK_REQUIRED: Business must supply the typed bounded-wave arrangement.');
8
- const binding = { protocol: PROTOCOL, migration: arrangement.migration, revision: arrangement.revision, policy: arrangement.policy };
9
- const post = async (action, body, extra = {}) => {
10
- signal.throwIfAborted();
11
- const grant = await fresh(action, { bounded: { ...binding, ...extra } });
12
- return request(grant, body, signal);
13
- };
14
- let parentId = arrangement.parentId;
15
- if (parentId !== undefined && !id(parentId)) throw Error('Invalid bounded parent identifier.');
16
- // Replaying page zero resolves the durable preparation cursor, including a
17
- // response lost before Business saved the latest page checkpoint.
18
- {
19
- let ordinal = 0;
20
- for (;;) {
21
- const prepared = await post('prepare', { ...(parentId ? { parentId } : { uploadId, ...(arrangement.catchup ? { catchup: true } : {}) }), ordinal });
22
- if (!id(prepared.parentId) || !Number.isSafeInteger(prepared.next) || prepared.next <= ordinal) throw Error('Invalid bounded preparation response.');
23
- parentId = prepared.parentId; ordinal = prepared.next;
24
- await checkpoint({ parent_id: parentId, prepared_children: ordinal, aggregate_success: false });
25
- const status = await post('wave-status', { parentId });
26
- if (!Number.isSafeInteger(status.census?.children) || status.census.children < ordinal) throw Error('Invalid complete census response.');
27
- if (ordinal === status.census.children) break;
28
- }
29
- }
30
- if (!id(parentId)) throw Error('Invalid bounded parent identifier.');
31
- const sealed = await post('seal-parent', { parentId });
32
- if (!digest(sealed.fingerprint) || sealed.parentId !== parentId || !Array.isArray(sealed.children)) throw Error('Invalid sealed parent response.');
33
- const approval = arrangement.parentApproval;
34
- if (!approval) return { parentId, fingerprint: sealed.fingerprint, state: 'awaiting_parent_approval', aggregateSuccess: false, children: sealed.children };
35
- if (approval.fingerprint !== sealed.fingerprint || !Array.isArray(arrangement.children) || arrangement.children.length !== sealed.children.length
36
- || arrangement.children.some((c, i) => c.ordinal !== i || c.fingerprint !== sealed.children[i].fingerprint)) throw Error('Business approvals differ from the immutable server children.');
37
- const parentBinding = { parentFingerprint: approval.fingerprint, parentApprovalRecordId: approval.id, parentApprovedAt: approval.at };
38
- await post('review-parent', { parentId }, parentBinding);
39
- for (const child of arrangement.children) {
40
- // Approvals are request/grant inputs to Business, never proof from this task.
41
- if (!child.approval) return { parentId, state: 'awaiting_child_approval', ordinal: child.ordinal, aggregateSuccess: false };
42
- for (;;) {
43
- const status = await post('wave-status', { parentId });
44
- if (status.state === 'withdrawn') throw Error('Bounded parent was withdrawn.');
45
- const current = status.children?.[child.ordinal];
46
- if (!current || current.fingerprint !== child.fingerprint) throw Error('Server child inventory changed.');
47
- if (current.childSuccess) break;
48
- const applied = await post('apply-child', { parentId, ordinal: child.ordinal, reviewedFingerprint: child.fingerprint }, { ...parentBinding, childFingerprint: child.fingerprint });
49
- await checkpoint({ parent_id: parentId, child: child.ordinal, job_id: applied.job?.id, child_success: applied.childSuccess === true, aggregate_success: false });
50
- if (!applied.job || ['failed','undone','rolled_back'].includes(applied.job.state)) return { parentId, state: 'partial', ordinal: child.ordinal, aggregateSuccess: false };
51
- }
52
- }
53
- const aggregate = await post('finalize-parent', { parentId }, parentBinding);
54
- if (aggregate.parentId !== parentId || aggregate.fingerprint !== sealed.fingerprint || aggregate.aggregateSuccess !== true || aggregate.completeHead !== parentId) throw Error('Destination did not return a complete aggregate receipt.');
55
- await checkpoint({ parent_id: parentId, aggregate_success: true });
56
- return aggregate;
57
- }
@@ -1,49 +0,0 @@
1
- import { sendBoundedWaves } from './bounded-waves.mjs';
2
- import { ExportWaiting } from './vendor-door.mjs';
3
- import { withUploadedBundle, door, checkedPreview, plural } from './vendor-door.mjs';
4
-
5
- // Why the door refused a catch-up under the owner's standing approval
6
- // (import-kit door, "Catch-up and recovery": `catchup_requires_proposal:<reason>`).
7
- const beyond = {
8
- empty_baseline: 'the app holds no earlier import to compare with',
9
- source_records_gone: 'records were removed in the old tool',
10
- native_or_mapping_conflict: 'records changed in the app since they were imported',
11
- rejected_or_unmapped_records: 'some records cannot be brought over as they are',
12
- change_must_be_under_five_percent: '5% or more of the records changed',
13
- };
14
-
15
- /**
16
- * Perform a claimed catchup.vendor: the vendor's current export uploaded into the
17
- * app's import door, then a catch-up plan of it (a zero-write preview of what
18
- * changed since the import). Business mints the plan grant under the owner's
19
- * standing catch-up approval when one covers this tool; the door then refuses a
20
- * change at or above its bound, and run asks for a plan grant for the owner's
21
- * review instead. Completion reports the door's upload_id and plan_id and the
22
- * bundle's SHA-256; Business applies the plan under the standing approval or
23
- * after the owner approves it. Returns the line to say.
24
- */
25
- export function runCatchup({ lease, task, grant, directory, say, ownerPage }) {
26
- return withUploadedBundle({ lease, task, grant, directory, say,
27
- doing: (name, origin, keyed) => `Bringing over recent ${name} changes through the shipped read-only adapter. ${keyed ? 'The key stays on this computer; records' : 'Records'} go only to the app's import door at ${origin}.` },
28
- async ({ origin, uploadId, bundle, fresh, preview: uploadedPreview }) => {
29
- if (task.bounded_waves) {
30
- const result=await sendBoundedWaves({arrangement:{...task.bounded_waves,catchup:true},uploadId,fresh,request:door,signal:lease.signal,checkpoint:cp=>lease.checkpointSaved(cp)});
31
- if (!result.aggregateSuccess) throw new ExportWaiting(`Bounded catch-up ${result.parentId} is ${result.state}; no complete migration receipt.`, 'configuration_required');
32
- return {outputs:{parent_id:result.parentId,parent_fingerprint:result.fingerprint,aggregate_success:true},said:`Destination ${origin} reconciled the complete bounded catch-up ${result.parentId}.`};
33
- }
34
- let planGrant = await fresh('plan'), refused = null, preview;
35
- try { preview = uploadedPreview ?? await door(planGrant, { uploadId, catchup: true }, lease.signal); }
36
- catch (error) {
37
- const reason = planGrant.catchup === 'bounded' && /^catchup_requires_proposal:([a-z0-9_]+)$/.exec(error.doorCode ?? '')?.[1];
38
- if (!reason) throw error;
39
- refused = Object.hasOwn(beyond, reason) ? beyond[reason] : 'it is more than that approval covers';
40
- planGrant = await fresh('plan', { review: true });
41
- preview = await door(planGrant, { uploadId, catchup: true }, lease.signal);
42
- }
43
- const { records, created, updated, unchanged, gone, rejected } = checkedPreview(preview, bundle);
44
- const planned = `Catch-up planned in ${origin}: ${plural(records, 'source record')}; ${created} new, ${updated} changed, ${unchanged} unchanged, ${gone} gone, ${rejected} rejected.`;
45
- return { outputs: { upload_id: uploadId, plan_id: preview.planId, bundle_sha256: bundle.sha256 },
46
- said: planGrant.catchup === 'bounded' ? `${planned} It is within the owner's standing catch-up approval, so Business applies it and checks the result.`
47
- : `${planned} ${refused ? `It needs the owner's own approval (${refused}): nothing` : 'Nothing'} changes in the app until the owner approves this catch-up in the browser: ${ownerPage}` };
48
- });
49
- }
@@ -1,79 +0,0 @@
1
- // The non-secret inputs a leased vendor task carries for its shipped adapter
2
- // (the executor task view's `export_inputs`, platform executors/tasks.ts): one
3
- // closed shape per vendor, never a key. Business fills them from the Business's
4
- // inventory evidence and the owner's answers; a window the plan does not state
5
- // is the vendor's full history (`since` null or absent).
6
- //
7
- // sentry {organization: {id, slug, region: 'us'|'de'}, window: {since}}
8
- // pagerduty {window: {since}, schedules: 'all'} (the plan moves every rota and
9
- // keeps PagerDuty as the pager)
10
- // sorry {page: {host}}
11
- // uptimerobot needs none.
12
-
13
- const DAY = 86_400_000;
14
- const invalid = () => { throw Error('Business returned invalid export inputs for this task.'); };
15
- const plain = value => value && typeof value === 'object' && !Array.isArray(value) ? value : invalid();
16
- function closed(value, required, optional = []) {
17
- plain(value);
18
- if (Object.keys(value).some(key => !required.includes(key) && !optional.includes(key)) || required.some(key => !Object.hasOwn(value, key))) invalid();
19
- return value;
20
- }
21
- const sourceId = value => typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value) ? value : invalid();
22
- /** The window's start as an ISO instant, or null for the vendor's full history. */
23
- function since(window) {
24
- closed(window, [], ['since']);
25
- const value = window.since ?? null;
26
- if (value === null) return null;
27
- if (typeof value !== 'string' || value.length > 40 || !/^\d{4}-\d\d-\d\dT/.test(value) || !Number.isFinite(Date.parse(value))) invalid();
28
- return new Date(value).toISOString();
29
- }
30
-
31
- const shapes = {
32
- zendesk(value) {
33
- closed(value, ['account'], ['mode','highWater']);
34
- const account = closed(value.account, ['subdomain']);
35
- if (typeof account.subdomain !== 'string' || !/^(?!-)[a-z0-9-]{1,63}(?<!-)$/u.test(account.subdomain)
36
- || !['snapshot','sweep','census','retire'].includes(value.mode ?? 'snapshot')
37
- || value.highWater !== undefined && (!Number.isSafeInteger(value.highWater) || value.highWater < 0)
38
- || value.mode === 'sweep' && value.highWater === undefined) invalid();
39
- return { account: { subdomain: account.subdomain }, mode: value.mode ?? 'snapshot', ...(value.highWater !== undefined ? { highWater: value.highWater } : {}) };
40
- },
41
- uptimerobot: () => ({}),
42
- 'happytohelp-legacy': () => ({}),
43
- sentry(value, now) {
44
- closed(value, ['organization', 'window']);
45
- const organization = closed(value.organization, ['id', 'slug', 'region']);
46
- if (!['us', 'de'].includes(organization.region)) invalid();
47
- const id = Number.isSafeInteger(organization.id) && organization.id >= 0 ? String(organization.id) : sourceId(organization.id);
48
- // Full history: from the start of time, which Sentry answers with all it retains.
49
- return { organization: { id, slug: sourceId(organization.slug), region: organization.region }, start: since(value.window) ?? new Date(0).toISOString(), end: new Date(now).toISOString() };
50
- },
51
- pagerduty(value, now) {
52
- closed(value, ['window', 'schedules']);
53
- if (value.schedules !== 'all') invalid();
54
- // Rotas move into Oncall beside PagerDuty, so the window runs in whole UTC days
55
- // to two weeks ahead (the coming overrides; the default of Oncall's own
56
- // exporter). Oncall compares a schedule's final timeline over at most 31 days
57
- // and refuses a longer one, so the window starts at the plan's `since` or 31
58
- // days before its end, whichever is later.
59
- const until = Math.floor(now / DAY) * DAY + 14 * DAY, start = since(value.window), floor = until - 31 * DAY;
60
- return { start: new Date(start === null ? floor : Math.max(Math.floor(Date.parse(start) / DAY) * DAY, floor)).toISOString(), end: new Date(until).toISOString() };
61
- },
62
- sorry(value) {
63
- closed(value, ['page']);
64
- const page = closed(value.page, ['host']);
65
- if (typeof page.host !== 'string' || page.host.length > 253 || !/^[a-z0-9.-]+$/.test(page.host)) invalid();
66
- return { page: { host: page.host } };
67
- },
68
- };
69
- /** What each vendor's inputs are, in the owner's terms, while a task lacks them. */
70
- export const inputNeeds = { zendesk: 'its Zendesk account subdomain', sentry: 'its Sentry organization and export window', pagerduty: 'its export window', sorry: "its status page's address" };
71
-
72
- /** The shipped adapter's inputs for `vendor` from the leased task's
73
- * `export_inputs`, or null when the task carries none yet. */
74
- export function exportInputs(vendor, task, now = Date.now()) {
75
- if (!Object.hasOwn(shapes, vendor)) return null;
76
- if (vendor === 'uptimerobot' || vendor === 'happytohelp-legacy') return {};
77
- if (task.export_inputs === undefined || task.export_inputs === null) return null;
78
- return shapes[vendor](task.export_inputs, now);
79
- }
@@ -1,24 +0,0 @@
1
- import { sendBoundedWaves } from './bounded-waves.mjs';
2
- import { ExportWaiting } from './vendor-door.mjs';
3
- import { withUploadedBundle, door, checkedPreview, plural } from './vendor-door.mjs';
4
-
5
- /** Perform a claimed export.vendor: the shipped adapter's bundle uploaded into
6
- * the app's import door, then the door's plan (a zero-write preview), then
7
- * completion with the door's upload_id and plan_id and the bundle's SHA-256.
8
- * Returns the line to say. */
9
- export function runExport({ lease, task, grant, directory, say, ownerPage }) {
10
- return withUploadedBundle({ lease, task, grant, directory, say,
11
- doing: (name, origin, keyed) => `Exporting ${name} through the shipped read-only adapter. ${keyed ? 'The key stays on this computer; records' : 'Records'} go only to the app's import door at ${origin}.` },
12
- async ({ origin, uploadId, bundle, fresh, preview: uploadedPreview }) => {
13
- if (task.bounded_waves) {
14
- const result=await sendBoundedWaves({arrangement:task.bounded_waves,uploadId,fresh,request:door,signal:lease.signal,checkpoint:cp=>lease.checkpointSaved(cp)});
15
- if (!result.aggregateSuccess) throw new ExportWaiting(`Bounded import ${result.parentId} is ${result.state}; no complete migration receipt.`, 'configuration_required');
16
- return {outputs:{parent_id:result.parentId,parent_fingerprint:result.fingerprint,aggregate_success:true},said:`Destination ${origin} reconciled the complete bounded import ${result.parentId}.`};
17
- }
18
- const preview = uploadedPreview ?? await door(await fresh('plan'), { uploadId, ...(bundle.mode === 'sweep' ? { catchup: true } : {}) }, lease.signal), { records, created, updated, unchanged, rejected } = checkedPreview(preview, bundle);
19
- // Identifiers only: Business reads that plan from the door with its own grants
20
- // and completes the task only if it is the plan of exactly this bundle.
21
- return { outputs: { upload_id: uploadId, plan_id: preview.planId, bundle_sha256: bundle.sha256 },
22
- said: `Import preview ready in ${origin}: ${plural(records, 'source record')}; ${created} to create, ${updated} to update, ${unchanged} unchanged, ${rejected} rejected. Nothing was imported: the owner reviews and approves this move in the browser: ${ownerPage}` };
23
- });
24
- }
@@ -1,117 +0,0 @@
1
- import { constants } from 'node:fs';
2
- import { open, readFile, realpath, mkdir, chmod, rename, unlink, writeFile } from 'node:fs/promises';
3
- import { join, resolve } from 'node:path';
4
- import { createHash, randomUUID } from 'node:crypto';
5
- import { assertChunk } from './outbound.mjs';
6
- import { stateDirectory } from './state.mjs';
7
-
8
- export const LEGACY_FORMAT = 'happytohelp-legacy-chunks-v1';
9
- const MAX_BYTES = 32 * 1024 ** 3, MAX_JSON = 32 * 1024 ** 2, MAX_FILES = 100_000;
10
- const sha = bytes => createHash('sha256').update(bytes).digest('hex');
11
- const count = n => Number.isSafeInteger(n) && n >= 0;
12
- const registrationPath = () => join(stateDirectory(), 'legacy-package.json');
13
- const refused = (file, row, reason) => { throw Error(`Legacy package refused: ${file}, row ${row}: ${reason}. Use HappyToHelp's repass, then register the package again.`); };
14
- const filename = name => typeof name === 'string' && /^\w[\w.-]*$/.test(name) && name !== 'index.ndjson' && name !== 'rer-package.json';
15
-
16
- /** Every opened source is a regular file, never a symlink; hashing is streamed. */
17
- async function inspect(root, name, expected, json = false) {
18
- if (!filename(name)) refused('manifest.json', 1, 'invalid filename');
19
- let file;
20
- try { file = await open(join(root, name), constants.O_RDONLY | constants.O_NOFOLLOW); }
21
- catch { refused(name, 0, 'file unavailable'); }
22
- try {
23
- const info = await file.stat();
24
- if (!info.isFile() || !count(expected.bytes) || info.size !== expected.bytes || info.size > (json ? MAX_JSON : MAX_BYTES)) refused(name, 0, 'size mismatch or limit');
25
- const hash = createHash('sha256'), chunks = [];
26
- for await (const chunk of file.createReadStream({ autoClose: false })) { hash.update(chunk); if (json) chunks.push(chunk); }
27
- if (!/^[a-f0-9]{64}$/.test(expected.sha256 ?? '') || hash.digest('hex') !== expected.sha256) refused(name, 0, 'SHA-256 mismatch');
28
- if (!json) return;
29
- let value;
30
- try { value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks))); } catch { refused(name, 1, 'invalid JSON'); }
31
- const rows = Array.isArray(value) ? value : [value];
32
- // Check decoded strings as well as raw JSON, so escaping cannot hide a token.
33
- const detect = value => {
34
- if (typeof value === 'string') assertChunk(Buffer.from(value), []);
35
- else if (value && typeof value === 'object') for (const [key, child] of Object.entries(value)) { assertChunk(Buffer.from(key), []); detect(child); }
36
- };
37
- for (let i = 0; i < rows.length; i++) {
38
- try { assertChunk(Buffer.from(JSON.stringify(rows[i])), []); detect(rows[i]); } catch { refused(name, i + 1, 'outbound detector'); }
39
- }
40
- return value;
41
- } finally { await file.close(); }
42
- }
43
-
44
- /** Validate the complete legacy database package before any upload. Media are customer originals. */
45
- export async function verifyLegacyPackage(directory) {
46
- let path, handle, manifestBytes;
47
- try { path = await realpath(resolve(directory)); handle = await open(join(path, 'manifest.json'), constants.O_RDONLY | constants.O_NOFOLLOW); }
48
- catch { refused('manifest.json', 0, 'file unavailable'); }
49
- try { const info = await handle.stat(); if (!info.isFile() || info.size > MAX_JSON) refused('manifest.json', 1, 'manifest size'); manifestBytes = await handle.readFile(); }
50
- finally { await handle.close(); }
51
- let manifest;
52
- try { manifest = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(manifestBytes)); } catch { refused('manifest.json', 1, 'invalid JSON'); }
53
- if (manifest?.format !== LEGACY_FORMAT || !Array.isArray(manifest.chunks) || !Array.isArray(manifest.context) || !Array.isArray(manifest.objects)
54
- || !manifest.source?.rowCounts || typeof manifest.source.rowCounts !== 'object' || Array.isArray(manifest.source.rowCounts)
55
- || !Array.isArray(manifest.source.attachments) || manifest.attachmentsComplete !== true) refused('manifest.json', 1, 'invalid legacy manifest');
56
- await inspect(path, 'manifest.json', { bytes: manifestBytes.length, sha256: sha(manifestBytes) }, true);
57
- const files = [{ path: 'manifest.json', file: join(path, 'manifest.json'), bytes: manifestBytes.length, sha256: sha(manifestBytes) }], seen = new Set(['manifest.json']), rowCounts = {};
58
- let totalBytes = manifestBytes.length;
59
- const add = (name, bytes, sha256) => {
60
- if (!filename(name) || seen.has(name) || files.length >= MAX_FILES) refused('manifest.json', 1, 'duplicate filename or file limit');
61
- if (!count(bytes) || (totalBytes += bytes) > MAX_BYTES) refused(name, 0, 'package byte limit');
62
- seen.add(name); files.push({ path: name, file: join(path, name), bytes, sha256 });
63
- };
64
- for (const [name, n] of Object.entries(manifest.source.rowCounts)) if (!name || !count(n)) refused('manifest.json', 1, 'invalid row counts');
65
- for (const [parts, primary] of [[manifest.chunks, true], [manifest.context, false]]) for (const part of parts) {
66
- if (!part || typeof part.table !== 'string' || !Object.hasOwn(manifest.source.rowCounts, part.table) || !count(part.count)) refused('manifest.json', 1, 'invalid chunk count or table');
67
- add(part.file, part.bytes, part.sha256);
68
- const rows = await inspect(path, part.file, part, true);
69
- if (!Array.isArray(rows) || rows.length !== part.count) refused(part.file, 0, 'row count mismatch');
70
- if (primary) Object.defineProperty(rowCounts, part.table, { value: (Object.hasOwn(rowCounts, part.table) ? rowCounts[part.table] : 0) + rows.length, enumerable: true, configurable: true });
71
- }
72
- for (const [table, n] of Object.entries(manifest.source.rowCounts)) if ((Object.hasOwn(rowCounts, table) ? rowCounts[table] : 0) !== n) refused('manifest.json', 1, 'table count mismatch');
73
- const keys = new Map(), media = new Set(); let objectBytes = 0;
74
- for (const object of manifest.objects) {
75
- if (!object || typeof object.key !== 'string' || !object.key.startsWith('legacy/') || object.key.split('/').includes('..') || object.mediaId == null
76
- || keys.has(object.key) || media.has(String(object.mediaId)) || !count(object.size)) refused('manifest.json', 1, 'invalid object identity');
77
- keys.set(object.key, String(object.mediaId)); media.add(String(object.mediaId));
78
- add(object.file, object.size, object.sha256);
79
- await inspect(path, object.file, { bytes: object.size, sha256: object.sha256 });
80
- objectBytes += object.size;
81
- }
82
- const attachments = manifest.source.attachments;
83
- if (attachments.length !== manifest.objects.length || new Set(attachments.map(e => e?.key)).size !== attachments.length
84
- || attachments.some(e => !e || keys.get(e.key) !== String(e.mediaId))) refused('manifest.json', 1, 'attachment count or identity mismatch');
85
- const bytes = files.reduce((n, f) => n + f.bytes, 0);
86
- if (!count(bytes) || bytes > MAX_BYTES) refused('manifest.json', 1, 'package byte limit');
87
- return { path, manifestSha256: sha(manifestBytes), bytes, rowCounts: { ...manifest.source.rowCounts }, objects: { count: manifest.objects.length, bytes: objectBytes }, files, manifest };
88
- }
89
-
90
- export async function loadLegacyRegistration() {
91
- try { return JSON.parse(await readFile(registrationPath(), 'utf8')); } catch (error) { if (error.code === 'ENOENT') return null; throw error; }
92
- }
93
- export async function registeredLegacyPackage() {
94
- const saved = await loadLegacyRegistration();
95
- if (!saved) throw Error('No legacy package is registered. Run rer business legacy-package add DIR.');
96
- let current;
97
- try { current = await verifyLegacyPackage(saved.path); } catch { throw Error(`Legacy package changed or is unavailable: ${saved.path}. Use HappyToHelp's repass and register it again.`); }
98
- if (JSON.stringify(summary(current)) !== JSON.stringify(saved)) throw Error(`Legacy package changed: ${saved.path}. Register the package again.`);
99
- return current;
100
- }
101
- const summary = ({ path, manifestSha256, bytes, rowCounts, objects }) => ({ path, manifestSha256, bytes, rowCounts, objects });
102
- export async function legacyPackage(action, directory) {
103
- if (!['add', 'remove', 'status'].includes(action)) throw Error('Usage: rer business legacy-package add|remove|status DIR');
104
- if (action === 'add') {
105
- const verified = await verifyLegacyPackage(directory), target = registrationPath();
106
- await mkdir(stateDirectory(), { recursive: true, mode: 0o700 }); await chmod(stateDirectory(), 0o700);
107
- const temporary = target + '.' + randomUUID() + '.tmp';
108
- try { await writeFile(temporary, JSON.stringify(summary(verified)), { mode: 0o600, flag: 'wx' }); await rename(temporary, target); }
109
- finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
110
- }
111
- const saved = await loadLegacyRegistration();
112
- if (saved && saved.path !== await realpath(resolve(directory))) throw Error('That directory is not the registered legacy package.');
113
- if (action === 'remove') await unlink(registrationPath()).catch(error => { if (error.code !== 'ENOENT') throw error; });
114
- const result = action === 'remove' ? null : saved;
115
- // Registration never prints paths or customer rows; a changed task names its package locally.
116
- process.stdout.write(JSON.stringify(result ? { bytes: result.bytes, rowCounts: result.rowCounts, objects: result.objects, manifestSha256: result.manifestSha256 } : { registered: false }) + '\n');
117
- }
@@ -1,127 +0,0 @@
1
- import { constants } from 'node:fs';
2
- import { open, mkdir, chmod, readFile, rename, unlink, writeFile } from 'node:fs/promises';
3
- import { join } from 'node:path';
4
- import { createHash, randomUUID } from 'node:crypto';
5
-
6
- const CHUNK = 65_536, MAX_BYTES = 32 * 1024 ** 3;
7
- const hash = bytes => createHash('sha256').update(bytes).digest('hex');
8
- const id = value => typeof value === 'string' && /^[0-9A-Za-z][0-9A-Za-z_-]{0,127}$/.test(value);
9
- const pathValid = value => typeof value === 'string' && /^[A-Za-z0-9_][A-Za-z0-9_./-]*$/.test(value) && !value.split('/').some(p => !p || p === '.' || p === '..');
10
- async function durableJson(path, value) {
11
- const temporary = path + '.' + randomUUID() + '.tmp';
12
- const file = await open(temporary, 'wx', 0o600);
13
- try { await file.writeFile(JSON.stringify(value)); await file.sync(); } finally { await file.close(); }
14
- try { await rename(temporary, path); } finally { await unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
15
- }
16
-
17
- /** Concatenated index and exact source files; no JSON reserialization of the package files. */
18
- export async function buildPackage({ files, source, mode = 'snapshot', records, totals, quota, directory, signal }) {
19
- if (!Number.isSafeInteger(quota) || quota < CHUNK || quota > 2 * 1024 ** 2) throw Error('Unsupported package grant byte quota.');
20
- if (!Array.isArray(files) || !files.length || files.length > 100_000) throw Error('Invalid package files.');
21
- let offset = 0; const names = new Set();
22
- const entries = files.map(file => {
23
- if (!pathValid(file.path) || file.path === 'index.ndjson' || names.has(file.path) || !Number.isSafeInteger(file.bytes) || file.bytes < 0 || !/^[a-f0-9]{64}$/.test(file.sha256)) throw Error('Invalid package file.');
24
- names.add(file.path);
25
- const entry = { path: file.path, bytes: file.bytes, sha256: file.sha256, offset };
26
- offset += file.bytes;
27
- if (!Number.isSafeInteger(offset) || offset > MAX_BYTES) throw Error('Package byte limit.');
28
- return entry;
29
- });
30
- const index = Buffer.from(entries.map(row => JSON.stringify(row) + '\n').join(''));
31
- if (index.length > quota || index.length + offset > MAX_BYTES) throw Error('Package index or stream byte limit.');
32
- await mkdir(directory, { recursive: true, mode: 0o700 }); await chmod(directory, 0o700);
33
- const segments = [], streamHash = createHash('sha256'); let pending = Buffer.alloc(0), bytes = 0;
34
- const save = async value => {
35
- const ordinal = segments.length, path = join(directory, `segment-${ordinal}`), temporary = path + '.tmp';
36
- const file = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW, 0o600);
37
- try { await file.writeFile(value); await file.sync(); } finally { await file.close(); }
38
- await rename(temporary, path); segments.push({ bytes: value.length, sha256: hash(value), file: path });
39
- };
40
- const feed = async value => {
41
- signal?.throwIfAborted(); streamHash.update(value); bytes += value.length;
42
- let position = 0;
43
- while (position < value.length) {
44
- const length = Math.min(quota - pending.length, value.length - position);
45
- pending = Buffer.concat([pending, value.subarray(position, position + length)]); position += length;
46
- if (pending.length === quota) { await save(pending); pending = Buffer.alloc(0); }
47
- }
48
- };
49
- await feed(index);
50
- for (const file of files) {
51
- const handle = await open(file.file, constants.O_RDONLY | constants.O_NOFOLLOW), digest = createHash('sha256'); let size = 0;
52
- try {
53
- const info = await handle.stat(); if (!info.isFile() || info.size !== file.bytes) throw Error('Package source changed before upload.');
54
- for await (const chunk of handle.createReadStream({ autoClose: false, highWaterMark: CHUNK })) {
55
- size += chunk.length; if (size > file.bytes) throw Error('Package source changed before upload.');
56
- digest.update(chunk); await feed(chunk);
57
- }
58
- if (size !== file.bytes || digest.digest('hex') !== file.sha256) throw Error('Package source changed before upload.');
59
- } finally { await handle.close(); }
60
- }
61
- if (pending.length) await save(pending);
62
- const manifest = { format: 'rer-door-package/1', source, mode, stream: { bytes, sha256: streamHash.digest('hex') },
63
- segments: segments.map(({ bytes, sha256 }) => ({ bytes, sha256 })), index: { path: 'index.ndjson', bytes: index.length, sha256: hash(index) }, records, totals };
64
- const manifestBytes = Buffer.from(JSON.stringify(manifest));
65
- if (manifestBytes.length > quota) throw Error('Package manifest exceeds the grant byte quota.');
66
- const manifestPath = join(directory, 'rer-package.json'); await writeFile(manifestPath, manifestBytes, { mode: 0o600 });
67
- return { manifest, directory, segments, manifestFile: { bytes: manifestBytes.length, sha256: hash(manifestBytes), file: manifestPath } };
68
- }
69
-
70
- /** Persist upload ids before their first request. Inventory is the authority after an interrupted ACK. */
71
- export async function transferPackage({ prepared, fresh, request, signal, checkpoint, catchup = false, planAction = 'plan', planInput = {} }) {
72
- const { manifestFile, segments, directory } = prepared, journalPath = join(directory, 'journal.json');
73
- let journal;
74
- try { journal = JSON.parse(await readFile(journalPath, 'utf8')); } catch (error) { if (error.code !== 'ENOENT') throw error; }
75
- if (!journal || journal.manifestSha256 !== manifestFile.sha256) journal = { manifestSha256: manifestFile.sha256, uploads: {}, supported: false };
76
- if (!journal.uploads || typeof journal.uploads !== 'object') throw Error('Invalid package journal.');
77
- const save = () => durableJson(journalPath, journal);
78
- let done = 0;
79
- async function upload(file, name, key) {
80
- let uploadId = journal.uploads[key], inventory = null;
81
- if (uploadId !== undefined && !id(uploadId)) throw Error('Invalid package journal upload id.');
82
- if (uploadId) {
83
- try { inventory = await request(await fresh('uploads'), { uploadId }, signal); }
84
- catch (error) { if (error.doorStatus !== 404) throw error; uploadId = null; }
85
- }
86
- if (!uploadId) { uploadId = randomUUID(); journal.uploads[key] = uploadId; await save(); }
87
- const handle = await open(file.file, constants.O_RDONLY | constants.O_NOFOLLOW);
88
- try {
89
- const totalChunks = Math.ceil(file.bytes / CHUNK), received = new Map();
90
- if (inventory) {
91
- if (inventory.uploadId !== uploadId || inventory.bundleSha256 !== file.sha256 || !Array.isArray(inventory.received)) throw Error('Invalid package inventory.');
92
- for (const row of inventory.received) {
93
- if (!Number.isSafeInteger(row.ordinal) || row.ordinal < 0 || row.ordinal >= totalChunks || received.has(row.ordinal)) throw Error('Invalid package chunk inventory.');
94
- received.set(row.ordinal, row);
95
- }
96
- }
97
- for (let index = 0; index < totalChunks; index++) {
98
- signal.throwIfAborted();
99
- const bytes = Buffer.alloc(Math.min(CHUNK, file.bytes - index * CHUNK)), { bytesRead } = await handle.read(bytes, 0, bytes.length, index * CHUNK);
100
- if (bytesRead !== bytes.length) throw Error('Package spool changed.');
101
- const row = received.get(index);
102
- if (row && (row.sha256 !== hash(bytes) || row.size !== bytes.length)) throw Error('Package inventory differs from local bytes.');
103
- if (!row) {
104
- const result = await request(await fresh('uploads'), { uploadId, name, bundleSha256: file.sha256, totalBytes: file.bytes, totalChunks, index, chunkSha256: hash(bytes), base64: bytes.toString('base64') }, signal);
105
- if (result.uploadId !== uploadId || result.bundleSha256 !== file.sha256 || index === totalChunks - 1 && result.complete !== true) throw Error('Invalid package upload answer.');
106
- }
107
- done += bytes.length;
108
- await checkpoint({ bytes_done: done });
109
- }
110
- } finally { await handle.close(); }
111
- return uploadId;
112
- }
113
- const uploadId = await upload(manifestFile, 'rer-package.json', 'manifest');
114
- let preview;
115
- try { preview = await request(await fresh(planAction), { ...planInput, uploadId, ...(catchup ? { catchup: true } : {}) }, signal); }
116
- catch (error) {
117
- if (error.doorStatus !== 409 || error.doorCode !== 'package_segment_missing' || !Array.isArray(error.doorDetails?.missing) || !error.doorDetails.missing.length
118
- || error.doorDetails.missing.some(row => !segments.some(s => s.sha256 === row.sha256 && s.bytes === row.bytes))) {
119
- throw Object.assign(Error('SEED_PACKAGE_UNSUPPORTED: the seed did not accept a manifest-first package probe. No segments were sent.'), { cause: error });
120
- }
121
- journal.supported = true; await save();
122
- }
123
- if (preview && !journal.supported && planAction==='prepare' && preview.protocol==='rer-bounded-waves/1' && typeof preview.parentId==='string') {journal.supported=true;await save();}
124
- if (preview && !journal.supported) throw Error('SEED_PACKAGE_UNSUPPORTED: the seed did not request package segments. No segments were sent.');
125
- if (!preview) for (let index = 0; index < segments.length; index++) await upload(segments[index], 'rer-segment', String(index));
126
- return { uploadId, bundle: { size: manifestFile.bytes, sha256: manifestFile.sha256 }, ...(preview ? { preview } : {}) };
127
- }