@evident-ai/cli 3.4.1-dev.2f1b44b → 3.4.1-dev.31006db
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/index.js +1094 -62
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -371,14 +371,14 @@ function blank() {
|
|
|
371
371
|
console.log();
|
|
372
372
|
}
|
|
373
373
|
function waitForEnter(prompt = "Press Enter to continue...") {
|
|
374
|
-
return new Promise((
|
|
374
|
+
return new Promise((resolve4) => {
|
|
375
375
|
process.stdout.write(chalk.dim(prompt));
|
|
376
376
|
const handler = () => {
|
|
377
377
|
process.stdin.removeListener("data", handler);
|
|
378
378
|
process.stdin.setRawMode?.(false);
|
|
379
379
|
process.stdin.pause();
|
|
380
380
|
console.log();
|
|
381
|
-
|
|
381
|
+
resolve4();
|
|
382
382
|
};
|
|
383
383
|
if (process.stdin.isTTY) {
|
|
384
384
|
process.stdin.setRawMode?.(true);
|
|
@@ -388,7 +388,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
|
|
|
388
388
|
});
|
|
389
389
|
}
|
|
390
390
|
function sleep(ms) {
|
|
391
|
-
return new Promise((
|
|
391
|
+
return new Promise((resolve4) => setTimeout(resolve4, ms));
|
|
392
392
|
}
|
|
393
393
|
|
|
394
394
|
// src/commands/login.ts
|
|
@@ -466,19 +466,19 @@ async function tokenLogin() {
|
|
|
466
466
|
);
|
|
467
467
|
blank();
|
|
468
468
|
process.stdout.write("Paste token: ");
|
|
469
|
-
const token = await new Promise((
|
|
469
|
+
const token = await new Promise((resolve4) => {
|
|
470
470
|
let data = "";
|
|
471
471
|
process.stdin.setEncoding("utf8");
|
|
472
472
|
process.stdin.on("data", (chunk) => {
|
|
473
473
|
data += chunk;
|
|
474
474
|
});
|
|
475
475
|
process.stdin.on("end", () => {
|
|
476
|
-
|
|
476
|
+
resolve4(data.trim());
|
|
477
477
|
});
|
|
478
478
|
if (process.stdin.isTTY) {
|
|
479
479
|
process.stdin.once("data", (chunk) => {
|
|
480
480
|
process.stdin.pause();
|
|
481
|
-
|
|
481
|
+
resolve4(chunk.toString().trim());
|
|
482
482
|
});
|
|
483
483
|
process.stdin.resume();
|
|
484
484
|
}
|
|
@@ -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 readFileSync5, writeFileSync as writeFileSync4 } from "fs";
|
|
1187
|
+
import { homedir as homedir5 } from "os";
|
|
1188
|
+
import { isAbsolute as isAbsolute3, join as join8, parse, resolve as resolvePath2 } from "path";
|
|
1188
1189
|
import chalk6 from "chalk";
|
|
1189
1190
|
|
|
1190
1191
|
// ../../packages/types/src/agents/index.ts
|
|
@@ -1688,11 +1689,517 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
1688
1689
|
if (health.healthy) {
|
|
1689
1690
|
return health;
|
|
1690
1691
|
}
|
|
1691
|
-
await new Promise((
|
|
1692
|
+
await new Promise((resolve4) => setTimeout(resolve4, 1e3));
|
|
1692
1693
|
}
|
|
1693
1694
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
1694
1695
|
}
|
|
1695
1696
|
|
|
1697
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1698
|
+
import { spawn as spawn2 } from "child_process";
|
|
1699
|
+
import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "fs";
|
|
1700
|
+
import { homedir as homedir2 } from "os";
|
|
1701
|
+
import { dirname as dirname2, resolve as resolvePath } from "path";
|
|
1702
|
+
|
|
1703
|
+
// src/lib/runner-synchroniser.ts
|
|
1704
|
+
import { spawn } from "child_process";
|
|
1705
|
+
function appendError(stderr, error2) {
|
|
1706
|
+
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
1707
|
+
return stderr === "" ? message : `${stderr}
|
|
1708
|
+
${message}`;
|
|
1709
|
+
}
|
|
1710
|
+
function runSynchroniser(args, opts) {
|
|
1711
|
+
return new Promise((resolve4) => {
|
|
1712
|
+
let child;
|
|
1713
|
+
let stdout = "";
|
|
1714
|
+
let stderr = "";
|
|
1715
|
+
let settled = false;
|
|
1716
|
+
const timer = {};
|
|
1717
|
+
const finish = (result) => {
|
|
1718
|
+
if (settled) return;
|
|
1719
|
+
settled = true;
|
|
1720
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
1721
|
+
resolve4(result);
|
|
1722
|
+
};
|
|
1723
|
+
try {
|
|
1724
|
+
child = spawn("runner-synchroniser", args, {
|
|
1725
|
+
env: opts.env ?? process.env,
|
|
1726
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1727
|
+
});
|
|
1728
|
+
} catch (error2) {
|
|
1729
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1730
|
+
return;
|
|
1731
|
+
}
|
|
1732
|
+
child.stdout?.setEncoding("utf8");
|
|
1733
|
+
child.stdout?.on("data", (chunk) => {
|
|
1734
|
+
stdout += chunk;
|
|
1735
|
+
});
|
|
1736
|
+
child.stderr?.setEncoding("utf8");
|
|
1737
|
+
child.stderr?.on("data", (chunk) => {
|
|
1738
|
+
stderr += chunk;
|
|
1739
|
+
});
|
|
1740
|
+
child.once("error", (error2) => {
|
|
1741
|
+
finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
|
|
1742
|
+
});
|
|
1743
|
+
child.once("close", (code) => {
|
|
1744
|
+
finish({ code, stdout, stderr, timedOut: false });
|
|
1745
|
+
});
|
|
1746
|
+
timer.handle = setTimeout(
|
|
1747
|
+
() => {
|
|
1748
|
+
child.kill("SIGKILL");
|
|
1749
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1750
|
+
},
|
|
1751
|
+
Math.max(0, opts.timeoutMs)
|
|
1752
|
+
);
|
|
1753
|
+
});
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
// src/lib/opencode/session-db-boot.ts
|
|
1757
|
+
var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
|
|
1758
|
+
var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
|
|
1759
|
+
var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
|
|
1760
|
+
var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
|
|
1761
|
+
function commandError(result) {
|
|
1762
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
1763
|
+
}
|
|
1764
|
+
function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
|
|
1765
|
+
options.reportRecovery({
|
|
1766
|
+
v: 1,
|
|
1767
|
+
event: "session_db_recovery",
|
|
1768
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1769
|
+
stage,
|
|
1770
|
+
outcome,
|
|
1771
|
+
severity: "error",
|
|
1772
|
+
reason,
|
|
1773
|
+
litestream_exit_code: litestreamExitCode,
|
|
1774
|
+
attempt: null,
|
|
1775
|
+
replica_objects: null,
|
|
1776
|
+
replica_bytes: null,
|
|
1777
|
+
quarantine_destination: null,
|
|
1778
|
+
quarantined_objects: null,
|
|
1779
|
+
quarantine_failed_objects: null,
|
|
1780
|
+
quarantined_bytes: null,
|
|
1781
|
+
verified_restore_point: null,
|
|
1782
|
+
restore_points_tried: null,
|
|
1783
|
+
replication_suspended: stage === "restore"
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
function clearMarker(options) {
|
|
1787
|
+
if (!options.noReplicateMarker) return;
|
|
1788
|
+
try {
|
|
1789
|
+
unlinkSync2(options.noReplicateMarker);
|
|
1790
|
+
} catch (error2) {
|
|
1791
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
|
|
1792
|
+
options.log(
|
|
1793
|
+
`Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1794
|
+
"warn"
|
|
1795
|
+
);
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
function markNoReplicate(options, message) {
|
|
1799
|
+
if (options.noReplicateMarker) {
|
|
1800
|
+
try {
|
|
1801
|
+
mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
|
|
1802
|
+
writeFileSync(options.noReplicateMarker, "");
|
|
1803
|
+
} catch (error2) {
|
|
1804
|
+
options.log(
|
|
1805
|
+
`Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1806
|
+
"error"
|
|
1807
|
+
);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
|
|
1811
|
+
}
|
|
1812
|
+
function discardSessionDbDebris(options) {
|
|
1813
|
+
for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
|
|
1814
|
+
try {
|
|
1815
|
+
unlinkSync2(path);
|
|
1816
|
+
} catch (error2) {
|
|
1817
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
|
|
1818
|
+
options.log(
|
|
1819
|
+
`Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1820
|
+
"warn"
|
|
1821
|
+
);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
function splitDiagnostics(text) {
|
|
1826
|
+
return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
|
|
1827
|
+
}
|
|
1828
|
+
function logSynchroniserDiagnostics(result, options) {
|
|
1829
|
+
for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
|
|
1830
|
+
}
|
|
1831
|
+
function parseSingleQuotedAssignment(line) {
|
|
1832
|
+
const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
|
|
1833
|
+
if (!match || !match[2].startsWith("'")) return null;
|
|
1834
|
+
const valueSource = match[2];
|
|
1835
|
+
let value = "";
|
|
1836
|
+
for (let index = 1; index < valueSource.length; index++) {
|
|
1837
|
+
const character = valueSource[index];
|
|
1838
|
+
if (character !== "'") {
|
|
1839
|
+
value += character;
|
|
1840
|
+
continue;
|
|
1841
|
+
}
|
|
1842
|
+
if (index === valueSource.length - 1) return [match[1], value];
|
|
1843
|
+
if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
|
|
1844
|
+
value += "'";
|
|
1845
|
+
index += 3;
|
|
1846
|
+
}
|
|
1847
|
+
return null;
|
|
1848
|
+
}
|
|
1849
|
+
function parseSynchroniserEnv(stdout) {
|
|
1850
|
+
const values = {};
|
|
1851
|
+
for (const line of stdout.split("\n")) {
|
|
1852
|
+
if (line.trim() === "") continue;
|
|
1853
|
+
const assignment = parseSingleQuotedAssignment(line);
|
|
1854
|
+
if (!assignment) return null;
|
|
1855
|
+
values[assignment[0]] = assignment[1];
|
|
1856
|
+
}
|
|
1857
|
+
return values;
|
|
1858
|
+
}
|
|
1859
|
+
function runCommand(command, args, options) {
|
|
1860
|
+
return new Promise((resolve4) => {
|
|
1861
|
+
let child;
|
|
1862
|
+
let stdout = "";
|
|
1863
|
+
let stderr = "";
|
|
1864
|
+
let settled = false;
|
|
1865
|
+
const finish = (result) => {
|
|
1866
|
+
if (settled) return;
|
|
1867
|
+
settled = true;
|
|
1868
|
+
if (timer) clearTimeout(timer);
|
|
1869
|
+
resolve4(result);
|
|
1870
|
+
};
|
|
1871
|
+
try {
|
|
1872
|
+
child = spawn2(command, args, {
|
|
1873
|
+
env: options.env,
|
|
1874
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
1875
|
+
});
|
|
1876
|
+
} catch (error2) {
|
|
1877
|
+
resolve4({
|
|
1878
|
+
code: null,
|
|
1879
|
+
stdout,
|
|
1880
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
1881
|
+
timedOut: false
|
|
1882
|
+
});
|
|
1883
|
+
return;
|
|
1884
|
+
}
|
|
1885
|
+
child.stdout?.setEncoding("utf8");
|
|
1886
|
+
child.stdout?.on("data", (chunk) => {
|
|
1887
|
+
stdout += chunk;
|
|
1888
|
+
});
|
|
1889
|
+
child.stderr?.setEncoding("utf8");
|
|
1890
|
+
child.stderr?.on("data", (chunk) => {
|
|
1891
|
+
stderr += chunk;
|
|
1892
|
+
});
|
|
1893
|
+
child.once("error", (error2) => {
|
|
1894
|
+
finish({
|
|
1895
|
+
code: null,
|
|
1896
|
+
stdout,
|
|
1897
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
1898
|
+
${error2.message}`,
|
|
1899
|
+
timedOut: false
|
|
1900
|
+
});
|
|
1901
|
+
});
|
|
1902
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
1903
|
+
const timer = setTimeout(
|
|
1904
|
+
() => {
|
|
1905
|
+
child.kill("SIGKILL");
|
|
1906
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
1907
|
+
},
|
|
1908
|
+
Math.max(0, options.timeoutMs)
|
|
1909
|
+
);
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
async function ensureLitestreamConfig(options, env) {
|
|
1913
|
+
const configPath = options.litestreamConfig;
|
|
1914
|
+
if (!configPath) {
|
|
1915
|
+
markNoReplicate(options, "no Litestream configuration path was provided");
|
|
1916
|
+
reportRecord(
|
|
1917
|
+
"restore",
|
|
1918
|
+
"restore_misconfigured",
|
|
1919
|
+
"litestream_config_unavailable",
|
|
1920
|
+
null,
|
|
1921
|
+
options
|
|
1922
|
+
);
|
|
1923
|
+
return null;
|
|
1924
|
+
}
|
|
1925
|
+
try {
|
|
1926
|
+
if (statSync2(configPath).size > 0) return configPath;
|
|
1927
|
+
} catch (error2) {
|
|
1928
|
+
if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
|
|
1929
|
+
options.log(
|
|
1930
|
+
`Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1931
|
+
"warn"
|
|
1932
|
+
);
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
const rendered = await runSynchroniser(["litestream-config"], {
|
|
1936
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
1937
|
+
env
|
|
1938
|
+
});
|
|
1939
|
+
logSynchroniserDiagnostics(rendered, options);
|
|
1940
|
+
if (rendered.timedOut || rendered.code !== 0) {
|
|
1941
|
+
options.log(
|
|
1942
|
+
`Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
|
|
1943
|
+
"error"
|
|
1944
|
+
);
|
|
1945
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1946
|
+
reportRecord(
|
|
1947
|
+
"restore",
|
|
1948
|
+
"restore_misconfigured",
|
|
1949
|
+
"litestream_config_unavailable",
|
|
1950
|
+
null,
|
|
1951
|
+
options
|
|
1952
|
+
);
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
try {
|
|
1956
|
+
mkdirSync(dirname2(configPath), { recursive: true });
|
|
1957
|
+
writeFileSync(configPath, rendered.stdout);
|
|
1958
|
+
} catch (error2) {
|
|
1959
|
+
options.log(
|
|
1960
|
+
`Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
1961
|
+
"error"
|
|
1962
|
+
);
|
|
1963
|
+
markNoReplicate(options, `could not generate ${configPath}`);
|
|
1964
|
+
reportRecord(
|
|
1965
|
+
"restore",
|
|
1966
|
+
"restore_misconfigured",
|
|
1967
|
+
"litestream_config_unavailable",
|
|
1968
|
+
null,
|
|
1969
|
+
options
|
|
1970
|
+
);
|
|
1971
|
+
return null;
|
|
1972
|
+
}
|
|
1973
|
+
const version2 = await runCommand("litestream", ["version"], {
|
|
1974
|
+
env,
|
|
1975
|
+
timeoutMs: 1e4
|
|
1976
|
+
});
|
|
1977
|
+
const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
|
|
1978
|
+
const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
|
|
1979
|
+
options.log(
|
|
1980
|
+
`litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
|
|
1981
|
+
);
|
|
1982
|
+
return configPath;
|
|
1983
|
+
}
|
|
1984
|
+
function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
|
|
1985
|
+
discardSessionDbDebris(options);
|
|
1986
|
+
markNoReplicate(options, message);
|
|
1987
|
+
reportRecord("restore", outcome, reason, litestreamExitCode, options);
|
|
1988
|
+
}
|
|
1989
|
+
async function restoreSessionDb(options, configPath, env) {
|
|
1990
|
+
const restored = await runCommand(
|
|
1991
|
+
"litestream",
|
|
1992
|
+
["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
|
|
1993
|
+
{ env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
|
|
1994
|
+
);
|
|
1995
|
+
for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
|
|
1996
|
+
if (restored.timedOut || restored.code === 124 || restored.code === 137) {
|
|
1997
|
+
restoreGiveUp(
|
|
1998
|
+
options,
|
|
1999
|
+
"restore_deadline_exceeded",
|
|
2000
|
+
`SESSION-DB-RESTORE-TRUNCATED: litestream restore did not finish within ${SESSION_DB_RESTORE_TIMEOUT_MS}ms; opencode starts with a fresh session DB and nothing is replicated this boot`,
|
|
2001
|
+
restored.code ?? 124
|
|
2002
|
+
);
|
|
2003
|
+
return;
|
|
2004
|
+
}
|
|
2005
|
+
if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
|
|
2006
|
+
options.log(`litestream restore could not run (${commandError(restored)})`, "error");
|
|
2007
|
+
restoreGiveUp(
|
|
2008
|
+
options,
|
|
2009
|
+
"restore_tool_unusable",
|
|
2010
|
+
`restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
|
|
2011
|
+
restored.code
|
|
2012
|
+
);
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
const classified = await runSynchroniser(
|
|
2016
|
+
[
|
|
2017
|
+
"session-db-classify",
|
|
2018
|
+
String(restored.code ?? 1),
|
|
2019
|
+
"1",
|
|
2020
|
+
"--on-unusable-replica=leave",
|
|
2021
|
+
"--fresh-db-fallback"
|
|
2022
|
+
],
|
|
2023
|
+
{ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
|
|
2024
|
+
);
|
|
2025
|
+
logSynchroniserDiagnostics(classified, options);
|
|
2026
|
+
const classifyCode = classified.code;
|
|
2027
|
+
switch (classifyCode) {
|
|
2028
|
+
case 0:
|
|
2029
|
+
return;
|
|
2030
|
+
case 31:
|
|
2031
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2032
|
+
options.log(
|
|
2033
|
+
"SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
|
|
2034
|
+
"warn"
|
|
2035
|
+
);
|
|
2036
|
+
return;
|
|
2037
|
+
case 32:
|
|
2038
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2039
|
+
discardSessionDbDebris(options);
|
|
2040
|
+
markNoReplicate(
|
|
2041
|
+
options,
|
|
2042
|
+
"session-db-classify asked for another restore attempt (32), but this boot has budget for only one; treating it as a give-up rather than retrying"
|
|
2043
|
+
);
|
|
2044
|
+
return;
|
|
2045
|
+
case 30:
|
|
2046
|
+
restoreGiveUp(
|
|
2047
|
+
options,
|
|
2048
|
+
"classification_fatal",
|
|
2049
|
+
"session-db-classify returned fatal (30); see the FATAL message above",
|
|
2050
|
+
restored.code,
|
|
2051
|
+
"restore_misconfigured"
|
|
2052
|
+
);
|
|
2053
|
+
return;
|
|
2054
|
+
default:
|
|
2055
|
+
restoreGiveUp(
|
|
2056
|
+
options,
|
|
2057
|
+
"classification_unrecognised",
|
|
2058
|
+
`session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
|
|
2059
|
+
restored.code
|
|
2060
|
+
);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
async function verifySessionDb(options, configPath, env) {
|
|
2064
|
+
if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
|
|
2065
|
+
const result = await runSynchroniser(["session-db-verify", configPath], {
|
|
2066
|
+
timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
|
|
2067
|
+
env: {
|
|
2068
|
+
...env,
|
|
2069
|
+
// The synchroniser reads this value in SECONDS. Keep this at 120, not
|
|
2070
|
+
// 120_000, so the walkback gives up before the outer process bound.
|
|
2071
|
+
EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
|
|
2072
|
+
SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
|
|
2073
|
+
)
|
|
2074
|
+
}
|
|
2075
|
+
});
|
|
2076
|
+
logSynchroniserDiagnostics(result, options);
|
|
2077
|
+
if (result.timedOut || result.code === 124 || result.code === 137) {
|
|
2078
|
+
options.log(
|
|
2079
|
+
`SESSION-DB-VERIFY-TIMEOUT: verification did not finish within its ${SESSION_DB_VERIFY_TIMEOUT_MS}ms deadline; continuing with the restored opencode.db as-is, unverified`,
|
|
2080
|
+
"warn"
|
|
2081
|
+
);
|
|
2082
|
+
return false;
|
|
2083
|
+
}
|
|
2084
|
+
if (result.code === 34) {
|
|
2085
|
+
reportRecord(
|
|
2086
|
+
"verify",
|
|
2087
|
+
"session_db_boot_refused",
|
|
2088
|
+
result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
|
|
2089
|
+
null,
|
|
2090
|
+
options
|
|
2091
|
+
);
|
|
2092
|
+
return true;
|
|
2093
|
+
}
|
|
2094
|
+
if (result.code === 33) {
|
|
2095
|
+
options.log(
|
|
2096
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
|
|
2097
|
+
"warn"
|
|
2098
|
+
);
|
|
2099
|
+
return false;
|
|
2100
|
+
}
|
|
2101
|
+
if (result.code !== 0) {
|
|
2102
|
+
options.log(
|
|
2103
|
+
`SESSION-DB-VERIFY-UNKNOWN: session-db-verify exited ${result.code ?? "null"}, which is none of its documented answers; continuing with the restored opencode.db as-is`,
|
|
2104
|
+
"warn"
|
|
2105
|
+
);
|
|
2106
|
+
}
|
|
2107
|
+
return false;
|
|
2108
|
+
}
|
|
2109
|
+
options.log(
|
|
2110
|
+
"skipping session-DB verification: this boot's session DB was not proven safe to replicate",
|
|
2111
|
+
"debug"
|
|
2112
|
+
);
|
|
2113
|
+
return false;
|
|
2114
|
+
}
|
|
2115
|
+
function fileExists(path) {
|
|
2116
|
+
try {
|
|
2117
|
+
statSync2(path);
|
|
2118
|
+
return true;
|
|
2119
|
+
} catch (error2) {
|
|
2120
|
+
if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
|
|
2121
|
+
return true;
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
async function restoreAndVerifySessionDb(options) {
|
|
2125
|
+
const env = options.env ?? process.env;
|
|
2126
|
+
clearMarker(options);
|
|
2127
|
+
acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
|
|
2128
|
+
const synchroniserEnv = await runSynchroniser(["env"], {
|
|
2129
|
+
timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
|
|
2130
|
+
env
|
|
2131
|
+
});
|
|
2132
|
+
logSynchroniserDiagnostics(synchroniserEnv, options);
|
|
2133
|
+
if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
|
|
2134
|
+
options.log(
|
|
2135
|
+
`runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
|
|
2136
|
+
"error"
|
|
2137
|
+
);
|
|
2138
|
+
markNoReplicate(
|
|
2139
|
+
options,
|
|
2140
|
+
"could not resolve the runner-synchroniser configuration (see the ERROR above)"
|
|
2141
|
+
);
|
|
2142
|
+
reportRecord(
|
|
2143
|
+
"restore",
|
|
2144
|
+
"restore_misconfigured",
|
|
2145
|
+
"synchroniser_config_unresolved",
|
|
2146
|
+
null,
|
|
2147
|
+
options
|
|
2148
|
+
);
|
|
2149
|
+
return { verifyFatal: false };
|
|
2150
|
+
}
|
|
2151
|
+
const values = parseSynchroniserEnv(synchroniserEnv.stdout);
|
|
2152
|
+
if (!values) {
|
|
2153
|
+
markNoReplicate(
|
|
2154
|
+
options,
|
|
2155
|
+
"the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
|
|
2156
|
+
);
|
|
2157
|
+
reportRecord(
|
|
2158
|
+
"restore",
|
|
2159
|
+
"restore_misconfigured",
|
|
2160
|
+
"synchroniser_config_unevaluable",
|
|
2161
|
+
null,
|
|
2162
|
+
options
|
|
2163
|
+
);
|
|
2164
|
+
return { verifyFatal: false };
|
|
2165
|
+
}
|
|
2166
|
+
const synchroniserDbPath = values.OPENCODE_DB_PATH;
|
|
2167
|
+
if (!synchroniserDbPath) {
|
|
2168
|
+
markNoReplicate(
|
|
2169
|
+
options,
|
|
2170
|
+
"run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
|
|
2171
|
+
);
|
|
2172
|
+
reportRecord(
|
|
2173
|
+
"restore",
|
|
2174
|
+
"restore_misconfigured",
|
|
2175
|
+
"synchroniser_config_incomplete",
|
|
2176
|
+
null,
|
|
2177
|
+
options
|
|
2178
|
+
);
|
|
2179
|
+
return { verifyFatal: false };
|
|
2180
|
+
}
|
|
2181
|
+
if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
|
|
2182
|
+
options.log(
|
|
2183
|
+
`runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
|
|
2184
|
+
"warn"
|
|
2185
|
+
);
|
|
2186
|
+
}
|
|
2187
|
+
if (!values.PERSISTENCE_BUCKET) {
|
|
2188
|
+
options.log(
|
|
2189
|
+
"SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
|
|
2190
|
+
"warn"
|
|
2191
|
+
);
|
|
2192
|
+
return { verifyFatal: false };
|
|
2193
|
+
}
|
|
2194
|
+
const configPath = await ensureLitestreamConfig(options, env);
|
|
2195
|
+
if (!configPath) return { verifyFatal: false };
|
|
2196
|
+
await restoreSessionDb(options, configPath, env);
|
|
2197
|
+
if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
|
|
2198
|
+
return { verifyFatal: false };
|
|
2199
|
+
}
|
|
2200
|
+
return { verifyFatal: await verifySessionDb(options, configPath, env) };
|
|
2201
|
+
}
|
|
2202
|
+
|
|
1696
2203
|
// src/lib/opencode/opencode-version-gate.ts
|
|
1697
2204
|
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
1698
2205
|
function isQueueValidatedVersion(version2) {
|
|
@@ -1707,7 +2214,7 @@ function buildOpenCodeVersionWarning(version2) {
|
|
|
1707
2214
|
}
|
|
1708
2215
|
|
|
1709
2216
|
// src/lib/opencode/process.ts
|
|
1710
|
-
import { execSync, spawn } from "child_process";
|
|
2217
|
+
import { execSync, spawn as spawn3 } from "child_process";
|
|
1711
2218
|
|
|
1712
2219
|
// src/lib/process-stop.ts
|
|
1713
2220
|
async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
@@ -1717,7 +2224,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
|
1717
2224
|
if (child.exitCode !== null || child.signalCode !== null) {
|
|
1718
2225
|
return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
|
|
1719
2226
|
}
|
|
1720
|
-
return new Promise((
|
|
2227
|
+
return new Promise((resolve4, reject) => {
|
|
1721
2228
|
let forced = false;
|
|
1722
2229
|
let settled = false;
|
|
1723
2230
|
const timer = setTimeout(() => {
|
|
@@ -1737,7 +2244,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
|
|
|
1737
2244
|
settled = true;
|
|
1738
2245
|
clearTimeout(timer);
|
|
1739
2246
|
child.removeListener("exit", onExit);
|
|
1740
|
-
|
|
2247
|
+
resolve4(result);
|
|
1741
2248
|
};
|
|
1742
2249
|
const fail = (error2) => {
|
|
1743
2250
|
if (settled) return;
|
|
@@ -1919,18 +2426,27 @@ async function findHealthyOpenCodeInstances() {
|
|
|
1919
2426
|
}
|
|
1920
2427
|
return healthy;
|
|
1921
2428
|
}
|
|
1922
|
-
async function startOpenCode(port) {
|
|
2429
|
+
async function startOpenCode(port, options = {}) {
|
|
1923
2430
|
let command = "opencode";
|
|
1924
|
-
|
|
2431
|
+
const printLogs = options.inheritStdio ? ["--print-logs"] : [];
|
|
2432
|
+
let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
|
|
1925
2433
|
try {
|
|
1926
2434
|
execSync("which opencode", { stdio: "ignore" });
|
|
1927
2435
|
} catch {
|
|
1928
2436
|
command = "npx";
|
|
1929
|
-
args = [
|
|
1930
|
-
|
|
1931
|
-
|
|
2437
|
+
args = [
|
|
2438
|
+
"opencode",
|
|
2439
|
+
"serve",
|
|
2440
|
+
"--port",
|
|
2441
|
+
port.toString(),
|
|
2442
|
+
"--hostname",
|
|
2443
|
+
"127.0.0.1",
|
|
2444
|
+
...printLogs
|
|
2445
|
+
];
|
|
2446
|
+
}
|
|
2447
|
+
const child = spawn3(command, args, {
|
|
1932
2448
|
detached: true,
|
|
1933
|
-
stdio: "ignore",
|
|
2449
|
+
stdio: options.inheritStdio ? "inherit" : "ignore",
|
|
1934
2450
|
cwd: process.cwd()
|
|
1935
2451
|
});
|
|
1936
2452
|
return child;
|
|
@@ -2416,7 +2932,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
|
|
|
2416
2932
|
}
|
|
2417
2933
|
}
|
|
2418
2934
|
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
2419
|
-
await new Promise((
|
|
2935
|
+
await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
|
|
2420
2936
|
}
|
|
2421
2937
|
}
|
|
2422
2938
|
return null;
|
|
@@ -2769,13 +3285,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
|
|
|
2769
3285
|
}
|
|
2770
3286
|
|
|
2771
3287
|
// src/lib/opencode/session-db-size.ts
|
|
2772
|
-
import { statSync as
|
|
3288
|
+
import { statSync as statSync3 } from "fs";
|
|
2773
3289
|
import { join as join3 } from "path";
|
|
2774
3290
|
var LARGE_DB_THRESHOLD_BYTES = 268435456;
|
|
2775
3291
|
function statSessionDbBytes(homeDir) {
|
|
2776
3292
|
const dbPath = join3(homeDir, ".local", "share", "opencode", "opencode.db");
|
|
2777
3293
|
try {
|
|
2778
|
-
return
|
|
3294
|
+
return statSync3(dbPath).size;
|
|
2779
3295
|
} catch (err) {
|
|
2780
3296
|
const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
|
|
2781
3297
|
if (!isMissingFile) {
|
|
@@ -2801,11 +3317,11 @@ function buildSessionStoreSizeWarning(input) {
|
|
|
2801
3317
|
}
|
|
2802
3318
|
|
|
2803
3319
|
// src/lib/opencode/session-db-reclaim.ts
|
|
2804
|
-
import { statSync as
|
|
2805
|
-
import { dirname as
|
|
3320
|
+
import { statSync as statSync4, statfsSync } from "fs";
|
|
3321
|
+
import { dirname as dirname3 } from "path";
|
|
2806
3322
|
function insufficientSpaceReason(dbPath, requiredBytes) {
|
|
2807
3323
|
try {
|
|
2808
|
-
const fsStats = statfsSync(
|
|
3324
|
+
const fsStats = statfsSync(dirname3(dbPath));
|
|
2809
3325
|
const availableBytes = fsStats.bavail * fsStats.bsize;
|
|
2810
3326
|
if (availableBytes < requiredBytes) {
|
|
2811
3327
|
return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
|
|
@@ -2874,7 +3390,7 @@ async function reclaimSessionDbSpace(input) {
|
|
|
2874
3390
|
);
|
|
2875
3391
|
return { ok: false, skipped: "full-vacuum-blocked" };
|
|
2876
3392
|
}
|
|
2877
|
-
const fileBytesForGuard =
|
|
3393
|
+
const fileBytesForGuard = statSync4(dbPath).size;
|
|
2878
3394
|
const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
|
|
2879
3395
|
if (skipReason !== null) {
|
|
2880
3396
|
console.warn(
|
|
@@ -3002,12 +3518,12 @@ var StreamForwarder = class {
|
|
|
3002
3518
|
let endBody;
|
|
3003
3519
|
if (has_body) {
|
|
3004
3520
|
const chunks = [];
|
|
3005
|
-
bodyPromise = new Promise((
|
|
3521
|
+
bodyPromise = new Promise((resolve4) => {
|
|
3006
3522
|
pushBody = (buf) => {
|
|
3007
3523
|
chunks.push(buf);
|
|
3008
3524
|
};
|
|
3009
3525
|
endBody = () => {
|
|
3010
|
-
|
|
3526
|
+
resolve4(Buffer.concat(chunks));
|
|
3011
3527
|
};
|
|
3012
3528
|
});
|
|
3013
3529
|
}
|
|
@@ -3136,7 +3652,7 @@ function connectTunnel(options) {
|
|
|
3136
3652
|
} = options;
|
|
3137
3653
|
const tunnelUrl = getTunnelUrlConfig();
|
|
3138
3654
|
const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
|
|
3139
|
-
return new Promise((
|
|
3655
|
+
return new Promise((resolve4, reject) => {
|
|
3140
3656
|
const ws = new WebSocket2(url, {
|
|
3141
3657
|
headers: {
|
|
3142
3658
|
Authorization: authHeader
|
|
@@ -3200,7 +3716,7 @@ function connectTunnel(options) {
|
|
|
3200
3716
|
clearTimeout(connectionTimeout);
|
|
3201
3717
|
const connectedAgentId = message.agent_id ?? agentId;
|
|
3202
3718
|
onConnected?.(connectedAgentId);
|
|
3203
|
-
|
|
3719
|
+
resolve4({
|
|
3204
3720
|
ws,
|
|
3205
3721
|
close: () => ws.close(1e3, "CLI shutdown")
|
|
3206
3722
|
});
|
|
@@ -3331,10 +3847,10 @@ var RunnerConnection = class {
|
|
|
3331
3847
|
};
|
|
3332
3848
|
|
|
3333
3849
|
// src/lib/tunnel/ready-marker.ts
|
|
3334
|
-
import { writeFileSync } from "fs";
|
|
3850
|
+
import { writeFileSync as writeFileSync2 } from "fs";
|
|
3335
3851
|
function writeTunnelReadyMarker(path, agentId) {
|
|
3336
3852
|
try {
|
|
3337
|
-
|
|
3853
|
+
writeFileSync2(path, `${agentId}
|
|
3338
3854
|
`);
|
|
3339
3855
|
return { ok: true };
|
|
3340
3856
|
} catch (error2) {
|
|
@@ -3343,9 +3859,9 @@ function writeTunnelReadyMarker(path, agentId) {
|
|
|
3343
3859
|
}
|
|
3344
3860
|
|
|
3345
3861
|
// src/lib/replication.ts
|
|
3346
|
-
import { spawn as
|
|
3862
|
+
import { spawn as spawn4 } from "child_process";
|
|
3347
3863
|
function startSessionDbReplication(configPath) {
|
|
3348
|
-
return
|
|
3864
|
+
return spawn4("litestream", ["replicate", "-config", configPath], {
|
|
3349
3865
|
stdio: "inherit"
|
|
3350
3866
|
});
|
|
3351
3867
|
}
|
|
@@ -3358,9 +3874,35 @@ async function stopSessionDbReplication(child, timeoutMs) {
|
|
|
3358
3874
|
);
|
|
3359
3875
|
}
|
|
3360
3876
|
|
|
3361
|
-
// src/lib/
|
|
3877
|
+
// src/lib/process-liveness.ts
|
|
3362
3878
|
import { readFileSync as readFileSync3 } from "fs";
|
|
3363
|
-
|
|
3879
|
+
function isProcessAlive(pid) {
|
|
3880
|
+
try {
|
|
3881
|
+
process.kill(pid, 0);
|
|
3882
|
+
} catch (error2) {
|
|
3883
|
+
const code = error2.code;
|
|
3884
|
+
if (code === "ESRCH") return false;
|
|
3885
|
+
if (code === "EPERM") return true;
|
|
3886
|
+
console.error(
|
|
3887
|
+
`[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
3888
|
+
);
|
|
3889
|
+
return false;
|
|
3890
|
+
}
|
|
3891
|
+
if (process.platform !== "linux") return true;
|
|
3892
|
+
try {
|
|
3893
|
+
const status2 = readFileSync3(`/proc/${pid}/status`, "utf8");
|
|
3894
|
+
return !/^State:\s+Z(?:\s|$)/m.test(status2);
|
|
3895
|
+
} catch (error2) {
|
|
3896
|
+
console.error(
|
|
3897
|
+
`[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
3898
|
+
);
|
|
3899
|
+
return true;
|
|
3900
|
+
}
|
|
3901
|
+
}
|
|
3902
|
+
|
|
3903
|
+
// src/lib/openai-usage.ts
|
|
3904
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
3905
|
+
import { homedir as homedir3 } from "os";
|
|
3364
3906
|
import { join as join4 } from "path";
|
|
3365
3907
|
var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
|
|
3366
3908
|
var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
|
|
@@ -3375,7 +3917,7 @@ function isLocalCredentialProblem2(err) {
|
|
|
3375
3917
|
}
|
|
3376
3918
|
function readOpenCodeChatGptCredentials() {
|
|
3377
3919
|
try {
|
|
3378
|
-
const raw =
|
|
3920
|
+
const raw = readFileSync4(join4(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
|
|
3379
3921
|
let parsed;
|
|
3380
3922
|
try {
|
|
3381
3923
|
parsed = JSON.parse(raw);
|
|
@@ -3748,7 +4290,7 @@ function createResourceUsageCollector(homeDir) {
|
|
|
3748
4290
|
}
|
|
3749
4291
|
|
|
3750
4292
|
// src/lib/channels/driver.ts
|
|
3751
|
-
import { homedir as
|
|
4293
|
+
import { homedir as homedir4 } from "os";
|
|
3752
4294
|
|
|
3753
4295
|
// src/lib/runner-file-sync.ts
|
|
3754
4296
|
import { join as join6 } from "path";
|
|
@@ -3756,7 +4298,7 @@ import { join as join6 } from "path";
|
|
|
3756
4298
|
// src/lib/file-push.ts
|
|
3757
4299
|
import { randomUUID } from "crypto";
|
|
3758
4300
|
import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
|
|
3759
|
-
import { basename, dirname as
|
|
4301
|
+
import { basename, dirname as dirname4, isAbsolute, join as join5, relative, resolve as resolve2, sep } from "path";
|
|
3760
4302
|
var FILE_MODE = 384;
|
|
3761
4303
|
var DIRECTORY_MODE = 448;
|
|
3762
4304
|
async function writePushedFile(request) {
|
|
@@ -3787,7 +4329,7 @@ async function writePushedFile(request) {
|
|
|
3787
4329
|
}
|
|
3788
4330
|
try {
|
|
3789
4331
|
const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
|
|
3790
|
-
|
|
4332
|
+
dirname4(candidate)
|
|
3791
4333
|
);
|
|
3792
4334
|
const realTarget = join5(existingAncestor, ...missingSegments, basename(candidate));
|
|
3793
4335
|
const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
|
|
@@ -3799,8 +4341,8 @@ async function writePushedFile(request) {
|
|
|
3799
4341
|
}
|
|
3800
4342
|
if (missingSegments.length > 0) {
|
|
3801
4343
|
await createMissingDirectories(existingAncestor, missingSegments);
|
|
3802
|
-
const realParent = await realpath(
|
|
3803
|
-
if (realParent !==
|
|
4344
|
+
const realParent = await realpath(dirname4(realTarget));
|
|
4345
|
+
if (realParent !== dirname4(realTarget) || !contains(allowedDirectory, realTarget)) {
|
|
3804
4346
|
return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
|
|
3805
4347
|
path: realTarget,
|
|
3806
4348
|
bytes,
|
|
@@ -3843,7 +4385,7 @@ async function resolveNearestExistingAncestor(directory) {
|
|
|
3843
4385
|
try {
|
|
3844
4386
|
return { existingAncestor: await realpath(current), missingSegments };
|
|
3845
4387
|
} catch (err) {
|
|
3846
|
-
const parent =
|
|
4388
|
+
const parent = dirname4(current);
|
|
3847
4389
|
if (err.code !== "ENOENT" || parent === current) {
|
|
3848
4390
|
throw err;
|
|
3849
4391
|
}
|
|
@@ -3904,7 +4446,7 @@ async function createMissingDirectories(existingAncestor, missingSegments) {
|
|
|
3904
4446
|
}
|
|
3905
4447
|
}
|
|
3906
4448
|
async function writeAtomically(realTarget, content) {
|
|
3907
|
-
const temporaryPath = join5(
|
|
4449
|
+
const temporaryPath = join5(dirname4(realTarget), `.evident-push-${randomUUID()}.tmp`);
|
|
3908
4450
|
let handle;
|
|
3909
4451
|
try {
|
|
3910
4452
|
handle = await open2(temporaryPath, "wx", FILE_MODE);
|
|
@@ -4585,7 +5127,7 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
4585
5127
|
this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
4586
5128
|
this.now = config.now ?? (() => Date.now());
|
|
4587
5129
|
this.fileSyncDirectories = config.fileSyncDirectories ?? [];
|
|
4588
|
-
this.homeDir = config.homeDir ??
|
|
5130
|
+
this.homeDir = config.homeDir ?? homedir4();
|
|
4589
5131
|
this.maxActiveSessions = config.maxActiveSessions;
|
|
4590
5132
|
this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
|
|
4591
5133
|
this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
|
|
@@ -8006,7 +8548,7 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
8006
8548
|
}
|
|
8007
8549
|
if (!ctx.interactive) {
|
|
8008
8550
|
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
8009
|
-
const proc = await startOpenCode(ctx.port);
|
|
8551
|
+
const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
|
|
8010
8552
|
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
|
|
8011
8553
|
if (!health.healthy) {
|
|
8012
8554
|
return {
|
|
@@ -8074,7 +8616,7 @@ Port ${port} is already in use.`));
|
|
|
8074
8616
|
}
|
|
8075
8617
|
if (action === "start") {
|
|
8076
8618
|
const spinner = ora2("Starting OpenCode...").start();
|
|
8077
|
-
const proc = await startOpenCode(port);
|
|
8619
|
+
const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
|
|
8078
8620
|
const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
|
|
8079
8621
|
if (!health.healthy) {
|
|
8080
8622
|
spinner.fail("Failed to start OpenCode");
|
|
@@ -8086,6 +8628,316 @@ Port ${port} is already in use.`));
|
|
|
8086
8628
|
return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
|
|
8087
8629
|
}
|
|
8088
8630
|
|
|
8631
|
+
// src/lib/runner-credentials.ts
|
|
8632
|
+
import { chmodSync as chmodSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
8633
|
+
import { spawn as spawn5 } from "child_process";
|
|
8634
|
+
var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
|
|
8635
|
+
var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
|
|
8636
|
+
var GITHUB_PROBE_TIMEOUT_MS = 1e4;
|
|
8637
|
+
var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
|
|
8638
|
+
var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
|
|
8639
|
+
function commandError2(result) {
|
|
8640
|
+
return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
|
|
8641
|
+
}
|
|
8642
|
+
var runCommand2 = (command, args, opts) => {
|
|
8643
|
+
return new Promise((resolve4) => {
|
|
8644
|
+
let child;
|
|
8645
|
+
let stdout = "";
|
|
8646
|
+
let stderr = "";
|
|
8647
|
+
let settled = false;
|
|
8648
|
+
const timer = {};
|
|
8649
|
+
const finish = (result) => {
|
|
8650
|
+
if (settled) return;
|
|
8651
|
+
settled = true;
|
|
8652
|
+
if (timer.handle) clearTimeout(timer.handle);
|
|
8653
|
+
resolve4(result);
|
|
8654
|
+
};
|
|
8655
|
+
try {
|
|
8656
|
+
child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
|
|
8657
|
+
} catch (error2) {
|
|
8658
|
+
finish({
|
|
8659
|
+
code: null,
|
|
8660
|
+
stdout,
|
|
8661
|
+
stderr: error2 instanceof Error ? error2.message : String(error2),
|
|
8662
|
+
timedOut: false
|
|
8663
|
+
});
|
|
8664
|
+
return;
|
|
8665
|
+
}
|
|
8666
|
+
child.stdout?.setEncoding("utf8");
|
|
8667
|
+
child.stdout?.on("data", (chunk) => {
|
|
8668
|
+
stdout += chunk;
|
|
8669
|
+
});
|
|
8670
|
+
child.stderr?.setEncoding("utf8");
|
|
8671
|
+
child.stderr?.on("data", (chunk) => {
|
|
8672
|
+
stderr += chunk;
|
|
8673
|
+
});
|
|
8674
|
+
child.once("error", (error2) => {
|
|
8675
|
+
finish({
|
|
8676
|
+
code: null,
|
|
8677
|
+
stdout,
|
|
8678
|
+
stderr: stderr === "" ? error2.message : `${stderr}
|
|
8679
|
+
${error2.message}`,
|
|
8680
|
+
timedOut: false
|
|
8681
|
+
});
|
|
8682
|
+
});
|
|
8683
|
+
child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
|
|
8684
|
+
timer.handle = setTimeout(
|
|
8685
|
+
() => {
|
|
8686
|
+
child.kill("SIGKILL");
|
|
8687
|
+
finish({ code: null, stdout, stderr, timedOut: true });
|
|
8688
|
+
},
|
|
8689
|
+
Math.max(0, opts.timeoutMs)
|
|
8690
|
+
);
|
|
8691
|
+
});
|
|
8692
|
+
};
|
|
8693
|
+
function isEnvironmentObject(value) {
|
|
8694
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8695
|
+
}
|
|
8696
|
+
function secretFailure(marker, detail, log3) {
|
|
8697
|
+
const message = `${marker}: ${detail}`;
|
|
8698
|
+
log3(message, "error");
|
|
8699
|
+
return new Error(message);
|
|
8700
|
+
}
|
|
8701
|
+
async function installRunnerSecret({
|
|
8702
|
+
env,
|
|
8703
|
+
log: log3,
|
|
8704
|
+
commandRunner
|
|
8705
|
+
}) {
|
|
8706
|
+
const arn = env.RUNNER_SECRET_ARN?.trim();
|
|
8707
|
+
if (!arn) {
|
|
8708
|
+
log3("runner secret is not configured; continuing without GitHub and MCP credentials");
|
|
8709
|
+
return false;
|
|
8710
|
+
}
|
|
8711
|
+
const result = await (commandRunner ?? runCommand2)(
|
|
8712
|
+
"aws",
|
|
8713
|
+
[
|
|
8714
|
+
"secretsmanager",
|
|
8715
|
+
"get-secret-value",
|
|
8716
|
+
"--secret-id",
|
|
8717
|
+
arn,
|
|
8718
|
+
"--query",
|
|
8719
|
+
"SecretString",
|
|
8720
|
+
"--output",
|
|
8721
|
+
"text"
|
|
8722
|
+
],
|
|
8723
|
+
{ env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
|
|
8724
|
+
);
|
|
8725
|
+
if (result.timedOut) {
|
|
8726
|
+
throw secretFailure(
|
|
8727
|
+
"CREDENTIAL-RESTORE-TIMEOUT",
|
|
8728
|
+
`runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
|
|
8729
|
+
log3
|
|
8730
|
+
);
|
|
8731
|
+
}
|
|
8732
|
+
if (result.code !== 0) {
|
|
8733
|
+
throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
|
|
8734
|
+
}
|
|
8735
|
+
let payload;
|
|
8736
|
+
try {
|
|
8737
|
+
payload = JSON.parse(result.stdout);
|
|
8738
|
+
} catch (error2) {
|
|
8739
|
+
log3(
|
|
8740
|
+
`RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
8741
|
+
"warn"
|
|
8742
|
+
);
|
|
8743
|
+
return false;
|
|
8744
|
+
}
|
|
8745
|
+
if (!isEnvironmentObject(payload)) {
|
|
8746
|
+
log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
|
|
8747
|
+
return false;
|
|
8748
|
+
}
|
|
8749
|
+
let populated = 0;
|
|
8750
|
+
let skipped = 0;
|
|
8751
|
+
let githubTokenPopulated = false;
|
|
8752
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
8753
|
+
if (typeof value !== "string" || value.length === 0) continue;
|
|
8754
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
|
|
8755
|
+
log3(
|
|
8756
|
+
`RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
|
|
8757
|
+
"warn"
|
|
8758
|
+
);
|
|
8759
|
+
skipped += 1;
|
|
8760
|
+
continue;
|
|
8761
|
+
}
|
|
8762
|
+
env[key] = value;
|
|
8763
|
+
populated += 1;
|
|
8764
|
+
if (key === "GH_TOKEN") githubTokenPopulated = true;
|
|
8765
|
+
}
|
|
8766
|
+
if (populated === 0) {
|
|
8767
|
+
log3(
|
|
8768
|
+
"RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
|
|
8769
|
+
"warn"
|
|
8770
|
+
);
|
|
8771
|
+
} else {
|
|
8772
|
+
log3(
|
|
8773
|
+
`RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
|
|
8774
|
+
);
|
|
8775
|
+
}
|
|
8776
|
+
return githubTokenPopulated;
|
|
8777
|
+
}
|
|
8778
|
+
function restoreFailure(operation, result, log3) {
|
|
8779
|
+
const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
|
|
8780
|
+
log3(message, "error");
|
|
8781
|
+
return new Error(message);
|
|
8782
|
+
}
|
|
8783
|
+
async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
|
|
8784
|
+
const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
|
|
8785
|
+
if (result.timedOut) {
|
|
8786
|
+
log3(
|
|
8787
|
+
`CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
8788
|
+
"warn"
|
|
8789
|
+
);
|
|
8790
|
+
return result;
|
|
8791
|
+
}
|
|
8792
|
+
if (result.code !== 0) throw restoreFailure(operation, result, log3);
|
|
8793
|
+
return result;
|
|
8794
|
+
}
|
|
8795
|
+
async function restoreCredentialStores({
|
|
8796
|
+
env,
|
|
8797
|
+
log: log3,
|
|
8798
|
+
synchroniserRunner = runSynchroniser
|
|
8799
|
+
}) {
|
|
8800
|
+
await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
|
|
8801
|
+
await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
|
|
8802
|
+
const result = await synchroniserRunner(["model-auth-ready"], {
|
|
8803
|
+
timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
|
|
8804
|
+
});
|
|
8805
|
+
if (result.timedOut) {
|
|
8806
|
+
log3(
|
|
8807
|
+
`CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
|
|
8808
|
+
"warn"
|
|
8809
|
+
);
|
|
8810
|
+
return;
|
|
8811
|
+
}
|
|
8812
|
+
switch (result.code) {
|
|
8813
|
+
case 0:
|
|
8814
|
+
return;
|
|
8815
|
+
case 10:
|
|
8816
|
+
log3(
|
|
8817
|
+
`no model credentials under s3://${env.LITESTREAM_BUCKET ?? ""}/${env.LITESTREAM_PREFIX ?? ""}/ (neither claude/credentials.json nor opencode/auth.json yielded valid JSON) and neither ANTHROPIC_API_KEY nor OPENAI_API_KEY is set. This VM boots and connects; a turn that needs a model provider fails until one is connected. See 'Seeding a credential store' in infrastructure/evident-runner/MICROVM.md.`,
|
|
8818
|
+
"warn"
|
|
8819
|
+
);
|
|
8820
|
+
return;
|
|
8821
|
+
default:
|
|
8822
|
+
log3("could not determine whether this VM has model credentials", "warn");
|
|
8823
|
+
}
|
|
8824
|
+
}
|
|
8825
|
+
var GIT_CREDENTIAL_HELPER_CONTENT = [
|
|
8826
|
+
"#!/usr/bin/env bash",
|
|
8827
|
+
'[ "$1" = get ] || exit 0',
|
|
8828
|
+
"echo username=x-access-token",
|
|
8829
|
+
'echo "password=${GH_TOKEN}"',
|
|
8830
|
+
""
|
|
8831
|
+
].join("\n");
|
|
8832
|
+
async function probeGitHubAccess({ env, log: log3 }) {
|
|
8833
|
+
const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
|
|
8834
|
+
env,
|
|
8835
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8836
|
+
});
|
|
8837
|
+
if (auth.timedOut) {
|
|
8838
|
+
log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
|
|
8839
|
+
return;
|
|
8840
|
+
}
|
|
8841
|
+
if (auth.code !== 0) {
|
|
8842
|
+
log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
|
|
8843
|
+
return;
|
|
8844
|
+
}
|
|
8845
|
+
log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
|
|
8846
|
+
const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
|
|
8847
|
+
env,
|
|
8848
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8849
|
+
});
|
|
8850
|
+
if (remote.code !== 0 || remote.timedOut) return;
|
|
8851
|
+
const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
|
|
8852
|
+
if (!repo) return;
|
|
8853
|
+
const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
|
|
8854
|
+
env,
|
|
8855
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8856
|
+
});
|
|
8857
|
+
if (repository.timedOut) {
|
|
8858
|
+
log3(
|
|
8859
|
+
`GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
|
|
8860
|
+
"warn"
|
|
8861
|
+
);
|
|
8862
|
+
} else if (repository.code !== 0) {
|
|
8863
|
+
log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
|
|
8864
|
+
}
|
|
8865
|
+
}
|
|
8866
|
+
async function configureGitHubAccess({ env, log: log3 }) {
|
|
8867
|
+
if (!env.GH_TOKEN) {
|
|
8868
|
+
log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
|
|
8869
|
+
return;
|
|
8870
|
+
}
|
|
8871
|
+
try {
|
|
8872
|
+
env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
|
|
8873
|
+
writeFileSync3(GIT_CONFIG_GLOBAL, "");
|
|
8874
|
+
writeFileSync3(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
|
|
8875
|
+
chmodSync2(GIT_CREDENTIAL_HELPER, 448);
|
|
8876
|
+
const config = [
|
|
8877
|
+
["user.name", env.GIT_USER_NAME ?? "evident-bot"],
|
|
8878
|
+
["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
|
|
8879
|
+
["init.defaultBranch", "main"],
|
|
8880
|
+
["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
|
|
8881
|
+
];
|
|
8882
|
+
for (const [key, value] of config) {
|
|
8883
|
+
const result = await runCommand2("git", ["config", "--global", key, value], {
|
|
8884
|
+
env,
|
|
8885
|
+
timeoutMs: GITHUB_PROBE_TIMEOUT_MS
|
|
8886
|
+
});
|
|
8887
|
+
if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
|
|
8888
|
+
}
|
|
8889
|
+
} catch (error2) {
|
|
8890
|
+
log3(
|
|
8891
|
+
`GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
|
|
8892
|
+
"warn"
|
|
8893
|
+
);
|
|
8894
|
+
return;
|
|
8895
|
+
}
|
|
8896
|
+
void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
|
|
8897
|
+
log3(
|
|
8898
|
+
`GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
|
|
8899
|
+
"warn"
|
|
8900
|
+
);
|
|
8901
|
+
});
|
|
8902
|
+
}
|
|
8903
|
+
|
|
8904
|
+
// src/lib/opencode/config-overlay.ts
|
|
8905
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
8906
|
+
import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
|
|
8907
|
+
import { isAbsolute as isAbsolute2, join as join7, resolve as resolve3 } from "path";
|
|
8908
|
+
function isFile(filePath) {
|
|
8909
|
+
return existsSync2(filePath) && statSync5(filePath).isFile();
|
|
8910
|
+
}
|
|
8911
|
+
function applyRunnerOpenCodeConfig({
|
|
8912
|
+
overlayPath,
|
|
8913
|
+
cwd = process.cwd(),
|
|
8914
|
+
log: log3
|
|
8915
|
+
}) {
|
|
8916
|
+
if (!overlayPath) {
|
|
8917
|
+
log3("runner OpenCode config is not configured; using the baked project config", "debug");
|
|
8918
|
+
return;
|
|
8919
|
+
}
|
|
8920
|
+
const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
|
|
8921
|
+
const target = isFile(join7(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
|
|
8922
|
+
if (!isFile(source)) {
|
|
8923
|
+
log3(
|
|
8924
|
+
`RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
|
|
8925
|
+
"error"
|
|
8926
|
+
);
|
|
8927
|
+
return;
|
|
8928
|
+
}
|
|
8929
|
+
copyFileSync(source, join7(cwd, target));
|
|
8930
|
+
try {
|
|
8931
|
+
execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
8932
|
+
stdio: "ignore"
|
|
8933
|
+
});
|
|
8934
|
+
} catch (error2) {
|
|
8935
|
+
const detail = error2 instanceof Error ? error2.message : String(error2);
|
|
8936
|
+
log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
|
|
8937
|
+
}
|
|
8938
|
+
log3(`Applied runner OpenCode config ${source} to ${join7(cwd, target)}`);
|
|
8939
|
+
}
|
|
8940
|
+
|
|
8089
8941
|
// src/commands/run.ts
|
|
8090
8942
|
var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
8091
8943
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
@@ -8123,11 +8975,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
|
|
|
8123
8975
|
if (trimmed === "") {
|
|
8124
8976
|
throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
|
|
8125
8977
|
}
|
|
8126
|
-
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ?
|
|
8127
|
-
if (!
|
|
8978
|
+
const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join8(homeDir, trimmed.slice(2)) : trimmed;
|
|
8979
|
+
if (!isAbsolute3(expanded)) {
|
|
8128
8980
|
throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
|
|
8129
8981
|
}
|
|
8130
|
-
const normalized =
|
|
8982
|
+
const normalized = resolvePath2(expanded);
|
|
8131
8983
|
if (parse(normalized).root === normalized) {
|
|
8132
8984
|
throw new Error(
|
|
8133
8985
|
`--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
|
|
@@ -8243,7 +9095,7 @@ function logActivity(state, entry) {
|
|
|
8243
9095
|
}
|
|
8244
9096
|
function reportSessionDbRecovery(state) {
|
|
8245
9097
|
try {
|
|
8246
|
-
const report = drainSessionDbRecoveryReport({ homeDir:
|
|
9098
|
+
const report = drainSessionDbRecoveryReport({ homeDir: homedir5(), env: process.env });
|
|
8247
9099
|
for (const record of report.records) {
|
|
8248
9100
|
const activity = buildSessionDbRecoveryActivity(record);
|
|
8249
9101
|
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
@@ -8261,6 +9113,16 @@ function reportSessionDbRecovery(state) {
|
|
|
8261
9113
|
);
|
|
8262
9114
|
}
|
|
8263
9115
|
}
|
|
9116
|
+
function reportSessionDbRecoveryRecord(state, record) {
|
|
9117
|
+
const activity = buildSessionDbRecoveryActivity(record);
|
|
9118
|
+
if (!activity) throw new Error("could not map session-DB recovery record");
|
|
9119
|
+
logActivity(state, {
|
|
9120
|
+
type: activity.level === "error" ? "error" : "info",
|
|
9121
|
+
level: activity.level,
|
|
9122
|
+
...activity.level === "error" ? { error: activity.message } : { message: activity.message },
|
|
9123
|
+
metadata: activity.metadata
|
|
9124
|
+
});
|
|
9125
|
+
}
|
|
8264
9126
|
function displayStatus(state) {
|
|
8265
9127
|
if (!state.interactive) return;
|
|
8266
9128
|
const attempt = state.connection?.reconnectAttempt ?? 0;
|
|
@@ -8433,7 +9295,7 @@ async function driveChannels(state, driver) {
|
|
|
8433
9295
|
}
|
|
8434
9296
|
}
|
|
8435
9297
|
}
|
|
8436
|
-
await new Promise((
|
|
9298
|
+
await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
|
|
8437
9299
|
const cycleMs = performance.now() - cycleStartedAtMs;
|
|
8438
9300
|
if (idleThisCycle) idleMs += cycleMs;
|
|
8439
9301
|
if (unreachableThisCycle) unreachableMs += cycleMs;
|
|
@@ -8456,7 +9318,7 @@ async function driveChannels(state, driver) {
|
|
|
8456
9318
|
var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
|
|
8457
9319
|
var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
|
|
8458
9320
|
function sessionDbPath() {
|
|
8459
|
-
return
|
|
9321
|
+
return join8(homedir5(), ".local", "share", "opencode", "opencode.db");
|
|
8460
9322
|
}
|
|
8461
9323
|
async function runSweep(state, driver, config) {
|
|
8462
9324
|
const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
|
|
@@ -8539,7 +9401,7 @@ function scheduleSessionCleanup(state, driver, options) {
|
|
|
8539
9401
|
for (const warning2 of config.warnings) {
|
|
8540
9402
|
logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
|
|
8541
9403
|
}
|
|
8542
|
-
const dbBytes = statSessionDbBytes(
|
|
9404
|
+
const dbBytes = statSessionDbBytes(homedir5());
|
|
8543
9405
|
void (async () => {
|
|
8544
9406
|
const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
|
|
8545
9407
|
const sizeWarning = buildSessionStoreSizeWarning({
|
|
@@ -8740,7 +9602,7 @@ function scheduleResourceUsageReporting(state, options) {
|
|
|
8740
9602
|
});
|
|
8741
9603
|
return;
|
|
8742
9604
|
}
|
|
8743
|
-
const collect = createResourceUsageCollector(
|
|
9605
|
+
const collect = createResourceUsageCollector(homedir5());
|
|
8744
9606
|
let consecutiveFailures = 0;
|
|
8745
9607
|
const tick = async () => {
|
|
8746
9608
|
try {
|
|
@@ -8913,7 +9775,12 @@ async function run(options) {
|
|
|
8913
9775
|
let fileSyncDirectories;
|
|
8914
9776
|
try {
|
|
8915
9777
|
logLevel = resolveLogLevel(options);
|
|
8916
|
-
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo,
|
|
9778
|
+
fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir5());
|
|
9779
|
+
if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
|
|
9780
|
+
throw new Error(
|
|
9781
|
+
"--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
|
|
9782
|
+
);
|
|
9783
|
+
}
|
|
8917
9784
|
} catch (error2) {
|
|
8918
9785
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
8919
9786
|
if (options.json) {
|
|
@@ -9005,8 +9872,8 @@ async function run(options) {
|
|
|
9005
9872
|
return true;
|
|
9006
9873
|
}
|
|
9007
9874
|
);
|
|
9008
|
-
const timedOut = new Promise((
|
|
9009
|
-
timer = setTimeout(() =>
|
|
9875
|
+
const timedOut = new Promise((resolve4) => {
|
|
9876
|
+
timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
|
|
9010
9877
|
});
|
|
9011
9878
|
if (!await Promise.race([flushed, timedOut])) {
|
|
9012
9879
|
log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
|
|
@@ -9146,7 +10013,67 @@ async function run(options) {
|
|
|
9146
10013
|
} else {
|
|
9147
10014
|
log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
|
|
9148
10015
|
}
|
|
10016
|
+
if (options.restoreRunnerCredentials) {
|
|
10017
|
+
log2(state, "Restoring runner credentials before starting OpenCode");
|
|
10018
|
+
const credentialContext = {
|
|
10019
|
+
env: process.env,
|
|
10020
|
+
log: (message, level = "info") => {
|
|
10021
|
+
if (level === "error") {
|
|
10022
|
+
logActivity(state, { type: "error", error: message });
|
|
10023
|
+
} else {
|
|
10024
|
+
logActivity(state, { type: "info", level, message });
|
|
10025
|
+
}
|
|
10026
|
+
}
|
|
10027
|
+
};
|
|
10028
|
+
const githubTokenPopulated = await installRunnerSecret(credentialContext);
|
|
10029
|
+
await restoreCredentialStores(credentialContext);
|
|
10030
|
+
if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
|
|
10031
|
+
}
|
|
10032
|
+
let sessionDbVerifyFatal = false;
|
|
10033
|
+
if (!options.restoreSessionDb) {
|
|
10034
|
+
log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
|
|
10035
|
+
} else {
|
|
10036
|
+
const health = await checkOpenCodeHealth(state.port);
|
|
10037
|
+
if (health.healthy) {
|
|
10038
|
+
log2(
|
|
10039
|
+
state,
|
|
10040
|
+
"Skipping session-DB restore: OpenCode is already serving this database",
|
|
10041
|
+
"debug"
|
|
10042
|
+
);
|
|
10043
|
+
} else {
|
|
10044
|
+
const result = await restoreAndVerifySessionDb({
|
|
10045
|
+
dbPath: sessionDbPath(),
|
|
10046
|
+
litestreamConfig: options.litestreamConfig,
|
|
10047
|
+
noReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10048
|
+
env: process.env,
|
|
10049
|
+
log: (message, level = "info") => {
|
|
10050
|
+
if (level === "error") {
|
|
10051
|
+
logActivity(state, { type: "error", error: message });
|
|
10052
|
+
} else {
|
|
10053
|
+
logActivity(state, { type: "info", level, message });
|
|
10054
|
+
}
|
|
10055
|
+
},
|
|
10056
|
+
reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
|
|
10057
|
+
});
|
|
10058
|
+
sessionDbVerifyFatal = result.verifyFatal;
|
|
10059
|
+
}
|
|
10060
|
+
}
|
|
9149
10061
|
reportSessionDbRecovery(state);
|
|
10062
|
+
if (sessionDbVerifyFatal) {
|
|
10063
|
+
throw new Error(
|
|
10064
|
+
"SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
|
|
10065
|
+
);
|
|
10066
|
+
}
|
|
10067
|
+
applyRunnerOpenCodeConfig({
|
|
10068
|
+
overlayPath: options.opencodeConfigOverlay,
|
|
10069
|
+
log: (message, level = "info") => {
|
|
10070
|
+
if (level === "error") {
|
|
10071
|
+
logActivity(state, { type: "error", error: message });
|
|
10072
|
+
} else {
|
|
10073
|
+
logActivity(state, { type: "info", level, message });
|
|
10074
|
+
}
|
|
10075
|
+
}
|
|
10076
|
+
});
|
|
9150
10077
|
const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
|
|
9151
10078
|
for (const warning2 of opencodeStartTimeoutWarnings) {
|
|
9152
10079
|
logActivity(state, { type: "info", level: "warn", message: warning2 });
|
|
@@ -9162,11 +10089,24 @@ async function run(options) {
|
|
|
9162
10089
|
interactive: state.interactive,
|
|
9163
10090
|
agentId: state.agentId,
|
|
9164
10091
|
log: (message) => log2(state, message),
|
|
9165
|
-
startTimeoutMs: opencodeStartTimeoutMs
|
|
10092
|
+
startTimeoutMs: opencodeStartTimeoutMs,
|
|
10093
|
+
inheritStdio: Boolean(options.opencodePidFile)
|
|
9166
10094
|
});
|
|
9167
10095
|
state.port = oc.port;
|
|
9168
|
-
state.opencodeProcess = oc.process;
|
|
10096
|
+
state.opencodeProcess = options.opencodePidFile ? null : oc.process;
|
|
9169
10097
|
state.opencodeVersion = oc.version;
|
|
10098
|
+
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
10099
|
+
try {
|
|
10100
|
+
writeFileSync4(options.opencodePidFile, `${oc.process.pid}
|
|
10101
|
+
`, { mode: 384 });
|
|
10102
|
+
chmodSync3(options.opencodePidFile, 384);
|
|
10103
|
+
} catch (error2) {
|
|
10104
|
+
logActivity(state, {
|
|
10105
|
+
type: "error",
|
|
10106
|
+
error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10107
|
+
});
|
|
10108
|
+
}
|
|
10109
|
+
}
|
|
9170
10110
|
state.opencodeConnected = oc.notReadyReason === null;
|
|
9171
10111
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
9172
10112
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
@@ -9203,7 +10143,75 @@ async function run(options) {
|
|
|
9203
10143
|
ocSpinner?.fail(error2.message);
|
|
9204
10144
|
throw error2;
|
|
9205
10145
|
}
|
|
9206
|
-
if (options.
|
|
10146
|
+
if (options.litestreamPidFile) {
|
|
10147
|
+
if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
|
|
10148
|
+
log2(
|
|
10149
|
+
state,
|
|
10150
|
+
`Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
|
|
10151
|
+
);
|
|
10152
|
+
} else if (!options.litestreamConfig) {
|
|
10153
|
+
logActivity(state, {
|
|
10154
|
+
type: "info",
|
|
10155
|
+
level: "warn",
|
|
10156
|
+
message: "Skipping Litestream replication because no configuration file was provided"
|
|
10157
|
+
});
|
|
10158
|
+
} else {
|
|
10159
|
+
let existingPid;
|
|
10160
|
+
if (existsSync3(options.litestreamPidFile)) {
|
|
10161
|
+
try {
|
|
10162
|
+
const rawPid = readFileSync5(options.litestreamPidFile, "utf8").trim();
|
|
10163
|
+
const parsedPid = Number(rawPid);
|
|
10164
|
+
if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
|
|
10165
|
+
existingPid = parsedPid;
|
|
10166
|
+
}
|
|
10167
|
+
} catch (error2) {
|
|
10168
|
+
logActivity(state, {
|
|
10169
|
+
type: "info",
|
|
10170
|
+
level: "warn",
|
|
10171
|
+
message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10172
|
+
});
|
|
10173
|
+
}
|
|
10174
|
+
}
|
|
10175
|
+
if (existingPid !== void 0 && isProcessAlive(existingPid)) {
|
|
10176
|
+
log2(state, `Litestream replication is already running with pid ${existingPid}`);
|
|
10177
|
+
} else {
|
|
10178
|
+
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
10179
|
+
state.litestreamProcess = null;
|
|
10180
|
+
let failureHandled = false;
|
|
10181
|
+
const reportImageOwnedReplicationFailure = (message) => {
|
|
10182
|
+
if (failureHandled || state.shuttingDown || !state.running) return;
|
|
10183
|
+
failureHandled = true;
|
|
10184
|
+
logActivity(state, { type: "error", error: message });
|
|
10185
|
+
if (state.interactive) displayStatus(state);
|
|
10186
|
+
};
|
|
10187
|
+
litestreamProcess.on("exit", (code, signal) => {
|
|
10188
|
+
reportImageOwnedReplicationFailure(
|
|
10189
|
+
`Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
|
|
10190
|
+
);
|
|
10191
|
+
});
|
|
10192
|
+
litestreamProcess.on("error", (error2) => {
|
|
10193
|
+
reportImageOwnedReplicationFailure(
|
|
10194
|
+
`Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10195
|
+
);
|
|
10196
|
+
});
|
|
10197
|
+
try {
|
|
10198
|
+
if (litestreamProcess.pid !== void 0) {
|
|
10199
|
+
writeFileSync4(options.litestreamPidFile, `${litestreamProcess.pid}
|
|
10200
|
+
`, {
|
|
10201
|
+
mode: 384
|
|
10202
|
+
});
|
|
10203
|
+
chmodSync3(options.litestreamPidFile, 384);
|
|
10204
|
+
}
|
|
10205
|
+
} catch (error2) {
|
|
10206
|
+
logActivity(state, {
|
|
10207
|
+
type: "error",
|
|
10208
|
+
error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
10209
|
+
});
|
|
10210
|
+
}
|
|
10211
|
+
log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
|
|
10212
|
+
}
|
|
10213
|
+
}
|
|
10214
|
+
} else if (options.litestreamConfig) {
|
|
9207
10215
|
const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
|
|
9208
10216
|
state.litestreamProcess = litestreamProcess;
|
|
9209
10217
|
let failureHandled = false;
|
|
@@ -9248,7 +10256,7 @@ async function run(options) {
|
|
|
9248
10256
|
// #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
|
|
9249
10257
|
// REJECTED with `file_sync_disabled` on the ack, not silently ignored.
|
|
9250
10258
|
fileSyncDirectories,
|
|
9251
|
-
homeDir:
|
|
10259
|
+
homeDir: homedir5(),
|
|
9252
10260
|
maxActiveSessions,
|
|
9253
10261
|
log: (entry) => (
|
|
9254
10262
|
// Thread the driver's real level straight through so `debug`/`warn`
|
|
@@ -9500,6 +10508,24 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9500
10508
|
).option(
|
|
9501
10509
|
"--litestream-config <path>",
|
|
9502
10510
|
"Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
|
|
10511
|
+
).option(
|
|
10512
|
+
"--opencode-pid-file <path>",
|
|
10513
|
+
"Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
10514
|
+
).option(
|
|
10515
|
+
"--litestream-pid-file <path>",
|
|
10516
|
+
"Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
|
|
10517
|
+
).option(
|
|
10518
|
+
"--session-db-no-replicate-marker <path>",
|
|
10519
|
+
"Read this marker as a gate before starting Litestream; the session-DB restore writes it when this boot's database is not safe to replicate."
|
|
10520
|
+
).option(
|
|
10521
|
+
"--restore-session-db",
|
|
10522
|
+
"Restore and verify the OpenCode session database before starting OpenCode; requires --litestream-config for the replica and --session-db-no-replicate-marker for give-up records."
|
|
10523
|
+
).option(
|
|
10524
|
+
"--restore-runner-credentials",
|
|
10525
|
+
"Restore the hosted runner secret and persisted credential stores before starting OpenCode."
|
|
10526
|
+
).option(
|
|
10527
|
+
"--opencode-config-overlay <path>",
|
|
10528
|
+
"Apply this runner-provided OpenCode config before starting OpenCode."
|
|
9503
10529
|
).action(
|
|
9504
10530
|
(options) => {
|
|
9505
10531
|
run({
|
|
@@ -9532,7 +10558,13 @@ program.command("run").description("Connect to Evident and process messages").op
|
|
|
9532
10558
|
// resolveFileSyncDirectories.
|
|
9533
10559
|
enableFileSyncTo: options.enableFileSyncTo,
|
|
9534
10560
|
tunnelReadyFile: options.tunnelReadyFile,
|
|
9535
|
-
litestreamConfig: options.litestreamConfig
|
|
10561
|
+
litestreamConfig: options.litestreamConfig,
|
|
10562
|
+
opencodePidFile: options.opencodePidFile,
|
|
10563
|
+
litestreamPidFile: options.litestreamPidFile,
|
|
10564
|
+
sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
|
|
10565
|
+
restoreSessionDb: options.restoreSessionDb,
|
|
10566
|
+
restoreRunnerCredentials: options.restoreRunnerCredentials,
|
|
10567
|
+
opencodeConfigOverlay: options.opencodeConfigOverlay
|
|
9536
10568
|
});
|
|
9537
10569
|
}
|
|
9538
10570
|
);
|