@sjawhar/claude-envoy 1.39.1 → 1.40.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": "1.39.1"
5
+ "version": "1.40.0"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-envoy",
3
- "version": "1.39.1",
3
+ "version": "1.40.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
  }
@@ -44858,16 +44858,16 @@ function createReplayEngine(options) {
44858
44858
  unwritten.push(entry);
44859
44859
  };
44860
44860
  const flush = (alive, atOnce = false) => {
44861
- writes = writes.then(async () => {
44861
+ const written = writes.then(async () => {
44862
44862
  if (!alive())
44863
- return;
44863
+ return false;
44864
44864
  const json = stateJSON();
44865
44865
  const delivered = unwritten;
44866
44866
  if (json === writtenState && delivered.length === 0)
44867
- return;
44867
+ return true;
44868
44868
  const shape = stateJSON(false);
44869
44869
  if (delivered.length === 0 && !atOnce && shape === writtenShape && writtenAt !== undefined && now() - writtenAt < REPLAY_STATE_REFRESH_MS) {
44870
- return;
44870
+ return true;
44871
44871
  }
44872
44872
  unwritten = [];
44873
44873
  const state = json === writtenState ? undefined : JSON.parse(json);
@@ -44882,12 +44882,17 @@ function createReplayEngine(options) {
44882
44882
  writtenShape = shape;
44883
44883
  writtenAt = now();
44884
44884
  }
44885
+ return true;
44885
44886
  } catch (error) {
44886
44887
  unwritten = [...delivered, ...unwritten];
44887
44888
  log("replay: could not persist the replay state; the next write carries it", error);
44889
+ return false;
44888
44890
  }
44889
44891
  });
44890
- return writes;
44892
+ writes = written.then(() => {
44893
+ return;
44894
+ });
44895
+ return written;
44891
44896
  };
44892
44897
  const syncFollowed = () => {
44893
44898
  const followed = new Set(host.followedSubjects());
@@ -45353,18 +45358,20 @@ function createReplayEngine(options) {
45353
45358
  async recordDeliveredFor(sessionID, frame) {
45354
45359
  const alive = bindTo(sessionID, writeSession)?.alive;
45355
45360
  if (alive === undefined)
45356
- return;
45361
+ return false;
45357
45362
  record2(replayLedgerIdentity(frame));
45358
- await flush(alive);
45363
+ return await flush(alive);
45359
45364
  },
45360
- async liveFailed(followed, envelope, error, receivedAt) {
45365
+ async liveFailed(sessionID, followed, envelope, error, receivedAt) {
45361
45366
  const started = cursorStarts.get(followed);
45362
45367
  if (!isRoleForward(envelope) && answered.has(followed) && cursors.get(followed)?.after !== undefined && (started === undefined || started <= receivedAt)) {
45363
45368
  return;
45364
45369
  }
45365
- const alive = bind()?.alive;
45366
- if (alive === undefined)
45370
+ const alive = bindTo(sessionID, currentSession)?.alive;
45371
+ if (alive === undefined) {
45372
+ log("replay: a failed frame was not reported here; the session it names is closed");
45367
45373
  return;
45374
+ }
45368
45375
  try {
45369
45376
  await showFailureNotice(failureNotice(envelope, inboundTimestamp(envelope.issued_at), messageFor(error)), alive);
45370
45377
  } catch (thrown) {
@@ -45767,11 +45774,12 @@ function createConsumerDelivery(options) {
45767
45774
  settle(entry, "nak");
45768
45775
  return;
45769
45776
  }
45777
+ log(`frame ${entry.frame.streamSeq} failed and does not come again; its session gets the notice`, error);
45770
45778
  safely("failure notice", () => {
45771
45779
  const { subject, data } = entry.frame;
45772
45780
  const { envelope } = renderInbound(data, sessionID, subject);
45773
45781
  if (envelope !== undefined)
45774
- ledger.liveFailed(subject, envelope, error, now());
45782
+ ledger.liveFailed(sessionID, subject, envelope, error, now());
45775
45783
  });
45776
45784
  settle(entry, "done");
45777
45785
  };
@@ -55490,7 +55498,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
55490
55498
 
55491
55499
  // package.json
55492
55500
  var name = "@sjawhar/claude-envoy";
55493
- var version2 = "1.39.1";
55501
+ var version2 = "1.40.0";
55494
55502
  var legion = {
55495
55503
  daemonApiVersion: 68
55496
55504
  };
@@ -55641,9 +55649,15 @@ class AgentStreamPublisher {
55641
55649
  #listedCommands;
55642
55650
  #commands;
55643
55651
  #commandsSeq = 0;
55652
+ #fitListed;
55653
+ #fitLimit = 0;
55654
+ #fitEmptyBytes = 0;
55644
55655
  #settings;
55645
55656
  #settingsSeq = 0;
55646
55657
  #settingsJson;
55658
+ #context;
55659
+ #contextSeq = 0;
55660
+ #contextJson;
55647
55661
  constructor(deps) {
55648
55662
  this.#deps = deps;
55649
55663
  this.#seq = Math.trunc(deps.now()) * SEQ_PER_MS;
@@ -55657,6 +55671,9 @@ class AgentStreamPublisher {
55657
55671
  if (this.#settings !== undefined) {
55658
55672
  this.#deps.publish(subject, JSON.stringify(this.#settingsFrame(this.#settings)));
55659
55673
  }
55674
+ if (this.#context !== undefined) {
55675
+ this.#deps.publish(subject, JSON.stringify(this.#contextFrame(this.#context)));
55676
+ }
55660
55677
  if (this.#commands !== undefined) {
55661
55678
  this.#deps.publish(subject, JSON.stringify(this.#commandsFrame(this.#commands)));
55662
55679
  }
@@ -55673,6 +55690,8 @@ class AgentStreamPublisher {
55673
55690
  this.#commands = undefined;
55674
55691
  this.#settings = undefined;
55675
55692
  this.#settingsJson = undefined;
55693
+ this.#context = undefined;
55694
+ this.#contextJson = undefined;
55676
55695
  }
55677
55696
  setCommands(subject, commands) {
55678
55697
  const seen = new Set;
@@ -55716,13 +55735,43 @@ class AgentStreamPublisher {
55716
55735
  this.#deps.publish(subject, JSON.stringify(this.#settingsFrame(kept)));
55717
55736
  this.#fitCommands(subject);
55718
55737
  }
55738
+ setContext(subject, usage) {
55739
+ if (usage === undefined)
55740
+ return;
55741
+ if (!(usage.contextWindow > 0))
55742
+ return;
55743
+ if (typeof usage.percent !== "number" && !(usage.tokens > 0))
55744
+ return;
55745
+ const kept = {
55746
+ contextWindow: usage.contextWindow,
55747
+ tokens: usage.tokens
55748
+ };
55749
+ const json = JSON.stringify(kept);
55750
+ if (json === this.#contextJson)
55751
+ return;
55752
+ this.#context = kept;
55753
+ this.#contextJson = json;
55754
+ this.#seq += 1;
55755
+ this.#contextSeq = this.#seq;
55756
+ if (this.watched)
55757
+ this.#deps.publish(subject, JSON.stringify(this.#contextFrame(kept)));
55758
+ this.#fitCommands(subject);
55759
+ }
55719
55760
  #fitCommands(subject) {
55720
55761
  const listed = this.#listedCommands;
55721
55762
  if (listed === undefined)
55722
55763
  return;
55723
55764
  const settings = this.#settings === undefined ? 0 : jsonBytes(this.#settingsFrame(this.#settings)) + 1;
55724
- const limit = AGENT_STREAM_LIMITS.historyBytes - REPLAY_ARRAY_BYTES - settings;
55725
- let bytes = jsonBytes(this.#commandsFrame([], this.#seq + 1));
55765
+ const context = this.#context === undefined ? 0 : jsonBytes(this.#contextFrame(this.#context)) + 1;
55766
+ const limit = AGENT_STREAM_LIMITS.historyBytes - REPLAY_ARRAY_BYTES - settings - context;
55767
+ const emptyBytes = jsonBytes(this.#commandsFrame([], this.#seq + 1));
55768
+ const unchanged = listed === this.#fitListed && limit === this.#fitLimit && emptyBytes === this.#fitEmptyBytes;
55769
+ if (unchanged)
55770
+ return;
55771
+ this.#fitListed = listed;
55772
+ this.#fitLimit = limit;
55773
+ this.#fitEmptyBytes = emptyBytes;
55774
+ let bytes = emptyBytes;
55726
55775
  const kept = [];
55727
55776
  for (const command of listed) {
55728
55777
  const cost = jsonBytes(command) + (kept.length === 0 ? 0 : 1);
@@ -55745,6 +55794,9 @@ class AgentStreamPublisher {
55745
55794
  #settingsFrame(settings) {
55746
55795
  return { ...settings, kind: "settings", seq: this.#settingsSeq, v: AGENT_STREAM_PROTOCOL };
55747
55796
  }
55797
+ #contextFrame(context) {
55798
+ return { ...context, kind: "context", seq: this.#contextSeq, v: AGENT_STREAM_PROTOCOL };
55799
+ }
55748
55800
  record(subject, raw, streaming, dispatchMessageId) {
55749
55801
  const message = readMessage(raw);
55750
55802
  if (message === null)
@@ -55793,6 +55845,8 @@ class AgentStreamPublisher {
55793
55845
  const tail = [];
55794
55846
  if (this.#settings !== undefined)
55795
55847
  tail.push(this.#settingsFrame(this.#settings));
55848
+ if (this.#context !== undefined)
55849
+ tail.push(this.#contextFrame(this.#context));
55796
55850
  if (this.#commands !== undefined)
55797
55851
  tail.push(this.#commandsFrame(this.#commands));
55798
55852
  let used = REPLAY_ARRAY_BYTES + tail.reduce((bytes, frame, index) => bytes + jsonBytes(frame) + (index === 0 ? 0 : 1), 0);
@@ -59636,7 +59690,7 @@ async function startChannelSession(options) {
59636
59690
  failed: (message, topic, error, receivedAt) => {
59637
59691
  const envelope = renderInbound(message.raw, identity.id, message.subject).envelope;
59638
59692
  if (envelope !== undefined)
59639
- replay.liveFailed(topic, envelope, error, receivedAt);
59693
+ replay.liveFailed(identity.id, topic, envelope, error, receivedAt);
59640
59694
  },
59641
59695
  deliver: async (message) => {
59642
59696
  takeRemovals(message.raw);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/claude-envoy",
3
- "version": "1.39.1",
3
+ "version": "1.40.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Claude Code plugin for Legion's Envoy bus",
6
6
  "type": "module",
@@ -1087,9 +1087,11 @@ export async function startChannelSession(options: ChannelSessionOptions): Promi
1087
1087
  // A frame no sweep will show again gets its notice at once: a role forward, already
1088
1088
  // acknowledged, one on a subscription the replay does not read (refused, not started, or
1089
1089
  // the listener's replay failing), or one received before its subject's cursor started.
1090
- // The stream's copy of any other waits for a sweep.
1090
+ // The stream's copy of any other waits for a sweep. The notice is asked for this
1091
+ // process's one session by name, read when the failure lands.
1091
1092
  const envelope = renderInbound(message.raw, identity.id, message.subject).envelope
1092
- if (envelope !== undefined) void replay.liveFailed(topic, envelope, error, receivedAt)
1093
+ if (envelope !== undefined)
1094
+ void replay.liveFailed(identity.id, topic, envelope, error, receivedAt)
1093
1095
  },
1094
1096
  deliver: async (message) => {
1095
1097
  takeRemovals(message.raw)