@sjawhar/claude-envoy 5.11.2 → 5.12.1

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": "5.11.2"
5
+ "version": "5.12.1"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-envoy",
3
- "version": "5.11.2",
3
+ "version": "5.12.1",
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
@@ -203,9 +203,12 @@ message from Dispatch's Agents page as that person's own turn.
203
203
  different message under the same key, and where its `ENVOY_URL` is not a plain http(s) URL (one
204
204
  with user info of any length, say) the URL is never printed; either way only the key is printed,
205
205
  with how to resend the original. A resend under a key exits 75 on any failure, since the send
206
- before it may have arrived. Any other refusal, one for the sender's rate among them, prints the
207
- listener's own sentence after `Claude Envoy send failed: `, exits 1 and is not sent again; so
208
- does a listener that refused the connection or whose name did not resolve.
206
+ before it may have arrived. A send whose idempotency key another sender's message already holds
207
+ stored nothing: it prints `Claude Envoy send not delivered: …`, naming that message's event id,
208
+ and exits 1; run again without the key, it goes under a new one. Any other refusal, one for the
209
+ sender's rate among them, prints the listener's own sentence after `Claude Envoy send failed: `,
210
+ exits 1 and is not sent again; so does a listener that refused the connection or whose name did
211
+ not resolve.
209
212
 
210
213
  ## What a delivered line carries
211
214
 
package/bin/envoy-send.ts CHANGED
@@ -3,6 +3,7 @@ import { holdUrlUserinfo, scrub, scrubProcessStreams } from "@legion/contracts/h
3
3
  import { URL_NOT_PRINTED } from "@legion/contracts/log-text"
4
4
  import { messageFor } from "@legion/envoy-client/errors"
5
5
  import { sessionConfigFile, sessionEnvoySettings } from "@legion/envoy-client/session-settings"
6
+ import { keyTakenText } from "@legion/envoy-client/tool-contract"
6
7
  import { EnvoySendUnconfirmedError } from "@legion/envoy-client/transport"
7
8
  import { configuredSessionId } from "../src/claude-session"
8
9
  import { sendEnvoyMessage } from "../src/envoy-client"
@@ -116,10 +117,18 @@ try {
116
117
  ...(send.idempotencyKey === undefined ? {} : { idempotencyKey: send.idempotencyKey }),
117
118
  sourceSession: session ?? "",
118
119
  })
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))
120
+ if (sent.keyTaken !== undefined) {
121
+ // Another sender's message holds the key, so the listener stored nothing for this send: it
122
+ // was not delivered, and it is no send that may have arrived. A run without the key mints a
123
+ // new one, which delivers it.
124
+ process.stderr.write(`Claude Envoy send ${keyTakenText(sent, "envoy_send")}\n`)
125
+ process.exitCode = 1
126
+ } else {
127
+ process.stdout.write(`Sent Envoy message to ${send.targetSession}.\n`)
128
+ // Stored, so it exits 0; but a send that went without its credential says why, once.
129
+ if (sent.withoutCredential !== undefined)
130
+ process.stderr.write(wentWithout(sent.withoutCredential))
131
+ }
123
132
  } catch (error) {
124
133
  // A send that may have arrived is not a failure, and nor is any failure of a resend that names
125
134
  // a key: the send under that key before it may have arrived, so a refusal now says nothing of
@@ -32146,6 +32146,9 @@ function metadataIdentity(audience, env = process.env, options = {}) {
32146
32146
  // ../cipher-client/src/person-identity.ts
32147
32147
  var ISSUER_TIMEOUT_SECONDS = 30;
32148
32148
  var INVALID_GRANT2 = "invalid_grant";
32149
+ function grantRefused(error) {
32150
+ return error instanceof ResponseBodyError && error.error === INVALID_GRANT2 && (error.status === 400 || error.status === 401);
32151
+ }
32149
32152
  function issuerSaid(error) {
32150
32153
  if (error instanceof ResponseBodyError) {
32151
32154
  const shown = shownOAuthCode(error.error);
@@ -32205,7 +32208,7 @@ function personIdentity(record, flow, options = {}) {
32205
32208
  config[customFetch2] = through(signal);
32206
32209
  answered = await refreshTokenGrant(config, refreshToken);
32207
32210
  } catch (error) {
32208
- if (error instanceof ResponseBodyError && error.error === INVALID_GRANT2) {
32211
+ if (grantRefused(error)) {
32209
32212
  throw new SignInEndedError(issuer, person);
32210
32213
  }
32211
32214
  throw new Error(`renew ${person}'s sign-in at ${issuer}: ${issuerSaid(error)}`);
package/dist/dispatch.js CHANGED
@@ -24955,6 +24955,9 @@ function metadataIdentity(audience, env = process.env, options = {}) {
24955
24955
  // ../cipher-client/src/person-identity.ts
24956
24956
  var ISSUER_TIMEOUT_SECONDS = 30;
24957
24957
  var INVALID_GRANT2 = "invalid_grant";
24958
+ function grantRefused(error) {
24959
+ return error instanceof ResponseBodyError && error.error === INVALID_GRANT2 && (error.status === 400 || error.status === 401);
24960
+ }
24958
24961
  function issuerSaid(error) {
24959
24962
  if (error instanceof ResponseBodyError) {
24960
24963
  const shown = shownOAuthCode(error.error);
@@ -25014,7 +25017,7 @@ function personIdentity(record, flow, options = {}) {
25014
25017
  config[customFetch2] = through(signal);
25015
25018
  answered = await refreshTokenGrant(config, refreshToken);
25016
25019
  } catch (error) {
25017
- if (error instanceof ResponseBodyError && error.error === INVALID_GRANT2) {
25020
+ if (grantRefused(error)) {
25018
25021
  throw new SignInEndedError(issuer, person);
25019
25022
  }
25020
25023
  throw new Error(`renew ${person}'s sign-in at ${issuer}: ${issuerSaid(error)}`);
@@ -34946,6 +34949,29 @@ var PublishResponseSchema = EnvelopeResponseSchema.extend({
34946
34949
  kept_reason: _enum2(["unclaimed", "holder_lapsed"]).optional(),
34947
34950
  kept_until: number2().int().optional()
34948
34951
  });
34952
+ var READS_HEADER = "Envoy-Reads";
34953
+ var READS_KEY_TAKEN = "key-taken";
34954
+ var KeyTakenWireSchema = object({
34955
+ event_id: string2().min(1),
34956
+ topic: string2(),
34957
+ dedupe_key: string2(),
34958
+ issued_at: number2().int(),
34959
+ duplicate: literal(true),
34960
+ payload_summary: literal("").optional()
34961
+ });
34962
+ function keyTakenOf(raw) {
34963
+ const taken = KeyTakenWireSchema.safeParse(raw);
34964
+ if (!taken.success)
34965
+ return;
34966
+ return {
34967
+ keyTaken: {
34968
+ eventId: taken.data.event_id,
34969
+ topic: taken.data.topic,
34970
+ issuedAt: taken.data.issued_at
34971
+ },
34972
+ delivered: false
34973
+ };
34974
+ }
34949
34975
  var DeliveryRefusalSchema = object({
34950
34976
  mode: string2(),
34951
34977
  reason: string2().optional()
@@ -35193,7 +35219,10 @@ function createEnvoyClient(config) {
35193
35219
  };
35194
35220
  const post = (path, body, options) => request(path, {
35195
35221
  method: "POST",
35196
- headers: { "Content-Type": "application/json" },
35222
+ headers: {
35223
+ "Content-Type": "application/json",
35224
+ ...options?.readsKeyTaken === true ? { [READS_HEADER]: READS_KEY_TAKEN } : {}
35225
+ },
35197
35226
  body: JSON.stringify(body)
35198
35227
  }, options);
35199
35228
  const subscribeBody = (input) => ({
@@ -35246,9 +35275,13 @@ function createEnvoyClient(config) {
35246
35275
  onNotSent: () => {
35247
35276
  notSent = true;
35248
35277
  },
35249
- ...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }
35278
+ ...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID, readsKeyTaken: true }
35250
35279
  });
35251
- response = EnvelopeResponseSchema.parse(answerJson(answer, "POST", "/v1/messages/send"));
35280
+ const raw = answerJson(answer, "POST", "/v1/messages/send");
35281
+ const taken = keyTakenOf(raw);
35282
+ if (taken !== undefined)
35283
+ return taken;
35284
+ response = EnvelopeResponseSchema.parse(raw);
35252
35285
  } catch (error) {
35253
35286
  const refused = notSent || error instanceof EnvoyApiError && error.details.status < 500;
35254
35287
  if (refused && !unanswered)
@@ -35267,7 +35300,7 @@ function createEnvoyClient(config) {
35267
35300
  },
35268
35301
  publish: async (input) => {
35269
35302
  const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
35270
- const response = PublishResponseSchema.parse(answerJson(await post("/v1/messages/publish", {
35303
+ const raw = answerJson(await post("/v1/messages/publish", {
35271
35304
  source: input.source ?? "agent",
35272
35305
  ...sourceSession(input.sourceSessionID),
35273
35306
  topic: input.topic,
@@ -35276,7 +35309,11 @@ function createEnvoyClient(config) {
35276
35309
  idempotency_key: idempotencyKey,
35277
35310
  ...messageMetadata(input),
35278
35311
  ...input.keep === undefined ? {} : { keep: input.keep }
35279
- }, input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }), "POST", "/v1/messages/publish"));
35312
+ }, input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID, readsKeyTaken: true }), "POST", "/v1/messages/publish");
35313
+ const taken = keyTakenOf(raw);
35314
+ if (taken !== undefined)
35315
+ return taken;
35316
+ const response = PublishResponseSchema.parse(raw);
35280
35317
  return {
35281
35318
  envelope: response,
35282
35319
  ...response.holder === undefined ? {} : { holder: response.holder },
@@ -44461,6 +44461,9 @@ function metadataIdentity(audience, env = process.env, options = {}) {
44461
44461
  // ../cipher-client/src/person-identity.ts
44462
44462
  var ISSUER_TIMEOUT_SECONDS = 30;
44463
44463
  var INVALID_GRANT2 = "invalid_grant";
44464
+ function grantRefused(error) {
44465
+ return error instanceof ResponseBodyError && error.error === INVALID_GRANT2 && (error.status === 400 || error.status === 401);
44466
+ }
44464
44467
  function issuerSaid(error) {
44465
44468
  if (error instanceof ResponseBodyError) {
44466
44469
  const shown = shownOAuthCode(error.error);
@@ -44520,7 +44523,7 @@ function personIdentity(record, flow, options = {}) {
44520
44523
  config[customFetch2] = through(signal);
44521
44524
  answered = await refreshTokenGrant(config, refreshToken);
44522
44525
  } catch (error) {
44523
- if (error instanceof ResponseBodyError && error.error === INVALID_GRANT2) {
44526
+ if (grantRefused(error)) {
44524
44527
  throw new SignInEndedError(issuer, person);
44525
44528
  }
44526
44529
  throw new Error(`renew ${person}'s sign-in at ${issuer}: ${issuerSaid(error)}`);
@@ -56834,6 +56837,29 @@ var PublishResponseSchema = EnvelopeResponseSchema.extend({
56834
56837
  kept_reason: _enum2(["unclaimed", "holder_lapsed"]).optional(),
56835
56838
  kept_until: number2().int().optional()
56836
56839
  });
56840
+ var READS_HEADER = "Envoy-Reads";
56841
+ var READS_KEY_TAKEN = "key-taken";
56842
+ var KeyTakenWireSchema = object({
56843
+ event_id: string2().min(1),
56844
+ topic: string2(),
56845
+ dedupe_key: string2(),
56846
+ issued_at: number2().int(),
56847
+ duplicate: literal(true),
56848
+ payload_summary: literal("").optional()
56849
+ });
56850
+ function keyTakenOf(raw) {
56851
+ const taken = KeyTakenWireSchema.safeParse(raw);
56852
+ if (!taken.success)
56853
+ return;
56854
+ return {
56855
+ keyTaken: {
56856
+ eventId: taken.data.event_id,
56857
+ topic: taken.data.topic,
56858
+ issuedAt: taken.data.issued_at
56859
+ },
56860
+ delivered: false
56861
+ };
56862
+ }
56837
56863
  var DeliveryRefusalSchema = object({
56838
56864
  mode: string2(),
56839
56865
  reason: string2().optional()
@@ -57081,7 +57107,10 @@ function createEnvoyClient(config) {
57081
57107
  };
57082
57108
  const post = (path, body, options) => request(path, {
57083
57109
  method: "POST",
57084
- headers: { "Content-Type": "application/json" },
57110
+ headers: {
57111
+ "Content-Type": "application/json",
57112
+ ...options?.readsKeyTaken === true ? { [READS_HEADER]: READS_KEY_TAKEN } : {}
57113
+ },
57085
57114
  body: JSON.stringify(body)
57086
57115
  }, options);
57087
57116
  const subscribeBody = (input) => ({
@@ -57134,9 +57163,13 @@ function createEnvoyClient(config) {
57134
57163
  onNotSent: () => {
57135
57164
  notSent = true;
57136
57165
  },
57137
- ...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }
57166
+ ...input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID, readsKeyTaken: true }
57138
57167
  });
57139
- response = EnvelopeResponseSchema.parse(answerJson(answer, "POST", "/v1/messages/send"));
57168
+ const raw = answerJson(answer, "POST", "/v1/messages/send");
57169
+ const taken = keyTakenOf(raw);
57170
+ if (taken !== undefined)
57171
+ return taken;
57172
+ response = EnvelopeResponseSchema.parse(raw);
57140
57173
  } catch (error) {
57141
57174
  const refused = notSent || error instanceof EnvoyApiError && error.details.status < 500;
57142
57175
  if (refused && !unanswered)
@@ -57155,7 +57188,7 @@ function createEnvoyClient(config) {
57155
57188
  },
57156
57189
  publish: async (input) => {
57157
57190
  const idempotencyKey = input.idempotencyKey ?? crypto.randomUUID();
57158
- const response = PublishResponseSchema.parse(answerJson(await post("/v1/messages/publish", {
57191
+ const raw = answerJson(await post("/v1/messages/publish", {
57159
57192
  source: input.source ?? "agent",
57160
57193
  ...sourceSession(input.sourceSessionID),
57161
57194
  topic: input.topic,
@@ -57164,7 +57197,11 @@ function createEnvoyClient(config) {
57164
57197
  idempotency_key: idempotencyKey,
57165
57198
  ...messageMetadata(input),
57166
57199
  ...input.keep === undefined ? {} : { keep: input.keep }
57167
- }, input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID }), "POST", "/v1/messages/publish"));
57200
+ }, input.sourceSessionID === undefined || input.sourceSessionID === "" ? {} : { actingSession: input.sourceSessionID, readsKeyTaken: true }), "POST", "/v1/messages/publish");
57201
+ const taken = keyTakenOf(raw);
57202
+ if (taken !== undefined)
57203
+ return taken;
57204
+ const response = PublishResponseSchema.parse(raw);
57168
57205
  return {
57169
57206
  envelope: response,
57170
57207
  ...response.holder === undefined ? {} : { holder: response.holder },
@@ -61908,6 +61945,21 @@ function sendConfirmationText(result) {
61908
61945
  const state = result.recipientState === "unresponsive" ? `unresponsive${since}, so it is stored until that session answers; ${UNRESPONSIVE_RULE}` : result.recipientState;
61909
61946
  return `sent ${result.envelope.event_id} to ${result.recipient} (${state})${result.confirmed ? "" : " (recipient unconfirmed by listener)"}`;
61910
61947
  }
61948
+ var KEY_TAKEN_AGAIN = {
61949
+ envoy_send: "send it again without an idempotency_key; Envoy gives it a new one",
61950
+ envoy_publish: "publish it again; Envoy gives every publish a new idempotency_key"
61951
+ };
61952
+ function keyTakenText(result, tool) {
61953
+ return `not delivered: another sender's message (${result.keyTaken.eventId}) already holds this idempotency_key, so Envoy stored nothing for you; ${KEY_TAKEN_AGAIN[tool]}`;
61954
+ }
61955
+ function keyTakenDetails(result) {
61956
+ return {
61957
+ event_id: result.keyTaken.eventId,
61958
+ topic: result.keyTaken.topic,
61959
+ delivered: false,
61960
+ key_taken: true
61961
+ };
61962
+ }
61911
61963
  function publishConfirmationText(result, options) {
61912
61964
  const published = `published ${result.envelope.event_id}`;
61913
61965
  if (result.recipients === 0) {
@@ -62147,7 +62199,7 @@ var import_nats5 = __toESM(require_mod4(), 1);
62147
62199
 
62148
62200
  // package.json
62149
62201
  var name2 = "@sjawhar/claude-envoy";
62150
- var version2 = "5.11.2";
62202
+ var version2 = "5.12.1";
62151
62203
  var legion = {
62152
62204
  daemonApiVersion: 217
62153
62205
  };
@@ -65188,6 +65240,16 @@ function createPhaseReminder(deps) {
65188
65240
  });
65189
65241
  if (reportKey !== key)
65190
65242
  return;
65243
+ if ("keyTaken" in answer) {
65244
+ reportKey = undefined;
65245
+ writeEntry();
65246
+ deps.log.warn("legion: another sender's message holds the architect notice's key; telling it again later", {
65247
+ issue: claim.issue,
65248
+ reminders,
65249
+ event_id: answer.keyTaken.eventId
65250
+ });
65251
+ return;
65252
+ }
65191
65253
  if (answer.kept === true) {
65192
65254
  if (answer.keptUntil === undefined) {
65193
65255
  reportKey = undefined;
@@ -68574,6 +68636,9 @@ async function executeEnvoyTool(runtime, name, input) {
68574
68636
  ...args.idempotency_key === undefined ? {} : { idempotencyKey: args.idempotency_key },
68575
68637
  ...toMessageMetadata(args)
68576
68638
  });
68639
+ if (result.keyTaken !== undefined) {
68640
+ return { message: keyTakenText(result, "envoy_send"), ...keyTakenDetails(result) };
68641
+ }
68577
68642
  return {
68578
68643
  message: sendConfirmationText(result),
68579
68644
  event_id: result.envelope.event_id,
@@ -68593,6 +68658,9 @@ async function executeEnvoyTool(runtime, name, input) {
68593
68658
  message: args.message,
68594
68659
  ...toMessageMetadata(args)
68595
68660
  });
68661
+ if (result.keyTaken !== undefined) {
68662
+ return { message: keyTakenText(result, "envoy_publish"), ...keyTakenDetails(result) };
68663
+ }
68596
68664
  return {
68597
68665
  message: publishConfirmationText(result, { senderNamed: true }),
68598
68666
  event_id: result.envelope.event_id,
@@ -32172,6 +32172,9 @@ function metadataIdentity(audience, env = process.env, options = {}) {
32172
32172
  // ../cipher-client/src/person-identity.ts
32173
32173
  var ISSUER_TIMEOUT_SECONDS = 30;
32174
32174
  var INVALID_GRANT2 = "invalid_grant";
32175
+ function grantRefused(error) {
32176
+ return error instanceof ResponseBodyError && error.error === INVALID_GRANT2 && (error.status === 400 || error.status === 401);
32177
+ }
32175
32178
  function issuerSaid(error) {
32176
32179
  if (error instanceof ResponseBodyError) {
32177
32180
  const shown = shownOAuthCode(error.error);
@@ -32231,7 +32234,7 @@ function personIdentity(record, flow, options = {}) {
32231
32234
  config[customFetch2] = through(signal);
32232
32235
  answered = await refreshTokenGrant(config, refreshToken);
32233
32236
  } catch (error) {
32234
- if (error instanceof ResponseBodyError && error.error === INVALID_GRANT2) {
32237
+ if (grantRefused(error)) {
32235
32238
  throw new SignInEndedError(issuer, person);
32236
32239
  }
32237
32240
  throw new Error(`renew ${person}'s sign-in at ${issuer}: ${issuerSaid(error)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/claude-envoy",
3
- "version": "5.11.2",
3
+ "version": "5.12.1",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Claude Code plugin for Legion's Envoy bus",
6
6
  "type": "module",
@@ -64,6 +64,8 @@ import {
64
64
  import {
65
65
  EnvoyToolOperation,
66
66
  envoyToolSpecs,
67
+ keyTakenDetails,
68
+ keyTakenText,
67
69
  publishConfirmationText,
68
70
  publishResultDetails,
69
71
  sendConfirmationText,
@@ -2325,6 +2327,9 @@ export async function executeEnvoyTool(
2325
2327
  ...(args.idempotency_key === undefined ? {} : { idempotencyKey: args.idempotency_key }),
2326
2328
  ...toMessageMetadata(args),
2327
2329
  })
2330
+ if (result.keyTaken !== undefined) {
2331
+ return { message: keyTakenText(result, "envoy_send"), ...keyTakenDetails(result) }
2332
+ }
2328
2333
  return {
2329
2334
  message: sendConfirmationText(result),
2330
2335
  event_id: result.envelope.event_id,
@@ -2349,6 +2354,9 @@ export async function executeEnvoyTool(
2349
2354
  // plugin uses (`publishConfirmationText`). This server always names its own session as the
2350
2355
  // source, so the listener sends that session a message for a kept copy no session took,
2351
2356
  // unless it drops the copy more than an hour late, and the line says so.
2357
+ if (result.keyTaken !== undefined) {
2358
+ return { message: keyTakenText(result, "envoy_publish"), ...keyTakenDetails(result) }
2359
+ }
2352
2360
  return {
2353
2361
  message: publishConfirmationText(result, { senderNamed: true }),
2354
2362
  event_id: result.envelope.event_id,
@@ -1,5 +1,5 @@
1
1
  import type { RequestCredential } from "@legion/envoy-client/bearer"
2
- import { createEnvoyClient } from "@legion/envoy-client/transport"
2
+ import { createEnvoyClient, type KeyTakenResult } from "@legion/envoy-client/transport"
3
3
 
4
4
  export type SendEnvoyMessageOptions = {
5
5
  readonly baseUrl: string
@@ -27,6 +27,8 @@ export type SentEnvoyMessage = {
27
27
  * went without included, and where the send was handed none to carry.
28
28
  */
29
29
  readonly withoutCredential: unknown
30
+ /** Never set on a stored send: see {@link KeyTakenResult}. */
31
+ readonly keyTaken?: undefined
30
32
  }
31
33
 
32
34
  /**
@@ -86,11 +88,13 @@ function recorded(credential: RequestCredential): {
86
88
  *
87
89
  * A stored send answers what it went without (`SentEnvoyMessage`), so the command can say so: a
88
90
  * sign-in revoked for good is stored without the credential by a listener that asks for none, and
89
- * nothing else would tell the person.
91
+ * nothing else would tell the person. A send whose idempotency key another sender's message
92
+ * already holds stored nothing, and answers that (`KeyTakenResult`), naming that message's event
93
+ * id: the message was not delivered, and a send under a new key delivers it.
90
94
  */
91
95
  export async function sendEnvoyMessage(
92
96
  options: SendEnvoyMessageOptions,
93
- ): Promise<SentEnvoyMessage> {
97
+ ): Promise<SentEnvoyMessage | KeyTakenResult> {
94
98
  const kept = options.credential === undefined ? undefined : recorded(options.credential)
95
99
  const client = createEnvoyClient({
96
100
  baseUrl: options.baseUrl,
@@ -101,11 +105,12 @@ export async function sendEnvoyMessage(
101
105
  // session's sending limit; a shell that names no session sends a blank one, which the transport
102
106
  // leaves off the wire. A refusal is an `EnvoyApiError` whose message is the listener's own
103
107
  // sentence, which `envoy-send` prints.
104
- await client.send({
108
+ const result = await client.send({
105
109
  sourceSessionID: options.sourceSession ?? "",
106
110
  targetSessionID: options.targetSession,
107
111
  message: options.message,
108
112
  ...(options.idempotencyKey === undefined ? {} : { idempotencyKey: options.idempotencyKey }),
109
113
  })
114
+ if (result.keyTaken !== undefined) return result
110
115
  return { withoutCredential: kept?.lastFailure() }
111
116
  }