@evident-ai/cli 3.0.1-dev.7a6d5aa → 3.0.1-dev.7bf63a1

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;
@@ -685,14 +712,14 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
685
712
  }
686
713
 
687
714
  // src/lib/opencode/opencode-version-gate.ts
688
- var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11"];
689
- function isQueueValidatedVersion(version) {
690
- if (!version) return false;
691
- return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version);
692
- }
693
- function buildOpenCodeVersionWarning(version) {
694
- if (isQueueValidatedVersion(version)) return null;
695
- const detected = version ? `v${version}` : "unknown";
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";
696
723
  const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
697
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.`;
698
725
  }
@@ -1009,7 +1036,11 @@ function roleOf(m) {
1009
1036
  }
1010
1037
  function completedOf(m) {
1011
1038
  if (!m || typeof m !== "object") return void 0;
1012
- 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;
1013
1044
  }
1014
1045
  function idOf(m) {
1015
1046
  if (!m || typeof m !== "object") return void 0;
@@ -1029,6 +1060,24 @@ function finishOf(m) {
1029
1060
  const infoFinish = m.info?.finish;
1030
1061
  return typeof infoFinish === "string" ? infoFinish : void 0;
1031
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";
1070
+ }
1071
+ async function getSessionMessages(port, sessionId) {
1072
+ try {
1073
+ const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/message`);
1074
+ if (!res.ok) return null;
1075
+ const body = await res.json();
1076
+ return Array.isArray(body) ? body : null;
1077
+ } catch {
1078
+ return null;
1079
+ }
1080
+ }
1032
1081
  async function createOpenCodeSession(port, directory) {
1033
1082
  const url = new URL(`${opencodeBase(port)}/session`);
1034
1083
  if (directory && directory.trim()) {
@@ -1046,9 +1095,16 @@ async function createOpenCodeSession(port, directory) {
1046
1095
  const data = await response.json();
1047
1096
  return data.id;
1048
1097
  }
1049
- async function sendPromptAsync(port, sessionId, content, options, messageId) {
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
+ );
1050
1107
  const body = {
1051
- messageID: messageId,
1052
1108
  parts: [{ type: "text", text: content }]
1053
1109
  };
1054
1110
  if (options?.agent) {
@@ -1072,6 +1128,29 @@ async function sendPromptAsync(port, sessionId, content, options, messageId) {
1072
1128
  const text = await res.text().catch(() => "");
1073
1129
  throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1074
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 };
1145
+ }
1146
+ }
1147
+ if (best) return best.id;
1148
+ }
1149
+ if (attempt < READ_BACK_ATTEMPTS - 1) {
1150
+ await new Promise((resolve) => setTimeout(resolve, READ_BACK_DELAY_MS));
1151
+ }
1152
+ }
1153
+ return null;
1075
1154
  }
1076
1155
  function findAssistantReplyAfter(messages, userMessageId) {
1077
1156
  if (!messages || messages.length === 0) return null;
@@ -1088,19 +1167,31 @@ function findAssistantReplyAfter(messages, userMessageId) {
1088
1167
  }
1089
1168
  function findLastAssistantReplyFor(messages, userMessageId) {
1090
1169
  if (!messages || messages.length === 0) return null;
1170
+ let lastCorrelated = null;
1171
+ let lastNonErrored = null;
1091
1172
  for (let i = messages.length - 1; i >= 0; i--) {
1092
1173
  const m = messages[i];
1093
- if (roleOf(m) === "assistant" && parentIdOf(m) === userMessageId) return m;
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
+ }
1094
1180
  }
1181
+ if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
1095
1182
  const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1096
1183
  if (userIndex === -1) return null;
1097
1184
  let last = null;
1185
+ let lastOk = null;
1098
1186
  for (let i = userIndex + 1; i < messages.length; i++) {
1099
1187
  const role = roleOf(messages[i]);
1100
1188
  if (role === "user") break;
1101
- if (role === "assistant") last = messages[i];
1189
+ if (role === "assistant") {
1190
+ last = messages[i];
1191
+ if (errorOf(messages[i]) == null) lastOk = messages[i];
1192
+ }
1102
1193
  }
1103
- return last;
1194
+ return lastOk ?? last;
1104
1195
  }
1105
1196
  function messageRunState(messages, userMessageId) {
1106
1197
  if (!messages || messages.length === 0) return "unknown";
@@ -1110,13 +1201,27 @@ function messageRunState(messages, userMessageId) {
1110
1201
  if (!reply) return "unknown";
1111
1202
  }
1112
1203
  if (!reply) return "queued";
1113
- if (completedOf(reply) == null) return "running";
1114
- if (finishOf(reply) === "tool-calls") return "running";
1115
- return "done";
1204
+ if (isAssistantInFlight(reply)) return "running";
1205
+ return errorOf(reply) != null ? "failed" : "done";
1116
1206
  }
1117
- function opencodeMessageIdFor(queuedMessageId) {
1118
- const sanitized = queuedMessageId.replace(/[^a-zA-Z0-9]/g, "_");
1119
- return `msg_${sanitized}`;
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
+ );
1120
1225
  }
1121
1226
 
1122
1227
  // src/lib/tunnel/connection.ts
@@ -1187,12 +1292,22 @@ var StreamForwarder = class {
1187
1292
  }
1188
1293
  async handleOpen(frame) {
1189
1294
  const { sid, method, path, headers, has_body } = frame;
1295
+ const correlationId = headers?.[CORRELATION_ID_HEADER];
1296
+ const startedAt = Date.now();
1190
1297
  if (path === TUNNEL_DRAIN_PING_PATH) {
1191
1298
  this.callbacks.onDrainPing?.();
1192
1299
  this.send({ type: "head", sid, status: 204, headers: {} });
1193
1300
  this.send({ type: "res_end", sid });
1194
1301
  return;
1195
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
+ }
1196
1311
  const ac = new AbortController();
1197
1312
  let bodyPromise;
1198
1313
  let pushBody;
@@ -1239,6 +1354,14 @@ var StreamForwarder = class {
1239
1354
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1240
1355
  });
1241
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
+ }
1242
1365
  this.callbacks.onHead?.(sid, upstream.status);
1243
1366
  try {
1244
1367
  if (upstream.body) {
@@ -1508,7 +1631,7 @@ var DEFAULT_RETRY_POLICY = {
1508
1631
  };
1509
1632
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1510
1633
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1511
- var DEFAULT_DISPATCH_CONFIRM_MS = 6e3;
1634
+ var DEFAULT_STUCK_QUEUED_MS = 6e4;
1512
1635
  var ChannelAuthError = class extends Error {
1513
1636
  constructor(message) {
1514
1637
  super(message);
@@ -1543,10 +1666,18 @@ var ChannelDriver = class {
1543
1666
  sleep;
1544
1667
  pausedPollIntervalMs;
1545
1668
  pausedMaxWaitMs;
1546
- dispatchConfirmMs;
1669
+ stuckQueuedMs;
1547
1670
  now;
1548
1671
  /** Cache of conversationId → opencode sessionId. */
1549
1672
  sessions = /* @__PURE__ */ new Map();
1673
+ /**
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();
1550
1681
  /**
1551
1682
  * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1552
1683
  * session: one polling loop services all of that session's in-flight messages.
@@ -1563,6 +1694,54 @@ var ChannelDriver = class {
1563
1694
  * a steady-state-poll re-dispatch will not double-run the message.
1564
1695
  */
1565
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();
1566
1745
  /**
1567
1746
  * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1568
1747
  * first session creation so drain-created sessions are rooted at the project
@@ -1570,8 +1749,33 @@ var ChannelDriver = class {
1570
1749
  * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1571
1750
  */
1572
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();
1573
1762
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1574
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;
1575
1779
  constructor(config2) {
1576
1780
  this.agentId = config2.agentId;
1577
1781
  this.port = config2.port;
@@ -1585,7 +1789,7 @@ var ChannelDriver = class {
1585
1789
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1586
1790
  this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1587
1791
  this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
1588
- this.dispatchConfirmMs = config2.dispatchConfirmMs ?? DEFAULT_DISPATCH_CONFIRM_MS;
1792
+ this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
1589
1793
  this.now = config2.now ?? (() => Date.now());
1590
1794
  }
1591
1795
  /** The IPv4-loopback base URL for the local `opencode serve`. */
@@ -1603,8 +1807,21 @@ var ChannelDriver = class {
1603
1807
  * @returns the number of messages NEWLY dispatched to opencode's native queue.
1604
1808
  */
1605
1809
  async drainPending() {
1810
+ if (this.stopped) return 0;
1606
1811
  if (this.draining) return 0;
1607
1812
  this.draining = true;
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() {
1608
1825
  let dispatched = 0;
1609
1826
  try {
1610
1827
  const conversations = await this.getPendingConversations();
@@ -1616,8 +1833,10 @@ var ChannelDriver = class {
1616
1833
  });
1617
1834
  }
1618
1835
  for (const conv of conversations) {
1836
+ if (this.stopped) break;
1619
1837
  dispatched += await this.processConversation(conv);
1620
1838
  }
1839
+ await this.readoptProcessing();
1621
1840
  } finally {
1622
1841
  this.draining = false;
1623
1842
  }
@@ -1635,6 +1854,51 @@ var ChannelDriver = class {
1635
1854
  }
1636
1855
  return false;
1637
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;
1901
+ }
1638
1902
  /**
1639
1903
  * Await all outstanding per-session watchers (WI-3).
1640
1904
  *
@@ -1669,15 +1933,18 @@ var ChannelDriver = class {
1669
1933
  const sessionId = await this.ensureSession(conv);
1670
1934
  const messages = await this.getPendingMessages(conv.id);
1671
1935
  let dispatched = 0;
1936
+ let skippedAlreadyDispatched = 0;
1672
1937
  for (const message of messages) {
1938
+ if (this.stopped) break;
1673
1939
  if (this.dispatched.has(message.id)) {
1940
+ skippedAlreadyDispatched += 1;
1674
1941
  continue;
1675
1942
  }
1676
- const opencodeMessageId = opencodeMessageIdFor(message.id);
1677
1943
  const options = {
1678
1944
  agent: message.opencode_agent ?? void 0,
1679
1945
  model: message.opencode_model ?? void 0
1680
1946
  };
1947
+ let opencodeMessageId;
1681
1948
  try {
1682
1949
  this.log({
1683
1950
  level: "info",
@@ -1685,7 +1952,10 @@ var ChannelDriver = class {
1685
1952
  conversation_id: conv.id,
1686
1953
  message_id: message.id
1687
1954
  });
1688
- await sendPromptAsync(this.port, sessionId, message.content, options, opencodeMessageId);
1955
+ opencodeMessageId = await this.dispatchLocked(
1956
+ sessionId,
1957
+ () => sendPromptAsync(this.port, sessionId, message.content, options)
1958
+ );
1689
1959
  } catch (err) {
1690
1960
  if (err instanceof ChannelAuthError) throw err;
1691
1961
  this.dispatched.delete(message.id);
@@ -1699,9 +1969,26 @@ var ChannelDriver = class {
1699
1969
  });
1700
1970
  continue;
1701
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;
1980
+ }
1702
1981
  this.dispatched.add(message.id);
1703
1982
  this.registerInFlight(conv, sessionId, message, opencodeMessageId);
1704
1983
  dispatched += 1;
1984
+ void this.postSignal(conv.id, message.id, "dispatched");
1985
+ }
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
+ });
1705
1992
  }
1706
1993
  this.ensureWatcherRunning(sessionId);
1707
1994
  return dispatched;
@@ -1739,6 +2026,25 @@ var ChannelDriver = class {
1739
2026
  // -------------------------------------------------------------------------
1740
2027
  // Per-session watcher (WI-3)
1741
2028
  // -------------------------------------------------------------------------
2029
+ /**
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).
2035
+ */
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
+ }
1742
2048
  /** Register a freshly-dispatched message with its session's watcher state. */
1743
2049
  registerInFlight(conv, sessionId, message, opencodeMessageId) {
1744
2050
  let watcher = this.watchers.get(sessionId);
@@ -1760,7 +2066,54 @@ var ChannelDriver = class {
1760
2066
  dispatchedAt: now,
1761
2067
  deadline: now + this.pausedMaxWaitMs,
1762
2068
  started: false,
1763
- done: false
2069
+ done: false,
2070
+ stuckReported: false
2071
+ });
2072
+ }
2073
+ /**
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).
2081
+ *
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).
2085
+ *
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
1764
2117
  });
1765
2118
  }
1766
2119
  /**
@@ -1826,6 +2179,7 @@ var ChannelDriver = class {
1826
2179
  conversation_id: watcher.conv.id
1827
2180
  });
1828
2181
  for (const evidentMessageId of [...watcher.inFlight.keys()]) {
2182
+ this.readopted.delete(evidentMessageId);
1829
2183
  this.removeInFlight(watcher, evidentMessageId);
1830
2184
  }
1831
2185
  return;
@@ -1846,10 +2200,15 @@ var ChannelDriver = class {
1846
2200
  async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
1847
2201
  const conv = watcher.conv;
1848
2202
  const state = messageRunState(messages, inFlight.opencodeMessageId);
1849
- if ((state === "running" || state === "done") && !inFlight.started) {
2203
+ if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
1850
2204
  let claimed;
1851
2205
  try {
1852
- claimed = await this.markProcessing(conv.id, inFlight.evidentMessageId, sessionId);
2206
+ claimed = await this.markProcessing(
2207
+ conv.id,
2208
+ inFlight.evidentMessageId,
2209
+ sessionId,
2210
+ inFlight.opencodeMessageId
2211
+ );
1853
2212
  } catch (err) {
1854
2213
  if (err instanceof ChannelAuthError) throw err;
1855
2214
  this.log({
@@ -1879,7 +2238,12 @@ var ChannelDriver = class {
1879
2238
  message_id: inFlight.evidentMessageId
1880
2239
  });
1881
2240
  try {
1882
- await this.markDone(conv.id, inFlight.evidentMessageId, sessionId);
2241
+ await this.markDone(
2242
+ conv.id,
2243
+ inFlight.evidentMessageId,
2244
+ sessionId,
2245
+ inFlight.opencodeMessageId
2246
+ );
1883
2247
  } catch (err) {
1884
2248
  if (err instanceof ChannelAuthError) throw err;
1885
2249
  if (err instanceof ChannelTerminalError) {
@@ -1915,10 +2279,59 @@ var ChannelDriver = class {
1915
2279
  this.removeInFlight(watcher, inFlight.evidentMessageId);
1916
2280
  return;
1917
2281
  }
1918
- if (state === "unknown") {
1919
- if (this.now() - inFlight.dispatchedAt >= this.dispatchConfirmMs) {
1920
- await this.redispatchInFlight(sessionId, inFlight);
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;
1921
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
+ });
1922
2335
  }
1923
2336
  if (this.now() >= inFlight.deadline) {
1924
2337
  this.log({
@@ -1927,48 +2340,395 @@ var ChannelDriver = class {
1927
2340
  conversation_id: conv.id,
1928
2341
  message_id: inFlight.evidentMessageId
1929
2342
  });
2343
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2344
+ watched_for_ms: this.now() - inFlight.dispatchedAt
2345
+ });
1930
2346
  this.removeInFlight(watcher, inFlight.evidentMessageId);
1931
2347
  }
1932
2348
  }
2349
+ // -------------------------------------------------------------------------
2350
+ // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
2351
+ // -------------------------------------------------------------------------
1933
2352
  /**
1934
- * Re-dispatch a message whose user row never appeared (idle-path guard). Safe:
1935
- * opencode treats a duplicate caller-supplied `messageID` as idempotent (PoC
1936
- * fact 9) one user message + one reply even if the original DID land. Resets
1937
- * the dispatch timestamp so the guard doesn't immediately fire again.
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.
1938
2365
  */
1939
- async redispatchInFlight(sessionId, inFlight) {
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
+ }
1940
2612
  const options = {
1941
- agent: inFlight.message.opencode_agent ?? void 0,
1942
- model: inFlight.message.opencode_model ?? void 0
2613
+ agent: row.opencode_agent ?? void 0,
2614
+ model: row.opencode_model ?? void 0
1943
2615
  };
1944
2616
  this.log({
1945
2617
  level: "info",
1946
- message: `Message ${inFlight.evidentMessageId.slice(0, 8)} not observed after dispatch \u2014 re-dispatching (idle-path guard)`,
1947
- message_id: inFlight.evidentMessageId
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
1948
2621
  });
2622
+ this.awaitingReadopt.add(row.id);
2623
+ let ocId;
1949
2624
  try {
1950
- await sendPromptAsync(
1951
- this.port,
2625
+ ocId = await this.dispatchLocked(
1952
2626
  sessionId,
1953
- inFlight.message.content,
1954
- options,
1955
- inFlight.opencodeMessageId
2627
+ () => sendPromptAsync(this.port, sessionId, row.content, options)
1956
2628
  );
1957
2629
  } catch (err) {
2630
+ this.awaitingReadopt.delete(row.id);
2631
+ if (err instanceof ChannelAuthError) throw err;
1958
2632
  this.log({
1959
2633
  level: "error",
1960
- message: `Re-dispatch failed for message ${inFlight.evidentMessageId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
1961
- message_id: inFlight.evidentMessageId
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
1962
2637
  });
2638
+ return;
1963
2639
  }
1964
- inFlight.dispatchedAt = this.now();
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
+ };
1965
2707
  }
1966
2708
  /**
1967
2709
  * Remove a message from the in-flight set AND the authoritative dispatched
1968
2710
  * set. Once the in-flight set empties, the watcher loop's `while` guard exits
1969
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).
1970
2720
  */
1971
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
+ }
1972
2732
  watcher.inFlight.delete(evidentMessageId);
1973
2733
  this.dispatched.delete(evidentMessageId);
1974
2734
  }
@@ -1997,8 +2757,8 @@ var ChannelDriver = class {
1997
2757
  } catch {
1998
2758
  }
1999
2759
  for (const q of questions) {
2000
- if (q.sessionID !== sessionId) continue;
2001
2760
  if (watcher.reportedQuestions.has(q.id)) continue;
2761
+ if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
2002
2762
  const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
2003
2763
  const reported = await this.reportInteraction(
2004
2764
  watcher.conv.id,
@@ -2018,8 +2778,8 @@ var ChannelDriver = class {
2018
2778
  } catch {
2019
2779
  }
2020
2780
  for (const p of permissions) {
2021
- if (p.sessionID !== sessionId) continue;
2022
2781
  if (watcher.reportedPermissions.has(p.id)) continue;
2782
+ if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
2023
2783
  const paused = this.attributeInteraction(watcher, p.messageID, messages);
2024
2784
  const reported = await this.reportInteraction(
2025
2785
  watcher.conv.id,
@@ -2030,6 +2790,50 @@ var ChannelDriver = class {
2030
2790
  if (reported) watcher.reportedPermissions.add(p.id);
2031
2791
  }
2032
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
+ }
2033
2837
  /**
2034
2838
  * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
2035
2839
  *
@@ -2112,6 +2916,35 @@ var ChannelDriver = class {
2112
2916
  }
2113
2917
  return await res.json();
2114
2918
  }
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
+ }
2115
2948
  /**
2116
2949
  * EXISTING combinedAuth route — now fired by the watcher on queued→running
2117
2950
  * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
@@ -2133,13 +2966,17 @@ var ChannelDriver = class {
2133
2966
  * A single attempt (no internal retry): the watcher's per-tick loop is the
2134
2967
  * retry vehicle for the swap-to-running.
2135
2968
  */
2136
- async markProcessing(conversationId, messageId, sessionId) {
2969
+ async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
2137
2970
  const res = await this.fetchImpl(
2138
2971
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
2139
2972
  {
2140
2973
  method: "PATCH",
2141
2974
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
2142
- 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
+ })
2143
2980
  }
2144
2981
  );
2145
2982
  this.assertAuth(res, "marking message as processing");
@@ -2177,13 +3014,17 @@ var ChannelDriver = class {
2177
3014
  * watcher retries next tick within the
2178
3015
  * deadline, Finding 4).
2179
3016
  */
2180
- async markDone(conversationId, messageId, sessionId) {
3017
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
2181
3018
  const res = await this.fetchImpl(
2182
3019
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
2183
3020
  {
2184
3021
  method: "PATCH",
2185
3022
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
2186
- body: JSON.stringify({ status: "done", opencode_session_id: sessionId })
3023
+ body: JSON.stringify({
3024
+ status: "done",
3025
+ opencode_session_id: sessionId,
3026
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
3027
+ })
2187
3028
  }
2188
3029
  );
2189
3030
  this.assertAuth(res, "marking message as done");
@@ -2193,7 +3034,17 @@ var ChannelDriver = class {
2193
3034
  }
2194
3035
  throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
2195
3036
  }
2196
- async markFailed(conversationId, messageId) {
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.
3043
+ */
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;
2197
3048
  await this.callWithRetry(
2198
3049
  "marking message as failed",
2199
3050
  () => this.fetchImpl(
@@ -2201,11 +3052,47 @@ var ChannelDriver = class {
2201
3052
  {
2202
3053
  method: "PATCH",
2203
3054
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
2204
- body: JSON.stringify({ status: "failed" })
3055
+ body: JSON.stringify(body)
2205
3056
  }
2206
3057
  )
2207
3058
  );
2208
3059
  }
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`,
3073
+ {
3074
+ method: "POST",
3075
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
3076
+ body: JSON.stringify({ signal, ...extra })
3077
+ }
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
+ }
3095
+ }
2209
3096
  async persistSession(conversationId, sessionId) {
2210
3097
  const res = await this.fetchImpl(
2211
3098
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}`,
@@ -2476,6 +3363,25 @@ async function resolveAgentIdFromKey(authHeader) {
2476
3363
  return { error: `Failed to resolve agent from key: ${message}` };
2477
3364
  }
2478
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
+ }
2479
3385
  async function getAgentInfo(agentId, authHeader) {
2480
3386
  const apiUrl = getApiUrlConfig();
2481
3387
  try {
@@ -2521,7 +3427,9 @@ async function getAgentInfo(agentId, authHeader) {
2521
3427
  // src/commands/run.ts
2522
3428
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
2523
3429
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
2524
- 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) {
2525
3433
  if (state.json) {
2526
3434
  console.log(
2527
3435
  JSON.stringify({
@@ -2546,9 +3454,9 @@ function logActivity(state, entry) {
2546
3454
  }
2547
3455
  if (!state.interactive) {
2548
3456
  if (entry.type === "error") {
2549
- log(state, entry.error ?? "Unknown error", true);
3457
+ log2(state, entry.error ?? "Unknown error", true);
2550
3458
  } else if (entry.type === "info" && entry.message) {
2551
- log(state, entry.message);
3459
+ log2(state, entry.message);
2552
3460
  }
2553
3461
  }
2554
3462
  }
@@ -2634,6 +3542,7 @@ async function handleAuthError(state, error2) {
2634
3542
  }
2635
3543
  async function driveChannels(state, driver) {
2636
3544
  let idlePolls = 0;
3545
+ let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
2637
3546
  while (state.running) {
2638
3547
  if (state.connection?.reconnecting && state.connection.reconnectPromise) {
2639
3548
  logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
@@ -2643,7 +3552,9 @@ async function driveChannels(state, driver) {
2643
3552
  try {
2644
3553
  const processed = await driver.drainPending();
2645
3554
  state.messageCount += processed;
2646
- if (processed > 0 || driver.hasInFlightWatchers()) {
3555
+ const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
3556
+ lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
3557
+ if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
2647
3558
  idlePolls = 0;
2648
3559
  if (processed > 0 && state.interactive) displayStatus(state);
2649
3560
  } else if (state.idleTimeout !== null) {
@@ -2683,8 +3594,42 @@ async function driveChannels(state, driver) {
2683
3594
  }
2684
3595
  }
2685
3596
  }
2686
- 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 = {}) {
2687
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);
2688
3633
  if (state.connection) {
2689
3634
  state.connection.close();
2690
3635
  state.connection = null;
@@ -2695,7 +3640,7 @@ async function cleanup(state) {
2695
3640
  logActivity(state, { type: "info", message: "Stopped OpenCode process" });
2696
3641
  displayStatus(state);
2697
3642
  } else {
2698
- log(state, "Stopped OpenCode process");
3643
+ log2(state, "Stopped OpenCode process");
2699
3644
  }
2700
3645
  state.opencodeProcess = null;
2701
3646
  }
@@ -2715,26 +3660,31 @@ async function run(options) {
2715
3660
  opencodeVersion: null,
2716
3661
  opencodeProcess: null,
2717
3662
  connection: null,
3663
+ channelDriver: null,
2718
3664
  running: true,
3665
+ shuttingDown: false,
2719
3666
  activityLog: [],
2720
3667
  messageCount: 0,
3668
+ lastProxiedActivityAt: null,
2721
3669
  authHeader: ""
2722
3670
  };
2723
3671
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
2724
- log(
3672
+ log2(
2725
3673
  state,
2726
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.",
2727
3675
  false
2728
3676
  );
2729
3677
  }
2730
3678
  const handleSignal = async () => {
3679
+ if (state.shuttingDown) return;
3680
+ state.shuttingDown = true;
2731
3681
  if (state.interactive) {
2732
3682
  logActivity(state, { type: "info", message: "Shutting down..." });
2733
3683
  displayStatus(state);
2734
3684
  } else {
2735
- log(state, "Shutting down...");
3685
+ log2(state, "Shutting down...");
2736
3686
  }
2737
- await cleanup(state);
3687
+ await cleanup(state, { graceful: true });
2738
3688
  await shutdownTelemetry();
2739
3689
  process.exit(0);
2740
3690
  };
@@ -2765,7 +3715,7 @@ async function run(options) {
2765
3715
  const resolved = await resolveAgentIdFromKey(state.authHeader);
2766
3716
  if (resolved.agent_id) {
2767
3717
  state.agentId = resolved.agent_id;
2768
- log(state, `Resolved agent ID from key: ${state.agentId}`);
3718
+ log2(state, `Resolved agent ID from key: ${state.agentId}`);
2769
3719
  if (state.interactive && !state.json) {
2770
3720
  logActivity(state, {
2771
3721
  type: "info",
@@ -2828,17 +3778,17 @@ async function run(options) {
2828
3778
  port: state.port,
2829
3779
  interactive: state.interactive,
2830
3780
  agentId: state.agentId,
2831
- log: (message) => log(state, message)
3781
+ log: (message) => log2(state, message)
2832
3782
  });
2833
3783
  state.port = oc.port;
2834
3784
  state.opencodeProcess = oc.process;
2835
3785
  state.opencodeVersion = oc.version;
2836
3786
  state.opencodeConnected = oc.process !== null || oc.version !== null;
2837
- const version = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
2838
- 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}`);
2839
3789
  const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
2840
3790
  if (versionWarning) {
2841
- log(state, versionWarning, false);
3791
+ log2(state, versionWarning, false);
2842
3792
  if (state.interactive && !state.json) {
2843
3793
  logActivity(state, { type: "info", message: versionWarning });
2844
3794
  }
@@ -2854,12 +3804,14 @@ async function run(options) {
2854
3804
  apiUrl: getApiUrlConfig(),
2855
3805
  getAuthHeader: () => state.authHeader,
2856
3806
  conversationFilter: state.conversationFilter,
3807
+ stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
2857
3808
  log: (entry) => logActivity(state, {
2858
3809
  type: entry.level === "error" ? "error" : "info",
2859
3810
  message: entry.message,
2860
3811
  error: entry.level === "error" ? entry.message : void 0
2861
3812
  })
2862
3813
  });
3814
+ state.channelDriver = channelDriver;
2863
3815
  const connection = new RunnerConnection({
2864
3816
  agentId: state.agentId,
2865
3817
  getAuthHeader: () => state.authHeader,
@@ -2873,7 +3825,11 @@ async function run(options) {
2873
3825
  type: "info",
2874
3826
  message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
2875
3827
  });
2876
- emitAgentConnected(state.agentId, { port: state.port });
3828
+ emitAgentConnected(state.agentId, {
3829
+ port: state.port,
3830
+ cli_version: getCliVersion(),
3831
+ opencode_version: state.opencodeVersion
3832
+ });
2877
3833
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
2878
3834
  if (state.interactive) displayStatus(state);
2879
3835
  channelDriver.drainPending().then((processed) => {
@@ -2907,9 +3863,14 @@ async function run(options) {
2907
3863
  logActivity(state, { type: "error", error: error2 });
2908
3864
  if (state.interactive) displayStatus(state);
2909
3865
  },
2910
- // 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.
2911
3871
  onResponse: () => {
2912
3872
  state.opencodeConnected = true;
3873
+ state.lastProxiedActivityAt = Date.now();
2913
3874
  },
2914
3875
  // A channel message was queued and the api-worker pinged us over the
2915
3876
  // tunnel to drain immediately instead of waiting for the next poll tick.
@@ -2948,9 +3909,10 @@ async function run(options) {
2948
3909
  throw error2;
2949
3910
  }
2950
3911
  if (!interactive || state.json) {
2951
- log(state, "Driving channel messages...");
3912
+ log2(state, "Driving channel messages...");
2952
3913
  }
2953
3914
  await driveChannels(state, channelDriver);
3915
+ if (state.shuttingDown) return;
2954
3916
  await cleanup(state);
2955
3917
  if (state.json) {
2956
3918
  console.log(
@@ -2960,11 +3922,12 @@ async function run(options) {
2960
3922
  })
2961
3923
  );
2962
3924
  } else if (!interactive) {
2963
- log(state, `Completed. Processed ${state.messageCount} message(s).`);
3925
+ log2(state, `Completed. Processed ${state.messageCount} message(s).`);
2964
3926
  }
2965
3927
  await shutdownTelemetry();
2966
3928
  process.exit(0);
2967
3929
  } catch (error2) {
3930
+ if (state.shuttingDown) return;
2968
3931
  await cleanup(state);
2969
3932
  const message = error2 instanceof Error ? error2.message : String(error2);
2970
3933
  if (state.json) {
@@ -2982,8 +3945,9 @@ async function run(options) {
2982
3945
  }
2983
3946
 
2984
3947
  // src/index.ts
3948
+ var { version } = createRequire(import.meta.url)("../package.json");
2985
3949
  var program = new Command();
2986
- 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(
2987
3951
  "--endpoint <url>",
2988
3952
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
2989
3953
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {