@evident-ai/cli 3.4.1-dev.f505f12 → 3.4.1-dev.fb713ba
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 +20 -0
- package/dist/index.js +1336 -81
- 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
|
}
|
|
@@ -1183,8 +1183,9 @@ async function claudeUsage() {
|
|
|
1183
1183
|
}
|
|
1184
1184
|
|
|
1185
1185
|
// src/commands/run.ts
|
|
1186
|
-
import {
|
|
1187
|
-
import {
|
|
1186
|
+
import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync5 } from "fs";
|
|
1187
|
+
import { homedir as homedir5 } from "os";
|
|
1188
|
+
import { isAbsolute as isAbsolute3, join as join9, parse, resolve as resolvePath2 } from "path";
|
|
1188
1189
|
import chalk6 from "chalk";
|
|
1189
1190
|
|
|
1190
1191
|
// ../../packages/types/src/agents/index.ts
|
|
@@ -1524,7 +1525,14 @@ function drainSessionDbRecoveryReport({
|
|
|
1524
1525
|
skippedLines++;
|
|
1525
1526
|
return [];
|
|
1526
1527
|
}
|
|
1527
|
-
return [
|
|
1528
|
+
return [
|
|
1529
|
+
{
|
|
1530
|
+
...value,
|
|
1531
|
+
provenance_reason: value.provenance_reason ?? null,
|
|
1532
|
+
provenance_migration_delta: value.provenance_migration_delta ?? null,
|
|
1533
|
+
replication_suspended: value.replication_suspended ?? false
|
|
1534
|
+
}
|
|
1535
|
+
];
|
|
1528
1536
|
} catch (error2) {
|
|
1529
1537
|
skippedLines++;
|
|
1530
1538
|
console.error(
|
|
@@ -1549,12 +1557,39 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1549
1557
|
const level = record.severity === "warning" ? "warn" : record.severity === "error" ? "error" : null;
|
|
1550
1558
|
if (!level) return null;
|
|
1551
1559
|
const noVerifiedPoint = record.verified_restore_point ? ` The verified restore point is ${record.verified_restore_point}.` : " No verified restore point is known.";
|
|
1560
|
+
const replication = record.replication_suspended ? " This start is not backing up its new session history; restart after fixing the cause." : "";
|
|
1561
|
+
const giveupMessage = (() => {
|
|
1562
|
+
switch (record.reason) {
|
|
1563
|
+
case "restore_deadline_exceeded":
|
|
1564
|
+
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.";
|
|
1565
|
+
case "restore_tool_unusable":
|
|
1566
|
+
case "classification_unrecognised":
|
|
1567
|
+
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.";
|
|
1568
|
+
case "synchroniser_config_unevaluable":
|
|
1569
|
+
case "synchroniser_config_incomplete":
|
|
1570
|
+
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.";
|
|
1571
|
+
case "synchroniser_config_unresolved":
|
|
1572
|
+
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.";
|
|
1573
|
+
case "litestream_config_unavailable":
|
|
1574
|
+
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.";
|
|
1575
|
+
case "classification_fatal":
|
|
1576
|
+
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.";
|
|
1577
|
+
default:
|
|
1578
|
+
return null;
|
|
1579
|
+
}
|
|
1580
|
+
})();
|
|
1581
|
+
if (giveupMessage)
|
|
1582
|
+
return {
|
|
1583
|
+
level,
|
|
1584
|
+
metadata: withoutContractFields(record),
|
|
1585
|
+
message: `${giveupMessage}${replication}`
|
|
1586
|
+
};
|
|
1552
1587
|
switch (record.outcome) {
|
|
1553
1588
|
case "fresh_session_db":
|
|
1554
1589
|
return {
|
|
1555
1590
|
level,
|
|
1556
1591
|
metadata: withoutContractFields(record),
|
|
1557
|
-
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
|
|
1592
|
+
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}`
|
|
1558
1593
|
};
|
|
1559
1594
|
case "restore_retried":
|
|
1560
1595
|
return {
|
|
@@ -1592,7 +1627,7 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1592
1627
|
return {
|
|
1593
1628
|
level,
|
|
1594
1629
|
metadata: withoutContractFields(record),
|
|
1595
|
-
message:
|
|
1630
|
+
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}`
|
|
1596
1631
|
};
|
|
1597
1632
|
case "session_db_boot_refused":
|
|
1598
1633
|
return {
|
|
@@ -1600,6 +1635,12 @@ function buildSessionDbRecoveryActivity(record) {
|
|
|
1600
1635
|
metadata: withoutContractFields(record),
|
|
1601
1636
|
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.`
|
|
1602
1637
|
};
|
|
1638
|
+
case "schema_provenance_mismatch":
|
|
1639
|
+
return {
|
|
1640
|
+
level,
|
|
1641
|
+
metadata: withoutContractFields(record),
|
|
1642
|
+
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.`
|
|
1643
|
+
};
|
|
1603
1644
|
default:
|
|
1604
1645
|
return null;
|
|
1605
1646
|
}
|
|
@@ -1614,7 +1655,8 @@ var OUTCOMES = /* @__PURE__ */ new Set([
|
|
|
1614
1655
|
"fresh_session_db",
|
|
1615
1656
|
"history_rolled_back",
|
|
1616
1657
|
"restore_misconfigured",
|
|
1617
|
-
"session_db_boot_refused"
|
|
1658
|
+
"session_db_boot_refused",
|
|
1659
|
+
"schema_provenance_mismatch"
|
|
1618
1660
|
]);
|
|
1619
1661
|
var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
|
|
1620
1662
|
var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
|
|
@@ -1632,7 +1674,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
|
|
|
1632
1674
|
function isSessionDbRecoveryRecord(value) {
|
|
1633
1675
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1634
1676
|
const record = value;
|
|
1635
|
-
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(
|
|
1677
|
+
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(
|
|
1636
1678
|
(field) => record[field] === null || typeof record[field] === "string"
|
|
1637
1679
|
);
|
|
1638
1680
|
}
|
|
@@ -1661,11 +1703,644 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
1661
1703
|
if (health.healthy) {
|
|
1662
1704
|
return health;
|
|
1663
1705
|
}
|
|
1664
|
-
await new Promise((
|
|
1706
|
+
await new Promise((resolve4) => setTimeout(resolve4, 1e3));
|
|
1665
1707
|
}
|
|
1666
1708
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
1667
1709
|
}
|
|
1668
1710
|
|
|
1711
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1712
|
+
import { spawn as spawn2 } from "child_process";
|
|
1713
|
+
import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "fs";
|
|
1714
|
+
import { homedir as homedir2 } from "os";
|
|
1715
|
+
import { dirname as dirname2, resolve as resolvePath } from "path";
|
|
1716
|
+
|
|
1717
|
+
// src/lib/runner-synchroniser.ts
|
|
1718
|
+
import { spawn } from "child_process";
|
|
1719
|
+
function appendError(stderr, error2) {
|
|
1720
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1721
|
+
return stderr === "" ? message : `${stderr}
|
|
1722
|
+
${message}`;
|
|
1723
|
+
}
|
|
1724
|
+
function runSynchroniser(args, opts) {
|
|
1725
|
+
return new Promise((resolve4) => {
|
|
1726
|
+
let child;
|
|
1727
|
+
let stdout = "";
|
|
1728
|
+
let stderr = "";
|
|
1729
|
+
let settled = false;
|
|
1730
|
+
const timer = {};
|
|
1731
|
+
const finish = (result) => {
|
|
1732
|
+
if (settled) return;
|
|
1733
|
+
settled = true;
|
|
1734
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
1735
|
+
resolve4(result);
|
|
1736
|
+
};
|
|
1737
|
+
try {
|
|
1738
|
+
child = spawn("runner-synchroniser", args, {
|
|
1739
|
+
env: opts.env ?? process.env,
|
|
1740
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1741
|
+
});
|
|
1742
|
+
} catch (error2) {
|
|
1743
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1744
|
+
return;
|
|
1745
|
+
}
|
|
1746
|
+
child.stdout?.setEncoding("utf8");
|
|
1747
|
+
child.stdout?.on("data", (chunk) => {
|
|
1748
|
+
stdout += chunk;
|
|
1749
|
+
});
|
|
1750
|
+
child.stderr?.setEncoding("utf8");
|
|
1751
|
+
child.stderr?.on("data", (chunk) => {
|
|
1752
|
+
stderr += chunk;
|
|
1753
|
+
});
|
|
1754
|
+
child.once("error", (error2) => {
|
|
1755
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1756
|
+
});
|
|
1757
|
+
child.once("close", (code) => {
|
|
1758
|
+
finish({ code, stdout, stderr, timedOut: false });
|
|
1759
|
+
});
|
|
1760
|
+
timer.handle = setTimeout(
|
|
1761
|
+
() => {
|
|
1762
|
+
child.kill("SIGKILL");
|
|
1763
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1764
|
+
},
|
|
1765
|
+
Math.max(0, opts.timeoutMs)
|
|
1766
|
+
);
|
|
1767
|
+
});
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1771
|
+
var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
|
|
1772
|
+
var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
|
|
1773
|
+
var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
|
|
1774
|
+
var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
|
|
1775
|
+
function commandError(result) {
|
|
1776
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
1777
|
+
}
|
|
1778
|
+
function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
|
|
1779
|
+
options.reportRecovery({
|
|
1780
|
+
v: 1,
|
|
1781
|
+
event: "session_db_recovery",
|
|
1782
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1783
|
+
stage,
|
|
1784
|
+
outcome,
|
|
1785
|
+
severity: "error",
|
|
1786
|
+
reason,
|
|
1787
|
+
litestream_exit_code: litestreamExitCode,
|
|
1788
|
+
attempt: null,
|
|
1789
|
+
replica_objects: null,
|
|
1790
|
+
replica_bytes: null,
|
|
1791
|
+
quarantine_destination: null,
|
|
1792
|
+
quarantined_objects: null,
|
|
1793
|
+
quarantine_failed_objects: null,
|
|
1794
|
+
quarantined_bytes: null,
|
|
1795
|
+
verified_restore_point: null,
|
|
1796
|
+
restore_points_tried: null,
|
|
1797
|
+
provenance_reason: null,
|
|
1798
|
+
provenance_migration_delta: null,
|
|
1799
|
+
replication_suspended: stage === "restore"
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
function clearMarker(options) {
|
|
1803
|
+
if (!options.noReplicateMarker) return;
|
|
1804
|
+
try {
|
|
1805
|
+
unlinkSync2(options.noReplicateMarker);
|
|
1806
|
+
} catch (error2) {
|
|
1807
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
|
|
1808
|
+
options.log(
|
|
1809
|
+
`Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1810
|
+
"warn"
|
|
1811
|
+
);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
function markNoReplicate(options, message) {
|
|
1815
|
+
if (options.noReplicateMarker) {
|
|
1816
|
+
try {
|
|
1817
|
+
mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
|
|
1818
|
+
writeFileSync(options.noReplicateMarker, "");
|
|
1819
|
+
} catch (error2) {
|
|
1820
|
+
options.log(
|
|
1821
|
+
`Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1822
|
+
"error"
|
|
1823
|
+
);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
|
|
1827
|
+
}
|
|
1828
|
+
function discardSessionDbDebris(options) {
|
|
1829
|
+
for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
|
|
1830
|
+
try {
|
|
1831
|
+
unlinkSync2(path);
|
|
1832
|
+
} catch (error2) {
|
|
1833
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
|
|
1834
|
+
options.log(
|
|
1835
|
+
`Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1836
|
+
"warn"
|
|
1837
|
+
);
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
function splitDiagnostics(text) {
|
|
1842
|
+
return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
|
|
1843
|
+
}
|
|
1844
|
+
function logSynchroniserDiagnostics(result, options) {
|
|
1845
|
+
for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
|
|
1846
|
+
}
|
|
1847
|
+
function parseSingleQuotedAssignment(line) {
|
|
1848
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
|
|
1849
|
+
if (!match || !match[2].startsWith("'")) return null;
|
|
1850
|
+
const valueSource = match[2];
|
|
1851
|
+
let value = "";
|
|
1852
|
+
for (let index = 1; index < valueSource.length; index++) {
|
|
1853
|
+
const character = valueSource[index];
|
|
1854
|
+
if (character !== "'") {
|
|
1855
|
+
value += character;
|
|
1856
|
+
continue;
|
|
1857
|
+
}
|
|
1858
|
+
if (index === valueSource.length - 1) return [match[1], value];
|
|
1859
|
+
if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
|
|
1860
|
+
value += "'";
|
|
1861
|
+
index += 3;
|
|
1862
|
+
}
|
|
1863
|
+
return null;
|
|
1864
|
+
}
|
|
1865
|
+
function parseSynchroniserEnv(stdout) {
|
|
1866
|
+
const values = {};
|
|
1867
|
+
for (const line of stdout.split("\n")) {
|
|
1868
|
+
if (line.trim() === "") continue;
|
|
1869
|
+
const assignment = parseSingleQuotedAssignment(line);
|
|
1870
|
+
if (!assignment) return null;
|
|
1871
|
+
values[assignment[0]] = assignment[1];
|
|
1872
|
+
}
|
|
1873
|
+
return values;
|
|
1874
|
+
}
|
|
1875
|
+
function runCommand(command, args, options) {
|
|
1876
|
+
return new Promise((resolve4) => {
|
|
1877
|
+
let child;
|
|
1878
|
+
let stdout = "";
|
|
1879
|
+
let stderr = "";
|
|
1880
|
+
let settled = false;
|
|
1881
|
+
const finish = (result) => {
|
|
1882
|
+
if (settled) return;
|
|
1883
|
+
settled = true;
|
|
1884
|
+
if (timer) clearTimeout(timer);
|
|
1885
|
+
resolve4(result);
|
|
1886
|
+
};
|
|
1887
|
+
try {
|
|
1888
|
+
child = spawn2(command, args, {
|
|
1889
|
+
env: options.env,
|
|
1890
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1891
|
+
});
|
|
1892
|
+
} catch (error2) {
|
|
1893
|
+
resolve4({
|
|
1894
|
+
code: null,
|
|
1895
|
+
stdout,
|
|
1896
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
1897
|
+
timedOut: false
|
|
1898
|
+
});
|
|
1899
|
+
return;
|
|
1900
|
+
}
|
|
1901
|
+
child.stdout?.setEncoding("utf8");
|
|
1902
|
+
child.stdout?.on("data", (chunk) => {
|
|
1903
|
+
stdout += chunk;
|
|
1904
|
+
});
|
|
1905
|
+
child.stderr?.setEncoding("utf8");
|
|
1906
|
+
child.stderr?.on("data", (chunk) => {
|
|
1907
|
+
stderr += chunk;
|
|
1908
|
+
});
|
|
1909
|
+
child.once("error", (error2) => {
|
|
1910
|
+
finish({
|
|
1911
|
+
code: null,
|
|
1912
|
+
stdout,
|
|
1913
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
1914
|
+
${error2.message}`,
|
|
1915
|
+
timedOut: false
|
|
1916
|
+
});
|
|
1917
|
+
});
|
|
1918
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
1919
|
+
const timer = setTimeout(
|
|
1920
|
+
() => {
|
|
1921
|
+
child.kill("SIGKILL");
|
|
1922
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1923
|
+
},
|
|
1924
|
+
Math.max(0, options.timeoutMs)
|
|
1925
|
+
);
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1928
|
+
async function ensureLitestreamConfig(options, env) {
|
|
1929
|
+
const configPath = options.litestreamConfig;
|
|
1930
|
+
if (!configPath) {
|
|
1931
|
+
markNoReplicate(options, "no Litestream configuration path was provided");
|
|
1932
|
+
reportRecord(
|
|
1933
|
+
"restore",
|
|
1934
|
+
"restore_misconfigured",
|
|
1935
|
+
"litestream_config_unavailable",
|
|
1936
|
+
null,
|
|
1937
|
+
options
|
|
1938
|
+
);
|
|
1939
|
+
return null;
|
|
1940
|
+
}
|
|
1941
|
+
try {
|
|
1942
|
+
if (statSync2(configPath).size > 0) return configPath;
|
|
1943
|
+
} catch (error2) {
|
|
1944
|
+
if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
|
|
1945
|
+
options.log(
|
|
1946
|
+
`Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1947
|
+
"warn"
|
|
1948
|
+
);
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
const rendered = await runSynchroniser(["litestream-config"], {
|
|
1952
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
1953
|
+
env
|
|
1954
|
+
});
|
|
1955
|
+
logSynchroniserDiagnostics(rendered, options);
|
|
1956
|
+
if (rendered.timedOut || rendered.code !== 0) {
|
|
1957
|
+
options.log(
|
|
1958
|
+
`Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
|
|
1959
|
+
"error"
|
|
1960
|
+
);
|
|
1961
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1962
|
+
reportRecord(
|
|
1963
|
+
"restore",
|
|
1964
|
+
"restore_misconfigured",
|
|
1965
|
+
"litestream_config_unavailable",
|
|
1966
|
+
null,
|
|
1967
|
+
options
|
|
1968
|
+
);
|
|
1969
|
+
return null;
|
|
1970
|
+
}
|
|
1971
|
+
try {
|
|
1972
|
+
mkdirSync(dirname2(configPath), { recursive: true });
|
|
1973
|
+
writeFileSync(configPath, rendered.stdout);
|
|
1974
|
+
} catch (error2) {
|
|
1975
|
+
options.log(
|
|
1976
|
+
`Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1977
|
+
"error"
|
|
1978
|
+
);
|
|
1979
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1980
|
+
reportRecord(
|
|
1981
|
+
"restore",
|
|
1982
|
+
"restore_misconfigured",
|
|
1983
|
+
"litestream_config_unavailable",
|
|
1984
|
+
null,
|
|
1985
|
+
options
|
|
1986
|
+
);
|
|
1987
|
+
return null;
|
|
1988
|
+
}
|
|
1989
|
+
const version2 = await runCommand("litestream", ["version"], {
|
|
1990
|
+
env,
|
|
1991
|
+
timeoutMs: 1e4
|
|
1992
|
+
});
|
|
1993
|
+
const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
|
|
1994
|
+
const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
|
|
1995
|
+
options.log(
|
|
1996
|
+
`litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
|
|
1997
|
+
);
|
|
1998
|
+
return configPath;
|
|
1999
|
+
}
|
|
2000
|
+
function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
|
|
2001
|
+
discardSessionDbDebris(options);
|
|
2002
|
+
markNoReplicate(options, message);
|
|
2003
|
+
reportRecord("restore", outcome, reason, litestreamExitCode, options);
|
|
2004
|
+
}
|
|
2005
|
+
async function restoreSessionDb(options, configPath, env) {
|
|
2006
|
+
const restored = await runCommand(
|
|
2007
|
+
"litestream",
|
|
2008
|
+
["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
|
|
2009
|
+
{ env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
|
|
2010
|
+
);
|
|
2011
|
+
for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
|
|
2012
|
+
if (restored.timedOut || restored.code === 124 || restored.code === 137) {
|
|
2013
|
+
restoreGiveUp(
|
|
2014
|
+
options,
|
|
2015
|
+
"restore_deadline_exceeded",
|
|
2016
|
+
`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`,
|
|
2017
|
+
restored.code ?? 124
|
|
2018
|
+
);
|
|
2019
|
+
return;
|
|
2020
|
+
}
|
|
2021
|
+
if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
|
|
2022
|
+
options.log(`litestream restore could not run (${commandError(restored)})`, "error");
|
|
2023
|
+
restoreGiveUp(
|
|
2024
|
+
options,
|
|
2025
|
+
"restore_tool_unusable",
|
|
2026
|
+
`restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
|
|
2027
|
+
restored.code
|
|
2028
|
+
);
|
|
2029
|
+
return;
|
|
2030
|
+
}
|
|
2031
|
+
const classified = await runSynchroniser(
|
|
2032
|
+
[
|
|
2033
|
+
"session-db-classify",
|
|
2034
|
+
String(restored.code ?? 1),
|
|
2035
|
+
"1",
|
|
2036
|
+
"--on-unusable-replica=leave",
|
|
2037
|
+
"--fresh-db-fallback"
|
|
2038
|
+
],
|
|
2039
|
+
{ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
|
|
2040
|
+
);
|
|
2041
|
+
logSynchroniserDiagnostics(classified, options);
|
|
2042
|
+
const classifyCode = classified.code;
|
|
2043
|
+
switch (classifyCode) {
|
|
2044
|
+
case 0:
|
|
2045
|
+
return;
|
|
2046
|
+
case 31:
|
|
2047
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2048
|
+
options.log(
|
|
2049
|
+
"SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
|
|
2050
|
+
"warn"
|
|
2051
|
+
);
|
|
2052
|
+
return;
|
|
2053
|
+
case 32:
|
|
2054
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2055
|
+
discardSessionDbDebris(options);
|
|
2056
|
+
markNoReplicate(
|
|
2057
|
+
options,
|
|
2058
|
+
"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"
|
|
2059
|
+
);
|
|
2060
|
+
return;
|
|
2061
|
+
case 30:
|
|
2062
|
+
restoreGiveUp(
|
|
2063
|
+
options,
|
|
2064
|
+
"classification_fatal",
|
|
2065
|
+
"session-db-classify returned fatal (30); see the FATAL message above",
|
|
2066
|
+
restored.code,
|
|
2067
|
+
"restore_misconfigured"
|
|
2068
|
+
);
|
|
2069
|
+
return;
|
|
2070
|
+
default:
|
|
2071
|
+
restoreGiveUp(
|
|
2072
|
+
options,
|
|
2073
|
+
"classification_unrecognised",
|
|
2074
|
+
`session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
|
|
2075
|
+
restored.code
|
|
2076
|
+
);
|
|
2077
|
+
}
|
|
2078
|
+
}
|
|
2079
|
+
async function verifySessionDb(options, configPath, env) {
|
|
2080
|
+
if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
|
|
2081
|
+
const result = await runSynchroniser(["session-db-verify", configPath], {
|
|
2082
|
+
timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
|
|
2083
|
+
env: {
|
|
2084
|
+
...env,
|
|
2085
|
+
// The synchroniser reads this value in SECONDS. Keep this at 120, not
|
|
2086
|
+
// 120_000, so the walkback gives up before the outer process bound.
|
|
2087
|
+
EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
|
|
2088
|
+
SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
|
|
2089
|
+
)
|
|
2090
|
+
}
|
|
2091
|
+
});
|
|
2092
|
+
logSynchroniserDiagnostics(result, options);
|
|
2093
|
+
if (result.timedOut || result.code === 124 || result.code === 137) {
|
|
2094
|
+
options.log(
|
|
2095
|
+
`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`,
|
|
2096
|
+
"warn"
|
|
2097
|
+
);
|
|
2098
|
+
return false;
|
|
2099
|
+
}
|
|
2100
|
+
if (result.code === 34) {
|
|
2101
|
+
reportRecord(
|
|
2102
|
+
"verify",
|
|
2103
|
+
"session_db_boot_refused",
|
|
2104
|
+
result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
|
|
2105
|
+
null,
|
|
2106
|
+
options
|
|
2107
|
+
);
|
|
2108
|
+
return true;
|
|
2109
|
+
}
|
|
2110
|
+
if (result.code === 33) {
|
|
2111
|
+
options.log(
|
|
2112
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
|
|
2113
|
+
"warn"
|
|
2114
|
+
);
|
|
2115
|
+
return false;
|
|
2116
|
+
}
|
|
2117
|
+
if (result.code !== 0) {
|
|
2118
|
+
options.log(
|
|
2119
|
+
`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`,
|
|
2120
|
+
"warn"
|
|
2121
|
+
);
|
|
2122
|
+
}
|
|
2123
|
+
return false;
|
|
2124
|
+
}
|
|
2125
|
+
options.log(
|
|
2126
|
+
"skipping session-DB verification: this boot's session DB was not proven safe to replicate",
|
|
2127
|
+
"debug"
|
|
2128
|
+
);
|
|
2129
|
+
return false;
|
|
2130
|
+
}
|
|
2131
|
+
function fileExists(path) {
|
|
2132
|
+
try {
|
|
2133
|
+
statSync2(path);
|
|
2134
|
+
return true;
|
|
2135
|
+
} catch (error2) {
|
|
2136
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
|
|
2137
|
+
return true;
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
async function restoreAndVerifySessionDb(options) {
|
|
2141
|
+
const env = options.env ?? process.env;
|
|
2142
|
+
clearMarker(options);
|
|
2143
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2144
|
+
const synchroniserEnv = await runSynchroniser(["env"], {
|
|
2145
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
2146
|
+
env
|
|
2147
|
+
});
|
|
2148
|
+
logSynchroniserDiagnostics(synchroniserEnv, options);
|
|
2149
|
+
if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
|
|
2150
|
+
options.log(
|
|
2151
|
+
`runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
|
|
2152
|
+
"error"
|
|
2153
|
+
);
|
|
2154
|
+
markNoReplicate(
|
|
2155
|
+
options,
|
|
2156
|
+
"could not resolve the runner-synchroniser configuration (see the ERROR above)"
|
|
2157
|
+
);
|
|
2158
|
+
reportRecord(
|
|
2159
|
+
"restore",
|
|
2160
|
+
"restore_misconfigured",
|
|
2161
|
+
"synchroniser_config_unresolved",
|
|
2162
|
+
null,
|
|
2163
|
+
options
|
|
2164
|
+
);
|
|
2165
|
+
return { verifyFatal: false };
|
|
2166
|
+
}
|
|
2167
|
+
const values = parseSynchroniserEnv(synchroniserEnv.stdout);
|
|
2168
|
+
if (!values) {
|
|
2169
|
+
markNoReplicate(
|
|
2170
|
+
options,
|
|
2171
|
+
"the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
|
|
2172
|
+
);
|
|
2173
|
+
reportRecord(
|
|
2174
|
+
"restore",
|
|
2175
|
+
"restore_misconfigured",
|
|
2176
|
+
"synchroniser_config_unevaluable",
|
|
2177
|
+
null,
|
|
2178
|
+
options
|
|
2179
|
+
);
|
|
2180
|
+
return { verifyFatal: false };
|
|
2181
|
+
}
|
|
2182
|
+
const synchroniserDbPath = values.OPENCODE_DB_PATH;
|
|
2183
|
+
if (!synchroniserDbPath) {
|
|
2184
|
+
markNoReplicate(
|
|
2185
|
+
options,
|
|
2186
|
+
"run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
|
|
2187
|
+
);
|
|
2188
|
+
reportRecord(
|
|
2189
|
+
"restore",
|
|
2190
|
+
"restore_misconfigured",
|
|
2191
|
+
"synchroniser_config_incomplete",
|
|
2192
|
+
null,
|
|
2193
|
+
options
|
|
2194
|
+
);
|
|
2195
|
+
return { verifyFatal: false };
|
|
2196
|
+
}
|
|
2197
|
+
if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
|
|
2198
|
+
options.log(
|
|
2199
|
+
`runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
|
|
2200
|
+
"warn"
|
|
2201
|
+
);
|
|
2202
|
+
}
|
|
2203
|
+
if (!values.PERSISTENCE_BUCKET) {
|
|
2204
|
+
options.log(
|
|
2205
|
+
"SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
|
|
2206
|
+
"warn"
|
|
2207
|
+
);
|
|
2208
|
+
return { verifyFatal: false };
|
|
2209
|
+
}
|
|
2210
|
+
const configPath = await ensureLitestreamConfig(options, env);
|
|
2211
|
+
if (!configPath) return { verifyFatal: false };
|
|
2212
|
+
await restoreSessionDb(options, configPath, env);
|
|
2213
|
+
if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
|
|
2214
|
+
return { verifyFatal: false };
|
|
2215
|
+
}
|
|
2216
|
+
return { verifyFatal: await verifySessionDb(options, configPath, env) };
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
// src/lib/opencode/session-db-provenance.ts
|
|
2220
|
+
import { createRequire } from "module";
|
|
2221
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
2222
|
+
import { dirname as dirname3, join as join3 } from "path";
|
|
2223
|
+
var require2 = createRequire(import.meta.url);
|
|
2224
|
+
function readSessionDbMigrationIds(dbPath) {
|
|
2225
|
+
let db;
|
|
2226
|
+
try {
|
|
2227
|
+
const { DatabaseSync } = require2("node:sqlite");
|
|
2228
|
+
db = new DatabaseSync(dbPath, { readOnly: true });
|
|
2229
|
+
const columns = db.prepare("PRAGMA table_info(migration)").all();
|
|
2230
|
+
const hasExpectedShape = columns.length === 2 && columns.some(
|
|
2231
|
+
(column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
|
|
2232
|
+
) && columns.some(
|
|
2233
|
+
(column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
|
|
2234
|
+
);
|
|
2235
|
+
if (!hasExpectedShape) {
|
|
2236
|
+
console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
|
|
2237
|
+
return null;
|
|
2238
|
+
}
|
|
2239
|
+
const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
|
|
2240
|
+
if (rows.some((row) => typeof row.id !== "string")) return null;
|
|
2241
|
+
return rows.map((row) => row.id);
|
|
2242
|
+
} catch (error2) {
|
|
2243
|
+
console.warn(
|
|
2244
|
+
`[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2245
|
+
);
|
|
2246
|
+
return null;
|
|
2247
|
+
} finally {
|
|
2248
|
+
try {
|
|
2249
|
+
db?.close();
|
|
2250
|
+
} catch (error2) {
|
|
2251
|
+
console.warn(
|
|
2252
|
+
`[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2253
|
+
);
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
function sessionDbProvenanceStatePath(homeDir, env) {
|
|
2258
|
+
const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
|
|
2259
|
+
return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
|
|
2260
|
+
}
|
|
2261
|
+
function loadSessionDbProvenanceState(path) {
|
|
2262
|
+
let value;
|
|
2263
|
+
try {
|
|
2264
|
+
value = JSON.parse(readFileSync3(path, "utf8"));
|
|
2265
|
+
} catch (error2) {
|
|
2266
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
|
|
2267
|
+
console.error(
|
|
2268
|
+
`[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2269
|
+
);
|
|
2270
|
+
return {};
|
|
2271
|
+
}
|
|
2272
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
2273
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2274
|
+
return {};
|
|
2275
|
+
}
|
|
2276
|
+
const state = {};
|
|
2277
|
+
for (const [dbPath, record] of Object.entries(value)) {
|
|
2278
|
+
if (!isSessionDbProvenanceRecord(record)) {
|
|
2279
|
+
console.error(`[session-db-provenance] ignored malformed state in ${path}`);
|
|
2280
|
+
return {};
|
|
2281
|
+
}
|
|
2282
|
+
state[dbPath] = record;
|
|
2283
|
+
}
|
|
2284
|
+
return state;
|
|
2285
|
+
}
|
|
2286
|
+
function saveSessionDbProvenanceState(path, state) {
|
|
2287
|
+
try {
|
|
2288
|
+
mkdirSync2(dirname3(path), { recursive: true });
|
|
2289
|
+
writeFileSync2(path, `${JSON.stringify(state, null, 2)}
|
|
2290
|
+
`, "utf8");
|
|
2291
|
+
} catch (error2) {
|
|
2292
|
+
console.error(
|
|
2293
|
+
`[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
2294
|
+
);
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
function evaluateSessionDbProvenance(input) {
|
|
2298
|
+
const { currentVersion, currentIds, previous } = input;
|
|
2299
|
+
if (!previous) return { anomaly: false, reason: null };
|
|
2300
|
+
const current = new Set(currentIds);
|
|
2301
|
+
const prior = new Set(previous.migrationIds);
|
|
2302
|
+
for (const id of prior) {
|
|
2303
|
+
if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
|
|
2304
|
+
}
|
|
2305
|
+
if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
|
|
2306
|
+
return { anomaly: true, reason: "foreign-version-migrations" };
|
|
2307
|
+
}
|
|
2308
|
+
return { anomaly: false, reason: null };
|
|
2309
|
+
}
|
|
2310
|
+
function checkSessionDbProvenance(input) {
|
|
2311
|
+
const { dbPath, currentVersion, homeDir, env } = input;
|
|
2312
|
+
const path = sessionDbProvenanceStatePath(homeDir, env);
|
|
2313
|
+
const state = loadSessionDbProvenanceState(path);
|
|
2314
|
+
const previous = state[dbPath];
|
|
2315
|
+
const currentIds = readSessionDbMigrationIds(dbPath);
|
|
2316
|
+
if (currentIds === null) {
|
|
2317
|
+
return {
|
|
2318
|
+
anomaly: false,
|
|
2319
|
+
reason: null,
|
|
2320
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2321
|
+
migrationDelta: null
|
|
2322
|
+
};
|
|
2323
|
+
}
|
|
2324
|
+
const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
|
|
2325
|
+
const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
|
|
2326
|
+
state[dbPath] = {
|
|
2327
|
+
opencodeVersion: currentVersion,
|
|
2328
|
+
migrationIds: currentIds,
|
|
2329
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2330
|
+
};
|
|
2331
|
+
saveSessionDbProvenanceState(path, state);
|
|
2332
|
+
return {
|
|
2333
|
+
...decision,
|
|
2334
|
+
recordedVersion: previous?.opencodeVersion ?? null,
|
|
2335
|
+
migrationDelta
|
|
2336
|
+
};
|
|
2337
|
+
}
|
|
2338
|
+
function isSessionDbProvenanceRecord(value) {
|
|
2339
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
2340
|
+
const record = value;
|
|
2341
|
+
return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
|
|
2342
|
+
}
|
|
2343
|
+
|
|
1669
2344
|
// src/lib/opencode/opencode-version-gate.ts
|
|
1670
2345
|
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
1671
2346
|
function isQueueValidatedVersion(version2) {
|
|
@@ -1680,7 +2355,7 @@ function buildOpenCodeVersionWarning(version2) {
|
|
|
1680
2355
|
}
|
|
1681
2356
|
|
|
1682
2357
|
// src/lib/opencode/process.ts
|
|
1683
|
-
import { execSync, spawn } from "child_process";
|
|
2358
|
+
import { execSync, spawn as spawn3 } from "child_process";
|
|
1684
2359
|
|
|
1685
2360
|
// src/lib/process-stop.ts
|
|
1686
2361
|
async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
@@ -1690,7 +2365,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
|
1690
2365
|
if (child.exitCode !== null || child.signalCode !== null) {
|
|
1691
2366
|
return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
|
|
1692
2367
|
}
|
|
1693
|
-
return new Promise((
|
|
2368
|
+
return new Promise((resolve4, reject) => {
|
|
1694
2369
|
let forced = false;
|
|
1695
2370
|
let settled = false;
|
|
1696
2371
|
const timer = setTimeout(() => {
|
|
@@ -1710,7 +2385,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
|
1710
2385
|
settled = true;
|
|
1711
2386
|
clearTimeout(timer);
|
|
1712
2387
|
child.removeListener("exit", onExit);
|
|
1713
|
-
|
|
2388
|
+
resolve4(result);
|
|
1714
2389
|
};
|
|
1715
2390
|
const fail = (error2) => {
|
|
1716
2391
|
if (settled) return;
|
|
@@ -1892,18 +2567,27 @@ async function findHealthyOpenCodeInstances() {
|
|
|
1892
2567
|
}
|
|
1893
2568
|
return healthy;
|
|
1894
2569
|
}
|
|
1895
|
-
async function startOpenCode(port) {
|
|
2570
|
+
async function startOpenCode(port, options = {}) {
|
|
1896
2571
|
let command = "opencode";
|
|
1897
|
-
|
|
2572
|
+
const printLogs = options.inheritStdio ? ["--print-logs"] : [];
|
|
2573
|
+
let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
|
|
1898
2574
|
try {
|
|
1899
2575
|
execSync("which opencode", { stdio: "ignore" });
|
|
1900
2576
|
} catch {
|
|
1901
2577
|
command = "npx";
|
|
1902
|
-
args = [
|
|
1903
|
-
|
|
1904
|
-
|
|
2578
|
+
args = [
|
|
2579
|
+
"opencode",
|
|
2580
|
+
"serve",
|
|
2581
|
+
"--port",
|
|
2582
|
+
port.toString(),
|
|
2583
|
+
"--hostname",
|
|
2584
|
+
"127.0.0.1",
|
|
2585
|
+
...printLogs
|
|
2586
|
+
];
|
|
2587
|
+
}
|
|
2588
|
+
const child = spawn3(command, args, {
|
|
1905
2589
|
detached: true,
|
|
1906
|
-
stdio: "ignore",
|
|
2590
|
+
stdio: options.inheritStdio ? "inherit" : "ignore",
|
|
1907
2591
|
cwd: process.cwd()
|
|
1908
2592
|
});
|
|
1909
2593
|
return child;
|
|
@@ -2389,7 +3073,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2389
3073
|
}
|
|
2390
3074
|
}
|
|
2391
3075
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
2392
|
-
await new Promise((
|
|
3076
|
+
await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
|
|
2393
3077
|
}
|
|
2394
3078
|
}
|
|
2395
3079
|
return null;
|
|
@@ -2742,13 +3426,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
|
2742
3426
|
}
|
|
2743
3427
|
|
|
2744
3428
|
// src/lib/opencode/session-db-size.ts
|
|
2745
|
-
import { statSync as
|
|
2746
|
-
import { join as
|
|
3429
|
+
import { statSync as statSync3 } from "fs";
|
|
3430
|
+
import { join as join4 } from "path";
|
|
2747
3431
|
var LARGE_DB_THRESHOLD_BYTES = 268435456;
|
|
2748
3432
|
function statSessionDbBytes(homeDir) {
|
|
2749
|
-
const dbPath =
|
|
3433
|
+
const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
|
|
2750
3434
|
try {
|
|
2751
|
-
return
|
|
3435
|
+
return statSync3(dbPath).size;
|
|
2752
3436
|
} catch (err) {
|
|
2753
3437
|
const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
|
|
2754
3438
|
if (!isMissingFile) {
|
|
@@ -2774,11 +3458,11 @@ function buildSessionStoreSizeWarning(input) {
|
|
|
2774
3458
|
}
|
|
2775
3459
|
|
|
2776
3460
|
// src/lib/opencode/session-db-reclaim.ts
|
|
2777
|
-
import { statSync as
|
|
2778
|
-
import { dirname as
|
|
3461
|
+
import { statSync as statSync4, statfsSync } from "fs";
|
|
3462
|
+
import { dirname as dirname4 } from "path";
|
|
2779
3463
|
function insufficientSpaceReason(dbPath, requiredBytes) {
|
|
2780
3464
|
try {
|
|
2781
|
-
const fsStats = statfsSync(
|
|
3465
|
+
const fsStats = statfsSync(dirname4(dbPath));
|
|
2782
3466
|
const availableBytes = fsStats.bavail * fsStats.bsize;
|
|
2783
3467
|
if (availableBytes < requiredBytes) {
|
|
2784
3468
|
return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
|
|
@@ -2847,7 +3531,7 @@ async function reclaimSessionDbSpace(input) {
|
|
|
2847
3531
|
);
|
|
2848
3532
|
return { ok: false, skipped: "full-vacuum-blocked" };
|
|
2849
3533
|
}
|
|
2850
|
-
const fileBytesForGuard =
|
|
3534
|
+
const fileBytesForGuard = statSync4(dbPath).size;
|
|
2851
3535
|
const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
|
|
2852
3536
|
if (skipReason !== null) {
|
|
2853
3537
|
console.warn(
|
|
@@ -2975,12 +3659,12 @@ var StreamForwarder = class {
|
|
|
2975
3659
|
let endBody;
|
|
2976
3660
|
if (has_body) {
|
|
2977
3661
|
const chunks = [];
|
|
2978
|
-
bodyPromise = new Promise((
|
|
3662
|
+
bodyPromise = new Promise((resolve4) => {
|
|
2979
3663
|
pushBody = (buf) => {
|
|
2980
3664
|
chunks.push(buf);
|
|
2981
3665
|
};
|
|
2982
3666
|
endBody = () => {
|
|
2983
|
-
|
|
3667
|
+
resolve4(Buffer.concat(chunks));
|
|
2984
3668
|
};
|
|
2985
3669
|
});
|
|
2986
3670
|
}
|
|
@@ -3109,7 +3793,7 @@ function connectTunnel(options) {
|
|
|
3109
3793
|
} = options;
|
|
3110
3794
|
const tunnelUrl = getTunnelUrlConfig();
|
|
3111
3795
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
3112
|
-
return new Promise((
|
|
3796
|
+
return new Promise((resolve4, reject) => {
|
|
3113
3797
|
const ws = new WebSocket2(url, {
|
|
3114
3798
|
headers: {
|
|
3115
3799
|
Authorization: authHeader
|
|
@@ -3173,7 +3857,7 @@ function connectTunnel(options) {
|
|
|
3173
3857
|
clearTimeout(connectionTimeout);
|
|
3174
3858
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
3175
3859
|
onConnected?.(connectedAgentId);
|
|
3176
|
-
|
|
3860
|
+
resolve4({
|
|
3177
3861
|
ws,
|
|
3178
3862
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
3179
3863
|
});
|
|
@@ -3304,10 +3988,10 @@ var RunnerConnection = class {
|
|
|
3304
3988
|
};
|
|
3305
3989
|
|
|
3306
3990
|
// src/lib/tunnel/ready-marker.ts
|
|
3307
|
-
import { writeFileSync } from "fs";
|
|
3991
|
+
import { writeFileSync as writeFileSync3 } from "fs";
|
|
3308
3992
|
function writeTunnelReadyMarker(path, agentId) {
|
|
3309
3993
|
try {
|
|
3310
|
-
|
|
3994
|
+
writeFileSync3(path, `${agentId}
|
|
3311
3995
|
`);
|
|
3312
3996
|
return { ok: true };
|
|
3313
3997
|
} catch (error2) {
|
|
@@ -3316,9 +4000,9 @@ function writeTunnelReadyMarker(path, agentId) {
|
|
|
3316
4000
|
}
|
|
3317
4001
|
|
|
3318
4002
|
// src/lib/replication.ts
|
|
3319
|
-
import { spawn as
|
|
4003
|
+
import { spawn as spawn4 } from "child_process";
|
|
3320
4004
|
function startSessionDbReplication(configPath) {
|
|
3321
|
-
return
|
|
4005
|
+
return spawn4("litestream", ["replicate", "-config", configPath], {
|
|
3322
4006
|
stdio: "inherit"
|
|
3323
4007
|
});
|
|
3324
4008
|
}
|
|
@@ -3331,10 +4015,36 @@ async function stopSessionDbReplication(child, timeoutMs) {
|
|
|
3331
4015
|
);
|
|
3332
4016
|
}
|
|
3333
4017
|
|
|
4018
|
+
// src/lib/process-liveness.ts
|
|
4019
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
4020
|
+
function isProcessAlive(pid) {
|
|
4021
|
+
try {
|
|
4022
|
+
process.kill(pid, 0);
|
|
4023
|
+
} catch (error2) {
|
|
4024
|
+
const code = error2.code;
|
|
4025
|
+
if (code === "ESRCH") return false;
|
|
4026
|
+
if (code === "EPERM") return true;
|
|
4027
|
+
console.error(
|
|
4028
|
+
`[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
4029
|
+
);
|
|
4030
|
+
return false;
|
|
4031
|
+
}
|
|
4032
|
+
if (process.platform !== "linux") return true;
|
|
4033
|
+
try {
|
|
4034
|
+
const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
|
|
4035
|
+
return !/^State:\s+Z(?:\s|$)/m.test(status2);
|
|
4036
|
+
} catch (error2) {
|
|
4037
|
+
console.error(
|
|
4038
|
+
`[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
4039
|
+
);
|
|
4040
|
+
return true;
|
|
4041
|
+
}
|
|
4042
|
+
}
|
|
4043
|
+
|
|
3334
4044
|
// src/lib/openai-usage.ts
|
|
3335
|
-
import { readFileSync as
|
|
3336
|
-
import { homedir as
|
|
3337
|
-
import { join as
|
|
4045
|
+
import { readFileSync as readFileSync5 } from "fs";
|
|
4046
|
+
import { homedir as homedir3 } from "os";
|
|
4047
|
+
import { join as join5 } from "path";
|
|
3338
4048
|
var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
3339
4049
|
var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
|
|
3340
4050
|
var OpenAiUsageError = class extends Error {
|
|
@@ -3348,7 +4058,7 @@ function isLocalCredentialProblem2(err) {
|
|
|
3348
4058
|
}
|
|
3349
4059
|
function readOpenCodeChatGptCredentials() {
|
|
3350
4060
|
try {
|
|
3351
|
-
const raw =
|
|
4061
|
+
const raw = readFileSync5(join5(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
|
|
3352
4062
|
let parsed;
|
|
3353
4063
|
try {
|
|
3354
4064
|
parsed = JSON.parse(raw);
|
|
@@ -3721,15 +4431,15 @@ function createResourceUsageCollector(homeDir) {
|
|
|
3721
4431
|
}
|
|
3722
4432
|
|
|
3723
4433
|
// src/lib/channels/driver.ts
|
|
3724
|
-
import { homedir as
|
|
4434
|
+
import { homedir as homedir4 } from "os";
|
|
3725
4435
|
|
|
3726
4436
|
// src/lib/runner-file-sync.ts
|
|
3727
|
-
import { join as
|
|
4437
|
+
import { join as join7 } from "path";
|
|
3728
4438
|
|
|
3729
4439
|
// src/lib/file-push.ts
|
|
3730
4440
|
import { randomUUID } from "crypto";
|
|
3731
4441
|
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
3732
|
-
import { basename, dirname as
|
|
4442
|
+
import { basename, dirname as dirname5, isAbsolute, join as join6, relative, resolve as resolve2, sep } from "path";
|
|
3733
4443
|
var FILE_MODE = 384;
|
|
3734
4444
|
var DIRECTORY_MODE = 448;
|
|
3735
4445
|
async function writePushedFile(request) {
|
|
@@ -3760,9 +4470,9 @@ async function writePushedFile(request) {
|
|
|
3760
4470
|
}
|
|
3761
4471
|
try {
|
|
3762
4472
|
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
3763
|
-
|
|
4473
|
+
dirname5(candidate)
|
|
3764
4474
|
);
|
|
3765
|
-
const realTarget =
|
|
4475
|
+
const realTarget = join6(existingAncestor, ...missingSegments, basename(candidate));
|
|
3766
4476
|
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
3767
4477
|
if (allowedDirectory === null) {
|
|
3768
4478
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
@@ -3772,8 +4482,8 @@ async function writePushedFile(request) {
|
|
|
3772
4482
|
}
|
|
3773
4483
|
if (missingSegments.length > 0) {
|
|
3774
4484
|
await createMissingDirectories(existingAncestor, missingSegments);
|
|
3775
|
-
const realParent = await realpath(
|
|
3776
|
-
if (realParent !==
|
|
4485
|
+
const realParent = await realpath(dirname5(realTarget));
|
|
4486
|
+
if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
3777
4487
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
3778
4488
|
path: realTarget,
|
|
3779
4489
|
bytes,
|
|
@@ -3798,7 +4508,7 @@ function expandAndValidate(requestedPath, homeDir) {
|
|
|
3798
4508
|
if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
|
|
3799
4509
|
return null;
|
|
3800
4510
|
}
|
|
3801
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4511
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
3802
4512
|
if (expanded.split(/[/\\]/).includes("..")) {
|
|
3803
4513
|
return null;
|
|
3804
4514
|
}
|
|
@@ -3816,7 +4526,7 @@ async function resolveNearestExistingAncestor(directory) {
|
|
|
3816
4526
|
try {
|
|
3817
4527
|
return { existingAncestor: await realpath(current), missingSegments };
|
|
3818
4528
|
} catch (err) {
|
|
3819
|
-
const parent =
|
|
4529
|
+
const parent = dirname5(current);
|
|
3820
4530
|
if (err.code !== "ENOENT" || parent === current) {
|
|
3821
4531
|
throw err;
|
|
3822
4532
|
}
|
|
@@ -3871,13 +4581,13 @@ function contains(realDirectory, realTarget) {
|
|
|
3871
4581
|
async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
3872
4582
|
let current = existingAncestor;
|
|
3873
4583
|
for (const segment of missingSegments) {
|
|
3874
|
-
current =
|
|
4584
|
+
current = join6(current, segment);
|
|
3875
4585
|
await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
|
|
3876
4586
|
await chmod(current, DIRECTORY_MODE);
|
|
3877
4587
|
}
|
|
3878
4588
|
}
|
|
3879
4589
|
async function writeAtomically(realTarget, content) {
|
|
3880
|
-
const temporaryPath =
|
|
4590
|
+
const temporaryPath = join6(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
3881
4591
|
let handle;
|
|
3882
4592
|
try {
|
|
3883
4593
|
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
@@ -4007,12 +4717,12 @@ var NOT_APPLIED = {
|
|
|
4007
4717
|
opencodeAuthApplied: false
|
|
4008
4718
|
};
|
|
4009
4719
|
function isClaudeCredentialPath(requestedPath, homeDir) {
|
|
4010
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4011
|
-
return expanded ===
|
|
4720
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4721
|
+
return expanded === join7(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
|
|
4012
4722
|
}
|
|
4013
4723
|
function isOpenCodeAuthPath(requestedPath, homeDir) {
|
|
4014
|
-
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ?
|
|
4015
|
-
return expanded ===
|
|
4724
|
+
const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
|
|
4725
|
+
return expanded === join7(homeDir, ...OPENCODE_AUTH_SEGMENTS);
|
|
4016
4726
|
}
|
|
4017
4727
|
async function applyOne(options, file) {
|
|
4018
4728
|
const label = `${file.id.slice(0, 8)} (${file.path})`;
|
|
@@ -4558,7 +5268,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4558
5268
|
this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
4559
5269
|
this.now = config.now ?? (() => Date.now());
|
|
4560
5270
|
this.fileSyncDirectories = config.fileSyncDirectories ?? [];
|
|
4561
|
-
this.homeDir = config.homeDir ??
|
|
5271
|
+
this.homeDir = config.homeDir ?? homedir4();
|
|
4562
5272
|
this.maxActiveSessions = config.maxActiveSessions;
|
|
4563
5273
|
this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
|
|
4564
5274
|
this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
|
|
@@ -7979,7 +8689,7 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
7979
8689
|
}
|
|
7980
8690
|
if (!ctx.interactive) {
|
|
7981
8691
|
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
7982
|
-
const proc = await startOpenCode(ctx.port);
|
|
8692
|
+
const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
|
|
7983
8693
|
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
7984
8694
|
if (!health.healthy) {
|
|
7985
8695
|
return {
|
|
@@ -8047,7 +8757,7 @@ Port ${port} is already in use.`));
|
|
|
8047
8757
|
}
|
|
8048
8758
|
if (action === "start") {
|
|
8049
8759
|
const spinner = ora2("Starting OpenCode...").start();
|
|
8050
|
-
const proc = await startOpenCode(port);
|
|
8760
|
+
const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
|
|
8051
8761
|
const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
|
|
8052
8762
|
if (!health.healthy) {
|
|
8053
8763
|
spinner.fail("Failed to start OpenCode");
|
|
@@ -8059,6 +8769,316 @@ Port ${port} is already in use.`));
|
|
|
8059
8769
|
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
8060
8770
|
}
|
|
8061
8771
|
|
|
8772
|
+
// src/lib/runner-credentials.ts
|
|
8773
|
+
import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "fs";
|
|
8774
|
+
import { spawn as spawn5 } from "child_process";
|
|
8775
|
+
var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
|
|
8776
|
+
var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
|
|
8777
|
+
var GITHUB_PROBE_TIMEOUT_MS = 1e4;
|
|
8778
|
+
var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
|
|
8779
|
+
var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
|
|
8780
|
+
function commandError2(result) {
|
|
8781
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
8782
|
+
}
|
|
8783
|
+
var runCommand2 = (command, args, opts) => {
|
|
8784
|
+
return new Promise((resolve4) => {
|
|
8785
|
+
let child;
|
|
8786
|
+
let stdout = "";
|
|
8787
|
+
let stderr = "";
|
|
8788
|
+
let settled = false;
|
|
8789
|
+
const timer = {};
|
|
8790
|
+
const finish = (result) => {
|
|
8791
|
+
if (settled) return;
|
|
8792
|
+
settled = true;
|
|
8793
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
8794
|
+
resolve4(result);
|
|
8795
|
+
};
|
|
8796
|
+
try {
|
|
8797
|
+
child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
|
|
8798
|
+
} catch (error2) {
|
|
8799
|
+
finish({
|
|
8800
|
+
code: null,
|
|
8801
|
+
stdout,
|
|
8802
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
8803
|
+
timedOut: false
|
|
8804
|
+
});
|
|
8805
|
+
return;
|
|
8806
|
+
}
|
|
8807
|
+
child.stdout?.setEncoding("utf8");
|
|
8808
|
+
child.stdout?.on("data", (chunk) => {
|
|
8809
|
+
stdout += chunk;
|
|
8810
|
+
});
|
|
8811
|
+
child.stderr?.setEncoding("utf8");
|
|
8812
|
+
child.stderr?.on("data", (chunk) => {
|
|
8813
|
+
stderr += chunk;
|
|
8814
|
+
});
|
|
8815
|
+
child.once("error", (error2) => {
|
|
8816
|
+
finish({
|
|
8817
|
+
code: null,
|
|
8818
|
+
stdout,
|
|
8819
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
8820
|
+
${error2.message}`,
|
|
8821
|
+
timedOut: false
|
|
8822
|
+
});
|
|
8823
|
+
});
|
|
8824
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
8825
|
+
timer.handle = setTimeout(
|
|
8826
|
+
() => {
|
|
8827
|
+
child.kill("SIGKILL");
|
|
8828
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
8829
|
+
},
|
|
8830
|
+
Math.max(0, opts.timeoutMs)
|
|
8831
|
+
);
|
|
8832
|
+
});
|
|
8833
|
+
};
|
|
8834
|
+
function isEnvironmentObject(value) {
|
|
8835
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8836
|
+
}
|
|
8837
|
+
function secretFailure(marker, detail, log3) {
|
|
8838
|
+
const message = `${marker}: ${detail}`;
|
|
8839
|
+
log3(message, "error");
|
|
8840
|
+
return new Error(message);
|
|
8841
|
+
}
|
|
8842
|
+
async function installRunnerSecret({
|
|
8843
|
+
env,
|
|
8844
|
+
log: log3,
|
|
8845
|
+
commandRunner
|
|
8846
|
+
}) {
|
|
8847
|
+
const arn = env.RUNNER_SECRET_ARN?.trim();
|
|
8848
|
+
if (!arn) {
|
|
8849
|
+
log3("runner secret is not configured; continuing without GitHub and MCP credentials");
|
|
8850
|
+
return false;
|
|
8851
|
+
}
|
|
8852
|
+
const result = await (commandRunner ?? runCommand2)(
|
|
8853
|
+
"aws",
|
|
8854
|
+
[
|
|
8855
|
+
"secretsmanager",
|
|
8856
|
+
"get-secret-value",
|
|
8857
|
+
"--secret-id",
|
|
8858
|
+
arn,
|
|
8859
|
+
"--query",
|
|
8860
|
+
"SecretString",
|
|
8861
|
+
"--output",
|
|
8862
|
+
"text"
|
|
8863
|
+
],
|
|
8864
|
+
{ env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
|
|
8865
|
+
);
|
|
8866
|
+
if (result.timedOut) {
|
|
8867
|
+
throw secretFailure(
|
|
8868
|
+
"CREDENTIAL-RESTORE-TIMEOUT",
|
|
8869
|
+
`runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
|
|
8870
|
+
log3
|
|
8871
|
+
);
|
|
8872
|
+
}
|
|
8873
|
+
if (result.code !== 0) {
|
|
8874
|
+
throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
|
|
8875
|
+
}
|
|
8876
|
+
let payload;
|
|
8877
|
+
try {
|
|
8878
|
+
payload = JSON.parse(result.stdout);
|
|
8879
|
+
} catch (error2) {
|
|
8880
|
+
log3(
|
|
8881
|
+
`RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
8882
|
+
"warn"
|
|
8883
|
+
);
|
|
8884
|
+
return false;
|
|
8885
|
+
}
|
|
8886
|
+
if (!isEnvironmentObject(payload)) {
|
|
8887
|
+
log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
|
|
8888
|
+
return false;
|
|
8889
|
+
}
|
|
8890
|
+
let populated = 0;
|
|
8891
|
+
let skipped = 0;
|
|
8892
|
+
let githubTokenPopulated = false;
|
|
8893
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
8894
|
+
if (typeof value !== "string" || value.length === 0) continue;
|
|
8895
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
|
|
8896
|
+
log3(
|
|
8897
|
+
`RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
|
|
8898
|
+
"warn"
|
|
8899
|
+
);
|
|
8900
|
+
skipped += 1;
|
|
8901
|
+
continue;
|
|
8902
|
+
}
|
|
8903
|
+
env[key] = value;
|
|
8904
|
+
populated += 1;
|
|
8905
|
+
if (key === "GH_TOKEN") githubTokenPopulated = true;
|
|
8906
|
+
}
|
|
8907
|
+
if (populated === 0) {
|
|
8908
|
+
log3(
|
|
8909
|
+
"RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
|
|
8910
|
+
"warn"
|
|
8911
|
+
);
|
|
8912
|
+
} else {
|
|
8913
|
+
log3(
|
|
8914
|
+
`RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
|
|
8915
|
+
);
|
|
8916
|
+
}
|
|
8917
|
+
return githubTokenPopulated;
|
|
8918
|
+
}
|
|
8919
|
+
function restoreFailure(operation, result, log3) {
|
|
8920
|
+
const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
|
|
8921
|
+
log3(message, "error");
|
|
8922
|
+
return new Error(message);
|
|
8923
|
+
}
|
|
8924
|
+
async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
|
|
8925
|
+
const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
|
|
8926
|
+
if (result.timedOut) {
|
|
8927
|
+
log3(
|
|
8928
|
+
`CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
8929
|
+
"warn"
|
|
8930
|
+
);
|
|
8931
|
+
return result;
|
|
8932
|
+
}
|
|
8933
|
+
if (result.code !== 0) throw restoreFailure(operation, result, log3);
|
|
8934
|
+
return result;
|
|
8935
|
+
}
|
|
8936
|
+
async function restoreCredentialStores({
|
|
8937
|
+
env,
|
|
8938
|
+
log: log3,
|
|
8939
|
+
synchroniserRunner = runSynchroniser
|
|
8940
|
+
}) {
|
|
8941
|
+
await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
|
|
8942
|
+
await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
|
|
8943
|
+
const result = await synchroniserRunner(["model-auth-ready"], {
|
|
8944
|
+
timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
|
|
8945
|
+
});
|
|
8946
|
+
if (result.timedOut) {
|
|
8947
|
+
log3(
|
|
8948
|
+
`CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
8949
|
+
"warn"
|
|
8950
|
+
);
|
|
8951
|
+
return;
|
|
8952
|
+
}
|
|
8953
|
+
switch (result.code) {
|
|
8954
|
+
case 0:
|
|
8955
|
+
return;
|
|
8956
|
+
case 10:
|
|
8957
|
+
log3(
|
|
8958
|
+
`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.`,
|
|
8959
|
+
"warn"
|
|
8960
|
+
);
|
|
8961
|
+
return;
|
|
8962
|
+
default:
|
|
8963
|
+
log3("could not determine whether this VM has model credentials", "warn");
|
|
8964
|
+
}
|
|
8965
|
+
}
|
|
8966
|
+
var GIT_CREDENTIAL_HELPER_CONTENT = [
|
|
8967
|
+
"#!/usr/bin/env bash",
|
|
8968
|
+
'[ "$1" = get ] || exit 0',
|
|
8969
|
+
"echo username=x-access-token",
|
|
8970
|
+
'echo "password=${GH_TOKEN}"',
|
|
8971
|
+
""
|
|
8972
|
+
].join("\n");
|
|
8973
|
+
async function probeGitHubAccess({ env, log: log3 }) {
|
|
8974
|
+
const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
|
|
8975
|
+
env,
|
|
8976
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8977
|
+
});
|
|
8978
|
+
if (auth.timedOut) {
|
|
8979
|
+
log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
|
|
8980
|
+
return;
|
|
8981
|
+
}
|
|
8982
|
+
if (auth.code !== 0) {
|
|
8983
|
+
log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
|
|
8984
|
+
return;
|
|
8985
|
+
}
|
|
8986
|
+
log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
|
|
8987
|
+
const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
|
|
8988
|
+
env,
|
|
8989
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8990
|
+
});
|
|
8991
|
+
if (remote.code !== 0 || remote.timedOut) return;
|
|
8992
|
+
const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
|
|
8993
|
+
if (!repo) return;
|
|
8994
|
+
const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
|
|
8995
|
+
env,
|
|
8996
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8997
|
+
});
|
|
8998
|
+
if (repository.timedOut) {
|
|
8999
|
+
log3(
|
|
9000
|
+
`GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
|
|
9001
|
+
"warn"
|
|
9002
|
+
);
|
|
9003
|
+
} else if (repository.code !== 0) {
|
|
9004
|
+
log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
|
|
9005
|
+
}
|
|
9006
|
+
}
|
|
9007
|
+
async function configureGitHubAccess({ env, log: log3 }) {
|
|
9008
|
+
if (!env.GH_TOKEN) {
|
|
9009
|
+
log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
|
|
9010
|
+
return;
|
|
9011
|
+
}
|
|
9012
|
+
try {
|
|
9013
|
+
env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
|
|
9014
|
+
writeFileSync4(GIT_CONFIG_GLOBAL, "");
|
|
9015
|
+
writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
|
|
9016
|
+
chmodSync2(GIT_CREDENTIAL_HELPER, 448);
|
|
9017
|
+
const config = [
|
|
9018
|
+
["user.name", env.GIT_USER_NAME ?? "evident-bot"],
|
|
9019
|
+
["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
|
|
9020
|
+
["init.defaultBranch", "main"],
|
|
9021
|
+
["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
|
|
9022
|
+
];
|
|
9023
|
+
for (const [key, value] of config) {
|
|
9024
|
+
const result = await runCommand2("git", ["config", "--global", key, value], {
|
|
9025
|
+
env,
|
|
9026
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
9027
|
+
});
|
|
9028
|
+
if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
|
|
9029
|
+
}
|
|
9030
|
+
} catch (error2) {
|
|
9031
|
+
log3(
|
|
9032
|
+
`GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
9033
|
+
"warn"
|
|
9034
|
+
);
|
|
9035
|
+
return;
|
|
9036
|
+
}
|
|
9037
|
+
void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
|
|
9038
|
+
log3(
|
|
9039
|
+
`GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
9040
|
+
"warn"
|
|
9041
|
+
);
|
|
9042
|
+
});
|
|
9043
|
+
}
|
|
9044
|
+
|
|
9045
|
+
// src/lib/opencode/config-overlay.ts
|
|
9046
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
9047
|
+
import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
|
|
9048
|
+
import { isAbsolute as isAbsolute2, join as join8, resolve as resolve3 } from "path";
|
|
9049
|
+
function isFile(filePath) {
|
|
9050
|
+
return existsSync2(filePath) && statSync5(filePath).isFile();
|
|
9051
|
+
}
|
|
9052
|
+
function applyRunnerOpenCodeConfig({
|
|
9053
|
+
overlayPath,
|
|
9054
|
+
cwd = process.cwd(),
|
|
9055
|
+
log: log3
|
|
9056
|
+
}) {
|
|
9057
|
+
if (!overlayPath) {
|
|
9058
|
+
log3("runner OpenCode config is not configured; using the baked project config", "debug");
|
|
9059
|
+
return;
|
|
9060
|
+
}
|
|
9061
|
+
const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
|
|
9062
|
+
const target = isFile(join8(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
|
|
9063
|
+
if (!isFile(source)) {
|
|
9064
|
+
log3(
|
|
9065
|
+
`RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
|
|
9066
|
+
"error"
|
|
9067
|
+
);
|
|
9068
|
+
return;
|
|
9069
|
+
}
|
|
9070
|
+
copyFileSync(source, join8(cwd, target));
|
|
9071
|
+
try {
|
|
9072
|
+
execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
9073
|
+
stdio: "ignore"
|
|
9074
|
+
});
|
|
9075
|
+
} catch (error2) {
|
|
9076
|
+
const detail = error2 instanceof Error ? error2.message : String(error2);
|
|
9077
|
+
log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
|
|
9078
|
+
}
|
|
9079
|
+
log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
|
|
9080
|
+
}
|
|
9081
|
+
|
|
8062
9082
|
// src/commands/run.ts
|
|
8063
9083
|
var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
8064
9084
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
@@ -8096,11 +9116,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
8096
9116
|
if (trimmed === "") {
|
|
8097
9117
|
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
8098
9118
|
}
|
|
8099
|
-
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ?
|
|
8100
|
-
if (!
|
|
9119
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join9(homeDir, trimmed.slice(2)) : trimmed;
|
|
9120
|
+
if (!isAbsolute3(expanded)) {
|
|
8101
9121
|
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
8102
9122
|
}
|
|
8103
|
-
const normalized =
|
|
9123
|
+
const normalized = resolvePath2(expanded);
|
|
8104
9124
|
if (parse(normalized).root === normalized) {
|
|
8105
9125
|
throw new Error(
|
|
8106
9126
|
`--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
|
|
@@ -8216,7 +9236,7 @@ function logActivity(state, entry) {
|
|
|
8216
9236
|
}
|
|
8217
9237
|
function reportSessionDbRecovery(state) {
|
|
8218
9238
|
try {
|
|
8219
|
-
const report = drainSessionDbRecoveryReport({ homeDir:
|
|
9239
|
+
const report = drainSessionDbRecoveryReport({ homeDir: homedir5(), env: process.env });
|
|
8220
9240
|
for (const record of report.records) {
|
|
8221
9241
|
const activity = buildSessionDbRecoveryActivity(record);
|
|
8222
9242
|
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
@@ -8234,6 +9254,16 @@ function reportSessionDbRecovery(state) {
|
|
|
8234
9254
|
);
|
|
8235
9255
|
}
|
|
8236
9256
|
}
|
|
9257
|
+
function reportSessionDbRecoveryRecord(state, record) {
|
|
9258
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
9259
|
+
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
9260
|
+
logActivity(state, {
|
|
9261
|
+
type: activity.level === "error" ? "error" : "info",
|
|
9262
|
+
level: activity.level,
|
|
9263
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
9264
|
+
metadata: activity.metadata
|
|
9265
|
+
});
|
|
9266
|
+
}
|
|
8237
9267
|
function displayStatus(state) {
|
|
8238
9268
|
if (!state.interactive) return;
|
|
8239
9269
|
const attempt = state.connection?.reconnectAttempt ?? 0;
|
|
@@ -8406,7 +9436,7 @@ async function driveChannels(state, driver) {
|
|
|
8406
9436
|
}
|
|
8407
9437
|
}
|
|
8408
9438
|
}
|
|
8409
|
-
await new Promise((
|
|
9439
|
+
await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
|
|
8410
9440
|
const cycleMs = performance.now() - cycleStartedAtMs;
|
|
8411
9441
|
if (idleThisCycle) idleMs += cycleMs;
|
|
8412
9442
|
if (unreachableThisCycle) unreachableMs += cycleMs;
|
|
@@ -8429,7 +9459,43 @@ async function driveChannels(state, driver) {
|
|
|
8429
9459
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
8430
9460
|
var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
|
|
8431
9461
|
function sessionDbPath() {
|
|
8432
|
-
return
|
|
9462
|
+
return join9(homedir5(), ".local", "share", "opencode", "opencode.db");
|
|
9463
|
+
}
|
|
9464
|
+
function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
|
|
9465
|
+
const record = {
|
|
9466
|
+
v: 1,
|
|
9467
|
+
event: "session_db_recovery",
|
|
9468
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
9469
|
+
stage: "verify",
|
|
9470
|
+
outcome: "schema_provenance_mismatch",
|
|
9471
|
+
severity: "error",
|
|
9472
|
+
reason: provenance.reason ?? "schema-provenance-mismatch",
|
|
9473
|
+
litestream_exit_code: null,
|
|
9474
|
+
attempt: null,
|
|
9475
|
+
replica_objects: null,
|
|
9476
|
+
replica_bytes: null,
|
|
9477
|
+
quarantine_destination: null,
|
|
9478
|
+
quarantined_objects: null,
|
|
9479
|
+
quarantine_failed_objects: null,
|
|
9480
|
+
quarantined_bytes: null,
|
|
9481
|
+
verified_restore_point: null,
|
|
9482
|
+
restore_points_tried: null,
|
|
9483
|
+
provenance_reason: provenance.reason,
|
|
9484
|
+
provenance_migration_delta: provenance.migrationDelta,
|
|
9485
|
+
replication_suspended: false,
|
|
9486
|
+
dbPath: sessionDbPath(),
|
|
9487
|
+
recorded_version: provenance.recordedVersion,
|
|
9488
|
+
current_version: currentVersion,
|
|
9489
|
+
provenance_pre_boot_migration_count: preBootMigrationCount
|
|
9490
|
+
};
|
|
9491
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
9492
|
+
if (!activity) throw new Error("could not map session-DB provenance activity");
|
|
9493
|
+
logActivity(state, {
|
|
9494
|
+
type: activity.level === "error" ? "error" : "info",
|
|
9495
|
+
level: activity.level,
|
|
9496
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
9497
|
+
metadata: activity.metadata
|
|
9498
|
+
});
|
|
8433
9499
|
}
|
|
8434
9500
|
async function runSweep(state, driver, config) {
|
|
8435
9501
|
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
@@ -8476,7 +9542,7 @@ async function runSweep(state, driver, config) {
|
|
|
8476
9542
|
const reclaimResult = await reclaimSessionDbSpace({
|
|
8477
9543
|
dbPath: sessionDbPath(),
|
|
8478
9544
|
maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
|
|
8479
|
-
allowFullVacuum: protectedNow.size === 0
|
|
9545
|
+
allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
|
|
8480
9546
|
});
|
|
8481
9547
|
if (reclaimResult.ok) {
|
|
8482
9548
|
const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
|
|
@@ -8512,7 +9578,7 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
8512
9578
|
for (const warning2 of config.warnings) {
|
|
8513
9579
|
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
8514
9580
|
}
|
|
8515
|
-
const dbBytes = statSessionDbBytes(
|
|
9581
|
+
const dbBytes = statSessionDbBytes(homedir5());
|
|
8516
9582
|
void (async () => {
|
|
8517
9583
|
const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
|
|
8518
9584
|
const sizeWarning = buildSessionStoreSizeWarning({
|
|
@@ -8713,7 +9779,7 @@ function scheduleResourceUsageReporting(state, options) {
|
|
|
8713
9779
|
});
|
|
8714
9780
|
return;
|
|
8715
9781
|
}
|
|
8716
|
-
const collect = createResourceUsageCollector(
|
|
9782
|
+
const collect = createResourceUsageCollector(homedir5());
|
|
8717
9783
|
let consecutiveFailures = 0;
|
|
8718
9784
|
const tick = async () => {
|
|
8719
9785
|
try {
|
|
@@ -8886,7 +9952,12 @@ async function run(options) {
|
|
|
8886
9952
|
let fileSyncDirectories;
|
|
8887
9953
|
try {
|
|
8888
9954
|
logLevel = resolveLogLevel(options);
|
|
8889
|
-
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo,
|
|
9955
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir5());
|
|
9956
|
+
if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
|
|
9957
|
+
throw new Error(
|
|
9958
|
+
"--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
|
|
9959
|
+
);
|
|
9960
|
+
}
|
|
8890
9961
|
} catch (error2) {
|
|
8891
9962
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
8892
9963
|
if (options.json) {
|
|
@@ -8910,6 +9981,7 @@ async function run(options) {
|
|
|
8910
9981
|
connected: false,
|
|
8911
9982
|
opencodeConnected: false,
|
|
8912
9983
|
opencodeVersion: null,
|
|
9984
|
+
sessionDbProvenanceAnomaly: false,
|
|
8913
9985
|
opencodeProcess: null,
|
|
8914
9986
|
litestreamProcess: null,
|
|
8915
9987
|
connection: null,
|
|
@@ -8978,8 +10050,8 @@ async function run(options) {
|
|
|
8978
10050
|
return true;
|
|
8979
10051
|
}
|
|
8980
10052
|
);
|
|
8981
|
-
const timedOut = new Promise((
|
|
8982
|
-
timer = setTimeout(() =>
|
|
10053
|
+
const timedOut = new Promise((resolve4) => {
|
|
10054
|
+
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
8983
10055
|
});
|
|
8984
10056
|
if (!await Promise.race([flushed, timedOut])) {
|
|
8985
10057
|
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
@@ -9119,7 +10191,67 @@ async function run(options) {
|
|
|
9119
10191
|
} else {
|
|
9120
10192
|
log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
|
|
9121
10193
|
}
|
|
10194
|
+
if (options.restoreRunnerCredentials) {
|
|
10195
|
+
log2(state, "Restoring runner credentials before starting OpenCode");
|
|
10196
|
+
const credentialContext = {
|
|
10197
|
+
env: process.env,
|
|
10198
|
+
log: (message, level = "info") => {
|
|
10199
|
+
if (level === "error") {
|
|
10200
|
+
logActivity(state, { type: "error", error: message });
|
|
10201
|
+
} else {
|
|
10202
|
+
logActivity(state, { type: "info", level, message });
|
|
10203
|
+
}
|
|
10204
|
+
}
|
|
10205
|
+
};
|
|
10206
|
+
const githubTokenPopulated = await installRunnerSecret(credentialContext);
|
|
10207
|
+
await restoreCredentialStores(credentialContext);
|
|
10208
|
+
if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
|
|
10209
|
+
}
|
|
10210
|
+
let sessionDbVerifyFatal = false;
|
|
10211
|
+
if (!options.restoreSessionDb) {
|
|
10212
|
+
log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
|
|
10213
|
+
} else {
|
|
10214
|
+
const health = await checkOpenCodeHealth(state.port);
|
|
10215
|
+
if (health.healthy) {
|
|
10216
|
+
log2(
|
|
10217
|
+
state,
|
|
10218
|
+
"Skipping session-DB restore: OpenCode is already serving this database",
|
|
10219
|
+
"debug"
|
|
10220
|
+
);
|
|
10221
|
+
} else {
|
|
10222
|
+
const result = await restoreAndVerifySessionDb({
|
|
10223
|
+
dbPath: sessionDbPath(),
|
|
10224
|
+
litestreamConfig: options.litestreamConfig,
|
|
10225
|
+
noReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10226
|
+
env: process.env,
|
|
10227
|
+
log: (message, level = "info") => {
|
|
10228
|
+
if (level === "error") {
|
|
10229
|
+
logActivity(state, { type: "error", error: message });
|
|
10230
|
+
} else {
|
|
10231
|
+
logActivity(state, { type: "info", level, message });
|
|
10232
|
+
}
|
|
10233
|
+
},
|
|
10234
|
+
reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
|
|
10235
|
+
});
|
|
10236
|
+
sessionDbVerifyFatal = result.verifyFatal;
|
|
10237
|
+
}
|
|
10238
|
+
}
|
|
9122
10239
|
reportSessionDbRecovery(state);
|
|
10240
|
+
if (sessionDbVerifyFatal) {
|
|
10241
|
+
throw new Error(
|
|
10242
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
|
|
10243
|
+
);
|
|
10244
|
+
}
|
|
10245
|
+
applyRunnerOpenCodeConfig({
|
|
10246
|
+
overlayPath: options.opencodeConfigOverlay,
|
|
10247
|
+
log: (message, level = "info") => {
|
|
10248
|
+
if (level === "error") {
|
|
10249
|
+
logActivity(state, { type: "error", error: message });
|
|
10250
|
+
} else {
|
|
10251
|
+
logActivity(state, { type: "info", level, message });
|
|
10252
|
+
}
|
|
10253
|
+
}
|
|
10254
|
+
});
|
|
9123
10255
|
const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
|
|
9124
10256
|
for (const warning2 of opencodeStartTimeoutWarnings) {
|
|
9125
10257
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
@@ -9128,6 +10260,7 @@ async function run(options) {
|
|
|
9128
10260
|
for (const warning2 of maxActiveSessionsWarnings) {
|
|
9129
10261
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
9130
10262
|
}
|
|
10263
|
+
const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
|
|
9131
10264
|
const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
|
|
9132
10265
|
try {
|
|
9133
10266
|
const oc = await ensureOpenCodeRunning({
|
|
@@ -9135,11 +10268,41 @@ async function run(options) {
|
|
|
9135
10268
|
interactive: state.interactive,
|
|
9136
10269
|
agentId: state.agentId,
|
|
9137
10270
|
log: (message) => log2(state, message),
|
|
9138
|
-
startTimeoutMs: opencodeStartTimeoutMs
|
|
10271
|
+
startTimeoutMs: opencodeStartTimeoutMs,
|
|
10272
|
+
inheritStdio: Boolean(options.opencodePidFile)
|
|
9139
10273
|
});
|
|
9140
10274
|
state.port = oc.port;
|
|
9141
|
-
state.opencodeProcess = oc.process;
|
|
10275
|
+
state.opencodeProcess = options.opencodePidFile ? null : oc.process;
|
|
9142
10276
|
state.opencodeVersion = oc.version;
|
|
10277
|
+
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
10278
|
+
try {
|
|
10279
|
+
writeFileSync5(options.opencodePidFile, `${oc.process.pid}
|
|
10280
|
+
`, { mode: 384 });
|
|
10281
|
+
chmodSync3(options.opencodePidFile, 384);
|
|
10282
|
+
} catch (error2) {
|
|
10283
|
+
logActivity(state, {
|
|
10284
|
+
type: "error",
|
|
10285
|
+
error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10286
|
+
});
|
|
10287
|
+
}
|
|
10288
|
+
}
|
|
10289
|
+
if (state.opencodeVersion !== null) {
|
|
10290
|
+
const provenance = checkSessionDbProvenance({
|
|
10291
|
+
dbPath: sessionDbPath(),
|
|
10292
|
+
currentVersion: state.opencodeVersion,
|
|
10293
|
+
homeDir: homedir5(),
|
|
10294
|
+
env: process.env
|
|
10295
|
+
});
|
|
10296
|
+
if (provenance.anomaly) {
|
|
10297
|
+
state.sessionDbProvenanceAnomaly = true;
|
|
10298
|
+
logSessionDbProvenanceMismatch(
|
|
10299
|
+
state,
|
|
10300
|
+
provenance,
|
|
10301
|
+
state.opencodeVersion,
|
|
10302
|
+
preBootMigrationIds?.length ?? null
|
|
10303
|
+
);
|
|
10304
|
+
}
|
|
10305
|
+
}
|
|
9143
10306
|
state.opencodeConnected = oc.notReadyReason === null;
|
|
9144
10307
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
9145
10308
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
@@ -9176,7 +10339,75 @@ async function run(options) {
|
|
|
9176
10339
|
ocSpinner?.fail(error2.message);
|
|
9177
10340
|
throw error2;
|
|
9178
10341
|
}
|
|
9179
|
-
if (options.
|
|
10342
|
+
if (options.litestreamPidFile) {
|
|
10343
|
+
if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
|
|
10344
|
+
log2(
|
|
10345
|
+
state,
|
|
10346
|
+
`Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
|
|
10347
|
+
);
|
|
10348
|
+
} else if (!options.litestreamConfig) {
|
|
10349
|
+
logActivity(state, {
|
|
10350
|
+
type: "info",
|
|
10351
|
+
level: "warn",
|
|
10352
|
+
message: "Skipping Litestream replication because no configuration file was provided"
|
|
10353
|
+
});
|
|
10354
|
+
} else {
|
|
10355
|
+
let existingPid;
|
|
10356
|
+
if (existsSync3(options.litestreamPidFile)) {
|
|
10357
|
+
try {
|
|
10358
|
+
const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
|
|
10359
|
+
const parsedPid = Number(rawPid);
|
|
10360
|
+
if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
|
|
10361
|
+
existingPid = parsedPid;
|
|
10362
|
+
}
|
|
10363
|
+
} catch (error2) {
|
|
10364
|
+
logActivity(state, {
|
|
10365
|
+
type: "info",
|
|
10366
|
+
level: "warn",
|
|
10367
|
+
message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10368
|
+
});
|
|
10369
|
+
}
|
|
10370
|
+
}
|
|
10371
|
+
if (existingPid !== void 0 && isProcessAlive(existingPid)) {
|
|
10372
|
+
log2(state, `Litestream replication is already running with pid ${existingPid}`);
|
|
10373
|
+
} else {
|
|
10374
|
+
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
10375
|
+
state.litestreamProcess = null;
|
|
10376
|
+
let failureHandled = false;
|
|
10377
|
+
const reportImageOwnedReplicationFailure = (message) => {
|
|
10378
|
+
if (failureHandled || state.shuttingDown || !state.running) return;
|
|
10379
|
+
failureHandled = true;
|
|
10380
|
+
logActivity(state, { type: "error", error: message });
|
|
10381
|
+
if (state.interactive) displayStatus(state);
|
|
10382
|
+
};
|
|
10383
|
+
litestreamProcess.on("exit", (code, signal) => {
|
|
10384
|
+
reportImageOwnedReplicationFailure(
|
|
10385
|
+
`Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
|
|
10386
|
+
);
|
|
10387
|
+
});
|
|
10388
|
+
litestreamProcess.on("error", (error2) => {
|
|
10389
|
+
reportImageOwnedReplicationFailure(
|
|
10390
|
+
`Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10391
|
+
);
|
|
10392
|
+
});
|
|
10393
|
+
try {
|
|
10394
|
+
if (litestreamProcess.pid !== void 0) {
|
|
10395
|
+
writeFileSync5(options.litestreamPidFile, `${litestreamProcess.pid}
|
|
10396
|
+
`, {
|
|
10397
|
+
mode: 384
|
|
10398
|
+
});
|
|
10399
|
+
chmodSync3(options.litestreamPidFile, 384);
|
|
10400
|
+
}
|
|
10401
|
+
} catch (error2) {
|
|
10402
|
+
logActivity(state, {
|
|
10403
|
+
type: "error",
|
|
10404
|
+
error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10405
|
+
});
|
|
10406
|
+
}
|
|
10407
|
+
log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
|
|
10408
|
+
}
|
|
10409
|
+
}
|
|
10410
|
+
} else if (options.litestreamConfig) {
|
|
9180
10411
|
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
9181
10412
|
state.litestreamProcess = litestreamProcess;
|
|
9182
10413
|
let failureHandled = false;
|
|
@@ -9221,7 +10452,7 @@ async function run(options) {
|
|
|
9221
10452
|
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
9222
10453
|
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
9223
10454
|
fileSyncDirectories,
|
|
9224
|
-
homeDir:
|
|
10455
|
+
homeDir: homedir5(),
|
|
9225
10456
|
maxActiveSessions,
|
|
9226
10457
|
log: (entry) => (
|
|
9227
10458
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
@@ -9413,7 +10644,7 @@ async function run(options) {
|
|
|
9413
10644
|
}
|
|
9414
10645
|
|
|
9415
10646
|
// src/index.ts
|
|
9416
|
-
var { version } =
|
|
10647
|
+
var { version } = createRequire2(import.meta.url)("../package.json");
|
|
9417
10648
|
var program = new Command();
|
|
9418
10649
|
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
|
|
9419
10650
|
"--endpoint <url>",
|
|
@@ -9473,6 +10704,24 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9473
10704
|
).option(
|
|
9474
10705
|
"--litestream-config <path>",
|
|
9475
10706
|
"Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
|
|
10707
|
+
).option(
|
|
10708
|
+
"--opencode-pid-file <path>",
|
|
10709
|
+
"Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
10710
|
+
).option(
|
|
10711
|
+
"--litestream-pid-file <path>",
|
|
10712
|
+
"Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
10713
|
+
).option(
|
|
10714
|
+
"--session-db-no-replicate-marker <path>",
|
|
10715
|
+
"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."
|
|
10716
|
+
).option(
|
|
10717
|
+
"--restore-session-db",
|
|
10718
|
+
"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."
|
|
10719
|
+
).option(
|
|
10720
|
+
"--restore-runner-credentials",
|
|
10721
|
+
"Restore the hosted runner secret and persisted credential stores before starting OpenCode."
|
|
10722
|
+
).option(
|
|
10723
|
+
"--opencode-config-overlay <path>",
|
|
10724
|
+
"Apply this runner-provided OpenCode config before starting OpenCode."
|
|
9476
10725
|
).action(
|
|
9477
10726
|
(options) => {
|
|
9478
10727
|
run({
|
|
@@ -9505,7 +10754,13 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9505
10754
|
// resolveFileSyncDirectories.
|
|
9506
10755
|
enableFileSyncTo: options.enableFileSyncTo,
|
|
9507
10756
|
tunnelReadyFile: options.tunnelReadyFile,
|
|
9508
|
-
litestreamConfig: options.litestreamConfig
|
|
10757
|
+
litestreamConfig: options.litestreamConfig,
|
|
10758
|
+
opencodePidFile: options.opencodePidFile,
|
|
10759
|
+
litestreamPidFile: options.litestreamPidFile,
|
|
10760
|
+
sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10761
|
+
restoreSessionDb: options.restoreSessionDb,
|
|
10762
|
+
restoreRunnerCredentials: options.restoreRunnerCredentials,
|
|
10763
|
+
opencodeConfigOverlay: options.opencodeConfigOverlay
|
|
9509
10764
|
});
|
|
9510
10765
|
}
|
|
9511
10766
|
);
|