@evident-ai/cli 3.1.1-dev.df276eb → 3.1.1-dev.eec1cca

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 ?? "");
@@ -3854,13 +3982,23 @@ var ChannelDriver = class _ChannelDriver {
3854
3982
  await this.markProcessing(conv.id, message.id, sessionId, ocId, title);
3855
3983
  } catch (err) {
3856
3984
  if (err instanceof ChannelAuthError) throw err;
3857
- this.log({
3858
- level: "warn",
3859
- message: `Re-drive: failed to restore message ${message.id.slice(0, 8)} to processing (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3860
- conversation_id: conv.id,
3861
- message_id: message.id
3862
- });
3863
- return "unresolved";
3985
+ if (err instanceof ChannelTerminalError) {
3986
+ this.log({
3987
+ level: "error",
3988
+ message: `Re-drive: the server definitively refused to restore message ${message.id.slice(0, 8)} to processing (terminal HTTP ${err.status} \u2014 the row is gone or the update was rejected); NOT reporting a re-attach`,
3989
+ conversation_id: conv.id,
3990
+ message_id: message.id
3991
+ });
3992
+ } else {
3993
+ this.log({
3994
+ level: "warn",
3995
+ message: `Re-drive: failed to restore message ${message.id.slice(0, 8)} to processing (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3996
+ conversation_id: conv.id,
3997
+ message_id: message.id
3998
+ });
3999
+ }
4000
+ const bound = await this.boundRedriveOutcome(conv, message, "reattach");
4001
+ return bound === "abandoned" ? "abandoned" : "unresolved";
3864
4002
  }
3865
4003
  this.clearRedriveUnresolved(message.id);
3866
4004
  this.registerReadopted(conv, sessionId, message, ocId ?? "", anchorMs);
@@ -3920,19 +4058,62 @@ var ChannelDriver = class _ChannelDriver {
3920
4058
  conversation_id: conv.id,
3921
4059
  message_id: message.id
3922
4060
  });
3923
- return "unresolved";
4061
+ const bound = await this.boundRedriveOutcome(conv, message, "settle");
4062
+ return bound === "abandoned" ? "abandoned" : "unresolved";
3924
4063
  }
3925
4064
  this.clearRedriveUnresolved(message.id);
3926
4065
  void this.postSignal(conv.id, message.id, "redrive_settled");
3927
4066
  return "settled";
3928
4067
  }
4068
+ /**
4069
+ * The permanent-failure outcome (#1348): the fence's own poll of this session
4070
+ * failed with the SAME opencode-answered signature
4071
+ * `MAX_IDENTICAL_REDRIVE_POLL_FAILURES` times in a row — a transient blip
4072
+ * would have varied or eventually cleared (see `pollSessionMessagesForRedrive`
4073
+ * and `recordRedrivePollFailure`), so this is a durable fault (e.g. #1345's
4074
+ * corrupted opencode session) rather than something worth retrying forever.
4075
+ * Mirrors `settleRedrive`'s error discipline: no `usage`/`failure` args to
4076
+ * `markFailed` (no opencode snapshot to extract them from — this poll never
4077
+ * got a readable one).
4078
+ */
4079
+ async failRedrivePollPermanent(conv, sessionId, message, signature, streak) {
4080
+ this.log({
4081
+ level: "error",
4082
+ 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`,
4083
+ conversation_id: conv.id,
4084
+ message_id: message.id
4085
+ });
4086
+ try {
4087
+ await this.markFailed(
4088
+ conv.id,
4089
+ message.id,
4090
+ sessionId,
4091
+ `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.`
4092
+ );
4093
+ } catch (err) {
4094
+ if (err instanceof ChannelAuthError) throw err;
4095
+ this.log({
4096
+ level: "warn",
4097
+ 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)}`,
4098
+ conversation_id: conv.id,
4099
+ message_id: message.id
4100
+ });
4101
+ const bound = await this.boundRedriveOutcome(conv, message, "fail_permanent");
4102
+ return bound === "abandoned" ? "abandoned" : "unresolved";
4103
+ }
4104
+ this.clearRedriveUnresolved(message.id);
4105
+ void this.postSignal(conv.id, message.id, "redrive_poll_failed");
4106
+ return "settled";
4107
+ }
3929
4108
  /**
3930
4109
  * The bounded `unresolved` outcome (Task 3.4): opencode's state could not be
3931
4110
  * 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.
4111
+ * A `pending` row is swept by the server's own `PENDING_MAX_AGE_MS` (24h,
4112
+ * #1368) cron arm, but that is a day-scale backstop this local bound acts
4113
+ * in minutes so the row (and the conversation it starves, per the ordering
4114
+ * invariant below) isn't left stranded for that long. Bound to the existing
4115
+ * `pausedMaxWaitMs` window (reusing the knob, not a new constant); takes
4116
+ * `dispatch` once elapsed.
3936
4117
  */
3937
4118
  resolveRedriveUnresolved(conv, message) {
3938
4119
  const now = this.now();
@@ -3951,10 +4132,153 @@ var ChannelDriver = class _ChannelDriver {
3951
4132
  }
3952
4133
  return "unresolved";
3953
4134
  }
3954
- /** Clear both `unresolved`-bound trackers for a row (any non-`unresolved` outcome). */
4135
+ /** Clear all `unresolved`/failure-streak trackers for a row (any non-`unresolved` outcome). */
3955
4136
  clearRedriveUnresolved(messageId) {
3956
4137
  this.redriveUnresolvedSince.delete(messageId);
3957
4138
  this.redriveUnresolvedSignalled.delete(messageId);
4139
+ this.redrivePollFailures.delete(messageId);
4140
+ this.redriveOutcomeUnreportedSignalled.delete(messageId);
4141
+ this.redriveOutcomeFailingSince.delete(messageId);
4142
+ this.redriveOutcomeAbandonedSignalled.delete(messageId);
4143
+ }
4144
+ /**
4145
+ * #1340: the dispatch loop reached a message and did NOT start a turn. Fires at
4146
+ * most once per (message, branch) streak — a wedged row is re-tried every tick,
4147
+ * and the per-tick count is already carried by the co-occurring
4148
+ * `redrive_unresolved`/`redrive_redispatched` signals.
4149
+ */
4150
+ signalDispatchNotStarted(conv, message, branch) {
4151
+ if (this.dispatchNotStartedSignalled.get(message.id) === branch) return;
4152
+ this.dispatchNotStartedSignalled.set(message.id, branch);
4153
+ void this.postSignal(conv.id, message.id, "dispatch_not_started", { branch });
4154
+ }
4155
+ /**
4156
+ * Class B (#1340): the runner DECIDED an outcome (reattach/settle/fail_permanent)
4157
+ * but its own PATCH to record it failed. Fires at most once per (message,
4158
+ * outcome) streak, and only while `boundRedriveOutcome` has not yet tripped —
4159
+ * once it trips, `redrive_outcome_abandoned` takes over reporting for the row
4160
+ * (#1366).
4161
+ */
4162
+ signalRedriveOutcomeUnreported(conv, message, outcome) {
4163
+ if (this.redriveOutcomeUnreportedSignalled.get(message.id) === outcome) return;
4164
+ this.redriveOutcomeUnreportedSignalled.set(message.id, outcome);
4165
+ void this.postSignal(conv.id, message.id, "redrive_outcome_unreported", {
4166
+ attempted_outcome: outcome
4167
+ });
4168
+ }
4169
+ /**
4170
+ * The runner-authored, honest error text for the terminal fallback a tripped
4171
+ * `boundRedriveOutcome` sends. Distinguishable per outcome and truthful about
4172
+ * what actually happened — the `settle`/done case must say the turn finished
4173
+ * but its result could not be recorded, never that the runner stopped
4174
+ * responding (that would be a lie for this shape, see #1366's "why this ships").
4175
+ */
4176
+ static REDRIVE_ABANDON_ERROR = {
4177
+ reattach: "your runner could not record that this message had started, so it was given up on",
4178
+ settle: "your runner finished this message but could not record the result, so the reply could not be delivered",
4179
+ 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"
4180
+ };
4181
+ /**
4182
+ * Bound for Class B (#1340, #1366): the runner DECIDED an outcome but its own
4183
+ * PATCH to record it failed. Two independent trip arms (either sufficient):
4184
+ * (1) this failure streak has lasted `pausedMaxWaitMs` — DURATION, not a tick
4185
+ * count, reusing the knob `resolveRedriveUnresolved` already established; (2)
4186
+ * the turn's `processing_started_at` age has crossed
4187
+ * `ABSOLUTE_MAX_PROCESSING_MS` — durable and restart-surviving, since arm (1)'s
4188
+ * in-memory streak resets on a scale-to-zero restart.
4189
+ *
4190
+ * INVARIANT — a tripped bound never suppresses the original outcome attempt;
4191
+ * it only adds a fallback after that attempt has failed again. This is only
4192
+ * ever reached from inside the catch of the ORIGINAL outcome PATCH, which is
4193
+ * attempted first on every tick whether or not this bound tripped before —
4194
+ * there is no give-up latch that would short-circuit it. That is what lets a
4195
+ * route-level fault that heals later still deliver the turn's real
4196
+ * `done`/`failed` payload: once the original PATCH succeeds again, this
4197
+ * helper is never entered and the row settles with its real result.
4198
+ */
4199
+ async boundRedriveOutcome(conv, message, outcome) {
4200
+ const now = this.now();
4201
+ const since = this.redriveOutcomeFailingSince.get(message.id);
4202
+ if (since === void 0) this.redriveOutcomeFailingSince.set(message.id, now);
4203
+ const durationTripped = now - (since ?? now) >= this.pausedMaxWaitMs;
4204
+ const parsed = message.processing_started_at ? Date.parse(message.processing_started_at) : NaN;
4205
+ const absoluteAgeTripped = !Number.isNaN(parsed) && now - parsed >= ABSOLUTE_MAX_PROCESSING_MS;
4206
+ if (!durationTripped && !absoluteAgeTripped) {
4207
+ this.signalRedriveOutcomeUnreported(conv, message, outcome);
4208
+ return "retry";
4209
+ }
4210
+ const arm = durationTripped ? "failure_window" : "absolute_age";
4211
+ try {
4212
+ await this.markFailed(
4213
+ conv.id,
4214
+ message.id,
4215
+ void 0,
4216
+ _ChannelDriver.REDRIVE_ABANDON_ERROR[outcome]
4217
+ );
4218
+ } catch (err) {
4219
+ if (err instanceof ChannelAuthError) throw err;
4220
+ this.log({
4221
+ level: "warn",
4222
+ 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)}`,
4223
+ conversation_id: conv.id,
4224
+ message_id: message.id
4225
+ });
4226
+ if (!this.redriveOutcomeAbandonedSignalled.has(message.id)) {
4227
+ this.redriveOutcomeAbandonedSignalled.add(message.id);
4228
+ void this.postSignal(conv.id, message.id, "redrive_outcome_abandoned", {
4229
+ attempted_outcome: outcome,
4230
+ reported: false,
4231
+ arm
4232
+ });
4233
+ }
4234
+ return "retry";
4235
+ }
4236
+ this.clearRedriveUnresolved(message.id);
4237
+ void this.postSignal(conv.id, message.id, "redrive_outcome_abandoned", {
4238
+ attempted_outcome: outcome,
4239
+ reported: true,
4240
+ arm
4241
+ });
4242
+ return "abandoned";
4243
+ }
4244
+ /**
4245
+ * Record one poll outcome toward the re-drive fence's consecutive-identical-
4246
+ * failure streak (#1348) and return the resulting count. `signature === null`
4247
+ * (a thrown exception, H1) always clears the streak and returns `0` — it is
4248
+ * never countable. Otherwise the streak continues only when BOTH the session
4249
+ * and the signature match the previous failure; anything else (a different
4250
+ * session, or the same session failing a DIFFERENT way) starts a fresh streak
4251
+ * at `1`.
4252
+ */
4253
+ recordRedrivePollFailure(messageId, sessionId, signature) {
4254
+ if (signature === null) {
4255
+ this.redrivePollFailures.delete(messageId);
4256
+ return 0;
4257
+ }
4258
+ const existing = this.redrivePollFailures.get(messageId);
4259
+ if (existing && existing.sessionId === sessionId && existing.signature === signature) {
4260
+ existing.count += 1;
4261
+ return existing.count;
4262
+ }
4263
+ this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
4264
+ return 1;
4265
+ }
4266
+ /**
4267
+ * Record one UNCONFIRMED-dispatch outcome (a `pending` row with no stored
4268
+ * `opencode_message_id` whose `sendPromptAsync` returned `null`) toward the
4269
+ * bound in `processConversation`'s dispatch loop, and return the resulting
4270
+ * count. Mirrors `recordRedrivePollFailure`'s session-scoping: a session
4271
+ * change starts a fresh streak at `1` rather than inheriting the old one's
4272
+ * count, since a new session is a genuinely different attempt.
4273
+ */
4274
+ recordUnconfirmedDispatch(messageId, sessionId) {
4275
+ const existing = this.unconfirmedDispatchFailures.get(messageId);
4276
+ if (existing && existing.sessionId === sessionId) {
4277
+ existing.count += 1;
4278
+ return existing.count;
4279
+ }
4280
+ this.unconfirmedDispatchFailures.set(messageId, { sessionId, count: 1 });
4281
+ return 1;
3958
4282
  }
3959
4283
  /**
3960
4284
  * Record that `sessionId` is no longer a valid binding for `conversationId`
@@ -3980,6 +4304,13 @@ var ChannelDriver = class _ChannelDriver {
3980
4304
  * `refusedSessionId` is set when the #553 guard fired — i.e. the persisted
3981
4305
  * binding was an id this runner had abandoned, so a resurrection genuinely
3982
4306
  * happened and a fresh session was bound instead. The caller reports it.
4307
+ *
4308
+ * `created` says the returned session was made JUST NOW, so it provably holds
4309
+ * no prior turn. The re-drive fence needs that as CONTRARY evidence ("nothing
4310
+ * to reconcile against") — distinct from the ambiguous "I polled and saw an
4311
+ * empty transcript", which stays a deferral. Keep it separate from
4312
+ * `refusedSessionId`: only the latter means a #553 resurrection happened, and
4313
+ * only it may drive the `session_superseded` signal.
3983
4314
  */
3984
4315
  async ensureSession(conv) {
3985
4316
  const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
@@ -3990,7 +4321,11 @@ var ChannelDriver = class _ChannelDriver {
3990
4321
  conversation_id: conv.id
3991
4322
  });
3992
4323
  this.sessions.delete(conv.id);
3993
- return { sessionId: await this.createAndBindSession(conv.id), refusedSessionId: bound };
4324
+ return {
4325
+ sessionId: await this.createAndBindSession(conv.id),
4326
+ refusedSessionId: bound,
4327
+ created: true
4328
+ };
3994
4329
  }
3995
4330
  if (bound) {
3996
4331
  const exists = await sessionExists(this.port, bound);
@@ -4001,12 +4336,12 @@ var ChannelDriver = class _ChannelDriver {
4001
4336
  conversation_id: conv.id
4002
4337
  });
4003
4338
  this.sessions.delete(conv.id);
4004
- return { sessionId: await this.createAndBindSession(conv.id) };
4339
+ return { sessionId: await this.createAndBindSession(conv.id), created: true };
4005
4340
  }
4006
4341
  this.sessions.set(conv.id, bound);
4007
- return { sessionId: bound };
4342
+ return { sessionId: bound, created: false };
4008
4343
  }
4009
- return { sessionId: await this.createAndBindSession(conv.id) };
4344
+ return { sessionId: await this.createAndBindSession(conv.id), created: true };
4010
4345
  }
4011
4346
  /**
4012
4347
  * Create a fresh OpenCode session for a conversation, cache the binding, and
@@ -4435,9 +4770,8 @@ var ChannelDriver = class _ChannelDriver {
4435
4770
  const awaitingHuman = observedOpen || latchedPaused;
4436
4771
  if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
4437
4772
  const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
4438
- let claimed;
4439
4773
  try {
4440
- claimed = await this.markProcessing(
4774
+ await this.markProcessing(
4441
4775
  conv.id,
4442
4776
  inFlight.evidentMessageId,
4443
4777
  sessionId,
@@ -4446,23 +4780,24 @@ var ChannelDriver = class _ChannelDriver {
4446
4780
  );
4447
4781
  } catch (err) {
4448
4782
  if (err instanceof ChannelAuthError) throw err;
4449
- this.log({
4450
- level: "warn",
4451
- message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
4452
- conversation_id: conv.id,
4453
- message_id: inFlight.evidentMessageId
4454
- });
4455
- return;
4783
+ if (err instanceof ChannelTerminalError) {
4784
+ this.log({
4785
+ level: "error",
4786
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (terminal HTTP ${err.status}) \u2014 the server definitively refused the swap`,
4787
+ conversation_id: conv.id,
4788
+ message_id: inFlight.evidentMessageId
4789
+ });
4790
+ } else {
4791
+ this.log({
4792
+ level: "warn",
4793
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
4794
+ conversation_id: conv.id,
4795
+ message_id: inFlight.evidentMessageId
4796
+ });
4797
+ return;
4798
+ }
4456
4799
  }
4457
4800
  inFlight.started = true;
4458
- if (!claimed) {
4459
- this.log({
4460
- level: "debug",
4461
- message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
4462
- conversation_id: conv.id,
4463
- message_id: inFlight.evidentMessageId
4464
- });
4465
- }
4466
4801
  }
4467
4802
  if (state === "done") {
4468
4803
  await this.settleMessageDone(sessionId, watcher, inFlight, messages);
@@ -5092,15 +5427,39 @@ var ChannelDriver = class _ChannelDriver {
5092
5427
  }
5093
5428
  if (ocId === null) {
5094
5429
  this.awaitingReadopt.delete(row.id);
5430
+ const streak = this.recordUnconfirmedDispatch(row.id, sessionId);
5431
+ if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
5432
+ this.unconfirmedDispatchFailures.delete(row.id);
5433
+ this.sessions.delete(readoptConv.id);
5434
+ this.supersede(readoptConv.id, sessionId);
5435
+ 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.`;
5436
+ this.log({
5437
+ level: "error",
5438
+ message: errorMessage,
5439
+ conversation_id: row.conversation_id,
5440
+ message_id: row.id
5441
+ });
5442
+ await this.markFailed(row.conversation_id, row.id, null, errorMessage).catch((markErr) => {
5443
+ this.log({
5444
+ level: "warn",
5445
+ 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)}`,
5446
+ conversation_id: row.conversation_id,
5447
+ message_id: row.id
5448
+ });
5449
+ });
5450
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
5451
+ return;
5452
+ }
5095
5453
  this.log({
5096
5454
  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`,
5455
+ 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
5456
  conversation_id: row.conversation_id,
5099
5457
  message_id: row.id
5100
5458
  });
5101
5459
  void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
5102
5460
  return;
5103
5461
  }
5462
+ this.unconfirmedDispatchFailures.delete(row.id);
5104
5463
  this.registerReadopted(readoptConv, sessionId, readoptMessage, ocId, this.processedAtMs(row));
5105
5464
  this.dispatched.add(row.id);
5106
5465
  this.readopted.add(row.id);
@@ -5765,18 +6124,22 @@ var ChannelDriver = class _ChannelDriver {
5765
6124
  * opencode_session_id}` → `notifyMessageStarted` (hourglass→runner swap +
5766
6125
  * deep-linked "View in Evident" notice).
5767
6126
  *
5768
- * Return/throw contract (consumed by the watcher's swap-to-running guard):
5769
- * - returns `true` → the server transitioned the row to processing;
5770
- * - returns `false` → the server gave a DEFINITIVE "already-processing"
5771
- * answer (a non-retryable, non-auth status e.g. a
5772
- * conflict because a duplicate already transitioned it),
5773
- * so the caller treats it as already-started and does NOT
5774
- * retry;
5775
- * - throws `ChannelAuthError` on 401/403 (terminal auth failure);
5776
- * - throws on a TRANSIENT failure (retryable 5xx/429 status, or a
5777
- * network-level error from `fetch`) — i.e. NO definitive server response —
5778
- * so the caller leaves the message un-started and retries the swap on the
5779
- * next tick.
6127
+ * Outcome contract (consumed by the watcher's swap-to-running guard):
6128
+ * - resolves (`void`) → the server transitioned the row to
6129
+ * processing (or idempotently confirmed
6130
+ * already-processing that answer is
6131
+ * still a 200, never a refusal);
6132
+ * - throws `ChannelAuthError` → 401/403 (terminal auth failure);
6133
+ * - throws `ChannelTerminalError` → a definitive non-retryable, non-auth 4xx
6134
+ * (404 the row or its conversation is
6135
+ * gone, 400 the update was rejected).
6136
+ * Retrying cannot help;
6137
+ * - throws a plain `Error` → a TRANSIENT failure (retryable 5xx/429
6138
+ * status, or a network-level error from
6139
+ * `fetch`) — i.e. NO definitive server
6140
+ * response — so the caller leaves the
6141
+ * message un-started and retries the swap
6142
+ * on the next tick.
5780
6143
  * A single attempt (no internal retry): the watcher's per-tick loop is the
5781
6144
  * retry vehicle for the swap-to-running.
5782
6145
  */
@@ -5795,11 +6158,11 @@ var ChannelDriver = class _ChannelDriver {
5795
6158
  }
5796
6159
  );
5797
6160
  this.assertAuth(res, "marking message as processing");
5798
- if (res.ok) return true;
6161
+ if (res.ok) return;
5799
6162
  if (isRetryableStatus(res.status)) {
5800
6163
  throw new Error(`marking message as processing: HTTP ${res.status}`);
5801
6164
  }
5802
- return false;
6165
+ throw new ChannelTerminalError(`marking message as processing: HTTP ${res.status}`, res.status);
5803
6166
  }
5804
6167
  /**
5805
6168
  * EXISTING combinedAuth completion route — idempotent (WI-CHAN-2). `PATCH