@evident-ai/cli 3.0.1-dev.fffc02d → 3.1.1-dev.120303a
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/README.md +25 -17
- package/dist/index.js +1549 -185
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -285,14 +285,14 @@ function blank() {
|
|
|
285
285
|
console.log();
|
|
286
286
|
}
|
|
287
287
|
function waitForEnter(prompt = "Press Enter to continue...") {
|
|
288
|
-
return new Promise((
|
|
288
|
+
return new Promise((resolve2) => {
|
|
289
289
|
process.stdout.write(chalk.dim(prompt));
|
|
290
290
|
const handler = () => {
|
|
291
291
|
process.stdin.removeListener("data", handler);
|
|
292
292
|
process.stdin.setRawMode?.(false);
|
|
293
293
|
process.stdin.pause();
|
|
294
294
|
console.log();
|
|
295
|
-
|
|
295
|
+
resolve2();
|
|
296
296
|
};
|
|
297
297
|
if (process.stdin.isTTY) {
|
|
298
298
|
process.stdin.setRawMode?.(true);
|
|
@@ -302,7 +302,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
|
|
|
302
302
|
});
|
|
303
303
|
}
|
|
304
304
|
function sleep(ms) {
|
|
305
|
-
return new Promise((
|
|
305
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
306
306
|
}
|
|
307
307
|
|
|
308
308
|
// src/commands/login.ts
|
|
@@ -376,19 +376,19 @@ async function tokenLogin() {
|
|
|
376
376
|
console.log("Visit your Evident dashboard to generate a CLI token.");
|
|
377
377
|
blank();
|
|
378
378
|
process.stdout.write("Paste token: ");
|
|
379
|
-
const token = await new Promise((
|
|
379
|
+
const token = await new Promise((resolve2) => {
|
|
380
380
|
let data = "";
|
|
381
381
|
process.stdin.setEncoding("utf8");
|
|
382
382
|
process.stdin.on("data", (chunk) => {
|
|
383
383
|
data += chunk;
|
|
384
384
|
});
|
|
385
385
|
process.stdin.on("end", () => {
|
|
386
|
-
|
|
386
|
+
resolve2(data.trim());
|
|
387
387
|
});
|
|
388
388
|
if (process.stdin.isTTY) {
|
|
389
389
|
process.stdin.once("data", (chunk) => {
|
|
390
390
|
process.stdin.pause();
|
|
391
|
-
|
|
391
|
+
resolve2(chunk.toString().trim());
|
|
392
392
|
});
|
|
393
393
|
process.stdin.resume();
|
|
394
394
|
}
|
|
@@ -499,6 +499,12 @@ function log(level, event, fields) {
|
|
|
499
499
|
);
|
|
500
500
|
}
|
|
501
501
|
}
|
|
502
|
+
function errorFields(err) {
|
|
503
|
+
if (err instanceof Error) {
|
|
504
|
+
return { error: err.message, error_name: err.name };
|
|
505
|
+
}
|
|
506
|
+
return { error: String(err) };
|
|
507
|
+
}
|
|
502
508
|
function stripQuery(url) {
|
|
503
509
|
try {
|
|
504
510
|
return new URL(url).pathname;
|
|
@@ -645,14 +651,27 @@ var EventTypes = {
|
|
|
645
651
|
// CLI lifecycle
|
|
646
652
|
CLI_STARTED: "cli.started",
|
|
647
653
|
CLI_COMMAND: "cli.command",
|
|
648
|
-
CLI_ERROR: "cli.error"
|
|
654
|
+
CLI_ERROR: "cli.error",
|
|
655
|
+
// Deprecation telemetry (#412) — usage of the old `--agent`/`EVIDENT_AGENT_KEY`
|
|
656
|
+
// names instead of the preferred `--runner`/`EVIDENT_RUNNER_KEY` (#409).
|
|
657
|
+
DEPRECATED_AGENT_FLAG_USED: "cli.deprecated_agent_flag_used",
|
|
658
|
+
DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
|
|
649
659
|
};
|
|
650
660
|
|
|
651
661
|
// src/lib/auth.ts
|
|
652
662
|
async function getAuthCredentials() {
|
|
663
|
+
const runnerKey = process.env.EVIDENT_RUNNER_KEY;
|
|
653
664
|
const agentKey = process.env.EVIDENT_AGENT_KEY;
|
|
665
|
+
if (runnerKey) {
|
|
666
|
+
return {
|
|
667
|
+
token: runnerKey,
|
|
668
|
+
authType: "agent_key",
|
|
669
|
+
keySource: "runner_key",
|
|
670
|
+
notice: agentKey ? "Both EVIDENT_RUNNER_KEY and EVIDENT_AGENT_KEY are set; using EVIDENT_RUNNER_KEY." : void 0
|
|
671
|
+
};
|
|
672
|
+
}
|
|
654
673
|
if (agentKey) {
|
|
655
|
-
return { token: agentKey, authType: "agent_key" };
|
|
674
|
+
return { token: agentKey, authType: "agent_key", keySource: "agent_key" };
|
|
656
675
|
}
|
|
657
676
|
const userToken = process.env.EVIDENT_TOKEN;
|
|
658
677
|
if (userToken) {
|
|
@@ -706,7 +725,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
706
725
|
if (health.healthy) {
|
|
707
726
|
return health;
|
|
708
727
|
}
|
|
709
|
-
await new Promise((
|
|
728
|
+
await new Promise((resolve2) => setTimeout(resolve2, 1e3));
|
|
710
729
|
}
|
|
711
730
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
712
731
|
}
|
|
@@ -721,7 +740,7 @@ function buildOpenCodeVersionWarning(version2) {
|
|
|
721
740
|
if (isQueueValidatedVersion(version2)) return null;
|
|
722
741
|
const detected = version2 ? `v${version2}` : "unknown";
|
|
723
742
|
const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
|
|
724
|
-
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack
|
|
743
|
+
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
|
|
725
744
|
}
|
|
726
745
|
|
|
727
746
|
// src/lib/opencode/process.ts
|
|
@@ -1013,6 +1032,12 @@ async function promptOpenCodeInstall(interactive) {
|
|
|
1013
1032
|
return action;
|
|
1014
1033
|
}
|
|
1015
1034
|
|
|
1035
|
+
// src/lib/opencode/provider-check.ts
|
|
1036
|
+
function buildNoProviderWarning(hasProvider) {
|
|
1037
|
+
if (hasProvider !== false) return null;
|
|
1038
|
+
return "Warning: opencode has no authenticated model provider configured, so it won't be able to answer prompts. Run `opencode auth login` to set one up (see https://opencode.ai for details).";
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1016
1041
|
// src/lib/opencode/session.ts
|
|
1017
1042
|
function opencodeBase(port) {
|
|
1018
1043
|
return `http://127.0.0.1:${port}`;
|
|
@@ -1078,6 +1103,84 @@ async function getSessionMessages(port, sessionId) {
|
|
|
1078
1103
|
return null;
|
|
1079
1104
|
}
|
|
1080
1105
|
}
|
|
1106
|
+
function isSessionActivelyGenerating(messages) {
|
|
1107
|
+
if (!messages || messages.length === 0) return false;
|
|
1108
|
+
const last = messages[messages.length - 1];
|
|
1109
|
+
if (roleOf(last) !== "assistant") return false;
|
|
1110
|
+
return completedOf(last) == null;
|
|
1111
|
+
}
|
|
1112
|
+
function sessionLastActivityMs(session) {
|
|
1113
|
+
const candidates = [
|
|
1114
|
+
session.time?.updated,
|
|
1115
|
+
session.time?.created,
|
|
1116
|
+
session.time_updated,
|
|
1117
|
+
session.time_created,
|
|
1118
|
+
session.updated,
|
|
1119
|
+
session.created
|
|
1120
|
+
];
|
|
1121
|
+
for (const c of candidates) {
|
|
1122
|
+
if (typeof c === "number" && Number.isFinite(c)) return c;
|
|
1123
|
+
}
|
|
1124
|
+
return null;
|
|
1125
|
+
}
|
|
1126
|
+
async function listSessions(port) {
|
|
1127
|
+
try {
|
|
1128
|
+
const res = await fetch(`${opencodeBase(port)}/session`);
|
|
1129
|
+
if (!res.ok) return null;
|
|
1130
|
+
const body = await res.json();
|
|
1131
|
+
return Array.isArray(body) ? body : null;
|
|
1132
|
+
} catch {
|
|
1133
|
+
return null;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
async function deleteSession(port, id) {
|
|
1137
|
+
try {
|
|
1138
|
+
const res = await fetch(`${opencodeBase(port)}/session/${id}`, { method: "DELETE" });
|
|
1139
|
+
return res.status >= 200 && res.status < 300;
|
|
1140
|
+
} catch {
|
|
1141
|
+
return false;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
async function sessionExists(port, id) {
|
|
1145
|
+
try {
|
|
1146
|
+
const res = await fetch(`${opencodeBase(port)}/session/${id}`);
|
|
1147
|
+
if (res.status >= 200 && res.status < 300) return true;
|
|
1148
|
+
if (res.status === 404) return false;
|
|
1149
|
+
return null;
|
|
1150
|
+
} catch {
|
|
1151
|
+
return null;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
async function getSessionStatuses(port) {
|
|
1155
|
+
try {
|
|
1156
|
+
const res = await fetch(`${opencodeBase(port)}/session/status`);
|
|
1157
|
+
if (!res.ok) {
|
|
1158
|
+
console.error(
|
|
1159
|
+
`[getSessionStatuses] GET /session/status returned HTTP ${res.status} (port ${port})`
|
|
1160
|
+
);
|
|
1161
|
+
return null;
|
|
1162
|
+
}
|
|
1163
|
+
const body = await res.json();
|
|
1164
|
+
if (body == null || typeof body !== "object" || Array.isArray(body)) {
|
|
1165
|
+
console.error(
|
|
1166
|
+
`[getSessionStatuses] GET /session/status body was not a plain object (port ${port})`
|
|
1167
|
+
);
|
|
1168
|
+
return null;
|
|
1169
|
+
}
|
|
1170
|
+
return body;
|
|
1171
|
+
} catch (err) {
|
|
1172
|
+
console.error(
|
|
1173
|
+
`[getSessionStatuses] GET /session/status failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
1174
|
+
);
|
|
1175
|
+
return null;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
async function isSessionOngoing(port, id) {
|
|
1179
|
+
const map = await getSessionStatuses(port);
|
|
1180
|
+
if (map == null) return null;
|
|
1181
|
+
const entry = map[id];
|
|
1182
|
+
return entry != null && entry.type !== "idle";
|
|
1183
|
+
}
|
|
1081
1184
|
async function createOpenCodeSession(port, directory) {
|
|
1082
1185
|
const url = new URL(`${opencodeBase(port)}/session`);
|
|
1083
1186
|
if (directory && directory.trim()) {
|
|
@@ -1095,17 +1198,128 @@ async function createOpenCodeSession(port, directory) {
|
|
|
1095
1198
|
const data = await response.json();
|
|
1096
1199
|
return data.id;
|
|
1097
1200
|
}
|
|
1201
|
+
async function getModelAttachmentCapability(port, model) {
|
|
1202
|
+
try {
|
|
1203
|
+
const res = await fetch(`${opencodeBase(port)}/config/providers`);
|
|
1204
|
+
if (!res.ok) {
|
|
1205
|
+
console.error(
|
|
1206
|
+
`[getModelAttachmentCapability] GET /config/providers returned HTTP ${res.status} (port ${port})`
|
|
1207
|
+
);
|
|
1208
|
+
return null;
|
|
1209
|
+
}
|
|
1210
|
+
const body = await res.json();
|
|
1211
|
+
const providers = Array.isArray(body?.providers) ? body.providers : null;
|
|
1212
|
+
if (!providers) {
|
|
1213
|
+
console.error(
|
|
1214
|
+
`[getModelAttachmentCapability] GET /config/providers body had no providers array (port ${port})`
|
|
1215
|
+
);
|
|
1216
|
+
return null;
|
|
1217
|
+
}
|
|
1218
|
+
const slash = model ? model.indexOf("/") : -1;
|
|
1219
|
+
const providerId = slash > 0 ? model.slice(0, slash) : void 0;
|
|
1220
|
+
let modelId = slash > 0 ? model.slice(slash + 1) : void 0;
|
|
1221
|
+
const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
|
|
1222
|
+
let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
|
|
1223
|
+
if (!provider && !providerId) {
|
|
1224
|
+
const defaultProviderIds = defaults2 ? Object.keys(defaults2) : [];
|
|
1225
|
+
if (defaultProviderIds.length === 1) {
|
|
1226
|
+
provider = providers.find((p) => p?.id === defaultProviderIds[0]);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
if (!provider || !provider.models) return null;
|
|
1230
|
+
if (!modelId && defaults2 && typeof provider.id === "string") {
|
|
1231
|
+
const def = defaults2[provider.id];
|
|
1232
|
+
if (typeof def === "string") modelId = def;
|
|
1233
|
+
}
|
|
1234
|
+
if (!modelId) {
|
|
1235
|
+
if (providerId) {
|
|
1236
|
+
const keys = Object.keys(provider.models);
|
|
1237
|
+
if (keys.length === 1) modelId = keys[0];
|
|
1238
|
+
}
|
|
1239
|
+
if (!modelId) return null;
|
|
1240
|
+
}
|
|
1241
|
+
const entry = provider.models[modelId];
|
|
1242
|
+
if (!entry || typeof entry !== "object") return null;
|
|
1243
|
+
if (entry.capabilities && typeof entry.capabilities === "object") {
|
|
1244
|
+
if (typeof entry.capabilities.attachment === "boolean") {
|
|
1245
|
+
return entry.capabilities.attachment;
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
return typeof entry.attachment === "boolean" ? entry.attachment : null;
|
|
1249
|
+
} catch (err) {
|
|
1250
|
+
console.error(
|
|
1251
|
+
`[getModelAttachmentCapability] GET /config/providers failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
1252
|
+
);
|
|
1253
|
+
return null;
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
async function buildFileParts(attachments, capable) {
|
|
1257
|
+
const outcomes = [];
|
|
1258
|
+
const parts = [];
|
|
1259
|
+
const capabilityUnknown = capable === null;
|
|
1260
|
+
if (capable !== true) {
|
|
1261
|
+
for (const a of attachments.inputs) {
|
|
1262
|
+
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "skipped" });
|
|
1263
|
+
}
|
|
1264
|
+
return { parts, outcomes, capabilityUnknown };
|
|
1265
|
+
}
|
|
1266
|
+
for (const a of attachments.inputs) {
|
|
1267
|
+
let dataUrl = null;
|
|
1268
|
+
try {
|
|
1269
|
+
dataUrl = await attachments.fetchDataUrl(a.index);
|
|
1270
|
+
} catch (err) {
|
|
1271
|
+
console.error(
|
|
1272
|
+
`[buildFileParts] attachment ${a.index} (${a.mime}) fetch threw \u2014 omitting: ${err instanceof Error ? err.message : String(err)}`
|
|
1273
|
+
);
|
|
1274
|
+
dataUrl = null;
|
|
1275
|
+
}
|
|
1276
|
+
if (dataUrl !== null && typeof dataUrl === "object") {
|
|
1277
|
+
outcomes.push({
|
|
1278
|
+
index: a.index,
|
|
1279
|
+
mime: a.mime,
|
|
1280
|
+
filename: a.filename,
|
|
1281
|
+
status: "failed",
|
|
1282
|
+
reason: "needs_reauth"
|
|
1283
|
+
});
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
if (dataUrl == null) {
|
|
1287
|
+
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "failed" });
|
|
1288
|
+
continue;
|
|
1289
|
+
}
|
|
1290
|
+
parts.push({
|
|
1291
|
+
type: "file",
|
|
1292
|
+
mime: a.mime,
|
|
1293
|
+
url: dataUrl,
|
|
1294
|
+
...a.filename ? { filename: a.filename } : {}
|
|
1295
|
+
});
|
|
1296
|
+
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "sent" });
|
|
1297
|
+
}
|
|
1298
|
+
return { parts, outcomes, capabilityUnknown };
|
|
1299
|
+
}
|
|
1098
1300
|
function messageText(m) {
|
|
1099
1301
|
if (!m || !Array.isArray(m.parts)) return "";
|
|
1100
1302
|
return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
1101
1303
|
}
|
|
1102
|
-
async function sendPromptAsync(port, sessionId, content, options) {
|
|
1304
|
+
async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
1103
1305
|
const before = await getSessionMessages(port, sessionId);
|
|
1104
1306
|
const knownUserIds = new Set(
|
|
1105
1307
|
(before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
|
|
1106
1308
|
);
|
|
1309
|
+
const parts = [{ type: "text", text: content }];
|
|
1310
|
+
let pendingOutcomes = null;
|
|
1311
|
+
if (attachments && attachments.inputs.length > 0) {
|
|
1312
|
+
const capable = await getModelAttachmentCapability(port, options?.model);
|
|
1313
|
+
const {
|
|
1314
|
+
parts: fileParts,
|
|
1315
|
+
outcomes,
|
|
1316
|
+
capabilityUnknown
|
|
1317
|
+
} = await buildFileParts(attachments, capable);
|
|
1318
|
+
parts.push(...fileParts);
|
|
1319
|
+
if (attachments.onOutcomes) pendingOutcomes = { outcomes, capabilityUnknown };
|
|
1320
|
+
}
|
|
1107
1321
|
const body = {
|
|
1108
|
-
parts
|
|
1322
|
+
parts
|
|
1109
1323
|
};
|
|
1110
1324
|
if (options?.agent) {
|
|
1111
1325
|
body.agent = options.agent;
|
|
@@ -1144,10 +1358,13 @@ async function sendPromptAsync(port, sessionId, content, options) {
|
|
|
1144
1358
|
best = { id, created };
|
|
1145
1359
|
}
|
|
1146
1360
|
}
|
|
1147
|
-
if (best)
|
|
1361
|
+
if (best) {
|
|
1362
|
+
if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
|
|
1363
|
+
return best.id;
|
|
1364
|
+
}
|
|
1148
1365
|
}
|
|
1149
1366
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
1150
|
-
await new Promise((
|
|
1367
|
+
await new Promise((resolve2) => setTimeout(resolve2, READ_BACK_DELAY_MS));
|
|
1151
1368
|
}
|
|
1152
1369
|
}
|
|
1153
1370
|
return null;
|
|
@@ -1193,6 +1410,72 @@ function findLastAssistantReplyFor(messages, userMessageId) {
|
|
|
1193
1410
|
}
|
|
1194
1411
|
return lastOk ?? last;
|
|
1195
1412
|
}
|
|
1413
|
+
function messageUsage(messages, userMessageId) {
|
|
1414
|
+
if (!messages || messages.length === 0) return null;
|
|
1415
|
+
const byParentAll = messages.filter(
|
|
1416
|
+
(m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
|
|
1417
|
+
);
|
|
1418
|
+
const byParentNonErrored = byParentAll.filter((m) => errorOf(m) == null);
|
|
1419
|
+
const byParent = byParentNonErrored.length > 0 ? byParentNonErrored : byParentAll;
|
|
1420
|
+
let correlated;
|
|
1421
|
+
if (byParent.length > 0) {
|
|
1422
|
+
correlated = byParent;
|
|
1423
|
+
} else {
|
|
1424
|
+
const reply = findAssistantReplyAfter(messages, userMessageId);
|
|
1425
|
+
correlated = reply ? [reply] : [];
|
|
1426
|
+
}
|
|
1427
|
+
if (correlated.length === 0) return null;
|
|
1428
|
+
let sawAnyUsage = false;
|
|
1429
|
+
let inputSum = 0;
|
|
1430
|
+
let outputSum = 0;
|
|
1431
|
+
let reasoningSum = 0;
|
|
1432
|
+
let cacheReadSum = 0;
|
|
1433
|
+
let cacheWriteSum = 0;
|
|
1434
|
+
let costSum = 0;
|
|
1435
|
+
let sawCost = false;
|
|
1436
|
+
let modelId = null;
|
|
1437
|
+
let providerId = null;
|
|
1438
|
+
for (const m of correlated) {
|
|
1439
|
+
const info = m.info;
|
|
1440
|
+
if (!info) continue;
|
|
1441
|
+
const tokens = info.tokens;
|
|
1442
|
+
if (tokens) {
|
|
1443
|
+
sawAnyUsage = true;
|
|
1444
|
+
inputSum += tokens.input ?? 0;
|
|
1445
|
+
outputSum += tokens.output ?? 0;
|
|
1446
|
+
reasoningSum += tokens.reasoning ?? 0;
|
|
1447
|
+
cacheReadSum += tokens.cache?.read ?? 0;
|
|
1448
|
+
cacheWriteSum += tokens.cache?.write ?? 0;
|
|
1449
|
+
}
|
|
1450
|
+
if (typeof info.cost === "number") {
|
|
1451
|
+
sawAnyUsage = true;
|
|
1452
|
+
sawCost = true;
|
|
1453
|
+
costSum += info.cost;
|
|
1454
|
+
}
|
|
1455
|
+
if (typeof info.modelID === "string") {
|
|
1456
|
+
sawAnyUsage = true;
|
|
1457
|
+
modelId = info.modelID;
|
|
1458
|
+
}
|
|
1459
|
+
if (typeof info.providerID === "string") {
|
|
1460
|
+
sawAnyUsage = true;
|
|
1461
|
+
providerId = info.providerID;
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
if (!sawAnyUsage) return null;
|
|
1465
|
+
return {
|
|
1466
|
+
usage_provider_id: providerId,
|
|
1467
|
+
usage_model_id: modelId,
|
|
1468
|
+
usage_tokens_input: inputSum,
|
|
1469
|
+
usage_tokens_output: outputSum,
|
|
1470
|
+
usage_tokens_reasoning: reasoningSum,
|
|
1471
|
+
usage_tokens_cache_read: cacheReadSum,
|
|
1472
|
+
usage_tokens_cache_write: cacheWriteSum,
|
|
1473
|
+
// NULL means "OpenCode never reported a cost" (never inferred from
|
|
1474
|
+
// tokens) — distinct from a genuine 0-cost turn, which would set
|
|
1475
|
+
// `sawCost` true with `costSum === 0`.
|
|
1476
|
+
usage_cost_usd: sawCost ? costSum : null
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1196
1479
|
function messageRunState(messages, userMessageId) {
|
|
1197
1480
|
if (!messages || messages.length === 0) return "unknown";
|
|
1198
1481
|
const hasUser = messages.some((m) => idOf(m) === userMessageId);
|
|
@@ -1204,6 +1487,11 @@ function messageRunState(messages, userMessageId) {
|
|
|
1204
1487
|
if (isAssistantInFlight(reply)) return "running";
|
|
1205
1488
|
return errorOf(reply) != null ? "failed" : "done";
|
|
1206
1489
|
}
|
|
1490
|
+
function isPreamblePinnedRunning(messages, userMessageId) {
|
|
1491
|
+
if (messageRunState(messages, userMessageId) !== "running") return false;
|
|
1492
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1493
|
+
return completedOf(reply) != null && finishOf(reply) === "tool-calls";
|
|
1494
|
+
}
|
|
1207
1495
|
function messageError(messages, userMessageId) {
|
|
1208
1496
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1209
1497
|
const error2 = errorOf(reply);
|
|
@@ -1223,6 +1511,141 @@ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
|
|
|
1223
1511
|
(m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
|
|
1224
1512
|
);
|
|
1225
1513
|
}
|
|
1514
|
+
async function hasAnyConfiguredProvider(port) {
|
|
1515
|
+
try {
|
|
1516
|
+
const res = await fetch(`${opencodeBase(port)}/config/providers`);
|
|
1517
|
+
if (!res.ok) {
|
|
1518
|
+
console.error(
|
|
1519
|
+
`[hasAnyConfiguredProvider] GET /config/providers returned HTTP ${res.status} (port ${port})`
|
|
1520
|
+
);
|
|
1521
|
+
return null;
|
|
1522
|
+
}
|
|
1523
|
+
const body = await res.json();
|
|
1524
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
1525
|
+
console.error(
|
|
1526
|
+
`[hasAnyConfiguredProvider] GET /config/providers body was not a plain object (port ${port})`
|
|
1527
|
+
);
|
|
1528
|
+
return null;
|
|
1529
|
+
}
|
|
1530
|
+
const defaults2 = body.default;
|
|
1531
|
+
if (!defaults2 || typeof defaults2 !== "object" || Array.isArray(defaults2)) {
|
|
1532
|
+
console.error(
|
|
1533
|
+
`[hasAnyConfiguredProvider] GET /config/providers body had no \`default\` object (port ${port})`
|
|
1534
|
+
);
|
|
1535
|
+
return null;
|
|
1536
|
+
}
|
|
1537
|
+
return Object.keys(defaults2).length > 0;
|
|
1538
|
+
} catch (err) {
|
|
1539
|
+
console.error(
|
|
1540
|
+
`[hasAnyConfiguredProvider] GET /config/providers failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
1541
|
+
);
|
|
1542
|
+
return null;
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
// src/lib/opencode/session-cleanup.ts
|
|
1547
|
+
var DURATION_UNIT_MS = {
|
|
1548
|
+
s: 1e3,
|
|
1549
|
+
m: 60 * 1e3,
|
|
1550
|
+
h: 60 * 60 * 1e3,
|
|
1551
|
+
d: 24 * 60 * 60 * 1e3
|
|
1552
|
+
};
|
|
1553
|
+
function parseDurationMs(input) {
|
|
1554
|
+
const trimmed = input.trim();
|
|
1555
|
+
const match = /^(\d+)([smhd])$/.exec(trimmed);
|
|
1556
|
+
if (!match) {
|
|
1557
|
+
throw new Error(
|
|
1558
|
+
`Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
const value = Number(match[1]);
|
|
1562
|
+
if (value <= 0) {
|
|
1563
|
+
throw new Error(`Invalid duration "${input}": must be a positive value.`);
|
|
1564
|
+
}
|
|
1565
|
+
return value * DURATION_UNIT_MS[match[2]];
|
|
1566
|
+
}
|
|
1567
|
+
function selectSessionsToDelete(sessions, opts) {
|
|
1568
|
+
const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
|
|
1569
|
+
if (maxAgeMs === void 0 && maxCount === void 0) return [];
|
|
1570
|
+
const ageEligible = (s) => {
|
|
1571
|
+
if (maxAgeMs === void 0) return false;
|
|
1572
|
+
if (s.lastActivityMs === null) return true;
|
|
1573
|
+
return nowMs - s.lastActivityMs > maxAgeMs;
|
|
1574
|
+
};
|
|
1575
|
+
const countEligibleIds = /* @__PURE__ */ new Set();
|
|
1576
|
+
if (maxCount !== void 0) {
|
|
1577
|
+
const byActivityDesc = [...sessions].sort(
|
|
1578
|
+
(a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
|
|
1579
|
+
);
|
|
1580
|
+
for (const s of byActivityDesc.slice(maxCount)) {
|
|
1581
|
+
countEligibleIds.add(s.id);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
const toDelete = [];
|
|
1585
|
+
for (const s of sessions) {
|
|
1586
|
+
if (protectedIds.has(s.id)) continue;
|
|
1587
|
+
if (ageEligible(s) || countEligibleIds.has(s.id)) {
|
|
1588
|
+
toDelete.push(s.id);
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
return toDelete;
|
|
1592
|
+
}
|
|
1593
|
+
var DEFAULT_INTERVAL = "1h";
|
|
1594
|
+
function resolve(flag, envValue, fallback) {
|
|
1595
|
+
return flag ?? envValue ?? fallback;
|
|
1596
|
+
}
|
|
1597
|
+
function parseMaxCount(input) {
|
|
1598
|
+
const trimmed = input.trim();
|
|
1599
|
+
if (!/^\d+$/.test(trimmed)) {
|
|
1600
|
+
throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
|
|
1601
|
+
}
|
|
1602
|
+
const value = Number(trimmed);
|
|
1603
|
+
if (value <= 0) {
|
|
1604
|
+
throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
|
|
1605
|
+
}
|
|
1606
|
+
return value;
|
|
1607
|
+
}
|
|
1608
|
+
function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
1609
|
+
const warnings = [];
|
|
1610
|
+
const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
|
|
1611
|
+
const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
|
|
1612
|
+
const intervalRaw = resolve(
|
|
1613
|
+
flags.interval,
|
|
1614
|
+
env.EVIDENT_SESSION_CLEANUP_INTERVAL,
|
|
1615
|
+
DEFAULT_INTERVAL
|
|
1616
|
+
);
|
|
1617
|
+
let maxAgeMs;
|
|
1618
|
+
if (maxAgeRaw !== void 0) {
|
|
1619
|
+
try {
|
|
1620
|
+
maxAgeMs = parseDurationMs(maxAgeRaw);
|
|
1621
|
+
} catch (err) {
|
|
1622
|
+
warnings.push(
|
|
1623
|
+
`Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
|
|
1624
|
+
);
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
let maxCount;
|
|
1628
|
+
if (maxCountRaw !== void 0) {
|
|
1629
|
+
try {
|
|
1630
|
+
maxCount = parseMaxCount(maxCountRaw);
|
|
1631
|
+
} catch (err) {
|
|
1632
|
+
warnings.push(
|
|
1633
|
+
`Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
|
|
1634
|
+
);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
let intervalMs;
|
|
1638
|
+
try {
|
|
1639
|
+
intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
|
|
1640
|
+
} catch (err) {
|
|
1641
|
+
warnings.push(
|
|
1642
|
+
`Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
|
|
1643
|
+
);
|
|
1644
|
+
intervalMs = parseDurationMs(DEFAULT_INTERVAL);
|
|
1645
|
+
}
|
|
1646
|
+
const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
|
|
1647
|
+
return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
|
|
1648
|
+
}
|
|
1226
1649
|
|
|
1227
1650
|
// src/lib/tunnel/connection.ts
|
|
1228
1651
|
import WebSocket2 from "ws";
|
|
@@ -1277,10 +1700,11 @@ var StreamForwarder = class {
|
|
|
1277
1700
|
* Abort every in-flight stream (e.g. on WebSocket close).
|
|
1278
1701
|
*/
|
|
1279
1702
|
abortAll() {
|
|
1280
|
-
for (const stream of this.inflight.
|
|
1703
|
+
for (const [sid, stream] of this.inflight.entries()) {
|
|
1281
1704
|
try {
|
|
1282
1705
|
stream.abort();
|
|
1283
|
-
} catch {
|
|
1706
|
+
} catch (err) {
|
|
1707
|
+
log("error", "forwarder_abort_failed", { sid, ...errorFields(err) });
|
|
1284
1708
|
}
|
|
1285
1709
|
}
|
|
1286
1710
|
this.inflight.clear();
|
|
@@ -1314,12 +1738,12 @@ var StreamForwarder = class {
|
|
|
1314
1738
|
let endBody;
|
|
1315
1739
|
if (has_body) {
|
|
1316
1740
|
const chunks = [];
|
|
1317
|
-
bodyPromise = new Promise((
|
|
1741
|
+
bodyPromise = new Promise((resolve2) => {
|
|
1318
1742
|
pushBody = (buf) => {
|
|
1319
1743
|
chunks.push(buf);
|
|
1320
1744
|
};
|
|
1321
1745
|
endBody = () => {
|
|
1322
|
-
|
|
1746
|
+
resolve2(Buffer.concat(chunks));
|
|
1323
1747
|
};
|
|
1324
1748
|
});
|
|
1325
1749
|
}
|
|
@@ -1430,31 +1854,20 @@ function connectTunnel(options) {
|
|
|
1430
1854
|
onConnected,
|
|
1431
1855
|
onDisconnected,
|
|
1432
1856
|
onError,
|
|
1433
|
-
onRequest,
|
|
1434
1857
|
onResponse,
|
|
1435
1858
|
onInfo,
|
|
1436
1859
|
onDrainPing
|
|
1437
1860
|
} = options;
|
|
1438
1861
|
const tunnelUrl = getTunnelUrlConfig();
|
|
1439
1862
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
1440
|
-
return new Promise((
|
|
1863
|
+
return new Promise((resolve2, reject) => {
|
|
1441
1864
|
const ws = new WebSocket2(url, {
|
|
1442
1865
|
headers: {
|
|
1443
1866
|
Authorization: authHeader
|
|
1444
1867
|
}
|
|
1445
1868
|
});
|
|
1446
|
-
const streamStartTimes = /* @__PURE__ */ new Map();
|
|
1447
1869
|
const forwarder = new StreamForwarder(ws, port, {
|
|
1448
|
-
|
|
1449
|
-
if (path === TUNNEL_DRAIN_PING_PATH) return;
|
|
1450
|
-
streamStartTimes.set(sid, Date.now());
|
|
1451
|
-
onRequest?.(method, path, sid);
|
|
1452
|
-
},
|
|
1453
|
-
onHead: (sid, status) => {
|
|
1454
|
-
const startedAt = streamStartTimes.get(sid);
|
|
1455
|
-
streamStartTimes.delete(sid);
|
|
1456
|
-
onResponse?.(status, startedAt ? Date.now() - startedAt : 0, sid);
|
|
1457
|
-
},
|
|
1870
|
+
onHead: () => onResponse?.(),
|
|
1458
1871
|
onDrainPing: () => onDrainPing?.()
|
|
1459
1872
|
});
|
|
1460
1873
|
const connectionTimeout = setTimeout(() => {
|
|
@@ -1502,7 +1915,7 @@ function connectTunnel(options) {
|
|
|
1502
1915
|
clearTimeout(connectionTimeout);
|
|
1503
1916
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
1504
1917
|
onConnected?.(connectedAgentId);
|
|
1505
|
-
|
|
1918
|
+
resolve2({
|
|
1506
1919
|
ws,
|
|
1507
1920
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
1508
1921
|
});
|
|
@@ -1530,7 +1943,6 @@ function connectTunnel(options) {
|
|
|
1530
1943
|
ws.on("close", (code, reason) => {
|
|
1531
1944
|
const reasonStr = reason.toString() || upgradeRejection || (code === 1006 ? "abnormal closure" : "No reason provided");
|
|
1532
1945
|
forwarder.abortAll();
|
|
1533
|
-
streamStartTimes.clear();
|
|
1534
1946
|
onDisconnected?.(code, reasonStr);
|
|
1535
1947
|
});
|
|
1536
1948
|
});
|
|
@@ -1565,7 +1977,11 @@ var RunnerConnection = class {
|
|
|
1565
1977
|
if (this.connection) {
|
|
1566
1978
|
try {
|
|
1567
1979
|
this.connection.close();
|
|
1568
|
-
} catch {
|
|
1980
|
+
} catch (err) {
|
|
1981
|
+
log("error", "runner_connection_close_failed", {
|
|
1982
|
+
agent_id: this.resolvedAgentId,
|
|
1983
|
+
...errorFields(err)
|
|
1984
|
+
});
|
|
1569
1985
|
}
|
|
1570
1986
|
this.connection = null;
|
|
1571
1987
|
}
|
|
@@ -1624,6 +2040,17 @@ function messageIdOf(m) {
|
|
|
1624
2040
|
const infoId = m.info?.id;
|
|
1625
2041
|
return typeof infoId === "string" ? infoId : void 0;
|
|
1626
2042
|
}
|
|
2043
|
+
function cleanImageMime(contentType) {
|
|
2044
|
+
if (!contentType) return null;
|
|
2045
|
+
const media = contentType.split(";")[0].trim().toLowerCase();
|
|
2046
|
+
return /^image\/[a-z0-9.+-]+$/.test(media) ? media : null;
|
|
2047
|
+
}
|
|
2048
|
+
var LOG_LEVELS = {
|
|
2049
|
+
debug: 0,
|
|
2050
|
+
info: 1,
|
|
2051
|
+
warn: 2,
|
|
2052
|
+
error: 3
|
|
2053
|
+
};
|
|
1627
2054
|
var DEFAULT_RETRY_POLICY = {
|
|
1628
2055
|
maxAttempts: 6,
|
|
1629
2056
|
baseDelayMs: 500,
|
|
@@ -1632,6 +2059,10 @@ var DEFAULT_RETRY_POLICY = {
|
|
|
1632
2059
|
var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
|
|
1633
2060
|
var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
1634
2061
|
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
2062
|
+
var HEARTBEAT_MS = 6e4;
|
|
2063
|
+
var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
|
|
2064
|
+
var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
|
|
2065
|
+
var MAX_SUPERSEDED_CONVERSATIONS = 256;
|
|
1635
2066
|
var ChannelAuthError = class extends Error {
|
|
1636
2067
|
constructor(message) {
|
|
1637
2068
|
super(message);
|
|
@@ -1654,7 +2085,7 @@ function backoffDelay(attempt, policy) {
|
|
|
1654
2085
|
function isRetryableStatus(status) {
|
|
1655
2086
|
return status === 429 || status >= 500 && status <= 599;
|
|
1656
2087
|
}
|
|
1657
|
-
var ChannelDriver = class {
|
|
2088
|
+
var ChannelDriver = class _ChannelDriver {
|
|
1658
2089
|
agentId;
|
|
1659
2090
|
port;
|
|
1660
2091
|
apiUrl;
|
|
@@ -1670,6 +2101,34 @@ var ChannelDriver = class {
|
|
|
1670
2101
|
now;
|
|
1671
2102
|
/** Cache of conversationId → opencode sessionId. */
|
|
1672
2103
|
sessions = /* @__PURE__ */ new Map();
|
|
2104
|
+
/**
|
|
2105
|
+
* conversationId → the opencode session this runner has ABANDONED as that
|
|
2106
|
+
* conversation's binding (#553), after a genuine (`sessionExists === true`)
|
|
2107
|
+
* dispatch failure: the session still exists but is wedged, so #485's self-heal
|
|
2108
|
+
* must bind a fresh one.
|
|
2109
|
+
*
|
|
2110
|
+
* Dropping the local binding + clearing the server row is not enough on its own:
|
|
2111
|
+
* a SIBLING message dispatched earlier in the same drain is still in-flight under
|
|
2112
|
+
* the same session, and its watcher's routine status writes carry
|
|
2113
|
+
* `opencode_session_id`, RESURRECTING the wedged id server-side after the clear —
|
|
2114
|
+
* and `ensureSession`'s persisted-id fallback then reuses it, defeating the
|
|
2115
|
+
* self-heal. This map makes the runner authoritative instead of racing those
|
|
2116
|
+
* writes: *`ensureSession` never reuses an abandoned id for that conversation,
|
|
2117
|
+
* whatever the server row says* — which holds even when the resurrecting write
|
|
2118
|
+
* is one we deliberately keep (see `markDone`).
|
|
2119
|
+
*
|
|
2120
|
+
* Bounded by construction, on both axes: keyed by CONVERSATION, so N failures on
|
|
2121
|
+
* one conversation hold ONE entry (the newest abandonment replaces the older), and
|
|
2122
|
+
* hard-capped at `MAX_SUPERSEDED_CONVERSATIONS` with FIFO eviction. Only the
|
|
2123
|
+
* NEWEST abandoned id per conversation is guarded: after a second abandonment a
|
|
2124
|
+
* late sibling of the FIRST session can write that id back and `ensureSession`
|
|
2125
|
+
* will reuse it — costing ONE repeat failure, which re-supersedes it. Deliberately
|
|
2126
|
+
* NOT dropped when the session's watcher tears down: `markDone` still writes the
|
|
2127
|
+
* abandoned id back (it must, or the reply is lost), so the guard has to outlive
|
|
2128
|
+
* the turn that resurrects it. In-memory only — a restart forgets it, at the same
|
|
2129
|
+
* bounded cost.
|
|
2130
|
+
*/
|
|
2131
|
+
supersededSessions = /* @__PURE__ */ new Map();
|
|
1673
2132
|
/**
|
|
1674
2133
|
* Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
|
|
1675
2134
|
* longer idempotent (no caller-supplied `messageID`), and its read-back picks
|
|
@@ -1728,6 +2187,15 @@ var ChannelDriver = class {
|
|
|
1728
2187
|
* the row leaves the processing list, exactly like `dontRedispatch`.
|
|
1729
2188
|
*/
|
|
1730
2189
|
doneUndeliverable = /* @__PURE__ */ new Set();
|
|
2190
|
+
/**
|
|
2191
|
+
* "Already emitted `readopt_poll_unresolved` for this row" (#229). The b1 /
|
|
2192
|
+
* unreadable-status re-evaluate leaf leaves the row UN-tracked so it is re-read
|
|
2193
|
+
* every ~2s drain until the status map becomes readable — but the server-visible
|
|
2194
|
+
* signal is an OUTCOME, so it must fire at most ONCE per row, not once per drain
|
|
2195
|
+
* (Bugbot "Re-adopt signals flood every drain"). Cleared when the row leaves the
|
|
2196
|
+
* processing list, exactly like `dontRedispatch`/`doneUndeliverable`.
|
|
2197
|
+
*/
|
|
2198
|
+
readoptPollUnresolvedSignalled = /* @__PURE__ */ new Set();
|
|
1731
2199
|
/**
|
|
1732
2200
|
* "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
|
|
1733
2201
|
* Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
|
|
@@ -1742,6 +2210,15 @@ var ChannelDriver = class {
|
|
|
1742
2210
|
* so the NEXT tick may retry exactly once more).
|
|
1743
2211
|
*/
|
|
1744
2212
|
awaitingReadopt = /* @__PURE__ */ new Set();
|
|
2213
|
+
/**
|
|
2214
|
+
* "Already signalled `attachments_skipped` for this Evident message id" (#376).
|
|
2215
|
+
* The in-thread skip note is an OUTCOME, so it must fire AT MOST ONCE per message
|
|
2216
|
+
* — never re-post on a re-dispatch of the same row (`forceReadoptRun` or the
|
|
2217
|
+
* next-tick null-id retry both re-run `sendPromptAsync`, which re-fires
|
|
2218
|
+
* `onOutcomes`). Mirrors `readoptPollUnresolvedSignalled`: a local dedup on the
|
|
2219
|
+
* outcome, not the dispatch. Not cleared (a message is signalled once for life).
|
|
2220
|
+
*/
|
|
2221
|
+
attachmentsSkippedSignalled = /* @__PURE__ */ new Set();
|
|
1745
2222
|
/**
|
|
1746
2223
|
* Cache of the opencode root directory (from `GET /path`). Resolved lazily on
|
|
1747
2224
|
* first session creation so drain-created sessions are rooted at the project
|
|
@@ -1759,6 +2236,19 @@ var ChannelDriver = class {
|
|
|
1759
2236
|
* entry = not yet resolved; `null` = resolved root (stop walking).
|
|
1760
2237
|
*/
|
|
1761
2238
|
sessionParents = /* @__PURE__ */ new Map();
|
|
2239
|
+
/**
|
|
2240
|
+
* Per-session OpenCode title cache (#310), keyed by sessionId. Only a resolved
|
|
2241
|
+
* NON-EMPTY, non-placeholder name is stored (terminal — a real session name
|
|
2242
|
+
* won't later un-name), so we do NOT re-GET `/session/:id` every tick. "Non-empty"
|
|
2243
|
+
* excludes OpenCode's synchronous default title (see
|
|
2244
|
+
* `OPENCODE_DEFAULT_TITLE_PREFIX`, #549) — that placeholder is treated the same
|
|
2245
|
+
* as an empty title so it never latches. A missing entry = not yet resolved OR
|
|
2246
|
+
* resolved-but-still-empty/placeholder → re-fetch on next need, since OpenCode
|
|
2247
|
+
* names sessions asynchronously mid-turn. Driver-level (not per-watcher) so both
|
|
2248
|
+
* the watcher completion path AND the restart-recovery re-adopt path (which has
|
|
2249
|
+
* no watcher) can resolve the title.
|
|
2250
|
+
*/
|
|
2251
|
+
sessionTitles = /* @__PURE__ */ new Map();
|
|
1762
2252
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
1763
2253
|
draining = false;
|
|
1764
2254
|
/**
|
|
@@ -1796,9 +2286,6 @@ var ChannelDriver = class {
|
|
|
1796
2286
|
get opencodeBase() {
|
|
1797
2287
|
return `http://127.0.0.1:${this.port}`;
|
|
1798
2288
|
}
|
|
1799
|
-
// -------------------------------------------------------------------------
|
|
1800
|
-
// Public API
|
|
1801
|
-
// -------------------------------------------------------------------------
|
|
1802
2289
|
/**
|
|
1803
2290
|
* Drain all pending channel conversations once: poll → dispatch → register.
|
|
1804
2291
|
* Called on tunnel `connected` (WI-CHAN-4) and on each poll tick by `run.ts`.
|
|
@@ -1854,6 +2341,28 @@ var ChannelDriver = class {
|
|
|
1854
2341
|
}
|
|
1855
2342
|
return false;
|
|
1856
2343
|
}
|
|
2344
|
+
/**
|
|
2345
|
+
* OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
|
|
2346
|
+
* exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
|
|
2347
|
+
* `watchers` entry whose `inFlight` set is non-empty — the same predicate
|
|
2348
|
+
* `hasInFlightWatchers()` uses, lifted to return the ids.
|
|
2349
|
+
*
|
|
2350
|
+
* Deliberately does NOT include `this.sessions` (the permanent, never-pruned
|
|
2351
|
+
* conversation→session cache). Protecting every bound-but-idle session there
|
|
2352
|
+
* would shield nearly every session and defeat cleanup — AND it is unnecessary:
|
|
2353
|
+
* `ensureSession` is self-healing (it recreates a session whose id no longer
|
|
2354
|
+
* exists), so deleting an idle bound session is harmless — the conversation's
|
|
2355
|
+
* next turn transparently rebinds a fresh one. The only thing worth protecting
|
|
2356
|
+
* is a session with a turn ACTIVELY in flight right now: tearing that down
|
|
2357
|
+
* mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
|
|
2358
|
+
*/
|
|
2359
|
+
protectedSessionIds() {
|
|
2360
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2361
|
+
for (const [sessionId, watcher] of this.watchers) {
|
|
2362
|
+
if (watcher.inFlight.size > 0) ids.add(sessionId);
|
|
2363
|
+
}
|
|
2364
|
+
return ids;
|
|
2365
|
+
}
|
|
1857
2366
|
/**
|
|
1858
2367
|
* Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
|
|
1859
2368
|
* this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
|
|
@@ -1918,9 +2427,7 @@ var ChannelDriver = class {
|
|
|
1918
2427
|
if (!stillLive) return;
|
|
1919
2428
|
}
|
|
1920
2429
|
}
|
|
1921
|
-
// -------------------------------------------------------------------------
|
|
1922
2430
|
// Conversation processing (WI-3 — async dispatch)
|
|
1923
|
-
// -------------------------------------------------------------------------
|
|
1924
2431
|
/**
|
|
1925
2432
|
* Dispatch each pending message for a conversation to opencode's native queue
|
|
1926
2433
|
* via `prompt_async` (Task 3.2) and register it with the conversation's
|
|
@@ -1930,10 +2437,15 @@ var ChannelDriver = class {
|
|
|
1930
2437
|
* @returns the count of messages NEWLY dispatched (not already in-flight).
|
|
1931
2438
|
*/
|
|
1932
2439
|
async processConversation(conv) {
|
|
1933
|
-
const sessionId = await this.ensureSession(conv);
|
|
2440
|
+
const { sessionId, refusedSessionId } = await this.ensureSession(conv);
|
|
1934
2441
|
const messages = await this.getPendingMessages(conv.id);
|
|
1935
2442
|
let dispatched = 0;
|
|
1936
2443
|
let skippedAlreadyDispatched = 0;
|
|
2444
|
+
if (refusedSessionId && messages.length > 0) {
|
|
2445
|
+
void this.postSignal(conv.id, messages[0].id, "session_superseded", {
|
|
2446
|
+
superseded_session_id: refusedSessionId
|
|
2447
|
+
});
|
|
2448
|
+
}
|
|
1937
2449
|
for (const message of messages) {
|
|
1938
2450
|
if (this.stopped) break;
|
|
1939
2451
|
if (this.dispatched.has(message.id)) {
|
|
@@ -1952,26 +2464,62 @@ var ChannelDriver = class {
|
|
|
1952
2464
|
conversation_id: conv.id,
|
|
1953
2465
|
message_id: message.id
|
|
1954
2466
|
});
|
|
2467
|
+
const sendAttachments = this.buildSendAttachments(conv, message);
|
|
1955
2468
|
opencodeMessageId = await this.dispatchLocked(
|
|
1956
2469
|
sessionId,
|
|
1957
|
-
() => sendPromptAsync(this.port, sessionId, message.content, options)
|
|
2470
|
+
() => sendPromptAsync(this.port, sessionId, message.content, options, sendAttachments)
|
|
1958
2471
|
);
|
|
1959
2472
|
} catch (err) {
|
|
1960
2473
|
if (err instanceof ChannelAuthError) throw err;
|
|
1961
2474
|
this.dispatched.delete(message.id);
|
|
1962
|
-
await this.
|
|
2475
|
+
const exists = await sessionExists(this.port, sessionId);
|
|
2476
|
+
if (exists === false) {
|
|
2477
|
+
this.sessions.delete(conv.id);
|
|
2478
|
+
this.log({
|
|
2479
|
+
level: "warn",
|
|
2480
|
+
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.`,
|
|
2481
|
+
conversation_id: conv.id,
|
|
2482
|
+
message_id: message.id
|
|
2483
|
+
});
|
|
2484
|
+
break;
|
|
2485
|
+
}
|
|
2486
|
+
if (exists === null) {
|
|
2487
|
+
this.log({
|
|
2488
|
+
level: "warn",
|
|
2489
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed and session (${sessionId.slice(0, 8)}) existence could not be confirmed (opencode momentarily unreachable) \u2014 deferring this and later messages for conversation ${conv.id.slice(0, 8)} to the next tick rather than treating it as a genuine failure.`,
|
|
2490
|
+
conversation_id: conv.id,
|
|
2491
|
+
message_id: message.id
|
|
2492
|
+
});
|
|
2493
|
+
break;
|
|
2494
|
+
}
|
|
2495
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
2496
|
+
this.sessions.delete(conv.id);
|
|
2497
|
+
this.supersede(conv.id, sessionId);
|
|
2498
|
+
this.log({
|
|
2499
|
+
level: "warn",
|
|
2500
|
+
message: `Abandoning OpenCode session ${sessionId.slice(0, 8)} as the binding for conversation ${conv.id.slice(0, 8)} (it exists but failed to run a turn) \u2014 a fresh session is created on the next tick, whatever the persisted binding says by then.`,
|
|
2501
|
+
conversation_id: conv.id,
|
|
2502
|
+
message_id: message.id
|
|
2503
|
+
});
|
|
2504
|
+
await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
|
|
2505
|
+
this.log({
|
|
2506
|
+
level: "warn",
|
|
2507
|
+
message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
|
|
2508
|
+
conversation_id: conv.id,
|
|
2509
|
+
message_id: message.id
|
|
2510
|
+
});
|
|
1963
2511
|
});
|
|
1964
2512
|
this.log({
|
|
1965
2513
|
level: "error",
|
|
1966
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
2514
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
|
|
1967
2515
|
conversation_id: conv.id,
|
|
1968
2516
|
message_id: message.id
|
|
1969
2517
|
});
|
|
1970
|
-
|
|
2518
|
+
break;
|
|
1971
2519
|
}
|
|
1972
2520
|
if (opencodeMessageId === null) {
|
|
1973
2521
|
this.log({
|
|
1974
|
-
level: "
|
|
2522
|
+
level: "warn",
|
|
1975
2523
|
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`,
|
|
1976
2524
|
conversation_id: conv.id,
|
|
1977
2525
|
message_id: message.id
|
|
@@ -1985,7 +2533,7 @@ var ChannelDriver = class {
|
|
|
1985
2533
|
}
|
|
1986
2534
|
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
1987
2535
|
this.log({
|
|
1988
|
-
level: "
|
|
2536
|
+
level: "warn",
|
|
1989
2537
|
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).`,
|
|
1990
2538
|
conversation_id: conv.id
|
|
1991
2539
|
});
|
|
@@ -1993,17 +2541,68 @@ var ChannelDriver = class {
|
|
|
1993
2541
|
this.ensureWatcherRunning(sessionId);
|
|
1994
2542
|
return dispatched;
|
|
1995
2543
|
}
|
|
2544
|
+
/**
|
|
2545
|
+
* Record that `sessionId` is no longer a valid binding for `conversationId`
|
|
2546
|
+
* (#553). Keyed by conversation and hard-capped, so it cannot grow with the
|
|
2547
|
+
* number of failures — see the `supersededSessions` field doc.
|
|
2548
|
+
*/
|
|
2549
|
+
supersede(conversationId, sessionId) {
|
|
2550
|
+
this.supersededSessions.delete(conversationId);
|
|
2551
|
+
this.supersededSessions.set(conversationId, sessionId);
|
|
2552
|
+
while (this.supersededSessions.size > MAX_SUPERSEDED_CONVERSATIONS) {
|
|
2553
|
+
const oldest = this.supersededSessions.keys().next().value;
|
|
2554
|
+
if (oldest === void 0) return;
|
|
2555
|
+
this.supersededSessions.delete(oldest);
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
/** Whether `sessionId` is the session this conversation has abandoned (#553). */
|
|
2559
|
+
isSuperseded(conversationId, sessionId) {
|
|
2560
|
+
return this.supersededSessions.get(conversationId) === sessionId;
|
|
2561
|
+
}
|
|
2562
|
+
/**
|
|
2563
|
+
* Resolve the opencode session to run this conversation's turns in.
|
|
2564
|
+
*
|
|
2565
|
+
* `refusedSessionId` is set when the #553 guard fired — i.e. the persisted
|
|
2566
|
+
* binding was an id this runner had abandoned, so a resurrection genuinely
|
|
2567
|
+
* happened and a fresh session was bound instead. The caller reports it.
|
|
2568
|
+
*/
|
|
1996
2569
|
async ensureSession(conv) {
|
|
1997
|
-
const
|
|
1998
|
-
if (
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2570
|
+
const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
|
|
2571
|
+
if (bound && this.isSuperseded(conv.id, bound)) {
|
|
2572
|
+
this.log({
|
|
2573
|
+
level: "warn",
|
|
2574
|
+
message: `OpenCode session ${bound.slice(0, 8)} was abandoned for conversation ${conv.id.slice(0, 8)} after a failed dispatch but is still bound to it (the persisted id was written back by a turn already in flight) \u2014 ignoring it and binding a fresh session.`,
|
|
2575
|
+
conversation_id: conv.id
|
|
2576
|
+
});
|
|
2577
|
+
this.sessions.delete(conv.id);
|
|
2578
|
+
return { sessionId: await this.createAndBindSession(conv.id), refusedSessionId: bound };
|
|
2002
2579
|
}
|
|
2580
|
+
if (bound) {
|
|
2581
|
+
const exists = await sessionExists(this.port, bound);
|
|
2582
|
+
if (exists === false) {
|
|
2583
|
+
this.log({
|
|
2584
|
+
level: "debug",
|
|
2585
|
+
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.`,
|
|
2586
|
+
conversation_id: conv.id
|
|
2587
|
+
});
|
|
2588
|
+
this.sessions.delete(conv.id);
|
|
2589
|
+
return { sessionId: await this.createAndBindSession(conv.id) };
|
|
2590
|
+
}
|
|
2591
|
+
this.sessions.set(conv.id, bound);
|
|
2592
|
+
return { sessionId: bound };
|
|
2593
|
+
}
|
|
2594
|
+
return { sessionId: await this.createAndBindSession(conv.id) };
|
|
2595
|
+
}
|
|
2596
|
+
/**
|
|
2597
|
+
* Create a fresh OpenCode session for a conversation, cache the binding, and
|
|
2598
|
+
* best-effort persist it server-side. Shared by the first-ever bind and the
|
|
2599
|
+
* self-heal recreate path in `ensureSession`.
|
|
2600
|
+
*/
|
|
2601
|
+
async createAndBindSession(conversationId) {
|
|
2003
2602
|
const directory = await this.resolveOpenCodeDirectory();
|
|
2004
2603
|
const sessionId = await createOpenCodeSession(this.port, directory);
|
|
2005
|
-
this.sessions.set(
|
|
2006
|
-
await this.persistSession(
|
|
2604
|
+
this.sessions.set(conversationId, sessionId);
|
|
2605
|
+
await this.persistSession(conversationId, sessionId).catch(() => {
|
|
2007
2606
|
});
|
|
2008
2607
|
return sessionId;
|
|
2009
2608
|
}
|
|
@@ -2017,15 +2616,13 @@ var ChannelDriver = class {
|
|
|
2017
2616
|
this.opencodeDirectory = await getOpenCodeDirectory(this.port);
|
|
2018
2617
|
if (!this.opencodeDirectory) {
|
|
2019
2618
|
this.log({
|
|
2020
|
-
level: "
|
|
2619
|
+
level: "warn",
|
|
2021
2620
|
message: "Could not determine opencode directory (GET /path) \u2014 new sessions may not appear in opencode web"
|
|
2022
2621
|
});
|
|
2023
2622
|
}
|
|
2024
2623
|
return this.opencodeDirectory;
|
|
2025
2624
|
}
|
|
2026
|
-
// -------------------------------------------------------------------------
|
|
2027
2625
|
// Per-session watcher (WI-3)
|
|
2028
|
-
// -------------------------------------------------------------------------
|
|
2029
2626
|
/**
|
|
2030
2627
|
* Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
|
|
2031
2628
|
* opencode session (Task 2.1a), so two dispatches into the SAME session can
|
|
@@ -2045,6 +2642,130 @@ var ChannelDriver = class {
|
|
|
2045
2642
|
);
|
|
2046
2643
|
return run2;
|
|
2047
2644
|
}
|
|
2645
|
+
// Inbound image attachments (#255, WI-8)
|
|
2646
|
+
/**
|
|
2647
|
+
* Build the `SendAttachmentsInput` for a message's inbound images, or
|
|
2648
|
+
* `undefined` when the message has none (so a text-only turn is unchanged).
|
|
2649
|
+
*
|
|
2650
|
+
* The driver OWNS the two channel-facing concerns the session module cannot:
|
|
2651
|
+
* - the AUTHENTICATED byte fetch through Evident's WI-6 endpoint
|
|
2652
|
+
* (`fetchAttachmentDataUrl`), using the SAME `getAuthHeader()` as every
|
|
2653
|
+
* other combinedAuth callback — the CLI NEVER talks to Slack directly;
|
|
2654
|
+
* - the in-thread SKIP NOTE (`signalAttachmentsSkipped`) posted over the
|
|
2655
|
+
* existing callback surface when any image was skipped/failed.
|
|
2656
|
+
* `sendPromptAsync` applies the capability gate + appends the `file` parts and
|
|
2657
|
+
* reports outcomes back via `onOutcomes`.
|
|
2658
|
+
*/
|
|
2659
|
+
buildSendAttachments(conv, message) {
|
|
2660
|
+
const refs = message.attachments;
|
|
2661
|
+
if (!refs || refs.length === 0) return void 0;
|
|
2662
|
+
return {
|
|
2663
|
+
inputs: refs.map((a, index) => ({
|
|
2664
|
+
index,
|
|
2665
|
+
mime: a.mime,
|
|
2666
|
+
...a.filename ? { filename: a.filename } : {}
|
|
2667
|
+
})),
|
|
2668
|
+
fetchDataUrl: (index) => this.fetchAttachmentDataUrl(message.id, index, refs[index].mime),
|
|
2669
|
+
onOutcomes: ({ outcomes, capabilityUnknown }) => this.signalAttachmentsSkipped(conv.id, message.id, outcomes, capabilityUnknown)
|
|
2670
|
+
};
|
|
2671
|
+
}
|
|
2672
|
+
/**
|
|
2673
|
+
* Fetch ONE inbound image's bytes through Evident's WI-6 endpoint
|
|
2674
|
+
* (`GET {apiUrl}/runners/{agentId}/attachments/{messageId}/{index}`) using the
|
|
2675
|
+
* existing authenticated fetch, and base64-encode into a
|
|
2676
|
+
* `data:<mime>;base64,<…>` URL for the opencode `file` part's `url`.
|
|
2677
|
+
*
|
|
2678
|
+
* The endpoint streams the source bytes verbatim (200), or returns 404
|
|
2679
|
+
* (not-owned / out-of-range / deleted-at-source / workspace gone) / 413
|
|
2680
|
+
* (over-cap). On ANY non-2xx or thrown failure we return `null` so the caller
|
|
2681
|
+
* OMITS that one image and the text turn still sends — NEVER throws the turn.
|
|
2682
|
+
* A 404 body carrying `{ reason: 'needs_reauth' }` (#547 — the server CONFIRMED
|
|
2683
|
+
* a Slack `files:read` scope problem via `files.info`) instead resolves the
|
|
2684
|
+
* `AttachmentFetchNeedsReauth` sentinel, so the in-thread note can steer the
|
|
2685
|
+
* user to reconnect Slack instead of a generic "unavailable". Failures are
|
|
2686
|
+
* logged with context (no silent swallow).
|
|
2687
|
+
*/
|
|
2688
|
+
async fetchAttachmentDataUrl(messageId, index, mime) {
|
|
2689
|
+
try {
|
|
2690
|
+
const res = await this.fetchImpl(
|
|
2691
|
+
`${this.apiUrl}/runners/${this.agentId}/attachments/${messageId}/${index}`,
|
|
2692
|
+
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2693
|
+
);
|
|
2694
|
+
if (!res.ok) {
|
|
2695
|
+
let reason;
|
|
2696
|
+
try {
|
|
2697
|
+
const body = await res.json();
|
|
2698
|
+
if (body && typeof body.reason === "string") reason = body.reason;
|
|
2699
|
+
} catch (parseErr) {
|
|
2700
|
+
this.log({
|
|
2701
|
+
level: "debug",
|
|
2702
|
+
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index}: error body was not JSON (${parseErr instanceof Error ? parseErr.message : String(parseErr)}) \u2014 treating as a plain failure`,
|
|
2703
|
+
message_id: messageId
|
|
2704
|
+
});
|
|
2705
|
+
}
|
|
2706
|
+
if (reason === "needs_reauth") {
|
|
2707
|
+
this.log({
|
|
2708
|
+
level: "error",
|
|
2709
|
+
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} returned HTTP ${res.status} \u2014 server confirmed a Slack reauth/scope problem \u2014 omitting this image (text turn proceeds)`,
|
|
2710
|
+
message_id: messageId
|
|
2711
|
+
});
|
|
2712
|
+
return { needsReauth: true };
|
|
2713
|
+
}
|
|
2714
|
+
this.log({
|
|
2715
|
+
level: "error",
|
|
2716
|
+
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} returned HTTP ${res.status} \u2014 omitting this image (text turn proceeds)`,
|
|
2717
|
+
message_id: messageId
|
|
2718
|
+
});
|
|
2719
|
+
return null;
|
|
2720
|
+
}
|
|
2721
|
+
const buf = await res.arrayBuffer();
|
|
2722
|
+
const base64 = Buffer.from(buf).toString("base64");
|
|
2723
|
+
const dataMime = cleanImageMime(res.headers.get("content-type")) || mime;
|
|
2724
|
+
return `data:${dataMime};base64,${base64}`;
|
|
2725
|
+
} catch (err) {
|
|
2726
|
+
this.log({
|
|
2727
|
+
level: "error",
|
|
2728
|
+
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} failed \u2014 omitting this image (text turn proceeds): ${err instanceof Error ? err.message : String(err)}`,
|
|
2729
|
+
message_id: messageId
|
|
2730
|
+
});
|
|
2731
|
+
return null;
|
|
2732
|
+
}
|
|
2733
|
+
}
|
|
2734
|
+
/**
|
|
2735
|
+
* On any skipped/failed image, post an in-thread note to Evident over the
|
|
2736
|
+
* EXISTING combinedAuth callback surface — the CLI NEVER posts to Slack directly.
|
|
2737
|
+
* Evident routes the note to source via `conversation.deliver`.
|
|
2738
|
+
*
|
|
2739
|
+
* The `POST .../messages/:id/signal` route accepts `attachments_skipped` (in
|
|
2740
|
+
* `messageSignalSchema`) and turns it into an in-thread note delivered through
|
|
2741
|
+
* `conversation.deliver` (e.g. "N image(s) couldn't be forwarded"), so the note
|
|
2742
|
+
* reaches the channel.
|
|
2743
|
+
*
|
|
2744
|
+
* Fire-and-forget: never throws into the send/tick (logs its own failure).
|
|
2745
|
+
*/
|
|
2746
|
+
signalAttachmentsSkipped(conversationId, messageId, outcomes, capabilityUnknown) {
|
|
2747
|
+
const skipped = outcomes.filter((o) => o.status === "skipped").length;
|
|
2748
|
+
const failed = outcomes.filter((o) => o.status === "failed").length;
|
|
2749
|
+
if (skipped === 0 && failed === 0) return;
|
|
2750
|
+
if (this.attachmentsSkippedSignalled.has(messageId)) return;
|
|
2751
|
+
this.attachmentsSkippedSignalled.add(messageId);
|
|
2752
|
+
const skippedReason = capabilityUnknown ? "unknown" : "unsupported";
|
|
2753
|
+
const failedReason = outcomes.some(
|
|
2754
|
+
(o) => o.status === "failed" && o.reason === "needs_reauth"
|
|
2755
|
+
) ? "needs_reauth" : void 0;
|
|
2756
|
+
this.log({
|
|
2757
|
+
level: "info",
|
|
2758
|
+
message: `Message ${messageId.slice(0, 8)}: ${skipped} image(s) skipped (${capabilityUnknown ? "capability was unreadable \u2014 failed open to text-only" : "model not attachment-capable"}), ${failed} image(s) unavailable (deleted-at-source or fetch failure) \u2014 noting to Evident`,
|
|
2759
|
+
conversation_id: conversationId,
|
|
2760
|
+
message_id: messageId
|
|
2761
|
+
});
|
|
2762
|
+
void this.postSignal(conversationId, messageId, "attachments_skipped", {
|
|
2763
|
+
skipped,
|
|
2764
|
+
failed,
|
|
2765
|
+
...skipped > 0 ? { skipped_reason: skippedReason } : {},
|
|
2766
|
+
...failedReason ? { failed_reason: failedReason } : {}
|
|
2767
|
+
});
|
|
2768
|
+
}
|
|
2048
2769
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
2049
2770
|
registerInFlight(conv, sessionId, message, opencodeMessageId) {
|
|
2050
2771
|
let watcher = this.watchers.get(sessionId);
|
|
@@ -2054,7 +2775,9 @@ var ChannelDriver = class {
|
|
|
2054
2775
|
inFlight: /* @__PURE__ */ new Map(),
|
|
2055
2776
|
loop: null,
|
|
2056
2777
|
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2057
|
-
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2778
|
+
reportedPermissions: /* @__PURE__ */ new Set(),
|
|
2779
|
+
lastGoodPollAt: this.now(),
|
|
2780
|
+
hadUsablePoll: false
|
|
2058
2781
|
};
|
|
2059
2782
|
this.watchers.set(sessionId, watcher);
|
|
2060
2783
|
}
|
|
@@ -2064,20 +2787,37 @@ var ChannelDriver = class {
|
|
|
2064
2787
|
opencodeMessageId,
|
|
2065
2788
|
message,
|
|
2066
2789
|
dispatchedAt: now,
|
|
2790
|
+
processingAnchorMs: now,
|
|
2067
2791
|
deadline: now + this.pausedMaxWaitMs,
|
|
2068
2792
|
started: false,
|
|
2069
2793
|
done: false,
|
|
2070
|
-
stuckReported: false
|
|
2794
|
+
stuckReported: false,
|
|
2795
|
+
lastAliveAt: 0,
|
|
2796
|
+
aliveInFlight: false,
|
|
2797
|
+
awaitingHumanLatched: false,
|
|
2798
|
+
pausedOnQuestion: false,
|
|
2799
|
+
pausedOnPermission: false,
|
|
2800
|
+
pausedClearConfirmed: false,
|
|
2801
|
+
pausedInFlight: false,
|
|
2802
|
+
deliveryDeadlineAnchored: false
|
|
2071
2803
|
});
|
|
2072
2804
|
}
|
|
2073
2805
|
/**
|
|
2074
2806
|
* Register a RE-ADOPTED `processing` message with its session watcher
|
|
2075
2807
|
* (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
|
|
2076
2808
|
* `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
|
|
2077
|
-
* `now
|
|
2078
|
-
* (10 min after `processed_at
|
|
2079
|
-
*
|
|
2080
|
-
*
|
|
2809
|
+
* `now`, so the paused/queued/unreachable cases settle on the same wall-clock a
|
|
2810
|
+
* fresh dispatch would (10 min after `processed_at`, not 10 min from now).
|
|
2811
|
+
*
|
|
2812
|
+
* This re-attaches into the SAME watcher, so the ADR-0047 progressing-vs-paused
|
|
2813
|
+
* give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
|
|
2814
|
+
* opencode reports ACTIVELY `running` is watched to completion (its liveness
|
|
2815
|
+
* heartbeat keeps the cron off its row), while a re-adopted turn that is paused
|
|
2816
|
+
* awaiting a human — or queued/unreachable — is still bounded by `deadline` and
|
|
2817
|
+
* handed to the cron. The old "the `deadline` must settle before the ~15-min
|
|
2818
|
+
* cron or they double-drive" reasoning is superseded: liveness now settles the
|
|
2819
|
+
* actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
|
|
2820
|
+
* (only the appear-guard uses it).
|
|
2081
2821
|
*
|
|
2082
2822
|
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
2083
2823
|
* `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
|
|
@@ -2095,7 +2835,9 @@ var ChannelDriver = class {
|
|
|
2095
2835
|
inFlight: /* @__PURE__ */ new Map(),
|
|
2096
2836
|
loop: null,
|
|
2097
2837
|
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2098
|
-
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2838
|
+
reportedPermissions: /* @__PURE__ */ new Set(),
|
|
2839
|
+
lastGoodPollAt: this.now(),
|
|
2840
|
+
hadUsablePoll: false
|
|
2099
2841
|
};
|
|
2100
2842
|
this.watchers.set(sessionId, watcher);
|
|
2101
2843
|
}
|
|
@@ -2104,6 +2846,10 @@ var ChannelDriver = class {
|
|
|
2104
2846
|
opencodeMessageId,
|
|
2105
2847
|
message,
|
|
2106
2848
|
dispatchedAt: this.now(),
|
|
2849
|
+
// Anchor the absolute-age ceiling to the SERVER-SIDE `processed_at` (the same
|
|
2850
|
+
// value seeding `deadline`), NOT `dispatchedAt` — so a re-adopted zombie's age
|
|
2851
|
+
// reflects the real turn duration and the ceiling fires on the ORIGINAL turn.
|
|
2852
|
+
processingAnchorMs: processedAtMs,
|
|
2107
2853
|
deadline: processedAtMs + this.pausedMaxWaitMs,
|
|
2108
2854
|
// The server row is ALREADY `processing`; do not re-fire markProcessing.
|
|
2109
2855
|
started: true,
|
|
@@ -2113,7 +2859,20 @@ var ChannelDriver = class {
|
|
|
2113
2859
|
// on `state === 'queued'` (turn produced no reply), not on `started`, so a
|
|
2114
2860
|
// re-adopted row left wedged in `queued` still emits the signal once
|
|
2115
2861
|
// (#210/#220 observability).
|
|
2116
|
-
stuckReported: false
|
|
2862
|
+
stuckReported: false,
|
|
2863
|
+
// Task 5.2: a re-adopted actively-running row re-attaches into the SAME
|
|
2864
|
+
// watcher and so hits the SAME actively-running heartbeat branch in
|
|
2865
|
+
// `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
|
|
2866
|
+
// re-adopted and is confirming this row alive" via that `alive` heartbeat,
|
|
2867
|
+
// with no extra `re_adopted` signal needed (folds old WI-6).
|
|
2868
|
+
lastAliveAt: 0,
|
|
2869
|
+
aliveInFlight: false,
|
|
2870
|
+
awaitingHumanLatched: false,
|
|
2871
|
+
pausedOnQuestion: false,
|
|
2872
|
+
pausedOnPermission: false,
|
|
2873
|
+
pausedClearConfirmed: false,
|
|
2874
|
+
pausedInFlight: false,
|
|
2875
|
+
deliveryDeadlineAnchored: false
|
|
2117
2876
|
});
|
|
2118
2877
|
}
|
|
2119
2878
|
/**
|
|
@@ -2164,12 +2923,30 @@ var ChannelDriver = class {
|
|
|
2164
2923
|
messages = Array.isArray(body) ? body : null;
|
|
2165
2924
|
}
|
|
2166
2925
|
} catch {
|
|
2167
|
-
continue;
|
|
2168
2926
|
}
|
|
2927
|
+
if (messages != null && messages.length > 0) {
|
|
2928
|
+
watcher.lastGoodPollAt = this.now();
|
|
2929
|
+
watcher.hadUsablePoll = true;
|
|
2930
|
+
} else {
|
|
2931
|
+
const emptyButReachable = messages != null;
|
|
2932
|
+
const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
|
|
2933
|
+
if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
|
|
2934
|
+
continue;
|
|
2935
|
+
}
|
|
2936
|
+
}
|
|
2937
|
+
const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
|
|
2169
2938
|
for (const inFlight of [...watcher.inFlight.values()]) {
|
|
2170
|
-
await this.serviceInFlightMessage(
|
|
2939
|
+
await this.serviceInFlightMessage(
|
|
2940
|
+
sessionId,
|
|
2941
|
+
watcher,
|
|
2942
|
+
inFlight,
|
|
2943
|
+
messages,
|
|
2944
|
+
openQuestions,
|
|
2945
|
+
openPermissions,
|
|
2946
|
+
questionsPolledOk,
|
|
2947
|
+
permissionsPolledOk
|
|
2948
|
+
);
|
|
2171
2949
|
}
|
|
2172
|
-
await this.pollInteractions(sessionId, watcher, messages);
|
|
2173
2950
|
}
|
|
2174
2951
|
} catch (err) {
|
|
2175
2952
|
if (err instanceof ChannelAuthError) {
|
|
@@ -2191,28 +2968,55 @@ var ChannelDriver = class {
|
|
|
2191
2968
|
});
|
|
2192
2969
|
}
|
|
2193
2970
|
}
|
|
2971
|
+
/**
|
|
2972
|
+
* On FIRST observing a terminal (done/failed) state, ensure the delivery
|
|
2973
|
+
* (markDone/markFailed) transient-retry path has a real window. A long
|
|
2974
|
+
* ACTIVELY-running turn is kept past its original `deadline`, so by completion
|
|
2975
|
+
* `now >= deadline` already holds and the retry bound below would fire on the
|
|
2976
|
+
* first transient PATCH failure — dropping the message before its reply lands
|
|
2977
|
+
* (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
|
|
2978
|
+
* to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
|
|
2979
|
+
* or past now, so a still-ample window is left untouched.
|
|
2980
|
+
*/
|
|
2981
|
+
anchorDeliveryDeadline(inFlight) {
|
|
2982
|
+
if (inFlight.deliveryDeadlineAnchored) return;
|
|
2983
|
+
inFlight.deliveryDeadlineAnchored = true;
|
|
2984
|
+
if (this.now() >= inFlight.deadline) {
|
|
2985
|
+
inFlight.deadline = this.now() + this.pausedMaxWaitMs;
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2194
2988
|
/**
|
|
2195
2989
|
* Drive ONE in-flight message's lifecycle from the tick's message snapshot.
|
|
2196
2990
|
* Fires markProcessing on queued→running and markDone on done (each once),
|
|
2197
2991
|
* applies the idle-path re-dispatch guard, and removes the message from the
|
|
2198
2992
|
* in-flight set on completion or timeout.
|
|
2199
2993
|
*/
|
|
2200
|
-
async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
|
|
2994
|
+
async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
|
|
2201
2995
|
const conv = watcher.conv;
|
|
2202
2996
|
const state = messageRunState(messages, inFlight.opencodeMessageId);
|
|
2997
|
+
const id = inFlight.evidentMessageId;
|
|
2998
|
+
if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
|
|
2999
|
+
else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
|
|
3000
|
+
if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
|
|
3001
|
+
else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
|
|
3002
|
+
const observedOpen = openQuestions.has(id) || openPermissions.has(id);
|
|
3003
|
+
const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
|
|
3004
|
+
const awaitingHuman = observedOpen || latchedPaused;
|
|
2203
3005
|
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
3006
|
+
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
2204
3007
|
let claimed;
|
|
2205
3008
|
try {
|
|
2206
3009
|
claimed = await this.markProcessing(
|
|
2207
3010
|
conv.id,
|
|
2208
3011
|
inFlight.evidentMessageId,
|
|
2209
3012
|
sessionId,
|
|
2210
|
-
inFlight.opencodeMessageId
|
|
3013
|
+
inFlight.opencodeMessageId,
|
|
3014
|
+
title
|
|
2211
3015
|
);
|
|
2212
3016
|
} catch (err) {
|
|
2213
3017
|
if (err instanceof ChannelAuthError) throw err;
|
|
2214
3018
|
this.log({
|
|
2215
|
-
level: "
|
|
3019
|
+
level: "warn",
|
|
2216
3020
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2217
3021
|
conversation_id: conv.id,
|
|
2218
3022
|
message_id: inFlight.evidentMessageId
|
|
@@ -2222,7 +3026,7 @@ var ChannelDriver = class {
|
|
|
2222
3026
|
inFlight.started = true;
|
|
2223
3027
|
if (!claimed) {
|
|
2224
3028
|
this.log({
|
|
2225
|
-
level: "
|
|
3029
|
+
level: "debug",
|
|
2226
3030
|
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
|
|
2227
3031
|
conversation_id: conv.id,
|
|
2228
3032
|
message_id: inFlight.evidentMessageId
|
|
@@ -2230,6 +3034,7 @@ var ChannelDriver = class {
|
|
|
2230
3034
|
}
|
|
2231
3035
|
}
|
|
2232
3036
|
if (state === "done") {
|
|
3037
|
+
this.anchorDeliveryDeadline(inFlight);
|
|
2233
3038
|
if (!inFlight.done) {
|
|
2234
3039
|
this.log({
|
|
2235
3040
|
level: "info",
|
|
@@ -2237,18 +3042,22 @@ var ChannelDriver = class {
|
|
|
2237
3042
|
conversation_id: conv.id,
|
|
2238
3043
|
message_id: inFlight.evidentMessageId
|
|
2239
3044
|
});
|
|
3045
|
+
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
3046
|
+
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
2240
3047
|
try {
|
|
2241
3048
|
await this.markDone(
|
|
2242
3049
|
conv.id,
|
|
2243
3050
|
inFlight.evidentMessageId,
|
|
2244
3051
|
sessionId,
|
|
2245
|
-
inFlight.opencodeMessageId
|
|
3052
|
+
inFlight.opencodeMessageId,
|
|
3053
|
+
title,
|
|
3054
|
+
usage
|
|
2246
3055
|
);
|
|
2247
3056
|
} catch (err) {
|
|
2248
3057
|
if (err instanceof ChannelAuthError) throw err;
|
|
2249
3058
|
if (err instanceof ChannelTerminalError) {
|
|
2250
3059
|
this.log({
|
|
2251
|
-
level: "
|
|
3060
|
+
level: "warn",
|
|
2252
3061
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2253
3062
|
conversation_id: conv.id,
|
|
2254
3063
|
message_id: inFlight.evidentMessageId
|
|
@@ -2258,7 +3067,7 @@ var ChannelDriver = class {
|
|
|
2258
3067
|
}
|
|
2259
3068
|
if (this.now() >= inFlight.deadline) {
|
|
2260
3069
|
this.log({
|
|
2261
|
-
level: "
|
|
3070
|
+
level: "warn",
|
|
2262
3071
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done within the watch window \u2014 leaving for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
|
|
2263
3072
|
conversation_id: conv.id,
|
|
2264
3073
|
message_id: inFlight.evidentMessageId
|
|
@@ -2267,7 +3076,7 @@ var ChannelDriver = class {
|
|
|
2267
3076
|
return;
|
|
2268
3077
|
}
|
|
2269
3078
|
this.log({
|
|
2270
|
-
level: "
|
|
3079
|
+
level: "warn",
|
|
2271
3080
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2272
3081
|
conversation_id: conv.id,
|
|
2273
3082
|
message_id: inFlight.evidentMessageId
|
|
@@ -2280,6 +3089,7 @@ var ChannelDriver = class {
|
|
|
2280
3089
|
return;
|
|
2281
3090
|
}
|
|
2282
3091
|
if (state === "failed") {
|
|
3092
|
+
this.anchorDeliveryDeadline(inFlight);
|
|
2283
3093
|
if (!inFlight.done) {
|
|
2284
3094
|
const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
|
|
2285
3095
|
this.log({
|
|
@@ -2288,13 +3098,14 @@ var ChannelDriver = class {
|
|
|
2288
3098
|
conversation_id: conv.id,
|
|
2289
3099
|
message_id: inFlight.evidentMessageId
|
|
2290
3100
|
});
|
|
3101
|
+
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
2291
3102
|
try {
|
|
2292
|
-
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
|
|
3103
|
+
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2, usage);
|
|
2293
3104
|
} catch (err) {
|
|
2294
3105
|
if (err instanceof ChannelAuthError) throw err;
|
|
2295
3106
|
if (err instanceof ChannelTerminalError) {
|
|
2296
3107
|
this.log({
|
|
2297
|
-
level: "
|
|
3108
|
+
level: "warn",
|
|
2298
3109
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2299
3110
|
conversation_id: conv.id,
|
|
2300
3111
|
message_id: inFlight.evidentMessageId
|
|
@@ -2304,7 +3115,7 @@ var ChannelDriver = class {
|
|
|
2304
3115
|
}
|
|
2305
3116
|
if (this.now() >= inFlight.deadline) {
|
|
2306
3117
|
this.log({
|
|
2307
|
-
level: "
|
|
3118
|
+
level: "warn",
|
|
2308
3119
|
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)}`,
|
|
2309
3120
|
conversation_id: conv.id,
|
|
2310
3121
|
message_id: inFlight.evidentMessageId
|
|
@@ -2313,7 +3124,7 @@ var ChannelDriver = class {
|
|
|
2313
3124
|
return;
|
|
2314
3125
|
}
|
|
2315
3126
|
this.log({
|
|
2316
|
-
level: "
|
|
3127
|
+
level: "warn",
|
|
2317
3128
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2318
3129
|
conversation_id: conv.id,
|
|
2319
3130
|
message_id: inFlight.evidentMessageId
|
|
@@ -2333,9 +3144,53 @@ var ChannelDriver = class {
|
|
|
2333
3144
|
stuck_for_ms: this.now() - inFlight.dispatchedAt
|
|
2334
3145
|
});
|
|
2335
3146
|
}
|
|
2336
|
-
|
|
3147
|
+
const activelyRunning = state === "running" && !awaitingHuman;
|
|
3148
|
+
if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
|
|
2337
3149
|
this.log({
|
|
2338
|
-
level: "
|
|
3150
|
+
level: "warn",
|
|
3151
|
+
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} exceeded the absolute processing ceiling (${Math.round((this.now() - inFlight.processingAnchorMs) / 6e4)}min, session ${sessionId}) while still actively running \u2014 releasing so the cron can reclaim it`,
|
|
3152
|
+
conversation_id: conv.id,
|
|
3153
|
+
message_id: inFlight.evidentMessageId
|
|
3154
|
+
});
|
|
3155
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
|
|
3156
|
+
watched_for_ms: this.now() - inFlight.processingAnchorMs
|
|
3157
|
+
});
|
|
3158
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3159
|
+
return;
|
|
3160
|
+
}
|
|
3161
|
+
if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
|
|
3162
|
+
inFlight.aliveInFlight = true;
|
|
3163
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
|
|
3164
|
+
inFlight.aliveInFlight = false;
|
|
3165
|
+
if (ok) inFlight.lastAliveAt = this.now();
|
|
3166
|
+
});
|
|
3167
|
+
}
|
|
3168
|
+
if (awaitingHuman) {
|
|
3169
|
+
if (!inFlight.awaitingHumanLatched) {
|
|
3170
|
+
inFlight.deadline = this.now() + this.pausedMaxWaitMs;
|
|
3171
|
+
inFlight.awaitingHumanLatched = true;
|
|
3172
|
+
}
|
|
3173
|
+
if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
|
|
3174
|
+
inFlight.pausedInFlight = true;
|
|
3175
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
|
|
3176
|
+
inFlight.pausedInFlight = false;
|
|
3177
|
+
if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
|
|
3178
|
+
});
|
|
3179
|
+
}
|
|
3180
|
+
} else if (inFlight.awaitingHumanLatched) {
|
|
3181
|
+
inFlight.awaitingHumanLatched = false;
|
|
3182
|
+
inFlight.pausedOnQuestion = false;
|
|
3183
|
+
inFlight.pausedOnPermission = false;
|
|
3184
|
+
inFlight.pausedClearConfirmed = false;
|
|
3185
|
+
}
|
|
3186
|
+
const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
|
|
3187
|
+
const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
|
|
3188
|
+
(sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
|
|
3189
|
+
);
|
|
3190
|
+
const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
|
|
3191
|
+
if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
|
|
3192
|
+
this.log({
|
|
3193
|
+
level: "debug",
|
|
2339
3194
|
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
|
|
2340
3195
|
conversation_id: conv.id,
|
|
2341
3196
|
message_id: inFlight.evidentMessageId
|
|
@@ -2346,9 +3201,7 @@ var ChannelDriver = class {
|
|
|
2346
3201
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2347
3202
|
}
|
|
2348
3203
|
}
|
|
2349
|
-
// -------------------------------------------------------------------------
|
|
2350
3204
|
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
2351
|
-
// -------------------------------------------------------------------------
|
|
2352
3205
|
/**
|
|
2353
3206
|
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
2354
3207
|
*
|
|
@@ -2365,15 +3218,20 @@ var ChannelDriver = class {
|
|
|
2365
3218
|
*/
|
|
2366
3219
|
async readoptProcessing() {
|
|
2367
3220
|
const rows = await this.getProcessingMessages();
|
|
2368
|
-
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
|
|
3221
|
+
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0 || this.readoptPollUnresolvedSignalled.size > 0) {
|
|
2369
3222
|
const stillProcessing = new Set(rows.map((r) => r.id));
|
|
2370
|
-
for (const id of [
|
|
3223
|
+
for (const id of [
|
|
3224
|
+
...this.dontRedispatch,
|
|
3225
|
+
...this.doneUndeliverable,
|
|
3226
|
+
...this.readoptPollUnresolvedSignalled
|
|
3227
|
+
]) {
|
|
2371
3228
|
if (!stillProcessing.has(id)) {
|
|
2372
3229
|
const cleared = this.dontRedispatch.delete(id);
|
|
2373
3230
|
const clearedUndeliverable = this.doneUndeliverable.delete(id);
|
|
3231
|
+
this.readoptPollUnresolvedSignalled.delete(id);
|
|
2374
3232
|
if (cleared || clearedUndeliverable) {
|
|
2375
3233
|
this.log({
|
|
2376
|
-
level: "
|
|
3234
|
+
level: "debug",
|
|
2377
3235
|
message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
|
|
2378
3236
|
message_id: id
|
|
2379
3237
|
});
|
|
@@ -2386,7 +3244,7 @@ var ChannelDriver = class {
|
|
|
2386
3244
|
for (const row of rows) {
|
|
2387
3245
|
if (!row.opencode_session_id) {
|
|
2388
3246
|
this.log({
|
|
2389
|
-
level: "
|
|
3247
|
+
level: "warn",
|
|
2390
3248
|
message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
|
|
2391
3249
|
conversation_id: row.conversation_id,
|
|
2392
3250
|
message_id: row.id
|
|
@@ -2403,7 +3261,7 @@ var ChannelDriver = class {
|
|
|
2403
3261
|
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
|
|
2404
3262
|
if (!res.ok) {
|
|
2405
3263
|
this.log({
|
|
2406
|
-
level: "
|
|
3264
|
+
level: "warn",
|
|
2407
3265
|
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
|
|
2408
3266
|
});
|
|
2409
3267
|
continue;
|
|
@@ -2411,7 +3269,7 @@ var ChannelDriver = class {
|
|
|
2411
3269
|
const body = await res.json();
|
|
2412
3270
|
if (!Array.isArray(body)) {
|
|
2413
3271
|
this.log({
|
|
2414
|
-
level: "
|
|
3272
|
+
level: "warn",
|
|
2415
3273
|
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
|
|
2416
3274
|
});
|
|
2417
3275
|
continue;
|
|
@@ -2419,13 +3277,15 @@ var ChannelDriver = class {
|
|
|
2419
3277
|
messages = body;
|
|
2420
3278
|
} catch (err) {
|
|
2421
3279
|
this.log({
|
|
2422
|
-
level: "
|
|
3280
|
+
level: "warn",
|
|
2423
3281
|
message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
|
|
2424
3282
|
});
|
|
2425
3283
|
continue;
|
|
2426
3284
|
}
|
|
3285
|
+
const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
|
|
3286
|
+
const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
|
|
2427
3287
|
for (const row of sessionRows) {
|
|
2428
|
-
await this.readoptOne(sessionId, row, messages);
|
|
3288
|
+
await this.readoptOne(sessionId, row, messages, sessionOngoing);
|
|
2429
3289
|
}
|
|
2430
3290
|
}
|
|
2431
3291
|
}
|
|
@@ -2447,10 +3307,10 @@ var ChannelDriver = class {
|
|
|
2447
3307
|
*
|
|
2448
3308
|
* Only `ChannelAuthError` propagates.
|
|
2449
3309
|
*/
|
|
2450
|
-
async readoptOne(sessionId, row, messages) {
|
|
3310
|
+
async readoptOne(sessionId, row, messages, sessionOngoing) {
|
|
2451
3311
|
if (this.isTracked(sessionId, row.id)) {
|
|
2452
3312
|
this.log({
|
|
2453
|
-
level: "
|
|
3313
|
+
level: "debug",
|
|
2454
3314
|
message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
|
|
2455
3315
|
conversation_id: row.conversation_id,
|
|
2456
3316
|
message_id: row.id
|
|
@@ -2462,7 +3322,7 @@ var ChannelDriver = class {
|
|
|
2462
3322
|
if (state === "done") {
|
|
2463
3323
|
if (this.doneUndeliverable.has(row.id)) {
|
|
2464
3324
|
this.log({
|
|
2465
|
-
level: "
|
|
3325
|
+
level: "debug",
|
|
2466
3326
|
message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
|
|
2467
3327
|
conversation_id: row.conversation_id,
|
|
2468
3328
|
message_id: row.id
|
|
@@ -2476,21 +3336,24 @@ var ChannelDriver = class {
|
|
|
2476
3336
|
message_id: row.id
|
|
2477
3337
|
});
|
|
2478
3338
|
try {
|
|
2479
|
-
await this.
|
|
3339
|
+
const title = await this.resolveSessionTitle(sessionId, row.conversation_id);
|
|
3340
|
+
const usage = messageUsage(messages, ocId ?? "");
|
|
3341
|
+
await this.markDone(row.conversation_id, row.id, sessionId, ocId, title, usage);
|
|
2480
3342
|
} catch (err) {
|
|
2481
3343
|
if (err instanceof ChannelAuthError) throw err;
|
|
2482
3344
|
if (err instanceof ChannelTerminalError) {
|
|
2483
3345
|
this.doneUndeliverable.add(row.id);
|
|
2484
3346
|
this.log({
|
|
2485
|
-
level: "
|
|
3347
|
+
level: "warn",
|
|
2486
3348
|
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}`,
|
|
2487
3349
|
conversation_id: row.conversation_id,
|
|
2488
3350
|
message_id: row.id
|
|
2489
3351
|
});
|
|
3352
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
|
|
2490
3353
|
return;
|
|
2491
3354
|
}
|
|
2492
3355
|
this.log({
|
|
2493
|
-
level: "
|
|
3356
|
+
level: "warn",
|
|
2494
3357
|
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2495
3358
|
conversation_id: row.conversation_id,
|
|
2496
3359
|
message_id: row.id
|
|
@@ -2498,10 +3361,12 @@ var ChannelDriver = class {
|
|
|
2498
3361
|
return;
|
|
2499
3362
|
}
|
|
2500
3363
|
this.dontRedispatch.delete(row.id);
|
|
3364
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_done");
|
|
2501
3365
|
return;
|
|
2502
3366
|
}
|
|
2503
3367
|
if (state === "failed") {
|
|
2504
3368
|
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
3369
|
+
const usage = messageUsage(messages, ocId ?? "");
|
|
2505
3370
|
this.log({
|
|
2506
3371
|
level: "error",
|
|
2507
3372
|
message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
@@ -2509,21 +3374,22 @@ var ChannelDriver = class {
|
|
|
2509
3374
|
message_id: row.id
|
|
2510
3375
|
});
|
|
2511
3376
|
try {
|
|
2512
|
-
await this.markFailed(row.conversation_id, row.id, sessionId, error2);
|
|
3377
|
+
await this.markFailed(row.conversation_id, row.id, sessionId, error2, usage);
|
|
2513
3378
|
} catch (err) {
|
|
2514
3379
|
if (err instanceof ChannelAuthError) throw err;
|
|
2515
3380
|
if (err instanceof ChannelTerminalError) {
|
|
2516
3381
|
this.doneUndeliverable.add(row.id);
|
|
2517
3382
|
this.log({
|
|
2518
|
-
level: "
|
|
3383
|
+
level: "warn",
|
|
2519
3384
|
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}`,
|
|
2520
3385
|
conversation_id: row.conversation_id,
|
|
2521
3386
|
message_id: row.id
|
|
2522
3387
|
});
|
|
3388
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
|
|
2523
3389
|
return;
|
|
2524
3390
|
}
|
|
2525
3391
|
this.log({
|
|
2526
|
-
level: "
|
|
3392
|
+
level: "warn",
|
|
2527
3393
|
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2528
3394
|
conversation_id: row.conversation_id,
|
|
2529
3395
|
message_id: row.id
|
|
@@ -2531,17 +3397,83 @@ var ChannelDriver = class {
|
|
|
2531
3397
|
return;
|
|
2532
3398
|
}
|
|
2533
3399
|
this.dontRedispatch.delete(row.id);
|
|
3400
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_failed");
|
|
2534
3401
|
return;
|
|
2535
3402
|
}
|
|
2536
3403
|
if (this.dontRedispatch.has(row.id)) {
|
|
2537
3404
|
this.log({
|
|
2538
|
-
level: "
|
|
3405
|
+
level: "debug",
|
|
2539
3406
|
message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
|
|
2540
3407
|
conversation_id: row.conversation_id,
|
|
2541
3408
|
message_id: row.id
|
|
2542
3409
|
});
|
|
2543
3410
|
return;
|
|
2544
3411
|
}
|
|
3412
|
+
let statusReadableOngoing = null;
|
|
3413
|
+
if (state === "running" && ocId) {
|
|
3414
|
+
const reply = findLastAssistantReplyFor(messages, ocId);
|
|
3415
|
+
const shape = this.replyCompletionShape(reply);
|
|
3416
|
+
const ongoing = sessionOngoing;
|
|
3417
|
+
statusReadableOngoing = ongoing;
|
|
3418
|
+
if (ongoing === false) {
|
|
3419
|
+
this.log({
|
|
3420
|
+
level: "info",
|
|
3421
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} running/${shape} but session ${sessionId.slice(0, 8)} is not-ongoing per GET /session/status (absent/idle) \u2014 re-dispatching from scratch (status-gated recovery)`,
|
|
3422
|
+
conversation_id: row.conversation_id,
|
|
3423
|
+
message_id: row.id
|
|
3424
|
+
});
|
|
3425
|
+
await this.forceReadoptRun(sessionId, row);
|
|
3426
|
+
return;
|
|
3427
|
+
}
|
|
3428
|
+
if (ongoing === true) {
|
|
3429
|
+
this.log({
|
|
3430
|
+
level: "debug",
|
|
3431
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} running/${shape} and session ${sessionId.slice(0, 8)} is ongoing per GET /session/status (busy/retry) \u2014 re-attaching watcher (no re-dispatch)`,
|
|
3432
|
+
conversation_id: row.conversation_id,
|
|
3433
|
+
message_id: row.id
|
|
3434
|
+
});
|
|
3435
|
+
} else {
|
|
3436
|
+
if (shape === "b1") {
|
|
3437
|
+
this.log({
|
|
3438
|
+
level: "debug",
|
|
3439
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} running/b1 but GET /session/status was unreadable (null) for session ${sessionId.slice(0, 8)} \u2014 NOT latching a b1 row on a transient status blip; leaving it un-tracked to re-evaluate on the next drain`,
|
|
3440
|
+
conversation_id: row.conversation_id,
|
|
3441
|
+
message_id: row.id
|
|
3442
|
+
});
|
|
3443
|
+
if (!this.readoptPollUnresolvedSignalled.has(row.id)) {
|
|
3444
|
+
this.readoptPollUnresolvedSignalled.add(row.id);
|
|
3445
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_poll_unresolved");
|
|
3446
|
+
}
|
|
3447
|
+
return;
|
|
3448
|
+
}
|
|
3449
|
+
this.log({
|
|
3450
|
+
level: "debug",
|
|
3451
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} running/${shape} but GET /session/status was unreadable (null) for session ${sessionId.slice(0, 8)} \u2014 falling back to the #253 preamble + descendant cross-check`,
|
|
3452
|
+
conversation_id: row.conversation_id,
|
|
3453
|
+
message_id: row.id
|
|
3454
|
+
});
|
|
3455
|
+
}
|
|
3456
|
+
}
|
|
3457
|
+
if (statusReadableOngoing === null && state === "running" && ocId && isPreamblePinnedRunning(messages, ocId)) {
|
|
3458
|
+
const descendantAlive = await this.isAnyDescendantSessionAlive(sessionId);
|
|
3459
|
+
if (descendantAlive === true) {
|
|
3460
|
+
this.log({
|
|
3461
|
+
level: "debug",
|
|
3462
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} preamble-pinned running (root ${sessionId.slice(0, 8)}) but a live descendant sub-agent session was found \u2014 treating as still running, re-attaching watcher (no re-dispatch)`,
|
|
3463
|
+
conversation_id: row.conversation_id,
|
|
3464
|
+
message_id: row.id
|
|
3465
|
+
});
|
|
3466
|
+
} else {
|
|
3467
|
+
this.log({
|
|
3468
|
+
level: "info",
|
|
3469
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} preamble-pinned on recovery (root ${sessionId.slice(0, 8)}), no live descendant runner \u2014 re-dispatching from scratch${descendantAlive === null ? " (descendant liveness indeterminate; a restart guarantees no live runner, so this does NOT block the re-dispatch)" : ""}`,
|
|
3470
|
+
conversation_id: row.conversation_id,
|
|
3471
|
+
message_id: row.id
|
|
3472
|
+
});
|
|
3473
|
+
await this.forceReadoptRun(sessionId, row);
|
|
3474
|
+
return;
|
|
3475
|
+
}
|
|
3476
|
+
}
|
|
2545
3477
|
if ((state === "running" || state === "queued") && ocId) {
|
|
2546
3478
|
const conv = this.convForRow(sessionId, row);
|
|
2547
3479
|
const message = this.queuedMessageForRow(row);
|
|
@@ -2550,11 +3482,12 @@ var ChannelDriver = class {
|
|
|
2550
3482
|
this.readopted.add(row.id);
|
|
2551
3483
|
this.ensureWatcherRunning(sessionId);
|
|
2552
3484
|
this.log({
|
|
2553
|
-
level: "
|
|
3485
|
+
level: "debug",
|
|
2554
3486
|
message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
|
|
2555
3487
|
conversation_id: row.conversation_id,
|
|
2556
3488
|
message_id: row.id
|
|
2557
3489
|
});
|
|
3490
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_reattached");
|
|
2558
3491
|
return;
|
|
2559
3492
|
}
|
|
2560
3493
|
await this.forceReadoptRun(sessionId, row);
|
|
@@ -2583,7 +3516,7 @@ var ChannelDriver = class {
|
|
|
2583
3516
|
async forceReadoptRun(sessionId, row) {
|
|
2584
3517
|
if (this.stopped) {
|
|
2585
3518
|
this.log({
|
|
2586
|
-
level: "
|
|
3519
|
+
level: "debug",
|
|
2587
3520
|
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`,
|
|
2588
3521
|
conversation_id: row.conversation_id,
|
|
2589
3522
|
message_id: row.id
|
|
@@ -2592,7 +3525,7 @@ var ChannelDriver = class {
|
|
|
2592
3525
|
}
|
|
2593
3526
|
if (this.awaitingReadopt.has(row.id)) {
|
|
2594
3527
|
this.log({
|
|
2595
|
-
level: "
|
|
3528
|
+
level: "debug",
|
|
2596
3529
|
message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
|
|
2597
3530
|
conversation_id: row.conversation_id,
|
|
2598
3531
|
message_id: row.id
|
|
@@ -2602,11 +3535,12 @@ var ChannelDriver = class {
|
|
|
2602
3535
|
if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
|
|
2603
3536
|
this.dontRedispatch.add(row.id);
|
|
2604
3537
|
this.log({
|
|
2605
|
-
level: "
|
|
3538
|
+
level: "debug",
|
|
2606
3539
|
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)`,
|
|
2607
3540
|
conversation_id: row.conversation_id,
|
|
2608
3541
|
message_id: row.id
|
|
2609
3542
|
});
|
|
3543
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_window_elapsed");
|
|
2610
3544
|
return;
|
|
2611
3545
|
}
|
|
2612
3546
|
const options = {
|
|
@@ -2620,40 +3554,44 @@ var ChannelDriver = class {
|
|
|
2620
3554
|
message_id: row.id
|
|
2621
3555
|
});
|
|
2622
3556
|
this.awaitingReadopt.add(row.id);
|
|
3557
|
+
const readoptConv = this.convForRow(sessionId, row);
|
|
3558
|
+
const readoptMessage = this.queuedMessageForRow(row);
|
|
3559
|
+
const sendAttachments = this.buildSendAttachments(readoptConv, readoptMessage);
|
|
2623
3560
|
let ocId;
|
|
2624
3561
|
try {
|
|
2625
3562
|
ocId = await this.dispatchLocked(
|
|
2626
3563
|
sessionId,
|
|
2627
|
-
() => sendPromptAsync(this.port, sessionId, row.content, options)
|
|
3564
|
+
() => sendPromptAsync(this.port, sessionId, row.content, options, sendAttachments)
|
|
2628
3565
|
);
|
|
2629
3566
|
} catch (err) {
|
|
2630
3567
|
this.awaitingReadopt.delete(row.id);
|
|
2631
3568
|
if (err instanceof ChannelAuthError) throw err;
|
|
2632
3569
|
this.log({
|
|
2633
|
-
level: "
|
|
3570
|
+
level: "warn",
|
|
2634
3571
|
message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2635
3572
|
conversation_id: row.conversation_id,
|
|
2636
3573
|
message_id: row.id
|
|
2637
3574
|
});
|
|
3575
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
|
|
2638
3576
|
return;
|
|
2639
3577
|
}
|
|
2640
3578
|
if (ocId === null) {
|
|
2641
3579
|
this.awaitingReadopt.delete(row.id);
|
|
2642
3580
|
this.log({
|
|
2643
|
-
level: "
|
|
3581
|
+
level: "warn",
|
|
2644
3582
|
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`,
|
|
2645
3583
|
conversation_id: row.conversation_id,
|
|
2646
3584
|
message_id: row.id
|
|
2647
3585
|
});
|
|
3586
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
|
|
2648
3587
|
return;
|
|
2649
3588
|
}
|
|
2650
|
-
|
|
2651
|
-
const message = this.queuedMessageForRow(row);
|
|
2652
|
-
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
3589
|
+
this.registerReadopted(readoptConv, sessionId, readoptMessage, ocId, this.processedAtMs(row));
|
|
2653
3590
|
this.dispatched.add(row.id);
|
|
2654
3591
|
this.readopted.add(row.id);
|
|
2655
3592
|
this.awaitingReadopt.delete(row.id);
|
|
2656
3593
|
this.ensureWatcherRunning(sessionId);
|
|
3594
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
|
|
2657
3595
|
}
|
|
2658
3596
|
/**
|
|
2659
3597
|
* True if `evidentMessageId` is already being driven — either in the
|
|
@@ -2702,7 +3640,8 @@ var ChannelDriver = class {
|
|
|
2702
3640
|
opencode_agent: row.opencode_agent,
|
|
2703
3641
|
opencode_model: row.opencode_model,
|
|
2704
3642
|
source_message_id: row.source_message_id,
|
|
2705
|
-
slack_user_id: row.slack_user_id
|
|
3643
|
+
slack_user_id: row.slack_user_id,
|
|
3644
|
+
attachments: row.attachments ?? null
|
|
2706
3645
|
};
|
|
2707
3646
|
}
|
|
2708
3647
|
/**
|
|
@@ -2723,7 +3662,7 @@ var ChannelDriver = class {
|
|
|
2723
3662
|
if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
|
|
2724
3663
|
this.dontRedispatch.add(evidentMessageId);
|
|
2725
3664
|
this.log({
|
|
2726
|
-
level: "
|
|
3665
|
+
level: "debug",
|
|
2727
3666
|
message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
|
|
2728
3667
|
conversation_id: watcher.conv.id,
|
|
2729
3668
|
message_id: evidentMessageId
|
|
@@ -2745,21 +3684,41 @@ var ChannelDriver = class {
|
|
|
2745
3684
|
* RUNNING (not done) is the one that paused. With one running message that is
|
|
2746
3685
|
* unambiguous; with several we prefer an explicit messageID match, else the
|
|
2747
3686
|
* oldest running message.
|
|
3687
|
+
*
|
|
3688
|
+
* Returns the set of in-flight Evident message ids that are paused awaiting a
|
|
3689
|
+
* human — an outstanding (still-open) question/permission is attributed to them.
|
|
3690
|
+
* `serviceInFlightMessage` uses this to keep an actively-running turn watched
|
|
3691
|
+
* forever (ADR-0047) while still bounding a turn merely blocked on a person who
|
|
3692
|
+
* may never answer. Attribution here covers ALL open interactions, not just
|
|
3693
|
+
* NEW (un-deduped) ones — a question stays "awaiting a human" until answered,
|
|
3694
|
+
* even after it was already surfaced to the channel.
|
|
2748
3695
|
*/
|
|
2749
3696
|
async pollInteractions(sessionId, watcher, messages) {
|
|
3697
|
+
const openQuestions = /* @__PURE__ */ new Set();
|
|
3698
|
+
const openPermissions = /* @__PURE__ */ new Set();
|
|
3699
|
+
let questionsPolledOk = true;
|
|
3700
|
+
let permissionsPolledOk = true;
|
|
2750
3701
|
let questions = [];
|
|
2751
3702
|
try {
|
|
2752
3703
|
const res = await this.fetchImpl(`${this.opencodeBase}/question`);
|
|
2753
3704
|
if (res.ok) {
|
|
2754
3705
|
const body = await res.json();
|
|
2755
|
-
|
|
3706
|
+
if (Array.isArray(body)) {
|
|
3707
|
+
questions = body;
|
|
3708
|
+
} else {
|
|
3709
|
+
questionsPolledOk = false;
|
|
3710
|
+
}
|
|
3711
|
+
} else {
|
|
3712
|
+
questionsPolledOk = false;
|
|
2756
3713
|
}
|
|
2757
3714
|
} catch {
|
|
3715
|
+
questionsPolledOk = false;
|
|
2758
3716
|
}
|
|
2759
3717
|
for (const q of questions) {
|
|
2760
|
-
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2761
3718
|
if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
|
|
2762
3719
|
const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
|
|
3720
|
+
if (paused) openQuestions.add(paused.evidentMessageId);
|
|
3721
|
+
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2763
3722
|
const reported = await this.reportInteraction(
|
|
2764
3723
|
watcher.conv.id,
|
|
2765
3724
|
"question",
|
|
@@ -2773,14 +3732,22 @@ var ChannelDriver = class {
|
|
|
2773
3732
|
const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
|
|
2774
3733
|
if (res.ok) {
|
|
2775
3734
|
const body = await res.json();
|
|
2776
|
-
|
|
3735
|
+
if (Array.isArray(body)) {
|
|
3736
|
+
permissions = body;
|
|
3737
|
+
} else {
|
|
3738
|
+
permissionsPolledOk = false;
|
|
3739
|
+
}
|
|
3740
|
+
} else {
|
|
3741
|
+
permissionsPolledOk = false;
|
|
2777
3742
|
}
|
|
2778
3743
|
} catch {
|
|
3744
|
+
permissionsPolledOk = false;
|
|
2779
3745
|
}
|
|
2780
3746
|
for (const p of permissions) {
|
|
2781
|
-
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2782
3747
|
if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
|
|
2783
3748
|
const paused = this.attributeInteraction(watcher, p.messageID, messages);
|
|
3749
|
+
if (paused) openPermissions.add(paused.evidentMessageId);
|
|
3750
|
+
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2784
3751
|
const reported = await this.reportInteraction(
|
|
2785
3752
|
watcher.conv.id,
|
|
2786
3753
|
"permission",
|
|
@@ -2789,6 +3756,7 @@ var ChannelDriver = class {
|
|
|
2789
3756
|
);
|
|
2790
3757
|
if (reported) watcher.reportedPermissions.add(p.id);
|
|
2791
3758
|
}
|
|
3759
|
+
return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
|
|
2792
3760
|
}
|
|
2793
3761
|
/**
|
|
2794
3762
|
* True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
|
|
@@ -2834,6 +3802,145 @@ var ChannelDriver = class {
|
|
|
2834
3802
|
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
2835
3803
|
return parent;
|
|
2836
3804
|
}
|
|
3805
|
+
/**
|
|
3806
|
+
* OpenCode's synchronous default session title (e.g.
|
|
3807
|
+
* `"New session - 1737800000000"`), assigned immediately when a session is
|
|
3808
|
+
* created — before OpenCode's async LLM-based auto-titling later renames it
|
|
3809
|
+
* mid-turn (#549). Matched by this literal, case-sensitive prefix only; the
|
|
3810
|
+
* timestamp suffix's exact format is deliberately NOT matched, since the prefix
|
|
3811
|
+
* alone is the stable, cheap signal and over-anchoring on the timestamp
|
|
3812
|
+
* representation risks silently breaking if OpenCode ever changes it. Accepted
|
|
3813
|
+
* trade-off: a genuine LLM-assigned title that happens to literally start with
|
|
3814
|
+
* this prefix would also fail to latch (see `resolveSessionTitle`) —
|
|
3815
|
+
* vanishingly unlikely in practice, and deliberately not engineered around.
|
|
3816
|
+
*/
|
|
3817
|
+
static OPENCODE_DEFAULT_TITLE_PREFIX = /^New session - /;
|
|
3818
|
+
/**
|
|
3819
|
+
* Resolve (and cache in `sessionTitles`) the OpenCode session TITLE (#310) so the
|
|
3820
|
+
* status PATCH can carry it into the "Live sessions" list. Driver-level cache so
|
|
3821
|
+
* BOTH the watcher completion path and the restart-recovery re-adopt path (which
|
|
3822
|
+
* has no watcher) can use it. `conversationId` is passed only for log context.
|
|
3823
|
+
* Best-effort:
|
|
3824
|
+
* - a resolved NON-EMPTY title that does NOT match
|
|
3825
|
+
* `OPENCODE_DEFAULT_TITLE_PREFIX` is cached and terminal (a real session name
|
|
3826
|
+
* won't later un-name), so we do NOT re-GET `/session/:id` every tick;
|
|
3827
|
+
* - while the title is still absent, empty, or matches the OpenCode
|
|
3828
|
+
* placeholder prefix (#549) we do NOT latch it — OpenCode names sessions
|
|
3829
|
+
* asynchronously mid-turn, so an early call (e.g. at `processing`) must leave
|
|
3830
|
+
* the cache unresolved and re-fetch on the next need so a later call (e.g. at
|
|
3831
|
+
* `done`) picks up the name assigned in the meantime. Such a call returns
|
|
3832
|
+
* `null` (omit the title on THIS PATCH) without caching. If a session is
|
|
3833
|
+
* never renamed, the title is omitted forever rather than ever persisting
|
|
3834
|
+
* the placeholder as a last resort;
|
|
3835
|
+
* - a failed request likewise leaves the cache unresolved (retry next need)
|
|
3836
|
+
* and returns `null` — it must NEVER throw or block completion.
|
|
3837
|
+
* A failure is logged with agent/session context (no silent catch).
|
|
3838
|
+
*/
|
|
3839
|
+
async resolveSessionTitle(sessionId, conversationId) {
|
|
3840
|
+
const cached = this.sessionTitles.get(sessionId);
|
|
3841
|
+
if (cached != null) return cached;
|
|
3842
|
+
try {
|
|
3843
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
|
|
3844
|
+
if (res.ok) {
|
|
3845
|
+
const body = await res.json();
|
|
3846
|
+
const title = body && typeof body.title === "string" ? body.title.trim() : "";
|
|
3847
|
+
if (title.length > 0 && !_ChannelDriver.OPENCODE_DEFAULT_TITLE_PREFIX.test(title)) {
|
|
3848
|
+
this.sessionTitles.set(sessionId, title);
|
|
3849
|
+
return title;
|
|
3850
|
+
}
|
|
3851
|
+
return null;
|
|
3852
|
+
}
|
|
3853
|
+
this.log({
|
|
3854
|
+
level: "debug",
|
|
3855
|
+
message: `Session title fetch for session ${sessionId.slice(0, 8)} (agent ${this.agentId.slice(0, 8)}) returned HTTP ${res.status} \u2014 omitting title`,
|
|
3856
|
+
conversation_id: conversationId
|
|
3857
|
+
});
|
|
3858
|
+
} catch (err) {
|
|
3859
|
+
this.log({
|
|
3860
|
+
level: "debug",
|
|
3861
|
+
message: `Best-effort session title fetch failed for session ${sessionId.slice(0, 8)} (agent ${this.agentId.slice(0, 8)}) \u2014 omitting title: ${err instanceof Error ? err.message : String(err)}`,
|
|
3862
|
+
conversation_id: conversationId
|
|
3863
|
+
});
|
|
3864
|
+
}
|
|
3865
|
+
return null;
|
|
3866
|
+
}
|
|
3867
|
+
/**
|
|
3868
|
+
* DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
|
|
3869
|
+
* (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
|
|
3870
|
+
*
|
|
3871
|
+
* The PRIMARY recovery trigger is "preamble-pinned on recovery ⇒ idle" — a
|
|
3872
|
+
* runner restart wipes OpenCode's in-memory `SessionStatus`/`Runner`, so a
|
|
3873
|
+
* completed `finish: "tool-calls"` root reply encountered during re-adoption is
|
|
3874
|
+
* idle by OpenCode's own definition and is re-dispatched. This method exists only
|
|
3875
|
+
* so the WI-3 caller can VETO that re-dispatch in the rare case a descendant is
|
|
3876
|
+
* provably in flight at the exact moment of recovery.
|
|
3877
|
+
*
|
|
3878
|
+
* "Alive" criterion (TIGHTENED): a descendant is alive only when it is PROVABLY,
|
|
3879
|
+
* ACTIVELY generating — its LAST message is an assistant still mid-generation
|
|
3880
|
+
* (`completed == null`, via `isSessionActivelyGenerating`). An
|
|
3881
|
+
* INCOMPLETE-BUT-NOT-GENERATING child — last message a user message, or a
|
|
3882
|
+
* completed `finish: "tool-calls"` step — is NOT alive after a restart (nothing
|
|
3883
|
+
* is generating once the runner is gone), so it does NOT veto. (This is
|
|
3884
|
+
* deliberately NOT `!isTurnComplete`, which also matches those dead-but-non-terminal
|
|
3885
|
+
* shapes and would falsely veto — re-hanging the very turn this path recovers.)
|
|
3886
|
+
*
|
|
3887
|
+
* Return contract (encoded so WI-3 need not re-derive it):
|
|
3888
|
+
* - `true` → a descendant is provably, actively generating (veto re-dispatch).
|
|
3889
|
+
* - `false` → descendants exist but none is actively generating (the restart
|
|
3890
|
+
* case), OR no descendant is found at all.
|
|
3891
|
+
* - `null` → liveness is INDETERMINATE (enumeration via `listSessions` failed).
|
|
3892
|
+
*
|
|
3893
|
+
* ⚠️ `null` (UNKNOWN) MUST NOT be treated as "alive": WI-3 treats `null` the same
|
|
3894
|
+
* as `false` and does NOT veto — a restart guarantees no live runner, so an
|
|
3895
|
+
* indeterminate cross-check almost always means "couldn't reach a child that no
|
|
3896
|
+
* longer exists". The inversion lives in the caller; this method just reports
|
|
3897
|
+
* true/false/null faithfully.
|
|
3898
|
+
*
|
|
3899
|
+
* VERIFY-BEFORE-DEPEND: we depend ONLY on (a) `parentID` from `GET /session/:id`
|
|
3900
|
+
* (already proven by the existing child-session interaction tests, via
|
|
3901
|
+
* `resolveSessionParent`/`sessionBelongsTo`) and (b) the child's own message-list
|
|
3902
|
+
* terminal state. We do NOT depend on any session-level `busy`/`idle` field —
|
|
3903
|
+
* there is none on `GET /session/:id`; OpenCode's busy state is in-memory
|
|
3904
|
+
* `SessionStatus` only.
|
|
3905
|
+
*/
|
|
3906
|
+
async isAnyDescendantSessionAlive(rootSessionId) {
|
|
3907
|
+
const sessions = await listSessions(this.port);
|
|
3908
|
+
if (!sessions) {
|
|
3909
|
+
this.log({
|
|
3910
|
+
level: "warn",
|
|
3911
|
+
message: `Re-adopt: could not enumerate sessions to cross-check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating child liveness as indeterminate`
|
|
3912
|
+
});
|
|
3913
|
+
return null;
|
|
3914
|
+
}
|
|
3915
|
+
for (const candidate of sessions) {
|
|
3916
|
+
if (!candidate?.id || candidate.id === rootSessionId) continue;
|
|
3917
|
+
if (!await this.sessionBelongsTo(candidate.id, rootSessionId)) continue;
|
|
3918
|
+
const childMsgs = await getSessionMessages(this.port, candidate.id);
|
|
3919
|
+
if (isSessionActivelyGenerating(childMsgs)) {
|
|
3920
|
+
return true;
|
|
3921
|
+
}
|
|
3922
|
+
}
|
|
3923
|
+
return false;
|
|
3924
|
+
}
|
|
3925
|
+
/**
|
|
3926
|
+
* Cheap decision-telemetry label for a running row's LAST correlated reply
|
|
3927
|
+
* (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
|
|
3928
|
+
* - `b1` — the reply itself is still in flight (`time.completed == null`) —
|
|
3929
|
+
* the aborted-in-flight production bug after a restart.
|
|
3930
|
+
* - `b2` — a COMPLETED reply pinned running only by `finish === "tool-calls"`
|
|
3931
|
+
* (the sub-agent preamble — #253's shape).
|
|
3932
|
+
* - `other` — any other shape (defensive; a running row is normally b1 or b2).
|
|
3933
|
+
* Reads `info.time.completed` / `info.finish` (tolerating the legacy top-level
|
|
3934
|
+
* shape) directly rather than re-importing the module-private `completedOf`/
|
|
3935
|
+
* `finishOf` — this is a display label only, not a correctness predicate.
|
|
3936
|
+
*/
|
|
3937
|
+
replyCompletionShape(reply) {
|
|
3938
|
+
if (!reply) return "other";
|
|
3939
|
+
const completed = reply.info?.time?.completed ?? reply.time?.completed;
|
|
3940
|
+
if (completed == null) return "b1";
|
|
3941
|
+
const finish = reply.info?.finish ?? reply.finish;
|
|
3942
|
+
return finish === "tool-calls" ? "b2" : "other";
|
|
3943
|
+
}
|
|
2837
3944
|
/**
|
|
2838
3945
|
* Attribute a surfaced interaction to the in-flight message it paused on (M-1).
|
|
2839
3946
|
*
|
|
@@ -2884,12 +3991,10 @@ var ChannelDriver = class {
|
|
|
2884
3991
|
}
|
|
2885
3992
|
return inFlight.sort(byOldest)[0];
|
|
2886
3993
|
}
|
|
2887
|
-
// -------------------------------------------------------------------------
|
|
2888
3994
|
// Evident API calls (combinedAuth thread routes)
|
|
2889
|
-
// -------------------------------------------------------------------------
|
|
2890
3995
|
async getPendingConversations() {
|
|
2891
3996
|
const res = await this.fetchImpl(
|
|
2892
|
-
`${this.apiUrl}/
|
|
3997
|
+
`${this.apiUrl}/runners/${this.agentId}/conversations/pending`,
|
|
2893
3998
|
{
|
|
2894
3999
|
headers: { Authorization: this.getAuthHeader() }
|
|
2895
4000
|
}
|
|
@@ -2907,7 +4012,7 @@ var ChannelDriver = class {
|
|
|
2907
4012
|
}
|
|
2908
4013
|
async getPendingMessages(conversationId) {
|
|
2909
4014
|
const res = await this.fetchImpl(
|
|
2910
|
-
`${this.apiUrl}/
|
|
4015
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages?status=pending`,
|
|
2911
4016
|
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2912
4017
|
);
|
|
2913
4018
|
this.assertAuth(res, "fetching pending messages");
|
|
@@ -2931,7 +4036,7 @@ var ChannelDriver = class {
|
|
|
2931
4036
|
*/
|
|
2932
4037
|
async getProcessingMessages() {
|
|
2933
4038
|
const res = await this.fetchImpl(
|
|
2934
|
-
`${this.apiUrl}/
|
|
4039
|
+
`${this.apiUrl}/runners/${this.agentId}/conversations/processing`,
|
|
2935
4040
|
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2936
4041
|
);
|
|
2937
4042
|
this.assertAuth(res, "fetching processing messages");
|
|
@@ -2945,6 +4050,32 @@ var ChannelDriver = class {
|
|
|
2945
4050
|
}
|
|
2946
4051
|
return messages;
|
|
2947
4052
|
}
|
|
4053
|
+
/**
|
|
4054
|
+
* The `opencode_session_id` fragment of a status PATCH body — `{}` when this
|
|
4055
|
+
* conversation has ABANDONED that session (#553). The field is optional
|
|
4056
|
+
* server-side and an absent one leaves the persisted binding untouched, so
|
|
4057
|
+
* omitting it is how a routine status write stops resurrecting it.
|
|
4058
|
+
*
|
|
4059
|
+
* ONLY for writes whose sole cost is a lost deep link. The `processing` notice
|
|
4060
|
+
* degrades to no "View in Evident" link (the reaction swap still fires) and the
|
|
4061
|
+
* turn-failure notice is built from the PATCH's own `error` text with a link off
|
|
4062
|
+
* the persisted row — neither loses content the user came for. `markDone`
|
|
4063
|
+
* deliberately does NOT use this helper: the server fetches the reply text
|
|
4064
|
+
* THROUGH the session id it is given, so suppressing there would replace the
|
|
4065
|
+
* agent's answer with a bare "✅ Done!" (the #183/#187 failure). The
|
|
4066
|
+
* `ensureSession` guard, not this suppression, is what makes the self-heal
|
|
4067
|
+
* stick.
|
|
4068
|
+
*/
|
|
4069
|
+
sessionIdBody(sessionId, conversationId, messageId, status) {
|
|
4070
|
+
if (!this.isSuperseded(conversationId, sessionId)) return { opencode_session_id: sessionId };
|
|
4071
|
+
this.log({
|
|
4072
|
+
level: "debug",
|
|
4073
|
+
message: `Omitting the abandoned OpenCode session ${sessionId.slice(0, 8)} from the '${status}' update for message ${messageId.slice(0, 8)} so it is not re-bound to conversation ${conversationId.slice(0, 8)}`,
|
|
4074
|
+
conversation_id: conversationId,
|
|
4075
|
+
message_id: messageId
|
|
4076
|
+
});
|
|
4077
|
+
return {};
|
|
4078
|
+
}
|
|
2948
4079
|
/**
|
|
2949
4080
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
2950
4081
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
@@ -2966,16 +4097,17 @@ var ChannelDriver = class {
|
|
|
2966
4097
|
* A single attempt (no internal retry): the watcher's per-tick loop is the
|
|
2967
4098
|
* retry vehicle for the swap-to-running.
|
|
2968
4099
|
*/
|
|
2969
|
-
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
4100
|
+
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId, title) {
|
|
2970
4101
|
const res = await this.fetchImpl(
|
|
2971
|
-
`${this.apiUrl}/
|
|
4102
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2972
4103
|
{
|
|
2973
4104
|
method: "PATCH",
|
|
2974
4105
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2975
4106
|
body: JSON.stringify({
|
|
2976
4107
|
status: "processing",
|
|
2977
|
-
|
|
2978
|
-
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
4108
|
+
...this.sessionIdBody(sessionId, conversationId, messageId, "processing"),
|
|
4109
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
4110
|
+
...title ? { title } : {}
|
|
2979
4111
|
})
|
|
2980
4112
|
}
|
|
2981
4113
|
);
|
|
@@ -3014,16 +4146,23 @@ var ChannelDriver = class {
|
|
|
3014
4146
|
* watcher retries next tick within the
|
|
3015
4147
|
* deadline, Finding 4).
|
|
3016
4148
|
*/
|
|
3017
|
-
async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
4149
|
+
async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage) {
|
|
3018
4150
|
const res = await this.fetchImpl(
|
|
3019
|
-
`${this.apiUrl}/
|
|
4151
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
3020
4152
|
{
|
|
3021
4153
|
method: "PATCH",
|
|
3022
4154
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3023
4155
|
body: JSON.stringify({
|
|
3024
4156
|
status: "done",
|
|
4157
|
+
// ALWAYS sent, even for a session this conversation has abandoned
|
|
4158
|
+
// (#553): the server reads the reply text back out of THIS session id
|
|
4159
|
+
// to deliver it. Omitting it would leave the user with "✅ Done!"
|
|
4160
|
+
// instead of the answer — a worse regression than the resurrection it
|
|
4161
|
+
// would prevent, which `ensureSession`'s guard handles anyway.
|
|
3025
4162
|
opencode_session_id: sessionId,
|
|
3026
|
-
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
4163
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
4164
|
+
...title ? { title } : {},
|
|
4165
|
+
...usage ? usage : {}
|
|
3027
4166
|
})
|
|
3028
4167
|
}
|
|
3029
4168
|
);
|
|
@@ -3036,19 +4175,29 @@ var ChannelDriver = class {
|
|
|
3036
4175
|
}
|
|
3037
4176
|
/**
|
|
3038
4177
|
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
3039
|
-
* when provided (issue #182)
|
|
3040
|
-
* `
|
|
3041
|
-
*
|
|
3042
|
-
*
|
|
4178
|
+
* when provided (issue #182). Three states for `sessionId`:
|
|
4179
|
+
* - omitted (`undefined`) → don't send the field, leave the persisted
|
|
4180
|
+
* session untouched (unused today; kept for API symmetry).
|
|
4181
|
+
* - a real id (`string`) → send it, update the persisted session (the
|
|
4182
|
+
* turn-failure call sites: an errored OpenCode turn).
|
|
4183
|
+
* - explicit `null` → send it, CLEAR the persisted session (issue
|
|
4184
|
+
* #485's dispatch-handoff-failure call site: the session id still
|
|
4185
|
+
* exists but is wedged, so the next attempt must get a fresh one
|
|
4186
|
+
* instead of reusing it — see WI-1's server-side null-clearing PATCH).
|
|
3043
4187
|
*/
|
|
3044
|
-
async markFailed(conversationId, messageId, sessionId, error2) {
|
|
4188
|
+
async markFailed(conversationId, messageId, sessionId, error2, usage) {
|
|
3045
4189
|
const body = { status: "failed" };
|
|
3046
|
-
if (sessionId
|
|
4190
|
+
if (sessionId === null) {
|
|
4191
|
+
body.opencode_session_id = null;
|
|
4192
|
+
} else if (sessionId !== void 0) {
|
|
4193
|
+
Object.assign(body, this.sessionIdBody(sessionId, conversationId, messageId, "failed"));
|
|
4194
|
+
}
|
|
3047
4195
|
if (error2 !== void 0) body.error = error2;
|
|
4196
|
+
if (usage) Object.assign(body, usage);
|
|
3048
4197
|
await this.callWithRetry(
|
|
3049
4198
|
"marking message as failed",
|
|
3050
4199
|
() => this.fetchImpl(
|
|
3051
|
-
`${this.apiUrl}/
|
|
4200
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
3052
4201
|
{
|
|
3053
4202
|
method: "PATCH",
|
|
3054
4203
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3065,11 +4214,17 @@ var ChannelDriver = class {
|
|
|
3065
4214
|
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
3066
4215
|
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
3067
4216
|
* context (no silent catch, per development-workflow).
|
|
4217
|
+
*
|
|
4218
|
+
* Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
|
|
4219
|
+
* telemetry), but the `paused` liveness-clear uses it to know whether to
|
|
4220
|
+
* RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
|
|
4221
|
+
* stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
|
|
4222
|
+
* leaves liveness").
|
|
3068
4223
|
*/
|
|
3069
4224
|
async postSignal(conversationId, messageId, signal, extra) {
|
|
3070
4225
|
try {
|
|
3071
4226
|
const res = await this.fetchImpl(
|
|
3072
|
-
`${this.apiUrl}/
|
|
4227
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
|
|
3073
4228
|
{
|
|
3074
4229
|
method: "POST",
|
|
3075
4230
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3078,24 +4233,27 @@ var ChannelDriver = class {
|
|
|
3078
4233
|
);
|
|
3079
4234
|
if (!res.ok) {
|
|
3080
4235
|
this.log({
|
|
3081
|
-
level: "
|
|
4236
|
+
level: "warn",
|
|
3082
4237
|
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
|
|
3083
4238
|
conversation_id: conversationId,
|
|
3084
4239
|
message_id: messageId
|
|
3085
4240
|
});
|
|
4241
|
+
return false;
|
|
3086
4242
|
}
|
|
4243
|
+
return true;
|
|
3087
4244
|
} catch (err) {
|
|
3088
4245
|
this.log({
|
|
3089
|
-
level: "
|
|
4246
|
+
level: "warn",
|
|
3090
4247
|
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
|
|
3091
4248
|
conversation_id: conversationId,
|
|
3092
4249
|
message_id: messageId
|
|
3093
4250
|
});
|
|
4251
|
+
return false;
|
|
3094
4252
|
}
|
|
3095
4253
|
}
|
|
3096
4254
|
async persistSession(conversationId, sessionId) {
|
|
3097
4255
|
const res = await this.fetchImpl(
|
|
3098
|
-
`${this.apiUrl}/
|
|
4256
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}`,
|
|
3099
4257
|
{
|
|
3100
4258
|
method: "PATCH",
|
|
3101
4259
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3121,7 +4279,7 @@ var ChannelDriver = class {
|
|
|
3121
4279
|
await this.callWithRetry(
|
|
3122
4280
|
"reporting interactive event",
|
|
3123
4281
|
() => this.fetchImpl(
|
|
3124
|
-
`${this.apiUrl}/
|
|
4282
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/interactive-event`,
|
|
3125
4283
|
{
|
|
3126
4284
|
method: "POST",
|
|
3127
4285
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3147,9 +4305,7 @@ var ChannelDriver = class {
|
|
|
3147
4305
|
return false;
|
|
3148
4306
|
}
|
|
3149
4307
|
}
|
|
3150
|
-
// -------------------------------------------------------------------------
|
|
3151
4308
|
// Retry wrapper
|
|
3152
|
-
// -------------------------------------------------------------------------
|
|
3153
4309
|
/**
|
|
3154
4310
|
* Invoke an Evident API call, retrying on transient failures (5xx / 429 /
|
|
3155
4311
|
* network errors) with exponential backoff + jitter (capped). Auth failures
|
|
@@ -3348,7 +4504,7 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
3348
4504
|
if (!response.ok) {
|
|
3349
4505
|
const serverMessage = await readErrorMessage(response);
|
|
3350
4506
|
return {
|
|
3351
|
-
error: `Failed to resolve
|
|
4507
|
+
error: `Failed to resolve runner from key (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
|
|
3352
4508
|
};
|
|
3353
4509
|
}
|
|
3354
4510
|
const data = await response.json();
|
|
@@ -3356,17 +4512,17 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
3356
4512
|
return { agent_id: data.agent_id };
|
|
3357
4513
|
}
|
|
3358
4514
|
return {
|
|
3359
|
-
error: "Cannot resolve
|
|
4515
|
+
error: "Cannot resolve runner ID: auth type is not agent_key. Please provide --agent explicitly."
|
|
3360
4516
|
};
|
|
3361
4517
|
} catch (error2) {
|
|
3362
4518
|
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
3363
|
-
return { error: `Failed to resolve
|
|
4519
|
+
return { error: `Failed to resolve runner from key: ${message}` };
|
|
3364
4520
|
}
|
|
3365
4521
|
}
|
|
3366
4522
|
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
3367
4523
|
const apiUrl = getApiUrlConfig();
|
|
3368
4524
|
try {
|
|
3369
|
-
const response = await fetch(`${apiUrl}/
|
|
4525
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}/disconnect`, {
|
|
3370
4526
|
method: "POST",
|
|
3371
4527
|
headers: { Authorization: authHeader }
|
|
3372
4528
|
});
|
|
@@ -3385,7 +4541,7 @@ async function notifyAgentDisconnected(agentId, authHeader) {
|
|
|
3385
4541
|
async function getAgentInfo(agentId, authHeader) {
|
|
3386
4542
|
const apiUrl = getApiUrlConfig();
|
|
3387
4543
|
try {
|
|
3388
|
-
const response = await fetch(`${apiUrl}/
|
|
4544
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}`, {
|
|
3389
4545
|
headers: { Authorization: authHeader }
|
|
3390
4546
|
});
|
|
3391
4547
|
if (response.status === 401) {
|
|
@@ -3396,12 +4552,12 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
3396
4552
|
const serverMessage = await readErrorMessage(response);
|
|
3397
4553
|
return {
|
|
3398
4554
|
valid: false,
|
|
3399
|
-
error: serverMessage ?? "You do not have access to this
|
|
4555
|
+
error: serverMessage ?? "You do not have access to this runner (it may belong to a different team or organization)."
|
|
3400
4556
|
};
|
|
3401
4557
|
}
|
|
3402
4558
|
if (response.status === 404) {
|
|
3403
4559
|
const serverMessage = await readErrorMessage(response);
|
|
3404
|
-
return { valid: false, error: serverMessage ?? `
|
|
4560
|
+
return { valid: false, error: serverMessage ?? `Runner ${agentId} not found` };
|
|
3405
4561
|
}
|
|
3406
4562
|
if (!response.ok) {
|
|
3407
4563
|
const serverMessage = await readErrorMessage(response);
|
|
@@ -3414,13 +4570,13 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
3414
4570
|
if (agent.agent_type !== "local") {
|
|
3415
4571
|
return {
|
|
3416
4572
|
valid: false,
|
|
3417
|
-
error: `
|
|
4573
|
+
error: `Runner is type '${agent.agent_type}', must be 'local' for CLI connection`
|
|
3418
4574
|
};
|
|
3419
4575
|
}
|
|
3420
4576
|
return { valid: true, agent };
|
|
3421
4577
|
} catch (error2) {
|
|
3422
4578
|
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
3423
|
-
return { valid: false, error: `Failed to validate
|
|
4579
|
+
return { valid: false, error: `Failed to validate runner: ${message}` };
|
|
3424
4580
|
}
|
|
3425
4581
|
}
|
|
3426
4582
|
|
|
@@ -3429,23 +4585,53 @@ var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
|
3429
4585
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
3430
4586
|
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
3431
4587
|
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
3432
|
-
function
|
|
4588
|
+
function resolveLogLevel(options) {
|
|
4589
|
+
const accepted = Object.keys(LOG_LEVELS);
|
|
4590
|
+
const validate = (value, source) => {
|
|
4591
|
+
const normalized = value.trim().toLowerCase();
|
|
4592
|
+
if (!accepted.includes(normalized)) {
|
|
4593
|
+
throw new Error(
|
|
4594
|
+
`Invalid log level "${value}"${source}; expected one of ${accepted.join(", ")}`
|
|
4595
|
+
);
|
|
4596
|
+
}
|
|
4597
|
+
return normalized;
|
|
4598
|
+
};
|
|
4599
|
+
if (options.logLevel !== void 0) {
|
|
4600
|
+
return validate(options.logLevel, " (--log-level)");
|
|
4601
|
+
}
|
|
4602
|
+
if (options.verbose) {
|
|
4603
|
+
return "debug";
|
|
4604
|
+
}
|
|
4605
|
+
const env = process.env.EVIDENT_LOG_LEVEL;
|
|
4606
|
+
if (env !== void 0 && env !== "") {
|
|
4607
|
+
return validate(env, " (EVIDENT_LOG_LEVEL)");
|
|
4608
|
+
}
|
|
4609
|
+
return "info";
|
|
4610
|
+
}
|
|
4611
|
+
function meetsThreshold(state, level) {
|
|
4612
|
+
return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
|
|
4613
|
+
}
|
|
4614
|
+
function log2(state, message, level = "info") {
|
|
4615
|
+
if (!meetsThreshold(state, level)) return;
|
|
3433
4616
|
if (state.json) {
|
|
3434
4617
|
console.log(
|
|
3435
4618
|
JSON.stringify({
|
|
3436
4619
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3437
|
-
level
|
|
4620
|
+
level,
|
|
3438
4621
|
message
|
|
3439
4622
|
})
|
|
3440
4623
|
);
|
|
3441
4624
|
} else if (!state.interactive) {
|
|
3442
|
-
const prefix =
|
|
4625
|
+
const prefix = level === "error" ? chalk6.red("\u2717") : level === "warn" ? chalk6.yellow("!") : level === "debug" ? chalk6.dim("\xB7") : chalk6.green("\u2022");
|
|
3443
4626
|
console.log(`${prefix} ${message}`);
|
|
3444
4627
|
}
|
|
3445
4628
|
}
|
|
3446
4629
|
function logActivity(state, entry) {
|
|
4630
|
+
const level = entry.level ?? (entry.type === "error" ? "error" : "info");
|
|
4631
|
+
if (!meetsThreshold(state, level)) return;
|
|
3447
4632
|
const fullEntry = {
|
|
3448
4633
|
...entry,
|
|
4634
|
+
level,
|
|
3449
4635
|
timestamp: /* @__PURE__ */ new Date()
|
|
3450
4636
|
};
|
|
3451
4637
|
state.activityLog.push(fullEntry);
|
|
@@ -3454,9 +4640,9 @@ function logActivity(state, entry) {
|
|
|
3454
4640
|
}
|
|
3455
4641
|
if (!state.interactive) {
|
|
3456
4642
|
if (entry.type === "error") {
|
|
3457
|
-
log2(state, entry.error ?? "Unknown error",
|
|
3458
|
-
} else if (entry.
|
|
3459
|
-
log2(state, entry.message);
|
|
4643
|
+
log2(state, entry.error ?? "Unknown error", level);
|
|
4644
|
+
} else if (entry.message) {
|
|
4645
|
+
log2(state, entry.message, level);
|
|
3460
4646
|
}
|
|
3461
4647
|
}
|
|
3462
4648
|
}
|
|
@@ -3583,7 +4769,7 @@ async function driveChannels(state, driver) {
|
|
|
3583
4769
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
|
|
3584
4770
|
if (state.interactive) displayStatus(state);
|
|
3585
4771
|
}
|
|
3586
|
-
await new Promise((
|
|
4772
|
+
await new Promise((resolve2) => setTimeout(resolve2, CHANNEL_POLL_INTERVAL_MS));
|
|
3587
4773
|
if (state.idleTimeout !== null && idlePolls >= 2) {
|
|
3588
4774
|
const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
|
|
3589
4775
|
if (idleMs > state.idleTimeout * 1e3) {
|
|
@@ -3594,6 +4780,81 @@ async function driveChannels(state, driver) {
|
|
|
3594
4780
|
}
|
|
3595
4781
|
}
|
|
3596
4782
|
}
|
|
4783
|
+
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
4784
|
+
async function runSweep(state, driver, config2) {
|
|
4785
|
+
const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
|
|
4786
|
+
try {
|
|
4787
|
+
const sessions = await listSessions(state.port);
|
|
4788
|
+
if (sessions === null) {
|
|
4789
|
+
logActivity(state, {
|
|
4790
|
+
type: "info",
|
|
4791
|
+
message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
|
|
4792
|
+
});
|
|
4793
|
+
return;
|
|
4794
|
+
}
|
|
4795
|
+
const toDelete = selectSessionsToDelete(
|
|
4796
|
+
sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
|
|
4797
|
+
{
|
|
4798
|
+
maxAgeMs: config2.maxAgeMs,
|
|
4799
|
+
maxCount: config2.maxCount,
|
|
4800
|
+
nowMs: Date.now(),
|
|
4801
|
+
protectedIds: driver.protectedSessionIds()
|
|
4802
|
+
}
|
|
4803
|
+
);
|
|
4804
|
+
const protectedNow = driver.protectedSessionIds();
|
|
4805
|
+
let deleted = 0;
|
|
4806
|
+
let failed = 0;
|
|
4807
|
+
let skippedNewlyActive = 0;
|
|
4808
|
+
for (const id of toDelete) {
|
|
4809
|
+
if (protectedNow.has(id)) {
|
|
4810
|
+
skippedNewlyActive++;
|
|
4811
|
+
logActivity(state, {
|
|
4812
|
+
type: "info",
|
|
4813
|
+
message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
|
|
4814
|
+
});
|
|
4815
|
+
continue;
|
|
4816
|
+
}
|
|
4817
|
+
if (await deleteSession(state.port, id)) deleted++;
|
|
4818
|
+
else failed++;
|
|
4819
|
+
}
|
|
4820
|
+
const failedNote = failed > 0 ? `, failed ${failed}` : "";
|
|
4821
|
+
const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
|
|
4822
|
+
logActivity(state, {
|
|
4823
|
+
type: "info",
|
|
4824
|
+
message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
|
|
4825
|
+
});
|
|
4826
|
+
} catch (error2) {
|
|
4827
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
4828
|
+
logActivity(state, {
|
|
4829
|
+
type: "error",
|
|
4830
|
+
error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
|
|
4831
|
+
});
|
|
4832
|
+
}
|
|
4833
|
+
}
|
|
4834
|
+
function scheduleSessionCleanup(state, driver, options) {
|
|
4835
|
+
const config2 = resolveSessionCleanupConfig(
|
|
4836
|
+
{
|
|
4837
|
+
maxAge: options.sessionCleanupMaxAge,
|
|
4838
|
+
maxCount: options.sessionCleanupMaxCount,
|
|
4839
|
+
interval: options.sessionCleanupInterval
|
|
4840
|
+
},
|
|
4841
|
+
process.env
|
|
4842
|
+
);
|
|
4843
|
+
for (const warning2 of config2.warnings) {
|
|
4844
|
+
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
4845
|
+
}
|
|
4846
|
+
if (!config2.enabled) return;
|
|
4847
|
+
logActivity(state, {
|
|
4848
|
+
type: "info",
|
|
4849
|
+
message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
|
|
4850
|
+
});
|
|
4851
|
+
const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
|
|
4852
|
+
const firstSweep = setTimeout(
|
|
4853
|
+
() => void runSweep(state, driver, config2),
|
|
4854
|
+
SESSION_CLEANUP_FIRST_SWEEP_MS
|
|
4855
|
+
);
|
|
4856
|
+
state.sessionCleanupTimers.push(interval, firstSweep);
|
|
4857
|
+
}
|
|
3597
4858
|
async function notifyOffline(state) {
|
|
3598
4859
|
if (!state.agentId || !state.authHeader) return;
|
|
3599
4860
|
if (!state.connected) {
|
|
@@ -3602,7 +4863,7 @@ async function notifyOffline(state) {
|
|
|
3602
4863
|
}
|
|
3603
4864
|
const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
|
|
3604
4865
|
if (result.ok) {
|
|
3605
|
-
log2(state, "Notified Evident the
|
|
4866
|
+
log2(state, "Notified Evident the runner is going offline");
|
|
3606
4867
|
} else {
|
|
3607
4868
|
logActivity(state, {
|
|
3608
4869
|
type: "error",
|
|
@@ -3613,6 +4874,11 @@ async function notifyOffline(state) {
|
|
|
3613
4874
|
}
|
|
3614
4875
|
async function cleanup(state, opts = {}) {
|
|
3615
4876
|
state.running = false;
|
|
4877
|
+
for (const timer of state.sessionCleanupTimers) {
|
|
4878
|
+
clearInterval(timer);
|
|
4879
|
+
clearTimeout(timer);
|
|
4880
|
+
}
|
|
4881
|
+
state.sessionCleanupTimers = [];
|
|
3616
4882
|
if (opts.graceful && state.channelDriver) {
|
|
3617
4883
|
state.channelDriver.stop();
|
|
3618
4884
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
@@ -3647,14 +4913,29 @@ async function cleanup(state, opts = {}) {
|
|
|
3647
4913
|
}
|
|
3648
4914
|
async function run(options) {
|
|
3649
4915
|
const interactive = isInteractive(options.json);
|
|
4916
|
+
let logLevel;
|
|
4917
|
+
try {
|
|
4918
|
+
logLevel = resolveLogLevel(options);
|
|
4919
|
+
} catch (error2) {
|
|
4920
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
4921
|
+
if (options.json) {
|
|
4922
|
+
console.log(JSON.stringify({ status: "error", error: message }));
|
|
4923
|
+
} else {
|
|
4924
|
+
printError(message);
|
|
4925
|
+
}
|
|
4926
|
+
await shutdownTelemetry();
|
|
4927
|
+
process.exit(1);
|
|
4928
|
+
return;
|
|
4929
|
+
}
|
|
3650
4930
|
const state = {
|
|
3651
|
-
agentId: options.agent || "",
|
|
4931
|
+
agentId: options.runner || options.agent || "",
|
|
3652
4932
|
agentName: null,
|
|
3653
4933
|
port: options.port ?? 4096,
|
|
3654
4934
|
conversationFilter: options.conversation ?? null,
|
|
3655
4935
|
idleTimeout: options.idleTimeout ?? null,
|
|
3656
4936
|
json: options.json ?? false,
|
|
3657
4937
|
interactive,
|
|
4938
|
+
logLevel,
|
|
3658
4939
|
connected: false,
|
|
3659
4940
|
opencodeConnected: false,
|
|
3660
4941
|
opencodeVersion: null,
|
|
@@ -3666,13 +4947,27 @@ async function run(options) {
|
|
|
3666
4947
|
activityLog: [],
|
|
3667
4948
|
messageCount: 0,
|
|
3668
4949
|
lastProxiedActivityAt: null,
|
|
4950
|
+
sessionCleanupTimers: [],
|
|
3669
4951
|
authHeader: ""
|
|
3670
4952
|
};
|
|
4953
|
+
if (!options.runner && options.agent) {
|
|
4954
|
+
telemetry.info(
|
|
4955
|
+
EventTypes.DEPRECATED_AGENT_FLAG_USED,
|
|
4956
|
+
"Deprecated --agent flag used instead of --runner",
|
|
4957
|
+
{ command: "run" },
|
|
4958
|
+
state.agentId
|
|
4959
|
+
);
|
|
4960
|
+
const agentFlagNotice = "--agent is deprecated, use --runner instead; will be removed in a future release.";
|
|
4961
|
+
log2(state, agentFlagNotice, "warn");
|
|
4962
|
+
if (state.interactive && !state.json) {
|
|
4963
|
+
logActivity(state, { type: "info", level: "warn", message: agentFlagNotice });
|
|
4964
|
+
}
|
|
4965
|
+
}
|
|
3671
4966
|
if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
|
|
3672
4967
|
log2(
|
|
3673
4968
|
state,
|
|
3674
|
-
"
|
|
3675
|
-
|
|
4969
|
+
"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.",
|
|
4970
|
+
"warn"
|
|
3676
4971
|
);
|
|
3677
4972
|
}
|
|
3678
4973
|
const handleSignal = async () => {
|
|
@@ -3696,7 +4991,9 @@ async function run(options) {
|
|
|
3696
4991
|
if (!interactive) {
|
|
3697
4992
|
printError("Authentication required");
|
|
3698
4993
|
blank();
|
|
3699
|
-
console.log(
|
|
4994
|
+
console.log(
|
|
4995
|
+
chalk6.dim("Set EVIDENT_RUNNER_KEY (or EVIDENT_AGENT_KEY) environment variable for CI")
|
|
4996
|
+
);
|
|
3700
4997
|
console.log(chalk6.dim("Or run `evident login` for interactive authentication"));
|
|
3701
4998
|
blank();
|
|
3702
4999
|
process.exit(1);
|
|
@@ -3710,26 +5007,51 @@ async function run(options) {
|
|
|
3710
5007
|
);
|
|
3711
5008
|
}
|
|
3712
5009
|
state.authHeader = getAuthHeader(credentials2);
|
|
5010
|
+
if (credentials2.notice) {
|
|
5011
|
+
log2(state, credentials2.notice, "warn");
|
|
5012
|
+
if (state.interactive && !state.json) {
|
|
5013
|
+
logActivity(state, { type: "info", level: "warn", message: credentials2.notice });
|
|
5014
|
+
}
|
|
5015
|
+
}
|
|
5016
|
+
if (credentials2.keySource === "agent_key") {
|
|
5017
|
+
telemetry.info(
|
|
5018
|
+
EventTypes.DEPRECATED_AGENT_KEY_ENV_USED,
|
|
5019
|
+
"Deprecated EVIDENT_AGENT_KEY env var used instead of EVIDENT_RUNNER_KEY",
|
|
5020
|
+
{ command: "run" },
|
|
5021
|
+
state.agentId
|
|
5022
|
+
);
|
|
5023
|
+
const agentKeyNotice = "EVIDENT_AGENT_KEY is deprecated, use EVIDENT_RUNNER_KEY instead; will be removed in a future release.";
|
|
5024
|
+
log2(state, agentKeyNotice, "warn");
|
|
5025
|
+
if (state.interactive && !state.json) {
|
|
5026
|
+
logActivity(state, { type: "info", level: "warn", message: agentKeyNotice });
|
|
5027
|
+
}
|
|
5028
|
+
}
|
|
3713
5029
|
if (!state.agentId) {
|
|
3714
5030
|
if (credentials2.authType === "agent_key") {
|
|
3715
5031
|
const resolved = await resolveAgentIdFromKey(state.authHeader);
|
|
3716
5032
|
if (resolved.agent_id) {
|
|
3717
5033
|
state.agentId = resolved.agent_id;
|
|
3718
|
-
log2(state, `Resolved
|
|
5034
|
+
log2(state, `Resolved runner ID from key: ${state.agentId}`);
|
|
3719
5035
|
if (state.interactive && !state.json) {
|
|
3720
5036
|
logActivity(state, {
|
|
3721
5037
|
type: "info",
|
|
3722
|
-
message: `
|
|
5038
|
+
message: `Runner ID resolved from key: ${state.agentId}`
|
|
3723
5039
|
});
|
|
3724
5040
|
}
|
|
3725
5041
|
} else {
|
|
3726
|
-
printError(resolved.error || "Failed to resolve
|
|
5042
|
+
printError(resolved.error || "Failed to resolve runner ID from key");
|
|
3727
5043
|
process.exit(1);
|
|
3728
5044
|
}
|
|
3729
5045
|
} else {
|
|
3730
|
-
printError(
|
|
5046
|
+
printError(
|
|
5047
|
+
"--runner (or --agent) is required when not using EVIDENT_RUNNER_KEY or EVIDENT_AGENT_KEY"
|
|
5048
|
+
);
|
|
3731
5049
|
blank();
|
|
3732
|
-
console.log(
|
|
5050
|
+
console.log(
|
|
5051
|
+
chalk6.dim(
|
|
5052
|
+
"Either provide --runner/--agent <id> or set EVIDENT_RUNNER_KEY/EVIDENT_AGENT_KEY"
|
|
5053
|
+
)
|
|
5054
|
+
);
|
|
3733
5055
|
blank();
|
|
3734
5056
|
process.exit(1);
|
|
3735
5057
|
}
|
|
@@ -3751,7 +5073,7 @@ async function run(options) {
|
|
|
3751
5073
|
console.log(chalk6.bold("Evident Run"));
|
|
3752
5074
|
console.log(chalk6.dim("-".repeat(40)));
|
|
3753
5075
|
}
|
|
3754
|
-
const spinner = interactive && !state.json ? ora3("Validating
|
|
5076
|
+
const spinner = interactive && !state.json ? ora3("Validating runner...").start() : null;
|
|
3755
5077
|
let validation = await getAgentInfo(state.agentId, state.authHeader);
|
|
3756
5078
|
if (!validation.valid && validation.authFailed && interactive) {
|
|
3757
5079
|
spinner?.fail("Authentication failed");
|
|
@@ -3763,14 +5085,14 @@ async function run(options) {
|
|
|
3763
5085
|
"Login successful! Retrying..."
|
|
3764
5086
|
);
|
|
3765
5087
|
state.authHeader = getAuthHeader(credentials2);
|
|
3766
|
-
spinner?.start("Validating
|
|
5088
|
+
spinner?.start("Validating runner...");
|
|
3767
5089
|
validation = await getAgentInfo(state.agentId, state.authHeader);
|
|
3768
5090
|
}
|
|
3769
5091
|
if (!validation.valid) {
|
|
3770
|
-
spinner?.fail(`
|
|
5092
|
+
spinner?.fail(`Runner validation failed: ${validation.error}`);
|
|
3771
5093
|
throw new Error(validation.error);
|
|
3772
5094
|
}
|
|
3773
|
-
spinner?.succeed(`
|
|
5095
|
+
spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
|
|
3774
5096
|
state.agentName = validation.agent.name;
|
|
3775
5097
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
3776
5098
|
try {
|
|
@@ -3788,9 +5110,24 @@ async function run(options) {
|
|
|
3788
5110
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
3789
5111
|
const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
|
|
3790
5112
|
if (versionWarning) {
|
|
3791
|
-
log2(state, versionWarning,
|
|
5113
|
+
log2(state, versionWarning, "warn");
|
|
5114
|
+
if (state.interactive && !state.json) {
|
|
5115
|
+
logActivity(state, { type: "info", level: "warn", message: versionWarning });
|
|
5116
|
+
}
|
|
5117
|
+
}
|
|
5118
|
+
const noProviderWarning = buildNoProviderWarning(await hasAnyConfiguredProvider(state.port));
|
|
5119
|
+
if (noProviderWarning) {
|
|
5120
|
+
log2(state, noProviderWarning, "warn");
|
|
3792
5121
|
if (state.interactive && !state.json) {
|
|
3793
|
-
logActivity(state, { type: "info", message:
|
|
5122
|
+
logActivity(state, { type: "info", level: "warn", message: noProviderWarning });
|
|
5123
|
+
blank();
|
|
5124
|
+
console.log(chalk6.yellow("\u26A0 No OpenCode model provider is configured."));
|
|
5125
|
+
console.log(
|
|
5126
|
+
chalk6.dim(
|
|
5127
|
+
`Run ${chalk6.cyan("opencode auth login")} to set one up \u2014 messages will fail until then.`
|
|
5128
|
+
)
|
|
5129
|
+
);
|
|
5130
|
+
blank();
|
|
3794
5131
|
}
|
|
3795
5132
|
}
|
|
3796
5133
|
} catch (error2) {
|
|
@@ -3805,11 +5142,17 @@ async function run(options) {
|
|
|
3805
5142
|
getAuthHeader: () => state.authHeader,
|
|
3806
5143
|
conversationFilter: state.conversationFilter,
|
|
3807
5144
|
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
3808
|
-
log: (entry) =>
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
5145
|
+
log: (entry) => (
|
|
5146
|
+
// Thread the driver's real level straight through so `debug`/`warn`
|
|
5147
|
+
// survive the sink filter (they no longer collapse to info). `type`
|
|
5148
|
+
// stays the coarse error/non-error split the activity log renders with.
|
|
5149
|
+
logActivity(state, {
|
|
5150
|
+
type: entry.level === "error" ? "error" : "info",
|
|
5151
|
+
level: entry.level,
|
|
5152
|
+
message: entry.message,
|
|
5153
|
+
error: entry.level === "error" ? entry.message : void 0
|
|
5154
|
+
})
|
|
5155
|
+
)
|
|
3813
5156
|
});
|
|
3814
5157
|
state.channelDriver = channelDriver;
|
|
3815
5158
|
const connection = new RunnerConnection({
|
|
@@ -3823,7 +5166,7 @@ async function run(options) {
|
|
|
3823
5166
|
state.agentId = agentId;
|
|
3824
5167
|
logActivity(state, {
|
|
3825
5168
|
type: "info",
|
|
3826
|
-
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (
|
|
5169
|
+
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (runner: ${agentId})`
|
|
3827
5170
|
});
|
|
3828
5171
|
emitAgentConnected(state.agentId, {
|
|
3829
5172
|
port: state.port,
|
|
@@ -3908,6 +5251,7 @@ async function run(options) {
|
|
|
3908
5251
|
if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
|
|
3909
5252
|
throw error2;
|
|
3910
5253
|
}
|
|
5254
|
+
scheduleSessionCleanup(state, channelDriver, options);
|
|
3911
5255
|
if (!interactive || state.json) {
|
|
3912
5256
|
log2(state, "Driving channel messages...");
|
|
3913
5257
|
}
|
|
@@ -3937,7 +5281,7 @@ async function run(options) {
|
|
|
3937
5281
|
}
|
|
3938
5282
|
telemetry.error(EventTypes.CLI_ERROR, `Run command failed: ${message}`, {
|
|
3939
5283
|
command: "run",
|
|
3940
|
-
agentId: options.agent
|
|
5284
|
+
agentId: options.runner || options.agent
|
|
3941
5285
|
});
|
|
3942
5286
|
await shutdownTelemetry();
|
|
3943
5287
|
process.exit(1);
|
|
@@ -3962,15 +5306,35 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
|
|
|
3962
5306
|
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);
|
|
3963
5307
|
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 }));
|
|
3964
5308
|
program.command("whoami").description("Show the currently logged in user").action(whoami);
|
|
3965
|
-
program.command("run").description("Connect to Evident and process messages").option("-a, --agent [id]", "
|
|
5309
|
+
program.command("run").description("Connect to Evident and process messages").option("-a, --agent [id]", "Runner ID to connect to (optional when EVIDENT_AGENT_KEY is set)").option("--runner [id]", "Alias for --agent (preferred name; wins if both are given)").option("-p, --port <port>", "OpenCode port (default: 4096)", "4096").option(
|
|
5310
|
+
"--log-level <level>",
|
|
5311
|
+
"Log verbosity: debug | info | warn | error (default: info). Env: EVIDENT_LOG_LEVEL"
|
|
5312
|
+
).option("-v, --verbose", "Alias for --log-level debug (ignored if --log-level is set)").option("-c, --conversation <id>", "Process only this specific conversation").option("--idle-timeout <seconds>", "Exit after N seconds idle").option("--json", "Output in JSON format").option(
|
|
5313
|
+
"--session-cleanup-max-age <duration>",
|
|
5314
|
+
"Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
|
|
5315
|
+
).option(
|
|
5316
|
+
"--session-cleanup-max-count <n>",
|
|
5317
|
+
"Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
|
|
5318
|
+
).option(
|
|
5319
|
+
"--session-cleanup-interval <duration>",
|
|
5320
|
+
"How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
|
|
5321
|
+
).action(
|
|
3966
5322
|
(options) => {
|
|
3967
5323
|
run({
|
|
3968
5324
|
agent: options.agent,
|
|
5325
|
+
runner: options.runner,
|
|
3969
5326
|
port: parseInt(options.port, 10),
|
|
5327
|
+
// Raw string — validation/precedence is single-sourced in run.ts's
|
|
5328
|
+
// resolveLogLevel (flag > -v > EVIDENT_LOG_LEVEL > info).
|
|
5329
|
+
logLevel: options.logLevel,
|
|
3970
5330
|
verbose: options.verbose,
|
|
3971
5331
|
conversation: options.conversation,
|
|
3972
5332
|
idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
|
|
3973
|
-
json: options.json
|
|
5333
|
+
json: options.json,
|
|
5334
|
+
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
5335
|
+
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
5336
|
+
sessionCleanupMaxCount: options.sessionCleanupMaxCount,
|
|
5337
|
+
sessionCleanupInterval: options.sessionCleanupInterval
|
|
3974
5338
|
});
|
|
3975
5339
|
}
|
|
3976
5340
|
);
|