@evident-ai/cli 3.0.1-dev.ec76080 → 3.0.1-dev.edba747

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
@@ -285,14 +285,14 @@ function blank() {
285
285
  console.log();
286
286
  }
287
287
  function waitForEnter(prompt = "Press Enter to continue...") {
288
- return new Promise((resolve2) => {
288
+ return new Promise((resolve) => {
289
289
  process.stdout.write(chalk.dim(prompt));
290
290
  const handler = () => {
291
291
  process.stdin.removeListener("data", handler);
292
292
  process.stdin.setRawMode?.(false);
293
293
  process.stdin.pause();
294
294
  console.log();
295
- resolve2();
295
+ resolve();
296
296
  };
297
297
  if (process.stdin.isTTY) {
298
298
  process.stdin.setRawMode?.(true);
@@ -302,7 +302,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
302
302
  });
303
303
  }
304
304
  function sleep(ms) {
305
- return new Promise((resolve2) => setTimeout(resolve2, ms));
305
+ return new Promise((resolve) => setTimeout(resolve, ms));
306
306
  }
307
307
 
308
308
  // src/commands/login.ts
@@ -376,19 +376,19 @@ async function tokenLogin() {
376
376
  console.log("Visit your Evident dashboard to generate a CLI token.");
377
377
  blank();
378
378
  process.stdout.write("Paste token: ");
379
- const token = await new Promise((resolve2) => {
379
+ const token = await new Promise((resolve) => {
380
380
  let data = "";
381
381
  process.stdin.setEncoding("utf8");
382
382
  process.stdin.on("data", (chunk) => {
383
383
  data += chunk;
384
384
  });
385
385
  process.stdin.on("end", () => {
386
- resolve2(data.trim());
386
+ resolve(data.trim());
387
387
  });
388
388
  if (process.stdin.isTTY) {
389
389
  process.stdin.once("data", (chunk) => {
390
390
  process.stdin.pause();
391
- resolve2(chunk.toString().trim());
391
+ resolve(chunk.toString().trim());
392
392
  });
393
393
  process.stdin.resume();
394
394
  }
@@ -471,6 +471,12 @@ import chalk6 from "chalk";
471
471
  import ora3 from "ora";
472
472
  import { select as select3 } from "@inquirer/prompts";
473
473
 
474
+ // ../../packages/types/src/opencode/index.ts
475
+ function opencodeMessageIdFor(queuedMessageId) {
476
+ const sanitized = queuedMessageId.replace(/[^a-zA-Z0-9]/g, "_");
477
+ return `msg_${sanitized}`;
478
+ }
479
+
474
480
  // ../../packages/types/src/telemetry/index.ts
475
481
  var TelemetryEventTypes = {
476
482
  // Agent activity events (shown in web UI activity log)
@@ -706,13 +712,13 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
706
712
  if (health.healthy) {
707
713
  return health;
708
714
  }
709
- await new Promise((resolve2) => setTimeout(resolve2, 1e3));
715
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
710
716
  }
711
717
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
712
718
  }
713
719
 
714
720
  // src/lib/opencode/opencode-version-gate.ts
715
- var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
721
+ var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11"];
716
722
  function isQueueValidatedVersion(version2) {
717
723
  if (!version2) return false;
718
724
  return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
@@ -1068,58 +1074,6 @@ function isAssistantInFlight(m) {
1068
1074
  if (completedOf(m) == null) return true;
1069
1075
  return finishOf(m) === "tool-calls";
1070
1076
  }
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
- }
1081
- function sessionLastActivityMs(session) {
1082
- const candidates = [
1083
- session.time?.updated,
1084
- session.time?.created,
1085
- session.time_updated,
1086
- session.time_created,
1087
- session.updated,
1088
- session.created
1089
- ];
1090
- for (const c of candidates) {
1091
- if (typeof c === "number" && Number.isFinite(c)) return c;
1092
- }
1093
- return null;
1094
- }
1095
- async function listSessions(port) {
1096
- try {
1097
- const res = await fetch(`${opencodeBase(port)}/session`);
1098
- if (!res.ok) return null;
1099
- const body = await res.json();
1100
- return Array.isArray(body) ? body : null;
1101
- } catch {
1102
- return null;
1103
- }
1104
- }
1105
- async function deleteSession(port, id) {
1106
- try {
1107
- const res = await fetch(`${opencodeBase(port)}/session/${id}`, { method: "DELETE" });
1108
- return res.status >= 200 && res.status < 300;
1109
- } catch {
1110
- return false;
1111
- }
1112
- }
1113
- async function sessionExists(port, id) {
1114
- try {
1115
- const res = await fetch(`${opencodeBase(port)}/session/${id}`);
1116
- if (res.status >= 200 && res.status < 300) return true;
1117
- if (res.status === 404) return false;
1118
- return null;
1119
- } catch {
1120
- return null;
1121
- }
1122
- }
1123
1077
  async function createOpenCodeSession(port, directory) {
1124
1078
  const url = new URL(`${opencodeBase(port)}/session`);
1125
1079
  if (directory && directory.trim()) {
@@ -1137,16 +1091,9 @@ async function createOpenCodeSession(port, directory) {
1137
1091
  const data = await response.json();
1138
1092
  return data.id;
1139
1093
  }
1140
- function messageText(m) {
1141
- if (!m || !Array.isArray(m.parts)) return "";
1142
- return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
1143
- }
1144
- async function sendPromptAsync(port, sessionId, content, options) {
1145
- const before = await getSessionMessages(port, sessionId);
1146
- const knownUserIds = new Set(
1147
- (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
1148
- );
1094
+ async function sendPromptAsync(port, sessionId, content, options, messageId) {
1149
1095
  const body = {
1096
+ messageID: messageId,
1150
1097
  parts: [{ type: "text", text: content }]
1151
1098
  };
1152
1099
  if (options?.agent) {
@@ -1170,29 +1117,6 @@ async function sendPromptAsync(port, sessionId, content, options) {
1170
1117
  const text = await res.text().catch(() => "");
1171
1118
  throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1172
1119
  }
1173
- const READ_BACK_ATTEMPTS = 5;
1174
- const READ_BACK_DELAY_MS = 150;
1175
- for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
1176
- const after = await getSessionMessages(port, sessionId);
1177
- if (after) {
1178
- let best = null;
1179
- for (const m of after) {
1180
- if (roleOf(m) !== "user") continue;
1181
- const id = idOf(m);
1182
- if (typeof id !== "string" || knownUserIds.has(id)) continue;
1183
- if (messageText(m) !== content) continue;
1184
- const created = createdOf(m) ?? 0;
1185
- if (best === null || created > best.created) {
1186
- best = { id, created };
1187
- }
1188
- }
1189
- if (best) return best.id;
1190
- }
1191
- if (attempt < READ_BACK_ATTEMPTS - 1) {
1192
- await new Promise((resolve2) => setTimeout(resolve2, READ_BACK_DELAY_MS));
1193
- }
1194
- }
1195
- return null;
1196
1120
  }
1197
1121
  function findAssistantReplyAfter(messages, userMessageId) {
1198
1122
  if (!messages || messages.length === 0) return null;
@@ -1265,109 +1189,30 @@ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
1265
1189
  (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
1266
1190
  );
1267
1191
  }
1268
-
1269
- // src/lib/opencode/session-cleanup.ts
1270
- var DURATION_UNIT_MS = {
1271
- s: 1e3,
1272
- m: 60 * 1e3,
1273
- h: 60 * 60 * 1e3,
1274
- d: 24 * 60 * 60 * 1e3
1275
- };
1276
- function parseDurationMs(input) {
1277
- const trimmed = input.trim();
1278
- const match = /^(\d+)([smhd])$/.exec(trimmed);
1279
- if (!match) {
1280
- throw new Error(
1281
- `Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
1282
- );
1283
- }
1284
- const value = Number(match[1]);
1285
- if (value <= 0) {
1286
- throw new Error(`Invalid duration "${input}": must be a positive value.`);
1287
- }
1288
- return value * DURATION_UNIT_MS[match[2]];
1289
- }
1290
- function selectSessionsToDelete(sessions, opts) {
1291
- const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
1292
- if (maxAgeMs === void 0 && maxCount === void 0) return [];
1293
- const ageEligible = (s) => {
1294
- if (maxAgeMs === void 0) return false;
1295
- if (s.lastActivityMs === null) return true;
1296
- return nowMs - s.lastActivityMs > maxAgeMs;
1297
- };
1298
- const countEligibleIds = /* @__PURE__ */ new Set();
1299
- if (maxCount !== void 0) {
1300
- const byActivityDesc = [...sessions].sort(
1301
- (a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
1302
- );
1303
- for (const s of byActivityDesc.slice(maxCount)) {
1304
- countEligibleIds.add(s.id);
1305
- }
1306
- }
1307
- const toDelete = [];
1308
- for (const s of sessions) {
1309
- if (protectedIds.has(s.id)) continue;
1310
- if (ageEligible(s) || countEligibleIds.has(s.id)) {
1311
- toDelete.push(s.id);
1192
+ function isSessionSettled(messages, now, settleMs) {
1193
+ if (!messages || messages.length === 0) return true;
1194
+ let newestAssistantCreated = null;
1195
+ for (const m of messages) {
1196
+ if (roleOf(m) !== "assistant") continue;
1197
+ if (isAssistantInFlight(m)) return false;
1198
+ const created = createdOf(m);
1199
+ if (typeof created === "number" && (newestAssistantCreated === null || created > newestAssistantCreated)) {
1200
+ newestAssistantCreated = created;
1312
1201
  }
1313
1202
  }
1314
- return toDelete;
1203
+ if (newestAssistantCreated === null) return true;
1204
+ return now - newestAssistantCreated >= settleMs;
1315
1205
  }
1316
- var DEFAULT_INTERVAL = "1h";
1317
- function resolve(flag, envValue, fallback) {
1318
- return flag ?? envValue ?? fallback;
1206
+ function opencodeMessageIdFor2(queuedMessageId) {
1207
+ return opencodeMessageIdFor(queuedMessageId);
1319
1208
  }
1320
- function parseMaxCount(input) {
1321
- const trimmed = input.trim();
1322
- if (!/^\d+$/.test(trimmed)) {
1323
- throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
1209
+ var NATIVE_ID_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1210
+ function nativeOpencodeMessageId() {
1211
+ let id = "msg_";
1212
+ for (let i = 0; i < 24; i++) {
1213
+ id += NATIVE_ID_ALPHABET[Math.floor(Math.random() * NATIVE_ID_ALPHABET.length)];
1324
1214
  }
1325
- const value = Number(trimmed);
1326
- if (value <= 0) {
1327
- throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
1328
- }
1329
- return value;
1330
- }
1331
- function resolveSessionCleanupConfig(flags, env = process.env) {
1332
- const warnings = [];
1333
- const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
1334
- const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
1335
- const intervalRaw = resolve(
1336
- flags.interval,
1337
- env.EVIDENT_SESSION_CLEANUP_INTERVAL,
1338
- DEFAULT_INTERVAL
1339
- );
1340
- let maxAgeMs;
1341
- if (maxAgeRaw !== void 0) {
1342
- try {
1343
- maxAgeMs = parseDurationMs(maxAgeRaw);
1344
- } catch (err) {
1345
- warnings.push(
1346
- `Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
1347
- );
1348
- }
1349
- }
1350
- let maxCount;
1351
- if (maxCountRaw !== void 0) {
1352
- try {
1353
- maxCount = parseMaxCount(maxCountRaw);
1354
- } catch (err) {
1355
- warnings.push(
1356
- `Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
1357
- );
1358
- }
1359
- }
1360
- let intervalMs;
1361
- try {
1362
- intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
1363
- } catch (err) {
1364
- warnings.push(
1365
- `Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
1366
- );
1367
- intervalMs = parseDurationMs(DEFAULT_INTERVAL);
1368
- }
1369
- const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
1370
- return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
1215
+ return id;
1371
1216
  }
1372
1217
 
1373
1218
  // src/lib/tunnel/connection.ts
@@ -1446,26 +1291,24 @@ var StreamForwarder = class {
1446
1291
  this.send({ type: "res_end", sid });
1447
1292
  return;
1448
1293
  }
1449
- if (process.env.DEBUG) {
1450
- log("debug", "agent_request", {
1451
- correlation_id: correlationId,
1452
- sid,
1453
- method,
1454
- path: stripQuery(path)
1455
- });
1456
- }
1294
+ log("info", "agent_request", {
1295
+ correlation_id: correlationId,
1296
+ sid,
1297
+ method,
1298
+ path: stripQuery(path)
1299
+ });
1457
1300
  const ac = new AbortController();
1458
1301
  let bodyPromise;
1459
1302
  let pushBody;
1460
1303
  let endBody;
1461
1304
  if (has_body) {
1462
1305
  const chunks = [];
1463
- bodyPromise = new Promise((resolve2) => {
1306
+ bodyPromise = new Promise((resolve) => {
1464
1307
  pushBody = (buf) => {
1465
1308
  chunks.push(buf);
1466
1309
  };
1467
1310
  endBody = () => {
1468
- resolve2(Buffer.concat(chunks));
1311
+ resolve(Buffer.concat(chunks));
1469
1312
  };
1470
1313
  });
1471
1314
  }
@@ -1500,14 +1343,12 @@ var StreamForwarder = class {
1500
1343
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1501
1344
  });
1502
1345
  this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
1503
- if (process.env.DEBUG) {
1504
- log("debug", "agent_response", {
1505
- correlation_id: correlationId,
1506
- sid,
1507
- status: upstream.status,
1508
- duration_ms: Date.now() - startedAt
1509
- });
1510
- }
1346
+ log("info", "agent_response", {
1347
+ correlation_id: correlationId,
1348
+ sid,
1349
+ status: upstream.status,
1350
+ duration_ms: Date.now() - startedAt
1351
+ });
1511
1352
  this.callbacks.onHead?.(sid, upstream.status);
1512
1353
  try {
1513
1354
  if (upstream.body) {
@@ -1583,7 +1424,7 @@ function connectTunnel(options) {
1583
1424
  } = options;
1584
1425
  const tunnelUrl = getTunnelUrlConfig();
1585
1426
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
1586
- return new Promise((resolve2, reject) => {
1427
+ return new Promise((resolve, reject) => {
1587
1428
  const ws = new WebSocket2(url, {
1588
1429
  headers: {
1589
1430
  Authorization: authHeader
@@ -1648,7 +1489,7 @@ function connectTunnel(options) {
1648
1489
  clearTimeout(connectionTimeout);
1649
1490
  const connectedAgentId = message.agent_id ?? agentId;
1650
1491
  onConnected?.(connectedAgentId);
1651
- resolve2({
1492
+ resolve({
1652
1493
  ws,
1653
1494
  close: () => ws.close(1e3, "CLI shutdown")
1654
1495
  });
@@ -1777,7 +1618,10 @@ var DEFAULT_RETRY_POLICY = {
1777
1618
  };
1778
1619
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1779
1620
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1621
+ var DEFAULT_DISPATCH_CONFIRM_MS = 6e3;
1780
1622
  var DEFAULT_STUCK_QUEUED_MS = 6e4;
1623
+ var DEFAULT_STUCK_QUEUED_REDRIVE_MAX = 3;
1624
+ var DEFAULT_SETTLE_MS = 3500;
1781
1625
  var ChannelAuthError = class extends Error {
1782
1626
  constructor(message) {
1783
1627
  super(message);
@@ -1812,18 +1656,13 @@ var ChannelDriver = class {
1812
1656
  sleep;
1813
1657
  pausedPollIntervalMs;
1814
1658
  pausedMaxWaitMs;
1659
+ dispatchConfirmMs;
1815
1660
  stuckQueuedMs;
1661
+ stuckQueuedRedriveMax;
1662
+ settleMs;
1816
1663
  now;
1817
1664
  /** Cache of conversationId → opencode sessionId. */
1818
1665
  sessions = /* @__PURE__ */ new Map();
1819
- /**
1820
- * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
1821
- * longer idempotent (no caller-supplied `messageID`), and its read-back picks
1822
- * "the one new user row" — which is only unambiguous if no OTHER dispatch into
1823
- * the SAME session interleaves its snapshot→POST→read-back. This map chains each
1824
- * session's dispatches so they run serially; distinct sessions stay concurrent.
1825
- */
1826
- sessionDispatchLocks = /* @__PURE__ */ new Map();
1827
1666
  /**
1828
1667
  * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1829
1668
  * session: one polling loop services all of that session's in-flight messages.
@@ -1874,20 +1713,6 @@ var ChannelDriver = class {
1874
1713
  * the row leaves the processing list, exactly like `dontRedispatch`.
1875
1714
  */
1876
1715
  doneUndeliverable = /* @__PURE__ */ new Set();
1877
- /**
1878
- * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
1879
- * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
1880
- * is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
1881
- * persist hasn't landed before tick N+1 re-reads the still-null
1882
- * `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
1883
- * A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
1884
- * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
1885
- * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
1886
- * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
1887
- * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
1888
- * so the NEXT tick may retry exactly once more).
1889
- */
1890
- awaitingReadopt = /* @__PURE__ */ new Set();
1891
1716
  /**
1892
1717
  * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1893
1718
  * first session creation so drain-created sessions are rooted at the project
@@ -1895,33 +1720,8 @@ var ChannelDriver = class {
1895
1720
  * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1896
1721
  */
1897
1722
  opencodeDirectory = void 0;
1898
- /**
1899
- * Cache of opencode `sessionId → parentID` (its parent session, or `null` when
1900
- * the session is a root with no parent). Sub-agents spawned via the `task` tool
1901
- * run in CHILD sessions whose `parentID` chains up to the Evident-created
1902
- * (watched) session; we resolve this once per session so a child-session
1903
- * question/permission can be attributed to the watched session's subtree
1904
- * (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
1905
- * entry = not yet resolved; `null` = resolved root (stop walking).
1906
- */
1907
- sessionParents = /* @__PURE__ */ new Map();
1908
1723
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1909
1724
  draining = false;
1910
- /**
1911
- * The currently-executing `drainPending()` promise, or null when idle. Lets a
1912
- * graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
1913
- * is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
1914
- * drain that entered before `stop()` still registers its watcher).
1915
- */
1916
- activeDrain = null;
1917
- /**
1918
- * Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
1919
- * dispatches NEW work (it returns 0 immediately) — but the per-session watcher
1920
- * loops already running keep going so in-flight turns can finish and deliver
1921
- * their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
1922
- * and stops opencode.
1923
- */
1924
- stopped = false;
1925
1725
  constructor(config2) {
1926
1726
  this.agentId = config2.agentId;
1927
1727
  this.port = config2.port;
@@ -1935,7 +1735,10 @@ var ChannelDriver = class {
1935
1735
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1936
1736
  this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1937
1737
  this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
1738
+ this.dispatchConfirmMs = config2.dispatchConfirmMs ?? DEFAULT_DISPATCH_CONFIRM_MS;
1938
1739
  this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
1740
+ this.stuckQueuedRedriveMax = config2.stuckQueuedRedriveMax ?? DEFAULT_STUCK_QUEUED_REDRIVE_MAX;
1741
+ this.settleMs = config2.settleMs ?? DEFAULT_SETTLE_MS;
1939
1742
  this.now = config2.now ?? (() => Date.now());
1940
1743
  }
1941
1744
  /** The IPv4-loopback base URL for the local `opencode serve`. */
@@ -1953,21 +1756,8 @@ var ChannelDriver = class {
1953
1756
  * @returns the number of messages NEWLY dispatched to opencode's native queue.
1954
1757
  */
1955
1758
  async drainPending() {
1956
- if (this.stopped) return 0;
1957
1759
  if (this.draining) return 0;
1958
1760
  this.draining = true;
1959
- const run2 = this.runDrain();
1960
- this.activeDrain = run2.then(
1961
- () => {
1962
- this.activeDrain = null;
1963
- },
1964
- () => {
1965
- this.activeDrain = null;
1966
- }
1967
- );
1968
- return run2;
1969
- }
1970
- async runDrain() {
1971
1761
  let dispatched = 0;
1972
1762
  try {
1973
1763
  const conversations = await this.getPendingConversations();
@@ -1979,7 +1769,6 @@ var ChannelDriver = class {
1979
1769
  });
1980
1770
  }
1981
1771
  for (const conv of conversations) {
1982
- if (this.stopped) break;
1983
1772
  dispatched += await this.processConversation(conv);
1984
1773
  }
1985
1774
  await this.readoptProcessing();
@@ -2000,73 +1789,6 @@ var ChannelDriver = class {
2000
1789
  }
2001
1790
  return false;
2002
1791
  }
2003
- /**
2004
- * OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
2005
- * exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
2006
- * `watchers` entry whose `inFlight` set is non-empty — the same predicate
2007
- * `hasInFlightWatchers()` uses, lifted to return the ids.
2008
- *
2009
- * Deliberately does NOT include `this.sessions` (the permanent, never-pruned
2010
- * conversation→session cache). Protecting every bound-but-idle session there
2011
- * would shield nearly every session and defeat cleanup — AND it is unnecessary:
2012
- * `ensureSession` is self-healing (it recreates a session whose id no longer
2013
- * exists), so deleting an idle bound session is harmless — the conversation's
2014
- * next turn transparently rebinds a fresh one. The only thing worth protecting
2015
- * is a session with a turn ACTIVELY in flight right now: tearing that down
2016
- * mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
2017
- */
2018
- protectedSessionIds() {
2019
- const ids = /* @__PURE__ */ new Set();
2020
- for (const [sessionId, watcher] of this.watchers) {
2021
- if (watcher.inFlight.size > 0) ids.add(sessionId);
2022
- }
2023
- return ids;
2024
- }
2025
- /**
2026
- * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
2027
- * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
2028
- * — but the watcher loops already tracking in-flight turns keep running, so a
2029
- * turn that has finished (or is about to) still fires `markDone` and delivers
2030
- * its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
2031
- */
2032
- stop() {
2033
- this.stopped = true;
2034
- }
2035
- /**
2036
- * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
2037
- * graceful shutdown, so a turn whose reply is ready — or completes within the
2038
- * window — is delivered before the process exits, instead of being cut off and
2039
- * left for the ADR-0046 restart-recovery path.
2040
- *
2041
- * Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
2042
- * far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
2043
- * window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
2044
- * set empties OR the timeout elapses. Anything still in flight at the timeout is
2045
- * safe to abandon — it stays `processing` server-side and is re-adopted on the
2046
- * next runner start (ADR-0046).
2047
- *
2048
- * @returns true if all in-flight work settled within the window; false if the
2049
- * timeout elapsed with work still in flight.
2050
- */
2051
- async waitForInFlight(timeoutMs) {
2052
- const deadline = this.now() + timeoutMs;
2053
- const step = Math.min(this.pausedPollIntervalMs, 250);
2054
- if (this.activeDrain) {
2055
- let drainSettled = false;
2056
- void this.activeDrain.then(() => {
2057
- drainSettled = true;
2058
- });
2059
- while (!drainSettled) {
2060
- if (this.now() >= deadline) return false;
2061
- await this.sleep(step);
2062
- }
2063
- }
2064
- while (this.hasInFlightWatchers()) {
2065
- if (this.now() >= deadline) return false;
2066
- await this.sleep(step);
2067
- }
2068
- return true;
2069
- }
2070
1792
  /**
2071
1793
  * Await all outstanding per-session watchers (WI-3).
2072
1794
  *
@@ -2103,16 +1825,15 @@ var ChannelDriver = class {
2103
1825
  let dispatched = 0;
2104
1826
  let skippedAlreadyDispatched = 0;
2105
1827
  for (const message of messages) {
2106
- if (this.stopped) break;
2107
1828
  if (this.dispatched.has(message.id)) {
2108
1829
  skippedAlreadyDispatched += 1;
2109
1830
  continue;
2110
1831
  }
1832
+ const opencodeMessageId = opencodeMessageIdFor2(message.id);
2111
1833
  const options = {
2112
1834
  agent: message.opencode_agent ?? void 0,
2113
1835
  model: message.opencode_model ?? void 0
2114
1836
  };
2115
- let opencodeMessageId;
2116
1837
  try {
2117
1838
  this.log({
2118
1839
  level: "info",
@@ -2120,23 +1841,10 @@ var ChannelDriver = class {
2120
1841
  conversation_id: conv.id,
2121
1842
  message_id: message.id
2122
1843
  });
2123
- opencodeMessageId = await this.dispatchLocked(
2124
- sessionId,
2125
- () => sendPromptAsync(this.port, sessionId, message.content, options)
2126
- );
1844
+ await sendPromptAsync(this.port, sessionId, message.content, options, opencodeMessageId);
2127
1845
  } catch (err) {
2128
1846
  if (err instanceof ChannelAuthError) throw err;
2129
1847
  this.dispatched.delete(message.id);
2130
- if (await sessionExists(this.port, sessionId) === false) {
2131
- this.sessions.delete(conv.id);
2132
- this.log({
2133
- level: "info",
2134
- message: `Message ${message.id.slice(0, 8)} dispatch hit a session (${sessionId.slice(0, 8)}) that was deleted mid-dispatch (cleanup race) \u2014 deferring this and later messages for conversation ${conv.id.slice(0, 8)} to the next tick (recreated then, in order). Already-dispatched turns keep their watcher.`,
2135
- conversation_id: conv.id,
2136
- message_id: message.id
2137
- });
2138
- break;
2139
- }
2140
1848
  await this.markFailed(conv.id, message.id).catch(() => {
2141
1849
  });
2142
1850
  this.log({
@@ -2147,15 +1855,6 @@ var ChannelDriver = class {
2147
1855
  });
2148
1856
  continue;
2149
1857
  }
2150
- if (opencodeMessageId === null) {
2151
- this.log({
2152
- level: "error",
2153
- 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`,
2154
- conversation_id: conv.id,
2155
- message_id: message.id
2156
- });
2157
- continue;
2158
- }
2159
1858
  this.dispatched.add(message.id);
2160
1859
  this.registerInFlight(conv, sessionId, message, opencodeMessageId);
2161
1860
  dispatched += 1;
@@ -2172,33 +1871,16 @@ var ChannelDriver = class {
2172
1871
  return dispatched;
2173
1872
  }
2174
1873
  async ensureSession(conv) {
2175
- const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
2176
- if (bound) {
2177
- const exists = await sessionExists(this.port, bound);
2178
- if (exists === false) {
2179
- this.log({
2180
- level: "info",
2181
- message: `OpenCode session ${bound} for conversation ${conv.id.slice(0, 8)} no longer exists (deleted or DB reset) \u2014 creating a fresh session and rebinding.`,
2182
- conversation_id: conv.id
2183
- });
2184
- this.sessions.delete(conv.id);
2185
- return this.createAndBindSession(conv.id);
2186
- }
2187
- this.sessions.set(conv.id, bound);
2188
- return bound;
1874
+ const cached = this.sessions.get(conv.id);
1875
+ if (cached) return cached;
1876
+ if (conv.opencode_session_id) {
1877
+ this.sessions.set(conv.id, conv.opencode_session_id);
1878
+ return conv.opencode_session_id;
2189
1879
  }
2190
- return this.createAndBindSession(conv.id);
2191
- }
2192
- /**
2193
- * Create a fresh OpenCode session for a conversation, cache the binding, and
2194
- * best-effort persist it server-side. Shared by the first-ever bind and the
2195
- * self-heal recreate path in `ensureSession`.
2196
- */
2197
- async createAndBindSession(conversationId) {
2198
1880
  const directory = await this.resolveOpenCodeDirectory();
2199
1881
  const sessionId = await createOpenCodeSession(this.port, directory);
2200
- this.sessions.set(conversationId, sessionId);
2201
- await this.persistSession(conversationId, sessionId).catch(() => {
1882
+ this.sessions.set(conv.id, sessionId);
1883
+ await this.persistSession(conv.id, sessionId).catch(() => {
2202
1884
  });
2203
1885
  return sessionId;
2204
1886
  }
@@ -2221,25 +1903,6 @@ var ChannelDriver = class {
2221
1903
  // -------------------------------------------------------------------------
2222
1904
  // Per-session watcher (WI-3)
2223
1905
  // -------------------------------------------------------------------------
2224
- /**
2225
- * Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
2226
- * opencode session (Task 2.1a), so two dispatches into the SAME session can
2227
- * never interleave and mis-correlate their read-backs. Distinct sessions run
2228
- * concurrently. The chained tail intentionally ignores the prior result/error
2229
- * (each dispatch reports its own outcome to its caller).
2230
- */
2231
- dispatchLocked(sessionId, fn) {
2232
- const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
2233
- const run2 = prior.then(fn, fn);
2234
- this.sessionDispatchLocks.set(
2235
- sessionId,
2236
- run2.then(
2237
- () => void 0,
2238
- () => void 0
2239
- )
2240
- );
2241
- return run2;
2242
- }
2243
1906
  /** Register a freshly-dispatched message with its session's watcher state. */
2244
1907
  registerInFlight(conv, sessionId, message, opencodeMessageId) {
2245
1908
  let watcher = this.watchers.get(sessionId);
@@ -2262,7 +1925,11 @@ var ChannelDriver = class {
2262
1925
  deadline: now + this.pausedMaxWaitMs,
2263
1926
  started: false,
2264
1927
  done: false,
2265
- stuckReported: false
1928
+ stuckReported: false,
1929
+ redriveAttempts: 0,
1930
+ lastRedriveAt: null,
1931
+ redriveOpencodeMessageId: null,
1932
+ attemptedOpencodeMessageIds: [opencodeMessageId]
2266
1933
  });
2267
1934
  }
2268
1935
  /**
@@ -2282,7 +1949,7 @@ var ChannelDriver = class {
2282
1949
  * server already flipped to `processing`; the running/done transitions still
2283
1950
  * fire from the watcher's normal branches.
2284
1951
  */
2285
- registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
1952
+ registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs, redriveOpencodeMessageId = null) {
2286
1953
  let watcher = this.watchers.get(sessionId);
2287
1954
  if (!watcher) {
2288
1955
  watcher = {
@@ -2304,11 +1971,17 @@ var ChannelDriver = class {
2304
1971
  started: true,
2305
1972
  done: false,
2306
1973
  // Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
2307
- // AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
2308
- // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2309
- // re-adopted row left wedged in `queued` still emits the signal once
2310
- // (#210/#220 observability).
2311
- stuckReported: false
1974
+ // AND the stuck-queued observer now INCLUDES re-adopted queued wedges: it
1975
+ // gates on `state === 'queued'` (turn produced no reply), not on `started`,
1976
+ // so a re-adopted row left wedged in `queued` still emits the signal once
1977
+ // (queued-followup-redrive, #210). A re-adopted `queued` wedge is ALSO
1978
+ // re-driven by the same path (ADR-0046 §c deferred the live-session redrive
1979
+ // to here) — `redriveAttempts` starts fresh so it gets the full budget.
1980
+ stuckReported: false,
1981
+ redriveAttempts: 0,
1982
+ lastRedriveAt: null,
1983
+ redriveOpencodeMessageId,
1984
+ attemptedOpencodeMessageIds: [opencodeMessageId]
2312
1985
  });
2313
1986
  }
2314
1987
  /**
@@ -2402,7 +2075,7 @@ var ChannelDriver = class {
2402
2075
  conv.id,
2403
2076
  inFlight.evidentMessageId,
2404
2077
  sessionId,
2405
- inFlight.opencodeMessageId
2078
+ inFlight.redriveOpencodeMessageId
2406
2079
  );
2407
2080
  } catch (err) {
2408
2081
  if (err instanceof ChannelAuthError) throw err;
@@ -2437,7 +2110,7 @@ var ChannelDriver = class {
2437
2110
  conv.id,
2438
2111
  inFlight.evidentMessageId,
2439
2112
  sessionId,
2440
- inFlight.opencodeMessageId
2113
+ inFlight.redriveOpencodeMessageId
2441
2114
  );
2442
2115
  } catch (err) {
2443
2116
  if (err instanceof ChannelAuthError) throw err;
@@ -2520,13 +2193,25 @@ var ChannelDriver = class {
2520
2193
  this.removeInFlight(watcher, inFlight.evidentMessageId);
2521
2194
  return;
2522
2195
  }
2196
+ if (state === "unknown") {
2197
+ if (this.now() - inFlight.dispatchedAt >= this.dispatchConfirmMs) {
2198
+ await this.redispatchInFlight(conv.id, sessionId, inFlight);
2199
+ }
2200
+ }
2523
2201
  const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
2524
2202
  const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
2525
- if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
2526
- inFlight.stuckReported = true;
2527
- void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2528
- stuck_for_ms: this.now() - inFlight.dispatchedAt
2529
- });
2203
+ if (state === "queued" && pastStuckBound && sessionIdle) {
2204
+ if (!inFlight.stuckReported) {
2205
+ inFlight.stuckReported = true;
2206
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2207
+ stuck_for_ms: this.now() - inFlight.dispatchedAt
2208
+ });
2209
+ }
2210
+ const dueForRedrive = inFlight.lastRedriveAt == null || this.now() - inFlight.lastRedriveAt >= this.stuckQueuedMs;
2211
+ if (dueForRedrive && isSessionSettled(messages, this.now(), this.settleMs)) {
2212
+ const removed = await this.redriveStuckQueued(sessionId, watcher, inFlight, messages);
2213
+ if (removed) return;
2214
+ }
2530
2215
  }
2531
2216
  if (this.now() >= inFlight.deadline) {
2532
2217
  this.log({
@@ -2541,6 +2226,183 @@ var ChannelDriver = class {
2541
2226
  this.removeInFlight(watcher, inFlight.evidentMessageId);
2542
2227
  }
2543
2228
  }
2229
+ /**
2230
+ * Re-dispatch a message whose user row never appeared (idle-path guard). Safe:
2231
+ * opencode treats a duplicate caller-supplied `messageID` as idempotent (PoC
2232
+ * fact 9) — one user message + one reply even if the original DID land. Resets
2233
+ * the dispatch timestamp so the guard doesn't immediately fire again.
2234
+ */
2235
+ async redispatchInFlight(conversationId, sessionId, inFlight) {
2236
+ const options = {
2237
+ agent: inFlight.message.opencode_agent ?? void 0,
2238
+ model: inFlight.message.opencode_model ?? void 0
2239
+ };
2240
+ this.log({
2241
+ level: "info",
2242
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} not observed after dispatch \u2014 re-dispatching (idle-path guard)`,
2243
+ message_id: inFlight.evidentMessageId
2244
+ });
2245
+ void this.postSignal(conversationId, inFlight.evidentMessageId, "redispatched");
2246
+ try {
2247
+ await sendPromptAsync(
2248
+ this.port,
2249
+ sessionId,
2250
+ inFlight.message.content,
2251
+ options,
2252
+ inFlight.opencodeMessageId
2253
+ );
2254
+ } catch (err) {
2255
+ this.log({
2256
+ level: "error",
2257
+ message: `Re-dispatch failed for message ${inFlight.evidentMessageId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
2258
+ message_id: inFlight.evidentMessageId
2259
+ });
2260
+ }
2261
+ inFlight.dispatchedAt = this.now();
2262
+ }
2263
+ /**
2264
+ * RE-DRIVE a stuck-`queued` follow-up so opencode actually runs it
2265
+ * (queued-followup-redrive; the fix for the 2026-07-18 dev incident).
2266
+ *
2267
+ * The wedge: a follow-up `prompt_async`'d mid-turn / in the post-turn settling
2268
+ * window is orphaned — its user message persists but the turn never runs
2269
+ * (`messageRunState === 'queued'`). Proven against real opencode 1.18.3 by an
2270
+ * isolated, interleaved experiment (`.harness/followup-send-mechanism-finding.md`):
2271
+ * the un-sticking variable is the `messageID` FORMAT. A follow-up carrying the
2272
+ * runner's CUSTOM underscore id (`msg_<sanitized-uuid>`) is NEVER picked up mid-
2273
+ * turn (0/24 trials); the SAME follow-up carrying a NATIVE-format id
2274
+ * (`msg_`+24 base62) runs once the prior turn settles (~50% locally, reliably on
2275
+ * dev). `parts[].id` and `agent`/`model` were proven IRRELEVANT.
2276
+ *
2277
+ * So we re-`prompt_async` into the SAME opencode session (opencode-web does
2278
+ * exactly this and it works — UPDATE 2 in the investigation doc), preserving the
2279
+ * conversation's history/continuity: we do NOT create a fresh session and we do
2280
+ * NOT overwrite the conversation's `opencode_session_id`. Only the opencode
2281
+ * user-message id changes — to a fresh native id per attempt (opencode's
2282
+ * caller-supplied id dedup is global+permanent, so each attempt needs a never-
2283
+ * seen id).
2284
+ *
2285
+ * Reply correlation is PRESERVED: the native id is random and NOT re-derivable
2286
+ * from the row id, so the runner carries it to the server (on markProcessing/
2287
+ * markDone via `redriveOpencodeMessageId`), which persists it on the row and
2288
+ * correlates the reply by THAT id (`conversation-notification.ts`). A normal,
2289
+ * never-re-driven message still correlates by the derived stable id — unchanged.
2290
+ *
2291
+ * Bounded to `stuckQueuedRedriveMax` attempts. On exhaustion the row is marked
2292
+ * FAILED (existing channel failure affordance) so the user is TOLD it could not
2293
+ * be answered rather than left silent, and it is removed from the in-flight set.
2294
+ *
2295
+ * @returns true if the message LEFT this watcher's in-flight set (gave up +
2296
+ * marked failed) — the caller then stops servicing it this tick. Returns false
2297
+ * when the follow-up stays in THIS watcher (re-driven in place, or the re-drive
2298
+ * send failed and the next window retries).
2299
+ */
2300
+ async redriveStuckQueued(sessionId, watcher, inFlight, messages) {
2301
+ if (await this.deliverIfAnyAttemptCompleted(sessionId, watcher, inFlight, messages)) {
2302
+ return true;
2303
+ }
2304
+ if (inFlight.redriveAttempts >= this.stuckQueuedRedriveMax) {
2305
+ this.log({
2306
+ level: "error",
2307
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} stuck queued after ${inFlight.redriveAttempts} re-drive attempt(s) \u2014 marking failed`,
2308
+ conversation_id: watcher.conv.id,
2309
+ message_id: inFlight.evidentMessageId
2310
+ });
2311
+ try {
2312
+ await this.markFailed(watcher.conv.id, inFlight.evidentMessageId);
2313
+ } catch (err) {
2314
+ if (err instanceof ChannelAuthError) throw err;
2315
+ this.log({
2316
+ level: "error",
2317
+ message: `Failed to mark stuck message ${inFlight.evidentMessageId.slice(0, 8)} failed (leaving for the cron safety net): ${err instanceof Error ? err.message : String(err)}`,
2318
+ conversation_id: watcher.conv.id,
2319
+ message_id: inFlight.evidentMessageId
2320
+ });
2321
+ }
2322
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2323
+ return true;
2324
+ }
2325
+ inFlight.redriveAttempts += 1;
2326
+ inFlight.lastRedriveAt = this.now();
2327
+ this.log({
2328
+ level: "info",
2329
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} stuck queued on an idle session \u2014 re-driving into the SAME session with a native id (attempt ${inFlight.redriveAttempts}/${this.stuckQueuedRedriveMax})`,
2330
+ conversation_id: watcher.conv.id,
2331
+ message_id: inFlight.evidentMessageId
2332
+ });
2333
+ void this.postSignal(watcher.conv.id, inFlight.evidentMessageId, "redriven", {
2334
+ redrive_attempt: inFlight.redriveAttempts
2335
+ });
2336
+ const options = {
2337
+ agent: inFlight.message.opencode_agent ?? void 0,
2338
+ model: inFlight.message.opencode_model ?? void 0
2339
+ };
2340
+ const nativeId = nativeOpencodeMessageId();
2341
+ try {
2342
+ await sendPromptAsync(this.port, sessionId, inFlight.message.content, options, nativeId);
2343
+ } catch (err) {
2344
+ if (err instanceof ChannelAuthError) throw err;
2345
+ inFlight.redriveAttempts -= 1;
2346
+ this.log({
2347
+ level: "error",
2348
+ message: `Re-drive failed for stuck message ${inFlight.evidentMessageId.slice(0, 8)} (will retry next window): ${err instanceof Error ? err.message : String(err)}`,
2349
+ conversation_id: watcher.conv.id,
2350
+ message_id: inFlight.evidentMessageId
2351
+ });
2352
+ return false;
2353
+ }
2354
+ inFlight.opencodeMessageId = nativeId;
2355
+ inFlight.redriveOpencodeMessageId = nativeId;
2356
+ inFlight.attemptedOpencodeMessageIds.push(nativeId);
2357
+ inFlight.started = false;
2358
+ return false;
2359
+ }
2360
+ /**
2361
+ * Finding 2 (Bugbot #217): scan EVERY opencode id this stuck message has been
2362
+ * driven under (`attemptedOpencodeMessageIds`) for a COMPLETED correlated reply
2363
+ * in the tick's snapshot; if one is found, markDone off THAT id (so the reply an
2364
+ * earlier re-drive attempt eventually produced is delivered) instead of
2365
+ * re-driving again or marking failed. Idempotent + guarded like the watcher's
2366
+ * done branch. `@returns` true when the message left the in-flight set (delivered
2367
+ * or terminally-undeliverable) — the caller then stops servicing it this tick.
2368
+ *
2369
+ * The LATEST id is normally handled by `serviceInFlightMessage`'s own `done`
2370
+ * branch; this covers the ids a re-drive OVERWROTE, which that branch no longer
2371
+ * polls. `messageRunState === 'done'` means a completed, non-errored correlated
2372
+ * reply exists for that id.
2373
+ */
2374
+ async deliverIfAnyAttemptCompleted(sessionId, watcher, inFlight, messages) {
2375
+ const completedId = inFlight.attemptedOpencodeMessageIds.find(
2376
+ (id) => messageRunState(messages, id) === "done"
2377
+ );
2378
+ if (!completedId) return false;
2379
+ if (inFlight.done) {
2380
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2381
+ return true;
2382
+ }
2383
+ this.log({
2384
+ level: "info",
2385
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed under an earlier re-drive attempt's id \u2014 marking done (not re-driving/failing)`,
2386
+ conversation_id: watcher.conv.id,
2387
+ message_id: inFlight.evidentMessageId
2388
+ });
2389
+ try {
2390
+ await this.markDone(watcher.conv.id, inFlight.evidentMessageId, sessionId, completedId);
2391
+ } catch (err) {
2392
+ if (err instanceof ChannelAuthError) throw err;
2393
+ this.log({
2394
+ level: "error",
2395
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done off an earlier re-drive attempt (leaving for the cron safety net): ${err instanceof Error ? err.message : String(err)}`,
2396
+ conversation_id: watcher.conv.id,
2397
+ message_id: inFlight.evidentMessageId
2398
+ });
2399
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2400
+ return true;
2401
+ }
2402
+ inFlight.done = true;
2403
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2404
+ return true;
2405
+ }
2544
2406
  // -------------------------------------------------------------------------
2545
2407
  // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
2546
2408
  // -------------------------------------------------------------------------
@@ -2628,17 +2490,19 @@ var ChannelDriver = class {
2628
2490
  * Re-adopt ONE `processing` row against the tick's session message snapshot
2629
2491
  * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
2630
2492
  *
2631
- * Branches on `messageRunState(messages, row.opencode_message_id)` — the
2632
- * opencode-assigned user-message id persisted on the first `processing` PATCH
2633
- * (#218). A row with a NULL stored id (dispatched but the read-back never landed
2634
- * before the restart) has no id to correlate treated as an orphan and
2635
- * re-dispatched (at most once, see `forceReadoptRun`):
2493
+ * Branches on `messageRunState(messages, effectiveId)`, where `effectiveId` is
2494
+ * the NATIVE id a prior lifetime's re-drive ran this row under
2495
+ * (`row.opencode_message_id`) if present, else the STABLE derived id
2496
+ * (`opencodeMessageIdFor(row.id)`). Consulting the native id is what makes a
2497
+ * re-driven row that ALREADY ran/completed resolve correctly on restart instead
2498
+ * of looking `queued` under the (never-run) stable id and being re-driven AGAIN
2499
+ * (Bugbot #217 Finding 3 — the duplicate-turn bug):
2636
2500
  * - `done` → `markDone` now (guarded like the watcher's done branch);
2637
2501
  * - `failed` → `markFailed` with the surfaced error (issue #182), so an
2638
2502
  * errored turn is reported failed on restart, NOT re-dispatched;
2639
2503
  * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
2640
- * tracking the stored id so the reply correlates by it;
2641
- * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
2504
+ * tracking the effective id so the reply correlates by it;
2505
+ * - `unknown` → re-dispatch the STABLE id + attach a watcher (orphan).
2642
2506
  *
2643
2507
  * Only `ChannelAuthError` propagates.
2644
2508
  */
@@ -2652,8 +2516,10 @@ var ChannelDriver = class {
2652
2516
  });
2653
2517
  return;
2654
2518
  }
2655
- const ocId = row.opencode_message_id;
2656
- const state = messageRunState(messages, ocId ?? "");
2519
+ const stableId = opencodeMessageIdFor2(row.id);
2520
+ const nativeRedriveId = row.opencode_message_id;
2521
+ const ocId = nativeRedriveId ?? stableId;
2522
+ const state = messageRunState(messages, ocId);
2657
2523
  if (state === "done") {
2658
2524
  if (this.doneUndeliverable.has(row.id)) {
2659
2525
  this.log({
@@ -2671,7 +2537,7 @@ var ChannelDriver = class {
2671
2537
  message_id: row.id
2672
2538
  });
2673
2539
  try {
2674
- await this.markDone(row.conversation_id, row.id, sessionId, ocId);
2540
+ await this.markDone(row.conversation_id, row.id, sessionId, nativeRedriveId);
2675
2541
  } catch (err) {
2676
2542
  if (err instanceof ChannelAuthError) throw err;
2677
2543
  if (err instanceof ChannelTerminalError) {
@@ -2696,7 +2562,7 @@ var ChannelDriver = class {
2696
2562
  return;
2697
2563
  }
2698
2564
  if (state === "failed") {
2699
- const error2 = messageError(messages, ocId ?? "") ?? void 0;
2565
+ const error2 = messageError(messages, ocId) ?? void 0;
2700
2566
  this.log({
2701
2567
  level: "error",
2702
2568
  message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
@@ -2737,16 +2603,23 @@ var ChannelDriver = class {
2737
2603
  });
2738
2604
  return;
2739
2605
  }
2740
- if ((state === "running" || state === "queued") && ocId) {
2606
+ if (state === "running" || state === "queued") {
2741
2607
  const conv = this.convForRow(sessionId, row);
2742
2608
  const message = this.queuedMessageForRow(row);
2743
- this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
2609
+ this.registerReadopted(
2610
+ conv,
2611
+ sessionId,
2612
+ message,
2613
+ ocId,
2614
+ this.processedAtMs(row),
2615
+ nativeRedriveId
2616
+ );
2744
2617
  this.dispatched.add(row.id);
2745
2618
  this.readopted.add(row.id);
2746
2619
  this.ensureWatcherRunning(sessionId);
2747
2620
  this.log({
2748
2621
  level: "info",
2749
- message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
2622
+ message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (${nativeRedriveId ? "native re-drive id" : "stable id"}, no re-dispatch)`,
2750
2623
  conversation_id: row.conversation_id,
2751
2624
  message_id: row.id
2752
2625
  });
@@ -2755,45 +2628,23 @@ var ChannelDriver = class {
2755
2628
  await this.forceReadoptRun(sessionId, row);
2756
2629
  }
2757
2630
  /**
2758
- * Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
2759
- *
2760
- * #218/WI-5: the row's user message is absent (never kept, or a null stored id),
2761
- * so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
2762
- * read it back, and register the watcher under the assigned id so the reply
2763
- * correlates server-side.
2631
+ * Re-dispatch an orphaned (`unknown`) `processing` row (ADR-0046 Decision §2).
2764
2632
  *
2765
- * ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
2766
- * id). Without a guard, if this dispatches on tick N but the read-back+persist
2767
- * hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
2768
- * tick N+1 would dispatch AGAIN duplicate user turns. The `awaitingReadopt`
2769
- * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
2770
- * short-circuit while the row is latched; clear it on a successful dispatch (the
2771
- * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
2772
- * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
2773
- * may retry exactly once more).
2633
+ * The stable-id user message is absent from the session, so we (re-)dispatch with
2634
+ * the STABLE id (`opencodeMessageIdFor(row.id)`) NOT a divergent per-attempt id.
2635
+ * This is what keeps the reply correlatable: the server's completion
2636
+ * notification looks for the reply under the stable id, so the fresh turn's reply
2637
+ * (which hangs off the stable id) is found and delivered. The residual
2638
+ * duplicate-incomplete-turn semantics (ADR §2, `.harness/restart-recovery-orphan-finding.md`)
2639
+ * are unchanged and, for an ABSENT id, cannot bite there is no existing turn
2640
+ * to swallow the duplicate.
2774
2641
  *
2775
- * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
2642
+ * `evidentMessageId = row.id` addresses the SERVER row; the stable
2643
+ * `opencodeMessageId` is what the watcher polls. Deadline anchored to
2776
2644
  * `processed_at` (Invariant 1).
2777
2645
  */
2778
2646
  async forceReadoptRun(sessionId, row) {
2779
- if (this.stopped) {
2780
- this.log({
2781
- level: "info",
2782
- 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`,
2783
- conversation_id: row.conversation_id,
2784
- message_id: row.id
2785
- });
2786
- return;
2787
- }
2788
- if (this.awaitingReadopt.has(row.id)) {
2789
- this.log({
2790
- level: "info",
2791
- message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
2792
- conversation_id: row.conversation_id,
2793
- message_id: row.id
2794
- });
2795
- return;
2796
- }
2647
+ const ocId = opencodeMessageIdFor2(row.id);
2797
2648
  if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
2798
2649
  this.dontRedispatch.add(row.id);
2799
2650
  this.log({
@@ -2810,19 +2661,13 @@ var ChannelDriver = class {
2810
2661
  };
2811
2662
  this.log({
2812
2663
  level: "info",
2813
- message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
2664
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching with the stable id`,
2814
2665
  conversation_id: row.conversation_id,
2815
2666
  message_id: row.id
2816
2667
  });
2817
- this.awaitingReadopt.add(row.id);
2818
- let ocId;
2819
2668
  try {
2820
- ocId = await this.dispatchLocked(
2821
- sessionId,
2822
- () => sendPromptAsync(this.port, sessionId, row.content, options)
2823
- );
2669
+ await sendPromptAsync(this.port, sessionId, row.content, options, ocId);
2824
2670
  } catch (err) {
2825
- this.awaitingReadopt.delete(row.id);
2826
2671
  if (err instanceof ChannelAuthError) throw err;
2827
2672
  this.log({
2828
2673
  level: "error",
@@ -2832,22 +2677,11 @@ var ChannelDriver = class {
2832
2677
  });
2833
2678
  return;
2834
2679
  }
2835
- if (ocId === null) {
2836
- this.awaitingReadopt.delete(row.id);
2837
- this.log({
2838
- level: "error",
2839
- 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`,
2840
- conversation_id: row.conversation_id,
2841
- message_id: row.id
2842
- });
2843
- return;
2844
- }
2845
2680
  const conv = this.convForRow(sessionId, row);
2846
2681
  const message = this.queuedMessageForRow(row);
2847
2682
  this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
2848
2683
  this.dispatched.add(row.id);
2849
2684
  this.readopted.add(row.id);
2850
- this.awaitingReadopt.delete(row.id);
2851
2685
  this.ensureWatcherRunning(sessionId);
2852
2686
  }
2853
2687
  /**
@@ -2952,8 +2786,8 @@ var ChannelDriver = class {
2952
2786
  } catch {
2953
2787
  }
2954
2788
  for (const q of questions) {
2789
+ if (q.sessionID !== sessionId) continue;
2955
2790
  if (watcher.reportedQuestions.has(q.id)) continue;
2956
- if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
2957
2791
  const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
2958
2792
  const reported = await this.reportInteraction(
2959
2793
  watcher.conv.id,
@@ -2973,8 +2807,8 @@ var ChannelDriver = class {
2973
2807
  } catch {
2974
2808
  }
2975
2809
  for (const p of permissions) {
2810
+ if (p.sessionID !== sessionId) continue;
2976
2811
  if (watcher.reportedPermissions.has(p.id)) continue;
2977
- if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
2978
2812
  const paused = this.attributeInteraction(watcher, p.messageID, messages);
2979
2813
  const reported = await this.reportInteraction(
2980
2814
  watcher.conv.id,
@@ -2985,50 +2819,6 @@ var ChannelDriver = class {
2985
2819
  if (reported) watcher.reportedPermissions.add(p.id);
2986
2820
  }
2987
2821
  }
2988
- /**
2989
- * True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
2990
- * i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
2991
- * watched root. Sub-agents spawned via the `task` tool run in child sessions,
2992
- * so their questions/permissions live under a different `sessionID` that must
2993
- * still be attributed to the root conversation the watcher owns.
2994
- *
2995
- * Parents are cached in `sessionParents` so we walk each session at most once;
2996
- * a bounded depth cap guards against a cycle or a pathological chain, and any
2997
- * fetch failure is treated as "not a descendant" (best-effort — the interaction
2998
- * simply isn't surfaced this tick and is retried next tick once resolvable).
2999
- */
3000
- async sessionBelongsTo(sessionId, rootSessionId) {
3001
- let current = sessionId;
3002
- for (let depth = 0; current && depth < 32; depth++) {
3003
- if (current === rootSessionId) return true;
3004
- const parent = await this.resolveSessionParent(current);
3005
- if (parent === null || parent === void 0) return false;
3006
- current = parent;
3007
- }
3008
- return false;
3009
- }
3010
- /**
3011
- * Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
3012
- * `null` for a root session (no parent) and `undefined` when opencode is
3013
- * unreachable / the session can't be read (so the caller stops walking without
3014
- * caching a wrong answer — the next tick retries).
3015
- */
3016
- async resolveSessionParent(sessionId) {
3017
- const cached = this.sessionParents.get(sessionId);
3018
- if (cached !== void 0) return cached;
3019
- let parent = void 0;
3020
- try {
3021
- const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
3022
- if (res.ok) {
3023
- const body = await res.json();
3024
- parent = body && typeof body.parentID === "string" ? body.parentID : null;
3025
- }
3026
- } catch {
3027
- parent = void 0;
3028
- }
3029
- if (parent !== void 0) this.sessionParents.set(sessionId, parent);
3030
- return parent;
3031
- }
3032
2822
  /**
3033
2823
  * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
3034
2824
  *
@@ -3558,25 +3348,6 @@ async function resolveAgentIdFromKey(authHeader) {
3558
3348
  return { error: `Failed to resolve agent from key: ${message}` };
3559
3349
  }
3560
3350
  }
3561
- async function notifyAgentDisconnected(agentId, authHeader) {
3562
- const apiUrl = getApiUrlConfig();
3563
- try {
3564
- const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
3565
- method: "POST",
3566
- headers: { Authorization: authHeader }
3567
- });
3568
- if (!response.ok) {
3569
- const serverMessage = await readErrorMessage(response);
3570
- return {
3571
- ok: false,
3572
- error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
3573
- };
3574
- }
3575
- return { ok: true };
3576
- } catch (error2) {
3577
- return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
3578
- }
3579
- }
3580
3351
  async function getAgentInfo(agentId, authHeader) {
3581
3352
  const apiUrl = getApiUrlConfig();
3582
3353
  try {
@@ -3623,7 +3394,7 @@ async function getAgentInfo(agentId, authHeader) {
3623
3394
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
3624
3395
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
3625
3396
  var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
3626
- var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
3397
+ var CHANNEL_SETTLE_MS = Number(process.env.EVIDENT_SETTLE_MS) || void 0;
3627
3398
  function log2(state, message, isError = false) {
3628
3399
  if (state.json) {
3629
3400
  console.log(
@@ -3778,7 +3549,7 @@ async function driveChannels(state, driver) {
3778
3549
  logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
3779
3550
  if (state.interactive) displayStatus(state);
3780
3551
  }
3781
- await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
3552
+ await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
3782
3553
  if (state.idleTimeout !== null && idlePolls >= 2) {
3783
3554
  const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
3784
3555
  if (idleMs > state.idleTimeout * 1e3) {
@@ -3789,122 +3560,8 @@ async function driveChannels(state, driver) {
3789
3560
  }
3790
3561
  }
3791
3562
  }
3792
- var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
3793
- async function runSweep(state, driver, config2) {
3794
- const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
3795
- try {
3796
- const sessions = await listSessions(state.port);
3797
- if (sessions === null) {
3798
- logActivity(state, {
3799
- type: "info",
3800
- message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
3801
- });
3802
- return;
3803
- }
3804
- const toDelete = selectSessionsToDelete(
3805
- sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
3806
- {
3807
- maxAgeMs: config2.maxAgeMs,
3808
- maxCount: config2.maxCount,
3809
- nowMs: Date.now(),
3810
- protectedIds: driver.protectedSessionIds()
3811
- }
3812
- );
3813
- const protectedNow = driver.protectedSessionIds();
3814
- let deleted = 0;
3815
- let failed = 0;
3816
- let skippedNewlyActive = 0;
3817
- for (const id of toDelete) {
3818
- if (protectedNow.has(id)) {
3819
- skippedNewlyActive++;
3820
- logActivity(state, {
3821
- type: "info",
3822
- message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
3823
- });
3824
- continue;
3825
- }
3826
- if (await deleteSession(state.port, id)) deleted++;
3827
- else failed++;
3828
- }
3829
- const failedNote = failed > 0 ? `, failed ${failed}` : "";
3830
- const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
3831
- logActivity(state, {
3832
- type: "info",
3833
- message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
3834
- });
3835
- } catch (error2) {
3836
- const message = error2 instanceof Error ? error2.message : String(error2);
3837
- logActivity(state, {
3838
- type: "error",
3839
- error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
3840
- });
3841
- }
3842
- }
3843
- function scheduleSessionCleanup(state, driver, options) {
3844
- const config2 = resolveSessionCleanupConfig(
3845
- {
3846
- maxAge: options.sessionCleanupMaxAge,
3847
- maxCount: options.sessionCleanupMaxCount,
3848
- interval: options.sessionCleanupInterval
3849
- },
3850
- process.env
3851
- );
3852
- for (const warning2 of config2.warnings) {
3853
- logActivity(state, { type: "info", message: `Session cleanup: ${warning2}` });
3854
- }
3855
- if (!config2.enabled) return;
3856
- logActivity(state, {
3857
- type: "info",
3858
- message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
3859
- });
3860
- const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
3861
- const firstSweep = setTimeout(
3862
- () => void runSweep(state, driver, config2),
3863
- SESSION_CLEANUP_FIRST_SWEEP_MS
3864
- );
3865
- state.sessionCleanupTimers.push(interval, firstSweep);
3866
- }
3867
- async function notifyOffline(state) {
3868
- if (!state.agentId || !state.authHeader) return;
3869
- if (!state.connected) {
3870
- log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
3871
- return;
3872
- }
3873
- const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
3874
- if (result.ok) {
3875
- log2(state, "Notified Evident the agent is going offline");
3876
- } else {
3877
- logActivity(state, {
3878
- type: "error",
3879
- error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
3880
- });
3881
- if (state.interactive) displayStatus(state);
3882
- }
3883
- }
3884
- async function cleanup(state, opts = {}) {
3563
+ async function cleanup(state) {
3885
3564
  state.running = false;
3886
- for (const timer of state.sessionCleanupTimers) {
3887
- clearInterval(timer);
3888
- clearTimeout(timer);
3889
- }
3890
- state.sessionCleanupTimers = [];
3891
- if (opts.graceful && state.channelDriver) {
3892
- state.channelDriver.stop();
3893
- log2(state, "Draining in-flight channel work before shutdown...");
3894
- if (state.interactive) {
3895
- logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
3896
- displayStatus(state);
3897
- }
3898
- const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
3899
- if (!settled) {
3900
- logActivity(state, {
3901
- type: "info",
3902
- message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
3903
- });
3904
- if (state.interactive) displayStatus(state);
3905
- }
3906
- }
3907
- await notifyOffline(state);
3908
3565
  if (state.connection) {
3909
3566
  state.connection.close();
3910
3567
  state.connection = null;
@@ -3935,13 +3592,10 @@ async function run(options) {
3935
3592
  opencodeVersion: null,
3936
3593
  opencodeProcess: null,
3937
3594
  connection: null,
3938
- channelDriver: null,
3939
3595
  running: true,
3940
- shuttingDown: false,
3941
3596
  activityLog: [],
3942
3597
  messageCount: 0,
3943
3598
  lastProxiedActivityAt: null,
3944
- sessionCleanupTimers: [],
3945
3599
  authHeader: ""
3946
3600
  };
3947
3601
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
@@ -3952,15 +3606,13 @@ async function run(options) {
3952
3606
  );
3953
3607
  }
3954
3608
  const handleSignal = async () => {
3955
- if (state.shuttingDown) return;
3956
- state.shuttingDown = true;
3957
3609
  if (state.interactive) {
3958
3610
  logActivity(state, { type: "info", message: "Shutting down..." });
3959
3611
  displayStatus(state);
3960
3612
  } else {
3961
3613
  log2(state, "Shutting down...");
3962
3614
  }
3963
- await cleanup(state, { graceful: true });
3615
+ await cleanup(state);
3964
3616
  await shutdownTelemetry();
3965
3617
  process.exit(0);
3966
3618
  };
@@ -4081,13 +3733,13 @@ async function run(options) {
4081
3733
  getAuthHeader: () => state.authHeader,
4082
3734
  conversationFilter: state.conversationFilter,
4083
3735
  stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
3736
+ settleMs: CHANNEL_SETTLE_MS,
4084
3737
  log: (entry) => logActivity(state, {
4085
3738
  type: entry.level === "error" ? "error" : "info",
4086
3739
  message: entry.message,
4087
3740
  error: entry.level === "error" ? entry.message : void 0
4088
3741
  })
4089
3742
  });
4090
- state.channelDriver = channelDriver;
4091
3743
  const connection = new RunnerConnection({
4092
3744
  agentId: state.agentId,
4093
3745
  getAuthHeader: () => state.authHeader,
@@ -4184,12 +3836,10 @@ async function run(options) {
4184
3836
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
4185
3837
  throw error2;
4186
3838
  }
4187
- scheduleSessionCleanup(state, channelDriver, options);
4188
3839
  if (!interactive || state.json) {
4189
3840
  log2(state, "Driving channel messages...");
4190
3841
  }
4191
3842
  await driveChannels(state, channelDriver);
4192
- if (state.shuttingDown) return;
4193
3843
  await cleanup(state);
4194
3844
  if (state.json) {
4195
3845
  console.log(
@@ -4204,7 +3854,6 @@ async function run(options) {
4204
3854
  await shutdownTelemetry();
4205
3855
  process.exit(0);
4206
3856
  } catch (error2) {
4207
- if (state.shuttingDown) return;
4208
3857
  await cleanup(state);
4209
3858
  const message = error2 instanceof Error ? error2.message : String(error2);
4210
3859
  if (state.json) {
@@ -4239,16 +3888,7 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
4239
3888
  program.command("login").description("Authenticate with Evident").option("--token", "Use token-based authentication (for CI/CD)").option("--no-browser", "Do not open the browser automatically").action(login);
4240
3889
  program.command("logout").description("Remove stored credentials for the current endpoint").option("--all", "Remove stored credentials for all endpoints").action((options) => logout({ all: options.all }));
4241
3890
  program.command("whoami").description("Show the currently logged in user").action(whoami);
4242
- program.command("run").description("Connect to Evident and process messages").option("-a, --agent [id]", "Agent ID to connect to (optional when EVIDENT_AGENT_KEY is set)").option("-p, --port <port>", "OpenCode port (default: 4096)", "4096").option("-v, --verbose", "Show detailed request/response information").option("-c, --conversation <id>", "Process only this specific conversation").option("--idle-timeout <seconds>", "Exit after N seconds idle").option("--json", "Output in JSON format").option(
4243
- "--session-cleanup-max-age <duration>",
4244
- "Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
4245
- ).option(
4246
- "--session-cleanup-max-count <n>",
4247
- "Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
4248
- ).option(
4249
- "--session-cleanup-interval <duration>",
4250
- "How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
4251
- ).action(
3891
+ program.command("run").description("Connect to Evident and process messages").option("-a, --agent [id]", "Agent ID to connect to (optional when EVIDENT_AGENT_KEY is set)").option("-p, --port <port>", "OpenCode port (default: 4096)", "4096").option("-v, --verbose", "Show detailed request/response information").option("-c, --conversation <id>", "Process only this specific conversation").option("--idle-timeout <seconds>", "Exit after N seconds idle").option("--json", "Output in JSON format").action(
4252
3892
  (options) => {
4253
3893
  run({
4254
3894
  agent: options.agent,
@@ -4256,11 +3896,7 @@ program.command("run").description("Connect to Evident and process messages").op
4256
3896
  verbose: options.verbose,
4257
3897
  conversation: options.conversation,
4258
3898
  idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
4259
- json: options.json,
4260
- // Raw strings — the resolver in run.ts single-sources parsing (M1).
4261
- sessionCleanupMaxAge: options.sessionCleanupMaxAge,
4262
- sessionCleanupMaxCount: options.sessionCleanupMaxCount,
4263
- sessionCleanupInterval: options.sessionCleanupInterval
3899
+ json: options.json
4264
3900
  });
4265
3901
  }
4266
3902
  );