@agentchatme/codex 0.7.1 → 0.7.2

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/dist/index.js CHANGED
@@ -4744,7 +4744,7 @@ function runSpawned(spec, captureBytes) {
4744
4744
  // node_modules/.pnpm/@agentchatme+agent-core@0.7.1/node_modules/@agentchatme/agent-core/dist/chunk-AQTRC7G4.js
4745
4745
  import * as path3 from "path";
4746
4746
 
4747
- // node_modules/.pnpm/agentchatme@1.5.0_ws@8.21.1/node_modules/agentchatme/dist/index.js
4747
+ // node_modules/.pnpm/agentchatme@1.5.1_ws@8.21.1/node_modules/agentchatme/dist/index.js
4748
4748
  var __defProp3 = Object.defineProperty;
4749
4749
  var __export3 = (target, all) => {
4750
4750
  for (var name in all)
@@ -5050,7 +5050,7 @@ function createAgentChatError(body, status, headers) {
5050
5050
  return new AgentChatError(body, status, requestId);
5051
5051
  }
5052
5052
  }
5053
- var VERSION = "1.5.0";
5053
+ var VERSION = "1.5.1";
5054
5054
  function detectRuntime() {
5055
5055
  const g = globalThis;
5056
5056
  if (typeof g.Bun?.version === "string") return `bun/${g.Bun.version}`;
@@ -9557,6 +9557,7 @@ function readResult(text2) {
9557
9557
  }
9558
9558
  var TRAY_LIMIT = 50;
9559
9559
  var SILENT_MEMORY = 1e3;
9560
+ var CONVERSATION_KEY = "conversation:";
9560
9561
  var PACE_MS = 6e4;
9561
9562
  var TURN_OVERDUE_MS = 30 * 6e4;
9562
9563
  var NO_PROGRESS_BACKOFF_MS = 1e3;
@@ -9579,30 +9580,98 @@ var Pacer = class {
9579
9580
  paceMs;
9580
9581
  now;
9581
9582
  until = /* @__PURE__ */ new Map();
9583
+ /** Orders holds and tray reads: a read lifts only holds older than its start. */
9584
+ seq = 0;
9585
+ /** A D5 pace. It never replaces a could-not-process hold at the same anchor (that input stays held). */
9582
9586
  hold(key, anchor) {
9583
- this.until.delete(key);
9584
- this.until.set(key, { anchor, until: this.now() + this.paceMs });
9585
- while (this.until.size > SILENT_MEMORY) {
9586
- const oldest = this.until.keys().next().value;
9587
- if (oldest === void 0) break;
9588
- this.until.delete(oldest);
9589
- }
9590
- }
9591
- held(key, anchor) {
9587
+ const prior = this.until.get(key);
9588
+ if (prior?.failure !== void 0 && prior.anchor === anchor) return;
9589
+ this.set(key, { anchor, until: this.now() + this.paceMs });
9590
+ }
9591
+ /** Could not process: hold the key at this anchor until a newer message moves it (held again at it: since the first time). */
9592
+ holdFailed(key, anchor, reason) {
9593
+ const prior = this.until.get(key);
9594
+ const since = prior?.failure !== void 0 && prior.anchor === anchor ? prior.failure.since : this.now();
9595
+ this.set(key, { anchor, until: Number.POSITIVE_INFINITY, failure: { reason, since, seq: ++this.seq } });
9596
+ }
9597
+ /** What holds the key at this anchor now: a pace, a could-not-process hold, or nothing (a stale entry goes). */
9598
+ check(key, anchor) {
9592
9599
  const entry = this.until.get(key);
9593
- if (!entry) return false;
9600
+ if (!entry) return null;
9594
9601
  if (entry.anchor !== anchor || this.now() >= entry.until) {
9595
9602
  this.until.delete(key);
9596
- return false;
9603
+ return null;
9597
9604
  }
9598
- return true;
9605
+ return entry.failure ? "failed" : "paced";
9599
9606
  }
9607
+ held(key, anchor) {
9608
+ return this.check(key, anchor) !== null;
9609
+ }
9610
+ /** A could-not-process hold on the key at exactly this anchor; changes nothing (a queued run's anchor may be older than the card's). */
9611
+ heldFailedAt(key, anchor) {
9612
+ const entry = this.until.get(key);
9613
+ return entry?.failure !== void 0 && entry.anchor === anchor;
9614
+ }
9615
+ /** Lift whatever holds the key (a pace or a could-not-process hold). */
9600
9616
  release(key) {
9601
9617
  this.until.delete(key);
9602
9618
  }
9619
+ /** Lift a pace; a could-not-process hold stays. */
9620
+ releasePace(key) {
9621
+ if (this.until.get(key)?.failure === void 0) this.until.delete(key);
9622
+ }
9603
9623
  releaseInvites() {
9604
9624
  for (const key of [...this.until.keys()]) if (key.startsWith("invite:")) this.until.delete(key);
9605
9625
  }
9626
+ /** Taken before a tray read: `keepFailedOn` lifts only holds made before it. */
9627
+ mark() {
9628
+ return this.seq;
9629
+ }
9630
+ /**
9631
+ * After a tray read from `mark` (`anchors`: each card's key and anchor):
9632
+ * a hold whose card shows another anchor is over (a newer message came),
9633
+ * and, on a complete tray (`complete`: not truncated), so is one whose
9634
+ * card is gone (read meanwhile: a verdict, a mark-read). Only holds made
9635
+ * before the read started: a read that began earlier says nothing of a
9636
+ * later hold.
9637
+ */
9638
+ keepFailedOn(anchors, mark, complete) {
9639
+ for (const [key, entry] of this.until) {
9640
+ if (entry.failure === void 0 || entry.failure.seq > mark) continue;
9641
+ const shown = anchors.get(key);
9642
+ if (shown === void 0 ? complete : shown !== entry.anchor) this.until.delete(key);
9643
+ }
9644
+ }
9645
+ /** The could-not-process holds, oldest first (by `since`). */
9646
+ failed() {
9647
+ const out = [];
9648
+ for (const [key, entry] of this.until) {
9649
+ if (entry.failure !== void 0 && entry.anchor !== null) {
9650
+ out.push({ key, anchor: entry.anchor, reason: entry.failure.reason, since: entry.failure.since });
9651
+ }
9652
+ }
9653
+ return out.sort((a, b) => a.since - b.since);
9654
+ }
9655
+ set(key, entry) {
9656
+ this.until.delete(key);
9657
+ this.until.set(key, entry);
9658
+ let paces = 0;
9659
+ let holds = 0;
9660
+ for (const held of this.until.values()) {
9661
+ if (held.failure === void 0) paces += 1;
9662
+ else holds += 1;
9663
+ }
9664
+ for (const [key2, held] of this.until) {
9665
+ if (paces <= SILENT_MEMORY && holds <= SILENT_MEMORY) break;
9666
+ if (held.failure === void 0 && paces > SILENT_MEMORY) {
9667
+ this.until.delete(key2);
9668
+ paces -= 1;
9669
+ } else if (held.failure !== void 0 && holds > SILENT_MEMORY) {
9670
+ this.until.delete(key2);
9671
+ holds -= 1;
9672
+ }
9673
+ }
9674
+ }
9606
9675
  };
9607
9676
  function clears(completion, failure) {
9608
9677
  return completion.kind === failure.source && completion.conversations.has(failure.conversationId);
@@ -9687,16 +9756,25 @@ var ReceiveLoop = class {
9687
9756
  rerunsCapped: 0,
9688
9757
  runtimeChecksOk: 0,
9689
9758
  runtimeChecksFailed: 0,
9690
- splitsStopped: 0
9759
+ splitsStopped: 0,
9760
+ startsHeld: 0
9691
9761
  };
9692
9762
  /** Conversations whose verdict a gate call is recording right now (an abandoned cycle's included). */
9693
9763
  recordsInFlight = /* @__PURE__ */ new Set();
9764
+ /**
9765
+ * Could not process: conversations whose report is out on another's
9766
+ * completion (`takeDue`): held out of the gate and of new runs, and a run
9767
+ * or turn of theirs waiting for a slot waits, until the server answers.
9768
+ */
9769
+ reporting = /* @__PURE__ */ new Map();
9694
9770
  /** Turns holding a slot (admitted, not yet ended or released). */
9695
9771
  slotted = /* @__PURE__ */ new Set();
9696
9772
  handle;
9697
9773
  started = false;
9698
9774
  stopped = false;
9699
9775
  heldWhileRunning = false;
9776
+ /** A cycle left a card out because its could-not-process report was out: `letGo` reads the tray again. */
9777
+ heldWhileReporting = false;
9700
9778
  activeTurns = 0;
9701
9779
  noProgressStreak = 0;
9702
9780
  moreTimer = null;
@@ -9894,7 +9972,13 @@ var ReceiveLoop = class {
9894
9972
  processingFailures: {
9895
9973
  ...this.failureCounts,
9896
9974
  awaitingEvidence: this.awaitingEvidence.size,
9897
- lastRuntimeCheck: this.lastRuntimeCheck === null ? null : { ...this.lastRuntimeCheck }
9975
+ lastRuntimeCheck: this.lastRuntimeCheck === null ? null : { ...this.lastRuntimeCheck },
9976
+ held: this.pacer.failed().map(({ key, anchor, reason, since }) => ({
9977
+ conversationId: key.slice(CONVERSATION_KEY.length),
9978
+ anchor,
9979
+ since: new Date(since).toISOString(),
9980
+ reason
9981
+ }))
9898
9982
  },
9899
9983
  lastError: this.lastError
9900
9984
  };
@@ -9924,7 +10008,7 @@ var ReceiveLoop = class {
9924
10008
  const id = typeof payload.conversation_id === "string" ? payload.conversation_id : void 0;
9925
10009
  const named = Array.isArray(payload.conversation_ids) ? payload.conversation_ids.filter((x) => typeof x === "string") : [];
9926
10010
  if (reason === "unanswered") {
9927
- for (const conversationId of named) this.pacer.release(`conversation:${conversationId}`);
10011
+ for (const conversationId of named) this.pacer.releasePace(`${CONVERSATION_KEY}${conversationId}`);
9928
10012
  this.pacer.releaseInvites();
9929
10013
  }
9930
10014
  this.wakeFrom("wake_frame", id, {
@@ -10104,20 +10188,22 @@ var ReceiveLoop = class {
10104
10188
  }
10105
10189
  }
10106
10190
  async gateCallCycle(run32) {
10191
+ const mark = this.pacer.mark();
10107
10192
  const tray = await this.readTray();
10108
10193
  if (run32.stale()) return this.summary("abandoned");
10109
10194
  if (tray === null) return this.summary("tray_failed", { failure: this.lastError });
10110
10195
  const cards = cardsOf(tray);
10196
+ this.forgetCleared(tray, mark);
10111
10197
  const invites = trayInvites(tray);
10112
10198
  const unheld = this.unheld(cards);
10113
10199
  const heldByTurns = cards.length - unheld.length;
10114
10200
  if (heldByTurns > 0) this.heldWhileRunning = true;
10115
- const gateCards = unheld.filter((card) => !this.pacer.held(`conversation:${card.conversation_id}`, anchorFrom(card)));
10201
+ const { open: gateCards, paced: pacedCards, heldAfterFailure } = this.paceCards(unheld);
10116
10202
  const openInvites = invites.filter((invite) => !this.tracker.inviteRunning(invite.invite_id));
10117
10203
  const owedInvites = openInvites.filter((invite) => !this.pacer.held(`invite:${invite.invite_id}`, null));
10118
- const paced = unheld.length - gateCards.length + (openInvites.length - owedInvites.length);
10204
+ const paced = pacedCards + (openInvites.length - owedInvites.length);
10119
10205
  const owedCards = gateCards.filter((card) => card.muted !== true);
10120
- const base = { cards: cards.length, invites: invites.length, heldByTurns, paced, truncated: tray.truncated === true };
10206
+ const base = { cards: cards.length, invites: invites.length, heldByTurns, paced, heldAfterFailure, truncated: tray.truncated === true };
10121
10207
  if (cards.length === 0 && invites.length === 0) return this.summary("empty", base);
10122
10208
  if (owedCards.length === 0 && owedInvites.length === 0) return this.summary("nothing_owed", base);
10123
10209
  const texts = await this.options.texts.ensure();
@@ -10140,7 +10226,8 @@ var ReceiveLoop = class {
10140
10226
  chunksOk: 0,
10141
10227
  chunksFailed: 0,
10142
10228
  splitCallsOk: 0,
10143
- splitCallsFailed: 0
10229
+ splitCallsFailed: 0,
10230
+ heldAfterFailure
10144
10231
  };
10145
10232
  let heldDuringGate = 0;
10146
10233
  let splitStopped = false;
@@ -10149,6 +10236,7 @@ var ReceiveLoop = class {
10149
10236
  if (owedCards.length > 0) {
10150
10237
  const gate = await this.gateInChunks(run32, tray, gateCards, texts);
10151
10238
  heldDuringGate = gate.held;
10239
+ tally.heldAfterFailure += gate.heldAfterFailure;
10152
10240
  tally.gated = gate.gated;
10153
10241
  tally.reply = gate.verdicts.reply;
10154
10242
  tally.dismiss = gate.verdicts.dismiss;
@@ -10171,7 +10259,7 @@ var ReceiveLoop = class {
10171
10259
  } else if (tally.chunksFailed + tally.splitCallsFailed > 0) {
10172
10260
  outcome = "fail_closed";
10173
10261
  } else if (owedInvites.length === 0 && !run32.stale()) {
10174
- return this.summary("nothing_owed", { ...base, heldByTurns: heldByTurns + heldDuringGate });
10262
+ return this.summary("nothing_owed", { ...base, heldByTurns: heldByTurns + heldDuringGate, heldAfterFailure: tally.heldAfterFailure });
10175
10263
  }
10176
10264
  }
10177
10265
  for (const invite of owedInvites) {
@@ -10188,10 +10276,54 @@ var ReceiveLoop = class {
10188
10276
  /**
10189
10277
  * Held out of the gate: a running or waiting turn, or a verdict still being
10190
10278
  * recorded (an abandoned cycle's last call): gating it again could record
10191
- * the same verdict twice (a second defer climbs the ladder).
10279
+ * the same verdict twice (a second defer climbs the ladder). Also a
10280
+ * conversation whose could-not-process report is out (`reporting`): its
10281
+ * answer may hold it.
10192
10282
  */
10193
10283
  unheld(cards) {
10194
- return this.tracker.withoutRunning(cards).filter((card) => !this.recordsInFlight.has(card.conversation_id));
10284
+ return this.tracker.withoutRunning(cards).filter((card) => {
10285
+ if (this.recordsInFlight.has(card.conversation_id)) return false;
10286
+ if (!this.reporting.has(card.conversation_id)) return true;
10287
+ this.heldWhileReporting = true;
10288
+ return false;
10289
+ });
10290
+ }
10291
+ /**
10292
+ * The cards neither paced (D5) nor held after a could-not-process answer
10293
+ * (`none` / `paused` through the anchor they still show), and how many of
10294
+ * each were left out. A card whose anchor moved is no longer held.
10295
+ */
10296
+ paceCards(cards) {
10297
+ const open = [];
10298
+ let paced = 0;
10299
+ let heldAfterFailure = 0;
10300
+ for (const card of cards) {
10301
+ const state = this.pacer.check(`${CONVERSATION_KEY}${card.conversation_id}`, anchorFrom(card));
10302
+ if (state === "paced") paced += 1;
10303
+ else if (state === "failed") heldAfterFailure += 1;
10304
+ else open.push(card);
10305
+ }
10306
+ return { open, paced, heldAfterFailure };
10307
+ }
10308
+ /** Held after a could-not-process answer, at the anchor this card still shows. */
10309
+ heldAfterFailure(card) {
10310
+ return this.pacer.check(`${CONVERSATION_KEY}${card.conversation_id}`, anchorFrom(card)) === "failed";
10311
+ }
10312
+ /**
10313
+ * A tray read after `mark`, every card on it (a turn's included): a held
10314
+ * conversation whose card shows a newer anchor is no longer held, and on
10315
+ * a complete tray (not truncated) neither is one whose card is gone (read
10316
+ * meanwhile: a verdict, a mark-read). A truncated tray proves nothing
10317
+ * about the cards it does not show.
10318
+ */
10319
+ forgetCleared(tray, mark) {
10320
+ const anchors = new Map(cardsOf(tray).map((card) => [`${CONVERSATION_KEY}${card.conversation_id}`, anchorFrom(card)]));
10321
+ this.pacer.keepFailedOn(anchors, mark, tray.truncated !== true);
10322
+ }
10323
+ /** The start of a run or reply turn: held after a could-not-process answer at the anchor it was started for. */
10324
+ failureHeld(handle) {
10325
+ const turn = handle.turn;
10326
+ return turn.kind === "reply" && this.pacer.heldFailedAt(`${CONVERSATION_KEY}${turn.conversationId}`, turn.anchor);
10195
10327
  }
10196
10328
  /**
10197
10329
  * The gate in chunks (contract v1 §3.6): the cards, in tray order, split
@@ -10225,6 +10357,7 @@ var ReceiveLoop = class {
10225
10357
  const out = {
10226
10358
  gated: 0,
10227
10359
  held: 0,
10360
+ heldAfterFailure: 0,
10228
10361
  reply: [],
10229
10362
  verdicts: { reply: 0, dismiss: 0, defer: 0 },
10230
10363
  undecided: 0,
@@ -10270,7 +10403,9 @@ var ReceiveLoop = class {
10270
10403
  const of = Math.ceil(chunked.length / size);
10271
10404
  for (let index2 = 1; index2 <= of; index2++) {
10272
10405
  if (run32.stale()) break;
10273
- const planned = chunked.slice((index2 - 1) * size, index2 * size);
10406
+ const slice = chunked.slice((index2 - 1) * size, index2 * size);
10407
+ const planned = slice.filter((card) => !this.heldAfterFailure(card));
10408
+ out.heldAfterFailure += slice.length - planned.length;
10274
10409
  const chunk = this.unheld(planned);
10275
10410
  out.held += planned.length - chunk.length;
10276
10411
  if (chunk.length < planned.length) this.heldWhileRunning = true;
@@ -10304,7 +10439,7 @@ var ReceiveLoop = class {
10304
10439
  if (report.outcome === "decided") {
10305
10440
  const through = decidedBy(result);
10306
10441
  for (const id of through) this.gateAlone.delete(id);
10307
- await run32.exclude(() => this.reportDue(this.completed("gate", through)));
10442
+ await run32.exclude(() => this.reportDue(this.takeDue(this.completed("gate", through))));
10308
10443
  continue;
10309
10444
  }
10310
10445
  this.failedAttempt("gate", chunk.filter((card) => card.muted !== true).map((card) => card.conversation_id));
@@ -10325,7 +10460,7 @@ var ReceiveLoop = class {
10325
10460
  if (run32.stale()) break splitting;
10326
10461
  const { card, reruns } = queue.shift();
10327
10462
  const id = card.conversation_id;
10328
- if (decided.has(id) || split.skip.has(id)) continue;
10463
+ if (decided.has(id) || split.skip.has(id) || this.heldAfterFailure(card)) continue;
10329
10464
  if (this.unheld([card]).length === 0) {
10330
10465
  if (!gated.has(id)) out.held += 1;
10331
10466
  this.heldWhileRunning = true;
@@ -10356,7 +10491,7 @@ var ReceiveLoop = class {
10356
10491
  if (report.outcome === "decided") {
10357
10492
  failuresInARow = 0;
10358
10493
  this.gateAlone.delete(id);
10359
- await run32.exclude(() => this.reportDue(this.completed("gate", decidedBy(result))));
10494
+ await run32.exclude(() => this.reportDue(this.takeDue(this.completed("gate", decidedBy(result)))));
10360
10495
  continue;
10361
10496
  }
10362
10497
  this.rememberGateAlone(id);
@@ -10421,12 +10556,14 @@ var ReceiveLoop = class {
10421
10556
  }
10422
10557
  }
10423
10558
  async singleRunCycle(run32) {
10559
+ const mark = this.pacer.mark();
10424
10560
  const tray = await this.readTray();
10425
10561
  if (run32.stale()) return this.summary("abandoned");
10426
10562
  let cards = null;
10427
10563
  let invites = null;
10428
10564
  let base = { cards: 0, invites: 0, heldByTurns: 0, truncated: false };
10429
10565
  if (tray !== null) {
10566
+ this.forgetCleared(tray, mark);
10430
10567
  cards = cardsOf(tray).filter((card) => card.muted !== true).length;
10431
10568
  invites = trayInvites(tray).length;
10432
10569
  base = { cards: cardsOf(tray).length, invites, heldByTurns: 0, truncated: tray.truncated === true };
@@ -10474,17 +10611,17 @@ ${texts.invite_prompt.trim()}` : texts.wake_instruction.trim();
10474
10611
  const unheld = this.unheld(cards);
10475
10612
  const heldByTurns = cards.length - unheld.length;
10476
10613
  if (heldByTurns > 0) this.heldWhileRunning = true;
10477
- const runCards = unheld.filter((card) => !this.pacer.held(`conversation:${card.conversation_id}`, anchorFrom(card)));
10614
+ const { open: runCards, paced: pacedCards, heldAfterFailure } = this.paceCards(unheld);
10478
10615
  const openInvites = invites.filter((invite) => !this.tracker.inviteRunning(invite.invite_id));
10479
10616
  const owedInvites = openInvites.filter((invite) => !this.pacer.held(`invite:${invite.invite_id}`, null));
10480
- const paced = unheld.length - runCards.length + (openInvites.length - owedInvites.length);
10617
+ const paced = pacedCards + (openInvites.length - owedInvites.length);
10481
10618
  const owedCards = runCards.filter((card) => card.muted !== true && card.preview != null);
10482
- const base = { cards: cards.length, invites: invites.length, heldByTurns, paced, truncated: tray.truncated === true };
10619
+ const base = { cards: cards.length, invites: invites.length, heldByTurns, paced, heldAfterFailure, truncated: tray.truncated === true };
10483
10620
  if (owedCards.length === 0 && owedInvites.length === 0) return this.summary("nothing_owed", base);
10484
10621
  const unready = await this.checkReady();
10485
10622
  if (run32.stale()) return this.summary("abandoned", base);
10486
10623
  if (unready !== null) return this.summary("tools_unavailable", { ...base, failure: unready });
10487
- const tally = { turnsStarted: 0, turnsRefused: 0, turnsQueued: 0 };
10624
+ const tally = { turnsStarted: 0, turnsRefused: 0, turnsQueued: 0, heldAfterFailure };
10488
10625
  const failedLast = owedCards.filter((card) => this.awaitingEvidence.has(card.conversation_id));
10489
10626
  for (const card of owedCards.filter((card2) => !failedLast.includes(card2))) {
10490
10627
  if (run32.stale()) break;
@@ -10528,6 +10665,8 @@ ${texts.invite_prompt.trim()}` : texts.wake_instruction.trim();
10528
10665
  const held = this.tracker.holds(handle);
10529
10666
  let completion = null;
10530
10667
  let failure = null;
10668
+ let due = [];
10669
+ let ended = false;
10531
10670
  try {
10532
10671
  this.countRunEnd(end);
10533
10672
  this.emit({ type: "run", end, kind: "conversation", conversationId: attempt.conversationId });
@@ -10541,8 +10680,10 @@ ${texts.invite_prompt.trim()}` : texts.wake_instruction.trim();
10541
10680
  failure = { ...attempt, outcome: end.timedOut === true ? "timed_out" : "failed", failedAt: this.now(), holding: handle };
10542
10681
  }
10543
10682
  }
10683
+ ended = true;
10544
10684
  } finally {
10545
10685
  if (failure === null) this.tracker.end(handle);
10686
+ if (ended && !this.stopped && completion !== null) due = this.takeDue(completion);
10546
10687
  this.releaseSlot(handle);
10547
10688
  if (failure === null) this.afterTurnEnded();
10548
10689
  }
@@ -10554,7 +10695,7 @@ ${texts.invite_prompt.trim()}` : texts.wake_instruction.trim();
10554
10695
  this.afterTurnEnded();
10555
10696
  }
10556
10697
  }
10557
- if (!this.stopped && completion !== null) await this.reportDue(completion);
10698
+ await this.reportDue(due);
10558
10699
  await this.checkParked("run");
10559
10700
  }
10560
10701
  /** The invite run: one run for the pending invites, holding each of them until it ends. */
@@ -10594,6 +10735,8 @@ ${texts.invite_prompt.trim()}` : texts.wake_instruction.trim();
10594
10735
  const end = await done;
10595
10736
  const held = this.tracker.holds(slot);
10596
10737
  let completion = null;
10738
+ let due = [];
10739
+ let ended = false;
10597
10740
  try {
10598
10741
  this.countRunEnd(end);
10599
10742
  this.emit({ type: "run", end, kind: "invite" });
@@ -10603,12 +10746,14 @@ ${texts.invite_prompt.trim()}` : texts.wake_instruction.trim();
10603
10746
  for (const invite of invites) this.pacer.hold(`invite:${invite.invite_id}`, null);
10604
10747
  }
10605
10748
  if (this.options.dismissIgnoredInvites !== false) await this.afterRunInvites(invites, end);
10749
+ ended = true;
10606
10750
  } finally {
10607
10751
  endAll();
10752
+ if (ended && !this.stopped && completion !== null) due = this.takeDue(completion);
10608
10753
  this.releaseSlot(slot);
10609
10754
  this.afterTurnEnded();
10610
10755
  }
10611
- if (!this.stopped && completion !== null) await this.reportDue(completion);
10756
+ await this.reportDue(due);
10612
10757
  await this.checkParked("run");
10613
10758
  }
10614
10759
  /** The runtime's `startRun`, never rejecting: a throw is a run that did not start. */
@@ -10701,7 +10846,13 @@ invite_id: ${invite.invite_id}`,
10701
10846
  * hands its slot on the same way.
10702
10847
  */
10703
10848
  async launch(handle, admit, drop) {
10704
- if (this.waiting.length > 0 || this.activeTurns >= this.maxTurns) {
10849
+ if (this.failureHeld(handle)) {
10850
+ this.tracker.end(handle);
10851
+ drop?.();
10852
+ this.failureCounts.startsHeld += 1;
10853
+ return "held";
10854
+ }
10855
+ if (this.waiting.length > 0 || this.activeTurns >= this.maxTurns || this.awaitsAnswer(handle)) {
10705
10856
  this.waiting.push({ handle, admit, ...drop ? { drop } : {} });
10706
10857
  this.counts.turnsQueued += 1;
10707
10858
  if (this.activeTurns < this.maxTurns) void this.pump();
@@ -10726,16 +10877,29 @@ invite_id: ${invite.invite_id}`,
10726
10877
  if (options.pump !== false) void this.pump();
10727
10878
  }
10728
10879
  async pump() {
10729
- while (this.waiting.length > 0 && this.activeTurns < this.maxTurns && !this.stopped) {
10730
- const next = this.waiting.shift();
10880
+ while (this.activeTurns < this.maxTurns && !this.stopped) {
10881
+ const index = this.waiting.findIndex((w) => !this.awaitsAnswer(w.handle));
10882
+ if (index < 0) break;
10883
+ const next = this.waiting.splice(index, 1)[0];
10731
10884
  if (!this.tracker.holds(next.handle)) continue;
10885
+ if (this.failureHeld(next.handle)) {
10886
+ this.tracker.end(next.handle);
10887
+ next.drop?.();
10888
+ this.failureCounts.startsHeld += 1;
10889
+ continue;
10890
+ }
10732
10891
  await this.admitInSlot(next.handle, next.admit);
10733
10892
  }
10734
10893
  }
10894
+ awaitsAnswer(handle) {
10895
+ return handle.turn.kind === "reply" && this.reporting.has(handle.turn.conversationId);
10896
+ }
10735
10897
  async afterReplyTurn(handle, attempt, done) {
10736
10898
  const reply = attempt.reply;
10737
10899
  let completion = null;
10738
10900
  let failure = null;
10901
+ let due = [];
10902
+ let ended = false;
10739
10903
  try {
10740
10904
  const end = await this.tracker.confirmedEnd(handle, done);
10741
10905
  if (end === null || !this.tracker.holds(handle)) return;
@@ -10745,8 +10909,10 @@ invite_id: ${invite.invite_id}`,
10745
10909
  if (end.status === "failed" && recorded?.freshRead === true && recorded.outcome.reason === "turn_failed") {
10746
10910
  failure = { ...attempt, outcome: end.timedOut === true ? "timed_out" : "failed", failedAt: this.now(), holding: handle };
10747
10911
  }
10912
+ ended = true;
10748
10913
  } finally {
10749
10914
  if (failure === null) this.tracker.end(handle);
10915
+ if (ended && !this.stopped && completion !== null) due = this.takeDue(completion);
10750
10916
  this.releaseSlot(handle);
10751
10917
  if (failure === null) this.afterTurnEnded();
10752
10918
  }
@@ -10758,7 +10924,7 @@ invite_id: ${invite.invite_id}`,
10758
10924
  this.afterTurnEnded();
10759
10925
  }
10760
10926
  }
10761
- if (!this.stopped && completion !== null) await this.track(this.reportDue(completion));
10927
+ await this.track(this.reportDue(due));
10762
10928
  await this.track(this.checkParked("turn"));
10763
10929
  }
10764
10930
  async recordReplyOutcome(handle, reply, end) {
@@ -10880,11 +11046,15 @@ invite_id: ${invite.invite_id}`,
10880
11046
  return completion;
10881
11047
  }
10882
11048
  /**
10883
- * Report the failures this completion is the evidence for (each once). One
10884
- * that waited past `AWAITING_EVIDENCE_MS` is dropped instead: a completion
10885
- * that late says little about the runtime when it failed.
11049
+ * The failures this completion is the evidence for (each once), taken off
11050
+ * the waiting list. Their conversations are held from now until the
11051
+ * server has answered (`reporting`): no gate, and a run or turn of theirs
11052
+ * waiting for a slot waits for the answer, so it never starts on the input
11053
+ * the answer is about. Take them before the completed work's slot frees.
11054
+ * One that waited past `AWAITING_EVIDENCE_MS` is dropped instead: a
11055
+ * completion that late says little about the runtime when it failed.
10886
11056
  */
10887
- async reportDue(completion) {
11057
+ takeDue(completion) {
10888
11058
  const now2 = this.now();
10889
11059
  const due = [];
10890
11060
  for (const failure of [...this.awaitingEvidence.values()]) {
@@ -10894,10 +11064,49 @@ invite_id: ${invite.invite_id}`,
10894
11064
  }
10895
11065
  if (evidences(completion, failure)) {
10896
11066
  this.awaitingEvidence.delete(failure.conversationId);
11067
+ this.holdForReport(failure);
10897
11068
  due.push(failure);
10898
11069
  }
10899
11070
  }
10900
- for (const failure of due) await this.reportFailure(failure, "other_run_completed");
11071
+ return due;
11072
+ }
11073
+ /** Report what `takeDue` took, letting go of each conversation once its answer is in: what waits for it goes on (or was dropped). */
11074
+ async reportDue(due) {
11075
+ const held = [...due];
11076
+ try {
11077
+ while (held.length > 0) {
11078
+ const failure = held[0];
11079
+ await this.reportFailure(failure, "other_run_completed");
11080
+ held.shift();
11081
+ this.letGo([failure]);
11082
+ }
11083
+ } finally {
11084
+ this.letGo(held);
11085
+ }
11086
+ }
11087
+ /** A report for this failure is due: its conversation is held until `letGo` (counted: two reports out hold it until both are answered). */
11088
+ holdForReport(failure) {
11089
+ this.reporting.set(failure.conversationId, (this.reporting.get(failure.conversationId) ?? 0) + 1);
11090
+ }
11091
+ /**
11092
+ * The answers are in (or the report is off): those conversations are no
11093
+ * longer held for it, what waits for them goes on, and a card a cycle left
11094
+ * out meanwhile for a report is read again (only then: a wake for
11095
+ * anything else could spin while a turn runs).
11096
+ */
11097
+ letGo(failures) {
11098
+ if (failures.length === 0) return;
11099
+ for (const { conversationId } of failures) {
11100
+ const left = (this.reporting.get(conversationId) ?? 0) - 1;
11101
+ if (left > 0) this.reporting.set(conversationId, left);
11102
+ else this.reporting.delete(conversationId);
11103
+ }
11104
+ if (this.stopped) return;
11105
+ if (this.waiting.length > 0) void this.pump();
11106
+ if (this.heldWhileReporting) {
11107
+ this.heldWhileReporting = false;
11108
+ this.wakeFrom("more");
11109
+ }
10901
11110
  }
10902
11111
  /** An attempt of this kind failed for these conversations (whatever comes of it): see `failingSince`. */
10903
11112
  failedAttempt(kind, conversations) {
@@ -10950,17 +11159,24 @@ invite_id: ${invite.invite_id}`,
10950
11159
  failure.outcome !== "timed_out" && !this.checkedFailures.has(failure) && this.now() - failure.failedAt <= AWAITING_EVIDENCE_MS && [...failing].every((id) => id === failure.conversationId)
10951
11160
  );
10952
11161
  if (due.length === 0) return;
10953
- for (const failure of due) this.checkedFailures.add(failure);
10954
- const ok = await this.runtimeCheck();
10955
11162
  for (const failure of due) {
10956
- if (this.stopped || this.awaitingEvidence.get(failure.conversationId) !== failure) continue;
10957
- if (!ok) {
11163
+ this.checkedFailures.add(failure);
11164
+ this.holdForReport(failure);
11165
+ }
11166
+ try {
11167
+ const ok = await this.runtimeCheck();
11168
+ for (const failure of due) {
11169
+ if (this.stopped || this.awaitingEvidence.get(failure.conversationId) !== failure) continue;
11170
+ if (!ok) {
11171
+ this.awaitingEvidence.delete(failure.conversationId);
11172
+ continue;
11173
+ }
11174
+ if (![...failing].every((id) => id === failure.conversationId)) continue;
10958
11175
  this.awaitingEvidence.delete(failure.conversationId);
10959
- continue;
11176
+ await this.reportFailure(failure, "runtime_check");
10960
11177
  }
10961
- if (![...failing].every((id) => id === failure.conversationId)) continue;
10962
- this.awaitingEvidence.delete(failure.conversationId);
10963
- await this.reportFailure(failure, "runtime_check");
11178
+ } finally {
11179
+ this.letGo(due);
10964
11180
  }
10965
11181
  }
10966
11182
  /** Nothing of this kind is running or queued: the runtime check may be asked now. */
@@ -11034,10 +11250,29 @@ invite_id: ${invite.invite_id}`,
11034
11250
  next = this.rerun(failure, result.action === "hidden");
11035
11251
  }
11036
11252
  }
11037
- if (result.action === "paused") this.dropWaiting(failure.conversationId);
11253
+ if (result.action === "none" || result.action === "paused") this.holdFailed(failure, result.action);
11038
11254
  if (next === "stop" || next === "rerun_capped") this.activeSplit?.skip.add(failure.conversationId);
11039
11255
  this.emit({ type: "processing_failure", ...base, result, next });
11040
11256
  }
11257
+ /**
11258
+ * The server answered `none` or `paused`: the conversation is held at the
11259
+ * anchor the report read through, so no later cycle (a message elsewhere,
11260
+ * a wake frame, a reconnect, an `unanswered` re-wake, a turn's end) runs it
11261
+ * again on the same input. A newer message moves its anchor and lifts the
11262
+ * hold (`paceCards`); a card read meanwhile leaves the tray, and the hold
11263
+ * with it (`forgetCleared`). What waits for it does not start: a run or
11264
+ * reply turn queued at that anchor (after `paused`, the server's mute, any),
11265
+ * and a reply turn to start again after the gate.
11266
+ */
11267
+ holdFailed(failure, reason) {
11268
+ const id = failure.conversationId;
11269
+ const anchor = failure.anchor;
11270
+ this.pacer.holdFailed(`${CONVERSATION_KEY}${id}`, anchor, reason);
11271
+ this.dropWaiting(id, reason === "paused" ? null : anchor);
11272
+ this.turnRerunsAfterGate = this.turnRerunsAfterGate.filter(
11273
+ ({ reply }) => reply.conversationId !== id || reason !== "paused" && anchorFrom(reply.card) !== anchor
11274
+ );
11275
+ }
11041
11276
  /**
11042
11277
  * Run the conversation again after a `retry` or `hidden` answer, after the
11043
11278
  * others already queued, and say what was done. Its run or reply turn takes
@@ -11095,14 +11330,20 @@ invite_id: ${invite.invite_id}`,
11095
11330
  else void this.startReplyTurn(reply, texts, reruns);
11096
11331
  }
11097
11332
  }
11098
- /** A `paused` conversation: a run or turn still waiting for a slot for it does not start. */
11099
- dropWaiting(conversationId) {
11333
+ /**
11334
+ * A held conversation: a run or turn still waiting for a slot for it does
11335
+ * not start; with an `anchor`, only one for that anchor (one queued for a
11336
+ * newer message is new input, and goes on).
11337
+ */
11338
+ dropWaiting(conversationId, anchor) {
11100
11339
  for (const entry of [...this.waiting]) {
11101
11340
  const turn = entry.handle.turn;
11102
11341
  if (turn.kind !== "reply" || turn.conversationId !== conversationId) continue;
11342
+ if (anchor !== null && turn.anchor !== anchor) continue;
11103
11343
  this.waiting.splice(this.waiting.indexOf(entry), 1);
11104
11344
  this.tracker.end(entry.handle);
11105
11345
  entry.drop?.();
11346
+ this.failureCounts.startsHeld += 1;
11106
11347
  }
11107
11348
  }
11108
11349
  rememberGateAlone(conversationId) {
@@ -11151,6 +11392,7 @@ invite_id: ${invite.invite_id}`,
11151
11392
  turnsQueued: 0,
11152
11393
  heldByTurns: 0,
11153
11394
  paced: 0,
11395
+ heldAfterFailure: 0,
11154
11396
  chunksOk: 0,
11155
11397
  chunksFailed: 0,
11156
11398
  splitCallsOk: 0,
@@ -11246,6 +11488,7 @@ function timeoutOption(value, fallback, name) {
11246
11488
  return value;
11247
11489
  }
11248
11490
  function launchKey(launch) {
11491
+ if (launch === "held") return "heldAfterFailure";
11249
11492
  return launch === "started" ? "turnsStarted" : launch === "queued" ? "turnsQueued" : "turnsRefused";
11250
11493
  }
11251
11494
  function trayInvites(tray) {
@@ -18204,7 +18447,7 @@ import * as os4 from "os";
18204
18447
  import * as path7 from "path";
18205
18448
 
18206
18449
  // src/version.ts
18207
- var VERSION3 = "0.7.1";
18450
+ var VERSION3 = "0.7.2";
18208
18451
 
18209
18452
  // src/host.ts
18210
18453
  function codexHome() {