@sjawhar/claude-envoy 4.6.1 → 4.7.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.
@@ -2,5 +2,5 @@
2
2
  "name": "claude-envoy",
3
3
  "description": "Put Envoy events into the Claude Code session's own conversation and expose Envoy MCP tools.",
4
4
  "mcpServers": "./.mcp.json",
5
- "version": "4.6.1"
5
+ "version": "4.7.0"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-envoy",
3
- "version": "4.6.1",
3
+ "version": "4.7.0",
4
4
  "description": "Put Envoy events into the Claude Code session's own conversation and expose Envoy MCP tools.",
5
5
  "mcpServers": {}
6
6
  }
package/README.md CHANGED
@@ -170,17 +170,33 @@ message from Dispatch's Agents page as that person's own turn.
170
170
  summary (the same shape as OMP on fork). `ENVOY_SESSION_ID` (QA override) disables the handoff.
171
171
  - `envoy_inbox` is local recovery state: the most recent 50 event summaries, with no envelope
172
172
  body, ordered newest first.
173
- - `bin/envoy-send.ts` sends direct messages through `@legion/envoy-client`'s `send`, the
174
- transport every other client uses, to the listener and with the bearer a command resolves:
175
- `envoySettings` over the envoy.json `envoyConfigPath("command")` chooses (the `ENVOY_CONFIG_FILE`
176
- the `open-asks` hook hands the session's Bash first), with `ENVOY_URL` and `ENVOY_TOKEN_FILE` or
177
- `ENVOY_TOKEN` over its `envoy` section. It sends the session its shell names
178
- (`ENVOY_SESSION_ID`, else `CLAUDE_CODE_SESSION_ID`) as `source_session`, so the listener's limit
179
- on each session's sending counts its messages. After no answer or a 5xx it sends the message
180
- once more under the same idempotency key, and Envoy drops that copy as a repeat. A refusal, one
181
- for the sender's rate among them, prints the listener's own sentence after
182
- `Claude Envoy send failed: `, exits 1 and is not sent again. With Envoy off it sends nothing and
183
- exits 1, saying why.
173
+ - `bin/envoy-send.ts` sends direct messages through the Envoy listener's HTTP API, to the listener
174
+ and with the bearer a command resolves: `envoySettings` over the envoy.json
175
+ `envoyConfigPath("command")` chooses (the `ENVOY_CONFIG_FILE` the `open-asks` hook hands the
176
+ session's Bash first), with `ENVOY_URL` and `ENVOY_TOKEN_FILE` or `ENVOY_TOKEN` over its `envoy`
177
+ section. With Envoy off it sends nothing and exits 1, saying why. It sends through envoy-client's
178
+ own send (`createEnvoyClient().send`), the transport every other client uses: one idempotency key
179
+ on every attempt, so the listener stores the message at most once; an attempt with no answer or a
180
+ 5xx sent again under that key; and the listener's URL named only as `urlForText` names it, never
181
+ its user info. It sends the session its shell names (`ENVOY_SESSION_ID`, else
182
+ `CLAUDE_CODE_SESSION_ID`) as `source_session`, so the listener's limit on each session's sending
183
+ counts its messages. A send the listener stored without this machine's credential (a listener
184
+ that asks for none takes one whose broker trade failed) prints its `Sent` line, exits 0, and
185
+ writes one line to stderr naming why it went without the credential, and where the refusal is
186
+ final (a revoked sign-in) that it is, with its remedy. A send that may have arrived is not a
187
+ failure: it exits 75 (`EX_TEMPFAIL`), names the key, and prints the command that sends it again
188
+ under that key, which the listener drops as a repeat if the first copy arrived: `cd <dir> &&
189
+ [ENVOY_CONFIG_FILE=<file>] [ENVOY_URL=…] [ENVOY_TOKEN_FILE=…] [ENVOY_SESSION_ID=…] bun <the
190
+ envoy-send.ts it ran> --idempotency-key <key> <target> <message>`, which carries what chose the
191
+ listener so it goes there from any shell (`ENVOY_CONFIG_FILE` where an envoy.json was read, the
192
+ others where set), each word quoted so bash and zsh read it alike. Where that command would
193
+ hold a secret this process withholds (the message names one, say), a printed copy would send a
194
+ different message under the same key, and where its `ENVOY_URL` is not a plain http(s) URL (one
195
+ with user info of any length, say) the URL is never printed; either way only the key is printed,
196
+ with how to resend the original. A resend under a key exits 75 on any failure, since the send
197
+ before it may have arrived. Any other refusal, one for the sender's rate among them, prints the
198
+ listener's own sentence after `Claude Envoy send failed: `, exits 1 and is not sent again; so
199
+ does a listener that refused the connection or whose name did not resolve.
184
200
 
185
201
  ## What a delivered line carries
186
202
 
@@ -502,12 +518,15 @@ token beside it, and its token only beside its own server. An `envoy` section in
502
518
  file is not read, which the channel server's notice says, and a checkout's file that is not valid
503
519
  refuses Dispatch alone, only while `DISPATCH_URL` is unset. The chosen file's `dispatch` section
504
520
  (`enabled`, `serverUrl`, `token`) and `envoy` section (`enabled`, `url`, `natsUrls`, `token`,
505
- `natsSeed`) are resolved each on its own; `enabled: false` turns one off, a missing file
521
+ `natsSeed`, `natsAuth`) are resolved each on its own; `enabled: false` turns one off, a missing file
506
522
  contributes nothing (each section is then on only when its variables name it), and a `token` or
507
523
  `natsSeed` starting with `!` is a command whose trimmed output is the value. `DISPATCH_URL`,
508
524
  `DISPATCH_TOKEN_FILE`, `DISPATCH_TOKEN`, `ENVOY_URL`, `ENVOY_NATS_URL`, `ENVOY_TOKEN_FILE`,
509
- `ENVOY_TOKEN`, `NATS_NKEY_SEED_FILE` and `NATS_NKEY_SEED` each override their own field; a file's
510
- token or seed goes with a variable's server only when it is the file's own. The `open-asks` hook
525
+ `ENVOY_TOKEN`, `NATS_NKEY_SEED_FILE`, `NATS_NKEY_SEED` and `NATS_AUTH` each override their own
526
+ field; a file's token or seed goes with a variable's server only when it is the file's own. With
527
+ `NATS_AUTH=broker` and no seed the NATS connection presents the session's own broker token, which
528
+ a hub's NATS auth service admits to that session's subjects alone, and a `/clear` moves it to the
529
+ new id's token before the new id's relay subject is subscribed. The `open-asks` hook
511
530
  hands the file on to the session's Bash as `ENVOY_CONFIG_FILE` beside `DISPATCH_HOST=claude`, and
512
531
  the `dispatch` command reads the file `ENVOY_CONFIG_FILE` names whenever `DISPATCH_HOST` is set; a
513
532
  Bash with neither, as a subagent's can be, has `CLAUDE_CODE_CHILD_SESSION=1` and takes this plugin's
@@ -1,14 +1,28 @@
1
+ import { scrubProcessStreams } from "@legion/contracts/held-secrets"
1
2
  import { runCodexEnvoyServer } from "../src/codex/codex-server"
2
3
  import { runEnvoyChannelServer } from "../src/envoy-channel-server"
4
+ import { exitProcess } from "../src/process-exit"
3
5
 
4
6
  // `codex/.codex-plugin/plugin.json` starts this bundle with `--host codex`; Claude Code's
5
7
  // `.mcp.json` starts it with no argument.
6
8
  const codex = process.argv.slice(2).join(" ") === "--host codex"
7
9
 
10
+ // Every line this process writes, its MCP stream on stdout and its failures on stderr, passes the
11
+ // scrub of the secrets it holds (`@legion/contracts/held-secrets`).
12
+ scrubProcessStreams()
13
+
8
14
  try {
9
15
  await (codex ? runCodexEnvoyServer() : runEnvoyChannelServer())
16
+ // The server's teardown has run. A handle nothing will use can outlive it (a NATS dial that a
17
+ // stop ended waits out nats.js's own dial timeout), so the process ends here, once what it
18
+ // wrote has reached the reader, rather than when that handle lets go.
19
+ await exitProcess(0)
10
20
  } catch (error) {
11
21
  const message = error instanceof Error ? error.message : "unknown channel server failure"
22
+ // The server's own teardown has run (its `finally`): the mod link is closed and the session, if
23
+ // one started, deregistered. A failed start ends the process, rather than leaving it alive on
24
+ // whatever handle is left (its stdin, a timer) with no session behind it, once the message and
25
+ // everything before it has been written.
12
26
  process.stderr.write(`${codex ? "Codex" : "Claude"} Envoy channel server failed: ${message}\n`)
13
- process.exitCode = 1
27
+ await exitProcess(1)
14
28
  }
package/bin/envoy-send.ts CHANGED
@@ -1,11 +1,80 @@
1
+ import { isFinalRefusal } from "@legion/broker-client/token-errors"
2
+ import { holdUrlUserinfo, scrub, scrubProcessStreams } from "@legion/contracts/held-secrets"
3
+ import { URL_NOT_PRINTED } from "@legion/contracts/log-text"
1
4
  import { messageFor } from "@legion/envoy-client/errors"
2
5
  import { sessionConfigFile, sessionEnvoySettings } from "@legion/envoy-client/session-settings"
3
- import { createEnvoyClient } from "@legion/envoy-client/transport"
6
+ import { EnvoySendUnconfirmedError } from "@legion/envoy-client/transport"
4
7
  import { configuredSessionId } from "../src/claude-session"
5
- import { parseSendArguments } from "../src/send-arguments"
8
+ import { sendEnvoyMessage } from "../src/envoy-client"
9
+ import {
10
+ IDEMPOTENCY_KEY_FLAG,
11
+ parseSendArguments,
12
+ type Resend,
13
+ resendCommand,
14
+ type SendArguments,
15
+ } from "../src/send-arguments"
6
16
 
17
+ /** The exit of a send that may or may not have arrived (sysexits' `EX_TEMPFAIL`), apart from a
18
+ * refusal's 1: a script tells the two apart by status, and resends only under the key the
19
+ * message names. */
20
+ const EXIT_UNCONFIRMED = 75
21
+
22
+ /** `name`'s value in this process's environment, where it is set and not empty. */
23
+ function setting(name: string): string | undefined {
24
+ const value = process.env[name]
25
+ return value === undefined || value === "" ? undefined : value
26
+ }
27
+
28
+ /**
29
+ * Prints how to send the message again under `key`. Where the command can be printed as it would
30
+ * run — every URL setting it carries is one a line may name, and the scrub of held secrets leaves
31
+ * it whole — it is printed ready to run. Otherwise the key is printed with how to resend the
32
+ * original, and no command: a URL setting that is not plain (`ENVOY_URL` with user info, of any
33
+ * length) is never printed (`resendCommand`), and where the scrub would change the command (the
34
+ * message holds a secret this process withholds) a printed copy would send something else under
35
+ * the same key.
36
+ */
37
+ function printResend(resend: Resend): void {
38
+ const printed = resendCommand(resend)
39
+ if ("command" in printed && scrub(printed.command) === printed.command) {
40
+ const token =
41
+ setting("ENVOY_TOKEN") === undefined
42
+ ? ""
43
+ : " (it presents the ENVOY_TOKEN of the shell it runs in, which this line does not print)"
44
+ process.stderr.write(`To send it again safely, run: ${printed.command}${token}\n`)
45
+ return
46
+ }
47
+ const why =
48
+ "unprinted" in printed
49
+ ? `the command is not printed, since it holds ${printed.unprinted} ${URL_NOT_PRINTED}.`
50
+ : "the command is not printed because it holds a secret this process withholds, so a printed copy could not reproduce the message, and would send a different one under the same key."
51
+ process.stderr.write(
52
+ `To send it again safely, run envoy-send again from ${resend.cwd} with ${IDEMPOTENCY_KEY_FLAG} ${resend.idempotencyKey}, the same target and the same message, with the settings this run had: ${why}\n`,
53
+ )
54
+ }
55
+
56
+ /**
57
+ * The one line a send the listener stored without this machine's credential prints beside its
58
+ * Sent line, naming why it went without: the credential's error as the transport names it where
59
+ * the send fails for it (`messageFor`, which names no token), after what went without it. A final
60
+ * refusal (`isFinalRefusal`: a revoked sign-in, a broker's refusal no retry cures) is named as one,
61
+ * since every later send from this machine goes without the credential too, and a listener that
62
+ * asks for it refuses each; its remedy is in its own words (`run: agent-secrets login`).
63
+ */
64
+ function wentWithout(error: unknown): string {
65
+ const why = isFinalRefusal(error)
66
+ ? "the credential was refused for good, so no retry mints it, and a listener that asks for it refuses every send from this machine until the refusal is cured"
67
+ : "the credential could not be minted"
68
+ return `Claude Envoy sent the message without this machine's Envoy credential, which this listener does not ask for; ${why}: ${messageFor(error)}\n`
69
+ }
70
+
71
+ // Every line this command writes passes the scrub of the secrets it holds.
72
+ scrubProcessStreams()
73
+
74
+ let send: SendArguments | undefined
75
+ let resend: Omit<Resend, "idempotencyKey"> | undefined
7
76
  try {
8
- const { targetSession, message } = parseSendArguments(process.argv.slice(2))
77
+ send = parseSendArguments(process.argv.slice(2))
9
78
  // The listener and bearer every command resolves: the envoy.json `envoyConfigPath("command")`
10
79
  // chooses (the ENVOY_CONFIG_FILE a session's plugin hands its shell first), with ENVOY_URL and
11
80
  // ENVOY_TOKEN_FILE or ENVOY_TOKEN over its envoy section. A send opens no NATS connection.
@@ -14,23 +83,61 @@ try {
14
83
  ENVOY_SESSION_ID: process.env["ENVOY_SESSION_ID"],
15
84
  CLAUDE_CODE_SESSION_ID: process.env["CLAUDE_CODE_SESSION_ID"],
16
85
  })
17
- const envoy = sessionEnvoySettings(sessionConfigFile("command", process.env), process.env, {
86
+ const file = sessionConfigFile("command", process.env)
87
+ const envoy = sessionEnvoySettings(file, process.env, {
18
88
  cwd: process.cwd(),
19
89
  nats: false,
20
90
  ...(session === undefined ? {} : { session }),
21
91
  })
22
92
  if (!envoy.enabled) throw new Error(`Envoy is off: ${envoy.error ?? envoy.notice}`)
23
- // The transport every other client sends through. It names this session as the message's
24
- // source, so the listener counts the message against this session's sending limit; a shell that
25
- // names no session sends a blank one, which the transport leaves off the wire. A refusal is an
26
- // `EnvoyApiError` whose message is the listener's own sentence, which the catch below prints.
27
- await createEnvoyClient({ baseUrl: envoy.url, credential: envoy.credential, fetch }).send({
28
- sourceSessionID: session ?? "",
29
- targetSessionID: targetSession,
30
- message,
93
+ // What chose this send's listener and credential, which a resend carries so it goes where this
94
+ // send went from any shell: the envoy.json read (a set ENVOY_CONFIG_FILE wins over every other
95
+ // way of finding one), ENVOY_URL and ENVOY_TOKEN_FILE where set, and the session.
96
+ const url = setting("ENVOY_URL")
97
+ if (url !== undefined) holdUrlUserinfo(url)
98
+ const settings: [string, string][] = []
99
+ if (file.path !== null) settings.push(["ENVOY_CONFIG_FILE", file.path])
100
+ if (url !== undefined) settings.push(["ENVOY_URL", url])
101
+ const tokenFile = setting("ENVOY_TOKEN_FILE")
102
+ if (tokenFile !== undefined) settings.push(["ENVOY_TOKEN_FILE", tokenFile])
103
+ if (session !== undefined) settings.push(["ENVOY_SESSION_ID", session])
104
+ resend = {
105
+ script: process.argv[1] ?? "envoy-send.ts",
106
+ cwd: process.cwd(),
107
+ settings,
108
+ targetSession: send.targetSession,
109
+ message: send.message,
110
+ }
111
+ const sent = await sendEnvoyMessage({
112
+ baseUrl: envoy.url,
113
+ credential: envoy.credential,
114
+ targetSession: send.targetSession,
115
+ message: send.message,
116
+ ...(send.idempotencyKey === undefined ? {} : { idempotencyKey: send.idempotencyKey }),
117
+ sourceSession: session ?? "",
31
118
  })
32
- process.stdout.write(`Sent Envoy message to ${targetSession}.\n`)
119
+ process.stdout.write(`Sent Envoy message to ${send.targetSession}.\n`)
120
+ // Stored, so it exits 0; but a send that went without its credential says why, once.
121
+ if (sent.withoutCredential !== undefined)
122
+ process.stderr.write(wentWithout(sent.withoutCredential))
33
123
  } catch (error) {
34
- process.stderr.write(`Claude Envoy send failed: ${messageFor(error)}\n`)
35
- process.exitCode = 1
124
+ // A send that may have arrived is not a failure, and nor is any failure of a resend that names
125
+ // a key: the send under that key before it may have arrived, so a refusal now says nothing of
126
+ // it. Both name the key, and the resend under it is safe; a run without the key would deliver
127
+ // the message twice.
128
+ const key =
129
+ error instanceof EnvoySendUnconfirmedError ? error.idempotencyKey : send?.idempotencyKey
130
+ if (send !== undefined && key !== undefined) {
131
+ const why =
132
+ error instanceof EnvoySendUnconfirmedError
133
+ ? messageFor(error)
134
+ : `this resend under idempotency_key ${key} failed (${messageFor(error)}), and the send under that key before it may have arrived`
135
+ process.stderr.write(`Claude Envoy send not confirmed: ${why}\n`)
136
+ const safe = error instanceof EnvoySendUnconfirmedError ? error.resendSafe : true
137
+ if (safe && resend !== undefined) printResend({ ...resend, idempotencyKey: key })
138
+ process.exitCode = EXIT_UNCONFIRMED
139
+ } else {
140
+ process.stderr.write(`Claude Envoy send failed: ${messageFor(error)}\n`)
141
+ process.exitCode = 1
142
+ }
36
143
  }
@@ -190,36 +190,6 @@ The complete list of contributors can be found at:
190
190
 
191
191
  ================================================================================
192
192
 
193
- jose@6.2.9
194
- License: MIT
195
- Source: panva/jose
196
-
197
- --- LICENSE.md ---
198
-
199
- The MIT License (MIT)
200
-
201
- Copyright (c) 2018 Filip Skokan
202
-
203
- Permission is hereby granted, free of charge, to any person obtaining a copy
204
- of this software and associated documentation files (the "Software"), to deal
205
- in the Software without restriction, including without limitation the rights
206
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
207
- copies of the Software, and to permit persons to whom the Software is
208
- furnished to do so, subject to the following conditions:
209
-
210
- The above copyright notice and this permission notice shall be included in all
211
- copies or substantial portions of the Software.
212
-
213
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
214
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
215
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
216
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
217
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
219
- SOFTWARE.
220
-
221
- ================================================================================
222
-
223
193
  json-schema-traverse@1.0.0
224
194
  License: MIT
225
195
  Source: git+https://github.com/epoberezkin/json-schema-traverse.git