@sjawhar/claude-envoy 5.18.0 → 5.19.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/claude-envoy",
3
- "version": "5.18.0",
3
+ "version": "5.19.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Claude Code plugin for Legion's Envoy bus",
6
6
  "type": "module",
@@ -3,7 +3,7 @@ import type { DispatchClient } from "@legion/envoy-client/dispatch-http"
3
3
  import { messageFor } from "@legion/envoy-client/errors"
4
4
  import { acceptUserTurn } from "@legion/pi-shared/accept-user-turn"
5
5
  import { typedInputReply } from "@legion/pi-shared/dispatch-user-turn"
6
- import type { UserTurnRouter } from "../user-turn-router"
6
+ import { acceptLanded, answerBtw, replyWarning, type UserTurnRouter } from "../user-turn-router"
7
7
  import type { UserTurnState } from "../user-turn-state"
8
8
  import type { CodexInboxItem } from "./codex-inbox"
9
9
 
@@ -20,7 +20,10 @@ import type { CodexInboxItem } from "./codex-inbox"
20
20
  * - A `/` message is answered that it was not run: Codex runs no command it did not read from its
21
21
  * own terminal.
22
22
  *
23
- * Everything else keeps the card it renders as.
23
+ * Everything else keeps the card it renders as. A write Dispatch made and gave no copy of its
24
+ * answer for (`DispatchWriteLandedError`: an accept, a reply, a BTW answer) is said as made, in
25
+ * the words `../user-turn-router` says it in (`acceptLanded`, `replyWarning`, `btwAnswerWarning`
26
+ * through `answerBtw`).
24
27
  */
25
28
  export interface CodexUserTurnOptions {
26
29
  readonly sessionId: () => string
@@ -48,16 +51,10 @@ export const CODEX_COMMAND_REFUSAL =
48
51
  export function createCodexUserTurnRouter(options: CodexUserTurnOptions): UserTurnRouter {
49
52
  const { state, warn } = options
50
53
 
54
+ /** A BTW answer Dispatch made and gave no copy of its answer for answered the attempt, so no
55
+ * error reply follows it, and the line says it landed (`answerBtw`). */
51
56
  const runBtw = (delivery: DispatchDelivery): void => {
52
- void (async () => {
53
- try {
54
- await options.reply(delivery, { body: await options.ask(delivery.body) })
55
- } catch (error) {
56
- await options.reply(delivery, { error: messageFor(error) })
57
- }
58
- })().catch((error: unknown) => {
59
- warn(`a BTW answer did not reach Dispatch — ${messageFor(error)}`)
60
- })
57
+ answerBtw(delivery, options.ask, options.reply, warn)
61
58
  }
62
59
 
63
60
  return {
@@ -74,6 +71,10 @@ export function createCodexUserTurnRouter(options: CodexUserTurnOptions): UserTu
74
71
  handled: state.handled,
75
72
  rendered,
76
73
  warn: (refusal) => {
74
+ if (refusal.reason === "landed") {
75
+ warn(acceptLanded(refusal.delivery.id, refusal.error))
76
+ return
77
+ }
77
78
  warn(
78
79
  `Dispatch message ${refusal.delivery.id} arrives as a card (${refusal.reason === "failed" ? messageFor(refusal.error) : refusal.reason})`,
79
80
  )
@@ -85,7 +86,7 @@ export function createCodexUserTurnRouter(options: CodexUserTurnOptions): UserTu
85
86
  const said = typedInputReply(turn.body, { error: CODEX_COMMAND_REFUSAL })
86
87
  if (said !== undefined) {
87
88
  void options.reply(delivery, { body: said }).catch((error: unknown) => {
88
- warn(`a reply to message ${delivery.id} did not reach Dispatch — ${messageFor(error)}`)
89
+ warn(replyWarning(delivery.id, error))
89
90
  })
90
91
  }
91
92
  return true
@@ -36,7 +36,7 @@ import {
36
36
  failureNoticeFields,
37
37
  } from "@legion/envoy-client/delivery-ledger"
38
38
  import { activeDispatchSettings } from "@legion/envoy-client/dispatch-config"
39
- import { DispatchClient } from "@legion/envoy-client/dispatch-http"
39
+ import { DispatchClient, DispatchWriteLandedError } from "@legion/envoy-client/dispatch-http"
40
40
  import { sessionDirectory as dispatchSessionDirectory } from "@legion/envoy-client/dispatch-session-directory"
41
41
  import { writeSessionTitle } from "@legion/envoy-client/dispatch-session-state"
42
42
  import {
@@ -606,7 +606,9 @@ export function createChannelDelivery(input: {
606
606
  if (rendered.skip) return tail.then(() => false)
607
607
  // A targeted Dispatch delivery the shared renderer could not validate is
608
608
  // never shown to the model: it is answered on Dispatch when it names a
609
- // message, and dropped with a log line when it does not.
609
+ // message, and dropped with a log line when it does not. A report Dispatch
610
+ // made and gave no copy of its answer for (`DispatchWriteLandedError`)
611
+ // answered the attempt, and its line says it landed.
610
612
  if (rendered.rejectedDelivery !== undefined) {
611
613
  const rejected = rendered.rejectedDelivery
612
614
  process.stderr.write(
@@ -618,7 +620,9 @@ export function createChannelDelivery(input: {
618
620
  () => false,
619
621
  (error: unknown) => {
620
622
  process.stderr.write(
621
- `envoy-channel: could not report the rejected delivery to Dispatch — ${messageFor(error)}\n`,
623
+ error instanceof DispatchWriteLandedError
624
+ ? `envoy-channel: the report of the rejected delivery landed on Dispatch, which gave no copy of its answer — ${error.message}\n`
625
+ : `envoy-channel: could not report the rejected delivery to Dispatch — ${messageFor(error)}\n`,
622
626
  )
623
627
  return false
624
628
  },
@@ -18,7 +18,12 @@
18
18
  // the body Dispatch answers with, never the frame's own text.
19
19
  import { comparableLines } from "@legion/contracts"
20
20
  import type { DispatchDelivery, RenderInboundResult } from "@legion/envoy-client/delivery"
21
- import type { DispatchClient } from "@legion/envoy-client/dispatch-http"
21
+ import {
22
+ type DispatchClient,
23
+ DispatchServiceError,
24
+ DispatchWriteLandedError,
25
+ DispatchWriteUnsettledError,
26
+ } from "@legion/envoy-client/dispatch-http"
22
27
  import { pictureAddresses } from "@legion/envoy-client/dispatch-pictures"
23
28
  import { messageFor } from "@legion/envoy-client/errors"
24
29
  import { type AcceptedUserTurnDelivery, acceptUserTurn } from "@legion/pi-shared/accept-user-turn"
@@ -91,6 +96,89 @@ export function thinkingPicked(effort: ModEffort): string {
91
96
  return `The session's thinking level is now ${effort}.`
92
97
  }
93
98
 
99
+ /** What stderr says of an accept of message `messageId` Dispatch recorded and gave no copy of its
100
+ * answer for (`DispatchWriteLandedError`): the attempt is spent as this session's, and the
101
+ * stored body a turn is made of is not in hand. Every host of this package says it in these
102
+ * words (`./codex/codex-user-turns`). */
103
+ export function acceptLanded(messageId: string, error: DispatchWriteLandedError): string {
104
+ return `Dispatch recorded this session's accept of message ${messageId} and gave no copy of its answer (${error.message}), so no turn follows; it arrives as a card`
105
+ }
106
+
107
+ /** Whether a post that ended in `error` may or may not have been posted: it ended with no
108
+ * definitive answer while an attempt of it may have reached Dispatch
109
+ * (`DispatchWriteUnsettledError`: an earlier attempt got a 5xx or failed in transport, or the last
110
+ * was cut off for silence or by the deadline), or on a 5xx (a `DispatchServiceError` of 500 or
111
+ * more, a gateway's included), which can follow a write Dispatch committed. Dispatch's own 4xx,
112
+ * and an error raised before anything was sent, say it was not posted. */
113
+ function mayHaveBeenPosted(error: unknown): boolean {
114
+ return (
115
+ error instanceof DispatchWriteUnsettledError ||
116
+ (error instanceof DispatchServiceError && error.status >= 500)
117
+ )
118
+ }
119
+
120
+ /** What stderr says of a reply to message `messageId` whose post ended in `error`. One Dispatch
121
+ * made and gave no copy of its answer for (`DispatchWriteLandedError`) is posted, and the
122
+ * error's message says what was made; one that may have landed (`mayHaveBeenPosted`: a
123
+ * `DispatchWriteUnsettledError`, or a 5xx) may or may not have been posted, and the line says
124
+ * so; any other was not posted, whether Dispatch refused it (a `DispatchServiceError` under 500,
125
+ * such as a 409) or it never reached Dispatch, and the error's message says which. Every host of
126
+ * this package says it in these words (`./codex/codex-user-turns`). */
127
+ export function replyWarning(messageId: string, error: unknown): string {
128
+ if (error instanceof DispatchWriteLandedError) {
129
+ return `a reply to message ${messageId} landed on Dispatch, which gave no copy of its answer — ${error.message}`
130
+ }
131
+ if (mayHaveBeenPosted(error)) {
132
+ return `a reply to message ${messageId} may or may not have been posted — ${messageFor(error)}`
133
+ }
134
+ return `a reply to message ${messageId} was not posted — ${messageFor(error)}`
135
+ }
136
+
137
+ /** What stderr says of a BTW answer to message `messageId` whose post ended in `error`. One
138
+ * Dispatch made and gave no copy of its answer for (`DispatchWriteLandedError`) answered the
139
+ * attempt, so no error reply follows it; one that may have landed (`mayHaveBeenPosted`: a
140
+ * `DispatchWriteUnsettledError`, or a 5xx) may or may not have been posted, and the line says
141
+ * so; any other was not posted, whether Dispatch refused it or it never reached Dispatch, and
142
+ * the error's message says which. */
143
+ export function btwAnswerWarning(messageId: string, error: unknown): string {
144
+ if (error instanceof DispatchWriteLandedError) {
145
+ return `a BTW answer to message ${messageId} landed on Dispatch, which gave no copy of its answer — ${error.message}`
146
+ }
147
+ if (mayHaveBeenPosted(error)) {
148
+ return `a BTW answer to message ${messageId} may or may not have been posted — ${messageFor(error)}`
149
+ }
150
+ return `a BTW answer to message ${messageId} was not posted — ${messageFor(error)}`
151
+ }
152
+
153
+ /** Answers a BTW on Dispatch with what `ask` makes of its body, and returns without waiting for
154
+ * it, so the frame is answered at once. A question `ask` could not answer, and an answer whose
155
+ * post Dispatch refused or that never reached it, is answered with the error that stopped it,
156
+ * and that error reply is all that is said of it. A reply Dispatch made and gave no copy of its
157
+ * answer for (`DispatchWriteLandedError`) answered the attempt, so no error reply follows it.
158
+ * Stderr gets a line (`btwAnswerWarning`) only where the last post made for the BTW ended in an
159
+ * error: that landed reply, or an error reply that failed too. Every host of this package
160
+ * answers a BTW through it (`./codex/codex-user-turns`). */
161
+ export function answerBtw(
162
+ delivery: DispatchDelivery,
163
+ ask: (question: string) => Promise<string>,
164
+ reply: (
165
+ delivery: DispatchDelivery,
166
+ result: { readonly body?: string; readonly error?: string },
167
+ ) => Promise<void>,
168
+ warn: (text: string) => void,
169
+ ): void {
170
+ void (async () => {
171
+ try {
172
+ await reply(delivery, { body: await ask(delivery.body) })
173
+ } catch (error) {
174
+ if (error instanceof DispatchWriteLandedError) throw error
175
+ await reply(delivery, { error: messageFor(error) })
176
+ }
177
+ })().catch((error: unknown) => {
178
+ warn(btwAnswerWarning(delivery.id, error))
179
+ })
180
+ }
181
+
94
182
  /** A message whose row of the conversation has not arrived yet: the person's own words, which
95
183
  * is what the row will say, and the Dispatch message that row is. A `/` message has no such
96
184
  * row - the only row it writes is the one Claude Code echoes the command it ran in, which the
@@ -250,6 +338,10 @@ export function createUserTurnRouter(options: UserTurnRouterOptions): UserTurnRo
250
338
  )
251
339
  return
252
340
  }
341
+ if (refusal.reason === "landed") {
342
+ warn(acceptLanded(delivery.id, refusal.error))
343
+ return
344
+ }
253
345
  warn(
254
346
  `Dispatch did not accept message ${delivery.id} as this session's turn (${messageFor(refusal.error)}); it arrives as a card`,
255
347
  )
@@ -327,11 +419,16 @@ export function createUserTurnRouter(options: UserTurnRouterOptions): UserTurnRo
327
419
  await reply(delivery, { ...result, followUp })
328
420
  }
329
421
 
330
- /** Posts one reply to a delivery, naming on stderr a reply that did not reach Dispatch. */
422
+ /** Posts one reply to a delivery, naming on stderr a reply that was not posted, which Dispatch
423
+ * refused or never received; one that may or may not have been posted, which ended with no
424
+ * definitive answer after an attempt may have reached Dispatch (`DispatchWriteUnsettledError`)
425
+ * or on a 5xx; and one Dispatch made and gave no copy of its answer for
426
+ * (`DispatchWriteLandedError`), which is posted: the error's message says what was made
427
+ * (`replyWarning`). */
331
428
  const answer = async (delivery: DispatchDelivery, body: string | undefined): Promise<void> => {
332
429
  if (body === undefined) return
333
430
  await post(delivery, { body }).catch((error: unknown) => {
334
- warn(`a reply to message ${delivery.id} did not reach Dispatch — ${messageFor(error)}`)
431
+ warn(replyWarning(delivery.id, error))
335
432
  })
336
433
  }
337
434
 
@@ -420,21 +517,19 @@ export function createUserTurnRouter(options: UserTurnRouterOptions): UserTurnRo
420
517
  * answer never asks for a follow-up: a BTW is answered one time, and the rule that a frame
421
518
  * this session is handed twice posts no second answer is what Dispatch leans on for it
422
519
  * (`replyAfterAnswer`, `packages/envoy/internal/dispatch/api/message_delivery.go`), since
423
- * nothing here guards a redelivered BTW and the fork would answer it afresh. */
520
+ * nothing here guards a redelivered BTW and the fork would answer it afresh. A session with
521
+ * no link to Claude Code cannot ask the model, which is the error its BTW is answered with
522
+ * (`answerBtw`). */
424
523
  const runBtw = (delivery: DispatchDelivery): void => {
425
- void (async () => {
426
- if (!session.connected()) {
427
- await reply(delivery, { error: "this session has no link to Claude Code" })
428
- return
429
- }
430
- try {
431
- await reply(delivery, { body: await session.fork(delivery.body) })
432
- } catch (error) {
433
- await reply(delivery, { error: messageFor(error) })
434
- }
435
- })().catch((error: unknown) => {
436
- warn(`a BTW answer did not reach Dispatch — ${messageFor(error)}`)
437
- })
524
+ answerBtw(
525
+ delivery,
526
+ async (question) => {
527
+ if (!session.connected()) throw new Error("this session has no link to Claude Code")
528
+ return session.fork(question)
529
+ },
530
+ reply,
531
+ warn,
532
+ )
438
533
  }
439
534
 
440
535
  return {