@evident-ai/cli 3.0.0 → 3.0.1-dev.0385cc8

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
@@ -32,10 +33,10 @@ function setTunnelUrl(url) {
32
33
  tunnelOverride = url ? url.replace(/\/+$/, "") : void 0;
33
34
  }
34
35
  function getApiUrl() {
35
- return process.env.EVIDENT_API_URL ?? endpointOverride ?? defaults.apiUrl;
36
+ return endpointOverride ?? process.env.EVIDENT_API_URL ?? defaults.apiUrl;
36
37
  }
37
38
  function getTunnelUrl() {
38
- return process.env.EVIDENT_TUNNEL_URL ?? tunnelOverride ?? defaults.tunnelUrl;
39
+ return tunnelOverride ?? process.env.EVIDENT_TUNNEL_URL ?? defaults.tunnelUrl;
39
40
  }
40
41
  var config = new Conf({
41
42
  projectName: "evident",
@@ -54,19 +55,28 @@ function getApiUrlConfig() {
54
55
  function getTunnelUrlConfig() {
55
56
  return getTunnelUrl();
56
57
  }
58
+ function credentialsKey() {
59
+ return getApiUrl();
60
+ }
57
61
  function getCredentials() {
58
- return {
59
- token: credentials.get("token"),
60
- user: credentials.get("user"),
61
- expiresAt: credentials.get("expiresAt")
62
- };
62
+ const byEndpoint = credentials.get("byEndpoint") ?? {};
63
+ return byEndpoint[credentialsKey()] ?? {};
63
64
  }
64
65
  function setCredentials(creds) {
65
- if (creds.token) credentials.set("token", creds.token);
66
- if (creds.user) credentials.set("user", creds.user);
67
- if (creds.expiresAt) credentials.set("expiresAt", creds.expiresAt);
66
+ const byEndpoint = credentials.get("byEndpoint") ?? {};
67
+ byEndpoint[credentialsKey()] = {
68
+ token: creds.token,
69
+ user: creds.user,
70
+ expiresAt: creds.expiresAt
71
+ };
72
+ credentials.set("byEndpoint", byEndpoint);
68
73
  }
69
74
  function clearCredentials() {
75
+ const byEndpoint = credentials.get("byEndpoint") ?? {};
76
+ delete byEndpoint[credentialsKey()];
77
+ credentials.set("byEndpoint", byEndpoint);
78
+ }
79
+ function clearAllCredentials() {
70
80
  credentials.clear();
71
81
  }
72
82
  function getCliName() {
@@ -176,7 +186,6 @@ var api = {
176
186
 
177
187
  // src/lib/keychain.ts
178
188
  var SERVICE_NAME = "evident-cli";
179
- var ACCOUNT_NAME = "default";
180
189
  async function getKeytar() {
181
190
  try {
182
191
  const keytar = await import("keytar");
@@ -188,10 +197,13 @@ async function getKeytar() {
188
197
  return null;
189
198
  }
190
199
  }
200
+ function keychainAccount() {
201
+ return getApiUrlConfig();
202
+ }
191
203
  async function storeToken(credentials2) {
192
204
  const keytar = await getKeytar();
193
205
  if (keytar) {
194
- await keytar.setPassword(SERVICE_NAME, ACCOUNT_NAME, JSON.stringify(credentials2));
206
+ await keytar.setPassword(SERVICE_NAME, keychainAccount(), JSON.stringify(credentials2));
195
207
  } else {
196
208
  setCredentials({
197
209
  token: credentials2.token,
@@ -203,12 +215,13 @@ async function storeToken(credentials2) {
203
215
  async function getToken() {
204
216
  const keytar = await getKeytar();
205
217
  if (keytar) {
206
- const stored = await keytar.getPassword(SERVICE_NAME, ACCOUNT_NAME);
218
+ const account = keychainAccount();
219
+ const stored = await keytar.getPassword(SERVICE_NAME, account);
207
220
  if (stored) {
208
221
  try {
209
222
  return JSON.parse(stored);
210
223
  } catch {
211
- await keytar.deletePassword(SERVICE_NAME, ACCOUNT_NAME);
224
+ await keytar.deletePassword(SERVICE_NAME, account);
212
225
  return null;
213
226
  }
214
227
  }
@@ -223,12 +236,26 @@ async function getToken() {
223
236
  }
224
237
  return null;
225
238
  }
226
- async function deleteToken() {
239
+ async function deleteToken(options = {}) {
227
240
  const keytar = await getKeytar();
228
241
  if (keytar) {
229
- await keytar.deletePassword(SERVICE_NAME, ACCOUNT_NAME);
242
+ if (options.all) {
243
+ const all = await keytar.findCredentials(SERVICE_NAME).catch(() => []);
244
+ await Promise.all(
245
+ all.map(
246
+ (entry) => keytar.deletePassword(SERVICE_NAME, entry.account).catch(() => {
247
+ })
248
+ )
249
+ );
250
+ } else {
251
+ await keytar.deletePassword(SERVICE_NAME, keychainAccount());
252
+ }
253
+ }
254
+ if (options.all) {
255
+ clearAllCredentials();
256
+ } else {
257
+ clearCredentials();
230
258
  }
231
- clearCredentials();
232
259
  }
233
260
 
234
261
  // src/utils/ui.ts
@@ -258,14 +285,14 @@ function blank() {
258
285
  console.log();
259
286
  }
260
287
  function waitForEnter(prompt = "Press Enter to continue...") {
261
- return new Promise((resolve) => {
288
+ return new Promise((resolve2) => {
262
289
  process.stdout.write(chalk.dim(prompt));
263
290
  const handler = () => {
264
291
  process.stdin.removeListener("data", handler);
265
292
  process.stdin.setRawMode?.(false);
266
293
  process.stdin.pause();
267
294
  console.log();
268
- resolve();
295
+ resolve2();
269
296
  };
270
297
  if (process.stdin.isTTY) {
271
298
  process.stdin.setRawMode?.(true);
@@ -275,7 +302,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
275
302
  });
276
303
  }
277
304
  function sleep(ms) {
278
- return new Promise((resolve) => setTimeout(resolve, ms));
305
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
279
306
  }
280
307
 
281
308
  // src/commands/login.ts
@@ -349,19 +376,19 @@ async function tokenLogin() {
349
376
  console.log("Visit your Evident dashboard to generate a CLI token.");
350
377
  blank();
351
378
  process.stdout.write("Paste token: ");
352
- const token = await new Promise((resolve) => {
379
+ const token = await new Promise((resolve2) => {
353
380
  let data = "";
354
381
  process.stdin.setEncoding("utf8");
355
382
  process.stdin.on("data", (chunk) => {
356
383
  data += chunk;
357
384
  });
358
385
  process.stdin.on("end", () => {
359
- resolve(data.trim());
386
+ resolve2(data.trim());
360
387
  });
361
388
  if (process.stdin.isTTY) {
362
389
  process.stdin.once("data", (chunk) => {
363
390
  process.stdin.pause();
364
- resolve(chunk.toString().trim());
391
+ resolve2(chunk.toString().trim());
365
392
  });
366
393
  process.stdin.resume();
367
394
  }
@@ -396,25 +423,32 @@ async function login(options) {
396
423
  }
397
424
 
398
425
  // src/commands/logout.ts
399
- async function logout() {
426
+ async function logout(options = {}) {
427
+ if (options.all) {
428
+ await deleteToken({ all: true });
429
+ printSuccess("Logged out of all endpoints.");
430
+ return;
431
+ }
400
432
  const credentials2 = await getToken();
401
433
  if (!credentials2) {
402
- printWarning("You are not logged in.");
434
+ printWarning(`You are not logged in to ${getApiUrlConfig()}.`);
403
435
  return;
404
436
  }
405
437
  await deleteToken();
406
- printSuccess("Logged out successfully.");
438
+ printSuccess(`Logged out of ${getApiUrlConfig()}.`);
407
439
  }
408
440
 
409
441
  // src/commands/whoami.ts
410
442
  import chalk3 from "chalk";
411
443
  async function whoami() {
444
+ const apiUrl = getApiUrlConfig();
412
445
  const credentials2 = await getToken();
413
446
  if (!credentials2) {
414
- printError("Not logged in. Run the `login` command to authenticate.");
447
+ printError(`Not logged in to ${apiUrl}. Run the \`login\` command to authenticate.`);
415
448
  process.exit(1);
416
449
  }
417
450
  blank();
451
+ console.log(keyValue("Endpoint", apiUrl));
418
452
  console.log(keyValue("User", chalk3.bold(credentials2.user.email)));
419
453
  console.log(keyValue("User ID", credentials2.user.id));
420
454
  if (credentials2.expiresAt) {
@@ -449,9 +483,36 @@ var TelemetryEventTypes = {
449
483
 
450
484
  // ../../packages/types/src/tunnel/index.ts
451
485
  var MAX_FRAME_BYTES = 256 * 1024;
486
+ var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
487
+
488
+ // ../../packages/types/src/logging/index.ts
489
+ var CORRELATION_ID_HEADER = "x-evident-correlation-id";
490
+ function log(level, event, fields) {
491
+ const method = level === "debug" ? "log" : level;
492
+ try {
493
+ console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
494
+ } catch (err) {
495
+ console.error(
496
+ "[evident] log_serialize_failed",
497
+ event,
498
+ err instanceof Error ? err.message : String(err)
499
+ );
500
+ }
501
+ }
502
+ function stripQuery(url) {
503
+ try {
504
+ return new URL(url).pathname;
505
+ } catch {
506
+ const q = url.indexOf("?");
507
+ return q === -1 ? url : url.slice(0, q);
508
+ }
509
+ }
452
510
 
453
511
  // src/lib/telemetry.ts
454
- 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
+ }
455
516
  var eventBuffer = [];
456
517
  var flushTimeout = null;
457
518
  var isShuttingDown = false;
@@ -645,11 +706,24 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
645
706
  if (health.healthy) {
646
707
  return health;
647
708
  }
648
- await new Promise((resolve) => setTimeout(resolve, 1e3));
709
+ await new Promise((resolve2) => setTimeout(resolve2, 1e3));
649
710
  }
650
711
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
651
712
  }
652
713
 
714
+ // src/lib/opencode/opencode-version-gate.ts
715
+ var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
716
+ function isQueueValidatedVersion(version2) {
717
+ if (!version2) return false;
718
+ return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
719
+ }
720
+ function buildOpenCodeVersionWarning(version2) {
721
+ if (isQueueValidatedVersion(version2)) return null;
722
+ const detected = version2 ? `v${version2}` : "unknown";
723
+ const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
724
+ return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack/WhatsApp) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
725
+ }
726
+
653
727
  // src/lib/opencode/process.ts
654
728
  import { execSync, spawn } from "child_process";
655
729
  var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
@@ -940,19 +1014,174 @@ async function promptOpenCodeInstall(interactive) {
940
1014
  }
941
1015
 
942
1016
  // src/lib/opencode/session.ts
943
- async function createOpenCodeSession(port) {
944
- const response = await fetch(`http://localhost:${port}/session`, {
1017
+ function opencodeBase(port) {
1018
+ return `http://127.0.0.1:${port}`;
1019
+ }
1020
+ async function getOpenCodeDirectory(port) {
1021
+ try {
1022
+ const res = await fetch(`${opencodeBase(port)}/path`);
1023
+ if (!res.ok) return null;
1024
+ const body = await res.json();
1025
+ const dir = typeof body.directory === "string" && body.directory || typeof body.worktree === "string" && body.worktree || typeof body.path?.cwd === "string" && body.path.cwd || typeof body.path?.directory === "string" && body.path.directory || null;
1026
+ return dir && dir.trim() ? dir.trim() : null;
1027
+ } catch {
1028
+ return null;
1029
+ }
1030
+ }
1031
+ function roleOf(m) {
1032
+ if (!m || typeof m !== "object") return void 0;
1033
+ if (typeof m.role === "string") return m.role;
1034
+ const infoRole = m.info?.role;
1035
+ return typeof infoRole === "string" ? infoRole : void 0;
1036
+ }
1037
+ function completedOf(m) {
1038
+ if (!m || typeof m !== "object") return void 0;
1039
+ return m.info?.time?.completed ?? m.time?.completed;
1040
+ }
1041
+ function createdOf(m) {
1042
+ if (!m || typeof m !== "object") return void 0;
1043
+ return m.info?.time?.created ?? m.time?.created;
1044
+ }
1045
+ function idOf(m) {
1046
+ if (!m || typeof m !== "object") return void 0;
1047
+ if (typeof m.id === "string") return m.id;
1048
+ const infoId = m.info?.id;
1049
+ return typeof infoId === "string" ? infoId : void 0;
1050
+ }
1051
+ function parentIdOf(m) {
1052
+ if (!m || typeof m !== "object") return void 0;
1053
+ if (typeof m.parentID === "string") return m.parentID;
1054
+ const infoParent = m.info?.parentID;
1055
+ return typeof infoParent === "string" ? infoParent : void 0;
1056
+ }
1057
+ function finishOf(m) {
1058
+ if (!m || typeof m !== "object") return void 0;
1059
+ if (typeof m.finish === "string") return m.finish;
1060
+ const infoFinish = m.info?.finish;
1061
+ return typeof infoFinish === "string" ? infoFinish : void 0;
1062
+ }
1063
+ function errorOf(m) {
1064
+ if (!m || typeof m !== "object") return void 0;
1065
+ return m.info?.error ?? m.error;
1066
+ }
1067
+ function isAssistantInFlight(m) {
1068
+ if (completedOf(m) == null) return true;
1069
+ return finishOf(m) === "tool-calls";
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
+ }
1159
+ async function createOpenCodeSession(port, directory) {
1160
+ const url = new URL(`${opencodeBase(port)}/session`);
1161
+ if (directory && directory.trim()) {
1162
+ url.searchParams.set("directory", directory.trim());
1163
+ }
1164
+ const response = await fetch(url, {
945
1165
  method: "POST",
946
1166
  headers: { "Content-Type": "application/json" },
947
1167
  body: JSON.stringify({})
948
1168
  });
949
1169
  if (!response.ok) {
950
- throw new Error(`Failed to create session: HTTP ${response.status}`);
1170
+ const text = await response.text().catch(() => "");
1171
+ throw new Error(`Failed to create session: HTTP ${response.status}${text ? `: ${text}` : ""}`);
951
1172
  }
952
1173
  const data = await response.json();
953
1174
  return data.id;
954
1175
  }
955
- async function sendMessageToOpenCode(port, sessionId, content, options, hooks, maxWaitMs = 10 * 60 * 1e3) {
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
+ );
956
1185
  const body = {
957
1186
  parts: [{ type: "text", text: content }]
958
1187
  };
@@ -968,76 +1197,218 @@ async function sendMessageToOpenCode(port, sessionId, content, options, hooks, m
968
1197
  };
969
1198
  }
970
1199
  }
971
- let pollDone = false;
972
- const reportedQuestions = /* @__PURE__ */ new Set();
973
- const reportedPermissions = /* @__PURE__ */ new Set();
974
- const pollInteractive = async () => {
975
- while (!pollDone) {
976
- await new Promise((resolve) => setTimeout(resolve, 1e3));
977
- if (pollDone) break;
978
- if (hooks?.onQuestion) {
979
- try {
980
- const res = await fetch(`http://localhost:${port}/question`);
981
- if (res.ok) {
982
- const questions = await res.json();
983
- for (const q of questions) {
984
- if (q.sessionID === sessionId && !reportedQuestions.has(q.id)) {
985
- reportedQuestions.add(q.id);
986
- await hooks.onQuestion(q);
987
- }
988
- }
989
- }
990
- } catch {
991
- }
992
- }
993
- if (hooks?.onPermission) {
994
- try {
995
- const res = await fetch(`http://localhost:${port}/permission`);
996
- if (res.ok) {
997
- const permissions = await res.json();
998
- for (const p of permissions) {
999
- if (p.sessionID === sessionId && !reportedPermissions.has(p.id)) {
1000
- reportedPermissions.add(p.id);
1001
- await hooks.onPermission(p);
1002
- }
1003
- }
1004
- }
1005
- } catch {
1200
+ const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
1201
+ method: "POST",
1202
+ headers: { "Content-Type": "application/json" },
1203
+ body: JSON.stringify(body)
1204
+ });
1205
+ if (res.status < 200 || res.status >= 300) {
1206
+ const text = await res.text().catch(() => "");
1207
+ throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
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 };
1006
1223
  }
1007
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;
1232
+ }
1233
+ function findAssistantReplyAfter(messages, userMessageId) {
1234
+ if (!messages || messages.length === 0) return null;
1235
+ const byParent = messages.find(
1236
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
1237
+ );
1238
+ if (byParent) return byParent;
1239
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1240
+ if (userIndex === -1) return null;
1241
+ for (let i = userIndex + 1; i < messages.length; i++) {
1242
+ if (roleOf(messages[i]) === "assistant") return messages[i];
1243
+ }
1244
+ return null;
1245
+ }
1246
+ function findLastAssistantReplyFor(messages, userMessageId) {
1247
+ if (!messages || messages.length === 0) return null;
1248
+ let lastCorrelated = null;
1249
+ let lastNonErrored = null;
1250
+ for (let i = messages.length - 1; i >= 0; i--) {
1251
+ const m = messages[i];
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
+ }
1258
+ }
1259
+ if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
1260
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1261
+ if (userIndex === -1) return null;
1262
+ let last = null;
1263
+ let lastOk = null;
1264
+ for (let i = userIndex + 1; i < messages.length; i++) {
1265
+ const role = roleOf(messages[i]);
1266
+ if (role === "user") break;
1267
+ if (role === "assistant") {
1268
+ last = messages[i];
1269
+ if (errorOf(messages[i]) == null) lastOk = messages[i];
1008
1270
  }
1271
+ }
1272
+ return lastOk ?? last;
1273
+ }
1274
+ function messageRunState(messages, userMessageId) {
1275
+ if (!messages || messages.length === 0) return "unknown";
1276
+ const hasUser = messages.some((m) => idOf(m) === userMessageId);
1277
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
1278
+ if (!hasUser) {
1279
+ if (!reply) return "unknown";
1280
+ }
1281
+ if (!reply) return "queued";
1282
+ if (isAssistantInFlight(reply)) return "running";
1283
+ return errorOf(reply) != null ? "failed" : "done";
1284
+ }
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;
1009
1338
  };
1010
- const sendMessage = async () => {
1011
- const controller = new AbortController();
1012
- const timer = setTimeout(() => controller.abort(), maxWaitMs);
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) {
1013
1383
  try {
1014
- const res = await fetch(`http://localhost:${port}/session/${sessionId}/message`, {
1015
- method: "POST",
1016
- headers: { "Content-Type": "application/json" },
1017
- body: JSON.stringify(body),
1018
- signal: controller.signal
1019
- });
1020
- if (!res.ok) {
1021
- const text = await res.text().catch(() => "");
1022
- throw new Error(`OpenCode message failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
1023
- }
1024
- const sessionRes = await fetch(`http://localhost:${port}/session/${sessionId}`).catch(
1025
- () => null
1384
+ maxAgeMs = parseDurationMs(maxAgeRaw);
1385
+ } catch (err) {
1386
+ warnings.push(
1387
+ `Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
1026
1388
  );
1027
- const session = sessionRes?.ok ? await sessionRes.json() : null;
1028
- return { title: session?.title };
1389
+ }
1390
+ }
1391
+ let maxCount;
1392
+ if (maxCountRaw !== void 0) {
1393
+ try {
1394
+ maxCount = parseMaxCount(maxCountRaw);
1029
1395
  } catch (err) {
1030
- if (err instanceof Error && err.name === "AbortError") {
1031
- throw new Error("Message processing timed out");
1032
- }
1033
- throw err;
1034
- } finally {
1035
- clearTimeout(timer);
1036
- pollDone = true;
1396
+ warnings.push(
1397
+ `Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
1398
+ );
1037
1399
  }
1038
- };
1039
- const [result] = await Promise.all([sendMessage(), pollInteractive()]);
1040
- return result;
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 };
1041
1412
  }
1042
1413
 
1043
1414
  // src/lib/tunnel/connection.ts
@@ -1108,18 +1479,34 @@ var StreamForwarder = class {
1108
1479
  }
1109
1480
  async handleOpen(frame) {
1110
1481
  const { sid, method, path, headers, has_body } = frame;
1482
+ const correlationId = headers?.[CORRELATION_ID_HEADER];
1483
+ const startedAt = Date.now();
1484
+ if (path === TUNNEL_DRAIN_PING_PATH) {
1485
+ this.callbacks.onDrainPing?.();
1486
+ this.send({ type: "head", sid, status: 204, headers: {} });
1487
+ this.send({ type: "res_end", sid });
1488
+ return;
1489
+ }
1490
+ if (process.env.DEBUG) {
1491
+ log("debug", "agent_request", {
1492
+ correlation_id: correlationId,
1493
+ sid,
1494
+ method,
1495
+ path: stripQuery(path)
1496
+ });
1497
+ }
1111
1498
  const ac = new AbortController();
1112
1499
  let bodyPromise;
1113
1500
  let pushBody;
1114
1501
  let endBody;
1115
1502
  if (has_body) {
1116
1503
  const chunks = [];
1117
- bodyPromise = new Promise((resolve) => {
1504
+ bodyPromise = new Promise((resolve2) => {
1118
1505
  pushBody = (buf) => {
1119
1506
  chunks.push(buf);
1120
1507
  };
1121
1508
  endBody = () => {
1122
- resolve(Buffer.concat(chunks));
1509
+ resolve2(Buffer.concat(chunks));
1123
1510
  };
1124
1511
  });
1125
1512
  }
@@ -1154,6 +1541,14 @@ var StreamForwarder = class {
1154
1541
  if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
1155
1542
  });
1156
1543
  this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
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
+ }
1157
1552
  this.callbacks.onHead?.(sid, upstream.status);
1158
1553
  try {
1159
1554
  if (upstream.body) {
@@ -1224,11 +1619,12 @@ function connectTunnel(options) {
1224
1619
  onError,
1225
1620
  onRequest,
1226
1621
  onResponse,
1227
- onInfo
1622
+ onInfo,
1623
+ onDrainPing
1228
1624
  } = options;
1229
1625
  const tunnelUrl = getTunnelUrlConfig();
1230
1626
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
1231
- return new Promise((resolve, reject) => {
1627
+ return new Promise((resolve2, reject) => {
1232
1628
  const ws = new WebSocket2(url, {
1233
1629
  headers: {
1234
1630
  Authorization: authHeader
@@ -1237,6 +1633,7 @@ function connectTunnel(options) {
1237
1633
  const streamStartTimes = /* @__PURE__ */ new Map();
1238
1634
  const forwarder = new StreamForwarder(ws, port, {
1239
1635
  onOpen: (sid, method, path) => {
1636
+ if (path === TUNNEL_DRAIN_PING_PATH) return;
1240
1637
  streamStartTimes.set(sid, Date.now());
1241
1638
  onRequest?.(method, path, sid);
1242
1639
  },
@@ -1244,7 +1641,8 @@ function connectTunnel(options) {
1244
1641
  const startedAt = streamStartTimes.get(sid);
1245
1642
  streamStartTimes.delete(sid);
1246
1643
  onResponse?.(status, startedAt ? Date.now() - startedAt : 0, sid);
1247
- }
1644
+ },
1645
+ onDrainPing: () => onDrainPing?.()
1248
1646
  });
1249
1647
  const connectionTimeout = setTimeout(() => {
1250
1648
  ws.close();
@@ -1291,7 +1689,7 @@ function connectTunnel(options) {
1291
1689
  clearTimeout(connectionTimeout);
1292
1690
  const connectedAgentId = message.agent_id ?? agentId;
1293
1691
  onConnected?.(connectedAgentId);
1294
- resolve({
1692
+ resolve2({
1295
1693
  ws,
1296
1694
  close: () => ws.close(1e3, "CLI shutdown")
1297
1695
  });
@@ -1386,6 +1784,7 @@ var RunnerConnection = class {
1386
1784
  },
1387
1785
  onError: (error2) => events.onError?.(error2),
1388
1786
  onResponse: () => events.onResponse?.(),
1787
+ onDrainPing: () => events.onDrainPing?.(),
1389
1788
  onInfo: (message) => events.onInfo?.(message)
1390
1789
  });
1391
1790
  return;
@@ -1406,17 +1805,37 @@ var RunnerConnection = class {
1406
1805
  };
1407
1806
 
1408
1807
  // src/lib/channels/driver.ts
1808
+ function messageIdOf(m) {
1809
+ if (!m || typeof m !== "object") return void 0;
1810
+ if (typeof m.id === "string") return m.id;
1811
+ const infoId = m.info?.id;
1812
+ return typeof infoId === "string" ? infoId : void 0;
1813
+ }
1409
1814
  var DEFAULT_RETRY_POLICY = {
1410
1815
  maxAttempts: 6,
1411
1816
  baseDelayMs: 500,
1412
1817
  maxDelayMs: 3e4
1413
1818
  };
1819
+ var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1820
+ var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1821
+ var DEFAULT_STUCK_QUEUED_MS = 6e4;
1822
+ var HEARTBEAT_MS = 6e4;
1823
+ var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
1824
+ var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
1414
1825
  var ChannelAuthError = class extends Error {
1415
1826
  constructor(message) {
1416
1827
  super(message);
1417
1828
  this.name = "ChannelAuthError";
1418
1829
  }
1419
1830
  };
1831
+ var ChannelTerminalError = class extends Error {
1832
+ status;
1833
+ constructor(message, status) {
1834
+ super(message);
1835
+ this.name = "ChannelTerminalError";
1836
+ this.status = status;
1837
+ }
1838
+ };
1420
1839
  function backoffDelay(attempt, policy) {
1421
1840
  const exp = policy.baseDelayMs * Math.pow(2, attempt);
1422
1841
  const capped = Math.min(policy.maxDelayMs, exp);
@@ -1435,10 +1854,127 @@ var ChannelDriver = class {
1435
1854
  log;
1436
1855
  fetchImpl;
1437
1856
  sleep;
1857
+ pausedPollIntervalMs;
1858
+ pausedMaxWaitMs;
1859
+ stuckQueuedMs;
1860
+ now;
1438
1861
  /** Cache of conversationId → opencode sessionId. */
1439
1862
  sessions = /* @__PURE__ */ new Map();
1863
+ /**
1864
+ * Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
1865
+ * longer idempotent (no caller-supplied `messageID`), and its read-back picks
1866
+ * "the one new user row" — which is only unambiguous if no OTHER dispatch into
1867
+ * the SAME session interleaves its snapshot→POST→read-back. This map chains each
1868
+ * session's dispatches so they run serially; distinct sessions stay concurrent.
1869
+ */
1870
+ sessionDispatchLocks = /* @__PURE__ */ new Map();
1871
+ /**
1872
+ * Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
1873
+ * session: one polling loop services all of that session's in-flight messages.
1874
+ * A session entry exists while it has any in-flight (dispatched-but-not-done)
1875
+ * message; it is removed once its in-flight set empties.
1876
+ */
1877
+ watchers = /* @__PURE__ */ new Map();
1878
+ /**
1879
+ * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
1880
+ * dispatched and are still in-flight. A message in this set is never
1881
+ * re-`prompt_async`-ed by a subsequent poll tick while it is queued/running.
1882
+ * Backed by a stable minted opencode `messageID` whose duplicate re-enqueue is
1883
+ * idempotent on opencode (PoC fact 9) — so even if this set is lost on restart,
1884
+ * a steady-state-poll re-dispatch will not double-run the message.
1885
+ */
1886
+ dispatched = /* @__PURE__ */ new Set();
1887
+ /**
1888
+ * Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
1889
+ * Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
1890
+ * so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
1891
+ * it is re-adopted and removed when its watcher settles or it is observed off
1892
+ * the processing list.
1893
+ */
1894
+ readopted = /* @__PURE__ */ new Set();
1895
+ /**
1896
+ * "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
1897
+ * re-adopted running/orphan row's watcher hit its `processed_at`-anchored
1898
+ * deadline (or an orphan whose window already elapsed): the still-`processing`
1899
+ * server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
1900
+ * drain until the 15-min cron resets it — spamming new turns.
1901
+ *
1902
+ * CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
1903
+ * does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
1904
+ * in opencode must still be delivered via `markDone` on the next drain — so
1905
+ * `readoptOne` computes `state` FIRST and this set is checked only on the
1906
+ * non-done path. It is cleared once the row leaves the processing list (cron
1907
+ * reset → it drains normally as `pending`), so it can never leak.
1908
+ */
1909
+ dontRedispatch = /* @__PURE__ */ new Set();
1910
+ /**
1911
+ * "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
1912
+ * re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
1913
+ * never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
1914
+ * that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
1915
+ * markDone failure must NOT land here (it must still retry next drain). Separate
1916
+ * from `dontRedispatch` because the two concerns are independent: a row can need
1917
+ * "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
1918
+ * the row leaves the processing list, exactly like `dontRedispatch`.
1919
+ */
1920
+ doneUndeliverable = /* @__PURE__ */ new Set();
1921
+ /**
1922
+ * "Already emitted `readopt_poll_unresolved` for this row" (#229). The b1 /
1923
+ * unreadable-status re-evaluate leaf leaves the row UN-tracked so it is re-read
1924
+ * every ~2s drain until the status map becomes readable — but the server-visible
1925
+ * signal is an OUTCOME, so it must fire at most ONCE per row, not once per drain
1926
+ * (Bugbot "Re-adopt signals flood every drain"). Cleared when the row leaves the
1927
+ * processing list, exactly like `dontRedispatch`/`doneUndeliverable`.
1928
+ */
1929
+ readoptPollUnresolvedSignalled = /* @__PURE__ */ new Set();
1930
+ /**
1931
+ * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
1932
+ * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
1933
+ * is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
1934
+ * persist hasn't landed before tick N+1 re-reads the still-null
1935
+ * `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
1936
+ * A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
1937
+ * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
1938
+ * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
1939
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
1940
+ * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
1941
+ * so the NEXT tick may retry exactly once more).
1942
+ */
1943
+ awaitingReadopt = /* @__PURE__ */ new Set();
1944
+ /**
1945
+ * Cache of the opencode root directory (from `GET /path`). Resolved lazily on
1946
+ * first session creation so drain-created sessions are rooted at the project
1947
+ * directory and thus visible in `opencode web`'s session list. `undefined` =
1948
+ * not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
1949
+ */
1950
+ opencodeDirectory = void 0;
1951
+ /**
1952
+ * Cache of opencode `sessionId → parentID` (its parent session, or `null` when
1953
+ * the session is a root with no parent). Sub-agents spawned via the `task` tool
1954
+ * run in CHILD sessions whose `parentID` chains up to the Evident-created
1955
+ * (watched) session; we resolve this once per session so a child-session
1956
+ * question/permission can be attributed to the watched session's subtree
1957
+ * (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
1958
+ * entry = not yet resolved; `null` = resolved root (stop walking).
1959
+ */
1960
+ sessionParents = /* @__PURE__ */ new Map();
1440
1961
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
1441
1962
  draining = false;
1963
+ /**
1964
+ * The currently-executing `drainPending()` promise, or null when idle. Lets a
1965
+ * graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
1966
+ * is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
1967
+ * drain that entered before `stop()` still registers its watcher).
1968
+ */
1969
+ activeDrain = null;
1970
+ /**
1971
+ * Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
1972
+ * dispatches NEW work (it returns 0 immediately) — but the per-session watcher
1973
+ * loops already running keep going so in-flight turns can finish and deliver
1974
+ * their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
1975
+ * and stops opencode.
1976
+ */
1977
+ stopped = false;
1442
1978
  constructor(config2) {
1443
1979
  this.agentId = config2.agentId;
1444
1980
  this.port = config2.port;
@@ -1450,6 +1986,10 @@ var ChannelDriver = class {
1450
1986
  });
1451
1987
  this.fetchImpl = config2.fetchImpl ?? fetch;
1452
1988
  this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
1989
+ this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
1990
+ this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
1991
+ this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
1992
+ this.now = config2.now ?? (() => Date.now());
1453
1993
  }
1454
1994
  /** The IPv4-loopback base URL for the local `opencode serve`. */
1455
1995
  get opencodeBase() {
@@ -1459,112 +1999,1452 @@ var ChannelDriver = class {
1459
1999
  // Public API
1460
2000
  // -------------------------------------------------------------------------
1461
2001
  /**
1462
- * Drain all pending channel conversations once: poll → processcallback.
2002
+ * Drain all pending channel conversations once: poll → dispatchregister.
1463
2003
  * Called on tunnel `connected` (WI-CHAN-4) and on each poll tick by `run.ts`.
1464
2004
  * Re-entrant calls while a drain is in flight are skipped (return 0).
1465
2005
  *
1466
- * @returns the number of messages processed.
2006
+ * @returns the number of messages NEWLY dispatched to opencode's native queue.
1467
2007
  */
1468
2008
  async drainPending() {
2009
+ if (this.stopped) return 0;
1469
2010
  if (this.draining) return 0;
1470
2011
  this.draining = true;
1471
- let processed = 0;
2012
+ const run2 = this.runDrain();
2013
+ this.activeDrain = run2.then(
2014
+ () => {
2015
+ this.activeDrain = null;
2016
+ },
2017
+ () => {
2018
+ this.activeDrain = null;
2019
+ }
2020
+ );
2021
+ return run2;
2022
+ }
2023
+ async runDrain() {
2024
+ let dispatched = 0;
1472
2025
  try {
1473
2026
  const conversations = await this.getPendingConversations();
2027
+ if (conversations.length > 0) {
2028
+ const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
2029
+ this.log({
2030
+ level: "info",
2031
+ message: `Found ${total} pending message(s) across ${conversations.length} conversation(s) \u2014 draining`
2032
+ });
2033
+ }
1474
2034
  for (const conv of conversations) {
1475
- processed += await this.processConversation(conv);
2035
+ if (this.stopped) break;
2036
+ dispatched += await this.processConversation(conv);
1476
2037
  }
2038
+ await this.readoptProcessing();
1477
2039
  } finally {
1478
2040
  this.draining = false;
1479
2041
  }
1480
- return processed;
2042
+ return dispatched;
2043
+ }
2044
+ /**
2045
+ * True while any per-session watcher has a non-empty in-flight dispatched set
2046
+ * (Task 3.7). `run.ts` treats this as NON-idle so `--idle-timeout` cannot exit
2047
+ * the process while a dispatched message is still queued/running — which would
2048
+ * kill the turn and orphan its reply.
2049
+ */
2050
+ hasInFlightWatchers() {
2051
+ for (const watcher of this.watchers.values()) {
2052
+ if (watcher.inFlight.size > 0) return true;
2053
+ }
2054
+ return false;
2055
+ }
2056
+ /**
2057
+ * OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
2058
+ * exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
2059
+ * `watchers` entry whose `inFlight` set is non-empty — the same predicate
2060
+ * `hasInFlightWatchers()` uses, lifted to return the ids.
2061
+ *
2062
+ * Deliberately does NOT include `this.sessions` (the permanent, never-pruned
2063
+ * conversation→session cache). Protecting every bound-but-idle session there
2064
+ * would shield nearly every session and defeat cleanup — AND it is unnecessary:
2065
+ * `ensureSession` is self-healing (it recreates a session whose id no longer
2066
+ * exists), so deleting an idle bound session is harmless — the conversation's
2067
+ * next turn transparently rebinds a fresh one. The only thing worth protecting
2068
+ * is a session with a turn ACTIVELY in flight right now: tearing that down
2069
+ * mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
2070
+ */
2071
+ protectedSessionIds() {
2072
+ const ids = /* @__PURE__ */ new Set();
2073
+ for (const [sessionId, watcher] of this.watchers) {
2074
+ if (watcher.inFlight.size > 0) ids.add(sessionId);
2075
+ }
2076
+ return ids;
2077
+ }
2078
+ /**
2079
+ * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
2080
+ * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
2081
+ * — but the watcher loops already tracking in-flight turns keep running, so a
2082
+ * turn that has finished (or is about to) still fires `markDone` and delivers
2083
+ * its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
2084
+ */
2085
+ stop() {
2086
+ this.stopped = true;
2087
+ }
2088
+ /**
2089
+ * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
2090
+ * graceful shutdown, so a turn whose reply is ready — or completes within the
2091
+ * window — is delivered before the process exits, instead of being cut off and
2092
+ * left for the ADR-0046 restart-recovery path.
2093
+ *
2094
+ * Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
2095
+ * far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
2096
+ * window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
2097
+ * set empties OR the timeout elapses. Anything still in flight at the timeout is
2098
+ * safe to abandon — it stays `processing` server-side and is re-adopted on the
2099
+ * next runner start (ADR-0046).
2100
+ *
2101
+ * @returns true if all in-flight work settled within the window; false if the
2102
+ * timeout elapsed with work still in flight.
2103
+ */
2104
+ async waitForInFlight(timeoutMs) {
2105
+ const deadline = this.now() + timeoutMs;
2106
+ const step = Math.min(this.pausedPollIntervalMs, 250);
2107
+ if (this.activeDrain) {
2108
+ let drainSettled = false;
2109
+ void this.activeDrain.then(() => {
2110
+ drainSettled = true;
2111
+ });
2112
+ while (!drainSettled) {
2113
+ if (this.now() >= deadline) return false;
2114
+ await this.sleep(step);
2115
+ }
2116
+ }
2117
+ while (this.hasInFlightWatchers()) {
2118
+ if (this.now() >= deadline) return false;
2119
+ await this.sleep(step);
2120
+ }
2121
+ return true;
2122
+ }
2123
+ /**
2124
+ * Await all outstanding per-session watchers (WI-3).
2125
+ *
2126
+ * In production the watcher loops are deliberately started-not-awaited so the
2127
+ * drain loop never blocks on them and process exit is not held up (the cron
2128
+ * recovers any abandoned ones). This helper exists primarily for deterministic
2129
+ * tests that need to observe a watcher's effect (the `processing`/`done` PATCH
2130
+ * or its giving up) after a non-blocking `drainPending`. Watcher loops never
2131
+ * reject, so this resolves.
2132
+ */
2133
+ async flushPausedWatchers() {
2134
+ while (true) {
2135
+ const loops = [...this.watchers.values()].map((w) => w.loop).filter((l) => l != null);
2136
+ if (loops.length === 0) return;
2137
+ await Promise.all(loops);
2138
+ const stillLive = [...this.watchers.values()].some((w) => w.loop != null);
2139
+ if (!stillLive) return;
2140
+ }
1481
2141
  }
1482
2142
  // -------------------------------------------------------------------------
1483
- // Conversation processing
2143
+ // Conversation processing (WI-3 — async dispatch)
1484
2144
  // -------------------------------------------------------------------------
2145
+ /**
2146
+ * Dispatch each pending message for a conversation to opencode's native queue
2147
+ * via `prompt_async` (Task 3.2) and register it with the conversation's
2148
+ * per-session watcher. Does NOT block on the turn and does NOT call
2149
+ * `markProcessing` here — that fires from the watcher on running-start.
2150
+ *
2151
+ * @returns the count of messages NEWLY dispatched (not already in-flight).
2152
+ */
1485
2153
  async processConversation(conv) {
1486
2154
  const sessionId = await this.ensureSession(conv);
1487
2155
  const messages = await this.getPendingMessages(conv.id);
1488
- let processed = 0;
2156
+ let dispatched = 0;
2157
+ let skippedAlreadyDispatched = 0;
1489
2158
  for (const message of messages) {
1490
- const claimed = await this.markProcessing(conv.id, message.id);
1491
- if (!claimed) {
1492
- this.log({
1493
- level: "info",
1494
- message: `Message ${message.id.slice(0, 8)} already claimed \u2014 skipping`,
1495
- conversation_id: conv.id,
1496
- message_id: message.id
1497
- });
2159
+ if (this.stopped) break;
2160
+ if (this.dispatched.has(message.id)) {
2161
+ skippedAlreadyDispatched += 1;
1498
2162
  continue;
1499
2163
  }
2164
+ const options = {
2165
+ agent: message.opencode_agent ?? void 0,
2166
+ model: message.opencode_model ?? void 0
2167
+ };
2168
+ let opencodeMessageId;
1500
2169
  try {
1501
- await sendMessageToOpenCode(
1502
- this.port,
1503
- sessionId,
1504
- message.content,
1505
- {
1506
- agent: message.opencode_agent ?? void 0,
1507
- model: message.opencode_model ?? void 0
1508
- },
1509
- {
1510
- onQuestion: (question) => this.reportInteraction(conv.id, "question", question),
1511
- onPermission: (permission) => this.reportInteraction(conv.id, "permission", permission)
1512
- }
1513
- );
1514
- await this.confirmCompletion(sessionId);
1515
- await this.markDone(conv.id, message.id, sessionId);
1516
- processed += 1;
1517
2170
  this.log({
1518
2171
  level: "info",
1519
- message: `Message ${message.id.slice(0, 8)} processed`,
2172
+ message: `Dispatching message ${message.id.slice(0, 8)} to OpenCode native queue (session ${sessionId.slice(0, 8)})`,
1520
2173
  conversation_id: conv.id,
1521
2174
  message_id: message.id
1522
2175
  });
2176
+ opencodeMessageId = await this.dispatchLocked(
2177
+ sessionId,
2178
+ () => sendPromptAsync(this.port, sessionId, message.content, options)
2179
+ );
1523
2180
  } catch (err) {
1524
2181
  if (err instanceof ChannelAuthError) throw err;
2182
+ this.dispatched.delete(message.id);
2183
+ if (await sessionExists(this.port, sessionId) === false) {
2184
+ this.sessions.delete(conv.id);
2185
+ this.log({
2186
+ level: "info",
2187
+ 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.`,
2188
+ conversation_id: conv.id,
2189
+ message_id: message.id
2190
+ });
2191
+ break;
2192
+ }
1525
2193
  await this.markFailed(conv.id, message.id).catch(() => {
1526
2194
  });
1527
2195
  this.log({
1528
2196
  level: "error",
1529
- message: `Message ${message.id.slice(0, 8)} failed: ${err instanceof Error ? err.message : String(err)}`,
2197
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${err instanceof Error ? err.message : String(err)}`,
1530
2198
  conversation_id: conv.id,
1531
2199
  message_id: message.id
1532
2200
  });
2201
+ continue;
1533
2202
  }
1534
- }
1535
- return processed;
2203
+ if (opencodeMessageId === null) {
2204
+ this.log({
2205
+ level: "error",
2206
+ 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`,
2207
+ conversation_id: conv.id,
2208
+ message_id: message.id
2209
+ });
2210
+ continue;
2211
+ }
2212
+ this.dispatched.add(message.id);
2213
+ this.registerInFlight(conv, sessionId, message, opencodeMessageId);
2214
+ dispatched += 1;
2215
+ void this.postSignal(conv.id, message.id, "dispatched");
2216
+ }
2217
+ if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
2218
+ this.log({
2219
+ level: "error",
2220
+ 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).`,
2221
+ conversation_id: conv.id
2222
+ });
2223
+ }
2224
+ this.ensureWatcherRunning(sessionId);
2225
+ return dispatched;
2226
+ }
2227
+ async ensureSession(conv) {
2228
+ const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
2229
+ if (bound) {
2230
+ const exists = await sessionExists(this.port, bound);
2231
+ if (exists === false) {
2232
+ this.log({
2233
+ level: "info",
2234
+ 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.`,
2235
+ conversation_id: conv.id
2236
+ });
2237
+ this.sessions.delete(conv.id);
2238
+ return this.createAndBindSession(conv.id);
2239
+ }
2240
+ this.sessions.set(conv.id, bound);
2241
+ return bound;
2242
+ }
2243
+ return this.createAndBindSession(conv.id);
2244
+ }
2245
+ /**
2246
+ * Create a fresh OpenCode session for a conversation, cache the binding, and
2247
+ * best-effort persist it server-side. Shared by the first-ever bind and the
2248
+ * self-heal recreate path in `ensureSession`.
2249
+ */
2250
+ async createAndBindSession(conversationId) {
2251
+ const directory = await this.resolveOpenCodeDirectory();
2252
+ const sessionId = await createOpenCodeSession(this.port, directory);
2253
+ this.sessions.set(conversationId, sessionId);
2254
+ await this.persistSession(conversationId, sessionId).catch(() => {
2255
+ });
2256
+ return sessionId;
2257
+ }
2258
+ /**
2259
+ * Lazily resolve (and cache) opencode's root directory via `GET /path`.
2260
+ * Resolved once per driver: `undefined` until first lookup, then the directory
2261
+ * string or `null` if unavailable (we don't keep retrying a missing `/path`).
2262
+ */
2263
+ async resolveOpenCodeDirectory() {
2264
+ if (this.opencodeDirectory !== void 0) return this.opencodeDirectory;
2265
+ this.opencodeDirectory = await getOpenCodeDirectory(this.port);
2266
+ if (!this.opencodeDirectory) {
2267
+ this.log({
2268
+ level: "info",
2269
+ message: "Could not determine opencode directory (GET /path) \u2014 new sessions may not appear in opencode web"
2270
+ });
2271
+ }
2272
+ return this.opencodeDirectory;
2273
+ }
2274
+ // -------------------------------------------------------------------------
2275
+ // Per-session watcher (WI-3)
2276
+ // -------------------------------------------------------------------------
2277
+ /**
2278
+ * Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
2279
+ * opencode session (Task 2.1a), so two dispatches into the SAME session can
2280
+ * never interleave and mis-correlate their read-backs. Distinct sessions run
2281
+ * concurrently. The chained tail intentionally ignores the prior result/error
2282
+ * (each dispatch reports its own outcome to its caller).
2283
+ */
2284
+ dispatchLocked(sessionId, fn) {
2285
+ const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
2286
+ const run2 = prior.then(fn, fn);
2287
+ this.sessionDispatchLocks.set(
2288
+ sessionId,
2289
+ run2.then(
2290
+ () => void 0,
2291
+ () => void 0
2292
+ )
2293
+ );
2294
+ return run2;
2295
+ }
2296
+ /** Register a freshly-dispatched message with its session's watcher state. */
2297
+ registerInFlight(conv, sessionId, message, opencodeMessageId) {
2298
+ let watcher = this.watchers.get(sessionId);
2299
+ if (!watcher) {
2300
+ watcher = {
2301
+ conv,
2302
+ inFlight: /* @__PURE__ */ new Map(),
2303
+ loop: null,
2304
+ reportedQuestions: /* @__PURE__ */ new Set(),
2305
+ reportedPermissions: /* @__PURE__ */ new Set(),
2306
+ lastGoodPollAt: this.now(),
2307
+ hadUsablePoll: false
2308
+ };
2309
+ this.watchers.set(sessionId, watcher);
2310
+ }
2311
+ const now = this.now();
2312
+ watcher.inFlight.set(message.id, {
2313
+ evidentMessageId: message.id,
2314
+ opencodeMessageId,
2315
+ message,
2316
+ dispatchedAt: now,
2317
+ processingAnchorMs: now,
2318
+ deadline: now + this.pausedMaxWaitMs,
2319
+ started: false,
2320
+ done: false,
2321
+ stuckReported: false,
2322
+ lastAliveAt: 0,
2323
+ aliveInFlight: false,
2324
+ awaitingHumanLatched: false,
2325
+ pausedOnQuestion: false,
2326
+ pausedOnPermission: false,
2327
+ pausedClearConfirmed: false,
2328
+ pausedInFlight: false,
2329
+ deliveryDeadlineAnchored: false
2330
+ });
2331
+ }
2332
+ /**
2333
+ * Register a RE-ADOPTED `processing` message with its session watcher
2334
+ * (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
2335
+ * `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
2336
+ * `now`, so the paused/queued/unreachable cases settle on the same wall-clock a
2337
+ * fresh dispatch would (10 min after `processed_at`, not 10 min from now).
2338
+ *
2339
+ * This re-attaches into the SAME watcher, so the ADR-0047 progressing-vs-paused
2340
+ * give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
2341
+ * opencode reports ACTIVELY `running` is watched to completion (its liveness
2342
+ * heartbeat keeps the cron off its row), while a re-adopted turn that is paused
2343
+ * awaiting a human — or queued/unreachable — is still bounded by `deadline` and
2344
+ * handed to the cron. The old "the `deadline` must settle before the ~15-min
2345
+ * cron or they double-drive" reasoning is superseded: liveness now settles the
2346
+ * actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
2347
+ * (only the appear-guard uses it).
2348
+ *
2349
+ * `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
2350
+ * `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
2351
+ * fresh-run path these differ (a fresh opencode id under the same server row).
2352
+ *
2353
+ * `started` is set true so the watcher does NOT re-`markProcessing` a row the
2354
+ * server already flipped to `processing`; the running/done transitions still
2355
+ * fire from the watcher's normal branches.
2356
+ */
2357
+ registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
2358
+ let watcher = this.watchers.get(sessionId);
2359
+ if (!watcher) {
2360
+ watcher = {
2361
+ conv,
2362
+ inFlight: /* @__PURE__ */ new Map(),
2363
+ loop: null,
2364
+ reportedQuestions: /* @__PURE__ */ new Set(),
2365
+ reportedPermissions: /* @__PURE__ */ new Set(),
2366
+ lastGoodPollAt: this.now(),
2367
+ hadUsablePoll: false
2368
+ };
2369
+ this.watchers.set(sessionId, watcher);
2370
+ }
2371
+ watcher.inFlight.set(message.id, {
2372
+ evidentMessageId: message.id,
2373
+ opencodeMessageId,
2374
+ message,
2375
+ dispatchedAt: this.now(),
2376
+ // Anchor the absolute-age ceiling to the SERVER-SIDE `processed_at` (the same
2377
+ // value seeding `deadline`), NOT `dispatchedAt` — so a re-adopted zombie's age
2378
+ // reflects the real turn duration and the ceiling fires on the ORIGINAL turn.
2379
+ processingAnchorMs: processedAtMs,
2380
+ deadline: processedAtMs + this.pausedMaxWaitMs,
2381
+ // The server row is ALREADY `processing`; do not re-fire markProcessing.
2382
+ started: true,
2383
+ done: false,
2384
+ // Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
2385
+ // AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
2386
+ // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2387
+ // re-adopted row left wedged in `queued` still emits the signal once
2388
+ // (#210/#220 observability).
2389
+ stuckReported: false,
2390
+ // Task 5.2: a re-adopted actively-running row re-attaches into the SAME
2391
+ // watcher and so hits the SAME actively-running heartbeat branch in
2392
+ // `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
2393
+ // re-adopted and is confirming this row alive" via that `alive` heartbeat,
2394
+ // with no extra `re_adopted` signal needed (folds old WI-6).
2395
+ lastAliveAt: 0,
2396
+ aliveInFlight: false,
2397
+ awaitingHumanLatched: false,
2398
+ pausedOnQuestion: false,
2399
+ pausedOnPermission: false,
2400
+ pausedClearConfirmed: false,
2401
+ pausedInFlight: false,
2402
+ deliveryDeadlineAnchored: false
2403
+ });
2404
+ }
2405
+ /**
2406
+ * Start (but do NOT await) the per-session watcher loop if it has in-flight
2407
+ * work and is not already running. Single-flight per session. The loop is
2408
+ * tracked on the watcher and cleared when it settles; it never rejects (fully
2409
+ * guarded), so a failed poll/callback can never crash the run loop — the cron
2410
+ * stays as the safety net.
2411
+ */
2412
+ ensureWatcherRunning(sessionId) {
2413
+ const watcher = this.watchers.get(sessionId);
2414
+ if (!watcher) return;
2415
+ if (watcher.loop) return;
2416
+ if (watcher.inFlight.size === 0) {
2417
+ this.watchers.delete(sessionId);
2418
+ return;
2419
+ }
2420
+ const loop = this.runWatcherLoop(sessionId, watcher).finally(() => {
2421
+ watcher.loop = null;
2422
+ if (watcher.inFlight.size === 0) {
2423
+ this.watchers.delete(sessionId);
2424
+ }
2425
+ });
2426
+ watcher.loop = loop;
2427
+ }
2428
+ /**
2429
+ * The per-session polling loop (WI-3). Once per tick it:
2430
+ * 1. polls `GET /session/:id/message` once and, per in-flight message,
2431
+ * computes `messageRunState` and fires markProcessing (queued→running) /
2432
+ * markDone (done) exactly once per transition;
2433
+ * 2. applies the idle-path re-dispatch guard (a dispatched message that never
2434
+ * APPEARS → re-dispatch — D1 obligation 2);
2435
+ * 3. polls `/question` + `/permission` (scoped to the session) and surfaces
2436
+ * NEW ones via `reportInteraction`, carrying the PAUSED message's own
2437
+ * `source_message_id`;
2438
+ * 4. drops messages that completed or timed out from the in-flight set.
2439
+ * Exits when the in-flight set empties. Never throws.
2440
+ */
2441
+ async runWatcherLoop(sessionId, watcher) {
2442
+ try {
2443
+ while (watcher.inFlight.size > 0) {
2444
+ await this.sleep(this.pausedPollIntervalMs);
2445
+ let messages = null;
2446
+ try {
2447
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
2448
+ if (res.ok) {
2449
+ const body = await res.json();
2450
+ messages = Array.isArray(body) ? body : null;
2451
+ }
2452
+ } catch {
2453
+ }
2454
+ if (messages != null && messages.length > 0) {
2455
+ watcher.lastGoodPollAt = this.now();
2456
+ watcher.hadUsablePoll = true;
2457
+ } else {
2458
+ const emptyButReachable = messages != null;
2459
+ const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
2460
+ if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
2461
+ continue;
2462
+ }
2463
+ }
2464
+ const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
2465
+ for (const inFlight of [...watcher.inFlight.values()]) {
2466
+ await this.serviceInFlightMessage(
2467
+ sessionId,
2468
+ watcher,
2469
+ inFlight,
2470
+ messages,
2471
+ openQuestions,
2472
+ openPermissions,
2473
+ questionsPolledOk,
2474
+ permissionsPolledOk
2475
+ );
2476
+ }
2477
+ }
2478
+ } catch (err) {
2479
+ if (err instanceof ChannelAuthError) {
2480
+ this.log({
2481
+ level: "error",
2482
+ message: `Session watcher aborted on auth failure for session ${sessionId.slice(0, 8)} \u2014 clearing in-flight state for re-drive after re-auth: ${err.message}`,
2483
+ conversation_id: watcher.conv.id
2484
+ });
2485
+ for (const evidentMessageId of [...watcher.inFlight.keys()]) {
2486
+ this.readopted.delete(evidentMessageId);
2487
+ this.removeInFlight(watcher, evidentMessageId);
2488
+ }
2489
+ return;
2490
+ }
2491
+ this.log({
2492
+ level: "error",
2493
+ message: `Session watcher failed for session ${sessionId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
2494
+ conversation_id: watcher.conv.id
2495
+ });
2496
+ }
2497
+ }
2498
+ /**
2499
+ * On FIRST observing a terminal (done/failed) state, ensure the delivery
2500
+ * (markDone/markFailed) transient-retry path has a real window. A long
2501
+ * ACTIVELY-running turn is kept past its original `deadline`, so by completion
2502
+ * `now >= deadline` already holds and the retry bound below would fire on the
2503
+ * first transient PATCH failure — dropping the message before its reply lands
2504
+ * (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
2505
+ * to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
2506
+ * or past now, so a still-ample window is left untouched.
2507
+ */
2508
+ anchorDeliveryDeadline(inFlight) {
2509
+ if (inFlight.deliveryDeadlineAnchored) return;
2510
+ inFlight.deliveryDeadlineAnchored = true;
2511
+ if (this.now() >= inFlight.deadline) {
2512
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2513
+ }
2514
+ }
2515
+ /**
2516
+ * Drive ONE in-flight message's lifecycle from the tick's message snapshot.
2517
+ * Fires markProcessing on queued→running and markDone on done (each once),
2518
+ * applies the idle-path re-dispatch guard, and removes the message from the
2519
+ * in-flight set on completion or timeout.
2520
+ */
2521
+ async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
2522
+ const conv = watcher.conv;
2523
+ const state = messageRunState(messages, inFlight.opencodeMessageId);
2524
+ const id = inFlight.evidentMessageId;
2525
+ if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
2526
+ else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
2527
+ if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
2528
+ else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
2529
+ const observedOpen = openQuestions.has(id) || openPermissions.has(id);
2530
+ const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
2531
+ const awaitingHuman = observedOpen || latchedPaused;
2532
+ if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
2533
+ let claimed;
2534
+ try {
2535
+ claimed = await this.markProcessing(
2536
+ conv.id,
2537
+ inFlight.evidentMessageId,
2538
+ sessionId,
2539
+ inFlight.opencodeMessageId
2540
+ );
2541
+ } catch (err) {
2542
+ if (err instanceof ChannelAuthError) throw err;
2543
+ this.log({
2544
+ level: "error",
2545
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2546
+ conversation_id: conv.id,
2547
+ message_id: inFlight.evidentMessageId
2548
+ });
2549
+ return;
2550
+ }
2551
+ inFlight.started = true;
2552
+ if (!claimed) {
2553
+ this.log({
2554
+ level: "info",
2555
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
2556
+ conversation_id: conv.id,
2557
+ message_id: inFlight.evidentMessageId
2558
+ });
2559
+ }
2560
+ }
2561
+ if (state === "done") {
2562
+ this.anchorDeliveryDeadline(inFlight);
2563
+ if (!inFlight.done) {
2564
+ this.log({
2565
+ level: "info",
2566
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
2567
+ conversation_id: conv.id,
2568
+ message_id: inFlight.evidentMessageId
2569
+ });
2570
+ try {
2571
+ await this.markDone(
2572
+ conv.id,
2573
+ inFlight.evidentMessageId,
2574
+ sessionId,
2575
+ inFlight.opencodeMessageId
2576
+ );
2577
+ } catch (err) {
2578
+ if (err instanceof ChannelAuthError) throw err;
2579
+ if (err instanceof ChannelTerminalError) {
2580
+ this.log({
2581
+ level: "error",
2582
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2583
+ conversation_id: conv.id,
2584
+ message_id: inFlight.evidentMessageId
2585
+ });
2586
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2587
+ return;
2588
+ }
2589
+ if (this.now() >= inFlight.deadline) {
2590
+ this.log({
2591
+ level: "error",
2592
+ 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)}`,
2593
+ conversation_id: conv.id,
2594
+ message_id: inFlight.evidentMessageId
2595
+ });
2596
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2597
+ return;
2598
+ }
2599
+ this.log({
2600
+ level: "error",
2601
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2602
+ conversation_id: conv.id,
2603
+ message_id: inFlight.evidentMessageId
2604
+ });
2605
+ return;
2606
+ }
2607
+ inFlight.done = true;
2608
+ }
2609
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2610
+ return;
2611
+ }
2612
+ if (state === "failed") {
2613
+ this.anchorDeliveryDeadline(inFlight);
2614
+ if (!inFlight.done) {
2615
+ const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
2616
+ this.log({
2617
+ level: "error",
2618
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2619
+ conversation_id: conv.id,
2620
+ message_id: inFlight.evidentMessageId
2621
+ });
2622
+ try {
2623
+ await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
2624
+ } catch (err) {
2625
+ if (err instanceof ChannelAuthError) throw err;
2626
+ if (err instanceof ChannelTerminalError) {
2627
+ this.log({
2628
+ level: "error",
2629
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
2630
+ conversation_id: conv.id,
2631
+ message_id: inFlight.evidentMessageId
2632
+ });
2633
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2634
+ return;
2635
+ }
2636
+ if (this.now() >= inFlight.deadline) {
2637
+ this.log({
2638
+ level: "error",
2639
+ 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)}`,
2640
+ conversation_id: conv.id,
2641
+ message_id: inFlight.evidentMessageId
2642
+ });
2643
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2644
+ return;
2645
+ }
2646
+ this.log({
2647
+ level: "error",
2648
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
2649
+ conversation_id: conv.id,
2650
+ message_id: inFlight.evidentMessageId
2651
+ });
2652
+ return;
2653
+ }
2654
+ inFlight.done = true;
2655
+ }
2656
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2657
+ return;
2658
+ }
2659
+ const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
2660
+ const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
2661
+ if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
2662
+ inFlight.stuckReported = true;
2663
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
2664
+ stuck_for_ms: this.now() - inFlight.dispatchedAt
2665
+ });
2666
+ }
2667
+ const activelyRunning = state === "running" && !awaitingHuman;
2668
+ if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
2669
+ this.log({
2670
+ level: "error",
2671
+ 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`,
2672
+ conversation_id: conv.id,
2673
+ message_id: inFlight.evidentMessageId
2674
+ });
2675
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2676
+ watched_for_ms: this.now() - inFlight.processingAnchorMs
2677
+ });
2678
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2679
+ return;
2680
+ }
2681
+ if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
2682
+ inFlight.aliveInFlight = true;
2683
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
2684
+ inFlight.aliveInFlight = false;
2685
+ if (ok) inFlight.lastAliveAt = this.now();
2686
+ });
2687
+ }
2688
+ if (awaitingHuman) {
2689
+ if (!inFlight.awaitingHumanLatched) {
2690
+ inFlight.deadline = this.now() + this.pausedMaxWaitMs;
2691
+ inFlight.awaitingHumanLatched = true;
2692
+ }
2693
+ if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
2694
+ inFlight.pausedInFlight = true;
2695
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
2696
+ inFlight.pausedInFlight = false;
2697
+ if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
2698
+ });
2699
+ }
2700
+ } else if (inFlight.awaitingHumanLatched) {
2701
+ inFlight.awaitingHumanLatched = false;
2702
+ inFlight.pausedOnQuestion = false;
2703
+ inFlight.pausedOnPermission = false;
2704
+ inFlight.pausedClearConfirmed = false;
2705
+ }
2706
+ const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
2707
+ const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
2708
+ (sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
2709
+ );
2710
+ const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
2711
+ if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
2712
+ this.log({
2713
+ level: "info",
2714
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
2715
+ conversation_id: conv.id,
2716
+ message_id: inFlight.evidentMessageId
2717
+ });
2718
+ void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
2719
+ watched_for_ms: this.now() - inFlight.dispatchedAt
2720
+ });
2721
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
2722
+ }
2723
+ }
2724
+ // -------------------------------------------------------------------------
2725
+ // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
2726
+ // -------------------------------------------------------------------------
2727
+ /**
2728
+ * Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
2729
+ *
2730
+ * The pending drain only re-drives `pending` rows; a message already flipped to
2731
+ * `processing` before the runner died is watched by nobody until the 15-min
2732
+ * cron resets it. Here we fetch those rows, and per row resolve its correlated
2733
+ * reply against opencode's OWN session store — completing, re-attaching, or
2734
+ * (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
2735
+ * is idempotent per message (Invariant 2): a row a watcher already tracks is
2736
+ * skipped in `readoptOne` — one driver, no double-drive.
2737
+ *
2738
+ * Only `ChannelAuthError` propagates (to `drainPending`, like the pending
2739
+ * path); every other early return LOGS a reason with context — no silent drop.
2740
+ */
2741
+ async readoptProcessing() {
2742
+ const rows = await this.getProcessingMessages();
2743
+ if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0 || this.readoptPollUnresolvedSignalled.size > 0) {
2744
+ const stillProcessing = new Set(rows.map((r) => r.id));
2745
+ for (const id of [
2746
+ ...this.dontRedispatch,
2747
+ ...this.doneUndeliverable,
2748
+ ...this.readoptPollUnresolvedSignalled
2749
+ ]) {
2750
+ if (!stillProcessing.has(id)) {
2751
+ const cleared = this.dontRedispatch.delete(id);
2752
+ const clearedUndeliverable = this.doneUndeliverable.delete(id);
2753
+ this.readoptPollUnresolvedSignalled.delete(id);
2754
+ if (cleared || clearedUndeliverable) {
2755
+ this.log({
2756
+ level: "info",
2757
+ message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
2758
+ message_id: id
2759
+ });
2760
+ }
2761
+ }
2762
+ }
2763
+ }
2764
+ if (rows.length === 0) return;
2765
+ const bySession = /* @__PURE__ */ new Map();
2766
+ for (const row of rows) {
2767
+ if (!row.opencode_session_id) {
2768
+ this.log({
2769
+ level: "error",
2770
+ message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
2771
+ conversation_id: row.conversation_id,
2772
+ message_id: row.id
2773
+ });
2774
+ continue;
2775
+ }
2776
+ const list = bySession.get(row.opencode_session_id) ?? [];
2777
+ list.push(row);
2778
+ bySession.set(row.opencode_session_id, list);
2779
+ }
2780
+ for (const [sessionId, sessionRows] of bySession) {
2781
+ let messages;
2782
+ try {
2783
+ const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
2784
+ if (!res.ok) {
2785
+ this.log({
2786
+ level: "error",
2787
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
2788
+ });
2789
+ continue;
2790
+ }
2791
+ const body = await res.json();
2792
+ if (!Array.isArray(body)) {
2793
+ this.log({
2794
+ level: "error",
2795
+ message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
2796
+ });
2797
+ continue;
2798
+ }
2799
+ messages = body;
2800
+ } catch (err) {
2801
+ this.log({
2802
+ level: "error",
2803
+ message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
2804
+ });
2805
+ continue;
2806
+ }
2807
+ const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
2808
+ const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
2809
+ for (const row of sessionRows) {
2810
+ await this.readoptOne(sessionId, row, messages, sessionOngoing);
2811
+ }
2812
+ }
2813
+ }
2814
+ /**
2815
+ * Re-adopt ONE `processing` row against the tick's session message snapshot
2816
+ * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
2817
+ *
2818
+ * Branches on `messageRunState(messages, row.opencode_message_id)` — the
2819
+ * opencode-assigned user-message id persisted on the first `processing` PATCH
2820
+ * (#218). A row with a NULL stored id (dispatched but the read-back never landed
2821
+ * before the restart) has no id to correlate → treated as an orphan and
2822
+ * re-dispatched (at most once, see `forceReadoptRun`):
2823
+ * - `done` → `markDone` now (guarded like the watcher's done branch);
2824
+ * - `failed` → `markFailed` with the surfaced error (issue #182), so an
2825
+ * errored turn is reported failed on restart, NOT re-dispatched;
2826
+ * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
2827
+ * tracking the stored id so the reply correlates by it;
2828
+ * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
2829
+ *
2830
+ * Only `ChannelAuthError` propagates.
2831
+ */
2832
+ async readoptOne(sessionId, row, messages, sessionOngoing) {
2833
+ if (this.isTracked(sessionId, row.id)) {
2834
+ this.log({
2835
+ level: "info",
2836
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
2837
+ conversation_id: row.conversation_id,
2838
+ message_id: row.id
2839
+ });
2840
+ return;
2841
+ }
2842
+ const ocId = row.opencode_message_id;
2843
+ const state = messageRunState(messages, ocId ?? "");
2844
+ if (state === "done") {
2845
+ if (this.doneUndeliverable.has(row.id)) {
2846
+ this.log({
2847
+ level: "info",
2848
+ message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
2849
+ conversation_id: row.conversation_id,
2850
+ message_id: row.id
2851
+ });
2852
+ return;
2853
+ }
2854
+ this.log({
2855
+ level: "info",
2856
+ message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
2857
+ conversation_id: row.conversation_id,
2858
+ message_id: row.id
2859
+ });
2860
+ try {
2861
+ await this.markDone(row.conversation_id, row.id, sessionId, ocId);
2862
+ } catch (err) {
2863
+ if (err instanceof ChannelAuthError) throw err;
2864
+ if (err instanceof ChannelTerminalError) {
2865
+ this.doneUndeliverable.add(row.id);
2866
+ this.log({
2867
+ level: "error",
2868
+ 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}`,
2869
+ conversation_id: row.conversation_id,
2870
+ message_id: row.id
2871
+ });
2872
+ void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
2873
+ return;
2874
+ }
2875
+ this.log({
2876
+ level: "error",
2877
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2878
+ conversation_id: row.conversation_id,
2879
+ message_id: row.id
2880
+ });
2881
+ return;
2882
+ }
2883
+ this.dontRedispatch.delete(row.id);
2884
+ void this.postSignal(row.conversation_id, row.id, "readopt_done");
2885
+ return;
2886
+ }
2887
+ if (state === "failed") {
2888
+ const error2 = messageError(messages, ocId ?? "") ?? void 0;
2889
+ this.log({
2890
+ level: "error",
2891
+ message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
2892
+ conversation_id: row.conversation_id,
2893
+ message_id: row.id
2894
+ });
2895
+ try {
2896
+ await this.markFailed(row.conversation_id, row.id, sessionId, error2);
2897
+ } catch (err) {
2898
+ if (err instanceof ChannelAuthError) throw err;
2899
+ if (err instanceof ChannelTerminalError) {
2900
+ this.doneUndeliverable.add(row.id);
2901
+ this.log({
2902
+ level: "error",
2903
+ 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}`,
2904
+ conversation_id: row.conversation_id,
2905
+ message_id: row.id
2906
+ });
2907
+ void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
2908
+ return;
2909
+ }
2910
+ this.log({
2911
+ level: "error",
2912
+ message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
2913
+ conversation_id: row.conversation_id,
2914
+ message_id: row.id
2915
+ });
2916
+ return;
2917
+ }
2918
+ this.dontRedispatch.delete(row.id);
2919
+ void this.postSignal(row.conversation_id, row.id, "readopt_failed");
2920
+ return;
2921
+ }
2922
+ if (this.dontRedispatch.has(row.id)) {
2923
+ this.log({
2924
+ level: "info",
2925
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
2926
+ conversation_id: row.conversation_id,
2927
+ message_id: row.id
2928
+ });
2929
+ return;
2930
+ }
2931
+ let statusReadableOngoing = null;
2932
+ if (state === "running" && ocId) {
2933
+ const reply = findLastAssistantReplyFor(messages, ocId);
2934
+ const shape = this.replyCompletionShape(reply);
2935
+ const ongoing = sessionOngoing;
2936
+ statusReadableOngoing = ongoing;
2937
+ if (ongoing === false) {
2938
+ this.log({
2939
+ level: "info",
2940
+ 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)`,
2941
+ conversation_id: row.conversation_id,
2942
+ message_id: row.id
2943
+ });
2944
+ await this.forceReadoptRun(sessionId, row);
2945
+ return;
2946
+ }
2947
+ if (ongoing === true) {
2948
+ this.log({
2949
+ level: "info",
2950
+ 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)`,
2951
+ conversation_id: row.conversation_id,
2952
+ message_id: row.id
2953
+ });
2954
+ } else {
2955
+ if (shape === "b1") {
2956
+ this.log({
2957
+ level: "info",
2958
+ 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`,
2959
+ conversation_id: row.conversation_id,
2960
+ message_id: row.id
2961
+ });
2962
+ if (!this.readoptPollUnresolvedSignalled.has(row.id)) {
2963
+ this.readoptPollUnresolvedSignalled.add(row.id);
2964
+ void this.postSignal(row.conversation_id, row.id, "readopt_poll_unresolved");
2965
+ }
2966
+ return;
2967
+ }
2968
+ this.log({
2969
+ level: "info",
2970
+ 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`,
2971
+ conversation_id: row.conversation_id,
2972
+ message_id: row.id
2973
+ });
2974
+ }
2975
+ }
2976
+ if (statusReadableOngoing === null && state === "running" && ocId && isPreamblePinnedRunning(messages, ocId)) {
2977
+ const descendantAlive = await this.isAnyDescendantSessionAlive(sessionId);
2978
+ if (descendantAlive === true) {
2979
+ this.log({
2980
+ level: "info",
2981
+ 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)`,
2982
+ conversation_id: row.conversation_id,
2983
+ message_id: row.id
2984
+ });
2985
+ } else {
2986
+ this.log({
2987
+ level: "info",
2988
+ 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)" : ""}`,
2989
+ conversation_id: row.conversation_id,
2990
+ message_id: row.id
2991
+ });
2992
+ await this.forceReadoptRun(sessionId, row);
2993
+ return;
2994
+ }
2995
+ }
2996
+ if ((state === "running" || state === "queued") && ocId) {
2997
+ const conv = this.convForRow(sessionId, row);
2998
+ const message = this.queuedMessageForRow(row);
2999
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
3000
+ this.dispatched.add(row.id);
3001
+ this.readopted.add(row.id);
3002
+ this.ensureWatcherRunning(sessionId);
3003
+ this.log({
3004
+ level: "info",
3005
+ message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
3006
+ conversation_id: row.conversation_id,
3007
+ message_id: row.id
3008
+ });
3009
+ void this.postSignal(row.conversation_id, row.id, "readopt_reattached");
3010
+ return;
3011
+ }
3012
+ await this.forceReadoptRun(sessionId, row);
3013
+ }
3014
+ /**
3015
+ * Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
3016
+ *
3017
+ * #218/WI-5: the row's user message is absent (never kept, or a null stored id),
3018
+ * so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
3019
+ * read it back, and register the watcher under the assigned id so the reply
3020
+ * correlates server-side.
3021
+ *
3022
+ * ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
3023
+ * id). Without a guard, if this dispatches on tick N but the read-back+persist
3024
+ * hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
3025
+ * tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
3026
+ * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
3027
+ * short-circuit while the row is latched; clear it on a successful dispatch (the
3028
+ * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
3029
+ * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
3030
+ * may retry exactly once more).
3031
+ *
3032
+ * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
3033
+ * `processed_at` (Invariant 1).
3034
+ */
3035
+ async forceReadoptRun(sessionId, row) {
3036
+ if (this.stopped) {
3037
+ this.log({
3038
+ level: "info",
3039
+ 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`,
3040
+ conversation_id: row.conversation_id,
3041
+ message_id: row.id
3042
+ });
3043
+ return;
3044
+ }
3045
+ if (this.awaitingReadopt.has(row.id)) {
3046
+ this.log({
3047
+ level: "info",
3048
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
3049
+ conversation_id: row.conversation_id,
3050
+ message_id: row.id
3051
+ });
3052
+ return;
3053
+ }
3054
+ if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
3055
+ this.dontRedispatch.add(row.id);
3056
+ this.log({
3057
+ level: "info",
3058
+ 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)`,
3059
+ conversation_id: row.conversation_id,
3060
+ message_id: row.id
3061
+ });
3062
+ void this.postSignal(row.conversation_id, row.id, "readopt_window_elapsed");
3063
+ return;
3064
+ }
3065
+ const options = {
3066
+ agent: row.opencode_agent ?? void 0,
3067
+ model: row.opencode_model ?? void 0
3068
+ };
3069
+ this.log({
3070
+ level: "info",
3071
+ message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
3072
+ conversation_id: row.conversation_id,
3073
+ message_id: row.id
3074
+ });
3075
+ this.awaitingReadopt.add(row.id);
3076
+ let ocId;
3077
+ try {
3078
+ ocId = await this.dispatchLocked(
3079
+ sessionId,
3080
+ () => sendPromptAsync(this.port, sessionId, row.content, options)
3081
+ );
3082
+ } catch (err) {
3083
+ this.awaitingReadopt.delete(row.id);
3084
+ if (err instanceof ChannelAuthError) throw err;
3085
+ this.log({
3086
+ level: "error",
3087
+ message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
3088
+ conversation_id: row.conversation_id,
3089
+ message_id: row.id
3090
+ });
3091
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3092
+ return;
3093
+ }
3094
+ if (ocId === null) {
3095
+ this.awaitingReadopt.delete(row.id);
3096
+ this.log({
3097
+ level: "error",
3098
+ 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`,
3099
+ conversation_id: row.conversation_id,
3100
+ message_id: row.id
3101
+ });
3102
+ void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3103
+ return;
3104
+ }
3105
+ const conv = this.convForRow(sessionId, row);
3106
+ const message = this.queuedMessageForRow(row);
3107
+ this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
3108
+ this.dispatched.add(row.id);
3109
+ this.readopted.add(row.id);
3110
+ this.awaitingReadopt.delete(row.id);
3111
+ this.ensureWatcherRunning(sessionId);
3112
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
1536
3113
  }
1537
- async ensureSession(conv) {
1538
- const cached = this.sessions.get(conv.id);
1539
- if (cached) return cached;
1540
- if (conv.opencode_session_id) {
1541
- this.sessions.set(conv.id, conv.opencode_session_id);
1542
- return conv.opencode_session_id;
1543
- }
1544
- const sessionId = await createOpenCodeSession(this.port);
1545
- this.sessions.set(conv.id, sessionId);
1546
- await this.persistSession(conv.id, sessionId).catch(() => {
3114
+ /**
3115
+ * True if `evidentMessageId` is already being driven — either in the
3116
+ * authoritative `dispatched` set or a live watcher's in-flight set for this
3117
+ * session (Invariant 2, WI-5). Either signal means a watcher owns the row.
3118
+ */
3119
+ isTracked(sessionId, evidentMessageId) {
3120
+ if (this.dispatched.has(evidentMessageId)) return true;
3121
+ const watcher = this.watchers.get(sessionId);
3122
+ return watcher?.inFlight.has(evidentMessageId) ?? false;
3123
+ }
3124
+ /**
3125
+ * Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
3126
+ * deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
3127
+ * for `processing` rows, but if it is somehow null/unparseable fall back to
3128
+ * `now` (defensive) AND log — a fallback means the anchor is weaker than
3129
+ * intended, which is worth surfacing.
3130
+ */
3131
+ processedAtMs(row) {
3132
+ const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
3133
+ if (!Number.isNaN(parsed)) return parsed;
3134
+ this.log({
3135
+ level: "error",
3136
+ message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
3137
+ conversation_id: row.conversation_id,
3138
+ message_id: row.id
1547
3139
  });
1548
- return sessionId;
3140
+ return this.now();
3141
+ }
3142
+ /** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
3143
+ convForRow(sessionId, row) {
3144
+ return {
3145
+ id: row.conversation_id,
3146
+ agent_id: this.agentId,
3147
+ opencode_session_id: sessionId,
3148
+ pending_message_count: 0,
3149
+ oldest_pending_at: row.processed_at
3150
+ };
3151
+ }
3152
+ /** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
3153
+ queuedMessageForRow(row) {
3154
+ return {
3155
+ id: row.id,
3156
+ content: row.content,
3157
+ status: "processing",
3158
+ opencode_agent: row.opencode_agent,
3159
+ opencode_model: row.opencode_model,
3160
+ source_message_id: row.source_message_id,
3161
+ slack_user_id: row.slack_user_id
3162
+ };
3163
+ }
3164
+ /**
3165
+ * Remove a message from the in-flight set AND the authoritative dispatched
3166
+ * set. Once the in-flight set empties, the watcher loop's `while` guard exits
3167
+ * and its `.finally` removes the session entry from `this.watchers`.
3168
+ *
3169
+ * Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
3170
+ * (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
3171
+ * cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
3172
+ * re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
3173
+ * re-adopted message that completed (`done`) needs no marker — it's leaving
3174
+ * `processing`. This suppresses only re-dispatch: if its reply later completes,
3175
+ * the done branch still delivers it (Bugbot #202).
3176
+ */
3177
+ removeInFlight(watcher, evidentMessageId) {
3178
+ const inFlight = watcher.inFlight.get(evidentMessageId);
3179
+ if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
3180
+ this.dontRedispatch.add(evidentMessageId);
3181
+ this.log({
3182
+ level: "info",
3183
+ message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
3184
+ conversation_id: watcher.conv.id,
3185
+ message_id: evidentMessageId
3186
+ });
3187
+ }
3188
+ watcher.inFlight.delete(evidentMessageId);
3189
+ this.dispatched.delete(evidentMessageId);
3190
+ }
3191
+ /**
3192
+ * Poll `/question` + `/permission` (scoped to the session) and surface NEW ones
3193
+ * via `reportInteraction` (Task 3.5), carrying the PAUSED message's own
3194
+ * `source_message_id` so the server @mentions the correct person under
3195
+ * concurrency. Dedups by interaction id across ticks (reused per-session sets).
3196
+ *
3197
+ * The interaction is attributed to the in-flight message it paused on. opencode
3198
+ * stamps a `messageID` on a permission (and `tool.messageID` on a question) =
3199
+ * the assistant message id, whose `parentID` is the user message id — but the
3200
+ * simplest robust attribution here is: the single in-flight message that is
3201
+ * RUNNING (not done) is the one that paused. With one running message that is
3202
+ * unambiguous; with several we prefer an explicit messageID match, else the
3203
+ * oldest running message.
3204
+ *
3205
+ * Returns the set of in-flight Evident message ids that are paused awaiting a
3206
+ * human — an outstanding (still-open) question/permission is attributed to them.
3207
+ * `serviceInFlightMessage` uses this to keep an actively-running turn watched
3208
+ * forever (ADR-0047) while still bounding a turn merely blocked on a person who
3209
+ * may never answer. Attribution here covers ALL open interactions, not just
3210
+ * NEW (un-deduped) ones — a question stays "awaiting a human" until answered,
3211
+ * even after it was already surfaced to the channel.
3212
+ */
3213
+ async pollInteractions(sessionId, watcher, messages) {
3214
+ const openQuestions = /* @__PURE__ */ new Set();
3215
+ const openPermissions = /* @__PURE__ */ new Set();
3216
+ let questionsPolledOk = true;
3217
+ let permissionsPolledOk = true;
3218
+ let questions = [];
3219
+ try {
3220
+ const res = await this.fetchImpl(`${this.opencodeBase}/question`);
3221
+ if (res.ok) {
3222
+ const body = await res.json();
3223
+ if (Array.isArray(body)) {
3224
+ questions = body;
3225
+ } else {
3226
+ questionsPolledOk = false;
3227
+ }
3228
+ } else {
3229
+ questionsPolledOk = false;
3230
+ }
3231
+ } catch {
3232
+ questionsPolledOk = false;
3233
+ }
3234
+ for (const q of questions) {
3235
+ if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
3236
+ const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
3237
+ if (paused) openQuestions.add(paused.evidentMessageId);
3238
+ if (watcher.reportedQuestions.has(q.id)) continue;
3239
+ const reported = await this.reportInteraction(
3240
+ watcher.conv.id,
3241
+ "question",
3242
+ q,
3243
+ paused?.message.source_message_id ?? void 0
3244
+ );
3245
+ if (reported) watcher.reportedQuestions.add(q.id);
3246
+ }
3247
+ let permissions = [];
3248
+ try {
3249
+ const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
3250
+ if (res.ok) {
3251
+ const body = await res.json();
3252
+ if (Array.isArray(body)) {
3253
+ permissions = body;
3254
+ } else {
3255
+ permissionsPolledOk = false;
3256
+ }
3257
+ } else {
3258
+ permissionsPolledOk = false;
3259
+ }
3260
+ } catch {
3261
+ permissionsPolledOk = false;
3262
+ }
3263
+ for (const p of permissions) {
3264
+ if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
3265
+ const paused = this.attributeInteraction(watcher, p.messageID, messages);
3266
+ if (paused) openPermissions.add(paused.evidentMessageId);
3267
+ if (watcher.reportedPermissions.has(p.id)) continue;
3268
+ const reported = await this.reportInteraction(
3269
+ watcher.conv.id,
3270
+ "permission",
3271
+ p,
3272
+ paused?.message.source_message_id ?? void 0
3273
+ );
3274
+ if (reported) watcher.reportedPermissions.add(p.id);
3275
+ }
3276
+ return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
3277
+ }
3278
+ /**
3279
+ * True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
3280
+ * i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
3281
+ * watched root. Sub-agents spawned via the `task` tool run in child sessions,
3282
+ * so their questions/permissions live under a different `sessionID` that must
3283
+ * still be attributed to the root conversation the watcher owns.
3284
+ *
3285
+ * Parents are cached in `sessionParents` so we walk each session at most once;
3286
+ * a bounded depth cap guards against a cycle or a pathological chain, and any
3287
+ * fetch failure is treated as "not a descendant" (best-effort — the interaction
3288
+ * simply isn't surfaced this tick and is retried next tick once resolvable).
3289
+ */
3290
+ async sessionBelongsTo(sessionId, rootSessionId) {
3291
+ let current = sessionId;
3292
+ for (let depth = 0; current && depth < 32; depth++) {
3293
+ if (current === rootSessionId) return true;
3294
+ const parent = await this.resolveSessionParent(current);
3295
+ if (parent === null || parent === void 0) return false;
3296
+ current = parent;
3297
+ }
3298
+ return false;
1549
3299
  }
1550
3300
  /**
1551
- * Local reconcile: re-query `GET /session/:id` and check `time.completed`.
1552
- * Best-effort if opencode is unreachable or the field is absent we proceed
1553
- * to mark done anyway (the blocking call already returned).
3301
+ * Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
3302
+ * `null` for a root session (no parent) and `undefined` when opencode is
3303
+ * unreachable / the session can't be read (so the caller stops walking without
3304
+ * caching a wrong answer — the next tick retries).
1554
3305
  */
1555
- async confirmCompletion(sessionId) {
3306
+ async resolveSessionParent(sessionId) {
3307
+ const cached = this.sessionParents.get(sessionId);
3308
+ if (cached !== void 0) return cached;
3309
+ let parent = void 0;
1556
3310
  try {
1557
3311
  const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
1558
- if (!res.ok) return;
1559
- const session = await res.json();
1560
- if (session.time && session.time.completed == null) {
1561
- this.log({
1562
- level: "info",
1563
- message: `Session ${sessionId.slice(0, 8)} not marked completed on reconcile \u2014 delivering anyway`
1564
- });
3312
+ if (res.ok) {
3313
+ const body = await res.json();
3314
+ parent = body && typeof body.parentID === "string" ? body.parentID : null;
1565
3315
  }
1566
3316
  } catch {
3317
+ parent = void 0;
3318
+ }
3319
+ if (parent !== void 0) this.sessionParents.set(sessionId, parent);
3320
+ return parent;
3321
+ }
3322
+ /**
3323
+ * DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
3324
+ * (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
3325
+ *
3326
+ * The PRIMARY recovery trigger is "preamble-pinned on recovery ⇒ idle" — a
3327
+ * runner restart wipes OpenCode's in-memory `SessionStatus`/`Runner`, so a
3328
+ * completed `finish: "tool-calls"` root reply encountered during re-adoption is
3329
+ * idle by OpenCode's own definition and is re-dispatched. This method exists only
3330
+ * so the WI-3 caller can VETO that re-dispatch in the rare case a descendant is
3331
+ * provably in flight at the exact moment of recovery.
3332
+ *
3333
+ * "Alive" criterion (TIGHTENED): a descendant is alive only when it is PROVABLY,
3334
+ * ACTIVELY generating — its LAST message is an assistant still mid-generation
3335
+ * (`completed == null`, via `isSessionActivelyGenerating`). An
3336
+ * INCOMPLETE-BUT-NOT-GENERATING child — last message a user message, or a
3337
+ * completed `finish: "tool-calls"` step — is NOT alive after a restart (nothing
3338
+ * is generating once the runner is gone), so it does NOT veto. (This is
3339
+ * deliberately NOT `!isTurnComplete`, which also matches those dead-but-non-terminal
3340
+ * shapes and would falsely veto — re-hanging the very turn this path recovers.)
3341
+ *
3342
+ * Return contract (encoded so WI-3 need not re-derive it):
3343
+ * - `true` → a descendant is provably, actively generating (veto re-dispatch).
3344
+ * - `false` → descendants exist but none is actively generating (the restart
3345
+ * case), OR no descendant is found at all.
3346
+ * - `null` → liveness is INDETERMINATE (enumeration via `listSessions` failed).
3347
+ *
3348
+ * ⚠️ `null` (UNKNOWN) MUST NOT be treated as "alive": WI-3 treats `null` the same
3349
+ * as `false` and does NOT veto — a restart guarantees no live runner, so an
3350
+ * indeterminate cross-check almost always means "couldn't reach a child that no
3351
+ * longer exists". The inversion lives in the caller; this method just reports
3352
+ * true/false/null faithfully.
3353
+ *
3354
+ * VERIFY-BEFORE-DEPEND: we depend ONLY on (a) `parentID` from `GET /session/:id`
3355
+ * (already proven by the existing child-session interaction tests, via
3356
+ * `resolveSessionParent`/`sessionBelongsTo`) and (b) the child's own message-list
3357
+ * terminal state. We do NOT depend on any session-level `busy`/`idle` field —
3358
+ * there is none on `GET /session/:id`; OpenCode's busy state is in-memory
3359
+ * `SessionStatus` only.
3360
+ */
3361
+ async isAnyDescendantSessionAlive(rootSessionId) {
3362
+ const sessions = await listSessions(this.port);
3363
+ if (!sessions) {
3364
+ this.log({
3365
+ level: "error",
3366
+ message: `Re-adopt: could not enumerate sessions to cross-check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating child liveness as indeterminate`
3367
+ });
3368
+ return null;
3369
+ }
3370
+ for (const candidate of sessions) {
3371
+ if (!candidate?.id || candidate.id === rootSessionId) continue;
3372
+ if (!await this.sessionBelongsTo(candidate.id, rootSessionId)) continue;
3373
+ const childMsgs = await getSessionMessages(this.port, candidate.id);
3374
+ if (isSessionActivelyGenerating(childMsgs)) {
3375
+ return true;
3376
+ }
3377
+ }
3378
+ return false;
3379
+ }
3380
+ /**
3381
+ * Cheap decision-telemetry label for a running row's LAST correlated reply
3382
+ * (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
3383
+ * - `b1` — the reply itself is still in flight (`time.completed == null`) —
3384
+ * the aborted-in-flight production bug after a restart.
3385
+ * - `b2` — a COMPLETED reply pinned running only by `finish === "tool-calls"`
3386
+ * (the sub-agent preamble — #253's shape).
3387
+ * - `other` — any other shape (defensive; a running row is normally b1 or b2).
3388
+ * Reads `info.time.completed` / `info.finish` (tolerating the legacy top-level
3389
+ * shape) directly rather than re-importing the module-private `completedOf`/
3390
+ * `finishOf` — this is a display label only, not a correctness predicate.
3391
+ */
3392
+ replyCompletionShape(reply) {
3393
+ if (!reply) return "other";
3394
+ const completed = reply.info?.time?.completed ?? reply.time?.completed;
3395
+ if (completed == null) return "b1";
3396
+ const finish = reply.info?.finish ?? reply.finish;
3397
+ return finish === "tool-calls" ? "b2" : "other";
3398
+ }
3399
+ /**
3400
+ * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
3401
+ *
3402
+ * The interaction carries `interactionMessageId` — the ASSISTANT message id
3403
+ * that raised it (a question's `tool.messageID` / a permission's `messageID`).
3404
+ * That assistant message is the reply to ONE of our minted user messages
3405
+ * (correlated by `parentID`, GATE-B). So when we have the tick's message
3406
+ * snapshot, we resolve each running in-flight message's correlated assistant
3407
+ * reply (`findAssistantReplyAfter`) and match its id against
3408
+ * `interactionMessageId` — giving an EXACT attribution even with several
3409
+ * messages in flight concurrently in one session.
3410
+ *
3411
+ * We fall back to the oldest running message ONLY when no exact match is
3412
+ * possible (the id is absent, the snapshot is missing, or the reply has not yet
3413
+ * been correlated). With a single running message either path is exact. Never
3414
+ * throws.
3415
+ *
3416
+ * Attribution must NOT depend on our own `started` PATCH flag: opencode can
3417
+ * START a turn AND raise a question/permission BEFORE our next tick fires
3418
+ * `markProcessing` (which sets `started`). Relying on `started` would leave the
3419
+ * running set empty in that window and let the server fall back to "newest
3420
+ * processing/pending" — possibly @mentioning a FOLLOW-UP author rather than the
3421
+ * person whose active turn actually paused. So we derive "running" from the
3422
+ * tick's `messages` snapshot via `messageRunState` instead.
3423
+ */
3424
+ attributeInteraction(watcher, interactionMessageId, messages) {
3425
+ const inFlight = [...watcher.inFlight.values()].filter((m) => !m.done);
3426
+ if (inFlight.length === 0) return void 0;
3427
+ if (interactionMessageId && messages) {
3428
+ const exact = inFlight.find((m) => {
3429
+ const reply = findAssistantReplyAfter(messages, m.opencodeMessageId);
3430
+ return reply != null && messageIdOf(reply) === interactionMessageId;
3431
+ });
3432
+ if (exact) return exact;
3433
+ }
3434
+ const byOldest = (a, b) => a.dispatchedAt - b.dispatchedAt;
3435
+ if (messages) {
3436
+ const runningPerSnapshot = inFlight.filter(
3437
+ (m) => messageRunState(messages, m.opencodeMessageId) === "running"
3438
+ );
3439
+ if (runningPerSnapshot.length > 0) {
3440
+ return runningPerSnapshot.sort(byOldest)[0];
3441
+ }
1567
3442
  }
3443
+ const startedRunning = inFlight.filter((m) => m.started);
3444
+ if (startedRunning.length > 0) {
3445
+ return startedRunning.sort(byOldest)[0];
3446
+ }
3447
+ return inFlight.sort(byOldest)[0];
1568
3448
  }
1569
3449
  // -------------------------------------------------------------------------
1570
3450
  // Evident API calls (combinedAuth thread routes)
@@ -1598,49 +3478,191 @@ var ChannelDriver = class {
1598
3478
  }
1599
3479
  return await res.json();
1600
3480
  }
1601
- async markProcessing(conversationId, messageId) {
3481
+ /**
3482
+ * Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
3483
+ * The pending path (`getPendingConversations`/`getPendingMessages`) only
3484
+ * surfaces `pending` rows, so a message already `processing` when the runner
3485
+ * died is invisible to it — this dedicated endpoint returns exactly those rows
3486
+ * with the fields the re-adopt path needs (`processed_at`,
3487
+ * `opencode_session_id`, routing).
3488
+ *
3489
+ * Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
3490
+ * array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
3491
+ * other non-ok so `drainPending`'s try/finally leaves `draining` false and the
3492
+ * next tick retries.
3493
+ */
3494
+ async getProcessingMessages() {
3495
+ const res = await this.fetchImpl(
3496
+ `${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
3497
+ { headers: { Authorization: this.getAuthHeader() } }
3498
+ );
3499
+ this.assertAuth(res, "fetching processing messages");
3500
+ if (!res.ok) {
3501
+ throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
3502
+ }
3503
+ const data = await res.json();
3504
+ let messages = data.messages ?? [];
3505
+ if (this.conversationFilter) {
3506
+ messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
3507
+ }
3508
+ return messages;
3509
+ }
3510
+ /**
3511
+ * EXISTING combinedAuth route — now fired by the watcher on queued→running
3512
+ * (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
3513
+ * opencode_session_id}` → `notifyMessageStarted` (hourglass→runner swap +
3514
+ * deep-linked "View in Evident" notice).
3515
+ *
3516
+ * Return/throw contract (consumed by the watcher's swap-to-running guard):
3517
+ * - returns `true` → the server transitioned the row to processing;
3518
+ * - returns `false` → the server gave a DEFINITIVE "already-processing"
3519
+ * answer (a non-retryable, non-auth status — e.g. a
3520
+ * conflict because a duplicate already transitioned it),
3521
+ * so the caller treats it as already-started and does NOT
3522
+ * retry;
3523
+ * - throws `ChannelAuthError` on 401/403 (terminal auth failure);
3524
+ * - throws on a TRANSIENT failure (retryable 5xx/429 status, or a
3525
+ * network-level error from `fetch`) — i.e. NO definitive server response —
3526
+ * so the caller leaves the message un-started and retries the swap on the
3527
+ * next tick.
3528
+ * A single attempt (no internal retry): the watcher's per-tick loop is the
3529
+ * retry vehicle for the swap-to-running.
3530
+ */
3531
+ async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
1602
3532
  const res = await this.fetchImpl(
1603
3533
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1604
3534
  {
1605
3535
  method: "PATCH",
1606
3536
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1607
- body: JSON.stringify({ status: "processing" })
3537
+ body: JSON.stringify({
3538
+ status: "processing",
3539
+ opencode_session_id: sessionId,
3540
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
3541
+ })
1608
3542
  }
1609
3543
  );
1610
3544
  this.assertAuth(res, "marking message as processing");
1611
- return res.ok;
3545
+ if (res.ok) return true;
3546
+ if (isRetryableStatus(res.status)) {
3547
+ throw new Error(`marking message as processing: HTTP ${res.status}`);
3548
+ }
3549
+ return false;
1612
3550
  }
1613
3551
  /**
1614
- * EXISTING combinedAuth completion route — idempotent + retried (WI-CHAN-2).
1615
- * `PATCH .../messages/:id {status:'done', opencode_session_id}`. The server's
3552
+ * EXISTING combinedAuth completion route — idempotent (WI-CHAN-2). `PATCH
3553
+ * .../messages/:id {status:'done', opencode_session_id}`. The server's
1616
3554
  * `queued_conversation_messages.status`/`processed_at` gate makes a re-call
1617
- * for an already-`done` message a no-op (no double Slack post).
3555
+ * for an already-`done` message a no-op (no double Slack post). Fired by the
3556
+ * watcher on per-message completion (Task 3.4) — no `confirmCompletion`
3557
+ * round-trip (we already observed completion via the message list).
3558
+ *
3559
+ * SINGLE ATTEMPT (no in-call `callWithRetry` backoff). The per-session watcher
3560
+ * services its in-flight messages SEQUENTIALLY within a tick
3561
+ * (`runWatcherLoop` → `serviceInFlightMessage`), so a long multi-attempt
3562
+ * backoff here would BLOCK sibling messages in the SAME session/tick: while
3563
+ * message A's done PATCH burned its internal retries, message B could not be
3564
+ * swapped to running even though opencode had already started it. Instead this
3565
+ * does ONE PATCH and surfaces the SAME outcome contract the watcher's markDone
3566
+ * handler already relies on, leaning on the per-tick retry across ticks
3567
+ * (bounded by `inFlight.deadline`) rather than an in-call retry:
3568
+ * - resolves (`void`) → the server transitioned the row to done
3569
+ * (or idempotently confirmed already-done);
3570
+ * - throws `ChannelAuthError` → 401/403 (terminal auth failure → loop
3571
+ * cleanup, Finding 1);
3572
+ * - throws `ChannelTerminalError`→ non-retryable, non-auth 4xx (will never
3573
+ * succeed → straight to the cron, Finding 4);
3574
+ * - throws a plain `Error` → TRANSIENT 5xx/429 or a network-level error
3575
+ * (no definitive server response → the
3576
+ * watcher retries next tick within the
3577
+ * deadline, Finding 4).
1618
3578
  */
1619
- async markDone(conversationId, messageId, sessionId) {
3579
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
3580
+ const res = await this.fetchImpl(
3581
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3582
+ {
3583
+ method: "PATCH",
3584
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
3585
+ body: JSON.stringify({
3586
+ status: "done",
3587
+ opencode_session_id: sessionId,
3588
+ ...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
3589
+ })
3590
+ }
3591
+ );
3592
+ this.assertAuth(res, "marking message as done");
3593
+ if (res.ok) return;
3594
+ if (isRetryableStatus(res.status)) {
3595
+ throw new Error(`marking message as done: HTTP ${res.status}`);
3596
+ }
3597
+ throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
3598
+ }
3599
+ /**
3600
+ * Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
3601
+ * when provided (issue #182): a bare `markFailed(conv, msg)` sends
3602
+ * `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
3603
+ * OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
3604
+ * failure reason reaches the channel.
3605
+ */
3606
+ async markFailed(conversationId, messageId, sessionId, error2) {
3607
+ const body = { status: "failed" };
3608
+ if (sessionId !== void 0) body.opencode_session_id = sessionId;
3609
+ if (error2 !== void 0) body.error = error2;
1620
3610
  await this.callWithRetry(
1621
- "marking message as done",
3611
+ "marking message as failed",
1622
3612
  () => this.fetchImpl(
1623
3613
  `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
1624
3614
  {
1625
3615
  method: "PATCH",
1626
3616
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1627
- body: JSON.stringify({ status: "done", opencode_session_id: sessionId })
3617
+ body: JSON.stringify(body)
1628
3618
  }
1629
3619
  )
1630
3620
  );
1631
3621
  }
1632
- async markFailed(conversationId, messageId) {
1633
- await this.callWithRetry(
1634
- "marking message as failed",
1635
- () => this.fetchImpl(
1636
- `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
3622
+ /**
3623
+ * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
3624
+ * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
3625
+ * — the server records it via `log()` (no DB write, no notification). This is
3626
+ * fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
3627
+ * MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
3628
+ * watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
3629
+ * context (no silent catch, per development-workflow).
3630
+ *
3631
+ * Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
3632
+ * telemetry), but the `paused` liveness-clear uses it to know whether to
3633
+ * RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
3634
+ * stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
3635
+ * leaves liveness").
3636
+ */
3637
+ async postSignal(conversationId, messageId, signal, extra) {
3638
+ try {
3639
+ const res = await this.fetchImpl(
3640
+ `${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
1637
3641
  {
1638
- method: "PATCH",
3642
+ method: "POST",
1639
3643
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1640
- body: JSON.stringify({ status: "failed" })
3644
+ body: JSON.stringify({ signal, ...extra })
1641
3645
  }
1642
- )
1643
- );
3646
+ );
3647
+ if (!res.ok) {
3648
+ this.log({
3649
+ level: "error",
3650
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
3651
+ conversation_id: conversationId,
3652
+ message_id: messageId
3653
+ });
3654
+ return false;
3655
+ }
3656
+ return true;
3657
+ } catch (err) {
3658
+ this.log({
3659
+ level: "error",
3660
+ message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
3661
+ conversation_id: conversationId,
3662
+ message_id: messageId
3663
+ });
3664
+ return false;
3665
+ }
1644
3666
  }
1645
3667
  async persistSession(conversationId, sessionId) {
1646
3668
  const res = await this.fetchImpl(
@@ -1655,10 +3677,17 @@ var ChannelDriver = class {
1655
3677
  }
1656
3678
  /**
1657
3679
  * EXISTING combinedAuth interaction route (WI-CHAN-3) — idempotent + retried.
1658
- * `POST .../interactive-event {type, data}`. The server persists the
1659
- * interaction and posts a link to the proxied opencode-web conversation.
3680
+ * `POST .../interactive-event {type, data, source_message_id?}`. The server
3681
+ * persists the interaction and posts a link to the proxied opencode-web
3682
+ * conversation, @mentioning the user who triggered THIS message's turn.
3683
+ *
3684
+ * WI-3 / WI-4 contract: `source_message_id` is the PAUSED message's own Slack
3685
+ * ts (`message.source_message_id`). The server resolves the @mention from that
3686
+ * message's user FIRST (falling back to the old "newest processing" precedence
3687
+ * only when absent), so the correct person is mentioned under concurrency. It
3688
+ * is OPTIONAL for back-compat with older clients / legacy rows.
1660
3689
  */
1661
- async reportInteraction(conversationId, type, data) {
3690
+ async reportInteraction(conversationId, type, data, sourceMessageId) {
1662
3691
  try {
1663
3692
  await this.callWithRetry(
1664
3693
  "reporting interactive event",
@@ -1667,7 +3696,9 @@ var ChannelDriver = class {
1667
3696
  {
1668
3697
  method: "POST",
1669
3698
  headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
1670
- body: JSON.stringify({ type, data })
3699
+ body: JSON.stringify(
3700
+ sourceMessageId ? { type, data, source_message_id: sourceMessageId } : { type, data }
3701
+ )
1671
3702
  }
1672
3703
  )
1673
3704
  );
@@ -1676,6 +3707,7 @@ var ChannelDriver = class {
1676
3707
  message: `${type} surfaced to channel (id: ${data.id.slice(0, 8)})`,
1677
3708
  conversation_id: conversationId
1678
3709
  });
3710
+ return true;
1679
3711
  } catch (err) {
1680
3712
  if (err instanceof ChannelAuthError) throw err;
1681
3713
  this.log({
@@ -1683,6 +3715,7 @@ var ChannelDriver = class {
1683
3715
  message: `Failed to surface ${type}: ${err instanceof Error ? err.message : String(err)}`,
1684
3716
  conversation_id: conversationId
1685
3717
  });
3718
+ return false;
1686
3719
  }
1687
3720
  }
1688
3721
  // -------------------------------------------------------------------------
@@ -1721,8 +3754,9 @@ var ChannelDriver = class {
1721
3754
  await this.sleep(backoffDelay(attempt, this.retry));
1722
3755
  continue;
1723
3756
  }
3757
+ break;
1724
3758
  }
1725
- throw new Error(`${context}: HTTP ${res.status}`);
3759
+ throw new ChannelTerminalError(`${context}: HTTP ${res.status}`, res.status);
1726
3760
  }
1727
3761
  throw lastError instanceof Error ? lastError : new Error(`${context}: exhausted retries`);
1728
3762
  }
@@ -1848,23 +3882,45 @@ Port ${port} is already in use.`));
1848
3882
  spinner.fail("Failed to start OpenCode");
1849
3883
  throw new Error("OpenCode failed to start");
1850
3884
  }
1851
- spinner.succeed(
1852
- `OpenCode running on port ${port}${health.version ? ` (v${health.version})` : ""}`
1853
- );
3885
+ spinner.stop();
1854
3886
  return { port, process: proc, version: health.version ?? null };
1855
3887
  }
1856
3888
  return { port, process: null, version: null };
1857
3889
  }
1858
3890
 
1859
3891
  // src/commands/agent-lookup.ts
3892
+ async function readErrorMessage(response) {
3893
+ const text = await response.text().catch(() => "");
3894
+ if (!text) return response.statusText || void 0;
3895
+ try {
3896
+ const data = JSON.parse(text);
3897
+ const message = data.message ?? data.error;
3898
+ if (typeof message === "string" && message.trim()) {
3899
+ return message;
3900
+ }
3901
+ } catch {
3902
+ }
3903
+ return text.trim() || response.statusText || void 0;
3904
+ }
3905
+ function authFailureHint(apiUrl, serverMessage) {
3906
+ const reason = serverMessage ? `: ${serverMessage}` : "";
3907
+ return `Authentication failed${reason}. Your credentials were rejected by ${apiUrl}. This usually means you logged in against a different environment, or your session expired \u2014 log in again pointing at this endpoint and retry.`;
3908
+ }
1860
3909
  async function resolveAgentIdFromKey(authHeader) {
1861
3910
  const apiUrl = getApiUrlConfig();
1862
3911
  try {
1863
3912
  const response = await fetch(`${apiUrl}/me`, {
1864
3913
  headers: { Authorization: authHeader }
1865
3914
  });
3915
+ if (response.status === 401) {
3916
+ const serverMessage = await readErrorMessage(response);
3917
+ return { error: authFailureHint(apiUrl, serverMessage), authFailed: true };
3918
+ }
1866
3919
  if (!response.ok) {
1867
- return { error: `Failed to resolve agent from key: HTTP ${response.status}` };
3920
+ const serverMessage = await readErrorMessage(response);
3921
+ return {
3922
+ error: `Failed to resolve agent from key (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
3923
+ };
1868
3924
  }
1869
3925
  const data = await response.json();
1870
3926
  if (data.auth_type === "agent_key" && data.agent_id) {
@@ -1878,20 +3934,52 @@ async function resolveAgentIdFromKey(authHeader) {
1878
3934
  return { error: `Failed to resolve agent from key: ${message}` };
1879
3935
  }
1880
3936
  }
3937
+ async function notifyAgentDisconnected(agentId, authHeader) {
3938
+ const apiUrl = getApiUrlConfig();
3939
+ try {
3940
+ const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
3941
+ method: "POST",
3942
+ headers: { Authorization: authHeader }
3943
+ });
3944
+ if (!response.ok) {
3945
+ const serverMessage = await readErrorMessage(response);
3946
+ return {
3947
+ ok: false,
3948
+ error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
3949
+ };
3950
+ }
3951
+ return { ok: true };
3952
+ } catch (error2) {
3953
+ return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
3954
+ }
3955
+ }
1881
3956
  async function getAgentInfo(agentId, authHeader) {
1882
3957
  const apiUrl = getApiUrlConfig();
1883
3958
  try {
1884
3959
  const response = await fetch(`${apiUrl}/agents/${agentId}`, {
1885
3960
  headers: { Authorization: authHeader }
1886
3961
  });
1887
- if (response.status === 404) {
1888
- return { valid: false, error: "Agent not found" };
1889
- }
1890
3962
  if (response.status === 401) {
1891
- return { valid: false, error: "Authentication failed", authFailed: true };
3963
+ const serverMessage = await readErrorMessage(response);
3964
+ return { valid: false, error: authFailureHint(apiUrl, serverMessage), authFailed: true };
3965
+ }
3966
+ if (response.status === 403) {
3967
+ const serverMessage = await readErrorMessage(response);
3968
+ return {
3969
+ valid: false,
3970
+ error: serverMessage ?? "You do not have access to this agent (it may belong to a different team or organization)."
3971
+ };
3972
+ }
3973
+ if (response.status === 404) {
3974
+ const serverMessage = await readErrorMessage(response);
3975
+ return { valid: false, error: serverMessage ?? `Agent ${agentId} not found` };
1892
3976
  }
1893
3977
  if (!response.ok) {
1894
- return { valid: false, error: `API error: ${response.status}` };
3978
+ const serverMessage = await readErrorMessage(response);
3979
+ return {
3980
+ valid: false,
3981
+ error: `API error (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
3982
+ };
1895
3983
  }
1896
3984
  const agent = await response.json();
1897
3985
  if (agent.agent_type !== "local") {
@@ -1910,7 +3998,9 @@ async function getAgentInfo(agentId, authHeader) {
1910
3998
  // src/commands/run.ts
1911
3999
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
1912
4000
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
1913
- function log(state, message, isError = false) {
4001
+ var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
4002
+ var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
4003
+ function log2(state, message, isError = false) {
1914
4004
  if (state.json) {
1915
4005
  console.log(
1916
4006
  JSON.stringify({
@@ -1935,9 +4025,9 @@ function logActivity(state, entry) {
1935
4025
  }
1936
4026
  if (!state.interactive) {
1937
4027
  if (entry.type === "error") {
1938
- log(state, entry.error ?? "Unknown error", true);
4028
+ log2(state, entry.error ?? "Unknown error", true);
1939
4029
  } else if (entry.type === "info" && entry.message) {
1940
- log(state, entry.message);
4030
+ log2(state, entry.message);
1941
4031
  }
1942
4032
  }
1943
4033
  }
@@ -2023,6 +4113,7 @@ async function handleAuthError(state, error2) {
2023
4113
  }
2024
4114
  async function driveChannels(state, driver) {
2025
4115
  let idlePolls = 0;
4116
+ let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
2026
4117
  while (state.running) {
2027
4118
  if (state.connection?.reconnecting && state.connection.reconnectPromise) {
2028
4119
  logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
@@ -2032,9 +4123,11 @@ async function driveChannels(state, driver) {
2032
4123
  try {
2033
4124
  const processed = await driver.drainPending();
2034
4125
  state.messageCount += processed;
2035
- if (processed > 0) {
4126
+ const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
4127
+ lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
4128
+ if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
2036
4129
  idlePolls = 0;
2037
- if (state.interactive) displayStatus(state);
4130
+ if (processed > 0 && state.interactive) displayStatus(state);
2038
4131
  } else if (state.idleTimeout !== null) {
2039
4132
  idlePolls++;
2040
4133
  if (idlePolls === 1) {
@@ -2061,7 +4154,7 @@ async function driveChannels(state, driver) {
2061
4154
  logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
2062
4155
  if (state.interactive) displayStatus(state);
2063
4156
  }
2064
- await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
4157
+ await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
2065
4158
  if (state.idleTimeout !== null && idlePolls >= 2) {
2066
4159
  const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
2067
4160
  if (idleMs > state.idleTimeout * 1e3) {
@@ -2072,8 +4165,122 @@ async function driveChannels(state, driver) {
2072
4165
  }
2073
4166
  }
2074
4167
  }
2075
- async function cleanup(state) {
4168
+ var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
4169
+ async function runSweep(state, driver, config2) {
4170
+ const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
4171
+ try {
4172
+ const sessions = await listSessions(state.port);
4173
+ if (sessions === null) {
4174
+ logActivity(state, {
4175
+ type: "info",
4176
+ message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
4177
+ });
4178
+ return;
4179
+ }
4180
+ const toDelete = selectSessionsToDelete(
4181
+ sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
4182
+ {
4183
+ maxAgeMs: config2.maxAgeMs,
4184
+ maxCount: config2.maxCount,
4185
+ nowMs: Date.now(),
4186
+ protectedIds: driver.protectedSessionIds()
4187
+ }
4188
+ );
4189
+ const protectedNow = driver.protectedSessionIds();
4190
+ let deleted = 0;
4191
+ let failed = 0;
4192
+ let skippedNewlyActive = 0;
4193
+ for (const id of toDelete) {
4194
+ if (protectedNow.has(id)) {
4195
+ skippedNewlyActive++;
4196
+ logActivity(state, {
4197
+ type: "info",
4198
+ message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
4199
+ });
4200
+ continue;
4201
+ }
4202
+ if (await deleteSession(state.port, id)) deleted++;
4203
+ else failed++;
4204
+ }
4205
+ const failedNote = failed > 0 ? `, failed ${failed}` : "";
4206
+ const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
4207
+ logActivity(state, {
4208
+ type: "info",
4209
+ message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
4210
+ });
4211
+ } catch (error2) {
4212
+ const message = error2 instanceof Error ? error2.message : String(error2);
4213
+ logActivity(state, {
4214
+ type: "error",
4215
+ error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
4216
+ });
4217
+ }
4218
+ }
4219
+ function scheduleSessionCleanup(state, driver, options) {
4220
+ const config2 = resolveSessionCleanupConfig(
4221
+ {
4222
+ maxAge: options.sessionCleanupMaxAge,
4223
+ maxCount: options.sessionCleanupMaxCount,
4224
+ interval: options.sessionCleanupInterval
4225
+ },
4226
+ process.env
4227
+ );
4228
+ for (const warning2 of config2.warnings) {
4229
+ logActivity(state, { type: "info", message: `Session cleanup: ${warning2}` });
4230
+ }
4231
+ if (!config2.enabled) return;
4232
+ logActivity(state, {
4233
+ type: "info",
4234
+ message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
4235
+ });
4236
+ const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
4237
+ const firstSweep = setTimeout(
4238
+ () => void runSweep(state, driver, config2),
4239
+ SESSION_CLEANUP_FIRST_SWEEP_MS
4240
+ );
4241
+ state.sessionCleanupTimers.push(interval, firstSweep);
4242
+ }
4243
+ async function notifyOffline(state) {
4244
+ if (!state.agentId || !state.authHeader) return;
4245
+ if (!state.connected) {
4246
+ log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
4247
+ return;
4248
+ }
4249
+ const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
4250
+ if (result.ok) {
4251
+ log2(state, "Notified Evident the agent is going offline");
4252
+ } else {
4253
+ logActivity(state, {
4254
+ type: "error",
4255
+ error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
4256
+ });
4257
+ if (state.interactive) displayStatus(state);
4258
+ }
4259
+ }
4260
+ async function cleanup(state, opts = {}) {
2076
4261
  state.running = false;
4262
+ for (const timer of state.sessionCleanupTimers) {
4263
+ clearInterval(timer);
4264
+ clearTimeout(timer);
4265
+ }
4266
+ state.sessionCleanupTimers = [];
4267
+ if (opts.graceful && state.channelDriver) {
4268
+ state.channelDriver.stop();
4269
+ log2(state, "Draining in-flight channel work before shutdown...");
4270
+ if (state.interactive) {
4271
+ logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
4272
+ displayStatus(state);
4273
+ }
4274
+ const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
4275
+ if (!settled) {
4276
+ logActivity(state, {
4277
+ type: "info",
4278
+ message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
4279
+ });
4280
+ if (state.interactive) displayStatus(state);
4281
+ }
4282
+ }
4283
+ await notifyOffline(state);
2077
4284
  if (state.connection) {
2078
4285
  state.connection.close();
2079
4286
  state.connection = null;
@@ -2084,7 +4291,7 @@ async function cleanup(state) {
2084
4291
  logActivity(state, { type: "info", message: "Stopped OpenCode process" });
2085
4292
  displayStatus(state);
2086
4293
  } else {
2087
- log(state, "Stopped OpenCode process");
4294
+ log2(state, "Stopped OpenCode process");
2088
4295
  }
2089
4296
  state.opencodeProcess = null;
2090
4297
  }
@@ -2104,26 +4311,32 @@ async function run(options) {
2104
4311
  opencodeVersion: null,
2105
4312
  opencodeProcess: null,
2106
4313
  connection: null,
4314
+ channelDriver: null,
2107
4315
  running: true,
4316
+ shuttingDown: false,
2108
4317
  activityLog: [],
2109
4318
  messageCount: 0,
4319
+ lastProxiedActivityAt: null,
4320
+ sessionCleanupTimers: [],
2110
4321
  authHeader: ""
2111
4322
  };
2112
4323
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
2113
- log(
4324
+ log2(
2114
4325
  state,
2115
4326
  "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.",
2116
4327
  false
2117
4328
  );
2118
4329
  }
2119
4330
  const handleSignal = async () => {
4331
+ if (state.shuttingDown) return;
4332
+ state.shuttingDown = true;
2120
4333
  if (state.interactive) {
2121
4334
  logActivity(state, { type: "info", message: "Shutting down..." });
2122
4335
  displayStatus(state);
2123
4336
  } else {
2124
- log(state, "Shutting down...");
4337
+ log2(state, "Shutting down...");
2125
4338
  }
2126
- await cleanup(state);
4339
+ await cleanup(state, { graceful: true });
2127
4340
  await shutdownTelemetry();
2128
4341
  process.exit(0);
2129
4342
  };
@@ -2154,7 +4367,7 @@ async function run(options) {
2154
4367
  const resolved = await resolveAgentIdFromKey(state.authHeader);
2155
4368
  if (resolved.agent_id) {
2156
4369
  state.agentId = resolved.agent_id;
2157
- log(state, `Resolved agent ID from key: ${state.agentId}`);
4370
+ log2(state, `Resolved agent ID from key: ${state.agentId}`);
2158
4371
  if (state.interactive && !state.json) {
2159
4372
  logActivity(state, {
2160
4373
  type: "info",
@@ -2217,14 +4430,21 @@ async function run(options) {
2217
4430
  port: state.port,
2218
4431
  interactive: state.interactive,
2219
4432
  agentId: state.agentId,
2220
- log: (message) => log(state, message)
4433
+ log: (message) => log2(state, message)
2221
4434
  });
2222
4435
  state.port = oc.port;
2223
4436
  state.opencodeProcess = oc.process;
2224
4437
  state.opencodeVersion = oc.version;
2225
4438
  state.opencodeConnected = oc.process !== null || oc.version !== null;
2226
- const version = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
2227
- ocSpinner?.succeed(`OpenCode running on port ${state.port}${version}`);
4439
+ const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
4440
+ ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
4441
+ const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
4442
+ if (versionWarning) {
4443
+ log2(state, versionWarning, false);
4444
+ if (state.interactive && !state.json) {
4445
+ logActivity(state, { type: "info", message: versionWarning });
4446
+ }
4447
+ }
2228
4448
  } catch (error2) {
2229
4449
  ocSpinner?.fail(error2.message);
2230
4450
  throw error2;
@@ -2236,12 +4456,14 @@ async function run(options) {
2236
4456
  apiUrl: getApiUrlConfig(),
2237
4457
  getAuthHeader: () => state.authHeader,
2238
4458
  conversationFilter: state.conversationFilter,
4459
+ stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
2239
4460
  log: (entry) => logActivity(state, {
2240
4461
  type: entry.level === "error" ? "error" : "info",
2241
4462
  message: entry.message,
2242
4463
  error: entry.level === "error" ? entry.message : void 0
2243
4464
  })
2244
4465
  });
4466
+ state.channelDriver = channelDriver;
2245
4467
  const connection = new RunnerConnection({
2246
4468
  agentId: state.agentId,
2247
4469
  getAuthHeader: () => state.authHeader,
@@ -2255,10 +4477,29 @@ async function run(options) {
2255
4477
  type: "info",
2256
4478
  message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
2257
4479
  });
2258
- emitAgentConnected(state.agentId, { port: state.port });
4480
+ emitAgentConnected(state.agentId, {
4481
+ port: state.port,
4482
+ cli_version: getCliVersion(),
4483
+ opencode_version: state.opencodeVersion
4484
+ });
2259
4485
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
2260
4486
  if (state.interactive) displayStatus(state);
2261
- channelDriver.drainPending().catch(() => {
4487
+ channelDriver.drainPending().then((processed) => {
4488
+ if (processed > 0) {
4489
+ state.messageCount += processed;
4490
+ logActivity(state, {
4491
+ type: "info",
4492
+ message: `Drained ${processed} queued message(s) on connect`
4493
+ });
4494
+ if (state.interactive) displayStatus(state);
4495
+ }
4496
+ }).catch((error2) => {
4497
+ const message = error2 instanceof Error ? error2.message : String(error2);
4498
+ logActivity(state, {
4499
+ type: "error",
4500
+ error: `Failed to drain queued messages on connect: ${message}`
4501
+ });
4502
+ if (state.interactive) displayStatus(state);
2262
4503
  });
2263
4504
  },
2264
4505
  onDisconnected: (code, reason) => {
@@ -2274,9 +4515,40 @@ async function run(options) {
2274
4515
  logActivity(state, { type: "error", error: error2 });
2275
4516
  if (state.interactive) displayStatus(state);
2276
4517
  },
2277
- // Web traffic is proxied transparently; only note opencode is live.
4518
+ // Web traffic is proxied transparently; note opencode is live and stamp
4519
+ // proxied activity so the idle loop treats interactive proxy use as work.
4520
+ // Fires per forwarded response head (incl. every SSE open) and excludes
4521
+ // the internal drain-ping, so an actively-used proxy keeps the timer
4522
+ // fresh while a lone idle SSE with no follow-up requests still ages out.
2278
4523
  onResponse: () => {
2279
4524
  state.opencodeConnected = true;
4525
+ state.lastProxiedActivityAt = Date.now();
4526
+ },
4527
+ // A channel message was queued and the api-worker pinged us over the
4528
+ // tunnel to drain immediately instead of waiting for the next poll tick.
4529
+ // Best-effort + non-fatal: mirror the on-connect drain block. A failed
4530
+ // drain here is logged and swallowed — the steady-state poll retries, so
4531
+ // a lost/failed ping can never orphan a message (§2 invariant).
4532
+ onDrainPing: () => {
4533
+ if (!state.running) return;
4534
+ logActivity(state, { type: "info", message: "Drain ping received \u2014 draining" });
4535
+ channelDriver.drainPending().then((processed) => {
4536
+ if (processed > 0) {
4537
+ state.messageCount += processed;
4538
+ logActivity(state, {
4539
+ type: "info",
4540
+ message: `Drained ${processed} queued message(s) on ping`
4541
+ });
4542
+ if (state.interactive) displayStatus(state);
4543
+ }
4544
+ }).catch((error2) => {
4545
+ const message = error2 instanceof Error ? error2.message : String(error2);
4546
+ logActivity(state, {
4547
+ type: "error",
4548
+ error: `Failed to drain queued messages on ping: ${message}`
4549
+ });
4550
+ if (state.interactive) displayStatus(state);
4551
+ });
2280
4552
  },
2281
4553
  onInfo: (message) => logActivity(state, { type: "info", message })
2282
4554
  }
@@ -2288,12 +4560,12 @@ async function run(options) {
2288
4560
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
2289
4561
  throw error2;
2290
4562
  }
2291
- if (interactive && !state.json) {
2292
- displayStatus(state);
2293
- } else {
2294
- log(state, "Driving channel messages...");
4563
+ scheduleSessionCleanup(state, channelDriver, options);
4564
+ if (!interactive || state.json) {
4565
+ log2(state, "Driving channel messages...");
2295
4566
  }
2296
4567
  await driveChannels(state, channelDriver);
4568
+ if (state.shuttingDown) return;
2297
4569
  await cleanup(state);
2298
4570
  if (state.json) {
2299
4571
  console.log(
@@ -2303,11 +4575,12 @@ async function run(options) {
2303
4575
  })
2304
4576
  );
2305
4577
  } else if (!interactive) {
2306
- log(state, `Completed. Processed ${state.messageCount} message(s).`);
4578
+ log2(state, `Completed. Processed ${state.messageCount} message(s).`);
2307
4579
  }
2308
4580
  await shutdownTelemetry();
2309
4581
  process.exit(0);
2310
4582
  } catch (error2) {
4583
+ if (state.shuttingDown) return;
2311
4584
  await cleanup(state);
2312
4585
  const message = error2 instanceof Error ? error2.message : String(error2);
2313
4586
  if (state.json) {
@@ -2325,8 +4598,9 @@ async function run(options) {
2325
4598
  }
2326
4599
 
2327
4600
  // src/index.ts
4601
+ var { version } = createRequire(import.meta.url)("../package.json");
2328
4602
  var program = new Command();
2329
- program.name("evident").description("Run OpenCode locally and connect it to Evident").version("0.1.0").option(
4603
+ program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
2330
4604
  "--endpoint <url>",
2331
4605
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
2332
4606
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
@@ -2339,9 +4613,18 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
2339
4613
  }
2340
4614
  });
2341
4615
  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);
2342
- program.command("logout").description("Remove stored credentials").action(logout);
4616
+ 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 }));
2343
4617
  program.command("whoami").description("Show the currently logged in user").action(whoami);
2344
- 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(
4618
+ program.command("run").description("Connect to Evident and process messages").option("-a, --agent [id]", "Agent ID to connect to (optional when EVIDENT_AGENT_KEY is set)").option("-p, --port <port>", "OpenCode port (default: 4096)", "4096").option("-v, --verbose", "Show detailed request/response information").option("-c, --conversation <id>", "Process only this specific conversation").option("--idle-timeout <seconds>", "Exit after N seconds idle").option("--json", "Output in JSON format").option(
4619
+ "--session-cleanup-max-age <duration>",
4620
+ "Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
4621
+ ).option(
4622
+ "--session-cleanup-max-count <n>",
4623
+ "Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
4624
+ ).option(
4625
+ "--session-cleanup-interval <duration>",
4626
+ "How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
4627
+ ).action(
2345
4628
  (options) => {
2346
4629
  run({
2347
4630
  agent: options.agent,
@@ -2349,7 +4632,11 @@ program.command("run").description("Connect to Evident and process messages").op
2349
4632
  verbose: options.verbose,
2350
4633
  conversation: options.conversation,
2351
4634
  idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
2352
- json: options.json
4635
+ json: options.json,
4636
+ // Raw strings — the resolver in run.ts single-sources parsing (M1).
4637
+ sessionCleanupMaxAge: options.sessionCleanupMaxAge,
4638
+ sessionCleanupMaxCount: options.sessionCleanupMaxCount,
4639
+ sessionCleanupInterval: options.sessionCleanupInterval
2353
4640
  });
2354
4641
  }
2355
4642
  );