@evident-ai/cli 3.0.1-dev.38fce7f → 3.0.1-dev.39d9b5f

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
@@ -284,14 +285,14 @@ function blank() {
284
285
  console.log();
285
286
  }
286
287
  function waitForEnter(prompt = "Press Enter to continue...") {
287
- return new Promise((resolve) => {
288
+ return new Promise((resolve2) => {
288
289
  process.stdout.write(chalk.dim(prompt));
289
290
  const handler = () => {
290
291
  process.stdin.removeListener("data", handler);
291
292
  process.stdin.setRawMode?.(false);
292
293
  process.stdin.pause();
293
294
  console.log();
294
- resolve();
295
+ resolve2();
295
296
  };
296
297
  if (process.stdin.isTTY) {
297
298
  process.stdin.setRawMode?.(true);
@@ -301,7 +302,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
301
302
  });
302
303
  }
303
304
  function sleep(ms) {
304
- return new Promise((resolve) => setTimeout(resolve, ms));
305
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
305
306
  }
306
307
 
307
308
  // src/commands/login.ts
@@ -375,19 +376,19 @@ async function tokenLogin() {
375
376
  console.log("Visit your Evident dashboard to generate a CLI token.");
376
377
  blank();
377
378
  process.stdout.write("Paste token: ");
378
- const token = await new Promise((resolve) => {
379
+ const token = await new Promise((resolve2) => {
379
380
  let data = "";
380
381
  process.stdin.setEncoding("utf8");
381
382
  process.stdin.on("data", (chunk) => {
382
383
  data += chunk;
383
384
  });
384
385
  process.stdin.on("end", () => {
385
- resolve(data.trim());
386
+ resolve2(data.trim());
386
387
  });
387
388
  if (process.stdin.isTTY) {
388
389
  process.stdin.once("data", (chunk) => {
389
390
  process.stdin.pause();
390
- resolve(chunk.toString().trim());
391
+ resolve2(chunk.toString().trim());
391
392
  });
392
393
  process.stdin.resume();
393
394
  }
@@ -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;
@@ -679,11 +706,24 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
679
706
  if (health.healthy) {
680
707
  return health;
681
708
  }
682
- await new Promise((resolve) => setTimeout(resolve, 1e3));
709
+ await new Promise((resolve2) => setTimeout(resolve2, 1e3));
683
710
  }
684
711
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
685
712
  }
686
713
 
714
+ // src/lib/opencode/opencode-version-gate.ts
715
+ var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
716
+ function isQueueValidatedVersion(version2) {
717
+ if (!version2) return false;
718
+ return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
719
+ }
720
+ function buildOpenCodeVersionWarning(version2) {
721
+ if (isQueueValidatedVersion(version2)) return null;
722
+ const detected = version2 ? `v${version2}` : "unknown";
723
+ const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
724
+ return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack/WhatsApp) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
725
+ }
726
+
687
727
  // src/lib/opencode/process.ts
688
728
  import { execSync, spawn } from "child_process";
689
729
  var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
@@ -996,7 +1036,37 @@ function roleOf(m) {
996
1036
  }
997
1037
  function completedOf(m) {
998
1038
  if (!m || typeof m !== "object") return void 0;
999
- return m.info?.time?.completed;
1039
+ return m.info?.time?.completed ?? m.time?.completed;
1040
+ }
1041
+ function createdOf(m) {
1042
+ if (!m || typeof m !== "object") return void 0;
1043
+ return m.info?.time?.created ?? m.time?.created;
1044
+ }
1045
+ function idOf(m) {
1046
+ if (!m || typeof m !== "object") return void 0;
1047
+ if (typeof m.id === "string") return m.id;
1048
+ const infoId = m.info?.id;
1049
+ return typeof infoId === "string" ? infoId : void 0;
1050
+ }
1051
+ function parentIdOf(m) {
1052
+ if (!m || typeof m !== "object") return void 0;
1053
+ if (typeof m.parentID === "string") return m.parentID;
1054
+ const infoParent = m.info?.parentID;
1055
+ return typeof infoParent === "string" ? infoParent : void 0;
1056
+ }
1057
+ function finishOf(m) {
1058
+ if (!m || typeof m !== "object") return void 0;
1059
+ if (typeof m.finish === "string") return m.finish;
1060
+ const infoFinish = m.info?.finish;
1061
+ return typeof infoFinish === "string" ? infoFinish : void 0;
1062
+ }
1063
+ function errorOf(m) {
1064
+ if (!m || typeof m !== "object") return void 0;
1065
+ return m.info?.error ?? m.error;
1066
+ }
1067
+ function isAssistantInFlight(m) {
1068
+ if (completedOf(m) == null) return true;
1069
+ return finishOf(m) === "tool-calls";
1000
1070
  }
1001
1071
  async function getSessionMessages(port, sessionId) {
1002
1072
  try {
@@ -1008,11 +1078,47 @@ async function getSessionMessages(port, sessionId) {
1008
1078
  return null;
1009
1079
  }
1010
1080
  }
1011
- function isTurnComplete(messages) {
1012
- if (!messages || messages.length === 0) return false;
1013
- const last = messages[messages.length - 1];
1014
- if (roleOf(last) !== "assistant") return false;
1015
- return completedOf(last) != null;
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
+ }
1016
1122
  }
1017
1123
  async function createOpenCodeSession(port, directory) {
1018
1124
  const url = new URL(`${opencodeBase(port)}/session`);
@@ -1031,7 +1137,15 @@ async function createOpenCodeSession(port, directory) {
1031
1137
  const data = await response.json();
1032
1138
  return data.id;
1033
1139
  }
1034
- async function sendMessageToOpenCode(port, sessionId, content, options, hooks, maxWaitMs = 10 * 60 * 1e3) {
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
+ );
1035
1149
  const body = {
1036
1150
  parts: [{ type: "text", text: content }]
1037
1151
  };
@@ -1047,79 +1161,213 @@ async function sendMessageToOpenCode(port, sessionId, content, options, hooks, m
1047
1161
  };
1048
1162
  }
1049
1163
  }
1050
- let pollDone = false;
1051
- const reportedQuestions = /* @__PURE__ */ new Set();
1052
- const reportedPermissions = /* @__PURE__ */ new Set();
1053
- const pollInteractive = async () => {
1054
- while (!pollDone) {
1055
- await new Promise((resolve) => setTimeout(resolve, 1e3));
1056
- if (pollDone) break;
1057
- if (hooks?.onQuestion) {
1058
- try {
1059
- const res = await fetch(`${opencodeBase(port)}/question`);
1060
- if (res.ok) {
1061
- const questions = await res.json();
1062
- for (const q of questions) {
1063
- if (q.sessionID === sessionId && !reportedQuestions.has(q.id)) {
1064
- reportedQuestions.add(q.id);
1065
- await hooks.onQuestion(q);
1066
- }
1067
- }
1068
- }
1069
- } catch {
1070
- }
1071
- }
1072
- if (hooks?.onPermission) {
1073
- try {
1074
- const res = await fetch(`${opencodeBase(port)}/permission`);
1075
- if (res.ok) {
1076
- const permissions = await res.json();
1077
- for (const p of permissions) {
1078
- if (p.sessionID === sessionId && !reportedPermissions.has(p.id)) {
1079
- reportedPermissions.add(p.id);
1080
- await hooks.onPermission(p);
1081
- }
1082
- }
1083
- }
1084
- } catch {
1164
+ const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
1165
+ method: "POST",
1166
+ headers: { "Content-Type": "application/json" },
1167
+ body: JSON.stringify(body)
1168
+ });
1169
+ if (res.status < 200 || res.status >= 300) {
1170
+ const text = await res.text().catch(() => "");
1171
+ throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1172
+ }
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 };
1085
1187
  }
1086
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
+ }
1197
+ function findAssistantReplyAfter(messages, userMessageId) {
1198
+ if (!messages || messages.length === 0) return null;
1199
+ const byParent = messages.find(
1200
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
1201
+ );
1202
+ if (byParent) return byParent;
1203
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1204
+ if (userIndex === -1) return null;
1205
+ for (let i = userIndex + 1; i < messages.length; i++) {
1206
+ if (roleOf(messages[i]) === "assistant") return messages[i];
1207
+ }
1208
+ return null;
1209
+ }
1210
+ function findLastAssistantReplyFor(messages, userMessageId) {
1211
+ if (!messages || messages.length === 0) return null;
1212
+ let lastCorrelated = null;
1213
+ let lastNonErrored = null;
1214
+ for (let i = messages.length - 1; i >= 0; i--) {
1215
+ const m = messages[i];
1216
+ if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
1217
+ if (lastCorrelated === null) lastCorrelated = m;
1218
+ if (errorOf(m) == null) {
1219
+ lastNonErrored = m;
1220
+ break;
1221
+ }
1222
+ }
1223
+ if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
1224
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1225
+ if (userIndex === -1) return null;
1226
+ let last = null;
1227
+ let lastOk = null;
1228
+ for (let i = userIndex + 1; i < messages.length; i++) {
1229
+ const role = roleOf(messages[i]);
1230
+ if (role === "user") break;
1231
+ if (role === "assistant") {
1232
+ last = messages[i];
1233
+ if (errorOf(messages[i]) == null) lastOk = messages[i];
1087
1234
  }
1235
+ }
1236
+ return lastOk ?? last;
1237
+ }
1238
+ function messageRunState(messages, userMessageId) {
1239
+ if (!messages || messages.length === 0) return "unknown";
1240
+ const hasUser = messages.some((m) => idOf(m) === userMessageId);
1241
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1242
+ if (!hasUser) {
1243
+ if (!reply) return "unknown";
1244
+ }
1245
+ if (!reply) return "queued";
1246
+ if (isAssistantInFlight(reply)) return "running";
1247
+ return errorOf(reply) != null ? "failed" : "done";
1248
+ }
1249
+ function messageError(messages, userMessageId) {
1250
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1251
+ const error2 = errorOf(reply);
1252
+ if (error2 == null) return null;
1253
+ if (typeof error2 === "string") return error2;
1254
+ if (typeof error2 === "object") {
1255
+ const e = error2;
1256
+ const dataMessage = e.data?.message;
1257
+ if (typeof dataMessage === "string") return dataMessage;
1258
+ if (typeof e.message === "string") return e.message;
1259
+ }
1260
+ return "The agent run failed.";
1261
+ }
1262
+ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
1263
+ if (!messages || messages.length === 0) return false;
1264
+ return messages.some(
1265
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
1266
+ );
1267
+ }
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;
1088
1297
  };
1089
- const sendMessage = async () => {
1090
- const controller = new AbortController();
1091
- const timer = setTimeout(() => controller.abort(), maxWaitMs);
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);
1312
+ }
1313
+ }
1314
+ return toDelete;
1315
+ }
1316
+ var DEFAULT_INTERVAL = "1h";
1317
+ function resolve(flag, envValue, fallback) {
1318
+ return flag ?? envValue ?? fallback;
1319
+ }
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.`);
1324
+ }
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) {
1092
1342
  try {
1093
- const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/message`, {
1094
- method: "POST",
1095
- headers: { "Content-Type": "application/json" },
1096
- body: JSON.stringify(body),
1097
- signal: controller.signal
1098
- });
1099
- if (!res.ok) {
1100
- const text = await res.text().catch(() => "");
1101
- throw new Error(`OpenCode message failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1102
- }
1103
- const sessionRes = await fetch(`${opencodeBase(port)}/session/${sessionId}`).catch(
1104
- () => null
1343
+ maxAgeMs = parseDurationMs(maxAgeRaw);
1344
+ } catch (err) {
1345
+ warnings.push(
1346
+ `Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
1105
1347
  );
1106
- const session = sessionRes?.ok ? await sessionRes.json() : null;
1107
- const reportedInteraction = reportedQuestions.size > 0 || reportedPermissions.size > 0;
1108
- const turnComplete = isTurnComplete(await getSessionMessages(port, sessionId));
1109
- const awaitingInteraction = reportedInteraction && !turnComplete;
1110
- return { title: session?.title, awaitingInteraction };
1348
+ }
1349
+ }
1350
+ let maxCount;
1351
+ if (maxCountRaw !== void 0) {
1352
+ try {
1353
+ maxCount = parseMaxCount(maxCountRaw);
1111
1354
  } catch (err) {
1112
- if (err instanceof Error && err.name === "AbortError") {
1113
- throw new Error("Message processing timed out");
1114
- }
1115
- throw err;
1116
- } finally {
1117
- clearTimeout(timer);
1118
- pollDone = true;
1355
+ warnings.push(
1356
+ `Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
1357
+ );
1119
1358
  }
1120
- };
1121
- const [result] = await Promise.all([sendMessage(), pollInteractive()]);
1122
- return result;
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 };
1123
1371
  }
1124
1372
 
1125
1373
  // src/lib/tunnel/connection.ts
@@ -1190,24 +1438,34 @@ var StreamForwarder = class {
1190
1438
  }
1191
1439
  async handleOpen(frame) {
1192
1440
  const { sid, method, path, headers, has_body } = frame;
1441
+ const correlationId = headers?.[CORRELATION_ID_HEADER];
1442
+ const startedAt = Date.now();
1193
1443
  if (path === TUNNEL_DRAIN_PING_PATH) {
1194
1444
  this.callbacks.onDrainPing?.();
1195
1445
  this.send({ type: "head", sid, status: 204, headers: {} });
1196
1446
  this.send({ type: "res_end", sid });
1197
1447
  return;
1198
1448
  }
1449
+ if (process.env.DEBUG) {
1450
+ log("debug", "agent_request", {
1451
+ correlation_id: correlationId,
1452
+ sid,
1453
+ method,
1454
+ path: stripQuery(path)
1455
+ });
1456
+ }
1199
1457
  const ac = new AbortController();
1200
1458
  let bodyPromise;
1201
1459
  let pushBody;
1202
1460
  let endBody;
1203
1461
  if (has_body) {
1204
1462
  const chunks = [];
1205
- bodyPromise = new Promise((resolve) => {
1463
+ bodyPromise = new Promise((resolve2) => {
1206
1464
  pushBody = (buf) => {
1207
1465
  chunks.push(buf);
1208
1466
  };
1209
1467
  endBody = () => {
1210
- resolve(Buffer.concat(chunks));
1468
+ resolve2(Buffer.concat(chunks));
1211
1469
  };
1212
1470
  });
1213
1471
  }
@@ -1242,6 +1500,14 @@ var StreamForwarder = class {
1242
1500
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1243
1501
  });
1244
1502
  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
+ }
1245
1511
  this.callbacks.onHead?.(sid, upstream.status);
1246
1512
  try {
1247
1513
  if (upstream.body) {
@@ -1317,7 +1583,7 @@ function connectTunnel(options) {
1317
1583
  } = options;
1318
1584
  const tunnelUrl = getTunnelUrlConfig();
1319
1585
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
1320
- return new Promise((resolve, reject) => {
1586
+ return new Promise((resolve2, reject) => {
1321
1587
  const ws = new WebSocket2(url, {
1322
1588
  headers: {
1323
1589
  Authorization: authHeader
@@ -1382,7 +1648,7 @@ function connectTunnel(options) {
1382
1648
  clearTimeout(connectionTimeout);
1383
1649
  const connectedAgentId = message.agent_id ?? agentId;
1384
1650
  onConnected?.(connectedAgentId);
1385
- resolve({
1651
+ resolve2({
1386
1652
  ws,
1387
1653
  close: () => ws.close(1e3, "CLI shutdown")
1388
1654
  });
@@ -1498,6 +1764,12 @@ var RunnerConnection = class {
1498
1764
  };
1499
1765
 
1500
1766
  // src/lib/channels/driver.ts
1767
+ function messageIdOf(m) {
1768
+ if (!m || typeof m !== "object") return void 0;
1769
+ if (typeof m.id === "string") return m.id;
1770
+ const infoId = m.info?.id;
1771
+ return typeof infoId === "string" ? infoId : void 0;
1772
+ }
1501
1773
  var DEFAULT_RETRY_POLICY = {
1502
1774
  maxAttempts: 6,
1503
1775
  baseDelayMs: 500,
@@ -1505,12 +1777,23 @@ var DEFAULT_RETRY_POLICY = {
1505
1777
  };
1506
1778
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1507
1779
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1780
+ var DEFAULT_STUCK_QUEUED_MS = 6e4;
1781
+ var HEARTBEAT_MS = 6e4;
1782
+ var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
1508
1783
  var ChannelAuthError = class extends Error {
1509
1784
  constructor(message) {
1510
1785
  super(message);
1511
1786
  this.name = "ChannelAuthError";
1512
1787
  }
1513
1788
  };
1789
+ var ChannelTerminalError = class extends Error {
1790
+ status;
1791
+ constructor(message, status) {
1792
+ super(message);
1793
+ this.name = "ChannelTerminalError";
1794
+ this.status = status;
1795
+ }
1796
+ };
1514
1797
  function backoffDelay(attempt, policy) {
1515
1798
  const exp = policy.baseDelayMs * Math.pow(2, attempt);
1516
1799
  const capped = Math.min(policy.maxDelayMs, exp);
@@ -1531,15 +1814,82 @@ var ChannelDriver = class {
1531
1814
  sleep;
1532
1815
  pausedPollIntervalMs;
1533
1816
  pausedMaxWaitMs;
1817
+ stuckQueuedMs;
1818
+ now;
1534
1819
  /** Cache of conversationId → opencode sessionId. */
1535
1820
  sessions = /* @__PURE__ */ new Map();
1536
1821
  /**
1537
- * Outstanding paused-session watchers, keyed by `message.id` (WI-2-CLI).
1538
- * Single-flight per message: while a watcher is live for a message we never
1539
- * start a second one. The message stays `processing` for the watcher's lifetime
1540
- * so the `?status=pending` drain cannot double-claim it.
1822
+ * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
1823
+ * longer idempotent (no caller-supplied `messageID`), and its read-back picks
1824
+ * "the one new user row" which is only unambiguous if no OTHER dispatch into
1825
+ * the SAME session interleaves its snapshot→POST→read-back. This map chains each
1826
+ * session's dispatches so they run serially; distinct sessions stay concurrent.
1827
+ */
1828
+ sessionDispatchLocks = /* @__PURE__ */ new Map();
1829
+ /**
1830
+ * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1831
+ * session: one polling loop services all of that session's in-flight messages.
1832
+ * A session entry exists while it has any in-flight (dispatched-but-not-done)
1833
+ * message; it is removed once its in-flight set empties.
1541
1834
  */
1542
1835
  watchers = /* @__PURE__ */ new Map();
1836
+ /**
1837
+ * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
1838
+ * dispatched and are still in-flight. A message in this set is never
1839
+ * re-`prompt_async`-ed by a subsequent poll tick while it is queued/running.
1840
+ * Backed by a stable minted opencode `messageID` whose duplicate re-enqueue is
1841
+ * idempotent on opencode (PoC fact 9) — so even if this set is lost on restart,
1842
+ * a steady-state-poll re-dispatch will not double-run the message.
1843
+ */
1844
+ dispatched = /* @__PURE__ */ new Set();
1845
+ /**
1846
+ * Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
1847
+ * Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
1848
+ * so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
1849
+ * it is re-adopted and removed when its watcher settles or it is observed off
1850
+ * the processing list.
1851
+ */
1852
+ readopted = /* @__PURE__ */ new Set();
1853
+ /**
1854
+ * "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
1855
+ * re-adopted running/orphan row's watcher hit its `processed_at`-anchored
1856
+ * deadline (or an orphan whose window already elapsed): the still-`processing`
1857
+ * server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
1858
+ * drain until the 15-min cron resets it — spamming new turns.
1859
+ *
1860
+ * CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
1861
+ * does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
1862
+ * in opencode must still be delivered via `markDone` on the next drain — so
1863
+ * `readoptOne` computes `state` FIRST and this set is checked only on the
1864
+ * non-done path. It is cleared once the row leaves the processing list (cron
1865
+ * reset → it drains normally as `pending`), so it can never leak.
1866
+ */
1867
+ dontRedispatch = /* @__PURE__ */ new Set();
1868
+ /**
1869
+ * "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
1870
+ * re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
1871
+ * never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
1872
+ * that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
1873
+ * markDone failure must NOT land here (it must still retry next drain). Separate
1874
+ * from `dontRedispatch` because the two concerns are independent: a row can need
1875
+ * "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
1876
+ * the row leaves the processing list, exactly like `dontRedispatch`.
1877
+ */
1878
+ doneUndeliverable = /* @__PURE__ */ new Set();
1879
+ /**
1880
+ * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
1881
+ * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
1882
+ * is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
1883
+ * persist hasn't landed before tick N+1 re-reads the still-null
1884
+ * `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
1885
+ * A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
1886
+ * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
1887
+ * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
1888
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
1889
+ * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
1890
+ * so the NEXT tick may retry exactly once more).
1891
+ */
1892
+ awaitingReadopt = /* @__PURE__ */ new Set();
1543
1893
  /**
1544
1894
  * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1545
1895
  * first session creation so drain-created sessions are rooted at the project
@@ -1547,8 +1897,33 @@ var ChannelDriver = class {
1547
1897
  * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1548
1898
  */
1549
1899
  opencodeDirectory = void 0;
1900
+ /**
1901
+ * Cache of opencode `sessionId → parentID` (its parent session, or `null` when
1902
+ * the session is a root with no parent). Sub-agents spawned via the `task` tool
1903
+ * run in CHILD sessions whose `parentID` chains up to the Evident-created
1904
+ * (watched) session; we resolve this once per session so a child-session
1905
+ * question/permission can be attributed to the watched session's subtree
1906
+ * (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
1907
+ * entry = not yet resolved; `null` = resolved root (stop walking).
1908
+ */
1909
+ sessionParents = /* @__PURE__ */ new Map();
1550
1910
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1551
1911
  draining = false;
1912
+ /**
1913
+ * The currently-executing `drainPending()` promise, or null when idle. Lets a
1914
+ * graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
1915
+ * is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
1916
+ * drain that entered before `stop()` still registers its watcher).
1917
+ */
1918
+ activeDrain = null;
1919
+ /**
1920
+ * Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
1921
+ * dispatches NEW work (it returns 0 immediately) — but the per-session watcher
1922
+ * loops already running keep going so in-flight turns can finish and deliver
1923
+ * their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
1924
+ * and stops opencode.
1925
+ */
1926
+ stopped = false;
1552
1927
  constructor(config2) {
1553
1928
  this.agentId = config2.agentId;
1554
1929
  this.port = config2.port;
@@ -1562,6 +1937,8 @@ var ChannelDriver = class {
1562
1937
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1563
1938
  this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1564
1939
  this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
1940
+ this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
1941
+ this.now = config2.now ?? (() => Date.now());
1565
1942
  }
1566
1943
  /** The IPv4-loopback base URL for the local `opencode serve`. */
1567
1944
  get opencodeBase() {
@@ -1571,16 +1948,29 @@ var ChannelDriver = class {
1571
1948
  // Public API
1572
1949
  // -------------------------------------------------------------------------
1573
1950
  /**
1574
- * Drain all pending channel conversations once: poll → processcallback.
1951
+ * Drain all pending channel conversations once: poll → dispatchregister.
1575
1952
  * Called on tunnel `connected` (WI-CHAN-4) and on each poll tick by `run.ts`.
1576
1953
  * Re-entrant calls while a drain is in flight are skipped (return 0).
1577
1954
  *
1578
- * @returns the number of messages processed.
1955
+ * @returns the number of messages NEWLY dispatched to opencode's native queue.
1579
1956
  */
1580
1957
  async drainPending() {
1958
+ if (this.stopped) return 0;
1581
1959
  if (this.draining) return 0;
1582
1960
  this.draining = true;
1583
- let processed = 0;
1961
+ const run2 = this.runDrain();
1962
+ this.activeDrain = run2.then(
1963
+ () => {
1964
+ this.activeDrain = null;
1965
+ },
1966
+ () => {
1967
+ this.activeDrain = null;
1968
+ }
1969
+ );
1970
+ return run2;
1971
+ }
1972
+ async runDrain() {
1973
+ let dispatched = 0;
1584
1974
  try {
1585
1975
  const conversations = await this.getPendingConversations();
1586
1976
  if (conversations.length > 0) {
@@ -1591,107 +1981,226 @@ var ChannelDriver = class {
1591
1981
  });
1592
1982
  }
1593
1983
  for (const conv of conversations) {
1594
- processed += await this.processConversation(conv);
1984
+ if (this.stopped) break;
1985
+ dispatched += await this.processConversation(conv);
1595
1986
  }
1987
+ await this.readoptProcessing();
1596
1988
  } finally {
1597
1989
  this.draining = false;
1598
1990
  }
1599
- return processed;
1991
+ return dispatched;
1992
+ }
1993
+ /**
1994
+ * True while any per-session watcher has a non-empty in-flight dispatched set
1995
+ * (Task 3.7). `run.ts` treats this as NON-idle so `--idle-timeout` cannot exit
1996
+ * the process while a dispatched message is still queued/running — which would
1997
+ * kill the turn and orphan its reply.
1998
+ */
1999
+ hasInFlightWatchers() {
2000
+ for (const watcher of this.watchers.values()) {
2001
+ if (watcher.inFlight.size > 0) return true;
2002
+ }
2003
+ return false;
2004
+ }
2005
+ /**
2006
+ * OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
2007
+ * exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
2008
+ * `watchers` entry whose `inFlight` set is non-empty — the same predicate
2009
+ * `hasInFlightWatchers()` uses, lifted to return the ids.
2010
+ *
2011
+ * Deliberately does NOT include `this.sessions` (the permanent, never-pruned
2012
+ * conversation→session cache). Protecting every bound-but-idle session there
2013
+ * would shield nearly every session and defeat cleanup — AND it is unnecessary:
2014
+ * `ensureSession` is self-healing (it recreates a session whose id no longer
2015
+ * exists), so deleting an idle bound session is harmless — the conversation's
2016
+ * next turn transparently rebinds a fresh one. The only thing worth protecting
2017
+ * is a session with a turn ACTIVELY in flight right now: tearing that down
2018
+ * mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
2019
+ */
2020
+ protectedSessionIds() {
2021
+ const ids = /* @__PURE__ */ new Set();
2022
+ for (const [sessionId, watcher] of this.watchers) {
2023
+ if (watcher.inFlight.size > 0) ids.add(sessionId);
2024
+ }
2025
+ return ids;
2026
+ }
2027
+ /**
2028
+ * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
2029
+ * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
2030
+ * — but the watcher loops already tracking in-flight turns keep running, so a
2031
+ * turn that has finished (or is about to) still fires `markDone` and delivers
2032
+ * its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
2033
+ */
2034
+ stop() {
2035
+ this.stopped = true;
2036
+ }
2037
+ /**
2038
+ * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
2039
+ * graceful shutdown, so a turn whose reply is ready — or completes within the
2040
+ * window — is delivered before the process exits, instead of being cut off and
2041
+ * left for the ADR-0046 restart-recovery path.
2042
+ *
2043
+ * Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
2044
+ * far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
2045
+ * window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
2046
+ * set empties OR the timeout elapses. Anything still in flight at the timeout is
2047
+ * safe to abandon — it stays `processing` server-side and is re-adopted on the
2048
+ * next runner start (ADR-0046).
2049
+ *
2050
+ * @returns true if all in-flight work settled within the window; false if the
2051
+ * timeout elapsed with work still in flight.
2052
+ */
2053
+ async waitForInFlight(timeoutMs) {
2054
+ const deadline = this.now() + timeoutMs;
2055
+ const step = Math.min(this.pausedPollIntervalMs, 250);
2056
+ if (this.activeDrain) {
2057
+ let drainSettled = false;
2058
+ void this.activeDrain.then(() => {
2059
+ drainSettled = true;
2060
+ });
2061
+ while (!drainSettled) {
2062
+ if (this.now() >= deadline) return false;
2063
+ await this.sleep(step);
2064
+ }
2065
+ }
2066
+ while (this.hasInFlightWatchers()) {
2067
+ if (this.now() >= deadline) return false;
2068
+ await this.sleep(step);
2069
+ }
2070
+ return true;
1600
2071
  }
1601
2072
  /**
1602
- * Await all outstanding paused-session watchers (WI-2-CLI).
2073
+ * Await all outstanding per-session watchers (WI-3).
1603
2074
  *
1604
- * In production the watchers are deliberately started-not-awaited so the drain
1605
- * loop never blocks on them and process exit is not held up (the cron recovers
1606
- * any abandoned ones). This helper exists primarily for deterministic tests
1607
- * that need to observe the watcher's effect (the `done` PATCH or its giving up)
1608
- * after a non-blocking `drainPending`. Watchers never reject, so this resolves.
2075
+ * In production the watcher loops are deliberately started-not-awaited so the
2076
+ * drain loop never blocks on them and process exit is not held up (the cron
2077
+ * recovers any abandoned ones). This helper exists primarily for deterministic
2078
+ * tests that need to observe a watcher's effect (the `processing`/`done` PATCH
2079
+ * or its giving up) after a non-blocking `drainPending`. Watcher loops never
2080
+ * reject, so this resolves.
1609
2081
  */
1610
2082
  async flushPausedWatchers() {
1611
- await Promise.all([...this.watchers.values()]);
2083
+ while (true) {
2084
+ const loops = [...this.watchers.values()].map((w) => w.loop).filter((l) => l != null);
2085
+ if (loops.length === 0) return;
2086
+ await Promise.all(loops);
2087
+ const stillLive = [...this.watchers.values()].some((w) => w.loop != null);
2088
+ if (!stillLive) return;
2089
+ }
1612
2090
  }
1613
2091
  // -------------------------------------------------------------------------
1614
- // Conversation processing
2092
+ // Conversation processing (WI-3 — async dispatch)
1615
2093
  // -------------------------------------------------------------------------
2094
+ /**
2095
+ * Dispatch each pending message for a conversation to opencode's native queue
2096
+ * via `prompt_async` (Task 3.2) and register it with the conversation's
2097
+ * per-session watcher. Does NOT block on the turn and does NOT call
2098
+ * `markProcessing` here — that fires from the watcher on running-start.
2099
+ *
2100
+ * @returns the count of messages NEWLY dispatched (not already in-flight).
2101
+ */
1616
2102
  async processConversation(conv) {
1617
2103
  const sessionId = await this.ensureSession(conv);
1618
2104
  const messages = await this.getPendingMessages(conv.id);
1619
- let processed = 0;
2105
+ let dispatched = 0;
2106
+ let skippedAlreadyDispatched = 0;
1620
2107
  for (const message of messages) {
1621
- const claimed = await this.markProcessing(conv.id, message.id);
1622
- if (!claimed) {
1623
- this.log({
1624
- level: "info",
1625
- message: `Message ${message.id.slice(0, 8)} already claimed \u2014 skipping`,
1626
- conversation_id: conv.id,
1627
- message_id: message.id
1628
- });
2108
+ if (this.stopped) break;
2109
+ if (this.dispatched.has(message.id)) {
2110
+ skippedAlreadyDispatched += 1;
1629
2111
  continue;
1630
2112
  }
2113
+ const options = {
2114
+ agent: message.opencode_agent ?? void 0,
2115
+ model: message.opencode_model ?? void 0
2116
+ };
2117
+ let opencodeMessageId;
1631
2118
  try {
1632
2119
  this.log({
1633
2120
  level: "info",
1634
- message: `Sending queued message ${message.id.slice(0, 8)} to OpenCode (session ${sessionId.slice(0, 8)})`,
2121
+ message: `Dispatching message ${message.id.slice(0, 8)} to OpenCode native queue (session ${sessionId.slice(0, 8)})`,
1635
2122
  conversation_id: conv.id,
1636
2123
  message_id: message.id
1637
2124
  });
1638
- const result = await sendMessageToOpenCode(
1639
- this.port,
2125
+ opencodeMessageId = await this.dispatchLocked(
1640
2126
  sessionId,
1641
- message.content,
1642
- {
1643
- agent: message.opencode_agent ?? void 0,
1644
- model: message.opencode_model ?? void 0
1645
- },
1646
- {
1647
- onQuestion: (question) => this.reportInteraction(conv.id, "question", question),
1648
- onPermission: (permission) => this.reportInteraction(conv.id, "permission", permission)
1649
- }
2127
+ () => sendPromptAsync(this.port, sessionId, message.content, options)
1650
2128
  );
1651
- if (result.awaitingInteraction) {
2129
+ } catch (err) {
2130
+ if (err instanceof ChannelAuthError) throw err;
2131
+ this.dispatched.delete(message.id);
2132
+ if (await sessionExists(this.port, sessionId) === false) {
2133
+ this.sessions.delete(conv.id);
1652
2134
  this.log({
1653
2135
  level: "info",
1654
- message: `Message ${message.id.slice(0, 8)} paused awaiting interaction \u2014 watching session for completion`,
2136
+ 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.`,
1655
2137
  conversation_id: conv.id,
1656
2138
  message_id: message.id
1657
2139
  });
1658
- this.startPausedWatcher(conv, message, sessionId);
1659
- continue;
2140
+ break;
1660
2141
  }
1661
- await this.confirmCompletion(sessionId);
1662
- await this.markDone(conv.id, message.id, sessionId);
1663
- processed += 1;
2142
+ await this.markFailed(conv.id, message.id).catch(() => {
2143
+ });
1664
2144
  this.log({
1665
- level: "info",
1666
- message: `Message ${message.id.slice(0, 8)} processed`,
2145
+ level: "error",
2146
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${err instanceof Error ? err.message : String(err)}`,
1667
2147
  conversation_id: conv.id,
1668
2148
  message_id: message.id
1669
2149
  });
1670
- } catch (err) {
1671
- if (err instanceof ChannelAuthError) throw err;
1672
- await this.markFailed(conv.id, message.id).catch(() => {
1673
- });
2150
+ continue;
2151
+ }
2152
+ if (opencodeMessageId === null) {
1674
2153
  this.log({
1675
2154
  level: "error",
1676
- message: `Message ${message.id.slice(0, 8)} failed: ${err instanceof Error ? err.message : String(err)}`,
2155
+ 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`,
1677
2156
  conversation_id: conv.id,
1678
2157
  message_id: message.id
1679
2158
  });
2159
+ continue;
1680
2160
  }
2161
+ this.dispatched.add(message.id);
2162
+ this.registerInFlight(conv, sessionId, message, opencodeMessageId);
2163
+ dispatched += 1;
2164
+ void this.postSignal(conv.id, message.id, "dispatched");
2165
+ }
2166
+ if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
2167
+ this.log({
2168
+ level: "error",
2169
+ 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).`,
2170
+ conversation_id: conv.id
2171
+ });
1681
2172
  }
1682
- return processed;
2173
+ this.ensureWatcherRunning(sessionId);
2174
+ return dispatched;
1683
2175
  }
1684
2176
  async ensureSession(conv) {
1685
- const cached = this.sessions.get(conv.id);
1686
- if (cached) return cached;
1687
- if (conv.opencode_session_id) {
1688
- this.sessions.set(conv.id, conv.opencode_session_id);
1689
- return conv.opencode_session_id;
2177
+ const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
2178
+ if (bound) {
2179
+ const exists = await sessionExists(this.port, bound);
2180
+ if (exists === false) {
2181
+ this.log({
2182
+ level: "info",
2183
+ 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.`,
2184
+ conversation_id: conv.id
2185
+ });
2186
+ this.sessions.delete(conv.id);
2187
+ return this.createAndBindSession(conv.id);
2188
+ }
2189
+ this.sessions.set(conv.id, bound);
2190
+ return bound;
1690
2191
  }
2192
+ return this.createAndBindSession(conv.id);
2193
+ }
2194
+ /**
2195
+ * Create a fresh OpenCode session for a conversation, cache the binding, and
2196
+ * best-effort persist it server-side. Shared by the first-ever bind and the
2197
+ * self-heal recreate path in `ensureSession`.
2198
+ */
2199
+ async createAndBindSession(conversationId) {
1691
2200
  const directory = await this.resolveOpenCodeDirectory();
1692
2201
  const sessionId = await createOpenCodeSession(this.port, directory);
1693
- this.sessions.set(conv.id, sessionId);
1694
- await this.persistSession(conv.id, sessionId).catch(() => {
2202
+ this.sessions.set(conversationId, sessionId);
2203
+ await this.persistSession(conversationId, sessionId).catch(() => {
1695
2204
  });
1696
2205
  return sessionId;
1697
2206
  }
@@ -1711,111 +2220,1004 @@ var ChannelDriver = class {
1711
2220
  }
1712
2221
  return this.opencodeDirectory;
1713
2222
  }
2223
+ // -------------------------------------------------------------------------
2224
+ // Per-session watcher (WI-3)
2225
+ // -------------------------------------------------------------------------
1714
2226
  /**
1715
- * Local reconcile: re-query `GET /session/:id/message` and check whether the
1716
- * last assistant message is message-level complete (`isTurnComplete`).
1717
- *
1718
- * This is a PURE OBSERVABILITY probe on the normal (non-paused) path: the
1719
- * blocking POST already returned, and `markDone` causes the server to re-fetch
1720
- * the messages itself (via `extractTextFromMessages`) when delivering the
1721
- * reply — so this round-trip never gates delivery. We keep it only to surface a
1722
- * truthful diagnostic when opencode hasn't yet recorded a completed assistant
1723
- * turn at reconcile time, then proceed to `markDone` regardless. Uses the
1724
- * injected `fetchImpl` and reuses only the pure `isTurnComplete` predicate.
2227
+ * Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
2228
+ * opencode session (Task 2.1a), so two dispatches into the SAME session can
2229
+ * never interleave and mis-correlate their read-backs. Distinct sessions run
2230
+ * concurrently. The chained tail intentionally ignores the prior result/error
2231
+ * (each dispatch reports its own outcome to its caller).
1725
2232
  */
1726
- async confirmCompletion(sessionId) {
1727
- try {
1728
- const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
1729
- if (!res.ok) return;
1730
- const body = await res.json();
1731
- const messages = Array.isArray(body) ? body : null;
1732
- if (!isTurnComplete(messages)) {
1733
- this.log({
1734
- level: "info",
1735
- message: `Session ${sessionId.slice(0, 8)} messages do not yet show a completed assistant turn on reconcile \u2014 delivering anyway`
1736
- });
1737
- }
1738
- } catch {
2233
+ dispatchLocked(sessionId, fn) {
2234
+ const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
2235
+ const run2 = prior.then(fn, fn);
2236
+ this.sessionDispatchLocks.set(
2237
+ sessionId,
2238
+ run2.then(
2239
+ () => void 0,
2240
+ () => void 0
2241
+ )
2242
+ );
2243
+ return run2;
2244
+ }
2245
+ /** Register a freshly-dispatched message with its session's watcher state. */
2246
+ registerInFlight(conv, sessionId, message, opencodeMessageId) {
2247
+ let watcher = this.watchers.get(sessionId);
2248
+ if (!watcher) {
2249
+ watcher = {
2250
+ conv,
2251
+ inFlight: /* @__PURE__ */ new Map(),
2252
+ loop: null,
2253
+ reportedQuestions: /* @__PURE__ */ new Set(),
2254
+ reportedPermissions: /* @__PURE__ */ new Set(),
2255
+ lastGoodPollAt: this.now(),
2256
+ hadUsablePoll: false
2257
+ };
2258
+ this.watchers.set(sessionId, watcher);
1739
2259
  }
2260
+ const now = this.now();
2261
+ watcher.inFlight.set(message.id, {
2262
+ evidentMessageId: message.id,
2263
+ opencodeMessageId,
2264
+ message,
2265
+ dispatchedAt: now,
2266
+ deadline: now + this.pausedMaxWaitMs,
2267
+ started: false,
2268
+ done: false,
2269
+ stuckReported: false,
2270
+ lastAliveAt: 0,
2271
+ aliveInFlight: false,
2272
+ awaitingHumanLatched: false,
2273
+ pausedOnQuestion: false,
2274
+ pausedOnPermission: false,
2275
+ pausedClearConfirmed: false,
2276
+ pausedInFlight: false,
2277
+ deliveryDeadlineAnchored: false
2278
+ });
1740
2279
  }
1741
- // -------------------------------------------------------------------------
1742
- // Paused-session watcher (WI-2-CLI)
1743
- // -------------------------------------------------------------------------
1744
2280
  /**
1745
- * Start (but do NOT await) a watcher that resumes a paused turn to completion.
2281
+ * Register a RE-ADOPTED `processing` message with its session watcher
2282
+ * (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
2283
+ * `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
2284
+ * `now`, so the paused/queued/unreachable cases settle on the same wall-clock a
2285
+ * fresh dispatch would (10 min after `processed_at`, not 10 min from now).
1746
2286
  *
1747
- * Single-flight per message: if a watcher is already live for this message we
1748
- * skip. The returned watcher promise is tracked in `this.watchers` and removed
1749
- * when it settles; it never rejects (the body is fully guarded), so a failed
1750
- * poll/markDone can never crash the run loop the cron stays as the safety net.
2287
+ * This re-attaches into the SAME watcher, so the ADR-0047 progressing-vs-paused
2288
+ * give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
2289
+ * opencode reports ACTIVELY `running` is watched to completion (its liveness
2290
+ * heartbeat keeps the cron off its row), while a re-adopted turn that is paused
2291
+ * awaiting a human — or queued/unreachable — is still bounded by `deadline` and
2292
+ * handed to the cron. The old "the `deadline` must settle before the ~15-min
2293
+ * cron or they double-drive" reasoning is superseded: liveness now settles the
2294
+ * actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
2295
+ * (only the appear-guard uses it).
2296
+ *
2297
+ * `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
2298
+ * `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
2299
+ * fresh-run path these differ (a fresh opencode id under the same server row).
2300
+ *
2301
+ * `started` is set true so the watcher does NOT re-`markProcessing` a row the
2302
+ * server already flipped to `processing`; the running/done transitions still
2303
+ * fire from the watcher's normal branches.
2304
+ */
2305
+ registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
2306
+ let watcher = this.watchers.get(sessionId);
2307
+ if (!watcher) {
2308
+ watcher = {
2309
+ conv,
2310
+ inFlight: /* @__PURE__ */ new Map(),
2311
+ loop: null,
2312
+ reportedQuestions: /* @__PURE__ */ new Set(),
2313
+ reportedPermissions: /* @__PURE__ */ new Set(),
2314
+ lastGoodPollAt: this.now(),
2315
+ hadUsablePoll: false
2316
+ };
2317
+ this.watchers.set(sessionId, watcher);
2318
+ }
2319
+ watcher.inFlight.set(message.id, {
2320
+ evidentMessageId: message.id,
2321
+ opencodeMessageId,
2322
+ message,
2323
+ dispatchedAt: this.now(),
2324
+ deadline: processedAtMs + this.pausedMaxWaitMs,
2325
+ // The server row is ALREADY `processing`; do not re-fire markProcessing.
2326
+ started: true,
2327
+ done: false,
2328
+ // Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
2329
+ // AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
2330
+ // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2331
+ // re-adopted row left wedged in `queued` still emits the signal once
2332
+ // (#210/#220 observability).
2333
+ stuckReported: false,
2334
+ // Task 5.2: a re-adopted actively-running row re-attaches into the SAME
2335
+ // watcher and so hits the SAME actively-running heartbeat branch in
2336
+ // `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
2337
+ // re-adopted and is confirming this row alive" via that `alive` heartbeat,
2338
+ // with no extra `re_adopted` signal needed (folds old WI-6).
2339
+ lastAliveAt: 0,
2340
+ aliveInFlight: false,
2341
+ awaitingHumanLatched: false,
2342
+ pausedOnQuestion: false,
2343
+ pausedOnPermission: false,
2344
+ pausedClearConfirmed: false,
2345
+ pausedInFlight: false,
2346
+ deliveryDeadlineAnchored: false
2347
+ });
2348
+ }
2349
+ /**
2350
+ * Start (but do NOT await) the per-session watcher loop if it has in-flight
2351
+ * work and is not already running. Single-flight per session. The loop is
2352
+ * tracked on the watcher and cleared when it settles; it never rejects (fully
2353
+ * guarded), so a failed poll/callback can never crash the run loop — the cron
2354
+ * stays as the safety net.
1751
2355
  */
1752
- startPausedWatcher(conv, message, sessionId) {
1753
- if (this.watchers.has(message.id)) return;
1754
- const watcher = this.watchPausedSession(conv, message, sessionId).finally(() => {
1755
- this.watchers.delete(message.id);
2356
+ ensureWatcherRunning(sessionId) {
2357
+ const watcher = this.watchers.get(sessionId);
2358
+ if (!watcher) return;
2359
+ if (watcher.loop) return;
2360
+ if (watcher.inFlight.size === 0) {
2361
+ this.watchers.delete(sessionId);
2362
+ return;
2363
+ }
2364
+ const loop = this.runWatcherLoop(sessionId, watcher).finally(() => {
2365
+ watcher.loop = null;
2366
+ if (watcher.inFlight.size === 0) {
2367
+ this.watchers.delete(sessionId);
2368
+ }
1756
2369
  });
1757
- this.watchers.set(message.id, watcher);
2370
+ watcher.loop = loop;
1758
2371
  }
1759
2372
  /**
1760
- * Poll `GET /session/:id/message` until the SAME paused turn is message-level
1761
- * complete the last message is an ASSISTANT message whose
1762
- * `info.time.completed` is set (the user answered the question/permission in
1763
- * opencode web and opencode finished the turn) then complete it via the
1764
- * EXISTING `markDone` PATCH — NO re-send of the original message. The server
1765
- * re-fetches the assistant messages on completion, so the watcher does not
1766
- * pass any reply text.
1767
- *
1768
- * Fetch seam: the poll uses the injected `this.fetchImpl` (preserving the
1769
- * tests' injection) and reuses only the pure `isTurnComplete` predicate from
1770
- * `session.ts` we do NOT call `getSessionMessages` (which uses the global
1771
- * `fetch`) here.
1772
- *
1773
- * Bounded by `pausedMaxWaitMs` (default 10 min, strictly < the 15-min cron
1774
- * reset): on timeout we STOP and leave the message `processing` so the cron
1775
- * remains the last-resort safety net. The whole body is wrapped so any
1776
- * poll/markDone failure is logged and swallowed — a watcher MUST NEVER throw
1777
- * out of the run loop.
2373
+ * The per-session polling loop (WI-3). Once per tick it:
2374
+ * 1. polls `GET /session/:id/message` once and, per in-flight message,
2375
+ * computes `messageRunState` and fires markProcessing (queued→running) /
2376
+ * markDone (done) exactly once per transition;
2377
+ * 2. applies the idle-path re-dispatch guard (a dispatched message that never
2378
+ * APPEARS re-dispatch D1 obligation 2);
2379
+ * 3. polls `/question` + `/permission` (scoped to the session) and surfaces
2380
+ * NEW ones via `reportInteraction`, carrying the PAUSED message's own
2381
+ * `source_message_id`;
2382
+ * 4. drops messages that completed or timed out from the in-flight set.
2383
+ * Exits when the in-flight set empties. Never throws.
1778
2384
  */
1779
- async watchPausedSession(conv, message, sessionId) {
1780
- const deadline = Date.now() + this.pausedMaxWaitMs;
2385
+ async runWatcherLoop(sessionId, watcher) {
1781
2386
  try {
1782
- while (Date.now() < deadline) {
2387
+ while (watcher.inFlight.size > 0) {
1783
2388
  await this.sleep(this.pausedPollIntervalMs);
1784
- let completed = false;
2389
+ let messages = null;
1785
2390
  try {
1786
2391
  const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
1787
2392
  if (res.ok) {
1788
2393
  const body = await res.json();
1789
- const messages = Array.isArray(body) ? body : null;
1790
- completed = isTurnComplete(messages);
2394
+ messages = Array.isArray(body) ? body : null;
1791
2395
  }
1792
2396
  } catch {
1793
- continue;
1794
2397
  }
1795
- if (!completed) continue;
2398
+ if (messages != null && messages.length > 0) {
2399
+ watcher.lastGoodPollAt = this.now();
2400
+ watcher.hadUsablePoll = true;
2401
+ } else {
2402
+ const emptyButReachable = messages != null;
2403
+ const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
2404
+ if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
2405
+ continue;
2406
+ }
2407
+ }
2408
+ const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
2409
+ for (const inFlight of [...watcher.inFlight.values()]) {
2410
+ await this.serviceInFlightMessage(
2411
+ sessionId,
2412
+ watcher,
2413
+ inFlight,
2414
+ messages,
2415
+ openQuestions,
2416
+ openPermissions,
2417
+ questionsPolledOk,
2418
+ permissionsPolledOk
2419
+ );
2420
+ }
2421
+ }
2422
+ } catch (err) {
2423
+ if (err instanceof ChannelAuthError) {
1796
2424
  this.log({
1797
- level: "info",
1798
- message: `Paused session ${sessionId.slice(0, 8)} completed \u2014 marking message ${message.id.slice(0, 8)} done`,
2425
+ level: "error",
2426
+ message: `Session watcher aborted on auth failure for session ${sessionId.slice(0, 8)} \u2014 clearing in-flight state for re-drive after re-auth: ${err.message}`,
2427
+ conversation_id: watcher.conv.id
2428
+ });
2429
+ for (const evidentMessageId of [...watcher.inFlight.keys()]) {
2430
+ this.readopted.delete(evidentMessageId);
2431
+ this.removeInFlight(watcher, evidentMessageId);
2432
+ }
2433
+ return;
2434
+ }
2435
+ this.log({
2436
+ level: "error",
2437
+ message: `Session watcher failed for session ${sessionId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
2438
+ conversation_id: watcher.conv.id
2439
+ });
2440
+ }
2441
+ }
2442
+ /**
2443
+ * On FIRST observing a terminal (done/failed) state, ensure the delivery
2444
+ * (markDone/markFailed) transient-retry path has a real window. A long
2445
+ * ACTIVELY-running turn is kept past its original `deadline`, so by completion
2446
+ * `now >= deadline` already holds and the retry bound below would fire on the
2447
+ * first transient PATCH failure — dropping the message before its reply lands
2448
+ * (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
2449
+ * to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
2450
+ * or past now, so a still-ample window is left untouched.
2451
+ */
2452
+ anchorDeliveryDeadline(inFlight) {
2453
+ if (inFlight.deliveryDeadlineAnchored) return;
2454
+ inFlight.deliveryDeadlineAnchored = true;
2455
+ if (this.now() >= inFlight.deadline) {
2456
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2457
+ }
2458
+ }
2459
+ /**
2460
+ * Drive ONE in-flight message's lifecycle from the tick's message snapshot.
2461
+ * Fires markProcessing on queued→running and markDone on done (each once),
2462
+ * applies the idle-path re-dispatch guard, and removes the message from the
2463
+ * in-flight set on completion or timeout.
2464
+ */
2465
+ async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
2466
+ const conv = watcher.conv;
2467
+ const state = messageRunState(messages, inFlight.opencodeMessageId);
2468
+ const id = inFlight.evidentMessageId;
2469
+ if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
2470
+ else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
2471
+ if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
2472
+ else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
2473
+ const observedOpen = openQuestions.has(id) || openPermissions.has(id);
2474
+ const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
2475
+ const awaitingHuman = observedOpen || latchedPaused;
2476
+ if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
2477
+ let claimed;
2478
+ try {
2479
+ claimed = await this.markProcessing(
2480
+ conv.id,
2481
+ inFlight.evidentMessageId,
2482
+ sessionId,
2483
+ inFlight.opencodeMessageId
2484
+ );
2485
+ } catch (err) {
2486
+ if (err instanceof ChannelAuthError) throw err;
2487
+ this.log({
2488
+ level: "error",
2489
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
1799
2490
  conversation_id: conv.id,
1800
- message_id: message.id
2491
+ message_id: inFlight.evidentMessageId
1801
2492
  });
1802
- await this.markDone(conv.id, message.id, sessionId);
1803
2493
  return;
1804
2494
  }
2495
+ inFlight.started = true;
2496
+ if (!claimed) {
2497
+ this.log({
2498
+ level: "info",
2499
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
2500
+ conversation_id: conv.id,
2501
+ message_id: inFlight.evidentMessageId
2502
+ });
2503
+ }
2504
+ }
2505
+ if (state === "done") {
2506
+ this.anchorDeliveryDeadline(inFlight);
2507
+ if (!inFlight.done) {
2508
+ this.log({
2509
+ level: "info",
2510
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
2511
+ conversation_id: conv.id,
2512
+ message_id: inFlight.evidentMessageId
2513
+ });
2514
+ try {
2515
+ await this.markDone(
2516
+ conv.id,
2517
+ inFlight.evidentMessageId,
2518
+ sessionId,
2519
+ inFlight.opencodeMessageId
2520
+ );
2521
+ } catch (err) {
2522
+ if (err instanceof ChannelAuthError) throw err;
2523
+ if (err instanceof ChannelTerminalError) {
2524
+ this.log({
2525
+ level: "error",
2526
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2527
+ conversation_id: conv.id,
2528
+ message_id: inFlight.evidentMessageId
2529
+ });
2530
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2531
+ return;
2532
+ }
2533
+ if (this.now() >= inFlight.deadline) {
2534
+ this.log({
2535
+ level: "error",
2536
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done within the watch window \u2014 leaving for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
2537
+ conversation_id: conv.id,
2538
+ message_id: inFlight.evidentMessageId
2539
+ });
2540
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2541
+ return;
2542
+ }
2543
+ this.log({
2544
+ level: "error",
2545
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2546
+ conversation_id: conv.id,
2547
+ message_id: inFlight.evidentMessageId
2548
+ });
2549
+ return;
2550
+ }
2551
+ inFlight.done = true;
2552
+ }
2553
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2554
+ return;
2555
+ }
2556
+ if (state === "failed") {
2557
+ this.anchorDeliveryDeadline(inFlight);
2558
+ if (!inFlight.done) {
2559
+ const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
2560
+ this.log({
2561
+ level: "error",
2562
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2563
+ conversation_id: conv.id,
2564
+ message_id: inFlight.evidentMessageId
2565
+ });
2566
+ try {
2567
+ await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
2568
+ } catch (err) {
2569
+ if (err instanceof ChannelAuthError) throw err;
2570
+ if (err instanceof ChannelTerminalError) {
2571
+ this.log({
2572
+ level: "error",
2573
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2574
+ conversation_id: conv.id,
2575
+ message_id: inFlight.evidentMessageId
2576
+ });
2577
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2578
+ return;
2579
+ }
2580
+ if (this.now() >= inFlight.deadline) {
2581
+ this.log({
2582
+ level: "error",
2583
+ 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)}`,
2584
+ conversation_id: conv.id,
2585
+ message_id: inFlight.evidentMessageId
2586
+ });
2587
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2588
+ return;
2589
+ }
2590
+ this.log({
2591
+ level: "error",
2592
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2593
+ conversation_id: conv.id,
2594
+ message_id: inFlight.evidentMessageId
2595
+ });
2596
+ return;
2597
+ }
2598
+ inFlight.done = true;
2599
+ }
2600
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2601
+ return;
2602
+ }
2603
+ const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
2604
+ const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
2605
+ if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
2606
+ inFlight.stuckReported = true;
2607
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2608
+ stuck_for_ms: this.now() - inFlight.dispatchedAt
2609
+ });
2610
+ }
2611
+ const activelyRunning = state === "running" && !awaitingHuman;
2612
+ if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
2613
+ inFlight.aliveInFlight = true;
2614
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
2615
+ inFlight.aliveInFlight = false;
2616
+ if (ok) inFlight.lastAliveAt = this.now();
2617
+ });
2618
+ }
2619
+ if (awaitingHuman) {
2620
+ if (!inFlight.awaitingHumanLatched) {
2621
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2622
+ inFlight.awaitingHumanLatched = true;
2623
+ }
2624
+ if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
2625
+ inFlight.pausedInFlight = true;
2626
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
2627
+ inFlight.pausedInFlight = false;
2628
+ if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
2629
+ });
2630
+ }
2631
+ } else if (inFlight.awaitingHumanLatched) {
2632
+ inFlight.awaitingHumanLatched = false;
2633
+ inFlight.pausedOnQuestion = false;
2634
+ inFlight.pausedOnPermission = false;
2635
+ inFlight.pausedClearConfirmed = false;
2636
+ }
2637
+ const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
2638
+ const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
2639
+ (sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
2640
+ );
2641
+ const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
2642
+ if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
1805
2643
  this.log({
1806
2644
  level: "info",
1807
- message: `Paused session ${sessionId.slice(0, 8)} did not complete within the watch window \u2014 leaving message ${message.id.slice(0, 8)} for the cron safety net`,
2645
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
1808
2646
  conversation_id: conv.id,
1809
- message_id: message.id
2647
+ message_id: inFlight.evidentMessageId
2648
+ });
2649
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2650
+ watched_for_ms: this.now() - inFlight.dispatchedAt
1810
2651
  });
2652
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2653
+ }
2654
+ }
2655
+ // -------------------------------------------------------------------------
2656
+ // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
2657
+ // -------------------------------------------------------------------------
2658
+ /**
2659
+ * Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
2660
+ *
2661
+ * The pending drain only re-drives `pending` rows; a message already flipped to
2662
+ * `processing` before the runner died is watched by nobody until the 15-min
2663
+ * cron resets it. Here we fetch those rows, and per row resolve its correlated
2664
+ * reply against opencode's OWN session store — completing, re-attaching, or
2665
+ * (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
2666
+ * is idempotent per message (Invariant 2): a row a watcher already tracks is
2667
+ * skipped in `readoptOne` — one driver, no double-drive.
2668
+ *
2669
+ * Only `ChannelAuthError` propagates (to `drainPending`, like the pending
2670
+ * path); every other early return LOGS a reason with context — no silent drop.
2671
+ */
2672
+ async readoptProcessing() {
2673
+ const rows = await this.getProcessingMessages();
2674
+ if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
2675
+ const stillProcessing = new Set(rows.map((r) => r.id));
2676
+ for (const id of [...this.dontRedispatch, ...this.doneUndeliverable]) {
2677
+ if (!stillProcessing.has(id)) {
2678
+ const cleared = this.dontRedispatch.delete(id);
2679
+ const clearedUndeliverable = this.doneUndeliverable.delete(id);
2680
+ if (cleared || clearedUndeliverable) {
2681
+ this.log({
2682
+ level: "info",
2683
+ message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
2684
+ message_id: id
2685
+ });
2686
+ }
2687
+ }
2688
+ }
2689
+ }
2690
+ if (rows.length === 0) return;
2691
+ const bySession = /* @__PURE__ */ new Map();
2692
+ for (const row of rows) {
2693
+ if (!row.opencode_session_id) {
2694
+ this.log({
2695
+ level: "error",
2696
+ message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
2697
+ conversation_id: row.conversation_id,
2698
+ message_id: row.id
2699
+ });
2700
+ continue;
2701
+ }
2702
+ const list = bySession.get(row.opencode_session_id) ?? [];
2703
+ list.push(row);
2704
+ bySession.set(row.opencode_session_id, list);
2705
+ }
2706
+ for (const [sessionId, sessionRows] of bySession) {
2707
+ let messages;
2708
+ try {
2709
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
2710
+ if (!res.ok) {
2711
+ this.log({
2712
+ level: "error",
2713
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
2714
+ });
2715
+ continue;
2716
+ }
2717
+ const body = await res.json();
2718
+ if (!Array.isArray(body)) {
2719
+ this.log({
2720
+ level: "error",
2721
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
2722
+ });
2723
+ continue;
2724
+ }
2725
+ messages = body;
2726
+ } catch (err) {
2727
+ this.log({
2728
+ level: "error",
2729
+ message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
2730
+ });
2731
+ continue;
2732
+ }
2733
+ for (const row of sessionRows) {
2734
+ await this.readoptOne(sessionId, row, messages);
2735
+ }
2736
+ }
2737
+ }
2738
+ /**
2739
+ * Re-adopt ONE `processing` row against the tick's session message snapshot
2740
+ * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
2741
+ *
2742
+ * Branches on `messageRunState(messages, row.opencode_message_id)` — the
2743
+ * opencode-assigned user-message id persisted on the first `processing` PATCH
2744
+ * (#218). A row with a NULL stored id (dispatched but the read-back never landed
2745
+ * before the restart) has no id to correlate → treated as an orphan and
2746
+ * re-dispatched (at most once, see `forceReadoptRun`):
2747
+ * - `done` → `markDone` now (guarded like the watcher's done branch);
2748
+ * - `failed` → `markFailed` with the surfaced error (issue #182), so an
2749
+ * errored turn is reported failed on restart, NOT re-dispatched;
2750
+ * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
2751
+ * tracking the stored id so the reply correlates by it;
2752
+ * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
2753
+ *
2754
+ * Only `ChannelAuthError` propagates.
2755
+ */
2756
+ async readoptOne(sessionId, row, messages) {
2757
+ if (this.isTracked(sessionId, row.id)) {
2758
+ this.log({
2759
+ level: "info",
2760
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
2761
+ conversation_id: row.conversation_id,
2762
+ message_id: row.id
2763
+ });
2764
+ return;
2765
+ }
2766
+ const ocId = row.opencode_message_id;
2767
+ const state = messageRunState(messages, ocId ?? "");
2768
+ if (state === "done") {
2769
+ if (this.doneUndeliverable.has(row.id)) {
2770
+ this.log({
2771
+ level: "info",
2772
+ message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
2773
+ conversation_id: row.conversation_id,
2774
+ message_id: row.id
2775
+ });
2776
+ return;
2777
+ }
2778
+ this.log({
2779
+ level: "info",
2780
+ message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
2781
+ conversation_id: row.conversation_id,
2782
+ message_id: row.id
2783
+ });
2784
+ try {
2785
+ await this.markDone(row.conversation_id, row.id, sessionId, ocId);
2786
+ } catch (err) {
2787
+ if (err instanceof ChannelAuthError) throw err;
2788
+ if (err instanceof ChannelTerminalError) {
2789
+ this.doneUndeliverable.add(row.id);
2790
+ this.log({
2791
+ level: "error",
2792
+ 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}`,
2793
+ conversation_id: row.conversation_id,
2794
+ message_id: row.id
2795
+ });
2796
+ return;
2797
+ }
2798
+ this.log({
2799
+ level: "error",
2800
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2801
+ conversation_id: row.conversation_id,
2802
+ message_id: row.id
2803
+ });
2804
+ return;
2805
+ }
2806
+ this.dontRedispatch.delete(row.id);
2807
+ return;
2808
+ }
2809
+ if (state === "failed") {
2810
+ const error2 = messageError(messages, ocId ?? "") ?? void 0;
2811
+ this.log({
2812
+ level: "error",
2813
+ message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2814
+ conversation_id: row.conversation_id,
2815
+ message_id: row.id
2816
+ });
2817
+ try {
2818
+ await this.markFailed(row.conversation_id, row.id, sessionId, error2);
2819
+ } catch (err) {
2820
+ if (err instanceof ChannelAuthError) throw err;
2821
+ if (err instanceof ChannelTerminalError) {
2822
+ this.doneUndeliverable.add(row.id);
2823
+ this.log({
2824
+ level: "error",
2825
+ 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}`,
2826
+ conversation_id: row.conversation_id,
2827
+ message_id: row.id
2828
+ });
2829
+ return;
2830
+ }
2831
+ this.log({
2832
+ level: "error",
2833
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2834
+ conversation_id: row.conversation_id,
2835
+ message_id: row.id
2836
+ });
2837
+ return;
2838
+ }
2839
+ this.dontRedispatch.delete(row.id);
2840
+ return;
2841
+ }
2842
+ if (this.dontRedispatch.has(row.id)) {
2843
+ this.log({
2844
+ level: "info",
2845
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
2846
+ conversation_id: row.conversation_id,
2847
+ message_id: row.id
2848
+ });
2849
+ return;
2850
+ }
2851
+ if ((state === "running" || state === "queued") && ocId) {
2852
+ const conv = this.convForRow(sessionId, row);
2853
+ const message = this.queuedMessageForRow(row);
2854
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
2855
+ this.dispatched.add(row.id);
2856
+ this.readopted.add(row.id);
2857
+ this.ensureWatcherRunning(sessionId);
2858
+ this.log({
2859
+ level: "info",
2860
+ message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
2861
+ conversation_id: row.conversation_id,
2862
+ message_id: row.id
2863
+ });
2864
+ return;
2865
+ }
2866
+ await this.forceReadoptRun(sessionId, row);
2867
+ }
2868
+ /**
2869
+ * Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
2870
+ *
2871
+ * #218/WI-5: the row's user message is absent (never kept, or a null stored id),
2872
+ * so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
2873
+ * read it back, and register the watcher under the assigned id so the reply
2874
+ * correlates server-side.
2875
+ *
2876
+ * ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
2877
+ * id). Without a guard, if this dispatches on tick N but the read-back+persist
2878
+ * hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
2879
+ * tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
2880
+ * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
2881
+ * short-circuit while the row is latched; clear it on a successful dispatch (the
2882
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
2883
+ * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
2884
+ * may retry exactly once more).
2885
+ *
2886
+ * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
2887
+ * `processed_at` (Invariant 1).
2888
+ */
2889
+ async forceReadoptRun(sessionId, row) {
2890
+ if (this.stopped) {
2891
+ this.log({
2892
+ level: "info",
2893
+ 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`,
2894
+ conversation_id: row.conversation_id,
2895
+ message_id: row.id
2896
+ });
2897
+ return;
2898
+ }
2899
+ if (this.awaitingReadopt.has(row.id)) {
2900
+ this.log({
2901
+ level: "info",
2902
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
2903
+ conversation_id: row.conversation_id,
2904
+ message_id: row.id
2905
+ });
2906
+ return;
2907
+ }
2908
+ if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
2909
+ this.dontRedispatch.add(row.id);
2910
+ this.log({
2911
+ level: "info",
2912
+ 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)`,
2913
+ conversation_id: row.conversation_id,
2914
+ message_id: row.id
2915
+ });
2916
+ return;
2917
+ }
2918
+ const options = {
2919
+ agent: row.opencode_agent ?? void 0,
2920
+ model: row.opencode_model ?? void 0
2921
+ };
2922
+ this.log({
2923
+ level: "info",
2924
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
2925
+ conversation_id: row.conversation_id,
2926
+ message_id: row.id
2927
+ });
2928
+ this.awaitingReadopt.add(row.id);
2929
+ let ocId;
2930
+ try {
2931
+ ocId = await this.dispatchLocked(
2932
+ sessionId,
2933
+ () => sendPromptAsync(this.port, sessionId, row.content, options)
2934
+ );
1811
2935
  } catch (err) {
2936
+ this.awaitingReadopt.delete(row.id);
2937
+ if (err instanceof ChannelAuthError) throw err;
1812
2938
  this.log({
1813
2939
  level: "error",
1814
- message: `Paused-session watcher failed for message ${message.id.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
1815
- conversation_id: conv.id,
1816
- message_id: message.id
2940
+ message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2941
+ conversation_id: row.conversation_id,
2942
+ message_id: row.id
2943
+ });
2944
+ return;
2945
+ }
2946
+ if (ocId === null) {
2947
+ this.awaitingReadopt.delete(row.id);
2948
+ this.log({
2949
+ level: "error",
2950
+ 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`,
2951
+ conversation_id: row.conversation_id,
2952
+ message_id: row.id
2953
+ });
2954
+ return;
2955
+ }
2956
+ const conv = this.convForRow(sessionId, row);
2957
+ const message = this.queuedMessageForRow(row);
2958
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
2959
+ this.dispatched.add(row.id);
2960
+ this.readopted.add(row.id);
2961
+ this.awaitingReadopt.delete(row.id);
2962
+ this.ensureWatcherRunning(sessionId);
2963
+ }
2964
+ /**
2965
+ * True if `evidentMessageId` is already being driven — either in the
2966
+ * authoritative `dispatched` set or a live watcher's in-flight set for this
2967
+ * session (Invariant 2, WI-5). Either signal means a watcher owns the row.
2968
+ */
2969
+ isTracked(sessionId, evidentMessageId) {
2970
+ if (this.dispatched.has(evidentMessageId)) return true;
2971
+ const watcher = this.watchers.get(sessionId);
2972
+ return watcher?.inFlight.has(evidentMessageId) ?? false;
2973
+ }
2974
+ /**
2975
+ * Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
2976
+ * deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
2977
+ * for `processing` rows, but if it is somehow null/unparseable fall back to
2978
+ * `now` (defensive) AND log — a fallback means the anchor is weaker than
2979
+ * intended, which is worth surfacing.
2980
+ */
2981
+ processedAtMs(row) {
2982
+ const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
2983
+ if (!Number.isNaN(parsed)) return parsed;
2984
+ this.log({
2985
+ level: "error",
2986
+ message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
2987
+ conversation_id: row.conversation_id,
2988
+ message_id: row.id
2989
+ });
2990
+ return this.now();
2991
+ }
2992
+ /** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
2993
+ convForRow(sessionId, row) {
2994
+ return {
2995
+ id: row.conversation_id,
2996
+ agent_id: this.agentId,
2997
+ opencode_session_id: sessionId,
2998
+ pending_message_count: 0,
2999
+ oldest_pending_at: row.processed_at
3000
+ };
3001
+ }
3002
+ /** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
3003
+ queuedMessageForRow(row) {
3004
+ return {
3005
+ id: row.id,
3006
+ content: row.content,
3007
+ status: "processing",
3008
+ opencode_agent: row.opencode_agent,
3009
+ opencode_model: row.opencode_model,
3010
+ source_message_id: row.source_message_id,
3011
+ slack_user_id: row.slack_user_id
3012
+ };
3013
+ }
3014
+ /**
3015
+ * Remove a message from the in-flight set AND the authoritative dispatched
3016
+ * set. Once the in-flight set empties, the watcher loop's `while` guard exits
3017
+ * and its `.finally` removes the session entry from `this.watchers`.
3018
+ *
3019
+ * Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
3020
+ * (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
3021
+ * cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
3022
+ * re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
3023
+ * re-adopted message that completed (`done`) needs no marker — it's leaving
3024
+ * `processing`. This suppresses only re-dispatch: if its reply later completes,
3025
+ * the done branch still delivers it (Bugbot #202).
3026
+ */
3027
+ removeInFlight(watcher, evidentMessageId) {
3028
+ const inFlight = watcher.inFlight.get(evidentMessageId);
3029
+ if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
3030
+ this.dontRedispatch.add(evidentMessageId);
3031
+ this.log({
3032
+ level: "info",
3033
+ message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
3034
+ conversation_id: watcher.conv.id,
3035
+ message_id: evidentMessageId
1817
3036
  });
1818
3037
  }
3038
+ watcher.inFlight.delete(evidentMessageId);
3039
+ this.dispatched.delete(evidentMessageId);
3040
+ }
3041
+ /**
3042
+ * Poll `/question` + `/permission` (scoped to the session) and surface NEW ones
3043
+ * via `reportInteraction` (Task 3.5), carrying the PAUSED message's own
3044
+ * `source_message_id` so the server @mentions the correct person under
3045
+ * concurrency. Dedups by interaction id across ticks (reused per-session sets).
3046
+ *
3047
+ * The interaction is attributed to the in-flight message it paused on. opencode
3048
+ * stamps a `messageID` on a permission (and `tool.messageID` on a question) =
3049
+ * the assistant message id, whose `parentID` is the user message id — but the
3050
+ * simplest robust attribution here is: the single in-flight message that is
3051
+ * RUNNING (not done) is the one that paused. With one running message that is
3052
+ * unambiguous; with several we prefer an explicit messageID match, else the
3053
+ * oldest running message.
3054
+ *
3055
+ * Returns the set of in-flight Evident message ids that are paused awaiting a
3056
+ * human — an outstanding (still-open) question/permission is attributed to them.
3057
+ * `serviceInFlightMessage` uses this to keep an actively-running turn watched
3058
+ * forever (ADR-0047) while still bounding a turn merely blocked on a person who
3059
+ * may never answer. Attribution here covers ALL open interactions, not just
3060
+ * NEW (un-deduped) ones — a question stays "awaiting a human" until answered,
3061
+ * even after it was already surfaced to the channel.
3062
+ */
3063
+ async pollInteractions(sessionId, watcher, messages) {
3064
+ const openQuestions = /* @__PURE__ */ new Set();
3065
+ const openPermissions = /* @__PURE__ */ new Set();
3066
+ let questionsPolledOk = true;
3067
+ let permissionsPolledOk = true;
3068
+ let questions = [];
3069
+ try {
3070
+ const res = await this.fetchImpl(`${this.opencodeBase}/question`);
3071
+ if (res.ok) {
3072
+ const body = await res.json();
3073
+ if (Array.isArray(body)) {
3074
+ questions = body;
3075
+ } else {
3076
+ questionsPolledOk = false;
3077
+ }
3078
+ } else {
3079
+ questionsPolledOk = false;
3080
+ }
3081
+ } catch {
3082
+ questionsPolledOk = false;
3083
+ }
3084
+ for (const q of questions) {
3085
+ if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
3086
+ const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
3087
+ if (paused) openQuestions.add(paused.evidentMessageId);
3088
+ if (watcher.reportedQuestions.has(q.id)) continue;
3089
+ const reported = await this.reportInteraction(
3090
+ watcher.conv.id,
3091
+ "question",
3092
+ q,
3093
+ paused?.message.source_message_id ?? void 0
3094
+ );
3095
+ if (reported) watcher.reportedQuestions.add(q.id);
3096
+ }
3097
+ let permissions = [];
3098
+ try {
3099
+ const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
3100
+ if (res.ok) {
3101
+ const body = await res.json();
3102
+ if (Array.isArray(body)) {
3103
+ permissions = body;
3104
+ } else {
3105
+ permissionsPolledOk = false;
3106
+ }
3107
+ } else {
3108
+ permissionsPolledOk = false;
3109
+ }
3110
+ } catch {
3111
+ permissionsPolledOk = false;
3112
+ }
3113
+ for (const p of permissions) {
3114
+ if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
3115
+ const paused = this.attributeInteraction(watcher, p.messageID, messages);
3116
+ if (paused) openPermissions.add(paused.evidentMessageId);
3117
+ if (watcher.reportedPermissions.has(p.id)) continue;
3118
+ const reported = await this.reportInteraction(
3119
+ watcher.conv.id,
3120
+ "permission",
3121
+ p,
3122
+ paused?.message.source_message_id ?? void 0
3123
+ );
3124
+ if (reported) watcher.reportedPermissions.add(p.id);
3125
+ }
3126
+ return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
3127
+ }
3128
+ /**
3129
+ * True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
3130
+ * i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
3131
+ * watched root. Sub-agents spawned via the `task` tool run in child sessions,
3132
+ * so their questions/permissions live under a different `sessionID` that must
3133
+ * still be attributed to the root conversation the watcher owns.
3134
+ *
3135
+ * Parents are cached in `sessionParents` so we walk each session at most once;
3136
+ * a bounded depth cap guards against a cycle or a pathological chain, and any
3137
+ * fetch failure is treated as "not a descendant" (best-effort — the interaction
3138
+ * simply isn't surfaced this tick and is retried next tick once resolvable).
3139
+ */
3140
+ async sessionBelongsTo(sessionId, rootSessionId) {
3141
+ let current = sessionId;
3142
+ for (let depth = 0; current && depth < 32; depth++) {
3143
+ if (current === rootSessionId) return true;
3144
+ const parent = await this.resolveSessionParent(current);
3145
+ if (parent === null || parent === void 0) return false;
3146
+ current = parent;
3147
+ }
3148
+ return false;
3149
+ }
3150
+ /**
3151
+ * Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
3152
+ * `null` for a root session (no parent) and `undefined` when opencode is
3153
+ * unreachable / the session can't be read (so the caller stops walking without
3154
+ * caching a wrong answer — the next tick retries).
3155
+ */
3156
+ async resolveSessionParent(sessionId) {
3157
+ const cached = this.sessionParents.get(sessionId);
3158
+ if (cached !== void 0) return cached;
3159
+ let parent = void 0;
3160
+ try {
3161
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
3162
+ if (res.ok) {
3163
+ const body = await res.json();
3164
+ parent = body && typeof body.parentID === "string" ? body.parentID : null;
3165
+ }
3166
+ } catch {
3167
+ parent = void 0;
3168
+ }
3169
+ if (parent !== void 0) this.sessionParents.set(sessionId, parent);
3170
+ return parent;
3171
+ }
3172
+ /**
3173
+ * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
3174
+ *
3175
+ * The interaction carries `interactionMessageId` — the ASSISTANT message id
3176
+ * that raised it (a question's `tool.messageID` / a permission's `messageID`).
3177
+ * That assistant message is the reply to ONE of our minted user messages
3178
+ * (correlated by `parentID`, GATE-B). So when we have the tick's message
3179
+ * snapshot, we resolve each running in-flight message's correlated assistant
3180
+ * reply (`findAssistantReplyAfter`) and match its id against
3181
+ * `interactionMessageId` — giving an EXACT attribution even with several
3182
+ * messages in flight concurrently in one session.
3183
+ *
3184
+ * We fall back to the oldest running message ONLY when no exact match is
3185
+ * possible (the id is absent, the snapshot is missing, or the reply has not yet
3186
+ * been correlated). With a single running message either path is exact. Never
3187
+ * throws.
3188
+ *
3189
+ * Attribution must NOT depend on our own `started` PATCH flag: opencode can
3190
+ * START a turn AND raise a question/permission BEFORE our next tick fires
3191
+ * `markProcessing` (which sets `started`). Relying on `started` would leave the
3192
+ * running set empty in that window and let the server fall back to "newest
3193
+ * processing/pending" — possibly @mentioning a FOLLOW-UP author rather than the
3194
+ * person whose active turn actually paused. So we derive "running" from the
3195
+ * tick's `messages` snapshot via `messageRunState` instead.
3196
+ */
3197
+ attributeInteraction(watcher, interactionMessageId, messages) {
3198
+ const inFlight = [...watcher.inFlight.values()].filter((m) => !m.done);
3199
+ if (inFlight.length === 0) return void 0;
3200
+ if (interactionMessageId && messages) {
3201
+ const exact = inFlight.find((m) => {
3202
+ const reply = findAssistantReplyAfter(messages, m.opencodeMessageId);
3203
+ return reply != null && messageIdOf(reply) === interactionMessageId;
3204
+ });
3205
+ if (exact) return exact;
3206
+ }
3207
+ const byOldest = (a, b) => a.dispatchedAt - b.dispatchedAt;
3208
+ if (messages) {
3209
+ const runningPerSnapshot = inFlight.filter(
3210
+ (m) => messageRunState(messages, m.opencodeMessageId) === "running"
3211
+ );
3212
+ if (runningPerSnapshot.length > 0) {
3213
+ return runningPerSnapshot.sort(byOldest)[0];
3214
+ }
3215
+ }
3216
+ const startedRunning = inFlight.filter((m) => m.started);
3217
+ if (startedRunning.length > 0) {
3218
+ return startedRunning.sort(byOldest)[0];
3219
+ }
3220
+ return inFlight.sort(byOldest)[0];
1819
3221
  }
1820
3222
  // -------------------------------------------------------------------------
1821
3223
  // Evident API calls (combinedAuth thread routes)
@@ -1849,49 +3251,191 @@ var ChannelDriver = class {
1849
3251
  }
1850
3252
  return await res.json();
1851
3253
  }
1852
- async markProcessing(conversationId, messageId) {
3254
+ /**
3255
+ * Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
3256
+ * The pending path (`getPendingConversations`/`getPendingMessages`) only
3257
+ * surfaces `pending` rows, so a message already `processing` when the runner
3258
+ * died is invisible to it — this dedicated endpoint returns exactly those rows
3259
+ * with the fields the re-adopt path needs (`processed_at`,
3260
+ * `opencode_session_id`, routing).
3261
+ *
3262
+ * Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
3263
+ * array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
3264
+ * other non-ok so `drainPending`'s try/finally leaves `draining` false and the
3265
+ * next tick retries.
3266
+ */
3267
+ async getProcessingMessages() {
3268
+ const res = await this.fetchImpl(
3269
+ `${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
3270
+ { headers: { Authorization: this.getAuthHeader() } }
3271
+ );
3272
+ this.assertAuth(res, "fetching processing messages");
3273
+ if (!res.ok) {
3274
+ throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
3275
+ }
3276
+ const data = await res.json();
3277
+ let messages = data.messages ?? [];
3278
+ if (this.conversationFilter) {
3279
+ messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
3280
+ }
3281
+ return messages;
3282
+ }
3283
+ /**
3284
+ * EXISTING combinedAuth route — now fired by the watcher on queued→running
3285
+ * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
3286
+ * opencode_session_id}` → `notifyMessageStarted` (hourglass→runner swap +
3287
+ * deep-linked "View in Evident" notice).
3288
+ *
3289
+ * Return/throw contract (consumed by the watcher's swap-to-running guard):
3290
+ * - returns `true` → the server transitioned the row to processing;
3291
+ * - returns `false` → the server gave a DEFINITIVE "already-processing"
3292
+ * answer (a non-retryable, non-auth status — e.g. a
3293
+ * conflict because a duplicate already transitioned it),
3294
+ * so the caller treats it as already-started and does NOT
3295
+ * retry;
3296
+ * - throws `ChannelAuthError` on 401/403 (terminal auth failure);
3297
+ * - throws on a TRANSIENT failure (retryable 5xx/429 status, or a
3298
+ * network-level error from `fetch`) — i.e. NO definitive server response —
3299
+ * so the caller leaves the message un-started and retries the swap on the
3300
+ * next tick.
3301
+ * A single attempt (no internal retry): the watcher's per-tick loop is the
3302
+ * retry vehicle for the swap-to-running.
3303
+ */
3304
+ async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
1853
3305
  const res = await this.fetchImpl(
1854
3306
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1855
3307
  {
1856
3308
  method: "PATCH",
1857
3309
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1858
- body: JSON.stringify({ status: "processing" })
3310
+ body: JSON.stringify({
3311
+ status: "processing",
3312
+ opencode_session_id: sessionId,
3313
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
3314
+ })
1859
3315
  }
1860
3316
  );
1861
3317
  this.assertAuth(res, "marking message as processing");
1862
- return res.ok;
3318
+ if (res.ok) return true;
3319
+ if (isRetryableStatus(res.status)) {
3320
+ throw new Error(`marking message as processing: HTTP ${res.status}`);
3321
+ }
3322
+ return false;
1863
3323
  }
1864
3324
  /**
1865
- * EXISTING combinedAuth completion route — idempotent + retried (WI-CHAN-2).
1866
- * `PATCH .../messages/:id {status:'done', opencode_session_id}`. The server's
3325
+ * EXISTING combinedAuth completion route — idempotent (WI-CHAN-2). `PATCH
3326
+ * .../messages/:id {status:'done', opencode_session_id}`. The server's
1867
3327
  * `queued_conversation_messages.status`/`processed_at` gate makes a re-call
1868
- * for an already-`done` message a no-op (no double Slack post).
3328
+ * for an already-`done` message a no-op (no double Slack post). Fired by the
3329
+ * watcher on per-message completion (Task 3.4) — no `confirmCompletion`
3330
+ * round-trip (we already observed completion via the message list).
3331
+ *
3332
+ * SINGLE ATTEMPT (no in-call `callWithRetry` backoff). The per-session watcher
3333
+ * services its in-flight messages SEQUENTIALLY within a tick
3334
+ * (`runWatcherLoop` → `serviceInFlightMessage`), so a long multi-attempt
3335
+ * backoff here would BLOCK sibling messages in the SAME session/tick: while
3336
+ * message A's done PATCH burned its internal retries, message B could not be
3337
+ * swapped to running even though opencode had already started it. Instead this
3338
+ * does ONE PATCH and surfaces the SAME outcome contract the watcher's markDone
3339
+ * handler already relies on, leaning on the per-tick retry across ticks
3340
+ * (bounded by `inFlight.deadline`) rather than an in-call retry:
3341
+ * - resolves (`void`) → the server transitioned the row to done
3342
+ * (or idempotently confirmed already-done);
3343
+ * - throws `ChannelAuthError` → 401/403 (terminal auth failure → loop
3344
+ * cleanup, Finding 1);
3345
+ * - throws `ChannelTerminalError`→ non-retryable, non-auth 4xx (will never
3346
+ * succeed → straight to the cron, Finding 4);
3347
+ * - throws a plain `Error` → TRANSIENT 5xx/429 or a network-level error
3348
+ * (no definitive server response → the
3349
+ * watcher retries next tick within the
3350
+ * deadline, Finding 4).
3351
+ */
3352
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
3353
+ const res = await this.fetchImpl(
3354
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3355
+ {
3356
+ method: "PATCH",
3357
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
3358
+ body: JSON.stringify({
3359
+ status: "done",
3360
+ opencode_session_id: sessionId,
3361
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
3362
+ })
3363
+ }
3364
+ );
3365
+ this.assertAuth(res, "marking message as done");
3366
+ if (res.ok) return;
3367
+ if (isRetryableStatus(res.status)) {
3368
+ throw new Error(`marking message as done: HTTP ${res.status}`);
3369
+ }
3370
+ throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
3371
+ }
3372
+ /**
3373
+ * Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
3374
+ * when provided (issue #182): a bare `markFailed(conv, msg)` sends
3375
+ * `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
3376
+ * OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
3377
+ * failure reason reaches the channel.
1869
3378
  */
1870
- async markDone(conversationId, messageId, sessionId) {
3379
+ async markFailed(conversationId, messageId, sessionId, error2) {
3380
+ const body = { status: "failed" };
3381
+ if (sessionId !== void 0) body.opencode_session_id = sessionId;
3382
+ if (error2 !== void 0) body.error = error2;
1871
3383
  await this.callWithRetry(
1872
- "marking message as done",
3384
+ "marking message as failed",
1873
3385
  () => this.fetchImpl(
1874
3386
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1875
3387
  {
1876
3388
  method: "PATCH",
1877
3389
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1878
- body: JSON.stringify({ status: "done", opencode_session_id: sessionId })
3390
+ body: JSON.stringify(body)
1879
3391
  }
1880
3392
  )
1881
3393
  );
1882
3394
  }
1883
- async markFailed(conversationId, messageId) {
1884
- await this.callWithRetry(
1885
- "marking message as failed",
1886
- () => this.fetchImpl(
1887
- `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3395
+ /**
3396
+ * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
3397
+ * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
3398
+ * — the server records it via `log()` (no DB write, no notification). This is
3399
+ * fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
3400
+ * MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
3401
+ * watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
3402
+ * context (no silent catch, per development-workflow).
3403
+ *
3404
+ * Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
3405
+ * telemetry), but the `paused` liveness-clear uses it to know whether to
3406
+ * RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
3407
+ * stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
3408
+ * leaves liveness").
3409
+ */
3410
+ async postSignal(conversationId, messageId, signal, extra) {
3411
+ try {
3412
+ const res = await this.fetchImpl(
3413
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
1888
3414
  {
1889
- method: "PATCH",
3415
+ method: "POST",
1890
3416
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1891
- body: JSON.stringify({ status: "failed" })
3417
+ body: JSON.stringify({ signal, ...extra })
1892
3418
  }
1893
- )
1894
- );
3419
+ );
3420
+ if (!res.ok) {
3421
+ this.log({
3422
+ level: "error",
3423
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
3424
+ conversation_id: conversationId,
3425
+ message_id: messageId
3426
+ });
3427
+ return false;
3428
+ }
3429
+ return true;
3430
+ } catch (err) {
3431
+ this.log({
3432
+ level: "error",
3433
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
3434
+ conversation_id: conversationId,
3435
+ message_id: messageId
3436
+ });
3437
+ return false;
3438
+ }
1895
3439
  }
1896
3440
  async persistSession(conversationId, sessionId) {
1897
3441
  const res = await this.fetchImpl(
@@ -1906,10 +3450,17 @@ var ChannelDriver = class {
1906
3450
  }
1907
3451
  /**
1908
3452
  * EXISTING combinedAuth interaction route (WI-CHAN-3) — idempotent + retried.
1909
- * `POST .../interactive-event {type, data}`. The server persists the
1910
- * interaction and posts a link to the proxied opencode-web conversation.
3453
+ * `POST .../interactive-event {type, data, source_message_id?}`. The server
3454
+ * persists the interaction and posts a link to the proxied opencode-web
3455
+ * conversation, @mentioning the user who triggered THIS message's turn.
3456
+ *
3457
+ * WI-3 / WI-4 contract: `source_message_id` is the PAUSED message's own Slack
3458
+ * ts (`message.source_message_id`). The server resolves the @mention from that
3459
+ * message's user FIRST (falling back to the old "newest processing" precedence
3460
+ * only when absent), so the correct person is mentioned under concurrency. It
3461
+ * is OPTIONAL for back-compat with older clients / legacy rows.
1911
3462
  */
1912
- async reportInteraction(conversationId, type, data) {
3463
+ async reportInteraction(conversationId, type, data, sourceMessageId) {
1913
3464
  try {
1914
3465
  await this.callWithRetry(
1915
3466
  "reporting interactive event",
@@ -1918,7 +3469,9 @@ var ChannelDriver = class {
1918
3469
  {
1919
3470
  method: "POST",
1920
3471
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1921
- body: JSON.stringify({ type, data })
3472
+ body: JSON.stringify(
3473
+ sourceMessageId ? { type, data, source_message_id: sourceMessageId } : { type, data }
3474
+ )
1922
3475
  }
1923
3476
  )
1924
3477
  );
@@ -1927,6 +3480,7 @@ var ChannelDriver = class {
1927
3480
  message: `${type} surfaced to channel (id: ${data.id.slice(0, 8)})`,
1928
3481
  conversation_id: conversationId
1929
3482
  });
3483
+ return true;
1930
3484
  } catch (err) {
1931
3485
  if (err instanceof ChannelAuthError) throw err;
1932
3486
  this.log({
@@ -1934,6 +3488,7 @@ var ChannelDriver = class {
1934
3488
  message: `Failed to surface ${type}: ${err instanceof Error ? err.message : String(err)}`,
1935
3489
  conversation_id: conversationId
1936
3490
  });
3491
+ return false;
1937
3492
  }
1938
3493
  }
1939
3494
  // -------------------------------------------------------------------------
@@ -1972,8 +3527,9 @@ var ChannelDriver = class {
1972
3527
  await this.sleep(backoffDelay(attempt, this.retry));
1973
3528
  continue;
1974
3529
  }
3530
+ break;
1975
3531
  }
1976
- throw new Error(`${context}: HTTP ${res.status}`);
3532
+ throw new ChannelTerminalError(`${context}: HTTP ${res.status}`, res.status);
1977
3533
  }
1978
3534
  throw lastError instanceof Error ? lastError : new Error(`${context}: exhausted retries`);
1979
3535
  }
@@ -2151,6 +3707,25 @@ async function resolveAgentIdFromKey(authHeader) {
2151
3707
  return { error: `Failed to resolve agent from key: ${message}` };
2152
3708
  }
2153
3709
  }
3710
+ async function notifyAgentDisconnected(agentId, authHeader) {
3711
+ const apiUrl = getApiUrlConfig();
3712
+ try {
3713
+ const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
3714
+ method: "POST",
3715
+ headers: { Authorization: authHeader }
3716
+ });
3717
+ if (!response.ok) {
3718
+ const serverMessage = await readErrorMessage(response);
3719
+ return {
3720
+ ok: false,
3721
+ error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
3722
+ };
3723
+ }
3724
+ return { ok: true };
3725
+ } catch (error2) {
3726
+ return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
3727
+ }
3728
+ }
2154
3729
  async function getAgentInfo(agentId, authHeader) {
2155
3730
  const apiUrl = getApiUrlConfig();
2156
3731
  try {
@@ -2196,7 +3771,9 @@ async function getAgentInfo(agentId, authHeader) {
2196
3771
  // src/commands/run.ts
2197
3772
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
2198
3773
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
2199
- function log(state, message, isError = false) {
3774
+ var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
3775
+ var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
3776
+ function log2(state, message, isError = false) {
2200
3777
  if (state.json) {
2201
3778
  console.log(
2202
3779
  JSON.stringify({
@@ -2221,9 +3798,9 @@ function logActivity(state, entry) {
2221
3798
  }
2222
3799
  if (!state.interactive) {
2223
3800
  if (entry.type === "error") {
2224
- log(state, entry.error ?? "Unknown error", true);
3801
+ log2(state, entry.error ?? "Unknown error", true);
2225
3802
  } else if (entry.type === "info" && entry.message) {
2226
- log(state, entry.message);
3803
+ log2(state, entry.message);
2227
3804
  }
2228
3805
  }
2229
3806
  }
@@ -2309,6 +3886,7 @@ async function handleAuthError(state, error2) {
2309
3886
  }
2310
3887
  async function driveChannels(state, driver) {
2311
3888
  let idlePolls = 0;
3889
+ let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
2312
3890
  while (state.running) {
2313
3891
  if (state.connection?.reconnecting && state.connection.reconnectPromise) {
2314
3892
  logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
@@ -2318,9 +3896,11 @@ async function driveChannels(state, driver) {
2318
3896
  try {
2319
3897
  const processed = await driver.drainPending();
2320
3898
  state.messageCount += processed;
2321
- if (processed > 0) {
3899
+ const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
3900
+ lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
3901
+ if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
2322
3902
  idlePolls = 0;
2323
- if (state.interactive) displayStatus(state);
3903
+ if (processed > 0 && state.interactive) displayStatus(state);
2324
3904
  } else if (state.idleTimeout !== null) {
2325
3905
  idlePolls++;
2326
3906
  if (idlePolls === 1) {
@@ -2347,7 +3927,7 @@ async function driveChannels(state, driver) {
2347
3927
  logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
2348
3928
  if (state.interactive) displayStatus(state);
2349
3929
  }
2350
- await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
3930
+ await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
2351
3931
  if (state.idleTimeout !== null && idlePolls >= 2) {
2352
3932
  const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
2353
3933
  if (idleMs > state.idleTimeout * 1e3) {
@@ -2358,8 +3938,122 @@ async function driveChannels(state, driver) {
2358
3938
  }
2359
3939
  }
2360
3940
  }
2361
- async function cleanup(state) {
3941
+ var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
3942
+ async function runSweep(state, driver, config2) {
3943
+ const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
3944
+ try {
3945
+ const sessions = await listSessions(state.port);
3946
+ if (sessions === null) {
3947
+ logActivity(state, {
3948
+ type: "info",
3949
+ message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
3950
+ });
3951
+ return;
3952
+ }
3953
+ const toDelete = selectSessionsToDelete(
3954
+ sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
3955
+ {
3956
+ maxAgeMs: config2.maxAgeMs,
3957
+ maxCount: config2.maxCount,
3958
+ nowMs: Date.now(),
3959
+ protectedIds: driver.protectedSessionIds()
3960
+ }
3961
+ );
3962
+ const protectedNow = driver.protectedSessionIds();
3963
+ let deleted = 0;
3964
+ let failed = 0;
3965
+ let skippedNewlyActive = 0;
3966
+ for (const id of toDelete) {
3967
+ if (protectedNow.has(id)) {
3968
+ skippedNewlyActive++;
3969
+ logActivity(state, {
3970
+ type: "info",
3971
+ message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
3972
+ });
3973
+ continue;
3974
+ }
3975
+ if (await deleteSession(state.port, id)) deleted++;
3976
+ else failed++;
3977
+ }
3978
+ const failedNote = failed > 0 ? `, failed ${failed}` : "";
3979
+ const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
3980
+ logActivity(state, {
3981
+ type: "info",
3982
+ message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
3983
+ });
3984
+ } catch (error2) {
3985
+ const message = error2 instanceof Error ? error2.message : String(error2);
3986
+ logActivity(state, {
3987
+ type: "error",
3988
+ error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
3989
+ });
3990
+ }
3991
+ }
3992
+ function scheduleSessionCleanup(state, driver, options) {
3993
+ const config2 = resolveSessionCleanupConfig(
3994
+ {
3995
+ maxAge: options.sessionCleanupMaxAge,
3996
+ maxCount: options.sessionCleanupMaxCount,
3997
+ interval: options.sessionCleanupInterval
3998
+ },
3999
+ process.env
4000
+ );
4001
+ for (const warning2 of config2.warnings) {
4002
+ logActivity(state, { type: "info", message: `Session cleanup: ${warning2}` });
4003
+ }
4004
+ if (!config2.enabled) return;
4005
+ logActivity(state, {
4006
+ type: "info",
4007
+ message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
4008
+ });
4009
+ const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
4010
+ const firstSweep = setTimeout(
4011
+ () => void runSweep(state, driver, config2),
4012
+ SESSION_CLEANUP_FIRST_SWEEP_MS
4013
+ );
4014
+ state.sessionCleanupTimers.push(interval, firstSweep);
4015
+ }
4016
+ async function notifyOffline(state) {
4017
+ if (!state.agentId || !state.authHeader) return;
4018
+ if (!state.connected) {
4019
+ log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
4020
+ return;
4021
+ }
4022
+ const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
4023
+ if (result.ok) {
4024
+ log2(state, "Notified Evident the agent is going offline");
4025
+ } else {
4026
+ logActivity(state, {
4027
+ type: "error",
4028
+ error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
4029
+ });
4030
+ if (state.interactive) displayStatus(state);
4031
+ }
4032
+ }
4033
+ async function cleanup(state, opts = {}) {
2362
4034
  state.running = false;
4035
+ for (const timer of state.sessionCleanupTimers) {
4036
+ clearInterval(timer);
4037
+ clearTimeout(timer);
4038
+ }
4039
+ state.sessionCleanupTimers = [];
4040
+ if (opts.graceful && state.channelDriver) {
4041
+ state.channelDriver.stop();
4042
+ log2(state, "Draining in-flight channel work before shutdown...");
4043
+ if (state.interactive) {
4044
+ logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
4045
+ displayStatus(state);
4046
+ }
4047
+ const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
4048
+ if (!settled) {
4049
+ logActivity(state, {
4050
+ type: "info",
4051
+ message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
4052
+ });
4053
+ if (state.interactive) displayStatus(state);
4054
+ }
4055
+ }
4056
+ await notifyOffline(state);
2363
4057
  if (state.connection) {
2364
4058
  state.connection.close();
2365
4059
  state.connection = null;
@@ -2370,7 +4064,7 @@ async function cleanup(state) {
2370
4064
  logActivity(state, { type: "info", message: "Stopped OpenCode process" });
2371
4065
  displayStatus(state);
2372
4066
  } else {
2373
- log(state, "Stopped OpenCode process");
4067
+ log2(state, "Stopped OpenCode process");
2374
4068
  }
2375
4069
  state.opencodeProcess = null;
2376
4070
  }
@@ -2390,26 +4084,32 @@ async function run(options) {
2390
4084
  opencodeVersion: null,
2391
4085
  opencodeProcess: null,
2392
4086
  connection: null,
4087
+ channelDriver: null,
2393
4088
  running: true,
4089
+ shuttingDown: false,
2394
4090
  activityLog: [],
2395
4091
  messageCount: 0,
4092
+ lastProxiedActivityAt: null,
4093
+ sessionCleanupTimers: [],
2396
4094
  authHeader: ""
2397
4095
  };
2398
4096
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
2399
- log(
4097
+ log2(
2400
4098
  state,
2401
4099
  "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.",
2402
4100
  false
2403
4101
  );
2404
4102
  }
2405
4103
  const handleSignal = async () => {
4104
+ if (state.shuttingDown) return;
4105
+ state.shuttingDown = true;
2406
4106
  if (state.interactive) {
2407
4107
  logActivity(state, { type: "info", message: "Shutting down..." });
2408
4108
  displayStatus(state);
2409
4109
  } else {
2410
- log(state, "Shutting down...");
4110
+ log2(state, "Shutting down...");
2411
4111
  }
2412
- await cleanup(state);
4112
+ await cleanup(state, { graceful: true });
2413
4113
  await shutdownTelemetry();
2414
4114
  process.exit(0);
2415
4115
  };
@@ -2440,7 +4140,7 @@ async function run(options) {
2440
4140
  const resolved = await resolveAgentIdFromKey(state.authHeader);
2441
4141
  if (resolved.agent_id) {
2442
4142
  state.agentId = resolved.agent_id;
2443
- log(state, `Resolved agent ID from key: ${state.agentId}`);
4143
+ log2(state, `Resolved agent ID from key: ${state.agentId}`);
2444
4144
  if (state.interactive && !state.json) {
2445
4145
  logActivity(state, {
2446
4146
  type: "info",
@@ -2503,14 +4203,21 @@ async function run(options) {
2503
4203
  port: state.port,
2504
4204
  interactive: state.interactive,
2505
4205
  agentId: state.agentId,
2506
- log: (message) => log(state, message)
4206
+ log: (message) => log2(state, message)
2507
4207
  });
2508
4208
  state.port = oc.port;
2509
4209
  state.opencodeProcess = oc.process;
2510
4210
  state.opencodeVersion = oc.version;
2511
4211
  state.opencodeConnected = oc.process !== null || oc.version !== null;
2512
- const version = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
2513
- ocSpinner?.succeed(`OpenCode running on port ${state.port}${version}`);
4212
+ const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
4213
+ ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
4214
+ const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
4215
+ if (versionWarning) {
4216
+ log2(state, versionWarning, false);
4217
+ if (state.interactive && !state.json) {
4218
+ logActivity(state, { type: "info", message: versionWarning });
4219
+ }
4220
+ }
2514
4221
  } catch (error2) {
2515
4222
  ocSpinner?.fail(error2.message);
2516
4223
  throw error2;
@@ -2522,12 +4229,14 @@ async function run(options) {
2522
4229
  apiUrl: getApiUrlConfig(),
2523
4230
  getAuthHeader: () => state.authHeader,
2524
4231
  conversationFilter: state.conversationFilter,
4232
+ stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
2525
4233
  log: (entry) => logActivity(state, {
2526
4234
  type: entry.level === "error" ? "error" : "info",
2527
4235
  message: entry.message,
2528
4236
  error: entry.level === "error" ? entry.message : void 0
2529
4237
  })
2530
4238
  });
4239
+ state.channelDriver = channelDriver;
2531
4240
  const connection = new RunnerConnection({
2532
4241
  agentId: state.agentId,
2533
4242
  getAuthHeader: () => state.authHeader,
@@ -2541,7 +4250,11 @@ async function run(options) {
2541
4250
  type: "info",
2542
4251
  message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
2543
4252
  });
2544
- emitAgentConnected(state.agentId, { port: state.port });
4253
+ emitAgentConnected(state.agentId, {
4254
+ port: state.port,
4255
+ cli_version: getCliVersion(),
4256
+ opencode_version: state.opencodeVersion
4257
+ });
2545
4258
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
2546
4259
  if (state.interactive) displayStatus(state);
2547
4260
  channelDriver.drainPending().then((processed) => {
@@ -2575,9 +4288,14 @@ async function run(options) {
2575
4288
  logActivity(state, { type: "error", error: error2 });
2576
4289
  if (state.interactive) displayStatus(state);
2577
4290
  },
2578
- // Web traffic is proxied transparently; only note opencode is live.
4291
+ // Web traffic is proxied transparently; note opencode is live and stamp
4292
+ // proxied activity so the idle loop treats interactive proxy use as work.
4293
+ // Fires per forwarded response head (incl. every SSE open) and excludes
4294
+ // the internal drain-ping, so an actively-used proxy keeps the timer
4295
+ // fresh while a lone idle SSE with no follow-up requests still ages out.
2579
4296
  onResponse: () => {
2580
4297
  state.opencodeConnected = true;
4298
+ state.lastProxiedActivityAt = Date.now();
2581
4299
  },
2582
4300
  // A channel message was queued and the api-worker pinged us over the
2583
4301
  // tunnel to drain immediately instead of waiting for the next poll tick.
@@ -2615,10 +4333,12 @@ async function run(options) {
2615
4333
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
2616
4334
  throw error2;
2617
4335
  }
4336
+ scheduleSessionCleanup(state, channelDriver, options);
2618
4337
  if (!interactive || state.json) {
2619
- log(state, "Driving channel messages...");
4338
+ log2(state, "Driving channel messages...");
2620
4339
  }
2621
4340
  await driveChannels(state, channelDriver);
4341
+ if (state.shuttingDown) return;
2622
4342
  await cleanup(state);
2623
4343
  if (state.json) {
2624
4344
  console.log(
@@ -2628,11 +4348,12 @@ async function run(options) {
2628
4348
  })
2629
4349
  );
2630
4350
  } else if (!interactive) {
2631
- log(state, `Completed. Processed ${state.messageCount} message(s).`);
4351
+ log2(state, `Completed. Processed ${state.messageCount} message(s).`);
2632
4352
  }
2633
4353
  await shutdownTelemetry();
2634
4354
  process.exit(0);
2635
4355
  } catch (error2) {
4356
+ if (state.shuttingDown) return;
2636
4357
  await cleanup(state);
2637
4358
  const message = error2 instanceof Error ? error2.message : String(error2);
2638
4359
  if (state.json) {
@@ -2650,8 +4371,9 @@ async function run(options) {
2650
4371
  }
2651
4372
 
2652
4373
  // src/index.ts
4374
+ var { version } = createRequire(import.meta.url)("../package.json");
2653
4375
  var program = new Command();
2654
- program.name("evident").description("Run OpenCode locally and connect it to Evident").version("0.1.0").option(
4376
+ program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
2655
4377
  "--endpoint <url>",
2656
4378
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
2657
4379
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
@@ -2666,7 +4388,16 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
2666
4388
  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);
2667
4389
  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 }));
2668
4390
  program.command("whoami").description("Show the currently logged in user").action(whoami);
2669
- 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(
4391
+ 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(
4392
+ "--session-cleanup-max-age <duration>",
4393
+ "Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
4394
+ ).option(
4395
+ "--session-cleanup-max-count <n>",
4396
+ "Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
4397
+ ).option(
4398
+ "--session-cleanup-interval <duration>",
4399
+ "How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
4400
+ ).action(
2670
4401
  (options) => {
2671
4402
  run({
2672
4403
  agent: options.agent,
@@ -2674,7 +4405,11 @@ program.command("run").description("Connect to Evident and process messages").op
2674
4405
  verbose: options.verbose,
2675
4406
  conversation: options.conversation,
2676
4407
  idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
2677
- json: options.json
4408
+ json: options.json,
4409
+ // Raw strings — the resolver in run.ts single-sources parsing (M1).
4410
+ sessionCleanupMaxAge: options.sessionCleanupMaxAge,
4411
+ sessionCleanupMaxCount: options.sessionCleanupMaxCount,
4412
+ sessionCleanupInterval: options.sessionCleanupInterval
2678
4413
  });
2679
4414
  }
2680
4415
  );