@evident-ai/cli 3.4.1-dev.7802262 → 3.4.1-dev.86c712d
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 +27 -0
- package/dist/index.js +2354 -442
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
-
import { createRequire } from "module";
|
|
4
|
+
import { createRequire as createRequire2 } from "module";
|
|
5
5
|
import { Command } from "commander";
|
|
6
6
|
|
|
7
7
|
// src/commands/login.ts
|
|
@@ -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
|
});
|
|
@@ -754,6 +763,13 @@ function toReportedOpenAiWindow(window) {
|
|
|
754
763
|
resets_at: window.resetsAt
|
|
755
764
|
};
|
|
756
765
|
}
|
|
766
|
+
function toReportedOpenAiSubscription(snapshot) {
|
|
767
|
+
if (!snapshot.subscription) return null;
|
|
768
|
+
return {
|
|
769
|
+
owner_email: snapshot.subscription.ownerEmail,
|
|
770
|
+
plan_type: snapshot.subscription.planType
|
|
771
|
+
};
|
|
772
|
+
}
|
|
757
773
|
async function reportOpenAiUsage(agentId, authHeader, snapshot) {
|
|
758
774
|
try {
|
|
759
775
|
const apiUrl = getApiUrlConfig();
|
|
@@ -764,7 +780,8 @@ async function reportOpenAiUsage(agentId, authHeader, snapshot) {
|
|
|
764
780
|
primary: toReportedOpenAiWindow(snapshot.primary),
|
|
765
781
|
secondary: toReportedOpenAiWindow(snapshot.secondary),
|
|
766
782
|
has_credits: snapshot.hasCredits,
|
|
767
|
-
credits_unlimited: snapshot.creditsUnlimited
|
|
783
|
+
credits_unlimited: snapshot.creditsUnlimited,
|
|
784
|
+
subscription: toReportedOpenAiSubscription(snapshot)
|
|
768
785
|
}),
|
|
769
786
|
signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
|
|
770
787
|
});
|
|
@@ -788,6 +805,7 @@ async function reportResourceUsage(agentId, authHeader, usage) {
|
|
|
788
805
|
headers: { Authorization: authHeader, "Content-Type": "application/json" },
|
|
789
806
|
body: JSON.stringify({
|
|
790
807
|
cpu_percent: usage.cpuPercent,
|
|
808
|
+
cpu_peak_percent: usage.cpuPeakPercent,
|
|
791
809
|
cpu_count: usage.cpuCount,
|
|
792
810
|
memory_total_bytes: usage.memoryTotalBytes,
|
|
793
811
|
memory_available_bytes: usage.memoryAvailableBytes,
|
|
@@ -996,7 +1014,10 @@ import { readFileSync } from "fs";
|
|
|
996
1014
|
import { homedir } from "os";
|
|
997
1015
|
import { join } from "path";
|
|
998
1016
|
var CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
|
|
1017
|
+
var CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
|
|
1018
|
+
var CLAUDE_PROFILE_TIMEOUT_MS = 2e3;
|
|
999
1019
|
var KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
1020
|
+
var cachedOwner = null;
|
|
1000
1021
|
var CLAUDE_CREDENTIALS_SEGMENTS = [".claude", ".credentials.json"];
|
|
1001
1022
|
function parseClaudeCliCredentials(raw) {
|
|
1002
1023
|
let parsed;
|
|
@@ -1070,6 +1091,47 @@ function toWindow(value) {
|
|
|
1070
1091
|
}
|
|
1071
1092
|
return { utilization: window.utilization, resetsAt };
|
|
1072
1093
|
}
|
|
1094
|
+
function ownerLookupFailure(error2) {
|
|
1095
|
+
const name = error2?.name;
|
|
1096
|
+
return name === "TimeoutError" || name === "AbortError" ? "timed out" : "request failed";
|
|
1097
|
+
}
|
|
1098
|
+
async function getClaudeUsageOwner(accessToken) {
|
|
1099
|
+
if (cachedOwner?.accessToken === accessToken) {
|
|
1100
|
+
return { owner: cachedOwner.owner, ownerLookupError: null };
|
|
1101
|
+
}
|
|
1102
|
+
try {
|
|
1103
|
+
const response = await fetch(CLAUDE_PROFILE_URL, {
|
|
1104
|
+
headers: {
|
|
1105
|
+
Authorization: `Bearer ${accessToken}`,
|
|
1106
|
+
"Content-Type": "application/json",
|
|
1107
|
+
"anthropic-version": "2023-06-01"
|
|
1108
|
+
},
|
|
1109
|
+
signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
|
|
1110
|
+
});
|
|
1111
|
+
if (!response.ok) {
|
|
1112
|
+
return { owner: null, ownerLookupError: `HTTP ${response.status}` };
|
|
1113
|
+
}
|
|
1114
|
+
let body;
|
|
1115
|
+
try {
|
|
1116
|
+
body = await response.json();
|
|
1117
|
+
} catch (error2) {
|
|
1118
|
+
return { owner: null, ownerLookupError: "malformed response" };
|
|
1119
|
+
}
|
|
1120
|
+
const profile = body;
|
|
1121
|
+
if (typeof profile.account?.email !== "string" || !profile.account.email.trim()) {
|
|
1122
|
+
return { owner: null, ownerLookupError: "malformed response" };
|
|
1123
|
+
}
|
|
1124
|
+
const owner = {
|
|
1125
|
+
email: profile.account.email,
|
|
1126
|
+
organizationName: typeof profile.organization?.name === "string" && profile.organization.name.trim() ? profile.organization.name.trim() : null,
|
|
1127
|
+
rateLimitTier: typeof profile.organization?.rate_limit_tier === "string" && profile.organization.rate_limit_tier.trim() ? profile.organization.rate_limit_tier.trim() : null
|
|
1128
|
+
};
|
|
1129
|
+
cachedOwner = { accessToken, owner };
|
|
1130
|
+
return { owner, ownerLookupError: null };
|
|
1131
|
+
} catch (error2) {
|
|
1132
|
+
return { owner: null, ownerLookupError: ownerLookupFailure(error2) };
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1073
1135
|
async function getClaudeUsage() {
|
|
1074
1136
|
const credentials2 = readClaudeCliCredentials();
|
|
1075
1137
|
if (!credentials2) {
|
|
@@ -1089,15 +1151,19 @@ async function getClaudeUsage() {
|
|
|
1089
1151
|
Authorization: `Bearer ${credentials2.accessToken}`,
|
|
1090
1152
|
"Content-Type": "application/json",
|
|
1091
1153
|
"anthropic-version": "2023-06-01"
|
|
1092
|
-
}
|
|
1154
|
+
},
|
|
1155
|
+
signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
|
|
1093
1156
|
});
|
|
1094
1157
|
if (!res.ok) {
|
|
1095
1158
|
throw new ClaudeUsageError(`Claude usage request failed: HTTP ${res.status}`, "request_failed");
|
|
1096
1159
|
}
|
|
1097
1160
|
const body = await res.json();
|
|
1161
|
+
const { owner, ownerLookupError } = await getClaudeUsageOwner(credentials2.accessToken);
|
|
1098
1162
|
return {
|
|
1099
1163
|
fiveHour: toWindow(body.five_hour),
|
|
1100
|
-
sevenDay: toWindow(body.seven_day)
|
|
1164
|
+
sevenDay: toWindow(body.seven_day),
|
|
1165
|
+
owner,
|
|
1166
|
+
ownerLookupError
|
|
1101
1167
|
};
|
|
1102
1168
|
}
|
|
1103
1169
|
|
|
@@ -1126,8 +1192,9 @@ async function claudeUsage() {
|
|
|
1126
1192
|
}
|
|
1127
1193
|
|
|
1128
1194
|
// src/commands/run.ts
|
|
1129
|
-
import {
|
|
1130
|
-
import {
|
|
1195
|
+
import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync6 } from "fs";
|
|
1196
|
+
import { homedir as homedir5 } from "os";
|
|
1197
|
+
import { isAbsolute as isAbsolute3, join as join9, parse, resolve as resolvePath2 } from "path";
|
|
1131
1198
|
import chalk6 from "chalk";
|
|
1132
1199
|
|
|
1133
1200
|
// ../../packages/types/src/agents/index.ts
|
|
@@ -1149,6 +1216,7 @@ var TelemetryEventTypes = {
|
|
|
1149
1216
|
// ../../packages/types/src/tunnel/index.ts
|
|
1150
1217
|
var MAX_FRAME_BYTES = 256 * 1024;
|
|
1151
1218
|
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
1219
|
+
var TUNNEL_USAGE_REARM_PING_PATH = "/__evident/usage-rearm";
|
|
1152
1220
|
|
|
1153
1221
|
// ../../packages/types/src/runner-files.ts
|
|
1154
1222
|
var MAX_FILE_PUSH_BYTES = 64 * 1024;
|
|
@@ -1467,7 +1535,14 @@ function drainSessionDbRecoveryReport({
|
|
|
1467
1535
|
skippedLines++;
|
|
1468
1536
|
return [];
|
|
1469
1537
|
}
|
|
1470
|
-
return [
|
|
1538
|
+
return [
|
|
1539
|
+
{
|
|
1540
|
+
...value,
|
|
1541
|
+
provenance_reason: value.provenance_reason ?? null,
|
|
1542
|
+
provenance_migration_delta: value.provenance_migration_delta ?? null,
|
|
1543
|
+
replication_suspended: value.replication_suspended ?? false
|
|
1544
|
+
}
|
|
1545
|
+
];
|
|
1471
1546
|
} catch (error2) {
|
|
1472
1547
|
skippedLines++;
|
|
1473
1548
|
console.error(
|
|
@@ -1492,12 +1567,39 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1492
1567
|
const level = record.severity === "warning" ? "warn" : record.severity === "error" ? "error" : null;
|
|
1493
1568
|
if (!level) return null;
|
|
1494
1569
|
const noVerifiedPoint = record.verified_restore_point ? ` The verified restore point is ${record.verified_restore_point}.` : " No verified restore point is known.";
|
|
1570
|
+
const replication = record.replication_suspended ? " This start is not backing up its new session history; restart after fixing the cause." : "";
|
|
1571
|
+
const giveupMessage = (() => {
|
|
1572
|
+
switch (record.reason) {
|
|
1573
|
+
case "restore_deadline_exceeded":
|
|
1574
|
+
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.";
|
|
1575
|
+
case "restore_tool_unusable":
|
|
1576
|
+
case "classification_unrecognised":
|
|
1577
|
+
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.";
|
|
1578
|
+
case "synchroniser_config_unevaluable":
|
|
1579
|
+
case "synchroniser_config_incomplete":
|
|
1580
|
+
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.";
|
|
1581
|
+
case "synchroniser_config_unresolved":
|
|
1582
|
+
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.";
|
|
1583
|
+
case "litestream_config_unavailable":
|
|
1584
|
+
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.";
|
|
1585
|
+
case "classification_fatal":
|
|
1586
|
+
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.";
|
|
1587
|
+
default:
|
|
1588
|
+
return null;
|
|
1589
|
+
}
|
|
1590
|
+
})();
|
|
1591
|
+
if (giveupMessage)
|
|
1592
|
+
return {
|
|
1593
|
+
level,
|
|
1594
|
+
metadata: withoutContractFields(record),
|
|
1595
|
+
message: `${giveupMessage}${replication}`
|
|
1596
|
+
};
|
|
1495
1597
|
switch (record.outcome) {
|
|
1496
1598
|
case "fresh_session_db":
|
|
1497
1599
|
return {
|
|
1498
1600
|
level,
|
|
1499
1601
|
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
|
|
1602
|
+
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
1603
|
};
|
|
1502
1604
|
case "restore_retried":
|
|
1503
1605
|
return {
|
|
@@ -1535,7 +1637,19 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1535
1637
|
return {
|
|
1536
1638
|
level,
|
|
1537
1639
|
metadata: withoutContractFields(record),
|
|
1538
|
-
message:
|
|
1640
|
+
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}`
|
|
1641
|
+
};
|
|
1642
|
+
case "session_db_boot_refused":
|
|
1643
|
+
return {
|
|
1644
|
+
level,
|
|
1645
|
+
metadata: withoutContractFields(record),
|
|
1646
|
+
message: `This runner did not come online because its damaged session database could not be safely separated from its active backup or proven removed. Backed-up session history remains readable at ${record.quarantine_destination ?? "its original location or the quarantine destination named in the boot logs"}; any local session-database files that remain were left in place and nothing opened or wrote them. See the runner boot logs for SESSION-DB-LOCAL-DISCARD-FAILED details.`
|
|
1647
|
+
};
|
|
1648
|
+
case "schema_provenance_mismatch":
|
|
1649
|
+
return {
|
|
1650
|
+
level,
|
|
1651
|
+
metadata: withoutContractFields(record),
|
|
1652
|
+
message: `Session database schema provenance mismatch for ${record.dbPath ?? record.db_path ?? "unknown"}: recorded version=${record.recorded_version ?? "unknown"}, current version=${record.current_version ?? "unknown"}, reason=${record.provenance_reason ?? "unknown"}, migration delta=${record.provenance_migration_delta ?? "unknown"}. Inspect the session database and runner backup before continuing.`
|
|
1539
1653
|
};
|
|
1540
1654
|
default:
|
|
1541
1655
|
return null;
|
|
@@ -1550,7 +1664,9 @@ var OUTCOMES = /* @__PURE__ */ new Set([
|
|
|
1550
1664
|
"restore_retried",
|
|
1551
1665
|
"fresh_session_db",
|
|
1552
1666
|
"history_rolled_back",
|
|
1553
|
-
"restore_misconfigured"
|
|
1667
|
+
"restore_misconfigured",
|
|
1668
|
+
"session_db_boot_refused",
|
|
1669
|
+
"schema_provenance_mismatch"
|
|
1554
1670
|
]);
|
|
1555
1671
|
var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
|
|
1556
1672
|
var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
|
|
@@ -1568,7 +1684,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
|
|
|
1568
1684
|
function isSessionDbRecoveryRecord(value) {
|
|
1569
1685
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1570
1686
|
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(
|
|
1687
|
+
return record.v === 1 && record.event === "session_db_recovery" && typeof record.at === "string" && STAGES.has(record.stage) && OUTCOMES.has(record.outcome) && SEVERITIES.has(record.severity) && typeof record.reason === "string" && (record.replication_suspended === void 0 || typeof record.replication_suspended === "boolean") && (record.provenance_reason === void 0 || record.provenance_reason === null || typeof record.provenance_reason === "string") && (record.provenance_migration_delta === void 0 || record.provenance_migration_delta === null || Number.isInteger(record.provenance_migration_delta)) && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
|
|
1572
1688
|
(field) => record[field] === null || typeof record[field] === "string"
|
|
1573
1689
|
);
|
|
1574
1690
|
}
|
|
@@ -1597,215 +1713,933 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
1597
1713
|
if (health.healthy) {
|
|
1598
1714
|
return health;
|
|
1599
1715
|
}
|
|
1600
|
-
await new Promise((
|
|
1716
|
+
await new Promise((resolve4) => setTimeout(resolve4, 1e3));
|
|
1601
1717
|
}
|
|
1602
1718
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
1603
1719
|
}
|
|
1604
1720
|
|
|
1605
|
-
// src/lib/opencode/
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
}
|
|
1611
|
-
function buildOpenCodeVersionWarning(version2) {
|
|
1612
|
-
if (isQueueValidatedVersion(version2)) return null;
|
|
1613
|
-
const detected = version2 ? `v${version2}` : "unknown";
|
|
1614
|
-
const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
|
|
1615
|
-
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
|
|
1616
|
-
}
|
|
1721
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1722
|
+
import { spawn as spawn2 } from "child_process";
|
|
1723
|
+
import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "fs";
|
|
1724
|
+
import { homedir as homedir2 } from "os";
|
|
1725
|
+
import { dirname as dirname2, resolve as resolvePath } from "path";
|
|
1617
1726
|
|
|
1618
|
-
// src/lib/
|
|
1619
|
-
import {
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1727
|
+
// src/lib/runner-synchroniser.ts
|
|
1728
|
+
import { spawn } from "child_process";
|
|
1729
|
+
function appendError(stderr, error2) {
|
|
1730
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1731
|
+
return stderr === "" ? message : `${stderr}
|
|
1732
|
+
${message}`;
|
|
1733
|
+
}
|
|
1734
|
+
function runSynchroniser(args, opts) {
|
|
1735
|
+
return new Promise((resolve4) => {
|
|
1736
|
+
let child;
|
|
1737
|
+
let stdout = "";
|
|
1738
|
+
let stderr = "";
|
|
1739
|
+
let settled = false;
|
|
1740
|
+
const timer = {};
|
|
1741
|
+
let abortListener;
|
|
1742
|
+
let spawnListener;
|
|
1743
|
+
const finish = (result) => {
|
|
1744
|
+
if (settled) return;
|
|
1745
|
+
settled = true;
|
|
1746
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
1747
|
+
if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
|
|
1748
|
+
if (spawnListener) child.removeListener("spawn", spawnListener);
|
|
1749
|
+
resolve4(result);
|
|
1750
|
+
};
|
|
1751
|
+
try {
|
|
1752
|
+
child = spawn("runner-synchroniser", args, {
|
|
1753
|
+
env: opts.env ?? process.env,
|
|
1754
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1755
|
+
});
|
|
1756
|
+
} catch (error2) {
|
|
1757
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
child.stdout?.setEncoding("utf8");
|
|
1761
|
+
child.stdout?.on("data", (chunk) => {
|
|
1762
|
+
stdout += chunk;
|
|
1763
|
+
});
|
|
1764
|
+
child.stderr?.setEncoding("utf8");
|
|
1765
|
+
child.stderr?.on("data", (chunk) => {
|
|
1766
|
+
stderr += chunk;
|
|
1767
|
+
});
|
|
1768
|
+
child.once("error", (error2) => {
|
|
1769
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1770
|
+
});
|
|
1771
|
+
child.once("close", (code) => {
|
|
1772
|
+
finish({ code, stdout, stderr, timedOut: false });
|
|
1773
|
+
});
|
|
1774
|
+
if (opts.signal) {
|
|
1775
|
+
const killChild = () => {
|
|
1776
|
+
if (child.pid === void 0) {
|
|
1777
|
+
if (!spawnListener) {
|
|
1778
|
+
spawnListener = killChild;
|
|
1779
|
+
child.once("spawn", spawnListener);
|
|
1780
|
+
}
|
|
1781
|
+
return;
|
|
1633
1782
|
}
|
|
1783
|
+
child.kill("SIGKILL");
|
|
1784
|
+
};
|
|
1785
|
+
abortListener = killChild;
|
|
1786
|
+
if (opts.signal.aborted) {
|
|
1787
|
+
abortListener();
|
|
1788
|
+
} else {
|
|
1789
|
+
opts.signal.addEventListener("abort", abortListener, { once: true });
|
|
1790
|
+
if (opts.signal.aborted) abortListener();
|
|
1634
1791
|
}
|
|
1635
|
-
} else if (platform === "linux") {
|
|
1636
|
-
const output = execSync(`readlink /proc/${pid}/cwd 2>/dev/null`, {
|
|
1637
|
-
encoding: "utf-8",
|
|
1638
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1639
|
-
}).trim();
|
|
1640
|
-
if (output) return output;
|
|
1641
1792
|
}
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1793
|
+
timer.handle = setTimeout(
|
|
1794
|
+
() => {
|
|
1795
|
+
child.kill("SIGKILL");
|
|
1796
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1797
|
+
},
|
|
1798
|
+
Math.max(0, opts.timeoutMs)
|
|
1799
|
+
);
|
|
1800
|
+
});
|
|
1645
1801
|
}
|
|
1646
|
-
|
|
1647
|
-
|
|
1802
|
+
|
|
1803
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1804
|
+
var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
|
|
1805
|
+
var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
|
|
1806
|
+
var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
|
|
1807
|
+
var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
|
|
1808
|
+
function commandError(result) {
|
|
1809
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
1810
|
+
}
|
|
1811
|
+
function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
|
|
1812
|
+
options.reportRecovery({
|
|
1813
|
+
v: 1,
|
|
1814
|
+
event: "session_db_recovery",
|
|
1815
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1816
|
+
stage,
|
|
1817
|
+
outcome,
|
|
1818
|
+
severity: "error",
|
|
1819
|
+
reason,
|
|
1820
|
+
litestream_exit_code: litestreamExitCode,
|
|
1821
|
+
attempt: null,
|
|
1822
|
+
replica_objects: null,
|
|
1823
|
+
replica_bytes: null,
|
|
1824
|
+
quarantine_destination: null,
|
|
1825
|
+
quarantined_objects: null,
|
|
1826
|
+
quarantine_failed_objects: null,
|
|
1827
|
+
quarantined_bytes: null,
|
|
1828
|
+
verified_restore_point: null,
|
|
1829
|
+
restore_points_tried: null,
|
|
1830
|
+
provenance_reason: null,
|
|
1831
|
+
provenance_migration_delta: null,
|
|
1832
|
+
replication_suspended: stage === "restore"
|
|
1833
|
+
});
|
|
1834
|
+
}
|
|
1835
|
+
function clearMarker(options) {
|
|
1836
|
+
if (!options.noReplicateMarker) return;
|
|
1648
1837
|
try {
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
})
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
} catch {
|
|
1838
|
+
unlinkSync2(options.noReplicateMarker);
|
|
1839
|
+
} catch (error2) {
|
|
1840
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
|
|
1841
|
+
options.log(
|
|
1842
|
+
`Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1843
|
+
"warn"
|
|
1844
|
+
);
|
|
1657
1845
|
}
|
|
1658
|
-
return false;
|
|
1659
1846
|
}
|
|
1660
|
-
function
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1847
|
+
function markNoReplicate(options, message) {
|
|
1848
|
+
if (options.noReplicateMarker) {
|
|
1849
|
+
try {
|
|
1850
|
+
mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
|
|
1851
|
+
writeFileSync(options.noReplicateMarker, "");
|
|
1852
|
+
} catch (error2) {
|
|
1853
|
+
options.log(
|
|
1854
|
+
`Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1855
|
+
"error"
|
|
1856
|
+
);
|
|
1665
1857
|
}
|
|
1666
1858
|
}
|
|
1667
|
-
|
|
1859
|
+
options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
|
|
1668
1860
|
}
|
|
1669
|
-
function
|
|
1670
|
-
const
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
}).trim();
|
|
1680
|
-
if (pgrepOutput) {
|
|
1681
|
-
pids = pgrepOutput.split("\n").map((p) => parseInt(p.trim(), 10)).filter((p) => !isNaN(p));
|
|
1682
|
-
}
|
|
1683
|
-
} catch {
|
|
1684
|
-
try {
|
|
1685
|
-
const psOutput = execSync('ps aux | grep -E "opencode (serve|--port)" | grep -v grep', {
|
|
1686
|
-
encoding: "utf-8",
|
|
1687
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1688
|
-
}).trim();
|
|
1689
|
-
if (psOutput) {
|
|
1690
|
-
for (const line of psOutput.split("\n")) {
|
|
1691
|
-
const parts = line.trim().split(/\s+/);
|
|
1692
|
-
if (parts.length >= 2) {
|
|
1693
|
-
const pid = parseInt(parts[1], 10);
|
|
1694
|
-
if (!isNaN(pid)) pids.push(pid);
|
|
1695
|
-
}
|
|
1696
|
-
}
|
|
1697
|
-
}
|
|
1698
|
-
} catch (err) {
|
|
1699
|
-
console.warn(
|
|
1700
|
-
`findOpenCodeProcesses: ps fallback failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1701
|
-
);
|
|
1702
|
-
}
|
|
1703
|
-
}
|
|
1704
|
-
for (const pid of pids) {
|
|
1705
|
-
try {
|
|
1706
|
-
const lsofOutput = execSync(`lsof -Pan -p ${pid} -i TCP -sTCP:LISTEN 2>/dev/null`, {
|
|
1707
|
-
encoding: "utf-8",
|
|
1708
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1709
|
-
}).trim();
|
|
1710
|
-
for (const line of lsofOutput.split("\n")) {
|
|
1711
|
-
const portMatch = line.match(/:(\d+)\s+\(LISTEN\)/);
|
|
1712
|
-
if (portMatch) {
|
|
1713
|
-
const port = parseInt(portMatch[1], 10);
|
|
1714
|
-
if (!isNaN(port) && !instances.some((i) => i.port === port)) {
|
|
1715
|
-
const cwd = getProcessCwd(pid);
|
|
1716
|
-
instances.push({ pid, port, cwd });
|
|
1717
|
-
}
|
|
1718
|
-
}
|
|
1719
|
-
}
|
|
1720
|
-
} catch {
|
|
1721
|
-
}
|
|
1722
|
-
}
|
|
1861
|
+
function discardSessionDbDebris(options) {
|
|
1862
|
+
for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
|
|
1863
|
+
try {
|
|
1864
|
+
unlinkSync2(path);
|
|
1865
|
+
} catch (error2) {
|
|
1866
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
|
|
1867
|
+
options.log(
|
|
1868
|
+
`Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1869
|
+
"warn"
|
|
1870
|
+
);
|
|
1723
1871
|
}
|
|
1724
|
-
} catch (err) {
|
|
1725
|
-
console.warn(
|
|
1726
|
-
`findOpenCodeProcesses: process detection failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1727
|
-
);
|
|
1728
1872
|
}
|
|
1729
|
-
return instances;
|
|
1730
1873
|
}
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
const checks = OPENCODE_PORT_RANGE.map(async (port) => {
|
|
1734
|
-
const health = await checkOpenCodeHealth(port);
|
|
1735
|
-
if (health.healthy) {
|
|
1736
|
-
let pid = 0;
|
|
1737
|
-
try {
|
|
1738
|
-
const lsofOutput = execSync(`lsof -ti :${port} -sTCP:LISTEN 2>/dev/null`, {
|
|
1739
|
-
encoding: "utf-8",
|
|
1740
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
1741
|
-
}).trim();
|
|
1742
|
-
if (lsofOutput) {
|
|
1743
|
-
pid = parseInt(lsofOutput.split("\n")[0], 10) || 0;
|
|
1744
|
-
}
|
|
1745
|
-
} catch {
|
|
1746
|
-
}
|
|
1747
|
-
const cwd = pid ? getProcessCwd(pid) : void 0;
|
|
1748
|
-
return { pid, port, cwd, version: health.version };
|
|
1749
|
-
}
|
|
1750
|
-
return null;
|
|
1751
|
-
});
|
|
1752
|
-
const results = await Promise.all(checks);
|
|
1753
|
-
for (const result of results) {
|
|
1754
|
-
if (result) {
|
|
1755
|
-
instances.push(result);
|
|
1756
|
-
}
|
|
1757
|
-
}
|
|
1758
|
-
return instances;
|
|
1874
|
+
function splitDiagnostics(text) {
|
|
1875
|
+
return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
|
|
1759
1876
|
}
|
|
1760
|
-
|
|
1761
|
-
const
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1877
|
+
function logSynchroniserDiagnostics(result, options) {
|
|
1878
|
+
for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
|
|
1879
|
+
}
|
|
1880
|
+
function parseSingleQuotedAssignment(line) {
|
|
1881
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
|
|
1882
|
+
if (!match || !match[2].startsWith("'")) return null;
|
|
1883
|
+
const valueSource = match[2];
|
|
1884
|
+
let value = "";
|
|
1885
|
+
for (let index = 1; index < valueSource.length; index++) {
|
|
1886
|
+
const character = valueSource[index];
|
|
1887
|
+
if (character !== "'") {
|
|
1888
|
+
value += character;
|
|
1889
|
+
continue;
|
|
1767
1890
|
}
|
|
1891
|
+
if (index === valueSource.length - 1) return [match[1], value];
|
|
1892
|
+
if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
|
|
1893
|
+
value += "'";
|
|
1894
|
+
index += 3;
|
|
1768
1895
|
}
|
|
1769
|
-
|
|
1770
|
-
const scanned = await scanPortsForOpenCode();
|
|
1771
|
-
return scanned;
|
|
1772
|
-
}
|
|
1773
|
-
return healthy;
|
|
1896
|
+
return null;
|
|
1774
1897
|
}
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1898
|
+
function parseSynchroniserEnv(stdout) {
|
|
1899
|
+
const values = {};
|
|
1900
|
+
for (const line of stdout.split("\n")) {
|
|
1901
|
+
if (line.trim() === "") continue;
|
|
1902
|
+
const assignment = parseSingleQuotedAssignment(line);
|
|
1903
|
+
if (!assignment) return null;
|
|
1904
|
+
values[assignment[0]] = assignment[1];
|
|
1905
|
+
}
|
|
1906
|
+
return values;
|
|
1907
|
+
}
|
|
1908
|
+
function runCommand(command, args, options) {
|
|
1909
|
+
return new Promise((resolve4) => {
|
|
1910
|
+
let child;
|
|
1911
|
+
let stdout = "";
|
|
1912
|
+
let stderr = "";
|
|
1913
|
+
let settled = false;
|
|
1914
|
+
const finish = (result) => {
|
|
1915
|
+
if (settled) return;
|
|
1916
|
+
settled = true;
|
|
1917
|
+
if (timer) clearTimeout(timer);
|
|
1918
|
+
resolve4(result);
|
|
1919
|
+
};
|
|
1920
|
+
try {
|
|
1921
|
+
child = spawn2(command, args, {
|
|
1922
|
+
env: options.env,
|
|
1923
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1924
|
+
});
|
|
1925
|
+
} catch (error2) {
|
|
1926
|
+
resolve4({
|
|
1927
|
+
code: null,
|
|
1928
|
+
stdout,
|
|
1929
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
1930
|
+
timedOut: false
|
|
1931
|
+
});
|
|
1932
|
+
return;
|
|
1933
|
+
}
|
|
1934
|
+
child.stdout?.setEncoding("utf8");
|
|
1935
|
+
child.stdout?.on("data", (chunk) => {
|
|
1936
|
+
stdout += chunk;
|
|
1937
|
+
});
|
|
1938
|
+
child.stderr?.setEncoding("utf8");
|
|
1939
|
+
child.stderr?.on("data", (chunk) => {
|
|
1940
|
+
stderr += chunk;
|
|
1941
|
+
});
|
|
1942
|
+
child.once("error", (error2) => {
|
|
1943
|
+
finish({
|
|
1944
|
+
code: null,
|
|
1945
|
+
stdout,
|
|
1946
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
1947
|
+
${error2.message}`,
|
|
1948
|
+
timedOut: false
|
|
1949
|
+
});
|
|
1950
|
+
});
|
|
1951
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
1952
|
+
const timer = setTimeout(
|
|
1953
|
+
() => {
|
|
1954
|
+
child.kill("SIGKILL");
|
|
1955
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1956
|
+
},
|
|
1957
|
+
Math.max(0, options.timeoutMs)
|
|
1958
|
+
);
|
|
1788
1959
|
});
|
|
1789
|
-
return child;
|
|
1790
1960
|
}
|
|
1791
|
-
function
|
|
1792
|
-
|
|
1793
|
-
|
|
1961
|
+
async function ensureLitestreamConfig(options, env) {
|
|
1962
|
+
const configPath = options.litestreamConfig;
|
|
1963
|
+
if (!configPath) {
|
|
1964
|
+
markNoReplicate(options, "no Litestream configuration path was provided");
|
|
1965
|
+
reportRecord(
|
|
1966
|
+
"restore",
|
|
1967
|
+
"restore_misconfigured",
|
|
1968
|
+
"litestream_config_unavailable",
|
|
1969
|
+
null,
|
|
1970
|
+
options
|
|
1971
|
+
);
|
|
1972
|
+
return null;
|
|
1794
1973
|
}
|
|
1795
1974
|
try {
|
|
1796
|
-
if (
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1975
|
+
if (statSync2(configPath).size > 0) return configPath;
|
|
1976
|
+
} catch (error2) {
|
|
1977
|
+
if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
|
|
1978
|
+
options.log(
|
|
1979
|
+
`Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1980
|
+
"warn"
|
|
1981
|
+
);
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
const rendered = await runSynchroniser(["litestream-config"], {
|
|
1985
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
1986
|
+
env
|
|
1987
|
+
});
|
|
1988
|
+
logSynchroniserDiagnostics(rendered, options);
|
|
1989
|
+
if (rendered.timedOut || rendered.code !== 0) {
|
|
1990
|
+
options.log(
|
|
1991
|
+
`Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
|
|
1992
|
+
"error"
|
|
1993
|
+
);
|
|
1994
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1995
|
+
reportRecord(
|
|
1996
|
+
"restore",
|
|
1997
|
+
"restore_misconfigured",
|
|
1998
|
+
"litestream_config_unavailable",
|
|
1999
|
+
null,
|
|
2000
|
+
options
|
|
2001
|
+
);
|
|
2002
|
+
return null;
|
|
2003
|
+
}
|
|
2004
|
+
try {
|
|
2005
|
+
mkdirSync(dirname2(configPath), { recursive: true });
|
|
2006
|
+
writeFileSync(configPath, rendered.stdout);
|
|
2007
|
+
} catch (error2) {
|
|
2008
|
+
options.log(
|
|
2009
|
+
`Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
2010
|
+
"error"
|
|
2011
|
+
);
|
|
2012
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
2013
|
+
reportRecord(
|
|
2014
|
+
"restore",
|
|
2015
|
+
"restore_misconfigured",
|
|
2016
|
+
"litestream_config_unavailable",
|
|
2017
|
+
null,
|
|
2018
|
+
options
|
|
2019
|
+
);
|
|
2020
|
+
return null;
|
|
2021
|
+
}
|
|
2022
|
+
const version2 = await runCommand("litestream", ["version"], {
|
|
2023
|
+
env,
|
|
2024
|
+
timeoutMs: 1e4
|
|
2025
|
+
});
|
|
2026
|
+
const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
|
|
2027
|
+
const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
|
|
2028
|
+
options.log(
|
|
2029
|
+
`litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
|
|
2030
|
+
);
|
|
2031
|
+
return configPath;
|
|
2032
|
+
}
|
|
2033
|
+
function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
|
|
2034
|
+
discardSessionDbDebris(options);
|
|
2035
|
+
markNoReplicate(options, message);
|
|
2036
|
+
reportRecord("restore", outcome, reason, litestreamExitCode, options);
|
|
2037
|
+
}
|
|
2038
|
+
async function restoreSessionDb(options, configPath, env) {
|
|
2039
|
+
const restored = await runCommand(
|
|
2040
|
+
"litestream",
|
|
2041
|
+
["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
|
|
2042
|
+
{ env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
|
|
2043
|
+
);
|
|
2044
|
+
for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
|
|
2045
|
+
if (restored.timedOut || restored.code === 124 || restored.code === 137) {
|
|
2046
|
+
restoreGiveUp(
|
|
2047
|
+
options,
|
|
2048
|
+
"restore_deadline_exceeded",
|
|
2049
|
+
`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`,
|
|
2050
|
+
restored.code ?? 124
|
|
2051
|
+
);
|
|
2052
|
+
return;
|
|
2053
|
+
}
|
|
2054
|
+
if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
|
|
2055
|
+
options.log(`litestream restore could not run (${commandError(restored)})`, "error");
|
|
2056
|
+
restoreGiveUp(
|
|
2057
|
+
options,
|
|
2058
|
+
"restore_tool_unusable",
|
|
2059
|
+
`restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
|
|
2060
|
+
restored.code
|
|
2061
|
+
);
|
|
2062
|
+
return;
|
|
2063
|
+
}
|
|
2064
|
+
const classified = await runSynchroniser(
|
|
2065
|
+
[
|
|
2066
|
+
"session-db-classify",
|
|
2067
|
+
String(restored.code ?? 1),
|
|
2068
|
+
"1",
|
|
2069
|
+
"--on-unusable-replica=leave",
|
|
2070
|
+
"--fresh-db-fallback"
|
|
2071
|
+
],
|
|
2072
|
+
{ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
|
|
2073
|
+
);
|
|
2074
|
+
logSynchroniserDiagnostics(classified, options);
|
|
2075
|
+
const classifyCode = classified.code;
|
|
2076
|
+
switch (classifyCode) {
|
|
2077
|
+
case 0:
|
|
2078
|
+
return;
|
|
2079
|
+
case 31:
|
|
2080
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2081
|
+
options.log(
|
|
2082
|
+
"SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
|
|
2083
|
+
"warn"
|
|
2084
|
+
);
|
|
2085
|
+
return;
|
|
2086
|
+
case 32:
|
|
2087
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2088
|
+
discardSessionDbDebris(options);
|
|
2089
|
+
markNoReplicate(
|
|
2090
|
+
options,
|
|
2091
|
+
"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"
|
|
2092
|
+
);
|
|
2093
|
+
return;
|
|
2094
|
+
case 30:
|
|
2095
|
+
restoreGiveUp(
|
|
2096
|
+
options,
|
|
2097
|
+
"classification_fatal",
|
|
2098
|
+
"session-db-classify returned fatal (30); see the FATAL message above",
|
|
2099
|
+
restored.code,
|
|
2100
|
+
"restore_misconfigured"
|
|
2101
|
+
);
|
|
2102
|
+
return;
|
|
2103
|
+
default:
|
|
2104
|
+
restoreGiveUp(
|
|
2105
|
+
options,
|
|
2106
|
+
"classification_unrecognised",
|
|
2107
|
+
`session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
|
|
2108
|
+
restored.code
|
|
2109
|
+
);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
async function verifySessionDb(options, configPath, env) {
|
|
2113
|
+
if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
|
|
2114
|
+
const result = await runSynchroniser(["session-db-verify", configPath], {
|
|
2115
|
+
timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
|
|
2116
|
+
env: {
|
|
2117
|
+
...env,
|
|
2118
|
+
// The synchroniser reads this value in SECONDS. Keep this at 120, not
|
|
2119
|
+
// 120_000, so the walkback gives up before the outer process bound.
|
|
2120
|
+
EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
|
|
2121
|
+
SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
|
|
2122
|
+
)
|
|
2123
|
+
}
|
|
2124
|
+
});
|
|
2125
|
+
logSynchroniserDiagnostics(result, options);
|
|
2126
|
+
if (result.timedOut || result.code === 124 || result.code === 137) {
|
|
2127
|
+
options.log(
|
|
2128
|
+
`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`,
|
|
2129
|
+
"warn"
|
|
2130
|
+
);
|
|
2131
|
+
return false;
|
|
2132
|
+
}
|
|
2133
|
+
if (result.code === 34) {
|
|
2134
|
+
reportRecord(
|
|
2135
|
+
"verify",
|
|
2136
|
+
"session_db_boot_refused",
|
|
2137
|
+
result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
|
|
2138
|
+
null,
|
|
2139
|
+
options
|
|
2140
|
+
);
|
|
2141
|
+
return true;
|
|
2142
|
+
}
|
|
2143
|
+
if (result.code === 33) {
|
|
2144
|
+
options.log(
|
|
2145
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
|
|
2146
|
+
"warn"
|
|
2147
|
+
);
|
|
2148
|
+
return false;
|
|
2149
|
+
}
|
|
2150
|
+
if (result.code !== 0) {
|
|
2151
|
+
options.log(
|
|
2152
|
+
`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`,
|
|
2153
|
+
"warn"
|
|
2154
|
+
);
|
|
2155
|
+
}
|
|
2156
|
+
return false;
|
|
2157
|
+
}
|
|
2158
|
+
options.log(
|
|
2159
|
+
"skipping session-DB verification: this boot's session DB was not proven safe to replicate",
|
|
2160
|
+
"debug"
|
|
2161
|
+
);
|
|
2162
|
+
return false;
|
|
2163
|
+
}
|
|
2164
|
+
function fileExists(path) {
|
|
2165
|
+
try {
|
|
2166
|
+
statSync2(path);
|
|
2167
|
+
return true;
|
|
2168
|
+
} catch (error2) {
|
|
2169
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
|
|
2170
|
+
return true;
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
async function restoreAndVerifySessionDb(options) {
|
|
2174
|
+
const env = options.env ?? process.env;
|
|
2175
|
+
clearMarker(options);
|
|
2176
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2177
|
+
const synchroniserEnv = await runSynchroniser(["env"], {
|
|
2178
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
2179
|
+
env
|
|
2180
|
+
});
|
|
2181
|
+
logSynchroniserDiagnostics(synchroniserEnv, options);
|
|
2182
|
+
if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
|
|
2183
|
+
options.log(
|
|
2184
|
+
`runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
|
|
2185
|
+
"error"
|
|
2186
|
+
);
|
|
2187
|
+
markNoReplicate(
|
|
2188
|
+
options,
|
|
2189
|
+
"could not resolve the runner-synchroniser configuration (see the ERROR above)"
|
|
2190
|
+
);
|
|
2191
|
+
reportRecord(
|
|
2192
|
+
"restore",
|
|
2193
|
+
"restore_misconfigured",
|
|
2194
|
+
"synchroniser_config_unresolved",
|
|
2195
|
+
null,
|
|
2196
|
+
options
|
|
2197
|
+
);
|
|
2198
|
+
return { verifyFatal: false };
|
|
2199
|
+
}
|
|
2200
|
+
const values = parseSynchroniserEnv(synchroniserEnv.stdout);
|
|
2201
|
+
if (!values) {
|
|
2202
|
+
markNoReplicate(
|
|
2203
|
+
options,
|
|
2204
|
+
"the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
|
|
2205
|
+
);
|
|
2206
|
+
reportRecord(
|
|
2207
|
+
"restore",
|
|
2208
|
+
"restore_misconfigured",
|
|
2209
|
+
"synchroniser_config_unevaluable",
|
|
2210
|
+
null,
|
|
2211
|
+
options
|
|
2212
|
+
);
|
|
2213
|
+
return { verifyFatal: false };
|
|
2214
|
+
}
|
|
2215
|
+
const synchroniserDbPath = values.OPENCODE_DB_PATH;
|
|
2216
|
+
if (!synchroniserDbPath) {
|
|
2217
|
+
markNoReplicate(
|
|
2218
|
+
options,
|
|
2219
|
+
"run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
|
|
2220
|
+
);
|
|
2221
|
+
reportRecord(
|
|
2222
|
+
"restore",
|
|
2223
|
+
"restore_misconfigured",
|
|
2224
|
+
"synchroniser_config_incomplete",
|
|
2225
|
+
null,
|
|
2226
|
+
options
|
|
2227
|
+
);
|
|
2228
|
+
return { verifyFatal: false };
|
|
2229
|
+
}
|
|
2230
|
+
if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
|
|
2231
|
+
options.log(
|
|
2232
|
+
`runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
|
|
2233
|
+
"warn"
|
|
2234
|
+
);
|
|
2235
|
+
}
|
|
2236
|
+
if (!values.PERSISTENCE_BUCKET) {
|
|
2237
|
+
options.log(
|
|
2238
|
+
"SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
|
|
2239
|
+
"warn"
|
|
2240
|
+
);
|
|
2241
|
+
return { verifyFatal: false };
|
|
2242
|
+
}
|
|
2243
|
+
const configPath = await ensureLitestreamConfig(options, env);
|
|
2244
|
+
if (!configPath) return { verifyFatal: false };
|
|
2245
|
+
await restoreSessionDb(options, configPath, env);
|
|
2246
|
+
if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
|
|
2247
|
+
return { verifyFatal: false };
|
|
2248
|
+
}
|
|
2249
|
+
return { verifyFatal: await verifySessionDb(options, configPath, env) };
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
// src/lib/opencode/session-db-provenance.ts
|
|
2253
|
+
import { createRequire } from "module";
|
|
2254
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
2255
|
+
import { dirname as dirname3, join as join3 } from "path";
|
|
2256
|
+
var require2 = createRequire(import.meta.url);
|
|
2257
|
+
function readSessionDbMigrationIds(dbPath) {
|
|
2258
|
+
let db;
|
|
2259
|
+
try {
|
|
2260
|
+
const { DatabaseSync } = require2("node:sqlite");
|
|
2261
|
+
db = new DatabaseSync(dbPath, { readOnly: true });
|
|
2262
|
+
const columns = db.prepare("PRAGMA table_info(migration)").all();
|
|
2263
|
+
const hasExpectedShape = columns.length === 2 && columns.some(
|
|
2264
|
+
(column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
|
|
2265
|
+
) && columns.some(
|
|
2266
|
+
(column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
|
|
2267
|
+
);
|
|
2268
|
+
if (!hasExpectedShape) {
|
|
2269
|
+
console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
|
|
2270
|
+
return null;
|
|
2271
|
+
}
|
|
2272
|
+
const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
|
|
2273
|
+
if (rows.some((row) => typeof row.id !== "string")) return null;
|
|
2274
|
+
return rows.map((row) => row.id);
|
|
2275
|
+
} catch (error2) {
|
|
2276
|
+
console.warn(
|
|
2277
|
+
`[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2278
|
+
);
|
|
2279
|
+
return null;
|
|
2280
|
+
} finally {
|
|
2281
|
+
try {
|
|
2282
|
+
db?.close();
|
|
2283
|
+
} catch (error2) {
|
|
1803
2284
|
console.warn(
|
|
1804
|
-
`
|
|
2285
|
+
`[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
1805
2286
|
);
|
|
1806
2287
|
}
|
|
1807
2288
|
}
|
|
1808
2289
|
}
|
|
2290
|
+
function sessionDbProvenanceStatePath(homeDir, env) {
|
|
2291
|
+
const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
|
|
2292
|
+
return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
|
|
2293
|
+
}
|
|
2294
|
+
function loadSessionDbProvenanceState(path) {
|
|
2295
|
+
let value;
|
|
2296
|
+
try {
|
|
2297
|
+
value = JSON.parse(readFileSync3(path, "utf8"));
|
|
2298
|
+
} catch (error2) {
|
|
2299
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
|
|
2300
|
+
console.error(
|
|
2301
|
+
`[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2302
|
+
);
|
|
2303
|
+
return {};
|
|
2304
|
+
}
|
|
2305
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2306
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2307
|
+
return {};
|
|
2308
|
+
}
|
|
2309
|
+
const state = {};
|
|
2310
|
+
for (const [dbPath, record] of Object.entries(value)) {
|
|
2311
|
+
if (!isSessionDbProvenanceRecord(record)) {
|
|
2312
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2313
|
+
return {};
|
|
2314
|
+
}
|
|
2315
|
+
state[dbPath] = record;
|
|
2316
|
+
}
|
|
2317
|
+
return state;
|
|
2318
|
+
}
|
|
2319
|
+
function saveSessionDbProvenanceState(path, state) {
|
|
2320
|
+
try {
|
|
2321
|
+
mkdirSync2(dirname3(path), { recursive: true });
|
|
2322
|
+
writeFileSync2(path, `${JSON.stringify(state, null, 2)}
|
|
2323
|
+
`, "utf8");
|
|
2324
|
+
} catch (error2) {
|
|
2325
|
+
console.error(
|
|
2326
|
+
`[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2327
|
+
);
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
function evaluateSessionDbProvenance(input) {
|
|
2331
|
+
const { currentVersion, currentIds, previous } = input;
|
|
2332
|
+
if (!previous) return { anomaly: false, reason: null };
|
|
2333
|
+
const current = new Set(currentIds);
|
|
2334
|
+
const prior = new Set(previous.migrationIds);
|
|
2335
|
+
for (const id of prior) {
|
|
2336
|
+
if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
|
|
2337
|
+
}
|
|
2338
|
+
if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
|
|
2339
|
+
return { anomaly: true, reason: "foreign-version-migrations" };
|
|
2340
|
+
}
|
|
2341
|
+
return { anomaly: false, reason: null };
|
|
2342
|
+
}
|
|
2343
|
+
function checkSessionDbProvenance(input) {
|
|
2344
|
+
const { dbPath, currentVersion, homeDir, env } = input;
|
|
2345
|
+
const path = sessionDbProvenanceStatePath(homeDir, env);
|
|
2346
|
+
const state = loadSessionDbProvenanceState(path);
|
|
2347
|
+
const previous = state[dbPath];
|
|
2348
|
+
const currentIds = readSessionDbMigrationIds(dbPath);
|
|
2349
|
+
if (currentIds === null) {
|
|
2350
|
+
return {
|
|
2351
|
+
anomaly: false,
|
|
2352
|
+
reason: null,
|
|
2353
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2354
|
+
migrationDelta: null
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
|
|
2358
|
+
const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
|
|
2359
|
+
state[dbPath] = {
|
|
2360
|
+
opencodeVersion: currentVersion,
|
|
2361
|
+
migrationIds: currentIds,
|
|
2362
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2363
|
+
};
|
|
2364
|
+
saveSessionDbProvenanceState(path, state);
|
|
2365
|
+
return {
|
|
2366
|
+
...decision,
|
|
2367
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2368
|
+
migrationDelta
|
|
2369
|
+
};
|
|
2370
|
+
}
|
|
2371
|
+
function isSessionDbProvenanceRecord(value) {
|
|
2372
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
2373
|
+
const record = value;
|
|
2374
|
+
return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
// src/lib/opencode/opencode-version-gate.ts
|
|
2378
|
+
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
2379
|
+
function isQueueValidatedVersion(version2) {
|
|
2380
|
+
if (!version2) return false;
|
|
2381
|
+
return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
|
|
2382
|
+
}
|
|
2383
|
+
function buildOpenCodeVersionWarning(version2) {
|
|
2384
|
+
if (isQueueValidatedVersion(version2)) return null;
|
|
2385
|
+
const detected = version2 ? `v${version2}` : "unknown";
|
|
2386
|
+
const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
|
|
2387
|
+
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
// src/lib/opencode/process.ts
|
|
2391
|
+
import { execSync, spawn as spawn3 } from "child_process";
|
|
2392
|
+
|
|
2393
|
+
// src/lib/process-stop.ts
|
|
2394
|
+
async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
2395
|
+
if (!child.pid) {
|
|
2396
|
+
return { outcome: "not-running", code: child.exitCode, signal: child.signalCode };
|
|
2397
|
+
}
|
|
2398
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
2399
|
+
return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
|
|
2400
|
+
}
|
|
2401
|
+
return new Promise((resolve4, reject) => {
|
|
2402
|
+
let forced = false;
|
|
2403
|
+
let settled = false;
|
|
2404
|
+
const timer = setTimeout(() => {
|
|
2405
|
+
forced = true;
|
|
2406
|
+
try {
|
|
2407
|
+
sendKill();
|
|
2408
|
+
} catch (error2) {
|
|
2409
|
+
if (error2.code === "ESRCH") {
|
|
2410
|
+
finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
|
|
2411
|
+
} else {
|
|
2412
|
+
fail(error2);
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
}, timeoutMs);
|
|
2416
|
+
const finish = (result) => {
|
|
2417
|
+
if (settled) return;
|
|
2418
|
+
settled = true;
|
|
2419
|
+
clearTimeout(timer);
|
|
2420
|
+
child.removeListener("exit", onExit);
|
|
2421
|
+
resolve4(result);
|
|
2422
|
+
};
|
|
2423
|
+
const fail = (error2) => {
|
|
2424
|
+
if (settled) return;
|
|
2425
|
+
settled = true;
|
|
2426
|
+
clearTimeout(timer);
|
|
2427
|
+
child.removeListener("exit", onExit);
|
|
2428
|
+
reject(error2);
|
|
2429
|
+
};
|
|
2430
|
+
const onExit = (code, signal) => {
|
|
2431
|
+
finish({ outcome: forced ? "killed" : "exited", code, signal });
|
|
2432
|
+
};
|
|
2433
|
+
child.once("exit", onExit);
|
|
2434
|
+
try {
|
|
2435
|
+
sendTerm();
|
|
2436
|
+
} catch (error2) {
|
|
2437
|
+
if (error2.code === "ESRCH") {
|
|
2438
|
+
finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
|
|
2439
|
+
} else {
|
|
2440
|
+
fail(error2);
|
|
2441
|
+
}
|
|
2442
|
+
return;
|
|
2443
|
+
}
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
// src/lib/opencode/process.ts
|
|
2448
|
+
var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
|
|
2449
|
+
function getProcessCwd(pid) {
|
|
2450
|
+
const platform = process.platform;
|
|
2451
|
+
try {
|
|
2452
|
+
if (platform === "darwin") {
|
|
2453
|
+
const output = execSync(`lsof -a -p ${pid} -d cwd -Fn 2>/dev/null`, {
|
|
2454
|
+
encoding: "utf-8",
|
|
2455
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2456
|
+
}).trim();
|
|
2457
|
+
const lines = output.split("\n");
|
|
2458
|
+
for (const line of lines) {
|
|
2459
|
+
if (line.startsWith("n") && !line.startsWith("n ")) {
|
|
2460
|
+
return line.slice(1);
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
} else if (platform === "linux") {
|
|
2464
|
+
const output = execSync(`readlink /proc/${pid}/cwd 2>/dev/null`, {
|
|
2465
|
+
encoding: "utf-8",
|
|
2466
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2467
|
+
}).trim();
|
|
2468
|
+
if (output) return output;
|
|
2469
|
+
}
|
|
2470
|
+
} catch {
|
|
2471
|
+
}
|
|
2472
|
+
return void 0;
|
|
2473
|
+
}
|
|
2474
|
+
function isPortInUse(port) {
|
|
2475
|
+
const platform = process.platform;
|
|
2476
|
+
try {
|
|
2477
|
+
if (platform === "darwin" || platform === "linux") {
|
|
2478
|
+
execSync(`lsof -i :${port} -sTCP:LISTEN 2>/dev/null`, {
|
|
2479
|
+
encoding: "utf-8",
|
|
2480
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2481
|
+
});
|
|
2482
|
+
return true;
|
|
2483
|
+
}
|
|
2484
|
+
} catch {
|
|
2485
|
+
}
|
|
2486
|
+
return false;
|
|
2487
|
+
}
|
|
2488
|
+
function findAvailablePort(startPort, maxAttempts = 10) {
|
|
2489
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
2490
|
+
const port = startPort + i;
|
|
2491
|
+
if (!isPortInUse(port)) {
|
|
2492
|
+
return port;
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
return null;
|
|
2496
|
+
}
|
|
2497
|
+
function findOpenCodeProcesses() {
|
|
2498
|
+
const instances = [];
|
|
2499
|
+
try {
|
|
2500
|
+
const platform = process.platform;
|
|
2501
|
+
if (platform === "darwin" || platform === "linux") {
|
|
2502
|
+
let pids = [];
|
|
2503
|
+
try {
|
|
2504
|
+
const pgrepOutput = execSync('pgrep -f "opencode serve|opencode-serve"', {
|
|
2505
|
+
encoding: "utf-8",
|
|
2506
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2507
|
+
}).trim();
|
|
2508
|
+
if (pgrepOutput) {
|
|
2509
|
+
pids = pgrepOutput.split("\n").map((p) => parseInt(p.trim(), 10)).filter((p) => !isNaN(p));
|
|
2510
|
+
}
|
|
2511
|
+
} catch {
|
|
2512
|
+
try {
|
|
2513
|
+
const psOutput = execSync('ps aux | grep -E "opencode (serve|--port)" | grep -v grep', {
|
|
2514
|
+
encoding: "utf-8",
|
|
2515
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2516
|
+
}).trim();
|
|
2517
|
+
if (psOutput) {
|
|
2518
|
+
for (const line of psOutput.split("\n")) {
|
|
2519
|
+
const parts = line.trim().split(/\s+/);
|
|
2520
|
+
if (parts.length >= 2) {
|
|
2521
|
+
const pid = parseInt(parts[1], 10);
|
|
2522
|
+
if (!isNaN(pid)) pids.push(pid);
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
}
|
|
2526
|
+
} catch (err) {
|
|
2527
|
+
console.warn(
|
|
2528
|
+
`findOpenCodeProcesses: ps fallback failed: ${err instanceof Error ? err.message : String(err)}`
|
|
2529
|
+
);
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
for (const pid of pids) {
|
|
2533
|
+
try {
|
|
2534
|
+
const lsofOutput = execSync(`lsof -Pan -p ${pid} -i TCP -sTCP:LISTEN 2>/dev/null`, {
|
|
2535
|
+
encoding: "utf-8",
|
|
2536
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2537
|
+
}).trim();
|
|
2538
|
+
for (const line of lsofOutput.split("\n")) {
|
|
2539
|
+
const portMatch = line.match(/:(\d+)\s+\(LISTEN\)/);
|
|
2540
|
+
if (portMatch) {
|
|
2541
|
+
const port = parseInt(portMatch[1], 10);
|
|
2542
|
+
if (!isNaN(port) && !instances.some((i) => i.port === port)) {
|
|
2543
|
+
const cwd = getProcessCwd(pid);
|
|
2544
|
+
instances.push({ pid, port, cwd });
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
}
|
|
2548
|
+
} catch {
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
} catch (err) {
|
|
2553
|
+
console.warn(
|
|
2554
|
+
`findOpenCodeProcesses: process detection failed: ${err instanceof Error ? err.message : String(err)}`
|
|
2555
|
+
);
|
|
2556
|
+
}
|
|
2557
|
+
return instances;
|
|
2558
|
+
}
|
|
2559
|
+
async function scanPortsForOpenCode() {
|
|
2560
|
+
const instances = [];
|
|
2561
|
+
const checks = OPENCODE_PORT_RANGE.map(async (port) => {
|
|
2562
|
+
const health = await checkOpenCodeHealth(port);
|
|
2563
|
+
if (health.healthy) {
|
|
2564
|
+
let pid = 0;
|
|
2565
|
+
try {
|
|
2566
|
+
const lsofOutput = execSync(`lsof -ti :${port} -sTCP:LISTEN 2>/dev/null`, {
|
|
2567
|
+
encoding: "utf-8",
|
|
2568
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2569
|
+
}).trim();
|
|
2570
|
+
if (lsofOutput) {
|
|
2571
|
+
pid = parseInt(lsofOutput.split("\n")[0], 10) || 0;
|
|
2572
|
+
}
|
|
2573
|
+
} catch {
|
|
2574
|
+
}
|
|
2575
|
+
const cwd = pid ? getProcessCwd(pid) : void 0;
|
|
2576
|
+
return { pid, port, cwd, version: health.version };
|
|
2577
|
+
}
|
|
2578
|
+
return null;
|
|
2579
|
+
});
|
|
2580
|
+
const results = await Promise.all(checks);
|
|
2581
|
+
for (const result of results) {
|
|
2582
|
+
if (result) {
|
|
2583
|
+
instances.push(result);
|
|
2584
|
+
}
|
|
2585
|
+
}
|
|
2586
|
+
return instances;
|
|
2587
|
+
}
|
|
2588
|
+
async function findHealthyOpenCodeInstances() {
|
|
2589
|
+
const processes = findOpenCodeProcesses();
|
|
2590
|
+
const healthy = [];
|
|
2591
|
+
for (const proc of processes) {
|
|
2592
|
+
const health = await checkOpenCodeHealth(proc.port);
|
|
2593
|
+
if (health.healthy) {
|
|
2594
|
+
healthy.push({ ...proc, version: health.version });
|
|
2595
|
+
}
|
|
2596
|
+
}
|
|
2597
|
+
if (healthy.length === 0) {
|
|
2598
|
+
const scanned = await scanPortsForOpenCode();
|
|
2599
|
+
return scanned;
|
|
2600
|
+
}
|
|
2601
|
+
return healthy;
|
|
2602
|
+
}
|
|
2603
|
+
async function startOpenCode(port, options = {}) {
|
|
2604
|
+
let command = "opencode";
|
|
2605
|
+
const printLogs = options.inheritStdio ? ["--print-logs"] : [];
|
|
2606
|
+
let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
|
|
2607
|
+
try {
|
|
2608
|
+
execSync("which opencode", { stdio: "ignore" });
|
|
2609
|
+
} catch {
|
|
2610
|
+
command = "npx";
|
|
2611
|
+
args = [
|
|
2612
|
+
"opencode",
|
|
2613
|
+
"serve",
|
|
2614
|
+
"--port",
|
|
2615
|
+
port.toString(),
|
|
2616
|
+
"--hostname",
|
|
2617
|
+
"127.0.0.1",
|
|
2618
|
+
...printLogs
|
|
2619
|
+
];
|
|
2620
|
+
}
|
|
2621
|
+
const child = spawn3(command, args, {
|
|
2622
|
+
detached: true,
|
|
2623
|
+
stdio: options.inheritStdio ? "inherit" : "ignore",
|
|
2624
|
+
cwd: process.cwd()
|
|
2625
|
+
});
|
|
2626
|
+
return child;
|
|
2627
|
+
}
|
|
2628
|
+
function stopOpenCodeAndWait(opencodeProcess, timeoutMs) {
|
|
2629
|
+
const sendSignal = (signal) => {
|
|
2630
|
+
if (process.platform === "win32") {
|
|
2631
|
+
opencodeProcess.kill(signal);
|
|
2632
|
+
} else {
|
|
2633
|
+
process.kill(-opencodeProcess.pid, signal);
|
|
2634
|
+
}
|
|
2635
|
+
};
|
|
2636
|
+
return stopProcessAndWait(
|
|
2637
|
+
opencodeProcess,
|
|
2638
|
+
timeoutMs,
|
|
2639
|
+
() => sendSignal("SIGTERM"),
|
|
2640
|
+
() => sendSignal("SIGKILL")
|
|
2641
|
+
);
|
|
2642
|
+
}
|
|
1809
2643
|
|
|
1810
2644
|
// src/lib/opencode/install.ts
|
|
1811
2645
|
import { execSync as execSync2 } from "child_process";
|
|
@@ -2091,6 +2925,7 @@ async function createOpenCodeSession(port, directory) {
|
|
|
2091
2925
|
return data.id;
|
|
2092
2926
|
}
|
|
2093
2927
|
async function getModelAttachmentCapability(port, model) {
|
|
2928
|
+
const { model: baseModel } = splitModelVariant(model);
|
|
2094
2929
|
try {
|
|
2095
2930
|
const res = await timedFetch(`${opencodeBase(port)}/config/providers`);
|
|
2096
2931
|
if (!res.ok) {
|
|
@@ -2107,9 +2942,9 @@ async function getModelAttachmentCapability(port, model) {
|
|
|
2107
2942
|
);
|
|
2108
2943
|
return null;
|
|
2109
2944
|
}
|
|
2110
|
-
const slash =
|
|
2111
|
-
const providerId = slash > 0 ?
|
|
2112
|
-
let modelId = slash > 0 ?
|
|
2945
|
+
const slash = baseModel ? baseModel.indexOf("/") : -1;
|
|
2946
|
+
const providerId = slash > 0 ? baseModel.slice(0, slash) : void 0;
|
|
2947
|
+
let modelId = slash > 0 ? baseModel.slice(slash + 1) : void 0;
|
|
2113
2948
|
const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
|
|
2114
2949
|
let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
|
|
2115
2950
|
if (!provider && !providerId) {
|
|
@@ -2189,6 +3024,29 @@ async function buildFileParts(attachments, capable) {
|
|
|
2189
3024
|
}
|
|
2190
3025
|
return { parts, outcomes, capabilityUnknown };
|
|
2191
3026
|
}
|
|
3027
|
+
function splitModelVariant(raw) {
|
|
3028
|
+
const value = raw?.trim();
|
|
3029
|
+
if (!value) return {};
|
|
3030
|
+
const hashIndex = value.indexOf("#");
|
|
3031
|
+
if (hashIndex === -1) return { model: value };
|
|
3032
|
+
const model = value.slice(0, hashIndex).trim() || void 0;
|
|
3033
|
+
const variant = value.slice(hashIndex + 1).trim() || void 0;
|
|
3034
|
+
return { model, variant };
|
|
3035
|
+
}
|
|
3036
|
+
function applyModelOptions(body, options) {
|
|
3037
|
+
if (options?.agent) body.agent = options.agent;
|
|
3038
|
+
const { model, variant } = splitModelVariant(options?.model);
|
|
3039
|
+
if (model) {
|
|
3040
|
+
const slashIndex = model.indexOf("/");
|
|
3041
|
+
if (slashIndex !== -1) {
|
|
3042
|
+
body.model = {
|
|
3043
|
+
providerID: model.substring(0, slashIndex),
|
|
3044
|
+
modelID: model.substring(slashIndex + 1)
|
|
3045
|
+
};
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
if (variant) body.variant = variant;
|
|
3049
|
+
}
|
|
2192
3050
|
function messageText(m) {
|
|
2193
3051
|
if (!m || !Array.isArray(m.parts)) return "";
|
|
2194
3052
|
return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
@@ -2210,21 +3068,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2210
3068
|
parts.push(...fileParts);
|
|
2211
3069
|
if (attachments.onOutcomes) pendingOutcomes = { outcomes, capabilityUnknown };
|
|
2212
3070
|
}
|
|
2213
|
-
const body = {
|
|
2214
|
-
parts
|
|
2215
|
-
};
|
|
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
|
-
}
|
|
3071
|
+
const body = {
|
|
3072
|
+
parts
|
|
3073
|
+
};
|
|
3074
|
+
applyModelOptions(body, options);
|
|
2228
3075
|
const res = await timedFetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
|
|
2229
3076
|
method: "POST",
|
|
2230
3077
|
headers: { "Content-Type": "application/json" },
|
|
@@ -2232,7 +3079,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2232
3079
|
});
|
|
2233
3080
|
if (res.status < 200 || res.status >= 300) {
|
|
2234
3081
|
const text = await res.text().catch(() => "");
|
|
2235
|
-
|
|
3082
|
+
const { variant } = splitModelVariant(options?.model);
|
|
3083
|
+
throw new Error(
|
|
3084
|
+
`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}${variant ? ` (variant: ${variant})` : ""}`
|
|
3085
|
+
);
|
|
2236
3086
|
}
|
|
2237
3087
|
const READ_BACK_ATTEMPTS = 5;
|
|
2238
3088
|
const READ_BACK_DELAY_MS = 150;
|
|
@@ -2256,7 +3106,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2256
3106
|
}
|
|
2257
3107
|
}
|
|
2258
3108
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
2259
|
-
await new Promise((
|
|
3109
|
+
await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
|
|
2260
3110
|
}
|
|
2261
3111
|
}
|
|
2262
3112
|
return null;
|
|
@@ -2387,7 +3237,7 @@ function isPreamblePinnedRunning(messages, userMessageId) {
|
|
|
2387
3237
|
return completedOf(reply) != null && finishOf(reply) === "tool-calls";
|
|
2388
3238
|
}
|
|
2389
3239
|
function isB2AbandonmentConfirmed(params) {
|
|
2390
|
-
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
|
|
3240
|
+
return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false && params.rootOngoing === false;
|
|
2391
3241
|
}
|
|
2392
3242
|
function isAmbiguousTerminalFinish(m) {
|
|
2393
3243
|
if (completedOf(m) == null) return false;
|
|
@@ -2400,7 +3250,7 @@ function isAmbiguousFinishPinnedRunning(messages, userMessageId) {
|
|
|
2400
3250
|
return isAmbiguousTerminalFinish(reply);
|
|
2401
3251
|
}
|
|
2402
3252
|
function isAmbiguousFinishResolved(params) {
|
|
2403
|
-
return params.sessionOngoing === false || params.pinnedForMs >= params.maxPinnedMs;
|
|
3253
|
+
return params.sessionOngoing === false || params.sessionOngoing !== true && params.pinnedForMs >= params.maxPinnedMs;
|
|
2404
3254
|
}
|
|
2405
3255
|
function messageError(messages, userMessageId) {
|
|
2406
3256
|
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
@@ -2609,13 +3459,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
|
2609
3459
|
}
|
|
2610
3460
|
|
|
2611
3461
|
// src/lib/opencode/session-db-size.ts
|
|
2612
|
-
import { statSync as
|
|
2613
|
-
import { join as
|
|
3462
|
+
import { statSync as statSync3 } from "fs";
|
|
3463
|
+
import { join as join4 } from "path";
|
|
2614
3464
|
var LARGE_DB_THRESHOLD_BYTES = 268435456;
|
|
2615
3465
|
function statSessionDbBytes(homeDir) {
|
|
2616
|
-
const dbPath =
|
|
3466
|
+
const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
|
|
2617
3467
|
try {
|
|
2618
|
-
return
|
|
3468
|
+
return statSync3(dbPath).size;
|
|
2619
3469
|
} catch (err) {
|
|
2620
3470
|
const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
|
|
2621
3471
|
if (!isMissingFile) {
|
|
@@ -2641,11 +3491,11 @@ function buildSessionStoreSizeWarning(input) {
|
|
|
2641
3491
|
}
|
|
2642
3492
|
|
|
2643
3493
|
// src/lib/opencode/session-db-reclaim.ts
|
|
2644
|
-
import { statSync as
|
|
2645
|
-
import { dirname as
|
|
3494
|
+
import { statSync as statSync4, statfsSync } from "fs";
|
|
3495
|
+
import { dirname as dirname4 } from "path";
|
|
2646
3496
|
function insufficientSpaceReason(dbPath, requiredBytes) {
|
|
2647
3497
|
try {
|
|
2648
|
-
const fsStats = statfsSync(
|
|
3498
|
+
const fsStats = statfsSync(dirname4(dbPath));
|
|
2649
3499
|
const availableBytes = fsStats.bavail * fsStats.bsize;
|
|
2650
3500
|
if (availableBytes < requiredBytes) {
|
|
2651
3501
|
return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
|
|
@@ -2714,7 +3564,7 @@ async function reclaimSessionDbSpace(input) {
|
|
|
2714
3564
|
);
|
|
2715
3565
|
return { ok: false, skipped: "full-vacuum-blocked" };
|
|
2716
3566
|
}
|
|
2717
|
-
const fileBytesForGuard =
|
|
3567
|
+
const fileBytesForGuard = statSync4(dbPath).size;
|
|
2718
3568
|
const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
|
|
2719
3569
|
if (skipReason !== null) {
|
|
2720
3570
|
console.warn(
|
|
@@ -2786,7 +3636,6 @@ var StreamForwarder = class {
|
|
|
2786
3636
|
handleFrame(frame) {
|
|
2787
3637
|
switch (frame.type) {
|
|
2788
3638
|
case "open":
|
|
2789
|
-
this.callbacks.onOpen?.(frame.sid, frame.method, frame.path);
|
|
2790
3639
|
void this.handleOpen(frame);
|
|
2791
3640
|
break;
|
|
2792
3641
|
case "req_data":
|
|
@@ -2822,12 +3671,21 @@ var StreamForwarder = class {
|
|
|
2822
3671
|
const { sid, method, path, headers, has_body } = frame;
|
|
2823
3672
|
const correlationId = headers?.[CORRELATION_ID_HEADER];
|
|
2824
3673
|
const startedAt = Date.now();
|
|
3674
|
+
if (path !== TUNNEL_DRAIN_PING_PATH && path !== TUNNEL_USAGE_REARM_PING_PATH) {
|
|
3675
|
+
this.callbacks.onOpen?.(sid, method, path);
|
|
3676
|
+
}
|
|
2825
3677
|
if (path === TUNNEL_DRAIN_PING_PATH) {
|
|
2826
3678
|
this.callbacks.onDrainPing?.();
|
|
2827
3679
|
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
2828
3680
|
this.send({ type: "res_end", sid });
|
|
2829
3681
|
return;
|
|
2830
3682
|
}
|
|
3683
|
+
if (path === TUNNEL_USAGE_REARM_PING_PATH) {
|
|
3684
|
+
this.callbacks.onUsageRearmPing?.();
|
|
3685
|
+
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
3686
|
+
this.send({ type: "res_end", sid });
|
|
3687
|
+
return;
|
|
3688
|
+
}
|
|
2831
3689
|
if (process.env.DEBUG) {
|
|
2832
3690
|
log("debug", "agent_request", {
|
|
2833
3691
|
correlation_id: correlationId,
|
|
@@ -2842,12 +3700,12 @@ var StreamForwarder = class {
|
|
|
2842
3700
|
let endBody;
|
|
2843
3701
|
if (has_body) {
|
|
2844
3702
|
const chunks = [];
|
|
2845
|
-
bodyPromise = new Promise((
|
|
3703
|
+
bodyPromise = new Promise((resolve4) => {
|
|
2846
3704
|
pushBody = (buf) => {
|
|
2847
3705
|
chunks.push(buf);
|
|
2848
3706
|
};
|
|
2849
3707
|
endBody = () => {
|
|
2850
|
-
|
|
3708
|
+
resolve4(Buffer.concat(chunks));
|
|
2851
3709
|
};
|
|
2852
3710
|
});
|
|
2853
3711
|
}
|
|
@@ -2972,11 +3830,12 @@ function connectTunnel(options) {
|
|
|
2972
3830
|
onResponse,
|
|
2973
3831
|
onInfo,
|
|
2974
3832
|
onWarning,
|
|
2975
|
-
onDrainPing
|
|
3833
|
+
onDrainPing,
|
|
3834
|
+
onUsageRearmPing
|
|
2976
3835
|
} = options;
|
|
2977
3836
|
const tunnelUrl = getTunnelUrlConfig();
|
|
2978
3837
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
2979
|
-
return new Promise((
|
|
3838
|
+
return new Promise((resolve4, reject) => {
|
|
2980
3839
|
const ws = new WebSocket2(url, {
|
|
2981
3840
|
headers: {
|
|
2982
3841
|
Authorization: authHeader
|
|
@@ -2984,7 +3843,8 @@ function connectTunnel(options) {
|
|
|
2984
3843
|
});
|
|
2985
3844
|
const forwarder = new StreamForwarder(ws, port, {
|
|
2986
3845
|
onHead: () => onResponse?.(),
|
|
2987
|
-
onDrainPing: () => onDrainPing?.()
|
|
3846
|
+
onDrainPing: () => onDrainPing?.(),
|
|
3847
|
+
onUsageRearmPing: () => onUsageRearmPing?.()
|
|
2988
3848
|
});
|
|
2989
3849
|
const connectionTimeout = setTimeout(() => {
|
|
2990
3850
|
ws.close();
|
|
@@ -3027,8 +3887,8 @@ function connectTunnel(options) {
|
|
|
3027
3887
|
try {
|
|
3028
3888
|
message = JSON.parse(data.toString());
|
|
3029
3889
|
} catch (error2) {
|
|
3030
|
-
const
|
|
3031
|
-
onError?.(`Failed to handle message: ${
|
|
3890
|
+
const errorMessage2 = error2 instanceof Error ? error2.message : "Unknown error";
|
|
3891
|
+
onError?.(`Failed to handle message: ${errorMessage2}`);
|
|
3032
3892
|
return;
|
|
3033
3893
|
}
|
|
3034
3894
|
if (isStreamFrame(message)) {
|
|
@@ -3040,7 +3900,7 @@ function connectTunnel(options) {
|
|
|
3040
3900
|
clearTimeout(connectionTimeout);
|
|
3041
3901
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
3042
3902
|
onConnected?.(connectedAgentId);
|
|
3043
|
-
|
|
3903
|
+
resolve4({
|
|
3044
3904
|
ws,
|
|
3045
3905
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
3046
3906
|
});
|
|
@@ -3145,6 +4005,7 @@ var RunnerConnection = class {
|
|
|
3145
4005
|
onError: (error2) => events.onError?.(error2),
|
|
3146
4006
|
onResponse: () => events.onResponse?.(),
|
|
3147
4007
|
onDrainPing: () => events.onDrainPing?.(),
|
|
4008
|
+
onUsageRearmPing: () => events.onUsageRearmPing?.(),
|
|
3148
4009
|
onInfo: (message) => events.onInfo?.(message),
|
|
3149
4010
|
onWarning: (message) => events.onWarning?.(message)
|
|
3150
4011
|
});
|
|
@@ -3171,10 +4032,10 @@ var RunnerConnection = class {
|
|
|
3171
4032
|
};
|
|
3172
4033
|
|
|
3173
4034
|
// src/lib/tunnel/ready-marker.ts
|
|
3174
|
-
import { writeFileSync } from "fs";
|
|
4035
|
+
import { writeFileSync as writeFileSync3 } from "fs";
|
|
3175
4036
|
function writeTunnelReadyMarker(path, agentId) {
|
|
3176
4037
|
try {
|
|
3177
|
-
|
|
4038
|
+
writeFileSync3(path, `${agentId}
|
|
3178
4039
|
`);
|
|
3179
4040
|
return { ok: true };
|
|
3180
4041
|
} catch (error2) {
|
|
@@ -3182,10 +4043,52 @@ function writeTunnelReadyMarker(path, agentId) {
|
|
|
3182
4043
|
}
|
|
3183
4044
|
}
|
|
3184
4045
|
|
|
4046
|
+
// src/lib/replication.ts
|
|
4047
|
+
import { spawn as spawn4 } from "child_process";
|
|
4048
|
+
function startSessionDbReplication(configPath) {
|
|
4049
|
+
return spawn4("litestream", ["replicate", "-config", configPath], {
|
|
4050
|
+
stdio: "inherit"
|
|
4051
|
+
});
|
|
4052
|
+
}
|
|
4053
|
+
async function stopSessionDbReplication(child, timeoutMs) {
|
|
4054
|
+
return stopProcessAndWait(
|
|
4055
|
+
child,
|
|
4056
|
+
timeoutMs,
|
|
4057
|
+
() => child.kill("SIGTERM"),
|
|
4058
|
+
() => child.kill("SIGKILL")
|
|
4059
|
+
);
|
|
4060
|
+
}
|
|
4061
|
+
|
|
4062
|
+
// src/lib/process-liveness.ts
|
|
4063
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
4064
|
+
function isProcessAlive(pid) {
|
|
4065
|
+
try {
|
|
4066
|
+
process.kill(pid, 0);
|
|
4067
|
+
} catch (error2) {
|
|
4068
|
+
const code = error2.code;
|
|
4069
|
+
if (code === "ESRCH") return false;
|
|
4070
|
+
if (code === "EPERM") return true;
|
|
4071
|
+
console.error(
|
|
4072
|
+
`[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
4073
|
+
);
|
|
4074
|
+
return false;
|
|
4075
|
+
}
|
|
4076
|
+
if (process.platform !== "linux") return true;
|
|
4077
|
+
try {
|
|
4078
|
+
const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
|
|
4079
|
+
return !/^State:\s+Z(?:\s|$)/m.test(status2);
|
|
4080
|
+
} catch (error2) {
|
|
4081
|
+
console.error(
|
|
4082
|
+
`[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
4083
|
+
);
|
|
4084
|
+
return true;
|
|
4085
|
+
}
|
|
4086
|
+
}
|
|
4087
|
+
|
|
3185
4088
|
// src/lib/openai-usage.ts
|
|
3186
|
-
import { readFileSync as
|
|
3187
|
-
import { homedir as
|
|
3188
|
-
import { join as
|
|
4089
|
+
import { readFileSync as readFileSync5 } from "fs";
|
|
4090
|
+
import { homedir as homedir3 } from "os";
|
|
4091
|
+
import { join as join5 } from "path";
|
|
3189
4092
|
var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
3190
4093
|
var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
|
|
3191
4094
|
var OpenAiUsageError = class extends Error {
|
|
@@ -3199,7 +4102,7 @@ function isLocalCredentialProblem2(err) {
|
|
|
3199
4102
|
}
|
|
3200
4103
|
function readOpenCodeChatGptCredentials() {
|
|
3201
4104
|
try {
|
|
3202
|
-
const raw =
|
|
4105
|
+
const raw = readFileSync5(join5(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
|
|
3203
4106
|
let parsed;
|
|
3204
4107
|
try {
|
|
3205
4108
|
parsed = JSON.parse(raw);
|
|
@@ -3221,6 +4124,23 @@ function readOpenCodeChatGptCredentials() {
|
|
|
3221
4124
|
return null;
|
|
3222
4125
|
}
|
|
3223
4126
|
}
|
|
4127
|
+
function parseChatGptIdentity(accessToken) {
|
|
4128
|
+
const segments = accessToken.split(".");
|
|
4129
|
+
if (segments.length !== 3) return null;
|
|
4130
|
+
let payload;
|
|
4131
|
+
try {
|
|
4132
|
+
const parsed = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
|
|
4133
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
4134
|
+
payload = parsed;
|
|
4135
|
+
} catch {
|
|
4136
|
+
return null;
|
|
4137
|
+
}
|
|
4138
|
+
const profile = payload["https://api.openai.com/profile"];
|
|
4139
|
+
const auth = payload["https://api.openai.com/auth"];
|
|
4140
|
+
const ownerEmail = typeof profile?.email === "string" ? profile.email.trim() || null : null;
|
|
4141
|
+
const planType = typeof auth?.chatgpt_plan_type === "string" ? auth.chatgpt_plan_type.trim() || null : null;
|
|
4142
|
+
return ownerEmail === null && planType === null ? null : { ownerEmail, planType };
|
|
4143
|
+
}
|
|
3224
4144
|
function toWindow2(headers, name) {
|
|
3225
4145
|
const utilizationHeader = headers.get(`x-codex-${name}-used-percent`);
|
|
3226
4146
|
const windowMinutesHeader = headers.get(`x-codex-${name}-window-minutes`);
|
|
@@ -3296,6 +4216,7 @@ async function getOpenAiUsage(port) {
|
|
|
3296
4216
|
"credentials_expired"
|
|
3297
4217
|
);
|
|
3298
4218
|
}
|
|
4219
|
+
const subscription = parseChatGptIdentity(credentials2.accessToken);
|
|
3299
4220
|
const models = await resolveProbeModels(port);
|
|
3300
4221
|
if (models.length === 0) {
|
|
3301
4222
|
throw new OpenAiUsageError("No supported OpenAI probe model is available.", "no_probe_model");
|
|
@@ -3328,7 +4249,7 @@ async function getOpenAiUsage(port) {
|
|
|
3328
4249
|
"no_usable_window"
|
|
3329
4250
|
);
|
|
3330
4251
|
}
|
|
3331
|
-
return usage;
|
|
4252
|
+
return { ...usage, subscription };
|
|
3332
4253
|
}
|
|
3333
4254
|
if (res.status === 401) {
|
|
3334
4255
|
throw new OpenAiUsageError(
|
|
@@ -3529,58 +4450,97 @@ function readDisk(homeDir) {
|
|
|
3529
4450
|
};
|
|
3530
4451
|
}
|
|
3531
4452
|
}
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
4453
|
+
var CPU_PEAK_WINDOW_MS = 6e4;
|
|
4454
|
+
var CPU_PEAK_WINDOW_SAMPLE_COUNT = 4;
|
|
4455
|
+
var CPU_PEAK_SAMPLE_INTERVAL_MS = CPU_PEAK_WINDOW_MS / CPU_PEAK_WINDOW_SAMPLE_COUNT;
|
|
4456
|
+
function createCpuPeakSampler() {
|
|
4457
|
+
const sampleHistory = new Array(CPU_PEAK_WINDOW_SAMPLE_COUNT);
|
|
4458
|
+
sampleHistory[0] = readCpuSample();
|
|
4459
|
+
let nextSampleIndex = 1;
|
|
4460
|
+
let sampleCount = 1;
|
|
4461
|
+
let peak = null;
|
|
4462
|
+
const timer = setInterval(() => {
|
|
3535
4463
|
const current = readCpuSample();
|
|
3536
|
-
const
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
const warnings = [];
|
|
3543
|
-
if (disk.warning) warnings.push(disk.warning);
|
|
3544
|
-
if (ecsWarning) warnings.push(ecsWarning);
|
|
3545
|
-
let cpuPercent = hostCpuPercent;
|
|
3546
|
-
let cpuCount = hostCpuCount;
|
|
3547
|
-
let memoryTotalBytes = totalmem();
|
|
3548
|
-
let memoryAvailableBytes = freemem();
|
|
3549
|
-
if (limits !== null) {
|
|
3550
|
-
cpuCount = limits.cpuCount;
|
|
3551
|
-
memoryTotalBytes = limits.memoryTotalBytes;
|
|
3552
|
-
memoryAvailableBytes = clamp(
|
|
3553
|
-
limits.memoryTotalBytes - (totalmem() - freemem()),
|
|
3554
|
-
0,
|
|
3555
|
-
limits.memoryTotalBytes
|
|
3556
|
-
);
|
|
3557
|
-
cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4464
|
+
const sampleFromWindowAgo = sampleCount === CPU_PEAK_WINDOW_SAMPLE_COUNT ? sampleHistory[nextSampleIndex] : void 0;
|
|
4465
|
+
if (sampleFromWindowAgo !== void 0) {
|
|
4466
|
+
const percentage = cpuPercentBetween(sampleFromWindowAgo, current);
|
|
4467
|
+
if (percentage !== null) {
|
|
4468
|
+
peak = peak === null ? percentage : Math.max(peak, percentage);
|
|
4469
|
+
}
|
|
3558
4470
|
}
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
4471
|
+
sampleHistory[nextSampleIndex] = current;
|
|
4472
|
+
nextSampleIndex = (nextSampleIndex + 1) % CPU_PEAK_WINDOW_SAMPLE_COUNT;
|
|
4473
|
+
sampleCount = Math.min(sampleCount + 1, CPU_PEAK_WINDOW_SAMPLE_COUNT);
|
|
4474
|
+
}, CPU_PEAK_SAMPLE_INTERVAL_MS);
|
|
4475
|
+
return {
|
|
4476
|
+
takeAndReset: () => {
|
|
4477
|
+
const currentPeak = peak;
|
|
4478
|
+
peak = null;
|
|
4479
|
+
return currentPeak;
|
|
4480
|
+
},
|
|
4481
|
+
stop: () => clearInterval(timer)
|
|
4482
|
+
};
|
|
4483
|
+
}
|
|
4484
|
+
function createResourceUsageCollector(homeDir) {
|
|
4485
|
+
let previous = readCpuSample();
|
|
4486
|
+
const cpuPeakSampler = createCpuPeakSampler();
|
|
4487
|
+
return {
|
|
4488
|
+
collect: async () => {
|
|
4489
|
+
const current = readCpuSample();
|
|
4490
|
+
const hostCpuPercent = cpuPercentBetween(previous, current);
|
|
4491
|
+
const hostCpuPeakPercent = cpuPeakSampler.takeAndReset();
|
|
4492
|
+
const hostCpuCount = cpus().length;
|
|
4493
|
+
previous = current;
|
|
4494
|
+
const disk = readDisk(homeDir);
|
|
4495
|
+
const opencodeDbBytes = statSessionDbBytes(homeDir);
|
|
4496
|
+
const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
|
|
4497
|
+
const warnings = [];
|
|
4498
|
+
if (disk.warning) warnings.push(disk.warning);
|
|
4499
|
+
if (ecsWarning) warnings.push(ecsWarning);
|
|
4500
|
+
let cpuPercent = hostCpuPercent;
|
|
4501
|
+
let cpuPeakPercent = hostCpuPeakPercent;
|
|
4502
|
+
let cpuCount = hostCpuCount;
|
|
4503
|
+
let memoryTotalBytes = totalmem();
|
|
4504
|
+
let memoryAvailableBytes = freemem();
|
|
4505
|
+
if (limits !== null) {
|
|
4506
|
+
cpuCount = limits.cpuCount;
|
|
4507
|
+
memoryTotalBytes = limits.memoryTotalBytes;
|
|
4508
|
+
memoryAvailableBytes = clamp(
|
|
4509
|
+
limits.memoryTotalBytes - (totalmem() - freemem()),
|
|
4510
|
+
0,
|
|
4511
|
+
limits.memoryTotalBytes
|
|
4512
|
+
);
|
|
4513
|
+
cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4514
|
+
cpuPeakPercent = hostCpuPeakPercent === null ? null : clamp(round2(hostCpuPeakPercent * hostCpuCount / limits.cpuCount), 0, 100);
|
|
4515
|
+
}
|
|
4516
|
+
return {
|
|
4517
|
+
usage: {
|
|
4518
|
+
cpuPercent,
|
|
4519
|
+
cpuPeakPercent,
|
|
4520
|
+
cpuCount,
|
|
4521
|
+
memoryTotalBytes,
|
|
4522
|
+
memoryAvailableBytes,
|
|
4523
|
+
diskTotalBytes: disk.totalBytes,
|
|
4524
|
+
diskFreeBytes: disk.freeBytes,
|
|
4525
|
+
opencodeDbBytes
|
|
4526
|
+
},
|
|
4527
|
+
warnings
|
|
4528
|
+
};
|
|
4529
|
+
},
|
|
4530
|
+
stop: cpuPeakSampler.stop
|
|
3571
4531
|
};
|
|
3572
4532
|
}
|
|
3573
4533
|
|
|
3574
4534
|
// src/lib/channels/driver.ts
|
|
3575
|
-
import { homedir as
|
|
4535
|
+
import { homedir as homedir4 } from "os";
|
|
3576
4536
|
|
|
3577
4537
|
// src/lib/runner-file-sync.ts
|
|
3578
|
-
import { join as
|
|
4538
|
+
import { join as join7 } from "path";
|
|
3579
4539
|
|
|
3580
4540
|
// src/lib/file-push.ts
|
|
3581
4541
|
import { randomUUID } from "crypto";
|
|
3582
4542
|
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
3583
|
-
import { basename, dirname as
|
|
4543
|
+
import { basename, dirname as dirname5, isAbsolute, join as join6, relative, resolve as resolve2, sep } from "path";
|
|
3584
4544
|
var FILE_MODE = 384;
|
|
3585
4545
|
var DIRECTORY_MODE = 448;
|
|
3586
4546
|
async function writePushedFile(request) {
|
|
@@ -3611,9 +4571,9 @@ async function writePushedFile(request) {
|
|
|
3611
4571
|
}
|
|
3612
4572
|
try {
|
|
3613
4573
|
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
3614
|
-
|
|
4574
|
+
dirname5(candidate)
|
|
3615
4575
|
);
|
|
3616
|
-
const realTarget =
|
|
4576
|
+
const realTarget = join6(existingAncestor, ...missingSegments, basename(candidate));
|
|
3617
4577
|
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
3618
4578
|
if (allowedDirectory === null) {
|
|
3619
4579
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
@@ -3623,8 +4583,8 @@ async function writePushedFile(request) {
|
|
|
3623
4583
|
}
|
|
3624
4584
|
if (missingSegments.length > 0) {
|
|
3625
4585
|
await createMissingDirectories(existingAncestor, missingSegments);
|
|
3626
|
-
const realParent = await realpath(
|
|
3627
|
-
if (realParent !==
|
|
4586
|
+
const realParent = await realpath(dirname5(realTarget));
|
|
4587
|
+
if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
3628
4588
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
3629
4589
|
path: realTarget,
|
|
3630
4590
|
bytes,
|
|
@@ -3649,7 +4609,7 @@ function expandAndValidate(requestedPath, homeDir) {
|
|
|
3649
4609
|
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
3650
4610
|
return null;
|
|
3651
4611
|
}
|
|
3652
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4612
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
3653
4613
|
if (expanded.split(/[/\\]/).includes("..")) {
|
|
3654
4614
|
return null;
|
|
3655
4615
|
}
|
|
@@ -3667,7 +4627,7 @@ async function resolveNearestExistingAncestor(directory) {
|
|
|
3667
4627
|
try {
|
|
3668
4628
|
return { existingAncestor: await realpath(current), missingSegments };
|
|
3669
4629
|
} catch (err) {
|
|
3670
|
-
const parent =
|
|
4630
|
+
const parent = dirname5(current);
|
|
3671
4631
|
if (err.code !== "ENOENT" || parent === current) {
|
|
3672
4632
|
throw err;
|
|
3673
4633
|
}
|
|
@@ -3722,13 +4682,13 @@ function contains(realDirectory, realTarget) {
|
|
|
3722
4682
|
async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
3723
4683
|
let current = existingAncestor;
|
|
3724
4684
|
for (const segment of missingSegments) {
|
|
3725
|
-
current =
|
|
4685
|
+
current = join6(current, segment);
|
|
3726
4686
|
await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
|
|
3727
4687
|
await chmod(current, DIRECTORY_MODE);
|
|
3728
4688
|
}
|
|
3729
4689
|
}
|
|
3730
4690
|
async function writeAtomically(realTarget, content) {
|
|
3731
|
-
const temporaryPath =
|
|
4691
|
+
const temporaryPath = join6(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
3732
4692
|
let handle;
|
|
3733
4693
|
try {
|
|
3734
4694
|
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
@@ -3858,12 +4818,12 @@ var NOT_APPLIED = {
|
|
|
3858
4818
|
opencodeAuthApplied: false
|
|
3859
4819
|
};
|
|
3860
4820
|
function isClaudeCredentialPath(requestedPath, homeDir) {
|
|
3861
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
3862
|
-
return expanded ===
|
|
4821
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4822
|
+
return expanded === join7(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
|
|
3863
4823
|
}
|
|
3864
4824
|
function isOpenCodeAuthPath(requestedPath, homeDir) {
|
|
3865
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
3866
|
-
return expanded ===
|
|
4825
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4826
|
+
return expanded === join7(homeDir, ...OPENCODE_AUTH_SEGMENTS);
|
|
3867
4827
|
}
|
|
3868
4828
|
async function applyOne(options, file) {
|
|
3869
4829
|
const label = `${file.id.slice(0, 8)} (${file.path})`;
|
|
@@ -4390,6 +5350,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4390
5350
|
* and stops opencode.
|
|
4391
5351
|
*/
|
|
4392
5352
|
stopped = false;
|
|
5353
|
+
recycleRequestedFlag = false;
|
|
4393
5354
|
constructor(config) {
|
|
4394
5355
|
this.agentId = config.agentId;
|
|
4395
5356
|
this.port = config.port;
|
|
@@ -4409,7 +5370,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4409
5370
|
this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
4410
5371
|
this.now = config.now ?? (() => Date.now());
|
|
4411
5372
|
this.fileSyncDirectories = config.fileSyncDirectories ?? [];
|
|
4412
|
-
this.homeDir = config.homeDir ??
|
|
5373
|
+
this.homeDir = config.homeDir ?? homedir4();
|
|
4413
5374
|
this.maxActiveSessions = config.maxActiveSessions;
|
|
4414
5375
|
this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
|
|
4415
5376
|
this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
|
|
@@ -4495,6 +5456,9 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4495
5456
|
let dispatched = 0;
|
|
4496
5457
|
try {
|
|
4497
5458
|
const conversations = await this.getPendingConversations();
|
|
5459
|
+
if (this.recycleRequestedFlag) {
|
|
5460
|
+
this.stop();
|
|
5461
|
+
}
|
|
4498
5462
|
if (conversations.length > 0) {
|
|
4499
5463
|
const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
|
|
4500
5464
|
this.log({
|
|
@@ -4623,6 +5587,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4623
5587
|
stop() {
|
|
4624
5588
|
this.stopped = true;
|
|
4625
5589
|
}
|
|
5590
|
+
/**
|
|
5591
|
+
* The server clears this request when a new MicroVM identity is recorded, so a
|
|
5592
|
+
* same-VM tunnel reconnect does not consume it. This is a plain read rather
|
|
5593
|
+
* than a consume; `run.ts` guards the action once-only.
|
|
5594
|
+
*/
|
|
5595
|
+
get recycleRequested() {
|
|
5596
|
+
return this.recycleRequestedFlag;
|
|
5597
|
+
}
|
|
4626
5598
|
/**
|
|
4627
5599
|
* Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
|
|
4628
5600
|
* graceful shutdown, so a turn whose reply is ready — or completes within the
|
|
@@ -4760,7 +5732,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4760
5732
|
this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
|
|
4761
5733
|
break;
|
|
4762
5734
|
}
|
|
4763
|
-
const
|
|
5735
|
+
const errorMessage2 = err instanceof Error ? err.message : String(err);
|
|
4764
5736
|
this.sessions.delete(conv.id);
|
|
4765
5737
|
this.supersede(conv.id, sessionId);
|
|
4766
5738
|
this.log({
|
|
@@ -4769,7 +5741,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4769
5741
|
conversation_id: conv.id,
|
|
4770
5742
|
message_id: message.id
|
|
4771
5743
|
});
|
|
4772
|
-
await this.markFailed(conv.id, message.id, null,
|
|
5744
|
+
await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
|
|
4773
5745
|
this.log({
|
|
4774
5746
|
level: "warn",
|
|
4775
5747
|
message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
|
|
@@ -4780,7 +5752,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4780
5752
|
});
|
|
4781
5753
|
this.log({
|
|
4782
5754
|
level: "error",
|
|
4783
|
-
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${
|
|
5755
|
+
message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage2}`,
|
|
4784
5756
|
conversation_id: conv.id,
|
|
4785
5757
|
message_id: message.id
|
|
4786
5758
|
});
|
|
@@ -4801,14 +5773,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4801
5773
|
this.unconfirmedDispatchFailures.delete(message.id);
|
|
4802
5774
|
this.sessions.delete(conv.id);
|
|
4803
5775
|
this.supersede(conv.id, sessionId);
|
|
4804
|
-
const
|
|
5776
|
+
const errorMessage2 = `OpenCode accepted this message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
|
|
4805
5777
|
this.log({
|
|
4806
5778
|
level: "error",
|
|
4807
|
-
message:
|
|
5779
|
+
message: errorMessage2,
|
|
4808
5780
|
conversation_id: conv.id,
|
|
4809
5781
|
message_id: message.id
|
|
4810
5782
|
});
|
|
4811
|
-
await this.markFailed(conv.id, message.id, null,
|
|
5783
|
+
await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
|
|
4812
5784
|
this.log({
|
|
4813
5785
|
level: "warn",
|
|
4814
5786
|
message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
|
|
@@ -5676,6 +6648,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5676
6648
|
deliveryDeadlineAnchored: false,
|
|
5677
6649
|
b2PinnedSinceMs: 0,
|
|
5678
6650
|
b2LastDescendantCheckMs: 0,
|
|
6651
|
+
b2RootOngoingHeldLogged: false,
|
|
5679
6652
|
b2AbandonedSignalled: false,
|
|
5680
6653
|
ambiguousPinnedSinceMs: 0,
|
|
5681
6654
|
ambiguousResolved: false
|
|
@@ -5770,6 +6743,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
5770
6743
|
deliveryDeadlineAnchored: false,
|
|
5771
6744
|
b2PinnedSinceMs: 0,
|
|
5772
6745
|
b2LastDescendantCheckMs: 0,
|
|
6746
|
+
b2RootOngoingHeldLogged: false,
|
|
5773
6747
|
b2AbandonedSignalled: false,
|
|
5774
6748
|
ambiguousPinnedSinceMs: 0,
|
|
5775
6749
|
ambiguousResolved: false
|
|
@@ -6123,6 +7097,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6123
7097
|
if (snapshotReadable) {
|
|
6124
7098
|
inFlight.b2PinnedSinceMs = 0;
|
|
6125
7099
|
inFlight.b2LastDescendantCheckMs = 0;
|
|
7100
|
+
inFlight.b2RootOngoingHeldLogged = false;
|
|
6126
7101
|
inFlight.b2AbandonedSignalled = false;
|
|
6127
7102
|
}
|
|
6128
7103
|
} else {
|
|
@@ -6134,11 +7109,15 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6134
7109
|
const pinnedForMs = this.now() - inFlight.b2PinnedSinceMs;
|
|
6135
7110
|
if (pinnedForMs >= B2_ABANDONMENT_MIN_PINNED_MS && this.now() - inFlight.b2LastDescendantCheckMs >= B2_ABANDONMENT_RECHECK_MS) {
|
|
6136
7111
|
inFlight.b2LastDescendantCheckMs = this.now();
|
|
6137
|
-
const descendantOngoing = await
|
|
7112
|
+
const [descendantOngoing, rootOngoing] = await Promise.all([
|
|
7113
|
+
this.isAnyDescendantSessionOngoing(sessionId),
|
|
7114
|
+
isSessionOngoing(this.port, sessionId)
|
|
7115
|
+
]);
|
|
6138
7116
|
if (isB2AbandonmentConfirmed({
|
|
6139
7117
|
pinnedForMs,
|
|
6140
7118
|
minPinnedMs: B2_ABANDONMENT_MIN_PINNED_MS,
|
|
6141
|
-
descendantOngoing
|
|
7119
|
+
descendantOngoing,
|
|
7120
|
+
rootOngoing
|
|
6142
7121
|
})) {
|
|
6143
7122
|
inFlight.b2AbandonedSignalled = true;
|
|
6144
7123
|
this.log({
|
|
@@ -6147,12 +7126,26 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6147
7126
|
conversation_id: conv.id,
|
|
6148
7127
|
message_id: id
|
|
6149
7128
|
});
|
|
7129
|
+
const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
|
|
6150
7130
|
void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
|
|
6151
|
-
watched_for_ms: pinnedForMs
|
|
7131
|
+
watched_for_ms: pinnedForMs,
|
|
7132
|
+
finish: reply?.info?.finish ?? reply?.finish,
|
|
7133
|
+
...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
|
|
7134
|
+
...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
|
|
7135
|
+
opencode_message_id: inFlight.opencodeMessageId
|
|
6152
7136
|
});
|
|
6153
7137
|
await this.settleMessageDone(sessionId, watcher, inFlight, messages);
|
|
6154
7138
|
return;
|
|
6155
7139
|
}
|
|
7140
|
+
if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
|
|
7141
|
+
inFlight.b2RootOngoingHeldLogged = true;
|
|
7142
|
+
this.log({
|
|
7143
|
+
level: "warn",
|
|
7144
|
+
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`,
|
|
7145
|
+
conversation_id: conv.id,
|
|
7146
|
+
message_id: id
|
|
7147
|
+
});
|
|
7148
|
+
}
|
|
6156
7149
|
}
|
|
6157
7150
|
}
|
|
6158
7151
|
const ambiguousPinnedNow = activelyRunning && isAmbiguousFinishPinnedRunning(messages, inFlight.opencodeMessageId);
|
|
@@ -6751,14 +7744,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
6751
7744
|
this.unconfirmedDispatchFailures.delete(row.id);
|
|
6752
7745
|
this.sessions.delete(readoptConv.id);
|
|
6753
7746
|
this.supersede(readoptConv.id, sessionId);
|
|
6754
|
-
const
|
|
7747
|
+
const errorMessage2 = `OpenCode accepted this re-dispatched message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
|
|
6755
7748
|
this.log({
|
|
6756
7749
|
level: "error",
|
|
6757
|
-
message:
|
|
7750
|
+
message: errorMessage2,
|
|
6758
7751
|
conversation_id: row.conversation_id,
|
|
6759
7752
|
message_id: row.id
|
|
6760
7753
|
});
|
|
6761
|
-
await this.markFailed(row.conversation_id, row.id, null,
|
|
7754
|
+
await this.markFailed(row.conversation_id, row.id, null, errorMessage2).catch((markErr) => {
|
|
6762
7755
|
this.log({
|
|
6763
7756
|
level: "warn",
|
|
6764
7757
|
message: `markFailed PATCH for message ${row.id.slice(0, 8)} (conversation ${row.conversation_id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
|
|
@@ -7373,6 +8366,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
7373
8366
|
throw new Error(`Failed to get pending conversations: HTTP ${res.status}`);
|
|
7374
8367
|
}
|
|
7375
8368
|
const data = await res.json();
|
|
8369
|
+
this.recycleRequestedFlag = data.recycle_requested === true;
|
|
7376
8370
|
let conversations = data.conversations;
|
|
7377
8371
|
if (this.conversationFilter) {
|
|
7378
8372
|
conversations = conversations.filter((c) => c.id === this.conversationFilter);
|
|
@@ -7807,86 +8801,631 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
7807
8801
|
throw new Error("OpenCode is not installed");
|
|
7808
8802
|
}
|
|
7809
8803
|
}
|
|
7810
|
-
if (!ctx.interactive) {
|
|
7811
|
-
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
7812
|
-
const proc = await startOpenCode(ctx.port);
|
|
7813
|
-
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
7814
|
-
if (!health.healthy) {
|
|
7815
|
-
return {
|
|
7816
|
-
port: ctx.port,
|
|
7817
|
-
process: proc,
|
|
7818
|
-
version: null,
|
|
7819
|
-
notReadyReason: `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s`
|
|
8804
|
+
if (!ctx.interactive) {
|
|
8805
|
+
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
8806
|
+
const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
|
|
8807
|
+
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
8808
|
+
if (!health.healthy) {
|
|
8809
|
+
return {
|
|
8810
|
+
port: ctx.port,
|
|
8811
|
+
process: proc,
|
|
8812
|
+
version: null,
|
|
8813
|
+
notReadyReason: `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s`
|
|
8814
|
+
};
|
|
8815
|
+
}
|
|
8816
|
+
ctx.log(`OpenCode started on port ${ctx.port}${health.version ? ` (v${health.version})` : ""}`);
|
|
8817
|
+
return {
|
|
8818
|
+
port: ctx.port,
|
|
8819
|
+
process: proc,
|
|
8820
|
+
version: health.version ?? null,
|
|
8821
|
+
notReadyReason: null
|
|
8822
|
+
};
|
|
8823
|
+
}
|
|
8824
|
+
let port = ctx.port;
|
|
8825
|
+
if (isPortInUse(port)) {
|
|
8826
|
+
console.log(chalk5.yellow(`
|
|
8827
|
+
Port ${port} is already in use.`));
|
|
8828
|
+
const alternativePort = findAvailablePort(port + 1);
|
|
8829
|
+
if (alternativePort) {
|
|
8830
|
+
const useAlternative = await select2({
|
|
8831
|
+
message: `Use port ${alternativePort} instead?`,
|
|
8832
|
+
choices: [
|
|
8833
|
+
{ name: `Yes, use port ${alternativePort}`, value: "yes" },
|
|
8834
|
+
{ name: "No, I will free the port manually", value: "no" }
|
|
8835
|
+
]
|
|
8836
|
+
});
|
|
8837
|
+
if (useAlternative === "yes") {
|
|
8838
|
+
port = alternativePort;
|
|
8839
|
+
} else {
|
|
8840
|
+
throw new Error(`Port ${ctx.port} is in use`);
|
|
8841
|
+
}
|
|
8842
|
+
}
|
|
8843
|
+
}
|
|
8844
|
+
const action = await select2({
|
|
8845
|
+
message: "OpenCode is not running. What would you like to do?",
|
|
8846
|
+
choices: [
|
|
8847
|
+
{
|
|
8848
|
+
name: "Start OpenCode for me",
|
|
8849
|
+
value: "start",
|
|
8850
|
+
description: `Run 'opencode serve --port ${port}'`
|
|
8851
|
+
},
|
|
8852
|
+
{
|
|
8853
|
+
name: "Show me the command",
|
|
8854
|
+
value: "manual",
|
|
8855
|
+
description: "Display the command to run manually"
|
|
8856
|
+
},
|
|
8857
|
+
{
|
|
8858
|
+
name: "Continue without OpenCode",
|
|
8859
|
+
value: "continue",
|
|
8860
|
+
description: "Requests will fail until OpenCode starts"
|
|
8861
|
+
}
|
|
8862
|
+
]
|
|
8863
|
+
});
|
|
8864
|
+
if (action === "manual") {
|
|
8865
|
+
blank();
|
|
8866
|
+
console.log(chalk5.bold("Run this command in another terminal:"));
|
|
8867
|
+
blank();
|
|
8868
|
+
console.log(` ${chalk5.cyan(`opencode serve --port ${port}`)}`);
|
|
8869
|
+
blank();
|
|
8870
|
+
throw new Error("Please start OpenCode manually");
|
|
8871
|
+
}
|
|
8872
|
+
if (action === "start") {
|
|
8873
|
+
const spinner = ora2("Starting OpenCode...").start();
|
|
8874
|
+
const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
|
|
8875
|
+
const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
|
|
8876
|
+
if (!health.healthy) {
|
|
8877
|
+
spinner.fail("Failed to start OpenCode");
|
|
8878
|
+
throw new Error("OpenCode failed to start");
|
|
8879
|
+
}
|
|
8880
|
+
spinner.stop();
|
|
8881
|
+
return { port, process: proc, version: health.version ?? null, notReadyReason: null };
|
|
8882
|
+
}
|
|
8883
|
+
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
8884
|
+
}
|
|
8885
|
+
|
|
8886
|
+
// src/lib/runner-credentials.ts
|
|
8887
|
+
import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "fs";
|
|
8888
|
+
import { spawn as spawn5 } from "child_process";
|
|
8889
|
+
var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
|
|
8890
|
+
var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
|
|
8891
|
+
var GITHUB_PROBE_TIMEOUT_MS = 1e4;
|
|
8892
|
+
var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
|
|
8893
|
+
var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
|
|
8894
|
+
function commandError2(result) {
|
|
8895
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
8896
|
+
}
|
|
8897
|
+
var runCommand2 = (command, args, opts) => {
|
|
8898
|
+
return new Promise((resolve4) => {
|
|
8899
|
+
let child;
|
|
8900
|
+
let stdout = "";
|
|
8901
|
+
let stderr = "";
|
|
8902
|
+
let settled = false;
|
|
8903
|
+
const timer = {};
|
|
8904
|
+
const finish = (result) => {
|
|
8905
|
+
if (settled) return;
|
|
8906
|
+
settled = true;
|
|
8907
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
8908
|
+
resolve4(result);
|
|
8909
|
+
};
|
|
8910
|
+
try {
|
|
8911
|
+
child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
|
|
8912
|
+
} catch (error2) {
|
|
8913
|
+
finish({
|
|
8914
|
+
code: null,
|
|
8915
|
+
stdout,
|
|
8916
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
8917
|
+
timedOut: false
|
|
8918
|
+
});
|
|
8919
|
+
return;
|
|
8920
|
+
}
|
|
8921
|
+
child.stdout?.setEncoding("utf8");
|
|
8922
|
+
child.stdout?.on("data", (chunk) => {
|
|
8923
|
+
stdout += chunk;
|
|
8924
|
+
});
|
|
8925
|
+
child.stderr?.setEncoding("utf8");
|
|
8926
|
+
child.stderr?.on("data", (chunk) => {
|
|
8927
|
+
stderr += chunk;
|
|
8928
|
+
});
|
|
8929
|
+
child.once("error", (error2) => {
|
|
8930
|
+
finish({
|
|
8931
|
+
code: null,
|
|
8932
|
+
stdout,
|
|
8933
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
8934
|
+
${error2.message}`,
|
|
8935
|
+
timedOut: false
|
|
8936
|
+
});
|
|
8937
|
+
});
|
|
8938
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
8939
|
+
timer.handle = setTimeout(
|
|
8940
|
+
() => {
|
|
8941
|
+
child.kill("SIGKILL");
|
|
8942
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
8943
|
+
},
|
|
8944
|
+
Math.max(0, opts.timeoutMs)
|
|
8945
|
+
);
|
|
8946
|
+
});
|
|
8947
|
+
};
|
|
8948
|
+
function isEnvironmentObject(value) {
|
|
8949
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8950
|
+
}
|
|
8951
|
+
function secretFailure(marker, detail, log3) {
|
|
8952
|
+
const message = `${marker}: ${detail}`;
|
|
8953
|
+
log3(message, "error");
|
|
8954
|
+
return new Error(message);
|
|
8955
|
+
}
|
|
8956
|
+
async function installRunnerSecret({
|
|
8957
|
+
env,
|
|
8958
|
+
log: log3,
|
|
8959
|
+
commandRunner
|
|
8960
|
+
}) {
|
|
8961
|
+
const arn = env.RUNNER_SECRET_ARN?.trim();
|
|
8962
|
+
if (!arn) {
|
|
8963
|
+
log3("runner secret is not configured; continuing without GitHub and MCP credentials");
|
|
8964
|
+
return false;
|
|
8965
|
+
}
|
|
8966
|
+
const result = await (commandRunner ?? runCommand2)(
|
|
8967
|
+
"aws",
|
|
8968
|
+
[
|
|
8969
|
+
"secretsmanager",
|
|
8970
|
+
"get-secret-value",
|
|
8971
|
+
"--secret-id",
|
|
8972
|
+
arn,
|
|
8973
|
+
"--query",
|
|
8974
|
+
"SecretString",
|
|
8975
|
+
"--output",
|
|
8976
|
+
"text"
|
|
8977
|
+
],
|
|
8978
|
+
{ env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
|
|
8979
|
+
);
|
|
8980
|
+
if (result.timedOut) {
|
|
8981
|
+
throw secretFailure(
|
|
8982
|
+
"CREDENTIAL-RESTORE-TIMEOUT",
|
|
8983
|
+
`runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
|
|
8984
|
+
log3
|
|
8985
|
+
);
|
|
8986
|
+
}
|
|
8987
|
+
if (result.code !== 0) {
|
|
8988
|
+
throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
|
|
8989
|
+
}
|
|
8990
|
+
let payload;
|
|
8991
|
+
try {
|
|
8992
|
+
payload = JSON.parse(result.stdout);
|
|
8993
|
+
} catch (error2) {
|
|
8994
|
+
log3(
|
|
8995
|
+
`RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
8996
|
+
"warn"
|
|
8997
|
+
);
|
|
8998
|
+
return false;
|
|
8999
|
+
}
|
|
9000
|
+
if (!isEnvironmentObject(payload)) {
|
|
9001
|
+
log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
|
|
9002
|
+
return false;
|
|
9003
|
+
}
|
|
9004
|
+
let populated = 0;
|
|
9005
|
+
let skipped = 0;
|
|
9006
|
+
let githubTokenPopulated = false;
|
|
9007
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
9008
|
+
if (typeof value !== "string" || value.length === 0) continue;
|
|
9009
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
|
|
9010
|
+
log3(
|
|
9011
|
+
`RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
|
|
9012
|
+
"warn"
|
|
9013
|
+
);
|
|
9014
|
+
skipped += 1;
|
|
9015
|
+
continue;
|
|
9016
|
+
}
|
|
9017
|
+
env[key] = value;
|
|
9018
|
+
populated += 1;
|
|
9019
|
+
if (key === "GH_TOKEN") githubTokenPopulated = true;
|
|
9020
|
+
}
|
|
9021
|
+
if (populated === 0) {
|
|
9022
|
+
log3(
|
|
9023
|
+
"RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
|
|
9024
|
+
"warn"
|
|
9025
|
+
);
|
|
9026
|
+
} else {
|
|
9027
|
+
log3(
|
|
9028
|
+
`RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
|
|
9029
|
+
);
|
|
9030
|
+
}
|
|
9031
|
+
return githubTokenPopulated;
|
|
9032
|
+
}
|
|
9033
|
+
function restoreFailure(operation, result, log3) {
|
|
9034
|
+
const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
|
|
9035
|
+
log3(message, "error");
|
|
9036
|
+
return new Error(message);
|
|
9037
|
+
}
|
|
9038
|
+
async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
|
|
9039
|
+
const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
|
|
9040
|
+
if (result.timedOut) {
|
|
9041
|
+
log3(
|
|
9042
|
+
`CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
9043
|
+
"warn"
|
|
9044
|
+
);
|
|
9045
|
+
return result;
|
|
9046
|
+
}
|
|
9047
|
+
if (result.code !== 0) throw restoreFailure(operation, result, log3);
|
|
9048
|
+
return result;
|
|
9049
|
+
}
|
|
9050
|
+
async function restoreCredentialStores({
|
|
9051
|
+
env,
|
|
9052
|
+
log: log3,
|
|
9053
|
+
synchroniserRunner = runSynchroniser
|
|
9054
|
+
}) {
|
|
9055
|
+
await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
|
|
9056
|
+
await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
|
|
9057
|
+
const result = await synchroniserRunner(["model-auth-ready"], {
|
|
9058
|
+
timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
|
|
9059
|
+
});
|
|
9060
|
+
if (result.timedOut) {
|
|
9061
|
+
log3(
|
|
9062
|
+
`CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
9063
|
+
"warn"
|
|
9064
|
+
);
|
|
9065
|
+
return;
|
|
9066
|
+
}
|
|
9067
|
+
switch (result.code) {
|
|
9068
|
+
case 0:
|
|
9069
|
+
return;
|
|
9070
|
+
case 10:
|
|
9071
|
+
log3(
|
|
9072
|
+
`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.`,
|
|
9073
|
+
"warn"
|
|
9074
|
+
);
|
|
9075
|
+
return;
|
|
9076
|
+
default:
|
|
9077
|
+
log3("could not determine whether this VM has model credentials", "warn");
|
|
9078
|
+
}
|
|
9079
|
+
}
|
|
9080
|
+
var GIT_CREDENTIAL_HELPER_CONTENT = [
|
|
9081
|
+
"#!/usr/bin/env bash",
|
|
9082
|
+
'[ "$1" = get ] || exit 0',
|
|
9083
|
+
"echo username=x-access-token",
|
|
9084
|
+
'echo "password=${GH_TOKEN}"',
|
|
9085
|
+
""
|
|
9086
|
+
].join("\n");
|
|
9087
|
+
async function probeGitHubAccess({ env, log: log3 }) {
|
|
9088
|
+
const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
|
|
9089
|
+
env,
|
|
9090
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
9091
|
+
});
|
|
9092
|
+
if (auth.timedOut) {
|
|
9093
|
+
log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
|
|
9094
|
+
return;
|
|
9095
|
+
}
|
|
9096
|
+
if (auth.code !== 0) {
|
|
9097
|
+
log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
|
|
9098
|
+
return;
|
|
9099
|
+
}
|
|
9100
|
+
log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
|
|
9101
|
+
const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
|
|
9102
|
+
env,
|
|
9103
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
9104
|
+
});
|
|
9105
|
+
if (remote.code !== 0 || remote.timedOut) return;
|
|
9106
|
+
const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
|
|
9107
|
+
if (!repo) return;
|
|
9108
|
+
const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
|
|
9109
|
+
env,
|
|
9110
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
9111
|
+
});
|
|
9112
|
+
if (repository.timedOut) {
|
|
9113
|
+
log3(
|
|
9114
|
+
`GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
|
|
9115
|
+
"warn"
|
|
9116
|
+
);
|
|
9117
|
+
} else if (repository.code !== 0) {
|
|
9118
|
+
log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
|
|
9119
|
+
}
|
|
9120
|
+
}
|
|
9121
|
+
async function configureGitHubAccess({ env, log: log3 }) {
|
|
9122
|
+
if (!env.GH_TOKEN) {
|
|
9123
|
+
log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
|
|
9124
|
+
return;
|
|
9125
|
+
}
|
|
9126
|
+
try {
|
|
9127
|
+
env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
|
|
9128
|
+
writeFileSync4(GIT_CONFIG_GLOBAL, "");
|
|
9129
|
+
writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
|
|
9130
|
+
chmodSync2(GIT_CREDENTIAL_HELPER, 448);
|
|
9131
|
+
const config = [
|
|
9132
|
+
["user.name", env.GIT_USER_NAME ?? "evident-bot"],
|
|
9133
|
+
["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
|
|
9134
|
+
["init.defaultBranch", "main"],
|
|
9135
|
+
["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
|
|
9136
|
+
];
|
|
9137
|
+
for (const [key, value] of config) {
|
|
9138
|
+
const result = await runCommand2("git", ["config", "--global", key, value], {
|
|
9139
|
+
env,
|
|
9140
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
9141
|
+
});
|
|
9142
|
+
if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
|
|
9143
|
+
}
|
|
9144
|
+
} catch (error2) {
|
|
9145
|
+
log3(
|
|
9146
|
+
`GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
9147
|
+
"warn"
|
|
9148
|
+
);
|
|
9149
|
+
return;
|
|
9150
|
+
}
|
|
9151
|
+
void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
|
|
9152
|
+
log3(
|
|
9153
|
+
`GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
9154
|
+
"warn"
|
|
9155
|
+
);
|
|
9156
|
+
});
|
|
9157
|
+
}
|
|
9158
|
+
|
|
9159
|
+
// src/lib/opencode/config-overlay.ts
|
|
9160
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
9161
|
+
import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
|
|
9162
|
+
import { isAbsolute as isAbsolute2, join as join8, resolve as resolve3 } from "path";
|
|
9163
|
+
function isFile(filePath) {
|
|
9164
|
+
return existsSync2(filePath) && statSync5(filePath).isFile();
|
|
9165
|
+
}
|
|
9166
|
+
function applyRunnerOpenCodeConfig({
|
|
9167
|
+
overlayPath,
|
|
9168
|
+
cwd = process.cwd(),
|
|
9169
|
+
log: log3
|
|
9170
|
+
}) {
|
|
9171
|
+
if (!overlayPath) {
|
|
9172
|
+
log3("runner OpenCode config is not configured; using the baked project config", "debug");
|
|
9173
|
+
return;
|
|
9174
|
+
}
|
|
9175
|
+
const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
|
|
9176
|
+
const target = isFile(join8(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
|
|
9177
|
+
if (!isFile(source)) {
|
|
9178
|
+
log3(
|
|
9179
|
+
`RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
|
|
9180
|
+
"error"
|
|
9181
|
+
);
|
|
9182
|
+
return;
|
|
9183
|
+
}
|
|
9184
|
+
copyFileSync(source, join8(cwd, target));
|
|
9185
|
+
try {
|
|
9186
|
+
execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
9187
|
+
stdio: "ignore"
|
|
9188
|
+
});
|
|
9189
|
+
} catch (error2) {
|
|
9190
|
+
const detail = error2 instanceof Error ? error2.message : String(error2);
|
|
9191
|
+
log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
|
|
9192
|
+
}
|
|
9193
|
+
log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
|
|
9194
|
+
}
|
|
9195
|
+
|
|
9196
|
+
// src/lib/credential-sync.ts
|
|
9197
|
+
import { renameSync, writeFileSync as writeFileSync5 } from "fs";
|
|
9198
|
+
var CREDENTIAL_SYNC_TICK_TIMEOUT_MS = 3e4;
|
|
9199
|
+
var CREDENTIAL_FLUSH_DEADLINE_MS = 8e3;
|
|
9200
|
+
var CREDENTIAL_FLUSH_ABORT_GRACE_MS = 500;
|
|
9201
|
+
var DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS = 60;
|
|
9202
|
+
var STORES = ["claude", "opencode"];
|
|
9203
|
+
var MAX_FLUSH_PASSES = 2;
|
|
9204
|
+
function outcomesWith(outcome) {
|
|
9205
|
+
return { claude: outcome, opencode: outcome };
|
|
9206
|
+
}
|
|
9207
|
+
function errorMessage(error2) {
|
|
9208
|
+
return error2 instanceof Error ? error2.message : String(error2);
|
|
9209
|
+
}
|
|
9210
|
+
function waitForSettlement(promise, timeoutMs) {
|
|
9211
|
+
return new Promise((resolve4) => {
|
|
9212
|
+
let settled = false;
|
|
9213
|
+
const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
|
|
9214
|
+
const finish = (value) => {
|
|
9215
|
+
if (settled) return;
|
|
9216
|
+
settled = true;
|
|
9217
|
+
clearTimeout(timer);
|
|
9218
|
+
resolve4(value);
|
|
9219
|
+
};
|
|
9220
|
+
promise.then(
|
|
9221
|
+
() => finish(true),
|
|
9222
|
+
() => finish(true)
|
|
9223
|
+
);
|
|
9224
|
+
});
|
|
9225
|
+
}
|
|
9226
|
+
function writeMarker(markerPath, outcomes, log3) {
|
|
9227
|
+
const body = `${STORES.map((store) => `${store}=${outcomes[store]}`).join("\n")}
|
|
9228
|
+
`;
|
|
9229
|
+
const temporaryPath = `${markerPath}.tmp`;
|
|
9230
|
+
try {
|
|
9231
|
+
writeFileSync5(temporaryPath, body, { mode: 384 });
|
|
9232
|
+
renameSync(temporaryPath, markerPath);
|
|
9233
|
+
} catch (error2) {
|
|
9234
|
+
log3(`CREDENTIAL-FLUSH-MARKER-UNWRITABLE: ${markerPath}: ${errorMessage(error2)}`, "warn");
|
|
9235
|
+
}
|
|
9236
|
+
}
|
|
9237
|
+
function intervalSeconds(env, log3) {
|
|
9238
|
+
const raw = env.CREDS_SYNC_INTERVAL;
|
|
9239
|
+
if (raw === void 0 || /^[1-9][0-9]*$/.test(raw) && Number.isSafeInteger(Number(raw))) {
|
|
9240
|
+
return raw === void 0 ? DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS : Number(raw);
|
|
9241
|
+
}
|
|
9242
|
+
log3(
|
|
9243
|
+
`CREDENTIAL-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${raw}' is not a positive integer; using ${DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS}s`,
|
|
9244
|
+
"warn"
|
|
9245
|
+
);
|
|
9246
|
+
return DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS;
|
|
9247
|
+
}
|
|
9248
|
+
async function runFlushStore(store, env, synchroniserRunner, log3, deadlineAt) {
|
|
9249
|
+
const remainingMs = deadlineAt - Date.now();
|
|
9250
|
+
if (remainingMs <= 0) return { outcome: "timeout", orphaned: false };
|
|
9251
|
+
const controller = new AbortController();
|
|
9252
|
+
let result;
|
|
9253
|
+
let failed = false;
|
|
9254
|
+
const completion = Promise.resolve().then(
|
|
9255
|
+
() => synchroniserRunner(["sync-once", store], {
|
|
9256
|
+
timeoutMs: remainingMs,
|
|
9257
|
+
env,
|
|
9258
|
+
signal: controller.signal
|
|
9259
|
+
})
|
|
9260
|
+
).then(
|
|
9261
|
+
(value) => {
|
|
9262
|
+
result = value;
|
|
9263
|
+
},
|
|
9264
|
+
(error2) => {
|
|
9265
|
+
failed = true;
|
|
9266
|
+
log3(`CREDENTIAL-FLUSH-ERROR: ${store}: ${errorMessage(error2)}`, "warn");
|
|
9267
|
+
}
|
|
9268
|
+
);
|
|
9269
|
+
const abortTimer = setTimeout(() => controller.abort(), remainingMs);
|
|
9270
|
+
const settledBeforeDeadline = await waitForSettlement(completion, remainingMs);
|
|
9271
|
+
clearTimeout(abortTimer);
|
|
9272
|
+
if (!settledBeforeDeadline) {
|
|
9273
|
+
controller.abort();
|
|
9274
|
+
const settledAfterAbort = await waitForSettlement(completion, CREDENTIAL_FLUSH_ABORT_GRACE_MS);
|
|
9275
|
+
if (!settledAfterAbort) return { outcome: "timeout", orphaned: true };
|
|
9276
|
+
return { outcome: "timeout", orphaned: false };
|
|
9277
|
+
}
|
|
9278
|
+
if (failed || !result) return { outcome: "failed", orphaned: false };
|
|
9279
|
+
if (result.timedOut || Date.now() >= deadlineAt) {
|
|
9280
|
+
return { outcome: "timeout", orphaned: false };
|
|
9281
|
+
}
|
|
9282
|
+
return { outcome: result.code === 0 ? "ok" : "failed", orphaned: false };
|
|
9283
|
+
}
|
|
9284
|
+
function createCredentialSync({
|
|
9285
|
+
markerPath,
|
|
9286
|
+
env,
|
|
9287
|
+
log: log3,
|
|
9288
|
+
synchroniserRunner = runSynchroniser
|
|
9289
|
+
}) {
|
|
9290
|
+
const persistenceDisabled = !env.PERSISTENCE_BUCKET;
|
|
9291
|
+
let disabled = persistenceDisabled;
|
|
9292
|
+
let armed = false;
|
|
9293
|
+
let stopped = false;
|
|
9294
|
+
let timer;
|
|
9295
|
+
let inFlight;
|
|
9296
|
+
let activeTickAbort;
|
|
9297
|
+
let lastTickFailed;
|
|
9298
|
+
let flushPromise;
|
|
9299
|
+
const scheduleTick = (intervalMs, startTick2) => {
|
|
9300
|
+
if (stopped) return;
|
|
9301
|
+
timer = setTimeout(() => {
|
|
9302
|
+
timer = void 0;
|
|
9303
|
+
startTick2();
|
|
9304
|
+
}, intervalMs);
|
|
9305
|
+
};
|
|
9306
|
+
const startTick = (intervalMs) => {
|
|
9307
|
+
if (stopped) return;
|
|
9308
|
+
const controller = new AbortController();
|
|
9309
|
+
activeTickAbort = controller;
|
|
9310
|
+
const tick = (async () => {
|
|
9311
|
+
const outcomes = {
|
|
9312
|
+
claude: "failed",
|
|
9313
|
+
opencode: "failed"
|
|
7820
9314
|
};
|
|
9315
|
+
for (const store of STORES) {
|
|
9316
|
+
if (controller.signal.aborted) break;
|
|
9317
|
+
try {
|
|
9318
|
+
const result = await synchroniserRunner(["sync-once", store], {
|
|
9319
|
+
timeoutMs: CREDENTIAL_SYNC_TICK_TIMEOUT_MS,
|
|
9320
|
+
env,
|
|
9321
|
+
signal: controller.signal
|
|
9322
|
+
});
|
|
9323
|
+
outcomes[store] = !result.timedOut && result.code === 0 ? "ok" : "failed";
|
|
9324
|
+
} catch (error2) {
|
|
9325
|
+
outcomes[store] = "failed";
|
|
9326
|
+
log3(`CREDENTIAL-SYNC-TICK-ERROR: ${store}: ${errorMessage(error2)}`, "debug");
|
|
9327
|
+
}
|
|
9328
|
+
}
|
|
9329
|
+
const failed = STORES.some((store) => outcomes[store] === "failed");
|
|
9330
|
+
log3(
|
|
9331
|
+
`CREDENTIAL-SYNC-TICK: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
|
|
9332
|
+
"debug"
|
|
9333
|
+
);
|
|
9334
|
+
if (failed && lastTickFailed !== true) {
|
|
9335
|
+
log3(
|
|
9336
|
+
"CREDENTIAL-SYNC-FAILED: an interval credential sync failed; retrying on the next tick",
|
|
9337
|
+
"warn"
|
|
9338
|
+
);
|
|
9339
|
+
} else if (!failed && lastTickFailed === true) {
|
|
9340
|
+
log3("CREDENTIAL-SYNC-RECOVERED: interval credential sync succeeded again", "warn");
|
|
9341
|
+
}
|
|
9342
|
+
lastTickFailed = failed;
|
|
9343
|
+
})().finally(() => {
|
|
9344
|
+
if (activeTickAbort === controller) activeTickAbort = void 0;
|
|
9345
|
+
if (inFlight === tick) inFlight = void 0;
|
|
9346
|
+
scheduleTick(intervalMs, () => startTick(intervalMs));
|
|
9347
|
+
});
|
|
9348
|
+
inFlight = tick;
|
|
9349
|
+
};
|
|
9350
|
+
const performFlush = async () => {
|
|
9351
|
+
stopped = true;
|
|
9352
|
+
if (timer) {
|
|
9353
|
+
clearTimeout(timer);
|
|
9354
|
+
timer = void 0;
|
|
9355
|
+
}
|
|
9356
|
+
const deadlineAt = Date.now() + CREDENTIAL_FLUSH_DEADLINE_MS;
|
|
9357
|
+
if (inFlight) {
|
|
9358
|
+
const settled = await waitForSettlement(inFlight, CREDENTIAL_FLUSH_DEADLINE_MS);
|
|
9359
|
+
if (!settled) {
|
|
9360
|
+
activeTickAbort?.abort();
|
|
9361
|
+
const settledAfterAbort = await waitForSettlement(
|
|
9362
|
+
inFlight,
|
|
9363
|
+
CREDENTIAL_FLUSH_ABORT_GRACE_MS
|
|
9364
|
+
);
|
|
9365
|
+
if (!settledAfterAbort) {
|
|
9366
|
+
log3(
|
|
9367
|
+
"CREDENTIAL-FLUSH-ORPHANED-TICK: a sync-once child could not be proven settled; skipping the boundary flush rather than becoming a second writer",
|
|
9368
|
+
"warn"
|
|
9369
|
+
);
|
|
9370
|
+
return { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9371
|
+
}
|
|
9372
|
+
}
|
|
7821
9373
|
}
|
|
7822
|
-
|
|
7823
|
-
|
|
7824
|
-
|
|
7825
|
-
|
|
7826
|
-
|
|
7827
|
-
|
|
7828
|
-
|
|
7829
|
-
|
|
7830
|
-
|
|
7831
|
-
|
|
7832
|
-
console.log(chalk5.yellow(`
|
|
7833
|
-
Port ${port} is already in use.`));
|
|
7834
|
-
const alternativePort = findAvailablePort(port + 1);
|
|
7835
|
-
if (alternativePort) {
|
|
7836
|
-
const useAlternative = await select2({
|
|
7837
|
-
message: `Use port ${alternativePort} instead?`,
|
|
7838
|
-
choices: [
|
|
7839
|
-
{ name: `Yes, use port ${alternativePort}`, value: "yes" },
|
|
7840
|
-
{ name: "No, I will free the port manually", value: "no" }
|
|
7841
|
-
]
|
|
7842
|
-
});
|
|
7843
|
-
if (useAlternative === "yes") {
|
|
7844
|
-
port = alternativePort;
|
|
7845
|
-
} else {
|
|
7846
|
-
throw new Error(`Port ${ctx.port} is in use`);
|
|
9374
|
+
if (disabled) return { outcomes: outcomesWith("skipped"), orphaned: false };
|
|
9375
|
+
const outcomes = outcomesWith("timeout");
|
|
9376
|
+
for (const store of STORES) {
|
|
9377
|
+
const result = await runFlushStore(store, env, synchroniserRunner, log3, deadlineAt);
|
|
9378
|
+
if (result.orphaned) {
|
|
9379
|
+
log3(
|
|
9380
|
+
"CREDENTIAL-FLUSH-ORPHANED-CHILD: a flush sync-once child could not be proven settled; refusing another flush pass rather than becoming a second writer",
|
|
9381
|
+
"warn"
|
|
9382
|
+
);
|
|
9383
|
+
return { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
7847
9384
|
}
|
|
9385
|
+
outcomes[store] = result.outcome;
|
|
7848
9386
|
}
|
|
7849
|
-
|
|
7850
|
-
|
|
7851
|
-
|
|
7852
|
-
|
|
7853
|
-
|
|
7854
|
-
|
|
7855
|
-
|
|
7856
|
-
|
|
7857
|
-
|
|
7858
|
-
|
|
7859
|
-
|
|
7860
|
-
|
|
7861
|
-
|
|
7862
|
-
|
|
7863
|
-
|
|
7864
|
-
name: "Continue without OpenCode",
|
|
7865
|
-
value: "continue",
|
|
7866
|
-
description: "Requests will fail until OpenCode starts"
|
|
9387
|
+
return { outcomes, orphaned: false };
|
|
9388
|
+
};
|
|
9389
|
+
let flushPasses = 0;
|
|
9390
|
+
let lastFlush;
|
|
9391
|
+
return {
|
|
9392
|
+
arm() {
|
|
9393
|
+
if (stopped || armed) return;
|
|
9394
|
+
armed = true;
|
|
9395
|
+
if (persistenceDisabled) {
|
|
9396
|
+
disabled = true;
|
|
9397
|
+
log3(
|
|
9398
|
+
"CREDENTIAL-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot",
|
|
9399
|
+
"warn"
|
|
9400
|
+
);
|
|
9401
|
+
return;
|
|
7867
9402
|
}
|
|
7868
|
-
|
|
7869
|
-
|
|
7870
|
-
|
|
7871
|
-
|
|
7872
|
-
|
|
7873
|
-
|
|
7874
|
-
|
|
7875
|
-
|
|
7876
|
-
|
|
7877
|
-
|
|
7878
|
-
|
|
7879
|
-
|
|
7880
|
-
|
|
7881
|
-
|
|
7882
|
-
|
|
7883
|
-
|
|
7884
|
-
|
|
9403
|
+
disabled = false;
|
|
9404
|
+
const intervalMs = intervalSeconds(env, log3) * 1e3;
|
|
9405
|
+
scheduleTick(intervalMs, () => startTick(intervalMs));
|
|
9406
|
+
},
|
|
9407
|
+
async stopAndFlush(publish) {
|
|
9408
|
+
let result;
|
|
9409
|
+
const runningFlush = flushPromise;
|
|
9410
|
+
if (runningFlush) {
|
|
9411
|
+
result = await runningFlush;
|
|
9412
|
+
} else if (flushPasses >= MAX_FLUSH_PASSES || lastFlush?.orphaned) {
|
|
9413
|
+
result = lastFlush ?? { outcomes: outcomesWith("timeout"), orphaned: true };
|
|
9414
|
+
} else {
|
|
9415
|
+
flushPasses++;
|
|
9416
|
+
const currentFlush = performFlush();
|
|
9417
|
+
flushPromise = currentFlush;
|
|
9418
|
+
try {
|
|
9419
|
+
result = await currentFlush;
|
|
9420
|
+
lastFlush = result;
|
|
9421
|
+
} finally {
|
|
9422
|
+
if (flushPromise === currentFlush) flushPromise = void 0;
|
|
9423
|
+
}
|
|
9424
|
+
}
|
|
9425
|
+
if (publish) writeMarker(markerPath, result.outcomes, log3);
|
|
9426
|
+
return result.outcomes;
|
|
7885
9427
|
}
|
|
7886
|
-
|
|
7887
|
-
return { port, process: proc, version: health.version ?? null, notReadyReason: null };
|
|
7888
|
-
}
|
|
7889
|
-
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
9428
|
+
};
|
|
7890
9429
|
}
|
|
7891
9430
|
|
|
7892
9431
|
// src/commands/run.ts
|
|
@@ -7895,6 +9434,7 @@ var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_
|
|
|
7895
9434
|
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
7896
9435
|
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
7897
9436
|
var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
9437
|
+
var CHILD_STOP_TIMEOUT_MS = 1e4;
|
|
7898
9438
|
function resolveLogLevel(options) {
|
|
7899
9439
|
const accepted = Object.keys(LOG_LEVELS);
|
|
7900
9440
|
const validate = (value, source) => {
|
|
@@ -7925,11 +9465,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
7925
9465
|
if (trimmed === "") {
|
|
7926
9466
|
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
7927
9467
|
}
|
|
7928
|
-
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ?
|
|
7929
|
-
if (!
|
|
9468
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join9(homeDir, trimmed.slice(2)) : trimmed;
|
|
9469
|
+
if (!isAbsolute3(expanded)) {
|
|
7930
9470
|
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
7931
9471
|
}
|
|
7932
|
-
const normalized =
|
|
9472
|
+
const normalized = resolvePath2(expanded);
|
|
7933
9473
|
if (parse(normalized).root === normalized) {
|
|
7934
9474
|
throw new Error(
|
|
7935
9475
|
`--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 +9585,7 @@ function logActivity(state, entry) {
|
|
|
8045
9585
|
}
|
|
8046
9586
|
function reportSessionDbRecovery(state) {
|
|
8047
9587
|
try {
|
|
8048
|
-
const report = drainSessionDbRecoveryReport({ homeDir:
|
|
9588
|
+
const report = drainSessionDbRecoveryReport({ homeDir: homedir5(), env: process.env });
|
|
8049
9589
|
for (const record of report.records) {
|
|
8050
9590
|
const activity = buildSessionDbRecoveryActivity(record);
|
|
8051
9591
|
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
@@ -8063,6 +9603,16 @@ function reportSessionDbRecovery(state) {
|
|
|
8063
9603
|
);
|
|
8064
9604
|
}
|
|
8065
9605
|
}
|
|
9606
|
+
function reportSessionDbRecoveryRecord(state, record) {
|
|
9607
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
9608
|
+
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
9609
|
+
logActivity(state, {
|
|
9610
|
+
type: activity.level === "error" ? "error" : "info",
|
|
9611
|
+
level: activity.level,
|
|
9612
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
9613
|
+
metadata: activity.metadata
|
|
9614
|
+
});
|
|
9615
|
+
}
|
|
8066
9616
|
function displayStatus(state) {
|
|
8067
9617
|
if (!state.interactive) return;
|
|
8068
9618
|
const attempt = state.connection?.reconnectAttempt ?? 0;
|
|
@@ -8175,6 +9725,10 @@ async function driveChannels(state, driver) {
|
|
|
8175
9725
|
consecutiveDrainFailures = 0;
|
|
8176
9726
|
unreachableMs = 0;
|
|
8177
9727
|
state.messageCount += processed;
|
|
9728
|
+
if (driver.recycleRequested) {
|
|
9729
|
+
await beginGracefulShutdown(state, "recycle");
|
|
9730
|
+
return;
|
|
9731
|
+
}
|
|
8178
9732
|
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
8179
9733
|
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
8180
9734
|
const fileActivitySnapshot = driver.fileSyncActivity();
|
|
@@ -8217,8 +9771,8 @@ async function driveChannels(state, driver) {
|
|
|
8217
9771
|
state.running = false;
|
|
8218
9772
|
break;
|
|
8219
9773
|
}
|
|
8220
|
-
const
|
|
8221
|
-
logActivity(state, { type: "error", error: `Channel processing error: ${
|
|
9774
|
+
const errorMessage2 = error2 instanceof Error ? error2.message : String(error2);
|
|
9775
|
+
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage2}` });
|
|
8222
9776
|
if (state.interactive) displayStatus(state);
|
|
8223
9777
|
if (driver.hasInFlightWatchers()) {
|
|
8224
9778
|
consecutiveDrainFailures = 0;
|
|
@@ -8235,7 +9789,7 @@ async function driveChannels(state, driver) {
|
|
|
8235
9789
|
}
|
|
8236
9790
|
}
|
|
8237
9791
|
}
|
|
8238
|
-
await new Promise((
|
|
9792
|
+
await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
|
|
8239
9793
|
const cycleMs = performance.now() - cycleStartedAtMs;
|
|
8240
9794
|
if (idleThisCycle) idleMs += cycleMs;
|
|
8241
9795
|
if (unreachableThisCycle) unreachableMs += cycleMs;
|
|
@@ -8258,7 +9812,43 @@ async function driveChannels(state, driver) {
|
|
|
8258
9812
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
8259
9813
|
var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
|
|
8260
9814
|
function sessionDbPath() {
|
|
8261
|
-
return
|
|
9815
|
+
return join9(homedir5(), ".local", "share", "opencode", "opencode.db");
|
|
9816
|
+
}
|
|
9817
|
+
function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
|
|
9818
|
+
const record = {
|
|
9819
|
+
v: 1,
|
|
9820
|
+
event: "session_db_recovery",
|
|
9821
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
9822
|
+
stage: "verify",
|
|
9823
|
+
outcome: "schema_provenance_mismatch",
|
|
9824
|
+
severity: "error",
|
|
9825
|
+
reason: provenance.reason ?? "schema-provenance-mismatch",
|
|
9826
|
+
litestream_exit_code: null,
|
|
9827
|
+
attempt: null,
|
|
9828
|
+
replica_objects: null,
|
|
9829
|
+
replica_bytes: null,
|
|
9830
|
+
quarantine_destination: null,
|
|
9831
|
+
quarantined_objects: null,
|
|
9832
|
+
quarantine_failed_objects: null,
|
|
9833
|
+
quarantined_bytes: null,
|
|
9834
|
+
verified_restore_point: null,
|
|
9835
|
+
restore_points_tried: null,
|
|
9836
|
+
provenance_reason: provenance.reason,
|
|
9837
|
+
provenance_migration_delta: provenance.migrationDelta,
|
|
9838
|
+
replication_suspended: false,
|
|
9839
|
+
dbPath: sessionDbPath(),
|
|
9840
|
+
recorded_version: provenance.recordedVersion,
|
|
9841
|
+
current_version: currentVersion,
|
|
9842
|
+
provenance_pre_boot_migration_count: preBootMigrationCount
|
|
9843
|
+
};
|
|
9844
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
9845
|
+
if (!activity) throw new Error("could not map session-DB provenance activity");
|
|
9846
|
+
logActivity(state, {
|
|
9847
|
+
type: activity.level === "error" ? "error" : "info",
|
|
9848
|
+
level: activity.level,
|
|
9849
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
9850
|
+
metadata: activity.metadata
|
|
9851
|
+
});
|
|
8262
9852
|
}
|
|
8263
9853
|
async function runSweep(state, driver, config) {
|
|
8264
9854
|
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
@@ -8305,7 +9895,7 @@ async function runSweep(state, driver, config) {
|
|
|
8305
9895
|
const reclaimResult = await reclaimSessionDbSpace({
|
|
8306
9896
|
dbPath: sessionDbPath(),
|
|
8307
9897
|
maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
|
|
8308
|
-
allowFullVacuum: protectedNow.size === 0
|
|
9898
|
+
allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
|
|
8309
9899
|
});
|
|
8310
9900
|
if (reclaimResult.ok) {
|
|
8311
9901
|
const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
|
|
@@ -8341,7 +9931,7 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
8341
9931
|
for (const warning2 of config.warnings) {
|
|
8342
9932
|
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
8343
9933
|
}
|
|
8344
|
-
const dbBytes = statSessionDbBytes(
|
|
9934
|
+
const dbBytes = statSessionDbBytes(homedir5());
|
|
8345
9935
|
void (async () => {
|
|
8346
9936
|
const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
|
|
8347
9937
|
const sizeWarning = buildSessionStoreSizeWarning({
|
|
@@ -8500,7 +10090,17 @@ function scheduleClaudeUsageReporting(state, options) {
|
|
|
8500
10090
|
setTimer: (timer) => {
|
|
8501
10091
|
state.claudeUsageTimer = timer;
|
|
8502
10092
|
},
|
|
8503
|
-
fetchUsage:
|
|
10093
|
+
fetchUsage: async () => {
|
|
10094
|
+
const usage = await getClaudeUsage();
|
|
10095
|
+
if (usage.ownerLookupError) {
|
|
10096
|
+
logActivity(state, {
|
|
10097
|
+
type: "info",
|
|
10098
|
+
level: "debug",
|
|
10099
|
+
message: `Claude usage owner lookup failed: ${usage.ownerLookupError}`
|
|
10100
|
+
});
|
|
10101
|
+
}
|
|
10102
|
+
return usage;
|
|
10103
|
+
},
|
|
8504
10104
|
report: (usage) => reportClaudeUsage(state.agentId, state.authHeader, usage),
|
|
8505
10105
|
isLocalCredentialProblem,
|
|
8506
10106
|
forcedOnHint: "run `claude` to sign in",
|
|
@@ -8532,7 +10132,8 @@ function scheduleResourceUsageReporting(state, options) {
|
|
|
8532
10132
|
});
|
|
8533
10133
|
return;
|
|
8534
10134
|
}
|
|
8535
|
-
const collect = createResourceUsageCollector(
|
|
10135
|
+
const { collect, stop } = createResourceUsageCollector(homedir5());
|
|
10136
|
+
state.stopResourceUsageSampling = stop;
|
|
8536
10137
|
let consecutiveFailures = 0;
|
|
8537
10138
|
const tick = async () => {
|
|
8538
10139
|
try {
|
|
@@ -8642,21 +10243,41 @@ async function cleanup(state, opts = {}) {
|
|
|
8642
10243
|
clearTimeout(state.resourceUsageTimer);
|
|
8643
10244
|
state.resourceUsageTimer = null;
|
|
8644
10245
|
}
|
|
10246
|
+
state.stopResourceUsageSampling?.();
|
|
10247
|
+
state.stopResourceUsageSampling = null;
|
|
10248
|
+
const credentialSync = state.credentialSync;
|
|
10249
|
+
const flushCredentials = credentialSync ? async (phase, publish) => {
|
|
10250
|
+
await timeShutdownPhase(state, durations, phase, async () => {
|
|
10251
|
+
const outcomes = await credentialSync.stopAndFlush(publish);
|
|
10252
|
+
const level = Object.values(outcomes).every((outcome) => outcome === "ok") ? "info" : "warn";
|
|
10253
|
+
log2(
|
|
10254
|
+
state,
|
|
10255
|
+
`Credential flush: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
|
|
10256
|
+
level
|
|
10257
|
+
);
|
|
10258
|
+
});
|
|
10259
|
+
} : void 0;
|
|
10260
|
+
let drainSettled = true;
|
|
8645
10261
|
if (opts.graceful && state.channelDriver) {
|
|
8646
10262
|
state.channelDriver.stop();
|
|
10263
|
+
}
|
|
10264
|
+
if (flushCredentials) {
|
|
10265
|
+
await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
|
|
10266
|
+
}
|
|
10267
|
+
if (opts.graceful && state.channelDriver) {
|
|
8647
10268
|
log2(state, "Draining in-flight channel work before shutdown...");
|
|
8648
10269
|
if (state.interactive) {
|
|
8649
10270
|
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
8650
10271
|
displayStatus(state);
|
|
8651
10272
|
}
|
|
8652
10273
|
const driver = state.channelDriver;
|
|
8653
|
-
|
|
10274
|
+
drainSettled = await timeShutdownPhase(
|
|
8654
10275
|
state,
|
|
8655
10276
|
durations,
|
|
8656
10277
|
"drain",
|
|
8657
10278
|
() => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
|
|
8658
10279
|
);
|
|
8659
|
-
if (!
|
|
10280
|
+
if (!drainSettled) {
|
|
8660
10281
|
logActivity(state, {
|
|
8661
10282
|
type: "info",
|
|
8662
10283
|
message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
|
|
@@ -8664,6 +10285,9 @@ async function cleanup(state, opts = {}) {
|
|
|
8664
10285
|
if (state.interactive) displayStatus(state);
|
|
8665
10286
|
}
|
|
8666
10287
|
}
|
|
10288
|
+
if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
|
|
10289
|
+
await flushCredentials("credential_flush_final", true);
|
|
10290
|
+
}
|
|
8667
10291
|
await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
|
|
8668
10292
|
if (state.connection) {
|
|
8669
10293
|
const connection = state.connection;
|
|
@@ -8672,24 +10296,83 @@ async function cleanup(state, opts = {}) {
|
|
|
8672
10296
|
}
|
|
8673
10297
|
if (state.opencodeProcess) {
|
|
8674
10298
|
const opencodeProcess = state.opencodeProcess;
|
|
8675
|
-
|
|
10299
|
+
const result = await timeShutdownPhase(
|
|
10300
|
+
state,
|
|
10301
|
+
durations,
|
|
10302
|
+
"opencode_stop",
|
|
10303
|
+
() => stopOpenCodeAndWait(opencodeProcess, CHILD_STOP_TIMEOUT_MS)
|
|
10304
|
+
);
|
|
8676
10305
|
if (state.interactive) {
|
|
8677
|
-
logActivity(state, { type: "info", message:
|
|
10306
|
+
logActivity(state, { type: "info", message: `Stopped OpenCode process (${result.outcome})` });
|
|
8678
10307
|
displayStatus(state);
|
|
8679
10308
|
} else {
|
|
8680
|
-
log2(state,
|
|
10309
|
+
log2(state, `Stopped OpenCode process (${result.outcome})`);
|
|
8681
10310
|
}
|
|
8682
10311
|
state.opencodeProcess = null;
|
|
8683
10312
|
}
|
|
10313
|
+
if (state.litestreamProcess) {
|
|
10314
|
+
const litestreamProcess = state.litestreamProcess;
|
|
10315
|
+
const result = await timeShutdownPhase(
|
|
10316
|
+
state,
|
|
10317
|
+
durations,
|
|
10318
|
+
"litestream_stop",
|
|
10319
|
+
() => stopSessionDbReplication(litestreamProcess, CHILD_STOP_TIMEOUT_MS)
|
|
10320
|
+
);
|
|
10321
|
+
log2(state, `Stopped litestream replication (${result.outcome})`);
|
|
10322
|
+
state.litestreamProcess = null;
|
|
10323
|
+
}
|
|
8684
10324
|
return durations;
|
|
8685
10325
|
}
|
|
10326
|
+
async function beginGracefulShutdown(state, trigger) {
|
|
10327
|
+
if (state.shuttingDown) return;
|
|
10328
|
+
state.shuttingDown = true;
|
|
10329
|
+
const shutdownStartedAt = Date.now();
|
|
10330
|
+
const shutdownMessage = trigger === "recycle" ? "Recycle requested \u2014 finishing in-flight work, then exiting" : "Shutting down...";
|
|
10331
|
+
if (state.interactive) {
|
|
10332
|
+
logActivity(state, { type: "info", message: shutdownMessage });
|
|
10333
|
+
displayStatus(state);
|
|
10334
|
+
} else {
|
|
10335
|
+
log2(state, shutdownMessage);
|
|
10336
|
+
}
|
|
10337
|
+
const durations = await cleanup(state, { graceful: true });
|
|
10338
|
+
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
10339
|
+
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
10340
|
+
let timer;
|
|
10341
|
+
const flushed = shutdownTelemetry().then(
|
|
10342
|
+
() => true,
|
|
10343
|
+
(error2) => {
|
|
10344
|
+
log2(
|
|
10345
|
+
state,
|
|
10346
|
+
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
10347
|
+
"warn"
|
|
10348
|
+
);
|
|
10349
|
+
return true;
|
|
10350
|
+
}
|
|
10351
|
+
);
|
|
10352
|
+
const timedOut = new Promise((resolve4) => {
|
|
10353
|
+
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
10354
|
+
});
|
|
10355
|
+
if (!await Promise.race([flushed, timedOut])) {
|
|
10356
|
+
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
10357
|
+
}
|
|
10358
|
+
clearTimeout(timer);
|
|
10359
|
+
});
|
|
10360
|
+
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
10361
|
+
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
10362
|
+
process.exit(0);
|
|
10363
|
+
}
|
|
8686
10364
|
async function run(options) {
|
|
8687
10365
|
const interactive = isInteractive(options.json);
|
|
8688
10366
|
let logLevel;
|
|
8689
10367
|
let fileSyncDirectories;
|
|
8690
10368
|
try {
|
|
8691
10369
|
logLevel = resolveLogLevel(options);
|
|
8692
|
-
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo,
|
|
10370
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir5());
|
|
10371
|
+
if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
|
|
10372
|
+
throw new Error(
|
|
10373
|
+
"--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
|
|
10374
|
+
);
|
|
10375
|
+
}
|
|
8693
10376
|
} catch (error2) {
|
|
8694
10377
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
8695
10378
|
if (options.json) {
|
|
@@ -8713,7 +10396,9 @@ async function run(options) {
|
|
|
8713
10396
|
connected: false,
|
|
8714
10397
|
opencodeConnected: false,
|
|
8715
10398
|
opencodeVersion: null,
|
|
10399
|
+
sessionDbProvenanceAnomaly: false,
|
|
8716
10400
|
opencodeProcess: null,
|
|
10401
|
+
litestreamProcess: null,
|
|
8717
10402
|
connection: null,
|
|
8718
10403
|
channelDriver: null,
|
|
8719
10404
|
running: true,
|
|
@@ -8727,9 +10412,24 @@ async function run(options) {
|
|
|
8727
10412
|
openaiUsageTimer: null,
|
|
8728
10413
|
openaiUsageRearm: null,
|
|
8729
10414
|
resourceUsageTimer: null,
|
|
10415
|
+
stopResourceUsageSampling: null,
|
|
10416
|
+
credentialSync: null,
|
|
8730
10417
|
authHeader: ""
|
|
8731
10418
|
};
|
|
8732
10419
|
setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
|
|
10420
|
+
if (options.credentialSyncMarker) {
|
|
10421
|
+
state.credentialSync = createCredentialSync({
|
|
10422
|
+
markerPath: options.credentialSyncMarker,
|
|
10423
|
+
env: process.env,
|
|
10424
|
+
log: (message, level = "info") => {
|
|
10425
|
+
if (level === "error") {
|
|
10426
|
+
logActivity(state, { type: "error", error: message });
|
|
10427
|
+
} else {
|
|
10428
|
+
logActivity(state, { type: "info", level, message });
|
|
10429
|
+
}
|
|
10430
|
+
}
|
|
10431
|
+
});
|
|
10432
|
+
}
|
|
8733
10433
|
if (fileSyncDirectories.length > 0) {
|
|
8734
10434
|
log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
|
|
8735
10435
|
} else {
|
|
@@ -8755,43 +10455,7 @@ async function run(options) {
|
|
|
8755
10455
|
"warn"
|
|
8756
10456
|
);
|
|
8757
10457
|
}
|
|
8758
|
-
const handleSignal =
|
|
8759
|
-
if (state.shuttingDown) return;
|
|
8760
|
-
state.shuttingDown = true;
|
|
8761
|
-
const shutdownStartedAt = Date.now();
|
|
8762
|
-
if (state.interactive) {
|
|
8763
|
-
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
8764
|
-
displayStatus(state);
|
|
8765
|
-
} else {
|
|
8766
|
-
log2(state, "Shutting down...");
|
|
8767
|
-
}
|
|
8768
|
-
const durations = await cleanup(state, { graceful: true });
|
|
8769
|
-
const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
|
|
8770
|
-
await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
|
|
8771
|
-
let timer;
|
|
8772
|
-
const flushed = shutdownTelemetry().then(
|
|
8773
|
-
() => true,
|
|
8774
|
-
(error2) => {
|
|
8775
|
-
log2(
|
|
8776
|
-
state,
|
|
8777
|
-
`Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
8778
|
-
"warn"
|
|
8779
|
-
);
|
|
8780
|
-
return true;
|
|
8781
|
-
}
|
|
8782
|
-
);
|
|
8783
|
-
const timedOut = new Promise((resolve3) => {
|
|
8784
|
-
timer = setTimeout(() => resolve3(false), telemetryBudgetMs);
|
|
8785
|
-
});
|
|
8786
|
-
if (!await Promise.race([flushed, timedOut])) {
|
|
8787
|
-
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
8788
|
-
}
|
|
8789
|
-
clearTimeout(timer);
|
|
8790
|
-
});
|
|
8791
|
-
const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
|
|
8792
|
-
log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
|
|
8793
|
-
process.exit(0);
|
|
8794
|
-
};
|
|
10458
|
+
const handleSignal = () => beginGracefulShutdown(state, "signal");
|
|
8795
10459
|
process.on("SIGINT", handleSignal);
|
|
8796
10460
|
process.on("SIGTERM", handleSignal);
|
|
8797
10461
|
try {
|
|
@@ -8921,7 +10585,68 @@ async function run(options) {
|
|
|
8921
10585
|
} else {
|
|
8922
10586
|
log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
|
|
8923
10587
|
}
|
|
10588
|
+
if (options.restoreRunnerCredentials) {
|
|
10589
|
+
log2(state, "Restoring runner credentials before starting OpenCode");
|
|
10590
|
+
const credentialContext = {
|
|
10591
|
+
env: process.env,
|
|
10592
|
+
log: (message, level = "info") => {
|
|
10593
|
+
if (level === "error") {
|
|
10594
|
+
logActivity(state, { type: "error", error: message });
|
|
10595
|
+
} else {
|
|
10596
|
+
logActivity(state, { type: "info", level, message });
|
|
10597
|
+
}
|
|
10598
|
+
}
|
|
10599
|
+
};
|
|
10600
|
+
const githubTokenPopulated = await installRunnerSecret(credentialContext);
|
|
10601
|
+
await restoreCredentialStores(credentialContext);
|
|
10602
|
+
if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
|
|
10603
|
+
}
|
|
10604
|
+
state.credentialSync?.arm();
|
|
10605
|
+
let sessionDbVerifyFatal = false;
|
|
10606
|
+
if (!options.restoreSessionDb) {
|
|
10607
|
+
log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
|
|
10608
|
+
} else {
|
|
10609
|
+
const health = await checkOpenCodeHealth(state.port);
|
|
10610
|
+
if (health.healthy) {
|
|
10611
|
+
log2(
|
|
10612
|
+
state,
|
|
10613
|
+
"Skipping session-DB restore: OpenCode is already serving this database",
|
|
10614
|
+
"debug"
|
|
10615
|
+
);
|
|
10616
|
+
} else {
|
|
10617
|
+
const result = await restoreAndVerifySessionDb({
|
|
10618
|
+
dbPath: sessionDbPath(),
|
|
10619
|
+
litestreamConfig: options.litestreamConfig,
|
|
10620
|
+
noReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10621
|
+
env: process.env,
|
|
10622
|
+
log: (message, level = "info") => {
|
|
10623
|
+
if (level === "error") {
|
|
10624
|
+
logActivity(state, { type: "error", error: message });
|
|
10625
|
+
} else {
|
|
10626
|
+
logActivity(state, { type: "info", level, message });
|
|
10627
|
+
}
|
|
10628
|
+
},
|
|
10629
|
+
reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
|
|
10630
|
+
});
|
|
10631
|
+
sessionDbVerifyFatal = result.verifyFatal;
|
|
10632
|
+
}
|
|
10633
|
+
}
|
|
8924
10634
|
reportSessionDbRecovery(state);
|
|
10635
|
+
if (sessionDbVerifyFatal) {
|
|
10636
|
+
throw new Error(
|
|
10637
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
|
|
10638
|
+
);
|
|
10639
|
+
}
|
|
10640
|
+
applyRunnerOpenCodeConfig({
|
|
10641
|
+
overlayPath: options.opencodeConfigOverlay,
|
|
10642
|
+
log: (message, level = "info") => {
|
|
10643
|
+
if (level === "error") {
|
|
10644
|
+
logActivity(state, { type: "error", error: message });
|
|
10645
|
+
} else {
|
|
10646
|
+
logActivity(state, { type: "info", level, message });
|
|
10647
|
+
}
|
|
10648
|
+
}
|
|
10649
|
+
});
|
|
8925
10650
|
const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
|
|
8926
10651
|
for (const warning2 of opencodeStartTimeoutWarnings) {
|
|
8927
10652
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
@@ -8930,6 +10655,7 @@ async function run(options) {
|
|
|
8930
10655
|
for (const warning2 of maxActiveSessionsWarnings) {
|
|
8931
10656
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
8932
10657
|
}
|
|
10658
|
+
const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
|
|
8933
10659
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
8934
10660
|
try {
|
|
8935
10661
|
const oc = await ensureOpenCodeRunning({
|
|
@@ -8937,11 +10663,41 @@ async function run(options) {
|
|
|
8937
10663
|
interactive: state.interactive,
|
|
8938
10664
|
agentId: state.agentId,
|
|
8939
10665
|
log: (message) => log2(state, message),
|
|
8940
|
-
startTimeoutMs: opencodeStartTimeoutMs
|
|
10666
|
+
startTimeoutMs: opencodeStartTimeoutMs,
|
|
10667
|
+
inheritStdio: Boolean(options.opencodePidFile)
|
|
8941
10668
|
});
|
|
8942
10669
|
state.port = oc.port;
|
|
8943
|
-
state.opencodeProcess = oc.process;
|
|
10670
|
+
state.opencodeProcess = options.opencodePidFile ? null : oc.process;
|
|
8944
10671
|
state.opencodeVersion = oc.version;
|
|
10672
|
+
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
10673
|
+
try {
|
|
10674
|
+
writeFileSync6(options.opencodePidFile, `${oc.process.pid}
|
|
10675
|
+
`, { mode: 384 });
|
|
10676
|
+
chmodSync3(options.opencodePidFile, 384);
|
|
10677
|
+
} catch (error2) {
|
|
10678
|
+
logActivity(state, {
|
|
10679
|
+
type: "error",
|
|
10680
|
+
error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10681
|
+
});
|
|
10682
|
+
}
|
|
10683
|
+
}
|
|
10684
|
+
if (state.opencodeVersion !== null) {
|
|
10685
|
+
const provenance = checkSessionDbProvenance({
|
|
10686
|
+
dbPath: sessionDbPath(),
|
|
10687
|
+
currentVersion: state.opencodeVersion,
|
|
10688
|
+
homeDir: homedir5(),
|
|
10689
|
+
env: process.env
|
|
10690
|
+
});
|
|
10691
|
+
if (provenance.anomaly) {
|
|
10692
|
+
state.sessionDbProvenanceAnomaly = true;
|
|
10693
|
+
logSessionDbProvenanceMismatch(
|
|
10694
|
+
state,
|
|
10695
|
+
provenance,
|
|
10696
|
+
state.opencodeVersion,
|
|
10697
|
+
preBootMigrationIds?.length ?? null
|
|
10698
|
+
);
|
|
10699
|
+
}
|
|
10700
|
+
}
|
|
8945
10701
|
state.opencodeConnected = oc.notReadyReason === null;
|
|
8946
10702
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
8947
10703
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
@@ -8978,6 +10734,108 @@ async function run(options) {
|
|
|
8978
10734
|
ocSpinner?.fail(error2.message);
|
|
8979
10735
|
throw error2;
|
|
8980
10736
|
}
|
|
10737
|
+
if (options.litestreamPidFile) {
|
|
10738
|
+
if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
|
|
10739
|
+
log2(
|
|
10740
|
+
state,
|
|
10741
|
+
`Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
|
|
10742
|
+
);
|
|
10743
|
+
} else if (!options.litestreamConfig) {
|
|
10744
|
+
logActivity(state, {
|
|
10745
|
+
type: "info",
|
|
10746
|
+
level: "warn",
|
|
10747
|
+
message: "Skipping Litestream replication because no configuration file was provided"
|
|
10748
|
+
});
|
|
10749
|
+
} else {
|
|
10750
|
+
let existingPid;
|
|
10751
|
+
if (existsSync3(options.litestreamPidFile)) {
|
|
10752
|
+
try {
|
|
10753
|
+
const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
|
|
10754
|
+
const parsedPid = Number(rawPid);
|
|
10755
|
+
if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
|
|
10756
|
+
existingPid = parsedPid;
|
|
10757
|
+
}
|
|
10758
|
+
} catch (error2) {
|
|
10759
|
+
logActivity(state, {
|
|
10760
|
+
type: "info",
|
|
10761
|
+
level: "warn",
|
|
10762
|
+
message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10763
|
+
});
|
|
10764
|
+
}
|
|
10765
|
+
}
|
|
10766
|
+
if (existingPid !== void 0 && isProcessAlive(existingPid)) {
|
|
10767
|
+
log2(state, `Litestream replication is already running with pid ${existingPid}`);
|
|
10768
|
+
} else {
|
|
10769
|
+
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
10770
|
+
state.litestreamProcess = null;
|
|
10771
|
+
let failureHandled = false;
|
|
10772
|
+
const reportImageOwnedReplicationFailure = (message) => {
|
|
10773
|
+
if (failureHandled || state.shuttingDown || !state.running) return;
|
|
10774
|
+
failureHandled = true;
|
|
10775
|
+
logActivity(state, { type: "error", error: message });
|
|
10776
|
+
if (state.interactive) displayStatus(state);
|
|
10777
|
+
};
|
|
10778
|
+
litestreamProcess.on("exit", (code, signal) => {
|
|
10779
|
+
reportImageOwnedReplicationFailure(
|
|
10780
|
+
`Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
|
|
10781
|
+
);
|
|
10782
|
+
});
|
|
10783
|
+
litestreamProcess.on("error", (error2) => {
|
|
10784
|
+
reportImageOwnedReplicationFailure(
|
|
10785
|
+
`Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10786
|
+
);
|
|
10787
|
+
});
|
|
10788
|
+
try {
|
|
10789
|
+
if (litestreamProcess.pid !== void 0) {
|
|
10790
|
+
writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
|
|
10791
|
+
`, {
|
|
10792
|
+
mode: 384
|
|
10793
|
+
});
|
|
10794
|
+
chmodSync3(options.litestreamPidFile, 384);
|
|
10795
|
+
}
|
|
10796
|
+
} catch (error2) {
|
|
10797
|
+
logActivity(state, {
|
|
10798
|
+
type: "error",
|
|
10799
|
+
error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10800
|
+
});
|
|
10801
|
+
}
|
|
10802
|
+
log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
|
|
10803
|
+
}
|
|
10804
|
+
}
|
|
10805
|
+
} else if (options.litestreamConfig) {
|
|
10806
|
+
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
10807
|
+
state.litestreamProcess = litestreamProcess;
|
|
10808
|
+
let failureHandled = false;
|
|
10809
|
+
const failRunForReplication = (message) => {
|
|
10810
|
+
if (failureHandled || state.shuttingDown || !state.running) return;
|
|
10811
|
+
failureHandled = true;
|
|
10812
|
+
state.shuttingDown = true;
|
|
10813
|
+
logActivity(state, { type: "error", error: message });
|
|
10814
|
+
if (state.interactive) displayStatus(state);
|
|
10815
|
+
void (async () => {
|
|
10816
|
+
try {
|
|
10817
|
+
await cleanup(state);
|
|
10818
|
+
await shutdownTelemetry();
|
|
10819
|
+
} catch (error2) {
|
|
10820
|
+
console.error(
|
|
10821
|
+
`[run] replication failure cleanup failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10822
|
+
);
|
|
10823
|
+
}
|
|
10824
|
+
process.exit(1);
|
|
10825
|
+
})();
|
|
10826
|
+
};
|
|
10827
|
+
litestreamProcess.on("exit", (code, signal) => {
|
|
10828
|
+
failRunForReplication(
|
|
10829
|
+
`Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
|
|
10830
|
+
);
|
|
10831
|
+
});
|
|
10832
|
+
litestreamProcess.on("error", (error2) => {
|
|
10833
|
+
failRunForReplication(
|
|
10834
|
+
`Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10835
|
+
);
|
|
10836
|
+
});
|
|
10837
|
+
log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
|
|
10838
|
+
}
|
|
8981
10839
|
const tunnelSpinner = interactive && !state.json ? ora3("Connecting tunnel...").start() : null;
|
|
8982
10840
|
const channelDriver = new ChannelDriver({
|
|
8983
10841
|
agentId: state.agentId,
|
|
@@ -8989,7 +10847,7 @@ async function run(options) {
|
|
|
8989
10847
|
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
8990
10848
|
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
8991
10849
|
fileSyncDirectories,
|
|
8992
|
-
homeDir:
|
|
10850
|
+
homeDir: homedir5(),
|
|
8993
10851
|
maxActiveSessions,
|
|
8994
10852
|
log: (entry) => (
|
|
8995
10853
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
@@ -9115,6 +10973,18 @@ async function run(options) {
|
|
|
9115
10973
|
if (state.interactive) displayStatus(state);
|
|
9116
10974
|
});
|
|
9117
10975
|
},
|
|
10976
|
+
// Both loops are rearmed because `rearm()` is idempotent for the
|
|
10977
|
+
// provider that did not just connect, and is a no-op when reporting is off.
|
|
10978
|
+
onUsageRearmPing: () => {
|
|
10979
|
+
if (!state.running) return;
|
|
10980
|
+
logActivity(state, {
|
|
10981
|
+
type: "info",
|
|
10982
|
+
level: "debug",
|
|
10983
|
+
message: "Usage rearm ping received"
|
|
10984
|
+
});
|
|
10985
|
+
state.claudeUsageRearm?.();
|
|
10986
|
+
state.openaiUsageRearm?.();
|
|
10987
|
+
},
|
|
9118
10988
|
onInfo: (message) => logActivity(state, { type: "info", message })
|
|
9119
10989
|
}
|
|
9120
10990
|
});
|
|
@@ -9135,7 +11005,17 @@ async function run(options) {
|
|
|
9135
11005
|
setTimer: (timer) => {
|
|
9136
11006
|
state.openaiUsageTimer = timer;
|
|
9137
11007
|
},
|
|
9138
|
-
fetchUsage: () =>
|
|
11008
|
+
fetchUsage: async () => {
|
|
11009
|
+
const usage = await getOpenAiUsage(state.port);
|
|
11010
|
+
if (usage.subscription === null) {
|
|
11011
|
+
logActivity(state, {
|
|
11012
|
+
type: "info",
|
|
11013
|
+
level: "debug",
|
|
11014
|
+
message: "OpenAI usage subscription could not be identified from the local credential"
|
|
11015
|
+
});
|
|
11016
|
+
}
|
|
11017
|
+
return usage;
|
|
11018
|
+
},
|
|
9139
11019
|
report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
|
|
9140
11020
|
isLocalCredentialProblem: isLocalCredentialProblem2,
|
|
9141
11021
|
forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
|
|
@@ -9181,7 +11061,7 @@ async function run(options) {
|
|
|
9181
11061
|
}
|
|
9182
11062
|
|
|
9183
11063
|
// src/index.ts
|
|
9184
|
-
var { version } =
|
|
11064
|
+
var { version } = createRequire2(import.meta.url)("../package.json");
|
|
9185
11065
|
var program = new Command();
|
|
9186
11066
|
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
|
|
9187
11067
|
"--endpoint <url>",
|
|
@@ -9238,6 +11118,30 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9238
11118
|
).option(
|
|
9239
11119
|
"--tunnel-ready-file <path>",
|
|
9240
11120
|
"Path to write once the tunnel is connected (opt-in; unused unless an operator/image sets it)"
|
|
11121
|
+
).option(
|
|
11122
|
+
"--litestream-config <path>",
|
|
11123
|
+
"Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
|
|
11124
|
+
).option(
|
|
11125
|
+
"--opencode-pid-file <path>",
|
|
11126
|
+
"Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
11127
|
+
).option(
|
|
11128
|
+
"--litestream-pid-file <path>",
|
|
11129
|
+
"Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
11130
|
+
).option(
|
|
11131
|
+
"--session-db-no-replicate-marker <path>",
|
|
11132
|
+
"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."
|
|
11133
|
+
).option(
|
|
11134
|
+
"--restore-session-db",
|
|
11135
|
+
"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."
|
|
11136
|
+
).option(
|
|
11137
|
+
"--restore-runner-credentials",
|
|
11138
|
+
"Restore the hosted runner secret and persisted credential stores before starting OpenCode."
|
|
11139
|
+
).option(
|
|
11140
|
+
"--opencode-config-overlay <path>",
|
|
11141
|
+
"Apply this runner-provided OpenCode config before starting OpenCode."
|
|
11142
|
+
).option(
|
|
11143
|
+
"--credential-sync-marker <path>",
|
|
11144
|
+
"Own the interval credential sync and write this marker once the shutdown flush has finished, so the runner image's lifecycle hooks can wait on it."
|
|
9241
11145
|
).action(
|
|
9242
11146
|
(options) => {
|
|
9243
11147
|
run({
|
|
@@ -9269,7 +11173,15 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9269
11173
|
// Raw values — expansion/validation is single-sourced in run.ts's
|
|
9270
11174
|
// resolveFileSyncDirectories.
|
|
9271
11175
|
enableFileSyncTo: options.enableFileSyncTo,
|
|
9272
|
-
tunnelReadyFile: options.tunnelReadyFile
|
|
11176
|
+
tunnelReadyFile: options.tunnelReadyFile,
|
|
11177
|
+
litestreamConfig: options.litestreamConfig,
|
|
11178
|
+
opencodePidFile: options.opencodePidFile,
|
|
11179
|
+
litestreamPidFile: options.litestreamPidFile,
|
|
11180
|
+
sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
11181
|
+
restoreSessionDb: options.restoreSessionDb,
|
|
11182
|
+
restoreRunnerCredentials: options.restoreRunnerCredentials,
|
|
11183
|
+
opencodeConfigOverlay: options.opencodeConfigOverlay,
|
|
11184
|
+
credentialSyncMarker: options.credentialSyncMarker
|
|
9273
11185
|
});
|
|
9274
11186
|
}
|
|
9275
11187
|
);
|