@evident-ai/cli 3.1.1-dev.c753555 → 3.1.1-dev.ca87eba

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
@@ -797,6 +797,16 @@ async function checkStatus(jsonMode) {
797
797
  exitCode: 1
798
798
  };
799
799
  }
800
+ if (response.status === 404) {
801
+ return {
802
+ ok: false,
803
+ endpoint: apiUrl,
804
+ authLabel: authLabelFor(credentials2),
805
+ reason: "endpoint_not_found",
806
+ error: `${apiUrl}/me returned HTTP 404 \u2014 that endpoint has no /me route, so it is probably missing the /v1 prefix. The credentials were NOT validated.`,
807
+ exitCode: 75
808
+ };
809
+ }
800
810
  if (response.status >= 500) {
801
811
  const serverMessage = await readErrorMessage(response);
802
812
  return {
@@ -3141,6 +3151,7 @@ var B2_ABANDONMENT_MIN_PINNED_MS = 3 * 6e4;
3141
3151
  var B2_ABANDONMENT_RECHECK_MS = HEARTBEAT_MS;
3142
3152
  var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
3143
3153
  var MAX_SUPERSEDED_CONVERSATIONS = 256;
3154
+ var MAX_IDENTICAL_REDRIVE_POLL_FAILURES = 5;
3144
3155
  var ChannelAuthError = class extends Error {
3145
3156
  constructor(message) {
3146
3157
  super(message);
@@ -3163,6 +3174,10 @@ function backoffDelay(attempt, policy) {
3163
3174
  function isRetryableStatus(status2) {
3164
3175
  return status2 === 429 || status2 >= 500 && status2 <= 599;
3165
3176
  }
3177
+ var VOLATILE_BODY_FIELD_PATTERN = /("(?:ref|requestId|request_id|traceId|trace_id)"\s*:\s*)"[^"]*"/gi;
3178
+ function normalizeRedrivePollFailureBody(body) {
3179
+ return body.replace(VOLATILE_BODY_FIELD_PATTERN, '$1"<redacted>"').replace(/\s+/g, " ").trim().slice(0, 200);
3180
+ }
3166
3181
  var ChannelDriver = class _ChannelDriver {
3167
3182
  agentId;
3168
3183
  port;
@@ -3294,6 +3309,66 @@ var ChannelDriver = class _ChannelDriver {
3294
3309
  * ADR-0047's own "unreachable ⇒ bounded" rule). Cleared on any other outcome.
3295
3310
  */
3296
3311
  redriveUnresolvedSince = /* @__PURE__ */ new Map();
3312
+ /**
3313
+ * Consecutive-identical-poll-failure streak for the re-drive fence (#1348),
3314
+ * keyed by Evident **message id** (not session) so `clearRedriveUnresolved`
3315
+ * can drop it with the other two trackers and it cannot leak. `sessionId` is
3316
+ * carried inside the entry, not the key: a session change is a different
3317
+ * situation and resets the streak, which gives the `(sessionId, message.id)`
3318
+ * pairing #1348 asks for without a composite map key.
3319
+ */
3320
+ redrivePollFailures = /* @__PURE__ */ new Map();
3321
+ /**
3322
+ * "Already emitted `redrive_outcome_unreported` for THIS (message, outcome)
3323
+ * streak" (Class B, #1340: the runner DECIDED reattach/settle/fail_permanent
3324
+ * but its own PATCH to record it failed — distinct from Class A's
3325
+ * `redrive_poll_failed`, where opencode itself can't be observed). Keyed by
3326
+ * message id, valued by the outcome currently failing to report, so a
3327
+ * change of outcome starts a fresh signal. Cleared by
3328
+ * `clearRedriveUnresolved` the instant either PATCH succeeds.
3329
+ */
3330
+ redriveOutcomeUnreportedSignalled = /* @__PURE__ */ new Map();
3331
+ /**
3332
+ * First `now()` a Class B outcome PATCH (reattach/settle/fail_permanent) was
3333
+ * observed to fail for this message (#1366's failure-window trip arm,
3334
+ * `boundRedriveOutcome`). Duration, not a tick count — bounded by the
3335
+ * existing `pausedMaxWaitMs` window (reusing the knob, not a new constant).
3336
+ * Cleared by `clearRedriveUnresolved` the instant the original PATCH
3337
+ * succeeds.
3338
+ */
3339
+ redriveOutcomeFailingSince = /* @__PURE__ */ new Map();
3340
+ /**
3341
+ * "Already posted `redrive_outcome_abandoned` with `reported: false` for this
3342
+ * row" (#1366) — the bound tripped but the terminal `markFailed` fallback ALSO
3343
+ * failed (the route-level fault of G2), so every following tick re-attempts
3344
+ * the same terminal PATCH. Guards that quiet retry from re-signalling on
3345
+ * every tick. Cleared by `clearRedriveUnresolved`.
3346
+ */
3347
+ redriveOutcomeAbandonedSignalled = /* @__PURE__ */ new Set();
3348
+ /**
3349
+ * "Already emitted `dispatch_not_started` for THIS (message, branch) streak"
3350
+ * (#1340). Valued by the branch currently firing, so a row that moves between
3351
+ * exits re-signals — the move IS the finding. Cleared only on a CONFIRMED
3352
+ * dispatch, never on the fence's decision to dispatch: `clearRedriveUnresolved`
3353
+ * runs on that decision (`resolveRedriveUnresolved`), so clearing there would
3354
+ * re-signal on every one of the 15h of re-dispatch attempts #1110 made.
3355
+ */
3356
+ dispatchNotStartedSignalled = /* @__PURE__ */ new Map();
3357
+ /**
3358
+ * Consecutive-UNCONFIRMED-dispatch streak for a `pending` row with NO stored
3359
+ * `opencode_message_id` yet — i.e. one that has never even reached the
3360
+ * re-drive fence above. `sendPromptAsync`'s POST may 2xx, but its own
3361
+ * read-back retries can never confirm the assigned id when the session's
3362
+ * message list is PERMANENTLY unreadable (e.g. a corrupted local opencode
3363
+ * SQLite DB, #1345/#1348's exact fault, just hit BEFORE the row is ever
3364
+ * dispatched instead of after). Unlike an already-dispatched row, THIS row has
3365
+ * no other safety net at all: the lifecycle cron only reclaims `status =
3366
+ * 'processing'` rows, and a row stuck here never reaches `processing`. Keyed
3367
+ * by message id, carrying `sessionId` so a session change (a fresh one bound
3368
+ * after abandonment) starts a new streak rather than inheriting the old
3369
+ * session's count — same shape as `redrivePollFailures` above.
3370
+ */
3371
+ unconfirmedDispatchFailures = /* @__PURE__ */ new Map();
3297
3372
  /**
3298
3373
  * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
3299
3374
  * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
@@ -3618,7 +3693,7 @@ var ChannelDriver = class _ChannelDriver {
3618
3693
  * @returns the count of messages NEWLY dispatched (not already in-flight).
3619
3694
  */
3620
3695
  async processConversation(conv) {
3621
- const { sessionId, refusedSessionId } = await this.ensureSession(conv);
3696
+ const { sessionId, refusedSessionId, created: sessionCreated } = await this.ensureSession(conv);
3622
3697
  const messages = await this.getPendingMessages(conv.id);
3623
3698
  let dispatched = 0;
3624
3699
  let skippedAlreadyDispatched = 0;
@@ -3634,7 +3709,10 @@ var ChannelDriver = class _ChannelDriver {
3634
3709
  continue;
3635
3710
  }
3636
3711
  if (message.opencode_message_id) {
3637
- const outcome = await this.resolveRedrive(conv, sessionId, message, refusedSessionId);
3712
+ const outcome = await this.resolveRedrive(conv, sessionId, message, sessionCreated);
3713
+ if (outcome === "abandoned") {
3714
+ continue;
3715
+ }
3638
3716
  if (outcome !== "dispatch") {
3639
3717
  break;
3640
3718
  }
@@ -3668,6 +3746,7 @@ var ChannelDriver = class _ChannelDriver {
3668
3746
  conversation_id: conv.id,
3669
3747
  message_id: message.id
3670
3748
  });
3749
+ this.signalDispatchNotStarted(conv, message, "session_deleted_race");
3671
3750
  break;
3672
3751
  }
3673
3752
  if (exists === null) {
@@ -3677,6 +3756,7 @@ var ChannelDriver = class _ChannelDriver {
3677
3756
  conversation_id: conv.id,
3678
3757
  message_id: message.id
3679
3758
  });
3759
+ this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
3680
3760
  break;
3681
3761
  }
3682
3762
  const errorMessage = err instanceof Error ? err.message : String(err);
@@ -3695,6 +3775,7 @@ var ChannelDriver = class _ChannelDriver {
3695
3775
  conversation_id: conv.id,
3696
3776
  message_id: message.id
3697
3777
  });
3778
+ this.signalDispatchNotStarted(conv, message, "failure_unreported");
3698
3779
  });
3699
3780
  this.log({
3700
3781
  level: "error",
@@ -3705,14 +3786,40 @@ var ChannelDriver = class _ChannelDriver {
3705
3786
  break;
3706
3787
  }
3707
3788
  if (opencodeMessageId === null) {
3789
+ const streak = this.recordUnconfirmedDispatch(message.id, sessionId);
3790
+ if (streak < MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
3791
+ this.log({
3792
+ level: "warn",
3793
+ message: `Message ${message.id.slice(0, 8)} dispatched but its opencode id could not be read back (${streak}/${MAX_IDENTICAL_REDRIVE_POLL_FAILURES}) \u2014 leaving un-tracked to retry next tick`,
3794
+ conversation_id: conv.id,
3795
+ message_id: message.id
3796
+ });
3797
+ this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
3798
+ continue;
3799
+ }
3800
+ this.unconfirmedDispatchFailures.delete(message.id);
3801
+ this.sessions.delete(conv.id);
3802
+ this.supersede(conv.id, sessionId);
3803
+ const errorMessage = `OpenCode accepted this message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
3708
3804
  this.log({
3709
- level: "warn",
3710
- message: `Message ${message.id.slice(0, 8)} dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next tick`,
3805
+ level: "error",
3806
+ message: errorMessage,
3711
3807
  conversation_id: conv.id,
3712
3808
  message_id: message.id
3713
3809
  });
3714
- continue;
3810
+ await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
3811
+ this.log({
3812
+ level: "warn",
3813
+ message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
3814
+ conversation_id: conv.id,
3815
+ message_id: message.id
3816
+ });
3817
+ this.signalDispatchNotStarted(conv, message, "abandon_unreported");
3818
+ });
3819
+ break;
3715
3820
  }
3821
+ this.unconfirmedDispatchFailures.delete(message.id);
3822
+ this.dispatchNotStartedSignalled.delete(message.id);
3716
3823
  this.dispatched.add(message.id);
3717
3824
  this.registerInFlight(conv, sessionId, message, opencodeMessageId);
3718
3825
  dispatched += 1;
@@ -3733,21 +3840,29 @@ var ChannelDriver = class _ChannelDriver {
3733
3840
  * INJECTED `fetchImpl` — NOT the imported `getSessionMessages` helper, which
3734
3841
  * hits the global `fetch` and would bypass the same override every other
3735
3842
  * opencode poll in this file respects. Mirrors `readoptProcessing`'s own
3736
- * snapshot fetch (`:3081-3111`). `null` = unreadable (non-OK response,
3737
- * non-array body, or a network exception) — treated as "can't observe",
3738
- * never as "confirmed gone".
3843
+ * snapshot fetch (`:3081-3111`).
3844
+ *
3845
+ * Returns `{ ok: true, messages }` on a readable snapshot, or
3846
+ * `{ ok: false, signature }` on failure — `signature` is a string that
3847
+ * repeats across attempts for the SAME underlying fault (used by the
3848
+ * consecutive-identical-failure bound, #1348), or `null` for a thrown
3849
+ * exception, which is NOT countable toward that bound (a network blip / an
3850
+ * opencode restart also throws identically every tick, and must keep
3851
+ * retrying unbounded rather than ever being treated as permanent).
3739
3852
  */
3740
3853
  async pollSessionMessagesForRedrive(conv, message, sessionId) {
3741
3854
  try {
3742
3855
  const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
3743
3856
  if (!res.ok) {
3857
+ const rawBody = await res.text();
3858
+ const normalized = normalizeRedrivePollFailureBody(rawBody);
3744
3859
  this.log({
3745
3860
  level: "warn",
3746
- message: `Re-drive: polling session ${sessionId.slice(0, 8)} for message ${message.id.slice(0, 8)} returned HTTP ${res.status} \u2014 treating as unreadable this tick`,
3861
+ message: `Re-drive: polling session ${sessionId.slice(0, 8)} for message ${message.id.slice(0, 8)} returned HTTP ${res.status}${normalized ? `: ${normalized}` : ""} \u2014 treating as unreadable this tick`,
3747
3862
  conversation_id: conv.id,
3748
3863
  message_id: message.id
3749
3864
  });
3750
- return null;
3865
+ return { ok: false, signature: `HTTP ${res.status}${normalized ? `: ${normalized}` : ""}` };
3751
3866
  }
3752
3867
  const body = await res.json();
3753
3868
  if (!Array.isArray(body)) {
@@ -3757,9 +3872,9 @@ var ChannelDriver = class _ChannelDriver {
3757
3872
  conversation_id: conv.id,
3758
3873
  message_id: message.id
3759
3874
  });
3760
- return null;
3875
+ return { ok: false, signature: "non-array message body" };
3761
3876
  }
3762
- return body;
3877
+ return { ok: true, messages: body };
3763
3878
  } catch (err) {
3764
3879
  this.log({
3765
3880
  level: "warn",
@@ -3767,7 +3882,7 @@ var ChannelDriver = class _ChannelDriver {
3767
3882
  conversation_id: conv.id,
3768
3883
  message_id: message.id
3769
3884
  });
3770
- return null;
3885
+ return { ok: false, signature: null };
3771
3886
  }
3772
3887
  }
3773
3888
  /**
@@ -3779,21 +3894,34 @@ var ChannelDriver = class _ChannelDriver {
3779
3894
  * without this fence the drain loop would re-`prompt_async` the SAME turn a
3780
3895
  * second time against live GitHub state. Mirrors `readoptOne`'s job for the
3781
3896
  * `processing` re-adopt path, but simpler: no b1/b2 preamble cross-check is
3782
- * needed here because `refusedSessionId` already handles the one case
3783
- * (#553 abandoned session) that path exists for.
3897
+ * needed here because `sessionCreated` already handles the cases (a #553
3898
+ * abandoned session, a #190 vanished one) that path exists for.
3784
3899
  *
3785
- * Only `ChannelAuthError` propagates; every other failure resolves to
3786
- * `unresolved` and is retried whole on the next ~2s drain tick.
3900
+ * Only `ChannelAuthError` propagates. A poll that fails identically
3901
+ * `MAX_IDENTICAL_REDRIVE_POLL_FAILURES` times in a row reports the message
3902
+ * failed instead of retrying it (#1348) — SEPARATE from, not a replacement
3903
+ * for, `resolveRedriveUnresolved`'s own `pausedMaxWaitMs` bound below. Every
3904
+ * other failure resolves to `unresolved` and is retried whole on the next
3905
+ * ~2s drain tick.
3787
3906
  */
3788
- async resolveRedrive(conv, sessionId, message, refusedSessionId) {
3907
+ async resolveRedrive(conv, sessionId, message, sessionCreated) {
3789
3908
  const ocId = message.opencode_message_id ?? null;
3790
- if (refusedSessionId) {
3909
+ if (sessionCreated) {
3791
3910
  this.clearRedriveUnresolved(message.id);
3792
3911
  void this.postSignal(conv.id, message.id, "redrive_redispatched");
3793
3912
  return "dispatch";
3794
3913
  }
3795
- const messages = await this.pollSessionMessagesForRedrive(conv, message, sessionId);
3796
- if (messages == null || messages.length === 0) {
3914
+ const polled = await this.pollSessionMessagesForRedrive(conv, message, sessionId);
3915
+ if (!polled.ok) {
3916
+ const streak = this.recordRedrivePollFailure(message.id, sessionId, polled.signature);
3917
+ if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES && polled.signature !== null) {
3918
+ return this.failRedrivePollPermanent(conv, sessionId, message, polled.signature, streak);
3919
+ }
3920
+ return this.resolveRedriveUnresolved(conv, message);
3921
+ }
3922
+ this.redrivePollFailures.delete(message.id);
3923
+ const messages = polled.messages;
3924
+ if (messages.length === 0) {
3797
3925
  return this.resolveRedriveUnresolved(conv, message);
3798
3926
  }
3799
3927
  const state = messageRunState(messages, ocId ?? "");
@@ -3860,7 +3988,8 @@ var ChannelDriver = class _ChannelDriver {
3860
3988
  conversation_id: conv.id,
3861
3989
  message_id: message.id
3862
3990
  });
3863
- return "unresolved";
3991
+ const bound = await this.boundRedriveOutcome(conv, message, "reattach");
3992
+ return bound === "abandoned" ? "abandoned" : "unresolved";
3864
3993
  }
3865
3994
  this.clearRedriveUnresolved(message.id);
3866
3995
  this.registerReadopted(conv, sessionId, message, ocId ?? "", anchorMs);
@@ -3920,19 +4049,62 @@ var ChannelDriver = class _ChannelDriver {
3920
4049
  conversation_id: conv.id,
3921
4050
  message_id: message.id
3922
4051
  });
3923
- return "unresolved";
4052
+ const bound = await this.boundRedriveOutcome(conv, message, "settle");
4053
+ return bound === "abandoned" ? "abandoned" : "unresolved";
3924
4054
  }
3925
4055
  this.clearRedriveUnresolved(message.id);
3926
4056
  void this.postSignal(conv.id, message.id, "redrive_settled");
3927
4057
  return "settled";
3928
4058
  }
4059
+ /**
4060
+ * The permanent-failure outcome (#1348): the fence's own poll of this session
4061
+ * failed with the SAME opencode-answered signature
4062
+ * `MAX_IDENTICAL_REDRIVE_POLL_FAILURES` times in a row — a transient blip
4063
+ * would have varied or eventually cleared (see `pollSessionMessagesForRedrive`
4064
+ * and `recordRedrivePollFailure`), so this is a durable fault (e.g. #1345's
4065
+ * corrupted opencode session) rather than something worth retrying forever.
4066
+ * Mirrors `settleRedrive`'s error discipline: no `usage`/`failure` args to
4067
+ * `markFailed` (no opencode snapshot to extract them from — this poll never
4068
+ * got a readable one).
4069
+ */
4070
+ async failRedrivePollPermanent(conv, sessionId, message, signature, streak) {
4071
+ this.log({
4072
+ level: "error",
4073
+ message: `Re-drive: message ${message.id.slice(0, 8)} (session ${sessionId.slice(0, 8)}) failed to poll with the identical signature "${signature}" ${streak} times in a row \u2014 reporting the message failed instead of retrying forever`,
4074
+ conversation_id: conv.id,
4075
+ message_id: message.id
4076
+ });
4077
+ try {
4078
+ await this.markFailed(
4079
+ conv.id,
4080
+ message.id,
4081
+ sessionId,
4082
+ `The runner could not read this conversation's state from OpenCode (${signature}). The same failure repeated ${streak} times in a row, so the message was not retried further.`
4083
+ );
4084
+ } catch (err) {
4085
+ if (err instanceof ChannelAuthError) throw err;
4086
+ this.log({
4087
+ level: "warn",
4088
+ message: `Re-drive: failed to report message ${message.id.slice(0, 8)} permanently failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
4089
+ conversation_id: conv.id,
4090
+ message_id: message.id
4091
+ });
4092
+ const bound = await this.boundRedriveOutcome(conv, message, "fail_permanent");
4093
+ return bound === "abandoned" ? "abandoned" : "unresolved";
4094
+ }
4095
+ this.clearRedriveUnresolved(message.id);
4096
+ void this.postSignal(conv.id, message.id, "redrive_poll_failed");
4097
+ return "settled";
4098
+ }
3929
4099
  /**
3930
4100
  * The bounded `unresolved` outcome (Task 3.4): opencode's state could not be
3931
4101
  * observed (snapshot unreadable/empty, or `isSessionOngoing` returned `null`).
3932
- * A `pending` row is invisible to every cron arm (all require `status =
3933
- * 'processing'`), so an indefinitely-`unresolved` row would be stranded with
3934
- * nothing driving it bound it to the existing `pausedMaxWaitMs` window
3935
- * (reusing the knob, not a new constant) and take `dispatch` once elapsed.
4102
+ * A `pending` row is swept by the server's own `PENDING_MAX_AGE_MS` (24h,
4103
+ * #1368) cron arm, but that is a day-scale backstop this local bound acts
4104
+ * in minutes so the row (and the conversation it starves, per the ordering
4105
+ * invariant below) isn't left stranded for that long. Bound to the existing
4106
+ * `pausedMaxWaitMs` window (reusing the knob, not a new constant); takes
4107
+ * `dispatch` once elapsed.
3936
4108
  */
3937
4109
  resolveRedriveUnresolved(conv, message) {
3938
4110
  const now = this.now();
@@ -3951,10 +4123,153 @@ var ChannelDriver = class _ChannelDriver {
3951
4123
  }
3952
4124
  return "unresolved";
3953
4125
  }
3954
- /** Clear both `unresolved`-bound trackers for a row (any non-`unresolved` outcome). */
4126
+ /** Clear all `unresolved`/failure-streak trackers for a row (any non-`unresolved` outcome). */
3955
4127
  clearRedriveUnresolved(messageId) {
3956
4128
  this.redriveUnresolvedSince.delete(messageId);
3957
4129
  this.redriveUnresolvedSignalled.delete(messageId);
4130
+ this.redrivePollFailures.delete(messageId);
4131
+ this.redriveOutcomeUnreportedSignalled.delete(messageId);
4132
+ this.redriveOutcomeFailingSince.delete(messageId);
4133
+ this.redriveOutcomeAbandonedSignalled.delete(messageId);
4134
+ }
4135
+ /**
4136
+ * #1340: the dispatch loop reached a message and did NOT start a turn. Fires at
4137
+ * most once per (message, branch) streak — a wedged row is re-tried every tick,
4138
+ * and the per-tick count is already carried by the co-occurring
4139
+ * `redrive_unresolved`/`redrive_redispatched` signals.
4140
+ */
4141
+ signalDispatchNotStarted(conv, message, branch) {
4142
+ if (this.dispatchNotStartedSignalled.get(message.id) === branch) return;
4143
+ this.dispatchNotStartedSignalled.set(message.id, branch);
4144
+ void this.postSignal(conv.id, message.id, "dispatch_not_started", { branch });
4145
+ }
4146
+ /**
4147
+ * Class B (#1340): the runner DECIDED an outcome (reattach/settle/fail_permanent)
4148
+ * but its own PATCH to record it failed. Fires at most once per (message,
4149
+ * outcome) streak, and only while `boundRedriveOutcome` has not yet tripped —
4150
+ * once it trips, `redrive_outcome_abandoned` takes over reporting for the row
4151
+ * (#1366).
4152
+ */
4153
+ signalRedriveOutcomeUnreported(conv, message, outcome) {
4154
+ if (this.redriveOutcomeUnreportedSignalled.get(message.id) === outcome) return;
4155
+ this.redriveOutcomeUnreportedSignalled.set(message.id, outcome);
4156
+ void this.postSignal(conv.id, message.id, "redrive_outcome_unreported", {
4157
+ attempted_outcome: outcome
4158
+ });
4159
+ }
4160
+ /**
4161
+ * The runner-authored, honest error text for the terminal fallback a tripped
4162
+ * `boundRedriveOutcome` sends. Distinguishable per outcome and truthful about
4163
+ * what actually happened — the `settle`/done case must say the turn finished
4164
+ * but its result could not be recorded, never that the runner stopped
4165
+ * responding (that would be a lie for this shape, see #1366's "why this ships").
4166
+ */
4167
+ static REDRIVE_ABANDON_ERROR = {
4168
+ reattach: "your runner could not record that this message had started, so it was given up on",
4169
+ settle: "your runner finished this message but could not record the result, so the reply could not be delivered",
4170
+ fail_permanent: "the runner could not read this conversation's state from OpenCode, and could not record that failure either, so the message was given up on"
4171
+ };
4172
+ /**
4173
+ * Bound for Class B (#1340, #1366): the runner DECIDED an outcome but its own
4174
+ * PATCH to record it failed. Two independent trip arms (either sufficient):
4175
+ * (1) this failure streak has lasted `pausedMaxWaitMs` — DURATION, not a tick
4176
+ * count, reusing the knob `resolveRedriveUnresolved` already established; (2)
4177
+ * the turn's `processing_started_at` age has crossed
4178
+ * `ABSOLUTE_MAX_PROCESSING_MS` — durable and restart-surviving, since arm (1)'s
4179
+ * in-memory streak resets on a scale-to-zero restart.
4180
+ *
4181
+ * INVARIANT — a tripped bound never suppresses the original outcome attempt;
4182
+ * it only adds a fallback after that attempt has failed again. This is only
4183
+ * ever reached from inside the catch of the ORIGINAL outcome PATCH, which is
4184
+ * attempted first on every tick whether or not this bound tripped before —
4185
+ * there is no give-up latch that would short-circuit it. That is what lets a
4186
+ * route-level fault that heals later still deliver the turn's real
4187
+ * `done`/`failed` payload: once the original PATCH succeeds again, this
4188
+ * helper is never entered and the row settles with its real result.
4189
+ */
4190
+ async boundRedriveOutcome(conv, message, outcome) {
4191
+ const now = this.now();
4192
+ const since = this.redriveOutcomeFailingSince.get(message.id);
4193
+ if (since === void 0) this.redriveOutcomeFailingSince.set(message.id, now);
4194
+ const durationTripped = now - (since ?? now) >= this.pausedMaxWaitMs;
4195
+ const parsed = message.processing_started_at ? Date.parse(message.processing_started_at) : NaN;
4196
+ const absoluteAgeTripped = !Number.isNaN(parsed) && now - parsed >= ABSOLUTE_MAX_PROCESSING_MS;
4197
+ if (!durationTripped && !absoluteAgeTripped) {
4198
+ this.signalRedriveOutcomeUnreported(conv, message, outcome);
4199
+ return "retry";
4200
+ }
4201
+ const arm = durationTripped ? "failure_window" : "absolute_age";
4202
+ try {
4203
+ await this.markFailed(
4204
+ conv.id,
4205
+ message.id,
4206
+ void 0,
4207
+ _ChannelDriver.REDRIVE_ABANDON_ERROR[outcome]
4208
+ );
4209
+ } catch (err) {
4210
+ if (err instanceof ChannelAuthError) throw err;
4211
+ this.log({
4212
+ level: "warn",
4213
+ message: `Re-drive bound: fallback markFailed for message ${message.id.slice(0, 8)} also failed (arm ${arm}, will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
4214
+ conversation_id: conv.id,
4215
+ message_id: message.id
4216
+ });
4217
+ if (!this.redriveOutcomeAbandonedSignalled.has(message.id)) {
4218
+ this.redriveOutcomeAbandonedSignalled.add(message.id);
4219
+ void this.postSignal(conv.id, message.id, "redrive_outcome_abandoned", {
4220
+ attempted_outcome: outcome,
4221
+ reported: false,
4222
+ arm
4223
+ });
4224
+ }
4225
+ return "retry";
4226
+ }
4227
+ this.clearRedriveUnresolved(message.id);
4228
+ void this.postSignal(conv.id, message.id, "redrive_outcome_abandoned", {
4229
+ attempted_outcome: outcome,
4230
+ reported: true,
4231
+ arm
4232
+ });
4233
+ return "abandoned";
4234
+ }
4235
+ /**
4236
+ * Record one poll outcome toward the re-drive fence's consecutive-identical-
4237
+ * failure streak (#1348) and return the resulting count. `signature === null`
4238
+ * (a thrown exception, H1) always clears the streak and returns `0` — it is
4239
+ * never countable. Otherwise the streak continues only when BOTH the session
4240
+ * and the signature match the previous failure; anything else (a different
4241
+ * session, or the same session failing a DIFFERENT way) starts a fresh streak
4242
+ * at `1`.
4243
+ */
4244
+ recordRedrivePollFailure(messageId, sessionId, signature) {
4245
+ if (signature === null) {
4246
+ this.redrivePollFailures.delete(messageId);
4247
+ return 0;
4248
+ }
4249
+ const existing = this.redrivePollFailures.get(messageId);
4250
+ if (existing && existing.sessionId === sessionId && existing.signature === signature) {
4251
+ existing.count += 1;
4252
+ return existing.count;
4253
+ }
4254
+ this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
4255
+ return 1;
4256
+ }
4257
+ /**
4258
+ * Record one UNCONFIRMED-dispatch outcome (a `pending` row with no stored
4259
+ * `opencode_message_id` whose `sendPromptAsync` returned `null`) toward the
4260
+ * bound in `processConversation`'s dispatch loop, and return the resulting
4261
+ * count. Mirrors `recordRedrivePollFailure`'s session-scoping: a session
4262
+ * change starts a fresh streak at `1` rather than inheriting the old one's
4263
+ * count, since a new session is a genuinely different attempt.
4264
+ */
4265
+ recordUnconfirmedDispatch(messageId, sessionId) {
4266
+ const existing = this.unconfirmedDispatchFailures.get(messageId);
4267
+ if (existing && existing.sessionId === sessionId) {
4268
+ existing.count += 1;
4269
+ return existing.count;
4270
+ }
4271
+ this.unconfirmedDispatchFailures.set(messageId, { sessionId, count: 1 });
4272
+ return 1;
3958
4273
  }
3959
4274
  /**
3960
4275
  * Record that `sessionId` is no longer a valid binding for `conversationId`
@@ -3980,6 +4295,13 @@ var ChannelDriver = class _ChannelDriver {
3980
4295
  * `refusedSessionId` is set when the #553 guard fired — i.e. the persisted
3981
4296
  * binding was an id this runner had abandoned, so a resurrection genuinely
3982
4297
  * happened and a fresh session was bound instead. The caller reports it.
4298
+ *
4299
+ * `created` says the returned session was made JUST NOW, so it provably holds
4300
+ * no prior turn. The re-drive fence needs that as CONTRARY evidence ("nothing
4301
+ * to reconcile against") — distinct from the ambiguous "I polled and saw an
4302
+ * empty transcript", which stays a deferral. Keep it separate from
4303
+ * `refusedSessionId`: only the latter means a #553 resurrection happened, and
4304
+ * only it may drive the `session_superseded` signal.
3983
4305
  */
3984
4306
  async ensureSession(conv) {
3985
4307
  const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
@@ -3990,7 +4312,11 @@ var ChannelDriver = class _ChannelDriver {
3990
4312
  conversation_id: conv.id
3991
4313
  });
3992
4314
  this.sessions.delete(conv.id);
3993
- return { sessionId: await this.createAndBindSession(conv.id), refusedSessionId: bound };
4315
+ return {
4316
+ sessionId: await this.createAndBindSession(conv.id),
4317
+ refusedSessionId: bound,
4318
+ created: true
4319
+ };
3994
4320
  }
3995
4321
  if (bound) {
3996
4322
  const exists = await sessionExists(this.port, bound);
@@ -4001,12 +4327,12 @@ var ChannelDriver = class _ChannelDriver {
4001
4327
  conversation_id: conv.id
4002
4328
  });
4003
4329
  this.sessions.delete(conv.id);
4004
- return { sessionId: await this.createAndBindSession(conv.id) };
4330
+ return { sessionId: await this.createAndBindSession(conv.id), created: true };
4005
4331
  }
4006
4332
  this.sessions.set(conv.id, bound);
4007
- return { sessionId: bound };
4333
+ return { sessionId: bound, created: false };
4008
4334
  }
4009
- return { sessionId: await this.createAndBindSession(conv.id) };
4335
+ return { sessionId: await this.createAndBindSession(conv.id), created: true };
4010
4336
  }
4011
4337
  /**
4012
4338
  * Create a fresh OpenCode session for a conversation, cache the binding, and
@@ -5092,15 +5418,39 @@ var ChannelDriver = class _ChannelDriver {
5092
5418
  }
5093
5419
  if (ocId === null) {
5094
5420
  this.awaitingReadopt.delete(row.id);
5421
+ const streak = this.recordUnconfirmedDispatch(row.id, sessionId);
5422
+ if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
5423
+ this.unconfirmedDispatchFailures.delete(row.id);
5424
+ this.sessions.delete(readoptConv.id);
5425
+ this.supersede(readoptConv.id, sessionId);
5426
+ const errorMessage = `OpenCode accepted this re-dispatched message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
5427
+ this.log({
5428
+ level: "error",
5429
+ message: errorMessage,
5430
+ conversation_id: row.conversation_id,
5431
+ message_id: row.id
5432
+ });
5433
+ await this.markFailed(row.conversation_id, row.id, null, errorMessage).catch((markErr) => {
5434
+ this.log({
5435
+ level: "warn",
5436
+ message: `markFailed PATCH for message ${row.id.slice(0, 8)} (conversation ${row.conversation_id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
5437
+ conversation_id: row.conversation_id,
5438
+ message_id: row.id
5439
+ });
5440
+ });
5441
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
5442
+ return;
5443
+ }
5095
5444
  this.log({
5096
5445
  level: "warn",
5097
- message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next drain`,
5446
+ message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back (${streak}/${MAX_IDENTICAL_REDRIVE_POLL_FAILURES}) \u2014 leaving un-tracked to retry next drain`,
5098
5447
  conversation_id: row.conversation_id,
5099
5448
  message_id: row.id
5100
5449
  });
5101
5450
  void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
5102
5451
  return;
5103
5452
  }
5453
+ this.unconfirmedDispatchFailures.delete(row.id);
5104
5454
  this.registerReadopted(readoptConv, sessionId, readoptMessage, ocId, this.processedAtMs(row));
5105
5455
  this.dispatched.add(row.id);
5106
5456
  this.readopted.add(row.id);