@evident-ai/cli 3.0.1-dev.116734c → 3.0.1-dev.1d33bd1

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
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/index.ts
4
+ import { createRequire } from "module";
4
5
  import { Command } from "commander";
5
6
 
6
7
  // src/commands/login.ts
@@ -484,8 +485,34 @@ var TelemetryEventTypes = {
484
485
  var MAX_FRAME_BYTES = 256 * 1024;
485
486
  var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
486
487
 
488
+ // ../../packages/types/src/logging/index.ts
489
+ var CORRELATION_ID_HEADER = "x-evident-correlation-id";
490
+ function log(level, event, fields) {
491
+ const method = level === "debug" ? "log" : level;
492
+ try {
493
+ console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
494
+ } catch (err) {
495
+ console.error(
496
+ "[evident] log_serialize_failed",
497
+ event,
498
+ err instanceof Error ? err.message : String(err)
499
+ );
500
+ }
501
+ }
502
+ function stripQuery(url) {
503
+ try {
504
+ return new URL(url).pathname;
505
+ } catch {
506
+ const q = url.indexOf("?");
507
+ return q === -1 ? url : url.slice(0, q);
508
+ }
509
+ }
510
+
487
511
  // src/lib/telemetry.ts
488
- var CLI_VERSION = process.env.npm_package_version || "unknown";
512
+ var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
513
+ function getCliVersion() {
514
+ return CLI_VERSION;
515
+ }
489
516
  var eventBuffer = [];
490
517
  var flushTimeout = null;
491
518
  var isShuttingDown = false;
@@ -684,6 +711,19 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
684
711
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
685
712
  }
686
713
 
714
+ // src/lib/opencode/opencode-version-gate.ts
715
+ var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
716
+ function isQueueValidatedVersion(version2) {
717
+ if (!version2) return false;
718
+ return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
719
+ }
720
+ function buildOpenCodeVersionWarning(version2) {
721
+ if (isQueueValidatedVersion(version2)) return null;
722
+ const detected = version2 ? `v${version2}` : "unknown";
723
+ const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
724
+ return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack/WhatsApp) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
725
+ }
726
+
687
727
  // src/lib/opencode/process.ts
688
728
  import { execSync, spawn } from "child_process";
689
729
  var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
@@ -996,7 +1036,37 @@ function roleOf(m) {
996
1036
  }
997
1037
  function completedOf(m) {
998
1038
  if (!m || typeof m !== "object") return void 0;
999
- return m.info?.time?.completed;
1039
+ return m.info?.time?.completed ?? m.time?.completed;
1040
+ }
1041
+ function createdOf(m) {
1042
+ if (!m || typeof m !== "object") return void 0;
1043
+ return m.info?.time?.created ?? m.time?.created;
1044
+ }
1045
+ function idOf(m) {
1046
+ if (!m || typeof m !== "object") return void 0;
1047
+ if (typeof m.id === "string") return m.id;
1048
+ const infoId = m.info?.id;
1049
+ return typeof infoId === "string" ? infoId : void 0;
1050
+ }
1051
+ function parentIdOf(m) {
1052
+ if (!m || typeof m !== "object") return void 0;
1053
+ if (typeof m.parentID === "string") return m.parentID;
1054
+ const infoParent = m.info?.parentID;
1055
+ return typeof infoParent === "string" ? infoParent : void 0;
1056
+ }
1057
+ function finishOf(m) {
1058
+ if (!m || typeof m !== "object") return void 0;
1059
+ if (typeof m.finish === "string") return m.finish;
1060
+ const infoFinish = m.info?.finish;
1061
+ return typeof infoFinish === "string" ? infoFinish : void 0;
1062
+ }
1063
+ function errorOf(m) {
1064
+ if (!m || typeof m !== "object") return void 0;
1065
+ return m.info?.error ?? m.error;
1066
+ }
1067
+ function isAssistantInFlight(m) {
1068
+ if (completedOf(m) == null) return true;
1069
+ return finishOf(m) === "tool-calls";
1000
1070
  }
1001
1071
  async function getSessionMessages(port, sessionId) {
1002
1072
  try {
@@ -1008,12 +1078,6 @@ async function getSessionMessages(port, sessionId) {
1008
1078
  return null;
1009
1079
  }
1010
1080
  }
1011
- function isTurnComplete(messages) {
1012
- if (!messages || messages.length === 0) return false;
1013
- const last = messages[messages.length - 1];
1014
- if (roleOf(last) !== "assistant") return false;
1015
- return completedOf(last) != null;
1016
- }
1017
1081
  async function createOpenCodeSession(port, directory) {
1018
1082
  const url = new URL(`${opencodeBase(port)}/session`);
1019
1083
  if (directory && directory.trim()) {
@@ -1031,7 +1095,15 @@ async function createOpenCodeSession(port, directory) {
1031
1095
  const data = await response.json();
1032
1096
  return data.id;
1033
1097
  }
1034
- async function sendMessageToOpenCode(port, sessionId, content, options, hooks, maxWaitMs = 10 * 60 * 1e3) {
1098
+ function messageText(m) {
1099
+ if (!m || !Array.isArray(m.parts)) return "";
1100
+ return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
1101
+ }
1102
+ async function sendPromptAsync(port, sessionId, content, options) {
1103
+ const before = await getSessionMessages(port, sessionId);
1104
+ const knownUserIds = new Set(
1105
+ (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
1106
+ );
1035
1107
  const body = {
1036
1108
  parts: [{ type: "text", text: content }]
1037
1109
  };
@@ -1047,79 +1119,109 @@ async function sendMessageToOpenCode(port, sessionId, content, options, hooks, m
1047
1119
  };
1048
1120
  }
1049
1121
  }
1050
- let pollDone = false;
1051
- const reportedQuestions = /* @__PURE__ */ new Set();
1052
- const reportedPermissions = /* @__PURE__ */ new Set();
1053
- const pollInteractive = async () => {
1054
- while (!pollDone) {
1055
- await new Promise((resolve) => setTimeout(resolve, 1e3));
1056
- if (pollDone) break;
1057
- if (hooks?.onQuestion) {
1058
- try {
1059
- const res = await fetch(`${opencodeBase(port)}/question`);
1060
- if (res.ok) {
1061
- const questions = await res.json();
1062
- for (const q of questions) {
1063
- if (q.sessionID === sessionId && !reportedQuestions.has(q.id)) {
1064
- reportedQuestions.add(q.id);
1065
- await hooks.onQuestion(q);
1066
- }
1067
- }
1068
- }
1069
- } catch {
1070
- }
1071
- }
1072
- if (hooks?.onPermission) {
1073
- try {
1074
- const res = await fetch(`${opencodeBase(port)}/permission`);
1075
- if (res.ok) {
1076
- const permissions = await res.json();
1077
- for (const p of permissions) {
1078
- if (p.sessionID === sessionId && !reportedPermissions.has(p.id)) {
1079
- reportedPermissions.add(p.id);
1080
- await hooks.onPermission(p);
1081
- }
1082
- }
1083
- }
1084
- } catch {
1122
+ const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
1123
+ method: "POST",
1124
+ headers: { "Content-Type": "application/json" },
1125
+ body: JSON.stringify(body)
1126
+ });
1127
+ if (res.status < 200 || res.status >= 300) {
1128
+ const text = await res.text().catch(() => "");
1129
+ throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1130
+ }
1131
+ const READ_BACK_ATTEMPTS = 5;
1132
+ const READ_BACK_DELAY_MS = 150;
1133
+ for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
1134
+ const after = await getSessionMessages(port, sessionId);
1135
+ if (after) {
1136
+ let best = null;
1137
+ for (const m of after) {
1138
+ if (roleOf(m) !== "user") continue;
1139
+ const id = idOf(m);
1140
+ if (typeof id !== "string" || knownUserIds.has(id)) continue;
1141
+ if (messageText(m) !== content) continue;
1142
+ const created = createdOf(m) ?? 0;
1143
+ if (best === null || created > best.created) {
1144
+ best = { id, created };
1085
1145
  }
1086
1146
  }
1147
+ if (best) return best.id;
1087
1148
  }
1088
- };
1089
- const sendMessage = async () => {
1090
- const controller = new AbortController();
1091
- const timer = setTimeout(() => controller.abort(), maxWaitMs);
1092
- try {
1093
- const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/message`, {
1094
- method: "POST",
1095
- headers: { "Content-Type": "application/json" },
1096
- body: JSON.stringify(body),
1097
- signal: controller.signal
1098
- });
1099
- if (!res.ok) {
1100
- const text = await res.text().catch(() => "");
1101
- throw new Error(`OpenCode message failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1102
- }
1103
- const sessionRes = await fetch(`${opencodeBase(port)}/session/${sessionId}`).catch(
1104
- () => null
1105
- );
1106
- const session = sessionRes?.ok ? await sessionRes.json() : null;
1107
- const reportedInteraction = reportedQuestions.size > 0 || reportedPermissions.size > 0;
1108
- const turnComplete = isTurnComplete(await getSessionMessages(port, sessionId));
1109
- const awaitingInteraction = reportedInteraction && !turnComplete;
1110
- return { title: session?.title, awaitingInteraction };
1111
- } catch (err) {
1112
- if (err instanceof Error && err.name === "AbortError") {
1113
- throw new Error("Message processing timed out");
1114
- }
1115
- throw err;
1116
- } finally {
1117
- clearTimeout(timer);
1118
- pollDone = true;
1149
+ if (attempt < READ_BACK_ATTEMPTS - 1) {
1150
+ await new Promise((resolve) => setTimeout(resolve, READ_BACK_DELAY_MS));
1119
1151
  }
1120
- };
1121
- const [result] = await Promise.all([sendMessage(), pollInteractive()]);
1122
- return result;
1152
+ }
1153
+ return null;
1154
+ }
1155
+ function findAssistantReplyAfter(messages, userMessageId) {
1156
+ if (!messages || messages.length === 0) return null;
1157
+ const byParent = messages.find(
1158
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
1159
+ );
1160
+ if (byParent) return byParent;
1161
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1162
+ if (userIndex === -1) return null;
1163
+ for (let i = userIndex + 1; i < messages.length; i++) {
1164
+ if (roleOf(messages[i]) === "assistant") return messages[i];
1165
+ }
1166
+ return null;
1167
+ }
1168
+ function findLastAssistantReplyFor(messages, userMessageId) {
1169
+ if (!messages || messages.length === 0) return null;
1170
+ let lastCorrelated = null;
1171
+ let lastNonErrored = null;
1172
+ for (let i = messages.length - 1; i >= 0; i--) {
1173
+ const m = messages[i];
1174
+ if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
1175
+ if (lastCorrelated === null) lastCorrelated = m;
1176
+ if (errorOf(m) == null) {
1177
+ lastNonErrored = m;
1178
+ break;
1179
+ }
1180
+ }
1181
+ if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
1182
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1183
+ if (userIndex === -1) return null;
1184
+ let last = null;
1185
+ let lastOk = null;
1186
+ for (let i = userIndex + 1; i < messages.length; i++) {
1187
+ const role = roleOf(messages[i]);
1188
+ if (role === "user") break;
1189
+ if (role === "assistant") {
1190
+ last = messages[i];
1191
+ if (errorOf(messages[i]) == null) lastOk = messages[i];
1192
+ }
1193
+ }
1194
+ return lastOk ?? last;
1195
+ }
1196
+ function messageRunState(messages, userMessageId) {
1197
+ if (!messages || messages.length === 0) return "unknown";
1198
+ const hasUser = messages.some((m) => idOf(m) === userMessageId);
1199
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1200
+ if (!hasUser) {
1201
+ if (!reply) return "unknown";
1202
+ }
1203
+ if (!reply) return "queued";
1204
+ if (isAssistantInFlight(reply)) return "running";
1205
+ return errorOf(reply) != null ? "failed" : "done";
1206
+ }
1207
+ function messageError(messages, userMessageId) {
1208
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1209
+ const error2 = errorOf(reply);
1210
+ if (error2 == null) return null;
1211
+ if (typeof error2 === "string") return error2;
1212
+ if (typeof error2 === "object") {
1213
+ const e = error2;
1214
+ const dataMessage = e.data?.message;
1215
+ if (typeof dataMessage === "string") return dataMessage;
1216
+ if (typeof e.message === "string") return e.message;
1217
+ }
1218
+ return "The agent run failed.";
1219
+ }
1220
+ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
1221
+ if (!messages || messages.length === 0) return false;
1222
+ return messages.some(
1223
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
1224
+ );
1123
1225
  }
1124
1226
 
1125
1227
  // src/lib/tunnel/connection.ts
@@ -1190,12 +1292,22 @@ var StreamForwarder = class {
1190
1292
  }
1191
1293
  async handleOpen(frame) {
1192
1294
  const { sid, method, path, headers, has_body } = frame;
1295
+ const correlationId = headers?.[CORRELATION_ID_HEADER];
1296
+ const startedAt = Date.now();
1193
1297
  if (path === TUNNEL_DRAIN_PING_PATH) {
1194
1298
  this.callbacks.onDrainPing?.();
1195
1299
  this.send({ type: "head", sid, status: 204, headers: {} });
1196
1300
  this.send({ type: "res_end", sid });
1197
1301
  return;
1198
1302
  }
1303
+ if (process.env.DEBUG) {
1304
+ log("debug", "agent_request", {
1305
+ correlation_id: correlationId,
1306
+ sid,
1307
+ method,
1308
+ path: stripQuery(path)
1309
+ });
1310
+ }
1199
1311
  const ac = new AbortController();
1200
1312
  let bodyPromise;
1201
1313
  let pushBody;
@@ -1242,6 +1354,14 @@ var StreamForwarder = class {
1242
1354
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1243
1355
  });
1244
1356
  this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
1357
+ if (process.env.DEBUG) {
1358
+ log("debug", "agent_response", {
1359
+ correlation_id: correlationId,
1360
+ sid,
1361
+ status: upstream.status,
1362
+ duration_ms: Date.now() - startedAt
1363
+ });
1364
+ }
1245
1365
  this.callbacks.onHead?.(sid, upstream.status);
1246
1366
  try {
1247
1367
  if (upstream.body) {
@@ -1498,6 +1618,12 @@ var RunnerConnection = class {
1498
1618
  };
1499
1619
 
1500
1620
  // src/lib/channels/driver.ts
1621
+ function messageIdOf(m) {
1622
+ if (!m || typeof m !== "object") return void 0;
1623
+ if (typeof m.id === "string") return m.id;
1624
+ const infoId = m.info?.id;
1625
+ return typeof infoId === "string" ? infoId : void 0;
1626
+ }
1501
1627
  var DEFAULT_RETRY_POLICY = {
1502
1628
  maxAttempts: 6,
1503
1629
  baseDelayMs: 500,
@@ -1505,12 +1631,21 @@ var DEFAULT_RETRY_POLICY = {
1505
1631
  };
1506
1632
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1507
1633
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1634
+ var DEFAULT_STUCK_QUEUED_MS = 6e4;
1508
1635
  var ChannelAuthError = class extends Error {
1509
1636
  constructor(message) {
1510
1637
  super(message);
1511
1638
  this.name = "ChannelAuthError";
1512
1639
  }
1513
1640
  };
1641
+ var ChannelTerminalError = class extends Error {
1642
+ status;
1643
+ constructor(message, status) {
1644
+ super(message);
1645
+ this.name = "ChannelTerminalError";
1646
+ this.status = status;
1647
+ }
1648
+ };
1514
1649
  function backoffDelay(attempt, policy) {
1515
1650
  const exp = policy.baseDelayMs * Math.pow(2, attempt);
1516
1651
  const capped = Math.min(policy.maxDelayMs, exp);
@@ -1531,15 +1666,82 @@ var ChannelDriver = class {
1531
1666
  sleep;
1532
1667
  pausedPollIntervalMs;
1533
1668
  pausedMaxWaitMs;
1669
+ stuckQueuedMs;
1670
+ now;
1534
1671
  /** Cache of conversationId → opencode sessionId. */
1535
1672
  sessions = /* @__PURE__ */ new Map();
1536
1673
  /**
1537
- * Outstanding paused-session watchers, keyed by `message.id` (WI-2-CLI).
1538
- * Single-flight per message: while a watcher is live for a message we never
1539
- * start a second one. The message stays `processing` for the watcher's lifetime
1540
- * so the `?status=pending` drain cannot double-claim it.
1674
+ * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
1675
+ * longer idempotent (no caller-supplied `messageID`), and its read-back picks
1676
+ * "the one new user row" which is only unambiguous if no OTHER dispatch into
1677
+ * the SAME session interleaves its snapshot→POST→read-back. This map chains each
1678
+ * session's dispatches so they run serially; distinct sessions stay concurrent.
1679
+ */
1680
+ sessionDispatchLocks = /* @__PURE__ */ new Map();
1681
+ /**
1682
+ * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1683
+ * session: one polling loop services all of that session's in-flight messages.
1684
+ * A session entry exists while it has any in-flight (dispatched-but-not-done)
1685
+ * message; it is removed once its in-flight set empties.
1541
1686
  */
1542
1687
  watchers = /* @__PURE__ */ new Map();
1688
+ /**
1689
+ * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
1690
+ * dispatched and are still in-flight. A message in this set is never
1691
+ * re-`prompt_async`-ed by a subsequent poll tick while it is queued/running.
1692
+ * Backed by a stable minted opencode `messageID` whose duplicate re-enqueue is
1693
+ * idempotent on opencode (PoC fact 9) — so even if this set is lost on restart,
1694
+ * a steady-state-poll re-dispatch will not double-run the message.
1695
+ */
1696
+ dispatched = /* @__PURE__ */ new Set();
1697
+ /**
1698
+ * Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
1699
+ * Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
1700
+ * so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
1701
+ * it is re-adopted and removed when its watcher settles or it is observed off
1702
+ * the processing list.
1703
+ */
1704
+ readopted = /* @__PURE__ */ new Set();
1705
+ /**
1706
+ * "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
1707
+ * re-adopted running/orphan row's watcher hit its `processed_at`-anchored
1708
+ * deadline (or an orphan whose window already elapsed): the still-`processing`
1709
+ * server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
1710
+ * drain until the 15-min cron resets it — spamming new turns.
1711
+ *
1712
+ * CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
1713
+ * does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
1714
+ * in opencode must still be delivered via `markDone` on the next drain — so
1715
+ * `readoptOne` computes `state` FIRST and this set is checked only on the
1716
+ * non-done path. It is cleared once the row leaves the processing list (cron
1717
+ * reset → it drains normally as `pending`), so it can never leak.
1718
+ */
1719
+ dontRedispatch = /* @__PURE__ */ new Set();
1720
+ /**
1721
+ * "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
1722
+ * re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
1723
+ * never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
1724
+ * that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
1725
+ * markDone failure must NOT land here (it must still retry next drain). Separate
1726
+ * from `dontRedispatch` because the two concerns are independent: a row can need
1727
+ * "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
1728
+ * the row leaves the processing list, exactly like `dontRedispatch`.
1729
+ */
1730
+ doneUndeliverable = /* @__PURE__ */ new Set();
1731
+ /**
1732
+ * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
1733
+ * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
1734
+ * is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
1735
+ * persist hasn't landed before tick N+1 re-reads the still-null
1736
+ * `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
1737
+ * A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
1738
+ * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
1739
+ * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
1740
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
1741
+ * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
1742
+ * so the NEXT tick may retry exactly once more).
1743
+ */
1744
+ awaitingReadopt = /* @__PURE__ */ new Set();
1543
1745
  /**
1544
1746
  * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1545
1747
  * first session creation so drain-created sessions are rooted at the project
@@ -1547,8 +1749,33 @@ var ChannelDriver = class {
1547
1749
  * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1548
1750
  */
1549
1751
  opencodeDirectory = void 0;
1752
+ /**
1753
+ * Cache of opencode `sessionId → parentID` (its parent session, or `null` when
1754
+ * the session is a root with no parent). Sub-agents spawned via the `task` tool
1755
+ * run in CHILD sessions whose `parentID` chains up to the Evident-created
1756
+ * (watched) session; we resolve this once per session so a child-session
1757
+ * question/permission can be attributed to the watched session's subtree
1758
+ * (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
1759
+ * entry = not yet resolved; `null` = resolved root (stop walking).
1760
+ */
1761
+ sessionParents = /* @__PURE__ */ new Map();
1550
1762
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1551
1763
  draining = false;
1764
+ /**
1765
+ * The currently-executing `drainPending()` promise, or null when idle. Lets a
1766
+ * graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
1767
+ * is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
1768
+ * drain that entered before `stop()` still registers its watcher).
1769
+ */
1770
+ activeDrain = null;
1771
+ /**
1772
+ * Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
1773
+ * dispatches NEW work (it returns 0 immediately) — but the per-session watcher
1774
+ * loops already running keep going so in-flight turns can finish and deliver
1775
+ * their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
1776
+ * and stops opencode.
1777
+ */
1778
+ stopped = false;
1552
1779
  constructor(config2) {
1553
1780
  this.agentId = config2.agentId;
1554
1781
  this.port = config2.port;
@@ -1562,6 +1789,8 @@ var ChannelDriver = class {
1562
1789
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1563
1790
  this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1564
1791
  this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
1792
+ this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
1793
+ this.now = config2.now ?? (() => Date.now());
1565
1794
  }
1566
1795
  /** The IPv4-loopback base URL for the local `opencode serve`. */
1567
1796
  get opencodeBase() {
@@ -1571,16 +1800,29 @@ var ChannelDriver = class {
1571
1800
  // Public API
1572
1801
  // -------------------------------------------------------------------------
1573
1802
  /**
1574
- * Drain all pending channel conversations once: poll → processcallback.
1803
+ * Drain all pending channel conversations once: poll → dispatchregister.
1575
1804
  * Called on tunnel `connected` (WI-CHAN-4) and on each poll tick by `run.ts`.
1576
1805
  * Re-entrant calls while a drain is in flight are skipped (return 0).
1577
1806
  *
1578
- * @returns the number of messages processed.
1807
+ * @returns the number of messages NEWLY dispatched to opencode's native queue.
1579
1808
  */
1580
1809
  async drainPending() {
1810
+ if (this.stopped) return 0;
1581
1811
  if (this.draining) return 0;
1582
1812
  this.draining = true;
1583
- let processed = 0;
1813
+ const run2 = this.runDrain();
1814
+ this.activeDrain = run2.then(
1815
+ () => {
1816
+ this.activeDrain = null;
1817
+ },
1818
+ () => {
1819
+ this.activeDrain = null;
1820
+ }
1821
+ );
1822
+ return run2;
1823
+ }
1824
+ async runDrain() {
1825
+ let dispatched = 0;
1584
1826
  try {
1585
1827
  const conversations = await this.getPendingConversations();
1586
1828
  if (conversations.length > 0) {
@@ -1591,95 +1833,165 @@ var ChannelDriver = class {
1591
1833
  });
1592
1834
  }
1593
1835
  for (const conv of conversations) {
1594
- processed += await this.processConversation(conv);
1836
+ if (this.stopped) break;
1837
+ dispatched += await this.processConversation(conv);
1595
1838
  }
1839
+ await this.readoptProcessing();
1596
1840
  } finally {
1597
1841
  this.draining = false;
1598
1842
  }
1599
- return processed;
1843
+ return dispatched;
1844
+ }
1845
+ /**
1846
+ * True while any per-session watcher has a non-empty in-flight dispatched set
1847
+ * (Task 3.7). `run.ts` treats this as NON-idle so `--idle-timeout` cannot exit
1848
+ * the process while a dispatched message is still queued/running — which would
1849
+ * kill the turn and orphan its reply.
1850
+ */
1851
+ hasInFlightWatchers() {
1852
+ for (const watcher of this.watchers.values()) {
1853
+ if (watcher.inFlight.size > 0) return true;
1854
+ }
1855
+ return false;
1856
+ }
1857
+ /**
1858
+ * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
1859
+ * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
1860
+ * — but the watcher loops already tracking in-flight turns keep running, so a
1861
+ * turn that has finished (or is about to) still fires `markDone` and delivers
1862
+ * its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
1863
+ */
1864
+ stop() {
1865
+ this.stopped = true;
1866
+ }
1867
+ /**
1868
+ * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
1869
+ * graceful shutdown, so a turn whose reply is ready — or completes within the
1870
+ * window — is delivered before the process exits, instead of being cut off and
1871
+ * left for the ADR-0046 restart-recovery path.
1872
+ *
1873
+ * Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
1874
+ * far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
1875
+ * window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
1876
+ * set empties OR the timeout elapses. Anything still in flight at the timeout is
1877
+ * safe to abandon — it stays `processing` server-side and is re-adopted on the
1878
+ * next runner start (ADR-0046).
1879
+ *
1880
+ * @returns true if all in-flight work settled within the window; false if the
1881
+ * timeout elapsed with work still in flight.
1882
+ */
1883
+ async waitForInFlight(timeoutMs) {
1884
+ const deadline = this.now() + timeoutMs;
1885
+ const step = Math.min(this.pausedPollIntervalMs, 250);
1886
+ if (this.activeDrain) {
1887
+ let drainSettled = false;
1888
+ void this.activeDrain.then(() => {
1889
+ drainSettled = true;
1890
+ });
1891
+ while (!drainSettled) {
1892
+ if (this.now() >= deadline) return false;
1893
+ await this.sleep(step);
1894
+ }
1895
+ }
1896
+ while (this.hasInFlightWatchers()) {
1897
+ if (this.now() >= deadline) return false;
1898
+ await this.sleep(step);
1899
+ }
1900
+ return true;
1600
1901
  }
1601
1902
  /**
1602
- * Await all outstanding paused-session watchers (WI-2-CLI).
1903
+ * Await all outstanding per-session watchers (WI-3).
1603
1904
  *
1604
- * In production the watchers are deliberately started-not-awaited so the drain
1605
- * loop never blocks on them and process exit is not held up (the cron recovers
1606
- * any abandoned ones). This helper exists primarily for deterministic tests
1607
- * that need to observe the watcher's effect (the `done` PATCH or its giving up)
1608
- * after a non-blocking `drainPending`. Watchers never reject, so this resolves.
1905
+ * In production the watcher loops are deliberately started-not-awaited so the
1906
+ * drain loop never blocks on them and process exit is not held up (the cron
1907
+ * recovers any abandoned ones). This helper exists primarily for deterministic
1908
+ * tests that need to observe a watcher's effect (the `processing`/`done` PATCH
1909
+ * or its giving up) after a non-blocking `drainPending`. Watcher loops never
1910
+ * reject, so this resolves.
1609
1911
  */
1610
1912
  async flushPausedWatchers() {
1611
- await Promise.all([...this.watchers.values()]);
1913
+ while (true) {
1914
+ const loops = [...this.watchers.values()].map((w) => w.loop).filter((l) => l != null);
1915
+ if (loops.length === 0) return;
1916
+ await Promise.all(loops);
1917
+ const stillLive = [...this.watchers.values()].some((w) => w.loop != null);
1918
+ if (!stillLive) return;
1919
+ }
1612
1920
  }
1613
1921
  // -------------------------------------------------------------------------
1614
- // Conversation processing
1922
+ // Conversation processing (WI-3 — async dispatch)
1615
1923
  // -------------------------------------------------------------------------
1924
+ /**
1925
+ * Dispatch each pending message for a conversation to opencode's native queue
1926
+ * via `prompt_async` (Task 3.2) and register it with the conversation's
1927
+ * per-session watcher. Does NOT block on the turn and does NOT call
1928
+ * `markProcessing` here — that fires from the watcher on running-start.
1929
+ *
1930
+ * @returns the count of messages NEWLY dispatched (not already in-flight).
1931
+ */
1616
1932
  async processConversation(conv) {
1617
1933
  const sessionId = await this.ensureSession(conv);
1618
1934
  const messages = await this.getPendingMessages(conv.id);
1619
- let processed = 0;
1935
+ let dispatched = 0;
1936
+ let skippedAlreadyDispatched = 0;
1620
1937
  for (const message of messages) {
1621
- const claimed = await this.markProcessing(conv.id, message.id, sessionId);
1622
- if (!claimed) {
1623
- this.log({
1624
- level: "info",
1625
- message: `Message ${message.id.slice(0, 8)} already claimed \u2014 skipping`,
1626
- conversation_id: conv.id,
1627
- message_id: message.id
1628
- });
1938
+ if (this.stopped) break;
1939
+ if (this.dispatched.has(message.id)) {
1940
+ skippedAlreadyDispatched += 1;
1629
1941
  continue;
1630
1942
  }
1943
+ const options = {
1944
+ agent: message.opencode_agent ?? void 0,
1945
+ model: message.opencode_model ?? void 0
1946
+ };
1947
+ let opencodeMessageId;
1631
1948
  try {
1632
1949
  this.log({
1633
1950
  level: "info",
1634
- message: `Sending queued message ${message.id.slice(0, 8)} to OpenCode (session ${sessionId.slice(0, 8)})`,
1951
+ message: `Dispatching message ${message.id.slice(0, 8)} to OpenCode native queue (session ${sessionId.slice(0, 8)})`,
1635
1952
  conversation_id: conv.id,
1636
1953
  message_id: message.id
1637
1954
  });
1638
- const result = await sendMessageToOpenCode(
1639
- this.port,
1955
+ opencodeMessageId = await this.dispatchLocked(
1640
1956
  sessionId,
1641
- message.content,
1642
- {
1643
- agent: message.opencode_agent ?? void 0,
1644
- model: message.opencode_model ?? void 0
1645
- },
1646
- {
1647
- onQuestion: (question) => this.reportInteraction(conv.id, "question", question),
1648
- onPermission: (permission) => this.reportInteraction(conv.id, "permission", permission)
1649
- }
1957
+ () => sendPromptAsync(this.port, sessionId, message.content, options)
1650
1958
  );
1651
- if (result.awaitingInteraction) {
1652
- this.log({
1653
- level: "info",
1654
- message: `Message ${message.id.slice(0, 8)} paused awaiting interaction \u2014 watching session for completion`,
1655
- conversation_id: conv.id,
1656
- message_id: message.id
1657
- });
1658
- this.startPausedWatcher(conv, message, sessionId);
1659
- continue;
1660
- }
1661
- await this.confirmCompletion(sessionId);
1662
- await this.markDone(conv.id, message.id, sessionId);
1663
- processed += 1;
1664
- this.log({
1665
- level: "info",
1666
- message: `Message ${message.id.slice(0, 8)} processed`,
1667
- conversation_id: conv.id,
1668
- message_id: message.id
1669
- });
1670
1959
  } catch (err) {
1671
1960
  if (err instanceof ChannelAuthError) throw err;
1961
+ this.dispatched.delete(message.id);
1672
1962
  await this.markFailed(conv.id, message.id).catch(() => {
1673
1963
  });
1674
1964
  this.log({
1675
1965
  level: "error",
1676
- message: `Message ${message.id.slice(0, 8)} failed: ${err instanceof Error ? err.message : String(err)}`,
1966
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${err instanceof Error ? err.message : String(err)}`,
1677
1967
  conversation_id: conv.id,
1678
1968
  message_id: message.id
1679
1969
  });
1970
+ continue;
1971
+ }
1972
+ if (opencodeMessageId === null) {
1973
+ this.log({
1974
+ level: "error",
1975
+ 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`,
1976
+ conversation_id: conv.id,
1977
+ message_id: message.id
1978
+ });
1979
+ continue;
1680
1980
  }
1981
+ this.dispatched.add(message.id);
1982
+ this.registerInFlight(conv, sessionId, message, opencodeMessageId);
1983
+ dispatched += 1;
1984
+ void this.postSignal(conv.id, message.id, "dispatched");
1681
1985
  }
1682
- return processed;
1986
+ if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
1987
+ this.log({
1988
+ level: "error",
1989
+ message: `Conversation ${conv.id.slice(0, 8)} has ${messages.length} pending message(s) but ALL are already marked dispatched locally (in-flight set: ${this.dispatched.size}) \u2014 none sent to OpenCode this tick. If this repeats, a message may be stuck acknowledged-but-never-dispatched (its watcher never settled).`,
1990
+ conversation_id: conv.id
1991
+ });
1992
+ }
1993
+ this.ensureWatcherRunning(sessionId);
1994
+ return dispatched;
1683
1995
  }
1684
1996
  async ensureSession(conv) {
1685
1997
  const cached = this.sessions.get(conv.id);
@@ -1711,118 +2023,873 @@ var ChannelDriver = class {
1711
2023
  }
1712
2024
  return this.opencodeDirectory;
1713
2025
  }
1714
- /**
1715
- * Local reconcile: re-query `GET /session/:id/message` and check whether the
1716
- * last assistant message is message-level complete (`isTurnComplete`).
1717
- *
1718
- * This is a PURE OBSERVABILITY probe on the normal (non-paused) path: the
1719
- * blocking POST already returned, and `markDone` causes the server to re-fetch
1720
- * the messages itself (via `extractTextFromMessages`) when delivering the
1721
- * reply — so this round-trip never gates delivery. We keep it only to surface a
1722
- * truthful diagnostic when opencode hasn't yet recorded a completed assistant
1723
- * turn at reconcile time, then proceed to `markDone` regardless. Uses the
1724
- * injected `fetchImpl` and reuses only the pure `isTurnComplete` predicate.
1725
- */
1726
- async confirmCompletion(sessionId) {
1727
- try {
1728
- const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
1729
- if (!res.ok) return;
1730
- const body = await res.json();
1731
- const messages = Array.isArray(body) ? body : null;
1732
- if (!isTurnComplete(messages)) {
1733
- this.log({
1734
- level: "info",
1735
- message: `Session ${sessionId.slice(0, 8)} messages do not yet show a completed assistant turn on reconcile \u2014 delivering anyway`
1736
- });
1737
- }
1738
- } catch {
1739
- }
1740
- }
1741
2026
  // -------------------------------------------------------------------------
1742
- // Paused-session watcher (WI-2-CLI)
2027
+ // Per-session watcher (WI-3)
1743
2028
  // -------------------------------------------------------------------------
1744
2029
  /**
1745
- * Start (but do NOT await) a watcher that resumes a paused turn to completion.
1746
- *
1747
- * Single-flight per message: if a watcher is already live for this message we
1748
- * skip. The returned watcher promise is tracked in `this.watchers` and removed
1749
- * when it settles; it never rejects (the body is fully guarded), so a failed
1750
- * poll/markDone can never crash the run loop — the cron stays as the safety net.
2030
+ * Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
2031
+ * opencode session (Task 2.1a), so two dispatches into the SAME session can
2032
+ * never interleave and mis-correlate their read-backs. Distinct sessions run
2033
+ * concurrently. The chained tail intentionally ignores the prior result/error
2034
+ * (each dispatch reports its own outcome to its caller).
1751
2035
  */
1752
- startPausedWatcher(conv, message, sessionId) {
1753
- if (this.watchers.has(message.id)) return;
1754
- const watcher = this.watchPausedSession(conv, message, sessionId).finally(() => {
1755
- this.watchers.delete(message.id);
2036
+ dispatchLocked(sessionId, fn) {
2037
+ const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
2038
+ const run2 = prior.then(fn, fn);
2039
+ this.sessionDispatchLocks.set(
2040
+ sessionId,
2041
+ run2.then(
2042
+ () => void 0,
2043
+ () => void 0
2044
+ )
2045
+ );
2046
+ return run2;
2047
+ }
2048
+ /** Register a freshly-dispatched message with its session's watcher state. */
2049
+ registerInFlight(conv, sessionId, message, opencodeMessageId) {
2050
+ let watcher = this.watchers.get(sessionId);
2051
+ if (!watcher) {
2052
+ watcher = {
2053
+ conv,
2054
+ inFlight: /* @__PURE__ */ new Map(),
2055
+ loop: null,
2056
+ reportedQuestions: /* @__PURE__ */ new Set(),
2057
+ reportedPermissions: /* @__PURE__ */ new Set()
2058
+ };
2059
+ this.watchers.set(sessionId, watcher);
2060
+ }
2061
+ const now = this.now();
2062
+ watcher.inFlight.set(message.id, {
2063
+ evidentMessageId: message.id,
2064
+ opencodeMessageId,
2065
+ message,
2066
+ dispatchedAt: now,
2067
+ deadline: now + this.pausedMaxWaitMs,
2068
+ started: false,
2069
+ done: false,
2070
+ stuckReported: false
1756
2071
  });
1757
- this.watchers.set(message.id, watcher);
1758
2072
  }
1759
2073
  /**
1760
- * Poll `GET /session/:id/message` until the SAME paused turn is message-level
1761
- * complete the last message is an ASSISTANT message whose
1762
- * `info.time.completed` is set (the user answered the question/permission in
1763
- * opencode web and opencode finished the turn) then complete it via the
1764
- * EXISTING `markDone` PATCH NO re-send of the original message. The server
1765
- * re-fetches the assistant messages on completion, so the watcher does not
1766
- * pass any reply text.
2074
+ * Register a RE-ADOPTED `processing` message with its session watcher
2075
+ * (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
2076
+ * `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
2077
+ * `now`: a row already `processing` for e.g. 5 min must give up ~5 min from now
2078
+ * (10 min after `processed_at`), not 10 min from now otherwise its deadline
2079
+ * lands ~15 min after `processed_at`, coinciding with the cron reset
2080
+ * double-drive race. `dispatchedAt` stays `now` (only the appear-guard uses it).
1767
2081
  *
1768
- * Fetch seam: the poll uses the injected `this.fetchImpl` (preserving the
1769
- * tests' injection) and reuses only the pure `isTurnComplete` predicate from
1770
- * `session.ts` we do NOT call `getSessionMessages` (which uses the global
1771
- * `fetch`) here.
2082
+ * `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
2083
+ * `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
2084
+ * fresh-run path these differ (a fresh opencode id under the same server row).
1772
2085
  *
1773
- * Bounded by `pausedMaxWaitMs` (default 10 min, strictly < the 15-min cron
1774
- * reset): on timeout we STOP and leave the message `processing` so the cron
1775
- * remains the last-resort safety net. The whole body is wrapped so any
1776
- * poll/markDone failure is logged and swallowed — a watcher MUST NEVER throw
1777
- * out of the run loop.
2086
+ * `started` is set true so the watcher does NOT re-`markProcessing` a row the
2087
+ * server already flipped to `processing`; the running/done transitions still
2088
+ * fire from the watcher's normal branches.
2089
+ */
2090
+ registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
2091
+ let watcher = this.watchers.get(sessionId);
2092
+ if (!watcher) {
2093
+ watcher = {
2094
+ conv,
2095
+ inFlight: /* @__PURE__ */ new Map(),
2096
+ loop: null,
2097
+ reportedQuestions: /* @__PURE__ */ new Set(),
2098
+ reportedPermissions: /* @__PURE__ */ new Set()
2099
+ };
2100
+ this.watchers.set(sessionId, watcher);
2101
+ }
2102
+ watcher.inFlight.set(message.id, {
2103
+ evidentMessageId: message.id,
2104
+ opencodeMessageId,
2105
+ message,
2106
+ dispatchedAt: this.now(),
2107
+ deadline: processedAtMs + this.pausedMaxWaitMs,
2108
+ // The server row is ALREADY `processing`; do not re-fire markProcessing.
2109
+ started: true,
2110
+ done: false,
2111
+ // Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
2112
+ // AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
2113
+ // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2114
+ // re-adopted row left wedged in `queued` still emits the signal once
2115
+ // (#210/#220 observability).
2116
+ stuckReported: false
2117
+ });
2118
+ }
2119
+ /**
2120
+ * Start (but do NOT await) the per-session watcher loop if it has in-flight
2121
+ * work and is not already running. Single-flight per session. The loop is
2122
+ * tracked on the watcher and cleared when it settles; it never rejects (fully
2123
+ * guarded), so a failed poll/callback can never crash the run loop — the cron
2124
+ * stays as the safety net.
1778
2125
  */
1779
- async watchPausedSession(conv, message, sessionId) {
1780
- const deadline = Date.now() + this.pausedMaxWaitMs;
2126
+ ensureWatcherRunning(sessionId) {
2127
+ const watcher = this.watchers.get(sessionId);
2128
+ if (!watcher) return;
2129
+ if (watcher.loop) return;
2130
+ if (watcher.inFlight.size === 0) {
2131
+ this.watchers.delete(sessionId);
2132
+ return;
2133
+ }
2134
+ const loop = this.runWatcherLoop(sessionId, watcher).finally(() => {
2135
+ watcher.loop = null;
2136
+ if (watcher.inFlight.size === 0) {
2137
+ this.watchers.delete(sessionId);
2138
+ }
2139
+ });
2140
+ watcher.loop = loop;
2141
+ }
2142
+ /**
2143
+ * The per-session polling loop (WI-3). Once per tick it:
2144
+ * 1. polls `GET /session/:id/message` once and, per in-flight message,
2145
+ * computes `messageRunState` and fires markProcessing (queued→running) /
2146
+ * markDone (done) exactly once per transition;
2147
+ * 2. applies the idle-path re-dispatch guard (a dispatched message that never
2148
+ * APPEARS → re-dispatch — D1 obligation 2);
2149
+ * 3. polls `/question` + `/permission` (scoped to the session) and surfaces
2150
+ * NEW ones via `reportInteraction`, carrying the PAUSED message's own
2151
+ * `source_message_id`;
2152
+ * 4. drops messages that completed or timed out from the in-flight set.
2153
+ * Exits when the in-flight set empties. Never throws.
2154
+ */
2155
+ async runWatcherLoop(sessionId, watcher) {
1781
2156
  try {
1782
- while (Date.now() < deadline) {
2157
+ while (watcher.inFlight.size > 0) {
1783
2158
  await this.sleep(this.pausedPollIntervalMs);
1784
- let completed = false;
2159
+ let messages = null;
1785
2160
  try {
1786
2161
  const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
1787
2162
  if (res.ok) {
1788
2163
  const body = await res.json();
1789
- const messages = Array.isArray(body) ? body : null;
1790
- completed = isTurnComplete(messages);
2164
+ messages = Array.isArray(body) ? body : null;
1791
2165
  }
1792
2166
  } catch {
1793
2167
  continue;
1794
2168
  }
1795
- if (!completed) continue;
2169
+ for (const inFlight of [...watcher.inFlight.values()]) {
2170
+ await this.serviceInFlightMessage(sessionId, watcher, inFlight, messages);
2171
+ }
2172
+ await this.pollInteractions(sessionId, watcher, messages);
2173
+ }
2174
+ } catch (err) {
2175
+ if (err instanceof ChannelAuthError) {
1796
2176
  this.log({
1797
- level: "info",
1798
- message: `Paused session ${sessionId.slice(0, 8)} completed \u2014 marking message ${message.id.slice(0, 8)} done`,
2177
+ level: "error",
2178
+ message: `Session watcher aborted on auth failure for session ${sessionId.slice(0, 8)} \u2014 clearing in-flight state for re-drive after re-auth: ${err.message}`,
2179
+ conversation_id: watcher.conv.id
2180
+ });
2181
+ for (const evidentMessageId of [...watcher.inFlight.keys()]) {
2182
+ this.readopted.delete(evidentMessageId);
2183
+ this.removeInFlight(watcher, evidentMessageId);
2184
+ }
2185
+ return;
2186
+ }
2187
+ this.log({
2188
+ level: "error",
2189
+ message: `Session watcher failed for session ${sessionId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
2190
+ conversation_id: watcher.conv.id
2191
+ });
2192
+ }
2193
+ }
2194
+ /**
2195
+ * Drive ONE in-flight message's lifecycle from the tick's message snapshot.
2196
+ * Fires markProcessing on queued→running and markDone on done (each once),
2197
+ * applies the idle-path re-dispatch guard, and removes the message from the
2198
+ * in-flight set on completion or timeout.
2199
+ */
2200
+ async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
2201
+ const conv = watcher.conv;
2202
+ const state = messageRunState(messages, inFlight.opencodeMessageId);
2203
+ if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
2204
+ let claimed;
2205
+ try {
2206
+ claimed = await this.markProcessing(
2207
+ conv.id,
2208
+ inFlight.evidentMessageId,
2209
+ sessionId,
2210
+ inFlight.opencodeMessageId
2211
+ );
2212
+ } catch (err) {
2213
+ if (err instanceof ChannelAuthError) throw err;
2214
+ this.log({
2215
+ level: "error",
2216
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
1799
2217
  conversation_id: conv.id,
1800
- message_id: message.id
2218
+ message_id: inFlight.evidentMessageId
1801
2219
  });
1802
- await this.markDone(conv.id, message.id, sessionId);
1803
2220
  return;
1804
2221
  }
2222
+ inFlight.started = true;
2223
+ if (!claimed) {
2224
+ this.log({
2225
+ level: "info",
2226
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
2227
+ conversation_id: conv.id,
2228
+ message_id: inFlight.evidentMessageId
2229
+ });
2230
+ }
2231
+ }
2232
+ if (state === "done") {
2233
+ if (!inFlight.done) {
2234
+ this.log({
2235
+ level: "info",
2236
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
2237
+ conversation_id: conv.id,
2238
+ message_id: inFlight.evidentMessageId
2239
+ });
2240
+ try {
2241
+ await this.markDone(
2242
+ conv.id,
2243
+ inFlight.evidentMessageId,
2244
+ sessionId,
2245
+ inFlight.opencodeMessageId
2246
+ );
2247
+ } catch (err) {
2248
+ if (err instanceof ChannelAuthError) throw err;
2249
+ if (err instanceof ChannelTerminalError) {
2250
+ this.log({
2251
+ level: "error",
2252
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2253
+ conversation_id: conv.id,
2254
+ message_id: inFlight.evidentMessageId
2255
+ });
2256
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2257
+ return;
2258
+ }
2259
+ if (this.now() >= inFlight.deadline) {
2260
+ this.log({
2261
+ level: "error",
2262
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done within the watch window \u2014 leaving for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
2263
+ conversation_id: conv.id,
2264
+ message_id: inFlight.evidentMessageId
2265
+ });
2266
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2267
+ return;
2268
+ }
2269
+ this.log({
2270
+ level: "error",
2271
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2272
+ conversation_id: conv.id,
2273
+ message_id: inFlight.evidentMessageId
2274
+ });
2275
+ return;
2276
+ }
2277
+ inFlight.done = true;
2278
+ }
2279
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2280
+ return;
2281
+ }
2282
+ if (state === "failed") {
2283
+ if (!inFlight.done) {
2284
+ const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
2285
+ this.log({
2286
+ level: "error",
2287
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2288
+ conversation_id: conv.id,
2289
+ message_id: inFlight.evidentMessageId
2290
+ });
2291
+ try {
2292
+ await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
2293
+ } catch (err) {
2294
+ if (err instanceof ChannelAuthError) throw err;
2295
+ if (err instanceof ChannelTerminalError) {
2296
+ this.log({
2297
+ level: "error",
2298
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2299
+ conversation_id: conv.id,
2300
+ message_id: inFlight.evidentMessageId
2301
+ });
2302
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2303
+ return;
2304
+ }
2305
+ if (this.now() >= inFlight.deadline) {
2306
+ this.log({
2307
+ level: "error",
2308
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed within the watch window \u2014 leaving for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
2309
+ conversation_id: conv.id,
2310
+ message_id: inFlight.evidentMessageId
2311
+ });
2312
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2313
+ return;
2314
+ }
2315
+ this.log({
2316
+ level: "error",
2317
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2318
+ conversation_id: conv.id,
2319
+ message_id: inFlight.evidentMessageId
2320
+ });
2321
+ return;
2322
+ }
2323
+ inFlight.done = true;
2324
+ }
2325
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2326
+ return;
2327
+ }
2328
+ const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
2329
+ const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
2330
+ if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
2331
+ inFlight.stuckReported = true;
2332
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2333
+ stuck_for_ms: this.now() - inFlight.dispatchedAt
2334
+ });
2335
+ }
2336
+ if (this.now() >= inFlight.deadline) {
1805
2337
  this.log({
1806
2338
  level: "info",
1807
- message: `Paused session ${sessionId.slice(0, 8)} did not complete within the watch window \u2014 leaving message ${message.id.slice(0, 8)} for the cron safety net`,
2339
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
1808
2340
  conversation_id: conv.id,
1809
- message_id: message.id
2341
+ message_id: inFlight.evidentMessageId
1810
2342
  });
1811
- } catch (err) {
1812
- this.log({
1813
- level: "error",
1814
- message: `Paused-session watcher failed for message ${message.id.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
1815
- conversation_id: conv.id,
1816
- message_id: message.id
2343
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2344
+ watched_for_ms: this.now() - inFlight.dispatchedAt
1817
2345
  });
2346
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
1818
2347
  }
1819
2348
  }
1820
2349
  // -------------------------------------------------------------------------
1821
- // Evident API calls (combinedAuth thread routes)
2350
+ // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
1822
2351
  // -------------------------------------------------------------------------
1823
- async getPendingConversations() {
1824
- const res = await this.fetchImpl(
1825
- `${this.apiUrl}/agents/${this.agentId}/conversations/pending`,
2352
+ /**
2353
+ * Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
2354
+ *
2355
+ * The pending drain only re-drives `pending` rows; a message already flipped to
2356
+ * `processing` before the runner died is watched by nobody until the 15-min
2357
+ * cron resets it. Here we fetch those rows, and per row resolve its correlated
2358
+ * reply against opencode's OWN session store — completing, re-attaching, or
2359
+ * (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
2360
+ * is idempotent per message (Invariant 2): a row a watcher already tracks is
2361
+ * skipped in `readoptOne` — one driver, no double-drive.
2362
+ *
2363
+ * Only `ChannelAuthError` propagates (to `drainPending`, like the pending
2364
+ * path); every other early return LOGS a reason with context — no silent drop.
2365
+ */
2366
+ async readoptProcessing() {
2367
+ const rows = await this.getProcessingMessages();
2368
+ if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
2369
+ const stillProcessing = new Set(rows.map((r) => r.id));
2370
+ for (const id of [...this.dontRedispatch, ...this.doneUndeliverable]) {
2371
+ if (!stillProcessing.has(id)) {
2372
+ const cleared = this.dontRedispatch.delete(id);
2373
+ const clearedUndeliverable = this.doneUndeliverable.delete(id);
2374
+ if (cleared || clearedUndeliverable) {
2375
+ this.log({
2376
+ level: "info",
2377
+ message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
2378
+ message_id: id
2379
+ });
2380
+ }
2381
+ }
2382
+ }
2383
+ }
2384
+ if (rows.length === 0) return;
2385
+ const bySession = /* @__PURE__ */ new Map();
2386
+ for (const row of rows) {
2387
+ if (!row.opencode_session_id) {
2388
+ this.log({
2389
+ level: "error",
2390
+ message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
2391
+ conversation_id: row.conversation_id,
2392
+ message_id: row.id
2393
+ });
2394
+ continue;
2395
+ }
2396
+ const list = bySession.get(row.opencode_session_id) ?? [];
2397
+ list.push(row);
2398
+ bySession.set(row.opencode_session_id, list);
2399
+ }
2400
+ for (const [sessionId, sessionRows] of bySession) {
2401
+ let messages;
2402
+ try {
2403
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
2404
+ if (!res.ok) {
2405
+ this.log({
2406
+ level: "error",
2407
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
2408
+ });
2409
+ continue;
2410
+ }
2411
+ const body = await res.json();
2412
+ if (!Array.isArray(body)) {
2413
+ this.log({
2414
+ level: "error",
2415
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
2416
+ });
2417
+ continue;
2418
+ }
2419
+ messages = body;
2420
+ } catch (err) {
2421
+ this.log({
2422
+ level: "error",
2423
+ message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
2424
+ });
2425
+ continue;
2426
+ }
2427
+ for (const row of sessionRows) {
2428
+ await this.readoptOne(sessionId, row, messages);
2429
+ }
2430
+ }
2431
+ }
2432
+ /**
2433
+ * Re-adopt ONE `processing` row against the tick's session message snapshot
2434
+ * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
2435
+ *
2436
+ * Branches on `messageRunState(messages, row.opencode_message_id)` — the
2437
+ * opencode-assigned user-message id persisted on the first `processing` PATCH
2438
+ * (#218). A row with a NULL stored id (dispatched but the read-back never landed
2439
+ * before the restart) has no id to correlate → treated as an orphan and
2440
+ * re-dispatched (at most once, see `forceReadoptRun`):
2441
+ * - `done` → `markDone` now (guarded like the watcher's done branch);
2442
+ * - `failed` → `markFailed` with the surfaced error (issue #182), so an
2443
+ * errored turn is reported failed on restart, NOT re-dispatched;
2444
+ * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
2445
+ * tracking the stored id so the reply correlates by it;
2446
+ * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
2447
+ *
2448
+ * Only `ChannelAuthError` propagates.
2449
+ */
2450
+ async readoptOne(sessionId, row, messages) {
2451
+ if (this.isTracked(sessionId, row.id)) {
2452
+ this.log({
2453
+ level: "info",
2454
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
2455
+ conversation_id: row.conversation_id,
2456
+ message_id: row.id
2457
+ });
2458
+ return;
2459
+ }
2460
+ const ocId = row.opencode_message_id;
2461
+ const state = messageRunState(messages, ocId ?? "");
2462
+ if (state === "done") {
2463
+ if (this.doneUndeliverable.has(row.id)) {
2464
+ this.log({
2465
+ level: "info",
2466
+ message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
2467
+ conversation_id: row.conversation_id,
2468
+ message_id: row.id
2469
+ });
2470
+ return;
2471
+ }
2472
+ this.log({
2473
+ level: "info",
2474
+ message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
2475
+ conversation_id: row.conversation_id,
2476
+ message_id: row.id
2477
+ });
2478
+ try {
2479
+ await this.markDone(row.conversation_id, row.id, sessionId, ocId);
2480
+ } catch (err) {
2481
+ if (err instanceof ChannelAuthError) throw err;
2482
+ if (err instanceof ChannelTerminalError) {
2483
+ this.doneUndeliverable.add(row.id);
2484
+ this.log({
2485
+ level: "error",
2486
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 parking until it leaves processing; leaving for the cron safety net: ${err.message}`,
2487
+ conversation_id: row.conversation_id,
2488
+ message_id: row.id
2489
+ });
2490
+ return;
2491
+ }
2492
+ this.log({
2493
+ level: "error",
2494
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2495
+ conversation_id: row.conversation_id,
2496
+ message_id: row.id
2497
+ });
2498
+ return;
2499
+ }
2500
+ this.dontRedispatch.delete(row.id);
2501
+ return;
2502
+ }
2503
+ if (state === "failed") {
2504
+ const error2 = messageError(messages, ocId ?? "") ?? void 0;
2505
+ this.log({
2506
+ level: "error",
2507
+ message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2508
+ conversation_id: row.conversation_id,
2509
+ message_id: row.id
2510
+ });
2511
+ try {
2512
+ await this.markFailed(row.conversation_id, row.id, sessionId, error2);
2513
+ } catch (err) {
2514
+ if (err instanceof ChannelAuthError) throw err;
2515
+ if (err instanceof ChannelTerminalError) {
2516
+ this.doneUndeliverable.add(row.id);
2517
+ this.log({
2518
+ level: "error",
2519
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 parking until it leaves processing; leaving for the cron safety net: ${err.message}`,
2520
+ conversation_id: row.conversation_id,
2521
+ message_id: row.id
2522
+ });
2523
+ return;
2524
+ }
2525
+ this.log({
2526
+ level: "error",
2527
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2528
+ conversation_id: row.conversation_id,
2529
+ message_id: row.id
2530
+ });
2531
+ return;
2532
+ }
2533
+ this.dontRedispatch.delete(row.id);
2534
+ return;
2535
+ }
2536
+ if (this.dontRedispatch.has(row.id)) {
2537
+ this.log({
2538
+ level: "info",
2539
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
2540
+ conversation_id: row.conversation_id,
2541
+ message_id: row.id
2542
+ });
2543
+ return;
2544
+ }
2545
+ if ((state === "running" || state === "queued") && ocId) {
2546
+ const conv = this.convForRow(sessionId, row);
2547
+ const message = this.queuedMessageForRow(row);
2548
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
2549
+ this.dispatched.add(row.id);
2550
+ this.readopted.add(row.id);
2551
+ this.ensureWatcherRunning(sessionId);
2552
+ this.log({
2553
+ level: "info",
2554
+ message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
2555
+ conversation_id: row.conversation_id,
2556
+ message_id: row.id
2557
+ });
2558
+ return;
2559
+ }
2560
+ await this.forceReadoptRun(sessionId, row);
2561
+ }
2562
+ /**
2563
+ * Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
2564
+ *
2565
+ * #218/WI-5: the row's user message is absent (never kept, or a null stored id),
2566
+ * so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
2567
+ * read it back, and register the watcher under the assigned id so the reply
2568
+ * correlates server-side.
2569
+ *
2570
+ * ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
2571
+ * id). Without a guard, if this dispatches on tick N but the read-back+persist
2572
+ * hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
2573
+ * tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
2574
+ * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
2575
+ * short-circuit while the row is latched; clear it on a successful dispatch (the
2576
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
2577
+ * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
2578
+ * may retry exactly once more).
2579
+ *
2580
+ * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
2581
+ * `processed_at` (Invariant 1).
2582
+ */
2583
+ async forceReadoptRun(sessionId, row) {
2584
+ if (this.stopped) {
2585
+ this.log({
2586
+ level: "info",
2587
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned but the runner is stopping \u2014 not starting a fresh turn; leaving for restart recovery`,
2588
+ conversation_id: row.conversation_id,
2589
+ message_id: row.id
2590
+ });
2591
+ return;
2592
+ }
2593
+ if (this.awaitingReadopt.has(row.id)) {
2594
+ this.log({
2595
+ level: "info",
2596
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
2597
+ conversation_id: row.conversation_id,
2598
+ message_id: row.id
2599
+ });
2600
+ return;
2601
+ }
2602
+ if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
2603
+ this.dontRedispatch.add(row.id);
2604
+ this.log({
2605
+ level: "info",
2606
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned but its re-adopt window has already elapsed \u2014 not dispatching an unwatchable turn; parking until it leaves processing (cron will reset it)`,
2607
+ conversation_id: row.conversation_id,
2608
+ message_id: row.id
2609
+ });
2610
+ return;
2611
+ }
2612
+ const options = {
2613
+ agent: row.opencode_agent ?? void 0,
2614
+ model: row.opencode_model ?? void 0
2615
+ };
2616
+ this.log({
2617
+ level: "info",
2618
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
2619
+ conversation_id: row.conversation_id,
2620
+ message_id: row.id
2621
+ });
2622
+ this.awaitingReadopt.add(row.id);
2623
+ let ocId;
2624
+ try {
2625
+ ocId = await this.dispatchLocked(
2626
+ sessionId,
2627
+ () => sendPromptAsync(this.port, sessionId, row.content, options)
2628
+ );
2629
+ } catch (err) {
2630
+ this.awaitingReadopt.delete(row.id);
2631
+ if (err instanceof ChannelAuthError) throw err;
2632
+ this.log({
2633
+ level: "error",
2634
+ message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2635
+ conversation_id: row.conversation_id,
2636
+ message_id: row.id
2637
+ });
2638
+ return;
2639
+ }
2640
+ if (ocId === null) {
2641
+ this.awaitingReadopt.delete(row.id);
2642
+ this.log({
2643
+ level: "error",
2644
+ 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`,
2645
+ conversation_id: row.conversation_id,
2646
+ message_id: row.id
2647
+ });
2648
+ return;
2649
+ }
2650
+ const conv = this.convForRow(sessionId, row);
2651
+ const message = this.queuedMessageForRow(row);
2652
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
2653
+ this.dispatched.add(row.id);
2654
+ this.readopted.add(row.id);
2655
+ this.awaitingReadopt.delete(row.id);
2656
+ this.ensureWatcherRunning(sessionId);
2657
+ }
2658
+ /**
2659
+ * True if `evidentMessageId` is already being driven — either in the
2660
+ * authoritative `dispatched` set or a live watcher's in-flight set for this
2661
+ * session (Invariant 2, WI-5). Either signal means a watcher owns the row.
2662
+ */
2663
+ isTracked(sessionId, evidentMessageId) {
2664
+ if (this.dispatched.has(evidentMessageId)) return true;
2665
+ const watcher = this.watchers.get(sessionId);
2666
+ return watcher?.inFlight.has(evidentMessageId) ?? false;
2667
+ }
2668
+ /**
2669
+ * Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
2670
+ * deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
2671
+ * for `processing` rows, but if it is somehow null/unparseable fall back to
2672
+ * `now` (defensive) AND log — a fallback means the anchor is weaker than
2673
+ * intended, which is worth surfacing.
2674
+ */
2675
+ processedAtMs(row) {
2676
+ const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
2677
+ if (!Number.isNaN(parsed)) return parsed;
2678
+ this.log({
2679
+ level: "error",
2680
+ message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
2681
+ conversation_id: row.conversation_id,
2682
+ message_id: row.id
2683
+ });
2684
+ return this.now();
2685
+ }
2686
+ /** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
2687
+ convForRow(sessionId, row) {
2688
+ return {
2689
+ id: row.conversation_id,
2690
+ agent_id: this.agentId,
2691
+ opencode_session_id: sessionId,
2692
+ pending_message_count: 0,
2693
+ oldest_pending_at: row.processed_at
2694
+ };
2695
+ }
2696
+ /** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
2697
+ queuedMessageForRow(row) {
2698
+ return {
2699
+ id: row.id,
2700
+ content: row.content,
2701
+ status: "processing",
2702
+ opencode_agent: row.opencode_agent,
2703
+ opencode_model: row.opencode_model,
2704
+ source_message_id: row.source_message_id,
2705
+ slack_user_id: row.slack_user_id
2706
+ };
2707
+ }
2708
+ /**
2709
+ * Remove a message from the in-flight set AND the authoritative dispatched
2710
+ * set. Once the in-flight set empties, the watcher loop's `while` guard exits
2711
+ * and its `.finally` removes the session entry from `this.watchers`.
2712
+ *
2713
+ * Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
2714
+ * (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
2715
+ * cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
2716
+ * re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
2717
+ * re-adopted message that completed (`done`) needs no marker — it's leaving
2718
+ * `processing`. This suppresses only re-dispatch: if its reply later completes,
2719
+ * the done branch still delivers it (Bugbot #202).
2720
+ */
2721
+ removeInFlight(watcher, evidentMessageId) {
2722
+ const inFlight = watcher.inFlight.get(evidentMessageId);
2723
+ if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
2724
+ this.dontRedispatch.add(evidentMessageId);
2725
+ this.log({
2726
+ level: "info",
2727
+ message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
2728
+ conversation_id: watcher.conv.id,
2729
+ message_id: evidentMessageId
2730
+ });
2731
+ }
2732
+ watcher.inFlight.delete(evidentMessageId);
2733
+ this.dispatched.delete(evidentMessageId);
2734
+ }
2735
+ /**
2736
+ * Poll `/question` + `/permission` (scoped to the session) and surface NEW ones
2737
+ * via `reportInteraction` (Task 3.5), carrying the PAUSED message's own
2738
+ * `source_message_id` so the server @mentions the correct person under
2739
+ * concurrency. Dedups by interaction id across ticks (reused per-session sets).
2740
+ *
2741
+ * The interaction is attributed to the in-flight message it paused on. opencode
2742
+ * stamps a `messageID` on a permission (and `tool.messageID` on a question) =
2743
+ * the assistant message id, whose `parentID` is the user message id — but the
2744
+ * simplest robust attribution here is: the single in-flight message that is
2745
+ * RUNNING (not done) is the one that paused. With one running message that is
2746
+ * unambiguous; with several we prefer an explicit messageID match, else the
2747
+ * oldest running message.
2748
+ */
2749
+ async pollInteractions(sessionId, watcher, messages) {
2750
+ let questions = [];
2751
+ try {
2752
+ const res = await this.fetchImpl(`${this.opencodeBase}/question`);
2753
+ if (res.ok) {
2754
+ const body = await res.json();
2755
+ questions = Array.isArray(body) ? body : [];
2756
+ }
2757
+ } catch {
2758
+ }
2759
+ for (const q of questions) {
2760
+ if (watcher.reportedQuestions.has(q.id)) continue;
2761
+ if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
2762
+ const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
2763
+ const reported = await this.reportInteraction(
2764
+ watcher.conv.id,
2765
+ "question",
2766
+ q,
2767
+ paused?.message.source_message_id ?? void 0
2768
+ );
2769
+ if (reported) watcher.reportedQuestions.add(q.id);
2770
+ }
2771
+ let permissions = [];
2772
+ try {
2773
+ const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
2774
+ if (res.ok) {
2775
+ const body = await res.json();
2776
+ permissions = Array.isArray(body) ? body : [];
2777
+ }
2778
+ } catch {
2779
+ }
2780
+ for (const p of permissions) {
2781
+ if (watcher.reportedPermissions.has(p.id)) continue;
2782
+ if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
2783
+ const paused = this.attributeInteraction(watcher, p.messageID, messages);
2784
+ const reported = await this.reportInteraction(
2785
+ watcher.conv.id,
2786
+ "permission",
2787
+ p,
2788
+ paused?.message.source_message_id ?? void 0
2789
+ );
2790
+ if (reported) watcher.reportedPermissions.add(p.id);
2791
+ }
2792
+ }
2793
+ /**
2794
+ * True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
2795
+ * i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
2796
+ * watched root. Sub-agents spawned via the `task` tool run in child sessions,
2797
+ * so their questions/permissions live under a different `sessionID` that must
2798
+ * still be attributed to the root conversation the watcher owns.
2799
+ *
2800
+ * Parents are cached in `sessionParents` so we walk each session at most once;
2801
+ * a bounded depth cap guards against a cycle or a pathological chain, and any
2802
+ * fetch failure is treated as "not a descendant" (best-effort — the interaction
2803
+ * simply isn't surfaced this tick and is retried next tick once resolvable).
2804
+ */
2805
+ async sessionBelongsTo(sessionId, rootSessionId) {
2806
+ let current = sessionId;
2807
+ for (let depth = 0; current && depth < 32; depth++) {
2808
+ if (current === rootSessionId) return true;
2809
+ const parent = await this.resolveSessionParent(current);
2810
+ if (parent === null || parent === void 0) return false;
2811
+ current = parent;
2812
+ }
2813
+ return false;
2814
+ }
2815
+ /**
2816
+ * Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
2817
+ * `null` for a root session (no parent) and `undefined` when opencode is
2818
+ * unreachable / the session can't be read (so the caller stops walking without
2819
+ * caching a wrong answer — the next tick retries).
2820
+ */
2821
+ async resolveSessionParent(sessionId) {
2822
+ const cached = this.sessionParents.get(sessionId);
2823
+ if (cached !== void 0) return cached;
2824
+ let parent = void 0;
2825
+ try {
2826
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
2827
+ if (res.ok) {
2828
+ const body = await res.json();
2829
+ parent = body && typeof body.parentID === "string" ? body.parentID : null;
2830
+ }
2831
+ } catch {
2832
+ parent = void 0;
2833
+ }
2834
+ if (parent !== void 0) this.sessionParents.set(sessionId, parent);
2835
+ return parent;
2836
+ }
2837
+ /**
2838
+ * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
2839
+ *
2840
+ * The interaction carries `interactionMessageId` — the ASSISTANT message id
2841
+ * that raised it (a question's `tool.messageID` / a permission's `messageID`).
2842
+ * That assistant message is the reply to ONE of our minted user messages
2843
+ * (correlated by `parentID`, GATE-B). So when we have the tick's message
2844
+ * snapshot, we resolve each running in-flight message's correlated assistant
2845
+ * reply (`findAssistantReplyAfter`) and match its id against
2846
+ * `interactionMessageId` — giving an EXACT attribution even with several
2847
+ * messages in flight concurrently in one session.
2848
+ *
2849
+ * We fall back to the oldest running message ONLY when no exact match is
2850
+ * possible (the id is absent, the snapshot is missing, or the reply has not yet
2851
+ * been correlated). With a single running message either path is exact. Never
2852
+ * throws.
2853
+ *
2854
+ * Attribution must NOT depend on our own `started` PATCH flag: opencode can
2855
+ * START a turn AND raise a question/permission BEFORE our next tick fires
2856
+ * `markProcessing` (which sets `started`). Relying on `started` would leave the
2857
+ * running set empty in that window and let the server fall back to "newest
2858
+ * processing/pending" — possibly @mentioning a FOLLOW-UP author rather than the
2859
+ * person whose active turn actually paused. So we derive "running" from the
2860
+ * tick's `messages` snapshot via `messageRunState` instead.
2861
+ */
2862
+ attributeInteraction(watcher, interactionMessageId, messages) {
2863
+ const inFlight = [...watcher.inFlight.values()].filter((m) => !m.done);
2864
+ if (inFlight.length === 0) return void 0;
2865
+ if (interactionMessageId && messages) {
2866
+ const exact = inFlight.find((m) => {
2867
+ const reply = findAssistantReplyAfter(messages, m.opencodeMessageId);
2868
+ return reply != null && messageIdOf(reply) === interactionMessageId;
2869
+ });
2870
+ if (exact) return exact;
2871
+ }
2872
+ const byOldest = (a, b) => a.dispatchedAt - b.dispatchedAt;
2873
+ if (messages) {
2874
+ const runningPerSnapshot = inFlight.filter(
2875
+ (m) => messageRunState(messages, m.opencodeMessageId) === "running"
2876
+ );
2877
+ if (runningPerSnapshot.length > 0) {
2878
+ return runningPerSnapshot.sort(byOldest)[0];
2879
+ }
2880
+ }
2881
+ const startedRunning = inFlight.filter((m) => m.started);
2882
+ if (startedRunning.length > 0) {
2883
+ return startedRunning.sort(byOldest)[0];
2884
+ }
2885
+ return inFlight.sort(byOldest)[0];
2886
+ }
2887
+ // -------------------------------------------------------------------------
2888
+ // Evident API calls (combinedAuth thread routes)
2889
+ // -------------------------------------------------------------------------
2890
+ async getPendingConversations() {
2891
+ const res = await this.fetchImpl(
2892
+ `${this.apiUrl}/agents/${this.agentId}/conversations/pending`,
1826
2893
  {
1827
2894
  headers: { Authorization: this.getAuthHeader() }
1828
2895
  }
@@ -1849,52 +2916,182 @@ var ChannelDriver = class {
1849
2916
  }
1850
2917
  return await res.json();
1851
2918
  }
1852
- async markProcessing(conversationId, messageId, sessionId) {
2919
+ /**
2920
+ * Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
2921
+ * The pending path (`getPendingConversations`/`getPendingMessages`) only
2922
+ * surfaces `pending` rows, so a message already `processing` when the runner
2923
+ * died is invisible to it — this dedicated endpoint returns exactly those rows
2924
+ * with the fields the re-adopt path needs (`processed_at`,
2925
+ * `opencode_session_id`, routing).
2926
+ *
2927
+ * Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
2928
+ * array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
2929
+ * other non-ok so `drainPending`'s try/finally leaves `draining` false and the
2930
+ * next tick retries.
2931
+ */
2932
+ async getProcessingMessages() {
2933
+ const res = await this.fetchImpl(
2934
+ `${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
2935
+ { headers: { Authorization: this.getAuthHeader() } }
2936
+ );
2937
+ this.assertAuth(res, "fetching processing messages");
2938
+ if (!res.ok) {
2939
+ throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
2940
+ }
2941
+ const data = await res.json();
2942
+ let messages = data.messages ?? [];
2943
+ if (this.conversationFilter) {
2944
+ messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
2945
+ }
2946
+ return messages;
2947
+ }
2948
+ /**
2949
+ * EXISTING combinedAuth route — now fired by the watcher on queued→running
2950
+ * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
2951
+ * opencode_session_id}` → `notifyMessageStarted` (hourglass→runner swap +
2952
+ * deep-linked "View in Evident" notice).
2953
+ *
2954
+ * Return/throw contract (consumed by the watcher's swap-to-running guard):
2955
+ * - returns `true` → the server transitioned the row to processing;
2956
+ * - returns `false` → the server gave a DEFINITIVE "already-processing"
2957
+ * answer (a non-retryable, non-auth status — e.g. a
2958
+ * conflict because a duplicate already transitioned it),
2959
+ * so the caller treats it as already-started and does NOT
2960
+ * retry;
2961
+ * - throws `ChannelAuthError` on 401/403 (terminal auth failure);
2962
+ * - throws on a TRANSIENT failure (retryable 5xx/429 status, or a
2963
+ * network-level error from `fetch`) — i.e. NO definitive server response —
2964
+ * so the caller leaves the message un-started and retries the swap on the
2965
+ * next tick.
2966
+ * A single attempt (no internal retry): the watcher's per-tick loop is the
2967
+ * retry vehicle for the swap-to-running.
2968
+ */
2969
+ async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
1853
2970
  const res = await this.fetchImpl(
1854
2971
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1855
2972
  {
1856
2973
  method: "PATCH",
1857
2974
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1858
- // Include the opencode session id so the server can deep-link the
1859
- // "View in Evident" notice straight to the conversation (the session
1860
- // already exists by now — ensureSession runs before claiming).
1861
- body: JSON.stringify({ status: "processing", opencode_session_id: sessionId })
2975
+ body: JSON.stringify({
2976
+ status: "processing",
2977
+ opencode_session_id: sessionId,
2978
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
2979
+ })
1862
2980
  }
1863
2981
  );
1864
2982
  this.assertAuth(res, "marking message as processing");
1865
- return res.ok;
2983
+ if (res.ok) return true;
2984
+ if (isRetryableStatus(res.status)) {
2985
+ throw new Error(`marking message as processing: HTTP ${res.status}`);
2986
+ }
2987
+ return false;
1866
2988
  }
1867
2989
  /**
1868
- * EXISTING combinedAuth completion route — idempotent + retried (WI-CHAN-2).
1869
- * `PATCH .../messages/:id {status:'done', opencode_session_id}`. The server's
2990
+ * EXISTING combinedAuth completion route — idempotent (WI-CHAN-2). `PATCH
2991
+ * .../messages/:id {status:'done', opencode_session_id}`. The server's
1870
2992
  * `queued_conversation_messages.status`/`processed_at` gate makes a re-call
1871
- * for an already-`done` message a no-op (no double Slack post).
2993
+ * for an already-`done` message a no-op (no double Slack post). Fired by the
2994
+ * watcher on per-message completion (Task 3.4) — no `confirmCompletion`
2995
+ * round-trip (we already observed completion via the message list).
2996
+ *
2997
+ * SINGLE ATTEMPT (no in-call `callWithRetry` backoff). The per-session watcher
2998
+ * services its in-flight messages SEQUENTIALLY within a tick
2999
+ * (`runWatcherLoop` → `serviceInFlightMessage`), so a long multi-attempt
3000
+ * backoff here would BLOCK sibling messages in the SAME session/tick: while
3001
+ * message A's done PATCH burned its internal retries, message B could not be
3002
+ * swapped to running even though opencode had already started it. Instead this
3003
+ * does ONE PATCH and surfaces the SAME outcome contract the watcher's markDone
3004
+ * handler already relies on, leaning on the per-tick retry across ticks
3005
+ * (bounded by `inFlight.deadline`) rather than an in-call retry:
3006
+ * - resolves (`void`) → the server transitioned the row to done
3007
+ * (or idempotently confirmed already-done);
3008
+ * - throws `ChannelAuthError` → 401/403 (terminal auth failure → loop
3009
+ * cleanup, Finding 1);
3010
+ * - throws `ChannelTerminalError`→ non-retryable, non-auth 4xx (will never
3011
+ * succeed → straight to the cron, Finding 4);
3012
+ * - throws a plain `Error` → TRANSIENT 5xx/429 or a network-level error
3013
+ * (no definitive server response → the
3014
+ * watcher retries next tick within the
3015
+ * deadline, Finding 4).
3016
+ */
3017
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
3018
+ const res = await this.fetchImpl(
3019
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3020
+ {
3021
+ method: "PATCH",
3022
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
3023
+ body: JSON.stringify({
3024
+ status: "done",
3025
+ opencode_session_id: sessionId,
3026
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
3027
+ })
3028
+ }
3029
+ );
3030
+ this.assertAuth(res, "marking message as done");
3031
+ if (res.ok) return;
3032
+ if (isRetryableStatus(res.status)) {
3033
+ throw new Error(`marking message as done: HTTP ${res.status}`);
3034
+ }
3035
+ throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
3036
+ }
3037
+ /**
3038
+ * Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
3039
+ * when provided (issue #182): a bare `markFailed(conv, msg)` sends
3040
+ * `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
3041
+ * OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
3042
+ * failure reason reaches the channel.
1872
3043
  */
1873
- async markDone(conversationId, messageId, sessionId) {
3044
+ async markFailed(conversationId, messageId, sessionId, error2) {
3045
+ const body = { status: "failed" };
3046
+ if (sessionId !== void 0) body.opencode_session_id = sessionId;
3047
+ if (error2 !== void 0) body.error = error2;
1874
3048
  await this.callWithRetry(
1875
- "marking message as done",
3049
+ "marking message as failed",
1876
3050
  () => this.fetchImpl(
1877
3051
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1878
3052
  {
1879
3053
  method: "PATCH",
1880
3054
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1881
- body: JSON.stringify({ status: "done", opencode_session_id: sessionId })
3055
+ body: JSON.stringify(body)
1882
3056
  }
1883
3057
  )
1884
3058
  );
1885
3059
  }
1886
- async markFailed(conversationId, messageId) {
1887
- await this.callWithRetry(
1888
- "marking message as failed",
1889
- () => this.fetchImpl(
1890
- `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3060
+ /**
3061
+ * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
3062
+ * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
3063
+ * — the server records it via `log()` (no DB write, no notification). This is
3064
+ * fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
3065
+ * MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
3066
+ * watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
3067
+ * context (no silent catch, per development-workflow).
3068
+ */
3069
+ async postSignal(conversationId, messageId, signal, extra) {
3070
+ try {
3071
+ const res = await this.fetchImpl(
3072
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
1891
3073
  {
1892
- method: "PATCH",
3074
+ method: "POST",
1893
3075
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1894
- body: JSON.stringify({ status: "failed" })
3076
+ body: JSON.stringify({ signal, ...extra })
1895
3077
  }
1896
- )
1897
- );
3078
+ );
3079
+ if (!res.ok) {
3080
+ this.log({
3081
+ level: "error",
3082
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
3083
+ conversation_id: conversationId,
3084
+ message_id: messageId
3085
+ });
3086
+ }
3087
+ } catch (err) {
3088
+ this.log({
3089
+ level: "error",
3090
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
3091
+ conversation_id: conversationId,
3092
+ message_id: messageId
3093
+ });
3094
+ }
1898
3095
  }
1899
3096
  async persistSession(conversationId, sessionId) {
1900
3097
  const res = await this.fetchImpl(
@@ -1909,10 +3106,17 @@ var ChannelDriver = class {
1909
3106
  }
1910
3107
  /**
1911
3108
  * EXISTING combinedAuth interaction route (WI-CHAN-3) — idempotent + retried.
1912
- * `POST .../interactive-event {type, data}`. The server persists the
1913
- * interaction and posts a link to the proxied opencode-web conversation.
3109
+ * `POST .../interactive-event {type, data, source_message_id?}`. The server
3110
+ * persists the interaction and posts a link to the proxied opencode-web
3111
+ * conversation, @mentioning the user who triggered THIS message's turn.
3112
+ *
3113
+ * WI-3 / WI-4 contract: `source_message_id` is the PAUSED message's own Slack
3114
+ * ts (`message.source_message_id`). The server resolves the @mention from that
3115
+ * message's user FIRST (falling back to the old "newest processing" precedence
3116
+ * only when absent), so the correct person is mentioned under concurrency. It
3117
+ * is OPTIONAL for back-compat with older clients / legacy rows.
1914
3118
  */
1915
- async reportInteraction(conversationId, type, data) {
3119
+ async reportInteraction(conversationId, type, data, sourceMessageId) {
1916
3120
  try {
1917
3121
  await this.callWithRetry(
1918
3122
  "reporting interactive event",
@@ -1921,7 +3125,9 @@ var ChannelDriver = class {
1921
3125
  {
1922
3126
  method: "POST",
1923
3127
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1924
- body: JSON.stringify({ type, data })
3128
+ body: JSON.stringify(
3129
+ sourceMessageId ? { type, data, source_message_id: sourceMessageId } : { type, data }
3130
+ )
1925
3131
  }
1926
3132
  )
1927
3133
  );
@@ -1930,6 +3136,7 @@ var ChannelDriver = class {
1930
3136
  message: `${type} surfaced to channel (id: ${data.id.slice(0, 8)})`,
1931
3137
  conversation_id: conversationId
1932
3138
  });
3139
+ return true;
1933
3140
  } catch (err) {
1934
3141
  if (err instanceof ChannelAuthError) throw err;
1935
3142
  this.log({
@@ -1937,6 +3144,7 @@ var ChannelDriver = class {
1937
3144
  message: `Failed to surface ${type}: ${err instanceof Error ? err.message : String(err)}`,
1938
3145
  conversation_id: conversationId
1939
3146
  });
3147
+ return false;
1940
3148
  }
1941
3149
  }
1942
3150
  // -------------------------------------------------------------------------
@@ -1975,8 +3183,9 @@ var ChannelDriver = class {
1975
3183
  await this.sleep(backoffDelay(attempt, this.retry));
1976
3184
  continue;
1977
3185
  }
3186
+ break;
1978
3187
  }
1979
- throw new Error(`${context}: HTTP ${res.status}`);
3188
+ throw new ChannelTerminalError(`${context}: HTTP ${res.status}`, res.status);
1980
3189
  }
1981
3190
  throw lastError instanceof Error ? lastError : new Error(`${context}: exhausted retries`);
1982
3191
  }
@@ -2154,6 +3363,25 @@ async function resolveAgentIdFromKey(authHeader) {
2154
3363
  return { error: `Failed to resolve agent from key: ${message}` };
2155
3364
  }
2156
3365
  }
3366
+ async function notifyAgentDisconnected(agentId, authHeader) {
3367
+ const apiUrl = getApiUrlConfig();
3368
+ try {
3369
+ const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
3370
+ method: "POST",
3371
+ headers: { Authorization: authHeader }
3372
+ });
3373
+ if (!response.ok) {
3374
+ const serverMessage = await readErrorMessage(response);
3375
+ return {
3376
+ ok: false,
3377
+ error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
3378
+ };
3379
+ }
3380
+ return { ok: true };
3381
+ } catch (error2) {
3382
+ return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
3383
+ }
3384
+ }
2157
3385
  async function getAgentInfo(agentId, authHeader) {
2158
3386
  const apiUrl = getApiUrlConfig();
2159
3387
  try {
@@ -2199,7 +3427,9 @@ async function getAgentInfo(agentId, authHeader) {
2199
3427
  // src/commands/run.ts
2200
3428
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
2201
3429
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
2202
- function log(state, message, isError = false) {
3430
+ var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
3431
+ var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
3432
+ function log2(state, message, isError = false) {
2203
3433
  if (state.json) {
2204
3434
  console.log(
2205
3435
  JSON.stringify({
@@ -2224,9 +3454,9 @@ function logActivity(state, entry) {
2224
3454
  }
2225
3455
  if (!state.interactive) {
2226
3456
  if (entry.type === "error") {
2227
- log(state, entry.error ?? "Unknown error", true);
3457
+ log2(state, entry.error ?? "Unknown error", true);
2228
3458
  } else if (entry.type === "info" && entry.message) {
2229
- log(state, entry.message);
3459
+ log2(state, entry.message);
2230
3460
  }
2231
3461
  }
2232
3462
  }
@@ -2312,6 +3542,7 @@ async function handleAuthError(state, error2) {
2312
3542
  }
2313
3543
  async function driveChannels(state, driver) {
2314
3544
  let idlePolls = 0;
3545
+ let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
2315
3546
  while (state.running) {
2316
3547
  if (state.connection?.reconnecting && state.connection.reconnectPromise) {
2317
3548
  logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
@@ -2321,9 +3552,11 @@ async function driveChannels(state, driver) {
2321
3552
  try {
2322
3553
  const processed = await driver.drainPending();
2323
3554
  state.messageCount += processed;
2324
- if (processed > 0) {
3555
+ const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
3556
+ lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
3557
+ if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
2325
3558
  idlePolls = 0;
2326
- if (state.interactive) displayStatus(state);
3559
+ if (processed > 0 && state.interactive) displayStatus(state);
2327
3560
  } else if (state.idleTimeout !== null) {
2328
3561
  idlePolls++;
2329
3562
  if (idlePolls === 1) {
@@ -2361,8 +3594,42 @@ async function driveChannels(state, driver) {
2361
3594
  }
2362
3595
  }
2363
3596
  }
2364
- async function cleanup(state) {
3597
+ async function notifyOffline(state) {
3598
+ if (!state.agentId || !state.authHeader) return;
3599
+ if (!state.connected) {
3600
+ log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
3601
+ return;
3602
+ }
3603
+ const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
3604
+ if (result.ok) {
3605
+ log2(state, "Notified Evident the agent is going offline");
3606
+ } else {
3607
+ logActivity(state, {
3608
+ type: "error",
3609
+ error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
3610
+ });
3611
+ if (state.interactive) displayStatus(state);
3612
+ }
3613
+ }
3614
+ async function cleanup(state, opts = {}) {
2365
3615
  state.running = false;
3616
+ if (opts.graceful && state.channelDriver) {
3617
+ state.channelDriver.stop();
3618
+ log2(state, "Draining in-flight channel work before shutdown...");
3619
+ if (state.interactive) {
3620
+ logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
3621
+ displayStatus(state);
3622
+ }
3623
+ const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
3624
+ if (!settled) {
3625
+ logActivity(state, {
3626
+ type: "info",
3627
+ message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
3628
+ });
3629
+ if (state.interactive) displayStatus(state);
3630
+ }
3631
+ }
3632
+ await notifyOffline(state);
2366
3633
  if (state.connection) {
2367
3634
  state.connection.close();
2368
3635
  state.connection = null;
@@ -2373,7 +3640,7 @@ async function cleanup(state) {
2373
3640
  logActivity(state, { type: "info", message: "Stopped OpenCode process" });
2374
3641
  displayStatus(state);
2375
3642
  } else {
2376
- log(state, "Stopped OpenCode process");
3643
+ log2(state, "Stopped OpenCode process");
2377
3644
  }
2378
3645
  state.opencodeProcess = null;
2379
3646
  }
@@ -2393,26 +3660,31 @@ async function run(options) {
2393
3660
  opencodeVersion: null,
2394
3661
  opencodeProcess: null,
2395
3662
  connection: null,
3663
+ channelDriver: null,
2396
3664
  running: true,
3665
+ shuttingDown: false,
2397
3666
  activityLog: [],
2398
3667
  messageCount: 0,
3668
+ lastProxiedActivityAt: null,
2399
3669
  authHeader: ""
2400
3670
  };
2401
3671
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
2402
- log(
3672
+ log2(
2403
3673
  state,
2404
3674
  "Warning: No --idle-timeout set in CI environment. The runner will poll indefinitely until the job times out. Consider adding --idle-timeout 30 to avoid wasting runner minutes.",
2405
3675
  false
2406
3676
  );
2407
3677
  }
2408
3678
  const handleSignal = async () => {
3679
+ if (state.shuttingDown) return;
3680
+ state.shuttingDown = true;
2409
3681
  if (state.interactive) {
2410
3682
  logActivity(state, { type: "info", message: "Shutting down..." });
2411
3683
  displayStatus(state);
2412
3684
  } else {
2413
- log(state, "Shutting down...");
3685
+ log2(state, "Shutting down...");
2414
3686
  }
2415
- await cleanup(state);
3687
+ await cleanup(state, { graceful: true });
2416
3688
  await shutdownTelemetry();
2417
3689
  process.exit(0);
2418
3690
  };
@@ -2443,7 +3715,7 @@ async function run(options) {
2443
3715
  const resolved = await resolveAgentIdFromKey(state.authHeader);
2444
3716
  if (resolved.agent_id) {
2445
3717
  state.agentId = resolved.agent_id;
2446
- log(state, `Resolved agent ID from key: ${state.agentId}`);
3718
+ log2(state, `Resolved agent ID from key: ${state.agentId}`);
2447
3719
  if (state.interactive && !state.json) {
2448
3720
  logActivity(state, {
2449
3721
  type: "info",
@@ -2506,14 +3778,21 @@ async function run(options) {
2506
3778
  port: state.port,
2507
3779
  interactive: state.interactive,
2508
3780
  agentId: state.agentId,
2509
- log: (message) => log(state, message)
3781
+ log: (message) => log2(state, message)
2510
3782
  });
2511
3783
  state.port = oc.port;
2512
3784
  state.opencodeProcess = oc.process;
2513
3785
  state.opencodeVersion = oc.version;
2514
3786
  state.opencodeConnected = oc.process !== null || oc.version !== null;
2515
- const version = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
2516
- ocSpinner?.succeed(`OpenCode running on port ${state.port}${version}`);
3787
+ const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
3788
+ ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
3789
+ const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
3790
+ if (versionWarning) {
3791
+ log2(state, versionWarning, false);
3792
+ if (state.interactive && !state.json) {
3793
+ logActivity(state, { type: "info", message: versionWarning });
3794
+ }
3795
+ }
2517
3796
  } catch (error2) {
2518
3797
  ocSpinner?.fail(error2.message);
2519
3798
  throw error2;
@@ -2525,12 +3804,14 @@ async function run(options) {
2525
3804
  apiUrl: getApiUrlConfig(),
2526
3805
  getAuthHeader: () => state.authHeader,
2527
3806
  conversationFilter: state.conversationFilter,
3807
+ stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
2528
3808
  log: (entry) => logActivity(state, {
2529
3809
  type: entry.level === "error" ? "error" : "info",
2530
3810
  message: entry.message,
2531
3811
  error: entry.level === "error" ? entry.message : void 0
2532
3812
  })
2533
3813
  });
3814
+ state.channelDriver = channelDriver;
2534
3815
  const connection = new RunnerConnection({
2535
3816
  agentId: state.agentId,
2536
3817
  getAuthHeader: () => state.authHeader,
@@ -2544,7 +3825,11 @@ async function run(options) {
2544
3825
  type: "info",
2545
3826
  message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
2546
3827
  });
2547
- emitAgentConnected(state.agentId, { port: state.port });
3828
+ emitAgentConnected(state.agentId, {
3829
+ port: state.port,
3830
+ cli_version: getCliVersion(),
3831
+ opencode_version: state.opencodeVersion
3832
+ });
2548
3833
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
2549
3834
  if (state.interactive) displayStatus(state);
2550
3835
  channelDriver.drainPending().then((processed) => {
@@ -2578,9 +3863,14 @@ async function run(options) {
2578
3863
  logActivity(state, { type: "error", error: error2 });
2579
3864
  if (state.interactive) displayStatus(state);
2580
3865
  },
2581
- // Web traffic is proxied transparently; only note opencode is live.
3866
+ // Web traffic is proxied transparently; note opencode is live and stamp
3867
+ // proxied activity so the idle loop treats interactive proxy use as work.
3868
+ // Fires per forwarded response head (incl. every SSE open) and excludes
3869
+ // the internal drain-ping, so an actively-used proxy keeps the timer
3870
+ // fresh while a lone idle SSE with no follow-up requests still ages out.
2582
3871
  onResponse: () => {
2583
3872
  state.opencodeConnected = true;
3873
+ state.lastProxiedActivityAt = Date.now();
2584
3874
  },
2585
3875
  // A channel message was queued and the api-worker pinged us over the
2586
3876
  // tunnel to drain immediately instead of waiting for the next poll tick.
@@ -2619,9 +3909,10 @@ async function run(options) {
2619
3909
  throw error2;
2620
3910
  }
2621
3911
  if (!interactive || state.json) {
2622
- log(state, "Driving channel messages...");
3912
+ log2(state, "Driving channel messages...");
2623
3913
  }
2624
3914
  await driveChannels(state, channelDriver);
3915
+ if (state.shuttingDown) return;
2625
3916
  await cleanup(state);
2626
3917
  if (state.json) {
2627
3918
  console.log(
@@ -2631,11 +3922,12 @@ async function run(options) {
2631
3922
  })
2632
3923
  );
2633
3924
  } else if (!interactive) {
2634
- log(state, `Completed. Processed ${state.messageCount} message(s).`);
3925
+ log2(state, `Completed. Processed ${state.messageCount} message(s).`);
2635
3926
  }
2636
3927
  await shutdownTelemetry();
2637
3928
  process.exit(0);
2638
3929
  } catch (error2) {
3930
+ if (state.shuttingDown) return;
2639
3931
  await cleanup(state);
2640
3932
  const message = error2 instanceof Error ? error2.message : String(error2);
2641
3933
  if (state.json) {
@@ -2653,8 +3945,9 @@ async function run(options) {
2653
3945
  }
2654
3946
 
2655
3947
  // src/index.ts
3948
+ var { version } = createRequire(import.meta.url)("../package.json");
2656
3949
  var program = new Command();
2657
- program.name("evident").description("Run OpenCode locally and connect it to Evident").version("0.1.0").option(
3950
+ program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
2658
3951
  "--endpoint <url>",
2659
3952
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
2660
3953
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {