@evident-ai/cli 3.0.1-dev.fffc02d → 3.1.0
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 +36 -24
- package/dist/index.js +2604 -315
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -11,8 +11,8 @@ import chalk2 from "chalk";
|
|
|
11
11
|
|
|
12
12
|
// src/lib/config.ts
|
|
13
13
|
import Conf from "conf";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
14
|
+
import { chmodSync, existsSync, statSync } from "fs";
|
|
15
|
+
import { dirname } from "path";
|
|
16
16
|
var PRODUCTION_API_URL = "https://api.production.evident.run/v1";
|
|
17
17
|
var PRODUCTION_TUNNEL_URL = "wss://tunnel.production.evident.run";
|
|
18
18
|
var defaults = {
|
|
@@ -47,8 +47,35 @@ var credentials = new Conf({
|
|
|
47
47
|
projectName: "evident",
|
|
48
48
|
projectSuffix: "",
|
|
49
49
|
configName: "credentials",
|
|
50
|
-
defaults: {}
|
|
50
|
+
defaults: {},
|
|
51
|
+
configFileMode: 384
|
|
51
52
|
});
|
|
53
|
+
var CREDENTIALS_FILE_MODE = 384;
|
|
54
|
+
var CREDENTIALS_DIR_MODE = 448;
|
|
55
|
+
var permissionWarningEmitted = false;
|
|
56
|
+
function hardenCredentialsPermissions() {
|
|
57
|
+
if (process.platform === "win32") {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const file = credentials.path;
|
|
61
|
+
for (const [path, mode] of [
|
|
62
|
+
[file, CREDENTIALS_FILE_MODE],
|
|
63
|
+
[dirname(file), CREDENTIALS_DIR_MODE]
|
|
64
|
+
]) {
|
|
65
|
+
try {
|
|
66
|
+
if (existsSync(path) && (statSync(path).mode & 511) !== mode) {
|
|
67
|
+
chmodSync(path, mode);
|
|
68
|
+
}
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (!permissionWarningEmitted) {
|
|
71
|
+
permissionWarningEmitted = true;
|
|
72
|
+
console.error(
|
|
73
|
+
`[config] could not restrict permissions on ${path}; the credentials file may be readable by other users on this machine: ${err instanceof Error ? err.message : String(err)}`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
52
79
|
function getApiUrlConfig() {
|
|
53
80
|
return getApiUrl();
|
|
54
81
|
}
|
|
@@ -59,6 +86,7 @@ function credentialsKey() {
|
|
|
59
86
|
return getApiUrl();
|
|
60
87
|
}
|
|
61
88
|
function getCredentials() {
|
|
89
|
+
hardenCredentialsPermissions();
|
|
62
90
|
const byEndpoint = credentials.get("byEndpoint") ?? {};
|
|
63
91
|
return byEndpoint[credentialsKey()] ?? {};
|
|
64
92
|
}
|
|
@@ -70,14 +98,17 @@ function setCredentials(creds) {
|
|
|
70
98
|
expiresAt: creds.expiresAt
|
|
71
99
|
};
|
|
72
100
|
credentials.set("byEndpoint", byEndpoint);
|
|
101
|
+
hardenCredentialsPermissions();
|
|
73
102
|
}
|
|
74
103
|
function clearCredentials() {
|
|
75
104
|
const byEndpoint = credentials.get("byEndpoint") ?? {};
|
|
76
105
|
delete byEndpoint[credentialsKey()];
|
|
77
106
|
credentials.set("byEndpoint", byEndpoint);
|
|
107
|
+
hardenCredentialsPermissions();
|
|
78
108
|
}
|
|
79
109
|
function clearAllCredentials() {
|
|
80
110
|
credentials.clear();
|
|
111
|
+
hardenCredentialsPermissions();
|
|
81
112
|
}
|
|
82
113
|
function getCliName() {
|
|
83
114
|
const argv1 = process.argv[1] || "";
|
|
@@ -285,14 +316,14 @@ function blank() {
|
|
|
285
316
|
console.log();
|
|
286
317
|
}
|
|
287
318
|
function waitForEnter(prompt = "Press Enter to continue...") {
|
|
288
|
-
return new Promise((
|
|
319
|
+
return new Promise((resolve3) => {
|
|
289
320
|
process.stdout.write(chalk.dim(prompt));
|
|
290
321
|
const handler = () => {
|
|
291
322
|
process.stdin.removeListener("data", handler);
|
|
292
323
|
process.stdin.setRawMode?.(false);
|
|
293
324
|
process.stdin.pause();
|
|
294
325
|
console.log();
|
|
295
|
-
|
|
326
|
+
resolve3();
|
|
296
327
|
};
|
|
297
328
|
if (process.stdin.isTTY) {
|
|
298
329
|
process.stdin.setRawMode?.(true);
|
|
@@ -302,7 +333,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
|
|
|
302
333
|
});
|
|
303
334
|
}
|
|
304
335
|
function sleep(ms) {
|
|
305
|
-
return new Promise((
|
|
336
|
+
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
306
337
|
}
|
|
307
338
|
|
|
308
339
|
// src/commands/login.ts
|
|
@@ -373,22 +404,23 @@ async function deviceFlowLogin(options) {
|
|
|
373
404
|
}
|
|
374
405
|
async function tokenLogin() {
|
|
375
406
|
console.log("Token login mode.");
|
|
376
|
-
console.log("
|
|
407
|
+
console.log("Run `evident login` on a machine with a browser to get a token.");
|
|
408
|
+
console.log("Manage or revoke existing tokens under Settings \u2192 CLI tokens.");
|
|
377
409
|
blank();
|
|
378
410
|
process.stdout.write("Paste token: ");
|
|
379
|
-
const token = await new Promise((
|
|
411
|
+
const token = await new Promise((resolve3) => {
|
|
380
412
|
let data = "";
|
|
381
413
|
process.stdin.setEncoding("utf8");
|
|
382
414
|
process.stdin.on("data", (chunk) => {
|
|
383
415
|
data += chunk;
|
|
384
416
|
});
|
|
385
417
|
process.stdin.on("end", () => {
|
|
386
|
-
|
|
418
|
+
resolve3(data.trim());
|
|
387
419
|
});
|
|
388
420
|
if (process.stdin.isTTY) {
|
|
389
421
|
process.stdin.once("data", (chunk) => {
|
|
390
422
|
process.stdin.pause();
|
|
391
|
-
|
|
423
|
+
resolve3(chunk.toString().trim());
|
|
392
424
|
});
|
|
393
425
|
process.stdin.resume();
|
|
394
426
|
}
|
|
@@ -467,9 +499,9 @@ async function whoami() {
|
|
|
467
499
|
}
|
|
468
500
|
|
|
469
501
|
// src/commands/run.ts
|
|
502
|
+
import { homedir as homedir2 } from "os";
|
|
503
|
+
import { isAbsolute as isAbsolute2, join as join2, parse, resolve as resolvePath } from "path";
|
|
470
504
|
import chalk6 from "chalk";
|
|
471
|
-
import ora3 from "ora";
|
|
472
|
-
import { select as select3 } from "@inquirer/prompts";
|
|
473
505
|
|
|
474
506
|
// ../../packages/types/src/telemetry/index.ts
|
|
475
507
|
var TelemetryEventTypes = {
|
|
@@ -485,6 +517,10 @@ var TelemetryEventTypes = {
|
|
|
485
517
|
var MAX_FRAME_BYTES = 256 * 1024;
|
|
486
518
|
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
487
519
|
|
|
520
|
+
// ../../packages/types/src/runner-files.ts
|
|
521
|
+
var MAX_FILE_PUSH_BYTES = 64 * 1024;
|
|
522
|
+
var MAX_FILE_SYNC_DIRECTORIES = 16;
|
|
523
|
+
|
|
488
524
|
// ../../packages/types/src/logging/index.ts
|
|
489
525
|
var CORRELATION_ID_HEADER = "x-evident-correlation-id";
|
|
490
526
|
function log(level, event, fields) {
|
|
@@ -499,6 +535,12 @@ function log(level, event, fields) {
|
|
|
499
535
|
);
|
|
500
536
|
}
|
|
501
537
|
}
|
|
538
|
+
function errorFields(err) {
|
|
539
|
+
if (err instanceof Error) {
|
|
540
|
+
return { error: err.message, error_name: err.name };
|
|
541
|
+
}
|
|
542
|
+
return { error: String(err) };
|
|
543
|
+
}
|
|
502
544
|
function stripQuery(url) {
|
|
503
545
|
try {
|
|
504
546
|
return new URL(url).pathname;
|
|
@@ -508,6 +550,10 @@ function stripQuery(url) {
|
|
|
508
550
|
}
|
|
509
551
|
}
|
|
510
552
|
|
|
553
|
+
// src/commands/run.ts
|
|
554
|
+
import ora3 from "ora";
|
|
555
|
+
import { select as select3 } from "@inquirer/prompts";
|
|
556
|
+
|
|
511
557
|
// src/lib/telemetry.ts
|
|
512
558
|
var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
513
559
|
function getCliVersion() {
|
|
@@ -645,14 +691,27 @@ var EventTypes = {
|
|
|
645
691
|
// CLI lifecycle
|
|
646
692
|
CLI_STARTED: "cli.started",
|
|
647
693
|
CLI_COMMAND: "cli.command",
|
|
648
|
-
CLI_ERROR: "cli.error"
|
|
694
|
+
CLI_ERROR: "cli.error",
|
|
695
|
+
// Deprecation telemetry (#412) — usage of the old `--agent`/`EVIDENT_AGENT_KEY`
|
|
696
|
+
// names instead of the preferred `--runner`/`EVIDENT_RUNNER_KEY` (#409).
|
|
697
|
+
DEPRECATED_AGENT_FLAG_USED: "cli.deprecated_agent_flag_used",
|
|
698
|
+
DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
|
|
649
699
|
};
|
|
650
700
|
|
|
651
701
|
// src/lib/auth.ts
|
|
652
702
|
async function getAuthCredentials() {
|
|
703
|
+
const runnerKey = process.env.EVIDENT_RUNNER_KEY;
|
|
653
704
|
const agentKey = process.env.EVIDENT_AGENT_KEY;
|
|
705
|
+
if (runnerKey) {
|
|
706
|
+
return {
|
|
707
|
+
token: runnerKey,
|
|
708
|
+
authType: "agent_key",
|
|
709
|
+
keySource: "runner_key",
|
|
710
|
+
notice: agentKey ? "Both EVIDENT_RUNNER_KEY and EVIDENT_AGENT_KEY are set; using EVIDENT_RUNNER_KEY." : void 0
|
|
711
|
+
};
|
|
712
|
+
}
|
|
654
713
|
if (agentKey) {
|
|
655
|
-
return { token: agentKey, authType: "agent_key" };
|
|
714
|
+
return { token: agentKey, authType: "agent_key", keySource: "agent_key" };
|
|
656
715
|
}
|
|
657
716
|
const userToken = process.env.EVIDENT_TOKEN;
|
|
658
717
|
if (userToken) {
|
|
@@ -706,7 +765,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
706
765
|
if (health.healthy) {
|
|
707
766
|
return health;
|
|
708
767
|
}
|
|
709
|
-
await new Promise((
|
|
768
|
+
await new Promise((resolve3) => setTimeout(resolve3, 1e3));
|
|
710
769
|
}
|
|
711
770
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
712
771
|
}
|
|
@@ -721,7 +780,7 @@ function buildOpenCodeVersionWarning(version2) {
|
|
|
721
780
|
if (isQueueValidatedVersion(version2)) return null;
|
|
722
781
|
const detected = version2 ? `v${version2}` : "unknown";
|
|
723
782
|
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
|
|
783
|
+
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
784
|
}
|
|
726
785
|
|
|
727
786
|
// src/lib/opencode/process.ts
|
|
@@ -1013,6 +1072,12 @@ async function promptOpenCodeInstall(interactive) {
|
|
|
1013
1072
|
return action;
|
|
1014
1073
|
}
|
|
1015
1074
|
|
|
1075
|
+
// src/lib/opencode/provider-check.ts
|
|
1076
|
+
function buildNoProviderWarning(hasProvider) {
|
|
1077
|
+
if (hasProvider !== false) return null;
|
|
1078
|
+
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).";
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1016
1081
|
// src/lib/opencode/session.ts
|
|
1017
1082
|
function opencodeBase(port) {
|
|
1018
1083
|
return `http://127.0.0.1:${port}`;
|
|
@@ -1078,6 +1143,84 @@ async function getSessionMessages(port, sessionId) {
|
|
|
1078
1143
|
return null;
|
|
1079
1144
|
}
|
|
1080
1145
|
}
|
|
1146
|
+
function isSessionActivelyGenerating(messages) {
|
|
1147
|
+
if (!messages || messages.length === 0) return false;
|
|
1148
|
+
const last = messages[messages.length - 1];
|
|
1149
|
+
if (roleOf(last) !== "assistant") return false;
|
|
1150
|
+
return completedOf(last) == null;
|
|
1151
|
+
}
|
|
1152
|
+
function sessionLastActivityMs(session) {
|
|
1153
|
+
const candidates = [
|
|
1154
|
+
session.time?.updated,
|
|
1155
|
+
session.time?.created,
|
|
1156
|
+
session.time_updated,
|
|
1157
|
+
session.time_created,
|
|
1158
|
+
session.updated,
|
|
1159
|
+
session.created
|
|
1160
|
+
];
|
|
1161
|
+
for (const c of candidates) {
|
|
1162
|
+
if (typeof c === "number" && Number.isFinite(c)) return c;
|
|
1163
|
+
}
|
|
1164
|
+
return null;
|
|
1165
|
+
}
|
|
1166
|
+
async function listSessions(port) {
|
|
1167
|
+
try {
|
|
1168
|
+
const res = await fetch(`${opencodeBase(port)}/session`);
|
|
1169
|
+
if (!res.ok) return null;
|
|
1170
|
+
const body = await res.json();
|
|
1171
|
+
return Array.isArray(body) ? body : null;
|
|
1172
|
+
} catch {
|
|
1173
|
+
return null;
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
async function deleteSession(port, id) {
|
|
1177
|
+
try {
|
|
1178
|
+
const res = await fetch(`${opencodeBase(port)}/session/${id}`, { method: "DELETE" });
|
|
1179
|
+
return res.status >= 200 && res.status < 300;
|
|
1180
|
+
} catch {
|
|
1181
|
+
return false;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
async function sessionExists(port, id) {
|
|
1185
|
+
try {
|
|
1186
|
+
const res = await fetch(`${opencodeBase(port)}/session/${id}`);
|
|
1187
|
+
if (res.status >= 200 && res.status < 300) return true;
|
|
1188
|
+
if (res.status === 404) return false;
|
|
1189
|
+
return null;
|
|
1190
|
+
} catch {
|
|
1191
|
+
return null;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
async function getSessionStatuses(port) {
|
|
1195
|
+
try {
|
|
1196
|
+
const res = await fetch(`${opencodeBase(port)}/session/status`);
|
|
1197
|
+
if (!res.ok) {
|
|
1198
|
+
console.error(
|
|
1199
|
+
`[getSessionStatuses] GET /session/status returned HTTP ${res.status} (port ${port})`
|
|
1200
|
+
);
|
|
1201
|
+
return null;
|
|
1202
|
+
}
|
|
1203
|
+
const body = await res.json();
|
|
1204
|
+
if (body == null || typeof body !== "object" || Array.isArray(body)) {
|
|
1205
|
+
console.error(
|
|
1206
|
+
`[getSessionStatuses] GET /session/status body was not a plain object (port ${port})`
|
|
1207
|
+
);
|
|
1208
|
+
return null;
|
|
1209
|
+
}
|
|
1210
|
+
return body;
|
|
1211
|
+
} catch (err) {
|
|
1212
|
+
console.error(
|
|
1213
|
+
`[getSessionStatuses] GET /session/status failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
1214
|
+
);
|
|
1215
|
+
return null;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
async function isSessionOngoing(port, id) {
|
|
1219
|
+
const map = await getSessionStatuses(port);
|
|
1220
|
+
if (map == null) return null;
|
|
1221
|
+
const entry = map[id];
|
|
1222
|
+
return entry != null && entry.type !== "idle";
|
|
1223
|
+
}
|
|
1081
1224
|
async function createOpenCodeSession(port, directory) {
|
|
1082
1225
|
const url = new URL(`${opencodeBase(port)}/session`);
|
|
1083
1226
|
if (directory && directory.trim()) {
|
|
@@ -1095,17 +1238,128 @@ async function createOpenCodeSession(port, directory) {
|
|
|
1095
1238
|
const data = await response.json();
|
|
1096
1239
|
return data.id;
|
|
1097
1240
|
}
|
|
1241
|
+
async function getModelAttachmentCapability(port, model) {
|
|
1242
|
+
try {
|
|
1243
|
+
const res = await fetch(`${opencodeBase(port)}/config/providers`);
|
|
1244
|
+
if (!res.ok) {
|
|
1245
|
+
console.error(
|
|
1246
|
+
`[getModelAttachmentCapability] GET /config/providers returned HTTP ${res.status} (port ${port})`
|
|
1247
|
+
);
|
|
1248
|
+
return null;
|
|
1249
|
+
}
|
|
1250
|
+
const body = await res.json();
|
|
1251
|
+
const providers = Array.isArray(body?.providers) ? body.providers : null;
|
|
1252
|
+
if (!providers) {
|
|
1253
|
+
console.error(
|
|
1254
|
+
`[getModelAttachmentCapability] GET /config/providers body had no providers array (port ${port})`
|
|
1255
|
+
);
|
|
1256
|
+
return null;
|
|
1257
|
+
}
|
|
1258
|
+
const slash = model ? model.indexOf("/") : -1;
|
|
1259
|
+
const providerId = slash > 0 ? model.slice(0, slash) : void 0;
|
|
1260
|
+
let modelId = slash > 0 ? model.slice(slash + 1) : void 0;
|
|
1261
|
+
const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
|
|
1262
|
+
let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
|
|
1263
|
+
if (!provider && !providerId) {
|
|
1264
|
+
const defaultProviderIds = defaults2 ? Object.keys(defaults2) : [];
|
|
1265
|
+
if (defaultProviderIds.length === 1) {
|
|
1266
|
+
provider = providers.find((p) => p?.id === defaultProviderIds[0]);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
if (!provider || !provider.models) return null;
|
|
1270
|
+
if (!modelId && defaults2 && typeof provider.id === "string") {
|
|
1271
|
+
const def = defaults2[provider.id];
|
|
1272
|
+
if (typeof def === "string") modelId = def;
|
|
1273
|
+
}
|
|
1274
|
+
if (!modelId) {
|
|
1275
|
+
if (providerId) {
|
|
1276
|
+
const keys = Object.keys(provider.models);
|
|
1277
|
+
if (keys.length === 1) modelId = keys[0];
|
|
1278
|
+
}
|
|
1279
|
+
if (!modelId) return null;
|
|
1280
|
+
}
|
|
1281
|
+
const entry = provider.models[modelId];
|
|
1282
|
+
if (!entry || typeof entry !== "object") return null;
|
|
1283
|
+
if (entry.capabilities && typeof entry.capabilities === "object") {
|
|
1284
|
+
if (typeof entry.capabilities.attachment === "boolean") {
|
|
1285
|
+
return entry.capabilities.attachment;
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
return typeof entry.attachment === "boolean" ? entry.attachment : null;
|
|
1289
|
+
} catch (err) {
|
|
1290
|
+
console.error(
|
|
1291
|
+
`[getModelAttachmentCapability] GET /config/providers failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
1292
|
+
);
|
|
1293
|
+
return null;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
async function buildFileParts(attachments, capable) {
|
|
1297
|
+
const outcomes = [];
|
|
1298
|
+
const parts = [];
|
|
1299
|
+
const capabilityUnknown = capable === null;
|
|
1300
|
+
if (capable !== true) {
|
|
1301
|
+
for (const a of attachments.inputs) {
|
|
1302
|
+
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "skipped" });
|
|
1303
|
+
}
|
|
1304
|
+
return { parts, outcomes, capabilityUnknown };
|
|
1305
|
+
}
|
|
1306
|
+
for (const a of attachments.inputs) {
|
|
1307
|
+
let dataUrl = null;
|
|
1308
|
+
try {
|
|
1309
|
+
dataUrl = await attachments.fetchDataUrl(a.index);
|
|
1310
|
+
} catch (err) {
|
|
1311
|
+
console.error(
|
|
1312
|
+
`[buildFileParts] attachment ${a.index} (${a.mime}) fetch threw \u2014 omitting: ${err instanceof Error ? err.message : String(err)}`
|
|
1313
|
+
);
|
|
1314
|
+
dataUrl = null;
|
|
1315
|
+
}
|
|
1316
|
+
if (dataUrl !== null && typeof dataUrl === "object") {
|
|
1317
|
+
outcomes.push({
|
|
1318
|
+
index: a.index,
|
|
1319
|
+
mime: a.mime,
|
|
1320
|
+
filename: a.filename,
|
|
1321
|
+
status: "failed",
|
|
1322
|
+
reason: "needs_reauth"
|
|
1323
|
+
});
|
|
1324
|
+
continue;
|
|
1325
|
+
}
|
|
1326
|
+
if (dataUrl == null) {
|
|
1327
|
+
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "failed" });
|
|
1328
|
+
continue;
|
|
1329
|
+
}
|
|
1330
|
+
parts.push({
|
|
1331
|
+
type: "file",
|
|
1332
|
+
mime: a.mime,
|
|
1333
|
+
url: dataUrl,
|
|
1334
|
+
...a.filename ? { filename: a.filename } : {}
|
|
1335
|
+
});
|
|
1336
|
+
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "sent" });
|
|
1337
|
+
}
|
|
1338
|
+
return { parts, outcomes, capabilityUnknown };
|
|
1339
|
+
}
|
|
1098
1340
|
function messageText(m) {
|
|
1099
1341
|
if (!m || !Array.isArray(m.parts)) return "";
|
|
1100
1342
|
return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
1101
1343
|
}
|
|
1102
|
-
async function sendPromptAsync(port, sessionId, content, options) {
|
|
1344
|
+
async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
1103
1345
|
const before = await getSessionMessages(port, sessionId);
|
|
1104
1346
|
const knownUserIds = new Set(
|
|
1105
1347
|
(before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
|
|
1106
1348
|
);
|
|
1349
|
+
const parts = [{ type: "text", text: content }];
|
|
1350
|
+
let pendingOutcomes = null;
|
|
1351
|
+
if (attachments && attachments.inputs.length > 0) {
|
|
1352
|
+
const capable = await getModelAttachmentCapability(port, options?.model);
|
|
1353
|
+
const {
|
|
1354
|
+
parts: fileParts,
|
|
1355
|
+
outcomes,
|
|
1356
|
+
capabilityUnknown
|
|
1357
|
+
} = await buildFileParts(attachments, capable);
|
|
1358
|
+
parts.push(...fileParts);
|
|
1359
|
+
if (attachments.onOutcomes) pendingOutcomes = { outcomes, capabilityUnknown };
|
|
1360
|
+
}
|
|
1107
1361
|
const body = {
|
|
1108
|
-
parts
|
|
1362
|
+
parts
|
|
1109
1363
|
};
|
|
1110
1364
|
if (options?.agent) {
|
|
1111
1365
|
body.agent = options.agent;
|
|
@@ -1144,10 +1398,13 @@ async function sendPromptAsync(port, sessionId, content, options) {
|
|
|
1144
1398
|
best = { id, created };
|
|
1145
1399
|
}
|
|
1146
1400
|
}
|
|
1147
|
-
if (best)
|
|
1401
|
+
if (best) {
|
|
1402
|
+
if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
|
|
1403
|
+
return best.id;
|
|
1404
|
+
}
|
|
1148
1405
|
}
|
|
1149
1406
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
1150
|
-
await new Promise((
|
|
1407
|
+
await new Promise((resolve3) => setTimeout(resolve3, READ_BACK_DELAY_MS));
|
|
1151
1408
|
}
|
|
1152
1409
|
}
|
|
1153
1410
|
return null;
|
|
@@ -1193,6 +1450,72 @@ function findLastAssistantReplyFor(messages, userMessageId) {
|
|
|
1193
1450
|
}
|
|
1194
1451
|
return lastOk ?? last;
|
|
1195
1452
|
}
|
|
1453
|
+
function messageUsage(messages, userMessageId) {
|
|
1454
|
+
if (!messages || messages.length === 0) return null;
|
|
1455
|
+
const byParentAll = messages.filter(
|
|
1456
|
+
(m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
|
|
1457
|
+
);
|
|
1458
|
+
const byParentNonErrored = byParentAll.filter((m) => errorOf(m) == null);
|
|
1459
|
+
const byParent = byParentNonErrored.length > 0 ? byParentNonErrored : byParentAll;
|
|
1460
|
+
let correlated;
|
|
1461
|
+
if (byParent.length > 0) {
|
|
1462
|
+
correlated = byParent;
|
|
1463
|
+
} else {
|
|
1464
|
+
const reply = findAssistantReplyAfter(messages, userMessageId);
|
|
1465
|
+
correlated = reply ? [reply] : [];
|
|
1466
|
+
}
|
|
1467
|
+
if (correlated.length === 0) return null;
|
|
1468
|
+
let sawAnyUsage = false;
|
|
1469
|
+
let inputSum = 0;
|
|
1470
|
+
let outputSum = 0;
|
|
1471
|
+
let reasoningSum = 0;
|
|
1472
|
+
let cacheReadSum = 0;
|
|
1473
|
+
let cacheWriteSum = 0;
|
|
1474
|
+
let costSum = 0;
|
|
1475
|
+
let sawCost = false;
|
|
1476
|
+
let modelId = null;
|
|
1477
|
+
let providerId = null;
|
|
1478
|
+
for (const m of correlated) {
|
|
1479
|
+
const info = m.info;
|
|
1480
|
+
if (!info) continue;
|
|
1481
|
+
const tokens = info.tokens;
|
|
1482
|
+
if (tokens) {
|
|
1483
|
+
sawAnyUsage = true;
|
|
1484
|
+
inputSum += tokens.input ?? 0;
|
|
1485
|
+
outputSum += tokens.output ?? 0;
|
|
1486
|
+
reasoningSum += tokens.reasoning ?? 0;
|
|
1487
|
+
cacheReadSum += tokens.cache?.read ?? 0;
|
|
1488
|
+
cacheWriteSum += tokens.cache?.write ?? 0;
|
|
1489
|
+
}
|
|
1490
|
+
if (typeof info.cost === "number") {
|
|
1491
|
+
sawAnyUsage = true;
|
|
1492
|
+
sawCost = true;
|
|
1493
|
+
costSum += info.cost;
|
|
1494
|
+
}
|
|
1495
|
+
if (typeof info.modelID === "string") {
|
|
1496
|
+
sawAnyUsage = true;
|
|
1497
|
+
modelId = info.modelID;
|
|
1498
|
+
}
|
|
1499
|
+
if (typeof info.providerID === "string") {
|
|
1500
|
+
sawAnyUsage = true;
|
|
1501
|
+
providerId = info.providerID;
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
if (!sawAnyUsage) return null;
|
|
1505
|
+
return {
|
|
1506
|
+
usage_provider_id: providerId,
|
|
1507
|
+
usage_model_id: modelId,
|
|
1508
|
+
usage_tokens_input: inputSum,
|
|
1509
|
+
usage_tokens_output: outputSum,
|
|
1510
|
+
usage_tokens_reasoning: reasoningSum,
|
|
1511
|
+
usage_tokens_cache_read: cacheReadSum,
|
|
1512
|
+
usage_tokens_cache_write: cacheWriteSum,
|
|
1513
|
+
// NULL means "OpenCode never reported a cost" (never inferred from
|
|
1514
|
+
// tokens) — distinct from a genuine 0-cost turn, which would set
|
|
1515
|
+
// `sawCost` true with `costSum === 0`.
|
|
1516
|
+
usage_cost_usd: sawCost ? costSum : null
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1196
1519
|
function messageRunState(messages, userMessageId) {
|
|
1197
1520
|
if (!messages || messages.length === 0) return "unknown";
|
|
1198
1521
|
const hasUser = messages.some((m) => idOf(m) === userMessageId);
|
|
@@ -1204,6 +1527,14 @@ function messageRunState(messages, userMessageId) {
|
|
|
1204
1527
|
if (isAssistantInFlight(reply)) return "running";
|
|
1205
1528
|
return errorOf(reply) != null ? "failed" : "done";
|
|
1206
1529
|
}
|
|
1530
|
+
function isPreamblePinnedRunning(messages, userMessageId) {
|
|
1531
|
+
if (messageRunState(messages, userMessageId) !== "running") return false;
|
|
1532
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1533
|
+
return completedOf(reply) != null && finishOf(reply) === "tool-calls";
|
|
1534
|
+
}
|
|
1535
|
+
function isB2AbandonmentConfirmed(params) {
|
|
1536
|
+
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
|
|
1537
|
+
}
|
|
1207
1538
|
function messageError(messages, userMessageId) {
|
|
1208
1539
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1209
1540
|
const error2 = errorOf(reply);
|
|
@@ -1223,6 +1554,141 @@ function hasRunningAssistantExcept(messages, exceptUserMessageId) {
|
|
|
1223
1554
|
(m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
|
|
1224
1555
|
);
|
|
1225
1556
|
}
|
|
1557
|
+
async function hasAnyConfiguredProvider(port) {
|
|
1558
|
+
try {
|
|
1559
|
+
const res = await fetch(`${opencodeBase(port)}/config/providers`);
|
|
1560
|
+
if (!res.ok) {
|
|
1561
|
+
console.error(
|
|
1562
|
+
`[hasAnyConfiguredProvider] GET /config/providers returned HTTP ${res.status} (port ${port})`
|
|
1563
|
+
);
|
|
1564
|
+
return null;
|
|
1565
|
+
}
|
|
1566
|
+
const body = await res.json();
|
|
1567
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
1568
|
+
console.error(
|
|
1569
|
+
`[hasAnyConfiguredProvider] GET /config/providers body was not a plain object (port ${port})`
|
|
1570
|
+
);
|
|
1571
|
+
return null;
|
|
1572
|
+
}
|
|
1573
|
+
const defaults2 = body.default;
|
|
1574
|
+
if (!defaults2 || typeof defaults2 !== "object" || Array.isArray(defaults2)) {
|
|
1575
|
+
console.error(
|
|
1576
|
+
`[hasAnyConfiguredProvider] GET /config/providers body had no \`default\` object (port ${port})`
|
|
1577
|
+
);
|
|
1578
|
+
return null;
|
|
1579
|
+
}
|
|
1580
|
+
return Object.keys(defaults2).length > 0;
|
|
1581
|
+
} catch (err) {
|
|
1582
|
+
console.error(
|
|
1583
|
+
`[hasAnyConfiguredProvider] GET /config/providers failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
1584
|
+
);
|
|
1585
|
+
return null;
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
// src/lib/opencode/session-cleanup.ts
|
|
1590
|
+
var DURATION_UNIT_MS = {
|
|
1591
|
+
s: 1e3,
|
|
1592
|
+
m: 60 * 1e3,
|
|
1593
|
+
h: 60 * 60 * 1e3,
|
|
1594
|
+
d: 24 * 60 * 60 * 1e3
|
|
1595
|
+
};
|
|
1596
|
+
function parseDurationMs(input) {
|
|
1597
|
+
const trimmed = input.trim();
|
|
1598
|
+
const match = /^(\d+)([smhd])$/.exec(trimmed);
|
|
1599
|
+
if (!match) {
|
|
1600
|
+
throw new Error(
|
|
1601
|
+
`Invalid duration "${input}": expected <number><unit> where unit is one of s, m, h, d (e.g. "7d", "24h", "30m", "90s").`
|
|
1602
|
+
);
|
|
1603
|
+
}
|
|
1604
|
+
const value = Number(match[1]);
|
|
1605
|
+
if (value <= 0) {
|
|
1606
|
+
throw new Error(`Invalid duration "${input}": must be a positive value.`);
|
|
1607
|
+
}
|
|
1608
|
+
return value * DURATION_UNIT_MS[match[2]];
|
|
1609
|
+
}
|
|
1610
|
+
function selectSessionsToDelete(sessions, opts) {
|
|
1611
|
+
const { maxAgeMs, maxCount, nowMs, protectedIds } = opts;
|
|
1612
|
+
if (maxAgeMs === void 0 && maxCount === void 0) return [];
|
|
1613
|
+
const ageEligible = (s) => {
|
|
1614
|
+
if (maxAgeMs === void 0) return false;
|
|
1615
|
+
if (s.lastActivityMs === null) return true;
|
|
1616
|
+
return nowMs - s.lastActivityMs > maxAgeMs;
|
|
1617
|
+
};
|
|
1618
|
+
const countEligibleIds = /* @__PURE__ */ new Set();
|
|
1619
|
+
if (maxCount !== void 0) {
|
|
1620
|
+
const byActivityDesc = [...sessions].sort(
|
|
1621
|
+
(a, b) => (b.lastActivityMs ?? -Infinity) - (a.lastActivityMs ?? -Infinity)
|
|
1622
|
+
);
|
|
1623
|
+
for (const s of byActivityDesc.slice(maxCount)) {
|
|
1624
|
+
countEligibleIds.add(s.id);
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
const toDelete = [];
|
|
1628
|
+
for (const s of sessions) {
|
|
1629
|
+
if (protectedIds.has(s.id)) continue;
|
|
1630
|
+
if (ageEligible(s) || countEligibleIds.has(s.id)) {
|
|
1631
|
+
toDelete.push(s.id);
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return toDelete;
|
|
1635
|
+
}
|
|
1636
|
+
var DEFAULT_INTERVAL = "1h";
|
|
1637
|
+
function resolve(flag, envValue, fallback) {
|
|
1638
|
+
return flag ?? envValue ?? fallback;
|
|
1639
|
+
}
|
|
1640
|
+
function parseMaxCount(input) {
|
|
1641
|
+
const trimmed = input.trim();
|
|
1642
|
+
if (!/^\d+$/.test(trimmed)) {
|
|
1643
|
+
throw new Error(`Invalid max-count "${input}": expected a positive integer.`);
|
|
1644
|
+
}
|
|
1645
|
+
const value = Number(trimmed);
|
|
1646
|
+
if (value <= 0) {
|
|
1647
|
+
throw new Error(`Invalid max-count "${input}": must be greater than 0.`);
|
|
1648
|
+
}
|
|
1649
|
+
return value;
|
|
1650
|
+
}
|
|
1651
|
+
function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
1652
|
+
const warnings = [];
|
|
1653
|
+
const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
|
|
1654
|
+
const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
|
|
1655
|
+
const intervalRaw = resolve(
|
|
1656
|
+
flags.interval,
|
|
1657
|
+
env.EVIDENT_SESSION_CLEANUP_INTERVAL,
|
|
1658
|
+
DEFAULT_INTERVAL
|
|
1659
|
+
);
|
|
1660
|
+
let maxAgeMs;
|
|
1661
|
+
if (maxAgeRaw !== void 0) {
|
|
1662
|
+
try {
|
|
1663
|
+
maxAgeMs = parseDurationMs(maxAgeRaw);
|
|
1664
|
+
} catch (err) {
|
|
1665
|
+
warnings.push(
|
|
1666
|
+
`Ignoring invalid --session-cleanup-max-age: ${err instanceof Error ? err.message : String(err)}`
|
|
1667
|
+
);
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
let maxCount;
|
|
1671
|
+
if (maxCountRaw !== void 0) {
|
|
1672
|
+
try {
|
|
1673
|
+
maxCount = parseMaxCount(maxCountRaw);
|
|
1674
|
+
} catch (err) {
|
|
1675
|
+
warnings.push(
|
|
1676
|
+
`Ignoring invalid --session-cleanup-max-count: ${err instanceof Error ? err.message : String(err)}`
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
let intervalMs;
|
|
1681
|
+
try {
|
|
1682
|
+
intervalMs = parseDurationMs(intervalRaw ?? DEFAULT_INTERVAL);
|
|
1683
|
+
} catch (err) {
|
|
1684
|
+
warnings.push(
|
|
1685
|
+
`Ignoring invalid --session-cleanup-interval, using default ${DEFAULT_INTERVAL}: ${err instanceof Error ? err.message : String(err)}`
|
|
1686
|
+
);
|
|
1687
|
+
intervalMs = parseDurationMs(DEFAULT_INTERVAL);
|
|
1688
|
+
}
|
|
1689
|
+
const enabled = maxAgeMs !== void 0 || maxCount !== void 0;
|
|
1690
|
+
return { enabled, maxAgeMs, maxCount, intervalMs, warnings };
|
|
1691
|
+
}
|
|
1226
1692
|
|
|
1227
1693
|
// src/lib/tunnel/connection.ts
|
|
1228
1694
|
import WebSocket2 from "ws";
|
|
@@ -1277,10 +1743,11 @@ var StreamForwarder = class {
|
|
|
1277
1743
|
* Abort every in-flight stream (e.g. on WebSocket close).
|
|
1278
1744
|
*/
|
|
1279
1745
|
abortAll() {
|
|
1280
|
-
for (const stream of this.inflight.
|
|
1746
|
+
for (const [sid, stream] of this.inflight.entries()) {
|
|
1281
1747
|
try {
|
|
1282
1748
|
stream.abort();
|
|
1283
|
-
} catch {
|
|
1749
|
+
} catch (err) {
|
|
1750
|
+
log("error", "forwarder_abort_failed", { sid, ...errorFields(err) });
|
|
1284
1751
|
}
|
|
1285
1752
|
}
|
|
1286
1753
|
this.inflight.clear();
|
|
@@ -1314,12 +1781,12 @@ var StreamForwarder = class {
|
|
|
1314
1781
|
let endBody;
|
|
1315
1782
|
if (has_body) {
|
|
1316
1783
|
const chunks = [];
|
|
1317
|
-
bodyPromise = new Promise((
|
|
1784
|
+
bodyPromise = new Promise((resolve3) => {
|
|
1318
1785
|
pushBody = (buf) => {
|
|
1319
1786
|
chunks.push(buf);
|
|
1320
1787
|
};
|
|
1321
1788
|
endBody = () => {
|
|
1322
|
-
|
|
1789
|
+
resolve3(Buffer.concat(chunks));
|
|
1323
1790
|
};
|
|
1324
1791
|
});
|
|
1325
1792
|
}
|
|
@@ -1430,31 +1897,20 @@ function connectTunnel(options) {
|
|
|
1430
1897
|
onConnected,
|
|
1431
1898
|
onDisconnected,
|
|
1432
1899
|
onError,
|
|
1433
|
-
onRequest,
|
|
1434
1900
|
onResponse,
|
|
1435
1901
|
onInfo,
|
|
1436
1902
|
onDrainPing
|
|
1437
1903
|
} = options;
|
|
1438
1904
|
const tunnelUrl = getTunnelUrlConfig();
|
|
1439
1905
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
1440
|
-
return new Promise((
|
|
1906
|
+
return new Promise((resolve3, reject) => {
|
|
1441
1907
|
const ws = new WebSocket2(url, {
|
|
1442
1908
|
headers: {
|
|
1443
1909
|
Authorization: authHeader
|
|
1444
1910
|
}
|
|
1445
1911
|
});
|
|
1446
|
-
const streamStartTimes = /* @__PURE__ */ new Map();
|
|
1447
1912
|
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
|
-
},
|
|
1913
|
+
onHead: () => onResponse?.(),
|
|
1458
1914
|
onDrainPing: () => onDrainPing?.()
|
|
1459
1915
|
});
|
|
1460
1916
|
const connectionTimeout = setTimeout(() => {
|
|
@@ -1502,7 +1958,7 @@ function connectTunnel(options) {
|
|
|
1502
1958
|
clearTimeout(connectionTimeout);
|
|
1503
1959
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
1504
1960
|
onConnected?.(connectedAgentId);
|
|
1505
|
-
|
|
1961
|
+
resolve3({
|
|
1506
1962
|
ws,
|
|
1507
1963
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
1508
1964
|
});
|
|
@@ -1530,7 +1986,6 @@ function connectTunnel(options) {
|
|
|
1530
1986
|
ws.on("close", (code, reason) => {
|
|
1531
1987
|
const reasonStr = reason.toString() || upgradeRejection || (code === 1006 ? "abnormal closure" : "No reason provided");
|
|
1532
1988
|
forwarder.abortAll();
|
|
1533
|
-
streamStartTimes.clear();
|
|
1534
1989
|
onDisconnected?.(code, reasonStr);
|
|
1535
1990
|
});
|
|
1536
1991
|
});
|
|
@@ -1565,7 +2020,11 @@ var RunnerConnection = class {
|
|
|
1565
2020
|
if (this.connection) {
|
|
1566
2021
|
try {
|
|
1567
2022
|
this.connection.close();
|
|
1568
|
-
} catch {
|
|
2023
|
+
} catch (err) {
|
|
2024
|
+
log("error", "runner_connection_close_failed", {
|
|
2025
|
+
agent_id: this.resolvedAgentId,
|
|
2026
|
+
...errorFields(err)
|
|
2027
|
+
});
|
|
1569
2028
|
}
|
|
1570
2029
|
this.connection = null;
|
|
1571
2030
|
}
|
|
@@ -1618,72 +2077,517 @@ var RunnerConnection = class {
|
|
|
1618
2077
|
};
|
|
1619
2078
|
|
|
1620
2079
|
// src/lib/channels/driver.ts
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
2080
|
+
import { homedir } from "os";
|
|
2081
|
+
|
|
2082
|
+
// src/lib/file-push.ts
|
|
2083
|
+
import { randomUUID } from "crypto";
|
|
2084
|
+
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
2085
|
+
import { basename, dirname as dirname2, isAbsolute, join, relative, resolve as resolve2, sep } from "path";
|
|
2086
|
+
var FILE_MODE = 384;
|
|
2087
|
+
var DIRECTORY_MODE = 448;
|
|
2088
|
+
async function writePushedFile(request) {
|
|
2089
|
+
const { requestedPath, content, allowedDirectories, homeDir } = request;
|
|
2090
|
+
const bytes = content.byteLength;
|
|
2091
|
+
if (allowedDirectories.length === 0) {
|
|
2092
|
+
return refuse("file_sync_disabled", "File sync is not enabled on this runner.", {
|
|
2093
|
+
path: requestedPath,
|
|
2094
|
+
bytes
|
|
2095
|
+
});
|
|
2096
|
+
}
|
|
2097
|
+
if (bytes > MAX_FILE_PUSH_BYTES) {
|
|
2098
|
+
return refuse(
|
|
2099
|
+
"file_too_large",
|
|
2100
|
+
`File is ${bytes} bytes; the limit is ${MAX_FILE_PUSH_BYTES}.`,
|
|
2101
|
+
{
|
|
2102
|
+
path: requestedPath,
|
|
2103
|
+
bytes
|
|
2104
|
+
}
|
|
2105
|
+
);
|
|
2106
|
+
}
|
|
2107
|
+
const candidate = expandAndValidate(requestedPath, homeDir);
|
|
2108
|
+
if (candidate === null) {
|
|
2109
|
+
return refuse("invalid_path", "The requested path is not a valid absolute file path.", {
|
|
2110
|
+
path: requestedPath,
|
|
2111
|
+
bytes
|
|
2112
|
+
});
|
|
2113
|
+
}
|
|
2114
|
+
try {
|
|
2115
|
+
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
2116
|
+
dirname2(candidate)
|
|
2117
|
+
);
|
|
2118
|
+
const realTarget = join(existingAncestor, ...missingSegments, basename(candidate));
|
|
2119
|
+
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
2120
|
+
if (allowedDirectory === null) {
|
|
2121
|
+
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
2122
|
+
path: realTarget,
|
|
2123
|
+
bytes
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
if (missingSegments.length > 0) {
|
|
2127
|
+
await createMissingDirectories(existingAncestor, missingSegments);
|
|
2128
|
+
const realParent = await realpath(dirname2(realTarget));
|
|
2129
|
+
if (realParent !== dirname2(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
2130
|
+
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
2131
|
+
path: realTarget,
|
|
2132
|
+
bytes,
|
|
2133
|
+
reason: "parent_changed_after_create"
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
await writeAtomically(realTarget, content);
|
|
2138
|
+
log("info", "file_push_written", { path: realTarget, bytes });
|
|
2139
|
+
return { ok: true, path: realTarget };
|
|
2140
|
+
} catch (err) {
|
|
2141
|
+
const errno = err.code ?? "UNKNOWN";
|
|
2142
|
+
return refuse("write_failed", `The runner could not write the file (${errno}).`, {
|
|
2143
|
+
path: candidate,
|
|
2144
|
+
bytes,
|
|
2145
|
+
errno,
|
|
2146
|
+
...errorFields(err)
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
1626
2149
|
}
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
maxDelayMs: 3e4
|
|
1631
|
-
};
|
|
1632
|
-
var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
|
|
1633
|
-
var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
1634
|
-
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
1635
|
-
var ChannelAuthError = class extends Error {
|
|
1636
|
-
constructor(message) {
|
|
1637
|
-
super(message);
|
|
1638
|
-
this.name = "ChannelAuthError";
|
|
2150
|
+
function expandAndValidate(requestedPath, homeDir) {
|
|
2151
|
+
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
2152
|
+
return null;
|
|
1639
2153
|
}
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
constructor(message, status) {
|
|
1644
|
-
super(message);
|
|
1645
|
-
this.name = "ChannelTerminalError";
|
|
1646
|
-
this.status = status;
|
|
2154
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
2155
|
+
if (expanded.split(/[/\\]/).includes("..")) {
|
|
2156
|
+
return null;
|
|
1647
2157
|
}
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
const
|
|
1652
|
-
|
|
2158
|
+
if (!isAbsolute(expanded)) {
|
|
2159
|
+
return null;
|
|
2160
|
+
}
|
|
2161
|
+
const candidate = resolve2(expanded);
|
|
2162
|
+
const name = basename(candidate);
|
|
2163
|
+
return name === "" || name === "." || name === ".." ? null : candidate;
|
|
1653
2164
|
}
|
|
1654
|
-
function
|
|
1655
|
-
|
|
2165
|
+
async function resolveNearestExistingAncestor(directory) {
|
|
2166
|
+
const missingSegments = [];
|
|
2167
|
+
let current = directory;
|
|
2168
|
+
for (; ; ) {
|
|
2169
|
+
try {
|
|
2170
|
+
return { existingAncestor: await realpath(current), missingSegments };
|
|
2171
|
+
} catch (err) {
|
|
2172
|
+
const parent = dirname2(current);
|
|
2173
|
+
if (err.code !== "ENOENT" || parent === current) {
|
|
2174
|
+
throw err;
|
|
2175
|
+
}
|
|
2176
|
+
missingSegments.unshift(basename(current));
|
|
2177
|
+
current = parent;
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
1656
2180
|
}
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
2181
|
+
async function findContainingAllowedDirectory(allowedDirectories, realTarget) {
|
|
2182
|
+
for (const directory of allowedDirectories) {
|
|
2183
|
+
if (!isAbsolute(directory)) {
|
|
2184
|
+
log("warn", "file_push_allowed_directory_skipped", { directory, reason: "not_absolute" });
|
|
2185
|
+
continue;
|
|
2186
|
+
}
|
|
2187
|
+
const realDirectory = await realpathCreatingIfMissing(directory);
|
|
2188
|
+
if (realDirectory !== null && contains(realDirectory, realTarget)) {
|
|
2189
|
+
return realDirectory;
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
return null;
|
|
2193
|
+
}
|
|
2194
|
+
async function realpathCreatingIfMissing(directory) {
|
|
2195
|
+
try {
|
|
2196
|
+
return await realpath(directory);
|
|
2197
|
+
} catch (err) {
|
|
2198
|
+
if (err.code !== "ENOENT") {
|
|
2199
|
+
log("warn", "file_push_allowed_directory_skipped", {
|
|
2200
|
+
directory,
|
|
2201
|
+
reason: "unresolvable",
|
|
2202
|
+
...errorFields(err)
|
|
2203
|
+
});
|
|
2204
|
+
return null;
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
try {
|
|
2208
|
+
await mkdir(directory, { recursive: true, mode: DIRECTORY_MODE });
|
|
2209
|
+
await chmod(directory, DIRECTORY_MODE);
|
|
2210
|
+
return await realpath(directory);
|
|
2211
|
+
} catch (err) {
|
|
2212
|
+
log("warn", "file_push_allowed_directory_skipped", {
|
|
2213
|
+
directory,
|
|
2214
|
+
reason: "create_failed",
|
|
2215
|
+
...errorFields(err)
|
|
2216
|
+
});
|
|
2217
|
+
return null;
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
function contains(realDirectory, realTarget) {
|
|
2221
|
+
const rel = relative(realDirectory, realTarget);
|
|
2222
|
+
return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
2223
|
+
}
|
|
2224
|
+
async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
2225
|
+
let current = existingAncestor;
|
|
2226
|
+
for (const segment of missingSegments) {
|
|
2227
|
+
current = join(current, segment);
|
|
2228
|
+
await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
|
|
2229
|
+
await chmod(current, DIRECTORY_MODE);
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
async function writeAtomically(realTarget, content) {
|
|
2233
|
+
const temporaryPath = join(dirname2(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
2234
|
+
let handle;
|
|
2235
|
+
try {
|
|
2236
|
+
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
2237
|
+
await handle.writeFile(content);
|
|
2238
|
+
await handle.chmod(FILE_MODE);
|
|
2239
|
+
await handle.close();
|
|
2240
|
+
handle = void 0;
|
|
2241
|
+
await rename(temporaryPath, realTarget);
|
|
2242
|
+
} catch (err) {
|
|
2243
|
+
await discardTemporaryFile(temporaryPath, handle);
|
|
2244
|
+
throw err;
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
async function discardTemporaryFile(temporaryPath, handle) {
|
|
2248
|
+
try {
|
|
2249
|
+
await handle?.close();
|
|
2250
|
+
} catch (err) {
|
|
2251
|
+
log("warn", "file_push_temp_close_failed", { path: temporaryPath, ...errorFields(err) });
|
|
2252
|
+
}
|
|
2253
|
+
try {
|
|
2254
|
+
await unlink(temporaryPath);
|
|
2255
|
+
} catch (err) {
|
|
2256
|
+
const errno = err.code;
|
|
2257
|
+
if (errno !== "ENOENT" && errno !== "ENOTDIR") {
|
|
2258
|
+
log("warn", "file_push_temp_cleanup_failed", { path: temporaryPath, ...errorFields(err) });
|
|
2259
|
+
}
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
function refuse(code, message, fields) {
|
|
2263
|
+
log(code === "write_failed" ? "error" : "warn", "file_push_refused", { code, ...fields });
|
|
2264
|
+
return { ok: false, code, message };
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
// src/lib/runner-file-sync.ts
|
|
2268
|
+
var MAX_ACK_ATTEMPTS = 5;
|
|
2269
|
+
async function syncPendingRunnerFiles(options) {
|
|
2270
|
+
const pending = await listPendingFiles(options);
|
|
2271
|
+
const pendingIds = new Set(pending.map((file) => file.id));
|
|
2272
|
+
for (const id of options.ackFailures.keys()) {
|
|
2273
|
+
if (!pendingIds.has(id)) options.ackFailures.delete(id);
|
|
2274
|
+
}
|
|
2275
|
+
if (pending.length === 0) return 0;
|
|
2276
|
+
options.log({
|
|
2277
|
+
level: "info",
|
|
2278
|
+
message: `Runner file sync: ${pending.length} file(s) queued for this runner`
|
|
2279
|
+
});
|
|
2280
|
+
let applied = 0;
|
|
2281
|
+
for (const file of pending) {
|
|
2282
|
+
if ((options.ackFailures.get(file.id) ?? 0) >= MAX_ACK_ATTEMPTS) continue;
|
|
2283
|
+
if (await applyOne(options, file)) applied += 1;
|
|
2284
|
+
}
|
|
2285
|
+
return applied;
|
|
2286
|
+
}
|
|
2287
|
+
async function listPendingFiles(options) {
|
|
2288
|
+
let res;
|
|
2289
|
+
try {
|
|
2290
|
+
res = await options.fetchImpl(`${options.apiUrl}/runners/${options.agentId}/files/pending`, {
|
|
2291
|
+
headers: { Authorization: options.getAuthHeader() }
|
|
2292
|
+
});
|
|
2293
|
+
} catch (err) {
|
|
2294
|
+
options.log({
|
|
2295
|
+
level: "warn",
|
|
2296
|
+
message: `Could not list pending runner files \u2014 retrying on the next drain: ${describe(err)}`
|
|
2297
|
+
});
|
|
2298
|
+
return [];
|
|
2299
|
+
}
|
|
2300
|
+
if (!res.ok) {
|
|
2301
|
+
options.log({
|
|
2302
|
+
level: res.status === 404 ? "debug" : "warn",
|
|
2303
|
+
message: `Listing pending runner files returned HTTP ${res.status} \u2014 retrying on the next drain`
|
|
2304
|
+
});
|
|
2305
|
+
return [];
|
|
2306
|
+
}
|
|
2307
|
+
let body;
|
|
2308
|
+
try {
|
|
2309
|
+
body = await res.json();
|
|
2310
|
+
} catch (err) {
|
|
2311
|
+
options.log({
|
|
2312
|
+
level: "warn",
|
|
2313
|
+
message: `Pending runner file list was not readable JSON \u2014 retrying on the next drain: ${describe(err)}`
|
|
2314
|
+
});
|
|
2315
|
+
return [];
|
|
2316
|
+
}
|
|
2317
|
+
if (!Array.isArray(body)) {
|
|
2318
|
+
options.log({
|
|
2319
|
+
level: "warn",
|
|
2320
|
+
message: "Pending runner file list was not an array \u2014 ignoring it for this drain"
|
|
2321
|
+
});
|
|
2322
|
+
return [];
|
|
2323
|
+
}
|
|
2324
|
+
const files = [];
|
|
2325
|
+
for (const entry of body) {
|
|
2326
|
+
const file = asPendingFile(entry);
|
|
2327
|
+
if (file === null) {
|
|
2328
|
+
options.log({
|
|
2329
|
+
level: "warn",
|
|
2330
|
+
message: "Ignoring a malformed pending runner file entry (expected id, path and size)"
|
|
2331
|
+
});
|
|
2332
|
+
continue;
|
|
2333
|
+
}
|
|
2334
|
+
files.push(file);
|
|
2335
|
+
}
|
|
2336
|
+
return files;
|
|
2337
|
+
}
|
|
2338
|
+
function asPendingFile(entry) {
|
|
2339
|
+
if (entry === null || typeof entry !== "object") return null;
|
|
2340
|
+
const { id, path, size } = entry;
|
|
2341
|
+
if (typeof id !== "string" || id === "") return null;
|
|
2342
|
+
if (typeof path !== "string" || path === "") return null;
|
|
2343
|
+
if (typeof size !== "number" || !Number.isFinite(size) || size < 0) return null;
|
|
2344
|
+
return { id, path, size };
|
|
2345
|
+
}
|
|
2346
|
+
async function applyOne(options, file) {
|
|
2347
|
+
const label = `${file.id.slice(0, 8)} (${file.path})`;
|
|
2348
|
+
if (options.allowedDirectories.length === 0) {
|
|
2349
|
+
options.log({
|
|
2350
|
+
level: "warn",
|
|
2351
|
+
message: `Runner file ${label} rejected: file sync is not enabled on this runner (start it with --enable-file-sync-to)`
|
|
2352
|
+
});
|
|
2353
|
+
await ack(options, file, "rejected", "file_sync_disabled");
|
|
2354
|
+
return false;
|
|
2355
|
+
}
|
|
2356
|
+
if (file.size > MAX_FILE_PUSH_BYTES) {
|
|
2357
|
+
options.log({
|
|
2358
|
+
level: "warn",
|
|
2359
|
+
message: `Runner file ${label} rejected: declared ${file.size} bytes, the limit is ${MAX_FILE_PUSH_BYTES}`
|
|
2360
|
+
});
|
|
2361
|
+
await ack(options, file, "rejected", "file_too_large");
|
|
2362
|
+
return false;
|
|
2363
|
+
}
|
|
2364
|
+
const download = await downloadContent(options, file, label);
|
|
2365
|
+
if (!download.ok) {
|
|
2366
|
+
if (download.terminal) await ack(options, file, "rejected", download.code);
|
|
2367
|
+
return false;
|
|
2368
|
+
}
|
|
2369
|
+
let outcome;
|
|
2370
|
+
try {
|
|
2371
|
+
outcome = await writePushedFile({
|
|
2372
|
+
requestedPath: file.path,
|
|
2373
|
+
content: download.content,
|
|
2374
|
+
allowedDirectories: options.allowedDirectories,
|
|
2375
|
+
homeDir: options.homeDir
|
|
2376
|
+
});
|
|
2377
|
+
} catch (err) {
|
|
2378
|
+
options.log({
|
|
2379
|
+
level: "error",
|
|
2380
|
+
message: `Runner file ${label} could not be written: ${describe(err)}`
|
|
2381
|
+
});
|
|
2382
|
+
await ack(options, file, "rejected", "write_failed");
|
|
2383
|
+
return false;
|
|
2384
|
+
}
|
|
2385
|
+
if (!outcome.ok) {
|
|
2386
|
+
options.log({
|
|
2387
|
+
level: "warn",
|
|
2388
|
+
message: `Runner file ${label} rejected (${outcome.code}): ${outcome.message}`
|
|
2389
|
+
});
|
|
2390
|
+
await ack(options, file, "rejected", outcome.code);
|
|
2391
|
+
return false;
|
|
2392
|
+
}
|
|
2393
|
+
options.log({
|
|
2394
|
+
level: "info",
|
|
2395
|
+
message: `Runner file ${label} applied (${download.content.byteLength} bytes)`
|
|
2396
|
+
});
|
|
2397
|
+
await ack(options, file, "applied");
|
|
2398
|
+
return true;
|
|
2399
|
+
}
|
|
2400
|
+
function durableDownloadCode(status) {
|
|
2401
|
+
return status === 413 ? "file_too_large" : "write_failed";
|
|
2402
|
+
}
|
|
2403
|
+
async function downloadContent(options, file, label) {
|
|
2404
|
+
try {
|
|
2405
|
+
const res = await options.fetchImpl(
|
|
2406
|
+
`${options.apiUrl}/runners/${options.agentId}/files/${file.id}/content`,
|
|
2407
|
+
{ headers: { Authorization: options.getAuthHeader() } }
|
|
2408
|
+
);
|
|
2409
|
+
if (!res.ok) {
|
|
2410
|
+
const terminal = res.status >= 400 && res.status < 500 && res.status !== 401 && res.status !== 403 && res.status !== 408 && res.status !== 429;
|
|
2411
|
+
if (!terminal) {
|
|
2412
|
+
options.log({
|
|
2413
|
+
level: "warn",
|
|
2414
|
+
message: `Downloading runner file ${label} returned HTTP ${res.status} \u2014 retrying on the next drain`
|
|
2415
|
+
});
|
|
2416
|
+
return { ok: false, terminal: false };
|
|
2417
|
+
}
|
|
2418
|
+
const code = durableDownloadCode(res.status);
|
|
2419
|
+
options.log({
|
|
2420
|
+
level: "error",
|
|
2421
|
+
message: `Downloading runner file ${label} returned HTTP ${res.status} \u2014 rejecting it as ${code} (the bytes never reached the writer)`
|
|
2422
|
+
});
|
|
2423
|
+
return { ok: false, terminal: true, code };
|
|
2424
|
+
}
|
|
2425
|
+
return { ok: true, content: Buffer.from(await res.arrayBuffer()) };
|
|
2426
|
+
} catch (err) {
|
|
2427
|
+
options.log({
|
|
2428
|
+
level: "warn",
|
|
2429
|
+
message: `Downloading runner file ${label} failed \u2014 retrying on the next drain: ${describe(err)}`
|
|
2430
|
+
});
|
|
2431
|
+
return { ok: false, terminal: false };
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
async function ack(options, file, status, reason) {
|
|
2435
|
+
const outcome = `${status}${reason ? ` (${reason})` : ""}`;
|
|
2436
|
+
try {
|
|
2437
|
+
const res = await options.fetchImpl(
|
|
2438
|
+
`${options.apiUrl}/runners/${options.agentId}/files/${file.id}/ack`,
|
|
2439
|
+
{
|
|
2440
|
+
method: "POST",
|
|
2441
|
+
headers: {
|
|
2442
|
+
Authorization: options.getAuthHeader(),
|
|
2443
|
+
"Content-Type": "application/json"
|
|
2444
|
+
},
|
|
2445
|
+
body: JSON.stringify(reason ? { status, reason } : { status })
|
|
2446
|
+
}
|
|
2447
|
+
);
|
|
2448
|
+
if (!res.ok) {
|
|
2449
|
+
recordAckFailure(
|
|
2450
|
+
options,
|
|
2451
|
+
file,
|
|
2452
|
+
`Acking runner file ${file.id.slice(0, 8)} as ${outcome} returned HTTP ${res.status}`
|
|
2453
|
+
);
|
|
2454
|
+
return;
|
|
2455
|
+
}
|
|
2456
|
+
options.ackFailures.delete(file.id);
|
|
2457
|
+
} catch (err) {
|
|
2458
|
+
recordAckFailure(
|
|
2459
|
+
options,
|
|
2460
|
+
file,
|
|
2461
|
+
`Acking runner file ${file.id.slice(0, 8)} as ${outcome} failed: ${describe(err)}`
|
|
2462
|
+
);
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
function recordAckFailure(options, file, what) {
|
|
2466
|
+
const attempts = (options.ackFailures.get(file.id) ?? 0) + 1;
|
|
2467
|
+
options.ackFailures.set(file.id, attempts);
|
|
2468
|
+
options.log({
|
|
2469
|
+
level: "error",
|
|
2470
|
+
message: attempts >= MAX_ACK_ATTEMPTS ? `${what} \u2014 giving up after ${attempts} attempts. It stays pending until the server expires it; restart the runner to retry.` : `${what} \u2014 it stays pending until a later drain re-acks it (attempt ${attempts} of ${MAX_ACK_ATTEMPTS})`
|
|
2471
|
+
});
|
|
2472
|
+
}
|
|
2473
|
+
function describe(err) {
|
|
2474
|
+
return err instanceof Error ? err.message : String(err);
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
// src/lib/channels/driver.ts
|
|
2478
|
+
function messageIdOf(m) {
|
|
2479
|
+
if (!m || typeof m !== "object") return void 0;
|
|
2480
|
+
if (typeof m.id === "string") return m.id;
|
|
2481
|
+
const infoId = m.info?.id;
|
|
2482
|
+
return typeof infoId === "string" ? infoId : void 0;
|
|
2483
|
+
}
|
|
2484
|
+
function cleanImageMime(contentType) {
|
|
2485
|
+
if (!contentType) return null;
|
|
2486
|
+
const media = contentType.split(";")[0].trim().toLowerCase();
|
|
2487
|
+
return /^image\/[a-z0-9.+-]+$/.test(media) ? media : null;
|
|
2488
|
+
}
|
|
2489
|
+
var LOG_LEVELS = {
|
|
2490
|
+
debug: 0,
|
|
2491
|
+
info: 1,
|
|
2492
|
+
warn: 2,
|
|
2493
|
+
error: 3
|
|
2494
|
+
};
|
|
2495
|
+
var DEFAULT_RETRY_POLICY = {
|
|
2496
|
+
maxAttempts: 6,
|
|
2497
|
+
baseDelayMs: 500,
|
|
2498
|
+
maxDelayMs: 3e4
|
|
2499
|
+
};
|
|
2500
|
+
var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
|
|
2501
|
+
var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
2502
|
+
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
2503
|
+
var HEARTBEAT_MS = 6e4;
|
|
2504
|
+
var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
|
|
2505
|
+
var B2_ABANDONMENT_MIN_PINNED_MS = 3 * 6e4;
|
|
2506
|
+
var B2_ABANDONMENT_RECHECK_MS = HEARTBEAT_MS;
|
|
2507
|
+
var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
|
|
2508
|
+
var MAX_SUPERSEDED_CONVERSATIONS = 256;
|
|
2509
|
+
var ChannelAuthError = class extends Error {
|
|
2510
|
+
constructor(message) {
|
|
2511
|
+
super(message);
|
|
2512
|
+
this.name = "ChannelAuthError";
|
|
2513
|
+
}
|
|
2514
|
+
};
|
|
2515
|
+
var ChannelTerminalError = class extends Error {
|
|
2516
|
+
status;
|
|
2517
|
+
constructor(message, status) {
|
|
2518
|
+
super(message);
|
|
2519
|
+
this.name = "ChannelTerminalError";
|
|
2520
|
+
this.status = status;
|
|
2521
|
+
}
|
|
2522
|
+
};
|
|
2523
|
+
function backoffDelay(attempt, policy) {
|
|
2524
|
+
const exp = policy.baseDelayMs * Math.pow(2, attempt);
|
|
2525
|
+
const capped = Math.min(policy.maxDelayMs, exp);
|
|
2526
|
+
return Math.floor(Math.random() * capped);
|
|
2527
|
+
}
|
|
2528
|
+
function isRetryableStatus(status) {
|
|
2529
|
+
return status === 429 || status >= 500 && status <= 599;
|
|
2530
|
+
}
|
|
2531
|
+
var ChannelDriver = class _ChannelDriver {
|
|
2532
|
+
agentId;
|
|
2533
|
+
port;
|
|
2534
|
+
apiUrl;
|
|
2535
|
+
getAuthHeader;
|
|
2536
|
+
conversationFilter;
|
|
2537
|
+
retry;
|
|
2538
|
+
log;
|
|
2539
|
+
fetchImpl;
|
|
2540
|
+
sleep;
|
|
2541
|
+
pausedPollIntervalMs;
|
|
2542
|
+
pausedMaxWaitMs;
|
|
2543
|
+
stuckQueuedMs;
|
|
2544
|
+
now;
|
|
2545
|
+
fileSyncDirectories;
|
|
2546
|
+
homeDir;
|
|
2547
|
+
/** Cache of conversationId → opencode sessionId. */
|
|
2548
|
+
sessions = /* @__PURE__ */ new Map();
|
|
2549
|
+
/**
|
|
2550
|
+
* conversationId → the opencode session this runner has ABANDONED as that
|
|
2551
|
+
* conversation's binding (#553), after a genuine (`sessionExists === true`)
|
|
2552
|
+
* dispatch failure: the session still exists but is wedged, so #485's self-heal
|
|
2553
|
+
* must bind a fresh one.
|
|
2554
|
+
*
|
|
2555
|
+
* Dropping the local binding + clearing the server row is not enough on its own:
|
|
2556
|
+
* a SIBLING message dispatched earlier in the same drain is still in-flight under
|
|
2557
|
+
* the same session, and its watcher's routine status writes carry
|
|
2558
|
+
* `opencode_session_id`, RESURRECTING the wedged id server-side after the clear —
|
|
2559
|
+
* and `ensureSession`'s persisted-id fallback then reuses it, defeating the
|
|
2560
|
+
* self-heal. This map makes the runner authoritative instead of racing those
|
|
2561
|
+
* writes: *`ensureSession` never reuses an abandoned id for that conversation,
|
|
2562
|
+
* whatever the server row says* — which holds even when the resurrecting write
|
|
2563
|
+
* is one we deliberately keep (see `markDone`).
|
|
2564
|
+
*
|
|
2565
|
+
* Bounded by construction, on both axes: keyed by CONVERSATION, so N failures on
|
|
2566
|
+
* one conversation hold ONE entry (the newest abandonment replaces the older), and
|
|
2567
|
+
* hard-capped at `MAX_SUPERSEDED_CONVERSATIONS` with FIFO eviction. Only the
|
|
2568
|
+
* NEWEST abandoned id per conversation is guarded: after a second abandonment a
|
|
2569
|
+
* late sibling of the FIRST session can write that id back and `ensureSession`
|
|
2570
|
+
* will reuse it — costing ONE repeat failure, which re-supersedes it. Deliberately
|
|
2571
|
+
* NOT dropped when the session's watcher tears down: `markDone` still writes the
|
|
2572
|
+
* abandoned id back (it must, or the reply is lost), so the guard has to outlive
|
|
2573
|
+
* the turn that resurrects it. In-memory only — a restart forgets it, at the same
|
|
2574
|
+
* bounded cost.
|
|
2575
|
+
*/
|
|
2576
|
+
supersededSessions = /* @__PURE__ */ new Map();
|
|
2577
|
+
/**
|
|
2578
|
+
* Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
|
|
2579
|
+
* longer idempotent (no caller-supplied `messageID`), and its read-back picks
|
|
2580
|
+
* "the one new user row" — which is only unambiguous if no OTHER dispatch into
|
|
2581
|
+
* the SAME session interleaves its snapshot→POST→read-back. This map chains each
|
|
2582
|
+
* session's dispatches so they run serially; distinct sessions stay concurrent.
|
|
2583
|
+
*/
|
|
2584
|
+
sessionDispatchLocks = /* @__PURE__ */ new Map();
|
|
2585
|
+
/**
|
|
2586
|
+
* Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
|
|
2587
|
+
* session: one polling loop services all of that session's in-flight messages.
|
|
2588
|
+
* A session entry exists while it has any in-flight (dispatched-but-not-done)
|
|
2589
|
+
* message; it is removed once its in-flight set empties.
|
|
2590
|
+
*/
|
|
1687
2591
|
watchers = /* @__PURE__ */ new Map();
|
|
1688
2592
|
/**
|
|
1689
2593
|
* AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
|
|
@@ -1728,6 +2632,15 @@ var ChannelDriver = class {
|
|
|
1728
2632
|
* the row leaves the processing list, exactly like `dontRedispatch`.
|
|
1729
2633
|
*/
|
|
1730
2634
|
doneUndeliverable = /* @__PURE__ */ new Set();
|
|
2635
|
+
/**
|
|
2636
|
+
* "Already emitted `readopt_poll_unresolved` for this row" (#229). The b1 /
|
|
2637
|
+
* unreadable-status re-evaluate leaf leaves the row UN-tracked so it is re-read
|
|
2638
|
+
* every ~2s drain until the status map becomes readable — but the server-visible
|
|
2639
|
+
* signal is an OUTCOME, so it must fire at most ONCE per row, not once per drain
|
|
2640
|
+
* (Bugbot "Re-adopt signals flood every drain"). Cleared when the row leaves the
|
|
2641
|
+
* processing list, exactly like `dontRedispatch`/`doneUndeliverable`.
|
|
2642
|
+
*/
|
|
2643
|
+
readoptPollUnresolvedSignalled = /* @__PURE__ */ new Set();
|
|
1731
2644
|
/**
|
|
1732
2645
|
* "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
|
|
1733
2646
|
* Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
|
|
@@ -1742,6 +2655,15 @@ var ChannelDriver = class {
|
|
|
1742
2655
|
* so the NEXT tick may retry exactly once more).
|
|
1743
2656
|
*/
|
|
1744
2657
|
awaitingReadopt = /* @__PURE__ */ new Set();
|
|
2658
|
+
/**
|
|
2659
|
+
* "Already signalled `attachments_skipped` for this Evident message id" (#376).
|
|
2660
|
+
* The in-thread skip note is an OUTCOME, so it must fire AT MOST ONCE per message
|
|
2661
|
+
* — never re-post on a re-dispatch of the same row (`forceReadoptRun` or the
|
|
2662
|
+
* next-tick null-id retry both re-run `sendPromptAsync`, which re-fires
|
|
2663
|
+
* `onOutcomes`). Mirrors `readoptPollUnresolvedSignalled`: a local dedup on the
|
|
2664
|
+
* outcome, not the dispatch. Not cleared (a message is signalled once for life).
|
|
2665
|
+
*/
|
|
2666
|
+
attachmentsSkippedSignalled = /* @__PURE__ */ new Set();
|
|
1745
2667
|
/**
|
|
1746
2668
|
* Cache of the opencode root directory (from `GET /path`). Resolved lazily on
|
|
1747
2669
|
* first session creation so drain-created sessions are rooted at the project
|
|
@@ -1759,8 +2681,39 @@ var ChannelDriver = class {
|
|
|
1759
2681
|
* entry = not yet resolved; `null` = resolved root (stop walking).
|
|
1760
2682
|
*/
|
|
1761
2683
|
sessionParents = /* @__PURE__ */ new Map();
|
|
2684
|
+
/**
|
|
2685
|
+
* Per-session OpenCode title cache (#310), keyed by sessionId. Only a resolved
|
|
2686
|
+
* NON-EMPTY, non-placeholder name is stored (terminal — a real session name
|
|
2687
|
+
* won't later un-name), so we do NOT re-GET `/session/:id` every tick. "Non-empty"
|
|
2688
|
+
* excludes OpenCode's synchronous default title (see
|
|
2689
|
+
* `OPENCODE_DEFAULT_TITLE_PREFIX`, #549) — that placeholder is treated the same
|
|
2690
|
+
* as an empty title so it never latches. A missing entry = not yet resolved OR
|
|
2691
|
+
* resolved-but-still-empty/placeholder → re-fetch on next need, since OpenCode
|
|
2692
|
+
* names sessions asynchronously mid-turn. Driver-level (not per-watcher) so both
|
|
2693
|
+
* the watcher completion path AND the restart-recovery re-adopt path (which has
|
|
2694
|
+
* no watcher) can resolve the title.
|
|
2695
|
+
*/
|
|
2696
|
+
sessionTitles = /* @__PURE__ */ new Map();
|
|
1762
2697
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
1763
2698
|
draining = false;
|
|
2699
|
+
/**
|
|
2700
|
+
* Serialises runner-file syncs (#559) so the ~2s poll tick and a concurrent
|
|
2701
|
+
* drain ping don't download, write and ack the same file twice.
|
|
2702
|
+
*/
|
|
2703
|
+
syncingFiles = false;
|
|
2704
|
+
/**
|
|
2705
|
+
* Consecutive failed acks per pending file (#559). Lives on the driver so it
|
|
2706
|
+
* survives across drains — without it, a file whose ack keeps failing is
|
|
2707
|
+
* re-downloaded and re-written every ~2s until the server expires it.
|
|
2708
|
+
*/
|
|
2709
|
+
fileAckFailures = /* @__PURE__ */ new Map();
|
|
2710
|
+
/**
|
|
2711
|
+
* Monotonic count of files this runner has pulled and written (#559). Only
|
|
2712
|
+
* ever increases, so `run.ts` detects work by comparing it against the value
|
|
2713
|
+
* it saw on the previous cycle — including work that landed mid-sleep, the
|
|
2714
|
+
* same trick `lastProxiedActivityAt` uses.
|
|
2715
|
+
*/
|
|
2716
|
+
appliedFileCount = 0;
|
|
1764
2717
|
/**
|
|
1765
2718
|
* The currently-executing `drainPending()` promise, or null when idle. Lets a
|
|
1766
2719
|
* graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
|
|
@@ -1791,14 +2744,13 @@ var ChannelDriver = class {
|
|
|
1791
2744
|
this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
|
|
1792
2745
|
this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
1793
2746
|
this.now = config2.now ?? (() => Date.now());
|
|
2747
|
+
this.fileSyncDirectories = config2.fileSyncDirectories ?? [];
|
|
2748
|
+
this.homeDir = config2.homeDir ?? homedir();
|
|
1794
2749
|
}
|
|
1795
2750
|
/** The IPv4-loopback base URL for the local `opencode serve`. */
|
|
1796
2751
|
get opencodeBase() {
|
|
1797
2752
|
return `http://127.0.0.1:${this.port}`;
|
|
1798
2753
|
}
|
|
1799
|
-
// -------------------------------------------------------------------------
|
|
1800
|
-
// Public API
|
|
1801
|
-
// -------------------------------------------------------------------------
|
|
1802
2754
|
/**
|
|
1803
2755
|
* Drain all pending channel conversations once: poll → dispatch → register.
|
|
1804
2756
|
* Called on tunnel `connected` (WI-CHAN-4) and on each poll tick by `run.ts`.
|
|
@@ -1821,6 +2773,47 @@ var ChannelDriver = class {
|
|
|
1821
2773
|
);
|
|
1822
2774
|
return run2;
|
|
1823
2775
|
}
|
|
2776
|
+
/**
|
|
2777
|
+
* Pull-and-apply any files Evident has queued for this runner (#559), riding
|
|
2778
|
+
* the EXISTING drain cycle — `run.ts` calls it from the same ~2s channel poll
|
|
2779
|
+
* and drain ping that call `drainPending()`. There is deliberately no channel,
|
|
2780
|
+
* control frame or poll loop of its own: worst-case latency is one poll tick.
|
|
2781
|
+
*
|
|
2782
|
+
* NEVER throws and never surfaces a `ChannelAuthError`: a file failure must not
|
|
2783
|
+
* cost a conversation turn. Failures are logged and either acked as a terminal
|
|
2784
|
+
* outcome or left pending for the next drain (see `runner-file-sync.ts`).
|
|
2785
|
+
*
|
|
2786
|
+
* Re-entrant calls are skipped (the poll tick and a drain ping can overlap).
|
|
2787
|
+
*
|
|
2788
|
+
* @returns the number of files written to disk.
|
|
2789
|
+
*/
|
|
2790
|
+
async syncPendingFiles() {
|
|
2791
|
+
if (this.stopped) return 0;
|
|
2792
|
+
if (this.syncingFiles) return 0;
|
|
2793
|
+
this.syncingFiles = true;
|
|
2794
|
+
try {
|
|
2795
|
+
const applied = await syncPendingRunnerFiles({
|
|
2796
|
+
agentId: this.agentId,
|
|
2797
|
+
apiUrl: this.apiUrl,
|
|
2798
|
+
getAuthHeader: this.getAuthHeader,
|
|
2799
|
+
fetchImpl: this.fetchImpl,
|
|
2800
|
+
allowedDirectories: this.fileSyncDirectories,
|
|
2801
|
+
homeDir: this.homeDir,
|
|
2802
|
+
ackFailures: this.fileAckFailures,
|
|
2803
|
+
log: this.log
|
|
2804
|
+
});
|
|
2805
|
+
this.appliedFileCount += applied;
|
|
2806
|
+
return applied;
|
|
2807
|
+
} catch (err) {
|
|
2808
|
+
this.log({
|
|
2809
|
+
level: "error",
|
|
2810
|
+
message: `Runner file sync failed unexpectedly (message processing is unaffected): ${err instanceof Error ? err.message : String(err)}`
|
|
2811
|
+
});
|
|
2812
|
+
return 0;
|
|
2813
|
+
} finally {
|
|
2814
|
+
this.syncingFiles = false;
|
|
2815
|
+
}
|
|
2816
|
+
}
|
|
1824
2817
|
async runDrain() {
|
|
1825
2818
|
let dispatched = 0;
|
|
1826
2819
|
try {
|
|
@@ -1854,6 +2847,50 @@ var ChannelDriver = class {
|
|
|
1854
2847
|
}
|
|
1855
2848
|
return false;
|
|
1856
2849
|
}
|
|
2850
|
+
/**
|
|
2851
|
+
* File-pull work, for `run.ts`'s idle accounting (#559).
|
|
2852
|
+
*
|
|
2853
|
+
* Pulling a file is real work that `drainPending()` knows nothing about, so
|
|
2854
|
+
* without this a near-idle runner counts a credential pull as an empty tick
|
|
2855
|
+
* and `--idle-timeout` can `process.exit` mid-pull — leaving a
|
|
2856
|
+
* `.evident-push-*.tmp` behind — or immediately after the write, before the
|
|
2857
|
+
* browser has run the authorize/callback that activates it (the user then sees
|
|
2858
|
+
* `saved_not_activated` for a runner that was fine).
|
|
2859
|
+
*
|
|
2860
|
+
* Two signals because one cannot cover both cases: `inFlight` is the pull
|
|
2861
|
+
* happening RIGHT NOW (it may outlive the tick that started it), and
|
|
2862
|
+
* `appliedFiles` is monotonic so a pull that started AND finished between two
|
|
2863
|
+
* idle checks still shows up as an advance.
|
|
2864
|
+
*
|
|
2865
|
+
* CALLER CONTRACT: sample `inFlight` BEFORE calling `syncPendingFiles()` for
|
|
2866
|
+
* the cycle. `syncPendingFiles` sets the flag synchronously, so a caller that
|
|
2867
|
+
* samples afterwards reads `true` every single cycle and can never idle out.
|
|
2868
|
+
*/
|
|
2869
|
+
fileSyncActivity() {
|
|
2870
|
+
return { appliedFiles: this.appliedFileCount, inFlight: this.syncingFiles };
|
|
2871
|
+
}
|
|
2872
|
+
/**
|
|
2873
|
+
* OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
|
|
2874
|
+
* exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
|
|
2875
|
+
* `watchers` entry whose `inFlight` set is non-empty — the same predicate
|
|
2876
|
+
* `hasInFlightWatchers()` uses, lifted to return the ids.
|
|
2877
|
+
*
|
|
2878
|
+
* Deliberately does NOT include `this.sessions` (the permanent, never-pruned
|
|
2879
|
+
* conversation→session cache). Protecting every bound-but-idle session there
|
|
2880
|
+
* would shield nearly every session and defeat cleanup — AND it is unnecessary:
|
|
2881
|
+
* `ensureSession` is self-healing (it recreates a session whose id no longer
|
|
2882
|
+
* exists), so deleting an idle bound session is harmless — the conversation's
|
|
2883
|
+
* next turn transparently rebinds a fresh one. The only thing worth protecting
|
|
2884
|
+
* is a session with a turn ACTIVELY in flight right now: tearing that down
|
|
2885
|
+
* mid-turn would strand the running `prompt_async`. Idle sessions are fair game.
|
|
2886
|
+
*/
|
|
2887
|
+
protectedSessionIds() {
|
|
2888
|
+
const ids = /* @__PURE__ */ new Set();
|
|
2889
|
+
for (const [sessionId, watcher] of this.watchers) {
|
|
2890
|
+
if (watcher.inFlight.size > 0) ids.add(sessionId);
|
|
2891
|
+
}
|
|
2892
|
+
return ids;
|
|
2893
|
+
}
|
|
1857
2894
|
/**
|
|
1858
2895
|
* Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
|
|
1859
2896
|
* this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
|
|
@@ -1893,7 +2930,7 @@ var ChannelDriver = class {
|
|
|
1893
2930
|
await this.sleep(step);
|
|
1894
2931
|
}
|
|
1895
2932
|
}
|
|
1896
|
-
while (this.hasInFlightWatchers()) {
|
|
2933
|
+
while (this.hasInFlightWatchers() || this.syncingFiles) {
|
|
1897
2934
|
if (this.now() >= deadline) return false;
|
|
1898
2935
|
await this.sleep(step);
|
|
1899
2936
|
}
|
|
@@ -1918,9 +2955,7 @@ var ChannelDriver = class {
|
|
|
1918
2955
|
if (!stillLive) return;
|
|
1919
2956
|
}
|
|
1920
2957
|
}
|
|
1921
|
-
// -------------------------------------------------------------------------
|
|
1922
2958
|
// Conversation processing (WI-3 — async dispatch)
|
|
1923
|
-
// -------------------------------------------------------------------------
|
|
1924
2959
|
/**
|
|
1925
2960
|
* Dispatch each pending message for a conversation to opencode's native queue
|
|
1926
2961
|
* via `prompt_async` (Task 3.2) and register it with the conversation's
|
|
@@ -1930,10 +2965,15 @@ var ChannelDriver = class {
|
|
|
1930
2965
|
* @returns the count of messages NEWLY dispatched (not already in-flight).
|
|
1931
2966
|
*/
|
|
1932
2967
|
async processConversation(conv) {
|
|
1933
|
-
const sessionId = await this.ensureSession(conv);
|
|
2968
|
+
const { sessionId, refusedSessionId } = await this.ensureSession(conv);
|
|
1934
2969
|
const messages = await this.getPendingMessages(conv.id);
|
|
1935
2970
|
let dispatched = 0;
|
|
1936
2971
|
let skippedAlreadyDispatched = 0;
|
|
2972
|
+
if (refusedSessionId && messages.length > 0) {
|
|
2973
|
+
void this.postSignal(conv.id, messages[0].id, "session_superseded", {
|
|
2974
|
+
superseded_session_id: refusedSessionId
|
|
2975
|
+
});
|
|
2976
|
+
}
|
|
1937
2977
|
for (const message of messages) {
|
|
1938
2978
|
if (this.stopped) break;
|
|
1939
2979
|
if (this.dispatched.has(message.id)) {
|
|
@@ -1952,26 +2992,62 @@ var ChannelDriver = class {
|
|
|
1952
2992
|
conversation_id: conv.id,
|
|
1953
2993
|
message_id: message.id
|
|
1954
2994
|
});
|
|
2995
|
+
const sendAttachments = this.buildSendAttachments(conv, message);
|
|
1955
2996
|
opencodeMessageId = await this.dispatchLocked(
|
|
1956
2997
|
sessionId,
|
|
1957
|
-
() => sendPromptAsync(this.port, sessionId, message.content, options)
|
|
2998
|
+
() => sendPromptAsync(this.port, sessionId, message.content, options, sendAttachments)
|
|
1958
2999
|
);
|
|
1959
3000
|
} catch (err) {
|
|
1960
3001
|
if (err instanceof ChannelAuthError) throw err;
|
|
1961
3002
|
this.dispatched.delete(message.id);
|
|
1962
|
-
await this.
|
|
3003
|
+
const exists = await sessionExists(this.port, sessionId);
|
|
3004
|
+
if (exists === false) {
|
|
3005
|
+
this.sessions.delete(conv.id);
|
|
3006
|
+
this.log({
|
|
3007
|
+
level: "warn",
|
|
3008
|
+
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.`,
|
|
3009
|
+
conversation_id: conv.id,
|
|
3010
|
+
message_id: message.id
|
|
3011
|
+
});
|
|
3012
|
+
break;
|
|
3013
|
+
}
|
|
3014
|
+
if (exists === null) {
|
|
3015
|
+
this.log({
|
|
3016
|
+
level: "warn",
|
|
3017
|
+
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.`,
|
|
3018
|
+
conversation_id: conv.id,
|
|
3019
|
+
message_id: message.id
|
|
3020
|
+
});
|
|
3021
|
+
break;
|
|
3022
|
+
}
|
|
3023
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
3024
|
+
this.sessions.delete(conv.id);
|
|
3025
|
+
this.supersede(conv.id, sessionId);
|
|
3026
|
+
this.log({
|
|
3027
|
+
level: "warn",
|
|
3028
|
+
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.`,
|
|
3029
|
+
conversation_id: conv.id,
|
|
3030
|
+
message_id: message.id
|
|
3031
|
+
});
|
|
3032
|
+
await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
|
|
3033
|
+
this.log({
|
|
3034
|
+
level: "warn",
|
|
3035
|
+
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)}`,
|
|
3036
|
+
conversation_id: conv.id,
|
|
3037
|
+
message_id: message.id
|
|
3038
|
+
});
|
|
1963
3039
|
});
|
|
1964
3040
|
this.log({
|
|
1965
3041
|
level: "error",
|
|
1966
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
3042
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
|
|
1967
3043
|
conversation_id: conv.id,
|
|
1968
3044
|
message_id: message.id
|
|
1969
3045
|
});
|
|
1970
|
-
|
|
3046
|
+
break;
|
|
1971
3047
|
}
|
|
1972
3048
|
if (opencodeMessageId === null) {
|
|
1973
3049
|
this.log({
|
|
1974
|
-
level: "
|
|
3050
|
+
level: "warn",
|
|
1975
3051
|
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
3052
|
conversation_id: conv.id,
|
|
1977
3053
|
message_id: message.id
|
|
@@ -1985,7 +3061,7 @@ var ChannelDriver = class {
|
|
|
1985
3061
|
}
|
|
1986
3062
|
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
1987
3063
|
this.log({
|
|
1988
|
-
level: "
|
|
3064
|
+
level: "warn",
|
|
1989
3065
|
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
3066
|
conversation_id: conv.id
|
|
1991
3067
|
});
|
|
@@ -1993,17 +3069,68 @@ var ChannelDriver = class {
|
|
|
1993
3069
|
this.ensureWatcherRunning(sessionId);
|
|
1994
3070
|
return dispatched;
|
|
1995
3071
|
}
|
|
3072
|
+
/**
|
|
3073
|
+
* Record that `sessionId` is no longer a valid binding for `conversationId`
|
|
3074
|
+
* (#553). Keyed by conversation and hard-capped, so it cannot grow with the
|
|
3075
|
+
* number of failures — see the `supersededSessions` field doc.
|
|
3076
|
+
*/
|
|
3077
|
+
supersede(conversationId, sessionId) {
|
|
3078
|
+
this.supersededSessions.delete(conversationId);
|
|
3079
|
+
this.supersededSessions.set(conversationId, sessionId);
|
|
3080
|
+
while (this.supersededSessions.size > MAX_SUPERSEDED_CONVERSATIONS) {
|
|
3081
|
+
const oldest = this.supersededSessions.keys().next().value;
|
|
3082
|
+
if (oldest === void 0) return;
|
|
3083
|
+
this.supersededSessions.delete(oldest);
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
/** Whether `sessionId` is the session this conversation has abandoned (#553). */
|
|
3087
|
+
isSuperseded(conversationId, sessionId) {
|
|
3088
|
+
return this.supersededSessions.get(conversationId) === sessionId;
|
|
3089
|
+
}
|
|
3090
|
+
/**
|
|
3091
|
+
* Resolve the opencode session to run this conversation's turns in.
|
|
3092
|
+
*
|
|
3093
|
+
* `refusedSessionId` is set when the #553 guard fired — i.e. the persisted
|
|
3094
|
+
* binding was an id this runner had abandoned, so a resurrection genuinely
|
|
3095
|
+
* happened and a fresh session was bound instead. The caller reports it.
|
|
3096
|
+
*/
|
|
1996
3097
|
async ensureSession(conv) {
|
|
1997
|
-
const
|
|
1998
|
-
if (
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
3098
|
+
const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
|
|
3099
|
+
if (bound && this.isSuperseded(conv.id, bound)) {
|
|
3100
|
+
this.log({
|
|
3101
|
+
level: "warn",
|
|
3102
|
+
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.`,
|
|
3103
|
+
conversation_id: conv.id
|
|
3104
|
+
});
|
|
3105
|
+
this.sessions.delete(conv.id);
|
|
3106
|
+
return { sessionId: await this.createAndBindSession(conv.id), refusedSessionId: bound };
|
|
3107
|
+
}
|
|
3108
|
+
if (bound) {
|
|
3109
|
+
const exists = await sessionExists(this.port, bound);
|
|
3110
|
+
if (exists === false) {
|
|
3111
|
+
this.log({
|
|
3112
|
+
level: "debug",
|
|
3113
|
+
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.`,
|
|
3114
|
+
conversation_id: conv.id
|
|
3115
|
+
});
|
|
3116
|
+
this.sessions.delete(conv.id);
|
|
3117
|
+
return { sessionId: await this.createAndBindSession(conv.id) };
|
|
3118
|
+
}
|
|
3119
|
+
this.sessions.set(conv.id, bound);
|
|
3120
|
+
return { sessionId: bound };
|
|
2002
3121
|
}
|
|
3122
|
+
return { sessionId: await this.createAndBindSession(conv.id) };
|
|
3123
|
+
}
|
|
3124
|
+
/**
|
|
3125
|
+
* Create a fresh OpenCode session for a conversation, cache the binding, and
|
|
3126
|
+
* best-effort persist it server-side. Shared by the first-ever bind and the
|
|
3127
|
+
* self-heal recreate path in `ensureSession`.
|
|
3128
|
+
*/
|
|
3129
|
+
async createAndBindSession(conversationId) {
|
|
2003
3130
|
const directory = await this.resolveOpenCodeDirectory();
|
|
2004
3131
|
const sessionId = await createOpenCodeSession(this.port, directory);
|
|
2005
|
-
this.sessions.set(
|
|
2006
|
-
await this.persistSession(
|
|
3132
|
+
this.sessions.set(conversationId, sessionId);
|
|
3133
|
+
await this.persistSession(conversationId, sessionId).catch(() => {
|
|
2007
3134
|
});
|
|
2008
3135
|
return sessionId;
|
|
2009
3136
|
}
|
|
@@ -2017,15 +3144,13 @@ var ChannelDriver = class {
|
|
|
2017
3144
|
this.opencodeDirectory = await getOpenCodeDirectory(this.port);
|
|
2018
3145
|
if (!this.opencodeDirectory) {
|
|
2019
3146
|
this.log({
|
|
2020
|
-
level: "
|
|
3147
|
+
level: "warn",
|
|
2021
3148
|
message: "Could not determine opencode directory (GET /path) \u2014 new sessions may not appear in opencode web"
|
|
2022
3149
|
});
|
|
2023
3150
|
}
|
|
2024
3151
|
return this.opencodeDirectory;
|
|
2025
3152
|
}
|
|
2026
|
-
// -------------------------------------------------------------------------
|
|
2027
3153
|
// Per-session watcher (WI-3)
|
|
2028
|
-
// -------------------------------------------------------------------------
|
|
2029
3154
|
/**
|
|
2030
3155
|
* Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
|
|
2031
3156
|
* opencode session (Task 2.1a), so two dispatches into the SAME session can
|
|
@@ -2045,6 +3170,130 @@ var ChannelDriver = class {
|
|
|
2045
3170
|
);
|
|
2046
3171
|
return run2;
|
|
2047
3172
|
}
|
|
3173
|
+
// Inbound image attachments (#255, WI-8)
|
|
3174
|
+
/**
|
|
3175
|
+
* Build the `SendAttachmentsInput` for a message's inbound images, or
|
|
3176
|
+
* `undefined` when the message has none (so a text-only turn is unchanged).
|
|
3177
|
+
*
|
|
3178
|
+
* The driver OWNS the two channel-facing concerns the session module cannot:
|
|
3179
|
+
* - the AUTHENTICATED byte fetch through Evident's WI-6 endpoint
|
|
3180
|
+
* (`fetchAttachmentDataUrl`), using the SAME `getAuthHeader()` as every
|
|
3181
|
+
* other combinedAuth callback — the CLI NEVER talks to Slack directly;
|
|
3182
|
+
* - the in-thread SKIP NOTE (`signalAttachmentsSkipped`) posted over the
|
|
3183
|
+
* existing callback surface when any image was skipped/failed.
|
|
3184
|
+
* `sendPromptAsync` applies the capability gate + appends the `file` parts and
|
|
3185
|
+
* reports outcomes back via `onOutcomes`.
|
|
3186
|
+
*/
|
|
3187
|
+
buildSendAttachments(conv, message) {
|
|
3188
|
+
const refs = message.attachments;
|
|
3189
|
+
if (!refs || refs.length === 0) return void 0;
|
|
3190
|
+
return {
|
|
3191
|
+
inputs: refs.map((a, index) => ({
|
|
3192
|
+
index,
|
|
3193
|
+
mime: a.mime,
|
|
3194
|
+
...a.filename ? { filename: a.filename } : {}
|
|
3195
|
+
})),
|
|
3196
|
+
fetchDataUrl: (index) => this.fetchAttachmentDataUrl(message.id, index, refs[index].mime),
|
|
3197
|
+
onOutcomes: ({ outcomes, capabilityUnknown }) => this.signalAttachmentsSkipped(conv.id, message.id, outcomes, capabilityUnknown)
|
|
3198
|
+
};
|
|
3199
|
+
}
|
|
3200
|
+
/**
|
|
3201
|
+
* Fetch ONE inbound image's bytes through Evident's WI-6 endpoint
|
|
3202
|
+
* (`GET {apiUrl}/runners/{agentId}/attachments/{messageId}/{index}`) using the
|
|
3203
|
+
* existing authenticated fetch, and base64-encode into a
|
|
3204
|
+
* `data:<mime>;base64,<…>` URL for the opencode `file` part's `url`.
|
|
3205
|
+
*
|
|
3206
|
+
* The endpoint streams the source bytes verbatim (200), or returns 404
|
|
3207
|
+
* (not-owned / out-of-range / deleted-at-source / workspace gone) / 413
|
|
3208
|
+
* (over-cap). On ANY non-2xx or thrown failure we return `null` so the caller
|
|
3209
|
+
* OMITS that one image and the text turn still sends — NEVER throws the turn.
|
|
3210
|
+
* A 404 body carrying `{ reason: 'needs_reauth' }` (#547 — the server CONFIRMED
|
|
3211
|
+
* a Slack `files:read` scope problem via `files.info`) instead resolves the
|
|
3212
|
+
* `AttachmentFetchNeedsReauth` sentinel, so the in-thread note can steer the
|
|
3213
|
+
* user to reconnect Slack instead of a generic "unavailable". Failures are
|
|
3214
|
+
* logged with context (no silent swallow).
|
|
3215
|
+
*/
|
|
3216
|
+
async fetchAttachmentDataUrl(messageId, index, mime) {
|
|
3217
|
+
try {
|
|
3218
|
+
const res = await this.fetchImpl(
|
|
3219
|
+
`${this.apiUrl}/runners/${this.agentId}/attachments/${messageId}/${index}`,
|
|
3220
|
+
{ headers: { Authorization: this.getAuthHeader() } }
|
|
3221
|
+
);
|
|
3222
|
+
if (!res.ok) {
|
|
3223
|
+
let reason;
|
|
3224
|
+
try {
|
|
3225
|
+
const body = await res.json();
|
|
3226
|
+
if (body && typeof body.reason === "string") reason = body.reason;
|
|
3227
|
+
} catch (parseErr) {
|
|
3228
|
+
this.log({
|
|
3229
|
+
level: "debug",
|
|
3230
|
+
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`,
|
|
3231
|
+
message_id: messageId
|
|
3232
|
+
});
|
|
3233
|
+
}
|
|
3234
|
+
if (reason === "needs_reauth") {
|
|
3235
|
+
this.log({
|
|
3236
|
+
level: "error",
|
|
3237
|
+
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)`,
|
|
3238
|
+
message_id: messageId
|
|
3239
|
+
});
|
|
3240
|
+
return { needsReauth: true };
|
|
3241
|
+
}
|
|
3242
|
+
this.log({
|
|
3243
|
+
level: "error",
|
|
3244
|
+
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} returned HTTP ${res.status} \u2014 omitting this image (text turn proceeds)`,
|
|
3245
|
+
message_id: messageId
|
|
3246
|
+
});
|
|
3247
|
+
return null;
|
|
3248
|
+
}
|
|
3249
|
+
const buf = await res.arrayBuffer();
|
|
3250
|
+
const base64 = Buffer.from(buf).toString("base64");
|
|
3251
|
+
const dataMime = cleanImageMime(res.headers.get("content-type")) || mime;
|
|
3252
|
+
return `data:${dataMime};base64,${base64}`;
|
|
3253
|
+
} catch (err) {
|
|
3254
|
+
this.log({
|
|
3255
|
+
level: "error",
|
|
3256
|
+
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)}`,
|
|
3257
|
+
message_id: messageId
|
|
3258
|
+
});
|
|
3259
|
+
return null;
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
/**
|
|
3263
|
+
* On any skipped/failed image, post an in-thread note to Evident over the
|
|
3264
|
+
* EXISTING combinedAuth callback surface — the CLI NEVER posts to Slack directly.
|
|
3265
|
+
* Evident routes the note to source via `conversation.deliver`.
|
|
3266
|
+
*
|
|
3267
|
+
* The `POST .../messages/:id/signal` route accepts `attachments_skipped` (in
|
|
3268
|
+
* `messageSignalSchema`) and turns it into an in-thread note delivered through
|
|
3269
|
+
* `conversation.deliver` (e.g. "N image(s) couldn't be forwarded"), so the note
|
|
3270
|
+
* reaches the channel.
|
|
3271
|
+
*
|
|
3272
|
+
* Fire-and-forget: never throws into the send/tick (logs its own failure).
|
|
3273
|
+
*/
|
|
3274
|
+
signalAttachmentsSkipped(conversationId, messageId, outcomes, capabilityUnknown) {
|
|
3275
|
+
const skipped = outcomes.filter((o) => o.status === "skipped").length;
|
|
3276
|
+
const failed = outcomes.filter((o) => o.status === "failed").length;
|
|
3277
|
+
if (skipped === 0 && failed === 0) return;
|
|
3278
|
+
if (this.attachmentsSkippedSignalled.has(messageId)) return;
|
|
3279
|
+
this.attachmentsSkippedSignalled.add(messageId);
|
|
3280
|
+
const skippedReason = capabilityUnknown ? "unknown" : "unsupported";
|
|
3281
|
+
const failedReason = outcomes.some(
|
|
3282
|
+
(o) => o.status === "failed" && o.reason === "needs_reauth"
|
|
3283
|
+
) ? "needs_reauth" : void 0;
|
|
3284
|
+
this.log({
|
|
3285
|
+
level: "info",
|
|
3286
|
+
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`,
|
|
3287
|
+
conversation_id: conversationId,
|
|
3288
|
+
message_id: messageId
|
|
3289
|
+
});
|
|
3290
|
+
void this.postSignal(conversationId, messageId, "attachments_skipped", {
|
|
3291
|
+
skipped,
|
|
3292
|
+
failed,
|
|
3293
|
+
...skipped > 0 ? { skipped_reason: skippedReason } : {},
|
|
3294
|
+
...failedReason ? { failed_reason: failedReason } : {}
|
|
3295
|
+
});
|
|
3296
|
+
}
|
|
2048
3297
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
2049
3298
|
registerInFlight(conv, sessionId, message, opencodeMessageId) {
|
|
2050
3299
|
let watcher = this.watchers.get(sessionId);
|
|
@@ -2054,7 +3303,9 @@ var ChannelDriver = class {
|
|
|
2054
3303
|
inFlight: /* @__PURE__ */ new Map(),
|
|
2055
3304
|
loop: null,
|
|
2056
3305
|
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2057
|
-
reportedPermissions: /* @__PURE__ */ new Set()
|
|
3306
|
+
reportedPermissions: /* @__PURE__ */ new Set(),
|
|
3307
|
+
lastGoodPollAt: this.now(),
|
|
3308
|
+
hadUsablePoll: false
|
|
2058
3309
|
};
|
|
2059
3310
|
this.watchers.set(sessionId, watcher);
|
|
2060
3311
|
}
|
|
@@ -2064,20 +3315,42 @@ var ChannelDriver = class {
|
|
|
2064
3315
|
opencodeMessageId,
|
|
2065
3316
|
message,
|
|
2066
3317
|
dispatchedAt: now,
|
|
3318
|
+
processingAnchorMs: now,
|
|
2067
3319
|
deadline: now + this.pausedMaxWaitMs,
|
|
2068
3320
|
started: false,
|
|
2069
3321
|
done: false,
|
|
2070
|
-
stuckReported: false
|
|
3322
|
+
stuckReported: false,
|
|
3323
|
+
lastAliveAt: 0,
|
|
3324
|
+
aliveInFlight: false,
|
|
3325
|
+
titleSynced: false,
|
|
3326
|
+
titleSyncInFlight: false,
|
|
3327
|
+
awaitingHumanLatched: false,
|
|
3328
|
+
pausedOnQuestion: false,
|
|
3329
|
+
pausedOnPermission: false,
|
|
3330
|
+
pausedClearConfirmed: false,
|
|
3331
|
+
pausedInFlight: false,
|
|
3332
|
+
deliveryDeadlineAnchored: false,
|
|
3333
|
+
b2PinnedSinceMs: 0,
|
|
3334
|
+
b2LastDescendantCheckMs: 0,
|
|
3335
|
+
b2AbandonedSignalled: false
|
|
2071
3336
|
});
|
|
2072
3337
|
}
|
|
2073
3338
|
/**
|
|
2074
3339
|
* Register a RE-ADOPTED `processing` message with its session watcher
|
|
2075
3340
|
* (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
|
|
2076
3341
|
* `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
|
|
2077
|
-
* `now
|
|
2078
|
-
* (10 min after `processed_at
|
|
2079
|
-
*
|
|
2080
|
-
*
|
|
3342
|
+
* `now`, so the paused/queued/unreachable cases settle on the same wall-clock a
|
|
3343
|
+
* fresh dispatch would (10 min after `processed_at`, not 10 min from now).
|
|
3344
|
+
*
|
|
3345
|
+
* This re-attaches into the SAME watcher, so the ADR-0047 progressing-vs-paused
|
|
3346
|
+
* give-up (`serviceInFlightMessage`) applies unchanged: a re-adopted turn
|
|
3347
|
+
* opencode reports ACTIVELY `running` is watched to completion (its liveness
|
|
3348
|
+
* heartbeat keeps the cron off its row), while a re-adopted turn that is paused
|
|
3349
|
+
* awaiting a human — or queued/unreachable — is still bounded by `deadline` and
|
|
3350
|
+
* handed to the cron. The old "the `deadline` must settle before the ~15-min
|
|
3351
|
+
* cron or they double-drive" reasoning is superseded: liveness now settles the
|
|
3352
|
+
* actively-running case; `deadline` settles the rest. `dispatchedAt` stays `now`
|
|
3353
|
+
* (only the appear-guard uses it).
|
|
2081
3354
|
*
|
|
2082
3355
|
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
2083
3356
|
* `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
|
|
@@ -2095,7 +3368,9 @@ var ChannelDriver = class {
|
|
|
2095
3368
|
inFlight: /* @__PURE__ */ new Map(),
|
|
2096
3369
|
loop: null,
|
|
2097
3370
|
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2098
|
-
reportedPermissions: /* @__PURE__ */ new Set()
|
|
3371
|
+
reportedPermissions: /* @__PURE__ */ new Set(),
|
|
3372
|
+
lastGoodPollAt: this.now(),
|
|
3373
|
+
hadUsablePoll: false
|
|
2099
3374
|
};
|
|
2100
3375
|
this.watchers.set(sessionId, watcher);
|
|
2101
3376
|
}
|
|
@@ -2104,6 +3379,10 @@ var ChannelDriver = class {
|
|
|
2104
3379
|
opencodeMessageId,
|
|
2105
3380
|
message,
|
|
2106
3381
|
dispatchedAt: this.now(),
|
|
3382
|
+
// Anchor the absolute-age ceiling to the SERVER-SIDE `processed_at` (the same
|
|
3383
|
+
// value seeding `deadline`), NOT `dispatchedAt` — so a re-adopted zombie's age
|
|
3384
|
+
// reflects the real turn duration and the ceiling fires on the ORIGINAL turn.
|
|
3385
|
+
processingAnchorMs: processedAtMs,
|
|
2107
3386
|
deadline: processedAtMs + this.pausedMaxWaitMs,
|
|
2108
3387
|
// The server row is ALREADY `processing`; do not re-fire markProcessing.
|
|
2109
3388
|
started: true,
|
|
@@ -2113,7 +3392,25 @@ var ChannelDriver = class {
|
|
|
2113
3392
|
// on `state === 'queued'` (turn produced no reply), not on `started`, so a
|
|
2114
3393
|
// re-adopted row left wedged in `queued` still emits the signal once
|
|
2115
3394
|
// (#210/#220 observability).
|
|
2116
|
-
stuckReported: false
|
|
3395
|
+
stuckReported: false,
|
|
3396
|
+
// Task 5.2: a re-adopted actively-running row re-attaches into the SAME
|
|
3397
|
+
// watcher and so hits the SAME actively-running heartbeat branch in
|
|
3398
|
+
// `serviceInFlightMessage` as a fresh dispatch — monitoring observes "runner
|
|
3399
|
+
// re-adopted and is confirming this row alive" via that `alive` heartbeat,
|
|
3400
|
+
// with no extra `re_adopted` signal needed (folds old WI-6).
|
|
3401
|
+
lastAliveAt: 0,
|
|
3402
|
+
aliveInFlight: false,
|
|
3403
|
+
titleSynced: false,
|
|
3404
|
+
titleSyncInFlight: false,
|
|
3405
|
+
awaitingHumanLatched: false,
|
|
3406
|
+
pausedOnQuestion: false,
|
|
3407
|
+
pausedOnPermission: false,
|
|
3408
|
+
pausedClearConfirmed: false,
|
|
3409
|
+
pausedInFlight: false,
|
|
3410
|
+
deliveryDeadlineAnchored: false,
|
|
3411
|
+
b2PinnedSinceMs: 0,
|
|
3412
|
+
b2LastDescendantCheckMs: 0,
|
|
3413
|
+
b2AbandonedSignalled: false
|
|
2117
3414
|
});
|
|
2118
3415
|
}
|
|
2119
3416
|
/**
|
|
@@ -2164,12 +3461,30 @@ var ChannelDriver = class {
|
|
|
2164
3461
|
messages = Array.isArray(body) ? body : null;
|
|
2165
3462
|
}
|
|
2166
3463
|
} catch {
|
|
2167
|
-
continue;
|
|
2168
3464
|
}
|
|
3465
|
+
if (messages != null && messages.length > 0) {
|
|
3466
|
+
watcher.lastGoodPollAt = this.now();
|
|
3467
|
+
watcher.hadUsablePoll = true;
|
|
3468
|
+
} else {
|
|
3469
|
+
const emptyButReachable = messages != null;
|
|
3470
|
+
const graceApplies = !emptyButReachable || watcher.hadUsablePoll;
|
|
3471
|
+
if (graceApplies && this.now() - watcher.lastGoodPollAt < POLL_MISS_GRACE_MS) {
|
|
3472
|
+
continue;
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
const { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk } = await this.pollInteractions(sessionId, watcher, messages);
|
|
2169
3476
|
for (const inFlight of [...watcher.inFlight.values()]) {
|
|
2170
|
-
await this.serviceInFlightMessage(
|
|
3477
|
+
await this.serviceInFlightMessage(
|
|
3478
|
+
sessionId,
|
|
3479
|
+
watcher,
|
|
3480
|
+
inFlight,
|
|
3481
|
+
messages,
|
|
3482
|
+
openQuestions,
|
|
3483
|
+
openPermissions,
|
|
3484
|
+
questionsPolledOk,
|
|
3485
|
+
permissionsPolledOk
|
|
3486
|
+
);
|
|
2171
3487
|
}
|
|
2172
|
-
await this.pollInteractions(sessionId, watcher, messages);
|
|
2173
3488
|
}
|
|
2174
3489
|
} catch (err) {
|
|
2175
3490
|
if (err instanceof ChannelAuthError) {
|
|
@@ -2191,28 +3506,55 @@ var ChannelDriver = class {
|
|
|
2191
3506
|
});
|
|
2192
3507
|
}
|
|
2193
3508
|
}
|
|
3509
|
+
/**
|
|
3510
|
+
* On FIRST observing a terminal (done/failed) state, ensure the delivery
|
|
3511
|
+
* (markDone/markFailed) transient-retry path has a real window. A long
|
|
3512
|
+
* ACTIVELY-running turn is kept past its original `deadline`, so by completion
|
|
3513
|
+
* `now >= deadline` already holds and the retry bound below would fire on the
|
|
3514
|
+
* first transient PATCH failure — dropping the message before its reply lands
|
|
3515
|
+
* (Bugbot "Stale deadline aborts long-turn delivery"). Re-anchor once (latched)
|
|
3516
|
+
* to a fresh `pausedMaxWaitMs` window; only extend if the current deadline is at
|
|
3517
|
+
* or past now, so a still-ample window is left untouched.
|
|
3518
|
+
*/
|
|
3519
|
+
anchorDeliveryDeadline(inFlight) {
|
|
3520
|
+
if (inFlight.deliveryDeadlineAnchored) return;
|
|
3521
|
+
inFlight.deliveryDeadlineAnchored = true;
|
|
3522
|
+
if (this.now() >= inFlight.deadline) {
|
|
3523
|
+
inFlight.deadline = this.now() + this.pausedMaxWaitMs;
|
|
3524
|
+
}
|
|
3525
|
+
}
|
|
2194
3526
|
/**
|
|
2195
3527
|
* Drive ONE in-flight message's lifecycle from the tick's message snapshot.
|
|
2196
3528
|
* Fires markProcessing on queued→running and markDone on done (each once),
|
|
2197
3529
|
* applies the idle-path re-dispatch guard, and removes the message from the
|
|
2198
3530
|
* in-flight set on completion or timeout.
|
|
2199
3531
|
*/
|
|
2200
|
-
async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
|
|
3532
|
+
async serviceInFlightMessage(sessionId, watcher, inFlight, messages, openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk) {
|
|
2201
3533
|
const conv = watcher.conv;
|
|
2202
3534
|
const state = messageRunState(messages, inFlight.opencodeMessageId);
|
|
3535
|
+
const id = inFlight.evidentMessageId;
|
|
3536
|
+
if (openQuestions.has(id)) inFlight.pausedOnQuestion = true;
|
|
3537
|
+
else if (questionsPolledOk) inFlight.pausedOnQuestion = false;
|
|
3538
|
+
if (openPermissions.has(id)) inFlight.pausedOnPermission = true;
|
|
3539
|
+
else if (permissionsPolledOk) inFlight.pausedOnPermission = false;
|
|
3540
|
+
const observedOpen = openQuestions.has(id) || openPermissions.has(id);
|
|
3541
|
+
const latchedPaused = inFlight.pausedOnQuestion || inFlight.pausedOnPermission;
|
|
3542
|
+
const awaitingHuman = observedOpen || latchedPaused;
|
|
2203
3543
|
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
3544
|
+
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
2204
3545
|
let claimed;
|
|
2205
3546
|
try {
|
|
2206
3547
|
claimed = await this.markProcessing(
|
|
2207
3548
|
conv.id,
|
|
2208
3549
|
inFlight.evidentMessageId,
|
|
2209
3550
|
sessionId,
|
|
2210
|
-
inFlight.opencodeMessageId
|
|
3551
|
+
inFlight.opencodeMessageId,
|
|
3552
|
+
title
|
|
2211
3553
|
);
|
|
2212
3554
|
} catch (err) {
|
|
2213
3555
|
if (err instanceof ChannelAuthError) throw err;
|
|
2214
3556
|
this.log({
|
|
2215
|
-
level: "
|
|
3557
|
+
level: "warn",
|
|
2216
3558
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2217
3559
|
conversation_id: conv.id,
|
|
2218
3560
|
message_id: inFlight.evidentMessageId
|
|
@@ -2222,7 +3564,7 @@ var ChannelDriver = class {
|
|
|
2222
3564
|
inFlight.started = true;
|
|
2223
3565
|
if (!claimed) {
|
|
2224
3566
|
this.log({
|
|
2225
|
-
level: "
|
|
3567
|
+
level: "debug",
|
|
2226
3568
|
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
|
|
2227
3569
|
conversation_id: conv.id,
|
|
2228
3570
|
message_id: inFlight.evidentMessageId
|
|
@@ -2230,56 +3572,11 @@ var ChannelDriver = class {
|
|
|
2230
3572
|
}
|
|
2231
3573
|
}
|
|
2232
3574
|
if (state === "done") {
|
|
2233
|
-
|
|
2234
|
-
this.log({
|
|
2235
|
-
level: "info",
|
|
2236
|
-
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
|
|
2237
|
-
conversation_id: conv.id,
|
|
2238
|
-
message_id: inFlight.evidentMessageId
|
|
2239
|
-
});
|
|
2240
|
-
try {
|
|
2241
|
-
await this.markDone(
|
|
2242
|
-
conv.id,
|
|
2243
|
-
inFlight.evidentMessageId,
|
|
2244
|
-
sessionId,
|
|
2245
|
-
inFlight.opencodeMessageId
|
|
2246
|
-
);
|
|
2247
|
-
} catch (err) {
|
|
2248
|
-
if (err instanceof ChannelAuthError) throw err;
|
|
2249
|
-
if (err instanceof ChannelTerminalError) {
|
|
2250
|
-
this.log({
|
|
2251
|
-
level: "error",
|
|
2252
|
-
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
|
-
conversation_id: conv.id,
|
|
2254
|
-
message_id: inFlight.evidentMessageId
|
|
2255
|
-
});
|
|
2256
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2257
|
-
return;
|
|
2258
|
-
}
|
|
2259
|
-
if (this.now() >= inFlight.deadline) {
|
|
2260
|
-
this.log({
|
|
2261
|
-
level: "error",
|
|
2262
|
-
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
|
-
conversation_id: conv.id,
|
|
2264
|
-
message_id: inFlight.evidentMessageId
|
|
2265
|
-
});
|
|
2266
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2267
|
-
return;
|
|
2268
|
-
}
|
|
2269
|
-
this.log({
|
|
2270
|
-
level: "error",
|
|
2271
|
-
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2272
|
-
conversation_id: conv.id,
|
|
2273
|
-
message_id: inFlight.evidentMessageId
|
|
2274
|
-
});
|
|
2275
|
-
return;
|
|
2276
|
-
}
|
|
2277
|
-
inFlight.done = true;
|
|
2278
|
-
}
|
|
2279
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3575
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
2280
3576
|
return;
|
|
2281
3577
|
}
|
|
2282
3578
|
if (state === "failed") {
|
|
3579
|
+
this.anchorDeliveryDeadline(inFlight);
|
|
2283
3580
|
if (!inFlight.done) {
|
|
2284
3581
|
const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
|
|
2285
3582
|
this.log({
|
|
@@ -2288,13 +3585,14 @@ var ChannelDriver = class {
|
|
|
2288
3585
|
conversation_id: conv.id,
|
|
2289
3586
|
message_id: inFlight.evidentMessageId
|
|
2290
3587
|
});
|
|
3588
|
+
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
2291
3589
|
try {
|
|
2292
|
-
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
|
|
3590
|
+
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2, usage);
|
|
2293
3591
|
} catch (err) {
|
|
2294
3592
|
if (err instanceof ChannelAuthError) throw err;
|
|
2295
3593
|
if (err instanceof ChannelTerminalError) {
|
|
2296
3594
|
this.log({
|
|
2297
|
-
level: "
|
|
3595
|
+
level: "warn",
|
|
2298
3596
|
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
3597
|
conversation_id: conv.id,
|
|
2300
3598
|
message_id: inFlight.evidentMessageId
|
|
@@ -2304,7 +3602,7 @@ var ChannelDriver = class {
|
|
|
2304
3602
|
}
|
|
2305
3603
|
if (this.now() >= inFlight.deadline) {
|
|
2306
3604
|
this.log({
|
|
2307
|
-
level: "
|
|
3605
|
+
level: "warn",
|
|
2308
3606
|
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
3607
|
conversation_id: conv.id,
|
|
2310
3608
|
message_id: inFlight.evidentMessageId
|
|
@@ -2313,7 +3611,7 @@ var ChannelDriver = class {
|
|
|
2313
3611
|
return;
|
|
2314
3612
|
}
|
|
2315
3613
|
this.log({
|
|
2316
|
-
level: "
|
|
3614
|
+
level: "warn",
|
|
2317
3615
|
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2318
3616
|
conversation_id: conv.id,
|
|
2319
3617
|
message_id: inFlight.evidentMessageId
|
|
@@ -2333,9 +3631,103 @@ var ChannelDriver = class {
|
|
|
2333
3631
|
stuck_for_ms: this.now() - inFlight.dispatchedAt
|
|
2334
3632
|
});
|
|
2335
3633
|
}
|
|
2336
|
-
|
|
3634
|
+
const activelyRunning = state === "running" && !awaitingHuman;
|
|
3635
|
+
const pinnedNow = activelyRunning && isPreamblePinnedRunning(messages, inFlight.opencodeMessageId);
|
|
3636
|
+
const snapshotReadable = messages != null && messages.length > 0;
|
|
3637
|
+
if (!pinnedNow) {
|
|
3638
|
+
if (snapshotReadable) {
|
|
3639
|
+
inFlight.b2PinnedSinceMs = 0;
|
|
3640
|
+
inFlight.b2LastDescendantCheckMs = 0;
|
|
3641
|
+
inFlight.b2AbandonedSignalled = false;
|
|
3642
|
+
}
|
|
3643
|
+
} else {
|
|
3644
|
+
if (inFlight.b2AbandonedSignalled) {
|
|
3645
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
3646
|
+
return;
|
|
3647
|
+
}
|
|
3648
|
+
if (inFlight.b2PinnedSinceMs === 0) inFlight.b2PinnedSinceMs = this.now();
|
|
3649
|
+
const pinnedForMs = this.now() - inFlight.b2PinnedSinceMs;
|
|
3650
|
+
if (pinnedForMs >= B2_ABANDONMENT_MIN_PINNED_MS && this.now() - inFlight.b2LastDescendantCheckMs >= B2_ABANDONMENT_RECHECK_MS) {
|
|
3651
|
+
inFlight.b2LastDescendantCheckMs = this.now();
|
|
3652
|
+
const descendantOngoing = await this.isAnyDescendantSessionOngoing(sessionId);
|
|
3653
|
+
if (isB2AbandonmentConfirmed({
|
|
3654
|
+
pinnedForMs,
|
|
3655
|
+
minPinnedMs: B2_ABANDONMENT_MIN_PINNED_MS,
|
|
3656
|
+
descendantOngoing
|
|
3657
|
+
})) {
|
|
3658
|
+
inFlight.b2AbandonedSignalled = true;
|
|
3659
|
+
this.log({
|
|
3660
|
+
level: "warn",
|
|
3661
|
+
message: `Message ${id.slice(0, 8)} b2-pinned for ${Math.round(pinnedForMs / 1e3)}s with no ongoing descendant sub-agent session (status-map confirmed) \u2014 treating the delegated/tool turn as abandoned, resolving done`,
|
|
3662
|
+
conversation_id: conv.id,
|
|
3663
|
+
message_id: id
|
|
3664
|
+
});
|
|
3665
|
+
void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
|
|
3666
|
+
watched_for_ms: pinnedForMs
|
|
3667
|
+
});
|
|
3668
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
3669
|
+
return;
|
|
3670
|
+
}
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
|
|
2337
3674
|
this.log({
|
|
2338
|
-
level: "
|
|
3675
|
+
level: "warn",
|
|
3676
|
+
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`,
|
|
3677
|
+
conversation_id: conv.id,
|
|
3678
|
+
message_id: inFlight.evidentMessageId
|
|
3679
|
+
});
|
|
3680
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
|
|
3681
|
+
watched_for_ms: this.now() - inFlight.processingAnchorMs
|
|
3682
|
+
});
|
|
3683
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3684
|
+
return;
|
|
3685
|
+
}
|
|
3686
|
+
if (activelyRunning && !inFlight.awaitingHumanLatched && !inFlight.aliveInFlight && this.now() - inFlight.lastAliveAt >= HEARTBEAT_MS) {
|
|
3687
|
+
inFlight.aliveInFlight = true;
|
|
3688
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "alive").then((ok) => {
|
|
3689
|
+
inFlight.aliveInFlight = false;
|
|
3690
|
+
if (ok) inFlight.lastAliveAt = this.now();
|
|
3691
|
+
});
|
|
3692
|
+
if (!inFlight.titleSynced && !inFlight.titleSyncInFlight) {
|
|
3693
|
+
inFlight.titleSyncInFlight = true;
|
|
3694
|
+
void this.resolveSessionTitle(sessionId, conv.id).then(async (title) => {
|
|
3695
|
+
if (!title) {
|
|
3696
|
+
inFlight.titleSyncInFlight = false;
|
|
3697
|
+
return;
|
|
3698
|
+
}
|
|
3699
|
+
const ok = await this.patchConversationTitle(conv.id, title);
|
|
3700
|
+
inFlight.titleSyncInFlight = false;
|
|
3701
|
+
if (ok) inFlight.titleSynced = true;
|
|
3702
|
+
});
|
|
3703
|
+
}
|
|
3704
|
+
}
|
|
3705
|
+
if (awaitingHuman) {
|
|
3706
|
+
if (!inFlight.awaitingHumanLatched) {
|
|
3707
|
+
inFlight.deadline = this.now() + this.pausedMaxWaitMs;
|
|
3708
|
+
inFlight.awaitingHumanLatched = true;
|
|
3709
|
+
}
|
|
3710
|
+
if (!inFlight.pausedClearConfirmed && !inFlight.pausedInFlight) {
|
|
3711
|
+
inFlight.pausedInFlight = true;
|
|
3712
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "paused").then((ok) => {
|
|
3713
|
+
inFlight.pausedInFlight = false;
|
|
3714
|
+
if (ok && inFlight.awaitingHumanLatched) inFlight.pausedClearConfirmed = true;
|
|
3715
|
+
});
|
|
3716
|
+
}
|
|
3717
|
+
} else if (inFlight.awaitingHumanLatched) {
|
|
3718
|
+
inFlight.awaitingHumanLatched = false;
|
|
3719
|
+
inFlight.pausedOnQuestion = false;
|
|
3720
|
+
inFlight.pausedOnPermission = false;
|
|
3721
|
+
inFlight.pausedClearConfirmed = false;
|
|
3722
|
+
}
|
|
3723
|
+
const siblingPaused = (sib) => openQuestions.has(sib.evidentMessageId) || openPermissions.has(sib.evidentMessageId) || sib.awaitingHumanLatched || sib.pausedOnQuestion || sib.pausedOnPermission;
|
|
3724
|
+
const hasActivelyRunningSibling = [...watcher.inFlight.values()].some(
|
|
3725
|
+
(sib) => sib.evidentMessageId !== inFlight.evidentMessageId && messageRunState(messages, sib.opencodeMessageId) === "running" && !siblingPaused(sib)
|
|
3726
|
+
);
|
|
3727
|
+
const queuedBehindRunningSibling = state === "queued" && hasActivelyRunningSibling;
|
|
3728
|
+
if (!activelyRunning && !queuedBehindRunningSibling && this.now() >= inFlight.deadline) {
|
|
3729
|
+
this.log({
|
|
3730
|
+
level: "debug",
|
|
2339
3731
|
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} did not complete within the watch window \u2014 leaving for the cron safety net`,
|
|
2340
3732
|
conversation_id: conv.id,
|
|
2341
3733
|
message_id: inFlight.evidentMessageId
|
|
@@ -2346,9 +3738,71 @@ var ChannelDriver = class {
|
|
|
2346
3738
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2347
3739
|
}
|
|
2348
3740
|
}
|
|
2349
|
-
|
|
3741
|
+
/**
|
|
3742
|
+
* Settle a message whose run-state has resolved `'done'` — extracted verbatim
|
|
3743
|
+
* (pure refactor, no behavior change) from `serviceInFlightMessage`'s former
|
|
3744
|
+
* inline `state === 'done'` branch body, so a SECOND caller (the #721
|
|
3745
|
+
* b2-abandonment resolution) can reach the exact same completion behavior
|
|
3746
|
+
* (delivery-deadline anchoring, title resolution, usage extraction, and
|
|
3747
|
+
* `markDone`'s auth/terminal/transient-retry discipline) without duplicating it
|
|
3748
|
+
* and risking the two copies silently drifting apart.
|
|
3749
|
+
*/
|
|
3750
|
+
async settleMessageDone(sessionId, watcher, inFlight, messages) {
|
|
3751
|
+
const conv = watcher.conv;
|
|
3752
|
+
this.anchorDeliveryDeadline(inFlight);
|
|
3753
|
+
if (!inFlight.done) {
|
|
3754
|
+
this.log({
|
|
3755
|
+
level: "info",
|
|
3756
|
+
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
|
|
3757
|
+
conversation_id: conv.id,
|
|
3758
|
+
message_id: inFlight.evidentMessageId
|
|
3759
|
+
});
|
|
3760
|
+
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
3761
|
+
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
3762
|
+
try {
|
|
3763
|
+
await this.markDone(
|
|
3764
|
+
conv.id,
|
|
3765
|
+
inFlight.evidentMessageId,
|
|
3766
|
+
sessionId,
|
|
3767
|
+
inFlight.opencodeMessageId,
|
|
3768
|
+
title,
|
|
3769
|
+
usage
|
|
3770
|
+
);
|
|
3771
|
+
} catch (err) {
|
|
3772
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
3773
|
+
if (err instanceof ChannelTerminalError) {
|
|
3774
|
+
this.log({
|
|
3775
|
+
level: "warn",
|
|
3776
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
3777
|
+
conversation_id: conv.id,
|
|
3778
|
+
message_id: inFlight.evidentMessageId
|
|
3779
|
+
});
|
|
3780
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3781
|
+
return;
|
|
3782
|
+
}
|
|
3783
|
+
if (this.now() >= inFlight.deadline) {
|
|
3784
|
+
this.log({
|
|
3785
|
+
level: "warn",
|
|
3786
|
+
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)}`,
|
|
3787
|
+
conversation_id: conv.id,
|
|
3788
|
+
message_id: inFlight.evidentMessageId
|
|
3789
|
+
});
|
|
3790
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3791
|
+
return;
|
|
3792
|
+
}
|
|
3793
|
+
this.log({
|
|
3794
|
+
level: "warn",
|
|
3795
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
3796
|
+
conversation_id: conv.id,
|
|
3797
|
+
message_id: inFlight.evidentMessageId
|
|
3798
|
+
});
|
|
3799
|
+
return;
|
|
3800
|
+
}
|
|
3801
|
+
inFlight.done = true;
|
|
3802
|
+
}
|
|
3803
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3804
|
+
}
|
|
2350
3805
|
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
2351
|
-
// -------------------------------------------------------------------------
|
|
2352
3806
|
/**
|
|
2353
3807
|
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
2354
3808
|
*
|
|
@@ -2365,15 +3819,20 @@ var ChannelDriver = class {
|
|
|
2365
3819
|
*/
|
|
2366
3820
|
async readoptProcessing() {
|
|
2367
3821
|
const rows = await this.getProcessingMessages();
|
|
2368
|
-
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
|
|
3822
|
+
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0 || this.readoptPollUnresolvedSignalled.size > 0) {
|
|
2369
3823
|
const stillProcessing = new Set(rows.map((r) => r.id));
|
|
2370
|
-
for (const id of [
|
|
3824
|
+
for (const id of [
|
|
3825
|
+
...this.dontRedispatch,
|
|
3826
|
+
...this.doneUndeliverable,
|
|
3827
|
+
...this.readoptPollUnresolvedSignalled
|
|
3828
|
+
]) {
|
|
2371
3829
|
if (!stillProcessing.has(id)) {
|
|
2372
3830
|
const cleared = this.dontRedispatch.delete(id);
|
|
2373
3831
|
const clearedUndeliverable = this.doneUndeliverable.delete(id);
|
|
3832
|
+
this.readoptPollUnresolvedSignalled.delete(id);
|
|
2374
3833
|
if (cleared || clearedUndeliverable) {
|
|
2375
3834
|
this.log({
|
|
2376
|
-
level: "
|
|
3835
|
+
level: "debug",
|
|
2377
3836
|
message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
|
|
2378
3837
|
message_id: id
|
|
2379
3838
|
});
|
|
@@ -2386,7 +3845,7 @@ var ChannelDriver = class {
|
|
|
2386
3845
|
for (const row of rows) {
|
|
2387
3846
|
if (!row.opencode_session_id) {
|
|
2388
3847
|
this.log({
|
|
2389
|
-
level: "
|
|
3848
|
+
level: "warn",
|
|
2390
3849
|
message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
|
|
2391
3850
|
conversation_id: row.conversation_id,
|
|
2392
3851
|
message_id: row.id
|
|
@@ -2403,7 +3862,7 @@ var ChannelDriver = class {
|
|
|
2403
3862
|
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
|
|
2404
3863
|
if (!res.ok) {
|
|
2405
3864
|
this.log({
|
|
2406
|
-
level: "
|
|
3865
|
+
level: "warn",
|
|
2407
3866
|
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
|
|
2408
3867
|
});
|
|
2409
3868
|
continue;
|
|
@@ -2411,7 +3870,7 @@ var ChannelDriver = class {
|
|
|
2411
3870
|
const body = await res.json();
|
|
2412
3871
|
if (!Array.isArray(body)) {
|
|
2413
3872
|
this.log({
|
|
2414
|
-
level: "
|
|
3873
|
+
level: "warn",
|
|
2415
3874
|
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
|
|
2416
3875
|
});
|
|
2417
3876
|
continue;
|
|
@@ -2419,13 +3878,15 @@ var ChannelDriver = class {
|
|
|
2419
3878
|
messages = body;
|
|
2420
3879
|
} catch (err) {
|
|
2421
3880
|
this.log({
|
|
2422
|
-
level: "
|
|
3881
|
+
level: "warn",
|
|
2423
3882
|
message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
|
|
2424
3883
|
});
|
|
2425
3884
|
continue;
|
|
2426
3885
|
}
|
|
3886
|
+
const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
|
|
3887
|
+
const sessionOngoing = anyUntracked ? await isSessionOngoing(this.port, sessionId) : null;
|
|
2427
3888
|
for (const row of sessionRows) {
|
|
2428
|
-
await this.readoptOne(sessionId, row, messages);
|
|
3889
|
+
await this.readoptOne(sessionId, row, messages, sessionOngoing);
|
|
2429
3890
|
}
|
|
2430
3891
|
}
|
|
2431
3892
|
}
|
|
@@ -2447,10 +3908,10 @@ var ChannelDriver = class {
|
|
|
2447
3908
|
*
|
|
2448
3909
|
* Only `ChannelAuthError` propagates.
|
|
2449
3910
|
*/
|
|
2450
|
-
async readoptOne(sessionId, row, messages) {
|
|
3911
|
+
async readoptOne(sessionId, row, messages, sessionOngoing) {
|
|
2451
3912
|
if (this.isTracked(sessionId, row.id)) {
|
|
2452
3913
|
this.log({
|
|
2453
|
-
level: "
|
|
3914
|
+
level: "debug",
|
|
2454
3915
|
message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
|
|
2455
3916
|
conversation_id: row.conversation_id,
|
|
2456
3917
|
message_id: row.id
|
|
@@ -2462,7 +3923,7 @@ var ChannelDriver = class {
|
|
|
2462
3923
|
if (state === "done") {
|
|
2463
3924
|
if (this.doneUndeliverable.has(row.id)) {
|
|
2464
3925
|
this.log({
|
|
2465
|
-
level: "
|
|
3926
|
+
level: "debug",
|
|
2466
3927
|
message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
|
|
2467
3928
|
conversation_id: row.conversation_id,
|
|
2468
3929
|
message_id: row.id
|
|
@@ -2476,21 +3937,24 @@ var ChannelDriver = class {
|
|
|
2476
3937
|
message_id: row.id
|
|
2477
3938
|
});
|
|
2478
3939
|
try {
|
|
2479
|
-
await this.
|
|
3940
|
+
const title = await this.resolveSessionTitle(sessionId, row.conversation_id);
|
|
3941
|
+
const usage = messageUsage(messages, ocId ?? "");
|
|
3942
|
+
await this.markDone(row.conversation_id, row.id, sessionId, ocId, title, usage);
|
|
2480
3943
|
} catch (err) {
|
|
2481
3944
|
if (err instanceof ChannelAuthError) throw err;
|
|
2482
3945
|
if (err instanceof ChannelTerminalError) {
|
|
2483
3946
|
this.doneUndeliverable.add(row.id);
|
|
2484
3947
|
this.log({
|
|
2485
|
-
level: "
|
|
3948
|
+
level: "warn",
|
|
2486
3949
|
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
3950
|
conversation_id: row.conversation_id,
|
|
2488
3951
|
message_id: row.id
|
|
2489
3952
|
});
|
|
3953
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
|
|
2490
3954
|
return;
|
|
2491
3955
|
}
|
|
2492
3956
|
this.log({
|
|
2493
|
-
level: "
|
|
3957
|
+
level: "warn",
|
|
2494
3958
|
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
3959
|
conversation_id: row.conversation_id,
|
|
2496
3960
|
message_id: row.id
|
|
@@ -2498,10 +3962,12 @@ var ChannelDriver = class {
|
|
|
2498
3962
|
return;
|
|
2499
3963
|
}
|
|
2500
3964
|
this.dontRedispatch.delete(row.id);
|
|
3965
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_done");
|
|
2501
3966
|
return;
|
|
2502
3967
|
}
|
|
2503
3968
|
if (state === "failed") {
|
|
2504
3969
|
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
3970
|
+
const usage = messageUsage(messages, ocId ?? "");
|
|
2505
3971
|
this.log({
|
|
2506
3972
|
level: "error",
|
|
2507
3973
|
message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
@@ -2509,38 +3975,105 @@ var ChannelDriver = class {
|
|
|
2509
3975
|
message_id: row.id
|
|
2510
3976
|
});
|
|
2511
3977
|
try {
|
|
2512
|
-
await this.markFailed(row.conversation_id, row.id, sessionId, error2);
|
|
3978
|
+
await this.markFailed(row.conversation_id, row.id, sessionId, error2, usage);
|
|
2513
3979
|
} catch (err) {
|
|
2514
3980
|
if (err instanceof ChannelAuthError) throw err;
|
|
2515
3981
|
if (err instanceof ChannelTerminalError) {
|
|
2516
3982
|
this.doneUndeliverable.add(row.id);
|
|
2517
3983
|
this.log({
|
|
2518
|
-
level: "
|
|
3984
|
+
level: "warn",
|
|
2519
3985
|
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
3986
|
conversation_id: row.conversation_id,
|
|
2521
3987
|
message_id: row.id
|
|
2522
3988
|
});
|
|
3989
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_undeliverable");
|
|
2523
3990
|
return;
|
|
2524
3991
|
}
|
|
2525
3992
|
this.log({
|
|
2526
|
-
level: "
|
|
2527
|
-
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
3993
|
+
level: "warn",
|
|
3994
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
3995
|
+
conversation_id: row.conversation_id,
|
|
3996
|
+
message_id: row.id
|
|
3997
|
+
});
|
|
3998
|
+
return;
|
|
3999
|
+
}
|
|
4000
|
+
this.dontRedispatch.delete(row.id);
|
|
4001
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_failed");
|
|
4002
|
+
return;
|
|
4003
|
+
}
|
|
4004
|
+
if (this.dontRedispatch.has(row.id)) {
|
|
4005
|
+
this.log({
|
|
4006
|
+
level: "debug",
|
|
4007
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
|
|
4008
|
+
conversation_id: row.conversation_id,
|
|
4009
|
+
message_id: row.id
|
|
4010
|
+
});
|
|
4011
|
+
return;
|
|
4012
|
+
}
|
|
4013
|
+
let statusReadableOngoing = null;
|
|
4014
|
+
if (state === "running" && ocId) {
|
|
4015
|
+
const reply = findLastAssistantReplyFor(messages, ocId);
|
|
4016
|
+
const shape = this.replyCompletionShape(reply);
|
|
4017
|
+
const ongoing = sessionOngoing;
|
|
4018
|
+
statusReadableOngoing = ongoing;
|
|
4019
|
+
if (ongoing === false) {
|
|
4020
|
+
this.log({
|
|
4021
|
+
level: "info",
|
|
4022
|
+
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)`,
|
|
4023
|
+
conversation_id: row.conversation_id,
|
|
4024
|
+
message_id: row.id
|
|
4025
|
+
});
|
|
4026
|
+
await this.forceReadoptRun(sessionId, row);
|
|
4027
|
+
return;
|
|
4028
|
+
}
|
|
4029
|
+
if (ongoing === true) {
|
|
4030
|
+
this.log({
|
|
4031
|
+
level: "debug",
|
|
4032
|
+
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)`,
|
|
4033
|
+
conversation_id: row.conversation_id,
|
|
4034
|
+
message_id: row.id
|
|
4035
|
+
});
|
|
4036
|
+
} else {
|
|
4037
|
+
if (shape === "b1") {
|
|
4038
|
+
this.log({
|
|
4039
|
+
level: "debug",
|
|
4040
|
+
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`,
|
|
4041
|
+
conversation_id: row.conversation_id,
|
|
4042
|
+
message_id: row.id
|
|
4043
|
+
});
|
|
4044
|
+
if (!this.readoptPollUnresolvedSignalled.has(row.id)) {
|
|
4045
|
+
this.readoptPollUnresolvedSignalled.add(row.id);
|
|
4046
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_poll_unresolved");
|
|
4047
|
+
}
|
|
4048
|
+
return;
|
|
4049
|
+
}
|
|
4050
|
+
this.log({
|
|
4051
|
+
level: "debug",
|
|
4052
|
+
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`,
|
|
4053
|
+
conversation_id: row.conversation_id,
|
|
4054
|
+
message_id: row.id
|
|
4055
|
+
});
|
|
4056
|
+
}
|
|
4057
|
+
}
|
|
4058
|
+
if (statusReadableOngoing === null && state === "running" && ocId && isPreamblePinnedRunning(messages, ocId)) {
|
|
4059
|
+
const descendantAlive = await this.isAnyDescendantSessionAlive(sessionId);
|
|
4060
|
+
if (descendantAlive === true) {
|
|
4061
|
+
this.log({
|
|
4062
|
+
level: "debug",
|
|
4063
|
+
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)`,
|
|
4064
|
+
conversation_id: row.conversation_id,
|
|
4065
|
+
message_id: row.id
|
|
4066
|
+
});
|
|
4067
|
+
} else {
|
|
4068
|
+
this.log({
|
|
4069
|
+
level: "info",
|
|
4070
|
+
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)" : ""}`,
|
|
2528
4071
|
conversation_id: row.conversation_id,
|
|
2529
4072
|
message_id: row.id
|
|
2530
4073
|
});
|
|
4074
|
+
await this.forceReadoptRun(sessionId, row);
|
|
2531
4075
|
return;
|
|
2532
4076
|
}
|
|
2533
|
-
this.dontRedispatch.delete(row.id);
|
|
2534
|
-
return;
|
|
2535
|
-
}
|
|
2536
|
-
if (this.dontRedispatch.has(row.id)) {
|
|
2537
|
-
this.log({
|
|
2538
|
-
level: "info",
|
|
2539
|
-
message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
|
|
2540
|
-
conversation_id: row.conversation_id,
|
|
2541
|
-
message_id: row.id
|
|
2542
|
-
});
|
|
2543
|
-
return;
|
|
2544
4077
|
}
|
|
2545
4078
|
if ((state === "running" || state === "queued") && ocId) {
|
|
2546
4079
|
const conv = this.convForRow(sessionId, row);
|
|
@@ -2550,11 +4083,12 @@ var ChannelDriver = class {
|
|
|
2550
4083
|
this.readopted.add(row.id);
|
|
2551
4084
|
this.ensureWatcherRunning(sessionId);
|
|
2552
4085
|
this.log({
|
|
2553
|
-
level: "
|
|
4086
|
+
level: "debug",
|
|
2554
4087
|
message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
|
|
2555
4088
|
conversation_id: row.conversation_id,
|
|
2556
4089
|
message_id: row.id
|
|
2557
4090
|
});
|
|
4091
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_reattached");
|
|
2558
4092
|
return;
|
|
2559
4093
|
}
|
|
2560
4094
|
await this.forceReadoptRun(sessionId, row);
|
|
@@ -2583,7 +4117,7 @@ var ChannelDriver = class {
|
|
|
2583
4117
|
async forceReadoptRun(sessionId, row) {
|
|
2584
4118
|
if (this.stopped) {
|
|
2585
4119
|
this.log({
|
|
2586
|
-
level: "
|
|
4120
|
+
level: "debug",
|
|
2587
4121
|
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
4122
|
conversation_id: row.conversation_id,
|
|
2589
4123
|
message_id: row.id
|
|
@@ -2592,7 +4126,7 @@ var ChannelDriver = class {
|
|
|
2592
4126
|
}
|
|
2593
4127
|
if (this.awaitingReadopt.has(row.id)) {
|
|
2594
4128
|
this.log({
|
|
2595
|
-
level: "
|
|
4129
|
+
level: "debug",
|
|
2596
4130
|
message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
|
|
2597
4131
|
conversation_id: row.conversation_id,
|
|
2598
4132
|
message_id: row.id
|
|
@@ -2602,11 +4136,12 @@ var ChannelDriver = class {
|
|
|
2602
4136
|
if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
|
|
2603
4137
|
this.dontRedispatch.add(row.id);
|
|
2604
4138
|
this.log({
|
|
2605
|
-
level: "
|
|
4139
|
+
level: "debug",
|
|
2606
4140
|
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
4141
|
conversation_id: row.conversation_id,
|
|
2608
4142
|
message_id: row.id
|
|
2609
4143
|
});
|
|
4144
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_window_elapsed");
|
|
2610
4145
|
return;
|
|
2611
4146
|
}
|
|
2612
4147
|
const options = {
|
|
@@ -2620,40 +4155,44 @@ var ChannelDriver = class {
|
|
|
2620
4155
|
message_id: row.id
|
|
2621
4156
|
});
|
|
2622
4157
|
this.awaitingReadopt.add(row.id);
|
|
4158
|
+
const readoptConv = this.convForRow(sessionId, row);
|
|
4159
|
+
const readoptMessage = this.queuedMessageForRow(row);
|
|
4160
|
+
const sendAttachments = this.buildSendAttachments(readoptConv, readoptMessage);
|
|
2623
4161
|
let ocId;
|
|
2624
4162
|
try {
|
|
2625
4163
|
ocId = await this.dispatchLocked(
|
|
2626
4164
|
sessionId,
|
|
2627
|
-
() => sendPromptAsync(this.port, sessionId, row.content, options)
|
|
4165
|
+
() => sendPromptAsync(this.port, sessionId, row.content, options, sendAttachments)
|
|
2628
4166
|
);
|
|
2629
4167
|
} catch (err) {
|
|
2630
4168
|
this.awaitingReadopt.delete(row.id);
|
|
2631
4169
|
if (err instanceof ChannelAuthError) throw err;
|
|
2632
4170
|
this.log({
|
|
2633
|
-
level: "
|
|
4171
|
+
level: "warn",
|
|
2634
4172
|
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
4173
|
conversation_id: row.conversation_id,
|
|
2636
4174
|
message_id: row.id
|
|
2637
4175
|
});
|
|
4176
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
|
|
2638
4177
|
return;
|
|
2639
4178
|
}
|
|
2640
4179
|
if (ocId === null) {
|
|
2641
4180
|
this.awaitingReadopt.delete(row.id);
|
|
2642
4181
|
this.log({
|
|
2643
|
-
level: "
|
|
4182
|
+
level: "warn",
|
|
2644
4183
|
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
4184
|
conversation_id: row.conversation_id,
|
|
2646
4185
|
message_id: row.id
|
|
2647
4186
|
});
|
|
4187
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
|
|
2648
4188
|
return;
|
|
2649
4189
|
}
|
|
2650
|
-
|
|
2651
|
-
const message = this.queuedMessageForRow(row);
|
|
2652
|
-
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
4190
|
+
this.registerReadopted(readoptConv, sessionId, readoptMessage, ocId, this.processedAtMs(row));
|
|
2653
4191
|
this.dispatched.add(row.id);
|
|
2654
4192
|
this.readopted.add(row.id);
|
|
2655
4193
|
this.awaitingReadopt.delete(row.id);
|
|
2656
4194
|
this.ensureWatcherRunning(sessionId);
|
|
4195
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
|
|
2657
4196
|
}
|
|
2658
4197
|
/**
|
|
2659
4198
|
* True if `evidentMessageId` is already being driven — either in the
|
|
@@ -2702,7 +4241,8 @@ var ChannelDriver = class {
|
|
|
2702
4241
|
opencode_agent: row.opencode_agent,
|
|
2703
4242
|
opencode_model: row.opencode_model,
|
|
2704
4243
|
source_message_id: row.source_message_id,
|
|
2705
|
-
slack_user_id: row.slack_user_id
|
|
4244
|
+
slack_user_id: row.slack_user_id,
|
|
4245
|
+
attachments: row.attachments ?? null
|
|
2706
4246
|
};
|
|
2707
4247
|
}
|
|
2708
4248
|
/**
|
|
@@ -2723,7 +4263,7 @@ var ChannelDriver = class {
|
|
|
2723
4263
|
if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
|
|
2724
4264
|
this.dontRedispatch.add(evidentMessageId);
|
|
2725
4265
|
this.log({
|
|
2726
|
-
level: "
|
|
4266
|
+
level: "debug",
|
|
2727
4267
|
message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
|
|
2728
4268
|
conversation_id: watcher.conv.id,
|
|
2729
4269
|
message_id: evidentMessageId
|
|
@@ -2745,21 +4285,41 @@ var ChannelDriver = class {
|
|
|
2745
4285
|
* RUNNING (not done) is the one that paused. With one running message that is
|
|
2746
4286
|
* unambiguous; with several we prefer an explicit messageID match, else the
|
|
2747
4287
|
* oldest running message.
|
|
4288
|
+
*
|
|
4289
|
+
* Returns the set of in-flight Evident message ids that are paused awaiting a
|
|
4290
|
+
* human — an outstanding (still-open) question/permission is attributed to them.
|
|
4291
|
+
* `serviceInFlightMessage` uses this to keep an actively-running turn watched
|
|
4292
|
+
* forever (ADR-0047) while still bounding a turn merely blocked on a person who
|
|
4293
|
+
* may never answer. Attribution here covers ALL open interactions, not just
|
|
4294
|
+
* NEW (un-deduped) ones — a question stays "awaiting a human" until answered,
|
|
4295
|
+
* even after it was already surfaced to the channel.
|
|
2748
4296
|
*/
|
|
2749
4297
|
async pollInteractions(sessionId, watcher, messages) {
|
|
4298
|
+
const openQuestions = /* @__PURE__ */ new Set();
|
|
4299
|
+
const openPermissions = /* @__PURE__ */ new Set();
|
|
4300
|
+
let questionsPolledOk = true;
|
|
4301
|
+
let permissionsPolledOk = true;
|
|
2750
4302
|
let questions = [];
|
|
2751
4303
|
try {
|
|
2752
4304
|
const res = await this.fetchImpl(`${this.opencodeBase}/question`);
|
|
2753
4305
|
if (res.ok) {
|
|
2754
4306
|
const body = await res.json();
|
|
2755
|
-
|
|
4307
|
+
if (Array.isArray(body)) {
|
|
4308
|
+
questions = body;
|
|
4309
|
+
} else {
|
|
4310
|
+
questionsPolledOk = false;
|
|
4311
|
+
}
|
|
4312
|
+
} else {
|
|
4313
|
+
questionsPolledOk = false;
|
|
2756
4314
|
}
|
|
2757
4315
|
} catch {
|
|
4316
|
+
questionsPolledOk = false;
|
|
2758
4317
|
}
|
|
2759
4318
|
for (const q of questions) {
|
|
2760
|
-
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2761
4319
|
if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
|
|
2762
4320
|
const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
|
|
4321
|
+
if (paused) openQuestions.add(paused.evidentMessageId);
|
|
4322
|
+
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2763
4323
|
const reported = await this.reportInteraction(
|
|
2764
4324
|
watcher.conv.id,
|
|
2765
4325
|
"question",
|
|
@@ -2773,14 +4333,22 @@ var ChannelDriver = class {
|
|
|
2773
4333
|
const res = await this.fetchImpl(`${this.opencodeBase}/permission`);
|
|
2774
4334
|
if (res.ok) {
|
|
2775
4335
|
const body = await res.json();
|
|
2776
|
-
|
|
4336
|
+
if (Array.isArray(body)) {
|
|
4337
|
+
permissions = body;
|
|
4338
|
+
} else {
|
|
4339
|
+
permissionsPolledOk = false;
|
|
4340
|
+
}
|
|
4341
|
+
} else {
|
|
4342
|
+
permissionsPolledOk = false;
|
|
2777
4343
|
}
|
|
2778
4344
|
} catch {
|
|
4345
|
+
permissionsPolledOk = false;
|
|
2779
4346
|
}
|
|
2780
4347
|
for (const p of permissions) {
|
|
2781
|
-
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2782
4348
|
if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
|
|
2783
4349
|
const paused = this.attributeInteraction(watcher, p.messageID, messages);
|
|
4350
|
+
if (paused) openPermissions.add(paused.evidentMessageId);
|
|
4351
|
+
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2784
4352
|
const reported = await this.reportInteraction(
|
|
2785
4353
|
watcher.conv.id,
|
|
2786
4354
|
"permission",
|
|
@@ -2789,6 +4357,7 @@ var ChannelDriver = class {
|
|
|
2789
4357
|
);
|
|
2790
4358
|
if (reported) watcher.reportedPermissions.add(p.id);
|
|
2791
4359
|
}
|
|
4360
|
+
return { openQuestions, openPermissions, questionsPolledOk, permissionsPolledOk };
|
|
2792
4361
|
}
|
|
2793
4362
|
/**
|
|
2794
4363
|
* True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
|
|
@@ -2812,6 +4381,47 @@ var ChannelDriver = class {
|
|
|
2812
4381
|
}
|
|
2813
4382
|
return false;
|
|
2814
4383
|
}
|
|
4384
|
+
/**
|
|
4385
|
+
* Tri-state variant of the upward parentID membership walk (#721), used ONLY
|
|
4386
|
+
* by `isAnyDescendantSessionOngoing`. Walks the SAME cached
|
|
4387
|
+
* `resolveSessionParent` chain `sessionBelongsTo` uses above, but — unlike
|
|
4388
|
+
* `sessionBelongsTo`, which deliberately collapses "confirmed not a
|
|
4389
|
+
* descendant" and "the walk's fetch failed" into the same `false` (safe for
|
|
4390
|
+
* its OTHER callers: interaction attribution and the recovery-path
|
|
4391
|
+
* `isAnyDescendantSessionAlive`, both of which just retry next tick with no
|
|
4392
|
+
* safety consequence either way) — this variant keeps those two outcomes
|
|
4393
|
+
* SEPARATE, because `isAnyDescendantSessionOngoing`'s caller
|
|
4394
|
+
* (`isB2AbandonmentConfirmed`) must never treat "couldn't tell" as "confirmed
|
|
4395
|
+
* not ongoing".
|
|
4396
|
+
*
|
|
4397
|
+
* Return contract:
|
|
4398
|
+
* - `true` → the walk reached `rootSessionId` — `sessionId` IS a descendant.
|
|
4399
|
+
* - `false` → the walk reached a definitive, parent-less root session
|
|
4400
|
+
* WITHOUT ever matching `rootSessionId` — `sessionId` is
|
|
4401
|
+
* CONFIRMED NOT a descendant of it.
|
|
4402
|
+
* - `null` → INDETERMINATE: a `GET /session/:id` fetch failed partway
|
|
4403
|
+
* through the walk (`resolveSessionParent` returned `undefined`),
|
|
4404
|
+
* or the depth cap (32) was hit without a definitive answer (a
|
|
4405
|
+
* pathological/cyclic chain proves nothing either way). NEVER
|
|
4406
|
+
* treat this the same as `false` — see `sessionBelongsTo`'s own
|
|
4407
|
+
* doc comment above for why that collapse is safe THERE but not
|
|
4408
|
+
* here.
|
|
4409
|
+
*
|
|
4410
|
+
* `sessionBelongsTo` itself is UNCHANGED — this is an additive helper scoped
|
|
4411
|
+
* to the live-path descendant check, not a modification of shared code used
|
|
4412
|
+
* by interaction attribution or the recovery path.
|
|
4413
|
+
*/
|
|
4414
|
+
async resolveSessionMembership(sessionId, rootSessionId) {
|
|
4415
|
+
let current = sessionId;
|
|
4416
|
+
for (let depth = 0; current && depth < 32; depth++) {
|
|
4417
|
+
if (current === rootSessionId) return true;
|
|
4418
|
+
const parent = await this.resolveSessionParent(current);
|
|
4419
|
+
if (parent === void 0) return null;
|
|
4420
|
+
if (parent === null) return false;
|
|
4421
|
+
current = parent;
|
|
4422
|
+
}
|
|
4423
|
+
return null;
|
|
4424
|
+
}
|
|
2815
4425
|
/**
|
|
2816
4426
|
* Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
|
|
2817
4427
|
* `null` for a root session (no parent) and `undefined` when opencode is
|
|
@@ -2834,6 +4444,271 @@ var ChannelDriver = class {
|
|
|
2834
4444
|
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
2835
4445
|
return parent;
|
|
2836
4446
|
}
|
|
4447
|
+
/**
|
|
4448
|
+
* OpenCode's synchronous default session title (e.g.
|
|
4449
|
+
* `"New session - 1737800000000"`), assigned immediately when a session is
|
|
4450
|
+
* created — before OpenCode's async LLM-based auto-titling later renames it
|
|
4451
|
+
* mid-turn (#549). Matched by this literal, case-sensitive prefix only; the
|
|
4452
|
+
* timestamp suffix's exact format is deliberately NOT matched, since the prefix
|
|
4453
|
+
* alone is the stable, cheap signal and over-anchoring on the timestamp
|
|
4454
|
+
* representation risks silently breaking if OpenCode ever changes it. Accepted
|
|
4455
|
+
* trade-off: a genuine LLM-assigned title that happens to literally start with
|
|
4456
|
+
* this prefix would also fail to latch (see `resolveSessionTitle`) —
|
|
4457
|
+
* vanishingly unlikely in practice, and deliberately not engineered around.
|
|
4458
|
+
*/
|
|
4459
|
+
static OPENCODE_DEFAULT_TITLE_PREFIX = /^New session - /;
|
|
4460
|
+
/**
|
|
4461
|
+
* Resolve (and cache in `sessionTitles`) the OpenCode session TITLE (#310) so the
|
|
4462
|
+
* status PATCH can carry it into the "Live sessions" list. Driver-level cache so
|
|
4463
|
+
* BOTH the watcher completion path and the restart-recovery re-adopt path (which
|
|
4464
|
+
* has no watcher) can use it. `conversationId` is passed only for log context.
|
|
4465
|
+
* Best-effort:
|
|
4466
|
+
* - a resolved NON-EMPTY title that does NOT match
|
|
4467
|
+
* `OPENCODE_DEFAULT_TITLE_PREFIX` is cached and terminal (a real session name
|
|
4468
|
+
* won't later un-name), so we do NOT re-GET `/session/:id` every tick;
|
|
4469
|
+
* - while the title is still absent, empty, or matches the OpenCode
|
|
4470
|
+
* placeholder prefix (#549) we do NOT latch it — OpenCode names sessions
|
|
4471
|
+
* asynchronously mid-turn, so an early call (e.g. at `processing`) must leave
|
|
4472
|
+
* the cache unresolved and re-fetch on the next need so a later call (e.g. at
|
|
4473
|
+
* `done`) picks up the name assigned in the meantime. Such a call returns
|
|
4474
|
+
* `null` (omit the title on THIS PATCH) without caching. If a session is
|
|
4475
|
+
* never renamed, the title is omitted forever rather than ever persisting
|
|
4476
|
+
* the placeholder as a last resort;
|
|
4477
|
+
* - a failed request likewise leaves the cache unresolved (retry next need)
|
|
4478
|
+
* and returns `null` — it must NEVER throw or block completion.
|
|
4479
|
+
* A failure is logged with agent/session context (no silent catch).
|
|
4480
|
+
*/
|
|
4481
|
+
async resolveSessionTitle(sessionId, conversationId) {
|
|
4482
|
+
const cached = this.sessionTitles.get(sessionId);
|
|
4483
|
+
if (cached != null) return cached;
|
|
4484
|
+
try {
|
|
4485
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
|
|
4486
|
+
if (res.ok) {
|
|
4487
|
+
const body = await res.json();
|
|
4488
|
+
const title = body && typeof body.title === "string" ? body.title.trim() : "";
|
|
4489
|
+
if (title.length > 0 && !_ChannelDriver.OPENCODE_DEFAULT_TITLE_PREFIX.test(title)) {
|
|
4490
|
+
this.sessionTitles.set(sessionId, title);
|
|
4491
|
+
return title;
|
|
4492
|
+
}
|
|
4493
|
+
return null;
|
|
4494
|
+
}
|
|
4495
|
+
this.log({
|
|
4496
|
+
level: "debug",
|
|
4497
|
+
message: `Session title fetch for session ${sessionId.slice(0, 8)} (agent ${this.agentId.slice(0, 8)}) returned HTTP ${res.status} \u2014 omitting title`,
|
|
4498
|
+
conversation_id: conversationId
|
|
4499
|
+
});
|
|
4500
|
+
} catch (err) {
|
|
4501
|
+
this.log({
|
|
4502
|
+
level: "debug",
|
|
4503
|
+
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)}`,
|
|
4504
|
+
conversation_id: conversationId
|
|
4505
|
+
});
|
|
4506
|
+
}
|
|
4507
|
+
return null;
|
|
4508
|
+
}
|
|
4509
|
+
/**
|
|
4510
|
+
* Best-effort mid-turn title sync (#711 follow-up): PATCH a resolved OpenCode
|
|
4511
|
+
* session title onto the conversation via the PLAIN conversation-update
|
|
4512
|
+
* endpoint (`PATCH /runners/:agentId/conversations/:conversationId`) — NOT the
|
|
4513
|
+
* message-status endpoint `markProcessing`/`markDone` use. Deliberately a
|
|
4514
|
+
* separate, lighter call: it carries no `status`, so it cannot re-trigger the
|
|
4515
|
+
* `processing`/`done` transition side effects (Slack notices, activity-log
|
|
4516
|
+
* rows, delivery jobs) those PATCHes gate on `transitioned` — this call only
|
|
4517
|
+
* ever touches `conversations.title`. That route (`routes/conversations.ts`)
|
|
4518
|
+
* skips a title write matching the stored value, so a redundant call with the
|
|
4519
|
+
* same title is a real no-op — it does not bump `updated_at`, which the
|
|
4520
|
+
* conversation list sorts and paginates on. (Note this is a DIFFERENT guard
|
|
4521
|
+
* from `threads.ts`'s "non-empty AND changed" one, which only covers the
|
|
4522
|
+
* message-status PATCH; the non-empty half is enforced here instead, by
|
|
4523
|
+
* `resolveSessionTitle` never returning an empty/placeholder title.)
|
|
4524
|
+
*
|
|
4525
|
+
* Telemetry-only / never blocks the caller, mirroring `postSignal`: a failure
|
|
4526
|
+
* is logged and the title is simply retried on the next heartbeat tick (the
|
|
4527
|
+
* caller only latches `titleSynced` on `true`).
|
|
4528
|
+
*/
|
|
4529
|
+
async patchConversationTitle(conversationId, title) {
|
|
4530
|
+
try {
|
|
4531
|
+
const res = await this.fetchImpl(
|
|
4532
|
+
`${this.apiUrl}/runners/${this.agentId}/conversations/${conversationId}`,
|
|
4533
|
+
{
|
|
4534
|
+
method: "PATCH",
|
|
4535
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
4536
|
+
body: JSON.stringify({ title })
|
|
4537
|
+
}
|
|
4538
|
+
);
|
|
4539
|
+
if (!res.ok) {
|
|
4540
|
+
this.log({
|
|
4541
|
+
level: "debug",
|
|
4542
|
+
message: `Mid-turn title sync PATCH for conversation ${conversationId.slice(0, 8)} returned HTTP ${res.status} (best-effort, will retry next heartbeat)`,
|
|
4543
|
+
conversation_id: conversationId
|
|
4544
|
+
});
|
|
4545
|
+
return false;
|
|
4546
|
+
}
|
|
4547
|
+
return true;
|
|
4548
|
+
} catch (err) {
|
|
4549
|
+
this.log({
|
|
4550
|
+
level: "debug",
|
|
4551
|
+
message: `Best-effort mid-turn title sync PATCH failed for conversation ${conversationId.slice(0, 8)} (will retry next heartbeat): ${err instanceof Error ? err.message : String(err)}`,
|
|
4552
|
+
conversation_id: conversationId
|
|
4553
|
+
});
|
|
4554
|
+
return false;
|
|
4555
|
+
}
|
|
4556
|
+
}
|
|
4557
|
+
/**
|
|
4558
|
+
* DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
|
|
4559
|
+
* (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
|
|
4560
|
+
*
|
|
4561
|
+
* The PRIMARY recovery trigger is "preamble-pinned on recovery ⇒ idle" — a
|
|
4562
|
+
* runner restart wipes OpenCode's in-memory `SessionStatus`/`Runner`, so a
|
|
4563
|
+
* completed `finish: "tool-calls"` root reply encountered during re-adoption is
|
|
4564
|
+
* idle by OpenCode's own definition and is re-dispatched. This method exists only
|
|
4565
|
+
* so the WI-3 caller can VETO that re-dispatch in the rare case a descendant is
|
|
4566
|
+
* provably in flight at the exact moment of recovery.
|
|
4567
|
+
*
|
|
4568
|
+
* "Alive" criterion (TIGHTENED): a descendant is alive only when it is PROVABLY,
|
|
4569
|
+
* ACTIVELY generating — its LAST message is an assistant still mid-generation
|
|
4570
|
+
* (`completed == null`, via `isSessionActivelyGenerating`). An
|
|
4571
|
+
* INCOMPLETE-BUT-NOT-GENERATING child — last message a user message, or a
|
|
4572
|
+
* completed `finish: "tool-calls"` step — is NOT alive after a restart (nothing
|
|
4573
|
+
* is generating once the runner is gone), so it does NOT veto. (This is
|
|
4574
|
+
* deliberately NOT `!isTurnComplete`, which also matches those dead-but-non-terminal
|
|
4575
|
+
* shapes and would falsely veto — re-hanging the very turn this path recovers.)
|
|
4576
|
+
*
|
|
4577
|
+
* Return contract (encoded so WI-3 need not re-derive it):
|
|
4578
|
+
* - `true` → a descendant is provably, actively generating (veto re-dispatch).
|
|
4579
|
+
* - `false` → descendants exist but none is actively generating (the restart
|
|
4580
|
+
* case), OR no descendant is found at all.
|
|
4581
|
+
* - `null` → liveness is INDETERMINATE (enumeration via `listSessions` failed).
|
|
4582
|
+
*
|
|
4583
|
+
* ⚠️ `null` (UNKNOWN) MUST NOT be treated as "alive": WI-3 treats `null` the same
|
|
4584
|
+
* as `false` and does NOT veto — a restart guarantees no live runner, so an
|
|
4585
|
+
* indeterminate cross-check almost always means "couldn't reach a child that no
|
|
4586
|
+
* longer exists". The inversion lives in the caller; this method just reports
|
|
4587
|
+
* true/false/null faithfully.
|
|
4588
|
+
*
|
|
4589
|
+
* VERIFY-BEFORE-DEPEND: we depend ONLY on (a) `parentID` from `GET /session/:id`
|
|
4590
|
+
* (already proven by the existing child-session interaction tests, via
|
|
4591
|
+
* `resolveSessionParent`/`sessionBelongsTo`) and (b) the child's own message-list
|
|
4592
|
+
* terminal state. We do NOT depend on any session-level `busy`/`idle` field —
|
|
4593
|
+
* there is none on `GET /session/:id`; OpenCode's busy state is in-memory
|
|
4594
|
+
* `SessionStatus` only.
|
|
4595
|
+
*/
|
|
4596
|
+
async isAnyDescendantSessionAlive(rootSessionId) {
|
|
4597
|
+
const sessions = await listSessions(this.port);
|
|
4598
|
+
if (!sessions) {
|
|
4599
|
+
this.log({
|
|
4600
|
+
level: "warn",
|
|
4601
|
+
message: `Re-adopt: could not enumerate sessions to cross-check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating child liveness as indeterminate`
|
|
4602
|
+
});
|
|
4603
|
+
return null;
|
|
4604
|
+
}
|
|
4605
|
+
for (const candidate of sessions) {
|
|
4606
|
+
if (!candidate?.id || candidate.id === rootSessionId) continue;
|
|
4607
|
+
if (!await this.sessionBelongsTo(candidate.id, rootSessionId)) continue;
|
|
4608
|
+
const childMsgs = await getSessionMessages(this.port, candidate.id);
|
|
4609
|
+
if (isSessionActivelyGenerating(childMsgs)) {
|
|
4610
|
+
return true;
|
|
4611
|
+
}
|
|
4612
|
+
}
|
|
4613
|
+
return false;
|
|
4614
|
+
}
|
|
4615
|
+
/**
|
|
4616
|
+
* LIVE-PATH descendant-liveness check (#721): is any descendant (`task`
|
|
4617
|
+
* sub-agent) session under `rootSessionId` currently ONGOING per OpenCode's own
|
|
4618
|
+
* in-memory status map (`isSessionOngoing` — `busy`/`retry`)?
|
|
4619
|
+
*
|
|
4620
|
+
* Deliberately NOT `isAnyDescendantSessionAlive` (the RECOVERY-path
|
|
4621
|
+
* cross-check above): that method judges liveness from the child's OWN
|
|
4622
|
+
* TRANSCRIPT (`isSessionActivelyGenerating`), which is the right (only) option
|
|
4623
|
+
* on the recovery path because a restart WIPES `SessionStatus`. On the LIVE
|
|
4624
|
+
* path the local opencode server IS running, so its in-memory status map is
|
|
4625
|
+
* live and authoritative — and per ADR-0047 §4a ("the child has its own entry
|
|
4626
|
+
* [in the map]"), a `task` descendant's OWN busy/retry entry reflects its
|
|
4627
|
+
* ENTIRE turn (including any tool call it is itself executing), not a
|
|
4628
|
+
* per-message transcript snapshot. This sidesteps the "child's own tool is
|
|
4629
|
+
* executing, between its step's completion and the next generation step"
|
|
4630
|
+
* transcript gap that a transcript-based check would need a second,
|
|
4631
|
+
* sustained-window bound to guard against — it is simply not derived from
|
|
4632
|
+
* message timestamps at all.
|
|
4633
|
+
*
|
|
4634
|
+
* Why not just check `isSessionOngoing(port, rootSessionId)` (the ROOT's own
|
|
4635
|
+
* status, as the recovery path does per §4a)? Because on the LIVE path the
|
|
4636
|
+
* root session can be shared: a SECOND, unrelated user message can land on the
|
|
4637
|
+
* SAME session (issue #721's own root cause) and keep the root `busy` for a
|
|
4638
|
+
* reason that has nothing to do with THIS message's delegation. A `task`
|
|
4639
|
+
* descendant session is spawned for exactly one delegated turn and never
|
|
4640
|
+
* reused, so its OWN status-map entry is unambiguous evidence about that one
|
|
4641
|
+
* delegation — which the root's status is not.
|
|
4642
|
+
*
|
|
4643
|
+
* Why membership is checked via `resolveSessionMembership`, NOT
|
|
4644
|
+
* `sessionBelongsTo`: `sessionBelongsTo` collapses a transient
|
|
4645
|
+
* `GET /session/:id` fetch failure into "not a descendant", which would
|
|
4646
|
+
* silently drop a genuinely-live candidate from consideration on the one
|
|
4647
|
+
* unlucky tick its membership-walk fetch hiccups (#721).
|
|
4648
|
+
* `resolveSessionMembership` keeps that failure mode as a distinct `null`
|
|
4649
|
+
* (indeterminate) so it is folded into THIS method's own `indeterminate` flag
|
|
4650
|
+
* instead.
|
|
4651
|
+
*
|
|
4652
|
+
* Return contract (note the DIFFERENT judge vs. `isAnyDescendantSessionAlive`):
|
|
4653
|
+
* - `true` → some descendant session is `busy`/`retry` (genuinely ongoing).
|
|
4654
|
+
* - `false` → enumeration succeeded, EVERY candidate's MEMBERSHIP was
|
|
4655
|
+
* confirmed either way (`resolveSessionMembership` never
|
|
4656
|
+
* returned `null`), and every CONFIRMED descendant's status read
|
|
4657
|
+
* succeeded and is not ongoing (includes "no descendant session
|
|
4658
|
+
* exists at all" — e.g. a plain, non-`task` tool call).
|
|
4659
|
+
* - `null` → INDETERMINATE: `listSessions` failed, OR at least one
|
|
4660
|
+
* candidate's MEMBERSHIP could not be confirmed
|
|
4661
|
+
* (`resolveSessionMembership` returned `null` — a fetch failure
|
|
4662
|
+
* or pathological chain partway through the parent walk), OR at
|
|
4663
|
+
* least one CONFIRMED descendant's `isSessionOngoing` read
|
|
4664
|
+
* failed — and no OTHER candidate was already confirmed `true`.
|
|
4665
|
+
* The caller MUST NOT treat `null` the same as `false` here
|
|
4666
|
+
* (unlike the recovery cross-check's contract) — see
|
|
4667
|
+
* `isB2AbandonmentConfirmed`.
|
|
4668
|
+
*/
|
|
4669
|
+
async isAnyDescendantSessionOngoing(rootSessionId) {
|
|
4670
|
+
const sessions = await listSessions(this.port);
|
|
4671
|
+
if (!sessions) {
|
|
4672
|
+
this.log({
|
|
4673
|
+
level: "warn",
|
|
4674
|
+
message: `Could not enumerate sessions to check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating descendant liveness as indeterminate`
|
|
4675
|
+
});
|
|
4676
|
+
return null;
|
|
4677
|
+
}
|
|
4678
|
+
let indeterminate = false;
|
|
4679
|
+
for (const candidate of sessions) {
|
|
4680
|
+
if (!candidate?.id || candidate.id === rootSessionId) continue;
|
|
4681
|
+
const membership = await this.resolveSessionMembership(candidate.id, rootSessionId);
|
|
4682
|
+
if (membership === null) {
|
|
4683
|
+
indeterminate = true;
|
|
4684
|
+
continue;
|
|
4685
|
+
}
|
|
4686
|
+
if (membership === false) continue;
|
|
4687
|
+
const ongoing = await isSessionOngoing(this.port, candidate.id);
|
|
4688
|
+
if (ongoing === true) return true;
|
|
4689
|
+
if (ongoing === null) indeterminate = true;
|
|
4690
|
+
}
|
|
4691
|
+
return indeterminate ? null : false;
|
|
4692
|
+
}
|
|
4693
|
+
/**
|
|
4694
|
+
* Cheap decision-telemetry label for a running row's LAST correlated reply
|
|
4695
|
+
* (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
|
|
4696
|
+
* - `b1` — the reply itself is still in flight (`time.completed == null`) —
|
|
4697
|
+
* the aborted-in-flight production bug after a restart.
|
|
4698
|
+
* - `b2` — a COMPLETED reply pinned running only by `finish === "tool-calls"`
|
|
4699
|
+
* (the sub-agent preamble — #253's shape).
|
|
4700
|
+
* - `other` — any other shape (defensive; a running row is normally b1 or b2).
|
|
4701
|
+
* Reads `info.time.completed` / `info.finish` (tolerating the legacy top-level
|
|
4702
|
+
* shape) directly rather than re-importing the module-private `completedOf`/
|
|
4703
|
+
* `finishOf` — this is a display label only, not a correctness predicate.
|
|
4704
|
+
*/
|
|
4705
|
+
replyCompletionShape(reply) {
|
|
4706
|
+
if (!reply) return "other";
|
|
4707
|
+
const completed = reply.info?.time?.completed ?? reply.time?.completed;
|
|
4708
|
+
if (completed == null) return "b1";
|
|
4709
|
+
const finish = reply.info?.finish ?? reply.finish;
|
|
4710
|
+
return finish === "tool-calls" ? "b2" : "other";
|
|
4711
|
+
}
|
|
2837
4712
|
/**
|
|
2838
4713
|
* Attribute a surfaced interaction to the in-flight message it paused on (M-1).
|
|
2839
4714
|
*
|
|
@@ -2884,12 +4759,10 @@ var ChannelDriver = class {
|
|
|
2884
4759
|
}
|
|
2885
4760
|
return inFlight.sort(byOldest)[0];
|
|
2886
4761
|
}
|
|
2887
|
-
// -------------------------------------------------------------------------
|
|
2888
4762
|
// Evident API calls (combinedAuth thread routes)
|
|
2889
|
-
// -------------------------------------------------------------------------
|
|
2890
4763
|
async getPendingConversations() {
|
|
2891
4764
|
const res = await this.fetchImpl(
|
|
2892
|
-
`${this.apiUrl}/
|
|
4765
|
+
`${this.apiUrl}/runners/${this.agentId}/conversations/pending`,
|
|
2893
4766
|
{
|
|
2894
4767
|
headers: { Authorization: this.getAuthHeader() }
|
|
2895
4768
|
}
|
|
@@ -2907,7 +4780,7 @@ var ChannelDriver = class {
|
|
|
2907
4780
|
}
|
|
2908
4781
|
async getPendingMessages(conversationId) {
|
|
2909
4782
|
const res = await this.fetchImpl(
|
|
2910
|
-
`${this.apiUrl}/
|
|
4783
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages?status=pending`,
|
|
2911
4784
|
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2912
4785
|
);
|
|
2913
4786
|
this.assertAuth(res, "fetching pending messages");
|
|
@@ -2931,7 +4804,7 @@ var ChannelDriver = class {
|
|
|
2931
4804
|
*/
|
|
2932
4805
|
async getProcessingMessages() {
|
|
2933
4806
|
const res = await this.fetchImpl(
|
|
2934
|
-
`${this.apiUrl}/
|
|
4807
|
+
`${this.apiUrl}/runners/${this.agentId}/conversations/processing`,
|
|
2935
4808
|
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2936
4809
|
);
|
|
2937
4810
|
this.assertAuth(res, "fetching processing messages");
|
|
@@ -2945,6 +4818,32 @@ var ChannelDriver = class {
|
|
|
2945
4818
|
}
|
|
2946
4819
|
return messages;
|
|
2947
4820
|
}
|
|
4821
|
+
/**
|
|
4822
|
+
* The `opencode_session_id` fragment of a status PATCH body — `{}` when this
|
|
4823
|
+
* conversation has ABANDONED that session (#553). The field is optional
|
|
4824
|
+
* server-side and an absent one leaves the persisted binding untouched, so
|
|
4825
|
+
* omitting it is how a routine status write stops resurrecting it.
|
|
4826
|
+
*
|
|
4827
|
+
* ONLY for writes whose sole cost is a lost deep link. The `processing` notice
|
|
4828
|
+
* degrades to no "View in Evident" link (the reaction swap still fires) and the
|
|
4829
|
+
* turn-failure notice is built from the PATCH's own `error` text with a link off
|
|
4830
|
+
* the persisted row — neither loses content the user came for. `markDone`
|
|
4831
|
+
* deliberately does NOT use this helper: the server fetches the reply text
|
|
4832
|
+
* THROUGH the session id it is given, so suppressing there would replace the
|
|
4833
|
+
* agent's answer with a bare "✅ Done!" (the #183/#187 failure). The
|
|
4834
|
+
* `ensureSession` guard, not this suppression, is what makes the self-heal
|
|
4835
|
+
* stick.
|
|
4836
|
+
*/
|
|
4837
|
+
sessionIdBody(sessionId, conversationId, messageId, status) {
|
|
4838
|
+
if (!this.isSuperseded(conversationId, sessionId)) return { opencode_session_id: sessionId };
|
|
4839
|
+
this.log({
|
|
4840
|
+
level: "debug",
|
|
4841
|
+
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)}`,
|
|
4842
|
+
conversation_id: conversationId,
|
|
4843
|
+
message_id: messageId
|
|
4844
|
+
});
|
|
4845
|
+
return {};
|
|
4846
|
+
}
|
|
2948
4847
|
/**
|
|
2949
4848
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
2950
4849
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
@@ -2966,16 +4865,17 @@ var ChannelDriver = class {
|
|
|
2966
4865
|
* A single attempt (no internal retry): the watcher's per-tick loop is the
|
|
2967
4866
|
* retry vehicle for the swap-to-running.
|
|
2968
4867
|
*/
|
|
2969
|
-
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
4868
|
+
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId, title) {
|
|
2970
4869
|
const res = await this.fetchImpl(
|
|
2971
|
-
`${this.apiUrl}/
|
|
4870
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2972
4871
|
{
|
|
2973
4872
|
method: "PATCH",
|
|
2974
4873
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2975
4874
|
body: JSON.stringify({
|
|
2976
4875
|
status: "processing",
|
|
2977
|
-
|
|
2978
|
-
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
4876
|
+
...this.sessionIdBody(sessionId, conversationId, messageId, "processing"),
|
|
4877
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
4878
|
+
...title ? { title } : {}
|
|
2979
4879
|
})
|
|
2980
4880
|
}
|
|
2981
4881
|
);
|
|
@@ -3014,16 +4914,23 @@ var ChannelDriver = class {
|
|
|
3014
4914
|
* watcher retries next tick within the
|
|
3015
4915
|
* deadline, Finding 4).
|
|
3016
4916
|
*/
|
|
3017
|
-
async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
4917
|
+
async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage) {
|
|
3018
4918
|
const res = await this.fetchImpl(
|
|
3019
|
-
`${this.apiUrl}/
|
|
4919
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
3020
4920
|
{
|
|
3021
4921
|
method: "PATCH",
|
|
3022
4922
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3023
4923
|
body: JSON.stringify({
|
|
3024
4924
|
status: "done",
|
|
4925
|
+
// ALWAYS sent, even for a session this conversation has abandoned
|
|
4926
|
+
// (#553): the server reads the reply text back out of THIS session id
|
|
4927
|
+
// to deliver it. Omitting it would leave the user with "✅ Done!"
|
|
4928
|
+
// instead of the answer — a worse regression than the resurrection it
|
|
4929
|
+
// would prevent, which `ensureSession`'s guard handles anyway.
|
|
3025
4930
|
opencode_session_id: sessionId,
|
|
3026
|
-
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
4931
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
4932
|
+
...title ? { title } : {},
|
|
4933
|
+
...usage ? usage : {}
|
|
3027
4934
|
})
|
|
3028
4935
|
}
|
|
3029
4936
|
);
|
|
@@ -3036,19 +4943,29 @@ var ChannelDriver = class {
|
|
|
3036
4943
|
}
|
|
3037
4944
|
/**
|
|
3038
4945
|
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
3039
|
-
* when provided (issue #182)
|
|
3040
|
-
* `
|
|
3041
|
-
*
|
|
3042
|
-
*
|
|
4946
|
+
* when provided (issue #182). Three states for `sessionId`:
|
|
4947
|
+
* - omitted (`undefined`) → don't send the field, leave the persisted
|
|
4948
|
+
* session untouched (unused today; kept for API symmetry).
|
|
4949
|
+
* - a real id (`string`) → send it, update the persisted session (the
|
|
4950
|
+
* turn-failure call sites: an errored OpenCode turn).
|
|
4951
|
+
* - explicit `null` → send it, CLEAR the persisted session (issue
|
|
4952
|
+
* #485's dispatch-handoff-failure call site: the session id still
|
|
4953
|
+
* exists but is wedged, so the next attempt must get a fresh one
|
|
4954
|
+
* instead of reusing it — see WI-1's server-side null-clearing PATCH).
|
|
3043
4955
|
*/
|
|
3044
|
-
async markFailed(conversationId, messageId, sessionId, error2) {
|
|
4956
|
+
async markFailed(conversationId, messageId, sessionId, error2, usage) {
|
|
3045
4957
|
const body = { status: "failed" };
|
|
3046
|
-
if (sessionId
|
|
4958
|
+
if (sessionId === null) {
|
|
4959
|
+
body.opencode_session_id = null;
|
|
4960
|
+
} else if (sessionId !== void 0) {
|
|
4961
|
+
Object.assign(body, this.sessionIdBody(sessionId, conversationId, messageId, "failed"));
|
|
4962
|
+
}
|
|
3047
4963
|
if (error2 !== void 0) body.error = error2;
|
|
4964
|
+
if (usage) Object.assign(body, usage);
|
|
3048
4965
|
await this.callWithRetry(
|
|
3049
4966
|
"marking message as failed",
|
|
3050
4967
|
() => this.fetchImpl(
|
|
3051
|
-
`${this.apiUrl}/
|
|
4968
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
3052
4969
|
{
|
|
3053
4970
|
method: "PATCH",
|
|
3054
4971
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3065,11 +4982,17 @@ var ChannelDriver = class {
|
|
|
3065
4982
|
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
3066
4983
|
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
3067
4984
|
* context (no silent catch, per development-workflow).
|
|
4985
|
+
*
|
|
4986
|
+
* Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
|
|
4987
|
+
* telemetry), but the `paused` liveness-clear uses it to know whether to
|
|
4988
|
+
* RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
|
|
4989
|
+
* stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
|
|
4990
|
+
* leaves liveness").
|
|
3068
4991
|
*/
|
|
3069
4992
|
async postSignal(conversationId, messageId, signal, extra) {
|
|
3070
4993
|
try {
|
|
3071
4994
|
const res = await this.fetchImpl(
|
|
3072
|
-
`${this.apiUrl}/
|
|
4995
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
|
|
3073
4996
|
{
|
|
3074
4997
|
method: "POST",
|
|
3075
4998
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3078,24 +5001,27 @@ var ChannelDriver = class {
|
|
|
3078
5001
|
);
|
|
3079
5002
|
if (!res.ok) {
|
|
3080
5003
|
this.log({
|
|
3081
|
-
level: "
|
|
5004
|
+
level: "warn",
|
|
3082
5005
|
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
|
|
3083
5006
|
conversation_id: conversationId,
|
|
3084
5007
|
message_id: messageId
|
|
3085
5008
|
});
|
|
5009
|
+
return false;
|
|
3086
5010
|
}
|
|
5011
|
+
return true;
|
|
3087
5012
|
} catch (err) {
|
|
3088
5013
|
this.log({
|
|
3089
|
-
level: "
|
|
5014
|
+
level: "warn",
|
|
3090
5015
|
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
|
|
3091
5016
|
conversation_id: conversationId,
|
|
3092
5017
|
message_id: messageId
|
|
3093
5018
|
});
|
|
5019
|
+
return false;
|
|
3094
5020
|
}
|
|
3095
5021
|
}
|
|
3096
5022
|
async persistSession(conversationId, sessionId) {
|
|
3097
5023
|
const res = await this.fetchImpl(
|
|
3098
|
-
`${this.apiUrl}/
|
|
5024
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}`,
|
|
3099
5025
|
{
|
|
3100
5026
|
method: "PATCH",
|
|
3101
5027
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3121,7 +5047,7 @@ var ChannelDriver = class {
|
|
|
3121
5047
|
await this.callWithRetry(
|
|
3122
5048
|
"reporting interactive event",
|
|
3123
5049
|
() => this.fetchImpl(
|
|
3124
|
-
`${this.apiUrl}/
|
|
5050
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/interactive-event`,
|
|
3125
5051
|
{
|
|
3126
5052
|
method: "POST",
|
|
3127
5053
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
@@ -3147,9 +5073,7 @@ var ChannelDriver = class {
|
|
|
3147
5073
|
return false;
|
|
3148
5074
|
}
|
|
3149
5075
|
}
|
|
3150
|
-
// -------------------------------------------------------------------------
|
|
3151
5076
|
// Retry wrapper
|
|
3152
|
-
// -------------------------------------------------------------------------
|
|
3153
5077
|
/**
|
|
3154
5078
|
* Invoke an Evident API call, retrying on transient failures (5xx / 429 /
|
|
3155
5079
|
* network errors) with exponential backoff + jitter (capped). Auth failures
|
|
@@ -3348,7 +5272,7 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
3348
5272
|
if (!response.ok) {
|
|
3349
5273
|
const serverMessage = await readErrorMessage(response);
|
|
3350
5274
|
return {
|
|
3351
|
-
error: `Failed to resolve
|
|
5275
|
+
error: `Failed to resolve runner from key (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
|
|
3352
5276
|
};
|
|
3353
5277
|
}
|
|
3354
5278
|
const data = await response.json();
|
|
@@ -3356,19 +5280,49 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
3356
5280
|
return { agent_id: data.agent_id };
|
|
3357
5281
|
}
|
|
3358
5282
|
return {
|
|
3359
|
-
error: "Cannot resolve
|
|
5283
|
+
error: "Cannot resolve runner ID: auth type is not agent_key. Please provide --agent explicitly."
|
|
3360
5284
|
};
|
|
3361
5285
|
} catch (error2) {
|
|
3362
5286
|
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
3363
|
-
return { error: `Failed to resolve
|
|
5287
|
+
return { error: `Failed to resolve runner from key: ${message}` };
|
|
3364
5288
|
}
|
|
3365
5289
|
}
|
|
5290
|
+
var BEST_EFFORT_NOTIFY_TIMEOUT_MS = 2e3;
|
|
3366
5291
|
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
3367
5292
|
const apiUrl = getApiUrlConfig();
|
|
3368
5293
|
try {
|
|
3369
|
-
const response = await fetch(`${apiUrl}/
|
|
5294
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}/disconnect`, {
|
|
3370
5295
|
method: "POST",
|
|
3371
|
-
headers: { Authorization: authHeader }
|
|
5296
|
+
headers: { Authorization: authHeader },
|
|
5297
|
+
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
5298
|
+
});
|
|
5299
|
+
if (!response.ok) {
|
|
5300
|
+
const serverMessage = await readErrorMessage(response);
|
|
5301
|
+
return {
|
|
5302
|
+
ok: false,
|
|
5303
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
5304
|
+
};
|
|
5305
|
+
}
|
|
5306
|
+
return { ok: true };
|
|
5307
|
+
} catch (error2) {
|
|
5308
|
+
return { ok: false, error: describeBestEffortError(error2) };
|
|
5309
|
+
}
|
|
5310
|
+
}
|
|
5311
|
+
function describeBestEffortError(error2) {
|
|
5312
|
+
const name = error2?.name;
|
|
5313
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
5314
|
+
return `timed out after ${BEST_EFFORT_NOTIFY_TIMEOUT_MS}ms`;
|
|
5315
|
+
}
|
|
5316
|
+
return error2 instanceof Error ? error2.message : String(error2);
|
|
5317
|
+
}
|
|
5318
|
+
async function reportMicrovmId(agentId, authHeader, microvmId) {
|
|
5319
|
+
try {
|
|
5320
|
+
const apiUrl = getApiUrlConfig();
|
|
5321
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}/microvm`, {
|
|
5322
|
+
method: "POST",
|
|
5323
|
+
headers: { Authorization: authHeader, "Content-Type": "application/json" },
|
|
5324
|
+
body: JSON.stringify({ microvm_id: microvmId }),
|
|
5325
|
+
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
3372
5326
|
});
|
|
3373
5327
|
if (!response.ok) {
|
|
3374
5328
|
const serverMessage = await readErrorMessage(response);
|
|
@@ -3379,13 +5333,13 @@ async function notifyAgentDisconnected(agentId, authHeader) {
|
|
|
3379
5333
|
}
|
|
3380
5334
|
return { ok: true };
|
|
3381
5335
|
} catch (error2) {
|
|
3382
|
-
return { ok: false, error:
|
|
5336
|
+
return { ok: false, error: describeBestEffortError(error2) };
|
|
3383
5337
|
}
|
|
3384
5338
|
}
|
|
3385
5339
|
async function getAgentInfo(agentId, authHeader) {
|
|
3386
5340
|
const apiUrl = getApiUrlConfig();
|
|
3387
5341
|
try {
|
|
3388
|
-
const response = await fetch(`${apiUrl}/
|
|
5342
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}`, {
|
|
3389
5343
|
headers: { Authorization: authHeader }
|
|
3390
5344
|
});
|
|
3391
5345
|
if (response.status === 401) {
|
|
@@ -3396,12 +5350,12 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
3396
5350
|
const serverMessage = await readErrorMessage(response);
|
|
3397
5351
|
return {
|
|
3398
5352
|
valid: false,
|
|
3399
|
-
error: serverMessage ?? "You do not have access to this
|
|
5353
|
+
error: serverMessage ?? "You do not have access to this runner (it may belong to a different team or organization)."
|
|
3400
5354
|
};
|
|
3401
5355
|
}
|
|
3402
5356
|
if (response.status === 404) {
|
|
3403
5357
|
const serverMessage = await readErrorMessage(response);
|
|
3404
|
-
return { valid: false, error: serverMessage ?? `
|
|
5358
|
+
return { valid: false, error: serverMessage ?? `Runner ${agentId} not found` };
|
|
3405
5359
|
}
|
|
3406
5360
|
if (!response.ok) {
|
|
3407
5361
|
const serverMessage = await readErrorMessage(response);
|
|
@@ -3414,13 +5368,13 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
3414
5368
|
if (agent.agent_type !== "local") {
|
|
3415
5369
|
return {
|
|
3416
5370
|
valid: false,
|
|
3417
|
-
error: `
|
|
5371
|
+
error: `Runner is type '${agent.agent_type}', must be 'local' for CLI connection`
|
|
3418
5372
|
};
|
|
3419
5373
|
}
|
|
3420
5374
|
return { valid: true, agent };
|
|
3421
5375
|
} catch (error2) {
|
|
3422
5376
|
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
3423
|
-
return { valid: false, error: `Failed to validate
|
|
5377
|
+
return { valid: false, error: `Failed to validate runner: ${message}` };
|
|
3424
5378
|
}
|
|
3425
5379
|
}
|
|
3426
5380
|
|
|
@@ -3429,23 +5383,82 @@ var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
|
3429
5383
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
3430
5384
|
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
3431
5385
|
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
3432
|
-
|
|
5386
|
+
var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
5387
|
+
function resolveLogLevel(options) {
|
|
5388
|
+
const accepted = Object.keys(LOG_LEVELS);
|
|
5389
|
+
const validate = (value, source) => {
|
|
5390
|
+
const normalized = value.trim().toLowerCase();
|
|
5391
|
+
if (!accepted.includes(normalized)) {
|
|
5392
|
+
throw new Error(
|
|
5393
|
+
`Invalid log level "${value}"${source}; expected one of ${accepted.join(", ")}`
|
|
5394
|
+
);
|
|
5395
|
+
}
|
|
5396
|
+
return normalized;
|
|
5397
|
+
};
|
|
5398
|
+
if (options.logLevel !== void 0) {
|
|
5399
|
+
return validate(options.logLevel, " (--log-level)");
|
|
5400
|
+
}
|
|
5401
|
+
if (options.verbose) {
|
|
5402
|
+
return "debug";
|
|
5403
|
+
}
|
|
5404
|
+
const env = process.env.EVIDENT_LOG_LEVEL;
|
|
5405
|
+
if (env !== void 0 && env !== "") {
|
|
5406
|
+
return validate(env, " (EVIDENT_LOG_LEVEL)");
|
|
5407
|
+
}
|
|
5408
|
+
return "info";
|
|
5409
|
+
}
|
|
5410
|
+
function resolveFileSyncDirectories(raw, homeDir) {
|
|
5411
|
+
const directories = [];
|
|
5412
|
+
for (const entry of raw ?? []) {
|
|
5413
|
+
const trimmed = entry.trim();
|
|
5414
|
+
if (trimmed === "") {
|
|
5415
|
+
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
5416
|
+
}
|
|
5417
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join2(homeDir, trimmed.slice(2)) : trimmed;
|
|
5418
|
+
if (!isAbsolute2(expanded)) {
|
|
5419
|
+
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
5420
|
+
}
|
|
5421
|
+
const normalized = resolvePath(expanded);
|
|
5422
|
+
if (parse(normalized).root === normalized) {
|
|
5423
|
+
throw new Error(
|
|
5424
|
+
`--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
|
|
5425
|
+
);
|
|
5426
|
+
}
|
|
5427
|
+
if (!directories.includes(normalized)) {
|
|
5428
|
+
directories.push(normalized);
|
|
5429
|
+
}
|
|
5430
|
+
}
|
|
5431
|
+
if (directories.length > MAX_FILE_SYNC_DIRECTORIES) {
|
|
5432
|
+
throw new Error(
|
|
5433
|
+
`--enable-file-sync-to accepts at most ${MAX_FILE_SYNC_DIRECTORIES} directories; got ${directories.length}`
|
|
5434
|
+
);
|
|
5435
|
+
}
|
|
5436
|
+
return directories;
|
|
5437
|
+
}
|
|
5438
|
+
function meetsThreshold(state, level) {
|
|
5439
|
+
return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
|
|
5440
|
+
}
|
|
5441
|
+
function log2(state, message, level = "info") {
|
|
5442
|
+
if (!meetsThreshold(state, level)) return;
|
|
3433
5443
|
if (state.json) {
|
|
3434
5444
|
console.log(
|
|
3435
5445
|
JSON.stringify({
|
|
3436
5446
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3437
|
-
level
|
|
5447
|
+
level,
|
|
3438
5448
|
message
|
|
3439
5449
|
})
|
|
3440
5450
|
);
|
|
3441
5451
|
} else if (!state.interactive) {
|
|
3442
|
-
const prefix =
|
|
5452
|
+
const prefix = level === "error" ? chalk6.red("\u2717") : level === "warn" ? chalk6.yellow("!") : level === "debug" ? chalk6.dim("\xB7") : chalk6.green("\u2022");
|
|
3443
5453
|
console.log(`${prefix} ${message}`);
|
|
3444
5454
|
}
|
|
3445
5455
|
}
|
|
3446
5456
|
function logActivity(state, entry) {
|
|
5457
|
+
const level = entry.level ?? (entry.type === "error" ? "error" : "info");
|
|
5458
|
+
if (!meetsThreshold(state, level)) return;
|
|
3447
5459
|
const fullEntry = {
|
|
3448
5460
|
...entry,
|
|
5461
|
+
level,
|
|
3449
5462
|
timestamp: /* @__PURE__ */ new Date()
|
|
3450
5463
|
};
|
|
3451
5464
|
state.activityLog.push(fullEntry);
|
|
@@ -3454,9 +5467,9 @@ function logActivity(state, entry) {
|
|
|
3454
5467
|
}
|
|
3455
5468
|
if (!state.interactive) {
|
|
3456
5469
|
if (entry.type === "error") {
|
|
3457
|
-
log2(state, entry.error ?? "Unknown error",
|
|
3458
|
-
} else if (entry.
|
|
3459
|
-
log2(state, entry.message);
|
|
5470
|
+
log2(state, entry.error ?? "Unknown error", level);
|
|
5471
|
+
} else if (entry.message) {
|
|
5472
|
+
log2(state, entry.message, level);
|
|
3460
5473
|
}
|
|
3461
5474
|
}
|
|
3462
5475
|
}
|
|
@@ -3543,18 +5556,29 @@ async function handleAuthError(state, error2) {
|
|
|
3543
5556
|
async function driveChannels(state, driver) {
|
|
3544
5557
|
let idlePolls = 0;
|
|
3545
5558
|
let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
5559
|
+
let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
3546
5560
|
while (state.running) {
|
|
3547
5561
|
if (state.connection?.reconnecting && state.connection.reconnectPromise) {
|
|
3548
5562
|
logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
|
|
3549
5563
|
if (state.interactive) displayStatus(state);
|
|
3550
5564
|
await state.connection.reconnectPromise;
|
|
3551
5565
|
}
|
|
5566
|
+
const carriedOverFileSync = driver.fileSyncActivity().inFlight;
|
|
5567
|
+
void driver.syncPendingFiles().catch(
|
|
5568
|
+
(error2) => logActivity(state, {
|
|
5569
|
+
type: "error",
|
|
5570
|
+
error: `Runner file sync failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
5571
|
+
})
|
|
5572
|
+
);
|
|
3552
5573
|
try {
|
|
3553
5574
|
const processed = await driver.drainPending();
|
|
3554
5575
|
state.messageCount += processed;
|
|
3555
5576
|
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
3556
5577
|
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
3557
|
-
|
|
5578
|
+
const appliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
5579
|
+
const fileActivity = carriedOverFileSync || appliedFiles !== lastSeenAppliedFiles;
|
|
5580
|
+
lastSeenAppliedFiles = appliedFiles;
|
|
5581
|
+
if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity || fileActivity) {
|
|
3558
5582
|
idlePolls = 0;
|
|
3559
5583
|
if (processed > 0 && state.interactive) displayStatus(state);
|
|
3560
5584
|
} else if (state.idleTimeout !== null) {
|
|
@@ -3583,7 +5607,7 @@ async function driveChannels(state, driver) {
|
|
|
3583
5607
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
|
|
3584
5608
|
if (state.interactive) displayStatus(state);
|
|
3585
5609
|
}
|
|
3586
|
-
await new Promise((
|
|
5610
|
+
await new Promise((resolve3) => setTimeout(resolve3, CHANNEL_POLL_INTERVAL_MS));
|
|
3587
5611
|
if (state.idleTimeout !== null && idlePolls >= 2) {
|
|
3588
5612
|
const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
|
|
3589
5613
|
if (idleMs > state.idleTimeout * 1e3) {
|
|
@@ -3594,6 +5618,81 @@ async function driveChannels(state, driver) {
|
|
|
3594
5618
|
}
|
|
3595
5619
|
}
|
|
3596
5620
|
}
|
|
5621
|
+
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
5622
|
+
async function runSweep(state, driver, config2) {
|
|
5623
|
+
const mode = `age=${config2.maxAgeMs ?? "\u2014"} count=${config2.maxCount ?? "\u2014"}`;
|
|
5624
|
+
try {
|
|
5625
|
+
const sessions = await listSessions(state.port);
|
|
5626
|
+
if (sessions === null) {
|
|
5627
|
+
logActivity(state, {
|
|
5628
|
+
type: "info",
|
|
5629
|
+
message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
|
|
5630
|
+
});
|
|
5631
|
+
return;
|
|
5632
|
+
}
|
|
5633
|
+
const toDelete = selectSessionsToDelete(
|
|
5634
|
+
sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
|
|
5635
|
+
{
|
|
5636
|
+
maxAgeMs: config2.maxAgeMs,
|
|
5637
|
+
maxCount: config2.maxCount,
|
|
5638
|
+
nowMs: Date.now(),
|
|
5639
|
+
protectedIds: driver.protectedSessionIds()
|
|
5640
|
+
}
|
|
5641
|
+
);
|
|
5642
|
+
const protectedNow = driver.protectedSessionIds();
|
|
5643
|
+
let deleted = 0;
|
|
5644
|
+
let failed = 0;
|
|
5645
|
+
let skippedNewlyActive = 0;
|
|
5646
|
+
for (const id of toDelete) {
|
|
5647
|
+
if (protectedNow.has(id)) {
|
|
5648
|
+
skippedNewlyActive++;
|
|
5649
|
+
logActivity(state, {
|
|
5650
|
+
type: "info",
|
|
5651
|
+
message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
|
|
5652
|
+
});
|
|
5653
|
+
continue;
|
|
5654
|
+
}
|
|
5655
|
+
if (await deleteSession(state.port, id)) deleted++;
|
|
5656
|
+
else failed++;
|
|
5657
|
+
}
|
|
5658
|
+
const failedNote = failed > 0 ? `, failed ${failed}` : "";
|
|
5659
|
+
const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
|
|
5660
|
+
logActivity(state, {
|
|
5661
|
+
type: "info",
|
|
5662
|
+
message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
|
|
5663
|
+
});
|
|
5664
|
+
} catch (error2) {
|
|
5665
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
5666
|
+
logActivity(state, {
|
|
5667
|
+
type: "error",
|
|
5668
|
+
error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
|
|
5669
|
+
});
|
|
5670
|
+
}
|
|
5671
|
+
}
|
|
5672
|
+
function scheduleSessionCleanup(state, driver, options) {
|
|
5673
|
+
const config2 = resolveSessionCleanupConfig(
|
|
5674
|
+
{
|
|
5675
|
+
maxAge: options.sessionCleanupMaxAge,
|
|
5676
|
+
maxCount: options.sessionCleanupMaxCount,
|
|
5677
|
+
interval: options.sessionCleanupInterval
|
|
5678
|
+
},
|
|
5679
|
+
process.env
|
|
5680
|
+
);
|
|
5681
|
+
for (const warning2 of config2.warnings) {
|
|
5682
|
+
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
5683
|
+
}
|
|
5684
|
+
if (!config2.enabled) return;
|
|
5685
|
+
logActivity(state, {
|
|
5686
|
+
type: "info",
|
|
5687
|
+
message: `Session cleanup enabled (age=${config2.maxAgeMs ?? "\u2014"}, count=${config2.maxCount ?? "\u2014"}, interval=${config2.intervalMs}ms)`
|
|
5688
|
+
});
|
|
5689
|
+
const interval = setInterval(() => void runSweep(state, driver, config2), config2.intervalMs);
|
|
5690
|
+
const firstSweep = setTimeout(
|
|
5691
|
+
() => void runSweep(state, driver, config2),
|
|
5692
|
+
SESSION_CLEANUP_FIRST_SWEEP_MS
|
|
5693
|
+
);
|
|
5694
|
+
state.sessionCleanupTimers.push(interval, firstSweep);
|
|
5695
|
+
}
|
|
3597
5696
|
async function notifyOffline(state) {
|
|
3598
5697
|
if (!state.agentId || !state.authHeader) return;
|
|
3599
5698
|
if (!state.connected) {
|
|
@@ -3602,7 +5701,7 @@ async function notifyOffline(state) {
|
|
|
3602
5701
|
}
|
|
3603
5702
|
const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
|
|
3604
5703
|
if (result.ok) {
|
|
3605
|
-
log2(state, "Notified Evident the
|
|
5704
|
+
log2(state, "Notified Evident the runner is going offline");
|
|
3606
5705
|
} else {
|
|
3607
5706
|
logActivity(state, {
|
|
3608
5707
|
type: "error",
|
|
@@ -3611,8 +5710,24 @@ async function notifyOffline(state) {
|
|
|
3611
5710
|
if (state.interactive) displayStatus(state);
|
|
3612
5711
|
}
|
|
3613
5712
|
}
|
|
5713
|
+
async function timeShutdownPhase(state, durations, name, run2) {
|
|
5714
|
+
const startedAt = Date.now();
|
|
5715
|
+
try {
|
|
5716
|
+
return await run2();
|
|
5717
|
+
} finally {
|
|
5718
|
+
const elapsedMs = Date.now() - startedAt;
|
|
5719
|
+
durations[name] = elapsedMs;
|
|
5720
|
+
log2(state, `Shutdown phase ${name}: ${elapsedMs}ms`);
|
|
5721
|
+
}
|
|
5722
|
+
}
|
|
3614
5723
|
async function cleanup(state, opts = {}) {
|
|
5724
|
+
const durations = {};
|
|
3615
5725
|
state.running = false;
|
|
5726
|
+
for (const timer of state.sessionCleanupTimers) {
|
|
5727
|
+
clearInterval(timer);
|
|
5728
|
+
clearTimeout(timer);
|
|
5729
|
+
}
|
|
5730
|
+
state.sessionCleanupTimers = [];
|
|
3616
5731
|
if (opts.graceful && state.channelDriver) {
|
|
3617
5732
|
state.channelDriver.stop();
|
|
3618
5733
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
@@ -3620,7 +5735,13 @@ async function cleanup(state, opts = {}) {
|
|
|
3620
5735
|
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
3621
5736
|
displayStatus(state);
|
|
3622
5737
|
}
|
|
3623
|
-
const
|
|
5738
|
+
const driver = state.channelDriver;
|
|
5739
|
+
const settled = await timeShutdownPhase(
|
|
5740
|
+
state,
|
|
5741
|
+
durations,
|
|
5742
|
+
"drain",
|
|
5743
|
+
() => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
|
|
5744
|
+
);
|
|
3624
5745
|
if (!settled) {
|
|
3625
5746
|
logActivity(state, {
|
|
3626
5747
|
type: "info",
|
|
@@ -3629,13 +5750,15 @@ async function cleanup(state, opts = {}) {
|
|
|
3629
5750
|
if (state.interactive) displayStatus(state);
|
|
3630
5751
|
}
|
|
3631
5752
|
}
|
|
3632
|
-
await notifyOffline(state);
|
|
5753
|
+
await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
|
|
3633
5754
|
if (state.connection) {
|
|
3634
|
-
state.connection
|
|
5755
|
+
const connection = state.connection;
|
|
5756
|
+
await timeShutdownPhase(state, durations, "tunnel_close", () => connection.close());
|
|
3635
5757
|
state.connection = null;
|
|
3636
5758
|
}
|
|
3637
5759
|
if (state.opencodeProcess) {
|
|
3638
|
-
|
|
5760
|
+
const opencodeProcess = state.opencodeProcess;
|
|
5761
|
+
await timeShutdownPhase(state, durations, "opencode_stop", () => stopOpenCode(opencodeProcess));
|
|
3639
5762
|
if (state.interactive) {
|
|
3640
5763
|
logActivity(state, { type: "info", message: "Stopped OpenCode process" });
|
|
3641
5764
|
displayStatus(state);
|
|
@@ -3644,17 +5767,35 @@ async function cleanup(state, opts = {}) {
|
|
|
3644
5767
|
}
|
|
3645
5768
|
state.opencodeProcess = null;
|
|
3646
5769
|
}
|
|
5770
|
+
return durations;
|
|
3647
5771
|
}
|
|
3648
5772
|
async function run(options) {
|
|
3649
5773
|
const interactive = isInteractive(options.json);
|
|
5774
|
+
let logLevel;
|
|
5775
|
+
let fileSyncDirectories;
|
|
5776
|
+
try {
|
|
5777
|
+
logLevel = resolveLogLevel(options);
|
|
5778
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir2());
|
|
5779
|
+
} catch (error2) {
|
|
5780
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
5781
|
+
if (options.json) {
|
|
5782
|
+
console.log(JSON.stringify({ status: "error", error: message }));
|
|
5783
|
+
} else {
|
|
5784
|
+
printError(message);
|
|
5785
|
+
}
|
|
5786
|
+
await shutdownTelemetry();
|
|
5787
|
+
process.exit(1);
|
|
5788
|
+
return;
|
|
5789
|
+
}
|
|
3650
5790
|
const state = {
|
|
3651
|
-
agentId: options.agent || "",
|
|
5791
|
+
agentId: options.runner || options.agent || "",
|
|
3652
5792
|
agentName: null,
|
|
3653
5793
|
port: options.port ?? 4096,
|
|
3654
5794
|
conversationFilter: options.conversation ?? null,
|
|
3655
5795
|
idleTimeout: options.idleTimeout ?? null,
|
|
3656
5796
|
json: options.json ?? false,
|
|
3657
5797
|
interactive,
|
|
5798
|
+
logLevel,
|
|
3658
5799
|
connected: false,
|
|
3659
5800
|
opencodeConnected: false,
|
|
3660
5801
|
opencodeVersion: null,
|
|
@@ -3666,26 +5807,69 @@ async function run(options) {
|
|
|
3666
5807
|
activityLog: [],
|
|
3667
5808
|
messageCount: 0,
|
|
3668
5809
|
lastProxiedActivityAt: null,
|
|
5810
|
+
sessionCleanupTimers: [],
|
|
3669
5811
|
authHeader: ""
|
|
3670
5812
|
};
|
|
5813
|
+
if (fileSyncDirectories.length > 0) {
|
|
5814
|
+
log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
|
|
5815
|
+
} else {
|
|
5816
|
+
log2(state, "File sync is disabled (no --enable-file-sync-to given)", "debug");
|
|
5817
|
+
}
|
|
5818
|
+
if (!options.runner && options.agent) {
|
|
5819
|
+
telemetry.info(
|
|
5820
|
+
EventTypes.DEPRECATED_AGENT_FLAG_USED,
|
|
5821
|
+
"Deprecated --agent flag used instead of --runner",
|
|
5822
|
+
{ command: "run" },
|
|
5823
|
+
state.agentId
|
|
5824
|
+
);
|
|
5825
|
+
const agentFlagNotice = "--agent is deprecated, use --runner instead; will be removed in a future release.";
|
|
5826
|
+
log2(state, agentFlagNotice, "warn");
|
|
5827
|
+
if (state.interactive && !state.json) {
|
|
5828
|
+
logActivity(state, { type: "info", level: "warn", message: agentFlagNotice });
|
|
5829
|
+
}
|
|
5830
|
+
}
|
|
3671
5831
|
if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
|
|
3672
5832
|
log2(
|
|
3673
5833
|
state,
|
|
3674
|
-
"
|
|
3675
|
-
|
|
5834
|
+
"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.",
|
|
5835
|
+
"warn"
|
|
3676
5836
|
);
|
|
3677
5837
|
}
|
|
3678
5838
|
const handleSignal = async () => {
|
|
3679
5839
|
if (state.shuttingDown) return;
|
|
3680
5840
|
state.shuttingDown = true;
|
|
5841
|
+
const shutdownStartedAt = Date.now();
|
|
3681
5842
|
if (state.interactive) {
|
|
3682
5843
|
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
3683
5844
|
displayStatus(state);
|
|
3684
5845
|
} else {
|
|
3685
5846
|
log2(state, "Shutting down...");
|
|
3686
5847
|
}
|
|
3687
|
-
await cleanup(state, { graceful: true });
|
|
3688
|
-
|
|
5848
|
+
const durations = await cleanup(state, { graceful: true });
|
|
5849
|
+
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
5850
|
+
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
5851
|
+
let timer;
|
|
5852
|
+
const flushed = shutdownTelemetry().then(
|
|
5853
|
+
() => true,
|
|
5854
|
+
(error2) => {
|
|
5855
|
+
log2(
|
|
5856
|
+
state,
|
|
5857
|
+
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
5858
|
+
"warn"
|
|
5859
|
+
);
|
|
5860
|
+
return true;
|
|
5861
|
+
}
|
|
5862
|
+
);
|
|
5863
|
+
const timedOut = new Promise((resolve3) => {
|
|
5864
|
+
timer = setTimeout(() => resolve3(false), telemetryBudgetMs);
|
|
5865
|
+
});
|
|
5866
|
+
if (!await Promise.race([flushed, timedOut])) {
|
|
5867
|
+
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
5868
|
+
}
|
|
5869
|
+
clearTimeout(timer);
|
|
5870
|
+
});
|
|
5871
|
+
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
5872
|
+
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
3689
5873
|
process.exit(0);
|
|
3690
5874
|
};
|
|
3691
5875
|
process.on("SIGINT", handleSignal);
|
|
@@ -3696,7 +5880,9 @@ async function run(options) {
|
|
|
3696
5880
|
if (!interactive) {
|
|
3697
5881
|
printError("Authentication required");
|
|
3698
5882
|
blank();
|
|
3699
|
-
console.log(
|
|
5883
|
+
console.log(
|
|
5884
|
+
chalk6.dim("Set EVIDENT_RUNNER_KEY (or EVIDENT_AGENT_KEY) environment variable for CI")
|
|
5885
|
+
);
|
|
3700
5886
|
console.log(chalk6.dim("Or run `evident login` for interactive authentication"));
|
|
3701
5887
|
blank();
|
|
3702
5888
|
process.exit(1);
|
|
@@ -3710,26 +5896,51 @@ async function run(options) {
|
|
|
3710
5896
|
);
|
|
3711
5897
|
}
|
|
3712
5898
|
state.authHeader = getAuthHeader(credentials2);
|
|
5899
|
+
if (credentials2.notice) {
|
|
5900
|
+
log2(state, credentials2.notice, "warn");
|
|
5901
|
+
if (state.interactive && !state.json) {
|
|
5902
|
+
logActivity(state, { type: "info", level: "warn", message: credentials2.notice });
|
|
5903
|
+
}
|
|
5904
|
+
}
|
|
5905
|
+
if (credentials2.keySource === "agent_key") {
|
|
5906
|
+
telemetry.info(
|
|
5907
|
+
EventTypes.DEPRECATED_AGENT_KEY_ENV_USED,
|
|
5908
|
+
"Deprecated EVIDENT_AGENT_KEY env var used instead of EVIDENT_RUNNER_KEY",
|
|
5909
|
+
{ command: "run" },
|
|
5910
|
+
state.agentId
|
|
5911
|
+
);
|
|
5912
|
+
const agentKeyNotice = "EVIDENT_AGENT_KEY is deprecated, use EVIDENT_RUNNER_KEY instead; will be removed in a future release.";
|
|
5913
|
+
log2(state, agentKeyNotice, "warn");
|
|
5914
|
+
if (state.interactive && !state.json) {
|
|
5915
|
+
logActivity(state, { type: "info", level: "warn", message: agentKeyNotice });
|
|
5916
|
+
}
|
|
5917
|
+
}
|
|
3713
5918
|
if (!state.agentId) {
|
|
3714
5919
|
if (credentials2.authType === "agent_key") {
|
|
3715
5920
|
const resolved = await resolveAgentIdFromKey(state.authHeader);
|
|
3716
5921
|
if (resolved.agent_id) {
|
|
3717
5922
|
state.agentId = resolved.agent_id;
|
|
3718
|
-
log2(state, `Resolved
|
|
5923
|
+
log2(state, `Resolved runner ID from key: ${state.agentId}`);
|
|
3719
5924
|
if (state.interactive && !state.json) {
|
|
3720
5925
|
logActivity(state, {
|
|
3721
5926
|
type: "info",
|
|
3722
|
-
message: `
|
|
5927
|
+
message: `Runner ID resolved from key: ${state.agentId}`
|
|
3723
5928
|
});
|
|
3724
5929
|
}
|
|
3725
5930
|
} else {
|
|
3726
|
-
printError(resolved.error || "Failed to resolve
|
|
5931
|
+
printError(resolved.error || "Failed to resolve runner ID from key");
|
|
3727
5932
|
process.exit(1);
|
|
3728
5933
|
}
|
|
3729
5934
|
} else {
|
|
3730
|
-
printError(
|
|
5935
|
+
printError(
|
|
5936
|
+
"--runner (or --agent) is required when not using EVIDENT_RUNNER_KEY or EVIDENT_AGENT_KEY"
|
|
5937
|
+
);
|
|
3731
5938
|
blank();
|
|
3732
|
-
console.log(
|
|
5939
|
+
console.log(
|
|
5940
|
+
chalk6.dim(
|
|
5941
|
+
"Either provide --runner/--agent <id> or set EVIDENT_RUNNER_KEY/EVIDENT_AGENT_KEY"
|
|
5942
|
+
)
|
|
5943
|
+
);
|
|
3733
5944
|
blank();
|
|
3734
5945
|
process.exit(1);
|
|
3735
5946
|
}
|
|
@@ -3751,7 +5962,7 @@ async function run(options) {
|
|
|
3751
5962
|
console.log(chalk6.bold("Evident Run"));
|
|
3752
5963
|
console.log(chalk6.dim("-".repeat(40)));
|
|
3753
5964
|
}
|
|
3754
|
-
const spinner = interactive && !state.json ? ora3("Validating
|
|
5965
|
+
const spinner = interactive && !state.json ? ora3("Validating runner...").start() : null;
|
|
3755
5966
|
let validation = await getAgentInfo(state.agentId, state.authHeader);
|
|
3756
5967
|
if (!validation.valid && validation.authFailed && interactive) {
|
|
3757
5968
|
spinner?.fail("Authentication failed");
|
|
@@ -3763,15 +5974,30 @@ async function run(options) {
|
|
|
3763
5974
|
"Login successful! Retrying..."
|
|
3764
5975
|
);
|
|
3765
5976
|
state.authHeader = getAuthHeader(credentials2);
|
|
3766
|
-
spinner?.start("Validating
|
|
5977
|
+
spinner?.start("Validating runner...");
|
|
3767
5978
|
validation = await getAgentInfo(state.agentId, state.authHeader);
|
|
3768
5979
|
}
|
|
3769
5980
|
if (!validation.valid) {
|
|
3770
|
-
spinner?.fail(`
|
|
5981
|
+
spinner?.fail(`Runner validation failed: ${validation.error}`);
|
|
3771
5982
|
throw new Error(validation.error);
|
|
3772
5983
|
}
|
|
3773
|
-
spinner?.succeed(`
|
|
5984
|
+
spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
|
|
3774
5985
|
state.agentName = validation.agent.name;
|
|
5986
|
+
const microvmId = process.env.MICROVM_ID?.trim();
|
|
5987
|
+
if (microvmId) {
|
|
5988
|
+
const reported = await reportMicrovmId(state.agentId, state.authHeader, microvmId);
|
|
5989
|
+
if (reported.ok) {
|
|
5990
|
+
log2(state, "Reported MicroVM identity so this runner can be resumed rather than restarted");
|
|
5991
|
+
} else {
|
|
5992
|
+
const message = `Could not report MicroVM identity (future wakes will cold-start): ${reported.error}`;
|
|
5993
|
+
log2(state, message, "warn");
|
|
5994
|
+
if (state.interactive && !state.json) {
|
|
5995
|
+
logActivity(state, { type: "info", level: "warn", message });
|
|
5996
|
+
}
|
|
5997
|
+
}
|
|
5998
|
+
} else {
|
|
5999
|
+
log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
|
|
6000
|
+
}
|
|
3775
6001
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
3776
6002
|
try {
|
|
3777
6003
|
const oc = await ensureOpenCodeRunning({
|
|
@@ -3788,9 +6014,24 @@ async function run(options) {
|
|
|
3788
6014
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
3789
6015
|
const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
|
|
3790
6016
|
if (versionWarning) {
|
|
3791
|
-
log2(state, versionWarning,
|
|
6017
|
+
log2(state, versionWarning, "warn");
|
|
6018
|
+
if (state.interactive && !state.json) {
|
|
6019
|
+
logActivity(state, { type: "info", level: "warn", message: versionWarning });
|
|
6020
|
+
}
|
|
6021
|
+
}
|
|
6022
|
+
const noProviderWarning = buildNoProviderWarning(await hasAnyConfiguredProvider(state.port));
|
|
6023
|
+
if (noProviderWarning) {
|
|
6024
|
+
log2(state, noProviderWarning, "warn");
|
|
3792
6025
|
if (state.interactive && !state.json) {
|
|
3793
|
-
logActivity(state, { type: "info", message:
|
|
6026
|
+
logActivity(state, { type: "info", level: "warn", message: noProviderWarning });
|
|
6027
|
+
blank();
|
|
6028
|
+
console.log(chalk6.yellow("\u26A0 No OpenCode model provider is configured."));
|
|
6029
|
+
console.log(
|
|
6030
|
+
chalk6.dim(
|
|
6031
|
+
`Run ${chalk6.cyan("opencode auth login")} to set one up \u2014 messages will fail until then.`
|
|
6032
|
+
)
|
|
6033
|
+
);
|
|
6034
|
+
blank();
|
|
3794
6035
|
}
|
|
3795
6036
|
}
|
|
3796
6037
|
} catch (error2) {
|
|
@@ -3805,11 +6046,21 @@ async function run(options) {
|
|
|
3805
6046
|
getAuthHeader: () => state.authHeader,
|
|
3806
6047
|
conversationFilter: state.conversationFilter,
|
|
3807
6048
|
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
6049
|
+
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
6050
|
+
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
6051
|
+
fileSyncDirectories,
|
|
6052
|
+
homeDir: homedir2(),
|
|
6053
|
+
log: (entry) => (
|
|
6054
|
+
// Thread the driver's real level straight through so `debug`/`warn`
|
|
6055
|
+
// survive the sink filter (they no longer collapse to info). `type`
|
|
6056
|
+
// stays the coarse error/non-error split the activity log renders with.
|
|
6057
|
+
logActivity(state, {
|
|
6058
|
+
type: entry.level === "error" ? "error" : "info",
|
|
6059
|
+
level: entry.level,
|
|
6060
|
+
message: entry.message,
|
|
6061
|
+
error: entry.level === "error" ? entry.message : void 0
|
|
6062
|
+
})
|
|
6063
|
+
)
|
|
3813
6064
|
});
|
|
3814
6065
|
state.channelDriver = channelDriver;
|
|
3815
6066
|
const connection = new RunnerConnection({
|
|
@@ -3823,7 +6074,7 @@ async function run(options) {
|
|
|
3823
6074
|
state.agentId = agentId;
|
|
3824
6075
|
logActivity(state, {
|
|
3825
6076
|
type: "info",
|
|
3826
|
-
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (
|
|
6077
|
+
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (runner: ${agentId})`
|
|
3827
6078
|
});
|
|
3828
6079
|
emitAgentConnected(state.agentId, {
|
|
3829
6080
|
port: state.port,
|
|
@@ -3880,6 +6131,12 @@ async function run(options) {
|
|
|
3880
6131
|
onDrainPing: () => {
|
|
3881
6132
|
if (!state.running) return;
|
|
3882
6133
|
logActivity(state, { type: "info", message: "Drain ping received \u2014 draining" });
|
|
6134
|
+
void channelDriver.syncPendingFiles().catch(
|
|
6135
|
+
(error2) => logActivity(state, {
|
|
6136
|
+
type: "error",
|
|
6137
|
+
error: `Runner file sync failed on ping: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
6138
|
+
})
|
|
6139
|
+
);
|
|
3883
6140
|
channelDriver.drainPending().then((processed) => {
|
|
3884
6141
|
if (processed > 0) {
|
|
3885
6142
|
state.messageCount += processed;
|
|
@@ -3908,6 +6165,7 @@ async function run(options) {
|
|
|
3908
6165
|
if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
|
|
3909
6166
|
throw error2;
|
|
3910
6167
|
}
|
|
6168
|
+
scheduleSessionCleanup(state, channelDriver, options);
|
|
3911
6169
|
if (!interactive || state.json) {
|
|
3912
6170
|
log2(state, "Driving channel messages...");
|
|
3913
6171
|
}
|
|
@@ -3937,7 +6195,7 @@ async function run(options) {
|
|
|
3937
6195
|
}
|
|
3938
6196
|
telemetry.error(EventTypes.CLI_ERROR, `Run command failed: ${message}`, {
|
|
3939
6197
|
command: "run",
|
|
3940
|
-
agentId: options.agent
|
|
6198
|
+
agentId: options.runner || options.agent
|
|
3941
6199
|
});
|
|
3942
6200
|
await shutdownTelemetry();
|
|
3943
6201
|
process.exit(1);
|
|
@@ -3962,15 +6220,46 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
|
|
|
3962
6220
|
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
6221
|
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
6222
|
program.command("whoami").description("Show the currently logged in user").action(whoami);
|
|
3965
|
-
program.command("run").description("Connect to Evident and process messages").option("
|
|
6223
|
+
program.command("run").description("Connect to Evident and process messages").option("--runner [id]", "Runner ID to connect to (optional when EVIDENT_RUNNER_KEY is set)").option(
|
|
6224
|
+
"-a, --agent [id]",
|
|
6225
|
+
"Deprecated alias for --runner (still supported; --runner wins if both are given)"
|
|
6226
|
+
).option("-p, --port <port>", "OpenCode port (default: 4096)", "4096").option(
|
|
6227
|
+
"--log-level <level>",
|
|
6228
|
+
"Log verbosity: debug | info | warn | error (default: info). Env: EVIDENT_LOG_LEVEL"
|
|
6229
|
+
).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(
|
|
6230
|
+
"--session-cleanup-max-age <duration>",
|
|
6231
|
+
"Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
|
|
6232
|
+
).option(
|
|
6233
|
+
"--session-cleanup-max-count <n>",
|
|
6234
|
+
"Keep only the newest N OpenCode sessions. Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_COUNT"
|
|
6235
|
+
).option(
|
|
6236
|
+
"--session-cleanup-interval <duration>",
|
|
6237
|
+
"How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
|
|
6238
|
+
).option(
|
|
6239
|
+
"--enable-file-sync-to <dir>",
|
|
6240
|
+
"Allow Evident to write files into this directory (repeatable). Omit to disable file sync entirely.",
|
|
6241
|
+
(value, previous) => previous.concat([value]),
|
|
6242
|
+
[]
|
|
6243
|
+
).action(
|
|
3966
6244
|
(options) => {
|
|
3967
6245
|
run({
|
|
3968
6246
|
agent: options.agent,
|
|
6247
|
+
runner: options.runner,
|
|
3969
6248
|
port: parseInt(options.port, 10),
|
|
6249
|
+
// Raw string — validation/precedence is single-sourced in run.ts's
|
|
6250
|
+
// resolveLogLevel (flag > -v > EVIDENT_LOG_LEVEL > info).
|
|
6251
|
+
logLevel: options.logLevel,
|
|
3970
6252
|
verbose: options.verbose,
|
|
3971
6253
|
conversation: options.conversation,
|
|
3972
6254
|
idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
|
|
3973
|
-
json: options.json
|
|
6255
|
+
json: options.json,
|
|
6256
|
+
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
6257
|
+
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
6258
|
+
sessionCleanupMaxCount: options.sessionCleanupMaxCount,
|
|
6259
|
+
sessionCleanupInterval: options.sessionCleanupInterval,
|
|
6260
|
+
// Raw values — expansion/validation is single-sourced in run.ts's
|
|
6261
|
+
// resolveFileSyncDirectories.
|
|
6262
|
+
enableFileSyncTo: options.enableFileSyncTo
|
|
3974
6263
|
});
|
|
3975
6264
|
}
|
|
3976
6265
|
);
|