agent-embassy 3.1.0 → 4.0.0

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 (112) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/CONTRIBUTING.md +92 -202
  3. package/README.md +157 -155
  4. package/SECURITY.md +191 -348
  5. package/dist/src/gateway/broker-check.d.ts +26 -0
  6. package/dist/src/gateway/broker-check.js +89 -0
  7. package/dist/src/gateway/broker-check.js.map +1 -0
  8. package/dist/src/gateway/broker-control.d.ts +72 -0
  9. package/dist/src/gateway/broker-control.js +164 -0
  10. package/dist/src/gateway/broker-control.js.map +1 -0
  11. package/dist/src/gateway/broker.d.ts +117 -0
  12. package/dist/src/gateway/broker.js +186 -0
  13. package/dist/src/gateway/broker.js.map +1 -0
  14. package/dist/src/gateway/claude-peer.d.ts +1 -124
  15. package/dist/src/gateway/claude-peer.js +61 -1052
  16. package/dist/src/gateway/claude-peer.js.map +1 -1
  17. package/dist/src/gateway/codex-stateless-transport.d.ts +0 -12
  18. package/dist/src/gateway/codex-stateless-transport.js +8 -142
  19. package/dist/src/gateway/codex-stateless-transport.js.map +1 -1
  20. package/dist/src/gateway/config.d.ts +2 -6
  21. package/dist/src/gateway/config.js +17 -27
  22. package/dist/src/gateway/config.js.map +1 -1
  23. package/dist/src/gateway/coordinator.d.ts +57 -0
  24. package/dist/src/gateway/coordinator.js +217 -0
  25. package/dist/src/gateway/coordinator.js.map +1 -0
  26. package/dist/src/gateway/core-cli.d.ts +15 -0
  27. package/dist/src/gateway/core-cli.js +282 -0
  28. package/dist/src/gateway/core-cli.js.map +1 -0
  29. package/dist/src/gateway/core-service-command.d.ts +16 -0
  30. package/dist/src/gateway/core-service-command.js +71 -0
  31. package/dist/src/gateway/core-service-command.js.map +1 -0
  32. package/dist/src/gateway/endpoint-directory.d.ts +33 -0
  33. package/dist/src/gateway/endpoint-directory.js +223 -0
  34. package/dist/src/gateway/endpoint-directory.js.map +1 -0
  35. package/dist/src/gateway/federation.d.ts +89 -0
  36. package/dist/src/gateway/federation.js +632 -0
  37. package/dist/src/gateway/federation.js.map +1 -0
  38. package/dist/src/gateway/ledger-codec.d.ts +3 -0
  39. package/dist/src/gateway/ledger-codec.js +159 -0
  40. package/dist/src/gateway/ledger-codec.js.map +1 -0
  41. package/dist/src/gateway/ledger.d.ts +138 -0
  42. package/dist/src/gateway/ledger.js +319 -0
  43. package/dist/src/gateway/ledger.js.map +1 -0
  44. package/dist/src/gateway/local-control.d.ts +23 -0
  45. package/dist/src/gateway/local-control.js +489 -0
  46. package/dist/src/gateway/local-control.js.map +1 -0
  47. package/dist/src/gateway/native-destinations.d.ts +34 -0
  48. package/dist/src/gateway/native-destinations.js +272 -0
  49. package/dist/src/gateway/native-destinations.js.map +1 -0
  50. package/dist/src/gateway/owned-state.d.ts +57 -0
  51. package/dist/src/gateway/owned-state.js +398 -0
  52. package/dist/src/gateway/owned-state.js.map +1 -0
  53. package/dist/src/gateway/provenance-envelope.d.ts +3 -3
  54. package/dist/src/gateway/provenance-envelope.js +3 -23
  55. package/dist/src/gateway/provenance-envelope.js.map +1 -1
  56. package/dist/src/gateway/runtime.d.ts +41 -0
  57. package/dist/src/gateway/runtime.js +175 -0
  58. package/dist/src/gateway/runtime.js.map +1 -0
  59. package/docs/CONFIGURATION.md +178 -266
  60. package/docs/DELIVERY.md +133 -82
  61. package/docs/GATEWAY-ARCHITECTURE.md +276 -949
  62. package/package.json +5 -5
  63. package/skills/embassy-peer/SKILL.md +38 -196
  64. package/skills/embassy-peer/agents/openai.yaml +2 -2
  65. package/dist/src/gateway/claude-helper-protocol.d.ts +0 -117
  66. package/dist/src/gateway/claude-helper-protocol.js +0 -122
  67. package/dist/src/gateway/claude-helper-protocol.js.map +0 -1
  68. package/dist/src/gateway/claude-helper-supervisor.d.ts +0 -87
  69. package/dist/src/gateway/claude-helper-supervisor.js +0 -374
  70. package/dist/src/gateway/claude-helper-supervisor.js.map +0 -1
  71. package/dist/src/gateway/claude-helper.d.ts +0 -1
  72. package/dist/src/gateway/claude-helper.js +0 -252
  73. package/dist/src/gateway/claude-helper.js.map +0 -1
  74. package/dist/src/gateway/cli.d.ts +0 -56
  75. package/dist/src/gateway/cli.js +0 -1532
  76. package/dist/src/gateway/cli.js.map +0 -1
  77. package/dist/src/gateway/codex-socket-holder.d.ts +0 -26
  78. package/dist/src/gateway/codex-socket-holder.js +0 -76
  79. package/dist/src/gateway/codex-socket-holder.js.map +0 -1
  80. package/dist/src/gateway/control.d.ts +0 -279
  81. package/dist/src/gateway/control.js +0 -933
  82. package/dist/src/gateway/control.js.map +0 -1
  83. package/dist/src/gateway/peer-client.d.ts +0 -58
  84. package/dist/src/gateway/peer-client.js +0 -217
  85. package/dist/src/gateway/peer-client.js.map +0 -1
  86. package/dist/src/gateway/peer-mailbox.d.ts +0 -78
  87. package/dist/src/gateway/peer-mailbox.js +0 -168
  88. package/dist/src/gateway/peer-mailbox.js.map +0 -1
  89. package/dist/src/gateway/peer-protocol.d.ts +0 -110
  90. package/dist/src/gateway/peer-protocol.js +0 -79
  91. package/dist/src/gateway/peer-protocol.js.map +0 -1
  92. package/dist/src/gateway/peer-stdio.d.ts +0 -23
  93. package/dist/src/gateway/peer-stdio.js +0 -100
  94. package/dist/src/gateway/peer-stdio.js.map +0 -1
  95. package/dist/src/gateway/providers.d.ts +0 -164
  96. package/dist/src/gateway/providers.js +0 -975
  97. package/dist/src/gateway/providers.js.map +0 -1
  98. package/dist/src/gateway/server.d.ts +0 -46
  99. package/dist/src/gateway/server.js +0 -216
  100. package/dist/src/gateway/server.js.map +0 -1
  101. package/dist/src/gateway/service.d.ts +0 -371
  102. package/dist/src/gateway/service.js +0 -2335
  103. package/dist/src/gateway/service.js.map +0 -1
  104. package/dist/src/gateway/status-view.d.ts +0 -119
  105. package/dist/src/gateway/status-view.js +0 -484
  106. package/dist/src/gateway/status-view.js.map +0 -1
  107. package/dist/src/gateway/store.d.ts +0 -188
  108. package/dist/src/gateway/store.js +0 -2360
  109. package/dist/src/gateway/store.js.map +0 -1
  110. package/dist/src/gateway/types.d.ts +0 -541
  111. package/dist/src/gateway/types.js +0 -332
  112. package/dist/src/gateway/types.js.map +0 -1
@@ -1,1532 +0,0 @@
1
- #!/usr/bin/env node
2
- /** Foreground broker plus bounded metadata-only control client. */
3
- import { randomBytes } from "node:crypto";
4
- import { realpathSync } from "node:fs";
5
- import { lstat, realpath } from "node:fs/promises";
6
- import { userInfo } from "node:os";
7
- import path from "node:path";
8
- import { fileURLToPath } from "node:url";
9
- import { BridgeError } from "../errors.js";
10
- import { GATEWAY_CONTROL_DEFAULT_TIMEOUT_MS, GATEWAY_CONTROL_MAX_MESSAGE_BYTES, GATEWAY_CONTROL_MAX_RESPONSE_BYTES, GATEWAY_CONTROL_PROTOCOL_VERSION, GatewayControlTransportError, isClaudeSessionSelector, isGatewayAlias, isGatewayConversationId, isGatewayDeliveryToken, isGatewayReplyAddress, sendGatewayControlRequest } from "./control.js";
11
- import { defaultGatewayStateDir, loadGatewayConfig } from "./config.js";
12
- import { isDefaultedGatewayNodeInventory, loadGatewayNodeInventory } from "./federation-nodes.js";
13
- import { runGatewayServer } from "./server.js";
14
- import { PeerHandlerError, runPeerStdio } from "./peer-stdio.js";
15
- import { boundedServiceDetail, defaultProbeHostLease, defaultRunLaunchctl, installServiceAgent, readOwnedSmallFile, recordedPid, serviceAgentStatus, uninstallServiceAgent } from "./service-agent.js";
16
- import { GATEWAY_CONTROLLER_LOCK_FILE } from "./store.js";
17
- import { diffWatch, emptyWatchState, renderStatus, renderWatchEvent, STATUS_RECENT, STATUS_ROUTE_STALE_AFTER_MS, terminalPainter } from "./status-view.js";
18
- const THREAD_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
19
- const CLI_MAX_OUTPUT_BYTES = GATEWAY_CONTROL_MAX_RESPONSE_BYTES;
20
- const DELIVERY_POLL_INTERVAL_MS = 250;
21
- const DELIVERY_POLL_MIN_REQUEST_TIMEOUT_MS = 50;
22
- const PEER_AWAIT_REQUEST_TIMEOUT_MS = 35_000;
23
- export const EMBASSY_VERSION = "3.1.0";
24
- // RELEASE VERSION SWEEP — every place the version lives, seven sites:
25
- // package.json; npm-shrinkwrap.json (two: the root and packages[""]); this
26
- // constant; test/gateway-cli.test.ts's literal package-version assertion; and
27
- // README.md's two `embassy status` renders. test/status-view.test.ts pins
28
- // those renders to this constant and derives its own fixture and regexes from
29
- // it, so it is not a sweep site.
30
- export const gatewayCliCommands = [
31
- "serve", "service", "health", "status", "watch", "check", "delivery-status",
32
- "wait-delivery", "refresh", "register-codex",
33
- "unregister-codex",
34
- "send", "reply",
35
- "register-peer", "unregister-peer", "await",
36
- "peer-stdio", "retire",
37
- ];
38
- export const gatewayCliExitCodes = Object.freeze({
39
- ok: 0,
40
- invalidInput: 2,
41
- rejected: 3,
42
- unavailable: 4,
43
- ambiguous: 5,
44
- failure: 6,
45
- });
46
- const HELP_USAGE = `Embassy — local messaging for Claude Code and Codex
47
-
48
- Usage:
49
- embassy <command> [options]
50
-
51
- Commands:
52
- serve Run the socket-only broker
53
- service install|uninstall|status
54
- Run the broker as a macOS launchd agent
55
- health Check broker health
56
- status [--json] [--recent <n>]
57
- Show the broker, connectors, routes, and recent
58
- messages; JSON when piped or with --json
59
- watch [--json] Tail messages and route activity until Ctrl-C
60
- check [--to <alias>] [--timeout <s>]
61
- Round-trip self-test against a registered peer
62
- refresh Rescan for Claude sessions
63
- retire --alias <alias> Remove a local route and report settled work
64
- register-codex --alias <codex-alias> [--succeeds <old-alias>]
65
- Register or succeed a Codex task
66
- unregister-codex --alias <codex-alias>
67
- Unregister the current Codex task
68
- register-peer --alias <peer-alias> [--token-stdin|--emit-env]
69
- Register a universal shell peer
70
- unregister-peer --alias <peer-alias> [--token-stdin]
71
- Unregister a universal shell peer
72
- await --alias <peer-alias> [--token-stdin]
73
- Wait for one peer message and acknowledge stdout
74
- peer-stdio Serve the bounded federation protocol on stdin/stdout
75
- send --from <own-alias> --to <alias-or-uuid> [--expects-reply]
76
- send --from <own-alias> --conversation <token>
77
- Exactly one of --to and --conversation; body from stdin.
78
- A discovered Claude session's route installs on first send
79
- reply Deprecated alias for send --conversation
80
- delivery-status --token <delivery-token>
81
- Read a delivery token
82
- wait-delivery --token <delivery-token>
83
- Wait for terminal delivery status
84
-
85
- Options:
86
- --token-stdin Read the peer token as the first LF-terminated stdin line
87
- --emit-env Print the first registration token as an export command
88
- --version, -v Print the version
89
- --help, -h Show this help
90
- `;
91
- /**
92
- * Fixed one-line stderr summaries; stdout carries the protocol and stderr
93
- * never carries private detail. The `service` subtree is the one deliberate
94
- * exception, and only for its own local files: it reports the plist path, the
95
- * log path, a missing program path, and launchctl's bounded stderr, because
96
- * managing those files is the whole command. launchctl's *stdout* is never
97
- * quoted — a `print` dump carries the agent's environment values.
98
- */
99
- const CLI_STDERR = {
100
- input: "request rejected.",
101
- decision: "gateway rejected the request.",
102
- unavailable: "gateway unavailable.",
103
- ambiguous: "outcome ambiguous; do not retry automatically.",
104
- failure: "command failed.",
105
- unsafe: "gateway state directory or socket has unexpected permissions or ownership. Verify the exact path, owner, and modes before retrying.",
106
- tokenUnknown: "delivery token not recognized; it may have expired or left bounded retention.",
107
- deliveryTimeout: "the delivery has not settled yet; the gateway is still running. Check again later with embassy delivery-status.",
108
- };
109
- /** Exact next-step remedies appended after the summary for the faults that have one. */
110
- const CLI_HINT = {
111
- noBrokerRunning: "No broker is running (state dir {stateDir}). Run `embassy service install` once, or `embassy serve` in a terminal — or verify EMBASSY_STATE_DIR is not scrubbed or misdirected (for example by a sandboxed task's HOME).",
112
- controlConnectDenied: "the broker may be running, but this process cannot connect; grant this task write access to the gateway state directory, then retry. Do not start a second broker. If access should already work, verify EMBASSY_STATE_DIR names this user's own state directory.",
113
- controlInvalidResponse: "if either Embassy installation changed recently, rebuild or repoint this client to the broker's installation; otherwise restart the broker, then retry.",
114
- controlVersionMismatch: "rebuild or repoint this client to the broker's Embassy installation, then retry.",
115
- stateAccessDenied: "local policy denied access to the gateway state directory; grant this process access, then retry starting the broker. If access should already work, verify EMBASSY_STATE_DIR names this user's own state directory.",
116
- messageTooLarge: "message exceeds the 16 KiB acceptance cap; shorten or split it. For long prose, pipe the body from a file.",
117
- stateResetRequired: "state reset required; follow docs/CONFIGURATION.md#private-state-reset. Resetting abandons unsettled work. To check for unsettled work after upgrading, temporarily use Embassy 2.0.x before resetting.",
118
- callerIdentityConflict: "both agent identities were inherited; rerun this Codex-side call with env -u CLAUDE_CODE_MESSAGING_SOCKET, or this Claude-side call with env -u CODEX_THREAD_ID",
119
- aliasHostMismatch: "aliases on this machine end with @{localHost} (from {stateDir}/nodes.json); found @{given}",
120
- aliasHostDefaulted: "no nodes.json has been written at {stateDir} yet — a broker writes it on first start; until then this machine defaults to @{localHost}; found @{given}",
121
- stateInUse: "another broker may own {stateDir}: if `embassy serve` is not running anywhere, the lock {stateDir}/.gateway-controller.lock is stale (recorded host {host}, pid {pid}) — remove it and start again.",
122
- stateInUseUnrecorded: "another broker may own {stateDir}: if `embassy serve` is not running anywhere, the lock {stateDir}/.gateway-controller.lock is stale — remove it and start again.",
123
- stateLockUnverified: "the lock {stateDir}/.gateway-controller.lock cannot be read as a controller record; if `embassy serve` is not running anywhere, remove that file and start again.",
124
- nodeInventoryChanged: "nodes.json at {stateDir} changed while the broker was starting; start again.",
125
- stateWriteFailed: "nodes.json could not be written at {stateDir} (disk full, read-only, or quota?)",
126
- stateSyncFailed: "nodes.json was written at {stateDir} but the directory could not be synced; start again and check the volume",
127
- unknownTarget: "no current route answers to that name. A Claude session is addressed by its live name: run embassy refresh, then read embassy status for the name it has now.",
128
- aliasCollision: "the alias names more than one live session; rename one, or address the session by UUID with --to <session-uuid>.",
129
- workspaceBroad: "that session's workspace is a filesystem root or overlaps a temporary root; reopen the session in a specific project directory outside temporary roots.",
130
- workspaceUnsafe: "the session workspace or user home failed directory checks; verify access, ownership, permissions and canonical, non-symlink paths before retrying.",
131
- callerAliasMismatch: "--from must be the sending session's own alias; read the name embassy status shows for this session.",
132
- targetChanged: "the session you addressed renamed or exited while the send was being set up; run embassy refresh and address it by its current name.",
133
- checkNoTarget: "no Codex task is registered, so there is nothing to check. Run `embassy register-codex --alias codex-<name>@{localHost}` from inside the task, or name any current route with `embassy check --to <alias>`.",
134
- checkAllStale: "every registered Codex task is unobserved or was last observed more than ten minutes ago ({aliases}), so nothing was sent. Read `embassy status` for each one's remedy, or check one anyway with `embassy check --to <alias>`.",
135
- peerAliasRequired: "a peer token is present (EMBASSY_PEER_TOKEN or --token-stdin) but --from is not a peer-* alias; unset the token or name the peer route.",
136
- codexCallerRequired: "--from names a Codex task ({alias}) but no CODEX_THREAD_ID was inherited; run this inside that Codex task, as a shell step of its current turn.",
137
- peerCallerRequired: "--from names a shell peer ({alias}) but no peer token was given; pass the peer_ token on the first stdin line with --token-stdin (or EMBASSY_PEER_TOKEN from a stable shell).",
138
- codexRouteUnregistered: "no registration for {alias} on this broker; run `embassy register-codex --alias {alias}` from inside the Codex task (after a private state reset, every Codex task registers again).",
139
- codexThreadMismatch: "{alias} is not registered to this task's inherited CODEX_THREAD_ID; --from must name the alias this task registered (read it from `embassy status`), or register this task with `embassy register-codex`.",
140
- callerIdentityRequired: "no caller credential was inherited, and a conversation names no route to infer one from: run this inside the Codex task (CODEX_THREAD_ID), inside the Claude Code session (CLAUDE_CODE_MESSAGING_SOCKET), or as a registered shell peer with --token-stdin and the peer_ token on the first stdin line.",
141
- unknownMethod: "the broker does not implement that control method, so this client and the broker are from different Embassy builds; rebuild or update this client to the broker's Embassy installation, then retry.",
142
- };
143
- /**
144
- * The three connect-stage codes that all mean the same thing to the person
145
- * reading them: nothing is serving this state directory. A missing socket
146
- * never reaches the transport (validatePrivateGatewayControlSocket maps it to
147
- * CONTROL_SOCKET_UNAVAILABLE first), and a broker that died leaving its socket
148
- * behind reports CONTROL_LISTENER_UNAVAILABLE — so the hint has to cover all
149
- * three or it is unreachable in practice.
150
- */
151
- const isNoBrokerCode = (code) => code === "CONTROL_SOCKET_UNAVAILABLE" || code === "CONTROL_SOCKET_MISSING" ||
152
- code === "CONTROL_LISTENER_UNAVAILABLE";
153
- /** Best effort, and never throws: a hint must not replace the fault it explains. */
154
- function resolvedStateDirForHint(env) {
155
- try {
156
- return path.resolve(defaultGatewayStateDir(env));
157
- }
158
- catch {
159
- return env.EMBASSY_STATE_DIR ?? env.XDG_STATE_HOME ?? "unresolvable";
160
- }
161
- }
162
- /**
163
- * Renders a CLI_HINT entry, substituting {name} placeholders from `vars` in a
164
- * single pass: a substituted value is never rescanned, so a state directory
165
- * literally named `/tmp/{host}` cannot expand into anything else.
166
- */
167
- function renderHint(hint, vars) {
168
- const text = CLI_HINT[hint];
169
- if (vars === undefined)
170
- return text;
171
- return text.replace(/\{([A-Za-z][A-Za-z0-9]*)\}/g, (token, name) => Object.hasOwn(vars, name) ? vars[name] : token);
172
- }
173
- const hintLine = (hint, env) => `[embassy] ${renderHint(hint, { stateDir: resolvedStateDirForHint(env) })}\n`;
174
- class CliFault extends Error {
175
- code;
176
- retryable;
177
- hint;
178
- kind;
179
- hintVars;
180
- constructor(code, retryable = false, hint, kind, hintVars) {
181
- super("The gateway client rejected the request.");
182
- this.code = code;
183
- this.retryable = retryable;
184
- this.hint = hint;
185
- this.kind = kind;
186
- this.hintVars = hintVars;
187
- this.name = "CliFault";
188
- }
189
- }
190
- function fault(code = "INVALID_ARGUMENTS") { throw new CliFault(code); }
191
- const isCommand = (value) => value !== undefined && gatewayCliCommands.includes(value);
192
- function parseOptions(args, valueNames, flagNames = []) {
193
- const values = new Set(valueNames), flags = new Set(flagNames);
194
- const parsed = Object.create(null);
195
- for (let index = 0; index < args.length; index += 1) {
196
- const token = args[index];
197
- if (token === undefined || !token.startsWith("--") || token.length <= 2)
198
- fault();
199
- const name = token.slice(2);
200
- if (Object.hasOwn(parsed, name))
201
- fault();
202
- if (flags.has(name)) {
203
- parsed[name] = true;
204
- continue;
205
- }
206
- if (!values.has(name))
207
- fault();
208
- const value = args[index + 1];
209
- if (value === undefined || value.startsWith("--") || value.length === 0)
210
- fault();
211
- parsed[name] = value;
212
- index += 1;
213
- }
214
- return parsed;
215
- }
216
- const fixedStderr = (kind) => `[embassy] ${CLI_STDERR[kind]}\n`;
217
- function requireString(options, name) {
218
- const value = options[name];
219
- if (typeof value !== "string")
220
- fault();
221
- return value;
222
- }
223
- function count(options, minimum, maximum = minimum) {
224
- const count = Object.keys(options).length;
225
- if (count < minimum || count > maximum)
226
- fault();
227
- }
228
- function requireAlias(options, name) {
229
- const alias = requireString(options, name);
230
- if (!isGatewayAlias(alias))
231
- fault();
232
- return alias;
233
- }
234
- function requireCodexAlias(options, name) {
235
- const alias = requireAlias(options, name);
236
- if (!alias.startsWith("codex-"))
237
- fault();
238
- return alias;
239
- }
240
- const gatewayAliasHost = (alias) => alias.slice(alias.lastIndexOf("@") + 1);
241
- /**
242
- * An alias naming another host is rejected before any broker call. The hint
243
- * says where this machine's own host came from, because the two cases have
244
- * different remedies: a durable nodes.json is the answer, while a defaulted
245
- * identity is still provisional until the first `embassy serve` records it.
246
- */
247
- function aliasHostFault(local, given) {
248
- return new CliFault("INVALID_ARGUMENTS", false, local.defaulted ? "aliasHostDefaulted" : "aliasHostMismatch", undefined, { localHost: local.host, given, stateDir: local.stateDir });
249
- }
250
- function requireConversationId(options, name) {
251
- const conversationId = requireString(options, name);
252
- if (!isGatewayConversationId(conversationId))
253
- fault();
254
- return conversationId;
255
- }
256
- function requireDestinationSelector(options, name) {
257
- const selector = requireString(options, name);
258
- if (!isClaudeSessionSelector(selector))
259
- fault();
260
- return selector;
261
- }
262
- function requireDeliveryToken(options, name) {
263
- const token = requireString(options, name);
264
- if (!isGatewayDeliveryToken(token))
265
- fault();
266
- return token;
267
- }
268
- function requireCodexThreadId(env) {
269
- const threadId = env.CODEX_THREAD_ID;
270
- if (typeof threadId !== "string" || !THREAD_ID_PATTERN.test(threadId))
271
- fault("CODEX_IDENTITY_REQUIRED");
272
- return threadId.toLowerCase();
273
- }
274
- const hasIdentity = (value) => typeof value === "string" && value.length > 0;
275
- const PEER_TOKEN_PATTERN = /^peer_[A-Za-z0-9_-]{32}$/;
276
- function peerTokenSource(options, env) {
277
- const inherited = env.EMBASSY_PEER_TOKEN;
278
- if ((options["token-stdin"] === true || hasIdentity(inherited)) &&
279
- (hasIdentity(env.CODEX_THREAD_ID) || hasIdentity(env.CLAUDE_CODE_MESSAGING_SOCKET)))
280
- throw callerIdentityConflictFault(env);
281
- if (options["token-stdin"] === true && hasIdentity(inherited))
282
- throw callerIdentityConflictFault(env);
283
- if (hasIdentity(inherited) && !PEER_TOKEN_PATTERN.test(inherited))
284
- fault("CALLER_IDENTITY_REQUIRED");
285
- return options["token-stdin"] === true ? "stdin" : hasIdentity(inherited) ? inherited : undefined;
286
- }
287
- function requirePeerAlias(options) {
288
- const alias = requireAlias(options, "alias");
289
- if (!alias.startsWith("peer-"))
290
- fault();
291
- return alias;
292
- }
293
- function callerIdentityConflictFault(env) {
294
- const both = hasIdentity(env.CODEX_THREAD_ID) && hasIdentity(env.CLAUDE_CODE_MESSAGING_SOCKET);
295
- return new CliFault("CALLER_IDENTITY_CONFLICT", false, both ? "callerIdentityConflict" : undefined);
296
- }
297
- function requireExclusiveCodexThreadId(env) {
298
- if (hasIdentity(env.CLAUDE_CODE_MESSAGING_SOCKET))
299
- throw callerIdentityConflictFault(env);
300
- return requireCodexThreadId(env);
301
- }
302
- function optionalClaudeReplyAddress(env) {
303
- const socketPath = env.CLAUDE_CODE_MESSAGING_SOCKET;
304
- if (socketPath === undefined || socketPath.length === 0)
305
- return undefined;
306
- if (socketPath.includes("\0") || !path.isAbsolute(socketPath) || path.resolve(socketPath) !== socketPath)
307
- fault("CLAUDE_IDENTITY_INVALID");
308
- const replyAddress = `uds:${socketPath}`;
309
- if (!isGatewayReplyAddress(replyAddress))
310
- fault("CLAUDE_IDENTITY_INVALID");
311
- return replyAddress;
312
- }
313
- function requireClaudeReplyAddress(env) {
314
- const replyAddress = optionalClaudeReplyAddress(env);
315
- if (replyAddress === undefined)
316
- fault("CLAUDE_IDENTITY_REQUIRED");
317
- return replyAddress;
318
- }
319
- function requireExclusiveClaudeReplyAddress(env) {
320
- if (hasIdentity(env.CODEX_THREAD_ID))
321
- throw callerIdentityConflictFault(env);
322
- return requireClaudeReplyAddress(env);
323
- }
324
- async function readMessageBody(stdin) {
325
- const chunks = [];
326
- let length = 0;
327
- for await (const chunk of stdin) {
328
- if (typeof chunk !== "string" && !Buffer.isBuffer(chunk))
329
- fault("INVALID_MESSAGE_INPUT");
330
- const buffer = Buffer.from(chunk);
331
- length += buffer.length;
332
- if (length > GATEWAY_CONTROL_MAX_MESSAGE_BYTES)
333
- throw new CliFault("MESSAGE_TOO_LARGE", false, "messageTooLarge");
334
- chunks.push(buffer);
335
- }
336
- let text;
337
- try {
338
- text = new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks, length));
339
- }
340
- catch {
341
- fault("INVALID_MESSAGE_INPUT");
342
- }
343
- if (text.trim().length === 0 || text.includes("\0"))
344
- fault("MESSAGE_REQUIRED");
345
- return text;
346
- }
347
- async function readPeerInput(stdin, source, body) {
348
- if (source !== "stdin")
349
- return { token: source, ...(body ? { text: await readMessageBody(stdin) } : {}) };
350
- const chunks = [];
351
- let length = 0;
352
- for await (const chunk of stdin) {
353
- if (typeof chunk !== "string" && !Buffer.isBuffer(chunk))
354
- fault("INVALID_MESSAGE_INPUT");
355
- const buffer = Buffer.from(chunk);
356
- length += buffer.length;
357
- if (length > GATEWAY_CONTROL_MAX_MESSAGE_BYTES + 38)
358
- throw new CliFault("MESSAGE_TOO_LARGE", false, "messageTooLarge");
359
- chunks.push(buffer);
360
- }
361
- const value = Buffer.concat(chunks, length), newline = value.indexOf(0x0a);
362
- if (newline !== 37 || !PEER_TOKEN_PATTERN.test(value.subarray(0, newline).toString("utf8")) || (!body && newline !== value.length - 1))
363
- fault("INVALID_MESSAGE_INPUT");
364
- const token = value.subarray(0, newline).toString("utf8");
365
- if (!body)
366
- return { token };
367
- let text;
368
- try {
369
- text = new TextDecoder("utf-8", { fatal: true }).decode(value.subarray(newline + 1));
370
- }
371
- catch {
372
- fault("INVALID_MESSAGE_INPUT");
373
- }
374
- if (text.trim().length === 0 || text.includes("\0"))
375
- fault("MESSAGE_REQUIRED");
376
- return { token, text };
377
- }
378
- const emptyParams = (args) => args.length === 0 ? {} : fault();
379
- const envelope = (method, params) => ({ protocolVersion: GATEWAY_CONTROL_PROTOCOL_VERSION, method, params });
380
- async function buildRequest(command, args, env, stdin, loadLocalHost) {
381
- const simple = {
382
- health: "health", refresh: "refresh_discovery",
383
- };
384
- const simpleMethod = simple[command];
385
- if (simpleMethod !== undefined)
386
- return envelope(simpleMethod, emptyParams(args));
387
- switch (command) {
388
- case "serve":
389
- case "service":
390
- case "peer-stdio":
391
- case "status":
392
- case "watch":
393
- case "check":
394
- return fault();
395
- case "health":
396
- case "refresh": return fault();
397
- case "delivery-status":
398
- case "wait-delivery": {
399
- const options = parseOptions(args, ["token"]);
400
- count(options, 1);
401
- return envelope("delivery_status", { token: requireDeliveryToken(options, "token") });
402
- }
403
- case "register-codex": {
404
- const options = parseOptions(args, ["alias", "succeeds"]);
405
- const alias = requireCodexAlias(options, "alias");
406
- const succeedsAlias = options.succeeds === undefined ? undefined : requireCodexAlias(options, "succeeds");
407
- count(options, succeedsAlias === undefined ? 1 : 2, 2);
408
- const threadId = requireExclusiveCodexThreadId(env);
409
- if (succeedsAlias === alias)
410
- fault();
411
- const local = await loadLocalHost();
412
- if (gatewayAliasHost(alias) !== local.host)
413
- throw aliasHostFault(local, gatewayAliasHost(alias));
414
- if (succeedsAlias !== undefined && gatewayAliasHost(succeedsAlias) !== local.host) {
415
- throw aliasHostFault(local, gatewayAliasHost(succeedsAlias));
416
- }
417
- return envelope("register_codex", {
418
- alias, threadId, hostId: local.host, busyPolicy: "queue",
419
- ...(succeedsAlias === undefined ? {} : { succeedsAlias }),
420
- });
421
- }
422
- case "unregister-codex": {
423
- const options = parseOptions(args, ["alias"]);
424
- count(options, 1);
425
- const alias = requireCodexAlias(options, "alias");
426
- const threadId = requireExclusiveCodexThreadId(env);
427
- const local = await loadLocalHost();
428
- if (gatewayAliasHost(alias) !== local.host)
429
- throw aliasHostFault(local, gatewayAliasHost(alias));
430
- return envelope("unregister_codex", { alias, threadId });
431
- }
432
- case "retire": {
433
- const options = parseOptions(args, ["alias"]);
434
- count(options, 1, 1);
435
- const alias = requireAlias(options, "alias"), local = await loadLocalHost();
436
- if (gatewayAliasHost(alias) !== local.host)
437
- throw aliasHostFault(local, gatewayAliasHost(alias));
438
- return envelope("retire_route", { alias });
439
- }
440
- case "register-peer":
441
- case "unregister-peer":
442
- case "await": {
443
- const options = parseOptions(args, ["alias"], ["token-stdin", "emit-env"]);
444
- count(options, 1, 3);
445
- const alias = requirePeerAlias(options), source = peerTokenSource(options, env);
446
- if (hasIdentity(env.CODEX_THREAD_ID) || hasIdentity(env.CLAUDE_CODE_MESSAGING_SOCKET))
447
- throw callerIdentityConflictFault(env);
448
- if (command !== "register-peer" && options["emit-env"] === true)
449
- fault();
450
- if (options["emit-env"] === true && source !== undefined)
451
- fault();
452
- const requireLocalAlias = async () => {
453
- const local = await loadLocalHost();
454
- if (gatewayAliasHost(alias) !== local.host)
455
- throw aliasHostFault(local, gatewayAliasHost(alias));
456
- };
457
- if (source === undefined) {
458
- if (command !== "register-peer")
459
- fault("CALLER_IDENTITY_REQUIRED");
460
- await requireLocalAlias();
461
- return envelope("register_peer", { alias });
462
- }
463
- const { token } = await readPeerInput(stdin, source, false);
464
- await requireLocalAlias();
465
- return envelope(command === "register-peer" ? "register_peer" : command === "unregister-peer" ? "unregister_peer" : "await_peer", { alias, token });
466
- }
467
- // `reply` is the deprecated spelling of `send --conversation`: it names the
468
- // caller's own alias `--alias` instead of `--from`, and both verbs build
469
- // the one `send` request. Keep it until the reply hints already delivered
470
- // in older envelopes have aged out.
471
- case "send":
472
- case "reply": {
473
- const options = command === "reply"
474
- ? parseOptions(args, ["conversation", "alias"], ["token-stdin"])
475
- : parseOptions(args, ["from", "to", "conversation"], ["expects-reply", "token-stdin"]);
476
- count(options, 2, command === "reply" ? 3 : 4);
477
- const fromAlias = requireAlias(options, command === "reply" ? "alias" : "from");
478
- const conversationId = options.conversation === undefined
479
- ? undefined : requireConversationId(options, "conversation");
480
- const toAlias = options.to === undefined
481
- ? undefined : requireDestinationSelector(options, "to");
482
- // One target, and a conversation is always answered expecting a reply.
483
- if ((toAlias === undefined) === (conversationId === undefined))
484
- fault();
485
- if (conversationId !== undefined && options["expects-reply"] === true)
486
- fault();
487
- const source = peerTokenSource(options, env);
488
- const principals = Number(hasIdentity(env.CODEX_THREAD_ID)) + Number(hasIdentity(env.CLAUDE_CODE_MESSAGING_SOCKET)) + Number(source !== undefined);
489
- if (principals > 1)
490
- throw callerIdentityConflictFault(env);
491
- // A conversation names no route to infer a provider from, so a caller
492
- // with nothing inherited gets the provider-neutral code and every
493
- // credential it could present; a route send keeps its Claude-shaped
494
- // default. A peer credential authenticates a peer-* alias and nothing
495
- // else, in either addressing form.
496
- if (principals === 0) {
497
- if (conversationId !== undefined)
498
- throw new CliFault("CALLER_IDENTITY_REQUIRED", false, "callerIdentityRequired");
499
- // A route send names its sender's shape, so the hint can name the
500
- // one credential that shape needs; only a Claude-shaped alias keeps
501
- // the Claude-specific code.
502
- if (fromAlias.startsWith("codex-"))
503
- throw new CliFault("CALLER_IDENTITY_REQUIRED", false, "codexCallerRequired", undefined, { alias: fromAlias });
504
- if (fromAlias.startsWith("peer-"))
505
- throw new CliFault("CALLER_IDENTITY_REQUIRED", false, "peerCallerRequired", undefined, { alias: fromAlias });
506
- }
507
- if (source !== undefined && !fromAlias.startsWith("peer-"))
508
- throw new CliFault("INVALID_ARGUMENTS", false, "peerAliasRequired");
509
- const peer = source === undefined ? undefined : await readPeerInput(stdin, source, true);
510
- const authority = peer === undefined ? hasIdentity(env.CODEX_THREAD_ID)
511
- ? { threadId: requireExclusiveCodexThreadId(env) }
512
- : { replyAddress: requireExclusiveClaudeReplyAddress(env) }
513
- : { peerToken: peer.token };
514
- const target = toAlias === undefined
515
- ? { conversationId: conversationId }
516
- : { toAlias, expectsReply: options["expects-reply"] === true };
517
- return envelope("send", {
518
- fromAlias, text: peer?.text ?? await readMessageBody(stdin), ...target, ...authority,
519
- });
520
- }
521
- }
522
- }
523
- export async function validatePrivateGatewayControlSocket(stateDir, socketPath) {
524
- if (process.platform === "win32")
525
- throw new CliFault("CONTROL_SOCKET_UNAVAILABLE", true);
526
- let state, socket;
527
- try {
528
- [state, socket] = await Promise.all([lstat(stateDir), lstat(socketPath)]);
529
- }
530
- catch (error) {
531
- const code = error !== null && typeof error === "object" && "code" in error
532
- ? error.code : undefined;
533
- if (code === "EPERM" || code === "EACCES")
534
- throw new CliFault("CONTROL_CONNECT_DENIED", true, "controlConnectDenied");
535
- throw new CliFault("CONTROL_SOCKET_UNAVAILABLE", true);
536
- }
537
- const uid = process.getuid?.();
538
- if (state.isSymbolicLink() || !state.isDirectory() || (state.mode & 0o777) !== 0o700 || (uid !== undefined && state.uid !== uid)) {
539
- throw new CliFault("CONTROL_STATE_UNSAFE", false, undefined, "unsafe");
540
- }
541
- if (socket.isSymbolicLink() || !socket.isSocket() || (socket.mode & 0o777) !== 0o600 || (uid !== undefined && socket.uid !== uid)) {
542
- throw new CliFault("CONTROL_SOCKET_UNSAFE", false, undefined, "unsafe");
543
- }
544
- let stateReal, parentReal;
545
- try {
546
- [stateReal, parentReal] = await Promise.all([realpath(stateDir), realpath(path.dirname(socketPath))]);
547
- }
548
- catch {
549
- throw new CliFault("CONTROL_SOCKET_UNSAFE", false, undefined, "unsafe");
550
- }
551
- if (path.dirname(socketPath) !== stateDir || stateReal !== parentReal) {
552
- throw new CliFault("CONTROL_SOCKET_UNSAFE", false, undefined, "unsafe");
553
- }
554
- }
555
- function serializedOutput(value) {
556
- const line = `${JSON.stringify(value)}\n`;
557
- if (Buffer.byteLength(line, "utf8") > CLI_MAX_OUTPUT_BYTES)
558
- fault("OUTPUT_TOO_LARGE");
559
- return line;
560
- }
561
- async function writeComplete(output, frame) {
562
- await new Promise((resolve, reject) => {
563
- let settled = false;
564
- const done = (error) => { if (settled)
565
- return; settled = true; error ? reject(error) : resolve(); };
566
- try {
567
- if (typeof output.write(frame, done) !== "boolean")
568
- done();
569
- }
570
- catch (error) {
571
- done(error instanceof Error ? error : new Error("stdout write failed"));
572
- }
573
- });
574
- }
575
- function writeFailure(stdout, stderr, command, code, options) {
576
- stdout.write(serializedOutput({ ok: false, command: command ?? "unknown", error: {
577
- code, ambiguous: options.ambiguous ?? false, retryable: options.retryable ?? false,
578
- ...(options.detail ?? {}),
579
- } }));
580
- stderr.write(fixedStderr(options.kind));
581
- }
582
- function writeStateResetHint(stderr, code) {
583
- if (code === "GATEWAY_STATE_SCHEMA_UNSUPPORTED" || code === "CORRUPT_GATEWAY_STATE") {
584
- stderr.write(`[embassy] ${CLI_HINT.stateResetRequired}\n`);
585
- }
586
- }
587
- /**
588
- * A BridgeError's own message never reaches a terminal — stderr carries only
589
- * fixed lines and these hints, so nothing private can escape through a
590
- * message. Any remedy an operator must actually read therefore lives here,
591
- * keyed by code and interpolating only the resolved state directory and the
592
- * bounded values the error carried in `detail`.
593
- */
594
- const BRIDGE_ERROR_HINTS = {
595
- GATEWAY_STATE_IN_USE: "stateInUse",
596
- GATEWAY_STATE_LOCK_UNVERIFIED: "stateLockUnverified",
597
- GATEWAY_NODE_INVENTORY_CHANGED: "nodeInventoryChanged",
598
- GATEWAY_STATE_WRITE_FAILED: "stateWriteFailed",
599
- };
600
- /** The state directory a hint should name, resolved the same way every command resolves it. */
601
- function hintStateDir(env) {
602
- try {
603
- return path.resolve(defaultGatewayStateDir(env));
604
- }
605
- catch {
606
- return "the Embassy state directory";
607
- }
608
- }
609
- function writeBridgeErrorHint(stderr, error, env) {
610
- const hint = BRIDGE_ERROR_HINTS[error.code];
611
- if (hint === undefined)
612
- return;
613
- const detail = error.detail;
614
- // Two codes render differently depending on what the error could establish:
615
- // a lock whose recorded machine name is unrepresentable names no host at
616
- // all, and a write that reached the file but not the directory entry says so.
617
- const named = hint === "stateInUse" && (detail?.host === undefined || detail.pid === undefined)
618
- ? "stateInUseUnrecorded"
619
- : hint === "stateWriteFailed" && detail?.stage === "sync" ? "stateSyncFailed" : hint;
620
- stderr.write(`[embassy] ${renderHint(named, {
621
- stateDir: hintStateDir(env),
622
- ...(detail?.host === undefined ? {} : { host: detail.host }),
623
- ...(detail?.pid === undefined ? {} : { pid: detail.pid }),
624
- })}\n`);
625
- }
626
- function isRejectedResult(result) {
627
- return result !== null && typeof result === "object" && result.accepted === false;
628
- }
629
- function refusalHint(request, result) {
630
- if (!isRejectedResult(result))
631
- return undefined;
632
- const reason = result.reason;
633
- const from = request.method === "send" ? request.params.fromAlias : undefined;
634
- if (reason === "PEER_ALIAS_COLLISION")
635
- return { hint: "aliasCollision" };
636
- if (reason === "CLAUDE_PEER_WORKSPACE_BROAD")
637
- return { hint: "workspaceBroad" };
638
- if (reason === "CLAUDE_PEER_WORKSPACE_UNSAFE")
639
- return { hint: "workspaceUnsafe" };
640
- // A Codex task with no registration under its alias — every task, after a
641
- // private state reset — is told to register; a registration held by another
642
- // task is a genuine mismatch with its own remedy; a conversation answered
643
- // from the wrong end gets the own-alias remedy.
644
- if (reason === "ROUTE_UNREGISTERED" && from !== undefined && from.startsWith("codex-")) {
645
- return { hint: "codexRouteUnregistered", vars: { alias: from } };
646
- }
647
- if (reason === "CODEX_THREAD_MISMATCH" && from !== undefined)
648
- return { hint: "codexThreadMismatch", vars: { alias: from } };
649
- if (reason === "CLAUDE_ROUTE_MISMATCH" || reason === "CONVERSATION_CALLER_MISMATCH")
650
- return { hint: "callerAliasMismatch" };
651
- if (reason === "CLAUDE_TARGET_CHANGED")
652
- return { hint: "targetChanged" };
653
- if (result.code !== "not_found" || request.method !== "send")
654
- return undefined;
655
- const target = request.params.toAlias;
656
- if (target === undefined)
657
- return undefined;
658
- return target.startsWith("codex-") || target.startsWith("peer-") ? undefined : { hint: "unknownTarget" };
659
- }
660
- /** UNKNOWN_METHOD is build skew, not an argument error: say which side to move. */
661
- function writeUnknownMethodHint(stderr, code) {
662
- if (code === "UNKNOWN_METHOD")
663
- stderr.write(`[embassy] ${CLI_HINT.unknownMethod}\n`);
664
- }
665
- function reportControlFailure(command, code, stdout, stderr) {
666
- writeFailure(stdout, stderr, command, code, { kind: "failure" });
667
- writeUnknownMethodHint(stderr, code);
668
- return gatewayCliExitCodes.failure;
669
- }
670
- function responseExitCode(response) {
671
- return !response.ok ? gatewayCliExitCodes.failure
672
- : isRejectedResult(response.result) ? gatewayCliExitCodes.rejected : gatewayCliExitCodes.ok;
673
- }
674
- function waitDeliveryExitCode(response) {
675
- return response.ok && response.result.found && response.result.terminal && response.result.state === "delivered"
676
- ? gatewayCliExitCodes.ok : gatewayCliExitCodes.failure;
677
- }
678
- const defaultDelay = async (milliseconds) => await new Promise((resolve) => setTimeout(resolve, milliseconds));
679
- async function waitForDelivery(socketPath, request, sendRequest, now, delay,
680
- // `check` bounds this wait by its own budget; `wait-delivery` keeps waiting
681
- // to the broker's delivery deadline, which is hours by default.
682
- maximumWaitMs,
683
- // `check` also passes its Ctrl-C signal, so an operator who gives up during
684
- // this wait reaches the cleanup hop within one poll instead of one budget.
685
- signal) {
686
- let deadline = maximumWaitMs === undefined ? undefined : now() + maximumWaitMs;
687
- while (true) {
688
- if (signal?.aborted === true)
689
- return { kind: "interrupted" };
690
- const remaining = deadline === undefined ? undefined : deadline - now();
691
- if (remaining !== undefined && remaining <= 0)
692
- return { kind: "timeout" };
693
- if (remaining !== undefined && remaining < DELIVERY_POLL_MIN_REQUEST_TIMEOUT_MS) {
694
- await delay(remaining);
695
- return { kind: "timeout" };
696
- }
697
- let response;
698
- try {
699
- response = await sendRequest({
700
- socketPath, request,
701
- timeoutMs: remaining === undefined ? GATEWAY_CONTROL_DEFAULT_TIMEOUT_MS
702
- : Math.min(GATEWAY_CONTROL_DEFAULT_TIMEOUT_MS, Math.floor(remaining)),
703
- });
704
- }
705
- catch (error) {
706
- if (deadline !== undefined && now() >= deadline && error instanceof GatewayControlTransportError && error.recoverable)
707
- return { kind: "timeout" };
708
- throw error;
709
- }
710
- if (!response.ok) {
711
- if (response.error.code === "REQUEST_TIMEOUT" && deadline !== undefined && now() >= deadline)
712
- return { kind: "timeout" };
713
- return { kind: "response", response };
714
- }
715
- if (!response.result.found)
716
- return { kind: "unknown" };
717
- if (response.result.terminal)
718
- return { kind: "response", response };
719
- const observed = Date.parse(response.result.deadlineAt) + GATEWAY_CONTROL_DEFAULT_TIMEOUT_MS;
720
- if (!Number.isFinite(observed))
721
- throw new Error("invalid validated delivery deadline");
722
- deadline = deadline === undefined ? observed : Math.min(deadline, observed);
723
- const after = deadline - now();
724
- if (after <= 0)
725
- return { kind: "timeout" };
726
- await delay(Math.min(DELIVERY_POLL_INTERVAL_MS, after));
727
- }
728
- }
729
- const SERVICE_HEALTH_DEADLINE_MS = 10_000;
730
- const SERVICE_HEALTH_POLL_INTERVAL_MS = 200;
731
- /**
732
- * Per-attempt cap. Without one, each attempt inherits the 3-second control
733
- * timeout and a stalled socket stretches the "10 s" window past two minutes.
734
- */
735
- const SERVICE_HEALTH_REQUEST_TIMEOUT_MS = 1_000;
736
- /**
737
- * control.ts rejects any timeout below 50 ms with CONTROL_INVALID_RESPONSE.
738
- * An attempt squeezed into the tail of the window would therefore fabricate a
739
- * fault and become the "last observed" code, so the poll treats less than
740
- * this much remaining as the deadline already reached.
741
- */
742
- const SERVICE_HEALTH_MIN_REQUEST_TIMEOUT_MS = 50;
743
- /** A second, independent bound: a clock that never advances cannot loop forever. */
744
- const SERVICE_HEALTH_MAX_ATTEMPTS = SERVICE_HEALTH_DEADLINE_MS / SERVICE_HEALTH_POLL_INTERVAL_MS;
745
- /** Elapsed time is measured monotonically; a wall-clock step must not move it. */
746
- const monotonicNow = () => performance.now();
747
- /**
748
- * Codes that answer the question rather than postpone it. Polling still runs
749
- * to the deadline — a mode or ownership check can be momentarily unlucky
750
- * while the broker is publishing its socket — but if the *last* thing
751
- * observed was one of these refusals rather than silence, install reports it
752
- * with that code's own class and points at `embassy health`, the command that
753
- * explains it, instead of a retryable timeout.
754
- */
755
- const SERVICE_HEALTH_DECISIVE = new Map([
756
- ["CONTROL_STATE_UNSAFE", { kind: "unsafe", retryable: false, exitCode: gatewayCliExitCodes.invalidInput }],
757
- ["CONTROL_SOCKET_UNSAFE", { kind: "unsafe", retryable: false, exitCode: gatewayCliExitCodes.invalidInput }],
758
- ["CONTROL_CONNECT_DENIED", { kind: "unavailable", hint: "controlConnectDenied", retryable: true, exitCode: gatewayCliExitCodes.unavailable }],
759
- ["CONTROL_VERSION_MISMATCH", { kind: "unavailable", hint: "controlVersionMismatch", retryable: true, exitCode: gatewayCliExitCodes.unavailable }],
760
- ]);
761
- /**
762
- * The install command's own probe, bounded by wall clock rather than by an
763
- * attempt count: a freshly bootstrapped launchd agent has to load its state
764
- * and publish a control socket, which on a cold cache is seconds. This never
765
- * throws, but silence at the deadline is not success — install reports the
766
- * last code it observed and exits non-zero, because an agent that never
767
- * answered is exactly the case the operator has to hear about.
768
- */
769
- async function pollServiceHealth(config, sendRequest, validateSocket, delay, now) {
770
- const started = now();
771
- const elapsed = () => Math.max(0, now() - started);
772
- const remainingMs = () => SERVICE_HEALTH_DEADLINE_MS - elapsed();
773
- let lastObserved = "SERVICE_HEALTH_NO_RESPONSE";
774
- for (let attempt = 0; attempt < SERVICE_HEALTH_MAX_ATTEMPTS; attempt += 1) {
775
- const remaining = remainingMs();
776
- if (remaining < SERVICE_HEALTH_MIN_REQUEST_TIMEOUT_MS)
777
- break;
778
- try {
779
- await validateSocket(config.stateDir, config.controlSocketPath);
780
- const response = await sendRequest({
781
- socketPath: config.controlSocketPath,
782
- request: envelope("health", {}),
783
- timeoutMs: Math.floor(Math.min(remaining, SERVICE_HEALTH_REQUEST_TIMEOUT_MS)),
784
- });
785
- if (response.ok)
786
- return { ok: true, result: response.result, elapsedMs: elapsed() };
787
- lastObserved = response.error.code;
788
- }
789
- catch (error) {
790
- lastObserved = error instanceof GatewayControlTransportError ? error.code
791
- : error instanceof CliFault ? error.code
792
- : error instanceof BridgeError ? error.code
793
- : "SERVICE_HEALTH_NO_RESPONSE";
794
- }
795
- const left = remainingMs();
796
- if (left < SERVICE_HEALTH_MIN_REQUEST_TIMEOUT_MS)
797
- break;
798
- await delay(Math.min(SERVICE_HEALTH_POLL_INTERVAL_MS, left));
799
- }
800
- return { ok: false, lastObserved, elapsedMs: elapsed() };
801
- }
802
- const WATCH_POLL_INTERVAL_MS = 1_000;
803
- const CHECK_TIMEOUT_DEFAULT_SECONDS = 60;
804
- const CHECK_TIMEOUT_MAX_SECONDS = 600;
805
- /** control.ts refuses a request timeout below this, so it is also the floor here. */
806
- const CONTROL_MIN_REQUEST_TIMEOUT_MS = 50;
807
- /**
808
- * The unit the attempt bound is counted in while `check` awaits a reply: one
809
- * attempt per second of budget, plus one. A broker answering `timeout`
810
- * instantly therefore cannot spin the loop even if the clock never moves.
811
- */
812
- const CHECK_REPLY_ATTEMPT_MS = 1_000;
813
- const MAX_CONTROLLER_LOCK_BYTES = 4_096;
814
- /**
815
- * Whether stdout is a terminal. Reading the stream's own `isTTY` rather than
816
- * `process.stdout` keeps this injectable: a test writes to a capture that
817
- * declares `isTTY`, and nothing consults the real process.
818
- */
819
- const isTerminal = (output) => output.isTTY === true;
820
- /** Color is opt-out through NO_COLOR, and never carries meaning by itself. */
821
- const useColor = (output, env) => isTerminal(output) && (env.NO_COLOR === undefined || env.NO_COLOR.length === 0);
822
- /** A bounded integer option; anything outside the range is an argument fault. */
823
- function boundedOption(options, name, fallback, minimum, maximum) {
824
- const raw = options[name];
825
- if (raw === undefined)
826
- return fallback;
827
- if (typeof raw !== "string" || !/^[0-9]{1,9}$/.test(raw))
828
- fault();
829
- const value = Number(raw);
830
- if (value < minimum || value > maximum)
831
- fault();
832
- return value;
833
- }
834
- /**
835
- * The broker's pid, from the controller lock its store writes in the state
836
- * directory. Best effort and never throws: `status` prints with or without it,
837
- * and that file's path is already named in the CLI's own hints, so this
838
- * discloses nothing new.
839
- */
840
- export async function readGatewayControllerPid(stateDir) {
841
- return recordedPid(await readOwnedSmallFile(path.join(stateDir, GATEWAY_CONTROLLER_LOCK_FILE), process.getuid?.(), MAX_CONTROLLER_LOCK_BYTES));
842
- }
843
- /**
844
- * `watch` and `check` both run until the operator stops them, and `check` has
845
- * a temporary registration to take back down when that happens. Tests inject
846
- * the signal; a real terminal gets SIGINT/SIGTERM handlers that are removed
847
- * again on the way out, so Ctrl-C runs the cleanup path instead of killing
848
- * node with 130 and leaving the registration to its own expiry.
849
- */
850
- function interruptSignal(signal) {
851
- if (signal !== undefined)
852
- return { signal, dispose: () => undefined };
853
- const controller = new AbortController();
854
- const stop = () => controller.abort();
855
- process.on("SIGINT", stop);
856
- process.on("SIGTERM", stop);
857
- return { signal: controller.signal, dispose: () => {
858
- process.off("SIGINT", stop);
859
- process.off("SIGTERM", stop);
860
- } };
861
- }
862
- /**
863
- * How long the throwaway registration is allowed to outlive the check: the
864
- * whole budget plus a minute, so a broker that is merely slow still finds a
865
- * live mailbox, and an operator who kills the process at the worst possible
866
- * moment waits at most that long for the broker to reclaim it.
867
- */
868
- const CHECK_IDENTITY_GRACE_MS = 60_000;
869
- /**
870
- * Set when a real Ctrl-C interrupted `check`. The abandoned `await_peer`
871
- * request may still hold its socket — and with it the event loop — until the
872
- * broker answers it. The operator already has the summary, so the entry point
873
- * exits once stdio has drained instead of lingering on that socket.
874
- */
875
- let interruptedCheckExit = false;
876
- /**
877
- * The round-trip self-test. It mints its own principal — a throwaway `peer-*`
878
- * registration — rather than borrowing an inherited Codex or Claude identity,
879
- * because the operator running it after a Claude Code or Codex upgrade is
880
- * usually in a plain terminal with neither. That also gives the reply
881
- * somewhere to arrive: the peer mailbox this process is the only waiter on.
882
- * The conversation token returned by the send is the correlation seam; the
883
- * echoed id is confirmation printed beside it, never a second identity.
884
- */
885
- async function runGatewayCheck(options) {
886
- const { sendRequest, socketPath, stdout, stderr } = options;
887
- const paint = terminalPainter(options.color);
888
- const call = async (method, params, timeoutMs) => {
889
- const response = await sendRequest({
890
- socketPath, request: envelope(method, params),
891
- ...(timeoutMs === undefined ? {} : { timeoutMs }),
892
- });
893
- if (!response.ok)
894
- writeUnknownMethodHint(stderr, response.error.code);
895
- return response;
896
- };
897
- const hops = [];
898
- const emit = (hop) => {
899
- hops.push(hop);
900
- const mark = hop.ok ? paint("ok ", "green") : paint("FAIL", "red");
901
- const timing = hop.elapsedMs === undefined ? "" : ` ${String(hop.elapsedMs)} ms`;
902
- stdout.write(` ${mark} ${hop.name.padEnd(9)} ${hop.detail}${timing}\n`);
903
- return hop;
904
- };
905
- const alias = `peer-check-${randomBytes(4).toString("hex")}@${options.hostId}`;
906
- const id = randomBytes(4).toString("hex");
907
- stdout.write(`embassy check ${id} → ${options.target}\n\n`);
908
- let stage = options.now();
909
- const lap = () => {
910
- const at = options.now();
911
- const elapsed = Math.max(0, Math.round(at - stage));
912
- stage = at;
913
- return elapsed;
914
- };
915
- const ttlMs = options.timeoutMs + CHECK_IDENTITY_GRACE_MS;
916
- // Ctrl-C. The signal is raced against the two long waits below, so the
917
- // cleanup hop runs the moment the operator gives up, not when the broker's
918
- // long-poll happens to return.
919
- let interrupted = false;
920
- const interruption = new Promise((resolve) => {
921
- if (options.interrupt.aborted) {
922
- resolve("interrupted");
923
- return;
924
- }
925
- options.interrupt.addEventListener("abort", () => resolve("interrupted"), { once: true });
926
- });
927
- const summarize = (exitCode) => {
928
- const failed = hops.find((hop) => !hop.ok && hop.name !== "cleanup");
929
- const cleanup = hops.find((hop) => !hop.ok && hop.name === "cleanup");
930
- stdout.write(`\n${failed !== undefined
931
- ? `check ${interrupted ? "interrupted" : "failed"} at the ${failed.name} hop`
932
- : cleanup === undefined ? "check passed"
933
- : `check passed; cleanup failed (${cleanup.code ?? "no safe code"})`}\n`);
934
- return failed === undefined && cleanup !== undefined ? gatewayCliExitCodes.failure : exitCode;
935
- };
936
- const registered = await call("register_peer", { alias, ephemeral: true, ttlMs });
937
- if (!registered.ok || !("token" in registered.result)) {
938
- const refusal = !registered.ok ? registered.error.code
939
- : `the broker refused a temporary check identity (${registered.result.code}${"reason" in registered.result && registered.result.reason !== undefined
940
- ? ` ${registered.result.reason}` : ""})`;
941
- emit({ ok: false, name: "register", detail: refusal, elapsedMs: lap() });
942
- return summarize(gatewayCliExitCodes.failure);
943
- }
944
- const token = registered.result.token;
945
- emit({ ok: true, name: "register", detail: `${alias} (ephemeral, ${String(Math.round(ttlMs / 60_000))} min)`, elapsedMs: lap() });
946
- // Every later hop runs inside this closure so that one `finally` releases
947
- // the temporary identity and the summary line is printed exactly once,
948
- // whichever hop gave up first.
949
- const roundTrip = async () => {
950
- // The body names no reply verb: the broker's own reply hint, delivered
951
- // inside the provenance envelope, always carries the current one.
952
- const text = `[embassy check ${id}] Embassy round-trip self-test. Reply with one line repeating ${id}, using the command in this message's reply hint.`;
953
- const sent = await call("send", {
954
- fromAlias: alias, toAlias: options.target, text, expectsReply: true, peerToken: token
955
- });
956
- if (!sent.ok) {
957
- emit({ ok: false, name: "send", detail: sent.error.code, elapsedMs: lap() });
958
- return gatewayCliExitCodes.failure;
959
- }
960
- if (!sent.result.accepted) {
961
- const { code, reason } = sent.result;
962
- emit({ ok: false, name: "send",
963
- detail: `${code}${reason === undefined ? "" : ` ${reason}`}`, elapsedMs: lap() });
964
- return gatewayCliExitCodes.rejected;
965
- }
966
- const { conversationId, deliveryToken } = sent.result;
967
- emit({ ok: true, name: "send", detail: `accepted, conversation …${conversationId.slice(-8)}`, elapsedMs: lap() });
968
- const delivery = await waitForDelivery(socketPath, envelope("delivery_status", { token: deliveryToken }), sendRequest, options.now, options.delay, options.timeoutMs, options.interrupt);
969
- if (delivery.kind === "interrupted") {
970
- interrupted = true;
971
- emit({ ok: false, name: "delivered", detail: "interrupted before it settled", elapsedMs: lap() });
972
- return gatewayCliExitCodes.failure;
973
- }
974
- if (delivery.kind !== "response" || !delivery.response.ok || !delivery.response.result.found ||
975
- delivery.response.result.state !== "delivered") {
976
- const detail = delivery.kind === "timeout"
977
- ? `still unsettled after ${String(Math.round(options.timeoutMs / 1_000))} s`
978
- : delivery.kind === "unknown" ? "the delivery token left bounded retention"
979
- : !delivery.response.ok ? delivery.response.error.code
980
- : delivery.response.result.found
981
- ? `${delivery.response.result.state}${delivery.response.result.safeErrorCode === undefined
982
- ? "" : ` ${delivery.response.result.safeErrorCode}`}`
983
- : "the delivery token left bounded retention";
984
- emit({ ok: false, name: "delivered", detail, elapsedMs: lap() });
985
- return gatewayCliExitCodes.failure;
986
- }
987
- emit({ ok: true, name: "delivered", detail: "the peer's transport accepted it", elapsedMs: lap() });
988
- // Two independent bounds, as everywhere else that polls here: the wall
989
- // clock, and an attempt count, so a broker that answers "timeout"
990
- // instantly cannot spin this loop forever.
991
- const replyDeadline = options.now() + options.timeoutMs;
992
- const attempts = Math.ceil(options.timeoutMs / CHECK_REPLY_ATTEMPT_MS) + 1;
993
- let answered = false;
994
- let uncorrelated = 0;
995
- for (let attempt = 0; attempt < attempts && !answered; attempt += 1) {
996
- const remaining = replyDeadline - options.now();
997
- if (remaining < CONTROL_MIN_REQUEST_TIMEOUT_MS)
998
- break;
999
- // The long-poll is raced against Ctrl-C rather than merely checked
1000
- // between polls: the broker holds `await_peer` open for up to 35 s, and
1001
- // the cleanup below must not wait for that. The abandoned request ends
1002
- // on its own once the cleanup retires the mailbox it was waiting on.
1003
- const waited = options.interrupt.aborted ? "interrupted" : await Promise.race([
1004
- call("await_peer", { alias, token }, Math.min(PEER_AWAIT_REQUEST_TIMEOUT_MS, Math.floor(remaining))),
1005
- interruption,
1006
- ]);
1007
- if (waited === "interrupted") {
1008
- interrupted = true;
1009
- emit({ ok: false, name: "reply", detail: "interrupted before a reply arrived", elapsedMs: lap() });
1010
- return gatewayCliExitCodes.failure;
1011
- }
1012
- if (!waited.ok) {
1013
- if (waited.error.code === "REQUEST_TIMEOUT")
1014
- continue;
1015
- emit({ ok: false, name: "reply", detail: waited.error.code, elapsedMs: lap() });
1016
- return gatewayCliExitCodes.failure;
1017
- }
1018
- if (waited.result.state === "timeout")
1019
- continue;
1020
- // The receipt is part of the round trip: a message the broker would not
1021
- // let this mailbox acknowledge is a failed reply hop, not a passed one.
1022
- const receipt = await call("peer_receipt", { alias, token, receipt: waited.result.receipt });
1023
- if (!receipt.ok || !receipt.result.accepted) {
1024
- emit({ ok: false, name: "reply", detail: !receipt.ok ? receipt.error.code
1025
- : `receipt refused (${receipt.result.code}${"reason" in receipt.result && receipt.result.reason !== undefined
1026
- ? ` ${String(receipt.result.reason)}` : ""})`, elapsedMs: lap() });
1027
- return gatewayCliExitCodes.failure;
1028
- }
1029
- const frame = JSON.parse(waited.result.frame);
1030
- if (frame.result.conversationId !== conversationId) {
1031
- // Consumed, because this mailbox has exactly one waiter and the
1032
- // message would otherwise sit unacknowledged — but never silently:
1033
- // an answer that opened its own conversation is the single most
1034
- // likely way a peer's reply rule has drifted.
1035
- uncorrelated += 1;
1036
- stdout.write(paint(` note received an uncorrelated message from ${frame.result.fromAlias} (new conversation) — replies must use \`embassy send --conversation <token>\`\n`, "yellow"));
1037
- continue;
1038
- }
1039
- emit({ ok: true, name: "reply", detail: `${frame.result.fromAlias}${frame.result.text.includes(id) ? ` echoed ${id}` : ` answered without repeating ${id}`}`,
1040
- elapsedMs: lap() });
1041
- answered = true;
1042
- }
1043
- if (!answered) {
1044
- emit({ ok: false, name: "reply", detail: `no reply within ${String(Math.round(options.timeoutMs / 1_000))} s — the peer received the message but did not answer${uncorrelated === 0 ? "" : ` (${String(uncorrelated)} uncorrelated message(s) received)`}`,
1045
- elapsedMs: lap() });
1046
- return gatewayCliExitCodes.failure;
1047
- }
1048
- return gatewayCliExitCodes.ok;
1049
- };
1050
- let exitCode;
1051
- try {
1052
- exitCode = await roundTrip();
1053
- }
1054
- finally {
1055
- const released = await call("unregister_peer", { alias, token }).catch(() => undefined);
1056
- const failure = released === undefined ? "CONTROL_REQUEST_FAILED"
1057
- : !released.ok ? released.error.code
1058
- : released.result.accepted ? undefined : released.result.code;
1059
- emit({ ok: failure === undefined, name: "cleanup",
1060
- ...(failure === undefined ? {} : { code: failure }),
1061
- detail: failure === undefined ? "temporary check identity removed"
1062
- : `the temporary check identity could not be removed; it expires on its own within ${String(Math.round(ttlMs / 60_000))} min` });
1063
- }
1064
- return summarize(exitCode);
1065
- }
1066
- /** Run one command; foreground runners own and release their signal handlers. */
1067
- async function runServiceCommand(args, dependencies, loadIdentity) {
1068
- const command = "service";
1069
- const env = dependencies.env ?? process.env;
1070
- const stdout = dependencies.stdout ?? process.stdout, stderr = dependencies.stderr ?? process.stderr;
1071
- const sendRequest = dependencies.sendRequest ?? sendGatewayControlRequest;
1072
- const validateSocket = dependencies.validateControlSocket ?? validatePrivateGatewayControlSocket;
1073
- const success = (result) => {
1074
- stdout.write(serializedOutput({ ok: true, command, result }));
1075
- };
1076
- const subcommand = args[0];
1077
- if (args.length !== 1)
1078
- fault();
1079
- if (subcommand !== "install" && subcommand !== "uninstall" && subcommand !== "status")
1080
- fault();
1081
- // Identity and the state directory are validated before the first
1082
- // launchd side effect. Loading them afterwards meant a missing
1083
- // inventory or an unusable state root exited 2 "request rejected"
1084
- // while the agent was already bootstrapped and looping.
1085
- const config = subcommand === "install" ? (await loadIdentity()).config : undefined;
1086
- const serviceDeps = {
1087
- homeDir: (dependencies.serviceHomeDir ?? (() => userInfo().homedir))(),
1088
- runLaunchctl: dependencies.runLaunchctl ?? defaultRunLaunchctl,
1089
- env, execPath: process.execPath, cliPath: fileURLToPath(import.meta.url),
1090
- uid: process.getuid(),
1091
- delay: dependencies.delay ?? defaultDelay, now: dependencies.now ?? monotonicNow,
1092
- probeHostLease: dependencies.probeHostLease ?? defaultProbeHostLease,
1093
- };
1094
- try {
1095
- if (subcommand === "install") {
1096
- const installed = await installServiceAgent(serviceDeps);
1097
- const health = await pollServiceHealth(config, sendRequest, validateSocket, dependencies.delay ?? defaultDelay, dependencies.now ?? monotonicNow);
1098
- if (!health.ok) {
1099
- // The agent stays installed either way: this is a report about
1100
- // the broker, not an install failure, so nothing is rolled back.
1101
- const elapsed = (health.elapsedMs / 1000).toFixed(1);
1102
- const decisive = SERVICE_HEALTH_DECISIVE.get(health.lastObserved);
1103
- writeFailure(stdout, stderr, command, "SERVICE_HEALTH_UNAVAILABLE", {
1104
- retryable: decisive?.retryable ?? true, kind: decisive?.kind ?? "unavailable",
1105
- detail: { lastObserved: health.lastObserved },
1106
- });
1107
- stderr.write(decisive === undefined
1108
- ? `[embassy] Installed, but the broker did not answer within ${elapsed} s; last observed ${health.lastObserved}. Run \`embassy service status\` or \`embassy health\`; log: ${installed.logPath}.\n`
1109
- : `[embassy] Installed, but the broker answered ${health.lastObserved} after ${elapsed} s. Run \`embassy health\` to diagnose it; log: ${installed.logPath}.\n`);
1110
- if (decisive?.hint !== undefined)
1111
- stderr.write(hintLine(decisive.hint, env));
1112
- return decisive?.exitCode ?? gatewayCliExitCodes.unavailable;
1113
- }
1114
- success({ subcommand, ...installed, health });
1115
- return gatewayCliExitCodes.ok;
1116
- }
1117
- if (subcommand === "uninstall") {
1118
- success({ subcommand, ...(await uninstallServiceAgent(serviceDeps)) });
1119
- return gatewayCliExitCodes.ok;
1120
- }
1121
- const status = await serviceAgentStatus(serviceDeps);
1122
- success({ subcommand, ...status });
1123
- if (status.state !== "unknown")
1124
- return gatewayCliExitCodes.ok;
1125
- stderr.write(fixedStderr("unavailable"));
1126
- stderr.write(`[embassy] ${status.note}${status.launchctlStderr === undefined ? "" : ` launchctl: ${status.launchctlStderr}`}\n`);
1127
- return gatewayCliExitCodes.unavailable;
1128
- }
1129
- catch (error) {
1130
- // launchctl's own stderr and the instance lease's own message are the
1131
- // whole value of these failures; the generic handler below discards
1132
- // the message and reports only the code. A genuine filesystem failure
1133
- // on this path (an unreadable plist, an undeletable one) is a real,
1134
- // recoverable service failure, not an INTERNAL_ERROR — but only an
1135
- // errno-shaped one. A string `code` alone would also match CliFault
1136
- // and the lease's own spawn failures, relabelling faults that already
1137
- // carry a truer code of their own.
1138
- const errno = error !== null && typeof error === "object" &&
1139
- (typeof error.errno === "number" ||
1140
- typeof error.syscall === "string");
1141
- if (!(error instanceof BridgeError) && !(errno && error instanceof Error))
1142
- throw error;
1143
- const failure = error instanceof BridgeError ? error : new BridgeError("SERVICE_AGENT_FILESYSTEM_FAILED", `The service command could not complete: ${boundedServiceDetail(error.message)}`, true);
1144
- writeFailure(stdout, stderr, command, failure.code, {
1145
- retryable: failure.recoverable,
1146
- kind: failure.code === "SERVICE_AGENT_PATH_UNSAFE" ? "unsafe"
1147
- : failure.recoverable ? "unavailable" : "input",
1148
- });
1149
- stderr.write(`[embassy] ${failure.message}\n`);
1150
- return failure.recoverable ? gatewayCliExitCodes.unavailable : gatewayCliExitCodes.invalidInput;
1151
- }
1152
- }
1153
- export async function runGatewayCli(argv = process.argv.slice(2), dependencies = {}) {
1154
- const env = dependencies.env ?? process.env;
1155
- const stdin = dependencies.stdin ?? process.stdin, stdout = dependencies.stdout ?? process.stdout;
1156
- const stderr = dependencies.stderr ?? process.stderr, loadConfig = dependencies.loadConfig ?? loadGatewayConfig;
1157
- const sendRequest = dependencies.sendRequest ?? sendGatewayControlRequest;
1158
- const validateSocket = dependencies.validateControlSocket ?? validatePrivateGatewayControlSocket;
1159
- if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
1160
- stdout.write(`embassy ${EMBASSY_VERSION}\n`);
1161
- return gatewayCliExitCodes.ok;
1162
- }
1163
- const command = isCommand(argv[0]) ? argv[0] : undefined;
1164
- const args = argv.slice(1);
1165
- let serverReady = false;
1166
- let identity;
1167
- const loadIdentity = () => identity ??= (async () => {
1168
- const inventory = await (dependencies.loadNodeInventory ?? loadGatewayNodeInventory)(path.resolve(defaultGatewayStateDir(env)));
1169
- return { inventory, config: loadConfig(env, inventory), defaulted: isDefaultedGatewayNodeInventory(inventory) };
1170
- })();
1171
- const success = (result) => {
1172
- stdout.write(serializedOutput({ ok: true, command: command, result }));
1173
- };
1174
- try {
1175
- if (argv.length === 0 || argv[0] === "--help" || argv[0] === "-h") {
1176
- emptyParams(args);
1177
- stdout.write(HELP_USAGE);
1178
- return gatewayCliExitCodes.ok;
1179
- }
1180
- if (command === undefined)
1181
- fault("UNKNOWN_COMMAND");
1182
- if (command === "peer-stdio") {
1183
- emptyParams(args);
1184
- try {
1185
- const { config, inventory } = await loadIdentity();
1186
- await validateSocket(config.stateDir, config.controlSocketPath);
1187
- let peerHost, firstCatalog;
1188
- const request = async (method, params) => {
1189
- const response = await sendRequest({ socketPath: config.controlSocketPath,
1190
- request: { protocolVersion: GATEWAY_CONTROL_PROTOCOL_VERSION, method, params } });
1191
- if (!response.ok)
1192
- throw new PeerHandlerError({ code: -32000, message: "Local broker refused peer authority" });
1193
- return response.result;
1194
- };
1195
- const session = (dependencies.runPeerStdio ?? runPeerStdio)({
1196
- localHost: inventory.host, input: stdin, output: stdout,
1197
- handlers: {
1198
- initialize: async ({ host }) => {
1199
- if (!inventory.nodes.includes(host))
1200
- throw new PeerHandlerError({ code: -32001, message: "Peer host not configured" });
1201
- peerHost = host;
1202
- firstCatalog = await request("peer_catalog", { peerHost: host });
1203
- },
1204
- catalog: async () => {
1205
- const cached = firstCatalog;
1206
- firstCatalog = undefined;
1207
- return cached ?? await request("peer_catalog", { peerHost: peerHost });
1208
- },
1209
- handoff: async (handoff) => {
1210
- firstCatalog = undefined;
1211
- const result = await request("peer_handoff", { peerHost: peerHost, handoff });
1212
- if (!result.accepted)
1213
- throw new PeerHandlerError({
1214
- code: -32000, message: "Peer handoff refused", data: result
1215
- });
1216
- return result;
1217
- },
1218
- },
1219
- });
1220
- await session.done;
1221
- return gatewayCliExitCodes.ok;
1222
- }
1223
- catch {
1224
- stderr.write("Embassy peer transport failed.\n");
1225
- return gatewayCliExitCodes.unavailable;
1226
- }
1227
- }
1228
- if (command === "service")
1229
- return await runServiceCommand(args, dependencies, loadIdentity);
1230
- if (command === "status" || command === "watch" || command === "check") {
1231
- // Options are parsed before anything is loaded or contacted, so an
1232
- // unknown flag is a flat argument fault with no side effect at all.
1233
- const options = command === "status" ? parseOptions(args, ["recent"], ["json"])
1234
- : command === "watch" ? parseOptions(args, [], ["json"])
1235
- : parseOptions(args, ["to", "timeout"]);
1236
- count(options, 0, 2);
1237
- const recent = boundedOption(options, "recent", STATUS_RECENT.default, STATUS_RECENT.minimum, STATUS_RECENT.maximum);
1238
- const timeoutSeconds = boundedOption(options, "timeout", CHECK_TIMEOUT_DEFAULT_SECONDS, 1, CHECK_TIMEOUT_MAX_SECONDS);
1239
- const target = options.to === undefined ? undefined : requireDestinationSelector(options, "to");
1240
- const { config } = await loadIdentity();
1241
- await validateSocket(config.stateDir, config.controlSocketPath);
1242
- const ask = async (method, params) => await sendRequest({
1243
- socketPath: config.controlSocketPath,
1244
- request: envelope(method, params),
1245
- });
1246
- if (command === "status") {
1247
- // `status` is read-only. It deliberately does NOT rescan: a rescan
1248
- // performs the passive-discovery scan SECURITY.md reserves for an
1249
- // explicit request, and journals a `discovery_refreshed` row into the
1250
- // same bounded activity ring this pane exists to show — 256 status
1251
- // calls would evict every route retirement it was meant to surface.
1252
- // The header reports how old the scan is and offers `embassy refresh`.
1253
- const response = await ask("list_snapshot", {});
1254
- if (!response.ok) {
1255
- return reportControlFailure(command, response.error.code, stdout, stderr);
1256
- }
1257
- if (options.json === true || !isTerminal(stdout)) {
1258
- success(response.result);
1259
- return gatewayCliExitCodes.ok;
1260
- }
1261
- const pid = await (dependencies.readControllerPid ?? readGatewayControllerPid)(config.stateDir);
1262
- stdout.write(renderStatus(response.result, {
1263
- stateDir: config.stateDir, version: EMBASSY_VERSION, recent,
1264
- color: useColor(stdout, env), now: (dependencies.now ?? Date.now)(),
1265
- ...(pid === undefined ? {} : { pid }),
1266
- }));
1267
- return gatewayCliExitCodes.ok;
1268
- }
1269
- if (command === "watch") {
1270
- const json = options.json === true;
1271
- const color = !json && useColor(stdout, env);
1272
- const interrupt = interruptSignal(dependencies.watchSignal);
1273
- const delay = dependencies.delay ?? defaultDelay;
1274
- let state;
1275
- let revision;
1276
- try {
1277
- while (!interrupt.signal.aborted) {
1278
- const observed = await ask("observe_snapshot", {});
1279
- if (!observed.ok) {
1280
- return reportControlFailure(command, observed.error.code, stdout, stderr);
1281
- }
1282
- if (observed.result.snapshotRevision !== revision) {
1283
- revision = observed.result.snapshotRevision;
1284
- const diff = diffWatch(state ?? emptyWatchState, observed.result.snapshot);
1285
- // The first poll only establishes the baseline: replaying the
1286
- // whole retained ledger the moment a tail starts is noise.
1287
- if (state !== undefined) {
1288
- for (const event of diff.events) {
1289
- stdout.write(json ? `${JSON.stringify(event)}\n`
1290
- : `${renderWatchEvent(event, color)}\n`);
1291
- }
1292
- }
1293
- state = diff.state;
1294
- }
1295
- if (interrupt.signal.aborted)
1296
- break;
1297
- await delay(WATCH_POLL_INTERVAL_MS);
1298
- }
1299
- }
1300
- finally {
1301
- interrupt.dispose();
1302
- }
1303
- return gatewayCliExitCodes.ok;
1304
- }
1305
- const clock = dependencies.now ?? Date.now;
1306
- let chosen = target;
1307
- if (chosen === undefined) {
1308
- const snapshot = await ask("list_snapshot", {});
1309
- if (!snapshot.ok) {
1310
- return reportControlFailure(command, snapshot.error.code, stdout, stderr);
1311
- }
1312
- const candidates = snapshot.result.routes
1313
- .filter((route) => route.provider === "codex" && route.enabled)
1314
- .sort((left, right) => left.alias.localeCompare(right.alias));
1315
- if (candidates.length === 0) {
1316
- throw new CliFault("INVALID_ARGUMENTS", false, "checkNoTarget", undefined, { localHost: config.hostId });
1317
- }
1318
- // Eligibility is observation. A task nothing has observed — ever, or
1319
- // within ten minutes — proves nothing about upstream drift; a send
1320
- // into it just times out. The most recently observed eligible task is
1321
- // the target, and when none qualifies every alias is named so the
1322
- // operator can read its remedy.
1323
- const observedAt = (route) => {
1324
- const at = route.lastSeenAt === undefined ? Number.NaN : Date.parse(route.lastSeenAt);
1325
- return Number.isFinite(at) ? at : undefined;
1326
- };
1327
- const eligible = candidates
1328
- .map((route) => ({ route, observed: observedAt(route) }))
1329
- .filter((row) => row.observed !== undefined && clock() - row.observed <= STATUS_ROUTE_STALE_AFTER_MS)
1330
- .sort((left, right) => right.observed - left.observed || left.route.alias.localeCompare(right.route.alias));
1331
- if (eligible.length === 0) {
1332
- throw new CliFault("INVALID_ARGUMENTS", false, "checkAllStale", undefined, { aliases: candidates.map((route) => route.alias).join(", ") });
1333
- }
1334
- chosen = eligible[0].route.alias;
1335
- }
1336
- const checkInterrupt = interruptSignal(dependencies.watchSignal);
1337
- try {
1338
- return await runGatewayCheck({
1339
- socketPath: config.controlSocketPath, hostId: config.hostId, target: chosen,
1340
- timeoutMs: timeoutSeconds * 1_000, sendRequest, stdout, stderr,
1341
- interrupt: checkInterrupt.signal, now: clock,
1342
- delay: dependencies.delay ?? defaultDelay, color: useColor(stdout, env),
1343
- });
1344
- }
1345
- finally {
1346
- if (dependencies.watchSignal === undefined && checkInterrupt.signal.aborted)
1347
- interruptedCheckExit = true;
1348
- checkInterrupt.dispose();
1349
- }
1350
- }
1351
- if (command === "serve") {
1352
- emptyParams(args);
1353
- await (dependencies.runServer ?? runGatewayServer)({
1354
- env,
1355
- ...(dependencies.serverSignal === undefined ? {} : { signal: dependencies.serverSignal }),
1356
- onReady: async (result) => {
1357
- if (serverReady)
1358
- fault("SERVER_READY_ALREADY_EMITTED");
1359
- success(result);
1360
- serverReady = true;
1361
- },
1362
- });
1363
- if (!serverReady)
1364
- fault("SERVER_NOT_READY");
1365
- return gatewayCliExitCodes.ok;
1366
- }
1367
- const request = await buildRequest(command, args, env, stdin, async () => {
1368
- const { config, defaulted } = await loadIdentity();
1369
- return { host: config.hostId, defaulted, stateDir: config.stateDir };
1370
- });
1371
- const { config } = await loadIdentity();
1372
- await validateSocket(config.stateDir, config.controlSocketPath);
1373
- let response;
1374
- let waited;
1375
- if (command === "await") {
1376
- if (request.method !== "await_peer")
1377
- fault();
1378
- while (true) {
1379
- const current = await sendRequest({ socketPath: config.controlSocketPath, request, timeoutMs: PEER_AWAIT_REQUEST_TIMEOUT_MS });
1380
- if (!current.ok) {
1381
- response = current;
1382
- break;
1383
- }
1384
- if (current.result.state === "timeout")
1385
- continue;
1386
- try {
1387
- await writeComplete(stdout, current.result.frame);
1388
- }
1389
- catch {
1390
- stderr.write(fixedStderr("failure"));
1391
- return gatewayCliExitCodes.failure;
1392
- }
1393
- try {
1394
- const receipt = await sendRequest({ socketPath: config.controlSocketPath,
1395
- request: envelope("peer_receipt", { alias: request.params.alias, token: request.params.token, receipt: current.result.receipt }) });
1396
- if (!receipt.ok) {
1397
- stderr.write(fixedStderr("failure"));
1398
- return gatewayCliExitCodes.failure;
1399
- }
1400
- if (isRejectedResult(receipt.result)) {
1401
- stderr.write(fixedStderr("decision"));
1402
- return gatewayCliExitCodes.rejected;
1403
- }
1404
- return gatewayCliExitCodes.ok;
1405
- }
1406
- catch (error) {
1407
- const transport = error instanceof GatewayControlTransportError;
1408
- stderr.write(fixedStderr(transport ? error.ambiguous ? "ambiguous" : "unavailable" : "failure"));
1409
- return transport ? error.ambiguous ? gatewayCliExitCodes.ambiguous : gatewayCliExitCodes.unavailable : gatewayCliExitCodes.failure;
1410
- }
1411
- }
1412
- }
1413
- else if (command === "wait-delivery") {
1414
- if (request.method !== "delivery_status")
1415
- fault();
1416
- const outcome = await waitForDelivery(config.controlSocketPath, request, sendRequest, dependencies.now ?? Date.now, dependencies.delay ?? defaultDelay);
1417
- if (outcome.kind === "unknown") {
1418
- writeFailure(stdout, stderr, command, "DELIVERY_TOKEN_UNKNOWN", { kind: "tokenUnknown" });
1419
- return gatewayCliExitCodes.rejected;
1420
- }
1421
- // `timeout` is the only other outcome here: an interrupt needs a signal,
1422
- // and `wait-delivery` passes none.
1423
- if (outcome.kind !== "response") {
1424
- writeFailure(stdout, stderr, command, "DELIVERY_WAIT_TIMEOUT", { retryable: true, kind: "deliveryTimeout" });
1425
- return gatewayCliExitCodes.unavailable;
1426
- }
1427
- waited = outcome.response;
1428
- response = waited;
1429
- }
1430
- else {
1431
- response = await sendRequest({ socketPath: config.controlSocketPath, request });
1432
- }
1433
- if (!response.ok) {
1434
- return reportControlFailure(command, response.error.code, stdout, stderr);
1435
- }
1436
- if (command === "register-peer" && args.includes("--emit-env") && "token" in response.result) {
1437
- stdout.write(`export EMBASSY_PEER_TOKEN='${response.result.token}'\n`);
1438
- return gatewayCliExitCodes.ok;
1439
- }
1440
- success(response.result);
1441
- const exitCode = waited === undefined ? responseExitCode(response) : waitDeliveryExitCode(waited);
1442
- if (exitCode === gatewayCliExitCodes.rejected) {
1443
- stderr.write(fixedStderr("decision"));
1444
- const hint = refusalHint(request, response.result);
1445
- if (hint !== undefined)
1446
- stderr.write(`[embassy] ${renderHint(hint.hint, hint.vars)}\n`);
1447
- }
1448
- else if (command === "wait-delivery" && exitCode === gatewayCliExitCodes.failure)
1449
- stderr.write(fixedStderr("failure"));
1450
- return exitCode;
1451
- }
1452
- catch (error) {
1453
- if (command === "serve" && serverReady) {
1454
- stderr.write(fixedStderr("failure"));
1455
- return gatewayCliExitCodes.failure;
1456
- }
1457
- if (error instanceof GatewayControlTransportError) {
1458
- const ambiguous = error.ambiguous;
1459
- writeFailure(stdout, stderr, command, error.code, {
1460
- ambiguous, retryable: ambiguous ? false : error.recoverable,
1461
- kind: ambiguous ? "ambiguous" : "unavailable",
1462
- });
1463
- if (error.code === "CONTROL_VERSION_MISMATCH") {
1464
- stderr.write(`[embassy] ${CLI_HINT.controlVersionMismatch}\n`);
1465
- }
1466
- else if (error.code === "CONTROL_INVALID_RESPONSE") {
1467
- stderr.write(`[embassy] ${CLI_HINT.controlInvalidResponse}\n`);
1468
- }
1469
- if (error.code === "CONTROL_CONNECT_DENIED")
1470
- stderr.write(`[embassy] ${CLI_HINT.controlConnectDenied}\n`);
1471
- if (isNoBrokerCode(error.code))
1472
- stderr.write(hintLine("noBrokerRunning", env));
1473
- return ambiguous ? gatewayCliExitCodes.ambiguous : gatewayCliExitCodes.unavailable;
1474
- }
1475
- if (error instanceof CliFault) {
1476
- writeFailure(stdout, stderr, command, error.code, {
1477
- retryable: error.retryable, kind: error.kind ?? (error.retryable ? "unavailable" : "input"),
1478
- });
1479
- // Every hint may name the state directory; a fault that carries its own
1480
- // bounded values overrides that default with them.
1481
- if (error.hint !== undefined)
1482
- stderr.write(`[embassy] ${renderHint(error.hint, { stateDir: resolvedStateDirForHint(env), ...error.hintVars })}\n`);
1483
- if (isNoBrokerCode(error.code))
1484
- stderr.write(hintLine("noBrokerRunning", env));
1485
- return error.retryable ? gatewayCliExitCodes.unavailable : gatewayCliExitCodes.invalidInput;
1486
- }
1487
- if (error instanceof BridgeError) {
1488
- if (error.code === "GATEWAY_STATE_COMMIT_OUTCOME_UNKNOWN") {
1489
- writeFailure(stdout, stderr, command, error.code, { ambiguous: true, kind: "ambiguous" });
1490
- return gatewayCliExitCodes.ambiguous;
1491
- }
1492
- writeFailure(stdout, stderr, command, error.code, {
1493
- retryable: error.recoverable, kind: error.recoverable ? "unavailable" : "input",
1494
- });
1495
- writeStateResetHint(stderr, error.code);
1496
- writeBridgeErrorHint(stderr, error, env);
1497
- if (error.code === "CONTROL_CONNECT_DENIED")
1498
- stderr.write(`[embassy] ${CLI_HINT[command === "serve" ? "stateAccessDenied" : "controlConnectDenied"]}\n`);
1499
- return error.recoverable ? gatewayCliExitCodes.unavailable : gatewayCliExitCodes.invalidInput;
1500
- }
1501
- writeFailure(stdout, stderr, command, "INTERNAL_ERROR", { kind: "failure" });
1502
- return gatewayCliExitCodes.failure;
1503
- }
1504
- }
1505
- function isDirectExecution() {
1506
- const entry = process.argv[1];
1507
- if (entry === undefined)
1508
- return false;
1509
- try {
1510
- // npm exposes package binaries through symlinks. Compare canonical paths
1511
- // so an installed `embassy` executable is recognized as the entry point,
1512
- // while importing this module in tests remains side-effect free.
1513
- return realpathSync(entry) === realpathSync(fileURLToPath(import.meta.url));
1514
- }
1515
- catch {
1516
- return false;
1517
- }
1518
- }
1519
- if (isDirectExecution()) {
1520
- void runGatewayCli().then((exitCode) => {
1521
- process.exitCode = exitCode;
1522
- // Bounded exit after an interrupted `check`: drain stdout, then stderr,
1523
- // then leave — see `interruptedCheckExit`.
1524
- if (interruptedCheckExit) {
1525
- process.stdout.write("", () => process.stderr.write("", () => process.exit(exitCode)));
1526
- }
1527
- }, () => {
1528
- process.stderr.write(fixedStderr("failure"));
1529
- process.exitCode = gatewayCliExitCodes.failure;
1530
- });
1531
- }
1532
- //# sourceMappingURL=cli.js.map