@evident-ai/cli 3.4.1-dev.dbc0a1f → 3.4.1-dev.ee4e02f
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 +6 -0
- package/dist/index.js +1015 -187
- 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,9 +1192,9 @@ 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 homedir5 } from "os";
|
|
1188
|
-
import { isAbsolute as isAbsolute3, join as
|
|
1195
|
+
import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync6 } from "node:fs";
|
|
1196
|
+
import { homedir as homedir5 } from "node:os";
|
|
1197
|
+
import { isAbsolute as isAbsolute3, join as join9, parse, resolve as resolvePath2 } from "node:path";
|
|
1189
1198
|
import chalk6 from "chalk";
|
|
1190
1199
|
|
|
1191
1200
|
// ../../packages/types/src/agents/index.ts
|
|
@@ -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;
|
|
@@ -1495,8 +1505,8 @@ function forwardRunnerActivity(entry, context) {
|
|
|
1495
1505
|
}
|
|
1496
1506
|
|
|
1497
1507
|
// src/lib/opencode/session-db-recovery-report.ts
|
|
1498
|
-
import { readFileSync as readFileSync2, unlinkSync } from "fs";
|
|
1499
|
-
import { join as join2 } from "path";
|
|
1508
|
+
import { readFileSync as readFileSync2, unlinkSync } from "node:fs";
|
|
1509
|
+
import { join as join2 } from "node:path";
|
|
1500
1510
|
function sessionDbRecoveryReportPath(homeDir, env) {
|
|
1501
1511
|
const override = env.EVIDENT_SESSION_DB_RECOVERY_REPORT?.trim();
|
|
1502
1512
|
return override || join2(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
|
|
@@ -1525,7 +1535,14 @@ function drainSessionDbRecoveryReport({
|
|
|
1525
1535
|
skippedLines++;
|
|
1526
1536
|
return [];
|
|
1527
1537
|
}
|
|
1528
|
-
return [
|
|
1538
|
+
return [
|
|
1539
|
+
{
|
|
1540
|
+
...value,
|
|
1541
|
+
provenance_reason: value.provenance_reason ?? null,
|
|
1542
|
+
provenance_migration_delta: value.provenance_migration_delta ?? null,
|
|
1543
|
+
replication_suspended: value.replication_suspended ?? false
|
|
1544
|
+
}
|
|
1545
|
+
];
|
|
1529
1546
|
} catch (error2) {
|
|
1530
1547
|
skippedLines++;
|
|
1531
1548
|
console.error(
|
|
@@ -1628,6 +1645,12 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1628
1645
|
metadata: withoutContractFields(record),
|
|
1629
1646
|
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
1647
|
};
|
|
1648
|
+
case "schema_provenance_mismatch":
|
|
1649
|
+
return {
|
|
1650
|
+
level,
|
|
1651
|
+
metadata: withoutContractFields(record),
|
|
1652
|
+
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.`
|
|
1653
|
+
};
|
|
1631
1654
|
default:
|
|
1632
1655
|
return null;
|
|
1633
1656
|
}
|
|
@@ -1642,7 +1665,8 @@ var OUTCOMES = /* @__PURE__ */ new Set([
|
|
|
1642
1665
|
"fresh_session_db",
|
|
1643
1666
|
"history_rolled_back",
|
|
1644
1667
|
"restore_misconfigured",
|
|
1645
|
-
"session_db_boot_refused"
|
|
1668
|
+
"session_db_boot_refused",
|
|
1669
|
+
"schema_provenance_mismatch"
|
|
1646
1670
|
]);
|
|
1647
1671
|
var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
|
|
1648
1672
|
var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
|
|
@@ -1660,7 +1684,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
|
|
|
1660
1684
|
function isSessionDbRecoveryRecord(value) {
|
|
1661
1685
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1662
1686
|
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(
|
|
1687
|
+
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
1688
|
(field) => record[field] === null || typeof record[field] === "string"
|
|
1665
1689
|
);
|
|
1666
1690
|
}
|
|
@@ -1695,13 +1719,13 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
1695
1719
|
}
|
|
1696
1720
|
|
|
1697
1721
|
// 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";
|
|
1722
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
1723
|
+
import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
|
|
1724
|
+
import { homedir as homedir2 } from "node:os";
|
|
1725
|
+
import { dirname as dirname2, resolve as resolvePath } from "node:path";
|
|
1702
1726
|
|
|
1703
1727
|
// src/lib/runner-synchroniser.ts
|
|
1704
|
-
import { spawn } from "child_process";
|
|
1728
|
+
import { spawn } from "node:child_process";
|
|
1705
1729
|
function appendError(stderr, error2) {
|
|
1706
1730
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1707
1731
|
return stderr === "" ? message : `${stderr}
|
|
@@ -1714,10 +1738,14 @@ function runSynchroniser(args, opts) {
|
|
|
1714
1738
|
let stderr = "";
|
|
1715
1739
|
let settled = false;
|
|
1716
1740
|
const timer = {};
|
|
1741
|
+
let abortListener;
|
|
1742
|
+
let spawnListener;
|
|
1717
1743
|
const finish = (result) => {
|
|
1718
1744
|
if (settled) return;
|
|
1719
1745
|
settled = true;
|
|
1720
1746
|
if (timer.handle) clearTimeout(timer.handle);
|
|
1747
|
+
if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
|
|
1748
|
+
if (spawnListener) child.removeListener("spawn", spawnListener);
|
|
1721
1749
|
resolve4(result);
|
|
1722
1750
|
};
|
|
1723
1751
|
try {
|
|
@@ -1743,6 +1771,25 @@ function runSynchroniser(args, opts) {
|
|
|
1743
1771
|
child.once("close", (code) => {
|
|
1744
1772
|
finish({ code, stdout, stderr, timedOut: false });
|
|
1745
1773
|
});
|
|
1774
|
+
if (opts.signal) {
|
|
1775
|
+
const killChild = () => {
|
|
1776
|
+
if (child.pid === void 0) {
|
|
1777
|
+
if (!spawnListener) {
|
|
1778
|
+
spawnListener = killChild;
|
|
1779
|
+
child.once("spawn", spawnListener);
|
|
1780
|
+
}
|
|
1781
|
+
return;
|
|
1782
|
+
}
|
|
1783
|
+
child.kill("SIGKILL");
|
|
1784
|
+
};
|
|
1785
|
+
abortListener = killChild;
|
|
1786
|
+
if (opts.signal.aborted) {
|
|
1787
|
+
abortListener();
|
|
1788
|
+
} else {
|
|
1789
|
+
opts.signal.addEventListener("abort", abortListener, { once: true });
|
|
1790
|
+
if (opts.signal.aborted) abortListener();
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1746
1793
|
timer.handle = setTimeout(
|
|
1747
1794
|
() => {
|
|
1748
1795
|
child.kill("SIGKILL");
|
|
@@ -1780,6 +1827,8 @@ function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
|
|
|
1780
1827
|
quarantined_bytes: null,
|
|
1781
1828
|
verified_restore_point: null,
|
|
1782
1829
|
restore_points_tried: null,
|
|
1830
|
+
provenance_reason: null,
|
|
1831
|
+
provenance_migration_delta: null,
|
|
1783
1832
|
replication_suspended: stage === "restore"
|
|
1784
1833
|
});
|
|
1785
1834
|
}
|
|
@@ -2200,6 +2249,131 @@ async function restoreAndVerifySessionDb(options) {
|
|
|
2200
2249
|
return { verifyFatal: await verifySessionDb(options, configPath, env) };
|
|
2201
2250
|
}
|
|
2202
2251
|
|
|
2252
|
+
// src/lib/opencode/session-db-provenance.ts
|
|
2253
|
+
import { createRequire } from "node:module";
|
|
2254
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "node:fs";
|
|
2255
|
+
import { dirname as dirname3, join as join3 } from "node:path";
|
|
2256
|
+
var require2 = createRequire(import.meta.url);
|
|
2257
|
+
function readSessionDbMigrationIds(dbPath) {
|
|
2258
|
+
let db;
|
|
2259
|
+
try {
|
|
2260
|
+
const { DatabaseSync } = require2("node:sqlite");
|
|
2261
|
+
db = new DatabaseSync(dbPath, { readOnly: true });
|
|
2262
|
+
const columns = db.prepare("PRAGMA table_info(migration)").all();
|
|
2263
|
+
const hasExpectedShape = columns.length === 2 && columns.some(
|
|
2264
|
+
(column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
|
|
2265
|
+
) && columns.some(
|
|
2266
|
+
(column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
|
|
2267
|
+
);
|
|
2268
|
+
if (!hasExpectedShape) {
|
|
2269
|
+
console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
|
|
2270
|
+
return null;
|
|
2271
|
+
}
|
|
2272
|
+
const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
|
|
2273
|
+
if (rows.some((row) => typeof row.id !== "string")) return null;
|
|
2274
|
+
return rows.map((row) => row.id);
|
|
2275
|
+
} catch (error2) {
|
|
2276
|
+
console.warn(
|
|
2277
|
+
`[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2278
|
+
);
|
|
2279
|
+
return null;
|
|
2280
|
+
} finally {
|
|
2281
|
+
try {
|
|
2282
|
+
db?.close();
|
|
2283
|
+
} catch (error2) {
|
|
2284
|
+
console.warn(
|
|
2285
|
+
`[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2286
|
+
);
|
|
2287
|
+
}
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
function sessionDbProvenanceStatePath(homeDir, env) {
|
|
2291
|
+
const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
|
|
2292
|
+
return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
|
|
2293
|
+
}
|
|
2294
|
+
function loadSessionDbProvenanceState(path) {
|
|
2295
|
+
let value;
|
|
2296
|
+
try {
|
|
2297
|
+
value = JSON.parse(readFileSync3(path, "utf8"));
|
|
2298
|
+
} catch (error2) {
|
|
2299
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
|
|
2300
|
+
console.error(
|
|
2301
|
+
`[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2302
|
+
);
|
|
2303
|
+
return {};
|
|
2304
|
+
}
|
|
2305
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2306
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2307
|
+
return {};
|
|
2308
|
+
}
|
|
2309
|
+
const state = {};
|
|
2310
|
+
for (const [dbPath, record] of Object.entries(value)) {
|
|
2311
|
+
if (!isSessionDbProvenanceRecord(record)) {
|
|
2312
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2313
|
+
return {};
|
|
2314
|
+
}
|
|
2315
|
+
state[dbPath] = record;
|
|
2316
|
+
}
|
|
2317
|
+
return state;
|
|
2318
|
+
}
|
|
2319
|
+
function saveSessionDbProvenanceState(path, state) {
|
|
2320
|
+
try {
|
|
2321
|
+
mkdirSync2(dirname3(path), { recursive: true });
|
|
2322
|
+
writeFileSync2(path, `${JSON.stringify(state, null, 2)}
|
|
2323
|
+
`, "utf8");
|
|
2324
|
+
} catch (error2) {
|
|
2325
|
+
console.error(
|
|
2326
|
+
`[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2327
|
+
);
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
function evaluateSessionDbProvenance(input) {
|
|
2331
|
+
const { currentVersion, currentIds, previous } = input;
|
|
2332
|
+
if (!previous) return { anomaly: false, reason: null };
|
|
2333
|
+
const current = new Set(currentIds);
|
|
2334
|
+
const prior = new Set(previous.migrationIds);
|
|
2335
|
+
for (const id of prior) {
|
|
2336
|
+
if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
|
|
2337
|
+
}
|
|
2338
|
+
if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
|
|
2339
|
+
return { anomaly: true, reason: "foreign-version-migrations" };
|
|
2340
|
+
}
|
|
2341
|
+
return { anomaly: false, reason: null };
|
|
2342
|
+
}
|
|
2343
|
+
function checkSessionDbProvenance(input) {
|
|
2344
|
+
const { dbPath, currentVersion, homeDir, env } = input;
|
|
2345
|
+
const path = sessionDbProvenanceStatePath(homeDir, env);
|
|
2346
|
+
const state = loadSessionDbProvenanceState(path);
|
|
2347
|
+
const previous = state[dbPath];
|
|
2348
|
+
const currentIds = readSessionDbMigrationIds(dbPath);
|
|
2349
|
+
if (currentIds === null) {
|
|
2350
|
+
return {
|
|
2351
|
+
anomaly: false,
|
|
2352
|
+
reason: null,
|
|
2353
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2354
|
+
migrationDelta: null
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
|
|
2358
|
+
const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
|
|
2359
|
+
state[dbPath] = {
|
|
2360
|
+
opencodeVersion: currentVersion,
|
|
2361
|
+
migrationIds: currentIds,
|
|
2362
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2363
|
+
};
|
|
2364
|
+
saveSessionDbProvenanceState(path, state);
|
|
2365
|
+
return {
|
|
2366
|
+
...decision,
|
|
2367
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2368
|
+
migrationDelta
|
|
2369
|
+
};
|
|
2370
|
+
}
|
|
2371
|
+
function isSessionDbProvenanceRecord(value) {
|
|
2372
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
2373
|
+
const record = value;
|
|
2374
|
+
return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2203
2377
|
// src/lib/opencode/opencode-version-gate.ts
|
|
2204
2378
|
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
2205
2379
|
function isQueueValidatedVersion(version2) {
|
|
@@ -2978,6 +3152,44 @@ function findLastAssistantReplyFor(messages, userMessageId) {
|
|
|
2978
3152
|
}
|
|
2979
3153
|
return lastOk ?? last;
|
|
2980
3154
|
}
|
|
3155
|
+
function collectSubagentSessions(messages, userMessageId) {
|
|
3156
|
+
if (!messages || messages.length === 0) return [];
|
|
3157
|
+
const byParent = messages.filter(
|
|
3158
|
+
(message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
|
|
3159
|
+
);
|
|
3160
|
+
const assistants = byParent.length > 0 ? byParent : [];
|
|
3161
|
+
if (assistants.length === 0) {
|
|
3162
|
+
const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
|
|
3163
|
+
if (userIndex === -1) return [];
|
|
3164
|
+
for (let i = userIndex + 1; i < messages.length; i++) {
|
|
3165
|
+
const message = messages[i];
|
|
3166
|
+
if (roleOf(message) === "user") break;
|
|
3167
|
+
if (roleOf(message) === "assistant") assistants.push(message);
|
|
3168
|
+
}
|
|
3169
|
+
}
|
|
3170
|
+
const refs = [];
|
|
3171
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3172
|
+
for (const message of assistants) {
|
|
3173
|
+
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
3174
|
+
for (const part of parts) {
|
|
3175
|
+
if (!part || typeof part !== "object" || part.type !== "tool" || part.tool !== "task")
|
|
3176
|
+
continue;
|
|
3177
|
+
const state = part.state;
|
|
3178
|
+
if (!state || typeof state !== "object") continue;
|
|
3179
|
+
const metadata = state.metadata;
|
|
3180
|
+
if (!metadata || typeof metadata !== "object") continue;
|
|
3181
|
+
const sessionId = metadata.sessionId;
|
|
3182
|
+
if (typeof sessionId !== "string" || sessionId.length === 0 || seen.has(sessionId)) continue;
|
|
3183
|
+
seen.add(sessionId);
|
|
3184
|
+
const start = state.time?.start;
|
|
3185
|
+
refs.push({
|
|
3186
|
+
sessionId,
|
|
3187
|
+
startedAtMs: typeof start === "number" && Number.isFinite(start) ? start : null
|
|
3188
|
+
});
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
3191
|
+
return refs;
|
|
3192
|
+
}
|
|
2981
3193
|
function messageUsage(messages, userMessageId) {
|
|
2982
3194
|
if (!messages || messages.length === 0) return null;
|
|
2983
3195
|
const byParentAll = messages.filter(
|
|
@@ -3106,8 +3318,7 @@ function isAbortedTerminalReply(messages, userMessageId) {
|
|
|
3106
3318
|
}
|
|
3107
3319
|
return false;
|
|
3108
3320
|
}
|
|
3109
|
-
function
|
|
3110
|
-
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
3321
|
+
function classifyReplyAuthError(reply) {
|
|
3111
3322
|
const error2 = errorOf(reply);
|
|
3112
3323
|
if (error2 == null || typeof error2 !== "object") return null;
|
|
3113
3324
|
const e = error2;
|
|
@@ -3132,6 +3343,32 @@ function messageFailure(messages, userMessageId) {
|
|
|
3132
3343
|
}
|
|
3133
3344
|
return null;
|
|
3134
3345
|
}
|
|
3346
|
+
function messageFailure(messages, userMessageId) {
|
|
3347
|
+
return classifyReplyAuthError(findLastAssistantReplyFor(messages, userMessageId));
|
|
3348
|
+
}
|
|
3349
|
+
function findLatestSubagentAuthOutcome(messages, sinceMs) {
|
|
3350
|
+
if (!messages || messages.length === 0) return null;
|
|
3351
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
3352
|
+
const message = messages[i];
|
|
3353
|
+
if (roleOf(message) !== "assistant") continue;
|
|
3354
|
+
const created = createdOf(message);
|
|
3355
|
+
if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
|
|
3356
|
+
const failure = classifyReplyAuthError(message);
|
|
3357
|
+
if (failure) {
|
|
3358
|
+
if (!failure.providerId) return null;
|
|
3359
|
+
return { providerId: failure.providerId, outcome: "failed", failure };
|
|
3360
|
+
}
|
|
3361
|
+
const providerId = message.info?.providerID;
|
|
3362
|
+
if (errorOf(message) == null && typeof providerId === "string" && providerId.length > 0) {
|
|
3363
|
+
return { providerId, outcome: "succeeded" };
|
|
3364
|
+
}
|
|
3365
|
+
return null;
|
|
3366
|
+
}
|
|
3367
|
+
return null;
|
|
3368
|
+
}
|
|
3369
|
+
function findSubagentAuthOutcome(messages, sinceMs) {
|
|
3370
|
+
return findLatestSubagentAuthOutcome(messages, sinceMs);
|
|
3371
|
+
}
|
|
3135
3372
|
function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProviderId, replyModelId = null) {
|
|
3136
3373
|
if (classified != null) return classified;
|
|
3137
3374
|
if (hasConfiguredProvider !== false) return null;
|
|
@@ -3285,11 +3522,11 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
|
3285
3522
|
}
|
|
3286
3523
|
|
|
3287
3524
|
// src/lib/opencode/session-db-size.ts
|
|
3288
|
-
import { statSync as statSync3 } from "fs";
|
|
3289
|
-
import { join as
|
|
3525
|
+
import { statSync as statSync3 } from "node:fs";
|
|
3526
|
+
import { join as join4 } from "node:path";
|
|
3290
3527
|
var LARGE_DB_THRESHOLD_BYTES = 268435456;
|
|
3291
3528
|
function statSessionDbBytes(homeDir) {
|
|
3292
|
-
const dbPath =
|
|
3529
|
+
const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
|
|
3293
3530
|
try {
|
|
3294
3531
|
return statSync3(dbPath).size;
|
|
3295
3532
|
} catch (err) {
|
|
@@ -3317,11 +3554,15 @@ function buildSessionStoreSizeWarning(input) {
|
|
|
3317
3554
|
}
|
|
3318
3555
|
|
|
3319
3556
|
// src/lib/opencode/session-db-reclaim.ts
|
|
3320
|
-
import { statSync as statSync4, statfsSync } from "fs";
|
|
3321
|
-
import { dirname as
|
|
3557
|
+
import { statSync as statSync4, statfsSync } from "node:fs";
|
|
3558
|
+
import { dirname as dirname4 } from "node:path";
|
|
3559
|
+
function errorMessage(error2) {
|
|
3560
|
+
if (!(error2 instanceof Error)) return String(error2);
|
|
3561
|
+
return error2.cause instanceof Error ? error2.cause.message : error2.message;
|
|
3562
|
+
}
|
|
3322
3563
|
function insufficientSpaceReason(dbPath, requiredBytes) {
|
|
3323
3564
|
try {
|
|
3324
|
-
const fsStats = statfsSync(
|
|
3565
|
+
const fsStats = statfsSync(dirname4(dbPath));
|
|
3325
3566
|
const availableBytes = fsStats.bavail * fsStats.bsize;
|
|
3326
3567
|
if (availableBytes < requiredBytes) {
|
|
3327
3568
|
return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
|
|
@@ -3344,17 +3585,17 @@ async function probeReclaimAvailability(input) {
|
|
|
3344
3585
|
const { dbPath, requiredBytes } = input;
|
|
3345
3586
|
let sqlite;
|
|
3346
3587
|
try {
|
|
3347
|
-
sqlite = await import("sqlite");
|
|
3588
|
+
sqlite = await import("node:sqlite");
|
|
3348
3589
|
} catch (err) {
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
return "sqlite-unavailable";
|
|
3590
|
+
const detail = `Node ${process.version}: ${errorMessage(err)}`;
|
|
3591
|
+
console.warn(`[probeReclaimAvailability] node:sqlite unavailable for ${dbPath}: ` + detail);
|
|
3592
|
+
return { reason: "sqlite-unavailable", detail };
|
|
3353
3593
|
}
|
|
3354
3594
|
let autoVacuum = null;
|
|
3355
3595
|
try {
|
|
3356
3596
|
const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
|
|
3357
3597
|
try {
|
|
3598
|
+
db.exec("PRAGMA busy_timeout=5000");
|
|
3358
3599
|
autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
|
|
3359
3600
|
} finally {
|
|
3360
3601
|
db.close();
|
|
@@ -3365,23 +3606,25 @@ async function probeReclaimAvailability(input) {
|
|
|
3365
3606
|
);
|
|
3366
3607
|
}
|
|
3367
3608
|
if (autoVacuum !== 0) return null;
|
|
3368
|
-
return insufficientSpaceReason(dbPath, requiredBytes) !== null ? "insufficient-disk-space" : null;
|
|
3609
|
+
return insufficientSpaceReason(dbPath, requiredBytes) !== null ? { reason: "insufficient-disk-space" } : null;
|
|
3369
3610
|
}
|
|
3370
3611
|
async function reclaimSessionDbSpace(input) {
|
|
3371
3612
|
const { dbPath, maxPages, allowFullVacuum = true } = input;
|
|
3372
3613
|
let sqlite;
|
|
3373
3614
|
try {
|
|
3374
|
-
sqlite = await import("sqlite");
|
|
3615
|
+
sqlite = await import("node:sqlite");
|
|
3375
3616
|
} catch (err) {
|
|
3617
|
+
const detail = `Node ${process.version}: ${errorMessage(err)}`;
|
|
3376
3618
|
console.warn(
|
|
3377
|
-
`[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${
|
|
3619
|
+
`[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${detail}`
|
|
3378
3620
|
);
|
|
3379
|
-
return { ok: false, skipped: "sqlite-unavailable" };
|
|
3621
|
+
return { ok: false, skipped: "sqlite-unavailable", detail };
|
|
3380
3622
|
}
|
|
3381
3623
|
const { DatabaseSync } = sqlite;
|
|
3382
3624
|
let db;
|
|
3383
3625
|
try {
|
|
3384
3626
|
db = new DatabaseSync(dbPath);
|
|
3627
|
+
db.exec("PRAGMA busy_timeout=5000");
|
|
3385
3628
|
const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
|
|
3386
3629
|
if (autoVacuum === 0) {
|
|
3387
3630
|
if (!allowFullVacuum) {
|
|
@@ -3418,10 +3661,12 @@ async function reclaimSessionDbSpace(input) {
|
|
|
3418
3661
|
);
|
|
3419
3662
|
return { ok: false, skipped: "auto-vacuum-not-applicable" };
|
|
3420
3663
|
} catch (err) {
|
|
3421
|
-
console.error(
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3664
|
+
console.error(`[reclaimSessionDbSpace] reclaim failed for ${dbPath}: ` + errorMessage(err));
|
|
3665
|
+
return {
|
|
3666
|
+
ok: false,
|
|
3667
|
+
skipped: "reclaim-error",
|
|
3668
|
+
detail: errorMessage(err)
|
|
3669
|
+
};
|
|
3425
3670
|
} finally {
|
|
3426
3671
|
db?.close();
|
|
3427
3672
|
}
|
|
@@ -3462,7 +3707,6 @@ var StreamForwarder = class {
|
|
|
3462
3707
|
handleFrame(frame) {
|
|
3463
3708
|
switch (frame.type) {
|
|
3464
3709
|
case "open":
|
|
3465
|
-
this.callbacks.onOpen?.(frame.sid, frame.method, frame.path);
|
|
3466
3710
|
void this.handleOpen(frame);
|
|
3467
3711
|
break;
|
|
3468
3712
|
case "req_data":
|
|
@@ -3498,12 +3742,21 @@ var StreamForwarder = class {
|
|
|
3498
3742
|
const { sid, method, path, headers, has_body } = frame;
|
|
3499
3743
|
const correlationId = headers?.[CORRELATION_ID_HEADER];
|
|
3500
3744
|
const startedAt = Date.now();
|
|
3745
|
+
if (path !== TUNNEL_DRAIN_PING_PATH && path !== TUNNEL_USAGE_REARM_PING_PATH) {
|
|
3746
|
+
this.callbacks.onOpen?.(sid, method, path);
|
|
3747
|
+
}
|
|
3501
3748
|
if (path === TUNNEL_DRAIN_PING_PATH) {
|
|
3502
3749
|
this.callbacks.onDrainPing?.();
|
|
3503
3750
|
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
3504
3751
|
this.send({ type: "res_end", sid });
|
|
3505
3752
|
return;
|
|
3506
3753
|
}
|
|
3754
|
+
if (path === TUNNEL_USAGE_REARM_PING_PATH) {
|
|
3755
|
+
this.callbacks.onUsageRearmPing?.();
|
|
3756
|
+
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
3757
|
+
this.send({ type: "res_end", sid });
|
|
3758
|
+
return;
|
|
3759
|
+
}
|
|
3507
3760
|
if (process.env.DEBUG) {
|
|
3508
3761
|
log("debug", "agent_request", {
|
|
3509
3762
|
correlation_id: correlationId,
|
|
@@ -3648,7 +3901,8 @@ function connectTunnel(options) {
|
|
|
3648
3901
|
onResponse,
|
|
3649
3902
|
onInfo,
|
|
3650
3903
|
onWarning,
|
|
3651
|
-
onDrainPing
|
|
3904
|
+
onDrainPing,
|
|
3905
|
+
onUsageRearmPing
|
|
3652
3906
|
} = options;
|
|
3653
3907
|
const tunnelUrl = getTunnelUrlConfig();
|
|
3654
3908
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
@@ -3660,7 +3914,8 @@ function connectTunnel(options) {
|
|
|
3660
3914
|
});
|
|
3661
3915
|
const forwarder = new StreamForwarder(ws, port, {
|
|
3662
3916
|
onHead: () => onResponse?.(),
|
|
3663
|
-
onDrainPing: () => onDrainPing?.()
|
|
3917
|
+
onDrainPing: () => onDrainPing?.(),
|
|
3918
|
+
onUsageRearmPing: () => onUsageRearmPing?.()
|
|
3664
3919
|
});
|
|
3665
3920
|
const connectionTimeout = setTimeout(() => {
|
|
3666
3921
|
ws.close();
|
|
@@ -3703,8 +3958,8 @@ function connectTunnel(options) {
|
|
|
3703
3958
|
try {
|
|
3704
3959
|
message = JSON.parse(data.toString());
|
|
3705
3960
|
} catch (error2) {
|
|
3706
|
-
const
|
|
3707
|
-
onError?.(`Failed to handle message: ${
|
|
3961
|
+
const errorMessage3 = error2 instanceof Error ? error2.message : "Unknown error";
|
|
3962
|
+
onError?.(`Failed to handle message: ${errorMessage3}`);
|
|
3708
3963
|
return;
|
|
3709
3964
|
}
|
|
3710
3965
|
if (isStreamFrame(message)) {
|
|
@@ -3821,6 +4076,7 @@ var RunnerConnection = class {
|
|
|
3821
4076
|
onError: (error2) => events.onError?.(error2),
|
|
3822
4077
|
onResponse: () => events.onResponse?.(),
|
|
3823
4078
|
onDrainPing: () => events.onDrainPing?.(),
|
|
4079
|
+
onUsageRearmPing: () => events.onUsageRearmPing?.(),
|
|
3824
4080
|
onInfo: (message) => events.onInfo?.(message),
|
|
3825
4081
|
onWarning: (message) => events.onWarning?.(message)
|
|
3826
4082
|
});
|
|
@@ -3847,10 +4103,10 @@ var RunnerConnection = class {
|
|
|
3847
4103
|
};
|
|
3848
4104
|
|
|
3849
4105
|
// src/lib/tunnel/ready-marker.ts
|
|
3850
|
-
import { writeFileSync as
|
|
4106
|
+
import { writeFileSync as writeFileSync3 } from "node:fs";
|
|
3851
4107
|
function writeTunnelReadyMarker(path, agentId) {
|
|
3852
4108
|
try {
|
|
3853
|
-
|
|
4109
|
+
writeFileSync3(path, `${agentId}
|
|
3854
4110
|
`);
|
|
3855
4111
|
return { ok: true };
|
|
3856
4112
|
} catch (error2) {
|
|
@@ -3859,7 +4115,7 @@ function writeTunnelReadyMarker(path, agentId) {
|
|
|
3859
4115
|
}
|
|
3860
4116
|
|
|
3861
4117
|
// src/lib/replication.ts
|
|
3862
|
-
import { spawn as spawn4 } from "child_process";
|
|
4118
|
+
import { spawn as spawn4 } from "node:child_process";
|
|
3863
4119
|
function startSessionDbReplication(configPath) {
|
|
3864
4120
|
return spawn4("litestream", ["replicate", "-config", configPath], {
|
|
3865
4121
|
stdio: "inherit"
|
|
@@ -3875,7 +4131,7 @@ async function stopSessionDbReplication(child, timeoutMs) {
|
|
|
3875
4131
|
}
|
|
3876
4132
|
|
|
3877
4133
|
// src/lib/process-liveness.ts
|
|
3878
|
-
import { readFileSync as
|
|
4134
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
3879
4135
|
function isProcessAlive(pid) {
|
|
3880
4136
|
try {
|
|
3881
4137
|
process.kill(pid, 0);
|
|
@@ -3890,7 +4146,7 @@ function isProcessAlive(pid) {
|
|
|
3890
4146
|
}
|
|
3891
4147
|
if (process.platform !== "linux") return true;
|
|
3892
4148
|
try {
|
|
3893
|
-
const status2 =
|
|
4149
|
+
const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
|
|
3894
4150
|
return !/^State:\s+Z(?:\s|$)/m.test(status2);
|
|
3895
4151
|
} catch (error2) {
|
|
3896
4152
|
console.error(
|
|
@@ -3901,9 +4157,9 @@ function isProcessAlive(pid) {
|
|
|
3901
4157
|
}
|
|
3902
4158
|
|
|
3903
4159
|
// src/lib/openai-usage.ts
|
|
3904
|
-
import { readFileSync as
|
|
3905
|
-
import { homedir as homedir3 } from "os";
|
|
3906
|
-
import { join as
|
|
4160
|
+
import { readFileSync as readFileSync5 } from "node:fs";
|
|
4161
|
+
import { homedir as homedir3 } from "node:os";
|
|
4162
|
+
import { join as join5 } from "node:path";
|
|
3907
4163
|
var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
3908
4164
|
var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
|
|
3909
4165
|
var OpenAiUsageError = class extends Error {
|
|
@@ -3917,7 +4173,7 @@ function isLocalCredentialProblem2(err) {
|
|
|
3917
4173
|
}
|
|
3918
4174
|
function readOpenCodeChatGptCredentials() {
|
|
3919
4175
|
try {
|
|
3920
|
-
const raw =
|
|
4176
|
+
const raw = readFileSync5(join5(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
|
|
3921
4177
|
let parsed;
|
|
3922
4178
|
try {
|
|
3923
4179
|
parsed = JSON.parse(raw);
|
|
@@ -3939,6 +4195,23 @@ function readOpenCodeChatGptCredentials() {
|
|
|
3939
4195
|
return null;
|
|
3940
4196
|
}
|
|
3941
4197
|
}
|
|
4198
|
+
function parseChatGptIdentity(accessToken) {
|
|
4199
|
+
const segments = accessToken.split(".");
|
|
4200
|
+
if (segments.length !== 3) return null;
|
|
4201
|
+
let payload;
|
|
4202
|
+
try {
|
|
4203
|
+
const parsed = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
|
|
4204
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
4205
|
+
payload = parsed;
|
|
4206
|
+
} catch {
|
|
4207
|
+
return null;
|
|
4208
|
+
}
|
|
4209
|
+
const profile = payload["https://api.openai.com/profile"];
|
|
4210
|
+
const auth = payload["https://api.openai.com/auth"];
|
|
4211
|
+
const ownerEmail = typeof profile?.email === "string" ? profile.email.trim() || null : null;
|
|
4212
|
+
const planType = typeof auth?.chatgpt_plan_type === "string" ? auth.chatgpt_plan_type.trim() || null : null;
|
|
4213
|
+
return ownerEmail === null && planType === null ? null : { ownerEmail, planType };
|
|
4214
|
+
}
|
|
3942
4215
|
function toWindow2(headers, name) {
|
|
3943
4216
|
const utilizationHeader = headers.get(`x-codex-${name}-used-percent`);
|
|
3944
4217
|
const windowMinutesHeader = headers.get(`x-codex-${name}-window-minutes`);
|
|
@@ -4014,6 +4287,7 @@ async function getOpenAiUsage(port) {
|
|
|
4014
4287
|
"credentials_expired"
|
|
4015
4288
|
);
|
|
4016
4289
|
}
|
|
4290
|
+
const subscription = parseChatGptIdentity(credentials2.accessToken);
|
|
4017
4291
|
const models = await resolveProbeModels(port);
|
|
4018
4292
|
if (models.length === 0) {
|
|
4019
4293
|
throw new OpenAiUsageError("No supported OpenAI probe model is available.", "no_probe_model");
|
|
@@ -4046,7 +4320,7 @@ async function getOpenAiUsage(port) {
|
|
|
4046
4320
|
"no_usable_window"
|
|
4047
4321
|
);
|
|
4048
4322
|
}
|
|
4049
|
-
return usage;
|
|
4323
|
+
return { ...usage, subscription };
|
|
4050
4324
|
}
|
|
4051
4325
|
if (res.status === 401) {
|
|
4052
4326
|
throw new OpenAiUsageError(
|
|
@@ -4161,8 +4435,8 @@ function resolveResourceUsageReportingEnabled(flagValue, env) {
|
|
|
4161
4435
|
}
|
|
4162
4436
|
|
|
4163
4437
|
// src/lib/resource-usage.ts
|
|
4164
|
-
import { cpus, totalmem, freemem } from "os";
|
|
4165
|
-
import { statfsSync as statfsSync2 } from "fs";
|
|
4438
|
+
import { cpus, totalmem, freemem } from "node:os";
|
|
4439
|
+
import { statfsSync as statfsSync2 } from "node:fs";
|
|
4166
4440
|
|
|
4167
4441
|
// src/lib/ecs-task-metadata.ts
|
|
4168
4442
|
var ECS_METADATA_TIMEOUT_MS = 2e3;
|
|
@@ -4247,58 +4521,97 @@ function readDisk(homeDir) {
|
|
|
4247
4521
|
};
|
|
4248
4522
|
}
|
|
4249
4523
|
}
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4524
|
+
var CPU_PEAK_WINDOW_MS = 6e4;
|
|
4525
|
+
var CPU_PEAK_WINDOW_SAMPLE_COUNT = 4;
|
|
4526
|
+
var CPU_PEAK_SAMPLE_INTERVAL_MS = CPU_PEAK_WINDOW_MS / CPU_PEAK_WINDOW_SAMPLE_COUNT;
|
|
4527
|
+
function createCpuPeakSampler() {
|
|
4528
|
+
const sampleHistory = new Array(CPU_PEAK_WINDOW_SAMPLE_COUNT);
|
|
4529
|
+
sampleHistory[0] = readCpuSample();
|
|
4530
|
+
let nextSampleIndex = 1;
|
|
4531
|
+
let sampleCount = 1;
|
|
4532
|
+
let peak = null;
|
|
4533
|
+
const timer = setInterval(() => {
|
|
4253
4534
|
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);
|
|
4535
|
+
const sampleFromWindowAgo = sampleCount === CPU_PEAK_WINDOW_SAMPLE_COUNT ? sampleHistory[nextSampleIndex] : void 0;
|
|
4536
|
+
if (sampleFromWindowAgo !== void 0) {
|
|
4537
|
+
const percentage = cpuPercentBetween(sampleFromWindowAgo, current);
|
|
4538
|
+
if (percentage !== null) {
|
|
4539
|
+
peak = peak === null ? percentage : Math.max(peak, percentage);
|
|
4540
|
+
}
|
|
4276
4541
|
}
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4542
|
+
sampleHistory[nextSampleIndex] = current;
|
|
4543
|
+
nextSampleIndex = (nextSampleIndex + 1) % CPU_PEAK_WINDOW_SAMPLE_COUNT;
|
|
4544
|
+
sampleCount = Math.min(sampleCount + 1, CPU_PEAK_WINDOW_SAMPLE_COUNT);
|
|
4545
|
+
}, CPU_PEAK_SAMPLE_INTERVAL_MS);
|
|
4546
|
+
return {
|
|
4547
|
+
takeAndReset: () => {
|
|
4548
|
+
const currentPeak = peak;
|
|
4549
|
+
peak = null;
|
|
4550
|
+
return currentPeak;
|
|
4551
|
+
},
|
|
4552
|
+
stop: () => clearInterval(timer)
|
|
4553
|
+
};
|
|
4554
|
+
}
|
|
4555
|
+
function createResourceUsageCollector(homeDir) {
|
|
4556
|
+
let previous = readCpuSample();
|
|
4557
|
+
const cpuPeakSampler = createCpuPeakSampler();
|
|
4558
|
+
return {
|
|
4559
|
+
collect: async () => {
|
|
4560
|
+
const current = readCpuSample();
|
|
4561
|
+
const hostCpuPercent = cpuPercentBetween(previous, current);
|
|
4562
|
+
const hostCpuPeakPercent = cpuPeakSampler.takeAndReset();
|
|
4563
|
+
const hostCpuCount = cpus().length;
|
|
4564
|
+
previous = current;
|
|
4565
|
+
const disk = readDisk(homeDir);
|
|
4566
|
+
const opencodeDbBytes = statSessionDbBytes(homeDir);
|
|
4567
|
+
const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
|
|
4568
|
+
const warnings = [];
|
|
4569
|
+
if (disk.warning) warnings.push(disk.warning);
|
|
4570
|
+
if (ecsWarning) warnings.push(ecsWarning);
|
|
4571
|
+
let cpuPercent = hostCpuPercent;
|
|
4572
|
+
let cpuPeakPercent = hostCpuPeakPercent;
|
|
4573
|
+
let cpuCount = hostCpuCount;
|
|
4574
|
+
let memoryTotalBytes = totalmem();
|
|
4575
|
+
let memoryAvailableBytes = freemem();
|
|
4576
|
+
if (limits !== null) {
|
|
4577
|
+
cpuCount = limits.cpuCount;
|
|
4578
|
+
memoryTotalBytes = limits.memoryTotalBytes;
|
|
4579
|
+
memoryAvailableBytes = clamp(
|
|
4580
|
+
limits.memoryTotalBytes - (totalmem() - freemem()),
|
|
4581
|
+
0,
|
|
4582
|
+
limits.memoryTotalBytes
|
|
4583
|
+
);
|
|
4584
|
+
cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4585
|
+
cpuPeakPercent = hostCpuPeakPercent === null ? null : clamp(round2(hostCpuPeakPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4586
|
+
}
|
|
4587
|
+
return {
|
|
4588
|
+
usage: {
|
|
4589
|
+
cpuPercent,
|
|
4590
|
+
cpuPeakPercent,
|
|
4591
|
+
cpuCount,
|
|
4592
|
+
memoryTotalBytes,
|
|
4593
|
+
memoryAvailableBytes,
|
|
4594
|
+
diskTotalBytes: disk.totalBytes,
|
|
4595
|
+
diskFreeBytes: disk.freeBytes,
|
|
4596
|
+
opencodeDbBytes
|
|
4597
|
+
},
|
|
4598
|
+
warnings
|
|
4599
|
+
};
|
|
4600
|
+
},
|
|
4601
|
+
stop: cpuPeakSampler.stop
|
|
4289
4602
|
};
|
|
4290
4603
|
}
|
|
4291
4604
|
|
|
4292
4605
|
// src/lib/channels/driver.ts
|
|
4293
|
-
import { homedir as homedir4 } from "os";
|
|
4606
|
+
import { homedir as homedir4 } from "node:os";
|
|
4294
4607
|
|
|
4295
4608
|
// src/lib/runner-file-sync.ts
|
|
4296
|
-
import { join as
|
|
4609
|
+
import { join as join7 } from "node:path";
|
|
4297
4610
|
|
|
4298
4611
|
// src/lib/file-push.ts
|
|
4299
|
-
import { randomUUID } from "crypto";
|
|
4300
|
-
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
4301
|
-
import { basename, dirname as
|
|
4612
|
+
import { randomUUID } from "node:crypto";
|
|
4613
|
+
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "node:fs/promises";
|
|
4614
|
+
import { basename, dirname as dirname5, isAbsolute, join as join6, relative, resolve as resolve2, sep } from "node:path";
|
|
4302
4615
|
var FILE_MODE = 384;
|
|
4303
4616
|
var DIRECTORY_MODE = 448;
|
|
4304
4617
|
async function writePushedFile(request) {
|
|
@@ -4329,9 +4642,9 @@ async function writePushedFile(request) {
|
|
|
4329
4642
|
}
|
|
4330
4643
|
try {
|
|
4331
4644
|
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
4332
|
-
|
|
4645
|
+
dirname5(candidate)
|
|
4333
4646
|
);
|
|
4334
|
-
const realTarget =
|
|
4647
|
+
const realTarget = join6(existingAncestor, ...missingSegments, basename(candidate));
|
|
4335
4648
|
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
4336
4649
|
if (allowedDirectory === null) {
|
|
4337
4650
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
@@ -4341,8 +4654,8 @@ async function writePushedFile(request) {
|
|
|
4341
4654
|
}
|
|
4342
4655
|
if (missingSegments.length > 0) {
|
|
4343
4656
|
await createMissingDirectories(existingAncestor, missingSegments);
|
|
4344
|
-
const realParent = await realpath(
|
|
4345
|
-
if (realParent !==
|
|
4657
|
+
const realParent = await realpath(dirname5(realTarget));
|
|
4658
|
+
if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
4346
4659
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
4347
4660
|
path: realTarget,
|
|
4348
4661
|
bytes,
|
|
@@ -4367,7 +4680,7 @@ function expandAndValidate(requestedPath, homeDir) {
|
|
|
4367
4680
|
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
4368
4681
|
return null;
|
|
4369
4682
|
}
|
|
4370
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4683
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4371
4684
|
if (expanded.split(/[/\\]/).includes("..")) {
|
|
4372
4685
|
return null;
|
|
4373
4686
|
}
|
|
@@ -4385,7 +4698,7 @@ async function resolveNearestExistingAncestor(directory) {
|
|
|
4385
4698
|
try {
|
|
4386
4699
|
return { existingAncestor: await realpath(current), missingSegments };
|
|
4387
4700
|
} catch (err) {
|
|
4388
|
-
const parent =
|
|
4701
|
+
const parent = dirname5(current);
|
|
4389
4702
|
if (err.code !== "ENOENT" || parent === current) {
|
|
4390
4703
|
throw err;
|
|
4391
4704
|
}
|
|
@@ -4440,13 +4753,13 @@ function contains(realDirectory, realTarget) {
|
|
|
4440
4753
|
async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
4441
4754
|
let current = existingAncestor;
|
|
4442
4755
|
for (const segment of missingSegments) {
|
|
4443
|
-
current =
|
|
4756
|
+
current = join6(current, segment);
|
|
4444
4757
|
await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
|
|
4445
4758
|
await chmod(current, DIRECTORY_MODE);
|
|
4446
4759
|
}
|
|
4447
4760
|
}
|
|
4448
4761
|
async function writeAtomically(realTarget, content) {
|
|
4449
|
-
const temporaryPath =
|
|
4762
|
+
const temporaryPath = join6(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
4450
4763
|
let handle;
|
|
4451
4764
|
try {
|
|
4452
4765
|
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
@@ -4576,12 +4889,12 @@ var NOT_APPLIED = {
|
|
|
4576
4889
|
opencodeAuthApplied: false
|
|
4577
4890
|
};
|
|
4578
4891
|
function isClaudeCredentialPath(requestedPath, homeDir) {
|
|
4579
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4580
|
-
return expanded ===
|
|
4892
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4893
|
+
return expanded === join7(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
|
|
4581
4894
|
}
|
|
4582
4895
|
function isOpenCodeAuthPath(requestedPath, homeDir) {
|
|
4583
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4584
|
-
return expanded ===
|
|
4896
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4897
|
+
return expanded === join7(homeDir, ...OPENCODE_AUTH_SEGMENTS);
|
|
4585
4898
|
}
|
|
4586
4899
|
async function applyOne(options, file) {
|
|
4587
4900
|
const label = `${file.id.slice(0, 8)} (${file.path})`;
|
|
@@ -5108,6 +5421,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5108
5421
|
* and stops opencode.
|
|
5109
5422
|
*/
|
|
5110
5423
|
stopped = false;
|
|
5424
|
+
recycleRequestedFlag = false;
|
|
5111
5425
|
constructor(config) {
|
|
5112
5426
|
this.agentId = config.agentId;
|
|
5113
5427
|
this.port = config.port;
|
|
@@ -5213,6 +5527,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5213
5527
|
let dispatched = 0;
|
|
5214
5528
|
try {
|
|
5215
5529
|
const conversations = await this.getPendingConversations();
|
|
5530
|
+
if (this.recycleRequestedFlag) {
|
|
5531
|
+
this.stop();
|
|
5532
|
+
}
|
|
5216
5533
|
if (conversations.length > 0) {
|
|
5217
5534
|
const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
|
|
5218
5535
|
this.log({
|
|
@@ -5341,6 +5658,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5341
5658
|
stop() {
|
|
5342
5659
|
this.stopped = true;
|
|
5343
5660
|
}
|
|
5661
|
+
/**
|
|
5662
|
+
* The server clears this request when a new MicroVM identity is recorded, so a
|
|
5663
|
+
* same-VM tunnel reconnect does not consume it. This is a plain read rather
|
|
5664
|
+
* than a consume; `run.ts` guards the action once-only.
|
|
5665
|
+
*/
|
|
5666
|
+
get recycleRequested() {
|
|
5667
|
+
return this.recycleRequestedFlag;
|
|
5668
|
+
}
|
|
5344
5669
|
/**
|
|
5345
5670
|
* Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
|
|
5346
5671
|
* graceful shutdown, so a turn whose reply is ready — or completes within the
|
|
@@ -5478,7 +5803,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5478
5803
|
this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
|
|
5479
5804
|
break;
|
|
5480
5805
|
}
|
|
5481
|
-
const
|
|
5806
|
+
const errorMessage3 = err instanceof Error ? err.message : String(err);
|
|
5482
5807
|
this.sessions.delete(conv.id);
|
|
5483
5808
|
this.supersede(conv.id, sessionId);
|
|
5484
5809
|
this.log({
|
|
@@ -5487,7 +5812,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5487
5812
|
conversation_id: conv.id,
|
|
5488
5813
|
message_id: message.id
|
|
5489
5814
|
});
|
|
5490
|
-
await this.markFailed(conv.id, message.id, null,
|
|
5815
|
+
await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
|
|
5491
5816
|
this.log({
|
|
5492
5817
|
level: "warn",
|
|
5493
5818
|
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 +5823,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5498
5823
|
});
|
|
5499
5824
|
this.log({
|
|
5500
5825
|
level: "error",
|
|
5501
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
5826
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage3}`,
|
|
5502
5827
|
conversation_id: conv.id,
|
|
5503
5828
|
message_id: message.id
|
|
5504
5829
|
});
|
|
@@ -5519,14 +5844,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5519
5844
|
this.unconfirmedDispatchFailures.delete(message.id);
|
|
5520
5845
|
this.sessions.delete(conv.id);
|
|
5521
5846
|
this.supersede(conv.id, sessionId);
|
|
5522
|
-
const
|
|
5847
|
+
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
5848
|
this.log({
|
|
5524
5849
|
level: "error",
|
|
5525
|
-
message:
|
|
5850
|
+
message: errorMessage3,
|
|
5526
5851
|
conversation_id: conv.id,
|
|
5527
5852
|
message_id: message.id
|
|
5528
5853
|
});
|
|
5529
|
-
await this.markFailed(conv.id, message.id, null,
|
|
5854
|
+
await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
|
|
5530
5855
|
this.log({
|
|
5531
5856
|
level: "warn",
|
|
5532
5857
|
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 +6185,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5860
6185
|
});
|
|
5861
6186
|
await this.markFailed(conv.id, message.id, sessionId, error2, usage, failure);
|
|
5862
6187
|
}
|
|
6188
|
+
if (ocId !== null) {
|
|
6189
|
+
await this.reportSubagentAuthFailures(conv.id, ocId, message.id, messages);
|
|
6190
|
+
}
|
|
5863
6191
|
} catch (err) {
|
|
5864
6192
|
if (err instanceof ChannelAuthError) throw err;
|
|
5865
6193
|
this.log({
|
|
@@ -6824,6 +7152,12 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6824
7152
|
return;
|
|
6825
7153
|
}
|
|
6826
7154
|
inFlight.done = true;
|
|
7155
|
+
await this.reportSubagentAuthFailures(
|
|
7156
|
+
watcher.conv.id,
|
|
7157
|
+
inFlight.opencodeMessageId,
|
|
7158
|
+
inFlight.evidentMessageId,
|
|
7159
|
+
messages
|
|
7160
|
+
);
|
|
6827
7161
|
}
|
|
6828
7162
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
6829
7163
|
return;
|
|
@@ -7066,6 +7400,12 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7066
7400
|
return;
|
|
7067
7401
|
}
|
|
7068
7402
|
inFlight.done = true;
|
|
7403
|
+
await this.reportSubagentAuthFailures(
|
|
7404
|
+
watcher.conv.id,
|
|
7405
|
+
inFlight.opencodeMessageId,
|
|
7406
|
+
inFlight.evidentMessageId,
|
|
7407
|
+
messages
|
|
7408
|
+
);
|
|
7069
7409
|
}
|
|
7070
7410
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
7071
7411
|
}
|
|
@@ -7236,6 +7576,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7236
7576
|
});
|
|
7237
7577
|
return;
|
|
7238
7578
|
}
|
|
7579
|
+
await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
|
|
7239
7580
|
this.dontRedispatch.delete(row.id);
|
|
7240
7581
|
void this.postSignal(row.conversation_id, row.id, "readopt_failed");
|
|
7241
7582
|
return;
|
|
@@ -7397,6 +7738,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7397
7738
|
});
|
|
7398
7739
|
return;
|
|
7399
7740
|
}
|
|
7741
|
+
if (ocId !== null) {
|
|
7742
|
+
await this.reportSubagentAuthFailures(row.conversation_id, ocId, row.id, messages);
|
|
7743
|
+
}
|
|
7400
7744
|
this.dontRedispatch.delete(row.id);
|
|
7401
7745
|
void this.postSignal(row.conversation_id, row.id, "readopt_done");
|
|
7402
7746
|
}
|
|
@@ -7490,14 +7834,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7490
7834
|
this.unconfirmedDispatchFailures.delete(row.id);
|
|
7491
7835
|
this.sessions.delete(readoptConv.id);
|
|
7492
7836
|
this.supersede(readoptConv.id, sessionId);
|
|
7493
|
-
const
|
|
7837
|
+
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
7838
|
this.log({
|
|
7495
7839
|
level: "error",
|
|
7496
|
-
message:
|
|
7840
|
+
message: errorMessage3,
|
|
7497
7841
|
conversation_id: row.conversation_id,
|
|
7498
7842
|
message_id: row.id
|
|
7499
7843
|
});
|
|
7500
|
-
await this.markFailed(row.conversation_id, row.id, null,
|
|
7844
|
+
await this.markFailed(row.conversation_id, row.id, null, errorMessage3).catch((markErr) => {
|
|
7501
7845
|
this.log({
|
|
7502
7846
|
level: "warn",
|
|
7503
7847
|
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 +8456,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
8112
8456
|
throw new Error(`Failed to get pending conversations: HTTP ${res.status}`);
|
|
8113
8457
|
}
|
|
8114
8458
|
const data = await res.json();
|
|
8459
|
+
this.recycleRequestedFlag = data.recycle_requested === true;
|
|
8115
8460
|
let conversations = data.conversations;
|
|
8116
8461
|
if (this.conversationFilter) {
|
|
8117
8462
|
conversations = conversations.filter((c) => c.id === this.conversationFilter);
|
|
@@ -8347,6 +8692,111 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
8347
8692
|
reply?.info?.modelID ?? null
|
|
8348
8693
|
);
|
|
8349
8694
|
}
|
|
8695
|
+
async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
|
|
8696
|
+
const providerId = failure.providerId ?? "(unknown)";
|
|
8697
|
+
try {
|
|
8698
|
+
const res = await this.fetchImpl(
|
|
8699
|
+
`${this.apiUrl}/runners/${this.agentId}/model-auth-failures`,
|
|
8700
|
+
{
|
|
8701
|
+
method: "POST",
|
|
8702
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
8703
|
+
body: JSON.stringify({
|
|
8704
|
+
provider_id: failure.providerId,
|
|
8705
|
+
model_id: failure.modelId,
|
|
8706
|
+
reason: failure.reason
|
|
8707
|
+
})
|
|
8708
|
+
}
|
|
8709
|
+
);
|
|
8710
|
+
if (!res.ok) {
|
|
8711
|
+
this.log({
|
|
8712
|
+
level: "warn",
|
|
8713
|
+
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)})`,
|
|
8714
|
+
conversation_id: conversationId,
|
|
8715
|
+
message_id: messageId
|
|
8716
|
+
});
|
|
8717
|
+
}
|
|
8718
|
+
} catch (err) {
|
|
8719
|
+
this.log({
|
|
8720
|
+
level: "warn",
|
|
8721
|
+
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)}`,
|
|
8722
|
+
conversation_id: conversationId,
|
|
8723
|
+
message_id: messageId
|
|
8724
|
+
});
|
|
8725
|
+
}
|
|
8726
|
+
}
|
|
8727
|
+
async clearSubagentModelAuthFailure(providerId, conversationId, messageId) {
|
|
8728
|
+
try {
|
|
8729
|
+
const res = await this.fetchImpl(
|
|
8730
|
+
`${this.apiUrl}/runners/${this.agentId}/model-auth-failures/${encodeURIComponent(providerId)}`,
|
|
8731
|
+
{
|
|
8732
|
+
method: "DELETE",
|
|
8733
|
+
headers: { Authorization: this.getAuthHeader() }
|
|
8734
|
+
}
|
|
8735
|
+
);
|
|
8736
|
+
if (!res.ok) {
|
|
8737
|
+
this.log({
|
|
8738
|
+
level: "warn",
|
|
8739
|
+
message: `Sub-agent model-auth clear for provider ${providerId} returned HTTP ${res.status} (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)})`,
|
|
8740
|
+
conversation_id: conversationId,
|
|
8741
|
+
message_id: messageId
|
|
8742
|
+
});
|
|
8743
|
+
}
|
|
8744
|
+
} catch (err) {
|
|
8745
|
+
this.log({
|
|
8746
|
+
level: "warn",
|
|
8747
|
+
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)}`,
|
|
8748
|
+
conversation_id: conversationId,
|
|
8749
|
+
message_id: messageId
|
|
8750
|
+
});
|
|
8751
|
+
}
|
|
8752
|
+
}
|
|
8753
|
+
async reportSubagentAuthFailures(conversationId, opencodeMessageId, evidentMessageId, messages) {
|
|
8754
|
+
const refs = collectSubagentSessions(messages, opencodeMessageId);
|
|
8755
|
+
if (refs.length === 0) return;
|
|
8756
|
+
const failedProviders = /* @__PURE__ */ new Map();
|
|
8757
|
+
const succeededProviders = /* @__PURE__ */ new Set();
|
|
8758
|
+
for (const ref of refs) {
|
|
8759
|
+
try {
|
|
8760
|
+
const childMessages = await getSessionMessages(this.port, ref.sessionId);
|
|
8761
|
+
if (childMessages === null) {
|
|
8762
|
+
this.log({
|
|
8763
|
+
level: "debug",
|
|
8764
|
+
message: `Could not read sub-agent session ${ref.sessionId} while checking credential failures \u2014 skipping it`,
|
|
8765
|
+
conversation_id: conversationId,
|
|
8766
|
+
message_id: evidentMessageId
|
|
8767
|
+
});
|
|
8768
|
+
continue;
|
|
8769
|
+
}
|
|
8770
|
+
const outcome = findSubagentAuthOutcome(childMessages, ref.startedAtMs);
|
|
8771
|
+
if (!outcome) continue;
|
|
8772
|
+
if (outcome.outcome === "failed") {
|
|
8773
|
+
failedProviders.set(outcome.providerId, outcome.failure);
|
|
8774
|
+
} else {
|
|
8775
|
+
succeededProviders.add(outcome.providerId);
|
|
8776
|
+
}
|
|
8777
|
+
} catch (err) {
|
|
8778
|
+
this.log({
|
|
8779
|
+
level: "warn",
|
|
8780
|
+
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)}`,
|
|
8781
|
+
conversation_id: conversationId,
|
|
8782
|
+
message_id: evidentMessageId
|
|
8783
|
+
});
|
|
8784
|
+
}
|
|
8785
|
+
}
|
|
8786
|
+
for (const [providerId, failure] of failedProviders) {
|
|
8787
|
+
this.log({
|
|
8788
|
+
level: "warn",
|
|
8789
|
+
message: `Sub-agent turn failed on provider ${providerId} (${failure.reason}) \u2014 recording credential evidence`,
|
|
8790
|
+
conversation_id: conversationId,
|
|
8791
|
+
message_id: evidentMessageId
|
|
8792
|
+
});
|
|
8793
|
+
await this.recordSubagentModelAuthFailure(failure, conversationId, evidentMessageId);
|
|
8794
|
+
}
|
|
8795
|
+
for (const providerId of succeededProviders) {
|
|
8796
|
+
if (failedProviders.has(providerId)) continue;
|
|
8797
|
+
await this.clearSubagentModelAuthFailure(providerId, conversationId, evidentMessageId);
|
|
8798
|
+
}
|
|
8799
|
+
}
|
|
8350
8800
|
/**
|
|
8351
8801
|
* Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
|
|
8352
8802
|
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
@@ -8629,8 +9079,8 @@ Port ${port} is already in use.`));
|
|
|
8629
9079
|
}
|
|
8630
9080
|
|
|
8631
9081
|
// src/lib/runner-credentials.ts
|
|
8632
|
-
import { chmodSync as chmodSync2, writeFileSync as
|
|
8633
|
-
import { spawn as spawn5 } from "child_process";
|
|
9082
|
+
import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "node:fs";
|
|
9083
|
+
import { spawn as spawn5 } from "node:child_process";
|
|
8634
9084
|
var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
|
|
8635
9085
|
var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
|
|
8636
9086
|
var GITHUB_PROBE_TIMEOUT_MS = 1e4;
|
|
@@ -8870,8 +9320,8 @@ async function configureGitHubAccess({ env, log: log3 }) {
|
|
|
8870
9320
|
}
|
|
8871
9321
|
try {
|
|
8872
9322
|
env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
|
|
8873
|
-
|
|
8874
|
-
|
|
9323
|
+
writeFileSync4(GIT_CONFIG_GLOBAL, "");
|
|
9324
|
+
writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
|
|
8875
9325
|
chmodSync2(GIT_CREDENTIAL_HELPER, 448);
|
|
8876
9326
|
const config = [
|
|
8877
9327
|
["user.name", env.GIT_USER_NAME ?? "evident-bot"],
|
|
@@ -8902,9 +9352,9 @@ async function configureGitHubAccess({ env, log: log3 }) {
|
|
|
8902
9352
|
}
|
|
8903
9353
|
|
|
8904
9354
|
// src/lib/opencode/config-overlay.ts
|
|
8905
|
-
import { execFileSync as execFileSync2 } from "child_process";
|
|
8906
|
-
import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
|
|
8907
|
-
import { isAbsolute as isAbsolute2, join as
|
|
9355
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
9356
|
+
import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "node:fs";
|
|
9357
|
+
import { isAbsolute as isAbsolute2, join as join8, resolve as resolve3 } from "node:path";
|
|
8908
9358
|
function isFile(filePath) {
|
|
8909
9359
|
return existsSync2(filePath) && statSync5(filePath).isFile();
|
|
8910
9360
|
}
|
|
@@ -8918,7 +9368,7 @@ function applyRunnerOpenCodeConfig({
|
|
|
8918
9368
|
return;
|
|
8919
9369
|
}
|
|
8920
9370
|
const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
|
|
8921
|
-
const target = isFile(
|
|
9371
|
+
const target = isFile(join8(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
|
|
8922
9372
|
if (!isFile(source)) {
|
|
8923
9373
|
log3(
|
|
8924
9374
|
`RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
|
|
@@ -8926,7 +9376,7 @@ function applyRunnerOpenCodeConfig({
|
|
|
8926
9376
|
);
|
|
8927
9377
|
return;
|
|
8928
9378
|
}
|
|
8929
|
-
copyFileSync(source,
|
|
9379
|
+
copyFileSync(source, join8(cwd, target));
|
|
8930
9380
|
try {
|
|
8931
9381
|
execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
8932
9382
|
stdio: "ignore"
|
|
@@ -8935,7 +9385,242 @@ function applyRunnerOpenCodeConfig({
|
|
|
8935
9385
|
const detail = error2 instanceof Error ? error2.message : String(error2);
|
|
8936
9386
|
log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
|
|
8937
9387
|
}
|
|
8938
|
-
log3(`Applied runner OpenCode config ${source} to ${
|
|
9388
|
+
log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
|
|
9389
|
+
}
|
|
9390
|
+
|
|
9391
|
+
// src/lib/credential-sync.ts
|
|
9392
|
+
import { renameSync, writeFileSync as writeFileSync5 } from "node:fs";
|
|
9393
|
+
var CREDENTIAL_SYNC_TICK_TIMEOUT_MS = 3e4;
|
|
9394
|
+
var CREDENTIAL_FLUSH_DEADLINE_MS = 8e3;
|
|
9395
|
+
var CREDENTIAL_FLUSH_ABORT_GRACE_MS = 500;
|
|
9396
|
+
var DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS = 60;
|
|
9397
|
+
var STORES = ["claude", "opencode"];
|
|
9398
|
+
var MAX_FLUSH_PASSES = 2;
|
|
9399
|
+
function outcomesWith(outcome) {
|
|
9400
|
+
return { claude: outcome, opencode: outcome };
|
|
9401
|
+
}
|
|
9402
|
+
function errorMessage2(error2) {
|
|
9403
|
+
return error2 instanceof Error ? error2.message : String(error2);
|
|
9404
|
+
}
|
|
9405
|
+
function waitForSettlement(promise, timeoutMs) {
|
|
9406
|
+
return new Promise((resolve4) => {
|
|
9407
|
+
let settled = false;
|
|
9408
|
+
const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
|
|
9409
|
+
const finish = (value) => {
|
|
9410
|
+
if (settled) return;
|
|
9411
|
+
settled = true;
|
|
9412
|
+
clearTimeout(timer);
|
|
9413
|
+
resolve4(value);
|
|
9414
|
+
};
|
|
9415
|
+
promise.then(
|
|
9416
|
+
() => finish(true),
|
|
9417
|
+
() => finish(true)
|
|
9418
|
+
);
|
|
9419
|
+
});
|
|
9420
|
+
}
|
|
9421
|
+
function writeMarker(markerPath, outcomes, log3) {
|
|
9422
|
+
const body = `${STORES.map((store) => `${store}=${outcomes[store]}`).join("\n")}
|
|
9423
|
+
`;
|
|
9424
|
+
const temporaryPath = `${markerPath}.tmp`;
|
|
9425
|
+
try {
|
|
9426
|
+
writeFileSync5(temporaryPath, body, { mode: 384 });
|
|
9427
|
+
renameSync(temporaryPath, markerPath);
|
|
9428
|
+
} catch (error2) {
|
|
9429
|
+
log3(`CREDENTIAL-FLUSH-MARKER-UNWRITABLE: ${markerPath}: ${errorMessage2(error2)}`, "warn");
|
|
9430
|
+
}
|
|
9431
|
+
}
|
|
9432
|
+
function intervalSeconds(env, log3) {
|
|
9433
|
+
const raw = env.CREDS_SYNC_INTERVAL;
|
|
9434
|
+
if (raw === void 0 || /^[1-9][0-9]*$/.test(raw) && Number.isSafeInteger(Number(raw))) {
|
|
9435
|
+
return raw === void 0 ? DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS : Number(raw);
|
|
9436
|
+
}
|
|
9437
|
+
log3(
|
|
9438
|
+
`CREDENTIAL-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${raw}' is not a positive integer; using ${DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS}s`,
|
|
9439
|
+
"warn"
|
|
9440
|
+
);
|
|
9441
|
+
return DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS;
|
|
9442
|
+
}
|
|
9443
|
+
async function runFlushStore(store, env, synchroniserRunner, log3, deadlineAt) {
|
|
9444
|
+
const remainingMs = deadlineAt - Date.now();
|
|
9445
|
+
if (remainingMs <= 0) return { outcome: "timeout", orphaned: false };
|
|
9446
|
+
const controller = new AbortController();
|
|
9447
|
+
let result;
|
|
9448
|
+
let failed = false;
|
|
9449
|
+
const completion = Promise.resolve().then(
|
|
9450
|
+
() => synchroniserRunner(["sync-once", store], {
|
|
9451
|
+
timeoutMs: remainingMs,
|
|
9452
|
+
env,
|
|
9453
|
+
signal: controller.signal
|
|
9454
|
+
})
|
|
9455
|
+
).then(
|
|
9456
|
+
(value) => {
|
|
9457
|
+
result = value;
|
|
9458
|
+
},
|
|
9459
|
+
(error2) => {
|
|
9460
|
+
failed = true;
|
|
9461
|
+
log3(`CREDENTIAL-FLUSH-ERROR: ${store}: ${errorMessage2(error2)}`, "warn");
|
|
9462
|
+
}
|
|
9463
|
+
);
|
|
9464
|
+
const abortTimer = setTimeout(() => controller.abort(), remainingMs);
|
|
9465
|
+
const settledBeforeDeadline = await waitForSettlement(completion, remainingMs);
|
|
9466
|
+
clearTimeout(abortTimer);
|
|
9467
|
+
if (!settledBeforeDeadline) {
|
|
9468
|
+
controller.abort();
|
|
9469
|
+
const settledAfterAbort = await waitForSettlement(completion, CREDENTIAL_FLUSH_ABORT_GRACE_MS);
|
|
9470
|
+
if (!settledAfterAbort) return { outcome: "timeout", orphaned: true };
|
|
9471
|
+
return { outcome: "timeout", orphaned: false };
|
|
9472
|
+
}
|
|
9473
|
+
if (failed || !result) return { outcome: "failed", orphaned: false };
|
|
9474
|
+
if (result.timedOut || Date.now() >= deadlineAt) {
|
|
9475
|
+
return { outcome: "timeout", orphaned: false };
|
|
9476
|
+
}
|
|
9477
|
+
return { outcome: result.code === 0 ? "ok" : "failed", orphaned: false };
|
|
9478
|
+
}
|
|
9479
|
+
function createCredentialSync({
|
|
9480
|
+
markerPath,
|
|
9481
|
+
env,
|
|
9482
|
+
log: log3,
|
|
9483
|
+
synchroniserRunner = runSynchroniser
|
|
9484
|
+
}) {
|
|
9485
|
+
const persistenceDisabled = !env.PERSISTENCE_BUCKET;
|
|
9486
|
+
let disabled = persistenceDisabled;
|
|
9487
|
+
let armed = false;
|
|
9488
|
+
let stopped = false;
|
|
9489
|
+
let timer;
|
|
9490
|
+
let inFlight;
|
|
9491
|
+
let activeTickAbort;
|
|
9492
|
+
let lastTickFailed;
|
|
9493
|
+
let flushPromise;
|
|
9494
|
+
const scheduleTick = (intervalMs, startTick2) => {
|
|
9495
|
+
if (stopped) return;
|
|
9496
|
+
timer = setTimeout(() => {
|
|
9497
|
+
timer = void 0;
|
|
9498
|
+
startTick2();
|
|
9499
|
+
}, intervalMs);
|
|
9500
|
+
};
|
|
9501
|
+
const startTick = (intervalMs) => {
|
|
9502
|
+
if (stopped) return;
|
|
9503
|
+
const controller = new AbortController();
|
|
9504
|
+
activeTickAbort = controller;
|
|
9505
|
+
const tick = (async () => {
|
|
9506
|
+
const outcomes = {
|
|
9507
|
+
claude: "failed",
|
|
9508
|
+
opencode: "failed"
|
|
9509
|
+
};
|
|
9510
|
+
for (const store of STORES) {
|
|
9511
|
+
if (controller.signal.aborted) break;
|
|
9512
|
+
try {
|
|
9513
|
+
const result = await synchroniserRunner(["sync-once", store], {
|
|
9514
|
+
timeoutMs: CREDENTIAL_SYNC_TICK_TIMEOUT_MS,
|
|
9515
|
+
env,
|
|
9516
|
+
signal: controller.signal
|
|
9517
|
+
});
|
|
9518
|
+
outcomes[store] = !result.timedOut && result.code === 0 ? "ok" : "failed";
|
|
9519
|
+
} catch (error2) {
|
|
9520
|
+
outcomes[store] = "failed";
|
|
9521
|
+
log3(`CREDENTIAL-SYNC-TICK-ERROR: ${store}: ${errorMessage2(error2)}`, "debug");
|
|
9522
|
+
}
|
|
9523
|
+
}
|
|
9524
|
+
const failed = STORES.some((store) => outcomes[store] === "failed");
|
|
9525
|
+
log3(
|
|
9526
|
+
`CREDENTIAL-SYNC-TICK: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
|
|
9527
|
+
"debug"
|
|
9528
|
+
);
|
|
9529
|
+
if (failed && lastTickFailed !== true) {
|
|
9530
|
+
log3(
|
|
9531
|
+
"CREDENTIAL-SYNC-FAILED: an interval credential sync failed; retrying on the next tick",
|
|
9532
|
+
"warn"
|
|
9533
|
+
);
|
|
9534
|
+
} else if (!failed && lastTickFailed === true) {
|
|
9535
|
+
log3("CREDENTIAL-SYNC-RECOVERED: interval credential sync succeeded again", "warn");
|
|
9536
|
+
}
|
|
9537
|
+
lastTickFailed = failed;
|
|
9538
|
+
})().finally(() => {
|
|
9539
|
+
if (activeTickAbort === controller) activeTickAbort = void 0;
|
|
9540
|
+
if (inFlight === tick) inFlight = void 0;
|
|
9541
|
+
scheduleTick(intervalMs, () => startTick(intervalMs));
|
|
9542
|
+
});
|
|
9543
|
+
inFlight = tick;
|
|
9544
|
+
};
|
|
9545
|
+
const performFlush = async () => {
|
|
9546
|
+
stopped = true;
|
|
9547
|
+
if (timer) {
|
|
9548
|
+
clearTimeout(timer);
|
|
9549
|
+
timer = void 0;
|
|
9550
|
+
}
|
|
9551
|
+
const deadlineAt = Date.now() + CREDENTIAL_FLUSH_DEADLINE_MS;
|
|
9552
|
+
if (inFlight) {
|
|
9553
|
+
const settled = await waitForSettlement(inFlight, CREDENTIAL_FLUSH_DEADLINE_MS);
|
|
9554
|
+
if (!settled) {
|
|
9555
|
+
activeTickAbort?.abort();
|
|
9556
|
+
const settledAfterAbort = await waitForSettlement(
|
|
9557
|
+
inFlight,
|
|
9558
|
+
CREDENTIAL_FLUSH_ABORT_GRACE_MS
|
|
9559
|
+
);
|
|
9560
|
+
if (!settledAfterAbort) {
|
|
9561
|
+
log3(
|
|
9562
|
+
"CREDENTIAL-FLUSH-ORPHANED-TICK: a sync-once child could not be proven settled; skipping the boundary flush rather than becoming a second writer",
|
|
9563
|
+
"warn"
|
|
9564
|
+
);
|
|
9565
|
+
return { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9566
|
+
}
|
|
9567
|
+
}
|
|
9568
|
+
}
|
|
9569
|
+
if (disabled) return { outcomes: outcomesWith("skipped"), orphaned: false };
|
|
9570
|
+
const outcomes = outcomesWith("timeout");
|
|
9571
|
+
for (const store of STORES) {
|
|
9572
|
+
const result = await runFlushStore(store, env, synchroniserRunner, log3, deadlineAt);
|
|
9573
|
+
if (result.orphaned) {
|
|
9574
|
+
log3(
|
|
9575
|
+
"CREDENTIAL-FLUSH-ORPHANED-CHILD: a flush sync-once child could not be proven settled; refusing another flush pass rather than becoming a second writer",
|
|
9576
|
+
"warn"
|
|
9577
|
+
);
|
|
9578
|
+
return { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9579
|
+
}
|
|
9580
|
+
outcomes[store] = result.outcome;
|
|
9581
|
+
}
|
|
9582
|
+
return { outcomes, orphaned: false };
|
|
9583
|
+
};
|
|
9584
|
+
let flushPasses = 0;
|
|
9585
|
+
let lastFlush;
|
|
9586
|
+
return {
|
|
9587
|
+
arm() {
|
|
9588
|
+
if (stopped || armed) return;
|
|
9589
|
+
armed = true;
|
|
9590
|
+
if (persistenceDisabled) {
|
|
9591
|
+
disabled = true;
|
|
9592
|
+
log3(
|
|
9593
|
+
"CREDENTIAL-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot",
|
|
9594
|
+
"warn"
|
|
9595
|
+
);
|
|
9596
|
+
return;
|
|
9597
|
+
}
|
|
9598
|
+
disabled = false;
|
|
9599
|
+
const intervalMs = intervalSeconds(env, log3) * 1e3;
|
|
9600
|
+
scheduleTick(intervalMs, () => startTick(intervalMs));
|
|
9601
|
+
},
|
|
9602
|
+
async stopAndFlush(publish) {
|
|
9603
|
+
let result;
|
|
9604
|
+
const runningFlush = flushPromise;
|
|
9605
|
+
if (runningFlush) {
|
|
9606
|
+
result = await runningFlush;
|
|
9607
|
+
} else if (flushPasses >= MAX_FLUSH_PASSES || lastFlush?.orphaned) {
|
|
9608
|
+
result = lastFlush ?? { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9609
|
+
} else {
|
|
9610
|
+
flushPasses++;
|
|
9611
|
+
const currentFlush = performFlush();
|
|
9612
|
+
flushPromise = currentFlush;
|
|
9613
|
+
try {
|
|
9614
|
+
result = await currentFlush;
|
|
9615
|
+
lastFlush = result;
|
|
9616
|
+
} finally {
|
|
9617
|
+
if (flushPromise === currentFlush) flushPromise = void 0;
|
|
9618
|
+
}
|
|
9619
|
+
}
|
|
9620
|
+
if (publish) writeMarker(markerPath, result.outcomes, log3);
|
|
9621
|
+
return result.outcomes;
|
|
9622
|
+
}
|
|
9623
|
+
};
|
|
8939
9624
|
}
|
|
8940
9625
|
|
|
8941
9626
|
// src/commands/run.ts
|
|
@@ -8975,7 +9660,7 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
8975
9660
|
if (trimmed === "") {
|
|
8976
9661
|
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
8977
9662
|
}
|
|
8978
|
-
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ?
|
|
9663
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join9(homeDir, trimmed.slice(2)) : trimmed;
|
|
8979
9664
|
if (!isAbsolute3(expanded)) {
|
|
8980
9665
|
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
8981
9666
|
}
|
|
@@ -9235,6 +9920,10 @@ async function driveChannels(state, driver) {
|
|
|
9235
9920
|
consecutiveDrainFailures = 0;
|
|
9236
9921
|
unreachableMs = 0;
|
|
9237
9922
|
state.messageCount += processed;
|
|
9923
|
+
if (driver.recycleRequested) {
|
|
9924
|
+
await beginGracefulShutdown(state, "recycle");
|
|
9925
|
+
return;
|
|
9926
|
+
}
|
|
9238
9927
|
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
9239
9928
|
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
9240
9929
|
const fileActivitySnapshot = driver.fileSyncActivity();
|
|
@@ -9277,8 +9966,8 @@ async function driveChannels(state, driver) {
|
|
|
9277
9966
|
state.running = false;
|
|
9278
9967
|
break;
|
|
9279
9968
|
}
|
|
9280
|
-
const
|
|
9281
|
-
logActivity(state, { type: "error", error: `Channel processing error: ${
|
|
9969
|
+
const errorMessage3 = error2 instanceof Error ? error2.message : String(error2);
|
|
9970
|
+
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage3}` });
|
|
9282
9971
|
if (state.interactive) displayStatus(state);
|
|
9283
9972
|
if (driver.hasInFlightWatchers()) {
|
|
9284
9973
|
consecutiveDrainFailures = 0;
|
|
@@ -9316,9 +10005,54 @@ async function driveChannels(state, driver) {
|
|
|
9316
10005
|
}
|
|
9317
10006
|
}
|
|
9318
10007
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
9319
|
-
var SESSION_DB_RECLAIM_MAX_PAGES =
|
|
10008
|
+
var SESSION_DB_RECLAIM_MAX_PAGES = 2e4;
|
|
10009
|
+
function shouldWarnForReclaimSkip(reason) {
|
|
10010
|
+
if (reason !== "sqlite-unavailable") return false;
|
|
10011
|
+
const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
|
|
10012
|
+
if (!version2) return false;
|
|
10013
|
+
const major = Number(version2[1]);
|
|
10014
|
+
const minor = Number(version2[2]);
|
|
10015
|
+
const patch = Number(version2[3]);
|
|
10016
|
+
return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
|
|
10017
|
+
}
|
|
9320
10018
|
function sessionDbPath() {
|
|
9321
|
-
return
|
|
10019
|
+
return join9(homedir5(), ".local", "share", "opencode", "opencode.db");
|
|
10020
|
+
}
|
|
10021
|
+
function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
|
|
10022
|
+
const record = {
|
|
10023
|
+
v: 1,
|
|
10024
|
+
event: "session_db_recovery",
|
|
10025
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
10026
|
+
stage: "verify",
|
|
10027
|
+
outcome: "schema_provenance_mismatch",
|
|
10028
|
+
severity: "error",
|
|
10029
|
+
reason: provenance.reason ?? "schema-provenance-mismatch",
|
|
10030
|
+
litestream_exit_code: null,
|
|
10031
|
+
attempt: null,
|
|
10032
|
+
replica_objects: null,
|
|
10033
|
+
replica_bytes: null,
|
|
10034
|
+
quarantine_destination: null,
|
|
10035
|
+
quarantined_objects: null,
|
|
10036
|
+
quarantine_failed_objects: null,
|
|
10037
|
+
quarantined_bytes: null,
|
|
10038
|
+
verified_restore_point: null,
|
|
10039
|
+
restore_points_tried: null,
|
|
10040
|
+
provenance_reason: provenance.reason,
|
|
10041
|
+
provenance_migration_delta: provenance.migrationDelta,
|
|
10042
|
+
replication_suspended: false,
|
|
10043
|
+
dbPath: sessionDbPath(),
|
|
10044
|
+
recorded_version: provenance.recordedVersion,
|
|
10045
|
+
current_version: currentVersion,
|
|
10046
|
+
provenance_pre_boot_migration_count: preBootMigrationCount
|
|
10047
|
+
};
|
|
10048
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
10049
|
+
if (!activity) throw new Error("could not map session-DB provenance activity");
|
|
10050
|
+
logActivity(state, {
|
|
10051
|
+
type: activity.level === "error" ? "error" : "info",
|
|
10052
|
+
level: activity.level,
|
|
10053
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
10054
|
+
metadata: activity.metadata
|
|
10055
|
+
});
|
|
9322
10056
|
}
|
|
9323
10057
|
async function runSweep(state, driver, config) {
|
|
9324
10058
|
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
@@ -9365,7 +10099,7 @@ async function runSweep(state, driver, config) {
|
|
|
9365
10099
|
const reclaimResult = await reclaimSessionDbSpace({
|
|
9366
10100
|
dbPath: sessionDbPath(),
|
|
9367
10101
|
maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
|
|
9368
|
-
allowFullVacuum: protectedNow.size === 0
|
|
10102
|
+
allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
|
|
9369
10103
|
});
|
|
9370
10104
|
if (reclaimResult.ok) {
|
|
9371
10105
|
const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
|
|
@@ -9378,7 +10112,7 @@ async function runSweep(state, driver, config) {
|
|
|
9378
10112
|
} else {
|
|
9379
10113
|
logActivity(state, {
|
|
9380
10114
|
type: "info",
|
|
9381
|
-
message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})`
|
|
10115
|
+
message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})` + (reclaimResult.detail ? `: ${reclaimResult.detail}` : "")
|
|
9382
10116
|
});
|
|
9383
10117
|
}
|
|
9384
10118
|
} catch (error2) {
|
|
@@ -9403,11 +10137,18 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
9403
10137
|
}
|
|
9404
10138
|
const dbBytes = statSessionDbBytes(homedir5());
|
|
9405
10139
|
void (async () => {
|
|
9406
|
-
const
|
|
10140
|
+
const reclaimAvailability = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
|
|
10141
|
+
if (reclaimAvailability !== null) {
|
|
10142
|
+
logActivity(state, {
|
|
10143
|
+
type: "info",
|
|
10144
|
+
level: shouldWarnForReclaimSkip(reclaimAvailability.reason) ? "warn" : "info",
|
|
10145
|
+
message: `Session cleanup: reclaim preflight skipped (${reclaimAvailability.reason})` + (reclaimAvailability.detail ? `: ${reclaimAvailability.detail}` : "")
|
|
10146
|
+
});
|
|
10147
|
+
}
|
|
9407
10148
|
const sizeWarning = buildSessionStoreSizeWarning({
|
|
9408
10149
|
dbBytes,
|
|
9409
10150
|
cleanupEnabled: config.enabled,
|
|
9410
|
-
reclaimSkipReason
|
|
10151
|
+
reclaimSkipReason: reclaimAvailability?.reason ?? null
|
|
9411
10152
|
});
|
|
9412
10153
|
if (sizeWarning !== null) {
|
|
9413
10154
|
logActivity(state, { type: "info", level: "warn", message: sizeWarning });
|
|
@@ -9602,7 +10343,8 @@ function scheduleResourceUsageReporting(state, options) {
|
|
|
9602
10343
|
});
|
|
9603
10344
|
return;
|
|
9604
10345
|
}
|
|
9605
|
-
const collect = createResourceUsageCollector(homedir5());
|
|
10346
|
+
const { collect, stop } = createResourceUsageCollector(homedir5());
|
|
10347
|
+
state.stopResourceUsageSampling = stop;
|
|
9606
10348
|
let consecutiveFailures = 0;
|
|
9607
10349
|
const tick = async () => {
|
|
9608
10350
|
try {
|
|
@@ -9712,21 +10454,41 @@ async function cleanup(state, opts = {}) {
|
|
|
9712
10454
|
clearTimeout(state.resourceUsageTimer);
|
|
9713
10455
|
state.resourceUsageTimer = null;
|
|
9714
10456
|
}
|
|
10457
|
+
state.stopResourceUsageSampling?.();
|
|
10458
|
+
state.stopResourceUsageSampling = null;
|
|
10459
|
+
const credentialSync = state.credentialSync;
|
|
10460
|
+
const flushCredentials = credentialSync ? async (phase, publish) => {
|
|
10461
|
+
await timeShutdownPhase(state, durations, phase, async () => {
|
|
10462
|
+
const outcomes = await credentialSync.stopAndFlush(publish);
|
|
10463
|
+
const level = Object.values(outcomes).every((outcome) => outcome === "ok") ? "info" : "warn";
|
|
10464
|
+
log2(
|
|
10465
|
+
state,
|
|
10466
|
+
`Credential flush: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
|
|
10467
|
+
level
|
|
10468
|
+
);
|
|
10469
|
+
});
|
|
10470
|
+
} : void 0;
|
|
10471
|
+
let drainSettled = true;
|
|
9715
10472
|
if (opts.graceful && state.channelDriver) {
|
|
9716
10473
|
state.channelDriver.stop();
|
|
10474
|
+
}
|
|
10475
|
+
if (flushCredentials) {
|
|
10476
|
+
await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
|
|
10477
|
+
}
|
|
10478
|
+
if (opts.graceful && state.channelDriver) {
|
|
9717
10479
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
9718
10480
|
if (state.interactive) {
|
|
9719
10481
|
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
9720
10482
|
displayStatus(state);
|
|
9721
10483
|
}
|
|
9722
10484
|
const driver = state.channelDriver;
|
|
9723
|
-
|
|
10485
|
+
drainSettled = await timeShutdownPhase(
|
|
9724
10486
|
state,
|
|
9725
10487
|
durations,
|
|
9726
10488
|
"drain",
|
|
9727
10489
|
() => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
|
|
9728
10490
|
);
|
|
9729
|
-
if (!
|
|
10491
|
+
if (!drainSettled) {
|
|
9730
10492
|
logActivity(state, {
|
|
9731
10493
|
type: "info",
|
|
9732
10494
|
message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
|
|
@@ -9734,6 +10496,9 @@ async function cleanup(state, opts = {}) {
|
|
|
9734
10496
|
if (state.interactive) displayStatus(state);
|
|
9735
10497
|
}
|
|
9736
10498
|
}
|
|
10499
|
+
if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
|
|
10500
|
+
await flushCredentials("credential_flush_final", true);
|
|
10501
|
+
}
|
|
9737
10502
|
await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
|
|
9738
10503
|
if (state.connection) {
|
|
9739
10504
|
const connection = state.connection;
|
|
@@ -9769,6 +10534,44 @@ async function cleanup(state, opts = {}) {
|
|
|
9769
10534
|
}
|
|
9770
10535
|
return durations;
|
|
9771
10536
|
}
|
|
10537
|
+
async function beginGracefulShutdown(state, trigger) {
|
|
10538
|
+
if (state.shuttingDown) return;
|
|
10539
|
+
state.shuttingDown = true;
|
|
10540
|
+
const shutdownStartedAt = Date.now();
|
|
10541
|
+
const shutdownMessage = trigger === "recycle" ? "Recycle requested \u2014 finishing in-flight work, then exiting" : "Shutting down...";
|
|
10542
|
+
if (state.interactive) {
|
|
10543
|
+
logActivity(state, { type: "info", message: shutdownMessage });
|
|
10544
|
+
displayStatus(state);
|
|
10545
|
+
} else {
|
|
10546
|
+
log2(state, shutdownMessage);
|
|
10547
|
+
}
|
|
10548
|
+
const durations = await cleanup(state, { graceful: true });
|
|
10549
|
+
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
10550
|
+
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
10551
|
+
let timer;
|
|
10552
|
+
const flushed = shutdownTelemetry().then(
|
|
10553
|
+
() => true,
|
|
10554
|
+
(error2) => {
|
|
10555
|
+
log2(
|
|
10556
|
+
state,
|
|
10557
|
+
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
10558
|
+
"warn"
|
|
10559
|
+
);
|
|
10560
|
+
return true;
|
|
10561
|
+
}
|
|
10562
|
+
);
|
|
10563
|
+
const timedOut = new Promise((resolve4) => {
|
|
10564
|
+
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
10565
|
+
});
|
|
10566
|
+
if (!await Promise.race([flushed, timedOut])) {
|
|
10567
|
+
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
10568
|
+
}
|
|
10569
|
+
clearTimeout(timer);
|
|
10570
|
+
});
|
|
10571
|
+
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
10572
|
+
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
10573
|
+
process.exit(0);
|
|
10574
|
+
}
|
|
9772
10575
|
async function run(options) {
|
|
9773
10576
|
const interactive = isInteractive(options.json);
|
|
9774
10577
|
let logLevel;
|
|
@@ -9804,6 +10607,7 @@ async function run(options) {
|
|
|
9804
10607
|
connected: false,
|
|
9805
10608
|
opencodeConnected: false,
|
|
9806
10609
|
opencodeVersion: null,
|
|
10610
|
+
sessionDbProvenanceAnomaly: false,
|
|
9807
10611
|
opencodeProcess: null,
|
|
9808
10612
|
litestreamProcess: null,
|
|
9809
10613
|
connection: null,
|
|
@@ -9819,9 +10623,24 @@ async function run(options) {
|
|
|
9819
10623
|
openaiUsageTimer: null,
|
|
9820
10624
|
openaiUsageRearm: null,
|
|
9821
10625
|
resourceUsageTimer: null,
|
|
10626
|
+
stopResourceUsageSampling: null,
|
|
10627
|
+
credentialSync: null,
|
|
9822
10628
|
authHeader: ""
|
|
9823
10629
|
};
|
|
9824
10630
|
setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
|
|
10631
|
+
if (options.credentialSyncMarker) {
|
|
10632
|
+
state.credentialSync = createCredentialSync({
|
|
10633
|
+
markerPath: options.credentialSyncMarker,
|
|
10634
|
+
env: process.env,
|
|
10635
|
+
log: (message, level = "info") => {
|
|
10636
|
+
if (level === "error") {
|
|
10637
|
+
logActivity(state, { type: "error", error: message });
|
|
10638
|
+
} else {
|
|
10639
|
+
logActivity(state, { type: "info", level, message });
|
|
10640
|
+
}
|
|
10641
|
+
}
|
|
10642
|
+
});
|
|
10643
|
+
}
|
|
9825
10644
|
if (fileSyncDirectories.length > 0) {
|
|
9826
10645
|
log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
|
|
9827
10646
|
} else {
|
|
@@ -9847,43 +10666,7 @@ async function run(options) {
|
|
|
9847
10666
|
"warn"
|
|
9848
10667
|
);
|
|
9849
10668
|
}
|
|
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
|
-
};
|
|
10669
|
+
const handleSignal = () => beginGracefulShutdown(state, "signal");
|
|
9887
10670
|
process.on("SIGINT", handleSignal);
|
|
9888
10671
|
process.on("SIGTERM", handleSignal);
|
|
9889
10672
|
try {
|
|
@@ -10029,6 +10812,7 @@ async function run(options) {
|
|
|
10029
10812
|
await restoreCredentialStores(credentialContext);
|
|
10030
10813
|
if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
|
|
10031
10814
|
}
|
|
10815
|
+
state.credentialSync?.arm();
|
|
10032
10816
|
let sessionDbVerifyFatal = false;
|
|
10033
10817
|
if (!options.restoreSessionDb) {
|
|
10034
10818
|
log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
|
|
@@ -10082,6 +10866,7 @@ async function run(options) {
|
|
|
10082
10866
|
for (const warning2 of maxActiveSessionsWarnings) {
|
|
10083
10867
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
10084
10868
|
}
|
|
10869
|
+
const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
|
|
10085
10870
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
10086
10871
|
try {
|
|
10087
10872
|
const oc = await ensureOpenCodeRunning({
|
|
@@ -10097,7 +10882,7 @@ async function run(options) {
|
|
|
10097
10882
|
state.opencodeVersion = oc.version;
|
|
10098
10883
|
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
10099
10884
|
try {
|
|
10100
|
-
|
|
10885
|
+
writeFileSync6(options.opencodePidFile, `${oc.process.pid}
|
|
10101
10886
|
`, { mode: 384 });
|
|
10102
10887
|
chmodSync3(options.opencodePidFile, 384);
|
|
10103
10888
|
} catch (error2) {
|
|
@@ -10107,6 +10892,23 @@ async function run(options) {
|
|
|
10107
10892
|
});
|
|
10108
10893
|
}
|
|
10109
10894
|
}
|
|
10895
|
+
if (state.opencodeVersion !== null) {
|
|
10896
|
+
const provenance = checkSessionDbProvenance({
|
|
10897
|
+
dbPath: sessionDbPath(),
|
|
10898
|
+
currentVersion: state.opencodeVersion,
|
|
10899
|
+
homeDir: homedir5(),
|
|
10900
|
+
env: process.env
|
|
10901
|
+
});
|
|
10902
|
+
if (provenance.anomaly) {
|
|
10903
|
+
state.sessionDbProvenanceAnomaly = true;
|
|
10904
|
+
logSessionDbProvenanceMismatch(
|
|
10905
|
+
state,
|
|
10906
|
+
provenance,
|
|
10907
|
+
state.opencodeVersion,
|
|
10908
|
+
preBootMigrationIds?.length ?? null
|
|
10909
|
+
);
|
|
10910
|
+
}
|
|
10911
|
+
}
|
|
10110
10912
|
state.opencodeConnected = oc.notReadyReason === null;
|
|
10111
10913
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
10112
10914
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
@@ -10159,7 +10961,7 @@ async function run(options) {
|
|
|
10159
10961
|
let existingPid;
|
|
10160
10962
|
if (existsSync3(options.litestreamPidFile)) {
|
|
10161
10963
|
try {
|
|
10162
|
-
const rawPid =
|
|
10964
|
+
const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
|
|
10163
10965
|
const parsedPid = Number(rawPid);
|
|
10164
10966
|
if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
|
|
10165
10967
|
existingPid = parsedPid;
|
|
@@ -10196,7 +10998,7 @@ async function run(options) {
|
|
|
10196
10998
|
});
|
|
10197
10999
|
try {
|
|
10198
11000
|
if (litestreamProcess.pid !== void 0) {
|
|
10199
|
-
|
|
11001
|
+
writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
|
|
10200
11002
|
`, {
|
|
10201
11003
|
mode: 384
|
|
10202
11004
|
});
|
|
@@ -10382,6 +11184,18 @@ async function run(options) {
|
|
|
10382
11184
|
if (state.interactive) displayStatus(state);
|
|
10383
11185
|
});
|
|
10384
11186
|
},
|
|
11187
|
+
// Both loops are rearmed because `rearm()` is idempotent for the
|
|
11188
|
+
// provider that did not just connect, and is a no-op when reporting is off.
|
|
11189
|
+
onUsageRearmPing: () => {
|
|
11190
|
+
if (!state.running) return;
|
|
11191
|
+
logActivity(state, {
|
|
11192
|
+
type: "info",
|
|
11193
|
+
level: "debug",
|
|
11194
|
+
message: "Usage rearm ping received"
|
|
11195
|
+
});
|
|
11196
|
+
state.claudeUsageRearm?.();
|
|
11197
|
+
state.openaiUsageRearm?.();
|
|
11198
|
+
},
|
|
10385
11199
|
onInfo: (message) => logActivity(state, { type: "info", message })
|
|
10386
11200
|
}
|
|
10387
11201
|
});
|
|
@@ -10402,7 +11216,17 @@ async function run(options) {
|
|
|
10402
11216
|
setTimer: (timer) => {
|
|
10403
11217
|
state.openaiUsageTimer = timer;
|
|
10404
11218
|
},
|
|
10405
|
-
fetchUsage: () =>
|
|
11219
|
+
fetchUsage: async () => {
|
|
11220
|
+
const usage = await getOpenAiUsage(state.port);
|
|
11221
|
+
if (usage.subscription === null) {
|
|
11222
|
+
logActivity(state, {
|
|
11223
|
+
type: "info",
|
|
11224
|
+
level: "debug",
|
|
11225
|
+
message: "OpenAI usage subscription could not be identified from the local credential"
|
|
11226
|
+
});
|
|
11227
|
+
}
|
|
11228
|
+
return usage;
|
|
11229
|
+
},
|
|
10406
11230
|
report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
|
|
10407
11231
|
isLocalCredentialProblem: isLocalCredentialProblem2,
|
|
10408
11232
|
forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
|
|
@@ -10448,7 +11272,7 @@ async function run(options) {
|
|
|
10448
11272
|
}
|
|
10449
11273
|
|
|
10450
11274
|
// src/index.ts
|
|
10451
|
-
var { version } =
|
|
11275
|
+
var { version } = createRequire2(import.meta.url)("../package.json");
|
|
10452
11276
|
var program = new Command();
|
|
10453
11277
|
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
|
|
10454
11278
|
"--endpoint <url>",
|
|
@@ -10526,6 +11350,9 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
10526
11350
|
).option(
|
|
10527
11351
|
"--opencode-config-overlay <path>",
|
|
10528
11352
|
"Apply this runner-provided OpenCode config before starting OpenCode."
|
|
11353
|
+
).option(
|
|
11354
|
+
"--credential-sync-marker <path>",
|
|
11355
|
+
"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
11356
|
).action(
|
|
10530
11357
|
(options) => {
|
|
10531
11358
|
run({
|
|
@@ -10564,7 +11391,8 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
10564
11391
|
sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10565
11392
|
restoreSessionDb: options.restoreSessionDb,
|
|
10566
11393
|
restoreRunnerCredentials: options.restoreRunnerCredentials,
|
|
10567
|
-
opencodeConfigOverlay: options.opencodeConfigOverlay
|
|
11394
|
+
opencodeConfigOverlay: options.opencodeConfigOverlay,
|
|
11395
|
+
credentialSyncMarker: options.credentialSyncMarker
|
|
10568
11396
|
});
|
|
10569
11397
|
}
|
|
10570
11398
|
);
|