@sjawhar/claude-envoy 4.20.0 → 4.21.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.
@@ -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": "4.20.0"
5
+ "version": "4.21.0"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-envoy",
3
- "version": "4.20.0",
3
+ "version": "4.21.0",
4
4
  "description": "Put Envoy events into the Claude Code session's own conversation and expose Envoy MCP tools.",
5
5
  "mcpServers": {}
6
6
  }
@@ -15349,6 +15349,67 @@ var DispatchServerStartedPayloadSchema = strictObject({
15349
15349
  commit: string2().min(1),
15350
15350
  started_at: exports_iso.datetime()
15351
15351
  });
15352
+ var EVENT_TYPE_MEMBERS = {
15353
+ "architecture.sync_failed": true,
15354
+ "architecture.synced": true,
15355
+ "artifact.approved": true,
15356
+ "artifact.changes_requested": true,
15357
+ "artifact.created": true,
15358
+ "artifact.human_edited": true,
15359
+ "artifact.removed": true,
15360
+ "artifact.renamed": true,
15361
+ "artifact.version": true,
15362
+ "ask.anchor_refreshed": true,
15363
+ "ask.answered": true,
15364
+ "ask.asked_of": true,
15365
+ "ask.edited": true,
15366
+ "ask.follower_added": true,
15367
+ "ask.follower_removed": true,
15368
+ "ask.handed_back": true,
15369
+ "ask.opened": true,
15370
+ "ask.resolved": true,
15371
+ "block.invalid": true,
15372
+ "block.repaired": true,
15373
+ "child.added": true,
15374
+ "child.removed": true,
15375
+ "child.status": true,
15376
+ "comment.anchor_refreshed": true,
15377
+ "comment.answered": true,
15378
+ "comment.created": true,
15379
+ "comment.delivery": true,
15380
+ "comment.edited": true,
15381
+ "comment.reopened": true,
15382
+ "comment.resolved": true,
15383
+ "decision.kept": true,
15384
+ "handoff.recorded": true,
15385
+ "issue.claimed": true,
15386
+ "issue.closed": true,
15387
+ "issue.created": true,
15388
+ "issue.deployed": true,
15389
+ "issue.pull_request_attribution_changed": true,
15390
+ "issue.released": true,
15391
+ "issue.updated": true,
15392
+ "message.accepted": true,
15393
+ "message.answered": true,
15394
+ "message.created": true,
15395
+ "message.delivery": true,
15396
+ "priority.updated": true,
15397
+ "project.created": true,
15398
+ "project.updated": true,
15399
+ "result.recorded": true,
15400
+ "result.retracted": true,
15401
+ "settings.architecture_source.updated": true,
15402
+ "settings.project_default_assignee.updated": true,
15403
+ "settings.repo_project.updated": true,
15404
+ "subscription.remove_requested": true,
15405
+ "subscription.removed": true,
15406
+ "suggestion.accepted": true,
15407
+ "suggestion.rejected": true,
15408
+ "user_agent_state.updated": true,
15409
+ "user_state.updated": true,
15410
+ "user_thread_state.updated": true
15411
+ };
15412
+ var DISPATCH_EVENT_TYPES = Object.keys(EVENT_TYPE_MEMBERS).sort();
15352
15413
  var DispatchEventSchema = object({
15353
15414
  issue_key: string2().nullable(),
15354
15415
  artifact_id: string2().nullish(),
@@ -29490,6 +29551,24 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
29490
29551
  }
29491
29552
  return new LoggableError(`${source} is broker, but the trade for the session's token failed: ${tradeFailureReason(error)}`);
29492
29553
  };
29554
+ const expiresAt = () => held?.token.expiresAt ?? new Date(0);
29555
+ const needsReconnect = () => presented !== undefined && held !== undefined && presented !== held.token.accessToken;
29556
+ const drop = () => {
29557
+ const dropped = held;
29558
+ held = undefined;
29559
+ if (dropped !== undefined)
29560
+ dropTradedToken(env, dropped);
29561
+ };
29562
+ const tokenGeneration = () => generation;
29563
+ const tokenSession = () => held?.session;
29564
+ const refusals = brokerTokenRefusals({
29565
+ expiresAt,
29566
+ needsReconnect,
29567
+ drop,
29568
+ tokenGeneration,
29569
+ tokenSession
29570
+ });
29571
+ let admissions = { count: 0, dialedAt: undefined };
29493
29572
  return {
29494
29573
  ready: async (options = {}) => {
29495
29574
  if (live())
@@ -29528,16 +29607,16 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
29528
29607
  presented = accessToken;
29529
29608
  return { auth_token: accessToken, sig };
29530
29609
  },
29531
- expiresAt: () => held?.token.expiresAt ?? new Date(0),
29532
- needsReconnect: () => presented !== undefined && held !== undefined && presented !== held.token.accessToken,
29533
- drop: () => {
29534
- const dropped = held;
29535
- held = undefined;
29536
- if (dropped !== undefined)
29537
- dropTradedToken(env, dropped);
29610
+ expiresAt,
29611
+ needsReconnect,
29612
+ tokenGeneration,
29613
+ tokenSession,
29614
+ refused: refusals.refused,
29615
+ admitted: (dialedAt) => {
29616
+ admissions = { count: admissions.count + 1, dialedAt };
29617
+ refusals.reset();
29538
29618
  },
29539
- tokenGeneration: () => generation,
29540
- tokenSession: () => held?.session
29619
+ admissions: () => admissions
29541
29620
  };
29542
29621
  }
29543
29622
  var RENEW_FLOOR_MS = 1000;
@@ -29551,6 +29630,36 @@ var REFUSED_CODES = new Set([
29551
29630
  ]);
29552
29631
  var ERRORED = "error";
29553
29632
  var RECONNECTED = "reconnect";
29633
+ function brokerTokenRefusals(token) {
29634
+ let refusals = 0;
29635
+ let counting = token.tokenGeneration();
29636
+ const droppedFor = new Set;
29637
+ return {
29638
+ refused: (code) => {
29639
+ const live = token.expiresAt().getTime() > Date.now();
29640
+ if (!REFUSED_CODES.has(code) || !live || token.needsReconnect())
29641
+ return;
29642
+ const generation = token.tokenGeneration();
29643
+ if (generation !== counting) {
29644
+ counting = generation;
29645
+ refusals = 0;
29646
+ }
29647
+ refusals += 1;
29648
+ const session = token.tokenSession();
29649
+ const droppedBefore = session !== undefined && droppedFor.has(session);
29650
+ if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || droppedBefore) {
29651
+ return { droppedBefore, dropped: false };
29652
+ }
29653
+ droppedFor.add(session);
29654
+ token.drop();
29655
+ return { droppedBefore, dropped: true };
29656
+ },
29657
+ reset: () => {
29658
+ refusals = 0;
29659
+ droppedFor.clear();
29660
+ }
29661
+ };
29662
+ }
29554
29663
  function warnedEvery(warn) {
29555
29664
  let warnedAt;
29556
29665
  return {
@@ -29646,38 +29755,23 @@ function keepNatsCredential(connection, credential, warn) {
29646
29755
  else
29647
29756
  schedule(0);
29648
29757
  };
29649
- const live = () => credential.expiresAt().getTime() > Date.now();
29650
29758
  (async () => {
29651
- let refusals = 0;
29652
- let counting = credential.tokenGeneration();
29653
- const droppedFor = new Set;
29654
29759
  for await (const status of statuses) {
29655
29760
  if (stopped)
29656
29761
  return;
29657
29762
  if (status.type === RECONNECTED) {
29658
- refusals = 0;
29659
- droppedFor.clear();
29763
+ credential.admitted();
29660
29764
  refusal.reset();
29661
29765
  continue;
29662
29766
  }
29663
29767
  if (status.type !== ERRORED || typeof status.data !== "string")
29664
29768
  continue;
29665
- if (!REFUSED_CODES.has(status.data) || !live() || credential.needsReconnect())
29769
+ const counted = credential.refused(status.data);
29770
+ if (counted === undefined)
29666
29771
  continue;
29667
- const token = credential.tokenGeneration();
29668
- if (token !== counting) {
29669
- counting = token;
29670
- refusals = 0;
29671
- }
29672
- refusals += 1;
29673
- const session = credential.tokenSession();
29674
- const dropped = session !== undefined && droppedFor.has(session);
29675
- refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${dropped ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
29676
- if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || dropped)
29677
- continue;
29678
- droppedFor.add(session);
29679
- credential.drop();
29680
- renewNow();
29772
+ refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${counted.droppedBefore ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
29773
+ if (counted.dropped)
29774
+ renewNow();
29681
29775
  }
29682
29776
  })().catch((error) => {
29683
29777
  if (!stopped)
package/dist/dispatch.js CHANGED
@@ -16646,6 +16646,67 @@ var DispatchServerStartedPayloadSchema = strictObject({
16646
16646
  commit: string2().min(1),
16647
16647
  started_at: exports_iso.datetime()
16648
16648
  });
16649
+ var EVENT_TYPE_MEMBERS = {
16650
+ "architecture.sync_failed": true,
16651
+ "architecture.synced": true,
16652
+ "artifact.approved": true,
16653
+ "artifact.changes_requested": true,
16654
+ "artifact.created": true,
16655
+ "artifact.human_edited": true,
16656
+ "artifact.removed": true,
16657
+ "artifact.renamed": true,
16658
+ "artifact.version": true,
16659
+ "ask.anchor_refreshed": true,
16660
+ "ask.answered": true,
16661
+ "ask.asked_of": true,
16662
+ "ask.edited": true,
16663
+ "ask.follower_added": true,
16664
+ "ask.follower_removed": true,
16665
+ "ask.handed_back": true,
16666
+ "ask.opened": true,
16667
+ "ask.resolved": true,
16668
+ "block.invalid": true,
16669
+ "block.repaired": true,
16670
+ "child.added": true,
16671
+ "child.removed": true,
16672
+ "child.status": true,
16673
+ "comment.anchor_refreshed": true,
16674
+ "comment.answered": true,
16675
+ "comment.created": true,
16676
+ "comment.delivery": true,
16677
+ "comment.edited": true,
16678
+ "comment.reopened": true,
16679
+ "comment.resolved": true,
16680
+ "decision.kept": true,
16681
+ "handoff.recorded": true,
16682
+ "issue.claimed": true,
16683
+ "issue.closed": true,
16684
+ "issue.created": true,
16685
+ "issue.deployed": true,
16686
+ "issue.pull_request_attribution_changed": true,
16687
+ "issue.released": true,
16688
+ "issue.updated": true,
16689
+ "message.accepted": true,
16690
+ "message.answered": true,
16691
+ "message.created": true,
16692
+ "message.delivery": true,
16693
+ "priority.updated": true,
16694
+ "project.created": true,
16695
+ "project.updated": true,
16696
+ "result.recorded": true,
16697
+ "result.retracted": true,
16698
+ "settings.architecture_source.updated": true,
16699
+ "settings.project_default_assignee.updated": true,
16700
+ "settings.repo_project.updated": true,
16701
+ "subscription.remove_requested": true,
16702
+ "subscription.removed": true,
16703
+ "suggestion.accepted": true,
16704
+ "suggestion.rejected": true,
16705
+ "user_agent_state.updated": true,
16706
+ "user_state.updated": true,
16707
+ "user_thread_state.updated": true
16708
+ };
16709
+ var DISPATCH_EVENT_TYPES = Object.keys(EVENT_TYPE_MEMBERS).sort();
16649
16710
  var DispatchEventSchema = object({
16650
16711
  issue_key: string2().nullable(),
16651
16712
  artifact_id: string2().nullish(),
@@ -30741,6 +30802,24 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
30741
30802
  }
30742
30803
  return new LoggableError(`${source} is broker, but the trade for the session's token failed: ${tradeFailureReason(error)}`);
30743
30804
  };
30805
+ const expiresAt = () => held?.token.expiresAt ?? new Date(0);
30806
+ const needsReconnect = () => presented !== undefined && held !== undefined && presented !== held.token.accessToken;
30807
+ const drop = () => {
30808
+ const dropped = held;
30809
+ held = undefined;
30810
+ if (dropped !== undefined)
30811
+ dropTradedToken(env, dropped);
30812
+ };
30813
+ const tokenGeneration = () => generation;
30814
+ const tokenSession = () => held?.session;
30815
+ const refusals = brokerTokenRefusals({
30816
+ expiresAt,
30817
+ needsReconnect,
30818
+ drop,
30819
+ tokenGeneration,
30820
+ tokenSession
30821
+ });
30822
+ let admissions = { count: 0, dialedAt: undefined };
30744
30823
  return {
30745
30824
  ready: async (options = {}) => {
30746
30825
  if (live())
@@ -30779,16 +30858,16 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
30779
30858
  presented = accessToken;
30780
30859
  return { auth_token: accessToken, sig };
30781
30860
  },
30782
- expiresAt: () => held?.token.expiresAt ?? new Date(0),
30783
- needsReconnect: () => presented !== undefined && held !== undefined && presented !== held.token.accessToken,
30784
- drop: () => {
30785
- const dropped = held;
30786
- held = undefined;
30787
- if (dropped !== undefined)
30788
- dropTradedToken(env, dropped);
30861
+ expiresAt,
30862
+ needsReconnect,
30863
+ tokenGeneration,
30864
+ tokenSession,
30865
+ refused: refusals.refused,
30866
+ admitted: (dialedAt) => {
30867
+ admissions = { count: admissions.count + 1, dialedAt };
30868
+ refusals.reset();
30789
30869
  },
30790
- tokenGeneration: () => generation,
30791
- tokenSession: () => held?.session
30870
+ admissions: () => admissions
30792
30871
  };
30793
30872
  }
30794
30873
  var RENEW_FLOOR_MS = 1000;
@@ -30802,6 +30881,36 @@ var REFUSED_CODES = new Set([
30802
30881
  ]);
30803
30882
  var ERRORED = "error";
30804
30883
  var RECONNECTED = "reconnect";
30884
+ function brokerTokenRefusals(token) {
30885
+ let refusals = 0;
30886
+ let counting = token.tokenGeneration();
30887
+ const droppedFor = new Set;
30888
+ return {
30889
+ refused: (code) => {
30890
+ const live = token.expiresAt().getTime() > Date.now();
30891
+ if (!REFUSED_CODES.has(code) || !live || token.needsReconnect())
30892
+ return;
30893
+ const generation = token.tokenGeneration();
30894
+ if (generation !== counting) {
30895
+ counting = generation;
30896
+ refusals = 0;
30897
+ }
30898
+ refusals += 1;
30899
+ const session = token.tokenSession();
30900
+ const droppedBefore = session !== undefined && droppedFor.has(session);
30901
+ if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || droppedBefore) {
30902
+ return { droppedBefore, dropped: false };
30903
+ }
30904
+ droppedFor.add(session);
30905
+ token.drop();
30906
+ return { droppedBefore, dropped: true };
30907
+ },
30908
+ reset: () => {
30909
+ refusals = 0;
30910
+ droppedFor.clear();
30911
+ }
30912
+ };
30913
+ }
30805
30914
  function warnedEvery(warn) {
30806
30915
  let warnedAt;
30807
30916
  return {
@@ -30897,38 +31006,23 @@ function keepNatsCredential(connection, credential, warn) {
30897
31006
  else
30898
31007
  schedule(0);
30899
31008
  };
30900
- const live = () => credential.expiresAt().getTime() > Date.now();
30901
31009
  (async () => {
30902
- let refusals = 0;
30903
- let counting = credential.tokenGeneration();
30904
- const droppedFor = new Set;
30905
31010
  for await (const status of statuses) {
30906
31011
  if (stopped)
30907
31012
  return;
30908
31013
  if (status.type === RECONNECTED) {
30909
- refusals = 0;
30910
- droppedFor.clear();
31014
+ credential.admitted();
30911
31015
  refusal.reset();
30912
31016
  continue;
30913
31017
  }
30914
31018
  if (status.type !== ERRORED || typeof status.data !== "string")
30915
31019
  continue;
30916
- if (!REFUSED_CODES.has(status.data) || !live() || credential.needsReconnect())
31020
+ const counted = credential.refused(status.data);
31021
+ if (counted === undefined)
30917
31022
  continue;
30918
- const token = credential.tokenGeneration();
30919
- if (token !== counting) {
30920
- counting = token;
30921
- refusals = 0;
30922
- }
30923
- refusals += 1;
30924
- const session = credential.tokenSession();
30925
- const dropped = session !== undefined && droppedFor.has(session);
30926
- refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${dropped ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
30927
- if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || dropped)
30928
- continue;
30929
- droppedFor.add(session);
30930
- credential.drop();
30931
- renewNow();
31023
+ refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${counted.droppedBefore ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
31024
+ if (counted.dropped)
31025
+ renewNow();
30932
31026
  }
30933
31027
  })().catch((error) => {
30934
31028
  if (!stopped)
@@ -38149,6 +38149,67 @@ var DispatchServerStartedPayloadSchema = strictObject({
38149
38149
  commit: string2().min(1),
38150
38150
  started_at: exports_iso.datetime()
38151
38151
  });
38152
+ var EVENT_TYPE_MEMBERS = {
38153
+ "architecture.sync_failed": true,
38154
+ "architecture.synced": true,
38155
+ "artifact.approved": true,
38156
+ "artifact.changes_requested": true,
38157
+ "artifact.created": true,
38158
+ "artifact.human_edited": true,
38159
+ "artifact.removed": true,
38160
+ "artifact.renamed": true,
38161
+ "artifact.version": true,
38162
+ "ask.anchor_refreshed": true,
38163
+ "ask.answered": true,
38164
+ "ask.asked_of": true,
38165
+ "ask.edited": true,
38166
+ "ask.follower_added": true,
38167
+ "ask.follower_removed": true,
38168
+ "ask.handed_back": true,
38169
+ "ask.opened": true,
38170
+ "ask.resolved": true,
38171
+ "block.invalid": true,
38172
+ "block.repaired": true,
38173
+ "child.added": true,
38174
+ "child.removed": true,
38175
+ "child.status": true,
38176
+ "comment.anchor_refreshed": true,
38177
+ "comment.answered": true,
38178
+ "comment.created": true,
38179
+ "comment.delivery": true,
38180
+ "comment.edited": true,
38181
+ "comment.reopened": true,
38182
+ "comment.resolved": true,
38183
+ "decision.kept": true,
38184
+ "handoff.recorded": true,
38185
+ "issue.claimed": true,
38186
+ "issue.closed": true,
38187
+ "issue.created": true,
38188
+ "issue.deployed": true,
38189
+ "issue.pull_request_attribution_changed": true,
38190
+ "issue.released": true,
38191
+ "issue.updated": true,
38192
+ "message.accepted": true,
38193
+ "message.answered": true,
38194
+ "message.created": true,
38195
+ "message.delivery": true,
38196
+ "priority.updated": true,
38197
+ "project.created": true,
38198
+ "project.updated": true,
38199
+ "result.recorded": true,
38200
+ "result.retracted": true,
38201
+ "settings.architecture_source.updated": true,
38202
+ "settings.project_default_assignee.updated": true,
38203
+ "settings.repo_project.updated": true,
38204
+ "subscription.remove_requested": true,
38205
+ "subscription.removed": true,
38206
+ "suggestion.accepted": true,
38207
+ "suggestion.rejected": true,
38208
+ "user_agent_state.updated": true,
38209
+ "user_state.updated": true,
38210
+ "user_thread_state.updated": true
38211
+ };
38212
+ var DISPATCH_EVENT_TYPES = Object.keys(EVENT_TYPE_MEMBERS).sort();
38152
38213
  var DispatchEventSchema = object({
38153
38214
  issue_key: string2().nullable(),
38154
38215
  artifact_id: string2().nullish(),
@@ -52650,6 +52711,24 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
52650
52711
  }
52651
52712
  return new LoggableError(`${source} is broker, but the trade for the session's token failed: ${tradeFailureReason(error)}`);
52652
52713
  };
52714
+ const expiresAt = () => held?.token.expiresAt ?? new Date(0);
52715
+ const needsReconnect = () => presented !== undefined && held !== undefined && presented !== held.token.accessToken;
52716
+ const drop = () => {
52717
+ const dropped = held;
52718
+ held = undefined;
52719
+ if (dropped !== undefined)
52720
+ dropTradedToken(env, dropped);
52721
+ };
52722
+ const tokenGeneration = () => generation;
52723
+ const tokenSession = () => held?.session;
52724
+ const refusals = brokerTokenRefusals({
52725
+ expiresAt,
52726
+ needsReconnect,
52727
+ drop,
52728
+ tokenGeneration,
52729
+ tokenSession
52730
+ });
52731
+ let admissions = { count: 0, dialedAt: undefined };
52653
52732
  return {
52654
52733
  ready: async (options = {}) => {
52655
52734
  if (live())
@@ -52688,16 +52767,16 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
52688
52767
  presented = accessToken;
52689
52768
  return { auth_token: accessToken, sig };
52690
52769
  },
52691
- expiresAt: () => held?.token.expiresAt ?? new Date(0),
52692
- needsReconnect: () => presented !== undefined && held !== undefined && presented !== held.token.accessToken,
52693
- drop: () => {
52694
- const dropped = held;
52695
- held = undefined;
52696
- if (dropped !== undefined)
52697
- dropTradedToken(env, dropped);
52770
+ expiresAt,
52771
+ needsReconnect,
52772
+ tokenGeneration,
52773
+ tokenSession,
52774
+ refused: refusals.refused,
52775
+ admitted: (dialedAt) => {
52776
+ admissions = { count: admissions.count + 1, dialedAt };
52777
+ refusals.reset();
52698
52778
  },
52699
- tokenGeneration: () => generation,
52700
- tokenSession: () => held?.session
52779
+ admissions: () => admissions
52701
52780
  };
52702
52781
  }
52703
52782
  var RENEW_FLOOR_MS = 1000;
@@ -52711,6 +52790,36 @@ var REFUSED_CODES = new Set([
52711
52790
  ]);
52712
52791
  var ERRORED = "error";
52713
52792
  var RECONNECTED = "reconnect";
52793
+ function brokerTokenRefusals(token) {
52794
+ let refusals = 0;
52795
+ let counting = token.tokenGeneration();
52796
+ const droppedFor = new Set;
52797
+ return {
52798
+ refused: (code) => {
52799
+ const live = token.expiresAt().getTime() > Date.now();
52800
+ if (!REFUSED_CODES.has(code) || !live || token.needsReconnect())
52801
+ return;
52802
+ const generation = token.tokenGeneration();
52803
+ if (generation !== counting) {
52804
+ counting = generation;
52805
+ refusals = 0;
52806
+ }
52807
+ refusals += 1;
52808
+ const session = token.tokenSession();
52809
+ const droppedBefore = session !== undefined && droppedFor.has(session);
52810
+ if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || droppedBefore) {
52811
+ return { droppedBefore, dropped: false };
52812
+ }
52813
+ droppedFor.add(session);
52814
+ token.drop();
52815
+ return { droppedBefore, dropped: true };
52816
+ },
52817
+ reset: () => {
52818
+ refusals = 0;
52819
+ droppedFor.clear();
52820
+ }
52821
+ };
52822
+ }
52714
52823
  function warnedEvery(warn) {
52715
52824
  let warnedAt;
52716
52825
  return {
@@ -52806,38 +52915,23 @@ function keepNatsCredential(connection, credential, warn) {
52806
52915
  else
52807
52916
  schedule(0);
52808
52917
  };
52809
- const live = () => credential.expiresAt().getTime() > Date.now();
52810
52918
  (async () => {
52811
- let refusals = 0;
52812
- let counting = credential.tokenGeneration();
52813
- const droppedFor = new Set;
52814
52919
  for await (const status of statuses) {
52815
52920
  if (stopped)
52816
52921
  return;
52817
52922
  if (status.type === RECONNECTED) {
52818
- refusals = 0;
52819
- droppedFor.clear();
52923
+ credential.admitted();
52820
52924
  refusal.reset();
52821
52925
  continue;
52822
52926
  }
52823
52927
  if (status.type !== ERRORED || typeof status.data !== "string")
52824
52928
  continue;
52825
- if (!REFUSED_CODES.has(status.data) || !live() || credential.needsReconnect())
52826
- continue;
52827
- const token = credential.tokenGeneration();
52828
- if (token !== counting) {
52829
- counting = token;
52830
- refusals = 0;
52831
- }
52832
- refusals += 1;
52833
- const session = credential.tokenSession();
52834
- const dropped = session !== undefined && droppedFor.has(session);
52835
- refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${dropped ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
52836
- if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || dropped)
52929
+ const counted = credential.refused(status.data);
52930
+ if (counted === undefined)
52837
52931
  continue;
52838
- droppedFor.add(session);
52839
- credential.drop();
52840
- renewNow();
52932
+ refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${counted.droppedBefore ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
52933
+ if (counted.dropped)
52934
+ renewNow();
52841
52935
  }
52842
52936
  })().catch((error) => {
52843
52937
  if (!stopped)
@@ -56136,10 +56230,72 @@ function natsConnectionOptions(input) {
56136
56230
  timeout: NATS_DIAL_TIMEOUT_MS,
56137
56231
  reconnect: true,
56138
56232
  maxReconnectAttempts: -1,
56139
- reconnectTimeWait: 2000,
56140
56233
  pingInterval: NATS_PING_INTERVAL_MS,
56141
56234
  maxPingOut: NATS_MAX_PINGS_OUT,
56142
- ...input.credential.kind === "broker" ? { ignoreAuthErrorAbort: true } : {}
56235
+ ...input.credential.kind === "broker" ? {
56236
+ ignoreAuthErrorAbort: true,
56237
+ reconnectDelayHandler: brokerRedialDelays(input.credential.broker)
56238
+ } : { reconnectTimeWait: 2000 }
56239
+ };
56240
+ }
56241
+ var BROKER_REDIAL_FIRST_MS = 4000;
56242
+ var BROKER_REDIAL_CAP_MS = 20000;
56243
+ var BROKER_REDIAL_OUTAGE_GAP_MS = 60000;
56244
+ var BROKER_REDIAL_POLL_MS = 1000;
56245
+ var DUE_SLACK_MS = 50;
56246
+ function brokerRedialWaitBounds(wait) {
56247
+ const highestMs = Math.min(BROKER_REDIAL_CAP_MS, BROKER_REDIAL_FIRST_MS * 2 ** Math.min(wait - 1, 30));
56248
+ return { lowestMs: highestMs / 2, highestMs };
56249
+ }
56250
+ function brokerRedialWaitMs(wait, random) {
56251
+ const { lowestMs, highestMs } = brokerRedialWaitBounds(wait);
56252
+ return Math.round(lowestMs + random() * (highestMs - lowestMs));
56253
+ }
56254
+ function brokerRedialDelays(credential, random = Math.random, now = Date.now, pollMs = BROKER_REDIAL_POLL_MS) {
56255
+ let lastCall = Number.NEGATIVE_INFINITY;
56256
+ let dialedAt = 0;
56257
+ let dueAt = Number.NEGATIVE_INFINITY;
56258
+ let waits = 0;
56259
+ let rushed;
56260
+ let dialedSession;
56261
+ let rushedInWait = false;
56262
+ let admissions = credential.admissions().count;
56263
+ return () => {
56264
+ const at = now();
56265
+ if (at - lastCall > BROKER_REDIAL_OUTAGE_GAP_MS) {
56266
+ dueAt = Number.NEGATIVE_INFINITY;
56267
+ waits = 0;
56268
+ }
56269
+ lastCall = at;
56270
+ const admitted = credential.admissions();
56271
+ if (admitted.count !== admissions) {
56272
+ admissions = admitted.count;
56273
+ rushedInWait = false;
56274
+ if (admitted.dialedAt !== undefined && admitted.dialedAt > dialedAt) {
56275
+ dialedAt = admitted.dialedAt;
56276
+ }
56277
+ }
56278
+ const unpresented = credential.needsReconnect() ? credential.tokenGeneration() : undefined;
56279
+ const session = credential.tokenSession();
56280
+ if (at < dueAt - DUE_SLACK_MS) {
56281
+ const renewal = session === dialedSession;
56282
+ if (unpresented === undefined || unpresented === rushed || rushedInWait && renewal) {
56283
+ return Math.min(dueAt, at + pollMs) - dialedAt;
56284
+ }
56285
+ rushed = unpresented;
56286
+ rushedInWait = true;
56287
+ dialedSession = session;
56288
+ dialedAt = at;
56289
+ return 0;
56290
+ }
56291
+ if (unpresented !== undefined)
56292
+ rushed = unpresented;
56293
+ dialedSession = session;
56294
+ rushedInWait = false;
56295
+ waits += 1;
56296
+ dialedAt = at;
56297
+ dueAt = at + brokerRedialWaitMs(waits, random);
56298
+ return 0;
56143
56299
  };
56144
56300
  }
56145
56301
  function natsServersForLog(setting, count) {
@@ -56154,6 +56310,7 @@ var NATS_DIAL_PLAN = {
56154
56310
  maxDelayMs: 4000,
56155
56311
  budgetMs: 25000
56156
56312
  };
56313
+ var NATS_DIAL_TOKEN_DROPPED = "the hub refused the session's broker token twice, so the token was dropped and another trade started";
56157
56314
  var NATS_MIN_REDIAL_BOUND_MS = 500;
56158
56315
 
56159
56316
  class NatsDialBoundError extends LoggableError {
@@ -56175,18 +56332,41 @@ var NEVER_CURED = {
56175
56332
  function redialCures(error, options) {
56176
56333
  if (error instanceof NatsDialBoundError)
56177
56334
  return true;
56178
- if (!(error instanceof Error) || !("code" in error) || typeof error.code !== "string")
56335
+ const code = errorCode(error);
56336
+ if (code === undefined)
56179
56337
  return false;
56180
- const { code } = error;
56181
56338
  if (AUTHORIZATION_REFUSALS[code] === true)
56182
56339
  return options.ignoreAuthErrorAbort === true;
56183
56340
  return NEVER_CURED[code] !== true && !code.startsWith("ERR_");
56184
56341
  }
56342
+ function errorCode(error) {
56343
+ return error instanceof Error && "code" in error && typeof error.code === "string" ? error.code : undefined;
56344
+ }
56345
+ function brokerDialRefusals(credential, random) {
56346
+ if (credential?.kind !== "broker")
56347
+ return;
56348
+ const { broker } = credential;
56349
+ let refusals = 0;
56350
+ return {
56351
+ refused: (code) => {
56352
+ if (code === undefined || AUTHORIZATION_REFUSALS[code] !== true)
56353
+ return;
56354
+ refusals += 1;
56355
+ const dropped = broker.refused(code)?.dropped === true;
56356
+ if (dropped)
56357
+ broker.renew(AbortSignal.timeout(TRADE_DEADLINE_MS)).catch(() => {});
56358
+ return { waitMs: brokerRedialWaitMs(refusals, random), dropped };
56359
+ },
56360
+ admitted: (dialedAt) => broker.admitted(dialedAt)
56361
+ };
56362
+ }
56185
56363
  async function connectNats(connect, options, plan, hooks = {}) {
56186
56364
  const started = performance.now();
56187
56365
  const elapsed = () => performance.now() - started;
56188
56366
  const timeout = options.timeout ?? NATS_DIAL_TIMEOUT_MS;
56189
56367
  const servers = Array.isArray(options.servers) ? Math.max(1, options.servers.length) : 1;
56368
+ const brokerRefusals = brokerDialRefusals(hooks.credential, hooks.random ?? Math.random);
56369
+ const wait = hooks.delay ?? delay;
56190
56370
  let delayMs = plan.firstDelayMs;
56191
56371
  let failed;
56192
56372
  for (let attempt = 1;; attempt += 1) {
@@ -56194,24 +56374,30 @@ async function connectNats(connect, options, plan, hooks = {}) {
56194
56374
  throw failed ?? new LoggableError("the NATS dial was stopped before it began");
56195
56375
  }
56196
56376
  const bound = Math.max(1, Math.round(Math.min(timeout, attempt === 1 ? hooks.firstBoundMs ?? timeout : plan.budgetMs - elapsed())));
56377
+ const dialedAt = Date.now();
56197
56378
  try {
56198
56379
  const nc = await boundedDial(connect, {
56199
56380
  ...options,
56200
56381
  timeout: Math.min(bound, Math.max(NATS_MIN_REDIAL_BOUND_MS, Math.floor(bound / servers)))
56201
56382
  }, bound);
56202
56383
  keepWholeTimeout(nc, timeout);
56384
+ brokerRefusals?.admitted(dialedAt);
56203
56385
  return nc;
56204
56386
  } catch (error) {
56205
56387
  failed = error;
56206
- const waitEnds = elapsed() + delayMs;
56207
- if (!redialCures(error, options) || waitEnds + NATS_MIN_REDIAL_BOUND_MS > plan.budgetMs || hooks.stopped?.() === true) {
56388
+ const refusal = brokerRefusals?.refused(errorCode(error));
56389
+ if (refusal?.dropped === true)
56390
+ hooks.onTokenDropped?.(error, attempt);
56391
+ const waitMs = refusal?.waitMs ?? delayMs;
56392
+ if (!redialCures(error, options) || elapsed() + waitMs + NATS_MIN_REDIAL_BOUND_MS > plan.budgetMs || hooks.stopped?.() === true) {
56208
56393
  throw error;
56209
56394
  }
56210
- hooks.onRetry?.(error, delayMs, attempt);
56211
- await delay(delayMs);
56395
+ hooks.onRetry?.(error, waitMs, attempt);
56396
+ await wait(waitMs);
56212
56397
  if (plan.budgetMs - elapsed() < NATS_MIN_REDIAL_BOUND_MS)
56213
56398
  throw error;
56214
- delayMs = Math.min(delayMs * 2, plan.maxDelayMs);
56399
+ if (refusal === undefined)
56400
+ delayMs = Math.min(delayMs * 2, plan.maxDelayMs);
56215
56401
  }
56216
56402
  }
56217
56403
  }
@@ -56286,7 +56472,7 @@ var CHANNEL_DIAL_PLAN = {
56286
56472
  ...NATS_DIAL_PLAN,
56287
56473
  budgetMs: Number.POSITIVE_INFINITY
56288
56474
  };
56289
- async function dialChannelNats(inbound, name, warn, dial = import_nats4.connect, signal, plan = CHANNEL_DIAL_PLAN) {
56475
+ async function dialChannelNats(inbound, name, warn, dial = import_nats4.connect, signal, plan = CHANNEL_DIAL_PLAN, waits = {}) {
56290
56476
  const stopped = Promise.withResolvers();
56291
56477
  const onAbort = () => stopped.resolve("stopping");
56292
56478
  if (signal?.aborted === true)
@@ -56309,7 +56495,10 @@ async function dialChannelNats(inbound, name, warn, dial = import_nats4.connect,
56309
56495
  const broker = natsCredential.kind === "broker" ? natsCredential.broker : undefined;
56310
56496
  await unlessStopped(broker?.ready({ life: signal ?? new AbortController().signal }) ?? Promise.resolve());
56311
56497
  const dialing = connectNats(dial, natsConnectionOptions({ servers: inbound.natsUrls, name, credential: natsCredential }), plan, {
56498
+ ...waits,
56312
56499
  stopped: () => signal?.aborted === true,
56500
+ credential: natsCredential,
56501
+ onTokenDropped: (error, attempt) => warn(`${NATS_DIAL_TOKEN_DROPPED} (dial ${attempt})`, error),
56313
56502
  onRetry: (error, delayMs) => warn(`${failed(error)}; dialing again in ${delayMs} ms`, error)
56314
56503
  });
56315
56504
  let connection;
@@ -57836,7 +58025,7 @@ var import_nats5 = __toESM(require_mod4(), 1);
57836
58025
 
57837
58026
  // package.json
57838
58027
  var name2 = "@sjawhar/claude-envoy";
57839
- var version2 = "4.20.0";
58028
+ var version2 = "4.21.0";
57840
58029
  var legion = {
57841
58030
  daemonApiVersion: 202
57842
58031
  };
@@ -15553,6 +15553,67 @@ var DispatchServerStartedPayloadSchema = strictObject({
15553
15553
  commit: string2().min(1),
15554
15554
  started_at: exports_iso.datetime()
15555
15555
  });
15556
+ var EVENT_TYPE_MEMBERS = {
15557
+ "architecture.sync_failed": true,
15558
+ "architecture.synced": true,
15559
+ "artifact.approved": true,
15560
+ "artifact.changes_requested": true,
15561
+ "artifact.created": true,
15562
+ "artifact.human_edited": true,
15563
+ "artifact.removed": true,
15564
+ "artifact.renamed": true,
15565
+ "artifact.version": true,
15566
+ "ask.anchor_refreshed": true,
15567
+ "ask.answered": true,
15568
+ "ask.asked_of": true,
15569
+ "ask.edited": true,
15570
+ "ask.follower_added": true,
15571
+ "ask.follower_removed": true,
15572
+ "ask.handed_back": true,
15573
+ "ask.opened": true,
15574
+ "ask.resolved": true,
15575
+ "block.invalid": true,
15576
+ "block.repaired": true,
15577
+ "child.added": true,
15578
+ "child.removed": true,
15579
+ "child.status": true,
15580
+ "comment.anchor_refreshed": true,
15581
+ "comment.answered": true,
15582
+ "comment.created": true,
15583
+ "comment.delivery": true,
15584
+ "comment.edited": true,
15585
+ "comment.reopened": true,
15586
+ "comment.resolved": true,
15587
+ "decision.kept": true,
15588
+ "handoff.recorded": true,
15589
+ "issue.claimed": true,
15590
+ "issue.closed": true,
15591
+ "issue.created": true,
15592
+ "issue.deployed": true,
15593
+ "issue.pull_request_attribution_changed": true,
15594
+ "issue.released": true,
15595
+ "issue.updated": true,
15596
+ "message.accepted": true,
15597
+ "message.answered": true,
15598
+ "message.created": true,
15599
+ "message.delivery": true,
15600
+ "priority.updated": true,
15601
+ "project.created": true,
15602
+ "project.updated": true,
15603
+ "result.recorded": true,
15604
+ "result.retracted": true,
15605
+ "settings.architecture_source.updated": true,
15606
+ "settings.project_default_assignee.updated": true,
15607
+ "settings.repo_project.updated": true,
15608
+ "subscription.remove_requested": true,
15609
+ "subscription.removed": true,
15610
+ "suggestion.accepted": true,
15611
+ "suggestion.rejected": true,
15612
+ "user_agent_state.updated": true,
15613
+ "user_state.updated": true,
15614
+ "user_thread_state.updated": true
15615
+ };
15616
+ var DISPATCH_EVENT_TYPES = Object.keys(EVENT_TYPE_MEMBERS).sort();
15556
15617
  var DispatchEventSchema = object({
15557
15618
  issue_key: string2().nullable(),
15558
15619
  artifact_id: string2().nullish(),
@@ -29516,6 +29577,24 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
29516
29577
  }
29517
29578
  return new LoggableError(`${source} is broker, but the trade for the session's token failed: ${tradeFailureReason(error)}`);
29518
29579
  };
29580
+ const expiresAt = () => held?.token.expiresAt ?? new Date(0);
29581
+ const needsReconnect = () => presented !== undefined && held !== undefined && presented !== held.token.accessToken;
29582
+ const drop = () => {
29583
+ const dropped = held;
29584
+ held = undefined;
29585
+ if (dropped !== undefined)
29586
+ dropTradedToken(env, dropped);
29587
+ };
29588
+ const tokenGeneration = () => generation;
29589
+ const tokenSession = () => held?.session;
29590
+ const refusals = brokerTokenRefusals({
29591
+ expiresAt,
29592
+ needsReconnect,
29593
+ drop,
29594
+ tokenGeneration,
29595
+ tokenSession
29596
+ });
29597
+ let admissions = { count: 0, dialedAt: undefined };
29519
29598
  return {
29520
29599
  ready: async (options = {}) => {
29521
29600
  if (live())
@@ -29554,16 +29633,16 @@ function brokerNatsCredential(env, session, source = NATS_AUTH_VAR, tradeDeadlin
29554
29633
  presented = accessToken;
29555
29634
  return { auth_token: accessToken, sig };
29556
29635
  },
29557
- expiresAt: () => held?.token.expiresAt ?? new Date(0),
29558
- needsReconnect: () => presented !== undefined && held !== undefined && presented !== held.token.accessToken,
29559
- drop: () => {
29560
- const dropped = held;
29561
- held = undefined;
29562
- if (dropped !== undefined)
29563
- dropTradedToken(env, dropped);
29636
+ expiresAt,
29637
+ needsReconnect,
29638
+ tokenGeneration,
29639
+ tokenSession,
29640
+ refused: refusals.refused,
29641
+ admitted: (dialedAt) => {
29642
+ admissions = { count: admissions.count + 1, dialedAt };
29643
+ refusals.reset();
29564
29644
  },
29565
- tokenGeneration: () => generation,
29566
- tokenSession: () => held?.session
29645
+ admissions: () => admissions
29567
29646
  };
29568
29647
  }
29569
29648
  var RENEW_FLOOR_MS = 1000;
@@ -29577,6 +29656,36 @@ var REFUSED_CODES = new Set([
29577
29656
  ]);
29578
29657
  var ERRORED = "error";
29579
29658
  var RECONNECTED = "reconnect";
29659
+ function brokerTokenRefusals(token) {
29660
+ let refusals = 0;
29661
+ let counting = token.tokenGeneration();
29662
+ const droppedFor = new Set;
29663
+ return {
29664
+ refused: (code) => {
29665
+ const live = token.expiresAt().getTime() > Date.now();
29666
+ if (!REFUSED_CODES.has(code) || !live || token.needsReconnect())
29667
+ return;
29668
+ const generation = token.tokenGeneration();
29669
+ if (generation !== counting) {
29670
+ counting = generation;
29671
+ refusals = 0;
29672
+ }
29673
+ refusals += 1;
29674
+ const session = token.tokenSession();
29675
+ const droppedBefore = session !== undefined && droppedFor.has(session);
29676
+ if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || droppedBefore) {
29677
+ return { droppedBefore, dropped: false };
29678
+ }
29679
+ droppedFor.add(session);
29680
+ token.drop();
29681
+ return { droppedBefore, dropped: true };
29682
+ },
29683
+ reset: () => {
29684
+ refusals = 0;
29685
+ droppedFor.clear();
29686
+ }
29687
+ };
29688
+ }
29580
29689
  function warnedEvery(warn) {
29581
29690
  let warnedAt;
29582
29691
  return {
@@ -29672,38 +29781,23 @@ function keepNatsCredential(connection, credential, warn) {
29672
29781
  else
29673
29782
  schedule(0);
29674
29783
  };
29675
- const live = () => credential.expiresAt().getTime() > Date.now();
29676
29784
  (async () => {
29677
- let refusals = 0;
29678
- let counting = credential.tokenGeneration();
29679
- const droppedFor = new Set;
29680
29785
  for await (const status of statuses) {
29681
29786
  if (stopped)
29682
29787
  return;
29683
29788
  if (status.type === RECONNECTED) {
29684
- refusals = 0;
29685
- droppedFor.clear();
29789
+ credential.admitted();
29686
29790
  refusal.reset();
29687
29791
  continue;
29688
29792
  }
29689
29793
  if (status.type !== ERRORED || typeof status.data !== "string")
29690
29794
  continue;
29691
- if (!REFUSED_CODES.has(status.data) || !live() || credential.needsReconnect())
29795
+ const counted = credential.refused(status.data);
29796
+ if (counted === undefined)
29692
29797
  continue;
29693
- const token = credential.tokenGeneration();
29694
- if (token !== counting) {
29695
- counting = token;
29696
- refusals = 0;
29697
- }
29698
- refusals += 1;
29699
- const session = credential.tokenSession();
29700
- const dropped = session !== undefined && droppedFor.has(session);
29701
- refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${dropped ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
29702
- if (refusals !== REFUSALS_BEFORE_DROP || session === undefined || dropped)
29703
- continue;
29704
- droppedFor.add(session);
29705
- credential.drop();
29706
- renewNow();
29798
+ refusal.warn(`the NATS server refused the connection's live broker token (${status.data}): the auth service is away, or the token was revoked; the connection keeps redialling, and ${counted.droppedBefore ? "this session's token was dropped and another traded once already, so none is dropped again until the connection is back" : "at the second refusal the token is dropped and another traded"}`);
29799
+ if (counted.dropped)
29800
+ renewNow();
29707
29801
  }
29708
29802
  })().catch((error) => {
29709
29803
  if (!stopped)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/claude-envoy",
3
- "version": "4.20.0",
3
+ "version": "4.21.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Claude Code plugin for Legion's Envoy bus",
6
6
  "type": "module",
@@ -8,6 +8,8 @@ import { LoggableError } from "@legion/envoy-client/errors"
8
8
  import {
9
9
  connectNats,
10
10
  NATS_DIAL_PLAN,
11
+ NATS_DIAL_TOKEN_DROPPED,
12
+ type NatsDialHooks,
11
13
  type NatsDialPlan,
12
14
  natsConnectionOptions,
13
15
  natsDialFailure,
@@ -124,16 +126,19 @@ export interface ChannelNatsSettings {
124
126
  * signed into the CONNECT itself, so a live one is taken before the dial; and the connection is
125
127
  * then kept on a token the auth service admits (`keepNatsCredential`, warnings through `warn`,
126
128
  * with no dial error, which tells them from a redial's), until `stop` or the connection closing.
127
- * A dial that fails is dialed again as `plan` says, which by default is for the server's life;
129
+ * A dial that fails is dialed again as `plan` says, which by default is for the server's life, and
130
+ * a hub's refusal of the session's broker token on the broker-token backoff (`connectNats`);
128
131
  * each redial is warned of, naming the servers by their setting and the credential it presented
129
132
  * beside why it failed, as a log may print both
130
133
  * (`natsDialFailure`: no part of a server's text, and the reason nats.js's code or the server's
131
134
  * fixed refusal, never text derived from what the servers were set to or what the client sent),
132
- * and `warn` is handed the failed dial's error with its line. A dial that fails for good (an
133
- * authorization refusal, or the end of a finite `plan`) throws the same line, as a
134
- * `LoggableError`; the error keeps no cause: nats.js's own error still carries them. A token that
135
- * cannot be taken fails with the credential's own line (`BrokerNatsCredential.ready`). `dial` is
136
- * nats.js's `connect`, or a test's.
135
+ * and `warn` is handed the failed dial's error with its line; a dial whose refusal dropped the
136
+ * session's broker token is warned of at the drop (`NATS_DIAL_TOKEN_DROPPED`). A dial that fails
137
+ * for good (an authorization refusal of a seed or of no credential, or the end of a finite `plan`)
138
+ * throws the same line, as a `LoggableError`; the error keeps no cause: nats.js's own error still
139
+ * carries them. A token that cannot be taken fails with the credential's own line
140
+ * (`BrokerNatsCredential.ready`). `dial` is nats.js's `connect`, or a test's, and `waits` how the
141
+ * dials wait between them, a test's where it sets one.
137
142
  *
138
143
  * `signal` aborting (the process is stopping) ends the wait for the token and for the dial at once,
139
144
  * with `START_STOPPING`, and starts no further dial; the token trade itself goes on, since it is
@@ -147,6 +152,7 @@ export async function dialChannelNats(
147
152
  dial: (options: ConnectionOptions) => Promise<NatsConnection> = connect,
148
153
  signal?: AbortSignal,
149
154
  plan: NatsDialPlan = CHANNEL_DIAL_PLAN,
155
+ waits: Pick<NatsDialHooks, "delay" | "random"> = {},
150
156
  ): Promise<ChannelNats> {
151
157
  const stopped = Promise.withResolvers<"stopping">()
152
158
  const onAbort = (): void => stopped.resolve("stopping")
@@ -186,13 +192,19 @@ export async function dialChannelNats(
186
192
  // A dial that fails is dialed again for the server's life, as the token wait above is:
187
193
  // Claude Code starts no channel server again once one ended, and a hub closes a dial whose
188
194
  // CONNECT came after its 2 s authentication timeout, then answers the next one. Each dial has
189
- // its own bound (`connectNats`); a stop ends the redials.
195
+ // its own bound (`connectNats`); a stop ends the redials. A hub's refusal of the session's
196
+ // broker token waits on the broker-token backoff, 2-4 s doubling to 10-20 s, and a token it
197
+ // refuses twice is dropped and another trade started at once.
190
198
  const dialing = connectNats(
191
199
  dial,
192
200
  natsConnectionOptions({ servers: inbound.natsUrls, name, credential: natsCredential }),
193
201
  plan,
194
202
  {
203
+ ...waits,
195
204
  stopped: () => signal?.aborted === true,
205
+ credential: natsCredential,
206
+ onTokenDropped: (error, attempt) =>
207
+ warn(`${NATS_DIAL_TOKEN_DROPPED} (dial ${attempt})`, error),
196
208
  onRetry: (error, delayMs) =>
197
209
  warn(`${failed(error)}; dialing again in ${delayMs} ms`, error),
198
210
  },