@evident-ai/cli 3.4.1-dev.c6e7cf0 → 3.4.1-dev.cb56b3f

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/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((resolve3) => {
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
- resolve3();
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((resolve3) => setTimeout(resolve3, ms));
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((resolve3) => {
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
- resolve3(data.trim());
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
- resolve3(chunk.toString().trim());
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 { homedir as homedir4 } from "os";
1187
- import { isAbsolute as isAbsolute2, join as join7, parse, resolve as resolvePath } from "path";
1186
+ import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync6 } 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 [value];
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: "This runner started with a fresh session database 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."
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,667 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
1661
1703
  if (health.healthy) {
1662
1704
  return health;
1663
1705
  }
1664
- await new Promise((resolve3) => setTimeout(resolve3, 1e3));
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
+ let abortListener;
1732
+ let spawnListener;
1733
+ const finish = (result) => {
1734
+ if (settled) return;
1735
+ settled = true;
1736
+ if (timer.handle) clearTimeout(timer.handle);
1737
+ if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
1738
+ if (spawnListener) child.removeListener("spawn", spawnListener);
1739
+ resolve4(result);
1740
+ };
1741
+ try {
1742
+ child = spawn("runner-synchroniser", args, {
1743
+ env: opts.env ?? process.env,
1744
+ stdio: ["ignore", "pipe", "pipe"]
1745
+ });
1746
+ } catch (error2) {
1747
+ finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
1748
+ return;
1749
+ }
1750
+ child.stdout?.setEncoding("utf8");
1751
+ child.stdout?.on("data", (chunk) => {
1752
+ stdout += chunk;
1753
+ });
1754
+ child.stderr?.setEncoding("utf8");
1755
+ child.stderr?.on("data", (chunk) => {
1756
+ stderr += chunk;
1757
+ });
1758
+ child.once("error", (error2) => {
1759
+ finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
1760
+ });
1761
+ child.once("close", (code) => {
1762
+ finish({ code, stdout, stderr, timedOut: false });
1763
+ });
1764
+ if (opts.signal) {
1765
+ const killChild = () => {
1766
+ if (child.pid === void 0) {
1767
+ if (!spawnListener) {
1768
+ spawnListener = killChild;
1769
+ child.once("spawn", spawnListener);
1770
+ }
1771
+ return;
1772
+ }
1773
+ child.kill("SIGKILL");
1774
+ };
1775
+ abortListener = killChild;
1776
+ if (opts.signal.aborted) {
1777
+ abortListener();
1778
+ } else {
1779
+ opts.signal.addEventListener("abort", abortListener, { once: true });
1780
+ if (opts.signal.aborted) abortListener();
1781
+ }
1782
+ }
1783
+ timer.handle = setTimeout(
1784
+ () => {
1785
+ child.kill("SIGKILL");
1786
+ finish({ code: null, stdout, stderr, timedOut: true });
1787
+ },
1788
+ Math.max(0, opts.timeoutMs)
1789
+ );
1790
+ });
1791
+ }
1792
+
1793
+ // src/lib/opencode/session-db-boot.ts
1794
+ var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
1795
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
1796
+ var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
1797
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
1798
+ function commandError(result) {
1799
+ return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
1800
+ }
1801
+ function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
1802
+ options.reportRecovery({
1803
+ v: 1,
1804
+ event: "session_db_recovery",
1805
+ at: (/* @__PURE__ */ new Date()).toISOString(),
1806
+ stage,
1807
+ outcome,
1808
+ severity: "error",
1809
+ reason,
1810
+ litestream_exit_code: litestreamExitCode,
1811
+ attempt: null,
1812
+ replica_objects: null,
1813
+ replica_bytes: null,
1814
+ quarantine_destination: null,
1815
+ quarantined_objects: null,
1816
+ quarantine_failed_objects: null,
1817
+ quarantined_bytes: null,
1818
+ verified_restore_point: null,
1819
+ restore_points_tried: null,
1820
+ provenance_reason: null,
1821
+ provenance_migration_delta: null,
1822
+ replication_suspended: stage === "restore"
1823
+ });
1824
+ }
1825
+ function clearMarker(options) {
1826
+ if (!options.noReplicateMarker) return;
1827
+ try {
1828
+ unlinkSync2(options.noReplicateMarker);
1829
+ } catch (error2) {
1830
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
1831
+ options.log(
1832
+ `Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1833
+ "warn"
1834
+ );
1835
+ }
1836
+ }
1837
+ function markNoReplicate(options, message) {
1838
+ if (options.noReplicateMarker) {
1839
+ try {
1840
+ mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
1841
+ writeFileSync(options.noReplicateMarker, "");
1842
+ } catch (error2) {
1843
+ options.log(
1844
+ `Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1845
+ "error"
1846
+ );
1847
+ }
1848
+ }
1849
+ options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
1850
+ }
1851
+ function discardSessionDbDebris(options) {
1852
+ for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
1853
+ try {
1854
+ unlinkSync2(path);
1855
+ } catch (error2) {
1856
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
1857
+ options.log(
1858
+ `Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1859
+ "warn"
1860
+ );
1861
+ }
1862
+ }
1863
+ }
1864
+ function splitDiagnostics(text) {
1865
+ return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
1866
+ }
1867
+ function logSynchroniserDiagnostics(result, options) {
1868
+ for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
1869
+ }
1870
+ function parseSingleQuotedAssignment(line) {
1871
+ const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
1872
+ if (!match || !match[2].startsWith("'")) return null;
1873
+ const valueSource = match[2];
1874
+ let value = "";
1875
+ for (let index = 1; index < valueSource.length; index++) {
1876
+ const character = valueSource[index];
1877
+ if (character !== "'") {
1878
+ value += character;
1879
+ continue;
1880
+ }
1881
+ if (index === valueSource.length - 1) return [match[1], value];
1882
+ if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
1883
+ value += "'";
1884
+ index += 3;
1885
+ }
1886
+ return null;
1887
+ }
1888
+ function parseSynchroniserEnv(stdout) {
1889
+ const values = {};
1890
+ for (const line of stdout.split("\n")) {
1891
+ if (line.trim() === "") continue;
1892
+ const assignment = parseSingleQuotedAssignment(line);
1893
+ if (!assignment) return null;
1894
+ values[assignment[0]] = assignment[1];
1895
+ }
1896
+ return values;
1897
+ }
1898
+ function runCommand(command, args, options) {
1899
+ return new Promise((resolve4) => {
1900
+ let child;
1901
+ let stdout = "";
1902
+ let stderr = "";
1903
+ let settled = false;
1904
+ const finish = (result) => {
1905
+ if (settled) return;
1906
+ settled = true;
1907
+ if (timer) clearTimeout(timer);
1908
+ resolve4(result);
1909
+ };
1910
+ try {
1911
+ child = spawn2(command, args, {
1912
+ env: options.env,
1913
+ stdio: ["ignore", "pipe", "pipe"]
1914
+ });
1915
+ } catch (error2) {
1916
+ resolve4({
1917
+ code: null,
1918
+ stdout,
1919
+ stderr: error2 instanceof Error ? error2.message : String(error2),
1920
+ timedOut: false
1921
+ });
1922
+ return;
1923
+ }
1924
+ child.stdout?.setEncoding("utf8");
1925
+ child.stdout?.on("data", (chunk) => {
1926
+ stdout += chunk;
1927
+ });
1928
+ child.stderr?.setEncoding("utf8");
1929
+ child.stderr?.on("data", (chunk) => {
1930
+ stderr += chunk;
1931
+ });
1932
+ child.once("error", (error2) => {
1933
+ finish({
1934
+ code: null,
1935
+ stdout,
1936
+ stderr: stderr === "" ? error2.message : `${stderr}
1937
+ ${error2.message}`,
1938
+ timedOut: false
1939
+ });
1940
+ });
1941
+ child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
1942
+ const timer = setTimeout(
1943
+ () => {
1944
+ child.kill("SIGKILL");
1945
+ finish({ code: null, stdout, stderr, timedOut: true });
1946
+ },
1947
+ Math.max(0, options.timeoutMs)
1948
+ );
1949
+ });
1950
+ }
1951
+ async function ensureLitestreamConfig(options, env) {
1952
+ const configPath = options.litestreamConfig;
1953
+ if (!configPath) {
1954
+ markNoReplicate(options, "no Litestream configuration path was provided");
1955
+ reportRecord(
1956
+ "restore",
1957
+ "restore_misconfigured",
1958
+ "litestream_config_unavailable",
1959
+ null,
1960
+ options
1961
+ );
1962
+ return null;
1963
+ }
1964
+ try {
1965
+ if (statSync2(configPath).size > 0) return configPath;
1966
+ } catch (error2) {
1967
+ if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
1968
+ options.log(
1969
+ `Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1970
+ "warn"
1971
+ );
1972
+ }
1973
+ }
1974
+ const rendered = await runSynchroniser(["litestream-config"], {
1975
+ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
1976
+ env
1977
+ });
1978
+ logSynchroniserDiagnostics(rendered, options);
1979
+ if (rendered.timedOut || rendered.code !== 0) {
1980
+ options.log(
1981
+ `Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
1982
+ "error"
1983
+ );
1984
+ markNoReplicate(options, `could not generate ${configPath}`);
1985
+ reportRecord(
1986
+ "restore",
1987
+ "restore_misconfigured",
1988
+ "litestream_config_unavailable",
1989
+ null,
1990
+ options
1991
+ );
1992
+ return null;
1993
+ }
1994
+ try {
1995
+ mkdirSync(dirname2(configPath), { recursive: true });
1996
+ writeFileSync(configPath, rendered.stdout);
1997
+ } catch (error2) {
1998
+ options.log(
1999
+ `Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
2000
+ "error"
2001
+ );
2002
+ markNoReplicate(options, `could not generate ${configPath}`);
2003
+ reportRecord(
2004
+ "restore",
2005
+ "restore_misconfigured",
2006
+ "litestream_config_unavailable",
2007
+ null,
2008
+ options
2009
+ );
2010
+ return null;
2011
+ }
2012
+ const version2 = await runCommand("litestream", ["version"], {
2013
+ env,
2014
+ timeoutMs: 1e4
2015
+ });
2016
+ const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
2017
+ const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
2018
+ options.log(
2019
+ `litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
2020
+ );
2021
+ return configPath;
2022
+ }
2023
+ function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
2024
+ discardSessionDbDebris(options);
2025
+ markNoReplicate(options, message);
2026
+ reportRecord("restore", outcome, reason, litestreamExitCode, options);
2027
+ }
2028
+ async function restoreSessionDb(options, configPath, env) {
2029
+ const restored = await runCommand(
2030
+ "litestream",
2031
+ ["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
2032
+ { env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
2033
+ );
2034
+ for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
2035
+ if (restored.timedOut || restored.code === 124 || restored.code === 137) {
2036
+ restoreGiveUp(
2037
+ options,
2038
+ "restore_deadline_exceeded",
2039
+ `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`,
2040
+ restored.code ?? 124
2041
+ );
2042
+ return;
2043
+ }
2044
+ if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
2045
+ options.log(`litestream restore could not run (${commandError(restored)})`, "error");
2046
+ restoreGiveUp(
2047
+ options,
2048
+ "restore_tool_unusable",
2049
+ `restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
2050
+ restored.code
2051
+ );
2052
+ return;
2053
+ }
2054
+ const classified = await runSynchroniser(
2055
+ [
2056
+ "session-db-classify",
2057
+ String(restored.code ?? 1),
2058
+ "1",
2059
+ "--on-unusable-replica=leave",
2060
+ "--fresh-db-fallback"
2061
+ ],
2062
+ { timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
2063
+ );
2064
+ logSynchroniserDiagnostics(classified, options);
2065
+ const classifyCode = classified.code;
2066
+ switch (classifyCode) {
2067
+ case 0:
2068
+ return;
2069
+ case 31:
2070
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2071
+ options.log(
2072
+ "SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
2073
+ "warn"
2074
+ );
2075
+ return;
2076
+ case 32:
2077
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2078
+ discardSessionDbDebris(options);
2079
+ markNoReplicate(
2080
+ options,
2081
+ "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"
2082
+ );
2083
+ return;
2084
+ case 30:
2085
+ restoreGiveUp(
2086
+ options,
2087
+ "classification_fatal",
2088
+ "session-db-classify returned fatal (30); see the FATAL message above",
2089
+ restored.code,
2090
+ "restore_misconfigured"
2091
+ );
2092
+ return;
2093
+ default:
2094
+ restoreGiveUp(
2095
+ options,
2096
+ "classification_unrecognised",
2097
+ `session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
2098
+ restored.code
2099
+ );
2100
+ }
2101
+ }
2102
+ async function verifySessionDb(options, configPath, env) {
2103
+ if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
2104
+ const result = await runSynchroniser(["session-db-verify", configPath], {
2105
+ timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
2106
+ env: {
2107
+ ...env,
2108
+ // The synchroniser reads this value in SECONDS. Keep this at 120, not
2109
+ // 120_000, so the walkback gives up before the outer process bound.
2110
+ EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
2111
+ SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
2112
+ )
2113
+ }
2114
+ });
2115
+ logSynchroniserDiagnostics(result, options);
2116
+ if (result.timedOut || result.code === 124 || result.code === 137) {
2117
+ options.log(
2118
+ `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`,
2119
+ "warn"
2120
+ );
2121
+ return false;
2122
+ }
2123
+ if (result.code === 34) {
2124
+ reportRecord(
2125
+ "verify",
2126
+ "session_db_boot_refused",
2127
+ result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
2128
+ null,
2129
+ options
2130
+ );
2131
+ return true;
2132
+ }
2133
+ if (result.code === 33) {
2134
+ options.log(
2135
+ "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
2136
+ "warn"
2137
+ );
2138
+ return false;
2139
+ }
2140
+ if (result.code !== 0) {
2141
+ options.log(
2142
+ `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`,
2143
+ "warn"
2144
+ );
2145
+ }
2146
+ return false;
2147
+ }
2148
+ options.log(
2149
+ "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
2150
+ "debug"
2151
+ );
2152
+ return false;
2153
+ }
2154
+ function fileExists(path) {
2155
+ try {
2156
+ statSync2(path);
2157
+ return true;
2158
+ } catch (error2) {
2159
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
2160
+ return true;
2161
+ }
2162
+ }
2163
+ async function restoreAndVerifySessionDb(options) {
2164
+ const env = options.env ?? process.env;
2165
+ clearMarker(options);
2166
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2167
+ const synchroniserEnv = await runSynchroniser(["env"], {
2168
+ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
2169
+ env
2170
+ });
2171
+ logSynchroniserDiagnostics(synchroniserEnv, options);
2172
+ if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
2173
+ options.log(
2174
+ `runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
2175
+ "error"
2176
+ );
2177
+ markNoReplicate(
2178
+ options,
2179
+ "could not resolve the runner-synchroniser configuration (see the ERROR above)"
2180
+ );
2181
+ reportRecord(
2182
+ "restore",
2183
+ "restore_misconfigured",
2184
+ "synchroniser_config_unresolved",
2185
+ null,
2186
+ options
2187
+ );
2188
+ return { verifyFatal: false };
2189
+ }
2190
+ const values = parseSynchroniserEnv(synchroniserEnv.stdout);
2191
+ if (!values) {
2192
+ markNoReplicate(
2193
+ options,
2194
+ "the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
2195
+ );
2196
+ reportRecord(
2197
+ "restore",
2198
+ "restore_misconfigured",
2199
+ "synchroniser_config_unevaluable",
2200
+ null,
2201
+ options
2202
+ );
2203
+ return { verifyFatal: false };
2204
+ }
2205
+ const synchroniserDbPath = values.OPENCODE_DB_PATH;
2206
+ if (!synchroniserDbPath) {
2207
+ markNoReplicate(
2208
+ options,
2209
+ "run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
2210
+ );
2211
+ reportRecord(
2212
+ "restore",
2213
+ "restore_misconfigured",
2214
+ "synchroniser_config_incomplete",
2215
+ null,
2216
+ options
2217
+ );
2218
+ return { verifyFatal: false };
2219
+ }
2220
+ if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
2221
+ options.log(
2222
+ `runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
2223
+ "warn"
2224
+ );
2225
+ }
2226
+ if (!values.PERSISTENCE_BUCKET) {
2227
+ options.log(
2228
+ "SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
2229
+ "warn"
2230
+ );
2231
+ return { verifyFatal: false };
2232
+ }
2233
+ const configPath = await ensureLitestreamConfig(options, env);
2234
+ if (!configPath) return { verifyFatal: false };
2235
+ await restoreSessionDb(options, configPath, env);
2236
+ if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
2237
+ return { verifyFatal: false };
2238
+ }
2239
+ return { verifyFatal: await verifySessionDb(options, configPath, env) };
2240
+ }
2241
+
2242
+ // src/lib/opencode/session-db-provenance.ts
2243
+ import { createRequire } from "module";
2244
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
2245
+ import { dirname as dirname3, join as join3 } from "path";
2246
+ var require2 = createRequire(import.meta.url);
2247
+ function readSessionDbMigrationIds(dbPath) {
2248
+ let db;
2249
+ try {
2250
+ const { DatabaseSync } = require2("node:sqlite");
2251
+ db = new DatabaseSync(dbPath, { readOnly: true });
2252
+ const columns = db.prepare("PRAGMA table_info(migration)").all();
2253
+ const hasExpectedShape = columns.length === 2 && columns.some(
2254
+ (column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
2255
+ ) && columns.some(
2256
+ (column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
2257
+ );
2258
+ if (!hasExpectedShape) {
2259
+ console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
2260
+ return null;
2261
+ }
2262
+ const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
2263
+ if (rows.some((row) => typeof row.id !== "string")) return null;
2264
+ return rows.map((row) => row.id);
2265
+ } catch (error2) {
2266
+ console.warn(
2267
+ `[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2268
+ );
2269
+ return null;
2270
+ } finally {
2271
+ try {
2272
+ db?.close();
2273
+ } catch (error2) {
2274
+ console.warn(
2275
+ `[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2276
+ );
2277
+ }
2278
+ }
2279
+ }
2280
+ function sessionDbProvenanceStatePath(homeDir, env) {
2281
+ const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
2282
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
2283
+ }
2284
+ function loadSessionDbProvenanceState(path) {
2285
+ let value;
2286
+ try {
2287
+ value = JSON.parse(readFileSync3(path, "utf8"));
2288
+ } catch (error2) {
2289
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
2290
+ console.error(
2291
+ `[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2292
+ );
2293
+ return {};
2294
+ }
2295
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
2296
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2297
+ return {};
2298
+ }
2299
+ const state = {};
2300
+ for (const [dbPath, record] of Object.entries(value)) {
2301
+ if (!isSessionDbProvenanceRecord(record)) {
2302
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2303
+ return {};
2304
+ }
2305
+ state[dbPath] = record;
2306
+ }
2307
+ return state;
2308
+ }
2309
+ function saveSessionDbProvenanceState(path, state) {
2310
+ try {
2311
+ mkdirSync2(dirname3(path), { recursive: true });
2312
+ writeFileSync2(path, `${JSON.stringify(state, null, 2)}
2313
+ `, "utf8");
2314
+ } catch (error2) {
2315
+ console.error(
2316
+ `[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2317
+ );
2318
+ }
2319
+ }
2320
+ function evaluateSessionDbProvenance(input) {
2321
+ const { currentVersion, currentIds, previous } = input;
2322
+ if (!previous) return { anomaly: false, reason: null };
2323
+ const current = new Set(currentIds);
2324
+ const prior = new Set(previous.migrationIds);
2325
+ for (const id of prior) {
2326
+ if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
2327
+ }
2328
+ if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
2329
+ return { anomaly: true, reason: "foreign-version-migrations" };
2330
+ }
2331
+ return { anomaly: false, reason: null };
2332
+ }
2333
+ function checkSessionDbProvenance(input) {
2334
+ const { dbPath, currentVersion, homeDir, env } = input;
2335
+ const path = sessionDbProvenanceStatePath(homeDir, env);
2336
+ const state = loadSessionDbProvenanceState(path);
2337
+ const previous = state[dbPath];
2338
+ const currentIds = readSessionDbMigrationIds(dbPath);
2339
+ if (currentIds === null) {
2340
+ return {
2341
+ anomaly: false,
2342
+ reason: null,
2343
+ recordedVersion: previous?.opencodeVersion ?? null,
2344
+ migrationDelta: null
2345
+ };
2346
+ }
2347
+ const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
2348
+ const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
2349
+ state[dbPath] = {
2350
+ opencodeVersion: currentVersion,
2351
+ migrationIds: currentIds,
2352
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2353
+ };
2354
+ saveSessionDbProvenanceState(path, state);
2355
+ return {
2356
+ ...decision,
2357
+ recordedVersion: previous?.opencodeVersion ?? null,
2358
+ migrationDelta
2359
+ };
2360
+ }
2361
+ function isSessionDbProvenanceRecord(value) {
2362
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
2363
+ const record = value;
2364
+ return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
2365
+ }
2366
+
1669
2367
  // src/lib/opencode/opencode-version-gate.ts
1670
2368
  var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
1671
2369
  function isQueueValidatedVersion(version2) {
@@ -1680,7 +2378,63 @@ function buildOpenCodeVersionWarning(version2) {
1680
2378
  }
1681
2379
 
1682
2380
  // src/lib/opencode/process.ts
1683
- import { execSync, spawn } from "child_process";
2381
+ import { execSync, spawn as spawn3 } from "child_process";
2382
+
2383
+ // src/lib/process-stop.ts
2384
+ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
2385
+ if (!child.pid) {
2386
+ return { outcome: "not-running", code: child.exitCode, signal: child.signalCode };
2387
+ }
2388
+ if (child.exitCode !== null || child.signalCode !== null) {
2389
+ return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
2390
+ }
2391
+ return new Promise((resolve4, reject) => {
2392
+ let forced = false;
2393
+ let settled = false;
2394
+ const timer = setTimeout(() => {
2395
+ forced = true;
2396
+ try {
2397
+ sendKill();
2398
+ } catch (error2) {
2399
+ if (error2.code === "ESRCH") {
2400
+ finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
2401
+ } else {
2402
+ fail(error2);
2403
+ }
2404
+ }
2405
+ }, timeoutMs);
2406
+ const finish = (result) => {
2407
+ if (settled) return;
2408
+ settled = true;
2409
+ clearTimeout(timer);
2410
+ child.removeListener("exit", onExit);
2411
+ resolve4(result);
2412
+ };
2413
+ const fail = (error2) => {
2414
+ if (settled) return;
2415
+ settled = true;
2416
+ clearTimeout(timer);
2417
+ child.removeListener("exit", onExit);
2418
+ reject(error2);
2419
+ };
2420
+ const onExit = (code, signal) => {
2421
+ finish({ outcome: forced ? "killed" : "exited", code, signal });
2422
+ };
2423
+ child.once("exit", onExit);
2424
+ try {
2425
+ sendTerm();
2426
+ } catch (error2) {
2427
+ if (error2.code === "ESRCH") {
2428
+ finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
2429
+ } else {
2430
+ fail(error2);
2431
+ }
2432
+ return;
2433
+ }
2434
+ });
2435
+ }
2436
+
2437
+ // src/lib/opencode/process.ts
1684
2438
  var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
1685
2439
  function getProcessCwd(pid) {
1686
2440
  const platform = process.platform;
@@ -1836,39 +2590,45 @@ async function findHealthyOpenCodeInstances() {
1836
2590
  }
1837
2591
  return healthy;
1838
2592
  }
1839
- async function startOpenCode(port) {
2593
+ async function startOpenCode(port, options = {}) {
1840
2594
  let command = "opencode";
1841
- let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1"];
2595
+ const printLogs = options.inheritStdio ? ["--print-logs"] : [];
2596
+ let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
1842
2597
  try {
1843
2598
  execSync("which opencode", { stdio: "ignore" });
1844
2599
  } catch {
1845
2600
  command = "npx";
1846
- args = ["opencode", "serve", "--port", port.toString(), "--hostname", "127.0.0.1"];
1847
- }
1848
- const child = spawn(command, args, {
2601
+ args = [
2602
+ "opencode",
2603
+ "serve",
2604
+ "--port",
2605
+ port.toString(),
2606
+ "--hostname",
2607
+ "127.0.0.1",
2608
+ ...printLogs
2609
+ ];
2610
+ }
2611
+ const child = spawn3(command, args, {
1849
2612
  detached: true,
1850
- stdio: "ignore",
2613
+ stdio: options.inheritStdio ? "inherit" : "ignore",
1851
2614
  cwd: process.cwd()
1852
2615
  });
1853
2616
  return child;
1854
2617
  }
1855
- function stopOpenCode(opencodeProcess) {
1856
- if (!opencodeProcess || !opencodeProcess.pid) {
1857
- return;
1858
- }
1859
- try {
2618
+ function stopOpenCodeAndWait(opencodeProcess, timeoutMs) {
2619
+ const sendSignal = (signal) => {
1860
2620
  if (process.platform === "win32") {
1861
- opencodeProcess.kill("SIGTERM");
2621
+ opencodeProcess.kill(signal);
1862
2622
  } else {
1863
- process.kill(-opencodeProcess.pid, "SIGTERM");
1864
- }
1865
- } catch (err) {
1866
- if (err.code !== "ESRCH") {
1867
- console.warn(
1868
- `stopOpenCode: kill failed: ${err instanceof Error ? err.message : String(err)}`
1869
- );
2623
+ process.kill(-opencodeProcess.pid, signal);
1870
2624
  }
1871
- }
2625
+ };
2626
+ return stopProcessAndWait(
2627
+ opencodeProcess,
2628
+ timeoutMs,
2629
+ () => sendSignal("SIGTERM"),
2630
+ () => sendSignal("SIGKILL")
2631
+ );
1872
2632
  }
1873
2633
 
1874
2634
  // src/lib/opencode/install.ts
@@ -2155,6 +2915,7 @@ async function createOpenCodeSession(port, directory) {
2155
2915
  return data.id;
2156
2916
  }
2157
2917
  async function getModelAttachmentCapability(port, model) {
2918
+ const { model: baseModel } = splitModelVariant(model);
2158
2919
  try {
2159
2920
  const res = await timedFetch(`${opencodeBase(port)}/config/providers`);
2160
2921
  if (!res.ok) {
@@ -2171,9 +2932,9 @@ async function getModelAttachmentCapability(port, model) {
2171
2932
  );
2172
2933
  return null;
2173
2934
  }
2174
- const slash = model ? model.indexOf("/") : -1;
2175
- const providerId = slash > 0 ? model.slice(0, slash) : void 0;
2176
- let modelId = slash > 0 ? model.slice(slash + 1) : void 0;
2935
+ const slash = baseModel ? baseModel.indexOf("/") : -1;
2936
+ const providerId = slash > 0 ? baseModel.slice(0, slash) : void 0;
2937
+ let modelId = slash > 0 ? baseModel.slice(slash + 1) : void 0;
2177
2938
  const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
2178
2939
  let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
2179
2940
  if (!provider && !providerId) {
@@ -2253,6 +3014,29 @@ async function buildFileParts(attachments, capable) {
2253
3014
  }
2254
3015
  return { parts, outcomes, capabilityUnknown };
2255
3016
  }
3017
+ function splitModelVariant(raw) {
3018
+ const value = raw?.trim();
3019
+ if (!value) return {};
3020
+ const hashIndex = value.indexOf("#");
3021
+ if (hashIndex === -1) return { model: value };
3022
+ const model = value.slice(0, hashIndex).trim() || void 0;
3023
+ const variant = value.slice(hashIndex + 1).trim() || void 0;
3024
+ return { model, variant };
3025
+ }
3026
+ function applyModelOptions(body, options) {
3027
+ if (options?.agent) body.agent = options.agent;
3028
+ const { model, variant } = splitModelVariant(options?.model);
3029
+ if (model) {
3030
+ const slashIndex = model.indexOf("/");
3031
+ if (slashIndex !== -1) {
3032
+ body.model = {
3033
+ providerID: model.substring(0, slashIndex),
3034
+ modelID: model.substring(slashIndex + 1)
3035
+ };
3036
+ }
3037
+ }
3038
+ if (variant) body.variant = variant;
3039
+ }
2256
3040
  function messageText(m) {
2257
3041
  if (!m || !Array.isArray(m.parts)) return "";
2258
3042
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
@@ -2277,18 +3061,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2277
3061
  const body = {
2278
3062
  parts
2279
3063
  };
2280
- if (options?.agent) {
2281
- body.agent = options.agent;
2282
- }
2283
- if (options?.model) {
2284
- const slashIndex = options.model.indexOf("/");
2285
- if (slashIndex !== -1) {
2286
- body.model = {
2287
- providerID: options.model.substring(0, slashIndex),
2288
- modelID: options.model.substring(slashIndex + 1)
2289
- };
2290
- }
2291
- }
3064
+ applyModelOptions(body, options);
2292
3065
  const res = await timedFetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
2293
3066
  method: "POST",
2294
3067
  headers: { "Content-Type": "application/json" },
@@ -2296,7 +3069,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2296
3069
  });
2297
3070
  if (res.status < 200 || res.status >= 300) {
2298
3071
  const text = await res.text().catch(() => "");
2299
- throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
3072
+ const { variant } = splitModelVariant(options?.model);
3073
+ throw new Error(
3074
+ `OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}${variant ? ` (variant: ${variant})` : ""}`
3075
+ );
2300
3076
  }
2301
3077
  const READ_BACK_ATTEMPTS = 5;
2302
3078
  const READ_BACK_DELAY_MS = 150;
@@ -2320,7 +3096,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2320
3096
  }
2321
3097
  }
2322
3098
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2323
- await new Promise((resolve3) => setTimeout(resolve3, READ_BACK_DELAY_MS));
3099
+ await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
2324
3100
  }
2325
3101
  }
2326
3102
  return null;
@@ -2451,7 +3227,7 @@ function isPreamblePinnedRunning(messages, userMessageId) {
2451
3227
  return completedOf(reply) != null && finishOf(reply) === "tool-calls";
2452
3228
  }
2453
3229
  function isB2AbandonmentConfirmed(params) {
2454
- return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
3230
+ return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false && params.rootOngoing === false;
2455
3231
  }
2456
3232
  function isAmbiguousTerminalFinish(m) {
2457
3233
  if (completedOf(m) == null) return false;
@@ -2464,7 +3240,7 @@ function isAmbiguousFinishPinnedRunning(messages, userMessageId) {
2464
3240
  return isAmbiguousTerminalFinish(reply);
2465
3241
  }
2466
3242
  function isAmbiguousFinishResolved(params) {
2467
- return params.sessionOngoing === false || params.pinnedForMs >= params.maxPinnedMs;
3243
+ return params.sessionOngoing === false || params.sessionOngoing !== true && params.pinnedForMs >= params.maxPinnedMs;
2468
3244
  }
2469
3245
  function messageError(messages, userMessageId) {
2470
3246
  const reply = findLastAssistantReplyFor(messages, userMessageId);
@@ -2673,13 +3449,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
2673
3449
  }
2674
3450
 
2675
3451
  // src/lib/opencode/session-db-size.ts
2676
- import { statSync as statSync2 } from "fs";
2677
- import { join as join3 } from "path";
3452
+ import { statSync as statSync3 } from "fs";
3453
+ import { join as join4 } from "path";
2678
3454
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
2679
3455
  function statSessionDbBytes(homeDir) {
2680
- const dbPath = join3(homeDir, ".local", "share", "opencode", "opencode.db");
3456
+ const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
2681
3457
  try {
2682
- return statSync2(dbPath).size;
3458
+ return statSync3(dbPath).size;
2683
3459
  } catch (err) {
2684
3460
  const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
2685
3461
  if (!isMissingFile) {
@@ -2705,11 +3481,11 @@ function buildSessionStoreSizeWarning(input) {
2705
3481
  }
2706
3482
 
2707
3483
  // src/lib/opencode/session-db-reclaim.ts
2708
- import { statSync as statSync3, statfsSync } from "fs";
2709
- import { dirname as dirname2 } from "path";
3484
+ import { statSync as statSync4, statfsSync } from "fs";
3485
+ import { dirname as dirname4 } from "path";
2710
3486
  function insufficientSpaceReason(dbPath, requiredBytes) {
2711
3487
  try {
2712
- const fsStats = statfsSync(dirname2(dbPath));
3488
+ const fsStats = statfsSync(dirname4(dbPath));
2713
3489
  const availableBytes = fsStats.bavail * fsStats.bsize;
2714
3490
  if (availableBytes < requiredBytes) {
2715
3491
  return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
@@ -2778,7 +3554,7 @@ async function reclaimSessionDbSpace(input) {
2778
3554
  );
2779
3555
  return { ok: false, skipped: "full-vacuum-blocked" };
2780
3556
  }
2781
- const fileBytesForGuard = statSync3(dbPath).size;
3557
+ const fileBytesForGuard = statSync4(dbPath).size;
2782
3558
  const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
2783
3559
  if (skipReason !== null) {
2784
3560
  console.warn(
@@ -2906,12 +3682,12 @@ var StreamForwarder = class {
2906
3682
  let endBody;
2907
3683
  if (has_body) {
2908
3684
  const chunks = [];
2909
- bodyPromise = new Promise((resolve3) => {
3685
+ bodyPromise = new Promise((resolve4) => {
2910
3686
  pushBody = (buf) => {
2911
3687
  chunks.push(buf);
2912
3688
  };
2913
3689
  endBody = () => {
2914
- resolve3(Buffer.concat(chunks));
3690
+ resolve4(Buffer.concat(chunks));
2915
3691
  };
2916
3692
  });
2917
3693
  }
@@ -3040,7 +3816,7 @@ function connectTunnel(options) {
3040
3816
  } = options;
3041
3817
  const tunnelUrl = getTunnelUrlConfig();
3042
3818
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
3043
- return new Promise((resolve3, reject) => {
3819
+ return new Promise((resolve4, reject) => {
3044
3820
  const ws = new WebSocket2(url, {
3045
3821
  headers: {
3046
3822
  Authorization: authHeader
@@ -3091,8 +3867,8 @@ function connectTunnel(options) {
3091
3867
  try {
3092
3868
  message = JSON.parse(data.toString());
3093
3869
  } catch (error2) {
3094
- const errorMessage = error2 instanceof Error ? error2.message : "Unknown error";
3095
- onError?.(`Failed to handle message: ${errorMessage}`);
3870
+ const errorMessage2 = error2 instanceof Error ? error2.message : "Unknown error";
3871
+ onError?.(`Failed to handle message: ${errorMessage2}`);
3096
3872
  return;
3097
3873
  }
3098
3874
  if (isStreamFrame(message)) {
@@ -3104,7 +3880,7 @@ function connectTunnel(options) {
3104
3880
  clearTimeout(connectionTimeout);
3105
3881
  const connectedAgentId = message.agent_id ?? agentId;
3106
3882
  onConnected?.(connectedAgentId);
3107
- resolve3({
3883
+ resolve4({
3108
3884
  ws,
3109
3885
  close: () => ws.close(1e3, "CLI shutdown")
3110
3886
  });
@@ -3235,10 +4011,10 @@ var RunnerConnection = class {
3235
4011
  };
3236
4012
 
3237
4013
  // src/lib/tunnel/ready-marker.ts
3238
- import { writeFileSync } from "fs";
4014
+ import { writeFileSync as writeFileSync3 } from "fs";
3239
4015
  function writeTunnelReadyMarker(path, agentId) {
3240
4016
  try {
3241
- writeFileSync(path, `${agentId}
4017
+ writeFileSync3(path, `${agentId}
3242
4018
  `);
3243
4019
  return { ok: true };
3244
4020
  } catch (error2) {
@@ -3246,10 +4022,52 @@ function writeTunnelReadyMarker(path, agentId) {
3246
4022
  }
3247
4023
  }
3248
4024
 
4025
+ // src/lib/replication.ts
4026
+ import { spawn as spawn4 } from "child_process";
4027
+ function startSessionDbReplication(configPath) {
4028
+ return spawn4("litestream", ["replicate", "-config", configPath], {
4029
+ stdio: "inherit"
4030
+ });
4031
+ }
4032
+ async function stopSessionDbReplication(child, timeoutMs) {
4033
+ return stopProcessAndWait(
4034
+ child,
4035
+ timeoutMs,
4036
+ () => child.kill("SIGTERM"),
4037
+ () => child.kill("SIGKILL")
4038
+ );
4039
+ }
4040
+
4041
+ // src/lib/process-liveness.ts
4042
+ import { readFileSync as readFileSync4 } from "fs";
4043
+ function isProcessAlive(pid) {
4044
+ try {
4045
+ process.kill(pid, 0);
4046
+ } catch (error2) {
4047
+ const code = error2.code;
4048
+ if (code === "ESRCH") return false;
4049
+ if (code === "EPERM") return true;
4050
+ console.error(
4051
+ `[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
4052
+ );
4053
+ return false;
4054
+ }
4055
+ if (process.platform !== "linux") return true;
4056
+ try {
4057
+ const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
4058
+ return !/^State:\s+Z(?:\s|$)/m.test(status2);
4059
+ } catch (error2) {
4060
+ console.error(
4061
+ `[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
4062
+ );
4063
+ return true;
4064
+ }
4065
+ }
4066
+
3249
4067
  // src/lib/openai-usage.ts
3250
- import { readFileSync as readFileSync3 } from "fs";
3251
- import { homedir as homedir2 } from "os";
3252
- import { join as join4 } from "path";
4068
+ import { readFileSync as readFileSync5 } from "fs";
4069
+ import { homedir as homedir3 } from "os";
4070
+ import { join as join5 } from "path";
3253
4071
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3254
4072
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3255
4073
  var OpenAiUsageError = class extends Error {
@@ -3263,7 +4081,7 @@ function isLocalCredentialProblem2(err) {
3263
4081
  }
3264
4082
  function readOpenCodeChatGptCredentials() {
3265
4083
  try {
3266
- const raw = readFileSync3(join4(homedir2(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4084
+ const raw = readFileSync5(join5(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3267
4085
  let parsed;
3268
4086
  try {
3269
4087
  parsed = JSON.parse(raw);
@@ -3636,15 +4454,15 @@ function createResourceUsageCollector(homeDir) {
3636
4454
  }
3637
4455
 
3638
4456
  // src/lib/channels/driver.ts
3639
- import { homedir as homedir3 } from "os";
4457
+ import { homedir as homedir4 } from "os";
3640
4458
 
3641
4459
  // src/lib/runner-file-sync.ts
3642
- import { join as join6 } from "path";
4460
+ import { join as join7 } from "path";
3643
4461
 
3644
4462
  // src/lib/file-push.ts
3645
4463
  import { randomUUID } from "crypto";
3646
4464
  import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
3647
- import { basename, dirname as dirname3, isAbsolute, join as join5, relative, resolve as resolve2, sep } from "path";
4465
+ import { basename, dirname as dirname5, isAbsolute, join as join6, relative, resolve as resolve2, sep } from "path";
3648
4466
  var FILE_MODE = 384;
3649
4467
  var DIRECTORY_MODE = 448;
3650
4468
  async function writePushedFile(request) {
@@ -3675,9 +4493,9 @@ async function writePushedFile(request) {
3675
4493
  }
3676
4494
  try {
3677
4495
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
3678
- dirname3(candidate)
4496
+ dirname5(candidate)
3679
4497
  );
3680
- const realTarget = join5(existingAncestor, ...missingSegments, basename(candidate));
4498
+ const realTarget = join6(existingAncestor, ...missingSegments, basename(candidate));
3681
4499
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
3682
4500
  if (allowedDirectory === null) {
3683
4501
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -3687,8 +4505,8 @@ async function writePushedFile(request) {
3687
4505
  }
3688
4506
  if (missingSegments.length > 0) {
3689
4507
  await createMissingDirectories(existingAncestor, missingSegments);
3690
- const realParent = await realpath(dirname3(realTarget));
3691
- if (realParent !== dirname3(realTarget) || !contains(allowedDirectory, realTarget)) {
4508
+ const realParent = await realpath(dirname5(realTarget));
4509
+ if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
3692
4510
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
3693
4511
  path: realTarget,
3694
4512
  bytes,
@@ -3713,7 +4531,7 @@ function expandAndValidate(requestedPath, homeDir) {
3713
4531
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
3714
4532
  return null;
3715
4533
  }
3716
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join5(homeDir, requestedPath.slice(2)) : requestedPath;
4534
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3717
4535
  if (expanded.split(/[/\\]/).includes("..")) {
3718
4536
  return null;
3719
4537
  }
@@ -3731,7 +4549,7 @@ async function resolveNearestExistingAncestor(directory) {
3731
4549
  try {
3732
4550
  return { existingAncestor: await realpath(current), missingSegments };
3733
4551
  } catch (err) {
3734
- const parent = dirname3(current);
4552
+ const parent = dirname5(current);
3735
4553
  if (err.code !== "ENOENT" || parent === current) {
3736
4554
  throw err;
3737
4555
  }
@@ -3786,13 +4604,13 @@ function contains(realDirectory, realTarget) {
3786
4604
  async function createMissingDirectories(existingAncestor, missingSegments) {
3787
4605
  let current = existingAncestor;
3788
4606
  for (const segment of missingSegments) {
3789
- current = join5(current, segment);
4607
+ current = join6(current, segment);
3790
4608
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
3791
4609
  await chmod(current, DIRECTORY_MODE);
3792
4610
  }
3793
4611
  }
3794
4612
  async function writeAtomically(realTarget, content) {
3795
- const temporaryPath = join5(dirname3(realTarget), `.evident-push-${randomUUID()}.tmp`);
4613
+ const temporaryPath = join6(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
3796
4614
  let handle;
3797
4615
  try {
3798
4616
  handle = await open2(temporaryPath, "wx", FILE_MODE);
@@ -3922,12 +4740,12 @@ var NOT_APPLIED = {
3922
4740
  opencodeAuthApplied: false
3923
4741
  };
3924
4742
  function isClaudeCredentialPath(requestedPath, homeDir) {
3925
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3926
- return expanded === join6(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
4743
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
4744
+ return expanded === join7(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
3927
4745
  }
3928
4746
  function isOpenCodeAuthPath(requestedPath, homeDir) {
3929
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3930
- return expanded === join6(homeDir, ...OPENCODE_AUTH_SEGMENTS);
4747
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
4748
+ return expanded === join7(homeDir, ...OPENCODE_AUTH_SEGMENTS);
3931
4749
  }
3932
4750
  async function applyOne(options, file) {
3933
4751
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -4473,7 +5291,7 @@ var ChannelDriver = class _ChannelDriver {
4473
5291
  this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
4474
5292
  this.now = config.now ?? (() => Date.now());
4475
5293
  this.fileSyncDirectories = config.fileSyncDirectories ?? [];
4476
- this.homeDir = config.homeDir ?? homedir3();
5294
+ this.homeDir = config.homeDir ?? homedir4();
4477
5295
  this.maxActiveSessions = config.maxActiveSessions;
4478
5296
  this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
4479
5297
  this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
@@ -4824,7 +5642,7 @@ var ChannelDriver = class _ChannelDriver {
4824
5642
  this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
4825
5643
  break;
4826
5644
  }
4827
- const errorMessage = err instanceof Error ? err.message : String(err);
5645
+ const errorMessage2 = err instanceof Error ? err.message : String(err);
4828
5646
  this.sessions.delete(conv.id);
4829
5647
  this.supersede(conv.id, sessionId);
4830
5648
  this.log({
@@ -4833,7 +5651,7 @@ var ChannelDriver = class _ChannelDriver {
4833
5651
  conversation_id: conv.id,
4834
5652
  message_id: message.id
4835
5653
  });
4836
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
5654
+ await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
4837
5655
  this.log({
4838
5656
  level: "warn",
4839
5657
  message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
@@ -4844,7 +5662,7 @@ var ChannelDriver = class _ChannelDriver {
4844
5662
  });
4845
5663
  this.log({
4846
5664
  level: "error",
4847
- message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
5665
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage2}`,
4848
5666
  conversation_id: conv.id,
4849
5667
  message_id: message.id
4850
5668
  });
@@ -4865,14 +5683,14 @@ var ChannelDriver = class _ChannelDriver {
4865
5683
  this.unconfirmedDispatchFailures.delete(message.id);
4866
5684
  this.sessions.delete(conv.id);
4867
5685
  this.supersede(conv.id, sessionId);
4868
- const errorMessage = `OpenCode accepted this message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
5686
+ const errorMessage2 = `OpenCode accepted this message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
4869
5687
  this.log({
4870
5688
  level: "error",
4871
- message: errorMessage,
5689
+ message: errorMessage2,
4872
5690
  conversation_id: conv.id,
4873
5691
  message_id: message.id
4874
5692
  });
4875
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
5693
+ await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
4876
5694
  this.log({
4877
5695
  level: "warn",
4878
5696
  message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
@@ -5740,6 +6558,7 @@ var ChannelDriver = class _ChannelDriver {
5740
6558
  deliveryDeadlineAnchored: false,
5741
6559
  b2PinnedSinceMs: 0,
5742
6560
  b2LastDescendantCheckMs: 0,
6561
+ b2RootOngoingHeldLogged: false,
5743
6562
  b2AbandonedSignalled: false,
5744
6563
  ambiguousPinnedSinceMs: 0,
5745
6564
  ambiguousResolved: false
@@ -5834,6 +6653,7 @@ var ChannelDriver = class _ChannelDriver {
5834
6653
  deliveryDeadlineAnchored: false,
5835
6654
  b2PinnedSinceMs: 0,
5836
6655
  b2LastDescendantCheckMs: 0,
6656
+ b2RootOngoingHeldLogged: false,
5837
6657
  b2AbandonedSignalled: false,
5838
6658
  ambiguousPinnedSinceMs: 0,
5839
6659
  ambiguousResolved: false
@@ -6187,6 +7007,7 @@ var ChannelDriver = class _ChannelDriver {
6187
7007
  if (snapshotReadable) {
6188
7008
  inFlight.b2PinnedSinceMs = 0;
6189
7009
  inFlight.b2LastDescendantCheckMs = 0;
7010
+ inFlight.b2RootOngoingHeldLogged = false;
6190
7011
  inFlight.b2AbandonedSignalled = false;
6191
7012
  }
6192
7013
  } else {
@@ -6198,11 +7019,15 @@ var ChannelDriver = class _ChannelDriver {
6198
7019
  const pinnedForMs = this.now() - inFlight.b2PinnedSinceMs;
6199
7020
  if (pinnedForMs >= B2_ABANDONMENT_MIN_PINNED_MS && this.now() - inFlight.b2LastDescendantCheckMs >= B2_ABANDONMENT_RECHECK_MS) {
6200
7021
  inFlight.b2LastDescendantCheckMs = this.now();
6201
- const descendantOngoing = await this.isAnyDescendantSessionOngoing(sessionId);
7022
+ const [descendantOngoing, rootOngoing] = await Promise.all([
7023
+ this.isAnyDescendantSessionOngoing(sessionId),
7024
+ isSessionOngoing(this.port, sessionId)
7025
+ ]);
6202
7026
  if (isB2AbandonmentConfirmed({
6203
7027
  pinnedForMs,
6204
7028
  minPinnedMs: B2_ABANDONMENT_MIN_PINNED_MS,
6205
- descendantOngoing
7029
+ descendantOngoing,
7030
+ rootOngoing
6206
7031
  })) {
6207
7032
  inFlight.b2AbandonedSignalled = true;
6208
7033
  this.log({
@@ -6211,12 +7036,26 @@ var ChannelDriver = class _ChannelDriver {
6211
7036
  conversation_id: conv.id,
6212
7037
  message_id: id
6213
7038
  });
7039
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
6214
7040
  void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
6215
- watched_for_ms: pinnedForMs
7041
+ watched_for_ms: pinnedForMs,
7042
+ finish: reply?.info?.finish ?? reply?.finish,
7043
+ ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
7044
+ ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
7045
+ opencode_message_id: inFlight.opencodeMessageId
6216
7046
  });
6217
7047
  await this.settleMessageDone(sessionId, watcher, inFlight, messages);
6218
7048
  return;
6219
7049
  }
7050
+ if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
7051
+ inFlight.b2RootOngoingHeldLogged = true;
7052
+ this.log({
7053
+ level: "warn",
7054
+ message: `Message ${id.slice(0, 8)} b2-pinned for ${Math.round(pinnedForMs / 1e3)}s with root_ongoing=${rootOngoing} and descendant_ongoing=${descendantOngoing} \u2014 holding until OpenCode confirms the root is idle`,
7055
+ conversation_id: conv.id,
7056
+ message_id: id
7057
+ });
7058
+ }
6220
7059
  }
6221
7060
  }
6222
7061
  const ambiguousPinnedNow = activelyRunning && isAmbiguousFinishPinnedRunning(messages, inFlight.opencodeMessageId);
@@ -6815,14 +7654,14 @@ var ChannelDriver = class _ChannelDriver {
6815
7654
  this.unconfirmedDispatchFailures.delete(row.id);
6816
7655
  this.sessions.delete(readoptConv.id);
6817
7656
  this.supersede(readoptConv.id, sessionId);
6818
- const errorMessage = `OpenCode accepted this re-dispatched message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
7657
+ const errorMessage2 = `OpenCode accepted this re-dispatched message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
6819
7658
  this.log({
6820
7659
  level: "error",
6821
- message: errorMessage,
7660
+ message: errorMessage2,
6822
7661
  conversation_id: row.conversation_id,
6823
7662
  message_id: row.id
6824
7663
  });
6825
- await this.markFailed(row.conversation_id, row.id, null, errorMessage).catch((markErr) => {
7664
+ await this.markFailed(row.conversation_id, row.id, null, errorMessage2).catch((markErr) => {
6826
7665
  this.log({
6827
7666
  level: "warn",
6828
7667
  message: `markFailed PATCH for message ${row.id.slice(0, 8)} (conversation ${row.conversation_id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
@@ -7873,7 +8712,7 @@ async function ensureOpenCodeRunning(ctx) {
7873
8712
  }
7874
8713
  if (!ctx.interactive) {
7875
8714
  ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
7876
- const proc = await startOpenCode(ctx.port);
8715
+ const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
7877
8716
  const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
7878
8717
  if (!health.healthy) {
7879
8718
  return {
@@ -7941,7 +8780,7 @@ Port ${port} is already in use.`));
7941
8780
  }
7942
8781
  if (action === "start") {
7943
8782
  const spinner = ora2("Starting OpenCode...").start();
7944
- const proc = await startOpenCode(port);
8783
+ const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
7945
8784
  const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
7946
8785
  if (!health.healthy) {
7947
8786
  spinner.fail("Failed to start OpenCode");
@@ -7953,12 +8792,558 @@ Port ${port} is already in use.`));
7953
8792
  return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
7954
8793
  }
7955
8794
 
8795
+ // src/lib/runner-credentials.ts
8796
+ import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "fs";
8797
+ import { spawn as spawn5 } from "child_process";
8798
+ var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
8799
+ var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
8800
+ var GITHUB_PROBE_TIMEOUT_MS = 1e4;
8801
+ var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
8802
+ var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
8803
+ function commandError2(result) {
8804
+ return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
8805
+ }
8806
+ var runCommand2 = (command, args, opts) => {
8807
+ return new Promise((resolve4) => {
8808
+ let child;
8809
+ let stdout = "";
8810
+ let stderr = "";
8811
+ let settled = false;
8812
+ const timer = {};
8813
+ const finish = (result) => {
8814
+ if (settled) return;
8815
+ settled = true;
8816
+ if (timer.handle) clearTimeout(timer.handle);
8817
+ resolve4(result);
8818
+ };
8819
+ try {
8820
+ child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
8821
+ } catch (error2) {
8822
+ finish({
8823
+ code: null,
8824
+ stdout,
8825
+ stderr: error2 instanceof Error ? error2.message : String(error2),
8826
+ timedOut: false
8827
+ });
8828
+ return;
8829
+ }
8830
+ child.stdout?.setEncoding("utf8");
8831
+ child.stdout?.on("data", (chunk) => {
8832
+ stdout += chunk;
8833
+ });
8834
+ child.stderr?.setEncoding("utf8");
8835
+ child.stderr?.on("data", (chunk) => {
8836
+ stderr += chunk;
8837
+ });
8838
+ child.once("error", (error2) => {
8839
+ finish({
8840
+ code: null,
8841
+ stdout,
8842
+ stderr: stderr === "" ? error2.message : `${stderr}
8843
+ ${error2.message}`,
8844
+ timedOut: false
8845
+ });
8846
+ });
8847
+ child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
8848
+ timer.handle = setTimeout(
8849
+ () => {
8850
+ child.kill("SIGKILL");
8851
+ finish({ code: null, stdout, stderr, timedOut: true });
8852
+ },
8853
+ Math.max(0, opts.timeoutMs)
8854
+ );
8855
+ });
8856
+ };
8857
+ function isEnvironmentObject(value) {
8858
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8859
+ }
8860
+ function secretFailure(marker, detail, log3) {
8861
+ const message = `${marker}: ${detail}`;
8862
+ log3(message, "error");
8863
+ return new Error(message);
8864
+ }
8865
+ async function installRunnerSecret({
8866
+ env,
8867
+ log: log3,
8868
+ commandRunner
8869
+ }) {
8870
+ const arn = env.RUNNER_SECRET_ARN?.trim();
8871
+ if (!arn) {
8872
+ log3("runner secret is not configured; continuing without GitHub and MCP credentials");
8873
+ return false;
8874
+ }
8875
+ const result = await (commandRunner ?? runCommand2)(
8876
+ "aws",
8877
+ [
8878
+ "secretsmanager",
8879
+ "get-secret-value",
8880
+ "--secret-id",
8881
+ arn,
8882
+ "--query",
8883
+ "SecretString",
8884
+ "--output",
8885
+ "text"
8886
+ ],
8887
+ { env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
8888
+ );
8889
+ if (result.timedOut) {
8890
+ throw secretFailure(
8891
+ "CREDENTIAL-RESTORE-TIMEOUT",
8892
+ `runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
8893
+ log3
8894
+ );
8895
+ }
8896
+ if (result.code !== 0) {
8897
+ throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
8898
+ }
8899
+ let payload;
8900
+ try {
8901
+ payload = JSON.parse(result.stdout);
8902
+ } catch (error2) {
8903
+ log3(
8904
+ `RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
8905
+ "warn"
8906
+ );
8907
+ return false;
8908
+ }
8909
+ if (!isEnvironmentObject(payload)) {
8910
+ log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
8911
+ return false;
8912
+ }
8913
+ let populated = 0;
8914
+ let skipped = 0;
8915
+ let githubTokenPopulated = false;
8916
+ for (const [key, value] of Object.entries(payload)) {
8917
+ if (typeof value !== "string" || value.length === 0) continue;
8918
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
8919
+ log3(
8920
+ `RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
8921
+ "warn"
8922
+ );
8923
+ skipped += 1;
8924
+ continue;
8925
+ }
8926
+ env[key] = value;
8927
+ populated += 1;
8928
+ if (key === "GH_TOKEN") githubTokenPopulated = true;
8929
+ }
8930
+ if (populated === 0) {
8931
+ log3(
8932
+ "RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
8933
+ "warn"
8934
+ );
8935
+ } else {
8936
+ log3(
8937
+ `RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
8938
+ );
8939
+ }
8940
+ return githubTokenPopulated;
8941
+ }
8942
+ function restoreFailure(operation, result, log3) {
8943
+ const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
8944
+ log3(message, "error");
8945
+ return new Error(message);
8946
+ }
8947
+ async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
8948
+ const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
8949
+ if (result.timedOut) {
8950
+ log3(
8951
+ `CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
8952
+ "warn"
8953
+ );
8954
+ return result;
8955
+ }
8956
+ if (result.code !== 0) throw restoreFailure(operation, result, log3);
8957
+ return result;
8958
+ }
8959
+ async function restoreCredentialStores({
8960
+ env,
8961
+ log: log3,
8962
+ synchroniserRunner = runSynchroniser
8963
+ }) {
8964
+ await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
8965
+ await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
8966
+ const result = await synchroniserRunner(["model-auth-ready"], {
8967
+ timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
8968
+ });
8969
+ if (result.timedOut) {
8970
+ log3(
8971
+ `CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
8972
+ "warn"
8973
+ );
8974
+ return;
8975
+ }
8976
+ switch (result.code) {
8977
+ case 0:
8978
+ return;
8979
+ case 10:
8980
+ log3(
8981
+ `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.`,
8982
+ "warn"
8983
+ );
8984
+ return;
8985
+ default:
8986
+ log3("could not determine whether this VM has model credentials", "warn");
8987
+ }
8988
+ }
8989
+ var GIT_CREDENTIAL_HELPER_CONTENT = [
8990
+ "#!/usr/bin/env bash",
8991
+ '[ "$1" = get ] || exit 0',
8992
+ "echo username=x-access-token",
8993
+ 'echo "password=${GH_TOKEN}"',
8994
+ ""
8995
+ ].join("\n");
8996
+ async function probeGitHubAccess({ env, log: log3 }) {
8997
+ const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
8998
+ env,
8999
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9000
+ });
9001
+ if (auth.timedOut) {
9002
+ log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
9003
+ return;
9004
+ }
9005
+ if (auth.code !== 0) {
9006
+ log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
9007
+ return;
9008
+ }
9009
+ log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
9010
+ const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
9011
+ env,
9012
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9013
+ });
9014
+ if (remote.code !== 0 || remote.timedOut) return;
9015
+ const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
9016
+ if (!repo) return;
9017
+ const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
9018
+ env,
9019
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9020
+ });
9021
+ if (repository.timedOut) {
9022
+ log3(
9023
+ `GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
9024
+ "warn"
9025
+ );
9026
+ } else if (repository.code !== 0) {
9027
+ log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
9028
+ }
9029
+ }
9030
+ async function configureGitHubAccess({ env, log: log3 }) {
9031
+ if (!env.GH_TOKEN) {
9032
+ log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
9033
+ return;
9034
+ }
9035
+ try {
9036
+ env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
9037
+ writeFileSync4(GIT_CONFIG_GLOBAL, "");
9038
+ writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
9039
+ chmodSync2(GIT_CREDENTIAL_HELPER, 448);
9040
+ const config = [
9041
+ ["user.name", env.GIT_USER_NAME ?? "evident-bot"],
9042
+ ["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
9043
+ ["init.defaultBranch", "main"],
9044
+ ["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
9045
+ ];
9046
+ for (const [key, value] of config) {
9047
+ const result = await runCommand2("git", ["config", "--global", key, value], {
9048
+ env,
9049
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9050
+ });
9051
+ if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
9052
+ }
9053
+ } catch (error2) {
9054
+ log3(
9055
+ `GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
9056
+ "warn"
9057
+ );
9058
+ return;
9059
+ }
9060
+ void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
9061
+ log3(
9062
+ `GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
9063
+ "warn"
9064
+ );
9065
+ });
9066
+ }
9067
+
9068
+ // src/lib/opencode/config-overlay.ts
9069
+ import { execFileSync as execFileSync2 } from "child_process";
9070
+ import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
9071
+ import { isAbsolute as isAbsolute2, join as join8, resolve as resolve3 } from "path";
9072
+ function isFile(filePath) {
9073
+ return existsSync2(filePath) && statSync5(filePath).isFile();
9074
+ }
9075
+ function applyRunnerOpenCodeConfig({
9076
+ overlayPath,
9077
+ cwd = process.cwd(),
9078
+ log: log3
9079
+ }) {
9080
+ if (!overlayPath) {
9081
+ log3("runner OpenCode config is not configured; using the baked project config", "debug");
9082
+ return;
9083
+ }
9084
+ const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
9085
+ const target = isFile(join8(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
9086
+ if (!isFile(source)) {
9087
+ log3(
9088
+ `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
9089
+ "error"
9090
+ );
9091
+ return;
9092
+ }
9093
+ copyFileSync(source, join8(cwd, target));
9094
+ try {
9095
+ execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
9096
+ stdio: "ignore"
9097
+ });
9098
+ } catch (error2) {
9099
+ const detail = error2 instanceof Error ? error2.message : String(error2);
9100
+ log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
9101
+ }
9102
+ log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
9103
+ }
9104
+
9105
+ // src/lib/credential-sync.ts
9106
+ import { renameSync, writeFileSync as writeFileSync5 } from "fs";
9107
+ var CREDENTIAL_SYNC_TICK_TIMEOUT_MS = 3e4;
9108
+ var CREDENTIAL_FLUSH_DEADLINE_MS = 8e3;
9109
+ var CREDENTIAL_FLUSH_ABORT_GRACE_MS = 500;
9110
+ var DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS = 60;
9111
+ var STORES = ["claude", "opencode"];
9112
+ var MAX_FLUSH_PASSES = 2;
9113
+ function outcomesWith(outcome) {
9114
+ return { claude: outcome, opencode: outcome };
9115
+ }
9116
+ function errorMessage(error2) {
9117
+ return error2 instanceof Error ? error2.message : String(error2);
9118
+ }
9119
+ function waitForSettlement(promise, timeoutMs) {
9120
+ return new Promise((resolve4) => {
9121
+ let settled = false;
9122
+ const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
9123
+ const finish = (value) => {
9124
+ if (settled) return;
9125
+ settled = true;
9126
+ clearTimeout(timer);
9127
+ resolve4(value);
9128
+ };
9129
+ promise.then(
9130
+ () => finish(true),
9131
+ () => finish(true)
9132
+ );
9133
+ });
9134
+ }
9135
+ function writeMarker(markerPath, outcomes, log3) {
9136
+ const body = `${STORES.map((store) => `${store}=${outcomes[store]}`).join("\n")}
9137
+ `;
9138
+ const temporaryPath = `${markerPath}.tmp`;
9139
+ try {
9140
+ writeFileSync5(temporaryPath, body, { mode: 384 });
9141
+ renameSync(temporaryPath, markerPath);
9142
+ } catch (error2) {
9143
+ log3(`CREDENTIAL-FLUSH-MARKER-UNWRITABLE: ${markerPath}: ${errorMessage(error2)}`, "warn");
9144
+ }
9145
+ }
9146
+ function intervalSeconds(env, log3) {
9147
+ const raw = env.CREDS_SYNC_INTERVAL;
9148
+ if (raw === void 0 || /^[1-9][0-9]*$/.test(raw) && Number.isSafeInteger(Number(raw))) {
9149
+ return raw === void 0 ? DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS : Number(raw);
9150
+ }
9151
+ log3(
9152
+ `CREDENTIAL-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${raw}' is not a positive integer; using ${DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS}s`,
9153
+ "warn"
9154
+ );
9155
+ return DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS;
9156
+ }
9157
+ async function runFlushStore(store, env, synchroniserRunner, log3, deadlineAt) {
9158
+ const remainingMs = deadlineAt - Date.now();
9159
+ if (remainingMs <= 0) return { outcome: "timeout", orphaned: false };
9160
+ const controller = new AbortController();
9161
+ let result;
9162
+ let failed = false;
9163
+ const completion = Promise.resolve().then(
9164
+ () => synchroniserRunner(["sync-once", store], {
9165
+ timeoutMs: remainingMs,
9166
+ env,
9167
+ signal: controller.signal
9168
+ })
9169
+ ).then(
9170
+ (value) => {
9171
+ result = value;
9172
+ },
9173
+ (error2) => {
9174
+ failed = true;
9175
+ log3(`CREDENTIAL-FLUSH-ERROR: ${store}: ${errorMessage(error2)}`, "warn");
9176
+ }
9177
+ );
9178
+ const abortTimer = setTimeout(() => controller.abort(), remainingMs);
9179
+ const settledBeforeDeadline = await waitForSettlement(completion, remainingMs);
9180
+ clearTimeout(abortTimer);
9181
+ if (!settledBeforeDeadline) {
9182
+ controller.abort();
9183
+ const settledAfterAbort = await waitForSettlement(completion, CREDENTIAL_FLUSH_ABORT_GRACE_MS);
9184
+ if (!settledAfterAbort) return { outcome: "timeout", orphaned: true };
9185
+ return { outcome: "timeout", orphaned: false };
9186
+ }
9187
+ if (failed || !result) return { outcome: "failed", orphaned: false };
9188
+ if (result.timedOut || Date.now() >= deadlineAt) {
9189
+ return { outcome: "timeout", orphaned: false };
9190
+ }
9191
+ return { outcome: result.code === 0 ? "ok" : "failed", orphaned: false };
9192
+ }
9193
+ function createCredentialSync({
9194
+ markerPath,
9195
+ env,
9196
+ log: log3,
9197
+ synchroniserRunner = runSynchroniser
9198
+ }) {
9199
+ const persistenceDisabled = !env.PERSISTENCE_BUCKET;
9200
+ let disabled = persistenceDisabled;
9201
+ let armed = false;
9202
+ let stopped = false;
9203
+ let timer;
9204
+ let inFlight;
9205
+ let activeTickAbort;
9206
+ let lastTickFailed;
9207
+ let flushPromise;
9208
+ const scheduleTick = (intervalMs, startTick2) => {
9209
+ if (stopped) return;
9210
+ timer = setTimeout(() => {
9211
+ timer = void 0;
9212
+ startTick2();
9213
+ }, intervalMs);
9214
+ };
9215
+ const startTick = (intervalMs) => {
9216
+ if (stopped) return;
9217
+ const controller = new AbortController();
9218
+ activeTickAbort = controller;
9219
+ const tick = (async () => {
9220
+ const outcomes = {
9221
+ claude: "failed",
9222
+ opencode: "failed"
9223
+ };
9224
+ for (const store of STORES) {
9225
+ if (controller.signal.aborted) break;
9226
+ try {
9227
+ const result = await synchroniserRunner(["sync-once", store], {
9228
+ timeoutMs: CREDENTIAL_SYNC_TICK_TIMEOUT_MS,
9229
+ env,
9230
+ signal: controller.signal
9231
+ });
9232
+ outcomes[store] = !result.timedOut && result.code === 0 ? "ok" : "failed";
9233
+ } catch (error2) {
9234
+ outcomes[store] = "failed";
9235
+ log3(`CREDENTIAL-SYNC-TICK-ERROR: ${store}: ${errorMessage(error2)}`, "debug");
9236
+ }
9237
+ }
9238
+ const failed = STORES.some((store) => outcomes[store] === "failed");
9239
+ log3(
9240
+ `CREDENTIAL-SYNC-TICK: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
9241
+ "debug"
9242
+ );
9243
+ if (failed && lastTickFailed !== true) {
9244
+ log3(
9245
+ "CREDENTIAL-SYNC-FAILED: an interval credential sync failed; retrying on the next tick",
9246
+ "warn"
9247
+ );
9248
+ } else if (!failed && lastTickFailed === true) {
9249
+ log3("CREDENTIAL-SYNC-RECOVERED: interval credential sync succeeded again", "warn");
9250
+ }
9251
+ lastTickFailed = failed;
9252
+ })().finally(() => {
9253
+ if (activeTickAbort === controller) activeTickAbort = void 0;
9254
+ if (inFlight === tick) inFlight = void 0;
9255
+ scheduleTick(intervalMs, () => startTick(intervalMs));
9256
+ });
9257
+ inFlight = tick;
9258
+ };
9259
+ const performFlush = async () => {
9260
+ stopped = true;
9261
+ if (timer) {
9262
+ clearTimeout(timer);
9263
+ timer = void 0;
9264
+ }
9265
+ const deadlineAt = Date.now() + CREDENTIAL_FLUSH_DEADLINE_MS;
9266
+ if (inFlight) {
9267
+ const settled = await waitForSettlement(inFlight, CREDENTIAL_FLUSH_DEADLINE_MS);
9268
+ if (!settled) {
9269
+ activeTickAbort?.abort();
9270
+ const settledAfterAbort = await waitForSettlement(
9271
+ inFlight,
9272
+ CREDENTIAL_FLUSH_ABORT_GRACE_MS
9273
+ );
9274
+ if (!settledAfterAbort) {
9275
+ log3(
9276
+ "CREDENTIAL-FLUSH-ORPHANED-TICK: a sync-once child could not be proven settled; skipping the boundary flush rather than becoming a second writer",
9277
+ "warn"
9278
+ );
9279
+ return { outcomes: outcomesWith("timeout"), orphaned: true };
9280
+ }
9281
+ }
9282
+ }
9283
+ if (disabled) return { outcomes: outcomesWith("skipped"), orphaned: false };
9284
+ const outcomes = outcomesWith("timeout");
9285
+ for (const store of STORES) {
9286
+ const result = await runFlushStore(store, env, synchroniserRunner, log3, deadlineAt);
9287
+ if (result.orphaned) {
9288
+ log3(
9289
+ "CREDENTIAL-FLUSH-ORPHANED-CHILD: a flush sync-once child could not be proven settled; refusing another flush pass rather than becoming a second writer",
9290
+ "warn"
9291
+ );
9292
+ return { outcomes: outcomesWith("timeout"), orphaned: true };
9293
+ }
9294
+ outcomes[store] = result.outcome;
9295
+ }
9296
+ return { outcomes, orphaned: false };
9297
+ };
9298
+ let flushPasses = 0;
9299
+ let lastFlush;
9300
+ return {
9301
+ arm() {
9302
+ if (stopped || armed) return;
9303
+ armed = true;
9304
+ if (persistenceDisabled) {
9305
+ disabled = true;
9306
+ log3(
9307
+ "CREDENTIAL-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot",
9308
+ "warn"
9309
+ );
9310
+ return;
9311
+ }
9312
+ disabled = false;
9313
+ const intervalMs = intervalSeconds(env, log3) * 1e3;
9314
+ scheduleTick(intervalMs, () => startTick(intervalMs));
9315
+ },
9316
+ async stopAndFlush(publish) {
9317
+ let result;
9318
+ const runningFlush = flushPromise;
9319
+ if (runningFlush) {
9320
+ result = await runningFlush;
9321
+ } else if (flushPasses >= MAX_FLUSH_PASSES || lastFlush?.orphaned) {
9322
+ result = lastFlush ?? { outcomes: outcomesWith("timeout"), orphaned: true };
9323
+ } else {
9324
+ flushPasses++;
9325
+ const currentFlush = performFlush();
9326
+ flushPromise = currentFlush;
9327
+ try {
9328
+ result = await currentFlush;
9329
+ lastFlush = result;
9330
+ } finally {
9331
+ if (flushPromise === currentFlush) flushPromise = void 0;
9332
+ }
9333
+ }
9334
+ if (publish) writeMarker(markerPath, result.outcomes, log3);
9335
+ return result.outcomes;
9336
+ }
9337
+ };
9338
+ }
9339
+
7956
9340
  // src/commands/run.ts
7957
9341
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
7958
9342
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
7959
9343
  var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
7960
9344
  var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
7961
9345
  var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
9346
+ var CHILD_STOP_TIMEOUT_MS = 1e4;
7962
9347
  function resolveLogLevel(options) {
7963
9348
  const accepted = Object.keys(LOG_LEVELS);
7964
9349
  const validate = (value, source) => {
@@ -7989,11 +9374,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
7989
9374
  if (trimmed === "") {
7990
9375
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
7991
9376
  }
7992
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join7(homeDir, trimmed.slice(2)) : trimmed;
7993
- if (!isAbsolute2(expanded)) {
9377
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join9(homeDir, trimmed.slice(2)) : trimmed;
9378
+ if (!isAbsolute3(expanded)) {
7994
9379
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
7995
9380
  }
7996
- const normalized = resolvePath(expanded);
9381
+ const normalized = resolvePath2(expanded);
7997
9382
  if (parse(normalized).root === normalized) {
7998
9383
  throw new Error(
7999
9384
  `--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
@@ -8109,7 +9494,7 @@ function logActivity(state, entry) {
8109
9494
  }
8110
9495
  function reportSessionDbRecovery(state) {
8111
9496
  try {
8112
- const report = drainSessionDbRecoveryReport({ homeDir: homedir4(), env: process.env });
9497
+ const report = drainSessionDbRecoveryReport({ homeDir: homedir5(), env: process.env });
8113
9498
  for (const record of report.records) {
8114
9499
  const activity = buildSessionDbRecoveryActivity(record);
8115
9500
  if (!activity) throw new Error("could not map session-DB recovery record");
@@ -8127,6 +9512,16 @@ function reportSessionDbRecovery(state) {
8127
9512
  );
8128
9513
  }
8129
9514
  }
9515
+ function reportSessionDbRecoveryRecord(state, record) {
9516
+ const activity = buildSessionDbRecoveryActivity(record);
9517
+ if (!activity) throw new Error("could not map session-DB recovery record");
9518
+ logActivity(state, {
9519
+ type: activity.level === "error" ? "error" : "info",
9520
+ level: activity.level,
9521
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
9522
+ metadata: activity.metadata
9523
+ });
9524
+ }
8130
9525
  function displayStatus(state) {
8131
9526
  if (!state.interactive) return;
8132
9527
  const attempt = state.connection?.reconnectAttempt ?? 0;
@@ -8281,8 +9676,8 @@ async function driveChannels(state, driver) {
8281
9676
  state.running = false;
8282
9677
  break;
8283
9678
  }
8284
- const errorMessage = error2 instanceof Error ? error2.message : String(error2);
8285
- logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
9679
+ const errorMessage2 = error2 instanceof Error ? error2.message : String(error2);
9680
+ logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage2}` });
8286
9681
  if (state.interactive) displayStatus(state);
8287
9682
  if (driver.hasInFlightWatchers()) {
8288
9683
  consecutiveDrainFailures = 0;
@@ -8299,7 +9694,7 @@ async function driveChannels(state, driver) {
8299
9694
  }
8300
9695
  }
8301
9696
  }
8302
- await new Promise((resolve3) => setTimeout(resolve3, CHANNEL_POLL_INTERVAL_MS));
9697
+ await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
8303
9698
  const cycleMs = performance.now() - cycleStartedAtMs;
8304
9699
  if (idleThisCycle) idleMs += cycleMs;
8305
9700
  if (unreachableThisCycle) unreachableMs += cycleMs;
@@ -8322,7 +9717,43 @@ async function driveChannels(state, driver) {
8322
9717
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
8323
9718
  var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
8324
9719
  function sessionDbPath() {
8325
- return join7(homedir4(), ".local", "share", "opencode", "opencode.db");
9720
+ return join9(homedir5(), ".local", "share", "opencode", "opencode.db");
9721
+ }
9722
+ function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
9723
+ const record = {
9724
+ v: 1,
9725
+ event: "session_db_recovery",
9726
+ at: (/* @__PURE__ */ new Date()).toISOString(),
9727
+ stage: "verify",
9728
+ outcome: "schema_provenance_mismatch",
9729
+ severity: "error",
9730
+ reason: provenance.reason ?? "schema-provenance-mismatch",
9731
+ litestream_exit_code: null,
9732
+ attempt: null,
9733
+ replica_objects: null,
9734
+ replica_bytes: null,
9735
+ quarantine_destination: null,
9736
+ quarantined_objects: null,
9737
+ quarantine_failed_objects: null,
9738
+ quarantined_bytes: null,
9739
+ verified_restore_point: null,
9740
+ restore_points_tried: null,
9741
+ provenance_reason: provenance.reason,
9742
+ provenance_migration_delta: provenance.migrationDelta,
9743
+ replication_suspended: false,
9744
+ dbPath: sessionDbPath(),
9745
+ recorded_version: provenance.recordedVersion,
9746
+ current_version: currentVersion,
9747
+ provenance_pre_boot_migration_count: preBootMigrationCount
9748
+ };
9749
+ const activity = buildSessionDbRecoveryActivity(record);
9750
+ if (!activity) throw new Error("could not map session-DB provenance activity");
9751
+ logActivity(state, {
9752
+ type: activity.level === "error" ? "error" : "info",
9753
+ level: activity.level,
9754
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
9755
+ metadata: activity.metadata
9756
+ });
8326
9757
  }
8327
9758
  async function runSweep(state, driver, config) {
8328
9759
  const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
@@ -8369,7 +9800,7 @@ async function runSweep(state, driver, config) {
8369
9800
  const reclaimResult = await reclaimSessionDbSpace({
8370
9801
  dbPath: sessionDbPath(),
8371
9802
  maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
8372
- allowFullVacuum: protectedNow.size === 0
9803
+ allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
8373
9804
  });
8374
9805
  if (reclaimResult.ok) {
8375
9806
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
@@ -8405,7 +9836,7 @@ function scheduleSessionCleanup(state, driver, options) {
8405
9836
  for (const warning2 of config.warnings) {
8406
9837
  logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
8407
9838
  }
8408
- const dbBytes = statSessionDbBytes(homedir4());
9839
+ const dbBytes = statSessionDbBytes(homedir5());
8409
9840
  void (async () => {
8410
9841
  const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
8411
9842
  const sizeWarning = buildSessionStoreSizeWarning({
@@ -8606,7 +10037,7 @@ function scheduleResourceUsageReporting(state, options) {
8606
10037
  });
8607
10038
  return;
8608
10039
  }
8609
- const collect = createResourceUsageCollector(homedir4());
10040
+ const collect = createResourceUsageCollector(homedir5());
8610
10041
  let consecutiveFailures = 0;
8611
10042
  const tick = async () => {
8612
10043
  try {
@@ -8716,21 +10147,39 @@ async function cleanup(state, opts = {}) {
8716
10147
  clearTimeout(state.resourceUsageTimer);
8717
10148
  state.resourceUsageTimer = null;
8718
10149
  }
10150
+ const credentialSync = state.credentialSync;
10151
+ const flushCredentials = credentialSync ? async (phase, publish) => {
10152
+ await timeShutdownPhase(state, durations, phase, async () => {
10153
+ const outcomes = await credentialSync.stopAndFlush(publish);
10154
+ const level = Object.values(outcomes).every((outcome) => outcome === "ok") ? "info" : "warn";
10155
+ log2(
10156
+ state,
10157
+ `Credential flush: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
10158
+ level
10159
+ );
10160
+ });
10161
+ } : void 0;
10162
+ let drainSettled = true;
8719
10163
  if (opts.graceful && state.channelDriver) {
8720
10164
  state.channelDriver.stop();
10165
+ }
10166
+ if (flushCredentials) {
10167
+ await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
10168
+ }
10169
+ if (opts.graceful && state.channelDriver) {
8721
10170
  log2(state, "Draining in-flight channel work before shutdown...");
8722
10171
  if (state.interactive) {
8723
10172
  logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
8724
10173
  displayStatus(state);
8725
10174
  }
8726
10175
  const driver = state.channelDriver;
8727
- const settled = await timeShutdownPhase(
10176
+ drainSettled = await timeShutdownPhase(
8728
10177
  state,
8729
10178
  durations,
8730
10179
  "drain",
8731
10180
  () => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
8732
10181
  );
8733
- if (!settled) {
10182
+ if (!drainSettled) {
8734
10183
  logActivity(state, {
8735
10184
  type: "info",
8736
10185
  message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
@@ -8738,6 +10187,9 @@ async function cleanup(state, opts = {}) {
8738
10187
  if (state.interactive) displayStatus(state);
8739
10188
  }
8740
10189
  }
10190
+ if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
10191
+ await flushCredentials("credential_flush_final", true);
10192
+ }
8741
10193
  await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
8742
10194
  if (state.connection) {
8743
10195
  const connection = state.connection;
@@ -8746,15 +10198,31 @@ async function cleanup(state, opts = {}) {
8746
10198
  }
8747
10199
  if (state.opencodeProcess) {
8748
10200
  const opencodeProcess = state.opencodeProcess;
8749
- await timeShutdownPhase(state, durations, "opencode_stop", () => stopOpenCode(opencodeProcess));
10201
+ const result = await timeShutdownPhase(
10202
+ state,
10203
+ durations,
10204
+ "opencode_stop",
10205
+ () => stopOpenCodeAndWait(opencodeProcess, CHILD_STOP_TIMEOUT_MS)
10206
+ );
8750
10207
  if (state.interactive) {
8751
- logActivity(state, { type: "info", message: "Stopped OpenCode process" });
10208
+ logActivity(state, { type: "info", message: `Stopped OpenCode process (${result.outcome})` });
8752
10209
  displayStatus(state);
8753
10210
  } else {
8754
- log2(state, "Stopped OpenCode process");
10211
+ log2(state, `Stopped OpenCode process (${result.outcome})`);
8755
10212
  }
8756
10213
  state.opencodeProcess = null;
8757
10214
  }
10215
+ if (state.litestreamProcess) {
10216
+ const litestreamProcess = state.litestreamProcess;
10217
+ const result = await timeShutdownPhase(
10218
+ state,
10219
+ durations,
10220
+ "litestream_stop",
10221
+ () => stopSessionDbReplication(litestreamProcess, CHILD_STOP_TIMEOUT_MS)
10222
+ );
10223
+ log2(state, `Stopped litestream replication (${result.outcome})`);
10224
+ state.litestreamProcess = null;
10225
+ }
8758
10226
  return durations;
8759
10227
  }
8760
10228
  async function run(options) {
@@ -8763,7 +10231,12 @@ async function run(options) {
8763
10231
  let fileSyncDirectories;
8764
10232
  try {
8765
10233
  logLevel = resolveLogLevel(options);
8766
- fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir4());
10234
+ fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir5());
10235
+ if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
10236
+ throw new Error(
10237
+ "--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
10238
+ );
10239
+ }
8767
10240
  } catch (error2) {
8768
10241
  const message = error2 instanceof Error ? error2.message : String(error2);
8769
10242
  if (options.json) {
@@ -8787,7 +10260,9 @@ async function run(options) {
8787
10260
  connected: false,
8788
10261
  opencodeConnected: false,
8789
10262
  opencodeVersion: null,
10263
+ sessionDbProvenanceAnomaly: false,
8790
10264
  opencodeProcess: null,
10265
+ litestreamProcess: null,
8791
10266
  connection: null,
8792
10267
  channelDriver: null,
8793
10268
  running: true,
@@ -8801,9 +10276,23 @@ async function run(options) {
8801
10276
  openaiUsageTimer: null,
8802
10277
  openaiUsageRearm: null,
8803
10278
  resourceUsageTimer: null,
10279
+ credentialSync: null,
8804
10280
  authHeader: ""
8805
10281
  };
8806
10282
  setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
10283
+ if (options.credentialSyncMarker) {
10284
+ state.credentialSync = createCredentialSync({
10285
+ markerPath: options.credentialSyncMarker,
10286
+ env: process.env,
10287
+ log: (message, level = "info") => {
10288
+ if (level === "error") {
10289
+ logActivity(state, { type: "error", error: message });
10290
+ } else {
10291
+ logActivity(state, { type: "info", level, message });
10292
+ }
10293
+ }
10294
+ });
10295
+ }
8807
10296
  if (fileSyncDirectories.length > 0) {
8808
10297
  log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
8809
10298
  } else {
@@ -8854,8 +10343,8 @@ async function run(options) {
8854
10343
  return true;
8855
10344
  }
8856
10345
  );
8857
- const timedOut = new Promise((resolve3) => {
8858
- timer = setTimeout(() => resolve3(false), telemetryBudgetMs);
10346
+ const timedOut = new Promise((resolve4) => {
10347
+ timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
8859
10348
  });
8860
10349
  if (!await Promise.race([flushed, timedOut])) {
8861
10350
  log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
@@ -8995,7 +10484,68 @@ async function run(options) {
8995
10484
  } else {
8996
10485
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
8997
10486
  }
10487
+ if (options.restoreRunnerCredentials) {
10488
+ log2(state, "Restoring runner credentials before starting OpenCode");
10489
+ const credentialContext = {
10490
+ env: process.env,
10491
+ log: (message, level = "info") => {
10492
+ if (level === "error") {
10493
+ logActivity(state, { type: "error", error: message });
10494
+ } else {
10495
+ logActivity(state, { type: "info", level, message });
10496
+ }
10497
+ }
10498
+ };
10499
+ const githubTokenPopulated = await installRunnerSecret(credentialContext);
10500
+ await restoreCredentialStores(credentialContext);
10501
+ if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
10502
+ }
10503
+ state.credentialSync?.arm();
10504
+ let sessionDbVerifyFatal = false;
10505
+ if (!options.restoreSessionDb) {
10506
+ log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
10507
+ } else {
10508
+ const health = await checkOpenCodeHealth(state.port);
10509
+ if (health.healthy) {
10510
+ log2(
10511
+ state,
10512
+ "Skipping session-DB restore: OpenCode is already serving this database",
10513
+ "debug"
10514
+ );
10515
+ } else {
10516
+ const result = await restoreAndVerifySessionDb({
10517
+ dbPath: sessionDbPath(),
10518
+ litestreamConfig: options.litestreamConfig,
10519
+ noReplicateMarker: options.sessionDbNoReplicateMarker,
10520
+ env: process.env,
10521
+ log: (message, level = "info") => {
10522
+ if (level === "error") {
10523
+ logActivity(state, { type: "error", error: message });
10524
+ } else {
10525
+ logActivity(state, { type: "info", level, message });
10526
+ }
10527
+ },
10528
+ reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
10529
+ });
10530
+ sessionDbVerifyFatal = result.verifyFatal;
10531
+ }
10532
+ }
8998
10533
  reportSessionDbRecovery(state);
10534
+ if (sessionDbVerifyFatal) {
10535
+ throw new Error(
10536
+ "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
10537
+ );
10538
+ }
10539
+ applyRunnerOpenCodeConfig({
10540
+ overlayPath: options.opencodeConfigOverlay,
10541
+ log: (message, level = "info") => {
10542
+ if (level === "error") {
10543
+ logActivity(state, { type: "error", error: message });
10544
+ } else {
10545
+ logActivity(state, { type: "info", level, message });
10546
+ }
10547
+ }
10548
+ });
8999
10549
  const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
9000
10550
  for (const warning2 of opencodeStartTimeoutWarnings) {
9001
10551
  logActivity(state, { type: "info", level: "warn", message: warning2 });
@@ -9004,6 +10554,7 @@ async function run(options) {
9004
10554
  for (const warning2 of maxActiveSessionsWarnings) {
9005
10555
  logActivity(state, { type: "info", level: "warn", message: warning2 });
9006
10556
  }
10557
+ const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
9007
10558
  const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
9008
10559
  try {
9009
10560
  const oc = await ensureOpenCodeRunning({
@@ -9011,11 +10562,41 @@ async function run(options) {
9011
10562
  interactive: state.interactive,
9012
10563
  agentId: state.agentId,
9013
10564
  log: (message) => log2(state, message),
9014
- startTimeoutMs: opencodeStartTimeoutMs
10565
+ startTimeoutMs: opencodeStartTimeoutMs,
10566
+ inheritStdio: Boolean(options.opencodePidFile)
9015
10567
  });
9016
10568
  state.port = oc.port;
9017
- state.opencodeProcess = oc.process;
10569
+ state.opencodeProcess = options.opencodePidFile ? null : oc.process;
9018
10570
  state.opencodeVersion = oc.version;
10571
+ if (options.opencodePidFile && oc.process?.pid !== void 0) {
10572
+ try {
10573
+ writeFileSync6(options.opencodePidFile, `${oc.process.pid}
10574
+ `, { mode: 384 });
10575
+ chmodSync3(options.opencodePidFile, 384);
10576
+ } catch (error2) {
10577
+ logActivity(state, {
10578
+ type: "error",
10579
+ error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
10580
+ });
10581
+ }
10582
+ }
10583
+ if (state.opencodeVersion !== null) {
10584
+ const provenance = checkSessionDbProvenance({
10585
+ dbPath: sessionDbPath(),
10586
+ currentVersion: state.opencodeVersion,
10587
+ homeDir: homedir5(),
10588
+ env: process.env
10589
+ });
10590
+ if (provenance.anomaly) {
10591
+ state.sessionDbProvenanceAnomaly = true;
10592
+ logSessionDbProvenanceMismatch(
10593
+ state,
10594
+ provenance,
10595
+ state.opencodeVersion,
10596
+ preBootMigrationIds?.length ?? null
10597
+ );
10598
+ }
10599
+ }
9019
10600
  state.opencodeConnected = oc.notReadyReason === null;
9020
10601
  const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
9021
10602
  ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
@@ -9052,6 +10633,108 @@ async function run(options) {
9052
10633
  ocSpinner?.fail(error2.message);
9053
10634
  throw error2;
9054
10635
  }
10636
+ if (options.litestreamPidFile) {
10637
+ if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
10638
+ log2(
10639
+ state,
10640
+ `Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
10641
+ );
10642
+ } else if (!options.litestreamConfig) {
10643
+ logActivity(state, {
10644
+ type: "info",
10645
+ level: "warn",
10646
+ message: "Skipping Litestream replication because no configuration file was provided"
10647
+ });
10648
+ } else {
10649
+ let existingPid;
10650
+ if (existsSync3(options.litestreamPidFile)) {
10651
+ try {
10652
+ const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
10653
+ const parsedPid = Number(rawPid);
10654
+ if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
10655
+ existingPid = parsedPid;
10656
+ }
10657
+ } catch (error2) {
10658
+ logActivity(state, {
10659
+ type: "info",
10660
+ level: "warn",
10661
+ message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
10662
+ });
10663
+ }
10664
+ }
10665
+ if (existingPid !== void 0 && isProcessAlive(existingPid)) {
10666
+ log2(state, `Litestream replication is already running with pid ${existingPid}`);
10667
+ } else {
10668
+ const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
10669
+ state.litestreamProcess = null;
10670
+ let failureHandled = false;
10671
+ const reportImageOwnedReplicationFailure = (message) => {
10672
+ if (failureHandled || state.shuttingDown || !state.running) return;
10673
+ failureHandled = true;
10674
+ logActivity(state, { type: "error", error: message });
10675
+ if (state.interactive) displayStatus(state);
10676
+ };
10677
+ litestreamProcess.on("exit", (code, signal) => {
10678
+ reportImageOwnedReplicationFailure(
10679
+ `Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
10680
+ );
10681
+ });
10682
+ litestreamProcess.on("error", (error2) => {
10683
+ reportImageOwnedReplicationFailure(
10684
+ `Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
10685
+ );
10686
+ });
10687
+ try {
10688
+ if (litestreamProcess.pid !== void 0) {
10689
+ writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
10690
+ `, {
10691
+ mode: 384
10692
+ });
10693
+ chmodSync3(options.litestreamPidFile, 384);
10694
+ }
10695
+ } catch (error2) {
10696
+ logActivity(state, {
10697
+ type: "error",
10698
+ error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
10699
+ });
10700
+ }
10701
+ log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
10702
+ }
10703
+ }
10704
+ } else if (options.litestreamConfig) {
10705
+ const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
10706
+ state.litestreamProcess = litestreamProcess;
10707
+ let failureHandled = false;
10708
+ const failRunForReplication = (message) => {
10709
+ if (failureHandled || state.shuttingDown || !state.running) return;
10710
+ failureHandled = true;
10711
+ state.shuttingDown = true;
10712
+ logActivity(state, { type: "error", error: message });
10713
+ if (state.interactive) displayStatus(state);
10714
+ void (async () => {
10715
+ try {
10716
+ await cleanup(state);
10717
+ await shutdownTelemetry();
10718
+ } catch (error2) {
10719
+ console.error(
10720
+ `[run] replication failure cleanup failed: ${error2 instanceof Error ? error2.message : String(error2)}`
10721
+ );
10722
+ }
10723
+ process.exit(1);
10724
+ })();
10725
+ };
10726
+ litestreamProcess.on("exit", (code, signal) => {
10727
+ failRunForReplication(
10728
+ `Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
10729
+ );
10730
+ });
10731
+ litestreamProcess.on("error", (error2) => {
10732
+ failRunForReplication(
10733
+ `Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
10734
+ );
10735
+ });
10736
+ log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
10737
+ }
9055
10738
  const tunnelSpinner = interactive && !state.json ? ora3("Connecting tunnel...").start() : null;
9056
10739
  const channelDriver = new ChannelDriver({
9057
10740
  agentId: state.agentId,
@@ -9063,7 +10746,7 @@ async function run(options) {
9063
10746
  // #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
9064
10747
  // REJECTED with `file_sync_disabled` on the ack, not silently ignored.
9065
10748
  fileSyncDirectories,
9066
- homeDir: homedir4(),
10749
+ homeDir: homedir5(),
9067
10750
  maxActiveSessions,
9068
10751
  log: (entry) => (
9069
10752
  // Thread the driver's real level straight through so `debug`/`warn`
@@ -9255,7 +10938,7 @@ async function run(options) {
9255
10938
  }
9256
10939
 
9257
10940
  // src/index.ts
9258
- var { version } = createRequire(import.meta.url)("../package.json");
10941
+ var { version } = createRequire2(import.meta.url)("../package.json");
9259
10942
  var program = new Command();
9260
10943
  program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
9261
10944
  "--endpoint <url>",
@@ -9312,6 +10995,30 @@ program.command("run").description("Connect to Evident and process messages").op
9312
10995
  ).option(
9313
10996
  "--tunnel-ready-file <path>",
9314
10997
  "Path to write once the tunnel is connected (opt-in; unused unless an operator/image sets it)"
10998
+ ).option(
10999
+ "--litestream-config <path>",
11000
+ "Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
11001
+ ).option(
11002
+ "--opencode-pid-file <path>",
11003
+ "Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
11004
+ ).option(
11005
+ "--litestream-pid-file <path>",
11006
+ "Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
11007
+ ).option(
11008
+ "--session-db-no-replicate-marker <path>",
11009
+ "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."
11010
+ ).option(
11011
+ "--restore-session-db",
11012
+ "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."
11013
+ ).option(
11014
+ "--restore-runner-credentials",
11015
+ "Restore the hosted runner secret and persisted credential stores before starting OpenCode."
11016
+ ).option(
11017
+ "--opencode-config-overlay <path>",
11018
+ "Apply this runner-provided OpenCode config before starting OpenCode."
11019
+ ).option(
11020
+ "--credential-sync-marker <path>",
11021
+ "Own the interval credential sync and write this marker once the shutdown flush has finished, so the runner image's lifecycle hooks can wait on it."
9315
11022
  ).action(
9316
11023
  (options) => {
9317
11024
  run({
@@ -9343,7 +11050,15 @@ program.command("run").description("Connect to Evident and process messages").op
9343
11050
  // Raw values — expansion/validation is single-sourced in run.ts's
9344
11051
  // resolveFileSyncDirectories.
9345
11052
  enableFileSyncTo: options.enableFileSyncTo,
9346
- tunnelReadyFile: options.tunnelReadyFile
11053
+ tunnelReadyFile: options.tunnelReadyFile,
11054
+ litestreamConfig: options.litestreamConfig,
11055
+ opencodePidFile: options.opencodePidFile,
11056
+ litestreamPidFile: options.litestreamPidFile,
11057
+ sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
11058
+ restoreSessionDb: options.restoreSessionDb,
11059
+ restoreRunnerCredentials: options.restoreRunnerCredentials,
11060
+ opencodeConfigOverlay: options.opencodeConfigOverlay,
11061
+ credentialSyncMarker: options.credentialSyncMarker
9347
11062
  });
9348
11063
  }
9349
11064
  );