@sjawhar/claude-envoy 6.13.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.
@@ -18042,6 +18042,11 @@ var COMMENT_ARGUMENT = "Comment id (uuid), or a dispatch://KEY/comment/<id> or "
18042
18042
  var MESSAGE_ARGUMENT = "Message id (uuid), or a dispatch://KEY/message/<id> reference; a reference accepts an " + "8+ character id prefix that is unique on its owner.";
18043
18043
  var ASK_BOUND_RULE = "Dispatch bounds an ask's question and its options together, refuses a longer one and names " + "the bound, and `dispatch limits` prints it. An ask is short and asks one thing; its reasoning " + "goes in the document, not in the question.";
18044
18044
  var DISPATCH_HANDOFF_BODY_MAX = 262144;
18045
+ var STEP_RESULTS = ["passed", "failed", "stopped"];
18046
+ var PLANNED_STEP_STATES = ["not started", "running", ...STEP_RESULTS];
18047
+ var STEP_TASK_MAX = 64;
18048
+ var STEP_TASK_PATTERN = new RegExp(`^[a-z0-9][a-z0-9-]{0,${STEP_TASK_MAX - 1}}$`);
18049
+ var STEP_TASK_RULE_WORDS = `lowercase letters, digits and hyphens, starting with a letter or a digit, at most ${STEP_TASK_MAX} bytes`;
18045
18050
  var HANDOFF_PHASE_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
18046
18051
  var HANDOFF_PHASE_RULE_WORDS = "a lowercase letter, then up to 31 lowercase letters, digits or hyphens, such as implement";
18047
18052
  var HANDOFF_PHASE_RULE = `phase is not a handoff phase: ${HANDOFF_PHASE_RULE_WORDS}`;
@@ -18897,7 +18902,7 @@ var dispatchToolSpecs = [
18897
18902
  {
18898
18903
  name: "dispatch_steps",
18899
18904
  example: { issue: "DSP-1" },
18900
- description: "Print each step that ran on an issue, as the Legion daemon recorded it at the step's " + "start and at its end, and the experiment and the variant the issue was drawn into, where " + "its records name one. The first line is `<KEY>: <n> steps in <m> tasks`. Where the " + "records of the issue's newest run name an experiment, the next line is `Experiment " + "<experiment>: variant <variant>.`, and an issue whose newest run's records name none " + "prints no such line. Then each run of a step prints one line, task by task, in the order " + "each task first ran: `<task> " + "<step> <kind> <role> <type> attempt <attempt> <agent> <model> <started_at> <ended_at> " + "<result>`. kind is step, retro or merge. step is the step's place in its task, from 1, " + "and 0 for a retro or a merge. attempt counts the runs of the same task, step and kind, " + "from 1. agent is the claim of the role that ran the step, and model is the model that " + "answered it, as provider/model. result is passed, failed or stopped. A field with no " + "value prints -, and a run that has not ended prints running for its end and its result. " + "A closed issue's steps read too. An issue that no step ran on prints 0 steps in 0 tasks, " + "and a key Dispatch does not hold fails as dispatch read fails for it, NOT_FOUND: not " + `found. ${ISSUE_REFERENCE}`,
18905
+ description: "Print each task of an issue's plan and each step that ran on the issue, as the Legion " + "daemon recorded it at the step's start and at its end, and the experiment and the " + "variant the issue was drawn into, where its records name one. The first line is " + "`<KEY>: <n> steps in <m> tasks`, where m counts each task listed, a task of the plan " + "with no run included. Where the records of the issue's newest run name an experiment, " + "the next line is `Experiment <experiment>: variant <variant>.`, and an issue whose " + "newest run's records name none prints no such line. The tasks of the issue's newest " + "plan come first, in the plan's order, then each other task with a run, in the order " + "each first ran. A task of the plan prints `task <task> needs <needs>: <place> <step> " + "<state>, ...`: needs is the tasks it needs as a list, [a,b], [] where it needs none, " + "and - where its plan node recorded none, then each step the plan gives it, where state " + "is where Dispatch says it stands among the records of the plan's own run: not started, " + "running, or the result of its newest run there. Then each run of a step of the task " + "prints one line: `<task> " + "<step> <kind> <role> <type> attempt <attempt> <agent> <model> <started_at> <ended_at> " + "<result>`. kind is step, retro or merge. step is the step's place in its task, from 1, " + "and 0 for a retro or a merge. attempt counts the runs of the same task, step and kind, " + "from 1. agent is the claim of the role that ran the step, and model is the model that " + "answered it, as provider/model. result is passed, failed or stopped. A field with no " + "value prints -, and a run that has not ended prints running for its end and its result. " + "A closed issue's steps read too. An issue that no step ran on and that has no plan prints " + "0 steps in 0 tasks, and a key Dispatch does not hold fails as dispatch read fails for it, " + `NOT_FOUND: not found. ${ISSUE_REFERENCE}`,
18901
18906
  arguments: (z) => ({
18902
18907
  issue: z.string().describe(ISSUE_REFERENCE)
18903
18908
  }),
@@ -22769,6 +22774,7 @@ var SESSION_LIVE_WINDOW_MS = 300000;
22769
22774
  var LISTENER_CALL_TIMEOUT_MS = 5000;
22770
22775
  var LISTENER_CALL_RETRY_DELAY_MS = 250;
22771
22776
  var LISTENER_CALL_ATTEMPTS = 2;
22777
+ var BUS_UNANSWERED_PREFIX = "envoy: the message bus did not answer";
22772
22778
  // ../contracts/src/related-forms.ts
22773
22779
  function relatedCheckFormOf(suggestions) {
22774
22780
  const { outcome, missing } = suggestions;
@@ -35894,6 +35900,11 @@ function stepLine(record) {
35894
35900
  ...record.ended_at === null ? ["running", "running"] : [record.ended_at, orDash(record.result)]
35895
35901
  ].join(" ");
35896
35902
  }
35903
+ function taskLine(task, needs, planned) {
35904
+ const states = planned.map((step) => `${step.step} ${step.name} ${step.state}`);
35905
+ const needed = needs === null ? "-" : `[${needs.join(",")}]`;
35906
+ return `task ${task} needs ${needed}: ${states.join(", ")}`;
35907
+ }
35897
35908
  function variantLines(records) {
35898
35909
  const newest = Math.max(...records.map((record) => record.generation));
35899
35910
  const named = records.find((record) => record.generation === newest && record.experiment != null && record.variant != null);
@@ -35902,13 +35913,20 @@ function variantLines(records) {
35902
35913
  async function stepsCommand(context) {
35903
35914
  const { client, issue } = context;
35904
35915
  const read = await client.listSteps(issue());
35905
- const tasks = (read.tasks ?? []).map((task) => ({ task: task.task, steps: task.steps ?? [] }));
35916
+ const tasks = (read.tasks ?? []).map((task) => ({ ...task, steps: task.steps ?? [] }));
35906
35917
  const records = tasks.flatMap((task) => task.steps);
35918
+ const lines = tasks.flatMap((task) => {
35919
+ const runs = task.steps.map(stepLine);
35920
+ const planned = task.planned ?? [];
35921
+ if (planned.length === 0)
35922
+ return runs;
35923
+ return [taskLine(task.task, task.needs ?? null, planned), ...runs];
35924
+ });
35907
35925
  return {
35908
35926
  text: [
35909
35927
  `${read.issue}: ${records.length} steps in ${tasks.length} tasks`,
35910
35928
  ...variantLines(records),
35911
- ...records.map(stepLine)
35929
+ ...lines
35912
35930
  ].join(`
35913
35931
  `),
35914
35932
  details: { issue: read.issue, tasks }
@@ -38066,6 +38084,8 @@ function keepNatsCredential(connection, credential, warn) {
38066
38084
  if (credential.needsReconnect())
38067
38085
  await connection.reconnect();
38068
38086
  } catch (error) {
38087
+ if (stopped)
38088
+ return;
38069
38089
  if (error instanceof SessionMovedError) {
38070
38090
  schedule(0);
38071
38091
  return;
@@ -46,8 +46,10 @@ So on a task with no reviewer step and no `acceptance:owner`, such a check holds
46
46
 
47
47
  ## What Legion refuses
48
48
 
49
- `register_plan` answers `409 PLAN_REFUSED` with `reasons: [{task, reason}]`, one reason for each cause, its `task` empty for a cause of the whole plan.
50
- Fix each reason and give a new version.
49
+ `register_plan` answers `409 PLAN_REFUSED` with `reasons: [{task, reason}]`, a reason for each cause, its `task` empty for a cause of the whole plan.
50
+ Past the first reasons of one kind, it counts the rest of that kind in one more reason, of the whole plan.
51
+ Give each task block an id of lowercase letters, digits and hyphens, starting with a letter or a digit, at most 64 bytes: Legion refuses any other id, since Dispatch records no step of such a task.
52
+ Fix each reason and give a new version; the next refusal names what is left.
51
53
  A reason that ends `This release of Legion does not carry it out yet.` names a plan a later release takes: write it again within the table above.
52
54
 
53
55
  An issue whose experiment's group runs the project's default steps takes no plan: give it none, and it runs the default steps.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/claude-envoy",
3
- "version": "6.13.0",
3
+ "version": "6.14.1",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Claude Code plugin for Legion's Envoy bus",
6
6
  "type": "module",
@@ -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; the server's stderr by default. */
73
- readonly warn?: (text: string) => void
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(`the live view could not read this session's model — ${messageFor(error)}`)
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(`the live view could not read this session's commands — ${messageFor(error)}`)
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
- (why) =>
302
- warn(
303
- `this session's relay socket has no token: ${why}; the session starts once a trade succeeds`,
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 stderr,
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
- process.stderr.write(`envoy-channel: ${text}\n`)
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({ identity: id, stateDirectory, why: inbound.off })
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