@runeditrun/rer 0.2.85 → 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 (144) hide show
  1. package/README.md +138 -29
  2. package/cli/README.md +61 -1
  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/business/archive-task.mjs +32 -0
  14. package/cli/business/carry.mjs +3 -1
  15. package/cli/business/catchup-task.mjs +42 -0
  16. package/cli/business/choices.mjs +97 -0
  17. package/cli/business/command.mjs +23 -5
  18. package/cli/business/connect.mjs +19 -7
  19. package/cli/business/detector.mjs +4 -1
  20. package/cli/business/device.mjs +13 -0
  21. package/cli/business/export-inputs.mjs +69 -0
  22. package/cli/business/export-task.mjs +17 -0
  23. package/cli/business/invocation.mjs +8 -1
  24. package/cli/business/keychain.mjs +8 -1
  25. package/cli/business/lease.mjs +11 -1
  26. package/cli/business/local-commands.mjs +79 -31
  27. package/cli/business/outbound.mjs +9 -4
  28. package/cli/business/parsers-config.mjs +8 -7
  29. package/cli/business/parsers-syntax.mjs +7 -3
  30. package/cli/business/permissions.mjs +2 -0
  31. package/cli/business/questions.mjs +89 -9
  32. package/cli/business/setup-command.mjs +49 -0
  33. package/cli/business/setup-company.json +15 -0
  34. package/cli/business/setup-demo.mjs +95 -0
  35. package/cli/business/state.mjs +44 -10
  36. package/cli/business/supervisor.mjs +94 -35
  37. package/cli/business/vendor-door.mjs +230 -0
  38. package/cli/business/vendors/README.md +29 -3
  39. package/cli/business/vendors/common.mjs +8 -6
  40. package/cli/business/vendors/index.mjs +22 -11
  41. package/cli/business/vendors/sorry.mjs +106 -0
  42. package/cli/contract.mjs +2 -2
  43. package/cli/demo-deploy-error.mjs +14 -0
  44. package/cli/demo-native.mjs +4 -1
  45. package/cli/deploy-config.mjs +1 -1
  46. package/cli/deploy-owner-input.mjs +3 -1
  47. package/cli/deploy-variables.mjs +58 -0
  48. package/cli/door-clauses.mjs +157 -0
  49. package/cli/export.mjs +16 -4
  50. package/cli/hosted-output.mjs +17 -7
  51. package/cli/inspect.mjs +4 -0
  52. package/cli/jsonc.mjs +8 -4
  53. package/cli/lifecycle.mjs +3 -1
  54. package/cli/main.mjs +1 -0
  55. package/cli/managed-data-schema.mjs +14 -1
  56. package/cli/portability-empty.mjs +6 -0
  57. package/cli/portability-migrations.mjs +26 -4
  58. package/cli/production-auth.mjs +1 -1
  59. package/cli/production-mail.mjs +8 -2
  60. package/edition-authoring/archive-source.mjs +59 -0
  61. package/edition-authoring/base-copies.mjs +92 -18
  62. package/edition-authoring/cli.mjs +1 -1
  63. package/edition-authoring/edition-migrations.mjs +7 -3
  64. package/edition-authoring/module-closure.mjs +2 -16
  65. package/edition-authoring/workflow-copies.mjs +1 -1
  66. package/edition-authoring/workflow-scripts.mjs +12 -3
  67. package/foundation/runtime/contract-docs.mjs +215 -32
  68. package/foundation/runtime/contract-prose-patterns.mjs +14 -0
  69. package/foundation/runtime/contract-prose.mjs +88 -0
  70. package/global/CHANGELOG.md +93 -0
  71. package/global/README.md +138 -29
  72. package/global/door/command.mjs +3 -1
  73. package/global/main.mjs +1 -1
  74. package/modules/agent-door/CLAUSES.md +215 -0
  75. package/modules/agent-door/conformance/audit.mjs +66 -0
  76. package/modules/agent-door/conformance/authority.d.mts +2 -0
  77. package/modules/agent-door/conformance/authority.mjs +17 -0
  78. package/modules/agent-door/conformance/crypto.mjs +36 -0
  79. package/modules/agent-door/conformance/discovery.mjs +61 -0
  80. package/modules/agent-door/conformance/index.mjs +41 -0
  81. package/modules/agent-door/conformance/invocation.mjs +32 -0
  82. package/modules/agent-door/conformance/journey.mjs +112 -0
  83. package/modules/agent-door/conformance/narrowing.d.mts +1 -0
  84. package/modules/agent-door/conformance/narrowing.mjs +21 -0
  85. package/modules/agent-door/conformance/negative.mjs +47 -0
  86. package/modules/agent-door/conformance/network-modules.mjs +86 -0
  87. package/modules/agent-door/conformance/network-runtime.mjs +46 -0
  88. package/modules/agent-door/conformance/policy-phases.mjs +75 -0
  89. package/modules/agent-door/conformance/protocol.mjs +145 -0
  90. package/modules/agent-door/conformance/replay.mjs +38 -0
  91. package/modules/agent-door/conformance/schema.mjs +71 -0
  92. package/modules/agent-door/conformance/security.mjs +33 -0
  93. package/modules/agent-door/conformance/sponsors.mjs +68 -0
  94. package/modules/agent-door/conformance/surface.mjs +89 -0
  95. package/modules/agent-door/conformance/target.mjs +18 -0
  96. package/modules/agent-door/conformance/tool-name.d.mts +1 -0
  97. package/modules/agent-door/conformance/tool-name.mjs +7 -0
  98. package/modules/agent-door/conformance/webhooks.mjs +30 -0
  99. package/modules/container/image-variables.mjs +8 -2
  100. package/modules/demo-host/README.md +6 -0
  101. package/modules/demo-host/ownership.mjs +39 -0
  102. package/modules/demo-host/plan.mjs +2 -1
  103. package/modules/demo-host/remove.mjs +6 -1
  104. package/modules/import-kit/schema/importer-verification-receipt.schema.json +171 -0
  105. package/modules/import-kit/schema/importers.schema.json +107 -0
  106. package/modules/import-kit/src/door/ADOPTING.md +20 -0
  107. package/modules/import-kit/src/door/README.md +2 -1
  108. package/modules/import-kit/src/door/admission.ts +5 -3
  109. package/modules/import-kit/src/door/archive.ts +5 -2
  110. package/modules/import-kit/src/door/authority.ts +1 -0
  111. package/modules/import-kit/src/door/grants.ts +1 -1
  112. package/modules/import-kit/src/door/handler.ts +14 -8
  113. package/modules/import-kit/src/door/index.ts +1 -1
  114. package/modules/import-kit/src/door/jobs.ts +40 -0
  115. package/modules/import-kit/src/door/operator.ts +3 -3
  116. package/modules/import-kit/src/door/planning.ts +10 -6
  117. package/modules/import-kit/src/door/protocol.ts +2 -2
  118. package/modules/import-kit/src/door/raw.ts +25 -0
  119. package/modules/import-kit/src/door/refusals.ts +1 -0
  120. package/modules/import-kit/src/door/types.ts +26 -6
  121. package/modules/import-kit/src/receipt/encoding.ts +37 -0
  122. package/modules/import-kit/src/receipt/index.ts +69 -0
  123. package/modules/import-kit/src/receipt/policy.ts +24 -0
  124. package/modules/import-kit/src/receipt/types.ts +64 -0
  125. package/modules/import-kit/src/receipt/uri.ts +45 -0
  126. package/modules/import-kit/src/receipt/validation.ts +87 -0
  127. package/modules/import-kit/src/status/encoding.ts +13 -0
  128. package/modules/import-kit/src/status/evidence.ts +218 -0
  129. package/modules/import-kit/src/status/index.ts +56 -0
  130. package/modules/import-kit/src/status/schema.ts +208 -0
  131. package/modules/import-kit/src/status/types.ts +86 -0
  132. package/modules/operator-auth/accounts/delegated.mjs +48 -0
  133. package/modules/operator-auth/index.d.mts +7 -0
  134. package/modules/operator-auth/service.mjs +2 -0
  135. package/modules/portability/README.md +32 -11
  136. package/modules/portability/bridge.mjs +22 -2
  137. package/modules/portability/copies.mjs +8 -1
  138. package/modules/portability/derived.mjs +33 -0
  139. package/modules/portability/http-errors.mjs +1 -1
  140. package/modules/portability/import.mjs +14 -7
  141. package/package.json +4 -2
  142. package/scripts/publish-check/copy_identities.py +3 -1
  143. package/scripts/publish-check/github_refs.py +32 -13
  144. package/cli/business/opt-ins.mjs +0 -37
@@ -0,0 +1,15 @@
1
+ {
2
+ "synthetic": true,
3
+ "name": "Juniper Harbor Software — Synthetic demo",
4
+ "profile": {
5
+ "display_name": "Juniper Harbor Software — Synthetic demo",
6
+ "legal_name": "Juniper Harbor Software Inc. (synthetic)",
7
+ "country": "US",
8
+ "state": "Delaware",
9
+ "state_of_organization": "Delaware",
10
+ "entity_type": "Corporation",
11
+ "currency": "USD",
12
+ "timezone": "America/New_York",
13
+ "brand_color": "#245943"
14
+ }
15
+ }
@@ -0,0 +1,95 @@
1
+ // Mirrored from runeditrun/platform 0261033177e8e05284c037369ee9c3a85922a8aa, scripts/business/demo.mjs.
2
+ import { readFile } from 'node:fs/promises';
3
+ const company = JSON.parse(await readFile(new URL('./setup-company.json', import.meta.url), 'utf8'));
4
+ export const demoPolicy={schema:'rer-agent-policy/1',catalogue:'rer-agent-classes/1',rules:[
5
+ {id:'internal',class:'standard',do:'autonomous'},
6
+ {id:'context',class:'sensitive',do:'autonomous'},
7
+ {id:'owner',class:'*',do:'approval',approver:['role:owner'],wait:'P1D'},
8
+ ],spend:{none:true}};
9
+
10
+ /** The only injectable boundary is HTTP. Tokens, cookies, policy envelopes and peer bodies never enter the receipt. */
11
+ export async function setupDemo({origin,cookie,hosting='managed',waitSeconds=240,fetch:request=globalThis.fetch,now=Date.now,wait=ms=>new Promise(resolve=>setTimeout(resolve,ms))}) {
12
+ const api=async(path,method='GET',body,token)=>{
13
+ let response;
14
+ try{response=await request(new URL(path,origin),{method,redirect:'manual',signal:AbortSignal.timeout(60000),headers:{Origin:origin,Cookie:cookie,'Content-Type':'application/json',...(token?{'X-RER-Demo-Seed-Token':token}:{})},...(body===undefined?{}:{body:JSON.stringify(body)})});}
15
+ catch{throw Error('platform_transport_unavailable');}
16
+ let answer;try{answer=await response.json();}catch{throw Error(`platform_http_${response.status}`);}
17
+ if(!response.ok){const code=answer?.code;throw Error(typeof code==='string'&&/^[a-z0-9_]{1,100}$/.test(code)?code:`platform_http_${response.status}`);}
18
+ return answer;
19
+ };
20
+ const businesses=(await api('/api/business')).businesses;
21
+ let business=businesses.find(row=>row.name===company.name);
22
+ if(!business){
23
+ business=(await api('/api/business','POST',{name:company.name,default_hosting:hosting,executor_preference:hosting==='self_host'?'local':'managed'})).business;
24
+ await api('/api/business/'+encodeURIComponent(business.id)+'/profile','PUT',company.profile);
25
+ }
26
+ if(business.default_hosting!==hosting)throw Error('existing_demo_hosting_differs');
27
+ const root='/api/business/'+encodeURIComponent(business.id),initial=await api(root+'/demo');
28
+ // Return exactly the same projection after the first successful run and on every repeat.
29
+ if(initial.complete)return initial;
30
+ const pending=initial.seeds.filter(seed=>seed.adopted&&!seed.complete);
31
+ if(!pending.length)return initial;
32
+ let policy=await api(root+'/agents/policy');
33
+ if(policy.paused)throw Error('demo_is_paused_resume_as_owner');
34
+ const errors=new Map(),credentials=new Map(),ready=pending.filter(seed=>seed.provisioned);
35
+ let waiting=pending.filter(seed=>!seed.provisioned);
36
+ if(waiting.length){
37
+ const plan=await api(root+'/demo/plan','POST',{confirmed:true});
38
+ if(plan.plan_id&&!plan.approved)await api(root+'/plans/'+encodeURIComponent(plan.plan_id)+'/approve','POST',{});
39
+ const deadline=now()+waitSeconds*1000;
40
+ do{
41
+ await api(root+'/demo/advance','POST',{});
42
+ const status=await api(root+'/demo'),apps=(await api(root+'/apps')).apps,next=[];
43
+ for(const seed of waiting){
44
+ const current=status.seeds.find(row=>row.app===seed.app),installed=apps.find(row=>row.app===seed.app);
45
+ if(current?.provisioned)ready.push(current);
46
+ else if(installed?.failure||installed?.stage==='failed')errors.set(seed.app,'M4 provisioning failed');
47
+ else if(installed?.waits?.length||installed?.answers_needed?.length)errors.set(seed.app,'M4 setup needs hosting, payment, domain or app answers');
48
+ else next.push(seed);
49
+ }
50
+ waiting=next;
51
+ if(waiting.length&&now()<deadline)await wait(Math.min(3000,Math.max(0,deadline-now())));
52
+ }while(waiting.length&&now()<deadline);
53
+ for(const seed of waiting)errors.set(seed.app,'Run the same command to resume provisioning');
54
+ }
55
+ const eligible=[];
56
+ for(const seed of ready){
57
+ try{
58
+ // This is the shared Business sender, not a demo-specific settings signer or transport.
59
+ const settings=await api(root+'/settings/deliver','POST',{});
60
+ const delivery=settings.deliveries.find(row=>row.installation_id===seed.installation_id);
61
+ if(!delivery||delivery.state!=='delivered')throw Error(delivery?.code??'settings_not_delivered');
62
+ if(policy.revision&&!seed.no_policy_refused)throw Error('no_policy_probe_requires_fresh_demo');
63
+ const issued=await api(root+'/agents/seed-token','POST',{seed:seed.app,installation_id:seed.installation_id,
64
+ agent:{id:'demo:'+business.id,name:'Synthetic demo setup'},scopes:seed.scopes});
65
+ credentials.set(seed.app,issued.access_token);
66
+ if(!policy.revision&&!seed.no_policy_refused)await api(root+'/demo/apps/'+encodeURIComponent(seed.app)+'/door','POST',{phase:'unset'},issued.access_token);
67
+ eligible.push(seed);
68
+ }catch(error){errors.set(seed.app,error.message);}
69
+ }
70
+ if(eligible.length&&!policy.revision){
71
+ let draft=policy.draft;
72
+ if(draft&&JSON.stringify(draft.document)!==JSON.stringify(demoPolicy))throw Error('existing_policy_draft_requires_owner_review');
73
+ if(!draft)draft=(await api(root+'/agents/policy/revisions','POST',{document:demoPolicy})).revision;
74
+ await api(root+'/agents/policy/revisions/'+draft.revision+'/confirm','POST',{digest:draft.document_sha256});
75
+ policy=await api(root+'/agents/policy');
76
+ }
77
+ const seeded=[];
78
+ for(const seed of eligible){
79
+ try{await api(root+'/demo/apps/'+encodeURIComponent(seed.app)+'/door','POST',{phase:'seed'},credentials.get(seed.app));seeded.push(seed);}
80
+ catch(error){errors.set(seed.app,error.message);}
81
+ }
82
+ if(seeded.length){
83
+ await api(root+'/pause','POST',{paused:true});
84
+ let allPaused=true;
85
+ for(const seed of seeded){
86
+ try{await api(root+'/demo/apps/'+encodeURIComponent(seed.app)+'/door','POST',{phase:'paused'},credentials.get(seed.app));}
87
+ catch(error){allPaused=false;errors.set(seed.app,error.message);}
88
+ }
89
+ if(allPaused)await api(root+'/pause','POST',{paused:false});
90
+ }
91
+ credentials.clear();
92
+ const receipt=await api(root+'/demo');
93
+ for(const seed of receipt.seeds)if(!seed.complete&&errors.has(seed.app))seed.cause=errors.get(seed.app);
94
+ return receipt;
95
+ }
@@ -1,4 +1,5 @@
1
- import { mkdir, readFile, readdir, writeFile, rename, chmod, open, unlink, rm, rmdir, lstat } from 'node:fs/promises';
1
+ import { mkdir, readFile, readdir, writeFile, rename, chmod, open, unlink, rm, rmdir, lstat, utimes } from 'node:fs/promises';
2
+ import { readFileSync, unlinkSync } from 'node:fs';
2
3
  import { dirname, join, resolve } from 'node:path';
3
4
  import { homedir } from 'node:os';
4
5
  import { createHash, randomUUID } from 'node:crypto';
@@ -81,7 +82,15 @@ export async function checkStateWritable() {
81
82
 
82
83
  // Files this CLI writes in its state folder. A folder named by RER_BUSINESS_STATE_DIR
83
84
  // may hold other things, so only these are removed from it.
84
- const owned = /^(?:(?:state|pairing|progress|permissions|service|carried)\.json|run\.lock|disconnect|service(?:-error)?\.log|discovery-[A-Za-z0-9_.:-]+\.(?:json|tmp)|.*[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.tmp(?:\.check)?)$/;
85
+ const owned = /^(?:(?:state|pairing|progress|permissions|service|carried)\.json|run\.lock|disconnect|service(?:-error)?\.log|discovery-[A-Za-z0-9_.:-]+\.(?:json|tmp)|export-[0-9a-f]{32}\.bundle|.*[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.tmp(?:\.check)?)$/;
86
+ /** After a revocation whose local cleanup was refused, keep only what a later
87
+ * disconnect needs to finish: the state and the receipts of what remains. */
88
+ export async function removeWorkFiles(keep = ['state.json', 'permissions.json', 'service.json']) {
89
+ let entries;
90
+ try { entries = await readdir(stateDirectory()); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
91
+ for (const name of entries) if (owned.test(name) && !keep.includes(name)) await rm(join(stateDirectory(), name), { force: true });
92
+ }
93
+
85
94
  export async function removeStateDirectory() {
86
95
  const directory = stateDirectory();
87
96
  if (!chosenStateDirectory()) {
@@ -96,22 +105,47 @@ export async function removeStateDirectory() {
96
105
  await rmdir(directory).catch(error => { if (!['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes(error.code)) throw error; });
97
106
  }
98
107
 
108
+ // run.lock holds the supervisor's process id, and its time is refreshed while
109
+ // it runs. A lock whose process is gone, or that has not been refreshed for
110
+ // two minutes (a process id a sandbox cannot check), is stale and replaced.
111
+ const lockFresh = 120_000;
112
+ export const lockPath = () => join(stateDirectory(), 'run.lock');
113
+ export async function liveSupervisor(path = lockPath()) {
114
+ let text, info;
115
+ try { [text, info] = await Promise.all([readFile(path, 'utf8'), lstat(path)]); }
116
+ catch (error) { if (error.code === 'ENOENT') return null; throw error; }
117
+ // A lock is written right after it is created; give an empty one a moment.
118
+ if (!text) return Date.now() - info.mtimeMs < 5_000 ? 'starting' : null;
119
+ const pid = Number(text);
120
+ if (!Number.isSafeInteger(pid) || pid <= 0) return null;
121
+ try { process.kill(pid, 0); }
122
+ catch (error) { if (error.code === 'ESRCH') return null; if (error.code !== 'EPERM') throw error; }
123
+ return Date.now() - info.mtimeMs > lockFresh ? null : pid;
124
+ }
125
+
126
+ /** Take run.lock, replacing a stale one. The returned release removes it; it is
127
+ * also removed on every exit of this process, including after a signal. */
99
128
  export async function lockSupervisor() {
100
- const directory = stateDirectory();
129
+ const directory = stateDirectory(), path = lockPath();
101
130
  await mkdir(directory, { recursive: true, mode: 0o700 });
102
- const path = join(directory, 'run.lock');
103
131
  for (let attempt = 0; attempt < 2; attempt++) {
104
132
  try {
105
133
  const file = await open(path, 'wx', 0o600);
106
134
  await file.writeFile(String(process.pid)); await file.close();
107
- return async () => { await unlink(path).catch(error => { if (error.code !== 'ENOENT') throw error; }); };
135
+ const refresh = setInterval(() => { const now = new Date(); utimes(path, now, now).catch(() => {}); }, 30_000);
136
+ refresh.unref();
137
+ const exit = () => { try { if (readFileSync(path, 'utf8') === String(process.pid)) unlinkSync(path); } catch { /* Already gone. */ } };
138
+ process.once('exit', exit);
139
+ const release = async () => { clearInterval(refresh); process.off('exit', exit); exit(); };
140
+ // A signal is often followed closely by SIGKILL (an agent closing its
141
+ // terminal), so a signal handler removes the lock synchronously at once.
142
+ release.now = exit;
143
+ return release;
108
144
  } catch (error) {
109
145
  if (error.code !== 'EEXIST') throw error;
110
- const pid = Number(await readFile(path, 'utf8'));
111
- if (!Number.isSafeInteger(pid) || pid <= 0) throw Error('Business supervisor lock is invalid.');
112
- try { process.kill(pid, 0); throw Error('A Business supervisor is already running.'); }
113
- catch (check) { if (check.code !== 'ESRCH') throw check; }
114
- await unlink(path);
146
+ const pid = await liveSupervisor(path);
147
+ if (pid) throw Error(`A Business supervisor is already running for this computer${pid === 'starting' ? '' : ` (process ${pid})`}. Let it keep working, or stop it before starting another.`);
148
+ await unlink(path).catch(error => { if (error.code !== 'ENOENT') throw error; });
115
149
  }
116
150
  }
117
151
  throw Error('Could not acquire Business supervisor lock.');
@@ -3,36 +3,73 @@ import { randomUUID } from 'node:crypto';
3
3
  import { access, unlink } from 'node:fs/promises';
4
4
  import { join } from 'node:path';
5
5
  import { Api, taskPath, pause } from './api.mjs';
6
- import { paired } from './local-commands.mjs';
7
- import { createCredentialStore } from './keychain.mjs';
8
- import { nextQuestions } from './questions.mjs';
9
- 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';
10
9
  import { loadProgress, saveProgress } from './progress.mjs';
11
10
  import { Lease } from './lease.mjs';
12
11
  import { runDiscovery } from './discovery-task.mjs';
13
- import { applyOptIns } from './opt-ins.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';
14
18
 
15
- const kinds = new Set(['discover.local', 'export.vendor']);
16
- 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;
17
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). */
18
28
  export async function run({ once = false } = {}) {
19
29
  const { state, credential } = await paired(), api = new Api(state.origin, credential), directory = stateDirectory();
20
- const parked = new Set(), unlock = await lockSupervisor(), stop = new AbortController();
21
- const interrupted = () => stop.abort(Error('Business supervisor stopped.'));
22
- process.once('SIGTERM', interrupted); process.once('SIGINT', interrupted);
23
- const monitor = setInterval(() => { access(join(directory, 'disconnect')).then(interrupted, () => {}); }, 250);
24
- let progress, stopCarry;
25
-
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); };
26
48
  try {
27
- await applyOptIns(api, state, { report: false });
28
49
  stopCarry = await startCarryRefresh(api, state, stop.signal);
29
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;
30
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`);
31
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
+ }
32
61
  if (!progress.pendingClaim) {
33
- 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);
34
65
  const selected = summaries.find(task => kinds.has(task.kind) && ['ready','leased'].includes(task.state) && !parked.has(task.task_id));
35
- 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;
36
73
  progress.pendingClaim = { claim_id: randomUUID(), task_id: selected.task_id };
37
74
  await saveProgress(progress);
38
75
  }
@@ -42,11 +79,21 @@ export async function run({ once = false } = {}) {
42
79
  if (error.status !== 409) throw error;
43
80
  progress = {}; await saveProgress(progress);
44
81
  if (once) throw error;
45
- await pause(25_000, stop.signal); continue;
82
+ await pause(checkIn, stop.signal); continue;
46
83
  }
47
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;
48
86
  progress = {}; await saveProgress(progress);
49
- 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
+ }
50
97
  continue;
51
98
  }
52
99
  if (!kinds.has(claimed.task.kind) || claimed.task.kind_version !== 1) throw Error('Business server returned an unimplemented task kind.');
@@ -55,22 +102,32 @@ export async function run({ once = false } = {}) {
55
102
  try {
56
103
  progress.current = { kind: claimed.task.kind, checkpoint: claimed.task.checkpoint, uploadedBytes: claimed.task.checkpoint?.bytes_done ?? 0 };
57
104
  await saveProgress(progress);
58
- if (claimed.task.kind === 'export.vendor') {
59
- // A destination is not upload authority. Keep vendor keys local until
60
- // the seed's signed import grant and chunk transport are connected.
61
- await lease.send(taskPath(claimed.task.id, 'events'), { event: { kind: 'question', payload: { question: 'connected_runner_required' } } });
62
- parked.add(claimed.task.id);
63
- if (claimed.grant.available === false && claimed.grant.destination === `unavailable:${claimed.grant.reason}`) {
64
- const reason = unavailableReasons.has(claimed.grant.reason) ? claimed.grant.reason : 'connected-seed-import-required';
65
- 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;
66
116
  }
67
- 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.');
68
130
  }
69
- const result = await runDiscovery({ task: claimed.task, grant: claimed.grant, checkpoint: claimed.task.checkpoint, signal: lease.signal,
70
- send: (destination, body) => lease.send(destination, body), saveCheckpoint: checkpoint => lease.checkpointSaved(checkpoint), stateDir: directory });
71
- await lease.complete(); finished = true;
72
- await result.cleanup();
73
- process.stdout.write('Business discover.local completed and checked by the server.\n');
74
131
  } catch (error) { failure = lease.signal.aborted ? lease.signal.reason : error; }
75
132
  finally {
76
133
  if (!finished && failure?.status !== 401) {
@@ -88,11 +145,13 @@ export async function run({ once = false } = {}) {
88
145
  } while (!stop.signal.aborted);
89
146
  } catch (error) {
90
147
  if (error.status === 401) {
91
- await createCredentialStore().delete(credentialHandle(state.origin));
92
- 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`);
93
150
  } else if (!stop.signal.aborted) throw error;
94
151
  } finally {
95
152
  await stopCarry?.();
96
- clearInterval(monitor); process.off('SIGTERM', interrupted); process.off('SIGINT', interrupted); await unlock();
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`);
97
156
  }
98
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
+ }