@evident-ai/cli 3.5.2-dev.afae02b → 3.5.2-dev.bbda545

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
@@ -315,14 +315,14 @@ function blank() {
315
315
  console.log();
316
316
  }
317
317
  function waitForEnter(prompt = "Press Enter to continue...") {
318
- return new Promise((resolve4) => {
318
+ return new Promise((resolve5) => {
319
319
  process.stdout.write(chalk.dim(prompt));
320
320
  const handler = () => {
321
321
  process.stdin.removeListener("data", handler);
322
322
  process.stdin.setRawMode?.(false);
323
323
  process.stdin.pause();
324
324
  console.log();
325
- resolve4();
325
+ resolve5();
326
326
  };
327
327
  if (process.stdin.isTTY) {
328
328
  process.stdin.setRawMode?.(true);
@@ -332,7 +332,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
332
332
  });
333
333
  }
334
334
  function sleep(ms) {
335
- return new Promise((resolve4) => setTimeout(resolve4, ms));
335
+ return new Promise((resolve5) => setTimeout(resolve5, ms));
336
336
  }
337
337
 
338
338
  // src/commands/login.ts
@@ -410,19 +410,19 @@ async function tokenLogin() {
410
410
  );
411
411
  blank();
412
412
  process.stdout.write("Paste token: ");
413
- const token = await new Promise((resolve4) => {
413
+ const token = await new Promise((resolve5) => {
414
414
  let data = "";
415
415
  process.stdin.setEncoding("utf8");
416
416
  process.stdin.on("data", (chunk) => {
417
417
  data += chunk;
418
418
  });
419
419
  process.stdin.on("end", () => {
420
- resolve4(data.trim());
420
+ resolve5(data.trim());
421
421
  });
422
422
  if (process.stdin.isTTY) {
423
423
  process.stdin.once("data", (chunk) => {
424
424
  process.stdin.pause();
425
- resolve4(chunk.toString().trim());
425
+ resolve5(chunk.toString().trim());
426
426
  });
427
427
  process.stdin.resume();
428
428
  }
@@ -703,6 +703,31 @@ async function reportResourceUsage(agentId, authHeader, usage) {
703
703
  opencode_db_bytes: usage.opencodeDbBytes
704
704
  });
705
705
  }
706
+ async function getRunnerCredentialVault(agentId, authHeader) {
707
+ const empty = { prefix: null, vaultId: null, source: null };
708
+ try {
709
+ const apiUrl = getApiUrlConfig();
710
+ const response = await fetch(`${apiUrl}/runners/${agentId}/credential-vault`, {
711
+ headers: { Authorization: authHeader },
712
+ signal: AbortSignal.timeout(1e4)
713
+ });
714
+ if (!response.ok) {
715
+ const serverMessage = await readErrorMessage(response);
716
+ return {
717
+ ...empty,
718
+ error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
719
+ };
720
+ }
721
+ const data = await response.json();
722
+ return {
723
+ prefix: data.credential_vault_prefix,
724
+ vaultId: data.vault_id,
725
+ source: data.source
726
+ };
727
+ } catch (error2) {
728
+ return { ...empty, error: describeTimeoutError(error2, 1e4) };
729
+ }
730
+ }
706
731
  async function getAgentInfo(agentId, authHeader) {
707
732
  const apiUrl = getApiUrlConfig();
708
733
  try {
@@ -877,6 +902,76 @@ async function status(options = {}) {
877
902
  process.exit(result.exitCode);
878
903
  }
879
904
 
905
+ // src/commands/credential-vault-env.ts
906
+ function shellQuote(value) {
907
+ return `'${value.replaceAll("'", "'\\''")}'`;
908
+ }
909
+ function printAssignment(prefix) {
910
+ console.log(`CREDENTIAL_VAULT_PREFIX=${shellQuote(prefix ?? "")}`);
911
+ }
912
+ function safeReason(reason, credentials2, authHeader) {
913
+ let safe = reason;
914
+ for (const secret of [
915
+ authHeader,
916
+ credentials2?.token,
917
+ process.env.EVIDENT_RUNNER_KEY,
918
+ process.env.EVIDENT_AGENT_KEY,
919
+ process.env.EVIDENT_TOKEN
920
+ ]) {
921
+ if (secret) safe = safe.replaceAll(secret, "[redacted]");
922
+ }
923
+ return safe.replace(/[\r\n]+/g, " ");
924
+ }
925
+ function printFallback(reason, credentials2, authHeader) {
926
+ printAssignment(null);
927
+ console.error(
928
+ `WARNING: ${safeReason(reason, credentials2, authHeader)}; using this runner's own credential prefix.`
929
+ );
930
+ }
931
+ async function credentialVaultEnv() {
932
+ let credentials2 = null;
933
+ let authHeader;
934
+ try {
935
+ credentials2 = await getAuthCredentials();
936
+ if (!credentials2) {
937
+ printFallback("no runner credentials are configured");
938
+ return;
939
+ }
940
+ authHeader = getAuthHeader(credentials2);
941
+ const runner = await resolveAgentIdFromKey(authHeader);
942
+ if (!runner.agent_id) {
943
+ printFallback(
944
+ runner.error ?? "the runner identity could not be resolved",
945
+ credentials2,
946
+ authHeader
947
+ );
948
+ return;
949
+ }
950
+ const lookup = await getRunnerCredentialVault(runner.agent_id, authHeader);
951
+ if (lookup.error) {
952
+ printFallback(`credential vault lookup failed: ${lookup.error}`, credentials2, authHeader);
953
+ return;
954
+ }
955
+ if (lookup.prefix === null) {
956
+ printFallback("this runner is not linked to a credential vault", credentials2, authHeader);
957
+ return;
958
+ }
959
+ if (/[\r\n]/.test(lookup.prefix)) {
960
+ printFallback("credential vault lookup returned an invalid prefix", credentials2, authHeader);
961
+ return;
962
+ }
963
+ printAssignment(lookup.prefix);
964
+ console.error(
965
+ `VAULT-MODE: credential stores use vault=${lookup.vaultId} source=${lookup.source} prefix=${lookup.prefix}.`
966
+ );
967
+ } catch (error2) {
968
+ const detail = credentials2 === null ? "credential resolution failed" : `credential vault lookup could not be completed: ${error2 instanceof Error ? error2.message : String(error2)}`;
969
+ printFallback(detail, credentials2, authHeader);
970
+ } finally {
971
+ process.exitCode = 0;
972
+ }
973
+ }
974
+
880
975
  // src/lib/claude-usage.ts
881
976
  import { execFileSync } from "node:child_process";
882
977
  import { readFileSync } from "node:fs";
@@ -1063,7 +1158,7 @@ async function claudeUsage() {
1063
1158
  // src/commands/run.ts
1064
1159
  import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync7, writeFileSync as writeFileSync6 } from "node:fs";
1065
1160
  import { homedir as homedir7 } from "node:os";
1066
- import { isAbsolute as isAbsolute3, join as join11, parse, resolve as resolvePath2 } from "node:path";
1161
+ import { isAbsolute as isAbsolute4, join as join12, parse, resolve as resolvePath2 } from "node:path";
1067
1162
  import chalk7 from "chalk";
1068
1163
 
1069
1164
  // ../../packages/types/src/agents/index.ts
@@ -1198,7 +1293,11 @@ var FORWARD_FAILURE_REASON_HEADER = "X-Evident-Failure-Reason";
1198
1293
  function log(level, event, fields) {
1199
1294
  const method = level === "debug" ? "log" : level;
1200
1295
  try {
1201
- console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
1296
+ const payload = Object.fromEntries(
1297
+ Object.entries({ level, event, ...fields }).filter(([, value]) => value !== void 0)
1298
+ );
1299
+ const secondArgument = globalThis.navigator?.userAgent === "Cloudflare-Workers" ? payload : JSON.stringify(payload);
1300
+ console[method]("[evident]", secondArgument);
1202
1301
  } catch (err) {
1203
1302
  console.error(
1204
1303
  "[evident] log_serialize_failed",
@@ -1227,7 +1326,7 @@ import ora4 from "ora";
1227
1326
  import { select as select4 } from "@inquirer/prompts";
1228
1327
 
1229
1328
  // src/lib/telemetry.ts
1230
- var CLI_VERSION = (true ? "3.5.2-dev.afae02b" : void 0) ?? process.env.npm_package_version ?? "unknown";
1329
+ var CLI_VERSION = (true ? "3.5.2-dev.bbda545" : void 0) ?? process.env.npm_package_version ?? "unknown";
1231
1330
  function getCliVersion() {
1232
1331
  return CLI_VERSION;
1233
1332
  }
@@ -1388,20 +1487,47 @@ var EventTypes = {
1388
1487
  DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
1389
1488
  };
1390
1489
 
1391
- // src/lib/runner-activity-telemetry.ts
1392
- import { homedir as homedir2 } from "node:os";
1393
-
1394
1490
  // src/lib/opencode/session-db-reclaim.ts
1395
1491
  import { statSync as statSync2, statfsSync } from "node:fs";
1396
1492
  import { dirname as dirname2 } from "node:path";
1397
- var SESSION_DB_RECLAIM_CHUNK_PAGES = 2e3;
1493
+
1494
+ // src/lib/diagnostic-redaction.ts
1495
+ import { homedir as homedir2 } from "node:os";
1496
+ function redactHomeDirectory(message) {
1497
+ const home = homedir2();
1498
+ if (!home || home === "/") {
1499
+ return message;
1500
+ }
1501
+ const pathSegmentCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-";
1502
+ let result = "";
1503
+ let searchFrom = 0;
1504
+ let homeIndex;
1505
+ while ((homeIndex = message.indexOf(home, searchFrom)) !== -1) {
1506
+ const homeEnd = homeIndex + home.length;
1507
+ const previousCharacter = message[homeIndex - 1];
1508
+ const nextCharacter = message[homeEnd];
1509
+ const atPathBoundary = (!previousCharacter || !pathSegmentCharacters.includes(previousCharacter)) && (!nextCharacter || !pathSegmentCharacters.includes(nextCharacter));
1510
+ result += message.slice(searchFrom, homeIndex);
1511
+ result += atPathBoundary ? "~" : home;
1512
+ searchFrom = homeEnd;
1513
+ }
1514
+ return result + message.slice(searchFrom);
1515
+ }
1516
+ function redactDiagnosticText(message) {
1517
+ const redacted = message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/(?<![A-Za-z0-9_-])sk-ant-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-ant-***").replace(/(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-proj-***").replace(/(?<![A-Za-z0-9_-])sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "sk-***").replace(/(?<![A-Za-z0-9_-])(gh[oprsu])_[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "$1_***").replace(/(?<![A-Za-z0-9_-])github_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_-])/g, "github_pat_***").replace(/https?:\/\/\S+/g, "<url>");
1518
+ return redactHomeDirectory(redacted);
1519
+ }
1520
+
1521
+ // src/lib/opencode/session-db-reclaim.ts
1522
+ var SESSION_DB_RECLAIM_CHUNK_PAGES = 100;
1398
1523
  var SESSION_DB_RECLAIM_TIME_BUDGET_MS = 500;
1399
1524
  var SESSION_DB_RECLAIM_PAGE_CEILING = 4e4;
1525
+ var SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS = 5e3;
1400
1526
  function errorMessage(error2) {
1401
1527
  if (!(error2 instanceof Error)) return String(error2);
1402
1528
  return error2.cause instanceof Error ? error2.cause.message : error2.message;
1403
1529
  }
1404
- function isSessionStoreWriteFailure(error2) {
1530
+ function errorMessages(error2) {
1405
1531
  const messages = [];
1406
1532
  const visited = /* @__PURE__ */ new Set();
1407
1533
  let current = error2;
@@ -1425,12 +1551,81 @@ function isSessionStoreWriteFailure(error2) {
1425
1551
  }
1426
1552
  break;
1427
1553
  }
1428
- return messages.some(
1554
+ return messages;
1555
+ }
1556
+ function isSessionStoreWriteFailure(error2) {
1557
+ return errorMessages(error2).some(
1429
1558
  (message) => /(?:database or disk is full|disk is full|no space left on device|SQLITE_(?:FULL|IOERR)(?:\b|_))/i.test(
1430
1559
  message
1431
1560
  )
1432
1561
  );
1433
1562
  }
1563
+ var SQLITE_CAUSE_TOKEN = /(?:^|cause:\s+)(SQLiteError:\s+[A-Za-z][A-Za-z0-9]*(?:\s+[A-Za-z][A-Za-z0-9]*){0,7})(?=\s*(?:[)\]"',;]|$))/i;
1564
+ var EFFECT_SQL_CAUSE_TOKEN = /(?:^|cause:\s+)(effect\/sql\/SqlError\/[A-Za-z][A-Za-z0-9_]*)/i;
1565
+ var PARAMS_FIELD = /\bparams\s*(?:=|:)/i;
1566
+ function isCauseBearingText(text) {
1567
+ return /^(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text.trim()) || /(?:^|\s)cause:\s+(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text);
1568
+ }
1569
+ function structuredLogFields(line) {
1570
+ const fields = [];
1571
+ for (let index = 0; index < line.length; index++) {
1572
+ if (line[index] === '"') {
1573
+ index++;
1574
+ while (index < line.length) {
1575
+ if (line[index] === "\\") {
1576
+ index++;
1577
+ } else if (line[index] === '"') {
1578
+ break;
1579
+ }
1580
+ index++;
1581
+ }
1582
+ continue;
1583
+ }
1584
+ const atFieldBoundary = index === 0 || /\s/.test(line[index - 1]);
1585
+ const key = line.startsWith("error=", index) ? "error=" : line.startsWith("cause=", index) ? "cause=" : null;
1586
+ if (!atFieldBoundary || key === null) continue;
1587
+ const valueStart = index + key.length;
1588
+ if (line[valueStart] !== '"') {
1589
+ const valueEnd2 = line.indexOf(" ", valueStart);
1590
+ fields.push(line.slice(valueStart, valueEnd2 === -1 ? line.length : valueEnd2));
1591
+ index = valueEnd2 === -1 ? line.length : valueEnd2;
1592
+ continue;
1593
+ }
1594
+ let valueEnd = valueStart + 1;
1595
+ for (; valueEnd < line.length; valueEnd++) {
1596
+ if (line[valueEnd] === "\\") {
1597
+ valueEnd++;
1598
+ } else if (line[valueEnd] === '"') {
1599
+ break;
1600
+ }
1601
+ }
1602
+ fields.push(line.slice(valueStart + 1, valueEnd));
1603
+ index = valueEnd;
1604
+ }
1605
+ return fields;
1606
+ }
1607
+ function structuredCauseMessages(text) {
1608
+ const fields = structuredLogFields(text);
1609
+ if (fields.length > 0) return fields;
1610
+ return isCauseBearingText(text) ? [text] : [];
1611
+ }
1612
+ function causeTokenFromText(text) {
1613
+ const paramsIndex = text.search(PARAMS_FIELD);
1614
+ for (const pattern of [SQLITE_CAUSE_TOKEN, EFFECT_SQL_CAUSE_TOKEN]) {
1615
+ const match = pattern.exec(text);
1616
+ if (match?.[1] && (paramsIndex === -1 || match.index < paramsIndex)) {
1617
+ return match[1];
1618
+ }
1619
+ }
1620
+ return null;
1621
+ }
1622
+ function sessionStoreCauseToken(value) {
1623
+ for (const message of structuredCauseMessages(value)) {
1624
+ const token = causeTokenFromText(message);
1625
+ if (token !== null) return redactDiagnosticText(token).slice(0, 120);
1626
+ }
1627
+ return null;
1628
+ }
1434
1629
  function insufficientSpaceReason(dbPath, requiredBytes) {
1435
1630
  try {
1436
1631
  const fsStats = statfsSync(dirname2(dbPath));
@@ -1452,6 +1647,7 @@ function readFreelistCount(db) {
1452
1647
  return db.prepare("PRAGMA freelist_count").get().freelist_count;
1453
1648
  }
1454
1649
  function readCheckpointResult(db) {
1650
+ db.exec("PRAGMA busy_timeout=0");
1455
1651
  const row = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
1456
1652
  return { busy: row.busy !== 0, log: row.log, checkpointed: row.checkpointed };
1457
1653
  }
@@ -1469,7 +1665,7 @@ async function probeReclaimAvailability(input) {
1469
1665
  try {
1470
1666
  const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
1471
1667
  try {
1472
- db.exec("PRAGMA busy_timeout=5000");
1668
+ db.exec(`PRAGMA busy_timeout=${SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS}`);
1473
1669
  autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1474
1670
  } finally {
1475
1671
  db.close();
@@ -1489,7 +1685,8 @@ async function reclaimSessionDbSpace(input) {
1489
1685
  timeBudgetMs = SESSION_DB_RECLAIM_TIME_BUDGET_MS,
1490
1686
  chunkPages = SESSION_DB_RECLAIM_CHUNK_PAGES,
1491
1687
  now = () => performance.now(),
1492
- allowFullVacuum = true
1688
+ hasLiveTurn = false,
1689
+ hasProvenanceAnomaly = false
1493
1690
  } = input;
1494
1691
  let sqlite;
1495
1692
  try {
@@ -1505,17 +1702,19 @@ async function reclaimSessionDbSpace(input) {
1505
1702
  let db;
1506
1703
  try {
1507
1704
  db = new DatabaseSync(dbPath);
1508
- db.exec("PRAGMA busy_timeout=5000");
1705
+ db.exec(`PRAGMA busy_timeout=${SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS}`);
1509
1706
  const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1510
1707
  if (autoVacuum === 0) {
1511
- if (!allowFullVacuum) {
1512
- console.warn(
1513
- `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: a session turn is live`
1514
- );
1708
+ if (hasLiveTurn || hasProvenanceAnomaly) {
1709
+ const detail = [
1710
+ ...hasLiveTurn ? ["a session turn is live"] : [],
1711
+ ...hasProvenanceAnomaly ? ["session-DB provenance is anomalous"] : []
1712
+ ].join(" and ");
1713
+ console.warn(`[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: ${detail}`);
1515
1714
  return {
1516
1715
  ok: false,
1517
1716
  skipped: "full-vacuum-blocked",
1518
- detail: "a session turn is live"
1717
+ detail
1519
1718
  };
1520
1719
  }
1521
1720
  const fileBytesForGuard = statSync2(dbPath).size;
@@ -1533,7 +1732,9 @@ async function reclaimSessionDbSpace(input) {
1533
1732
  db.exec("VACUUM");
1534
1733
  const afterBytes = readLogicalBytes(db);
1535
1734
  const freelistAfter = readFreelistCount(db);
1735
+ const checkpointStartedAt = now();
1536
1736
  const checkpoint = readCheckpointResult(db);
1737
+ const checkpointElapsedMs = Math.max(0, now() - checkpointStartedAt);
1537
1738
  return {
1538
1739
  ok: true,
1539
1740
  mode: "convert",
@@ -1544,10 +1745,18 @@ async function reclaimSessionDbSpace(input) {
1544
1745
  pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1545
1746
  stoppedBy: "freelist-drained",
1546
1747
  elapsedMs: Math.max(0, now() - startedAt),
1748
+ checkpointElapsedMs,
1547
1749
  checkpoint
1548
1750
  };
1549
1751
  }
1550
1752
  if (autoVacuum === 2) {
1753
+ if (hasLiveTurn) {
1754
+ const detail = "a session turn is live";
1755
+ console.warn(
1756
+ `[reclaimSessionDbSpace] skipping incremental reclaim of ${dbPath}: ${detail}`
1757
+ );
1758
+ return { ok: false, skipped: "live-turn", detail };
1759
+ }
1551
1760
  const beforeBytes = readLogicalBytes(db);
1552
1761
  const freelistBefore = readFreelistCount(db);
1553
1762
  const startedAt = now();
@@ -1572,7 +1781,9 @@ async function reclaimSessionDbSpace(input) {
1572
1781
  freelistAfter = readFreelistCount(db);
1573
1782
  }
1574
1783
  const afterBytes = readLogicalBytes(db);
1784
+ const checkpointStartedAt = now();
1575
1785
  const checkpoint = readCheckpointResult(db);
1786
+ const checkpointElapsedMs = Math.max(0, now() - checkpointStartedAt);
1576
1787
  return {
1577
1788
  ok: true,
1578
1789
  mode: "incremental",
@@ -1583,6 +1794,7 @@ async function reclaimSessionDbSpace(input) {
1583
1794
  pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1584
1795
  stoppedBy,
1585
1796
  elapsedMs: Math.max(0, now() - startedAt),
1797
+ checkpointElapsedMs,
1586
1798
  checkpoint
1587
1799
  };
1588
1800
  }
@@ -1602,6 +1814,263 @@ async function reclaimSessionDbSpace(input) {
1602
1814
  }
1603
1815
  }
1604
1816
 
1817
+ // src/lib/opencode/dependency-install-diagnostic.ts
1818
+ import { lstatSync, realpathSync } from "node:fs";
1819
+ import { basename, dirname as dirname3, isAbsolute, join as join2, relative, resolve, sep } from "node:path";
1820
+ var DEPENDENCY_INSTALL_FAILURE_MESSAGE = "background dependency install failed";
1821
+ var MAX_DISTINCT_DIAGNOSTICS = 32;
1822
+ var EFFECT = "OpenCode could not install background dependencies for this scope; its plugins and tools may be unavailable";
1823
+ function readOpenCodeLogField(line, key) {
1824
+ const match = line.match(new RegExp(`(?:^|\\s)${key}=(?:"((?:\\\\.|[^"])*)"|(\\S+))`));
1825
+ if (!match) return void 0;
1826
+ if (match[1] === void 0) return match[2];
1827
+ return match[1].replace(/\\(["\\n])/g, (_match, escaped) => {
1828
+ return escaped === "n" ? "\n" : escaped;
1829
+ });
1830
+ }
1831
+ function parseTrigger(line) {
1832
+ if (readOpenCodeLogField(line, "message") !== DEPENDENCY_INSTALL_FAILURE_MESSAGE) {
1833
+ return null;
1834
+ }
1835
+ const error2 = readOpenCodeLogField(line, "error");
1836
+ if (!error2 || !/\bEACCES\b/.test(error2)) return null;
1837
+ const errnoMatch = error2.match(/\bEACCES:\s*permission denied,\s*([a-z_]+)\s+'([^']*)'/);
1838
+ const capturedPath = errnoMatch?.[2];
1839
+ return {
1840
+ dir: readOpenCodeLogField(line, "dir"),
1841
+ op: errnoMatch?.[1],
1842
+ path: capturedPath && isAbsolute(capturedPath) ? capturedPath : void 0
1843
+ };
1844
+ }
1845
+ function getErrorCode(error2) {
1846
+ if (typeof error2 === "object" && error2 !== null && "code" in error2) {
1847
+ const code = error2.code;
1848
+ if (typeof code === "string") return code;
1849
+ }
1850
+ return "UNKNOWN";
1851
+ }
1852
+ function isWithin(path, root) {
1853
+ const candidate = resolve(path);
1854
+ const resolvedRoot = resolve(root);
1855
+ return candidate === resolvedRoot || candidate.startsWith(`${resolvedRoot}${sep}`);
1856
+ }
1857
+ function rootSpecs(context) {
1858
+ return [
1859
+ { token: "~", configured: resolve(context.homeDir) },
1860
+ { token: "$WORKSPACE", configured: resolve(context.cwd) }
1861
+ ];
1862
+ }
1863
+ function resolveRoots(context) {
1864
+ const resolved = [];
1865
+ const unresolved = [];
1866
+ for (const spec of rootSpecs(context)) {
1867
+ try {
1868
+ resolved.push({ ...spec, real: realpathSync(spec.configured) });
1869
+ } catch (_error) {
1870
+ unresolved.push(spec);
1871
+ }
1872
+ }
1873
+ return { resolved, unresolved };
1874
+ }
1875
+ function resolveNearestAncestor(failingPath, remaining, error2) {
1876
+ let ancestor = dirname3(failingPath);
1877
+ const suffix = [basename(failingPath), ...remaining];
1878
+ while (ancestor) {
1879
+ try {
1880
+ return {
1881
+ path: resolve(realpathSync(ancestor), ...suffix),
1882
+ failure: { path: resolve(failingPath), error: error2 }
1883
+ };
1884
+ } catch (ancestorError) {
1885
+ if (ancestor === dirname3(ancestor) || !getErrorCode(ancestorError).startsWith("E")) {
1886
+ return null;
1887
+ }
1888
+ suffix.unshift(basename(ancestor));
1889
+ ancestor = dirname3(ancestor);
1890
+ }
1891
+ }
1892
+ return null;
1893
+ }
1894
+ function resolveExistingPath(absolutePath) {
1895
+ if (!isAbsolute(absolutePath)) return null;
1896
+ const candidate = resolve(absolutePath);
1897
+ const remaining = [];
1898
+ let current = candidate;
1899
+ while (current) {
1900
+ try {
1901
+ const stat2 = lstatSync(current);
1902
+ if (stat2.isSymbolicLink()) {
1903
+ if (remaining.length === 0) {
1904
+ return { path: join2(realpathSync(dirname3(current)), basename(current)) };
1905
+ }
1906
+ return { path: resolve(realpathSync(current), ...remaining) };
1907
+ }
1908
+ return { path: resolve(realpathSync(current), ...remaining) };
1909
+ } catch (error2) {
1910
+ const code = getErrorCode(error2);
1911
+ if (code !== "ENOENT" && code !== "ENOTDIR") {
1912
+ return resolveNearestAncestor(current, remaining, error2);
1913
+ }
1914
+ }
1915
+ if (current === dirname3(current)) return null;
1916
+ remaining.unshift(basename(current));
1917
+ current = dirname3(current);
1918
+ }
1919
+ return null;
1920
+ }
1921
+ function tokenise(absolutePath, roots) {
1922
+ const pathResolution = resolveExistingPath(absolutePath);
1923
+ if (!pathResolution) return null;
1924
+ const root = roots.reduce((selected, candidate) => {
1925
+ if (!isWithin(pathResolution.path, candidate.real)) return selected;
1926
+ if (!selected || candidate.real.length > selected.real.length || candidate.real.length === selected.real.length && candidate.token === "$WORKSPACE") {
1927
+ return candidate;
1928
+ }
1929
+ return selected;
1930
+ }, null);
1931
+ if (!root) return null;
1932
+ const suffix = relative(root.real, pathResolution.path);
1933
+ return {
1934
+ value: suffix ? `${root.token}/${suffix.split(sep).join("/")}` : root.token,
1935
+ root,
1936
+ resolutionFailure: pathResolution.failure
1937
+ };
1938
+ }
1939
+ function classifyScope(dir, roots) {
1940
+ if (!dir) return "other";
1941
+ const tokenisedDir = tokenise(dir, roots.resolved);
1942
+ if (tokenisedDir) return tokenisedDir.root.token === "$WORKSPACE" ? "workspace" : "global";
1943
+ for (const spec of roots.unresolved) {
1944
+ if (isWithin(dir, spec.configured)) {
1945
+ return spec.token === "$WORKSPACE" ? "workspace" : "global";
1946
+ }
1947
+ }
1948
+ return "other";
1949
+ }
1950
+ function unresolvedRootForPath(path, unresolved) {
1951
+ return unresolved.find((spec) => isWithin(path, spec.configured));
1952
+ }
1953
+ function statType(stat2) {
1954
+ if (stat2.isDirectory()) return "d";
1955
+ if (stat2.isFile()) return "f";
1956
+ if (stat2.isSymbolicLink()) return "l";
1957
+ return "o";
1958
+ }
1959
+ function renderStat(name, stat2) {
1960
+ const mode = (stat2.mode & 4095).toString(8).padStart(4, "0");
1961
+ return `${name}:${statType(stat2)}:${stat2.uid}:${stat2.gid}:${mode}`;
1962
+ }
1963
+ function renderStatError(name, error2) {
1964
+ const code = getErrorCode(error2);
1965
+ return `${name}:${renderErrorCode(code)}`;
1966
+ }
1967
+ function renderErrorCode(code) {
1968
+ if (code === "ENOENT") return "missing";
1969
+ return code.startsWith("E") ? code : `E${code}`;
1970
+ }
1971
+ function inspectChain(rootReal, rootToken, absolutePath, resolutionFailure) {
1972
+ const target = resolve(absolutePath);
1973
+ const relativePath = relative(rootReal, target);
1974
+ const components = relativePath ? relativePath.split(sep) : [];
1975
+ const entries = [];
1976
+ let current = rootReal;
1977
+ let status2 = "complete";
1978
+ let finalStat;
1979
+ for (const name of [rootToken, ...components]) {
1980
+ if (name !== rootToken) current = join2(current, name);
1981
+ if (resolutionFailure && resolve(current) === resolve(resolutionFailure.path)) {
1982
+ entries.push(renderStatError(name, resolutionFailure.error));
1983
+ status2 = "stat_failed";
1984
+ break;
1985
+ }
1986
+ try {
1987
+ const stat2 = lstatSync(current);
1988
+ entries.push(renderStat(name, stat2));
1989
+ finalStat = stat2;
1990
+ } catch (error2) {
1991
+ entries.push(renderStatError(name, error2));
1992
+ if (getErrorCode(error2) !== "ENOENT") status2 = "stat_failed";
1993
+ break;
1994
+ }
1995
+ }
1996
+ let symlinkTarget;
1997
+ let symlinkMeta;
1998
+ if (finalStat?.isSymbolicLink() && entries.length === components.length + 1) {
1999
+ try {
2000
+ symlinkTarget = realpathSync(current);
2001
+ try {
2002
+ symlinkMeta = renderStat("", lstatSync(symlinkTarget)).slice(1);
2003
+ } catch (error2) {
2004
+ symlinkMeta = renderErrorCode(getErrorCode(error2));
2005
+ status2 = "stat_failed";
2006
+ }
2007
+ } catch (error2) {
2008
+ symlinkTarget = "<unresolved>";
2009
+ symlinkMeta = renderErrorCode(getErrorCode(error2));
2010
+ status2 = "stat_failed";
2011
+ }
2012
+ }
2013
+ return { status: status2, chain: entries.join(","), symlinkTarget, symlinkMeta };
2014
+ }
2015
+ function buildDiagnostic(line, context) {
2016
+ const trigger = parseTrigger(line);
2017
+ if (!trigger) return null;
2018
+ const roots = resolveRoots(context);
2019
+ const scope = classifyScope(trigger.dir, roots);
2020
+ const pathSource = trigger.path ? "error" : trigger.dir ? "dir" : "none";
2021
+ const inspectedPath = trigger.path ?? trigger.dir;
2022
+ const microvmId = context.microvmId?.trim() || "unknown";
2023
+ const euid = context.euid === void 0 ? "unavailable" : String(context.euid);
2024
+ const egid = context.egid === void 0 ? "unavailable" : String(context.egid);
2025
+ const op = trigger.op ?? "unknown";
2026
+ if (!inspectedPath) {
2027
+ return {
2028
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=no_path path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=unknown cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=unavailable`,
2029
+ localDetail: null
2030
+ };
2031
+ }
2032
+ const tokenisedPath = tokenise(inspectedPath, roots.resolved);
2033
+ if (!tokenisedPath) {
2034
+ const unresolvedRoot = unresolvedRootForPath(inspectedPath, roots.unresolved);
2035
+ if (unresolvedRoot) {
2036
+ return {
2037
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=root_unresolved path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=unknown cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=unavailable`,
2038
+ localDetail: null
2039
+ };
2040
+ }
2041
+ return {
2042
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=outside_roots path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=<outside-approved-roots> cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=unavailable`,
2043
+ localDetail: `OPENCODE-DEPENDENCY-INSTALL-EACCES (local only): outside approved roots: ${inspectedPath}`
2044
+ };
2045
+ }
2046
+ const inspection = inspectChain(
2047
+ tokenisedPath.root.real,
2048
+ tokenisedPath.root.token,
2049
+ inspectedPath,
2050
+ tokenisedPath.resolutionFailure
2051
+ );
2052
+ const optionalSymlinkFields = inspection.symlinkTarget ? ` symlink_target=${tokenise(inspection.symlinkTarget, roots.resolved)?.value ?? "<outside-approved-roots>"}` + (inspection.symlinkMeta ? ` symlink_meta=${inspection.symlinkMeta}` : "") : "";
2053
+ return {
2054
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=${inspection.status} path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=${tokenisedPath.value} cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=${inspection.chain}${optionalSymlinkFields}`,
2055
+ localDetail: null
2056
+ };
2057
+ }
2058
+ function createDependencyInstallDiagnosticReporter({
2059
+ context,
2060
+ emitEvent: emitEvent2,
2061
+ emitLocal
2062
+ }) {
2063
+ const emittedEvents = /* @__PURE__ */ new Set();
2064
+ return (line) => {
2065
+ const diagnostic = buildDiagnostic(line, context);
2066
+ if (!diagnostic || emittedEvents.has(diagnostic.event)) return;
2067
+ if (emittedEvents.size >= MAX_DISTINCT_DIAGNOSTICS) return;
2068
+ emittedEvents.add(diagnostic.event);
2069
+ emitEvent2(diagnostic.event);
2070
+ if (diagnostic.localDetail) emitLocal(diagnostic.localDetail);
2071
+ };
2072
+ }
2073
+
1605
2074
  // src/lib/runner-activity-telemetry.ts
1606
2075
  var FORWARDED_LEVELS_BY_SOURCE = {
1607
2076
  "cli.run": /* @__PURE__ */ new Set(["warn", "error"]),
@@ -1633,7 +2102,17 @@ function parseOpenCodeLogLine(line) {
1633
2102
  }
1634
2103
  var MAX_LINE_BUFFER_BYTES = 16 * 1024;
1635
2104
  var REPEAT_SUPPRESSION_WINDOW_MS = 6e4;
1636
- function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure) {
2105
+ function sanitiseDependencyInstallWarning(line) {
2106
+ const withoutDirectory = line.replace(
2107
+ /((?:^|\s)dir=)(?:"(?:\\.|[^"])*"|\/[^\s)"']+|\S+)/g,
2108
+ "$1<path>"
2109
+ );
2110
+ return withoutDirectory.replace(
2111
+ /((?:^|\s)error=)"((?:\\.|[^"])*)"/,
2112
+ (_match, prefix, error2) => `${prefix}"${error2.replace(/'\/[^']*'/g, "'<path>'")}"`
2113
+ );
2114
+ }
2115
+ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure, onDependencyInstallFailure) {
1637
2116
  let buffer = Buffer.alloc(0);
1638
2117
  const repeatStates = /* @__PURE__ */ new Map();
1639
2118
  const forwardLine = (line, parsed, repeatsSuppressed = 0) => {
@@ -1645,7 +2124,7 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1645
2124
  forwardRunnerActivity(
1646
2125
  {
1647
2126
  level: parsed.level,
1648
- error: line,
2127
+ error: parsed.message === DEPENDENCY_INSTALL_FAILURE_MESSAGE ? sanitiseDependencyInstallWarning(line) : line,
1649
2128
  metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
1650
2129
  source: "opencode"
1651
2130
  },
@@ -1663,18 +2142,7 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1663
2142
  repeatStates.delete(key);
1664
2143
  }
1665
2144
  };
1666
- const flushLine = (line) => {
1667
- if (onSessionStoreWriteFailure && isSessionStoreWriteFailure(line)) {
1668
- try {
1669
- onSessionStoreWriteFailure();
1670
- } catch (err) {
1671
- reportTelemetryDiagnostic(
1672
- `Runner activity telemetry could not record session-store write failure: ${err instanceof Error ? err.message : String(err)}`
1673
- );
1674
- }
1675
- }
1676
- const parsed = parseOpenCodeLogLine(line);
1677
- if (!parsed) return;
2145
+ const forwardWithRepeatSuppression = (line, parsed) => {
1678
2146
  const now = Date.now();
1679
2147
  const key = parsed.message === void 0 ? void 0 : `${parsed.level}\0${parsed.message}\0${parsed.sessionID ?? ""}\0${parsed.messageID ?? ""}`;
1680
2148
  expireRepeatStates(now, key);
@@ -1697,6 +2165,35 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1697
2165
  previous.parsed = parsed;
1698
2166
  previous.repeatsSuppressed++;
1699
2167
  };
2168
+ const flushLine = (line) => {
2169
+ const parsed = parseOpenCodeLogLine(line);
2170
+ const isDiskFailure = isSessionStoreWriteFailure(line);
2171
+ const cause = sessionStoreCauseToken(line);
2172
+ if (onSessionStoreWriteFailure && (isDiskFailure || cause !== null)) {
2173
+ try {
2174
+ onSessionStoreWriteFailure({
2175
+ isDiskFailure,
2176
+ sessionId: parsed?.sessionID ?? null,
2177
+ cause
2178
+ });
2179
+ } catch (err) {
2180
+ reportTelemetryDiagnostic(
2181
+ `Runner activity telemetry could not record session-store write failure: ${err instanceof Error ? err.message : String(err)}`
2182
+ );
2183
+ }
2184
+ }
2185
+ if (!parsed) return;
2186
+ forwardWithRepeatSuppression(line, parsed);
2187
+ if (onDependencyInstallFailure && parsed.message === DEPENDENCY_INSTALL_FAILURE_MESSAGE) {
2188
+ try {
2189
+ onDependencyInstallFailure(line);
2190
+ } catch (err) {
2191
+ reportTelemetryDiagnostic(
2192
+ `Runner activity telemetry could not record dependency-install failure diagnostic: ${err instanceof Error ? err.message : String(err)}`
2193
+ );
2194
+ }
2195
+ }
2196
+ };
1700
2197
  const forward = ((chunk) => {
1701
2198
  buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, "utf-8")]);
1702
2199
  let newlineIndex;
@@ -1722,34 +2219,38 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1722
2219
  var MAX_MESSAGE_LENGTH = 500;
1723
2220
  var MAX_METADATA_VALUE_LENGTH = 200;
1724
2221
  var MAX_METADATA_ENTRIES = 20;
1725
- var TRUNCATION_MARKER = "\u2026";
1726
- function redactHomeDirectory(message) {
1727
- const home = homedir2();
1728
- if (!home || home === "/") {
1729
- return message;
1730
- }
1731
- const pathSegmentCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-";
1732
- let result = "";
1733
- let searchFrom = 0;
1734
- let homeIndex;
1735
- while ((homeIndex = message.indexOf(home, searchFrom)) !== -1) {
1736
- const homeEnd = homeIndex + home.length;
1737
- const previousCharacter = message[homeIndex - 1];
1738
- const nextCharacter = message[homeEnd];
1739
- const atPathBoundary = home === "/" || (!previousCharacter || !pathSegmentCharacters.includes(previousCharacter)) && (!nextCharacter || !pathSegmentCharacters.includes(nextCharacter));
1740
- result += message.slice(searchFrom, homeIndex);
1741
- result += atPathBoundary ? "~" : home;
1742
- searchFrom = homeEnd;
1743
- }
1744
- return result + message.slice(searchFrom);
1745
- }
1746
- function redactDiagnosticText(message) {
1747
- const redacted = message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/(?<![A-Za-z0-9_-])sk-ant-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-ant-***").replace(/(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-proj-***").replace(/(?<![A-Za-z0-9_-])sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "sk-***").replace(/(?<![A-Za-z0-9_-])(gh[oprsu])_[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "$1_***").replace(/(?<![A-Za-z0-9_-])github_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_-])/g, "github_pat_***").replace(/https?:\/\/\S+/g, "<url>");
1748
- return redactHomeDirectory(redacted);
1749
- }
1750
2222
  function truncate(message) {
1751
2223
  if (message.length <= MAX_MESSAGE_LENGTH) return message;
1752
- return message.slice(0, MAX_MESSAGE_LENGTH - TRUNCATION_MARKER.length) + TRUNCATION_MARKER;
2224
+ let dropped = message.length - MAX_MESSAGE_LENGTH;
2225
+ let marker = `\u2026${dropped} chars omitted\u2026`;
2226
+ let previousDropped = -1;
2227
+ while (dropped !== previousDropped) {
2228
+ previousDropped = dropped;
2229
+ const nextDropped = message.length - MAX_MESSAGE_LENGTH + marker.length;
2230
+ dropped = nextDropped;
2231
+ marker = `\u2026${dropped} chars omitted\u2026`;
2232
+ }
2233
+ const remaining = MAX_MESSAGE_LENGTH - marker.length;
2234
+ const headLength = Math.ceil(remaining * 0.6);
2235
+ const tailLength = remaining - headLength;
2236
+ const effectMatch = message.startsWith("OPENCODE-DEPENDENCY-INSTALL-EACCES:") ? message.match(/ effect="[^"]*"/) : null;
2237
+ if (effectMatch?.index !== void 0) {
2238
+ const effectEnd = effectMatch.index + effectMatch[0].length;
2239
+ if (effectEnd > headLength && effectEnd < message.length) {
2240
+ const effectLength = effectMatch[0].length;
2241
+ const nonEffectLength = remaining - effectLength;
2242
+ const preservedTailLength = Math.min(
2243
+ message.length - effectEnd,
2244
+ nonEffectLength - Math.min(effectMatch.index, Math.ceil(nonEffectLength * 0.6))
2245
+ );
2246
+ const preservedHeadLength = Math.min(
2247
+ effectMatch.index,
2248
+ nonEffectLength - preservedTailLength
2249
+ );
2250
+ return message.slice(0, preservedHeadLength) + marker + message.slice(effectMatch.index, effectEnd) + message.slice(message.length - preservedTailLength);
2251
+ }
2252
+ }
2253
+ return message.slice(0, headLength) + marker + message.slice(message.length - tailLength);
1753
2254
  }
1754
2255
  function sanitiseMetadata(metadata) {
1755
2256
  if (!metadata) return {};
@@ -1826,10 +2327,10 @@ function forwardRunnerActivity(entry, context) {
1826
2327
 
1827
2328
  // src/lib/opencode/session-db-recovery-report.ts
1828
2329
  import { readFileSync as readFileSync2, unlinkSync } from "node:fs";
1829
- import { join as join2 } from "node:path";
2330
+ import { join as join3 } from "node:path";
1830
2331
  function sessionDbRecoveryReportPath(homeDir, env) {
1831
2332
  const override = env.EVIDENT_SESSION_DB_RECOVERY_REPORT?.trim();
1832
- return override || join2(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
2333
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
1833
2334
  }
1834
2335
  function drainSessionDbRecoveryReport({
1835
2336
  homeDir,
@@ -1959,12 +2460,23 @@ function buildSessionDbRecoveryActivity(record) {
1959
2460
  metadata: withoutContractFields(record),
1960
2461
  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}`
1961
2462
  };
1962
- case "session_db_boot_refused":
1963
- return {
1964
- level,
1965
- metadata: withoutContractFields(record),
1966
- 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.`
1967
- };
2463
+ case "session_db_boot_refused": {
2464
+ const message = (() => {
2465
+ switch (record.reason) {
2466
+ case "local_discard_failed":
2467
+ return `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.`;
2468
+ case "replica_separation_unproven":
2469
+ return `This runner did not come online because its damaged session database could not be safely separated from its active backup. The SESSION-DB-REPLICA-SEPARATION-UNVERIFIED boot log contains the partial-quarantine moved and remaining counts. 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.`;
2470
+ case "verification_timeout_before_separation":
2471
+ return "This runner did not come online because session database verification timed out after detecting corruption, before replica separation was attempted. No replica progress is claimed. See SESSION-DB-VERIFY-TIMEOUT in the runner boot logs before retrying.";
2472
+ case "verification_timeout_during_separation":
2473
+ return "This runner did not come online because session database verification timed out while replica separation was in progress. Separation completion and object removal were not proven. See SESSION-DB-VERIFY-TIMEOUT in the runner boot logs before retrying.";
2474
+ default:
2475
+ return "This runner did not come online because session database recovery could not be completed. Inspect the runner boot logs for the refusal reason before retrying.";
2476
+ }
2477
+ })();
2478
+ return { level, metadata: withoutContractFields(record), message };
2479
+ }
1968
2480
  case "schema_provenance_mismatch":
1969
2481
  return {
1970
2482
  level,
@@ -2091,7 +2603,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
2091
2603
  if (health.healthy || health.authFailed) {
2092
2604
  return health;
2093
2605
  }
2094
- await new Promise((resolve4) => setTimeout(resolve4, 1e3));
2606
+ await new Promise((resolve5) => setTimeout(resolve5, 1e3));
2095
2607
  }
2096
2608
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
2097
2609
  }
@@ -2102,7 +2614,7 @@ async function waitForOpenCode2Health(port, password, timeoutMs = 3e4) {
2102
2614
  if (health.healthy || health.authFailed) {
2103
2615
  return health;
2104
2616
  }
2105
- await new Promise((resolve4) => setTimeout(resolve4, 1e3));
2617
+ await new Promise((resolve5) => setTimeout(resolve5, 1e3));
2106
2618
  }
2107
2619
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
2108
2620
  }
@@ -2250,8 +2762,12 @@ function errorOf(m) {
2250
2762
  if (!m || typeof m !== "object") return void 0;
2251
2763
  return m.info?.error ?? m.error;
2252
2764
  }
2765
+ function isTerminalStopAnswer(m) {
2766
+ return completedOf(m) != null && errorOf(m) == null && finishOf(m) === "stop";
2767
+ }
2253
2768
  function isAssistantInFlight(m) {
2254
2769
  if (completedOf(m) == null) return true;
2770
+ if (errorOf(m) != null) return false;
2255
2771
  return finishOf(m) === "tool-calls";
2256
2772
  }
2257
2773
  async function getSessionMessages(port, sessionId, client) {
@@ -2365,7 +2881,18 @@ async function deleteSession(port, id, client) {
2365
2881
  { method: "DELETE" },
2366
2882
  { allowStatuses: ALL_HTTP_STATUSES }
2367
2883
  );
2368
- return { ok: res.status >= 200 && res.status < 300, status: res.status };
2884
+ if (res.status >= 200 && res.status < 300) {
2885
+ return { ok: true, status: res.status };
2886
+ }
2887
+ try {
2888
+ const body = await res.text();
2889
+ return body ? { ok: false, status: res.status, body } : { ok: false, status: res.status };
2890
+ } catch (error2) {
2891
+ console.error(
2892
+ `[deleteSession] DELETE /session/${id} response body read failed for HTTP ${res.status}: ${error2 instanceof Error ? error2.message : String(error2)}`
2893
+ );
2894
+ return { ok: false, status: res.status };
2895
+ }
2369
2896
  } catch (error2) {
2370
2897
  console.error(
2371
2898
  `[deleteSession] DELETE /session/${id} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
@@ -2608,11 +3135,31 @@ function messageText(m) {
2608
3135
  if (!m || !Array.isArray(m.parts)) return "";
2609
3136
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2610
3137
  }
2611
- async function sendPromptAsync(port, sessionId, content, options, attachments, client) {
3138
+ function findDispatchedUserMessageId(messages, content, excludeIds) {
3139
+ if (!messages || messages.length === 0) return null;
3140
+ let best = null;
3141
+ for (const message of messages) {
3142
+ if (roleOf2(message) !== "user") continue;
3143
+ const id = idOf2(message);
3144
+ if (typeof id !== "string" || excludeIds?.has(id)) continue;
3145
+ if (messageText(message) !== content) continue;
3146
+ const created = createdOf(message) ?? 0;
3147
+ if (best === null || created > best.created) best = { id, created };
3148
+ }
3149
+ return best?.id ?? null;
3150
+ }
3151
+ async function sendPromptAsync(port, sessionId, content, options, attachments, client, callbacks) {
2612
3152
  const before = await getSessionMessages(port, sessionId, client);
2613
3153
  const knownUserIds = new Set(
2614
3154
  (before ?? []).filter((m) => roleOf2(m) === "user").map((m) => idOf2(m)).filter((id) => typeof id === "string")
2615
3155
  );
3156
+ if (callbacks) {
3157
+ try {
3158
+ callbacks.onKnownUserIds(before === null ? null : knownUserIds);
3159
+ } catch (error2) {
3160
+ callbacks.onKnownUserIdsError(error2);
3161
+ }
3162
+ }
2616
3163
  const parts = [{ type: "text", text: content }];
2617
3164
  let pendingOutcomes = null;
2618
3165
  if (attachments && attachments.inputs.length > 0) {
@@ -2651,25 +3198,13 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2651
3198
  const READ_BACK_DELAY_MS = 150;
2652
3199
  for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
2653
3200
  const after = await getSessionMessages(port, sessionId, client);
2654
- if (after) {
2655
- let best = null;
2656
- for (const m of after) {
2657
- if (roleOf2(m) !== "user") continue;
2658
- const id = idOf2(m);
2659
- if (typeof id !== "string" || knownUserIds.has(id)) continue;
2660
- if (messageText(m) !== content) continue;
2661
- const created = createdOf(m) ?? 0;
2662
- if (best === null || created > best.created) {
2663
- best = { id, created };
2664
- }
2665
- }
2666
- if (best) {
2667
- if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
2668
- return best.id;
2669
- }
3201
+ const dispatchedUserMessageId = findDispatchedUserMessageId(after, content, knownUserIds);
3202
+ if (dispatchedUserMessageId !== null) {
3203
+ if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
3204
+ return dispatchedUserMessageId;
2670
3205
  }
2671
3206
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2672
- await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
3207
+ await new Promise((resolve5) => setTimeout(resolve5, READ_BACK_DELAY_MS));
2673
3208
  }
2674
3209
  }
2675
3210
  return null;
@@ -2690,30 +3225,41 @@ function findAssistantReplyAfter(messages, userMessageId) {
2690
3225
  function findLastAssistantReplyFor(messages, userMessageId) {
2691
3226
  if (!messages || messages.length === 0) return null;
2692
3227
  let lastCorrelated = null;
2693
- let lastNonErrored = null;
2694
3228
  for (let i = messages.length - 1; i >= 0; i--) {
2695
3229
  const m = messages[i];
2696
3230
  if (roleOf2(m) !== "assistant" || parentIdOf2(m) !== userMessageId) continue;
2697
- if (lastCorrelated === null) lastCorrelated = m;
2698
- if (errorOf(m) == null) {
2699
- lastNonErrored = m;
2700
- break;
3231
+ lastCorrelated = m;
3232
+ break;
3233
+ }
3234
+ if (lastCorrelated) {
3235
+ if (errorOf(lastCorrelated) == null) return lastCorrelated;
3236
+ for (let i = messages.length - 1; i >= 0; i--) {
3237
+ const m = messages[i];
3238
+ if (roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId && isTerminalStopAnswer(m)) {
3239
+ return m;
3240
+ }
2701
3241
  }
3242
+ return lastCorrelated;
2702
3243
  }
2703
- if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
2704
3244
  const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2705
3245
  if (userIndex === -1) return null;
2706
3246
  let last = null;
2707
- let lastOk = null;
3247
+ let lastAssistantIndex = -1;
2708
3248
  for (let i = userIndex + 1; i < messages.length; i++) {
2709
3249
  const role = roleOf2(messages[i]);
2710
3250
  if (role === "user") break;
2711
3251
  if (role === "assistant") {
2712
3252
  last = messages[i];
2713
- if (errorOf(messages[i]) == null) lastOk = messages[i];
3253
+ lastAssistantIndex = i;
2714
3254
  }
2715
3255
  }
2716
- return lastOk ?? last;
3256
+ if (last === null || errorOf(last) == null) return last;
3257
+ for (let i = lastAssistantIndex; i > userIndex; i--) {
3258
+ const m = messages[i];
3259
+ if (roleOf2(m) === "user") break;
3260
+ if (roleOf2(m) === "assistant" && isTerminalStopAnswer(m)) return m;
3261
+ }
3262
+ return last;
2717
3263
  }
2718
3264
  function collectSubagentSessions(messages, userMessageId) {
2719
3265
  if (!messages || messages.length === 0) return [];
@@ -2819,8 +3365,9 @@ function attributeTaskCallUsage(messages, windows) {
2819
3365
  }
2820
3366
  function sumAssistantUsage(messages) {
2821
3367
  if (!messages || messages.length === 0) return null;
2822
- const nonErrored = messages.filter((message) => errorOf(message) == null);
2823
- const selected = nonErrored.length > 0 ? nonErrored : messages;
3368
+ const hasTrailingError = errorOf(messages[messages.length - 1]) != null;
3369
+ const hasTerminalStopAnswer = messages.some(isTerminalStopAnswer);
3370
+ const selected = hasTrailingError && hasTerminalStopAnswer ? messages.filter((message) => errorOf(message) == null) : messages;
2824
3371
  let sawAnyUsage = false;
2825
3372
  let inputSum = 0;
2826
3373
  let outputSum = 0;
@@ -2829,9 +3376,11 @@ function sumAssistantUsage(messages) {
2829
3376
  let cacheWriteSum = 0;
2830
3377
  let costSum = 0;
2831
3378
  let sawCost = false;
3379
+ let toolCallCount = 0;
2832
3380
  let modelId = null;
2833
3381
  let providerId = null;
2834
3382
  for (const m of selected) {
3383
+ toolCallCount += m.parts?.filter((part) => part.type === "tool").length ?? 0;
2835
3384
  const info = m.info;
2836
3385
  if (!info) continue;
2837
3386
  const tokens = info.tokens;
@@ -2868,7 +3417,8 @@ function sumAssistantUsage(messages) {
2868
3417
  usage_tokens_cache_write: cacheWriteSum,
2869
3418
  // NULL means OpenCode never reported a cost; it is distinct from a genuine
2870
3419
  // zero-cost message, which sets `sawCost` with `costSum === 0`.
2871
- usage_cost_usd: sawCost ? costSum : null
3420
+ usage_cost_usd: sawCost ? costSum : null,
3421
+ turn_tool_calls: toolCallCount
2872
3422
  };
2873
3423
  }
2874
3424
  function messageUsage(messages, userMessageId) {
@@ -2876,11 +3426,9 @@ function messageUsage(messages, userMessageId) {
2876
3426
  const byParentAll = messages.filter(
2877
3427
  (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2878
3428
  );
2879
- const byParentNonErrored = byParentAll.filter((m) => errorOf(m) == null);
2880
- const byParent = byParentNonErrored.length > 0 ? byParentNonErrored : byParentAll;
2881
3429
  let correlated;
2882
- if (byParent.length > 0) {
2883
- correlated = byParent;
3430
+ if (byParentAll.length > 0) {
3431
+ correlated = byParentAll;
2884
3432
  } else {
2885
3433
  const reply = findAssistantReplyAfter(messages, userMessageId);
2886
3434
  correlated = reply ? [reply] : [];
@@ -2926,14 +3474,38 @@ function messageError(messages, userMessageId) {
2926
3474
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2927
3475
  const error2 = errorOf(reply);
2928
3476
  if (error2 == null) return null;
2929
- if (typeof error2 === "string") return error2;
2930
- if (typeof error2 === "object") {
3477
+ let rendered;
3478
+ if (typeof error2 === "string") {
3479
+ rendered = error2;
3480
+ } else if (typeof error2 === "object") {
2931
3481
  const e = error2;
2932
3482
  const dataMessage = e.data?.message;
2933
- if (typeof dataMessage === "string") return dataMessage;
2934
- if (typeof e.message === "string") return e.message;
3483
+ if (typeof dataMessage === "string") {
3484
+ rendered = dataMessage;
3485
+ } else if (typeof e.message === "string") {
3486
+ rendered = e.message;
3487
+ } else {
3488
+ rendered = "The agent run failed.";
3489
+ }
3490
+ } else {
3491
+ rendered = "The agent run failed.";
2935
3492
  }
2936
- return "The agent run failed.";
3493
+ const toolPart = Array.isArray(reply?.parts) ? reply.parts.find(
3494
+ (part) => part && typeof part === "object" && part.type === "tool" && part.state?.status === "error"
3495
+ ) : void 0;
3496
+ return typeof toolPart?.tool === "string" && toolPart.tool.length > 0 ? `${rendered} (tool "${toolPart.tool}")` : rendered;
3497
+ }
3498
+ function describeAbandonedReplyShape(messages, userMessageId) {
3499
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
3500
+ if (!reply) return null;
3501
+ const parts = Array.isArray(reply.parts) ? reply.parts : [];
3502
+ const lastTool = [...parts].reverse().find((part) => part?.type === "tool");
3503
+ return {
3504
+ partTypes: parts.slice(-12).map((part) => typeof part?.type === "string" ? part.type : "(unknown)"),
3505
+ droppedPartCount: Math.max(0, parts.length - 12),
3506
+ lastToolName: typeof lastTool?.tool === "string" && lastTool.tool.length > 0 ? lastTool.tool : null,
3507
+ lastToolStatus: typeof lastTool?.state?.status === "string" && lastTool.state.status.length > 0 ? lastTool.state.status : null
3508
+ };
2937
3509
  }
2938
3510
  function isAbortedTerminalReply(messages, userMessageId) {
2939
3511
  const reply = findLastAssistantReplyFor(messages, userMessageId);
@@ -2950,19 +3522,32 @@ function isAbortedTerminalReply(messages, userMessageId) {
2950
3522
  }
2951
3523
  return false;
2952
3524
  }
3525
+ var CLAUDE_AUTH_EXPIRY_MESSAGES = Object.freeze([
3526
+ "Claude Code credentials are unavailable or expired. Run `claude` to refresh them."
3527
+ ]);
3528
+ function extractReplyErrorShape(reply) {
3529
+ const error2 = errorOf(reply);
3530
+ if (error2 == null || typeof error2 !== "object") {
3531
+ return { name: null, statusCode: null };
3532
+ }
3533
+ const e = error2;
3534
+ const name = typeof e.name === "string" ? e.name.slice(0, 100) : null;
3535
+ const data = e.data;
3536
+ const statusCode = typeof data?.statusCode === "number" && Number.isFinite(data.statusCode) ? data.statusCode : null;
3537
+ return { name, statusCode };
3538
+ }
2953
3539
  function classifyReplyAuthError(reply) {
2954
3540
  const error2 = errorOf(reply);
2955
3541
  if (error2 == null || typeof error2 !== "object") return null;
2956
3542
  const e = error2;
2957
3543
  const replyProviderId = reply?.info?.providerID ?? null;
2958
3544
  const replyModelId = reply?.info?.modelID ?? null;
3545
+ const data = e.data;
2959
3546
  if (e.name === "ProviderAuthError") {
2960
- const data = e.data;
2961
3547
  const providerId = typeof data?.providerID === "string" && data.providerID || replyProviderId;
2962
3548
  return { kind: "model_auth", providerId, modelId: replyModelId, reason: "missing" };
2963
3549
  }
2964
3550
  if (e.name === "APIError") {
2965
- const data = e.data;
2966
3551
  const statusCode = data?.statusCode;
2967
3552
  if (statusCode === 401 || statusCode === 403) {
2968
3553
  return {
@@ -2973,11 +3558,23 @@ function classifyReplyAuthError(reply) {
2973
3558
  };
2974
3559
  }
2975
3560
  }
3561
+ const errorMessage3 = typeof data?.message === "string" ? data.message : typeof e.message === "string" ? e.message : null;
3562
+ if (errorMessage3 !== null && CLAUDE_AUTH_EXPIRY_MESSAGES.some((knownMessage) => errorMessage3.includes(knownMessage))) {
3563
+ return {
3564
+ kind: "model_auth",
3565
+ providerId: typeof data?.providerID === "string" && data.providerID || replyProviderId,
3566
+ modelId: replyModelId,
3567
+ reason: "rejected"
3568
+ };
3569
+ }
2976
3570
  return null;
2977
3571
  }
2978
3572
  function messageFailure(messages, userMessageId) {
2979
3573
  return classifyReplyAuthError(findLastAssistantReplyFor(messages, userMessageId));
2980
3574
  }
3575
+ function isProviderModelNotFoundError(reason) {
3576
+ return reason.includes("Model not found:");
3577
+ }
2981
3578
  function findLatestSubagentAuthOutcome(messages, sinceMs) {
2982
3579
  if (!messages || messages.length === 0) return null;
2983
3580
  for (let i = messages.length - 1; i >= 0; i--) {
@@ -3186,23 +3783,49 @@ async function readSessionErrorStream(port, options, client) {
3186
3783
  if (reader) void reader.cancel().catch(() => void 0);
3187
3784
  }
3188
3785
  }
3189
- async function reloadProviderCache(port, client) {
3190
- if (client?.version === "v2") return;
3786
+ async function disposeAndReloadProviderConfig(port, client) {
3787
+ if (client?.version === "v2") return { outcome: "skipped_v2" };
3191
3788
  try {
3192
- const res = await requestWithClient(port, client, "/config", {
3193
- method: "PATCH",
3194
- headers: { "Content-Type": "application/json" },
3195
- body: JSON.stringify({})
3196
- });
3789
+ const res = await requestWithClient(
3790
+ port,
3791
+ client,
3792
+ "/config",
3793
+ {
3794
+ method: "PATCH",
3795
+ headers: { "Content-Type": "application/json" },
3796
+ body: JSON.stringify({})
3797
+ },
3798
+ { allowStatuses: ALL_HTTP_STATUSES }
3799
+ );
3197
3800
  if (!res.ok) {
3198
3801
  console.error(
3199
- `[reloadProviderCache] PATCH /config returned HTTP ${res.status} (port ${port})`
3802
+ `[disposeAndReloadProviderConfig] PATCH /config returned HTTP ${res.status} (port ${port})`
3200
3803
  );
3804
+ return { outcome: "failed", status: res.status, detail: null };
3201
3805
  }
3806
+ return { outcome: "reloaded" };
3202
3807
  } catch (err) {
3203
3808
  console.error(
3204
- `[reloadProviderCache] PATCH /config failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
3809
+ `[disposeAndReloadProviderConfig] PATCH /config failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
3205
3810
  );
3811
+ return {
3812
+ outcome: "failed",
3813
+ status: null,
3814
+ detail: err instanceof Error ? err.message : String(err)
3815
+ };
3816
+ }
3817
+ }
3818
+ async function probeOpenCodeRequestReadiness(port, client) {
3819
+ try {
3820
+ const res = await requestWithClient(port, client, "/config", void 0, {
3821
+ allowStatuses: ALL_HTTP_STATUSES
3822
+ });
3823
+ const body = await res.text();
3824
+ return { ok: res.ok, status: res.status, bodyEmpty: body.trim().length === 0, detail: null };
3825
+ } catch (err) {
3826
+ const detail = err instanceof Error ? err.message : String(err);
3827
+ console.error(`[probeOpenCodeRequestReadiness] GET /config failed (port ${port}): ${detail}`);
3828
+ return { ok: false, status: null, bodyEmpty: null, detail };
3206
3829
  }
3207
3830
  }
3208
3831
 
@@ -3508,7 +4131,18 @@ async function deleteV2Session(client, sessionId) {
3508
4131
  { method: "DELETE" },
3509
4132
  { allowStatuses: Array.from({ length: 500 }, (_, index) => index + 100) }
3510
4133
  );
3511
- return { ok: response.status >= 200 && response.status < 300, status: response.status };
4134
+ if (response.status >= 200 && response.status < 300) {
4135
+ return { ok: true, status: response.status };
4136
+ }
4137
+ try {
4138
+ const body = await response.text();
4139
+ return body ? { ok: false, status: response.status, body } : { ok: false, status: response.status };
4140
+ } catch (error2) {
4141
+ console.error(
4142
+ `[deleteV2Session] DELETE /api/session/${sessionId} response body read failed for HTTP ${response.status}: ${error2 instanceof Error ? error2.message : String(error2)}`
4143
+ );
4144
+ return { ok: false, status: response.status };
4145
+ }
3512
4146
  } catch (error2) {
3513
4147
  console.error(
3514
4148
  `[deleteV2Session] DELETE /api/session/${sessionId} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
@@ -3684,12 +4318,12 @@ async function readV2SessionErrorStream(client, options) {
3684
4318
 
3685
4319
  // src/lib/opencode/credential-import-v2.ts
3686
4320
  import { readFileSync as readFileSync4 } from "node:fs";
3687
- import { join as join4 } from "node:path";
4321
+ import { join as join5 } from "node:path";
3688
4322
 
3689
4323
  // src/lib/openai-usage.ts
3690
4324
  import { readFileSync as readFileSync3 } from "node:fs";
3691
4325
  import { homedir as homedir3 } from "node:os";
3692
- import { join as join3 } from "node:path";
4326
+ import { join as join4 } from "node:path";
3693
4327
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3694
4328
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3695
4329
  var OpenAiUsageError = class extends Error {
@@ -3703,7 +4337,7 @@ function isLocalCredentialProblem2(err) {
3703
4337
  }
3704
4338
  function readOpenCodeChatGptCredentials() {
3705
4339
  try {
3706
- const raw = readFileSync3(join3(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4340
+ const raw = readFileSync3(join4(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3707
4341
  let parsed;
3708
4342
  try {
3709
4343
  parsed = JSON.parse(raw);
@@ -3929,13 +4563,13 @@ var INTEGRATION_LIST_RETRY_DELAY_MS = 1e3;
3929
4563
  function isRecord3(value) {
3930
4564
  return typeof value === "object" && value !== null && !Array.isArray(value);
3931
4565
  }
3932
- var sleep2 = (milliseconds) => new Promise((resolve4) => setTimeout(resolve4, milliseconds));
4566
+ var sleep2 = (milliseconds) => new Promise((resolve5) => setTimeout(resolve5, milliseconds));
3933
4567
  function emptyResult(unsupported = [], outcome = "completed") {
3934
4568
  return { outcome, imported: [], alreadyConnected: [], unsupported, failed: [] };
3935
4569
  }
3936
4570
  function readOpenCodeCredentials(homeDir, log3) {
3937
4571
  try {
3938
- const raw = readFileSync4(join4(homeDir, ...OPENCODE_AUTH_SEGMENTS), "utf8");
4572
+ const raw = readFileSync4(join5(homeDir, ...OPENCODE_AUTH_SEGMENTS), "utf8");
3939
4573
  let parsed;
3940
4574
  try {
3941
4575
  parsed = JSON.parse(raw);
@@ -4137,7 +4771,7 @@ async function importOpenCodeV2Credentials({
4137
4771
  import { spawn as spawn2 } from "node:child_process";
4138
4772
  import { mkdirSync, statSync as statSync3, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
4139
4773
  import { homedir as homedir4 } from "node:os";
4140
- import { dirname as dirname3, resolve as resolvePath } from "node:path";
4774
+ import { dirname as dirname4, resolve as resolvePath } from "node:path";
4141
4775
 
4142
4776
  // src/lib/runner-synchroniser.ts
4143
4777
  import { spawn } from "node:child_process";
@@ -4147,7 +4781,7 @@ function appendError(stderr, error2) {
4147
4781
  ${message}`;
4148
4782
  }
4149
4783
  function runSynchroniser(args, opts) {
4150
- return new Promise((resolve4) => {
4784
+ return new Promise((resolve5) => {
4151
4785
  let child;
4152
4786
  let stdout = "";
4153
4787
  let stderr = "";
@@ -4161,7 +4795,7 @@ function runSynchroniser(args, opts) {
4161
4795
  if (timer.handle) clearTimeout(timer.handle);
4162
4796
  if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
4163
4797
  if (spawnListener) child.removeListener("spawn", spawnListener);
4164
- resolve4(result);
4798
+ resolve5(result);
4165
4799
  };
4166
4800
  try {
4167
4801
  child = spawn("runner-synchroniser", args, {
@@ -4217,9 +4851,12 @@ function runSynchroniser(args, opts) {
4217
4851
 
4218
4852
  // src/lib/opencode/session-db-boot.ts
4219
4853
  var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
4220
- var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
4854
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 36e4;
4221
4855
  var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
4222
- var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
4856
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 40;
4857
+ var SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS = 30;
4858
+ var SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS = 120;
4859
+ var SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS = 90;
4223
4860
  function commandError(result) {
4224
4861
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
4225
4862
  }
@@ -4262,7 +4899,7 @@ function clearMarker(options) {
4262
4899
  function markNoReplicate(options, message) {
4263
4900
  if (options.noReplicateMarker) {
4264
4901
  try {
4265
- mkdirSync(dirname3(options.noReplicateMarker), { recursive: true });
4902
+ mkdirSync(dirname4(options.noReplicateMarker), { recursive: true });
4266
4903
  writeFileSync(options.noReplicateMarker, "");
4267
4904
  } catch (error2) {
4268
4905
  options.log(
@@ -4321,7 +4958,7 @@ function parseSynchroniserEnv(stdout) {
4321
4958
  return values;
4322
4959
  }
4323
4960
  function runCommand(command, args, options) {
4324
- return new Promise((resolve4) => {
4961
+ return new Promise((resolve5) => {
4325
4962
  let child;
4326
4963
  let stdout = "";
4327
4964
  let stderr = "";
@@ -4330,7 +4967,7 @@ function runCommand(command, args, options) {
4330
4967
  if (settled) return;
4331
4968
  settled = true;
4332
4969
  if (timer) clearTimeout(timer);
4333
- resolve4(result);
4970
+ resolve5(result);
4334
4971
  };
4335
4972
  try {
4336
4973
  child = spawn2(command, args, {
@@ -4338,7 +4975,7 @@ function runCommand(command, args, options) {
4338
4975
  stdio: ["ignore", "pipe", "pipe"]
4339
4976
  });
4340
4977
  } catch (error2) {
4341
- resolve4({
4978
+ resolve5({
4342
4979
  code: null,
4343
4980
  stdout,
4344
4981
  stderr: error2 instanceof Error ? error2.message : String(error2),
@@ -4417,7 +5054,7 @@ async function ensureLitestreamConfig(options, env) {
4417
5054
  return null;
4418
5055
  }
4419
5056
  try {
4420
- mkdirSync(dirname3(configPath), { recursive: true });
5057
+ mkdirSync(dirname4(configPath), { recursive: true });
4421
5058
  writeFileSync(configPath, rendered.stdout);
4422
5059
  } catch (error2) {
4423
5060
  options.log(
@@ -4530,20 +5167,37 @@ async function verifySessionDb(options, configPath, env) {
4530
5167
  timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
4531
5168
  env: {
4532
5169
  ...env,
4533
- // The synchroniser reads this value in SECONDS. Keep this at 120, not
4534
- // 120_000, so the walkback gives up before the outer process bound.
5170
+ // The synchroniser reads all four values in SECONDS.
4535
5171
  EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
4536
5172
  SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
5173
+ ),
5174
+ EVIDENT_SESSION_DB_RESTORE_POINTS_TIMEOUT_SECONDS: String(
5175
+ SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS
5176
+ ),
5177
+ EVIDENT_SESSION_DB_RESTORE_CANDIDATE_TIMEOUT_SECONDS: String(
5178
+ SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS
5179
+ ),
5180
+ EVIDENT_SESSION_DB_DISPOSAL_BUDGET_SECONDS: String(
5181
+ SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS
4537
5182
  )
4538
5183
  }
4539
5184
  });
4540
5185
  logSynchroniserDiagnostics(result, options);
4541
5186
  if (result.timedOut || result.code === 124 || result.code === 137) {
5187
+ if (result.stderr.includes("SESSION-DB-INTEGRITY-FAILED")) {
5188
+ const reason = result.stderr.includes("SESSION-DB-REPLICA-SIZE") ? "verification_timeout_during_separation" : "verification_timeout_before_separation";
5189
+ options.log(
5190
+ `SESSION-DB-VERIFY-TIMEOUT: verification did not finish within its ${SESSION_DB_VERIFY_TIMEOUT_MS}ms deadline; refusing to start with the restored opencode.db (${reason})`,
5191
+ "error"
5192
+ );
5193
+ reportRecord("verify", "session_db_boot_refused", reason, null, options);
5194
+ return { verifyFatal: true, verifyFatalReason: reason };
5195
+ }
4542
5196
  options.log(
4543
5197
  `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`,
4544
5198
  "warn"
4545
5199
  );
4546
- return false;
5200
+ return { verifyFatal: false };
4547
5201
  }
4548
5202
  if (result.code === 34) {
4549
5203
  reportRecord(
@@ -4553,14 +5207,14 @@ async function verifySessionDb(options, configPath, env) {
4553
5207
  null,
4554
5208
  options
4555
5209
  );
4556
- return true;
5210
+ return { verifyFatal: true };
4557
5211
  }
4558
5212
  if (result.code === 33) {
4559
5213
  options.log(
4560
5214
  "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
4561
5215
  "warn"
4562
5216
  );
4563
- return false;
5217
+ return { verifyFatal: false };
4564
5218
  }
4565
5219
  if (result.code !== 0) {
4566
5220
  options.log(
@@ -4568,13 +5222,13 @@ async function verifySessionDb(options, configPath, env) {
4568
5222
  "warn"
4569
5223
  );
4570
5224
  }
4571
- return false;
5225
+ return { verifyFatal: false };
4572
5226
  }
4573
5227
  options.log(
4574
5228
  "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
4575
5229
  "debug"
4576
5230
  );
4577
- return false;
5231
+ return { verifyFatal: false };
4578
5232
  }
4579
5233
  function fileExists(path) {
4580
5234
  try {
@@ -4661,13 +5315,13 @@ async function restoreAndVerifySessionDb(options) {
4661
5315
  if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
4662
5316
  return { verifyFatal: false };
4663
5317
  }
4664
- return { verifyFatal: await verifySessionDb(options, configPath, env) };
5318
+ return verifySessionDb(options, configPath, env);
4665
5319
  }
4666
5320
 
4667
5321
  // src/lib/opencode/session-db-provenance.ts
4668
5322
  import { createRequire } from "node:module";
4669
5323
  import { mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "node:fs";
4670
- import { dirname as dirname4, join as join5 } from "node:path";
5324
+ import { dirname as dirname5, join as join6 } from "node:path";
4671
5325
  var require2 = createRequire(import.meta.url);
4672
5326
  function readSessionDbMigrationIds(dbPath) {
4673
5327
  let db;
@@ -4704,7 +5358,7 @@ function readSessionDbMigrationIds(dbPath) {
4704
5358
  }
4705
5359
  function sessionDbProvenanceStatePath(homeDir, env) {
4706
5360
  const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
4707
- return override || join5(homeDir, ".local", "state", "evident", "session-db-provenance.json");
5361
+ return override || join6(homeDir, ".local", "state", "evident", "session-db-provenance.json");
4708
5362
  }
4709
5363
  function loadSessionDbProvenanceState(path) {
4710
5364
  let value;
@@ -4733,7 +5387,7 @@ function loadSessionDbProvenanceState(path) {
4733
5387
  }
4734
5388
  function saveSessionDbProvenanceState(path, state) {
4735
5389
  try {
4736
- mkdirSync2(dirname4(path), { recursive: true });
5390
+ mkdirSync2(dirname5(path), { recursive: true });
4737
5391
  writeFileSync2(path, `${JSON.stringify(state, null, 2)}
4738
5392
  `, "utf8");
4739
5393
  } catch (error2) {
@@ -4824,7 +5478,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
4824
5478
  if (child.exitCode !== null || child.signalCode !== null) {
4825
5479
  return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
4826
5480
  }
4827
- return new Promise((resolve4, reject) => {
5481
+ return new Promise((resolve5, reject) => {
4828
5482
  let forced = false;
4829
5483
  let settled = false;
4830
5484
  const timer = setTimeout(() => {
@@ -4844,7 +5498,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
4844
5498
  settled = true;
4845
5499
  clearTimeout(timer);
4846
5500
  child.removeListener("exit", onExit);
4847
- resolve4(result);
5501
+ resolve5(result);
4848
5502
  };
4849
5503
  const fail = (error2) => {
4850
5504
  if (settled) return;
@@ -5320,7 +5974,7 @@ function selectSessionsToDelete(sessions, opts) {
5320
5974
  );
5321
5975
  }
5322
5976
  var DEFAULT_INTERVAL = "1h";
5323
- function resolve(flag, envValue, fallback) {
5977
+ function resolve2(flag, envValue, fallback) {
5324
5978
  return flag ?? envValue ?? fallback;
5325
5979
  }
5326
5980
  function parseMaxCount(input) {
@@ -5336,9 +5990,9 @@ function parseMaxCount(input) {
5336
5990
  }
5337
5991
  function resolveSessionCleanupConfig(flags, env = process.env) {
5338
5992
  const warnings = [];
5339
- const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
5340
- const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
5341
- const intervalRaw = resolve(
5993
+ const maxAgeRaw = resolve2(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
5994
+ const maxCountRaw = resolve2(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
5995
+ const intervalRaw = resolve2(
5342
5996
  flags.interval,
5343
5997
  env.EVIDENT_SESSION_CLEANUP_INTERVAL,
5344
5998
  DEFAULT_INTERVAL
@@ -5378,10 +6032,10 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
5378
6032
 
5379
6033
  // src/lib/opencode/session-db-size.ts
5380
6034
  import { statSync as statSync4 } from "node:fs";
5381
- import { join as join6 } from "node:path";
6035
+ import { join as join7 } from "node:path";
5382
6036
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
5383
6037
  function statSessionDbBytes(homeDir) {
5384
- const dbPath = join6(homeDir, ".local", "share", "opencode", "opencode.db");
6038
+ const dbPath = join7(homeDir, ".local", "share", "opencode", "opencode.db");
5385
6039
  try {
5386
6040
  return statSync4(dbPath).size;
5387
6041
  } catch (err) {
@@ -5411,12 +6065,12 @@ function buildSessionStoreSizeWarning(input) {
5411
6065
  // src/lib/opencode/log-tail.ts
5412
6066
  import { statSync as statSync5 } from "node:fs";
5413
6067
  import { homedir as homedir5 } from "node:os";
5414
- import { join as join7 } from "node:path";
6068
+ import { join as join8 } from "node:path";
5415
6069
  import { open as open2, stat } from "node:fs/promises";
5416
6070
  var DEFAULT_POLL_INTERVAL_MS = 1e3;
5417
6071
  function resolveOpenCodeLogPath(homeDir = homedir5(), env = process.env) {
5418
- const dataDir = env.XDG_DATA_HOME || join7(homeDir, ".local", "share");
5419
- return join7(dataDir, "opencode", "log", "opencode.log");
6072
+ const dataDir = env.XDG_DATA_HOME || join8(homeDir, ".local", "share");
6073
+ return join8(dataDir, "opencode", "log", "opencode.log");
5420
6074
  }
5421
6075
  function isEnoent(error2) {
5422
6076
  return error2?.code === "ENOENT";
@@ -5571,8 +6225,8 @@ var StreamForwarder = class {
5571
6225
  pauseStream(sid, stream) {
5572
6226
  if (!stream.paused) {
5573
6227
  stream.paused = true;
5574
- stream.pausePromise = new Promise((resolve4) => {
5575
- stream.resume = resolve4;
6228
+ stream.pausePromise = new Promise((resolve5) => {
6229
+ stream.resume = resolve5;
5576
6230
  });
5577
6231
  }
5578
6232
  if (stream.pauseTimer !== null) clearTimeout(stream.pauseTimer);
@@ -5639,12 +6293,12 @@ var StreamForwarder = class {
5639
6293
  let endBody;
5640
6294
  if (has_body) {
5641
6295
  const chunks = [];
5642
- bodyPromise = new Promise((resolve4) => {
6296
+ bodyPromise = new Promise((resolve5) => {
5643
6297
  pushBody = (buf) => {
5644
6298
  chunks.push(buf);
5645
6299
  };
5646
6300
  endBody = () => {
5647
- resolve4(Buffer.concat(chunks));
6301
+ resolve5(Buffer.concat(chunks));
5648
6302
  };
5649
6303
  });
5650
6304
  }
@@ -5815,7 +6469,7 @@ function connectTunnel(options) {
5815
6469
  } = options;
5816
6470
  const tunnelUrl = getTunnelUrlConfig();
5817
6471
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
5818
- return new Promise((resolve4, reject) => {
6472
+ return new Promise((resolve5, reject) => {
5819
6473
  const ws = new WebSocket2(url, {
5820
6474
  headers: {
5821
6475
  Authorization: authHeader,
@@ -5924,7 +6578,7 @@ function connectTunnel(options) {
5924
6578
  forwarder.setBinaryFramesSupported(message.binary_frames === true);
5925
6579
  const connectedAgentId = message.agent_id ?? agentId;
5926
6580
  onConnected?.(connectedAgentId);
5927
- resolve4({
6581
+ resolve5({
5928
6582
  ws,
5929
6583
  close: () => ws.close(1e3, "CLI shutdown")
5930
6584
  });
@@ -6442,6 +7096,7 @@ function createSessionStoreHealth(options = {}) {
6442
7096
  const stalenessMs = Math.max(0, options.stalenessMs ?? SESSION_STORE_HEALTH_STALENESS_MS);
6443
7097
  let lastUnwritableAt = null;
6444
7098
  let lastWritableAt = null;
7099
+ const causes = /* @__PURE__ */ new Map();
6445
7100
  return {
6446
7101
  observeUnwritable: () => {
6447
7102
  lastUnwritableAt = now();
@@ -6453,6 +7108,23 @@ function createSessionStoreHealth(options = {}) {
6453
7108
  if (lastUnwritableAt === null) return false;
6454
7109
  if (lastWritableAt !== null && lastWritableAt >= lastUnwritableAt) return false;
6455
7110
  return now() - lastUnwritableAt < stalenessMs;
7111
+ },
7112
+ observeCause: (sessionId, cause) => {
7113
+ if (sessionId === null) return;
7114
+ causes.delete(sessionId);
7115
+ causes.set(sessionId, { cause, observedAt: now() });
7116
+ while (causes.size > 256) {
7117
+ const oldest = causes.keys().next().value;
7118
+ if (typeof oldest !== "string") break;
7119
+ causes.delete(oldest);
7120
+ }
7121
+ },
7122
+ readCause: (sessionId) => {
7123
+ const observed = causes.get(sessionId);
7124
+ if (!observed) return null;
7125
+ causes.delete(sessionId);
7126
+ if (now() - observed.observedAt >= stalenessMs) return null;
7127
+ return observed.cause;
6456
7128
  }
6457
7129
  };
6458
7130
  }
@@ -6461,12 +7133,12 @@ function createSessionStoreHealth(options = {}) {
6461
7133
  import { homedir as homedir6 } from "node:os";
6462
7134
 
6463
7135
  // src/lib/runner-file-sync.ts
6464
- import { join as join9 } from "node:path";
7136
+ import { join as join10 } from "node:path";
6465
7137
 
6466
7138
  // src/lib/file-push.ts
6467
7139
  import { randomUUID } from "node:crypto";
6468
7140
  import { chmod, mkdir, open as open3, realpath, rename, unlink } from "node:fs/promises";
6469
- import { basename, dirname as dirname5, isAbsolute, join as join8, relative, resolve as resolve2, sep } from "node:path";
7141
+ import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute2, join as join9, relative as relative2, resolve as resolve3, sep as sep2 } from "node:path";
6470
7142
  var FILE_MODE = 384;
6471
7143
  var DIRECTORY_MODE = 448;
6472
7144
  async function writePushedFile(request2) {
@@ -6497,9 +7169,9 @@ async function writePushedFile(request2) {
6497
7169
  }
6498
7170
  try {
6499
7171
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
6500
- dirname5(candidate)
7172
+ dirname6(candidate)
6501
7173
  );
6502
- const realTarget = join8(existingAncestor, ...missingSegments, basename(candidate));
7174
+ const realTarget = join9(existingAncestor, ...missingSegments, basename2(candidate));
6503
7175
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
6504
7176
  if (allowedDirectory === null) {
6505
7177
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -6509,8 +7181,8 @@ async function writePushedFile(request2) {
6509
7181
  }
6510
7182
  if (missingSegments.length > 0) {
6511
7183
  await createMissingDirectories(existingAncestor, missingSegments);
6512
- const realParent = await realpath(dirname5(realTarget));
6513
- if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
7184
+ const realParent = await realpath(dirname6(realTarget));
7185
+ if (realParent !== dirname6(realTarget) || !contains(allowedDirectory, realTarget)) {
6514
7186
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
6515
7187
  path: realTarget,
6516
7188
  bytes,
@@ -6535,15 +7207,15 @@ function expandAndValidate(requestedPath, homeDir) {
6535
7207
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
6536
7208
  return null;
6537
7209
  }
6538
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
7210
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6539
7211
  if (expanded.split(/[/\\]/).includes("..")) {
6540
7212
  return null;
6541
7213
  }
6542
- if (!isAbsolute(expanded)) {
7214
+ if (!isAbsolute2(expanded)) {
6543
7215
  return null;
6544
7216
  }
6545
- const candidate = resolve2(expanded);
6546
- const name = basename(candidate);
7217
+ const candidate = resolve3(expanded);
7218
+ const name = basename2(candidate);
6547
7219
  return name === "" || name === "." || name === ".." ? null : candidate;
6548
7220
  }
6549
7221
  async function resolveNearestExistingAncestor(directory) {
@@ -6553,18 +7225,18 @@ async function resolveNearestExistingAncestor(directory) {
6553
7225
  try {
6554
7226
  return { existingAncestor: await realpath(current), missingSegments };
6555
7227
  } catch (err) {
6556
- const parent = dirname5(current);
7228
+ const parent = dirname6(current);
6557
7229
  if (err.code !== "ENOENT" || parent === current) {
6558
7230
  throw err;
6559
7231
  }
6560
- missingSegments.unshift(basename(current));
7232
+ missingSegments.unshift(basename2(current));
6561
7233
  current = parent;
6562
7234
  }
6563
7235
  }
6564
7236
  }
6565
7237
  async function findContainingAllowedDirectory(allowedDirectories, realTarget) {
6566
7238
  for (const directory of allowedDirectories) {
6567
- if (!isAbsolute(directory)) {
7239
+ if (!isAbsolute2(directory)) {
6568
7240
  log("warn", "file_push_allowed_directory_skipped", { directory, reason: "not_absolute" });
6569
7241
  continue;
6570
7242
  }
@@ -6602,19 +7274,19 @@ async function realpathCreatingIfMissing(directory) {
6602
7274
  }
6603
7275
  }
6604
7276
  function contains(realDirectory, realTarget) {
6605
- const rel = relative(realDirectory, realTarget);
6606
- return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
7277
+ const rel = relative2(realDirectory, realTarget);
7278
+ return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep2}`) && !isAbsolute2(rel);
6607
7279
  }
6608
7280
  async function createMissingDirectories(existingAncestor, missingSegments) {
6609
7281
  let current = existingAncestor;
6610
7282
  for (const segment of missingSegments) {
6611
- current = join8(current, segment);
7283
+ current = join9(current, segment);
6612
7284
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
6613
7285
  await chmod(current, DIRECTORY_MODE);
6614
7286
  }
6615
7287
  }
6616
7288
  async function writeAtomically(realTarget, content) {
6617
- const temporaryPath = join8(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
7289
+ const temporaryPath = join9(dirname6(realTarget), `.evident-push-${randomUUID()}.tmp`);
6618
7290
  let handle;
6619
7291
  try {
6620
7292
  handle = await open3(temporaryPath, "wx", FILE_MODE);
@@ -6744,12 +7416,12 @@ var NOT_APPLIED = {
6744
7416
  opencodeAuthApplied: false
6745
7417
  };
6746
7418
  function isClaudeCredentialPath(requestedPath, homeDir) {
6747
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6748
- return expanded === join9(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
7419
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join10(homeDir, requestedPath.slice(2)) : requestedPath;
7420
+ return expanded === join10(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
6749
7421
  }
6750
7422
  function isOpenCodeAuthPath(requestedPath, homeDir) {
6751
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6752
- return expanded === join9(homeDir, ...OPENCODE_AUTH_SEGMENTS);
7423
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join10(homeDir, requestedPath.slice(2)) : requestedPath;
7424
+ return expanded === join10(homeDir, ...OPENCODE_AUTH_SEGMENTS);
6753
7425
  }
6754
7426
  async function applyOne(options, file) {
6755
7427
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -6893,6 +7565,11 @@ function messageIdOf(m) {
6893
7565
  const infoId = m.info?.id;
6894
7566
  return typeof infoId === "string" ? infoId : void 0;
6895
7567
  }
7568
+ function composeAbandonmentNote(finish, replyShape, outcome) {
7569
+ const base = `OpenCode ended this turn after a tool call (finish="${finish ?? "(unknown)"}") ` + (outcome === "without-answer" ? "without writing an answer, and confirmed it is no longer working on this conversation. Evident is reporting the run as failed so the failure and its reason are visible in failed runs." : "after writing only part of an answer, and confirmed it is no longer working on this conversation. Evident delivered what was written and is reporting the run as finished.");
7570
+ if (replyShape === null) return base;
7571
+ return `${base} ${replyShape.lastToolName === null ? "No tool call was recorded" : `The last tool call was "${replyShape.lastToolName}" with status "${replyShape.lastToolStatus ?? "(unknown)"}"`}; the reply's final part types were [${replyShape.partTypes.join(", ")}]${replyShape.droppedPartCount > 0 ? ` (${replyShape.droppedPartCount} earlier part types omitted)` : ""}.`;
7572
+ }
6896
7573
  function cleanImageMime(contentType) {
6897
7574
  if (!contentType) return null;
6898
7575
  const media = contentType.split(";")[0].trim().toLowerCase();
@@ -6913,6 +7590,10 @@ var SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS = 2e3;
6913
7590
  var SESSION_ERROR_STREAM_HEALTHY_MS = 5e3;
6914
7591
  var SESSION_ERROR_BUFFER_TTL_MS = SESSION_ERROR_STREAM_HEALTHY_MS;
6915
7592
  var MAX_BUFFERED_SESSION_ERRORS = 256;
7593
+ var SESSION_ERROR_RETENTION_TTL_MS = 6e4;
7594
+ var SESSION_STORE_UNWRITABLE_ERROR = "The runner session database could not be written, so this turn did not finish. Nothing was lost from the queue. Clear disk space and retry.";
7595
+ var SESSION_ERROR_RETAINED_DETAIL = "Runner-observed OpenCode session error: ";
7596
+ var SESSION_ERROR_OBSERVED_CAUSE = "Runner-observed OpenCode session-store cause: ";
6916
7597
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
6917
7598
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
6918
7599
  var DRIVER_SESSION_ENUMERATION_LIMIT = 20;
@@ -7070,6 +7751,7 @@ var ChannelDriver = class _ChannelDriver {
7070
7751
  * with a short TTL so an unmatched session cannot retain an event indefinitely.
7071
7752
  */
7072
7753
  bufferedSessionErrors = /* @__PURE__ */ new Map();
7754
+ retainedSessionErrors = /* @__PURE__ */ new Map();
7073
7755
  /**
7074
7756
  * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
7075
7757
  * dispatched and are still in-flight. A message in this set is never
@@ -7102,10 +7784,18 @@ var ChannelDriver = class _ChannelDriver {
7102
7784
  * row is `processing` or `pending`; clear it only when absent from both lists.
7103
7785
  */
7104
7786
  untrackableAck = /* @__PURE__ */ new Map();
7787
+ /** Session binding used by the first untrackable-ack report, for retries. */
7788
+ untrackableAckSessionIds = /* @__PURE__ */ new Map();
7105
7789
  /** Messages whose untrackable-ack failure signal was emitted while fenced. */
7106
7790
  untrackableAckSignalled = /* @__PURE__ */ new Set();
7107
7791
  /** Messages whose first untrackable-ack fence warning was already logged. */
7108
7792
  untrackableAckWarned = /* @__PURE__ */ new Set();
7793
+ /**
7794
+ * Accepted V1 prompts whose assigned ids were not read back; these rows are
7795
+ * re-read instead of POSTed again. A null snapshot cannot safely exclude an older
7796
+ * identical-text turn, so it never produces a correlation.
7797
+ */
7798
+ awaitingCorrelation = /* @__PURE__ */ new Map();
7109
7799
  /** Pending rows seen in the current drain, used to retain terminal dispatch fences. */
7110
7800
  pendingMessageIds = /* @__PURE__ */ new Set();
7111
7801
  /**
@@ -7191,21 +7881,6 @@ var ChannelDriver = class _ChannelDriver {
7191
7881
  * re-signal on every one of the 15h of re-dispatch attempts #1110 made.
7192
7882
  */
7193
7883
  dispatchNotStartedSignalled = /* @__PURE__ */ new Map();
7194
- /**
7195
- * Consecutive-UNCONFIRMED-dispatch streak for a `pending` row with NO stored
7196
- * `opencode_message_id` yet — i.e. one that has never even reached the
7197
- * re-drive fence above. `sendPromptAsync`'s POST may 2xx, but its own
7198
- * read-back retries can never confirm the assigned id when the session's
7199
- * message list is PERMANENTLY unreadable (e.g. a corrupted local opencode
7200
- * SQLite DB, #1345/#1348's exact fault, just hit BEFORE the row is ever
7201
- * dispatched instead of after). Unlike an already-dispatched row, THIS row has
7202
- * no other safety net at all: the lifecycle cron only reclaims `status =
7203
- * 'processing'` rows, and a row stuck here never reaches `processing`. Keyed
7204
- * by message id, carrying `sessionId` so a session change (a fresh one bound
7205
- * after abandonment) starts a new streak rather than inheriting the old
7206
- * session's count — same shape as `redrivePollFailures` above.
7207
- */
7208
- unconfirmedDispatchFailures = /* @__PURE__ */ new Map();
7209
7884
  /**
7210
7885
  * Consecutive give-ups for a message whose OpenCode user row remains queued.
7211
7886
  * The session id scopes the count: a fresh session is a new attempt after a
@@ -7223,8 +7898,8 @@ var ChannelDriver = class _ChannelDriver {
7223
7898
  * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
7224
7899
  * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
7225
7900
  * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
7226
- * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
7227
- * so the NEXT tick may retry exactly once more).
7901
+ * re-entry) OR on a failed/unresolved dispatch (the accepted prompt is handed to
7902
+ * the terminal-report fence and is never sent again).
7228
7903
  */
7229
7904
  awaitingReadopt = /* @__PURE__ */ new Set();
7230
7905
  /**
@@ -7542,6 +8217,49 @@ var ChannelDriver = class _ChannelDriver {
7542
8217
  }
7543
8218
  return ids;
7544
8219
  }
8220
+ /** Find an accepted prompt's user row without adopting an older or concurrent turn. */
8221
+ findAwaitingCorrelationId(entry, sessionId, messages) {
8222
+ if (entry.sessionId !== sessionId || entry.knownUserIds === null) return null;
8223
+ const excludeIds = new Set(entry.knownUserIds);
8224
+ for (const inFlight of this.watchers.get(entry.sessionId)?.inFlight.values() ?? []) {
8225
+ excludeIds.add(inFlight.opencodeMessageId);
8226
+ }
8227
+ return findDispatchedUserMessageId(messages, entry.content, excludeIds);
8228
+ }
8229
+ /** Hand an exhausted accepted-but-untrackable prompt to the terminal-report fence. */
8230
+ async handoffUntrackableCorrelation(conversationId, messageId, sessionId, attempts, source) {
8231
+ this.awaitingCorrelation.delete(messageId);
8232
+ this.awaitingReadopt.delete(messageId);
8233
+ this.sessions.delete(conversationId);
8234
+ this.supersede(conversationId, sessionId);
8235
+ const dispatchDescription = source === "readopt" ? "re-dispatched message" : "message";
8236
+ const errorMessage3 = `OpenCode accepted this ${dispatchDescription} once, but its assigned id was never confirmed after ${attempts} attempts to match it up \u2014 the message was deliberately not sent again, so no duplicate turn was created; session ${sessionId.slice(0, 8)} was abandoned; a fresh one is used for further messages.`;
8237
+ this.untrackableAck.set(messageId, errorMessage3);
8238
+ this.untrackableAckSessionIds.set(messageId, null);
8239
+ this.log({
8240
+ level: "error",
8241
+ message: errorMessage3,
8242
+ conversation_id: conversationId,
8243
+ message_id: messageId
8244
+ });
8245
+ const reportError = await this.reportUntrackableAck(
8246
+ conversationId,
8247
+ messageId,
8248
+ null,
8249
+ errorMessage3
8250
+ );
8251
+ if (reportError !== null) {
8252
+ this.log({
8253
+ level: "warn",
8254
+ message: `markFailed PATCH for message ${messageId.slice(0, 8)} (conversation ${conversationId.slice(0, 8)}) failed: ${reportError.message}`,
8255
+ conversation_id: conversationId,
8256
+ message_id: messageId
8257
+ });
8258
+ }
8259
+ if (source === "readopt") {
8260
+ void this.postSignal(conversationId, messageId, "readopt_orphan_unsent");
8261
+ }
8262
+ }
7545
8263
  /**
7546
8264
  * File-pull work, for `run.ts`'s idle accounting (#559).
7547
8265
  *
@@ -7721,6 +8439,56 @@ var ChannelDriver = class _ChannelDriver {
7721
8439
  }
7722
8440
  break;
7723
8441
  }
8442
+ const correlation = this.awaitingCorrelation.get(message.id);
8443
+ if (correlation) {
8444
+ correlation.attempts += 1;
8445
+ let correlatedId = null;
8446
+ if (correlation.knownUserIds !== null && correlation.sessionId === sessionId) {
8447
+ const polled = await this.pollSessionMessagesForRedrive(
8448
+ conv,
8449
+ message,
8450
+ correlation.sessionId
8451
+ );
8452
+ if (polled.ok)
8453
+ correlatedId = this.findAwaitingCorrelationId(correlation, sessionId, polled.messages);
8454
+ } else if (correlation.knownUserIds === null) {
8455
+ this.log({
8456
+ level: "debug",
8457
+ message: `Message ${message.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
8458
+ conversation_id: conv.id,
8459
+ message_id: message.id
8460
+ });
8461
+ }
8462
+ if (correlatedId !== null) {
8463
+ this.awaitingCorrelation.delete(message.id);
8464
+ this.dispatchNotStartedSignalled.delete(message.id);
8465
+ this.releasedOpencodeIds.delete(message.id);
8466
+ this.dispatched.add(message.id);
8467
+ this.registerInFlight(conv, correlation.sessionId, message, correlatedId);
8468
+ dispatched += 1;
8469
+ this.log({
8470
+ level: "info",
8471
+ message: `Matched accepted message ${message.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
8472
+ conversation_id: conv.id,
8473
+ message_id: message.id
8474
+ });
8475
+ void this.postSignal(conv.id, message.id, "ack_correlated", {
8476
+ opencode_message_id: correlatedId
8477
+ });
8478
+ continue;
8479
+ }
8480
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
8481
+ await this.handoffUntrackableCorrelation(
8482
+ conv.id,
8483
+ message.id,
8484
+ correlation.sessionId,
8485
+ correlation.attempts,
8486
+ "fresh"
8487
+ );
8488
+ break;
8489
+ }
8490
+ break;
8491
+ }
7724
8492
  const effectiveOpencodeMessageId = message.opencode_message_id ?? this.releasedOpencodeIds.get(message.id)?.opencodeMessageId ?? null;
7725
8493
  if (effectiveOpencodeMessageId) {
7726
8494
  const outcome = await this.resolveRedrive(
@@ -7742,6 +8510,7 @@ var ChannelDriver = class _ChannelDriver {
7742
8510
  model: message.opencode_model ?? void 0
7743
8511
  };
7744
8512
  let opencodeMessageId;
8513
+ let preDispatchUserIds = null;
7745
8514
  try {
7746
8515
  this.log({
7747
8516
  level: "info",
@@ -7771,7 +8540,20 @@ var ChannelDriver = class _ChannelDriver {
7771
8540
  message.content,
7772
8541
  options,
7773
8542
  sendAttachments,
7774
- this.openCodeClient
8543
+ this.openCodeClient,
8544
+ {
8545
+ onKnownUserIds: (ids) => {
8546
+ preDispatchUserIds = ids;
8547
+ },
8548
+ onKnownUserIdsError: (error2) => {
8549
+ this.log({
8550
+ level: "warn",
8551
+ message: `Pre-dispatch snapshot callback failed for message ${message.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
8552
+ conversation_id: conv.id,
8553
+ message_id: message.id
8554
+ });
8555
+ }
8556
+ }
7775
8557
  )
7776
8558
  );
7777
8559
  } catch (err) {
@@ -7779,6 +8561,7 @@ var ChannelDriver = class _ChannelDriver {
7779
8561
  if (this.isV2 && err instanceof OpenCodeV2PromptAckError) {
7780
8562
  const errorMessage4 = err instanceof Error ? err.message : String(err);
7781
8563
  this.untrackableAck.set(message.id, errorMessage4);
8564
+ this.untrackableAckSessionIds.set(message.id, null);
7782
8565
  this.log({
7783
8566
  level: "error",
7784
8567
  message: `V2 prompt dispatch for message ${message.id.slice(0, 8)} failed after a positive ack with no usable id: ${errorMessage4}`,
@@ -7854,39 +8637,21 @@ var ChannelDriver = class _ChannelDriver {
7854
8637
  if (this.isV2) {
7855
8638
  throw new Error("V2 prompt dispatch completed without an acknowledged message id");
7856
8639
  }
7857
- const streak = this.recordUnconfirmedDispatch(message.id, sessionId);
7858
- if (streak < MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
7859
- this.log({
7860
- level: "warn",
7861
- message: `Message ${message.id.slice(0, 8)} dispatched but its opencode id could not be read back (${streak}/${MAX_IDENTICAL_REDRIVE_POLL_FAILURES}) \u2014 leaving un-tracked to retry next tick`,
7862
- conversation_id: conv.id,
7863
- message_id: message.id
7864
- });
7865
- this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7866
- continue;
7867
- }
7868
- this.unconfirmedDispatchFailures.delete(message.id);
7869
- this.sessions.delete(conv.id);
7870
- this.supersede(conv.id, sessionId);
7871
- const errorMessage3 = `OpenCode accepted this message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
8640
+ this.awaitingCorrelation.set(message.id, {
8641
+ sessionId,
8642
+ content: message.content,
8643
+ attempts: 0,
8644
+ knownUserIds: preDispatchUserIds
8645
+ });
7872
8646
  this.log({
7873
- level: "error",
7874
- message: errorMessage3,
8647
+ level: "warn",
8648
+ message: `OpenCode accepted message ${message.id.slice(0, 8)}, but its assigned id could not be read back \u2014 the message will not be sent again while the runner matches it to the turn already running`,
7875
8649
  conversation_id: conv.id,
7876
8650
  message_id: message.id
7877
8651
  });
7878
- await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
7879
- this.log({
7880
- level: "warn",
7881
- message: `markFailed PATCH for message ${message.id.slice(0, 8)} (conversation ${conv.id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
7882
- conversation_id: conv.id,
7883
- message_id: message.id
7884
- });
7885
- this.signalDispatchNotStarted(conv, message, "abandon_unreported");
7886
- });
8652
+ this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7887
8653
  break;
7888
8654
  }
7889
- this.unconfirmedDispatchFailures.delete(message.id);
7890
8655
  this.dispatchNotStartedSignalled.delete(message.id);
7891
8656
  this.releasedOpencodeIds.delete(message.id);
7892
8657
  this.dispatched.add(message.id);
@@ -8491,23 +9256,6 @@ var ChannelDriver = class _ChannelDriver {
8491
9256
  this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
8492
9257
  return 1;
8493
9258
  }
8494
- /**
8495
- * Record one UNCONFIRMED-dispatch outcome (a `pending` row with no stored
8496
- * `opencode_message_id` whose `sendPromptAsync` returned `null`) toward the
8497
- * bound in `processConversation`'s dispatch loop, and return the resulting
8498
- * count. Mirrors `recordRedrivePollFailure`'s session-scoping: a session
8499
- * change starts a fresh streak at `1` rather than inheriting the old one's
8500
- * count, since a new session is a genuinely different attempt.
8501
- */
8502
- recordUnconfirmedDispatch(messageId, sessionId) {
8503
- const existing = this.unconfirmedDispatchFailures.get(messageId);
8504
- if (existing && existing.sessionId === sessionId) {
8505
- existing.count += 1;
8506
- return existing.count;
8507
- }
8508
- this.unconfirmedDispatchFailures.set(messageId, { sessionId, count: 1 });
8509
- return 1;
8510
- }
8511
9259
  /** Record one queued give-up toward the never-started bound. */
8512
9260
  recordNeverStartedAttempt(messageId, sessionId) {
8513
9261
  const existing = this.neverStartedAttempts.get(messageId);
@@ -8844,6 +9592,28 @@ var ChannelDriver = class _ChannelDriver {
8844
9592
  this.bufferedSessionErrors.delete(oldest);
8845
9593
  }
8846
9594
  }
9595
+ retainSessionError(event) {
9596
+ this.retainedSessionErrors.delete(event.sessionId);
9597
+ this.retainedSessionErrors.set(event.sessionId, {
9598
+ reason: event.reason,
9599
+ observedAt: this.now()
9600
+ });
9601
+ while (this.retainedSessionErrors.size > MAX_BUFFERED_SESSION_ERRORS) {
9602
+ const oldest = this.retainedSessionErrors.keys().next().value;
9603
+ if (typeof oldest !== "string") break;
9604
+ this.retainedSessionErrors.delete(oldest);
9605
+ }
9606
+ }
9607
+ readRetainedSessionError(sessionId) {
9608
+ const retained = this.retainedSessionErrors.get(sessionId);
9609
+ if (!retained) return null;
9610
+ if (this.now() - retained.observedAt >= SESSION_ERROR_RETENTION_TTL_MS) {
9611
+ this.retainedSessionErrors.delete(sessionId);
9612
+ return null;
9613
+ }
9614
+ this.retainedSessionErrors.delete(sessionId);
9615
+ return retained.reason;
9616
+ }
8847
9617
  /**
8848
9618
  * Build a fresh `SessionWatcher`. Seeds `lastTickAt`/`lastObservedTickAt`
8849
9619
  * EQUAL (#1618) so a watcher whose loop has not started ticking yet is never
@@ -9170,9 +9940,10 @@ var ChannelDriver = class _ChannelDriver {
9170
9940
  return;
9171
9941
  }
9172
9942
  if ([...watcher.inFlight.values()].some((message) => message.started && !message.done)) {
9943
+ this.retainSessionError(event);
9173
9944
  this.log({
9174
- level: "debug",
9175
- message: `A turn is already running in session ${event.sessionId.slice(0, 8)} \u2014 deferring to transcript polling`,
9945
+ level: "warn",
9946
+ message: `A turn is already running in session ${event.sessionId.slice(0, 8)} \u2014 retaining the session error and deferring to transcript polling: ${event.reason}`,
9176
9947
  conversation_id: watcher.conv.id
9177
9948
  });
9178
9949
  return;
@@ -9202,9 +9973,10 @@ var ChannelDriver = class _ChannelDriver {
9202
9973
  const messages = await this.getSessionMessages(event.sessionId);
9203
9974
  const state = messageRunState(messages, inFlight.opencodeMessageId);
9204
9975
  if (state !== "queued") {
9976
+ this.retainSessionError(event);
9205
9977
  this.log({
9206
- level: "debug",
9207
- message: `Session error for message ${inFlight.evidentMessageId.slice(0, 8)} observed state ${state}; leaving it to transcript polling`,
9978
+ level: "warn",
9979
+ message: `Session error for message ${inFlight.evidentMessageId.slice(0, 8)} in session ${event.sessionId.slice(0, 8)} observed state ${state}; retaining the reason for transcript polling: ${event.reason}`,
9208
9980
  conversation_id: watcher.conv.id,
9209
9981
  message_id: inFlight.evidentMessageId
9210
9982
  });
@@ -9216,14 +9988,53 @@ var ChannelDriver = class _ChannelDriver {
9216
9988
  conversation_id: watcher.conv.id,
9217
9989
  message_id: inFlight.evidentMessageId
9218
9990
  });
9219
- await this.markFailed(
9220
- watcher.conv.id,
9221
- inFlight.evidentMessageId,
9222
- event.sessionId,
9223
- `OpenCode could not run this turn: ${event.reason}`
9224
- );
9991
+ const isMissingProvider = isProviderModelNotFoundError(event.reason);
9992
+ if (isMissingProvider) {
9993
+ const dispatchedModel = inFlight.message.opencode_model;
9994
+ const separator = dispatchedModel?.indexOf("/") ?? -1;
9995
+ const hasProviderAndModel = dispatchedModel !== null && separator > 0 && separator < dispatchedModel.length - 1;
9996
+ await this.markFailed(
9997
+ watcher.conv.id,
9998
+ inFlight.evidentMessageId,
9999
+ event.sessionId,
10000
+ `OpenCode could not run this turn: ${event.reason}`,
10001
+ void 0,
10002
+ {
10003
+ classification: {
10004
+ kind: "model_auth",
10005
+ providerId: hasProviderAndModel ? dispatchedModel.slice(0, separator) : null,
10006
+ modelId: hasProviderAndModel ? dispatchedModel.slice(separator + 1) : null,
10007
+ reason: "missing"
10008
+ },
10009
+ errorShape: { name: null, statusCode: null }
10010
+ }
10011
+ );
10012
+ } else {
10013
+ await this.markFailed(
10014
+ watcher.conv.id,
10015
+ inFlight.evidentMessageId,
10016
+ event.sessionId,
10017
+ `OpenCode could not run this turn: ${event.reason}`
10018
+ );
10019
+ }
9225
10020
  inFlight.done = true;
9226
10021
  this.removeInFlight(watcher, inFlight.evidentMessageId);
10022
+ if (isMissingProvider) {
10023
+ if (this.isV2) {
10024
+ void this.postSignal(
10025
+ watcher.conv.id,
10026
+ inFlight.evidentMessageId,
10027
+ "provider_registry_reload_unsupported"
10028
+ );
10029
+ } else {
10030
+ await disposeAndReloadProviderConfig(this.port, this.openCodeClient);
10031
+ void this.postSignal(
10032
+ watcher.conv.id,
10033
+ inFlight.evidentMessageId,
10034
+ "provider_registry_reload_issued"
10035
+ );
10036
+ }
10037
+ }
9227
10038
  } catch (err) {
9228
10039
  if (err instanceof ChannelAuthError) {
9229
10040
  this.log({
@@ -9433,7 +10244,16 @@ var ChannelDriver = class _ChannelDriver {
9433
10244
  if (state === "failed") {
9434
10245
  this.anchorDeliveryDeadline(inFlight);
9435
10246
  if (!inFlight.done) {
9436
- const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
10247
+ const messageErrorText = messageError(messages, inFlight.opencodeMessageId);
10248
+ const retainedReason = this.readRetainedSessionError(sessionId);
10249
+ const retainedError = retainedReason === null || messageErrorText !== null && messageErrorText.includes(retainedReason) ? null : `${SESSION_ERROR_RETAINED_DETAIL}${retainedReason}`;
10250
+ const observedCause = this.sessionStoreHealth?.readCause(sessionId);
10251
+ const causeError = observedCause === null || observedCause === void 0 ? null : `${SESSION_ERROR_OBSERVED_CAUSE}${observedCause}`;
10252
+ const storeError = this.sessionStoreHealth?.isUnwritable() ? SESSION_STORE_UNWRITABLE_ERROR : null;
10253
+ const runnerDetails = [retainedError, causeError, storeError].filter(
10254
+ (detail) => detail !== null
10255
+ );
10256
+ const error2 = messageErrorText === null ? runnerDetails.length === 0 ? void 0 : runnerDetails.join(" ") : [messageErrorText, ...runnerDetails].join(" ");
9437
10257
  this.log({
9438
10258
  level: "error",
9439
10259
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
@@ -9523,17 +10343,28 @@ var ChannelDriver = class _ChannelDriver {
9523
10343
  }
9524
10344
  } else {
9525
10345
  if (inFlight.b2AbandonedSignalled) {
10346
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
10347
+ const finish = reply?.info?.finish ?? reply?.finish;
9526
10348
  if (inFlight.b2AbandonedWithoutAnswer) {
9527
- const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
9528
10349
  await this.settleMessageAbandonedWithoutAnswer(
9529
10350
  sessionId,
9530
10351
  watcher,
9531
10352
  inFlight,
9532
10353
  messages,
9533
- reply?.info?.finish ?? reply?.finish
10354
+ finish
10355
+ );
10356
+ } else {
10357
+ await this.settleMessageDone(
10358
+ sessionId,
10359
+ watcher,
10360
+ inFlight,
10361
+ messages,
10362
+ composeAbandonmentNote(
10363
+ finish,
10364
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10365
+ "with-answer"
10366
+ )
9534
10367
  );
9535
- } else {
9536
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
9537
10368
  }
9538
10369
  return;
9539
10370
  }
@@ -9596,7 +10427,17 @@ var ChannelDriver = class _ChannelDriver {
9596
10427
  ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
9597
10428
  opencode_message_id: inFlight.opencodeMessageId
9598
10429
  });
9599
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
10430
+ await this.settleMessageDone(
10431
+ sessionId,
10432
+ watcher,
10433
+ inFlight,
10434
+ messages,
10435
+ composeAbandonmentNote(
10436
+ finish,
10437
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10438
+ "with-answer"
10439
+ )
10440
+ );
9600
10441
  return;
9601
10442
  }
9602
10443
  if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
@@ -9796,15 +10637,11 @@ var ChannelDriver = class _ChannelDriver {
9796
10637
  }
9797
10638
  }
9798
10639
  /**
9799
- * Settle a message whose run-state has resolved `'done'` — extracted verbatim
9800
- * (pure refactor, no behavior change) from `serviceInFlightMessage`'s former
9801
- * inline `state === 'done'` branch body, so a SECOND caller (the #721
9802
- * b2-abandonment resolution) can reach the exact same completion behavior
9803
- * (delivery-deadline anchoring, title resolution, usage extraction, and
9804
- * `markDone`'s auth/terminal/transient-retry discipline) without duplicating it
9805
- * and risking the two copies silently drifting apart.
10640
+ * Settle a message whose run-state has resolved `'done'`. Normal completion and
10641
+ * confirmed b2 abandonment share the same delivery, usage, and terminal handling.
10642
+ * Normal completions omit the optional terminal diagnostic.
9806
10643
  */
9807
- async settleMessageDone(sessionId, watcher, inFlight, messages) {
10644
+ async settleMessageDone(sessionId, watcher, inFlight, messages, note) {
9808
10645
  const conv = watcher.conv;
9809
10646
  this.anchorDeliveryDeadline(inFlight);
9810
10647
  if (!inFlight.done) {
@@ -9831,7 +10668,8 @@ var ChannelDriver = class _ChannelDriver {
9831
10668
  title,
9832
10669
  usage,
9833
10670
  usageAgentName,
9834
- subagentInvocations
10671
+ subagentInvocations,
10672
+ note
9835
10673
  );
9836
10674
  } catch (err) {
9837
10675
  if (err instanceof ChannelAuthError) throw err;
@@ -9876,7 +10714,11 @@ var ChannelDriver = class _ChannelDriver {
9876
10714
  /** Report a confirmed b2 abandonment that produced no answer text. */
9877
10715
  async settleMessageAbandonedWithoutAnswer(sessionId, watcher, inFlight, messages, finish) {
9878
10716
  const conv = watcher.conv;
9879
- const error2 = `OpenCode ended this turn after a tool call (finish="${finish ?? "(unknown)"}") without writing an answer, and confirmed it is no longer working on this conversation. Evident is reporting the run as failed so the failure and its reason are visible in failed runs.`;
10717
+ const error2 = composeAbandonmentNote(
10718
+ finish,
10719
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10720
+ "without-answer"
10721
+ );
9880
10722
  this.anchorDeliveryDeadline(inFlight);
9881
10723
  this.log({
9882
10724
  level: "warn",
@@ -10003,7 +10845,7 @@ var ChannelDriver = class _ChannelDriver {
10003
10845
  */
10004
10846
  async settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight) {
10005
10847
  const conv = watcher.conv;
10006
- const error2 = "The runner session database could not be written, so this turn did not finish. Nothing was lost from the queue. Clear disk space and retry.";
10848
+ const error2 = SESSION_STORE_UNWRITABLE_ERROR;
10007
10849
  this.log({
10008
10850
  level: "error",
10009
10851
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was not resolved as done: ${error2}`,
@@ -10094,11 +10936,22 @@ var ChannelDriver = class _ChannelDriver {
10094
10936
  }
10095
10937
  }
10096
10938
  }
10097
- if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0) {
10939
+ if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0 || this.awaitingCorrelation.size > 0) {
10098
10940
  const stillProcessing = new Set(rows.map((r) => r.id));
10941
+ for (const id of this.awaitingCorrelation.keys()) {
10942
+ if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
10943
+ this.awaitingCorrelation.delete(id);
10944
+ this.log({
10945
+ level: "debug",
10946
+ message: `Re-adopt: message ${id.slice(0, 8)} left processing and pending \u2014 cleared awaiting-correlation fence`,
10947
+ message_id: id
10948
+ });
10949
+ }
10950
+ }
10099
10951
  for (const id of [...this.untrackableAck.keys()]) {
10100
10952
  if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
10101
10953
  this.untrackableAck.delete(id);
10954
+ this.untrackableAckSessionIds.delete(id);
10102
10955
  this.untrackableAckSignalled.delete(id);
10103
10956
  this.untrackableAckWarned.delete(id);
10104
10957
  this.log({
@@ -10138,7 +10991,7 @@ var ChannelDriver = class _ChannelDriver {
10138
10991
  bySession.set(row.opencode_session_id, list);
10139
10992
  }
10140
10993
  for (const [sessionId, sessionRows] of bySession) {
10141
- let messages;
10994
+ let messages = null;
10142
10995
  try {
10143
10996
  if (this.isV2) {
10144
10997
  const snapshot = await this.getSessionMessages(sessionId);
@@ -10147,9 +11000,9 @@ var ChannelDriver = class _ChannelDriver {
10147
11000
  level: "warn",
10148
11001
  message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned an unreadable message snapshot \u2014 skipping this session this tick`
10149
11002
  });
10150
- continue;
11003
+ } else {
11004
+ messages = snapshot;
10151
11005
  }
10152
- messages = snapshot;
10153
11006
  } else {
10154
11007
  const polled = await pollSessionMessagesForRedrive(
10155
11008
  this.port,
@@ -10162,15 +11015,22 @@ var ChannelDriver = class _ChannelDriver {
10162
11015
  level: "warn",
10163
11016
  message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned ${polled.malformed ? "a non-array message body" : `HTTP ${polled.status ?? "a network error"}${normalized ? `: ${normalized}` : ""}`} \u2014 skipping this session this tick`
10164
11017
  });
10165
- continue;
11018
+ } else {
11019
+ messages = polled.messages;
10166
11020
  }
10167
- messages = polled.messages;
10168
11021
  }
10169
11022
  } catch (err) {
10170
11023
  this.log({
10171
11024
  level: "warn",
10172
11025
  message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
10173
11026
  });
11027
+ }
11028
+ if (messages === null) {
11029
+ for (const row of sessionRows) {
11030
+ if (this.awaitingCorrelation.has(row.id)) {
11031
+ await this.advanceReadoptCorrelation(sessionId, row, null);
11032
+ }
11033
+ }
10174
11034
  continue;
10175
11035
  }
10176
11036
  const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
@@ -10180,6 +11040,59 @@ var ChannelDriver = class _ChannelDriver {
10180
11040
  }
10181
11041
  }
10182
11042
  }
11043
+ /** Advance a re-adopted accepted prompt through the shared correlation fence. */
11044
+ async advanceReadoptCorrelation(sessionId, row, messages) {
11045
+ const correlation = this.awaitingCorrelation.get(row.id);
11046
+ if (!correlation) return false;
11047
+ correlation.attempts += 1;
11048
+ const correlatedId = messages === null ? null : this.findAwaitingCorrelationId(correlation, sessionId, messages);
11049
+ if (correlatedId !== null) {
11050
+ this.awaitingCorrelation.delete(row.id);
11051
+ const conv = this.convForRow(sessionId, row);
11052
+ const message = this.queuedMessageForRow(row);
11053
+ const processedAtMs = this.processedAtMs(row);
11054
+ this.registerReadopted(conv, sessionId, message, correlatedId, processedAtMs, processedAtMs);
11055
+ this.dispatched.add(row.id);
11056
+ this.readopted.add(row.id);
11057
+ this.ensureWatcherRunning(sessionId);
11058
+ this.log({
11059
+ level: "info",
11060
+ message: `Re-adopt: matched accepted message ${row.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
11061
+ conversation_id: row.conversation_id,
11062
+ message_id: row.id
11063
+ });
11064
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
11065
+ void this.postSignal(row.conversation_id, row.id, "ack_correlated", {
11066
+ opencode_message_id: correlatedId
11067
+ });
11068
+ return true;
11069
+ }
11070
+ if (correlation.knownUserIds === null) {
11071
+ this.log({
11072
+ level: "debug",
11073
+ message: `Re-adopt: message ${row.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
11074
+ conversation_id: row.conversation_id,
11075
+ message_id: row.id
11076
+ });
11077
+ } else if (correlation.sessionId !== sessionId) {
11078
+ this.log({
11079
+ level: "warn",
11080
+ message: `Re-adopt: message ${row.id.slice(0, 8)} moved from original session ${correlation.sessionId.slice(0, 8)} to ${sessionId.slice(0, 8)} while awaiting correlation \u2014 refusing to adopt a turn from the rebound session`,
11081
+ conversation_id: row.conversation_id,
11082
+ message_id: row.id
11083
+ });
11084
+ }
11085
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
11086
+ await this.handoffUntrackableCorrelation(
11087
+ row.conversation_id,
11088
+ row.id,
11089
+ correlation.sessionId,
11090
+ correlation.attempts,
11091
+ "readopt"
11092
+ );
11093
+ }
11094
+ return true;
11095
+ }
10183
11096
  /**
10184
11097
  * Re-adopt ONE `processing` row against the tick's session message snapshot
10185
11098
  * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
@@ -10187,8 +11100,10 @@ var ChannelDriver = class _ChannelDriver {
10187
11100
  * Branches on `messageRunState(messages, row.opencode_message_id)` — the
10188
11101
  * opencode-assigned user-message id persisted on the first `processing` PATCH
10189
11102
  * (#218). A row with a NULL stored id (dispatched but the read-back never landed
10190
- * before the restart) has no id to correlate → treated as an orphan and
10191
- * re-dispatched (at most once, see `forceReadoptRun`):
11103
+ * before the restart) is first offered to an awaiting-correlation fence recorded
11104
+ * by this process; without that fence, it has no id to correlate after restart
11105
+ * and is treated as an orphan and re-dispatched (at most once, see
11106
+ * `forceReadoptRun`):
10192
11107
  * - `done` → `markDone` now (guarded like the watcher's done branch);
10193
11108
  * - `failed` → `markFailed` with the surfaced error (issue #182), so an
10194
11109
  * errored turn is reported failed on restart, NOT re-dispatched —
@@ -10197,7 +11112,8 @@ var ChannelDriver = class _ChannelDriver {
10197
11112
  * re-dispatched instead (issue #1310, see the branch below);
10198
11113
  * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
10199
11114
  * tracking the stored id so the reply correlates by it;
10200
- * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
11115
+ * - `unknown`/null id → correlate through an awaiting fence when present;
11116
+ * otherwise re-dispatch (opencode assigns a fresh id) + attach a watcher.
10201
11117
  *
10202
11118
  * Only `ChannelAuthError` propagates.
10203
11119
  */
@@ -10211,6 +11127,7 @@ var ChannelDriver = class _ChannelDriver {
10211
11127
  });
10212
11128
  return;
10213
11129
  }
11130
+ if (await this.advanceReadoptCorrelation(sessionId, row, messages)) return;
10214
11131
  const ocId = row.opencode_message_id;
10215
11132
  const state = messageRunState(messages, ocId ?? "");
10216
11133
  if (state === "done") {
@@ -10277,12 +11194,48 @@ var ChannelDriver = class _ChannelDriver {
10277
11194
  await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
10278
11195
  this.dontRedispatch.delete(row.id);
10279
11196
  this.untrackableAck.delete(row.id);
11197
+ this.untrackableAckSessionIds.delete(row.id);
10280
11198
  this.untrackableAckSignalled.delete(row.id);
10281
11199
  this.untrackableAckWarned.delete(row.id);
10282
11200
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
10283
11201
  return;
10284
11202
  }
10285
- if (this.dontRedispatch.has(row.id) || this.untrackableAck.has(row.id)) {
11203
+ if (this.untrackableAck.has(row.id)) {
11204
+ if (this.untrackableAckSignalled.has(row.id)) {
11205
+ const firstFenceWarning = !this.untrackableAckWarned.has(row.id);
11206
+ this.untrackableAckWarned.add(row.id);
11207
+ this.log({
11208
+ level: firstFenceWarning ? "warn" : "debug",
11209
+ message: `Re-adopt: message ${row.id.slice(0, 8)} is fenced after an untrackable OpenCode turn \u2014 skipping re-dispatch and retrying the terminal report`,
11210
+ conversation_id: row.conversation_id,
11211
+ message_id: row.id
11212
+ });
11213
+ const errorMessage3 = this.untrackableAck.get(row.id);
11214
+ const reportError = await this.reportUntrackableAck(
11215
+ row.conversation_id,
11216
+ row.id,
11217
+ this.untrackableAckSessionIds.get(row.id) ?? null,
11218
+ errorMessage3
11219
+ );
11220
+ if (reportError !== null) {
11221
+ this.log({
11222
+ level: "warn",
11223
+ message: `Re-adopt: failed to report untrackable message ${row.id.slice(0, 8)} (will retry next drain): ${reportError.message}`,
11224
+ conversation_id: row.conversation_id,
11225
+ message_id: row.id
11226
+ });
11227
+ }
11228
+ } else {
11229
+ this.log({
11230
+ level: "debug",
11231
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
11232
+ conversation_id: row.conversation_id,
11233
+ message_id: row.id
11234
+ });
11235
+ }
11236
+ return;
11237
+ }
11238
+ if (this.dontRedispatch.has(row.id)) {
10286
11239
  this.log({
10287
11240
  level: "debug",
10288
11241
  message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
@@ -10460,6 +11413,7 @@ var ChannelDriver = class _ChannelDriver {
10460
11413
  }
10461
11414
  this.dontRedispatch.delete(row.id);
10462
11415
  this.untrackableAck.delete(row.id);
11416
+ this.untrackableAckSessionIds.delete(row.id);
10463
11417
  this.untrackableAckSignalled.delete(row.id);
10464
11418
  this.untrackableAckWarned.delete(row.id);
10465
11419
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
@@ -10479,8 +11433,9 @@ var ChannelDriver = class _ChannelDriver {
10479
11433
  * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
10480
11434
  * short-circuit while the row is latched; clear it on a successful dispatch (the
10481
11435
  * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
10482
- * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
10483
- * may retry exactly once more).
11436
+ * re-entry) or when the dispatch itself failed before acceptance. An accepted V1
11437
+ * dispatch whose id is not readable enters the bounded correlation fence instead
11438
+ * of retrying; exhaustion hands it to `untrackableAck`.
10484
11439
  *
10485
11440
  * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
10486
11441
  * `processed_at` (Invariant 1).
@@ -10543,6 +11498,7 @@ var ChannelDriver = class _ChannelDriver {
10543
11498
  );
10544
11499
  }
10545
11500
  let ocId;
11501
+ let preDispatchUserIds = null;
10546
11502
  try {
10547
11503
  ocId = this.isV2 ? await sendV2Prompt(this.openCodeClient, sessionId, row.content) : await this.dispatchLocked(
10548
11504
  sessionId,
@@ -10552,7 +11508,20 @@ var ChannelDriver = class _ChannelDriver {
10552
11508
  row.content,
10553
11509
  options,
10554
11510
  sendAttachments,
10555
- this.openCodeClient
11511
+ this.openCodeClient,
11512
+ {
11513
+ onKnownUserIds: (ids) => {
11514
+ preDispatchUserIds = ids;
11515
+ },
11516
+ onKnownUserIdsError: (error2) => {
11517
+ this.log({
11518
+ level: "warn",
11519
+ message: `Pre-dispatch snapshot callback failed for message ${row.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
11520
+ conversation_id: row.conversation_id,
11521
+ message_id: row.id
11522
+ });
11523
+ }
11524
+ }
10556
11525
  )
10557
11526
  );
10558
11527
  } catch (err) {
@@ -10585,6 +11554,7 @@ var ChannelDriver = class _ChannelDriver {
10585
11554
  }
10586
11555
  } else {
10587
11556
  this.untrackableAck.set(row.id, errorMessage3);
11557
+ this.untrackableAckSessionIds.set(row.id, sessionId);
10588
11558
  }
10589
11559
  this.log({
10590
11560
  level: "error",
@@ -10633,39 +11603,20 @@ var ChannelDriver = class _ChannelDriver {
10633
11603
  }
10634
11604
  if (ocId === null) {
10635
11605
  this.awaitingReadopt.delete(row.id);
10636
- const streak = this.recordUnconfirmedDispatch(row.id, sessionId);
10637
- if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
10638
- this.unconfirmedDispatchFailures.delete(row.id);
10639
- this.sessions.delete(readoptConv.id);
10640
- this.supersede(readoptConv.id, sessionId);
10641
- const errorMessage3 = `OpenCode accepted this re-dispatched message ${streak} times in a row but never confirmed its assigned id (session ${sessionId.slice(0, 8)}'s message list could not be read back) \u2014 the session was abandoned; a fresh one is used for further messages.`;
10642
- this.log({
10643
- level: "error",
10644
- message: errorMessage3,
10645
- conversation_id: row.conversation_id,
10646
- message_id: row.id
10647
- });
10648
- await this.markFailed(row.conversation_id, row.id, null, errorMessage3).catch((markErr) => {
10649
- this.log({
10650
- level: "warn",
10651
- message: `markFailed PATCH for message ${row.id.slice(0, 8)} (conversation ${row.conversation_id.slice(0, 8)}) failed (best-effort, not retried): ${markErr instanceof Error ? markErr.message : String(markErr)}`,
10652
- conversation_id: row.conversation_id,
10653
- message_id: row.id
10654
- });
10655
- });
10656
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
10657
- return;
10658
- }
11606
+ this.awaitingCorrelation.set(row.id, {
11607
+ sessionId,
11608
+ content: row.content,
11609
+ attempts: 0,
11610
+ knownUserIds: preDispatchUserIds
11611
+ });
10659
11612
  this.log({
10660
11613
  level: "warn",
10661
- message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back (${streak}/${MAX_IDENTICAL_REDRIVE_POLL_FAILURES}) \u2014 leaving un-tracked to retry next drain`,
11614
+ message: `Re-adopt: OpenCode accepted message ${row.id.slice(0, 8)}, but its assigned id could not be read back \u2014 the message will not be sent again while the runner matches it to the turn already running`,
10662
11615
  conversation_id: row.conversation_id,
10663
11616
  message_id: row.id
10664
11617
  });
10665
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
10666
11618
  return;
10667
11619
  }
10668
- this.unconfirmedDispatchFailures.delete(row.id);
10669
11620
  const processedAtMs = this.processedAtMs(row);
10670
11621
  this.registerReadopted(
10671
11622
  readoptConv,
@@ -11004,14 +11955,14 @@ var ChannelDriver = class _ChannelDriver {
11004
11955
  const fetchChildWithoutBlocking = async (sessionId) => {
11005
11956
  const pending = fetchChildMessages(sessionId);
11006
11957
  let timer;
11007
- const timeout = new Promise((resolve4) => {
11958
+ const timeout = new Promise((resolve5) => {
11008
11959
  timer = setTimeout(() => {
11009
11960
  this.log({
11010
11961
  level: "warn",
11011
11962
  message: `Best-effort subagent session fetch for message ${messageId.slice(0, 8)} and child session ${sessionId.slice(0, 8)} was slow \u2014 omitting invocation telemetry without delaying completion`,
11012
11963
  message_id: messageId
11013
11964
  });
11014
- resolve4(null);
11965
+ resolve5(null);
11015
11966
  }, SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS);
11016
11967
  });
11017
11968
  try {
@@ -11571,7 +12522,7 @@ var ChannelDriver = class _ChannelDriver {
11571
12522
  * watcher retries next tick within the
11572
12523
  * deadline, Finding 4).
11573
12524
  */
11574
- async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations) {
12525
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations, error2) {
11575
12526
  const res = await this.fetchImpl(
11576
12527
  `${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
11577
12528
  {
@@ -11589,7 +12540,8 @@ var ChannelDriver = class _ChannelDriver {
11589
12540
  ...title ? { title } : {},
11590
12541
  ...usage ? usage : {},
11591
12542
  ...usageAgentName ? { usage_agent_name: usageAgentName } : {},
11592
- ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {}
12543
+ ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {},
12544
+ ...error2 ? { error: error2 } : {}
11593
12545
  })
11594
12546
  }
11595
12547
  );
@@ -11649,11 +12601,17 @@ var ChannelDriver = class _ChannelDriver {
11649
12601
  if (subagentInvocations && subagentInvocations.length > 0) {
11650
12602
  body.subagent_invocations = subagentInvocations;
11651
12603
  }
11652
- if (failure) {
11653
- body.failure_kind = failure.kind;
11654
- body.failure_provider_id = failure.providerId;
11655
- body.failure_model_id = failure.modelId;
11656
- body.failure_reason = failure.reason;
12604
+ if (failure && failure.errorShape.name !== null) {
12605
+ body.failure_error_name = failure.errorShape.name;
12606
+ }
12607
+ if (failure && failure.errorShape.statusCode !== null) {
12608
+ body.failure_status_code = failure.errorShape.statusCode;
12609
+ }
12610
+ if (failure?.classification) {
12611
+ body.failure_kind = failure.classification.kind;
12612
+ body.failure_provider_id = failure.classification.providerId;
12613
+ body.failure_model_id = failure.classification.modelId;
12614
+ body.failure_reason = failure.classification.reason;
11657
12615
  }
11658
12616
  try {
11659
12617
  await this.callWithRetry(
@@ -11674,27 +12632,28 @@ var ChannelDriver = class _ChannelDriver {
11674
12632
  this.clearSubagentInvocationCaches(messageId);
11675
12633
  }
11676
12634
  /**
11677
- * Classify an errored turn as a model-auth failure (#736 P1-4), for `markFailed`.
12635
+ * Classify an errored turn and extract its bounded error shape for `markFailed`.
11678
12636
  *
11679
12637
  * `messageFailure` alone (structured OpenCode error → `model_auth`) covers
11680
12638
  * most cases; when it returns `null` on this ALREADY-FAILED turn, fall back
11681
12639
  * to the P1-2b zero-provider check — one extra loopback call to
11682
12640
  * `hasAnyConfiguredProvider`, only reached when the structured classifier
11683
12641
  * couldn't place it. Fails open (never throws): a fallback probe failure
11684
- * (`null`/indeterminate) leaves the classification `null`, which produces
11685
- * today's byte-identical PATCH body via `markFailed`'s `if (failure)` guard.
12642
+ * (`null`/indeterminate) leaves the classification `null`; the raw error shape
12643
+ * is still retained for diagnostics.
11686
12644
  */
11687
12645
  async classifyModelAuthFailure(messages, userMessageId) {
11688
- const classified = messageFailure(messages, userMessageId);
11689
- if (classified != null) return classified;
11690
12646
  const reply = findLastAssistantReplyFor(messages, userMessageId);
11691
- const hasProvider = await this.hasAnyConfiguredProvider();
11692
- return applyZeroProviderFallback(
12647
+ const errorShape = extractReplyErrorShape(reply);
12648
+ const classified = messageFailure(messages, userMessageId);
12649
+ if (classified != null) return { classification: classified, errorShape };
12650
+ const classification = applyZeroProviderFallback(
11693
12651
  classified,
11694
- hasProvider,
12652
+ await this.hasAnyConfiguredProvider(),
11695
12653
  reply?.info?.providerID ?? null,
11696
12654
  reply?.info?.modelID ?? null
11697
12655
  );
12656
+ return { classification, errorShape };
11698
12657
  }
11699
12658
  async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
11700
12659
  const providerId = failure.providerId ?? "(unknown)";
@@ -12252,7 +13211,7 @@ function commandError2(result) {
12252
13211
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
12253
13212
  }
12254
13213
  var runCommand2 = (command, args, opts) => {
12255
- return new Promise((resolve4) => {
13214
+ return new Promise((resolve5) => {
12256
13215
  let child;
12257
13216
  let stdout = "";
12258
13217
  let stderr = "";
@@ -12262,7 +13221,7 @@ var runCommand2 = (command, args, opts) => {
12262
13221
  if (settled) return;
12263
13222
  settled = true;
12264
13223
  if (timer.handle) clearTimeout(timer.handle);
12265
- resolve4(result);
13224
+ resolve5(result);
12266
13225
  };
12267
13226
  try {
12268
13227
  child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
@@ -12317,7 +13276,9 @@ async function installRunnerSecret({
12317
13276
  }) {
12318
13277
  const arn = env.RUNNER_SECRET_ARN?.trim();
12319
13278
  if (!arn) {
12320
- log3("runner secret is not configured; continuing without GitHub and MCP credentials");
13279
+ if (!env.GH_TOKEN) {
13280
+ log3("runner secret is not configured; continuing without GitHub and MCP credentials");
13281
+ }
12321
13282
  return false;
12322
13283
  }
12323
13284
  const result = await (commandRunner ?? runCommand2)(
@@ -12516,7 +13477,7 @@ async function configureGitHubAccess({ env, log: log3 }) {
12516
13477
  // src/lib/opencode/config-overlay.ts
12517
13478
  import { execFileSync as execFileSync2 } from "node:child_process";
12518
13479
  import { copyFileSync, existsSync as existsSync2, statSync as statSync6 } from "node:fs";
12519
- import { isAbsolute as isAbsolute2, join as join10, resolve as resolve3 } from "node:path";
13480
+ import { isAbsolute as isAbsolute3, join as join11, resolve as resolve4 } from "node:path";
12520
13481
  function isFile(filePath) {
12521
13482
  return existsSync2(filePath) && statSync6(filePath).isFile();
12522
13483
  }
@@ -12529,8 +13490,8 @@ function applyRunnerOpenCodeConfig({
12529
13490
  log3("runner OpenCode config is not configured; using the baked project config", "debug");
12530
13491
  return;
12531
13492
  }
12532
- const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
12533
- const target = isFile(join10(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
13493
+ const source = isAbsolute3(overlayPath) ? overlayPath : resolve4(cwd, overlayPath);
13494
+ const target = isFile(join11(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
12534
13495
  if (!isFile(source)) {
12535
13496
  log3(
12536
13497
  `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
@@ -12538,7 +13499,7 @@ function applyRunnerOpenCodeConfig({
12538
13499
  );
12539
13500
  return;
12540
13501
  }
12541
- copyFileSync(source, join10(cwd, target));
13502
+ copyFileSync(source, join11(cwd, target));
12542
13503
  try {
12543
13504
  execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
12544
13505
  stdio: "ignore"
@@ -12547,7 +13508,7 @@ function applyRunnerOpenCodeConfig({
12547
13508
  const detail = error2 instanceof Error ? error2.message : String(error2);
12548
13509
  log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
12549
13510
  }
12550
- log3(`Applied runner OpenCode config ${source} to ${join10(cwd, target)}`);
13511
+ log3(`Applied runner OpenCode config ${source} to ${join11(cwd, target)}`);
12551
13512
  }
12552
13513
 
12553
13514
  // src/lib/credential-sync.ts
@@ -12565,14 +13526,14 @@ function errorMessage2(error2) {
12565
13526
  return error2 instanceof Error ? error2.message : String(error2);
12566
13527
  }
12567
13528
  function waitForSettlement(promise, timeoutMs) {
12568
- return new Promise((resolve4) => {
13529
+ return new Promise((resolve5) => {
12569
13530
  let settled = false;
12570
13531
  const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
12571
13532
  const finish = (value) => {
12572
13533
  if (settled) return;
12573
13534
  settled = true;
12574
13535
  clearTimeout(timer);
12575
- resolve4(value);
13536
+ resolve5(value);
12576
13537
  };
12577
13538
  promise.then(
12578
13539
  () => finish(true),
@@ -12789,6 +13750,8 @@ function createCredentialSync({
12789
13750
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
12790
13751
  var MAX_DISCONNECT_REASON_LENGTH = 200;
12791
13752
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
13753
+ var CREDENTIAL_ADOPTION_PROBE_ATTEMPTS = 3;
13754
+ var CREDENTIAL_ADOPTION_PROBE_INTERVAL_MS = 1e3;
12792
13755
  var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
12793
13756
  var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
12794
13757
  var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
@@ -12823,8 +13786,8 @@ function resolveFileSyncDirectories(raw, homeDir) {
12823
13786
  if (trimmed === "") {
12824
13787
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
12825
13788
  }
12826
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join11(homeDir, trimmed.slice(2)) : trimmed;
12827
- if (!isAbsolute3(expanded)) {
13789
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join12(homeDir, trimmed.slice(2)) : trimmed;
13790
+ if (!isAbsolute4(expanded)) {
12828
13791
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
12829
13792
  }
12830
13793
  const normalized = resolvePath2(expanded);
@@ -12924,8 +13887,7 @@ function resolveMaxActiveSessions(options, env = process.env) {
12924
13887
  function meetsThreshold(state, level) {
12925
13888
  return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
12926
13889
  }
12927
- function log2(state, message, level = "info") {
12928
- if (!meetsThreshold(state, level)) return;
13890
+ function writeConsoleLog(state, message, level) {
12929
13891
  if (state.json) {
12930
13892
  console.log(
12931
13893
  JSON.stringify({
@@ -12934,10 +13896,18 @@ function log2(state, message, level = "info") {
12934
13896
  message
12935
13897
  })
12936
13898
  );
12937
- } else if (!state.interactive) {
12938
- const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
12939
- console.log(`${prefix} ${message}`);
13899
+ return;
12940
13900
  }
13901
+ const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
13902
+ console.log(`${prefix} ${message}`);
13903
+ }
13904
+ function log2(state, message, level = "info") {
13905
+ if (!meetsThreshold(state, level)) return;
13906
+ if (state.json || !state.interactive) writeConsoleLog(state, message, level);
13907
+ }
13908
+ function logLocalOnly(state, message, level = "info") {
13909
+ if (!meetsThreshold(state, level)) return;
13910
+ writeConsoleLog(state, message, level);
12941
13911
  }
12942
13912
  function logActivity(state, entry) {
12943
13913
  const level = entry.level ?? (entry.type === "error" ? "error" : "info");
@@ -13129,6 +14099,125 @@ async function handleAuthError(state, error2) {
13129
14099
  return { success: false };
13130
14100
  }
13131
14101
  }
14102
+ function logCredentialAdoptionActivity(state, activity, level) {
14103
+ const message = (() => {
14104
+ switch (activity.outcome) {
14105
+ case "deferred":
14106
+ return `CREDENTIAL-ADOPTION: deferred generation ${activity.generation} with ${activity.active_sessions} active session(s)`;
14107
+ case "reloaded_ready":
14108
+ return `CREDENTIAL-ADOPTION: adopted generation ${activity.generation} with ${activity.active_sessions} active session(s); readiness probe succeeded after ${activity.probe_attempts} attempt(s)`;
14109
+ case "reloaded_interrupted": {
14110
+ const response = activity.probe_status === null ? "without an HTTP response" : `with HTTP ${activity.probe_status} and ${activity.probe_body_empty ? "an empty" : "a non-empty"} body`;
14111
+ return `CREDENTIAL-ADOPTION: generation ${activity.generation} with ${activity.active_sessions} active session(s); replacement instance answered ${response} after ${activity.probe_attempts} probe attempt(s)`;
14112
+ }
14113
+ case "reload_failed":
14114
+ return `CREDENTIAL-ADOPTION: failed to adopt generation ${activity.generation} with ${activity.active_sessions} active session(s)`;
14115
+ case "skipped_v2":
14116
+ return `CREDENTIAL-ADOPTION: skipped generation ${activity.generation} with ${activity.active_sessions} active session(s) on OpenCode V2`;
14117
+ }
14118
+ })();
14119
+ logActivity(state, {
14120
+ type: level === "error" ? "error" : "info",
14121
+ level,
14122
+ ...level === "error" ? { error: message } : { message },
14123
+ metadata: {
14124
+ ...activity,
14125
+ opencode_version: state.opencodeVersion,
14126
+ port: state.port
14127
+ }
14128
+ });
14129
+ }
14130
+ async function adoptCredentialGeneration(state, generation, adoption, activeSessions, deferredForMs) {
14131
+ const reload = await disposeAndReloadProviderConfig(
14132
+ state.port,
14133
+ state.opencodeClient ?? void 0
14134
+ );
14135
+ if (reload.outcome === "failed") {
14136
+ logCredentialAdoptionActivity(
14137
+ state,
14138
+ {
14139
+ outcome: "reload_failed",
14140
+ generation,
14141
+ active_sessions: activeSessions,
14142
+ adoption,
14143
+ deferred_for_ms: deferredForMs,
14144
+ reload_status: reload.status,
14145
+ reload_detail: reload.detail,
14146
+ probe_attempts: null,
14147
+ probe_status: null,
14148
+ probe_body_empty: null,
14149
+ probe_detail: null
14150
+ },
14151
+ "error"
14152
+ );
14153
+ return;
14154
+ }
14155
+ if (reload.outcome === "skipped_v2") {
14156
+ logCredentialAdoptionActivity(
14157
+ state,
14158
+ {
14159
+ outcome: "skipped_v2",
14160
+ generation,
14161
+ active_sessions: activeSessions,
14162
+ adoption,
14163
+ deferred_for_ms: deferredForMs,
14164
+ reload_status: null,
14165
+ reload_detail: null,
14166
+ probe_attempts: null,
14167
+ probe_status: null,
14168
+ probe_body_empty: null,
14169
+ probe_detail: null
14170
+ },
14171
+ "debug"
14172
+ );
14173
+ return;
14174
+ }
14175
+ let lastProbe = null;
14176
+ let attempts = 0;
14177
+ for (attempts = 1; attempts <= CREDENTIAL_ADOPTION_PROBE_ATTEMPTS; attempts++) {
14178
+ lastProbe = await probeOpenCodeRequestReadiness(state.port, state.opencodeClient ?? void 0);
14179
+ if (lastProbe.ok) {
14180
+ logCredentialAdoptionActivity(
14181
+ state,
14182
+ {
14183
+ outcome: "reloaded_ready",
14184
+ generation,
14185
+ active_sessions: activeSessions,
14186
+ adoption,
14187
+ deferred_for_ms: deferredForMs,
14188
+ reload_status: null,
14189
+ reload_detail: null,
14190
+ probe_attempts: attempts,
14191
+ probe_status: lastProbe.status,
14192
+ probe_body_empty: lastProbe.bodyEmpty,
14193
+ probe_detail: lastProbe.detail
14194
+ },
14195
+ "warn"
14196
+ );
14197
+ return;
14198
+ }
14199
+ if (attempts < CREDENTIAL_ADOPTION_PROBE_ATTEMPTS) {
14200
+ await new Promise((resolve5) => setTimeout(resolve5, CREDENTIAL_ADOPTION_PROBE_INTERVAL_MS));
14201
+ }
14202
+ }
14203
+ logCredentialAdoptionActivity(
14204
+ state,
14205
+ {
14206
+ outcome: "reloaded_interrupted",
14207
+ generation,
14208
+ active_sessions: activeSessions,
14209
+ adoption,
14210
+ deferred_for_ms: deferredForMs,
14211
+ reload_status: null,
14212
+ reload_detail: null,
14213
+ probe_attempts: CREDENTIAL_ADOPTION_PROBE_ATTEMPTS,
14214
+ probe_status: lastProbe?.status ?? null,
14215
+ probe_body_empty: lastProbe?.bodyEmpty ?? null,
14216
+ probe_detail: lastProbe?.detail ?? null
14217
+ },
14218
+ "error"
14219
+ );
14220
+ }
13132
14221
  async function driveChannels(state, driver) {
13133
14222
  let idlePolls = 0;
13134
14223
  let idleMs = 0;
@@ -13138,6 +14227,15 @@ async function driveChannels(state, driver) {
13138
14227
  let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
13139
14228
  let lastSeenClaudeApplies = driver.fileSyncActivity().claudeCredentialApplies;
13140
14229
  let lastSeenOpencodeAuthApplies = driver.fileSyncActivity().opencodeAuthApplyCount;
14230
+ const credentialAdoption = {
14231
+ generation: 0,
14232
+ pendingGeneration: null,
14233
+ pendingSinceMs: null,
14234
+ deferredGeneration: null,
14235
+ adoption: 0
14236
+ };
14237
+ const stayAwakeHolds = () => state.stayAwakeUntilMs !== null && Date.now() < state.stayAwakeUntilMs;
14238
+ let stayAwakeSuppressionLogged = false;
13141
14239
  while (state.running) {
13142
14240
  const cycleStartedAtMs = performance.now();
13143
14241
  let idleThisCycle = false;
@@ -13179,12 +14277,30 @@ async function driveChannels(state, driver) {
13179
14277
  lastSeenOpencodeAuthApplies = opencodeAuthApplies;
13180
14278
  if (opencodeAuthApplied) state.openaiUsageRearm?.();
13181
14279
  if (claudeCredentialApplied || opencodeAuthApplied) {
13182
- void reloadProviderCache(state.port, state.opencodeClient ?? void 0).catch(
13183
- (error2) => logActivity(state, {
13184
- type: "error",
13185
- error: `OpenCode provider cache reload failed on port ${state.port}: ${error2 instanceof Error ? error2.message : String(error2)}`
13186
- })
13187
- );
14280
+ credentialAdoption.generation++;
14281
+ if (state.opencodeApiVersion === "v2") {
14282
+ logCredentialAdoptionActivity(
14283
+ state,
14284
+ {
14285
+ outcome: "skipped_v2",
14286
+ generation: credentialAdoption.generation,
14287
+ active_sessions: driver.protectedSessionIds().size,
14288
+ adoption: null,
14289
+ deferred_for_ms: null,
14290
+ reload_status: null,
14291
+ reload_detail: null,
14292
+ probe_attempts: null,
14293
+ probe_status: null,
14294
+ probe_body_empty: null,
14295
+ probe_detail: null
14296
+ },
14297
+ "debug"
14298
+ );
14299
+ } else {
14300
+ credentialAdoption.pendingGeneration = credentialAdoption.generation;
14301
+ credentialAdoption.pendingSinceMs = Date.now();
14302
+ credentialAdoption.deferredGeneration = null;
14303
+ }
13188
14304
  }
13189
14305
  if (opencodeAuthApplied && state.opencodeApiVersion === "v2") {
13190
14306
  void importOpenCodeV2CredentialsForState(state).catch(
@@ -13239,7 +14355,50 @@ async function driveChannels(state, driver) {
13239
14355
  }
13240
14356
  }
13241
14357
  }
13242
- await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
14358
+ if (credentialAdoption.pendingGeneration !== null) {
14359
+ const activeSessions = driver.protectedSessionIds();
14360
+ const generation = credentialAdoption.pendingGeneration;
14361
+ if (activeSessions.size > 0) {
14362
+ if (credentialAdoption.deferredGeneration !== generation) {
14363
+ credentialAdoption.deferredGeneration = generation;
14364
+ logCredentialAdoptionActivity(
14365
+ state,
14366
+ {
14367
+ outcome: "deferred",
14368
+ generation,
14369
+ active_sessions: activeSessions.size,
14370
+ adoption: null,
14371
+ deferred_for_ms: null,
14372
+ reload_status: null,
14373
+ reload_detail: null,
14374
+ probe_attempts: null,
14375
+ probe_status: null,
14376
+ probe_body_empty: null,
14377
+ probe_detail: null
14378
+ },
14379
+ "warn"
14380
+ );
14381
+ }
14382
+ } else {
14383
+ const deferredForMs = credentialAdoption.deferredGeneration === generation && credentialAdoption.pendingSinceMs !== null ? Math.max(0, Date.now() - credentialAdoption.pendingSinceMs) : null;
14384
+ credentialAdoption.pendingGeneration = null;
14385
+ credentialAdoption.pendingSinceMs = null;
14386
+ credentialAdoption.adoption++;
14387
+ void adoptCredentialGeneration(
14388
+ state,
14389
+ generation,
14390
+ credentialAdoption.adoption,
14391
+ activeSessions.size,
14392
+ deferredForMs
14393
+ ).catch(
14394
+ (error2) => logActivity(state, {
14395
+ type: "error",
14396
+ error: `CREDENTIAL-ADOPTION: adoption failed for generation ${generation} with ${activeSessions.size} active session(s) on port ${state.port}: ${error2 instanceof Error ? error2.message : String(error2)}`
14397
+ })
14398
+ );
14399
+ }
14400
+ }
14401
+ await new Promise((resolve5) => setTimeout(resolve5, CHANNEL_POLL_INTERVAL_MS));
13243
14402
  const cycleMs = performance.now() - cycleStartedAtMs;
13244
14403
  if (idleThisCycle) idleMs += cycleMs;
13245
14404
  if (unreachableThisCycle) unreachableMs += cycleMs;
@@ -13252,15 +14411,27 @@ async function driveChannels(state, driver) {
13252
14411
  if (state.interactive) displayStatus(state);
13253
14412
  break;
13254
14413
  }
13255
- if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3) {
14414
+ if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && !stayAwakeHolds()) {
13256
14415
  logActivity(state, { type: "info", message: "Idle timeout reached" });
13257
14416
  if (state.interactive) displayStatus(state);
13258
14417
  break;
13259
14418
  }
14419
+ if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && stayAwakeHolds()) {
14420
+ if (!stayAwakeSuppressionLogged) {
14421
+ stayAwakeSuppressionLogged = true;
14422
+ logActivity(state, {
14423
+ type: "info",
14424
+ message: "Idle, but staying awake until the requested minimum passes"
14425
+ });
14426
+ }
14427
+ }
13260
14428
  }
13261
14429
  }
13262
14430
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
13263
14431
  var SESSION_CLEANUP_MAX_DELETES_PER_SWEEP = 100;
14432
+ var SESSION_CLEANUP_MAX_ISOLATED_DELETE_FAILURES = 2;
14433
+ var SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES = 2;
14434
+ var MAX_SESSION_DELETE_BODY_LENGTH = 200;
13264
14435
  function shouldWarnForReclaimSkip(reason) {
13265
14436
  if (reason !== "sqlite-unavailable" && reason !== "disk-exhaustion") return false;
13266
14437
  const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
@@ -13271,7 +14442,7 @@ function shouldWarnForReclaimSkip(reason) {
13271
14442
  return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
13272
14443
  }
13273
14444
  function sessionDbPath() {
13274
- return join11(homedir7(), ".local", "share", "opencode", "opencode.db");
14445
+ return join12(homedir7(), ".local", "share", "opencode", "opencode.db");
13275
14446
  }
13276
14447
  function logSessionCleanupActivity(state, phase, entry) {
13277
14448
  logActivity(state, {
@@ -13348,8 +14519,12 @@ async function runSweep(state, driver, config) {
13348
14519
  protectedIds: protectedNow
13349
14520
  }
13350
14521
  );
13351
- const deleteCandidates = toDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
13352
- remaining = toDelete.length - deleteCandidates.length;
14522
+ const eligibleToDelete = toDelete.filter(
14523
+ (id) => (state.sessionDeleteFailures.get(id)?.isolatedFailures ?? 0) < SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES
14524
+ );
14525
+ const deleteCandidates = eligibleToDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
14526
+ remaining = eligibleToDelete.length - deleteCandidates.length;
14527
+ const answeredFailures = [];
13353
14528
  protectedNow = driver.protectedSessionIds();
13354
14529
  for (const id of deleteCandidates) {
13355
14530
  if (protectedNow.has(id)) {
@@ -13372,8 +14547,44 @@ async function runSweep(state, driver, config) {
13372
14547
  if (statusClass === 4) failed4xx++;
13373
14548
  else if (statusClass === 5) failed5xx++;
13374
14549
  else failedOtherStatus++;
14550
+ answeredFailures.push({
14551
+ id,
14552
+ status: outcome.status,
14553
+ ...outcome.body !== void 0 ? { body: outcome.body } : {}
14554
+ });
14555
+ }
14556
+ }
14557
+ const givenUpFailures = [];
14558
+ if (failed > SESSION_CLEANUP_MAX_ISOLATED_DELETE_FAILURES) {
14559
+ for (const { id } of answeredFailures) state.sessionDeleteFailures.delete(id);
14560
+ } else {
14561
+ for (const failure of answeredFailures) {
14562
+ const previous = state.sessionDeleteFailures.get(failure.id);
14563
+ const record = previous?.status === failure.status ? { status: failure.status, isolatedFailures: previous.isolatedFailures + 1 } : { status: failure.status, isolatedFailures: 1 };
14564
+ state.sessionDeleteFailures.set(failure.id, record);
14565
+ if (record.isolatedFailures >= SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES) {
14566
+ givenUpFailures.push(failure);
14567
+ }
13375
14568
  }
13376
14569
  }
14570
+ for (const failure of givenUpFailures) {
14571
+ const excerpt = redactDiagnosticText(failure.body ?? "(no response body)").slice(
14572
+ 0,
14573
+ MAX_SESSION_DELETE_BODY_LENGTH
14574
+ );
14575
+ logSessionCleanupActivity(state, "sweep", {
14576
+ type: "info",
14577
+ level: "warn",
14578
+ message: `Session cleanup: gave up on session ${failure.id} after ${SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES} isolated delete failures (status ${failure.status}): ${excerpt}`,
14579
+ metadata: {
14580
+ outcome: "session-delete-given-up",
14581
+ session_id: failure.id,
14582
+ status: failure.status,
14583
+ body_excerpt: excerpt,
14584
+ given_up_count: givenUpFailures.length
14585
+ }
14586
+ });
14587
+ }
13377
14588
  const failedNote = failed > 0 ? `, failed ${failed}` : "";
13378
14589
  const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
13379
14590
  const remainingNote = remaining > 0 ? `, remaining ${remaining}` : "";
@@ -13394,21 +14605,31 @@ async function runSweep(state, driver, config) {
13394
14605
  sessions_remaining: remaining,
13395
14606
  ...enumerationReason !== null ? { session_enumeration_reason: enumerationReason } : {}
13396
14607
  };
14608
+ const protectedForReclaim = driver.protectedSessionIds();
13397
14609
  const reclaimResult = await reclaimSessionDbSpace({
13398
14610
  dbPath: sessionDbPath(),
13399
14611
  chunkPages: SESSION_DB_RECLAIM_CHUNK_PAGES,
13400
14612
  pageCeiling: SESSION_DB_RECLAIM_PAGE_CEILING,
13401
14613
  timeBudgetMs: SESSION_DB_RECLAIM_TIME_BUDGET_MS,
13402
- allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
14614
+ hasLiveTurn: protectedForReclaim.size > 0,
14615
+ hasProvenanceAnomaly: state.sessionDbProvenanceAnomaly
13403
14616
  });
13404
14617
  if (reclaimResult.ok) {
13405
14618
  state.sessionStoreHealth.observeWritable();
13406
14619
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
13407
14620
  const afterMib = (reclaimResult.afterBytes / 1024 / 1024).toFixed(1);
13408
14621
  const checkpointNote = reclaimResult.checkpoint.busy ? ` (on-disk file truncation deferred: checkpoint busy, ${reclaimResult.checkpoint.log} WAL frames pending)` : "";
14622
+ const loopElapsedMs = Math.max(
14623
+ 0,
14624
+ reclaimResult.elapsedMs - reclaimResult.checkpointElapsedMs
14625
+ );
14626
+ const reclaimIsSlow = reclaimResult.elapsedMs >= 4 * SESSION_DB_RECLAIM_TIME_BUDGET_MS;
14627
+ const perPageNote = reclaimResult.pagesReclaimed > 0 ? `, per-page ${(loopElapsedMs / reclaimResult.pagesReclaimed).toFixed(2)} ms/page` : "";
14628
+ const slowReclaimNote = reclaimIsSlow ? ` (slow reclaim: total elapsed ${reclaimResult.elapsedMs} ms, checkpoint ${reclaimResult.checkpointElapsedMs} ms, loop ${loopElapsedMs} ms, budget ${SESSION_DB_RECLAIM_TIME_BUDGET_MS} ms${perPageNote})` : "";
13409
14629
  logSessionCleanupActivity(state, "sweep", {
13410
14630
  type: "info",
13411
- message: `Session cleanup: reclaimed session-db space (${reclaimResult.mode}): ${beforeMib} MiB -> ${afterMib} MiB (stopped by ${reclaimResult.stoppedBy}; freelist ${reclaimResult.freelistBefore} -> ${reclaimResult.freelistAfter} pages)` + checkpointNote,
14631
+ level: reclaimIsSlow ? "warn" : "info",
14632
+ message: `Session cleanup: reclaimed session-db space (${reclaimResult.mode}): ${beforeMib} MiB -> ${afterMib} MiB (stopped by ${reclaimResult.stoppedBy}; freelist ${reclaimResult.freelistBefore} -> ${reclaimResult.freelistAfter} pages)` + checkpointNote + slowReclaimNote,
13412
14633
  metadata: {
13413
14634
  outcome: "reclaimed",
13414
14635
  mode: reclaimResult.mode,
@@ -13421,6 +14642,7 @@ async function runSweep(state, driver, config) {
13421
14642
  freelist_after: reclaimResult.freelistAfter,
13422
14643
  pages_reclaimed: reclaimResult.pagesReclaimed,
13423
14644
  reclaim_elapsed_ms: reclaimResult.elapsedMs,
14645
+ checkpoint_elapsed_ms: reclaimResult.checkpointElapsedMs,
13424
14646
  ...sessionMetadata
13425
14647
  }
13426
14648
  });
@@ -13531,10 +14753,12 @@ function scheduleUsageReporting(state, params) {
13531
14753
  let phase = "dormant";
13532
14754
  let rearmRequested = false;
13533
14755
  const armProbe = () => {
14756
+ if (!state.running) return;
13534
14757
  phase = "probe-pending";
13535
14758
  params.setTimer(setTimeout(() => void tick(true), params.firstDelayMs()));
13536
14759
  };
13537
14760
  const scheduleNextTick = () => {
14761
+ if (!state.running) return;
13538
14762
  if (rearmRequested) {
13539
14763
  rearmRequested = false;
13540
14764
  armProbe();
@@ -13568,6 +14792,7 @@ function scheduleUsageReporting(state, params) {
13568
14792
  phase = "tick-in-flight";
13569
14793
  try {
13570
14794
  const usage = await params.fetchUsage();
14795
+ if (!state.running) return;
13571
14796
  const result = await params.report(usage);
13572
14797
  if (result.ok) {
13573
14798
  if (consecutiveFailures > 0) {
@@ -13706,6 +14931,7 @@ function scheduleResourceUsageReporting(state, options) {
13706
14931
  message: lowDiskDecision.warning
13707
14932
  });
13708
14933
  }
14934
+ if (!state.running) return;
13709
14935
  const result = await reportResourceUsage(state.agentId, state.authHeader, usage);
13710
14936
  if (result.ok) {
13711
14937
  if (consecutiveFailures > 0) {
@@ -13738,7 +14964,9 @@ function scheduleResourceUsageReporting(state, options) {
13738
14964
  message: `Resource usage reporting failed: ${message}${failureStreakSuffix(consecutiveFailures)}`
13739
14965
  });
13740
14966
  } finally {
13741
- state.resourceUsageTimer = setTimeout(() => void tick(), usageReportDelayMs());
14967
+ if (state.running) {
14968
+ state.resourceUsageTimer = setTimeout(() => void tick(), usageReportDelayMs());
14969
+ }
13742
14970
  }
13743
14971
  };
13744
14972
  state.resourceUsageTimer = setTimeout(() => void tick(), firstReportDelayMs());
@@ -13900,8 +15128,8 @@ async function beginGracefulShutdown(state, trigger) {
13900
15128
  return true;
13901
15129
  }
13902
15130
  );
13903
- const timedOut = new Promise((resolve4) => {
13904
- timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
15131
+ const timedOut = new Promise((resolve5) => {
15132
+ timer = setTimeout(() => resolve5(false), telemetryBudgetMs);
13905
15133
  });
13906
15134
  if (!await Promise.race([flushed, timedOut])) {
13907
15135
  log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
@@ -13941,6 +15169,8 @@ async function run(options) {
13941
15169
  port: options.port ?? 4096,
13942
15170
  conversationFilter: options.conversation ?? null,
13943
15171
  idleTimeout: options.idleTimeout ?? null,
15172
+ stayAwakeSeconds: null,
15173
+ stayAwakeUntilMs: null,
13944
15174
  json: options.json ?? false,
13945
15175
  interactive,
13946
15176
  logLevel,
@@ -13964,6 +15194,7 @@ async function run(options) {
13964
15194
  messageCount: 0,
13965
15195
  lastProxiedActivityAt: null,
13966
15196
  sessionCleanupTimers: [],
15197
+ sessionDeleteFailures: /* @__PURE__ */ new Map(),
13967
15198
  claudeUsageTimer: null,
13968
15199
  claudeUsageRearm: null,
13969
15200
  openaiUsageTimer: null,
@@ -14129,6 +15360,17 @@ async function run(options) {
14129
15360
  }
14130
15361
  spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
14131
15362
  state.agentName = validation.agent.name;
15363
+ const requestedStayAwakeSeconds = validation.agent?.wake_stay_awake_seconds;
15364
+ if (typeof requestedStayAwakeSeconds === "number" && Number.isFinite(requestedStayAwakeSeconds) && requestedStayAwakeSeconds > 0) {
15365
+ state.stayAwakeSeconds = requestedStayAwakeSeconds;
15366
+ } else if (requestedStayAwakeSeconds !== void 0 && requestedStayAwakeSeconds !== null) {
15367
+ state.stayAwakeSeconds = null;
15368
+ log2(
15369
+ state,
15370
+ `Ignoring invalid wake_stay_awake_seconds (type: ${typeof requestedStayAwakeSeconds})`,
15371
+ "warn"
15372
+ );
15373
+ }
14132
15374
  const microvmId = process.env.MICROVM_ID?.trim();
14133
15375
  if (microvmId) {
14134
15376
  const reported = await reportMicrovmId(state.agentId, state.authHeader, microvmId);
@@ -14144,29 +15386,73 @@ async function run(options) {
14144
15386
  } else {
14145
15387
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
14146
15388
  }
14147
- if (options.restoreRunnerCredentials) {
14148
- log2(state, "Restoring runner credentials before starting OpenCode");
14149
- const credentialContext = {
14150
- env: process.env,
14151
- log: (message, level = "info") => {
14152
- if (level === "error") {
14153
- logActivity(state, { type: "error", error: message });
14154
- } else {
14155
- logActivity(state, { type: "info", level, message });
14156
- }
15389
+ if (options.restoreRunnerCredentials || options.credentialSyncMarker) {
15390
+ const logCredentialVaultResolution = (message, level) => {
15391
+ log2(state, message, level);
15392
+ if (state.interactive && !state.json) {
15393
+ logActivity(state, { type: "info", level, message });
14157
15394
  }
14158
15395
  };
14159
- const githubTokenPopulated = await installRunnerSecret(credentialContext);
15396
+ const vaultResolution = await getRunnerCredentialVault(state.agentId, state.authHeader);
15397
+ if (vaultResolution.error) {
15398
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
15399
+ logCredentialVaultResolution(
15400
+ `CREDENTIAL-VAULT-LOOKUP-FAILED: ${vaultResolution.error}; credentials will be read from and written to this runner's own prefix this boot`,
15401
+ "warn"
15402
+ );
15403
+ } else if (vaultResolution.prefix !== null && vaultResolution.vaultId !== null && vaultResolution.source !== null) {
15404
+ process.env.CREDENTIAL_VAULT_PREFIX = vaultResolution.prefix;
15405
+ logCredentialVaultResolution(
15406
+ `CREDENTIAL-VAULT: source=${vaultResolution.source} vault=${vaultResolution.vaultId} prefix=${vaultResolution.prefix}`,
15407
+ "info"
15408
+ );
15409
+ } else {
15410
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
15411
+ logCredentialVaultResolution(
15412
+ "CREDENTIAL-VAULT-NONE: this runner is not linked to a credential vault; using its own prefix",
15413
+ "debug"
15414
+ );
15415
+ }
15416
+ }
15417
+ const credentialContext = {
15418
+ env: process.env,
15419
+ log: (message, level = "info") => {
15420
+ if (level === "error") {
15421
+ logActivity(state, { type: "error", error: message });
15422
+ } else {
15423
+ logActivity(state, { type: "info", level, message });
15424
+ }
15425
+ }
15426
+ };
15427
+ const githubTokenPopulated = await installRunnerSecret(credentialContext);
15428
+ if (options.restoreRunnerCredentials) {
14160
15429
  await restoreCredentialStores(credentialContext);
14161
- if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
14162
15430
  }
15431
+ if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
14163
15432
  state.credentialSync?.arm();
15433
+ const dependencyInstallDiagnosticReporter = createDependencyInstallDiagnosticReporter({
15434
+ context: {
15435
+ homeDir: homedir7(),
15436
+ cwd: process.cwd(),
15437
+ microvmId: process.env.MICROVM_ID?.trim(),
15438
+ euid: process.getuid?.(),
15439
+ egid: process.getgid?.()
15440
+ },
15441
+ emitEvent: (message) => logActivity(state, { type: "info", level: "warn", message }),
15442
+ emitLocal: (message) => logLocalOnly(state, message, "warn")
15443
+ });
14164
15444
  const openCodeActivityForwarder = createOpenCodeActivityForwarder(
14165
15445
  () => ({
14166
15446
  agentId: state.agentId,
14167
15447
  authHeader: state.authHeader
14168
15448
  }),
14169
- state.sessionStoreHealth.observeUnwritable
15449
+ ({ isDiskFailure, sessionId, cause }) => {
15450
+ if (isDiskFailure) state.sessionStoreHealth.observeUnwritable();
15451
+ if (cause !== null && sessionId !== null) {
15452
+ state.sessionStoreHealth.observeCause(sessionId, cause);
15453
+ }
15454
+ },
15455
+ dependencyInstallDiagnosticReporter
14170
15456
  );
14171
15457
  const openCodeLogTail = tailOpenCodeLogFile(
14172
15458
  resolveOpenCodeLogPath(homedir7(), process.env),
@@ -14177,22 +15463,27 @@ async function run(options) {
14177
15463
  openCodeLogTail.stop();
14178
15464
  };
14179
15465
  let sessionDbVerifyFatal = false;
15466
+ let sessionDbVerifyFatalReason;
14180
15467
  if (!options.restoreSessionDb) {
14181
- log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
15468
+ logActivity(state, {
15469
+ type: "info",
15470
+ level: "warn",
15471
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=flag-absent \u2014 Skipping session-DB restore: --restore-session-db was not passed"
15472
+ });
14182
15473
  } else {
14183
15474
  const health = await checkOpenCodeHealth(state.port);
14184
15475
  if (health.healthy) {
14185
- log2(
14186
- state,
14187
- "Skipping session-DB restore: OpenCode is already serving this database",
14188
- "debug"
14189
- );
15476
+ logActivity(state, {
15477
+ type: "info",
15478
+ level: "warn",
15479
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=opencode-serving \u2014 Skipping session-DB restore: OpenCode is already serving this database"
15480
+ });
14190
15481
  } else if (health.authFailed) {
14191
- log2(
14192
- state,
14193
- "Skipping session-DB restore: OpenCode answered with an authentication challenge; assuming this database is in use",
14194
- "debug"
14195
- );
15482
+ logActivity(state, {
15483
+ type: "info",
15484
+ level: "warn",
15485
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=opencode-auth-challenge \u2014 Skipping session-DB restore: OpenCode answered with an authentication challenge; assuming this database is in use"
15486
+ });
14196
15487
  } else {
14197
15488
  const result = await restoreAndVerifySessionDb({
14198
15489
  dbPath: sessionDbPath(),
@@ -14209,12 +15500,13 @@ async function run(options) {
14209
15500
  reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
14210
15501
  });
14211
15502
  sessionDbVerifyFatal = result.verifyFatal;
15503
+ sessionDbVerifyFatalReason = result.verifyFatalReason;
14212
15504
  }
14213
15505
  }
14214
15506
  reportSessionDbRecovery(state);
14215
15507
  if (sessionDbVerifyFatal) {
14216
15508
  throw new Error(
14217
- "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
15509
+ sessionDbVerifyFatalReason === void 0 ? "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk" : `SESSION-DB-VERIFY-TIMEOUT: session DB verification timed out (${sessionDbVerifyFatalReason}); refusing to start`
14218
15510
  );
14219
15511
  }
14220
15512
  applyRunnerOpenCodeConfig({
@@ -14322,7 +15614,7 @@ async function run(options) {
14322
15614
  logActivity(state, { type: "info", level: "warn", message: versionWarning });
14323
15615
  }
14324
15616
  }
14325
- await reloadProviderCache(state.port, state.opencodeClient ?? void 0);
15617
+ await disposeAndReloadProviderConfig(state.port, state.opencodeClient ?? void 0);
14326
15618
  if (state.opencodeApiVersion === "v2" && state.opencodeClient !== null) {
14327
15619
  await importOpenCodeV2CredentialsForState(state);
14328
15620
  }
@@ -14493,6 +15785,13 @@ async function run(options) {
14493
15785
  type: "info",
14494
15786
  message: connectionMessage
14495
15787
  });
15788
+ if (!isReconnect && state.stayAwakeSeconds !== null && state.stayAwakeUntilMs === null) {
15789
+ state.stayAwakeUntilMs = Date.now() + state.stayAwakeSeconds * 1e3;
15790
+ logActivity(state, {
15791
+ type: "info",
15792
+ message: `Staying awake for at least ${state.stayAwakeSeconds}s as requested by the wake`
15793
+ });
15794
+ }
14496
15795
  if (options.tunnelReadyFile) {
14497
15796
  const marker = writeTunnelReadyMarker(options.tunnelReadyFile, agentId);
14498
15797
  if (marker.ok) {
@@ -14541,14 +15840,14 @@ async function run(options) {
14541
15840
  },
14542
15841
  onDisconnected: (code, reason, transition) => {
14543
15842
  state.connected = false;
14544
- const safeReason = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
15843
+ const safeReason2 = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
14545
15844
  logActivity(state, {
14546
15845
  type: "info",
14547
- message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason})`
15846
+ message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason2})`
14548
15847
  });
14549
15848
  emitAgentDisconnected(state.agentId, {
14550
15849
  code,
14551
- reason: safeReason,
15850
+ reason: safeReason2,
14552
15851
  connection_generation: transition.generation
14553
15852
  });
14554
15853
  if (state.interactive) displayStatus(state);
@@ -14627,6 +15926,7 @@ async function run(options) {
14627
15926
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
14628
15927
  throw error2;
14629
15928
  }
15929
+ if (!state.running) return;
14630
15930
  scheduleSessionCleanup(state, channelDriver, options);
14631
15931
  state.claudeUsageRearm = scheduleClaudeUsageReporting(state, options);
14632
15932
  state.openaiUsageRearm = scheduleUsageReporting(state, {
@@ -14711,6 +16011,7 @@ program.command("login").description("Authenticate with Evident").option("--toke
14711
16011
  program.command("logout").description("Remove stored credentials for the current endpoint").option("--all", "Remove stored credentials for all endpoints").action((options) => logout({ all: options.all }));
14712
16012
  program.command("whoami").description("Show the currently logged in user").action(whoami);
14713
16013
  program.command("status").description("Check whether the configured credentials can reach Evident").option("--json", "Output in JSON format").action((options) => status({ json: options.json }));
16014
+ program.command("credential-vault-env").description("Print the shell assignment a runner image evals before starting the synchroniser").action(credentialVaultEnv);
14714
16015
  program.command("claude-usage").description("[spike] Show Claude subscription usage (requires a local `claude login`)").action(claudeUsage);
14715
16016
  program.command("run").description("Connect to Evident and process messages").option("--runner [id]", "Runner ID to connect to (optional when EVIDENT_RUNNER_KEY is set)").option(
14716
16017
  "-a, --agent [id]",