@evident-ai/cli 3.0.1-dev.284117a → 3.0.1-dev.291178f

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,83 @@ async function getSessionMessages(port, sessionId) {
1008
1078
  return null;
1009
1079
  }
1010
1080
  }
1011
- function isTurnComplete(messages) {
1081
+ function isSessionActivelyGenerating(messages) {
1012
1082
  if (!messages || messages.length === 0) return false;
1013
1083
  const last = messages[messages.length - 1];
1014
1084
  if (roleOf(last) !== "assistant") return false;
1015
- return completedOf(last) != null;
1085
+ return completedOf(last) == null;
1086
+ }
1087
+ function sessionLastActivityMs(session) {
1088
+ const candidates = [
1089
+ session.time?.updated,
1090
+ session.time?.created,
1091
+ session.time_updated,
1092
+ session.time_created,
1093
+ session.updated,
1094
+ session.created
1095
+ ];
1096
+ for (const c of candidates) {
1097
+ if (typeof c === "number" && Number.isFinite(c)) return c;
1098
+ }
1099
+ return null;
1100
+ }
1101
+ async function listSessions(port) {
1102
+ try {
1103
+ const res = await fetch(`${opencodeBase(port)}/session`);
1104
+ if (!res.ok) return null;
1105
+ const body = await res.json();
1106
+ return Array.isArray(body) ? body : null;
1107
+ } catch {
1108
+ return null;
1109
+ }
1110
+ }
1111
+ async function deleteSession(port, id) {
1112
+ try {
1113
+ const res = await fetch(`${opencodeBase(port)}/session/${id}`, { method: "DELETE" });
1114
+ return res.status >= 200 && res.status < 300;
1115
+ } catch {
1116
+ return false;
1117
+ }
1118
+ }
1119
+ async function sessionExists(port, id) {
1120
+ try {
1121
+ const res = await fetch(`${opencodeBase(port)}/session/${id}`);
1122
+ if (res.status >= 200 && res.status < 300) return true;
1123
+ if (res.status === 404) return false;
1124
+ return null;
1125
+ } catch {
1126
+ return null;
1127
+ }
1128
+ }
1129
+ async function getSessionStatuses(port) {
1130
+ try {
1131
+ const res = await fetch(`${opencodeBase(port)}/session/status`);
1132
+ if (!res.ok) {
1133
+ console.error(
1134
+ `[getSessionStatuses] GET /session/status returned HTTP ${res.status} (port ${port})`
1135
+ );
1136
+ return null;
1137
+ }
1138
+ const body = await res.json();
1139
+ if (body == null || typeof body !== "object" || Array.isArray(body)) {
1140
+ console.error(
1141
+ `[getSessionStatuses] GET /session/status body was not a plain object (port ${port})`
1142
+ );
1143
+ return null;
1144
+ }
1145
+ return body;
1146
+ } catch (err) {
1147
+ console.error(
1148
+ `[getSessionStatuses] GET /session/status failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
1149
+ );
1150
+ return null;
1151
+ }
1152
+ }
1153
+ async function isSessionOngoing(port, id) {
1154
+ const map = await getSessionStatuses(port);
1155
+ if (map == null) return null;
1156
+ const entry = map[id];
1157
+ return entry != null && entry.type !== "idle";
1016
1158
  }
1017
1159
  async function createOpenCodeSession(port, directory) {
1018
1160
  const url = new URL(`${opencodeBase(port)}/session`);
@@ -1031,9 +1173,113 @@ async function createOpenCodeSession(port, directory) {
1031
1173
  const data = await response.json();
1032
1174
  return data.id;
1033
1175
  }
1034
- async function sendMessageToOpenCode(port, sessionId, content, options, hooks, maxWaitMs = 10 * 60 * 1e3) {
1176
+ async function getModelAttachmentCapability(port, model) {
1177
+ try {
1178
+ const res = await fetch(`${opencodeBase(port)}/config/providers`);
1179
+ if (!res.ok) {
1180
+ console.error(
1181
+ `[getModelAttachmentCapability] GET /config/providers returned HTTP ${res.status} (port ${port})`
1182
+ );
1183
+ return null;
1184
+ }
1185
+ const body = await res.json();
1186
+ const providers = Array.isArray(body?.providers) ? body.providers : null;
1187
+ if (!providers) {
1188
+ console.error(
1189
+ `[getModelAttachmentCapability] GET /config/providers body had no providers array (port ${port})`
1190
+ );
1191
+ return null;
1192
+ }
1193
+ const slash = model ? model.indexOf("/") : -1;
1194
+ const providerId = slash > 0 ? model.slice(0, slash) : void 0;
1195
+ let modelId = slash > 0 ? model.slice(slash + 1) : void 0;
1196
+ const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
1197
+ let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
1198
+ if (!provider && !providerId) {
1199
+ const defaultProviderIds = defaults2 ? Object.keys(defaults2) : [];
1200
+ if (defaultProviderIds.length === 1) {
1201
+ provider = providers.find((p) => p?.id === defaultProviderIds[0]);
1202
+ }
1203
+ }
1204
+ if (!provider || !provider.models) return null;
1205
+ if (!modelId && defaults2 && typeof provider.id === "string") {
1206
+ const def = defaults2[provider.id];
1207
+ if (typeof def === "string") modelId = def;
1208
+ }
1209
+ if (!modelId) {
1210
+ if (providerId) {
1211
+ const keys = Object.keys(provider.models);
1212
+ if (keys.length === 1) modelId = keys[0];
1213
+ }
1214
+ if (!modelId) return null;
1215
+ }
1216
+ const entry = provider.models[modelId];
1217
+ if (!entry || typeof entry !== "object") return null;
1218
+ return typeof entry.attachment === "boolean" ? entry.attachment : null;
1219
+ } catch (err) {
1220
+ console.error(
1221
+ `[getModelAttachmentCapability] GET /config/providers failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
1222
+ );
1223
+ return null;
1224
+ }
1225
+ }
1226
+ async function buildFileParts(attachments, capable) {
1227
+ const outcomes = [];
1228
+ const parts = [];
1229
+ const capabilityUnknown = capable === null;
1230
+ if (capable !== true) {
1231
+ for (const a of attachments.inputs) {
1232
+ outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "skipped" });
1233
+ }
1234
+ return { parts, outcomes, capabilityUnknown };
1235
+ }
1236
+ for (const a of attachments.inputs) {
1237
+ let dataUrl = null;
1238
+ try {
1239
+ dataUrl = await attachments.fetchDataUrl(a.index);
1240
+ } catch (err) {
1241
+ console.error(
1242
+ `[buildFileParts] attachment ${a.index} (${a.mime}) fetch threw \u2014 omitting: ${err instanceof Error ? err.message : String(err)}`
1243
+ );
1244
+ dataUrl = null;
1245
+ }
1246
+ if (dataUrl == null) {
1247
+ outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "failed" });
1248
+ continue;
1249
+ }
1250
+ parts.push({
1251
+ type: "file",
1252
+ mime: a.mime,
1253
+ url: dataUrl,
1254
+ ...a.filename ? { filename: a.filename } : {}
1255
+ });
1256
+ outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "sent" });
1257
+ }
1258
+ return { parts, outcomes, capabilityUnknown };
1259
+ }
1260
+ function messageText(m) {
1261
+ if (!m || !Array.isArray(m.parts)) return "";
1262
+ return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
1263
+ }
1264
+ async function sendPromptAsync(port, sessionId, content, options, attachments) {
1265
+ const before = await getSessionMessages(port, sessionId);
1266
+ const knownUserIds = new Set(
1267
+ (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
1268
+ );
1269
+ const parts = [{ type: "text", text: content }];
1270
+ let pendingOutcomes = null;
1271
+ if (attachments && attachments.inputs.length > 0) {
1272
+ const capable = await getModelAttachmentCapability(port, options?.model);
1273
+ const {
1274
+ parts: fileParts,
1275
+ outcomes,
1276
+ capabilityUnknown
1277
+ } = await buildFileParts(attachments, capable);
1278
+ parts.push(...fileParts);
1279
+ if (attachments.onOutcomes) pendingOutcomes = { outcomes, capabilityUnknown };
1280
+ }
1035
1281
  const body = {
1036
- parts: [{ type: "text", text: content }]
1282
+ parts
1037
1283
  };
1038
1284
  if (options?.agent) {
1039
1285
  body.agent = options.agent;
@@ -1047,79 +1293,221 @@ async function sendMessageToOpenCode(port, sessionId, content, options, hooks, m
1047
1293
  };
1048
1294
  }
1049
1295
  }
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 {
1296
+ const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
1297
+ method: "POST",
1298
+ headers: { "Content-Type": "application/json" },
1299
+ body: JSON.stringify(body)
1300
+ });
1301
+ if (res.status < 200 || res.status >= 300) {
1302
+ const text = await res.text().catch(() => "");
1303
+ throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1304
+ }
1305
+ const READ_BACK_ATTEMPTS = 5;
1306
+ const READ_BACK_DELAY_MS = 150;
1307
+ for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
1308
+ const after = await getSessionMessages(port, sessionId);
1309
+ if (after) {
1310
+ let best = null;
1311
+ for (const m of after) {
1312
+ if (roleOf(m) !== "user") continue;
1313
+ const id = idOf(m);
1314
+ if (typeof id !== "string" || knownUserIds.has(id)) continue;
1315
+ if (messageText(m) !== content) continue;
1316
+ const created = createdOf(m) ?? 0;
1317
+ if (best === null || created > best.created) {
1318
+ best = { id, created };
1070
1319
  }
1071
1320
  }
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 {
1085
- }
1321
+ if (best) {
1322
+ if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
1323
+ return best.id;
1086
1324
  }
1087
1325
  }
1326
+ if (attempt < READ_BACK_ATTEMPTS - 1) {
1327
+ await new Promise((resolve2) => setTimeout(resolve2, READ_BACK_DELAY_MS));
1328
+ }
1329
+ }
1330
+ return null;
1331
+ }
1332
+ function findAssistantReplyAfter(messages, userMessageId) {
1333
+ if (!messages || messages.length === 0) return null;
1334
+ const byParent = messages.find(
1335
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
1336
+ );
1337
+ if (byParent) return byParent;
1338
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1339
+ if (userIndex === -1) return null;
1340
+ for (let i = userIndex + 1; i < messages.length; i++) {
1341
+ if (roleOf(messages[i]) === "assistant") return messages[i];
1342
+ }
1343
+ return null;
1344
+ }
1345
+ function findLastAssistantReplyFor(messages, userMessageId) {
1346
+ if (!messages || messages.length === 0) return null;
1347
+ let lastCorrelated = null;
1348
+ let lastNonErrored = null;
1349
+ for (let i = messages.length - 1; i >= 0; i--) {
1350
+ const m = messages[i];
1351
+ if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
1352
+ if (lastCorrelated === null) lastCorrelated = m;
1353
+ if (errorOf(m) == null) {
1354
+ lastNonErrored = m;
1355
+ break;
1356
+ }
1357
+ }
1358
+ if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
1359
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1360
+ if (userIndex === -1) return null;
1361
+ let last = null;
1362
+ let lastOk = null;
1363
+ for (let i = userIndex + 1; i < messages.length; i++) {
1364
+ const role = roleOf(messages[i]);
1365
+ if (role === "user") break;
1366
+ if (role === "assistant") {
1367
+ last = messages[i];
1368
+ if (errorOf(messages[i]) == null) lastOk = messages[i];
1369
+ }
1370
+ }
1371
+ return lastOk ?? last;
1372
+ }
1373
+ function messageRunState(messages, userMessageId) {
1374
+ if (!messages || messages.length === 0) return "unknown";
1375
+ const hasUser = messages.some((m) => idOf(m) === userMessageId);
1376
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1377
+ if (!hasUser) {
1378
+ if (!reply) return "unknown";
1379
+ }
1380
+ if (!reply) return "queued";
1381
+ if (isAssistantInFlight(reply)) return "running";
1382
+ return errorOf(reply) != null ? "failed" : "done";
1383
+ }
1384
+ function isPreamblePinnedRunning(messages, userMessageId) {
1385
+ if (messageRunState(messages, userMessageId) !== "running") return false;
1386
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1387
+ return completedOf(reply) != null && finishOf(reply) === "tool-calls";
1388
+ }
1389
+ function messageError(messages, userMessageId) {
1390
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1391
+ const error2 = errorOf(reply);
1392
+ if (error2 == null) return null;
1393
+ if (typeof error2 === "string") return error2;
1394
+ if (typeof error2 === "object") {
1395
+ const e = error2;
1396
+ const dataMessage = e.data?.message;
1397
+ if (typeof dataMessage === "string") return dataMessage;
1398
+ if (typeof e.message === "string") return e.message;
1399
+ }
1400
+ return "The agent run failed.";
1401
+ }
1402
+ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
1403
+ if (!messages || messages.length === 0) return false;
1404
+ return messages.some(
1405
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
1406
+ );
1407
+ }
1408
+
1409
+ // src/lib/opencode/session-cleanup.ts
1410
+ var DURATION_UNIT_MS = {
1411
+ s: 1e3,
1412
+ m: 60 * 1e3,
1413
+ h: 60 * 60 * 1e3,
1414
+ d: 24 * 60 * 60 * 1e3
1415
+ };
1416
+ function parseDurationMs(input) {
1417
+ const trimmed = input.trim();
1418
+ const match = /^(\d+)([smhd])$/.exec(trimmed);
1419
+ if (!match) {
1420
+ throw new Error(
1421
+ `Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
1422
+ );
1423
+ }
1424
+ const value = Number(match[1]);
1425
+ if (value <= 0) {
1426
+ throw new Error(`Invalid duration "${input}": must be a positive value.`);
1427
+ }
1428
+ return value * DURATION_UNIT_MS[match[2]];
1429
+ }
1430
+ function selectSessionsToDelete(sessions, opts) {
1431
+ const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
1432
+ if (maxAgeMs === void 0 && maxCount === void 0) return [];
1433
+ const ageEligible = (s) => {
1434
+ if (maxAgeMs === void 0) return false;
1435
+ if (s.lastActivityMs === null) return true;
1436
+ return nowMs - s.lastActivityMs > maxAgeMs;
1088
1437
  };
1089
- const sendMessage = async () => {
1090
- const controller = new AbortController();
1091
- const timer = setTimeout(() => controller.abort(), maxWaitMs);
1438
+ const countEligibleIds = /* @__PURE__ */ new Set();
1439
+ if (maxCount !== void 0) {
1440
+ const byActivityDesc = [...sessions].sort(
1441
+ (a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
1442
+ );
1443
+ for (const s of byActivityDesc.slice(maxCount)) {
1444
+ countEligibleIds.add(s.id);
1445
+ }
1446
+ }
1447
+ const toDelete = [];
1448
+ for (const s of sessions) {
1449
+ if (protectedIds.has(s.id)) continue;
1450
+ if (ageEligible(s) || countEligibleIds.has(s.id)) {
1451
+ toDelete.push(s.id);
1452
+ }
1453
+ }
1454
+ return toDelete;
1455
+ }
1456
+ var DEFAULT_INTERVAL = "1h";
1457
+ function resolve(flag, envValue, fallback) {
1458
+ return flag ?? envValue ?? fallback;
1459
+ }
1460
+ function parseMaxCount(input) {
1461
+ const trimmed = input.trim();
1462
+ if (!/^\d+$/.test(trimmed)) {
1463
+ throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
1464
+ }
1465
+ const value = Number(trimmed);
1466
+ if (value <= 0) {
1467
+ throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
1468
+ }
1469
+ return value;
1470
+ }
1471
+ function resolveSessionCleanupConfig(flags, env = process.env) {
1472
+ const warnings = [];
1473
+ const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
1474
+ const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
1475
+ const intervalRaw = resolve(
1476
+ flags.interval,
1477
+ env.EVIDENT_SESSION_CLEANUP_INTERVAL,
1478
+ DEFAULT_INTERVAL
1479
+ );
1480
+ let maxAgeMs;
1481
+ if (maxAgeRaw !== void 0) {
1092
1482
  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
1483
+ maxAgeMs = parseDurationMs(maxAgeRaw);
1484
+ } catch (err) {
1485
+ warnings.push(
1486
+ `Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
1105
1487
  );
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 };
1488
+ }
1489
+ }
1490
+ let maxCount;
1491
+ if (maxCountRaw !== void 0) {
1492
+ try {
1493
+ maxCount = parseMaxCount(maxCountRaw);
1111
1494
  } 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;
1495
+ warnings.push(
1496
+ `Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
1497
+ );
1119
1498
  }
1120
- };
1121
- const [result] = await Promise.all([sendMessage(), pollInteractive()]);
1122
- return result;
1499
+ }
1500
+ let intervalMs;
1501
+ try {
1502
+ intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
1503
+ } catch (err) {
1504
+ warnings.push(
1505
+ `Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
1506
+ );
1507
+ intervalMs = parseDurationMs(DEFAULT_INTERVAL);
1508
+ }
1509
+ const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
1510
+ return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
1123
1511
  }
1124
1512
 
1125
1513
  // src/lib/tunnel/connection.ts
@@ -1190,24 +1578,34 @@ var StreamForwarder = class {
1190
1578
  }
1191
1579
  async handleOpen(frame) {
1192
1580
  const { sid, method, path, headers, has_body } = frame;
1581
+ const correlationId = headers?.[CORRELATION_ID_HEADER];
1582
+ const startedAt = Date.now();
1193
1583
  if (path === TUNNEL_DRAIN_PING_PATH) {
1194
1584
  this.callbacks.onDrainPing?.();
1195
1585
  this.send({ type: "head", sid, status: 204, headers: {} });
1196
1586
  this.send({ type: "res_end", sid });
1197
1587
  return;
1198
1588
  }
1589
+ if (process.env.DEBUG) {
1590
+ log("debug", "agent_request", {
1591
+ correlation_id: correlationId,
1592
+ sid,
1593
+ method,
1594
+ path: stripQuery(path)
1595
+ });
1596
+ }
1199
1597
  const ac = new AbortController();
1200
1598
  let bodyPromise;
1201
1599
  let pushBody;
1202
1600
  let endBody;
1203
1601
  if (has_body) {
1204
1602
  const chunks = [];
1205
- bodyPromise = new Promise((resolve) => {
1603
+ bodyPromise = new Promise((resolve2) => {
1206
1604
  pushBody = (buf) => {
1207
1605
  chunks.push(buf);
1208
1606
  };
1209
1607
  endBody = () => {
1210
- resolve(Buffer.concat(chunks));
1608
+ resolve2(Buffer.concat(chunks));
1211
1609
  };
1212
1610
  });
1213
1611
  }
@@ -1242,6 +1640,14 @@ var StreamForwarder = class {
1242
1640
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1243
1641
  });
1244
1642
  this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
1643
+ if (process.env.DEBUG) {
1644
+ log("debug", "agent_response", {
1645
+ correlation_id: correlationId,
1646
+ sid,
1647
+ status: upstream.status,
1648
+ duration_ms: Date.now() - startedAt
1649
+ });
1650
+ }
1245
1651
  this.callbacks.onHead?.(sid, upstream.status);
1246
1652
  try {
1247
1653
  if (upstream.body) {
@@ -1317,7 +1723,7 @@ function connectTunnel(options) {
1317
1723
  } = options;
1318
1724
  const tunnelUrl = getTunnelUrlConfig();
1319
1725
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
1320
- return new Promise((resolve, reject) => {
1726
+ return new Promise((resolve2, reject) => {
1321
1727
  const ws = new WebSocket2(url, {
1322
1728
  headers: {
1323
1729
  Authorization: authHeader
@@ -1382,7 +1788,7 @@ function connectTunnel(options) {
1382
1788
  clearTimeout(connectionTimeout);
1383
1789
  const connectedAgentId = message.agent_id ?? agentId;
1384
1790
  onConnected?.(connectedAgentId);
1385
- resolve({
1791
+ resolve2({
1386
1792
  ws,
1387
1793
  close: () => ws.close(1e3, "CLI shutdown")
1388
1794
  });
@@ -1498,6 +1904,23 @@ var RunnerConnection = class {
1498
1904
  };
1499
1905
 
1500
1906
  // src/lib/channels/driver.ts
1907
+ function messageIdOf(m) {
1908
+ if (!m || typeof m !== "object") return void 0;
1909
+ if (typeof m.id === "string") return m.id;
1910
+ const infoId = m.info?.id;
1911
+ return typeof infoId === "string" ? infoId : void 0;
1912
+ }
1913
+ function cleanImageMime(contentType) {
1914
+ if (!contentType) return null;
1915
+ const media = contentType.split(";")[0].trim().toLowerCase();
1916
+ return /^image\/[a-z0-9.+-]+$/.test(media) ? media : null;
1917
+ }
1918
+ var LOG_LEVELS = {
1919
+ debug: 0,
1920
+ info: 1,
1921
+ warn: 2,
1922
+ error: 3
1923
+ };
1501
1924
  var DEFAULT_RETRY_POLICY = {
1502
1925
  maxAttempts: 6,
1503
1926
  baseDelayMs: 500,
@@ -1505,12 +1928,24 @@ var DEFAULT_RETRY_POLICY = {
1505
1928
  };
1506
1929
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1507
1930
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1931
+ var DEFAULT_STUCK_QUEUED_MS = 6e4;
1932
+ var HEARTBEAT_MS = 6e4;
1933
+ var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
1934
+ var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
1508
1935
  var ChannelAuthError = class extends Error {
1509
1936
  constructor(message) {
1510
1937
  super(message);
1511
1938
  this.name = "ChannelAuthError";
1512
1939
  }
1513
1940
  };
1941
+ var ChannelTerminalError = class extends Error {
1942
+ status;
1943
+ constructor(message, status) {
1944
+ super(message);
1945
+ this.name = "ChannelTerminalError";
1946
+ this.status = status;
1947
+ }
1948
+ };
1514
1949
  function backoffDelay(attempt, policy) {
1515
1950
  const exp = policy.baseDelayMs * Math.pow(2, attempt);
1516
1951
  const capped = Math.min(policy.maxDelayMs, exp);
@@ -1531,15 +1966,100 @@ var ChannelDriver = class {
1531
1966
  sleep;
1532
1967
  pausedPollIntervalMs;
1533
1968
  pausedMaxWaitMs;
1969
+ stuckQueuedMs;
1970
+ now;
1534
1971
  /** Cache of conversationId → opencode sessionId. */
1535
1972
  sessions = /* @__PURE__ */ new Map();
1536
1973
  /**
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.
1974
+ * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
1975
+ * longer idempotent (no caller-supplied `messageID`), and its read-back picks
1976
+ * "the one new user row" which is only unambiguous if no OTHER dispatch into
1977
+ * the SAME session interleaves its snapshot→POST→read-back. This map chains each
1978
+ * session's dispatches so they run serially; distinct sessions stay concurrent.
1979
+ */
1980
+ sessionDispatchLocks = /* @__PURE__ */ new Map();
1981
+ /**
1982
+ * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1983
+ * session: one polling loop services all of that session's in-flight messages.
1984
+ * A session entry exists while it has any in-flight (dispatched-but-not-done)
1985
+ * message; it is removed once its in-flight set empties.
1541
1986
  */
1542
1987
  watchers = /* @__PURE__ */ new Map();
1988
+ /**
1989
+ * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
1990
+ * dispatched and are still in-flight. A message in this set is never
1991
+ * re-`prompt_async`-ed by a subsequent poll tick while it is queued/running.
1992
+ * Backed by a stable minted opencode `messageID` whose duplicate re-enqueue is
1993
+ * idempotent on opencode (PoC fact 9) — so even if this set is lost on restart,
1994
+ * a steady-state-poll re-dispatch will not double-run the message.
1995
+ */
1996
+ dispatched = /* @__PURE__ */ new Set();
1997
+ /**
1998
+ * Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
1999
+ * Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
2000
+ * so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
2001
+ * it is re-adopted and removed when its watcher settles or it is observed off
2002
+ * the processing list.
2003
+ */
2004
+ readopted = /* @__PURE__ */ new Set();
2005
+ /**
2006
+ * "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
2007
+ * re-adopted running/orphan row's watcher hit its `processed_at`-anchored
2008
+ * deadline (or an orphan whose window already elapsed): the still-`processing`
2009
+ * server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
2010
+ * drain until the 15-min cron resets it — spamming new turns.
2011
+ *
2012
+ * CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
2013
+ * does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
2014
+ * in opencode must still be delivered via `markDone` on the next drain — so
2015
+ * `readoptOne` computes `state` FIRST and this set is checked only on the
2016
+ * non-done path. It is cleared once the row leaves the processing list (cron
2017
+ * reset → it drains normally as `pending`), so it can never leak.
2018
+ */
2019
+ dontRedispatch = /* @__PURE__ */ new Set();
2020
+ /**
2021
+ * "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
2022
+ * re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
2023
+ * never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
2024
+ * that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
2025
+ * markDone failure must NOT land here (it must still retry next drain). Separate
2026
+ * from `dontRedispatch` because the two concerns are independent: a row can need
2027
+ * "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
2028
+ * the row leaves the processing list, exactly like `dontRedispatch`.
2029
+ */
2030
+ doneUndeliverable = /* @__PURE__ */ new Set();
2031
+ /**
2032
+ * "Already emitted `readopt_poll_unresolved` for this row" (#229). The b1 /
2033
+ * unreadable-status re-evaluate leaf leaves the row UN-tracked so it is re-read
2034
+ * every ~2s drain until the status map becomes readable — but the server-visible
2035
+ * signal is an OUTCOME, so it must fire at most ONCE per row, not once per drain
2036
+ * (Bugbot "Re-adopt signals flood every drain"). Cleared when the row leaves the
2037
+ * processing list, exactly like `dontRedispatch`/`doneUndeliverable`.
2038
+ */
2039
+ readoptPollUnresolvedSignalled = /* @__PURE__ */ new Set();
2040
+ /**
2041
+ * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
2042
+ * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
2043
+ * is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
2044
+ * persist hasn't landed before tick N+1 re-reads the still-null
2045
+ * `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
2046
+ * A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
2047
+ * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
2048
+ * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
2049
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
2050
+ * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
2051
+ * so the NEXT tick may retry exactly once more).
2052
+ */
2053
+ awaitingReadopt = /* @__PURE__ */ new Set();
2054
+ /**
2055
+ * "Already signalled `attachments_skipped` for this Evident message id" (#376).
2056
+ * The in-thread skip note is an OUTCOME, so it must fire AT MOST ONCE per message
2057
+ * — never re-post on a re-dispatch of the same row (`forceReadoptRun` or the
2058
+ * next-tick null-id retry both re-run `sendPromptAsync`, which re-fires
2059
+ * `onOutcomes`). Mirrors `readoptPollUnresolvedSignalled`: a local dedup on the
2060
+ * outcome, not the dispatch. Not cleared (a message is signalled once for life).
2061
+ */
2062
+ attachmentsSkippedSignalled = /* @__PURE__ */ new Set();
1543
2063
  /**
1544
2064
  * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1545
2065
  * first session creation so drain-created sessions are rooted at the project
@@ -1547,8 +2067,43 @@ var ChannelDriver = class {
1547
2067
  * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1548
2068
  */
1549
2069
  opencodeDirectory = void 0;
2070
+ /**
2071
+ * Cache of opencode `sessionId → parentID` (its parent session, or `null` when
2072
+ * the session is a root with no parent). Sub-agents spawned via the `task` tool
2073
+ * run in CHILD sessions whose `parentID` chains up to the Evident-created
2074
+ * (watched) session; we resolve this once per session so a child-session
2075
+ * question/permission can be attributed to the watched session's subtree
2076
+ * (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
2077
+ * entry = not yet resolved; `null` = resolved root (stop walking).
2078
+ */
2079
+ sessionParents = /* @__PURE__ */ new Map();
2080
+ /**
2081
+ * Per-session OpenCode title cache (#310), keyed by sessionId. Only a resolved
2082
+ * NON-EMPTY name is stored (terminal — a real session name won't later un-name),
2083
+ * so we do NOT re-GET `/session/:id` every tick. A missing entry = not yet
2084
+ * resolved OR resolved-but-still-empty → re-fetch on next need, since OpenCode
2085
+ * names sessions asynchronously mid-turn. Driver-level (not per-watcher) so both
2086
+ * the watcher completion path AND the restart-recovery re-adopt path (which has
2087
+ * no watcher) can resolve the title.
2088
+ */
2089
+ sessionTitles = /* @__PURE__ */ new Map();
1550
2090
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1551
2091
  draining = false;
2092
+ /**
2093
+ * The currently-executing `drainPending()` promise, or null when idle. Lets a
2094
+ * graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
2095
+ * is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
2096
+ * drain that entered before `stop()` still registers its watcher).
2097
+ */
2098
+ activeDrain = null;
2099
+ /**
2100
+ * Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
2101
+ * dispatches NEW work (it returns 0 immediately) — but the per-session watcher
2102
+ * loops already running keep going so in-flight turns can finish and deliver
2103
+ * their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
2104
+ * and stops opencode.
2105
+ */
2106
+ stopped = false;
1552
2107
  constructor(config2) {
1553
2108
  this.agentId = config2.agentId;
1554
2109
  this.port = config2.port;
@@ -1562,25 +2117,37 @@ var ChannelDriver = class {
1562
2117
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1563
2118
  this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1564
2119
  this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
2120
+ this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
2121
+ this.now = config2.now ?? (() => Date.now());
1565
2122
  }
1566
2123
  /** The IPv4-loopback base URL for the local `opencode serve`. */
1567
2124
  get opencodeBase() {
1568
2125
  return `http://127.0.0.1:${this.port}`;
1569
2126
  }
1570
- // -------------------------------------------------------------------------
1571
- // Public API
1572
- // -------------------------------------------------------------------------
1573
2127
  /**
1574
- * Drain all pending channel conversations once: poll → processcallback.
2128
+ * Drain all pending channel conversations once: poll → dispatchregister.
1575
2129
  * Called on tunnel `connected` (WI-CHAN-4) and on each poll tick by `run.ts`.
1576
2130
  * Re-entrant calls while a drain is in flight are skipped (return 0).
1577
2131
  *
1578
- * @returns the number of messages processed.
2132
+ * @returns the number of messages NEWLY dispatched to opencode's native queue.
1579
2133
  */
1580
2134
  async drainPending() {
2135
+ if (this.stopped) return 0;
1581
2136
  if (this.draining) return 0;
1582
2137
  this.draining = true;
1583
- let processed = 0;
2138
+ const run2 = this.runDrain();
2139
+ this.activeDrain = run2.then(
2140
+ () => {
2141
+ this.activeDrain = null;
2142
+ },
2143
+ () => {
2144
+ this.activeDrain = null;
2145
+ }
2146
+ );
2147
+ return run2;
2148
+ }
2149
+ async runDrain() {
2150
+ let dispatched = 0;
1584
2151
  try {
1585
2152
  const conversations = await this.getPendingConversations();
1586
2153
  if (conversations.length > 0) {
@@ -1591,235 +2158,1565 @@ var ChannelDriver = class {
1591
2158
  });
1592
2159
  }
1593
2160
  for (const conv of conversations) {
1594
- processed += await this.processConversation(conv);
2161
+ if (this.stopped) break;
2162
+ dispatched += await this.processConversation(conv);
1595
2163
  }
2164
+ await this.readoptProcessing();
1596
2165
  } finally {
1597
2166
  this.draining = false;
1598
2167
  }
1599
- return processed;
2168
+ return dispatched;
2169
+ }
2170
+ /**
2171
+ * True while any per-session watcher has a non-empty in-flight dispatched set
2172
+ * (Task 3.7). `run.ts` treats this as NON-idle so `--idle-timeout` cannot exit
2173
+ * the process while a dispatched message is still queued/running — which would
2174
+ * kill the turn and orphan its reply.
2175
+ */
2176
+ hasInFlightWatchers() {
2177
+ for (const watcher of this.watchers.values()) {
2178
+ if (watcher.inFlight.size > 0) return true;
2179
+ }
2180
+ return false;
2181
+ }
2182
+ /**
2183
+ * OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
2184
+ * exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
2185
+ * `watchers` entry whose `inFlight` set is non-empty — the same predicate
2186
+ * `hasInFlightWatchers()` uses, lifted to return the ids.
2187
+ *
2188
+ * Deliberately does NOT include `this.sessions` (the permanent, never-pruned
2189
+ * conversation→session cache). Protecting every bound-but-idle session there
2190
+ * would shield nearly every session and defeat cleanup — AND it is unnecessary:
2191
+ * `ensureSession` is self-healing (it recreates a session whose id no longer
2192
+ * exists), so deleting an idle bound session is harmless — the conversation's
2193
+ * next turn transparently rebinds a fresh one. The only thing worth protecting
2194
+ * is a session with a turn ACTIVELY in flight right now: tearing that down
2195
+ * mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
2196
+ */
2197
+ protectedSessionIds() {
2198
+ const ids = /* @__PURE__ */ new Set();
2199
+ for (const [sessionId, watcher] of this.watchers) {
2200
+ if (watcher.inFlight.size > 0) ids.add(sessionId);
2201
+ }
2202
+ return ids;
2203
+ }
2204
+ /**
2205
+ * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
2206
+ * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
2207
+ * — but the watcher loops already tracking in-flight turns keep running, so a
2208
+ * turn that has finished (or is about to) still fires `markDone` and delivers
2209
+ * its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
2210
+ */
2211
+ stop() {
2212
+ this.stopped = true;
2213
+ }
2214
+ /**
2215
+ * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
2216
+ * graceful shutdown, so a turn whose reply is ready — or completes within the
2217
+ * window — is delivered before the process exits, instead of being cut off and
2218
+ * left for the ADR-0046 restart-recovery path.
2219
+ *
2220
+ * Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
2221
+ * far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
2222
+ * window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
2223
+ * set empties OR the timeout elapses. Anything still in flight at the timeout is
2224
+ * safe to abandon — it stays `processing` server-side and is re-adopted on the
2225
+ * next runner start (ADR-0046).
2226
+ *
2227
+ * @returns true if all in-flight work settled within the window; false if the
2228
+ * timeout elapsed with work still in flight.
2229
+ */
2230
+ async waitForInFlight(timeoutMs) {
2231
+ const deadline = this.now() + timeoutMs;
2232
+ const step = Math.min(this.pausedPollIntervalMs, 250);
2233
+ if (this.activeDrain) {
2234
+ let drainSettled = false;
2235
+ void this.activeDrain.then(() => {
2236
+ drainSettled = true;
2237
+ });
2238
+ while (!drainSettled) {
2239
+ if (this.now() >= deadline) return false;
2240
+ await this.sleep(step);
2241
+ }
2242
+ }
2243
+ while (this.hasInFlightWatchers()) {
2244
+ if (this.now() >= deadline) return false;
2245
+ await this.sleep(step);
2246
+ }
2247
+ return true;
1600
2248
  }
1601
2249
  /**
1602
- * Await all outstanding paused-session watchers (WI-2-CLI).
2250
+ * Await all outstanding per-session watchers (WI-3).
1603
2251
  *
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.
2252
+ * In production the watcher loops are deliberately started-not-awaited so the
2253
+ * drain loop never blocks on them and process exit is not held up (the cron
2254
+ * recovers any abandoned ones). This helper exists primarily for deterministic
2255
+ * tests that need to observe a watcher's effect (the `processing`/`done` PATCH
2256
+ * or its giving up) after a non-blocking `drainPending`. Watcher loops never
2257
+ * reject, so this resolves.
1609
2258
  */
1610
2259
  async flushPausedWatchers() {
1611
- await Promise.all([...this.watchers.values()]);
2260
+ while (true) {
2261
+ const loops = [...this.watchers.values()].map((w) => w.loop).filter((l) => l != null);
2262
+ if (loops.length === 0) return;
2263
+ await Promise.all(loops);
2264
+ const stillLive = [...this.watchers.values()].some((w) => w.loop != null);
2265
+ if (!stillLive) return;
2266
+ }
1612
2267
  }
1613
- // -------------------------------------------------------------------------
1614
- // Conversation processing
1615
- // -------------------------------------------------------------------------
2268
+ // Conversation processing (WI-3 — async dispatch)
2269
+ /**
2270
+ * Dispatch each pending message for a conversation to opencode's native queue
2271
+ * via `prompt_async` (Task 3.2) and register it with the conversation's
2272
+ * per-session watcher. Does NOT block on the turn and does NOT call
2273
+ * `markProcessing` here — that fires from the watcher on running-start.
2274
+ *
2275
+ * @returns the count of messages NEWLY dispatched (not already in-flight).
2276
+ */
1616
2277
  async processConversation(conv) {
1617
2278
  const sessionId = await this.ensureSession(conv);
1618
2279
  const messages = await this.getPendingMessages(conv.id);
1619
- let processed = 0;
2280
+ let dispatched = 0;
2281
+ let skippedAlreadyDispatched = 0;
1620
2282
  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
- });
2283
+ if (this.stopped) break;
2284
+ if (this.dispatched.has(message.id)) {
2285
+ skippedAlreadyDispatched += 1;
1629
2286
  continue;
1630
2287
  }
2288
+ const options = {
2289
+ agent: message.opencode_agent ?? void 0,
2290
+ model: message.opencode_model ?? void 0
2291
+ };
2292
+ let opencodeMessageId;
1631
2293
  try {
1632
2294
  this.log({
1633
2295
  level: "info",
1634
- message: `Sending queued message ${message.id.slice(0, 8)} to OpenCode (session ${sessionId.slice(0, 8)})`,
2296
+ message: `Dispatching message ${message.id.slice(0, 8)} to OpenCode native queue (session ${sessionId.slice(0, 8)})`,
1635
2297
  conversation_id: conv.id,
1636
2298
  message_id: message.id
1637
2299
  });
1638
- const result = await sendMessageToOpenCode(
1639
- this.port,
2300
+ const sendAttachments = this.buildSendAttachments(conv, message);
2301
+ opencodeMessageId = await this.dispatchLocked(
1640
2302
  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
- }
2303
+ () => sendPromptAsync(this.port, sessionId, message.content, options, sendAttachments)
1650
2304
  );
1651
- if (result.awaitingInteraction) {
2305
+ } catch (err) {
2306
+ if (err instanceof ChannelAuthError) throw err;
2307
+ this.dispatched.delete(message.id);
2308
+ if (await sessionExists(this.port, sessionId) === false) {
2309
+ this.sessions.delete(conv.id);
1652
2310
  this.log({
1653
- level: "info",
1654
- message: `Message ${message.id.slice(0, 8)} paused awaiting interaction \u2014 watching session for completion`,
2311
+ level: "warn",
2312
+ 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
2313
  conversation_id: conv.id,
1656
2314
  message_id: message.id
1657
2315
  });
1658
- this.startPausedWatcher(conv, message, sessionId);
1659
- continue;
2316
+ break;
1660
2317
  }
1661
- await this.confirmCompletion(sessionId);
1662
- await this.markDone(conv.id, message.id, sessionId);
1663
- processed += 1;
1664
- this.log({
1665
- level: "info",
1666
- message: `Message ${message.id.slice(0, 8)} processed`,
1667
- conversation_id: conv.id,
1668
- message_id: message.id
1669
- });
1670
- } catch (err) {
1671
- if (err instanceof ChannelAuthError) throw err;
1672
2318
  await this.markFailed(conv.id, message.id).catch(() => {
1673
2319
  });
1674
2320
  this.log({
1675
2321
  level: "error",
1676
- message: `Message ${message.id.slice(0, 8)} failed: ${err instanceof Error ? err.message : String(err)}`,
2322
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${err instanceof Error ? err.message : String(err)}`,
1677
2323
  conversation_id: conv.id,
1678
2324
  message_id: message.id
1679
2325
  });
2326
+ continue;
2327
+ }
2328
+ if (opencodeMessageId === null) {
2329
+ this.log({
2330
+ level: "warn",
2331
+ 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`,
2332
+ conversation_id: conv.id,
2333
+ message_id: message.id
2334
+ });
2335
+ continue;
2336
+ }
2337
+ this.dispatched.add(message.id);
2338
+ this.registerInFlight(conv, sessionId, message, opencodeMessageId);
2339
+ dispatched += 1;
2340
+ void this.postSignal(conv.id, message.id, "dispatched");
2341
+ }
2342
+ if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
2343
+ this.log({
2344
+ level: "warn",
2345
+ 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).`,
2346
+ conversation_id: conv.id
2347
+ });
2348
+ }
2349
+ this.ensureWatcherRunning(sessionId);
2350
+ return dispatched;
2351
+ }
2352
+ async ensureSession(conv) {
2353
+ const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
2354
+ if (bound) {
2355
+ const exists = await sessionExists(this.port, bound);
2356
+ if (exists === false) {
2357
+ this.log({
2358
+ level: "debug",
2359
+ 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.`,
2360
+ conversation_id: conv.id
2361
+ });
2362
+ this.sessions.delete(conv.id);
2363
+ return this.createAndBindSession(conv.id);
2364
+ }
2365
+ this.sessions.set(conv.id, bound);
2366
+ return bound;
2367
+ }
2368
+ return this.createAndBindSession(conv.id);
2369
+ }
2370
+ /**
2371
+ * Create a fresh OpenCode session for a conversation, cache the binding, and
2372
+ * best-effort persist it server-side. Shared by the first-ever bind and the
2373
+ * self-heal recreate path in `ensureSession`.
2374
+ */
2375
+ async createAndBindSession(conversationId) {
2376
+ const directory = await this.resolveOpenCodeDirectory();
2377
+ const sessionId = await createOpenCodeSession(this.port, directory);
2378
+ this.sessions.set(conversationId, sessionId);
2379
+ await this.persistSession(conversationId, sessionId).catch(() => {
2380
+ });
2381
+ return sessionId;
2382
+ }
2383
+ /**
2384
+ * Lazily resolve (and cache) opencode's root directory via `GET /path`.
2385
+ * Resolved once per driver: `undefined` until first lookup, then the directory
2386
+ * string or `null` if unavailable (we don't keep retrying a missing `/path`).
2387
+ */
2388
+ async resolveOpenCodeDirectory() {
2389
+ if (this.opencodeDirectory !== void 0) return this.opencodeDirectory;
2390
+ this.opencodeDirectory = await getOpenCodeDirectory(this.port);
2391
+ if (!this.opencodeDirectory) {
2392
+ this.log({
2393
+ level: "warn",
2394
+ message: "Could not determine opencode directory (GET /path) \u2014 new sessions may not appear in opencode web"
2395
+ });
2396
+ }
2397
+ return this.opencodeDirectory;
2398
+ }
2399
+ // Per-session watcher (WI-3)
2400
+ /**
2401
+ * Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
2402
+ * opencode session (Task 2.1a), so two dispatches into the SAME session can
2403
+ * never interleave and mis-correlate their read-backs. Distinct sessions run
2404
+ * concurrently. The chained tail intentionally ignores the prior result/error
2405
+ * (each dispatch reports its own outcome to its caller).
2406
+ */
2407
+ dispatchLocked(sessionId, fn) {
2408
+ const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
2409
+ const run2 = prior.then(fn, fn);
2410
+ this.sessionDispatchLocks.set(
2411
+ sessionId,
2412
+ run2.then(
2413
+ () => void 0,
2414
+ () => void 0
2415
+ )
2416
+ );
2417
+ return run2;
2418
+ }
2419
+ // Inbound image attachments (#255, WI-8)
2420
+ /**
2421
+ * Build the `SendAttachmentsInput` for a message's inbound images, or
2422
+ * `undefined` when the message has none (so a text-only turn is unchanged).
2423
+ *
2424
+ * The driver OWNS the two channel-facing concerns the session module cannot:
2425
+ * - the AUTHENTICATED byte fetch through Evident's WI-6 endpoint
2426
+ * (`fetchAttachmentDataUrl`), using the SAME `getAuthHeader()` as every
2427
+ * other combinedAuth callback — the CLI NEVER talks to Slack directly;
2428
+ * - the in-thread SKIP NOTE (`signalAttachmentsSkipped`) posted over the
2429
+ * existing callback surface when any image was skipped/failed.
2430
+ * `sendPromptAsync` applies the capability gate + appends the `file` parts and
2431
+ * reports outcomes back via `onOutcomes`.
2432
+ */
2433
+ buildSendAttachments(conv, message) {
2434
+ const refs = message.attachments;
2435
+ if (!refs || refs.length === 0) return void 0;
2436
+ return {
2437
+ inputs: refs.map((a, index) => ({
2438
+ index,
2439
+ mime: a.mime,
2440
+ ...a.filename ? { filename: a.filename } : {}
2441
+ })),
2442
+ fetchDataUrl: (index) => this.fetchAttachmentDataUrl(message.id, index, refs[index].mime),
2443
+ onOutcomes: ({ outcomes, capabilityUnknown }) => this.signalAttachmentsSkipped(conv.id, message.id, outcomes, capabilityUnknown)
2444
+ };
2445
+ }
2446
+ /**
2447
+ * Fetch ONE inbound image's bytes through Evident's WI-6 endpoint
2448
+ * (`GET {apiUrl}/agents/{agentId}/attachments/{messageId}/{index}`) using the
2449
+ * existing authenticated fetch, and base64-encode into a
2450
+ * `data:<mime>;base64,<…>` URL for the opencode `file` part's `url`.
2451
+ *
2452
+ * The endpoint streams the source bytes verbatim (200), or returns 404
2453
+ * (not-owned / out-of-range / deleted-at-source / workspace gone) / 413
2454
+ * (over-cap). On ANY non-2xx or thrown failure we return `null` so the caller
2455
+ * OMITS that one image and the text turn still sends — NEVER throws the turn.
2456
+ * Failures are logged with context (no silent swallow).
2457
+ */
2458
+ async fetchAttachmentDataUrl(messageId, index, mime) {
2459
+ try {
2460
+ const res = await this.fetchImpl(
2461
+ `${this.apiUrl}/agents/${this.agentId}/attachments/${messageId}/${index}`,
2462
+ { headers: { Authorization: this.getAuthHeader() } }
2463
+ );
2464
+ if (!res.ok) {
2465
+ this.log({
2466
+ level: "error",
2467
+ message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} returned HTTP ${res.status} \u2014 omitting this image (text turn proceeds)`,
2468
+ message_id: messageId
2469
+ });
2470
+ return null;
2471
+ }
2472
+ const buf = await res.arrayBuffer();
2473
+ const base64 = Buffer.from(buf).toString("base64");
2474
+ const dataMime = cleanImageMime(res.headers.get("content-type")) || mime;
2475
+ return `data:${dataMime};base64,${base64}`;
2476
+ } catch (err) {
2477
+ this.log({
2478
+ level: "error",
2479
+ message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} failed \u2014 omitting this image (text turn proceeds): ${err instanceof Error ? err.message : String(err)}`,
2480
+ message_id: messageId
2481
+ });
2482
+ return null;
2483
+ }
2484
+ }
2485
+ /**
2486
+ * On any skipped/failed image, post an in-thread note to Evident over the
2487
+ * EXISTING combinedAuth callback surface — the CLI NEVER posts to Slack directly.
2488
+ * Evident routes the note to source via `conversation.deliver`.
2489
+ *
2490
+ * The `POST .../messages/:id/signal` route accepts `attachments_skipped` (in
2491
+ * `messageSignalSchema`) and turns it into an in-thread note delivered through
2492
+ * `conversation.deliver` (e.g. "N image(s) couldn't be forwarded"), so the note
2493
+ * reaches the channel.
2494
+ *
2495
+ * Fire-and-forget: never throws into the send/tick (logs its own failure).
2496
+ */
2497
+ signalAttachmentsSkipped(conversationId, messageId, outcomes, capabilityUnknown) {
2498
+ const skipped = outcomes.filter((o) => o.status === "skipped").length;
2499
+ const failed = outcomes.filter((o) => o.status === "failed").length;
2500
+ if (skipped === 0 && failed === 0) return;
2501
+ if (this.attachmentsSkippedSignalled.has(messageId)) return;
2502
+ this.attachmentsSkippedSignalled.add(messageId);
2503
+ const skippedReason = capabilityUnknown ? "unknown" : "unsupported";
2504
+ this.log({
2505
+ level: "info",
2506
+ message: `Message ${messageId.slice(0, 8)}: ${skipped} image(s) skipped (${capabilityUnknown ? "capability was unreadable \u2014 failed open to text-only" : "model not attachment-capable"}), ${failed} image(s) unavailable (deleted-at-source or fetch failure) \u2014 noting to Evident`,
2507
+ conversation_id: conversationId,
2508
+ message_id: messageId
2509
+ });
2510
+ void this.postSignal(conversationId, messageId, "attachments_skipped", {
2511
+ skipped,
2512
+ failed,
2513
+ ...skipped > 0 ? { skipped_reason: skippedReason } : {}
2514
+ });
2515
+ }
2516
+ /** Register a freshly-dispatched message with its session's watcher state. */
2517
+ registerInFlight(conv, sessionId, message, opencodeMessageId) {
2518
+ let watcher = this.watchers.get(sessionId);
2519
+ if (!watcher) {
2520
+ watcher = {
2521
+ conv,
2522
+ inFlight: /* @__PURE__ */ new Map(),
2523
+ loop: null,
2524
+ reportedQuestions: /* @__PURE__ */ new Set(),
2525
+ reportedPermissions: /* @__PURE__ */ new Set(),
2526
+ lastGoodPollAt: this.now(),
2527
+ hadUsablePoll: false
2528
+ };
2529
+ this.watchers.set(sessionId, watcher);
2530
+ }
2531
+ const now = this.now();
2532
+ watcher.inFlight.set(message.id, {
2533
+ evidentMessageId: message.id,
2534
+ opencodeMessageId,
2535
+ message,
2536
+ dispatchedAt: now,
2537
+ processingAnchorMs: now,
2538
+ deadline: now + this.pausedMaxWaitMs,
2539
+ started: false,
2540
+ done: false,
2541
+ stuckReported: false,
2542
+ lastAliveAt: 0,
2543
+ aliveInFlight: false,
2544
+ awaitingHumanLatched: false,
2545
+ pausedOnQuestion: false,
2546
+ pausedOnPermission: false,
2547
+ pausedClearConfirmed: false,
2548
+ pausedInFlight: false,
2549
+ deliveryDeadlineAnchored: false
2550
+ });
2551
+ }
2552
+ /**
2553
+ * Register a RE-ADOPTED `processing` message with its session watcher
2554
+ * (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
2555
+ * `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
2556
+ * `now`, so the paused/queued/unreachable cases settle on the same wall-clock a
2557
+ * fresh dispatch would (10 min after `processed_at`, not 10 min from now).
2558
+ *
2559
+ * This re-attaches into the SAME watcher, so the ADR-0047 progressing-vs-paused
2560
+ * give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
2561
+ * opencode reports ACTIVELY `running` is watched to completion (its liveness
2562
+ * heartbeat keeps the cron off its row), while a re-adopted turn that is paused
2563
+ * awaiting a human — or queued/unreachable — is still bounded by `deadline` and
2564
+ * handed to the cron. The old "the `deadline` must settle before the ~15-min
2565
+ * cron or they double-drive" reasoning is superseded: liveness now settles the
2566
+ * actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
2567
+ * (only the appear-guard uses it).
2568
+ *
2569
+ * `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
2570
+ * `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
2571
+ * fresh-run path these differ (a fresh opencode id under the same server row).
2572
+ *
2573
+ * `started` is set true so the watcher does NOT re-`markProcessing` a row the
2574
+ * server already flipped to `processing`; the running/done transitions still
2575
+ * fire from the watcher's normal branches.
2576
+ */
2577
+ registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
2578
+ let watcher = this.watchers.get(sessionId);
2579
+ if (!watcher) {
2580
+ watcher = {
2581
+ conv,
2582
+ inFlight: /* @__PURE__ */ new Map(),
2583
+ loop: null,
2584
+ reportedQuestions: /* @__PURE__ */ new Set(),
2585
+ reportedPermissions: /* @__PURE__ */ new Set(),
2586
+ lastGoodPollAt: this.now(),
2587
+ hadUsablePoll: false
2588
+ };
2589
+ this.watchers.set(sessionId, watcher);
2590
+ }
2591
+ watcher.inFlight.set(message.id, {
2592
+ evidentMessageId: message.id,
2593
+ opencodeMessageId,
2594
+ message,
2595
+ dispatchedAt: this.now(),
2596
+ // Anchor the absolute-age ceiling to the SERVER-SIDE `processed_at` (the same
2597
+ // value seeding `deadline`), NOT `dispatchedAt` — so a re-adopted zombie's age
2598
+ // reflects the real turn duration and the ceiling fires on the ORIGINAL turn.
2599
+ processingAnchorMs: processedAtMs,
2600
+ deadline: processedAtMs + this.pausedMaxWaitMs,
2601
+ // The server row is ALREADY `processing`; do not re-fire markProcessing.
2602
+ started: true,
2603
+ done: false,
2604
+ // Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
2605
+ // AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
2606
+ // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2607
+ // re-adopted row left wedged in `queued` still emits the signal once
2608
+ // (#210/#220 observability).
2609
+ stuckReported: false,
2610
+ // Task 5.2: a re-adopted actively-running row re-attaches into the SAME
2611
+ // watcher and so hits the SAME actively-running heartbeat branch in
2612
+ // `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
2613
+ // re-adopted and is confirming this row alive" via that `alive` heartbeat,
2614
+ // with no extra `re_adopted` signal needed (folds old WI-6).
2615
+ lastAliveAt: 0,
2616
+ aliveInFlight: false,
2617
+ awaitingHumanLatched: false,
2618
+ pausedOnQuestion: false,
2619
+ pausedOnPermission: false,
2620
+ pausedClearConfirmed: false,
2621
+ pausedInFlight: false,
2622
+ deliveryDeadlineAnchored: false
2623
+ });
2624
+ }
2625
+ /**
2626
+ * Start (but do NOT await) the per-session watcher loop if it has in-flight
2627
+ * work and is not already running. Single-flight per session. The loop is
2628
+ * tracked on the watcher and cleared when it settles; it never rejects (fully
2629
+ * guarded), so a failed poll/callback can never crash the run loop — the cron
2630
+ * stays as the safety net.
2631
+ */
2632
+ ensureWatcherRunning(sessionId) {
2633
+ const watcher = this.watchers.get(sessionId);
2634
+ if (!watcher) return;
2635
+ if (watcher.loop) return;
2636
+ if (watcher.inFlight.size === 0) {
2637
+ this.watchers.delete(sessionId);
2638
+ return;
2639
+ }
2640
+ const loop = this.runWatcherLoop(sessionId, watcher).finally(() => {
2641
+ watcher.loop = null;
2642
+ if (watcher.inFlight.size === 0) {
2643
+ this.watchers.delete(sessionId);
2644
+ }
2645
+ });
2646
+ watcher.loop = loop;
2647
+ }
2648
+ /**
2649
+ * The per-session polling loop (WI-3). Once per tick it:
2650
+ * 1. polls `GET /session/:id/message` once and, per in-flight message,
2651
+ * computes `messageRunState` and fires markProcessing (queued→running) /
2652
+ * markDone (done) exactly once per transition;
2653
+ * 2. applies the idle-path re-dispatch guard (a dispatched message that never
2654
+ * APPEARS → re-dispatch — D1 obligation 2);
2655
+ * 3. polls `/question` + `/permission` (scoped to the session) and surfaces
2656
+ * NEW ones via `reportInteraction`, carrying the PAUSED message's own
2657
+ * `source_message_id`;
2658
+ * 4. drops messages that completed or timed out from the in-flight set.
2659
+ * Exits when the in-flight set empties. Never throws.
2660
+ */
2661
+ async runWatcherLoop(sessionId, watcher) {
2662
+ try {
2663
+ while (watcher.inFlight.size > 0) {
2664
+ await this.sleep(this.pausedPollIntervalMs);
2665
+ let messages = null;
2666
+ try {
2667
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
2668
+ if (res.ok) {
2669
+ const body = await res.json();
2670
+ messages = Array.isArray(body) ? body : null;
2671
+ }
2672
+ } catch {
2673
+ }
2674
+ if (messages != null && messages.length > 0) {
2675
+ watcher.lastGoodPollAt = this.now();
2676
+ watcher.hadUsablePoll = true;
2677
+ } else {
2678
+ const emptyButReachable = messages != null;
2679
+ const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
2680
+ if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
2681
+ continue;
2682
+ }
2683
+ }
2684
+ const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
2685
+ for (const inFlight of [...watcher.inFlight.values()]) {
2686
+ await this.serviceInFlightMessage(
2687
+ sessionId,
2688
+ watcher,
2689
+ inFlight,
2690
+ messages,
2691
+ openQuestions,
2692
+ openPermissions,
2693
+ questionsPolledOk,
2694
+ permissionsPolledOk
2695
+ );
2696
+ }
2697
+ }
2698
+ } catch (err) {
2699
+ if (err instanceof ChannelAuthError) {
2700
+ this.log({
2701
+ level: "error",
2702
+ 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}`,
2703
+ conversation_id: watcher.conv.id
2704
+ });
2705
+ for (const evidentMessageId of [...watcher.inFlight.keys()]) {
2706
+ this.readopted.delete(evidentMessageId);
2707
+ this.removeInFlight(watcher, evidentMessageId);
2708
+ }
2709
+ return;
2710
+ }
2711
+ this.log({
2712
+ level: "error",
2713
+ message: `Session watcher failed for session ${sessionId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
2714
+ conversation_id: watcher.conv.id
2715
+ });
2716
+ }
2717
+ }
2718
+ /**
2719
+ * On FIRST observing a terminal (done/failed) state, ensure the delivery
2720
+ * (markDone/markFailed) transient-retry path has a real window. A long
2721
+ * ACTIVELY-running turn is kept past its original `deadline`, so by completion
2722
+ * `now >= deadline` already holds and the retry bound below would fire on the
2723
+ * first transient PATCH failure — dropping the message before its reply lands
2724
+ * (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
2725
+ * to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
2726
+ * or past now, so a still-ample window is left untouched.
2727
+ */
2728
+ anchorDeliveryDeadline(inFlight) {
2729
+ if (inFlight.deliveryDeadlineAnchored) return;
2730
+ inFlight.deliveryDeadlineAnchored = true;
2731
+ if (this.now() >= inFlight.deadline) {
2732
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2733
+ }
2734
+ }
2735
+ /**
2736
+ * Drive ONE in-flight message's lifecycle from the tick's message snapshot.
2737
+ * Fires markProcessing on queued→running and markDone on done (each once),
2738
+ * applies the idle-path re-dispatch guard, and removes the message from the
2739
+ * in-flight set on completion or timeout.
2740
+ */
2741
+ async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
2742
+ const conv = watcher.conv;
2743
+ const state = messageRunState(messages, inFlight.opencodeMessageId);
2744
+ const id = inFlight.evidentMessageId;
2745
+ if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
2746
+ else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
2747
+ if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
2748
+ else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
2749
+ const observedOpen = openQuestions.has(id) || openPermissions.has(id);
2750
+ const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
2751
+ const awaitingHuman = observedOpen || latchedPaused;
2752
+ if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
2753
+ const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
2754
+ let claimed;
2755
+ try {
2756
+ claimed = await this.markProcessing(
2757
+ conv.id,
2758
+ inFlight.evidentMessageId,
2759
+ sessionId,
2760
+ inFlight.opencodeMessageId,
2761
+ title
2762
+ );
2763
+ } catch (err) {
2764
+ if (err instanceof ChannelAuthError) throw err;
2765
+ this.log({
2766
+ level: "warn",
2767
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2768
+ conversation_id: conv.id,
2769
+ message_id: inFlight.evidentMessageId
2770
+ });
2771
+ return;
2772
+ }
2773
+ inFlight.started = true;
2774
+ if (!claimed) {
2775
+ this.log({
2776
+ level: "debug",
2777
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
2778
+ conversation_id: conv.id,
2779
+ message_id: inFlight.evidentMessageId
2780
+ });
2781
+ }
2782
+ }
2783
+ if (state === "done") {
2784
+ this.anchorDeliveryDeadline(inFlight);
2785
+ if (!inFlight.done) {
2786
+ this.log({
2787
+ level: "info",
2788
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
2789
+ conversation_id: conv.id,
2790
+ message_id: inFlight.evidentMessageId
2791
+ });
2792
+ const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
2793
+ try {
2794
+ await this.markDone(
2795
+ conv.id,
2796
+ inFlight.evidentMessageId,
2797
+ sessionId,
2798
+ inFlight.opencodeMessageId,
2799
+ title
2800
+ );
2801
+ } catch (err) {
2802
+ if (err instanceof ChannelAuthError) throw err;
2803
+ if (err instanceof ChannelTerminalError) {
2804
+ this.log({
2805
+ level: "warn",
2806
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2807
+ conversation_id: conv.id,
2808
+ message_id: inFlight.evidentMessageId
2809
+ });
2810
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2811
+ return;
2812
+ }
2813
+ if (this.now() >= inFlight.deadline) {
2814
+ this.log({
2815
+ level: "warn",
2816
+ 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)}`,
2817
+ conversation_id: conv.id,
2818
+ message_id: inFlight.evidentMessageId
2819
+ });
2820
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2821
+ return;
2822
+ }
2823
+ this.log({
2824
+ level: "warn",
2825
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2826
+ conversation_id: conv.id,
2827
+ message_id: inFlight.evidentMessageId
2828
+ });
2829
+ return;
2830
+ }
2831
+ inFlight.done = true;
2832
+ }
2833
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2834
+ return;
2835
+ }
2836
+ if (state === "failed") {
2837
+ this.anchorDeliveryDeadline(inFlight);
2838
+ if (!inFlight.done) {
2839
+ const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
2840
+ this.log({
2841
+ level: "error",
2842
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2843
+ conversation_id: conv.id,
2844
+ message_id: inFlight.evidentMessageId
2845
+ });
2846
+ try {
2847
+ await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
2848
+ } catch (err) {
2849
+ if (err instanceof ChannelAuthError) throw err;
2850
+ if (err instanceof ChannelTerminalError) {
2851
+ this.log({
2852
+ level: "warn",
2853
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2854
+ conversation_id: conv.id,
2855
+ message_id: inFlight.evidentMessageId
2856
+ });
2857
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2858
+ return;
2859
+ }
2860
+ if (this.now() >= inFlight.deadline) {
2861
+ this.log({
2862
+ level: "warn",
2863
+ 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)}`,
2864
+ conversation_id: conv.id,
2865
+ message_id: inFlight.evidentMessageId
2866
+ });
2867
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2868
+ return;
2869
+ }
2870
+ this.log({
2871
+ level: "warn",
2872
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2873
+ conversation_id: conv.id,
2874
+ message_id: inFlight.evidentMessageId
2875
+ });
2876
+ return;
2877
+ }
2878
+ inFlight.done = true;
2879
+ }
2880
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2881
+ return;
2882
+ }
2883
+ const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
2884
+ const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
2885
+ if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
2886
+ inFlight.stuckReported = true;
2887
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2888
+ stuck_for_ms: this.now() - inFlight.dispatchedAt
2889
+ });
2890
+ }
2891
+ const activelyRunning = state === "running" && !awaitingHuman;
2892
+ if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
2893
+ this.log({
2894
+ level: "warn",
2895
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} exceeded the absolute processing ceiling (${Math.round((this.now() - inFlight.processingAnchorMs) / 6e4)}min, session ${sessionId}) while still actively running \u2014 releasing so the cron can reclaim it`,
2896
+ conversation_id: conv.id,
2897
+ message_id: inFlight.evidentMessageId
2898
+ });
2899
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2900
+ watched_for_ms: this.now() - inFlight.processingAnchorMs
2901
+ });
2902
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2903
+ return;
2904
+ }
2905
+ if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
2906
+ inFlight.aliveInFlight = true;
2907
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
2908
+ inFlight.aliveInFlight = false;
2909
+ if (ok) inFlight.lastAliveAt = this.now();
2910
+ });
2911
+ }
2912
+ if (awaitingHuman) {
2913
+ if (!inFlight.awaitingHumanLatched) {
2914
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2915
+ inFlight.awaitingHumanLatched = true;
2916
+ }
2917
+ if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
2918
+ inFlight.pausedInFlight = true;
2919
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
2920
+ inFlight.pausedInFlight = false;
2921
+ if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
2922
+ });
2923
+ }
2924
+ } else if (inFlight.awaitingHumanLatched) {
2925
+ inFlight.awaitingHumanLatched = false;
2926
+ inFlight.pausedOnQuestion = false;
2927
+ inFlight.pausedOnPermission = false;
2928
+ inFlight.pausedClearConfirmed = false;
2929
+ }
2930
+ const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
2931
+ const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
2932
+ (sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
2933
+ );
2934
+ const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
2935
+ if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
2936
+ this.log({
2937
+ level: "debug",
2938
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
2939
+ conversation_id: conv.id,
2940
+ message_id: inFlight.evidentMessageId
2941
+ });
2942
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2943
+ watched_for_ms: this.now() - inFlight.dispatchedAt
2944
+ });
2945
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2946
+ }
2947
+ }
2948
+ // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
2949
+ /**
2950
+ * Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
2951
+ *
2952
+ * The pending drain only re-drives `pending` rows; a message already flipped to
2953
+ * `processing` before the runner died is watched by nobody until the 15-min
2954
+ * cron resets it. Here we fetch those rows, and per row resolve its correlated
2955
+ * reply against opencode's OWN session store — completing, re-attaching, or
2956
+ * (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
2957
+ * is idempotent per message (Invariant 2): a row a watcher already tracks is
2958
+ * skipped in `readoptOne` — one driver, no double-drive.
2959
+ *
2960
+ * Only `ChannelAuthError` propagates (to `drainPending`, like the pending
2961
+ * path); every other early return LOGS a reason with context — no silent drop.
2962
+ */
2963
+ async readoptProcessing() {
2964
+ const rows = await this.getProcessingMessages();
2965
+ if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0 || this.readoptPollUnresolvedSignalled.size > 0) {
2966
+ const stillProcessing = new Set(rows.map((r) => r.id));
2967
+ for (const id of [
2968
+ ...this.dontRedispatch,
2969
+ ...this.doneUndeliverable,
2970
+ ...this.readoptPollUnresolvedSignalled
2971
+ ]) {
2972
+ if (!stillProcessing.has(id)) {
2973
+ const cleared = this.dontRedispatch.delete(id);
2974
+ const clearedUndeliverable = this.doneUndeliverable.delete(id);
2975
+ this.readoptPollUnresolvedSignalled.delete(id);
2976
+ if (cleared || clearedUndeliverable) {
2977
+ this.log({
2978
+ level: "debug",
2979
+ message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
2980
+ message_id: id
2981
+ });
2982
+ }
2983
+ }
2984
+ }
2985
+ }
2986
+ if (rows.length === 0) return;
2987
+ const bySession = /* @__PURE__ */ new Map();
2988
+ for (const row of rows) {
2989
+ if (!row.opencode_session_id) {
2990
+ this.log({
2991
+ level: "warn",
2992
+ message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
2993
+ conversation_id: row.conversation_id,
2994
+ message_id: row.id
2995
+ });
2996
+ continue;
2997
+ }
2998
+ const list = bySession.get(row.opencode_session_id) ?? [];
2999
+ list.push(row);
3000
+ bySession.set(row.opencode_session_id, list);
3001
+ }
3002
+ for (const [sessionId, sessionRows] of bySession) {
3003
+ let messages;
3004
+ try {
3005
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
3006
+ if (!res.ok) {
3007
+ this.log({
3008
+ level: "warn",
3009
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
3010
+ });
3011
+ continue;
3012
+ }
3013
+ const body = await res.json();
3014
+ if (!Array.isArray(body)) {
3015
+ this.log({
3016
+ level: "warn",
3017
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
3018
+ });
3019
+ continue;
3020
+ }
3021
+ messages = body;
3022
+ } catch (err) {
3023
+ this.log({
3024
+ level: "warn",
3025
+ message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
3026
+ });
3027
+ continue;
3028
+ }
3029
+ const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
3030
+ const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
3031
+ for (const row of sessionRows) {
3032
+ await this.readoptOne(sessionId, row, messages, sessionOngoing);
3033
+ }
3034
+ }
3035
+ }
3036
+ /**
3037
+ * Re-adopt ONE `processing` row against the tick's session message snapshot
3038
+ * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
3039
+ *
3040
+ * Branches on `messageRunState(messages, row.opencode_message_id)` — the
3041
+ * opencode-assigned user-message id persisted on the first `processing` PATCH
3042
+ * (#218). A row with a NULL stored id (dispatched but the read-back never landed
3043
+ * before the restart) has no id to correlate → treated as an orphan and
3044
+ * re-dispatched (at most once, see `forceReadoptRun`):
3045
+ * - `done` → `markDone` now (guarded like the watcher's done branch);
3046
+ * - `failed` → `markFailed` with the surfaced error (issue #182), so an
3047
+ * errored turn is reported failed on restart, NOT re-dispatched;
3048
+ * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
3049
+ * tracking the stored id so the reply correlates by it;
3050
+ * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
3051
+ *
3052
+ * Only `ChannelAuthError` propagates.
3053
+ */
3054
+ async readoptOne(sessionId, row, messages, sessionOngoing) {
3055
+ if (this.isTracked(sessionId, row.id)) {
3056
+ this.log({
3057
+ level: "debug",
3058
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
3059
+ conversation_id: row.conversation_id,
3060
+ message_id: row.id
3061
+ });
3062
+ return;
3063
+ }
3064
+ const ocId = row.opencode_message_id;
3065
+ const state = messageRunState(messages, ocId ?? "");
3066
+ if (state === "done") {
3067
+ if (this.doneUndeliverable.has(row.id)) {
3068
+ this.log({
3069
+ level: "debug",
3070
+ message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
3071
+ conversation_id: row.conversation_id,
3072
+ message_id: row.id
3073
+ });
3074
+ return;
1680
3075
  }
3076
+ this.log({
3077
+ level: "info",
3078
+ message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
3079
+ conversation_id: row.conversation_id,
3080
+ message_id: row.id
3081
+ });
3082
+ try {
3083
+ const title = await this.resolveSessionTitle(sessionId, row.conversation_id);
3084
+ await this.markDone(row.conversation_id, row.id, sessionId, ocId, title);
3085
+ } catch (err) {
3086
+ if (err instanceof ChannelAuthError) throw err;
3087
+ if (err instanceof ChannelTerminalError) {
3088
+ this.doneUndeliverable.add(row.id);
3089
+ this.log({
3090
+ level: "warn",
3091
+ 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}`,
3092
+ conversation_id: row.conversation_id,
3093
+ message_id: row.id
3094
+ });
3095
+ void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
3096
+ return;
3097
+ }
3098
+ this.log({
3099
+ level: "warn",
3100
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3101
+ conversation_id: row.conversation_id,
3102
+ message_id: row.id
3103
+ });
3104
+ return;
3105
+ }
3106
+ this.dontRedispatch.delete(row.id);
3107
+ void this.postSignal(row.conversation_id, row.id, "readopt_done");
3108
+ return;
3109
+ }
3110
+ if (state === "failed") {
3111
+ const error2 = messageError(messages, ocId ?? "") ?? void 0;
3112
+ this.log({
3113
+ level: "error",
3114
+ message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
3115
+ conversation_id: row.conversation_id,
3116
+ message_id: row.id
3117
+ });
3118
+ try {
3119
+ await this.markFailed(row.conversation_id, row.id, sessionId, error2);
3120
+ } catch (err) {
3121
+ if (err instanceof ChannelAuthError) throw err;
3122
+ if (err instanceof ChannelTerminalError) {
3123
+ this.doneUndeliverable.add(row.id);
3124
+ this.log({
3125
+ level: "warn",
3126
+ 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}`,
3127
+ conversation_id: row.conversation_id,
3128
+ message_id: row.id
3129
+ });
3130
+ void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
3131
+ return;
3132
+ }
3133
+ this.log({
3134
+ level: "warn",
3135
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3136
+ conversation_id: row.conversation_id,
3137
+ message_id: row.id
3138
+ });
3139
+ return;
3140
+ }
3141
+ this.dontRedispatch.delete(row.id);
3142
+ void this.postSignal(row.conversation_id, row.id, "readopt_failed");
3143
+ return;
3144
+ }
3145
+ if (this.dontRedispatch.has(row.id)) {
3146
+ this.log({
3147
+ level: "debug",
3148
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
3149
+ conversation_id: row.conversation_id,
3150
+ message_id: row.id
3151
+ });
3152
+ return;
3153
+ }
3154
+ let statusReadableOngoing = null;
3155
+ if (state === "running" && ocId) {
3156
+ const reply = findLastAssistantReplyFor(messages, ocId);
3157
+ const shape = this.replyCompletionShape(reply);
3158
+ const ongoing = sessionOngoing;
3159
+ statusReadableOngoing = ongoing;
3160
+ if (ongoing === false) {
3161
+ this.log({
3162
+ level: "info",
3163
+ message: `Re-adopt: message ${row.id.slice(0, 8)} running/${shape} but session ${sessionId.slice(0, 8)} is not-ongoing per GET /session/status (absent/idle) \u2014 re-dispatching from scratch (status-gated recovery)`,
3164
+ conversation_id: row.conversation_id,
3165
+ message_id: row.id
3166
+ });
3167
+ await this.forceReadoptRun(sessionId, row);
3168
+ return;
3169
+ }
3170
+ if (ongoing === true) {
3171
+ this.log({
3172
+ level: "debug",
3173
+ message: `Re-adopt: message ${row.id.slice(0, 8)} running/${shape} and session ${sessionId.slice(0, 8)} is ongoing per GET /session/status (busy/retry) \u2014 re-attaching watcher (no re-dispatch)`,
3174
+ conversation_id: row.conversation_id,
3175
+ message_id: row.id
3176
+ });
3177
+ } else {
3178
+ if (shape === "b1") {
3179
+ this.log({
3180
+ level: "debug",
3181
+ message: `Re-adopt: message ${row.id.slice(0, 8)} running/b1 but GET /session/status was unreadable (null) for session ${sessionId.slice(0, 8)} \u2014 NOT latching a b1 row on a transient status blip; leaving it un-tracked to re-evaluate on the next drain`,
3182
+ conversation_id: row.conversation_id,
3183
+ message_id: row.id
3184
+ });
3185
+ if (!this.readoptPollUnresolvedSignalled.has(row.id)) {
3186
+ this.readoptPollUnresolvedSignalled.add(row.id);
3187
+ void this.postSignal(row.conversation_id, row.id, "readopt_poll_unresolved");
3188
+ }
3189
+ return;
3190
+ }
3191
+ this.log({
3192
+ level: "debug",
3193
+ message: `Re-adopt: message ${row.id.slice(0, 8)} running/${shape} but GET /session/status was unreadable (null) for session ${sessionId.slice(0, 8)} \u2014 falling back to the #253 preamble + descendant cross-check`,
3194
+ conversation_id: row.conversation_id,
3195
+ message_id: row.id
3196
+ });
3197
+ }
3198
+ }
3199
+ if (statusReadableOngoing === null && state === "running" && ocId && isPreamblePinnedRunning(messages, ocId)) {
3200
+ const descendantAlive = await this.isAnyDescendantSessionAlive(sessionId);
3201
+ if (descendantAlive === true) {
3202
+ this.log({
3203
+ level: "debug",
3204
+ message: `Re-adopt: message ${row.id.slice(0, 8)} preamble-pinned running (root ${sessionId.slice(0, 8)}) but a live descendant sub-agent session was found \u2014 treating as still running, re-attaching watcher (no re-dispatch)`,
3205
+ conversation_id: row.conversation_id,
3206
+ message_id: row.id
3207
+ });
3208
+ } else {
3209
+ this.log({
3210
+ level: "info",
3211
+ message: `Re-adopt: message ${row.id.slice(0, 8)} preamble-pinned on recovery (root ${sessionId.slice(0, 8)}), no live descendant runner \u2014 re-dispatching from scratch${descendantAlive === null ? " (descendant liveness indeterminate; a restart guarantees no live runner, so this does NOT block the re-dispatch)" : ""}`,
3212
+ conversation_id: row.conversation_id,
3213
+ message_id: row.id
3214
+ });
3215
+ await this.forceReadoptRun(sessionId, row);
3216
+ return;
3217
+ }
3218
+ }
3219
+ if ((state === "running" || state === "queued") && ocId) {
3220
+ const conv = this.convForRow(sessionId, row);
3221
+ const message = this.queuedMessageForRow(row);
3222
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
3223
+ this.dispatched.add(row.id);
3224
+ this.readopted.add(row.id);
3225
+ this.ensureWatcherRunning(sessionId);
3226
+ this.log({
3227
+ level: "debug",
3228
+ message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
3229
+ conversation_id: row.conversation_id,
3230
+ message_id: row.id
3231
+ });
3232
+ void this.postSignal(row.conversation_id, row.id, "readopt_reattached");
3233
+ return;
3234
+ }
3235
+ await this.forceReadoptRun(sessionId, row);
3236
+ }
3237
+ /**
3238
+ * Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
3239
+ *
3240
+ * #218/WI-5: the row's user message is absent (never kept, or a null stored id),
3241
+ * so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
3242
+ * read it back, and register the watcher under the assigned id so the reply
3243
+ * correlates server-side.
3244
+ *
3245
+ * ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
3246
+ * id). Without a guard, if this dispatches on tick N but the read-back+persist
3247
+ * hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
3248
+ * tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
3249
+ * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
3250
+ * short-circuit while the row is latched; clear it on a successful dispatch (the
3251
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
3252
+ * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
3253
+ * may retry exactly once more).
3254
+ *
3255
+ * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
3256
+ * `processed_at` (Invariant 1).
3257
+ */
3258
+ async forceReadoptRun(sessionId, row) {
3259
+ if (this.stopped) {
3260
+ this.log({
3261
+ level: "debug",
3262
+ 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`,
3263
+ conversation_id: row.conversation_id,
3264
+ message_id: row.id
3265
+ });
3266
+ return;
3267
+ }
3268
+ if (this.awaitingReadopt.has(row.id)) {
3269
+ this.log({
3270
+ level: "debug",
3271
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
3272
+ conversation_id: row.conversation_id,
3273
+ message_id: row.id
3274
+ });
3275
+ return;
3276
+ }
3277
+ if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
3278
+ this.dontRedispatch.add(row.id);
3279
+ this.log({
3280
+ level: "debug",
3281
+ 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)`,
3282
+ conversation_id: row.conversation_id,
3283
+ message_id: row.id
3284
+ });
3285
+ void this.postSignal(row.conversation_id, row.id, "readopt_window_elapsed");
3286
+ return;
1681
3287
  }
1682
- return processed;
1683
- }
1684
- 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;
3288
+ const options = {
3289
+ agent: row.opencode_agent ?? void 0,
3290
+ model: row.opencode_model ?? void 0
3291
+ };
3292
+ this.log({
3293
+ level: "info",
3294
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
3295
+ conversation_id: row.conversation_id,
3296
+ message_id: row.id
3297
+ });
3298
+ this.awaitingReadopt.add(row.id);
3299
+ const readoptConv = this.convForRow(sessionId, row);
3300
+ const readoptMessage = this.queuedMessageForRow(row);
3301
+ const sendAttachments = this.buildSendAttachments(readoptConv, readoptMessage);
3302
+ let ocId;
3303
+ try {
3304
+ ocId = await this.dispatchLocked(
3305
+ sessionId,
3306
+ () => sendPromptAsync(this.port, sessionId, row.content, options, sendAttachments)
3307
+ );
3308
+ } catch (err) {
3309
+ this.awaitingReadopt.delete(row.id);
3310
+ if (err instanceof ChannelAuthError) throw err;
3311
+ this.log({
3312
+ level: "warn",
3313
+ message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3314
+ conversation_id: row.conversation_id,
3315
+ message_id: row.id
3316
+ });
3317
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3318
+ return;
1690
3319
  }
1691
- const directory = await this.resolveOpenCodeDirectory();
1692
- const sessionId = await createOpenCodeSession(this.port, directory);
1693
- this.sessions.set(conv.id, sessionId);
1694
- await this.persistSession(conv.id, sessionId).catch(() => {
3320
+ if (ocId === null) {
3321
+ this.awaitingReadopt.delete(row.id);
3322
+ this.log({
3323
+ level: "warn",
3324
+ 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`,
3325
+ conversation_id: row.conversation_id,
3326
+ message_id: row.id
3327
+ });
3328
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3329
+ return;
3330
+ }
3331
+ this.registerReadopted(readoptConv, sessionId, readoptMessage, ocId, this.processedAtMs(row));
3332
+ this.dispatched.add(row.id);
3333
+ this.readopted.add(row.id);
3334
+ this.awaitingReadopt.delete(row.id);
3335
+ this.ensureWatcherRunning(sessionId);
3336
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
3337
+ }
3338
+ /**
3339
+ * True if `evidentMessageId` is already being driven — either in the
3340
+ * authoritative `dispatched` set or a live watcher's in-flight set for this
3341
+ * session (Invariant 2, WI-5). Either signal means a watcher owns the row.
3342
+ */
3343
+ isTracked(sessionId, evidentMessageId) {
3344
+ if (this.dispatched.has(evidentMessageId)) return true;
3345
+ const watcher = this.watchers.get(sessionId);
3346
+ return watcher?.inFlight.has(evidentMessageId) ?? false;
3347
+ }
3348
+ /**
3349
+ * Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
3350
+ * deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
3351
+ * for `processing` rows, but if it is somehow null/unparseable fall back to
3352
+ * `now` (defensive) AND log — a fallback means the anchor is weaker than
3353
+ * intended, which is worth surfacing.
3354
+ */
3355
+ processedAtMs(row) {
3356
+ const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
3357
+ if (!Number.isNaN(parsed)) return parsed;
3358
+ this.log({
3359
+ level: "error",
3360
+ message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
3361
+ conversation_id: row.conversation_id,
3362
+ message_id: row.id
1695
3363
  });
1696
- return sessionId;
3364
+ return this.now();
3365
+ }
3366
+ /** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
3367
+ convForRow(sessionId, row) {
3368
+ return {
3369
+ id: row.conversation_id,
3370
+ agent_id: this.agentId,
3371
+ opencode_session_id: sessionId,
3372
+ pending_message_count: 0,
3373
+ oldest_pending_at: row.processed_at
3374
+ };
3375
+ }
3376
+ /** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
3377
+ queuedMessageForRow(row) {
3378
+ return {
3379
+ id: row.id,
3380
+ content: row.content,
3381
+ status: "processing",
3382
+ opencode_agent: row.opencode_agent,
3383
+ opencode_model: row.opencode_model,
3384
+ source_message_id: row.source_message_id,
3385
+ slack_user_id: row.slack_user_id,
3386
+ attachments: row.attachments ?? null
3387
+ };
1697
3388
  }
1698
3389
  /**
1699
- * Lazily resolve (and cache) opencode's root directory via `GET /path`.
1700
- * Resolved once per driver: `undefined` until first lookup, then the directory
1701
- * string or `null` if unavailable (we don't keep retrying a missing `/path`).
3390
+ * Remove a message from the in-flight set AND the authoritative dispatched
3391
+ * set. Once the in-flight set empties, the watcher loop's `while` guard exits
3392
+ * and its `.finally` removes the session entry from `this.watchers`.
3393
+ *
3394
+ * Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
3395
+ * (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
3396
+ * cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
3397
+ * re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
3398
+ * re-adopted message that completed (`done`) needs no marker — it's leaving
3399
+ * `processing`. This suppresses only re-dispatch: if its reply later completes,
3400
+ * the done branch still delivers it (Bugbot #202).
1702
3401
  */
1703
- async resolveOpenCodeDirectory() {
1704
- if (this.opencodeDirectory !== void 0) return this.opencodeDirectory;
1705
- this.opencodeDirectory = await getOpenCodeDirectory(this.port);
1706
- if (!this.opencodeDirectory) {
3402
+ removeInFlight(watcher, evidentMessageId) {
3403
+ const inFlight = watcher.inFlight.get(evidentMessageId);
3404
+ if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
3405
+ this.dontRedispatch.add(evidentMessageId);
1707
3406
  this.log({
1708
- level: "info",
1709
- message: "Could not determine opencode directory (GET /path) \u2014 new sessions may not appear in opencode web"
3407
+ level: "debug",
3408
+ message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
3409
+ conversation_id: watcher.conv.id,
3410
+ message_id: evidentMessageId
1710
3411
  });
1711
3412
  }
1712
- return this.opencodeDirectory;
3413
+ watcher.inFlight.delete(evidentMessageId);
3414
+ this.dispatched.delete(evidentMessageId);
1713
3415
  }
1714
3416
  /**
1715
- * Local reconcile: re-query `GET /session/:id/message` and check whether the
1716
- * last assistant message is message-level complete (`isTurnComplete`).
3417
+ * Poll `/question` + `/permission` (scoped to the session) and surface NEW ones
3418
+ * via `reportInteraction` (Task 3.5), carrying the PAUSED message's own
3419
+ * `source_message_id` so the server @mentions the correct person under
3420
+ * concurrency. Dedups by interaction id across ticks (reused per-session sets).
3421
+ *
3422
+ * The interaction is attributed to the in-flight message it paused on. opencode
3423
+ * stamps a `messageID` on a permission (and `tool.messageID` on a question) =
3424
+ * the assistant message id, whose `parentID` is the user message id — but the
3425
+ * simplest robust attribution here is: the single in-flight message that is
3426
+ * RUNNING (not done) is the one that paused. With one running message that is
3427
+ * unambiguous; with several we prefer an explicit messageID match, else the
3428
+ * oldest running message.
1717
3429
  *
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.
3430
+ * Returns the set of in-flight Evident message ids that are paused awaiting a
3431
+ * human an outstanding (still-open) question/permission is attributed to them.
3432
+ * `serviceInFlightMessage` uses this to keep an actively-running turn watched
3433
+ * forever (ADR-0047) while still bounding a turn merely blocked on a person who
3434
+ * may never answer. Attribution here covers ALL open interactions, not just
3435
+ * NEW (un-deduped) ones a question stays "awaiting a human" until answered,
3436
+ * even after it was already surfaced to the channel.
1725
3437
  */
1726
- async confirmCompletion(sessionId) {
3438
+ async pollInteractions(sessionId, watcher, messages) {
3439
+ const openQuestions = /* @__PURE__ */ new Set();
3440
+ const openPermissions = /* @__PURE__ */ new Set();
3441
+ let questionsPolledOk = true;
3442
+ let permissionsPolledOk = true;
3443
+ let questions = [];
1727
3444
  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
- });
3445
+ const res = await this.fetchImpl(`${this.opencodeBase}/question`);
3446
+ if (res.ok) {
3447
+ const body = await res.json();
3448
+ if (Array.isArray(body)) {
3449
+ questions = body;
3450
+ } else {
3451
+ questionsPolledOk = false;
3452
+ }
3453
+ } else {
3454
+ questionsPolledOk = false;
1737
3455
  }
1738
3456
  } catch {
3457
+ questionsPolledOk = false;
1739
3458
  }
3459
+ for (const q of questions) {
3460
+ if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
3461
+ const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
3462
+ if (paused) openQuestions.add(paused.evidentMessageId);
3463
+ if (watcher.reportedQuestions.has(q.id)) continue;
3464
+ const reported = await this.reportInteraction(
3465
+ watcher.conv.id,
3466
+ "question",
3467
+ q,
3468
+ paused?.message.source_message_id ?? void 0
3469
+ );
3470
+ if (reported) watcher.reportedQuestions.add(q.id);
3471
+ }
3472
+ let permissions = [];
3473
+ try {
3474
+ const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
3475
+ if (res.ok) {
3476
+ const body = await res.json();
3477
+ if (Array.isArray(body)) {
3478
+ permissions = body;
3479
+ } else {
3480
+ permissionsPolledOk = false;
3481
+ }
3482
+ } else {
3483
+ permissionsPolledOk = false;
3484
+ }
3485
+ } catch {
3486
+ permissionsPolledOk = false;
3487
+ }
3488
+ for (const p of permissions) {
3489
+ if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
3490
+ const paused = this.attributeInteraction(watcher, p.messageID, messages);
3491
+ if (paused) openPermissions.add(paused.evidentMessageId);
3492
+ if (watcher.reportedPermissions.has(p.id)) continue;
3493
+ const reported = await this.reportInteraction(
3494
+ watcher.conv.id,
3495
+ "permission",
3496
+ p,
3497
+ paused?.message.source_message_id ?? void 0
3498
+ );
3499
+ if (reported) watcher.reportedPermissions.add(p.id);
3500
+ }
3501
+ return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
1740
3502
  }
1741
- // -------------------------------------------------------------------------
1742
- // Paused-session watcher (WI-2-CLI)
1743
- // -------------------------------------------------------------------------
1744
3503
  /**
1745
- * Start (but do NOT await) a watcher that resumes a paused turn to completion.
3504
+ * True when `sessionId` is the `rootSessionId` itself OR a descendant of it
3505
+ * i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
3506
+ * watched root. Sub-agents spawned via the `task` tool run in child sessions,
3507
+ * so their questions/permissions live under a different `sessionID` that must
3508
+ * still be attributed to the root conversation the watcher owns.
1746
3509
  *
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.
3510
+ * Parents are cached in `sessionParents` so we walk each session at most once;
3511
+ * a bounded depth cap guards against a cycle or a pathological chain, and any
3512
+ * fetch failure is treated as "not a descendant" (best-effort the interaction
3513
+ * simply isn't surfaced this tick and is retried next tick once resolvable).
1751
3514
  */
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);
1756
- });
1757
- this.watchers.set(message.id, watcher);
3515
+ async sessionBelongsTo(sessionId, rootSessionId) {
3516
+ let current = sessionId;
3517
+ for (let depth = 0; current && depth < 32; depth++) {
3518
+ if (current === rootSessionId) return true;
3519
+ const parent = await this.resolveSessionParent(current);
3520
+ if (parent === null || parent === void 0) return false;
3521
+ current = parent;
3522
+ }
3523
+ return false;
1758
3524
  }
1759
3525
  /**
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.
3526
+ * Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
3527
+ * `null` for a root session (no parent) and `undefined` when opencode is
3528
+ * unreachable / the session can't be read (so the caller stops walking without
3529
+ * caching a wrong answer the next tick retries).
1778
3530
  */
1779
- async watchPausedSession(conv, message, sessionId) {
1780
- const deadline = Date.now() + this.pausedMaxWaitMs;
3531
+ async resolveSessionParent(sessionId) {
3532
+ const cached = this.sessionParents.get(sessionId);
3533
+ if (cached !== void 0) return cached;
3534
+ let parent = void 0;
1781
3535
  try {
1782
- while (Date.now() < deadline) {
1783
- await this.sleep(this.pausedPollIntervalMs);
1784
- let completed = false;
1785
- try {
1786
- const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
1787
- if (res.ok) {
1788
- const body = await res.json();
1789
- const messages = Array.isArray(body) ? body : null;
1790
- completed = isTurnComplete(messages);
1791
- }
1792
- } catch {
1793
- continue;
3536
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
3537
+ if (res.ok) {
3538
+ const body = await res.json();
3539
+ parent = body && typeof body.parentID === "string" ? body.parentID : null;
3540
+ }
3541
+ } catch {
3542
+ parent = void 0;
3543
+ }
3544
+ if (parent !== void 0) this.sessionParents.set(sessionId, parent);
3545
+ return parent;
3546
+ }
3547
+ /**
3548
+ * Resolve (and cache in `sessionTitles`) the OpenCode session TITLE (#310) so the
3549
+ * status PATCH can carry it into the "Live sessions" list. Driver-level cache so
3550
+ * BOTH the watcher completion path and the restart-recovery re-adopt path (which
3551
+ * has no watcher) can use it. `conversationId` is passed only for log context.
3552
+ * Best-effort:
3553
+ * - a resolved NON-EMPTY title is cached and terminal (a real session name
3554
+ * won't later un-name), so we do NOT re-GET `/session/:id` every tick;
3555
+ * - while the title is still absent/empty we do NOT latch it — OpenCode names
3556
+ * sessions asynchronously mid-turn, so an early call (e.g. at `processing`)
3557
+ * must leave the cache unresolved and re-fetch on the next need so a later
3558
+ * call (e.g. at `done`) picks up the name assigned in the meantime. Such a
3559
+ * call returns `null` (omit the title on THIS PATCH) without caching;
3560
+ * - a failed request likewise leaves the cache unresolved (retry next need)
3561
+ * and returns `null` — it must NEVER throw or block completion.
3562
+ * A failure is logged with agent/session context (no silent catch).
3563
+ */
3564
+ async resolveSessionTitle(sessionId, conversationId) {
3565
+ const cached = this.sessionTitles.get(sessionId);
3566
+ if (cached != null) return cached;
3567
+ try {
3568
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
3569
+ if (res.ok) {
3570
+ const body = await res.json();
3571
+ const title = body && typeof body.title === "string" ? body.title.trim() : "";
3572
+ if (title.length > 0) {
3573
+ this.sessionTitles.set(sessionId, title);
3574
+ return title;
1794
3575
  }
1795
- if (!completed) continue;
1796
- this.log({
1797
- level: "info",
1798
- message: `Paused session ${sessionId.slice(0, 8)} completed \u2014 marking message ${message.id.slice(0, 8)} done`,
1799
- conversation_id: conv.id,
1800
- message_id: message.id
1801
- });
1802
- await this.markDone(conv.id, message.id, sessionId);
1803
- return;
3576
+ return null;
1804
3577
  }
1805
3578
  this.log({
1806
- 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`,
1808
- conversation_id: conv.id,
1809
- message_id: message.id
3579
+ level: "debug",
3580
+ message: `Session title fetch for session ${sessionId.slice(0, 8)} (agent ${this.agentId.slice(0, 8)}) returned HTTP ${res.status} \u2014 omitting title`,
3581
+ conversation_id: conversationId
1810
3582
  });
1811
3583
  } catch (err) {
1812
3584
  this.log({
1813
- level: "error",
1814
- message: `Paused-session watcher failed for message ${message.id.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
1815
- conversation_id: conv.id,
1816
- message_id: message.id
3585
+ level: "debug",
3586
+ message: `Best-effort session title fetch failed for session ${sessionId.slice(0, 8)} (agent ${this.agentId.slice(0, 8)}) \u2014 omitting title: ${err instanceof Error ? err.message : String(err)}`,
3587
+ conversation_id: conversationId
3588
+ });
3589
+ }
3590
+ return null;
3591
+ }
3592
+ /**
3593
+ * DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
3594
+ * (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
3595
+ *
3596
+ * The PRIMARY recovery trigger is "preamble-pinned on recovery ⇒ idle" — a
3597
+ * runner restart wipes OpenCode's in-memory `SessionStatus`/`Runner`, so a
3598
+ * completed `finish: "tool-calls"` root reply encountered during re-adoption is
3599
+ * idle by OpenCode's own definition and is re-dispatched. This method exists only
3600
+ * so the WI-3 caller can VETO that re-dispatch in the rare case a descendant is
3601
+ * provably in flight at the exact moment of recovery.
3602
+ *
3603
+ * "Alive" criterion (TIGHTENED): a descendant is alive only when it is PROVABLY,
3604
+ * ACTIVELY generating — its LAST message is an assistant still mid-generation
3605
+ * (`completed == null`, via `isSessionActivelyGenerating`). An
3606
+ * INCOMPLETE-BUT-NOT-GENERATING child — last message a user message, or a
3607
+ * completed `finish: "tool-calls"` step — is NOT alive after a restart (nothing
3608
+ * is generating once the runner is gone), so it does NOT veto. (This is
3609
+ * deliberately NOT `!isTurnComplete`, which also matches those dead-but-non-terminal
3610
+ * shapes and would falsely veto — re-hanging the very turn this path recovers.)
3611
+ *
3612
+ * Return contract (encoded so WI-3 need not re-derive it):
3613
+ * - `true` → a descendant is provably, actively generating (veto re-dispatch).
3614
+ * - `false` → descendants exist but none is actively generating (the restart
3615
+ * case), OR no descendant is found at all.
3616
+ * - `null` → liveness is INDETERMINATE (enumeration via `listSessions` failed).
3617
+ *
3618
+ * ⚠️ `null` (UNKNOWN) MUST NOT be treated as "alive": WI-3 treats `null` the same
3619
+ * as `false` and does NOT veto — a restart guarantees no live runner, so an
3620
+ * indeterminate cross-check almost always means "couldn't reach a child that no
3621
+ * longer exists". The inversion lives in the caller; this method just reports
3622
+ * true/false/null faithfully.
3623
+ *
3624
+ * VERIFY-BEFORE-DEPEND: we depend ONLY on (a) `parentID` from `GET /session/:id`
3625
+ * (already proven by the existing child-session interaction tests, via
3626
+ * `resolveSessionParent`/`sessionBelongsTo`) and (b) the child's own message-list
3627
+ * terminal state. We do NOT depend on any session-level `busy`/`idle` field —
3628
+ * there is none on `GET /session/:id`; OpenCode's busy state is in-memory
3629
+ * `SessionStatus` only.
3630
+ */
3631
+ async isAnyDescendantSessionAlive(rootSessionId) {
3632
+ const sessions = await listSessions(this.port);
3633
+ if (!sessions) {
3634
+ this.log({
3635
+ level: "warn",
3636
+ message: `Re-adopt: could not enumerate sessions to cross-check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating child liveness as indeterminate`
3637
+ });
3638
+ return null;
3639
+ }
3640
+ for (const candidate of sessions) {
3641
+ if (!candidate?.id || candidate.id === rootSessionId) continue;
3642
+ if (!await this.sessionBelongsTo(candidate.id, rootSessionId)) continue;
3643
+ const childMsgs = await getSessionMessages(this.port, candidate.id);
3644
+ if (isSessionActivelyGenerating(childMsgs)) {
3645
+ return true;
3646
+ }
3647
+ }
3648
+ return false;
3649
+ }
3650
+ /**
3651
+ * Cheap decision-telemetry label for a running row's LAST correlated reply
3652
+ * (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
3653
+ * - `b1` — the reply itself is still in flight (`time.completed == null`) —
3654
+ * the aborted-in-flight production bug after a restart.
3655
+ * - `b2` — a COMPLETED reply pinned running only by `finish === "tool-calls"`
3656
+ * (the sub-agent preamble — #253's shape).
3657
+ * - `other` — any other shape (defensive; a running row is normally b1 or b2).
3658
+ * Reads `info.time.completed` / `info.finish` (tolerating the legacy top-level
3659
+ * shape) directly rather than re-importing the module-private `completedOf`/
3660
+ * `finishOf` — this is a display label only, not a correctness predicate.
3661
+ */
3662
+ replyCompletionShape(reply) {
3663
+ if (!reply) return "other";
3664
+ const completed = reply.info?.time?.completed ?? reply.time?.completed;
3665
+ if (completed == null) return "b1";
3666
+ const finish = reply.info?.finish ?? reply.finish;
3667
+ return finish === "tool-calls" ? "b2" : "other";
3668
+ }
3669
+ /**
3670
+ * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
3671
+ *
3672
+ * The interaction carries `interactionMessageId` — the ASSISTANT message id
3673
+ * that raised it (a question's `tool.messageID` / a permission's `messageID`).
3674
+ * That assistant message is the reply to ONE of our minted user messages
3675
+ * (correlated by `parentID`, GATE-B). So when we have the tick's message
3676
+ * snapshot, we resolve each running in-flight message's correlated assistant
3677
+ * reply (`findAssistantReplyAfter`) and match its id against
3678
+ * `interactionMessageId` — giving an EXACT attribution even with several
3679
+ * messages in flight concurrently in one session.
3680
+ *
3681
+ * We fall back to the oldest running message ONLY when no exact match is
3682
+ * possible (the id is absent, the snapshot is missing, or the reply has not yet
3683
+ * been correlated). With a single running message either path is exact. Never
3684
+ * throws.
3685
+ *
3686
+ * Attribution must NOT depend on our own `started` PATCH flag: opencode can
3687
+ * START a turn AND raise a question/permission BEFORE our next tick fires
3688
+ * `markProcessing` (which sets `started`). Relying on `started` would leave the
3689
+ * running set empty in that window and let the server fall back to "newest
3690
+ * processing/pending" — possibly @mentioning a FOLLOW-UP author rather than the
3691
+ * person whose active turn actually paused. So we derive "running" from the
3692
+ * tick's `messages` snapshot via `messageRunState` instead.
3693
+ */
3694
+ attributeInteraction(watcher, interactionMessageId, messages) {
3695
+ const inFlight = [...watcher.inFlight.values()].filter((m) => !m.done);
3696
+ if (inFlight.length === 0) return void 0;
3697
+ if (interactionMessageId && messages) {
3698
+ const exact = inFlight.find((m) => {
3699
+ const reply = findAssistantReplyAfter(messages, m.opencodeMessageId);
3700
+ return reply != null && messageIdOf(reply) === interactionMessageId;
1817
3701
  });
3702
+ if (exact) return exact;
3703
+ }
3704
+ const byOldest = (a, b) => a.dispatchedAt - b.dispatchedAt;
3705
+ if (messages) {
3706
+ const runningPerSnapshot = inFlight.filter(
3707
+ (m) => messageRunState(messages, m.opencodeMessageId) === "running"
3708
+ );
3709
+ if (runningPerSnapshot.length > 0) {
3710
+ return runningPerSnapshot.sort(byOldest)[0];
3711
+ }
1818
3712
  }
3713
+ const startedRunning = inFlight.filter((m) => m.started);
3714
+ if (startedRunning.length > 0) {
3715
+ return startedRunning.sort(byOldest)[0];
3716
+ }
3717
+ return inFlight.sort(byOldest)[0];
1819
3718
  }
1820
- // -------------------------------------------------------------------------
1821
3719
  // Evident API calls (combinedAuth thread routes)
1822
- // -------------------------------------------------------------------------
1823
3720
  async getPendingConversations() {
1824
3721
  const res = await this.fetchImpl(
1825
3722
  `${this.apiUrl}/agents/${this.agentId}/conversations/pending`,
@@ -1849,49 +3746,193 @@ var ChannelDriver = class {
1849
3746
  }
1850
3747
  return await res.json();
1851
3748
  }
1852
- async markProcessing(conversationId, messageId) {
3749
+ /**
3750
+ * Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
3751
+ * The pending path (`getPendingConversations`/`getPendingMessages`) only
3752
+ * surfaces `pending` rows, so a message already `processing` when the runner
3753
+ * died is invisible to it — this dedicated endpoint returns exactly those rows
3754
+ * with the fields the re-adopt path needs (`processed_at`,
3755
+ * `opencode_session_id`, routing).
3756
+ *
3757
+ * Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
3758
+ * array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
3759
+ * other non-ok so `drainPending`'s try/finally leaves `draining` false and the
3760
+ * next tick retries.
3761
+ */
3762
+ async getProcessingMessages() {
3763
+ const res = await this.fetchImpl(
3764
+ `${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
3765
+ { headers: { Authorization: this.getAuthHeader() } }
3766
+ );
3767
+ this.assertAuth(res, "fetching processing messages");
3768
+ if (!res.ok) {
3769
+ throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
3770
+ }
3771
+ const data = await res.json();
3772
+ let messages = data.messages ?? [];
3773
+ if (this.conversationFilter) {
3774
+ messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
3775
+ }
3776
+ return messages;
3777
+ }
3778
+ /**
3779
+ * EXISTING combinedAuth route — now fired by the watcher on queued→running
3780
+ * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
3781
+ * opencode_session_id}` → `notifyMessageStarted` (hourglass→runner swap +
3782
+ * deep-linked "View in Evident" notice).
3783
+ *
3784
+ * Return/throw contract (consumed by the watcher's swap-to-running guard):
3785
+ * - returns `true` → the server transitioned the row to processing;
3786
+ * - returns `false` → the server gave a DEFINITIVE "already-processing"
3787
+ * answer (a non-retryable, non-auth status — e.g. a
3788
+ * conflict because a duplicate already transitioned it),
3789
+ * so the caller treats it as already-started and does NOT
3790
+ * retry;
3791
+ * - throws `ChannelAuthError` on 401/403 (terminal auth failure);
3792
+ * - throws on a TRANSIENT failure (retryable 5xx/429 status, or a
3793
+ * network-level error from `fetch`) — i.e. NO definitive server response —
3794
+ * so the caller leaves the message un-started and retries the swap on the
3795
+ * next tick.
3796
+ * A single attempt (no internal retry): the watcher's per-tick loop is the
3797
+ * retry vehicle for the swap-to-running.
3798
+ */
3799
+ async markProcessing(conversationId, messageId, sessionId, opencodeMessageId, title) {
1853
3800
  const res = await this.fetchImpl(
1854
3801
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1855
3802
  {
1856
3803
  method: "PATCH",
1857
3804
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1858
- body: JSON.stringify({ status: "processing" })
3805
+ body: JSON.stringify({
3806
+ status: "processing",
3807
+ opencode_session_id: sessionId,
3808
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
3809
+ ...title ? { title } : {}
3810
+ })
1859
3811
  }
1860
3812
  );
1861
3813
  this.assertAuth(res, "marking message as processing");
1862
- return res.ok;
3814
+ if (res.ok) return true;
3815
+ if (isRetryableStatus(res.status)) {
3816
+ throw new Error(`marking message as processing: HTTP ${res.status}`);
3817
+ }
3818
+ return false;
1863
3819
  }
1864
3820
  /**
1865
- * EXISTING combinedAuth completion route — idempotent + retried (WI-CHAN-2).
1866
- * `PATCH .../messages/:id {status:'done', opencode_session_id}`. The server's
3821
+ * EXISTING combinedAuth completion route — idempotent (WI-CHAN-2). `PATCH
3822
+ * .../messages/:id {status:'done', opencode_session_id}`. The server's
1867
3823
  * `queued_conversation_messages.status`/`processed_at` gate makes a re-call
1868
- * for an already-`done` message a no-op (no double Slack post).
3824
+ * for an already-`done` message a no-op (no double Slack post). Fired by the
3825
+ * watcher on per-message completion (Task 3.4) — no `confirmCompletion`
3826
+ * round-trip (we already observed completion via the message list).
3827
+ *
3828
+ * SINGLE ATTEMPT (no in-call `callWithRetry` backoff). The per-session watcher
3829
+ * services its in-flight messages SEQUENTIALLY within a tick
3830
+ * (`runWatcherLoop` → `serviceInFlightMessage`), so a long multi-attempt
3831
+ * backoff here would BLOCK sibling messages in the SAME session/tick: while
3832
+ * message A's done PATCH burned its internal retries, message B could not be
3833
+ * swapped to running even though opencode had already started it. Instead this
3834
+ * does ONE PATCH and surfaces the SAME outcome contract the watcher's markDone
3835
+ * handler already relies on, leaning on the per-tick retry across ticks
3836
+ * (bounded by `inFlight.deadline`) rather than an in-call retry:
3837
+ * - resolves (`void`) → the server transitioned the row to done
3838
+ * (or idempotently confirmed already-done);
3839
+ * - throws `ChannelAuthError` → 401/403 (terminal auth failure → loop
3840
+ * cleanup, Finding 1);
3841
+ * - throws `ChannelTerminalError`→ non-retryable, non-auth 4xx (will never
3842
+ * succeed → straight to the cron, Finding 4);
3843
+ * - throws a plain `Error` → TRANSIENT 5xx/429 or a network-level error
3844
+ * (no definitive server response → the
3845
+ * watcher retries next tick within the
3846
+ * deadline, Finding 4).
3847
+ */
3848
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId, title) {
3849
+ const res = await this.fetchImpl(
3850
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3851
+ {
3852
+ method: "PATCH",
3853
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
3854
+ body: JSON.stringify({
3855
+ status: "done",
3856
+ opencode_session_id: sessionId,
3857
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
3858
+ ...title ? { title } : {}
3859
+ })
3860
+ }
3861
+ );
3862
+ this.assertAuth(res, "marking message as done");
3863
+ if (res.ok) return;
3864
+ if (isRetryableStatus(res.status)) {
3865
+ throw new Error(`marking message as done: HTTP ${res.status}`);
3866
+ }
3867
+ throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
3868
+ }
3869
+ /**
3870
+ * Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
3871
+ * when provided (issue #182): a bare `markFailed(conv, msg)` sends
3872
+ * `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
3873
+ * OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
3874
+ * failure reason reaches the channel.
1869
3875
  */
1870
- async markDone(conversationId, messageId, sessionId) {
3876
+ async markFailed(conversationId, messageId, sessionId, error2) {
3877
+ const body = { status: "failed" };
3878
+ if (sessionId !== void 0) body.opencode_session_id = sessionId;
3879
+ if (error2 !== void 0) body.error = error2;
1871
3880
  await this.callWithRetry(
1872
- "marking message as done",
3881
+ "marking message as failed",
1873
3882
  () => this.fetchImpl(
1874
3883
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1875
3884
  {
1876
3885
  method: "PATCH",
1877
3886
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1878
- body: JSON.stringify({ status: "done", opencode_session_id: sessionId })
3887
+ body: JSON.stringify(body)
1879
3888
  }
1880
3889
  )
1881
3890
  );
1882
3891
  }
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}`,
3892
+ /**
3893
+ * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
3894
+ * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
3895
+ * — the server records it via `log()` (no DB write, no notification). This is
3896
+ * fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
3897
+ * MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
3898
+ * watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
3899
+ * context (no silent catch, per development-workflow).
3900
+ *
3901
+ * Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
3902
+ * telemetry), but the `paused` liveness-clear uses it to know whether to
3903
+ * RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
3904
+ * stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
3905
+ * leaves liveness").
3906
+ */
3907
+ async postSignal(conversationId, messageId, signal, extra) {
3908
+ try {
3909
+ const res = await this.fetchImpl(
3910
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
1888
3911
  {
1889
- method: "PATCH",
3912
+ method: "POST",
1890
3913
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1891
- body: JSON.stringify({ status: "failed" })
3914
+ body: JSON.stringify({ signal, ...extra })
1892
3915
  }
1893
- )
1894
- );
3916
+ );
3917
+ if (!res.ok) {
3918
+ this.log({
3919
+ level: "warn",
3920
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
3921
+ conversation_id: conversationId,
3922
+ message_id: messageId
3923
+ });
3924
+ return false;
3925
+ }
3926
+ return true;
3927
+ } catch (err) {
3928
+ this.log({
3929
+ level: "warn",
3930
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
3931
+ conversation_id: conversationId,
3932
+ message_id: messageId
3933
+ });
3934
+ return false;
3935
+ }
1895
3936
  }
1896
3937
  async persistSession(conversationId, sessionId) {
1897
3938
  const res = await this.fetchImpl(
@@ -1906,10 +3947,17 @@ var ChannelDriver = class {
1906
3947
  }
1907
3948
  /**
1908
3949
  * 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.
3950
+ * `POST .../interactive-event {type, data, source_message_id?}`. The server
3951
+ * persists the interaction and posts a link to the proxied opencode-web
3952
+ * conversation, @mentioning the user who triggered THIS message's turn.
3953
+ *
3954
+ * WI-3 / WI-4 contract: `source_message_id` is the PAUSED message's own Slack
3955
+ * ts (`message.source_message_id`). The server resolves the @mention from that
3956
+ * message's user FIRST (falling back to the old "newest processing" precedence
3957
+ * only when absent), so the correct person is mentioned under concurrency. It
3958
+ * is OPTIONAL for back-compat with older clients / legacy rows.
1911
3959
  */
1912
- async reportInteraction(conversationId, type, data) {
3960
+ async reportInteraction(conversationId, type, data, sourceMessageId) {
1913
3961
  try {
1914
3962
  await this.callWithRetry(
1915
3963
  "reporting interactive event",
@@ -1918,7 +3966,9 @@ var ChannelDriver = class {
1918
3966
  {
1919
3967
  method: "POST",
1920
3968
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1921
- body: JSON.stringify({ type, data })
3969
+ body: JSON.stringify(
3970
+ sourceMessageId ? { type, data, source_message_id: sourceMessageId } : { type, data }
3971
+ )
1922
3972
  }
1923
3973
  )
1924
3974
  );
@@ -1927,6 +3977,7 @@ var ChannelDriver = class {
1927
3977
  message: `${type} surfaced to channel (id: ${data.id.slice(0, 8)})`,
1928
3978
  conversation_id: conversationId
1929
3979
  });
3980
+ return true;
1930
3981
  } catch (err) {
1931
3982
  if (err instanceof ChannelAuthError) throw err;
1932
3983
  this.log({
@@ -1934,11 +3985,10 @@ var ChannelDriver = class {
1934
3985
  message: `Failed to surface ${type}: ${err instanceof Error ? err.message : String(err)}`,
1935
3986
  conversation_id: conversationId
1936
3987
  });
3988
+ return false;
1937
3989
  }
1938
3990
  }
1939
- // -------------------------------------------------------------------------
1940
3991
  // Retry wrapper
1941
- // -------------------------------------------------------------------------
1942
3992
  /**
1943
3993
  * Invoke an Evident API call, retrying on transient failures (5xx / 429 /
1944
3994
  * network errors) with exponential backoff + jitter (capped). Auth failures
@@ -1972,8 +4022,9 @@ var ChannelDriver = class {
1972
4022
  await this.sleep(backoffDelay(attempt, this.retry));
1973
4023
  continue;
1974
4024
  }
4025
+ break;
1975
4026
  }
1976
- throw new Error(`${context}: HTTP ${res.status}`);
4027
+ throw new ChannelTerminalError(`${context}: HTTP ${res.status}`, res.status);
1977
4028
  }
1978
4029
  throw lastError instanceof Error ? lastError : new Error(`${context}: exhausted retries`);
1979
4030
  }
@@ -2151,6 +4202,25 @@ async function resolveAgentIdFromKey(authHeader) {
2151
4202
  return { error: `Failed to resolve agent from key: ${message}` };
2152
4203
  }
2153
4204
  }
4205
+ async function notifyAgentDisconnected(agentId, authHeader) {
4206
+ const apiUrl = getApiUrlConfig();
4207
+ try {
4208
+ const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
4209
+ method: "POST",
4210
+ headers: { Authorization: authHeader }
4211
+ });
4212
+ if (!response.ok) {
4213
+ const serverMessage = await readErrorMessage(response);
4214
+ return {
4215
+ ok: false,
4216
+ error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
4217
+ };
4218
+ }
4219
+ return { ok: true };
4220
+ } catch (error2) {
4221
+ return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
4222
+ }
4223
+ }
2154
4224
  async function getAgentInfo(agentId, authHeader) {
2155
4225
  const apiUrl = getApiUrlConfig();
2156
4226
  try {
@@ -2196,23 +4266,55 @@ async function getAgentInfo(agentId, authHeader) {
2196
4266
  // src/commands/run.ts
2197
4267
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
2198
4268
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
2199
- function log(state, message, isError = false) {
4269
+ var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
4270
+ var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
4271
+ function resolveLogLevel(options) {
4272
+ const accepted = Object.keys(LOG_LEVELS);
4273
+ const validate = (value, source) => {
4274
+ const normalized = value.trim().toLowerCase();
4275
+ if (!accepted.includes(normalized)) {
4276
+ throw new Error(
4277
+ `Invalid log level "${value}"${source}; expected one of ${accepted.join(", ")}`
4278
+ );
4279
+ }
4280
+ return normalized;
4281
+ };
4282
+ if (options.logLevel !== void 0) {
4283
+ return validate(options.logLevel, " (--log-level)");
4284
+ }
4285
+ if (options.verbose) {
4286
+ return "debug";
4287
+ }
4288
+ const env = process.env.EVIDENT_LOG_LEVEL;
4289
+ if (env !== void 0 && env !== "") {
4290
+ return validate(env, " (EVIDENT_LOG_LEVEL)");
4291
+ }
4292
+ return "info";
4293
+ }
4294
+ function meetsThreshold(state, level) {
4295
+ return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
4296
+ }
4297
+ function log2(state, message, level = "info") {
4298
+ if (!meetsThreshold(state, level)) return;
2200
4299
  if (state.json) {
2201
4300
  console.log(
2202
4301
  JSON.stringify({
2203
4302
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2204
- level: isError ? "error" : "info",
4303
+ level,
2205
4304
  message
2206
4305
  })
2207
4306
  );
2208
4307
  } else if (!state.interactive) {
2209
- const prefix = isError ? chalk6.red("\u2717") : chalk6.green("\u2022");
4308
+ const prefix = level === "error" ? chalk6.red("\u2717") : level === "warn" ? chalk6.yellow("!") : level === "debug" ? chalk6.dim("\xB7") : chalk6.green("\u2022");
2210
4309
  console.log(`${prefix} ${message}`);
2211
4310
  }
2212
4311
  }
2213
4312
  function logActivity(state, entry) {
4313
+ const level = entry.level ?? (entry.type === "error" ? "error" : "info");
4314
+ if (!meetsThreshold(state, level)) return;
2214
4315
  const fullEntry = {
2215
4316
  ...entry,
4317
+ level,
2216
4318
  timestamp: /* @__PURE__ */ new Date()
2217
4319
  };
2218
4320
  state.activityLog.push(fullEntry);
@@ -2221,9 +4323,9 @@ function logActivity(state, entry) {
2221
4323
  }
2222
4324
  if (!state.interactive) {
2223
4325
  if (entry.type === "error") {
2224
- log(state, entry.error ?? "Unknown error", true);
2225
- } else if (entry.type === "info" && entry.message) {
2226
- log(state, entry.message);
4326
+ log2(state, entry.error ?? "Unknown error", level);
4327
+ } else if (entry.message) {
4328
+ log2(state, entry.message, level);
2227
4329
  }
2228
4330
  }
2229
4331
  }
@@ -2309,6 +4411,7 @@ async function handleAuthError(state, error2) {
2309
4411
  }
2310
4412
  async function driveChannels(state, driver) {
2311
4413
  let idlePolls = 0;
4414
+ let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
2312
4415
  while (state.running) {
2313
4416
  if (state.connection?.reconnecting && state.connection.reconnectPromise) {
2314
4417
  logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
@@ -2318,9 +4421,11 @@ async function driveChannels(state, driver) {
2318
4421
  try {
2319
4422
  const processed = await driver.drainPending();
2320
4423
  state.messageCount += processed;
2321
- if (processed > 0) {
4424
+ const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
4425
+ lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
4426
+ if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
2322
4427
  idlePolls = 0;
2323
- if (state.interactive) displayStatus(state);
4428
+ if (processed > 0 && state.interactive) displayStatus(state);
2324
4429
  } else if (state.idleTimeout !== null) {
2325
4430
  idlePolls++;
2326
4431
  if (idlePolls === 1) {
@@ -2347,7 +4452,7 @@ async function driveChannels(state, driver) {
2347
4452
  logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
2348
4453
  if (state.interactive) displayStatus(state);
2349
4454
  }
2350
- await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
4455
+ await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
2351
4456
  if (state.idleTimeout !== null && idlePolls >= 2) {
2352
4457
  const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
2353
4458
  if (idleMs > state.idleTimeout * 1e3) {
@@ -2358,8 +4463,122 @@ async function driveChannels(state, driver) {
2358
4463
  }
2359
4464
  }
2360
4465
  }
2361
- async function cleanup(state) {
4466
+ var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
4467
+ async function runSweep(state, driver, config2) {
4468
+ const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
4469
+ try {
4470
+ const sessions = await listSessions(state.port);
4471
+ if (sessions === null) {
4472
+ logActivity(state, {
4473
+ type: "info",
4474
+ message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
4475
+ });
4476
+ return;
4477
+ }
4478
+ const toDelete = selectSessionsToDelete(
4479
+ sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
4480
+ {
4481
+ maxAgeMs: config2.maxAgeMs,
4482
+ maxCount: config2.maxCount,
4483
+ nowMs: Date.now(),
4484
+ protectedIds: driver.protectedSessionIds()
4485
+ }
4486
+ );
4487
+ const protectedNow = driver.protectedSessionIds();
4488
+ let deleted = 0;
4489
+ let failed = 0;
4490
+ let skippedNewlyActive = 0;
4491
+ for (const id of toDelete) {
4492
+ if (protectedNow.has(id)) {
4493
+ skippedNewlyActive++;
4494
+ logActivity(state, {
4495
+ type: "info",
4496
+ message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
4497
+ });
4498
+ continue;
4499
+ }
4500
+ if (await deleteSession(state.port, id)) deleted++;
4501
+ else failed++;
4502
+ }
4503
+ const failedNote = failed > 0 ? `, failed ${failed}` : "";
4504
+ const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
4505
+ logActivity(state, {
4506
+ type: "info",
4507
+ message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
4508
+ });
4509
+ } catch (error2) {
4510
+ const message = error2 instanceof Error ? error2.message : String(error2);
4511
+ logActivity(state, {
4512
+ type: "error",
4513
+ error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
4514
+ });
4515
+ }
4516
+ }
4517
+ function scheduleSessionCleanup(state, driver, options) {
4518
+ const config2 = resolveSessionCleanupConfig(
4519
+ {
4520
+ maxAge: options.sessionCleanupMaxAge,
4521
+ maxCount: options.sessionCleanupMaxCount,
4522
+ interval: options.sessionCleanupInterval
4523
+ },
4524
+ process.env
4525
+ );
4526
+ for (const warning2 of config2.warnings) {
4527
+ logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
4528
+ }
4529
+ if (!config2.enabled) return;
4530
+ logActivity(state, {
4531
+ type: "info",
4532
+ message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
4533
+ });
4534
+ const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
4535
+ const firstSweep = setTimeout(
4536
+ () => void runSweep(state, driver, config2),
4537
+ SESSION_CLEANUP_FIRST_SWEEP_MS
4538
+ );
4539
+ state.sessionCleanupTimers.push(interval, firstSweep);
4540
+ }
4541
+ async function notifyOffline(state) {
4542
+ if (!state.agentId || !state.authHeader) return;
4543
+ if (!state.connected) {
4544
+ log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
4545
+ return;
4546
+ }
4547
+ const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
4548
+ if (result.ok) {
4549
+ log2(state, "Notified Evident the agent is going offline");
4550
+ } else {
4551
+ logActivity(state, {
4552
+ type: "error",
4553
+ error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
4554
+ });
4555
+ if (state.interactive) displayStatus(state);
4556
+ }
4557
+ }
4558
+ async function cleanup(state, opts = {}) {
2362
4559
  state.running = false;
4560
+ for (const timer of state.sessionCleanupTimers) {
4561
+ clearInterval(timer);
4562
+ clearTimeout(timer);
4563
+ }
4564
+ state.sessionCleanupTimers = [];
4565
+ if (opts.graceful && state.channelDriver) {
4566
+ state.channelDriver.stop();
4567
+ log2(state, "Draining in-flight channel work before shutdown...");
4568
+ if (state.interactive) {
4569
+ logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
4570
+ displayStatus(state);
4571
+ }
4572
+ const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
4573
+ if (!settled) {
4574
+ logActivity(state, {
4575
+ type: "info",
4576
+ message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
4577
+ });
4578
+ if (state.interactive) displayStatus(state);
4579
+ }
4580
+ }
4581
+ await notifyOffline(state);
2363
4582
  if (state.connection) {
2364
4583
  state.connection.close();
2365
4584
  state.connection = null;
@@ -2370,13 +4589,27 @@ async function cleanup(state) {
2370
4589
  logActivity(state, { type: "info", message: "Stopped OpenCode process" });
2371
4590
  displayStatus(state);
2372
4591
  } else {
2373
- log(state, "Stopped OpenCode process");
4592
+ log2(state, "Stopped OpenCode process");
2374
4593
  }
2375
4594
  state.opencodeProcess = null;
2376
4595
  }
2377
4596
  }
2378
4597
  async function run(options) {
2379
4598
  const interactive = isInteractive(options.json);
4599
+ let logLevel;
4600
+ try {
4601
+ logLevel = resolveLogLevel(options);
4602
+ } catch (error2) {
4603
+ const message = error2 instanceof Error ? error2.message : String(error2);
4604
+ if (options.json) {
4605
+ console.log(JSON.stringify({ status: "error", error: message }));
4606
+ } else {
4607
+ printError(message);
4608
+ }
4609
+ await shutdownTelemetry();
4610
+ process.exit(1);
4611
+ return;
4612
+ }
2380
4613
  const state = {
2381
4614
  agentId: options.agent || "",
2382
4615
  agentName: null,
@@ -2385,31 +4618,38 @@ async function run(options) {
2385
4618
  idleTimeout: options.idleTimeout ?? null,
2386
4619
  json: options.json ?? false,
2387
4620
  interactive,
4621
+ logLevel,
2388
4622
  connected: false,
2389
4623
  opencodeConnected: false,
2390
4624
  opencodeVersion: null,
2391
4625
  opencodeProcess: null,
2392
4626
  connection: null,
4627
+ channelDriver: null,
2393
4628
  running: true,
4629
+ shuttingDown: false,
2394
4630
  activityLog: [],
2395
4631
  messageCount: 0,
4632
+ lastProxiedActivityAt: null,
4633
+ sessionCleanupTimers: [],
2396
4634
  authHeader: ""
2397
4635
  };
2398
4636
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
2399
- log(
4637
+ log2(
2400
4638
  state,
2401
- "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
- false
4639
+ "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.",
4640
+ "warn"
2403
4641
  );
2404
4642
  }
2405
4643
  const handleSignal = async () => {
4644
+ if (state.shuttingDown) return;
4645
+ state.shuttingDown = true;
2406
4646
  if (state.interactive) {
2407
4647
  logActivity(state, { type: "info", message: "Shutting down..." });
2408
4648
  displayStatus(state);
2409
4649
  } else {
2410
- log(state, "Shutting down...");
4650
+ log2(state, "Shutting down...");
2411
4651
  }
2412
- await cleanup(state);
4652
+ await cleanup(state, { graceful: true });
2413
4653
  await shutdownTelemetry();
2414
4654
  process.exit(0);
2415
4655
  };
@@ -2440,7 +4680,7 @@ async function run(options) {
2440
4680
  const resolved = await resolveAgentIdFromKey(state.authHeader);
2441
4681
  if (resolved.agent_id) {
2442
4682
  state.agentId = resolved.agent_id;
2443
- log(state, `Resolved agent ID from key: ${state.agentId}`);
4683
+ log2(state, `Resolved agent ID from key: ${state.agentId}`);
2444
4684
  if (state.interactive && !state.json) {
2445
4685
  logActivity(state, {
2446
4686
  type: "info",
@@ -2503,14 +4743,21 @@ async function run(options) {
2503
4743
  port: state.port,
2504
4744
  interactive: state.interactive,
2505
4745
  agentId: state.agentId,
2506
- log: (message) => log(state, message)
4746
+ log: (message) => log2(state, message)
2507
4747
  });
2508
4748
  state.port = oc.port;
2509
4749
  state.opencodeProcess = oc.process;
2510
4750
  state.opencodeVersion = oc.version;
2511
4751
  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}`);
4752
+ const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
4753
+ ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
4754
+ const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
4755
+ if (versionWarning) {
4756
+ log2(state, versionWarning, "warn");
4757
+ if (state.interactive && !state.json) {
4758
+ logActivity(state, { type: "info", level: "warn", message: versionWarning });
4759
+ }
4760
+ }
2514
4761
  } catch (error2) {
2515
4762
  ocSpinner?.fail(error2.message);
2516
4763
  throw error2;
@@ -2522,12 +4769,20 @@ async function run(options) {
2522
4769
  apiUrl: getApiUrlConfig(),
2523
4770
  getAuthHeader: () => state.authHeader,
2524
4771
  conversationFilter: state.conversationFilter,
2525
- log: (entry) => logActivity(state, {
2526
- type: entry.level === "error" ? "error" : "info",
2527
- message: entry.message,
2528
- error: entry.level === "error" ? entry.message : void 0
2529
- })
4772
+ stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
4773
+ log: (entry) => (
4774
+ // Thread the driver's real level straight through so `debug`/`warn`
4775
+ // survive the sink filter (they no longer collapse to info). `type`
4776
+ // stays the coarse error/non-error split the activity log renders with.
4777
+ logActivity(state, {
4778
+ type: entry.level === "error" ? "error" : "info",
4779
+ level: entry.level,
4780
+ message: entry.message,
4781
+ error: entry.level === "error" ? entry.message : void 0
4782
+ })
4783
+ )
2530
4784
  });
4785
+ state.channelDriver = channelDriver;
2531
4786
  const connection = new RunnerConnection({
2532
4787
  agentId: state.agentId,
2533
4788
  getAuthHeader: () => state.authHeader,
@@ -2541,7 +4796,11 @@ async function run(options) {
2541
4796
  type: "info",
2542
4797
  message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
2543
4798
  });
2544
- emitAgentConnected(state.agentId, { port: state.port });
4799
+ emitAgentConnected(state.agentId, {
4800
+ port: state.port,
4801
+ cli_version: getCliVersion(),
4802
+ opencode_version: state.opencodeVersion
4803
+ });
2545
4804
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
2546
4805
  if (state.interactive) displayStatus(state);
2547
4806
  channelDriver.drainPending().then((processed) => {
@@ -2575,9 +4834,14 @@ async function run(options) {
2575
4834
  logActivity(state, { type: "error", error: error2 });
2576
4835
  if (state.interactive) displayStatus(state);
2577
4836
  },
2578
- // Web traffic is proxied transparently; only note opencode is live.
4837
+ // Web traffic is proxied transparently; note opencode is live and stamp
4838
+ // proxied activity so the idle loop treats interactive proxy use as work.
4839
+ // Fires per forwarded response head (incl. every SSE open) and excludes
4840
+ // the internal drain-ping, so an actively-used proxy keeps the timer
4841
+ // fresh while a lone idle SSE with no follow-up requests still ages out.
2579
4842
  onResponse: () => {
2580
4843
  state.opencodeConnected = true;
4844
+ state.lastProxiedActivityAt = Date.now();
2581
4845
  },
2582
4846
  // A channel message was queued and the api-worker pinged us over the
2583
4847
  // tunnel to drain immediately instead of waiting for the next poll tick.
@@ -2615,10 +4879,12 @@ async function run(options) {
2615
4879
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
2616
4880
  throw error2;
2617
4881
  }
4882
+ scheduleSessionCleanup(state, channelDriver, options);
2618
4883
  if (!interactive || state.json) {
2619
- log(state, "Driving channel messages...");
4884
+ log2(state, "Driving channel messages...");
2620
4885
  }
2621
4886
  await driveChannels(state, channelDriver);
4887
+ if (state.shuttingDown) return;
2622
4888
  await cleanup(state);
2623
4889
  if (state.json) {
2624
4890
  console.log(
@@ -2628,11 +4894,12 @@ async function run(options) {
2628
4894
  })
2629
4895
  );
2630
4896
  } else if (!interactive) {
2631
- log(state, `Completed. Processed ${state.messageCount} message(s).`);
4897
+ log2(state, `Completed. Processed ${state.messageCount} message(s).`);
2632
4898
  }
2633
4899
  await shutdownTelemetry();
2634
4900
  process.exit(0);
2635
4901
  } catch (error2) {
4902
+ if (state.shuttingDown) return;
2636
4903
  await cleanup(state);
2637
4904
  const message = error2 instanceof Error ? error2.message : String(error2);
2638
4905
  if (state.json) {
@@ -2650,8 +4917,9 @@ async function run(options) {
2650
4917
  }
2651
4918
 
2652
4919
  // src/index.ts
4920
+ var { version } = createRequire(import.meta.url)("../package.json");
2653
4921
  var program = new Command();
2654
- program.name("evident").description("Run OpenCode locally and connect it to Evident").version("0.1.0").option(
4922
+ program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
2655
4923
  "--endpoint <url>",
2656
4924
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
2657
4925
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
@@ -2666,15 +4934,34 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
2666
4934
  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
4935
  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
4936
  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(
4937
+ 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(
4938
+ "--log-level <level>",
4939
+ "Log verbosity: debug | info | warn | error (default: info). Env: EVIDENT_LOG_LEVEL"
4940
+ ).option("-v, --verbose", "Alias for --log-level debug (ignored if --log-level is set)").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(
4941
+ "--session-cleanup-max-age <duration>",
4942
+ "Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
4943
+ ).option(
4944
+ "--session-cleanup-max-count <n>",
4945
+ "Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
4946
+ ).option(
4947
+ "--session-cleanup-interval <duration>",
4948
+ "How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
4949
+ ).action(
2670
4950
  (options) => {
2671
4951
  run({
2672
4952
  agent: options.agent,
2673
4953
  port: parseInt(options.port, 10),
4954
+ // Raw string — validation/precedence is single-sourced in run.ts's
4955
+ // resolveLogLevel (flag > -v > EVIDENT_LOG_LEVEL > info).
4956
+ logLevel: options.logLevel,
2674
4957
  verbose: options.verbose,
2675
4958
  conversation: options.conversation,
2676
4959
  idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
2677
- json: options.json
4960
+ json: options.json,
4961
+ // Raw strings — the resolver in run.ts single-sources parsing (M1).
4962
+ sessionCleanupMaxAge: options.sessionCleanupMaxAge,
4963
+ sessionCleanupMaxCount: options.sessionCleanupMaxCount,
4964
+ sessionCleanupInterval: options.sessionCleanupInterval
2678
4965
  });
2679
4966
  }
2680
4967
  );