@evident-ai/cli 3.0.1-dev.10c527f → 3.0.1-dev.1d33bd1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -285,14 +285,14 @@ function blank() {
285
285
  console.log();
286
286
  }
287
287
  function waitForEnter(prompt = "Press Enter to continue...") {
288
- return new Promise((resolve2) => {
288
+ return new Promise((resolve) => {
289
289
  process.stdout.write(chalk.dim(prompt));
290
290
  const handler = () => {
291
291
  process.stdin.removeListener("data", handler);
292
292
  process.stdin.setRawMode?.(false);
293
293
  process.stdin.pause();
294
294
  console.log();
295
- resolve2();
295
+ resolve();
296
296
  };
297
297
  if (process.stdin.isTTY) {
298
298
  process.stdin.setRawMode?.(true);
@@ -302,7 +302,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
302
302
  });
303
303
  }
304
304
  function sleep(ms) {
305
- return new Promise((resolve2) => setTimeout(resolve2, ms));
305
+ return new Promise((resolve) => setTimeout(resolve, ms));
306
306
  }
307
307
 
308
308
  // src/commands/login.ts
@@ -376,19 +376,19 @@ async function tokenLogin() {
376
376
  console.log("Visit your Evident dashboard to generate a CLI token.");
377
377
  blank();
378
378
  process.stdout.write("Paste token: ");
379
- const token = await new Promise((resolve2) => {
379
+ const token = await new Promise((resolve) => {
380
380
  let data = "";
381
381
  process.stdin.setEncoding("utf8");
382
382
  process.stdin.on("data", (chunk) => {
383
383
  data += chunk;
384
384
  });
385
385
  process.stdin.on("end", () => {
386
- resolve2(data.trim());
386
+ resolve(data.trim());
387
387
  });
388
388
  if (process.stdin.isTTY) {
389
389
  process.stdin.once("data", (chunk) => {
390
390
  process.stdin.pause();
391
- resolve2(chunk.toString().trim());
391
+ resolve(chunk.toString().trim());
392
392
  });
393
393
  process.stdin.resume();
394
394
  }
@@ -706,7 +706,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
706
706
  if (health.healthy) {
707
707
  return health;
708
708
  }
709
- await new Promise((resolve2) => setTimeout(resolve2, 1e3));
709
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
710
710
  }
711
711
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
712
712
  }
@@ -1078,84 +1078,6 @@ async function getSessionMessages(port, sessionId) {
1078
1078
  return null;
1079
1079
  }
1080
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
1081
  async function createOpenCodeSession(port, directory) {
1160
1082
  const url = new URL(`${opencodeBase(port)}/session`);
1161
1083
  if (directory && directory.trim()) {
@@ -1225,7 +1147,7 @@ async function sendPromptAsync(port, sessionId, content, options) {
1225
1147
  if (best) return best.id;
1226
1148
  }
1227
1149
  if (attempt < READ_BACK_ATTEMPTS - 1) {
1228
- await new Promise((resolve2) => setTimeout(resolve2, READ_BACK_DELAY_MS));
1150
+ await new Promise((resolve) => setTimeout(resolve, READ_BACK_DELAY_MS));
1229
1151
  }
1230
1152
  }
1231
1153
  return null;
@@ -1282,11 +1204,6 @@ function messageRunState(messages, userMessageId) {
1282
1204
  if (isAssistantInFlight(reply)) return "running";
1283
1205
  return errorOf(reply) != null ? "failed" : "done";
1284
1206
  }
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
1207
  function messageError(messages, userMessageId) {
1291
1208
  const reply = findLastAssistantReplyFor(messages, userMessageId);
1292
1209
  const error2 = errorOf(reply);
@@ -1307,110 +1224,6 @@ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
1307
1224
  );
1308
1225
  }
1309
1226
 
1310
- // src/lib/opencode/session-cleanup.ts
1311
- var DURATION_UNIT_MS = {
1312
- s: 1e3,
1313
- m: 60 * 1e3,
1314
- h: 60 * 60 * 1e3,
1315
- d: 24 * 60 * 60 * 1e3
1316
- };
1317
- function parseDurationMs(input) {
1318
- const trimmed = input.trim();
1319
- const match = /^(\d+)([smhd])$/.exec(trimmed);
1320
- if (!match) {
1321
- throw new Error(
1322
- `Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
1323
- );
1324
- }
1325
- const value = Number(match[1]);
1326
- if (value <= 0) {
1327
- throw new Error(`Invalid duration "${input}": must be a positive value.`);
1328
- }
1329
- return value * DURATION_UNIT_MS[match[2]];
1330
- }
1331
- function selectSessionsToDelete(sessions, opts) {
1332
- const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
1333
- if (maxAgeMs === void 0 && maxCount === void 0) return [];
1334
- const ageEligible = (s) => {
1335
- if (maxAgeMs === void 0) return false;
1336
- if (s.lastActivityMs === null) return true;
1337
- return nowMs - s.lastActivityMs > maxAgeMs;
1338
- };
1339
- const countEligibleIds = /* @__PURE__ */ new Set();
1340
- if (maxCount !== void 0) {
1341
- const byActivityDesc = [...sessions].sort(
1342
- (a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
1343
- );
1344
- for (const s of byActivityDesc.slice(maxCount)) {
1345
- countEligibleIds.add(s.id);
1346
- }
1347
- }
1348
- const toDelete = [];
1349
- for (const s of sessions) {
1350
- if (protectedIds.has(s.id)) continue;
1351
- if (ageEligible(s) || countEligibleIds.has(s.id)) {
1352
- toDelete.push(s.id);
1353
- }
1354
- }
1355
- return toDelete;
1356
- }
1357
- var DEFAULT_INTERVAL = "1h";
1358
- function resolve(flag, envValue, fallback) {
1359
- return flag ?? envValue ?? fallback;
1360
- }
1361
- function parseMaxCount(input) {
1362
- const trimmed = input.trim();
1363
- if (!/^\d+$/.test(trimmed)) {
1364
- throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
1365
- }
1366
- const value = Number(trimmed);
1367
- if (value <= 0) {
1368
- throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
1369
- }
1370
- return value;
1371
- }
1372
- function resolveSessionCleanupConfig(flags, env = process.env) {
1373
- const warnings = [];
1374
- const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
1375
- const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
1376
- const intervalRaw = resolve(
1377
- flags.interval,
1378
- env.EVIDENT_SESSION_CLEANUP_INTERVAL,
1379
- DEFAULT_INTERVAL
1380
- );
1381
- let maxAgeMs;
1382
- if (maxAgeRaw !== void 0) {
1383
- try {
1384
- maxAgeMs = parseDurationMs(maxAgeRaw);
1385
- } catch (err) {
1386
- warnings.push(
1387
- `Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
1388
- );
1389
- }
1390
- }
1391
- let maxCount;
1392
- if (maxCountRaw !== void 0) {
1393
- try {
1394
- maxCount = parseMaxCount(maxCountRaw);
1395
- } catch (err) {
1396
- warnings.push(
1397
- `Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
1398
- );
1399
- }
1400
- }
1401
- let intervalMs;
1402
- try {
1403
- intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
1404
- } catch (err) {
1405
- warnings.push(
1406
- `Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
1407
- );
1408
- intervalMs = parseDurationMs(DEFAULT_INTERVAL);
1409
- }
1410
- const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
1411
- return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
1412
- }
1413
-
1414
1227
  // src/lib/tunnel/connection.ts
1415
1228
  import WebSocket2 from "ws";
1416
1229
 
@@ -1501,12 +1314,12 @@ var StreamForwarder = class {
1501
1314
  let endBody;
1502
1315
  if (has_body) {
1503
1316
  const chunks = [];
1504
- bodyPromise = new Promise((resolve2) => {
1317
+ bodyPromise = new Promise((resolve) => {
1505
1318
  pushBody = (buf) => {
1506
1319
  chunks.push(buf);
1507
1320
  };
1508
1321
  endBody = () => {
1509
- resolve2(Buffer.concat(chunks));
1322
+ resolve(Buffer.concat(chunks));
1510
1323
  };
1511
1324
  });
1512
1325
  }
@@ -1624,7 +1437,7 @@ function connectTunnel(options) {
1624
1437
  } = options;
1625
1438
  const tunnelUrl = getTunnelUrlConfig();
1626
1439
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
1627
- return new Promise((resolve2, reject) => {
1440
+ return new Promise((resolve, reject) => {
1628
1441
  const ws = new WebSocket2(url, {
1629
1442
  headers: {
1630
1443
  Authorization: authHeader
@@ -1689,7 +1502,7 @@ function connectTunnel(options) {
1689
1502
  clearTimeout(connectionTimeout);
1690
1503
  const connectedAgentId = message.agent_id ?? agentId;
1691
1504
  onConnected?.(connectedAgentId);
1692
- resolve2({
1505
+ resolve({
1693
1506
  ws,
1694
1507
  close: () => ws.close(1e3, "CLI shutdown")
1695
1508
  });
@@ -1819,9 +1632,6 @@ var DEFAULT_RETRY_POLICY = {
1819
1632
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
1820
1633
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
1821
1634
  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;
1825
1635
  var ChannelAuthError = class extends Error {
1826
1636
  constructor(message) {
1827
1637
  super(message);
@@ -1918,15 +1728,6 @@ var ChannelDriver = class {
1918
1728
  * the row leaves the processing list, exactly like `dontRedispatch`.
1919
1729
  */
1920
1730
  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
1731
  /**
1931
1732
  * "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
1932
1733
  * Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
@@ -2053,28 +1854,6 @@ var ChannelDriver = class {
2053
1854
  }
2054
1855
  return false;
2055
1856
  }
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
1857
  /**
2079
1858
  * Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
2080
1859
  * this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
@@ -2180,16 +1959,6 @@ var ChannelDriver = class {
2180
1959
  } catch (err) {
2181
1960
  if (err instanceof ChannelAuthError) throw err;
2182
1961
  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
- }
2193
1962
  await this.markFailed(conv.id, message.id).catch(() => {
2194
1963
  });
2195
1964
  this.log({
@@ -2225,33 +1994,16 @@ var ChannelDriver = class {
2225
1994
  return dispatched;
2226
1995
  }
2227
1996
  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;
1997
+ const cached = this.sessions.get(conv.id);
1998
+ if (cached) return cached;
1999
+ if (conv.opencode_session_id) {
2000
+ this.sessions.set(conv.id, conv.opencode_session_id);
2001
+ return conv.opencode_session_id;
2242
2002
  }
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
2003
  const directory = await this.resolveOpenCodeDirectory();
2252
2004
  const sessionId = await createOpenCodeSession(this.port, directory);
2253
- this.sessions.set(conversationId, sessionId);
2254
- await this.persistSession(conversationId, sessionId).catch(() => {
2005
+ this.sessions.set(conv.id, sessionId);
2006
+ await this.persistSession(conv.id, sessionId).catch(() => {
2255
2007
  });
2256
2008
  return sessionId;
2257
2009
  }
@@ -2302,9 +2054,7 @@ var ChannelDriver = class {
2302
2054
  inFlight: /* @__PURE__ */ new Map(),
2303
2055
  loop: null,
2304
2056
  reportedQuestions: /* @__PURE__ */ new Set(),
2305
- reportedPermissions: /* @__PURE__ */ new Set(),
2306
- lastGoodPollAt: this.now(),
2307
- hadUsablePoll: false
2057
+ reportedPermissions: /* @__PURE__ */ new Set()
2308
2058
  };
2309
2059
  this.watchers.set(sessionId, watcher);
2310
2060
  }
@@ -2314,37 +2064,20 @@ var ChannelDriver = class {
2314
2064
  opencodeMessageId,
2315
2065
  message,
2316
2066
  dispatchedAt: now,
2317
- processingAnchorMs: now,
2318
2067
  deadline: now + this.pausedMaxWaitMs,
2319
2068
  started: false,
2320
2069
  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
2070
+ stuckReported: false
2330
2071
  });
2331
2072
  }
2332
2073
  /**
2333
2074
  * Register a RE-ADOPTED `processing` message with its session watcher
2334
2075
  * (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
2335
2076
  * `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).
2077
+ * `now`: a row already `processing` for e.g. 5 min must give up ~5 min from now
2078
+ * (10 min after `processed_at`), not 10 min from now — otherwise its deadline
2079
+ * lands ~15 min after `processed_at`, coinciding with the cron reset →
2080
+ * double-drive race. `dispatchedAt` stays `now` (only the appear-guard uses it).
2348
2081
  *
2349
2082
  * `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
2350
2083
  * `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
@@ -2362,9 +2095,7 @@ var ChannelDriver = class {
2362
2095
  inFlight: /* @__PURE__ */ new Map(),
2363
2096
  loop: null,
2364
2097
  reportedQuestions: /* @__PURE__ */ new Set(),
2365
- reportedPermissions: /* @__PURE__ */ new Set(),
2366
- lastGoodPollAt: this.now(),
2367
- hadUsablePoll: false
2098
+ reportedPermissions: /* @__PURE__ */ new Set()
2368
2099
  };
2369
2100
  this.watchers.set(sessionId, watcher);
2370
2101
  }
@@ -2373,10 +2104,6 @@ var ChannelDriver = class {
2373
2104
  opencodeMessageId,
2374
2105
  message,
2375
2106
  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
2107
  deadline: processedAtMs + this.pausedMaxWaitMs,
2381
2108
  // The server row is ALREADY `processing`; do not re-fire markProcessing.
2382
2109
  started: true,
@@ -2386,20 +2113,7 @@ var ChannelDriver = class {
2386
2113
  // on `state === 'queued'` (turn produced no reply), not on `started`, so a
2387
2114
  // re-adopted row left wedged in `queued` still emits the signal once
2388
2115
  // (#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
2116
+ stuckReported: false
2403
2117
  });
2404
2118
  }
2405
2119
  /**
@@ -2450,30 +2164,12 @@ var ChannelDriver = class {
2450
2164
  messages = Array.isArray(body) ? body : null;
2451
2165
  }
2452
2166
  } catch {
2167
+ continue;
2453
2168
  }
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
2169
  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
- );
2170
+ await this.serviceInFlightMessage(sessionId, watcher, inFlight, messages);
2476
2171
  }
2172
+ await this.pollInteractions(sessionId, watcher, messages);
2477
2173
  }
2478
2174
  } catch (err) {
2479
2175
  if (err instanceof ChannelAuthError) {
@@ -2495,40 +2191,15 @@ var ChannelDriver = class {
2495
2191
  });
2496
2192
  }
2497
2193
  }
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
2194
  /**
2516
2195
  * Drive ONE in-flight message's lifecycle from the tick's message snapshot.
2517
2196
  * Fires markProcessing on queued→running and markDone on done (each once),
2518
2197
  * applies the idle-path re-dispatch guard, and removes the message from the
2519
2198
  * in-flight set on completion or timeout.
2520
2199
  */
2521
- async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
2200
+ async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
2522
2201
  const conv = watcher.conv;
2523
2202
  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
2203
  if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
2533
2204
  let claimed;
2534
2205
  try {
@@ -2559,7 +2230,6 @@ var ChannelDriver = class {
2559
2230
  }
2560
2231
  }
2561
2232
  if (state === "done") {
2562
- this.anchorDeliveryDeadline(inFlight);
2563
2233
  if (!inFlight.done) {
2564
2234
  this.log({
2565
2235
  level: "info",
@@ -2610,7 +2280,6 @@ var ChannelDriver = class {
2610
2280
  return;
2611
2281
  }
2612
2282
  if (state === "failed") {
2613
- this.anchorDeliveryDeadline(inFlight);
2614
2283
  if (!inFlight.done) {
2615
2284
  const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
2616
2285
  this.log({
@@ -2664,51 +2333,7 @@ var ChannelDriver = class {
2664
2333
  stuck_for_ms: this.now() - inFlight.dispatchedAt
2665
2334
  });
2666
2335
  }
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) {
2336
+ if (this.now() >= inFlight.deadline) {
2712
2337
  this.log({
2713
2338
  level: "info",
2714
2339
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
@@ -2740,17 +2365,12 @@ var ChannelDriver = class {
2740
2365
  */
2741
2366
  async readoptProcessing() {
2742
2367
  const rows = await this.getProcessingMessages();
2743
- if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0 || this.readoptPollUnresolvedSignalled.size > 0) {
2368
+ if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
2744
2369
  const stillProcessing = new Set(rows.map((r) => r.id));
2745
- for (const id of [
2746
- ...this.dontRedispatch,
2747
- ...this.doneUndeliverable,
2748
- ...this.readoptPollUnresolvedSignalled
2749
- ]) {
2370
+ for (const id of [...this.dontRedispatch, ...this.doneUndeliverable]) {
2750
2371
  if (!stillProcessing.has(id)) {
2751
2372
  const cleared = this.dontRedispatch.delete(id);
2752
2373
  const clearedUndeliverable = this.doneUndeliverable.delete(id);
2753
- this.readoptPollUnresolvedSignalled.delete(id);
2754
2374
  if (cleared || clearedUndeliverable) {
2755
2375
  this.log({
2756
2376
  level: "info",
@@ -2804,10 +2424,8 @@ var ChannelDriver = class {
2804
2424
  });
2805
2425
  continue;
2806
2426
  }
2807
- const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
2808
- const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
2809
2427
  for (const row of sessionRows) {
2810
- await this.readoptOne(sessionId, row, messages, sessionOngoing);
2428
+ await this.readoptOne(sessionId, row, messages);
2811
2429
  }
2812
2430
  }
2813
2431
  }
@@ -2829,7 +2447,7 @@ var ChannelDriver = class {
2829
2447
  *
2830
2448
  * Only `ChannelAuthError` propagates.
2831
2449
  */
2832
- async readoptOne(sessionId, row, messages, sessionOngoing) {
2450
+ async readoptOne(sessionId, row, messages) {
2833
2451
  if (this.isTracked(sessionId, row.id)) {
2834
2452
  this.log({
2835
2453
  level: "info",
@@ -2869,7 +2487,6 @@ var ChannelDriver = class {
2869
2487
  conversation_id: row.conversation_id,
2870
2488
  message_id: row.id
2871
2489
  });
2872
- void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
2873
2490
  return;
2874
2491
  }
2875
2492
  this.log({
@@ -2881,7 +2498,6 @@ var ChannelDriver = class {
2881
2498
  return;
2882
2499
  }
2883
2500
  this.dontRedispatch.delete(row.id);
2884
- void this.postSignal(row.conversation_id, row.id, "readopt_done");
2885
2501
  return;
2886
2502
  }
2887
2503
  if (state === "failed") {
@@ -2904,7 +2520,6 @@ var ChannelDriver = class {
2904
2520
  conversation_id: row.conversation_id,
2905
2521
  message_id: row.id
2906
2522
  });
2907
- void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
2908
2523
  return;
2909
2524
  }
2910
2525
  this.log({
@@ -2916,7 +2531,6 @@ var ChannelDriver = class {
2916
2531
  return;
2917
2532
  }
2918
2533
  this.dontRedispatch.delete(row.id);
2919
- void this.postSignal(row.conversation_id, row.id, "readopt_failed");
2920
2534
  return;
2921
2535
  }
2922
2536
  if (this.dontRedispatch.has(row.id)) {
@@ -2928,71 +2542,6 @@ var ChannelDriver = class {
2928
2542
  });
2929
2543
  return;
2930
2544
  }
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
2545
  if ((state === "running" || state === "queued") && ocId) {
2997
2546
  const conv = this.convForRow(sessionId, row);
2998
2547
  const message = this.queuedMessageForRow(row);
@@ -3006,7 +2555,6 @@ var ChannelDriver = class {
3006
2555
  conversation_id: row.conversation_id,
3007
2556
  message_id: row.id
3008
2557
  });
3009
- void this.postSignal(row.conversation_id, row.id, "readopt_reattached");
3010
2558
  return;
3011
2559
  }
3012
2560
  await this.forceReadoptRun(sessionId, row);
@@ -3059,7 +2607,6 @@ var ChannelDriver = class {
3059
2607
  conversation_id: row.conversation_id,
3060
2608
  message_id: row.id
3061
2609
  });
3062
- void this.postSignal(row.conversation_id, row.id, "readopt_window_elapsed");
3063
2610
  return;
3064
2611
  }
3065
2612
  const options = {
@@ -3088,7 +2635,6 @@ var ChannelDriver = class {
3088
2635
  conversation_id: row.conversation_id,
3089
2636
  message_id: row.id
3090
2637
  });
3091
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3092
2638
  return;
3093
2639
  }
3094
2640
  if (ocId === null) {
@@ -3099,7 +2645,6 @@ var ChannelDriver = class {
3099
2645
  conversation_id: row.conversation_id,
3100
2646
  message_id: row.id
3101
2647
  });
3102
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
3103
2648
  return;
3104
2649
  }
3105
2650
  const conv = this.convForRow(sessionId, row);
@@ -3109,7 +2654,6 @@ var ChannelDriver = class {
3109
2654
  this.readopted.add(row.id);
3110
2655
  this.awaitingReadopt.delete(row.id);
3111
2656
  this.ensureWatcherRunning(sessionId);
3112
- void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
3113
2657
  }
3114
2658
  /**
3115
2659
  * True if `evidentMessageId` is already being driven — either in the
@@ -3201,41 +2745,21 @@ var ChannelDriver = class {
3201
2745
  * RUNNING (not done) is the one that paused. With one running message that is
3202
2746
  * unambiguous; with several we prefer an explicit messageID match, else the
3203
2747
  * 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
2748
  */
3213
2749
  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
2750
  let questions = [];
3219
2751
  try {
3220
2752
  const res = await this.fetchImpl(`${this.opencodeBase}/question`);
3221
2753
  if (res.ok) {
3222
2754
  const body = await res.json();
3223
- if (Array.isArray(body)) {
3224
- questions = body;
3225
- } else {
3226
- questionsPolledOk = false;
3227
- }
3228
- } else {
3229
- questionsPolledOk = false;
2755
+ questions = Array.isArray(body) ? body : [];
3230
2756
  }
3231
2757
  } catch {
3232
- questionsPolledOk = false;
3233
2758
  }
3234
2759
  for (const q of questions) {
2760
+ if (watcher.reportedQuestions.has(q.id)) continue;
3235
2761
  if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
3236
2762
  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
2763
  const reported = await this.reportInteraction(
3240
2764
  watcher.conv.id,
3241
2765
  "question",
@@ -3249,22 +2773,14 @@ var ChannelDriver = class {
3249
2773
  const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
3250
2774
  if (res.ok) {
3251
2775
  const body = await res.json();
3252
- if (Array.isArray(body)) {
3253
- permissions = body;
3254
- } else {
3255
- permissionsPolledOk = false;
3256
- }
3257
- } else {
3258
- permissionsPolledOk = false;
2776
+ permissions = Array.isArray(body) ? body : [];
3259
2777
  }
3260
2778
  } catch {
3261
- permissionsPolledOk = false;
3262
2779
  }
3263
2780
  for (const p of permissions) {
2781
+ if (watcher.reportedPermissions.has(p.id)) continue;
3264
2782
  if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
3265
2783
  const paused = this.attributeInteraction(watcher, p.messageID, messages);
3266
- if (paused) openPermissions.add(paused.evidentMessageId);
3267
- if (watcher.reportedPermissions.has(p.id)) continue;
3268
2784
  const reported = await this.reportInteraction(
3269
2785
  watcher.conv.id,
3270
2786
  "permission",
@@ -3273,7 +2789,6 @@ var ChannelDriver = class {
3273
2789
  );
3274
2790
  if (reported) watcher.reportedPermissions.add(p.id);
3275
2791
  }
3276
- return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
3277
2792
  }
3278
2793
  /**
3279
2794
  * True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
@@ -3319,83 +2834,6 @@ var ChannelDriver = class {
3319
2834
  if (parent !== void 0) this.sessionParents.set(sessionId, parent);
3320
2835
  return parent;
3321
2836
  }
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
2837
  /**
3400
2838
  * Attribute a surfaced interaction to the in-flight message it paused on (M-1).
3401
2839
  *
@@ -3627,12 +3065,6 @@ var ChannelDriver = class {
3627
3065
  * MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
3628
3066
  * watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
3629
3067
  * 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
3068
  */
3637
3069
  async postSignal(conversationId, messageId, signal, extra) {
3638
3070
  try {
@@ -3651,9 +3083,7 @@ var ChannelDriver = class {
3651
3083
  conversation_id: conversationId,
3652
3084
  message_id: messageId
3653
3085
  });
3654
- return false;
3655
3086
  }
3656
- return true;
3657
3087
  } catch (err) {
3658
3088
  this.log({
3659
3089
  level: "error",
@@ -3661,7 +3091,6 @@ var ChannelDriver = class {
3661
3091
  conversation_id: conversationId,
3662
3092
  message_id: messageId
3663
3093
  });
3664
- return false;
3665
3094
  }
3666
3095
  }
3667
3096
  async persistSession(conversationId, sessionId) {
@@ -4154,7 +3583,7 @@ async function driveChannels(state, driver) {
4154
3583
  logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
4155
3584
  if (state.interactive) displayStatus(state);
4156
3585
  }
4157
- await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
3586
+ await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
4158
3587
  if (state.idleTimeout !== null && idlePolls >= 2) {
4159
3588
  const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
4160
3589
  if (idleMs > state.idleTimeout * 1e3) {
@@ -4165,81 +3594,6 @@ async function driveChannels(state, driver) {
4165
3594
  }
4166
3595
  }
4167
3596
  }
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
3597
  async function notifyOffline(state) {
4244
3598
  if (!state.agentId || !state.authHeader) return;
4245
3599
  if (!state.connected) {
@@ -4259,11 +3613,6 @@ async function notifyOffline(state) {
4259
3613
  }
4260
3614
  async function cleanup(state, opts = {}) {
4261
3615
  state.running = false;
4262
- for (const timer of state.sessionCleanupTimers) {
4263
- clearInterval(timer);
4264
- clearTimeout(timer);
4265
- }
4266
- state.sessionCleanupTimers = [];
4267
3616
  if (opts.graceful && state.channelDriver) {
4268
3617
  state.channelDriver.stop();
4269
3618
  log2(state, "Draining in-flight channel work before shutdown...");
@@ -4317,7 +3666,6 @@ async function run(options) {
4317
3666
  activityLog: [],
4318
3667
  messageCount: 0,
4319
3668
  lastProxiedActivityAt: null,
4320
- sessionCleanupTimers: [],
4321
3669
  authHeader: ""
4322
3670
  };
4323
3671
  if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
@@ -4560,7 +3908,6 @@ async function run(options) {
4560
3908
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
4561
3909
  throw error2;
4562
3910
  }
4563
- scheduleSessionCleanup(state, channelDriver, options);
4564
3911
  if (!interactive || state.json) {
4565
3912
  log2(state, "Driving channel messages...");
4566
3913
  }
@@ -4615,16 +3962,7 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
4615
3962
  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);
4616
3963
  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 }));
4617
3964
  program.command("whoami").description("Show the currently logged in user").action(whoami);
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(
3965
+ 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(
4628
3966
  (options) => {
4629
3967
  run({
4630
3968
  agent: options.agent,
@@ -4632,11 +3970,7 @@ program.command("run").description("Connect to Evident and process messages").op
4632
3970
  verbose: options.verbose,
4633
3971
  conversation: options.conversation,
4634
3972
  idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
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
3973
+ json: options.json
4640
3974
  });
4641
3975
  }
4642
3976
  );