@andorra-labs/code 0.1.16 → 0.1.18

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/dist/src/acp-agent.d.ts +6 -0
  2. package/dist/src/acp-agent.js +15 -4
  3. package/dist/src/agent-execution.js +108 -19
  4. package/dist/src/agent-mcp-catalog.js +1 -0
  5. package/dist/src/agent-mcp.d.ts +3 -0
  6. package/dist/src/agent-mcp.js +33 -1
  7. package/dist/src/andorra/entry.d.ts +10 -0
  8. package/dist/src/andorra/entry.js +18 -4
  9. package/dist/src/andorra/errors.js +3 -0
  10. package/dist/src/andorra/hosted.d.ts +2 -1
  11. package/dist/src/andorra/hosted.js +2 -1
  12. package/dist/src/andorra/notices.d.ts +5 -0
  13. package/dist/src/andorra/notices.js +7 -0
  14. package/dist/src/andorra/state.d.ts +3 -2
  15. package/dist/src/andorra/state.js +4 -4
  16. package/dist/src/andorra/terminal.js +7 -2
  17. package/dist/src/anthropic-credentials.js +2 -0
  18. package/dist/src/app-server/error.d.ts +5 -0
  19. package/dist/src/app-server/error.js +7 -0
  20. package/dist/src/app-server/protocol.d.ts +30 -44
  21. package/dist/src/app-server/protocol.js +5 -1
  22. package/dist/src/app-server/workspace.d.ts +108 -0
  23. package/dist/src/app-server/workspace.js +608 -11
  24. package/dist/src/app-server.d.ts +390 -33
  25. package/dist/src/app-server.js +3033 -565
  26. package/dist/src/attachments.d.ts +10 -0
  27. package/dist/src/attachments.js +102 -7
  28. package/dist/src/automation-tool.js +2 -1
  29. package/dist/src/bridge/cloud.d.ts +27 -0
  30. package/dist/src/bridge/cloud.js +187 -0
  31. package/dist/src/bridge/command.d.ts +30 -0
  32. package/dist/src/bridge/command.js +359 -0
  33. package/dist/src/bridge/control.d.ts +70 -0
  34. package/dist/src/bridge/control.js +238 -0
  35. package/dist/src/bridge/engines.d.ts +90 -0
  36. package/dist/src/bridge/engines.js +601 -0
  37. package/dist/src/bridge/github.d.ts +108 -0
  38. package/dist/src/bridge/github.js +378 -0
  39. package/dist/src/bridge/host-ops.d.ts +9 -0
  40. package/dist/src/bridge/host-ops.js +139 -0
  41. package/dist/src/bridge/host.d.ts +47 -0
  42. package/dist/src/bridge/host.js +543 -0
  43. package/dist/src/bridge/link.d.ts +38 -0
  44. package/dist/src/bridge/link.js +508 -0
  45. package/dist/src/bridge/login-link.d.ts +29 -0
  46. package/dist/src/bridge/login-link.js +54 -0
  47. package/dist/src/bridge/protocol.d.ts +136 -0
  48. package/dist/src/bridge/protocol.js +11 -0
  49. package/dist/src/bridge/store.d.ts +12 -0
  50. package/dist/src/bridge/store.js +61 -0
  51. package/dist/src/bridge/wording.d.ts +6 -0
  52. package/dist/src/bridge/wording.js +10 -0
  53. package/dist/src/browser-server.js +7 -1
  54. package/dist/src/browser-ui/tokens.css +1 -1
  55. package/dist/src/bug-report-attachments.d.ts +48 -0
  56. package/dist/src/bug-report-attachments.js +327 -0
  57. package/dist/src/bug-report.d.ts +23 -3
  58. package/dist/src/bug-report.js +72 -9
  59. package/dist/src/change-authors.d.ts +41 -0
  60. package/dist/src/change-authors.js +146 -0
  61. package/dist/src/cli.d.ts +1 -3
  62. package/dist/src/cli.js +191 -35
  63. package/dist/src/codex-provider.js +1 -1
  64. package/dist/src/coding-tools.js +24 -17
  65. package/dist/src/command-screens.d.ts +6 -0
  66. package/dist/src/command-screens.js +14 -11
  67. package/dist/src/commands.d.ts +15 -3
  68. package/dist/src/commands.js +2 -1
  69. package/dist/src/compaction-phrases.js +1 -1
  70. package/dist/src/completion.js +89 -8
  71. package/dist/src/config.d.ts +71 -3
  72. package/dist/src/config.js +205 -78
  73. package/dist/src/context-budget.js +15 -2
  74. package/dist/src/deadline.js +13 -1
  75. package/dist/src/decision/DecisionEngine.d.ts +2 -0
  76. package/dist/src/decision/DecisionManager.d.ts +11 -0
  77. package/dist/src/decision/DecisionManager.js +36 -0
  78. package/dist/src/decision/applyTaskKind.d.ts +8 -0
  79. package/dist/src/decision/applyTaskKind.js +53 -0
  80. package/dist/src/decision/checks.js +1 -1
  81. package/dist/src/decision/hostedPolicy.js +2 -1
  82. package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +2 -0
  83. package/dist/src/decision/providers/AndorraDecisionProvider.js +12 -0
  84. package/dist/src/decision/requestPhrases.js +0 -1
  85. package/dist/src/decision/taskKind.d.ts +52 -0
  86. package/dist/src/decision/taskKind.js +76 -0
  87. package/dist/src/decision/types.d.ts +1 -1
  88. package/dist/src/default-graph.js +1 -1
  89. package/dist/src/efficiency.d.ts +5 -0
  90. package/dist/src/efficiency.js +5 -1
  91. package/dist/src/effort-routing.d.ts +27 -0
  92. package/dist/src/effort-routing.js +34 -0
  93. package/dist/src/evidence.d.ts +110 -0
  94. package/dist/src/evidence.js +195 -0
  95. package/dist/src/file-diff.d.ts +4 -0
  96. package/dist/src/file-diff.js +2 -1
  97. package/dist/src/github-app/agent-writes.d.ts +102 -0
  98. package/dist/src/github-app/agent-writes.js +320 -0
  99. package/dist/src/github-app/client.d.ts +368 -0
  100. package/dist/src/github-app/client.js +830 -0
  101. package/dist/src/github-app/event-source.d.ts +208 -0
  102. package/dist/src/github-app/event-source.js +684 -0
  103. package/dist/src/github-app/metrics.d.ts +32 -0
  104. package/dist/src/github-app/metrics.js +49 -0
  105. package/dist/src/github-app/review-loop.d.ts +274 -0
  106. package/dist/src/github-app/review-loop.js +1177 -0
  107. package/dist/src/github-app/review-node.d.ts +81 -0
  108. package/dist/src/github-app/review-node.js +205 -0
  109. package/dist/src/github-app/review-post.d.ts +73 -0
  110. package/dist/src/github-app/review-post.js +139 -0
  111. package/dist/src/github-app/review-text.d.ts +107 -0
  112. package/dist/src/github-app/review-text.js +1133 -0
  113. package/dist/src/github-app/review-threads.d.ts +47 -0
  114. package/dist/src/github-app/review-threads.js +107 -0
  115. package/dist/src/github-app/reviewer.d.ts +121 -0
  116. package/dist/src/github-app/reviewer.js +598 -0
  117. package/dist/src/github-app/store.d.ts +59 -0
  118. package/dist/src/github-app/store.js +148 -0
  119. package/dist/src/github-app/triggers.d.ts +141 -0
  120. package/dist/src/github-app/triggers.js +441 -0
  121. package/dist/src/github-names.d.ts +15 -0
  122. package/dist/src/github-names.js +15 -0
  123. package/dist/src/glob-search.d.ts +61 -0
  124. package/dist/src/glob-search.js +180 -0
  125. package/dist/src/graph-health.js +6 -1
  126. package/dist/src/graph-manager-chat.js +1 -1
  127. package/dist/src/graph-preview.d.ts +48 -0
  128. package/dist/src/graph-preview.js +56 -0
  129. package/dist/src/graph-provider-portability.js +2 -1
  130. package/dist/src/graph-runner.js +65 -4
  131. package/dist/src/graph-server.js +7 -5
  132. package/dist/src/graph-sync.js +3 -3
  133. package/dist/src/graph-ui/app.js +121 -7
  134. package/dist/src/graph-ui/icons.js +2 -1
  135. package/dist/src/graph-ui/studio.js +15 -1
  136. package/dist/src/graph-ui/tokens.css +1 -1
  137. package/dist/src/graphs.d.ts +40 -1
  138. package/dist/src/graphs.js +164 -15
  139. package/dist/src/hardened-runtime.js +32 -21
  140. package/dist/src/image-input.d.ts +16 -0
  141. package/dist/src/image-input.js +41 -0
  142. package/dist/src/membership.d.ts +10 -0
  143. package/dist/src/membership.js +19 -4
  144. package/dist/src/model-health-cache.js +1 -1
  145. package/dist/src/model-metadata.d.ts +6 -0
  146. package/dist/src/model-metadata.js +35 -15
  147. package/dist/src/model-override.d.ts +2 -1
  148. package/dist/src/model-override.js +9 -0
  149. package/dist/src/model-relay/local.d.ts +116 -0
  150. package/dist/src/model-relay/local.js +468 -0
  151. package/dist/src/model-relay/remote.d.ts +129 -0
  152. package/dist/src/model-relay/remote.js +252 -0
  153. package/dist/src/model-relay/runs.d.ts +34 -0
  154. package/dist/src/model-relay/runs.js +146 -0
  155. package/dist/src/model-relay/wire.d.ts +5 -0
  156. package/dist/src/model-relay/wire.js +5 -0
  157. package/dist/src/model-tiers.d.ts +3 -0
  158. package/dist/src/model-tiers.js +3 -3
  159. package/dist/src/project-registry.d.ts +21 -0
  160. package/dist/src/project-registry.js +74 -0
  161. package/dist/src/provider-engine.d.ts +13 -1
  162. package/dist/src/provider-engine.js +74 -0
  163. package/dist/src/provider-import.d.ts +0 -12
  164. package/dist/src/provider-import.js +7 -195
  165. package/dist/src/providers.d.ts +28 -2
  166. package/dist/src/providers.js +59 -12
  167. package/dist/src/pull-request-logs.d.ts +12 -1
  168. package/dist/src/pull-request-logs.js +14 -3
  169. package/dist/src/pull-requests.d.ts +338 -8
  170. package/dist/src/pull-requests.js +482 -53
  171. package/dist/src/react-runtime.js +99 -16
  172. package/dist/src/remote/engine-pool.d.ts +88 -4
  173. package/dist/src/remote/engine-pool.js +367 -90
  174. package/dist/src/remote/roots.d.ts +19 -0
  175. package/dist/src/remote/roots.js +45 -7
  176. package/dist/src/remote/types.d.ts +0 -44
  177. package/dist/src/run-control.d.ts +7 -0
  178. package/dist/src/run-control.js +23 -0
  179. package/dist/src/run-records.d.ts +7 -2
  180. package/dist/src/run-records.js +71 -6
  181. package/dist/src/runtime.d.ts +34 -2
  182. package/dist/src/runtime.js +18 -2
  183. package/dist/src/session-changes.d.ts +35 -0
  184. package/dist/src/session-changes.js +194 -6
  185. package/dist/src/session-monitor.js +2 -1
  186. package/dist/src/session-reaper.d.ts +1 -1
  187. package/dist/src/session-reaper.js +6 -1
  188. package/dist/src/session-summary.d.ts +2 -1
  189. package/dist/src/session-summary.js +2 -1
  190. package/dist/src/sessions.d.ts +35 -1
  191. package/dist/src/sessions.js +70 -4
  192. package/dist/src/skills/bundled/bug-report/SKILL.md +51 -7
  193. package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
  194. package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
  195. package/dist/src/skills/bundled/provenance.json +3 -3
  196. package/dist/src/skills/runtime-skills.js +1 -1
  197. package/dist/src/task-checkout.d.ts +141 -0
  198. package/dist/src/task-checkout.js +558 -0
  199. package/dist/src/telemetry/emitter.d.ts +15 -0
  200. package/dist/src/telemetry/emitter.js +35 -1
  201. package/dist/src/telemetry/schema.d.ts +25 -2
  202. package/dist/src/telemetry/schema.js +52 -4
  203. package/dist/src/telemetry/surface.d.ts +2 -1
  204. package/dist/src/telemetry/surface.js +4 -1
  205. package/dist/src/terminal-ui.d.ts +1 -0
  206. package/dist/src/terminal-ui.js +10 -3
  207. package/dist/src/test-edit-guard.d.ts +37 -0
  208. package/dist/src/test-edit-guard.js +216 -3
  209. package/dist/src/thinking-level-detect.d.ts +11 -0
  210. package/dist/src/thinking-level-detect.js +47 -0
  211. package/dist/src/thinking-levels.d.ts +30 -0
  212. package/dist/src/thinking-levels.js +124 -0
  213. package/dist/src/timers.d.ts +3 -0
  214. package/dist/src/tool-catalog.js +1 -1
  215. package/dist/src/tools.js +59 -9
  216. package/dist/src/transcript.js +1 -1
  217. package/dist/src/types.d.ts +34 -0
  218. package/package.json +6 -1
  219. package/dist/src/remote/cert.d.ts +0 -11
  220. package/dist/src/remote/cert.js +0 -87
  221. package/dist/src/remote/client.d.ts +0 -119
  222. package/dist/src/remote/client.js +0 -544
  223. package/dist/src/remote/command.d.ts +0 -4
  224. package/dist/src/remote/command.js +0 -194
  225. package/dist/src/remote/errors.d.ts +0 -11
  226. package/dist/src/remote/errors.js +0 -14
  227. package/dist/src/remote/network.d.ts +0 -22
  228. package/dist/src/remote/network.js +0 -102
  229. package/dist/src/remote/pairing.d.ts +0 -63
  230. package/dist/src/remote/pairing.js +0 -299
  231. package/dist/src/remote/pem.d.ts +0 -2
  232. package/dist/src/remote/pem.js +0 -7
  233. package/dist/src/remote/serve.d.ts +0 -68
  234. package/dist/src/remote/serve.js +0 -244
  235. package/dist/src/remote/server.d.ts +0 -21
  236. package/dist/src/remote/server.js +0 -406
@@ -1,544 +0,0 @@
1
- import { createHash, randomBytes } from 'node:crypto';
2
- import * as tls from 'node:tls';
3
- import { isIP } from 'node:net';
4
- import { hostname as osHostname } from 'node:os';
5
- import { request as httpRequest } from 'node:http';
6
- import { request as httpsRequest } from 'node:https';
7
- import { RemotePeerError, InviteRejectedError } from './errors.js';
8
- import { pemFromDer } from './pem.js';
9
- import { resolvePeerAddress, resolvePeerAddresses } from './network.js';
10
- const REQUEST_TIMEOUT_MS = 15_000;
11
- const MAX_RESPONSE_BYTES = 2 * 1024 * 1024;
12
- export function addressAttemptBudgetMs(deadlineAt, now) {
13
- return deadlineAt === undefined ? REQUEST_TIMEOUT_MS : Math.min(REQUEST_TIMEOUT_MS, deadlineAt - now);
14
- }
15
- function fingerprintOf(der) {
16
- return createHash('sha256').update(der).digest('base64url');
17
- }
18
- function classifyTlsFailure(error, sawCertificate) {
19
- const detail = `${error.code ?? ''} ${error.message}`;
20
- if (/certificate|verify|self-|altname|ERR_TLS_CERT_ALTNAME/i.test(detail)) {
21
- return new RemotePeerError('identity', 'The peer presented a different certificate than the one you paired. Re-pairing is required.');
22
- }
23
- if (/EPROTO|wrong version number|packet length|not secure|sslv3|alert/i.test(detail) && !sawCertificate) {
24
- return new RemotePeerError('network', 'The peer port did not answer the TLS handshake.');
25
- }
26
- return new RemotePeerError('network', `Peer TLS failed: ${error.message}`);
27
- }
28
- function classifyFailure(error, identityError) {
29
- if (identityError)
30
- return identityError;
31
- const code = error.code ?? '';
32
- const detail = `${code} ${error.message}`;
33
- if (code === 'ECONNREFUSED' || code === 'EHOSTUNREACH' || code === 'ENETUNREACH' || code === 'ETIMEDOUT' || code === 'ENOTFOUND' || code === 'EAI_AGAIN') {
34
- return new RemotePeerError('offline', `Could not reach the paired machine (${code}).`);
35
- }
36
- if (/certificate|verify|self-|ERR_TLS_CERT_ALTNAME|unable to verify/i.test(detail)) {
37
- return new RemotePeerError('identity', 'The peer presented a certificate that does not match the one you paired. Re-pairing is required.');
38
- }
39
- if (/EPROTO|wrong version number|packet length|sslv3|alert|not secure/i.test(detail)) {
40
- return new RemotePeerError('network', 'The peer port did not answer the TLS handshake.');
41
- }
42
- return new RemotePeerError('network', error.message);
43
- }
44
- async function request(target, method, path, body, hostOrder, deadlineAt, limits = {}, signal) {
45
- signal?.throwIfAborted();
46
- const maxResponseBytes = limits.maxResponseBytes ?? MAX_RESPONSE_BYTES;
47
- const host = hostOrder ?? target.host;
48
- const resolveAddresses = resolvePeerAddresses(host).catch((error) => {
49
- throw classifyFailure(error, undefined);
50
- });
51
- let resolveTimer;
52
- let removeAbortListener;
53
- const pending = [resolveAddresses];
54
- if (deadlineAt !== undefined)
55
- pending.push(new Promise((_, reject) => {
56
- const remaining = deadlineAt - Date.now();
57
- if (remaining <= 0)
58
- reject(new RemotePeerError('offline', `Reaching ${host} ran out of time.`));
59
- else
60
- resolveTimer = setTimeout(() => reject(new RemotePeerError('offline', `Reaching ${host} ran out of time.`)), remaining);
61
- }));
62
- if (signal)
63
- pending.push(new Promise((_, reject) => {
64
- const onAbort = () => reject(signal.reason ?? new Error('Peer request cancelled.'));
65
- removeAbortListener = () => signal.removeEventListener('abort', onAbort);
66
- signal.addEventListener('abort', onAbort, { once: true });
67
- if (signal.aborted)
68
- onAbort();
69
- }));
70
- const addresses = await Promise.race(pending).finally(() => {
71
- if (resolveTimer !== undefined)
72
- clearTimeout(resolveTimer);
73
- removeAbortListener?.();
74
- });
75
- signal?.throwIfAborted();
76
- const certPem = target.certPem;
77
- if (target.transport === 'https' && !certPem)
78
- throw new RemotePeerError('network', 'No pinned certificate for this workspace — it must be re-paired.');
79
- const payload = body === undefined ? undefined : JSON.stringify(body);
80
- const send = (address, attemptTimeoutMs) => new Promise((resolve, reject) => {
81
- if (signal?.aborted) {
82
- reject(signal.reason ?? new Error('Peer request cancelled.'));
83
- return;
84
- }
85
- let identityError;
86
- let settled = false;
87
- let deadlineTimer;
88
- const stopDeadline = () => { if (deadlineTimer !== undefined) {
89
- clearTimeout(deadlineTimer);
90
- deadlineTimer = undefined;
91
- } };
92
- const fail = (error) => { if (!settled) {
93
- settled = true;
94
- stopDeadline();
95
- reject(error);
96
- } };
97
- const succeed = (value) => { if (!settled) {
98
- settled = true;
99
- stopDeadline();
100
- resolve(value);
101
- } };
102
- const chunks = [];
103
- let size = 0;
104
- const options = {
105
- host: address.address,
106
- port: target.port,
107
- method,
108
- path,
109
- ...(signal ? { signal } : {}),
110
- ...(target.transport === 'https' ? {} : { agent: false }),
111
- headers: {
112
- ...(target.token ? { authorization: `Bearer ${target.token}` } : {}),
113
- ...(payload !== undefined ? { 'content-type': 'application/json', 'content-length': String(Buffer.byteLength(payload)) } : {}),
114
- host: `${target.host}:${target.port}`,
115
- },
116
- };
117
- if (target.transport === 'https' && certPem) {
118
- const tlsOptions = {
119
- host: address.address,
120
- port: target.port,
121
- ...(isIP(host) ? {} : { servername: host }),
122
- ca: [certPem],
123
- rejectUnauthorized: true,
124
- checkServerIdentity: () => undefined,
125
- timeout: attemptTimeoutMs,
126
- };
127
- Object.assign(options, tlsOptions);
128
- options.createConnection = (_connectOptions, onCreated) => {
129
- const socket = tls.connect(tlsOptions);
130
- let gated = false;
131
- if (signal) {
132
- const onAbort = () => {
133
- socket.destroy();
134
- if (!gated) {
135
- gated = true;
136
- onCreated(new Error('Peer request cancelled.'));
137
- }
138
- };
139
- signal.addEventListener('abort', onAbort, { once: true });
140
- socket.once('close', () => signal.removeEventListener('abort', onAbort));
141
- if (signal.aborted) {
142
- onAbort();
143
- return;
144
- }
145
- }
146
- socket.once('secureConnect', () => {
147
- if (gated)
148
- return;
149
- gated = true;
150
- const certificate = socket.getPeerCertificate(true);
151
- const der = certificate?.raw;
152
- if (!der) {
153
- identityError = new RemotePeerError('identity', 'The peer presented no certificate.');
154
- socket.destroy();
155
- onCreated(identityError);
156
- return;
157
- }
158
- const presented = fingerprintOf(Buffer.from(der));
159
- if (presented !== target.fingerprint) {
160
- identityError = new RemotePeerError('identity', 'The peer presented a different certificate than the one you paired. Re-pairing is required.');
161
- socket.destroy();
162
- onCreated(identityError);
163
- return;
164
- }
165
- onCreated(null, socket);
166
- });
167
- socket.once('error', (error) => {
168
- if (gated)
169
- return;
170
- gated = true;
171
- onCreated(error);
172
- });
173
- socket.on('timeout', () => {
174
- socket.destroy();
175
- if (!gated) {
176
- gated = true;
177
- onCreated(new RemotePeerError('offline', `Connecting to ${host}:${target.port} timed out.`));
178
- }
179
- });
180
- };
181
- }
182
- else {
183
- options.timeout = attemptTimeoutMs;
184
- }
185
- const req = (target.transport === 'https' ? httpsRequest : httpRequest)(options, res => {
186
- res.on('data', chunk => {
187
- size += chunk.length;
188
- if (size > maxResponseBytes) {
189
- req.destroy();
190
- fail(new RemotePeerError('network', 'Peer response exceeded the size limit.'));
191
- return;
192
- }
193
- chunks.push(chunk);
194
- });
195
- res.on('end', () => {
196
- const status = res.statusCode ?? 0;
197
- const text = Buffer.concat(chunks).toString('utf8');
198
- let parsed;
199
- try {
200
- parsed = text ? JSON.parse(text) : undefined;
201
- }
202
- catch {
203
- parsed = undefined;
204
- }
205
- const detail = parsed && typeof parsed === 'object' && 'error' in parsed && typeof parsed.error === 'string'
206
- ? parsed.error : undefined;
207
- if (status >= 300 && status < 400) {
208
- fail(new RemotePeerError('network', 'The peer tried to redirect this request, which remote browsing does not follow.'));
209
- return;
210
- }
211
- if (status === 401) {
212
- fail(new RemotePeerError('unauthorized', detail ?? 'This device is no longer authorized on that workspace.'));
213
- return;
214
- }
215
- if (status >= 400) {
216
- fail(new RemotePeerError('rejected', detail ?? `The peer refused the request (${status}).`));
217
- return;
218
- }
219
- if (!settled)
220
- succeed(parsed);
221
- });
222
- res.on('error', (error) => fail(new RemotePeerError('network', error.message)));
223
- });
224
- req.setTimeout(attemptTimeoutMs, () => { req.destroy(); fail(new RemotePeerError('offline', 'The peer did not answer in time.')); });
225
- req.on('error', (error) => fail(classifyFailure(error, identityError)));
226
- if (payload !== undefined)
227
- req.write(payload);
228
- req.end();
229
- if (deadlineAt !== undefined) {
230
- const left = deadlineAt - Date.now();
231
- const spent = () => { fail(new RemotePeerError('offline', `Reaching ${host} ran out of time.`)); req.destroy(); };
232
- if (left <= 0)
233
- spent();
234
- else {
235
- deadlineTimer = setTimeout(spent, left);
236
- deadlineTimer.unref?.();
237
- }
238
- }
239
- });
240
- let lastError;
241
- for (const address of addresses) {
242
- signal?.throwIfAborted();
243
- const attemptTimeoutMs = addressAttemptBudgetMs(deadlineAt, Date.now());
244
- if (attemptTimeoutMs <= 0) {
245
- lastError ??= new RemotePeerError('offline', `Reaching ${host} ran out of time.`);
246
- break;
247
- }
248
- try {
249
- return await send(address, attemptTimeoutMs);
250
- }
251
- catch (error) {
252
- signal?.throwIfAborted();
253
- const failure = error instanceof RemotePeerError ? error : new RemotePeerError('network', String(error));
254
- if (failure.kind === 'offline' || failure.kind === 'network') {
255
- lastError = failure;
256
- continue;
257
- }
258
- throw failure;
259
- }
260
- }
261
- throw lastError ?? new RemotePeerError('network', `Could not reach ${host}.`);
262
- }
263
- export function peerWhoami(target, hostOrder, deadlineAt, signal) {
264
- return request(target, 'GET', '/api/whoami', undefined, hostOrder, deadlineAt, {}, signal);
265
- }
266
- export function peerProjects(target, hostOrder) {
267
- return request(target, 'GET', '/api/projects', undefined, hostOrder);
268
- }
269
- export function peerRoots(target, hostOrder) {
270
- return request(target, 'GET', '/api/roots', undefined, hostOrder);
271
- }
272
- export function peerProviderExport(target, hostOrder, deadlineAt, signal) {
273
- return request(target, 'GET', '/api/providers/export', undefined, hostOrder, deadlineAt, {}, signal);
274
- }
275
- export function peerBrowse(target, rootId, rel, hostOrder) {
276
- return request(target, 'GET', `/api/browse?root=${encodeURIComponent(rootId)}&rel=${encodeURIComponent(rel)}`, undefined, hostOrder);
277
- }
278
- export function peerOwnerRoots(target, hostOrder) {
279
- return request(target, 'GET', '/api/owner/roots', undefined, hostOrder);
280
- }
281
- export function peerOwnerBrowse(target, path, hostOrder) {
282
- return request(target, 'GET', `/api/owner/browse?path=${encodeURIComponent(path)}`, undefined, hostOrder);
283
- }
284
- export function peerOwnerAddProject(target, path, hostOrder) {
285
- return request(target, 'POST', '/api/owner/projects', { path }, hostOrder);
286
- }
287
- export async function capturePeerCertificate(host, port, transport, expectedFingerprint) {
288
- if (transport === 'http') {
289
- const address = await resolvePeerAddress(host).catch((error) => { throw new InviteRejectedError(error.message); });
290
- void address;
291
- return { host, certPem: '' };
292
- }
293
- const addresses = await resolvePeerAddresses(host).catch((error) => { throw new InviteRejectedError(error.message); });
294
- const captureFrom = async (address) => {
295
- const socket = await new Promise((resolve, reject) => {
296
- const connecting = tls.connect({
297
- host: address.address,
298
- port,
299
- ...(isIP(host) ? {} : { servername: host }),
300
- rejectUnauthorized: false,
301
- checkServerIdentity: () => undefined,
302
- timeout: REQUEST_TIMEOUT_MS,
303
- });
304
- connecting.once('secureConnect', () => resolve(connecting));
305
- connecting.once('error', (error) => reject(new InviteRejectedError(`Could not reach ${host}:${port} over TLS. ${error.message}`)));
306
- connecting.once('timeout', () => { connecting.destroy(); reject(new InviteRejectedError(`Connecting to ${host}:${port} timed out.`)); });
307
- });
308
- try {
309
- const certificate = socket.getPeerCertificate(true);
310
- const der = certificate?.raw;
311
- if (!der)
312
- throw new InviteRejectedError('The peer presented no certificate.');
313
- const presented = fingerprintOf(Buffer.from(der));
314
- if (presented !== expectedFingerprint) {
315
- throw new InviteRejectedError('The machine behind that pairing code presented a different certificate. The code may be mistyped, stale, or intercepted — nothing was paired.');
316
- }
317
- return pemFromDer(Buffer.from(der));
318
- }
319
- finally {
320
- socket.destroy();
321
- }
322
- };
323
- let lastError;
324
- for (const address of addresses) {
325
- try {
326
- return { host, certPem: await captureFrom(address) };
327
- }
328
- catch (error) {
329
- const failure = error instanceof Error ? error : new Error(String(error));
330
- if (failure instanceof InviteRejectedError && /different certificate/i.test(failure.message))
331
- throw failure;
332
- lastError = failure;
333
- }
334
- }
335
- throw lastError ?? new InviteRejectedError(`Could not reach ${host}:${port} over TLS.`);
336
- }
337
- export async function redeemPairingPlan(plan, options = {}) {
338
- let lastError;
339
- for (const host of plan.hosts) {
340
- try {
341
- const pinned = await capturePeerCertificate(host, plan.port, plan.transport, plan.fingerprint);
342
- const target = { host, port: plan.port, transport: plan.transport, fingerprint: plan.fingerprint, ...(plan.transport === 'https' ? { certPem: pinned.certPem } : {}) };
343
- const response = await request(target, 'POST', '/api/pair/redeem', { invite: plan.invite, label: options.label ?? osHostname() }, host);
344
- return { ...response, grant: response.grant === 'full' && plan.transport === 'https' ? 'full' : 'browse', host, certPem: pinned.certPem };
345
- }
346
- catch (error) {
347
- lastError = error instanceof Error ? error : new Error(String(error));
348
- if (error instanceof RemotePeerError && (error.kind === 'rejected' || error.kind === 'identity' || error.kind === 'unauthorized'))
349
- throw error;
350
- if (error instanceof InviteRejectedError && /different certificate/i.test(error.message))
351
- throw error;
352
- }
353
- }
354
- throw lastError ?? new InviteRejectedError('None of the addresses in that pairing code could be reached.');
355
- }
356
- export function newWorkspaceIdForClient() {
357
- return `ws-${randomBytes(6).toString('hex')}`;
358
- }
359
- const MAX_ENGINE_RESPONSE_BYTES = 12 * 1024 * 1024;
360
- export function peerEngineRequest(target, call, hostOrder, deadlineAt) {
361
- return request(target, 'POST', '/api/engine/request', call, hostOrder, deadlineAt, { maxResponseBytes: MAX_ENGINE_RESPONSE_BYTES });
362
- }
363
- const EVENTS_INACTIVITY_MS = 45_000;
364
- export function peerEngineEvents(target, host, connectionId, viewerWorkspaceId, onEvent, signal) {
365
- return new Promise((resolve, reject) => {
366
- let settled = false;
367
- let req;
368
- let inactivity;
369
- const armInactivity = () => {
370
- clearTimeout(inactivity);
371
- inactivity = setTimeout(() => { req?.destroy(new RemotePeerError('network', 'The event stream went quiet.')); }, EVENTS_INACTIVITY_MS);
372
- inactivity.unref?.();
373
- };
374
- const cleanup = () => {
375
- clearTimeout(inactivity);
376
- signal.removeEventListener('abort', onAbort);
377
- req?.destroy();
378
- };
379
- const finish = (outcome) => { if (!settled) {
380
- settled = true;
381
- cleanup();
382
- resolve(outcome);
383
- } };
384
- const fail = (error) => { if (!settled) {
385
- settled = true;
386
- cleanup();
387
- reject(error);
388
- } };
389
- function onAbort() {
390
- finish({ kind: 'ended' });
391
- }
392
- signal.addEventListener('abort', onAbort);
393
- void (async () => {
394
- let addresses;
395
- try {
396
- addresses = await resolvePeerAddresses(host);
397
- }
398
- catch (error) {
399
- fail(new RemotePeerError('offline', `Could not resolve ${host}: ${error.message}`));
400
- return;
401
- }
402
- const certPem = target.certPem;
403
- if (target.transport === 'https' && !certPem) {
404
- fail(new RemotePeerError('network', 'No pinned certificate for this workspace — it must be re-paired.'));
405
- return;
406
- }
407
- const query = `?connectionId=${encodeURIComponent(connectionId)}${viewerWorkspaceId !== undefined ? `&viewerWorkspaceId=${encodeURIComponent(viewerWorkspaceId)}` : ''}`;
408
- let lastReachability;
409
- for (const address of addresses) {
410
- if (settled || signal.aborted)
411
- return;
412
- const outcome = await new Promise(perAttempt => {
413
- let identityError;
414
- const options = {
415
- host: address.address,
416
- port: target.port,
417
- method: 'GET',
418
- path: `/api/engine/events${query}`,
419
- ...(target.transport === 'https' ? {} : { agent: false }),
420
- headers: {
421
- ...(target.token ? { authorization: `Bearer ${target.token}` } : {}),
422
- accept: 'text/event-stream',
423
- host: `${target.host}:${target.port}`,
424
- },
425
- timeout: REQUEST_TIMEOUT_MS,
426
- };
427
- if (target.transport === 'https' && certPem) {
428
- const tlsOptions = {
429
- host: address.address,
430
- port: target.port,
431
- ...(isIP(host) ? {} : { servername: host }),
432
- ca: [certPem],
433
- rejectUnauthorized: true,
434
- checkServerIdentity: () => undefined,
435
- timeout: REQUEST_TIMEOUT_MS,
436
- };
437
- Object.assign(options, tlsOptions);
438
- options.createConnection = (_connectOptions, onCreated) => {
439
- const socket = tls.connect(tlsOptions);
440
- let gated = false;
441
- socket.once('secureConnect', () => {
442
- if (gated)
443
- return;
444
- gated = true;
445
- const der = socket.getPeerCertificate(true)?.raw;
446
- if (!der) {
447
- identityError = new RemotePeerError('identity', 'The peer presented no certificate.');
448
- socket.destroy();
449
- onCreated(identityError);
450
- return;
451
- }
452
- if (fingerprintOf(Buffer.from(der)) !== target.fingerprint) {
453
- identityError = new RemotePeerError('identity', 'The peer presented a different certificate than the one you paired. Re-pairing is required.');
454
- socket.destroy();
455
- onCreated(identityError);
456
- return;
457
- }
458
- socket.setTimeout(0);
459
- onCreated(null, socket);
460
- });
461
- socket.once('error', (error) => {
462
- if (gated)
463
- return;
464
- gated = true;
465
- onCreated(error);
466
- });
467
- socket.on('timeout', () => {
468
- socket.destroy();
469
- if (!gated) {
470
- gated = true;
471
- onCreated(new RemotePeerError('offline', `Connecting to ${host}:${target.port} timed out.`));
472
- }
473
- });
474
- };
475
- }
476
- const attempt = (target.transport === 'https' ? httpsRequest : httpRequest)(options, res => {
477
- const status = res.statusCode ?? 0;
478
- if (status === 401) {
479
- res.resume();
480
- perAttempt(new RemotePeerError('unauthorized', 'This device is no longer authorized on that workspace.'));
481
- return;
482
- }
483
- if (status >= 500) {
484
- res.resume();
485
- perAttempt(new RemotePeerError('network', `The peer's event stream failed (${status}).`));
486
- return;
487
- }
488
- if (status >= 400) {
489
- res.resume();
490
- perAttempt(new RemotePeerError('rejected', `The peer refused the event stream (${status}).`));
491
- return;
492
- }
493
- if (status >= 300) {
494
- res.resume();
495
- perAttempt(new RemotePeerError('network', 'The peer tried to redirect the event stream, which remote access does not follow.'));
496
- return;
497
- }
498
- let buffer = '';
499
- armInactivity();
500
- res.setEncoding('utf8');
501
- res.on('data', (chunk) => {
502
- armInactivity();
503
- buffer += chunk;
504
- let boundary = buffer.indexOf('\n\n');
505
- while (boundary >= 0) {
506
- const frame = buffer.slice(0, boundary);
507
- buffer = buffer.slice(boundary + 2);
508
- for (const line of frame.split('\n')) {
509
- if (!line.startsWith('data: '))
510
- continue;
511
- try {
512
- const parsed = JSON.parse(line.slice(6));
513
- if (typeof parsed.engine === 'string' && typeof parsed.event === 'string') {
514
- onEvent({ engine: parsed.engine, event: parsed.event, data: (parsed.data ?? {}) });
515
- }
516
- }
517
- catch { }
518
- }
519
- boundary = buffer.indexOf('\n\n');
520
- }
521
- });
522
- res.on('end', () => perAttempt({ kind: 'ended' }));
523
- res.on('error', (error) => perAttempt(new RemotePeerError('network', error.message)));
524
- res.on('close', () => perAttempt({ kind: 'ended' }));
525
- });
526
- req = attempt;
527
- attempt.on('error', (error) => perAttempt(classifyFailure(error, identityError)));
528
- attempt.end();
529
- });
530
- if (outcome instanceof RemotePeerError) {
531
- if (outcome.kind === 'offline' || outcome.kind === 'network') {
532
- lastReachability = outcome;
533
- continue;
534
- }
535
- fail(outcome);
536
- return;
537
- }
538
- finish(outcome);
539
- return;
540
- }
541
- fail(lastReachability ?? new RemotePeerError('network', `Could not reach ${host}.`));
542
- })();
543
- });
544
- }
@@ -1,4 +0,0 @@
1
- export declare const REMOTE_USAGE = "Usage: andorra remote serve [--port N] [--insecure-http] [--rotate-cert]\n andorra remote status\n andorra remote code\n andorra remote devices | andorra remote revoke <device-id>\n andorra remote share <path> [--label name] | andorra remote unshare <share-id>\n andorra remote pair <pairing-code> [--name nickname]\n andorra remote workspaces | andorra remote unpair <workspace-id>\n andorra remote rotate-cert";
2
- export declare function runRemoteCommand(args: string[], print: (line: string) => void, dependencies?: {
3
- version?: string;
4
- }): Promise<number>;