@evident-ai/cli 3.4.1-dev.31006db → 3.4.1-dev.444e897
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 +17 -0
- package/dist/index.js +1459 -281
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
-
import { createRequire } from "module";
|
|
4
|
+
import { createRequire as createRequire2 } from "module";
|
|
5
5
|
import { Command } from "commander";
|
|
6
6
|
|
|
7
7
|
// src/commands/login.ts
|
|
@@ -11,8 +11,8 @@ import chalk2 from "chalk";
|
|
|
11
11
|
|
|
12
12
|
// src/lib/config.ts
|
|
13
13
|
import Conf from "conf";
|
|
14
|
-
import { chmodSync, existsSync, statSync } from "fs";
|
|
15
|
-
import { dirname } from "path";
|
|
14
|
+
import { chmodSync, existsSync, statSync } from "node:fs";
|
|
15
|
+
import { dirname } from "node: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 = {
|
|
@@ -763,6 +763,13 @@ function toReportedOpenAiWindow(window) {
|
|
|
763
763
|
resets_at: window.resetsAt
|
|
764
764
|
};
|
|
765
765
|
}
|
|
766
|
+
function toReportedOpenAiSubscription(snapshot) {
|
|
767
|
+
if (!snapshot.subscription) return null;
|
|
768
|
+
return {
|
|
769
|
+
owner_email: snapshot.subscription.ownerEmail,
|
|
770
|
+
plan_type: snapshot.subscription.planType
|
|
771
|
+
};
|
|
772
|
+
}
|
|
766
773
|
async function reportOpenAiUsage(agentId, authHeader, snapshot) {
|
|
767
774
|
try {
|
|
768
775
|
const apiUrl = getApiUrlConfig();
|
|
@@ -773,7 +780,8 @@ async function reportOpenAiUsage(agentId, authHeader, snapshot) {
|
|
|
773
780
|
primary: toReportedOpenAiWindow(snapshot.primary),
|
|
774
781
|
secondary: toReportedOpenAiWindow(snapshot.secondary),
|
|
775
782
|
has_credits: snapshot.hasCredits,
|
|
776
|
-
credits_unlimited: snapshot.creditsUnlimited
|
|
783
|
+
credits_unlimited: snapshot.creditsUnlimited,
|
|
784
|
+
subscription: toReportedOpenAiSubscription(snapshot)
|
|
777
785
|
}),
|
|
778
786
|
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
779
787
|
});
|
|
@@ -797,6 +805,7 @@ async function reportResourceUsage(agentId, authHeader, usage) {
|
|
|
797
805
|
headers: { Authorization: authHeader, "Content-Type": "application/json" },
|
|
798
806
|
body: JSON.stringify({
|
|
799
807
|
cpu_percent: usage.cpuPercent,
|
|
808
|
+
cpu_peak_percent: usage.cpuPeakPercent,
|
|
800
809
|
cpu_count: usage.cpuCount,
|
|
801
810
|
memory_total_bytes: usage.memoryTotalBytes,
|
|
802
811
|
memory_available_bytes: usage.memoryAvailableBytes,
|
|
@@ -1000,10 +1009,10 @@ async function status(options = {}) {
|
|
|
1000
1009
|
}
|
|
1001
1010
|
|
|
1002
1011
|
// src/lib/claude-usage.ts
|
|
1003
|
-
import { execFileSync } from "child_process";
|
|
1004
|
-
import { readFileSync } from "fs";
|
|
1005
|
-
import { homedir } from "os";
|
|
1006
|
-
import { join } from "path";
|
|
1012
|
+
import { execFileSync } from "node:child_process";
|
|
1013
|
+
import { readFileSync } from "node:fs";
|
|
1014
|
+
import { homedir } from "node:os";
|
|
1015
|
+
import { join } from "node:path";
|
|
1007
1016
|
var CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
|
|
1008
1017
|
var CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
|
|
1009
1018
|
var CLAUDE_PROFILE_TIMEOUT_MS = 2e3;
|
|
@@ -1183,10 +1192,10 @@ async function claudeUsage() {
|
|
|
1183
1192
|
}
|
|
1184
1193
|
|
|
1185
1194
|
// src/commands/run.ts
|
|
1186
|
-
import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as
|
|
1187
|
-
import { homedir as
|
|
1188
|
-
import { isAbsolute as isAbsolute3, join as
|
|
1189
|
-
import
|
|
1195
|
+
import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync6 } from "node:fs";
|
|
1196
|
+
import { homedir as homedir6 } from "node:os";
|
|
1197
|
+
import { isAbsolute as isAbsolute3, join as join10, parse, resolve as resolvePath2 } from "node:path";
|
|
1198
|
+
import chalk7 from "chalk";
|
|
1190
1199
|
|
|
1191
1200
|
// ../../packages/types/src/agents/index.ts
|
|
1192
1201
|
var MICROVM_MAX_LIFETIME_MS = 8 * 60 * 6e4;
|
|
@@ -1207,6 +1216,7 @@ var TelemetryEventTypes = {
|
|
|
1207
1216
|
// ../../packages/types/src/tunnel/index.ts
|
|
1208
1217
|
var MAX_FRAME_BYTES = 256 * 1024;
|
|
1209
1218
|
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
1219
|
+
var TUNNEL_USAGE_REARM_PING_PATH = "/__evident/usage-rearm";
|
|
1210
1220
|
|
|
1211
1221
|
// ../../packages/types/src/runner-files.ts
|
|
1212
1222
|
var MAX_FILE_PUSH_BYTES = 64 * 1024;
|
|
@@ -1244,7 +1254,7 @@ function stripQuery(url) {
|
|
|
1244
1254
|
|
|
1245
1255
|
// src/commands/run.ts
|
|
1246
1256
|
import ora3 from "ora";
|
|
1247
|
-
import { select as
|
|
1257
|
+
import { select as select4 } from "@inquirer/prompts";
|
|
1248
1258
|
|
|
1249
1259
|
// src/lib/telemetry.ts
|
|
1250
1260
|
var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
@@ -1417,12 +1427,50 @@ var SEVERITY_BY_LEVEL = {
|
|
|
1417
1427
|
warn: "warning",
|
|
1418
1428
|
error: "error"
|
|
1419
1429
|
};
|
|
1430
|
+
function parseOpenCodeLogLine(line) {
|
|
1431
|
+
const normalisedLine = line.replace(/\r$/, "");
|
|
1432
|
+
const levelMatch = normalisedLine.match(/(?:^|\s)level=(\w+)/i);
|
|
1433
|
+
if (!levelMatch) return null;
|
|
1434
|
+
const level = levelMatch[1].toUpperCase();
|
|
1435
|
+
if (level !== "WARN" && level !== "ERROR") return null;
|
|
1436
|
+
const sessionMatch = normalisedLine.match(/(?:^|\s)sessionID=(\S+)/);
|
|
1437
|
+
return { level: level === "WARN" ? "warn" : "error", sessionID: sessionMatch?.[1] };
|
|
1438
|
+
}
|
|
1439
|
+
var MAX_LINE_BUFFER_BYTES = 16 * 1024;
|
|
1440
|
+
function createOpenCodeActivityForwarder(getContext) {
|
|
1441
|
+
let buffer = Buffer.alloc(0);
|
|
1442
|
+
const flushLine = (line) => {
|
|
1443
|
+
const parsed = parseOpenCodeLogLine(line);
|
|
1444
|
+
if (!parsed) return;
|
|
1445
|
+
forwardRunnerActivity(
|
|
1446
|
+
{
|
|
1447
|
+
level: parsed.level,
|
|
1448
|
+
error: line,
|
|
1449
|
+
metadata: parsed.sessionID ? { sessionID: parsed.sessionID } : void 0,
|
|
1450
|
+
source: "opencode"
|
|
1451
|
+
},
|
|
1452
|
+
getContext()
|
|
1453
|
+
);
|
|
1454
|
+
};
|
|
1455
|
+
return (chunk) => {
|
|
1456
|
+
buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, "utf-8")]);
|
|
1457
|
+
let newlineIndex;
|
|
1458
|
+
while ((newlineIndex = buffer.indexOf(10)) !== -1) {
|
|
1459
|
+
flushLine(buffer.subarray(0, newlineIndex).toString("utf-8").replace(/\r$/, ""));
|
|
1460
|
+
buffer = buffer.subarray(newlineIndex + 1);
|
|
1461
|
+
}
|
|
1462
|
+
if (buffer.length > MAX_LINE_BUFFER_BYTES) {
|
|
1463
|
+
flushLine(buffer.toString("utf-8"));
|
|
1464
|
+
buffer = Buffer.alloc(0);
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1420
1468
|
var MAX_MESSAGE_LENGTH = 500;
|
|
1421
1469
|
var MAX_METADATA_VALUE_LENGTH = 200;
|
|
1422
1470
|
var MAX_METADATA_ENTRIES = 20;
|
|
1423
1471
|
var TRUNCATION_MARKER = "\u2026";
|
|
1424
1472
|
function redact(message) {
|
|
1425
|
-
return message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/https?:\/\/\S+/g, "<url>");
|
|
1473
|
+
return message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/(?<![A-Za-z0-9_-])sk-ant-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-ant-***").replace(/(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-proj-***").replace(/(?<![A-Za-z0-9_-])sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "sk-***").replace(/(?<![A-Za-z0-9_-])(gh[oprsu])_[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "$1_***").replace(/(?<![A-Za-z0-9_-])github_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_-])/g, "github_pat_***").replace(/https?:\/\/\S+/g, "<url>");
|
|
1426
1474
|
}
|
|
1427
1475
|
function truncate(message) {
|
|
1428
1476
|
if (message.length <= MAX_MESSAGE_LENGTH) return message;
|
|
@@ -1448,43 +1496,47 @@ function sanitiseMetadata(metadata) {
|
|
|
1448
1496
|
}
|
|
1449
1497
|
var RATE_LIMIT_WINDOW_MS = 6e4;
|
|
1450
1498
|
var RATE_LIMIT_MAX_EVENTS = 30;
|
|
1451
|
-
var
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1499
|
+
var rateWindows = /* @__PURE__ */ new Map();
|
|
1500
|
+
function admitUnderRateLimit(source, now) {
|
|
1501
|
+
let window = rateWindows.get(source);
|
|
1502
|
+
if (!window) {
|
|
1503
|
+
window = { windowStartedAt: 0, windowCount: 0, windowDroppedCount: 0 };
|
|
1504
|
+
rateWindows.set(source, window);
|
|
1505
|
+
}
|
|
1506
|
+
if (now - window.windowStartedAt >= RATE_LIMIT_WINDOW_MS) {
|
|
1507
|
+
if (window.windowDroppedCount > 0) {
|
|
1457
1508
|
console.error(
|
|
1458
|
-
`[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)`
|
|
1509
|
+
`[runner-activity-telemetry] rate cap reached: dropped ${window.windowDroppedCount} ${window.windowDroppedCount === 1 ? "entry" : "entries"} in the last ${RATE_LIMIT_WINDOW_MS / 1e3}s (cap ${RATE_LIMIT_MAX_EVENTS}/min) for source "${source}"`
|
|
1459
1510
|
);
|
|
1460
1511
|
}
|
|
1461
|
-
windowStartedAt = now;
|
|
1462
|
-
windowCount = 0;
|
|
1463
|
-
windowDroppedCount = 0;
|
|
1512
|
+
window.windowStartedAt = now;
|
|
1513
|
+
window.windowCount = 0;
|
|
1514
|
+
window.windowDroppedCount = 0;
|
|
1464
1515
|
}
|
|
1465
|
-
if (windowCount >= RATE_LIMIT_MAX_EVENTS) {
|
|
1466
|
-
windowDroppedCount++;
|
|
1467
|
-
if (windowDroppedCount === 1) {
|
|
1516
|
+
if (window.windowCount >= RATE_LIMIT_MAX_EVENTS) {
|
|
1517
|
+
window.windowDroppedCount++;
|
|
1518
|
+
if (window.windowDroppedCount === 1) {
|
|
1468
1519
|
console.error(
|
|
1469
|
-
`[runner-activity-telemetry] rate cap reached (${RATE_LIMIT_MAX_EVENTS} per ${RATE_LIMIT_WINDOW_MS / 1e3}s) \u2014 dropping further entries this window`
|
|
1520
|
+
`[runner-activity-telemetry] rate cap reached (${RATE_LIMIT_MAX_EVENTS} per ${RATE_LIMIT_WINDOW_MS / 1e3}s) \u2014 dropping further entries this window for source "${source}"`
|
|
1470
1521
|
);
|
|
1471
1522
|
}
|
|
1472
1523
|
return false;
|
|
1473
1524
|
}
|
|
1474
|
-
windowCount++;
|
|
1525
|
+
window.windowCount++;
|
|
1475
1526
|
return true;
|
|
1476
1527
|
}
|
|
1477
1528
|
function forwardRunnerActivity(entry, context) {
|
|
1478
1529
|
try {
|
|
1479
1530
|
if (!FORWARDED_LEVELS.has(entry.level)) return;
|
|
1480
1531
|
if (!context.agentId || !context.authHeader) return;
|
|
1481
|
-
|
|
1532
|
+
const source = entry.source ?? "cli.run";
|
|
1533
|
+
if (!admitUnderRateLimit(source, Date.now())) return;
|
|
1482
1534
|
const rawMessage = entry.error ?? entry.message ?? "";
|
|
1483
1535
|
const message = truncate(redact(rawMessage));
|
|
1484
1536
|
logEvent(TelemetryEventTypes.RUNNER_ACTIVITY, {
|
|
1485
1537
|
severity: SEVERITY_BY_LEVEL[entry.level],
|
|
1486
1538
|
message,
|
|
1487
|
-
metadata: { ...sanitiseMetadata(entry.metadata), source
|
|
1539
|
+
metadata: { ...sanitiseMetadata(entry.metadata), source },
|
|
1488
1540
|
agentId: context.agentId
|
|
1489
1541
|
});
|
|
1490
1542
|
} catch (err) {
|
|
@@ -1495,8 +1547,8 @@ function forwardRunnerActivity(entry, context) {
|
|
|
1495
1547
|
}
|
|
1496
1548
|
|
|
1497
1549
|
// src/lib/opencode/session-db-recovery-report.ts
|
|
1498
|
-
import { readFileSync as readFileSync2, unlinkSync } from "fs";
|
|
1499
|
-
import { join as join2 } from "path";
|
|
1550
|
+
import { readFileSync as readFileSync2, unlinkSync } from "node:fs";
|
|
1551
|
+
import { join as join2 } from "node:path";
|
|
1500
1552
|
function sessionDbRecoveryReportPath(homeDir, env) {
|
|
1501
1553
|
const override = env.EVIDENT_SESSION_DB_RECOVERY_REPORT?.trim();
|
|
1502
1554
|
return override || join2(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
|
|
@@ -1525,7 +1577,14 @@ function drainSessionDbRecoveryReport({
|
|
|
1525
1577
|
skippedLines++;
|
|
1526
1578
|
return [];
|
|
1527
1579
|
}
|
|
1528
|
-
return [
|
|
1580
|
+
return [
|
|
1581
|
+
{
|
|
1582
|
+
...value,
|
|
1583
|
+
provenance_reason: value.provenance_reason ?? null,
|
|
1584
|
+
provenance_migration_delta: value.provenance_migration_delta ?? null,
|
|
1585
|
+
replication_suspended: value.replication_suspended ?? false
|
|
1586
|
+
}
|
|
1587
|
+
];
|
|
1529
1588
|
} catch (error2) {
|
|
1530
1589
|
skippedLines++;
|
|
1531
1590
|
console.error(
|
|
@@ -1628,6 +1687,12 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1628
1687
|
metadata: withoutContractFields(record),
|
|
1629
1688
|
message: `This runner did not come online because its damaged session database could not be safely separated from its active backup or proven removed. Backed-up session history remains readable at ${record.quarantine_destination ?? "its original location or the quarantine destination named in the boot logs"}; any local session-database files that remain were left in place and nothing opened or wrote them. See the runner boot logs for SESSION-DB-LOCAL-DISCARD-FAILED details.`
|
|
1630
1689
|
};
|
|
1690
|
+
case "schema_provenance_mismatch":
|
|
1691
|
+
return {
|
|
1692
|
+
level,
|
|
1693
|
+
metadata: withoutContractFields(record),
|
|
1694
|
+
message: `Session database schema provenance mismatch for ${record.dbPath ?? record.db_path ?? "unknown"}: recorded version=${record.recorded_version ?? "unknown"}, current version=${record.current_version ?? "unknown"}, reason=${record.provenance_reason ?? "unknown"}, migration delta=${record.provenance_migration_delta ?? "unknown"}. Inspect the session database and runner backup before continuing.`
|
|
1695
|
+
};
|
|
1631
1696
|
default:
|
|
1632
1697
|
return null;
|
|
1633
1698
|
}
|
|
@@ -1642,7 +1707,8 @@ var OUTCOMES = /* @__PURE__ */ new Set([
|
|
|
1642
1707
|
"fresh_session_db",
|
|
1643
1708
|
"history_rolled_back",
|
|
1644
1709
|
"restore_misconfigured",
|
|
1645
|
-
"session_db_boot_refused"
|
|
1710
|
+
"session_db_boot_refused",
|
|
1711
|
+
"schema_provenance_mismatch"
|
|
1646
1712
|
]);
|
|
1647
1713
|
var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
|
|
1648
1714
|
var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
|
|
@@ -1660,7 +1726,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
|
|
|
1660
1726
|
function isSessionDbRecoveryRecord(value) {
|
|
1661
1727
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1662
1728
|
const record = value;
|
|
1663
|
-
return record.v === 1 && record.event === "session_db_recovery" && typeof record.at === "string" && STAGES.has(record.stage) && OUTCOMES.has(record.outcome) && SEVERITIES.has(record.severity) && typeof record.reason === "string" && (record.replication_suspended === void 0 || typeof record.replication_suspended === "boolean") && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
|
|
1729
|
+
return record.v === 1 && record.event === "session_db_recovery" && typeof record.at === "string" && STAGES.has(record.stage) && OUTCOMES.has(record.outcome) && SEVERITIES.has(record.severity) && typeof record.reason === "string" && (record.replication_suspended === void 0 || typeof record.replication_suspended === "boolean") && (record.provenance_reason === void 0 || record.provenance_reason === null || typeof record.provenance_reason === "string") && (record.provenance_migration_delta === void 0 || record.provenance_migration_delta === null || Number.isInteger(record.provenance_migration_delta)) && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
|
|
1664
1730
|
(field) => record[field] === null || typeof record[field] === "string"
|
|
1665
1731
|
);
|
|
1666
1732
|
}
|
|
@@ -1695,13 +1761,13 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
1695
1761
|
}
|
|
1696
1762
|
|
|
1697
1763
|
// src/lib/opencode/session-db-boot.ts
|
|
1698
|
-
import { spawn as spawn2 } from "child_process";
|
|
1699
|
-
import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "fs";
|
|
1700
|
-
import { homedir as homedir2 } from "os";
|
|
1701
|
-
import { dirname as dirname2, resolve as resolvePath } from "path";
|
|
1764
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
1765
|
+
import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
|
|
1766
|
+
import { homedir as homedir2 } from "node:os";
|
|
1767
|
+
import { dirname as dirname2, resolve as resolvePath } from "node:path";
|
|
1702
1768
|
|
|
1703
1769
|
// src/lib/runner-synchroniser.ts
|
|
1704
|
-
import { spawn } from "child_process";
|
|
1770
|
+
import { spawn } from "node:child_process";
|
|
1705
1771
|
function appendError(stderr, error2) {
|
|
1706
1772
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1707
1773
|
return stderr === "" ? message : `${stderr}
|
|
@@ -1714,10 +1780,14 @@ function runSynchroniser(args, opts) {
|
|
|
1714
1780
|
let stderr = "";
|
|
1715
1781
|
let settled = false;
|
|
1716
1782
|
const timer = {};
|
|
1783
|
+
let abortListener;
|
|
1784
|
+
let spawnListener;
|
|
1717
1785
|
const finish = (result) => {
|
|
1718
1786
|
if (settled) return;
|
|
1719
1787
|
settled = true;
|
|
1720
1788
|
if (timer.handle) clearTimeout(timer.handle);
|
|
1789
|
+
if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
|
|
1790
|
+
if (spawnListener) child.removeListener("spawn", spawnListener);
|
|
1721
1791
|
resolve4(result);
|
|
1722
1792
|
};
|
|
1723
1793
|
try {
|
|
@@ -1743,6 +1813,25 @@ function runSynchroniser(args, opts) {
|
|
|
1743
1813
|
child.once("close", (code) => {
|
|
1744
1814
|
finish({ code, stdout, stderr, timedOut: false });
|
|
1745
1815
|
});
|
|
1816
|
+
if (opts.signal) {
|
|
1817
|
+
const killChild = () => {
|
|
1818
|
+
if (child.pid === void 0) {
|
|
1819
|
+
if (!spawnListener) {
|
|
1820
|
+
spawnListener = killChild;
|
|
1821
|
+
child.once("spawn", spawnListener);
|
|
1822
|
+
}
|
|
1823
|
+
return;
|
|
1824
|
+
}
|
|
1825
|
+
child.kill("SIGKILL");
|
|
1826
|
+
};
|
|
1827
|
+
abortListener = killChild;
|
|
1828
|
+
if (opts.signal.aborted) {
|
|
1829
|
+
abortListener();
|
|
1830
|
+
} else {
|
|
1831
|
+
opts.signal.addEventListener("abort", abortListener, { once: true });
|
|
1832
|
+
if (opts.signal.aborted) abortListener();
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1746
1835
|
timer.handle = setTimeout(
|
|
1747
1836
|
() => {
|
|
1748
1837
|
child.kill("SIGKILL");
|
|
@@ -1780,6 +1869,8 @@ function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
|
|
|
1780
1869
|
quarantined_bytes: null,
|
|
1781
1870
|
verified_restore_point: null,
|
|
1782
1871
|
restore_points_tried: null,
|
|
1872
|
+
provenance_reason: null,
|
|
1873
|
+
provenance_migration_delta: null,
|
|
1783
1874
|
replication_suspended: stage === "restore"
|
|
1784
1875
|
});
|
|
1785
1876
|
}
|
|
@@ -2200,6 +2291,131 @@ async function restoreAndVerifySessionDb(options) {
|
|
|
2200
2291
|
return { verifyFatal: await verifySessionDb(options, configPath, env) };
|
|
2201
2292
|
}
|
|
2202
2293
|
|
|
2294
|
+
// src/lib/opencode/session-db-provenance.ts
|
|
2295
|
+
import { createRequire } from "node:module";
|
|
2296
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "node:fs";
|
|
2297
|
+
import { dirname as dirname3, join as join3 } from "node:path";
|
|
2298
|
+
var require2 = createRequire(import.meta.url);
|
|
2299
|
+
function readSessionDbMigrationIds(dbPath) {
|
|
2300
|
+
let db;
|
|
2301
|
+
try {
|
|
2302
|
+
const { DatabaseSync } = require2("node:sqlite");
|
|
2303
|
+
db = new DatabaseSync(dbPath, { readOnly: true });
|
|
2304
|
+
const columns = db.prepare("PRAGMA table_info(migration)").all();
|
|
2305
|
+
const hasExpectedShape = columns.length === 2 && columns.some(
|
|
2306
|
+
(column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
|
|
2307
|
+
) && columns.some(
|
|
2308
|
+
(column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
|
|
2309
|
+
);
|
|
2310
|
+
if (!hasExpectedShape) {
|
|
2311
|
+
console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
|
|
2312
|
+
return null;
|
|
2313
|
+
}
|
|
2314
|
+
const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
|
|
2315
|
+
if (rows.some((row) => typeof row.id !== "string")) return null;
|
|
2316
|
+
return rows.map((row) => row.id);
|
|
2317
|
+
} catch (error2) {
|
|
2318
|
+
console.warn(
|
|
2319
|
+
`[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2320
|
+
);
|
|
2321
|
+
return null;
|
|
2322
|
+
} finally {
|
|
2323
|
+
try {
|
|
2324
|
+
db?.close();
|
|
2325
|
+
} catch (error2) {
|
|
2326
|
+
console.warn(
|
|
2327
|
+
`[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2328
|
+
);
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
function sessionDbProvenanceStatePath(homeDir, env) {
|
|
2333
|
+
const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
|
|
2334
|
+
return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
|
|
2335
|
+
}
|
|
2336
|
+
function loadSessionDbProvenanceState(path) {
|
|
2337
|
+
let value;
|
|
2338
|
+
try {
|
|
2339
|
+
value = JSON.parse(readFileSync3(path, "utf8"));
|
|
2340
|
+
} catch (error2) {
|
|
2341
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
|
|
2342
|
+
console.error(
|
|
2343
|
+
`[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2344
|
+
);
|
|
2345
|
+
return {};
|
|
2346
|
+
}
|
|
2347
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2348
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2349
|
+
return {};
|
|
2350
|
+
}
|
|
2351
|
+
const state = {};
|
|
2352
|
+
for (const [dbPath, record] of Object.entries(value)) {
|
|
2353
|
+
if (!isSessionDbProvenanceRecord(record)) {
|
|
2354
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2355
|
+
return {};
|
|
2356
|
+
}
|
|
2357
|
+
state[dbPath] = record;
|
|
2358
|
+
}
|
|
2359
|
+
return state;
|
|
2360
|
+
}
|
|
2361
|
+
function saveSessionDbProvenanceState(path, state) {
|
|
2362
|
+
try {
|
|
2363
|
+
mkdirSync2(dirname3(path), { recursive: true });
|
|
2364
|
+
writeFileSync2(path, `${JSON.stringify(state, null, 2)}
|
|
2365
|
+
`, "utf8");
|
|
2366
|
+
} catch (error2) {
|
|
2367
|
+
console.error(
|
|
2368
|
+
`[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2369
|
+
);
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
function evaluateSessionDbProvenance(input) {
|
|
2373
|
+
const { currentVersion, currentIds, previous } = input;
|
|
2374
|
+
if (!previous) return { anomaly: false, reason: null };
|
|
2375
|
+
const current = new Set(currentIds);
|
|
2376
|
+
const prior = new Set(previous.migrationIds);
|
|
2377
|
+
for (const id of prior) {
|
|
2378
|
+
if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
|
|
2379
|
+
}
|
|
2380
|
+
if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
|
|
2381
|
+
return { anomaly: true, reason: "foreign-version-migrations" };
|
|
2382
|
+
}
|
|
2383
|
+
return { anomaly: false, reason: null };
|
|
2384
|
+
}
|
|
2385
|
+
function checkSessionDbProvenance(input) {
|
|
2386
|
+
const { dbPath, currentVersion, homeDir, env } = input;
|
|
2387
|
+
const path = sessionDbProvenanceStatePath(homeDir, env);
|
|
2388
|
+
const state = loadSessionDbProvenanceState(path);
|
|
2389
|
+
const previous = state[dbPath];
|
|
2390
|
+
const currentIds = readSessionDbMigrationIds(dbPath);
|
|
2391
|
+
if (currentIds === null) {
|
|
2392
|
+
return {
|
|
2393
|
+
anomaly: false,
|
|
2394
|
+
reason: null,
|
|
2395
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2396
|
+
migrationDelta: null
|
|
2397
|
+
};
|
|
2398
|
+
}
|
|
2399
|
+
const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
|
|
2400
|
+
const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
|
|
2401
|
+
state[dbPath] = {
|
|
2402
|
+
opencodeVersion: currentVersion,
|
|
2403
|
+
migrationIds: currentIds,
|
|
2404
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2405
|
+
};
|
|
2406
|
+
saveSessionDbProvenanceState(path, state);
|
|
2407
|
+
return {
|
|
2408
|
+
...decision,
|
|
2409
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2410
|
+
migrationDelta
|
|
2411
|
+
};
|
|
2412
|
+
}
|
|
2413
|
+
function isSessionDbProvenanceRecord(value) {
|
|
2414
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
2415
|
+
const record = value;
|
|
2416
|
+
return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2203
2419
|
// src/lib/opencode/opencode-version-gate.ts
|
|
2204
2420
|
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
2205
2421
|
function isQueueValidatedVersion(version2) {
|
|
@@ -2272,6 +2488,17 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
|
2272
2488
|
|
|
2273
2489
|
// src/lib/opencode/process.ts
|
|
2274
2490
|
var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
|
|
2491
|
+
var VALID_OPENCODE_LOG_LEVELS = /* @__PURE__ */ new Set(["DEBUG", "INFO", "WARN", "ERROR"]);
|
|
2492
|
+
function resolveOpenCodeLogLevel(env) {
|
|
2493
|
+
const raw = env.OPENCODE_LOG_LEVEL;
|
|
2494
|
+
if (!raw) return "INFO";
|
|
2495
|
+
const upper = raw.toUpperCase();
|
|
2496
|
+
if (VALID_OPENCODE_LOG_LEVELS.has(upper)) return upper;
|
|
2497
|
+
console.warn(
|
|
2498
|
+
`startOpenCode: ignoring invalid OPENCODE_LOG_LEVEL "${raw}" (expected DEBUG|INFO|WARN|ERROR) \u2014 using INFO`
|
|
2499
|
+
);
|
|
2500
|
+
return "INFO";
|
|
2501
|
+
}
|
|
2275
2502
|
function getProcessCwd(pid) {
|
|
2276
2503
|
const platform = process.platform;
|
|
2277
2504
|
try {
|
|
@@ -2320,14 +2547,14 @@ function findAvailablePort(startPort, maxAttempts = 10) {
|
|
|
2320
2547
|
}
|
|
2321
2548
|
return null;
|
|
2322
2549
|
}
|
|
2323
|
-
function
|
|
2550
|
+
function findProcessesByPattern(pgrepPattern, psPattern) {
|
|
2324
2551
|
const instances = [];
|
|
2325
2552
|
try {
|
|
2326
2553
|
const platform = process.platform;
|
|
2327
2554
|
if (platform === "darwin" || platform === "linux") {
|
|
2328
2555
|
let pids = [];
|
|
2329
2556
|
try {
|
|
2330
|
-
const pgrepOutput = execSync(
|
|
2557
|
+
const pgrepOutput = execSync(`pgrep -f "${pgrepPattern}"`, {
|
|
2331
2558
|
encoding: "utf-8",
|
|
2332
2559
|
stdio: ["pipe", "pipe", "pipe"]
|
|
2333
2560
|
}).trim();
|
|
@@ -2336,7 +2563,7 @@ function findOpenCodeProcesses() {
|
|
|
2336
2563
|
}
|
|
2337
2564
|
} catch {
|
|
2338
2565
|
try {
|
|
2339
|
-
const psOutput = execSync(
|
|
2566
|
+
const psOutput = execSync(`ps aux | grep -E "${psPattern}" | grep -v grep`, {
|
|
2340
2567
|
encoding: "utf-8",
|
|
2341
2568
|
stdio: ["pipe", "pipe", "pipe"]
|
|
2342
2569
|
}).trim();
|
|
@@ -2382,6 +2609,9 @@ function findOpenCodeProcesses() {
|
|
|
2382
2609
|
}
|
|
2383
2610
|
return instances;
|
|
2384
2611
|
}
|
|
2612
|
+
function findOpenCodeProcesses() {
|
|
2613
|
+
return findProcessesByPattern("opencode serve|opencode-serve", "opencode (serve|--port)");
|
|
2614
|
+
}
|
|
2385
2615
|
async function scanPortsForOpenCode() {
|
|
2386
2616
|
const instances = [];
|
|
2387
2617
|
const checks = OPENCODE_PORT_RANGE.map(async (port) => {
|
|
@@ -2428,7 +2658,7 @@ async function findHealthyOpenCodeInstances() {
|
|
|
2428
2658
|
}
|
|
2429
2659
|
async function startOpenCode(port, options = {}) {
|
|
2430
2660
|
let command = "opencode";
|
|
2431
|
-
const printLogs = options.inheritStdio ? ["--print-logs"] : [];
|
|
2661
|
+
const printLogs = options.inheritStdio ? ["--print-logs", "--log-level", resolveOpenCodeLogLevel(process.env)] : [];
|
|
2432
2662
|
let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
|
|
2433
2663
|
try {
|
|
2434
2664
|
execSync("which opencode", { stdio: "ignore" });
|
|
@@ -2485,6 +2715,19 @@ function isOpenCodeInstalled() {
|
|
|
2485
2715
|
return false;
|
|
2486
2716
|
}
|
|
2487
2717
|
}
|
|
2718
|
+
function isOpenCode2Installed() {
|
|
2719
|
+
try {
|
|
2720
|
+
const platform = process.platform;
|
|
2721
|
+
if (platform === "win32") {
|
|
2722
|
+
execSync2("where opencode2", { stdio: "ignore" });
|
|
2723
|
+
} else {
|
|
2724
|
+
execSync2("which opencode2", { stdio: "ignore" });
|
|
2725
|
+
}
|
|
2726
|
+
return true;
|
|
2727
|
+
} catch {
|
|
2728
|
+
return false;
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2488
2731
|
async function promptOpenCodeInstall(interactive) {
|
|
2489
2732
|
if (!interactive) {
|
|
2490
2733
|
console.log(
|
|
@@ -2494,7 +2737,11 @@ async function promptOpenCodeInstall(interactive) {
|
|
|
2494
2737
|
install_url: OPENCODE_INSTALL_URL,
|
|
2495
2738
|
install_commands: {
|
|
2496
2739
|
npm: "npm install -g opencode-ai",
|
|
2497
|
-
curl: "curl -fsSL https://opencode.ai/install.sh | sh"
|
|
2740
|
+
curl: "curl -fsSL https://opencode.ai/install.sh | sh",
|
|
2741
|
+
v2: {
|
|
2742
|
+
npm: "npm install -g @opencode-ai/cli@beta",
|
|
2743
|
+
curl: "curl -fsSL https://opencode.ai/v2/install | bash"
|
|
2744
|
+
}
|
|
2498
2745
|
}
|
|
2499
2746
|
})
|
|
2500
2747
|
);
|
|
@@ -2978,6 +3225,44 @@ function findLastAssistantReplyFor(messages, userMessageId) {
|
|
|
2978
3225
|
}
|
|
2979
3226
|
return lastOk ?? last;
|
|
2980
3227
|
}
|
|
3228
|
+
function collectSubagentSessions(messages, userMessageId) {
|
|
3229
|
+
if (!messages || messages.length === 0) return [];
|
|
3230
|
+
const byParent = messages.filter(
|
|
3231
|
+
(message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
|
|
3232
|
+
);
|
|
3233
|
+
const assistants = byParent.length > 0 ? byParent : [];
|
|
3234
|
+
if (assistants.length === 0) {
|
|
3235
|
+
const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
|
|
3236
|
+
if (userIndex === -1) return [];
|
|
3237
|
+
for (let i = userIndex + 1; i < messages.length; i++) {
|
|
3238
|
+
const message = messages[i];
|
|
3239
|
+
if (roleOf(message) === "user") break;
|
|
3240
|
+
if (roleOf(message) === "assistant") assistants.push(message);
|
|
3241
|
+
}
|
|
3242
|
+
}
|
|
3243
|
+
const refs = [];
|
|
3244
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3245
|
+
for (const message of assistants) {
|
|
3246
|
+
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
3247
|
+
for (const part of parts) {
|
|
3248
|
+
if (!part || typeof part !== "object" || part.type !== "tool" || part.tool !== "task")
|
|
3249
|
+
continue;
|
|
3250
|
+
const state = part.state;
|
|
3251
|
+
if (!state || typeof state !== "object") continue;
|
|
3252
|
+
const metadata = state.metadata;
|
|
3253
|
+
if (!metadata || typeof metadata !== "object") continue;
|
|
3254
|
+
const sessionId = metadata.sessionId;
|
|
3255
|
+
if (typeof sessionId !== "string" || sessionId.length === 0 || seen.has(sessionId)) continue;
|
|
3256
|
+
seen.add(sessionId);
|
|
3257
|
+
const start = state.time?.start;
|
|
3258
|
+
refs.push({
|
|
3259
|
+
sessionId,
|
|
3260
|
+
startedAtMs: typeof start === "number" && Number.isFinite(start) ? start : null
|
|
3261
|
+
});
|
|
3262
|
+
}
|
|
3263
|
+
}
|
|
3264
|
+
return refs;
|
|
3265
|
+
}
|
|
2981
3266
|
function messageUsage(messages, userMessageId) {
|
|
2982
3267
|
if (!messages || messages.length === 0) return null;
|
|
2983
3268
|
const byParentAll = messages.filter(
|
|
@@ -3106,8 +3391,7 @@ function isAbortedTerminalReply(messages, userMessageId) {
|
|
|
3106
3391
|
}
|
|
3107
3392
|
return false;
|
|
3108
3393
|
}
|
|
3109
|
-
function
|
|
3110
|
-
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
3394
|
+
function classifyReplyAuthError(reply) {
|
|
3111
3395
|
const error2 = errorOf(reply);
|
|
3112
3396
|
if (error2 == null || typeof error2 !== "object") return null;
|
|
3113
3397
|
const e = error2;
|
|
@@ -3132,6 +3416,32 @@ function messageFailure(messages, userMessageId) {
|
|
|
3132
3416
|
}
|
|
3133
3417
|
return null;
|
|
3134
3418
|
}
|
|
3419
|
+
function messageFailure(messages, userMessageId) {
|
|
3420
|
+
return classifyReplyAuthError(findLastAssistantReplyFor(messages, userMessageId));
|
|
3421
|
+
}
|
|
3422
|
+
function findLatestSubagentAuthOutcome(messages, sinceMs) {
|
|
3423
|
+
if (!messages || messages.length === 0) return null;
|
|
3424
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
3425
|
+
const message = messages[i];
|
|
3426
|
+
if (roleOf(message) !== "assistant") continue;
|
|
3427
|
+
const created = createdOf(message);
|
|
3428
|
+
if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
|
|
3429
|
+
const failure = classifyReplyAuthError(message);
|
|
3430
|
+
if (failure) {
|
|
3431
|
+
if (!failure.providerId) return null;
|
|
3432
|
+
return { providerId: failure.providerId, outcome: "failed", failure };
|
|
3433
|
+
}
|
|
3434
|
+
const providerId = message.info?.providerID;
|
|
3435
|
+
if (errorOf(message) == null && typeof providerId === "string" && providerId.length > 0) {
|
|
3436
|
+
return { providerId, outcome: "succeeded" };
|
|
3437
|
+
}
|
|
3438
|
+
return null;
|
|
3439
|
+
}
|
|
3440
|
+
return null;
|
|
3441
|
+
}
|
|
3442
|
+
function findSubagentAuthOutcome(messages, sinceMs) {
|
|
3443
|
+
return findLatestSubagentAuthOutcome(messages, sinceMs);
|
|
3444
|
+
}
|
|
3135
3445
|
function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProviderId, replyModelId = null) {
|
|
3136
3446
|
if (classified != null) return classified;
|
|
3137
3447
|
if (hasConfiguredProvider !== false) return null;
|
|
@@ -3179,6 +3489,24 @@ async function hasAnyConfiguredProvider(port) {
|
|
|
3179
3489
|
return null;
|
|
3180
3490
|
}
|
|
3181
3491
|
}
|
|
3492
|
+
async function reloadProviderCache(port) {
|
|
3493
|
+
try {
|
|
3494
|
+
const res = await timedFetch(`${opencodeBase(port)}/config`, {
|
|
3495
|
+
method: "PATCH",
|
|
3496
|
+
headers: { "Content-Type": "application/json" },
|
|
3497
|
+
body: JSON.stringify({})
|
|
3498
|
+
});
|
|
3499
|
+
if (!res.ok) {
|
|
3500
|
+
console.error(
|
|
3501
|
+
`[reloadProviderCache] PATCH /config returned HTTP ${res.status} (port ${port})`
|
|
3502
|
+
);
|
|
3503
|
+
}
|
|
3504
|
+
} catch (err) {
|
|
3505
|
+
console.error(
|
|
3506
|
+
`[reloadProviderCache] PATCH /config failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
|
|
3507
|
+
);
|
|
3508
|
+
}
|
|
3509
|
+
}
|
|
3182
3510
|
|
|
3183
3511
|
// src/lib/opencode/session-cleanup.ts
|
|
3184
3512
|
var DURATION_UNIT_MS = {
|
|
@@ -3285,11 +3613,11 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
|
3285
3613
|
}
|
|
3286
3614
|
|
|
3287
3615
|
// src/lib/opencode/session-db-size.ts
|
|
3288
|
-
import { statSync as statSync3 } from "fs";
|
|
3289
|
-
import { join as
|
|
3616
|
+
import { statSync as statSync3 } from "node:fs";
|
|
3617
|
+
import { join as join4 } from "node:path";
|
|
3290
3618
|
var LARGE_DB_THRESHOLD_BYTES = 268435456;
|
|
3291
3619
|
function statSessionDbBytes(homeDir) {
|
|
3292
|
-
const dbPath =
|
|
3620
|
+
const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
|
|
3293
3621
|
try {
|
|
3294
3622
|
return statSync3(dbPath).size;
|
|
3295
3623
|
} catch (err) {
|
|
@@ -3316,12 +3644,99 @@ function buildSessionStoreSizeWarning(input) {
|
|
|
3316
3644
|
return null;
|
|
3317
3645
|
}
|
|
3318
3646
|
|
|
3647
|
+
// src/lib/opencode/log-tail.ts
|
|
3648
|
+
import { statSync as statSync4 } from "node:fs";
|
|
3649
|
+
import { homedir as homedir3 } from "node:os";
|
|
3650
|
+
import { join as join5 } from "node:path";
|
|
3651
|
+
import { open as open2, stat } from "node:fs/promises";
|
|
3652
|
+
var DEFAULT_POLL_INTERVAL_MS = 1e3;
|
|
3653
|
+
function resolveOpenCodeLogPath(homeDir = homedir3(), env = process.env) {
|
|
3654
|
+
const dataDir = env.XDG_DATA_HOME || join5(homeDir, ".local", "share");
|
|
3655
|
+
return join5(dataDir, "opencode", "log", "opencode.log");
|
|
3656
|
+
}
|
|
3657
|
+
function isEnoent(error2) {
|
|
3658
|
+
return error2?.code === "ENOENT";
|
|
3659
|
+
}
|
|
3660
|
+
function reportFailure(operation, logPath, error2) {
|
|
3661
|
+
console.error(
|
|
3662
|
+
`[opencode-log-tail] ${operation} failed for ${logPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
3663
|
+
);
|
|
3664
|
+
}
|
|
3665
|
+
function tailOpenCodeLogFile(logPath, onChunk, opts = {}) {
|
|
3666
|
+
const pollIntervalMs = opts.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
|
|
3667
|
+
let offset = 0;
|
|
3668
|
+
let inode = null;
|
|
3669
|
+
let baselineReady = true;
|
|
3670
|
+
try {
|
|
3671
|
+
const initial = statSync4(logPath);
|
|
3672
|
+
offset = initial.size;
|
|
3673
|
+
inode = initial.ino;
|
|
3674
|
+
} catch (error2) {
|
|
3675
|
+
if (!isEnoent(error2)) {
|
|
3676
|
+
reportFailure("initial stat", logPath, error2);
|
|
3677
|
+
baselineReady = false;
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
let polling = false;
|
|
3681
|
+
let stopped = false;
|
|
3682
|
+
const poll = async () => {
|
|
3683
|
+
if (polling || stopped) return;
|
|
3684
|
+
polling = true;
|
|
3685
|
+
try {
|
|
3686
|
+
let current;
|
|
3687
|
+
try {
|
|
3688
|
+
current = await stat(logPath);
|
|
3689
|
+
} catch (error2) {
|
|
3690
|
+
if (!isEnoent(error2)) reportFailure("stat", logPath, error2);
|
|
3691
|
+
return;
|
|
3692
|
+
}
|
|
3693
|
+
if (!baselineReady) {
|
|
3694
|
+
offset = current.size;
|
|
3695
|
+
inode = current.ino;
|
|
3696
|
+
baselineReady = true;
|
|
3697
|
+
return;
|
|
3698
|
+
}
|
|
3699
|
+
if (inode !== null && current.ino !== inode || current.size < offset) {
|
|
3700
|
+
offset = 0;
|
|
3701
|
+
}
|
|
3702
|
+
inode = current.ino;
|
|
3703
|
+
if (current.size === offset) return;
|
|
3704
|
+
const length = current.size - offset;
|
|
3705
|
+
const fh = await open2(logPath, "r");
|
|
3706
|
+
try {
|
|
3707
|
+
const buf = Buffer.alloc(length);
|
|
3708
|
+
const { bytesRead } = await fh.read(buf, 0, length, offset);
|
|
3709
|
+
offset += bytesRead;
|
|
3710
|
+
if (bytesRead > 0) onChunk(buf.subarray(0, bytesRead));
|
|
3711
|
+
} finally {
|
|
3712
|
+
await fh.close();
|
|
3713
|
+
}
|
|
3714
|
+
} catch (error2) {
|
|
3715
|
+
if (!isEnoent(error2)) reportFailure("poll", logPath, error2);
|
|
3716
|
+
} finally {
|
|
3717
|
+
polling = false;
|
|
3718
|
+
}
|
|
3719
|
+
};
|
|
3720
|
+
const interval = setInterval(() => void poll(), pollIntervalMs);
|
|
3721
|
+
void poll();
|
|
3722
|
+
return {
|
|
3723
|
+
stop: () => {
|
|
3724
|
+
stopped = true;
|
|
3725
|
+
clearInterval(interval);
|
|
3726
|
+
}
|
|
3727
|
+
};
|
|
3728
|
+
}
|
|
3729
|
+
|
|
3319
3730
|
// src/lib/opencode/session-db-reclaim.ts
|
|
3320
|
-
import { statSync as
|
|
3321
|
-
import { dirname as
|
|
3731
|
+
import { statSync as statSync5, statfsSync } from "node:fs";
|
|
3732
|
+
import { dirname as dirname4 } from "node:path";
|
|
3733
|
+
function errorMessage(error2) {
|
|
3734
|
+
if (!(error2 instanceof Error)) return String(error2);
|
|
3735
|
+
return error2.cause instanceof Error ? error2.cause.message : error2.message;
|
|
3736
|
+
}
|
|
3322
3737
|
function insufficientSpaceReason(dbPath, requiredBytes) {
|
|
3323
3738
|
try {
|
|
3324
|
-
const fsStats = statfsSync(
|
|
3739
|
+
const fsStats = statfsSync(dirname4(dbPath));
|
|
3325
3740
|
const availableBytes = fsStats.bavail * fsStats.bsize;
|
|
3326
3741
|
if (availableBytes < requiredBytes) {
|
|
3327
3742
|
return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
|
|
@@ -3344,17 +3759,17 @@ async function probeReclaimAvailability(input) {
|
|
|
3344
3759
|
const { dbPath, requiredBytes } = input;
|
|
3345
3760
|
let sqlite;
|
|
3346
3761
|
try {
|
|
3347
|
-
sqlite = await import("sqlite");
|
|
3762
|
+
sqlite = await import("node:sqlite");
|
|
3348
3763
|
} catch (err) {
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
return "sqlite-unavailable";
|
|
3764
|
+
const detail = `Node ${process.version}: ${errorMessage(err)}`;
|
|
3765
|
+
console.warn(`[probeReclaimAvailability] node:sqlite unavailable for ${dbPath}: ` + detail);
|
|
3766
|
+
return { reason: "sqlite-unavailable", detail };
|
|
3353
3767
|
}
|
|
3354
3768
|
let autoVacuum = null;
|
|
3355
3769
|
try {
|
|
3356
3770
|
const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
|
|
3357
3771
|
try {
|
|
3772
|
+
db.exec("PRAGMA busy_timeout=5000");
|
|
3358
3773
|
autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
|
|
3359
3774
|
} finally {
|
|
3360
3775
|
db.close();
|
|
@@ -3365,23 +3780,25 @@ async function probeReclaimAvailability(input) {
|
|
|
3365
3780
|
);
|
|
3366
3781
|
}
|
|
3367
3782
|
if (autoVacuum !== 0) return null;
|
|
3368
|
-
return insufficientSpaceReason(dbPath, requiredBytes) !== null ? "insufficient-disk-space" : null;
|
|
3783
|
+
return insufficientSpaceReason(dbPath, requiredBytes) !== null ? { reason: "insufficient-disk-space" } : null;
|
|
3369
3784
|
}
|
|
3370
3785
|
async function reclaimSessionDbSpace(input) {
|
|
3371
3786
|
const { dbPath, maxPages, allowFullVacuum = true } = input;
|
|
3372
3787
|
let sqlite;
|
|
3373
3788
|
try {
|
|
3374
|
-
sqlite = await import("sqlite");
|
|
3789
|
+
sqlite = await import("node:sqlite");
|
|
3375
3790
|
} catch (err) {
|
|
3791
|
+
const detail = `Node ${process.version}: ${errorMessage(err)}`;
|
|
3376
3792
|
console.warn(
|
|
3377
|
-
`[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${
|
|
3793
|
+
`[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${detail}`
|
|
3378
3794
|
);
|
|
3379
|
-
return { ok: false, skipped: "sqlite-unavailable" };
|
|
3795
|
+
return { ok: false, skipped: "sqlite-unavailable", detail };
|
|
3380
3796
|
}
|
|
3381
3797
|
const { DatabaseSync } = sqlite;
|
|
3382
3798
|
let db;
|
|
3383
3799
|
try {
|
|
3384
3800
|
db = new DatabaseSync(dbPath);
|
|
3801
|
+
db.exec("PRAGMA busy_timeout=5000");
|
|
3385
3802
|
const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
|
|
3386
3803
|
if (autoVacuum === 0) {
|
|
3387
3804
|
if (!allowFullVacuum) {
|
|
@@ -3390,7 +3807,7 @@ async function reclaimSessionDbSpace(input) {
|
|
|
3390
3807
|
);
|
|
3391
3808
|
return { ok: false, skipped: "full-vacuum-blocked" };
|
|
3392
3809
|
}
|
|
3393
|
-
const fileBytesForGuard =
|
|
3810
|
+
const fileBytesForGuard = statSync5(dbPath).size;
|
|
3394
3811
|
const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
|
|
3395
3812
|
if (skipReason !== null) {
|
|
3396
3813
|
console.warn(
|
|
@@ -3418,10 +3835,12 @@ async function reclaimSessionDbSpace(input) {
|
|
|
3418
3835
|
);
|
|
3419
3836
|
return { ok: false, skipped: "auto-vacuum-not-applicable" };
|
|
3420
3837
|
} catch (err) {
|
|
3421
|
-
console.error(
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3838
|
+
console.error(`[reclaimSessionDbSpace] reclaim failed for ${dbPath}: ` + errorMessage(err));
|
|
3839
|
+
return {
|
|
3840
|
+
ok: false,
|
|
3841
|
+
skipped: "reclaim-error",
|
|
3842
|
+
detail: errorMessage(err)
|
|
3843
|
+
};
|
|
3425
3844
|
} finally {
|
|
3426
3845
|
db?.close();
|
|
3427
3846
|
}
|
|
@@ -3462,7 +3881,6 @@ var StreamForwarder = class {
|
|
|
3462
3881
|
handleFrame(frame) {
|
|
3463
3882
|
switch (frame.type) {
|
|
3464
3883
|
case "open":
|
|
3465
|
-
this.callbacks.onOpen?.(frame.sid, frame.method, frame.path);
|
|
3466
3884
|
void this.handleOpen(frame);
|
|
3467
3885
|
break;
|
|
3468
3886
|
case "req_data":
|
|
@@ -3498,12 +3916,21 @@ var StreamForwarder = class {
|
|
|
3498
3916
|
const { sid, method, path, headers, has_body } = frame;
|
|
3499
3917
|
const correlationId = headers?.[CORRELATION_ID_HEADER];
|
|
3500
3918
|
const startedAt = Date.now();
|
|
3919
|
+
if (path !== TUNNEL_DRAIN_PING_PATH && path !== TUNNEL_USAGE_REARM_PING_PATH) {
|
|
3920
|
+
this.callbacks.onOpen?.(sid, method, path);
|
|
3921
|
+
}
|
|
3501
3922
|
if (path === TUNNEL_DRAIN_PING_PATH) {
|
|
3502
3923
|
this.callbacks.onDrainPing?.();
|
|
3503
3924
|
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
3504
3925
|
this.send({ type: "res_end", sid });
|
|
3505
3926
|
return;
|
|
3506
3927
|
}
|
|
3928
|
+
if (path === TUNNEL_USAGE_REARM_PING_PATH) {
|
|
3929
|
+
this.callbacks.onUsageRearmPing?.();
|
|
3930
|
+
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
3931
|
+
this.send({ type: "res_end", sid });
|
|
3932
|
+
return;
|
|
3933
|
+
}
|
|
3507
3934
|
if (process.env.DEBUG) {
|
|
3508
3935
|
log("debug", "agent_request", {
|
|
3509
3936
|
correlation_id: correlationId,
|
|
@@ -3648,7 +4075,8 @@ function connectTunnel(options) {
|
|
|
3648
4075
|
onResponse,
|
|
3649
4076
|
onInfo,
|
|
3650
4077
|
onWarning,
|
|
3651
|
-
onDrainPing
|
|
4078
|
+
onDrainPing,
|
|
4079
|
+
onUsageRearmPing
|
|
3652
4080
|
} = options;
|
|
3653
4081
|
const tunnelUrl = getTunnelUrlConfig();
|
|
3654
4082
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
@@ -3660,7 +4088,8 @@ function connectTunnel(options) {
|
|
|
3660
4088
|
});
|
|
3661
4089
|
const forwarder = new StreamForwarder(ws, port, {
|
|
3662
4090
|
onHead: () => onResponse?.(),
|
|
3663
|
-
onDrainPing: () => onDrainPing?.()
|
|
4091
|
+
onDrainPing: () => onDrainPing?.(),
|
|
4092
|
+
onUsageRearmPing: () => onUsageRearmPing?.()
|
|
3664
4093
|
});
|
|
3665
4094
|
const connectionTimeout = setTimeout(() => {
|
|
3666
4095
|
ws.close();
|
|
@@ -3703,8 +4132,8 @@ function connectTunnel(options) {
|
|
|
3703
4132
|
try {
|
|
3704
4133
|
message = JSON.parse(data.toString());
|
|
3705
4134
|
} catch (error2) {
|
|
3706
|
-
const
|
|
3707
|
-
onError?.(`Failed to handle message: ${
|
|
4135
|
+
const errorMessage3 = error2 instanceof Error ? error2.message : "Unknown error";
|
|
4136
|
+
onError?.(`Failed to handle message: ${errorMessage3}`);
|
|
3708
4137
|
return;
|
|
3709
4138
|
}
|
|
3710
4139
|
if (isStreamFrame(message)) {
|
|
@@ -3821,6 +4250,7 @@ var RunnerConnection = class {
|
|
|
3821
4250
|
onError: (error2) => events.onError?.(error2),
|
|
3822
4251
|
onResponse: () => events.onResponse?.(),
|
|
3823
4252
|
onDrainPing: () => events.onDrainPing?.(),
|
|
4253
|
+
onUsageRearmPing: () => events.onUsageRearmPing?.(),
|
|
3824
4254
|
onInfo: (message) => events.onInfo?.(message),
|
|
3825
4255
|
onWarning: (message) => events.onWarning?.(message)
|
|
3826
4256
|
});
|
|
@@ -3847,10 +4277,10 @@ var RunnerConnection = class {
|
|
|
3847
4277
|
};
|
|
3848
4278
|
|
|
3849
4279
|
// src/lib/tunnel/ready-marker.ts
|
|
3850
|
-
import { writeFileSync as
|
|
4280
|
+
import { writeFileSync as writeFileSync3 } from "node:fs";
|
|
3851
4281
|
function writeTunnelReadyMarker(path, agentId) {
|
|
3852
4282
|
try {
|
|
3853
|
-
|
|
4283
|
+
writeFileSync3(path, `${agentId}
|
|
3854
4284
|
`);
|
|
3855
4285
|
return { ok: true };
|
|
3856
4286
|
} catch (error2) {
|
|
@@ -3859,7 +4289,7 @@ function writeTunnelReadyMarker(path, agentId) {
|
|
|
3859
4289
|
}
|
|
3860
4290
|
|
|
3861
4291
|
// src/lib/replication.ts
|
|
3862
|
-
import { spawn as spawn4 } from "child_process";
|
|
4292
|
+
import { spawn as spawn4 } from "node:child_process";
|
|
3863
4293
|
function startSessionDbReplication(configPath) {
|
|
3864
4294
|
return spawn4("litestream", ["replicate", "-config", configPath], {
|
|
3865
4295
|
stdio: "inherit"
|
|
@@ -3875,7 +4305,7 @@ async function stopSessionDbReplication(child, timeoutMs) {
|
|
|
3875
4305
|
}
|
|
3876
4306
|
|
|
3877
4307
|
// src/lib/process-liveness.ts
|
|
3878
|
-
import { readFileSync as
|
|
4308
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
3879
4309
|
function isProcessAlive(pid) {
|
|
3880
4310
|
try {
|
|
3881
4311
|
process.kill(pid, 0);
|
|
@@ -3890,7 +4320,7 @@ function isProcessAlive(pid) {
|
|
|
3890
4320
|
}
|
|
3891
4321
|
if (process.platform !== "linux") return true;
|
|
3892
4322
|
try {
|
|
3893
|
-
const status2 =
|
|
4323
|
+
const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
|
|
3894
4324
|
return !/^State:\s+Z(?:\s|$)/m.test(status2);
|
|
3895
4325
|
} catch (error2) {
|
|
3896
4326
|
console.error(
|
|
@@ -3901,9 +4331,9 @@ function isProcessAlive(pid) {
|
|
|
3901
4331
|
}
|
|
3902
4332
|
|
|
3903
4333
|
// src/lib/openai-usage.ts
|
|
3904
|
-
import { readFileSync as
|
|
3905
|
-
import { homedir as
|
|
3906
|
-
import { join as
|
|
4334
|
+
import { readFileSync as readFileSync5 } from "node:fs";
|
|
4335
|
+
import { homedir as homedir4 } from "node:os";
|
|
4336
|
+
import { join as join6 } from "node:path";
|
|
3907
4337
|
var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
3908
4338
|
var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
|
|
3909
4339
|
var OpenAiUsageError = class extends Error {
|
|
@@ -3917,7 +4347,7 @@ function isLocalCredentialProblem2(err) {
|
|
|
3917
4347
|
}
|
|
3918
4348
|
function readOpenCodeChatGptCredentials() {
|
|
3919
4349
|
try {
|
|
3920
|
-
const raw =
|
|
4350
|
+
const raw = readFileSync5(join6(homedir4(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
|
|
3921
4351
|
let parsed;
|
|
3922
4352
|
try {
|
|
3923
4353
|
parsed = JSON.parse(raw);
|
|
@@ -3939,6 +4369,23 @@ function readOpenCodeChatGptCredentials() {
|
|
|
3939
4369
|
return null;
|
|
3940
4370
|
}
|
|
3941
4371
|
}
|
|
4372
|
+
function parseChatGptIdentity(accessToken) {
|
|
4373
|
+
const segments = accessToken.split(".");
|
|
4374
|
+
if (segments.length !== 3) return null;
|
|
4375
|
+
let payload;
|
|
4376
|
+
try {
|
|
4377
|
+
const parsed = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
|
|
4378
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
4379
|
+
payload = parsed;
|
|
4380
|
+
} catch {
|
|
4381
|
+
return null;
|
|
4382
|
+
}
|
|
4383
|
+
const profile = payload["https://api.openai.com/profile"];
|
|
4384
|
+
const auth = payload["https://api.openai.com/auth"];
|
|
4385
|
+
const ownerEmail = typeof profile?.email === "string" ? profile.email.trim() || null : null;
|
|
4386
|
+
const planType = typeof auth?.chatgpt_plan_type === "string" ? auth.chatgpt_plan_type.trim() || null : null;
|
|
4387
|
+
return ownerEmail === null && planType === null ? null : { ownerEmail, planType };
|
|
4388
|
+
}
|
|
3942
4389
|
function toWindow2(headers, name) {
|
|
3943
4390
|
const utilizationHeader = headers.get(`x-codex-${name}-used-percent`);
|
|
3944
4391
|
const windowMinutesHeader = headers.get(`x-codex-${name}-window-minutes`);
|
|
@@ -4014,6 +4461,7 @@ async function getOpenAiUsage(port) {
|
|
|
4014
4461
|
"credentials_expired"
|
|
4015
4462
|
);
|
|
4016
4463
|
}
|
|
4464
|
+
const subscription = parseChatGptIdentity(credentials2.accessToken);
|
|
4017
4465
|
const models = await resolveProbeModels(port);
|
|
4018
4466
|
if (models.length === 0) {
|
|
4019
4467
|
throw new OpenAiUsageError("No supported OpenAI probe model is available.", "no_probe_model");
|
|
@@ -4046,7 +4494,7 @@ async function getOpenAiUsage(port) {
|
|
|
4046
4494
|
"no_usable_window"
|
|
4047
4495
|
);
|
|
4048
4496
|
}
|
|
4049
|
-
return usage;
|
|
4497
|
+
return { ...usage, subscription };
|
|
4050
4498
|
}
|
|
4051
4499
|
if (res.status === 401) {
|
|
4052
4500
|
throw new OpenAiUsageError(
|
|
@@ -4161,8 +4609,8 @@ function resolveResourceUsageReportingEnabled(flagValue, env) {
|
|
|
4161
4609
|
}
|
|
4162
4610
|
|
|
4163
4611
|
// src/lib/resource-usage.ts
|
|
4164
|
-
import { cpus, totalmem, freemem } from "os";
|
|
4165
|
-
import { statfsSync as statfsSync2 } from "fs";
|
|
4612
|
+
import { cpus, totalmem, freemem } from "node:os";
|
|
4613
|
+
import { statfsSync as statfsSync2 } from "node:fs";
|
|
4166
4614
|
|
|
4167
4615
|
// src/lib/ecs-task-metadata.ts
|
|
4168
4616
|
var ECS_METADATA_TIMEOUT_MS = 2e3;
|
|
@@ -4247,58 +4695,97 @@ function readDisk(homeDir) {
|
|
|
4247
4695
|
};
|
|
4248
4696
|
}
|
|
4249
4697
|
}
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4698
|
+
var CPU_PEAK_WINDOW_MS = 6e4;
|
|
4699
|
+
var CPU_PEAK_WINDOW_SAMPLE_COUNT = 4;
|
|
4700
|
+
var CPU_PEAK_SAMPLE_INTERVAL_MS = CPU_PEAK_WINDOW_MS / CPU_PEAK_WINDOW_SAMPLE_COUNT;
|
|
4701
|
+
function createCpuPeakSampler() {
|
|
4702
|
+
const sampleHistory = new Array(CPU_PEAK_WINDOW_SAMPLE_COUNT);
|
|
4703
|
+
sampleHistory[0] = readCpuSample();
|
|
4704
|
+
let nextSampleIndex = 1;
|
|
4705
|
+
let sampleCount = 1;
|
|
4706
|
+
let peak = null;
|
|
4707
|
+
const timer = setInterval(() => {
|
|
4253
4708
|
const current = readCpuSample();
|
|
4254
|
-
const
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
const warnings = [];
|
|
4261
|
-
if (disk.warning) warnings.push(disk.warning);
|
|
4262
|
-
if (ecsWarning) warnings.push(ecsWarning);
|
|
4263
|
-
let cpuPercent = hostCpuPercent;
|
|
4264
|
-
let cpuCount = hostCpuCount;
|
|
4265
|
-
let memoryTotalBytes = totalmem();
|
|
4266
|
-
let memoryAvailableBytes = freemem();
|
|
4267
|
-
if (limits !== null) {
|
|
4268
|
-
cpuCount = limits.cpuCount;
|
|
4269
|
-
memoryTotalBytes = limits.memoryTotalBytes;
|
|
4270
|
-
memoryAvailableBytes = clamp(
|
|
4271
|
-
limits.memoryTotalBytes - (totalmem() - freemem()),
|
|
4272
|
-
0,
|
|
4273
|
-
limits.memoryTotalBytes
|
|
4274
|
-
);
|
|
4275
|
-
cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4709
|
+
const sampleFromWindowAgo = sampleCount === CPU_PEAK_WINDOW_SAMPLE_COUNT ? sampleHistory[nextSampleIndex] : void 0;
|
|
4710
|
+
if (sampleFromWindowAgo !== void 0) {
|
|
4711
|
+
const percentage = cpuPercentBetween(sampleFromWindowAgo, current);
|
|
4712
|
+
if (percentage !== null) {
|
|
4713
|
+
peak = peak === null ? percentage : Math.max(peak, percentage);
|
|
4714
|
+
}
|
|
4276
4715
|
}
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4716
|
+
sampleHistory[nextSampleIndex] = current;
|
|
4717
|
+
nextSampleIndex = (nextSampleIndex + 1) % CPU_PEAK_WINDOW_SAMPLE_COUNT;
|
|
4718
|
+
sampleCount = Math.min(sampleCount + 1, CPU_PEAK_WINDOW_SAMPLE_COUNT);
|
|
4719
|
+
}, CPU_PEAK_SAMPLE_INTERVAL_MS);
|
|
4720
|
+
return {
|
|
4721
|
+
takeAndReset: () => {
|
|
4722
|
+
const currentPeak = peak;
|
|
4723
|
+
peak = null;
|
|
4724
|
+
return currentPeak;
|
|
4725
|
+
},
|
|
4726
|
+
stop: () => clearInterval(timer)
|
|
4727
|
+
};
|
|
4728
|
+
}
|
|
4729
|
+
function createResourceUsageCollector(homeDir) {
|
|
4730
|
+
let previous = readCpuSample();
|
|
4731
|
+
const cpuPeakSampler = createCpuPeakSampler();
|
|
4732
|
+
return {
|
|
4733
|
+
collect: async () => {
|
|
4734
|
+
const current = readCpuSample();
|
|
4735
|
+
const hostCpuPercent = cpuPercentBetween(previous, current);
|
|
4736
|
+
const hostCpuPeakPercent = cpuPeakSampler.takeAndReset();
|
|
4737
|
+
const hostCpuCount = cpus().length;
|
|
4738
|
+
previous = current;
|
|
4739
|
+
const disk = readDisk(homeDir);
|
|
4740
|
+
const opencodeDbBytes = statSessionDbBytes(homeDir);
|
|
4741
|
+
const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
|
|
4742
|
+
const warnings = [];
|
|
4743
|
+
if (disk.warning) warnings.push(disk.warning);
|
|
4744
|
+
if (ecsWarning) warnings.push(ecsWarning);
|
|
4745
|
+
let cpuPercent = hostCpuPercent;
|
|
4746
|
+
let cpuPeakPercent = hostCpuPeakPercent;
|
|
4747
|
+
let cpuCount = hostCpuCount;
|
|
4748
|
+
let memoryTotalBytes = totalmem();
|
|
4749
|
+
let memoryAvailableBytes = freemem();
|
|
4750
|
+
if (limits !== null) {
|
|
4751
|
+
cpuCount = limits.cpuCount;
|
|
4752
|
+
memoryTotalBytes = limits.memoryTotalBytes;
|
|
4753
|
+
memoryAvailableBytes = clamp(
|
|
4754
|
+
limits.memoryTotalBytes - (totalmem() - freemem()),
|
|
4755
|
+
0,
|
|
4756
|
+
limits.memoryTotalBytes
|
|
4757
|
+
);
|
|
4758
|
+
cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4759
|
+
cpuPeakPercent = hostCpuPeakPercent === null ? null : clamp(round2(hostCpuPeakPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4760
|
+
}
|
|
4761
|
+
return {
|
|
4762
|
+
usage: {
|
|
4763
|
+
cpuPercent,
|
|
4764
|
+
cpuPeakPercent,
|
|
4765
|
+
cpuCount,
|
|
4766
|
+
memoryTotalBytes,
|
|
4767
|
+
memoryAvailableBytes,
|
|
4768
|
+
diskTotalBytes: disk.totalBytes,
|
|
4769
|
+
diskFreeBytes: disk.freeBytes,
|
|
4770
|
+
opencodeDbBytes
|
|
4771
|
+
},
|
|
4772
|
+
warnings
|
|
4773
|
+
};
|
|
4774
|
+
},
|
|
4775
|
+
stop: cpuPeakSampler.stop
|
|
4289
4776
|
};
|
|
4290
4777
|
}
|
|
4291
4778
|
|
|
4292
4779
|
// src/lib/channels/driver.ts
|
|
4293
|
-
import { homedir as
|
|
4780
|
+
import { homedir as homedir5 } from "node:os";
|
|
4294
4781
|
|
|
4295
4782
|
// src/lib/runner-file-sync.ts
|
|
4296
|
-
import { join as
|
|
4783
|
+
import { join as join8 } from "node:path";
|
|
4297
4784
|
|
|
4298
4785
|
// src/lib/file-push.ts
|
|
4299
|
-
import { randomUUID } from "crypto";
|
|
4300
|
-
import { chmod, mkdir, open as
|
|
4301
|
-
import { basename, dirname as
|
|
4786
|
+
import { randomUUID } from "node:crypto";
|
|
4787
|
+
import { chmod, mkdir, open as open3, realpath, rename, unlink } from "node:fs/promises";
|
|
4788
|
+
import { basename, dirname as dirname5, isAbsolute, join as join7, relative, resolve as resolve2, sep } from "node:path";
|
|
4302
4789
|
var FILE_MODE = 384;
|
|
4303
4790
|
var DIRECTORY_MODE = 448;
|
|
4304
4791
|
async function writePushedFile(request) {
|
|
@@ -4329,9 +4816,9 @@ async function writePushedFile(request) {
|
|
|
4329
4816
|
}
|
|
4330
4817
|
try {
|
|
4331
4818
|
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
4332
|
-
|
|
4819
|
+
dirname5(candidate)
|
|
4333
4820
|
);
|
|
4334
|
-
const realTarget =
|
|
4821
|
+
const realTarget = join7(existingAncestor, ...missingSegments, basename(candidate));
|
|
4335
4822
|
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
4336
4823
|
if (allowedDirectory === null) {
|
|
4337
4824
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
@@ -4341,8 +4828,8 @@ async function writePushedFile(request) {
|
|
|
4341
4828
|
}
|
|
4342
4829
|
if (missingSegments.length > 0) {
|
|
4343
4830
|
await createMissingDirectories(existingAncestor, missingSegments);
|
|
4344
|
-
const realParent = await realpath(
|
|
4345
|
-
if (realParent !==
|
|
4831
|
+
const realParent = await realpath(dirname5(realTarget));
|
|
4832
|
+
if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
4346
4833
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
4347
4834
|
path: realTarget,
|
|
4348
4835
|
bytes,
|
|
@@ -4367,7 +4854,7 @@ function expandAndValidate(requestedPath, homeDir) {
|
|
|
4367
4854
|
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
4368
4855
|
return null;
|
|
4369
4856
|
}
|
|
4370
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4857
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4371
4858
|
if (expanded.split(/[/\\]/).includes("..")) {
|
|
4372
4859
|
return null;
|
|
4373
4860
|
}
|
|
@@ -4385,7 +4872,7 @@ async function resolveNearestExistingAncestor(directory) {
|
|
|
4385
4872
|
try {
|
|
4386
4873
|
return { existingAncestor: await realpath(current), missingSegments };
|
|
4387
4874
|
} catch (err) {
|
|
4388
|
-
const parent =
|
|
4875
|
+
const parent = dirname5(current);
|
|
4389
4876
|
if (err.code !== "ENOENT" || parent === current) {
|
|
4390
4877
|
throw err;
|
|
4391
4878
|
}
|
|
@@ -4440,16 +4927,16 @@ function contains(realDirectory, realTarget) {
|
|
|
4440
4927
|
async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
4441
4928
|
let current = existingAncestor;
|
|
4442
4929
|
for (const segment of missingSegments) {
|
|
4443
|
-
current =
|
|
4930
|
+
current = join7(current, segment);
|
|
4444
4931
|
await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
|
|
4445
4932
|
await chmod(current, DIRECTORY_MODE);
|
|
4446
4933
|
}
|
|
4447
4934
|
}
|
|
4448
4935
|
async function writeAtomically(realTarget, content) {
|
|
4449
|
-
const temporaryPath =
|
|
4936
|
+
const temporaryPath = join7(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
4450
4937
|
let handle;
|
|
4451
4938
|
try {
|
|
4452
|
-
handle = await
|
|
4939
|
+
handle = await open3(temporaryPath, "wx", FILE_MODE);
|
|
4453
4940
|
await handle.writeFile(content);
|
|
4454
4941
|
await handle.chmod(FILE_MODE);
|
|
4455
4942
|
await handle.close();
|
|
@@ -4576,12 +5063,12 @@ var NOT_APPLIED = {
|
|
|
4576
5063
|
opencodeAuthApplied: false
|
|
4577
5064
|
};
|
|
4578
5065
|
function isClaudeCredentialPath(requestedPath, homeDir) {
|
|
4579
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4580
|
-
return expanded ===
|
|
5066
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
5067
|
+
return expanded === join8(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
|
|
4581
5068
|
}
|
|
4582
5069
|
function isOpenCodeAuthPath(requestedPath, homeDir) {
|
|
4583
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4584
|
-
return expanded ===
|
|
5070
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
5071
|
+
return expanded === join8(homeDir, ...OPENCODE_AUTH_SEGMENTS);
|
|
4585
5072
|
}
|
|
4586
5073
|
async function applyOne(options, file) {
|
|
4587
5074
|
const label = `${file.id.slice(0, 8)} (${file.path})`;
|
|
@@ -5108,6 +5595,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5108
5595
|
* and stops opencode.
|
|
5109
5596
|
*/
|
|
5110
5597
|
stopped = false;
|
|
5598
|
+
recycleRequestedFlag = false;
|
|
5111
5599
|
constructor(config) {
|
|
5112
5600
|
this.agentId = config.agentId;
|
|
5113
5601
|
this.port = config.port;
|
|
@@ -5127,7 +5615,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5127
5615
|
this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
5128
5616
|
this.now = config.now ?? (() => Date.now());
|
|
5129
5617
|
this.fileSyncDirectories = config.fileSyncDirectories ?? [];
|
|
5130
|
-
this.homeDir = config.homeDir ??
|
|
5618
|
+
this.homeDir = config.homeDir ?? homedir5();
|
|
5131
5619
|
this.maxActiveSessions = config.maxActiveSessions;
|
|
5132
5620
|
this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
|
|
5133
5621
|
this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
|
|
@@ -5213,6 +5701,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5213
5701
|
let dispatched = 0;
|
|
5214
5702
|
try {
|
|
5215
5703
|
const conversations = await this.getPendingConversations();
|
|
5704
|
+
if (this.recycleRequestedFlag) {
|
|
5705
|
+
this.stop();
|
|
5706
|
+
}
|
|
5216
5707
|
if (conversations.length > 0) {
|
|
5217
5708
|
const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
|
|
5218
5709
|
this.log({
|
|
@@ -5341,6 +5832,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5341
5832
|
stop() {
|
|
5342
5833
|
this.stopped = true;
|
|
5343
5834
|
}
|
|
5835
|
+
/**
|
|
5836
|
+
* The server clears this request when a new MicroVM identity is recorded, so a
|
|
5837
|
+
* same-VM tunnel reconnect does not consume it. This is a plain read rather
|
|
5838
|
+
* than a consume; `run.ts` guards the action once-only.
|
|
5839
|
+
*/
|
|
5840
|
+
get recycleRequested() {
|
|
5841
|
+
return this.recycleRequestedFlag;
|
|
5842
|
+
}
|
|
5344
5843
|
/**
|
|
5345
5844
|
* Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
|
|
5346
5845
|
* graceful shutdown, so a turn whose reply is ready — or completes within the
|
|
@@ -5478,7 +5977,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5478
5977
|
this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
|
|
5479
5978
|
break;
|
|
5480
5979
|
}
|
|
5481
|
-
const
|
|
5980
|
+
const errorMessage3 = err instanceof Error ? err.message : String(err);
|
|
5482
5981
|
this.sessions.delete(conv.id);
|
|
5483
5982
|
this.supersede(conv.id, sessionId);
|
|
5484
5983
|
this.log({
|
|
@@ -5487,7 +5986,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5487
5986
|
conversation_id: conv.id,
|
|
5488
5987
|
message_id: message.id
|
|
5489
5988
|
});
|
|
5490
|
-
await this.markFailed(conv.id, message.id, null,
|
|
5989
|
+
await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
|
|
5491
5990
|
this.log({
|
|
5492
5991
|
level: "warn",
|
|
5493
5992
|
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)}`,
|
|
@@ -5498,7 +5997,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5498
5997
|
});
|
|
5499
5998
|
this.log({
|
|
5500
5999
|
level: "error",
|
|
5501
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
6000
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage3}`,
|
|
5502
6001
|
conversation_id: conv.id,
|
|
5503
6002
|
message_id: message.id
|
|
5504
6003
|
});
|
|
@@ -5519,14 +6018,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5519
6018
|
this.unconfirmedDispatchFailures.delete(message.id);
|
|
5520
6019
|
this.sessions.delete(conv.id);
|
|
5521
6020
|
this.supersede(conv.id, sessionId);
|
|
5522
|
-
const
|
|
6021
|
+
const errorMessage3 = `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.`;
|
|
5523
6022
|
this.log({
|
|
5524
6023
|
level: "error",
|
|
5525
|
-
message:
|
|
6024
|
+
message: errorMessage3,
|
|
5526
6025
|
conversation_id: conv.id,
|
|
5527
6026
|
message_id: message.id
|
|
5528
6027
|
});
|
|
5529
|
-
await this.markFailed(conv.id, message.id, null,
|
|
6028
|
+
await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
|
|
5530
6029
|
this.log({
|
|
5531
6030
|
level: "warn",
|
|
5532
6031
|
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)}`,
|
|
@@ -5860,6 +6359,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5860
6359
|
});
|
|
5861
6360
|
await this.markFailed(conv.id, message.id, sessionId, error2, usage, failure);
|
|
5862
6361
|
}
|
|
6362
|
+
if (ocId !== null) {
|
|
6363
|
+
await this.reportSubagentAuthFailures(conv.id, ocId, message.id, messages);
|
|
6364
|
+
}
|
|
5863
6365
|
} catch (err) {
|
|
5864
6366
|
if (err instanceof ChannelAuthError) throw err;
|
|
5865
6367
|
this.log({
|
|
@@ -6824,6 +7326,12 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6824
7326
|
return;
|
|
6825
7327
|
}
|
|
6826
7328
|
inFlight.done = true;
|
|
7329
|
+
await this.reportSubagentAuthFailures(
|
|
7330
|
+
watcher.conv.id,
|
|
7331
|
+
inFlight.opencodeMessageId,
|
|
7332
|
+
inFlight.evidentMessageId,
|
|
7333
|
+
messages
|
|
7334
|
+
);
|
|
6827
7335
|
}
|
|
6828
7336
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
6829
7337
|
return;
|
|
@@ -7066,6 +7574,12 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7066
7574
|
return;
|
|
7067
7575
|
}
|
|
7068
7576
|
inFlight.done = true;
|
|
7577
|
+
await this.reportSubagentAuthFailures(
|
|
7578
|
+
watcher.conv.id,
|
|
7579
|
+
inFlight.opencodeMessageId,
|
|
7580
|
+
inFlight.evidentMessageId,
|
|
7581
|
+
messages
|
|
7582
|
+
);
|
|
7069
7583
|
}
|
|
7070
7584
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
7071
7585
|
}
|
|
@@ -7236,6 +7750,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7236
7750
|
});
|
|
7237
7751
|
return;
|
|
7238
7752
|
}
|
|
7753
|
+
await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
|
|
7239
7754
|
this.dontRedispatch.delete(row.id);
|
|
7240
7755
|
void this.postSignal(row.conversation_id, row.id, "readopt_failed");
|
|
7241
7756
|
return;
|
|
@@ -7397,6 +7912,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7397
7912
|
});
|
|
7398
7913
|
return;
|
|
7399
7914
|
}
|
|
7915
|
+
if (ocId !== null) {
|
|
7916
|
+
await this.reportSubagentAuthFailures(row.conversation_id, ocId, row.id, messages);
|
|
7917
|
+
}
|
|
7400
7918
|
this.dontRedispatch.delete(row.id);
|
|
7401
7919
|
void this.postSignal(row.conversation_id, row.id, "readopt_done");
|
|
7402
7920
|
}
|
|
@@ -7490,14 +8008,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7490
8008
|
this.unconfirmedDispatchFailures.delete(row.id);
|
|
7491
8009
|
this.sessions.delete(readoptConv.id);
|
|
7492
8010
|
this.supersede(readoptConv.id, sessionId);
|
|
7493
|
-
const
|
|
8011
|
+
const errorMessage3 = `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.`;
|
|
7494
8012
|
this.log({
|
|
7495
8013
|
level: "error",
|
|
7496
|
-
message:
|
|
8014
|
+
message: errorMessage3,
|
|
7497
8015
|
conversation_id: row.conversation_id,
|
|
7498
8016
|
message_id: row.id
|
|
7499
8017
|
});
|
|
7500
|
-
await this.markFailed(row.conversation_id, row.id, null,
|
|
8018
|
+
await this.markFailed(row.conversation_id, row.id, null, errorMessage3).catch((markErr) => {
|
|
7501
8019
|
this.log({
|
|
7502
8020
|
level: "warn",
|
|
7503
8021
|
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)}`,
|
|
@@ -8112,6 +8630,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
8112
8630
|
throw new Error(`Failed to get pending conversations: HTTP ${res.status}`);
|
|
8113
8631
|
}
|
|
8114
8632
|
const data = await res.json();
|
|
8633
|
+
this.recycleRequestedFlag = data.recycle_requested === true;
|
|
8115
8634
|
let conversations = data.conversations;
|
|
8116
8635
|
if (this.conversationFilter) {
|
|
8117
8636
|
conversations = conversations.filter((c) => c.id === this.conversationFilter);
|
|
@@ -8347,65 +8866,170 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
8347
8866
|
reply?.info?.modelID ?? null
|
|
8348
8867
|
);
|
|
8349
8868
|
}
|
|
8350
|
-
|
|
8351
|
-
|
|
8352
|
-
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
8353
|
-
* — the server records it via `log()` (no DB write, no notification). This is
|
|
8354
|
-
* fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
|
|
8355
|
-
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
8356
|
-
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
8357
|
-
* context (no silent catch, per development-workflow).
|
|
8358
|
-
*
|
|
8359
|
-
* Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
|
|
8360
|
-
* telemetry), but the `paused` liveness-clear uses it to know whether to
|
|
8361
|
-
* RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
|
|
8362
|
-
* stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
|
|
8363
|
-
* leaves liveness").
|
|
8364
|
-
*/
|
|
8365
|
-
async postSignal(conversationId, messageId, signal, extra) {
|
|
8869
|
+
async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
|
|
8870
|
+
const providerId = failure.providerId ?? "(unknown)";
|
|
8366
8871
|
try {
|
|
8367
8872
|
const res = await this.fetchImpl(
|
|
8368
|
-
`${this.apiUrl}/runners/${this.agentId}/
|
|
8873
|
+
`${this.apiUrl}/runners/${this.agentId}/model-auth-failures`,
|
|
8369
8874
|
{
|
|
8370
8875
|
method: "POST",
|
|
8371
8876
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
8372
|
-
body: JSON.stringify({
|
|
8877
|
+
body: JSON.stringify({
|
|
8878
|
+
provider_id: failure.providerId,
|
|
8879
|
+
model_id: failure.modelId,
|
|
8880
|
+
reason: failure.reason
|
|
8881
|
+
})
|
|
8373
8882
|
}
|
|
8374
8883
|
);
|
|
8375
8884
|
if (!res.ok) {
|
|
8376
8885
|
this.log({
|
|
8377
8886
|
level: "warn",
|
|
8378
|
-
message: `
|
|
8887
|
+
message: `Sub-agent model-auth report for provider ${providerId} (${failure.reason}) returned HTTP ${res.status} (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)})`,
|
|
8379
8888
|
conversation_id: conversationId,
|
|
8380
8889
|
message_id: messageId
|
|
8381
8890
|
});
|
|
8382
|
-
return false;
|
|
8383
8891
|
}
|
|
8384
|
-
return true;
|
|
8385
8892
|
} catch (err) {
|
|
8386
8893
|
this.log({
|
|
8387
8894
|
level: "warn",
|
|
8388
|
-
message: `
|
|
8895
|
+
message: `Sub-agent model-auth report for provider ${providerId} (${failure.reason}) failed with status network-error (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)}): ${err instanceof Error ? err.message : String(err)}`,
|
|
8389
8896
|
conversation_id: conversationId,
|
|
8390
8897
|
message_id: messageId
|
|
8391
8898
|
});
|
|
8392
|
-
return false;
|
|
8393
8899
|
}
|
|
8394
8900
|
}
|
|
8395
|
-
async
|
|
8396
|
-
|
|
8397
|
-
|
|
8398
|
-
|
|
8399
|
-
|
|
8400
|
-
|
|
8401
|
-
|
|
8901
|
+
async clearSubagentModelAuthFailure(providerId, conversationId, messageId) {
|
|
8902
|
+
try {
|
|
8903
|
+
const res = await this.fetchImpl(
|
|
8904
|
+
`${this.apiUrl}/runners/${this.agentId}/model-auth-failures/${encodeURIComponent(providerId)}`,
|
|
8905
|
+
{
|
|
8906
|
+
method: "DELETE",
|
|
8907
|
+
headers: { Authorization: this.getAuthHeader() }
|
|
8908
|
+
}
|
|
8909
|
+
);
|
|
8910
|
+
if (!res.ok) {
|
|
8911
|
+
this.log({
|
|
8912
|
+
level: "warn",
|
|
8913
|
+
message: `Sub-agent model-auth clear for provider ${providerId} returned HTTP ${res.status} (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)})`,
|
|
8914
|
+
conversation_id: conversationId,
|
|
8915
|
+
message_id: messageId
|
|
8916
|
+
});
|
|
8402
8917
|
}
|
|
8403
|
-
)
|
|
8404
|
-
|
|
8918
|
+
} catch (err) {
|
|
8919
|
+
this.log({
|
|
8920
|
+
level: "warn",
|
|
8921
|
+
message: `Sub-agent model-auth clear for provider ${providerId} failed with status network-error (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)}): ${err instanceof Error ? err.message : String(err)}`,
|
|
8922
|
+
conversation_id: conversationId,
|
|
8923
|
+
message_id: messageId
|
|
8924
|
+
});
|
|
8925
|
+
}
|
|
8405
8926
|
}
|
|
8406
|
-
|
|
8407
|
-
|
|
8408
|
-
|
|
8927
|
+
async reportSubagentAuthFailures(conversationId, opencodeMessageId, evidentMessageId, messages) {
|
|
8928
|
+
const refs = collectSubagentSessions(messages, opencodeMessageId);
|
|
8929
|
+
if (refs.length === 0) return;
|
|
8930
|
+
const failedProviders = /* @__PURE__ */ new Map();
|
|
8931
|
+
const succeededProviders = /* @__PURE__ */ new Set();
|
|
8932
|
+
for (const ref of refs) {
|
|
8933
|
+
try {
|
|
8934
|
+
const childMessages = await getSessionMessages(this.port, ref.sessionId);
|
|
8935
|
+
if (childMessages === null) {
|
|
8936
|
+
this.log({
|
|
8937
|
+
level: "debug",
|
|
8938
|
+
message: `Could not read sub-agent session ${ref.sessionId} while checking credential failures \u2014 skipping it`,
|
|
8939
|
+
conversation_id: conversationId,
|
|
8940
|
+
message_id: evidentMessageId
|
|
8941
|
+
});
|
|
8942
|
+
continue;
|
|
8943
|
+
}
|
|
8944
|
+
const outcome = findSubagentAuthOutcome(childMessages, ref.startedAtMs);
|
|
8945
|
+
if (!outcome) continue;
|
|
8946
|
+
if (outcome.outcome === "failed") {
|
|
8947
|
+
failedProviders.set(outcome.providerId, outcome.failure);
|
|
8948
|
+
} else {
|
|
8949
|
+
succeededProviders.add(outcome.providerId);
|
|
8950
|
+
}
|
|
8951
|
+
} catch (err) {
|
|
8952
|
+
this.log({
|
|
8953
|
+
level: "warn",
|
|
8954
|
+
message: `Failed to inspect sub-agent session ${ref.sessionId} for credential failures (conversation ${conversationId.slice(0, 8)}, message ${evidentMessageId.slice(0, 8)}): ${err instanceof Error ? err.message : String(err)}`,
|
|
8955
|
+
conversation_id: conversationId,
|
|
8956
|
+
message_id: evidentMessageId
|
|
8957
|
+
});
|
|
8958
|
+
}
|
|
8959
|
+
}
|
|
8960
|
+
for (const [providerId, failure] of failedProviders) {
|
|
8961
|
+
this.log({
|
|
8962
|
+
level: "warn",
|
|
8963
|
+
message: `Sub-agent turn failed on provider ${providerId} (${failure.reason}) \u2014 recording credential evidence`,
|
|
8964
|
+
conversation_id: conversationId,
|
|
8965
|
+
message_id: evidentMessageId
|
|
8966
|
+
});
|
|
8967
|
+
await this.recordSubagentModelAuthFailure(failure, conversationId, evidentMessageId);
|
|
8968
|
+
}
|
|
8969
|
+
for (const providerId of succeededProviders) {
|
|
8970
|
+
if (failedProviders.has(providerId)) continue;
|
|
8971
|
+
await this.clearSubagentModelAuthFailure(providerId, conversationId, evidentMessageId);
|
|
8972
|
+
}
|
|
8973
|
+
}
|
|
8974
|
+
/**
|
|
8975
|
+
* Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
|
|
8976
|
+
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
8977
|
+
* — the server records it via `log()` (no DB write, no notification). This is
|
|
8978
|
+
* fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
|
|
8979
|
+
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
8980
|
+
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
8981
|
+
* context (no silent catch, per development-workflow).
|
|
8982
|
+
*
|
|
8983
|
+
* Returns whether the POST SUCCEEDED (2xx). Most callers ignore this (pure
|
|
8984
|
+
* telemetry), but the `paused` liveness-clear uses it to know whether to
|
|
8985
|
+
* RE-ASSERT on a later tick — a single dropped `paused` POST must not leave a
|
|
8986
|
+
* stale `last_seen_alive_at` on a still-paused row (Bugbot "Failed paused signal
|
|
8987
|
+
* leaves liveness").
|
|
8988
|
+
*/
|
|
8989
|
+
async postSignal(conversationId, messageId, signal, extra) {
|
|
8990
|
+
try {
|
|
8991
|
+
const res = await this.fetchImpl(
|
|
8992
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
|
|
8993
|
+
{
|
|
8994
|
+
method: "POST",
|
|
8995
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
8996
|
+
body: JSON.stringify({ signal, ...extra })
|
|
8997
|
+
}
|
|
8998
|
+
);
|
|
8999
|
+
if (!res.ok) {
|
|
9000
|
+
this.log({
|
|
9001
|
+
level: "warn",
|
|
9002
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
|
|
9003
|
+
conversation_id: conversationId,
|
|
9004
|
+
message_id: messageId
|
|
9005
|
+
});
|
|
9006
|
+
return false;
|
|
9007
|
+
}
|
|
9008
|
+
return true;
|
|
9009
|
+
} catch (err) {
|
|
9010
|
+
this.log({
|
|
9011
|
+
level: "warn",
|
|
9012
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
|
|
9013
|
+
conversation_id: conversationId,
|
|
9014
|
+
message_id: messageId
|
|
9015
|
+
});
|
|
9016
|
+
return false;
|
|
9017
|
+
}
|
|
9018
|
+
}
|
|
9019
|
+
async persistSession(conversationId, sessionId) {
|
|
9020
|
+
const res = await this.fetchImpl(
|
|
9021
|
+
`${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}`,
|
|
9022
|
+
{
|
|
9023
|
+
method: "PATCH",
|
|
9024
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
9025
|
+
body: JSON.stringify({ opencode_session_id: sessionId })
|
|
9026
|
+
}
|
|
9027
|
+
);
|
|
9028
|
+
this.assertAuth(res, "persisting session id");
|
|
9029
|
+
}
|
|
9030
|
+
/**
|
|
9031
|
+
* EXISTING combinedAuth interaction route (WI-CHAN-3) — idempotent + retried.
|
|
9032
|
+
* `POST .../interactive-event {type, data, source_message_id?}`. The server
|
|
8409
9033
|
* persists the interaction and posts a link to the proxied opencode-web
|
|
8410
9034
|
* conversation, @mentioning the user who triggered THIS message's turn.
|
|
8411
9035
|
*
|
|
@@ -8500,6 +9124,13 @@ import chalk5 from "chalk";
|
|
|
8500
9124
|
import ora2 from "ora";
|
|
8501
9125
|
import { select as select2 } from "@inquirer/prompts";
|
|
8502
9126
|
var INTERACTIVE_START_TIMEOUT_MS = 3e4;
|
|
9127
|
+
function checkNonInteractivePortConflict(port, isPortInUseFn) {
|
|
9128
|
+
if (isPortInUseFn(port)) {
|
|
9129
|
+
throw new Error(
|
|
9130
|
+
`Port ${port} is already in use by a non-OpenCode process. Free it or pass --port.`
|
|
9131
|
+
);
|
|
9132
|
+
}
|
|
9133
|
+
}
|
|
8503
9134
|
async function ensureOpenCodeRunning(ctx) {
|
|
8504
9135
|
const healthCheck = await checkOpenCodeHealth(ctx.port);
|
|
8505
9136
|
if (healthCheck.healthy) {
|
|
@@ -8547,6 +9178,7 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
8547
9178
|
}
|
|
8548
9179
|
}
|
|
8549
9180
|
if (!ctx.interactive) {
|
|
9181
|
+
checkNonInteractivePortConflict(ctx.port, isPortInUse);
|
|
8550
9182
|
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
8551
9183
|
const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
|
|
8552
9184
|
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
@@ -8628,9 +9260,119 @@ Port ${port} is already in use.`));
|
|
|
8628
9260
|
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
8629
9261
|
}
|
|
8630
9262
|
|
|
9263
|
+
// src/commands/ensure-opencode-v2.ts
|
|
9264
|
+
import chalk6 from "chalk";
|
|
9265
|
+
import { select as select3 } from "@inquirer/prompts";
|
|
9266
|
+
async function probeOpenCode2WithoutPassword(port) {
|
|
9267
|
+
try {
|
|
9268
|
+
const response = await fetch(`http://127.0.0.1:${port}/global/health`, {
|
|
9269
|
+
signal: AbortSignal.timeout(2e3)
|
|
9270
|
+
});
|
|
9271
|
+
if (response.status === 401) {
|
|
9272
|
+
return { healthy: false, authFailed: true, error: "HTTP 401" };
|
|
9273
|
+
}
|
|
9274
|
+
if (!response.ok) {
|
|
9275
|
+
return { healthy: false, error: `HTTP ${response.status}` };
|
|
9276
|
+
}
|
|
9277
|
+
return { healthy: true };
|
|
9278
|
+
} catch (error2) {
|
|
9279
|
+
return {
|
|
9280
|
+
healthy: false,
|
|
9281
|
+
error: error2 instanceof Error ? error2.message : "Unknown error"
|
|
9282
|
+
};
|
|
9283
|
+
}
|
|
9284
|
+
}
|
|
9285
|
+
function unknownPasswordError(port) {
|
|
9286
|
+
return new Error(
|
|
9287
|
+
`OpenCode V2 (opencode2) is already running on port ${port} with a password this runner doesn't know. Free the port or pass --port.`
|
|
9288
|
+
);
|
|
9289
|
+
}
|
|
9290
|
+
function v2SessionSupportIncompleteError() {
|
|
9291
|
+
return new Error(
|
|
9292
|
+
"OpenCode V2 (opencode2) session support is incomplete \u2014 see https://github.com/sroze/evident/issues/2075. Not starting a new opencode2 process."
|
|
9293
|
+
);
|
|
9294
|
+
}
|
|
9295
|
+
async function ensureOpenCode2Running(ctx) {
|
|
9296
|
+
const initialHealth = await probeOpenCode2WithoutPassword(ctx.port);
|
|
9297
|
+
if (initialHealth.authFailed) {
|
|
9298
|
+
throw unknownPasswordError(ctx.port);
|
|
9299
|
+
}
|
|
9300
|
+
if (initialHealth.healthy) {
|
|
9301
|
+
return {
|
|
9302
|
+
port: ctx.port,
|
|
9303
|
+
process: null,
|
|
9304
|
+
version: null,
|
|
9305
|
+
notReadyReason: null,
|
|
9306
|
+
password: null
|
|
9307
|
+
};
|
|
9308
|
+
}
|
|
9309
|
+
if (!isOpenCode2Installed()) {
|
|
9310
|
+
throw new Error(
|
|
9311
|
+
"OpenCode V2 (opencode2) is not installed. Install it with: npm install -g @opencode-ai/cli@beta"
|
|
9312
|
+
);
|
|
9313
|
+
}
|
|
9314
|
+
let port = ctx.port;
|
|
9315
|
+
if (!ctx.interactive) {
|
|
9316
|
+
checkNonInteractivePortConflict(port, isPortInUse);
|
|
9317
|
+
} else if (isPortInUse(port)) {
|
|
9318
|
+
console.log(chalk6.yellow(`
|
|
9319
|
+
Port ${port} is already in use.`));
|
|
9320
|
+
const alternativePort = findAvailablePort(port + 1);
|
|
9321
|
+
if (alternativePort) {
|
|
9322
|
+
const useAlternative = await select3({
|
|
9323
|
+
message: `Use port ${alternativePort} instead?`,
|
|
9324
|
+
choices: [
|
|
9325
|
+
{ name: `Yes, use port ${alternativePort}`, value: "yes" },
|
|
9326
|
+
{ name: "No, I will free the port manually", value: "no" }
|
|
9327
|
+
]
|
|
9328
|
+
});
|
|
9329
|
+
if (useAlternative === "yes") {
|
|
9330
|
+
port = alternativePort;
|
|
9331
|
+
} else {
|
|
9332
|
+
throw new Error(`Port ${ctx.port} is in use`);
|
|
9333
|
+
}
|
|
9334
|
+
}
|
|
9335
|
+
}
|
|
9336
|
+
if (!ctx.interactive) {
|
|
9337
|
+
throw v2SessionSupportIncompleteError();
|
|
9338
|
+
}
|
|
9339
|
+
console.log(chalk6.yellow(`
|
|
9340
|
+
${v2SessionSupportIncompleteError().message}`));
|
|
9341
|
+
const action = await select3({
|
|
9342
|
+
message: "OpenCode V2 is not running. What would you like to do?",
|
|
9343
|
+
choices: [
|
|
9344
|
+
{
|
|
9345
|
+
name: "Show me the command",
|
|
9346
|
+
value: "manual",
|
|
9347
|
+
description: "Display the command to run manually"
|
|
9348
|
+
},
|
|
9349
|
+
{
|
|
9350
|
+
name: "Continue without OpenCode V2",
|
|
9351
|
+
value: "continue",
|
|
9352
|
+
description: "Requests will fail until OpenCode V2 starts"
|
|
9353
|
+
}
|
|
9354
|
+
]
|
|
9355
|
+
});
|
|
9356
|
+
if (action === "manual") {
|
|
9357
|
+
blank();
|
|
9358
|
+
console.log(chalk6.bold("Run this command in another terminal:"));
|
|
9359
|
+
blank();
|
|
9360
|
+
console.log(` ${chalk6.cyan(`opencode2 serve --port ${port}`)}`);
|
|
9361
|
+
blank();
|
|
9362
|
+
throw new Error("Please start OpenCode V2 manually");
|
|
9363
|
+
}
|
|
9364
|
+
return {
|
|
9365
|
+
port,
|
|
9366
|
+
process: null,
|
|
9367
|
+
version: null,
|
|
9368
|
+
notReadyReason: "you chose to continue without OpenCode V2",
|
|
9369
|
+
password: null
|
|
9370
|
+
};
|
|
9371
|
+
}
|
|
9372
|
+
|
|
8631
9373
|
// src/lib/runner-credentials.ts
|
|
8632
|
-
import { chmodSync as chmodSync2, writeFileSync as
|
|
8633
|
-
import { spawn as spawn5 } from "child_process";
|
|
9374
|
+
import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "node:fs";
|
|
9375
|
+
import { spawn as spawn5 } from "node:child_process";
|
|
8634
9376
|
var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
|
|
8635
9377
|
var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
|
|
8636
9378
|
var GITHUB_PROBE_TIMEOUT_MS = 1e4;
|
|
@@ -8870,8 +9612,8 @@ async function configureGitHubAccess({ env, log: log3 }) {
|
|
|
8870
9612
|
}
|
|
8871
9613
|
try {
|
|
8872
9614
|
env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
|
|
8873
|
-
|
|
8874
|
-
|
|
9615
|
+
writeFileSync4(GIT_CONFIG_GLOBAL, "");
|
|
9616
|
+
writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
|
|
8875
9617
|
chmodSync2(GIT_CREDENTIAL_HELPER, 448);
|
|
8876
9618
|
const config = [
|
|
8877
9619
|
["user.name", env.GIT_USER_NAME ?? "evident-bot"],
|
|
@@ -8902,11 +9644,11 @@ async function configureGitHubAccess({ env, log: log3 }) {
|
|
|
8902
9644
|
}
|
|
8903
9645
|
|
|
8904
9646
|
// src/lib/opencode/config-overlay.ts
|
|
8905
|
-
import { execFileSync as execFileSync2 } from "child_process";
|
|
8906
|
-
import { copyFileSync, existsSync as existsSync2, statSync as
|
|
8907
|
-
import { isAbsolute as isAbsolute2, join as
|
|
9647
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
9648
|
+
import { copyFileSync, existsSync as existsSync2, statSync as statSync6 } from "node:fs";
|
|
9649
|
+
import { isAbsolute as isAbsolute2, join as join9, resolve as resolve3 } from "node:path";
|
|
8908
9650
|
function isFile(filePath) {
|
|
8909
|
-
return existsSync2(filePath) &&
|
|
9651
|
+
return existsSync2(filePath) && statSync6(filePath).isFile();
|
|
8910
9652
|
}
|
|
8911
9653
|
function applyRunnerOpenCodeConfig({
|
|
8912
9654
|
overlayPath,
|
|
@@ -8918,7 +9660,7 @@ function applyRunnerOpenCodeConfig({
|
|
|
8918
9660
|
return;
|
|
8919
9661
|
}
|
|
8920
9662
|
const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
|
|
8921
|
-
const target = isFile(
|
|
9663
|
+
const target = isFile(join9(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
|
|
8922
9664
|
if (!isFile(source)) {
|
|
8923
9665
|
log3(
|
|
8924
9666
|
`RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
|
|
@@ -8926,7 +9668,7 @@ function applyRunnerOpenCodeConfig({
|
|
|
8926
9668
|
);
|
|
8927
9669
|
return;
|
|
8928
9670
|
}
|
|
8929
|
-
copyFileSync(source,
|
|
9671
|
+
copyFileSync(source, join9(cwd, target));
|
|
8930
9672
|
try {
|
|
8931
9673
|
execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
8932
9674
|
stdio: "ignore"
|
|
@@ -8935,7 +9677,242 @@ function applyRunnerOpenCodeConfig({
|
|
|
8935
9677
|
const detail = error2 instanceof Error ? error2.message : String(error2);
|
|
8936
9678
|
log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
|
|
8937
9679
|
}
|
|
8938
|
-
log3(`Applied runner OpenCode config ${source} to ${
|
|
9680
|
+
log3(`Applied runner OpenCode config ${source} to ${join9(cwd, target)}`);
|
|
9681
|
+
}
|
|
9682
|
+
|
|
9683
|
+
// src/lib/credential-sync.ts
|
|
9684
|
+
import { renameSync, writeFileSync as writeFileSync5 } from "node:fs";
|
|
9685
|
+
var CREDENTIAL_SYNC_TICK_TIMEOUT_MS = 3e4;
|
|
9686
|
+
var CREDENTIAL_FLUSH_DEADLINE_MS = 8e3;
|
|
9687
|
+
var CREDENTIAL_FLUSH_ABORT_GRACE_MS = 500;
|
|
9688
|
+
var DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS = 60;
|
|
9689
|
+
var STORES = ["claude", "opencode"];
|
|
9690
|
+
var MAX_FLUSH_PASSES = 2;
|
|
9691
|
+
function outcomesWith(outcome) {
|
|
9692
|
+
return { claude: outcome, opencode: outcome };
|
|
9693
|
+
}
|
|
9694
|
+
function errorMessage2(error2) {
|
|
9695
|
+
return error2 instanceof Error ? error2.message : String(error2);
|
|
9696
|
+
}
|
|
9697
|
+
function waitForSettlement(promise, timeoutMs) {
|
|
9698
|
+
return new Promise((resolve4) => {
|
|
9699
|
+
let settled = false;
|
|
9700
|
+
const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
|
|
9701
|
+
const finish = (value) => {
|
|
9702
|
+
if (settled) return;
|
|
9703
|
+
settled = true;
|
|
9704
|
+
clearTimeout(timer);
|
|
9705
|
+
resolve4(value);
|
|
9706
|
+
};
|
|
9707
|
+
promise.then(
|
|
9708
|
+
() => finish(true),
|
|
9709
|
+
() => finish(true)
|
|
9710
|
+
);
|
|
9711
|
+
});
|
|
9712
|
+
}
|
|
9713
|
+
function writeMarker(markerPath, outcomes, log3) {
|
|
9714
|
+
const body = `${STORES.map((store) => `${store}=${outcomes[store]}`).join("\n")}
|
|
9715
|
+
`;
|
|
9716
|
+
const temporaryPath = `${markerPath}.tmp`;
|
|
9717
|
+
try {
|
|
9718
|
+
writeFileSync5(temporaryPath, body, { mode: 384 });
|
|
9719
|
+
renameSync(temporaryPath, markerPath);
|
|
9720
|
+
} catch (error2) {
|
|
9721
|
+
log3(`CREDENTIAL-FLUSH-MARKER-UNWRITABLE: ${markerPath}: ${errorMessage2(error2)}`, "warn");
|
|
9722
|
+
}
|
|
9723
|
+
}
|
|
9724
|
+
function intervalSeconds(env, log3) {
|
|
9725
|
+
const raw = env.CREDS_SYNC_INTERVAL;
|
|
9726
|
+
if (raw === void 0 || /^[1-9][0-9]*$/.test(raw) && Number.isSafeInteger(Number(raw))) {
|
|
9727
|
+
return raw === void 0 ? DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS : Number(raw);
|
|
9728
|
+
}
|
|
9729
|
+
log3(
|
|
9730
|
+
`CREDENTIAL-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${raw}' is not a positive integer; using ${DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS}s`,
|
|
9731
|
+
"warn"
|
|
9732
|
+
);
|
|
9733
|
+
return DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS;
|
|
9734
|
+
}
|
|
9735
|
+
async function runFlushStore(store, env, synchroniserRunner, log3, deadlineAt) {
|
|
9736
|
+
const remainingMs = deadlineAt - Date.now();
|
|
9737
|
+
if (remainingMs <= 0) return { outcome: "timeout", orphaned: false };
|
|
9738
|
+
const controller = new AbortController();
|
|
9739
|
+
let result;
|
|
9740
|
+
let failed = false;
|
|
9741
|
+
const completion = Promise.resolve().then(
|
|
9742
|
+
() => synchroniserRunner(["sync-once", store], {
|
|
9743
|
+
timeoutMs: remainingMs,
|
|
9744
|
+
env,
|
|
9745
|
+
signal: controller.signal
|
|
9746
|
+
})
|
|
9747
|
+
).then(
|
|
9748
|
+
(value) => {
|
|
9749
|
+
result = value;
|
|
9750
|
+
},
|
|
9751
|
+
(error2) => {
|
|
9752
|
+
failed = true;
|
|
9753
|
+
log3(`CREDENTIAL-FLUSH-ERROR: ${store}: ${errorMessage2(error2)}`, "warn");
|
|
9754
|
+
}
|
|
9755
|
+
);
|
|
9756
|
+
const abortTimer = setTimeout(() => controller.abort(), remainingMs);
|
|
9757
|
+
const settledBeforeDeadline = await waitForSettlement(completion, remainingMs);
|
|
9758
|
+
clearTimeout(abortTimer);
|
|
9759
|
+
if (!settledBeforeDeadline) {
|
|
9760
|
+
controller.abort();
|
|
9761
|
+
const settledAfterAbort = await waitForSettlement(completion, CREDENTIAL_FLUSH_ABORT_GRACE_MS);
|
|
9762
|
+
if (!settledAfterAbort) return { outcome: "timeout", orphaned: true };
|
|
9763
|
+
return { outcome: "timeout", orphaned: false };
|
|
9764
|
+
}
|
|
9765
|
+
if (failed || !result) return { outcome: "failed", orphaned: false };
|
|
9766
|
+
if (result.timedOut || Date.now() >= deadlineAt) {
|
|
9767
|
+
return { outcome: "timeout", orphaned: false };
|
|
9768
|
+
}
|
|
9769
|
+
return { outcome: result.code === 0 ? "ok" : "failed", orphaned: false };
|
|
9770
|
+
}
|
|
9771
|
+
function createCredentialSync({
|
|
9772
|
+
markerPath,
|
|
9773
|
+
env,
|
|
9774
|
+
log: log3,
|
|
9775
|
+
synchroniserRunner = runSynchroniser
|
|
9776
|
+
}) {
|
|
9777
|
+
const persistenceDisabled = !env.PERSISTENCE_BUCKET;
|
|
9778
|
+
let disabled = persistenceDisabled;
|
|
9779
|
+
let armed = false;
|
|
9780
|
+
let stopped = false;
|
|
9781
|
+
let timer;
|
|
9782
|
+
let inFlight;
|
|
9783
|
+
let activeTickAbort;
|
|
9784
|
+
let lastTickFailed;
|
|
9785
|
+
let flushPromise;
|
|
9786
|
+
const scheduleTick = (intervalMs, startTick2) => {
|
|
9787
|
+
if (stopped) return;
|
|
9788
|
+
timer = setTimeout(() => {
|
|
9789
|
+
timer = void 0;
|
|
9790
|
+
startTick2();
|
|
9791
|
+
}, intervalMs);
|
|
9792
|
+
};
|
|
9793
|
+
const startTick = (intervalMs) => {
|
|
9794
|
+
if (stopped) return;
|
|
9795
|
+
const controller = new AbortController();
|
|
9796
|
+
activeTickAbort = controller;
|
|
9797
|
+
const tick = (async () => {
|
|
9798
|
+
const outcomes = {
|
|
9799
|
+
claude: "failed",
|
|
9800
|
+
opencode: "failed"
|
|
9801
|
+
};
|
|
9802
|
+
for (const store of STORES) {
|
|
9803
|
+
if (controller.signal.aborted) break;
|
|
9804
|
+
try {
|
|
9805
|
+
const result = await synchroniserRunner(["sync-once", store], {
|
|
9806
|
+
timeoutMs: CREDENTIAL_SYNC_TICK_TIMEOUT_MS,
|
|
9807
|
+
env,
|
|
9808
|
+
signal: controller.signal
|
|
9809
|
+
});
|
|
9810
|
+
outcomes[store] = !result.timedOut && result.code === 0 ? "ok" : "failed";
|
|
9811
|
+
} catch (error2) {
|
|
9812
|
+
outcomes[store] = "failed";
|
|
9813
|
+
log3(`CREDENTIAL-SYNC-TICK-ERROR: ${store}: ${errorMessage2(error2)}`, "debug");
|
|
9814
|
+
}
|
|
9815
|
+
}
|
|
9816
|
+
const failed = STORES.some((store) => outcomes[store] === "failed");
|
|
9817
|
+
log3(
|
|
9818
|
+
`CREDENTIAL-SYNC-TICK: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
|
|
9819
|
+
"debug"
|
|
9820
|
+
);
|
|
9821
|
+
if (failed && lastTickFailed !== true) {
|
|
9822
|
+
log3(
|
|
9823
|
+
"CREDENTIAL-SYNC-FAILED: an interval credential sync failed; retrying on the next tick",
|
|
9824
|
+
"warn"
|
|
9825
|
+
);
|
|
9826
|
+
} else if (!failed && lastTickFailed === true) {
|
|
9827
|
+
log3("CREDENTIAL-SYNC-RECOVERED: interval credential sync succeeded again", "warn");
|
|
9828
|
+
}
|
|
9829
|
+
lastTickFailed = failed;
|
|
9830
|
+
})().finally(() => {
|
|
9831
|
+
if (activeTickAbort === controller) activeTickAbort = void 0;
|
|
9832
|
+
if (inFlight === tick) inFlight = void 0;
|
|
9833
|
+
scheduleTick(intervalMs, () => startTick(intervalMs));
|
|
9834
|
+
});
|
|
9835
|
+
inFlight = tick;
|
|
9836
|
+
};
|
|
9837
|
+
const performFlush = async () => {
|
|
9838
|
+
stopped = true;
|
|
9839
|
+
if (timer) {
|
|
9840
|
+
clearTimeout(timer);
|
|
9841
|
+
timer = void 0;
|
|
9842
|
+
}
|
|
9843
|
+
const deadlineAt = Date.now() + CREDENTIAL_FLUSH_DEADLINE_MS;
|
|
9844
|
+
if (inFlight) {
|
|
9845
|
+
const settled = await waitForSettlement(inFlight, CREDENTIAL_FLUSH_DEADLINE_MS);
|
|
9846
|
+
if (!settled) {
|
|
9847
|
+
activeTickAbort?.abort();
|
|
9848
|
+
const settledAfterAbort = await waitForSettlement(
|
|
9849
|
+
inFlight,
|
|
9850
|
+
CREDENTIAL_FLUSH_ABORT_GRACE_MS
|
|
9851
|
+
);
|
|
9852
|
+
if (!settledAfterAbort) {
|
|
9853
|
+
log3(
|
|
9854
|
+
"CREDENTIAL-FLUSH-ORPHANED-TICK: a sync-once child could not be proven settled; skipping the boundary flush rather than becoming a second writer",
|
|
9855
|
+
"warn"
|
|
9856
|
+
);
|
|
9857
|
+
return { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9858
|
+
}
|
|
9859
|
+
}
|
|
9860
|
+
}
|
|
9861
|
+
if (disabled) return { outcomes: outcomesWith("skipped"), orphaned: false };
|
|
9862
|
+
const outcomes = outcomesWith("timeout");
|
|
9863
|
+
for (const store of STORES) {
|
|
9864
|
+
const result = await runFlushStore(store, env, synchroniserRunner, log3, deadlineAt);
|
|
9865
|
+
if (result.orphaned) {
|
|
9866
|
+
log3(
|
|
9867
|
+
"CREDENTIAL-FLUSH-ORPHANED-CHILD: a flush sync-once child could not be proven settled; refusing another flush pass rather than becoming a second writer",
|
|
9868
|
+
"warn"
|
|
9869
|
+
);
|
|
9870
|
+
return { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9871
|
+
}
|
|
9872
|
+
outcomes[store] = result.outcome;
|
|
9873
|
+
}
|
|
9874
|
+
return { outcomes, orphaned: false };
|
|
9875
|
+
};
|
|
9876
|
+
let flushPasses = 0;
|
|
9877
|
+
let lastFlush;
|
|
9878
|
+
return {
|
|
9879
|
+
arm() {
|
|
9880
|
+
if (stopped || armed) return;
|
|
9881
|
+
armed = true;
|
|
9882
|
+
if (persistenceDisabled) {
|
|
9883
|
+
disabled = true;
|
|
9884
|
+
log3(
|
|
9885
|
+
"CREDENTIAL-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot",
|
|
9886
|
+
"warn"
|
|
9887
|
+
);
|
|
9888
|
+
return;
|
|
9889
|
+
}
|
|
9890
|
+
disabled = false;
|
|
9891
|
+
const intervalMs = intervalSeconds(env, log3) * 1e3;
|
|
9892
|
+
scheduleTick(intervalMs, () => startTick(intervalMs));
|
|
9893
|
+
},
|
|
9894
|
+
async stopAndFlush(publish) {
|
|
9895
|
+
let result;
|
|
9896
|
+
const runningFlush = flushPromise;
|
|
9897
|
+
if (runningFlush) {
|
|
9898
|
+
result = await runningFlush;
|
|
9899
|
+
} else if (flushPasses >= MAX_FLUSH_PASSES || lastFlush?.orphaned) {
|
|
9900
|
+
result = lastFlush ?? { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9901
|
+
} else {
|
|
9902
|
+
flushPasses++;
|
|
9903
|
+
const currentFlush = performFlush();
|
|
9904
|
+
flushPromise = currentFlush;
|
|
9905
|
+
try {
|
|
9906
|
+
result = await currentFlush;
|
|
9907
|
+
lastFlush = result;
|
|
9908
|
+
} finally {
|
|
9909
|
+
if (flushPromise === currentFlush) flushPromise = void 0;
|
|
9910
|
+
}
|
|
9911
|
+
}
|
|
9912
|
+
if (publish) writeMarker(markerPath, result.outcomes, log3);
|
|
9913
|
+
return result.outcomes;
|
|
9914
|
+
}
|
|
9915
|
+
};
|
|
8939
9916
|
}
|
|
8940
9917
|
|
|
8941
9918
|
// src/commands/run.ts
|
|
@@ -8975,7 +9952,7 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
8975
9952
|
if (trimmed === "") {
|
|
8976
9953
|
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
8977
9954
|
}
|
|
8978
|
-
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ?
|
|
9955
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join10(homeDir, trimmed.slice(2)) : trimmed;
|
|
8979
9956
|
if (!isAbsolute3(expanded)) {
|
|
8980
9957
|
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
8981
9958
|
}
|
|
@@ -8999,6 +9976,28 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
8999
9976
|
var DEFAULT_OPENCODE_START_TIMEOUT_SECONDS = 180;
|
|
9000
9977
|
var MAX_OPENCODE_START_TIMEOUT_SECONDS = 3600;
|
|
9001
9978
|
var OPENCODE_START_TIMEOUT_ENV = "EVIDENT_OPENCODE_START_TIMEOUT";
|
|
9979
|
+
var OPENCODE_VERSION_ENV = "EVIDENT_OPENCODE_VERSION";
|
|
9980
|
+
function resolveOpenCodeVersion(options, env = process.env) {
|
|
9981
|
+
let raw;
|
|
9982
|
+
let source;
|
|
9983
|
+
if (options.opencodeVersion !== void 0) {
|
|
9984
|
+
raw = options.opencodeVersion;
|
|
9985
|
+
source = "--opencode-version";
|
|
9986
|
+
} else if (env[OPENCODE_VERSION_ENV] !== void 0 && env[OPENCODE_VERSION_ENV] !== "") {
|
|
9987
|
+
raw = env[OPENCODE_VERSION_ENV];
|
|
9988
|
+
source = OPENCODE_VERSION_ENV;
|
|
9989
|
+
} else {
|
|
9990
|
+
return { version: "v1", warnings: [] };
|
|
9991
|
+
}
|
|
9992
|
+
const normalized = raw.trim().toLowerCase();
|
|
9993
|
+
if (normalized !== "v1" && normalized !== "v2") {
|
|
9994
|
+
return {
|
|
9995
|
+
version: "v1",
|
|
9996
|
+
warnings: [`Ignoring invalid ${source} "${raw}": expected v1 or v2; using the default v1`]
|
|
9997
|
+
};
|
|
9998
|
+
}
|
|
9999
|
+
return { version: normalized, warnings: [] };
|
|
10000
|
+
}
|
|
9002
10001
|
function resolveOpenCodeStartTimeoutMs(options, env = process.env) {
|
|
9003
10002
|
const defaultMs = DEFAULT_OPENCODE_START_TIMEOUT_SECONDS * 1e3;
|
|
9004
10003
|
let raw;
|
|
@@ -9065,7 +10064,7 @@ function log2(state, message, level = "info") {
|
|
|
9065
10064
|
})
|
|
9066
10065
|
);
|
|
9067
10066
|
} else if (!state.interactive) {
|
|
9068
|
-
const prefix = level === "error" ?
|
|
10067
|
+
const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
|
|
9069
10068
|
console.log(`${prefix} ${message}`);
|
|
9070
10069
|
}
|
|
9071
10070
|
}
|
|
@@ -9095,7 +10094,7 @@ function logActivity(state, entry) {
|
|
|
9095
10094
|
}
|
|
9096
10095
|
function reportSessionDbRecovery(state) {
|
|
9097
10096
|
try {
|
|
9098
|
-
const report = drainSessionDbRecoveryReport({ homeDir:
|
|
10097
|
+
const report = drainSessionDbRecoveryReport({ homeDir: homedir6(), env: process.env });
|
|
9099
10098
|
for (const record of report.records) {
|
|
9100
10099
|
const activity = buildSessionDbRecoveryActivity(record);
|
|
9101
10100
|
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
@@ -9126,18 +10125,18 @@ function reportSessionDbRecoveryRecord(state, record) {
|
|
|
9126
10125
|
function displayStatus(state) {
|
|
9127
10126
|
if (!state.interactive) return;
|
|
9128
10127
|
const attempt = state.connection?.reconnectAttempt ?? 0;
|
|
9129
|
-
const tunnel = state.connected ?
|
|
9130
|
-
const opencode = state.opencodeConnected ?
|
|
9131
|
-
const messages = state.messageCount > 0 ?
|
|
10128
|
+
const tunnel = state.connected ? chalk7.green("tunnel: connected") : attempt > 0 ? chalk7.yellow(`tunnel: reconnecting (#${attempt})`) : chalk7.yellow("tunnel: connecting");
|
|
10129
|
+
const opencode = state.opencodeConnected ? chalk7.green(`opencode: :${state.port}`) : chalk7.red(`opencode: :${state.port} (down)`);
|
|
10130
|
+
const messages = state.messageCount > 0 ? chalk7.dim(` \xB7 ${state.messageCount} processed`) : "";
|
|
9132
10131
|
const last = state.activityLog[state.activityLog.length - 1];
|
|
9133
|
-
const detail = last ?
|
|
10132
|
+
const detail = last ? chalk7.dim(` \xB7 ${last.type === "error" ? last.error ?? "" : last.message ?? ""}`) : "";
|
|
9134
10133
|
const agent = state.agentName ?? state.agentId;
|
|
9135
10134
|
console.log(
|
|
9136
|
-
`${
|
|
10135
|
+
`${chalk7.bold("Evident")} ${chalk7.dim(agent)} ${tunnel} ${opencode}${messages}${detail}`
|
|
9137
10136
|
);
|
|
9138
10137
|
}
|
|
9139
10138
|
async function promptForLogin(promptMessage, successMessage) {
|
|
9140
|
-
const action = await
|
|
10139
|
+
const action = await select4({
|
|
9141
10140
|
message: promptMessage,
|
|
9142
10141
|
choices: [
|
|
9143
10142
|
{
|
|
@@ -9153,7 +10152,7 @@ async function promptForLogin(promptMessage, successMessage) {
|
|
|
9153
10152
|
]
|
|
9154
10153
|
});
|
|
9155
10154
|
if (action === "exit") {
|
|
9156
|
-
console.log(
|
|
10155
|
+
console.log(chalk7.dim(`
|
|
9157
10156
|
You can log in later by running: ${getCliName()} login`));
|
|
9158
10157
|
process.exit(0);
|
|
9159
10158
|
}
|
|
@@ -9164,7 +10163,7 @@ You can log in later by running: ${getCliName()} login`));
|
|
|
9164
10163
|
process.exit(1);
|
|
9165
10164
|
}
|
|
9166
10165
|
blank();
|
|
9167
|
-
console.log(
|
|
10166
|
+
console.log(chalk7.green(successMessage));
|
|
9168
10167
|
blank();
|
|
9169
10168
|
return { token: credentials2.token, authType: "bearer", user: credentials2.user };
|
|
9170
10169
|
}
|
|
@@ -9177,12 +10176,12 @@ async function handleAuthError(state, error2) {
|
|
|
9177
10176
|
if (state.interactive) displayStatus(state);
|
|
9178
10177
|
if (!state.interactive) {
|
|
9179
10178
|
blank();
|
|
9180
|
-
console.log(
|
|
9181
|
-
console.log(
|
|
10179
|
+
console.log(chalk7.red("Authentication expired"));
|
|
10180
|
+
console.log(chalk7.dim("Your authentication token is no longer valid."));
|
|
9182
10181
|
blank();
|
|
9183
|
-
console.log(
|
|
9184
|
-
console.log(
|
|
9185
|
-
console.log(
|
|
10182
|
+
console.log(chalk7.dim("To fix this:"));
|
|
10183
|
+
console.log(chalk7.dim(` 1. Run '${getCliName()} login' to re-authenticate`));
|
|
10184
|
+
console.log(chalk7.dim(" 2. Restart this command"));
|
|
9186
10185
|
blank();
|
|
9187
10186
|
await cleanup(state);
|
|
9188
10187
|
await shutdownTelemetry();
|
|
@@ -9190,7 +10189,7 @@ async function handleAuthError(state, error2) {
|
|
|
9190
10189
|
return { success: false };
|
|
9191
10190
|
}
|
|
9192
10191
|
blank();
|
|
9193
|
-
console.log(
|
|
10192
|
+
console.log(chalk7.yellow("Your authentication has expired."));
|
|
9194
10193
|
blank();
|
|
9195
10194
|
try {
|
|
9196
10195
|
const credentials2 = await promptForLogin(
|
|
@@ -9235,6 +10234,10 @@ async function driveChannels(state, driver) {
|
|
|
9235
10234
|
consecutiveDrainFailures = 0;
|
|
9236
10235
|
unreachableMs = 0;
|
|
9237
10236
|
state.messageCount += processed;
|
|
10237
|
+
if (driver.recycleRequested) {
|
|
10238
|
+
await beginGracefulShutdown(state, "recycle");
|
|
10239
|
+
return;
|
|
10240
|
+
}
|
|
9238
10241
|
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
9239
10242
|
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
9240
10243
|
const fileActivitySnapshot = driver.fileSyncActivity();
|
|
@@ -9250,6 +10253,14 @@ async function driveChannels(state, driver) {
|
|
|
9250
10253
|
const opencodeAuthApplied = opencodeAuthApplies !== lastSeenOpencodeAuthApplies;
|
|
9251
10254
|
lastSeenOpencodeAuthApplies = opencodeAuthApplies;
|
|
9252
10255
|
if (opencodeAuthApplied) state.openaiUsageRearm?.();
|
|
10256
|
+
if (claudeCredentialApplied || opencodeAuthApplied) {
|
|
10257
|
+
void reloadProviderCache(state.port).catch(
|
|
10258
|
+
(error2) => logActivity(state, {
|
|
10259
|
+
type: "error",
|
|
10260
|
+
error: `OpenCode provider cache reload failed on port ${state.port}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10261
|
+
})
|
|
10262
|
+
);
|
|
10263
|
+
}
|
|
9253
10264
|
if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity || fileActivity) {
|
|
9254
10265
|
idlePolls = 0;
|
|
9255
10266
|
idleMs = 0;
|
|
@@ -9277,8 +10288,8 @@ async function driveChannels(state, driver) {
|
|
|
9277
10288
|
state.running = false;
|
|
9278
10289
|
break;
|
|
9279
10290
|
}
|
|
9280
|
-
const
|
|
9281
|
-
logActivity(state, { type: "error", error: `Channel processing error: ${
|
|
10291
|
+
const errorMessage3 = error2 instanceof Error ? error2.message : String(error2);
|
|
10292
|
+
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage3}` });
|
|
9282
10293
|
if (state.interactive) displayStatus(state);
|
|
9283
10294
|
if (driver.hasInFlightWatchers()) {
|
|
9284
10295
|
consecutiveDrainFailures = 0;
|
|
@@ -9316,9 +10327,54 @@ async function driveChannels(state, driver) {
|
|
|
9316
10327
|
}
|
|
9317
10328
|
}
|
|
9318
10329
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
9319
|
-
var SESSION_DB_RECLAIM_MAX_PAGES =
|
|
10330
|
+
var SESSION_DB_RECLAIM_MAX_PAGES = 2e4;
|
|
10331
|
+
function shouldWarnForReclaimSkip(reason) {
|
|
10332
|
+
if (reason !== "sqlite-unavailable") return false;
|
|
10333
|
+
const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
|
|
10334
|
+
if (!version2) return false;
|
|
10335
|
+
const major = Number(version2[1]);
|
|
10336
|
+
const minor = Number(version2[2]);
|
|
10337
|
+
const patch = Number(version2[3]);
|
|
10338
|
+
return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
|
|
10339
|
+
}
|
|
9320
10340
|
function sessionDbPath() {
|
|
9321
|
-
return
|
|
10341
|
+
return join10(homedir6(), ".local", "share", "opencode", "opencode.db");
|
|
10342
|
+
}
|
|
10343
|
+
function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
|
|
10344
|
+
const record = {
|
|
10345
|
+
v: 1,
|
|
10346
|
+
event: "session_db_recovery",
|
|
10347
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10348
|
+
stage: "verify",
|
|
10349
|
+
outcome: "schema_provenance_mismatch",
|
|
10350
|
+
severity: "error",
|
|
10351
|
+
reason: provenance.reason ?? "schema-provenance-mismatch",
|
|
10352
|
+
litestream_exit_code: null,
|
|
10353
|
+
attempt: null,
|
|
10354
|
+
replica_objects: null,
|
|
10355
|
+
replica_bytes: null,
|
|
10356
|
+
quarantine_destination: null,
|
|
10357
|
+
quarantined_objects: null,
|
|
10358
|
+
quarantine_failed_objects: null,
|
|
10359
|
+
quarantined_bytes: null,
|
|
10360
|
+
verified_restore_point: null,
|
|
10361
|
+
restore_points_tried: null,
|
|
10362
|
+
provenance_reason: provenance.reason,
|
|
10363
|
+
provenance_migration_delta: provenance.migrationDelta,
|
|
10364
|
+
replication_suspended: false,
|
|
10365
|
+
dbPath: sessionDbPath(),
|
|
10366
|
+
recorded_version: provenance.recordedVersion,
|
|
10367
|
+
current_version: currentVersion,
|
|
10368
|
+
provenance_pre_boot_migration_count: preBootMigrationCount
|
|
10369
|
+
};
|
|
10370
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
10371
|
+
if (!activity) throw new Error("could not map session-DB provenance activity");
|
|
10372
|
+
logActivity(state, {
|
|
10373
|
+
type: activity.level === "error" ? "error" : "info",
|
|
10374
|
+
level: activity.level,
|
|
10375
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
10376
|
+
metadata: activity.metadata
|
|
10377
|
+
});
|
|
9322
10378
|
}
|
|
9323
10379
|
async function runSweep(state, driver, config) {
|
|
9324
10380
|
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
@@ -9365,7 +10421,7 @@ async function runSweep(state, driver, config) {
|
|
|
9365
10421
|
const reclaimResult = await reclaimSessionDbSpace({
|
|
9366
10422
|
dbPath: sessionDbPath(),
|
|
9367
10423
|
maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
|
|
9368
|
-
allowFullVacuum: protectedNow.size === 0
|
|
10424
|
+
allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
|
|
9369
10425
|
});
|
|
9370
10426
|
if (reclaimResult.ok) {
|
|
9371
10427
|
const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
|
|
@@ -9378,7 +10434,7 @@ async function runSweep(state, driver, config) {
|
|
|
9378
10434
|
} else {
|
|
9379
10435
|
logActivity(state, {
|
|
9380
10436
|
type: "info",
|
|
9381
|
-
message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})`
|
|
10437
|
+
message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})` + (reclaimResult.detail ? `: ${reclaimResult.detail}` : "")
|
|
9382
10438
|
});
|
|
9383
10439
|
}
|
|
9384
10440
|
} catch (error2) {
|
|
@@ -9401,13 +10457,20 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
9401
10457
|
for (const warning2 of config.warnings) {
|
|
9402
10458
|
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
9403
10459
|
}
|
|
9404
|
-
const dbBytes = statSessionDbBytes(
|
|
10460
|
+
const dbBytes = statSessionDbBytes(homedir6());
|
|
9405
10461
|
void (async () => {
|
|
9406
|
-
const
|
|
10462
|
+
const reclaimAvailability = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
|
|
10463
|
+
if (reclaimAvailability !== null) {
|
|
10464
|
+
logActivity(state, {
|
|
10465
|
+
type: "info",
|
|
10466
|
+
level: shouldWarnForReclaimSkip(reclaimAvailability.reason) ? "warn" : "info",
|
|
10467
|
+
message: `Session cleanup: reclaim preflight skipped (${reclaimAvailability.reason})` + (reclaimAvailability.detail ? `: ${reclaimAvailability.detail}` : "")
|
|
10468
|
+
});
|
|
10469
|
+
}
|
|
9407
10470
|
const sizeWarning = buildSessionStoreSizeWarning({
|
|
9408
10471
|
dbBytes,
|
|
9409
10472
|
cleanupEnabled: config.enabled,
|
|
9410
|
-
reclaimSkipReason
|
|
10473
|
+
reclaimSkipReason: reclaimAvailability?.reason ?? null
|
|
9411
10474
|
});
|
|
9412
10475
|
if (sizeWarning !== null) {
|
|
9413
10476
|
logActivity(state, { type: "info", level: "warn", message: sizeWarning });
|
|
@@ -9602,7 +10665,8 @@ function scheduleResourceUsageReporting(state, options) {
|
|
|
9602
10665
|
});
|
|
9603
10666
|
return;
|
|
9604
10667
|
}
|
|
9605
|
-
const collect = createResourceUsageCollector(
|
|
10668
|
+
const { collect, stop } = createResourceUsageCollector(homedir6());
|
|
10669
|
+
state.stopResourceUsageSampling = stop;
|
|
9606
10670
|
let consecutiveFailures = 0;
|
|
9607
10671
|
const tick = async () => {
|
|
9608
10672
|
try {
|
|
@@ -9698,6 +10762,8 @@ async function cleanup(state, opts = {}) {
|
|
|
9698
10762
|
clearTimeout(timer);
|
|
9699
10763
|
}
|
|
9700
10764
|
state.sessionCleanupTimers = [];
|
|
10765
|
+
state.stopOpenCodeLogTail?.();
|
|
10766
|
+
state.stopOpenCodeLogTail = null;
|
|
9701
10767
|
if (state.claudeUsageTimer) {
|
|
9702
10768
|
clearTimeout(state.claudeUsageTimer);
|
|
9703
10769
|
state.claudeUsageTimer = null;
|
|
@@ -9712,21 +10778,41 @@ async function cleanup(state, opts = {}) {
|
|
|
9712
10778
|
clearTimeout(state.resourceUsageTimer);
|
|
9713
10779
|
state.resourceUsageTimer = null;
|
|
9714
10780
|
}
|
|
10781
|
+
state.stopResourceUsageSampling?.();
|
|
10782
|
+
state.stopResourceUsageSampling = null;
|
|
10783
|
+
const credentialSync = state.credentialSync;
|
|
10784
|
+
const flushCredentials = credentialSync ? async (phase, publish) => {
|
|
10785
|
+
await timeShutdownPhase(state, durations, phase, async () => {
|
|
10786
|
+
const outcomes = await credentialSync.stopAndFlush(publish);
|
|
10787
|
+
const level = Object.values(outcomes).every((outcome) => outcome === "ok") ? "info" : "warn";
|
|
10788
|
+
log2(
|
|
10789
|
+
state,
|
|
10790
|
+
`Credential flush: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
|
|
10791
|
+
level
|
|
10792
|
+
);
|
|
10793
|
+
});
|
|
10794
|
+
} : void 0;
|
|
10795
|
+
let drainSettled = true;
|
|
9715
10796
|
if (opts.graceful && state.channelDriver) {
|
|
9716
10797
|
state.channelDriver.stop();
|
|
10798
|
+
}
|
|
10799
|
+
if (flushCredentials) {
|
|
10800
|
+
await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
|
|
10801
|
+
}
|
|
10802
|
+
if (opts.graceful && state.channelDriver) {
|
|
9717
10803
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
9718
10804
|
if (state.interactive) {
|
|
9719
10805
|
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
9720
10806
|
displayStatus(state);
|
|
9721
10807
|
}
|
|
9722
10808
|
const driver = state.channelDriver;
|
|
9723
|
-
|
|
10809
|
+
drainSettled = await timeShutdownPhase(
|
|
9724
10810
|
state,
|
|
9725
10811
|
durations,
|
|
9726
10812
|
"drain",
|
|
9727
10813
|
() => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
|
|
9728
10814
|
);
|
|
9729
|
-
if (!
|
|
10815
|
+
if (!drainSettled) {
|
|
9730
10816
|
logActivity(state, {
|
|
9731
10817
|
type: "info",
|
|
9732
10818
|
message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
|
|
@@ -9734,6 +10820,9 @@ async function cleanup(state, opts = {}) {
|
|
|
9734
10820
|
if (state.interactive) displayStatus(state);
|
|
9735
10821
|
}
|
|
9736
10822
|
}
|
|
10823
|
+
if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
|
|
10824
|
+
await flushCredentials("credential_flush_final", true);
|
|
10825
|
+
}
|
|
9737
10826
|
await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
|
|
9738
10827
|
if (state.connection) {
|
|
9739
10828
|
const connection = state.connection;
|
|
@@ -9769,13 +10858,51 @@ async function cleanup(state, opts = {}) {
|
|
|
9769
10858
|
}
|
|
9770
10859
|
return durations;
|
|
9771
10860
|
}
|
|
10861
|
+
async function beginGracefulShutdown(state, trigger) {
|
|
10862
|
+
if (state.shuttingDown) return;
|
|
10863
|
+
state.shuttingDown = true;
|
|
10864
|
+
const shutdownStartedAt = Date.now();
|
|
10865
|
+
const shutdownMessage = trigger === "recycle" ? "Recycle requested \u2014 finishing in-flight work, then exiting" : "Shutting down...";
|
|
10866
|
+
if (state.interactive) {
|
|
10867
|
+
logActivity(state, { type: "info", message: shutdownMessage });
|
|
10868
|
+
displayStatus(state);
|
|
10869
|
+
} else {
|
|
10870
|
+
log2(state, shutdownMessage);
|
|
10871
|
+
}
|
|
10872
|
+
const durations = await cleanup(state, { graceful: true });
|
|
10873
|
+
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
10874
|
+
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
10875
|
+
let timer;
|
|
10876
|
+
const flushed = shutdownTelemetry().then(
|
|
10877
|
+
() => true,
|
|
10878
|
+
(error2) => {
|
|
10879
|
+
log2(
|
|
10880
|
+
state,
|
|
10881
|
+
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
10882
|
+
"warn"
|
|
10883
|
+
);
|
|
10884
|
+
return true;
|
|
10885
|
+
}
|
|
10886
|
+
);
|
|
10887
|
+
const timedOut = new Promise((resolve4) => {
|
|
10888
|
+
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
10889
|
+
});
|
|
10890
|
+
if (!await Promise.race([flushed, timedOut])) {
|
|
10891
|
+
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
10892
|
+
}
|
|
10893
|
+
clearTimeout(timer);
|
|
10894
|
+
});
|
|
10895
|
+
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
10896
|
+
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
10897
|
+
process.exit(0);
|
|
10898
|
+
}
|
|
9772
10899
|
async function run(options) {
|
|
9773
10900
|
const interactive = isInteractive(options.json);
|
|
9774
10901
|
let logLevel;
|
|
9775
10902
|
let fileSyncDirectories;
|
|
9776
10903
|
try {
|
|
9777
10904
|
logLevel = resolveLogLevel(options);
|
|
9778
|
-
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo,
|
|
10905
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir6());
|
|
9779
10906
|
if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
|
|
9780
10907
|
throw new Error(
|
|
9781
10908
|
"--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
|
|
@@ -9804,7 +10931,9 @@ async function run(options) {
|
|
|
9804
10931
|
connected: false,
|
|
9805
10932
|
opencodeConnected: false,
|
|
9806
10933
|
opencodeVersion: null,
|
|
10934
|
+
sessionDbProvenanceAnomaly: false,
|
|
9807
10935
|
opencodeProcess: null,
|
|
10936
|
+
stopOpenCodeLogTail: null,
|
|
9808
10937
|
litestreamProcess: null,
|
|
9809
10938
|
connection: null,
|
|
9810
10939
|
channelDriver: null,
|
|
@@ -9819,9 +10948,24 @@ async function run(options) {
|
|
|
9819
10948
|
openaiUsageTimer: null,
|
|
9820
10949
|
openaiUsageRearm: null,
|
|
9821
10950
|
resourceUsageTimer: null,
|
|
10951
|
+
stopResourceUsageSampling: null,
|
|
10952
|
+
credentialSync: null,
|
|
9822
10953
|
authHeader: ""
|
|
9823
10954
|
};
|
|
9824
10955
|
setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
|
|
10956
|
+
if (options.credentialSyncMarker) {
|
|
10957
|
+
state.credentialSync = createCredentialSync({
|
|
10958
|
+
markerPath: options.credentialSyncMarker,
|
|
10959
|
+
env: process.env,
|
|
10960
|
+
log: (message, level = "info") => {
|
|
10961
|
+
if (level === "error") {
|
|
10962
|
+
logActivity(state, { type: "error", error: message });
|
|
10963
|
+
} else {
|
|
10964
|
+
logActivity(state, { type: "info", level, message });
|
|
10965
|
+
}
|
|
10966
|
+
}
|
|
10967
|
+
});
|
|
10968
|
+
}
|
|
9825
10969
|
if (fileSyncDirectories.length > 0) {
|
|
9826
10970
|
log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
|
|
9827
10971
|
} else {
|
|
@@ -9847,43 +10991,7 @@ async function run(options) {
|
|
|
9847
10991
|
"warn"
|
|
9848
10992
|
);
|
|
9849
10993
|
}
|
|
9850
|
-
const handleSignal =
|
|
9851
|
-
if (state.shuttingDown) return;
|
|
9852
|
-
state.shuttingDown = true;
|
|
9853
|
-
const shutdownStartedAt = Date.now();
|
|
9854
|
-
if (state.interactive) {
|
|
9855
|
-
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
9856
|
-
displayStatus(state);
|
|
9857
|
-
} else {
|
|
9858
|
-
log2(state, "Shutting down...");
|
|
9859
|
-
}
|
|
9860
|
-
const durations = await cleanup(state, { graceful: true });
|
|
9861
|
-
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
9862
|
-
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
9863
|
-
let timer;
|
|
9864
|
-
const flushed = shutdownTelemetry().then(
|
|
9865
|
-
() => true,
|
|
9866
|
-
(error2) => {
|
|
9867
|
-
log2(
|
|
9868
|
-
state,
|
|
9869
|
-
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
9870
|
-
"warn"
|
|
9871
|
-
);
|
|
9872
|
-
return true;
|
|
9873
|
-
}
|
|
9874
|
-
);
|
|
9875
|
-
const timedOut = new Promise((resolve4) => {
|
|
9876
|
-
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
9877
|
-
});
|
|
9878
|
-
if (!await Promise.race([flushed, timedOut])) {
|
|
9879
|
-
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
9880
|
-
}
|
|
9881
|
-
clearTimeout(timer);
|
|
9882
|
-
});
|
|
9883
|
-
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
9884
|
-
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
9885
|
-
process.exit(0);
|
|
9886
|
-
};
|
|
10994
|
+
const handleSignal = () => beginGracefulShutdown(state, "signal");
|
|
9887
10995
|
process.on("SIGINT", handleSignal);
|
|
9888
10996
|
process.on("SIGTERM", handleSignal);
|
|
9889
10997
|
try {
|
|
@@ -9893,15 +11001,15 @@ async function run(options) {
|
|
|
9893
11001
|
printError("Authentication required");
|
|
9894
11002
|
blank();
|
|
9895
11003
|
console.log(
|
|
9896
|
-
|
|
11004
|
+
chalk7.dim("Set EVIDENT_RUNNER_KEY (or EVIDENT_AGENT_KEY) environment variable for CI")
|
|
9897
11005
|
);
|
|
9898
|
-
console.log(
|
|
11006
|
+
console.log(chalk7.dim("Or run `evident login` for interactive authentication"));
|
|
9899
11007
|
blank();
|
|
9900
11008
|
process.exit(1);
|
|
9901
11009
|
return;
|
|
9902
11010
|
}
|
|
9903
11011
|
blank();
|
|
9904
|
-
console.log(
|
|
11012
|
+
console.log(chalk7.yellow("You are not logged in to Evident."));
|
|
9905
11013
|
blank();
|
|
9906
11014
|
credentials2 = await promptForLogin(
|
|
9907
11015
|
"Would you like to log in now?",
|
|
@@ -9951,7 +11059,7 @@ async function run(options) {
|
|
|
9951
11059
|
);
|
|
9952
11060
|
blank();
|
|
9953
11061
|
console.log(
|
|
9954
|
-
|
|
11062
|
+
chalk7.dim(
|
|
9955
11063
|
"Either provide --runner/--agent <id> or set EVIDENT_RUNNER_KEY/EVIDENT_AGENT_KEY"
|
|
9956
11064
|
)
|
|
9957
11065
|
);
|
|
@@ -9974,15 +11082,15 @@ async function run(options) {
|
|
|
9974
11082
|
);
|
|
9975
11083
|
if (interactive && !state.json) {
|
|
9976
11084
|
blank();
|
|
9977
|
-
console.log(
|
|
9978
|
-
console.log(
|
|
11085
|
+
console.log(chalk7.bold("Evident Run"));
|
|
11086
|
+
console.log(chalk7.dim("-".repeat(40)));
|
|
9979
11087
|
}
|
|
9980
11088
|
const spinner = interactive && !state.json ? ora3("Validating runner...").start() : null;
|
|
9981
11089
|
let validation = await getAgentInfo(state.agentId, state.authHeader);
|
|
9982
11090
|
if (!validation.valid && validation.authFailed && interactive) {
|
|
9983
11091
|
spinner?.fail("Authentication failed");
|
|
9984
11092
|
blank();
|
|
9985
|
-
console.log(
|
|
11093
|
+
console.log(chalk7.yellow("Your authentication token is invalid or expired."));
|
|
9986
11094
|
blank();
|
|
9987
11095
|
credentials2 = await promptForLogin(
|
|
9988
11096
|
"Would you like to log in again?",
|
|
@@ -10029,6 +11137,14 @@ async function run(options) {
|
|
|
10029
11137
|
await restoreCredentialStores(credentialContext);
|
|
10030
11138
|
if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
|
|
10031
11139
|
}
|
|
11140
|
+
state.credentialSync?.arm();
|
|
11141
|
+
state.stopOpenCodeLogTail = tailOpenCodeLogFile(
|
|
11142
|
+
resolveOpenCodeLogPath(homedir6(), process.env),
|
|
11143
|
+
createOpenCodeActivityForwarder(() => ({
|
|
11144
|
+
agentId: state.agentId,
|
|
11145
|
+
authHeader: state.authHeader
|
|
11146
|
+
}))
|
|
11147
|
+
).stop;
|
|
10032
11148
|
let sessionDbVerifyFatal = false;
|
|
10033
11149
|
if (!options.restoreSessionDb) {
|
|
10034
11150
|
log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
|
|
@@ -10078,13 +11194,28 @@ async function run(options) {
|
|
|
10078
11194
|
for (const warning2 of opencodeStartTimeoutWarnings) {
|
|
10079
11195
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
10080
11196
|
}
|
|
11197
|
+
const { version: opencodeVersion, warnings: opencodeVersionWarnings } = resolveOpenCodeVersion(
|
|
11198
|
+
options,
|
|
11199
|
+
process.env
|
|
11200
|
+
);
|
|
11201
|
+
for (const warning2 of opencodeVersionWarnings) {
|
|
11202
|
+
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
11203
|
+
}
|
|
10081
11204
|
const { value: maxActiveSessions, warnings: maxActiveSessionsWarnings } = resolveMaxActiveSessions(options, process.env);
|
|
10082
11205
|
for (const warning2 of maxActiveSessionsWarnings) {
|
|
10083
11206
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
10084
11207
|
}
|
|
11208
|
+
const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
|
|
10085
11209
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
10086
11210
|
try {
|
|
10087
|
-
const oc = await
|
|
11211
|
+
const oc = opencodeVersion === "v2" ? await ensureOpenCode2Running({
|
|
11212
|
+
port: state.port,
|
|
11213
|
+
interactive: state.interactive,
|
|
11214
|
+
agentId: state.agentId,
|
|
11215
|
+
log: (message) => log2(state, message),
|
|
11216
|
+
startTimeoutMs: opencodeStartTimeoutMs,
|
|
11217
|
+
inheritStdio: Boolean(options.opencodePidFile)
|
|
11218
|
+
}) : await ensureOpenCodeRunning({
|
|
10088
11219
|
port: state.port,
|
|
10089
11220
|
interactive: state.interactive,
|
|
10090
11221
|
agentId: state.agentId,
|
|
@@ -10097,7 +11228,7 @@ async function run(options) {
|
|
|
10097
11228
|
state.opencodeVersion = oc.version;
|
|
10098
11229
|
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
10099
11230
|
try {
|
|
10100
|
-
|
|
11231
|
+
writeFileSync6(options.opencodePidFile, `${oc.process.pid}
|
|
10101
11232
|
`, { mode: 384 });
|
|
10102
11233
|
chmodSync3(options.opencodePidFile, 384);
|
|
10103
11234
|
} catch (error2) {
|
|
@@ -10107,6 +11238,23 @@ async function run(options) {
|
|
|
10107
11238
|
});
|
|
10108
11239
|
}
|
|
10109
11240
|
}
|
|
11241
|
+
if (state.opencodeVersion !== null) {
|
|
11242
|
+
const provenance = checkSessionDbProvenance({
|
|
11243
|
+
dbPath: sessionDbPath(),
|
|
11244
|
+
currentVersion: state.opencodeVersion,
|
|
11245
|
+
homeDir: homedir6(),
|
|
11246
|
+
env: process.env
|
|
11247
|
+
});
|
|
11248
|
+
if (provenance.anomaly) {
|
|
11249
|
+
state.sessionDbProvenanceAnomaly = true;
|
|
11250
|
+
logSessionDbProvenanceMismatch(
|
|
11251
|
+
state,
|
|
11252
|
+
provenance,
|
|
11253
|
+
state.opencodeVersion,
|
|
11254
|
+
preBootMigrationIds?.length ?? null
|
|
11255
|
+
);
|
|
11256
|
+
}
|
|
11257
|
+
}
|
|
10110
11258
|
state.opencodeConnected = oc.notReadyReason === null;
|
|
10111
11259
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
10112
11260
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
@@ -10129,10 +11277,10 @@ async function run(options) {
|
|
|
10129
11277
|
if (state.interactive && !state.json) {
|
|
10130
11278
|
logActivity(state, { type: "info", level: "warn", message: noProviderWarning });
|
|
10131
11279
|
blank();
|
|
10132
|
-
console.log(
|
|
11280
|
+
console.log(chalk7.yellow("\u26A0 No OpenCode model provider is configured."));
|
|
10133
11281
|
console.log(
|
|
10134
|
-
|
|
10135
|
-
`Run ${
|
|
11282
|
+
chalk7.dim(
|
|
11283
|
+
`Run ${chalk7.cyan("opencode auth login")} to set one up \u2014 messages will fail until then.`
|
|
10136
11284
|
)
|
|
10137
11285
|
);
|
|
10138
11286
|
blank();
|
|
@@ -10159,7 +11307,7 @@ async function run(options) {
|
|
|
10159
11307
|
let existingPid;
|
|
10160
11308
|
if (existsSync3(options.litestreamPidFile)) {
|
|
10161
11309
|
try {
|
|
10162
|
-
const rawPid =
|
|
11310
|
+
const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
|
|
10163
11311
|
const parsedPid = Number(rawPid);
|
|
10164
11312
|
if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
|
|
10165
11313
|
existingPid = parsedPid;
|
|
@@ -10196,7 +11344,7 @@ async function run(options) {
|
|
|
10196
11344
|
});
|
|
10197
11345
|
try {
|
|
10198
11346
|
if (litestreamProcess.pid !== void 0) {
|
|
10199
|
-
|
|
11347
|
+
writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
|
|
10200
11348
|
`, {
|
|
10201
11349
|
mode: 384
|
|
10202
11350
|
});
|
|
@@ -10256,7 +11404,7 @@ async function run(options) {
|
|
|
10256
11404
|
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
10257
11405
|
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
10258
11406
|
fileSyncDirectories,
|
|
10259
|
-
homeDir:
|
|
11407
|
+
homeDir: homedir6(),
|
|
10260
11408
|
maxActiveSessions,
|
|
10261
11409
|
log: (entry) => (
|
|
10262
11410
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
@@ -10382,6 +11530,18 @@ async function run(options) {
|
|
|
10382
11530
|
if (state.interactive) displayStatus(state);
|
|
10383
11531
|
});
|
|
10384
11532
|
},
|
|
11533
|
+
// Both loops are rearmed because `rearm()` is idempotent for the
|
|
11534
|
+
// provider that did not just connect, and is a no-op when reporting is off.
|
|
11535
|
+
onUsageRearmPing: () => {
|
|
11536
|
+
if (!state.running) return;
|
|
11537
|
+
logActivity(state, {
|
|
11538
|
+
type: "info",
|
|
11539
|
+
level: "debug",
|
|
11540
|
+
message: "Usage rearm ping received"
|
|
11541
|
+
});
|
|
11542
|
+
state.claudeUsageRearm?.();
|
|
11543
|
+
state.openaiUsageRearm?.();
|
|
11544
|
+
},
|
|
10385
11545
|
onInfo: (message) => logActivity(state, { type: "info", message })
|
|
10386
11546
|
}
|
|
10387
11547
|
});
|
|
@@ -10402,7 +11562,17 @@ async function run(options) {
|
|
|
10402
11562
|
setTimer: (timer) => {
|
|
10403
11563
|
state.openaiUsageTimer = timer;
|
|
10404
11564
|
},
|
|
10405
|
-
fetchUsage: () =>
|
|
11565
|
+
fetchUsage: async () => {
|
|
11566
|
+
const usage = await getOpenAiUsage(state.port);
|
|
11567
|
+
if (usage.subscription === null) {
|
|
11568
|
+
logActivity(state, {
|
|
11569
|
+
type: "info",
|
|
11570
|
+
level: "debug",
|
|
11571
|
+
message: "OpenAI usage subscription could not be identified from the local credential"
|
|
11572
|
+
});
|
|
11573
|
+
}
|
|
11574
|
+
return usage;
|
|
11575
|
+
},
|
|
10406
11576
|
report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
|
|
10407
11577
|
isLocalCredentialProblem: isLocalCredentialProblem2,
|
|
10408
11578
|
forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
|
|
@@ -10448,7 +11618,7 @@ async function run(options) {
|
|
|
10448
11618
|
}
|
|
10449
11619
|
|
|
10450
11620
|
// src/index.ts
|
|
10451
|
-
var { version } =
|
|
11621
|
+
var { version } = createRequire2(import.meta.url)("../package.json");
|
|
10452
11622
|
var program = new Command();
|
|
10453
11623
|
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
|
|
10454
11624
|
"--endpoint <url>",
|
|
@@ -10476,6 +11646,9 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
10476
11646
|
).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(
|
|
10477
11647
|
"--opencode-start-timeout <seconds>",
|
|
10478
11648
|
"Seconds to wait for OpenCode to become healthy when the runner starts it (default: 180). Env: EVIDENT_OPENCODE_START_TIMEOUT"
|
|
11649
|
+
).option(
|
|
11650
|
+
"--opencode-version <v1|v2>",
|
|
11651
|
+
"Which OpenCode major version to launch: v1 or v2 (default: v1). Env: EVIDENT_OPENCODE_VERSION"
|
|
10479
11652
|
).option("--json", "Output in JSON format").option(
|
|
10480
11653
|
"--session-cleanup-max-age <duration>",
|
|
10481
11654
|
"Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
|
|
@@ -10526,6 +11699,9 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
10526
11699
|
).option(
|
|
10527
11700
|
"--opencode-config-overlay <path>",
|
|
10528
11701
|
"Apply this runner-provided OpenCode config before starting OpenCode."
|
|
11702
|
+
).option(
|
|
11703
|
+
"--credential-sync-marker <path>",
|
|
11704
|
+
"Own the interval credential sync and write this marker once the shutdown flush has finished, so the runner image's lifecycle hooks can wait on it."
|
|
10529
11705
|
).action(
|
|
10530
11706
|
(options) => {
|
|
10531
11707
|
run({
|
|
@@ -10541,6 +11717,7 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
10541
11717
|
// Raw string — validation/precedence is single-sourced in run.ts's
|
|
10542
11718
|
// resolveOpenCodeStartTimeoutMs (flag > EVIDENT_OPENCODE_START_TIMEOUT > 180s default).
|
|
10543
11719
|
opencodeStartTimeout: options.opencodeStartTimeout,
|
|
11720
|
+
opencodeVersion: options.opencodeVersion,
|
|
10544
11721
|
json: options.json,
|
|
10545
11722
|
// Raw strings — the resolver in run.ts single-sources parsing (M1).
|
|
10546
11723
|
sessionCleanupMaxAge: options.sessionCleanupMaxAge,
|
|
@@ -10564,7 +11741,8 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
10564
11741
|
sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10565
11742
|
restoreSessionDb: options.restoreSessionDb,
|
|
10566
11743
|
restoreRunnerCredentials: options.restoreRunnerCredentials,
|
|
10567
|
-
opencodeConfigOverlay: options.opencodeConfigOverlay
|
|
11744
|
+
opencodeConfigOverlay: options.opencodeConfigOverlay,
|
|
11745
|
+
credentialSyncMarker: options.credentialSyncMarker
|
|
10568
11746
|
});
|
|
10569
11747
|
}
|
|
10570
11748
|
);
|