@evident-ai/cli 3.1.1-dev.3d68bee → 3.1.1-dev.4c17a8b

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
@@ -2062,6 +2062,7 @@ var DEFAULT_STUCK_QUEUED_MS = 6e4;
2062
2062
  var HEARTBEAT_MS = 6e4;
2063
2063
  var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
2064
2064
  var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
2065
+ var MAX_SUPERSEDED_CONVERSATIONS = 256;
2065
2066
  var ChannelAuthError = class extends Error {
2066
2067
  constructor(message) {
2067
2068
  super(message);
@@ -2100,6 +2101,34 @@ var ChannelDriver = class _ChannelDriver {
2100
2101
  now;
2101
2102
  /** Cache of conversationId → opencode sessionId. */
2102
2103
  sessions = /* @__PURE__ */ new Map();
2104
+ /**
2105
+ * conversationId → the opencode session this runner has ABANDONED as that
2106
+ * conversation's binding (#553), after a genuine (`sessionExists === true`)
2107
+ * dispatch failure: the session still exists but is wedged, so #485's self-heal
2108
+ * must bind a fresh one.
2109
+ *
2110
+ * Dropping the local binding + clearing the server row is not enough on its own:
2111
+ * a SIBLING message dispatched earlier in the same drain is still in-flight under
2112
+ * the same session, and its watcher's routine status writes carry
2113
+ * `opencode_session_id`, RESURRECTING the wedged id server-side after the clear —
2114
+ * and `ensureSession`'s persisted-id fallback then reuses it, defeating the
2115
+ * self-heal. This map makes the runner authoritative instead of racing those
2116
+ * writes: *`ensureSession` never reuses an abandoned id for that conversation,
2117
+ * whatever the server row says* — which holds even when the resurrecting write
2118
+ * is one we deliberately keep (see `markDone`).
2119
+ *
2120
+ * Bounded by construction, on both axes: keyed by CONVERSATION, so N failures on
2121
+ * one conversation hold ONE entry (the newest abandonment replaces the older), and
2122
+ * hard-capped at `MAX_SUPERSEDED_CONVERSATIONS` with FIFO eviction. Only the
2123
+ * NEWEST abandoned id per conversation is guarded: after a second abandonment a
2124
+ * late sibling of the FIRST session can write that id back and `ensureSession`
2125
+ * will reuse it — costing ONE repeat failure, which re-supersedes it. Deliberately
2126
+ * NOT dropped when the session's watcher tears down: `markDone` still writes the
2127
+ * abandoned id back (it must, or the reply is lost), so the guard has to outlive
2128
+ * the turn that resurrects it. In-memory only — a restart forgets it, at the same
2129
+ * bounded cost.
2130
+ */
2131
+ supersededSessions = /* @__PURE__ */ new Map();
2103
2132
  /**
2104
2133
  * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
2105
2134
  * longer idempotent (no caller-supplied `messageID`), and its read-back picks
@@ -2408,10 +2437,15 @@ var ChannelDriver = class _ChannelDriver {
2408
2437
  * @returns the count of messages NEWLY dispatched (not already in-flight).
2409
2438
  */
2410
2439
  async processConversation(conv) {
2411
- const sessionId = await this.ensureSession(conv);
2440
+ const { sessionId, refusedSessionId } = await this.ensureSession(conv);
2412
2441
  const messages = await this.getPendingMessages(conv.id);
2413
2442
  let dispatched = 0;
2414
2443
  let skippedAlreadyDispatched = 0;
2444
+ if (refusedSessionId && messages.length > 0) {
2445
+ void this.postSignal(conv.id, messages[0].id, "session_superseded", {
2446
+ superseded_session_id: refusedSessionId
2447
+ });
2448
+ }
2415
2449
  for (const message of messages) {
2416
2450
  if (this.stopped) break;
2417
2451
  if (this.dispatched.has(message.id)) {
@@ -2460,6 +2494,13 @@ var ChannelDriver = class _ChannelDriver {
2460
2494
  }
2461
2495
  const errorMessage = err instanceof Error ? err.message : String(err);
2462
2496
  this.sessions.delete(conv.id);
2497
+ this.supersede(conv.id, sessionId);
2498
+ this.log({
2499
+ level: "warn",
2500
+ message: `Abandoning OpenCode session ${sessionId.slice(0, 8)} as the binding for conversation ${conv.id.slice(0, 8)} (it exists but failed to run a turn) \u2014 a fresh session is created on the next tick, whatever the persisted binding says by then.`,
2501
+ conversation_id: conv.id,
2502
+ message_id: message.id
2503
+ });
2463
2504
  await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
2464
2505
  this.log({
2465
2506
  level: "warn",
@@ -2500,8 +2541,42 @@ var ChannelDriver = class _ChannelDriver {
2500
2541
  this.ensureWatcherRunning(sessionId);
2501
2542
  return dispatched;
2502
2543
  }
2544
+ /**
2545
+ * Record that `sessionId` is no longer a valid binding for `conversationId`
2546
+ * (#553). Keyed by conversation and hard-capped, so it cannot grow with the
2547
+ * number of failures — see the `supersededSessions` field doc.
2548
+ */
2549
+ supersede(conversationId, sessionId) {
2550
+ this.supersededSessions.delete(conversationId);
2551
+ this.supersededSessions.set(conversationId, sessionId);
2552
+ while (this.supersededSessions.size > MAX_SUPERSEDED_CONVERSATIONS) {
2553
+ const oldest = this.supersededSessions.keys().next().value;
2554
+ if (oldest === void 0) return;
2555
+ this.supersededSessions.delete(oldest);
2556
+ }
2557
+ }
2558
+ /** Whether `sessionId` is the session this conversation has abandoned (#553). */
2559
+ isSuperseded(conversationId, sessionId) {
2560
+ return this.supersededSessions.get(conversationId) === sessionId;
2561
+ }
2562
+ /**
2563
+ * Resolve the opencode session to run this conversation's turns in.
2564
+ *
2565
+ * `refusedSessionId` is set when the #553 guard fired — i.e. the persisted
2566
+ * binding was an id this runner had abandoned, so a resurrection genuinely
2567
+ * happened and a fresh session was bound instead. The caller reports it.
2568
+ */
2503
2569
  async ensureSession(conv) {
2504
2570
  const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
2571
+ if (bound && this.isSuperseded(conv.id, bound)) {
2572
+ this.log({
2573
+ level: "warn",
2574
+ message: `OpenCode session ${bound.slice(0, 8)} was abandoned for conversation ${conv.id.slice(0, 8)} after a failed dispatch but is still bound to it (the persisted id was written back by a turn already in flight) \u2014 ignoring it and binding a fresh session.`,
2575
+ conversation_id: conv.id
2576
+ });
2577
+ this.sessions.delete(conv.id);
2578
+ return { sessionId: await this.createAndBindSession(conv.id), refusedSessionId: bound };
2579
+ }
2505
2580
  if (bound) {
2506
2581
  const exists = await sessionExists(this.port, bound);
2507
2582
  if (exists === false) {
@@ -2511,12 +2586,12 @@ var ChannelDriver = class _ChannelDriver {
2511
2586
  conversation_id: conv.id
2512
2587
  });
2513
2588
  this.sessions.delete(conv.id);
2514
- return this.createAndBindSession(conv.id);
2589
+ return { sessionId: await this.createAndBindSession(conv.id) };
2515
2590
  }
2516
2591
  this.sessions.set(conv.id, bound);
2517
- return bound;
2592
+ return { sessionId: bound };
2518
2593
  }
2519
- return this.createAndBindSession(conv.id);
2594
+ return { sessionId: await this.createAndBindSession(conv.id) };
2520
2595
  }
2521
2596
  /**
2522
2597
  * Create a fresh OpenCode session for a conversation, cache the binding, and
@@ -3975,6 +4050,32 @@ var ChannelDriver = class _ChannelDriver {
3975
4050
  }
3976
4051
  return messages;
3977
4052
  }
4053
+ /**
4054
+ * The `opencode_session_id` fragment of a status PATCH body — `{}` when this
4055
+ * conversation has ABANDONED that session (#553). The field is optional
4056
+ * server-side and an absent one leaves the persisted binding untouched, so
4057
+ * omitting it is how a routine status write stops resurrecting it.
4058
+ *
4059
+ * ONLY for writes whose sole cost is a lost deep link. The `processing` notice
4060
+ * degrades to no "View in Evident" link (the reaction swap still fires) and the
4061
+ * turn-failure notice is built from the PATCH's own `error` text with a link off
4062
+ * the persisted row — neither loses content the user came for. `markDone`
4063
+ * deliberately does NOT use this helper: the server fetches the reply text
4064
+ * THROUGH the session id it is given, so suppressing there would replace the
4065
+ * agent's answer with a bare "✅ Done!" (the #183/#187 failure). The
4066
+ * `ensureSession` guard, not this suppression, is what makes the self-heal
4067
+ * stick.
4068
+ */
4069
+ sessionIdBody(sessionId, conversationId, messageId, status) {
4070
+ if (!this.isSuperseded(conversationId, sessionId)) return { opencode_session_id: sessionId };
4071
+ this.log({
4072
+ level: "debug",
4073
+ message: `Omitting the abandoned OpenCode session ${sessionId.slice(0, 8)} from the '${status}' update for message ${messageId.slice(0, 8)} so it is not re-bound to conversation ${conversationId.slice(0, 8)}`,
4074
+ conversation_id: conversationId,
4075
+ message_id: messageId
4076
+ });
4077
+ return {};
4078
+ }
3978
4079
  /**
3979
4080
  * EXISTING combinedAuth route — now fired by the watcher on queued→running
3980
4081
  * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
@@ -4004,7 +4105,7 @@ var ChannelDriver = class _ChannelDriver {
4004
4105
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
4005
4106
  body: JSON.stringify({
4006
4107
  status: "processing",
4007
- opencode_session_id: sessionId,
4108
+ ...this.sessionIdBody(sessionId, conversationId, messageId, "processing"),
4008
4109
  ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
4009
4110
  ...title ? { title } : {}
4010
4111
  })
@@ -4053,6 +4154,11 @@ var ChannelDriver = class _ChannelDriver {
4053
4154
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
4054
4155
  body: JSON.stringify({
4055
4156
  status: "done",
4157
+ // ALWAYS sent, even for a session this conversation has abandoned
4158
+ // (#553): the server reads the reply text back out of THIS session id
4159
+ // to deliver it. Omitting it would leave the user with "✅ Done!"
4160
+ // instead of the answer — a worse regression than the resurrection it
4161
+ // would prevent, which `ensureSession`'s guard handles anyway.
4056
4162
  opencode_session_id: sessionId,
4057
4163
  ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
4058
4164
  ...title ? { title } : {},
@@ -4081,7 +4187,11 @@ var ChannelDriver = class _ChannelDriver {
4081
4187
  */
4082
4188
  async markFailed(conversationId, messageId, sessionId, error2, usage) {
4083
4189
  const body = { status: "failed" };
4084
- if (sessionId !== void 0) body.opencode_session_id = sessionId;
4190
+ if (sessionId === null) {
4191
+ body.opencode_session_id = null;
4192
+ } else if (sessionId !== void 0) {
4193
+ Object.assign(body, this.sessionIdBody(sessionId, conversationId, messageId, "failed"));
4194
+ }
4085
4195
  if (error2 !== void 0) body.error = error2;
4086
4196
  if (usage) Object.assign(body, usage);
4087
4197
  await this.callWithRetry(