@runeditrun/rer 0.2.84 → 0.2.86

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 (236) hide show
  1. package/README.md +156 -50
  2. package/cli/README.md +78 -2
  3. package/cli/agent-door.mjs +149 -0
  4. package/cli/agent-fixtures.mjs +16 -0
  5. package/cli/agent-mail.mjs +54 -0
  6. package/cli/agent-network.mjs +64 -0
  7. package/cli/agent-reviewers.mjs +110 -0
  8. package/cli/agent-secrets.mjs +23 -0
  9. package/cli/agent-sponsors.mjs +43 -0
  10. package/cli/agent-surface.mjs +34 -0
  11. package/cli/agent-webhooks.mjs +19 -0
  12. package/cli/authoring.mjs +2 -1
  13. package/cli/bounded.mjs +1 -1
  14. package/cli/business/archive-task.mjs +32 -0
  15. package/cli/business/carry.mjs +151 -0
  16. package/cli/business/catchup-task.mjs +42 -0
  17. package/cli/business/choices.mjs +97 -0
  18. package/cli/business/command.mjs +29 -6
  19. package/cli/business/connect.mjs +20 -8
  20. package/cli/business/detector.mjs +4 -1
  21. package/cli/business/device.mjs +13 -0
  22. package/cli/business/export-inputs.mjs +69 -0
  23. package/cli/business/export-task.mjs +17 -0
  24. package/cli/business/invocation.mjs +8 -1
  25. package/cli/business/keychain.mjs +8 -1
  26. package/cli/business/lease.mjs +11 -1
  27. package/cli/business/local-commands.mjs +80 -30
  28. package/cli/business/outbound.mjs +9 -4
  29. package/cli/business/parsers-config.mjs +8 -7
  30. package/cli/business/parsers-syntax.mjs +7 -3
  31. package/cli/business/permissions.mjs +2 -0
  32. package/cli/business/questions.mjs +89 -6
  33. package/cli/business/setup-command.mjs +49 -0
  34. package/cli/business/setup-company.json +15 -0
  35. package/cli/business/setup-demo.mjs +95 -0
  36. package/cli/business/state.mjs +44 -10
  37. package/cli/business/supervisor.mjs +97 -32
  38. package/cli/business/vendor-door.mjs +230 -0
  39. package/cli/business/vendors/README.md +29 -3
  40. package/cli/business/vendors/common.mjs +8 -6
  41. package/cli/business/vendors/index.mjs +22 -11
  42. package/cli/business/vendors/sorry.mjs +106 -0
  43. package/cli/container-transfer.mjs +2 -2
  44. package/cli/contract.mjs +7 -7
  45. package/cli/demo-deploy-error.mjs +14 -0
  46. package/cli/demo-native.mjs +6 -2
  47. package/cli/deploy-command.mjs +6 -4
  48. package/cli/deploy-config.mjs +1 -1
  49. package/cli/deploy-owner-input.mjs +3 -1
  50. package/cli/deploy-variables.mjs +58 -0
  51. package/cli/deploy.mjs +9 -7
  52. package/cli/door-clauses.mjs +157 -0
  53. package/cli/errors.mjs +1 -1
  54. package/cli/export.mjs +16 -4
  55. package/cli/hosted-output.mjs +17 -7
  56. package/cli/human-error.mjs +20 -0
  57. package/cli/inspect.mjs +4 -0
  58. package/cli/jsonc.mjs +8 -4
  59. package/cli/lifecycle.mjs +26 -6
  60. package/cli/main.mjs +2 -1
  61. package/cli/managed-data-schema.mjs +127 -0
  62. package/cli/managed-data-zip.mjs +116 -0
  63. package/cli/managed-data.mjs +110 -0
  64. package/cli/managed-server.mjs +25 -14
  65. package/cli/native-owner-entry.mjs +86 -0
  66. package/cli/output.mjs +3 -2
  67. package/cli/parse.mjs +2 -2
  68. package/cli/portability-empty.mjs +65 -0
  69. package/cli/portability-migrations.mjs +33 -9
  70. package/cli/portability-policy.mjs +5 -2
  71. package/cli/portability-worker.mjs +15 -6
  72. package/cli/portability.mjs +30 -8
  73. package/cli/production-auth.mjs +1 -1
  74. package/cli/production-mail.mjs +8 -2
  75. package/cli/ready-summary.mjs +9 -0
  76. package/cli/run-transfers.mjs +37 -0
  77. package/cli/runtime-front-door.mjs +4 -4
  78. package/cli/server-proxy.mjs +4 -3
  79. package/cli/server-registry.mjs +13 -8
  80. package/cli/site.mjs +6 -1
  81. package/cli/transfers.mjs +17 -4
  82. package/edition-authoring/archive-source.mjs +59 -0
  83. package/edition-authoring/base-copies.mjs +147 -25
  84. package/edition-authoring/cli.mjs +1 -1
  85. package/edition-authoring/edition-migrations.mjs +7 -3
  86. package/edition-authoring/module-closure.mjs +2 -16
  87. package/edition-authoring/workflow-copies.mjs +1 -1
  88. package/edition-authoring/workflow-scripts.mjs +12 -3
  89. package/foundation/runtime/contract-docs.mjs +218 -34
  90. package/foundation/runtime/contract-prose-patterns.mjs +14 -0
  91. package/foundation/runtime/contract-prose.mjs +88 -0
  92. package/foundation/runtime/run-data.d.mts +1 -0
  93. package/foundation/runtime/run-data.mjs +2 -0
  94. package/global/CHANGELOG.md +126 -0
  95. package/global/README.md +156 -50
  96. package/global/cli/command.mjs +8 -2
  97. package/global/cli/install.mjs +4 -5
  98. package/global/cli/path-link.mjs +3 -2
  99. package/global/cli/repo-command.mjs +3 -7
  100. package/global/door/command.mjs +3 -1
  101. package/global/launch/dev.mjs +12 -2
  102. package/global/launch/facts.mjs +2 -2
  103. package/global/launch/render.mjs +16 -9
  104. package/global/launch/state.mjs +4 -4
  105. package/global/main.mjs +13 -8
  106. package/install.sh +17 -6
  107. package/modules/agent-door/CLAUSES.md +215 -0
  108. package/modules/agent-door/conformance/audit.mjs +66 -0
  109. package/modules/agent-door/conformance/authority.d.mts +2 -0
  110. package/modules/agent-door/conformance/authority.mjs +17 -0
  111. package/modules/agent-door/conformance/crypto.mjs +36 -0
  112. package/modules/agent-door/conformance/discovery.mjs +61 -0
  113. package/modules/agent-door/conformance/index.mjs +41 -0
  114. package/modules/agent-door/conformance/invocation.mjs +32 -0
  115. package/modules/agent-door/conformance/journey.mjs +112 -0
  116. package/modules/agent-door/conformance/narrowing.d.mts +1 -0
  117. package/modules/agent-door/conformance/narrowing.mjs +21 -0
  118. package/modules/agent-door/conformance/negative.mjs +47 -0
  119. package/modules/agent-door/conformance/network-modules.mjs +86 -0
  120. package/modules/agent-door/conformance/network-runtime.mjs +46 -0
  121. package/modules/agent-door/conformance/policy-phases.mjs +75 -0
  122. package/modules/agent-door/conformance/protocol.mjs +145 -0
  123. package/modules/agent-door/conformance/replay.mjs +38 -0
  124. package/modules/agent-door/conformance/schema.mjs +71 -0
  125. package/modules/agent-door/conformance/security.mjs +33 -0
  126. package/modules/agent-door/conformance/sponsors.mjs +68 -0
  127. package/modules/agent-door/conformance/surface.mjs +89 -0
  128. package/modules/agent-door/conformance/target.mjs +18 -0
  129. package/modules/agent-door/conformance/tool-name.d.mts +1 -0
  130. package/modules/agent-door/conformance/tool-name.mjs +7 -0
  131. package/modules/agent-door/conformance/webhooks.mjs +30 -0
  132. package/modules/container/README.md +6 -1
  133. package/modules/container/adapters/d1-limits.mjs +286 -0
  134. package/modules/container/adapters/d1-sqlite.mjs +95 -0
  135. package/modules/container/adapters/d1-workerd-preload.mjs +19 -0
  136. package/modules/container/adapters/d1-workerd-setup.mjs +26 -0
  137. package/modules/container/adapters/d1-workerd.mjs +5 -0
  138. package/modules/container/adapters/sqlite-worker.mjs +5 -3
  139. package/modules/container/build.mjs +1 -0
  140. package/modules/container/image-variables.mjs +14 -3
  141. package/modules/container/transfer.mjs +14 -4
  142. package/modules/container/worker-engine.mjs +3 -3
  143. package/modules/demo-host/README.md +6 -0
  144. package/modules/demo-host/build.mjs +1 -0
  145. package/modules/demo-host/native/prepare.mjs +2 -1
  146. package/modules/demo-host/native/sql.mjs +7 -2
  147. package/modules/demo-host/ownership.mjs +39 -0
  148. package/modules/demo-host/plan.mjs +2 -1
  149. package/modules/demo-host/remove.mjs +6 -1
  150. package/modules/deploy-kit/README.md +22 -2
  151. package/modules/deploy-kit/d1-import-diagnostics.mjs +56 -0
  152. package/modules/deploy-kit/d1-import.mjs +17 -5
  153. package/modules/deploy-kit/diagnostics.mjs +29 -3
  154. package/modules/deploy-kit/owner-cli.mjs +20 -7
  155. package/modules/deploy-kit/provider-transport.mjs +19 -4
  156. package/modules/deploy-kit/provider.mjs +57 -22
  157. package/modules/deploy-kit/readiness-window.mjs +1 -1
  158. package/modules/deploy-kit/site.mjs +8 -3
  159. package/modules/deploy-kit/worker.mjs +4 -4
  160. package/modules/import-kit/schema/importer-verification-receipt.schema.json +171 -0
  161. package/modules/import-kit/schema/importers.schema.json +107 -0
  162. package/modules/import-kit/src/d1/README.md +23 -4
  163. package/modules/import-kit/src/d1/atomic-prefix.ts +2 -2
  164. package/modules/import-kit/src/d1/budget.ts +15 -7
  165. package/modules/import-kit/src/d1/column-layout.ts +5 -0
  166. package/modules/import-kit/src/d1/creation.ts +15 -8
  167. package/modules/import-kit/src/d1/database.ts +3 -3
  168. package/modules/import-kit/src/d1/erasure-pages.ts +2 -1
  169. package/modules/import-kit/src/d1/erasure.ts +2 -2
  170. package/modules/import-kit/src/d1/jobs.ts +10 -6
  171. package/modules/import-kit/src/d1/plans.ts +14 -8
  172. package/modules/import-kit/src/d1/retirement.ts +2 -2
  173. package/modules/import-kit/src/d1/sets.ts +1 -3
  174. package/modules/import-kit/src/d1/staged-creation.ts +3 -3
  175. package/modules/import-kit/src/door/ADOPTING.md +20 -0
  176. package/modules/import-kit/src/door/README.md +2 -1
  177. package/modules/import-kit/src/door/admission.ts +5 -3
  178. package/modules/import-kit/src/door/archive.ts +5 -2
  179. package/modules/import-kit/src/door/authority.ts +1 -0
  180. package/modules/import-kit/src/door/grants.ts +1 -1
  181. package/modules/import-kit/src/door/handler.ts +14 -8
  182. package/modules/import-kit/src/door/index.ts +1 -1
  183. package/modules/import-kit/src/door/jobs.ts +40 -0
  184. package/modules/import-kit/src/door/limits.ts +1 -1
  185. package/modules/import-kit/src/door/operator.ts +3 -3
  186. package/modules/import-kit/src/door/planning.ts +10 -6
  187. package/modules/import-kit/src/door/protocol.ts +2 -2
  188. package/modules/import-kit/src/door/raw.ts +25 -0
  189. package/modules/import-kit/src/door/refusals.ts +2 -1
  190. package/modules/import-kit/src/door/types.ts +26 -6
  191. package/modules/import-kit/src/receipt/encoding.ts +37 -0
  192. package/modules/import-kit/src/receipt/index.ts +69 -0
  193. package/modules/import-kit/src/receipt/policy.ts +24 -0
  194. package/modules/import-kit/src/receipt/types.ts +64 -0
  195. package/modules/import-kit/src/receipt/uri.ts +45 -0
  196. package/modules/import-kit/src/receipt/validation.ts +87 -0
  197. package/modules/import-kit/src/status/encoding.ts +13 -0
  198. package/modules/import-kit/src/status/evidence.ts +218 -0
  199. package/modules/import-kit/src/status/index.ts +56 -0
  200. package/modules/import-kit/src/status/schema.ts +208 -0
  201. package/modules/import-kit/src/status/types.ts +86 -0
  202. package/modules/operator-auth/accounts/delegated.mjs +48 -0
  203. package/modules/operator-auth/index.d.mts +7 -0
  204. package/modules/operator-auth/service.mjs +2 -0
  205. package/modules/portability/README.md +138 -9
  206. package/modules/portability/backfills.mjs +9 -8
  207. package/modules/portability/bridge.mjs +32 -5
  208. package/modules/portability/catalogue.mjs +12 -5
  209. package/modules/portability/copies.mjs +8 -1
  210. package/modules/portability/derived.mjs +33 -0
  211. package/modules/portability/files.mjs +4 -3
  212. package/modules/portability/http-errors.mjs +1 -1
  213. package/modules/portability/http.mjs +3 -3
  214. package/modules/portability/import.mjs +29 -13
  215. package/modules/portability/index.d.mts +4 -3
  216. package/modules/portability/index.mjs +1 -1
  217. package/modules/portability/ledgers.mjs +3 -2
  218. package/modules/portability/legacy-safety.mjs +11 -2
  219. package/modules/portability/migration-history.mjs +2 -2
  220. package/modules/portability/progress.mjs +1 -0
  221. package/modules/portability/restore-empty.mjs +12 -0
  222. package/modules/release/github-verify.mjs +11 -2
  223. package/onboarding/adapter.mjs +35 -6
  224. package/onboarding/app-fetch.mjs +6 -4
  225. package/onboarding/development-runtime.mjs +19 -0
  226. package/onboarding/local/operation.mjs +45 -27
  227. package/onboarding/loopback-fetch.mjs +16 -0
  228. package/onboarding/operator-installer.mjs +2 -4
  229. package/onboarding/process-diagnostics.mjs +6 -2
  230. package/onboarding/production-runtime.mjs +4 -2
  231. package/onboarding/readiness.mjs +1 -0
  232. package/package.json +4 -2
  233. package/scripts/publish-check/copy_identities.py +3 -1
  234. package/scripts/publish-check/github_refs.py +32 -13
  235. package/scripts/publish-check/public_hygiene.py +1 -1
  236. package/cli/business/opt-ins.mjs +0 -33
@@ -1,33 +1,75 @@
1
+ import { startCarryRefresh } from './carry.mjs';
1
2
  import { randomUUID } from 'node:crypto';
2
3
  import { access, unlink } from 'node:fs/promises';
3
4
  import { join } from 'node:path';
4
5
  import { Api, taskPath, pause } from './api.mjs';
5
- import { paired } from './local-commands.mjs';
6
- import { createCredentialStore } from './keychain.mjs';
7
- import { nextQuestions } from './questions.mjs';
8
- import { lockSupervisor, stateDirectory, credentialHandle } from './state.mjs';
6
+ import { paired, revokedByServer, revokedText } from './local-commands.mjs';
7
+ import { nextQuestions, ownerLink, waitingFor, agentWaiting, ownerWaiting } from './questions.mjs';
8
+ import { lockSupervisor, stateDirectory, saveState, loadState } from './state.mjs';
9
9
  import { loadProgress, saveProgress } from './progress.mjs';
10
10
  import { Lease } from './lease.mjs';
11
11
  import { runDiscovery } from './discovery-task.mjs';
12
+ import { applyChoices } from './choices.mjs';
13
+ import { businessCommand } from './invocation.mjs';
14
+ import { ExportWaiting, vendorHandle } from './vendor-door.mjs';
15
+ import { runExport } from './export-task.mjs';
16
+ import { runCatchup } from './catchup-task.mjs';
17
+ import { runArchive } from './archive-task.mjs';
12
18
 
13
- const kinds = new Set(['discover.local', 'export.vendor']);
14
- const unavailableReasons = new Set(['ambiguous-target-installation', 'no-linked-installation', 'installation-import-origin-unavailable', 'installation-owner-claim-unconfigured', 'connected-seed-import-required']);
19
+ // The vendor tasks this computer performs into an app's import door.
20
+ const vendorWork = { 'export.vendor': runExport, 'catchup.vendor': runCatchup, 'switchoff.archive': runArchive };
21
+ const kinds = new Set(['discover.local', ...Object.keys(vendorWork)]);
22
+ export function supportsTaskKind(kind) { return kinds.has(kind); }
23
+ const checkIn = 25_000;
15
24
 
25
+ /** Work this computer's tasks. It says what it is doing and what it waits
26
+ * for, applies the owner's choices at every check-in, and removes run.lock on
27
+ * every exit, including Ctrl-C, SIGTERM and a closed terminal (SIGHUP). */
16
28
  export async function run({ once = false } = {}) {
17
29
  const { state, credential } = await paired(), api = new Api(state.origin, credential), directory = stateDirectory();
18
- const parked = new Set(), unlock = await lockSupervisor(), stop = new AbortController();
19
- const interrupted = () => stop.abort(Error('Business supervisor stopped.'));
20
- process.once('SIGTERM', interrupted); process.once('SIGINT', interrupted);
21
- const monitor = setInterval(() => { access(join(directory, 'disconnect')).then(interrupted, () => {}); }, 250);
22
- let progress;
30
+ // Tasks this run handed back as paused, with what each waits for. An export
31
+ // waiting only for the owner's vendor key is picked up again once it is stored.
32
+ const parked = new Map(), unlock = await lockSupervisor(), stop = new AbortController();
33
+ // A terminal's Ctrl-C reaches npx and this process together, and npx then
34
+ // forwards another SIGINT, which can arrive at any moment until this process
35
+ // has exited. So the handlers stay for the rest of the process: a repeated
36
+ // signal finds them instead of Node's default action, which would end the
37
+ // process before it hands its lease back, removes run.lock or says it stopped.
38
+ // Each one removes run.lock (again, harmlessly); the first one stops the run.
39
+ const interrupted = signal => () => { unlock.now(); stop.abort(Object.assign(Error('Business supervisor stopped.'), { signal })); };
40
+ for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP']) process.on(signal, interrupted(signal));
41
+ const monitor = setInterval(() => { access(join(directory, 'disconnect')).then(() => stop.abort(Error('Business supervisor stopped for disconnect.')), () => {}); }, 250);
42
+ let progress, stopCarry, said = '', held = null, summaries = [];
43
+ const say = text => { if (text !== said) { said = text; process.stdout.write(text + '\n'); } };
44
+ // An agent step that waits is said in its own words, never as "Nothing yet".
45
+ const waits = () => [...new Set([...summaries.filter(task => parked.has(task.task_id)).map(task => parked.get(task.task_id).message), ...agentWaiting(summaries)])];
46
+ // With no agent step waiting, a move's owner step (freeze, rollback, cancellation) is named with its link.
47
+ const idle = () => { const owner = ownerWaiting(summaries, state); return owner.length ? owner.join('\n') : waitingFor(state); };
23
48
  try {
49
+ stopCarry = await startCarryRefresh(api, state, stop.signal);
24
50
  progress = await loadProgress();
51
+ // A claim saved before this computer stopped is the task it was doing.
52
+ held = progress.pendingClaim?.task_id ?? null;
25
53
  await unlink(join(directory, 'disconnect')).catch(error => { if (error.code !== 'ENOENT') throw error; });
54
+ if (!once) process.stdout.write(`Business run is working this computer's tasks for ${ownerLink(state)}. It checks in every ${checkIn / 1000} seconds and keeps going until you stop it (Ctrl-C).\n`);
26
55
  do {
56
+ const { serviceInstalled } = await applyChoices(api, state);
57
+ if (serviceInstalled && !once) {
58
+ process.stdout.write('The background service now works this computer\'s tasks, so this run stops here.\n');
59
+ return;
60
+ }
27
61
  if (!progress.pendingClaim) {
28
- const summaries = await nextQuestions(api, stop.signal);
62
+ summaries = await nextQuestions(api, stop.signal);
63
+ const handles = (await loadState())?.vendorHandles ?? [];
64
+ for (const [id, wait] of parked) if (wait.keyMissing && handles.includes(vendorHandle(wait.keyMissing))) parked.delete(id);
29
65
  const selected = summaries.find(task => kinds.has(task.kind) && ['ready','leased'].includes(task.state) && !parked.has(task.task_id));
30
- if (!selected) { if (once) { process.stdout.write('Nothing yet.\n'); return; } await pause(25_000, stop.signal); continue; }
66
+ if (!selected) {
67
+ const waiting = waits();
68
+ say(waiting.length ? waiting.join('\n') : idle());
69
+ if (once) { if (!waiting.length) process.stdout.write('Nothing yet.\n'); return; }
70
+ await pause(checkIn, stop.signal); continue;
71
+ }
72
+ held = selected.state === 'leased' ? selected.task_id : null;
31
73
  progress.pendingClaim = { claim_id: randomUUID(), task_id: selected.task_id };
32
74
  await saveProgress(progress);
33
75
  }
@@ -37,11 +79,21 @@ export async function run({ once = false } = {}) {
37
79
  if (error.status !== 409) throw error;
38
80
  progress = {}; await saveProgress(progress);
39
81
  if (once) throw error;
40
- await pause(25_000, stop.signal); continue;
82
+ await pause(checkIn, stop.signal); continue;
41
83
  }
42
84
  if (!claimed.task || claimed.task.state !== 'leased' || claimed.task.fence !== claimed.fence || Date.parse(claimed.lease_expires_at) <= Date.now()) {
85
+ const interrupted = held === progress.pendingClaim?.task_id;
43
86
  progress = {}; await saveProgress(progress);
44
- if (once && !claimed.task) { process.stdout.write('Nothing yet.\n'); return; }
87
+ // A task this computer held before it was stopped stays leased until the
88
+ // lease ends; then the server returns it to ready with its checkpoint.
89
+ if (!claimed.task && interrupted) say('Waiting for the server to hand back the task this computer was doing when it stopped; its earlier lease has not ended yet.');
90
+ if (once && !claimed.task) {
91
+ // Only with no agent step waiting does run say "Nothing yet".
92
+ const waiting = interrupted ? [] : waits();
93
+ if (!interrupted) say(waiting.length ? waiting.join('\n') : idle());
94
+ if (!interrupted && !waiting.length) process.stdout.write('Nothing yet.\n');
95
+ return;
96
+ }
45
97
  continue;
46
98
  }
47
99
  if (!kinds.has(claimed.task.kind) || claimed.task.kind_version !== 1) throw Error('Business server returned an unimplemented task kind.');
@@ -50,22 +102,32 @@ export async function run({ once = false } = {}) {
50
102
  try {
51
103
  progress.current = { kind: claimed.task.kind, checkpoint: claimed.task.checkpoint, uploadedBytes: claimed.task.checkpoint?.bytes_done ?? 0 };
52
104
  await saveProgress(progress);
53
- if (claimed.task.kind === 'export.vendor') {
54
- // A destination is not upload authority. Keep vendor keys local until
55
- // the seed's signed import grant and chunk transport are connected.
56
- await lease.send(taskPath(claimed.task.id, 'events'), { event: { kind: 'question', payload: { question: 'connected_runner_required' } } });
57
- parked.add(claimed.task.id);
58
- if (claimed.grant.available === false && claimed.grant.destination === `unavailable:${claimed.grant.reason}`) {
59
- const reason = unavailableReasons.has(claimed.grant.reason) ? claimed.grant.reason : 'connected-seed-import-required';
60
- throw Error(`Business vendor export unavailable: ${reason}. Task released as paused.`);
105
+ if (Object.hasOwn(vendorWork, claimed.task.kind)) {
106
+ try {
107
+ const done = await vendorWork[claimed.task.kind]({ lease, task: claimed.task, grant: claimed.grant, directory, say, ownerPage: ownerLink(state) });
108
+ finished = true;
109
+ say(done);
110
+ } catch (error) {
111
+ if (!(error instanceof ExportWaiting) || lease.signal.aborted) throw error;
112
+ // Business shows the owner what this vendor task waits for; the lease goes back as paused.
113
+ parked.set(claimed.task.id, { message: error.message, keyMissing: error.keyMissing });
114
+ await lease.send(taskPath(claimed.task.id, 'events'), { event: { kind: 'question', payload: { question: error.question } } });
115
+ throw error;
61
116
  }
62
- throw Error('Business vendor export needs a connected seed upload grant and execution-input contract.');
117
+ } else {
118
+ const page = claimed.task.checkpoint?.page;
119
+ say(page ? `Resuming the names-only scan of this computer's allowed folders from page ${page + 1}.` : 'Scanning this computer\'s allowed folders for tool names (names only; no file contents leave this computer).');
120
+ const result = await runDiscovery({ task: claimed.task, grant: claimed.grant, checkpoint: claimed.task.checkpoint, signal: lease.signal,
121
+ send: (destination, body) => lease.send(destination, body), stateDir: directory,
122
+ saveCheckpoint: async checkpoint => {
123
+ await lease.checkpointSaved(checkpoint);
124
+ if (checkpoint.page) process.stdout.write(`Sent page ${checkpoint.page} of ${checkpoint.total}; the server has ${checkpoint.records_done} names and saved this checkpoint.\n`);
125
+ } });
126
+ await lease.complete(); finished = true;
127
+ await result.cleanup();
128
+ if (!state.discovered) { state.discovered = true; await saveState(state); }
129
+ say('Business discover.local completed and checked by the server.');
63
130
  }
64
- const result = await runDiscovery({ task: claimed.task, grant: claimed.grant, checkpoint: claimed.task.checkpoint, signal: lease.signal,
65
- send: (destination, body) => lease.send(destination, body), saveCheckpoint: checkpoint => lease.checkpointSaved(checkpoint), stateDir: directory });
66
- await lease.complete(); finished = true;
67
- await result.cleanup();
68
- process.stdout.write('Business discover.local completed and checked by the server.\n');
69
131
  } catch (error) { failure = lease.signal.aborted ? lease.signal.reason : error; }
70
132
  finally {
71
133
  if (!finished && failure?.status !== 401) {
@@ -83,10 +145,13 @@ export async function run({ once = false } = {}) {
83
145
  } while (!stop.signal.aborted);
84
146
  } catch (error) {
85
147
  if (error.status === 401) {
86
- await createCredentialStore().delete(credentialHandle(state.origin));
87
- process.stderr.write('Business pairing expired or was revoked. Supervisor stopped.\n');
148
+ const revoked = await revokedByServer(state);
149
+ process.stderr.write(`Business pairing expired or was revoked, so this supervisor stopped. This computer was ${revokedText(revoked)}\n`);
88
150
  } else if (!stop.signal.aborted) throw error;
89
151
  } finally {
90
- clearInterval(monitor); process.off('SIGTERM', interrupted); process.off('SIGINT', interrupted); await unlock();
152
+ await stopCarry?.();
153
+ clearInterval(monitor);
154
+ await unlock();
155
+ if (stop.signal.reason?.signal) process.stdout.write(`Business supervisor stopped (${stop.signal.reason.signal}); run.lock removed. Run ${businessCommand('run')} again to continue from the saved checkpoint.\n`);
91
156
  }
92
157
  }
@@ -0,0 +1,230 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { constants } from 'node:fs';
3
+ import { open, unlink } from 'node:fs/promises';
4
+ import { join } from 'node:path';
5
+ import { taskPath } from './api.mjs';
6
+ import { assertChunk } from './outbound.mjs';
7
+ import { createCredentialStore } from './keychain.mjs';
8
+ import { exportVendor, VENDORS, KEYED_VENDORS, VENDOR_CAPABILITIES, VendorExportError } from './vendors/index.mjs';
9
+ import { exportInputs, inputNeeds } from './export-inputs.mjs';
10
+ import { businessCommand } from './invocation.mjs';
11
+
12
+ // What every vendor task that writes into an app's import door shares
13
+ // (modules/import-kit/src/door): the claimed grant's door, one RER-signed grant
14
+ // per door request, the owner's read-only key from OS credential storage, the
15
+ // shipped adapter into a private 0600 bundle, and 64 KiB chunks.
16
+
17
+ // The door's upload unit ("Requests"): every chunk is exactly 64 KiB except the last.
18
+ const CHUNK_BYTES = 65_536;
19
+ const DOOR_RESPONSE_BYTES = 4 * 1024 * 1024;
20
+ // The catalogue names the platform builds vendor task titles from.
21
+ export const vendorNames = Object.freeze({ sentry: 'Sentry', uptimerobot: 'UptimeRobot', pagerduty: 'PagerDuty', calendly: 'Calendly', sorry: 'Sorry' });
22
+ /** Whether a vendor's export needs the owner's read-only key (Sorry's public page does not). */
23
+ export const keyed = vendor => KEYED_VENDORS.includes(vendor);
24
+ export const vendorHandle = vendor => `vendor:${vendor}:read`;
25
+ export const hash = bytes => createHash('sha256').update(bytes).digest('hex');
26
+ const loopback = hostname => ['localhost', '127.0.0.1', '[::1]'].includes(hostname);
27
+ // The door identifiers Business accepts in a task's outputs (platform business/imports.ts).
28
+ export const DOOR_ID = /^[0-9A-Za-z][0-9A-Za-z_-]{0,127}$/;
29
+ export const count = value => Number.isSafeInteger(value) && value >= 0 ? value : null;
30
+ export const plural = (value, word) => `${value} ${word}${value === 1 ? '' : 's'}`;
31
+
32
+ /** A vendor task that cannot go on until someone else acts. The supervisor tells
33
+ * Business with `question`, hands the lease back as paused and says `message`. */
34
+ export class ExportWaiting extends Error {
35
+ constructor(message, question, { keyMissing = null } = {}) { super(message); this.question = question; this.keyMissing = keyMissing; }
36
+ }
37
+
38
+ // The platform's task titles (business/task-titles.ts) for the kinds that name a vendor.
39
+ const titles = {
40
+ 'export.vendor': name => `Export ${name} history`,
41
+ 'catchup.vendor': name => `Bring over recent ${name} changes`,
42
+ 'switchoff.archive': name => `Archive ${name} before switching it off`,
43
+ 'freeze.vendor': name => `Freeze ${name} before the switch`,
44
+ 'switchoff.cancel': name => `Cancel ${name}`,
45
+ };
46
+ /** The shipped vendor a task names, read from its catalogue title
47
+ * (`Export UptimeRobot history`), or null. Run trusts only the grant's namespace. */
48
+ export function titledVendor(kind, title) {
49
+ if (!Object.hasOwn(titles, kind)) return null;
50
+ return VENDORS.find(vendor => title === titles[kind](vendorNames[vendor])) ?? null;
51
+ }
52
+
53
+ const keyCommand = vendor => businessCommand(`vendor-key add ${vendor ?? '<vendor>'}`);
54
+ /** The owner-typed step that keeps a vendor's read-only key on this computer. */
55
+ export function keyStep(vendor) {
56
+ return `ask the owner to type this in their own terminal, outside any agent sandbox (it asks for the key with hidden input; never put a key in chat, a command or a file):\n ${keyCommand(vendor)}`;
57
+ }
58
+
59
+ const unavailableWords = {
60
+ 'vendor-not-in-catalogue': 'this tool has no shipped export',
61
+ 'no-linked-installation': 'Business has not set up the app this tool moves into',
62
+ 'ambiguous-target-installation': 'more than one app could receive this export',
63
+ 'installation-import-origin-unavailable': "the app's address is not known yet",
64
+ 'owner-claim-keys-unconfigured': 'Business cannot sign import grants yet',
65
+ 'import-door-off': "the app's import door is off",
66
+ 'installation-owner-claim-unconfigured': "the app's import door does not trust Business yet",
67
+ 'door-trust-keys-stale': "the app's import door holds old Business keys",
68
+ 'owner-not-claimed': 'the owner has not claimed the app yet',
69
+ 'connected-seed-import-required': "the app's import door is not ready",
70
+ };
71
+
72
+ /** The claimed grant's door: its origin and byte quota. */
73
+ function destination(task, grant) {
74
+ if (grant.available === false || typeof grant.destination === 'string' && grant.destination.startsWith('unavailable:')) {
75
+ const reason = Object.hasOwn(unavailableWords, grant.reason) && grant.destination === `unavailable:${grant.reason}` ? grant.reason : 'connected-seed-import-required';
76
+ throw new ExportWaiting(`Business vendor export unavailable: ${reason} (${unavailableWords[reason]}). Task released as paused.`, 'connected_runner_required');
77
+ }
78
+ let url;
79
+ try { url = new URL(grant.destination); } catch { throw Error('Business returned an invalid import destination.'); }
80
+ if (!(url.protocol === 'https:' || url.protocol === 'http:' && loopback(url.hostname)) || url.username || url.password || url.search || url.hash
81
+ || url.pathname !== '/api/rer/import/uploads' || grant.import_grants !== taskPath(task.id, 'import-grants')
82
+ || !Number.isSafeInteger(grant.quota_bytes) || grant.quota_bytes < 1 || grant.quota_bytes > 300_000_000) throw Error('Business returned an invalid import destination.');
83
+ return { origin: url.origin, quota: grant.quota_bytes };
84
+ }
85
+
86
+ /** Inputs the shipped adapter needs beyond the key: the leased task's own
87
+ * non-secret `export_inputs`. A task that does not carry them yet waits for
88
+ * Business (which asks the owner when its evidence is missing). */
89
+ function adapterInputs(vendor, task) {
90
+ const inputs = exportInputs(vendor, task);
91
+ if (inputs) return inputs;
92
+ const needs = vendor === 'calendly' ? 'an import door that takes its folder of files' : `${inputNeeds[vendor]}, which this task does not carry yet`;
93
+ throw new ExportWaiting(`Business vendor export for ${vendorNames[vendor]} needs ${needs}. Task released as paused.`, 'configuration_required');
94
+ }
95
+
96
+ /** One RER-signed import grant for one door request. `review` asks for a
97
+ * catch-up plan grant without the owner's standing approval; a plan grant
98
+ * Business minted under that approval says so with `catchup: 'bounded'`. */
99
+ async function doorGrant(lease, action, origin, vendor, { review = false } = {}) {
100
+ const grant = await lease.importGrant(action, review ? { review: true } : {});
101
+ if (!grant || grant.action !== action || grant.destination !== new URL(`/api/rer/import/${action}`, origin).href
102
+ || typeof grant.authorization !== 'string' || grant.authorization.length > 8192 || !/^Bearer [A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(grant.authorization)
103
+ || !VENDORS.includes(grant.namespace) || vendor && grant.namespace !== vendor || !(Date.parse(grant.expires_at) > Date.now())
104
+ || Object.hasOwn(grant, 'catchup') && (grant.catchup !== 'bounded' || action !== 'plan' || review)) throw Error('Business returned an invalid import grant.');
105
+ return grant;
106
+ }
107
+
108
+ /** POST one request to the app's door. Only the door's error code is ever
109
+ * shown: its bodies can hold vendor records. */
110
+ export async function door(grant, body, signal) {
111
+ const action = grant.action;
112
+ let response;
113
+ try {
114
+ response = await fetch(grant.destination, { method: 'POST', redirect: 'error', body: JSON.stringify(body),
115
+ headers: { authorization: grant.authorization, 'content-type': 'application/json' }, signal: AbortSignal.any([signal, AbortSignal.timeout(30_000)]) });
116
+ } catch (error) {
117
+ if (signal.aborted) throw signal.reason;
118
+ const code = error?.cause?.code ?? error?.code;
119
+ throw Error(`Could not reach the app's import door at ${new URL(grant.destination).origin} (${error?.name === 'TimeoutError' ? 'no answer within 30 seconds' : typeof code === 'string' && /^[A-Z_]{2,40}$/.test(code) ? code : 'the request failed'}).`);
120
+ }
121
+ const chunks = []; let size = 0;
122
+ for await (const chunk of response.body ?? []) {
123
+ size += chunk.byteLength;
124
+ if (size > DOOR_RESPONSE_BYTES) { await response.body.cancel().catch(() => {}); throw Error(`The app's import door answered the ${action} request with more than the CLI reads.`); }
125
+ chunks.push(chunk);
126
+ }
127
+ let result = null;
128
+ try { result = JSON.parse(Buffer.concat(chunks).toString('utf8')); } catch { /* Judged below. */ }
129
+ if (!response.ok) {
130
+ // A door code is snake_case, sometimes with one named reason (`catchup_requires_proposal:source_records_gone`).
131
+ const doorCode = typeof result?.error === 'string' && /^[a-z][a-z0-9_]{0,79}(?::[a-z][a-z0-9_]{0,79})?$/.test(result.error) ? result.error : null;
132
+ throw Object.assign(Error(`The app's import door refused the ${action} request (HTTP ${response.status}${doorCode ? `: ${doorCode}` : ''}).`), { doorStatus: response.status, doorCode });
133
+ }
134
+ if (!result || typeof result !== 'object' || Array.isArray(result)) throw Error(`The app's import door returned an invalid ${action} answer.`);
135
+ return result;
136
+ }
137
+
138
+ /** Run the shipped adapter into a private 0600 bundle file, checking every
139
+ * item with the outbound detector before it is written. */
140
+ async function exportBundle({ vendor, key = null, inputs, path, quota, signal }) {
141
+ await unlink(path).catch(error => { if (error.code !== 'ENOENT') throw error; });
142
+ const file = await open(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
143
+ const digest = createHash('sha256'); let size = 0;
144
+ try {
145
+ for await (const item of exportVendor(vendor, { key, inputs, signal })) {
146
+ assertChunk(item.bytes, key ? [key] : []);
147
+ size += item.bytes.length;
148
+ if (size > quota) throw Error(`Business vendor export is larger than the app's import door accepts (${quota} bytes). Nothing was uploaded.`);
149
+ digest.update(item.bytes);
150
+ await file.write(item.bytes);
151
+ }
152
+ await file.sync();
153
+ } catch (error) {
154
+ if (!(error instanceof VendorExportError)) throw error;
155
+ if (error.code === 'HTTP_REFUSED' && [401, 403].includes(error.status) && key) {
156
+ throw new ExportWaiting(`${vendorNames[vendor]} refused the stored read-only key (HTTP ${error.status}). To replace it, ${keyStep(vendor)}`, 'owner_required');
157
+ }
158
+ throw Error(`${vendorNames[vendor]} export stopped (${error.code}${error.status ? `, HTTP ${error.status}` : ''}). Nothing was uploaded.`);
159
+ } finally { await file.close(); }
160
+ if (!size) throw Error(`${vendorNames[vendor]} export produced nothing to upload.`);
161
+ return { size, sha256: digest.digest('hex') };
162
+ }
163
+
164
+ async function readChunk(path, index, size) {
165
+ const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
166
+ try {
167
+ const length = Math.min(CHUNK_BYTES, size - index * CHUNK_BYTES), bytes = Buffer.alloc(length);
168
+ const { bytesRead } = await file.read(bytes, 0, length, index * CHUNK_BYTES);
169
+ if (bytesRead !== length) throw Error('Business vendor export bundle changed while uploading.');
170
+ return bytes;
171
+ } finally { await file.close(); }
172
+ }
173
+
174
+ /** A door plan's preview of exactly this bundle: its ids and the door's counts. */
175
+ export function checkedPreview(preview, bundle) {
176
+ const summary = preview.summary ?? {}, [records, created, updated, unchanged, gone] = ['sourceRecords', 'created', 'updated', 'unchanged', 'gone'].map(name => count(summary[name]));
177
+ const rejected = count(preview.counts?.rejected);
178
+ if (typeof preview.planId !== 'string' || !DOOR_ID.test(preview.planId) || !/^[a-f0-9]{64}$/.test(preview.fingerprint ?? '')
179
+ || preview.bundleSha256 !== bundle.sha256 || [records, created, updated, unchanged, gone, rejected].includes(null)) throw Error("The app's import door returned an invalid preview.");
180
+ return { records, created, updated, unchanged, gone, rejected };
181
+ }
182
+
183
+ /**
184
+ * The shared start of a claimed vendor door task: the claimed grant's door, the
185
+ * vendor its first grant names, the owner's key from the OS store, the shipped
186
+ * adapter into a private 0600 bundle, then the bundle in 64 KiB chunks, each with
187
+ * a fresh grant. A vendor that needs no key (a public status page) reads none.
188
+ * `finish` gets the uploaded bundle and returns `{outputs, said}`:
189
+ * the task then completes with those outputs (identifiers only, which Business
190
+ * checks against the door itself) and `said` is the line to say. The bundle file
191
+ * is removed on every exit. `doing(name, origin, keyed)` is said first.
192
+ */
193
+ export async function withUploadedBundle({ lease, task, grant: claimedGrant, directory, say, doing }, finish) {
194
+ const signal = lease.signal, { origin, quota } = destination(task, claimedGrant);
195
+ // The first grant names the vendor (its namespace); it then carries chunk 0.
196
+ let spare = await doorGrant(lease, 'uploads', origin, null);
197
+ const vendor = spare.namespace, name = vendorNames[vendor], capability = VENDOR_CAPABILITIES[vendor];
198
+ const fresh = async (action, options) => {
199
+ if (action === 'uploads' && spare && Date.parse(spare.expires_at) - Date.now() > 60_000) { const grant = spare; spare = null; return grant; }
200
+ spare = null;
201
+ return doorGrant(lease, action, origin, vendor, options);
202
+ };
203
+ const inputs = adapterInputs(vendor, task);
204
+ let key = keyed(vendor) ? await createCredentialStore().get(vendorHandle(vendor)) : null;
205
+ if (keyed(vendor) && !key) throw new ExportWaiting(`Business vendor export needs the owner's read-only ${name} key on this computer: ${keyStep(vendor)}\nThen run ${businessCommand('run')} again.`, 'owner_required', { keyMissing: vendor });
206
+ const path = join(directory, `export-${hash(task.id).slice(0, 32)}.bundle`);
207
+ try {
208
+ say(doing(name, origin, keyed(vendor)));
209
+ let bundle;
210
+ try { bundle = await exportBundle({ vendor, key, inputs, path, quota, signal }); } finally { key = undefined; }
211
+ const total = Math.ceil(bundle.size / CHUNK_BYTES);
212
+ let uploadId;
213
+ for (let index = 0; index < total; index++) {
214
+ signal.throwIfAborted();
215
+ const bytes = await readChunk(path, index, bundle.size);
216
+ const result = await door(await fresh('uploads'), { ...(uploadId ? { uploadId } : {}), name: capability.output, bundleSha256: bundle.sha256,
217
+ totalBytes: bundle.size, totalChunks: total, index, chunkSha256: hash(bytes), base64: bytes.toString('base64') }, signal);
218
+ if (typeof result.uploadId !== 'string' || !DOOR_ID.test(result.uploadId) || uploadId && result.uploadId !== uploadId
219
+ || result.bundleSha256 !== bundle.sha256 || index === total - 1 && result.complete !== true) throw Error("The app's import door returned an invalid upload answer.");
220
+ uploadId = result.uploadId;
221
+ await lease.checkpointSaved({ page: index + 1, total, bytes_done: Math.min((index + 1) * CHUNK_BYTES, bundle.size), progress: Math.floor((index + 1) / total * 90) });
222
+ }
223
+ const { outputs, said } = await finish({ vendor, name, origin, uploadId, bundle, fresh });
224
+ await lease.checkpointSaved({ page: total, total, bytes_done: bundle.size, progress: 100 });
225
+ await lease.complete(outputs);
226
+ return said;
227
+ } finally {
228
+ await unlink(path).catch(error => { if (error.code !== 'ENOENT') throw error; });
229
+ }
230
+ }
@@ -15,12 +15,15 @@ with their own supported readers and native adapters.
15
15
 
16
16
  `exportVendor(vendor, {key, inputs, checkpoint, signal})` is an async generator.
17
17
  It yields `{name, bytes: Buffer, checkpoint}`. Keys remain in memory and are
18
- attached only to GET requests on the adapter's fixed HTTPS origin. Redirects,
18
+ attached only to GET requests on the adapter's fixed HTTPS origin. Sorry reads a
19
+ public status page and takes no key (`key` must be absent); `KEYED_VENDORS` lists
20
+ the vendors that need one. Redirects,
19
21
  caller origins, arbitrary paths and credential-bearing pagination are refused.
20
22
  No export runs merely by importing these modules. No account export was run
21
23
  during implementation.
22
24
 
23
- `VENDORS` is the closed vendor enum. `VENDOR_CAPABILITIES` describes the adapter,
25
+ `VENDORS` is the closed vendor enum (`sentry`, `uptimerobot`, `pagerduty`,
26
+ `calendly`, `sorry`). `VENDOR_CAPABILITIES` describes the adapter,
24
27
  required vendor-key capability, native importer format, layout and read scopes.
25
28
  These adapters cannot verify that a supplied key itself has read-only scope;
26
29
  the owner must create the documented read-only key where the vendor offers one.
@@ -51,12 +54,19 @@ import. Never treat a partial stream as a completed bundle.
51
54
  Every item is at most 1 MiB. Exports are bounded to 1,000 requests, one request
52
55
  per second, 30 seconds per request, 2 MiB/499 API records for Sentry,
53
56
  512 KiB/500 records for PagerDuty, and 8 MiB/2,500 records for UptimeRobot and
54
- Calendly. The output bound includes JSON framing and Calendly's manifest.
57
+ Calendly. Sorry follows Lamp's reader: at most 200 requests (one saved file
58
+ each), one every 125 ms, 8 MiB of responses, 5,000 records and a 16 MiB bundle. The output bound includes JSON framing and Calendly's manifest.
55
59
  Limits stop rather than truncate. AbortSignal stops waits, HTTP reads and
56
60
  emission. HTTP errors contain only a fixed code and numeric status; network,
57
61
  parsing and unexpected errors never expose response text, URLs or key values.
58
62
  429 stops with `RATE_LIMITED`; there is no hidden retry loop.
59
63
 
64
+ The Business CLI supervisor currently captures a complete private bundle before
65
+ uploading it. After a lease handover, its next run captures and uploads the export
66
+ again under a fresh lease; it does not resume a partially uploaded bundle in
67
+ place. Only a completed upload and the door's matching plan are reported as a
68
+ preview. Calendly's folder layout remains outside this single-bundle path.
69
+
60
70
  ## Credentials in responses
61
71
 
62
72
  Every adapter strips credential values from a parsed response before it emits
@@ -110,9 +120,23 @@ pagination link is still refused.
110
120
  * `pagerduty`: `start`/`end` (or `since`/`until`). Exports users, schedule details
111
121
  with layers/final coverage, overrides for the window and escalation policies.
112
122
  Schedule listing only discovers IDs and is not duplicated in the bundle.
123
+ Oncall compares a schedule's final timeline over at most 31 days, so the
124
+ Business CLI passes a window of at most 31 whole UTC days ending two weeks
125
+ ahead.
113
126
  Overrides retain full boundaries with `overflow=true` and add `schedule_id`,
114
127
  as Oncall's documented bundle requires. Incident history and paging
115
128
  configuration are not fetched.
129
+ * `sorry`: `page: {host}`, the public status page's host name (a public DNS
130
+ name; no scheme, port, path, IP address or local name). Reads, anonymously,
131
+ `/api/v1/status`, every `components` and `notices` page (`meta.next_page`) and
132
+ each notice's detail with its components and full update history, like Lamp's
133
+ public Move in. The bundle is Lamp's door `sorry` source: `{source: 'sorry',
134
+ publicUrl, publicAccessAcknowledged: true, files: [{name, text}]}`, each file
135
+ the original response text under the name Lamp's reader requires
136
+ (`sorry-status.json`, `sorry-components[-N].json`, `sorry-notices[-N].json`,
137
+ `sorry-notice-<id>.json`). Every response came from the page's own public API,
138
+ so it is public by construction; Lamp still refuses private or
139
+ audience-scoped records, and public responses carry no subscribers.
116
140
  * `calendly`: optional `asOf` timestamp (recommended for a repeatable catch-up).
117
141
  An omitted timestamp is captured at first invocation and retained on resume.
118
142
  Downloads the current user, event types, availability schedules, per-type
@@ -129,6 +153,8 @@ their file-writing command wrappers.
129
153
 
130
154
  * [Dial downloader at 6014c99875dd002545e194ab23a7852d30e94e01](https://github.com/just-every/run-dial/blob/6014c99875dd002545e194ab23a7852d30e94e01/scripts/move-in/download.mjs).
131
155
  * [Uptime guide at 157686b52ecc652d64250a30ec224f1a47355a6c](https://github.com/just-every/run-uptime/blob/157686b52ecc652d64250a30ec224f1a47355a6c/docs/moving-from-uptimerobot.md).
156
+ * [Lamp's Sorry public reader at a0179bfe](https://github.com/just-every/run-lamp/blob/a0179bfe/src/core/features/statuspage-import/sorry/fetch.ts)
157
+ and its door source (`door-plan.ts` `doorExport`).
132
158
  * [Oncall guide at 4f928effc3ce735f3d246bb8a3c4bbb8436cae6b](https://github.com/just-every/run-oncall/blob/4f928effc3ce735f3d246bb8a3c4bbb8436cae6b/docs/move-in.md).
133
159
  * Spool's proposal-cited `app/docs/SENTRY-SWITCHING.md` and
134
160
  `app/src/domain/sentry-import/source.ts`, local source commit
@@ -162,8 +162,9 @@ export function client({ origin, headers, route, signal, limits, counters, key }
162
162
  const url = new URL(path, origin);
163
163
  if (url.origin !== origin || url.username || url.password || url.hash || !route(url)) fail('INVALID_REFERENCE');
164
164
  if (++counters.requests > limits.requests) fail('REQUEST_LIMIT');
165
- // At most one request per second; no automatic retry or rate-limit hammering.
166
- const pause = 1000 - (Date.now() - lastRequest);
165
+ // At most one request per `spacing` (a second unless the adapter's limits say
166
+ // otherwise); no automatic retry or rate-limit hammering.
167
+ const pause = (limits.spacing ?? 1000) - (Date.now() - lastRequest);
167
168
  if (pause > 0) await delay(pause, undefined, { signal });
168
169
  lastRequest = Date.now();
169
170
  const timeout = AbortSignal.timeout(30_000);
@@ -189,9 +190,10 @@ export function client({ origin, headers, route, signal, limits, counters, key }
189
190
  }
190
191
  const bytes = Buffer.concat(chunks);
191
192
  // A reflected authentication key must never become a vendor artifact.
192
- if (bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
193
- let body;
194
- try { body = JSON.parse(bytes.toString('utf8')); } catch { fail('INVALID_JSON'); }
195
- return { body, headers: response.headers };
193
+ if (key && bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
194
+ let text, body;
195
+ try { text = new TextDecoder('utf-8', { fatal: true }).decode(bytes); body = JSON.parse(text); } catch { fail('INVALID_JSON'); }
196
+ // `text` is the response exactly as received, for adapters whose bundle keeps original bytes.
197
+ return { body, text, headers: response.headers };
196
198
  };
197
199
  }
@@ -3,22 +3,31 @@ import * as sentry from './sentry.mjs';
3
3
  import * as uptimerobot from './uptimerobot.mjs';
4
4
  import * as pagerduty from './pagerduty.mjs';
5
5
  import * as calendly from './calendly.mjs';
6
+ import * as sorry from './sorry.mjs';
6
7
 
7
8
  export { VendorExportError } from './common.mjs';
8
- export const VENDORS = Object.freeze(['sentry','uptimerobot','pagerduty','calendly']);
9
- const adapters = { sentry, uptimerobot, pagerduty, calendly };
9
+ export const VENDORS = Object.freeze(['sentry','uptimerobot','pagerduty','calendly','sorry']);
10
+ const adapters = { sentry, uptimerobot, pagerduty, calendly, sorry };
10
11
  export const VENDOR_CAPABILITIES = Object.freeze({
11
12
  sentry: Object.freeze({ vendor: 'sentry', adapter: 'rer:sentry', capability: 'vendor_key:sentry', format: 'spool-sentry-org-export-v1', layout: 'concatenate', output: 'sentry-export.json', readOnly: true, resume: true, scopes: ['org:read','project:read','event:read'], origins: Object.values(sentry.origins) }),
12
13
  uptimerobot: Object.freeze({ vendor: 'uptimerobot', adapter: 'rer:uptimerobot', capability: 'vendor_key:uptimerobot', format: 'uptimerobot-v3-monitors', layout: 'concatenate', output: 'uptimerobot-monitors.json', readOnly: true, resume: true, scopes: ['read-only API key'], origins: [uptimerobot.origin] }),
13
14
  pagerduty: Object.freeze({ vendor: 'pagerduty', adapter: 'rer:pagerduty', capability: 'vendor_key:pagerduty', format: 'pagerduty-saved-responses', layout: 'concatenate', output: 'pagerduty-export.json', readOnly: true, resume: true, scopes: ['read-only REST API key'], origins: [pagerduty.origin] }),
14
15
  calendly: Object.freeze({ vendor: 'calendly', adapter: 'rer:calendly', capability: 'vendor_key:calendly', format: 'dial-calendly-api-folder-v1', layout: 'files', output: 'manifest.json', readOnly: true, resume: true, scopes: ['personal access token with read access'], origins: [calendly.origin] }),
16
+ // A public status page: read anonymously from the page's own address, so it takes no key.
17
+ sorry: Object.freeze({ vendor: 'sorry', adapter: 'rer:sorry', capability: null, format: 'lamp-sorry-public-responses', layout: 'concatenate', output: 'sorry-export.json', readOnly: true, resume: true, scopes: [], origins: ['https://<page host>'] }),
15
18
  });
19
+ /** The vendors whose export needs the owner's read-only key. */
20
+ export const KEYED_VENDORS = Object.freeze(VENDORS.filter(vendor => VENDOR_CAPABILITIES[vendor].capability !== null));
16
21
  const LIMITS = Object.freeze({
17
22
  // Spool's 500-record bound also counts its synthesized manifest record.
18
23
  sentry: { bytes: 2 * 1024 * 1024, records: 499 },
19
24
  uptimerobot: { bytes: 8 * 1024 * 1024, records: 2500 },
20
25
  pagerduty: { bytes: 512 * 1024, records: 500 },
21
26
  calendly: { bytes: 8 * 1024 * 1024, records: 2500 },
27
+ // Lamp's Sorry reader: 8 MiB of responses in at most 200 files and 5,000
28
+ // records; its door takes twice that, as the bundle carries each response as
29
+ // JSON text. Lamp reads a public page at most every 125 ms.
30
+ sorry: { bytes: 8 * 1024 * 1024, emitted: 16 * 1024 * 1024, records: 5000, requests: 200, spacing: 125 },
22
31
  });
23
32
 
24
33
  /** Persist each checkpoint only after the corresponding bytes have been acknowledged.
@@ -27,27 +36,29 @@ const LIMITS = Object.freeze({
27
36
  export async function* exportVendor(vendor, { key, inputs = {}, checkpoint, signal } = {}) {
28
37
  try {
29
38
  if (!VENDORS.includes(vendor)) fail('UNSUPPORTED_VENDOR');
30
- if (typeof key !== 'string' || !key || key.length > 8192 || /[\u0000-\u0020\u007f]/u.test(key)) fail('INVALID_KEY');
39
+ const keyed = KEYED_VENDORS.includes(vendor);
40
+ if (keyed ? typeof key !== 'string' || !key || key.length > 8192 || /[\u0000-\u0020\u007f]/u.test(key) : key != null) fail('INVALID_KEY');
31
41
  object(inputs);
32
42
  const fingerprint = hash(JSON.stringify(stable({ vendor, inputs })));
33
- const limits = { ...LIMITS[vendor], requests: 1000, pageBytes: 1024 * 1024 - 16 * 1024 };
43
+ const limits = { requests: 1000, ...LIMITS[vendor], pageBytes: 1024 * 1024 - 16 * 1024 };
34
44
  let state, sequence = 0;
35
45
  let counters = { requests: 0, received: 0, emitted: 0, records: 0 };
36
46
  if (checkpoint != null) {
37
47
  const encoded = JSON.stringify(checkpoint);
38
- if (Buffer.byteLength(encoded) >= 128 * 1024 || encoded.includes(key)) fail('INVALID_CHECKPOINT');
48
+ if (Buffer.byteLength(encoded) >= 128 * 1024 || key && encoded.includes(key)) fail('INVALID_CHECKPOINT');
39
49
  const cp = JSON.parse(encoded);
40
50
  shape(cp, ['version','vendor','fingerprint','sequence','counters','state']);
41
51
  if (cp.version !== 1 || cp.vendor !== vendor || cp.fingerprint !== fingerprint) fail('INVALID_CHECKPOINT');
42
52
  sequence = assertCount(cp.sequence, 2000);
43
53
  shape(cp.counters, ['requests','received','emitted','records']); object(cp.state);
44
- counters = Object.fromEntries(Object.keys(counters).map(name => [name, assertCount(cp.counters[name], name === 'requests' ? limits.requests : name === 'records' ? limits.records : limits.bytes)]));
54
+ counters = Object.fromEntries(Object.keys(counters).map(name => [name, assertCount(cp.counters[name], name === 'requests' ? limits.requests : name === 'records' ? limits.records : name === 'emitted' ? limits.emitted ?? limits.bytes : limits.bytes)]));
45
55
  state = cp.state;
46
56
  }
47
57
  const adapter = adapters[vendor];
48
- const origin = vendor === 'sentry' ? adapter.origins[inputs.organization?.region] : adapter.origin;
58
+ const origin = vendor === 'sentry' ? adapter.origins[inputs.organization?.region] : vendor === 'sorry' ? adapter.originOf(inputs) : adapter.origin;
49
59
  if (!origin) fail('INVALID_INPUT');
50
- const headers = { Accept: vendor === 'pagerduty' ? 'application/vnd.pagerduty+json;version=2' : 'application/json', Authorization: vendor === 'pagerduty' ? `Token token=${key}` : `Bearer ${key}` };
60
+ const headers = { Accept: vendor === 'pagerduty' ? 'application/vnd.pagerduty+json;version=2' : 'application/json',
61
+ ...(keyed ? { Authorization: vendor === 'pagerduty' ? `Token token=${key}` : `Bearer ${key}` } : {}) };
51
62
  const ctx = {
52
63
  inputs, state,
53
64
  get: client({ origin, headers, route: adapter.route, signal, limits, counters, key }),
@@ -58,12 +69,12 @@ export async function* exportVendor(vendor, { key, inputs = {}, checkpoint, sign
58
69
  item(value, nextState, name) {
59
70
  signal?.throwIfAborted();
60
71
  const bytes = Buffer.isBuffer(value) ? value : Buffer.from(value);
61
- if (bytes.length > 1024 * 1024 || (counters.emitted += bytes.length) > limits.bytes) fail('BYTE_LIMIT');
62
- if (bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
72
+ if (bytes.length > 1024 * 1024 || (counters.emitted += bytes.length) > (limits.emitted ?? limits.bytes)) fail('BYTE_LIMIT');
73
+ if (key && bytes.includes(Buffer.from(key))) fail('CREDENTIAL_IN_RESPONSE');
63
74
  if (++sequence > 2000) fail('ITEM_LIMIT');
64
75
  const cp = { version: 1, vendor, fingerprint, sequence, counters: { ...counters }, state: nextState };
65
76
  const encoded = JSON.stringify(cp);
66
- if (Buffer.byteLength(encoded) >= 128 * 1024 || encoded.includes(key)) fail('CHECKPOINT_LIMIT');
77
+ if (Buffer.byteLength(encoded) >= 128 * 1024 || key && encoded.includes(key)) fail('CHECKPOINT_LIMIT');
67
78
  return { name: name ?? `${String(sequence).padStart(6,'0')}.part`, bytes, checkpoint: JSON.parse(encoded) };
68
79
  },
69
80
  };