@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.
- package/dist/src/acp-agent.d.ts +6 -0
- package/dist/src/acp-agent.js +15 -4
- package/dist/src/agent-execution.js +108 -19
- package/dist/src/agent-mcp-catalog.js +1 -0
- package/dist/src/agent-mcp.d.ts +3 -0
- package/dist/src/agent-mcp.js +33 -1
- package/dist/src/andorra/entry.d.ts +10 -0
- package/dist/src/andorra/entry.js +18 -4
- package/dist/src/andorra/errors.js +3 -0
- package/dist/src/andorra/hosted.d.ts +2 -1
- package/dist/src/andorra/hosted.js +2 -1
- package/dist/src/andorra/notices.d.ts +5 -0
- package/dist/src/andorra/notices.js +7 -0
- package/dist/src/andorra/state.d.ts +3 -2
- package/dist/src/andorra/state.js +4 -4
- package/dist/src/andorra/terminal.js +7 -2
- package/dist/src/anthropic-credentials.js +2 -0
- package/dist/src/app-server/error.d.ts +5 -0
- package/dist/src/app-server/error.js +7 -0
- package/dist/src/app-server/protocol.d.ts +30 -44
- package/dist/src/app-server/protocol.js +5 -1
- package/dist/src/app-server/workspace.d.ts +108 -0
- package/dist/src/app-server/workspace.js +608 -11
- package/dist/src/app-server.d.ts +390 -33
- package/dist/src/app-server.js +3033 -565
- package/dist/src/attachments.d.ts +10 -0
- package/dist/src/attachments.js +102 -7
- package/dist/src/automation-tool.js +2 -1
- package/dist/src/bridge/cloud.d.ts +27 -0
- package/dist/src/bridge/cloud.js +187 -0
- package/dist/src/bridge/command.d.ts +30 -0
- package/dist/src/bridge/command.js +359 -0
- package/dist/src/bridge/control.d.ts +70 -0
- package/dist/src/bridge/control.js +238 -0
- package/dist/src/bridge/engines.d.ts +90 -0
- package/dist/src/bridge/engines.js +601 -0
- package/dist/src/bridge/github.d.ts +108 -0
- package/dist/src/bridge/github.js +378 -0
- package/dist/src/bridge/host-ops.d.ts +9 -0
- package/dist/src/bridge/host-ops.js +139 -0
- package/dist/src/bridge/host.d.ts +47 -0
- package/dist/src/bridge/host.js +543 -0
- package/dist/src/bridge/link.d.ts +38 -0
- package/dist/src/bridge/link.js +508 -0
- package/dist/src/bridge/login-link.d.ts +29 -0
- package/dist/src/bridge/login-link.js +54 -0
- package/dist/src/bridge/protocol.d.ts +136 -0
- package/dist/src/bridge/protocol.js +11 -0
- package/dist/src/bridge/store.d.ts +12 -0
- package/dist/src/bridge/store.js +61 -0
- package/dist/src/bridge/wording.d.ts +6 -0
- package/dist/src/bridge/wording.js +10 -0
- package/dist/src/browser-server.js +7 -1
- package/dist/src/browser-ui/tokens.css +1 -1
- package/dist/src/bug-report-attachments.d.ts +48 -0
- package/dist/src/bug-report-attachments.js +327 -0
- package/dist/src/bug-report.d.ts +23 -3
- package/dist/src/bug-report.js +72 -9
- package/dist/src/change-authors.d.ts +41 -0
- package/dist/src/change-authors.js +146 -0
- package/dist/src/cli.d.ts +1 -3
- package/dist/src/cli.js +191 -35
- package/dist/src/codex-provider.js +1 -1
- package/dist/src/coding-tools.js +24 -17
- package/dist/src/command-screens.d.ts +6 -0
- package/dist/src/command-screens.js +14 -11
- package/dist/src/commands.d.ts +15 -3
- package/dist/src/commands.js +2 -1
- package/dist/src/compaction-phrases.js +1 -1
- package/dist/src/completion.js +89 -8
- package/dist/src/config.d.ts +71 -3
- package/dist/src/config.js +205 -78
- package/dist/src/context-budget.js +15 -2
- package/dist/src/deadline.js +13 -1
- package/dist/src/decision/DecisionEngine.d.ts +2 -0
- package/dist/src/decision/DecisionManager.d.ts +11 -0
- package/dist/src/decision/DecisionManager.js +36 -0
- package/dist/src/decision/applyTaskKind.d.ts +8 -0
- package/dist/src/decision/applyTaskKind.js +53 -0
- package/dist/src/decision/checks.js +1 -1
- package/dist/src/decision/hostedPolicy.js +2 -1
- package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +2 -0
- package/dist/src/decision/providers/AndorraDecisionProvider.js +12 -0
- package/dist/src/decision/requestPhrases.js +0 -1
- package/dist/src/decision/taskKind.d.ts +52 -0
- package/dist/src/decision/taskKind.js +76 -0
- package/dist/src/decision/types.d.ts +1 -1
- package/dist/src/default-graph.js +1 -1
- package/dist/src/efficiency.d.ts +5 -0
- package/dist/src/efficiency.js +5 -1
- package/dist/src/effort-routing.d.ts +27 -0
- package/dist/src/effort-routing.js +34 -0
- package/dist/src/evidence.d.ts +110 -0
- package/dist/src/evidence.js +195 -0
- package/dist/src/file-diff.d.ts +4 -0
- package/dist/src/file-diff.js +2 -1
- package/dist/src/github-app/agent-writes.d.ts +102 -0
- package/dist/src/github-app/agent-writes.js +320 -0
- package/dist/src/github-app/client.d.ts +368 -0
- package/dist/src/github-app/client.js +830 -0
- package/dist/src/github-app/event-source.d.ts +208 -0
- package/dist/src/github-app/event-source.js +684 -0
- package/dist/src/github-app/metrics.d.ts +32 -0
- package/dist/src/github-app/metrics.js +49 -0
- package/dist/src/github-app/review-loop.d.ts +274 -0
- package/dist/src/github-app/review-loop.js +1177 -0
- package/dist/src/github-app/review-node.d.ts +81 -0
- package/dist/src/github-app/review-node.js +205 -0
- package/dist/src/github-app/review-post.d.ts +73 -0
- package/dist/src/github-app/review-post.js +139 -0
- package/dist/src/github-app/review-text.d.ts +107 -0
- package/dist/src/github-app/review-text.js +1133 -0
- package/dist/src/github-app/review-threads.d.ts +47 -0
- package/dist/src/github-app/review-threads.js +107 -0
- package/dist/src/github-app/reviewer.d.ts +121 -0
- package/dist/src/github-app/reviewer.js +598 -0
- package/dist/src/github-app/store.d.ts +59 -0
- package/dist/src/github-app/store.js +148 -0
- package/dist/src/github-app/triggers.d.ts +141 -0
- package/dist/src/github-app/triggers.js +441 -0
- package/dist/src/github-names.d.ts +15 -0
- package/dist/src/github-names.js +15 -0
- package/dist/src/glob-search.d.ts +61 -0
- package/dist/src/glob-search.js +180 -0
- package/dist/src/graph-health.js +6 -1
- package/dist/src/graph-manager-chat.js +1 -1
- package/dist/src/graph-preview.d.ts +48 -0
- package/dist/src/graph-preview.js +56 -0
- package/dist/src/graph-provider-portability.js +2 -1
- package/dist/src/graph-runner.js +65 -4
- package/dist/src/graph-server.js +7 -5
- package/dist/src/graph-sync.js +3 -3
- package/dist/src/graph-ui/app.js +121 -7
- package/dist/src/graph-ui/icons.js +2 -1
- package/dist/src/graph-ui/studio.js +15 -1
- package/dist/src/graph-ui/tokens.css +1 -1
- package/dist/src/graphs.d.ts +40 -1
- package/dist/src/graphs.js +164 -15
- package/dist/src/hardened-runtime.js +32 -21
- package/dist/src/image-input.d.ts +16 -0
- package/dist/src/image-input.js +41 -0
- package/dist/src/membership.d.ts +10 -0
- package/dist/src/membership.js +19 -4
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +6 -0
- package/dist/src/model-metadata.js +35 -15
- package/dist/src/model-override.d.ts +2 -1
- package/dist/src/model-override.js +9 -0
- package/dist/src/model-relay/local.d.ts +116 -0
- package/dist/src/model-relay/local.js +468 -0
- package/dist/src/model-relay/remote.d.ts +129 -0
- package/dist/src/model-relay/remote.js +252 -0
- package/dist/src/model-relay/runs.d.ts +34 -0
- package/dist/src/model-relay/runs.js +146 -0
- package/dist/src/model-relay/wire.d.ts +5 -0
- package/dist/src/model-relay/wire.js +5 -0
- package/dist/src/model-tiers.d.ts +3 -0
- package/dist/src/model-tiers.js +3 -3
- package/dist/src/project-registry.d.ts +21 -0
- package/dist/src/project-registry.js +74 -0
- package/dist/src/provider-engine.d.ts +13 -1
- package/dist/src/provider-engine.js +74 -0
- package/dist/src/provider-import.d.ts +0 -12
- package/dist/src/provider-import.js +7 -195
- package/dist/src/providers.d.ts +28 -2
- package/dist/src/providers.js +59 -12
- package/dist/src/pull-request-logs.d.ts +12 -1
- package/dist/src/pull-request-logs.js +14 -3
- package/dist/src/pull-requests.d.ts +338 -8
- package/dist/src/pull-requests.js +482 -53
- package/dist/src/react-runtime.js +99 -16
- package/dist/src/remote/engine-pool.d.ts +88 -4
- package/dist/src/remote/engine-pool.js +367 -90
- package/dist/src/remote/roots.d.ts +19 -0
- package/dist/src/remote/roots.js +45 -7
- package/dist/src/remote/types.d.ts +0 -44
- package/dist/src/run-control.d.ts +7 -0
- package/dist/src/run-control.js +23 -0
- package/dist/src/run-records.d.ts +7 -2
- package/dist/src/run-records.js +71 -6
- package/dist/src/runtime.d.ts +34 -2
- package/dist/src/runtime.js +18 -2
- package/dist/src/session-changes.d.ts +35 -0
- package/dist/src/session-changes.js +194 -6
- package/dist/src/session-monitor.js +2 -1
- package/dist/src/session-reaper.d.ts +1 -1
- package/dist/src/session-reaper.js +6 -1
- package/dist/src/session-summary.d.ts +2 -1
- package/dist/src/session-summary.js +2 -1
- package/dist/src/sessions.d.ts +35 -1
- package/dist/src/sessions.js +70 -4
- package/dist/src/skills/bundled/bug-report/SKILL.md +51 -7
- package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
- package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
- package/dist/src/skills/bundled/provenance.json +3 -3
- package/dist/src/skills/runtime-skills.js +1 -1
- package/dist/src/task-checkout.d.ts +141 -0
- package/dist/src/task-checkout.js +558 -0
- package/dist/src/telemetry/emitter.d.ts +15 -0
- package/dist/src/telemetry/emitter.js +35 -1
- package/dist/src/telemetry/schema.d.ts +25 -2
- package/dist/src/telemetry/schema.js +52 -4
- package/dist/src/telemetry/surface.d.ts +2 -1
- package/dist/src/telemetry/surface.js +4 -1
- package/dist/src/terminal-ui.d.ts +1 -0
- package/dist/src/terminal-ui.js +10 -3
- package/dist/src/test-edit-guard.d.ts +37 -0
- package/dist/src/test-edit-guard.js +216 -3
- package/dist/src/thinking-level-detect.d.ts +11 -0
- package/dist/src/thinking-level-detect.js +47 -0
- package/dist/src/thinking-levels.d.ts +30 -0
- package/dist/src/thinking-levels.js +124 -0
- package/dist/src/timers.d.ts +3 -0
- package/dist/src/tool-catalog.js +1 -1
- package/dist/src/tools.js +59 -9
- package/dist/src/transcript.js +1 -1
- package/dist/src/types.d.ts +34 -0
- package/package.json +6 -1
- package/dist/src/remote/cert.d.ts +0 -11
- package/dist/src/remote/cert.js +0 -87
- package/dist/src/remote/client.d.ts +0 -119
- package/dist/src/remote/client.js +0 -544
- package/dist/src/remote/command.d.ts +0 -4
- package/dist/src/remote/command.js +0 -194
- package/dist/src/remote/errors.d.ts +0 -11
- package/dist/src/remote/errors.js +0 -14
- package/dist/src/remote/network.d.ts +0 -22
- package/dist/src/remote/network.js +0 -102
- package/dist/src/remote/pairing.d.ts +0 -63
- package/dist/src/remote/pairing.js +0 -299
- package/dist/src/remote/pem.d.ts +0 -2
- package/dist/src/remote/pem.js +0 -7
- package/dist/src/remote/serve.d.ts +0 -68
- package/dist/src/remote/serve.js +0 -244
- package/dist/src/remote/server.d.ts +0 -21
- package/dist/src/remote/server.js +0 -406
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { AppServerError } from '../app-server/error.js';
|
|
3
|
+
import { CLIENT_DEAD_MS, CLIENT_PING_MS, CLOSE, MAX_FRAME_BYTES, PROTOCOL_VERSION } from './protocol.js';
|
|
4
|
+
import { bothBridgeCommands } from './wording.js';
|
|
5
|
+
const RELAY_REASONS = new Set(['offline', 'not-found', 'slow', 'unlinked', 'limit']);
|
|
6
|
+
const HOST_ERROR_CODES = new Set(['not-found', 'invalid-params', 'forbidden']);
|
|
7
|
+
/** A relay socket that has not opened by then is abandoned and the dial fails as a network error. */
|
|
8
|
+
export const CONNECT_TIMEOUT_MS = 10_000;
|
|
9
|
+
const READY_TIMEOUT_MS = 15_000;
|
|
10
|
+
const OPEN_TIMEOUT_MS = 30_000;
|
|
11
|
+
const PRESENCE_TIMEOUT_MS = 10_000;
|
|
12
|
+
const DIAL_ATTEMPTS = 3;
|
|
13
|
+
const REFRESH_LEAD_MS = 300_000;
|
|
14
|
+
const MIN_REFRESH_GAP_MS = 30_000;
|
|
15
|
+
const REFRESH_RETRY_MS = 15_000;
|
|
16
|
+
const UNKNOWN_EXPIRY_REFRESH_MS = 10 * 60_000;
|
|
17
|
+
const SOCKET_OPEN = 1;
|
|
18
|
+
const LOST_MESSAGE = 'The connection to Andorra\'s relay was lost.';
|
|
19
|
+
const TIMEOUT_MESSAGE = 'The machine did not answer in time.';
|
|
20
|
+
const CLOSED_ERRORS = {
|
|
21
|
+
offline: { code: 'network', message: 'That machine went offline.' },
|
|
22
|
+
'not-found': { code: 'not-found', message: `That machine is not connected to Andorra. Start the bridge on it: ${bothBridgeCommands('run')}.` },
|
|
23
|
+
slow: { code: 'network', message: 'This connection fell behind the machine\'s updates and was closed.' },
|
|
24
|
+
unlinked: { code: 'not-found', message: 'That machine was removed from your account.' },
|
|
25
|
+
limit: { code: 'busy', message: 'Too many connections to other machines are open in this app. Close some and try again.' },
|
|
26
|
+
relay: { code: 'network', message: LOST_MESSAGE },
|
|
27
|
+
};
|
|
28
|
+
/** A link, request or host call the relay ended for `reason`. */
|
|
29
|
+
export class RelayClosedError extends AppServerError {
|
|
30
|
+
reason;
|
|
31
|
+
constructor(reason) {
|
|
32
|
+
super(CLOSED_ERRORS[reason].code, CLOSED_ERRORS[reason].message);
|
|
33
|
+
this.reason = reason;
|
|
34
|
+
this.name = 'RelayClosedError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const isRecord = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
38
|
+
const ENGINE_VERSION = /^\d{1,4}\.\d{1,4}\.\d{1,4}$/;
|
|
39
|
+
const keyOf = (env, connectionId) => `${env}\n${connectionId}`;
|
|
40
|
+
function hostOf(value) {
|
|
41
|
+
if (!isRecord(value))
|
|
42
|
+
return undefined;
|
|
43
|
+
const { hostname, platform, version } = value;
|
|
44
|
+
if (typeof hostname !== 'string' || typeof platform !== 'string' || typeof version !== 'string')
|
|
45
|
+
return undefined;
|
|
46
|
+
return { hostname, platform, version };
|
|
47
|
+
}
|
|
48
|
+
function engineFrameOf(value) {
|
|
49
|
+
if (!isRecord(value) || typeof value.engine !== 'string' || typeof value.event !== 'string' || !isRecord(value.data))
|
|
50
|
+
return undefined;
|
|
51
|
+
return { engine: value.engine, event: value.event, data: value.data };
|
|
52
|
+
}
|
|
53
|
+
/** Milliseconds since the epoch at which a JWT expires, or undefined when it cannot be read. */
|
|
54
|
+
function tokenExpiry(token) {
|
|
55
|
+
try {
|
|
56
|
+
const payload = JSON.parse(Buffer.from(token.split('.')[1] ?? '', 'base64url').toString('utf8'));
|
|
57
|
+
return isRecord(payload) && typeof payload.exp === 'number' ? payload.exp * 1000 : undefined;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function failure(error) {
|
|
64
|
+
if (error instanceof AppServerError)
|
|
65
|
+
return error;
|
|
66
|
+
return new AppServerError('auth', `Could not read your Andorra sign-in: ${error instanceof Error ? error.message : String(error)}`);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The desktop engine's one socket to Andorra's relay, shared by every machine it reaches. It is dialled on first use
|
|
70
|
+
* and again on the first call after it drops.
|
|
71
|
+
*/
|
|
72
|
+
export class RelayLink {
|
|
73
|
+
#options;
|
|
74
|
+
#socket;
|
|
75
|
+
#connecting;
|
|
76
|
+
#handshake;
|
|
77
|
+
#token = '';
|
|
78
|
+
#refreshTimer;
|
|
79
|
+
#pingTimer;
|
|
80
|
+
#deadTimer;
|
|
81
|
+
#lastFrameAt = 0;
|
|
82
|
+
#disposed = false;
|
|
83
|
+
#pending = new Map();
|
|
84
|
+
#opening = new Map();
|
|
85
|
+
#links = new Map();
|
|
86
|
+
#presence = new Set();
|
|
87
|
+
constructor(options) {
|
|
88
|
+
this.#options = options;
|
|
89
|
+
}
|
|
90
|
+
async open(env, connectionId, viewerWorkspaceId, onEvent, onClosed) {
|
|
91
|
+
const socket = await this.#ensure();
|
|
92
|
+
const key = keyOf(env, connectionId);
|
|
93
|
+
this.#opening.get(key)?.reject(new AppServerError('cancelled', 'A newer connection to that machine replaced this one.'));
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
|
+
const done = () => { clearTimeout(timer); if (this.#opening.get(key) === opening)
|
|
96
|
+
this.#opening.delete(key); };
|
|
97
|
+
const opening = {
|
|
98
|
+
env,
|
|
99
|
+
resolve: host => { done(); this.#links.set(key, { env, onEvent, onClosed }); resolve({ host }); },
|
|
100
|
+
reject: error => { done(); reject(error); },
|
|
101
|
+
};
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
opening.reject(new AppServerError('network', TIMEOUT_MESSAGE));
|
|
104
|
+
this.#send({ t: 'close', env, connectionId });
|
|
105
|
+
}, OPEN_TIMEOUT_MS);
|
|
106
|
+
timer.unref?.();
|
|
107
|
+
this.#opening.set(key, opening);
|
|
108
|
+
this.#links.delete(key);
|
|
109
|
+
if (!this.#send({ t: 'open', env, connectionId, ...(viewerWorkspaceId !== undefined ? { viewerWorkspaceId } : {}) }, socket)) {
|
|
110
|
+
opening.reject(new AppServerError('network', LOST_MESSAGE));
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async request(env, body, deadlineAt) {
|
|
115
|
+
return await this.#call(env, 'request', deadlineAt, id => ({ t: 'request', env, id, body }));
|
|
116
|
+
}
|
|
117
|
+
host(env, op, params, deadlineAt) {
|
|
118
|
+
return this.#call(env, 'host', deadlineAt, id => ({ t: 'host', env, id, op, params }));
|
|
119
|
+
}
|
|
120
|
+
close(env, connectionId) {
|
|
121
|
+
const key = keyOf(env, connectionId);
|
|
122
|
+
this.#links.delete(key);
|
|
123
|
+
this.#opening.get(key)?.reject(new AppServerError('cancelled', 'The connection to that machine was closed.'));
|
|
124
|
+
this.#send({ t: 'close', env, connectionId });
|
|
125
|
+
}
|
|
126
|
+
async presence() {
|
|
127
|
+
const socket = await this.#ensure();
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
const done = () => { clearTimeout(timer); this.#presence.delete(waiter); };
|
|
130
|
+
const waiter = { resolve: (online) => { done(); resolve(online); }, reject: (error) => { done(); reject(error); } };
|
|
131
|
+
const timer = setTimeout(() => waiter.reject(new AppServerError('network', 'Andorra\'s relay did not answer in time.')), PRESENCE_TIMEOUT_MS);
|
|
132
|
+
timer.unref?.();
|
|
133
|
+
this.#presence.add(waiter);
|
|
134
|
+
if (!this.#send({ t: 'presence' }, socket))
|
|
135
|
+
waiter.reject(new AppServerError('network', LOST_MESSAGE));
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** Ends the link for good. Pending calls reject with `error`: by default, that the engine is shutting down. */
|
|
139
|
+
dispose(error = new AppServerError('cancelled', 'The engine is shutting down.')) {
|
|
140
|
+
if (this.#disposed)
|
|
141
|
+
return;
|
|
142
|
+
this.#disposed = true;
|
|
143
|
+
const socket = this.#socket ?? this.#handshake;
|
|
144
|
+
this.#socket = undefined;
|
|
145
|
+
this.#handshake = undefined;
|
|
146
|
+
// Closing the engine is not a lost link: nobody is told to reconnect.
|
|
147
|
+
this.#links.clear();
|
|
148
|
+
this.#stopHeartbeat();
|
|
149
|
+
this.#fail(error);
|
|
150
|
+
try {
|
|
151
|
+
socket?.close(1000);
|
|
152
|
+
}
|
|
153
|
+
catch { }
|
|
154
|
+
}
|
|
155
|
+
#call(env, kind, deadlineAt, frame) {
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
const id = randomUUID();
|
|
158
|
+
let settled = false;
|
|
159
|
+
const finish = (settle) => {
|
|
160
|
+
if (settled)
|
|
161
|
+
return;
|
|
162
|
+
settled = true;
|
|
163
|
+
clearTimeout(timer);
|
|
164
|
+
if (this.#pending.get(id) === pending)
|
|
165
|
+
this.#pending.delete(id);
|
|
166
|
+
settle();
|
|
167
|
+
};
|
|
168
|
+
const pending = { env, kind, resolve: value => finish(() => resolve(value)), reject: error => finish(() => reject(error)) };
|
|
169
|
+
const timer = setTimeout(() => pending.reject(new AppServerError('network', TIMEOUT_MESSAGE)), Math.max(0, deadlineAt - Date.now()));
|
|
170
|
+
timer.unref?.();
|
|
171
|
+
this.#ensure().then(socket => {
|
|
172
|
+
if (settled)
|
|
173
|
+
return;
|
|
174
|
+
const text = JSON.stringify(frame(id));
|
|
175
|
+
if (Buffer.byteLength(text) > MAX_FRAME_BYTES) {
|
|
176
|
+
pending.reject(new AppServerError('invalid-params', 'That request is larger than the relay link allows.'));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
this.#pending.set(id, pending);
|
|
180
|
+
if (!this.#sendText(text, socket))
|
|
181
|
+
pending.reject(new AppServerError('network', LOST_MESSAGE));
|
|
182
|
+
}, (error) => pending.reject(failure(error)));
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
#ensure() {
|
|
186
|
+
if (this.#disposed)
|
|
187
|
+
return Promise.reject(new AppServerError('cancelled', 'The engine is shutting down.'));
|
|
188
|
+
if (this.#socket)
|
|
189
|
+
return Promise.resolve(this.#socket);
|
|
190
|
+
this.#connecting ??= this.#dial().finally(() => { this.#connecting = undefined; });
|
|
191
|
+
return this.#connecting;
|
|
192
|
+
}
|
|
193
|
+
async #dial() {
|
|
194
|
+
const backoff = this.#options.backoffMs ?? (attempt => Math.min(30_000, 1000 * 2 ** attempt));
|
|
195
|
+
for (let attempt = 0;; attempt++) {
|
|
196
|
+
let token;
|
|
197
|
+
try {
|
|
198
|
+
token = await this.#options.token();
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
throw failure(error);
|
|
202
|
+
}
|
|
203
|
+
if (this.#disposed)
|
|
204
|
+
throw new AppServerError('cancelled', 'The engine is shutting down.');
|
|
205
|
+
const version = await this.#engineVersion();
|
|
206
|
+
if (this.#disposed)
|
|
207
|
+
throw new AppServerError('cancelled', 'The engine is shutting down.');
|
|
208
|
+
const outcome = await this.#handshakeWith(token, version);
|
|
209
|
+
if ('socket' in outcome)
|
|
210
|
+
return outcome.socket;
|
|
211
|
+
if (this.#disposed)
|
|
212
|
+
throw new AppServerError('cancelled', 'The engine is shutting down.');
|
|
213
|
+
// 4401 is a refused token or a brief outage of the relay's key set, 4403 a token that ran out: fetch a fresh one and retry.
|
|
214
|
+
if (outcome.code !== CLOSE.unauthorized && outcome.code !== CLOSE.expired)
|
|
215
|
+
throw new AppServerError('network', 'Andorra\'s relay could not be reached.');
|
|
216
|
+
if (attempt + 1 >= DIAL_ATTEMPTS)
|
|
217
|
+
throw new AppServerError('auth', 'Andorra\'s relay did not accept your sign-in. Try again in a moment.');
|
|
218
|
+
await new Promise(resolve => { const timer = setTimeout(resolve, backoff(attempt)); timer.unref?.(); });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async #engineVersion() {
|
|
222
|
+
try {
|
|
223
|
+
const version = await this.#options.version?.();
|
|
224
|
+
return version !== undefined && ENGINE_VERSION.test(version) ? version : undefined;
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
#handshakeWith(token, version) {
|
|
231
|
+
return new Promise(resolve => {
|
|
232
|
+
let socket;
|
|
233
|
+
try {
|
|
234
|
+
socket = (this.#options.socket ?? (url => new WebSocket(url)))(this.#options.relayUrl);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
resolve({ code: undefined });
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
this.#handshake = socket;
|
|
241
|
+
let settled = false;
|
|
242
|
+
let readyTimer;
|
|
243
|
+
const connectTimer = setTimeout(() => { settle({ code: undefined }); try {
|
|
244
|
+
socket.close(1000);
|
|
245
|
+
}
|
|
246
|
+
catch { } }, CONNECT_TIMEOUT_MS);
|
|
247
|
+
connectTimer.unref?.();
|
|
248
|
+
const settle = (outcome) => {
|
|
249
|
+
if (settled)
|
|
250
|
+
return;
|
|
251
|
+
settled = true;
|
|
252
|
+
clearTimeout(connectTimer);
|
|
253
|
+
clearTimeout(readyTimer);
|
|
254
|
+
if (this.#handshake === socket)
|
|
255
|
+
this.#handshake = undefined;
|
|
256
|
+
resolve(outcome);
|
|
257
|
+
};
|
|
258
|
+
socket.addEventListener('open', () => {
|
|
259
|
+
if (settled)
|
|
260
|
+
return;
|
|
261
|
+
clearTimeout(connectTimer);
|
|
262
|
+
this.#sendText(JSON.stringify({ t: 'hello', v: PROTOCOL_VERSION, role: 'desktop', token, ...(version !== undefined ? { version } : {}) }), socket);
|
|
263
|
+
readyTimer = setTimeout(() => { settle({ code: undefined }); try {
|
|
264
|
+
socket.close(1000);
|
|
265
|
+
}
|
|
266
|
+
catch { } }, READY_TIMEOUT_MS);
|
|
267
|
+
readyTimer.unref?.();
|
|
268
|
+
});
|
|
269
|
+
socket.addEventListener('message', event => {
|
|
270
|
+
if (this.#socket === socket)
|
|
271
|
+
this.#lastFrameAt = Date.now();
|
|
272
|
+
const frame = this.#parse(event.data);
|
|
273
|
+
if (!frame)
|
|
274
|
+
return;
|
|
275
|
+
if (!settled) {
|
|
276
|
+
if (frame.t !== 'ready')
|
|
277
|
+
return;
|
|
278
|
+
if (this.#disposed) {
|
|
279
|
+
settle({ code: undefined });
|
|
280
|
+
try {
|
|
281
|
+
socket.close(1000);
|
|
282
|
+
}
|
|
283
|
+
catch { }
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
this.#socket = socket;
|
|
287
|
+
this.#token = token;
|
|
288
|
+
this.#scheduleRefresh(tokenExpiry(token));
|
|
289
|
+
this.#startHeartbeat(socket);
|
|
290
|
+
settle({ socket });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (this.#socket === socket)
|
|
294
|
+
this.#handle(frame);
|
|
295
|
+
});
|
|
296
|
+
socket.addEventListener('close', event => {
|
|
297
|
+
if (!settled) {
|
|
298
|
+
settle({ code: event.code });
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (this.#socket === socket)
|
|
302
|
+
this.#lost();
|
|
303
|
+
});
|
|
304
|
+
socket.addEventListener('error', () => {
|
|
305
|
+
if (settled)
|
|
306
|
+
return;
|
|
307
|
+
settle({ code: undefined });
|
|
308
|
+
try {
|
|
309
|
+
socket.close(1000);
|
|
310
|
+
}
|
|
311
|
+
catch { }
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
#parse(data) {
|
|
316
|
+
if (typeof data !== 'string' || Buffer.byteLength(data) > MAX_FRAME_BYTES)
|
|
317
|
+
return undefined;
|
|
318
|
+
let frame;
|
|
319
|
+
try {
|
|
320
|
+
frame = JSON.parse(data);
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
return isRecord(frame) && typeof frame.t === 'string' ? frame : undefined;
|
|
326
|
+
}
|
|
327
|
+
#handle(frame) {
|
|
328
|
+
const { t, env, connectionId, id } = frame;
|
|
329
|
+
if (t === 'pong')
|
|
330
|
+
return;
|
|
331
|
+
if (t === 'presence') {
|
|
332
|
+
if (!Array.isArray(frame.online))
|
|
333
|
+
return;
|
|
334
|
+
const online = frame.online.filter((entry) => typeof entry === 'string');
|
|
335
|
+
for (const waiter of [...this.#presence])
|
|
336
|
+
waiter.resolve(online);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (typeof env !== 'string')
|
|
340
|
+
return;
|
|
341
|
+
if (t === 'opened') {
|
|
342
|
+
const host = hostOf(frame.host);
|
|
343
|
+
if (typeof connectionId !== 'string' || !host)
|
|
344
|
+
return;
|
|
345
|
+
this.#opening.get(keyOf(env, connectionId))?.resolve(host);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (t === 'event') {
|
|
349
|
+
const engineFrame = engineFrameOf(frame.frame);
|
|
350
|
+
if (typeof connectionId !== 'string' || !engineFrame)
|
|
351
|
+
return;
|
|
352
|
+
this.#links.get(keyOf(env, connectionId))?.onEvent(engineFrame);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (t === 'answer') {
|
|
356
|
+
if (typeof id !== 'string')
|
|
357
|
+
return;
|
|
358
|
+
const pending = this.#pending.get(id);
|
|
359
|
+
if (!pending || pending.env !== env)
|
|
360
|
+
return;
|
|
361
|
+
this.#answer(pending, frame.answer);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (t === 'closed') {
|
|
365
|
+
const reason = frame.reason;
|
|
366
|
+
if (typeof reason !== 'string' || !RELAY_REASONS.has(reason))
|
|
367
|
+
return;
|
|
368
|
+
if (connectionId !== undefined && typeof connectionId !== 'string')
|
|
369
|
+
return;
|
|
370
|
+
this.#closed(env, connectionId, reason);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
#answer(pending, answer) {
|
|
374
|
+
if (!isRecord(answer)) {
|
|
375
|
+
pending.reject(new AppServerError('remote', 'The machine sent a malformed answer.'));
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const error = isRecord(answer.error) && typeof answer.error.code === 'string' && typeof answer.error.message === 'string'
|
|
379
|
+
? { code: answer.error.code, message: answer.error.message } : undefined;
|
|
380
|
+
if (pending.kind === 'request') {
|
|
381
|
+
pending.resolve(error ? { error } : { result: answer.result });
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (error)
|
|
385
|
+
pending.reject(new AppServerError(HOST_ERROR_CODES.has(error.code) ? error.code : 'remote', error.message));
|
|
386
|
+
else
|
|
387
|
+
pending.resolve(answer.result);
|
|
388
|
+
}
|
|
389
|
+
#closed(env, connectionId, reason) {
|
|
390
|
+
const ended = connectionId === undefined
|
|
391
|
+
? [...this.#links.keys(), ...this.#opening.keys()].filter(key => key.startsWith(`${env}\n`))
|
|
392
|
+
: [keyOf(env, connectionId)];
|
|
393
|
+
for (const key of ended) {
|
|
394
|
+
const opening = this.#opening.get(key);
|
|
395
|
+
if (opening) {
|
|
396
|
+
opening.reject(new RelayClosedError(reason));
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
const link = this.#links.get(key);
|
|
400
|
+
if (!link)
|
|
401
|
+
continue;
|
|
402
|
+
this.#links.delete(key);
|
|
403
|
+
link.onClosed(reason);
|
|
404
|
+
}
|
|
405
|
+
// A slow cut ends one event stream; the machine still answers. Every other reason means nothing more will come from it.
|
|
406
|
+
if (reason === 'slow')
|
|
407
|
+
return;
|
|
408
|
+
for (const pending of [...this.#pending.values()])
|
|
409
|
+
if (pending.env === env)
|
|
410
|
+
pending.reject(new RelayClosedError(reason));
|
|
411
|
+
}
|
|
412
|
+
/** Pings the relay every CLIENT_PING_MS and gives the socket up once it has carried nothing for CLIENT_DEAD_MS. */
|
|
413
|
+
#startHeartbeat(socket) {
|
|
414
|
+
this.#stopHeartbeat();
|
|
415
|
+
this.#lastFrameAt = Date.now();
|
|
416
|
+
this.#pingTimer = setInterval(() => { if (this.#socket === socket)
|
|
417
|
+
this.#send({ t: 'ping' }, socket); }, CLIENT_PING_MS);
|
|
418
|
+
this.#pingTimer.unref?.();
|
|
419
|
+
const watch = () => {
|
|
420
|
+
if (this.#socket !== socket)
|
|
421
|
+
return;
|
|
422
|
+
const quiet = Date.now() - this.#lastFrameAt;
|
|
423
|
+
if (quiet >= CLIENT_DEAD_MS) {
|
|
424
|
+
// The socket may still look open to the OS; nothing more will arrive on it.
|
|
425
|
+
this.#lost();
|
|
426
|
+
try {
|
|
427
|
+
socket.close(1000);
|
|
428
|
+
}
|
|
429
|
+
catch { }
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
this.#deadTimer = setTimeout(watch, CLIENT_DEAD_MS - quiet);
|
|
433
|
+
this.#deadTimer.unref?.();
|
|
434
|
+
};
|
|
435
|
+
watch();
|
|
436
|
+
}
|
|
437
|
+
#stopHeartbeat() {
|
|
438
|
+
clearInterval(this.#pingTimer);
|
|
439
|
+
clearTimeout(this.#deadTimer);
|
|
440
|
+
this.#pingTimer = undefined;
|
|
441
|
+
this.#deadTimer = undefined;
|
|
442
|
+
}
|
|
443
|
+
#lost() {
|
|
444
|
+
this.#socket = undefined;
|
|
445
|
+
this.#stopHeartbeat();
|
|
446
|
+
clearTimeout(this.#refreshTimer);
|
|
447
|
+
this.#refreshTimer = undefined;
|
|
448
|
+
this.#fail(new AppServerError('network', LOST_MESSAGE));
|
|
449
|
+
const links = [...this.#links.values()];
|
|
450
|
+
this.#links.clear();
|
|
451
|
+
for (const link of links)
|
|
452
|
+
link.onClosed('relay');
|
|
453
|
+
}
|
|
454
|
+
#fail(error) {
|
|
455
|
+
clearTimeout(this.#refreshTimer);
|
|
456
|
+
this.#refreshTimer = undefined;
|
|
457
|
+
for (const pending of [...this.#pending.values()])
|
|
458
|
+
pending.reject(error);
|
|
459
|
+
for (const opening of [...this.#opening.values()])
|
|
460
|
+
opening.reject(error);
|
|
461
|
+
for (const waiter of [...this.#presence])
|
|
462
|
+
waiter.reject(error);
|
|
463
|
+
}
|
|
464
|
+
#scheduleRefresh(expiresAt, delay) {
|
|
465
|
+
clearTimeout(this.#refreshTimer);
|
|
466
|
+
const wait = delay ?? (expiresAt === undefined ? UNKNOWN_EXPIRY_REFRESH_MS : Math.max(MIN_REFRESH_GAP_MS, expiresAt - REFRESH_LEAD_MS - Date.now()));
|
|
467
|
+
this.#refreshTimer = setTimeout(() => void this.#refresh(), wait);
|
|
468
|
+
this.#refreshTimer.unref?.();
|
|
469
|
+
}
|
|
470
|
+
async #refresh() {
|
|
471
|
+
const socket = this.#socket;
|
|
472
|
+
if (!socket)
|
|
473
|
+
return;
|
|
474
|
+
let token;
|
|
475
|
+
try {
|
|
476
|
+
token = await this.#options.token();
|
|
477
|
+
}
|
|
478
|
+
catch {
|
|
479
|
+
// A sign-in that cannot be renewed lets the relay end the socket at expiry; the next call then reports login-required.
|
|
480
|
+
if (this.#socket === socket)
|
|
481
|
+
this.#scheduleRefresh(undefined, REFRESH_RETRY_MS);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
if (this.#socket !== socket)
|
|
485
|
+
return;
|
|
486
|
+
// The token store only rotates the access token shortly before it expires, so an early ask can return the same one.
|
|
487
|
+
if (token !== this.#token && this.#send({ t: 'auth', token }, socket))
|
|
488
|
+
this.#token = token;
|
|
489
|
+
this.#scheduleRefresh(tokenExpiry(this.#token));
|
|
490
|
+
}
|
|
491
|
+
#send(frame, socket = this.#socket) {
|
|
492
|
+
const text = JSON.stringify(frame);
|
|
493
|
+
if (Buffer.byteLength(text) > MAX_FRAME_BYTES)
|
|
494
|
+
return false;
|
|
495
|
+
return this.#sendText(text, socket);
|
|
496
|
+
}
|
|
497
|
+
#sendText(text, socket) {
|
|
498
|
+
if (!socket || socket.readyState !== SOCKET_OPEN)
|
|
499
|
+
return false;
|
|
500
|
+
try {
|
|
501
|
+
socket.send(text);
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
catch {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** The code exactly as the service formats it, or a server error: nothing else may reach a printed link or an opener. */
|
|
2
|
+
export declare function checkedLinkCode(code: string): string;
|
|
3
|
+
export declare function linkAddress(code: string): string;
|
|
4
|
+
export declare function overSshFromEnvironment(environment: NodeJS.ProcessEnv): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* The opener for the platforms whose installed desktop app handles andorra-code:// (Windows and macOS). The Linux build
|
|
7
|
+
* (AppImage) registers no scheme, so there is none for Linux: the link is printed and the code typed in.
|
|
8
|
+
*/
|
|
9
|
+
export declare function openCommand(platform: NodeJS.Platform, address: string): {
|
|
10
|
+
command: string;
|
|
11
|
+
args: string[];
|
|
12
|
+
} | undefined;
|
|
13
|
+
interface OpenerProcess {
|
|
14
|
+
once(event: 'error', listener: () => void): unknown;
|
|
15
|
+
once(event: 'exit', listener: (code: number | null) => void): unknown;
|
|
16
|
+
kill(): unknown;
|
|
17
|
+
unref?(): unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface OpenOptions {
|
|
20
|
+
platform?: NodeJS.Platform;
|
|
21
|
+
spawn?: (command: string, args: string[], options: {
|
|
22
|
+
stdio: 'ignore';
|
|
23
|
+
windowsHide: boolean;
|
|
24
|
+
}) => OpenerProcess;
|
|
25
|
+
timeoutMs?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Hands the address to the operating system's opener (no shell). Resolves false when it could not be started, failed or took too long. */
|
|
28
|
+
export declare function openOnThisComputer(address: string, options?: OpenOptions): Promise<boolean>;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from 'node:child_process';
|
|
2
|
+
import { ControlError } from './control.js';
|
|
3
|
+
/** What the service issues: 8 characters of its code alphabet, shown as XXXX-XXXX (infra/andorra-auth/environments.py). */
|
|
4
|
+
const LINK_CODE = /^[ABCDEFGHJKMNPQRSTVWXYZ23456789]{4}-[ABCDEFGHJKMNPQRSTVWXYZ23456789]{4}$/;
|
|
5
|
+
const OPEN_TIMEOUT_MS = 3000;
|
|
6
|
+
/** The code exactly as the service formats it, or a server error: nothing else may reach a printed link or an opener. */
|
|
7
|
+
export function checkedLinkCode(code) {
|
|
8
|
+
if (!LINK_CODE.test(code))
|
|
9
|
+
throw new ControlError('server', 'The Andorra service answered with a link code in an unexpected form.');
|
|
10
|
+
return code;
|
|
11
|
+
}
|
|
12
|
+
export function linkAddress(code) {
|
|
13
|
+
return `andorra-code://link?code=${checkedLinkCode(code)}`;
|
|
14
|
+
}
|
|
15
|
+
export function overSshFromEnvironment(environment) {
|
|
16
|
+
return Boolean(environment.SSH_CONNECTION?.trim() || environment.SSH_TTY?.trim());
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The opener for the platforms whose installed desktop app handles andorra-code:// (Windows and macOS). The Linux build
|
|
20
|
+
* (AppImage) registers no scheme, so there is none for Linux: the link is printed and the code typed in.
|
|
21
|
+
*/
|
|
22
|
+
export function openCommand(platform, address) {
|
|
23
|
+
if (platform === 'win32')
|
|
24
|
+
return { command: 'rundll32', args: ['url.dll,FileProtocolHandler', address] };
|
|
25
|
+
if (platform === 'darwin')
|
|
26
|
+
return { command: 'open', args: [address] };
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Hands the address to the operating system's opener (no shell). Resolves false when it could not be started, failed or took too long. */
|
|
30
|
+
export function openOnThisComputer(address, options = {}) {
|
|
31
|
+
const opener = openCommand(options.platform ?? process.platform, address);
|
|
32
|
+
if (opener === undefined)
|
|
33
|
+
return Promise.resolve(false);
|
|
34
|
+
const { command, args } = opener;
|
|
35
|
+
const start = options.spawn ?? ((file, fileArgs, spawnOptions) => nodeSpawn(file, fileArgs, spawnOptions));
|
|
36
|
+
return new Promise(resolveOpen => {
|
|
37
|
+
let child;
|
|
38
|
+
try {
|
|
39
|
+
child = start(command, args, { stdio: 'ignore', windowsHide: true });
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
resolveOpen(false);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const timer = setTimeout(() => { try {
|
|
46
|
+
child.unref?.();
|
|
47
|
+
child.kill();
|
|
48
|
+
}
|
|
49
|
+
catch { /* already gone */ } resolveOpen(false); }, options.timeoutMs ?? OPEN_TIMEOUT_MS);
|
|
50
|
+
timer.unref?.();
|
|
51
|
+
child.once('error', () => { clearTimeout(timer); resolveOpen(false); });
|
|
52
|
+
child.once('exit', code => { clearTimeout(timer); resolveOpen(code === 0); });
|
|
53
|
+
});
|
|
54
|
+
}
|