@evident-ai/cli 3.0.1-dev.ab867cb → 3.0.1-dev.b58bd29
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 +41 -680
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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((
|
|
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
|
-
|
|
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((
|
|
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((
|
|
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
|
-
|
|
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
|
-
|
|
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((
|
|
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((
|
|
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((
|
|
1317
|
+
bodyPromise = new Promise((resolve) => {
|
|
1505
1318
|
pushBody = (buf) => {
|
|
1506
1319
|
chunks.push(buf);
|
|
1507
1320
|
};
|
|
1508
1321
|
endBody = () => {
|
|
1509
|
-
|
|
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((
|
|
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
|
-
|
|
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);
|
|
@@ -2044,28 +1854,6 @@ var ChannelDriver = class {
|
|
|
2044
1854
|
}
|
|
2045
1855
|
return false;
|
|
2046
1856
|
}
|
|
2047
|
-
/**
|
|
2048
|
-
* OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
|
|
2049
|
-
* exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
|
|
2050
|
-
* `watchers` entry whose `inFlight` set is non-empty — the same predicate
|
|
2051
|
-
* `hasInFlightWatchers()` uses, lifted to return the ids.
|
|
2052
|
-
*
|
|
2053
|
-
* Deliberately does NOT include `this.sessions` (the permanent, never-pruned
|
|
2054
|
-
* conversation→session cache). Protecting every bound-but-idle session there
|
|
2055
|
-
* would shield nearly every session and defeat cleanup — AND it is unnecessary:
|
|
2056
|
-
* `ensureSession` is self-healing (it recreates a session whose id no longer
|
|
2057
|
-
* exists), so deleting an idle bound session is harmless — the conversation's
|
|
2058
|
-
* next turn transparently rebinds a fresh one. The only thing worth protecting
|
|
2059
|
-
* is a session with a turn ACTIVELY in flight right now: tearing that down
|
|
2060
|
-
* mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
|
|
2061
|
-
*/
|
|
2062
|
-
protectedSessionIds() {
|
|
2063
|
-
const ids = /* @__PURE__ */ new Set();
|
|
2064
|
-
for (const [sessionId, watcher] of this.watchers) {
|
|
2065
|
-
if (watcher.inFlight.size > 0) ids.add(sessionId);
|
|
2066
|
-
}
|
|
2067
|
-
return ids;
|
|
2068
|
-
}
|
|
2069
1857
|
/**
|
|
2070
1858
|
* Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
|
|
2071
1859
|
* this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
|
|
@@ -2171,16 +1959,6 @@ var ChannelDriver = class {
|
|
|
2171
1959
|
} catch (err) {
|
|
2172
1960
|
if (err instanceof ChannelAuthError) throw err;
|
|
2173
1961
|
this.dispatched.delete(message.id);
|
|
2174
|
-
if (await sessionExists(this.port, sessionId) === false) {
|
|
2175
|
-
this.sessions.delete(conv.id);
|
|
2176
|
-
this.log({
|
|
2177
|
-
level: "info",
|
|
2178
|
-
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.`,
|
|
2179
|
-
conversation_id: conv.id,
|
|
2180
|
-
message_id: message.id
|
|
2181
|
-
});
|
|
2182
|
-
break;
|
|
2183
|
-
}
|
|
2184
1962
|
await this.markFailed(conv.id, message.id).catch(() => {
|
|
2185
1963
|
});
|
|
2186
1964
|
this.log({
|
|
@@ -2216,33 +1994,16 @@ var ChannelDriver = class {
|
|
|
2216
1994
|
return dispatched;
|
|
2217
1995
|
}
|
|
2218
1996
|
async ensureSession(conv) {
|
|
2219
|
-
const
|
|
2220
|
-
if (
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
level: "info",
|
|
2225
|
-
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.`,
|
|
2226
|
-
conversation_id: conv.id
|
|
2227
|
-
});
|
|
2228
|
-
this.sessions.delete(conv.id);
|
|
2229
|
-
return this.createAndBindSession(conv.id);
|
|
2230
|
-
}
|
|
2231
|
-
this.sessions.set(conv.id, bound);
|
|
2232
|
-
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;
|
|
2233
2002
|
}
|
|
2234
|
-
return this.createAndBindSession(conv.id);
|
|
2235
|
-
}
|
|
2236
|
-
/**
|
|
2237
|
-
* Create a fresh OpenCode session for a conversation, cache the binding, and
|
|
2238
|
-
* best-effort persist it server-side. Shared by the first-ever bind and the
|
|
2239
|
-
* self-heal recreate path in `ensureSession`.
|
|
2240
|
-
*/
|
|
2241
|
-
async createAndBindSession(conversationId) {
|
|
2242
2003
|
const directory = await this.resolveOpenCodeDirectory();
|
|
2243
2004
|
const sessionId = await createOpenCodeSession(this.port, directory);
|
|
2244
|
-
this.sessions.set(
|
|
2245
|
-
await this.persistSession(
|
|
2005
|
+
this.sessions.set(conv.id, sessionId);
|
|
2006
|
+
await this.persistSession(conv.id, sessionId).catch(() => {
|
|
2246
2007
|
});
|
|
2247
2008
|
return sessionId;
|
|
2248
2009
|
}
|
|
@@ -2293,9 +2054,7 @@ var ChannelDriver = class {
|
|
|
2293
2054
|
inFlight: /* @__PURE__ */ new Map(),
|
|
2294
2055
|
loop: null,
|
|
2295
2056
|
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2296
|
-
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2297
|
-
lastGoodPollAt: this.now(),
|
|
2298
|
-
hadUsablePoll: false
|
|
2057
|
+
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2299
2058
|
};
|
|
2300
2059
|
this.watchers.set(sessionId, watcher);
|
|
2301
2060
|
}
|
|
@@ -2305,37 +2064,20 @@ var ChannelDriver = class {
|
|
|
2305
2064
|
opencodeMessageId,
|
|
2306
2065
|
message,
|
|
2307
2066
|
dispatchedAt: now,
|
|
2308
|
-
processingAnchorMs: now,
|
|
2309
2067
|
deadline: now + this.pausedMaxWaitMs,
|
|
2310
2068
|
started: false,
|
|
2311
2069
|
done: false,
|
|
2312
|
-
stuckReported: false
|
|
2313
|
-
lastAliveAt: 0,
|
|
2314
|
-
aliveInFlight: false,
|
|
2315
|
-
awaitingHumanLatched: false,
|
|
2316
|
-
pausedOnQuestion: false,
|
|
2317
|
-
pausedOnPermission: false,
|
|
2318
|
-
pausedClearConfirmed: false,
|
|
2319
|
-
pausedInFlight: false,
|
|
2320
|
-
deliveryDeadlineAnchored: false
|
|
2070
|
+
stuckReported: false
|
|
2321
2071
|
});
|
|
2322
2072
|
}
|
|
2323
2073
|
/**
|
|
2324
2074
|
* Register a RE-ADOPTED `processing` message with its session watcher
|
|
2325
2075
|
* (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
|
|
2326
2076
|
* `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
|
|
2327
|
-
* `now
|
|
2328
|
-
*
|
|
2329
|
-
*
|
|
2330
|
-
*
|
|
2331
|
-
* give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
|
|
2332
|
-
* opencode reports ACTIVELY `running` is watched to completion (its liveness
|
|
2333
|
-
* heartbeat keeps the cron off its row), while a re-adopted turn that is paused
|
|
2334
|
-
* awaiting a human — or queued/unreachable — is still bounded by `deadline` and
|
|
2335
|
-
* handed to the cron. The old "the `deadline` must settle before the ~15-min
|
|
2336
|
-
* cron or they double-drive" reasoning is superseded: liveness now settles the
|
|
2337
|
-
* actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
|
|
2338
|
-
* (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).
|
|
2339
2081
|
*
|
|
2340
2082
|
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
2341
2083
|
* `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
|
|
@@ -2353,9 +2095,7 @@ var ChannelDriver = class {
|
|
|
2353
2095
|
inFlight: /* @__PURE__ */ new Map(),
|
|
2354
2096
|
loop: null,
|
|
2355
2097
|
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2356
|
-
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2357
|
-
lastGoodPollAt: this.now(),
|
|
2358
|
-
hadUsablePoll: false
|
|
2098
|
+
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2359
2099
|
};
|
|
2360
2100
|
this.watchers.set(sessionId, watcher);
|
|
2361
2101
|
}
|
|
@@ -2364,10 +2104,6 @@ var ChannelDriver = class {
|
|
|
2364
2104
|
opencodeMessageId,
|
|
2365
2105
|
message,
|
|
2366
2106
|
dispatchedAt: this.now(),
|
|
2367
|
-
// Anchor the absolute-age ceiling to the SERVER-SIDE `processed_at` (the same
|
|
2368
|
-
// value seeding `deadline`), NOT `dispatchedAt` — so a re-adopted zombie's age
|
|
2369
|
-
// reflects the real turn duration and the ceiling fires on the ORIGINAL turn.
|
|
2370
|
-
processingAnchorMs: processedAtMs,
|
|
2371
2107
|
deadline: processedAtMs + this.pausedMaxWaitMs,
|
|
2372
2108
|
// The server row is ALREADY `processing`; do not re-fire markProcessing.
|
|
2373
2109
|
started: true,
|
|
@@ -2377,20 +2113,7 @@ var ChannelDriver = class {
|
|
|
2377
2113
|
// on `state === 'queued'` (turn produced no reply), not on `started`, so a
|
|
2378
2114
|
// re-adopted row left wedged in `queued` still emits the signal once
|
|
2379
2115
|
// (#210/#220 observability).
|
|
2380
|
-
stuckReported: false
|
|
2381
|
-
// Task 5.2: a re-adopted actively-running row re-attaches into the SAME
|
|
2382
|
-
// watcher and so hits the SAME actively-running heartbeat branch in
|
|
2383
|
-
// `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
|
|
2384
|
-
// re-adopted and is confirming this row alive" via that `alive` heartbeat,
|
|
2385
|
-
// with no extra `re_adopted` signal needed (folds old WI-6).
|
|
2386
|
-
lastAliveAt: 0,
|
|
2387
|
-
aliveInFlight: false,
|
|
2388
|
-
awaitingHumanLatched: false,
|
|
2389
|
-
pausedOnQuestion: false,
|
|
2390
|
-
pausedOnPermission: false,
|
|
2391
|
-
pausedClearConfirmed: false,
|
|
2392
|
-
pausedInFlight: false,
|
|
2393
|
-
deliveryDeadlineAnchored: false
|
|
2116
|
+
stuckReported: false
|
|
2394
2117
|
});
|
|
2395
2118
|
}
|
|
2396
2119
|
/**
|
|
@@ -2441,30 +2164,12 @@ var ChannelDriver = class {
|
|
|
2441
2164
|
messages = Array.isArray(body) ? body : null;
|
|
2442
2165
|
}
|
|
2443
2166
|
} catch {
|
|
2167
|
+
continue;
|
|
2444
2168
|
}
|
|
2445
|
-
if (messages != null && messages.length > 0) {
|
|
2446
|
-
watcher.lastGoodPollAt = this.now();
|
|
2447
|
-
watcher.hadUsablePoll = true;
|
|
2448
|
-
} else {
|
|
2449
|
-
const emptyButReachable = messages != null;
|
|
2450
|
-
const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
|
|
2451
|
-
if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
|
|
2452
|
-
continue;
|
|
2453
|
-
}
|
|
2454
|
-
}
|
|
2455
|
-
const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
|
|
2456
2169
|
for (const inFlight of [...watcher.inFlight.values()]) {
|
|
2457
|
-
await this.serviceInFlightMessage(
|
|
2458
|
-
sessionId,
|
|
2459
|
-
watcher,
|
|
2460
|
-
inFlight,
|
|
2461
|
-
messages,
|
|
2462
|
-
openQuestions,
|
|
2463
|
-
openPermissions,
|
|
2464
|
-
questionsPolledOk,
|
|
2465
|
-
permissionsPolledOk
|
|
2466
|
-
);
|
|
2170
|
+
await this.serviceInFlightMessage(sessionId, watcher, inFlight, messages);
|
|
2467
2171
|
}
|
|
2172
|
+
await this.pollInteractions(sessionId, watcher, messages);
|
|
2468
2173
|
}
|
|
2469
2174
|
} catch (err) {
|
|
2470
2175
|
if (err instanceof ChannelAuthError) {
|
|
@@ -2486,40 +2191,15 @@ var ChannelDriver = class {
|
|
|
2486
2191
|
});
|
|
2487
2192
|
}
|
|
2488
2193
|
}
|
|
2489
|
-
/**
|
|
2490
|
-
* On FIRST observing a terminal (done/failed) state, ensure the delivery
|
|
2491
|
-
* (markDone/markFailed) transient-retry path has a real window. A long
|
|
2492
|
-
* ACTIVELY-running turn is kept past its original `deadline`, so by completion
|
|
2493
|
-
* `now >= deadline` already holds and the retry bound below would fire on the
|
|
2494
|
-
* first transient PATCH failure — dropping the message before its reply lands
|
|
2495
|
-
* (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
|
|
2496
|
-
* to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
|
|
2497
|
-
* or past now, so a still-ample window is left untouched.
|
|
2498
|
-
*/
|
|
2499
|
-
anchorDeliveryDeadline(inFlight) {
|
|
2500
|
-
if (inFlight.deliveryDeadlineAnchored) return;
|
|
2501
|
-
inFlight.deliveryDeadlineAnchored = true;
|
|
2502
|
-
if (this.now() >= inFlight.deadline) {
|
|
2503
|
-
inFlight.deadline = this.now() + this.pausedMaxWaitMs;
|
|
2504
|
-
}
|
|
2505
|
-
}
|
|
2506
2194
|
/**
|
|
2507
2195
|
* Drive ONE in-flight message's lifecycle from the tick's message snapshot.
|
|
2508
2196
|
* Fires markProcessing on queued→running and markDone on done (each once),
|
|
2509
2197
|
* applies the idle-path re-dispatch guard, and removes the message from the
|
|
2510
2198
|
* in-flight set on completion or timeout.
|
|
2511
2199
|
*/
|
|
2512
|
-
async serviceInFlightMessage(sessionId, watcher, inFlight, messages
|
|
2200
|
+
async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
|
|
2513
2201
|
const conv = watcher.conv;
|
|
2514
2202
|
const state = messageRunState(messages, inFlight.opencodeMessageId);
|
|
2515
|
-
const id = inFlight.evidentMessageId;
|
|
2516
|
-
if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
|
|
2517
|
-
else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
|
|
2518
|
-
if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
|
|
2519
|
-
else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
|
|
2520
|
-
const observedOpen = openQuestions.has(id) || openPermissions.has(id);
|
|
2521
|
-
const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
|
|
2522
|
-
const awaitingHuman = observedOpen || latchedPaused;
|
|
2523
2203
|
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
2524
2204
|
let claimed;
|
|
2525
2205
|
try {
|
|
@@ -2550,7 +2230,6 @@ var ChannelDriver = class {
|
|
|
2550
2230
|
}
|
|
2551
2231
|
}
|
|
2552
2232
|
if (state === "done") {
|
|
2553
|
-
this.anchorDeliveryDeadline(inFlight);
|
|
2554
2233
|
if (!inFlight.done) {
|
|
2555
2234
|
this.log({
|
|
2556
2235
|
level: "info",
|
|
@@ -2601,7 +2280,6 @@ var ChannelDriver = class {
|
|
|
2601
2280
|
return;
|
|
2602
2281
|
}
|
|
2603
2282
|
if (state === "failed") {
|
|
2604
|
-
this.anchorDeliveryDeadline(inFlight);
|
|
2605
2283
|
if (!inFlight.done) {
|
|
2606
2284
|
const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
|
|
2607
2285
|
this.log({
|
|
@@ -2655,51 +2333,7 @@ var ChannelDriver = class {
|
|
|
2655
2333
|
stuck_for_ms: this.now() - inFlight.dispatchedAt
|
|
2656
2334
|
});
|
|
2657
2335
|
}
|
|
2658
|
-
|
|
2659
|
-
if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
|
|
2660
|
-
this.log({
|
|
2661
|
-
level: "error",
|
|
2662
|
-
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`,
|
|
2663
|
-
conversation_id: conv.id,
|
|
2664
|
-
message_id: inFlight.evidentMessageId
|
|
2665
|
-
});
|
|
2666
|
-
void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
|
|
2667
|
-
watched_for_ms: this.now() - inFlight.processingAnchorMs
|
|
2668
|
-
});
|
|
2669
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2670
|
-
return;
|
|
2671
|
-
}
|
|
2672
|
-
if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
|
|
2673
|
-
inFlight.aliveInFlight = true;
|
|
2674
|
-
void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
|
|
2675
|
-
inFlight.aliveInFlight = false;
|
|
2676
|
-
if (ok) inFlight.lastAliveAt = this.now();
|
|
2677
|
-
});
|
|
2678
|
-
}
|
|
2679
|
-
if (awaitingHuman) {
|
|
2680
|
-
if (!inFlight.awaitingHumanLatched) {
|
|
2681
|
-
inFlight.deadline = this.now() + this.pausedMaxWaitMs;
|
|
2682
|
-
inFlight.awaitingHumanLatched = true;
|
|
2683
|
-
}
|
|
2684
|
-
if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
|
|
2685
|
-
inFlight.pausedInFlight = true;
|
|
2686
|
-
void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
|
|
2687
|
-
inFlight.pausedInFlight = false;
|
|
2688
|
-
if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
|
|
2689
|
-
});
|
|
2690
|
-
}
|
|
2691
|
-
} else if (inFlight.awaitingHumanLatched) {
|
|
2692
|
-
inFlight.awaitingHumanLatched = false;
|
|
2693
|
-
inFlight.pausedOnQuestion = false;
|
|
2694
|
-
inFlight.pausedOnPermission = false;
|
|
2695
|
-
inFlight.pausedClearConfirmed = false;
|
|
2696
|
-
}
|
|
2697
|
-
const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
|
|
2698
|
-
const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
|
|
2699
|
-
(sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
|
|
2700
|
-
);
|
|
2701
|
-
const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
|
|
2702
|
-
if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
|
|
2336
|
+
if (this.now() >= inFlight.deadline) {
|
|
2703
2337
|
this.log({
|
|
2704
2338
|
level: "info",
|
|
2705
2339
|
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
|
|
@@ -2790,10 +2424,8 @@ var ChannelDriver = class {
|
|
|
2790
2424
|
});
|
|
2791
2425
|
continue;
|
|
2792
2426
|
}
|
|
2793
|
-
const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
|
|
2794
|
-
const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
|
|
2795
2427
|
for (const row of sessionRows) {
|
|
2796
|
-
await this.readoptOne(sessionId, row, messages
|
|
2428
|
+
await this.readoptOne(sessionId, row, messages);
|
|
2797
2429
|
}
|
|
2798
2430
|
}
|
|
2799
2431
|
}
|
|
@@ -2815,7 +2447,7 @@ var ChannelDriver = class {
|
|
|
2815
2447
|
*
|
|
2816
2448
|
* Only `ChannelAuthError` propagates.
|
|
2817
2449
|
*/
|
|
2818
|
-
async readoptOne(sessionId, row, messages
|
|
2450
|
+
async readoptOne(sessionId, row, messages) {
|
|
2819
2451
|
if (this.isTracked(sessionId, row.id)) {
|
|
2820
2452
|
this.log({
|
|
2821
2453
|
level: "info",
|
|
@@ -2910,67 +2542,6 @@ var ChannelDriver = class {
|
|
|
2910
2542
|
});
|
|
2911
2543
|
return;
|
|
2912
2544
|
}
|
|
2913
|
-
let statusReadableOngoing = null;
|
|
2914
|
-
if (state === "running" && ocId) {
|
|
2915
|
-
const reply = findLastAssistantReplyFor(messages, ocId);
|
|
2916
|
-
const shape = this.replyCompletionShape(reply);
|
|
2917
|
-
const ongoing = sessionOngoing;
|
|
2918
|
-
statusReadableOngoing = ongoing;
|
|
2919
|
-
if (ongoing === false) {
|
|
2920
|
-
this.log({
|
|
2921
|
-
level: "info",
|
|
2922
|
-
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)`,
|
|
2923
|
-
conversation_id: row.conversation_id,
|
|
2924
|
-
message_id: row.id
|
|
2925
|
-
});
|
|
2926
|
-
await this.forceReadoptRun(sessionId, row);
|
|
2927
|
-
return;
|
|
2928
|
-
}
|
|
2929
|
-
if (ongoing === true) {
|
|
2930
|
-
this.log({
|
|
2931
|
-
level: "info",
|
|
2932
|
-
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)`,
|
|
2933
|
-
conversation_id: row.conversation_id,
|
|
2934
|
-
message_id: row.id
|
|
2935
|
-
});
|
|
2936
|
-
} else {
|
|
2937
|
-
if (shape === "b1") {
|
|
2938
|
-
this.log({
|
|
2939
|
-
level: "info",
|
|
2940
|
-
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`,
|
|
2941
|
-
conversation_id: row.conversation_id,
|
|
2942
|
-
message_id: row.id
|
|
2943
|
-
});
|
|
2944
|
-
return;
|
|
2945
|
-
}
|
|
2946
|
-
this.log({
|
|
2947
|
-
level: "info",
|
|
2948
|
-
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`,
|
|
2949
|
-
conversation_id: row.conversation_id,
|
|
2950
|
-
message_id: row.id
|
|
2951
|
-
});
|
|
2952
|
-
}
|
|
2953
|
-
}
|
|
2954
|
-
if (statusReadableOngoing === null && state === "running" && ocId && isPreamblePinnedRunning(messages, ocId)) {
|
|
2955
|
-
const descendantAlive = await this.isAnyDescendantSessionAlive(sessionId);
|
|
2956
|
-
if (descendantAlive === true) {
|
|
2957
|
-
this.log({
|
|
2958
|
-
level: "info",
|
|
2959
|
-
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)`,
|
|
2960
|
-
conversation_id: row.conversation_id,
|
|
2961
|
-
message_id: row.id
|
|
2962
|
-
});
|
|
2963
|
-
} else {
|
|
2964
|
-
this.log({
|
|
2965
|
-
level: "info",
|
|
2966
|
-
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)" : ""}`,
|
|
2967
|
-
conversation_id: row.conversation_id,
|
|
2968
|
-
message_id: row.id
|
|
2969
|
-
});
|
|
2970
|
-
await this.forceReadoptRun(sessionId, row);
|
|
2971
|
-
return;
|
|
2972
|
-
}
|
|
2973
|
-
}
|
|
2974
2545
|
if ((state === "running" || state === "queued") && ocId) {
|
|
2975
2546
|
const conv = this.convForRow(sessionId, row);
|
|
2976
2547
|
const message = this.queuedMessageForRow(row);
|
|
@@ -3174,41 +2745,21 @@ var ChannelDriver = class {
|
|
|
3174
2745
|
* RUNNING (not done) is the one that paused. With one running message that is
|
|
3175
2746
|
* unambiguous; with several we prefer an explicit messageID match, else the
|
|
3176
2747
|
* oldest running message.
|
|
3177
|
-
*
|
|
3178
|
-
* Returns the set of in-flight Evident message ids that are paused awaiting a
|
|
3179
|
-
* human — an outstanding (still-open) question/permission is attributed to them.
|
|
3180
|
-
* `serviceInFlightMessage` uses this to keep an actively-running turn watched
|
|
3181
|
-
* forever (ADR-0047) while still bounding a turn merely blocked on a person who
|
|
3182
|
-
* may never answer. Attribution here covers ALL open interactions, not just
|
|
3183
|
-
* NEW (un-deduped) ones — a question stays "awaiting a human" until answered,
|
|
3184
|
-
* even after it was already surfaced to the channel.
|
|
3185
2748
|
*/
|
|
3186
2749
|
async pollInteractions(sessionId, watcher, messages) {
|
|
3187
|
-
const openQuestions = /* @__PURE__ */ new Set();
|
|
3188
|
-
const openPermissions = /* @__PURE__ */ new Set();
|
|
3189
|
-
let questionsPolledOk = true;
|
|
3190
|
-
let permissionsPolledOk = true;
|
|
3191
2750
|
let questions = [];
|
|
3192
2751
|
try {
|
|
3193
2752
|
const res = await this.fetchImpl(`${this.opencodeBase}/question`);
|
|
3194
2753
|
if (res.ok) {
|
|
3195
2754
|
const body = await res.json();
|
|
3196
|
-
|
|
3197
|
-
questions = body;
|
|
3198
|
-
} else {
|
|
3199
|
-
questionsPolledOk = false;
|
|
3200
|
-
}
|
|
3201
|
-
} else {
|
|
3202
|
-
questionsPolledOk = false;
|
|
2755
|
+
questions = Array.isArray(body) ? body : [];
|
|
3203
2756
|
}
|
|
3204
2757
|
} catch {
|
|
3205
|
-
questionsPolledOk = false;
|
|
3206
2758
|
}
|
|
3207
2759
|
for (const q of questions) {
|
|
2760
|
+
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
3208
2761
|
if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
|
|
3209
2762
|
const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
|
|
3210
|
-
if (paused) openQuestions.add(paused.evidentMessageId);
|
|
3211
|
-
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
3212
2763
|
const reported = await this.reportInteraction(
|
|
3213
2764
|
watcher.conv.id,
|
|
3214
2765
|
"question",
|
|
@@ -3222,22 +2773,14 @@ var ChannelDriver = class {
|
|
|
3222
2773
|
const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
|
|
3223
2774
|
if (res.ok) {
|
|
3224
2775
|
const body = await res.json();
|
|
3225
|
-
|
|
3226
|
-
permissions = body;
|
|
3227
|
-
} else {
|
|
3228
|
-
permissionsPolledOk = false;
|
|
3229
|
-
}
|
|
3230
|
-
} else {
|
|
3231
|
-
permissionsPolledOk = false;
|
|
2776
|
+
permissions = Array.isArray(body) ? body : [];
|
|
3232
2777
|
}
|
|
3233
2778
|
} catch {
|
|
3234
|
-
permissionsPolledOk = false;
|
|
3235
2779
|
}
|
|
3236
2780
|
for (const p of permissions) {
|
|
2781
|
+
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
3237
2782
|
if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
|
|
3238
2783
|
const paused = this.attributeInteraction(watcher, p.messageID, messages);
|
|
3239
|
-
if (paused) openPermissions.add(paused.evidentMessageId);
|
|
3240
|
-
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
3241
2784
|
const reported = await this.reportInteraction(
|
|
3242
2785
|
watcher.conv.id,
|
|
3243
2786
|
"permission",
|
|
@@ -3246,7 +2789,6 @@ var ChannelDriver = class {
|
|
|
3246
2789
|
);
|
|
3247
2790
|
if (reported) watcher.reportedPermissions.add(p.id);
|
|
3248
2791
|
}
|
|
3249
|
-
return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
|
|
3250
2792
|
}
|
|
3251
2793
|
/**
|
|
3252
2794
|
* True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
|
|
@@ -3292,83 +2834,6 @@ var ChannelDriver = class {
|
|
|
3292
2834
|
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
3293
2835
|
return parent;
|
|
3294
2836
|
}
|
|
3295
|
-
/**
|
|
3296
|
-
* DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
|
|
3297
|
-
* (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
|
|
3298
|
-
*
|
|
3299
|
-
* The PRIMARY recovery trigger is "preamble-pinned on recovery ⇒ idle" — a
|
|
3300
|
-
* runner restart wipes OpenCode's in-memory `SessionStatus`/`Runner`, so a
|
|
3301
|
-
* completed `finish: "tool-calls"` root reply encountered during re-adoption is
|
|
3302
|
-
* idle by OpenCode's own definition and is re-dispatched. This method exists only
|
|
3303
|
-
* so the WI-3 caller can VETO that re-dispatch in the rare case a descendant is
|
|
3304
|
-
* provably in flight at the exact moment of recovery.
|
|
3305
|
-
*
|
|
3306
|
-
* "Alive" criterion (TIGHTENED): a descendant is alive only when it is PROVABLY,
|
|
3307
|
-
* ACTIVELY generating — its LAST message is an assistant still mid-generation
|
|
3308
|
-
* (`completed == null`, via `isSessionActivelyGenerating`). An
|
|
3309
|
-
* INCOMPLETE-BUT-NOT-GENERATING child — last message a user message, or a
|
|
3310
|
-
* completed `finish: "tool-calls"` step — is NOT alive after a restart (nothing
|
|
3311
|
-
* is generating once the runner is gone), so it does NOT veto. (This is
|
|
3312
|
-
* deliberately NOT `!isTurnComplete`, which also matches those dead-but-non-terminal
|
|
3313
|
-
* shapes and would falsely veto — re-hanging the very turn this path recovers.)
|
|
3314
|
-
*
|
|
3315
|
-
* Return contract (encoded so WI-3 need not re-derive it):
|
|
3316
|
-
* - `true` → a descendant is provably, actively generating (veto re-dispatch).
|
|
3317
|
-
* - `false` → descendants exist but none is actively generating (the restart
|
|
3318
|
-
* case), OR no descendant is found at all.
|
|
3319
|
-
* - `null` → liveness is INDETERMINATE (enumeration via `listSessions` failed).
|
|
3320
|
-
*
|
|
3321
|
-
* ⚠️ `null` (UNKNOWN) MUST NOT be treated as "alive": WI-3 treats `null` the same
|
|
3322
|
-
* as `false` and does NOT veto — a restart guarantees no live runner, so an
|
|
3323
|
-
* indeterminate cross-check almost always means "couldn't reach a child that no
|
|
3324
|
-
* longer exists". The inversion lives in the caller; this method just reports
|
|
3325
|
-
* true/false/null faithfully.
|
|
3326
|
-
*
|
|
3327
|
-
* VERIFY-BEFORE-DEPEND: we depend ONLY on (a) `parentID` from `GET /session/:id`
|
|
3328
|
-
* (already proven by the existing child-session interaction tests, via
|
|
3329
|
-
* `resolveSessionParent`/`sessionBelongsTo`) and (b) the child's own message-list
|
|
3330
|
-
* terminal state. We do NOT depend on any session-level `busy`/`idle` field —
|
|
3331
|
-
* there is none on `GET /session/:id`; OpenCode's busy state is in-memory
|
|
3332
|
-
* `SessionStatus` only.
|
|
3333
|
-
*/
|
|
3334
|
-
async isAnyDescendantSessionAlive(rootSessionId) {
|
|
3335
|
-
const sessions = await listSessions(this.port);
|
|
3336
|
-
if (!sessions) {
|
|
3337
|
-
this.log({
|
|
3338
|
-
level: "error",
|
|
3339
|
-
message: `Re-adopt: could not enumerate sessions to cross-check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating child liveness as indeterminate`
|
|
3340
|
-
});
|
|
3341
|
-
return null;
|
|
3342
|
-
}
|
|
3343
|
-
for (const candidate of sessions) {
|
|
3344
|
-
if (!candidate?.id || candidate.id === rootSessionId) continue;
|
|
3345
|
-
if (!await this.sessionBelongsTo(candidate.id, rootSessionId)) continue;
|
|
3346
|
-
const childMsgs = await getSessionMessages(this.port, candidate.id);
|
|
3347
|
-
if (isSessionActivelyGenerating(childMsgs)) {
|
|
3348
|
-
return true;
|
|
3349
|
-
}
|
|
3350
|
-
}
|
|
3351
|
-
return false;
|
|
3352
|
-
}
|
|
3353
|
-
/**
|
|
3354
|
-
* Cheap decision-telemetry label for a running row's LAST correlated reply
|
|
3355
|
-
* (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
|
|
3356
|
-
* - `b1` — the reply itself is still in flight (`time.completed == null`) —
|
|
3357
|
-
* the aborted-in-flight production bug after a restart.
|
|
3358
|
-
* - `b2` — a COMPLETED reply pinned running only by `finish === "tool-calls"`
|
|
3359
|
-
* (the sub-agent preamble — #253's shape).
|
|
3360
|
-
* - `other` — any other shape (defensive; a running row is normally b1 or b2).
|
|
3361
|
-
* Reads `info.time.completed` / `info.finish` (tolerating the legacy top-level
|
|
3362
|
-
* shape) directly rather than re-importing the module-private `completedOf`/
|
|
3363
|
-
* `finishOf` — this is a display label only, not a correctness predicate.
|
|
3364
|
-
*/
|
|
3365
|
-
replyCompletionShape(reply) {
|
|
3366
|
-
if (!reply) return "other";
|
|
3367
|
-
const completed = reply.info?.time?.completed ?? reply.time?.completed;
|
|
3368
|
-
if (completed == null) return "b1";
|
|
3369
|
-
const finish = reply.info?.finish ?? reply.finish;
|
|
3370
|
-
return finish === "tool-calls" ? "b2" : "other";
|
|
3371
|
-
}
|
|
3372
2837
|
/**
|
|
3373
2838
|
* Attribute a surfaced interaction to the in-flight message it paused on (M-1).
|
|
3374
2839
|
*
|
|
@@ -3600,12 +3065,6 @@ var ChannelDriver = class {
|
|
|
3600
3065
|
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
3601
3066
|
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
3602
3067
|
* context (no silent catch, per development-workflow).
|
|
3603
|
-
*
|
|
3604
|
-
* Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
|
|
3605
|
-
* telemetry), but the `paused` liveness-clear uses it to know whether to
|
|
3606
|
-
* RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
|
|
3607
|
-
* stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
|
|
3608
|
-
* leaves liveness").
|
|
3609
3068
|
*/
|
|
3610
3069
|
async postSignal(conversationId, messageId, signal, extra) {
|
|
3611
3070
|
try {
|
|
@@ -3624,9 +3083,7 @@ var ChannelDriver = class {
|
|
|
3624
3083
|
conversation_id: conversationId,
|
|
3625
3084
|
message_id: messageId
|
|
3626
3085
|
});
|
|
3627
|
-
return false;
|
|
3628
3086
|
}
|
|
3629
|
-
return true;
|
|
3630
3087
|
} catch (err) {
|
|
3631
3088
|
this.log({
|
|
3632
3089
|
level: "error",
|
|
@@ -3634,7 +3091,6 @@ var ChannelDriver = class {
|
|
|
3634
3091
|
conversation_id: conversationId,
|
|
3635
3092
|
message_id: messageId
|
|
3636
3093
|
});
|
|
3637
|
-
return false;
|
|
3638
3094
|
}
|
|
3639
3095
|
}
|
|
3640
3096
|
async persistSession(conversationId, sessionId) {
|
|
@@ -4127,7 +3583,7 @@ async function driveChannels(state, driver) {
|
|
|
4127
3583
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
|
|
4128
3584
|
if (state.interactive) displayStatus(state);
|
|
4129
3585
|
}
|
|
4130
|
-
await new Promise((
|
|
3586
|
+
await new Promise((resolve) => setTimeout(resolve, CHANNEL_POLL_INTERVAL_MS));
|
|
4131
3587
|
if (state.idleTimeout !== null && idlePolls >= 2) {
|
|
4132
3588
|
const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
|
|
4133
3589
|
if (idleMs > state.idleTimeout * 1e3) {
|
|
@@ -4138,81 +3594,6 @@ async function driveChannels(state, driver) {
|
|
|
4138
3594
|
}
|
|
4139
3595
|
}
|
|
4140
3596
|
}
|
|
4141
|
-
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
4142
|
-
async function runSweep(state, driver, config2) {
|
|
4143
|
-
const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
|
|
4144
|
-
try {
|
|
4145
|
-
const sessions = await listSessions(state.port);
|
|
4146
|
-
if (sessions === null) {
|
|
4147
|
-
logActivity(state, {
|
|
4148
|
-
type: "info",
|
|
4149
|
-
message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
|
|
4150
|
-
});
|
|
4151
|
-
return;
|
|
4152
|
-
}
|
|
4153
|
-
const toDelete = selectSessionsToDelete(
|
|
4154
|
-
sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
|
|
4155
|
-
{
|
|
4156
|
-
maxAgeMs: config2.maxAgeMs,
|
|
4157
|
-
maxCount: config2.maxCount,
|
|
4158
|
-
nowMs: Date.now(),
|
|
4159
|
-
protectedIds: driver.protectedSessionIds()
|
|
4160
|
-
}
|
|
4161
|
-
);
|
|
4162
|
-
const protectedNow = driver.protectedSessionIds();
|
|
4163
|
-
let deleted = 0;
|
|
4164
|
-
let failed = 0;
|
|
4165
|
-
let skippedNewlyActive = 0;
|
|
4166
|
-
for (const id of toDelete) {
|
|
4167
|
-
if (protectedNow.has(id)) {
|
|
4168
|
-
skippedNewlyActive++;
|
|
4169
|
-
logActivity(state, {
|
|
4170
|
-
type: "info",
|
|
4171
|
-
message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
|
|
4172
|
-
});
|
|
4173
|
-
continue;
|
|
4174
|
-
}
|
|
4175
|
-
if (await deleteSession(state.port, id)) deleted++;
|
|
4176
|
-
else failed++;
|
|
4177
|
-
}
|
|
4178
|
-
const failedNote = failed > 0 ? `, failed ${failed}` : "";
|
|
4179
|
-
const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
|
|
4180
|
-
logActivity(state, {
|
|
4181
|
-
type: "info",
|
|
4182
|
-
message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
|
|
4183
|
-
});
|
|
4184
|
-
} catch (error2) {
|
|
4185
|
-
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
4186
|
-
logActivity(state, {
|
|
4187
|
-
type: "error",
|
|
4188
|
-
error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
|
|
4189
|
-
});
|
|
4190
|
-
}
|
|
4191
|
-
}
|
|
4192
|
-
function scheduleSessionCleanup(state, driver, options) {
|
|
4193
|
-
const config2 = resolveSessionCleanupConfig(
|
|
4194
|
-
{
|
|
4195
|
-
maxAge: options.sessionCleanupMaxAge,
|
|
4196
|
-
maxCount: options.sessionCleanupMaxCount,
|
|
4197
|
-
interval: options.sessionCleanupInterval
|
|
4198
|
-
},
|
|
4199
|
-
process.env
|
|
4200
|
-
);
|
|
4201
|
-
for (const warning2 of config2.warnings) {
|
|
4202
|
-
logActivity(state, { type: "info", message: `Session cleanup: ${warning2}` });
|
|
4203
|
-
}
|
|
4204
|
-
if (!config2.enabled) return;
|
|
4205
|
-
logActivity(state, {
|
|
4206
|
-
type: "info",
|
|
4207
|
-
message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
|
|
4208
|
-
});
|
|
4209
|
-
const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
|
|
4210
|
-
const firstSweep = setTimeout(
|
|
4211
|
-
() => void runSweep(state, driver, config2),
|
|
4212
|
-
SESSION_CLEANUP_FIRST_SWEEP_MS
|
|
4213
|
-
);
|
|
4214
|
-
state.sessionCleanupTimers.push(interval, firstSweep);
|
|
4215
|
-
}
|
|
4216
3597
|
async function notifyOffline(state) {
|
|
4217
3598
|
if (!state.agentId || !state.authHeader) return;
|
|
4218
3599
|
if (!state.connected) {
|
|
@@ -4232,11 +3613,6 @@ async function notifyOffline(state) {
|
|
|
4232
3613
|
}
|
|
4233
3614
|
async function cleanup(state, opts = {}) {
|
|
4234
3615
|
state.running = false;
|
|
4235
|
-
for (const timer of state.sessionCleanupTimers) {
|
|
4236
|
-
clearInterval(timer);
|
|
4237
|
-
clearTimeout(timer);
|
|
4238
|
-
}
|
|
4239
|
-
state.sessionCleanupTimers = [];
|
|
4240
3616
|
if (opts.graceful && state.channelDriver) {
|
|
4241
3617
|
state.channelDriver.stop();
|
|
4242
3618
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
@@ -4290,7 +3666,6 @@ async function run(options) {
|
|
|
4290
3666
|
activityLog: [],
|
|
4291
3667
|
messageCount: 0,
|
|
4292
3668
|
lastProxiedActivityAt: null,
|
|
4293
|
-
sessionCleanupTimers: [],
|
|
4294
3669
|
authHeader: ""
|
|
4295
3670
|
};
|
|
4296
3671
|
if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
|
|
@@ -4533,7 +3908,6 @@ async function run(options) {
|
|
|
4533
3908
|
if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
|
|
4534
3909
|
throw error2;
|
|
4535
3910
|
}
|
|
4536
|
-
scheduleSessionCleanup(state, channelDriver, options);
|
|
4537
3911
|
if (!interactive || state.json) {
|
|
4538
3912
|
log2(state, "Driving channel messages...");
|
|
4539
3913
|
}
|
|
@@ -4588,16 +3962,7 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
|
|
|
4588
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);
|
|
4589
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 }));
|
|
4590
3964
|
program.command("whoami").description("Show the currently logged in user").action(whoami);
|
|
4591
|
-
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").
|
|
4592
|
-
"--session-cleanup-max-age <duration>",
|
|
4593
|
-
"Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
|
|
4594
|
-
).option(
|
|
4595
|
-
"--session-cleanup-max-count <n>",
|
|
4596
|
-
"Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
|
|
4597
|
-
).option(
|
|
4598
|
-
"--session-cleanup-interval <duration>",
|
|
4599
|
-
"How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
|
|
4600
|
-
).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(
|
|
4601
3966
|
(options) => {
|
|
4602
3967
|
run({
|
|
4603
3968
|
agent: options.agent,
|
|
@@ -4605,11 +3970,7 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
4605
3970
|
verbose: options.verbose,
|
|
4606
3971
|
conversation: options.conversation,
|
|
4607
3972
|
idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
|
|
4608
|
-
json: options.json
|
|
4609
|
-
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
4610
|
-
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
4611
|
-
sessionCleanupMaxCount: options.sessionCleanupMaxCount,
|
|
4612
|
-
sessionCleanupInterval: options.sessionCleanupInterval
|
|
3973
|
+
json: options.json
|
|
4613
3974
|
});
|
|
4614
3975
|
}
|
|
4615
3976
|
);
|