@evident-ai/cli 3.1.1-dev.716aea1 → 3.1.1-dev.79f77c7
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 +15 -11
- package/dist/index.js +1210 -104
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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() {
|
|
@@ -719,7 +765,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
719
765
|
if (health.healthy) {
|
|
720
766
|
return health;
|
|
721
767
|
}
|
|
722
|
-
await new Promise((
|
|
768
|
+
await new Promise((resolve3) => setTimeout(resolve3, 1e3));
|
|
723
769
|
}
|
|
724
770
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
725
771
|
}
|
|
@@ -1267,6 +1313,16 @@ async function buildFileParts(attachments, capable) {
|
|
|
1267
1313
|
);
|
|
1268
1314
|
dataUrl = null;
|
|
1269
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
|
+
}
|
|
1270
1326
|
if (dataUrl == null) {
|
|
1271
1327
|
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "failed" });
|
|
1272
1328
|
continue;
|
|
@@ -1348,7 +1404,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
1348
1404
|
}
|
|
1349
1405
|
}
|
|
1350
1406
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
1351
|
-
await new Promise((
|
|
1407
|
+
await new Promise((resolve3) => setTimeout(resolve3, READ_BACK_DELAY_MS));
|
|
1352
1408
|
}
|
|
1353
1409
|
}
|
|
1354
1410
|
return null;
|
|
@@ -1476,6 +1532,9 @@ function isPreamblePinnedRunning(messages, userMessageId) {
|
|
|
1476
1532
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1477
1533
|
return completedOf(reply) != null && finishOf(reply) === "tool-calls";
|
|
1478
1534
|
}
|
|
1535
|
+
function isB2AbandonmentConfirmed(params) {
|
|
1536
|
+
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
|
|
1537
|
+
}
|
|
1479
1538
|
function messageError(messages, userMessageId) {
|
|
1480
1539
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1481
1540
|
const error2 = errorOf(reply);
|
|
@@ -1684,10 +1743,11 @@ var StreamForwarder = class {
|
|
|
1684
1743
|
* Abort every in-flight stream (e.g. on WebSocket close).
|
|
1685
1744
|
*/
|
|
1686
1745
|
abortAll() {
|
|
1687
|
-
for (const stream of this.inflight.
|
|
1746
|
+
for (const [sid, stream] of this.inflight.entries()) {
|
|
1688
1747
|
try {
|
|
1689
1748
|
stream.abort();
|
|
1690
|
-
} catch {
|
|
1749
|
+
} catch (err) {
|
|
1750
|
+
log("error", "forwarder_abort_failed", { sid, ...errorFields(err) });
|
|
1691
1751
|
}
|
|
1692
1752
|
}
|
|
1693
1753
|
this.inflight.clear();
|
|
@@ -1721,12 +1781,12 @@ var StreamForwarder = class {
|
|
|
1721
1781
|
let endBody;
|
|
1722
1782
|
if (has_body) {
|
|
1723
1783
|
const chunks = [];
|
|
1724
|
-
bodyPromise = new Promise((
|
|
1784
|
+
bodyPromise = new Promise((resolve3) => {
|
|
1725
1785
|
pushBody = (buf) => {
|
|
1726
1786
|
chunks.push(buf);
|
|
1727
1787
|
};
|
|
1728
1788
|
endBody = () => {
|
|
1729
|
-
|
|
1789
|
+
resolve3(Buffer.concat(chunks));
|
|
1730
1790
|
};
|
|
1731
1791
|
});
|
|
1732
1792
|
}
|
|
@@ -1843,7 +1903,7 @@ function connectTunnel(options) {
|
|
|
1843
1903
|
} = options;
|
|
1844
1904
|
const tunnelUrl = getTunnelUrlConfig();
|
|
1845
1905
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
1846
|
-
return new Promise((
|
|
1906
|
+
return new Promise((resolve3, reject) => {
|
|
1847
1907
|
const ws = new WebSocket2(url, {
|
|
1848
1908
|
headers: {
|
|
1849
1909
|
Authorization: authHeader
|
|
@@ -1898,7 +1958,7 @@ function connectTunnel(options) {
|
|
|
1898
1958
|
clearTimeout(connectionTimeout);
|
|
1899
1959
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
1900
1960
|
onConnected?.(connectedAgentId);
|
|
1901
|
-
|
|
1961
|
+
resolve3({
|
|
1902
1962
|
ws,
|
|
1903
1963
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
1904
1964
|
});
|
|
@@ -1960,7 +2020,11 @@ var RunnerConnection = class {
|
|
|
1960
2020
|
if (this.connection) {
|
|
1961
2021
|
try {
|
|
1962
2022
|
this.connection.close();
|
|
1963
|
-
} catch {
|
|
2023
|
+
} catch (err) {
|
|
2024
|
+
log("error", "runner_connection_close_failed", {
|
|
2025
|
+
agent_id: this.resolvedAgentId,
|
|
2026
|
+
...errorFields(err)
|
|
2027
|
+
});
|
|
1964
2028
|
}
|
|
1965
2029
|
this.connection = null;
|
|
1966
2030
|
}
|
|
@@ -2012,6 +2076,404 @@ var RunnerConnection = class {
|
|
|
2012
2076
|
}
|
|
2013
2077
|
};
|
|
2014
2078
|
|
|
2079
|
+
// src/lib/channels/driver.ts
|
|
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
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
function expandAndValidate(requestedPath, homeDir) {
|
|
2151
|
+
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
2152
|
+
return null;
|
|
2153
|
+
}
|
|
2154
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
2155
|
+
if (expanded.split(/[/\\]/).includes("..")) {
|
|
2156
|
+
return null;
|
|
2157
|
+
}
|
|
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;
|
|
2164
|
+
}
|
|
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
|
+
}
|
|
2180
|
+
}
|
|
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
|
+
|
|
2015
2477
|
// src/lib/channels/driver.ts
|
|
2016
2478
|
function messageIdOf(m) {
|
|
2017
2479
|
if (!m || typeof m !== "object") return void 0;
|
|
@@ -2040,7 +2502,10 @@ var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
|
2040
2502
|
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
2041
2503
|
var HEARTBEAT_MS = 6e4;
|
|
2042
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;
|
|
2043
2507
|
var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
|
|
2508
|
+
var MAX_SUPERSEDED_CONVERSATIONS = 256;
|
|
2044
2509
|
var ChannelAuthError = class extends Error {
|
|
2045
2510
|
constructor(message) {
|
|
2046
2511
|
super(message);
|
|
@@ -2077,8 +2542,38 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2077
2542
|
pausedMaxWaitMs;
|
|
2078
2543
|
stuckQueuedMs;
|
|
2079
2544
|
now;
|
|
2545
|
+
fileSyncDirectories;
|
|
2546
|
+
homeDir;
|
|
2080
2547
|
/** Cache of conversationId → opencode sessionId. */
|
|
2081
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();
|
|
2082
2577
|
/**
|
|
2083
2578
|
* Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
|
|
2084
2579
|
* longer idempotent (no caller-supplied `messageID`), and its read-back picks
|
|
@@ -2201,6 +2696,24 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2201
2696
|
sessionTitles = /* @__PURE__ */ new Map();
|
|
2202
2697
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
2203
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;
|
|
2204
2717
|
/**
|
|
2205
2718
|
* The currently-executing `drainPending()` promise, or null when idle. Lets a
|
|
2206
2719
|
* graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
|
|
@@ -2231,6 +2744,8 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2231
2744
|
this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
|
|
2232
2745
|
this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
2233
2746
|
this.now = config2.now ?? (() => Date.now());
|
|
2747
|
+
this.fileSyncDirectories = config2.fileSyncDirectories ?? [];
|
|
2748
|
+
this.homeDir = config2.homeDir ?? homedir();
|
|
2234
2749
|
}
|
|
2235
2750
|
/** The IPv4-loopback base URL for the local `opencode serve`. */
|
|
2236
2751
|
get opencodeBase() {
|
|
@@ -2258,6 +2773,47 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2258
2773
|
);
|
|
2259
2774
|
return run2;
|
|
2260
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
|
+
}
|
|
2261
2817
|
async runDrain() {
|
|
2262
2818
|
let dispatched = 0;
|
|
2263
2819
|
try {
|
|
@@ -2291,6 +2847,28 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2291
2847
|
}
|
|
2292
2848
|
return false;
|
|
2293
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
|
+
}
|
|
2294
2872
|
/**
|
|
2295
2873
|
* OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
|
|
2296
2874
|
* exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
|
|
@@ -2352,7 +2930,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2352
2930
|
await this.sleep(step);
|
|
2353
2931
|
}
|
|
2354
2932
|
}
|
|
2355
|
-
while (this.hasInFlightWatchers()) {
|
|
2933
|
+
while (this.hasInFlightWatchers() || this.syncingFiles) {
|
|
2356
2934
|
if (this.now() >= deadline) return false;
|
|
2357
2935
|
await this.sleep(step);
|
|
2358
2936
|
}
|
|
@@ -2387,10 +2965,15 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2387
2965
|
* @returns the count of messages NEWLY dispatched (not already in-flight).
|
|
2388
2966
|
*/
|
|
2389
2967
|
async processConversation(conv) {
|
|
2390
|
-
const sessionId = await this.ensureSession(conv);
|
|
2968
|
+
const { sessionId, refusedSessionId } = await this.ensureSession(conv);
|
|
2391
2969
|
const messages = await this.getPendingMessages(conv.id);
|
|
2392
2970
|
let dispatched = 0;
|
|
2393
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
|
+
}
|
|
2394
2977
|
for (const message of messages) {
|
|
2395
2978
|
if (this.stopped) break;
|
|
2396
2979
|
if (this.dispatched.has(message.id)) {
|
|
@@ -2417,7 +3000,8 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2417
3000
|
} catch (err) {
|
|
2418
3001
|
if (err instanceof ChannelAuthError) throw err;
|
|
2419
3002
|
this.dispatched.delete(message.id);
|
|
2420
|
-
|
|
3003
|
+
const exists = await sessionExists(this.port, sessionId);
|
|
3004
|
+
if (exists === false) {
|
|
2421
3005
|
this.sessions.delete(conv.id);
|
|
2422
3006
|
this.log({
|
|
2423
3007
|
level: "warn",
|
|
@@ -2427,15 +3011,39 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2427
3011
|
});
|
|
2428
3012
|
break;
|
|
2429
3013
|
}
|
|
2430
|
-
|
|
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
|
+
});
|
|
2431
3039
|
});
|
|
2432
3040
|
this.log({
|
|
2433
3041
|
level: "error",
|
|
2434
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
3042
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
|
|
2435
3043
|
conversation_id: conv.id,
|
|
2436
3044
|
message_id: message.id
|
|
2437
3045
|
});
|
|
2438
|
-
|
|
3046
|
+
break;
|
|
2439
3047
|
}
|
|
2440
3048
|
if (opencodeMessageId === null) {
|
|
2441
3049
|
this.log({
|
|
@@ -2461,8 +3069,42 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2461
3069
|
this.ensureWatcherRunning(sessionId);
|
|
2462
3070
|
return dispatched;
|
|
2463
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
|
+
*/
|
|
2464
3097
|
async ensureSession(conv) {
|
|
2465
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
|
+
}
|
|
2466
3108
|
if (bound) {
|
|
2467
3109
|
const exists = await sessionExists(this.port, bound);
|
|
2468
3110
|
if (exists === false) {
|
|
@@ -2472,12 +3114,12 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2472
3114
|
conversation_id: conv.id
|
|
2473
3115
|
});
|
|
2474
3116
|
this.sessions.delete(conv.id);
|
|
2475
|
-
return this.createAndBindSession(conv.id);
|
|
3117
|
+
return { sessionId: await this.createAndBindSession(conv.id) };
|
|
2476
3118
|
}
|
|
2477
3119
|
this.sessions.set(conv.id, bound);
|
|
2478
|
-
return bound;
|
|
3120
|
+
return { sessionId: bound };
|
|
2479
3121
|
}
|
|
2480
|
-
return this.createAndBindSession(conv.id);
|
|
3122
|
+
return { sessionId: await this.createAndBindSession(conv.id) };
|
|
2481
3123
|
}
|
|
2482
3124
|
/**
|
|
2483
3125
|
* Create a fresh OpenCode session for a conversation, cache the binding, and
|
|
@@ -2565,7 +3207,11 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2565
3207
|
* (not-owned / out-of-range / deleted-at-source / workspace gone) / 413
|
|
2566
3208
|
* (over-cap). On ANY non-2xx or thrown failure we return `null` so the caller
|
|
2567
3209
|
* OMITS that one image and the text turn still sends — NEVER throws the turn.
|
|
2568
|
-
*
|
|
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).
|
|
2569
3215
|
*/
|
|
2570
3216
|
async fetchAttachmentDataUrl(messageId, index, mime) {
|
|
2571
3217
|
try {
|
|
@@ -2574,6 +3220,25 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2574
3220
|
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2575
3221
|
);
|
|
2576
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
|
+
}
|
|
2577
3242
|
this.log({
|
|
2578
3243
|
level: "error",
|
|
2579
3244
|
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} returned HTTP ${res.status} \u2014 omitting this image (text turn proceeds)`,
|
|
@@ -2613,6 +3278,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2613
3278
|
if (this.attachmentsSkippedSignalled.has(messageId)) return;
|
|
2614
3279
|
this.attachmentsSkippedSignalled.add(messageId);
|
|
2615
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;
|
|
2616
3284
|
this.log({
|
|
2617
3285
|
level: "info",
|
|
2618
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`,
|
|
@@ -2622,7 +3290,8 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2622
3290
|
void this.postSignal(conversationId, messageId, "attachments_skipped", {
|
|
2623
3291
|
skipped,
|
|
2624
3292
|
failed,
|
|
2625
|
-
...skipped > 0 ? { skipped_reason: skippedReason } : {}
|
|
3293
|
+
...skipped > 0 ? { skipped_reason: skippedReason } : {},
|
|
3294
|
+
...failedReason ? { failed_reason: failedReason } : {}
|
|
2626
3295
|
});
|
|
2627
3296
|
}
|
|
2628
3297
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
@@ -2653,12 +3322,17 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2653
3322
|
stuckReported: false,
|
|
2654
3323
|
lastAliveAt: 0,
|
|
2655
3324
|
aliveInFlight: false,
|
|
3325
|
+
titleSynced: false,
|
|
3326
|
+
titleSyncInFlight: false,
|
|
2656
3327
|
awaitingHumanLatched: false,
|
|
2657
3328
|
pausedOnQuestion: false,
|
|
2658
3329
|
pausedOnPermission: false,
|
|
2659
3330
|
pausedClearConfirmed: false,
|
|
2660
3331
|
pausedInFlight: false,
|
|
2661
|
-
deliveryDeadlineAnchored: false
|
|
3332
|
+
deliveryDeadlineAnchored: false,
|
|
3333
|
+
b2PinnedSinceMs: 0,
|
|
3334
|
+
b2LastDescendantCheckMs: 0,
|
|
3335
|
+
b2AbandonedSignalled: false
|
|
2662
3336
|
});
|
|
2663
3337
|
}
|
|
2664
3338
|
/**
|
|
@@ -2726,12 +3400,17 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2726
3400
|
// with no extra `re_adopted` signal needed (folds old WI-6).
|
|
2727
3401
|
lastAliveAt: 0,
|
|
2728
3402
|
aliveInFlight: false,
|
|
3403
|
+
titleSynced: false,
|
|
3404
|
+
titleSyncInFlight: false,
|
|
2729
3405
|
awaitingHumanLatched: false,
|
|
2730
3406
|
pausedOnQuestion: false,
|
|
2731
3407
|
pausedOnPermission: false,
|
|
2732
3408
|
pausedClearConfirmed: false,
|
|
2733
3409
|
pausedInFlight: false,
|
|
2734
|
-
deliveryDeadlineAnchored: false
|
|
3410
|
+
deliveryDeadlineAnchored: false,
|
|
3411
|
+
b2PinnedSinceMs: 0,
|
|
3412
|
+
b2LastDescendantCheckMs: 0,
|
|
3413
|
+
b2AbandonedSignalled: false
|
|
2735
3414
|
});
|
|
2736
3415
|
}
|
|
2737
3416
|
/**
|
|
@@ -2893,58 +3572,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
2893
3572
|
}
|
|
2894
3573
|
}
|
|
2895
3574
|
if (state === "done") {
|
|
2896
|
-
this.
|
|
2897
|
-
if (!inFlight.done) {
|
|
2898
|
-
this.log({
|
|
2899
|
-
level: "info",
|
|
2900
|
-
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
|
|
2901
|
-
conversation_id: conv.id,
|
|
2902
|
-
message_id: inFlight.evidentMessageId
|
|
2903
|
-
});
|
|
2904
|
-
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
2905
|
-
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
2906
|
-
try {
|
|
2907
|
-
await this.markDone(
|
|
2908
|
-
conv.id,
|
|
2909
|
-
inFlight.evidentMessageId,
|
|
2910
|
-
sessionId,
|
|
2911
|
-
inFlight.opencodeMessageId,
|
|
2912
|
-
title,
|
|
2913
|
-
usage
|
|
2914
|
-
);
|
|
2915
|
-
} catch (err) {
|
|
2916
|
-
if (err instanceof ChannelAuthError) throw err;
|
|
2917
|
-
if (err instanceof ChannelTerminalError) {
|
|
2918
|
-
this.log({
|
|
2919
|
-
level: "warn",
|
|
2920
|
-
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2921
|
-
conversation_id: conv.id,
|
|
2922
|
-
message_id: inFlight.evidentMessageId
|
|
2923
|
-
});
|
|
2924
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2925
|
-
return;
|
|
2926
|
-
}
|
|
2927
|
-
if (this.now() >= inFlight.deadline) {
|
|
2928
|
-
this.log({
|
|
2929
|
-
level: "warn",
|
|
2930
|
-
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)}`,
|
|
2931
|
-
conversation_id: conv.id,
|
|
2932
|
-
message_id: inFlight.evidentMessageId
|
|
2933
|
-
});
|
|
2934
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2935
|
-
return;
|
|
2936
|
-
}
|
|
2937
|
-
this.log({
|
|
2938
|
-
level: "warn",
|
|
2939
|
-
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2940
|
-
conversation_id: conv.id,
|
|
2941
|
-
message_id: inFlight.evidentMessageId
|
|
2942
|
-
});
|
|
2943
|
-
return;
|
|
2944
|
-
}
|
|
2945
|
-
inFlight.done = true;
|
|
2946
|
-
}
|
|
2947
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3575
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
2948
3576
|
return;
|
|
2949
3577
|
}
|
|
2950
3578
|
if (state === "failed") {
|
|
@@ -3004,6 +3632,44 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3004
3632
|
});
|
|
3005
3633
|
}
|
|
3006
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
|
+
}
|
|
3007
3673
|
if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
|
|
3008
3674
|
this.log({
|
|
3009
3675
|
level: "warn",
|
|
@@ -3023,6 +3689,18 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3023
3689
|
inFlight.aliveInFlight = false;
|
|
3024
3690
|
if (ok) inFlight.lastAliveAt = this.now();
|
|
3025
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
|
+
}
|
|
3026
3704
|
}
|
|
3027
3705
|
if (awaitingHuman) {
|
|
3028
3706
|
if (!inFlight.awaitingHumanLatched) {
|
|
@@ -3060,6 +3738,70 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3060
3738
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3061
3739
|
}
|
|
3062
3740
|
}
|
|
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
|
+
}
|
|
3063
3805
|
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
3064
3806
|
/**
|
|
3065
3807
|
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
@@ -3639,6 +4381,47 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3639
4381
|
}
|
|
3640
4382
|
return false;
|
|
3641
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
|
+
}
|
|
3642
4425
|
/**
|
|
3643
4426
|
* Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
|
|
3644
4427
|
* `null` for a root session (no parent) and `undefined` when opencode is
|
|
@@ -3723,6 +4506,54 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3723
4506
|
}
|
|
3724
4507
|
return null;
|
|
3725
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
|
+
}
|
|
3726
4557
|
/**
|
|
3727
4558
|
* DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
|
|
3728
4559
|
* (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
|
|
@@ -3781,6 +4612,84 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3781
4612
|
}
|
|
3782
4613
|
return false;
|
|
3783
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
|
+
}
|
|
3784
4693
|
/**
|
|
3785
4694
|
* Cheap decision-telemetry label for a running row's LAST correlated reply
|
|
3786
4695
|
* (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
|
|
@@ -3909,6 +4818,32 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3909
4818
|
}
|
|
3910
4819
|
return messages;
|
|
3911
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
|
+
}
|
|
3912
4847
|
/**
|
|
3913
4848
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
3914
4849
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
@@ -3938,7 +4873,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3938
4873
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3939
4874
|
body: JSON.stringify({
|
|
3940
4875
|
status: "processing",
|
|
3941
|
-
|
|
4876
|
+
...this.sessionIdBody(sessionId, conversationId, messageId, "processing"),
|
|
3942
4877
|
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
3943
4878
|
...title ? { title } : {}
|
|
3944
4879
|
})
|
|
@@ -3987,6 +4922,11 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
3987
4922
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3988
4923
|
body: JSON.stringify({
|
|
3989
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.
|
|
3990
4930
|
opencode_session_id: sessionId,
|
|
3991
4931
|
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
3992
4932
|
...title ? { title } : {},
|
|
@@ -4003,14 +4943,23 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4003
4943
|
}
|
|
4004
4944
|
/**
|
|
4005
4945
|
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
4006
|
-
* when provided (issue #182)
|
|
4007
|
-
* `
|
|
4008
|
-
*
|
|
4009
|
-
*
|
|
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).
|
|
4010
4955
|
*/
|
|
4011
4956
|
async markFailed(conversationId, messageId, sessionId, error2, usage) {
|
|
4012
4957
|
const body = { status: "failed" };
|
|
4013
|
-
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
|
+
}
|
|
4014
4963
|
if (error2 !== void 0) body.error = error2;
|
|
4015
4964
|
if (usage) Object.assign(body, usage);
|
|
4016
4965
|
await this.callWithRetry(
|
|
@@ -4338,12 +5287,14 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
4338
5287
|
return { error: `Failed to resolve runner from key: ${message}` };
|
|
4339
5288
|
}
|
|
4340
5289
|
}
|
|
5290
|
+
var BEST_EFFORT_NOTIFY_TIMEOUT_MS = 2e3;
|
|
4341
5291
|
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
4342
5292
|
const apiUrl = getApiUrlConfig();
|
|
4343
5293
|
try {
|
|
4344
5294
|
const response = await fetch(`${apiUrl}/runners/${agentId}/disconnect`, {
|
|
4345
5295
|
method: "POST",
|
|
4346
|
-
headers: { Authorization: authHeader }
|
|
5296
|
+
headers: { Authorization: authHeader },
|
|
5297
|
+
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
4347
5298
|
});
|
|
4348
5299
|
if (!response.ok) {
|
|
4349
5300
|
const serverMessage = await readErrorMessage(response);
|
|
@@ -4354,7 +5305,35 @@ async function notifyAgentDisconnected(agentId, authHeader) {
|
|
|
4354
5305
|
}
|
|
4355
5306
|
return { ok: true };
|
|
4356
5307
|
} catch (error2) {
|
|
4357
|
-
return { ok: false, error:
|
|
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)
|
|
5326
|
+
});
|
|
5327
|
+
if (!response.ok) {
|
|
5328
|
+
const serverMessage = await readErrorMessage(response);
|
|
5329
|
+
return {
|
|
5330
|
+
ok: false,
|
|
5331
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
5332
|
+
};
|
|
5333
|
+
}
|
|
5334
|
+
return { ok: true };
|
|
5335
|
+
} catch (error2) {
|
|
5336
|
+
return { ok: false, error: describeBestEffortError(error2) };
|
|
4358
5337
|
}
|
|
4359
5338
|
}
|
|
4360
5339
|
async function getAgentInfo(agentId, authHeader) {
|
|
@@ -4404,6 +5383,7 @@ var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
|
4404
5383
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
4405
5384
|
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
4406
5385
|
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
5386
|
+
var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
4407
5387
|
function resolveLogLevel(options) {
|
|
4408
5388
|
const accepted = Object.keys(LOG_LEVELS);
|
|
4409
5389
|
const validate = (value, source) => {
|
|
@@ -4427,6 +5407,34 @@ function resolveLogLevel(options) {
|
|
|
4427
5407
|
}
|
|
4428
5408
|
return "info";
|
|
4429
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
|
+
}
|
|
4430
5438
|
function meetsThreshold(state, level) {
|
|
4431
5439
|
return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
|
|
4432
5440
|
}
|
|
@@ -4548,18 +5556,29 @@ async function handleAuthError(state, error2) {
|
|
|
4548
5556
|
async function driveChannels(state, driver) {
|
|
4549
5557
|
let idlePolls = 0;
|
|
4550
5558
|
let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
5559
|
+
let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
4551
5560
|
while (state.running) {
|
|
4552
5561
|
if (state.connection?.reconnecting && state.connection.reconnectPromise) {
|
|
4553
5562
|
logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
|
|
4554
5563
|
if (state.interactive) displayStatus(state);
|
|
4555
5564
|
await state.connection.reconnectPromise;
|
|
4556
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
|
+
);
|
|
4557
5573
|
try {
|
|
4558
5574
|
const processed = await driver.drainPending();
|
|
4559
5575
|
state.messageCount += processed;
|
|
4560
5576
|
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
4561
5577
|
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
4562
|
-
|
|
5578
|
+
const appliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
5579
|
+
const fileActivity = carriedOverFileSync || appliedFiles !== lastSeenAppliedFiles;
|
|
5580
|
+
lastSeenAppliedFiles = appliedFiles;
|
|
5581
|
+
if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity || fileActivity) {
|
|
4563
5582
|
idlePolls = 0;
|
|
4564
5583
|
if (processed > 0 && state.interactive) displayStatus(state);
|
|
4565
5584
|
} else if (state.idleTimeout !== null) {
|
|
@@ -4588,7 +5607,7 @@ async function driveChannels(state, driver) {
|
|
|
4588
5607
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
|
|
4589
5608
|
if (state.interactive) displayStatus(state);
|
|
4590
5609
|
}
|
|
4591
|
-
await new Promise((
|
|
5610
|
+
await new Promise((resolve3) => setTimeout(resolve3, CHANNEL_POLL_INTERVAL_MS));
|
|
4592
5611
|
if (state.idleTimeout !== null && idlePolls >= 2) {
|
|
4593
5612
|
const idleMs = idlePolls * CHANNEL_POLL_INTERVAL_MS;
|
|
4594
5613
|
if (idleMs > state.idleTimeout * 1e3) {
|
|
@@ -4691,7 +5710,18 @@ async function notifyOffline(state) {
|
|
|
4691
5710
|
if (state.interactive) displayStatus(state);
|
|
4692
5711
|
}
|
|
4693
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
|
+
}
|
|
4694
5723
|
async function cleanup(state, opts = {}) {
|
|
5724
|
+
const durations = {};
|
|
4695
5725
|
state.running = false;
|
|
4696
5726
|
for (const timer of state.sessionCleanupTimers) {
|
|
4697
5727
|
clearInterval(timer);
|
|
@@ -4705,7 +5735,13 @@ async function cleanup(state, opts = {}) {
|
|
|
4705
5735
|
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
4706
5736
|
displayStatus(state);
|
|
4707
5737
|
}
|
|
4708
|
-
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
|
+
);
|
|
4709
5745
|
if (!settled) {
|
|
4710
5746
|
logActivity(state, {
|
|
4711
5747
|
type: "info",
|
|
@@ -4714,13 +5750,15 @@ async function cleanup(state, opts = {}) {
|
|
|
4714
5750
|
if (state.interactive) displayStatus(state);
|
|
4715
5751
|
}
|
|
4716
5752
|
}
|
|
4717
|
-
await notifyOffline(state);
|
|
5753
|
+
await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
|
|
4718
5754
|
if (state.connection) {
|
|
4719
|
-
state.connection
|
|
5755
|
+
const connection = state.connection;
|
|
5756
|
+
await timeShutdownPhase(state, durations, "tunnel_close", () => connection.close());
|
|
4720
5757
|
state.connection = null;
|
|
4721
5758
|
}
|
|
4722
5759
|
if (state.opencodeProcess) {
|
|
4723
|
-
|
|
5760
|
+
const opencodeProcess = state.opencodeProcess;
|
|
5761
|
+
await timeShutdownPhase(state, durations, "opencode_stop", () => stopOpenCode(opencodeProcess));
|
|
4724
5762
|
if (state.interactive) {
|
|
4725
5763
|
logActivity(state, { type: "info", message: "Stopped OpenCode process" });
|
|
4726
5764
|
displayStatus(state);
|
|
@@ -4729,12 +5767,15 @@ async function cleanup(state, opts = {}) {
|
|
|
4729
5767
|
}
|
|
4730
5768
|
state.opencodeProcess = null;
|
|
4731
5769
|
}
|
|
5770
|
+
return durations;
|
|
4732
5771
|
}
|
|
4733
5772
|
async function run(options) {
|
|
4734
5773
|
const interactive = isInteractive(options.json);
|
|
4735
5774
|
let logLevel;
|
|
5775
|
+
let fileSyncDirectories;
|
|
4736
5776
|
try {
|
|
4737
5777
|
logLevel = resolveLogLevel(options);
|
|
5778
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir2());
|
|
4738
5779
|
} catch (error2) {
|
|
4739
5780
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
4740
5781
|
if (options.json) {
|
|
@@ -4769,6 +5810,11 @@ async function run(options) {
|
|
|
4769
5810
|
sessionCleanupTimers: [],
|
|
4770
5811
|
authHeader: ""
|
|
4771
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
|
+
}
|
|
4772
5818
|
if (!options.runner && options.agent) {
|
|
4773
5819
|
telemetry.info(
|
|
4774
5820
|
EventTypes.DEPRECATED_AGENT_FLAG_USED,
|
|
@@ -4792,14 +5838,38 @@ async function run(options) {
|
|
|
4792
5838
|
const handleSignal = async () => {
|
|
4793
5839
|
if (state.shuttingDown) return;
|
|
4794
5840
|
state.shuttingDown = true;
|
|
5841
|
+
const shutdownStartedAt = Date.now();
|
|
4795
5842
|
if (state.interactive) {
|
|
4796
5843
|
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
4797
5844
|
displayStatus(state);
|
|
4798
5845
|
} else {
|
|
4799
5846
|
log2(state, "Shutting down...");
|
|
4800
5847
|
}
|
|
4801
|
-
await cleanup(state, { graceful: true });
|
|
4802
|
-
|
|
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})`);
|
|
4803
5873
|
process.exit(0);
|
|
4804
5874
|
};
|
|
4805
5875
|
process.on("SIGINT", handleSignal);
|
|
@@ -4913,6 +5983,21 @@ async function run(options) {
|
|
|
4913
5983
|
}
|
|
4914
5984
|
spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
|
|
4915
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
|
+
}
|
|
4916
6001
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
4917
6002
|
try {
|
|
4918
6003
|
const oc = await ensureOpenCodeRunning({
|
|
@@ -4961,6 +6046,10 @@ async function run(options) {
|
|
|
4961
6046
|
getAuthHeader: () => state.authHeader,
|
|
4962
6047
|
conversationFilter: state.conversationFilter,
|
|
4963
6048
|
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
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(),
|
|
4964
6053
|
log: (entry) => (
|
|
4965
6054
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
4966
6055
|
// survive the sink filter (they no longer collapse to info). `type`
|
|
@@ -5042,6 +6131,12 @@ async function run(options) {
|
|
|
5042
6131
|
onDrainPing: () => {
|
|
5043
6132
|
if (!state.running) return;
|
|
5044
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
|
+
);
|
|
5045
6140
|
channelDriver.drainPending().then((processed) => {
|
|
5046
6141
|
if (processed > 0) {
|
|
5047
6142
|
state.messageCount += processed;
|
|
@@ -5125,7 +6220,10 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
|
|
|
5125
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);
|
|
5126
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 }));
|
|
5127
6222
|
program.command("whoami").description("Show the currently logged in user").action(whoami);
|
|
5128
|
-
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(
|
|
5129
6227
|
"--log-level <level>",
|
|
5130
6228
|
"Log verbosity: debug | info | warn | error (default: info). Env: EVIDENT_LOG_LEVEL"
|
|
5131
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(
|
|
@@ -5137,6 +6235,11 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
5137
6235
|
).option(
|
|
5138
6236
|
"--session-cleanup-interval <duration>",
|
|
5139
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
|
+
[]
|
|
5140
6243
|
).action(
|
|
5141
6244
|
(options) => {
|
|
5142
6245
|
run({
|
|
@@ -5153,7 +6256,10 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
5153
6256
|
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
5154
6257
|
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
5155
6258
|
sessionCleanupMaxCount: options.sessionCleanupMaxCount,
|
|
5156
|
-
sessionCleanupInterval: options.sessionCleanupInterval
|
|
6259
|
+
sessionCleanupInterval: options.sessionCleanupInterval,
|
|
6260
|
+
// Raw values — expansion/validation is single-sourced in run.ts's
|
|
6261
|
+
// resolveFileSyncDirectories.
|
|
6262
|
+
enableFileSyncTo: options.enableFileSyncTo
|
|
5157
6263
|
});
|
|
5158
6264
|
}
|
|
5159
6265
|
);
|