@sjawhar/claude-envoy 4.21.2 → 4.22.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/.claude-plugin/plugin.json +1 -1
- package/.omp-plugin/plugin.json +1 -1
- package/README.md +20 -7
- package/dist/codex-hook.js +54 -4
- package/dist/dispatch.js +57 -4
- package/dist/envoy-channel.js +1794 -1028
- package/dist/session-hook.js +54 -4
- package/package.json +1 -1
- package/src/agent-stream-runner.ts +5 -0
- package/src/channel-connection.ts +110 -12
- package/src/channel-topics.ts +26 -39
- package/src/codex/codex-agent-stream.ts +4 -0
- package/src/codex/codex-server.ts +14 -5
- package/src/control-subscription.ts +8 -1
- package/src/envoy-channel-server.ts +46 -7
package/dist/session-hook.js
CHANGED
|
@@ -19218,7 +19218,8 @@ var EnvoySectionSchema = strictObject({
|
|
|
19218
19218
|
natsUrls: array(string2().trim().min(1, "must not be empty")).optional(),
|
|
19219
19219
|
token: string2().optional(),
|
|
19220
19220
|
natsSeed: string2().optional(),
|
|
19221
|
-
natsAuth: string2().optional()
|
|
19221
|
+
natsAuth: string2().optional(),
|
|
19222
|
+
inbound: string2().optional()
|
|
19222
19223
|
});
|
|
19223
19224
|
var EnvoyFileSchema = looseObject({
|
|
19224
19225
|
$schema: string2().optional(),
|
|
@@ -29804,6 +29805,7 @@ function resolvedInterval(raw, fallback) {
|
|
|
29804
29805
|
const named = namedInterval(raw);
|
|
29805
29806
|
return named === undefined ? fallback : Math.min(Math.max(named, MIN_INTERVAL_MS), MAX_INTERVAL_MS);
|
|
29806
29807
|
}
|
|
29808
|
+
var ENVOY_INBOUND_VAR = "ENVOY_INBOUND";
|
|
29807
29809
|
function natsSeedFromEnvironment(env) {
|
|
29808
29810
|
const { NATS_NKEY_SEED_FILE: file, NATS_NKEY_SEED: plain } = env;
|
|
29809
29811
|
if (file !== undefined) {
|
|
@@ -29873,6 +29875,33 @@ function natsCredential(env, section, namedNatsUrls, filePath, session) {
|
|
|
29873
29875
|
throw new NoSessionNamedError(auth.source);
|
|
29874
29876
|
return { kind: "broker", broker: brokerNatsCredential(env, session, auth.source) };
|
|
29875
29877
|
}
|
|
29878
|
+
var LOOPBACK_HOST = /^(localhost|127(\.\d{1,3}){3}|\[::1\])$/i;
|
|
29879
|
+
function natsConflictText(source, natsSetting) {
|
|
29880
|
+
return `${source} is listener, and ${natsSetting} names NATS servers: set one of the two`;
|
|
29881
|
+
}
|
|
29882
|
+
function listenerInbound(source, natsSetting, credential, session) {
|
|
29883
|
+
if (natsSetting !== undefined)
|
|
29884
|
+
return { off: natsConflictText(source, natsSetting) };
|
|
29885
|
+
if ("noBrokerRoute" in credential) {
|
|
29886
|
+
return { off: `${source} is listener, but ${credential.noBrokerRoute}` };
|
|
29887
|
+
}
|
|
29888
|
+
if (credential.scheme !== "DPoP") {
|
|
29889
|
+
return {
|
|
29890
|
+
off: `${source} is listener, but the listener's credential is a bearer (ENVOY_TOKEN, ENVOY_TOKEN_FILE or envoy.token), and the relay socket admits only the session's broker token`
|
|
29891
|
+
};
|
|
29892
|
+
}
|
|
29893
|
+
if (session === undefined) {
|
|
29894
|
+
return {
|
|
29895
|
+
off: `${source} is listener, but this process names no session for its relay socket's token`,
|
|
29896
|
+
noSessionNamed: true
|
|
29897
|
+
};
|
|
29898
|
+
}
|
|
29899
|
+
return { relaySetting: source, relayCredential: credential };
|
|
29900
|
+
}
|
|
29901
|
+
function plainHttpOffMachine(url) {
|
|
29902
|
+
const host = URL.canParse(url) ? new URL(url) : undefined;
|
|
29903
|
+
return host?.protocol === "http:" && !LOOPBACK_HOST.test(host.hostname) ? host.hostname : undefined;
|
|
29904
|
+
}
|
|
29876
29905
|
function envoySettings(env, config, options = {}) {
|
|
29877
29906
|
if (config.kind === "failed")
|
|
29878
29907
|
return offWithError(config.error);
|
|
@@ -29891,7 +29920,18 @@ function envoySettings(env, config, options = {}) {
|
|
|
29891
29920
|
}
|
|
29892
29921
|
const listener = section.url ?? DEFAULT_ENVOY_URL;
|
|
29893
29922
|
const url = namedUrl ?? listener;
|
|
29923
|
+
const namedInbound = nonEmpty(env.ENVOY_INBOUND);
|
|
29924
|
+
const choice = namedInbound !== undefined ? { value: namedInbound, source: ENVOY_INBOUND_VAR } : section.inbound === undefined ? undefined : { value: section.inbound, source: `envoy.inbound in ${config.path}` };
|
|
29925
|
+
const natsSetting = namedNatsUrls.length > 0 ? "ENVOY_NATS_URL" : (section.natsUrls ?? []).length > 0 ? `envoy.natsUrls in ${config.path}` : undefined;
|
|
29926
|
+
if (choice?.value === "listener") {
|
|
29927
|
+
const host = plainHttpOffMachine(url);
|
|
29928
|
+
if (host !== undefined) {
|
|
29929
|
+
const plainHttp = `the listener's address is plain http to ${host}, which is not this machine, so the session's token would cross the network unencrypted; use an https address`;
|
|
29930
|
+
return offWithError(natsSetting === undefined ? `${choice.source} is listener, but ${plainHttp}` : `${natsConflictText(choice.source, natsSetting)}. And ${plainHttp}`);
|
|
29931
|
+
}
|
|
29932
|
+
}
|
|
29894
29933
|
let credential;
|
|
29934
|
+
let noBrokerRoute = "";
|
|
29895
29935
|
try {
|
|
29896
29936
|
const source = bearerSource(env, "ENVOY", () => section.token === undefined ? undefined : {
|
|
29897
29937
|
token: section.token,
|
|
@@ -29901,7 +29941,10 @@ function envoySettings(env, config, options = {}) {
|
|
|
29901
29941
|
checkout: false
|
|
29902
29942
|
});
|
|
29903
29943
|
if (source.kind === "none") {
|
|
29904
|
-
|
|
29944
|
+
const route = brokerRouteConfigured(env);
|
|
29945
|
+
credential = route.traded ? brokerCredential(env, options.session) : undefined;
|
|
29946
|
+
if (!route.traded)
|
|
29947
|
+
noBrokerRoute = route.gap;
|
|
29905
29948
|
} else {
|
|
29906
29949
|
credential = bearerCredential(source.kind === "variable" ? source.token : fileBearer(source, url, "ENVOY", env));
|
|
29907
29950
|
}
|
|
@@ -29912,13 +29955,17 @@ function envoySettings(env, config, options = {}) {
|
|
|
29912
29955
|
let inbound;
|
|
29913
29956
|
if (options.nats === false)
|
|
29914
29957
|
inbound = { off: NO_NATS_CONNECTION };
|
|
29915
|
-
else if (
|
|
29958
|
+
else if (choice !== undefined && choice.value !== "nats" && choice.value !== "listener") {
|
|
29959
|
+
inbound = { off: `${choice.source} is neither nats nor listener` };
|
|
29960
|
+
} else if (choice?.value === "listener") {
|
|
29961
|
+
inbound = listenerInbound(choice.source, natsSetting, credential ?? { noBrokerRoute }, options.session);
|
|
29962
|
+
} else if (natsSetting === undefined)
|
|
29916
29963
|
inbound = { off: NO_NATS_SERVER };
|
|
29917
29964
|
else {
|
|
29918
29965
|
try {
|
|
29919
29966
|
inbound = {
|
|
29920
29967
|
natsUrls,
|
|
29921
|
-
natsServersSetting:
|
|
29968
|
+
natsServersSetting: natsSetting,
|
|
29922
29969
|
natsCredential: natsCredential(env, section, namedNatsUrls, config.path, options.session)
|
|
29923
29970
|
};
|
|
29924
29971
|
} catch (error) {
|
|
@@ -29995,6 +30042,9 @@ function startNotice(config, dispatch, envoy, nats, lost) {
|
|
|
29995
30042
|
if (envoy.enabled && nats && "off" in envoy.inbound) {
|
|
29996
30043
|
parts.push(`Envoy's inbound messages are off: ${envoy.inbound.off}; the Envoy tools still work`);
|
|
29997
30044
|
}
|
|
30045
|
+
if (envoy.enabled && nats && "relaySetting" in envoy.inbound) {
|
|
30046
|
+
parts.push(`Envoy's inbound messages come through the listener's relay socket (${envoy.inbound.relaySetting} is listener), which carries no live view: the Agents page shows nothing of this session's turns`);
|
|
30047
|
+
}
|
|
29998
30048
|
if (envoy.enabled && envoy.passDeadlineRaisedFrom !== undefined) {
|
|
29999
30049
|
parts.push(`Envoy's pass deadline is ${envoy.passDeadlineMs} ms, not the ${envoy.passDeadlineRaisedFrom} ms ENVOY_PASS_DEADLINE_MS names: a deadline inside one ${envoy.heartbeatMs} ms heartbeat interval would abandon every pass before the next tick, so the registration refresh the heartbeat posts for a stalled pass would never run`);
|
|
30000
30050
|
}
|
package/package.json
CHANGED
|
@@ -61,6 +61,10 @@ export interface AgentStreamOptions {
|
|
|
61
61
|
* subjects, and the new control subject is taken at once.
|
|
62
62
|
*/
|
|
63
63
|
readonly admitted?: (session: string) => Promise<void>
|
|
64
|
+
/** Whether a viewer can take this session: false over the listener's relay socket, which
|
|
65
|
+
* carries no agent stream (`ControlSubscriptionOptions.liveView`). The mod's events still
|
|
66
|
+
* reach `observe`. True by default. */
|
|
67
|
+
readonly liveView?: boolean
|
|
64
68
|
/** Whether this session is a Legion pane, whose first command the daemon's bridge writes
|
|
65
69
|
* rather than a person (`./agent-stream-claude`, `PANE_SEEDED_COMMAND`). */
|
|
66
70
|
readonly isPane?: boolean
|
|
@@ -151,6 +155,7 @@ export function startAgentStream(options: AgentStreamOptions): AgentStreamRunner
|
|
|
151
155
|
connection: options.connection,
|
|
152
156
|
session: () => session,
|
|
153
157
|
...(options.admitted === undefined ? {} : { admitted: options.admitted }),
|
|
158
|
+
...(options.liveView === undefined ? {} : { liveView: options.liveView }),
|
|
154
159
|
warn,
|
|
155
160
|
answer: answerControl,
|
|
156
161
|
})
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
// The
|
|
2
|
-
// the session's credential and redialed until it opens (`dialChannelNats`),
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
1
|
+
// The inbound connection a channel server holds for its session, over the session's life: NATS,
|
|
2
|
+
// dialed with the session's credential and redialed until it opens (`dialChannelNats`), or the
|
|
3
|
+
// listener's relay socket where the settings say `ENVOY_INBOUND=listener` (`dialChannelInbound`);
|
|
4
|
+
// admitted as one session id at a time where it presents the session's broker token
|
|
5
|
+
// (`sessionAdmission`, `admittingMoves`), replaced once it closes for good (`channelReopener`),
|
|
6
|
+
// and closed with a bound where no session took it (`closeUnstartedConnection`). Both channel
|
|
7
|
+
// servers take theirs from here: Claude Code's (`./envoy-channel-server`) and Codex's
|
|
8
|
+
// (`./codex/codex-server`).
|
|
9
|
+
import type { RequestCredential } from "@legion/envoy-client/bearer"
|
|
10
|
+
import { inboundUnavailable } from "@legion/envoy-client/consumer-delivery"
|
|
7
11
|
import { LoggableError } from "@legion/envoy-client/errors"
|
|
12
|
+
import { connectListenerRelay, type RelayOutage } from "@legion/envoy-client/listener-relay"
|
|
8
13
|
import {
|
|
9
14
|
connectNats,
|
|
10
15
|
NATS_DIAL_PLAN,
|
|
@@ -121,6 +126,16 @@ export interface ChannelNatsSettings {
|
|
|
121
126
|
readonly natsCredential: NatsCredential
|
|
122
127
|
}
|
|
123
128
|
|
|
129
|
+
/** What `dialChannelInbound` opens the listener's relay socket with (`EnvoyInbound` where
|
|
130
|
+
* `ENVOY_INBOUND=listener`): the setting that chose it, and the session's broker credential. */
|
|
131
|
+
export interface ChannelRelaySettings {
|
|
132
|
+
readonly relaySetting: string
|
|
133
|
+
readonly relayCredential: RequestCredential
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** How a server takes its session's inbound messages (`EnvoyInbound` where they are on). */
|
|
137
|
+
export type ChannelInboundSettings = ChannelNatsSettings | ChannelRelaySettings
|
|
138
|
+
|
|
124
139
|
/**
|
|
125
140
|
* Opens the session's NATS connection with the credential `inbound` resolved. A broker token is
|
|
126
141
|
* signed into the CONNECT itself, so a live one is taken before the dial; and the connection is
|
|
@@ -231,6 +246,77 @@ export async function dialChannelNats(
|
|
|
231
246
|
}
|
|
232
247
|
}
|
|
233
248
|
|
|
249
|
+
/** The channel server's inbound connection, over NATS or the listener's relay socket
|
|
250
|
+
* (`dialChannelInbound`). */
|
|
251
|
+
export interface ChannelInbound {
|
|
252
|
+
readonly connection: ChannelConnection
|
|
253
|
+
/** Stops the keeper of a NATS connection's broker token; nothing for the others. */
|
|
254
|
+
readonly stop: () => void
|
|
255
|
+
/** The move a `/clear` or a `/new` runs before the new id's relay subject opens, where the
|
|
256
|
+
* connection is admitted as one session at a time: a NATS connection presenting the session's
|
|
257
|
+
* broker token (`followNatsCredential`), and the relay socket, which is opened for one session
|
|
258
|
+
* with that session's own token (`ListenerRelay.follow`). */
|
|
259
|
+
readonly followSession?: () => Promise<void>
|
|
260
|
+
/** Has a refused trade of the connection's own told `outage`, the session's, rather than the
|
|
261
|
+
* report it was opened with: the relay socket's (`ListenerRelay.reportOutagesTo`); absent for
|
|
262
|
+
* NATS, which trades no token of its own outside the keeper. */
|
|
263
|
+
readonly reportOutagesTo?: (outage: RelayOutage) => void
|
|
264
|
+
/** Whether the session's live view takes its control subject on the connection: never over the
|
|
265
|
+
* relay socket, which carries no agent stream (`ControlSubscriptionOptions.liveView`). */
|
|
266
|
+
readonly liveView: boolean
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Opens the session's inbound as its settings say: NATS (`dialChannelNats`, `dial` its `connect`),
|
|
271
|
+
* or the relay socket of the listener at `listenerUrl` for the session `session` names now, with
|
|
272
|
+
* the session's broker credential (`connectListenerRelay`), answered once its first socket is
|
|
273
|
+
* open. The socket opens again on its own after a close for the server's life, warning on `warn`
|
|
274
|
+
* why an open failed; a token the broker refused is told on `warn` until the session holds the
|
|
275
|
+
* socket and tells its own outage (`ListenerRelay.reportOutagesTo`), which the session's first
|
|
276
|
+
* accepted registration ends. `signal` aborting (the process is stopping) ends either wait at once.
|
|
277
|
+
*/
|
|
278
|
+
export async function dialChannelInbound(
|
|
279
|
+
inbound: ChannelInboundSettings,
|
|
280
|
+
listenerUrl: string,
|
|
281
|
+
session: () => string,
|
|
282
|
+
name: string,
|
|
283
|
+
warn: (text: string, dialError?: unknown) => void,
|
|
284
|
+
dial: (options: ConnectionOptions) => Promise<NatsConnection> = connect,
|
|
285
|
+
signal?: AbortSignal,
|
|
286
|
+
): Promise<ChannelInbound> {
|
|
287
|
+
if ("natsUrls" in inbound) {
|
|
288
|
+
return { ...(await dialChannelNats(inbound, name, warn, dial, signal)), liveView: true }
|
|
289
|
+
}
|
|
290
|
+
const relay = await connectListenerRelay(
|
|
291
|
+
{
|
|
292
|
+
url: listenerUrl,
|
|
293
|
+
session,
|
|
294
|
+
credential: inbound.relayCredential,
|
|
295
|
+
// The socket's report until the session holds it (`reportOutagesTo`): before the session
|
|
296
|
+
// starts there is no session to tell, so a refused trade is said once on stderr, worded
|
|
297
|
+
// (`inboundUnavailable`), however many trades fail. The socket tries the trade again on its
|
|
298
|
+
// own, 1 s doubling to a minute; once one succeeds the socket opens and the session starts,
|
|
299
|
+
// and nothing tells the session of the refusals before.
|
|
300
|
+
outage: inboundUnavailable(
|
|
301
|
+
(why) =>
|
|
302
|
+
warn(
|
|
303
|
+
`this session's relay socket has no token: ${why}; the session starts once a trade succeeds`,
|
|
304
|
+
),
|
|
305
|
+
() => undefined,
|
|
306
|
+
),
|
|
307
|
+
log: (line) => warn(line),
|
|
308
|
+
},
|
|
309
|
+
signal,
|
|
310
|
+
)
|
|
311
|
+
return {
|
|
312
|
+
connection: relay,
|
|
313
|
+
stop: () => {},
|
|
314
|
+
followSession: () => relay.follow(),
|
|
315
|
+
reportOutagesTo: (outage) => relay.reportOutagesTo(outage),
|
|
316
|
+
liveView: false,
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
234
320
|
/** How long a start that failed waits for its connection to drain before closing it. */
|
|
235
321
|
const START_DRAIN_BOUND_MS = 2_000
|
|
236
322
|
|
|
@@ -251,15 +337,22 @@ export interface ReopenedConnection {
|
|
|
251
337
|
readonly connection: ChannelConnection
|
|
252
338
|
/** `ChannelSessionOptions.followSession`, for this connection. */
|
|
253
339
|
readonly followSession?: () => Promise<void>
|
|
340
|
+
/** `ChannelSessionOptions.reportOutagesTo`, for this connection. */
|
|
341
|
+
readonly reportOutagesTo?: (outage: RelayOutage) => void
|
|
254
342
|
}
|
|
255
343
|
|
|
256
|
-
/** Opens a new
|
|
344
|
+
/** Opens a new inbound connection in place of one closed for good
|
|
345
|
+
* (`ChannelSessionOptions.reopen`). */
|
|
257
346
|
export type ReopenConnection = (failed: (dialError: unknown) => void) => Promise<ReopenedConnection>
|
|
258
347
|
|
|
259
348
|
/** What a server hands `channelReopener`. */
|
|
260
349
|
export interface ChannelReopenerOptions {
|
|
261
|
-
/** What the server's first connection was
|
|
262
|
-
readonly inbound:
|
|
350
|
+
/** What the server's first connection was opened with. */
|
|
351
|
+
readonly inbound: ChannelInboundSettings
|
|
352
|
+
/** The listener a relay socket is opened at (`EnabledEnvoySettings.url`). */
|
|
353
|
+
readonly listenerUrl: string
|
|
354
|
+
/** The session id the server holds now, which a relay socket is opened for. */
|
|
355
|
+
readonly session: () => string
|
|
263
356
|
/** The connection name every connection of the server presents. */
|
|
264
357
|
readonly name: string
|
|
265
358
|
/** Aborts when the server stops: it ends a dial's waits and its redials, and closes a connection
|
|
@@ -276,8 +369,10 @@ export interface ChannelReopenerOptions {
|
|
|
276
369
|
|
|
277
370
|
/**
|
|
278
371
|
* A server's `ChannelSessionOptions.reopen`: a new connection once the session's closed for good,
|
|
279
|
-
*
|
|
280
|
-
* server stops.
|
|
372
|
+
* opened as the server's first one was (`dialChannelInbound`), warning of each redial on stderr,
|
|
373
|
+
* until one opens or the server stops. A relay socket opens again on its own after every close, so
|
|
374
|
+
* it closes for good only as the server closes it; a NATS connection is what this replaces in a
|
|
375
|
+
* running session. The closed connection's token keeper stops before the dial, and the new one's is
|
|
281
376
|
* the one the teardown stops; the live view moves to the new connection. Under a broker token the
|
|
282
377
|
* new connection presents the token the session's credential holds as it dials, the one traded
|
|
283
378
|
* last (a lapsed one is traded again for the id the session has now): through a `/clear`, the old
|
|
@@ -292,8 +387,10 @@ export interface ChannelReopenerOptions {
|
|
|
292
387
|
export function channelReopener(options: ChannelReopenerOptions): ReopenConnection {
|
|
293
388
|
return async (failed) => {
|
|
294
389
|
options.swapKeeper(() => undefined)
|
|
295
|
-
const next = await
|
|
390
|
+
const next = await dialChannelInbound(
|
|
296
391
|
options.inbound,
|
|
392
|
+
options.listenerUrl,
|
|
393
|
+
options.session,
|
|
297
394
|
options.name,
|
|
298
395
|
(text, dialError) => {
|
|
299
396
|
process.stderr.write(`envoy-channel: ${text}\n`)
|
|
@@ -313,6 +410,7 @@ export function channelReopener(options: ChannelReopenerOptions): ReopenConnecti
|
|
|
313
410
|
return {
|
|
314
411
|
connection: next.connection,
|
|
315
412
|
...(followSession === undefined ? {} : { followSession }),
|
|
413
|
+
...(next.reportOutagesTo === undefined ? {} : { reportOutagesTo: next.reportOutagesTo }),
|
|
316
414
|
}
|
|
317
415
|
}
|
|
318
416
|
}
|
package/src/channel-topics.ts
CHANGED
|
@@ -1,50 +1,37 @@
|
|
|
1
|
-
import type { RelayHeaders } from "@legion/envoy-client/consumer-delivery"
|
|
2
1
|
import { DedupeIdentitySchema } from "@legion/envoy-client/delivery"
|
|
3
2
|
import { natsFailureReason } from "@legion/envoy-client/errors"
|
|
3
|
+
import type {
|
|
4
|
+
InboundConnection,
|
|
5
|
+
InboundMessage,
|
|
6
|
+
InboundStatus,
|
|
7
|
+
InboundSubscription,
|
|
8
|
+
} from "@legion/envoy-client/inbound-connection"
|
|
4
9
|
import { expandSubscriptionTopics } from "@legion/envoy-client/transport"
|
|
5
10
|
import { z } from "zod"
|
|
6
11
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* answered on a reply: a session publishes no receipt.
|
|
16
|
-
*/
|
|
17
|
-
readonly reply?: string
|
|
18
|
-
/** The message's NATS headers; only a relay frame (consumer delivery) carries any read here. */
|
|
19
|
-
readonly headers?: RelayHeaders
|
|
20
|
-
}
|
|
12
|
+
/**
|
|
13
|
+
* A message as the connection yields it (a nats.js `Msg`, or a relay socket's). Two kinds carry a
|
|
14
|
+
* reply: a relay frame, whose reply is the relay's own answer token (`RelayMessage`, consumer
|
|
15
|
+
* delivery), and a request on the live view's control subject, answered there
|
|
16
|
+
* (`./agent-stream-runner`). No Envoy envelope is answered on a reply: a session publishes no
|
|
17
|
+
* receipt. Only a relay frame carries headers read here.
|
|
18
|
+
*/
|
|
19
|
+
export type ChannelBrokerMessage = InboundMessage
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
21
|
+
/** A subscription of the channel session's connection. */
|
|
22
|
+
export type ChannelTopicSubscription = InboundSubscription
|
|
25
23
|
|
|
26
|
-
/** A
|
|
27
|
-
export
|
|
28
|
-
readonly type: string
|
|
29
|
-
}
|
|
24
|
+
/** A status event of the channel session's connection; only its `type` is read. */
|
|
25
|
+
export type ChannelConnectionStatus = InboundStatus
|
|
30
26
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
isClosed(): boolean
|
|
40
|
-
drain(): Promise<void>
|
|
41
|
-
close(): Promise<void>
|
|
42
|
-
/** The connection's status events (nats.js `status()`), which end when it closes. */
|
|
43
|
-
status(): AsyncIterable<ChannelConnectionStatus>
|
|
44
|
-
/** Settles once the connection is closed for good, with the error that closed it, if any
|
|
45
|
-
* (nats.js `closed()`); already settled for one closed before the call. */
|
|
46
|
-
closed(): Promise<unknown>
|
|
47
|
-
}
|
|
27
|
+
/**
|
|
28
|
+
* The inbound connection a channel session holds, NATS or the listener's relay socket
|
|
29
|
+
* (`@legion/envoy-client/inbound-connection`). Over NATS a channel session opens two
|
|
30
|
+
* subscriptions on it: the session's relay subject, where every Envoy message arrives, and the
|
|
31
|
+
* live view's control subject, which a viewer takes the session on (`./agent-stream-runner`). Over
|
|
32
|
+
* the relay socket it opens the relay subject alone: the socket carries no live view.
|
|
33
|
+
*/
|
|
34
|
+
export type ChannelConnection = InboundConnection
|
|
48
35
|
|
|
49
36
|
/**
|
|
50
37
|
* The topics this session follows, and the broker connection it holds now. Nothing is subscribed
|
|
@@ -52,6 +52,9 @@ export interface CodexAgentStreamOptions {
|
|
|
52
52
|
* Claude Code live view's).
|
|
53
53
|
*/
|
|
54
54
|
readonly admitted?: (session: string) => Promise<void>
|
|
55
|
+
/** Whether a viewer can take this session: false over the listener's relay socket, which
|
|
56
|
+
* carries no agent stream (`ControlSubscriptionOptions.liveView`). True by default. */
|
|
57
|
+
readonly liveView?: boolean
|
|
55
58
|
}
|
|
56
59
|
|
|
57
60
|
/** How many expected person messages are kept for tagging. */
|
|
@@ -69,6 +72,7 @@ export function startCodexAgentStream(options: CodexAgentStreamOptions): CodexAg
|
|
|
69
72
|
connection: options.connection,
|
|
70
73
|
session: () => session,
|
|
71
74
|
...(options.admitted === undefined ? {} : { admitted: options.admitted }),
|
|
75
|
+
...(options.liveView === undefined ? {} : { liveView: options.liveView }),
|
|
72
76
|
warn,
|
|
73
77
|
answer: answerControl,
|
|
74
78
|
})
|
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
admittingMoves,
|
|
26
26
|
channelReopener,
|
|
27
27
|
closeUnstartedConnection,
|
|
28
|
-
|
|
28
|
+
dialChannelInbound,
|
|
29
29
|
sessionAdmission,
|
|
30
30
|
} from "../channel-connection"
|
|
31
31
|
import type { ChannelConnection } from "../channel-topics"
|
|
@@ -536,16 +536,19 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
536
536
|
if ("off" in inbound) {
|
|
537
537
|
session = inboundOffChannelSession({ identity: id, stateDirectory, why: inbound.off })
|
|
538
538
|
} else {
|
|
539
|
-
// Dialled as the Claude Code session's connection is (`
|
|
540
|
-
// credential, or the session's broker token, which is then kept renewed and, at a
|
|
541
|
-
// moved to the new session's (`followSession`) before that session's subjects are
|
|
539
|
+
// Dialled as the Claude Code session's connection is (`dialChannelInbound`): NATS with a
|
|
540
|
+
// seed, no credential, or the session's broker token, which is then kept renewed and, at a
|
|
541
|
+
// `/new`, moved to the new session's (`followSession`) before that session's subjects are
|
|
542
|
+
// taken; or the listener's relay socket, opened again for the new session at a `/new`. A
|
|
542
543
|
// connection that closes for good is replaced as the Claude Code one is (`channelReopener`):
|
|
543
544
|
// Codex starts no plugin server again for a thread whose server lost its connection.
|
|
544
545
|
const natsName = `codex-envoy-channel-${id.id}`
|
|
545
546
|
// A stop asked for while the token or the dial is awaited ends the start, and is no failure:
|
|
546
547
|
// the process ends as an asked-for stop does, as the Claude Code server's does.
|
|
547
|
-
const dialled = await
|
|
548
|
+
const dialled = await dialChannelInbound(
|
|
548
549
|
inbound,
|
|
550
|
+
envoy.url,
|
|
551
|
+
() => id.id,
|
|
549
552
|
natsName,
|
|
550
553
|
warn,
|
|
551
554
|
connect,
|
|
@@ -568,6 +571,8 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
568
571
|
const followSession = admitting(dialled.followSession)
|
|
569
572
|
const reopen = channelReopener({
|
|
570
573
|
inbound,
|
|
574
|
+
listenerUrl: envoy.url,
|
|
575
|
+
session: () => id.id,
|
|
571
576
|
name: natsName,
|
|
572
577
|
signal: stopping.signal,
|
|
573
578
|
swapKeeper: (stop) => {
|
|
@@ -590,6 +595,9 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
590
595
|
heartbeatMs: envoy.heartbeatMs,
|
|
591
596
|
identity: id,
|
|
592
597
|
...(followSession === undefined ? {} : { followSession }),
|
|
598
|
+
...(dialled.reportOutagesTo === undefined
|
|
599
|
+
? {}
|
|
600
|
+
: { reportOutagesTo: dialled.reportOutagesTo }),
|
|
593
601
|
notifier: {
|
|
594
602
|
notification: ({ content, eventId }) =>
|
|
595
603
|
codexDelivery.deliver({ content, ...(eventId === undefined ? {} : { eventId }) }),
|
|
@@ -636,6 +644,7 @@ export async function runCodexEnvoyServer(): Promise<void> {
|
|
|
636
644
|
session = started
|
|
637
645
|
stream = startCodexAgentStream({
|
|
638
646
|
connection,
|
|
647
|
+
liveView: dialled.liveView,
|
|
639
648
|
sessionId: id.id,
|
|
640
649
|
warn,
|
|
641
650
|
...(admission === undefined
|
|
@@ -31,6 +31,13 @@ export interface ControlSubscriptionOptions {
|
|
|
31
31
|
* is taken at once.
|
|
32
32
|
*/
|
|
33
33
|
readonly admitted?: (session: string) => Promise<void>
|
|
34
|
+
/**
|
|
35
|
+
* Whether a viewer can take this session: false where the session's inbound is the listener's
|
|
36
|
+
* relay socket (`ENVOY_INBOUND=listener`), which carries no agent stream
|
|
37
|
+
* (`@legion/envoy-client/listener-relay`), so the control subject is never taken, no viewer arms
|
|
38
|
+
* the view's publisher, and no frame goes out. True by default.
|
|
39
|
+
*/
|
|
40
|
+
readonly liveView?: boolean
|
|
34
41
|
/** Where a failure that must not stop the session is written. */
|
|
35
42
|
readonly warn: (text: string) => void
|
|
36
43
|
/**
|
|
@@ -108,7 +115,7 @@ export function controlSubscription(options: ControlSubscriptionOptions): Contro
|
|
|
108
115
|
/** Takes the control subject on the connection held now. A closed one throws on a subscribe, so
|
|
109
116
|
* the subject waits for the connection the server swaps in (`useConnection`). */
|
|
110
117
|
function subscribe(): void {
|
|
111
|
-
if (stopped || connection.isClosed()) return
|
|
118
|
+
if (stopped || options.liveView === false || connection.isClosed()) return
|
|
112
119
|
subscription?.unsubscribe()
|
|
113
120
|
const forSession = options.session()
|
|
114
121
|
const taken = connection.subscribe(agentStreamControlSubject(forSession))
|
|
@@ -19,7 +19,6 @@ import {
|
|
|
19
19
|
inboundRestoredNoticeText,
|
|
20
20
|
inboundUnavailable,
|
|
21
21
|
inboundUnavailableNoticeText,
|
|
22
|
-
registerWithRelay,
|
|
23
22
|
} from "@legion/envoy-client/consumer-delivery"
|
|
24
23
|
import {
|
|
25
24
|
createDeliveryDedupe,
|
|
@@ -46,7 +45,9 @@ import {
|
|
|
46
45
|
SHUTDOWN_SETTLE_MS,
|
|
47
46
|
} from "@legion/envoy-client/envoy-settings"
|
|
48
47
|
import { LoggableError, messageFor, natsFailureReason } from "@legion/envoy-client/errors"
|
|
48
|
+
import type { RelayOutage } from "@legion/envoy-client/listener-relay"
|
|
49
49
|
import { machineID } from "@legion/envoy-client/machine"
|
|
50
|
+
import { refusedTopicsNoticeText, registerWithRelay } from "@legion/envoy-client/relay-registration"
|
|
50
51
|
import {
|
|
51
52
|
type SessionConfigFile,
|
|
52
53
|
sessionConfigFile,
|
|
@@ -98,7 +99,7 @@ import {
|
|
|
98
99
|
admittingMoves,
|
|
99
100
|
channelReopener,
|
|
100
101
|
closeUnstartedConnection,
|
|
101
|
-
|
|
102
|
+
dialChannelInbound,
|
|
102
103
|
type ReopenConnection,
|
|
103
104
|
type ReopenedConnection,
|
|
104
105
|
START_STOPPING,
|
|
@@ -372,6 +373,9 @@ export interface ChannelSessionOptions {
|
|
|
372
373
|
* alone. A failed move leaves the relay closed and the next registration retries it.
|
|
373
374
|
*/
|
|
374
375
|
readonly followSession?: () => Promise<void>
|
|
376
|
+
/** Has a refused trade of the connection's own told the session's outage report: the relay
|
|
377
|
+
* socket's (`ListenerRelay.reportOutagesTo`); absent for NATS. */
|
|
378
|
+
readonly reportOutagesTo?: (outage: RelayOutage) => void
|
|
375
379
|
/**
|
|
376
380
|
* Told each `/clear` this session adopts from the handoff file, as it moves from `previous` to
|
|
377
381
|
* `next` (`adoptHandoff`), before the new id registers: what else the server keeps under the
|
|
@@ -1341,6 +1345,9 @@ export async function startChannelSession(options: ChannelSessionOptions): Promi
|
|
|
1341
1345
|
})
|
|
1342
1346
|
},
|
|
1343
1347
|
)
|
|
1348
|
+
// A relay socket's refused trade is this session's outage too, told once with its
|
|
1349
|
+
// registrations', which only a registration the listener accepted ends.
|
|
1350
|
+
options.reportOutagesTo?.(inboundOutage)
|
|
1344
1351
|
|
|
1345
1352
|
/** The title last stamped, so a heartbeat stamps nothing new. */
|
|
1346
1353
|
let stampedTitle: { readonly sessionID: string; readonly title: string } | undefined
|
|
@@ -1418,7 +1425,7 @@ export async function startChannelSession(options: ChannelSessionOptions): Promi
|
|
|
1418
1425
|
relay: asking?.delivery,
|
|
1419
1426
|
...(relayMissing === undefined ? {} : { relayMissing }),
|
|
1420
1427
|
ownAgentSubject: directSubject,
|
|
1421
|
-
followed: followedTopics.topics(),
|
|
1428
|
+
followed: () => followedTopics.topics(),
|
|
1422
1429
|
roles: heldRole === undefined ? [] : [heldRole],
|
|
1423
1430
|
outage: inboundOutage,
|
|
1424
1431
|
send: (topics, deliveryRequest) => {
|
|
@@ -1453,6 +1460,27 @@ export async function startChannelSession(options: ChannelSessionOptions): Promi
|
|
|
1453
1460
|
answered: (result) => {
|
|
1454
1461
|
if (asking !== undefined) takeDeliveryAnswer(sessionID, asking, result)
|
|
1455
1462
|
},
|
|
1463
|
+
// An edge listener refused these topics for the projects of the session's token: they stop
|
|
1464
|
+
// being followed, here and in the ledger, so no later registration carries them and a
|
|
1465
|
+
// restart does not follow them again, and the session is told which, once, where it is told
|
|
1466
|
+
// an outage, whichever path registered. A session this process has since left keeps its own.
|
|
1467
|
+
drop: (topics, reason) => {
|
|
1468
|
+
if (identity.id !== sessionID) return
|
|
1469
|
+
for (const topic of topics) userTopics.delete(topic)
|
|
1470
|
+
followedTopics.unfollow(topics)
|
|
1471
|
+
ledger.followedChanged().catch((error: unknown) => {
|
|
1472
|
+
process.stderr.write(
|
|
1473
|
+
`envoy-channel: the topics the listener refused stay in the delivery state until it is written next — ${messageFor(error)}\n`,
|
|
1474
|
+
)
|
|
1475
|
+
})
|
|
1476
|
+
if (shuttingDown) return
|
|
1477
|
+
delivery.notice(refusedTopicsNoticeText(topics, reason)).catch((noticeError: unknown) => {
|
|
1478
|
+
process.stderr.write(
|
|
1479
|
+
`envoy-channel: the session could not be told which topics the listener refused — ${messageFor(noticeError)}\n`,
|
|
1480
|
+
)
|
|
1481
|
+
})
|
|
1482
|
+
},
|
|
1483
|
+
log: (line) => process.stderr.write(`envoy-channel: ${line}\n`),
|
|
1456
1484
|
})
|
|
1457
1485
|
}
|
|
1458
1486
|
|
|
@@ -2109,6 +2137,7 @@ export async function startChannelSession(options: ChannelSessionOptions): Promi
|
|
|
2109
2137
|
}
|
|
2110
2138
|
connection = live
|
|
2111
2139
|
followSession = reopened.followSession
|
|
2140
|
+
reopened.reportOutagesTo?.(inboundOutage)
|
|
2112
2141
|
// A handoff that waits for its relay opens it on this connection as it registers.
|
|
2113
2142
|
if (relay !== undefined && !handoffDeliveryPending) subscribeRelay(relay)
|
|
2114
2143
|
watchConnection(live)
|
|
@@ -2775,12 +2804,16 @@ export async function runEnvoyChannelServer(): Promise<void> {
|
|
|
2775
2804
|
session = inboundOffChannelSession({ identity, stateDirectory, why: inbound.off })
|
|
2776
2805
|
} else {
|
|
2777
2806
|
starting =
|
|
2778
|
-
|
|
2779
|
-
? "it
|
|
2780
|
-
:
|
|
2807
|
+
"relaySetting" in inbound
|
|
2808
|
+
? "it opens the listener's relay socket for this session, and its stderr says why an open failed"
|
|
2809
|
+
: inbound.natsCredential.kind === "broker"
|
|
2810
|
+
? "it waits for this session's broker token, and its stderr says what each trade came to"
|
|
2811
|
+
: "it connects to NATS"
|
|
2781
2812
|
const natsName = `claude-envoy-channel-${identity.id}`
|
|
2782
|
-
const dialled = await
|
|
2813
|
+
const dialled = await dialChannelInbound(
|
|
2783
2814
|
inbound,
|
|
2815
|
+
envoy.url,
|
|
2816
|
+
currentSessionId,
|
|
2784
2817
|
natsName,
|
|
2785
2818
|
(text, dialError) => {
|
|
2786
2819
|
// A redial's warning names what the start waits on; the token keeper warns through the
|
|
@@ -2808,6 +2841,8 @@ export async function runEnvoyChannelServer(): Promise<void> {
|
|
|
2808
2841
|
const followSession = admitting(dialled.followSession)
|
|
2809
2842
|
const reopen = channelReopener({
|
|
2810
2843
|
inbound,
|
|
2844
|
+
listenerUrl: envoy.url,
|
|
2845
|
+
session: currentSessionId,
|
|
2811
2846
|
name: natsName,
|
|
2812
2847
|
signal: booting.signal,
|
|
2813
2848
|
swapKeeper: (stop) => {
|
|
@@ -2838,6 +2873,9 @@ export async function runEnvoyChannelServer(): Promise<void> {
|
|
|
2838
2873
|
// The QA override names a fixed identity; only a Claude-minted id follows `/clear`.
|
|
2839
2874
|
...(configuredValue(overrideSessionId) === undefined ? { handoffPid: process.ppid } : {}),
|
|
2840
2875
|
...(followSession === undefined ? {} : { followSession }),
|
|
2876
|
+
...(dialled.reportOutagesTo === undefined
|
|
2877
|
+
? {}
|
|
2878
|
+
: { reportOutagesTo: dialled.reportOutagesTo }),
|
|
2841
2879
|
// The live view follows a `/clear` the mod reported before it began listening.
|
|
2842
2880
|
adopted: (previous, next) => agentStream?.sessionAdopted(previous, next),
|
|
2843
2881
|
reopen,
|
|
@@ -2859,6 +2897,7 @@ export async function runEnvoyChannelServer(): Promise<void> {
|
|
|
2859
2897
|
if (stopping || connection === undefined) return
|
|
2860
2898
|
agentStream = startAgentStream({
|
|
2861
2899
|
connection,
|
|
2900
|
+
liveView: dialled.liveView,
|
|
2862
2901
|
// Only a Claude-minted id follows `/clear`; the override names a fixed identity.
|
|
2863
2902
|
followsSessionId: configuredValue(overrideSessionId) === undefined,
|
|
2864
2903
|
...(admission === undefined ? {} : { admitted: (id: string) => admission.admitted(id) }),
|