@evident-ai/cli 3.4.1-dev.57ff127 → 3.4.1-dev.59c7df3
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 +21 -0
- package/dist/index.js +1399 -104
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -371,14 +371,14 @@ function blank() {
|
|
|
371
371
|
console.log();
|
|
372
372
|
}
|
|
373
373
|
function waitForEnter(prompt = "Press Enter to continue...") {
|
|
374
|
-
return new Promise((
|
|
374
|
+
return new Promise((resolve4) => {
|
|
375
375
|
process.stdout.write(chalk.dim(prompt));
|
|
376
376
|
const handler = () => {
|
|
377
377
|
process.stdin.removeListener("data", handler);
|
|
378
378
|
process.stdin.setRawMode?.(false);
|
|
379
379
|
process.stdin.pause();
|
|
380
380
|
console.log();
|
|
381
|
-
|
|
381
|
+
resolve4();
|
|
382
382
|
};
|
|
383
383
|
if (process.stdin.isTTY) {
|
|
384
384
|
process.stdin.setRawMode?.(true);
|
|
@@ -388,7 +388,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
|
|
|
388
388
|
});
|
|
389
389
|
}
|
|
390
390
|
function sleep(ms) {
|
|
391
|
-
return new Promise((
|
|
391
|
+
return new Promise((resolve4) => setTimeout(resolve4, ms));
|
|
392
392
|
}
|
|
393
393
|
|
|
394
394
|
// src/commands/login.ts
|
|
@@ -466,19 +466,19 @@ async function tokenLogin() {
|
|
|
466
466
|
);
|
|
467
467
|
blank();
|
|
468
468
|
process.stdout.write("Paste token: ");
|
|
469
|
-
const token = await new Promise((
|
|
469
|
+
const token = await new Promise((resolve4) => {
|
|
470
470
|
let data = "";
|
|
471
471
|
process.stdin.setEncoding("utf8");
|
|
472
472
|
process.stdin.on("data", (chunk) => {
|
|
473
473
|
data += chunk;
|
|
474
474
|
});
|
|
475
475
|
process.stdin.on("end", () => {
|
|
476
|
-
|
|
476
|
+
resolve4(data.trim());
|
|
477
477
|
});
|
|
478
478
|
if (process.stdin.isTTY) {
|
|
479
479
|
process.stdin.once("data", (chunk) => {
|
|
480
480
|
process.stdin.pause();
|
|
481
|
-
|
|
481
|
+
resolve4(chunk.toString().trim());
|
|
482
482
|
});
|
|
483
483
|
process.stdin.resume();
|
|
484
484
|
}
|
|
@@ -722,6 +722,14 @@ function toReportedWindow(window) {
|
|
|
722
722
|
if (!window) return null;
|
|
723
723
|
return { utilization: window.utilization, resets_at: window.resetsAt };
|
|
724
724
|
}
|
|
725
|
+
function toReportedOwner(snapshot) {
|
|
726
|
+
if (!snapshot.owner) return null;
|
|
727
|
+
return {
|
|
728
|
+
email: snapshot.owner.email,
|
|
729
|
+
organization_name: snapshot.owner.organizationName,
|
|
730
|
+
rate_limit_tier: snapshot.owner.rateLimitTier
|
|
731
|
+
};
|
|
732
|
+
}
|
|
725
733
|
async function reportClaudeUsage(agentId, authHeader, snapshot) {
|
|
726
734
|
try {
|
|
727
735
|
const apiUrl = getApiUrlConfig();
|
|
@@ -730,7 +738,8 @@ async function reportClaudeUsage(agentId, authHeader, snapshot) {
|
|
|
730
738
|
headers: { Authorization: authHeader, "Content-Type": "application/json" },
|
|
731
739
|
body: JSON.stringify({
|
|
732
740
|
five_hour: toReportedWindow(snapshot.fiveHour),
|
|
733
|
-
seven_day: toReportedWindow(snapshot.sevenDay)
|
|
741
|
+
seven_day: toReportedWindow(snapshot.sevenDay),
|
|
742
|
+
owner: toReportedOwner(snapshot)
|
|
734
743
|
}),
|
|
735
744
|
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
736
745
|
});
|
|
@@ -996,7 +1005,10 @@ import { readFileSync } from "fs";
|
|
|
996
1005
|
import { homedir } from "os";
|
|
997
1006
|
import { join } from "path";
|
|
998
1007
|
var CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
|
|
1008
|
+
var CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
|
|
1009
|
+
var CLAUDE_PROFILE_TIMEOUT_MS = 2e3;
|
|
999
1010
|
var KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
1011
|
+
var cachedOwner = null;
|
|
1000
1012
|
var CLAUDE_CREDENTIALS_SEGMENTS = [".claude", ".credentials.json"];
|
|
1001
1013
|
function parseClaudeCliCredentials(raw) {
|
|
1002
1014
|
let parsed;
|
|
@@ -1070,6 +1082,47 @@ function toWindow(value) {
|
|
|
1070
1082
|
}
|
|
1071
1083
|
return { utilization: window.utilization, resetsAt };
|
|
1072
1084
|
}
|
|
1085
|
+
function ownerLookupFailure(error2) {
|
|
1086
|
+
const name = error2?.name;
|
|
1087
|
+
return name === "TimeoutError" || name === "AbortError" ? "timed out" : "request failed";
|
|
1088
|
+
}
|
|
1089
|
+
async function getClaudeUsageOwner(accessToken) {
|
|
1090
|
+
if (cachedOwner?.accessToken === accessToken) {
|
|
1091
|
+
return { owner: cachedOwner.owner, ownerLookupError: null };
|
|
1092
|
+
}
|
|
1093
|
+
try {
|
|
1094
|
+
const response = await fetch(CLAUDE_PROFILE_URL, {
|
|
1095
|
+
headers: {
|
|
1096
|
+
Authorization: `Bearer ${accessToken}`,
|
|
1097
|
+
"Content-Type": "application/json",
|
|
1098
|
+
"anthropic-version": "2023-06-01"
|
|
1099
|
+
},
|
|
1100
|
+
signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
|
|
1101
|
+
});
|
|
1102
|
+
if (!response.ok) {
|
|
1103
|
+
return { owner: null, ownerLookupError: `HTTP ${response.status}` };
|
|
1104
|
+
}
|
|
1105
|
+
let body;
|
|
1106
|
+
try {
|
|
1107
|
+
body = await response.json();
|
|
1108
|
+
} catch (error2) {
|
|
1109
|
+
return { owner: null, ownerLookupError: "malformed response" };
|
|
1110
|
+
}
|
|
1111
|
+
const profile = body;
|
|
1112
|
+
if (typeof profile.account?.email !== "string" || !profile.account.email.trim()) {
|
|
1113
|
+
return { owner: null, ownerLookupError: "malformed response" };
|
|
1114
|
+
}
|
|
1115
|
+
const owner = {
|
|
1116
|
+
email: profile.account.email,
|
|
1117
|
+
organizationName: typeof profile.organization?.name === "string" && profile.organization.name.trim() ? profile.organization.name.trim() : null,
|
|
1118
|
+
rateLimitTier: typeof profile.organization?.rate_limit_tier === "string" && profile.organization.rate_limit_tier.trim() ? profile.organization.rate_limit_tier.trim() : null
|
|
1119
|
+
};
|
|
1120
|
+
cachedOwner = { accessToken, owner };
|
|
1121
|
+
return { owner, ownerLookupError: null };
|
|
1122
|
+
} catch (error2) {
|
|
1123
|
+
return { owner: null, ownerLookupError: ownerLookupFailure(error2) };
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1073
1126
|
async function getClaudeUsage() {
|
|
1074
1127
|
const credentials2 = readClaudeCliCredentials();
|
|
1075
1128
|
if (!credentials2) {
|
|
@@ -1089,15 +1142,19 @@ async function getClaudeUsage() {
|
|
|
1089
1142
|
Authorization: `Bearer ${credentials2.accessToken}`,
|
|
1090
1143
|
"Content-Type": "application/json",
|
|
1091
1144
|
"anthropic-version": "2023-06-01"
|
|
1092
|
-
}
|
|
1145
|
+
},
|
|
1146
|
+
signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
|
|
1093
1147
|
});
|
|
1094
1148
|
if (!res.ok) {
|
|
1095
1149
|
throw new ClaudeUsageError(`Claude usage request failed: HTTP ${res.status}`, "request_failed");
|
|
1096
1150
|
}
|
|
1097
1151
|
const body = await res.json();
|
|
1152
|
+
const { owner, ownerLookupError } = await getClaudeUsageOwner(credentials2.accessToken);
|
|
1098
1153
|
return {
|
|
1099
1154
|
fiveHour: toWindow(body.five_hour),
|
|
1100
|
-
sevenDay: toWindow(body.seven_day)
|
|
1155
|
+
sevenDay: toWindow(body.seven_day),
|
|
1156
|
+
owner,
|
|
1157
|
+
ownerLookupError
|
|
1101
1158
|
};
|
|
1102
1159
|
}
|
|
1103
1160
|
|
|
@@ -1126,8 +1183,9 @@ async function claudeUsage() {
|
|
|
1126
1183
|
}
|
|
1127
1184
|
|
|
1128
1185
|
// src/commands/run.ts
|
|
1129
|
-
import {
|
|
1130
|
-
import {
|
|
1186
|
+
import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync5, writeFileSync as writeFileSync4 } from "fs";
|
|
1187
|
+
import { homedir as homedir5 } from "os";
|
|
1188
|
+
import { isAbsolute as isAbsolute3, join as join8, parse, resolve as resolvePath2 } from "path";
|
|
1131
1189
|
import chalk6 from "chalk";
|
|
1132
1190
|
|
|
1133
1191
|
// ../../packages/types/src/agents/index.ts
|
|
@@ -1467,7 +1525,7 @@ function drainSessionDbRecoveryReport({
|
|
|
1467
1525
|
skippedLines++;
|
|
1468
1526
|
return [];
|
|
1469
1527
|
}
|
|
1470
|
-
return [value];
|
|
1528
|
+
return [{ ...value, replication_suspended: value.replication_suspended ?? false }];
|
|
1471
1529
|
} catch (error2) {
|
|
1472
1530
|
skippedLines++;
|
|
1473
1531
|
console.error(
|
|
@@ -1492,12 +1550,39 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1492
1550
|
const level = record.severity === "warning" ? "warn" : record.severity === "error" ? "error" : null;
|
|
1493
1551
|
if (!level) return null;
|
|
1494
1552
|
const noVerifiedPoint = record.verified_restore_point ? ` The verified restore point is ${record.verified_restore_point}.` : " No verified restore point is known.";
|
|
1553
|
+
const replication = record.replication_suspended ? " This start is not backing up its new session history; restart after fixing the cause." : "";
|
|
1554
|
+
const giveupMessage = (() => {
|
|
1555
|
+
switch (record.reason) {
|
|
1556
|
+
case "restore_deadline_exceeded":
|
|
1557
|
+
return "The restore did not finish before this startup deadline. Earlier sessions are unavailable for this start; collect the SESSION-DB-RESTORE-TRUNCATED boot log before retrying.";
|
|
1558
|
+
case "restore_tool_unusable":
|
|
1559
|
+
case "classification_unrecognised":
|
|
1560
|
+
return "The restore or classification tool produced an unsupported result. Inspect the named boot marker or log and repair the runner image or bundle before restarting.";
|
|
1561
|
+
case "synchroniser_config_unevaluable":
|
|
1562
|
+
case "synchroniser_config_incomplete":
|
|
1563
|
+
return "The installed hook and synchroniser bundle could not agree on configuration. Inspect the named boot marker or log and repair the runner image or bundle before restarting.";
|
|
1564
|
+
case "synchroniser_config_unresolved":
|
|
1565
|
+
return "The synchroniser configuration could not be resolved. Inspect the preceding boot error and repair the identified runner image, bundle, or local environment before restarting.";
|
|
1566
|
+
case "litestream_config_unavailable":
|
|
1567
|
+
return "The local Litestream configuration could not be generated or written. Inspect the preceding boot error and repair the runner image, bundle, local environment, or write permissions before restarting.";
|
|
1568
|
+
case "classification_fatal":
|
|
1569
|
+
return "Session history could not be restored because backup setup could not be established. Inspect the named boot marker or log and repair the identified backup configuration or permissions.";
|
|
1570
|
+
default:
|
|
1571
|
+
return null;
|
|
1572
|
+
}
|
|
1573
|
+
})();
|
|
1574
|
+
if (giveupMessage)
|
|
1575
|
+
return {
|
|
1576
|
+
level,
|
|
1577
|
+
metadata: withoutContractFields(record),
|
|
1578
|
+
message: `${giveupMessage}${replication}`
|
|
1579
|
+
};
|
|
1495
1580
|
switch (record.outcome) {
|
|
1496
1581
|
case "fresh_session_db":
|
|
1497
1582
|
return {
|
|
1498
1583
|
level,
|
|
1499
1584
|
metadata: withoutContractFields(record),
|
|
1500
|
-
message: `This runner started with a fresh session database. Earlier sessions are unavailable, and queued conversation or session references from before this start may no longer resolve.${noVerifiedPoint} Review the runner's backup configuration before relying on restored session history
|
|
1585
|
+
message: `This runner started with a fresh session database. Earlier sessions are unavailable, and queued conversation or session references from before this start may no longer resolve.${noVerifiedPoint} Review the runner's backup configuration before relying on restored session history.${replication}`
|
|
1501
1586
|
};
|
|
1502
1587
|
case "restore_retried":
|
|
1503
1588
|
return {
|
|
@@ -1535,7 +1620,13 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1535
1620
|
return {
|
|
1536
1621
|
level,
|
|
1537
1622
|
metadata: withoutContractFields(record),
|
|
1538
|
-
message:
|
|
1623
|
+
message: `This runner started without restoring session history because it could not verify where its session history is backed up. Nothing in the backup was changed; check the runner backup configuration and permissions.${replication}`
|
|
1624
|
+
};
|
|
1625
|
+
case "session_db_boot_refused":
|
|
1626
|
+
return {
|
|
1627
|
+
level,
|
|
1628
|
+
metadata: withoutContractFields(record),
|
|
1629
|
+
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.`
|
|
1539
1630
|
};
|
|
1540
1631
|
default:
|
|
1541
1632
|
return null;
|
|
@@ -1550,7 +1641,8 @@ var OUTCOMES = /* @__PURE__ */ new Set([
|
|
|
1550
1641
|
"restore_retried",
|
|
1551
1642
|
"fresh_session_db",
|
|
1552
1643
|
"history_rolled_back",
|
|
1553
|
-
"restore_misconfigured"
|
|
1644
|
+
"restore_misconfigured",
|
|
1645
|
+
"session_db_boot_refused"
|
|
1554
1646
|
]);
|
|
1555
1647
|
var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
|
|
1556
1648
|
var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
|
|
@@ -1568,7 +1660,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
|
|
|
1568
1660
|
function isSessionDbRecoveryRecord(value) {
|
|
1569
1661
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1570
1662
|
const record = value;
|
|
1571
|
-
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" && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
|
|
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(
|
|
1572
1664
|
(field) => record[field] === null || typeof record[field] === "string"
|
|
1573
1665
|
);
|
|
1574
1666
|
}
|
|
@@ -1597,11 +1689,517 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
1597
1689
|
if (health.healthy) {
|
|
1598
1690
|
return health;
|
|
1599
1691
|
}
|
|
1600
|
-
await new Promise((
|
|
1692
|
+
await new Promise((resolve4) => setTimeout(resolve4, 1e3));
|
|
1601
1693
|
}
|
|
1602
1694
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
1603
1695
|
}
|
|
1604
1696
|
|
|
1697
|
+
// 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";
|
|
1702
|
+
|
|
1703
|
+
// src/lib/runner-synchroniser.ts
|
|
1704
|
+
import { spawn } from "child_process";
|
|
1705
|
+
function appendError(stderr, error2) {
|
|
1706
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1707
|
+
return stderr === "" ? message : `${stderr}
|
|
1708
|
+
${message}`;
|
|
1709
|
+
}
|
|
1710
|
+
function runSynchroniser(args, opts) {
|
|
1711
|
+
return new Promise((resolve4) => {
|
|
1712
|
+
let child;
|
|
1713
|
+
let stdout = "";
|
|
1714
|
+
let stderr = "";
|
|
1715
|
+
let settled = false;
|
|
1716
|
+
const timer = {};
|
|
1717
|
+
const finish = (result) => {
|
|
1718
|
+
if (settled) return;
|
|
1719
|
+
settled = true;
|
|
1720
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
1721
|
+
resolve4(result);
|
|
1722
|
+
};
|
|
1723
|
+
try {
|
|
1724
|
+
child = spawn("runner-synchroniser", args, {
|
|
1725
|
+
env: opts.env ?? process.env,
|
|
1726
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1727
|
+
});
|
|
1728
|
+
} catch (error2) {
|
|
1729
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1730
|
+
return;
|
|
1731
|
+
}
|
|
1732
|
+
child.stdout?.setEncoding("utf8");
|
|
1733
|
+
child.stdout?.on("data", (chunk) => {
|
|
1734
|
+
stdout += chunk;
|
|
1735
|
+
});
|
|
1736
|
+
child.stderr?.setEncoding("utf8");
|
|
1737
|
+
child.stderr?.on("data", (chunk) => {
|
|
1738
|
+
stderr += chunk;
|
|
1739
|
+
});
|
|
1740
|
+
child.once("error", (error2) => {
|
|
1741
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1742
|
+
});
|
|
1743
|
+
child.once("close", (code) => {
|
|
1744
|
+
finish({ code, stdout, stderr, timedOut: false });
|
|
1745
|
+
});
|
|
1746
|
+
timer.handle = setTimeout(
|
|
1747
|
+
() => {
|
|
1748
|
+
child.kill("SIGKILL");
|
|
1749
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1750
|
+
},
|
|
1751
|
+
Math.max(0, opts.timeoutMs)
|
|
1752
|
+
);
|
|
1753
|
+
});
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1757
|
+
var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
|
|
1758
|
+
var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
|
|
1759
|
+
var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
|
|
1760
|
+
var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
|
|
1761
|
+
function commandError(result) {
|
|
1762
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
1763
|
+
}
|
|
1764
|
+
function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
|
|
1765
|
+
options.reportRecovery({
|
|
1766
|
+
v: 1,
|
|
1767
|
+
event: "session_db_recovery",
|
|
1768
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1769
|
+
stage,
|
|
1770
|
+
outcome,
|
|
1771
|
+
severity: "error",
|
|
1772
|
+
reason,
|
|
1773
|
+
litestream_exit_code: litestreamExitCode,
|
|
1774
|
+
attempt: null,
|
|
1775
|
+
replica_objects: null,
|
|
1776
|
+
replica_bytes: null,
|
|
1777
|
+
quarantine_destination: null,
|
|
1778
|
+
quarantined_objects: null,
|
|
1779
|
+
quarantine_failed_objects: null,
|
|
1780
|
+
quarantined_bytes: null,
|
|
1781
|
+
verified_restore_point: null,
|
|
1782
|
+
restore_points_tried: null,
|
|
1783
|
+
replication_suspended: stage === "restore"
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
function clearMarker(options) {
|
|
1787
|
+
if (!options.noReplicateMarker) return;
|
|
1788
|
+
try {
|
|
1789
|
+
unlinkSync2(options.noReplicateMarker);
|
|
1790
|
+
} catch (error2) {
|
|
1791
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
|
|
1792
|
+
options.log(
|
|
1793
|
+
`Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1794
|
+
"warn"
|
|
1795
|
+
);
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
function markNoReplicate(options, message) {
|
|
1799
|
+
if (options.noReplicateMarker) {
|
|
1800
|
+
try {
|
|
1801
|
+
mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
|
|
1802
|
+
writeFileSync(options.noReplicateMarker, "");
|
|
1803
|
+
} catch (error2) {
|
|
1804
|
+
options.log(
|
|
1805
|
+
`Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1806
|
+
"error"
|
|
1807
|
+
);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
|
|
1811
|
+
}
|
|
1812
|
+
function discardSessionDbDebris(options) {
|
|
1813
|
+
for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
|
|
1814
|
+
try {
|
|
1815
|
+
unlinkSync2(path);
|
|
1816
|
+
} catch (error2) {
|
|
1817
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
|
|
1818
|
+
options.log(
|
|
1819
|
+
`Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1820
|
+
"warn"
|
|
1821
|
+
);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
function splitDiagnostics(text) {
|
|
1826
|
+
return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
|
|
1827
|
+
}
|
|
1828
|
+
function logSynchroniserDiagnostics(result, options) {
|
|
1829
|
+
for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
|
|
1830
|
+
}
|
|
1831
|
+
function parseSingleQuotedAssignment(line) {
|
|
1832
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
|
|
1833
|
+
if (!match || !match[2].startsWith("'")) return null;
|
|
1834
|
+
const valueSource = match[2];
|
|
1835
|
+
let value = "";
|
|
1836
|
+
for (let index = 1; index < valueSource.length; index++) {
|
|
1837
|
+
const character = valueSource[index];
|
|
1838
|
+
if (character !== "'") {
|
|
1839
|
+
value += character;
|
|
1840
|
+
continue;
|
|
1841
|
+
}
|
|
1842
|
+
if (index === valueSource.length - 1) return [match[1], value];
|
|
1843
|
+
if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
|
|
1844
|
+
value += "'";
|
|
1845
|
+
index += 3;
|
|
1846
|
+
}
|
|
1847
|
+
return null;
|
|
1848
|
+
}
|
|
1849
|
+
function parseSynchroniserEnv(stdout) {
|
|
1850
|
+
const values = {};
|
|
1851
|
+
for (const line of stdout.split("\n")) {
|
|
1852
|
+
if (line.trim() === "") continue;
|
|
1853
|
+
const assignment = parseSingleQuotedAssignment(line);
|
|
1854
|
+
if (!assignment) return null;
|
|
1855
|
+
values[assignment[0]] = assignment[1];
|
|
1856
|
+
}
|
|
1857
|
+
return values;
|
|
1858
|
+
}
|
|
1859
|
+
function runCommand(command, args, options) {
|
|
1860
|
+
return new Promise((resolve4) => {
|
|
1861
|
+
let child;
|
|
1862
|
+
let stdout = "";
|
|
1863
|
+
let stderr = "";
|
|
1864
|
+
let settled = false;
|
|
1865
|
+
const finish = (result) => {
|
|
1866
|
+
if (settled) return;
|
|
1867
|
+
settled = true;
|
|
1868
|
+
if (timer) clearTimeout(timer);
|
|
1869
|
+
resolve4(result);
|
|
1870
|
+
};
|
|
1871
|
+
try {
|
|
1872
|
+
child = spawn2(command, args, {
|
|
1873
|
+
env: options.env,
|
|
1874
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1875
|
+
});
|
|
1876
|
+
} catch (error2) {
|
|
1877
|
+
resolve4({
|
|
1878
|
+
code: null,
|
|
1879
|
+
stdout,
|
|
1880
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
1881
|
+
timedOut: false
|
|
1882
|
+
});
|
|
1883
|
+
return;
|
|
1884
|
+
}
|
|
1885
|
+
child.stdout?.setEncoding("utf8");
|
|
1886
|
+
child.stdout?.on("data", (chunk) => {
|
|
1887
|
+
stdout += chunk;
|
|
1888
|
+
});
|
|
1889
|
+
child.stderr?.setEncoding("utf8");
|
|
1890
|
+
child.stderr?.on("data", (chunk) => {
|
|
1891
|
+
stderr += chunk;
|
|
1892
|
+
});
|
|
1893
|
+
child.once("error", (error2) => {
|
|
1894
|
+
finish({
|
|
1895
|
+
code: null,
|
|
1896
|
+
stdout,
|
|
1897
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
1898
|
+
${error2.message}`,
|
|
1899
|
+
timedOut: false
|
|
1900
|
+
});
|
|
1901
|
+
});
|
|
1902
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
1903
|
+
const timer = setTimeout(
|
|
1904
|
+
() => {
|
|
1905
|
+
child.kill("SIGKILL");
|
|
1906
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1907
|
+
},
|
|
1908
|
+
Math.max(0, options.timeoutMs)
|
|
1909
|
+
);
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
async function ensureLitestreamConfig(options, env) {
|
|
1913
|
+
const configPath = options.litestreamConfig;
|
|
1914
|
+
if (!configPath) {
|
|
1915
|
+
markNoReplicate(options, "no Litestream configuration path was provided");
|
|
1916
|
+
reportRecord(
|
|
1917
|
+
"restore",
|
|
1918
|
+
"restore_misconfigured",
|
|
1919
|
+
"litestream_config_unavailable",
|
|
1920
|
+
null,
|
|
1921
|
+
options
|
|
1922
|
+
);
|
|
1923
|
+
return null;
|
|
1924
|
+
}
|
|
1925
|
+
try {
|
|
1926
|
+
if (statSync2(configPath).size > 0) return configPath;
|
|
1927
|
+
} catch (error2) {
|
|
1928
|
+
if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
|
|
1929
|
+
options.log(
|
|
1930
|
+
`Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1931
|
+
"warn"
|
|
1932
|
+
);
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
const rendered = await runSynchroniser(["litestream-config"], {
|
|
1936
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
1937
|
+
env
|
|
1938
|
+
});
|
|
1939
|
+
logSynchroniserDiagnostics(rendered, options);
|
|
1940
|
+
if (rendered.timedOut || rendered.code !== 0) {
|
|
1941
|
+
options.log(
|
|
1942
|
+
`Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
|
|
1943
|
+
"error"
|
|
1944
|
+
);
|
|
1945
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1946
|
+
reportRecord(
|
|
1947
|
+
"restore",
|
|
1948
|
+
"restore_misconfigured",
|
|
1949
|
+
"litestream_config_unavailable",
|
|
1950
|
+
null,
|
|
1951
|
+
options
|
|
1952
|
+
);
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
try {
|
|
1956
|
+
mkdirSync(dirname2(configPath), { recursive: true });
|
|
1957
|
+
writeFileSync(configPath, rendered.stdout);
|
|
1958
|
+
} catch (error2) {
|
|
1959
|
+
options.log(
|
|
1960
|
+
`Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1961
|
+
"error"
|
|
1962
|
+
);
|
|
1963
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1964
|
+
reportRecord(
|
|
1965
|
+
"restore",
|
|
1966
|
+
"restore_misconfigured",
|
|
1967
|
+
"litestream_config_unavailable",
|
|
1968
|
+
null,
|
|
1969
|
+
options
|
|
1970
|
+
);
|
|
1971
|
+
return null;
|
|
1972
|
+
}
|
|
1973
|
+
const version2 = await runCommand("litestream", ["version"], {
|
|
1974
|
+
env,
|
|
1975
|
+
timeoutMs: 1e4
|
|
1976
|
+
});
|
|
1977
|
+
const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
|
|
1978
|
+
const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
|
|
1979
|
+
options.log(
|
|
1980
|
+
`litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
|
|
1981
|
+
);
|
|
1982
|
+
return configPath;
|
|
1983
|
+
}
|
|
1984
|
+
function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
|
|
1985
|
+
discardSessionDbDebris(options);
|
|
1986
|
+
markNoReplicate(options, message);
|
|
1987
|
+
reportRecord("restore", outcome, reason, litestreamExitCode, options);
|
|
1988
|
+
}
|
|
1989
|
+
async function restoreSessionDb(options, configPath, env) {
|
|
1990
|
+
const restored = await runCommand(
|
|
1991
|
+
"litestream",
|
|
1992
|
+
["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
|
|
1993
|
+
{ env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
|
|
1994
|
+
);
|
|
1995
|
+
for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
|
|
1996
|
+
if (restored.timedOut || restored.code === 124 || restored.code === 137) {
|
|
1997
|
+
restoreGiveUp(
|
|
1998
|
+
options,
|
|
1999
|
+
"restore_deadline_exceeded",
|
|
2000
|
+
`SESSION-DB-RESTORE-TRUNCATED: litestream restore did not finish within ${SESSION_DB_RESTORE_TIMEOUT_MS}ms; opencode starts with a fresh session DB and nothing is replicated this boot`,
|
|
2001
|
+
restored.code ?? 124
|
|
2002
|
+
);
|
|
2003
|
+
return;
|
|
2004
|
+
}
|
|
2005
|
+
if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
|
|
2006
|
+
options.log(`litestream restore could not run (${commandError(restored)})`, "error");
|
|
2007
|
+
restoreGiveUp(
|
|
2008
|
+
options,
|
|
2009
|
+
"restore_tool_unusable",
|
|
2010
|
+
`restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
|
|
2011
|
+
restored.code
|
|
2012
|
+
);
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
const classified = await runSynchroniser(
|
|
2016
|
+
[
|
|
2017
|
+
"session-db-classify",
|
|
2018
|
+
String(restored.code ?? 1),
|
|
2019
|
+
"1",
|
|
2020
|
+
"--on-unusable-replica=leave",
|
|
2021
|
+
"--fresh-db-fallback"
|
|
2022
|
+
],
|
|
2023
|
+
{ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
|
|
2024
|
+
);
|
|
2025
|
+
logSynchroniserDiagnostics(classified, options);
|
|
2026
|
+
const classifyCode = classified.code;
|
|
2027
|
+
switch (classifyCode) {
|
|
2028
|
+
case 0:
|
|
2029
|
+
return;
|
|
2030
|
+
case 31:
|
|
2031
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2032
|
+
options.log(
|
|
2033
|
+
"SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
|
|
2034
|
+
"warn"
|
|
2035
|
+
);
|
|
2036
|
+
return;
|
|
2037
|
+
case 32:
|
|
2038
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2039
|
+
discardSessionDbDebris(options);
|
|
2040
|
+
markNoReplicate(
|
|
2041
|
+
options,
|
|
2042
|
+
"session-db-classify asked for another restore attempt (32), but this boot has budget for only one; treating it as a give-up rather than retrying"
|
|
2043
|
+
);
|
|
2044
|
+
return;
|
|
2045
|
+
case 30:
|
|
2046
|
+
restoreGiveUp(
|
|
2047
|
+
options,
|
|
2048
|
+
"classification_fatal",
|
|
2049
|
+
"session-db-classify returned fatal (30); see the FATAL message above",
|
|
2050
|
+
restored.code,
|
|
2051
|
+
"restore_misconfigured"
|
|
2052
|
+
);
|
|
2053
|
+
return;
|
|
2054
|
+
default:
|
|
2055
|
+
restoreGiveUp(
|
|
2056
|
+
options,
|
|
2057
|
+
"classification_unrecognised",
|
|
2058
|
+
`session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
|
|
2059
|
+
restored.code
|
|
2060
|
+
);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
async function verifySessionDb(options, configPath, env) {
|
|
2064
|
+
if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
|
|
2065
|
+
const result = await runSynchroniser(["session-db-verify", configPath], {
|
|
2066
|
+
timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
|
|
2067
|
+
env: {
|
|
2068
|
+
...env,
|
|
2069
|
+
// The synchroniser reads this value in SECONDS. Keep this at 120, not
|
|
2070
|
+
// 120_000, so the walkback gives up before the outer process bound.
|
|
2071
|
+
EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
|
|
2072
|
+
SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
|
|
2073
|
+
)
|
|
2074
|
+
}
|
|
2075
|
+
});
|
|
2076
|
+
logSynchroniserDiagnostics(result, options);
|
|
2077
|
+
if (result.timedOut || result.code === 124 || result.code === 137) {
|
|
2078
|
+
options.log(
|
|
2079
|
+
`SESSION-DB-VERIFY-TIMEOUT: verification did not finish within its ${SESSION_DB_VERIFY_TIMEOUT_MS}ms deadline; continuing with the restored opencode.db as-is, unverified`,
|
|
2080
|
+
"warn"
|
|
2081
|
+
);
|
|
2082
|
+
return false;
|
|
2083
|
+
}
|
|
2084
|
+
if (result.code === 34) {
|
|
2085
|
+
reportRecord(
|
|
2086
|
+
"verify",
|
|
2087
|
+
"session_db_boot_refused",
|
|
2088
|
+
result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
|
|
2089
|
+
null,
|
|
2090
|
+
options
|
|
2091
|
+
);
|
|
2092
|
+
return true;
|
|
2093
|
+
}
|
|
2094
|
+
if (result.code === 33) {
|
|
2095
|
+
options.log(
|
|
2096
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
|
|
2097
|
+
"warn"
|
|
2098
|
+
);
|
|
2099
|
+
return false;
|
|
2100
|
+
}
|
|
2101
|
+
if (result.code !== 0) {
|
|
2102
|
+
options.log(
|
|
2103
|
+
`SESSION-DB-VERIFY-UNKNOWN: session-db-verify exited ${result.code ?? "null"}, which is none of its documented answers; continuing with the restored opencode.db as-is`,
|
|
2104
|
+
"warn"
|
|
2105
|
+
);
|
|
2106
|
+
}
|
|
2107
|
+
return false;
|
|
2108
|
+
}
|
|
2109
|
+
options.log(
|
|
2110
|
+
"skipping session-DB verification: this boot's session DB was not proven safe to replicate",
|
|
2111
|
+
"debug"
|
|
2112
|
+
);
|
|
2113
|
+
return false;
|
|
2114
|
+
}
|
|
2115
|
+
function fileExists(path) {
|
|
2116
|
+
try {
|
|
2117
|
+
statSync2(path);
|
|
2118
|
+
return true;
|
|
2119
|
+
} catch (error2) {
|
|
2120
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
|
|
2121
|
+
return true;
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
async function restoreAndVerifySessionDb(options) {
|
|
2125
|
+
const env = options.env ?? process.env;
|
|
2126
|
+
clearMarker(options);
|
|
2127
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2128
|
+
const synchroniserEnv = await runSynchroniser(["env"], {
|
|
2129
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
2130
|
+
env
|
|
2131
|
+
});
|
|
2132
|
+
logSynchroniserDiagnostics(synchroniserEnv, options);
|
|
2133
|
+
if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
|
|
2134
|
+
options.log(
|
|
2135
|
+
`runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
|
|
2136
|
+
"error"
|
|
2137
|
+
);
|
|
2138
|
+
markNoReplicate(
|
|
2139
|
+
options,
|
|
2140
|
+
"could not resolve the runner-synchroniser configuration (see the ERROR above)"
|
|
2141
|
+
);
|
|
2142
|
+
reportRecord(
|
|
2143
|
+
"restore",
|
|
2144
|
+
"restore_misconfigured",
|
|
2145
|
+
"synchroniser_config_unresolved",
|
|
2146
|
+
null,
|
|
2147
|
+
options
|
|
2148
|
+
);
|
|
2149
|
+
return { verifyFatal: false };
|
|
2150
|
+
}
|
|
2151
|
+
const values = parseSynchroniserEnv(synchroniserEnv.stdout);
|
|
2152
|
+
if (!values) {
|
|
2153
|
+
markNoReplicate(
|
|
2154
|
+
options,
|
|
2155
|
+
"the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
|
|
2156
|
+
);
|
|
2157
|
+
reportRecord(
|
|
2158
|
+
"restore",
|
|
2159
|
+
"restore_misconfigured",
|
|
2160
|
+
"synchroniser_config_unevaluable",
|
|
2161
|
+
null,
|
|
2162
|
+
options
|
|
2163
|
+
);
|
|
2164
|
+
return { verifyFatal: false };
|
|
2165
|
+
}
|
|
2166
|
+
const synchroniserDbPath = values.OPENCODE_DB_PATH;
|
|
2167
|
+
if (!synchroniserDbPath) {
|
|
2168
|
+
markNoReplicate(
|
|
2169
|
+
options,
|
|
2170
|
+
"run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
|
|
2171
|
+
);
|
|
2172
|
+
reportRecord(
|
|
2173
|
+
"restore",
|
|
2174
|
+
"restore_misconfigured",
|
|
2175
|
+
"synchroniser_config_incomplete",
|
|
2176
|
+
null,
|
|
2177
|
+
options
|
|
2178
|
+
);
|
|
2179
|
+
return { verifyFatal: false };
|
|
2180
|
+
}
|
|
2181
|
+
if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
|
|
2182
|
+
options.log(
|
|
2183
|
+
`runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
|
|
2184
|
+
"warn"
|
|
2185
|
+
);
|
|
2186
|
+
}
|
|
2187
|
+
if (!values.PERSISTENCE_BUCKET) {
|
|
2188
|
+
options.log(
|
|
2189
|
+
"SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
|
|
2190
|
+
"warn"
|
|
2191
|
+
);
|
|
2192
|
+
return { verifyFatal: false };
|
|
2193
|
+
}
|
|
2194
|
+
const configPath = await ensureLitestreamConfig(options, env);
|
|
2195
|
+
if (!configPath) return { verifyFatal: false };
|
|
2196
|
+
await restoreSessionDb(options, configPath, env);
|
|
2197
|
+
if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
|
|
2198
|
+
return { verifyFatal: false };
|
|
2199
|
+
}
|
|
2200
|
+
return { verifyFatal: await verifySessionDb(options, configPath, env) };
|
|
2201
|
+
}
|
|
2202
|
+
|
|
1605
2203
|
// src/lib/opencode/opencode-version-gate.ts
|
|
1606
2204
|
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
1607
2205
|
function isQueueValidatedVersion(version2) {
|
|
@@ -1616,7 +2214,63 @@ function buildOpenCodeVersionWarning(version2) {
|
|
|
1616
2214
|
}
|
|
1617
2215
|
|
|
1618
2216
|
// src/lib/opencode/process.ts
|
|
1619
|
-
import { execSync, spawn } from "child_process";
|
|
2217
|
+
import { execSync, spawn as spawn3 } from "child_process";
|
|
2218
|
+
|
|
2219
|
+
// src/lib/process-stop.ts
|
|
2220
|
+
async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
2221
|
+
if (!child.pid) {
|
|
2222
|
+
return { outcome: "not-running", code: child.exitCode, signal: child.signalCode };
|
|
2223
|
+
}
|
|
2224
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
2225
|
+
return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
|
|
2226
|
+
}
|
|
2227
|
+
return new Promise((resolve4, reject) => {
|
|
2228
|
+
let forced = false;
|
|
2229
|
+
let settled = false;
|
|
2230
|
+
const timer = setTimeout(() => {
|
|
2231
|
+
forced = true;
|
|
2232
|
+
try {
|
|
2233
|
+
sendKill();
|
|
2234
|
+
} catch (error2) {
|
|
2235
|
+
if (error2.code === "ESRCH") {
|
|
2236
|
+
finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
|
|
2237
|
+
} else {
|
|
2238
|
+
fail(error2);
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
}, timeoutMs);
|
|
2242
|
+
const finish = (result) => {
|
|
2243
|
+
if (settled) return;
|
|
2244
|
+
settled = true;
|
|
2245
|
+
clearTimeout(timer);
|
|
2246
|
+
child.removeListener("exit", onExit);
|
|
2247
|
+
resolve4(result);
|
|
2248
|
+
};
|
|
2249
|
+
const fail = (error2) => {
|
|
2250
|
+
if (settled) return;
|
|
2251
|
+
settled = true;
|
|
2252
|
+
clearTimeout(timer);
|
|
2253
|
+
child.removeListener("exit", onExit);
|
|
2254
|
+
reject(error2);
|
|
2255
|
+
};
|
|
2256
|
+
const onExit = (code, signal) => {
|
|
2257
|
+
finish({ outcome: forced ? "killed" : "exited", code, signal });
|
|
2258
|
+
};
|
|
2259
|
+
child.once("exit", onExit);
|
|
2260
|
+
try {
|
|
2261
|
+
sendTerm();
|
|
2262
|
+
} catch (error2) {
|
|
2263
|
+
if (error2.code === "ESRCH") {
|
|
2264
|
+
finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
|
|
2265
|
+
} else {
|
|
2266
|
+
fail(error2);
|
|
2267
|
+
}
|
|
2268
|
+
return;
|
|
2269
|
+
}
|
|
2270
|
+
});
|
|
2271
|
+
}
|
|
2272
|
+
|
|
2273
|
+
// src/lib/opencode/process.ts
|
|
1620
2274
|
var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
|
|
1621
2275
|
function getProcessCwd(pid) {
|
|
1622
2276
|
const platform = process.platform;
|
|
@@ -1772,39 +2426,45 @@ async function findHealthyOpenCodeInstances() {
|
|
|
1772
2426
|
}
|
|
1773
2427
|
return healthy;
|
|
1774
2428
|
}
|
|
1775
|
-
async function startOpenCode(port) {
|
|
2429
|
+
async function startOpenCode(port, options = {}) {
|
|
1776
2430
|
let command = "opencode";
|
|
1777
|
-
|
|
2431
|
+
const printLogs = options.inheritStdio ? ["--print-logs"] : [];
|
|
2432
|
+
let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
|
|
1778
2433
|
try {
|
|
1779
2434
|
execSync("which opencode", { stdio: "ignore" });
|
|
1780
2435
|
} catch {
|
|
1781
2436
|
command = "npx";
|
|
1782
|
-
args = [
|
|
1783
|
-
|
|
1784
|
-
|
|
2437
|
+
args = [
|
|
2438
|
+
"opencode",
|
|
2439
|
+
"serve",
|
|
2440
|
+
"--port",
|
|
2441
|
+
port.toString(),
|
|
2442
|
+
"--hostname",
|
|
2443
|
+
"127.0.0.1",
|
|
2444
|
+
...printLogs
|
|
2445
|
+
];
|
|
2446
|
+
}
|
|
2447
|
+
const child = spawn3(command, args, {
|
|
1785
2448
|
detached: true,
|
|
1786
|
-
stdio: "ignore",
|
|
2449
|
+
stdio: options.inheritStdio ? "inherit" : "ignore",
|
|
1787
2450
|
cwd: process.cwd()
|
|
1788
2451
|
});
|
|
1789
2452
|
return child;
|
|
1790
2453
|
}
|
|
1791
|
-
function
|
|
1792
|
-
|
|
1793
|
-
return;
|
|
1794
|
-
}
|
|
1795
|
-
try {
|
|
2454
|
+
function stopOpenCodeAndWait(opencodeProcess, timeoutMs) {
|
|
2455
|
+
const sendSignal = (signal) => {
|
|
1796
2456
|
if (process.platform === "win32") {
|
|
1797
|
-
opencodeProcess.kill(
|
|
2457
|
+
opencodeProcess.kill(signal);
|
|
1798
2458
|
} else {
|
|
1799
|
-
process.kill(-opencodeProcess.pid,
|
|
2459
|
+
process.kill(-opencodeProcess.pid, signal);
|
|
1800
2460
|
}
|
|
1801
|
-
}
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
2461
|
+
};
|
|
2462
|
+
return stopProcessAndWait(
|
|
2463
|
+
opencodeProcess,
|
|
2464
|
+
timeoutMs,
|
|
2465
|
+
() => sendSignal("SIGTERM"),
|
|
2466
|
+
() => sendSignal("SIGKILL")
|
|
2467
|
+
);
|
|
1808
2468
|
}
|
|
1809
2469
|
|
|
1810
2470
|
// src/lib/opencode/install.ts
|
|
@@ -2091,6 +2751,7 @@ async function createOpenCodeSession(port, directory) {
|
|
|
2091
2751
|
return data.id;
|
|
2092
2752
|
}
|
|
2093
2753
|
async function getModelAttachmentCapability(port, model) {
|
|
2754
|
+
const { model: baseModel } = splitModelVariant(model);
|
|
2094
2755
|
try {
|
|
2095
2756
|
const res = await timedFetch(`${opencodeBase(port)}/config/providers`);
|
|
2096
2757
|
if (!res.ok) {
|
|
@@ -2107,9 +2768,9 @@ async function getModelAttachmentCapability(port, model) {
|
|
|
2107
2768
|
);
|
|
2108
2769
|
return null;
|
|
2109
2770
|
}
|
|
2110
|
-
const slash =
|
|
2111
|
-
const providerId = slash > 0 ?
|
|
2112
|
-
let modelId = slash > 0 ?
|
|
2771
|
+
const slash = baseModel ? baseModel.indexOf("/") : -1;
|
|
2772
|
+
const providerId = slash > 0 ? baseModel.slice(0, slash) : void 0;
|
|
2773
|
+
let modelId = slash > 0 ? baseModel.slice(slash + 1) : void 0;
|
|
2113
2774
|
const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
|
|
2114
2775
|
let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
|
|
2115
2776
|
if (!provider && !providerId) {
|
|
@@ -2189,6 +2850,29 @@ async function buildFileParts(attachments, capable) {
|
|
|
2189
2850
|
}
|
|
2190
2851
|
return { parts, outcomes, capabilityUnknown };
|
|
2191
2852
|
}
|
|
2853
|
+
function splitModelVariant(raw) {
|
|
2854
|
+
const value = raw?.trim();
|
|
2855
|
+
if (!value) return {};
|
|
2856
|
+
const hashIndex = value.indexOf("#");
|
|
2857
|
+
if (hashIndex === -1) return { model: value };
|
|
2858
|
+
const model = value.slice(0, hashIndex).trim() || void 0;
|
|
2859
|
+
const variant = value.slice(hashIndex + 1).trim() || void 0;
|
|
2860
|
+
return { model, variant };
|
|
2861
|
+
}
|
|
2862
|
+
function applyModelOptions(body, options) {
|
|
2863
|
+
if (options?.agent) body.agent = options.agent;
|
|
2864
|
+
const { model, variant } = splitModelVariant(options?.model);
|
|
2865
|
+
if (model) {
|
|
2866
|
+
const slashIndex = model.indexOf("/");
|
|
2867
|
+
if (slashIndex !== -1) {
|
|
2868
|
+
body.model = {
|
|
2869
|
+
providerID: model.substring(0, slashIndex),
|
|
2870
|
+
modelID: model.substring(slashIndex + 1)
|
|
2871
|
+
};
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
if (variant) body.variant = variant;
|
|
2875
|
+
}
|
|
2192
2876
|
function messageText(m) {
|
|
2193
2877
|
if (!m || !Array.isArray(m.parts)) return "";
|
|
2194
2878
|
return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
@@ -2213,18 +2897,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2213
2897
|
const body = {
|
|
2214
2898
|
parts
|
|
2215
2899
|
};
|
|
2216
|
-
|
|
2217
|
-
body.agent = options.agent;
|
|
2218
|
-
}
|
|
2219
|
-
if (options?.model) {
|
|
2220
|
-
const slashIndex = options.model.indexOf("/");
|
|
2221
|
-
if (slashIndex !== -1) {
|
|
2222
|
-
body.model = {
|
|
2223
|
-
providerID: options.model.substring(0, slashIndex),
|
|
2224
|
-
modelID: options.model.substring(slashIndex + 1)
|
|
2225
|
-
};
|
|
2226
|
-
}
|
|
2227
|
-
}
|
|
2900
|
+
applyModelOptions(body, options);
|
|
2228
2901
|
const res = await timedFetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
|
|
2229
2902
|
method: "POST",
|
|
2230
2903
|
headers: { "Content-Type": "application/json" },
|
|
@@ -2232,7 +2905,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2232
2905
|
});
|
|
2233
2906
|
if (res.status < 200 || res.status >= 300) {
|
|
2234
2907
|
const text = await res.text().catch(() => "");
|
|
2235
|
-
|
|
2908
|
+
const { variant } = splitModelVariant(options?.model);
|
|
2909
|
+
throw new Error(
|
|
2910
|
+
`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}${variant ? ` (variant: ${variant})` : ""}`
|
|
2911
|
+
);
|
|
2236
2912
|
}
|
|
2237
2913
|
const READ_BACK_ATTEMPTS = 5;
|
|
2238
2914
|
const READ_BACK_DELAY_MS = 150;
|
|
@@ -2256,7 +2932,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2256
2932
|
}
|
|
2257
2933
|
}
|
|
2258
2934
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
2259
|
-
await new Promise((
|
|
2935
|
+
await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
|
|
2260
2936
|
}
|
|
2261
2937
|
}
|
|
2262
2938
|
return null;
|
|
@@ -2387,7 +3063,7 @@ function isPreamblePinnedRunning(messages, userMessageId) {
|
|
|
2387
3063
|
return completedOf(reply) != null && finishOf(reply) === "tool-calls";
|
|
2388
3064
|
}
|
|
2389
3065
|
function isB2AbandonmentConfirmed(params) {
|
|
2390
|
-
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
|
|
3066
|
+
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false && params.rootOngoing === false;
|
|
2391
3067
|
}
|
|
2392
3068
|
function isAmbiguousTerminalFinish(m) {
|
|
2393
3069
|
if (completedOf(m) == null) return false;
|
|
@@ -2400,7 +3076,7 @@ function isAmbiguousFinishPinnedRunning(messages, userMessageId) {
|
|
|
2400
3076
|
return isAmbiguousTerminalFinish(reply);
|
|
2401
3077
|
}
|
|
2402
3078
|
function isAmbiguousFinishResolved(params) {
|
|
2403
|
-
return params.sessionOngoing === false || params.pinnedForMs >= params.maxPinnedMs;
|
|
3079
|
+
return params.sessionOngoing === false || params.sessionOngoing !== true && params.pinnedForMs >= params.maxPinnedMs;
|
|
2404
3080
|
}
|
|
2405
3081
|
function messageError(messages, userMessageId) {
|
|
2406
3082
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
@@ -2609,13 +3285,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
|
2609
3285
|
}
|
|
2610
3286
|
|
|
2611
3287
|
// src/lib/opencode/session-db-size.ts
|
|
2612
|
-
import { statSync as
|
|
3288
|
+
import { statSync as statSync3 } from "fs";
|
|
2613
3289
|
import { join as join3 } from "path";
|
|
2614
3290
|
var LARGE_DB_THRESHOLD_BYTES = 268435456;
|
|
2615
3291
|
function statSessionDbBytes(homeDir) {
|
|
2616
3292
|
const dbPath = join3(homeDir, ".local", "share", "opencode", "opencode.db");
|
|
2617
3293
|
try {
|
|
2618
|
-
return
|
|
3294
|
+
return statSync3(dbPath).size;
|
|
2619
3295
|
} catch (err) {
|
|
2620
3296
|
const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
|
|
2621
3297
|
if (!isMissingFile) {
|
|
@@ -2641,11 +3317,11 @@ function buildSessionStoreSizeWarning(input) {
|
|
|
2641
3317
|
}
|
|
2642
3318
|
|
|
2643
3319
|
// src/lib/opencode/session-db-reclaim.ts
|
|
2644
|
-
import { statSync as
|
|
2645
|
-
import { dirname as
|
|
3320
|
+
import { statSync as statSync4, statfsSync } from "fs";
|
|
3321
|
+
import { dirname as dirname3 } from "path";
|
|
2646
3322
|
function insufficientSpaceReason(dbPath, requiredBytes) {
|
|
2647
3323
|
try {
|
|
2648
|
-
const fsStats = statfsSync(
|
|
3324
|
+
const fsStats = statfsSync(dirname3(dbPath));
|
|
2649
3325
|
const availableBytes = fsStats.bavail * fsStats.bsize;
|
|
2650
3326
|
if (availableBytes < requiredBytes) {
|
|
2651
3327
|
return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
|
|
@@ -2714,7 +3390,7 @@ async function reclaimSessionDbSpace(input) {
|
|
|
2714
3390
|
);
|
|
2715
3391
|
return { ok: false, skipped: "full-vacuum-blocked" };
|
|
2716
3392
|
}
|
|
2717
|
-
const fileBytesForGuard =
|
|
3393
|
+
const fileBytesForGuard = statSync4(dbPath).size;
|
|
2718
3394
|
const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
|
|
2719
3395
|
if (skipReason !== null) {
|
|
2720
3396
|
console.warn(
|
|
@@ -2842,12 +3518,12 @@ var StreamForwarder = class {
|
|
|
2842
3518
|
let endBody;
|
|
2843
3519
|
if (has_body) {
|
|
2844
3520
|
const chunks = [];
|
|
2845
|
-
bodyPromise = new Promise((
|
|
3521
|
+
bodyPromise = new Promise((resolve4) => {
|
|
2846
3522
|
pushBody = (buf) => {
|
|
2847
3523
|
chunks.push(buf);
|
|
2848
3524
|
};
|
|
2849
3525
|
endBody = () => {
|
|
2850
|
-
|
|
3526
|
+
resolve4(Buffer.concat(chunks));
|
|
2851
3527
|
};
|
|
2852
3528
|
});
|
|
2853
3529
|
}
|
|
@@ -2976,7 +3652,7 @@ function connectTunnel(options) {
|
|
|
2976
3652
|
} = options;
|
|
2977
3653
|
const tunnelUrl = getTunnelUrlConfig();
|
|
2978
3654
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
2979
|
-
return new Promise((
|
|
3655
|
+
return new Promise((resolve4, reject) => {
|
|
2980
3656
|
const ws = new WebSocket2(url, {
|
|
2981
3657
|
headers: {
|
|
2982
3658
|
Authorization: authHeader
|
|
@@ -3040,7 +3716,7 @@ function connectTunnel(options) {
|
|
|
3040
3716
|
clearTimeout(connectionTimeout);
|
|
3041
3717
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
3042
3718
|
onConnected?.(connectedAgentId);
|
|
3043
|
-
|
|
3719
|
+
resolve4({
|
|
3044
3720
|
ws,
|
|
3045
3721
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
3046
3722
|
});
|
|
@@ -3171,10 +3847,10 @@ var RunnerConnection = class {
|
|
|
3171
3847
|
};
|
|
3172
3848
|
|
|
3173
3849
|
// src/lib/tunnel/ready-marker.ts
|
|
3174
|
-
import { writeFileSync } from "fs";
|
|
3850
|
+
import { writeFileSync as writeFileSync2 } from "fs";
|
|
3175
3851
|
function writeTunnelReadyMarker(path, agentId) {
|
|
3176
3852
|
try {
|
|
3177
|
-
|
|
3853
|
+
writeFileSync2(path, `${agentId}
|
|
3178
3854
|
`);
|
|
3179
3855
|
return { ok: true };
|
|
3180
3856
|
} catch (error2) {
|
|
@@ -3182,9 +3858,51 @@ function writeTunnelReadyMarker(path, agentId) {
|
|
|
3182
3858
|
}
|
|
3183
3859
|
}
|
|
3184
3860
|
|
|
3185
|
-
// src/lib/
|
|
3861
|
+
// src/lib/replication.ts
|
|
3862
|
+
import { spawn as spawn4 } from "child_process";
|
|
3863
|
+
function startSessionDbReplication(configPath) {
|
|
3864
|
+
return spawn4("litestream", ["replicate", "-config", configPath], {
|
|
3865
|
+
stdio: "inherit"
|
|
3866
|
+
});
|
|
3867
|
+
}
|
|
3868
|
+
async function stopSessionDbReplication(child, timeoutMs) {
|
|
3869
|
+
return stopProcessAndWait(
|
|
3870
|
+
child,
|
|
3871
|
+
timeoutMs,
|
|
3872
|
+
() => child.kill("SIGTERM"),
|
|
3873
|
+
() => child.kill("SIGKILL")
|
|
3874
|
+
);
|
|
3875
|
+
}
|
|
3876
|
+
|
|
3877
|
+
// src/lib/process-liveness.ts
|
|
3186
3878
|
import { readFileSync as readFileSync3 } from "fs";
|
|
3187
|
-
|
|
3879
|
+
function isProcessAlive(pid) {
|
|
3880
|
+
try {
|
|
3881
|
+
process.kill(pid, 0);
|
|
3882
|
+
} catch (error2) {
|
|
3883
|
+
const code = error2.code;
|
|
3884
|
+
if (code === "ESRCH") return false;
|
|
3885
|
+
if (code === "EPERM") return true;
|
|
3886
|
+
console.error(
|
|
3887
|
+
`[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
3888
|
+
);
|
|
3889
|
+
return false;
|
|
3890
|
+
}
|
|
3891
|
+
if (process.platform !== "linux") return true;
|
|
3892
|
+
try {
|
|
3893
|
+
const status2 = readFileSync3(`/proc/${pid}/status`, "utf8");
|
|
3894
|
+
return !/^State:\s+Z(?:\s|$)/m.test(status2);
|
|
3895
|
+
} catch (error2) {
|
|
3896
|
+
console.error(
|
|
3897
|
+
`[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
3898
|
+
);
|
|
3899
|
+
return true;
|
|
3900
|
+
}
|
|
3901
|
+
}
|
|
3902
|
+
|
|
3903
|
+
// src/lib/openai-usage.ts
|
|
3904
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
3905
|
+
import { homedir as homedir3 } from "os";
|
|
3188
3906
|
import { join as join4 } from "path";
|
|
3189
3907
|
var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
3190
3908
|
var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
|
|
@@ -3199,7 +3917,7 @@ function isLocalCredentialProblem2(err) {
|
|
|
3199
3917
|
}
|
|
3200
3918
|
function readOpenCodeChatGptCredentials() {
|
|
3201
3919
|
try {
|
|
3202
|
-
const raw =
|
|
3920
|
+
const raw = readFileSync4(join4(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
|
|
3203
3921
|
let parsed;
|
|
3204
3922
|
try {
|
|
3205
3923
|
parsed = JSON.parse(raw);
|
|
@@ -3572,7 +4290,7 @@ function createResourceUsageCollector(homeDir) {
|
|
|
3572
4290
|
}
|
|
3573
4291
|
|
|
3574
4292
|
// src/lib/channels/driver.ts
|
|
3575
|
-
import { homedir as
|
|
4293
|
+
import { homedir as homedir4 } from "os";
|
|
3576
4294
|
|
|
3577
4295
|
// src/lib/runner-file-sync.ts
|
|
3578
4296
|
import { join as join6 } from "path";
|
|
@@ -3580,7 +4298,7 @@ import { join as join6 } from "path";
|
|
|
3580
4298
|
// src/lib/file-push.ts
|
|
3581
4299
|
import { randomUUID } from "crypto";
|
|
3582
4300
|
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
3583
|
-
import { basename, dirname as
|
|
4301
|
+
import { basename, dirname as dirname4, isAbsolute, join as join5, relative, resolve as resolve2, sep } from "path";
|
|
3584
4302
|
var FILE_MODE = 384;
|
|
3585
4303
|
var DIRECTORY_MODE = 448;
|
|
3586
4304
|
async function writePushedFile(request) {
|
|
@@ -3611,7 +4329,7 @@ async function writePushedFile(request) {
|
|
|
3611
4329
|
}
|
|
3612
4330
|
try {
|
|
3613
4331
|
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
3614
|
-
|
|
4332
|
+
dirname4(candidate)
|
|
3615
4333
|
);
|
|
3616
4334
|
const realTarget = join5(existingAncestor, ...missingSegments, basename(candidate));
|
|
3617
4335
|
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
@@ -3623,8 +4341,8 @@ async function writePushedFile(request) {
|
|
|
3623
4341
|
}
|
|
3624
4342
|
if (missingSegments.length > 0) {
|
|
3625
4343
|
await createMissingDirectories(existingAncestor, missingSegments);
|
|
3626
|
-
const realParent = await realpath(
|
|
3627
|
-
if (realParent !==
|
|
4344
|
+
const realParent = await realpath(dirname4(realTarget));
|
|
4345
|
+
if (realParent !== dirname4(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
3628
4346
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
3629
4347
|
path: realTarget,
|
|
3630
4348
|
bytes,
|
|
@@ -3667,7 +4385,7 @@ async function resolveNearestExistingAncestor(directory) {
|
|
|
3667
4385
|
try {
|
|
3668
4386
|
return { existingAncestor: await realpath(current), missingSegments };
|
|
3669
4387
|
} catch (err) {
|
|
3670
|
-
const parent =
|
|
4388
|
+
const parent = dirname4(current);
|
|
3671
4389
|
if (err.code !== "ENOENT" || parent === current) {
|
|
3672
4390
|
throw err;
|
|
3673
4391
|
}
|
|
@@ -3728,7 +4446,7 @@ async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
|
3728
4446
|
}
|
|
3729
4447
|
}
|
|
3730
4448
|
async function writeAtomically(realTarget, content) {
|
|
3731
|
-
const temporaryPath = join5(
|
|
4449
|
+
const temporaryPath = join5(dirname4(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
3732
4450
|
let handle;
|
|
3733
4451
|
try {
|
|
3734
4452
|
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
@@ -4409,7 +5127,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4409
5127
|
this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
4410
5128
|
this.now = config.now ?? (() => Date.now());
|
|
4411
5129
|
this.fileSyncDirectories = config.fileSyncDirectories ?? [];
|
|
4412
|
-
this.homeDir = config.homeDir ??
|
|
5130
|
+
this.homeDir = config.homeDir ?? homedir4();
|
|
4413
5131
|
this.maxActiveSessions = config.maxActiveSessions;
|
|
4414
5132
|
this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
|
|
4415
5133
|
this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
|
|
@@ -5676,6 +6394,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5676
6394
|
deliveryDeadlineAnchored: false,
|
|
5677
6395
|
b2PinnedSinceMs: 0,
|
|
5678
6396
|
b2LastDescendantCheckMs: 0,
|
|
6397
|
+
b2RootOngoingHeldLogged: false,
|
|
5679
6398
|
b2AbandonedSignalled: false,
|
|
5680
6399
|
ambiguousPinnedSinceMs: 0,
|
|
5681
6400
|
ambiguousResolved: false
|
|
@@ -5770,6 +6489,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5770
6489
|
deliveryDeadlineAnchored: false,
|
|
5771
6490
|
b2PinnedSinceMs: 0,
|
|
5772
6491
|
b2LastDescendantCheckMs: 0,
|
|
6492
|
+
b2RootOngoingHeldLogged: false,
|
|
5773
6493
|
b2AbandonedSignalled: false,
|
|
5774
6494
|
ambiguousPinnedSinceMs: 0,
|
|
5775
6495
|
ambiguousResolved: false
|
|
@@ -6123,6 +6843,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6123
6843
|
if (snapshotReadable) {
|
|
6124
6844
|
inFlight.b2PinnedSinceMs = 0;
|
|
6125
6845
|
inFlight.b2LastDescendantCheckMs = 0;
|
|
6846
|
+
inFlight.b2RootOngoingHeldLogged = false;
|
|
6126
6847
|
inFlight.b2AbandonedSignalled = false;
|
|
6127
6848
|
}
|
|
6128
6849
|
} else {
|
|
@@ -6134,11 +6855,15 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6134
6855
|
const pinnedForMs = this.now() - inFlight.b2PinnedSinceMs;
|
|
6135
6856
|
if (pinnedForMs >= B2_ABANDONMENT_MIN_PINNED_MS && this.now() - inFlight.b2LastDescendantCheckMs >= B2_ABANDONMENT_RECHECK_MS) {
|
|
6136
6857
|
inFlight.b2LastDescendantCheckMs = this.now();
|
|
6137
|
-
const descendantOngoing = await
|
|
6858
|
+
const [descendantOngoing, rootOngoing] = await Promise.all([
|
|
6859
|
+
this.isAnyDescendantSessionOngoing(sessionId),
|
|
6860
|
+
isSessionOngoing(this.port, sessionId)
|
|
6861
|
+
]);
|
|
6138
6862
|
if (isB2AbandonmentConfirmed({
|
|
6139
6863
|
pinnedForMs,
|
|
6140
6864
|
minPinnedMs: B2_ABANDONMENT_MIN_PINNED_MS,
|
|
6141
|
-
descendantOngoing
|
|
6865
|
+
descendantOngoing,
|
|
6866
|
+
rootOngoing
|
|
6142
6867
|
})) {
|
|
6143
6868
|
inFlight.b2AbandonedSignalled = true;
|
|
6144
6869
|
this.log({
|
|
@@ -6147,12 +6872,26 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6147
6872
|
conversation_id: conv.id,
|
|
6148
6873
|
message_id: id
|
|
6149
6874
|
});
|
|
6875
|
+
const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
|
|
6150
6876
|
void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
|
|
6151
|
-
watched_for_ms: pinnedForMs
|
|
6877
|
+
watched_for_ms: pinnedForMs,
|
|
6878
|
+
finish: reply?.info?.finish ?? reply?.finish,
|
|
6879
|
+
...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
|
|
6880
|
+
...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
|
|
6881
|
+
opencode_message_id: inFlight.opencodeMessageId
|
|
6152
6882
|
});
|
|
6153
6883
|
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
6154
6884
|
return;
|
|
6155
6885
|
}
|
|
6886
|
+
if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
|
|
6887
|
+
inFlight.b2RootOngoingHeldLogged = true;
|
|
6888
|
+
this.log({
|
|
6889
|
+
level: "warn",
|
|
6890
|
+
message: `Message ${id.slice(0, 8)} b2-pinned for ${Math.round(pinnedForMs / 1e3)}s with root_ongoing=${rootOngoing} and descendant_ongoing=${descendantOngoing} \u2014 holding until OpenCode confirms the root is idle`,
|
|
6891
|
+
conversation_id: conv.id,
|
|
6892
|
+
message_id: id
|
|
6893
|
+
});
|
|
6894
|
+
}
|
|
6156
6895
|
}
|
|
6157
6896
|
}
|
|
6158
6897
|
const ambiguousPinnedNow = activelyRunning && isAmbiguousFinishPinnedRunning(messages, inFlight.opencodeMessageId);
|
|
@@ -7809,7 +8548,7 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
7809
8548
|
}
|
|
7810
8549
|
if (!ctx.interactive) {
|
|
7811
8550
|
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
7812
|
-
const proc = await startOpenCode(ctx.port);
|
|
8551
|
+
const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
|
|
7813
8552
|
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
7814
8553
|
if (!health.healthy) {
|
|
7815
8554
|
return {
|
|
@@ -7877,7 +8616,7 @@ Port ${port} is already in use.`));
|
|
|
7877
8616
|
}
|
|
7878
8617
|
if (action === "start") {
|
|
7879
8618
|
const spinner = ora2("Starting OpenCode...").start();
|
|
7880
|
-
const proc = await startOpenCode(port);
|
|
8619
|
+
const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
|
|
7881
8620
|
const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
|
|
7882
8621
|
if (!health.healthy) {
|
|
7883
8622
|
spinner.fail("Failed to start OpenCode");
|
|
@@ -7889,12 +8628,323 @@ Port ${port} is already in use.`));
|
|
|
7889
8628
|
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
7890
8629
|
}
|
|
7891
8630
|
|
|
8631
|
+
// src/lib/runner-credentials.ts
|
|
8632
|
+
import { chmodSync as chmodSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
8633
|
+
import { spawn as spawn5 } from "child_process";
|
|
8634
|
+
var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
|
|
8635
|
+
var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
|
|
8636
|
+
var GITHUB_PROBE_TIMEOUT_MS = 1e4;
|
|
8637
|
+
var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
|
|
8638
|
+
var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
|
|
8639
|
+
function commandError2(result) {
|
|
8640
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
8641
|
+
}
|
|
8642
|
+
var runCommand2 = (command, args, opts) => {
|
|
8643
|
+
return new Promise((resolve4) => {
|
|
8644
|
+
let child;
|
|
8645
|
+
let stdout = "";
|
|
8646
|
+
let stderr = "";
|
|
8647
|
+
let settled = false;
|
|
8648
|
+
const timer = {};
|
|
8649
|
+
const finish = (result) => {
|
|
8650
|
+
if (settled) return;
|
|
8651
|
+
settled = true;
|
|
8652
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
8653
|
+
resolve4(result);
|
|
8654
|
+
};
|
|
8655
|
+
try {
|
|
8656
|
+
child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
|
|
8657
|
+
} catch (error2) {
|
|
8658
|
+
finish({
|
|
8659
|
+
code: null,
|
|
8660
|
+
stdout,
|
|
8661
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
8662
|
+
timedOut: false
|
|
8663
|
+
});
|
|
8664
|
+
return;
|
|
8665
|
+
}
|
|
8666
|
+
child.stdout?.setEncoding("utf8");
|
|
8667
|
+
child.stdout?.on("data", (chunk) => {
|
|
8668
|
+
stdout += chunk;
|
|
8669
|
+
});
|
|
8670
|
+
child.stderr?.setEncoding("utf8");
|
|
8671
|
+
child.stderr?.on("data", (chunk) => {
|
|
8672
|
+
stderr += chunk;
|
|
8673
|
+
});
|
|
8674
|
+
child.once("error", (error2) => {
|
|
8675
|
+
finish({
|
|
8676
|
+
code: null,
|
|
8677
|
+
stdout,
|
|
8678
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
8679
|
+
${error2.message}`,
|
|
8680
|
+
timedOut: false
|
|
8681
|
+
});
|
|
8682
|
+
});
|
|
8683
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
8684
|
+
timer.handle = setTimeout(
|
|
8685
|
+
() => {
|
|
8686
|
+
child.kill("SIGKILL");
|
|
8687
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
8688
|
+
},
|
|
8689
|
+
Math.max(0, opts.timeoutMs)
|
|
8690
|
+
);
|
|
8691
|
+
});
|
|
8692
|
+
};
|
|
8693
|
+
function isEnvironmentObject(value) {
|
|
8694
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8695
|
+
}
|
|
8696
|
+
function secretFailure(marker, detail, log3) {
|
|
8697
|
+
const message = `${marker}: ${detail}`;
|
|
8698
|
+
log3(message, "error");
|
|
8699
|
+
return new Error(message);
|
|
8700
|
+
}
|
|
8701
|
+
async function installRunnerSecret({
|
|
8702
|
+
env,
|
|
8703
|
+
log: log3,
|
|
8704
|
+
commandRunner
|
|
8705
|
+
}) {
|
|
8706
|
+
const arn = env.RUNNER_SECRET_ARN?.trim();
|
|
8707
|
+
if (!arn) {
|
|
8708
|
+
log3("runner secret is not configured; continuing without GitHub and MCP credentials");
|
|
8709
|
+
return false;
|
|
8710
|
+
}
|
|
8711
|
+
const result = await (commandRunner ?? runCommand2)(
|
|
8712
|
+
"aws",
|
|
8713
|
+
[
|
|
8714
|
+
"secretsmanager",
|
|
8715
|
+
"get-secret-value",
|
|
8716
|
+
"--secret-id",
|
|
8717
|
+
arn,
|
|
8718
|
+
"--query",
|
|
8719
|
+
"SecretString",
|
|
8720
|
+
"--output",
|
|
8721
|
+
"text"
|
|
8722
|
+
],
|
|
8723
|
+
{ env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
|
|
8724
|
+
);
|
|
8725
|
+
if (result.timedOut) {
|
|
8726
|
+
throw secretFailure(
|
|
8727
|
+
"CREDENTIAL-RESTORE-TIMEOUT",
|
|
8728
|
+
`runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
|
|
8729
|
+
log3
|
|
8730
|
+
);
|
|
8731
|
+
}
|
|
8732
|
+
if (result.code !== 0) {
|
|
8733
|
+
throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
|
|
8734
|
+
}
|
|
8735
|
+
let payload;
|
|
8736
|
+
try {
|
|
8737
|
+
payload = JSON.parse(result.stdout);
|
|
8738
|
+
} catch (error2) {
|
|
8739
|
+
log3(
|
|
8740
|
+
`RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
8741
|
+
"warn"
|
|
8742
|
+
);
|
|
8743
|
+
return false;
|
|
8744
|
+
}
|
|
8745
|
+
if (!isEnvironmentObject(payload)) {
|
|
8746
|
+
log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
|
|
8747
|
+
return false;
|
|
8748
|
+
}
|
|
8749
|
+
let populated = 0;
|
|
8750
|
+
let skipped = 0;
|
|
8751
|
+
let githubTokenPopulated = false;
|
|
8752
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
8753
|
+
if (typeof value !== "string" || value.length === 0) continue;
|
|
8754
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
|
|
8755
|
+
log3(
|
|
8756
|
+
`RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
|
|
8757
|
+
"warn"
|
|
8758
|
+
);
|
|
8759
|
+
skipped += 1;
|
|
8760
|
+
continue;
|
|
8761
|
+
}
|
|
8762
|
+
env[key] = value;
|
|
8763
|
+
populated += 1;
|
|
8764
|
+
if (key === "GH_TOKEN") githubTokenPopulated = true;
|
|
8765
|
+
}
|
|
8766
|
+
if (populated === 0) {
|
|
8767
|
+
log3(
|
|
8768
|
+
"RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
|
|
8769
|
+
"warn"
|
|
8770
|
+
);
|
|
8771
|
+
} else {
|
|
8772
|
+
log3(
|
|
8773
|
+
`RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
|
|
8774
|
+
);
|
|
8775
|
+
}
|
|
8776
|
+
return githubTokenPopulated;
|
|
8777
|
+
}
|
|
8778
|
+
function restoreFailure(operation, result, log3) {
|
|
8779
|
+
const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
|
|
8780
|
+
log3(message, "error");
|
|
8781
|
+
return new Error(message);
|
|
8782
|
+
}
|
|
8783
|
+
async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
|
|
8784
|
+
const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
|
|
8785
|
+
if (result.timedOut) {
|
|
8786
|
+
log3(
|
|
8787
|
+
`CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
8788
|
+
"warn"
|
|
8789
|
+
);
|
|
8790
|
+
return result;
|
|
8791
|
+
}
|
|
8792
|
+
if (result.code !== 0) throw restoreFailure(operation, result, log3);
|
|
8793
|
+
return result;
|
|
8794
|
+
}
|
|
8795
|
+
async function restoreCredentialStores({
|
|
8796
|
+
env,
|
|
8797
|
+
log: log3,
|
|
8798
|
+
synchroniserRunner = runSynchroniser
|
|
8799
|
+
}) {
|
|
8800
|
+
await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
|
|
8801
|
+
await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
|
|
8802
|
+
const result = await synchroniserRunner(["model-auth-ready"], {
|
|
8803
|
+
timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
|
|
8804
|
+
});
|
|
8805
|
+
if (result.timedOut) {
|
|
8806
|
+
log3(
|
|
8807
|
+
`CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
8808
|
+
"warn"
|
|
8809
|
+
);
|
|
8810
|
+
return;
|
|
8811
|
+
}
|
|
8812
|
+
switch (result.code) {
|
|
8813
|
+
case 0:
|
|
8814
|
+
return;
|
|
8815
|
+
case 10:
|
|
8816
|
+
log3(
|
|
8817
|
+
`no model credentials under s3://${env.LITESTREAM_BUCKET ?? ""}/${env.LITESTREAM_PREFIX ?? ""}/ (neither claude/credentials.json nor opencode/auth.json yielded valid JSON) and neither ANTHROPIC_API_KEY nor OPENAI_API_KEY is set. This VM boots and connects; a turn that needs a model provider fails until one is connected. See 'Seeding a credential store' in infrastructure/evident-runner/MICROVM.md.`,
|
|
8818
|
+
"warn"
|
|
8819
|
+
);
|
|
8820
|
+
return;
|
|
8821
|
+
default:
|
|
8822
|
+
log3("could not determine whether this VM has model credentials", "warn");
|
|
8823
|
+
}
|
|
8824
|
+
}
|
|
8825
|
+
var GIT_CREDENTIAL_HELPER_CONTENT = [
|
|
8826
|
+
"#!/usr/bin/env bash",
|
|
8827
|
+
'[ "$1" = get ] || exit 0',
|
|
8828
|
+
"echo username=x-access-token",
|
|
8829
|
+
'echo "password=${GH_TOKEN}"',
|
|
8830
|
+
""
|
|
8831
|
+
].join("\n");
|
|
8832
|
+
async function probeGitHubAccess({ env, log: log3 }) {
|
|
8833
|
+
const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
|
|
8834
|
+
env,
|
|
8835
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8836
|
+
});
|
|
8837
|
+
if (auth.timedOut) {
|
|
8838
|
+
log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
|
|
8839
|
+
return;
|
|
8840
|
+
}
|
|
8841
|
+
if (auth.code !== 0) {
|
|
8842
|
+
log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
|
|
8843
|
+
return;
|
|
8844
|
+
}
|
|
8845
|
+
log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
|
|
8846
|
+
const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
|
|
8847
|
+
env,
|
|
8848
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8849
|
+
});
|
|
8850
|
+
if (remote.code !== 0 || remote.timedOut) return;
|
|
8851
|
+
const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
|
|
8852
|
+
if (!repo) return;
|
|
8853
|
+
const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
|
|
8854
|
+
env,
|
|
8855
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8856
|
+
});
|
|
8857
|
+
if (repository.timedOut) {
|
|
8858
|
+
log3(
|
|
8859
|
+
`GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
|
|
8860
|
+
"warn"
|
|
8861
|
+
);
|
|
8862
|
+
} else if (repository.code !== 0) {
|
|
8863
|
+
log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
|
|
8864
|
+
}
|
|
8865
|
+
}
|
|
8866
|
+
async function configureGitHubAccess({ env, log: log3 }) {
|
|
8867
|
+
if (!env.GH_TOKEN) {
|
|
8868
|
+
log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
|
|
8869
|
+
return;
|
|
8870
|
+
}
|
|
8871
|
+
try {
|
|
8872
|
+
env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
|
|
8873
|
+
writeFileSync3(GIT_CONFIG_GLOBAL, "");
|
|
8874
|
+
writeFileSync3(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
|
|
8875
|
+
chmodSync2(GIT_CREDENTIAL_HELPER, 448);
|
|
8876
|
+
const config = [
|
|
8877
|
+
["user.name", env.GIT_USER_NAME ?? "evident-bot"],
|
|
8878
|
+
["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
|
|
8879
|
+
["init.defaultBranch", "main"],
|
|
8880
|
+
["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
|
|
8881
|
+
];
|
|
8882
|
+
for (const [key, value] of config) {
|
|
8883
|
+
const result = await runCommand2("git", ["config", "--global", key, value], {
|
|
8884
|
+
env,
|
|
8885
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8886
|
+
});
|
|
8887
|
+
if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
|
|
8888
|
+
}
|
|
8889
|
+
} catch (error2) {
|
|
8890
|
+
log3(
|
|
8891
|
+
`GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
8892
|
+
"warn"
|
|
8893
|
+
);
|
|
8894
|
+
return;
|
|
8895
|
+
}
|
|
8896
|
+
void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
|
|
8897
|
+
log3(
|
|
8898
|
+
`GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
8899
|
+
"warn"
|
|
8900
|
+
);
|
|
8901
|
+
});
|
|
8902
|
+
}
|
|
8903
|
+
|
|
8904
|
+
// 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 join7, resolve as resolve3 } from "path";
|
|
8908
|
+
function isFile(filePath) {
|
|
8909
|
+
return existsSync2(filePath) && statSync5(filePath).isFile();
|
|
8910
|
+
}
|
|
8911
|
+
function applyRunnerOpenCodeConfig({
|
|
8912
|
+
overlayPath,
|
|
8913
|
+
cwd = process.cwd(),
|
|
8914
|
+
log: log3
|
|
8915
|
+
}) {
|
|
8916
|
+
if (!overlayPath) {
|
|
8917
|
+
log3("runner OpenCode config is not configured; using the baked project config", "debug");
|
|
8918
|
+
return;
|
|
8919
|
+
}
|
|
8920
|
+
const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
|
|
8921
|
+
const target = isFile(join7(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
|
|
8922
|
+
if (!isFile(source)) {
|
|
8923
|
+
log3(
|
|
8924
|
+
`RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
|
|
8925
|
+
"error"
|
|
8926
|
+
);
|
|
8927
|
+
return;
|
|
8928
|
+
}
|
|
8929
|
+
copyFileSync(source, join7(cwd, target));
|
|
8930
|
+
try {
|
|
8931
|
+
execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
8932
|
+
stdio: "ignore"
|
|
8933
|
+
});
|
|
8934
|
+
} catch (error2) {
|
|
8935
|
+
const detail = error2 instanceof Error ? error2.message : String(error2);
|
|
8936
|
+
log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
|
|
8937
|
+
}
|
|
8938
|
+
log3(`Applied runner OpenCode config ${source} to ${join7(cwd, target)}`);
|
|
8939
|
+
}
|
|
8940
|
+
|
|
7892
8941
|
// src/commands/run.ts
|
|
7893
8942
|
var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
7894
8943
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
7895
8944
|
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
7896
8945
|
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
7897
8946
|
var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
8947
|
+
var CHILD_STOP_TIMEOUT_MS = 1e4;
|
|
7898
8948
|
function resolveLogLevel(options) {
|
|
7899
8949
|
const accepted = Object.keys(LOG_LEVELS);
|
|
7900
8950
|
const validate = (value, source) => {
|
|
@@ -7925,11 +8975,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
7925
8975
|
if (trimmed === "") {
|
|
7926
8976
|
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
7927
8977
|
}
|
|
7928
|
-
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ?
|
|
7929
|
-
if (!
|
|
8978
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join8(homeDir, trimmed.slice(2)) : trimmed;
|
|
8979
|
+
if (!isAbsolute3(expanded)) {
|
|
7930
8980
|
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
7931
8981
|
}
|
|
7932
|
-
const normalized =
|
|
8982
|
+
const normalized = resolvePath2(expanded);
|
|
7933
8983
|
if (parse(normalized).root === normalized) {
|
|
7934
8984
|
throw new Error(
|
|
7935
8985
|
`--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
|
|
@@ -8045,7 +9095,7 @@ function logActivity(state, entry) {
|
|
|
8045
9095
|
}
|
|
8046
9096
|
function reportSessionDbRecovery(state) {
|
|
8047
9097
|
try {
|
|
8048
|
-
const report = drainSessionDbRecoveryReport({ homeDir:
|
|
9098
|
+
const report = drainSessionDbRecoveryReport({ homeDir: homedir5(), env: process.env });
|
|
8049
9099
|
for (const record of report.records) {
|
|
8050
9100
|
const activity = buildSessionDbRecoveryActivity(record);
|
|
8051
9101
|
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
@@ -8063,6 +9113,16 @@ function reportSessionDbRecovery(state) {
|
|
|
8063
9113
|
);
|
|
8064
9114
|
}
|
|
8065
9115
|
}
|
|
9116
|
+
function reportSessionDbRecoveryRecord(state, record) {
|
|
9117
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
9118
|
+
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
9119
|
+
logActivity(state, {
|
|
9120
|
+
type: activity.level === "error" ? "error" : "info",
|
|
9121
|
+
level: activity.level,
|
|
9122
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
9123
|
+
metadata: activity.metadata
|
|
9124
|
+
});
|
|
9125
|
+
}
|
|
8066
9126
|
function displayStatus(state) {
|
|
8067
9127
|
if (!state.interactive) return;
|
|
8068
9128
|
const attempt = state.connection?.reconnectAttempt ?? 0;
|
|
@@ -8235,7 +9295,7 @@ async function driveChannels(state, driver) {
|
|
|
8235
9295
|
}
|
|
8236
9296
|
}
|
|
8237
9297
|
}
|
|
8238
|
-
await new Promise((
|
|
9298
|
+
await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
|
|
8239
9299
|
const cycleMs = performance.now() - cycleStartedAtMs;
|
|
8240
9300
|
if (idleThisCycle) idleMs += cycleMs;
|
|
8241
9301
|
if (unreachableThisCycle) unreachableMs += cycleMs;
|
|
@@ -8258,7 +9318,7 @@ async function driveChannels(state, driver) {
|
|
|
8258
9318
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
8259
9319
|
var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
|
|
8260
9320
|
function sessionDbPath() {
|
|
8261
|
-
return
|
|
9321
|
+
return join8(homedir5(), ".local", "share", "opencode", "opencode.db");
|
|
8262
9322
|
}
|
|
8263
9323
|
async function runSweep(state, driver, config) {
|
|
8264
9324
|
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
@@ -8341,7 +9401,7 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
8341
9401
|
for (const warning2 of config.warnings) {
|
|
8342
9402
|
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
8343
9403
|
}
|
|
8344
|
-
const dbBytes = statSessionDbBytes(
|
|
9404
|
+
const dbBytes = statSessionDbBytes(homedir5());
|
|
8345
9405
|
void (async () => {
|
|
8346
9406
|
const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
|
|
8347
9407
|
const sizeWarning = buildSessionStoreSizeWarning({
|
|
@@ -8500,7 +9560,17 @@ function scheduleClaudeUsageReporting(state, options) {
|
|
|
8500
9560
|
setTimer: (timer) => {
|
|
8501
9561
|
state.claudeUsageTimer = timer;
|
|
8502
9562
|
},
|
|
8503
|
-
fetchUsage:
|
|
9563
|
+
fetchUsage: async () => {
|
|
9564
|
+
const usage = await getClaudeUsage();
|
|
9565
|
+
if (usage.ownerLookupError) {
|
|
9566
|
+
logActivity(state, {
|
|
9567
|
+
type: "info",
|
|
9568
|
+
level: "debug",
|
|
9569
|
+
message: `Claude usage owner lookup failed: ${usage.ownerLookupError}`
|
|
9570
|
+
});
|
|
9571
|
+
}
|
|
9572
|
+
return usage;
|
|
9573
|
+
},
|
|
8504
9574
|
report: (usage) => reportClaudeUsage(state.agentId, state.authHeader, usage),
|
|
8505
9575
|
isLocalCredentialProblem,
|
|
8506
9576
|
forcedOnHint: "run `claude` to sign in",
|
|
@@ -8532,7 +9602,7 @@ function scheduleResourceUsageReporting(state, options) {
|
|
|
8532
9602
|
});
|
|
8533
9603
|
return;
|
|
8534
9604
|
}
|
|
8535
|
-
const collect = createResourceUsageCollector(
|
|
9605
|
+
const collect = createResourceUsageCollector(homedir5());
|
|
8536
9606
|
let consecutiveFailures = 0;
|
|
8537
9607
|
const tick = async () => {
|
|
8538
9608
|
try {
|
|
@@ -8672,15 +9742,31 @@ async function cleanup(state, opts = {}) {
|
|
|
8672
9742
|
}
|
|
8673
9743
|
if (state.opencodeProcess) {
|
|
8674
9744
|
const opencodeProcess = state.opencodeProcess;
|
|
8675
|
-
|
|
9745
|
+
const result = await timeShutdownPhase(
|
|
9746
|
+
state,
|
|
9747
|
+
durations,
|
|
9748
|
+
"opencode_stop",
|
|
9749
|
+
() => stopOpenCodeAndWait(opencodeProcess, CHILD_STOP_TIMEOUT_MS)
|
|
9750
|
+
);
|
|
8676
9751
|
if (state.interactive) {
|
|
8677
|
-
logActivity(state, { type: "info", message:
|
|
9752
|
+
logActivity(state, { type: "info", message: `Stopped OpenCode process (${result.outcome})` });
|
|
8678
9753
|
displayStatus(state);
|
|
8679
9754
|
} else {
|
|
8680
|
-
log2(state,
|
|
9755
|
+
log2(state, `Stopped OpenCode process (${result.outcome})`);
|
|
8681
9756
|
}
|
|
8682
9757
|
state.opencodeProcess = null;
|
|
8683
9758
|
}
|
|
9759
|
+
if (state.litestreamProcess) {
|
|
9760
|
+
const litestreamProcess = state.litestreamProcess;
|
|
9761
|
+
const result = await timeShutdownPhase(
|
|
9762
|
+
state,
|
|
9763
|
+
durations,
|
|
9764
|
+
"litestream_stop",
|
|
9765
|
+
() => stopSessionDbReplication(litestreamProcess, CHILD_STOP_TIMEOUT_MS)
|
|
9766
|
+
);
|
|
9767
|
+
log2(state, `Stopped litestream replication (${result.outcome})`);
|
|
9768
|
+
state.litestreamProcess = null;
|
|
9769
|
+
}
|
|
8684
9770
|
return durations;
|
|
8685
9771
|
}
|
|
8686
9772
|
async function run(options) {
|
|
@@ -8689,7 +9775,12 @@ async function run(options) {
|
|
|
8689
9775
|
let fileSyncDirectories;
|
|
8690
9776
|
try {
|
|
8691
9777
|
logLevel = resolveLogLevel(options);
|
|
8692
|
-
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo,
|
|
9778
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir5());
|
|
9779
|
+
if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
|
|
9780
|
+
throw new Error(
|
|
9781
|
+
"--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
|
|
9782
|
+
);
|
|
9783
|
+
}
|
|
8693
9784
|
} catch (error2) {
|
|
8694
9785
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
8695
9786
|
if (options.json) {
|
|
@@ -8714,6 +9805,7 @@ async function run(options) {
|
|
|
8714
9805
|
opencodeConnected: false,
|
|
8715
9806
|
opencodeVersion: null,
|
|
8716
9807
|
opencodeProcess: null,
|
|
9808
|
+
litestreamProcess: null,
|
|
8717
9809
|
connection: null,
|
|
8718
9810
|
channelDriver: null,
|
|
8719
9811
|
running: true,
|
|
@@ -8780,8 +9872,8 @@ async function run(options) {
|
|
|
8780
9872
|
return true;
|
|
8781
9873
|
}
|
|
8782
9874
|
);
|
|
8783
|
-
const timedOut = new Promise((
|
|
8784
|
-
timer = setTimeout(() =>
|
|
9875
|
+
const timedOut = new Promise((resolve4) => {
|
|
9876
|
+
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
8785
9877
|
});
|
|
8786
9878
|
if (!await Promise.race([flushed, timedOut])) {
|
|
8787
9879
|
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
@@ -8921,7 +10013,67 @@ async function run(options) {
|
|
|
8921
10013
|
} else {
|
|
8922
10014
|
log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
|
|
8923
10015
|
}
|
|
10016
|
+
if (options.restoreRunnerCredentials) {
|
|
10017
|
+
log2(state, "Restoring runner credentials before starting OpenCode");
|
|
10018
|
+
const credentialContext = {
|
|
10019
|
+
env: process.env,
|
|
10020
|
+
log: (message, level = "info") => {
|
|
10021
|
+
if (level === "error") {
|
|
10022
|
+
logActivity(state, { type: "error", error: message });
|
|
10023
|
+
} else {
|
|
10024
|
+
logActivity(state, { type: "info", level, message });
|
|
10025
|
+
}
|
|
10026
|
+
}
|
|
10027
|
+
};
|
|
10028
|
+
const githubTokenPopulated = await installRunnerSecret(credentialContext);
|
|
10029
|
+
await restoreCredentialStores(credentialContext);
|
|
10030
|
+
if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
|
|
10031
|
+
}
|
|
10032
|
+
let sessionDbVerifyFatal = false;
|
|
10033
|
+
if (!options.restoreSessionDb) {
|
|
10034
|
+
log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
|
|
10035
|
+
} else {
|
|
10036
|
+
const health = await checkOpenCodeHealth(state.port);
|
|
10037
|
+
if (health.healthy) {
|
|
10038
|
+
log2(
|
|
10039
|
+
state,
|
|
10040
|
+
"Skipping session-DB restore: OpenCode is already serving this database",
|
|
10041
|
+
"debug"
|
|
10042
|
+
);
|
|
10043
|
+
} else {
|
|
10044
|
+
const result = await restoreAndVerifySessionDb({
|
|
10045
|
+
dbPath: sessionDbPath(),
|
|
10046
|
+
litestreamConfig: options.litestreamConfig,
|
|
10047
|
+
noReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10048
|
+
env: process.env,
|
|
10049
|
+
log: (message, level = "info") => {
|
|
10050
|
+
if (level === "error") {
|
|
10051
|
+
logActivity(state, { type: "error", error: message });
|
|
10052
|
+
} else {
|
|
10053
|
+
logActivity(state, { type: "info", level, message });
|
|
10054
|
+
}
|
|
10055
|
+
},
|
|
10056
|
+
reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
|
|
10057
|
+
});
|
|
10058
|
+
sessionDbVerifyFatal = result.verifyFatal;
|
|
10059
|
+
}
|
|
10060
|
+
}
|
|
8924
10061
|
reportSessionDbRecovery(state);
|
|
10062
|
+
if (sessionDbVerifyFatal) {
|
|
10063
|
+
throw new Error(
|
|
10064
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
|
|
10065
|
+
);
|
|
10066
|
+
}
|
|
10067
|
+
applyRunnerOpenCodeConfig({
|
|
10068
|
+
overlayPath: options.opencodeConfigOverlay,
|
|
10069
|
+
log: (message, level = "info") => {
|
|
10070
|
+
if (level === "error") {
|
|
10071
|
+
logActivity(state, { type: "error", error: message });
|
|
10072
|
+
} else {
|
|
10073
|
+
logActivity(state, { type: "info", level, message });
|
|
10074
|
+
}
|
|
10075
|
+
}
|
|
10076
|
+
});
|
|
8925
10077
|
const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
|
|
8926
10078
|
for (const warning2 of opencodeStartTimeoutWarnings) {
|
|
8927
10079
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
@@ -8937,11 +10089,24 @@ async function run(options) {
|
|
|
8937
10089
|
interactive: state.interactive,
|
|
8938
10090
|
agentId: state.agentId,
|
|
8939
10091
|
log: (message) => log2(state, message),
|
|
8940
|
-
startTimeoutMs: opencodeStartTimeoutMs
|
|
10092
|
+
startTimeoutMs: opencodeStartTimeoutMs,
|
|
10093
|
+
inheritStdio: Boolean(options.opencodePidFile)
|
|
8941
10094
|
});
|
|
8942
10095
|
state.port = oc.port;
|
|
8943
|
-
state.opencodeProcess = oc.process;
|
|
10096
|
+
state.opencodeProcess = options.opencodePidFile ? null : oc.process;
|
|
8944
10097
|
state.opencodeVersion = oc.version;
|
|
10098
|
+
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
10099
|
+
try {
|
|
10100
|
+
writeFileSync4(options.opencodePidFile, `${oc.process.pid}
|
|
10101
|
+
`, { mode: 384 });
|
|
10102
|
+
chmodSync3(options.opencodePidFile, 384);
|
|
10103
|
+
} catch (error2) {
|
|
10104
|
+
logActivity(state, {
|
|
10105
|
+
type: "error",
|
|
10106
|
+
error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10107
|
+
});
|
|
10108
|
+
}
|
|
10109
|
+
}
|
|
8945
10110
|
state.opencodeConnected = oc.notReadyReason === null;
|
|
8946
10111
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
8947
10112
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
@@ -8978,6 +10143,108 @@ async function run(options) {
|
|
|
8978
10143
|
ocSpinner?.fail(error2.message);
|
|
8979
10144
|
throw error2;
|
|
8980
10145
|
}
|
|
10146
|
+
if (options.litestreamPidFile) {
|
|
10147
|
+
if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
|
|
10148
|
+
log2(
|
|
10149
|
+
state,
|
|
10150
|
+
`Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
|
|
10151
|
+
);
|
|
10152
|
+
} else if (!options.litestreamConfig) {
|
|
10153
|
+
logActivity(state, {
|
|
10154
|
+
type: "info",
|
|
10155
|
+
level: "warn",
|
|
10156
|
+
message: "Skipping Litestream replication because no configuration file was provided"
|
|
10157
|
+
});
|
|
10158
|
+
} else {
|
|
10159
|
+
let existingPid;
|
|
10160
|
+
if (existsSync3(options.litestreamPidFile)) {
|
|
10161
|
+
try {
|
|
10162
|
+
const rawPid = readFileSync5(options.litestreamPidFile, "utf8").trim();
|
|
10163
|
+
const parsedPid = Number(rawPid);
|
|
10164
|
+
if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
|
|
10165
|
+
existingPid = parsedPid;
|
|
10166
|
+
}
|
|
10167
|
+
} catch (error2) {
|
|
10168
|
+
logActivity(state, {
|
|
10169
|
+
type: "info",
|
|
10170
|
+
level: "warn",
|
|
10171
|
+
message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10172
|
+
});
|
|
10173
|
+
}
|
|
10174
|
+
}
|
|
10175
|
+
if (existingPid !== void 0 && isProcessAlive(existingPid)) {
|
|
10176
|
+
log2(state, `Litestream replication is already running with pid ${existingPid}`);
|
|
10177
|
+
} else {
|
|
10178
|
+
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
10179
|
+
state.litestreamProcess = null;
|
|
10180
|
+
let failureHandled = false;
|
|
10181
|
+
const reportImageOwnedReplicationFailure = (message) => {
|
|
10182
|
+
if (failureHandled || state.shuttingDown || !state.running) return;
|
|
10183
|
+
failureHandled = true;
|
|
10184
|
+
logActivity(state, { type: "error", error: message });
|
|
10185
|
+
if (state.interactive) displayStatus(state);
|
|
10186
|
+
};
|
|
10187
|
+
litestreamProcess.on("exit", (code, signal) => {
|
|
10188
|
+
reportImageOwnedReplicationFailure(
|
|
10189
|
+
`Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
|
|
10190
|
+
);
|
|
10191
|
+
});
|
|
10192
|
+
litestreamProcess.on("error", (error2) => {
|
|
10193
|
+
reportImageOwnedReplicationFailure(
|
|
10194
|
+
`Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10195
|
+
);
|
|
10196
|
+
});
|
|
10197
|
+
try {
|
|
10198
|
+
if (litestreamProcess.pid !== void 0) {
|
|
10199
|
+
writeFileSync4(options.litestreamPidFile, `${litestreamProcess.pid}
|
|
10200
|
+
`, {
|
|
10201
|
+
mode: 384
|
|
10202
|
+
});
|
|
10203
|
+
chmodSync3(options.litestreamPidFile, 384);
|
|
10204
|
+
}
|
|
10205
|
+
} catch (error2) {
|
|
10206
|
+
logActivity(state, {
|
|
10207
|
+
type: "error",
|
|
10208
|
+
error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10209
|
+
});
|
|
10210
|
+
}
|
|
10211
|
+
log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
|
|
10212
|
+
}
|
|
10213
|
+
}
|
|
10214
|
+
} else if (options.litestreamConfig) {
|
|
10215
|
+
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
10216
|
+
state.litestreamProcess = litestreamProcess;
|
|
10217
|
+
let failureHandled = false;
|
|
10218
|
+
const failRunForReplication = (message) => {
|
|
10219
|
+
if (failureHandled || state.shuttingDown || !state.running) return;
|
|
10220
|
+
failureHandled = true;
|
|
10221
|
+
state.shuttingDown = true;
|
|
10222
|
+
logActivity(state, { type: "error", error: message });
|
|
10223
|
+
if (state.interactive) displayStatus(state);
|
|
10224
|
+
void (async () => {
|
|
10225
|
+
try {
|
|
10226
|
+
await cleanup(state);
|
|
10227
|
+
await shutdownTelemetry();
|
|
10228
|
+
} catch (error2) {
|
|
10229
|
+
console.error(
|
|
10230
|
+
`[run] replication failure cleanup failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10231
|
+
);
|
|
10232
|
+
}
|
|
10233
|
+
process.exit(1);
|
|
10234
|
+
})();
|
|
10235
|
+
};
|
|
10236
|
+
litestreamProcess.on("exit", (code, signal) => {
|
|
10237
|
+
failRunForReplication(
|
|
10238
|
+
`Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
|
|
10239
|
+
);
|
|
10240
|
+
});
|
|
10241
|
+
litestreamProcess.on("error", (error2) => {
|
|
10242
|
+
failRunForReplication(
|
|
10243
|
+
`Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10244
|
+
);
|
|
10245
|
+
});
|
|
10246
|
+
log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
|
|
10247
|
+
}
|
|
8981
10248
|
const tunnelSpinner = interactive && !state.json ? ora3("Connecting tunnel...").start() : null;
|
|
8982
10249
|
const channelDriver = new ChannelDriver({
|
|
8983
10250
|
agentId: state.agentId,
|
|
@@ -8989,7 +10256,7 @@ async function run(options) {
|
|
|
8989
10256
|
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
8990
10257
|
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
8991
10258
|
fileSyncDirectories,
|
|
8992
|
-
homeDir:
|
|
10259
|
+
homeDir: homedir5(),
|
|
8993
10260
|
maxActiveSessions,
|
|
8994
10261
|
log: (entry) => (
|
|
8995
10262
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
@@ -9238,6 +10505,27 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9238
10505
|
).option(
|
|
9239
10506
|
"--tunnel-ready-file <path>",
|
|
9240
10507
|
"Path to write once the tunnel is connected (opt-in; unused unless an operator/image sets it)"
|
|
10508
|
+
).option(
|
|
10509
|
+
"--litestream-config <path>",
|
|
10510
|
+
"Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
|
|
10511
|
+
).option(
|
|
10512
|
+
"--opencode-pid-file <path>",
|
|
10513
|
+
"Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
10514
|
+
).option(
|
|
10515
|
+
"--litestream-pid-file <path>",
|
|
10516
|
+
"Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
10517
|
+
).option(
|
|
10518
|
+
"--session-db-no-replicate-marker <path>",
|
|
10519
|
+
"Read this marker as a gate before starting Litestream; the session-DB restore writes it when this boot's database is not safe to replicate."
|
|
10520
|
+
).option(
|
|
10521
|
+
"--restore-session-db",
|
|
10522
|
+
"Restore and verify the OpenCode session database before starting OpenCode; requires --litestream-config for the replica and --session-db-no-replicate-marker for give-up records."
|
|
10523
|
+
).option(
|
|
10524
|
+
"--restore-runner-credentials",
|
|
10525
|
+
"Restore the hosted runner secret and persisted credential stores before starting OpenCode."
|
|
10526
|
+
).option(
|
|
10527
|
+
"--opencode-config-overlay <path>",
|
|
10528
|
+
"Apply this runner-provided OpenCode config before starting OpenCode."
|
|
9241
10529
|
).action(
|
|
9242
10530
|
(options) => {
|
|
9243
10531
|
run({
|
|
@@ -9269,7 +10557,14 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9269
10557
|
// Raw values — expansion/validation is single-sourced in run.ts's
|
|
9270
10558
|
// resolveFileSyncDirectories.
|
|
9271
10559
|
enableFileSyncTo: options.enableFileSyncTo,
|
|
9272
|
-
tunnelReadyFile: options.tunnelReadyFile
|
|
10560
|
+
tunnelReadyFile: options.tunnelReadyFile,
|
|
10561
|
+
litestreamConfig: options.litestreamConfig,
|
|
10562
|
+
opencodePidFile: options.opencodePidFile,
|
|
10563
|
+
litestreamPidFile: options.litestreamPidFile,
|
|
10564
|
+
sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10565
|
+
restoreSessionDb: options.restoreSessionDb,
|
|
10566
|
+
restoreRunnerCredentials: options.restoreRunnerCredentials,
|
|
10567
|
+
opencodeConfigOverlay: options.opencodeConfigOverlay
|
|
9273
10568
|
});
|
|
9274
10569
|
}
|
|
9275
10570
|
);
|