@evident-ai/cli 3.0.1-dev.65c0838 → 3.0.1-dev.6671916
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 +1372 -94
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
+
import { createRequire } from "module";
|
|
4
5
|
import { Command } from "commander";
|
|
5
6
|
|
|
6
7
|
// src/commands/login.ts
|
|
@@ -284,14 +285,14 @@ function blank() {
|
|
|
284
285
|
console.log();
|
|
285
286
|
}
|
|
286
287
|
function waitForEnter(prompt = "Press Enter to continue...") {
|
|
287
|
-
return new Promise((
|
|
288
|
+
return new Promise((resolve2) => {
|
|
288
289
|
process.stdout.write(chalk.dim(prompt));
|
|
289
290
|
const handler = () => {
|
|
290
291
|
process.stdin.removeListener("data", handler);
|
|
291
292
|
process.stdin.setRawMode?.(false);
|
|
292
293
|
process.stdin.pause();
|
|
293
294
|
console.log();
|
|
294
|
-
|
|
295
|
+
resolve2();
|
|
295
296
|
};
|
|
296
297
|
if (process.stdin.isTTY) {
|
|
297
298
|
process.stdin.setRawMode?.(true);
|
|
@@ -301,7 +302,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
|
|
|
301
302
|
});
|
|
302
303
|
}
|
|
303
304
|
function sleep(ms) {
|
|
304
|
-
return new Promise((
|
|
305
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
305
306
|
}
|
|
306
307
|
|
|
307
308
|
// src/commands/login.ts
|
|
@@ -375,19 +376,19 @@ async function tokenLogin() {
|
|
|
375
376
|
console.log("Visit your Evident dashboard to generate a CLI token.");
|
|
376
377
|
blank();
|
|
377
378
|
process.stdout.write("Paste token: ");
|
|
378
|
-
const token = await new Promise((
|
|
379
|
+
const token = await new Promise((resolve2) => {
|
|
379
380
|
let data = "";
|
|
380
381
|
process.stdin.setEncoding("utf8");
|
|
381
382
|
process.stdin.on("data", (chunk) => {
|
|
382
383
|
data += chunk;
|
|
383
384
|
});
|
|
384
385
|
process.stdin.on("end", () => {
|
|
385
|
-
|
|
386
|
+
resolve2(data.trim());
|
|
386
387
|
});
|
|
387
388
|
if (process.stdin.isTTY) {
|
|
388
389
|
process.stdin.once("data", (chunk) => {
|
|
389
390
|
process.stdin.pause();
|
|
390
|
-
|
|
391
|
+
resolve2(chunk.toString().trim());
|
|
391
392
|
});
|
|
392
393
|
process.stdin.resume();
|
|
393
394
|
}
|
|
@@ -484,8 +485,34 @@ var TelemetryEventTypes = {
|
|
|
484
485
|
var MAX_FRAME_BYTES = 256 * 1024;
|
|
485
486
|
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
486
487
|
|
|
488
|
+
// ../../packages/types/src/logging/index.ts
|
|
489
|
+
var CORRELATION_ID_HEADER = "x-evident-correlation-id";
|
|
490
|
+
function log(level, event, fields) {
|
|
491
|
+
const method = level === "debug" ? "log" : level;
|
|
492
|
+
try {
|
|
493
|
+
console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
|
|
494
|
+
} catch (err) {
|
|
495
|
+
console.error(
|
|
496
|
+
"[evident] log_serialize_failed",
|
|
497
|
+
event,
|
|
498
|
+
err instanceof Error ? err.message : String(err)
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function stripQuery(url) {
|
|
503
|
+
try {
|
|
504
|
+
return new URL(url).pathname;
|
|
505
|
+
} catch {
|
|
506
|
+
const q = url.indexOf("?");
|
|
507
|
+
return q === -1 ? url : url.slice(0, q);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
487
511
|
// src/lib/telemetry.ts
|
|
488
|
-
var CLI_VERSION = process.env.npm_package_version
|
|
512
|
+
var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
513
|
+
function getCliVersion() {
|
|
514
|
+
return CLI_VERSION;
|
|
515
|
+
}
|
|
489
516
|
var eventBuffer = [];
|
|
490
517
|
var flushTimeout = null;
|
|
491
518
|
var isShuttingDown = false;
|
|
@@ -679,20 +706,20 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
679
706
|
if (health.healthy) {
|
|
680
707
|
return health;
|
|
681
708
|
}
|
|
682
|
-
await new Promise((
|
|
709
|
+
await new Promise((resolve2) => setTimeout(resolve2, 1e3));
|
|
683
710
|
}
|
|
684
711
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
685
712
|
}
|
|
686
713
|
|
|
687
714
|
// src/lib/opencode/opencode-version-gate.ts
|
|
688
|
-
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11"];
|
|
689
|
-
function isQueueValidatedVersion(
|
|
690
|
-
if (!
|
|
691
|
-
return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(
|
|
692
|
-
}
|
|
693
|
-
function buildOpenCodeVersionWarning(
|
|
694
|
-
if (isQueueValidatedVersion(
|
|
695
|
-
const detected =
|
|
715
|
+
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
716
|
+
function isQueueValidatedVersion(version2) {
|
|
717
|
+
if (!version2) return false;
|
|
718
|
+
return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
|
|
719
|
+
}
|
|
720
|
+
function buildOpenCodeVersionWarning(version2) {
|
|
721
|
+
if (isQueueValidatedVersion(version2)) return null;
|
|
722
|
+
const detected = version2 ? `v${version2}` : "unknown";
|
|
696
723
|
const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
|
|
697
724
|
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack/WhatsApp) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
|
|
698
725
|
}
|
|
@@ -1009,7 +1036,11 @@ function roleOf(m) {
|
|
|
1009
1036
|
}
|
|
1010
1037
|
function completedOf(m) {
|
|
1011
1038
|
if (!m || typeof m !== "object") return void 0;
|
|
1012
|
-
return m.info?.time?.completed;
|
|
1039
|
+
return m.info?.time?.completed ?? m.time?.completed;
|
|
1040
|
+
}
|
|
1041
|
+
function createdOf(m) {
|
|
1042
|
+
if (!m || typeof m !== "object") return void 0;
|
|
1043
|
+
return m.info?.time?.created ?? m.time?.created;
|
|
1013
1044
|
}
|
|
1014
1045
|
function idOf(m) {
|
|
1015
1046
|
if (!m || typeof m !== "object") return void 0;
|
|
@@ -1023,6 +1054,72 @@ function parentIdOf(m) {
|
|
|
1023
1054
|
const infoParent = m.info?.parentID;
|
|
1024
1055
|
return typeof infoParent === "string" ? infoParent : void 0;
|
|
1025
1056
|
}
|
|
1057
|
+
function finishOf(m) {
|
|
1058
|
+
if (!m || typeof m !== "object") return void 0;
|
|
1059
|
+
if (typeof m.finish === "string") return m.finish;
|
|
1060
|
+
const infoFinish = m.info?.finish;
|
|
1061
|
+
return typeof infoFinish === "string" ? infoFinish : void 0;
|
|
1062
|
+
}
|
|
1063
|
+
function errorOf(m) {
|
|
1064
|
+
if (!m || typeof m !== "object") return void 0;
|
|
1065
|
+
return m.info?.error ?? m.error;
|
|
1066
|
+
}
|
|
1067
|
+
function isAssistantInFlight(m) {
|
|
1068
|
+
if (completedOf(m) == null) return true;
|
|
1069
|
+
return finishOf(m) === "tool-calls";
|
|
1070
|
+
}
|
|
1071
|
+
async function getSessionMessages(port, sessionId) {
|
|
1072
|
+
try {
|
|
1073
|
+
const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/message`);
|
|
1074
|
+
if (!res.ok) return null;
|
|
1075
|
+
const body = await res.json();
|
|
1076
|
+
return Array.isArray(body) ? body : null;
|
|
1077
|
+
} catch {
|
|
1078
|
+
return null;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function sessionLastActivityMs(session) {
|
|
1082
|
+
const candidates = [
|
|
1083
|
+
session.time?.updated,
|
|
1084
|
+
session.time?.created,
|
|
1085
|
+
session.time_updated,
|
|
1086
|
+
session.time_created,
|
|
1087
|
+
session.updated,
|
|
1088
|
+
session.created
|
|
1089
|
+
];
|
|
1090
|
+
for (const c of candidates) {
|
|
1091
|
+
if (typeof c === "number" && Number.isFinite(c)) return c;
|
|
1092
|
+
}
|
|
1093
|
+
return null;
|
|
1094
|
+
}
|
|
1095
|
+
async function listSessions(port) {
|
|
1096
|
+
try {
|
|
1097
|
+
const res = await fetch(`${opencodeBase(port)}/session`);
|
|
1098
|
+
if (!res.ok) return null;
|
|
1099
|
+
const body = await res.json();
|
|
1100
|
+
return Array.isArray(body) ? body : null;
|
|
1101
|
+
} catch {
|
|
1102
|
+
return null;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
async function deleteSession(port, id) {
|
|
1106
|
+
try {
|
|
1107
|
+
const res = await fetch(`${opencodeBase(port)}/session/${id}`, { method: "DELETE" });
|
|
1108
|
+
return res.status >= 200 && res.status < 300;
|
|
1109
|
+
} catch {
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
async function sessionExists(port, id) {
|
|
1114
|
+
try {
|
|
1115
|
+
const res = await fetch(`${opencodeBase(port)}/session/${id}`);
|
|
1116
|
+
if (res.status >= 200 && res.status < 300) return true;
|
|
1117
|
+
if (res.status === 404) return false;
|
|
1118
|
+
return null;
|
|
1119
|
+
} catch {
|
|
1120
|
+
return null;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1026
1123
|
async function createOpenCodeSession(port, directory) {
|
|
1027
1124
|
const url = new URL(`${opencodeBase(port)}/session`);
|
|
1028
1125
|
if (directory && directory.trim()) {
|
|
@@ -1040,9 +1137,16 @@ async function createOpenCodeSession(port, directory) {
|
|
|
1040
1137
|
const data = await response.json();
|
|
1041
1138
|
return data.id;
|
|
1042
1139
|
}
|
|
1043
|
-
|
|
1140
|
+
function messageText(m) {
|
|
1141
|
+
if (!m || !Array.isArray(m.parts)) return "";
|
|
1142
|
+
return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
1143
|
+
}
|
|
1144
|
+
async function sendPromptAsync(port, sessionId, content, options) {
|
|
1145
|
+
const before = await getSessionMessages(port, sessionId);
|
|
1146
|
+
const knownUserIds = new Set(
|
|
1147
|
+
(before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
|
|
1148
|
+
);
|
|
1044
1149
|
const body = {
|
|
1045
|
-
messageID: messageId,
|
|
1046
1150
|
parts: [{ type: "text", text: content }]
|
|
1047
1151
|
};
|
|
1048
1152
|
if (options?.agent) {
|
|
@@ -1066,6 +1170,29 @@ async function sendPromptAsync(port, sessionId, content, options, messageId) {
|
|
|
1066
1170
|
const text = await res.text().catch(() => "");
|
|
1067
1171
|
throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
|
|
1068
1172
|
}
|
|
1173
|
+
const READ_BACK_ATTEMPTS = 5;
|
|
1174
|
+
const READ_BACK_DELAY_MS = 150;
|
|
1175
|
+
for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
|
|
1176
|
+
const after = await getSessionMessages(port, sessionId);
|
|
1177
|
+
if (after) {
|
|
1178
|
+
let best = null;
|
|
1179
|
+
for (const m of after) {
|
|
1180
|
+
if (roleOf(m) !== "user") continue;
|
|
1181
|
+
const id = idOf(m);
|
|
1182
|
+
if (typeof id !== "string" || knownUserIds.has(id)) continue;
|
|
1183
|
+
if (messageText(m) !== content) continue;
|
|
1184
|
+
const created = createdOf(m) ?? 0;
|
|
1185
|
+
if (best === null || created > best.created) {
|
|
1186
|
+
best = { id, created };
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
if (best) return best.id;
|
|
1190
|
+
}
|
|
1191
|
+
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
1192
|
+
await new Promise((resolve2) => setTimeout(resolve2, READ_BACK_DELAY_MS));
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
return null;
|
|
1069
1196
|
}
|
|
1070
1197
|
function findAssistantReplyAfter(messages, userMessageId) {
|
|
1071
1198
|
if (!messages || messages.length === 0) return null;
|
|
@@ -1080,19 +1207,167 @@ function findAssistantReplyAfter(messages, userMessageId) {
|
|
|
1080
1207
|
}
|
|
1081
1208
|
return null;
|
|
1082
1209
|
}
|
|
1210
|
+
function findLastAssistantReplyFor(messages, userMessageId) {
|
|
1211
|
+
if (!messages || messages.length === 0) return null;
|
|
1212
|
+
let lastCorrelated = null;
|
|
1213
|
+
let lastNonErrored = null;
|
|
1214
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1215
|
+
const m = messages[i];
|
|
1216
|
+
if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
|
|
1217
|
+
if (lastCorrelated === null) lastCorrelated = m;
|
|
1218
|
+
if (errorOf(m) == null) {
|
|
1219
|
+
lastNonErrored = m;
|
|
1220
|
+
break;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
|
|
1224
|
+
const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
|
|
1225
|
+
if (userIndex === -1) return null;
|
|
1226
|
+
let last = null;
|
|
1227
|
+
let lastOk = null;
|
|
1228
|
+
for (let i = userIndex + 1; i < messages.length; i++) {
|
|
1229
|
+
const role = roleOf(messages[i]);
|
|
1230
|
+
if (role === "user") break;
|
|
1231
|
+
if (role === "assistant") {
|
|
1232
|
+
last = messages[i];
|
|
1233
|
+
if (errorOf(messages[i]) == null) lastOk = messages[i];
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
return lastOk ?? last;
|
|
1237
|
+
}
|
|
1083
1238
|
function messageRunState(messages, userMessageId) {
|
|
1084
1239
|
if (!messages || messages.length === 0) return "unknown";
|
|
1085
1240
|
const hasUser = messages.some((m) => idOf(m) === userMessageId);
|
|
1086
|
-
const reply =
|
|
1241
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1087
1242
|
if (!hasUser) {
|
|
1088
1243
|
if (!reply) return "unknown";
|
|
1089
1244
|
}
|
|
1090
1245
|
if (!reply) return "queued";
|
|
1091
|
-
|
|
1246
|
+
if (isAssistantInFlight(reply)) return "running";
|
|
1247
|
+
return errorOf(reply) != null ? "failed" : "done";
|
|
1248
|
+
}
|
|
1249
|
+
function messageError(messages, userMessageId) {
|
|
1250
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1251
|
+
const error2 = errorOf(reply);
|
|
1252
|
+
if (error2 == null) return null;
|
|
1253
|
+
if (typeof error2 === "string") return error2;
|
|
1254
|
+
if (typeof error2 === "object") {
|
|
1255
|
+
const e = error2;
|
|
1256
|
+
const dataMessage = e.data?.message;
|
|
1257
|
+
if (typeof dataMessage === "string") return dataMessage;
|
|
1258
|
+
if (typeof e.message === "string") return e.message;
|
|
1259
|
+
}
|
|
1260
|
+
return "The agent run failed.";
|
|
1261
|
+
}
|
|
1262
|
+
function hasRunningAssistantExcept(messages, exceptUserMessageId) {
|
|
1263
|
+
if (!messages || messages.length === 0) return false;
|
|
1264
|
+
return messages.some(
|
|
1265
|
+
(m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
// src/lib/opencode/session-cleanup.ts
|
|
1270
|
+
var DURATION_UNIT_MS = {
|
|
1271
|
+
s: 1e3,
|
|
1272
|
+
m: 60 * 1e3,
|
|
1273
|
+
h: 60 * 60 * 1e3,
|
|
1274
|
+
d: 24 * 60 * 60 * 1e3
|
|
1275
|
+
};
|
|
1276
|
+
function parseDurationMs(input) {
|
|
1277
|
+
const trimmed = input.trim();
|
|
1278
|
+
const match = /^(\d+)([smhd])$/.exec(trimmed);
|
|
1279
|
+
if (!match) {
|
|
1280
|
+
throw new Error(
|
|
1281
|
+
`Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
const value = Number(match[1]);
|
|
1285
|
+
if (value <= 0) {
|
|
1286
|
+
throw new Error(`Invalid duration "${input}": must be a positive value.`);
|
|
1287
|
+
}
|
|
1288
|
+
return value * DURATION_UNIT_MS[match[2]];
|
|
1289
|
+
}
|
|
1290
|
+
function selectSessionsToDelete(sessions, opts) {
|
|
1291
|
+
const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
|
|
1292
|
+
if (maxAgeMs === void 0 && maxCount === void 0) return [];
|
|
1293
|
+
const ageEligible = (s) => {
|
|
1294
|
+
if (maxAgeMs === void 0) return false;
|
|
1295
|
+
if (s.lastActivityMs === null) return true;
|
|
1296
|
+
return nowMs - s.lastActivityMs > maxAgeMs;
|
|
1297
|
+
};
|
|
1298
|
+
const countEligibleIds = /* @__PURE__ */ new Set();
|
|
1299
|
+
if (maxCount !== void 0) {
|
|
1300
|
+
const byActivityDesc = [...sessions].sort(
|
|
1301
|
+
(a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
|
|
1302
|
+
);
|
|
1303
|
+
for (const s of byActivityDesc.slice(maxCount)) {
|
|
1304
|
+
countEligibleIds.add(s.id);
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
const toDelete = [];
|
|
1308
|
+
for (const s of sessions) {
|
|
1309
|
+
if (protectedIds.has(s.id)) continue;
|
|
1310
|
+
if (ageEligible(s) || countEligibleIds.has(s.id)) {
|
|
1311
|
+
toDelete.push(s.id);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
return toDelete;
|
|
1315
|
+
}
|
|
1316
|
+
var DEFAULT_INTERVAL = "1h";
|
|
1317
|
+
function resolve(flag, envValue, fallback) {
|
|
1318
|
+
return flag ?? envValue ?? fallback;
|
|
1319
|
+
}
|
|
1320
|
+
function parseMaxCount(input) {
|
|
1321
|
+
const trimmed = input.trim();
|
|
1322
|
+
if (!/^\d+$/.test(trimmed)) {
|
|
1323
|
+
throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
|
|
1324
|
+
}
|
|
1325
|
+
const value = Number(trimmed);
|
|
1326
|
+
if (value <= 0) {
|
|
1327
|
+
throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
|
|
1328
|
+
}
|
|
1329
|
+
return value;
|
|
1092
1330
|
}
|
|
1093
|
-
function
|
|
1094
|
-
const
|
|
1095
|
-
|
|
1331
|
+
function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
1332
|
+
const warnings = [];
|
|
1333
|
+
const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
|
|
1334
|
+
const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
|
|
1335
|
+
const intervalRaw = resolve(
|
|
1336
|
+
flags.interval,
|
|
1337
|
+
env.EVIDENT_SESSION_CLEANUP_INTERVAL,
|
|
1338
|
+
DEFAULT_INTERVAL
|
|
1339
|
+
);
|
|
1340
|
+
let maxAgeMs;
|
|
1341
|
+
if (maxAgeRaw !== void 0) {
|
|
1342
|
+
try {
|
|
1343
|
+
maxAgeMs = parseDurationMs(maxAgeRaw);
|
|
1344
|
+
} catch (err) {
|
|
1345
|
+
warnings.push(
|
|
1346
|
+
`Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
|
|
1347
|
+
);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
let maxCount;
|
|
1351
|
+
if (maxCountRaw !== void 0) {
|
|
1352
|
+
try {
|
|
1353
|
+
maxCount = parseMaxCount(maxCountRaw);
|
|
1354
|
+
} catch (err) {
|
|
1355
|
+
warnings.push(
|
|
1356
|
+
`Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
|
|
1357
|
+
);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
let intervalMs;
|
|
1361
|
+
try {
|
|
1362
|
+
intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
|
|
1363
|
+
} catch (err) {
|
|
1364
|
+
warnings.push(
|
|
1365
|
+
`Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
|
|
1366
|
+
);
|
|
1367
|
+
intervalMs = parseDurationMs(DEFAULT_INTERVAL);
|
|
1368
|
+
}
|
|
1369
|
+
const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
|
|
1370
|
+
return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
|
|
1096
1371
|
}
|
|
1097
1372
|
|
|
1098
1373
|
// src/lib/tunnel/connection.ts
|
|
@@ -1163,24 +1438,34 @@ var StreamForwarder = class {
|
|
|
1163
1438
|
}
|
|
1164
1439
|
async handleOpen(frame) {
|
|
1165
1440
|
const { sid, method, path, headers, has_body } = frame;
|
|
1441
|
+
const correlationId = headers?.[CORRELATION_ID_HEADER];
|
|
1442
|
+
const startedAt = Date.now();
|
|
1166
1443
|
if (path === TUNNEL_DRAIN_PING_PATH) {
|
|
1167
1444
|
this.callbacks.onDrainPing?.();
|
|
1168
1445
|
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
1169
1446
|
this.send({ type: "res_end", sid });
|
|
1170
1447
|
return;
|
|
1171
1448
|
}
|
|
1449
|
+
if (process.env.DEBUG) {
|
|
1450
|
+
log("debug", "agent_request", {
|
|
1451
|
+
correlation_id: correlationId,
|
|
1452
|
+
sid,
|
|
1453
|
+
method,
|
|
1454
|
+
path: stripQuery(path)
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1172
1457
|
const ac = new AbortController();
|
|
1173
1458
|
let bodyPromise;
|
|
1174
1459
|
let pushBody;
|
|
1175
1460
|
let endBody;
|
|
1176
1461
|
if (has_body) {
|
|
1177
1462
|
const chunks = [];
|
|
1178
|
-
bodyPromise = new Promise((
|
|
1463
|
+
bodyPromise = new Promise((resolve2) => {
|
|
1179
1464
|
pushBody = (buf) => {
|
|
1180
1465
|
chunks.push(buf);
|
|
1181
1466
|
};
|
|
1182
1467
|
endBody = () => {
|
|
1183
|
-
|
|
1468
|
+
resolve2(Buffer.concat(chunks));
|
|
1184
1469
|
};
|
|
1185
1470
|
});
|
|
1186
1471
|
}
|
|
@@ -1215,6 +1500,14 @@ var StreamForwarder = class {
|
|
|
1215
1500
|
if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
|
|
1216
1501
|
});
|
|
1217
1502
|
this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
|
|
1503
|
+
if (process.env.DEBUG) {
|
|
1504
|
+
log("debug", "agent_response", {
|
|
1505
|
+
correlation_id: correlationId,
|
|
1506
|
+
sid,
|
|
1507
|
+
status: upstream.status,
|
|
1508
|
+
duration_ms: Date.now() - startedAt
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1218
1511
|
this.callbacks.onHead?.(sid, upstream.status);
|
|
1219
1512
|
try {
|
|
1220
1513
|
if (upstream.body) {
|
|
@@ -1290,7 +1583,7 @@ function connectTunnel(options) {
|
|
|
1290
1583
|
} = options;
|
|
1291
1584
|
const tunnelUrl = getTunnelUrlConfig();
|
|
1292
1585
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
1293
|
-
return new Promise((
|
|
1586
|
+
return new Promise((resolve2, reject) => {
|
|
1294
1587
|
const ws = new WebSocket2(url, {
|
|
1295
1588
|
headers: {
|
|
1296
1589
|
Authorization: authHeader
|
|
@@ -1355,7 +1648,7 @@ function connectTunnel(options) {
|
|
|
1355
1648
|
clearTimeout(connectionTimeout);
|
|
1356
1649
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
1357
1650
|
onConnected?.(connectedAgentId);
|
|
1358
|
-
|
|
1651
|
+
resolve2({
|
|
1359
1652
|
ws,
|
|
1360
1653
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
1361
1654
|
});
|
|
@@ -1484,7 +1777,7 @@ var DEFAULT_RETRY_POLICY = {
|
|
|
1484
1777
|
};
|
|
1485
1778
|
var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
|
|
1486
1779
|
var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
1487
|
-
var
|
|
1780
|
+
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
1488
1781
|
var ChannelAuthError = class extends Error {
|
|
1489
1782
|
constructor(message) {
|
|
1490
1783
|
super(message);
|
|
@@ -1519,10 +1812,18 @@ var ChannelDriver = class {
|
|
|
1519
1812
|
sleep;
|
|
1520
1813
|
pausedPollIntervalMs;
|
|
1521
1814
|
pausedMaxWaitMs;
|
|
1522
|
-
|
|
1815
|
+
stuckQueuedMs;
|
|
1523
1816
|
now;
|
|
1524
1817
|
/** Cache of conversationId → opencode sessionId. */
|
|
1525
1818
|
sessions = /* @__PURE__ */ new Map();
|
|
1819
|
+
/**
|
|
1820
|
+
* Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
|
|
1821
|
+
* longer idempotent (no caller-supplied `messageID`), and its read-back picks
|
|
1822
|
+
* "the one new user row" — which is only unambiguous if no OTHER dispatch into
|
|
1823
|
+
* the SAME session interleaves its snapshot→POST→read-back. This map chains each
|
|
1824
|
+
* session's dispatches so they run serially; distinct sessions stay concurrent.
|
|
1825
|
+
*/
|
|
1826
|
+
sessionDispatchLocks = /* @__PURE__ */ new Map();
|
|
1526
1827
|
/**
|
|
1527
1828
|
* Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
|
|
1528
1829
|
* session: one polling loop services all of that session's in-flight messages.
|
|
@@ -1539,6 +1840,54 @@ var ChannelDriver = class {
|
|
|
1539
1840
|
* a steady-state-poll re-dispatch will not double-run the message.
|
|
1540
1841
|
*/
|
|
1541
1842
|
dispatched = /* @__PURE__ */ new Set();
|
|
1843
|
+
/**
|
|
1844
|
+
* Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
|
|
1845
|
+
* Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
|
|
1846
|
+
* so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
|
|
1847
|
+
* it is re-adopted and removed when its watcher settles or it is observed off
|
|
1848
|
+
* the processing list.
|
|
1849
|
+
*/
|
|
1850
|
+
readopted = /* @__PURE__ */ new Set();
|
|
1851
|
+
/**
|
|
1852
|
+
* "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
|
|
1853
|
+
* re-adopted running/orphan row's watcher hit its `processed_at`-anchored
|
|
1854
|
+
* deadline (or an orphan whose window already elapsed): the still-`processing`
|
|
1855
|
+
* server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
|
|
1856
|
+
* drain until the 15-min cron resets it — spamming new turns.
|
|
1857
|
+
*
|
|
1858
|
+
* CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
|
|
1859
|
+
* does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
|
|
1860
|
+
* in opencode must still be delivered via `markDone` on the next drain — so
|
|
1861
|
+
* `readoptOne` computes `state` FIRST and this set is checked only on the
|
|
1862
|
+
* non-done path. It is cleared once the row leaves the processing list (cron
|
|
1863
|
+
* reset → it drains normally as `pending`), so it can never leak.
|
|
1864
|
+
*/
|
|
1865
|
+
dontRedispatch = /* @__PURE__ */ new Set();
|
|
1866
|
+
/**
|
|
1867
|
+
* "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
|
|
1868
|
+
* re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
|
|
1869
|
+
* never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
|
|
1870
|
+
* that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
|
|
1871
|
+
* markDone failure must NOT land here (it must still retry next drain). Separate
|
|
1872
|
+
* from `dontRedispatch` because the two concerns are independent: a row can need
|
|
1873
|
+
* "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
|
|
1874
|
+
* the row leaves the processing list, exactly like `dontRedispatch`.
|
|
1875
|
+
*/
|
|
1876
|
+
doneUndeliverable = /* @__PURE__ */ new Set();
|
|
1877
|
+
/**
|
|
1878
|
+
* "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
|
|
1879
|
+
* Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
|
|
1880
|
+
* is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
|
|
1881
|
+
* persist hasn't landed before tick N+1 re-reads the still-null
|
|
1882
|
+
* `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
|
|
1883
|
+
* A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
|
|
1884
|
+
* short-circuits while it is present, so a null-id row is re-dispatched AT MOST
|
|
1885
|
+
* ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
|
|
1886
|
+
* row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
|
|
1887
|
+
* re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
|
|
1888
|
+
* so the NEXT tick may retry exactly once more).
|
|
1889
|
+
*/
|
|
1890
|
+
awaitingReadopt = /* @__PURE__ */ new Set();
|
|
1542
1891
|
/**
|
|
1543
1892
|
* Cache of the opencode root directory (from `GET /path`). Resolved lazily on
|
|
1544
1893
|
* first session creation so drain-created sessions are rooted at the project
|
|
@@ -1546,8 +1895,33 @@ var ChannelDriver = class {
|
|
|
1546
1895
|
* not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
|
|
1547
1896
|
*/
|
|
1548
1897
|
opencodeDirectory = void 0;
|
|
1898
|
+
/**
|
|
1899
|
+
* Cache of opencode `sessionId → parentID` (its parent session, or `null` when
|
|
1900
|
+
* the session is a root with no parent). Sub-agents spawned via the `task` tool
|
|
1901
|
+
* run in CHILD sessions whose `parentID` chains up to the Evident-created
|
|
1902
|
+
* (watched) session; we resolve this once per session so a child-session
|
|
1903
|
+
* question/permission can be attributed to the watched session's subtree
|
|
1904
|
+
* (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
|
|
1905
|
+
* entry = not yet resolved; `null` = resolved root (stop walking).
|
|
1906
|
+
*/
|
|
1907
|
+
sessionParents = /* @__PURE__ */ new Map();
|
|
1549
1908
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
1550
1909
|
draining = false;
|
|
1910
|
+
/**
|
|
1911
|
+
* The currently-executing `drainPending()` promise, or null when idle. Lets a
|
|
1912
|
+
* graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
|
|
1913
|
+
* is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
|
|
1914
|
+
* drain that entered before `stop()` still registers its watcher).
|
|
1915
|
+
*/
|
|
1916
|
+
activeDrain = null;
|
|
1917
|
+
/**
|
|
1918
|
+
* Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
|
|
1919
|
+
* dispatches NEW work (it returns 0 immediately) — but the per-session watcher
|
|
1920
|
+
* loops already running keep going so in-flight turns can finish and deliver
|
|
1921
|
+
* their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
|
|
1922
|
+
* and stops opencode.
|
|
1923
|
+
*/
|
|
1924
|
+
stopped = false;
|
|
1551
1925
|
constructor(config2) {
|
|
1552
1926
|
this.agentId = config2.agentId;
|
|
1553
1927
|
this.port = config2.port;
|
|
@@ -1561,7 +1935,7 @@ var ChannelDriver = class {
|
|
|
1561
1935
|
this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
1562
1936
|
this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
|
|
1563
1937
|
this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
|
|
1564
|
-
this.
|
|
1938
|
+
this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
1565
1939
|
this.now = config2.now ?? (() => Date.now());
|
|
1566
1940
|
}
|
|
1567
1941
|
/** The IPv4-loopback base URL for the local `opencode serve`. */
|
|
@@ -1579,8 +1953,21 @@ var ChannelDriver = class {
|
|
|
1579
1953
|
* @returns the number of messages NEWLY dispatched to opencode's native queue.
|
|
1580
1954
|
*/
|
|
1581
1955
|
async drainPending() {
|
|
1956
|
+
if (this.stopped) return 0;
|
|
1582
1957
|
if (this.draining) return 0;
|
|
1583
1958
|
this.draining = true;
|
|
1959
|
+
const run2 = this.runDrain();
|
|
1960
|
+
this.activeDrain = run2.then(
|
|
1961
|
+
() => {
|
|
1962
|
+
this.activeDrain = null;
|
|
1963
|
+
},
|
|
1964
|
+
() => {
|
|
1965
|
+
this.activeDrain = null;
|
|
1966
|
+
}
|
|
1967
|
+
);
|
|
1968
|
+
return run2;
|
|
1969
|
+
}
|
|
1970
|
+
async runDrain() {
|
|
1584
1971
|
let dispatched = 0;
|
|
1585
1972
|
try {
|
|
1586
1973
|
const conversations = await this.getPendingConversations();
|
|
@@ -1592,8 +1979,10 @@ var ChannelDriver = class {
|
|
|
1592
1979
|
});
|
|
1593
1980
|
}
|
|
1594
1981
|
for (const conv of conversations) {
|
|
1982
|
+
if (this.stopped) break;
|
|
1595
1983
|
dispatched += await this.processConversation(conv);
|
|
1596
1984
|
}
|
|
1985
|
+
await this.readoptProcessing();
|
|
1597
1986
|
} finally {
|
|
1598
1987
|
this.draining = false;
|
|
1599
1988
|
}
|
|
@@ -1611,6 +2000,73 @@ var ChannelDriver = class {
|
|
|
1611
2000
|
}
|
|
1612
2001
|
return false;
|
|
1613
2002
|
}
|
|
2003
|
+
/**
|
|
2004
|
+
* OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
|
|
2005
|
+
* exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
|
|
2006
|
+
* `watchers` entry whose `inFlight` set is non-empty — the same predicate
|
|
2007
|
+
* `hasInFlightWatchers()` uses, lifted to return the ids.
|
|
2008
|
+
*
|
|
2009
|
+
* Deliberately does NOT include `this.sessions` (the permanent, never-pruned
|
|
2010
|
+
* conversation→session cache). Protecting every bound-but-idle session there
|
|
2011
|
+
* would shield nearly every session and defeat cleanup — AND it is unnecessary:
|
|
2012
|
+
* `ensureSession` is self-healing (it recreates a session whose id no longer
|
|
2013
|
+
* exists), so deleting an idle bound session is harmless — the conversation's
|
|
2014
|
+
* next turn transparently rebinds a fresh one. The only thing worth protecting
|
|
2015
|
+
* is a session with a turn ACTIVELY in flight right now: tearing that down
|
|
2016
|
+
* mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
|
|
2017
|
+
*/
|
|
2018
|
+
protectedSessionIds() {
|
|
2019
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2020
|
+
for (const [sessionId, watcher] of this.watchers) {
|
|
2021
|
+
if (watcher.inFlight.size > 0) ids.add(sessionId);
|
|
2022
|
+
}
|
|
2023
|
+
return ids;
|
|
2024
|
+
}
|
|
2025
|
+
/**
|
|
2026
|
+
* Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
|
|
2027
|
+
* this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
|
|
2028
|
+
* — but the watcher loops already tracking in-flight turns keep running, so a
|
|
2029
|
+
* turn that has finished (or is about to) still fires `markDone` and delivers
|
|
2030
|
+
* its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
|
|
2031
|
+
*/
|
|
2032
|
+
stop() {
|
|
2033
|
+
this.stopped = true;
|
|
2034
|
+
}
|
|
2035
|
+
/**
|
|
2036
|
+
* Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
|
|
2037
|
+
* graceful shutdown, so a turn whose reply is ready — or completes within the
|
|
2038
|
+
* window — is delivered before the process exits, instead of being cut off and
|
|
2039
|
+
* left for the ADR-0046 restart-recovery path.
|
|
2040
|
+
*
|
|
2041
|
+
* Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
|
|
2042
|
+
* far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
|
|
2043
|
+
* window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
|
|
2044
|
+
* set empties OR the timeout elapses. Anything still in flight at the timeout is
|
|
2045
|
+
* safe to abandon — it stays `processing` server-side and is re-adopted on the
|
|
2046
|
+
* next runner start (ADR-0046).
|
|
2047
|
+
*
|
|
2048
|
+
* @returns true if all in-flight work settled within the window; false if the
|
|
2049
|
+
* timeout elapsed with work still in flight.
|
|
2050
|
+
*/
|
|
2051
|
+
async waitForInFlight(timeoutMs) {
|
|
2052
|
+
const deadline = this.now() + timeoutMs;
|
|
2053
|
+
const step = Math.min(this.pausedPollIntervalMs, 250);
|
|
2054
|
+
if (this.activeDrain) {
|
|
2055
|
+
let drainSettled = false;
|
|
2056
|
+
void this.activeDrain.then(() => {
|
|
2057
|
+
drainSettled = true;
|
|
2058
|
+
});
|
|
2059
|
+
while (!drainSettled) {
|
|
2060
|
+
if (this.now() >= deadline) return false;
|
|
2061
|
+
await this.sleep(step);
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
while (this.hasInFlightWatchers()) {
|
|
2065
|
+
if (this.now() >= deadline) return false;
|
|
2066
|
+
await this.sleep(step);
|
|
2067
|
+
}
|
|
2068
|
+
return true;
|
|
2069
|
+
}
|
|
1614
2070
|
/**
|
|
1615
2071
|
* Await all outstanding per-session watchers (WI-3).
|
|
1616
2072
|
*
|
|
@@ -1645,15 +2101,18 @@ var ChannelDriver = class {
|
|
|
1645
2101
|
const sessionId = await this.ensureSession(conv);
|
|
1646
2102
|
const messages = await this.getPendingMessages(conv.id);
|
|
1647
2103
|
let dispatched = 0;
|
|
2104
|
+
let skippedAlreadyDispatched = 0;
|
|
1648
2105
|
for (const message of messages) {
|
|
2106
|
+
if (this.stopped) break;
|
|
1649
2107
|
if (this.dispatched.has(message.id)) {
|
|
2108
|
+
skippedAlreadyDispatched += 1;
|
|
1650
2109
|
continue;
|
|
1651
2110
|
}
|
|
1652
|
-
const opencodeMessageId = opencodeMessageIdFor(message.id);
|
|
1653
2111
|
const options = {
|
|
1654
2112
|
agent: message.opencode_agent ?? void 0,
|
|
1655
2113
|
model: message.opencode_model ?? void 0
|
|
1656
2114
|
};
|
|
2115
|
+
let opencodeMessageId;
|
|
1657
2116
|
try {
|
|
1658
2117
|
this.log({
|
|
1659
2118
|
level: "info",
|
|
@@ -1661,10 +2120,23 @@ var ChannelDriver = class {
|
|
|
1661
2120
|
conversation_id: conv.id,
|
|
1662
2121
|
message_id: message.id
|
|
1663
2122
|
});
|
|
1664
|
-
await
|
|
2123
|
+
opencodeMessageId = await this.dispatchLocked(
|
|
2124
|
+
sessionId,
|
|
2125
|
+
() => sendPromptAsync(this.port, sessionId, message.content, options)
|
|
2126
|
+
);
|
|
1665
2127
|
} catch (err) {
|
|
1666
2128
|
if (err instanceof ChannelAuthError) throw err;
|
|
1667
2129
|
this.dispatched.delete(message.id);
|
|
2130
|
+
if (await sessionExists(this.port, sessionId) === false) {
|
|
2131
|
+
this.sessions.delete(conv.id);
|
|
2132
|
+
this.log({
|
|
2133
|
+
level: "info",
|
|
2134
|
+
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.`,
|
|
2135
|
+
conversation_id: conv.id,
|
|
2136
|
+
message_id: message.id
|
|
2137
|
+
});
|
|
2138
|
+
break;
|
|
2139
|
+
}
|
|
1668
2140
|
await this.markFailed(conv.id, message.id).catch(() => {
|
|
1669
2141
|
});
|
|
1670
2142
|
this.log({
|
|
@@ -1675,24 +2147,58 @@ var ChannelDriver = class {
|
|
|
1675
2147
|
});
|
|
1676
2148
|
continue;
|
|
1677
2149
|
}
|
|
2150
|
+
if (opencodeMessageId === null) {
|
|
2151
|
+
this.log({
|
|
2152
|
+
level: "error",
|
|
2153
|
+
message: `Message ${message.id.slice(0, 8)} dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next tick`,
|
|
2154
|
+
conversation_id: conv.id,
|
|
2155
|
+
message_id: message.id
|
|
2156
|
+
});
|
|
2157
|
+
continue;
|
|
2158
|
+
}
|
|
1678
2159
|
this.dispatched.add(message.id);
|
|
1679
2160
|
this.registerInFlight(conv, sessionId, message, opencodeMessageId);
|
|
1680
2161
|
dispatched += 1;
|
|
2162
|
+
void this.postSignal(conv.id, message.id, "dispatched");
|
|
2163
|
+
}
|
|
2164
|
+
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
2165
|
+
this.log({
|
|
2166
|
+
level: "error",
|
|
2167
|
+
message: `Conversation ${conv.id.slice(0, 8)} has ${messages.length} pending message(s) but ALL are already marked dispatched locally (in-flight set: ${this.dispatched.size}) \u2014 none sent to OpenCode this tick. If this repeats, a message may be stuck acknowledged-but-never-dispatched (its watcher never settled).`,
|
|
2168
|
+
conversation_id: conv.id
|
|
2169
|
+
});
|
|
1681
2170
|
}
|
|
1682
2171
|
this.ensureWatcherRunning(sessionId);
|
|
1683
2172
|
return dispatched;
|
|
1684
2173
|
}
|
|
1685
2174
|
async ensureSession(conv) {
|
|
1686
|
-
const
|
|
1687
|
-
if (
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
2175
|
+
const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
|
|
2176
|
+
if (bound) {
|
|
2177
|
+
const exists = await sessionExists(this.port, bound);
|
|
2178
|
+
if (exists === false) {
|
|
2179
|
+
this.log({
|
|
2180
|
+
level: "info",
|
|
2181
|
+
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.`,
|
|
2182
|
+
conversation_id: conv.id
|
|
2183
|
+
});
|
|
2184
|
+
this.sessions.delete(conv.id);
|
|
2185
|
+
return this.createAndBindSession(conv.id);
|
|
2186
|
+
}
|
|
2187
|
+
this.sessions.set(conv.id, bound);
|
|
2188
|
+
return bound;
|
|
1691
2189
|
}
|
|
2190
|
+
return this.createAndBindSession(conv.id);
|
|
2191
|
+
}
|
|
2192
|
+
/**
|
|
2193
|
+
* Create a fresh OpenCode session for a conversation, cache the binding, and
|
|
2194
|
+
* best-effort persist it server-side. Shared by the first-ever bind and the
|
|
2195
|
+
* self-heal recreate path in `ensureSession`.
|
|
2196
|
+
*/
|
|
2197
|
+
async createAndBindSession(conversationId) {
|
|
1692
2198
|
const directory = await this.resolveOpenCodeDirectory();
|
|
1693
2199
|
const sessionId = await createOpenCodeSession(this.port, directory);
|
|
1694
|
-
this.sessions.set(
|
|
1695
|
-
await this.persistSession(
|
|
2200
|
+
this.sessions.set(conversationId, sessionId);
|
|
2201
|
+
await this.persistSession(conversationId, sessionId).catch(() => {
|
|
1696
2202
|
});
|
|
1697
2203
|
return sessionId;
|
|
1698
2204
|
}
|
|
@@ -1715,6 +2221,25 @@ var ChannelDriver = class {
|
|
|
1715
2221
|
// -------------------------------------------------------------------------
|
|
1716
2222
|
// Per-session watcher (WI-3)
|
|
1717
2223
|
// -------------------------------------------------------------------------
|
|
2224
|
+
/**
|
|
2225
|
+
* Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
|
|
2226
|
+
* opencode session (Task 2.1a), so two dispatches into the SAME session can
|
|
2227
|
+
* never interleave and mis-correlate their read-backs. Distinct sessions run
|
|
2228
|
+
* concurrently. The chained tail intentionally ignores the prior result/error
|
|
2229
|
+
* (each dispatch reports its own outcome to its caller).
|
|
2230
|
+
*/
|
|
2231
|
+
dispatchLocked(sessionId, fn) {
|
|
2232
|
+
const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
|
|
2233
|
+
const run2 = prior.then(fn, fn);
|
|
2234
|
+
this.sessionDispatchLocks.set(
|
|
2235
|
+
sessionId,
|
|
2236
|
+
run2.then(
|
|
2237
|
+
() => void 0,
|
|
2238
|
+
() => void 0
|
|
2239
|
+
)
|
|
2240
|
+
);
|
|
2241
|
+
return run2;
|
|
2242
|
+
}
|
|
1718
2243
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
1719
2244
|
registerInFlight(conv, sessionId, message, opencodeMessageId) {
|
|
1720
2245
|
let watcher = this.watchers.get(sessionId);
|
|
@@ -1736,7 +2261,54 @@ var ChannelDriver = class {
|
|
|
1736
2261
|
dispatchedAt: now,
|
|
1737
2262
|
deadline: now + this.pausedMaxWaitMs,
|
|
1738
2263
|
started: false,
|
|
1739
|
-
done: false
|
|
2264
|
+
done: false,
|
|
2265
|
+
stuckReported: false
|
|
2266
|
+
});
|
|
2267
|
+
}
|
|
2268
|
+
/**
|
|
2269
|
+
* Register a RE-ADOPTED `processing` message with its session watcher
|
|
2270
|
+
* (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
|
|
2271
|
+
* `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
|
|
2272
|
+
* `now`: a row already `processing` for e.g. 5 min must give up ~5 min from now
|
|
2273
|
+
* (10 min after `processed_at`), not 10 min from now — otherwise its deadline
|
|
2274
|
+
* lands ~15 min after `processed_at`, coinciding with the cron reset →
|
|
2275
|
+
* double-drive race. `dispatchedAt` stays `now` (only the appear-guard uses it).
|
|
2276
|
+
*
|
|
2277
|
+
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
2278
|
+
* `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
|
|
2279
|
+
* fresh-run path these differ (a fresh opencode id under the same server row).
|
|
2280
|
+
*
|
|
2281
|
+
* `started` is set true so the watcher does NOT re-`markProcessing` a row the
|
|
2282
|
+
* server already flipped to `processing`; the running/done transitions still
|
|
2283
|
+
* fire from the watcher's normal branches.
|
|
2284
|
+
*/
|
|
2285
|
+
registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
|
|
2286
|
+
let watcher = this.watchers.get(sessionId);
|
|
2287
|
+
if (!watcher) {
|
|
2288
|
+
watcher = {
|
|
2289
|
+
conv,
|
|
2290
|
+
inFlight: /* @__PURE__ */ new Map(),
|
|
2291
|
+
loop: null,
|
|
2292
|
+
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2293
|
+
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2294
|
+
};
|
|
2295
|
+
this.watchers.set(sessionId, watcher);
|
|
2296
|
+
}
|
|
2297
|
+
watcher.inFlight.set(message.id, {
|
|
2298
|
+
evidentMessageId: message.id,
|
|
2299
|
+
opencodeMessageId,
|
|
2300
|
+
message,
|
|
2301
|
+
dispatchedAt: this.now(),
|
|
2302
|
+
deadline: processedAtMs + this.pausedMaxWaitMs,
|
|
2303
|
+
// The server row is ALREADY `processing`; do not re-fire markProcessing.
|
|
2304
|
+
started: true,
|
|
2305
|
+
done: false,
|
|
2306
|
+
// Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
|
|
2307
|
+
// AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
|
|
2308
|
+
// on `state === 'queued'` (turn produced no reply), not on `started`, so a
|
|
2309
|
+
// re-adopted row left wedged in `queued` still emits the signal once
|
|
2310
|
+
// (#210/#220 observability).
|
|
2311
|
+
stuckReported: false
|
|
1740
2312
|
});
|
|
1741
2313
|
}
|
|
1742
2314
|
/**
|
|
@@ -1802,6 +2374,7 @@ var ChannelDriver = class {
|
|
|
1802
2374
|
conversation_id: watcher.conv.id
|
|
1803
2375
|
});
|
|
1804
2376
|
for (const evidentMessageId of [...watcher.inFlight.keys()]) {
|
|
2377
|
+
this.readopted.delete(evidentMessageId);
|
|
1805
2378
|
this.removeInFlight(watcher, evidentMessageId);
|
|
1806
2379
|
}
|
|
1807
2380
|
return;
|
|
@@ -1822,10 +2395,15 @@ var ChannelDriver = class {
|
|
|
1822
2395
|
async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
|
|
1823
2396
|
const conv = watcher.conv;
|
|
1824
2397
|
const state = messageRunState(messages, inFlight.opencodeMessageId);
|
|
1825
|
-
if ((state === "running" || state === "done") && !inFlight.started) {
|
|
2398
|
+
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
1826
2399
|
let claimed;
|
|
1827
2400
|
try {
|
|
1828
|
-
claimed = await this.markProcessing(
|
|
2401
|
+
claimed = await this.markProcessing(
|
|
2402
|
+
conv.id,
|
|
2403
|
+
inFlight.evidentMessageId,
|
|
2404
|
+
sessionId,
|
|
2405
|
+
inFlight.opencodeMessageId
|
|
2406
|
+
);
|
|
1829
2407
|
} catch (err) {
|
|
1830
2408
|
if (err instanceof ChannelAuthError) throw err;
|
|
1831
2409
|
this.log({
|
|
@@ -1855,7 +2433,12 @@ var ChannelDriver = class {
|
|
|
1855
2433
|
message_id: inFlight.evidentMessageId
|
|
1856
2434
|
});
|
|
1857
2435
|
try {
|
|
1858
|
-
await this.markDone(
|
|
2436
|
+
await this.markDone(
|
|
2437
|
+
conv.id,
|
|
2438
|
+
inFlight.evidentMessageId,
|
|
2439
|
+
sessionId,
|
|
2440
|
+
inFlight.opencodeMessageId
|
|
2441
|
+
);
|
|
1859
2442
|
} catch (err) {
|
|
1860
2443
|
if (err instanceof ChannelAuthError) throw err;
|
|
1861
2444
|
if (err instanceof ChannelTerminalError) {
|
|
@@ -1891,10 +2474,59 @@ var ChannelDriver = class {
|
|
|
1891
2474
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
1892
2475
|
return;
|
|
1893
2476
|
}
|
|
1894
|
-
if (state === "
|
|
1895
|
-
if (
|
|
1896
|
-
|
|
2477
|
+
if (state === "failed") {
|
|
2478
|
+
if (!inFlight.done) {
|
|
2479
|
+
const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
|
|
2480
|
+
this.log({
|
|
2481
|
+
level: "error",
|
|
2482
|
+
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
2483
|
+
conversation_id: conv.id,
|
|
2484
|
+
message_id: inFlight.evidentMessageId
|
|
2485
|
+
});
|
|
2486
|
+
try {
|
|
2487
|
+
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
|
|
2488
|
+
} catch (err) {
|
|
2489
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2490
|
+
if (err instanceof ChannelTerminalError) {
|
|
2491
|
+
this.log({
|
|
2492
|
+
level: "error",
|
|
2493
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2494
|
+
conversation_id: conv.id,
|
|
2495
|
+
message_id: inFlight.evidentMessageId
|
|
2496
|
+
});
|
|
2497
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
if (this.now() >= inFlight.deadline) {
|
|
2501
|
+
this.log({
|
|
2502
|
+
level: "error",
|
|
2503
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed within the watch window \u2014 leaving for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
|
|
2504
|
+
conversation_id: conv.id,
|
|
2505
|
+
message_id: inFlight.evidentMessageId
|
|
2506
|
+
});
|
|
2507
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2508
|
+
return;
|
|
2509
|
+
}
|
|
2510
|
+
this.log({
|
|
2511
|
+
level: "error",
|
|
2512
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2513
|
+
conversation_id: conv.id,
|
|
2514
|
+
message_id: inFlight.evidentMessageId
|
|
2515
|
+
});
|
|
2516
|
+
return;
|
|
2517
|
+
}
|
|
2518
|
+
inFlight.done = true;
|
|
1897
2519
|
}
|
|
2520
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2521
|
+
return;
|
|
2522
|
+
}
|
|
2523
|
+
const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
|
|
2524
|
+
const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
|
|
2525
|
+
if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
|
|
2526
|
+
inFlight.stuckReported = true;
|
|
2527
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
|
|
2528
|
+
stuck_for_ms: this.now() - inFlight.dispatchedAt
|
|
2529
|
+
});
|
|
1898
2530
|
}
|
|
1899
2531
|
if (this.now() >= inFlight.deadline) {
|
|
1900
2532
|
this.log({
|
|
@@ -1903,48 +2535,395 @@ var ChannelDriver = class {
|
|
|
1903
2535
|
conversation_id: conv.id,
|
|
1904
2536
|
message_id: inFlight.evidentMessageId
|
|
1905
2537
|
});
|
|
2538
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
|
|
2539
|
+
watched_for_ms: this.now() - inFlight.dispatchedAt
|
|
2540
|
+
});
|
|
1906
2541
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
1907
2542
|
}
|
|
1908
2543
|
}
|
|
2544
|
+
// -------------------------------------------------------------------------
|
|
2545
|
+
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
2546
|
+
// -------------------------------------------------------------------------
|
|
2547
|
+
/**
|
|
2548
|
+
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
2549
|
+
*
|
|
2550
|
+
* The pending drain only re-drives `pending` rows; a message already flipped to
|
|
2551
|
+
* `processing` before the runner died is watched by nobody until the 15-min
|
|
2552
|
+
* cron resets it. Here we fetch those rows, and per row resolve its correlated
|
|
2553
|
+
* reply against opencode's OWN session store — completing, re-attaching, or
|
|
2554
|
+
* (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
|
|
2555
|
+
* is idempotent per message (Invariant 2): a row a watcher already tracks is
|
|
2556
|
+
* skipped in `readoptOne` — one driver, no double-drive.
|
|
2557
|
+
*
|
|
2558
|
+
* Only `ChannelAuthError` propagates (to `drainPending`, like the pending
|
|
2559
|
+
* path); every other early return LOGS a reason with context — no silent drop.
|
|
2560
|
+
*/
|
|
2561
|
+
async readoptProcessing() {
|
|
2562
|
+
const rows = await this.getProcessingMessages();
|
|
2563
|
+
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
|
|
2564
|
+
const stillProcessing = new Set(rows.map((r) => r.id));
|
|
2565
|
+
for (const id of [...this.dontRedispatch, ...this.doneUndeliverable]) {
|
|
2566
|
+
if (!stillProcessing.has(id)) {
|
|
2567
|
+
const cleared = this.dontRedispatch.delete(id);
|
|
2568
|
+
const clearedUndeliverable = this.doneUndeliverable.delete(id);
|
|
2569
|
+
if (cleared || clearedUndeliverable) {
|
|
2570
|
+
this.log({
|
|
2571
|
+
level: "info",
|
|
2572
|
+
message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
|
|
2573
|
+
message_id: id
|
|
2574
|
+
});
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
if (rows.length === 0) return;
|
|
2580
|
+
const bySession = /* @__PURE__ */ new Map();
|
|
2581
|
+
for (const row of rows) {
|
|
2582
|
+
if (!row.opencode_session_id) {
|
|
2583
|
+
this.log({
|
|
2584
|
+
level: "error",
|
|
2585
|
+
message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
|
|
2586
|
+
conversation_id: row.conversation_id,
|
|
2587
|
+
message_id: row.id
|
|
2588
|
+
});
|
|
2589
|
+
continue;
|
|
2590
|
+
}
|
|
2591
|
+
const list = bySession.get(row.opencode_session_id) ?? [];
|
|
2592
|
+
list.push(row);
|
|
2593
|
+
bySession.set(row.opencode_session_id, list);
|
|
2594
|
+
}
|
|
2595
|
+
for (const [sessionId, sessionRows] of bySession) {
|
|
2596
|
+
let messages;
|
|
2597
|
+
try {
|
|
2598
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
|
|
2599
|
+
if (!res.ok) {
|
|
2600
|
+
this.log({
|
|
2601
|
+
level: "error",
|
|
2602
|
+
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
|
|
2603
|
+
});
|
|
2604
|
+
continue;
|
|
2605
|
+
}
|
|
2606
|
+
const body = await res.json();
|
|
2607
|
+
if (!Array.isArray(body)) {
|
|
2608
|
+
this.log({
|
|
2609
|
+
level: "error",
|
|
2610
|
+
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
|
|
2611
|
+
});
|
|
2612
|
+
continue;
|
|
2613
|
+
}
|
|
2614
|
+
messages = body;
|
|
2615
|
+
} catch (err) {
|
|
2616
|
+
this.log({
|
|
2617
|
+
level: "error",
|
|
2618
|
+
message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
|
|
2619
|
+
});
|
|
2620
|
+
continue;
|
|
2621
|
+
}
|
|
2622
|
+
for (const row of sessionRows) {
|
|
2623
|
+
await this.readoptOne(sessionId, row, messages);
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
}
|
|
1909
2627
|
/**
|
|
1910
|
-
* Re-
|
|
1911
|
-
*
|
|
1912
|
-
*
|
|
1913
|
-
*
|
|
2628
|
+
* Re-adopt ONE `processing` row against the tick's session message snapshot
|
|
2629
|
+
* (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
|
|
2630
|
+
*
|
|
2631
|
+
* Branches on `messageRunState(messages, row.opencode_message_id)` — the
|
|
2632
|
+
* opencode-assigned user-message id persisted on the first `processing` PATCH
|
|
2633
|
+
* (#218). A row with a NULL stored id (dispatched but the read-back never landed
|
|
2634
|
+
* before the restart) has no id to correlate → treated as an orphan and
|
|
2635
|
+
* re-dispatched (at most once, see `forceReadoptRun`):
|
|
2636
|
+
* - `done` → `markDone` now (guarded like the watcher's done branch);
|
|
2637
|
+
* - `failed` → `markFailed` with the surfaced error (issue #182), so an
|
|
2638
|
+
* errored turn is reported failed on restart, NOT re-dispatched;
|
|
2639
|
+
* - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
|
|
2640
|
+
* tracking the stored id so the reply correlates by it;
|
|
2641
|
+
* - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
|
|
2642
|
+
*
|
|
2643
|
+
* Only `ChannelAuthError` propagates.
|
|
1914
2644
|
*/
|
|
1915
|
-
async
|
|
2645
|
+
async readoptOne(sessionId, row, messages) {
|
|
2646
|
+
if (this.isTracked(sessionId, row.id)) {
|
|
2647
|
+
this.log({
|
|
2648
|
+
level: "info",
|
|
2649
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
|
|
2650
|
+
conversation_id: row.conversation_id,
|
|
2651
|
+
message_id: row.id
|
|
2652
|
+
});
|
|
2653
|
+
return;
|
|
2654
|
+
}
|
|
2655
|
+
const ocId = row.opencode_message_id;
|
|
2656
|
+
const state = messageRunState(messages, ocId ?? "");
|
|
2657
|
+
if (state === "done") {
|
|
2658
|
+
if (this.doneUndeliverable.has(row.id)) {
|
|
2659
|
+
this.log({
|
|
2660
|
+
level: "info",
|
|
2661
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
|
|
2662
|
+
conversation_id: row.conversation_id,
|
|
2663
|
+
message_id: row.id
|
|
2664
|
+
});
|
|
2665
|
+
return;
|
|
2666
|
+
}
|
|
2667
|
+
this.log({
|
|
2668
|
+
level: "info",
|
|
2669
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
|
|
2670
|
+
conversation_id: row.conversation_id,
|
|
2671
|
+
message_id: row.id
|
|
2672
|
+
});
|
|
2673
|
+
try {
|
|
2674
|
+
await this.markDone(row.conversation_id, row.id, sessionId, ocId);
|
|
2675
|
+
} catch (err) {
|
|
2676
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2677
|
+
if (err instanceof ChannelTerminalError) {
|
|
2678
|
+
this.doneUndeliverable.add(row.id);
|
|
2679
|
+
this.log({
|
|
2680
|
+
level: "error",
|
|
2681
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 parking until it leaves processing; leaving for the cron safety net: ${err.message}`,
|
|
2682
|
+
conversation_id: row.conversation_id,
|
|
2683
|
+
message_id: row.id
|
|
2684
|
+
});
|
|
2685
|
+
return;
|
|
2686
|
+
}
|
|
2687
|
+
this.log({
|
|
2688
|
+
level: "error",
|
|
2689
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2690
|
+
conversation_id: row.conversation_id,
|
|
2691
|
+
message_id: row.id
|
|
2692
|
+
});
|
|
2693
|
+
return;
|
|
2694
|
+
}
|
|
2695
|
+
this.dontRedispatch.delete(row.id);
|
|
2696
|
+
return;
|
|
2697
|
+
}
|
|
2698
|
+
if (state === "failed") {
|
|
2699
|
+
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
2700
|
+
this.log({
|
|
2701
|
+
level: "error",
|
|
2702
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
2703
|
+
conversation_id: row.conversation_id,
|
|
2704
|
+
message_id: row.id
|
|
2705
|
+
});
|
|
2706
|
+
try {
|
|
2707
|
+
await this.markFailed(row.conversation_id, row.id, sessionId, error2);
|
|
2708
|
+
} catch (err) {
|
|
2709
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2710
|
+
if (err instanceof ChannelTerminalError) {
|
|
2711
|
+
this.doneUndeliverable.add(row.id);
|
|
2712
|
+
this.log({
|
|
2713
|
+
level: "error",
|
|
2714
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 parking until it leaves processing; leaving for the cron safety net: ${err.message}`,
|
|
2715
|
+
conversation_id: row.conversation_id,
|
|
2716
|
+
message_id: row.id
|
|
2717
|
+
});
|
|
2718
|
+
return;
|
|
2719
|
+
}
|
|
2720
|
+
this.log({
|
|
2721
|
+
level: "error",
|
|
2722
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2723
|
+
conversation_id: row.conversation_id,
|
|
2724
|
+
message_id: row.id
|
|
2725
|
+
});
|
|
2726
|
+
return;
|
|
2727
|
+
}
|
|
2728
|
+
this.dontRedispatch.delete(row.id);
|
|
2729
|
+
return;
|
|
2730
|
+
}
|
|
2731
|
+
if (this.dontRedispatch.has(row.id)) {
|
|
2732
|
+
this.log({
|
|
2733
|
+
level: "info",
|
|
2734
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
|
|
2735
|
+
conversation_id: row.conversation_id,
|
|
2736
|
+
message_id: row.id
|
|
2737
|
+
});
|
|
2738
|
+
return;
|
|
2739
|
+
}
|
|
2740
|
+
if ((state === "running" || state === "queued") && ocId) {
|
|
2741
|
+
const conv = this.convForRow(sessionId, row);
|
|
2742
|
+
const message = this.queuedMessageForRow(row);
|
|
2743
|
+
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
2744
|
+
this.dispatched.add(row.id);
|
|
2745
|
+
this.readopted.add(row.id);
|
|
2746
|
+
this.ensureWatcherRunning(sessionId);
|
|
2747
|
+
this.log({
|
|
2748
|
+
level: "info",
|
|
2749
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
|
|
2750
|
+
conversation_id: row.conversation_id,
|
|
2751
|
+
message_id: row.id
|
|
2752
|
+
});
|
|
2753
|
+
return;
|
|
2754
|
+
}
|
|
2755
|
+
await this.forceReadoptRun(sessionId, row);
|
|
2756
|
+
}
|
|
2757
|
+
/**
|
|
2758
|
+
* Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
|
|
2759
|
+
*
|
|
2760
|
+
* #218/WI-5: the row's user message is absent (never kept, or a null stored id),
|
|
2761
|
+
* so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
|
|
2762
|
+
* read it back, and register the watcher under the assigned id so the reply
|
|
2763
|
+
* correlates server-side.
|
|
2764
|
+
*
|
|
2765
|
+
* ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
|
|
2766
|
+
* id). Without a guard, if this dispatches on tick N but the read-back+persist
|
|
2767
|
+
* hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
|
|
2768
|
+
* tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
|
|
2769
|
+
* latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
|
|
2770
|
+
* short-circuit while the row is latched; clear it on a successful dispatch (the
|
|
2771
|
+
* row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
|
|
2772
|
+
* re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
|
|
2773
|
+
* may retry exactly once more).
|
|
2774
|
+
*
|
|
2775
|
+
* `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
|
|
2776
|
+
* `processed_at` (Invariant 1).
|
|
2777
|
+
*/
|
|
2778
|
+
async forceReadoptRun(sessionId, row) {
|
|
2779
|
+
if (this.stopped) {
|
|
2780
|
+
this.log({
|
|
2781
|
+
level: "info",
|
|
2782
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned but the runner is stopping \u2014 not starting a fresh turn; leaving for restart recovery`,
|
|
2783
|
+
conversation_id: row.conversation_id,
|
|
2784
|
+
message_id: row.id
|
|
2785
|
+
});
|
|
2786
|
+
return;
|
|
2787
|
+
}
|
|
2788
|
+
if (this.awaitingReadopt.has(row.id)) {
|
|
2789
|
+
this.log({
|
|
2790
|
+
level: "info",
|
|
2791
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
|
|
2792
|
+
conversation_id: row.conversation_id,
|
|
2793
|
+
message_id: row.id
|
|
2794
|
+
});
|
|
2795
|
+
return;
|
|
2796
|
+
}
|
|
2797
|
+
if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
|
|
2798
|
+
this.dontRedispatch.add(row.id);
|
|
2799
|
+
this.log({
|
|
2800
|
+
level: "info",
|
|
2801
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned but its re-adopt window has already elapsed \u2014 not dispatching an unwatchable turn; parking until it leaves processing (cron will reset it)`,
|
|
2802
|
+
conversation_id: row.conversation_id,
|
|
2803
|
+
message_id: row.id
|
|
2804
|
+
});
|
|
2805
|
+
return;
|
|
2806
|
+
}
|
|
1916
2807
|
const options = {
|
|
1917
|
-
agent:
|
|
1918
|
-
model:
|
|
2808
|
+
agent: row.opencode_agent ?? void 0,
|
|
2809
|
+
model: row.opencode_model ?? void 0
|
|
1919
2810
|
};
|
|
1920
2811
|
this.log({
|
|
1921
2812
|
level: "info",
|
|
1922
|
-
message: `
|
|
1923
|
-
|
|
2813
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
|
|
2814
|
+
conversation_id: row.conversation_id,
|
|
2815
|
+
message_id: row.id
|
|
1924
2816
|
});
|
|
2817
|
+
this.awaitingReadopt.add(row.id);
|
|
2818
|
+
let ocId;
|
|
1925
2819
|
try {
|
|
1926
|
-
await
|
|
1927
|
-
this.port,
|
|
2820
|
+
ocId = await this.dispatchLocked(
|
|
1928
2821
|
sessionId,
|
|
1929
|
-
|
|
1930
|
-
options,
|
|
1931
|
-
inFlight.opencodeMessageId
|
|
2822
|
+
() => sendPromptAsync(this.port, sessionId, row.content, options)
|
|
1932
2823
|
);
|
|
1933
2824
|
} catch (err) {
|
|
2825
|
+
this.awaitingReadopt.delete(row.id);
|
|
2826
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
1934
2827
|
this.log({
|
|
1935
2828
|
level: "error",
|
|
1936
|
-
message: `Re-dispatch failed for message ${
|
|
1937
|
-
|
|
2829
|
+
message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2830
|
+
conversation_id: row.conversation_id,
|
|
2831
|
+
message_id: row.id
|
|
1938
2832
|
});
|
|
2833
|
+
return;
|
|
2834
|
+
}
|
|
2835
|
+
if (ocId === null) {
|
|
2836
|
+
this.awaitingReadopt.delete(row.id);
|
|
2837
|
+
this.log({
|
|
2838
|
+
level: "error",
|
|
2839
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next drain`,
|
|
2840
|
+
conversation_id: row.conversation_id,
|
|
2841
|
+
message_id: row.id
|
|
2842
|
+
});
|
|
2843
|
+
return;
|
|
1939
2844
|
}
|
|
1940
|
-
|
|
2845
|
+
const conv = this.convForRow(sessionId, row);
|
|
2846
|
+
const message = this.queuedMessageForRow(row);
|
|
2847
|
+
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
2848
|
+
this.dispatched.add(row.id);
|
|
2849
|
+
this.readopted.add(row.id);
|
|
2850
|
+
this.awaitingReadopt.delete(row.id);
|
|
2851
|
+
this.ensureWatcherRunning(sessionId);
|
|
2852
|
+
}
|
|
2853
|
+
/**
|
|
2854
|
+
* True if `evidentMessageId` is already being driven — either in the
|
|
2855
|
+
* authoritative `dispatched` set or a live watcher's in-flight set for this
|
|
2856
|
+
* session (Invariant 2, WI-5). Either signal means a watcher owns the row.
|
|
2857
|
+
*/
|
|
2858
|
+
isTracked(sessionId, evidentMessageId) {
|
|
2859
|
+
if (this.dispatched.has(evidentMessageId)) return true;
|
|
2860
|
+
const watcher = this.watchers.get(sessionId);
|
|
2861
|
+
return watcher?.inFlight.has(evidentMessageId) ?? false;
|
|
2862
|
+
}
|
|
2863
|
+
/**
|
|
2864
|
+
* Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
|
|
2865
|
+
* deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
|
|
2866
|
+
* for `processing` rows, but if it is somehow null/unparseable fall back to
|
|
2867
|
+
* `now` (defensive) AND log — a fallback means the anchor is weaker than
|
|
2868
|
+
* intended, which is worth surfacing.
|
|
2869
|
+
*/
|
|
2870
|
+
processedAtMs(row) {
|
|
2871
|
+
const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
|
|
2872
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
2873
|
+
this.log({
|
|
2874
|
+
level: "error",
|
|
2875
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
|
|
2876
|
+
conversation_id: row.conversation_id,
|
|
2877
|
+
message_id: row.id
|
|
2878
|
+
});
|
|
2879
|
+
return this.now();
|
|
2880
|
+
}
|
|
2881
|
+
/** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
|
|
2882
|
+
convForRow(sessionId, row) {
|
|
2883
|
+
return {
|
|
2884
|
+
id: row.conversation_id,
|
|
2885
|
+
agent_id: this.agentId,
|
|
2886
|
+
opencode_session_id: sessionId,
|
|
2887
|
+
pending_message_count: 0,
|
|
2888
|
+
oldest_pending_at: row.processed_at
|
|
2889
|
+
};
|
|
2890
|
+
}
|
|
2891
|
+
/** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
|
|
2892
|
+
queuedMessageForRow(row) {
|
|
2893
|
+
return {
|
|
2894
|
+
id: row.id,
|
|
2895
|
+
content: row.content,
|
|
2896
|
+
status: "processing",
|
|
2897
|
+
opencode_agent: row.opencode_agent,
|
|
2898
|
+
opencode_model: row.opencode_model,
|
|
2899
|
+
source_message_id: row.source_message_id,
|
|
2900
|
+
slack_user_id: row.slack_user_id
|
|
2901
|
+
};
|
|
1941
2902
|
}
|
|
1942
2903
|
/**
|
|
1943
2904
|
* Remove a message from the in-flight set AND the authoritative dispatched
|
|
1944
2905
|
* set. Once the in-flight set empties, the watcher loop's `while` guard exits
|
|
1945
2906
|
* and its `.finally` removes the session entry from `this.watchers`.
|
|
2907
|
+
*
|
|
2908
|
+
* Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
|
|
2909
|
+
* (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
|
|
2910
|
+
* cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
|
|
2911
|
+
* re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
|
|
2912
|
+
* re-adopted message that completed (`done`) needs no marker — it's leaving
|
|
2913
|
+
* `processing`. This suppresses only re-dispatch: if its reply later completes,
|
|
2914
|
+
* the done branch still delivers it (Bugbot #202).
|
|
1946
2915
|
*/
|
|
1947
2916
|
removeInFlight(watcher, evidentMessageId) {
|
|
2917
|
+
const inFlight = watcher.inFlight.get(evidentMessageId);
|
|
2918
|
+
if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
|
|
2919
|
+
this.dontRedispatch.add(evidentMessageId);
|
|
2920
|
+
this.log({
|
|
2921
|
+
level: "info",
|
|
2922
|
+
message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
|
|
2923
|
+
conversation_id: watcher.conv.id,
|
|
2924
|
+
message_id: evidentMessageId
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
1948
2927
|
watcher.inFlight.delete(evidentMessageId);
|
|
1949
2928
|
this.dispatched.delete(evidentMessageId);
|
|
1950
2929
|
}
|
|
@@ -1973,8 +2952,8 @@ var ChannelDriver = class {
|
|
|
1973
2952
|
} catch {
|
|
1974
2953
|
}
|
|
1975
2954
|
for (const q of questions) {
|
|
1976
|
-
if (q.sessionID !== sessionId) continue;
|
|
1977
2955
|
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2956
|
+
if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
|
|
1978
2957
|
const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
|
|
1979
2958
|
const reported = await this.reportInteraction(
|
|
1980
2959
|
watcher.conv.id,
|
|
@@ -1994,8 +2973,8 @@ var ChannelDriver = class {
|
|
|
1994
2973
|
} catch {
|
|
1995
2974
|
}
|
|
1996
2975
|
for (const p of permissions) {
|
|
1997
|
-
if (p.sessionID !== sessionId) continue;
|
|
1998
2976
|
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2977
|
+
if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
|
|
1999
2978
|
const paused = this.attributeInteraction(watcher, p.messageID, messages);
|
|
2000
2979
|
const reported = await this.reportInteraction(
|
|
2001
2980
|
watcher.conv.id,
|
|
@@ -2006,6 +2985,50 @@ var ChannelDriver = class {
|
|
|
2006
2985
|
if (reported) watcher.reportedPermissions.add(p.id);
|
|
2007
2986
|
}
|
|
2008
2987
|
}
|
|
2988
|
+
/**
|
|
2989
|
+
* True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
|
|
2990
|
+
* i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
|
|
2991
|
+
* watched root. Sub-agents spawned via the `task` tool run in child sessions,
|
|
2992
|
+
* so their questions/permissions live under a different `sessionID` that must
|
|
2993
|
+
* still be attributed to the root conversation the watcher owns.
|
|
2994
|
+
*
|
|
2995
|
+
* Parents are cached in `sessionParents` so we walk each session at most once;
|
|
2996
|
+
* a bounded depth cap guards against a cycle or a pathological chain, and any
|
|
2997
|
+
* fetch failure is treated as "not a descendant" (best-effort — the interaction
|
|
2998
|
+
* simply isn't surfaced this tick and is retried next tick once resolvable).
|
|
2999
|
+
*/
|
|
3000
|
+
async sessionBelongsTo(sessionId, rootSessionId) {
|
|
3001
|
+
let current = sessionId;
|
|
3002
|
+
for (let depth = 0; current && depth < 32; depth++) {
|
|
3003
|
+
if (current === rootSessionId) return true;
|
|
3004
|
+
const parent = await this.resolveSessionParent(current);
|
|
3005
|
+
if (parent === null || parent === void 0) return false;
|
|
3006
|
+
current = parent;
|
|
3007
|
+
}
|
|
3008
|
+
return false;
|
|
3009
|
+
}
|
|
3010
|
+
/**
|
|
3011
|
+
* Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
|
|
3012
|
+
* `null` for a root session (no parent) and `undefined` when opencode is
|
|
3013
|
+
* unreachable / the session can't be read (so the caller stops walking without
|
|
3014
|
+
* caching a wrong answer — the next tick retries).
|
|
3015
|
+
*/
|
|
3016
|
+
async resolveSessionParent(sessionId) {
|
|
3017
|
+
const cached = this.sessionParents.get(sessionId);
|
|
3018
|
+
if (cached !== void 0) return cached;
|
|
3019
|
+
let parent = void 0;
|
|
3020
|
+
try {
|
|
3021
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
|
|
3022
|
+
if (res.ok) {
|
|
3023
|
+
const body = await res.json();
|
|
3024
|
+
parent = body && typeof body.parentID === "string" ? body.parentID : null;
|
|
3025
|
+
}
|
|
3026
|
+
} catch {
|
|
3027
|
+
parent = void 0;
|
|
3028
|
+
}
|
|
3029
|
+
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
3030
|
+
return parent;
|
|
3031
|
+
}
|
|
2009
3032
|
/**
|
|
2010
3033
|
* Attribute a surfaced interaction to the in-flight message it paused on (M-1).
|
|
2011
3034
|
*
|
|
@@ -2088,6 +3111,35 @@ var ChannelDriver = class {
|
|
|
2088
3111
|
}
|
|
2089
3112
|
return await res.json();
|
|
2090
3113
|
}
|
|
3114
|
+
/**
|
|
3115
|
+
* Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
|
|
3116
|
+
* The pending path (`getPendingConversations`/`getPendingMessages`) only
|
|
3117
|
+
* surfaces `pending` rows, so a message already `processing` when the runner
|
|
3118
|
+
* died is invisible to it — this dedicated endpoint returns exactly those rows
|
|
3119
|
+
* with the fields the re-adopt path needs (`processed_at`,
|
|
3120
|
+
* `opencode_session_id`, routing).
|
|
3121
|
+
*
|
|
3122
|
+
* Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
|
|
3123
|
+
* array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
|
|
3124
|
+
* other non-ok so `drainPending`'s try/finally leaves `draining` false and the
|
|
3125
|
+
* next tick retries.
|
|
3126
|
+
*/
|
|
3127
|
+
async getProcessingMessages() {
|
|
3128
|
+
const res = await this.fetchImpl(
|
|
3129
|
+
`${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
|
|
3130
|
+
{ headers: { Authorization: this.getAuthHeader() } }
|
|
3131
|
+
);
|
|
3132
|
+
this.assertAuth(res, "fetching processing messages");
|
|
3133
|
+
if (!res.ok) {
|
|
3134
|
+
throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
|
|
3135
|
+
}
|
|
3136
|
+
const data = await res.json();
|
|
3137
|
+
let messages = data.messages ?? [];
|
|
3138
|
+
if (this.conversationFilter) {
|
|
3139
|
+
messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
|
|
3140
|
+
}
|
|
3141
|
+
return messages;
|
|
3142
|
+
}
|
|
2091
3143
|
/**
|
|
2092
3144
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
2093
3145
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
@@ -2109,13 +3161,17 @@ var ChannelDriver = class {
|
|
|
2109
3161
|
* A single attempt (no internal retry): the watcher's per-tick loop is the
|
|
2110
3162
|
* retry vehicle for the swap-to-running.
|
|
2111
3163
|
*/
|
|
2112
|
-
async markProcessing(conversationId, messageId, sessionId) {
|
|
3164
|
+
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
2113
3165
|
const res = await this.fetchImpl(
|
|
2114
3166
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2115
3167
|
{
|
|
2116
3168
|
method: "PATCH",
|
|
2117
3169
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2118
|
-
body: JSON.stringify({
|
|
3170
|
+
body: JSON.stringify({
|
|
3171
|
+
status: "processing",
|
|
3172
|
+
opencode_session_id: sessionId,
|
|
3173
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
3174
|
+
})
|
|
2119
3175
|
}
|
|
2120
3176
|
);
|
|
2121
3177
|
this.assertAuth(res, "marking message as processing");
|
|
@@ -2153,13 +3209,17 @@ var ChannelDriver = class {
|
|
|
2153
3209
|
* watcher retries next tick within the
|
|
2154
3210
|
* deadline, Finding 4).
|
|
2155
3211
|
*/
|
|
2156
|
-
async markDone(conversationId, messageId, sessionId) {
|
|
3212
|
+
async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
2157
3213
|
const res = await this.fetchImpl(
|
|
2158
3214
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2159
3215
|
{
|
|
2160
3216
|
method: "PATCH",
|
|
2161
3217
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2162
|
-
body: JSON.stringify({
|
|
3218
|
+
body: JSON.stringify({
|
|
3219
|
+
status: "done",
|
|
3220
|
+
opencode_session_id: sessionId,
|
|
3221
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
3222
|
+
})
|
|
2163
3223
|
}
|
|
2164
3224
|
);
|
|
2165
3225
|
this.assertAuth(res, "marking message as done");
|
|
@@ -2169,7 +3229,17 @@ var ChannelDriver = class {
|
|
|
2169
3229
|
}
|
|
2170
3230
|
throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
|
|
2171
3231
|
}
|
|
2172
|
-
|
|
3232
|
+
/**
|
|
3233
|
+
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
3234
|
+
* when provided (issue #182): a bare `markFailed(conv, msg)` sends
|
|
3235
|
+
* `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
|
|
3236
|
+
* OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
|
|
3237
|
+
* failure reason reaches the channel.
|
|
3238
|
+
*/
|
|
3239
|
+
async markFailed(conversationId, messageId, sessionId, error2) {
|
|
3240
|
+
const body = { status: "failed" };
|
|
3241
|
+
if (sessionId !== void 0) body.opencode_session_id = sessionId;
|
|
3242
|
+
if (error2 !== void 0) body.error = error2;
|
|
2173
3243
|
await this.callWithRetry(
|
|
2174
3244
|
"marking message as failed",
|
|
2175
3245
|
() => this.fetchImpl(
|
|
@@ -2177,11 +3247,47 @@ var ChannelDriver = class {
|
|
|
2177
3247
|
{
|
|
2178
3248
|
method: "PATCH",
|
|
2179
3249
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2180
|
-
body: JSON.stringify(
|
|
3250
|
+
body: JSON.stringify(body)
|
|
2181
3251
|
}
|
|
2182
3252
|
)
|
|
2183
3253
|
);
|
|
2184
3254
|
}
|
|
3255
|
+
/**
|
|
3256
|
+
* Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
|
|
3257
|
+
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
3258
|
+
* — the server records it via `log()` (no DB write, no notification). This is
|
|
3259
|
+
* fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
|
|
3260
|
+
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
3261
|
+
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
3262
|
+
* context (no silent catch, per development-workflow).
|
|
3263
|
+
*/
|
|
3264
|
+
async postSignal(conversationId, messageId, signal, extra) {
|
|
3265
|
+
try {
|
|
3266
|
+
const res = await this.fetchImpl(
|
|
3267
|
+
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
|
|
3268
|
+
{
|
|
3269
|
+
method: "POST",
|
|
3270
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3271
|
+
body: JSON.stringify({ signal, ...extra })
|
|
3272
|
+
}
|
|
3273
|
+
);
|
|
3274
|
+
if (!res.ok) {
|
|
3275
|
+
this.log({
|
|
3276
|
+
level: "error",
|
|
3277
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
|
|
3278
|
+
conversation_id: conversationId,
|
|
3279
|
+
message_id: messageId
|
|
3280
|
+
});
|
|
3281
|
+
}
|
|
3282
|
+
} catch (err) {
|
|
3283
|
+
this.log({
|
|
3284
|
+
level: "error",
|
|
3285
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
|
|
3286
|
+
conversation_id: conversationId,
|
|
3287
|
+
message_id: messageId
|
|
3288
|
+
});
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
2185
3291
|
async persistSession(conversationId, sessionId) {
|
|
2186
3292
|
const res = await this.fetchImpl(
|
|
2187
3293
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}`,
|
|
@@ -2452,6 +3558,25 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
2452
3558
|
return { error: `Failed to resolve agent from key: ${message}` };
|
|
2453
3559
|
}
|
|
2454
3560
|
}
|
|
3561
|
+
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
3562
|
+
const apiUrl = getApiUrlConfig();
|
|
3563
|
+
try {
|
|
3564
|
+
const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
|
|
3565
|
+
method: "POST",
|
|
3566
|
+
headers: { Authorization: authHeader }
|
|
3567
|
+
});
|
|
3568
|
+
if (!response.ok) {
|
|
3569
|
+
const serverMessage = await readErrorMessage(response);
|
|
3570
|
+
return {
|
|
3571
|
+
ok: false,
|
|
3572
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
3573
|
+
};
|
|
3574
|
+
}
|
|
3575
|
+
return { ok: true };
|
|
3576
|
+
} catch (error2) {
|
|
3577
|
+
return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
|
|
3578
|
+
}
|
|
3579
|
+
}
|
|
2455
3580
|
async function getAgentInfo(agentId, authHeader) {
|
|
2456
3581
|
const apiUrl = getApiUrlConfig();
|
|
2457
3582
|
try {
|
|
@@ -2497,7 +3622,9 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
2497
3622
|
// src/commands/run.ts
|
|
2498
3623
|
var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
2499
3624
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
2500
|
-
|
|
3625
|
+
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
3626
|
+
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
3627
|
+
function log2(state, message, isError = false) {
|
|
2501
3628
|
if (state.json) {
|
|
2502
3629
|
console.log(
|
|
2503
3630
|
JSON.stringify({
|
|
@@ -2522,9 +3649,9 @@ function logActivity(state, entry) {
|
|
|
2522
3649
|
}
|
|
2523
3650
|
if (!state.interactive) {
|
|
2524
3651
|
if (entry.type === "error") {
|
|
2525
|
-
|
|
3652
|
+
log2(state, entry.error ?? "Unknown error", true);
|
|
2526
3653
|
} else if (entry.type === "info" && entry.message) {
|
|
2527
|
-
|
|
3654
|
+
log2(state, entry.message);
|
|
2528
3655
|
}
|
|
2529
3656
|
}
|
|
2530
3657
|
}
|
|
@@ -2610,6 +3737,7 @@ async function handleAuthError(state, error2) {
|
|
|
2610
3737
|
}
|
|
2611
3738
|
async function driveChannels(state, driver) {
|
|
2612
3739
|
let idlePolls = 0;
|
|
3740
|
+
let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
2613
3741
|
while (state.running) {
|
|
2614
3742
|
if (state.connection?.reconnecting && state.connection.reconnectPromise) {
|
|
2615
3743
|
logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
|
|
@@ -2619,7 +3747,9 @@ async function driveChannels(state, driver) {
|
|
|
2619
3747
|
try {
|
|
2620
3748
|
const processed = await driver.drainPending();
|
|
2621
3749
|
state.messageCount += processed;
|
|
2622
|
-
|
|
3750
|
+
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
3751
|
+
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
3752
|
+
if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
|
|
2623
3753
|
idlePolls = 0;
|
|
2624
3754
|
if (processed > 0 && state.interactive) displayStatus(state);
|
|
2625
3755
|
} else if (state.idleTimeout !== null) {
|
|
@@ -2648,7 +3778,7 @@ async function driveChannels(state, driver) {
|
|
|
2648
3778
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
|
|
2649
3779
|
if (state.interactive) displayStatus(state);
|
|
2650
3780
|
}
|
|
2651
|
-
await new Promise((
|
|
3781
|
+
await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
|
|
2652
3782
|
if (state.idleTimeout !== null && idlePolls >= 2) {
|
|
2653
3783
|
const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
|
|
2654
3784
|
if (idleMs > state.idleTimeout * 1e3) {
|
|
@@ -2659,8 +3789,122 @@ async function driveChannels(state, driver) {
|
|
|
2659
3789
|
}
|
|
2660
3790
|
}
|
|
2661
3791
|
}
|
|
2662
|
-
|
|
3792
|
+
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
3793
|
+
async function runSweep(state, driver, config2) {
|
|
3794
|
+
const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
|
|
3795
|
+
try {
|
|
3796
|
+
const sessions = await listSessions(state.port);
|
|
3797
|
+
if (sessions === null) {
|
|
3798
|
+
logActivity(state, {
|
|
3799
|
+
type: "info",
|
|
3800
|
+
message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
|
|
3801
|
+
});
|
|
3802
|
+
return;
|
|
3803
|
+
}
|
|
3804
|
+
const toDelete = selectSessionsToDelete(
|
|
3805
|
+
sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
|
|
3806
|
+
{
|
|
3807
|
+
maxAgeMs: config2.maxAgeMs,
|
|
3808
|
+
maxCount: config2.maxCount,
|
|
3809
|
+
nowMs: Date.now(),
|
|
3810
|
+
protectedIds: driver.protectedSessionIds()
|
|
3811
|
+
}
|
|
3812
|
+
);
|
|
3813
|
+
const protectedNow = driver.protectedSessionIds();
|
|
3814
|
+
let deleted = 0;
|
|
3815
|
+
let failed = 0;
|
|
3816
|
+
let skippedNewlyActive = 0;
|
|
3817
|
+
for (const id of toDelete) {
|
|
3818
|
+
if (protectedNow.has(id)) {
|
|
3819
|
+
skippedNewlyActive++;
|
|
3820
|
+
logActivity(state, {
|
|
3821
|
+
type: "info",
|
|
3822
|
+
message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
|
|
3823
|
+
});
|
|
3824
|
+
continue;
|
|
3825
|
+
}
|
|
3826
|
+
if (await deleteSession(state.port, id)) deleted++;
|
|
3827
|
+
else failed++;
|
|
3828
|
+
}
|
|
3829
|
+
const failedNote = failed > 0 ? `, failed ${failed}` : "";
|
|
3830
|
+
const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
|
|
3831
|
+
logActivity(state, {
|
|
3832
|
+
type: "info",
|
|
3833
|
+
message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
|
|
3834
|
+
});
|
|
3835
|
+
} catch (error2) {
|
|
3836
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
3837
|
+
logActivity(state, {
|
|
3838
|
+
type: "error",
|
|
3839
|
+
error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
|
|
3840
|
+
});
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
function scheduleSessionCleanup(state, driver, options) {
|
|
3844
|
+
const config2 = resolveSessionCleanupConfig(
|
|
3845
|
+
{
|
|
3846
|
+
maxAge: options.sessionCleanupMaxAge,
|
|
3847
|
+
maxCount: options.sessionCleanupMaxCount,
|
|
3848
|
+
interval: options.sessionCleanupInterval
|
|
3849
|
+
},
|
|
3850
|
+
process.env
|
|
3851
|
+
);
|
|
3852
|
+
for (const warning2 of config2.warnings) {
|
|
3853
|
+
logActivity(state, { type: "info", message: `Session cleanup: ${warning2}` });
|
|
3854
|
+
}
|
|
3855
|
+
if (!config2.enabled) return;
|
|
3856
|
+
logActivity(state, {
|
|
3857
|
+
type: "info",
|
|
3858
|
+
message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
|
|
3859
|
+
});
|
|
3860
|
+
const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
|
|
3861
|
+
const firstSweep = setTimeout(
|
|
3862
|
+
() => void runSweep(state, driver, config2),
|
|
3863
|
+
SESSION_CLEANUP_FIRST_SWEEP_MS
|
|
3864
|
+
);
|
|
3865
|
+
state.sessionCleanupTimers.push(interval, firstSweep);
|
|
3866
|
+
}
|
|
3867
|
+
async function notifyOffline(state) {
|
|
3868
|
+
if (!state.agentId || !state.authHeader) return;
|
|
3869
|
+
if (!state.connected) {
|
|
3870
|
+
log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
|
|
3871
|
+
return;
|
|
3872
|
+
}
|
|
3873
|
+
const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
|
|
3874
|
+
if (result.ok) {
|
|
3875
|
+
log2(state, "Notified Evident the agent is going offline");
|
|
3876
|
+
} else {
|
|
3877
|
+
logActivity(state, {
|
|
3878
|
+
type: "error",
|
|
3879
|
+
error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
|
|
3880
|
+
});
|
|
3881
|
+
if (state.interactive) displayStatus(state);
|
|
3882
|
+
}
|
|
3883
|
+
}
|
|
3884
|
+
async function cleanup(state, opts = {}) {
|
|
2663
3885
|
state.running = false;
|
|
3886
|
+
for (const timer of state.sessionCleanupTimers) {
|
|
3887
|
+
clearInterval(timer);
|
|
3888
|
+
clearTimeout(timer);
|
|
3889
|
+
}
|
|
3890
|
+
state.sessionCleanupTimers = [];
|
|
3891
|
+
if (opts.graceful && state.channelDriver) {
|
|
3892
|
+
state.channelDriver.stop();
|
|
3893
|
+
log2(state, "Draining in-flight channel work before shutdown...");
|
|
3894
|
+
if (state.interactive) {
|
|
3895
|
+
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
3896
|
+
displayStatus(state);
|
|
3897
|
+
}
|
|
3898
|
+
const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
|
|
3899
|
+
if (!settled) {
|
|
3900
|
+
logActivity(state, {
|
|
3901
|
+
type: "info",
|
|
3902
|
+
message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
|
|
3903
|
+
});
|
|
3904
|
+
if (state.interactive) displayStatus(state);
|
|
3905
|
+
}
|
|
3906
|
+
}
|
|
3907
|
+
await notifyOffline(state);
|
|
2664
3908
|
if (state.connection) {
|
|
2665
3909
|
state.connection.close();
|
|
2666
3910
|
state.connection = null;
|
|
@@ -2671,7 +3915,7 @@ async function cleanup(state) {
|
|
|
2671
3915
|
logActivity(state, { type: "info", message: "Stopped OpenCode process" });
|
|
2672
3916
|
displayStatus(state);
|
|
2673
3917
|
} else {
|
|
2674
|
-
|
|
3918
|
+
log2(state, "Stopped OpenCode process");
|
|
2675
3919
|
}
|
|
2676
3920
|
state.opencodeProcess = null;
|
|
2677
3921
|
}
|
|
@@ -2691,26 +3935,32 @@ async function run(options) {
|
|
|
2691
3935
|
opencodeVersion: null,
|
|
2692
3936
|
opencodeProcess: null,
|
|
2693
3937
|
connection: null,
|
|
3938
|
+
channelDriver: null,
|
|
2694
3939
|
running: true,
|
|
3940
|
+
shuttingDown: false,
|
|
2695
3941
|
activityLog: [],
|
|
2696
3942
|
messageCount: 0,
|
|
3943
|
+
lastProxiedActivityAt: null,
|
|
3944
|
+
sessionCleanupTimers: [],
|
|
2697
3945
|
authHeader: ""
|
|
2698
3946
|
};
|
|
2699
3947
|
if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
|
|
2700
|
-
|
|
3948
|
+
log2(
|
|
2701
3949
|
state,
|
|
2702
3950
|
"Warning: No --idle-timeout set in CI environment. The runner will poll indefinitely until the job times out. Consider adding --idle-timeout 30 to avoid wasting runner minutes.",
|
|
2703
3951
|
false
|
|
2704
3952
|
);
|
|
2705
3953
|
}
|
|
2706
3954
|
const handleSignal = async () => {
|
|
3955
|
+
if (state.shuttingDown) return;
|
|
3956
|
+
state.shuttingDown = true;
|
|
2707
3957
|
if (state.interactive) {
|
|
2708
3958
|
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
2709
3959
|
displayStatus(state);
|
|
2710
3960
|
} else {
|
|
2711
|
-
|
|
3961
|
+
log2(state, "Shutting down...");
|
|
2712
3962
|
}
|
|
2713
|
-
await cleanup(state);
|
|
3963
|
+
await cleanup(state, { graceful: true });
|
|
2714
3964
|
await shutdownTelemetry();
|
|
2715
3965
|
process.exit(0);
|
|
2716
3966
|
};
|
|
@@ -2741,7 +3991,7 @@ async function run(options) {
|
|
|
2741
3991
|
const resolved = await resolveAgentIdFromKey(state.authHeader);
|
|
2742
3992
|
if (resolved.agent_id) {
|
|
2743
3993
|
state.agentId = resolved.agent_id;
|
|
2744
|
-
|
|
3994
|
+
log2(state, `Resolved agent ID from key: ${state.agentId}`);
|
|
2745
3995
|
if (state.interactive && !state.json) {
|
|
2746
3996
|
logActivity(state, {
|
|
2747
3997
|
type: "info",
|
|
@@ -2804,17 +4054,17 @@ async function run(options) {
|
|
|
2804
4054
|
port: state.port,
|
|
2805
4055
|
interactive: state.interactive,
|
|
2806
4056
|
agentId: state.agentId,
|
|
2807
|
-
log: (message) =>
|
|
4057
|
+
log: (message) => log2(state, message)
|
|
2808
4058
|
});
|
|
2809
4059
|
state.port = oc.port;
|
|
2810
4060
|
state.opencodeProcess = oc.process;
|
|
2811
4061
|
state.opencodeVersion = oc.version;
|
|
2812
4062
|
state.opencodeConnected = oc.process !== null || oc.version !== null;
|
|
2813
|
-
const
|
|
2814
|
-
ocSpinner?.succeed(`OpenCode running on port ${state.port}${
|
|
4063
|
+
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
4064
|
+
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
2815
4065
|
const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
|
|
2816
4066
|
if (versionWarning) {
|
|
2817
|
-
|
|
4067
|
+
log2(state, versionWarning, false);
|
|
2818
4068
|
if (state.interactive && !state.json) {
|
|
2819
4069
|
logActivity(state, { type: "info", message: versionWarning });
|
|
2820
4070
|
}
|
|
@@ -2830,12 +4080,14 @@ async function run(options) {
|
|
|
2830
4080
|
apiUrl: getApiUrlConfig(),
|
|
2831
4081
|
getAuthHeader: () => state.authHeader,
|
|
2832
4082
|
conversationFilter: state.conversationFilter,
|
|
4083
|
+
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
2833
4084
|
log: (entry) => logActivity(state, {
|
|
2834
4085
|
type: entry.level === "error" ? "error" : "info",
|
|
2835
4086
|
message: entry.message,
|
|
2836
4087
|
error: entry.level === "error" ? entry.message : void 0
|
|
2837
4088
|
})
|
|
2838
4089
|
});
|
|
4090
|
+
state.channelDriver = channelDriver;
|
|
2839
4091
|
const connection = new RunnerConnection({
|
|
2840
4092
|
agentId: state.agentId,
|
|
2841
4093
|
getAuthHeader: () => state.authHeader,
|
|
@@ -2849,7 +4101,11 @@ async function run(options) {
|
|
|
2849
4101
|
type: "info",
|
|
2850
4102
|
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
|
|
2851
4103
|
});
|
|
2852
|
-
emitAgentConnected(state.agentId, {
|
|
4104
|
+
emitAgentConnected(state.agentId, {
|
|
4105
|
+
port: state.port,
|
|
4106
|
+
cli_version: getCliVersion(),
|
|
4107
|
+
opencode_version: state.opencodeVersion
|
|
4108
|
+
});
|
|
2853
4109
|
if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
|
|
2854
4110
|
if (state.interactive) displayStatus(state);
|
|
2855
4111
|
channelDriver.drainPending().then((processed) => {
|
|
@@ -2883,9 +4139,14 @@ async function run(options) {
|
|
|
2883
4139
|
logActivity(state, { type: "error", error: error2 });
|
|
2884
4140
|
if (state.interactive) displayStatus(state);
|
|
2885
4141
|
},
|
|
2886
|
-
// Web traffic is proxied transparently;
|
|
4142
|
+
// Web traffic is proxied transparently; note opencode is live and stamp
|
|
4143
|
+
// proxied activity so the idle loop treats interactive proxy use as work.
|
|
4144
|
+
// Fires per forwarded response head (incl. every SSE open) and excludes
|
|
4145
|
+
// the internal drain-ping, so an actively-used proxy keeps the timer
|
|
4146
|
+
// fresh while a lone idle SSE with no follow-up requests still ages out.
|
|
2887
4147
|
onResponse: () => {
|
|
2888
4148
|
state.opencodeConnected = true;
|
|
4149
|
+
state.lastProxiedActivityAt = Date.now();
|
|
2889
4150
|
},
|
|
2890
4151
|
// A channel message was queued and the api-worker pinged us over the
|
|
2891
4152
|
// tunnel to drain immediately instead of waiting for the next poll tick.
|
|
@@ -2923,10 +4184,12 @@ async function run(options) {
|
|
|
2923
4184
|
if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
|
|
2924
4185
|
throw error2;
|
|
2925
4186
|
}
|
|
4187
|
+
scheduleSessionCleanup(state, channelDriver, options);
|
|
2926
4188
|
if (!interactive || state.json) {
|
|
2927
|
-
|
|
4189
|
+
log2(state, "Driving channel messages...");
|
|
2928
4190
|
}
|
|
2929
4191
|
await driveChannels(state, channelDriver);
|
|
4192
|
+
if (state.shuttingDown) return;
|
|
2930
4193
|
await cleanup(state);
|
|
2931
4194
|
if (state.json) {
|
|
2932
4195
|
console.log(
|
|
@@ -2936,11 +4199,12 @@ async function run(options) {
|
|
|
2936
4199
|
})
|
|
2937
4200
|
);
|
|
2938
4201
|
} else if (!interactive) {
|
|
2939
|
-
|
|
4202
|
+
log2(state, `Completed. Processed ${state.messageCount} message(s).`);
|
|
2940
4203
|
}
|
|
2941
4204
|
await shutdownTelemetry();
|
|
2942
4205
|
process.exit(0);
|
|
2943
4206
|
} catch (error2) {
|
|
4207
|
+
if (state.shuttingDown) return;
|
|
2944
4208
|
await cleanup(state);
|
|
2945
4209
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
2946
4210
|
if (state.json) {
|
|
@@ -2958,8 +4222,9 @@ async function run(options) {
|
|
|
2958
4222
|
}
|
|
2959
4223
|
|
|
2960
4224
|
// src/index.ts
|
|
4225
|
+
var { version } = createRequire(import.meta.url)("../package.json");
|
|
2961
4226
|
var program = new Command();
|
|
2962
|
-
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(
|
|
4227
|
+
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
|
|
2963
4228
|
"--endpoint <url>",
|
|
2964
4229
|
"Evident API base URL (default: production; e.g. http://localhost:3001)"
|
|
2965
4230
|
).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
|
|
@@ -2974,7 +4239,16 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
|
|
|
2974
4239
|
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);
|
|
2975
4240
|
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 }));
|
|
2976
4241
|
program.command("whoami").description("Show the currently logged in user").action(whoami);
|
|
2977
|
-
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").
|
|
4242
|
+
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(
|
|
4243
|
+
"--session-cleanup-max-age <duration>",
|
|
4244
|
+
"Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
|
|
4245
|
+
).option(
|
|
4246
|
+
"--session-cleanup-max-count <n>",
|
|
4247
|
+
"Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
|
|
4248
|
+
).option(
|
|
4249
|
+
"--session-cleanup-interval <duration>",
|
|
4250
|
+
"How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
|
|
4251
|
+
).action(
|
|
2978
4252
|
(options) => {
|
|
2979
4253
|
run({
|
|
2980
4254
|
agent: options.agent,
|
|
@@ -2982,7 +4256,11 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
2982
4256
|
verbose: options.verbose,
|
|
2983
4257
|
conversation: options.conversation,
|
|
2984
4258
|
idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
|
|
2985
|
-
json: options.json
|
|
4259
|
+
json: options.json,
|
|
4260
|
+
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
4261
|
+
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
4262
|
+
sessionCleanupMaxCount: options.sessionCleanupMaxCount,
|
|
4263
|
+
sessionCleanupInterval: options.sessionCleanupInterval
|
|
2986
4264
|
});
|
|
2987
4265
|
}
|
|
2988
4266
|
);
|