@evident-ai/cli 3.5.2-dev.df1ae49 → 3.5.2-dev.f76a2fb

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,12 +1158,82 @@ 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
1070
1165
  var MICROVM_MAX_LIFETIME_MS = 8 * 60 * 6e4;
1071
1166
 
1167
+ // ../../packages/types/src/opencode/index.ts
1168
+ function roleOf(m) {
1169
+ if (!m || typeof m !== "object") return void 0;
1170
+ if (typeof m.role === "string") return m.role;
1171
+ const infoRole = m.info?.role;
1172
+ return typeof infoRole === "string" ? infoRole : void 0;
1173
+ }
1174
+ function idOf(m) {
1175
+ if (!m || typeof m !== "object") return void 0;
1176
+ if (typeof m.id === "string") return m.id;
1177
+ const infoId = m.info?.id;
1178
+ return typeof infoId === "string" ? infoId : void 0;
1179
+ }
1180
+ function parentIdOf(m) {
1181
+ if (!m || typeof m !== "object") return void 0;
1182
+ if (typeof m.parentID === "string") return m.parentID;
1183
+ const infoParent = m.info?.parentID;
1184
+ return typeof infoParent === "string" ? infoParent : void 0;
1185
+ }
1186
+ function textPartsOf(m) {
1187
+ if (!m || typeof m !== "object") return [];
1188
+ const parts = Array.isArray(m.parts) ? m.parts : [];
1189
+ return parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text ?? "").filter(Boolean);
1190
+ }
1191
+ function correlatedAssistantRun(messages, userMessageId) {
1192
+ if (!messages || messages.length === 0) return [];
1193
+ const byParent = messages.filter(
1194
+ (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
1195
+ );
1196
+ if (byParent.length > 0) return byParent;
1197
+ const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
1198
+ if (userIndex === -1) return [];
1199
+ const run2 = [];
1200
+ for (let i = userIndex + 1; i < messages.length; i++) {
1201
+ const role = roleOf(messages[i]);
1202
+ if (role === "user") break;
1203
+ if (role === "assistant") run2.push(messages[i]);
1204
+ }
1205
+ return run2;
1206
+ }
1207
+ function lastText(assistantRun) {
1208
+ for (let i = assistantRun.length - 1; i >= 0; i--) {
1209
+ const textParts = textPartsOf(assistantRun[i]);
1210
+ if (textParts.length > 0) return textParts.join("\n").trim() || null;
1211
+ }
1212
+ return null;
1213
+ }
1214
+ function effectiveCorrelatedRun(messages, userMessageId) {
1215
+ const run2 = correlatedAssistantRun(messages, userMessageId);
1216
+ if (lastText(run2) !== null) return run2;
1217
+ const userIndex = messages?.findIndex((m) => idOf(m) === userMessageId) ?? -1;
1218
+ if (userIndex === -1) return run2;
1219
+ let continuation;
1220
+ for (let i = userIndex + 1; messages && i < messages.length; i++) {
1221
+ if (roleOf(messages[i]) === "user") {
1222
+ continuation = messages[i];
1223
+ break;
1224
+ }
1225
+ }
1226
+ const continuationId = idOf(continuation);
1227
+ if (roleOf(continuation) === "user" && textPartsOf(continuation).join("\n").trim().toLowerCase() === "continue" && continuationId) {
1228
+ const continuationRun = correlatedAssistantRun(messages, continuationId);
1229
+ if (continuationRun.length > 0) return continuationRun;
1230
+ }
1231
+ return run2;
1232
+ }
1233
+ function extractReplyTextForMessage(messages, opencodeUserMessageId) {
1234
+ return lastText(effectiveCorrelatedRun(messages, opencodeUserMessageId));
1235
+ }
1236
+
1072
1237
  // ../../packages/types/src/telemetry/index.ts
1073
1238
  var TelemetryEventTypes = {
1074
1239
  // Agent activity events (shown in web UI activity log)
@@ -1157,7 +1322,7 @@ import ora4 from "ora";
1157
1322
  import { select as select4 } from "@inquirer/prompts";
1158
1323
 
1159
1324
  // src/lib/telemetry.ts
1160
- var CLI_VERSION = (true ? "3.5.2-dev.df1ae49" : void 0) ?? process.env.npm_package_version ?? "unknown";
1325
+ var CLI_VERSION = (true ? "3.5.2-dev.f76a2fb" : void 0) ?? process.env.npm_package_version ?? "unknown";
1161
1326
  function getCliVersion() {
1162
1327
  return CLI_VERSION;
1163
1328
  }
@@ -1318,20 +1483,47 @@ var EventTypes = {
1318
1483
  DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
1319
1484
  };
1320
1485
 
1321
- // src/lib/runner-activity-telemetry.ts
1322
- import { homedir as homedir2 } from "node:os";
1323
-
1324
1486
  // src/lib/opencode/session-db-reclaim.ts
1325
1487
  import { statSync as statSync2, statfsSync } from "node:fs";
1326
1488
  import { dirname as dirname2 } from "node:path";
1327
- var SESSION_DB_RECLAIM_CHUNK_PAGES = 2e3;
1489
+
1490
+ // src/lib/diagnostic-redaction.ts
1491
+ import { homedir as homedir2 } from "node:os";
1492
+ function redactHomeDirectory(message) {
1493
+ const home = homedir2();
1494
+ if (!home || home === "/") {
1495
+ return message;
1496
+ }
1497
+ const pathSegmentCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-";
1498
+ let result = "";
1499
+ let searchFrom = 0;
1500
+ let homeIndex;
1501
+ while ((homeIndex = message.indexOf(home, searchFrom)) !== -1) {
1502
+ const homeEnd = homeIndex + home.length;
1503
+ const previousCharacter = message[homeIndex - 1];
1504
+ const nextCharacter = message[homeEnd];
1505
+ const atPathBoundary = (!previousCharacter || !pathSegmentCharacters.includes(previousCharacter)) && (!nextCharacter || !pathSegmentCharacters.includes(nextCharacter));
1506
+ result += message.slice(searchFrom, homeIndex);
1507
+ result += atPathBoundary ? "~" : home;
1508
+ searchFrom = homeEnd;
1509
+ }
1510
+ return result + message.slice(searchFrom);
1511
+ }
1512
+ function redactDiagnosticText(message) {
1513
+ 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>");
1514
+ return redactHomeDirectory(redacted);
1515
+ }
1516
+
1517
+ // src/lib/opencode/session-db-reclaim.ts
1518
+ var SESSION_DB_RECLAIM_CHUNK_PAGES = 100;
1328
1519
  var SESSION_DB_RECLAIM_TIME_BUDGET_MS = 500;
1329
1520
  var SESSION_DB_RECLAIM_PAGE_CEILING = 4e4;
1521
+ var SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS = 5e3;
1330
1522
  function errorMessage(error2) {
1331
1523
  if (!(error2 instanceof Error)) return String(error2);
1332
1524
  return error2.cause instanceof Error ? error2.cause.message : error2.message;
1333
1525
  }
1334
- function isSessionStoreWriteFailure(error2) {
1526
+ function errorMessages(error2) {
1335
1527
  const messages = [];
1336
1528
  const visited = /* @__PURE__ */ new Set();
1337
1529
  let current = error2;
@@ -1355,12 +1547,81 @@ function isSessionStoreWriteFailure(error2) {
1355
1547
  }
1356
1548
  break;
1357
1549
  }
1358
- return messages.some(
1550
+ return messages;
1551
+ }
1552
+ function isSessionStoreWriteFailure(error2) {
1553
+ return errorMessages(error2).some(
1359
1554
  (message) => /(?:database or disk is full|disk is full|no space left on device|SQLITE_(?:FULL|IOERR)(?:\b|_))/i.test(
1360
1555
  message
1361
1556
  )
1362
1557
  );
1363
1558
  }
1559
+ 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;
1560
+ var EFFECT_SQL_CAUSE_TOKEN = /(?:^|cause:\s+)(effect\/sql\/SqlError\/[A-Za-z][A-Za-z0-9_]*)/i;
1561
+ var PARAMS_FIELD = /\bparams\s*(?:=|:)/i;
1562
+ function isCauseBearingText(text) {
1563
+ return /^(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text.trim()) || /(?:^|\s)cause:\s+(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text);
1564
+ }
1565
+ function structuredLogFields(line) {
1566
+ const fields = [];
1567
+ for (let index = 0; index < line.length; index++) {
1568
+ if (line[index] === '"') {
1569
+ index++;
1570
+ while (index < line.length) {
1571
+ if (line[index] === "\\") {
1572
+ index++;
1573
+ } else if (line[index] === '"') {
1574
+ break;
1575
+ }
1576
+ index++;
1577
+ }
1578
+ continue;
1579
+ }
1580
+ const atFieldBoundary = index === 0 || /\s/.test(line[index - 1]);
1581
+ const key = line.startsWith("error=", index) ? "error=" : line.startsWith("cause=", index) ? "cause=" : null;
1582
+ if (!atFieldBoundary || key === null) continue;
1583
+ const valueStart = index + key.length;
1584
+ if (line[valueStart] !== '"') {
1585
+ const valueEnd2 = line.indexOf(" ", valueStart);
1586
+ fields.push(line.slice(valueStart, valueEnd2 === -1 ? line.length : valueEnd2));
1587
+ index = valueEnd2 === -1 ? line.length : valueEnd2;
1588
+ continue;
1589
+ }
1590
+ let valueEnd = valueStart + 1;
1591
+ for (; valueEnd < line.length; valueEnd++) {
1592
+ if (line[valueEnd] === "\\") {
1593
+ valueEnd++;
1594
+ } else if (line[valueEnd] === '"') {
1595
+ break;
1596
+ }
1597
+ }
1598
+ fields.push(line.slice(valueStart + 1, valueEnd));
1599
+ index = valueEnd;
1600
+ }
1601
+ return fields;
1602
+ }
1603
+ function structuredCauseMessages(text) {
1604
+ const fields = structuredLogFields(text);
1605
+ if (fields.length > 0) return fields;
1606
+ return isCauseBearingText(text) ? [text] : [];
1607
+ }
1608
+ function causeTokenFromText(text) {
1609
+ const paramsIndex = text.search(PARAMS_FIELD);
1610
+ for (const pattern of [SQLITE_CAUSE_TOKEN, EFFECT_SQL_CAUSE_TOKEN]) {
1611
+ const match = pattern.exec(text);
1612
+ if (match?.[1] && (paramsIndex === -1 || match.index < paramsIndex)) {
1613
+ return match[1];
1614
+ }
1615
+ }
1616
+ return null;
1617
+ }
1618
+ function sessionStoreCauseToken(value) {
1619
+ for (const message of structuredCauseMessages(value)) {
1620
+ const token = causeTokenFromText(message);
1621
+ if (token !== null) return redactDiagnosticText(token).slice(0, 120);
1622
+ }
1623
+ return null;
1624
+ }
1364
1625
  function insufficientSpaceReason(dbPath, requiredBytes) {
1365
1626
  try {
1366
1627
  const fsStats = statfsSync(dirname2(dbPath));
@@ -1382,6 +1643,7 @@ function readFreelistCount(db) {
1382
1643
  return db.prepare("PRAGMA freelist_count").get().freelist_count;
1383
1644
  }
1384
1645
  function readCheckpointResult(db) {
1646
+ db.exec("PRAGMA busy_timeout=0");
1385
1647
  const row = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
1386
1648
  return { busy: row.busy !== 0, log: row.log, checkpointed: row.checkpointed };
1387
1649
  }
@@ -1399,7 +1661,7 @@ async function probeReclaimAvailability(input) {
1399
1661
  try {
1400
1662
  const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
1401
1663
  try {
1402
- db.exec("PRAGMA busy_timeout=5000");
1664
+ db.exec(`PRAGMA busy_timeout=${SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS}`);
1403
1665
  autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1404
1666
  } finally {
1405
1667
  db.close();
@@ -1419,7 +1681,8 @@ async function reclaimSessionDbSpace(input) {
1419
1681
  timeBudgetMs = SESSION_DB_RECLAIM_TIME_BUDGET_MS,
1420
1682
  chunkPages = SESSION_DB_RECLAIM_CHUNK_PAGES,
1421
1683
  now = () => performance.now(),
1422
- allowFullVacuum = true
1684
+ hasLiveTurn = false,
1685
+ hasProvenanceAnomaly = false
1423
1686
  } = input;
1424
1687
  let sqlite;
1425
1688
  try {
@@ -1435,17 +1698,19 @@ async function reclaimSessionDbSpace(input) {
1435
1698
  let db;
1436
1699
  try {
1437
1700
  db = new DatabaseSync(dbPath);
1438
- db.exec("PRAGMA busy_timeout=5000");
1701
+ db.exec(`PRAGMA busy_timeout=${SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS}`);
1439
1702
  const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1440
1703
  if (autoVacuum === 0) {
1441
- if (!allowFullVacuum) {
1442
- console.warn(
1443
- `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: a session turn is live`
1444
- );
1704
+ if (hasLiveTurn || hasProvenanceAnomaly) {
1705
+ const detail = [
1706
+ ...hasLiveTurn ? ["a session turn is live"] : [],
1707
+ ...hasProvenanceAnomaly ? ["session-DB provenance is anomalous"] : []
1708
+ ].join(" and ");
1709
+ console.warn(`[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: ${detail}`);
1445
1710
  return {
1446
1711
  ok: false,
1447
1712
  skipped: "full-vacuum-blocked",
1448
- detail: "a session turn is live"
1713
+ detail
1449
1714
  };
1450
1715
  }
1451
1716
  const fileBytesForGuard = statSync2(dbPath).size;
@@ -1463,7 +1728,9 @@ async function reclaimSessionDbSpace(input) {
1463
1728
  db.exec("VACUUM");
1464
1729
  const afterBytes = readLogicalBytes(db);
1465
1730
  const freelistAfter = readFreelistCount(db);
1731
+ const checkpointStartedAt = now();
1466
1732
  const checkpoint = readCheckpointResult(db);
1733
+ const checkpointElapsedMs = Math.max(0, now() - checkpointStartedAt);
1467
1734
  return {
1468
1735
  ok: true,
1469
1736
  mode: "convert",
@@ -1474,10 +1741,18 @@ async function reclaimSessionDbSpace(input) {
1474
1741
  pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1475
1742
  stoppedBy: "freelist-drained",
1476
1743
  elapsedMs: Math.max(0, now() - startedAt),
1744
+ checkpointElapsedMs,
1477
1745
  checkpoint
1478
1746
  };
1479
1747
  }
1480
1748
  if (autoVacuum === 2) {
1749
+ if (hasLiveTurn) {
1750
+ const detail = "a session turn is live";
1751
+ console.warn(
1752
+ `[reclaimSessionDbSpace] skipping incremental reclaim of ${dbPath}: ${detail}`
1753
+ );
1754
+ return { ok: false, skipped: "live-turn", detail };
1755
+ }
1481
1756
  const beforeBytes = readLogicalBytes(db);
1482
1757
  const freelistBefore = readFreelistCount(db);
1483
1758
  const startedAt = now();
@@ -1502,7 +1777,9 @@ async function reclaimSessionDbSpace(input) {
1502
1777
  freelistAfter = readFreelistCount(db);
1503
1778
  }
1504
1779
  const afterBytes = readLogicalBytes(db);
1780
+ const checkpointStartedAt = now();
1505
1781
  const checkpoint = readCheckpointResult(db);
1782
+ const checkpointElapsedMs = Math.max(0, now() - checkpointStartedAt);
1506
1783
  return {
1507
1784
  ok: true,
1508
1785
  mode: "incremental",
@@ -1513,6 +1790,7 @@ async function reclaimSessionDbSpace(input) {
1513
1790
  pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1514
1791
  stoppedBy,
1515
1792
  elapsedMs: Math.max(0, now() - startedAt),
1793
+ checkpointElapsedMs,
1516
1794
  checkpoint
1517
1795
  };
1518
1796
  }
@@ -1532,6 +1810,263 @@ async function reclaimSessionDbSpace(input) {
1532
1810
  }
1533
1811
  }
1534
1812
 
1813
+ // src/lib/opencode/dependency-install-diagnostic.ts
1814
+ import { lstatSync, realpathSync } from "node:fs";
1815
+ import { basename, dirname as dirname3, isAbsolute, join as join2, relative, resolve, sep } from "node:path";
1816
+ var DEPENDENCY_INSTALL_FAILURE_MESSAGE = "background dependency install failed";
1817
+ var MAX_DISTINCT_DIAGNOSTICS = 32;
1818
+ var EFFECT = "OpenCode could not install background dependencies for this scope; its plugins and tools may be unavailable";
1819
+ function readOpenCodeLogField(line, key) {
1820
+ const match = line.match(new RegExp(`(?:^|\\s)${key}=(?:"((?:\\\\.|[^"])*)"|(\\S+))`));
1821
+ if (!match) return void 0;
1822
+ if (match[1] === void 0) return match[2];
1823
+ return match[1].replace(/\\(["\\n])/g, (_match, escaped) => {
1824
+ return escaped === "n" ? "\n" : escaped;
1825
+ });
1826
+ }
1827
+ function parseTrigger(line) {
1828
+ if (readOpenCodeLogField(line, "message") !== DEPENDENCY_INSTALL_FAILURE_MESSAGE) {
1829
+ return null;
1830
+ }
1831
+ const error2 = readOpenCodeLogField(line, "error");
1832
+ if (!error2 || !/\bEACCES\b/.test(error2)) return null;
1833
+ const errnoMatch = error2.match(/\bEACCES:\s*permission denied,\s*([a-z_]+)\s+'([^']*)'/);
1834
+ const capturedPath = errnoMatch?.[2];
1835
+ return {
1836
+ dir: readOpenCodeLogField(line, "dir"),
1837
+ op: errnoMatch?.[1],
1838
+ path: capturedPath && isAbsolute(capturedPath) ? capturedPath : void 0
1839
+ };
1840
+ }
1841
+ function getErrorCode(error2) {
1842
+ if (typeof error2 === "object" && error2 !== null && "code" in error2) {
1843
+ const code = error2.code;
1844
+ if (typeof code === "string") return code;
1845
+ }
1846
+ return "UNKNOWN";
1847
+ }
1848
+ function isWithin(path, root) {
1849
+ const candidate = resolve(path);
1850
+ const resolvedRoot = resolve(root);
1851
+ return candidate === resolvedRoot || candidate.startsWith(`${resolvedRoot}${sep}`);
1852
+ }
1853
+ function rootSpecs(context) {
1854
+ return [
1855
+ { token: "~", configured: resolve(context.homeDir) },
1856
+ { token: "$WORKSPACE", configured: resolve(context.cwd) }
1857
+ ];
1858
+ }
1859
+ function resolveRoots(context) {
1860
+ const resolved = [];
1861
+ const unresolved = [];
1862
+ for (const spec of rootSpecs(context)) {
1863
+ try {
1864
+ resolved.push({ ...spec, real: realpathSync(spec.configured) });
1865
+ } catch (_error) {
1866
+ unresolved.push(spec);
1867
+ }
1868
+ }
1869
+ return { resolved, unresolved };
1870
+ }
1871
+ function resolveNearestAncestor(failingPath, remaining, error2) {
1872
+ let ancestor = dirname3(failingPath);
1873
+ const suffix = [basename(failingPath), ...remaining];
1874
+ while (ancestor) {
1875
+ try {
1876
+ return {
1877
+ path: resolve(realpathSync(ancestor), ...suffix),
1878
+ failure: { path: resolve(failingPath), error: error2 }
1879
+ };
1880
+ } catch (ancestorError) {
1881
+ if (ancestor === dirname3(ancestor) || !getErrorCode(ancestorError).startsWith("E")) {
1882
+ return null;
1883
+ }
1884
+ suffix.unshift(basename(ancestor));
1885
+ ancestor = dirname3(ancestor);
1886
+ }
1887
+ }
1888
+ return null;
1889
+ }
1890
+ function resolveExistingPath(absolutePath) {
1891
+ if (!isAbsolute(absolutePath)) return null;
1892
+ const candidate = resolve(absolutePath);
1893
+ const remaining = [];
1894
+ let current = candidate;
1895
+ while (current) {
1896
+ try {
1897
+ const stat2 = lstatSync(current);
1898
+ if (stat2.isSymbolicLink()) {
1899
+ if (remaining.length === 0) {
1900
+ return { path: join2(realpathSync(dirname3(current)), basename(current)) };
1901
+ }
1902
+ return { path: resolve(realpathSync(current), ...remaining) };
1903
+ }
1904
+ return { path: resolve(realpathSync(current), ...remaining) };
1905
+ } catch (error2) {
1906
+ const code = getErrorCode(error2);
1907
+ if (code !== "ENOENT" && code !== "ENOTDIR") {
1908
+ return resolveNearestAncestor(current, remaining, error2);
1909
+ }
1910
+ }
1911
+ if (current === dirname3(current)) return null;
1912
+ remaining.unshift(basename(current));
1913
+ current = dirname3(current);
1914
+ }
1915
+ return null;
1916
+ }
1917
+ function tokenise(absolutePath, roots) {
1918
+ const pathResolution = resolveExistingPath(absolutePath);
1919
+ if (!pathResolution) return null;
1920
+ const root = roots.reduce((selected, candidate) => {
1921
+ if (!isWithin(pathResolution.path, candidate.real)) return selected;
1922
+ if (!selected || candidate.real.length > selected.real.length || candidate.real.length === selected.real.length && candidate.token === "$WORKSPACE") {
1923
+ return candidate;
1924
+ }
1925
+ return selected;
1926
+ }, null);
1927
+ if (!root) return null;
1928
+ const suffix = relative(root.real, pathResolution.path);
1929
+ return {
1930
+ value: suffix ? `${root.token}/${suffix.split(sep).join("/")}` : root.token,
1931
+ root,
1932
+ resolutionFailure: pathResolution.failure
1933
+ };
1934
+ }
1935
+ function classifyScope(dir, roots) {
1936
+ if (!dir) return "other";
1937
+ const tokenisedDir = tokenise(dir, roots.resolved);
1938
+ if (tokenisedDir) return tokenisedDir.root.token === "$WORKSPACE" ? "workspace" : "global";
1939
+ for (const spec of roots.unresolved) {
1940
+ if (isWithin(dir, spec.configured)) {
1941
+ return spec.token === "$WORKSPACE" ? "workspace" : "global";
1942
+ }
1943
+ }
1944
+ return "other";
1945
+ }
1946
+ function unresolvedRootForPath(path, unresolved) {
1947
+ return unresolved.find((spec) => isWithin(path, spec.configured));
1948
+ }
1949
+ function statType(stat2) {
1950
+ if (stat2.isDirectory()) return "d";
1951
+ if (stat2.isFile()) return "f";
1952
+ if (stat2.isSymbolicLink()) return "l";
1953
+ return "o";
1954
+ }
1955
+ function renderStat(name, stat2) {
1956
+ const mode = (stat2.mode & 4095).toString(8).padStart(4, "0");
1957
+ return `${name}:${statType(stat2)}:${stat2.uid}:${stat2.gid}:${mode}`;
1958
+ }
1959
+ function renderStatError(name, error2) {
1960
+ const code = getErrorCode(error2);
1961
+ return `${name}:${renderErrorCode(code)}`;
1962
+ }
1963
+ function renderErrorCode(code) {
1964
+ if (code === "ENOENT") return "missing";
1965
+ return code.startsWith("E") ? code : `E${code}`;
1966
+ }
1967
+ function inspectChain(rootReal, rootToken, absolutePath, resolutionFailure) {
1968
+ const target = resolve(absolutePath);
1969
+ const relativePath = relative(rootReal, target);
1970
+ const components = relativePath ? relativePath.split(sep) : [];
1971
+ const entries = [];
1972
+ let current = rootReal;
1973
+ let status2 = "complete";
1974
+ let finalStat;
1975
+ for (const name of [rootToken, ...components]) {
1976
+ if (name !== rootToken) current = join2(current, name);
1977
+ if (resolutionFailure && resolve(current) === resolve(resolutionFailure.path)) {
1978
+ entries.push(renderStatError(name, resolutionFailure.error));
1979
+ status2 = "stat_failed";
1980
+ break;
1981
+ }
1982
+ try {
1983
+ const stat2 = lstatSync(current);
1984
+ entries.push(renderStat(name, stat2));
1985
+ finalStat = stat2;
1986
+ } catch (error2) {
1987
+ entries.push(renderStatError(name, error2));
1988
+ if (getErrorCode(error2) !== "ENOENT") status2 = "stat_failed";
1989
+ break;
1990
+ }
1991
+ }
1992
+ let symlinkTarget;
1993
+ let symlinkMeta;
1994
+ if (finalStat?.isSymbolicLink() && entries.length === components.length + 1) {
1995
+ try {
1996
+ symlinkTarget = realpathSync(current);
1997
+ try {
1998
+ symlinkMeta = renderStat("", lstatSync(symlinkTarget)).slice(1);
1999
+ } catch (error2) {
2000
+ symlinkMeta = renderErrorCode(getErrorCode(error2));
2001
+ status2 = "stat_failed";
2002
+ }
2003
+ } catch (error2) {
2004
+ symlinkTarget = "<unresolved>";
2005
+ symlinkMeta = renderErrorCode(getErrorCode(error2));
2006
+ status2 = "stat_failed";
2007
+ }
2008
+ }
2009
+ return { status: status2, chain: entries.join(","), symlinkTarget, symlinkMeta };
2010
+ }
2011
+ function buildDiagnostic(line, context) {
2012
+ const trigger = parseTrigger(line);
2013
+ if (!trigger) return null;
2014
+ const roots = resolveRoots(context);
2015
+ const scope = classifyScope(trigger.dir, roots);
2016
+ const pathSource = trigger.path ? "error" : trigger.dir ? "dir" : "none";
2017
+ const inspectedPath = trigger.path ?? trigger.dir;
2018
+ const microvmId = context.microvmId?.trim() || "unknown";
2019
+ const euid = context.euid === void 0 ? "unavailable" : String(context.euid);
2020
+ const egid = context.egid === void 0 ? "unavailable" : String(context.egid);
2021
+ const op = trigger.op ?? "unknown";
2022
+ if (!inspectedPath) {
2023
+ return {
2024
+ 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`,
2025
+ localDetail: null
2026
+ };
2027
+ }
2028
+ const tokenisedPath = tokenise(inspectedPath, roots.resolved);
2029
+ if (!tokenisedPath) {
2030
+ const unresolvedRoot = unresolvedRootForPath(inspectedPath, roots.unresolved);
2031
+ if (unresolvedRoot) {
2032
+ return {
2033
+ 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`,
2034
+ localDetail: null
2035
+ };
2036
+ }
2037
+ return {
2038
+ 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`,
2039
+ localDetail: `OPENCODE-DEPENDENCY-INSTALL-EACCES (local only): outside approved roots: ${inspectedPath}`
2040
+ };
2041
+ }
2042
+ const inspection = inspectChain(
2043
+ tokenisedPath.root.real,
2044
+ tokenisedPath.root.token,
2045
+ inspectedPath,
2046
+ tokenisedPath.resolutionFailure
2047
+ );
2048
+ const optionalSymlinkFields = inspection.symlinkTarget ? ` symlink_target=${tokenise(inspection.symlinkTarget, roots.resolved)?.value ?? "<outside-approved-roots>"}` + (inspection.symlinkMeta ? ` symlink_meta=${inspection.symlinkMeta}` : "") : "";
2049
+ return {
2050
+ 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}`,
2051
+ localDetail: null
2052
+ };
2053
+ }
2054
+ function createDependencyInstallDiagnosticReporter({
2055
+ context,
2056
+ emitEvent: emitEvent2,
2057
+ emitLocal
2058
+ }) {
2059
+ const emittedEvents = /* @__PURE__ */ new Set();
2060
+ return (line) => {
2061
+ const diagnostic = buildDiagnostic(line, context);
2062
+ if (!diagnostic || emittedEvents.has(diagnostic.event)) return;
2063
+ if (emittedEvents.size >= MAX_DISTINCT_DIAGNOSTICS) return;
2064
+ emittedEvents.add(diagnostic.event);
2065
+ emitEvent2(diagnostic.event);
2066
+ if (diagnostic.localDetail) emitLocal(diagnostic.localDetail);
2067
+ };
2068
+ }
2069
+
1535
2070
  // src/lib/runner-activity-telemetry.ts
1536
2071
  var FORWARDED_LEVELS_BY_SOURCE = {
1537
2072
  "cli.run": /* @__PURE__ */ new Set(["warn", "error"]),
@@ -1549,11 +2084,13 @@ function parseOpenCodeLogLine(line) {
1549
2084
  if (!levelMatch) return null;
1550
2085
  const level = levelMatch[1].toUpperCase();
1551
2086
  if (level !== "WARN" && level !== "ERROR") return null;
1552
- const sessionMatch = normalisedLine.match(/(?:^|\s)sessionID=(\S+)/);
2087
+ const sessionMatch = normalisedLine.match(/(?:^|\s)(?:sessionID|session\.id)=(\S+)/);
2088
+ const messageIDMatch = normalisedLine.match(/(?:^|\s)messageID=(\S+)/);
1553
2089
  const messageMatch = normalisedLine.match(/(?:^|\s)message=(?:"((?:\\.|[^"])*)"|(\S+))/);
1554
2090
  const parsed = {
1555
2091
  level: level === "WARN" ? "warn" : "error",
1556
- sessionID: sessionMatch?.[1]
2092
+ sessionID: sessionMatch?.[1],
2093
+ messageID: messageIDMatch?.[1]
1557
2094
  };
1558
2095
  const message = messageMatch?.[1] ?? messageMatch?.[2];
1559
2096
  if (message !== void 0) parsed.message = message;
@@ -1561,18 +2098,29 @@ function parseOpenCodeLogLine(line) {
1561
2098
  }
1562
2099
  var MAX_LINE_BUFFER_BYTES = 16 * 1024;
1563
2100
  var REPEAT_SUPPRESSION_WINDOW_MS = 6e4;
1564
- function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure) {
2101
+ function sanitiseDependencyInstallWarning(line) {
2102
+ const withoutDirectory = line.replace(
2103
+ /((?:^|\s)dir=)(?:"(?:\\.|[^"])*"|\/[^\s)"']+|\S+)/g,
2104
+ "$1<path>"
2105
+ );
2106
+ return withoutDirectory.replace(
2107
+ /((?:^|\s)error=)"((?:\\.|[^"])*)"/,
2108
+ (_match, prefix, error2) => `${prefix}"${error2.replace(/'\/[^']*'/g, "'<path>'")}"`
2109
+ );
2110
+ }
2111
+ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure, onDependencyInstallFailure) {
1565
2112
  let buffer = Buffer.alloc(0);
1566
2113
  const repeatStates = /* @__PURE__ */ new Map();
1567
2114
  const forwardLine = (line, parsed, repeatsSuppressed = 0) => {
1568
2115
  const metadata = {
1569
2116
  ...parsed.sessionID ? { sessionID: parsed.sessionID } : {},
2117
+ ...parsed.messageID ? { messageID: parsed.messageID } : {},
1570
2118
  ...repeatsSuppressed > 0 ? { repeats_suppressed: repeatsSuppressed } : {}
1571
2119
  };
1572
2120
  forwardRunnerActivity(
1573
2121
  {
1574
2122
  level: parsed.level,
1575
- error: line,
2123
+ error: parsed.message === DEPENDENCY_INSTALL_FAILURE_MESSAGE ? sanitiseDependencyInstallWarning(line) : line,
1576
2124
  metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
1577
2125
  source: "opencode"
1578
2126
  },
@@ -1590,20 +2138,9 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1590
2138
  repeatStates.delete(key);
1591
2139
  }
1592
2140
  };
1593
- const flushLine = (line) => {
1594
- if (onSessionStoreWriteFailure && isSessionStoreWriteFailure(line)) {
1595
- try {
1596
- onSessionStoreWriteFailure();
1597
- } catch (err) {
1598
- reportTelemetryDiagnostic(
1599
- `Runner activity telemetry could not record session-store write failure: ${err instanceof Error ? err.message : String(err)}`
1600
- );
1601
- }
1602
- }
1603
- const parsed = parseOpenCodeLogLine(line);
1604
- if (!parsed) return;
2141
+ const forwardWithRepeatSuppression = (line, parsed) => {
1605
2142
  const now = Date.now();
1606
- const key = parsed.message === void 0 ? void 0 : `${parsed.level}\0${parsed.message}`;
2143
+ const key = parsed.message === void 0 ? void 0 : `${parsed.level}\0${parsed.message}\0${parsed.sessionID ?? ""}\0${parsed.messageID ?? ""}`;
1607
2144
  expireRepeatStates(now, key);
1608
2145
  if (key === void 0) {
1609
2146
  forwardLine(line, parsed);
@@ -1624,6 +2161,35 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1624
2161
  previous.parsed = parsed;
1625
2162
  previous.repeatsSuppressed++;
1626
2163
  };
2164
+ const flushLine = (line) => {
2165
+ const parsed = parseOpenCodeLogLine(line);
2166
+ const isDiskFailure = isSessionStoreWriteFailure(line);
2167
+ const cause = sessionStoreCauseToken(line);
2168
+ if (onSessionStoreWriteFailure && (isDiskFailure || cause !== null)) {
2169
+ try {
2170
+ onSessionStoreWriteFailure({
2171
+ isDiskFailure,
2172
+ sessionId: parsed?.sessionID ?? null,
2173
+ cause
2174
+ });
2175
+ } catch (err) {
2176
+ reportTelemetryDiagnostic(
2177
+ `Runner activity telemetry could not record session-store write failure: ${err instanceof Error ? err.message : String(err)}`
2178
+ );
2179
+ }
2180
+ }
2181
+ if (!parsed) return;
2182
+ forwardWithRepeatSuppression(line, parsed);
2183
+ if (onDependencyInstallFailure && parsed.message === DEPENDENCY_INSTALL_FAILURE_MESSAGE) {
2184
+ try {
2185
+ onDependencyInstallFailure(line);
2186
+ } catch (err) {
2187
+ reportTelemetryDiagnostic(
2188
+ `Runner activity telemetry could not record dependency-install failure diagnostic: ${err instanceof Error ? err.message : String(err)}`
2189
+ );
2190
+ }
2191
+ }
2192
+ };
1627
2193
  const forward = ((chunk) => {
1628
2194
  buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, "utf-8")]);
1629
2195
  let newlineIndex;
@@ -1649,34 +2215,38 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1649
2215
  var MAX_MESSAGE_LENGTH = 500;
1650
2216
  var MAX_METADATA_VALUE_LENGTH = 200;
1651
2217
  var MAX_METADATA_ENTRIES = 20;
1652
- var TRUNCATION_MARKER = "\u2026";
1653
- function redactHomeDirectory(message) {
1654
- const home = homedir2();
1655
- if (!home || home === "/") {
1656
- return message;
1657
- }
1658
- const pathSegmentCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-";
1659
- let result = "";
1660
- let searchFrom = 0;
1661
- let homeIndex;
1662
- while ((homeIndex = message.indexOf(home, searchFrom)) !== -1) {
1663
- const homeEnd = homeIndex + home.length;
1664
- const previousCharacter = message[homeIndex - 1];
1665
- const nextCharacter = message[homeEnd];
1666
- const atPathBoundary = home === "/" || (!previousCharacter || !pathSegmentCharacters.includes(previousCharacter)) && (!nextCharacter || !pathSegmentCharacters.includes(nextCharacter));
1667
- result += message.slice(searchFrom, homeIndex);
1668
- result += atPathBoundary ? "~" : home;
1669
- searchFrom = homeEnd;
1670
- }
1671
- return result + message.slice(searchFrom);
1672
- }
1673
- function redactDiagnosticText(message) {
1674
- 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>");
1675
- return redactHomeDirectory(redacted);
1676
- }
1677
2218
  function truncate(message) {
1678
2219
  if (message.length <= MAX_MESSAGE_LENGTH) return message;
1679
- return message.slice(0, MAX_MESSAGE_LENGTH - TRUNCATION_MARKER.length) + TRUNCATION_MARKER;
2220
+ let dropped = message.length - MAX_MESSAGE_LENGTH;
2221
+ let marker = `\u2026${dropped} chars omitted\u2026`;
2222
+ let previousDropped = -1;
2223
+ while (dropped !== previousDropped) {
2224
+ previousDropped = dropped;
2225
+ const nextDropped = message.length - MAX_MESSAGE_LENGTH + marker.length;
2226
+ dropped = nextDropped;
2227
+ marker = `\u2026${dropped} chars omitted\u2026`;
2228
+ }
2229
+ const remaining = MAX_MESSAGE_LENGTH - marker.length;
2230
+ const headLength = Math.ceil(remaining * 0.6);
2231
+ const tailLength = remaining - headLength;
2232
+ const effectMatch = message.startsWith("OPENCODE-DEPENDENCY-INSTALL-EACCES:") ? message.match(/ effect="[^"]*"/) : null;
2233
+ if (effectMatch?.index !== void 0) {
2234
+ const effectEnd = effectMatch.index + effectMatch[0].length;
2235
+ if (effectEnd > headLength && effectEnd < message.length) {
2236
+ const effectLength = effectMatch[0].length;
2237
+ const nonEffectLength = remaining - effectLength;
2238
+ const preservedTailLength = Math.min(
2239
+ message.length - effectEnd,
2240
+ nonEffectLength - Math.min(effectMatch.index, Math.ceil(nonEffectLength * 0.6))
2241
+ );
2242
+ const preservedHeadLength = Math.min(
2243
+ effectMatch.index,
2244
+ nonEffectLength - preservedTailLength
2245
+ );
2246
+ return message.slice(0, preservedHeadLength) + marker + message.slice(effectMatch.index, effectEnd) + message.slice(message.length - preservedTailLength);
2247
+ }
2248
+ }
2249
+ return message.slice(0, headLength) + marker + message.slice(message.length - tailLength);
1680
2250
  }
1681
2251
  function sanitiseMetadata(metadata) {
1682
2252
  if (!metadata) return {};
@@ -1753,10 +2323,10 @@ function forwardRunnerActivity(entry, context) {
1753
2323
 
1754
2324
  // src/lib/opencode/session-db-recovery-report.ts
1755
2325
  import { readFileSync as readFileSync2, unlinkSync } from "node:fs";
1756
- import { join as join2 } from "node:path";
2326
+ import { join as join3 } from "node:path";
1757
2327
  function sessionDbRecoveryReportPath(homeDir, env) {
1758
2328
  const override = env.EVIDENT_SESSION_DB_RECOVERY_REPORT?.trim();
1759
- return override || join2(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
2329
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
1760
2330
  }
1761
2331
  function drainSessionDbRecoveryReport({
1762
2332
  homeDir,
@@ -1886,12 +2456,23 @@ function buildSessionDbRecoveryActivity(record) {
1886
2456
  metadata: withoutContractFields(record),
1887
2457
  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}`
1888
2458
  };
1889
- case "session_db_boot_refused":
1890
- return {
1891
- level,
1892
- metadata: withoutContractFields(record),
1893
- 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.`
1894
- };
2459
+ case "session_db_boot_refused": {
2460
+ const message = (() => {
2461
+ switch (record.reason) {
2462
+ case "local_discard_failed":
2463
+ 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.`;
2464
+ case "replica_separation_unproven":
2465
+ 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.`;
2466
+ case "verification_timeout_before_separation":
2467
+ 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.";
2468
+ case "verification_timeout_during_separation":
2469
+ 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.";
2470
+ default:
2471
+ 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.";
2472
+ }
2473
+ })();
2474
+ return { level, metadata: withoutContractFields(record), message };
2475
+ }
1895
2476
  case "schema_provenance_mismatch":
1896
2477
  return {
1897
2478
  level,
@@ -2018,7 +2599,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
2018
2599
  if (health.healthy || health.authFailed) {
2019
2600
  return health;
2020
2601
  }
2021
- await new Promise((resolve4) => setTimeout(resolve4, 1e3));
2602
+ await new Promise((resolve5) => setTimeout(resolve5, 1e3));
2022
2603
  }
2023
2604
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
2024
2605
  }
@@ -2029,7 +2610,7 @@ async function waitForOpenCode2Health(port, password, timeoutMs = 3e4) {
2029
2610
  if (health.healthy || health.authFailed) {
2030
2611
  return health;
2031
2612
  }
2032
- await new Promise((resolve4) => setTimeout(resolve4, 1e3));
2613
+ await new Promise((resolve5) => setTimeout(resolve5, 1e3));
2033
2614
  }
2034
2615
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
2035
2616
  }
@@ -2141,7 +2722,7 @@ async function getOpenCodeDirectory(port, client) {
2141
2722
  return null;
2142
2723
  }
2143
2724
  }
2144
- function roleOf(m) {
2725
+ function roleOf2(m) {
2145
2726
  if (!m || typeof m !== "object") return void 0;
2146
2727
  if (typeof m.role === "string") return m.role;
2147
2728
  const infoRole = m.info?.role;
@@ -2155,13 +2736,13 @@ function createdOf(m) {
2155
2736
  if (!m || typeof m !== "object") return void 0;
2156
2737
  return m.info?.time?.created ?? m.time?.created;
2157
2738
  }
2158
- function idOf(m) {
2739
+ function idOf2(m) {
2159
2740
  if (!m || typeof m !== "object") return void 0;
2160
2741
  if (typeof m.id === "string") return m.id;
2161
2742
  const infoId = m.info?.id;
2162
2743
  return typeof infoId === "string" ? infoId : void 0;
2163
2744
  }
2164
- function parentIdOf(m) {
2745
+ function parentIdOf2(m) {
2165
2746
  if (!m || typeof m !== "object") return void 0;
2166
2747
  if (typeof m.parentID === "string") return m.parentID;
2167
2748
  const infoParent = m.info?.parentID;
@@ -2177,8 +2758,12 @@ function errorOf(m) {
2177
2758
  if (!m || typeof m !== "object") return void 0;
2178
2759
  return m.info?.error ?? m.error;
2179
2760
  }
2761
+ function isTerminalStopAnswer(m) {
2762
+ return completedOf(m) != null && errorOf(m) == null && finishOf(m) === "stop";
2763
+ }
2180
2764
  function isAssistantInFlight(m) {
2181
2765
  if (completedOf(m) == null) return true;
2766
+ if (errorOf(m) != null) return false;
2182
2767
  return finishOf(m) === "tool-calls";
2183
2768
  }
2184
2769
  async function getSessionMessages(port, sessionId, client) {
@@ -2226,7 +2811,7 @@ async function pollSessionMessagesForRedrive(port, sessionId, client) {
2226
2811
  function isSessionActivelyGenerating(messages) {
2227
2812
  if (!messages || messages.length === 0) return false;
2228
2813
  const last = messages[messages.length - 1];
2229
- if (roleOf(last) !== "assistant") return false;
2814
+ if (roleOf2(last) !== "assistant") return false;
2230
2815
  return completedOf(last) == null;
2231
2816
  }
2232
2817
  var FULL_SESSION_ENUMERATION_MAX_SESSIONS = 3200;
@@ -2292,7 +2877,18 @@ async function deleteSession(port, id, client) {
2292
2877
  { method: "DELETE" },
2293
2878
  { allowStatuses: ALL_HTTP_STATUSES }
2294
2879
  );
2295
- return { ok: res.status >= 200 && res.status < 300, status: res.status };
2880
+ if (res.status >= 200 && res.status < 300) {
2881
+ return { ok: true, status: res.status };
2882
+ }
2883
+ try {
2884
+ const body = await res.text();
2885
+ return body ? { ok: false, status: res.status, body } : { ok: false, status: res.status };
2886
+ } catch (error2) {
2887
+ console.error(
2888
+ `[deleteSession] DELETE /session/${id} response body read failed for HTTP ${res.status}: ${error2 instanceof Error ? error2.message : String(error2)}`
2889
+ );
2890
+ return { ok: false, status: res.status };
2891
+ }
2296
2892
  } catch (error2) {
2297
2893
  console.error(
2298
2894
  `[deleteSession] DELETE /session/${id} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
@@ -2535,11 +3131,31 @@ function messageText(m) {
2535
3131
  if (!m || !Array.isArray(m.parts)) return "";
2536
3132
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2537
3133
  }
2538
- async function sendPromptAsync(port, sessionId, content, options, attachments, client) {
3134
+ function findDispatchedUserMessageId(messages, content, excludeIds) {
3135
+ if (!messages || messages.length === 0) return null;
3136
+ let best = null;
3137
+ for (const message of messages) {
3138
+ if (roleOf2(message) !== "user") continue;
3139
+ const id = idOf2(message);
3140
+ if (typeof id !== "string" || excludeIds?.has(id)) continue;
3141
+ if (messageText(message) !== content) continue;
3142
+ const created = createdOf(message) ?? 0;
3143
+ if (best === null || created > best.created) best = { id, created };
3144
+ }
3145
+ return best?.id ?? null;
3146
+ }
3147
+ async function sendPromptAsync(port, sessionId, content, options, attachments, client, callbacks) {
2539
3148
  const before = await getSessionMessages(port, sessionId, client);
2540
3149
  const knownUserIds = new Set(
2541
- (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
3150
+ (before ?? []).filter((m) => roleOf2(m) === "user").map((m) => idOf2(m)).filter((id) => typeof id === "string")
2542
3151
  );
3152
+ if (callbacks) {
3153
+ try {
3154
+ callbacks.onKnownUserIds(before === null ? null : knownUserIds);
3155
+ } catch (error2) {
3156
+ callbacks.onKnownUserIdsError(error2);
3157
+ }
3158
+ }
2543
3159
  const parts = [{ type: "text", text: content }];
2544
3160
  let pendingOutcomes = null;
2545
3161
  if (attachments && attachments.inputs.length > 0) {
@@ -2578,25 +3194,13 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2578
3194
  const READ_BACK_DELAY_MS = 150;
2579
3195
  for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
2580
3196
  const after = await getSessionMessages(port, sessionId, client);
2581
- if (after) {
2582
- let best = null;
2583
- for (const m of after) {
2584
- if (roleOf(m) !== "user") continue;
2585
- const id = idOf(m);
2586
- if (typeof id !== "string" || knownUserIds.has(id)) continue;
2587
- if (messageText(m) !== content) continue;
2588
- const created = createdOf(m) ?? 0;
2589
- if (best === null || created > best.created) {
2590
- best = { id, created };
2591
- }
2592
- }
2593
- if (best) {
2594
- if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
2595
- return best.id;
2596
- }
3197
+ const dispatchedUserMessageId = findDispatchedUserMessageId(after, content, knownUserIds);
3198
+ if (dispatchedUserMessageId !== null) {
3199
+ if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
3200
+ return dispatchedUserMessageId;
2597
3201
  }
2598
3202
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2599
- await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
3203
+ await new Promise((resolve5) => setTimeout(resolve5, READ_BACK_DELAY_MS));
2600
3204
  }
2601
3205
  }
2602
3206
  return null;
@@ -2604,57 +3208,68 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2604
3208
  function findAssistantReplyAfter(messages, userMessageId) {
2605
3209
  if (!messages || messages.length === 0) return null;
2606
3210
  const byParent = messages.find(
2607
- (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
3211
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2608
3212
  );
2609
3213
  if (byParent) return byParent;
2610
- const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
3214
+ const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2611
3215
  if (userIndex === -1) return null;
2612
3216
  for (let i = userIndex + 1; i < messages.length; i++) {
2613
- if (roleOf(messages[i]) === "assistant") return messages[i];
3217
+ if (roleOf2(messages[i]) === "assistant") return messages[i];
2614
3218
  }
2615
3219
  return null;
2616
3220
  }
2617
3221
  function findLastAssistantReplyFor(messages, userMessageId) {
2618
3222
  if (!messages || messages.length === 0) return null;
2619
3223
  let lastCorrelated = null;
2620
- let lastNonErrored = null;
2621
3224
  for (let i = messages.length - 1; i >= 0; i--) {
2622
3225
  const m = messages[i];
2623
- if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
2624
- if (lastCorrelated === null) lastCorrelated = m;
2625
- if (errorOf(m) == null) {
2626
- lastNonErrored = m;
2627
- break;
3226
+ if (roleOf2(m) !== "assistant" || parentIdOf2(m) !== userMessageId) continue;
3227
+ lastCorrelated = m;
3228
+ break;
3229
+ }
3230
+ if (lastCorrelated) {
3231
+ if (errorOf(lastCorrelated) == null) return lastCorrelated;
3232
+ for (let i = messages.length - 1; i >= 0; i--) {
3233
+ const m = messages[i];
3234
+ if (roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId && isTerminalStopAnswer(m)) {
3235
+ return m;
3236
+ }
2628
3237
  }
3238
+ return lastCorrelated;
2629
3239
  }
2630
- if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
2631
- const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
3240
+ const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2632
3241
  if (userIndex === -1) return null;
2633
3242
  let last = null;
2634
- let lastOk = null;
3243
+ let lastAssistantIndex = -1;
2635
3244
  for (let i = userIndex + 1; i < messages.length; i++) {
2636
- const role = roleOf(messages[i]);
3245
+ const role = roleOf2(messages[i]);
2637
3246
  if (role === "user") break;
2638
3247
  if (role === "assistant") {
2639
3248
  last = messages[i];
2640
- if (errorOf(messages[i]) == null) lastOk = messages[i];
3249
+ lastAssistantIndex = i;
2641
3250
  }
2642
3251
  }
2643
- return lastOk ?? last;
3252
+ if (last === null || errorOf(last) == null) return last;
3253
+ for (let i = lastAssistantIndex; i > userIndex; i--) {
3254
+ const m = messages[i];
3255
+ if (roleOf2(m) === "user") break;
3256
+ if (roleOf2(m) === "assistant" && isTerminalStopAnswer(m)) return m;
3257
+ }
3258
+ return last;
2644
3259
  }
2645
3260
  function collectSubagentSessions(messages, userMessageId) {
2646
3261
  if (!messages || messages.length === 0) return [];
2647
3262
  const byParent = messages.filter(
2648
- (message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
3263
+ (message) => roleOf2(message) === "assistant" && parentIdOf2(message) === userMessageId
2649
3264
  );
2650
3265
  const assistants = byParent.length > 0 ? byParent : [];
2651
3266
  if (assistants.length === 0) {
2652
- const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3267
+ const userIndex = messages.findIndex((message) => idOf2(message) === userMessageId);
2653
3268
  if (userIndex === -1) return [];
2654
3269
  for (let i = userIndex + 1; i < messages.length; i++) {
2655
3270
  const message = messages[i];
2656
- if (roleOf(message) === "user") break;
2657
- if (roleOf(message) === "assistant") assistants.push(message);
3271
+ if (roleOf2(message) === "user") break;
3272
+ if (roleOf2(message) === "assistant") assistants.push(message);
2658
3273
  }
2659
3274
  }
2660
3275
  const refs = [];
@@ -2694,7 +3309,7 @@ function collectTaskCalls(messages, userMessageId) {
2694
3309
  if (!messages || messages.length === 0) return [];
2695
3310
  const calls = [];
2696
3311
  for (const message of messages) {
2697
- if (roleOf(message) !== "assistant" || parentIdOf(message) !== userMessageId) continue;
3312
+ if (roleOf2(message) !== "assistant" || parentIdOf2(message) !== userMessageId) continue;
2698
3313
  for (const part of message.parts ?? []) {
2699
3314
  if (part.tool !== "task" || !part.callID || !part.state || part.state.status === "pending") {
2700
3315
  continue;
@@ -2724,7 +3339,7 @@ function attributeTaskCallUsage(messages, windows) {
2724
3339
  for (const window of eligibleWindows) assignments.set(window.callID, []);
2725
3340
  const unattributed = [];
2726
3341
  for (const message of messages ?? []) {
2727
- if (roleOf(message) !== "assistant") continue;
3342
+ if (roleOf2(message) !== "assistant") continue;
2728
3343
  const created = finiteNumber(createdOf(message));
2729
3344
  const matching = created === null ? [] : eligibleWindows.filter(
2730
3345
  (window) => window.timeStart <= created && (window.timeEnd === null || window.timeEnd === void 0 || created <= window.timeEnd)
@@ -2746,8 +3361,9 @@ function attributeTaskCallUsage(messages, windows) {
2746
3361
  }
2747
3362
  function sumAssistantUsage(messages) {
2748
3363
  if (!messages || messages.length === 0) return null;
2749
- const nonErrored = messages.filter((message) => errorOf(message) == null);
2750
- const selected = nonErrored.length > 0 ? nonErrored : messages;
3364
+ const hasTrailingError = errorOf(messages[messages.length - 1]) != null;
3365
+ const hasTerminalStopAnswer = messages.some(isTerminalStopAnswer);
3366
+ const selected = hasTrailingError && hasTerminalStopAnswer ? messages.filter((message) => errorOf(message) == null) : messages;
2751
3367
  let sawAnyUsage = false;
2752
3368
  let inputSum = 0;
2753
3369
  let outputSum = 0;
@@ -2756,9 +3372,11 @@ function sumAssistantUsage(messages) {
2756
3372
  let cacheWriteSum = 0;
2757
3373
  let costSum = 0;
2758
3374
  let sawCost = false;
3375
+ let toolCallCount = 0;
2759
3376
  let modelId = null;
2760
3377
  let providerId = null;
2761
3378
  for (const m of selected) {
3379
+ toolCallCount += m.parts?.filter((part) => part.type === "tool").length ?? 0;
2762
3380
  const info = m.info;
2763
3381
  if (!info) continue;
2764
3382
  const tokens = info.tokens;
@@ -2795,19 +3413,18 @@ function sumAssistantUsage(messages) {
2795
3413
  usage_tokens_cache_write: cacheWriteSum,
2796
3414
  // NULL means OpenCode never reported a cost; it is distinct from a genuine
2797
3415
  // zero-cost message, which sets `sawCost` with `costSum === 0`.
2798
- usage_cost_usd: sawCost ? costSum : null
3416
+ usage_cost_usd: sawCost ? costSum : null,
3417
+ turn_tool_calls: toolCallCount
2799
3418
  };
2800
3419
  }
2801
3420
  function messageUsage(messages, userMessageId) {
2802
3421
  if (!messages || messages.length === 0) return null;
2803
3422
  const byParentAll = messages.filter(
2804
- (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
3423
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2805
3424
  );
2806
- const byParentNonErrored = byParentAll.filter((m) => errorOf(m) == null);
2807
- const byParent = byParentNonErrored.length > 0 ? byParentNonErrored : byParentAll;
2808
3425
  let correlated;
2809
- if (byParent.length > 0) {
2810
- correlated = byParent;
3426
+ if (byParentAll.length > 0) {
3427
+ correlated = byParentAll;
2811
3428
  } else {
2812
3429
  const reply = findAssistantReplyAfter(messages, userMessageId);
2813
3430
  correlated = reply ? [reply] : [];
@@ -2817,7 +3434,7 @@ function messageUsage(messages, userMessageId) {
2817
3434
  }
2818
3435
  function messageRunState(messages, userMessageId) {
2819
3436
  if (!messages || messages.length === 0) return "unknown";
2820
- const hasUser = messages.some((m) => idOf(m) === userMessageId);
3437
+ const hasUser = messages.some((m) => idOf2(m) === userMessageId);
2821
3438
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2822
3439
  if (!hasUser) {
2823
3440
  if (!reply) return "unknown";
@@ -2853,14 +3470,38 @@ function messageError(messages, userMessageId) {
2853
3470
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2854
3471
  const error2 = errorOf(reply);
2855
3472
  if (error2 == null) return null;
2856
- if (typeof error2 === "string") return error2;
2857
- if (typeof error2 === "object") {
3473
+ let rendered;
3474
+ if (typeof error2 === "string") {
3475
+ rendered = error2;
3476
+ } else if (typeof error2 === "object") {
2858
3477
  const e = error2;
2859
3478
  const dataMessage = e.data?.message;
2860
- if (typeof dataMessage === "string") return dataMessage;
2861
- if (typeof e.message === "string") return e.message;
3479
+ if (typeof dataMessage === "string") {
3480
+ rendered = dataMessage;
3481
+ } else if (typeof e.message === "string") {
3482
+ rendered = e.message;
3483
+ } else {
3484
+ rendered = "The agent run failed.";
3485
+ }
3486
+ } else {
3487
+ rendered = "The agent run failed.";
2862
3488
  }
2863
- return "The agent run failed.";
3489
+ const toolPart = Array.isArray(reply?.parts) ? reply.parts.find(
3490
+ (part) => part && typeof part === "object" && part.type === "tool" && part.state?.status === "error"
3491
+ ) : void 0;
3492
+ return typeof toolPart?.tool === "string" && toolPart.tool.length > 0 ? `${rendered} (tool "${toolPart.tool}")` : rendered;
3493
+ }
3494
+ function describeAbandonedReplyShape(messages, userMessageId) {
3495
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
3496
+ if (!reply) return null;
3497
+ const parts = Array.isArray(reply.parts) ? reply.parts : [];
3498
+ const lastTool = [...parts].reverse().find((part) => part?.type === "tool");
3499
+ return {
3500
+ partTypes: parts.slice(-12).map((part) => typeof part?.type === "string" ? part.type : "(unknown)"),
3501
+ droppedPartCount: Math.max(0, parts.length - 12),
3502
+ lastToolName: typeof lastTool?.tool === "string" && lastTool.tool.length > 0 ? lastTool.tool : null,
3503
+ lastToolStatus: typeof lastTool?.state?.status === "string" && lastTool.state.status.length > 0 ? lastTool.state.status : null
3504
+ };
2864
3505
  }
2865
3506
  function isAbortedTerminalReply(messages, userMessageId) {
2866
3507
  const reply = findLastAssistantReplyFor(messages, userMessageId);
@@ -2877,19 +3518,32 @@ function isAbortedTerminalReply(messages, userMessageId) {
2877
3518
  }
2878
3519
  return false;
2879
3520
  }
3521
+ var CLAUDE_AUTH_EXPIRY_MESSAGES = Object.freeze([
3522
+ "Claude Code credentials are unavailable or expired. Run `claude` to refresh them."
3523
+ ]);
3524
+ function extractReplyErrorShape(reply) {
3525
+ const error2 = errorOf(reply);
3526
+ if (error2 == null || typeof error2 !== "object") {
3527
+ return { name: null, statusCode: null };
3528
+ }
3529
+ const e = error2;
3530
+ const name = typeof e.name === "string" ? e.name.slice(0, 100) : null;
3531
+ const data = e.data;
3532
+ const statusCode = typeof data?.statusCode === "number" && Number.isFinite(data.statusCode) ? data.statusCode : null;
3533
+ return { name, statusCode };
3534
+ }
2880
3535
  function classifyReplyAuthError(reply) {
2881
3536
  const error2 = errorOf(reply);
2882
3537
  if (error2 == null || typeof error2 !== "object") return null;
2883
3538
  const e = error2;
2884
3539
  const replyProviderId = reply?.info?.providerID ?? null;
2885
3540
  const replyModelId = reply?.info?.modelID ?? null;
3541
+ const data = e.data;
2886
3542
  if (e.name === "ProviderAuthError") {
2887
- const data = e.data;
2888
3543
  const providerId = typeof data?.providerID === "string" && data.providerID || replyProviderId;
2889
3544
  return { kind: "model_auth", providerId, modelId: replyModelId, reason: "missing" };
2890
3545
  }
2891
3546
  if (e.name === "APIError") {
2892
- const data = e.data;
2893
3547
  const statusCode = data?.statusCode;
2894
3548
  if (statusCode === 401 || statusCode === 403) {
2895
3549
  return {
@@ -2900,6 +3554,15 @@ function classifyReplyAuthError(reply) {
2900
3554
  };
2901
3555
  }
2902
3556
  }
3557
+ const errorMessage3 = typeof data?.message === "string" ? data.message : typeof e.message === "string" ? e.message : null;
3558
+ if (errorMessage3 !== null && CLAUDE_AUTH_EXPIRY_MESSAGES.some((knownMessage) => errorMessage3.includes(knownMessage))) {
3559
+ return {
3560
+ kind: "model_auth",
3561
+ providerId: typeof data?.providerID === "string" && data.providerID || replyProviderId,
3562
+ modelId: replyModelId,
3563
+ reason: "rejected"
3564
+ };
3565
+ }
2903
3566
  return null;
2904
3567
  }
2905
3568
  function messageFailure(messages, userMessageId) {
@@ -2909,7 +3572,7 @@ function findLatestSubagentAuthOutcome(messages, sinceMs) {
2909
3572
  if (!messages || messages.length === 0) return null;
2910
3573
  for (let i = messages.length - 1; i >= 0; i--) {
2911
3574
  const message = messages[i];
2912
- if (roleOf(message) !== "assistant") continue;
3575
+ if (roleOf2(message) !== "assistant") continue;
2913
3576
  const created = createdOf(message);
2914
3577
  if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
2915
3578
  const failure = classifyReplyAuthError(message);
@@ -2941,25 +3604,25 @@ function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProvi
2941
3604
  function hasRunningAssistantExcept(messages, exceptUserMessageId) {
2942
3605
  if (!messages || messages.length === 0) return false;
2943
3606
  return messages.some(
2944
- (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
3607
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) !== exceptUserMessageId && isAssistantInFlight(m)
2945
3608
  );
2946
3609
  }
2947
3610
  function hasLaterSiblingTurnStarted(messages, userMessageId, siblingUserMessageIds) {
2948
3611
  if (!messages || messages.length === 0) return false;
2949
- const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3612
+ const userIndex = messages.findIndex((message) => idOf2(message) === userMessageId);
2950
3613
  if (userIndex === -1) return false;
2951
3614
  let hasLaterUser = false;
2952
3615
  let hasStartedLaterUser = false;
2953
3616
  for (let i = userIndex + 1; i < messages.length; i++) {
2954
3617
  const message = messages[i];
2955
- if (roleOf(message) !== "user") continue;
3618
+ if (roleOf2(message) !== "user") continue;
2956
3619
  hasLaterUser = true;
2957
- const laterUserMessageId = idOf(message);
3620
+ const laterUserMessageId = idOf2(message);
2958
3621
  if (laterUserMessageId === void 0 || !siblingUserMessageIds.has(laterUserMessageId)) {
2959
3622
  return false;
2960
3623
  }
2961
3624
  if (messages.some(
2962
- (candidate) => roleOf(candidate) === "assistant" && parentIdOf(candidate) === laterUserMessageId
3625
+ (candidate) => roleOf2(candidate) === "assistant" && parentIdOf2(candidate) === laterUserMessageId
2963
3626
  )) {
2964
3627
  hasStartedLaterUser = true;
2965
3628
  }
@@ -3435,7 +4098,18 @@ async function deleteV2Session(client, sessionId) {
3435
4098
  { method: "DELETE" },
3436
4099
  { allowStatuses: Array.from({ length: 500 }, (_, index) => index + 100) }
3437
4100
  );
3438
- return { ok: response.status >= 200 && response.status < 300, status: response.status };
4101
+ if (response.status >= 200 && response.status < 300) {
4102
+ return { ok: true, status: response.status };
4103
+ }
4104
+ try {
4105
+ const body = await response.text();
4106
+ return body ? { ok: false, status: response.status, body } : { ok: false, status: response.status };
4107
+ } catch (error2) {
4108
+ console.error(
4109
+ `[deleteV2Session] DELETE /api/session/${sessionId} response body read failed for HTTP ${response.status}: ${error2 instanceof Error ? error2.message : String(error2)}`
4110
+ );
4111
+ return { ok: false, status: response.status };
4112
+ }
3439
4113
  } catch (error2) {
3440
4114
  console.error(
3441
4115
  `[deleteV2Session] DELETE /api/session/${sessionId} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
@@ -3611,12 +4285,12 @@ async function readV2SessionErrorStream(client, options) {
3611
4285
 
3612
4286
  // src/lib/opencode/credential-import-v2.ts
3613
4287
  import { readFileSync as readFileSync4 } from "node:fs";
3614
- import { join as join4 } from "node:path";
4288
+ import { join as join5 } from "node:path";
3615
4289
 
3616
4290
  // src/lib/openai-usage.ts
3617
4291
  import { readFileSync as readFileSync3 } from "node:fs";
3618
4292
  import { homedir as homedir3 } from "node:os";
3619
- import { join as join3 } from "node:path";
4293
+ import { join as join4 } from "node:path";
3620
4294
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3621
4295
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3622
4296
  var OpenAiUsageError = class extends Error {
@@ -3630,7 +4304,7 @@ function isLocalCredentialProblem2(err) {
3630
4304
  }
3631
4305
  function readOpenCodeChatGptCredentials() {
3632
4306
  try {
3633
- const raw = readFileSync3(join3(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4307
+ const raw = readFileSync3(join4(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3634
4308
  let parsed;
3635
4309
  try {
3636
4310
  parsed = JSON.parse(raw);
@@ -3856,13 +4530,13 @@ var INTEGRATION_LIST_RETRY_DELAY_MS = 1e3;
3856
4530
  function isRecord3(value) {
3857
4531
  return typeof value === "object" && value !== null && !Array.isArray(value);
3858
4532
  }
3859
- var sleep2 = (milliseconds) => new Promise((resolve4) => setTimeout(resolve4, milliseconds));
4533
+ var sleep2 = (milliseconds) => new Promise((resolve5) => setTimeout(resolve5, milliseconds));
3860
4534
  function emptyResult(unsupported = [], outcome = "completed") {
3861
4535
  return { outcome, imported: [], alreadyConnected: [], unsupported, failed: [] };
3862
4536
  }
3863
4537
  function readOpenCodeCredentials(homeDir, log3) {
3864
4538
  try {
3865
- const raw = readFileSync4(join4(homeDir, ...OPENCODE_AUTH_SEGMENTS), "utf8");
4539
+ const raw = readFileSync4(join5(homeDir, ...OPENCODE_AUTH_SEGMENTS), "utf8");
3866
4540
  let parsed;
3867
4541
  try {
3868
4542
  parsed = JSON.parse(raw);
@@ -4064,7 +4738,7 @@ async function importOpenCodeV2Credentials({
4064
4738
  import { spawn as spawn2 } from "node:child_process";
4065
4739
  import { mkdirSync, statSync as statSync3, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
4066
4740
  import { homedir as homedir4 } from "node:os";
4067
- import { dirname as dirname3, resolve as resolvePath } from "node:path";
4741
+ import { dirname as dirname4, resolve as resolvePath } from "node:path";
4068
4742
 
4069
4743
  // src/lib/runner-synchroniser.ts
4070
4744
  import { spawn } from "node:child_process";
@@ -4074,7 +4748,7 @@ function appendError(stderr, error2) {
4074
4748
  ${message}`;
4075
4749
  }
4076
4750
  function runSynchroniser(args, opts) {
4077
- return new Promise((resolve4) => {
4751
+ return new Promise((resolve5) => {
4078
4752
  let child;
4079
4753
  let stdout = "";
4080
4754
  let stderr = "";
@@ -4088,7 +4762,7 @@ function runSynchroniser(args, opts) {
4088
4762
  if (timer.handle) clearTimeout(timer.handle);
4089
4763
  if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
4090
4764
  if (spawnListener) child.removeListener("spawn", spawnListener);
4091
- resolve4(result);
4765
+ resolve5(result);
4092
4766
  };
4093
4767
  try {
4094
4768
  child = spawn("runner-synchroniser", args, {
@@ -4144,9 +4818,12 @@ function runSynchroniser(args, opts) {
4144
4818
 
4145
4819
  // src/lib/opencode/session-db-boot.ts
4146
4820
  var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
4147
- var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
4821
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 36e4;
4148
4822
  var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
4149
- var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
4823
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 40;
4824
+ var SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS = 30;
4825
+ var SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS = 120;
4826
+ var SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS = 90;
4150
4827
  function commandError(result) {
4151
4828
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
4152
4829
  }
@@ -4189,7 +4866,7 @@ function clearMarker(options) {
4189
4866
  function markNoReplicate(options, message) {
4190
4867
  if (options.noReplicateMarker) {
4191
4868
  try {
4192
- mkdirSync(dirname3(options.noReplicateMarker), { recursive: true });
4869
+ mkdirSync(dirname4(options.noReplicateMarker), { recursive: true });
4193
4870
  writeFileSync(options.noReplicateMarker, "");
4194
4871
  } catch (error2) {
4195
4872
  options.log(
@@ -4248,7 +4925,7 @@ function parseSynchroniserEnv(stdout) {
4248
4925
  return values;
4249
4926
  }
4250
4927
  function runCommand(command, args, options) {
4251
- return new Promise((resolve4) => {
4928
+ return new Promise((resolve5) => {
4252
4929
  let child;
4253
4930
  let stdout = "";
4254
4931
  let stderr = "";
@@ -4257,7 +4934,7 @@ function runCommand(command, args, options) {
4257
4934
  if (settled) return;
4258
4935
  settled = true;
4259
4936
  if (timer) clearTimeout(timer);
4260
- resolve4(result);
4937
+ resolve5(result);
4261
4938
  };
4262
4939
  try {
4263
4940
  child = spawn2(command, args, {
@@ -4265,7 +4942,7 @@ function runCommand(command, args, options) {
4265
4942
  stdio: ["ignore", "pipe", "pipe"]
4266
4943
  });
4267
4944
  } catch (error2) {
4268
- resolve4({
4945
+ resolve5({
4269
4946
  code: null,
4270
4947
  stdout,
4271
4948
  stderr: error2 instanceof Error ? error2.message : String(error2),
@@ -4344,7 +5021,7 @@ async function ensureLitestreamConfig(options, env) {
4344
5021
  return null;
4345
5022
  }
4346
5023
  try {
4347
- mkdirSync(dirname3(configPath), { recursive: true });
5024
+ mkdirSync(dirname4(configPath), { recursive: true });
4348
5025
  writeFileSync(configPath, rendered.stdout);
4349
5026
  } catch (error2) {
4350
5027
  options.log(
@@ -4457,20 +5134,37 @@ async function verifySessionDb(options, configPath, env) {
4457
5134
  timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
4458
5135
  env: {
4459
5136
  ...env,
4460
- // The synchroniser reads this value in SECONDS. Keep this at 120, not
4461
- // 120_000, so the walkback gives up before the outer process bound.
5137
+ // The synchroniser reads all four values in SECONDS.
4462
5138
  EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
4463
5139
  SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
5140
+ ),
5141
+ EVIDENT_SESSION_DB_RESTORE_POINTS_TIMEOUT_SECONDS: String(
5142
+ SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS
5143
+ ),
5144
+ EVIDENT_SESSION_DB_RESTORE_CANDIDATE_TIMEOUT_SECONDS: String(
5145
+ SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS
5146
+ ),
5147
+ EVIDENT_SESSION_DB_DISPOSAL_BUDGET_SECONDS: String(
5148
+ SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS
4464
5149
  )
4465
5150
  }
4466
5151
  });
4467
5152
  logSynchroniserDiagnostics(result, options);
4468
5153
  if (result.timedOut || result.code === 124 || result.code === 137) {
5154
+ if (result.stderr.includes("SESSION-DB-INTEGRITY-FAILED")) {
5155
+ const reason = result.stderr.includes("SESSION-DB-REPLICA-SIZE") ? "verification_timeout_during_separation" : "verification_timeout_before_separation";
5156
+ options.log(
5157
+ `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})`,
5158
+ "error"
5159
+ );
5160
+ reportRecord("verify", "session_db_boot_refused", reason, null, options);
5161
+ return { verifyFatal: true, verifyFatalReason: reason };
5162
+ }
4469
5163
  options.log(
4470
5164
  `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`,
4471
5165
  "warn"
4472
5166
  );
4473
- return false;
5167
+ return { verifyFatal: false };
4474
5168
  }
4475
5169
  if (result.code === 34) {
4476
5170
  reportRecord(
@@ -4480,14 +5174,14 @@ async function verifySessionDb(options, configPath, env) {
4480
5174
  null,
4481
5175
  options
4482
5176
  );
4483
- return true;
5177
+ return { verifyFatal: true };
4484
5178
  }
4485
5179
  if (result.code === 33) {
4486
5180
  options.log(
4487
5181
  "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
4488
5182
  "warn"
4489
5183
  );
4490
- return false;
5184
+ return { verifyFatal: false };
4491
5185
  }
4492
5186
  if (result.code !== 0) {
4493
5187
  options.log(
@@ -4495,13 +5189,13 @@ async function verifySessionDb(options, configPath, env) {
4495
5189
  "warn"
4496
5190
  );
4497
5191
  }
4498
- return false;
5192
+ return { verifyFatal: false };
4499
5193
  }
4500
5194
  options.log(
4501
5195
  "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
4502
5196
  "debug"
4503
5197
  );
4504
- return false;
5198
+ return { verifyFatal: false };
4505
5199
  }
4506
5200
  function fileExists(path) {
4507
5201
  try {
@@ -4588,13 +5282,13 @@ async function restoreAndVerifySessionDb(options) {
4588
5282
  if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
4589
5283
  return { verifyFatal: false };
4590
5284
  }
4591
- return { verifyFatal: await verifySessionDb(options, configPath, env) };
5285
+ return verifySessionDb(options, configPath, env);
4592
5286
  }
4593
5287
 
4594
5288
  // src/lib/opencode/session-db-provenance.ts
4595
5289
  import { createRequire } from "node:module";
4596
5290
  import { mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "node:fs";
4597
- import { dirname as dirname4, join as join5 } from "node:path";
5291
+ import { dirname as dirname5, join as join6 } from "node:path";
4598
5292
  var require2 = createRequire(import.meta.url);
4599
5293
  function readSessionDbMigrationIds(dbPath) {
4600
5294
  let db;
@@ -4631,7 +5325,7 @@ function readSessionDbMigrationIds(dbPath) {
4631
5325
  }
4632
5326
  function sessionDbProvenanceStatePath(homeDir, env) {
4633
5327
  const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
4634
- return override || join5(homeDir, ".local", "state", "evident", "session-db-provenance.json");
5328
+ return override || join6(homeDir, ".local", "state", "evident", "session-db-provenance.json");
4635
5329
  }
4636
5330
  function loadSessionDbProvenanceState(path) {
4637
5331
  let value;
@@ -4660,7 +5354,7 @@ function loadSessionDbProvenanceState(path) {
4660
5354
  }
4661
5355
  function saveSessionDbProvenanceState(path, state) {
4662
5356
  try {
4663
- mkdirSync2(dirname4(path), { recursive: true });
5357
+ mkdirSync2(dirname5(path), { recursive: true });
4664
5358
  writeFileSync2(path, `${JSON.stringify(state, null, 2)}
4665
5359
  `, "utf8");
4666
5360
  } catch (error2) {
@@ -4751,7 +5445,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
4751
5445
  if (child.exitCode !== null || child.signalCode !== null) {
4752
5446
  return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
4753
5447
  }
4754
- return new Promise((resolve4, reject) => {
5448
+ return new Promise((resolve5, reject) => {
4755
5449
  let forced = false;
4756
5450
  let settled = false;
4757
5451
  const timer = setTimeout(() => {
@@ -4771,7 +5465,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
4771
5465
  settled = true;
4772
5466
  clearTimeout(timer);
4773
5467
  child.removeListener("exit", onExit);
4774
- resolve4(result);
5468
+ resolve5(result);
4775
5469
  };
4776
5470
  const fail = (error2) => {
4777
5471
  if (settled) return;
@@ -5247,7 +5941,7 @@ function selectSessionsToDelete(sessions, opts) {
5247
5941
  );
5248
5942
  }
5249
5943
  var DEFAULT_INTERVAL = "1h";
5250
- function resolve(flag, envValue, fallback) {
5944
+ function resolve2(flag, envValue, fallback) {
5251
5945
  return flag ?? envValue ?? fallback;
5252
5946
  }
5253
5947
  function parseMaxCount(input) {
@@ -5263,9 +5957,9 @@ function parseMaxCount(input) {
5263
5957
  }
5264
5958
  function resolveSessionCleanupConfig(flags, env = process.env) {
5265
5959
  const warnings = [];
5266
- const maxAgeRaw = resolve(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
5267
- const maxCountRaw = resolve(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
5268
- const intervalRaw = resolve(
5960
+ const maxAgeRaw = resolve2(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
5961
+ const maxCountRaw = resolve2(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
5962
+ const intervalRaw = resolve2(
5269
5963
  flags.interval,
5270
5964
  env.EVIDENT_SESSION_CLEANUP_INTERVAL,
5271
5965
  DEFAULT_INTERVAL
@@ -5305,10 +5999,10 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
5305
5999
 
5306
6000
  // src/lib/opencode/session-db-size.ts
5307
6001
  import { statSync as statSync4 } from "node:fs";
5308
- import { join as join6 } from "node:path";
6002
+ import { join as join7 } from "node:path";
5309
6003
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
5310
6004
  function statSessionDbBytes(homeDir) {
5311
- const dbPath = join6(homeDir, ".local", "share", "opencode", "opencode.db");
6005
+ const dbPath = join7(homeDir, ".local", "share", "opencode", "opencode.db");
5312
6006
  try {
5313
6007
  return statSync4(dbPath).size;
5314
6008
  } catch (err) {
@@ -5338,12 +6032,12 @@ function buildSessionStoreSizeWarning(input) {
5338
6032
  // src/lib/opencode/log-tail.ts
5339
6033
  import { statSync as statSync5 } from "node:fs";
5340
6034
  import { homedir as homedir5 } from "node:os";
5341
- import { join as join7 } from "node:path";
6035
+ import { join as join8 } from "node:path";
5342
6036
  import { open as open2, stat } from "node:fs/promises";
5343
6037
  var DEFAULT_POLL_INTERVAL_MS = 1e3;
5344
6038
  function resolveOpenCodeLogPath(homeDir = homedir5(), env = process.env) {
5345
- const dataDir = env.XDG_DATA_HOME || join7(homeDir, ".local", "share");
5346
- return join7(dataDir, "opencode", "log", "opencode.log");
6039
+ const dataDir = env.XDG_DATA_HOME || join8(homeDir, ".local", "share");
6040
+ return join8(dataDir, "opencode", "log", "opencode.log");
5347
6041
  }
5348
6042
  function isEnoent(error2) {
5349
6043
  return error2?.code === "ENOENT";
@@ -5498,8 +6192,8 @@ var StreamForwarder = class {
5498
6192
  pauseStream(sid, stream) {
5499
6193
  if (!stream.paused) {
5500
6194
  stream.paused = true;
5501
- stream.pausePromise = new Promise((resolve4) => {
5502
- stream.resume = resolve4;
6195
+ stream.pausePromise = new Promise((resolve5) => {
6196
+ stream.resume = resolve5;
5503
6197
  });
5504
6198
  }
5505
6199
  if (stream.pauseTimer !== null) clearTimeout(stream.pauseTimer);
@@ -5566,12 +6260,12 @@ var StreamForwarder = class {
5566
6260
  let endBody;
5567
6261
  if (has_body) {
5568
6262
  const chunks = [];
5569
- bodyPromise = new Promise((resolve4) => {
6263
+ bodyPromise = new Promise((resolve5) => {
5570
6264
  pushBody = (buf) => {
5571
6265
  chunks.push(buf);
5572
6266
  };
5573
6267
  endBody = () => {
5574
- resolve4(Buffer.concat(chunks));
6268
+ resolve5(Buffer.concat(chunks));
5575
6269
  };
5576
6270
  });
5577
6271
  }
@@ -5742,7 +6436,7 @@ function connectTunnel(options) {
5742
6436
  } = options;
5743
6437
  const tunnelUrl = getTunnelUrlConfig();
5744
6438
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
5745
- return new Promise((resolve4, reject) => {
6439
+ return new Promise((resolve5, reject) => {
5746
6440
  const ws = new WebSocket2(url, {
5747
6441
  headers: {
5748
6442
  Authorization: authHeader,
@@ -5851,7 +6545,7 @@ function connectTunnel(options) {
5851
6545
  forwarder.setBinaryFramesSupported(message.binary_frames === true);
5852
6546
  const connectedAgentId = message.agent_id ?? agentId;
5853
6547
  onConnected?.(connectedAgentId);
5854
- resolve4({
6548
+ resolve5({
5855
6549
  ws,
5856
6550
  close: () => ws.close(1e3, "CLI shutdown")
5857
6551
  });
@@ -6369,6 +7063,7 @@ function createSessionStoreHealth(options = {}) {
6369
7063
  const stalenessMs = Math.max(0, options.stalenessMs ?? SESSION_STORE_HEALTH_STALENESS_MS);
6370
7064
  let lastUnwritableAt = null;
6371
7065
  let lastWritableAt = null;
7066
+ const causes = /* @__PURE__ */ new Map();
6372
7067
  return {
6373
7068
  observeUnwritable: () => {
6374
7069
  lastUnwritableAt = now();
@@ -6380,6 +7075,23 @@ function createSessionStoreHealth(options = {}) {
6380
7075
  if (lastUnwritableAt === null) return false;
6381
7076
  if (lastWritableAt !== null && lastWritableAt >= lastUnwritableAt) return false;
6382
7077
  return now() - lastUnwritableAt < stalenessMs;
7078
+ },
7079
+ observeCause: (sessionId, cause) => {
7080
+ if (sessionId === null) return;
7081
+ causes.delete(sessionId);
7082
+ causes.set(sessionId, { cause, observedAt: now() });
7083
+ while (causes.size > 256) {
7084
+ const oldest = causes.keys().next().value;
7085
+ if (typeof oldest !== "string") break;
7086
+ causes.delete(oldest);
7087
+ }
7088
+ },
7089
+ readCause: (sessionId) => {
7090
+ const observed = causes.get(sessionId);
7091
+ if (!observed) return null;
7092
+ causes.delete(sessionId);
7093
+ if (now() - observed.observedAt >= stalenessMs) return null;
7094
+ return observed.cause;
6383
7095
  }
6384
7096
  };
6385
7097
  }
@@ -6388,12 +7100,12 @@ function createSessionStoreHealth(options = {}) {
6388
7100
  import { homedir as homedir6 } from "node:os";
6389
7101
 
6390
7102
  // src/lib/runner-file-sync.ts
6391
- import { join as join9 } from "node:path";
7103
+ import { join as join10 } from "node:path";
6392
7104
 
6393
7105
  // src/lib/file-push.ts
6394
7106
  import { randomUUID } from "node:crypto";
6395
7107
  import { chmod, mkdir, open as open3, realpath, rename, unlink } from "node:fs/promises";
6396
- import { basename, dirname as dirname5, isAbsolute, join as join8, relative, resolve as resolve2, sep } from "node:path";
7108
+ 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";
6397
7109
  var FILE_MODE = 384;
6398
7110
  var DIRECTORY_MODE = 448;
6399
7111
  async function writePushedFile(request2) {
@@ -6424,9 +7136,9 @@ async function writePushedFile(request2) {
6424
7136
  }
6425
7137
  try {
6426
7138
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
6427
- dirname5(candidate)
7139
+ dirname6(candidate)
6428
7140
  );
6429
- const realTarget = join8(existingAncestor, ...missingSegments, basename(candidate));
7141
+ const realTarget = join9(existingAncestor, ...missingSegments, basename2(candidate));
6430
7142
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
6431
7143
  if (allowedDirectory === null) {
6432
7144
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -6436,8 +7148,8 @@ async function writePushedFile(request2) {
6436
7148
  }
6437
7149
  if (missingSegments.length > 0) {
6438
7150
  await createMissingDirectories(existingAncestor, missingSegments);
6439
- const realParent = await realpath(dirname5(realTarget));
6440
- if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
7151
+ const realParent = await realpath(dirname6(realTarget));
7152
+ if (realParent !== dirname6(realTarget) || !contains(allowedDirectory, realTarget)) {
6441
7153
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
6442
7154
  path: realTarget,
6443
7155
  bytes,
@@ -6462,15 +7174,15 @@ function expandAndValidate(requestedPath, homeDir) {
6462
7174
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
6463
7175
  return null;
6464
7176
  }
6465
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
7177
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6466
7178
  if (expanded.split(/[/\\]/).includes("..")) {
6467
7179
  return null;
6468
7180
  }
6469
- if (!isAbsolute(expanded)) {
7181
+ if (!isAbsolute2(expanded)) {
6470
7182
  return null;
6471
7183
  }
6472
- const candidate = resolve2(expanded);
6473
- const name = basename(candidate);
7184
+ const candidate = resolve3(expanded);
7185
+ const name = basename2(candidate);
6474
7186
  return name === "" || name === "." || name === ".." ? null : candidate;
6475
7187
  }
6476
7188
  async function resolveNearestExistingAncestor(directory) {
@@ -6480,18 +7192,18 @@ async function resolveNearestExistingAncestor(directory) {
6480
7192
  try {
6481
7193
  return { existingAncestor: await realpath(current), missingSegments };
6482
7194
  } catch (err) {
6483
- const parent = dirname5(current);
7195
+ const parent = dirname6(current);
6484
7196
  if (err.code !== "ENOENT" || parent === current) {
6485
7197
  throw err;
6486
7198
  }
6487
- missingSegments.unshift(basename(current));
7199
+ missingSegments.unshift(basename2(current));
6488
7200
  current = parent;
6489
7201
  }
6490
7202
  }
6491
7203
  }
6492
7204
  async function findContainingAllowedDirectory(allowedDirectories, realTarget) {
6493
7205
  for (const directory of allowedDirectories) {
6494
- if (!isAbsolute(directory)) {
7206
+ if (!isAbsolute2(directory)) {
6495
7207
  log("warn", "file_push_allowed_directory_skipped", { directory, reason: "not_absolute" });
6496
7208
  continue;
6497
7209
  }
@@ -6529,19 +7241,19 @@ async function realpathCreatingIfMissing(directory) {
6529
7241
  }
6530
7242
  }
6531
7243
  function contains(realDirectory, realTarget) {
6532
- const rel = relative(realDirectory, realTarget);
6533
- return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
7244
+ const rel = relative2(realDirectory, realTarget);
7245
+ return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep2}`) && !isAbsolute2(rel);
6534
7246
  }
6535
7247
  async function createMissingDirectories(existingAncestor, missingSegments) {
6536
7248
  let current = existingAncestor;
6537
7249
  for (const segment of missingSegments) {
6538
- current = join8(current, segment);
7250
+ current = join9(current, segment);
6539
7251
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
6540
7252
  await chmod(current, DIRECTORY_MODE);
6541
7253
  }
6542
7254
  }
6543
7255
  async function writeAtomically(realTarget, content) {
6544
- const temporaryPath = join8(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
7256
+ const temporaryPath = join9(dirname6(realTarget), `.evident-push-${randomUUID()}.tmp`);
6545
7257
  let handle;
6546
7258
  try {
6547
7259
  handle = await open3(temporaryPath, "wx", FILE_MODE);
@@ -6671,12 +7383,12 @@ var NOT_APPLIED = {
6671
7383
  opencodeAuthApplied: false
6672
7384
  };
6673
7385
  function isClaudeCredentialPath(requestedPath, homeDir) {
6674
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6675
- return expanded === join9(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
7386
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join10(homeDir, requestedPath.slice(2)) : requestedPath;
7387
+ return expanded === join10(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
6676
7388
  }
6677
7389
  function isOpenCodeAuthPath(requestedPath, homeDir) {
6678
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6679
- return expanded === join9(homeDir, ...OPENCODE_AUTH_SEGMENTS);
7390
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join10(homeDir, requestedPath.slice(2)) : requestedPath;
7391
+ return expanded === join10(homeDir, ...OPENCODE_AUTH_SEGMENTS);
6680
7392
  }
6681
7393
  async function applyOne(options, file) {
6682
7394
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -6820,6 +7532,11 @@ function messageIdOf(m) {
6820
7532
  const infoId = m.info?.id;
6821
7533
  return typeof infoId === "string" ? infoId : void 0;
6822
7534
  }
7535
+ function composeAbandonmentNote(finish, replyShape, outcome) {
7536
+ 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.");
7537
+ if (replyShape === null) return base;
7538
+ 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)` : ""}.`;
7539
+ }
6823
7540
  function cleanImageMime(contentType) {
6824
7541
  if (!contentType) return null;
6825
7542
  const media = contentType.split(";")[0].trim().toLowerCase();
@@ -6840,6 +7557,10 @@ var SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS = 2e3;
6840
7557
  var SESSION_ERROR_STREAM_HEALTHY_MS = 5e3;
6841
7558
  var SESSION_ERROR_BUFFER_TTL_MS = SESSION_ERROR_STREAM_HEALTHY_MS;
6842
7559
  var MAX_BUFFERED_SESSION_ERRORS = 256;
7560
+ var SESSION_ERROR_RETENTION_TTL_MS = 6e4;
7561
+ 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.";
7562
+ var SESSION_ERROR_RETAINED_DETAIL = "Runner-observed OpenCode session error: ";
7563
+ var SESSION_ERROR_OBSERVED_CAUSE = "Runner-observed OpenCode session-store cause: ";
6843
7564
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
6844
7565
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
6845
7566
  var DRIVER_SESSION_ENUMERATION_LIMIT = 20;
@@ -6997,6 +7718,7 @@ var ChannelDriver = class _ChannelDriver {
6997
7718
  * with a short TTL so an unmatched session cannot retain an event indefinitely.
6998
7719
  */
6999
7720
  bufferedSessionErrors = /* @__PURE__ */ new Map();
7721
+ retainedSessionErrors = /* @__PURE__ */ new Map();
7000
7722
  /**
7001
7723
  * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
7002
7724
  * dispatched and are still in-flight. A message in this set is never
@@ -7029,10 +7751,18 @@ var ChannelDriver = class _ChannelDriver {
7029
7751
  * row is `processing` or `pending`; clear it only when absent from both lists.
7030
7752
  */
7031
7753
  untrackableAck = /* @__PURE__ */ new Map();
7754
+ /** Session binding used by the first untrackable-ack report, for retries. */
7755
+ untrackableAckSessionIds = /* @__PURE__ */ new Map();
7032
7756
  /** Messages whose untrackable-ack failure signal was emitted while fenced. */
7033
7757
  untrackableAckSignalled = /* @__PURE__ */ new Set();
7034
7758
  /** Messages whose first untrackable-ack fence warning was already logged. */
7035
7759
  untrackableAckWarned = /* @__PURE__ */ new Set();
7760
+ /**
7761
+ * Accepted V1 prompts whose assigned ids were not read back; these rows are
7762
+ * re-read instead of POSTed again. A null snapshot cannot safely exclude an older
7763
+ * identical-text turn, so it never produces a correlation.
7764
+ */
7765
+ awaitingCorrelation = /* @__PURE__ */ new Map();
7036
7766
  /** Pending rows seen in the current drain, used to retain terminal dispatch fences. */
7037
7767
  pendingMessageIds = /* @__PURE__ */ new Set();
7038
7768
  /**
@@ -7118,21 +7848,6 @@ var ChannelDriver = class _ChannelDriver {
7118
7848
  * re-signal on every one of the 15h of re-dispatch attempts #1110 made.
7119
7849
  */
7120
7850
  dispatchNotStartedSignalled = /* @__PURE__ */ new Map();
7121
- /**
7122
- * Consecutive-UNCONFIRMED-dispatch streak for a `pending` row with NO stored
7123
- * `opencode_message_id` yet — i.e. one that has never even reached the
7124
- * re-drive fence above. `sendPromptAsync`'s POST may 2xx, but its own
7125
- * read-back retries can never confirm the assigned id when the session's
7126
- * message list is PERMANENTLY unreadable (e.g. a corrupted local opencode
7127
- * SQLite DB, #1345/#1348's exact fault, just hit BEFORE the row is ever
7128
- * dispatched instead of after). Unlike an already-dispatched row, THIS row has
7129
- * no other safety net at all: the lifecycle cron only reclaims `status =
7130
- * 'processing'` rows, and a row stuck here never reaches `processing`. Keyed
7131
- * by message id, carrying `sessionId` so a session change (a fresh one bound
7132
- * after abandonment) starts a new streak rather than inheriting the old
7133
- * session's count — same shape as `redrivePollFailures` above.
7134
- */
7135
- unconfirmedDispatchFailures = /* @__PURE__ */ new Map();
7136
7851
  /**
7137
7852
  * Consecutive give-ups for a message whose OpenCode user row remains queued.
7138
7853
  * The session id scopes the count: a fresh session is a new attempt after a
@@ -7150,8 +7865,8 @@ var ChannelDriver = class _ChannelDriver {
7150
7865
  * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
7151
7866
  * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
7152
7867
  * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
7153
- * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
7154
- * so the NEXT tick may retry exactly once more).
7868
+ * re-entry) OR on a failed/unresolved dispatch (the accepted prompt is handed to
7869
+ * the terminal-report fence and is never sent again).
7155
7870
  */
7156
7871
  awaitingReadopt = /* @__PURE__ */ new Set();
7157
7872
  /**
@@ -7469,6 +8184,49 @@ var ChannelDriver = class _ChannelDriver {
7469
8184
  }
7470
8185
  return ids;
7471
8186
  }
8187
+ /** Find an accepted prompt's user row without adopting an older or concurrent turn. */
8188
+ findAwaitingCorrelationId(entry, sessionId, messages) {
8189
+ if (entry.sessionId !== sessionId || entry.knownUserIds === null) return null;
8190
+ const excludeIds = new Set(entry.knownUserIds);
8191
+ for (const inFlight of this.watchers.get(entry.sessionId)?.inFlight.values() ?? []) {
8192
+ excludeIds.add(inFlight.opencodeMessageId);
8193
+ }
8194
+ return findDispatchedUserMessageId(messages, entry.content, excludeIds);
8195
+ }
8196
+ /** Hand an exhausted accepted-but-untrackable prompt to the terminal-report fence. */
8197
+ async handoffUntrackableCorrelation(conversationId, messageId, sessionId, attempts, source) {
8198
+ this.awaitingCorrelation.delete(messageId);
8199
+ this.awaitingReadopt.delete(messageId);
8200
+ this.sessions.delete(conversationId);
8201
+ this.supersede(conversationId, sessionId);
8202
+ const dispatchDescription = source === "readopt" ? "re-dispatched message" : "message";
8203
+ 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.`;
8204
+ this.untrackableAck.set(messageId, errorMessage3);
8205
+ this.untrackableAckSessionIds.set(messageId, null);
8206
+ this.log({
8207
+ level: "error",
8208
+ message: errorMessage3,
8209
+ conversation_id: conversationId,
8210
+ message_id: messageId
8211
+ });
8212
+ const reportError = await this.reportUntrackableAck(
8213
+ conversationId,
8214
+ messageId,
8215
+ null,
8216
+ errorMessage3
8217
+ );
8218
+ if (reportError !== null) {
8219
+ this.log({
8220
+ level: "warn",
8221
+ message: `markFailed PATCH for message ${messageId.slice(0, 8)} (conversation ${conversationId.slice(0, 8)}) failed: ${reportError.message}`,
8222
+ conversation_id: conversationId,
8223
+ message_id: messageId
8224
+ });
8225
+ }
8226
+ if (source === "readopt") {
8227
+ void this.postSignal(conversationId, messageId, "readopt_orphan_unsent");
8228
+ }
8229
+ }
7472
8230
  /**
7473
8231
  * File-pull work, for `run.ts`'s idle accounting (#559).
7474
8232
  *
@@ -7648,6 +8406,56 @@ var ChannelDriver = class _ChannelDriver {
7648
8406
  }
7649
8407
  break;
7650
8408
  }
8409
+ const correlation = this.awaitingCorrelation.get(message.id);
8410
+ if (correlation) {
8411
+ correlation.attempts += 1;
8412
+ let correlatedId = null;
8413
+ if (correlation.knownUserIds !== null && correlation.sessionId === sessionId) {
8414
+ const polled = await this.pollSessionMessagesForRedrive(
8415
+ conv,
8416
+ message,
8417
+ correlation.sessionId
8418
+ );
8419
+ if (polled.ok)
8420
+ correlatedId = this.findAwaitingCorrelationId(correlation, sessionId, polled.messages);
8421
+ } else if (correlation.knownUserIds === null) {
8422
+ this.log({
8423
+ level: "debug",
8424
+ message: `Message ${message.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
8425
+ conversation_id: conv.id,
8426
+ message_id: message.id
8427
+ });
8428
+ }
8429
+ if (correlatedId !== null) {
8430
+ this.awaitingCorrelation.delete(message.id);
8431
+ this.dispatchNotStartedSignalled.delete(message.id);
8432
+ this.releasedOpencodeIds.delete(message.id);
8433
+ this.dispatched.add(message.id);
8434
+ this.registerInFlight(conv, correlation.sessionId, message, correlatedId);
8435
+ dispatched += 1;
8436
+ this.log({
8437
+ level: "info",
8438
+ message: `Matched accepted message ${message.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
8439
+ conversation_id: conv.id,
8440
+ message_id: message.id
8441
+ });
8442
+ void this.postSignal(conv.id, message.id, "ack_correlated", {
8443
+ opencode_message_id: correlatedId
8444
+ });
8445
+ continue;
8446
+ }
8447
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
8448
+ await this.handoffUntrackableCorrelation(
8449
+ conv.id,
8450
+ message.id,
8451
+ correlation.sessionId,
8452
+ correlation.attempts,
8453
+ "fresh"
8454
+ );
8455
+ break;
8456
+ }
8457
+ break;
8458
+ }
7651
8459
  const effectiveOpencodeMessageId = message.opencode_message_id ?? this.releasedOpencodeIds.get(message.id)?.opencodeMessageId ?? null;
7652
8460
  if (effectiveOpencodeMessageId) {
7653
8461
  const outcome = await this.resolveRedrive(
@@ -7669,6 +8477,7 @@ var ChannelDriver = class _ChannelDriver {
7669
8477
  model: message.opencode_model ?? void 0
7670
8478
  };
7671
8479
  let opencodeMessageId;
8480
+ let preDispatchUserIds = null;
7672
8481
  try {
7673
8482
  this.log({
7674
8483
  level: "info",
@@ -7698,7 +8507,20 @@ var ChannelDriver = class _ChannelDriver {
7698
8507
  message.content,
7699
8508
  options,
7700
8509
  sendAttachments,
7701
- this.openCodeClient
8510
+ this.openCodeClient,
8511
+ {
8512
+ onKnownUserIds: (ids) => {
8513
+ preDispatchUserIds = ids;
8514
+ },
8515
+ onKnownUserIdsError: (error2) => {
8516
+ this.log({
8517
+ level: "warn",
8518
+ message: `Pre-dispatch snapshot callback failed for message ${message.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
8519
+ conversation_id: conv.id,
8520
+ message_id: message.id
8521
+ });
8522
+ }
8523
+ }
7702
8524
  )
7703
8525
  );
7704
8526
  } catch (err) {
@@ -7706,6 +8528,7 @@ var ChannelDriver = class _ChannelDriver {
7706
8528
  if (this.isV2 && err instanceof OpenCodeV2PromptAckError) {
7707
8529
  const errorMessage4 = err instanceof Error ? err.message : String(err);
7708
8530
  this.untrackableAck.set(message.id, errorMessage4);
8531
+ this.untrackableAckSessionIds.set(message.id, null);
7709
8532
  this.log({
7710
8533
  level: "error",
7711
8534
  message: `V2 prompt dispatch for message ${message.id.slice(0, 8)} failed after a positive ack with no usable id: ${errorMessage4}`,
@@ -7781,39 +8604,21 @@ var ChannelDriver = class _ChannelDriver {
7781
8604
  if (this.isV2) {
7782
8605
  throw new Error("V2 prompt dispatch completed without an acknowledged message id");
7783
8606
  }
7784
- const streak = this.recordUnconfirmedDispatch(message.id, sessionId);
7785
- if (streak < MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
7786
- this.log({
7787
- level: "warn",
7788
- 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`,
7789
- conversation_id: conv.id,
7790
- message_id: message.id
7791
- });
7792
- this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7793
- continue;
7794
- }
7795
- this.unconfirmedDispatchFailures.delete(message.id);
7796
- this.sessions.delete(conv.id);
7797
- this.supersede(conv.id, sessionId);
7798
- 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.`;
8607
+ this.awaitingCorrelation.set(message.id, {
8608
+ sessionId,
8609
+ content: message.content,
8610
+ attempts: 0,
8611
+ knownUserIds: preDispatchUserIds
8612
+ });
7799
8613
  this.log({
7800
- level: "error",
7801
- message: errorMessage3,
8614
+ level: "warn",
8615
+ 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`,
7802
8616
  conversation_id: conv.id,
7803
8617
  message_id: message.id
7804
8618
  });
7805
- await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
7806
- this.log({
7807
- level: "warn",
7808
- 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)}`,
7809
- conversation_id: conv.id,
7810
- message_id: message.id
7811
- });
7812
- this.signalDispatchNotStarted(conv, message, "abandon_unreported");
7813
- });
8619
+ this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7814
8620
  break;
7815
8621
  }
7816
- this.unconfirmedDispatchFailures.delete(message.id);
7817
8622
  this.dispatchNotStartedSignalled.delete(message.id);
7818
8623
  this.releasedOpencodeIds.delete(message.id);
7819
8624
  this.dispatched.add(message.id);
@@ -8418,23 +9223,6 @@ var ChannelDriver = class _ChannelDriver {
8418
9223
  this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
8419
9224
  return 1;
8420
9225
  }
8421
- /**
8422
- * Record one UNCONFIRMED-dispatch outcome (a `pending` row with no stored
8423
- * `opencode_message_id` whose `sendPromptAsync` returned `null`) toward the
8424
- * bound in `processConversation`'s dispatch loop, and return the resulting
8425
- * count. Mirrors `recordRedrivePollFailure`'s session-scoping: a session
8426
- * change starts a fresh streak at `1` rather than inheriting the old one's
8427
- * count, since a new session is a genuinely different attempt.
8428
- */
8429
- recordUnconfirmedDispatch(messageId, sessionId) {
8430
- const existing = this.unconfirmedDispatchFailures.get(messageId);
8431
- if (existing && existing.sessionId === sessionId) {
8432
- existing.count += 1;
8433
- return existing.count;
8434
- }
8435
- this.unconfirmedDispatchFailures.set(messageId, { sessionId, count: 1 });
8436
- return 1;
8437
- }
8438
9226
  /** Record one queued give-up toward the never-started bound. */
8439
9227
  recordNeverStartedAttempt(messageId, sessionId) {
8440
9228
  const existing = this.neverStartedAttempts.get(messageId);
@@ -8746,6 +9534,7 @@ var ChannelDriver = class _ChannelDriver {
8746
9534
  b2LastDescendantCheckMs: 0,
8747
9535
  b2RootOngoingHeldLogged: false,
8748
9536
  b2AbandonedSignalled: false,
9537
+ b2AbandonedWithoutAnswer: false,
8749
9538
  ambiguousPinnedSinceMs: 0,
8750
9539
  ambiguousResolved: false,
8751
9540
  sessionStoreFailurePending: false,
@@ -8770,6 +9559,28 @@ var ChannelDriver = class _ChannelDriver {
8770
9559
  this.bufferedSessionErrors.delete(oldest);
8771
9560
  }
8772
9561
  }
9562
+ retainSessionError(event) {
9563
+ this.retainedSessionErrors.delete(event.sessionId);
9564
+ this.retainedSessionErrors.set(event.sessionId, {
9565
+ reason: event.reason,
9566
+ observedAt: this.now()
9567
+ });
9568
+ while (this.retainedSessionErrors.size > MAX_BUFFERED_SESSION_ERRORS) {
9569
+ const oldest = this.retainedSessionErrors.keys().next().value;
9570
+ if (typeof oldest !== "string") break;
9571
+ this.retainedSessionErrors.delete(oldest);
9572
+ }
9573
+ }
9574
+ readRetainedSessionError(sessionId) {
9575
+ const retained = this.retainedSessionErrors.get(sessionId);
9576
+ if (!retained) return null;
9577
+ if (this.now() - retained.observedAt >= SESSION_ERROR_RETENTION_TTL_MS) {
9578
+ this.retainedSessionErrors.delete(sessionId);
9579
+ return null;
9580
+ }
9581
+ this.retainedSessionErrors.delete(sessionId);
9582
+ return retained.reason;
9583
+ }
8773
9584
  /**
8774
9585
  * Build a fresh `SessionWatcher`. Seeds `lastTickAt`/`lastObservedTickAt`
8775
9586
  * EQUAL (#1618) so a watcher whose loop has not started ticking yet is never
@@ -8865,6 +9676,7 @@ var ChannelDriver = class _ChannelDriver {
8865
9676
  b2LastDescendantCheckMs: 0,
8866
9677
  b2RootOngoingHeldLogged: false,
8867
9678
  b2AbandonedSignalled: false,
9679
+ b2AbandonedWithoutAnswer: false,
8868
9680
  ambiguousPinnedSinceMs: 0,
8869
9681
  ambiguousResolved: false,
8870
9682
  sessionStoreFailurePending: false,
@@ -9095,9 +9907,10 @@ var ChannelDriver = class _ChannelDriver {
9095
9907
  return;
9096
9908
  }
9097
9909
  if ([...watcher.inFlight.values()].some((message) => message.started && !message.done)) {
9910
+ this.retainSessionError(event);
9098
9911
  this.log({
9099
- level: "debug",
9100
- message: `A turn is already running in session ${event.sessionId.slice(0, 8)} \u2014 deferring to transcript polling`,
9912
+ level: "warn",
9913
+ 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}`,
9101
9914
  conversation_id: watcher.conv.id
9102
9915
  });
9103
9916
  return;
@@ -9127,9 +9940,10 @@ var ChannelDriver = class _ChannelDriver {
9127
9940
  const messages = await this.getSessionMessages(event.sessionId);
9128
9941
  const state = messageRunState(messages, inFlight.opencodeMessageId);
9129
9942
  if (state !== "queued") {
9943
+ this.retainSessionError(event);
9130
9944
  this.log({
9131
- level: "debug",
9132
- message: `Session error for message ${inFlight.evidentMessageId.slice(0, 8)} observed state ${state}; leaving it to transcript polling`,
9945
+ level: "warn",
9946
+ 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}`,
9133
9947
  conversation_id: watcher.conv.id,
9134
9948
  message_id: inFlight.evidentMessageId
9135
9949
  });
@@ -9358,7 +10172,16 @@ var ChannelDriver = class _ChannelDriver {
9358
10172
  if (state === "failed") {
9359
10173
  this.anchorDeliveryDeadline(inFlight);
9360
10174
  if (!inFlight.done) {
9361
- const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
10175
+ const messageErrorText = messageError(messages, inFlight.opencodeMessageId);
10176
+ const retainedReason = this.readRetainedSessionError(sessionId);
10177
+ const retainedError = retainedReason === null || messageErrorText !== null && messageErrorText.includes(retainedReason) ? null : `${SESSION_ERROR_RETAINED_DETAIL}${retainedReason}`;
10178
+ const observedCause = this.sessionStoreHealth?.readCause(sessionId);
10179
+ const causeError = observedCause === null || observedCause === void 0 ? null : `${SESSION_ERROR_OBSERVED_CAUSE}${observedCause}`;
10180
+ const storeError = this.sessionStoreHealth?.isUnwritable() ? SESSION_STORE_UNWRITABLE_ERROR : null;
10181
+ const runnerDetails = [retainedError, causeError, storeError].filter(
10182
+ (detail) => detail !== null
10183
+ );
10184
+ const error2 = messageErrorText === null ? runnerDetails.length === 0 ? void 0 : runnerDetails.join(" ") : [messageErrorText, ...runnerDetails].join(" ");
9362
10185
  this.log({
9363
10186
  level: "error",
9364
10187
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
@@ -9444,10 +10267,33 @@ var ChannelDriver = class _ChannelDriver {
9444
10267
  inFlight.b2LastDescendantCheckMs = 0;
9445
10268
  inFlight.b2RootOngoingHeldLogged = false;
9446
10269
  inFlight.b2AbandonedSignalled = false;
10270
+ inFlight.b2AbandonedWithoutAnswer = false;
9447
10271
  }
9448
10272
  } else {
9449
10273
  if (inFlight.b2AbandonedSignalled) {
9450
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
10274
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
10275
+ const finish = reply?.info?.finish ?? reply?.finish;
10276
+ if (inFlight.b2AbandonedWithoutAnswer) {
10277
+ await this.settleMessageAbandonedWithoutAnswer(
10278
+ sessionId,
10279
+ watcher,
10280
+ inFlight,
10281
+ messages,
10282
+ finish
10283
+ );
10284
+ } else {
10285
+ await this.settleMessageDone(
10286
+ sessionId,
10287
+ watcher,
10288
+ inFlight,
10289
+ messages,
10290
+ composeAbandonmentNote(
10291
+ finish,
10292
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10293
+ "with-answer"
10294
+ )
10295
+ );
10296
+ }
9451
10297
  return;
9452
10298
  }
9453
10299
  if (inFlight.b2PinnedSinceMs === 0) inFlight.b2PinnedSinceMs = this.now();
@@ -9475,21 +10321,51 @@ var ChannelDriver = class _ChannelDriver {
9475
10321
  return;
9476
10322
  }
9477
10323
  inFlight.b2AbandonedSignalled = true;
10324
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
10325
+ const finish = reply?.info?.finish ?? reply?.finish;
10326
+ if (extractReplyTextForMessage(messages, inFlight.opencodeMessageId) === null) {
10327
+ inFlight.b2AbandonedWithoutAnswer = true;
10328
+ void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
10329
+ watched_for_ms: pinnedForMs,
10330
+ run_state: "failed",
10331
+ finish,
10332
+ ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
10333
+ ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
10334
+ opencode_message_id: inFlight.opencodeMessageId
10335
+ });
10336
+ await this.settleMessageAbandonedWithoutAnswer(
10337
+ sessionId,
10338
+ watcher,
10339
+ inFlight,
10340
+ messages,
10341
+ finish
10342
+ );
10343
+ return;
10344
+ }
9478
10345
  this.log({
9479
10346
  level: "warn",
9480
10347
  message: `Message ${id.slice(0, 8)} b2-pinned for ${Math.round(pinnedForMs / 1e3)}s with no ongoing descendant sub-agent session (status-map confirmed) \u2014 treating the delegated/tool turn as abandoned, resolving done`,
9481
10348
  conversation_id: conv.id,
9482
10349
  message_id: id
9483
10350
  });
9484
- const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
9485
10351
  void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
9486
10352
  watched_for_ms: pinnedForMs,
9487
- finish: reply?.info?.finish ?? reply?.finish,
10353
+ finish,
9488
10354
  ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
9489
10355
  ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
9490
10356
  opencode_message_id: inFlight.opencodeMessageId
9491
10357
  });
9492
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
10358
+ await this.settleMessageDone(
10359
+ sessionId,
10360
+ watcher,
10361
+ inFlight,
10362
+ messages,
10363
+ composeAbandonmentNote(
10364
+ finish,
10365
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10366
+ "with-answer"
10367
+ )
10368
+ );
9493
10369
  return;
9494
10370
  }
9495
10371
  if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
@@ -9689,15 +10565,11 @@ var ChannelDriver = class _ChannelDriver {
9689
10565
  }
9690
10566
  }
9691
10567
  /**
9692
- * Settle a message whose run-state has resolved `'done'` — extracted verbatim
9693
- * (pure refactor, no behavior change) from `serviceInFlightMessage`'s former
9694
- * inline `state === 'done'` branch body, so a SECOND caller (the #721
9695
- * b2-abandonment resolution) can reach the exact same completion behavior
9696
- * (delivery-deadline anchoring, title resolution, usage extraction, and
9697
- * `markDone`'s auth/terminal/transient-retry discipline) without duplicating it
9698
- * and risking the two copies silently drifting apart.
10568
+ * Settle a message whose run-state has resolved `'done'`. Normal completion and
10569
+ * confirmed b2 abandonment share the same delivery, usage, and terminal handling.
10570
+ * Normal completions omit the optional terminal diagnostic.
9699
10571
  */
9700
- async settleMessageDone(sessionId, watcher, inFlight, messages) {
10572
+ async settleMessageDone(sessionId, watcher, inFlight, messages, note) {
9701
10573
  const conv = watcher.conv;
9702
10574
  this.anchorDeliveryDeadline(inFlight);
9703
10575
  if (!inFlight.done) {
@@ -9724,7 +10596,8 @@ var ChannelDriver = class _ChannelDriver {
9724
10596
  title,
9725
10597
  usage,
9726
10598
  usageAgentName,
9727
- subagentInvocations
10599
+ subagentInvocations,
10600
+ note
9728
10601
  );
9729
10602
  } catch (err) {
9730
10603
  if (err instanceof ChannelAuthError) throw err;
@@ -9766,6 +10639,78 @@ var ChannelDriver = class _ChannelDriver {
9766
10639
  }
9767
10640
  this.removeInFlight(watcher, inFlight.evidentMessageId);
9768
10641
  }
10642
+ /** Report a confirmed b2 abandonment that produced no answer text. */
10643
+ async settleMessageAbandonedWithoutAnswer(sessionId, watcher, inFlight, messages, finish) {
10644
+ const conv = watcher.conv;
10645
+ const error2 = composeAbandonmentNote(
10646
+ finish,
10647
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10648
+ "without-answer"
10649
+ );
10650
+ this.anchorDeliveryDeadline(inFlight);
10651
+ this.log({
10652
+ level: "warn",
10653
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was abandoned without an answer \u2014 ${error2}`,
10654
+ conversation_id: conv.id,
10655
+ message_id: inFlight.evidentMessageId
10656
+ });
10657
+ const usage = messageUsage(messages, inFlight.opencodeMessageId);
10658
+ const usageAgentName = this.usageAgentName(messages ?? [], inFlight.opencodeMessageId);
10659
+ const subagentInvocations = await this.resolveSubagentInvocations(
10660
+ messages,
10661
+ inFlight.opencodeMessageId,
10662
+ inFlight.evidentMessageId
10663
+ );
10664
+ try {
10665
+ await this.markFailed(
10666
+ conv.id,
10667
+ inFlight.evidentMessageId,
10668
+ sessionId,
10669
+ error2,
10670
+ usage,
10671
+ void 0,
10672
+ usageAgentName,
10673
+ subagentInvocations
10674
+ );
10675
+ } catch (err) {
10676
+ if (err instanceof ChannelAuthError) throw err;
10677
+ if (err instanceof ChannelTerminalError) {
10678
+ this.log({
10679
+ level: "warn",
10680
+ message: `Failed to report abandoned no-answer message ${inFlight.evidentMessageId.slice(0, 8)} as failed (terminal HTTP ${err.status}) \u2014 leaving it for the cron safety net: ${err.message}`,
10681
+ conversation_id: conv.id,
10682
+ message_id: inFlight.evidentMessageId
10683
+ });
10684
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10685
+ return;
10686
+ }
10687
+ if (this.now() >= inFlight.deadline) {
10688
+ this.log({
10689
+ level: "warn",
10690
+ message: `Failed to report abandoned no-answer message ${inFlight.evidentMessageId.slice(0, 8)} as failed within the delivery window \u2014 leaving it for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
10691
+ conversation_id: conv.id,
10692
+ message_id: inFlight.evidentMessageId
10693
+ });
10694
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10695
+ return;
10696
+ }
10697
+ this.log({
10698
+ level: "warn",
10699
+ message: `Failed to report abandoned no-answer message ${inFlight.evidentMessageId.slice(0, 8)} as failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
10700
+ conversation_id: conv.id,
10701
+ message_id: inFlight.evidentMessageId
10702
+ });
10703
+ return;
10704
+ }
10705
+ inFlight.done = true;
10706
+ await this.reportSubagentAuthFailures(
10707
+ watcher.conv.id,
10708
+ inFlight.opencodeMessageId,
10709
+ inFlight.evidentMessageId,
10710
+ messages
10711
+ );
10712
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10713
+ }
9769
10714
  /**
9770
10715
  * Report a queued message as failed after the never-started bound. The accepted
9771
10716
  * OpenCode copy is not cancelled, so the local fence remains on unsuccessful
@@ -9828,7 +10773,7 @@ var ChannelDriver = class _ChannelDriver {
9828
10773
  */
9829
10774
  async settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight) {
9830
10775
  const conv = watcher.conv;
9831
- 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.";
10776
+ const error2 = SESSION_STORE_UNWRITABLE_ERROR;
9832
10777
  this.log({
9833
10778
  level: "error",
9834
10779
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was not resolved as done: ${error2}`,
@@ -9919,11 +10864,22 @@ var ChannelDriver = class _ChannelDriver {
9919
10864
  }
9920
10865
  }
9921
10866
  }
9922
- if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0) {
10867
+ if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0 || this.awaitingCorrelation.size > 0) {
9923
10868
  const stillProcessing = new Set(rows.map((r) => r.id));
10869
+ for (const id of this.awaitingCorrelation.keys()) {
10870
+ if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
10871
+ this.awaitingCorrelation.delete(id);
10872
+ this.log({
10873
+ level: "debug",
10874
+ message: `Re-adopt: message ${id.slice(0, 8)} left processing and pending \u2014 cleared awaiting-correlation fence`,
10875
+ message_id: id
10876
+ });
10877
+ }
10878
+ }
9924
10879
  for (const id of [...this.untrackableAck.keys()]) {
9925
10880
  if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
9926
10881
  this.untrackableAck.delete(id);
10882
+ this.untrackableAckSessionIds.delete(id);
9927
10883
  this.untrackableAckSignalled.delete(id);
9928
10884
  this.untrackableAckWarned.delete(id);
9929
10885
  this.log({
@@ -9963,7 +10919,7 @@ var ChannelDriver = class _ChannelDriver {
9963
10919
  bySession.set(row.opencode_session_id, list);
9964
10920
  }
9965
10921
  for (const [sessionId, sessionRows] of bySession) {
9966
- let messages;
10922
+ let messages = null;
9967
10923
  try {
9968
10924
  if (this.isV2) {
9969
10925
  const snapshot = await this.getSessionMessages(sessionId);
@@ -9972,9 +10928,9 @@ var ChannelDriver = class _ChannelDriver {
9972
10928
  level: "warn",
9973
10929
  message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned an unreadable message snapshot \u2014 skipping this session this tick`
9974
10930
  });
9975
- continue;
10931
+ } else {
10932
+ messages = snapshot;
9976
10933
  }
9977
- messages = snapshot;
9978
10934
  } else {
9979
10935
  const polled = await pollSessionMessagesForRedrive(
9980
10936
  this.port,
@@ -9987,15 +10943,22 @@ var ChannelDriver = class _ChannelDriver {
9987
10943
  level: "warn",
9988
10944
  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`
9989
10945
  });
9990
- continue;
10946
+ } else {
10947
+ messages = polled.messages;
9991
10948
  }
9992
- messages = polled.messages;
9993
10949
  }
9994
10950
  } catch (err) {
9995
10951
  this.log({
9996
10952
  level: "warn",
9997
10953
  message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
9998
10954
  });
10955
+ }
10956
+ if (messages === null) {
10957
+ for (const row of sessionRows) {
10958
+ if (this.awaitingCorrelation.has(row.id)) {
10959
+ await this.advanceReadoptCorrelation(sessionId, row, null);
10960
+ }
10961
+ }
9999
10962
  continue;
10000
10963
  }
10001
10964
  const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
@@ -10005,6 +10968,59 @@ var ChannelDriver = class _ChannelDriver {
10005
10968
  }
10006
10969
  }
10007
10970
  }
10971
+ /** Advance a re-adopted accepted prompt through the shared correlation fence. */
10972
+ async advanceReadoptCorrelation(sessionId, row, messages) {
10973
+ const correlation = this.awaitingCorrelation.get(row.id);
10974
+ if (!correlation) return false;
10975
+ correlation.attempts += 1;
10976
+ const correlatedId = messages === null ? null : this.findAwaitingCorrelationId(correlation, sessionId, messages);
10977
+ if (correlatedId !== null) {
10978
+ this.awaitingCorrelation.delete(row.id);
10979
+ const conv = this.convForRow(sessionId, row);
10980
+ const message = this.queuedMessageForRow(row);
10981
+ const processedAtMs = this.processedAtMs(row);
10982
+ this.registerReadopted(conv, sessionId, message, correlatedId, processedAtMs, processedAtMs);
10983
+ this.dispatched.add(row.id);
10984
+ this.readopted.add(row.id);
10985
+ this.ensureWatcherRunning(sessionId);
10986
+ this.log({
10987
+ level: "info",
10988
+ message: `Re-adopt: matched accepted message ${row.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
10989
+ conversation_id: row.conversation_id,
10990
+ message_id: row.id
10991
+ });
10992
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
10993
+ void this.postSignal(row.conversation_id, row.id, "ack_correlated", {
10994
+ opencode_message_id: correlatedId
10995
+ });
10996
+ return true;
10997
+ }
10998
+ if (correlation.knownUserIds === null) {
10999
+ this.log({
11000
+ level: "debug",
11001
+ message: `Re-adopt: message ${row.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
11002
+ conversation_id: row.conversation_id,
11003
+ message_id: row.id
11004
+ });
11005
+ } else if (correlation.sessionId !== sessionId) {
11006
+ this.log({
11007
+ level: "warn",
11008
+ 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`,
11009
+ conversation_id: row.conversation_id,
11010
+ message_id: row.id
11011
+ });
11012
+ }
11013
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
11014
+ await this.handoffUntrackableCorrelation(
11015
+ row.conversation_id,
11016
+ row.id,
11017
+ correlation.sessionId,
11018
+ correlation.attempts,
11019
+ "readopt"
11020
+ );
11021
+ }
11022
+ return true;
11023
+ }
10008
11024
  /**
10009
11025
  * Re-adopt ONE `processing` row against the tick's session message snapshot
10010
11026
  * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
@@ -10012,8 +11028,10 @@ var ChannelDriver = class _ChannelDriver {
10012
11028
  * Branches on `messageRunState(messages, row.opencode_message_id)` — the
10013
11029
  * opencode-assigned user-message id persisted on the first `processing` PATCH
10014
11030
  * (#218). A row with a NULL stored id (dispatched but the read-back never landed
10015
- * before the restart) has no id to correlate → treated as an orphan and
10016
- * re-dispatched (at most once, see `forceReadoptRun`):
11031
+ * before the restart) is first offered to an awaiting-correlation fence recorded
11032
+ * by this process; without that fence, it has no id to correlate after restart
11033
+ * and is treated as an orphan and re-dispatched (at most once, see
11034
+ * `forceReadoptRun`):
10017
11035
  * - `done` → `markDone` now (guarded like the watcher's done branch);
10018
11036
  * - `failed` → `markFailed` with the surfaced error (issue #182), so an
10019
11037
  * errored turn is reported failed on restart, NOT re-dispatched —
@@ -10022,7 +11040,8 @@ var ChannelDriver = class _ChannelDriver {
10022
11040
  * re-dispatched instead (issue #1310, see the branch below);
10023
11041
  * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
10024
11042
  * tracking the stored id so the reply correlates by it;
10025
- * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
11043
+ * - `unknown`/null id → correlate through an awaiting fence when present;
11044
+ * otherwise re-dispatch (opencode assigns a fresh id) + attach a watcher.
10026
11045
  *
10027
11046
  * Only `ChannelAuthError` propagates.
10028
11047
  */
@@ -10036,6 +11055,7 @@ var ChannelDriver = class _ChannelDriver {
10036
11055
  });
10037
11056
  return;
10038
11057
  }
11058
+ if (await this.advanceReadoptCorrelation(sessionId, row, messages)) return;
10039
11059
  const ocId = row.opencode_message_id;
10040
11060
  const state = messageRunState(messages, ocId ?? "");
10041
11061
  if (state === "done") {
@@ -10102,12 +11122,48 @@ var ChannelDriver = class _ChannelDriver {
10102
11122
  await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
10103
11123
  this.dontRedispatch.delete(row.id);
10104
11124
  this.untrackableAck.delete(row.id);
11125
+ this.untrackableAckSessionIds.delete(row.id);
10105
11126
  this.untrackableAckSignalled.delete(row.id);
10106
11127
  this.untrackableAckWarned.delete(row.id);
10107
11128
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
10108
11129
  return;
10109
11130
  }
10110
- if (this.dontRedispatch.has(row.id) || this.untrackableAck.has(row.id)) {
11131
+ if (this.untrackableAck.has(row.id)) {
11132
+ if (this.untrackableAckSignalled.has(row.id)) {
11133
+ const firstFenceWarning = !this.untrackableAckWarned.has(row.id);
11134
+ this.untrackableAckWarned.add(row.id);
11135
+ this.log({
11136
+ level: firstFenceWarning ? "warn" : "debug",
11137
+ 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`,
11138
+ conversation_id: row.conversation_id,
11139
+ message_id: row.id
11140
+ });
11141
+ const errorMessage3 = this.untrackableAck.get(row.id);
11142
+ const reportError = await this.reportUntrackableAck(
11143
+ row.conversation_id,
11144
+ row.id,
11145
+ this.untrackableAckSessionIds.get(row.id) ?? null,
11146
+ errorMessage3
11147
+ );
11148
+ if (reportError !== null) {
11149
+ this.log({
11150
+ level: "warn",
11151
+ message: `Re-adopt: failed to report untrackable message ${row.id.slice(0, 8)} (will retry next drain): ${reportError.message}`,
11152
+ conversation_id: row.conversation_id,
11153
+ message_id: row.id
11154
+ });
11155
+ }
11156
+ } else {
11157
+ this.log({
11158
+ level: "debug",
11159
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
11160
+ conversation_id: row.conversation_id,
11161
+ message_id: row.id
11162
+ });
11163
+ }
11164
+ return;
11165
+ }
11166
+ if (this.dontRedispatch.has(row.id)) {
10111
11167
  this.log({
10112
11168
  level: "debug",
10113
11169
  message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
@@ -10285,6 +11341,7 @@ var ChannelDriver = class _ChannelDriver {
10285
11341
  }
10286
11342
  this.dontRedispatch.delete(row.id);
10287
11343
  this.untrackableAck.delete(row.id);
11344
+ this.untrackableAckSessionIds.delete(row.id);
10288
11345
  this.untrackableAckSignalled.delete(row.id);
10289
11346
  this.untrackableAckWarned.delete(row.id);
10290
11347
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
@@ -10304,8 +11361,9 @@ var ChannelDriver = class _ChannelDriver {
10304
11361
  * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
10305
11362
  * short-circuit while the row is latched; clear it on a successful dispatch (the
10306
11363
  * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
10307
- * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
10308
- * may retry exactly once more).
11364
+ * re-entry) or when the dispatch itself failed before acceptance. An accepted V1
11365
+ * dispatch whose id is not readable enters the bounded correlation fence instead
11366
+ * of retrying; exhaustion hands it to `untrackableAck`.
10309
11367
  *
10310
11368
  * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
10311
11369
  * `processed_at` (Invariant 1).
@@ -10368,6 +11426,7 @@ var ChannelDriver = class _ChannelDriver {
10368
11426
  );
10369
11427
  }
10370
11428
  let ocId;
11429
+ let preDispatchUserIds = null;
10371
11430
  try {
10372
11431
  ocId = this.isV2 ? await sendV2Prompt(this.openCodeClient, sessionId, row.content) : await this.dispatchLocked(
10373
11432
  sessionId,
@@ -10377,7 +11436,20 @@ var ChannelDriver = class _ChannelDriver {
10377
11436
  row.content,
10378
11437
  options,
10379
11438
  sendAttachments,
10380
- this.openCodeClient
11439
+ this.openCodeClient,
11440
+ {
11441
+ onKnownUserIds: (ids) => {
11442
+ preDispatchUserIds = ids;
11443
+ },
11444
+ onKnownUserIdsError: (error2) => {
11445
+ this.log({
11446
+ level: "warn",
11447
+ message: `Pre-dispatch snapshot callback failed for message ${row.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
11448
+ conversation_id: row.conversation_id,
11449
+ message_id: row.id
11450
+ });
11451
+ }
11452
+ }
10381
11453
  )
10382
11454
  );
10383
11455
  } catch (err) {
@@ -10410,6 +11482,7 @@ var ChannelDriver = class _ChannelDriver {
10410
11482
  }
10411
11483
  } else {
10412
11484
  this.untrackableAck.set(row.id, errorMessage3);
11485
+ this.untrackableAckSessionIds.set(row.id, sessionId);
10413
11486
  }
10414
11487
  this.log({
10415
11488
  level: "error",
@@ -10458,39 +11531,20 @@ var ChannelDriver = class _ChannelDriver {
10458
11531
  }
10459
11532
  if (ocId === null) {
10460
11533
  this.awaitingReadopt.delete(row.id);
10461
- const streak = this.recordUnconfirmedDispatch(row.id, sessionId);
10462
- if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
10463
- this.unconfirmedDispatchFailures.delete(row.id);
10464
- this.sessions.delete(readoptConv.id);
10465
- this.supersede(readoptConv.id, sessionId);
10466
- 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.`;
10467
- this.log({
10468
- level: "error",
10469
- message: errorMessage3,
10470
- conversation_id: row.conversation_id,
10471
- message_id: row.id
10472
- });
10473
- await this.markFailed(row.conversation_id, row.id, null, errorMessage3).catch((markErr) => {
10474
- this.log({
10475
- level: "warn",
10476
- 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)}`,
10477
- conversation_id: row.conversation_id,
10478
- message_id: row.id
10479
- });
10480
- });
10481
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
10482
- return;
10483
- }
11534
+ this.awaitingCorrelation.set(row.id, {
11535
+ sessionId,
11536
+ content: row.content,
11537
+ attempts: 0,
11538
+ knownUserIds: preDispatchUserIds
11539
+ });
10484
11540
  this.log({
10485
11541
  level: "warn",
10486
- 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`,
11542
+ 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`,
10487
11543
  conversation_id: row.conversation_id,
10488
11544
  message_id: row.id
10489
11545
  });
10490
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
10491
11546
  return;
10492
11547
  }
10493
- this.unconfirmedDispatchFailures.delete(row.id);
10494
11548
  const processedAtMs = this.processedAtMs(row);
10495
11549
  this.registerReadopted(
10496
11550
  readoptConv,
@@ -10829,14 +11883,14 @@ var ChannelDriver = class _ChannelDriver {
10829
11883
  const fetchChildWithoutBlocking = async (sessionId) => {
10830
11884
  const pending = fetchChildMessages(sessionId);
10831
11885
  let timer;
10832
- const timeout = new Promise((resolve4) => {
11886
+ const timeout = new Promise((resolve5) => {
10833
11887
  timer = setTimeout(() => {
10834
11888
  this.log({
10835
11889
  level: "warn",
10836
11890
  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`,
10837
11891
  message_id: messageId
10838
11892
  });
10839
- resolve4(null);
11893
+ resolve5(null);
10840
11894
  }, SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS);
10841
11895
  });
10842
11896
  try {
@@ -11396,7 +12450,7 @@ var ChannelDriver = class _ChannelDriver {
11396
12450
  * watcher retries next tick within the
11397
12451
  * deadline, Finding 4).
11398
12452
  */
11399
- async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations) {
12453
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations, error2) {
11400
12454
  const res = await this.fetchImpl(
11401
12455
  `${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
11402
12456
  {
@@ -11414,7 +12468,8 @@ var ChannelDriver = class _ChannelDriver {
11414
12468
  ...title ? { title } : {},
11415
12469
  ...usage ? usage : {},
11416
12470
  ...usageAgentName ? { usage_agent_name: usageAgentName } : {},
11417
- ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {}
12471
+ ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {},
12472
+ ...error2 ? { error: error2 } : {}
11418
12473
  })
11419
12474
  }
11420
12475
  );
@@ -11474,11 +12529,17 @@ var ChannelDriver = class _ChannelDriver {
11474
12529
  if (subagentInvocations && subagentInvocations.length > 0) {
11475
12530
  body.subagent_invocations = subagentInvocations;
11476
12531
  }
11477
- if (failure) {
11478
- body.failure_kind = failure.kind;
11479
- body.failure_provider_id = failure.providerId;
11480
- body.failure_model_id = failure.modelId;
11481
- body.failure_reason = failure.reason;
12532
+ if (failure && failure.errorShape.name !== null) {
12533
+ body.failure_error_name = failure.errorShape.name;
12534
+ }
12535
+ if (failure && failure.errorShape.statusCode !== null) {
12536
+ body.failure_status_code = failure.errorShape.statusCode;
12537
+ }
12538
+ if (failure?.classification) {
12539
+ body.failure_kind = failure.classification.kind;
12540
+ body.failure_provider_id = failure.classification.providerId;
12541
+ body.failure_model_id = failure.classification.modelId;
12542
+ body.failure_reason = failure.classification.reason;
11482
12543
  }
11483
12544
  try {
11484
12545
  await this.callWithRetry(
@@ -11499,27 +12560,28 @@ var ChannelDriver = class _ChannelDriver {
11499
12560
  this.clearSubagentInvocationCaches(messageId);
11500
12561
  }
11501
12562
  /**
11502
- * Classify an errored turn as a model-auth failure (#736 P1-4), for `markFailed`.
12563
+ * Classify an errored turn and extract its bounded error shape for `markFailed`.
11503
12564
  *
11504
12565
  * `messageFailure` alone (structured OpenCode error → `model_auth`) covers
11505
12566
  * most cases; when it returns `null` on this ALREADY-FAILED turn, fall back
11506
12567
  * to the P1-2b zero-provider check — one extra loopback call to
11507
12568
  * `hasAnyConfiguredProvider`, only reached when the structured classifier
11508
12569
  * couldn't place it. Fails open (never throws): a fallback probe failure
11509
- * (`null`/indeterminate) leaves the classification `null`, which produces
11510
- * today's byte-identical PATCH body via `markFailed`'s `if (failure)` guard.
12570
+ * (`null`/indeterminate) leaves the classification `null`; the raw error shape
12571
+ * is still retained for diagnostics.
11511
12572
  */
11512
12573
  async classifyModelAuthFailure(messages, userMessageId) {
11513
- const classified = messageFailure(messages, userMessageId);
11514
- if (classified != null) return classified;
11515
12574
  const reply = findLastAssistantReplyFor(messages, userMessageId);
11516
- const hasProvider = await this.hasAnyConfiguredProvider();
11517
- return applyZeroProviderFallback(
12575
+ const errorShape = extractReplyErrorShape(reply);
12576
+ const classified = messageFailure(messages, userMessageId);
12577
+ if (classified != null) return { classification: classified, errorShape };
12578
+ const classification = applyZeroProviderFallback(
11518
12579
  classified,
11519
- hasProvider,
12580
+ await this.hasAnyConfiguredProvider(),
11520
12581
  reply?.info?.providerID ?? null,
11521
12582
  reply?.info?.modelID ?? null
11522
12583
  );
12584
+ return { classification, errorShape };
11523
12585
  }
11524
12586
  async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
11525
12587
  const providerId = failure.providerId ?? "(unknown)";
@@ -12077,7 +13139,7 @@ function commandError2(result) {
12077
13139
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
12078
13140
  }
12079
13141
  var runCommand2 = (command, args, opts) => {
12080
- return new Promise((resolve4) => {
13142
+ return new Promise((resolve5) => {
12081
13143
  let child;
12082
13144
  let stdout = "";
12083
13145
  let stderr = "";
@@ -12087,7 +13149,7 @@ var runCommand2 = (command, args, opts) => {
12087
13149
  if (settled) return;
12088
13150
  settled = true;
12089
13151
  if (timer.handle) clearTimeout(timer.handle);
12090
- resolve4(result);
13152
+ resolve5(result);
12091
13153
  };
12092
13154
  try {
12093
13155
  child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
@@ -12142,7 +13204,9 @@ async function installRunnerSecret({
12142
13204
  }) {
12143
13205
  const arn = env.RUNNER_SECRET_ARN?.trim();
12144
13206
  if (!arn) {
12145
- log3("runner secret is not configured; continuing without GitHub and MCP credentials");
13207
+ if (!env.GH_TOKEN) {
13208
+ log3("runner secret is not configured; continuing without GitHub and MCP credentials");
13209
+ }
12146
13210
  return false;
12147
13211
  }
12148
13212
  const result = await (commandRunner ?? runCommand2)(
@@ -12341,7 +13405,7 @@ async function configureGitHubAccess({ env, log: log3 }) {
12341
13405
  // src/lib/opencode/config-overlay.ts
12342
13406
  import { execFileSync as execFileSync2 } from "node:child_process";
12343
13407
  import { copyFileSync, existsSync as existsSync2, statSync as statSync6 } from "node:fs";
12344
- import { isAbsolute as isAbsolute2, join as join10, resolve as resolve3 } from "node:path";
13408
+ import { isAbsolute as isAbsolute3, join as join11, resolve as resolve4 } from "node:path";
12345
13409
  function isFile(filePath) {
12346
13410
  return existsSync2(filePath) && statSync6(filePath).isFile();
12347
13411
  }
@@ -12354,8 +13418,8 @@ function applyRunnerOpenCodeConfig({
12354
13418
  log3("runner OpenCode config is not configured; using the baked project config", "debug");
12355
13419
  return;
12356
13420
  }
12357
- const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
12358
- const target = isFile(join10(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
13421
+ const source = isAbsolute3(overlayPath) ? overlayPath : resolve4(cwd, overlayPath);
13422
+ const target = isFile(join11(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
12359
13423
  if (!isFile(source)) {
12360
13424
  log3(
12361
13425
  `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
@@ -12363,7 +13427,7 @@ function applyRunnerOpenCodeConfig({
12363
13427
  );
12364
13428
  return;
12365
13429
  }
12366
- copyFileSync(source, join10(cwd, target));
13430
+ copyFileSync(source, join11(cwd, target));
12367
13431
  try {
12368
13432
  execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
12369
13433
  stdio: "ignore"
@@ -12372,7 +13436,7 @@ function applyRunnerOpenCodeConfig({
12372
13436
  const detail = error2 instanceof Error ? error2.message : String(error2);
12373
13437
  log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
12374
13438
  }
12375
- log3(`Applied runner OpenCode config ${source} to ${join10(cwd, target)}`);
13439
+ log3(`Applied runner OpenCode config ${source} to ${join11(cwd, target)}`);
12376
13440
  }
12377
13441
 
12378
13442
  // src/lib/credential-sync.ts
@@ -12390,14 +13454,14 @@ function errorMessage2(error2) {
12390
13454
  return error2 instanceof Error ? error2.message : String(error2);
12391
13455
  }
12392
13456
  function waitForSettlement(promise, timeoutMs) {
12393
- return new Promise((resolve4) => {
13457
+ return new Promise((resolve5) => {
12394
13458
  let settled = false;
12395
13459
  const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
12396
13460
  const finish = (value) => {
12397
13461
  if (settled) return;
12398
13462
  settled = true;
12399
13463
  clearTimeout(timer);
12400
- resolve4(value);
13464
+ resolve5(value);
12401
13465
  };
12402
13466
  promise.then(
12403
13467
  () => finish(true),
@@ -12648,8 +13712,8 @@ function resolveFileSyncDirectories(raw, homeDir) {
12648
13712
  if (trimmed === "") {
12649
13713
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
12650
13714
  }
12651
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join11(homeDir, trimmed.slice(2)) : trimmed;
12652
- if (!isAbsolute3(expanded)) {
13715
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join12(homeDir, trimmed.slice(2)) : trimmed;
13716
+ if (!isAbsolute4(expanded)) {
12653
13717
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
12654
13718
  }
12655
13719
  const normalized = resolvePath2(expanded);
@@ -12749,8 +13813,7 @@ function resolveMaxActiveSessions(options, env = process.env) {
12749
13813
  function meetsThreshold(state, level) {
12750
13814
  return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
12751
13815
  }
12752
- function log2(state, message, level = "info") {
12753
- if (!meetsThreshold(state, level)) return;
13816
+ function writeConsoleLog(state, message, level) {
12754
13817
  if (state.json) {
12755
13818
  console.log(
12756
13819
  JSON.stringify({
@@ -12759,10 +13822,18 @@ function log2(state, message, level = "info") {
12759
13822
  message
12760
13823
  })
12761
13824
  );
12762
- } else if (!state.interactive) {
12763
- const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
12764
- console.log(`${prefix} ${message}`);
13825
+ return;
12765
13826
  }
13827
+ const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
13828
+ console.log(`${prefix} ${message}`);
13829
+ }
13830
+ function log2(state, message, level = "info") {
13831
+ if (!meetsThreshold(state, level)) return;
13832
+ if (state.json || !state.interactive) writeConsoleLog(state, message, level);
13833
+ }
13834
+ function logLocalOnly(state, message, level = "info") {
13835
+ if (!meetsThreshold(state, level)) return;
13836
+ writeConsoleLog(state, message, level);
12766
13837
  }
12767
13838
  function logActivity(state, entry) {
12768
13839
  const level = entry.level ?? (entry.type === "error" ? "error" : "info");
@@ -12963,6 +14034,8 @@ async function driveChannels(state, driver) {
12963
14034
  let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
12964
14035
  let lastSeenClaudeApplies = driver.fileSyncActivity().claudeCredentialApplies;
12965
14036
  let lastSeenOpencodeAuthApplies = driver.fileSyncActivity().opencodeAuthApplyCount;
14037
+ const stayAwakeHolds = () => state.stayAwakeUntilMs !== null && Date.now() < state.stayAwakeUntilMs;
14038
+ let stayAwakeSuppressionLogged = false;
12966
14039
  while (state.running) {
12967
14040
  const cycleStartedAtMs = performance.now();
12968
14041
  let idleThisCycle = false;
@@ -13064,7 +14137,7 @@ async function driveChannels(state, driver) {
13064
14137
  }
13065
14138
  }
13066
14139
  }
13067
- await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
14140
+ await new Promise((resolve5) => setTimeout(resolve5, CHANNEL_POLL_INTERVAL_MS));
13068
14141
  const cycleMs = performance.now() - cycleStartedAtMs;
13069
14142
  if (idleThisCycle) idleMs += cycleMs;
13070
14143
  if (unreachableThisCycle) unreachableMs += cycleMs;
@@ -13077,15 +14150,27 @@ async function driveChannels(state, driver) {
13077
14150
  if (state.interactive) displayStatus(state);
13078
14151
  break;
13079
14152
  }
13080
- if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3) {
14153
+ if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && !stayAwakeHolds()) {
13081
14154
  logActivity(state, { type: "info", message: "Idle timeout reached" });
13082
14155
  if (state.interactive) displayStatus(state);
13083
14156
  break;
13084
14157
  }
14158
+ if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && stayAwakeHolds()) {
14159
+ if (!stayAwakeSuppressionLogged) {
14160
+ stayAwakeSuppressionLogged = true;
14161
+ logActivity(state, {
14162
+ type: "info",
14163
+ message: "Idle, but staying awake until the requested minimum passes"
14164
+ });
14165
+ }
14166
+ }
13085
14167
  }
13086
14168
  }
13087
14169
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
13088
14170
  var SESSION_CLEANUP_MAX_DELETES_PER_SWEEP = 100;
14171
+ var SESSION_CLEANUP_MAX_ISOLATED_DELETE_FAILURES = 2;
14172
+ var SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES = 2;
14173
+ var MAX_SESSION_DELETE_BODY_LENGTH = 200;
13089
14174
  function shouldWarnForReclaimSkip(reason) {
13090
14175
  if (reason !== "sqlite-unavailable" && reason !== "disk-exhaustion") return false;
13091
14176
  const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
@@ -13096,7 +14181,7 @@ function shouldWarnForReclaimSkip(reason) {
13096
14181
  return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
13097
14182
  }
13098
14183
  function sessionDbPath() {
13099
- return join11(homedir7(), ".local", "share", "opencode", "opencode.db");
14184
+ return join12(homedir7(), ".local", "share", "opencode", "opencode.db");
13100
14185
  }
13101
14186
  function logSessionCleanupActivity(state, phase, entry) {
13102
14187
  logActivity(state, {
@@ -13173,8 +14258,12 @@ async function runSweep(state, driver, config) {
13173
14258
  protectedIds: protectedNow
13174
14259
  }
13175
14260
  );
13176
- const deleteCandidates = toDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
13177
- remaining = toDelete.length - deleteCandidates.length;
14261
+ const eligibleToDelete = toDelete.filter(
14262
+ (id) => (state.sessionDeleteFailures.get(id)?.isolatedFailures ?? 0) < SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES
14263
+ );
14264
+ const deleteCandidates = eligibleToDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
14265
+ remaining = eligibleToDelete.length - deleteCandidates.length;
14266
+ const answeredFailures = [];
13178
14267
  protectedNow = driver.protectedSessionIds();
13179
14268
  for (const id of deleteCandidates) {
13180
14269
  if (protectedNow.has(id)) {
@@ -13197,8 +14286,44 @@ async function runSweep(state, driver, config) {
13197
14286
  if (statusClass === 4) failed4xx++;
13198
14287
  else if (statusClass === 5) failed5xx++;
13199
14288
  else failedOtherStatus++;
14289
+ answeredFailures.push({
14290
+ id,
14291
+ status: outcome.status,
14292
+ ...outcome.body !== void 0 ? { body: outcome.body } : {}
14293
+ });
14294
+ }
14295
+ }
14296
+ const givenUpFailures = [];
14297
+ if (failed > SESSION_CLEANUP_MAX_ISOLATED_DELETE_FAILURES) {
14298
+ for (const { id } of answeredFailures) state.sessionDeleteFailures.delete(id);
14299
+ } else {
14300
+ for (const failure of answeredFailures) {
14301
+ const previous = state.sessionDeleteFailures.get(failure.id);
14302
+ const record = previous?.status === failure.status ? { status: failure.status, isolatedFailures: previous.isolatedFailures + 1 } : { status: failure.status, isolatedFailures: 1 };
14303
+ state.sessionDeleteFailures.set(failure.id, record);
14304
+ if (record.isolatedFailures >= SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES) {
14305
+ givenUpFailures.push(failure);
14306
+ }
13200
14307
  }
13201
14308
  }
14309
+ for (const failure of givenUpFailures) {
14310
+ const excerpt = redactDiagnosticText(failure.body ?? "(no response body)").slice(
14311
+ 0,
14312
+ MAX_SESSION_DELETE_BODY_LENGTH
14313
+ );
14314
+ logSessionCleanupActivity(state, "sweep", {
14315
+ type: "info",
14316
+ level: "warn",
14317
+ message: `Session cleanup: gave up on session ${failure.id} after ${SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES} isolated delete failures (status ${failure.status}): ${excerpt}`,
14318
+ metadata: {
14319
+ outcome: "session-delete-given-up",
14320
+ session_id: failure.id,
14321
+ status: failure.status,
14322
+ body_excerpt: excerpt,
14323
+ given_up_count: givenUpFailures.length
14324
+ }
14325
+ });
14326
+ }
13202
14327
  const failedNote = failed > 0 ? `, failed ${failed}` : "";
13203
14328
  const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
13204
14329
  const remainingNote = remaining > 0 ? `, remaining ${remaining}` : "";
@@ -13219,21 +14344,31 @@ async function runSweep(state, driver, config) {
13219
14344
  sessions_remaining: remaining,
13220
14345
  ...enumerationReason !== null ? { session_enumeration_reason: enumerationReason } : {}
13221
14346
  };
14347
+ const protectedForReclaim = driver.protectedSessionIds();
13222
14348
  const reclaimResult = await reclaimSessionDbSpace({
13223
14349
  dbPath: sessionDbPath(),
13224
14350
  chunkPages: SESSION_DB_RECLAIM_CHUNK_PAGES,
13225
14351
  pageCeiling: SESSION_DB_RECLAIM_PAGE_CEILING,
13226
14352
  timeBudgetMs: SESSION_DB_RECLAIM_TIME_BUDGET_MS,
13227
- allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
14353
+ hasLiveTurn: protectedForReclaim.size > 0,
14354
+ hasProvenanceAnomaly: state.sessionDbProvenanceAnomaly
13228
14355
  });
13229
14356
  if (reclaimResult.ok) {
13230
14357
  state.sessionStoreHealth.observeWritable();
13231
14358
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
13232
14359
  const afterMib = (reclaimResult.afterBytes / 1024 / 1024).toFixed(1);
13233
14360
  const checkpointNote = reclaimResult.checkpoint.busy ? ` (on-disk file truncation deferred: checkpoint busy, ${reclaimResult.checkpoint.log} WAL frames pending)` : "";
14361
+ const loopElapsedMs = Math.max(
14362
+ 0,
14363
+ reclaimResult.elapsedMs - reclaimResult.checkpointElapsedMs
14364
+ );
14365
+ const reclaimIsSlow = reclaimResult.elapsedMs >= 4 * SESSION_DB_RECLAIM_TIME_BUDGET_MS;
14366
+ const perPageNote = reclaimResult.pagesReclaimed > 0 ? `, per-page ${(loopElapsedMs / reclaimResult.pagesReclaimed).toFixed(2)} ms/page` : "";
14367
+ 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})` : "";
13234
14368
  logSessionCleanupActivity(state, "sweep", {
13235
14369
  type: "info",
13236
- message: `Session cleanup: reclaimed session-db space (${reclaimResult.mode}): ${beforeMib} MiB -> ${afterMib} MiB (stopped by ${reclaimResult.stoppedBy}; freelist ${reclaimResult.freelistBefore} -> ${reclaimResult.freelistAfter} pages)` + checkpointNote,
14370
+ level: reclaimIsSlow ? "warn" : "info",
14371
+ 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,
13237
14372
  metadata: {
13238
14373
  outcome: "reclaimed",
13239
14374
  mode: reclaimResult.mode,
@@ -13246,6 +14381,7 @@ async function runSweep(state, driver, config) {
13246
14381
  freelist_after: reclaimResult.freelistAfter,
13247
14382
  pages_reclaimed: reclaimResult.pagesReclaimed,
13248
14383
  reclaim_elapsed_ms: reclaimResult.elapsedMs,
14384
+ checkpoint_elapsed_ms: reclaimResult.checkpointElapsedMs,
13249
14385
  ...sessionMetadata
13250
14386
  }
13251
14387
  });
@@ -13356,10 +14492,12 @@ function scheduleUsageReporting(state, params) {
13356
14492
  let phase = "dormant";
13357
14493
  let rearmRequested = false;
13358
14494
  const armProbe = () => {
14495
+ if (!state.running) return;
13359
14496
  phase = "probe-pending";
13360
14497
  params.setTimer(setTimeout(() => void tick(true), params.firstDelayMs()));
13361
14498
  };
13362
14499
  const scheduleNextTick = () => {
14500
+ if (!state.running) return;
13363
14501
  if (rearmRequested) {
13364
14502
  rearmRequested = false;
13365
14503
  armProbe();
@@ -13393,6 +14531,7 @@ function scheduleUsageReporting(state, params) {
13393
14531
  phase = "tick-in-flight";
13394
14532
  try {
13395
14533
  const usage = await params.fetchUsage();
14534
+ if (!state.running) return;
13396
14535
  const result = await params.report(usage);
13397
14536
  if (result.ok) {
13398
14537
  if (consecutiveFailures > 0) {
@@ -13531,6 +14670,7 @@ function scheduleResourceUsageReporting(state, options) {
13531
14670
  message: lowDiskDecision.warning
13532
14671
  });
13533
14672
  }
14673
+ if (!state.running) return;
13534
14674
  const result = await reportResourceUsage(state.agentId, state.authHeader, usage);
13535
14675
  if (result.ok) {
13536
14676
  if (consecutiveFailures > 0) {
@@ -13563,7 +14703,9 @@ function scheduleResourceUsageReporting(state, options) {
13563
14703
  message: `Resource usage reporting failed: ${message}${failureStreakSuffix(consecutiveFailures)}`
13564
14704
  });
13565
14705
  } finally {
13566
- state.resourceUsageTimer = setTimeout(() => void tick(), usageReportDelayMs());
14706
+ if (state.running) {
14707
+ state.resourceUsageTimer = setTimeout(() => void tick(), usageReportDelayMs());
14708
+ }
13567
14709
  }
13568
14710
  };
13569
14711
  state.resourceUsageTimer = setTimeout(() => void tick(), firstReportDelayMs());
@@ -13725,8 +14867,8 @@ async function beginGracefulShutdown(state, trigger) {
13725
14867
  return true;
13726
14868
  }
13727
14869
  );
13728
- const timedOut = new Promise((resolve4) => {
13729
- timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
14870
+ const timedOut = new Promise((resolve5) => {
14871
+ timer = setTimeout(() => resolve5(false), telemetryBudgetMs);
13730
14872
  });
13731
14873
  if (!await Promise.race([flushed, timedOut])) {
13732
14874
  log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
@@ -13766,6 +14908,8 @@ async function run(options) {
13766
14908
  port: options.port ?? 4096,
13767
14909
  conversationFilter: options.conversation ?? null,
13768
14910
  idleTimeout: options.idleTimeout ?? null,
14911
+ stayAwakeSeconds: null,
14912
+ stayAwakeUntilMs: null,
13769
14913
  json: options.json ?? false,
13770
14914
  interactive,
13771
14915
  logLevel,
@@ -13789,6 +14933,7 @@ async function run(options) {
13789
14933
  messageCount: 0,
13790
14934
  lastProxiedActivityAt: null,
13791
14935
  sessionCleanupTimers: [],
14936
+ sessionDeleteFailures: /* @__PURE__ */ new Map(),
13792
14937
  claudeUsageTimer: null,
13793
14938
  claudeUsageRearm: null,
13794
14939
  openaiUsageTimer: null,
@@ -13954,6 +15099,17 @@ async function run(options) {
13954
15099
  }
13955
15100
  spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
13956
15101
  state.agentName = validation.agent.name;
15102
+ const requestedStayAwakeSeconds = validation.agent?.wake_stay_awake_seconds;
15103
+ if (typeof requestedStayAwakeSeconds === "number" && Number.isFinite(requestedStayAwakeSeconds) && requestedStayAwakeSeconds > 0) {
15104
+ state.stayAwakeSeconds = requestedStayAwakeSeconds;
15105
+ } else if (requestedStayAwakeSeconds !== void 0 && requestedStayAwakeSeconds !== null) {
15106
+ state.stayAwakeSeconds = null;
15107
+ log2(
15108
+ state,
15109
+ `Ignoring invalid wake_stay_awake_seconds (type: ${typeof requestedStayAwakeSeconds})`,
15110
+ "warn"
15111
+ );
15112
+ }
13957
15113
  const microvmId = process.env.MICROVM_ID?.trim();
13958
15114
  if (microvmId) {
13959
15115
  const reported = await reportMicrovmId(state.agentId, state.authHeader, microvmId);
@@ -13969,29 +15125,73 @@ async function run(options) {
13969
15125
  } else {
13970
15126
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
13971
15127
  }
13972
- if (options.restoreRunnerCredentials) {
13973
- log2(state, "Restoring runner credentials before starting OpenCode");
13974
- const credentialContext = {
13975
- env: process.env,
13976
- log: (message, level = "info") => {
13977
- if (level === "error") {
13978
- logActivity(state, { type: "error", error: message });
13979
- } else {
13980
- logActivity(state, { type: "info", level, message });
13981
- }
15128
+ if (options.restoreRunnerCredentials || options.credentialSyncMarker) {
15129
+ const logCredentialVaultResolution = (message, level) => {
15130
+ log2(state, message, level);
15131
+ if (state.interactive && !state.json) {
15132
+ logActivity(state, { type: "info", level, message });
13982
15133
  }
13983
15134
  };
13984
- const githubTokenPopulated = await installRunnerSecret(credentialContext);
15135
+ const vaultResolution = await getRunnerCredentialVault(state.agentId, state.authHeader);
15136
+ if (vaultResolution.error) {
15137
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
15138
+ logCredentialVaultResolution(
15139
+ `CREDENTIAL-VAULT-LOOKUP-FAILED: ${vaultResolution.error}; credentials will be read from and written to this runner's own prefix this boot`,
15140
+ "warn"
15141
+ );
15142
+ } else if (vaultResolution.prefix !== null && vaultResolution.vaultId !== null && vaultResolution.source !== null) {
15143
+ process.env.CREDENTIAL_VAULT_PREFIX = vaultResolution.prefix;
15144
+ logCredentialVaultResolution(
15145
+ `CREDENTIAL-VAULT: source=${vaultResolution.source} vault=${vaultResolution.vaultId} prefix=${vaultResolution.prefix}`,
15146
+ "info"
15147
+ );
15148
+ } else {
15149
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
15150
+ logCredentialVaultResolution(
15151
+ "CREDENTIAL-VAULT-NONE: this runner is not linked to a credential vault; using its own prefix",
15152
+ "debug"
15153
+ );
15154
+ }
15155
+ }
15156
+ const credentialContext = {
15157
+ env: process.env,
15158
+ log: (message, level = "info") => {
15159
+ if (level === "error") {
15160
+ logActivity(state, { type: "error", error: message });
15161
+ } else {
15162
+ logActivity(state, { type: "info", level, message });
15163
+ }
15164
+ }
15165
+ };
15166
+ const githubTokenPopulated = await installRunnerSecret(credentialContext);
15167
+ if (options.restoreRunnerCredentials) {
13985
15168
  await restoreCredentialStores(credentialContext);
13986
- if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
13987
15169
  }
15170
+ if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
13988
15171
  state.credentialSync?.arm();
15172
+ const dependencyInstallDiagnosticReporter = createDependencyInstallDiagnosticReporter({
15173
+ context: {
15174
+ homeDir: homedir7(),
15175
+ cwd: process.cwd(),
15176
+ microvmId: process.env.MICROVM_ID?.trim(),
15177
+ euid: process.getuid?.(),
15178
+ egid: process.getgid?.()
15179
+ },
15180
+ emitEvent: (message) => logActivity(state, { type: "info", level: "warn", message }),
15181
+ emitLocal: (message) => logLocalOnly(state, message, "warn")
15182
+ });
13989
15183
  const openCodeActivityForwarder = createOpenCodeActivityForwarder(
13990
15184
  () => ({
13991
15185
  agentId: state.agentId,
13992
15186
  authHeader: state.authHeader
13993
15187
  }),
13994
- state.sessionStoreHealth.observeUnwritable
15188
+ ({ isDiskFailure, sessionId, cause }) => {
15189
+ if (isDiskFailure) state.sessionStoreHealth.observeUnwritable();
15190
+ if (cause !== null && sessionId !== null) {
15191
+ state.sessionStoreHealth.observeCause(sessionId, cause);
15192
+ }
15193
+ },
15194
+ dependencyInstallDiagnosticReporter
13995
15195
  );
13996
15196
  const openCodeLogTail = tailOpenCodeLogFile(
13997
15197
  resolveOpenCodeLogPath(homedir7(), process.env),
@@ -14002,22 +15202,27 @@ async function run(options) {
14002
15202
  openCodeLogTail.stop();
14003
15203
  };
14004
15204
  let sessionDbVerifyFatal = false;
15205
+ let sessionDbVerifyFatalReason;
14005
15206
  if (!options.restoreSessionDb) {
14006
- log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
15207
+ logActivity(state, {
15208
+ type: "info",
15209
+ level: "warn",
15210
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=flag-absent \u2014 Skipping session-DB restore: --restore-session-db was not passed"
15211
+ });
14007
15212
  } else {
14008
15213
  const health = await checkOpenCodeHealth(state.port);
14009
15214
  if (health.healthy) {
14010
- log2(
14011
- state,
14012
- "Skipping session-DB restore: OpenCode is already serving this database",
14013
- "debug"
14014
- );
15215
+ logActivity(state, {
15216
+ type: "info",
15217
+ level: "warn",
15218
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=opencode-serving \u2014 Skipping session-DB restore: OpenCode is already serving this database"
15219
+ });
14015
15220
  } else if (health.authFailed) {
14016
- log2(
14017
- state,
14018
- "Skipping session-DB restore: OpenCode answered with an authentication challenge; assuming this database is in use",
14019
- "debug"
14020
- );
15221
+ logActivity(state, {
15222
+ type: "info",
15223
+ level: "warn",
15224
+ 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"
15225
+ });
14021
15226
  } else {
14022
15227
  const result = await restoreAndVerifySessionDb({
14023
15228
  dbPath: sessionDbPath(),
@@ -14034,12 +15239,13 @@ async function run(options) {
14034
15239
  reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
14035
15240
  });
14036
15241
  sessionDbVerifyFatal = result.verifyFatal;
15242
+ sessionDbVerifyFatalReason = result.verifyFatalReason;
14037
15243
  }
14038
15244
  }
14039
15245
  reportSessionDbRecovery(state);
14040
15246
  if (sessionDbVerifyFatal) {
14041
15247
  throw new Error(
14042
- "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
15248
+ 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`
14043
15249
  );
14044
15250
  }
14045
15251
  applyRunnerOpenCodeConfig({
@@ -14318,6 +15524,13 @@ async function run(options) {
14318
15524
  type: "info",
14319
15525
  message: connectionMessage
14320
15526
  });
15527
+ if (!isReconnect && state.stayAwakeSeconds !== null && state.stayAwakeUntilMs === null) {
15528
+ state.stayAwakeUntilMs = Date.now() + state.stayAwakeSeconds * 1e3;
15529
+ logActivity(state, {
15530
+ type: "info",
15531
+ message: `Staying awake for at least ${state.stayAwakeSeconds}s as requested by the wake`
15532
+ });
15533
+ }
14321
15534
  if (options.tunnelReadyFile) {
14322
15535
  const marker = writeTunnelReadyMarker(options.tunnelReadyFile, agentId);
14323
15536
  if (marker.ok) {
@@ -14366,14 +15579,14 @@ async function run(options) {
14366
15579
  },
14367
15580
  onDisconnected: (code, reason, transition) => {
14368
15581
  state.connected = false;
14369
- const safeReason = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
15582
+ const safeReason2 = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
14370
15583
  logActivity(state, {
14371
15584
  type: "info",
14372
- message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason})`
15585
+ message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason2})`
14373
15586
  });
14374
15587
  emitAgentDisconnected(state.agentId, {
14375
15588
  code,
14376
- reason: safeReason,
15589
+ reason: safeReason2,
14377
15590
  connection_generation: transition.generation
14378
15591
  });
14379
15592
  if (state.interactive) displayStatus(state);
@@ -14452,6 +15665,7 @@ async function run(options) {
14452
15665
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
14453
15666
  throw error2;
14454
15667
  }
15668
+ if (!state.running) return;
14455
15669
  scheduleSessionCleanup(state, channelDriver, options);
14456
15670
  state.claudeUsageRearm = scheduleClaudeUsageReporting(state, options);
14457
15671
  state.openaiUsageRearm = scheduleUsageReporting(state, {
@@ -14536,6 +15750,7 @@ program.command("login").description("Authenticate with Evident").option("--toke
14536
15750
  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 }));
14537
15751
  program.command("whoami").description("Show the currently logged in user").action(whoami);
14538
15752
  program.command("status").description("Check whether the configured credentials can reach Evident").option("--json", "Output in JSON format").action((options) => status({ json: options.json }));
15753
+ program.command("credential-vault-env").description("Print the shell assignment a runner image evals before starting the synchroniser").action(credentialVaultEnv);
14539
15754
  program.command("claude-usage").description("[spike] Show Claude subscription usage (requires a local `claude login`)").action(claudeUsage);
14540
15755
  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(
14541
15756
  "-a, --agent [id]",