@evident-ai/cli 3.4.1-dev.8715e3a → 3.4.1-dev.88fb738

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
  }
@@ -722,6 +722,14 @@ function toReportedWindow(window) {
722
722
  if (!window) return null;
723
723
  return { utilization: window.utilization, resets_at: window.resetsAt };
724
724
  }
725
+ function toReportedOwner(snapshot) {
726
+ if (!snapshot.owner) return null;
727
+ return {
728
+ email: snapshot.owner.email,
729
+ organization_name: snapshot.owner.organizationName,
730
+ rate_limit_tier: snapshot.owner.rateLimitTier
731
+ };
732
+ }
725
733
  async function reportClaudeUsage(agentId, authHeader, snapshot) {
726
734
  try {
727
735
  const apiUrl = getApiUrlConfig();
@@ -730,7 +738,8 @@ async function reportClaudeUsage(agentId, authHeader, snapshot) {
730
738
  headers: { Authorization: authHeader, "Content-Type": "application/json" },
731
739
  body: JSON.stringify({
732
740
  five_hour: toReportedWindow(snapshot.fiveHour),
733
- seven_day: toReportedWindow(snapshot.sevenDay)
741
+ seven_day: toReportedWindow(snapshot.sevenDay),
742
+ owner: toReportedOwner(snapshot)
734
743
  }),
735
744
  signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
736
745
  });
@@ -996,7 +1005,10 @@ import { readFileSync } from "fs";
996
1005
  import { homedir } from "os";
997
1006
  import { join } from "path";
998
1007
  var CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
1008
+ var CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
1009
+ var CLAUDE_PROFILE_TIMEOUT_MS = 2e3;
999
1010
  var KEYCHAIN_SERVICE = "Claude Code-credentials";
1011
+ var cachedOwner = null;
1000
1012
  var CLAUDE_CREDENTIALS_SEGMENTS = [".claude", ".credentials.json"];
1001
1013
  function parseClaudeCliCredentials(raw) {
1002
1014
  let parsed;
@@ -1070,6 +1082,47 @@ function toWindow(value) {
1070
1082
  }
1071
1083
  return { utilization: window.utilization, resetsAt };
1072
1084
  }
1085
+ function ownerLookupFailure(error2) {
1086
+ const name = error2?.name;
1087
+ return name === "TimeoutError" || name === "AbortError" ? "timed out" : "request failed";
1088
+ }
1089
+ async function getClaudeUsageOwner(accessToken) {
1090
+ if (cachedOwner?.accessToken === accessToken) {
1091
+ return { owner: cachedOwner.owner, ownerLookupError: null };
1092
+ }
1093
+ try {
1094
+ const response = await fetch(CLAUDE_PROFILE_URL, {
1095
+ headers: {
1096
+ Authorization: `Bearer ${accessToken}`,
1097
+ "Content-Type": "application/json",
1098
+ "anthropic-version": "2023-06-01"
1099
+ },
1100
+ signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
1101
+ });
1102
+ if (!response.ok) {
1103
+ return { owner: null, ownerLookupError: `HTTP ${response.status}` };
1104
+ }
1105
+ let body;
1106
+ try {
1107
+ body = await response.json();
1108
+ } catch (error2) {
1109
+ return { owner: null, ownerLookupError: "malformed response" };
1110
+ }
1111
+ const profile = body;
1112
+ if (typeof profile.account?.email !== "string" || !profile.account.email.trim()) {
1113
+ return { owner: null, ownerLookupError: "malformed response" };
1114
+ }
1115
+ const owner = {
1116
+ email: profile.account.email,
1117
+ organizationName: typeof profile.organization?.name === "string" && profile.organization.name.trim() ? profile.organization.name.trim() : null,
1118
+ rateLimitTier: typeof profile.organization?.rate_limit_tier === "string" && profile.organization.rate_limit_tier.trim() ? profile.organization.rate_limit_tier.trim() : null
1119
+ };
1120
+ cachedOwner = { accessToken, owner };
1121
+ return { owner, ownerLookupError: null };
1122
+ } catch (error2) {
1123
+ return { owner: null, ownerLookupError: ownerLookupFailure(error2) };
1124
+ }
1125
+ }
1073
1126
  async function getClaudeUsage() {
1074
1127
  const credentials2 = readClaudeCliCredentials();
1075
1128
  if (!credentials2) {
@@ -1089,15 +1142,19 @@ async function getClaudeUsage() {
1089
1142
  Authorization: `Bearer ${credentials2.accessToken}`,
1090
1143
  "Content-Type": "application/json",
1091
1144
  "anthropic-version": "2023-06-01"
1092
- }
1145
+ },
1146
+ signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
1093
1147
  });
1094
1148
  if (!res.ok) {
1095
1149
  throw new ClaudeUsageError(`Claude usage request failed: HTTP ${res.status}`, "request_failed");
1096
1150
  }
1097
1151
  const body = await res.json();
1152
+ const { owner, ownerLookupError } = await getClaudeUsageOwner(credentials2.accessToken);
1098
1153
  return {
1099
1154
  fiveHour: toWindow(body.five_hour),
1100
- sevenDay: toWindow(body.seven_day)
1155
+ sevenDay: toWindow(body.seven_day),
1156
+ owner,
1157
+ ownerLookupError
1101
1158
  };
1102
1159
  }
1103
1160
 
@@ -1126,8 +1183,9 @@ async function claudeUsage() {
1126
1183
  }
1127
1184
 
1128
1185
  // src/commands/run.ts
1129
- import { homedir as homedir4 } from "os";
1130
- 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 writeFileSync5 } from "fs";
1187
+ import { homedir as homedir5 } from "os";
1188
+ import { isAbsolute as isAbsolute3, join as join9, parse, resolve as resolvePath2 } from "path";
1131
1189
  import chalk6 from "chalk";
1132
1190
 
1133
1191
  // ../../packages/types/src/agents/index.ts
@@ -1467,7 +1525,14 @@ function drainSessionDbRecoveryReport({
1467
1525
  skippedLines++;
1468
1526
  return [];
1469
1527
  }
1470
- 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
+ ];
1471
1536
  } catch (error2) {
1472
1537
  skippedLines++;
1473
1538
  console.error(
@@ -1492,12 +1557,39 @@ function buildSessionDbRecoveryActivity(record) {
1492
1557
  const level = record.severity === "warning" ? "warn" : record.severity === "error" ? "error" : null;
1493
1558
  if (!level) return null;
1494
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
+ };
1495
1587
  switch (record.outcome) {
1496
1588
  case "fresh_session_db":
1497
1589
  return {
1498
1590
  level,
1499
1591
  metadata: withoutContractFields(record),
1500
- message: `This runner started with a fresh session database. Earlier sessions are unavailable, and queued conversation or session references from before this start may no longer resolve.${noVerifiedPoint} Review the runner's backup configuration before relying on restored session history.`
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}`
1501
1593
  };
1502
1594
  case "restore_retried":
1503
1595
  return {
@@ -1535,7 +1627,19 @@ function buildSessionDbRecoveryActivity(record) {
1535
1627
  return {
1536
1628
  level,
1537
1629
  metadata: withoutContractFields(record),
1538
- 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}`
1631
+ };
1632
+ case "session_db_boot_refused":
1633
+ return {
1634
+ level,
1635
+ metadata: withoutContractFields(record),
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.`
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.`
1539
1643
  };
1540
1644
  default:
1541
1645
  return null;
@@ -1550,7 +1654,9 @@ var OUTCOMES = /* @__PURE__ */ new Set([
1550
1654
  "restore_retried",
1551
1655
  "fresh_session_db",
1552
1656
  "history_rolled_back",
1553
- "restore_misconfigured"
1657
+ "restore_misconfigured",
1658
+ "session_db_boot_refused",
1659
+ "schema_provenance_mismatch"
1554
1660
  ]);
1555
1661
  var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
1556
1662
  var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
@@ -1568,7 +1674,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
1568
1674
  function isSessionDbRecoveryRecord(value) {
1569
1675
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
1570
1676
  const record = value;
1571
- return record.v === 1 && record.event === "session_db_recovery" && typeof record.at === "string" && STAGES.has(record.stage) && OUTCOMES.has(record.outcome) && SEVERITIES.has(record.severity) && typeof record.reason === "string" && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
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(
1572
1678
  (field) => record[field] === null || typeof record[field] === "string"
1573
1679
  );
1574
1680
  }
@@ -1597,11 +1703,644 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
1597
1703
  if (health.healthy) {
1598
1704
  return health;
1599
1705
  }
1600
- await new Promise((resolve3) => setTimeout(resolve3, 1e3));
1706
+ await new Promise((resolve4) => setTimeout(resolve4, 1e3));
1601
1707
  }
1602
1708
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
1603
1709
  }
1604
1710
 
1711
+ // src/lib/opencode/session-db-boot.ts
1712
+ import { spawn as spawn2 } from "child_process";
1713
+ import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "fs";
1714
+ import { homedir as homedir2 } from "os";
1715
+ import { dirname as dirname2, resolve as resolvePath } from "path";
1716
+
1717
+ // src/lib/runner-synchroniser.ts
1718
+ import { spawn } from "child_process";
1719
+ function appendError(stderr, error2) {
1720
+ const message = error2 instanceof Error ? error2.message : String(error2);
1721
+ return stderr === "" ? message : `${stderr}
1722
+ ${message}`;
1723
+ }
1724
+ function runSynchroniser(args, opts) {
1725
+ return new Promise((resolve4) => {
1726
+ let child;
1727
+ let stdout = "";
1728
+ let stderr = "";
1729
+ let settled = false;
1730
+ const timer = {};
1731
+ const finish = (result) => {
1732
+ if (settled) return;
1733
+ settled = true;
1734
+ if (timer.handle) clearTimeout(timer.handle);
1735
+ resolve4(result);
1736
+ };
1737
+ try {
1738
+ child = spawn("runner-synchroniser", args, {
1739
+ env: opts.env ?? process.env,
1740
+ stdio: ["ignore", "pipe", "pipe"]
1741
+ });
1742
+ } catch (error2) {
1743
+ finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
1744
+ return;
1745
+ }
1746
+ child.stdout?.setEncoding("utf8");
1747
+ child.stdout?.on("data", (chunk) => {
1748
+ stdout += chunk;
1749
+ });
1750
+ child.stderr?.setEncoding("utf8");
1751
+ child.stderr?.on("data", (chunk) => {
1752
+ stderr += chunk;
1753
+ });
1754
+ child.once("error", (error2) => {
1755
+ finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
1756
+ });
1757
+ child.once("close", (code) => {
1758
+ finish({ code, stdout, stderr, timedOut: false });
1759
+ });
1760
+ timer.handle = setTimeout(
1761
+ () => {
1762
+ child.kill("SIGKILL");
1763
+ finish({ code: null, stdout, stderr, timedOut: true });
1764
+ },
1765
+ Math.max(0, opts.timeoutMs)
1766
+ );
1767
+ });
1768
+ }
1769
+
1770
+ // src/lib/opencode/session-db-boot.ts
1771
+ var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
1772
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
1773
+ var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
1774
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
1775
+ function commandError(result) {
1776
+ return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
1777
+ }
1778
+ function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
1779
+ options.reportRecovery({
1780
+ v: 1,
1781
+ event: "session_db_recovery",
1782
+ at: (/* @__PURE__ */ new Date()).toISOString(),
1783
+ stage,
1784
+ outcome,
1785
+ severity: "error",
1786
+ reason,
1787
+ litestream_exit_code: litestreamExitCode,
1788
+ attempt: null,
1789
+ replica_objects: null,
1790
+ replica_bytes: null,
1791
+ quarantine_destination: null,
1792
+ quarantined_objects: null,
1793
+ quarantine_failed_objects: null,
1794
+ quarantined_bytes: null,
1795
+ verified_restore_point: null,
1796
+ restore_points_tried: null,
1797
+ provenance_reason: null,
1798
+ provenance_migration_delta: null,
1799
+ replication_suspended: stage === "restore"
1800
+ });
1801
+ }
1802
+ function clearMarker(options) {
1803
+ if (!options.noReplicateMarker) return;
1804
+ try {
1805
+ unlinkSync2(options.noReplicateMarker);
1806
+ } catch (error2) {
1807
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
1808
+ options.log(
1809
+ `Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1810
+ "warn"
1811
+ );
1812
+ }
1813
+ }
1814
+ function markNoReplicate(options, message) {
1815
+ if (options.noReplicateMarker) {
1816
+ try {
1817
+ mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
1818
+ writeFileSync(options.noReplicateMarker, "");
1819
+ } catch (error2) {
1820
+ options.log(
1821
+ `Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1822
+ "error"
1823
+ );
1824
+ }
1825
+ }
1826
+ options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
1827
+ }
1828
+ function discardSessionDbDebris(options) {
1829
+ for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
1830
+ try {
1831
+ unlinkSync2(path);
1832
+ } catch (error2) {
1833
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
1834
+ options.log(
1835
+ `Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1836
+ "warn"
1837
+ );
1838
+ }
1839
+ }
1840
+ }
1841
+ function splitDiagnostics(text) {
1842
+ return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
1843
+ }
1844
+ function logSynchroniserDiagnostics(result, options) {
1845
+ for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
1846
+ }
1847
+ function parseSingleQuotedAssignment(line) {
1848
+ const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
1849
+ if (!match || !match[2].startsWith("'")) return null;
1850
+ const valueSource = match[2];
1851
+ let value = "";
1852
+ for (let index = 1; index < valueSource.length; index++) {
1853
+ const character = valueSource[index];
1854
+ if (character !== "'") {
1855
+ value += character;
1856
+ continue;
1857
+ }
1858
+ if (index === valueSource.length - 1) return [match[1], value];
1859
+ if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
1860
+ value += "'";
1861
+ index += 3;
1862
+ }
1863
+ return null;
1864
+ }
1865
+ function parseSynchroniserEnv(stdout) {
1866
+ const values = {};
1867
+ for (const line of stdout.split("\n")) {
1868
+ if (line.trim() === "") continue;
1869
+ const assignment = parseSingleQuotedAssignment(line);
1870
+ if (!assignment) return null;
1871
+ values[assignment[0]] = assignment[1];
1872
+ }
1873
+ return values;
1874
+ }
1875
+ function runCommand(command, args, options) {
1876
+ return new Promise((resolve4) => {
1877
+ let child;
1878
+ let stdout = "";
1879
+ let stderr = "";
1880
+ let settled = false;
1881
+ const finish = (result) => {
1882
+ if (settled) return;
1883
+ settled = true;
1884
+ if (timer) clearTimeout(timer);
1885
+ resolve4(result);
1886
+ };
1887
+ try {
1888
+ child = spawn2(command, args, {
1889
+ env: options.env,
1890
+ stdio: ["ignore", "pipe", "pipe"]
1891
+ });
1892
+ } catch (error2) {
1893
+ resolve4({
1894
+ code: null,
1895
+ stdout,
1896
+ stderr: error2 instanceof Error ? error2.message : String(error2),
1897
+ timedOut: false
1898
+ });
1899
+ return;
1900
+ }
1901
+ child.stdout?.setEncoding("utf8");
1902
+ child.stdout?.on("data", (chunk) => {
1903
+ stdout += chunk;
1904
+ });
1905
+ child.stderr?.setEncoding("utf8");
1906
+ child.stderr?.on("data", (chunk) => {
1907
+ stderr += chunk;
1908
+ });
1909
+ child.once("error", (error2) => {
1910
+ finish({
1911
+ code: null,
1912
+ stdout,
1913
+ stderr: stderr === "" ? error2.message : `${stderr}
1914
+ ${error2.message}`,
1915
+ timedOut: false
1916
+ });
1917
+ });
1918
+ child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
1919
+ const timer = setTimeout(
1920
+ () => {
1921
+ child.kill("SIGKILL");
1922
+ finish({ code: null, stdout, stderr, timedOut: true });
1923
+ },
1924
+ Math.max(0, options.timeoutMs)
1925
+ );
1926
+ });
1927
+ }
1928
+ async function ensureLitestreamConfig(options, env) {
1929
+ const configPath = options.litestreamConfig;
1930
+ if (!configPath) {
1931
+ markNoReplicate(options, "no Litestream configuration path was provided");
1932
+ reportRecord(
1933
+ "restore",
1934
+ "restore_misconfigured",
1935
+ "litestream_config_unavailable",
1936
+ null,
1937
+ options
1938
+ );
1939
+ return null;
1940
+ }
1941
+ try {
1942
+ if (statSync2(configPath).size > 0) return configPath;
1943
+ } catch (error2) {
1944
+ if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
1945
+ options.log(
1946
+ `Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1947
+ "warn"
1948
+ );
1949
+ }
1950
+ }
1951
+ const rendered = await runSynchroniser(["litestream-config"], {
1952
+ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
1953
+ env
1954
+ });
1955
+ logSynchroniserDiagnostics(rendered, options);
1956
+ if (rendered.timedOut || rendered.code !== 0) {
1957
+ options.log(
1958
+ `Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
1959
+ "error"
1960
+ );
1961
+ markNoReplicate(options, `could not generate ${configPath}`);
1962
+ reportRecord(
1963
+ "restore",
1964
+ "restore_misconfigured",
1965
+ "litestream_config_unavailable",
1966
+ null,
1967
+ options
1968
+ );
1969
+ return null;
1970
+ }
1971
+ try {
1972
+ mkdirSync(dirname2(configPath), { recursive: true });
1973
+ writeFileSync(configPath, rendered.stdout);
1974
+ } catch (error2) {
1975
+ options.log(
1976
+ `Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1977
+ "error"
1978
+ );
1979
+ markNoReplicate(options, `could not generate ${configPath}`);
1980
+ reportRecord(
1981
+ "restore",
1982
+ "restore_misconfigured",
1983
+ "litestream_config_unavailable",
1984
+ null,
1985
+ options
1986
+ );
1987
+ return null;
1988
+ }
1989
+ const version2 = await runCommand("litestream", ["version"], {
1990
+ env,
1991
+ timeoutMs: 1e4
1992
+ });
1993
+ const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
1994
+ const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
1995
+ options.log(
1996
+ `litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
1997
+ );
1998
+ return configPath;
1999
+ }
2000
+ function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
2001
+ discardSessionDbDebris(options);
2002
+ markNoReplicate(options, message);
2003
+ reportRecord("restore", outcome, reason, litestreamExitCode, options);
2004
+ }
2005
+ async function restoreSessionDb(options, configPath, env) {
2006
+ const restored = await runCommand(
2007
+ "litestream",
2008
+ ["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
2009
+ { env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
2010
+ );
2011
+ for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
2012
+ if (restored.timedOut || restored.code === 124 || restored.code === 137) {
2013
+ restoreGiveUp(
2014
+ options,
2015
+ "restore_deadline_exceeded",
2016
+ `SESSION-DB-RESTORE-TRUNCATED: litestream restore did not finish within ${SESSION_DB_RESTORE_TIMEOUT_MS}ms; opencode starts with a fresh session DB and nothing is replicated this boot`,
2017
+ restored.code ?? 124
2018
+ );
2019
+ return;
2020
+ }
2021
+ if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
2022
+ options.log(`litestream restore could not run (${commandError(restored)})`, "error");
2023
+ restoreGiveUp(
2024
+ options,
2025
+ "restore_tool_unusable",
2026
+ `restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
2027
+ restored.code
2028
+ );
2029
+ return;
2030
+ }
2031
+ const classified = await runSynchroniser(
2032
+ [
2033
+ "session-db-classify",
2034
+ String(restored.code ?? 1),
2035
+ "1",
2036
+ "--on-unusable-replica=leave",
2037
+ "--fresh-db-fallback"
2038
+ ],
2039
+ { timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
2040
+ );
2041
+ logSynchroniserDiagnostics(classified, options);
2042
+ const classifyCode = classified.code;
2043
+ switch (classifyCode) {
2044
+ case 0:
2045
+ return;
2046
+ case 31:
2047
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2048
+ options.log(
2049
+ "SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
2050
+ "warn"
2051
+ );
2052
+ return;
2053
+ case 32:
2054
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2055
+ discardSessionDbDebris(options);
2056
+ markNoReplicate(
2057
+ options,
2058
+ "session-db-classify asked for another restore attempt (32), but this boot has budget for only one; treating it as a give-up rather than retrying"
2059
+ );
2060
+ return;
2061
+ case 30:
2062
+ restoreGiveUp(
2063
+ options,
2064
+ "classification_fatal",
2065
+ "session-db-classify returned fatal (30); see the FATAL message above",
2066
+ restored.code,
2067
+ "restore_misconfigured"
2068
+ );
2069
+ return;
2070
+ default:
2071
+ restoreGiveUp(
2072
+ options,
2073
+ "classification_unrecognised",
2074
+ `session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
2075
+ restored.code
2076
+ );
2077
+ }
2078
+ }
2079
+ async function verifySessionDb(options, configPath, env) {
2080
+ if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
2081
+ const result = await runSynchroniser(["session-db-verify", configPath], {
2082
+ timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
2083
+ env: {
2084
+ ...env,
2085
+ // The synchroniser reads this value in SECONDS. Keep this at 120, not
2086
+ // 120_000, so the walkback gives up before the outer process bound.
2087
+ EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
2088
+ SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
2089
+ )
2090
+ }
2091
+ });
2092
+ logSynchroniserDiagnostics(result, options);
2093
+ if (result.timedOut || result.code === 124 || result.code === 137) {
2094
+ options.log(
2095
+ `SESSION-DB-VERIFY-TIMEOUT: verification did not finish within its ${SESSION_DB_VERIFY_TIMEOUT_MS}ms deadline; continuing with the restored opencode.db as-is, unverified`,
2096
+ "warn"
2097
+ );
2098
+ return false;
2099
+ }
2100
+ if (result.code === 34) {
2101
+ reportRecord(
2102
+ "verify",
2103
+ "session_db_boot_refused",
2104
+ result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
2105
+ null,
2106
+ options
2107
+ );
2108
+ return true;
2109
+ }
2110
+ if (result.code === 33) {
2111
+ options.log(
2112
+ "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
2113
+ "warn"
2114
+ );
2115
+ return false;
2116
+ }
2117
+ if (result.code !== 0) {
2118
+ options.log(
2119
+ `SESSION-DB-VERIFY-UNKNOWN: session-db-verify exited ${result.code ?? "null"}, which is none of its documented answers; continuing with the restored opencode.db as-is`,
2120
+ "warn"
2121
+ );
2122
+ }
2123
+ return false;
2124
+ }
2125
+ options.log(
2126
+ "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
2127
+ "debug"
2128
+ );
2129
+ return false;
2130
+ }
2131
+ function fileExists(path) {
2132
+ try {
2133
+ statSync2(path);
2134
+ return true;
2135
+ } catch (error2) {
2136
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
2137
+ return true;
2138
+ }
2139
+ }
2140
+ async function restoreAndVerifySessionDb(options) {
2141
+ const env = options.env ?? process.env;
2142
+ clearMarker(options);
2143
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2144
+ const synchroniserEnv = await runSynchroniser(["env"], {
2145
+ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
2146
+ env
2147
+ });
2148
+ logSynchroniserDiagnostics(synchroniserEnv, options);
2149
+ if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
2150
+ options.log(
2151
+ `runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
2152
+ "error"
2153
+ );
2154
+ markNoReplicate(
2155
+ options,
2156
+ "could not resolve the runner-synchroniser configuration (see the ERROR above)"
2157
+ );
2158
+ reportRecord(
2159
+ "restore",
2160
+ "restore_misconfigured",
2161
+ "synchroniser_config_unresolved",
2162
+ null,
2163
+ options
2164
+ );
2165
+ return { verifyFatal: false };
2166
+ }
2167
+ const values = parseSynchroniserEnv(synchroniserEnv.stdout);
2168
+ if (!values) {
2169
+ markNoReplicate(
2170
+ options,
2171
+ "the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
2172
+ );
2173
+ reportRecord(
2174
+ "restore",
2175
+ "restore_misconfigured",
2176
+ "synchroniser_config_unevaluable",
2177
+ null,
2178
+ options
2179
+ );
2180
+ return { verifyFatal: false };
2181
+ }
2182
+ const synchroniserDbPath = values.OPENCODE_DB_PATH;
2183
+ if (!synchroniserDbPath) {
2184
+ markNoReplicate(
2185
+ options,
2186
+ "run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
2187
+ );
2188
+ reportRecord(
2189
+ "restore",
2190
+ "restore_misconfigured",
2191
+ "synchroniser_config_incomplete",
2192
+ null,
2193
+ options
2194
+ );
2195
+ return { verifyFatal: false };
2196
+ }
2197
+ if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
2198
+ options.log(
2199
+ `runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
2200
+ "warn"
2201
+ );
2202
+ }
2203
+ if (!values.PERSISTENCE_BUCKET) {
2204
+ options.log(
2205
+ "SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
2206
+ "warn"
2207
+ );
2208
+ return { verifyFatal: false };
2209
+ }
2210
+ const configPath = await ensureLitestreamConfig(options, env);
2211
+ if (!configPath) return { verifyFatal: false };
2212
+ await restoreSessionDb(options, configPath, env);
2213
+ if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
2214
+ return { verifyFatal: false };
2215
+ }
2216
+ return { verifyFatal: await verifySessionDb(options, configPath, env) };
2217
+ }
2218
+
2219
+ // src/lib/opencode/session-db-provenance.ts
2220
+ import { createRequire } from "module";
2221
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
2222
+ import { dirname as dirname3, join as join3 } from "path";
2223
+ var require2 = createRequire(import.meta.url);
2224
+ function readSessionDbMigrationIds(dbPath) {
2225
+ let db;
2226
+ try {
2227
+ const { DatabaseSync } = require2("node:sqlite");
2228
+ db = new DatabaseSync(dbPath, { readOnly: true });
2229
+ const columns = db.prepare("PRAGMA table_info(migration)").all();
2230
+ const hasExpectedShape = columns.length === 2 && columns.some(
2231
+ (column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
2232
+ ) && columns.some(
2233
+ (column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
2234
+ );
2235
+ if (!hasExpectedShape) {
2236
+ console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
2237
+ return null;
2238
+ }
2239
+ const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
2240
+ if (rows.some((row) => typeof row.id !== "string")) return null;
2241
+ return rows.map((row) => row.id);
2242
+ } catch (error2) {
2243
+ console.warn(
2244
+ `[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2245
+ );
2246
+ return null;
2247
+ } finally {
2248
+ try {
2249
+ db?.close();
2250
+ } catch (error2) {
2251
+ console.warn(
2252
+ `[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2253
+ );
2254
+ }
2255
+ }
2256
+ }
2257
+ function sessionDbProvenanceStatePath(homeDir, env) {
2258
+ const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
2259
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
2260
+ }
2261
+ function loadSessionDbProvenanceState(path) {
2262
+ let value;
2263
+ try {
2264
+ value = JSON.parse(readFileSync3(path, "utf8"));
2265
+ } catch (error2) {
2266
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
2267
+ console.error(
2268
+ `[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2269
+ );
2270
+ return {};
2271
+ }
2272
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
2273
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2274
+ return {};
2275
+ }
2276
+ const state = {};
2277
+ for (const [dbPath, record] of Object.entries(value)) {
2278
+ if (!isSessionDbProvenanceRecord(record)) {
2279
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2280
+ return {};
2281
+ }
2282
+ state[dbPath] = record;
2283
+ }
2284
+ return state;
2285
+ }
2286
+ function saveSessionDbProvenanceState(path, state) {
2287
+ try {
2288
+ mkdirSync2(dirname3(path), { recursive: true });
2289
+ writeFileSync2(path, `${JSON.stringify(state, null, 2)}
2290
+ `, "utf8");
2291
+ } catch (error2) {
2292
+ console.error(
2293
+ `[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2294
+ );
2295
+ }
2296
+ }
2297
+ function evaluateSessionDbProvenance(input) {
2298
+ const { currentVersion, currentIds, previous } = input;
2299
+ if (!previous) return { anomaly: false, reason: null };
2300
+ const current = new Set(currentIds);
2301
+ const prior = new Set(previous.migrationIds);
2302
+ for (const id of prior) {
2303
+ if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
2304
+ }
2305
+ if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
2306
+ return { anomaly: true, reason: "foreign-version-migrations" };
2307
+ }
2308
+ return { anomaly: false, reason: null };
2309
+ }
2310
+ function checkSessionDbProvenance(input) {
2311
+ const { dbPath, currentVersion, homeDir, env } = input;
2312
+ const path = sessionDbProvenanceStatePath(homeDir, env);
2313
+ const state = loadSessionDbProvenanceState(path);
2314
+ const previous = state[dbPath];
2315
+ const currentIds = readSessionDbMigrationIds(dbPath);
2316
+ if (currentIds === null) {
2317
+ return {
2318
+ anomaly: false,
2319
+ reason: null,
2320
+ recordedVersion: previous?.opencodeVersion ?? null,
2321
+ migrationDelta: null
2322
+ };
2323
+ }
2324
+ const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
2325
+ const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
2326
+ state[dbPath] = {
2327
+ opencodeVersion: currentVersion,
2328
+ migrationIds: currentIds,
2329
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2330
+ };
2331
+ saveSessionDbProvenanceState(path, state);
2332
+ return {
2333
+ ...decision,
2334
+ recordedVersion: previous?.opencodeVersion ?? null,
2335
+ migrationDelta
2336
+ };
2337
+ }
2338
+ function isSessionDbProvenanceRecord(value) {
2339
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
2340
+ const record = value;
2341
+ return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
2342
+ }
2343
+
1605
2344
  // src/lib/opencode/opencode-version-gate.ts
1606
2345
  var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
1607
2346
  function isQueueValidatedVersion(version2) {
@@ -1616,7 +2355,63 @@ function buildOpenCodeVersionWarning(version2) {
1616
2355
  }
1617
2356
 
1618
2357
  // src/lib/opencode/process.ts
1619
- import { execSync, spawn } from "child_process";
2358
+ import { execSync, spawn as spawn3 } from "child_process";
2359
+
2360
+ // src/lib/process-stop.ts
2361
+ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
2362
+ if (!child.pid) {
2363
+ return { outcome: "not-running", code: child.exitCode, signal: child.signalCode };
2364
+ }
2365
+ if (child.exitCode !== null || child.signalCode !== null) {
2366
+ return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
2367
+ }
2368
+ return new Promise((resolve4, reject) => {
2369
+ let forced = false;
2370
+ let settled = false;
2371
+ const timer = setTimeout(() => {
2372
+ forced = true;
2373
+ try {
2374
+ sendKill();
2375
+ } catch (error2) {
2376
+ if (error2.code === "ESRCH") {
2377
+ finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
2378
+ } else {
2379
+ fail(error2);
2380
+ }
2381
+ }
2382
+ }, timeoutMs);
2383
+ const finish = (result) => {
2384
+ if (settled) return;
2385
+ settled = true;
2386
+ clearTimeout(timer);
2387
+ child.removeListener("exit", onExit);
2388
+ resolve4(result);
2389
+ };
2390
+ const fail = (error2) => {
2391
+ if (settled) return;
2392
+ settled = true;
2393
+ clearTimeout(timer);
2394
+ child.removeListener("exit", onExit);
2395
+ reject(error2);
2396
+ };
2397
+ const onExit = (code, signal) => {
2398
+ finish({ outcome: forced ? "killed" : "exited", code, signal });
2399
+ };
2400
+ child.once("exit", onExit);
2401
+ try {
2402
+ sendTerm();
2403
+ } catch (error2) {
2404
+ if (error2.code === "ESRCH") {
2405
+ finish({ outcome: "exited", code: child.exitCode, signal: child.signalCode });
2406
+ } else {
2407
+ fail(error2);
2408
+ }
2409
+ return;
2410
+ }
2411
+ });
2412
+ }
2413
+
2414
+ // src/lib/opencode/process.ts
1620
2415
  var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
1621
2416
  function getProcessCwd(pid) {
1622
2417
  const platform = process.platform;
@@ -1772,39 +2567,45 @@ async function findHealthyOpenCodeInstances() {
1772
2567
  }
1773
2568
  return healthy;
1774
2569
  }
1775
- async function startOpenCode(port) {
2570
+ async function startOpenCode(port, options = {}) {
1776
2571
  let command = "opencode";
1777
- let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1"];
2572
+ const printLogs = options.inheritStdio ? ["--print-logs"] : [];
2573
+ let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
1778
2574
  try {
1779
2575
  execSync("which opencode", { stdio: "ignore" });
1780
2576
  } catch {
1781
2577
  command = "npx";
1782
- args = ["opencode", "serve", "--port", port.toString(), "--hostname", "127.0.0.1"];
1783
- }
1784
- const child = spawn(command, args, {
2578
+ args = [
2579
+ "opencode",
2580
+ "serve",
2581
+ "--port",
2582
+ port.toString(),
2583
+ "--hostname",
2584
+ "127.0.0.1",
2585
+ ...printLogs
2586
+ ];
2587
+ }
2588
+ const child = spawn3(command, args, {
1785
2589
  detached: true,
1786
- stdio: "ignore",
2590
+ stdio: options.inheritStdio ? "inherit" : "ignore",
1787
2591
  cwd: process.cwd()
1788
2592
  });
1789
2593
  return child;
1790
2594
  }
1791
- function stopOpenCode(opencodeProcess) {
1792
- if (!opencodeProcess || !opencodeProcess.pid) {
1793
- return;
1794
- }
1795
- try {
2595
+ function stopOpenCodeAndWait(opencodeProcess, timeoutMs) {
2596
+ const sendSignal = (signal) => {
1796
2597
  if (process.platform === "win32") {
1797
- opencodeProcess.kill("SIGTERM");
2598
+ opencodeProcess.kill(signal);
1798
2599
  } else {
1799
- process.kill(-opencodeProcess.pid, "SIGTERM");
1800
- }
1801
- } catch (err) {
1802
- if (err.code !== "ESRCH") {
1803
- console.warn(
1804
- `stopOpenCode: kill failed: ${err instanceof Error ? err.message : String(err)}`
1805
- );
2600
+ process.kill(-opencodeProcess.pid, signal);
1806
2601
  }
1807
- }
2602
+ };
2603
+ return stopProcessAndWait(
2604
+ opencodeProcess,
2605
+ timeoutMs,
2606
+ () => sendSignal("SIGTERM"),
2607
+ () => sendSignal("SIGKILL")
2608
+ );
1808
2609
  }
1809
2610
 
1810
2611
  // src/lib/opencode/install.ts
@@ -2091,6 +2892,7 @@ async function createOpenCodeSession(port, directory) {
2091
2892
  return data.id;
2092
2893
  }
2093
2894
  async function getModelAttachmentCapability(port, model) {
2895
+ const { model: baseModel } = splitModelVariant(model);
2094
2896
  try {
2095
2897
  const res = await timedFetch(`${opencodeBase(port)}/config/providers`);
2096
2898
  if (!res.ok) {
@@ -2107,9 +2909,9 @@ async function getModelAttachmentCapability(port, model) {
2107
2909
  );
2108
2910
  return null;
2109
2911
  }
2110
- const slash = model ? model.indexOf("/") : -1;
2111
- const providerId = slash > 0 ? model.slice(0, slash) : void 0;
2112
- let modelId = slash > 0 ? model.slice(slash + 1) : void 0;
2912
+ const slash = baseModel ? baseModel.indexOf("/") : -1;
2913
+ const providerId = slash > 0 ? baseModel.slice(0, slash) : void 0;
2914
+ let modelId = slash > 0 ? baseModel.slice(slash + 1) : void 0;
2113
2915
  const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
2114
2916
  let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
2115
2917
  if (!provider && !providerId) {
@@ -2189,6 +2991,29 @@ async function buildFileParts(attachments, capable) {
2189
2991
  }
2190
2992
  return { parts, outcomes, capabilityUnknown };
2191
2993
  }
2994
+ function splitModelVariant(raw) {
2995
+ const value = raw?.trim();
2996
+ if (!value) return {};
2997
+ const hashIndex = value.indexOf("#");
2998
+ if (hashIndex === -1) return { model: value };
2999
+ const model = value.slice(0, hashIndex).trim() || void 0;
3000
+ const variant = value.slice(hashIndex + 1).trim() || void 0;
3001
+ return { model, variant };
3002
+ }
3003
+ function applyModelOptions(body, options) {
3004
+ if (options?.agent) body.agent = options.agent;
3005
+ const { model, variant } = splitModelVariant(options?.model);
3006
+ if (model) {
3007
+ const slashIndex = model.indexOf("/");
3008
+ if (slashIndex !== -1) {
3009
+ body.model = {
3010
+ providerID: model.substring(0, slashIndex),
3011
+ modelID: model.substring(slashIndex + 1)
3012
+ };
3013
+ }
3014
+ }
3015
+ if (variant) body.variant = variant;
3016
+ }
2192
3017
  function messageText(m) {
2193
3018
  if (!m || !Array.isArray(m.parts)) return "";
2194
3019
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
@@ -2213,18 +3038,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2213
3038
  const body = {
2214
3039
  parts
2215
3040
  };
2216
- if (options?.agent) {
2217
- body.agent = options.agent;
2218
- }
2219
- if (options?.model) {
2220
- const slashIndex = options.model.indexOf("/");
2221
- if (slashIndex !== -1) {
2222
- body.model = {
2223
- providerID: options.model.substring(0, slashIndex),
2224
- modelID: options.model.substring(slashIndex + 1)
2225
- };
2226
- }
2227
- }
3041
+ applyModelOptions(body, options);
2228
3042
  const res = await timedFetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
2229
3043
  method: "POST",
2230
3044
  headers: { "Content-Type": "application/json" },
@@ -2232,7 +3046,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2232
3046
  });
2233
3047
  if (res.status < 200 || res.status >= 300) {
2234
3048
  const text = await res.text().catch(() => "");
2235
- throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
3049
+ const { variant } = splitModelVariant(options?.model);
3050
+ throw new Error(
3051
+ `OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}${variant ? ` (variant: ${variant})` : ""}`
3052
+ );
2236
3053
  }
2237
3054
  const READ_BACK_ATTEMPTS = 5;
2238
3055
  const READ_BACK_DELAY_MS = 150;
@@ -2256,7 +3073,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2256
3073
  }
2257
3074
  }
2258
3075
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2259
- await new Promise((resolve3) => setTimeout(resolve3, READ_BACK_DELAY_MS));
3076
+ await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
2260
3077
  }
2261
3078
  }
2262
3079
  return null;
@@ -2387,7 +3204,7 @@ function isPreamblePinnedRunning(messages, userMessageId) {
2387
3204
  return completedOf(reply) != null && finishOf(reply) === "tool-calls";
2388
3205
  }
2389
3206
  function isB2AbandonmentConfirmed(params) {
2390
- return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false;
3207
+ return params.pinnedForMs >= params.minPinnedMs && params.descendantOngoing === false && params.rootOngoing === false;
2391
3208
  }
2392
3209
  function isAmbiguousTerminalFinish(m) {
2393
3210
  if (completedOf(m) == null) return false;
@@ -2400,7 +3217,7 @@ function isAmbiguousFinishPinnedRunning(messages, userMessageId) {
2400
3217
  return isAmbiguousTerminalFinish(reply);
2401
3218
  }
2402
3219
  function isAmbiguousFinishResolved(params) {
2403
- return params.sessionOngoing === false || params.pinnedForMs >= params.maxPinnedMs;
3220
+ return params.sessionOngoing === false || params.sessionOngoing !== true && params.pinnedForMs >= params.maxPinnedMs;
2404
3221
  }
2405
3222
  function messageError(messages, userMessageId) {
2406
3223
  const reply = findLastAssistantReplyFor(messages, userMessageId);
@@ -2609,13 +3426,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
2609
3426
  }
2610
3427
 
2611
3428
  // src/lib/opencode/session-db-size.ts
2612
- import { statSync as statSync2 } from "fs";
2613
- import { join as join3 } from "path";
3429
+ import { statSync as statSync3 } from "fs";
3430
+ import { join as join4 } from "path";
2614
3431
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
2615
3432
  function statSessionDbBytes(homeDir) {
2616
- const dbPath = join3(homeDir, ".local", "share", "opencode", "opencode.db");
3433
+ const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
2617
3434
  try {
2618
- return statSync2(dbPath).size;
3435
+ return statSync3(dbPath).size;
2619
3436
  } catch (err) {
2620
3437
  const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
2621
3438
  if (!isMissingFile) {
@@ -2641,11 +3458,11 @@ function buildSessionStoreSizeWarning(input) {
2641
3458
  }
2642
3459
 
2643
3460
  // src/lib/opencode/session-db-reclaim.ts
2644
- import { statSync as statSync3, statfsSync } from "fs";
2645
- import { dirname as dirname2 } from "path";
3461
+ import { statSync as statSync4, statfsSync } from "fs";
3462
+ import { dirname as dirname4 } from "path";
2646
3463
  function insufficientSpaceReason(dbPath, requiredBytes) {
2647
3464
  try {
2648
- const fsStats = statfsSync(dirname2(dbPath));
3465
+ const fsStats = statfsSync(dirname4(dbPath));
2649
3466
  const availableBytes = fsStats.bavail * fsStats.bsize;
2650
3467
  if (availableBytes < requiredBytes) {
2651
3468
  return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
@@ -2714,7 +3531,7 @@ async function reclaimSessionDbSpace(input) {
2714
3531
  );
2715
3532
  return { ok: false, skipped: "full-vacuum-blocked" };
2716
3533
  }
2717
- const fileBytesForGuard = statSync3(dbPath).size;
3534
+ const fileBytesForGuard = statSync4(dbPath).size;
2718
3535
  const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
2719
3536
  if (skipReason !== null) {
2720
3537
  console.warn(
@@ -2842,12 +3659,12 @@ var StreamForwarder = class {
2842
3659
  let endBody;
2843
3660
  if (has_body) {
2844
3661
  const chunks = [];
2845
- bodyPromise = new Promise((resolve3) => {
3662
+ bodyPromise = new Promise((resolve4) => {
2846
3663
  pushBody = (buf) => {
2847
3664
  chunks.push(buf);
2848
3665
  };
2849
3666
  endBody = () => {
2850
- resolve3(Buffer.concat(chunks));
3667
+ resolve4(Buffer.concat(chunks));
2851
3668
  };
2852
3669
  });
2853
3670
  }
@@ -2976,7 +3793,7 @@ function connectTunnel(options) {
2976
3793
  } = options;
2977
3794
  const tunnelUrl = getTunnelUrlConfig();
2978
3795
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
2979
- return new Promise((resolve3, reject) => {
3796
+ return new Promise((resolve4, reject) => {
2980
3797
  const ws = new WebSocket2(url, {
2981
3798
  headers: {
2982
3799
  Authorization: authHeader
@@ -3040,7 +3857,7 @@ function connectTunnel(options) {
3040
3857
  clearTimeout(connectionTimeout);
3041
3858
  const connectedAgentId = message.agent_id ?? agentId;
3042
3859
  onConnected?.(connectedAgentId);
3043
- resolve3({
3860
+ resolve4({
3044
3861
  ws,
3045
3862
  close: () => ws.close(1e3, "CLI shutdown")
3046
3863
  });
@@ -3171,10 +3988,10 @@ var RunnerConnection = class {
3171
3988
  };
3172
3989
 
3173
3990
  // src/lib/tunnel/ready-marker.ts
3174
- import { writeFileSync } from "fs";
3991
+ import { writeFileSync as writeFileSync3 } from "fs";
3175
3992
  function writeTunnelReadyMarker(path, agentId) {
3176
3993
  try {
3177
- writeFileSync(path, `${agentId}
3994
+ writeFileSync3(path, `${agentId}
3178
3995
  `);
3179
3996
  return { ok: true };
3180
3997
  } catch (error2) {
@@ -3182,10 +3999,52 @@ function writeTunnelReadyMarker(path, agentId) {
3182
3999
  }
3183
4000
  }
3184
4001
 
4002
+ // src/lib/replication.ts
4003
+ import { spawn as spawn4 } from "child_process";
4004
+ function startSessionDbReplication(configPath) {
4005
+ return spawn4("litestream", ["replicate", "-config", configPath], {
4006
+ stdio: "inherit"
4007
+ });
4008
+ }
4009
+ async function stopSessionDbReplication(child, timeoutMs) {
4010
+ return stopProcessAndWait(
4011
+ child,
4012
+ timeoutMs,
4013
+ () => child.kill("SIGTERM"),
4014
+ () => child.kill("SIGKILL")
4015
+ );
4016
+ }
4017
+
4018
+ // src/lib/process-liveness.ts
4019
+ import { readFileSync as readFileSync4 } from "fs";
4020
+ function isProcessAlive(pid) {
4021
+ try {
4022
+ process.kill(pid, 0);
4023
+ } catch (error2) {
4024
+ const code = error2.code;
4025
+ if (code === "ESRCH") return false;
4026
+ if (code === "EPERM") return true;
4027
+ console.error(
4028
+ `[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
4029
+ );
4030
+ return false;
4031
+ }
4032
+ if (process.platform !== "linux") return true;
4033
+ try {
4034
+ const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
4035
+ return !/^State:\s+Z(?:\s|$)/m.test(status2);
4036
+ } catch (error2) {
4037
+ console.error(
4038
+ `[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
4039
+ );
4040
+ return true;
4041
+ }
4042
+ }
4043
+
3185
4044
  // src/lib/openai-usage.ts
3186
- import { readFileSync as readFileSync3 } from "fs";
3187
- import { homedir as homedir2 } from "os";
3188
- import { join as join4 } from "path";
4045
+ import { readFileSync as readFileSync5 } from "fs";
4046
+ import { homedir as homedir3 } from "os";
4047
+ import { join as join5 } from "path";
3189
4048
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3190
4049
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3191
4050
  var OpenAiUsageError = class extends Error {
@@ -3199,7 +4058,7 @@ function isLocalCredentialProblem2(err) {
3199
4058
  }
3200
4059
  function readOpenCodeChatGptCredentials() {
3201
4060
  try {
3202
- const raw = readFileSync3(join4(homedir2(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4061
+ const raw = readFileSync5(join5(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3203
4062
  let parsed;
3204
4063
  try {
3205
4064
  parsed = JSON.parse(raw);
@@ -3572,15 +4431,15 @@ function createResourceUsageCollector(homeDir) {
3572
4431
  }
3573
4432
 
3574
4433
  // src/lib/channels/driver.ts
3575
- import { homedir as homedir3 } from "os";
4434
+ import { homedir as homedir4 } from "os";
3576
4435
 
3577
4436
  // src/lib/runner-file-sync.ts
3578
- import { join as join6 } from "path";
4437
+ import { join as join7 } from "path";
3579
4438
 
3580
4439
  // src/lib/file-push.ts
3581
4440
  import { randomUUID } from "crypto";
3582
4441
  import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
3583
- import { basename, dirname as dirname3, isAbsolute, join as join5, relative, resolve as resolve2, sep } from "path";
4442
+ import { basename, dirname as dirname5, isAbsolute, join as join6, relative, resolve as resolve2, sep } from "path";
3584
4443
  var FILE_MODE = 384;
3585
4444
  var DIRECTORY_MODE = 448;
3586
4445
  async function writePushedFile(request) {
@@ -3611,9 +4470,9 @@ async function writePushedFile(request) {
3611
4470
  }
3612
4471
  try {
3613
4472
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
3614
- dirname3(candidate)
4473
+ dirname5(candidate)
3615
4474
  );
3616
- const realTarget = join5(existingAncestor, ...missingSegments, basename(candidate));
4475
+ const realTarget = join6(existingAncestor, ...missingSegments, basename(candidate));
3617
4476
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
3618
4477
  if (allowedDirectory === null) {
3619
4478
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -3623,8 +4482,8 @@ async function writePushedFile(request) {
3623
4482
  }
3624
4483
  if (missingSegments.length > 0) {
3625
4484
  await createMissingDirectories(existingAncestor, missingSegments);
3626
- const realParent = await realpath(dirname3(realTarget));
3627
- if (realParent !== dirname3(realTarget) || !contains(allowedDirectory, realTarget)) {
4485
+ const realParent = await realpath(dirname5(realTarget));
4486
+ if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
3628
4487
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
3629
4488
  path: realTarget,
3630
4489
  bytes,
@@ -3649,7 +4508,7 @@ function expandAndValidate(requestedPath, homeDir) {
3649
4508
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
3650
4509
  return null;
3651
4510
  }
3652
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join5(homeDir, requestedPath.slice(2)) : requestedPath;
4511
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3653
4512
  if (expanded.split(/[/\\]/).includes("..")) {
3654
4513
  return null;
3655
4514
  }
@@ -3667,7 +4526,7 @@ async function resolveNearestExistingAncestor(directory) {
3667
4526
  try {
3668
4527
  return { existingAncestor: await realpath(current), missingSegments };
3669
4528
  } catch (err) {
3670
- const parent = dirname3(current);
4529
+ const parent = dirname5(current);
3671
4530
  if (err.code !== "ENOENT" || parent === current) {
3672
4531
  throw err;
3673
4532
  }
@@ -3722,13 +4581,13 @@ function contains(realDirectory, realTarget) {
3722
4581
  async function createMissingDirectories(existingAncestor, missingSegments) {
3723
4582
  let current = existingAncestor;
3724
4583
  for (const segment of missingSegments) {
3725
- current = join5(current, segment);
4584
+ current = join6(current, segment);
3726
4585
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
3727
4586
  await chmod(current, DIRECTORY_MODE);
3728
4587
  }
3729
4588
  }
3730
4589
  async function writeAtomically(realTarget, content) {
3731
- const temporaryPath = join5(dirname3(realTarget), `.evident-push-${randomUUID()}.tmp`);
4590
+ const temporaryPath = join6(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
3732
4591
  let handle;
3733
4592
  try {
3734
4593
  handle = await open2(temporaryPath, "wx", FILE_MODE);
@@ -3858,12 +4717,12 @@ var NOT_APPLIED = {
3858
4717
  opencodeAuthApplied: false
3859
4718
  };
3860
4719
  function isClaudeCredentialPath(requestedPath, homeDir) {
3861
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3862
- return expanded === join6(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
4720
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
4721
+ return expanded === join7(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
3863
4722
  }
3864
4723
  function isOpenCodeAuthPath(requestedPath, homeDir) {
3865
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3866
- return expanded === join6(homeDir, ...OPENCODE_AUTH_SEGMENTS);
4724
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
4725
+ return expanded === join7(homeDir, ...OPENCODE_AUTH_SEGMENTS);
3867
4726
  }
3868
4727
  async function applyOne(options, file) {
3869
4728
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -4409,7 +5268,7 @@ var ChannelDriver = class _ChannelDriver {
4409
5268
  this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
4410
5269
  this.now = config.now ?? (() => Date.now());
4411
5270
  this.fileSyncDirectories = config.fileSyncDirectories ?? [];
4412
- this.homeDir = config.homeDir ?? homedir3();
5271
+ this.homeDir = config.homeDir ?? homedir4();
4413
5272
  this.maxActiveSessions = config.maxActiveSessions;
4414
5273
  this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
4415
5274
  this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
@@ -5676,6 +6535,7 @@ var ChannelDriver = class _ChannelDriver {
5676
6535
  deliveryDeadlineAnchored: false,
5677
6536
  b2PinnedSinceMs: 0,
5678
6537
  b2LastDescendantCheckMs: 0,
6538
+ b2RootOngoingHeldLogged: false,
5679
6539
  b2AbandonedSignalled: false,
5680
6540
  ambiguousPinnedSinceMs: 0,
5681
6541
  ambiguousResolved: false
@@ -5770,6 +6630,7 @@ var ChannelDriver = class _ChannelDriver {
5770
6630
  deliveryDeadlineAnchored: false,
5771
6631
  b2PinnedSinceMs: 0,
5772
6632
  b2LastDescendantCheckMs: 0,
6633
+ b2RootOngoingHeldLogged: false,
5773
6634
  b2AbandonedSignalled: false,
5774
6635
  ambiguousPinnedSinceMs: 0,
5775
6636
  ambiguousResolved: false
@@ -6123,6 +6984,7 @@ var ChannelDriver = class _ChannelDriver {
6123
6984
  if (snapshotReadable) {
6124
6985
  inFlight.b2PinnedSinceMs = 0;
6125
6986
  inFlight.b2LastDescendantCheckMs = 0;
6987
+ inFlight.b2RootOngoingHeldLogged = false;
6126
6988
  inFlight.b2AbandonedSignalled = false;
6127
6989
  }
6128
6990
  } else {
@@ -6134,11 +6996,15 @@ var ChannelDriver = class _ChannelDriver {
6134
6996
  const pinnedForMs = this.now() - inFlight.b2PinnedSinceMs;
6135
6997
  if (pinnedForMs >= B2_ABANDONMENT_MIN_PINNED_MS && this.now() - inFlight.b2LastDescendantCheckMs >= B2_ABANDONMENT_RECHECK_MS) {
6136
6998
  inFlight.b2LastDescendantCheckMs = this.now();
6137
- const descendantOngoing = await this.isAnyDescendantSessionOngoing(sessionId);
6999
+ const [descendantOngoing, rootOngoing] = await Promise.all([
7000
+ this.isAnyDescendantSessionOngoing(sessionId),
7001
+ isSessionOngoing(this.port, sessionId)
7002
+ ]);
6138
7003
  if (isB2AbandonmentConfirmed({
6139
7004
  pinnedForMs,
6140
7005
  minPinnedMs: B2_ABANDONMENT_MIN_PINNED_MS,
6141
- descendantOngoing
7006
+ descendantOngoing,
7007
+ rootOngoing
6142
7008
  })) {
6143
7009
  inFlight.b2AbandonedSignalled = true;
6144
7010
  this.log({
@@ -6147,12 +7013,26 @@ var ChannelDriver = class _ChannelDriver {
6147
7013
  conversation_id: conv.id,
6148
7014
  message_id: id
6149
7015
  });
7016
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
6150
7017
  void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
6151
- watched_for_ms: pinnedForMs
7018
+ watched_for_ms: pinnedForMs,
7019
+ finish: reply?.info?.finish ?? reply?.finish,
7020
+ ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
7021
+ ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
7022
+ opencode_message_id: inFlight.opencodeMessageId
6152
7023
  });
6153
7024
  await this.settleMessageDone(sessionId, watcher, inFlight, messages);
6154
7025
  return;
6155
7026
  }
7027
+ if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
7028
+ inFlight.b2RootOngoingHeldLogged = true;
7029
+ this.log({
7030
+ level: "warn",
7031
+ 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`,
7032
+ conversation_id: conv.id,
7033
+ message_id: id
7034
+ });
7035
+ }
6156
7036
  }
6157
7037
  }
6158
7038
  const ambiguousPinnedNow = activelyRunning && isAmbiguousFinishPinnedRunning(messages, inFlight.opencodeMessageId);
@@ -7809,7 +8689,7 @@ async function ensureOpenCodeRunning(ctx) {
7809
8689
  }
7810
8690
  if (!ctx.interactive) {
7811
8691
  ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
7812
- const proc = await startOpenCode(ctx.port);
8692
+ const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
7813
8693
  const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
7814
8694
  if (!health.healthy) {
7815
8695
  return {
@@ -7877,7 +8757,7 @@ Port ${port} is already in use.`));
7877
8757
  }
7878
8758
  if (action === "start") {
7879
8759
  const spinner = ora2("Starting OpenCode...").start();
7880
- const proc = await startOpenCode(port);
8760
+ const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
7881
8761
  const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
7882
8762
  if (!health.healthy) {
7883
8763
  spinner.fail("Failed to start OpenCode");
@@ -7889,12 +8769,323 @@ Port ${port} is already in use.`));
7889
8769
  return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
7890
8770
  }
7891
8771
 
8772
+ // src/lib/runner-credentials.ts
8773
+ import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "fs";
8774
+ import { spawn as spawn5 } from "child_process";
8775
+ var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
8776
+ var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
8777
+ var GITHUB_PROBE_TIMEOUT_MS = 1e4;
8778
+ var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
8779
+ var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
8780
+ function commandError2(result) {
8781
+ return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
8782
+ }
8783
+ var runCommand2 = (command, args, opts) => {
8784
+ return new Promise((resolve4) => {
8785
+ let child;
8786
+ let stdout = "";
8787
+ let stderr = "";
8788
+ let settled = false;
8789
+ const timer = {};
8790
+ const finish = (result) => {
8791
+ if (settled) return;
8792
+ settled = true;
8793
+ if (timer.handle) clearTimeout(timer.handle);
8794
+ resolve4(result);
8795
+ };
8796
+ try {
8797
+ child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
8798
+ } catch (error2) {
8799
+ finish({
8800
+ code: null,
8801
+ stdout,
8802
+ stderr: error2 instanceof Error ? error2.message : String(error2),
8803
+ timedOut: false
8804
+ });
8805
+ return;
8806
+ }
8807
+ child.stdout?.setEncoding("utf8");
8808
+ child.stdout?.on("data", (chunk) => {
8809
+ stdout += chunk;
8810
+ });
8811
+ child.stderr?.setEncoding("utf8");
8812
+ child.stderr?.on("data", (chunk) => {
8813
+ stderr += chunk;
8814
+ });
8815
+ child.once("error", (error2) => {
8816
+ finish({
8817
+ code: null,
8818
+ stdout,
8819
+ stderr: stderr === "" ? error2.message : `${stderr}
8820
+ ${error2.message}`,
8821
+ timedOut: false
8822
+ });
8823
+ });
8824
+ child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
8825
+ timer.handle = setTimeout(
8826
+ () => {
8827
+ child.kill("SIGKILL");
8828
+ finish({ code: null, stdout, stderr, timedOut: true });
8829
+ },
8830
+ Math.max(0, opts.timeoutMs)
8831
+ );
8832
+ });
8833
+ };
8834
+ function isEnvironmentObject(value) {
8835
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8836
+ }
8837
+ function secretFailure(marker, detail, log3) {
8838
+ const message = `${marker}: ${detail}`;
8839
+ log3(message, "error");
8840
+ return new Error(message);
8841
+ }
8842
+ async function installRunnerSecret({
8843
+ env,
8844
+ log: log3,
8845
+ commandRunner
8846
+ }) {
8847
+ const arn = env.RUNNER_SECRET_ARN?.trim();
8848
+ if (!arn) {
8849
+ log3("runner secret is not configured; continuing without GitHub and MCP credentials");
8850
+ return false;
8851
+ }
8852
+ const result = await (commandRunner ?? runCommand2)(
8853
+ "aws",
8854
+ [
8855
+ "secretsmanager",
8856
+ "get-secret-value",
8857
+ "--secret-id",
8858
+ arn,
8859
+ "--query",
8860
+ "SecretString",
8861
+ "--output",
8862
+ "text"
8863
+ ],
8864
+ { env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
8865
+ );
8866
+ if (result.timedOut) {
8867
+ throw secretFailure(
8868
+ "CREDENTIAL-RESTORE-TIMEOUT",
8869
+ `runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
8870
+ log3
8871
+ );
8872
+ }
8873
+ if (result.code !== 0) {
8874
+ throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
8875
+ }
8876
+ let payload;
8877
+ try {
8878
+ payload = JSON.parse(result.stdout);
8879
+ } catch (error2) {
8880
+ log3(
8881
+ `RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
8882
+ "warn"
8883
+ );
8884
+ return false;
8885
+ }
8886
+ if (!isEnvironmentObject(payload)) {
8887
+ log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
8888
+ return false;
8889
+ }
8890
+ let populated = 0;
8891
+ let skipped = 0;
8892
+ let githubTokenPopulated = false;
8893
+ for (const [key, value] of Object.entries(payload)) {
8894
+ if (typeof value !== "string" || value.length === 0) continue;
8895
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
8896
+ log3(
8897
+ `RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
8898
+ "warn"
8899
+ );
8900
+ skipped += 1;
8901
+ continue;
8902
+ }
8903
+ env[key] = value;
8904
+ populated += 1;
8905
+ if (key === "GH_TOKEN") githubTokenPopulated = true;
8906
+ }
8907
+ if (populated === 0) {
8908
+ log3(
8909
+ "RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
8910
+ "warn"
8911
+ );
8912
+ } else {
8913
+ log3(
8914
+ `RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
8915
+ );
8916
+ }
8917
+ return githubTokenPopulated;
8918
+ }
8919
+ function restoreFailure(operation, result, log3) {
8920
+ const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
8921
+ log3(message, "error");
8922
+ return new Error(message);
8923
+ }
8924
+ async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
8925
+ const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
8926
+ if (result.timedOut) {
8927
+ log3(
8928
+ `CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
8929
+ "warn"
8930
+ );
8931
+ return result;
8932
+ }
8933
+ if (result.code !== 0) throw restoreFailure(operation, result, log3);
8934
+ return result;
8935
+ }
8936
+ async function restoreCredentialStores({
8937
+ env,
8938
+ log: log3,
8939
+ synchroniserRunner = runSynchroniser
8940
+ }) {
8941
+ await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
8942
+ await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
8943
+ const result = await synchroniserRunner(["model-auth-ready"], {
8944
+ timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
8945
+ });
8946
+ if (result.timedOut) {
8947
+ log3(
8948
+ `CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
8949
+ "warn"
8950
+ );
8951
+ return;
8952
+ }
8953
+ switch (result.code) {
8954
+ case 0:
8955
+ return;
8956
+ case 10:
8957
+ log3(
8958
+ `no model credentials under s3://${env.LITESTREAM_BUCKET ?? ""}/${env.LITESTREAM_PREFIX ?? ""}/ (neither claude/credentials.json nor opencode/auth.json yielded valid JSON) and neither ANTHROPIC_API_KEY nor OPENAI_API_KEY is set. This VM boots and connects; a turn that needs a model provider fails until one is connected. See 'Seeding a credential store' in infrastructure/evident-runner/MICROVM.md.`,
8959
+ "warn"
8960
+ );
8961
+ return;
8962
+ default:
8963
+ log3("could not determine whether this VM has model credentials", "warn");
8964
+ }
8965
+ }
8966
+ var GIT_CREDENTIAL_HELPER_CONTENT = [
8967
+ "#!/usr/bin/env bash",
8968
+ '[ "$1" = get ] || exit 0',
8969
+ "echo username=x-access-token",
8970
+ 'echo "password=${GH_TOKEN}"',
8971
+ ""
8972
+ ].join("\n");
8973
+ async function probeGitHubAccess({ env, log: log3 }) {
8974
+ const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
8975
+ env,
8976
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
8977
+ });
8978
+ if (auth.timedOut) {
8979
+ log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
8980
+ return;
8981
+ }
8982
+ if (auth.code !== 0) {
8983
+ log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
8984
+ return;
8985
+ }
8986
+ log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
8987
+ const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
8988
+ env,
8989
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
8990
+ });
8991
+ if (remote.code !== 0 || remote.timedOut) return;
8992
+ const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
8993
+ if (!repo) return;
8994
+ const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
8995
+ env,
8996
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
8997
+ });
8998
+ if (repository.timedOut) {
8999
+ log3(
9000
+ `GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
9001
+ "warn"
9002
+ );
9003
+ } else if (repository.code !== 0) {
9004
+ log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
9005
+ }
9006
+ }
9007
+ async function configureGitHubAccess({ env, log: log3 }) {
9008
+ if (!env.GH_TOKEN) {
9009
+ log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
9010
+ return;
9011
+ }
9012
+ try {
9013
+ env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
9014
+ writeFileSync4(GIT_CONFIG_GLOBAL, "");
9015
+ writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
9016
+ chmodSync2(GIT_CREDENTIAL_HELPER, 448);
9017
+ const config = [
9018
+ ["user.name", env.GIT_USER_NAME ?? "evident-bot"],
9019
+ ["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
9020
+ ["init.defaultBranch", "main"],
9021
+ ["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
9022
+ ];
9023
+ for (const [key, value] of config) {
9024
+ const result = await runCommand2("git", ["config", "--global", key, value], {
9025
+ env,
9026
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9027
+ });
9028
+ if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
9029
+ }
9030
+ } catch (error2) {
9031
+ log3(
9032
+ `GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
9033
+ "warn"
9034
+ );
9035
+ return;
9036
+ }
9037
+ void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
9038
+ log3(
9039
+ `GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
9040
+ "warn"
9041
+ );
9042
+ });
9043
+ }
9044
+
9045
+ // src/lib/opencode/config-overlay.ts
9046
+ import { execFileSync as execFileSync2 } from "child_process";
9047
+ import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
9048
+ import { isAbsolute as isAbsolute2, join as join8, resolve as resolve3 } from "path";
9049
+ function isFile(filePath) {
9050
+ return existsSync2(filePath) && statSync5(filePath).isFile();
9051
+ }
9052
+ function applyRunnerOpenCodeConfig({
9053
+ overlayPath,
9054
+ cwd = process.cwd(),
9055
+ log: log3
9056
+ }) {
9057
+ if (!overlayPath) {
9058
+ log3("runner OpenCode config is not configured; using the baked project config", "debug");
9059
+ return;
9060
+ }
9061
+ const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
9062
+ const target = isFile(join8(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
9063
+ if (!isFile(source)) {
9064
+ log3(
9065
+ `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
9066
+ "error"
9067
+ );
9068
+ return;
9069
+ }
9070
+ copyFileSync(source, join8(cwd, target));
9071
+ try {
9072
+ execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
9073
+ stdio: "ignore"
9074
+ });
9075
+ } catch (error2) {
9076
+ const detail = error2 instanceof Error ? error2.message : String(error2);
9077
+ log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
9078
+ }
9079
+ log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
9080
+ }
9081
+
7892
9082
  // src/commands/run.ts
7893
9083
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
7894
9084
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
7895
9085
  var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
7896
9086
  var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
7897
9087
  var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
9088
+ var CHILD_STOP_TIMEOUT_MS = 1e4;
7898
9089
  function resolveLogLevel(options) {
7899
9090
  const accepted = Object.keys(LOG_LEVELS);
7900
9091
  const validate = (value, source) => {
@@ -7925,11 +9116,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
7925
9116
  if (trimmed === "") {
7926
9117
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
7927
9118
  }
7928
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join7(homeDir, trimmed.slice(2)) : trimmed;
7929
- if (!isAbsolute2(expanded)) {
9119
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join9(homeDir, trimmed.slice(2)) : trimmed;
9120
+ if (!isAbsolute3(expanded)) {
7930
9121
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
7931
9122
  }
7932
- const normalized = resolvePath(expanded);
9123
+ const normalized = resolvePath2(expanded);
7933
9124
  if (parse(normalized).root === normalized) {
7934
9125
  throw new Error(
7935
9126
  `--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
@@ -8045,7 +9236,7 @@ function logActivity(state, entry) {
8045
9236
  }
8046
9237
  function reportSessionDbRecovery(state) {
8047
9238
  try {
8048
- const report = drainSessionDbRecoveryReport({ homeDir: homedir4(), env: process.env });
9239
+ const report = drainSessionDbRecoveryReport({ homeDir: homedir5(), env: process.env });
8049
9240
  for (const record of report.records) {
8050
9241
  const activity = buildSessionDbRecoveryActivity(record);
8051
9242
  if (!activity) throw new Error("could not map session-DB recovery record");
@@ -8063,6 +9254,16 @@ function reportSessionDbRecovery(state) {
8063
9254
  );
8064
9255
  }
8065
9256
  }
9257
+ function reportSessionDbRecoveryRecord(state, record) {
9258
+ const activity = buildSessionDbRecoveryActivity(record);
9259
+ if (!activity) throw new Error("could not map session-DB recovery record");
9260
+ logActivity(state, {
9261
+ type: activity.level === "error" ? "error" : "info",
9262
+ level: activity.level,
9263
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
9264
+ metadata: activity.metadata
9265
+ });
9266
+ }
8066
9267
  function displayStatus(state) {
8067
9268
  if (!state.interactive) return;
8068
9269
  const attempt = state.connection?.reconnectAttempt ?? 0;
@@ -8235,7 +9436,7 @@ async function driveChannels(state, driver) {
8235
9436
  }
8236
9437
  }
8237
9438
  }
8238
- await new Promise((resolve3) => setTimeout(resolve3, CHANNEL_POLL_INTERVAL_MS));
9439
+ await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
8239
9440
  const cycleMs = performance.now() - cycleStartedAtMs;
8240
9441
  if (idleThisCycle) idleMs += cycleMs;
8241
9442
  if (unreachableThisCycle) unreachableMs += cycleMs;
@@ -8258,7 +9459,43 @@ async function driveChannels(state, driver) {
8258
9459
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
8259
9460
  var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
8260
9461
  function sessionDbPath() {
8261
- return join7(homedir4(), ".local", "share", "opencode", "opencode.db");
9462
+ return join9(homedir5(), ".local", "share", "opencode", "opencode.db");
9463
+ }
9464
+ function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
9465
+ const record = {
9466
+ v: 1,
9467
+ event: "session_db_recovery",
9468
+ at: (/* @__PURE__ */ new Date()).toISOString(),
9469
+ stage: "verify",
9470
+ outcome: "schema_provenance_mismatch",
9471
+ severity: "error",
9472
+ reason: provenance.reason ?? "schema-provenance-mismatch",
9473
+ litestream_exit_code: null,
9474
+ attempt: null,
9475
+ replica_objects: null,
9476
+ replica_bytes: null,
9477
+ quarantine_destination: null,
9478
+ quarantined_objects: null,
9479
+ quarantine_failed_objects: null,
9480
+ quarantined_bytes: null,
9481
+ verified_restore_point: null,
9482
+ restore_points_tried: null,
9483
+ provenance_reason: provenance.reason,
9484
+ provenance_migration_delta: provenance.migrationDelta,
9485
+ replication_suspended: false,
9486
+ dbPath: sessionDbPath(),
9487
+ recorded_version: provenance.recordedVersion,
9488
+ current_version: currentVersion,
9489
+ provenance_pre_boot_migration_count: preBootMigrationCount
9490
+ };
9491
+ const activity = buildSessionDbRecoveryActivity(record);
9492
+ if (!activity) throw new Error("could not map session-DB provenance activity");
9493
+ logActivity(state, {
9494
+ type: activity.level === "error" ? "error" : "info",
9495
+ level: activity.level,
9496
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
9497
+ metadata: activity.metadata
9498
+ });
8262
9499
  }
8263
9500
  async function runSweep(state, driver, config) {
8264
9501
  const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
@@ -8305,7 +9542,7 @@ async function runSweep(state, driver, config) {
8305
9542
  const reclaimResult = await reclaimSessionDbSpace({
8306
9543
  dbPath: sessionDbPath(),
8307
9544
  maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
8308
- allowFullVacuum: protectedNow.size === 0
9545
+ allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
8309
9546
  });
8310
9547
  if (reclaimResult.ok) {
8311
9548
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
@@ -8341,7 +9578,7 @@ function scheduleSessionCleanup(state, driver, options) {
8341
9578
  for (const warning2 of config.warnings) {
8342
9579
  logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
8343
9580
  }
8344
- const dbBytes = statSessionDbBytes(homedir4());
9581
+ const dbBytes = statSessionDbBytes(homedir5());
8345
9582
  void (async () => {
8346
9583
  const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
8347
9584
  const sizeWarning = buildSessionStoreSizeWarning({
@@ -8500,7 +9737,17 @@ function scheduleClaudeUsageReporting(state, options) {
8500
9737
  setTimer: (timer) => {
8501
9738
  state.claudeUsageTimer = timer;
8502
9739
  },
8503
- fetchUsage: getClaudeUsage,
9740
+ fetchUsage: async () => {
9741
+ const usage = await getClaudeUsage();
9742
+ if (usage.ownerLookupError) {
9743
+ logActivity(state, {
9744
+ type: "info",
9745
+ level: "debug",
9746
+ message: `Claude usage owner lookup failed: ${usage.ownerLookupError}`
9747
+ });
9748
+ }
9749
+ return usage;
9750
+ },
8504
9751
  report: (usage) => reportClaudeUsage(state.agentId, state.authHeader, usage),
8505
9752
  isLocalCredentialProblem,
8506
9753
  forcedOnHint: "run `claude` to sign in",
@@ -8532,7 +9779,7 @@ function scheduleResourceUsageReporting(state, options) {
8532
9779
  });
8533
9780
  return;
8534
9781
  }
8535
- const collect = createResourceUsageCollector(homedir4());
9782
+ const collect = createResourceUsageCollector(homedir5());
8536
9783
  let consecutiveFailures = 0;
8537
9784
  const tick = async () => {
8538
9785
  try {
@@ -8672,15 +9919,31 @@ async function cleanup(state, opts = {}) {
8672
9919
  }
8673
9920
  if (state.opencodeProcess) {
8674
9921
  const opencodeProcess = state.opencodeProcess;
8675
- await timeShutdownPhase(state, durations, "opencode_stop", () => stopOpenCode(opencodeProcess));
9922
+ const result = await timeShutdownPhase(
9923
+ state,
9924
+ durations,
9925
+ "opencode_stop",
9926
+ () => stopOpenCodeAndWait(opencodeProcess, CHILD_STOP_TIMEOUT_MS)
9927
+ );
8676
9928
  if (state.interactive) {
8677
- logActivity(state, { type: "info", message: "Stopped OpenCode process" });
9929
+ logActivity(state, { type: "info", message: `Stopped OpenCode process (${result.outcome})` });
8678
9930
  displayStatus(state);
8679
9931
  } else {
8680
- log2(state, "Stopped OpenCode process");
9932
+ log2(state, `Stopped OpenCode process (${result.outcome})`);
8681
9933
  }
8682
9934
  state.opencodeProcess = null;
8683
9935
  }
9936
+ if (state.litestreamProcess) {
9937
+ const litestreamProcess = state.litestreamProcess;
9938
+ const result = await timeShutdownPhase(
9939
+ state,
9940
+ durations,
9941
+ "litestream_stop",
9942
+ () => stopSessionDbReplication(litestreamProcess, CHILD_STOP_TIMEOUT_MS)
9943
+ );
9944
+ log2(state, `Stopped litestream replication (${result.outcome})`);
9945
+ state.litestreamProcess = null;
9946
+ }
8684
9947
  return durations;
8685
9948
  }
8686
9949
  async function run(options) {
@@ -8689,7 +9952,12 @@ async function run(options) {
8689
9952
  let fileSyncDirectories;
8690
9953
  try {
8691
9954
  logLevel = resolveLogLevel(options);
8692
- fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir4());
9955
+ fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir5());
9956
+ if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
9957
+ throw new Error(
9958
+ "--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
9959
+ );
9960
+ }
8693
9961
  } catch (error2) {
8694
9962
  const message = error2 instanceof Error ? error2.message : String(error2);
8695
9963
  if (options.json) {
@@ -8713,7 +9981,9 @@ async function run(options) {
8713
9981
  connected: false,
8714
9982
  opencodeConnected: false,
8715
9983
  opencodeVersion: null,
9984
+ sessionDbProvenanceAnomaly: false,
8716
9985
  opencodeProcess: null,
9986
+ litestreamProcess: null,
8717
9987
  connection: null,
8718
9988
  channelDriver: null,
8719
9989
  running: true,
@@ -8780,8 +10050,8 @@ async function run(options) {
8780
10050
  return true;
8781
10051
  }
8782
10052
  );
8783
- const timedOut = new Promise((resolve3) => {
8784
- timer = setTimeout(() => resolve3(false), telemetryBudgetMs);
10053
+ const timedOut = new Promise((resolve4) => {
10054
+ timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
8785
10055
  });
8786
10056
  if (!await Promise.race([flushed, timedOut])) {
8787
10057
  log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
@@ -8921,7 +10191,67 @@ async function run(options) {
8921
10191
  } else {
8922
10192
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
8923
10193
  }
10194
+ if (options.restoreRunnerCredentials) {
10195
+ log2(state, "Restoring runner credentials before starting OpenCode");
10196
+ const credentialContext = {
10197
+ env: process.env,
10198
+ log: (message, level = "info") => {
10199
+ if (level === "error") {
10200
+ logActivity(state, { type: "error", error: message });
10201
+ } else {
10202
+ logActivity(state, { type: "info", level, message });
10203
+ }
10204
+ }
10205
+ };
10206
+ const githubTokenPopulated = await installRunnerSecret(credentialContext);
10207
+ await restoreCredentialStores(credentialContext);
10208
+ if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
10209
+ }
10210
+ let sessionDbVerifyFatal = false;
10211
+ if (!options.restoreSessionDb) {
10212
+ log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
10213
+ } else {
10214
+ const health = await checkOpenCodeHealth(state.port);
10215
+ if (health.healthy) {
10216
+ log2(
10217
+ state,
10218
+ "Skipping session-DB restore: OpenCode is already serving this database",
10219
+ "debug"
10220
+ );
10221
+ } else {
10222
+ const result = await restoreAndVerifySessionDb({
10223
+ dbPath: sessionDbPath(),
10224
+ litestreamConfig: options.litestreamConfig,
10225
+ noReplicateMarker: options.sessionDbNoReplicateMarker,
10226
+ env: process.env,
10227
+ log: (message, level = "info") => {
10228
+ if (level === "error") {
10229
+ logActivity(state, { type: "error", error: message });
10230
+ } else {
10231
+ logActivity(state, { type: "info", level, message });
10232
+ }
10233
+ },
10234
+ reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
10235
+ });
10236
+ sessionDbVerifyFatal = result.verifyFatal;
10237
+ }
10238
+ }
8924
10239
  reportSessionDbRecovery(state);
10240
+ if (sessionDbVerifyFatal) {
10241
+ throw new Error(
10242
+ "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
10243
+ );
10244
+ }
10245
+ applyRunnerOpenCodeConfig({
10246
+ overlayPath: options.opencodeConfigOverlay,
10247
+ log: (message, level = "info") => {
10248
+ if (level === "error") {
10249
+ logActivity(state, { type: "error", error: message });
10250
+ } else {
10251
+ logActivity(state, { type: "info", level, message });
10252
+ }
10253
+ }
10254
+ });
8925
10255
  const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
8926
10256
  for (const warning2 of opencodeStartTimeoutWarnings) {
8927
10257
  logActivity(state, { type: "info", level: "warn", message: warning2 });
@@ -8930,6 +10260,7 @@ async function run(options) {
8930
10260
  for (const warning2 of maxActiveSessionsWarnings) {
8931
10261
  logActivity(state, { type: "info", level: "warn", message: warning2 });
8932
10262
  }
10263
+ const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
8933
10264
  const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
8934
10265
  try {
8935
10266
  const oc = await ensureOpenCodeRunning({
@@ -8937,11 +10268,41 @@ async function run(options) {
8937
10268
  interactive: state.interactive,
8938
10269
  agentId: state.agentId,
8939
10270
  log: (message) => log2(state, message),
8940
- startTimeoutMs: opencodeStartTimeoutMs
10271
+ startTimeoutMs: opencodeStartTimeoutMs,
10272
+ inheritStdio: Boolean(options.opencodePidFile)
8941
10273
  });
8942
10274
  state.port = oc.port;
8943
- state.opencodeProcess = oc.process;
10275
+ state.opencodeProcess = options.opencodePidFile ? null : oc.process;
8944
10276
  state.opencodeVersion = oc.version;
10277
+ if (options.opencodePidFile && oc.process?.pid !== void 0) {
10278
+ try {
10279
+ writeFileSync5(options.opencodePidFile, `${oc.process.pid}
10280
+ `, { mode: 384 });
10281
+ chmodSync3(options.opencodePidFile, 384);
10282
+ } catch (error2) {
10283
+ logActivity(state, {
10284
+ type: "error",
10285
+ error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
10286
+ });
10287
+ }
10288
+ }
10289
+ if (state.opencodeVersion !== null) {
10290
+ const provenance = checkSessionDbProvenance({
10291
+ dbPath: sessionDbPath(),
10292
+ currentVersion: state.opencodeVersion,
10293
+ homeDir: homedir5(),
10294
+ env: process.env
10295
+ });
10296
+ if (provenance.anomaly) {
10297
+ state.sessionDbProvenanceAnomaly = true;
10298
+ logSessionDbProvenanceMismatch(
10299
+ state,
10300
+ provenance,
10301
+ state.opencodeVersion,
10302
+ preBootMigrationIds?.length ?? null
10303
+ );
10304
+ }
10305
+ }
8945
10306
  state.opencodeConnected = oc.notReadyReason === null;
8946
10307
  const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
8947
10308
  ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
@@ -8978,6 +10339,108 @@ async function run(options) {
8978
10339
  ocSpinner?.fail(error2.message);
8979
10340
  throw error2;
8980
10341
  }
10342
+ if (options.litestreamPidFile) {
10343
+ if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
10344
+ log2(
10345
+ state,
10346
+ `Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
10347
+ );
10348
+ } else if (!options.litestreamConfig) {
10349
+ logActivity(state, {
10350
+ type: "info",
10351
+ level: "warn",
10352
+ message: "Skipping Litestream replication because no configuration file was provided"
10353
+ });
10354
+ } else {
10355
+ let existingPid;
10356
+ if (existsSync3(options.litestreamPidFile)) {
10357
+ try {
10358
+ const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
10359
+ const parsedPid = Number(rawPid);
10360
+ if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
10361
+ existingPid = parsedPid;
10362
+ }
10363
+ } catch (error2) {
10364
+ logActivity(state, {
10365
+ type: "info",
10366
+ level: "warn",
10367
+ message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
10368
+ });
10369
+ }
10370
+ }
10371
+ if (existingPid !== void 0 && isProcessAlive(existingPid)) {
10372
+ log2(state, `Litestream replication is already running with pid ${existingPid}`);
10373
+ } else {
10374
+ const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
10375
+ state.litestreamProcess = null;
10376
+ let failureHandled = false;
10377
+ const reportImageOwnedReplicationFailure = (message) => {
10378
+ if (failureHandled || state.shuttingDown || !state.running) return;
10379
+ failureHandled = true;
10380
+ logActivity(state, { type: "error", error: message });
10381
+ if (state.interactive) displayStatus(state);
10382
+ };
10383
+ litestreamProcess.on("exit", (code, signal) => {
10384
+ reportImageOwnedReplicationFailure(
10385
+ `Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
10386
+ );
10387
+ });
10388
+ litestreamProcess.on("error", (error2) => {
10389
+ reportImageOwnedReplicationFailure(
10390
+ `Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
10391
+ );
10392
+ });
10393
+ try {
10394
+ if (litestreamProcess.pid !== void 0) {
10395
+ writeFileSync5(options.litestreamPidFile, `${litestreamProcess.pid}
10396
+ `, {
10397
+ mode: 384
10398
+ });
10399
+ chmodSync3(options.litestreamPidFile, 384);
10400
+ }
10401
+ } catch (error2) {
10402
+ logActivity(state, {
10403
+ type: "error",
10404
+ error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
10405
+ });
10406
+ }
10407
+ log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
10408
+ }
10409
+ }
10410
+ } else if (options.litestreamConfig) {
10411
+ const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
10412
+ state.litestreamProcess = litestreamProcess;
10413
+ let failureHandled = false;
10414
+ const failRunForReplication = (message) => {
10415
+ if (failureHandled || state.shuttingDown || !state.running) return;
10416
+ failureHandled = true;
10417
+ state.shuttingDown = true;
10418
+ logActivity(state, { type: "error", error: message });
10419
+ if (state.interactive) displayStatus(state);
10420
+ void (async () => {
10421
+ try {
10422
+ await cleanup(state);
10423
+ await shutdownTelemetry();
10424
+ } catch (error2) {
10425
+ console.error(
10426
+ `[run] replication failure cleanup failed: ${error2 instanceof Error ? error2.message : String(error2)}`
10427
+ );
10428
+ }
10429
+ process.exit(1);
10430
+ })();
10431
+ };
10432
+ litestreamProcess.on("exit", (code, signal) => {
10433
+ failRunForReplication(
10434
+ `Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
10435
+ );
10436
+ });
10437
+ litestreamProcess.on("error", (error2) => {
10438
+ failRunForReplication(
10439
+ `Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
10440
+ );
10441
+ });
10442
+ log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
10443
+ }
8981
10444
  const tunnelSpinner = interactive && !state.json ? ora3("Connecting tunnel...").start() : null;
8982
10445
  const channelDriver = new ChannelDriver({
8983
10446
  agentId: state.agentId,
@@ -8989,7 +10452,7 @@ async function run(options) {
8989
10452
  // #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
8990
10453
  // REJECTED with `file_sync_disabled` on the ack, not silently ignored.
8991
10454
  fileSyncDirectories,
8992
- homeDir: homedir4(),
10455
+ homeDir: homedir5(),
8993
10456
  maxActiveSessions,
8994
10457
  log: (entry) => (
8995
10458
  // Thread the driver's real level straight through so `debug`/`warn`
@@ -9181,7 +10644,7 @@ async function run(options) {
9181
10644
  }
9182
10645
 
9183
10646
  // src/index.ts
9184
- var { version } = createRequire(import.meta.url)("../package.json");
10647
+ var { version } = createRequire2(import.meta.url)("../package.json");
9185
10648
  var program = new Command();
9186
10649
  program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
9187
10650
  "--endpoint <url>",
@@ -9238,6 +10701,27 @@ program.command("run").description("Connect to Evident and process messages").op
9238
10701
  ).option(
9239
10702
  "--tunnel-ready-file <path>",
9240
10703
  "Path to write once the tunnel is connected (opt-in; unused unless an operator/image sets it)"
10704
+ ).option(
10705
+ "--litestream-config <path>",
10706
+ "Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
10707
+ ).option(
10708
+ "--opencode-pid-file <path>",
10709
+ "Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
10710
+ ).option(
10711
+ "--litestream-pid-file <path>",
10712
+ "Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
10713
+ ).option(
10714
+ "--session-db-no-replicate-marker <path>",
10715
+ "Read this marker as a gate before starting Litestream; the session-DB restore writes it when this boot's database is not safe to replicate."
10716
+ ).option(
10717
+ "--restore-session-db",
10718
+ "Restore and verify the OpenCode session database before starting OpenCode; requires --litestream-config for the replica and --session-db-no-replicate-marker for give-up records."
10719
+ ).option(
10720
+ "--restore-runner-credentials",
10721
+ "Restore the hosted runner secret and persisted credential stores before starting OpenCode."
10722
+ ).option(
10723
+ "--opencode-config-overlay <path>",
10724
+ "Apply this runner-provided OpenCode config before starting OpenCode."
9241
10725
  ).action(
9242
10726
  (options) => {
9243
10727
  run({
@@ -9269,7 +10753,14 @@ program.command("run").description("Connect to Evident and process messages").op
9269
10753
  // Raw values — expansion/validation is single-sourced in run.ts's
9270
10754
  // resolveFileSyncDirectories.
9271
10755
  enableFileSyncTo: options.enableFileSyncTo,
9272
- tunnelReadyFile: options.tunnelReadyFile
10756
+ tunnelReadyFile: options.tunnelReadyFile,
10757
+ litestreamConfig: options.litestreamConfig,
10758
+ opencodePidFile: options.opencodePidFile,
10759
+ litestreamPidFile: options.litestreamPidFile,
10760
+ sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
10761
+ restoreSessionDb: options.restoreSessionDb,
10762
+ restoreRunnerCredentials: options.restoreRunnerCredentials,
10763
+ opencodeConfigOverlay: options.opencodeConfigOverlay
9273
10764
  });
9274
10765
  }
9275
10766
  );