@sjawhar/pi-legion 19.40.1 → 19.41.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.
Files changed (2) hide show
  1. package/dist/legion.js +83 -31
  2. package/package.json +1 -1
package/dist/legion.js CHANGED
@@ -26542,7 +26542,8 @@ var ErrorWireSchema = object({
26542
26542
  holder: string2().optional(),
26543
26543
  holder_state: SessionStateSchema.optional(),
26544
26544
  last_seen: number2().int().optional(),
26545
- claim_released: boolean2().optional()
26545
+ claim_released: boolean2().optional(),
26546
+ retry_after_seconds: number2().int().positive().optional()
26546
26547
  });
26547
26548
  var EnvelopeResponseSchema = EnvelopeSchema.extend({
26548
26549
  recipient: string2().optional(),
@@ -26619,6 +26620,7 @@ class EnvoyApiError extends Error {
26619
26620
  sessionState;
26620
26621
  lastSeen;
26621
26622
  claimReleased;
26623
+ retryAfterSeconds;
26622
26624
  constructor(details) {
26623
26625
  let parsed;
26624
26626
  try {
@@ -26635,6 +26637,7 @@ class EnvoyApiError extends Error {
26635
26637
  this.sessionState = parsed?.holder_state ?? parsed?.state;
26636
26638
  this.lastSeen = parsed?.last_seen;
26637
26639
  this.claimReleased = parsed?.claim_released;
26640
+ this.retryAfterSeconds = parsed?.retry_after_seconds;
26638
26641
  }
26639
26642
  }
26640
26643
  var mintedDedupeKey2 = new RegExp(MINTED_DEDUPE_KEY_PATTERN);
@@ -29928,7 +29931,7 @@ import { logger as logger3 } from "@oh-my-pi/pi-utils";
29928
29931
  // package.json
29929
29932
  var package_default = {
29930
29933
  name: "@sjawhar/pi-legion",
29931
- version: "19.40.1",
29934
+ version: "19.41.0",
29932
29935
  license: "Apache-2.0",
29933
29936
  type: "module",
29934
29937
  omp: {
@@ -31000,6 +31003,8 @@ function stallNoticeTopic(input) {
31000
31003
  }
31001
31004
  return { kind: "topic", topic: roleTopic(token) };
31002
31005
  }
31006
+ var STALL_NOTICE_SENDS = 4;
31007
+ var NOTICE_UNTOLD = { ownerTold: false, resend: undefined };
31003
31008
  function createStallTeller(deps) {
31004
31009
  const told = new Map;
31005
31010
  const stopsTold = new Set;
@@ -31028,13 +31033,60 @@ function createStallTeller(deps) {
31028
31033
  return false;
31029
31034
  }
31030
31035
  };
31036
+ const sleep = deps.sleep ?? ((ms) => Bun.sleep(ms));
31037
+ const attempt = async (send) => {
31038
+ try {
31039
+ const answer = await send();
31040
+ return answer.kept === true ? { kind: "kept" } : { kind: "told" };
31041
+ } catch (error) {
31042
+ if (error instanceof EnvoyApiError && error.reason === "sender_rate_limited" && error.retryAfterSeconds !== undefined) {
31043
+ return { kind: "rate-limited", retryAfterSeconds: error.retryAfterSeconds, error };
31044
+ }
31045
+ return { kind: "refused", error };
31046
+ }
31047
+ };
31048
+ const logUntold = (outcome, context) => {
31049
+ if (outcome.kind === "kept") {
31050
+ deps.log.warn("legion: the listener kept the stall notice; no owner has taken it", context);
31051
+ return;
31052
+ }
31053
+ deps.log.warn("legion: the owner was not told about this session's stuck tool call", {
31054
+ ...context,
31055
+ error: messageFor(outcome.error)
31056
+ });
31057
+ };
31058
+ const logWait = (outcome, context) => {
31059
+ deps.log.warn("legion: the listener refused the stall notice for this session's send rate; it is sent again after the wait the listener named", { ...context, retryAfterSeconds: outcome.retryAfterSeconds, error: messageFor(outcome.error) });
31060
+ };
31061
+ const resendNotice = async (send, firstWaitSeconds, context, onTold) => {
31062
+ let waitSeconds = firstWaitSeconds;
31063
+ for (let sends = 2;sends <= STALL_NOTICE_SENDS; sends += 1) {
31064
+ const outcome = await attempt(() => sleep(waitSeconds * 1000).then(send));
31065
+ const where = { ...context, sends };
31066
+ if (outcome.kind === "told") {
31067
+ onTold?.();
31068
+ deps.log.warn("legion: the stall notice was sent again and its owner was told", where);
31069
+ return;
31070
+ }
31071
+ if (outcome.kind !== "rate-limited") {
31072
+ logUntold(outcome, where);
31073
+ return;
31074
+ }
31075
+ if (sends === STALL_NOTICE_SENDS) {
31076
+ deps.log.warn(`legion: the listener refused the stall notice for this session's send rate; it was given up after ${STALL_NOTICE_SENDS} sends`, { ...where, error: messageFor(outcome.error) });
31077
+ return;
31078
+ }
31079
+ logWait(outcome, where);
31080
+ waitSeconds = outcome.retryAfterSeconds;
31081
+ }
31082
+ };
31031
31083
  const publishNotice = async (notice, input, context) => {
31032
31084
  if (notice.kind === "none") {
31033
31085
  deps.log.warn("legion: nobody was told about this session's stuck tool call", {
31034
31086
  ...context,
31035
31087
  why: notice.why
31036
31088
  });
31037
- return false;
31089
+ return NOTICE_UNTOLD;
31038
31090
  }
31039
31091
  const claim = deps.claim();
31040
31092
  const publish = deps.publisher();
@@ -31043,32 +31095,30 @@ function createStallTeller(deps) {
31043
31095
  ...context,
31044
31096
  topic: notice.topic
31045
31097
  });
31046
- return false;
31047
- }
31048
- try {
31049
- const answer = await publish({
31050
- topic: notice.topic,
31051
- message: input.message,
31052
- payload: input.payload,
31053
- idempotencyKey: input.key,
31054
- sourceSessionID: claim.sessionID
31055
- });
31056
- if (answer.kept === true) {
31057
- deps.log.warn("legion: the listener kept the stall notice; no owner has taken it", {
31058
- ...context,
31059
- topic: notice.topic
31060
- });
31061
- return false;
31098
+ return NOTICE_UNTOLD;
31099
+ }
31100
+ const send = () => publish({
31101
+ topic: notice.topic,
31102
+ message: input.message,
31103
+ payload: input.payload,
31104
+ idempotencyKey: input.key,
31105
+ sourceSessionID: claim.sessionID
31106
+ });
31107
+ const where = { ...context, topic: notice.topic };
31108
+ const first = await attempt(send);
31109
+ if (first.kind === "told")
31110
+ return { ownerTold: true, resend: undefined };
31111
+ if (first.kind !== "rate-limited") {
31112
+ logUntold(first, where);
31113
+ return NOTICE_UNTOLD;
31114
+ }
31115
+ logWait(first, { ...where, sends: 1 });
31116
+ return {
31117
+ ownerTold: false,
31118
+ resend: (onTold) => {
31119
+ resendNotice(send, first.retryAfterSeconds, where, onTold);
31062
31120
  }
31063
- return true;
31064
- } catch (error) {
31065
- deps.log.warn("legion: the owner was not told about this session's stuck tool call", {
31066
- ...context,
31067
- topic: notice.topic,
31068
- error: messageFor(error)
31069
- });
31070
- return false;
31071
- }
31121
+ };
31072
31122
  };
31073
31123
  const noticeFor = async () => {
31074
31124
  const session = deps.session();
@@ -31104,13 +31154,14 @@ function createStallTeller(deps) {
31104
31154
  arguments: record.arguments,
31105
31155
  tree: record.tree.read ? record.tree.processes.length : record.tree.reason
31106
31156
  });
31107
- const ownerTold = await publishNotice(await noticeFor(), {
31157
+ const sent = await publishNotice(await noticeFor(), {
31108
31158
  message: stallNoticeMessage(record),
31109
31159
  payload: JSON.stringify(record),
31110
31160
  key: `legion-stall:${toolCallId}`
31111
31161
  }, { toolCallId, tool: record.tool });
31112
- const answer = { evidenceWritten, ownerTold };
31162
+ const answer = { evidenceWritten, ownerTold: sent.ownerTold };
31113
31163
  told.set(toolCallId, answer);
31164
+ sent.resend?.(() => told.set(toolCallId, { evidenceWritten, ownerTold: true }));
31114
31165
  return answer;
31115
31166
  },
31116
31167
  recordStep: ({ call, step, openMs, outcome }) => {
@@ -31150,11 +31201,12 @@ function createStallTeller(deps) {
31150
31201
  stopsTold.add(key);
31151
31202
  const claim = deps.claim();
31152
31203
  const who = claim === undefined ? "A session" : `The ${claim.role} of ${claim.issue}`;
31153
- await publishNotice(await noticeFor(), {
31204
+ const sent = await publishNotice(await noticeFor(), {
31154
31205
  message: `${who} has a \`${call.toolName}\` call it cannot recover from on its own: ${reason}. ` + "The evidence for the call is in that session's transcript.",
31155
31206
  payload: JSON.stringify({ toolCallId: call.toolCallId, tool: call.toolName, reason }),
31156
31207
  key: `legion-stall-stopped:${call.toolCallId}`
31157
31208
  }, { toolCallId: call.toolCallId, tool: call.toolName, reason });
31209
+ sent.resend?.();
31158
31210
  }
31159
31211
  };
31160
31212
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/pi-legion",
3
- "version": "19.40.1",
3
+ "version": "19.41.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "omp": {