@sjawhar/claude-envoy 6.14.0 → 6.14.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.
- package/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/README.md +42 -2
- package/dist/codex-hook.js +3 -0
- package/dist/dispatch.js +108 -57
- package/dist/envoy-channel.js +558 -219
- package/dist/session-hook.js +3 -0
- package/package.json +1 -1
- package/src/agent-stream-runner.ts +10 -7
- package/src/channel-connection.ts +21 -9
- package/src/codex/codex-server.ts +23 -2
- package/src/envoy-channel-server.ts +296 -113
- package/src/envoy-off.ts +16 -1
- package/src/inbound-outage-lines.ts +65 -0
- package/src/legion-claim.ts +13 -3
- package/src/stop-gates.ts +17 -0
package/dist/session-hook.js
CHANGED
|
@@ -22774,6 +22774,7 @@ var SESSION_LIVE_WINDOW_MS = 300000;
|
|
|
22774
22774
|
var LISTENER_CALL_TIMEOUT_MS = 5000;
|
|
22775
22775
|
var LISTENER_CALL_RETRY_DELAY_MS = 250;
|
|
22776
22776
|
var LISTENER_CALL_ATTEMPTS = 2;
|
|
22777
|
+
var BUS_UNANSWERED_PREFIX = "envoy: the message bus did not answer";
|
|
22777
22778
|
// ../contracts/src/related-forms.ts
|
|
22778
22779
|
function relatedCheckFormOf(suggestions) {
|
|
22779
22780
|
const { outcome, missing } = suggestions;
|
|
@@ -38083,6 +38084,8 @@ function keepNatsCredential(connection, credential, warn) {
|
|
|
38083
38084
|
if (credential.needsReconnect())
|
|
38084
38085
|
await connection.reconnect();
|
|
38085
38086
|
} catch (error) {
|
|
38087
|
+
if (stopped)
|
|
38088
|
+
return;
|
|
38086
38089
|
if (error instanceof SessionMovedError) {
|
|
38087
38090
|
schedule(0);
|
|
38088
38091
|
return;
|
package/package.json
CHANGED
|
@@ -23,6 +23,10 @@ import type { ModLink } from "./mod-link"
|
|
|
23
23
|
* wait this long means the mod is gone rather than busy, and the header keeps its last values. */
|
|
24
24
|
const AGENT_STREAM_CALL_TIMEOUT_MS = 5_000
|
|
25
25
|
|
|
26
|
+
/** How the live view's warning for a read of the mod that failed begins. The thing it read and
|
|
27
|
+
* the failure's message follow it (`refreshSettings`, `refreshCommands`). */
|
|
28
|
+
export const LIVE_VIEW_READ_FAILED = "the live view could not read this session's "
|
|
29
|
+
|
|
26
30
|
/**
|
|
27
31
|
* The most row ids this stream remembers having read, so a row that arrives twice is read once.
|
|
28
32
|
*
|
|
@@ -69,8 +73,9 @@ export interface AgentStreamOptions {
|
|
|
69
73
|
* rather than a person (`./agent-stream-claude`, `PANE_SEEDED_COMMAND`). */
|
|
70
74
|
readonly isPane?: boolean
|
|
71
75
|
readonly now?: () => number
|
|
72
|
-
/** Where a failure that must not stop the session is written
|
|
73
|
-
|
|
76
|
+
/** Where a failure that must not stop the session is written: the server's stderr, through its
|
|
77
|
+
* tell gate (`./stop-gates`), so a read the link's close at the stop ends says nothing. */
|
|
78
|
+
readonly warn: (text: string) => void
|
|
74
79
|
/**
|
|
75
80
|
* Takes every event this stream reads, before the stream records it. The link's events have one
|
|
76
81
|
* consumer and this is it, so whatever else needs them - the router of a person's messages, and
|
|
@@ -144,10 +149,8 @@ export interface AgentStreamRunner {
|
|
|
144
149
|
* publishes no settings and no commands, and its conversation stays empty.
|
|
145
150
|
*/
|
|
146
151
|
export function startAgentStream(options: AgentStreamOptions): AgentStreamRunner {
|
|
147
|
-
const { link } = options
|
|
152
|
+
const { link, warn } = options
|
|
148
153
|
const now = options.now ?? Date.now
|
|
149
|
-
const warn =
|
|
150
|
-
options.warn ?? ((text: string): void => void process.stderr.write(`envoy-channel: ${text}\n`))
|
|
151
154
|
/** The id both subjects name now; it moves with the mod's own id after a `/clear`. */
|
|
152
155
|
let session = options.sessionId
|
|
153
156
|
/** The viewer's control subject, on the session's connection now (`./control-subscription`). */
|
|
@@ -225,7 +228,7 @@ export function startAgentStream(options: AgentStreamOptions): AgentStreamRunner
|
|
|
225
228
|
if (!control.stopped) publishSettings()
|
|
226
229
|
})
|
|
227
230
|
.catch((error: unknown) => {
|
|
228
|
-
warn(
|
|
231
|
+
warn(`${LIVE_VIEW_READ_FAILED}model — ${messageFor(error)}`)
|
|
229
232
|
})
|
|
230
233
|
.finally(() => {
|
|
231
234
|
readingSettings = false
|
|
@@ -242,7 +245,7 @@ export function startAgentStream(options: AgentStreamOptions): AgentStreamRunner
|
|
|
242
245
|
if (!control.stopped) publishCommands()
|
|
243
246
|
})
|
|
244
247
|
.catch((error: unknown) => {
|
|
245
|
-
warn(
|
|
248
|
+
warn(`${LIVE_VIEW_READ_FAILED}commands — ${messageFor(error)}`)
|
|
246
249
|
})
|
|
247
250
|
.finally(() => {
|
|
248
251
|
readingCommands = false
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
// servers take theirs from here: Claude Code's (`./envoy-channel-server`) and Codex's
|
|
8
8
|
// (`./codex/codex-server`).
|
|
9
9
|
import type { RequestCredential } from "@legion/envoy-client/bearer"
|
|
10
|
-
import { inboundUnavailable } from "@legion/envoy-client/consumer-delivery"
|
|
11
10
|
import { LoggableError } from "@legion/envoy-client/errors"
|
|
11
|
+
import { inboundUnavailable } from "@legion/envoy-client/inbound-outage"
|
|
12
12
|
import { connectListenerRelay, type RelayOutage } from "@legion/envoy-client/listener-relay"
|
|
13
13
|
import {
|
|
14
14
|
connectNats,
|
|
@@ -274,6 +274,9 @@ export interface ChannelInbound {
|
|
|
274
274
|
* why an open failed; a token the broker refused is told on `warn` until the session holds the
|
|
275
275
|
* socket and tells its own outage (`ListenerRelay.reportOutagesTo`), which the session's first
|
|
276
276
|
* accepted registration ends. `signal` aborting (the process is stopping) ends either wait at once.
|
|
277
|
+
* `stopping` is the session's stop reading (R24 of dispatch://INFRA-3589): once it answers true,
|
|
278
|
+
* the relay socket opens no socket again and closes for good instead (`ListenerRelayOptions`).
|
|
279
|
+
* Unset, the session never reads as stopping.
|
|
277
280
|
*/
|
|
278
281
|
export async function dialChannelInbound(
|
|
279
282
|
inbound: ChannelInboundSettings,
|
|
@@ -283,6 +286,7 @@ export async function dialChannelInbound(
|
|
|
283
286
|
warn: (text: string, dialError?: unknown) => void,
|
|
284
287
|
dial: (options: ConnectionOptions) => Promise<NatsConnection> = connect,
|
|
285
288
|
signal?: AbortSignal,
|
|
289
|
+
stopping?: () => boolean,
|
|
286
290
|
): Promise<ChannelInbound> {
|
|
287
291
|
if ("natsUrls" in inbound) {
|
|
288
292
|
return { ...(await dialChannelNats(inbound, name, warn, dial, signal)), liveView: true }
|
|
@@ -297,14 +301,15 @@ export async function dialChannelInbound(
|
|
|
297
301
|
// (`inboundUnavailable`), however many trades fail. The socket tries the trade again on its
|
|
298
302
|
// own, 1 s doubling to a minute; once one succeeds the socket opens and the session starts,
|
|
299
303
|
// and nothing tells the session of the refusals before.
|
|
300
|
-
outage: inboundUnavailable(
|
|
301
|
-
(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
),
|
|
305
|
-
() => undefined,
|
|
304
|
+
outage: inboundUnavailable(({ why }) =>
|
|
305
|
+
warn(
|
|
306
|
+
`this session's relay socket has no token: ${why}; the session starts once a trade succeeds`,
|
|
307
|
+
),
|
|
306
308
|
),
|
|
307
309
|
log: (line) => warn(line),
|
|
310
|
+
// A socket that closes once the stop has begun is not opened again: the stop closes the
|
|
311
|
+
// session's relay and drains its connection itself, and deregisters the session after that.
|
|
312
|
+
...(stopping === undefined ? {} : { stopping }),
|
|
308
313
|
},
|
|
309
314
|
signal,
|
|
310
315
|
)
|
|
@@ -358,6 +363,12 @@ export interface ChannelReopenerOptions {
|
|
|
358
363
|
/** Aborts when the server stops: it ends a dial's waits and its redials, and closes a connection
|
|
359
364
|
* a dial opened after it. */
|
|
360
365
|
readonly signal: AbortSignal
|
|
366
|
+
/** The session's stop reading (`dialChannelInbound`): a relay socket opens no socket again
|
|
367
|
+
* once it answers true. */
|
|
368
|
+
readonly stopping: () => boolean
|
|
369
|
+
/** Writes one warning of a dial on the server's stderr, through the server's tell gate
|
|
370
|
+
* (`./stop-gates`): a relay socket's lines come here for its whole life. */
|
|
371
|
+
readonly warn: (text: string) => void
|
|
361
372
|
/** Stops the token keeper the server holds now and holds `stop` in its place: the one the
|
|
362
373
|
* server's teardown stops. */
|
|
363
374
|
readonly swapKeeper: (stop: () => void) => void
|
|
@@ -369,7 +380,7 @@ export interface ChannelReopenerOptions {
|
|
|
369
380
|
|
|
370
381
|
/**
|
|
371
382
|
* A server's `ChannelSessionOptions.reopen`: a new connection once the session's closed for good,
|
|
372
|
-
* opened as the server's first one was (`dialChannelInbound`), warning of each redial on
|
|
383
|
+
* opened as the server's first one was (`dialChannelInbound`), warning of each redial on `warn`,
|
|
373
384
|
* until one opens or the server stops. A relay socket opens again on its own after every close, so
|
|
374
385
|
* it closes for good only as the server closes it; a NATS connection is what this replaces in a
|
|
375
386
|
* running session. The closed connection's token keeper stops before the dial, and the new one's is
|
|
@@ -393,11 +404,12 @@ export function channelReopener(options: ChannelReopenerOptions): ReopenConnecti
|
|
|
393
404
|
options.session,
|
|
394
405
|
options.name,
|
|
395
406
|
(text, dialError) => {
|
|
396
|
-
|
|
407
|
+
options.warn(text)
|
|
397
408
|
if (dialError !== undefined) failed(dialError)
|
|
398
409
|
},
|
|
399
410
|
connect,
|
|
400
411
|
options.signal,
|
|
412
|
+
options.stopping,
|
|
401
413
|
)
|
|
402
414
|
if (options.signal.aborted) {
|
|
403
415
|
next.stop()
|
|
@@ -44,6 +44,7 @@ import { inboundOffChannelSession } from "../envoy-off"
|
|
|
44
44
|
import { channelConnectionName, installedPluginVersion } from "../installed-build"
|
|
45
45
|
import { SessionIdentity } from "../session-identity"
|
|
46
46
|
import { sessionTitle } from "../session-title"
|
|
47
|
+
import { gatedStderr } from "../stop-gates"
|
|
47
48
|
import { type CodexAgentStream, startCodexAgentStream } from "./codex-agent-stream"
|
|
48
49
|
import { askSideQuestion, type CodexCommand, codexProcessEnv, queueMessage } from "./codex-cli"
|
|
49
50
|
import { createCodexDelivery } from "./codex-delivery"
|
|
@@ -263,13 +264,17 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
263
264
|
const endedNow = (sessionId: string): Promise<boolean> =>
|
|
264
265
|
sessionEnded(codexEndedFile(stateDirectory, codexPid, sessionId))
|
|
265
266
|
const configFile = sessionConfigFile("codex", process.env)
|
|
266
|
-
const warn = (text: string): void => void process.stderr.write(`envoy-channel: ${text}\n`)
|
|
267
267
|
const bundlePath = resolve(pluginRoot, process.argv[1] ?? "")
|
|
268
268
|
|
|
269
269
|
let record: CodexSessionRecord | undefined
|
|
270
270
|
let runtime: ChannelToolRuntime | undefined
|
|
271
271
|
let notReady = "this session's Envoy side starts at its first prompt"
|
|
272
272
|
const stopping = new AbortController()
|
|
273
|
+
// The server's tell gate over its stop (`stop`, R24 of dispatch://INFRA-3589): once the stop
|
|
274
|
+
// has begun, a warning of its own is written nowhere. The session reads the same stop from
|
|
275
|
+
// `startSignal`, and the client's stop gate from `stopping.signal` too.
|
|
276
|
+
const serverStderr = gatedStderr(() => stopping.signal.aborted)
|
|
277
|
+
const warn = (text: string): void => serverStderr(`envoy-channel: ${text}\n`)
|
|
273
278
|
|
|
274
279
|
const server = new Server(MCP_SERVER_INFO, {
|
|
275
280
|
capabilities: { tools: {} },
|
|
@@ -388,6 +393,10 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
388
393
|
(entry) => {
|
|
389
394
|
self = entry
|
|
390
395
|
},
|
|
396
|
+
undefined,
|
|
397
|
+
undefined,
|
|
398
|
+
// The tell gate: a look that fails once the stop has begun writes no warning.
|
|
399
|
+
warn,
|
|
391
400
|
)
|
|
392
401
|
if (owning === undefined) {
|
|
393
402
|
await stopped.promise
|
|
@@ -486,6 +495,9 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
486
495
|
baseUrl: envoy.url,
|
|
487
496
|
credential: envoy.credential,
|
|
488
497
|
fetch: globalThis.fetch,
|
|
498
|
+
// The stop gate: once `stop` has begun, no call of the session's registration or role
|
|
499
|
+
// state is sent, the DELETE of the stop apart.
|
|
500
|
+
stopping: () => stopping.signal.aborted,
|
|
489
501
|
})
|
|
490
502
|
const clientInstanceID = newClientInstanceID()
|
|
491
503
|
/** The session the `codex` process runs now, as its record names it: what `codex queue` and the
|
|
@@ -534,7 +546,12 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
534
546
|
try {
|
|
535
547
|
const { inbound } = envoy
|
|
536
548
|
if ("off" in inbound) {
|
|
537
|
-
session = inboundOffChannelSession({
|
|
549
|
+
session = inboundOffChannelSession({
|
|
550
|
+
identity: id,
|
|
551
|
+
stateDirectory,
|
|
552
|
+
why: inbound.off,
|
|
553
|
+
stopping: () => stopping.signal.aborted,
|
|
554
|
+
})
|
|
538
555
|
} else {
|
|
539
556
|
// Dialled as the Claude Code session's connection is (`dialChannelInbound`): NATS with a
|
|
540
557
|
// seed, no credential, or the session's broker token, which is then kept renewed and, at a
|
|
@@ -557,6 +574,8 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
557
574
|
warn,
|
|
558
575
|
connect,
|
|
559
576
|
stopping.signal,
|
|
577
|
+
// The relay socket opens no socket again once the server's stop has begun.
|
|
578
|
+
() => stopping.signal.aborted,
|
|
560
579
|
).catch((error: unknown) => {
|
|
561
580
|
if (isStopping) return undefined
|
|
562
581
|
throw error
|
|
@@ -579,6 +598,8 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
579
598
|
session: () => id.id,
|
|
580
599
|
name: natsName,
|
|
581
600
|
signal: stopping.signal,
|
|
601
|
+
stopping: () => stopping.signal.aborted,
|
|
602
|
+
warn,
|
|
582
603
|
swapKeeper: (stop) => {
|
|
583
604
|
stopNatsKeeper()
|
|
584
605
|
stopNatsKeeper = stop
|