@evident-ai/cli 3.1.1-dev.e69750c → 3.1.1-dev.eec1cca
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 +60 -11
- package/dist/index.js +3156 -540
- 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 = {
|
|
@@ -38,17 +38,39 @@ function getApiUrl() {
|
|
|
38
38
|
function getTunnelUrl() {
|
|
39
39
|
return tunnelOverride ?? process.env.EVIDENT_TUNNEL_URL ?? defaults.tunnelUrl;
|
|
40
40
|
}
|
|
41
|
-
var config = new Conf({
|
|
42
|
-
projectName: "evident",
|
|
43
|
-
projectSuffix: "",
|
|
44
|
-
defaults
|
|
45
|
-
});
|
|
46
41
|
var credentials = new Conf({
|
|
47
42
|
projectName: "evident",
|
|
48
43
|
projectSuffix: "",
|
|
49
44
|
configName: "credentials",
|
|
50
|
-
defaults: {}
|
|
45
|
+
defaults: {},
|
|
46
|
+
configFileMode: 384
|
|
51
47
|
});
|
|
48
|
+
var CREDENTIALS_FILE_MODE = 384;
|
|
49
|
+
var CREDENTIALS_DIR_MODE = 448;
|
|
50
|
+
var permissionWarningEmitted = false;
|
|
51
|
+
function hardenCredentialsPermissions() {
|
|
52
|
+
if (process.platform === "win32") {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const file = credentials.path;
|
|
56
|
+
for (const [path, mode] of [
|
|
57
|
+
[file, CREDENTIALS_FILE_MODE],
|
|
58
|
+
[dirname(file), CREDENTIALS_DIR_MODE]
|
|
59
|
+
]) {
|
|
60
|
+
try {
|
|
61
|
+
if (existsSync(path) && (statSync(path).mode & 511) !== mode) {
|
|
62
|
+
chmodSync(path, mode);
|
|
63
|
+
}
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if (!permissionWarningEmitted) {
|
|
66
|
+
permissionWarningEmitted = true;
|
|
67
|
+
console.error(
|
|
68
|
+
`[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)}`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
52
74
|
function getApiUrlConfig() {
|
|
53
75
|
return getApiUrl();
|
|
54
76
|
}
|
|
@@ -59,6 +81,7 @@ function credentialsKey() {
|
|
|
59
81
|
return getApiUrl();
|
|
60
82
|
}
|
|
61
83
|
function getCredentials() {
|
|
84
|
+
hardenCredentialsPermissions();
|
|
62
85
|
const byEndpoint = credentials.get("byEndpoint") ?? {};
|
|
63
86
|
return byEndpoint[credentialsKey()] ?? {};
|
|
64
87
|
}
|
|
@@ -70,14 +93,17 @@ function setCredentials(creds) {
|
|
|
70
93
|
expiresAt: creds.expiresAt
|
|
71
94
|
};
|
|
72
95
|
credentials.set("byEndpoint", byEndpoint);
|
|
96
|
+
hardenCredentialsPermissions();
|
|
73
97
|
}
|
|
74
98
|
function clearCredentials() {
|
|
75
99
|
const byEndpoint = credentials.get("byEndpoint") ?? {};
|
|
76
100
|
delete byEndpoint[credentialsKey()];
|
|
77
101
|
credentials.set("byEndpoint", byEndpoint);
|
|
102
|
+
hardenCredentialsPermissions();
|
|
78
103
|
}
|
|
79
104
|
function clearAllCredentials() {
|
|
80
105
|
credentials.clear();
|
|
106
|
+
hardenCredentialsPermissions();
|
|
81
107
|
}
|
|
82
108
|
function getCliName() {
|
|
83
109
|
const argv1 = process.argv[1] || "";
|
|
@@ -236,16 +262,28 @@ async function getToken() {
|
|
|
236
262
|
}
|
|
237
263
|
return null;
|
|
238
264
|
}
|
|
265
|
+
function toError(err) {
|
|
266
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
267
|
+
}
|
|
239
268
|
async function deleteToken(options = {}) {
|
|
240
269
|
const keytar = await getKeytar();
|
|
270
|
+
const failures = [];
|
|
241
271
|
if (keytar) {
|
|
242
272
|
if (options.all) {
|
|
243
|
-
|
|
273
|
+
let accounts = [];
|
|
274
|
+
try {
|
|
275
|
+
accounts = await keytar.findCredentials(SERVICE_NAME);
|
|
276
|
+
} catch (err) {
|
|
277
|
+
failures.push({ type: "enumerate", error: toError(err) });
|
|
278
|
+
}
|
|
244
279
|
await Promise.all(
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
280
|
+
accounts.map(async (entry) => {
|
|
281
|
+
try {
|
|
282
|
+
await keytar.deletePassword(SERVICE_NAME, entry.account);
|
|
283
|
+
} catch (err) {
|
|
284
|
+
failures.push({ type: "delete", account: entry.account, error: toError(err) });
|
|
285
|
+
}
|
|
286
|
+
})
|
|
249
287
|
);
|
|
250
288
|
} else {
|
|
251
289
|
await keytar.deletePassword(SERVICE_NAME, keychainAccount());
|
|
@@ -256,6 +294,7 @@ async function deleteToken(options = {}) {
|
|
|
256
294
|
} else {
|
|
257
295
|
clearCredentials();
|
|
258
296
|
}
|
|
297
|
+
return { failures };
|
|
259
298
|
}
|
|
260
299
|
|
|
261
300
|
// src/utils/ui.ts
|
|
@@ -285,14 +324,14 @@ function blank() {
|
|
|
285
324
|
console.log();
|
|
286
325
|
}
|
|
287
326
|
function waitForEnter(prompt = "Press Enter to continue...") {
|
|
288
|
-
return new Promise((
|
|
327
|
+
return new Promise((resolve3) => {
|
|
289
328
|
process.stdout.write(chalk.dim(prompt));
|
|
290
329
|
const handler = () => {
|
|
291
330
|
process.stdin.removeListener("data", handler);
|
|
292
331
|
process.stdin.setRawMode?.(false);
|
|
293
332
|
process.stdin.pause();
|
|
294
333
|
console.log();
|
|
295
|
-
|
|
334
|
+
resolve3();
|
|
296
335
|
};
|
|
297
336
|
if (process.stdin.isTTY) {
|
|
298
337
|
process.stdin.setRawMode?.(true);
|
|
@@ -302,7 +341,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
|
|
|
302
341
|
});
|
|
303
342
|
}
|
|
304
343
|
function sleep(ms) {
|
|
305
|
-
return new Promise((
|
|
344
|
+
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
306
345
|
}
|
|
307
346
|
|
|
308
347
|
// src/commands/login.ts
|
|
@@ -373,22 +412,25 @@ async function deviceFlowLogin(options) {
|
|
|
373
412
|
}
|
|
374
413
|
async function tokenLogin() {
|
|
375
414
|
console.log("Token login mode.");
|
|
376
|
-
console.log("
|
|
415
|
+
console.log("Create a token under Settings \u2192 CLI tokens in the dashboard, then paste it below.");
|
|
416
|
+
console.log(
|
|
417
|
+
"(Alternatively, run `evident login` on a machine with a browser, or set EVIDENT_TOKEN for CI.)"
|
|
418
|
+
);
|
|
377
419
|
blank();
|
|
378
420
|
process.stdout.write("Paste token: ");
|
|
379
|
-
const token = await new Promise((
|
|
421
|
+
const token = await new Promise((resolve3) => {
|
|
380
422
|
let data = "";
|
|
381
423
|
process.stdin.setEncoding("utf8");
|
|
382
424
|
process.stdin.on("data", (chunk) => {
|
|
383
425
|
data += chunk;
|
|
384
426
|
});
|
|
385
427
|
process.stdin.on("end", () => {
|
|
386
|
-
|
|
428
|
+
resolve3(data.trim());
|
|
387
429
|
});
|
|
388
430
|
if (process.stdin.isTTY) {
|
|
389
431
|
process.stdin.once("data", (chunk) => {
|
|
390
432
|
process.stdin.pause();
|
|
391
|
-
|
|
433
|
+
resolve3(chunk.toString().trim());
|
|
392
434
|
});
|
|
393
435
|
process.stdin.resume();
|
|
394
436
|
}
|
|
@@ -397,13 +439,22 @@ async function tokenLogin() {
|
|
|
397
439
|
printError("No token provided.");
|
|
398
440
|
process.exit(1);
|
|
399
441
|
}
|
|
442
|
+
await validateAndStoreToken(token);
|
|
443
|
+
}
|
|
444
|
+
async function validateAndStoreToken(token) {
|
|
400
445
|
const spinner = ora("Validating token...").start();
|
|
401
446
|
try {
|
|
402
|
-
const result = await api.
|
|
447
|
+
const result = await api.get("/me", {
|
|
448
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
449
|
+
});
|
|
450
|
+
if (!result.user) {
|
|
451
|
+
throw new Error(
|
|
452
|
+
"This token is not a user login (e.g. a runner key). Paste a CLI token instead."
|
|
453
|
+
);
|
|
454
|
+
}
|
|
403
455
|
await storeToken({
|
|
404
456
|
token,
|
|
405
|
-
user: result.user
|
|
406
|
-
expiresAt: result.expires_at
|
|
457
|
+
user: { email: result.user.email }
|
|
407
458
|
});
|
|
408
459
|
spinner.stop();
|
|
409
460
|
printSuccess(`Logged in as ${chalk2.bold(result.user.email)}`);
|
|
@@ -423,9 +474,22 @@ async function login(options) {
|
|
|
423
474
|
}
|
|
424
475
|
|
|
425
476
|
// src/commands/logout.ts
|
|
477
|
+
function describeFailure(failure) {
|
|
478
|
+
if (failure.type === "enumerate") {
|
|
479
|
+
return `could not list stored keychain entries (${failure.error.message})`;
|
|
480
|
+
}
|
|
481
|
+
return `${failure.account} (${failure.error.message})`;
|
|
482
|
+
}
|
|
426
483
|
async function logout(options = {}) {
|
|
427
484
|
if (options.all) {
|
|
428
|
-
await deleteToken({ all: true });
|
|
485
|
+
const result = await deleteToken({ all: true });
|
|
486
|
+
if (result.failures.length > 0) {
|
|
487
|
+
printError(
|
|
488
|
+
`Failed to fully clear your keychain: ${result.failures.map(describeFailure).join("; ")}. Your local credentials file was cleared, but stale keychain entries may remain \u2014 run \`evident logout --all\` again, or remove them manually from your OS keychain / credential manager.`
|
|
489
|
+
);
|
|
490
|
+
process.exitCode = 1;
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
429
493
|
printSuccess("Logged out of all endpoints.");
|
|
430
494
|
return;
|
|
431
495
|
}
|
|
@@ -450,7 +514,9 @@ async function whoami() {
|
|
|
450
514
|
blank();
|
|
451
515
|
console.log(keyValue("Endpoint", apiUrl));
|
|
452
516
|
console.log(keyValue("User", chalk3.bold(credentials2.user.email)));
|
|
453
|
-
|
|
517
|
+
if (credentials2.user.id) {
|
|
518
|
+
console.log(keyValue("User ID", credentials2.user.id));
|
|
519
|
+
}
|
|
454
520
|
if (credentials2.expiresAt) {
|
|
455
521
|
const expiresAt = new Date(credentials2.expiresAt);
|
|
456
522
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -466,188 +532,6 @@ async function whoami() {
|
|
|
466
532
|
blank();
|
|
467
533
|
}
|
|
468
534
|
|
|
469
|
-
// src/commands/run.ts
|
|
470
|
-
import chalk6 from "chalk";
|
|
471
|
-
import ora3 from "ora";
|
|
472
|
-
import { select as select3 } from "@inquirer/prompts";
|
|
473
|
-
|
|
474
|
-
// ../../packages/types/src/telemetry/index.ts
|
|
475
|
-
var TelemetryEventTypes = {
|
|
476
|
-
// Agent activity events (shown in web UI activity log)
|
|
477
|
-
AGENT_CONNECTED: "agent.connected",
|
|
478
|
-
AGENT_DISCONNECTED: "agent.disconnected",
|
|
479
|
-
AGENT_MESSAGE_PROCESSING: "agent.message_processing",
|
|
480
|
-
AGENT_MESSAGE_DONE: "agent.message_done",
|
|
481
|
-
AGENT_MESSAGE_FAILED: "agent.message_failed"
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
// ../../packages/types/src/tunnel/index.ts
|
|
485
|
-
var MAX_FRAME_BYTES = 256 * 1024;
|
|
486
|
-
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
487
|
-
|
|
488
|
-
// ../../packages/types/src/logging/index.ts
|
|
489
|
-
var CORRELATION_ID_HEADER = "x-evident-correlation-id";
|
|
490
|
-
function log(level, event, fields) {
|
|
491
|
-
const method = level === "debug" ? "log" : level;
|
|
492
|
-
try {
|
|
493
|
-
console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
|
|
494
|
-
} catch (err) {
|
|
495
|
-
console.error(
|
|
496
|
-
"[evident] log_serialize_failed",
|
|
497
|
-
event,
|
|
498
|
-
err instanceof Error ? err.message : String(err)
|
|
499
|
-
);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
function stripQuery(url) {
|
|
503
|
-
try {
|
|
504
|
-
return new URL(url).pathname;
|
|
505
|
-
} catch {
|
|
506
|
-
const q = url.indexOf("?");
|
|
507
|
-
return q === -1 ? url : url.slice(0, q);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
// src/lib/telemetry.ts
|
|
512
|
-
var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
513
|
-
function getCliVersion() {
|
|
514
|
-
return CLI_VERSION;
|
|
515
|
-
}
|
|
516
|
-
var eventBuffer = [];
|
|
517
|
-
var flushTimeout = null;
|
|
518
|
-
var isShuttingDown = false;
|
|
519
|
-
var FLUSH_INTERVAL_MS = 5e3;
|
|
520
|
-
var MAX_BUFFER_SIZE = 50;
|
|
521
|
-
var FLUSH_TIMEOUT_MS = 3e3;
|
|
522
|
-
function logEvent(eventType, options = {}) {
|
|
523
|
-
const event = {
|
|
524
|
-
event_type: eventType,
|
|
525
|
-
severity: options.severity || "info",
|
|
526
|
-
message: options.message,
|
|
527
|
-
metadata: options.metadata,
|
|
528
|
-
agent_id: options.agentId,
|
|
529
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
530
|
-
};
|
|
531
|
-
eventBuffer.push(event);
|
|
532
|
-
if (options.severity === "error" || eventBuffer.length >= MAX_BUFFER_SIZE) {
|
|
533
|
-
void flushEvents();
|
|
534
|
-
} else if (!flushTimeout && !isShuttingDown) {
|
|
535
|
-
flushTimeout = setTimeout(() => {
|
|
536
|
-
flushTimeout = null;
|
|
537
|
-
void flushEvents();
|
|
538
|
-
}, FLUSH_INTERVAL_MS);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
var telemetry = {
|
|
542
|
-
debug: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "debug", message, metadata, agentId }),
|
|
543
|
-
info: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "info", message, metadata, agentId }),
|
|
544
|
-
warn: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "warning", message, metadata, agentId }),
|
|
545
|
-
error: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "error", message, metadata, agentId })
|
|
546
|
-
};
|
|
547
|
-
async function flushEvents() {
|
|
548
|
-
if (eventBuffer.length === 0) return;
|
|
549
|
-
const events = eventBuffer;
|
|
550
|
-
eventBuffer = [];
|
|
551
|
-
if (flushTimeout) {
|
|
552
|
-
clearTimeout(flushTimeout);
|
|
553
|
-
flushTimeout = null;
|
|
554
|
-
}
|
|
555
|
-
try {
|
|
556
|
-
const credentials2 = await getToken();
|
|
557
|
-
if (!credentials2) {
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
const apiUrl = getApiUrlConfig();
|
|
561
|
-
const controller = new AbortController();
|
|
562
|
-
const timeout = setTimeout(() => controller.abort(), FLUSH_TIMEOUT_MS);
|
|
563
|
-
try {
|
|
564
|
-
const request = {
|
|
565
|
-
events,
|
|
566
|
-
client_type: "cli",
|
|
567
|
-
client_version: CLI_VERSION
|
|
568
|
-
};
|
|
569
|
-
const response = await fetch(`${apiUrl}/telemetry/events`, {
|
|
570
|
-
method: "POST",
|
|
571
|
-
headers: {
|
|
572
|
-
"Content-Type": "application/json",
|
|
573
|
-
Authorization: `Bearer ${credentials2.token}`
|
|
574
|
-
},
|
|
575
|
-
body: JSON.stringify(request),
|
|
576
|
-
signal: controller.signal
|
|
577
|
-
});
|
|
578
|
-
if (!response.ok) {
|
|
579
|
-
console.error(`Telemetry flush failed: ${response.status}`);
|
|
580
|
-
}
|
|
581
|
-
} finally {
|
|
582
|
-
clearTimeout(timeout);
|
|
583
|
-
}
|
|
584
|
-
} catch (error2) {
|
|
585
|
-
if (process.env.DEBUG) {
|
|
586
|
-
console.error("Telemetry error:", error2);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
async function shutdownTelemetry() {
|
|
591
|
-
isShuttingDown = true;
|
|
592
|
-
if (flushTimeout) {
|
|
593
|
-
clearTimeout(flushTimeout);
|
|
594
|
-
flushTimeout = null;
|
|
595
|
-
}
|
|
596
|
-
await flushEvents();
|
|
597
|
-
}
|
|
598
|
-
function emitEvent(event) {
|
|
599
|
-
logEvent(event.event_type, {
|
|
600
|
-
severity: event.severity,
|
|
601
|
-
message: event.message,
|
|
602
|
-
metadata: event.metadata,
|
|
603
|
-
agentId: event.agent_id
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
function emitAgentConnected(agentId, metadata) {
|
|
607
|
-
emitEvent({
|
|
608
|
-
event_type: TelemetryEventTypes.AGENT_CONNECTED,
|
|
609
|
-
severity: "info",
|
|
610
|
-
message: "Agent CLI connected",
|
|
611
|
-
metadata,
|
|
612
|
-
agent_id: agentId
|
|
613
|
-
});
|
|
614
|
-
}
|
|
615
|
-
function emitAgentDisconnected(agentId, metadata) {
|
|
616
|
-
emitEvent({
|
|
617
|
-
event_type: TelemetryEventTypes.AGENT_DISCONNECTED,
|
|
618
|
-
severity: "info",
|
|
619
|
-
message: `Agent CLI disconnected (code: ${metadata.code})`,
|
|
620
|
-
metadata,
|
|
621
|
-
agent_id: agentId
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
|
-
var EventTypes = {
|
|
625
|
-
// Tunnel lifecycle
|
|
626
|
-
TUNNEL_STARTING: "tunnel.starting",
|
|
627
|
-
TUNNEL_CONNECTED: "tunnel.connected",
|
|
628
|
-
TUNNEL_DISCONNECTED: "tunnel.disconnected",
|
|
629
|
-
TUNNEL_RECONNECTING: "tunnel.reconnecting",
|
|
630
|
-
TUNNEL_ERROR: "tunnel.error",
|
|
631
|
-
// OpenCode communication
|
|
632
|
-
OPENCODE_HEALTH_CHECK: "opencode.health_check",
|
|
633
|
-
OPENCODE_HEALTH_OK: "opencode.health_ok",
|
|
634
|
-
OPENCODE_HEALTH_FAILED: "opencode.health_failed",
|
|
635
|
-
OPENCODE_REQUEST_RECEIVED: "opencode.request_received",
|
|
636
|
-
OPENCODE_REQUEST_FORWARDED: "opencode.request_forwarded",
|
|
637
|
-
OPENCODE_RESPONSE_SENT: "opencode.response_sent",
|
|
638
|
-
OPENCODE_UNREACHABLE: "opencode.unreachable",
|
|
639
|
-
OPENCODE_ERROR: "opencode.error",
|
|
640
|
-
// Authentication
|
|
641
|
-
AUTH_LOGIN_STARTED: "auth.login_started",
|
|
642
|
-
AUTH_LOGIN_SUCCESS: "auth.login_success",
|
|
643
|
-
AUTH_LOGIN_FAILED: "auth.login_failed",
|
|
644
|
-
AUTH_LOGOUT: "auth.logout",
|
|
645
|
-
// CLI lifecycle
|
|
646
|
-
CLI_STARTED: "cli.started",
|
|
647
|
-
CLI_COMMAND: "cli.command",
|
|
648
|
-
CLI_ERROR: "cli.error"
|
|
649
|
-
};
|
|
650
|
-
|
|
651
535
|
// src/lib/auth.ts
|
|
652
536
|
async function getAuthCredentials() {
|
|
653
537
|
const runnerKey = process.env.EVIDENT_RUNNER_KEY;
|
|
@@ -656,11 +540,12 @@ async function getAuthCredentials() {
|
|
|
656
540
|
return {
|
|
657
541
|
token: runnerKey,
|
|
658
542
|
authType: "agent_key",
|
|
543
|
+
keySource: "runner_key",
|
|
659
544
|
notice: agentKey ? "Both EVIDENT_RUNNER_KEY and EVIDENT_AGENT_KEY are set; using EVIDENT_RUNNER_KEY." : void 0
|
|
660
545
|
};
|
|
661
546
|
}
|
|
662
547
|
if (agentKey) {
|
|
663
|
-
return { token: agentKey, authType: "agent_key" };
|
|
548
|
+
return { token: agentKey, authType: "agent_key", keySource: "agent_key" };
|
|
664
549
|
}
|
|
665
550
|
const userToken = process.env.EVIDENT_TOKEN;
|
|
666
551
|
if (userToken) {
|
|
@@ -690,18 +575,735 @@ function isInteractive(jsonOutput) {
|
|
|
690
575
|
return true;
|
|
691
576
|
}
|
|
692
577
|
|
|
693
|
-
// src/
|
|
694
|
-
async function
|
|
578
|
+
// src/commands/agent-lookup.ts
|
|
579
|
+
async function readErrorMessage(response) {
|
|
580
|
+
const text = await response.text().catch(() => "");
|
|
581
|
+
if (!text) return response.statusText || void 0;
|
|
695
582
|
try {
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
583
|
+
const data = JSON.parse(text);
|
|
584
|
+
const message = data.message ?? data.error;
|
|
585
|
+
if (typeof message === "string" && message.trim()) {
|
|
586
|
+
return message;
|
|
587
|
+
}
|
|
588
|
+
} catch {
|
|
589
|
+
}
|
|
590
|
+
return text.trim() || response.statusText || void 0;
|
|
591
|
+
}
|
|
592
|
+
function authFailureHint(apiUrl, serverMessage) {
|
|
593
|
+
const reason = serverMessage ? `: ${serverMessage}` : "";
|
|
594
|
+
return `Authentication failed${reason}. Your credentials were rejected by ${apiUrl}. This usually means you logged in against a different environment, or your session expired \u2014 log in again pointing at this endpoint and retry.`;
|
|
595
|
+
}
|
|
596
|
+
async function resolveAgentIdFromKey(authHeader) {
|
|
597
|
+
const apiUrl = getApiUrlConfig();
|
|
598
|
+
try {
|
|
599
|
+
const response = await fetch(`${apiUrl}/me`, {
|
|
600
|
+
headers: { Authorization: authHeader }
|
|
699
601
|
});
|
|
602
|
+
if (response.status === 401) {
|
|
603
|
+
const serverMessage = await readErrorMessage(response);
|
|
604
|
+
return { error: authFailureHint(apiUrl, serverMessage), authFailed: true };
|
|
605
|
+
}
|
|
700
606
|
if (!response.ok) {
|
|
701
|
-
|
|
607
|
+
const serverMessage = await readErrorMessage(response);
|
|
608
|
+
return {
|
|
609
|
+
error: `Failed to resolve runner from key (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
|
|
610
|
+
};
|
|
702
611
|
}
|
|
703
|
-
const data = await response.json()
|
|
704
|
-
|
|
612
|
+
const data = await response.json();
|
|
613
|
+
if (data.auth_type === "agent_key" && data.agent_id) {
|
|
614
|
+
return { agent_id: data.agent_id };
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
error: "Cannot resolve runner ID: auth type is not agent_key. Please provide --runner explicitly."
|
|
618
|
+
};
|
|
619
|
+
} catch (error2) {
|
|
620
|
+
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
621
|
+
return { error: `Failed to resolve runner from key: ${message}` };
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
var BEST_EFFORT_NOTIFY_TIMEOUT_MS = 2e3;
|
|
625
|
+
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
626
|
+
const apiUrl = getApiUrlConfig();
|
|
627
|
+
try {
|
|
628
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}/disconnect`, {
|
|
629
|
+
method: "POST",
|
|
630
|
+
headers: { Authorization: authHeader },
|
|
631
|
+
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
632
|
+
});
|
|
633
|
+
if (!response.ok) {
|
|
634
|
+
const serverMessage = await readErrorMessage(response);
|
|
635
|
+
return {
|
|
636
|
+
ok: false,
|
|
637
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
return { ok: true };
|
|
641
|
+
} catch (error2) {
|
|
642
|
+
return { ok: false, error: describeBestEffortError(error2) };
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
function describeBestEffortError(error2) {
|
|
646
|
+
const name = error2?.name;
|
|
647
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
648
|
+
return `timed out after ${BEST_EFFORT_NOTIFY_TIMEOUT_MS}ms`;
|
|
649
|
+
}
|
|
650
|
+
return error2 instanceof Error ? error2.message : String(error2);
|
|
651
|
+
}
|
|
652
|
+
async function reportMicrovmId(agentId, authHeader, microvmId) {
|
|
653
|
+
try {
|
|
654
|
+
const apiUrl = getApiUrlConfig();
|
|
655
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}/microvm`, {
|
|
656
|
+
method: "POST",
|
|
657
|
+
headers: { Authorization: authHeader, "Content-Type": "application/json" },
|
|
658
|
+
body: JSON.stringify({ microvm_id: microvmId }),
|
|
659
|
+
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
660
|
+
});
|
|
661
|
+
if (!response.ok) {
|
|
662
|
+
const serverMessage = await readErrorMessage(response);
|
|
663
|
+
return {
|
|
664
|
+
ok: false,
|
|
665
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
return { ok: true };
|
|
669
|
+
} catch (error2) {
|
|
670
|
+
return { ok: false, error: describeBestEffortError(error2) };
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
function toReportedWindow(window) {
|
|
674
|
+
if (!window) return null;
|
|
675
|
+
return { utilization: window.utilization, resets_at: window.resetsAt };
|
|
676
|
+
}
|
|
677
|
+
async function reportClaudeUsage(agentId, authHeader, snapshot) {
|
|
678
|
+
try {
|
|
679
|
+
const apiUrl = getApiUrlConfig();
|
|
680
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}/claude-usage`, {
|
|
681
|
+
method: "POST",
|
|
682
|
+
headers: { Authorization: authHeader, "Content-Type": "application/json" },
|
|
683
|
+
body: JSON.stringify({
|
|
684
|
+
five_hour: toReportedWindow(snapshot.fiveHour),
|
|
685
|
+
seven_day: toReportedWindow(snapshot.sevenDay)
|
|
686
|
+
}),
|
|
687
|
+
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
688
|
+
});
|
|
689
|
+
if (!response.ok) {
|
|
690
|
+
const serverMessage = await readErrorMessage(response);
|
|
691
|
+
return {
|
|
692
|
+
ok: false,
|
|
693
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
return { ok: true };
|
|
697
|
+
} catch (error2) {
|
|
698
|
+
return { ok: false, error: describeBestEffortError(error2) };
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
async function getAgentInfo(agentId, authHeader) {
|
|
702
|
+
const apiUrl = getApiUrlConfig();
|
|
703
|
+
try {
|
|
704
|
+
const response = await fetch(`${apiUrl}/runners/${agentId}`, {
|
|
705
|
+
headers: { Authorization: authHeader }
|
|
706
|
+
});
|
|
707
|
+
if (response.status === 401) {
|
|
708
|
+
const serverMessage = await readErrorMessage(response);
|
|
709
|
+
return { valid: false, error: authFailureHint(apiUrl, serverMessage), authFailed: true };
|
|
710
|
+
}
|
|
711
|
+
if (response.status === 403) {
|
|
712
|
+
const serverMessage = await readErrorMessage(response);
|
|
713
|
+
return {
|
|
714
|
+
valid: false,
|
|
715
|
+
error: serverMessage ?? "You do not have access to this runner (it may belong to a different team or organization)."
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
if (response.status === 404) {
|
|
719
|
+
const serverMessage = await readErrorMessage(response);
|
|
720
|
+
return { valid: false, error: serverMessage ?? `Runner ${agentId} not found` };
|
|
721
|
+
}
|
|
722
|
+
if (!response.ok) {
|
|
723
|
+
const serverMessage = await readErrorMessage(response);
|
|
724
|
+
return {
|
|
725
|
+
valid: false,
|
|
726
|
+
error: `API error (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
const agent = await response.json();
|
|
730
|
+
if (agent.agent_type !== "local") {
|
|
731
|
+
return {
|
|
732
|
+
valid: false,
|
|
733
|
+
error: `Runner is type '${agent.agent_type}', must be 'local' for CLI connection`
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
return { valid: true, agent };
|
|
737
|
+
} catch (error2) {
|
|
738
|
+
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
739
|
+
return { valid: false, error: `Failed to validate runner: ${message}` };
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// src/commands/status.ts
|
|
744
|
+
var STATUS_TIMEOUT_MS = 1e4;
|
|
745
|
+
function authLabelFor(credentials2) {
|
|
746
|
+
if (credentials2.authType === "agent_key") {
|
|
747
|
+
return credentials2.keySource === "agent_key" ? "runner key (EVIDENT_AGENT_KEY)" : "runner key (EVIDENT_RUNNER_KEY)";
|
|
748
|
+
}
|
|
749
|
+
return "user token";
|
|
750
|
+
}
|
|
751
|
+
function describeFetchError(error2) {
|
|
752
|
+
const name = error2?.name;
|
|
753
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
754
|
+
return `timed out after ${STATUS_TIMEOUT_MS}ms waiting for a response`;
|
|
755
|
+
}
|
|
756
|
+
return error2 instanceof Error ? error2.message : String(error2);
|
|
757
|
+
}
|
|
758
|
+
async function checkStatus(jsonMode) {
|
|
759
|
+
const apiUrl = getApiUrlConfig();
|
|
760
|
+
const credentials2 = await getAuthCredentials();
|
|
761
|
+
if (!credentials2) {
|
|
762
|
+
return {
|
|
763
|
+
ok: false,
|
|
764
|
+
endpoint: apiUrl,
|
|
765
|
+
reason: "no_credentials",
|
|
766
|
+
error: "No credentials configured. Set EVIDENT_RUNNER_KEY (or EVIDENT_AGENT_KEY), or EVIDENT_TOKEN, or run `evident login`.",
|
|
767
|
+
exitCode: 1
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
if (credentials2.notice && !jsonMode) {
|
|
771
|
+
printWarning(credentials2.notice);
|
|
772
|
+
}
|
|
773
|
+
let response;
|
|
774
|
+
try {
|
|
775
|
+
response = await fetch(`${apiUrl}/me`, {
|
|
776
|
+
headers: { Authorization: getAuthHeader(credentials2) },
|
|
777
|
+
signal: AbortSignal.timeout(STATUS_TIMEOUT_MS)
|
|
778
|
+
});
|
|
779
|
+
} catch (error2) {
|
|
780
|
+
return {
|
|
781
|
+
ok: false,
|
|
782
|
+
endpoint: apiUrl,
|
|
783
|
+
authLabel: authLabelFor(credentials2),
|
|
784
|
+
reason: "unreachable",
|
|
785
|
+
error: `Could not reach ${apiUrl}: ${describeFetchError(error2)}. The credentials were NOT validated.`,
|
|
786
|
+
exitCode: 75
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
if (response.status === 401) {
|
|
790
|
+
const serverMessage = await readErrorMessage(response);
|
|
791
|
+
return {
|
|
792
|
+
ok: false,
|
|
793
|
+
endpoint: apiUrl,
|
|
794
|
+
authLabel: authLabelFor(credentials2),
|
|
795
|
+
reason: "unauthorized",
|
|
796
|
+
error: authFailureHint(apiUrl, serverMessage),
|
|
797
|
+
exitCode: 1
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
if (response.status === 404) {
|
|
801
|
+
return {
|
|
802
|
+
ok: false,
|
|
803
|
+
endpoint: apiUrl,
|
|
804
|
+
authLabel: authLabelFor(credentials2),
|
|
805
|
+
reason: "endpoint_not_found",
|
|
806
|
+
error: `${apiUrl}/me returned HTTP 404 \u2014 that endpoint has no /me route, so it is probably missing the /v1 prefix. The credentials were NOT validated.`,
|
|
807
|
+
exitCode: 75
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
if (response.status >= 500) {
|
|
811
|
+
const serverMessage = await readErrorMessage(response);
|
|
812
|
+
return {
|
|
813
|
+
ok: false,
|
|
814
|
+
endpoint: apiUrl,
|
|
815
|
+
authLabel: authLabelFor(credentials2),
|
|
816
|
+
reason: "unreachable",
|
|
817
|
+
error: `${apiUrl} returned HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}. The credentials were NOT validated.`,
|
|
818
|
+
exitCode: 75
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
if (!response.ok) {
|
|
822
|
+
const serverMessage = await readErrorMessage(response);
|
|
823
|
+
return {
|
|
824
|
+
ok: false,
|
|
825
|
+
endpoint: apiUrl,
|
|
826
|
+
authLabel: authLabelFor(credentials2),
|
|
827
|
+
reason: "http_error",
|
|
828
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`,
|
|
829
|
+
exitCode: 1
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
const data = await response.json();
|
|
833
|
+
return {
|
|
834
|
+
ok: true,
|
|
835
|
+
endpoint: apiUrl,
|
|
836
|
+
authType: data.auth_type,
|
|
837
|
+
authLabel: authLabelFor(credentials2),
|
|
838
|
+
runnerId: data.auth_type === "agent_key" ? data.agent_id : void 0,
|
|
839
|
+
reason: "ok",
|
|
840
|
+
exitCode: 0
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
function printJson(result) {
|
|
844
|
+
const payload = {
|
|
845
|
+
ok: result.ok,
|
|
846
|
+
endpoint: result.endpoint
|
|
847
|
+
};
|
|
848
|
+
if (result.authType) payload.auth_type = result.authType;
|
|
849
|
+
if (result.runnerId) payload.runner_id = result.runnerId;
|
|
850
|
+
if (result.reason) payload.reason = result.reason;
|
|
851
|
+
if (result.error) payload.error = result.error;
|
|
852
|
+
console.log(JSON.stringify(payload));
|
|
853
|
+
}
|
|
854
|
+
function printHuman(result) {
|
|
855
|
+
blank();
|
|
856
|
+
console.log(keyValue("Endpoint", result.endpoint));
|
|
857
|
+
if (result.ok) {
|
|
858
|
+
console.log(keyValue("Auth", result.authLabel ?? "\u2014"));
|
|
859
|
+
if (result.runnerId) {
|
|
860
|
+
console.log(keyValue("Runner", result.runnerId));
|
|
861
|
+
}
|
|
862
|
+
console.log(keyValue("Status", "OK \u2014 credentials accepted"));
|
|
863
|
+
blank();
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (result.authLabel) {
|
|
867
|
+
console.log(keyValue("Auth", result.authLabel));
|
|
868
|
+
}
|
|
869
|
+
blank();
|
|
870
|
+
printError(result.error ?? "Unknown error");
|
|
871
|
+
}
|
|
872
|
+
async function status(options = {}) {
|
|
873
|
+
const result = await checkStatus(Boolean(options.json));
|
|
874
|
+
if (options.json) {
|
|
875
|
+
printJson(result);
|
|
876
|
+
} else {
|
|
877
|
+
printHuman(result);
|
|
878
|
+
}
|
|
879
|
+
process.exit(result.exitCode);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// src/lib/claude-usage.ts
|
|
883
|
+
import { execFileSync } from "child_process";
|
|
884
|
+
import { readFileSync } from "fs";
|
|
885
|
+
import { homedir } from "os";
|
|
886
|
+
import { join } from "path";
|
|
887
|
+
var CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
|
|
888
|
+
var KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
889
|
+
function parseClaudeCliCredentials(raw) {
|
|
890
|
+
let parsed;
|
|
891
|
+
try {
|
|
892
|
+
parsed = JSON.parse(raw);
|
|
893
|
+
} catch {
|
|
894
|
+
return null;
|
|
895
|
+
}
|
|
896
|
+
const data = parsed.claudeAiOauth ?? parsed;
|
|
897
|
+
const creds = data;
|
|
898
|
+
if (typeof creds.accessToken !== "string" || typeof creds.expiresAt !== "number") {
|
|
899
|
+
return null;
|
|
900
|
+
}
|
|
901
|
+
return { accessToken: creds.accessToken, expiresAt: creds.expiresAt };
|
|
902
|
+
}
|
|
903
|
+
function readClaudeCliCredentials() {
|
|
904
|
+
if (process.platform === "darwin") {
|
|
905
|
+
try {
|
|
906
|
+
const raw = execFileSync(
|
|
907
|
+
"/usr/bin/security",
|
|
908
|
+
["find-generic-password", "-s", KEYCHAIN_SERVICE, "-w"],
|
|
909
|
+
{ encoding: "utf-8", timeout: 2e3, stdio: ["pipe", "pipe", "ignore"] }
|
|
910
|
+
);
|
|
911
|
+
return parseClaudeCliCredentials(raw);
|
|
912
|
+
} catch {
|
|
913
|
+
return null;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
try {
|
|
917
|
+
const raw = readFileSync(join(homedir(), ".claude", ".credentials.json"), "utf-8");
|
|
918
|
+
return parseClaudeCliCredentials(raw);
|
|
919
|
+
} catch {
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
var ClaudeUsageError = class extends Error {
|
|
924
|
+
constructor(message, reason) {
|
|
925
|
+
super(message);
|
|
926
|
+
this.reason = reason;
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
function isLocalCredentialProblem(err) {
|
|
930
|
+
return err instanceof ClaudeUsageError && (err.reason === "no_credentials" || err.reason === "credentials_expired");
|
|
931
|
+
}
|
|
932
|
+
function normalizeResetsAt(value) {
|
|
933
|
+
const ms = Date.parse(value);
|
|
934
|
+
return Number.isNaN(ms) ? null : new Date(ms).toISOString();
|
|
935
|
+
}
|
|
936
|
+
function toWindow(value) {
|
|
937
|
+
if (!value || typeof value !== "object") {
|
|
938
|
+
return null;
|
|
939
|
+
}
|
|
940
|
+
const window = value;
|
|
941
|
+
if (typeof window.utilization !== "number" || typeof window.resets_at !== "string") {
|
|
942
|
+
return null;
|
|
943
|
+
}
|
|
944
|
+
const resetsAt = normalizeResetsAt(window.resets_at);
|
|
945
|
+
if (resetsAt === null) {
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
return { utilization: window.utilization, resetsAt };
|
|
949
|
+
}
|
|
950
|
+
async function getClaudeUsage() {
|
|
951
|
+
const credentials2 = readClaudeCliCredentials();
|
|
952
|
+
if (!credentials2) {
|
|
953
|
+
throw new ClaudeUsageError(
|
|
954
|
+
"No local Claude Code login found. Run `claude` once to sign in with your Claude subscription.",
|
|
955
|
+
"no_credentials"
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
if (credentials2.expiresAt < Date.now()) {
|
|
959
|
+
throw new ClaudeUsageError(
|
|
960
|
+
"Claude Code credentials have expired. Run `claude` to refresh them.",
|
|
961
|
+
"credentials_expired"
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
const res = await fetch(CLAUDE_USAGE_URL, {
|
|
965
|
+
headers: {
|
|
966
|
+
Authorization: `Bearer ${credentials2.accessToken}`,
|
|
967
|
+
"Content-Type": "application/json",
|
|
968
|
+
"anthropic-version": "2023-06-01"
|
|
969
|
+
}
|
|
970
|
+
});
|
|
971
|
+
if (!res.ok) {
|
|
972
|
+
throw new ClaudeUsageError(`Claude usage request failed: HTTP ${res.status}`, "request_failed");
|
|
973
|
+
}
|
|
974
|
+
const body = await res.json();
|
|
975
|
+
return {
|
|
976
|
+
fiveHour: toWindow(body.five_hour),
|
|
977
|
+
sevenDay: toWindow(body.seven_day)
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// src/commands/claude-usage.ts
|
|
982
|
+
function formatWindow(label, window) {
|
|
983
|
+
if (!window) {
|
|
984
|
+
return keyValue(label, "not available for this plan");
|
|
985
|
+
}
|
|
986
|
+
const resetsAt = new Date(window.resetsAt);
|
|
987
|
+
return keyValue(label, `${window.utilization}% used, resets ${resetsAt.toLocaleString()}`);
|
|
988
|
+
}
|
|
989
|
+
async function claudeUsage() {
|
|
990
|
+
try {
|
|
991
|
+
const usage = await getClaudeUsage();
|
|
992
|
+
blank();
|
|
993
|
+
console.log(formatWindow("5-hour session", usage.fiveHour));
|
|
994
|
+
console.log(formatWindow("7-day", usage.sevenDay));
|
|
995
|
+
blank();
|
|
996
|
+
} catch (err) {
|
|
997
|
+
if (err instanceof ClaudeUsageError) {
|
|
998
|
+
printError(err.message);
|
|
999
|
+
process.exit(1);
|
|
1000
|
+
}
|
|
1001
|
+
throw err;
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
// src/commands/run.ts
|
|
1006
|
+
import { homedir as homedir3 } from "os";
|
|
1007
|
+
import { isAbsolute as isAbsolute2, join as join3, parse, resolve as resolvePath } from "path";
|
|
1008
|
+
import chalk6 from "chalk";
|
|
1009
|
+
|
|
1010
|
+
// ../../packages/types/src/agents/index.ts
|
|
1011
|
+
var MICROVM_MAX_LIFETIME_MS = 8 * 60 * 6e4;
|
|
1012
|
+
|
|
1013
|
+
// ../../packages/types/src/telemetry/index.ts
|
|
1014
|
+
var TelemetryEventTypes = {
|
|
1015
|
+
// Agent activity events (shown in web UI activity log)
|
|
1016
|
+
AGENT_CONNECTED: "agent.connected",
|
|
1017
|
+
AGENT_DISCONNECTED: "agent.disconnected",
|
|
1018
|
+
AGENT_MESSAGE_PROCESSING: "agent.message_processing",
|
|
1019
|
+
AGENT_MESSAGE_DONE: "agent.message_done",
|
|
1020
|
+
AGENT_MESSAGE_FAILED: "agent.message_failed",
|
|
1021
|
+
// A `warn`/`error` runner-side log line forwarded server-side for
|
|
1022
|
+
// observability (issue #916) — see `apps/cli/src/lib/runner-activity-telemetry.ts`.
|
|
1023
|
+
RUNNER_ACTIVITY: "runner.activity"
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
// ../../packages/types/src/tunnel/index.ts
|
|
1027
|
+
var MAX_FRAME_BYTES = 256 * 1024;
|
|
1028
|
+
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
1029
|
+
|
|
1030
|
+
// ../../packages/types/src/runner-files.ts
|
|
1031
|
+
var MAX_FILE_PUSH_BYTES = 64 * 1024;
|
|
1032
|
+
var MAX_FILE_SYNC_DIRECTORIES = 16;
|
|
1033
|
+
|
|
1034
|
+
// ../../packages/types/src/logging/index.ts
|
|
1035
|
+
var CORRELATION_ID_HEADER = "x-evident-correlation-id";
|
|
1036
|
+
function log(level, event, fields) {
|
|
1037
|
+
const method = level === "debug" ? "log" : level;
|
|
1038
|
+
try {
|
|
1039
|
+
console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
|
|
1040
|
+
} catch (err) {
|
|
1041
|
+
console.error(
|
|
1042
|
+
"[evident] log_serialize_failed",
|
|
1043
|
+
event,
|
|
1044
|
+
err instanceof Error ? err.message : String(err)
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
function errorFields(err) {
|
|
1049
|
+
if (err instanceof Error) {
|
|
1050
|
+
return { error: err.message, error_name: err.name };
|
|
1051
|
+
}
|
|
1052
|
+
return { error: String(err) };
|
|
1053
|
+
}
|
|
1054
|
+
function stripQuery(url) {
|
|
1055
|
+
try {
|
|
1056
|
+
return new URL(url).pathname;
|
|
1057
|
+
} catch {
|
|
1058
|
+
const q = url.indexOf("?");
|
|
1059
|
+
return q === -1 ? url : url.slice(0, q);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// src/commands/run.ts
|
|
1064
|
+
import ora3 from "ora";
|
|
1065
|
+
import { select as select3 } from "@inquirer/prompts";
|
|
1066
|
+
|
|
1067
|
+
// src/lib/telemetry.ts
|
|
1068
|
+
var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
1069
|
+
function getCliVersion() {
|
|
1070
|
+
return CLI_VERSION;
|
|
1071
|
+
}
|
|
1072
|
+
var eventBuffer = [];
|
|
1073
|
+
var flushTimeout = null;
|
|
1074
|
+
var isShuttingDown = false;
|
|
1075
|
+
var FLUSH_INTERVAL_MS = 5e3;
|
|
1076
|
+
var MAX_BUFFER_SIZE = 50;
|
|
1077
|
+
var FLUSH_TIMEOUT_MS = 3e3;
|
|
1078
|
+
var authProvider = null;
|
|
1079
|
+
function setTelemetryAuthProvider(provider) {
|
|
1080
|
+
authProvider = provider;
|
|
1081
|
+
}
|
|
1082
|
+
var FLUSH_FAILURE_LOG_INTERVAL_MS = 6e4;
|
|
1083
|
+
var lastFlushFailureLoggedAt = 0;
|
|
1084
|
+
var suppressedFlushFailureCount = 0;
|
|
1085
|
+
function logEvent(eventType, options = {}) {
|
|
1086
|
+
const event = {
|
|
1087
|
+
event_type: eventType,
|
|
1088
|
+
severity: options.severity || "info",
|
|
1089
|
+
message: options.message,
|
|
1090
|
+
metadata: options.metadata,
|
|
1091
|
+
agent_id: options.agentId,
|
|
1092
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1093
|
+
};
|
|
1094
|
+
eventBuffer.push(event);
|
|
1095
|
+
if (options.severity === "error" || eventBuffer.length >= MAX_BUFFER_SIZE) {
|
|
1096
|
+
void flushEvents();
|
|
1097
|
+
} else if (!flushTimeout && !isShuttingDown) {
|
|
1098
|
+
flushTimeout = setTimeout(() => {
|
|
1099
|
+
flushTimeout = null;
|
|
1100
|
+
void flushEvents();
|
|
1101
|
+
}, FLUSH_INTERVAL_MS);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
var telemetry = {
|
|
1105
|
+
debug: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "debug", message, metadata, agentId }),
|
|
1106
|
+
info: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "info", message, metadata, agentId }),
|
|
1107
|
+
warn: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "warning", message, metadata, agentId }),
|
|
1108
|
+
error: (eventType, message, metadata, agentId) => logEvent(eventType, { severity: "error", message, metadata, agentId })
|
|
1109
|
+
};
|
|
1110
|
+
async function flushEvents() {
|
|
1111
|
+
if (eventBuffer.length === 0) return;
|
|
1112
|
+
const events = eventBuffer;
|
|
1113
|
+
eventBuffer = [];
|
|
1114
|
+
if (flushTimeout) {
|
|
1115
|
+
clearTimeout(flushTimeout);
|
|
1116
|
+
flushTimeout = null;
|
|
1117
|
+
}
|
|
1118
|
+
try {
|
|
1119
|
+
const providerContext = authProvider?.();
|
|
1120
|
+
let authHeader;
|
|
1121
|
+
if (providerContext?.authHeader) {
|
|
1122
|
+
authHeader = providerContext.authHeader;
|
|
1123
|
+
} else {
|
|
1124
|
+
const credentials2 = await getToken();
|
|
1125
|
+
if (!credentials2) {
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
authHeader = `Bearer ${credentials2.token}`;
|
|
1129
|
+
}
|
|
1130
|
+
const apiUrl = getApiUrlConfig();
|
|
1131
|
+
const controller = new AbortController();
|
|
1132
|
+
const timeout = setTimeout(() => controller.abort(), FLUSH_TIMEOUT_MS);
|
|
1133
|
+
try {
|
|
1134
|
+
const request = {
|
|
1135
|
+
events,
|
|
1136
|
+
client_type: "cli",
|
|
1137
|
+
client_version: CLI_VERSION
|
|
1138
|
+
};
|
|
1139
|
+
const response = await fetch(`${apiUrl}/telemetry/events`, {
|
|
1140
|
+
method: "POST",
|
|
1141
|
+
headers: {
|
|
1142
|
+
"Content-Type": "application/json",
|
|
1143
|
+
Authorization: authHeader
|
|
1144
|
+
},
|
|
1145
|
+
body: JSON.stringify(request),
|
|
1146
|
+
signal: controller.signal
|
|
1147
|
+
});
|
|
1148
|
+
if (!response.ok) {
|
|
1149
|
+
console.error(`Telemetry flush failed: ${response.status}`);
|
|
1150
|
+
}
|
|
1151
|
+
} finally {
|
|
1152
|
+
clearTimeout(timeout);
|
|
1153
|
+
}
|
|
1154
|
+
} catch (error2) {
|
|
1155
|
+
const now = Date.now();
|
|
1156
|
+
if (now - lastFlushFailureLoggedAt >= FLUSH_FAILURE_LOG_INTERVAL_MS) {
|
|
1157
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1158
|
+
const suffix = suppressedFlushFailureCount > 0 ? ` (${suppressedFlushFailureCount} more suppressed in the last ${FLUSH_FAILURE_LOG_INTERVAL_MS / 1e3}s)` : "";
|
|
1159
|
+
console.error(`Telemetry flush error: ${message}${suffix}`);
|
|
1160
|
+
lastFlushFailureLoggedAt = now;
|
|
1161
|
+
suppressedFlushFailureCount = 0;
|
|
1162
|
+
} else {
|
|
1163
|
+
suppressedFlushFailureCount++;
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
async function shutdownTelemetry() {
|
|
1168
|
+
isShuttingDown = true;
|
|
1169
|
+
if (flushTimeout) {
|
|
1170
|
+
clearTimeout(flushTimeout);
|
|
1171
|
+
flushTimeout = null;
|
|
1172
|
+
}
|
|
1173
|
+
await flushEvents();
|
|
1174
|
+
}
|
|
1175
|
+
function emitEvent(event) {
|
|
1176
|
+
logEvent(event.event_type, {
|
|
1177
|
+
severity: event.severity,
|
|
1178
|
+
message: event.message,
|
|
1179
|
+
metadata: event.metadata,
|
|
1180
|
+
agentId: event.agent_id
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
function emitAgentConnected(agentId, metadata) {
|
|
1184
|
+
emitEvent({
|
|
1185
|
+
event_type: TelemetryEventTypes.AGENT_CONNECTED,
|
|
1186
|
+
severity: "info",
|
|
1187
|
+
message: "Agent CLI connected",
|
|
1188
|
+
metadata,
|
|
1189
|
+
agent_id: agentId
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
function emitAgentDisconnected(agentId, metadata) {
|
|
1193
|
+
emitEvent({
|
|
1194
|
+
event_type: TelemetryEventTypes.AGENT_DISCONNECTED,
|
|
1195
|
+
severity: "info",
|
|
1196
|
+
message: `Agent CLI disconnected (code: ${metadata.code})`,
|
|
1197
|
+
metadata,
|
|
1198
|
+
agent_id: agentId
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
var EventTypes = {
|
|
1202
|
+
// Tunnel lifecycle
|
|
1203
|
+
TUNNEL_STARTING: "tunnel.starting",
|
|
1204
|
+
TUNNEL_CONNECTED: "tunnel.connected",
|
|
1205
|
+
TUNNEL_DISCONNECTED: "tunnel.disconnected",
|
|
1206
|
+
TUNNEL_RECONNECTING: "tunnel.reconnecting",
|
|
1207
|
+
TUNNEL_ERROR: "tunnel.error",
|
|
1208
|
+
// OpenCode communication
|
|
1209
|
+
OPENCODE_HEALTH_CHECK: "opencode.health_check",
|
|
1210
|
+
OPENCODE_HEALTH_OK: "opencode.health_ok",
|
|
1211
|
+
OPENCODE_HEALTH_FAILED: "opencode.health_failed",
|
|
1212
|
+
OPENCODE_REQUEST_RECEIVED: "opencode.request_received",
|
|
1213
|
+
OPENCODE_REQUEST_FORWARDED: "opencode.request_forwarded",
|
|
1214
|
+
OPENCODE_RESPONSE_SENT: "opencode.response_sent",
|
|
1215
|
+
OPENCODE_UNREACHABLE: "opencode.unreachable",
|
|
1216
|
+
OPENCODE_ERROR: "opencode.error",
|
|
1217
|
+
// Authentication
|
|
1218
|
+
AUTH_LOGIN_STARTED: "auth.login_started",
|
|
1219
|
+
AUTH_LOGIN_SUCCESS: "auth.login_success",
|
|
1220
|
+
AUTH_LOGIN_FAILED: "auth.login_failed",
|
|
1221
|
+
AUTH_LOGOUT: "auth.logout",
|
|
1222
|
+
// CLI lifecycle
|
|
1223
|
+
CLI_STARTED: "cli.started",
|
|
1224
|
+
CLI_COMMAND: "cli.command",
|
|
1225
|
+
CLI_ERROR: "cli.error",
|
|
1226
|
+
// Deprecation telemetry (#412) — usage of the old `--agent`/`EVIDENT_AGENT_KEY`
|
|
1227
|
+
// names instead of the preferred `--runner`/`EVIDENT_RUNNER_KEY` (#409).
|
|
1228
|
+
DEPRECATED_AGENT_FLAG_USED: "cli.deprecated_agent_flag_used",
|
|
1229
|
+
DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
|
|
1230
|
+
};
|
|
1231
|
+
|
|
1232
|
+
// src/lib/runner-activity-telemetry.ts
|
|
1233
|
+
var FORWARDED_LEVELS = /* @__PURE__ */ new Set(["warn", "error"]);
|
|
1234
|
+
var SEVERITY_BY_LEVEL = {
|
|
1235
|
+
warn: "warning",
|
|
1236
|
+
error: "error"
|
|
1237
|
+
};
|
|
1238
|
+
var MAX_MESSAGE_LENGTH = 500;
|
|
1239
|
+
var TRUNCATION_MARKER = "\u2026";
|
|
1240
|
+
function redact(message) {
|
|
1241
|
+
return message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/https?:\/\/\S+/g, "<url>");
|
|
1242
|
+
}
|
|
1243
|
+
function truncate(message) {
|
|
1244
|
+
if (message.length <= MAX_MESSAGE_LENGTH) return message;
|
|
1245
|
+
return message.slice(0, MAX_MESSAGE_LENGTH - TRUNCATION_MARKER.length) + TRUNCATION_MARKER;
|
|
1246
|
+
}
|
|
1247
|
+
var RATE_LIMIT_WINDOW_MS = 6e4;
|
|
1248
|
+
var RATE_LIMIT_MAX_EVENTS = 30;
|
|
1249
|
+
var windowStartedAt = 0;
|
|
1250
|
+
var windowCount = 0;
|
|
1251
|
+
var windowDroppedCount = 0;
|
|
1252
|
+
function admitUnderRateLimit(now) {
|
|
1253
|
+
if (now - windowStartedAt >= RATE_LIMIT_WINDOW_MS) {
|
|
1254
|
+
if (windowDroppedCount > 0) {
|
|
1255
|
+
console.error(
|
|
1256
|
+
`[runner-activity-telemetry] rate cap reached: dropped ${windowDroppedCount} ${windowDroppedCount === 1 ? "entry" : "entries"} in the last ${RATE_LIMIT_WINDOW_MS / 1e3}s (cap ${RATE_LIMIT_MAX_EVENTS}/min)`
|
|
1257
|
+
);
|
|
1258
|
+
}
|
|
1259
|
+
windowStartedAt = now;
|
|
1260
|
+
windowCount = 0;
|
|
1261
|
+
windowDroppedCount = 0;
|
|
1262
|
+
}
|
|
1263
|
+
if (windowCount >= RATE_LIMIT_MAX_EVENTS) {
|
|
1264
|
+
windowDroppedCount++;
|
|
1265
|
+
if (windowDroppedCount === 1) {
|
|
1266
|
+
console.error(
|
|
1267
|
+
`[runner-activity-telemetry] rate cap reached (${RATE_LIMIT_MAX_EVENTS} per ${RATE_LIMIT_WINDOW_MS / 1e3}s) \u2014 dropping further entries this window`
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
return false;
|
|
1271
|
+
}
|
|
1272
|
+
windowCount++;
|
|
1273
|
+
return true;
|
|
1274
|
+
}
|
|
1275
|
+
function forwardRunnerActivity(entry, context) {
|
|
1276
|
+
try {
|
|
1277
|
+
if (!FORWARDED_LEVELS.has(entry.level)) return;
|
|
1278
|
+
if (!context.agentId || !context.authHeader) return;
|
|
1279
|
+
if (!admitUnderRateLimit(Date.now())) return;
|
|
1280
|
+
const rawMessage = entry.error ?? entry.message ?? "";
|
|
1281
|
+
const message = truncate(redact(rawMessage));
|
|
1282
|
+
logEvent(TelemetryEventTypes.RUNNER_ACTIVITY, {
|
|
1283
|
+
severity: SEVERITY_BY_LEVEL[entry.level],
|
|
1284
|
+
message,
|
|
1285
|
+
metadata: { source: "cli.run" },
|
|
1286
|
+
agentId: context.agentId
|
|
1287
|
+
});
|
|
1288
|
+
} catch (err) {
|
|
1289
|
+
console.error(
|
|
1290
|
+
`[runner-activity-telemetry] failed to forward runner activity: ${err instanceof Error ? err.message : String(err)}`
|
|
1291
|
+
);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
// src/lib/opencode/health.ts
|
|
1296
|
+
async function checkOpenCodeHealth(port) {
|
|
1297
|
+
try {
|
|
1298
|
+
const response = await fetch(`http://127.0.0.1:${port}/global/health`, {
|
|
1299
|
+
signal: AbortSignal.timeout(2e3)
|
|
1300
|
+
// 2 second timeout
|
|
1301
|
+
});
|
|
1302
|
+
if (!response.ok) {
|
|
1303
|
+
return { healthy: false, error: `HTTP ${response.status}` };
|
|
1304
|
+
}
|
|
1305
|
+
const data = await response.json().catch(() => ({}));
|
|
1306
|
+
return { healthy: true, version: data.version };
|
|
705
1307
|
} catch (error2) {
|
|
706
1308
|
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
707
1309
|
return { healthy: false, error: message };
|
|
@@ -714,7 +1316,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
714
1316
|
if (health.healthy) {
|
|
715
1317
|
return health;
|
|
716
1318
|
}
|
|
717
|
-
await new Promise((
|
|
1319
|
+
await new Promise((resolve3) => setTimeout(resolve3, 1e3));
|
|
718
1320
|
}
|
|
719
1321
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
720
1322
|
}
|
|
@@ -729,7 +1331,7 @@ function buildOpenCodeVersionWarning(version2) {
|
|
|
729
1331
|
if (isQueueValidatedVersion(version2)) return null;
|
|
730
1332
|
const detected = version2 ? `v${version2}` : "unknown";
|
|
731
1333
|
const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
|
|
732
|
-
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack
|
|
1334
|
+
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
|
|
733
1335
|
}
|
|
734
1336
|
|
|
735
1337
|
// src/lib/opencode/process.ts
|
|
@@ -1021,6 +1623,12 @@ async function promptOpenCodeInstall(interactive) {
|
|
|
1021
1623
|
return action;
|
|
1022
1624
|
}
|
|
1023
1625
|
|
|
1626
|
+
// src/lib/opencode/provider-check.ts
|
|
1627
|
+
function buildNoProviderWarning(hasProvider) {
|
|
1628
|
+
if (hasProvider !== false) return null;
|
|
1629
|
+
return "Warning: opencode has no authenticated model provider configured, so it won't be able to answer prompts. Run `opencode auth login` to set one up (see https://opencode.ai for details).";
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1024
1632
|
// src/lib/opencode/session.ts
|
|
1025
1633
|
function opencodeBase(port) {
|
|
1026
1634
|
return `http://127.0.0.1:${port}`;
|
|
@@ -1223,6 +1831,11 @@ async function getModelAttachmentCapability(port, model) {
|
|
|
1223
1831
|
}
|
|
1224
1832
|
const entry = provider.models[modelId];
|
|
1225
1833
|
if (!entry || typeof entry !== "object") return null;
|
|
1834
|
+
if (entry.capabilities && typeof entry.capabilities === "object") {
|
|
1835
|
+
if (typeof entry.capabilities.attachment === "boolean") {
|
|
1836
|
+
return entry.capabilities.attachment;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1226
1839
|
return typeof entry.attachment === "boolean" ? entry.attachment : null;
|
|
1227
1840
|
} catch (err) {
|
|
1228
1841
|
console.error(
|
|
@@ -1251,6 +1864,16 @@ async function buildFileParts(attachments, capable) {
|
|
|
1251
1864
|
);
|
|
1252
1865
|
dataUrl = null;
|
|
1253
1866
|
}
|
|
1867
|
+
if (dataUrl !== null && typeof dataUrl === "object") {
|
|
1868
|
+
outcomes.push({
|
|
1869
|
+
index: a.index,
|
|
1870
|
+
mime: a.mime,
|
|
1871
|
+
filename: a.filename,
|
|
1872
|
+
status: "failed",
|
|
1873
|
+
reason: "needs_reauth"
|
|
1874
|
+
});
|
|
1875
|
+
continue;
|
|
1876
|
+
}
|
|
1254
1877
|
if (dataUrl == null) {
|
|
1255
1878
|
outcomes.push({ index: a.index, mime: a.mime, filename: a.filename, status: "failed" });
|
|
1256
1879
|
continue;
|
|
@@ -1332,7 +1955,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
1332
1955
|
}
|
|
1333
1956
|
}
|
|
1334
1957
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
1335
|
-
await new Promise((
|
|
1958
|
+
await new Promise((resolve3) => setTimeout(resolve3, READ_BACK_DELAY_MS));
|
|
1336
1959
|
}
|
|
1337
1960
|
}
|
|
1338
1961
|
return null;
|
|
@@ -1460,6 +2083,9 @@ function isPreamblePinnedRunning(messages, userMessageId) {
|
|
|
1460
2083
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1461
2084
|
return completedOf(reply) != null && finishOf(reply) === "tool-calls";
|
|
1462
2085
|
}
|
|
2086
|
+
function isB2AbandonmentConfirmed(params) {
|
|
2087
|
+
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
|
|
2088
|
+
}
|
|
1463
2089
|
function messageError(messages, userMessageId) {
|
|
1464
2090
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1465
2091
|
const error2 = errorOf(reply);
|
|
@@ -1473,12 +2099,94 @@ function messageError(messages, userMessageId) {
|
|
|
1473
2099
|
}
|
|
1474
2100
|
return "The agent run failed.";
|
|
1475
2101
|
}
|
|
2102
|
+
function isAbortedTerminalReply(messages, userMessageId) {
|
|
2103
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
2104
|
+
const error2 = errorOf(reply);
|
|
2105
|
+
if (error2 == null) return false;
|
|
2106
|
+
if (typeof error2 === "string") return error2.trim() === "Aborted";
|
|
2107
|
+
if (typeof error2 === "object") {
|
|
2108
|
+
const e = error2;
|
|
2109
|
+
if (e.name === "MessageAbortedError") return true;
|
|
2110
|
+
if (e.name === "AbortError") return true;
|
|
2111
|
+
const dataMessage = e.data?.message;
|
|
2112
|
+
const rendered = typeof dataMessage === "string" ? dataMessage : typeof e.message === "string" ? e.message : null;
|
|
2113
|
+
return rendered != null && rendered.trim() === "Aborted";
|
|
2114
|
+
}
|
|
2115
|
+
return false;
|
|
2116
|
+
}
|
|
2117
|
+
function messageFailure(messages, userMessageId) {
|
|
2118
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
2119
|
+
const error2 = errorOf(reply);
|
|
2120
|
+
if (error2 == null || typeof error2 !== "object") return null;
|
|
2121
|
+
const e = error2;
|
|
2122
|
+
const replyProviderId = reply?.info?.providerID ?? null;
|
|
2123
|
+
const replyModelId = reply?.info?.modelID ?? null;
|
|
2124
|
+
if (e.name === "ProviderAuthError") {
|
|
2125
|
+
const data = e.data;
|
|
2126
|
+
const providerId = typeof data?.providerID === "string" && data.providerID || replyProviderId;
|
|
2127
|
+
return { kind: "model_auth", providerId, modelId: replyModelId, reason: "missing" };
|
|
2128
|
+
}
|
|
2129
|
+
if (e.name === "APIError") {
|
|
2130
|
+
const data = e.data;
|
|
2131
|
+
const statusCode = data?.statusCode;
|
|
2132
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
2133
|
+
return {
|
|
2134
|
+
kind: "model_auth",
|
|
2135
|
+
providerId: replyProviderId,
|
|
2136
|
+
modelId: replyModelId,
|
|
2137
|
+
reason: "rejected"
|
|
2138
|
+
};
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
return null;
|
|
2142
|
+
}
|
|
2143
|
+
function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProviderId, replyModelId = null) {
|
|
2144
|
+
if (classified != null) return classified;
|
|
2145
|
+
if (hasConfiguredProvider !== false) return null;
|
|
2146
|
+
return {
|
|
2147
|
+
kind: "model_auth",
|
|
2148
|
+
providerId: replyProviderId,
|
|
2149
|
+
modelId: replyModelId,
|
|
2150
|
+
reason: "missing"
|
|
2151
|
+
};
|
|
2152
|
+
}
|
|
1476
2153
|
function hasRunningAssistantExcept(messages, exceptUserMessageId) {
|
|
1477
2154
|
if (!messages || messages.length === 0) return false;
|
|
1478
2155
|
return messages.some(
|
|
1479
2156
|
(m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
|
|
1480
2157
|
);
|
|
1481
2158
|
}
|
|
2159
|
+
async function hasAnyConfiguredProvider(port) {
|
|
2160
|
+
try {
|
|
2161
|
+
const res = await fetch(`${opencodeBase(port)}/config/providers`);
|
|
2162
|
+
if (!res.ok) {
|
|
2163
|
+
console.error(
|
|
2164
|
+
`[hasAnyConfiguredProvider] GET /config/providers returned HTTP ${res.status} (port ${port})`
|
|
2165
|
+
);
|
|
2166
|
+
return null;
|
|
2167
|
+
}
|
|
2168
|
+
const body = await res.json();
|
|
2169
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
2170
|
+
console.error(
|
|
2171
|
+
`[hasAnyConfiguredProvider] GET /config/providers body was not a plain object (port ${port})`
|
|
2172
|
+
);
|
|
2173
|
+
return null;
|
|
2174
|
+
}
|
|
2175
|
+
const defaults2 = body.default;
|
|
2176
|
+
if (!defaults2 || typeof defaults2 !== "object" || Array.isArray(defaults2)) {
|
|
2177
|
+
console.error(
|
|
2178
|
+
`[hasAnyConfiguredProvider] GET /config/providers body had no \`default\` object (port ${port})`
|
|
2179
|
+
);
|
|
2180
|
+
return null;
|
|
2181
|
+
}
|
|
2182
|
+
return Object.keys(defaults2).length > 0;
|
|
2183
|
+
} catch (err) {
|
|
2184
|
+
console.error(
|
|
2185
|
+
`[hasAnyConfiguredProvider] GET /config/providers failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
2186
|
+
);
|
|
2187
|
+
return null;
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
1482
2190
|
|
|
1483
2191
|
// src/lib/opencode/session-cleanup.ts
|
|
1484
2192
|
var DURATION_UNIT_MS = {
|
|
@@ -1637,10 +2345,11 @@ var StreamForwarder = class {
|
|
|
1637
2345
|
* Abort every in-flight stream (e.g. on WebSocket close).
|
|
1638
2346
|
*/
|
|
1639
2347
|
abortAll() {
|
|
1640
|
-
for (const stream of this.inflight.
|
|
2348
|
+
for (const [sid, stream] of this.inflight.entries()) {
|
|
1641
2349
|
try {
|
|
1642
2350
|
stream.abort();
|
|
1643
|
-
} catch {
|
|
2351
|
+
} catch (err) {
|
|
2352
|
+
log("error", "forwarder_abort_failed", { sid, ...errorFields(err) });
|
|
1644
2353
|
}
|
|
1645
2354
|
}
|
|
1646
2355
|
this.inflight.clear();
|
|
@@ -1674,12 +2383,12 @@ var StreamForwarder = class {
|
|
|
1674
2383
|
let endBody;
|
|
1675
2384
|
if (has_body) {
|
|
1676
2385
|
const chunks = [];
|
|
1677
|
-
bodyPromise = new Promise((
|
|
2386
|
+
bodyPromise = new Promise((resolve3) => {
|
|
1678
2387
|
pushBody = (buf) => {
|
|
1679
2388
|
chunks.push(buf);
|
|
1680
2389
|
};
|
|
1681
2390
|
endBody = () => {
|
|
1682
|
-
|
|
2391
|
+
resolve3(Buffer.concat(chunks));
|
|
1683
2392
|
};
|
|
1684
2393
|
});
|
|
1685
2394
|
}
|
|
@@ -1790,31 +2499,20 @@ function connectTunnel(options) {
|
|
|
1790
2499
|
onConnected,
|
|
1791
2500
|
onDisconnected,
|
|
1792
2501
|
onError,
|
|
1793
|
-
onRequest,
|
|
1794
2502
|
onResponse,
|
|
1795
2503
|
onInfo,
|
|
1796
2504
|
onDrainPing
|
|
1797
2505
|
} = options;
|
|
1798
2506
|
const tunnelUrl = getTunnelUrlConfig();
|
|
1799
2507
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
1800
|
-
return new Promise((
|
|
2508
|
+
return new Promise((resolve3, reject) => {
|
|
1801
2509
|
const ws = new WebSocket2(url, {
|
|
1802
2510
|
headers: {
|
|
1803
2511
|
Authorization: authHeader
|
|
1804
2512
|
}
|
|
1805
2513
|
});
|
|
1806
|
-
const streamStartTimes = /* @__PURE__ */ new Map();
|
|
1807
2514
|
const forwarder = new StreamForwarder(ws, port, {
|
|
1808
|
-
|
|
1809
|
-
if (path === TUNNEL_DRAIN_PING_PATH) return;
|
|
1810
|
-
streamStartTimes.set(sid, Date.now());
|
|
1811
|
-
onRequest?.(method, path, sid);
|
|
1812
|
-
},
|
|
1813
|
-
onHead: (sid, status) => {
|
|
1814
|
-
const startedAt = streamStartTimes.get(sid);
|
|
1815
|
-
streamStartTimes.delete(sid);
|
|
1816
|
-
onResponse?.(status, startedAt ? Date.now() - startedAt : 0, sid);
|
|
1817
|
-
},
|
|
2515
|
+
onHead: () => onResponse?.(),
|
|
1818
2516
|
onDrainPing: () => onDrainPing?.()
|
|
1819
2517
|
});
|
|
1820
2518
|
const connectionTimeout = setTimeout(() => {
|
|
@@ -1862,7 +2560,7 @@ function connectTunnel(options) {
|
|
|
1862
2560
|
clearTimeout(connectionTimeout);
|
|
1863
2561
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
1864
2562
|
onConnected?.(connectedAgentId);
|
|
1865
|
-
|
|
2563
|
+
resolve3({
|
|
1866
2564
|
ws,
|
|
1867
2565
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
1868
2566
|
});
|
|
@@ -1890,7 +2588,6 @@ function connectTunnel(options) {
|
|
|
1890
2588
|
ws.on("close", (code, reason) => {
|
|
1891
2589
|
const reasonStr = reason.toString() || upgradeRejection || (code === 1006 ? "abnormal closure" : "No reason provided");
|
|
1892
2590
|
forwarder.abortAll();
|
|
1893
|
-
streamStartTimes.clear();
|
|
1894
2591
|
onDisconnected?.(code, reasonStr);
|
|
1895
2592
|
});
|
|
1896
2593
|
});
|
|
@@ -1925,7 +2622,11 @@ var RunnerConnection = class {
|
|
|
1925
2622
|
if (this.connection) {
|
|
1926
2623
|
try {
|
|
1927
2624
|
this.connection.close();
|
|
1928
|
-
} catch {
|
|
2625
|
+
} catch (err) {
|
|
2626
|
+
log("error", "runner_connection_close_failed", {
|
|
2627
|
+
agent_id: this.resolvedAgentId,
|
|
2628
|
+
...errorFields(err)
|
|
2629
|
+
});
|
|
1929
2630
|
}
|
|
1930
2631
|
this.connection = null;
|
|
1931
2632
|
}
|
|
@@ -1975,7 +2676,448 @@ var RunnerConnection = class {
|
|
|
1975
2676
|
}
|
|
1976
2677
|
this.reconnecting = false;
|
|
1977
2678
|
}
|
|
1978
|
-
};
|
|
2679
|
+
};
|
|
2680
|
+
|
|
2681
|
+
// src/lib/tunnel/ready-marker.ts
|
|
2682
|
+
import { writeFileSync } from "fs";
|
|
2683
|
+
function writeTunnelReadyMarker(path, agentId) {
|
|
2684
|
+
try {
|
|
2685
|
+
writeFileSync(path, `${agentId}
|
|
2686
|
+
`);
|
|
2687
|
+
return { ok: true };
|
|
2688
|
+
} catch (error2) {
|
|
2689
|
+
return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
|
|
2693
|
+
// src/lib/claude-usage-reporting.ts
|
|
2694
|
+
var VALID_MODES = ["auto", "on", "off"];
|
|
2695
|
+
function resolveClaudeUsageReportingMode(flagValue, env) {
|
|
2696
|
+
const raw = flagValue ?? env.EVIDENT_CLAUDE_USAGE_REPORTING;
|
|
2697
|
+
if (raw === void 0 || raw === "") {
|
|
2698
|
+
return { mode: "auto", warnings: [] };
|
|
2699
|
+
}
|
|
2700
|
+
const normalized = raw.trim().toLowerCase();
|
|
2701
|
+
if (VALID_MODES.includes(normalized)) {
|
|
2702
|
+
return { mode: normalized, warnings: [] };
|
|
2703
|
+
}
|
|
2704
|
+
const source = flagValue !== void 0 ? "--claude-usage-reporting" : "EVIDENT_CLAUDE_USAGE_REPORTING";
|
|
2705
|
+
return {
|
|
2706
|
+
mode: "auto",
|
|
2707
|
+
warnings: [
|
|
2708
|
+
`Ignoring invalid ${source} "${raw}": expected one of ${VALID_MODES.join(", ")}; using auto`
|
|
2709
|
+
]
|
|
2710
|
+
};
|
|
2711
|
+
}
|
|
2712
|
+
var BASE_REPORT_DELAY_MS = 10 * 6e4;
|
|
2713
|
+
var REPORT_DELAY_JITTER_FRACTION = 0.2;
|
|
2714
|
+
function nextReportDelayMs(random = Math.random) {
|
|
2715
|
+
const jitterRangeMs = BASE_REPORT_DELAY_MS * REPORT_DELAY_JITTER_FRACTION;
|
|
2716
|
+
return BASE_REPORT_DELAY_MS - jitterRangeMs + random() * (2 * jitterRangeMs);
|
|
2717
|
+
}
|
|
2718
|
+
var FIRST_REPORT_DELAY_MS = 5e3 + Math.random() * 1e4;
|
|
2719
|
+
var CLAUDE_USAGE_FAILURE_REESCALATION_TICKS = 6;
|
|
2720
|
+
function claudeUsageFailureLogLevel(consecutiveFailures) {
|
|
2721
|
+
return consecutiveFailures === 1 || consecutiveFailures % CLAUDE_USAGE_FAILURE_REESCALATION_TICKS === 0 ? "warn" : "debug";
|
|
2722
|
+
}
|
|
2723
|
+
|
|
2724
|
+
// src/lib/channels/driver.ts
|
|
2725
|
+
import { homedir as homedir2 } from "os";
|
|
2726
|
+
|
|
2727
|
+
// src/lib/file-push.ts
|
|
2728
|
+
import { randomUUID } from "crypto";
|
|
2729
|
+
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
2730
|
+
import { basename, dirname as dirname2, isAbsolute, join as join2, relative, resolve as resolve2, sep } from "path";
|
|
2731
|
+
var FILE_MODE = 384;
|
|
2732
|
+
var DIRECTORY_MODE = 448;
|
|
2733
|
+
async function writePushedFile(request) {
|
|
2734
|
+
const { requestedPath, content, allowedDirectories, homeDir } = request;
|
|
2735
|
+
const bytes = content.byteLength;
|
|
2736
|
+
if (allowedDirectories.length === 0) {
|
|
2737
|
+
return refuse("file_sync_disabled", "File sync is not enabled on this runner.", {
|
|
2738
|
+
path: requestedPath,
|
|
2739
|
+
bytes
|
|
2740
|
+
});
|
|
2741
|
+
}
|
|
2742
|
+
if (bytes > MAX_FILE_PUSH_BYTES) {
|
|
2743
|
+
return refuse(
|
|
2744
|
+
"file_too_large",
|
|
2745
|
+
`File is ${bytes} bytes; the limit is ${MAX_FILE_PUSH_BYTES}.`,
|
|
2746
|
+
{
|
|
2747
|
+
path: requestedPath,
|
|
2748
|
+
bytes
|
|
2749
|
+
}
|
|
2750
|
+
);
|
|
2751
|
+
}
|
|
2752
|
+
const candidate = expandAndValidate(requestedPath, homeDir);
|
|
2753
|
+
if (candidate === null) {
|
|
2754
|
+
return refuse("invalid_path", "The requested path is not a valid absolute file path.", {
|
|
2755
|
+
path: requestedPath,
|
|
2756
|
+
bytes
|
|
2757
|
+
});
|
|
2758
|
+
}
|
|
2759
|
+
try {
|
|
2760
|
+
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
2761
|
+
dirname2(candidate)
|
|
2762
|
+
);
|
|
2763
|
+
const realTarget = join2(existingAncestor, ...missingSegments, basename(candidate));
|
|
2764
|
+
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
2765
|
+
if (allowedDirectory === null) {
|
|
2766
|
+
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
2767
|
+
path: realTarget,
|
|
2768
|
+
bytes
|
|
2769
|
+
});
|
|
2770
|
+
}
|
|
2771
|
+
if (missingSegments.length > 0) {
|
|
2772
|
+
await createMissingDirectories(existingAncestor, missingSegments);
|
|
2773
|
+
const realParent = await realpath(dirname2(realTarget));
|
|
2774
|
+
if (realParent !== dirname2(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
2775
|
+
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
2776
|
+
path: realTarget,
|
|
2777
|
+
bytes,
|
|
2778
|
+
reason: "parent_changed_after_create"
|
|
2779
|
+
});
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
await writeAtomically(realTarget, content);
|
|
2783
|
+
log("info", "file_push_written", { path: realTarget, bytes });
|
|
2784
|
+
return { ok: true, path: realTarget };
|
|
2785
|
+
} catch (err) {
|
|
2786
|
+
const errno = err.code ?? "UNKNOWN";
|
|
2787
|
+
return refuse("write_failed", `The runner could not write the file (${errno}).`, {
|
|
2788
|
+
path: candidate,
|
|
2789
|
+
bytes,
|
|
2790
|
+
errno,
|
|
2791
|
+
...errorFields(err)
|
|
2792
|
+
});
|
|
2793
|
+
}
|
|
2794
|
+
}
|
|
2795
|
+
function expandAndValidate(requestedPath, homeDir) {
|
|
2796
|
+
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
2797
|
+
return null;
|
|
2798
|
+
}
|
|
2799
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join2(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
2800
|
+
if (expanded.split(/[/\\]/).includes("..")) {
|
|
2801
|
+
return null;
|
|
2802
|
+
}
|
|
2803
|
+
if (!isAbsolute(expanded)) {
|
|
2804
|
+
return null;
|
|
2805
|
+
}
|
|
2806
|
+
const candidate = resolve2(expanded);
|
|
2807
|
+
const name = basename(candidate);
|
|
2808
|
+
return name === "" || name === "." || name === ".." ? null : candidate;
|
|
2809
|
+
}
|
|
2810
|
+
async function resolveNearestExistingAncestor(directory) {
|
|
2811
|
+
const missingSegments = [];
|
|
2812
|
+
let current = directory;
|
|
2813
|
+
for (; ; ) {
|
|
2814
|
+
try {
|
|
2815
|
+
return { existingAncestor: await realpath(current), missingSegments };
|
|
2816
|
+
} catch (err) {
|
|
2817
|
+
const parent = dirname2(current);
|
|
2818
|
+
if (err.code !== "ENOENT" || parent === current) {
|
|
2819
|
+
throw err;
|
|
2820
|
+
}
|
|
2821
|
+
missingSegments.unshift(basename(current));
|
|
2822
|
+
current = parent;
|
|
2823
|
+
}
|
|
2824
|
+
}
|
|
2825
|
+
}
|
|
2826
|
+
async function findContainingAllowedDirectory(allowedDirectories, realTarget) {
|
|
2827
|
+
for (const directory of allowedDirectories) {
|
|
2828
|
+
if (!isAbsolute(directory)) {
|
|
2829
|
+
log("warn", "file_push_allowed_directory_skipped", { directory, reason: "not_absolute" });
|
|
2830
|
+
continue;
|
|
2831
|
+
}
|
|
2832
|
+
const realDirectory = await realpathCreatingIfMissing(directory);
|
|
2833
|
+
if (realDirectory !== null && contains(realDirectory, realTarget)) {
|
|
2834
|
+
return realDirectory;
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
return null;
|
|
2838
|
+
}
|
|
2839
|
+
async function realpathCreatingIfMissing(directory) {
|
|
2840
|
+
try {
|
|
2841
|
+
return await realpath(directory);
|
|
2842
|
+
} catch (err) {
|
|
2843
|
+
if (err.code !== "ENOENT") {
|
|
2844
|
+
log("warn", "file_push_allowed_directory_skipped", {
|
|
2845
|
+
directory,
|
|
2846
|
+
reason: "unresolvable",
|
|
2847
|
+
...errorFields(err)
|
|
2848
|
+
});
|
|
2849
|
+
return null;
|
|
2850
|
+
}
|
|
2851
|
+
}
|
|
2852
|
+
try {
|
|
2853
|
+
await mkdir(directory, { recursive: true, mode: DIRECTORY_MODE });
|
|
2854
|
+
await chmod(directory, DIRECTORY_MODE);
|
|
2855
|
+
return await realpath(directory);
|
|
2856
|
+
} catch (err) {
|
|
2857
|
+
log("warn", "file_push_allowed_directory_skipped", {
|
|
2858
|
+
directory,
|
|
2859
|
+
reason: "create_failed",
|
|
2860
|
+
...errorFields(err)
|
|
2861
|
+
});
|
|
2862
|
+
return null;
|
|
2863
|
+
}
|
|
2864
|
+
}
|
|
2865
|
+
function contains(realDirectory, realTarget) {
|
|
2866
|
+
const rel = relative(realDirectory, realTarget);
|
|
2867
|
+
return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
2868
|
+
}
|
|
2869
|
+
async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
2870
|
+
let current = existingAncestor;
|
|
2871
|
+
for (const segment of missingSegments) {
|
|
2872
|
+
current = join2(current, segment);
|
|
2873
|
+
await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
|
|
2874
|
+
await chmod(current, DIRECTORY_MODE);
|
|
2875
|
+
}
|
|
2876
|
+
}
|
|
2877
|
+
async function writeAtomically(realTarget, content) {
|
|
2878
|
+
const temporaryPath = join2(dirname2(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
2879
|
+
let handle;
|
|
2880
|
+
try {
|
|
2881
|
+
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
2882
|
+
await handle.writeFile(content);
|
|
2883
|
+
await handle.chmod(FILE_MODE);
|
|
2884
|
+
await handle.close();
|
|
2885
|
+
handle = void 0;
|
|
2886
|
+
await rename(temporaryPath, realTarget);
|
|
2887
|
+
} catch (err) {
|
|
2888
|
+
await discardTemporaryFile(temporaryPath, handle);
|
|
2889
|
+
throw err;
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
async function discardTemporaryFile(temporaryPath, handle) {
|
|
2893
|
+
try {
|
|
2894
|
+
await handle?.close();
|
|
2895
|
+
} catch (err) {
|
|
2896
|
+
log("warn", "file_push_temp_close_failed", { path: temporaryPath, ...errorFields(err) });
|
|
2897
|
+
}
|
|
2898
|
+
try {
|
|
2899
|
+
await unlink(temporaryPath);
|
|
2900
|
+
} catch (err) {
|
|
2901
|
+
const errno = err.code;
|
|
2902
|
+
if (errno !== "ENOENT" && errno !== "ENOTDIR") {
|
|
2903
|
+
log("warn", "file_push_temp_cleanup_failed", { path: temporaryPath, ...errorFields(err) });
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
function refuse(code, message, fields) {
|
|
2908
|
+
log(code === "write_failed" ? "error" : "warn", "file_push_refused", { code, ...fields });
|
|
2909
|
+
return { ok: false, code, message };
|
|
2910
|
+
}
|
|
2911
|
+
|
|
2912
|
+
// src/lib/runner-file-sync.ts
|
|
2913
|
+
var MAX_ACK_ATTEMPTS = 5;
|
|
2914
|
+
async function syncPendingRunnerFiles(options) {
|
|
2915
|
+
const pending = await listPendingFiles(options);
|
|
2916
|
+
const pendingIds = new Set(pending.map((file) => file.id));
|
|
2917
|
+
for (const id of options.ackFailures.keys()) {
|
|
2918
|
+
if (!pendingIds.has(id)) options.ackFailures.delete(id);
|
|
2919
|
+
}
|
|
2920
|
+
if (pending.length === 0) return 0;
|
|
2921
|
+
options.log({
|
|
2922
|
+
level: "info",
|
|
2923
|
+
message: `Runner file sync: ${pending.length} file(s) queued for this runner`
|
|
2924
|
+
});
|
|
2925
|
+
let applied = 0;
|
|
2926
|
+
for (const file of pending) {
|
|
2927
|
+
if ((options.ackFailures.get(file.id) ?? 0) >= MAX_ACK_ATTEMPTS) continue;
|
|
2928
|
+
if (await applyOne(options, file)) applied += 1;
|
|
2929
|
+
}
|
|
2930
|
+
return applied;
|
|
2931
|
+
}
|
|
2932
|
+
async function listPendingFiles(options) {
|
|
2933
|
+
let res;
|
|
2934
|
+
try {
|
|
2935
|
+
res = await options.fetchImpl(`${options.apiUrl}/runners/${options.agentId}/files/pending`, {
|
|
2936
|
+
headers: { Authorization: options.getAuthHeader() }
|
|
2937
|
+
});
|
|
2938
|
+
} catch (err) {
|
|
2939
|
+
options.log({
|
|
2940
|
+
level: "warn",
|
|
2941
|
+
message: `Could not list pending runner files \u2014 retrying on the next drain: ${describe(err)}`
|
|
2942
|
+
});
|
|
2943
|
+
return [];
|
|
2944
|
+
}
|
|
2945
|
+
if (!res.ok) {
|
|
2946
|
+
options.log({
|
|
2947
|
+
level: res.status === 404 ? "debug" : "warn",
|
|
2948
|
+
message: `Listing pending runner files returned HTTP ${res.status} \u2014 retrying on the next drain`
|
|
2949
|
+
});
|
|
2950
|
+
return [];
|
|
2951
|
+
}
|
|
2952
|
+
let body;
|
|
2953
|
+
try {
|
|
2954
|
+
body = await res.json();
|
|
2955
|
+
} catch (err) {
|
|
2956
|
+
options.log({
|
|
2957
|
+
level: "warn",
|
|
2958
|
+
message: `Pending runner file list was not readable JSON \u2014 retrying on the next drain: ${describe(err)}`
|
|
2959
|
+
});
|
|
2960
|
+
return [];
|
|
2961
|
+
}
|
|
2962
|
+
if (!Array.isArray(body)) {
|
|
2963
|
+
options.log({
|
|
2964
|
+
level: "warn",
|
|
2965
|
+
message: "Pending runner file list was not an array \u2014 ignoring it for this drain"
|
|
2966
|
+
});
|
|
2967
|
+
return [];
|
|
2968
|
+
}
|
|
2969
|
+
const files = [];
|
|
2970
|
+
for (const entry of body) {
|
|
2971
|
+
const file = asPendingFile(entry);
|
|
2972
|
+
if (file === null) {
|
|
2973
|
+
options.log({
|
|
2974
|
+
level: "warn",
|
|
2975
|
+
message: "Ignoring a malformed pending runner file entry (expected id, path and size)"
|
|
2976
|
+
});
|
|
2977
|
+
continue;
|
|
2978
|
+
}
|
|
2979
|
+
files.push(file);
|
|
2980
|
+
}
|
|
2981
|
+
return files;
|
|
2982
|
+
}
|
|
2983
|
+
function asPendingFile(entry) {
|
|
2984
|
+
if (entry === null || typeof entry !== "object") return null;
|
|
2985
|
+
const { id, path, size } = entry;
|
|
2986
|
+
if (typeof id !== "string" || id === "") return null;
|
|
2987
|
+
if (typeof path !== "string" || path === "") return null;
|
|
2988
|
+
if (typeof size !== "number" || !Number.isFinite(size) || size < 0) return null;
|
|
2989
|
+
return { id, path, size };
|
|
2990
|
+
}
|
|
2991
|
+
async function applyOne(options, file) {
|
|
2992
|
+
const label = `${file.id.slice(0, 8)} (${file.path})`;
|
|
2993
|
+
if (options.allowedDirectories.length === 0) {
|
|
2994
|
+
options.log({
|
|
2995
|
+
level: "warn",
|
|
2996
|
+
message: `Runner file ${label} rejected: file sync is not enabled on this runner (start it with --enable-file-sync-to)`
|
|
2997
|
+
});
|
|
2998
|
+
await ack(options, file, "rejected", "file_sync_disabled");
|
|
2999
|
+
return false;
|
|
3000
|
+
}
|
|
3001
|
+
if (file.size > MAX_FILE_PUSH_BYTES) {
|
|
3002
|
+
options.log({
|
|
3003
|
+
level: "warn",
|
|
3004
|
+
message: `Runner file ${label} rejected: declared ${file.size} bytes, the limit is ${MAX_FILE_PUSH_BYTES}`
|
|
3005
|
+
});
|
|
3006
|
+
await ack(options, file, "rejected", "file_too_large");
|
|
3007
|
+
return false;
|
|
3008
|
+
}
|
|
3009
|
+
const download = await downloadContent(options, file, label);
|
|
3010
|
+
if (!download.ok) {
|
|
3011
|
+
if (download.terminal) await ack(options, file, "rejected", download.code);
|
|
3012
|
+
return false;
|
|
3013
|
+
}
|
|
3014
|
+
let outcome;
|
|
3015
|
+
try {
|
|
3016
|
+
outcome = await writePushedFile({
|
|
3017
|
+
requestedPath: file.path,
|
|
3018
|
+
content: download.content,
|
|
3019
|
+
allowedDirectories: options.allowedDirectories,
|
|
3020
|
+
homeDir: options.homeDir
|
|
3021
|
+
});
|
|
3022
|
+
} catch (err) {
|
|
3023
|
+
options.log({
|
|
3024
|
+
level: "error",
|
|
3025
|
+
message: `Runner file ${label} could not be written: ${describe(err)}`
|
|
3026
|
+
});
|
|
3027
|
+
await ack(options, file, "rejected", "write_failed");
|
|
3028
|
+
return false;
|
|
3029
|
+
}
|
|
3030
|
+
if (!outcome.ok) {
|
|
3031
|
+
options.log({
|
|
3032
|
+
level: "warn",
|
|
3033
|
+
message: `Runner file ${label} rejected (${outcome.code}): ${outcome.message}`
|
|
3034
|
+
});
|
|
3035
|
+
await ack(options, file, "rejected", outcome.code);
|
|
3036
|
+
return false;
|
|
3037
|
+
}
|
|
3038
|
+
options.log({
|
|
3039
|
+
level: "info",
|
|
3040
|
+
message: `Runner file ${label} applied (${download.content.byteLength} bytes)`
|
|
3041
|
+
});
|
|
3042
|
+
await ack(options, file, "applied");
|
|
3043
|
+
return true;
|
|
3044
|
+
}
|
|
3045
|
+
function durableDownloadCode(status2) {
|
|
3046
|
+
return status2 === 413 ? "file_too_large" : "write_failed";
|
|
3047
|
+
}
|
|
3048
|
+
async function downloadContent(options, file, label) {
|
|
3049
|
+
try {
|
|
3050
|
+
const res = await options.fetchImpl(
|
|
3051
|
+
`${options.apiUrl}/runners/${options.agentId}/files/${file.id}/content`,
|
|
3052
|
+
{ headers: { Authorization: options.getAuthHeader() } }
|
|
3053
|
+
);
|
|
3054
|
+
if (!res.ok) {
|
|
3055
|
+
const terminal = res.status >= 400 && res.status < 500 && res.status !== 401 && res.status !== 403 && res.status !== 408 && res.status !== 429;
|
|
3056
|
+
if (!terminal) {
|
|
3057
|
+
options.log({
|
|
3058
|
+
level: "warn",
|
|
3059
|
+
message: `Downloading runner file ${label} returned HTTP ${res.status} \u2014 retrying on the next drain`
|
|
3060
|
+
});
|
|
3061
|
+
return { ok: false, terminal: false };
|
|
3062
|
+
}
|
|
3063
|
+
const code = durableDownloadCode(res.status);
|
|
3064
|
+
options.log({
|
|
3065
|
+
level: "error",
|
|
3066
|
+
message: `Downloading runner file ${label} returned HTTP ${res.status} \u2014 rejecting it as ${code} (the bytes never reached the writer)`
|
|
3067
|
+
});
|
|
3068
|
+
return { ok: false, terminal: true, code };
|
|
3069
|
+
}
|
|
3070
|
+
return { ok: true, content: Buffer.from(await res.arrayBuffer()) };
|
|
3071
|
+
} catch (err) {
|
|
3072
|
+
options.log({
|
|
3073
|
+
level: "warn",
|
|
3074
|
+
message: `Downloading runner file ${label} failed \u2014 retrying on the next drain: ${describe(err)}`
|
|
3075
|
+
});
|
|
3076
|
+
return { ok: false, terminal: false };
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
async function ack(options, file, status2, reason) {
|
|
3080
|
+
const outcome = `${status2}${reason ? ` (${reason})` : ""}`;
|
|
3081
|
+
try {
|
|
3082
|
+
const res = await options.fetchImpl(
|
|
3083
|
+
`${options.apiUrl}/runners/${options.agentId}/files/${file.id}/ack`,
|
|
3084
|
+
{
|
|
3085
|
+
method: "POST",
|
|
3086
|
+
headers: {
|
|
3087
|
+
Authorization: options.getAuthHeader(),
|
|
3088
|
+
"Content-Type": "application/json"
|
|
3089
|
+
},
|
|
3090
|
+
body: JSON.stringify(reason ? { status: status2, reason } : { status: status2 })
|
|
3091
|
+
}
|
|
3092
|
+
);
|
|
3093
|
+
if (!res.ok) {
|
|
3094
|
+
recordAckFailure(
|
|
3095
|
+
options,
|
|
3096
|
+
file,
|
|
3097
|
+
`Acking runner file ${file.id.slice(0, 8)} as ${outcome} returned HTTP ${res.status}`
|
|
3098
|
+
);
|
|
3099
|
+
return;
|
|
3100
|
+
}
|
|
3101
|
+
options.ackFailures.delete(file.id);
|
|
3102
|
+
} catch (err) {
|
|
3103
|
+
recordAckFailure(
|
|
3104
|
+
options,
|
|
3105
|
+
file,
|
|
3106
|
+
`Acking runner file ${file.id.slice(0, 8)} as ${outcome} failed: ${describe(err)}`
|
|
3107
|
+
);
|
|
3108
|
+
}
|
|
3109
|
+
}
|
|
3110
|
+
function recordAckFailure(options, file, what) {
|
|
3111
|
+
const attempts = (options.ackFailures.get(file.id) ?? 0) + 1;
|
|
3112
|
+
options.ackFailures.set(file.id, attempts);
|
|
3113
|
+
options.log({
|
|
3114
|
+
level: "error",
|
|
3115
|
+
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})`
|
|
3116
|
+
});
|
|
3117
|
+
}
|
|
3118
|
+
function describe(err) {
|
|
3119
|
+
return err instanceof Error ? err.message : String(err);
|
|
3120
|
+
}
|
|
1979
3121
|
|
|
1980
3122
|
// src/lib/channels/driver.ts
|
|
1981
3123
|
function messageIdOf(m) {
|
|
@@ -2005,7 +3147,11 @@ var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
|
2005
3147
|
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
2006
3148
|
var HEARTBEAT_MS = 6e4;
|
|
2007
3149
|
var ABSOLUTE_MAX_PROCESSING_MS = 6 * 60 * 60 * 1e3;
|
|
3150
|
+
var B2_ABANDONMENT_MIN_PINNED_MS = 3 * 6e4;
|
|
3151
|
+
var B2_ABANDONMENT_RECHECK_MS = HEARTBEAT_MS;
|
|
2008
3152
|
var POLL_MISS_GRACE_MS = HEARTBEAT_MS;
|
|
3153
|
+
var MAX_SUPERSEDED_CONVERSATIONS = 256;
|
|
3154
|
+
var MAX_IDENTICAL_REDRIVE_POLL_FAILURES = 5;
|
|
2009
3155
|
var ChannelAuthError = class extends Error {
|
|
2010
3156
|
constructor(message) {
|
|
2011
3157
|
super(message);
|
|
@@ -2014,10 +3160,10 @@ var ChannelAuthError = class extends Error {
|
|
|
2014
3160
|
};
|
|
2015
3161
|
var ChannelTerminalError = class extends Error {
|
|
2016
3162
|
status;
|
|
2017
|
-
constructor(message,
|
|
3163
|
+
constructor(message, status2) {
|
|
2018
3164
|
super(message);
|
|
2019
3165
|
this.name = "ChannelTerminalError";
|
|
2020
|
-
this.status =
|
|
3166
|
+
this.status = status2;
|
|
2021
3167
|
}
|
|
2022
3168
|
};
|
|
2023
3169
|
function backoffDelay(attempt, policy) {
|
|
@@ -2025,10 +3171,14 @@ function backoffDelay(attempt, policy) {
|
|
|
2025
3171
|
const capped = Math.min(policy.maxDelayMs, exp);
|
|
2026
3172
|
return Math.floor(Math.random() * capped);
|
|
2027
3173
|
}
|
|
2028
|
-
function isRetryableStatus(
|
|
2029
|
-
return
|
|
3174
|
+
function isRetryableStatus(status2) {
|
|
3175
|
+
return status2 === 429 || status2 >= 500 && status2 <= 599;
|
|
3176
|
+
}
|
|
3177
|
+
var VOLATILE_BODY_FIELD_PATTERN = /("(?:ref|requestId|request_id|traceId|trace_id)"\s*:\s*)"[^"]*"/gi;
|
|
3178
|
+
function normalizeRedrivePollFailureBody(body) {
|
|
3179
|
+
return body.replace(VOLATILE_BODY_FIELD_PATTERN, '$1"<redacted>"').replace(/\s+/g, " ").trim().slice(0, 200);
|
|
2030
3180
|
}
|
|
2031
|
-
var ChannelDriver = class {
|
|
3181
|
+
var ChannelDriver = class _ChannelDriver {
|
|
2032
3182
|
agentId;
|
|
2033
3183
|
port;
|
|
2034
3184
|
apiUrl;
|
|
@@ -2042,8 +3192,38 @@ var ChannelDriver = class {
|
|
|
2042
3192
|
pausedMaxWaitMs;
|
|
2043
3193
|
stuckQueuedMs;
|
|
2044
3194
|
now;
|
|
3195
|
+
fileSyncDirectories;
|
|
3196
|
+
homeDir;
|
|
2045
3197
|
/** Cache of conversationId → opencode sessionId. */
|
|
2046
3198
|
sessions = /* @__PURE__ */ new Map();
|
|
3199
|
+
/**
|
|
3200
|
+
* conversationId → the opencode session this runner has ABANDONED as that
|
|
3201
|
+
* conversation's binding (#553), after a genuine (`sessionExists === true`)
|
|
3202
|
+
* dispatch failure: the session still exists but is wedged, so #485's self-heal
|
|
3203
|
+
* must bind a fresh one.
|
|
3204
|
+
*
|
|
3205
|
+
* Dropping the local binding + clearing the server row is not enough on its own:
|
|
3206
|
+
* a SIBLING message dispatched earlier in the same drain is still in-flight under
|
|
3207
|
+
* the same session, and its watcher's routine status writes carry
|
|
3208
|
+
* `opencode_session_id`, RESURRECTING the wedged id server-side after the clear —
|
|
3209
|
+
* and `ensureSession`'s persisted-id fallback then reuses it, defeating the
|
|
3210
|
+
* self-heal. This map makes the runner authoritative instead of racing those
|
|
3211
|
+
* writes: *`ensureSession` never reuses an abandoned id for that conversation,
|
|
3212
|
+
* whatever the server row says* — which holds even when the resurrecting write
|
|
3213
|
+
* is one we deliberately keep (see `markDone`).
|
|
3214
|
+
*
|
|
3215
|
+
* Bounded by construction, on both axes: keyed by CONVERSATION, so N failures on
|
|
3216
|
+
* one conversation hold ONE entry (the newest abandonment replaces the older), and
|
|
3217
|
+
* hard-capped at `MAX_SUPERSEDED_CONVERSATIONS` with FIFO eviction. Only the
|
|
3218
|
+
* NEWEST abandoned id per conversation is guarded: after a second abandonment a
|
|
3219
|
+
* late sibling of the FIRST session can write that id back and `ensureSession`
|
|
3220
|
+
* will reuse it — costing ONE repeat failure, which re-supersedes it. Deliberately
|
|
3221
|
+
* NOT dropped when the session's watcher tears down: `markDone` still writes the
|
|
3222
|
+
* abandoned id back (it must, or the reply is lost), so the guard has to outlive
|
|
3223
|
+
* the turn that resurrects it. In-memory only — a restart forgets it, at the same
|
|
3224
|
+
* bounded cost.
|
|
3225
|
+
*/
|
|
3226
|
+
supersededSessions = /* @__PURE__ */ new Map();
|
|
2047
3227
|
/**
|
|
2048
3228
|
* Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
|
|
2049
3229
|
* longer idempotent (no caller-supplied `messageID`), and its read-back picks
|
|
@@ -2111,6 +3291,84 @@ var ChannelDriver = class {
|
|
|
2111
3291
|
* processing list, exactly like `dontRedispatch`/`doneUndeliverable`.
|
|
2112
3292
|
*/
|
|
2113
3293
|
readoptPollUnresolvedSignalled = /* @__PURE__ */ new Set();
|
|
3294
|
+
/**
|
|
3295
|
+
* "Already emitted `redrive_unresolved` for this row" (#965). Mirrors
|
|
3296
|
+
* `readoptPollUnresolvedSignalled`: `resolveRedrive`'s `unresolved` leaf recurs
|
|
3297
|
+
* every ~2s drain until opencode's status becomes readable, but the
|
|
3298
|
+
* server-visible signal is an OUTCOME, so it fires at most once per row. Cleared
|
|
3299
|
+
* on any non-`unresolved` outcome so the set cannot grow beyond the currently
|
|
3300
|
+
* unresolvable rows.
|
|
3301
|
+
*/
|
|
3302
|
+
redriveUnresolvedSignalled = /* @__PURE__ */ new Set();
|
|
3303
|
+
/**
|
|
3304
|
+
* First `now()` a `pending` row's re-drive was observed `unresolved` (#965). A
|
|
3305
|
+
* `pending` row is invisible to every cron arm (all require `status =
|
|
3306
|
+
* 'processing'`), so an indefinitely-`unresolved` row would be stranded with
|
|
3307
|
+
* nothing driving it. Once `now - since >= pausedMaxWaitMs`, `resolveRedrive`
|
|
3308
|
+
* takes `dispatch` instead of `unresolved` (reusing the existing knob — see
|
|
3309
|
+
* ADR-0047's own "unreachable ⇒ bounded" rule). Cleared on any other outcome.
|
|
3310
|
+
*/
|
|
3311
|
+
redriveUnresolvedSince = /* @__PURE__ */ new Map();
|
|
3312
|
+
/**
|
|
3313
|
+
* Consecutive-identical-poll-failure streak for the re-drive fence (#1348),
|
|
3314
|
+
* keyed by Evident **message id** (not session) so `clearRedriveUnresolved`
|
|
3315
|
+
* can drop it with the other two trackers and it cannot leak. `sessionId` is
|
|
3316
|
+
* carried inside the entry, not the key: a session change is a different
|
|
3317
|
+
* situation and resets the streak, which gives the `(sessionId, message.id)`
|
|
3318
|
+
* pairing #1348 asks for without a composite map key.
|
|
3319
|
+
*/
|
|
3320
|
+
redrivePollFailures = /* @__PURE__ */ new Map();
|
|
3321
|
+
/**
|
|
3322
|
+
* "Already emitted `redrive_outcome_unreported` for THIS (message, outcome)
|
|
3323
|
+
* streak" (Class B, #1340: the runner DECIDED reattach/settle/fail_permanent
|
|
3324
|
+
* but its own PATCH to record it failed — distinct from Class A's
|
|
3325
|
+
* `redrive_poll_failed`, where opencode itself can't be observed). Keyed by
|
|
3326
|
+
* message id, valued by the outcome currently failing to report, so a
|
|
3327
|
+
* change of outcome starts a fresh signal. Cleared by
|
|
3328
|
+
* `clearRedriveUnresolved` the instant either PATCH succeeds.
|
|
3329
|
+
*/
|
|
3330
|
+
redriveOutcomeUnreportedSignalled = /* @__PURE__ */ new Map();
|
|
3331
|
+
/**
|
|
3332
|
+
* First `now()` a Class B outcome PATCH (reattach/settle/fail_permanent) was
|
|
3333
|
+
* observed to fail for this message (#1366's failure-window trip arm,
|
|
3334
|
+
* `boundRedriveOutcome`). Duration, not a tick count — bounded by the
|
|
3335
|
+
* existing `pausedMaxWaitMs` window (reusing the knob, not a new constant).
|
|
3336
|
+
* Cleared by `clearRedriveUnresolved` the instant the original PATCH
|
|
3337
|
+
* succeeds.
|
|
3338
|
+
*/
|
|
3339
|
+
redriveOutcomeFailingSince = /* @__PURE__ */ new Map();
|
|
3340
|
+
/**
|
|
3341
|
+
* "Already posted `redrive_outcome_abandoned` with `reported: false` for this
|
|
3342
|
+
* row" (#1366) — the bound tripped but the terminal `markFailed` fallback ALSO
|
|
3343
|
+
* failed (the route-level fault of G2), so every following tick re-attempts
|
|
3344
|
+
* the same terminal PATCH. Guards that quiet retry from re-signalling on
|
|
3345
|
+
* every tick. Cleared by `clearRedriveUnresolved`.
|
|
3346
|
+
*/
|
|
3347
|
+
redriveOutcomeAbandonedSignalled = /* @__PURE__ */ new Set();
|
|
3348
|
+
/**
|
|
3349
|
+
* "Already emitted `dispatch_not_started` for THIS (message, branch) streak"
|
|
3350
|
+
* (#1340). Valued by the branch currently firing, so a row that moves between
|
|
3351
|
+
* exits re-signals — the move IS the finding. Cleared only on a CONFIRMED
|
|
3352
|
+
* dispatch, never on the fence's decision to dispatch: `clearRedriveUnresolved`
|
|
3353
|
+
* runs on that decision (`resolveRedriveUnresolved`), so clearing there would
|
|
3354
|
+
* re-signal on every one of the 15h of re-dispatch attempts #1110 made.
|
|
3355
|
+
*/
|
|
3356
|
+
dispatchNotStartedSignalled = /* @__PURE__ */ new Map();
|
|
3357
|
+
/**
|
|
3358
|
+
* Consecutive-UNCONFIRMED-dispatch streak for a `pending` row with NO stored
|
|
3359
|
+
* `opencode_message_id` yet — i.e. one that has never even reached the
|
|
3360
|
+
* re-drive fence above. `sendPromptAsync`'s POST may 2xx, but its own
|
|
3361
|
+
* read-back retries can never confirm the assigned id when the session's
|
|
3362
|
+
* message list is PERMANENTLY unreadable (e.g. a corrupted local opencode
|
|
3363
|
+
* SQLite DB, #1345/#1348's exact fault, just hit BEFORE the row is ever
|
|
3364
|
+
* dispatched instead of after). Unlike an already-dispatched row, THIS row has
|
|
3365
|
+
* no other safety net at all: the lifecycle cron only reclaims `status =
|
|
3366
|
+
* 'processing'` rows, and a row stuck here never reaches `processing`. Keyed
|
|
3367
|
+
* by message id, carrying `sessionId` so a session change (a fresh one bound
|
|
3368
|
+
* after abandonment) starts a new streak rather than inheriting the old
|
|
3369
|
+
* session's count — same shape as `redrivePollFailures` above.
|
|
3370
|
+
*/
|
|
3371
|
+
unconfirmedDispatchFailures = /* @__PURE__ */ new Map();
|
|
2114
3372
|
/**
|
|
2115
3373
|
* "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
|
|
2116
3374
|
* Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
|
|
@@ -2153,9 +3411,12 @@ var ChannelDriver = class {
|
|
|
2153
3411
|
sessionParents = /* @__PURE__ */ new Map();
|
|
2154
3412
|
/**
|
|
2155
3413
|
* Per-session OpenCode title cache (#310), keyed by sessionId. Only a resolved
|
|
2156
|
-
* NON-EMPTY name is stored (terminal — a real session name
|
|
2157
|
-
* so we do NOT re-GET `/session/:id` every tick.
|
|
2158
|
-
*
|
|
3414
|
+
* NON-EMPTY, non-placeholder name is stored (terminal — a real session name
|
|
3415
|
+
* won't later un-name), so we do NOT re-GET `/session/:id` every tick. "Non-empty"
|
|
3416
|
+
* excludes OpenCode's synchronous default title (see
|
|
3417
|
+
* `OPENCODE_DEFAULT_TITLE_PREFIX`, #549) — that placeholder is treated the same
|
|
3418
|
+
* as an empty title so it never latches. A missing entry = not yet resolved OR
|
|
3419
|
+
* resolved-but-still-empty/placeholder → re-fetch on next need, since OpenCode
|
|
2159
3420
|
* names sessions asynchronously mid-turn. Driver-level (not per-watcher) so both
|
|
2160
3421
|
* the watcher completion path AND the restart-recovery re-adopt path (which has
|
|
2161
3422
|
* no watcher) can resolve the title.
|
|
@@ -2163,6 +3424,24 @@ var ChannelDriver = class {
|
|
|
2163
3424
|
sessionTitles = /* @__PURE__ */ new Map();
|
|
2164
3425
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
2165
3426
|
draining = false;
|
|
3427
|
+
/**
|
|
3428
|
+
* Serialises runner-file syncs (#559) so the ~2s poll tick and a concurrent
|
|
3429
|
+
* drain ping don't download, write and ack the same file twice.
|
|
3430
|
+
*/
|
|
3431
|
+
syncingFiles = false;
|
|
3432
|
+
/**
|
|
3433
|
+
* Consecutive failed acks per pending file (#559). Lives on the driver so it
|
|
3434
|
+
* survives across drains — without it, a file whose ack keeps failing is
|
|
3435
|
+
* re-downloaded and re-written every ~2s until the server expires it.
|
|
3436
|
+
*/
|
|
3437
|
+
fileAckFailures = /* @__PURE__ */ new Map();
|
|
3438
|
+
/**
|
|
3439
|
+
* Monotonic count of files this runner has pulled and written (#559). Only
|
|
3440
|
+
* ever increases, so `run.ts` detects work by comparing it against the value
|
|
3441
|
+
* it saw on the previous cycle — including work that landed mid-sleep, the
|
|
3442
|
+
* same trick `lastProxiedActivityAt` uses.
|
|
3443
|
+
*/
|
|
3444
|
+
appliedFileCount = 0;
|
|
2166
3445
|
/**
|
|
2167
3446
|
* The currently-executing `drainPending()` promise, or null when idle. Lets a
|
|
2168
3447
|
* graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
|
|
@@ -2178,21 +3457,23 @@ var ChannelDriver = class {
|
|
|
2178
3457
|
* and stops opencode.
|
|
2179
3458
|
*/
|
|
2180
3459
|
stopped = false;
|
|
2181
|
-
constructor(
|
|
2182
|
-
this.agentId =
|
|
2183
|
-
this.port =
|
|
2184
|
-
this.apiUrl =
|
|
2185
|
-
this.getAuthHeader =
|
|
2186
|
-
this.conversationFilter =
|
|
2187
|
-
this.retry = { ...DEFAULT_RETRY_POLICY, ...
|
|
2188
|
-
this.log =
|
|
3460
|
+
constructor(config) {
|
|
3461
|
+
this.agentId = config.agentId;
|
|
3462
|
+
this.port = config.port;
|
|
3463
|
+
this.apiUrl = config.apiUrl.replace(/\/$/, "");
|
|
3464
|
+
this.getAuthHeader = config.getAuthHeader;
|
|
3465
|
+
this.conversationFilter = config.conversationFilter ?? null;
|
|
3466
|
+
this.retry = { ...DEFAULT_RETRY_POLICY, ...config.retry };
|
|
3467
|
+
this.log = config.log ?? (() => {
|
|
2189
3468
|
});
|
|
2190
|
-
this.fetchImpl =
|
|
2191
|
-
this.sleep =
|
|
2192
|
-
this.pausedPollIntervalMs =
|
|
2193
|
-
this.pausedMaxWaitMs =
|
|
2194
|
-
this.stuckQueuedMs =
|
|
2195
|
-
this.now =
|
|
3469
|
+
this.fetchImpl = config.fetchImpl ?? fetch;
|
|
3470
|
+
this.sleep = config.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
3471
|
+
this.pausedPollIntervalMs = config.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
|
|
3472
|
+
this.pausedMaxWaitMs = config.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
|
|
3473
|
+
this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
3474
|
+
this.now = config.now ?? (() => Date.now());
|
|
3475
|
+
this.fileSyncDirectories = config.fileSyncDirectories ?? [];
|
|
3476
|
+
this.homeDir = config.homeDir ?? homedir2();
|
|
2196
3477
|
}
|
|
2197
3478
|
/** The IPv4-loopback base URL for the local `opencode serve`. */
|
|
2198
3479
|
get opencodeBase() {
|
|
@@ -2220,6 +3501,47 @@ var ChannelDriver = class {
|
|
|
2220
3501
|
);
|
|
2221
3502
|
return run2;
|
|
2222
3503
|
}
|
|
3504
|
+
/**
|
|
3505
|
+
* Pull-and-apply any files Evident has queued for this runner (#559), riding
|
|
3506
|
+
* the EXISTING drain cycle — `run.ts` calls it from the same ~2s channel poll
|
|
3507
|
+
* and drain ping that call `drainPending()`. There is deliberately no channel,
|
|
3508
|
+
* control frame or poll loop of its own: worst-case latency is one poll tick.
|
|
3509
|
+
*
|
|
3510
|
+
* NEVER throws and never surfaces a `ChannelAuthError`: a file failure must not
|
|
3511
|
+
* cost a conversation turn. Failures are logged and either acked as a terminal
|
|
3512
|
+
* outcome or left pending for the next drain (see `runner-file-sync.ts`).
|
|
3513
|
+
*
|
|
3514
|
+
* Re-entrant calls are skipped (the poll tick and a drain ping can overlap).
|
|
3515
|
+
*
|
|
3516
|
+
* @returns the number of files written to disk.
|
|
3517
|
+
*/
|
|
3518
|
+
async syncPendingFiles() {
|
|
3519
|
+
if (this.stopped) return 0;
|
|
3520
|
+
if (this.syncingFiles) return 0;
|
|
3521
|
+
this.syncingFiles = true;
|
|
3522
|
+
try {
|
|
3523
|
+
const applied = await syncPendingRunnerFiles({
|
|
3524
|
+
agentId: this.agentId,
|
|
3525
|
+
apiUrl: this.apiUrl,
|
|
3526
|
+
getAuthHeader: this.getAuthHeader,
|
|
3527
|
+
fetchImpl: this.fetchImpl,
|
|
3528
|
+
allowedDirectories: this.fileSyncDirectories,
|
|
3529
|
+
homeDir: this.homeDir,
|
|
3530
|
+
ackFailures: this.fileAckFailures,
|
|
3531
|
+
log: this.log
|
|
3532
|
+
});
|
|
3533
|
+
this.appliedFileCount += applied;
|
|
3534
|
+
return applied;
|
|
3535
|
+
} catch (err) {
|
|
3536
|
+
this.log({
|
|
3537
|
+
level: "error",
|
|
3538
|
+
message: `Runner file sync failed unexpectedly (message processing is unaffected): ${err instanceof Error ? err.message : String(err)}`
|
|
3539
|
+
});
|
|
3540
|
+
return 0;
|
|
3541
|
+
} finally {
|
|
3542
|
+
this.syncingFiles = false;
|
|
3543
|
+
}
|
|
3544
|
+
}
|
|
2223
3545
|
async runDrain() {
|
|
2224
3546
|
let dispatched = 0;
|
|
2225
3547
|
try {
|
|
@@ -2253,6 +3575,28 @@ var ChannelDriver = class {
|
|
|
2253
3575
|
}
|
|
2254
3576
|
return false;
|
|
2255
3577
|
}
|
|
3578
|
+
/**
|
|
3579
|
+
* File-pull work, for `run.ts`'s idle accounting (#559).
|
|
3580
|
+
*
|
|
3581
|
+
* Pulling a file is real work that `drainPending()` knows nothing about, so
|
|
3582
|
+
* without this a near-idle runner counts a credential pull as an empty tick
|
|
3583
|
+
* and `--idle-timeout` can `process.exit` mid-pull — leaving a
|
|
3584
|
+
* `.evident-push-*.tmp` behind — or immediately after the write, before the
|
|
3585
|
+
* browser has run the authorize/callback that activates it (the user then sees
|
|
3586
|
+
* `saved_not_activated` for a runner that was fine).
|
|
3587
|
+
*
|
|
3588
|
+
* Two signals because one cannot cover both cases: `inFlight` is the pull
|
|
3589
|
+
* happening RIGHT NOW (it may outlive the tick that started it), and
|
|
3590
|
+
* `appliedFiles` is monotonic so a pull that started AND finished between two
|
|
3591
|
+
* idle checks still shows up as an advance.
|
|
3592
|
+
*
|
|
3593
|
+
* CALLER CONTRACT: sample `inFlight` BEFORE calling `syncPendingFiles()` for
|
|
3594
|
+
* the cycle. `syncPendingFiles` sets the flag synchronously, so a caller that
|
|
3595
|
+
* samples afterwards reads `true` every single cycle and can never idle out.
|
|
3596
|
+
*/
|
|
3597
|
+
fileSyncActivity() {
|
|
3598
|
+
return { appliedFiles: this.appliedFileCount, inFlight: this.syncingFiles };
|
|
3599
|
+
}
|
|
2256
3600
|
/**
|
|
2257
3601
|
* OpenCode session ids the session-cleanup sweep (issue #190) must NOT delete:
|
|
2258
3602
|
* exactly those with a live (dispatched-but-not-done / paused) turn, i.e. a
|
|
@@ -2314,7 +3658,7 @@ var ChannelDriver = class {
|
|
|
2314
3658
|
await this.sleep(step);
|
|
2315
3659
|
}
|
|
2316
3660
|
}
|
|
2317
|
-
while (this.hasInFlightWatchers()) {
|
|
3661
|
+
while (this.hasInFlightWatchers() || this.syncingFiles) {
|
|
2318
3662
|
if (this.now() >= deadline) return false;
|
|
2319
3663
|
await this.sleep(step);
|
|
2320
3664
|
}
|
|
@@ -2349,16 +3693,30 @@ var ChannelDriver = class {
|
|
|
2349
3693
|
* @returns the count of messages NEWLY dispatched (not already in-flight).
|
|
2350
3694
|
*/
|
|
2351
3695
|
async processConversation(conv) {
|
|
2352
|
-
const sessionId = await this.ensureSession(conv);
|
|
3696
|
+
const { sessionId, refusedSessionId, created: sessionCreated } = await this.ensureSession(conv);
|
|
2353
3697
|
const messages = await this.getPendingMessages(conv.id);
|
|
2354
3698
|
let dispatched = 0;
|
|
2355
3699
|
let skippedAlreadyDispatched = 0;
|
|
3700
|
+
if (refusedSessionId && messages.length > 0) {
|
|
3701
|
+
void this.postSignal(conv.id, messages[0].id, "session_superseded", {
|
|
3702
|
+
superseded_session_id: refusedSessionId
|
|
3703
|
+
});
|
|
3704
|
+
}
|
|
2356
3705
|
for (const message of messages) {
|
|
2357
3706
|
if (this.stopped) break;
|
|
2358
3707
|
if (this.dispatched.has(message.id)) {
|
|
2359
3708
|
skippedAlreadyDispatched += 1;
|
|
2360
3709
|
continue;
|
|
2361
3710
|
}
|
|
3711
|
+
if (message.opencode_message_id) {
|
|
3712
|
+
const outcome = await this.resolveRedrive(conv, sessionId, message, sessionCreated);
|
|
3713
|
+
if (outcome === "abandoned") {
|
|
3714
|
+
continue;
|
|
3715
|
+
}
|
|
3716
|
+
if (outcome !== "dispatch") {
|
|
3717
|
+
break;
|
|
3718
|
+
}
|
|
3719
|
+
}
|
|
2362
3720
|
const options = {
|
|
2363
3721
|
agent: message.opencode_agent ?? void 0,
|
|
2364
3722
|
model: message.opencode_model ?? void 0
|
|
@@ -2379,7 +3737,8 @@ var ChannelDriver = class {
|
|
|
2379
3737
|
} catch (err) {
|
|
2380
3738
|
if (err instanceof ChannelAuthError) throw err;
|
|
2381
3739
|
this.dispatched.delete(message.id);
|
|
2382
|
-
|
|
3740
|
+
const exists = await sessionExists(this.port, sessionId);
|
|
3741
|
+
if (exists === false) {
|
|
2383
3742
|
this.sessions.delete(conv.id);
|
|
2384
3743
|
this.log({
|
|
2385
3744
|
level: "warn",
|
|
@@ -2387,44 +3746,587 @@ var ChannelDriver = class {
|
|
|
2387
3746
|
conversation_id: conv.id,
|
|
2388
3747
|
message_id: message.id
|
|
2389
3748
|
});
|
|
3749
|
+
this.signalDispatchNotStarted(conv, message, "session_deleted_race");
|
|
3750
|
+
break;
|
|
3751
|
+
}
|
|
3752
|
+
if (exists === null) {
|
|
3753
|
+
this.log({
|
|
3754
|
+
level: "warn",
|
|
3755
|
+
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.`,
|
|
3756
|
+
conversation_id: conv.id,
|
|
3757
|
+
message_id: message.id
|
|
3758
|
+
});
|
|
3759
|
+
this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
|
|
2390
3760
|
break;
|
|
2391
3761
|
}
|
|
2392
|
-
|
|
3762
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
3763
|
+
this.sessions.delete(conv.id);
|
|
3764
|
+
this.supersede(conv.id, sessionId);
|
|
3765
|
+
this.log({
|
|
3766
|
+
level: "warn",
|
|
3767
|
+
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.`,
|
|
3768
|
+
conversation_id: conv.id,
|
|
3769
|
+
message_id: message.id
|
|
3770
|
+
});
|
|
3771
|
+
await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
|
|
3772
|
+
this.log({
|
|
3773
|
+
level: "warn",
|
|
3774
|
+
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)}`,
|
|
3775
|
+
conversation_id: conv.id,
|
|
3776
|
+
message_id: message.id
|
|
3777
|
+
});
|
|
3778
|
+
this.signalDispatchNotStarted(conv, message, "failure_unreported");
|
|
2393
3779
|
});
|
|
2394
3780
|
this.log({
|
|
2395
3781
|
level: "error",
|
|
2396
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
3782
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
|
|
2397
3783
|
conversation_id: conv.id,
|
|
2398
3784
|
message_id: message.id
|
|
2399
3785
|
});
|
|
2400
|
-
|
|
3786
|
+
break;
|
|
2401
3787
|
}
|
|
2402
3788
|
if (opencodeMessageId === null) {
|
|
3789
|
+
const streak = this.recordUnconfirmedDispatch(message.id, sessionId);
|
|
3790
|
+
if (streak < MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
|
|
3791
|
+
this.log({
|
|
3792
|
+
level: "warn",
|
|
3793
|
+
message: `Message ${message.id.slice(0, 8)} dispatched but its opencode id could not be read back (${streak}/${MAX_IDENTICAL_REDRIVE_POLL_FAILURES}) \u2014 leaving un-tracked to retry next tick`,
|
|
3794
|
+
conversation_id: conv.id,
|
|
3795
|
+
message_id: message.id
|
|
3796
|
+
});
|
|
3797
|
+
this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
|
|
3798
|
+
continue;
|
|
3799
|
+
}
|
|
3800
|
+
this.unconfirmedDispatchFailures.delete(message.id);
|
|
3801
|
+
this.sessions.delete(conv.id);
|
|
3802
|
+
this.supersede(conv.id, sessionId);
|
|
3803
|
+
const errorMessage = `OpenCode accepted this message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
|
|
2403
3804
|
this.log({
|
|
2404
|
-
level: "
|
|
2405
|
-
message:
|
|
3805
|
+
level: "error",
|
|
3806
|
+
message: errorMessage,
|
|
2406
3807
|
conversation_id: conv.id,
|
|
2407
3808
|
message_id: message.id
|
|
2408
3809
|
});
|
|
2409
|
-
|
|
3810
|
+
await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
|
|
3811
|
+
this.log({
|
|
3812
|
+
level: "warn",
|
|
3813
|
+
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)}`,
|
|
3814
|
+
conversation_id: conv.id,
|
|
3815
|
+
message_id: message.id
|
|
3816
|
+
});
|
|
3817
|
+
this.signalDispatchNotStarted(conv, message, "abandon_unreported");
|
|
3818
|
+
});
|
|
3819
|
+
break;
|
|
2410
3820
|
}
|
|
3821
|
+
this.unconfirmedDispatchFailures.delete(message.id);
|
|
3822
|
+
this.dispatchNotStartedSignalled.delete(message.id);
|
|
2411
3823
|
this.dispatched.add(message.id);
|
|
2412
3824
|
this.registerInFlight(conv, sessionId, message, opencodeMessageId);
|
|
2413
3825
|
dispatched += 1;
|
|
2414
3826
|
void this.postSignal(conv.id, message.id, "dispatched");
|
|
2415
3827
|
}
|
|
2416
|
-
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
3828
|
+
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
3829
|
+
this.log({
|
|
3830
|
+
level: "warn",
|
|
3831
|
+
message: `Conversation ${conv.id.slice(0, 8)} has ${messages.length} pending message(s) but ALL are already marked dispatched locally (in-flight set: ${this.dispatched.size}) \u2014 none sent to OpenCode this tick. If this repeats, a message may be stuck acknowledged-but-never-dispatched (its watcher never settled).`,
|
|
3832
|
+
conversation_id: conv.id
|
|
3833
|
+
});
|
|
3834
|
+
}
|
|
3835
|
+
this.ensureWatcherRunning(sessionId);
|
|
3836
|
+
return dispatched;
|
|
3837
|
+
}
|
|
3838
|
+
/**
|
|
3839
|
+
* Poll a session's message list for the re-drive fence (#965), via the
|
|
3840
|
+
* INJECTED `fetchImpl` — NOT the imported `getSessionMessages` helper, which
|
|
3841
|
+
* hits the global `fetch` and would bypass the same override every other
|
|
3842
|
+
* opencode poll in this file respects. Mirrors `readoptProcessing`'s own
|
|
3843
|
+
* snapshot fetch (`:3081-3111`).
|
|
3844
|
+
*
|
|
3845
|
+
* Returns `{ ok: true, messages }` on a readable snapshot, or
|
|
3846
|
+
* `{ ok: false, signature }` on failure — `signature` is a string that
|
|
3847
|
+
* repeats across attempts for the SAME underlying fault (used by the
|
|
3848
|
+
* consecutive-identical-failure bound, #1348), or `null` for a thrown
|
|
3849
|
+
* exception, which is NOT countable toward that bound (a network blip / an
|
|
3850
|
+
* opencode restart also throws identically every tick, and must keep
|
|
3851
|
+
* retrying unbounded rather than ever being treated as permanent).
|
|
3852
|
+
*/
|
|
3853
|
+
async pollSessionMessagesForRedrive(conv, message, sessionId) {
|
|
3854
|
+
try {
|
|
3855
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
|
|
3856
|
+
if (!res.ok) {
|
|
3857
|
+
const rawBody = await res.text();
|
|
3858
|
+
const normalized = normalizeRedrivePollFailureBody(rawBody);
|
|
3859
|
+
this.log({
|
|
3860
|
+
level: "warn",
|
|
3861
|
+
message: `Re-drive: polling session ${sessionId.slice(0, 8)} for message ${message.id.slice(0, 8)} returned HTTP ${res.status}${normalized ? `: ${normalized}` : ""} \u2014 treating as unreadable this tick`,
|
|
3862
|
+
conversation_id: conv.id,
|
|
3863
|
+
message_id: message.id
|
|
3864
|
+
});
|
|
3865
|
+
return { ok: false, signature: `HTTP ${res.status}${normalized ? `: ${normalized}` : ""}` };
|
|
3866
|
+
}
|
|
3867
|
+
const body = await res.json();
|
|
3868
|
+
if (!Array.isArray(body)) {
|
|
3869
|
+
this.log({
|
|
3870
|
+
level: "warn",
|
|
3871
|
+
message: `Re-drive: polling session ${sessionId.slice(0, 8)} for message ${message.id.slice(0, 8)} returned a non-array message body \u2014 treating as unreadable this tick`,
|
|
3872
|
+
conversation_id: conv.id,
|
|
3873
|
+
message_id: message.id
|
|
3874
|
+
});
|
|
3875
|
+
return { ok: false, signature: "non-array message body" };
|
|
3876
|
+
}
|
|
3877
|
+
return { ok: true, messages: body };
|
|
3878
|
+
} catch (err) {
|
|
3879
|
+
this.log({
|
|
3880
|
+
level: "warn",
|
|
3881
|
+
message: `Re-drive: failed to poll session ${sessionId.slice(0, 8)} for message ${message.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
3882
|
+
conversation_id: conv.id,
|
|
3883
|
+
message_id: message.id
|
|
3884
|
+
});
|
|
3885
|
+
return { ok: false, signature: null };
|
|
3886
|
+
}
|
|
3887
|
+
}
|
|
3888
|
+
/**
|
|
3889
|
+
* The re-drive fence for a `pending` row that already carries a stored
|
|
3890
|
+
* `opencode_message_id` (#965) — i.e. it has already been handed to opencode at
|
|
3891
|
+
* least once (see the invariant at `QueuedMessage.opencode_message_id`'s doc).
|
|
3892
|
+
* The lifecycle cron can falsely reclaim a `processing` row back to `pending`
|
|
3893
|
+
* mid-turn (a 5-minute liveness-staleness check racing a still-running turn);
|
|
3894
|
+
* without this fence the drain loop would re-`prompt_async` the SAME turn a
|
|
3895
|
+
* second time against live GitHub state. Mirrors `readoptOne`'s job for the
|
|
3896
|
+
* `processing` re-adopt path, but simpler: no b1/b2 preamble cross-check is
|
|
3897
|
+
* needed here because `sessionCreated` already handles the cases (a #553
|
|
3898
|
+
* abandoned session, a #190 vanished one) that path exists for.
|
|
3899
|
+
*
|
|
3900
|
+
* Only `ChannelAuthError` propagates. A poll that fails identically
|
|
3901
|
+
* `MAX_IDENTICAL_REDRIVE_POLL_FAILURES` times in a row reports the message
|
|
3902
|
+
* failed instead of retrying it (#1348) — SEPARATE from, not a replacement
|
|
3903
|
+
* for, `resolveRedriveUnresolved`'s own `pausedMaxWaitMs` bound below. Every
|
|
3904
|
+
* other failure resolves to `unresolved` and is retried whole on the next
|
|
3905
|
+
* ~2s drain tick.
|
|
3906
|
+
*/
|
|
3907
|
+
async resolveRedrive(conv, sessionId, message, sessionCreated) {
|
|
3908
|
+
const ocId = message.opencode_message_id ?? null;
|
|
3909
|
+
if (sessionCreated) {
|
|
3910
|
+
this.clearRedriveUnresolved(message.id);
|
|
3911
|
+
void this.postSignal(conv.id, message.id, "redrive_redispatched");
|
|
3912
|
+
return "dispatch";
|
|
3913
|
+
}
|
|
3914
|
+
const polled = await this.pollSessionMessagesForRedrive(conv, message, sessionId);
|
|
3915
|
+
if (!polled.ok) {
|
|
3916
|
+
const streak = this.recordRedrivePollFailure(message.id, sessionId, polled.signature);
|
|
3917
|
+
if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES && polled.signature !== null) {
|
|
3918
|
+
return this.failRedrivePollPermanent(conv, sessionId, message, polled.signature, streak);
|
|
3919
|
+
}
|
|
3920
|
+
return this.resolveRedriveUnresolved(conv, message);
|
|
3921
|
+
}
|
|
3922
|
+
this.redrivePollFailures.delete(message.id);
|
|
3923
|
+
const messages = polled.messages;
|
|
3924
|
+
if (messages.length === 0) {
|
|
3925
|
+
return this.resolveRedriveUnresolved(conv, message);
|
|
3926
|
+
}
|
|
3927
|
+
const state = messageRunState(messages, ocId ?? "");
|
|
3928
|
+
if (state === "failed" && isAbortedTerminalReply(messages, ocId ?? "")) {
|
|
3929
|
+
const ongoing = await isSessionOngoing(this.port, sessionId);
|
|
3930
|
+
if (ongoing === false) {
|
|
3931
|
+
this.log({
|
|
3932
|
+
level: "info",
|
|
3933
|
+
message: `Re-drive: message ${message.id.slice(0, 8)} carries an abort-shaped terminal error and session ${sessionId.slice(0, 8)} is not-ongoing per GET /session/status \u2014 restart orphan, re-dispatching instead of marking it permanently failed`,
|
|
3934
|
+
conversation_id: conv.id,
|
|
3935
|
+
message_id: message.id
|
|
3936
|
+
});
|
|
3937
|
+
this.clearRedriveUnresolved(message.id);
|
|
3938
|
+
void this.postSignal(conv.id, message.id, "redrive_redispatched");
|
|
3939
|
+
return "dispatch";
|
|
3940
|
+
}
|
|
3941
|
+
}
|
|
3942
|
+
if (state === "done" || state === "failed") {
|
|
3943
|
+
return this.settleRedrive(conv, sessionId, message, ocId, messages, state);
|
|
3944
|
+
}
|
|
3945
|
+
if (state === "running" || state === "queued") {
|
|
3946
|
+
const ongoing = await isSessionOngoing(this.port, sessionId);
|
|
3947
|
+
if (ongoing === true) {
|
|
3948
|
+
return this.reattachRedrive(conv, sessionId, message, ocId);
|
|
3949
|
+
}
|
|
3950
|
+
if (ongoing === false) {
|
|
3951
|
+
this.clearRedriveUnresolved(message.id);
|
|
3952
|
+
void this.postSignal(conv.id, message.id, "redrive_redispatched");
|
|
3953
|
+
return "dispatch";
|
|
3954
|
+
}
|
|
3955
|
+
return this.resolveRedriveUnresolved(conv, message);
|
|
3956
|
+
}
|
|
3957
|
+
this.clearRedriveUnresolved(message.id);
|
|
3958
|
+
void this.postSignal(conv.id, message.id, "redrive_redispatched");
|
|
3959
|
+
return "dispatch";
|
|
3960
|
+
}
|
|
3961
|
+
/**
|
|
3962
|
+
* The `reattached` outcome (Task 3.3): the prior turn is STILL ONGOING per
|
|
3963
|
+
* opencode's own status map — undo the false reclaim instead of starting a
|
|
3964
|
+
* second turn.
|
|
3965
|
+
*/
|
|
3966
|
+
async reattachRedrive(conv, sessionId, message, ocId) {
|
|
3967
|
+
let anchorMs;
|
|
3968
|
+
const parsed = message.processing_started_at ? Date.parse(message.processing_started_at) : NaN;
|
|
3969
|
+
if (!Number.isNaN(parsed)) {
|
|
3970
|
+
anchorMs = parsed;
|
|
3971
|
+
} else {
|
|
3972
|
+
anchorMs = this.now();
|
|
3973
|
+
this.log({
|
|
3974
|
+
level: "error",
|
|
3975
|
+
message: `Re-drive: message ${message.id.slice(0, 8)} has null/unparseable processing_started_at (${String(message.processing_started_at)}) \u2014 anchoring the watcher's absolute-age ceiling to now (defensive)`,
|
|
3976
|
+
conversation_id: conv.id,
|
|
3977
|
+
message_id: message.id
|
|
3978
|
+
});
|
|
3979
|
+
}
|
|
3980
|
+
const title = await this.resolveSessionTitle(sessionId, conv.id);
|
|
3981
|
+
try {
|
|
3982
|
+
await this.markProcessing(conv.id, message.id, sessionId, ocId, title);
|
|
3983
|
+
} catch (err) {
|
|
3984
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
3985
|
+
if (err instanceof ChannelTerminalError) {
|
|
3986
|
+
this.log({
|
|
3987
|
+
level: "error",
|
|
3988
|
+
message: `Re-drive: the server definitively refused to restore message ${message.id.slice(0, 8)} to processing (terminal HTTP ${err.status} \u2014 the row is gone or the update was rejected); NOT reporting a re-attach`,
|
|
3989
|
+
conversation_id: conv.id,
|
|
3990
|
+
message_id: message.id
|
|
3991
|
+
});
|
|
3992
|
+
} else {
|
|
3993
|
+
this.log({
|
|
3994
|
+
level: "warn",
|
|
3995
|
+
message: `Re-drive: failed to restore message ${message.id.slice(0, 8)} to processing (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
3996
|
+
conversation_id: conv.id,
|
|
3997
|
+
message_id: message.id
|
|
3998
|
+
});
|
|
3999
|
+
}
|
|
4000
|
+
const bound = await this.boundRedriveOutcome(conv, message, "reattach");
|
|
4001
|
+
return bound === "abandoned" ? "abandoned" : "unresolved";
|
|
4002
|
+
}
|
|
4003
|
+
this.clearRedriveUnresolved(message.id);
|
|
4004
|
+
this.registerReadopted(conv, sessionId, message, ocId ?? "", anchorMs);
|
|
4005
|
+
this.dispatched.add(message.id);
|
|
4006
|
+
this.readopted.add(message.id);
|
|
4007
|
+
this.ensureWatcherRunning(sessionId);
|
|
4008
|
+
const watchedForMs = this.now() - anchorMs;
|
|
4009
|
+
void this.postSignal(conv.id, message.id, "redrive_reattached", {
|
|
4010
|
+
watched_for_ms: watchedForMs
|
|
4011
|
+
});
|
|
4012
|
+
this.log({
|
|
4013
|
+
level: "warn",
|
|
4014
|
+
message: `Re-drive: message ${message.id.slice(0, 8)} (session ${sessionId.slice(0, 8)}) was wrongly reclaimed to pending while its turn was still running (watched ${watchedForMs}ms) \u2014 restored to processing instead of re-dispatching`,
|
|
4015
|
+
conversation_id: conv.id,
|
|
4016
|
+
message_id: message.id
|
|
4017
|
+
});
|
|
4018
|
+
return "reattached";
|
|
4019
|
+
}
|
|
4020
|
+
/**
|
|
4021
|
+
* The `settled` outcome (Task 3.2): the prior turn already finished (or
|
|
4022
|
+
* errored) while nobody was watching — deliver/report it instead of re-running.
|
|
4023
|
+
* Mirrors `readoptOne`'s `done`/`failed` branches' error discipline, simplified
|
|
4024
|
+
* (no `doneUndeliverable` park: a terminal PATCH failure here just retries next
|
|
4025
|
+
* drain, same as any other non-auth failure). The restart-abort carve-out that
|
|
4026
|
+
* keeps the two in step for `failed` lives in the caller (`resolveRedrive`, #1310),
|
|
4027
|
+
* so a row reaching this `failed` branch is a GENUINE failure.
|
|
4028
|
+
*/
|
|
4029
|
+
async settleRedrive(conv, sessionId, message, ocId, messages, state) {
|
|
4030
|
+
try {
|
|
4031
|
+
if (state === "done") {
|
|
4032
|
+
const title = await this.resolveSessionTitle(sessionId, conv.id);
|
|
4033
|
+
const usage = messageUsage(messages, ocId ?? "");
|
|
4034
|
+
this.log({
|
|
4035
|
+
level: "info",
|
|
4036
|
+
message: `Re-drive: message ${message.id.slice(0, 8)} completed while its row was wrongly reclaimed to pending \u2014 marking done instead of re-dispatching`,
|
|
4037
|
+
conversation_id: conv.id,
|
|
4038
|
+
message_id: message.id
|
|
4039
|
+
});
|
|
4040
|
+
await this.markDone(conv.id, message.id, sessionId, ocId, title, usage);
|
|
4041
|
+
} else {
|
|
4042
|
+
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
4043
|
+
const usage = messageUsage(messages, ocId ?? "");
|
|
4044
|
+
const failure = await this.classifyModelAuthFailure(messages, ocId ?? "");
|
|
4045
|
+
this.log({
|
|
4046
|
+
level: "error",
|
|
4047
|
+
message: `Re-drive: message ${message.id.slice(0, 8)} errored while its row was wrongly reclaimed to pending \u2014 marking failed instead of re-dispatching: ${error2 ?? "(no error text)"}`,
|
|
4048
|
+
conversation_id: conv.id,
|
|
4049
|
+
message_id: message.id
|
|
4050
|
+
});
|
|
4051
|
+
await this.markFailed(conv.id, message.id, sessionId, error2, usage, failure);
|
|
4052
|
+
}
|
|
4053
|
+
} catch (err) {
|
|
4054
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
4055
|
+
this.log({
|
|
4056
|
+
level: "warn",
|
|
4057
|
+
message: `Re-drive: failed to report message ${message.id.slice(0, 8)} ${state} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
4058
|
+
conversation_id: conv.id,
|
|
4059
|
+
message_id: message.id
|
|
4060
|
+
});
|
|
4061
|
+
const bound = await this.boundRedriveOutcome(conv, message, "settle");
|
|
4062
|
+
return bound === "abandoned" ? "abandoned" : "unresolved";
|
|
4063
|
+
}
|
|
4064
|
+
this.clearRedriveUnresolved(message.id);
|
|
4065
|
+
void this.postSignal(conv.id, message.id, "redrive_settled");
|
|
4066
|
+
return "settled";
|
|
4067
|
+
}
|
|
4068
|
+
/**
|
|
4069
|
+
* The permanent-failure outcome (#1348): the fence's own poll of this session
|
|
4070
|
+
* failed with the SAME opencode-answered signature
|
|
4071
|
+
* `MAX_IDENTICAL_REDRIVE_POLL_FAILURES` times in a row — a transient blip
|
|
4072
|
+
* would have varied or eventually cleared (see `pollSessionMessagesForRedrive`
|
|
4073
|
+
* and `recordRedrivePollFailure`), so this is a durable fault (e.g. #1345's
|
|
4074
|
+
* corrupted opencode session) rather than something worth retrying forever.
|
|
4075
|
+
* Mirrors `settleRedrive`'s error discipline: no `usage`/`failure` args to
|
|
4076
|
+
* `markFailed` (no opencode snapshot to extract them from — this poll never
|
|
4077
|
+
* got a readable one).
|
|
4078
|
+
*/
|
|
4079
|
+
async failRedrivePollPermanent(conv, sessionId, message, signature, streak) {
|
|
4080
|
+
this.log({
|
|
4081
|
+
level: "error",
|
|
4082
|
+
message: `Re-drive: message ${message.id.slice(0, 8)} (session ${sessionId.slice(0, 8)}) failed to poll with the identical signature "${signature}" ${streak} times in a row \u2014 reporting the message failed instead of retrying forever`,
|
|
4083
|
+
conversation_id: conv.id,
|
|
4084
|
+
message_id: message.id
|
|
4085
|
+
});
|
|
4086
|
+
try {
|
|
4087
|
+
await this.markFailed(
|
|
4088
|
+
conv.id,
|
|
4089
|
+
message.id,
|
|
4090
|
+
sessionId,
|
|
4091
|
+
`The runner could not read this conversation's state from OpenCode (${signature}). The same failure repeated ${streak} times in a row, so the message was not retried further.`
|
|
4092
|
+
);
|
|
4093
|
+
} catch (err) {
|
|
4094
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
4095
|
+
this.log({
|
|
4096
|
+
level: "warn",
|
|
4097
|
+
message: `Re-drive: failed to report message ${message.id.slice(0, 8)} permanently failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
4098
|
+
conversation_id: conv.id,
|
|
4099
|
+
message_id: message.id
|
|
4100
|
+
});
|
|
4101
|
+
const bound = await this.boundRedriveOutcome(conv, message, "fail_permanent");
|
|
4102
|
+
return bound === "abandoned" ? "abandoned" : "unresolved";
|
|
4103
|
+
}
|
|
4104
|
+
this.clearRedriveUnresolved(message.id);
|
|
4105
|
+
void this.postSignal(conv.id, message.id, "redrive_poll_failed");
|
|
4106
|
+
return "settled";
|
|
4107
|
+
}
|
|
4108
|
+
/**
|
|
4109
|
+
* The bounded `unresolved` outcome (Task 3.4): opencode's state could not be
|
|
4110
|
+
* observed (snapshot unreadable/empty, or `isSessionOngoing` returned `null`).
|
|
4111
|
+
* A `pending` row is swept by the server's own `PENDING_MAX_AGE_MS` (24h,
|
|
4112
|
+
* #1368) cron arm, but that is a day-scale backstop — this local bound acts
|
|
4113
|
+
* in minutes so the row (and the conversation it starves, per the ordering
|
|
4114
|
+
* invariant below) isn't left stranded for that long. Bound to the existing
|
|
4115
|
+
* `pausedMaxWaitMs` window (reusing the knob, not a new constant); takes
|
|
4116
|
+
* `dispatch` once elapsed.
|
|
4117
|
+
*/
|
|
4118
|
+
resolveRedriveUnresolved(conv, message) {
|
|
4119
|
+
const now = this.now();
|
|
4120
|
+
const since = this.redriveUnresolvedSince.get(message.id);
|
|
4121
|
+
if (since !== void 0 && now - since >= this.pausedMaxWaitMs) {
|
|
4122
|
+
this.clearRedriveUnresolved(message.id);
|
|
4123
|
+
void this.postSignal(conv.id, message.id, "redrive_redispatched");
|
|
4124
|
+
return "dispatch";
|
|
4125
|
+
}
|
|
4126
|
+
if (since === void 0) {
|
|
4127
|
+
this.redriveUnresolvedSince.set(message.id, now);
|
|
4128
|
+
}
|
|
4129
|
+
if (!this.redriveUnresolvedSignalled.has(message.id)) {
|
|
4130
|
+
this.redriveUnresolvedSignalled.add(message.id);
|
|
4131
|
+
void this.postSignal(conv.id, message.id, "redrive_unresolved");
|
|
4132
|
+
}
|
|
4133
|
+
return "unresolved";
|
|
4134
|
+
}
|
|
4135
|
+
/** Clear all `unresolved`/failure-streak trackers for a row (any non-`unresolved` outcome). */
|
|
4136
|
+
clearRedriveUnresolved(messageId) {
|
|
4137
|
+
this.redriveUnresolvedSince.delete(messageId);
|
|
4138
|
+
this.redriveUnresolvedSignalled.delete(messageId);
|
|
4139
|
+
this.redrivePollFailures.delete(messageId);
|
|
4140
|
+
this.redriveOutcomeUnreportedSignalled.delete(messageId);
|
|
4141
|
+
this.redriveOutcomeFailingSince.delete(messageId);
|
|
4142
|
+
this.redriveOutcomeAbandonedSignalled.delete(messageId);
|
|
4143
|
+
}
|
|
4144
|
+
/**
|
|
4145
|
+
* #1340: the dispatch loop reached a message and did NOT start a turn. Fires at
|
|
4146
|
+
* most once per (message, branch) streak — a wedged row is re-tried every tick,
|
|
4147
|
+
* and the per-tick count is already carried by the co-occurring
|
|
4148
|
+
* `redrive_unresolved`/`redrive_redispatched` signals.
|
|
4149
|
+
*/
|
|
4150
|
+
signalDispatchNotStarted(conv, message, branch) {
|
|
4151
|
+
if (this.dispatchNotStartedSignalled.get(message.id) === branch) return;
|
|
4152
|
+
this.dispatchNotStartedSignalled.set(message.id, branch);
|
|
4153
|
+
void this.postSignal(conv.id, message.id, "dispatch_not_started", { branch });
|
|
4154
|
+
}
|
|
4155
|
+
/**
|
|
4156
|
+
* Class B (#1340): the runner DECIDED an outcome (reattach/settle/fail_permanent)
|
|
4157
|
+
* but its own PATCH to record it failed. Fires at most once per (message,
|
|
4158
|
+
* outcome) streak, and only while `boundRedriveOutcome` has not yet tripped —
|
|
4159
|
+
* once it trips, `redrive_outcome_abandoned` takes over reporting for the row
|
|
4160
|
+
* (#1366).
|
|
4161
|
+
*/
|
|
4162
|
+
signalRedriveOutcomeUnreported(conv, message, outcome) {
|
|
4163
|
+
if (this.redriveOutcomeUnreportedSignalled.get(message.id) === outcome) return;
|
|
4164
|
+
this.redriveOutcomeUnreportedSignalled.set(message.id, outcome);
|
|
4165
|
+
void this.postSignal(conv.id, message.id, "redrive_outcome_unreported", {
|
|
4166
|
+
attempted_outcome: outcome
|
|
4167
|
+
});
|
|
4168
|
+
}
|
|
4169
|
+
/**
|
|
4170
|
+
* The runner-authored, honest error text for the terminal fallback a tripped
|
|
4171
|
+
* `boundRedriveOutcome` sends. Distinguishable per outcome and truthful about
|
|
4172
|
+
* what actually happened — the `settle`/done case must say the turn finished
|
|
4173
|
+
* but its result could not be recorded, never that the runner stopped
|
|
4174
|
+
* responding (that would be a lie for this shape, see #1366's "why this ships").
|
|
4175
|
+
*/
|
|
4176
|
+
static REDRIVE_ABANDON_ERROR = {
|
|
4177
|
+
reattach: "your runner could not record that this message had started, so it was given up on",
|
|
4178
|
+
settle: "your runner finished this message but could not record the result, so the reply could not be delivered",
|
|
4179
|
+
fail_permanent: "the runner could not read this conversation's state from OpenCode, and could not record that failure either, so the message was given up on"
|
|
4180
|
+
};
|
|
4181
|
+
/**
|
|
4182
|
+
* Bound for Class B (#1340, #1366): the runner DECIDED an outcome but its own
|
|
4183
|
+
* PATCH to record it failed. Two independent trip arms (either sufficient):
|
|
4184
|
+
* (1) this failure streak has lasted `pausedMaxWaitMs` — DURATION, not a tick
|
|
4185
|
+
* count, reusing the knob `resolveRedriveUnresolved` already established; (2)
|
|
4186
|
+
* the turn's `processing_started_at` age has crossed
|
|
4187
|
+
* `ABSOLUTE_MAX_PROCESSING_MS` — durable and restart-surviving, since arm (1)'s
|
|
4188
|
+
* in-memory streak resets on a scale-to-zero restart.
|
|
4189
|
+
*
|
|
4190
|
+
* INVARIANT — a tripped bound never suppresses the original outcome attempt;
|
|
4191
|
+
* it only adds a fallback after that attempt has failed again. This is only
|
|
4192
|
+
* ever reached from inside the catch of the ORIGINAL outcome PATCH, which is
|
|
4193
|
+
* attempted first on every tick whether or not this bound tripped before —
|
|
4194
|
+
* there is no give-up latch that would short-circuit it. That is what lets a
|
|
4195
|
+
* route-level fault that heals later still deliver the turn's real
|
|
4196
|
+
* `done`/`failed` payload: once the original PATCH succeeds again, this
|
|
4197
|
+
* helper is never entered and the row settles with its real result.
|
|
4198
|
+
*/
|
|
4199
|
+
async boundRedriveOutcome(conv, message, outcome) {
|
|
4200
|
+
const now = this.now();
|
|
4201
|
+
const since = this.redriveOutcomeFailingSince.get(message.id);
|
|
4202
|
+
if (since === void 0) this.redriveOutcomeFailingSince.set(message.id, now);
|
|
4203
|
+
const durationTripped = now - (since ?? now) >= this.pausedMaxWaitMs;
|
|
4204
|
+
const parsed = message.processing_started_at ? Date.parse(message.processing_started_at) : NaN;
|
|
4205
|
+
const absoluteAgeTripped = !Number.isNaN(parsed) && now - parsed >= ABSOLUTE_MAX_PROCESSING_MS;
|
|
4206
|
+
if (!durationTripped && !absoluteAgeTripped) {
|
|
4207
|
+
this.signalRedriveOutcomeUnreported(conv, message, outcome);
|
|
4208
|
+
return "retry";
|
|
4209
|
+
}
|
|
4210
|
+
const arm = durationTripped ? "failure_window" : "absolute_age";
|
|
4211
|
+
try {
|
|
4212
|
+
await this.markFailed(
|
|
4213
|
+
conv.id,
|
|
4214
|
+
message.id,
|
|
4215
|
+
void 0,
|
|
4216
|
+
_ChannelDriver.REDRIVE_ABANDON_ERROR[outcome]
|
|
4217
|
+
);
|
|
4218
|
+
} catch (err) {
|
|
4219
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
4220
|
+
this.log({
|
|
4221
|
+
level: "warn",
|
|
4222
|
+
message: `Re-drive bound: fallback markFailed for message ${message.id.slice(0, 8)} also failed (arm ${arm}, will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
4223
|
+
conversation_id: conv.id,
|
|
4224
|
+
message_id: message.id
|
|
4225
|
+
});
|
|
4226
|
+
if (!this.redriveOutcomeAbandonedSignalled.has(message.id)) {
|
|
4227
|
+
this.redriveOutcomeAbandonedSignalled.add(message.id);
|
|
4228
|
+
void this.postSignal(conv.id, message.id, "redrive_outcome_abandoned", {
|
|
4229
|
+
attempted_outcome: outcome,
|
|
4230
|
+
reported: false,
|
|
4231
|
+
arm
|
|
4232
|
+
});
|
|
4233
|
+
}
|
|
4234
|
+
return "retry";
|
|
4235
|
+
}
|
|
4236
|
+
this.clearRedriveUnresolved(message.id);
|
|
4237
|
+
void this.postSignal(conv.id, message.id, "redrive_outcome_abandoned", {
|
|
4238
|
+
attempted_outcome: outcome,
|
|
4239
|
+
reported: true,
|
|
4240
|
+
arm
|
|
4241
|
+
});
|
|
4242
|
+
return "abandoned";
|
|
4243
|
+
}
|
|
4244
|
+
/**
|
|
4245
|
+
* Record one poll outcome toward the re-drive fence's consecutive-identical-
|
|
4246
|
+
* failure streak (#1348) and return the resulting count. `signature === null`
|
|
4247
|
+
* (a thrown exception, H1) always clears the streak and returns `0` — it is
|
|
4248
|
+
* never countable. Otherwise the streak continues only when BOTH the session
|
|
4249
|
+
* and the signature match the previous failure; anything else (a different
|
|
4250
|
+
* session, or the same session failing a DIFFERENT way) starts a fresh streak
|
|
4251
|
+
* at `1`.
|
|
4252
|
+
*/
|
|
4253
|
+
recordRedrivePollFailure(messageId, sessionId, signature) {
|
|
4254
|
+
if (signature === null) {
|
|
4255
|
+
this.redrivePollFailures.delete(messageId);
|
|
4256
|
+
return 0;
|
|
4257
|
+
}
|
|
4258
|
+
const existing = this.redrivePollFailures.get(messageId);
|
|
4259
|
+
if (existing && existing.sessionId === sessionId && existing.signature === signature) {
|
|
4260
|
+
existing.count += 1;
|
|
4261
|
+
return existing.count;
|
|
4262
|
+
}
|
|
4263
|
+
this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
|
|
4264
|
+
return 1;
|
|
4265
|
+
}
|
|
4266
|
+
/**
|
|
4267
|
+
* Record one UNCONFIRMED-dispatch outcome (a `pending` row with no stored
|
|
4268
|
+
* `opencode_message_id` whose `sendPromptAsync` returned `null`) toward the
|
|
4269
|
+
* bound in `processConversation`'s dispatch loop, and return the resulting
|
|
4270
|
+
* count. Mirrors `recordRedrivePollFailure`'s session-scoping: a session
|
|
4271
|
+
* change starts a fresh streak at `1` rather than inheriting the old one's
|
|
4272
|
+
* count, since a new session is a genuinely different attempt.
|
|
4273
|
+
*/
|
|
4274
|
+
recordUnconfirmedDispatch(messageId, sessionId) {
|
|
4275
|
+
const existing = this.unconfirmedDispatchFailures.get(messageId);
|
|
4276
|
+
if (existing && existing.sessionId === sessionId) {
|
|
4277
|
+
existing.count += 1;
|
|
4278
|
+
return existing.count;
|
|
4279
|
+
}
|
|
4280
|
+
this.unconfirmedDispatchFailures.set(messageId, { sessionId, count: 1 });
|
|
4281
|
+
return 1;
|
|
4282
|
+
}
|
|
4283
|
+
/**
|
|
4284
|
+
* Record that `sessionId` is no longer a valid binding for `conversationId`
|
|
4285
|
+
* (#553). Keyed by conversation and hard-capped, so it cannot grow with the
|
|
4286
|
+
* number of failures — see the `supersededSessions` field doc.
|
|
4287
|
+
*/
|
|
4288
|
+
supersede(conversationId, sessionId) {
|
|
4289
|
+
this.supersededSessions.delete(conversationId);
|
|
4290
|
+
this.supersededSessions.set(conversationId, sessionId);
|
|
4291
|
+
while (this.supersededSessions.size > MAX_SUPERSEDED_CONVERSATIONS) {
|
|
4292
|
+
const oldest = this.supersededSessions.keys().next().value;
|
|
4293
|
+
if (oldest === void 0) return;
|
|
4294
|
+
this.supersededSessions.delete(oldest);
|
|
4295
|
+
}
|
|
4296
|
+
}
|
|
4297
|
+
/** Whether `sessionId` is the session this conversation has abandoned (#553). */
|
|
4298
|
+
isSuperseded(conversationId, sessionId) {
|
|
4299
|
+
return this.supersededSessions.get(conversationId) === sessionId;
|
|
4300
|
+
}
|
|
4301
|
+
/**
|
|
4302
|
+
* Resolve the opencode session to run this conversation's turns in.
|
|
4303
|
+
*
|
|
4304
|
+
* `refusedSessionId` is set when the #553 guard fired — i.e. the persisted
|
|
4305
|
+
* binding was an id this runner had abandoned, so a resurrection genuinely
|
|
4306
|
+
* happened and a fresh session was bound instead. The caller reports it.
|
|
4307
|
+
*
|
|
4308
|
+
* `created` says the returned session was made JUST NOW, so it provably holds
|
|
4309
|
+
* no prior turn. The re-drive fence needs that as CONTRARY evidence ("nothing
|
|
4310
|
+
* to reconcile against") — distinct from the ambiguous "I polled and saw an
|
|
4311
|
+
* empty transcript", which stays a deferral. Keep it separate from
|
|
4312
|
+
* `refusedSessionId`: only the latter means a #553 resurrection happened, and
|
|
4313
|
+
* only it may drive the `session_superseded` signal.
|
|
4314
|
+
*/
|
|
4315
|
+
async ensureSession(conv) {
|
|
4316
|
+
const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
|
|
4317
|
+
if (bound && this.isSuperseded(conv.id, bound)) {
|
|
2417
4318
|
this.log({
|
|
2418
4319
|
level: "warn",
|
|
2419
|
-
message: `
|
|
4320
|
+
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.`,
|
|
2420
4321
|
conversation_id: conv.id
|
|
2421
4322
|
});
|
|
4323
|
+
this.sessions.delete(conv.id);
|
|
4324
|
+
return {
|
|
4325
|
+
sessionId: await this.createAndBindSession(conv.id),
|
|
4326
|
+
refusedSessionId: bound,
|
|
4327
|
+
created: true
|
|
4328
|
+
};
|
|
2422
4329
|
}
|
|
2423
|
-
this.ensureWatcherRunning(sessionId);
|
|
2424
|
-
return dispatched;
|
|
2425
|
-
}
|
|
2426
|
-
async ensureSession(conv) {
|
|
2427
|
-
const bound = this.sessions.get(conv.id) ?? conv.opencode_session_id ?? null;
|
|
2428
4330
|
if (bound) {
|
|
2429
4331
|
const exists = await sessionExists(this.port, bound);
|
|
2430
4332
|
if (exists === false) {
|
|
@@ -2434,12 +4336,12 @@ var ChannelDriver = class {
|
|
|
2434
4336
|
conversation_id: conv.id
|
|
2435
4337
|
});
|
|
2436
4338
|
this.sessions.delete(conv.id);
|
|
2437
|
-
return this.createAndBindSession(conv.id);
|
|
4339
|
+
return { sessionId: await this.createAndBindSession(conv.id), created: true };
|
|
2438
4340
|
}
|
|
2439
4341
|
this.sessions.set(conv.id, bound);
|
|
2440
|
-
return bound;
|
|
4342
|
+
return { sessionId: bound, created: false };
|
|
2441
4343
|
}
|
|
2442
|
-
return this.createAndBindSession(conv.id);
|
|
4344
|
+
return { sessionId: await this.createAndBindSession(conv.id), created: true };
|
|
2443
4345
|
}
|
|
2444
4346
|
/**
|
|
2445
4347
|
* Create a fresh OpenCode session for a conversation, cache the binding, and
|
|
@@ -2450,7 +4352,12 @@ var ChannelDriver = class {
|
|
|
2450
4352
|
const directory = await this.resolveOpenCodeDirectory();
|
|
2451
4353
|
const sessionId = await createOpenCodeSession(this.port, directory);
|
|
2452
4354
|
this.sessions.set(conversationId, sessionId);
|
|
2453
|
-
await this.persistSession(conversationId, sessionId).catch(() => {
|
|
4355
|
+
await this.persistSession(conversationId, sessionId).catch((err) => {
|
|
4356
|
+
this.log({
|
|
4357
|
+
level: "warn",
|
|
4358
|
+
message: `Persisting the OpenCode session binding ${sessionId.slice(0, 8)} for conversation ${conversationId.slice(0, 8)} failed (best-effort, not retried) \u2014 the completion PATCH also carries opencode_session_id, so the binding is repaired when the turn finishes: ${err instanceof Error ? err.message : String(err)}`,
|
|
4359
|
+
conversation_id: conversationId
|
|
4360
|
+
});
|
|
2454
4361
|
});
|
|
2455
4362
|
return sessionId;
|
|
2456
4363
|
}
|
|
@@ -2527,7 +4434,11 @@ var ChannelDriver = class {
|
|
|
2527
4434
|
* (not-owned / out-of-range / deleted-at-source / workspace gone) / 413
|
|
2528
4435
|
* (over-cap). On ANY non-2xx or thrown failure we return `null` so the caller
|
|
2529
4436
|
* OMITS that one image and the text turn still sends — NEVER throws the turn.
|
|
2530
|
-
*
|
|
4437
|
+
* A 404 body carrying `{ reason: 'needs_reauth' }` (#547 — the server CONFIRMED
|
|
4438
|
+
* a Slack `files:read` scope problem via `files.info`) instead resolves the
|
|
4439
|
+
* `AttachmentFetchNeedsReauth` sentinel, so the in-thread note can steer the
|
|
4440
|
+
* user to reconnect Slack instead of a generic "unavailable". Failures are
|
|
4441
|
+
* logged with context (no silent swallow).
|
|
2531
4442
|
*/
|
|
2532
4443
|
async fetchAttachmentDataUrl(messageId, index, mime) {
|
|
2533
4444
|
try {
|
|
@@ -2536,6 +4447,25 @@ var ChannelDriver = class {
|
|
|
2536
4447
|
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2537
4448
|
);
|
|
2538
4449
|
if (!res.ok) {
|
|
4450
|
+
let reason;
|
|
4451
|
+
try {
|
|
4452
|
+
const body = await res.json();
|
|
4453
|
+
if (body && typeof body.reason === "string") reason = body.reason;
|
|
4454
|
+
} catch (parseErr) {
|
|
4455
|
+
this.log({
|
|
4456
|
+
level: "debug",
|
|
4457
|
+
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`,
|
|
4458
|
+
message_id: messageId
|
|
4459
|
+
});
|
|
4460
|
+
}
|
|
4461
|
+
if (reason === "needs_reauth") {
|
|
4462
|
+
this.log({
|
|
4463
|
+
level: "error",
|
|
4464
|
+
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)`,
|
|
4465
|
+
message_id: messageId
|
|
4466
|
+
});
|
|
4467
|
+
return { needsReauth: true };
|
|
4468
|
+
}
|
|
2539
4469
|
this.log({
|
|
2540
4470
|
level: "error",
|
|
2541
4471
|
message: `Attachment fetch for message ${messageId.slice(0, 8)} index ${index} returned HTTP ${res.status} \u2014 omitting this image (text turn proceeds)`,
|
|
@@ -2575,6 +4505,9 @@ var ChannelDriver = class {
|
|
|
2575
4505
|
if (this.attachmentsSkippedSignalled.has(messageId)) return;
|
|
2576
4506
|
this.attachmentsSkippedSignalled.add(messageId);
|
|
2577
4507
|
const skippedReason = capabilityUnknown ? "unknown" : "unsupported";
|
|
4508
|
+
const failedReason = outcomes.some(
|
|
4509
|
+
(o) => o.status === "failed" && o.reason === "needs_reauth"
|
|
4510
|
+
) ? "needs_reauth" : void 0;
|
|
2578
4511
|
this.log({
|
|
2579
4512
|
level: "info",
|
|
2580
4513
|
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`,
|
|
@@ -2584,7 +4517,8 @@ var ChannelDriver = class {
|
|
|
2584
4517
|
void this.postSignal(conversationId, messageId, "attachments_skipped", {
|
|
2585
4518
|
skipped,
|
|
2586
4519
|
failed,
|
|
2587
|
-
...skipped > 0 ? { skipped_reason: skippedReason } : {}
|
|
4520
|
+
...skipped > 0 ? { skipped_reason: skippedReason } : {},
|
|
4521
|
+
...failedReason ? { failed_reason: failedReason } : {}
|
|
2588
4522
|
});
|
|
2589
4523
|
}
|
|
2590
4524
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
@@ -2615,12 +4549,17 @@ var ChannelDriver = class {
|
|
|
2615
4549
|
stuckReported: false,
|
|
2616
4550
|
lastAliveAt: 0,
|
|
2617
4551
|
aliveInFlight: false,
|
|
4552
|
+
titleSynced: false,
|
|
4553
|
+
titleSyncInFlight: false,
|
|
2618
4554
|
awaitingHumanLatched: false,
|
|
2619
4555
|
pausedOnQuestion: false,
|
|
2620
4556
|
pausedOnPermission: false,
|
|
2621
4557
|
pausedClearConfirmed: false,
|
|
2622
4558
|
pausedInFlight: false,
|
|
2623
|
-
deliveryDeadlineAnchored: false
|
|
4559
|
+
deliveryDeadlineAnchored: false,
|
|
4560
|
+
b2PinnedSinceMs: 0,
|
|
4561
|
+
b2LastDescendantCheckMs: 0,
|
|
4562
|
+
b2AbandonedSignalled: false
|
|
2624
4563
|
});
|
|
2625
4564
|
}
|
|
2626
4565
|
/**
|
|
@@ -2635,9 +4574,10 @@ var ChannelDriver = class {
|
|
|
2635
4574
|
* opencode reports ACTIVELY `running` is watched to completion (its liveness
|
|
2636
4575
|
* heartbeat keeps the cron off its row), while a re-adopted turn that is paused
|
|
2637
4576
|
* awaiting a human — or queued/unreachable — is still bounded by `deadline` and
|
|
2638
|
-
* handed to the cron.
|
|
2639
|
-
*
|
|
2640
|
-
*
|
|
4577
|
+
* handed to the cron. Real invariant (#965): the cron MAY reclaim a row this
|
|
4578
|
+
* runner still holds; a reclaimed row that already ran is never re-dispatched
|
|
4579
|
+
* while opencode reports its turn ongoing (readopt's own gate here, and the
|
|
4580
|
+
* `pending`-row re-drive fence, `resolveRedrive`). `dispatchedAt` stays `now`
|
|
2641
4581
|
* (only the appear-guard uses it).
|
|
2642
4582
|
*
|
|
2643
4583
|
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
@@ -2688,12 +4628,17 @@ var ChannelDriver = class {
|
|
|
2688
4628
|
// with no extra `re_adopted` signal needed (folds old WI-6).
|
|
2689
4629
|
lastAliveAt: 0,
|
|
2690
4630
|
aliveInFlight: false,
|
|
4631
|
+
titleSynced: false,
|
|
4632
|
+
titleSyncInFlight: false,
|
|
2691
4633
|
awaitingHumanLatched: false,
|
|
2692
4634
|
pausedOnQuestion: false,
|
|
2693
4635
|
pausedOnPermission: false,
|
|
2694
4636
|
pausedClearConfirmed: false,
|
|
2695
4637
|
pausedInFlight: false,
|
|
2696
|
-
deliveryDeadlineAnchored: false
|
|
4638
|
+
deliveryDeadlineAnchored: false,
|
|
4639
|
+
b2PinnedSinceMs: 0,
|
|
4640
|
+
b2LastDescendantCheckMs: 0,
|
|
4641
|
+
b2AbandonedSignalled: false
|
|
2697
4642
|
});
|
|
2698
4643
|
}
|
|
2699
4644
|
/**
|
|
@@ -2825,9 +4770,8 @@ var ChannelDriver = class {
|
|
|
2825
4770
|
const awaitingHuman = observedOpen || latchedPaused;
|
|
2826
4771
|
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
2827
4772
|
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
2828
|
-
let claimed;
|
|
2829
4773
|
try {
|
|
2830
|
-
|
|
4774
|
+
await this.markProcessing(
|
|
2831
4775
|
conv.id,
|
|
2832
4776
|
inFlight.evidentMessageId,
|
|
2833
4777
|
sessionId,
|
|
@@ -2836,77 +4780,27 @@ var ChannelDriver = class {
|
|
|
2836
4780
|
);
|
|
2837
4781
|
} catch (err) {
|
|
2838
4782
|
if (err instanceof ChannelAuthError) throw err;
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
inFlight.started = true;
|
|
2848
|
-
if (!claimed) {
|
|
2849
|
-
this.log({
|
|
2850
|
-
level: "debug",
|
|
2851
|
-
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} already marked processing \u2014 continuing`,
|
|
2852
|
-
conversation_id: conv.id,
|
|
2853
|
-
message_id: inFlight.evidentMessageId
|
|
2854
|
-
});
|
|
2855
|
-
}
|
|
2856
|
-
}
|
|
2857
|
-
if (state === "done") {
|
|
2858
|
-
this.anchorDeliveryDeadline(inFlight);
|
|
2859
|
-
if (!inFlight.done) {
|
|
2860
|
-
this.log({
|
|
2861
|
-
level: "info",
|
|
2862
|
-
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
|
|
2863
|
-
conversation_id: conv.id,
|
|
2864
|
-
message_id: inFlight.evidentMessageId
|
|
2865
|
-
});
|
|
2866
|
-
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
2867
|
-
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
2868
|
-
try {
|
|
2869
|
-
await this.markDone(
|
|
2870
|
-
conv.id,
|
|
2871
|
-
inFlight.evidentMessageId,
|
|
2872
|
-
sessionId,
|
|
2873
|
-
inFlight.opencodeMessageId,
|
|
2874
|
-
title,
|
|
2875
|
-
usage
|
|
2876
|
-
);
|
|
2877
|
-
} catch (err) {
|
|
2878
|
-
if (err instanceof ChannelAuthError) throw err;
|
|
2879
|
-
if (err instanceof ChannelTerminalError) {
|
|
2880
|
-
this.log({
|
|
2881
|
-
level: "warn",
|
|
2882
|
-
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2883
|
-
conversation_id: conv.id,
|
|
2884
|
-
message_id: inFlight.evidentMessageId
|
|
2885
|
-
});
|
|
2886
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2887
|
-
return;
|
|
2888
|
-
}
|
|
2889
|
-
if (this.now() >= inFlight.deadline) {
|
|
2890
|
-
this.log({
|
|
2891
|
-
level: "warn",
|
|
2892
|
-
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)}`,
|
|
2893
|
-
conversation_id: conv.id,
|
|
2894
|
-
message_id: inFlight.evidentMessageId
|
|
2895
|
-
});
|
|
2896
|
-
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2897
|
-
return;
|
|
2898
|
-
}
|
|
4783
|
+
if (err instanceof ChannelTerminalError) {
|
|
4784
|
+
this.log({
|
|
4785
|
+
level: "error",
|
|
4786
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (terminal HTTP ${err.status}) \u2014 the server definitively refused the swap`,
|
|
4787
|
+
conversation_id: conv.id,
|
|
4788
|
+
message_id: inFlight.evidentMessageId
|
|
4789
|
+
});
|
|
4790
|
+
} else {
|
|
2899
4791
|
this.log({
|
|
2900
4792
|
level: "warn",
|
|
2901
|
-
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)}
|
|
4793
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} processing (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2902
4794
|
conversation_id: conv.id,
|
|
2903
4795
|
message_id: inFlight.evidentMessageId
|
|
2904
4796
|
});
|
|
2905
4797
|
return;
|
|
2906
4798
|
}
|
|
2907
|
-
inFlight.done = true;
|
|
2908
4799
|
}
|
|
2909
|
-
|
|
4800
|
+
inFlight.started = true;
|
|
4801
|
+
}
|
|
4802
|
+
if (state === "done") {
|
|
4803
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
2910
4804
|
return;
|
|
2911
4805
|
}
|
|
2912
4806
|
if (state === "failed") {
|
|
@@ -2920,8 +4814,16 @@ var ChannelDriver = class {
|
|
|
2920
4814
|
message_id: inFlight.evidentMessageId
|
|
2921
4815
|
});
|
|
2922
4816
|
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
4817
|
+
const failure = await this.classifyModelAuthFailure(messages, inFlight.opencodeMessageId);
|
|
2923
4818
|
try {
|
|
2924
|
-
await this.markFailed(
|
|
4819
|
+
await this.markFailed(
|
|
4820
|
+
conv.id,
|
|
4821
|
+
inFlight.evidentMessageId,
|
|
4822
|
+
sessionId,
|
|
4823
|
+
error2,
|
|
4824
|
+
usage,
|
|
4825
|
+
failure
|
|
4826
|
+
);
|
|
2925
4827
|
} catch (err) {
|
|
2926
4828
|
if (err instanceof ChannelAuthError) throw err;
|
|
2927
4829
|
if (err instanceof ChannelTerminalError) {
|
|
@@ -2966,6 +4868,44 @@ var ChannelDriver = class {
|
|
|
2966
4868
|
});
|
|
2967
4869
|
}
|
|
2968
4870
|
const activelyRunning = state === "running" && !awaitingHuman;
|
|
4871
|
+
const pinnedNow = activelyRunning && isPreamblePinnedRunning(messages, inFlight.opencodeMessageId);
|
|
4872
|
+
const snapshotReadable = messages != null && messages.length > 0;
|
|
4873
|
+
if (!pinnedNow) {
|
|
4874
|
+
if (snapshotReadable) {
|
|
4875
|
+
inFlight.b2PinnedSinceMs = 0;
|
|
4876
|
+
inFlight.b2LastDescendantCheckMs = 0;
|
|
4877
|
+
inFlight.b2AbandonedSignalled = false;
|
|
4878
|
+
}
|
|
4879
|
+
} else {
|
|
4880
|
+
if (inFlight.b2AbandonedSignalled) {
|
|
4881
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
4882
|
+
return;
|
|
4883
|
+
}
|
|
4884
|
+
if (inFlight.b2PinnedSinceMs === 0) inFlight.b2PinnedSinceMs = this.now();
|
|
4885
|
+
const pinnedForMs = this.now() - inFlight.b2PinnedSinceMs;
|
|
4886
|
+
if (pinnedForMs >= B2_ABANDONMENT_MIN_PINNED_MS && this.now() - inFlight.b2LastDescendantCheckMs >= B2_ABANDONMENT_RECHECK_MS) {
|
|
4887
|
+
inFlight.b2LastDescendantCheckMs = this.now();
|
|
4888
|
+
const descendantOngoing = await this.isAnyDescendantSessionOngoing(sessionId);
|
|
4889
|
+
if (isB2AbandonmentConfirmed({
|
|
4890
|
+
pinnedForMs,
|
|
4891
|
+
minPinnedMs: B2_ABANDONMENT_MIN_PINNED_MS,
|
|
4892
|
+
descendantOngoing
|
|
4893
|
+
})) {
|
|
4894
|
+
inFlight.b2AbandonedSignalled = true;
|
|
4895
|
+
this.log({
|
|
4896
|
+
level: "warn",
|
|
4897
|
+
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`,
|
|
4898
|
+
conversation_id: conv.id,
|
|
4899
|
+
message_id: id
|
|
4900
|
+
});
|
|
4901
|
+
void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
|
|
4902
|
+
watched_for_ms: pinnedForMs
|
|
4903
|
+
});
|
|
4904
|
+
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
4905
|
+
return;
|
|
4906
|
+
}
|
|
4907
|
+
}
|
|
4908
|
+
}
|
|
2969
4909
|
if (activelyRunning && this.now() - inFlight.processingAnchorMs >= ABSOLUTE_MAX_PROCESSING_MS) {
|
|
2970
4910
|
this.log({
|
|
2971
4911
|
level: "warn",
|
|
@@ -2985,6 +4925,18 @@ var ChannelDriver = class {
|
|
|
2985
4925
|
inFlight.aliveInFlight = false;
|
|
2986
4926
|
if (ok) inFlight.lastAliveAt = this.now();
|
|
2987
4927
|
});
|
|
4928
|
+
if (!inFlight.titleSynced && !inFlight.titleSyncInFlight) {
|
|
4929
|
+
inFlight.titleSyncInFlight = true;
|
|
4930
|
+
void this.resolveSessionTitle(sessionId, conv.id).then(async (title) => {
|
|
4931
|
+
if (!title) {
|
|
4932
|
+
inFlight.titleSyncInFlight = false;
|
|
4933
|
+
return;
|
|
4934
|
+
}
|
|
4935
|
+
const ok = await this.patchConversationTitle(conv.id, title);
|
|
4936
|
+
inFlight.titleSyncInFlight = false;
|
|
4937
|
+
if (ok) inFlight.titleSynced = true;
|
|
4938
|
+
});
|
|
4939
|
+
}
|
|
2988
4940
|
}
|
|
2989
4941
|
if (awaitingHuman) {
|
|
2990
4942
|
if (!inFlight.awaitingHumanLatched) {
|
|
@@ -3022,6 +4974,70 @@ var ChannelDriver = class {
|
|
|
3022
4974
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
3023
4975
|
}
|
|
3024
4976
|
}
|
|
4977
|
+
/**
|
|
4978
|
+
* Settle a message whose run-state has resolved `'done'` — extracted verbatim
|
|
4979
|
+
* (pure refactor, no behavior change) from `serviceInFlightMessage`'s former
|
|
4980
|
+
* inline `state === 'done'` branch body, so a SECOND caller (the #721
|
|
4981
|
+
* b2-abandonment resolution) can reach the exact same completion behavior
|
|
4982
|
+
* (delivery-deadline anchoring, title resolution, usage extraction, and
|
|
4983
|
+
* `markDone`'s auth/terminal/transient-retry discipline) without duplicating it
|
|
4984
|
+
* and risking the two copies silently drifting apart.
|
|
4985
|
+
*/
|
|
4986
|
+
async settleMessageDone(sessionId, watcher, inFlight, messages) {
|
|
4987
|
+
const conv = watcher.conv;
|
|
4988
|
+
this.anchorDeliveryDeadline(inFlight);
|
|
4989
|
+
if (!inFlight.done) {
|
|
4990
|
+
this.log({
|
|
4991
|
+
level: "info",
|
|
4992
|
+
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} completed \u2014 marking done`,
|
|
4993
|
+
conversation_id: conv.id,
|
|
4994
|
+
message_id: inFlight.evidentMessageId
|
|
4995
|
+
});
|
|
4996
|
+
const title = await this.resolveSessionTitle(sessionId, watcher.conv.id);
|
|
4997
|
+
const usage = messageUsage(messages, inFlight.opencodeMessageId);
|
|
4998
|
+
try {
|
|
4999
|
+
await this.markDone(
|
|
5000
|
+
conv.id,
|
|
5001
|
+
inFlight.evidentMessageId,
|
|
5002
|
+
sessionId,
|
|
5003
|
+
inFlight.opencodeMessageId,
|
|
5004
|
+
title,
|
|
5005
|
+
usage
|
|
5006
|
+
);
|
|
5007
|
+
} catch (err) {
|
|
5008
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
5009
|
+
if (err instanceof ChannelTerminalError) {
|
|
5010
|
+
this.log({
|
|
5011
|
+
level: "warn",
|
|
5012
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
5013
|
+
conversation_id: conv.id,
|
|
5014
|
+
message_id: inFlight.evidentMessageId
|
|
5015
|
+
});
|
|
5016
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
5017
|
+
return;
|
|
5018
|
+
}
|
|
5019
|
+
if (this.now() >= inFlight.deadline) {
|
|
5020
|
+
this.log({
|
|
5021
|
+
level: "warn",
|
|
5022
|
+
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)}`,
|
|
5023
|
+
conversation_id: conv.id,
|
|
5024
|
+
message_id: inFlight.evidentMessageId
|
|
5025
|
+
});
|
|
5026
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
5027
|
+
return;
|
|
5028
|
+
}
|
|
5029
|
+
this.log({
|
|
5030
|
+
level: "warn",
|
|
5031
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
5032
|
+
conversation_id: conv.id,
|
|
5033
|
+
message_id: inFlight.evidentMessageId
|
|
5034
|
+
});
|
|
5035
|
+
return;
|
|
5036
|
+
}
|
|
5037
|
+
inFlight.done = true;
|
|
5038
|
+
}
|
|
5039
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
5040
|
+
}
|
|
3025
5041
|
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
3026
5042
|
/**
|
|
3027
5043
|
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
@@ -3121,7 +5137,10 @@ var ChannelDriver = class {
|
|
|
3121
5137
|
* re-dispatched (at most once, see `forceReadoptRun`):
|
|
3122
5138
|
* - `done` → `markDone` now (guarded like the watcher's done branch);
|
|
3123
5139
|
* - `failed` → `markFailed` with the surfaced error (issue #182), so an
|
|
3124
|
-
* errored turn is reported failed on restart, NOT re-dispatched
|
|
5140
|
+
* errored turn is reported failed on restart, NOT re-dispatched —
|
|
5141
|
+
* EXCEPT a restart-ABORTED turn under a not-ongoing session,
|
|
5142
|
+
* which is a restart orphan wearing a terminal error and is
|
|
5143
|
+
* re-dispatched instead (issue #1310, see the branch below);
|
|
3125
5144
|
* - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
|
|
3126
5145
|
* tracking the stored id so the reply correlates by it;
|
|
3127
5146
|
* - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
|
|
@@ -3185,9 +5204,19 @@ var ChannelDriver = class {
|
|
|
3185
5204
|
void this.postSignal(row.conversation_id, row.id, "readopt_done");
|
|
3186
5205
|
return;
|
|
3187
5206
|
}
|
|
3188
|
-
|
|
5207
|
+
const restartAborted = state === "failed" && sessionOngoing === false && isAbortedTerminalReply(messages, ocId ?? "");
|
|
5208
|
+
if (restartAborted) {
|
|
5209
|
+
this.log({
|
|
5210
|
+
level: "info",
|
|
5211
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} carries an abort-shaped terminal error and session ${sessionId.slice(0, 8)} is not-ongoing per GET /session/status \u2014 restart orphan, re-dispatching instead of marking it permanently failed`,
|
|
5212
|
+
conversation_id: row.conversation_id,
|
|
5213
|
+
message_id: row.id
|
|
5214
|
+
});
|
|
5215
|
+
}
|
|
5216
|
+
if (state === "failed" && !restartAborted) {
|
|
3189
5217
|
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
3190
5218
|
const usage = messageUsage(messages, ocId ?? "");
|
|
5219
|
+
const failure = await this.classifyModelAuthFailure(messages, ocId ?? "");
|
|
3191
5220
|
this.log({
|
|
3192
5221
|
level: "error",
|
|
3193
5222
|
message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
@@ -3195,7 +5224,7 @@ var ChannelDriver = class {
|
|
|
3195
5224
|
message_id: row.id
|
|
3196
5225
|
});
|
|
3197
5226
|
try {
|
|
3198
|
-
await this.markFailed(row.conversation_id, row.id, sessionId, error2, usage);
|
|
5227
|
+
await this.markFailed(row.conversation_id, row.id, sessionId, error2, usage, failure);
|
|
3199
5228
|
} catch (err) {
|
|
3200
5229
|
if (err instanceof ChannelAuthError) throw err;
|
|
3201
5230
|
if (err instanceof ChannelTerminalError) {
|
|
@@ -3398,15 +5427,39 @@ var ChannelDriver = class {
|
|
|
3398
5427
|
}
|
|
3399
5428
|
if (ocId === null) {
|
|
3400
5429
|
this.awaitingReadopt.delete(row.id);
|
|
5430
|
+
const streak = this.recordUnconfirmedDispatch(row.id, sessionId);
|
|
5431
|
+
if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
|
|
5432
|
+
this.unconfirmedDispatchFailures.delete(row.id);
|
|
5433
|
+
this.sessions.delete(readoptConv.id);
|
|
5434
|
+
this.supersede(readoptConv.id, sessionId);
|
|
5435
|
+
const errorMessage = `OpenCode accepted this re-dispatched message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
|
|
5436
|
+
this.log({
|
|
5437
|
+
level: "error",
|
|
5438
|
+
message: errorMessage,
|
|
5439
|
+
conversation_id: row.conversation_id,
|
|
5440
|
+
message_id: row.id
|
|
5441
|
+
});
|
|
5442
|
+
await this.markFailed(row.conversation_id, row.id, null, errorMessage).catch((markErr) => {
|
|
5443
|
+
this.log({
|
|
5444
|
+
level: "warn",
|
|
5445
|
+
message: `markFailed PATCH for message ${row.id.slice(0, 8)} (conversation ${row.conversation_id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
|
|
5446
|
+
conversation_id: row.conversation_id,
|
|
5447
|
+
message_id: row.id
|
|
5448
|
+
});
|
|
5449
|
+
});
|
|
5450
|
+
void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
|
|
5451
|
+
return;
|
|
5452
|
+
}
|
|
3401
5453
|
this.log({
|
|
3402
5454
|
level: "warn",
|
|
3403
|
-
message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next drain`,
|
|
5455
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back (${streak}/${MAX_IDENTICAL_REDRIVE_POLL_FAILURES}) \u2014 leaving un-tracked to retry next drain`,
|
|
3404
5456
|
conversation_id: row.conversation_id,
|
|
3405
5457
|
message_id: row.id
|
|
3406
5458
|
});
|
|
3407
5459
|
void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
|
|
3408
5460
|
return;
|
|
3409
5461
|
}
|
|
5462
|
+
this.unconfirmedDispatchFailures.delete(row.id);
|
|
3410
5463
|
this.registerReadopted(readoptConv, sessionId, readoptMessage, ocId, this.processedAtMs(row));
|
|
3411
5464
|
this.dispatched.add(row.id);
|
|
3412
5465
|
this.readopted.add(row.id);
|
|
@@ -3462,7 +5515,8 @@ var ChannelDriver = class {
|
|
|
3462
5515
|
opencode_model: row.opencode_model,
|
|
3463
5516
|
source_message_id: row.source_message_id,
|
|
3464
5517
|
slack_user_id: row.slack_user_id,
|
|
3465
|
-
attachments: row.attachments ?? null
|
|
5518
|
+
attachments: row.attachments ?? null,
|
|
5519
|
+
opencode_message_id: row.opencode_message_id
|
|
3466
5520
|
};
|
|
3467
5521
|
}
|
|
3468
5522
|
/**
|
|
@@ -3601,6 +5655,47 @@ var ChannelDriver = class {
|
|
|
3601
5655
|
}
|
|
3602
5656
|
return false;
|
|
3603
5657
|
}
|
|
5658
|
+
/**
|
|
5659
|
+
* Tri-state variant of the upward parentID membership walk (#721), used ONLY
|
|
5660
|
+
* by `isAnyDescendantSessionOngoing`. Walks the SAME cached
|
|
5661
|
+
* `resolveSessionParent` chain `sessionBelongsTo` uses above, but — unlike
|
|
5662
|
+
* `sessionBelongsTo`, which deliberately collapses "confirmed not a
|
|
5663
|
+
* descendant" and "the walk's fetch failed" into the same `false` (safe for
|
|
5664
|
+
* its OTHER callers: interaction attribution and the recovery-path
|
|
5665
|
+
* `isAnyDescendantSessionAlive`, both of which just retry next tick with no
|
|
5666
|
+
* safety consequence either way) — this variant keeps those two outcomes
|
|
5667
|
+
* SEPARATE, because `isAnyDescendantSessionOngoing`'s caller
|
|
5668
|
+
* (`isB2AbandonmentConfirmed`) must never treat "couldn't tell" as "confirmed
|
|
5669
|
+
* not ongoing".
|
|
5670
|
+
*
|
|
5671
|
+
* Return contract:
|
|
5672
|
+
* - `true` → the walk reached `rootSessionId` — `sessionId` IS a descendant.
|
|
5673
|
+
* - `false` → the walk reached a definitive, parent-less root session
|
|
5674
|
+
* WITHOUT ever matching `rootSessionId` — `sessionId` is
|
|
5675
|
+
* CONFIRMED NOT a descendant of it.
|
|
5676
|
+
* - `null` → INDETERMINATE: a `GET /session/:id` fetch failed partway
|
|
5677
|
+
* through the walk (`resolveSessionParent` returned `undefined`),
|
|
5678
|
+
* or the depth cap (32) was hit without a definitive answer (a
|
|
5679
|
+
* pathological/cyclic chain proves nothing either way). NEVER
|
|
5680
|
+
* treat this the same as `false` — see `sessionBelongsTo`'s own
|
|
5681
|
+
* doc comment above for why that collapse is safe THERE but not
|
|
5682
|
+
* here.
|
|
5683
|
+
*
|
|
5684
|
+
* `sessionBelongsTo` itself is UNCHANGED — this is an additive helper scoped
|
|
5685
|
+
* to the live-path descendant check, not a modification of shared code used
|
|
5686
|
+
* by interaction attribution or the recovery path.
|
|
5687
|
+
*/
|
|
5688
|
+
async resolveSessionMembership(sessionId, rootSessionId) {
|
|
5689
|
+
let current = sessionId;
|
|
5690
|
+
for (let depth = 0; current && depth < 32; depth++) {
|
|
5691
|
+
if (current === rootSessionId) return true;
|
|
5692
|
+
const parent = await this.resolveSessionParent(current);
|
|
5693
|
+
if (parent === void 0) return null;
|
|
5694
|
+
if (parent === null) return false;
|
|
5695
|
+
current = parent;
|
|
5696
|
+
}
|
|
5697
|
+
return null;
|
|
5698
|
+
}
|
|
3604
5699
|
/**
|
|
3605
5700
|
* Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
|
|
3606
5701
|
* `null` for a root session (no parent) and `undefined` when opencode is
|
|
@@ -3623,19 +5718,36 @@ var ChannelDriver = class {
|
|
|
3623
5718
|
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
3624
5719
|
return parent;
|
|
3625
5720
|
}
|
|
5721
|
+
/**
|
|
5722
|
+
* OpenCode's synchronous default session title (e.g.
|
|
5723
|
+
* `"New session - 1737800000000"`), assigned immediately when a session is
|
|
5724
|
+
* created — before OpenCode's async LLM-based auto-titling later renames it
|
|
5725
|
+
* mid-turn (#549). Matched by this literal, case-sensitive prefix only; the
|
|
5726
|
+
* timestamp suffix's exact format is deliberately NOT matched, since the prefix
|
|
5727
|
+
* alone is the stable, cheap signal and over-anchoring on the timestamp
|
|
5728
|
+
* representation risks silently breaking if OpenCode ever changes it. Accepted
|
|
5729
|
+
* trade-off: a genuine LLM-assigned title that happens to literally start with
|
|
5730
|
+
* this prefix would also fail to latch (see `resolveSessionTitle`) —
|
|
5731
|
+
* vanishingly unlikely in practice, and deliberately not engineered around.
|
|
5732
|
+
*/
|
|
5733
|
+
static OPENCODE_DEFAULT_TITLE_PREFIX = /^New session - /;
|
|
3626
5734
|
/**
|
|
3627
5735
|
* Resolve (and cache in `sessionTitles`) the OpenCode session TITLE (#310) so the
|
|
3628
5736
|
* status PATCH can carry it into the "Live sessions" list. Driver-level cache so
|
|
3629
5737
|
* BOTH the watcher completion path and the restart-recovery re-adopt path (which
|
|
3630
5738
|
* has no watcher) can use it. `conversationId` is passed only for log context.
|
|
3631
5739
|
* Best-effort:
|
|
3632
|
-
* - a resolved NON-EMPTY title
|
|
5740
|
+
* - a resolved NON-EMPTY title that does NOT match
|
|
5741
|
+
* `OPENCODE_DEFAULT_TITLE_PREFIX` is cached and terminal (a real session name
|
|
3633
5742
|
* won't later un-name), so we do NOT re-GET `/session/:id` every tick;
|
|
3634
|
-
* - while the title is still absent
|
|
3635
|
-
*
|
|
3636
|
-
*
|
|
3637
|
-
*
|
|
3638
|
-
*
|
|
5743
|
+
* - while the title is still absent, empty, or matches the OpenCode
|
|
5744
|
+
* placeholder prefix (#549) we do NOT latch it — OpenCode names sessions
|
|
5745
|
+
* asynchronously mid-turn, so an early call (e.g. at `processing`) must leave
|
|
5746
|
+
* the cache unresolved and re-fetch on the next need so a later call (e.g. at
|
|
5747
|
+
* `done`) picks up the name assigned in the meantime. Such a call returns
|
|
5748
|
+
* `null` (omit the title on THIS PATCH) without caching. If a session is
|
|
5749
|
+
* never renamed, the title is omitted forever rather than ever persisting
|
|
5750
|
+
* the placeholder as a last resort;
|
|
3639
5751
|
* - a failed request likewise leaves the cache unresolved (retry next need)
|
|
3640
5752
|
* and returns `null` — it must NEVER throw or block completion.
|
|
3641
5753
|
* A failure is logged with agent/session context (no silent catch).
|
|
@@ -3648,7 +5760,7 @@ var ChannelDriver = class {
|
|
|
3648
5760
|
if (res.ok) {
|
|
3649
5761
|
const body = await res.json();
|
|
3650
5762
|
const title = body && typeof body.title === "string" ? body.title.trim() : "";
|
|
3651
|
-
if (title.length > 0) {
|
|
5763
|
+
if (title.length > 0 && !_ChannelDriver.OPENCODE_DEFAULT_TITLE_PREFIX.test(title)) {
|
|
3652
5764
|
this.sessionTitles.set(sessionId, title);
|
|
3653
5765
|
return title;
|
|
3654
5766
|
}
|
|
@@ -3668,6 +5780,54 @@ var ChannelDriver = class {
|
|
|
3668
5780
|
}
|
|
3669
5781
|
return null;
|
|
3670
5782
|
}
|
|
5783
|
+
/**
|
|
5784
|
+
* Best-effort mid-turn title sync (#711 follow-up): PATCH a resolved OpenCode
|
|
5785
|
+
* session title onto the conversation via the PLAIN conversation-update
|
|
5786
|
+
* endpoint (`PATCH /runners/:agentId/conversations/:conversationId`) — NOT the
|
|
5787
|
+
* message-status endpoint `markProcessing`/`markDone` use. Deliberately a
|
|
5788
|
+
* separate, lighter call: it carries no `status`, so it cannot re-trigger the
|
|
5789
|
+
* `processing`/`done` transition side effects (Slack notices, activity-log
|
|
5790
|
+
* rows, delivery jobs) those PATCHes gate on `transitioned` — this call only
|
|
5791
|
+
* ever touches `conversations.title`. That route (`routes/conversations.ts`)
|
|
5792
|
+
* skips a title write matching the stored value, so a redundant call with the
|
|
5793
|
+
* same title is a real no-op — it does not bump `updated_at`, which the
|
|
5794
|
+
* conversation list sorts and paginates on. (Note this is a DIFFERENT guard
|
|
5795
|
+
* from `threads.ts`'s "non-empty AND changed" one, which only covers the
|
|
5796
|
+
* message-status PATCH; the non-empty half is enforced here instead, by
|
|
5797
|
+
* `resolveSessionTitle` never returning an empty/placeholder title.)
|
|
5798
|
+
*
|
|
5799
|
+
* Telemetry-only / never blocks the caller, mirroring `postSignal`: a failure
|
|
5800
|
+
* is logged and the title is simply retried on the next heartbeat tick (the
|
|
5801
|
+
* caller only latches `titleSynced` on `true`).
|
|
5802
|
+
*/
|
|
5803
|
+
async patchConversationTitle(conversationId, title) {
|
|
5804
|
+
try {
|
|
5805
|
+
const res = await this.fetchImpl(
|
|
5806
|
+
`${this.apiUrl}/runners/${this.agentId}/conversations/${conversationId}`,
|
|
5807
|
+
{
|
|
5808
|
+
method: "PATCH",
|
|
5809
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
5810
|
+
body: JSON.stringify({ title })
|
|
5811
|
+
}
|
|
5812
|
+
);
|
|
5813
|
+
if (!res.ok) {
|
|
5814
|
+
this.log({
|
|
5815
|
+
level: "debug",
|
|
5816
|
+
message: `Mid-turn title sync PATCH for conversation ${conversationId.slice(0, 8)} returned HTTP ${res.status} (best-effort, will retry next heartbeat)`,
|
|
5817
|
+
conversation_id: conversationId
|
|
5818
|
+
});
|
|
5819
|
+
return false;
|
|
5820
|
+
}
|
|
5821
|
+
return true;
|
|
5822
|
+
} catch (err) {
|
|
5823
|
+
this.log({
|
|
5824
|
+
level: "debug",
|
|
5825
|
+
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)}`,
|
|
5826
|
+
conversation_id: conversationId
|
|
5827
|
+
});
|
|
5828
|
+
return false;
|
|
5829
|
+
}
|
|
5830
|
+
}
|
|
3671
5831
|
/**
|
|
3672
5832
|
* DEFENSIVE cross-check for the restart-recovery path (WI-2): is any descendant
|
|
3673
5833
|
* (`task` sub-agent) session under `rootSessionId` still genuinely doing work?
|
|
@@ -3726,6 +5886,84 @@ var ChannelDriver = class {
|
|
|
3726
5886
|
}
|
|
3727
5887
|
return false;
|
|
3728
5888
|
}
|
|
5889
|
+
/**
|
|
5890
|
+
* LIVE-PATH descendant-liveness check (#721): is any descendant (`task`
|
|
5891
|
+
* sub-agent) session under `rootSessionId` currently ONGOING per OpenCode's own
|
|
5892
|
+
* in-memory status map (`isSessionOngoing` — `busy`/`retry`)?
|
|
5893
|
+
*
|
|
5894
|
+
* Deliberately NOT `isAnyDescendantSessionAlive` (the RECOVERY-path
|
|
5895
|
+
* cross-check above): that method judges liveness from the child's OWN
|
|
5896
|
+
* TRANSCRIPT (`isSessionActivelyGenerating`), which is the right (only) option
|
|
5897
|
+
* on the recovery path because a restart WIPES `SessionStatus`. On the LIVE
|
|
5898
|
+
* path the local opencode server IS running, so its in-memory status map is
|
|
5899
|
+
* live and authoritative — and per ADR-0047 §4a ("the child has its own entry
|
|
5900
|
+
* [in the map]"), a `task` descendant's OWN busy/retry entry reflects its
|
|
5901
|
+
* ENTIRE turn (including any tool call it is itself executing), not a
|
|
5902
|
+
* per-message transcript snapshot. This sidesteps the "child's own tool is
|
|
5903
|
+
* executing, between its step's completion and the next generation step"
|
|
5904
|
+
* transcript gap that a transcript-based check would need a second,
|
|
5905
|
+
* sustained-window bound to guard against — it is simply not derived from
|
|
5906
|
+
* message timestamps at all.
|
|
5907
|
+
*
|
|
5908
|
+
* Why not just check `isSessionOngoing(port, rootSessionId)` (the ROOT's own
|
|
5909
|
+
* status, as the recovery path does per §4a)? Because on the LIVE path the
|
|
5910
|
+
* root session can be shared: a SECOND, unrelated user message can land on the
|
|
5911
|
+
* SAME session (issue #721's own root cause) and keep the root `busy` for a
|
|
5912
|
+
* reason that has nothing to do with THIS message's delegation. A `task`
|
|
5913
|
+
* descendant session is spawned for exactly one delegated turn and never
|
|
5914
|
+
* reused, so its OWN status-map entry is unambiguous evidence about that one
|
|
5915
|
+
* delegation — which the root's status is not.
|
|
5916
|
+
*
|
|
5917
|
+
* Why membership is checked via `resolveSessionMembership`, NOT
|
|
5918
|
+
* `sessionBelongsTo`: `sessionBelongsTo` collapses a transient
|
|
5919
|
+
* `GET /session/:id` fetch failure into "not a descendant", which would
|
|
5920
|
+
* silently drop a genuinely-live candidate from consideration on the one
|
|
5921
|
+
* unlucky tick its membership-walk fetch hiccups (#721).
|
|
5922
|
+
* `resolveSessionMembership` keeps that failure mode as a distinct `null`
|
|
5923
|
+
* (indeterminate) so it is folded into THIS method's own `indeterminate` flag
|
|
5924
|
+
* instead.
|
|
5925
|
+
*
|
|
5926
|
+
* Return contract (note the DIFFERENT judge vs. `isAnyDescendantSessionAlive`):
|
|
5927
|
+
* - `true` → some descendant session is `busy`/`retry` (genuinely ongoing).
|
|
5928
|
+
* - `false` → enumeration succeeded, EVERY candidate's MEMBERSHIP was
|
|
5929
|
+
* confirmed either way (`resolveSessionMembership` never
|
|
5930
|
+
* returned `null`), and every CONFIRMED descendant's status read
|
|
5931
|
+
* succeeded and is not ongoing (includes "no descendant session
|
|
5932
|
+
* exists at all" — e.g. a plain, non-`task` tool call).
|
|
5933
|
+
* - `null` → INDETERMINATE: `listSessions` failed, OR at least one
|
|
5934
|
+
* candidate's MEMBERSHIP could not be confirmed
|
|
5935
|
+
* (`resolveSessionMembership` returned `null` — a fetch failure
|
|
5936
|
+
* or pathological chain partway through the parent walk), OR at
|
|
5937
|
+
* least one CONFIRMED descendant's `isSessionOngoing` read
|
|
5938
|
+
* failed — and no OTHER candidate was already confirmed `true`.
|
|
5939
|
+
* The caller MUST NOT treat `null` the same as `false` here
|
|
5940
|
+
* (unlike the recovery cross-check's contract) — see
|
|
5941
|
+
* `isB2AbandonmentConfirmed`.
|
|
5942
|
+
*/
|
|
5943
|
+
async isAnyDescendantSessionOngoing(rootSessionId) {
|
|
5944
|
+
const sessions = await listSessions(this.port);
|
|
5945
|
+
if (!sessions) {
|
|
5946
|
+
this.log({
|
|
5947
|
+
level: "warn",
|
|
5948
|
+
message: `Could not enumerate sessions to check descendant liveness for root ${rootSessionId} (listSessions failed) \u2014 treating descendant liveness as indeterminate`
|
|
5949
|
+
});
|
|
5950
|
+
return null;
|
|
5951
|
+
}
|
|
5952
|
+
let indeterminate = false;
|
|
5953
|
+
for (const candidate of sessions) {
|
|
5954
|
+
if (!candidate?.id || candidate.id === rootSessionId) continue;
|
|
5955
|
+
const membership = await this.resolveSessionMembership(candidate.id, rootSessionId);
|
|
5956
|
+
if (membership === null) {
|
|
5957
|
+
indeterminate = true;
|
|
5958
|
+
continue;
|
|
5959
|
+
}
|
|
5960
|
+
if (membership === false) continue;
|
|
5961
|
+
const ongoing = await isSessionOngoing(this.port, candidate.id);
|
|
5962
|
+
if (ongoing === true) return true;
|
|
5963
|
+
if (ongoing === null) indeterminate = true;
|
|
5964
|
+
}
|
|
5965
|
+
return indeterminate ? null : false;
|
|
5966
|
+
}
|
|
3729
5967
|
/**
|
|
3730
5968
|
* Cheap decision-telemetry label for a running row's LAST correlated reply
|
|
3731
5969
|
* (WI-2 Task 2.2): which running SHAPE it is, for the status-gated recovery log.
|
|
@@ -3854,24 +6092,54 @@ var ChannelDriver = class {
|
|
|
3854
6092
|
}
|
|
3855
6093
|
return messages;
|
|
3856
6094
|
}
|
|
6095
|
+
/**
|
|
6096
|
+
* The `opencode_session_id` fragment of a status PATCH body — `{}` when this
|
|
6097
|
+
* conversation has ABANDONED that session (#553). The field is optional
|
|
6098
|
+
* server-side and an absent one leaves the persisted binding untouched, so
|
|
6099
|
+
* omitting it is how a routine status write stops resurrecting it.
|
|
6100
|
+
*
|
|
6101
|
+
* ONLY for writes whose sole cost is a lost deep link. The `processing` notice
|
|
6102
|
+
* degrades to no "View in Evident" link (the reaction swap still fires) and the
|
|
6103
|
+
* turn-failure notice is built from the PATCH's own `error` text with a link off
|
|
6104
|
+
* the persisted row — neither loses content the user came for. `markDone`
|
|
6105
|
+
* deliberately does NOT use this helper: the server fetches the reply text
|
|
6106
|
+
* THROUGH the session id it is given, so suppressing there would replace the
|
|
6107
|
+
* agent's answer with a bare "✅ Done!" (the #183/#187 failure). The
|
|
6108
|
+
* `ensureSession` guard, not this suppression, is what makes the self-heal
|
|
6109
|
+
* stick.
|
|
6110
|
+
*/
|
|
6111
|
+
sessionIdBody(sessionId, conversationId, messageId, status2) {
|
|
6112
|
+
if (!this.isSuperseded(conversationId, sessionId)) return { opencode_session_id: sessionId };
|
|
6113
|
+
this.log({
|
|
6114
|
+
level: "debug",
|
|
6115
|
+
message: `Omitting the abandoned OpenCode session ${sessionId.slice(0, 8)} from the '${status2}' update for message ${messageId.slice(0, 8)} so it is not re-bound to conversation ${conversationId.slice(0, 8)}`,
|
|
6116
|
+
conversation_id: conversationId,
|
|
6117
|
+
message_id: messageId
|
|
6118
|
+
});
|
|
6119
|
+
return {};
|
|
6120
|
+
}
|
|
3857
6121
|
/**
|
|
3858
6122
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
3859
6123
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
3860
6124
|
* opencode_session_id}` → `notifyMessageStarted` (hourglass→runner swap +
|
|
3861
6125
|
* deep-linked "View in Evident" notice).
|
|
3862
6126
|
*
|
|
3863
|
-
*
|
|
3864
|
-
* -
|
|
3865
|
-
*
|
|
3866
|
-
*
|
|
3867
|
-
*
|
|
3868
|
-
*
|
|
3869
|
-
*
|
|
3870
|
-
*
|
|
3871
|
-
*
|
|
3872
|
-
*
|
|
3873
|
-
*
|
|
3874
|
-
*
|
|
6127
|
+
* Outcome contract (consumed by the watcher's swap-to-running guard):
|
|
6128
|
+
* - resolves (`void`) → the server transitioned the row to
|
|
6129
|
+
* processing (or idempotently confirmed
|
|
6130
|
+
* already-processing — that answer is
|
|
6131
|
+
* still a 200, never a refusal);
|
|
6132
|
+
* - throws `ChannelAuthError` → 401/403 (terminal auth failure);
|
|
6133
|
+
* - throws `ChannelTerminalError` → a definitive non-retryable, non-auth 4xx
|
|
6134
|
+
* (404 the row or its conversation is
|
|
6135
|
+
* gone, 400 the update was rejected).
|
|
6136
|
+
* Retrying cannot help;
|
|
6137
|
+
* - throws a plain `Error` → a TRANSIENT failure (retryable 5xx/429
|
|
6138
|
+
* status, or a network-level error from
|
|
6139
|
+
* `fetch`) — i.e. NO definitive server
|
|
6140
|
+
* response — so the caller leaves the
|
|
6141
|
+
* message un-started and retries the swap
|
|
6142
|
+
* on the next tick.
|
|
3875
6143
|
* A single attempt (no internal retry): the watcher's per-tick loop is the
|
|
3876
6144
|
* retry vehicle for the swap-to-running.
|
|
3877
6145
|
*/
|
|
@@ -3883,18 +6151,18 @@ var ChannelDriver = class {
|
|
|
3883
6151
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3884
6152
|
body: JSON.stringify({
|
|
3885
6153
|
status: "processing",
|
|
3886
|
-
|
|
6154
|
+
...this.sessionIdBody(sessionId, conversationId, messageId, "processing"),
|
|
3887
6155
|
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
3888
6156
|
...title ? { title } : {}
|
|
3889
6157
|
})
|
|
3890
6158
|
}
|
|
3891
6159
|
);
|
|
3892
6160
|
this.assertAuth(res, "marking message as processing");
|
|
3893
|
-
if (res.ok) return
|
|
6161
|
+
if (res.ok) return;
|
|
3894
6162
|
if (isRetryableStatus(res.status)) {
|
|
3895
6163
|
throw new Error(`marking message as processing: HTTP ${res.status}`);
|
|
3896
6164
|
}
|
|
3897
|
-
|
|
6165
|
+
throw new ChannelTerminalError(`marking message as processing: HTTP ${res.status}`, res.status);
|
|
3898
6166
|
}
|
|
3899
6167
|
/**
|
|
3900
6168
|
* EXISTING combinedAuth completion route — idempotent (WI-CHAN-2). `PATCH
|
|
@@ -3932,6 +6200,11 @@ var ChannelDriver = class {
|
|
|
3932
6200
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3933
6201
|
body: JSON.stringify({
|
|
3934
6202
|
status: "done",
|
|
6203
|
+
// ALWAYS sent, even for a session this conversation has abandoned
|
|
6204
|
+
// (#553): the server reads the reply text back out of THIS session id
|
|
6205
|
+
// to deliver it. Omitting it would leave the user with "✅ Done!"
|
|
6206
|
+
// instead of the answer — a worse regression than the resurrection it
|
|
6207
|
+
// would prevent, which `ensureSession`'s guard handles anyway.
|
|
3935
6208
|
opencode_session_id: sessionId,
|
|
3936
6209
|
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {},
|
|
3937
6210
|
...title ? { title } : {},
|
|
@@ -3948,16 +6221,31 @@ var ChannelDriver = class {
|
|
|
3948
6221
|
}
|
|
3949
6222
|
/**
|
|
3950
6223
|
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
3951
|
-
* when provided (issue #182)
|
|
3952
|
-
* `
|
|
3953
|
-
*
|
|
3954
|
-
*
|
|
6224
|
+
* when provided (issue #182). Three states for `sessionId`:
|
|
6225
|
+
* - omitted (`undefined`) → don't send the field, leave the persisted
|
|
6226
|
+
* session untouched (unused today; kept for API symmetry).
|
|
6227
|
+
* - a real id (`string`) → send it, update the persisted session (the
|
|
6228
|
+
* turn-failure call sites: an errored OpenCode turn).
|
|
6229
|
+
* - explicit `null` → send it, CLEAR the persisted session (issue
|
|
6230
|
+
* #485's dispatch-handoff-failure call site: the session id still
|
|
6231
|
+
* exists but is wedged, so the next attempt must get a fresh one
|
|
6232
|
+
* instead of reusing it — see WI-1's server-side null-clearing PATCH).
|
|
3955
6233
|
*/
|
|
3956
|
-
async markFailed(conversationId, messageId, sessionId, error2, usage) {
|
|
6234
|
+
async markFailed(conversationId, messageId, sessionId, error2, usage, failure) {
|
|
3957
6235
|
const body = { status: "failed" };
|
|
3958
|
-
if (sessionId
|
|
6236
|
+
if (sessionId === null) {
|
|
6237
|
+
body.opencode_session_id = null;
|
|
6238
|
+
} else if (sessionId !== void 0) {
|
|
6239
|
+
Object.assign(body, this.sessionIdBody(sessionId, conversationId, messageId, "failed"));
|
|
6240
|
+
}
|
|
3959
6241
|
if (error2 !== void 0) body.error = error2;
|
|
3960
6242
|
if (usage) Object.assign(body, usage);
|
|
6243
|
+
if (failure) {
|
|
6244
|
+
body.failure_kind = failure.kind;
|
|
6245
|
+
body.failure_provider_id = failure.providerId;
|
|
6246
|
+
body.failure_model_id = failure.modelId;
|
|
6247
|
+
body.failure_reason = failure.reason;
|
|
6248
|
+
}
|
|
3961
6249
|
await this.callWithRetry(
|
|
3962
6250
|
"marking message as failed",
|
|
3963
6251
|
() => this.fetchImpl(
|
|
@@ -3970,6 +6258,29 @@ var ChannelDriver = class {
|
|
|
3970
6258
|
)
|
|
3971
6259
|
);
|
|
3972
6260
|
}
|
|
6261
|
+
/**
|
|
6262
|
+
* Classify an errored turn as a model-auth failure (#736 P1-4), for `markFailed`.
|
|
6263
|
+
*
|
|
6264
|
+
* `messageFailure` alone (structured OpenCode error → `model_auth`) covers
|
|
6265
|
+
* most cases; when it returns `null` on this ALREADY-FAILED turn, fall back
|
|
6266
|
+
* to the P1-2b zero-provider check — one extra loopback call to
|
|
6267
|
+
* `hasAnyConfiguredProvider`, only reached when the structured classifier
|
|
6268
|
+
* couldn't place it. Fails open (never throws): a fallback probe failure
|
|
6269
|
+
* (`null`/indeterminate) leaves the classification `null`, which produces
|
|
6270
|
+
* today's byte-identical PATCH body via `markFailed`'s `if (failure)` guard.
|
|
6271
|
+
*/
|
|
6272
|
+
async classifyModelAuthFailure(messages, userMessageId) {
|
|
6273
|
+
const classified = messageFailure(messages, userMessageId);
|
|
6274
|
+
if (classified != null) return classified;
|
|
6275
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
6276
|
+
const hasProvider = await hasAnyConfiguredProvider(this.port);
|
|
6277
|
+
return applyZeroProviderFallback(
|
|
6278
|
+
classified,
|
|
6279
|
+
hasProvider,
|
|
6280
|
+
reply?.info?.providerID ?? null,
|
|
6281
|
+
reply?.info?.modelID ?? null
|
|
6282
|
+
);
|
|
6283
|
+
}
|
|
3973
6284
|
/**
|
|
3974
6285
|
* Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
|
|
3975
6286
|
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
@@ -4122,10 +6433,16 @@ var ChannelDriver = class {
|
|
|
4122
6433
|
import chalk5 from "chalk";
|
|
4123
6434
|
import ora2 from "ora";
|
|
4124
6435
|
import { select as select2 } from "@inquirer/prompts";
|
|
6436
|
+
var INTERACTIVE_START_TIMEOUT_MS = 3e4;
|
|
4125
6437
|
async function ensureOpenCodeRunning(ctx) {
|
|
4126
6438
|
const healthCheck = await checkOpenCodeHealth(ctx.port);
|
|
4127
6439
|
if (healthCheck.healthy) {
|
|
4128
|
-
return {
|
|
6440
|
+
return {
|
|
6441
|
+
port: ctx.port,
|
|
6442
|
+
process: null,
|
|
6443
|
+
version: healthCheck.version ?? null,
|
|
6444
|
+
notReadyReason: null
|
|
6445
|
+
};
|
|
4129
6446
|
}
|
|
4130
6447
|
const runningInstances = await findHealthyOpenCodeInstances();
|
|
4131
6448
|
if (runningInstances.length > 0) {
|
|
@@ -4146,7 +6463,7 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
4146
6463
|
console.log(chalk5.yellow("Tip: Run with the correct port:"));
|
|
4147
6464
|
console.log(
|
|
4148
6465
|
chalk5.dim(
|
|
4149
|
-
` ${getCliName()} run --
|
|
6466
|
+
` ${getCliName()} run --runner ${ctx.agentId} --port ${runningInstances[0].port}`
|
|
4150
6467
|
)
|
|
4151
6468
|
);
|
|
4152
6469
|
}
|
|
@@ -4166,14 +6483,22 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
4166
6483
|
if (!ctx.interactive) {
|
|
4167
6484
|
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
4168
6485
|
const proc = await startOpenCode(ctx.port);
|
|
4169
|
-
const health = await waitForOpenCodeHealth(ctx.port,
|
|
6486
|
+
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
4170
6487
|
if (!health.healthy) {
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
6488
|
+
return {
|
|
6489
|
+
port: ctx.port,
|
|
6490
|
+
process: proc,
|
|
6491
|
+
version: null,
|
|
6492
|
+
notReadyReason: `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s`
|
|
6493
|
+
};
|
|
4174
6494
|
}
|
|
4175
6495
|
ctx.log(`OpenCode started on port ${ctx.port}${health.version ? ` (v${health.version})` : ""}`);
|
|
4176
|
-
return {
|
|
6496
|
+
return {
|
|
6497
|
+
port: ctx.port,
|
|
6498
|
+
process: proc,
|
|
6499
|
+
version: health.version ?? null,
|
|
6500
|
+
notReadyReason: null
|
|
6501
|
+
};
|
|
4177
6502
|
}
|
|
4178
6503
|
let port = ctx.port;
|
|
4179
6504
|
if (isPortInUse(port)) {
|
|
@@ -4226,122 +6551,15 @@ Port ${port} is already in use.`));
|
|
|
4226
6551
|
if (action === "start") {
|
|
4227
6552
|
const spinner = ora2("Starting OpenCode...").start();
|
|
4228
6553
|
const proc = await startOpenCode(port);
|
|
4229
|
-
const health = await waitForOpenCodeHealth(port,
|
|
6554
|
+
const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
|
|
4230
6555
|
if (!health.healthy) {
|
|
4231
6556
|
spinner.fail("Failed to start OpenCode");
|
|
4232
6557
|
throw new Error("OpenCode failed to start");
|
|
4233
6558
|
}
|
|
4234
6559
|
spinner.stop();
|
|
4235
|
-
return { port, process: proc, version: health.version ?? null };
|
|
4236
|
-
}
|
|
4237
|
-
return { port, process: null, version: null };
|
|
4238
|
-
}
|
|
4239
|
-
|
|
4240
|
-
// src/commands/agent-lookup.ts
|
|
4241
|
-
async function readErrorMessage(response) {
|
|
4242
|
-
const text = await response.text().catch(() => "");
|
|
4243
|
-
if (!text) return response.statusText || void 0;
|
|
4244
|
-
try {
|
|
4245
|
-
const data = JSON.parse(text);
|
|
4246
|
-
const message = data.message ?? data.error;
|
|
4247
|
-
if (typeof message === "string" && message.trim()) {
|
|
4248
|
-
return message;
|
|
4249
|
-
}
|
|
4250
|
-
} catch {
|
|
4251
|
-
}
|
|
4252
|
-
return text.trim() || response.statusText || void 0;
|
|
4253
|
-
}
|
|
4254
|
-
function authFailureHint(apiUrl, serverMessage) {
|
|
4255
|
-
const reason = serverMessage ? `: ${serverMessage}` : "";
|
|
4256
|
-
return `Authentication failed${reason}. Your credentials were rejected by ${apiUrl}. This usually means you logged in against a different environment, or your session expired \u2014 log in again pointing at this endpoint and retry.`;
|
|
4257
|
-
}
|
|
4258
|
-
async function resolveAgentIdFromKey(authHeader) {
|
|
4259
|
-
const apiUrl = getApiUrlConfig();
|
|
4260
|
-
try {
|
|
4261
|
-
const response = await fetch(`${apiUrl}/me`, {
|
|
4262
|
-
headers: { Authorization: authHeader }
|
|
4263
|
-
});
|
|
4264
|
-
if (response.status === 401) {
|
|
4265
|
-
const serverMessage = await readErrorMessage(response);
|
|
4266
|
-
return { error: authFailureHint(apiUrl, serverMessage), authFailed: true };
|
|
4267
|
-
}
|
|
4268
|
-
if (!response.ok) {
|
|
4269
|
-
const serverMessage = await readErrorMessage(response);
|
|
4270
|
-
return {
|
|
4271
|
-
error: `Failed to resolve runner from key (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
|
|
4272
|
-
};
|
|
4273
|
-
}
|
|
4274
|
-
const data = await response.json();
|
|
4275
|
-
if (data.auth_type === "agent_key" && data.agent_id) {
|
|
4276
|
-
return { agent_id: data.agent_id };
|
|
4277
|
-
}
|
|
4278
|
-
return {
|
|
4279
|
-
error: "Cannot resolve runner ID: auth type is not agent_key. Please provide --agent explicitly."
|
|
4280
|
-
};
|
|
4281
|
-
} catch (error2) {
|
|
4282
|
-
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
4283
|
-
return { error: `Failed to resolve runner from key: ${message}` };
|
|
4284
|
-
}
|
|
4285
|
-
}
|
|
4286
|
-
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
4287
|
-
const apiUrl = getApiUrlConfig();
|
|
4288
|
-
try {
|
|
4289
|
-
const response = await fetch(`${apiUrl}/runners/${agentId}/disconnect`, {
|
|
4290
|
-
method: "POST",
|
|
4291
|
-
headers: { Authorization: authHeader }
|
|
4292
|
-
});
|
|
4293
|
-
if (!response.ok) {
|
|
4294
|
-
const serverMessage = await readErrorMessage(response);
|
|
4295
|
-
return {
|
|
4296
|
-
ok: false,
|
|
4297
|
-
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
4298
|
-
};
|
|
4299
|
-
}
|
|
4300
|
-
return { ok: true };
|
|
4301
|
-
} catch (error2) {
|
|
4302
|
-
return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
|
|
4303
|
-
}
|
|
4304
|
-
}
|
|
4305
|
-
async function getAgentInfo(agentId, authHeader) {
|
|
4306
|
-
const apiUrl = getApiUrlConfig();
|
|
4307
|
-
try {
|
|
4308
|
-
const response = await fetch(`${apiUrl}/runners/${agentId}`, {
|
|
4309
|
-
headers: { Authorization: authHeader }
|
|
4310
|
-
});
|
|
4311
|
-
if (response.status === 401) {
|
|
4312
|
-
const serverMessage = await readErrorMessage(response);
|
|
4313
|
-
return { valid: false, error: authFailureHint(apiUrl, serverMessage), authFailed: true };
|
|
4314
|
-
}
|
|
4315
|
-
if (response.status === 403) {
|
|
4316
|
-
const serverMessage = await readErrorMessage(response);
|
|
4317
|
-
return {
|
|
4318
|
-
valid: false,
|
|
4319
|
-
error: serverMessage ?? "You do not have access to this runner (it may belong to a different team or organization)."
|
|
4320
|
-
};
|
|
4321
|
-
}
|
|
4322
|
-
if (response.status === 404) {
|
|
4323
|
-
const serverMessage = await readErrorMessage(response);
|
|
4324
|
-
return { valid: false, error: serverMessage ?? `Runner ${agentId} not found` };
|
|
4325
|
-
}
|
|
4326
|
-
if (!response.ok) {
|
|
4327
|
-
const serverMessage = await readErrorMessage(response);
|
|
4328
|
-
return {
|
|
4329
|
-
valid: false,
|
|
4330
|
-
error: `API error (HTTP ${response.status})${serverMessage ? `: ${serverMessage}` : ""}`
|
|
4331
|
-
};
|
|
4332
|
-
}
|
|
4333
|
-
const agent = await response.json();
|
|
4334
|
-
if (agent.agent_type !== "local") {
|
|
4335
|
-
return {
|
|
4336
|
-
valid: false,
|
|
4337
|
-
error: `Runner is type '${agent.agent_type}', must be 'local' for CLI connection`
|
|
4338
|
-
};
|
|
4339
|
-
}
|
|
4340
|
-
return { valid: true, agent };
|
|
4341
|
-
} catch (error2) {
|
|
4342
|
-
const message = error2 instanceof Error ? error2.message : "Unknown error";
|
|
4343
|
-
return { valid: false, error: `Failed to validate runner: ${message}` };
|
|
6560
|
+
return { port, process: proc, version: health.version ?? null, notReadyReason: null };
|
|
4344
6561
|
}
|
|
6562
|
+
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
4345
6563
|
}
|
|
4346
6564
|
|
|
4347
6565
|
// src/commands/run.ts
|
|
@@ -4349,6 +6567,7 @@ var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
|
4349
6567
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
4350
6568
|
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
4351
6569
|
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
6570
|
+
var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
4352
6571
|
function resolveLogLevel(options) {
|
|
4353
6572
|
const accepted = Object.keys(LOG_LEVELS);
|
|
4354
6573
|
const validate = (value, source) => {
|
|
@@ -4372,6 +6591,63 @@ function resolveLogLevel(options) {
|
|
|
4372
6591
|
}
|
|
4373
6592
|
return "info";
|
|
4374
6593
|
}
|
|
6594
|
+
function resolveFileSyncDirectories(raw, homeDir) {
|
|
6595
|
+
const directories = [];
|
|
6596
|
+
for (const entry of raw ?? []) {
|
|
6597
|
+
const trimmed = entry.trim();
|
|
6598
|
+
if (trimmed === "") {
|
|
6599
|
+
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
6600
|
+
}
|
|
6601
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join3(homeDir, trimmed.slice(2)) : trimmed;
|
|
6602
|
+
if (!isAbsolute2(expanded)) {
|
|
6603
|
+
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
6604
|
+
}
|
|
6605
|
+
const normalized = resolvePath(expanded);
|
|
6606
|
+
if (parse(normalized).root === normalized) {
|
|
6607
|
+
throw new Error(
|
|
6608
|
+
`--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
|
|
6609
|
+
);
|
|
6610
|
+
}
|
|
6611
|
+
if (!directories.includes(normalized)) {
|
|
6612
|
+
directories.push(normalized);
|
|
6613
|
+
}
|
|
6614
|
+
}
|
|
6615
|
+
if (directories.length > MAX_FILE_SYNC_DIRECTORIES) {
|
|
6616
|
+
throw new Error(
|
|
6617
|
+
`--enable-file-sync-to accepts at most ${MAX_FILE_SYNC_DIRECTORIES} directories; got ${directories.length}`
|
|
6618
|
+
);
|
|
6619
|
+
}
|
|
6620
|
+
return directories;
|
|
6621
|
+
}
|
|
6622
|
+
var DEFAULT_OPENCODE_START_TIMEOUT_SECONDS = 180;
|
|
6623
|
+
var MAX_OPENCODE_START_TIMEOUT_SECONDS = 3600;
|
|
6624
|
+
var OPENCODE_START_TIMEOUT_ENV = "EVIDENT_OPENCODE_START_TIMEOUT";
|
|
6625
|
+
function resolveOpenCodeStartTimeoutMs(options, env = process.env) {
|
|
6626
|
+
const defaultMs = DEFAULT_OPENCODE_START_TIMEOUT_SECONDS * 1e3;
|
|
6627
|
+
let raw;
|
|
6628
|
+
let source;
|
|
6629
|
+
if (options.opencodeStartTimeout !== void 0) {
|
|
6630
|
+
raw = options.opencodeStartTimeout;
|
|
6631
|
+
source = "--opencode-start-timeout";
|
|
6632
|
+
} else if (env[OPENCODE_START_TIMEOUT_ENV] !== void 0 && env[OPENCODE_START_TIMEOUT_ENV] !== "") {
|
|
6633
|
+
raw = env[OPENCODE_START_TIMEOUT_ENV];
|
|
6634
|
+
source = OPENCODE_START_TIMEOUT_ENV;
|
|
6635
|
+
} else {
|
|
6636
|
+
return { timeoutMs: defaultMs, warnings: [] };
|
|
6637
|
+
}
|
|
6638
|
+
const trimmed = raw.trim();
|
|
6639
|
+
const seconds = Number(trimmed);
|
|
6640
|
+
const isPositiveInteger = /^\d+$/.test(trimmed) && Number.isInteger(seconds) && seconds > 0;
|
|
6641
|
+
if (!isPositiveInteger || seconds > MAX_OPENCODE_START_TIMEOUT_SECONDS) {
|
|
6642
|
+
return {
|
|
6643
|
+
timeoutMs: defaultMs,
|
|
6644
|
+
warnings: [
|
|
6645
|
+
`Ignoring invalid ${source} "${raw}": expected a positive integer number of seconds (at most ${MAX_OPENCODE_START_TIMEOUT_SECONDS}); using the default ${DEFAULT_OPENCODE_START_TIMEOUT_SECONDS}s`
|
|
6646
|
+
]
|
|
6647
|
+
};
|
|
6648
|
+
}
|
|
6649
|
+
return { timeoutMs: seconds * 1e3, warnings: [] };
|
|
6650
|
+
}
|
|
4375
6651
|
function meetsThreshold(state, level) {
|
|
4376
6652
|
return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
|
|
4377
6653
|
}
|
|
@@ -4393,6 +6669,10 @@ function log2(state, message, level = "info") {
|
|
|
4393
6669
|
function logActivity(state, entry) {
|
|
4394
6670
|
const level = entry.level ?? (entry.type === "error" ? "error" : "info");
|
|
4395
6671
|
if (!meetsThreshold(state, level)) return;
|
|
6672
|
+
forwardRunnerActivity(
|
|
6673
|
+
{ level, message: entry.message, error: entry.error },
|
|
6674
|
+
{ agentId: state.agentId, authHeader: state.authHeader }
|
|
6675
|
+
);
|
|
4396
6676
|
const fullEntry = {
|
|
4397
6677
|
...entry,
|
|
4398
6678
|
level,
|
|
@@ -4492,23 +6772,46 @@ async function handleAuthError(state, error2) {
|
|
|
4492
6772
|
}
|
|
4493
6773
|
async function driveChannels(state, driver) {
|
|
4494
6774
|
let idlePolls = 0;
|
|
6775
|
+
let idleMs = 0;
|
|
6776
|
+
let consecutiveDrainFailures = 0;
|
|
6777
|
+
let unreachableMs = 0;
|
|
4495
6778
|
let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
6779
|
+
let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
4496
6780
|
while (state.running) {
|
|
6781
|
+
const cycleStartedAtMs = performance.now();
|
|
6782
|
+
let idleThisCycle = false;
|
|
6783
|
+
let unreachableThisCycle = false;
|
|
4497
6784
|
if (state.connection?.reconnecting && state.connection.reconnectPromise) {
|
|
4498
6785
|
logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
|
|
4499
6786
|
if (state.interactive) displayStatus(state);
|
|
4500
6787
|
await state.connection.reconnectPromise;
|
|
4501
6788
|
}
|
|
6789
|
+
const carriedOverFileSync = driver.fileSyncActivity().inFlight;
|
|
6790
|
+
void driver.syncPendingFiles().catch(
|
|
6791
|
+
(error2) => logActivity(state, {
|
|
6792
|
+
type: "error",
|
|
6793
|
+
error: `Runner file sync failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
6794
|
+
})
|
|
6795
|
+
);
|
|
4502
6796
|
try {
|
|
4503
6797
|
const processed = await driver.drainPending();
|
|
6798
|
+
consecutiveDrainFailures = 0;
|
|
6799
|
+
unreachableMs = 0;
|
|
4504
6800
|
state.messageCount += processed;
|
|
4505
6801
|
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
4506
6802
|
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
4507
|
-
|
|
6803
|
+
const appliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
6804
|
+
const filesApplied = appliedFiles !== lastSeenAppliedFiles;
|
|
6805
|
+
const fileActivity = carriedOverFileSync || filesApplied;
|
|
6806
|
+
lastSeenAppliedFiles = appliedFiles;
|
|
6807
|
+
if (filesApplied) state.claudeUsageRearm?.();
|
|
6808
|
+
if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity || fileActivity) {
|
|
4508
6809
|
idlePolls = 0;
|
|
6810
|
+
idleMs = 0;
|
|
4509
6811
|
if (processed > 0 && state.interactive) displayStatus(state);
|
|
4510
6812
|
} else if (state.idleTimeout !== null) {
|
|
4511
6813
|
idlePolls++;
|
|
6814
|
+
idleThisCycle = true;
|
|
4512
6815
|
if (idlePolls === 1) {
|
|
4513
6816
|
logActivity(state, {
|
|
4514
6817
|
type: "info",
|
|
@@ -4532,21 +6835,44 @@ async function driveChannels(state, driver) {
|
|
|
4532
6835
|
const errorMessage = error2 instanceof Error ? error2.message : String(error2);
|
|
4533
6836
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
|
|
4534
6837
|
if (state.interactive) displayStatus(state);
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
if (
|
|
4542
|
-
|
|
6838
|
+
if (driver.hasInFlightWatchers()) {
|
|
6839
|
+
consecutiveDrainFailures = 0;
|
|
6840
|
+
unreachableMs = 0;
|
|
6841
|
+
} else if (state.idleTimeout !== null) {
|
|
6842
|
+
consecutiveDrainFailures++;
|
|
6843
|
+
unreachableThisCycle = true;
|
|
6844
|
+
if (consecutiveDrainFailures === 1) {
|
|
6845
|
+
logActivity(state, {
|
|
6846
|
+
type: "info",
|
|
6847
|
+
message: `Cannot reach Evident, will exit if this persists past the idle timeout (timeout: ${state.idleTimeout}s)...`
|
|
6848
|
+
});
|
|
6849
|
+
if (state.interactive) displayStatus(state);
|
|
6850
|
+
}
|
|
4543
6851
|
}
|
|
4544
6852
|
}
|
|
6853
|
+
await new Promise((resolve3) => setTimeout(resolve3, CHANNEL_POLL_INTERVAL_MS));
|
|
6854
|
+
const cycleMs = performance.now() - cycleStartedAtMs;
|
|
6855
|
+
if (idleThisCycle) idleMs += cycleMs;
|
|
6856
|
+
if (unreachableThisCycle) unreachableMs += cycleMs;
|
|
6857
|
+
if (state.idleTimeout !== null && consecutiveDrainFailures >= 2 && unreachableMs > state.idleTimeout * 1e3) {
|
|
6858
|
+
logActivity(state, {
|
|
6859
|
+
type: "info",
|
|
6860
|
+
level: "warn",
|
|
6861
|
+
message: `Exiting: could not reach Evident for ${consecutiveDrainFailures} consecutive polls (${Math.round(unreachableMs / 1e3)}s)`
|
|
6862
|
+
});
|
|
6863
|
+
if (state.interactive) displayStatus(state);
|
|
6864
|
+
break;
|
|
6865
|
+
}
|
|
6866
|
+
if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3) {
|
|
6867
|
+
logActivity(state, { type: "info", message: "Idle timeout reached" });
|
|
6868
|
+
if (state.interactive) displayStatus(state);
|
|
6869
|
+
break;
|
|
6870
|
+
}
|
|
4545
6871
|
}
|
|
4546
6872
|
}
|
|
4547
6873
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
4548
|
-
async function runSweep(state, driver,
|
|
4549
|
-
const mode = `age=${
|
|
6874
|
+
async function runSweep(state, driver, config) {
|
|
6875
|
+
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
4550
6876
|
try {
|
|
4551
6877
|
const sessions = await listSessions(state.port);
|
|
4552
6878
|
if (sessions === null) {
|
|
@@ -4559,8 +6885,8 @@ async function runSweep(state, driver, config2) {
|
|
|
4559
6885
|
const toDelete = selectSessionsToDelete(
|
|
4560
6886
|
sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
|
|
4561
6887
|
{
|
|
4562
|
-
maxAgeMs:
|
|
4563
|
-
maxCount:
|
|
6888
|
+
maxAgeMs: config.maxAgeMs,
|
|
6889
|
+
maxCount: config.maxCount,
|
|
4564
6890
|
nowMs: Date.now(),
|
|
4565
6891
|
protectedIds: driver.protectedSessionIds()
|
|
4566
6892
|
}
|
|
@@ -4596,7 +6922,7 @@ async function runSweep(state, driver, config2) {
|
|
|
4596
6922
|
}
|
|
4597
6923
|
}
|
|
4598
6924
|
function scheduleSessionCleanup(state, driver, options) {
|
|
4599
|
-
const
|
|
6925
|
+
const config = resolveSessionCleanupConfig(
|
|
4600
6926
|
{
|
|
4601
6927
|
maxAge: options.sessionCleanupMaxAge,
|
|
4602
6928
|
maxCount: options.sessionCleanupMaxCount,
|
|
@@ -4604,21 +6930,129 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
4604
6930
|
},
|
|
4605
6931
|
process.env
|
|
4606
6932
|
);
|
|
4607
|
-
for (const warning2 of
|
|
6933
|
+
for (const warning2 of config.warnings) {
|
|
4608
6934
|
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
4609
6935
|
}
|
|
4610
|
-
if (!
|
|
6936
|
+
if (!config.enabled) return;
|
|
4611
6937
|
logActivity(state, {
|
|
4612
6938
|
type: "info",
|
|
4613
|
-
message: `Session cleanup enabled (age=${
|
|
6939
|
+
message: `Session cleanup enabled (age=${config.maxAgeMs ?? "\u2014"}, count=${config.maxCount ?? "\u2014"}, interval=${config.intervalMs}ms)`
|
|
4614
6940
|
});
|
|
4615
|
-
const interval = setInterval(() => void runSweep(state, driver,
|
|
6941
|
+
const interval = setInterval(() => void runSweep(state, driver, config), config.intervalMs);
|
|
4616
6942
|
const firstSweep = setTimeout(
|
|
4617
|
-
() => void runSweep(state, driver,
|
|
6943
|
+
() => void runSweep(state, driver, config),
|
|
4618
6944
|
SESSION_CLEANUP_FIRST_SWEEP_MS
|
|
4619
6945
|
);
|
|
4620
6946
|
state.sessionCleanupTimers.push(interval, firstSweep);
|
|
4621
6947
|
}
|
|
6948
|
+
function claudeUsageFailureStreakSuffix(consecutiveFailures) {
|
|
6949
|
+
return consecutiveFailures > 1 ? ` (${consecutiveFailures} consecutive failures)` : "";
|
|
6950
|
+
}
|
|
6951
|
+
function scheduleClaudeUsageReporting(state, options) {
|
|
6952
|
+
const { mode, warnings } = resolveClaudeUsageReportingMode(
|
|
6953
|
+
options.claudeUsageReporting,
|
|
6954
|
+
process.env
|
|
6955
|
+
);
|
|
6956
|
+
for (const warning2 of warnings) {
|
|
6957
|
+
logActivity(state, {
|
|
6958
|
+
type: "info",
|
|
6959
|
+
level: "warn",
|
|
6960
|
+
message: `Claude usage reporting: ${warning2}`
|
|
6961
|
+
});
|
|
6962
|
+
}
|
|
6963
|
+
if (mode === "off") {
|
|
6964
|
+
logActivity(state, {
|
|
6965
|
+
type: "info",
|
|
6966
|
+
level: "debug",
|
|
6967
|
+
message: "Claude usage reporting is off (--claude-usage-reporting off)"
|
|
6968
|
+
});
|
|
6969
|
+
return null;
|
|
6970
|
+
}
|
|
6971
|
+
let consecutiveFailures = 0;
|
|
6972
|
+
let armed = false;
|
|
6973
|
+
let rearmRequested = false;
|
|
6974
|
+
const scheduleNextTick = () => {
|
|
6975
|
+
armed = true;
|
|
6976
|
+
rearmRequested = false;
|
|
6977
|
+
state.claudeUsageTimer = setTimeout(() => void tick(false), nextReportDelayMs());
|
|
6978
|
+
};
|
|
6979
|
+
const rearm = () => {
|
|
6980
|
+
if (armed) {
|
|
6981
|
+
rearmRequested = true;
|
|
6982
|
+
return;
|
|
6983
|
+
}
|
|
6984
|
+
rearmRequested = false;
|
|
6985
|
+
armed = true;
|
|
6986
|
+
state.claudeUsageTimer = setTimeout(() => void tick(true), FIRST_REPORT_DELAY_MS);
|
|
6987
|
+
};
|
|
6988
|
+
const tick = async (isProbe) => {
|
|
6989
|
+
try {
|
|
6990
|
+
const usage = await getClaudeUsage();
|
|
6991
|
+
const result = await reportClaudeUsage(state.agentId, state.authHeader, usage);
|
|
6992
|
+
if (result.ok) {
|
|
6993
|
+
if (consecutiveFailures > 0) {
|
|
6994
|
+
logActivity(state, {
|
|
6995
|
+
type: "info",
|
|
6996
|
+
level: "info",
|
|
6997
|
+
message: "Claude usage reporting recovered"
|
|
6998
|
+
});
|
|
6999
|
+
}
|
|
7000
|
+
consecutiveFailures = 0;
|
|
7001
|
+
logActivity(state, {
|
|
7002
|
+
type: "info",
|
|
7003
|
+
level: "debug",
|
|
7004
|
+
message: "Reported Claude usage to Evident"
|
|
7005
|
+
});
|
|
7006
|
+
} else {
|
|
7007
|
+
consecutiveFailures++;
|
|
7008
|
+
logActivity(state, {
|
|
7009
|
+
type: "info",
|
|
7010
|
+
level: claudeUsageFailureLogLevel(consecutiveFailures),
|
|
7011
|
+
message: `Failed to report Claude usage: ${result.error}${claudeUsageFailureStreakSuffix(consecutiveFailures)}`
|
|
7012
|
+
});
|
|
7013
|
+
}
|
|
7014
|
+
scheduleNextTick();
|
|
7015
|
+
} catch (error2) {
|
|
7016
|
+
if (error2 instanceof ClaudeUsageError && isLocalCredentialProblem(error2)) {
|
|
7017
|
+
if (mode === "on") {
|
|
7018
|
+
logActivity(state, {
|
|
7019
|
+
type: "info",
|
|
7020
|
+
level: "warn",
|
|
7021
|
+
message: "Claude usage reporting is forced on but no usable Claude Code login was found \u2014 run `claude` to sign in; reporting will keep retrying"
|
|
7022
|
+
});
|
|
7023
|
+
scheduleNextTick();
|
|
7024
|
+
} else if (isProbe) {
|
|
7025
|
+
logActivity(state, {
|
|
7026
|
+
type: "info",
|
|
7027
|
+
level: "debug",
|
|
7028
|
+
message: `Claude usage reporting: ${error2.message}`
|
|
7029
|
+
});
|
|
7030
|
+
armed = false;
|
|
7031
|
+
if (rearmRequested) rearm();
|
|
7032
|
+
} else {
|
|
7033
|
+
logActivity(state, {
|
|
7034
|
+
type: "info",
|
|
7035
|
+
level: "debug",
|
|
7036
|
+
message: `Claude usage reporting: ${error2.message}`
|
|
7037
|
+
});
|
|
7038
|
+
scheduleNextTick();
|
|
7039
|
+
}
|
|
7040
|
+
} else {
|
|
7041
|
+
consecutiveFailures++;
|
|
7042
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
7043
|
+
logActivity(state, {
|
|
7044
|
+
type: "info",
|
|
7045
|
+
level: claudeUsageFailureLogLevel(consecutiveFailures),
|
|
7046
|
+
message: `Claude usage reporting failed: ${message}${claudeUsageFailureStreakSuffix(consecutiveFailures)}`
|
|
7047
|
+
});
|
|
7048
|
+
scheduleNextTick();
|
|
7049
|
+
}
|
|
7050
|
+
}
|
|
7051
|
+
};
|
|
7052
|
+
armed = true;
|
|
7053
|
+
state.claudeUsageTimer = setTimeout(() => void tick(true), FIRST_REPORT_DELAY_MS);
|
|
7054
|
+
return rearm;
|
|
7055
|
+
}
|
|
4622
7056
|
async function notifyOffline(state) {
|
|
4623
7057
|
if (!state.agentId || !state.authHeader) return;
|
|
4624
7058
|
if (!state.connected) {
|
|
@@ -4636,13 +7070,29 @@ async function notifyOffline(state) {
|
|
|
4636
7070
|
if (state.interactive) displayStatus(state);
|
|
4637
7071
|
}
|
|
4638
7072
|
}
|
|
7073
|
+
async function timeShutdownPhase(state, durations, name, run2) {
|
|
7074
|
+
const startedAt = Date.now();
|
|
7075
|
+
try {
|
|
7076
|
+
return await run2();
|
|
7077
|
+
} finally {
|
|
7078
|
+
const elapsedMs = Date.now() - startedAt;
|
|
7079
|
+
durations[name] = elapsedMs;
|
|
7080
|
+
log2(state, `Shutdown phase ${name}: ${elapsedMs}ms`);
|
|
7081
|
+
}
|
|
7082
|
+
}
|
|
4639
7083
|
async function cleanup(state, opts = {}) {
|
|
7084
|
+
const durations = {};
|
|
4640
7085
|
state.running = false;
|
|
4641
7086
|
for (const timer of state.sessionCleanupTimers) {
|
|
4642
7087
|
clearInterval(timer);
|
|
4643
7088
|
clearTimeout(timer);
|
|
4644
7089
|
}
|
|
4645
7090
|
state.sessionCleanupTimers = [];
|
|
7091
|
+
if (state.claudeUsageTimer) {
|
|
7092
|
+
clearTimeout(state.claudeUsageTimer);
|
|
7093
|
+
state.claudeUsageTimer = null;
|
|
7094
|
+
}
|
|
7095
|
+
state.claudeUsageRearm = null;
|
|
4646
7096
|
if (opts.graceful && state.channelDriver) {
|
|
4647
7097
|
state.channelDriver.stop();
|
|
4648
7098
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
@@ -4650,7 +7100,13 @@ async function cleanup(state, opts = {}) {
|
|
|
4650
7100
|
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
4651
7101
|
displayStatus(state);
|
|
4652
7102
|
}
|
|
4653
|
-
const
|
|
7103
|
+
const driver = state.channelDriver;
|
|
7104
|
+
const settled = await timeShutdownPhase(
|
|
7105
|
+
state,
|
|
7106
|
+
durations,
|
|
7107
|
+
"drain",
|
|
7108
|
+
() => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
|
|
7109
|
+
);
|
|
4654
7110
|
if (!settled) {
|
|
4655
7111
|
logActivity(state, {
|
|
4656
7112
|
type: "info",
|
|
@@ -4659,13 +7115,15 @@ async function cleanup(state, opts = {}) {
|
|
|
4659
7115
|
if (state.interactive) displayStatus(state);
|
|
4660
7116
|
}
|
|
4661
7117
|
}
|
|
4662
|
-
await notifyOffline(state);
|
|
7118
|
+
await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
|
|
4663
7119
|
if (state.connection) {
|
|
4664
|
-
state.connection
|
|
7120
|
+
const connection = state.connection;
|
|
7121
|
+
await timeShutdownPhase(state, durations, "tunnel_close", () => connection.close());
|
|
4665
7122
|
state.connection = null;
|
|
4666
7123
|
}
|
|
4667
7124
|
if (state.opencodeProcess) {
|
|
4668
|
-
|
|
7125
|
+
const opencodeProcess = state.opencodeProcess;
|
|
7126
|
+
await timeShutdownPhase(state, durations, "opencode_stop", () => stopOpenCode(opencodeProcess));
|
|
4669
7127
|
if (state.interactive) {
|
|
4670
7128
|
logActivity(state, { type: "info", message: "Stopped OpenCode process" });
|
|
4671
7129
|
displayStatus(state);
|
|
@@ -4674,12 +7132,15 @@ async function cleanup(state, opts = {}) {
|
|
|
4674
7132
|
}
|
|
4675
7133
|
state.opencodeProcess = null;
|
|
4676
7134
|
}
|
|
7135
|
+
return durations;
|
|
4677
7136
|
}
|
|
4678
7137
|
async function run(options) {
|
|
4679
7138
|
const interactive = isInteractive(options.json);
|
|
4680
7139
|
let logLevel;
|
|
7140
|
+
let fileSyncDirectories;
|
|
4681
7141
|
try {
|
|
4682
7142
|
logLevel = resolveLogLevel(options);
|
|
7143
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir3());
|
|
4683
7144
|
} catch (error2) {
|
|
4684
7145
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
4685
7146
|
if (options.json) {
|
|
@@ -4712,8 +7173,29 @@ async function run(options) {
|
|
|
4712
7173
|
messageCount: 0,
|
|
4713
7174
|
lastProxiedActivityAt: null,
|
|
4714
7175
|
sessionCleanupTimers: [],
|
|
7176
|
+
claudeUsageTimer: null,
|
|
7177
|
+
claudeUsageRearm: null,
|
|
4715
7178
|
authHeader: ""
|
|
4716
7179
|
};
|
|
7180
|
+
setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
|
|
7181
|
+
if (fileSyncDirectories.length > 0) {
|
|
7182
|
+
log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
|
|
7183
|
+
} else {
|
|
7184
|
+
log2(state, "File sync is disabled (no --enable-file-sync-to given)", "debug");
|
|
7185
|
+
}
|
|
7186
|
+
if (!options.runner && options.agent) {
|
|
7187
|
+
telemetry.info(
|
|
7188
|
+
EventTypes.DEPRECATED_AGENT_FLAG_USED,
|
|
7189
|
+
"Deprecated --agent flag used instead of --runner",
|
|
7190
|
+
{ command: "run" },
|
|
7191
|
+
state.agentId
|
|
7192
|
+
);
|
|
7193
|
+
const agentFlagNotice = "--agent is deprecated, use --runner instead; will be removed in a future release.";
|
|
7194
|
+
log2(state, agentFlagNotice, "warn");
|
|
7195
|
+
if (state.interactive && !state.json) {
|
|
7196
|
+
logActivity(state, { type: "info", level: "warn", message: agentFlagNotice });
|
|
7197
|
+
}
|
|
7198
|
+
}
|
|
4717
7199
|
if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
|
|
4718
7200
|
log2(
|
|
4719
7201
|
state,
|
|
@@ -4724,14 +7206,38 @@ async function run(options) {
|
|
|
4724
7206
|
const handleSignal = async () => {
|
|
4725
7207
|
if (state.shuttingDown) return;
|
|
4726
7208
|
state.shuttingDown = true;
|
|
7209
|
+
const shutdownStartedAt = Date.now();
|
|
4727
7210
|
if (state.interactive) {
|
|
4728
7211
|
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
4729
7212
|
displayStatus(state);
|
|
4730
7213
|
} else {
|
|
4731
7214
|
log2(state, "Shutting down...");
|
|
4732
7215
|
}
|
|
4733
|
-
await cleanup(state, { graceful: true });
|
|
4734
|
-
|
|
7216
|
+
const durations = await cleanup(state, { graceful: true });
|
|
7217
|
+
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
7218
|
+
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
7219
|
+
let timer;
|
|
7220
|
+
const flushed = shutdownTelemetry().then(
|
|
7221
|
+
() => true,
|
|
7222
|
+
(error2) => {
|
|
7223
|
+
log2(
|
|
7224
|
+
state,
|
|
7225
|
+
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
7226
|
+
"warn"
|
|
7227
|
+
);
|
|
7228
|
+
return true;
|
|
7229
|
+
}
|
|
7230
|
+
);
|
|
7231
|
+
const timedOut = new Promise((resolve3) => {
|
|
7232
|
+
timer = setTimeout(() => resolve3(false), telemetryBudgetMs);
|
|
7233
|
+
});
|
|
7234
|
+
if (!await Promise.race([flushed, timedOut])) {
|
|
7235
|
+
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
7236
|
+
}
|
|
7237
|
+
clearTimeout(timer);
|
|
7238
|
+
});
|
|
7239
|
+
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
7240
|
+
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
4735
7241
|
process.exit(0);
|
|
4736
7242
|
};
|
|
4737
7243
|
process.on("SIGINT", handleSignal);
|
|
@@ -4748,6 +7254,7 @@ async function run(options) {
|
|
|
4748
7254
|
console.log(chalk6.dim("Or run `evident login` for interactive authentication"));
|
|
4749
7255
|
blank();
|
|
4750
7256
|
process.exit(1);
|
|
7257
|
+
return;
|
|
4751
7258
|
}
|
|
4752
7259
|
blank();
|
|
4753
7260
|
console.log(chalk6.yellow("You are not logged in to Evident."));
|
|
@@ -4764,6 +7271,19 @@ async function run(options) {
|
|
|
4764
7271
|
logActivity(state, { type: "info", level: "warn", message: credentials2.notice });
|
|
4765
7272
|
}
|
|
4766
7273
|
}
|
|
7274
|
+
if (credentials2.keySource === "agent_key") {
|
|
7275
|
+
telemetry.info(
|
|
7276
|
+
EventTypes.DEPRECATED_AGENT_KEY_ENV_USED,
|
|
7277
|
+
"Deprecated EVIDENT_AGENT_KEY env var used instead of EVIDENT_RUNNER_KEY",
|
|
7278
|
+
{ command: "run" },
|
|
7279
|
+
state.agentId
|
|
7280
|
+
);
|
|
7281
|
+
const agentKeyNotice = "EVIDENT_AGENT_KEY is deprecated, use EVIDENT_RUNNER_KEY instead; will be removed in a future release.";
|
|
7282
|
+
log2(state, agentKeyNotice, "warn");
|
|
7283
|
+
if (state.interactive && !state.json) {
|
|
7284
|
+
logActivity(state, { type: "info", level: "warn", message: agentKeyNotice });
|
|
7285
|
+
}
|
|
7286
|
+
}
|
|
4767
7287
|
if (!state.agentId) {
|
|
4768
7288
|
if (credentials2.authType === "agent_key") {
|
|
4769
7289
|
const resolved = await resolveAgentIdFromKey(state.authHeader);
|
|
@@ -4779,6 +7299,7 @@ async function run(options) {
|
|
|
4779
7299
|
} else {
|
|
4780
7300
|
printError(resolved.error || "Failed to resolve runner ID from key");
|
|
4781
7301
|
process.exit(1);
|
|
7302
|
+
return;
|
|
4782
7303
|
}
|
|
4783
7304
|
} else {
|
|
4784
7305
|
printError(
|
|
@@ -4792,6 +7313,7 @@ async function run(options) {
|
|
|
4792
7313
|
);
|
|
4793
7314
|
blank();
|
|
4794
7315
|
process.exit(1);
|
|
7316
|
+
return;
|
|
4795
7317
|
}
|
|
4796
7318
|
}
|
|
4797
7319
|
telemetry.info(
|
|
@@ -4832,25 +7354,67 @@ async function run(options) {
|
|
|
4832
7354
|
}
|
|
4833
7355
|
spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
|
|
4834
7356
|
state.agentName = validation.agent.name;
|
|
7357
|
+
const microvmId = process.env.MICROVM_ID?.trim();
|
|
7358
|
+
if (microvmId) {
|
|
7359
|
+
const reported = await reportMicrovmId(state.agentId, state.authHeader, microvmId);
|
|
7360
|
+
if (reported.ok) {
|
|
7361
|
+
log2(state, "Reported MicroVM identity so this runner can be resumed rather than restarted");
|
|
7362
|
+
} else {
|
|
7363
|
+
const message = `Could not report MicroVM identity (future wakes will cold-start): ${reported.error}`;
|
|
7364
|
+
log2(state, message, "warn");
|
|
7365
|
+
if (state.interactive && !state.json) {
|
|
7366
|
+
logActivity(state, { type: "info", level: "warn", message });
|
|
7367
|
+
}
|
|
7368
|
+
}
|
|
7369
|
+
} else {
|
|
7370
|
+
log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
|
|
7371
|
+
}
|
|
7372
|
+
const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
|
|
7373
|
+
for (const warning2 of opencodeStartTimeoutWarnings) {
|
|
7374
|
+
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
7375
|
+
}
|
|
4835
7376
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
4836
7377
|
try {
|
|
4837
7378
|
const oc = await ensureOpenCodeRunning({
|
|
4838
7379
|
port: state.port,
|
|
4839
7380
|
interactive: state.interactive,
|
|
4840
7381
|
agentId: state.agentId,
|
|
4841
|
-
log: (message) => log2(state, message)
|
|
7382
|
+
log: (message) => log2(state, message),
|
|
7383
|
+
startTimeoutMs: opencodeStartTimeoutMs
|
|
4842
7384
|
});
|
|
4843
7385
|
state.port = oc.port;
|
|
4844
7386
|
state.opencodeProcess = oc.process;
|
|
4845
7387
|
state.opencodeVersion = oc.version;
|
|
4846
|
-
state.opencodeConnected = oc.
|
|
7388
|
+
state.opencodeConnected = oc.notReadyReason === null;
|
|
4847
7389
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
4848
7390
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
7391
|
+
if (!state.interactive && oc.notReadyReason !== null) {
|
|
7392
|
+
const message = `OpenCode is not ready on port ${state.port}: ${oc.notReadyReason}. The runner will still come online, but messages will fail until opencode answers \u2014 raise the wait with --opencode-start-timeout <seconds> (env ${OPENCODE_START_TIMEOUT_ENV}).`;
|
|
7393
|
+
logActivity(state, { type: "info", level: "warn", message });
|
|
7394
|
+
} else {
|
|
7395
|
+
const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
|
|
7396
|
+
if (versionWarning) {
|
|
7397
|
+
log2(state, versionWarning, "warn");
|
|
7398
|
+
if (state.interactive && !state.json) {
|
|
7399
|
+
logActivity(state, { type: "info", level: "warn", message: versionWarning });
|
|
7400
|
+
}
|
|
7401
|
+
}
|
|
7402
|
+
const noProviderWarning = buildNoProviderWarning(
|
|
7403
|
+
await hasAnyConfiguredProvider(state.port)
|
|
7404
|
+
);
|
|
7405
|
+
if (noProviderWarning) {
|
|
7406
|
+
log2(state, noProviderWarning, "warn");
|
|
7407
|
+
if (state.interactive && !state.json) {
|
|
7408
|
+
logActivity(state, { type: "info", level: "warn", message: noProviderWarning });
|
|
7409
|
+
blank();
|
|
7410
|
+
console.log(chalk6.yellow("\u26A0 No OpenCode model provider is configured."));
|
|
7411
|
+
console.log(
|
|
7412
|
+
chalk6.dim(
|
|
7413
|
+
`Run ${chalk6.cyan("opencode auth login")} to set one up \u2014 messages will fail until then.`
|
|
7414
|
+
)
|
|
7415
|
+
);
|
|
7416
|
+
blank();
|
|
7417
|
+
}
|
|
4854
7418
|
}
|
|
4855
7419
|
}
|
|
4856
7420
|
} catch (error2) {
|
|
@@ -4865,6 +7429,10 @@ async function run(options) {
|
|
|
4865
7429
|
getAuthHeader: () => state.authHeader,
|
|
4866
7430
|
conversationFilter: state.conversationFilter,
|
|
4867
7431
|
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
7432
|
+
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
7433
|
+
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
7434
|
+
fileSyncDirectories,
|
|
7435
|
+
homeDir: homedir3(),
|
|
4868
7436
|
log: (entry) => (
|
|
4869
7437
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
4870
7438
|
// survive the sink filter (they no longer collapse to info). `type`
|
|
@@ -4891,6 +7459,18 @@ async function run(options) {
|
|
|
4891
7459
|
type: "info",
|
|
4892
7460
|
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (runner: ${agentId})`
|
|
4893
7461
|
});
|
|
7462
|
+
if (options.tunnelReadyFile) {
|
|
7463
|
+
const marker = writeTunnelReadyMarker(options.tunnelReadyFile, agentId);
|
|
7464
|
+
if (marker.ok) {
|
|
7465
|
+
log2(state, `Wrote tunnel readiness marker to ${options.tunnelReadyFile}`, "debug");
|
|
7466
|
+
} else {
|
|
7467
|
+
log2(
|
|
7468
|
+
state,
|
|
7469
|
+
`Failed to write tunnel readiness marker to ${options.tunnelReadyFile}: ${marker.error}`,
|
|
7470
|
+
"error"
|
|
7471
|
+
);
|
|
7472
|
+
}
|
|
7473
|
+
}
|
|
4894
7474
|
emitAgentConnected(state.agentId, {
|
|
4895
7475
|
port: state.port,
|
|
4896
7476
|
cli_version: getCliVersion(),
|
|
@@ -4946,6 +7526,12 @@ async function run(options) {
|
|
|
4946
7526
|
onDrainPing: () => {
|
|
4947
7527
|
if (!state.running) return;
|
|
4948
7528
|
logActivity(state, { type: "info", message: "Drain ping received \u2014 draining" });
|
|
7529
|
+
void channelDriver.syncPendingFiles().catch(
|
|
7530
|
+
(error2) => logActivity(state, {
|
|
7531
|
+
type: "error",
|
|
7532
|
+
error: `Runner file sync failed on ping: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
7533
|
+
})
|
|
7534
|
+
);
|
|
4949
7535
|
channelDriver.drainPending().then((processed) => {
|
|
4950
7536
|
if (processed > 0) {
|
|
4951
7537
|
state.messageCount += processed;
|
|
@@ -4975,6 +7561,7 @@ async function run(options) {
|
|
|
4975
7561
|
throw error2;
|
|
4976
7562
|
}
|
|
4977
7563
|
scheduleSessionCleanup(state, channelDriver, options);
|
|
7564
|
+
state.claudeUsageRearm = scheduleClaudeUsageReporting(state, options);
|
|
4978
7565
|
if (!interactive || state.json) {
|
|
4979
7566
|
log2(state, "Driving channel messages...");
|
|
4980
7567
|
}
|
|
@@ -5029,10 +7616,18 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
|
|
|
5029
7616
|
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);
|
|
5030
7617
|
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 }));
|
|
5031
7618
|
program.command("whoami").description("Show the currently logged in user").action(whoami);
|
|
5032
|
-
program.command("
|
|
7619
|
+
program.command("status").description("Check whether the configured credentials can reach Evident").option("--json", "Output in JSON format").action((options) => status({ json: options.json }));
|
|
7620
|
+
program.command("claude-usage").description("[spike] Show Claude subscription usage (requires a local `claude login`)").action(claudeUsage);
|
|
7621
|
+
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(
|
|
7622
|
+
"-a, --agent [id]",
|
|
7623
|
+
"Deprecated alias for --runner (still supported; --runner wins if both are given)"
|
|
7624
|
+
).option("-p, --port <port>", "OpenCode port (default: 4096)", "4096").option(
|
|
5033
7625
|
"--log-level <level>",
|
|
5034
7626
|
"Log verbosity: debug | info | warn | error (default: info). Env: EVIDENT_LOG_LEVEL"
|
|
5035
|
-
).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(
|
|
7627
|
+
).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(
|
|
7628
|
+
"--opencode-start-timeout <seconds>",
|
|
7629
|
+
"Seconds to wait for OpenCode to become healthy when the runner starts it (default: 180). Env: EVIDENT_OPENCODE_START_TIMEOUT"
|
|
7630
|
+
).option("--json", "Output in JSON format").option(
|
|
5036
7631
|
"--session-cleanup-max-age <duration>",
|
|
5037
7632
|
"Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
|
|
5038
7633
|
).option(
|
|
@@ -5041,6 +7636,17 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
5041
7636
|
).option(
|
|
5042
7637
|
"--session-cleanup-interval <duration>",
|
|
5043
7638
|
"How often the cleanup sweep runs (default: 1h). Env: EVIDENT_SESSION_CLEANUP_INTERVAL"
|
|
7639
|
+
).option(
|
|
7640
|
+
"--claude-usage-reporting <mode>",
|
|
7641
|
+
"Report Claude subscription usage to Evident: auto | on | off (default: auto). Env: EVIDENT_CLAUDE_USAGE_REPORTING"
|
|
7642
|
+
).option(
|
|
7643
|
+
"--enable-file-sync-to <dir>",
|
|
7644
|
+
"Allow Evident to write files into this directory (repeatable). Omit to disable file sync entirely.",
|
|
7645
|
+
(value, previous) => previous.concat([value]),
|
|
7646
|
+
[]
|
|
7647
|
+
).option(
|
|
7648
|
+
"--tunnel-ready-file <path>",
|
|
7649
|
+
"Path to write once the tunnel is connected (opt-in; unused unless an operator/image sets it)"
|
|
5044
7650
|
).action(
|
|
5045
7651
|
(options) => {
|
|
5046
7652
|
run({
|
|
@@ -5053,11 +7659,21 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
5053
7659
|
verbose: options.verbose,
|
|
5054
7660
|
conversation: options.conversation,
|
|
5055
7661
|
idleTimeout: options.idleTimeout ? parseInt(options.idleTimeout, 10) : void 0,
|
|
7662
|
+
// Raw string — validation/precedence is single-sourced in run.ts's
|
|
7663
|
+
// resolveOpenCodeStartTimeoutMs (flag > EVIDENT_OPENCODE_START_TIMEOUT > 180s default).
|
|
7664
|
+
opencodeStartTimeout: options.opencodeStartTimeout,
|
|
5056
7665
|
json: options.json,
|
|
5057
7666
|
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
5058
7667
|
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
5059
7668
|
sessionCleanupMaxCount: options.sessionCleanupMaxCount,
|
|
5060
|
-
sessionCleanupInterval: options.sessionCleanupInterval
|
|
7669
|
+
sessionCleanupInterval: options.sessionCleanupInterval,
|
|
7670
|
+
// Raw string — the resolver in run.ts single-sources parsing
|
|
7671
|
+
// (resolveClaudeUsageReportingMode).
|
|
7672
|
+
claudeUsageReporting: options.claudeUsageReporting,
|
|
7673
|
+
// Raw values — expansion/validation is single-sourced in run.ts's
|
|
7674
|
+
// resolveFileSyncDirectories.
|
|
7675
|
+
enableFileSyncTo: options.enableFileSyncTo,
|
|
7676
|
+
tunnelReadyFile: options.tunnelReadyFile
|
|
5061
7677
|
});
|
|
5062
7678
|
}
|
|
5063
7679
|
);
|