@evident-ai/cli 3.0.1-dev.c138873 → 3.0.1-dev.c56b413

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
  }
@@ -470,12 +471,6 @@ import chalk6 from "chalk";
470
471
  import ora3 from "ora";
471
472
  import { select as select3 } from "@inquirer/prompts";
472
473
 
473
- // ../../packages/types/src/opencode/index.ts
474
- function opencodeMessageIdFor(queuedMessageId) {
475
- const sanitized = queuedMessageId.replace(/[^a-zA-Z0-9]/g, "_");
476
- return `msg_${sanitized}`;
477
- }
478
-
479
474
  // ../../packages/types/src/telemetry/index.ts
480
475
  var TelemetryEventTypes = {
481
476
  // Agent activity events (shown in web UI activity log)
@@ -514,7 +509,10 @@ function stripQuery(url) {
514
509
  }
515
510
 
516
511
  // src/lib/telemetry.ts
517
- 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
+ }
518
516
  var eventBuffer = [];
519
517
  var flushTimeout = null;
520
518
  var isShuttingDown = false;
@@ -708,20 +706,20 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
708
706
  if (health.healthy) {
709
707
  return health;
710
708
  }
711
- await new Promise((resolve) => setTimeout(resolve, 1e3));
709
+ await new Promise((resolve2) => setTimeout(resolve2, 1e3));
712
710
  }
713
711
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
714
712
  }
715
713
 
716
714
  // src/lib/opencode/opencode-version-gate.ts
717
- var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11"];
718
- function isQueueValidatedVersion(version) {
719
- if (!version) return false;
720
- return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version);
721
- }
722
- function buildOpenCodeVersionWarning(version) {
723
- if (isQueueValidatedVersion(version)) return null;
724
- const detected = version ? `v${version}` : "unknown";
715
+ var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
716
+ function isQueueValidatedVersion(version2) {
717
+ if (!version2) return false;
718
+ return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
719
+ }
720
+ function buildOpenCodeVersionWarning(version2) {
721
+ if (isQueueValidatedVersion(version2)) return null;
722
+ const detected = version2 ? `v${version2}` : "unknown";
725
723
  const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
726
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.`;
727
725
  }
@@ -1038,7 +1036,11 @@ function roleOf(m) {
1038
1036
  }
1039
1037
  function completedOf(m) {
1040
1038
  if (!m || typeof m !== "object") return void 0;
1041
- 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;
1042
1044
  }
1043
1045
  function idOf(m) {
1044
1046
  if (!m || typeof m !== "object") return void 0;
@@ -1058,6 +1060,102 @@ function finishOf(m) {
1058
1060
  const infoFinish = m.info?.finish;
1059
1061
  return typeof infoFinish === "string" ? infoFinish : void 0;
1060
1062
  }
1063
+ function errorOf(m) {
1064
+ if (!m || typeof m !== "object") return void 0;
1065
+ return m.info?.error ?? m.error;
1066
+ }
1067
+ function isAssistantInFlight(m) {
1068
+ if (completedOf(m) == null) return true;
1069
+ return finishOf(m) === "tool-calls";
1070
+ }
1071
+ async function getSessionMessages(port, sessionId) {
1072
+ try {
1073
+ const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/message`);
1074
+ if (!res.ok) return null;
1075
+ const body = await res.json();
1076
+ return Array.isArray(body) ? body : null;
1077
+ } catch {
1078
+ return null;
1079
+ }
1080
+ }
1081
+ function isSessionActivelyGenerating(messages) {
1082
+ if (!messages || messages.length === 0) return false;
1083
+ const last = messages[messages.length - 1];
1084
+ if (roleOf(last) !== "assistant") return false;
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";
1158
+ }
1061
1159
  async function createOpenCodeSession(port, directory) {
1062
1160
  const url = new URL(`${opencodeBase(port)}/session`);
1063
1161
  if (directory && directory.trim()) {
@@ -1075,9 +1173,16 @@ async function createOpenCodeSession(port, directory) {
1075
1173
  const data = await response.json();
1076
1174
  return data.id;
1077
1175
  }
1078
- async function sendPromptAsync(port, sessionId, content, options, messageId) {
1176
+ function messageText(m) {
1177
+ if (!m || !Array.isArray(m.parts)) return "";
1178
+ return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
1179
+ }
1180
+ async function sendPromptAsync(port, sessionId, content, options) {
1181
+ const before = await getSessionMessages(port, sessionId);
1182
+ const knownUserIds = new Set(
1183
+ (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
1184
+ );
1079
1185
  const body = {
1080
- messageID: messageId,
1081
1186
  parts: [{ type: "text", text: content }]
1082
1187
  };
1083
1188
  if (options?.agent) {
@@ -1101,6 +1206,29 @@ async function sendPromptAsync(port, sessionId, content, options, messageId) {
1101
1206
  const text = await res.text().catch(() => "");
1102
1207
  throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1103
1208
  }
1209
+ const READ_BACK_ATTEMPTS = 5;
1210
+ const READ_BACK_DELAY_MS = 150;
1211
+ for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
1212
+ const after = await getSessionMessages(port, sessionId);
1213
+ if (after) {
1214
+ let best = null;
1215
+ for (const m of after) {
1216
+ if (roleOf(m) !== "user") continue;
1217
+ const id = idOf(m);
1218
+ if (typeof id !== "string" || knownUserIds.has(id)) continue;
1219
+ if (messageText(m) !== content) continue;
1220
+ const created = createdOf(m) ?? 0;
1221
+ if (best === null || created > best.created) {
1222
+ best = { id, created };
1223
+ }
1224
+ }
1225
+ if (best) return best.id;
1226
+ }
1227
+ if (attempt < READ_BACK_ATTEMPTS - 1) {
1228
+ await new Promise((resolve2) => setTimeout(resolve2, READ_BACK_DELAY_MS));
1229
+ }
1230
+ }
1231
+ return null;
1104
1232
  }
1105
1233
  function findAssistantReplyAfter(messages, userMessageId) {
1106
1234
  if (!messages || messages.length === 0) return null;
@@ -1117,19 +1245,31 @@ function findAssistantReplyAfter(messages, userMessageId) {
1117
1245
  }
1118
1246
  function findLastAssistantReplyFor(messages, userMessageId) {
1119
1247
  if (!messages || messages.length === 0) return null;
1248
+ let lastCorrelated = null;
1249
+ let lastNonErrored = null;
1120
1250
  for (let i = messages.length - 1; i >= 0; i--) {
1121
1251
  const m = messages[i];
1122
- if (roleOf(m) === "assistant" && parentIdOf(m) === userMessageId) return m;
1252
+ if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
1253
+ if (lastCorrelated === null) lastCorrelated = m;
1254
+ if (errorOf(m) == null) {
1255
+ lastNonErrored = m;
1256
+ break;
1257
+ }
1123
1258
  }
1259
+ if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
1124
1260
  const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1125
1261
  if (userIndex === -1) return null;
1126
1262
  let last = null;
1263
+ let lastOk = null;
1127
1264
  for (let i = userIndex + 1; i < messages.length; i++) {
1128
1265
  const role = roleOf(messages[i]);
1129
1266
  if (role === "user") break;
1130
- if (role === "assistant") last = messages[i];
1267
+ if (role === "assistant") {
1268
+ last = messages[i];
1269
+ if (errorOf(messages[i]) == null) lastOk = messages[i];
1270
+ }
1131
1271
  }
1132
- return last;
1272
+ return lastOk ?? last;
1133
1273
  }
1134
1274
  function messageRunState(messages, userMessageId) {
1135
1275
  if (!messages || messages.length === 0) return "unknown";
@@ -1139,12 +1279,136 @@ function messageRunState(messages, userMessageId) {
1139
1279
  if (!reply) return "unknown";
1140
1280
  }
1141
1281
  if (!reply) return "queued";
1142
- if (completedOf(reply) == null) return "running";
1143
- if (finishOf(reply) === "tool-calls") return "running";
1144
- return "done";
1282
+ if (isAssistantInFlight(reply)) return "running";
1283
+ return errorOf(reply) != null ? "failed" : "done";
1145
1284
  }
1146
- function opencodeMessageIdFor2(queuedMessageId) {
1147
- return opencodeMessageIdFor(queuedMessageId);
1285
+ function isPreamblePinnedRunning(messages, userMessageId) {
1286
+ if (messageRunState(messages, userMessageId) !== "running") return false;
1287
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1288
+ return completedOf(reply) != null && finishOf(reply) === "tool-calls";
1289
+ }
1290
+ function messageError(messages, userMessageId) {
1291
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1292
+ const error2 = errorOf(reply);
1293
+ if (error2 == null) return null;
1294
+ if (typeof error2 === "string") return error2;
1295
+ if (typeof error2 === "object") {
1296
+ const e = error2;
1297
+ const dataMessage = e.data?.message;
1298
+ if (typeof dataMessage === "string") return dataMessage;
1299
+ if (typeof e.message === "string") return e.message;
1300
+ }
1301
+ return "The agent run failed.";
1302
+ }
1303
+ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
1304
+ if (!messages || messages.length === 0) return false;
1305
+ return messages.some(
1306
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
1307
+ );
1308
+ }
1309
+
1310
+ // src/lib/opencode/session-cleanup.ts
1311
+ var DURATION_UNIT_MS = {
1312
+ s: 1e3,
1313
+ m: 60 * 1e3,
1314
+ h: 60 * 60 * 1e3,
1315
+ d: 24 * 60 * 60 * 1e3
1316
+ };
1317
+ function parseDurationMs(input) {
1318
+ const trimmed = input.trim();
1319
+ const match = /^(\d+)([smhd])$/.exec(trimmed);
1320
+ if (!match) {
1321
+ throw new Error(
1322
+ `Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
1323
+ );
1324
+ }
1325
+ const value = Number(match[1]);
1326
+ if (value <= 0) {
1327
+ throw new Error(`Invalid duration "${input}": must be a positive value.`);
1328
+ }
1329
+ return value * DURATION_UNIT_MS[match[2]];
1330
+ }
1331
+ function selectSessionsToDelete(sessions, opts) {
1332
+ const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
1333
+ if (maxAgeMs === void 0 && maxCount === void 0) return [];
1334
+ const ageEligible = (s) => {
1335
+ if (maxAgeMs === void 0) return false;
1336
+ if (s.lastActivityMs === null) return true;
1337
+ return nowMs - s.lastActivityMs > maxAgeMs;
1338
+ };
1339
+ const countEligibleIds = /* @__PURE__ */ new Set();
1340
+ if (maxCount !== void 0) {
1341
+ const byActivityDesc = [...sessions].sort(
1342
+ (a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
1343
+ );
1344
+ for (const s of byActivityDesc.slice(maxCount)) {
1345
+ countEligibleIds.add(s.id);
1346
+ }
1347
+ }
1348
+ const toDelete = [];
1349
+ for (const s of sessions) {
1350
+ if (protectedIds.has(s.id)) continue;
1351
+ if (ageEligible(s) || countEligibleIds.has(s.id)) {
1352
+ toDelete.push(s.id);
1353
+ }
1354
+ }
1355
+ return toDelete;
1356
+ }
1357
+ var DEFAULT_INTERVAL = "1h";
1358
+ function resolve(flag, envValue, fallback) {
1359
+ return flag ?? envValue ?? fallback;
1360
+ }
1361
+ function parseMaxCount(input) {
1362
+ const trimmed = input.trim();
1363
+ if (!/^\d+$/.test(trimmed)) {
1364
+ throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
1365
+ }
1366
+ const value = Number(trimmed);
1367
+ if (value <= 0) {
1368
+ throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
1369
+ }
1370
+ return value;
1371
+ }
1372
+ function resolveSessionCleanupConfig(flags, env = process.env) {
1373
+ const warnings = [];
1374
+ const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
1375
+ const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
1376
+ const intervalRaw = resolve(
1377
+ flags.interval,
1378
+ env.EVIDENT_SESSION_CLEANUP_INTERVAL,
1379
+ DEFAULT_INTERVAL
1380
+ );
1381
+ let maxAgeMs;
1382
+ if (maxAgeRaw !== void 0) {
1383
+ try {
1384
+ maxAgeMs = parseDurationMs(maxAgeRaw);
1385
+ } catch (err) {
1386
+ warnings.push(
1387
+ `Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
1388
+ );
1389
+ }
1390
+ }
1391
+ let maxCount;
1392
+ if (maxCountRaw !== void 0) {
1393
+ try {
1394
+ maxCount = parseMaxCount(maxCountRaw);
1395
+ } catch (err) {
1396
+ warnings.push(
1397
+ `Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
1398
+ );
1399
+ }
1400
+ }
1401
+ let intervalMs;
1402
+ try {
1403
+ intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
1404
+ } catch (err) {
1405
+ warnings.push(
1406
+ `Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
1407
+ );
1408
+ intervalMs = parseDurationMs(DEFAULT_INTERVAL);
1409
+ }
1410
+ const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
1411
+ return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
1148
1412
  }
1149
1413
 
1150
1414
  // src/lib/tunnel/connection.ts
@@ -1223,24 +1487,26 @@ var StreamForwarder = class {
1223
1487
  this.send({ type: "res_end", sid });
1224
1488
  return;
1225
1489
  }
1226
- log("info", "agent_request", {
1227
- correlation_id: correlationId,
1228
- sid,
1229
- method,
1230
- path: stripQuery(path)
1231
- });
1490
+ if (process.env.DEBUG) {
1491
+ log("debug", "agent_request", {
1492
+ correlation_id: correlationId,
1493
+ sid,
1494
+ method,
1495
+ path: stripQuery(path)
1496
+ });
1497
+ }
1232
1498
  const ac = new AbortController();
1233
1499
  let bodyPromise;
1234
1500
  let pushBody;
1235
1501
  let endBody;
1236
1502
  if (has_body) {
1237
1503
  const chunks = [];
1238
- bodyPromise = new Promise((resolve) => {
1504
+ bodyPromise = new Promise((resolve2) => {
1239
1505
  pushBody = (buf) => {
1240
1506
  chunks.push(buf);
1241
1507
  };
1242
1508
  endBody = () => {
1243
- resolve(Buffer.concat(chunks));
1509
+ resolve2(Buffer.concat(chunks));
1244
1510
  };
1245
1511
  });
1246
1512
  }
@@ -1275,12 +1541,14 @@ var StreamForwarder = class {
1275
1541
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1276
1542
  });
1277
1543
  this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
1278
- log("info", "agent_response", {
1279
- correlation_id: correlationId,
1280
- sid,
1281
- status: upstream.status,
1282
- duration_ms: Date.now() - startedAt
1283
- });
1544
+ if (process.env.DEBUG) {
1545
+ log("debug", "agent_response", {
1546
+ correlation_id: correlationId,
1547
+ sid,
1548
+ status: upstream.status,
1549
+ duration_ms: Date.now() - startedAt
1550
+ });
1551
+ }
1284
1552
  this.callbacks.onHead?.(sid, upstream.status);
1285
1553
  try {
1286
1554
  if (upstream.body) {
@@ -1356,7 +1624,7 @@ function connectTunnel(options) {
1356
1624
  } = options;
1357
1625
  const tunnelUrl = getTunnelUrlConfig();
1358
1626
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
1359
- return new Promise((resolve, reject) => {
1627
+ return new Promise((resolve2, reject) => {
1360
1628
  const ws = new WebSocket2(url, {
1361
1629
  headers: {
1362
1630
  Authorization: authHeader
@@ -1421,7 +1689,7 @@ function connectTunnel(options) {
1421
1689
  clearTimeout(connectionTimeout);
1422
1690
  const connectedAgentId = message.agent_id ?? agentId;
1423
1691
  onConnected?.(connectedAgentId);
1424
- resolve({
1692
+ resolve2({
1425
1693
  ws,
1426
1694
  close: () => ws.close(1e3, "CLI shutdown")
1427
1695
  });
@@ -1543,6 +1811,12 @@ function messageIdOf(m) {
1543
1811
  const infoId = m.info?.id;
1544
1812
  return typeof infoId === "string" ? infoId : void 0;
1545
1813
  }
1814
+ var LOG_LEVELS = {
1815
+ debug: 0,
1816
+ info: 1,
1817
+ warn: 2,
1818
+ error: 3
1819
+ };
1546
1820
  var DEFAULT_RETRY_POLICY = {
1547
1821
  maxAttempts: 6,
1548
1822
  baseDelayMs: 500,
@@ -1550,7 +1824,10 @@ var DEFAULT_RETRY_POLICY = {
1550
1824
  };
1551
1825
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1552
1826
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1553
- var DEFAULT_DISPATCH_CONFIRM_MS = 6e3;
1827
+ var DEFAULT_STUCK_QUEUED_MS = 6e4;
1828
+ var HEARTBEAT_MS = 6e4;
1829
+ var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
1830
+ var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
1554
1831
  var ChannelAuthError = class extends Error {
1555
1832
  constructor(message) {
1556
1833
  super(message);
@@ -1585,10 +1862,18 @@ var ChannelDriver = class {
1585
1862
  sleep;
1586
1863
  pausedPollIntervalMs;
1587
1864
  pausedMaxWaitMs;
1588
- dispatchConfirmMs;
1865
+ stuckQueuedMs;
1589
1866
  now;
1590
1867
  /** Cache of conversationId → opencode sessionId. */
1591
1868
  sessions = /* @__PURE__ */ new Map();
1869
+ /**
1870
+ * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
1871
+ * longer idempotent (no caller-supplied `messageID`), and its read-back picks
1872
+ * "the one new user row" — which is only unambiguous if no OTHER dispatch into
1873
+ * the SAME session interleaves its snapshot→POST→read-back. This map chains each
1874
+ * session's dispatches so they run serially; distinct sessions stay concurrent.
1875
+ */
1876
+ sessionDispatchLocks = /* @__PURE__ */ new Map();
1592
1877
  /**
1593
1878
  * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1594
1879
  * session: one polling loop services all of that session's in-flight messages.
@@ -1605,6 +1890,63 @@ var ChannelDriver = class {
1605
1890
  * a steady-state-poll re-dispatch will not double-run the message.
1606
1891
  */
1607
1892
  dispatched = /* @__PURE__ */ new Set();
1893
+ /**
1894
+ * Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
1895
+ * Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
1896
+ * so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
1897
+ * it is re-adopted and removed when its watcher settles or it is observed off
1898
+ * the processing list.
1899
+ */
1900
+ readopted = /* @__PURE__ */ new Set();
1901
+ /**
1902
+ * "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
1903
+ * re-adopted running/orphan row's watcher hit its `processed_at`-anchored
1904
+ * deadline (or an orphan whose window already elapsed): the still-`processing`
1905
+ * server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
1906
+ * drain until the 15-min cron resets it — spamming new turns.
1907
+ *
1908
+ * CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
1909
+ * does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
1910
+ * in opencode must still be delivered via `markDone` on the next drain — so
1911
+ * `readoptOne` computes `state` FIRST and this set is checked only on the
1912
+ * non-done path. It is cleared once the row leaves the processing list (cron
1913
+ * reset → it drains normally as `pending`), so it can never leak.
1914
+ */
1915
+ dontRedispatch = /* @__PURE__ */ new Set();
1916
+ /**
1917
+ * "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
1918
+ * re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
1919
+ * never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
1920
+ * that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
1921
+ * markDone failure must NOT land here (it must still retry next drain). Separate
1922
+ * from `dontRedispatch` because the two concerns are independent: a row can need
1923
+ * "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
1924
+ * the row leaves the processing list, exactly like `dontRedispatch`.
1925
+ */
1926
+ doneUndeliverable = /* @__PURE__ */ new Set();
1927
+ /**
1928
+ * "Already emitted `readopt_poll_unresolved` for this row" (#229). The b1 /
1929
+ * unreadable-status re-evaluate leaf leaves the row UN-tracked so it is re-read
1930
+ * every ~2s drain until the status map becomes readable — but the server-visible
1931
+ * signal is an OUTCOME, so it must fire at most ONCE per row, not once per drain
1932
+ * (Bugbot "Re-adopt signals flood every drain"). Cleared when the row leaves the
1933
+ * processing list, exactly like `dontRedispatch`/`doneUndeliverable`.
1934
+ */
1935
+ readoptPollUnresolvedSignalled = /* @__PURE__ */ new Set();
1936
+ /**
1937
+ * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
1938
+ * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
1939
+ * is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
1940
+ * persist hasn't landed before tick N+1 re-reads the still-null
1941
+ * `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
1942
+ * A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
1943
+ * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
1944
+ * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
1945
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
1946
+ * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
1947
+ * so the NEXT tick may retry exactly once more).
1948
+ */
1949
+ awaitingReadopt = /* @__PURE__ */ new Set();
1608
1950
  /**
1609
1951
  * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1610
1952
  * first session creation so drain-created sessions are rooted at the project
@@ -1612,8 +1954,43 @@ var ChannelDriver = class {
1612
1954
  * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1613
1955
  */
1614
1956
  opencodeDirectory = void 0;
1957
+ /**
1958
+ * Cache of opencode `sessionId → parentID` (its parent session, or `null` when
1959
+ * the session is a root with no parent). Sub-agents spawned via the `task` tool
1960
+ * run in CHILD sessions whose `parentID` chains up to the Evident-created
1961
+ * (watched) session; we resolve this once per session so a child-session
1962
+ * question/permission can be attributed to the watched session's subtree
1963
+ * (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
1964
+ * entry = not yet resolved; `null` = resolved root (stop walking).
1965
+ */
1966
+ sessionParents = /* @__PURE__ */ new Map();
1967
+ /**
1968
+ * Per-session OpenCode title cache (#310), keyed by sessionId. Only a resolved
1969
+ * NON-EMPTY name is stored (terminal — a real session name won't later un-name),
1970
+ * so we do NOT re-GET `/session/:id` every tick. A missing entry = not yet
1971
+ * resolved OR resolved-but-still-empty → re-fetch on next need, since OpenCode
1972
+ * names sessions asynchronously mid-turn. Driver-level (not per-watcher) so both
1973
+ * the watcher completion path AND the restart-recovery re-adopt path (which has
1974
+ * no watcher) can resolve the title.
1975
+ */
1976
+ sessionTitles = /* @__PURE__ */ new Map();
1615
1977
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1616
1978
  draining = false;
1979
+ /**
1980
+ * The currently-executing `drainPending()` promise, or null when idle. Lets a
1981
+ * graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
1982
+ * is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
1983
+ * drain that entered before `stop()` still registers its watcher).
1984
+ */
1985
+ activeDrain = null;
1986
+ /**
1987
+ * Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
1988
+ * dispatches NEW work (it returns 0 immediately) — but the per-session watcher
1989
+ * loops already running keep going so in-flight turns can finish and deliver
1990
+ * their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
1991
+ * and stops opencode.
1992
+ */
1993
+ stopped = false;
1617
1994
  constructor(config2) {
1618
1995
  this.agentId = config2.agentId;
1619
1996
  this.port = config2.port;
@@ -1627,7 +2004,7 @@ var ChannelDriver = class {
1627
2004
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1628
2005
  this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1629
2006
  this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
1630
- this.dispatchConfirmMs = config2.dispatchConfirmMs ?? DEFAULT_DISPATCH_CONFIRM_MS;
2007
+ this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
1631
2008
  this.now = config2.now ?? (() => Date.now());
1632
2009
  }
1633
2010
  /** The IPv4-loopback base URL for the local `opencode serve`. */
@@ -1645,8 +2022,21 @@ var ChannelDriver = class {
1645
2022
  * @returns the number of messages NEWLY dispatched to opencode's native queue.
1646
2023
  */
1647
2024
  async drainPending() {
2025
+ if (this.stopped) return 0;
1648
2026
  if (this.draining) return 0;
1649
2027
  this.draining = true;
2028
+ const run2 = this.runDrain();
2029
+ this.activeDrain = run2.then(
2030
+ () => {
2031
+ this.activeDrain = null;
2032
+ },
2033
+ () => {
2034
+ this.activeDrain = null;
2035
+ }
2036
+ );
2037
+ return run2;
2038
+ }
2039
+ async runDrain() {
1650
2040
  let dispatched = 0;
1651
2041
  try {
1652
2042
  const conversations = await this.getPendingConversations();
@@ -1658,8 +2048,10 @@ var ChannelDriver = class {
1658
2048
  });
1659
2049
  }
1660
2050
  for (const conv of conversations) {
2051
+ if (this.stopped) break;
1661
2052
  dispatched += await this.processConversation(conv);
1662
2053
  }
2054
+ await this.readoptProcessing();
1663
2055
  } finally {
1664
2056
  this.draining = false;
1665
2057
  }
@@ -1677,6 +2069,73 @@ var ChannelDriver = class {
1677
2069
  }
1678
2070
  return false;
1679
2071
  }
2072
+ /**
2073
+ * OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
2074
+ * exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
2075
+ * `watchers` entry whose `inFlight` set is non-empty — the same predicate
2076
+ * `hasInFlightWatchers()` uses, lifted to return the ids.
2077
+ *
2078
+ * Deliberately does NOT include `this.sessions` (the permanent, never-pruned
2079
+ * conversation→session cache). Protecting every bound-but-idle session there
2080
+ * would shield nearly every session and defeat cleanup — AND it is unnecessary:
2081
+ * `ensureSession` is self-healing (it recreates a session whose id no longer
2082
+ * exists), so deleting an idle bound session is harmless — the conversation's
2083
+ * next turn transparently rebinds a fresh one. The only thing worth protecting
2084
+ * is a session with a turn ACTIVELY in flight right now: tearing that down
2085
+ * mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
2086
+ */
2087
+ protectedSessionIds() {
2088
+ const ids = /* @__PURE__ */ new Set();
2089
+ for (const [sessionId, watcher] of this.watchers) {
2090
+ if (watcher.inFlight.size > 0) ids.add(sessionId);
2091
+ }
2092
+ return ids;
2093
+ }
2094
+ /**
2095
+ * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
2096
+ * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
2097
+ * — but the watcher loops already tracking in-flight turns keep running, so a
2098
+ * turn that has finished (or is about to) still fires `markDone` and delivers
2099
+ * its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
2100
+ */
2101
+ stop() {
2102
+ this.stopped = true;
2103
+ }
2104
+ /**
2105
+ * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
2106
+ * graceful shutdown, so a turn whose reply is ready — or completes within the
2107
+ * window — is delivered before the process exits, instead of being cut off and
2108
+ * left for the ADR-0046 restart-recovery path.
2109
+ *
2110
+ * Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
2111
+ * far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
2112
+ * window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
2113
+ * set empties OR the timeout elapses. Anything still in flight at the timeout is
2114
+ * safe to abandon — it stays `processing` server-side and is re-adopted on the
2115
+ * next runner start (ADR-0046).
2116
+ *
2117
+ * @returns true if all in-flight work settled within the window; false if the
2118
+ * timeout elapsed with work still in flight.
2119
+ */
2120
+ async waitForInFlight(timeoutMs) {
2121
+ const deadline = this.now() + timeoutMs;
2122
+ const step = Math.min(this.pausedPollIntervalMs, 250);
2123
+ if (this.activeDrain) {
2124
+ let drainSettled = false;
2125
+ void this.activeDrain.then(() => {
2126
+ drainSettled = true;
2127
+ });
2128
+ while (!drainSettled) {
2129
+ if (this.now() >= deadline) return false;
2130
+ await this.sleep(step);
2131
+ }
2132
+ }
2133
+ while (this.hasInFlightWatchers()) {
2134
+ if (this.now() >= deadline) return false;
2135
+ await this.sleep(step);
2136
+ }
2137
+ return true;
2138
+ }
1680
2139
  /**
1681
2140
  * Await all outstanding per-session watchers (WI-3).
1682
2141
  *
@@ -1713,15 +2172,16 @@ var ChannelDriver = class {
1713
2172
  let dispatched = 0;
1714
2173
  let skippedAlreadyDispatched = 0;
1715
2174
  for (const message of messages) {
2175
+ if (this.stopped) break;
1716
2176
  if (this.dispatched.has(message.id)) {
1717
2177
  skippedAlreadyDispatched += 1;
1718
2178
  continue;
1719
2179
  }
1720
- const opencodeMessageId = opencodeMessageIdFor2(message.id);
1721
2180
  const options = {
1722
2181
  agent: message.opencode_agent ?? void 0,
1723
2182
  model: message.opencode_model ?? void 0
1724
2183
  };
2184
+ let opencodeMessageId;
1725
2185
  try {
1726
2186
  this.log({
1727
2187
  level: "info",
@@ -1729,10 +2189,23 @@ var ChannelDriver = class {
1729
2189
  conversation_id: conv.id,
1730
2190
  message_id: message.id
1731
2191
  });
1732
- await sendPromptAsync(this.port, sessionId, message.content, options, opencodeMessageId);
2192
+ opencodeMessageId = await this.dispatchLocked(
2193
+ sessionId,
2194
+ () => sendPromptAsync(this.port, sessionId, message.content, options)
2195
+ );
1733
2196
  } catch (err) {
1734
2197
  if (err instanceof ChannelAuthError) throw err;
1735
2198
  this.dispatched.delete(message.id);
2199
+ if (await sessionExists(this.port, sessionId) === false) {
2200
+ this.sessions.delete(conv.id);
2201
+ this.log({
2202
+ level: "warn",
2203
+ 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.`,
2204
+ conversation_id: conv.id,
2205
+ message_id: message.id
2206
+ });
2207
+ break;
2208
+ }
1736
2209
  await this.markFailed(conv.id, message.id).catch(() => {
1737
2210
  });
1738
2211
  this.log({
@@ -1743,13 +2216,23 @@ var ChannelDriver = class {
1743
2216
  });
1744
2217
  continue;
1745
2218
  }
2219
+ if (opencodeMessageId === null) {
2220
+ this.log({
2221
+ level: "warn",
2222
+ 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`,
2223
+ conversation_id: conv.id,
2224
+ message_id: message.id
2225
+ });
2226
+ continue;
2227
+ }
1746
2228
  this.dispatched.add(message.id);
1747
2229
  this.registerInFlight(conv, sessionId, message, opencodeMessageId);
1748
2230
  dispatched += 1;
2231
+ void this.postSignal(conv.id, message.id, "dispatched");
1749
2232
  }
1750
2233
  if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
1751
2234
  this.log({
1752
- level: "error",
2235
+ level: "warn",
1753
2236
  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).`,
1754
2237
  conversation_id: conv.id
1755
2238
  });
@@ -1758,16 +2241,33 @@ var ChannelDriver = class {
1758
2241
  return dispatched;
1759
2242
  }
1760
2243
  async ensureSession(conv) {
1761
- const cached = this.sessions.get(conv.id);
1762
- if (cached) return cached;
1763
- if (conv.opencode_session_id) {
1764
- this.sessions.set(conv.id, conv.opencode_session_id);
1765
- return conv.opencode_session_id;
2244
+ const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
2245
+ if (bound) {
2246
+ const exists = await sessionExists(this.port, bound);
2247
+ if (exists === false) {
2248
+ this.log({
2249
+ level: "debug",
2250
+ 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.`,
2251
+ conversation_id: conv.id
2252
+ });
2253
+ this.sessions.delete(conv.id);
2254
+ return this.createAndBindSession(conv.id);
2255
+ }
2256
+ this.sessions.set(conv.id, bound);
2257
+ return bound;
1766
2258
  }
2259
+ return this.createAndBindSession(conv.id);
2260
+ }
2261
+ /**
2262
+ * Create a fresh OpenCode session for a conversation, cache the binding, and
2263
+ * best-effort persist it server-side. Shared by the first-ever bind and the
2264
+ * self-heal recreate path in `ensureSession`.
2265
+ */
2266
+ async createAndBindSession(conversationId) {
1767
2267
  const directory = await this.resolveOpenCodeDirectory();
1768
2268
  const sessionId = await createOpenCodeSession(this.port, directory);
1769
- this.sessions.set(conv.id, sessionId);
1770
- await this.persistSession(conv.id, sessionId).catch(() => {
2269
+ this.sessions.set(conversationId, sessionId);
2270
+ await this.persistSession(conversationId, sessionId).catch(() => {
1771
2271
  });
1772
2272
  return sessionId;
1773
2273
  }
@@ -1781,7 +2281,7 @@ var ChannelDriver = class {
1781
2281
  this.opencodeDirectory = await getOpenCodeDirectory(this.port);
1782
2282
  if (!this.opencodeDirectory) {
1783
2283
  this.log({
1784
- level: "info",
2284
+ level: "warn",
1785
2285
  message: "Could not determine opencode directory (GET /path) \u2014 new sessions may not appear in opencode web"
1786
2286
  });
1787
2287
  }
@@ -1790,6 +2290,25 @@ var ChannelDriver = class {
1790
2290
  // -------------------------------------------------------------------------
1791
2291
  // Per-session watcher (WI-3)
1792
2292
  // -------------------------------------------------------------------------
2293
+ /**
2294
+ * Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
2295
+ * opencode session (Task 2.1a), so two dispatches into the SAME session can
2296
+ * never interleave and mis-correlate their read-backs. Distinct sessions run
2297
+ * concurrently. The chained tail intentionally ignores the prior result/error
2298
+ * (each dispatch reports its own outcome to its caller).
2299
+ */
2300
+ dispatchLocked(sessionId, fn) {
2301
+ const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
2302
+ const run2 = prior.then(fn, fn);
2303
+ this.sessionDispatchLocks.set(
2304
+ sessionId,
2305
+ run2.then(
2306
+ () => void 0,
2307
+ () => void 0
2308
+ )
2309
+ );
2310
+ return run2;
2311
+ }
1793
2312
  /** Register a freshly-dispatched message with its session's watcher state. */
1794
2313
  registerInFlight(conv, sessionId, message, opencodeMessageId) {
1795
2314
  let watcher = this.watchers.get(sessionId);
@@ -1799,7 +2318,9 @@ var ChannelDriver = class {
1799
2318
  inFlight: /* @__PURE__ */ new Map(),
1800
2319
  loop: null,
1801
2320
  reportedQuestions: /* @__PURE__ */ new Set(),
1802
- reportedPermissions: /* @__PURE__ */ new Set()
2321
+ reportedPermissions: /* @__PURE__ */ new Set(),
2322
+ lastGoodPollAt: this.now(),
2323
+ hadUsablePoll: false
1803
2324
  };
1804
2325
  this.watchers.set(sessionId, watcher);
1805
2326
  }
@@ -1809,9 +2330,92 @@ var ChannelDriver = class {
1809
2330
  opencodeMessageId,
1810
2331
  message,
1811
2332
  dispatchedAt: now,
2333
+ processingAnchorMs: now,
1812
2334
  deadline: now + this.pausedMaxWaitMs,
1813
2335
  started: false,
1814
- done: false
2336
+ done: false,
2337
+ stuckReported: false,
2338
+ lastAliveAt: 0,
2339
+ aliveInFlight: false,
2340
+ awaitingHumanLatched: false,
2341
+ pausedOnQuestion: false,
2342
+ pausedOnPermission: false,
2343
+ pausedClearConfirmed: false,
2344
+ pausedInFlight: false,
2345
+ deliveryDeadlineAnchored: false
2346
+ });
2347
+ }
2348
+ /**
2349
+ * Register a RE-ADOPTED `processing` message with its session watcher
2350
+ * (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
2351
+ * `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
2352
+ * `now`, so the paused/queued/unreachable cases settle on the same wall-clock a
2353
+ * fresh dispatch would (10 min after `processed_at`, not 10 min from now).
2354
+ *
2355
+ * This re-attaches into the SAME watcher, so the ADR-0047 progressing-vs-paused
2356
+ * give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
2357
+ * opencode reports ACTIVELY `running` is watched to completion (its liveness
2358
+ * heartbeat keeps the cron off its row), while a re-adopted turn that is paused
2359
+ * awaiting a human — or queued/unreachable — is still bounded by `deadline` and
2360
+ * handed to the cron. The old "the `deadline` must settle before the ~15-min
2361
+ * cron or they double-drive" reasoning is superseded: liveness now settles the
2362
+ * actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
2363
+ * (only the appear-guard uses it).
2364
+ *
2365
+ * `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
2366
+ * `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
2367
+ * fresh-run path these differ (a fresh opencode id under the same server row).
2368
+ *
2369
+ * `started` is set true so the watcher does NOT re-`markProcessing` a row the
2370
+ * server already flipped to `processing`; the running/done transitions still
2371
+ * fire from the watcher's normal branches.
2372
+ */
2373
+ registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
2374
+ let watcher = this.watchers.get(sessionId);
2375
+ if (!watcher) {
2376
+ watcher = {
2377
+ conv,
2378
+ inFlight: /* @__PURE__ */ new Map(),
2379
+ loop: null,
2380
+ reportedQuestions: /* @__PURE__ */ new Set(),
2381
+ reportedPermissions: /* @__PURE__ */ new Set(),
2382
+ lastGoodPollAt: this.now(),
2383
+ hadUsablePoll: false
2384
+ };
2385
+ this.watchers.set(sessionId, watcher);
2386
+ }
2387
+ watcher.inFlight.set(message.id, {
2388
+ evidentMessageId: message.id,
2389
+ opencodeMessageId,
2390
+ message,
2391
+ dispatchedAt: this.now(),
2392
+ // Anchor the absolute-age ceiling to the SERVER-SIDE `processed_at` (the same
2393
+ // value seeding `deadline`), NOT `dispatchedAt` — so a re-adopted zombie's age
2394
+ // reflects the real turn duration and the ceiling fires on the ORIGINAL turn.
2395
+ processingAnchorMs: processedAtMs,
2396
+ deadline: processedAtMs + this.pausedMaxWaitMs,
2397
+ // The server row is ALREADY `processing`; do not re-fire markProcessing.
2398
+ started: true,
2399
+ done: false,
2400
+ // Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
2401
+ // AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
2402
+ // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2403
+ // re-adopted row left wedged in `queued` still emits the signal once
2404
+ // (#210/#220 observability).
2405
+ stuckReported: false,
2406
+ // Task 5.2: a re-adopted actively-running row re-attaches into the SAME
2407
+ // watcher and so hits the SAME actively-running heartbeat branch in
2408
+ // `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
2409
+ // re-adopted and is confirming this row alive" via that `alive` heartbeat,
2410
+ // with no extra `re_adopted` signal needed (folds old WI-6).
2411
+ lastAliveAt: 0,
2412
+ aliveInFlight: false,
2413
+ awaitingHumanLatched: false,
2414
+ pausedOnQuestion: false,
2415
+ pausedOnPermission: false,
2416
+ pausedClearConfirmed: false,
2417
+ pausedInFlight: false,
2418
+ deliveryDeadlineAnchored: false
1815
2419
  });
1816
2420
  }
1817
2421
  /**
@@ -1862,12 +2466,30 @@ var ChannelDriver = class {
1862
2466
  messages = Array.isArray(body) ? body : null;
1863
2467
  }
1864
2468
  } catch {
1865
- continue;
1866
2469
  }
2470
+ if (messages != null && messages.length > 0) {
2471
+ watcher.lastGoodPollAt = this.now();
2472
+ watcher.hadUsablePoll = true;
2473
+ } else {
2474
+ const emptyButReachable = messages != null;
2475
+ const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
2476
+ if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
2477
+ continue;
2478
+ }
2479
+ }
2480
+ const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
1867
2481
  for (const inFlight of [...watcher.inFlight.values()]) {
1868
- await this.serviceInFlightMessage(sessionId, watcher, inFlight, messages);
2482
+ await this.serviceInFlightMessage(
2483
+ sessionId,
2484
+ watcher,
2485
+ inFlight,
2486
+ messages,
2487
+ openQuestions,
2488
+ openPermissions,
2489
+ questionsPolledOk,
2490
+ permissionsPolledOk
2491
+ );
1869
2492
  }
1870
- await this.pollInteractions(sessionId, watcher, messages);
1871
2493
  }
1872
2494
  } catch (err) {
1873
2495
  if (err instanceof ChannelAuthError) {
@@ -1877,6 +2499,7 @@ var ChannelDriver = class {
1877
2499
  conversation_id: watcher.conv.id
1878
2500
  });
1879
2501
  for (const evidentMessageId of [...watcher.inFlight.keys()]) {
2502
+ this.readopted.delete(evidentMessageId);
1880
2503
  this.removeInFlight(watcher, evidentMessageId);
1881
2504
  }
1882
2505
  return;
@@ -1888,23 +2511,55 @@ var ChannelDriver = class {
1888
2511
  });
1889
2512
  }
1890
2513
  }
2514
+ /**
2515
+ * On FIRST observing a terminal (done/failed) state, ensure the delivery
2516
+ * (markDone/markFailed) transient-retry path has a real window. A long
2517
+ * ACTIVELY-running turn is kept past its original `deadline`, so by completion
2518
+ * `now >= deadline` already holds and the retry bound below would fire on the
2519
+ * first transient PATCH failure — dropping the message before its reply lands
2520
+ * (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
2521
+ * to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
2522
+ * or past now, so a still-ample window is left untouched.
2523
+ */
2524
+ anchorDeliveryDeadline(inFlight) {
2525
+ if (inFlight.deliveryDeadlineAnchored) return;
2526
+ inFlight.deliveryDeadlineAnchored = true;
2527
+ if (this.now() >= inFlight.deadline) {
2528
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2529
+ }
2530
+ }
1891
2531
  /**
1892
2532
  * Drive ONE in-flight message's lifecycle from the tick's message snapshot.
1893
2533
  * Fires markProcessing on queued→running and markDone on done (each once),
1894
2534
  * applies the idle-path re-dispatch guard, and removes the message from the
1895
2535
  * in-flight set on completion or timeout.
1896
2536
  */
1897
- async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
2537
+ async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
1898
2538
  const conv = watcher.conv;
1899
2539
  const state = messageRunState(messages, inFlight.opencodeMessageId);
1900
- if ((state === "running" || state === "done") && !inFlight.started) {
2540
+ const id = inFlight.evidentMessageId;
2541
+ if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
2542
+ else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
2543
+ if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
2544
+ else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
2545
+ const observedOpen = openQuestions.has(id) || openPermissions.has(id);
2546
+ const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
2547
+ const awaitingHuman = observedOpen || latchedPaused;
2548
+ if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
2549
+ const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
1901
2550
  let claimed;
1902
2551
  try {
1903
- claimed = await this.markProcessing(conv.id, inFlight.evidentMessageId, sessionId);
2552
+ claimed = await this.markProcessing(
2553
+ conv.id,
2554
+ inFlight.evidentMessageId,
2555
+ sessionId,
2556
+ inFlight.opencodeMessageId,
2557
+ title
2558
+ );
1904
2559
  } catch (err) {
1905
2560
  if (err instanceof ChannelAuthError) throw err;
1906
2561
  this.log({
1907
- level: "error",
2562
+ level: "warn",
1908
2563
  message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
1909
2564
  conversation_id: conv.id,
1910
2565
  message_id: inFlight.evidentMessageId
@@ -1914,7 +2569,7 @@ var ChannelDriver = class {
1914
2569
  inFlight.started = true;
1915
2570
  if (!claimed) {
1916
2571
  this.log({
1917
- level: "info",
2572
+ level: "debug",
1918
2573
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
1919
2574
  conversation_id: conv.id,
1920
2575
  message_id: inFlight.evidentMessageId
@@ -1922,6 +2577,7 @@ var ChannelDriver = class {
1922
2577
  }
1923
2578
  }
1924
2579
  if (state === "done") {
2580
+ this.anchorDeliveryDeadline(inFlight);
1925
2581
  if (!inFlight.done) {
1926
2582
  this.log({
1927
2583
  level: "info",
@@ -1929,13 +2585,20 @@ var ChannelDriver = class {
1929
2585
  conversation_id: conv.id,
1930
2586
  message_id: inFlight.evidentMessageId
1931
2587
  });
2588
+ const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
1932
2589
  try {
1933
- await this.markDone(conv.id, inFlight.evidentMessageId, sessionId);
2590
+ await this.markDone(
2591
+ conv.id,
2592
+ inFlight.evidentMessageId,
2593
+ sessionId,
2594
+ inFlight.opencodeMessageId,
2595
+ title
2596
+ );
1934
2597
  } catch (err) {
1935
2598
  if (err instanceof ChannelAuthError) throw err;
1936
2599
  if (err instanceof ChannelTerminalError) {
1937
2600
  this.log({
1938
- level: "error",
2601
+ level: "warn",
1939
2602
  message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
1940
2603
  conversation_id: conv.id,
1941
2604
  message_id: inFlight.evidentMessageId
@@ -1945,7 +2608,7 @@ var ChannelDriver = class {
1945
2608
  }
1946
2609
  if (this.now() >= inFlight.deadline) {
1947
2610
  this.log({
1948
- level: "error",
2611
+ level: "warn",
1949
2612
  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)}`,
1950
2613
  conversation_id: conv.id,
1951
2614
  message_id: inFlight.evidentMessageId
@@ -1954,7 +2617,7 @@ var ChannelDriver = class {
1954
2617
  return;
1955
2618
  }
1956
2619
  this.log({
1957
- level: "error",
2620
+ level: "warn",
1958
2621
  message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
1959
2622
  conversation_id: conv.id,
1960
2623
  message_id: inFlight.evidentMessageId
@@ -1966,60 +2629,583 @@ var ChannelDriver = class {
1966
2629
  this.removeInFlight(watcher, inFlight.evidentMessageId);
1967
2630
  return;
1968
2631
  }
1969
- if (state === "unknown") {
1970
- if (this.now() - inFlight.dispatchedAt >= this.dispatchConfirmMs) {
1971
- await this.redispatchInFlight(sessionId, inFlight);
2632
+ if (state === "failed") {
2633
+ this.anchorDeliveryDeadline(inFlight);
2634
+ if (!inFlight.done) {
2635
+ const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
2636
+ this.log({
2637
+ level: "error",
2638
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2639
+ conversation_id: conv.id,
2640
+ message_id: inFlight.evidentMessageId
2641
+ });
2642
+ try {
2643
+ await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
2644
+ } catch (err) {
2645
+ if (err instanceof ChannelAuthError) throw err;
2646
+ if (err instanceof ChannelTerminalError) {
2647
+ this.log({
2648
+ level: "warn",
2649
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2650
+ conversation_id: conv.id,
2651
+ message_id: inFlight.evidentMessageId
2652
+ });
2653
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2654
+ return;
2655
+ }
2656
+ if (this.now() >= inFlight.deadline) {
2657
+ this.log({
2658
+ level: "warn",
2659
+ 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)}`,
2660
+ conversation_id: conv.id,
2661
+ message_id: inFlight.evidentMessageId
2662
+ });
2663
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2664
+ return;
2665
+ }
2666
+ this.log({
2667
+ level: "warn",
2668
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2669
+ conversation_id: conv.id,
2670
+ message_id: inFlight.evidentMessageId
2671
+ });
2672
+ return;
2673
+ }
2674
+ inFlight.done = true;
1972
2675
  }
2676
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2677
+ return;
1973
2678
  }
1974
- if (this.now() >= inFlight.deadline) {
2679
+ const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
2680
+ const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
2681
+ if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
2682
+ inFlight.stuckReported = true;
2683
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2684
+ stuck_for_ms: this.now() - inFlight.dispatchedAt
2685
+ });
2686
+ }
2687
+ const activelyRunning = state === "running" && !awaitingHuman;
2688
+ if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
1975
2689
  this.log({
1976
- level: "info",
1977
- message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
2690
+ level: "warn",
2691
+ 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`,
1978
2692
  conversation_id: conv.id,
1979
2693
  message_id: inFlight.evidentMessageId
1980
2694
  });
2695
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2696
+ watched_for_ms: this.now() - inFlight.processingAnchorMs
2697
+ });
1981
2698
  this.removeInFlight(watcher, inFlight.evidentMessageId);
2699
+ return;
1982
2700
  }
2701
+ if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
2702
+ inFlight.aliveInFlight = true;
2703
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
2704
+ inFlight.aliveInFlight = false;
2705
+ if (ok) inFlight.lastAliveAt = this.now();
2706
+ });
2707
+ }
2708
+ if (awaitingHuman) {
2709
+ if (!inFlight.awaitingHumanLatched) {
2710
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2711
+ inFlight.awaitingHumanLatched = true;
2712
+ }
2713
+ if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
2714
+ inFlight.pausedInFlight = true;
2715
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
2716
+ inFlight.pausedInFlight = false;
2717
+ if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
2718
+ });
2719
+ }
2720
+ } else if (inFlight.awaitingHumanLatched) {
2721
+ inFlight.awaitingHumanLatched = false;
2722
+ inFlight.pausedOnQuestion = false;
2723
+ inFlight.pausedOnPermission = false;
2724
+ inFlight.pausedClearConfirmed = false;
2725
+ }
2726
+ const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
2727
+ const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
2728
+ (sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
2729
+ );
2730
+ const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
2731
+ if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
2732
+ this.log({
2733
+ level: "debug",
2734
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
2735
+ conversation_id: conv.id,
2736
+ message_id: inFlight.evidentMessageId
2737
+ });
2738
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2739
+ watched_for_ms: this.now() - inFlight.dispatchedAt
2740
+ });
2741
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2742
+ }
2743
+ }
2744
+ // -------------------------------------------------------------------------
2745
+ // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
2746
+ // -------------------------------------------------------------------------
2747
+ /**
2748
+ * Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
2749
+ *
2750
+ * The pending drain only re-drives `pending` rows; a message already flipped to
2751
+ * `processing` before the runner died is watched by nobody until the 15-min
2752
+ * cron resets it. Here we fetch those rows, and per row resolve its correlated
2753
+ * reply against opencode's OWN session store — completing, re-attaching, or
2754
+ * (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
2755
+ * is idempotent per message (Invariant 2): a row a watcher already tracks is
2756
+ * skipped in `readoptOne` — one driver, no double-drive.
2757
+ *
2758
+ * Only `ChannelAuthError` propagates (to `drainPending`, like the pending
2759
+ * path); every other early return LOGS a reason with context — no silent drop.
2760
+ */
2761
+ async readoptProcessing() {
2762
+ const rows = await this.getProcessingMessages();
2763
+ if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0 || this.readoptPollUnresolvedSignalled.size > 0) {
2764
+ const stillProcessing = new Set(rows.map((r) => r.id));
2765
+ for (const id of [
2766
+ ...this.dontRedispatch,
2767
+ ...this.doneUndeliverable,
2768
+ ...this.readoptPollUnresolvedSignalled
2769
+ ]) {
2770
+ if (!stillProcessing.has(id)) {
2771
+ const cleared = this.dontRedispatch.delete(id);
2772
+ const clearedUndeliverable = this.doneUndeliverable.delete(id);
2773
+ this.readoptPollUnresolvedSignalled.delete(id);
2774
+ if (cleared || clearedUndeliverable) {
2775
+ this.log({
2776
+ level: "debug",
2777
+ message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
2778
+ message_id: id
2779
+ });
2780
+ }
2781
+ }
2782
+ }
2783
+ }
2784
+ if (rows.length === 0) return;
2785
+ const bySession = /* @__PURE__ */ new Map();
2786
+ for (const row of rows) {
2787
+ if (!row.opencode_session_id) {
2788
+ this.log({
2789
+ level: "warn",
2790
+ message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
2791
+ conversation_id: row.conversation_id,
2792
+ message_id: row.id
2793
+ });
2794
+ continue;
2795
+ }
2796
+ const list = bySession.get(row.opencode_session_id) ?? [];
2797
+ list.push(row);
2798
+ bySession.set(row.opencode_session_id, list);
2799
+ }
2800
+ for (const [sessionId, sessionRows] of bySession) {
2801
+ let messages;
2802
+ try {
2803
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
2804
+ if (!res.ok) {
2805
+ this.log({
2806
+ level: "warn",
2807
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
2808
+ });
2809
+ continue;
2810
+ }
2811
+ const body = await res.json();
2812
+ if (!Array.isArray(body)) {
2813
+ this.log({
2814
+ level: "warn",
2815
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
2816
+ });
2817
+ continue;
2818
+ }
2819
+ messages = body;
2820
+ } catch (err) {
2821
+ this.log({
2822
+ level: "warn",
2823
+ message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
2824
+ });
2825
+ continue;
2826
+ }
2827
+ const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
2828
+ const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
2829
+ for (const row of sessionRows) {
2830
+ await this.readoptOne(sessionId, row, messages, sessionOngoing);
2831
+ }
2832
+ }
2833
+ }
2834
+ /**
2835
+ * Re-adopt ONE `processing` row against the tick's session message snapshot
2836
+ * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
2837
+ *
2838
+ * Branches on `messageRunState(messages, row.opencode_message_id)` — the
2839
+ * opencode-assigned user-message id persisted on the first `processing` PATCH
2840
+ * (#218). A row with a NULL stored id (dispatched but the read-back never landed
2841
+ * before the restart) has no id to correlate → treated as an orphan and
2842
+ * re-dispatched (at most once, see `forceReadoptRun`):
2843
+ * - `done` → `markDone` now (guarded like the watcher's done branch);
2844
+ * - `failed` → `markFailed` with the surfaced error (issue #182), so an
2845
+ * errored turn is reported failed on restart, NOT re-dispatched;
2846
+ * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
2847
+ * tracking the stored id so the reply correlates by it;
2848
+ * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
2849
+ *
2850
+ * Only `ChannelAuthError` propagates.
2851
+ */
2852
+ async readoptOne(sessionId, row, messages, sessionOngoing) {
2853
+ if (this.isTracked(sessionId, row.id)) {
2854
+ this.log({
2855
+ level: "debug",
2856
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
2857
+ conversation_id: row.conversation_id,
2858
+ message_id: row.id
2859
+ });
2860
+ return;
2861
+ }
2862
+ const ocId = row.opencode_message_id;
2863
+ const state = messageRunState(messages, ocId ?? "");
2864
+ if (state === "done") {
2865
+ if (this.doneUndeliverable.has(row.id)) {
2866
+ this.log({
2867
+ level: "debug",
2868
+ message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
2869
+ conversation_id: row.conversation_id,
2870
+ message_id: row.id
2871
+ });
2872
+ return;
2873
+ }
2874
+ this.log({
2875
+ level: "info",
2876
+ message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
2877
+ conversation_id: row.conversation_id,
2878
+ message_id: row.id
2879
+ });
2880
+ try {
2881
+ const title = await this.resolveSessionTitle(sessionId, row.conversation_id);
2882
+ await this.markDone(row.conversation_id, row.id, sessionId, ocId, title);
2883
+ } catch (err) {
2884
+ if (err instanceof ChannelAuthError) throw err;
2885
+ if (err instanceof ChannelTerminalError) {
2886
+ this.doneUndeliverable.add(row.id);
2887
+ this.log({
2888
+ level: "warn",
2889
+ 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}`,
2890
+ conversation_id: row.conversation_id,
2891
+ message_id: row.id
2892
+ });
2893
+ void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
2894
+ return;
2895
+ }
2896
+ this.log({
2897
+ level: "warn",
2898
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2899
+ conversation_id: row.conversation_id,
2900
+ message_id: row.id
2901
+ });
2902
+ return;
2903
+ }
2904
+ this.dontRedispatch.delete(row.id);
2905
+ void this.postSignal(row.conversation_id, row.id, "readopt_done");
2906
+ return;
2907
+ }
2908
+ if (state === "failed") {
2909
+ const error2 = messageError(messages, ocId ?? "") ?? void 0;
2910
+ this.log({
2911
+ level: "error",
2912
+ message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2913
+ conversation_id: row.conversation_id,
2914
+ message_id: row.id
2915
+ });
2916
+ try {
2917
+ await this.markFailed(row.conversation_id, row.id, sessionId, error2);
2918
+ } catch (err) {
2919
+ if (err instanceof ChannelAuthError) throw err;
2920
+ if (err instanceof ChannelTerminalError) {
2921
+ this.doneUndeliverable.add(row.id);
2922
+ this.log({
2923
+ level: "warn",
2924
+ 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}`,
2925
+ conversation_id: row.conversation_id,
2926
+ message_id: row.id
2927
+ });
2928
+ void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
2929
+ return;
2930
+ }
2931
+ this.log({
2932
+ level: "warn",
2933
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2934
+ conversation_id: row.conversation_id,
2935
+ message_id: row.id
2936
+ });
2937
+ return;
2938
+ }
2939
+ this.dontRedispatch.delete(row.id);
2940
+ void this.postSignal(row.conversation_id, row.id, "readopt_failed");
2941
+ return;
2942
+ }
2943
+ if (this.dontRedispatch.has(row.id)) {
2944
+ this.log({
2945
+ level: "debug",
2946
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
2947
+ conversation_id: row.conversation_id,
2948
+ message_id: row.id
2949
+ });
2950
+ return;
2951
+ }
2952
+ let statusReadableOngoing = null;
2953
+ if (state === "running" && ocId) {
2954
+ const reply = findLastAssistantReplyFor(messages, ocId);
2955
+ const shape = this.replyCompletionShape(reply);
2956
+ const ongoing = sessionOngoing;
2957
+ statusReadableOngoing = ongoing;
2958
+ if (ongoing === false) {
2959
+ this.log({
2960
+ level: "info",
2961
+ 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)`,
2962
+ conversation_id: row.conversation_id,
2963
+ message_id: row.id
2964
+ });
2965
+ await this.forceReadoptRun(sessionId, row);
2966
+ return;
2967
+ }
2968
+ if (ongoing === true) {
2969
+ this.log({
2970
+ level: "debug",
2971
+ 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)`,
2972
+ conversation_id: row.conversation_id,
2973
+ message_id: row.id
2974
+ });
2975
+ } else {
2976
+ if (shape === "b1") {
2977
+ this.log({
2978
+ level: "debug",
2979
+ 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`,
2980
+ conversation_id: row.conversation_id,
2981
+ message_id: row.id
2982
+ });
2983
+ if (!this.readoptPollUnresolvedSignalled.has(row.id)) {
2984
+ this.readoptPollUnresolvedSignalled.add(row.id);
2985
+ void this.postSignal(row.conversation_id, row.id, "readopt_poll_unresolved");
2986
+ }
2987
+ return;
2988
+ }
2989
+ this.log({
2990
+ level: "debug",
2991
+ 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`,
2992
+ conversation_id: row.conversation_id,
2993
+ message_id: row.id
2994
+ });
2995
+ }
2996
+ }
2997
+ if (statusReadableOngoing === null && state === "running" && ocId && isPreamblePinnedRunning(messages, ocId)) {
2998
+ const descendantAlive = await this.isAnyDescendantSessionAlive(sessionId);
2999
+ if (descendantAlive === true) {
3000
+ this.log({
3001
+ level: "debug",
3002
+ 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)`,
3003
+ conversation_id: row.conversation_id,
3004
+ message_id: row.id
3005
+ });
3006
+ } else {
3007
+ this.log({
3008
+ level: "info",
3009
+ 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)" : ""}`,
3010
+ conversation_id: row.conversation_id,
3011
+ message_id: row.id
3012
+ });
3013
+ await this.forceReadoptRun(sessionId, row);
3014
+ return;
3015
+ }
3016
+ }
3017
+ if ((state === "running" || state === "queued") && ocId) {
3018
+ const conv = this.convForRow(sessionId, row);
3019
+ const message = this.queuedMessageForRow(row);
3020
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
3021
+ this.dispatched.add(row.id);
3022
+ this.readopted.add(row.id);
3023
+ this.ensureWatcherRunning(sessionId);
3024
+ this.log({
3025
+ level: "debug",
3026
+ message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
3027
+ conversation_id: row.conversation_id,
3028
+ message_id: row.id
3029
+ });
3030
+ void this.postSignal(row.conversation_id, row.id, "readopt_reattached");
3031
+ return;
3032
+ }
3033
+ await this.forceReadoptRun(sessionId, row);
1983
3034
  }
1984
3035
  /**
1985
- * Re-dispatch a message whose user row never appeared (idle-path guard). Safe:
1986
- * opencode treats a duplicate caller-supplied `messageID` as idempotent (PoC
1987
- * fact 9) one user message + one reply even if the original DID land. Resets
1988
- * the dispatch timestamp so the guard doesn't immediately fire again.
3036
+ * Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
3037
+ *
3038
+ * #218/WI-5: the row's user message is absent (never kept, or a null stored id),
3039
+ * so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
3040
+ * read it back, and register the watcher under the assigned id so the reply
3041
+ * correlates server-side.
3042
+ *
3043
+ * ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
3044
+ * id). Without a guard, if this dispatches on tick N but the read-back+persist
3045
+ * hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
3046
+ * tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
3047
+ * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
3048
+ * short-circuit while the row is latched; clear it on a successful dispatch (the
3049
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
3050
+ * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
3051
+ * may retry exactly once more).
3052
+ *
3053
+ * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
3054
+ * `processed_at` (Invariant 1).
1989
3055
  */
1990
- async redispatchInFlight(sessionId, inFlight) {
3056
+ async forceReadoptRun(sessionId, row) {
3057
+ if (this.stopped) {
3058
+ this.log({
3059
+ level: "debug",
3060
+ 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`,
3061
+ conversation_id: row.conversation_id,
3062
+ message_id: row.id
3063
+ });
3064
+ return;
3065
+ }
3066
+ if (this.awaitingReadopt.has(row.id)) {
3067
+ this.log({
3068
+ level: "debug",
3069
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
3070
+ conversation_id: row.conversation_id,
3071
+ message_id: row.id
3072
+ });
3073
+ return;
3074
+ }
3075
+ if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
3076
+ this.dontRedispatch.add(row.id);
3077
+ this.log({
3078
+ level: "debug",
3079
+ 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)`,
3080
+ conversation_id: row.conversation_id,
3081
+ message_id: row.id
3082
+ });
3083
+ void this.postSignal(row.conversation_id, row.id, "readopt_window_elapsed");
3084
+ return;
3085
+ }
1991
3086
  const options = {
1992
- agent: inFlight.message.opencode_agent ?? void 0,
1993
- model: inFlight.message.opencode_model ?? void 0
3087
+ agent: row.opencode_agent ?? void 0,
3088
+ model: row.opencode_model ?? void 0
1994
3089
  };
1995
3090
  this.log({
1996
3091
  level: "info",
1997
- message: `Message ${inFlight.evidentMessageId.slice(0, 8)} not observed after dispatch \u2014 re-dispatching (idle-path guard)`,
1998
- message_id: inFlight.evidentMessageId
3092
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
3093
+ conversation_id: row.conversation_id,
3094
+ message_id: row.id
1999
3095
  });
3096
+ this.awaitingReadopt.add(row.id);
3097
+ let ocId;
2000
3098
  try {
2001
- await sendPromptAsync(
2002
- this.port,
3099
+ ocId = await this.dispatchLocked(
2003
3100
  sessionId,
2004
- inFlight.message.content,
2005
- options,
2006
- inFlight.opencodeMessageId
3101
+ () => sendPromptAsync(this.port, sessionId, row.content, options)
2007
3102
  );
2008
3103
  } catch (err) {
3104
+ this.awaitingReadopt.delete(row.id);
3105
+ if (err instanceof ChannelAuthError) throw err;
2009
3106
  this.log({
2010
- level: "error",
2011
- message: `Re-dispatch failed for message ${inFlight.evidentMessageId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
2012
- message_id: inFlight.evidentMessageId
3107
+ level: "warn",
3108
+ message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3109
+ conversation_id: row.conversation_id,
3110
+ message_id: row.id
2013
3111
  });
3112
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3113
+ return;
2014
3114
  }
2015
- inFlight.dispatchedAt = this.now();
3115
+ if (ocId === null) {
3116
+ this.awaitingReadopt.delete(row.id);
3117
+ this.log({
3118
+ level: "warn",
3119
+ 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`,
3120
+ conversation_id: row.conversation_id,
3121
+ message_id: row.id
3122
+ });
3123
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3124
+ return;
3125
+ }
3126
+ const conv = this.convForRow(sessionId, row);
3127
+ const message = this.queuedMessageForRow(row);
3128
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
3129
+ this.dispatched.add(row.id);
3130
+ this.readopted.add(row.id);
3131
+ this.awaitingReadopt.delete(row.id);
3132
+ this.ensureWatcherRunning(sessionId);
3133
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
3134
+ }
3135
+ /**
3136
+ * True if `evidentMessageId` is already being driven — either in the
3137
+ * authoritative `dispatched` set or a live watcher's in-flight set for this
3138
+ * session (Invariant 2, WI-5). Either signal means a watcher owns the row.
3139
+ */
3140
+ isTracked(sessionId, evidentMessageId) {
3141
+ if (this.dispatched.has(evidentMessageId)) return true;
3142
+ const watcher = this.watchers.get(sessionId);
3143
+ return watcher?.inFlight.has(evidentMessageId) ?? false;
3144
+ }
3145
+ /**
3146
+ * Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
3147
+ * deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
3148
+ * for `processing` rows, but if it is somehow null/unparseable fall back to
3149
+ * `now` (defensive) AND log — a fallback means the anchor is weaker than
3150
+ * intended, which is worth surfacing.
3151
+ */
3152
+ processedAtMs(row) {
3153
+ const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
3154
+ if (!Number.isNaN(parsed)) return parsed;
3155
+ this.log({
3156
+ level: "error",
3157
+ message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
3158
+ conversation_id: row.conversation_id,
3159
+ message_id: row.id
3160
+ });
3161
+ return this.now();
3162
+ }
3163
+ /** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
3164
+ convForRow(sessionId, row) {
3165
+ return {
3166
+ id: row.conversation_id,
3167
+ agent_id: this.agentId,
3168
+ opencode_session_id: sessionId,
3169
+ pending_message_count: 0,
3170
+ oldest_pending_at: row.processed_at
3171
+ };
3172
+ }
3173
+ /** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
3174
+ queuedMessageForRow(row) {
3175
+ return {
3176
+ id: row.id,
3177
+ content: row.content,
3178
+ status: "processing",
3179
+ opencode_agent: row.opencode_agent,
3180
+ opencode_model: row.opencode_model,
3181
+ source_message_id: row.source_message_id,
3182
+ slack_user_id: row.slack_user_id
3183
+ };
2016
3184
  }
2017
3185
  /**
2018
3186
  * Remove a message from the in-flight set AND the authoritative dispatched
2019
3187
  * set. Once the in-flight set empties, the watcher loop's `while` guard exits
2020
3188
  * and its `.finally` removes the session entry from `this.watchers`.
3189
+ *
3190
+ * Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
3191
+ * (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
3192
+ * cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
3193
+ * re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
3194
+ * re-adopted message that completed (`done`) needs no marker — it's leaving
3195
+ * `processing`. This suppresses only re-dispatch: if its reply later completes,
3196
+ * the done branch still delivers it (Bugbot #202).
2021
3197
  */
2022
3198
  removeInFlight(watcher, evidentMessageId) {
3199
+ const inFlight = watcher.inFlight.get(evidentMessageId);
3200
+ if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
3201
+ this.dontRedispatch.add(evidentMessageId);
3202
+ this.log({
3203
+ level: "debug",
3204
+ message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
3205
+ conversation_id: watcher.conv.id,
3206
+ message_id: evidentMessageId
3207
+ });
3208
+ }
2023
3209
  watcher.inFlight.delete(evidentMessageId);
2024
3210
  this.dispatched.delete(evidentMessageId);
2025
3211
  }
@@ -2036,21 +3222,41 @@ var ChannelDriver = class {
2036
3222
  * RUNNING (not done) is the one that paused. With one running message that is
2037
3223
  * unambiguous; with several we prefer an explicit messageID match, else the
2038
3224
  * oldest running message.
3225
+ *
3226
+ * Returns the set of in-flight Evident message ids that are paused awaiting a
3227
+ * human — an outstanding (still-open) question/permission is attributed to them.
3228
+ * `serviceInFlightMessage` uses this to keep an actively-running turn watched
3229
+ * forever (ADR-0047) while still bounding a turn merely blocked on a person who
3230
+ * may never answer. Attribution here covers ALL open interactions, not just
3231
+ * NEW (un-deduped) ones — a question stays "awaiting a human" until answered,
3232
+ * even after it was already surfaced to the channel.
2039
3233
  */
2040
3234
  async pollInteractions(sessionId, watcher, messages) {
3235
+ const openQuestions = /* @__PURE__ */ new Set();
3236
+ const openPermissions = /* @__PURE__ */ new Set();
3237
+ let questionsPolledOk = true;
3238
+ let permissionsPolledOk = true;
2041
3239
  let questions = [];
2042
3240
  try {
2043
3241
  const res = await this.fetchImpl(`${this.opencodeBase}/question`);
2044
3242
  if (res.ok) {
2045
3243
  const body = await res.json();
2046
- questions = Array.isArray(body) ? body : [];
3244
+ if (Array.isArray(body)) {
3245
+ questions = body;
3246
+ } else {
3247
+ questionsPolledOk = false;
3248
+ }
3249
+ } else {
3250
+ questionsPolledOk = false;
2047
3251
  }
2048
3252
  } catch {
3253
+ questionsPolledOk = false;
2049
3254
  }
2050
3255
  for (const q of questions) {
2051
- if (q.sessionID !== sessionId) continue;
2052
- if (watcher.reportedQuestions.has(q.id)) continue;
3256
+ if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
2053
3257
  const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
3258
+ if (paused) openQuestions.add(paused.evidentMessageId);
3259
+ if (watcher.reportedQuestions.has(q.id)) continue;
2054
3260
  const reported = await this.reportInteraction(
2055
3261
  watcher.conv.id,
2056
3262
  "question",
@@ -2064,14 +3270,22 @@ var ChannelDriver = class {
2064
3270
  const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
2065
3271
  if (res.ok) {
2066
3272
  const body = await res.json();
2067
- permissions = Array.isArray(body) ? body : [];
3273
+ if (Array.isArray(body)) {
3274
+ permissions = body;
3275
+ } else {
3276
+ permissionsPolledOk = false;
3277
+ }
3278
+ } else {
3279
+ permissionsPolledOk = false;
2068
3280
  }
2069
3281
  } catch {
3282
+ permissionsPolledOk = false;
2070
3283
  }
2071
3284
  for (const p of permissions) {
2072
- if (p.sessionID !== sessionId) continue;
2073
- if (watcher.reportedPermissions.has(p.id)) continue;
3285
+ if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
2074
3286
  const paused = this.attributeInteraction(watcher, p.messageID, messages);
3287
+ if (paused) openPermissions.add(paused.evidentMessageId);
3288
+ if (watcher.reportedPermissions.has(p.id)) continue;
2075
3289
  const reported = await this.reportInteraction(
2076
3290
  watcher.conv.id,
2077
3291
  "permission",
@@ -2080,6 +3294,173 @@ var ChannelDriver = class {
2080
3294
  );
2081
3295
  if (reported) watcher.reportedPermissions.add(p.id);
2082
3296
  }
3297
+ return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
3298
+ }
3299
+ /**
3300
+ * True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
3301
+ * i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
3302
+ * watched root. Sub-agents spawned via the `task` tool run in child sessions,
3303
+ * so their questions/permissions live under a different `sessionID` that must
3304
+ * still be attributed to the root conversation the watcher owns.
3305
+ *
3306
+ * Parents are cached in `sessionParents` so we walk each session at most once;
3307
+ * a bounded depth cap guards against a cycle or a pathological chain, and any
3308
+ * fetch failure is treated as "not a descendant" (best-effort — the interaction
3309
+ * simply isn't surfaced this tick and is retried next tick once resolvable).
3310
+ */
3311
+ async sessionBelongsTo(sessionId, rootSessionId) {
3312
+ let current = sessionId;
3313
+ for (let depth = 0; current && depth < 32; depth++) {
3314
+ if (current === rootSessionId) return true;
3315
+ const parent = await this.resolveSessionParent(current);
3316
+ if (parent === null || parent === void 0) return false;
3317
+ current = parent;
3318
+ }
3319
+ return false;
3320
+ }
3321
+ /**
3322
+ * Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
3323
+ * `null` for a root session (no parent) and `undefined` when opencode is
3324
+ * unreachable / the session can't be read (so the caller stops walking without
3325
+ * caching a wrong answer — the next tick retries).
3326
+ */
3327
+ async resolveSessionParent(sessionId) {
3328
+ const cached = this.sessionParents.get(sessionId);
3329
+ if (cached !== void 0) return cached;
3330
+ let parent = void 0;
3331
+ try {
3332
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
3333
+ if (res.ok) {
3334
+ const body = await res.json();
3335
+ parent = body && typeof body.parentID === "string" ? body.parentID : null;
3336
+ }
3337
+ } catch {
3338
+ parent = void 0;
3339
+ }
3340
+ if (parent !== void 0) this.sessionParents.set(sessionId, parent);
3341
+ return parent;
3342
+ }
3343
+ /**
3344
+ * Resolve (and cache in `sessionTitles`) the OpenCode session TITLE (#310) so the
3345
+ * status PATCH can carry it into the "Live sessions" list. Driver-level cache so
3346
+ * BOTH the watcher completion path and the restart-recovery re-adopt path (which
3347
+ * has no watcher) can use it. `conversationId` is passed only for log context.
3348
+ * Best-effort:
3349
+ * - a resolved NON-EMPTY title is cached and terminal (a real session name
3350
+ * won't later un-name), so we do NOT re-GET `/session/:id` every tick;
3351
+ * - while the title is still absent/empty we do NOT latch it — OpenCode names
3352
+ * sessions asynchronously mid-turn, so an early call (e.g. at `processing`)
3353
+ * must leave the cache unresolved and re-fetch on the next need so a later
3354
+ * call (e.g. at `done`) picks up the name assigned in the meantime. Such a
3355
+ * call returns `null` (omit the title on THIS PATCH) without caching;
3356
+ * - a failed request likewise leaves the cache unresolved (retry next need)
3357
+ * and returns `null` — it must NEVER throw or block completion.
3358
+ * A failure is logged with agent/session context (no silent catch).
3359
+ */
3360
+ async resolveSessionTitle(sessionId, conversationId) {
3361
+ const cached = this.sessionTitles.get(sessionId);
3362
+ if (cached != null) return cached;
3363
+ try {
3364
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
3365
+ if (res.ok) {
3366
+ const body = await res.json();
3367
+ const title = body && typeof body.title === "string" ? body.title.trim() : "";
3368
+ if (title.length > 0) {
3369
+ this.sessionTitles.set(sessionId, title);
3370
+ return title;
3371
+ }
3372
+ return null;
3373
+ }
3374
+ this.log({
3375
+ level: "debug",
3376
+ message: `Session title fetch for session ${sessionId.slice(0, 8)} (agent ${this.agentId.slice(0, 8)}) returned HTTP ${res.status} \u2014 omitting title`,
3377
+ conversation_id: conversationId
3378
+ });
3379
+ } catch (err) {
3380
+ this.log({
3381
+ level: "debug",
3382
+ 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)}`,
3383
+ conversation_id: conversationId
3384
+ });
3385
+ }
3386
+ return null;
3387
+ }
3388
+ /**
3389
+ * DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
3390
+ * (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
3391
+ *
3392
+ * The PRIMARY recovery trigger is "preamble-pinned on recovery ⇒ idle" — a
3393
+ * runner restart wipes OpenCode's in-memory `SessionStatus`/`Runner`, so a
3394
+ * completed `finish: "tool-calls"` root reply encountered during re-adoption is
3395
+ * idle by OpenCode's own definition and is re-dispatched. This method exists only
3396
+ * so the WI-3 caller can VETO that re-dispatch in the rare case a descendant is
3397
+ * provably in flight at the exact moment of recovery.
3398
+ *
3399
+ * "Alive" criterion (TIGHTENED): a descendant is alive only when it is PROVABLY,
3400
+ * ACTIVELY generating — its LAST message is an assistant still mid-generation
3401
+ * (`completed == null`, via `isSessionActivelyGenerating`). An
3402
+ * INCOMPLETE-BUT-NOT-GENERATING child — last message a user message, or a
3403
+ * completed `finish: "tool-calls"` step — is NOT alive after a restart (nothing
3404
+ * is generating once the runner is gone), so it does NOT veto. (This is
3405
+ * deliberately NOT `!isTurnComplete`, which also matches those dead-but-non-terminal
3406
+ * shapes and would falsely veto — re-hanging the very turn this path recovers.)
3407
+ *
3408
+ * Return contract (encoded so WI-3 need not re-derive it):
3409
+ * - `true` → a descendant is provably, actively generating (veto re-dispatch).
3410
+ * - `false` → descendants exist but none is actively generating (the restart
3411
+ * case), OR no descendant is found at all.
3412
+ * - `null` → liveness is INDETERMINATE (enumeration via `listSessions` failed).
3413
+ *
3414
+ * ⚠️ `null` (UNKNOWN) MUST NOT be treated as "alive": WI-3 treats `null` the same
3415
+ * as `false` and does NOT veto — a restart guarantees no live runner, so an
3416
+ * indeterminate cross-check almost always means "couldn't reach a child that no
3417
+ * longer exists". The inversion lives in the caller; this method just reports
3418
+ * true/false/null faithfully.
3419
+ *
3420
+ * VERIFY-BEFORE-DEPEND: we depend ONLY on (a) `parentID` from `GET /session/:id`
3421
+ * (already proven by the existing child-session interaction tests, via
3422
+ * `resolveSessionParent`/`sessionBelongsTo`) and (b) the child's own message-list
3423
+ * terminal state. We do NOT depend on any session-level `busy`/`idle` field —
3424
+ * there is none on `GET /session/:id`; OpenCode's busy state is in-memory
3425
+ * `SessionStatus` only.
3426
+ */
3427
+ async isAnyDescendantSessionAlive(rootSessionId) {
3428
+ const sessions = await listSessions(this.port);
3429
+ if (!sessions) {
3430
+ this.log({
3431
+ level: "warn",
3432
+ message: `Re-adopt: could not enumerate sessions to cross-check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating child liveness as indeterminate`
3433
+ });
3434
+ return null;
3435
+ }
3436
+ for (const candidate of sessions) {
3437
+ if (!candidate?.id || candidate.id === rootSessionId) continue;
3438
+ if (!await this.sessionBelongsTo(candidate.id, rootSessionId)) continue;
3439
+ const childMsgs = await getSessionMessages(this.port, candidate.id);
3440
+ if (isSessionActivelyGenerating(childMsgs)) {
3441
+ return true;
3442
+ }
3443
+ }
3444
+ return false;
3445
+ }
3446
+ /**
3447
+ * Cheap decision-telemetry label for a running row's LAST correlated reply
3448
+ * (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
3449
+ * - `b1` — the reply itself is still in flight (`time.completed == null`) —
3450
+ * the aborted-in-flight production bug after a restart.
3451
+ * - `b2` — a COMPLETED reply pinned running only by `finish === "tool-calls"`
3452
+ * (the sub-agent preamble — #253's shape).
3453
+ * - `other` — any other shape (defensive; a running row is normally b1 or b2).
3454
+ * Reads `info.time.completed` / `info.finish` (tolerating the legacy top-level
3455
+ * shape) directly rather than re-importing the module-private `completedOf`/
3456
+ * `finishOf` — this is a display label only, not a correctness predicate.
3457
+ */
3458
+ replyCompletionShape(reply) {
3459
+ if (!reply) return "other";
3460
+ const completed = reply.info?.time?.completed ?? reply.time?.completed;
3461
+ if (completed == null) return "b1";
3462
+ const finish = reply.info?.finish ?? reply.finish;
3463
+ return finish === "tool-calls" ? "b2" : "other";
2083
3464
  }
2084
3465
  /**
2085
3466
  * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
@@ -2163,6 +3544,35 @@ var ChannelDriver = class {
2163
3544
  }
2164
3545
  return await res.json();
2165
3546
  }
3547
+ /**
3548
+ * Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
3549
+ * The pending path (`getPendingConversations`/`getPendingMessages`) only
3550
+ * surfaces `pending` rows, so a message already `processing` when the runner
3551
+ * died is invisible to it — this dedicated endpoint returns exactly those rows
3552
+ * with the fields the re-adopt path needs (`processed_at`,
3553
+ * `opencode_session_id`, routing).
3554
+ *
3555
+ * Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
3556
+ * array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
3557
+ * other non-ok so `drainPending`'s try/finally leaves `draining` false and the
3558
+ * next tick retries.
3559
+ */
3560
+ async getProcessingMessages() {
3561
+ const res = await this.fetchImpl(
3562
+ `${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
3563
+ { headers: { Authorization: this.getAuthHeader() } }
3564
+ );
3565
+ this.assertAuth(res, "fetching processing messages");
3566
+ if (!res.ok) {
3567
+ throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
3568
+ }
3569
+ const data = await res.json();
3570
+ let messages = data.messages ?? [];
3571
+ if (this.conversationFilter) {
3572
+ messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
3573
+ }
3574
+ return messages;
3575
+ }
2166
3576
  /**
2167
3577
  * EXISTING combinedAuth route — now fired by the watcher on queued→running
2168
3578
  * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
@@ -2184,13 +3594,18 @@ var ChannelDriver = class {
2184
3594
  * A single attempt (no internal retry): the watcher's per-tick loop is the
2185
3595
  * retry vehicle for the swap-to-running.
2186
3596
  */
2187
- async markProcessing(conversationId, messageId, sessionId) {
3597
+ async markProcessing(conversationId, messageId, sessionId, opencodeMessageId, title) {
2188
3598
  const res = await this.fetchImpl(
2189
3599
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
2190
3600
  {
2191
3601
  method: "PATCH",
2192
3602
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
2193
- body: JSON.stringify({ status: "processing", opencode_session_id: sessionId })
3603
+ body: JSON.stringify({
3604
+ status: "processing",
3605
+ opencode_session_id: sessionId,
3606
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
3607
+ ...title ? { title } : {}
3608
+ })
2194
3609
  }
2195
3610
  );
2196
3611
  this.assertAuth(res, "marking message as processing");
@@ -2228,13 +3643,18 @@ var ChannelDriver = class {
2228
3643
  * watcher retries next tick within the
2229
3644
  * deadline, Finding 4).
2230
3645
  */
2231
- async markDone(conversationId, messageId, sessionId) {
3646
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId, title) {
2232
3647
  const res = await this.fetchImpl(
2233
3648
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
2234
3649
  {
2235
3650
  method: "PATCH",
2236
3651
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
2237
- body: JSON.stringify({ status: "done", opencode_session_id: sessionId })
3652
+ body: JSON.stringify({
3653
+ status: "done",
3654
+ opencode_session_id: sessionId,
3655
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
3656
+ ...title ? { title } : {}
3657
+ })
2238
3658
  }
2239
3659
  );
2240
3660
  this.assertAuth(res, "marking message as done");
@@ -2244,7 +3664,17 @@ var ChannelDriver = class {
2244
3664
  }
2245
3665
  throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
2246
3666
  }
2247
- async markFailed(conversationId, messageId) {
3667
+ /**
3668
+ * Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
3669
+ * when provided (issue #182): a bare `markFailed(conv, msg)` sends
3670
+ * `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
3671
+ * OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
3672
+ * failure reason reaches the channel.
3673
+ */
3674
+ async markFailed(conversationId, messageId, sessionId, error2) {
3675
+ const body = { status: "failed" };
3676
+ if (sessionId !== void 0) body.opencode_session_id = sessionId;
3677
+ if (error2 !== void 0) body.error = error2;
2248
3678
  await this.callWithRetry(
2249
3679
  "marking message as failed",
2250
3680
  () => this.fetchImpl(
@@ -2252,11 +3682,56 @@ var ChannelDriver = class {
2252
3682
  {
2253
3683
  method: "PATCH",
2254
3684
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
2255
- body: JSON.stringify({ status: "failed" })
3685
+ body: JSON.stringify(body)
2256
3686
  }
2257
3687
  )
2258
3688
  );
2259
3689
  }
3690
+ /**
3691
+ * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
3692
+ * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
3693
+ * — the server records it via `log()` (no DB write, no notification). This is
3694
+ * fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
3695
+ * MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
3696
+ * watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
3697
+ * context (no silent catch, per development-workflow).
3698
+ *
3699
+ * Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
3700
+ * telemetry), but the `paused` liveness-clear uses it to know whether to
3701
+ * RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
3702
+ * stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
3703
+ * leaves liveness").
3704
+ */
3705
+ async postSignal(conversationId, messageId, signal, extra) {
3706
+ try {
3707
+ const res = await this.fetchImpl(
3708
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
3709
+ {
3710
+ method: "POST",
3711
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
3712
+ body: JSON.stringify({ signal, ...extra })
3713
+ }
3714
+ );
3715
+ if (!res.ok) {
3716
+ this.log({
3717
+ level: "warn",
3718
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
3719
+ conversation_id: conversationId,
3720
+ message_id: messageId
3721
+ });
3722
+ return false;
3723
+ }
3724
+ return true;
3725
+ } catch (err) {
3726
+ this.log({
3727
+ level: "warn",
3728
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
3729
+ conversation_id: conversationId,
3730
+ message_id: messageId
3731
+ });
3732
+ return false;
3733
+ }
3734
+ }
2260
3735
  async persistSession(conversationId, sessionId) {
2261
3736
  const res = await this.fetchImpl(
2262
3737
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}`,
@@ -2527,6 +4002,25 @@ async function resolveAgentIdFromKey(authHeader) {
2527
4002
  return { error: `Failed to resolve agent from key: ${message}` };
2528
4003
  }
2529
4004
  }
4005
+ async function notifyAgentDisconnected(agentId, authHeader) {
4006
+ const apiUrl = getApiUrlConfig();
4007
+ try {
4008
+ const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
4009
+ method: "POST",
4010
+ headers: { Authorization: authHeader }
4011
+ });
4012
+ if (!response.ok) {
4013
+ const serverMessage = await readErrorMessage(response);
4014
+ return {
4015
+ ok: false,
4016
+ error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
4017
+ };
4018
+ }
4019
+ return { ok: true };
4020
+ } catch (error2) {
4021
+ return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
4022
+ }
4023
+ }
2530
4024
  async function getAgentInfo(agentId, authHeader) {
2531
4025
  const apiUrl = getApiUrlConfig();
2532
4026
  try {
@@ -2572,23 +4066,55 @@ async function getAgentInfo(agentId, authHeader) {
2572
4066
  // src/commands/run.ts
2573
4067
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
2574
4068
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
2575
- function log2(state, message, isError = false) {
4069
+ var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
4070
+ var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
4071
+ function resolveLogLevel(options) {
4072
+ const accepted = Object.keys(LOG_LEVELS);
4073
+ const validate = (value, source) => {
4074
+ const normalized = value.trim().toLowerCase();
4075
+ if (!accepted.includes(normalized)) {
4076
+ throw new Error(
4077
+ `Invalid log level "${value}"${source}; expected one of ${accepted.join(", ")}`
4078
+ );
4079
+ }
4080
+ return normalized;
4081
+ };
4082
+ if (options.logLevel !== void 0) {
4083
+ return validate(options.logLevel, " (--log-level)");
4084
+ }
4085
+ if (options.verbose) {
4086
+ return "debug";
4087
+ }
4088
+ const env = process.env.EVIDENT_LOG_LEVEL;
4089
+ if (env !== void 0 && env !== "") {
4090
+ return validate(env, " (EVIDENT_LOG_LEVEL)");
4091
+ }
4092
+ return "info";
4093
+ }
4094
+ function meetsThreshold(state, level) {
4095
+ return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
4096
+ }
4097
+ function log2(state, message, level = "info") {
4098
+ if (!meetsThreshold(state, level)) return;
2576
4099
  if (state.json) {
2577
4100
  console.log(
2578
4101
  JSON.stringify({
2579
4102
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
2580
- level: isError ? "error" : "info",
4103
+ level,
2581
4104
  message
2582
4105
  })
2583
4106
  );
2584
4107
  } else if (!state.interactive) {
2585
- const prefix = isError ? chalk6.red("\u2717") : chalk6.green("\u2022");
4108
+ const prefix = level === "error" ? chalk6.red("\u2717") : level === "warn" ? chalk6.yellow("!") : level === "debug" ? chalk6.dim("\xB7") : chalk6.green("\u2022");
2586
4109
  console.log(`${prefix} ${message}`);
2587
4110
  }
2588
4111
  }
2589
4112
  function logActivity(state, entry) {
4113
+ const level = entry.level ?? (entry.type === "error" ? "error" : "info");
4114
+ if (!meetsThreshold(state, level)) return;
2590
4115
  const fullEntry = {
2591
4116
  ...entry,
4117
+ level,
2592
4118
  timestamp: /* @__PURE__ */ new Date()
2593
4119
  };
2594
4120
  state.activityLog.push(fullEntry);
@@ -2597,9 +4123,9 @@ function logActivity(state, entry) {
2597
4123
  }
2598
4124
  if (!state.interactive) {
2599
4125
  if (entry.type === "error") {
2600
- log2(state, entry.error ?? "Unknown error", true);
2601
- } else if (entry.type === "info" && entry.message) {
2602
- log2(state, entry.message);
4126
+ log2(state, entry.error ?? "Unknown error", level);
4127
+ } else if (entry.message) {
4128
+ log2(state, entry.message, level);
2603
4129
  }
2604
4130
  }
2605
4131
  }
@@ -2685,6 +4211,7 @@ async function handleAuthError(state, error2) {
2685
4211
  }
2686
4212
  async function driveChannels(state, driver) {
2687
4213
  let idlePolls = 0;
4214
+ let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
2688
4215
  while (state.running) {
2689
4216
  if (state.connection?.reconnecting && state.connection.reconnectPromise) {
2690
4217
  logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
@@ -2694,7 +4221,9 @@ async function driveChannels(state, driver) {
2694
4221
  try {
2695
4222
  const processed = await driver.drainPending();
2696
4223
  state.messageCount += processed;
2697
- if (processed > 0 || driver.hasInFlightWatchers()) {
4224
+ const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
4225
+ lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
4226
+ if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
2698
4227
  idlePolls = 0;
2699
4228
  if (processed > 0 && state.interactive) displayStatus(state);
2700
4229
  } else if (state.idleTimeout !== null) {
@@ -2723,7 +4252,7 @@ async function driveChannels(state, driver) {
2723
4252
  logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
2724
4253
  if (state.interactive) displayStatus(state);
2725
4254
  }
2726
- await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
4255
+ await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
2727
4256
  if (state.idleTimeout !== null && idlePolls >= 2) {
2728
4257
  const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
2729
4258
  if (idleMs > state.idleTimeout * 1e3) {
@@ -2734,8 +4263,122 @@ async function driveChannels(state, driver) {
2734
4263
  }
2735
4264
  }
2736
4265
  }
2737
- async function cleanup(state) {
4266
+ var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
4267
+ async function runSweep(state, driver, config2) {
4268
+ const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
4269
+ try {
4270
+ const sessions = await listSessions(state.port);
4271
+ if (sessions === null) {
4272
+ logActivity(state, {
4273
+ type: "info",
4274
+ message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
4275
+ });
4276
+ return;
4277
+ }
4278
+ const toDelete = selectSessionsToDelete(
4279
+ sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
4280
+ {
4281
+ maxAgeMs: config2.maxAgeMs,
4282
+ maxCount: config2.maxCount,
4283
+ nowMs: Date.now(),
4284
+ protectedIds: driver.protectedSessionIds()
4285
+ }
4286
+ );
4287
+ const protectedNow = driver.protectedSessionIds();
4288
+ let deleted = 0;
4289
+ let failed = 0;
4290
+ let skippedNewlyActive = 0;
4291
+ for (const id of toDelete) {
4292
+ if (protectedNow.has(id)) {
4293
+ skippedNewlyActive++;
4294
+ logActivity(state, {
4295
+ type: "info",
4296
+ message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
4297
+ });
4298
+ continue;
4299
+ }
4300
+ if (await deleteSession(state.port, id)) deleted++;
4301
+ else failed++;
4302
+ }
4303
+ const failedNote = failed > 0 ? `, failed ${failed}` : "";
4304
+ const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
4305
+ logActivity(state, {
4306
+ type: "info",
4307
+ message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
4308
+ });
4309
+ } catch (error2) {
4310
+ const message = error2 instanceof Error ? error2.message : String(error2);
4311
+ logActivity(state, {
4312
+ type: "error",
4313
+ error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
4314
+ });
4315
+ }
4316
+ }
4317
+ function scheduleSessionCleanup(state, driver, options) {
4318
+ const config2 = resolveSessionCleanupConfig(
4319
+ {
4320
+ maxAge: options.sessionCleanupMaxAge,
4321
+ maxCount: options.sessionCleanupMaxCount,
4322
+ interval: options.sessionCleanupInterval
4323
+ },
4324
+ process.env
4325
+ );
4326
+ for (const warning2 of config2.warnings) {
4327
+ logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
4328
+ }
4329
+ if (!config2.enabled) return;
4330
+ logActivity(state, {
4331
+ type: "info",
4332
+ message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
4333
+ });
4334
+ const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
4335
+ const firstSweep = setTimeout(
4336
+ () => void runSweep(state, driver, config2),
4337
+ SESSION_CLEANUP_FIRST_SWEEP_MS
4338
+ );
4339
+ state.sessionCleanupTimers.push(interval, firstSweep);
4340
+ }
4341
+ async function notifyOffline(state) {
4342
+ if (!state.agentId || !state.authHeader) return;
4343
+ if (!state.connected) {
4344
+ log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
4345
+ return;
4346
+ }
4347
+ const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
4348
+ if (result.ok) {
4349
+ log2(state, "Notified Evident the agent is going offline");
4350
+ } else {
4351
+ logActivity(state, {
4352
+ type: "error",
4353
+ error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
4354
+ });
4355
+ if (state.interactive) displayStatus(state);
4356
+ }
4357
+ }
4358
+ async function cleanup(state, opts = {}) {
2738
4359
  state.running = false;
4360
+ for (const timer of state.sessionCleanupTimers) {
4361
+ clearInterval(timer);
4362
+ clearTimeout(timer);
4363
+ }
4364
+ state.sessionCleanupTimers = [];
4365
+ if (opts.graceful && state.channelDriver) {
4366
+ state.channelDriver.stop();
4367
+ log2(state, "Draining in-flight channel work before shutdown...");
4368
+ if (state.interactive) {
4369
+ logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
4370
+ displayStatus(state);
4371
+ }
4372
+ const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
4373
+ if (!settled) {
4374
+ logActivity(state, {
4375
+ type: "info",
4376
+ message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
4377
+ });
4378
+ if (state.interactive) displayStatus(state);
4379
+ }
4380
+ }
4381
+ await notifyOffline(state);
2739
4382
  if (state.connection) {
2740
4383
  state.connection.close();
2741
4384
  state.connection = null;
@@ -2753,6 +4396,20 @@ async function cleanup(state) {
2753
4396
  }
2754
4397
  async function run(options) {
2755
4398
  const interactive = isInteractive(options.json);
4399
+ let logLevel;
4400
+ try {
4401
+ logLevel = resolveLogLevel(options);
4402
+ } catch (error2) {
4403
+ const message = error2 instanceof Error ? error2.message : String(error2);
4404
+ if (options.json) {
4405
+ console.log(JSON.stringify({ status: "error", error: message }));
4406
+ } else {
4407
+ printError(message);
4408
+ }
4409
+ await shutdownTelemetry();
4410
+ process.exit(1);
4411
+ return;
4412
+ }
2756
4413
  const state = {
2757
4414
  agentId: options.agent || "",
2758
4415
  agentName: null,
@@ -2761,31 +4418,38 @@ async function run(options) {
2761
4418
  idleTimeout: options.idleTimeout ?? null,
2762
4419
  json: options.json ?? false,
2763
4420
  interactive,
4421
+ logLevel,
2764
4422
  connected: false,
2765
4423
  opencodeConnected: false,
2766
4424
  opencodeVersion: null,
2767
4425
  opencodeProcess: null,
2768
4426
  connection: null,
4427
+ channelDriver: null,
2769
4428
  running: true,
4429
+ shuttingDown: false,
2770
4430
  activityLog: [],
2771
4431
  messageCount: 0,
4432
+ lastProxiedActivityAt: null,
4433
+ sessionCleanupTimers: [],
2772
4434
  authHeader: ""
2773
4435
  };
2774
4436
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
2775
4437
  log2(
2776
4438
  state,
2777
- "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.",
2778
- false
4439
+ "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.",
4440
+ "warn"
2779
4441
  );
2780
4442
  }
2781
4443
  const handleSignal = async () => {
4444
+ if (state.shuttingDown) return;
4445
+ state.shuttingDown = true;
2782
4446
  if (state.interactive) {
2783
4447
  logActivity(state, { type: "info", message: "Shutting down..." });
2784
4448
  displayStatus(state);
2785
4449
  } else {
2786
4450
  log2(state, "Shutting down...");
2787
4451
  }
2788
- await cleanup(state);
4452
+ await cleanup(state, { graceful: true });
2789
4453
  await shutdownTelemetry();
2790
4454
  process.exit(0);
2791
4455
  };
@@ -2885,13 +4549,13 @@ async function run(options) {
2885
4549
  state.opencodeProcess = oc.process;
2886
4550
  state.opencodeVersion = oc.version;
2887
4551
  state.opencodeConnected = oc.process !== null || oc.version !== null;
2888
- const version = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
2889
- ocSpinner?.succeed(`OpenCode running on port ${state.port}${version}`);
4552
+ const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
4553
+ ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
2890
4554
  const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
2891
4555
  if (versionWarning) {
2892
- log2(state, versionWarning, false);
4556
+ log2(state, versionWarning, "warn");
2893
4557
  if (state.interactive && !state.json) {
2894
- logActivity(state, { type: "info", message: versionWarning });
4558
+ logActivity(state, { type: "info", level: "warn", message: versionWarning });
2895
4559
  }
2896
4560
  }
2897
4561
  } catch (error2) {
@@ -2905,12 +4569,20 @@ async function run(options) {
2905
4569
  apiUrl: getApiUrlConfig(),
2906
4570
  getAuthHeader: () => state.authHeader,
2907
4571
  conversationFilter: state.conversationFilter,
2908
- log: (entry) => logActivity(state, {
2909
- type: entry.level === "error" ? "error" : "info",
2910
- message: entry.message,
2911
- error: entry.level === "error" ? entry.message : void 0
2912
- })
4572
+ stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
4573
+ log: (entry) => (
4574
+ // Thread the driver's real level straight through so `debug`/`warn`
4575
+ // survive the sink filter (they no longer collapse to info). `type`
4576
+ // stays the coarse error/non-error split the activity log renders with.
4577
+ logActivity(state, {
4578
+ type: entry.level === "error" ? "error" : "info",
4579
+ level: entry.level,
4580
+ message: entry.message,
4581
+ error: entry.level === "error" ? entry.message : void 0
4582
+ })
4583
+ )
2913
4584
  });
4585
+ state.channelDriver = channelDriver;
2914
4586
  const connection = new RunnerConnection({
2915
4587
  agentId: state.agentId,
2916
4588
  getAuthHeader: () => state.authHeader,
@@ -2924,7 +4596,11 @@ async function run(options) {
2924
4596
  type: "info",
2925
4597
  message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
2926
4598
  });
2927
- emitAgentConnected(state.agentId, { port: state.port });
4599
+ emitAgentConnected(state.agentId, {
4600
+ port: state.port,
4601
+ cli_version: getCliVersion(),
4602
+ opencode_version: state.opencodeVersion
4603
+ });
2928
4604
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
2929
4605
  if (state.interactive) displayStatus(state);
2930
4606
  channelDriver.drainPending().then((processed) => {
@@ -2958,9 +4634,14 @@ async function run(options) {
2958
4634
  logActivity(state, { type: "error", error: error2 });
2959
4635
  if (state.interactive) displayStatus(state);
2960
4636
  },
2961
- // Web traffic is proxied transparently; only note opencode is live.
4637
+ // Web traffic is proxied transparently; note opencode is live and stamp
4638
+ // proxied activity so the idle loop treats interactive proxy use as work.
4639
+ // Fires per forwarded response head (incl. every SSE open) and excludes
4640
+ // the internal drain-ping, so an actively-used proxy keeps the timer
4641
+ // fresh while a lone idle SSE with no follow-up requests still ages out.
2962
4642
  onResponse: () => {
2963
4643
  state.opencodeConnected = true;
4644
+ state.lastProxiedActivityAt = Date.now();
2964
4645
  },
2965
4646
  // A channel message was queued and the api-worker pinged us over the
2966
4647
  // tunnel to drain immediately instead of waiting for the next poll tick.
@@ -2998,10 +4679,12 @@ async function run(options) {
2998
4679
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
2999
4680
  throw error2;
3000
4681
  }
4682
+ scheduleSessionCleanup(state, channelDriver, options);
3001
4683
  if (!interactive || state.json) {
3002
4684
  log2(state, "Driving channel messages...");
3003
4685
  }
3004
4686
  await driveChannels(state, channelDriver);
4687
+ if (state.shuttingDown) return;
3005
4688
  await cleanup(state);
3006
4689
  if (state.json) {
3007
4690
  console.log(
@@ -3016,6 +4699,7 @@ async function run(options) {
3016
4699
  await shutdownTelemetry();
3017
4700
  process.exit(0);
3018
4701
  } catch (error2) {
4702
+ if (state.shuttingDown) return;
3019
4703
  await cleanup(state);
3020
4704
  const message = error2 instanceof Error ? error2.message : String(error2);
3021
4705
  if (state.json) {
@@ -3033,8 +4717,9 @@ async function run(options) {
3033
4717
  }
3034
4718
 
3035
4719
  // src/index.ts
4720
+ var { version } = createRequire(import.meta.url)("../package.json");
3036
4721
  var program = new Command();
3037
- program.name("evident").description("Run OpenCode locally and connect it to Evident").version("0.1.0").option(
4722
+ program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
3038
4723
  "--endpoint <url>",
3039
4724
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
3040
4725
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
@@ -3049,15 +4734,34 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
3049
4734
  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);
3050
4735
  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 }));
3051
4736
  program.command("whoami").description("Show the currently logged in user").action(whoami);
3052
- 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(
4737
+ 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(
4738
+ "--log-level <level>",
4739
+ "Log verbosity: debug | info | warn | error (default: info). Env: EVIDENT_LOG_LEVEL"
4740
+ ).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(
4741
+ "--session-cleanup-max-age <duration>",
4742
+ "Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
4743
+ ).option(
4744
+ "--session-cleanup-max-count <n>",
4745
+ "Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
4746
+ ).option(
4747
+ "--session-cleanup-interval <duration>",
4748
+ "How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
4749
+ ).action(
3053
4750
  (options) => {
3054
4751
  run({
3055
4752
  agent: options.agent,
3056
4753
  port: parseInt(options.port, 10),
4754
+ // Raw string — validation/precedence is single-sourced in run.ts's
4755
+ // resolveLogLevel (flag > -v > EVIDENT_LOG_LEVEL > info).
4756
+ logLevel: options.logLevel,
3057
4757
  verbose: options.verbose,
3058
4758
  conversation: options.conversation,
3059
4759
  idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
3060
- json: options.json
4760
+ json: options.json,
4761
+ // Raw strings — the resolver in run.ts single-sources parsing (M1).
4762
+ sessionCleanupMaxAge: options.sessionCleanupMaxAge,
4763
+ sessionCleanupMaxCount: options.sessionCleanupMaxCount,
4764
+ sessionCleanupInterval: options.sessionCleanupInterval
3061
4765
  });
3062
4766
  }
3063
4767
  );