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

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)
@@ -1128,7 +1293,11 @@ var FORWARD_FAILURE_REASON_HEADER = "X-Evident-Failure-Reason";
1128
1293
  function log(level, event, fields) {
1129
1294
  const method = level === "debug" ? "log" : level;
1130
1295
  try {
1131
- console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
1296
+ const payload = Object.fromEntries(
1297
+ Object.entries({ level, event, ...fields }).filter(([, value]) => value !== void 0)
1298
+ );
1299
+ const secondArgument = globalThis.navigator?.userAgent === "Cloudflare-Workers" ? payload : JSON.stringify(payload);
1300
+ console[method]("[evident]", secondArgument);
1132
1301
  } catch (err) {
1133
1302
  console.error(
1134
1303
  "[evident] log_serialize_failed",
@@ -1157,7 +1326,7 @@ import ora4 from "ora";
1157
1326
  import { select as select4 } from "@inquirer/prompts";
1158
1327
 
1159
1328
  // src/lib/telemetry.ts
1160
- var CLI_VERSION = (true ? "3.5.2-dev.df1ae49" : void 0) ?? process.env.npm_package_version ?? "unknown";
1329
+ var CLI_VERSION = (true ? "3.5.2-dev.dfbb46a" : void 0) ?? process.env.npm_package_version ?? "unknown";
1161
1330
  function getCliVersion() {
1162
1331
  return CLI_VERSION;
1163
1332
  }
@@ -1318,20 +1487,47 @@ var EventTypes = {
1318
1487
  DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
1319
1488
  };
1320
1489
 
1321
- // src/lib/runner-activity-telemetry.ts
1322
- import { homedir as homedir2 } from "node:os";
1323
-
1324
1490
  // src/lib/opencode/session-db-reclaim.ts
1325
1491
  import { statSync as statSync2, statfsSync } from "node:fs";
1326
1492
  import { dirname as dirname2 } from "node:path";
1327
- var SESSION_DB_RECLAIM_CHUNK_PAGES = 2e3;
1493
+
1494
+ // src/lib/diagnostic-redaction.ts
1495
+ import { homedir as homedir2 } from "node:os";
1496
+ function redactHomeDirectory(message) {
1497
+ const home = homedir2();
1498
+ if (!home || home === "/") {
1499
+ return message;
1500
+ }
1501
+ const pathSegmentCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-";
1502
+ let result = "";
1503
+ let searchFrom = 0;
1504
+ let homeIndex;
1505
+ while ((homeIndex = message.indexOf(home, searchFrom)) !== -1) {
1506
+ const homeEnd = homeIndex + home.length;
1507
+ const previousCharacter = message[homeIndex - 1];
1508
+ const nextCharacter = message[homeEnd];
1509
+ const atPathBoundary = (!previousCharacter || !pathSegmentCharacters.includes(previousCharacter)) && (!nextCharacter || !pathSegmentCharacters.includes(nextCharacter));
1510
+ result += message.slice(searchFrom, homeIndex);
1511
+ result += atPathBoundary ? "~" : home;
1512
+ searchFrom = homeEnd;
1513
+ }
1514
+ return result + message.slice(searchFrom);
1515
+ }
1516
+ function redactDiagnosticText(message) {
1517
+ const redacted = message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/(?<![A-Za-z0-9_-])sk-ant-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-ant-***").replace(/(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-proj-***").replace(/(?<![A-Za-z0-9_-])sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "sk-***").replace(/(?<![A-Za-z0-9_-])(gh[oprsu])_[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "$1_***").replace(/(?<![A-Za-z0-9_-])github_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_-])/g, "github_pat_***").replace(/https?:\/\/\S+/g, "<url>");
1518
+ return redactHomeDirectory(redacted);
1519
+ }
1520
+
1521
+ // src/lib/opencode/session-db-reclaim.ts
1522
+ var SESSION_DB_RECLAIM_CHUNK_PAGES = 100;
1328
1523
  var SESSION_DB_RECLAIM_TIME_BUDGET_MS = 500;
1329
1524
  var SESSION_DB_RECLAIM_PAGE_CEILING = 4e4;
1525
+ var SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS = 5e3;
1330
1526
  function errorMessage(error2) {
1331
1527
  if (!(error2 instanceof Error)) return String(error2);
1332
1528
  return error2.cause instanceof Error ? error2.cause.message : error2.message;
1333
1529
  }
1334
- function isSessionStoreWriteFailure(error2) {
1530
+ function errorMessages(error2) {
1335
1531
  const messages = [];
1336
1532
  const visited = /* @__PURE__ */ new Set();
1337
1533
  let current = error2;
@@ -1355,12 +1551,81 @@ function isSessionStoreWriteFailure(error2) {
1355
1551
  }
1356
1552
  break;
1357
1553
  }
1358
- return messages.some(
1554
+ return messages;
1555
+ }
1556
+ function isSessionStoreWriteFailure(error2) {
1557
+ return errorMessages(error2).some(
1359
1558
  (message) => /(?:database or disk is full|disk is full|no space left on device|SQLITE_(?:FULL|IOERR)(?:\b|_))/i.test(
1360
1559
  message
1361
1560
  )
1362
1561
  );
1363
1562
  }
1563
+ var SQLITE_CAUSE_TOKEN = /(?:^|cause:\s+)(SQLiteError:\s+[A-Za-z][A-Za-z0-9]*(?:\s+[A-Za-z][A-Za-z0-9]*){0,7})(?=\s*(?:[)\]"',;]|$))/i;
1564
+ var EFFECT_SQL_CAUSE_TOKEN = /(?:^|cause:\s+)(effect\/sql\/SqlError\/[A-Za-z][A-Za-z0-9_]*)/i;
1565
+ var PARAMS_FIELD = /\bparams\s*(?:=|:)/i;
1566
+ function isCauseBearingText(text) {
1567
+ return /^(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text.trim()) || /(?:^|\s)cause:\s+(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text);
1568
+ }
1569
+ function structuredLogFields(line) {
1570
+ const fields = [];
1571
+ for (let index = 0; index < line.length; index++) {
1572
+ if (line[index] === '"') {
1573
+ index++;
1574
+ while (index < line.length) {
1575
+ if (line[index] === "\\") {
1576
+ index++;
1577
+ } else if (line[index] === '"') {
1578
+ break;
1579
+ }
1580
+ index++;
1581
+ }
1582
+ continue;
1583
+ }
1584
+ const atFieldBoundary = index === 0 || /\s/.test(line[index - 1]);
1585
+ const key = line.startsWith("error=", index) ? "error=" : line.startsWith("cause=", index) ? "cause=" : null;
1586
+ if (!atFieldBoundary || key === null) continue;
1587
+ const valueStart = index + key.length;
1588
+ if (line[valueStart] !== '"') {
1589
+ const valueEnd2 = line.indexOf(" ", valueStart);
1590
+ fields.push(line.slice(valueStart, valueEnd2 === -1 ? line.length : valueEnd2));
1591
+ index = valueEnd2 === -1 ? line.length : valueEnd2;
1592
+ continue;
1593
+ }
1594
+ let valueEnd = valueStart + 1;
1595
+ for (; valueEnd < line.length; valueEnd++) {
1596
+ if (line[valueEnd] === "\\") {
1597
+ valueEnd++;
1598
+ } else if (line[valueEnd] === '"') {
1599
+ break;
1600
+ }
1601
+ }
1602
+ fields.push(line.slice(valueStart + 1, valueEnd));
1603
+ index = valueEnd;
1604
+ }
1605
+ return fields;
1606
+ }
1607
+ function structuredCauseMessages(text) {
1608
+ const fields = structuredLogFields(text);
1609
+ if (fields.length > 0) return fields;
1610
+ return isCauseBearingText(text) ? [text] : [];
1611
+ }
1612
+ function causeTokenFromText(text) {
1613
+ const paramsIndex = text.search(PARAMS_FIELD);
1614
+ for (const pattern of [SQLITE_CAUSE_TOKEN, EFFECT_SQL_CAUSE_TOKEN]) {
1615
+ const match = pattern.exec(text);
1616
+ if (match?.[1] && (paramsIndex === -1 || match.index < paramsIndex)) {
1617
+ return match[1];
1618
+ }
1619
+ }
1620
+ return null;
1621
+ }
1622
+ function sessionStoreCauseToken(value) {
1623
+ for (const message of structuredCauseMessages(value)) {
1624
+ const token = causeTokenFromText(message);
1625
+ if (token !== null) return redactDiagnosticText(token).slice(0, 120);
1626
+ }
1627
+ return null;
1628
+ }
1364
1629
  function insufficientSpaceReason(dbPath, requiredBytes) {
1365
1630
  try {
1366
1631
  const fsStats = statfsSync(dirname2(dbPath));
@@ -1382,6 +1647,7 @@ function readFreelistCount(db) {
1382
1647
  return db.prepare("PRAGMA freelist_count").get().freelist_count;
1383
1648
  }
1384
1649
  function readCheckpointResult(db) {
1650
+ db.exec("PRAGMA busy_timeout=0");
1385
1651
  const row = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
1386
1652
  return { busy: row.busy !== 0, log: row.log, checkpointed: row.checkpointed };
1387
1653
  }
@@ -1399,7 +1665,7 @@ async function probeReclaimAvailability(input) {
1399
1665
  try {
1400
1666
  const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
1401
1667
  try {
1402
- db.exec("PRAGMA busy_timeout=5000");
1668
+ db.exec(`PRAGMA busy_timeout=${SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS}`);
1403
1669
  autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1404
1670
  } finally {
1405
1671
  db.close();
@@ -1419,7 +1685,8 @@ async function reclaimSessionDbSpace(input) {
1419
1685
  timeBudgetMs = SESSION_DB_RECLAIM_TIME_BUDGET_MS,
1420
1686
  chunkPages = SESSION_DB_RECLAIM_CHUNK_PAGES,
1421
1687
  now = () => performance.now(),
1422
- allowFullVacuum = true
1688
+ hasLiveTurn = false,
1689
+ hasProvenanceAnomaly = false
1423
1690
  } = input;
1424
1691
  let sqlite;
1425
1692
  try {
@@ -1435,17 +1702,19 @@ async function reclaimSessionDbSpace(input) {
1435
1702
  let db;
1436
1703
  try {
1437
1704
  db = new DatabaseSync(dbPath);
1438
- db.exec("PRAGMA busy_timeout=5000");
1705
+ db.exec(`PRAGMA busy_timeout=${SESSION_DB_RECLAIM_BUSY_TIMEOUT_MS}`);
1439
1706
  const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1440
1707
  if (autoVacuum === 0) {
1441
- if (!allowFullVacuum) {
1442
- console.warn(
1443
- `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: a session turn is live`
1444
- );
1708
+ if (hasLiveTurn || hasProvenanceAnomaly) {
1709
+ const detail = [
1710
+ ...hasLiveTurn ? ["a session turn is live"] : [],
1711
+ ...hasProvenanceAnomaly ? ["session-DB provenance is anomalous"] : []
1712
+ ].join(" and ");
1713
+ console.warn(`[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: ${detail}`);
1445
1714
  return {
1446
1715
  ok: false,
1447
1716
  skipped: "full-vacuum-blocked",
1448
- detail: "a session turn is live"
1717
+ detail
1449
1718
  };
1450
1719
  }
1451
1720
  const fileBytesForGuard = statSync2(dbPath).size;
@@ -1463,7 +1732,9 @@ async function reclaimSessionDbSpace(input) {
1463
1732
  db.exec("VACUUM");
1464
1733
  const afterBytes = readLogicalBytes(db);
1465
1734
  const freelistAfter = readFreelistCount(db);
1735
+ const checkpointStartedAt = now();
1466
1736
  const checkpoint = readCheckpointResult(db);
1737
+ const checkpointElapsedMs = Math.max(0, now() - checkpointStartedAt);
1467
1738
  return {
1468
1739
  ok: true,
1469
1740
  mode: "convert",
@@ -1474,10 +1745,18 @@ async function reclaimSessionDbSpace(input) {
1474
1745
  pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1475
1746
  stoppedBy: "freelist-drained",
1476
1747
  elapsedMs: Math.max(0, now() - startedAt),
1748
+ checkpointElapsedMs,
1477
1749
  checkpoint
1478
1750
  };
1479
1751
  }
1480
1752
  if (autoVacuum === 2) {
1753
+ if (hasLiveTurn) {
1754
+ const detail = "a session turn is live";
1755
+ console.warn(
1756
+ `[reclaimSessionDbSpace] skipping incremental reclaim of ${dbPath}: ${detail}`
1757
+ );
1758
+ return { ok: false, skipped: "live-turn", detail };
1759
+ }
1481
1760
  const beforeBytes = readLogicalBytes(db);
1482
1761
  const freelistBefore = readFreelistCount(db);
1483
1762
  const startedAt = now();
@@ -1502,7 +1781,9 @@ async function reclaimSessionDbSpace(input) {
1502
1781
  freelistAfter = readFreelistCount(db);
1503
1782
  }
1504
1783
  const afterBytes = readLogicalBytes(db);
1784
+ const checkpointStartedAt = now();
1505
1785
  const checkpoint = readCheckpointResult(db);
1786
+ const checkpointElapsedMs = Math.max(0, now() - checkpointStartedAt);
1506
1787
  return {
1507
1788
  ok: true,
1508
1789
  mode: "incremental",
@@ -1513,6 +1794,7 @@ async function reclaimSessionDbSpace(input) {
1513
1794
  pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1514
1795
  stoppedBy,
1515
1796
  elapsedMs: Math.max(0, now() - startedAt),
1797
+ checkpointElapsedMs,
1516
1798
  checkpoint
1517
1799
  };
1518
1800
  }
@@ -1532,6 +1814,263 @@ async function reclaimSessionDbSpace(input) {
1532
1814
  }
1533
1815
  }
1534
1816
 
1817
+ // src/lib/opencode/dependency-install-diagnostic.ts
1818
+ import { lstatSync, realpathSync } from "node:fs";
1819
+ import { basename, dirname as dirname3, isAbsolute, join as join2, relative, resolve, sep } from "node:path";
1820
+ var DEPENDENCY_INSTALL_FAILURE_MESSAGE = "background dependency install failed";
1821
+ var MAX_DISTINCT_DIAGNOSTICS = 32;
1822
+ var EFFECT = "OpenCode could not install background dependencies for this scope; its plugins and tools may be unavailable";
1823
+ function readOpenCodeLogField(line, key) {
1824
+ const match = line.match(new RegExp(`(?:^|\\s)${key}=(?:"((?:\\\\.|[^"])*)"|(\\S+))`));
1825
+ if (!match) return void 0;
1826
+ if (match[1] === void 0) return match[2];
1827
+ return match[1].replace(/\\(["\\n])/g, (_match, escaped) => {
1828
+ return escaped === "n" ? "\n" : escaped;
1829
+ });
1830
+ }
1831
+ function parseTrigger(line) {
1832
+ if (readOpenCodeLogField(line, "message") !== DEPENDENCY_INSTALL_FAILURE_MESSAGE) {
1833
+ return null;
1834
+ }
1835
+ const error2 = readOpenCodeLogField(line, "error");
1836
+ if (!error2 || !/\bEACCES\b/.test(error2)) return null;
1837
+ const errnoMatch = error2.match(/\bEACCES:\s*permission denied,\s*([a-z_]+)\s+'([^']*)'/);
1838
+ const capturedPath = errnoMatch?.[2];
1839
+ return {
1840
+ dir: readOpenCodeLogField(line, "dir"),
1841
+ op: errnoMatch?.[1],
1842
+ path: capturedPath && isAbsolute(capturedPath) ? capturedPath : void 0
1843
+ };
1844
+ }
1845
+ function getErrorCode(error2) {
1846
+ if (typeof error2 === "object" && error2 !== null && "code" in error2) {
1847
+ const code = error2.code;
1848
+ if (typeof code === "string") return code;
1849
+ }
1850
+ return "UNKNOWN";
1851
+ }
1852
+ function isWithin(path, root) {
1853
+ const candidate = resolve(path);
1854
+ const resolvedRoot = resolve(root);
1855
+ return candidate === resolvedRoot || candidate.startsWith(`${resolvedRoot}${sep}`);
1856
+ }
1857
+ function rootSpecs(context) {
1858
+ return [
1859
+ { token: "~", configured: resolve(context.homeDir) },
1860
+ { token: "$WORKSPACE", configured: resolve(context.cwd) }
1861
+ ];
1862
+ }
1863
+ function resolveRoots(context) {
1864
+ const resolved = [];
1865
+ const unresolved = [];
1866
+ for (const spec of rootSpecs(context)) {
1867
+ try {
1868
+ resolved.push({ ...spec, real: realpathSync(spec.configured) });
1869
+ } catch (_error) {
1870
+ unresolved.push(spec);
1871
+ }
1872
+ }
1873
+ return { resolved, unresolved };
1874
+ }
1875
+ function resolveNearestAncestor(failingPath, remaining, error2) {
1876
+ let ancestor = dirname3(failingPath);
1877
+ const suffix = [basename(failingPath), ...remaining];
1878
+ while (ancestor) {
1879
+ try {
1880
+ return {
1881
+ path: resolve(realpathSync(ancestor), ...suffix),
1882
+ failure: { path: resolve(failingPath), error: error2 }
1883
+ };
1884
+ } catch (ancestorError) {
1885
+ if (ancestor === dirname3(ancestor) || !getErrorCode(ancestorError).startsWith("E")) {
1886
+ return null;
1887
+ }
1888
+ suffix.unshift(basename(ancestor));
1889
+ ancestor = dirname3(ancestor);
1890
+ }
1891
+ }
1892
+ return null;
1893
+ }
1894
+ function resolveExistingPath(absolutePath) {
1895
+ if (!isAbsolute(absolutePath)) return null;
1896
+ const candidate = resolve(absolutePath);
1897
+ const remaining = [];
1898
+ let current = candidate;
1899
+ while (current) {
1900
+ try {
1901
+ const stat2 = lstatSync(current);
1902
+ if (stat2.isSymbolicLink()) {
1903
+ if (remaining.length === 0) {
1904
+ return { path: join2(realpathSync(dirname3(current)), basename(current)) };
1905
+ }
1906
+ return { path: resolve(realpathSync(current), ...remaining) };
1907
+ }
1908
+ return { path: resolve(realpathSync(current), ...remaining) };
1909
+ } catch (error2) {
1910
+ const code = getErrorCode(error2);
1911
+ if (code !== "ENOENT" && code !== "ENOTDIR") {
1912
+ return resolveNearestAncestor(current, remaining, error2);
1913
+ }
1914
+ }
1915
+ if (current === dirname3(current)) return null;
1916
+ remaining.unshift(basename(current));
1917
+ current = dirname3(current);
1918
+ }
1919
+ return null;
1920
+ }
1921
+ function tokenise(absolutePath, roots) {
1922
+ const pathResolution = resolveExistingPath(absolutePath);
1923
+ if (!pathResolution) return null;
1924
+ const root = roots.reduce((selected, candidate) => {
1925
+ if (!isWithin(pathResolution.path, candidate.real)) return selected;
1926
+ if (!selected || candidate.real.length > selected.real.length || candidate.real.length === selected.real.length && candidate.token === "$WORKSPACE") {
1927
+ return candidate;
1928
+ }
1929
+ return selected;
1930
+ }, null);
1931
+ if (!root) return null;
1932
+ const suffix = relative(root.real, pathResolution.path);
1933
+ return {
1934
+ value: suffix ? `${root.token}/${suffix.split(sep).join("/")}` : root.token,
1935
+ root,
1936
+ resolutionFailure: pathResolution.failure
1937
+ };
1938
+ }
1939
+ function classifyScope(dir, roots) {
1940
+ if (!dir) return "other";
1941
+ const tokenisedDir = tokenise(dir, roots.resolved);
1942
+ if (tokenisedDir) return tokenisedDir.root.token === "$WORKSPACE" ? "workspace" : "global";
1943
+ for (const spec of roots.unresolved) {
1944
+ if (isWithin(dir, spec.configured)) {
1945
+ return spec.token === "$WORKSPACE" ? "workspace" : "global";
1946
+ }
1947
+ }
1948
+ return "other";
1949
+ }
1950
+ function unresolvedRootForPath(path, unresolved) {
1951
+ return unresolved.find((spec) => isWithin(path, spec.configured));
1952
+ }
1953
+ function statType(stat2) {
1954
+ if (stat2.isDirectory()) return "d";
1955
+ if (stat2.isFile()) return "f";
1956
+ if (stat2.isSymbolicLink()) return "l";
1957
+ return "o";
1958
+ }
1959
+ function renderStat(name, stat2) {
1960
+ const mode = (stat2.mode & 4095).toString(8).padStart(4, "0");
1961
+ return `${name}:${statType(stat2)}:${stat2.uid}:${stat2.gid}:${mode}`;
1962
+ }
1963
+ function renderStatError(name, error2) {
1964
+ const code = getErrorCode(error2);
1965
+ return `${name}:${renderErrorCode(code)}`;
1966
+ }
1967
+ function renderErrorCode(code) {
1968
+ if (code === "ENOENT") return "missing";
1969
+ return code.startsWith("E") ? code : `E${code}`;
1970
+ }
1971
+ function inspectChain(rootReal, rootToken, absolutePath, resolutionFailure) {
1972
+ const target = resolve(absolutePath);
1973
+ const relativePath = relative(rootReal, target);
1974
+ const components = relativePath ? relativePath.split(sep) : [];
1975
+ const entries = [];
1976
+ let current = rootReal;
1977
+ let status2 = "complete";
1978
+ let finalStat;
1979
+ for (const name of [rootToken, ...components]) {
1980
+ if (name !== rootToken) current = join2(current, name);
1981
+ if (resolutionFailure && resolve(current) === resolve(resolutionFailure.path)) {
1982
+ entries.push(renderStatError(name, resolutionFailure.error));
1983
+ status2 = "stat_failed";
1984
+ break;
1985
+ }
1986
+ try {
1987
+ const stat2 = lstatSync(current);
1988
+ entries.push(renderStat(name, stat2));
1989
+ finalStat = stat2;
1990
+ } catch (error2) {
1991
+ entries.push(renderStatError(name, error2));
1992
+ if (getErrorCode(error2) !== "ENOENT") status2 = "stat_failed";
1993
+ break;
1994
+ }
1995
+ }
1996
+ let symlinkTarget;
1997
+ let symlinkMeta;
1998
+ if (finalStat?.isSymbolicLink() && entries.length === components.length + 1) {
1999
+ try {
2000
+ symlinkTarget = realpathSync(current);
2001
+ try {
2002
+ symlinkMeta = renderStat("", lstatSync(symlinkTarget)).slice(1);
2003
+ } catch (error2) {
2004
+ symlinkMeta = renderErrorCode(getErrorCode(error2));
2005
+ status2 = "stat_failed";
2006
+ }
2007
+ } catch (error2) {
2008
+ symlinkTarget = "<unresolved>";
2009
+ symlinkMeta = renderErrorCode(getErrorCode(error2));
2010
+ status2 = "stat_failed";
2011
+ }
2012
+ }
2013
+ return { status: status2, chain: entries.join(","), symlinkTarget, symlinkMeta };
2014
+ }
2015
+ function buildDiagnostic(line, context) {
2016
+ const trigger = parseTrigger(line);
2017
+ if (!trigger) return null;
2018
+ const roots = resolveRoots(context);
2019
+ const scope = classifyScope(trigger.dir, roots);
2020
+ const pathSource = trigger.path ? "error" : trigger.dir ? "dir" : "none";
2021
+ const inspectedPath = trigger.path ?? trigger.dir;
2022
+ const microvmId = context.microvmId?.trim() || "unknown";
2023
+ const euid = context.euid === void 0 ? "unavailable" : String(context.euid);
2024
+ const egid = context.egid === void 0 ? "unavailable" : String(context.egid);
2025
+ const op = trigger.op ?? "unknown";
2026
+ if (!inspectedPath) {
2027
+ return {
2028
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=no_path path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=unknown cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=unavailable`,
2029
+ localDetail: null
2030
+ };
2031
+ }
2032
+ const tokenisedPath = tokenise(inspectedPath, roots.resolved);
2033
+ if (!tokenisedPath) {
2034
+ const unresolvedRoot = unresolvedRootForPath(inspectedPath, roots.unresolved);
2035
+ if (unresolvedRoot) {
2036
+ return {
2037
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=root_unresolved path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=unknown cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=unavailable`,
2038
+ localDetail: null
2039
+ };
2040
+ }
2041
+ return {
2042
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=outside_roots path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=<outside-approved-roots> cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=unavailable`,
2043
+ localDetail: `OPENCODE-DEPENDENCY-INSTALL-EACCES (local only): outside approved roots: ${inspectedPath}`
2044
+ };
2045
+ }
2046
+ const inspection = inspectChain(
2047
+ tokenisedPath.root.real,
2048
+ tokenisedPath.root.token,
2049
+ inspectedPath,
2050
+ tokenisedPath.resolutionFailure
2051
+ );
2052
+ const optionalSymlinkFields = inspection.symlinkTarget ? ` symlink_target=${tokenise(inspection.symlinkTarget, roots.resolved)?.value ?? "<outside-approved-roots>"}` + (inspection.symlinkMeta ? ` symlink_meta=${inspection.symlinkMeta}` : "") : "";
2053
+ return {
2054
+ event: `OPENCODE-DEPENDENCY-INSTALL-EACCES: status=${inspection.status} path_source=${pathSource} scope=${scope} microvm_id=${microvmId} op=${op} path=${tokenisedPath.value} cli_euid=${euid} cli_egid=${egid} effect="${EFFECT}" chain=${inspection.chain}${optionalSymlinkFields}`,
2055
+ localDetail: null
2056
+ };
2057
+ }
2058
+ function createDependencyInstallDiagnosticReporter({
2059
+ context,
2060
+ emitEvent: emitEvent2,
2061
+ emitLocal
2062
+ }) {
2063
+ const emittedEvents = /* @__PURE__ */ new Set();
2064
+ return (line) => {
2065
+ const diagnostic = buildDiagnostic(line, context);
2066
+ if (!diagnostic || emittedEvents.has(diagnostic.event)) return;
2067
+ if (emittedEvents.size >= MAX_DISTINCT_DIAGNOSTICS) return;
2068
+ emittedEvents.add(diagnostic.event);
2069
+ emitEvent2(diagnostic.event);
2070
+ if (diagnostic.localDetail) emitLocal(diagnostic.localDetail);
2071
+ };
2072
+ }
2073
+
1535
2074
  // src/lib/runner-activity-telemetry.ts
1536
2075
  var FORWARDED_LEVELS_BY_SOURCE = {
1537
2076
  "cli.run": /* @__PURE__ */ new Set(["warn", "error"]),
@@ -1549,11 +2088,13 @@ function parseOpenCodeLogLine(line) {
1549
2088
  if (!levelMatch) return null;
1550
2089
  const level = levelMatch[1].toUpperCase();
1551
2090
  if (level !== "WARN" && level !== "ERROR") return null;
1552
- const sessionMatch = normalisedLine.match(/(?:^|\s)sessionID=(\S+)/);
2091
+ const sessionMatch = normalisedLine.match(/(?:^|\s)(?:sessionID|session\.id)=(\S+)/);
2092
+ const messageIDMatch = normalisedLine.match(/(?:^|\s)messageID=(\S+)/);
1553
2093
  const messageMatch = normalisedLine.match(/(?:^|\s)message=(?:"((?:\\.|[^"])*)"|(\S+))/);
1554
2094
  const parsed = {
1555
2095
  level: level === "WARN" ? "warn" : "error",
1556
- sessionID: sessionMatch?.[1]
2096
+ sessionID: sessionMatch?.[1],
2097
+ messageID: messageIDMatch?.[1]
1557
2098
  };
1558
2099
  const message = messageMatch?.[1] ?? messageMatch?.[2];
1559
2100
  if (message !== void 0) parsed.message = message;
@@ -1561,18 +2102,29 @@ function parseOpenCodeLogLine(line) {
1561
2102
  }
1562
2103
  var MAX_LINE_BUFFER_BYTES = 16 * 1024;
1563
2104
  var REPEAT_SUPPRESSION_WINDOW_MS = 6e4;
1564
- function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure) {
2105
+ function sanitiseDependencyInstallWarning(line) {
2106
+ const withoutDirectory = line.replace(
2107
+ /((?:^|\s)dir=)(?:"(?:\\.|[^"])*"|\/[^\s)"']+|\S+)/g,
2108
+ "$1<path>"
2109
+ );
2110
+ return withoutDirectory.replace(
2111
+ /((?:^|\s)error=)"((?:\\.|[^"])*)"/,
2112
+ (_match, prefix, error2) => `${prefix}"${error2.replace(/'\/[^']*'/g, "'<path>'")}"`
2113
+ );
2114
+ }
2115
+ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure, onDependencyInstallFailure) {
1565
2116
  let buffer = Buffer.alloc(0);
1566
2117
  const repeatStates = /* @__PURE__ */ new Map();
1567
2118
  const forwardLine = (line, parsed, repeatsSuppressed = 0) => {
1568
2119
  const metadata = {
1569
2120
  ...parsed.sessionID ? { sessionID: parsed.sessionID } : {},
2121
+ ...parsed.messageID ? { messageID: parsed.messageID } : {},
1570
2122
  ...repeatsSuppressed > 0 ? { repeats_suppressed: repeatsSuppressed } : {}
1571
2123
  };
1572
2124
  forwardRunnerActivity(
1573
2125
  {
1574
2126
  level: parsed.level,
1575
- error: line,
2127
+ error: parsed.message === DEPENDENCY_INSTALL_FAILURE_MESSAGE ? sanitiseDependencyInstallWarning(line) : line,
1576
2128
  metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
1577
2129
  source: "opencode"
1578
2130
  },
@@ -1590,20 +2142,9 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1590
2142
  repeatStates.delete(key);
1591
2143
  }
1592
2144
  };
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;
2145
+ const forwardWithRepeatSuppression = (line, parsed) => {
1605
2146
  const now = Date.now();
1606
- const key = parsed.message === void 0 ? void 0 : `${parsed.level}\0${parsed.message}`;
2147
+ const key = parsed.message === void 0 ? void 0 : `${parsed.level}\0${parsed.message}\0${parsed.sessionID ?? ""}\0${parsed.messageID ?? ""}`;
1607
2148
  expireRepeatStates(now, key);
1608
2149
  if (key === void 0) {
1609
2150
  forwardLine(line, parsed);
@@ -1624,6 +2165,35 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1624
2165
  previous.parsed = parsed;
1625
2166
  previous.repeatsSuppressed++;
1626
2167
  };
2168
+ const flushLine = (line) => {
2169
+ const parsed = parseOpenCodeLogLine(line);
2170
+ const isDiskFailure = isSessionStoreWriteFailure(line);
2171
+ const cause = sessionStoreCauseToken(line);
2172
+ if (onSessionStoreWriteFailure && (isDiskFailure || cause !== null)) {
2173
+ try {
2174
+ onSessionStoreWriteFailure({
2175
+ isDiskFailure,
2176
+ sessionId: parsed?.sessionID ?? null,
2177
+ cause
2178
+ });
2179
+ } catch (err) {
2180
+ reportTelemetryDiagnostic(
2181
+ `Runner activity telemetry could not record session-store write failure: ${err instanceof Error ? err.message : String(err)}`
2182
+ );
2183
+ }
2184
+ }
2185
+ if (!parsed) return;
2186
+ forwardWithRepeatSuppression(line, parsed);
2187
+ if (onDependencyInstallFailure && parsed.message === DEPENDENCY_INSTALL_FAILURE_MESSAGE) {
2188
+ try {
2189
+ onDependencyInstallFailure(line);
2190
+ } catch (err) {
2191
+ reportTelemetryDiagnostic(
2192
+ `Runner activity telemetry could not record dependency-install failure diagnostic: ${err instanceof Error ? err.message : String(err)}`
2193
+ );
2194
+ }
2195
+ }
2196
+ };
1627
2197
  const forward = ((chunk) => {
1628
2198
  buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, "utf-8")]);
1629
2199
  let newlineIndex;
@@ -1649,34 +2219,38 @@ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure)
1649
2219
  var MAX_MESSAGE_LENGTH = 500;
1650
2220
  var MAX_METADATA_VALUE_LENGTH = 200;
1651
2221
  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
2222
  function truncate(message) {
1678
2223
  if (message.length <= MAX_MESSAGE_LENGTH) return message;
1679
- return message.slice(0, MAX_MESSAGE_LENGTH - TRUNCATION_MARKER.length) + TRUNCATION_MARKER;
2224
+ let dropped = message.length - MAX_MESSAGE_LENGTH;
2225
+ let marker = `\u2026${dropped} chars omitted\u2026`;
2226
+ let previousDropped = -1;
2227
+ while (dropped !== previousDropped) {
2228
+ previousDropped = dropped;
2229
+ const nextDropped = message.length - MAX_MESSAGE_LENGTH + marker.length;
2230
+ dropped = nextDropped;
2231
+ marker = `\u2026${dropped} chars omitted\u2026`;
2232
+ }
2233
+ const remaining = MAX_MESSAGE_LENGTH - marker.length;
2234
+ const headLength = Math.ceil(remaining * 0.6);
2235
+ const tailLength = remaining - headLength;
2236
+ const effectMatch = message.startsWith("OPENCODE-DEPENDENCY-INSTALL-EACCES:") ? message.match(/ effect="[^"]*"/) : null;
2237
+ if (effectMatch?.index !== void 0) {
2238
+ const effectEnd = effectMatch.index + effectMatch[0].length;
2239
+ if (effectEnd > headLength && effectEnd < message.length) {
2240
+ const effectLength = effectMatch[0].length;
2241
+ const nonEffectLength = remaining - effectLength;
2242
+ const preservedTailLength = Math.min(
2243
+ message.length - effectEnd,
2244
+ nonEffectLength - Math.min(effectMatch.index, Math.ceil(nonEffectLength * 0.6))
2245
+ );
2246
+ const preservedHeadLength = Math.min(
2247
+ effectMatch.index,
2248
+ nonEffectLength - preservedTailLength
2249
+ );
2250
+ return message.slice(0, preservedHeadLength) + marker + message.slice(effectMatch.index, effectEnd) + message.slice(message.length - preservedTailLength);
2251
+ }
2252
+ }
2253
+ return message.slice(0, headLength) + marker + message.slice(message.length - tailLength);
1680
2254
  }
1681
2255
  function sanitiseMetadata(metadata) {
1682
2256
  if (!metadata) return {};
@@ -1753,10 +2327,10 @@ function forwardRunnerActivity(entry, context) {
1753
2327
 
1754
2328
  // src/lib/opencode/session-db-recovery-report.ts
1755
2329
  import { readFileSync as readFileSync2, unlinkSync } from "node:fs";
1756
- import { join as join2 } from "node:path";
2330
+ import { join as join3 } from "node:path";
1757
2331
  function sessionDbRecoveryReportPath(homeDir, env) {
1758
2332
  const override = env.EVIDENT_SESSION_DB_RECOVERY_REPORT?.trim();
1759
- return override || join2(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
2333
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
1760
2334
  }
1761
2335
  function drainSessionDbRecoveryReport({
1762
2336
  homeDir,
@@ -1886,12 +2460,23 @@ function buildSessionDbRecoveryActivity(record) {
1886
2460
  metadata: withoutContractFields(record),
1887
2461
  message: `This runner started without restoring session history because it could not verify where its session history is backed up. Nothing in the backup was changed; check the runner backup configuration and permissions.${replication}`
1888
2462
  };
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
- };
2463
+ case "session_db_boot_refused": {
2464
+ const message = (() => {
2465
+ switch (record.reason) {
2466
+ case "local_discard_failed":
2467
+ return `This runner did not come online because its damaged session database could not be safely separated from its active backup or proven removed. Backed-up session history remains readable at ${record.quarantine_destination ?? "its original location or the quarantine destination named in the boot logs"}; any local session-database files that remain were left in place and nothing opened or wrote them. See the runner boot logs for SESSION-DB-LOCAL-DISCARD-FAILED details.`;
2468
+ case "replica_separation_unproven":
2469
+ return `This runner did not come online because its damaged session database could not be safely separated from its active backup. The SESSION-DB-REPLICA-SEPARATION-UNVERIFIED boot log contains the partial-quarantine moved and remaining counts. Backed-up session history remains readable at ${record.quarantine_destination ?? "its original location or the quarantine destination named in the boot logs"}; any local session-database files that remain were left in place and nothing opened or wrote them.`;
2470
+ case "verification_timeout_before_separation":
2471
+ return "This runner did not come online because session database verification timed out after detecting corruption, before replica separation was attempted. No replica progress is claimed. See SESSION-DB-VERIFY-TIMEOUT in the runner boot logs before retrying.";
2472
+ case "verification_timeout_during_separation":
2473
+ return "This runner did not come online because session database verification timed out while replica separation was in progress. Separation completion and object removal were not proven. See SESSION-DB-VERIFY-TIMEOUT in the runner boot logs before retrying.";
2474
+ default:
2475
+ return "This runner did not come online because session database recovery could not be completed. Inspect the runner boot logs for the refusal reason before retrying.";
2476
+ }
2477
+ })();
2478
+ return { level, metadata: withoutContractFields(record), message };
2479
+ }
1895
2480
  case "schema_provenance_mismatch":
1896
2481
  return {
1897
2482
  level,
@@ -2018,7 +2603,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
2018
2603
  if (health.healthy || health.authFailed) {
2019
2604
  return health;
2020
2605
  }
2021
- await new Promise((resolve4) => setTimeout(resolve4, 1e3));
2606
+ await new Promise((resolve5) => setTimeout(resolve5, 1e3));
2022
2607
  }
2023
2608
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
2024
2609
  }
@@ -2029,7 +2614,7 @@ async function waitForOpenCode2Health(port, password, timeoutMs = 3e4) {
2029
2614
  if (health.healthy || health.authFailed) {
2030
2615
  return health;
2031
2616
  }
2032
- await new Promise((resolve4) => setTimeout(resolve4, 1e3));
2617
+ await new Promise((resolve5) => setTimeout(resolve5, 1e3));
2033
2618
  }
2034
2619
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
2035
2620
  }
@@ -2141,7 +2726,7 @@ async function getOpenCodeDirectory(port, client) {
2141
2726
  return null;
2142
2727
  }
2143
2728
  }
2144
- function roleOf(m) {
2729
+ function roleOf2(m) {
2145
2730
  if (!m || typeof m !== "object") return void 0;
2146
2731
  if (typeof m.role === "string") return m.role;
2147
2732
  const infoRole = m.info?.role;
@@ -2155,13 +2740,13 @@ function createdOf(m) {
2155
2740
  if (!m || typeof m !== "object") return void 0;
2156
2741
  return m.info?.time?.created ?? m.time?.created;
2157
2742
  }
2158
- function idOf(m) {
2743
+ function idOf2(m) {
2159
2744
  if (!m || typeof m !== "object") return void 0;
2160
2745
  if (typeof m.id === "string") return m.id;
2161
2746
  const infoId = m.info?.id;
2162
2747
  return typeof infoId === "string" ? infoId : void 0;
2163
2748
  }
2164
- function parentIdOf(m) {
2749
+ function parentIdOf2(m) {
2165
2750
  if (!m || typeof m !== "object") return void 0;
2166
2751
  if (typeof m.parentID === "string") return m.parentID;
2167
2752
  const infoParent = m.info?.parentID;
@@ -2177,8 +2762,12 @@ function errorOf(m) {
2177
2762
  if (!m || typeof m !== "object") return void 0;
2178
2763
  return m.info?.error ?? m.error;
2179
2764
  }
2765
+ function isTerminalStopAnswer(m) {
2766
+ return completedOf(m) != null && errorOf(m) == null && finishOf(m) === "stop";
2767
+ }
2180
2768
  function isAssistantInFlight(m) {
2181
2769
  if (completedOf(m) == null) return true;
2770
+ if (errorOf(m) != null) return false;
2182
2771
  return finishOf(m) === "tool-calls";
2183
2772
  }
2184
2773
  async function getSessionMessages(port, sessionId, client) {
@@ -2226,7 +2815,7 @@ async function pollSessionMessagesForRedrive(port, sessionId, client) {
2226
2815
  function isSessionActivelyGenerating(messages) {
2227
2816
  if (!messages || messages.length === 0) return false;
2228
2817
  const last = messages[messages.length - 1];
2229
- if (roleOf(last) !== "assistant") return false;
2818
+ if (roleOf2(last) !== "assistant") return false;
2230
2819
  return completedOf(last) == null;
2231
2820
  }
2232
2821
  var FULL_SESSION_ENUMERATION_MAX_SESSIONS = 3200;
@@ -2292,7 +2881,18 @@ async function deleteSession(port, id, client) {
2292
2881
  { method: "DELETE" },
2293
2882
  { allowStatuses: ALL_HTTP_STATUSES }
2294
2883
  );
2295
- return { ok: res.status >= 200 && res.status < 300, status: res.status };
2884
+ if (res.status >= 200 && res.status < 300) {
2885
+ return { ok: true, status: res.status };
2886
+ }
2887
+ try {
2888
+ const body = await res.text();
2889
+ return body ? { ok: false, status: res.status, body } : { ok: false, status: res.status };
2890
+ } catch (error2) {
2891
+ console.error(
2892
+ `[deleteSession] DELETE /session/${id} response body read failed for HTTP ${res.status}: ${error2 instanceof Error ? error2.message : String(error2)}`
2893
+ );
2894
+ return { ok: false, status: res.status };
2895
+ }
2296
2896
  } catch (error2) {
2297
2897
  console.error(
2298
2898
  `[deleteSession] DELETE /session/${id} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
@@ -2535,11 +3135,31 @@ function messageText(m) {
2535
3135
  if (!m || !Array.isArray(m.parts)) return "";
2536
3136
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2537
3137
  }
2538
- async function sendPromptAsync(port, sessionId, content, options, attachments, client) {
3138
+ function findDispatchedUserMessageId(messages, content, excludeIds) {
3139
+ if (!messages || messages.length === 0) return null;
3140
+ let best = null;
3141
+ for (const message of messages) {
3142
+ if (roleOf2(message) !== "user") continue;
3143
+ const id = idOf2(message);
3144
+ if (typeof id !== "string" || excludeIds?.has(id)) continue;
3145
+ if (messageText(message) !== content) continue;
3146
+ const created = createdOf(message) ?? 0;
3147
+ if (best === null || created > best.created) best = { id, created };
3148
+ }
3149
+ return best?.id ?? null;
3150
+ }
3151
+ async function sendPromptAsync(port, sessionId, content, options, attachments, client, callbacks) {
2539
3152
  const before = await getSessionMessages(port, sessionId, client);
2540
3153
  const knownUserIds = new Set(
2541
- (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
3154
+ (before ?? []).filter((m) => roleOf2(m) === "user").map((m) => idOf2(m)).filter((id) => typeof id === "string")
2542
3155
  );
3156
+ if (callbacks) {
3157
+ try {
3158
+ callbacks.onKnownUserIds(before === null ? null : knownUserIds);
3159
+ } catch (error2) {
3160
+ callbacks.onKnownUserIdsError(error2);
3161
+ }
3162
+ }
2543
3163
  const parts = [{ type: "text", text: content }];
2544
3164
  let pendingOutcomes = null;
2545
3165
  if (attachments && attachments.inputs.length > 0) {
@@ -2578,25 +3198,13 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2578
3198
  const READ_BACK_DELAY_MS = 150;
2579
3199
  for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
2580
3200
  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
- }
3201
+ const dispatchedUserMessageId = findDispatchedUserMessageId(after, content, knownUserIds);
3202
+ if (dispatchedUserMessageId !== null) {
3203
+ if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
3204
+ return dispatchedUserMessageId;
2597
3205
  }
2598
3206
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2599
- await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
3207
+ await new Promise((resolve5) => setTimeout(resolve5, READ_BACK_DELAY_MS));
2600
3208
  }
2601
3209
  }
2602
3210
  return null;
@@ -2604,57 +3212,68 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2604
3212
  function findAssistantReplyAfter(messages, userMessageId) {
2605
3213
  if (!messages || messages.length === 0) return null;
2606
3214
  const byParent = messages.find(
2607
- (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
3215
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2608
3216
  );
2609
3217
  if (byParent) return byParent;
2610
- const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
3218
+ const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2611
3219
  if (userIndex === -1) return null;
2612
3220
  for (let i = userIndex + 1; i < messages.length; i++) {
2613
- if (roleOf(messages[i]) === "assistant") return messages[i];
3221
+ if (roleOf2(messages[i]) === "assistant") return messages[i];
2614
3222
  }
2615
3223
  return null;
2616
3224
  }
2617
3225
  function findLastAssistantReplyFor(messages, userMessageId) {
2618
3226
  if (!messages || messages.length === 0) return null;
2619
3227
  let lastCorrelated = null;
2620
- let lastNonErrored = null;
2621
3228
  for (let i = messages.length - 1; i >= 0; i--) {
2622
3229
  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;
3230
+ if (roleOf2(m) !== "assistant" || parentIdOf2(m) !== userMessageId) continue;
3231
+ lastCorrelated = m;
3232
+ break;
3233
+ }
3234
+ if (lastCorrelated) {
3235
+ if (errorOf(lastCorrelated) == null) return lastCorrelated;
3236
+ for (let i = messages.length - 1; i >= 0; i--) {
3237
+ const m = messages[i];
3238
+ if (roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId && isTerminalStopAnswer(m)) {
3239
+ return m;
3240
+ }
2628
3241
  }
3242
+ return lastCorrelated;
2629
3243
  }
2630
- if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
2631
- const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
3244
+ const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2632
3245
  if (userIndex === -1) return null;
2633
3246
  let last = null;
2634
- let lastOk = null;
3247
+ let lastAssistantIndex = -1;
2635
3248
  for (let i = userIndex + 1; i < messages.length; i++) {
2636
- const role = roleOf(messages[i]);
3249
+ const role = roleOf2(messages[i]);
2637
3250
  if (role === "user") break;
2638
3251
  if (role === "assistant") {
2639
3252
  last = messages[i];
2640
- if (errorOf(messages[i]) == null) lastOk = messages[i];
3253
+ lastAssistantIndex = i;
2641
3254
  }
2642
3255
  }
2643
- return lastOk ?? last;
3256
+ if (last === null || errorOf(last) == null) return last;
3257
+ for (let i = lastAssistantIndex; i > userIndex; i--) {
3258
+ const m = messages[i];
3259
+ if (roleOf2(m) === "user") break;
3260
+ if (roleOf2(m) === "assistant" && isTerminalStopAnswer(m)) return m;
3261
+ }
3262
+ return last;
2644
3263
  }
2645
3264
  function collectSubagentSessions(messages, userMessageId) {
2646
3265
  if (!messages || messages.length === 0) return [];
2647
3266
  const byParent = messages.filter(
2648
- (message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
3267
+ (message) => roleOf2(message) === "assistant" && parentIdOf2(message) === userMessageId
2649
3268
  );
2650
3269
  const assistants = byParent.length > 0 ? byParent : [];
2651
3270
  if (assistants.length === 0) {
2652
- const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3271
+ const userIndex = messages.findIndex((message) => idOf2(message) === userMessageId);
2653
3272
  if (userIndex === -1) return [];
2654
3273
  for (let i = userIndex + 1; i < messages.length; i++) {
2655
3274
  const message = messages[i];
2656
- if (roleOf(message) === "user") break;
2657
- if (roleOf(message) === "assistant") assistants.push(message);
3275
+ if (roleOf2(message) === "user") break;
3276
+ if (roleOf2(message) === "assistant") assistants.push(message);
2658
3277
  }
2659
3278
  }
2660
3279
  const refs = [];
@@ -2694,7 +3313,7 @@ function collectTaskCalls(messages, userMessageId) {
2694
3313
  if (!messages || messages.length === 0) return [];
2695
3314
  const calls = [];
2696
3315
  for (const message of messages) {
2697
- if (roleOf(message) !== "assistant" || parentIdOf(message) !== userMessageId) continue;
3316
+ if (roleOf2(message) !== "assistant" || parentIdOf2(message) !== userMessageId) continue;
2698
3317
  for (const part of message.parts ?? []) {
2699
3318
  if (part.tool !== "task" || !part.callID || !part.state || part.state.status === "pending") {
2700
3319
  continue;
@@ -2724,7 +3343,7 @@ function attributeTaskCallUsage(messages, windows) {
2724
3343
  for (const window of eligibleWindows) assignments.set(window.callID, []);
2725
3344
  const unattributed = [];
2726
3345
  for (const message of messages ?? []) {
2727
- if (roleOf(message) !== "assistant") continue;
3346
+ if (roleOf2(message) !== "assistant") continue;
2728
3347
  const created = finiteNumber(createdOf(message));
2729
3348
  const matching = created === null ? [] : eligibleWindows.filter(
2730
3349
  (window) => window.timeStart <= created && (window.timeEnd === null || window.timeEnd === void 0 || created <= window.timeEnd)
@@ -2746,8 +3365,9 @@ function attributeTaskCallUsage(messages, windows) {
2746
3365
  }
2747
3366
  function sumAssistantUsage(messages) {
2748
3367
  if (!messages || messages.length === 0) return null;
2749
- const nonErrored = messages.filter((message) => errorOf(message) == null);
2750
- const selected = nonErrored.length > 0 ? nonErrored : messages;
3368
+ const hasTrailingError = errorOf(messages[messages.length - 1]) != null;
3369
+ const hasTerminalStopAnswer = messages.some(isTerminalStopAnswer);
3370
+ const selected = hasTrailingError && hasTerminalStopAnswer ? messages.filter((message) => errorOf(message) == null) : messages;
2751
3371
  let sawAnyUsage = false;
2752
3372
  let inputSum = 0;
2753
3373
  let outputSum = 0;
@@ -2756,9 +3376,11 @@ function sumAssistantUsage(messages) {
2756
3376
  let cacheWriteSum = 0;
2757
3377
  let costSum = 0;
2758
3378
  let sawCost = false;
3379
+ let toolCallCount = 0;
2759
3380
  let modelId = null;
2760
3381
  let providerId = null;
2761
3382
  for (const m of selected) {
3383
+ toolCallCount += m.parts?.filter((part) => part.type === "tool").length ?? 0;
2762
3384
  const info = m.info;
2763
3385
  if (!info) continue;
2764
3386
  const tokens = info.tokens;
@@ -2795,19 +3417,18 @@ function sumAssistantUsage(messages) {
2795
3417
  usage_tokens_cache_write: cacheWriteSum,
2796
3418
  // NULL means OpenCode never reported a cost; it is distinct from a genuine
2797
3419
  // zero-cost message, which sets `sawCost` with `costSum === 0`.
2798
- usage_cost_usd: sawCost ? costSum : null
3420
+ usage_cost_usd: sawCost ? costSum : null,
3421
+ turn_tool_calls: toolCallCount
2799
3422
  };
2800
3423
  }
2801
3424
  function messageUsage(messages, userMessageId) {
2802
3425
  if (!messages || messages.length === 0) return null;
2803
3426
  const byParentAll = messages.filter(
2804
- (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
3427
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2805
3428
  );
2806
- const byParentNonErrored = byParentAll.filter((m) => errorOf(m) == null);
2807
- const byParent = byParentNonErrored.length > 0 ? byParentNonErrored : byParentAll;
2808
3429
  let correlated;
2809
- if (byParent.length > 0) {
2810
- correlated = byParent;
3430
+ if (byParentAll.length > 0) {
3431
+ correlated = byParentAll;
2811
3432
  } else {
2812
3433
  const reply = findAssistantReplyAfter(messages, userMessageId);
2813
3434
  correlated = reply ? [reply] : [];
@@ -2817,7 +3438,7 @@ function messageUsage(messages, userMessageId) {
2817
3438
  }
2818
3439
  function messageRunState(messages, userMessageId) {
2819
3440
  if (!messages || messages.length === 0) return "unknown";
2820
- const hasUser = messages.some((m) => idOf(m) === userMessageId);
3441
+ const hasUser = messages.some((m) => idOf2(m) === userMessageId);
2821
3442
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2822
3443
  if (!hasUser) {
2823
3444
  if (!reply) return "unknown";
@@ -2853,14 +3474,38 @@ function messageError(messages, userMessageId) {
2853
3474
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2854
3475
  const error2 = errorOf(reply);
2855
3476
  if (error2 == null) return null;
2856
- if (typeof error2 === "string") return error2;
2857
- if (typeof error2 === "object") {
3477
+ let rendered;
3478
+ if (typeof error2 === "string") {
3479
+ rendered = error2;
3480
+ } else if (typeof error2 === "object") {
2858
3481
  const e = error2;
2859
3482
  const dataMessage = e.data?.message;
2860
- if (typeof dataMessage === "string") return dataMessage;
2861
- if (typeof e.message === "string") return e.message;
3483
+ if (typeof dataMessage === "string") {
3484
+ rendered = dataMessage;
3485
+ } else if (typeof e.message === "string") {
3486
+ rendered = e.message;
3487
+ } else {
3488
+ rendered = "The agent run failed.";
3489
+ }
3490
+ } else {
3491
+ rendered = "The agent run failed.";
2862
3492
  }
2863
- return "The agent run failed.";
3493
+ const toolPart = Array.isArray(reply?.parts) ? reply.parts.find(
3494
+ (part) => part && typeof part === "object" && part.type === "tool" && part.state?.status === "error"
3495
+ ) : void 0;
3496
+ return typeof toolPart?.tool === "string" && toolPart.tool.length > 0 ? `${rendered} (tool "${toolPart.tool}")` : rendered;
3497
+ }
3498
+ function describeAbandonedReplyShape(messages, userMessageId) {
3499
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
3500
+ if (!reply) return null;
3501
+ const parts = Array.isArray(reply.parts) ? reply.parts : [];
3502
+ const lastTool = [...parts].reverse().find((part) => part?.type === "tool");
3503
+ return {
3504
+ partTypes: parts.slice(-12).map((part) => typeof part?.type === "string" ? part.type : "(unknown)"),
3505
+ droppedPartCount: Math.max(0, parts.length - 12),
3506
+ lastToolName: typeof lastTool?.tool === "string" && lastTool.tool.length > 0 ? lastTool.tool : null,
3507
+ lastToolStatus: typeof lastTool?.state?.status === "string" && lastTool.state.status.length > 0 ? lastTool.state.status : null
3508
+ };
2864
3509
  }
2865
3510
  function isAbortedTerminalReply(messages, userMessageId) {
2866
3511
  const reply = findLastAssistantReplyFor(messages, userMessageId);
@@ -2877,19 +3522,32 @@ function isAbortedTerminalReply(messages, userMessageId) {
2877
3522
  }
2878
3523
  return false;
2879
3524
  }
3525
+ var CLAUDE_AUTH_EXPIRY_MESSAGES = Object.freeze([
3526
+ "Claude Code credentials are unavailable or expired. Run `claude` to refresh them."
3527
+ ]);
3528
+ function extractReplyErrorShape(reply) {
3529
+ const error2 = errorOf(reply);
3530
+ if (error2 == null || typeof error2 !== "object") {
3531
+ return { name: null, statusCode: null };
3532
+ }
3533
+ const e = error2;
3534
+ const name = typeof e.name === "string" ? e.name.slice(0, 100) : null;
3535
+ const data = e.data;
3536
+ const statusCode = typeof data?.statusCode === "number" && Number.isFinite(data.statusCode) ? data.statusCode : null;
3537
+ return { name, statusCode };
3538
+ }
2880
3539
  function classifyReplyAuthError(reply) {
2881
3540
  const error2 = errorOf(reply);
2882
3541
  if (error2 == null || typeof error2 !== "object") return null;
2883
3542
  const e = error2;
2884
3543
  const replyProviderId = reply?.info?.providerID ?? null;
2885
3544
  const replyModelId = reply?.info?.modelID ?? null;
3545
+ const data = e.data;
2886
3546
  if (e.name === "ProviderAuthError") {
2887
- const data = e.data;
2888
3547
  const providerId = typeof data?.providerID === "string" && data.providerID || replyProviderId;
2889
3548
  return { kind: "model_auth", providerId, modelId: replyModelId, reason: "missing" };
2890
3549
  }
2891
3550
  if (e.name === "APIError") {
2892
- const data = e.data;
2893
3551
  const statusCode = data?.statusCode;
2894
3552
  if (statusCode === 401 || statusCode === 403) {
2895
3553
  return {
@@ -2900,16 +3558,28 @@ function classifyReplyAuthError(reply) {
2900
3558
  };
2901
3559
  }
2902
3560
  }
3561
+ const errorMessage3 = typeof data?.message === "string" ? data.message : typeof e.message === "string" ? e.message : null;
3562
+ if (errorMessage3 !== null && CLAUDE_AUTH_EXPIRY_MESSAGES.some((knownMessage) => errorMessage3.includes(knownMessage))) {
3563
+ return {
3564
+ kind: "model_auth",
3565
+ providerId: typeof data?.providerID === "string" && data.providerID || replyProviderId,
3566
+ modelId: replyModelId,
3567
+ reason: "rejected"
3568
+ };
3569
+ }
2903
3570
  return null;
2904
3571
  }
2905
3572
  function messageFailure(messages, userMessageId) {
2906
3573
  return classifyReplyAuthError(findLastAssistantReplyFor(messages, userMessageId));
2907
3574
  }
3575
+ function isProviderModelNotFoundError(reason) {
3576
+ return reason.includes("Model not found:");
3577
+ }
2908
3578
  function findLatestSubagentAuthOutcome(messages, sinceMs) {
2909
3579
  if (!messages || messages.length === 0) return null;
2910
3580
  for (let i = messages.length - 1; i >= 0; i--) {
2911
3581
  const message = messages[i];
2912
- if (roleOf(message) !== "assistant") continue;
3582
+ if (roleOf2(message) !== "assistant") continue;
2913
3583
  const created = createdOf(message);
2914
3584
  if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
2915
3585
  const failure = classifyReplyAuthError(message);
@@ -2941,25 +3611,25 @@ function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProvi
2941
3611
  function hasRunningAssistantExcept(messages, exceptUserMessageId) {
2942
3612
  if (!messages || messages.length === 0) return false;
2943
3613
  return messages.some(
2944
- (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
3614
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) !== exceptUserMessageId && isAssistantInFlight(m)
2945
3615
  );
2946
3616
  }
2947
3617
  function hasLaterSiblingTurnStarted(messages, userMessageId, siblingUserMessageIds) {
2948
3618
  if (!messages || messages.length === 0) return false;
2949
- const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3619
+ const userIndex = messages.findIndex((message) => idOf2(message) === userMessageId);
2950
3620
  if (userIndex === -1) return false;
2951
3621
  let hasLaterUser = false;
2952
3622
  let hasStartedLaterUser = false;
2953
3623
  for (let i = userIndex + 1; i < messages.length; i++) {
2954
3624
  const message = messages[i];
2955
- if (roleOf(message) !== "user") continue;
3625
+ if (roleOf2(message) !== "user") continue;
2956
3626
  hasLaterUser = true;
2957
- const laterUserMessageId = idOf(message);
3627
+ const laterUserMessageId = idOf2(message);
2958
3628
  if (laterUserMessageId === void 0 || !siblingUserMessageIds.has(laterUserMessageId)) {
2959
3629
  return false;
2960
3630
  }
2961
3631
  if (messages.some(
2962
- (candidate) => roleOf(candidate) === "assistant" && parentIdOf(candidate) === laterUserMessageId
3632
+ (candidate) => roleOf2(candidate) === "assistant" && parentIdOf2(candidate) === laterUserMessageId
2963
3633
  )) {
2964
3634
  hasStartedLaterUser = true;
2965
3635
  }
@@ -3113,23 +3783,49 @@ async function readSessionErrorStream(port, options, client) {
3113
3783
  if (reader) void reader.cancel().catch(() => void 0);
3114
3784
  }
3115
3785
  }
3116
- async function reloadProviderCache(port, client) {
3117
- if (client?.version === "v2") return;
3786
+ async function disposeAndReloadProviderConfig(port, client) {
3787
+ if (client?.version === "v2") return { outcome: "skipped_v2" };
3118
3788
  try {
3119
- const res = await requestWithClient(port, client, "/config", {
3120
- method: "PATCH",
3121
- headers: { "Content-Type": "application/json" },
3122
- body: JSON.stringify({})
3123
- });
3789
+ const res = await requestWithClient(
3790
+ port,
3791
+ client,
3792
+ "/config",
3793
+ {
3794
+ method: "PATCH",
3795
+ headers: { "Content-Type": "application/json" },
3796
+ body: JSON.stringify({})
3797
+ },
3798
+ { allowStatuses: ALL_HTTP_STATUSES }
3799
+ );
3124
3800
  if (!res.ok) {
3125
3801
  console.error(
3126
- `[reloadProviderCache] PATCH /config returned HTTP ${res.status} (port ${port})`
3802
+ `[disposeAndReloadProviderConfig] PATCH /config returned HTTP ${res.status} (port ${port})`
3127
3803
  );
3804
+ return { outcome: "failed", status: res.status, detail: null };
3128
3805
  }
3806
+ return { outcome: "reloaded" };
3129
3807
  } catch (err) {
3130
3808
  console.error(
3131
- `[reloadProviderCache] PATCH /config failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
3809
+ `[disposeAndReloadProviderConfig] PATCH /config failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
3132
3810
  );
3811
+ return {
3812
+ outcome: "failed",
3813
+ status: null,
3814
+ detail: err instanceof Error ? err.message : String(err)
3815
+ };
3816
+ }
3817
+ }
3818
+ async function probeOpenCodeRequestReadiness(port, client) {
3819
+ try {
3820
+ const res = await requestWithClient(port, client, "/config", void 0, {
3821
+ allowStatuses: ALL_HTTP_STATUSES
3822
+ });
3823
+ const body = await res.text();
3824
+ return { ok: res.ok, status: res.status, bodyEmpty: body.trim().length === 0, detail: null };
3825
+ } catch (err) {
3826
+ const detail = err instanceof Error ? err.message : String(err);
3827
+ console.error(`[probeOpenCodeRequestReadiness] GET /config failed (port ${port}): ${detail}`);
3828
+ return { ok: false, status: null, bodyEmpty: null, detail };
3133
3829
  }
3134
3830
  }
3135
3831
 
@@ -3435,7 +4131,18 @@ async function deleteV2Session(client, sessionId) {
3435
4131
  { method: "DELETE" },
3436
4132
  { allowStatuses: Array.from({ length: 500 }, (_, index) => index + 100) }
3437
4133
  );
3438
- return { ok: response.status >= 200 && response.status < 300, status: response.status };
4134
+ if (response.status >= 200 && response.status < 300) {
4135
+ return { ok: true, status: response.status };
4136
+ }
4137
+ try {
4138
+ const body = await response.text();
4139
+ return body ? { ok: false, status: response.status, body } : { ok: false, status: response.status };
4140
+ } catch (error2) {
4141
+ console.error(
4142
+ `[deleteV2Session] DELETE /api/session/${sessionId} response body read failed for HTTP ${response.status}: ${error2 instanceof Error ? error2.message : String(error2)}`
4143
+ );
4144
+ return { ok: false, status: response.status };
4145
+ }
3439
4146
  } catch (error2) {
3440
4147
  console.error(
3441
4148
  `[deleteV2Session] DELETE /api/session/${sessionId} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
@@ -3611,12 +4318,12 @@ async function readV2SessionErrorStream(client, options) {
3611
4318
 
3612
4319
  // src/lib/opencode/credential-import-v2.ts
3613
4320
  import { readFileSync as readFileSync4 } from "node:fs";
3614
- import { join as join4 } from "node:path";
4321
+ import { join as join5 } from "node:path";
3615
4322
 
3616
4323
  // src/lib/openai-usage.ts
3617
4324
  import { readFileSync as readFileSync3 } from "node:fs";
3618
4325
  import { homedir as homedir3 } from "node:os";
3619
- import { join as join3 } from "node:path";
4326
+ import { join as join4 } from "node:path";
3620
4327
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3621
4328
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3622
4329
  var OpenAiUsageError = class extends Error {
@@ -3630,7 +4337,7 @@ function isLocalCredentialProblem2(err) {
3630
4337
  }
3631
4338
  function readOpenCodeChatGptCredentials() {
3632
4339
  try {
3633
- const raw = readFileSync3(join3(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4340
+ const raw = readFileSync3(join4(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3634
4341
  let parsed;
3635
4342
  try {
3636
4343
  parsed = JSON.parse(raw);
@@ -3856,13 +4563,13 @@ var INTEGRATION_LIST_RETRY_DELAY_MS = 1e3;
3856
4563
  function isRecord3(value) {
3857
4564
  return typeof value === "object" && value !== null && !Array.isArray(value);
3858
4565
  }
3859
- var sleep2 = (milliseconds) => new Promise((resolve4) => setTimeout(resolve4, milliseconds));
4566
+ var sleep2 = (milliseconds) => new Promise((resolve5) => setTimeout(resolve5, milliseconds));
3860
4567
  function emptyResult(unsupported = [], outcome = "completed") {
3861
4568
  return { outcome, imported: [], alreadyConnected: [], unsupported, failed: [] };
3862
4569
  }
3863
4570
  function readOpenCodeCredentials(homeDir, log3) {
3864
4571
  try {
3865
- const raw = readFileSync4(join4(homeDir, ...OPENCODE_AUTH_SEGMENTS), "utf8");
4572
+ const raw = readFileSync4(join5(homeDir, ...OPENCODE_AUTH_SEGMENTS), "utf8");
3866
4573
  let parsed;
3867
4574
  try {
3868
4575
  parsed = JSON.parse(raw);
@@ -4064,7 +4771,7 @@ async function importOpenCodeV2Credentials({
4064
4771
  import { spawn as spawn2 } from "node:child_process";
4065
4772
  import { mkdirSync, statSync as statSync3, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
4066
4773
  import { homedir as homedir4 } from "node:os";
4067
- import { dirname as dirname3, resolve as resolvePath } from "node:path";
4774
+ import { dirname as dirname4, resolve as resolvePath } from "node:path";
4068
4775
 
4069
4776
  // src/lib/runner-synchroniser.ts
4070
4777
  import { spawn } from "node:child_process";
@@ -4074,7 +4781,7 @@ function appendError(stderr, error2) {
4074
4781
  ${message}`;
4075
4782
  }
4076
4783
  function runSynchroniser(args, opts) {
4077
- return new Promise((resolve4) => {
4784
+ return new Promise((resolve5) => {
4078
4785
  let child;
4079
4786
  let stdout = "";
4080
4787
  let stderr = "";
@@ -4088,7 +4795,7 @@ function runSynchroniser(args, opts) {
4088
4795
  if (timer.handle) clearTimeout(timer.handle);
4089
4796
  if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
4090
4797
  if (spawnListener) child.removeListener("spawn", spawnListener);
4091
- resolve4(result);
4798
+ resolve5(result);
4092
4799
  };
4093
4800
  try {
4094
4801
  child = spawn("runner-synchroniser", args, {
@@ -4144,9 +4851,12 @@ function runSynchroniser(args, opts) {
4144
4851
 
4145
4852
  // src/lib/opencode/session-db-boot.ts
4146
4853
  var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
4147
- var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
4854
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 36e4;
4148
4855
  var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
4149
- var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
4856
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 40;
4857
+ var SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS = 30;
4858
+ var SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS = 120;
4859
+ var SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS = 90;
4150
4860
  function commandError(result) {
4151
4861
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
4152
4862
  }
@@ -4189,7 +4899,7 @@ function clearMarker(options) {
4189
4899
  function markNoReplicate(options, message) {
4190
4900
  if (options.noReplicateMarker) {
4191
4901
  try {
4192
- mkdirSync(dirname3(options.noReplicateMarker), { recursive: true });
4902
+ mkdirSync(dirname4(options.noReplicateMarker), { recursive: true });
4193
4903
  writeFileSync(options.noReplicateMarker, "");
4194
4904
  } catch (error2) {
4195
4905
  options.log(
@@ -4248,7 +4958,7 @@ function parseSynchroniserEnv(stdout) {
4248
4958
  return values;
4249
4959
  }
4250
4960
  function runCommand(command, args, options) {
4251
- return new Promise((resolve4) => {
4961
+ return new Promise((resolve5) => {
4252
4962
  let child;
4253
4963
  let stdout = "";
4254
4964
  let stderr = "";
@@ -4257,7 +4967,7 @@ function runCommand(command, args, options) {
4257
4967
  if (settled) return;
4258
4968
  settled = true;
4259
4969
  if (timer) clearTimeout(timer);
4260
- resolve4(result);
4970
+ resolve5(result);
4261
4971
  };
4262
4972
  try {
4263
4973
  child = spawn2(command, args, {
@@ -4265,7 +4975,7 @@ function runCommand(command, args, options) {
4265
4975
  stdio: ["ignore", "pipe", "pipe"]
4266
4976
  });
4267
4977
  } catch (error2) {
4268
- resolve4({
4978
+ resolve5({
4269
4979
  code: null,
4270
4980
  stdout,
4271
4981
  stderr: error2 instanceof Error ? error2.message : String(error2),
@@ -4344,7 +5054,7 @@ async function ensureLitestreamConfig(options, env) {
4344
5054
  return null;
4345
5055
  }
4346
5056
  try {
4347
- mkdirSync(dirname3(configPath), { recursive: true });
5057
+ mkdirSync(dirname4(configPath), { recursive: true });
4348
5058
  writeFileSync(configPath, rendered.stdout);
4349
5059
  } catch (error2) {
4350
5060
  options.log(
@@ -4457,20 +5167,37 @@ async function verifySessionDb(options, configPath, env) {
4457
5167
  timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
4458
5168
  env: {
4459
5169
  ...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.
5170
+ // The synchroniser reads all four values in SECONDS.
4462
5171
  EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
4463
5172
  SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
5173
+ ),
5174
+ EVIDENT_SESSION_DB_RESTORE_POINTS_TIMEOUT_SECONDS: String(
5175
+ SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS
5176
+ ),
5177
+ EVIDENT_SESSION_DB_RESTORE_CANDIDATE_TIMEOUT_SECONDS: String(
5178
+ SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS
5179
+ ),
5180
+ EVIDENT_SESSION_DB_DISPOSAL_BUDGET_SECONDS: String(
5181
+ SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS
4464
5182
  )
4465
5183
  }
4466
5184
  });
4467
5185
  logSynchroniserDiagnostics(result, options);
4468
5186
  if (result.timedOut || result.code === 124 || result.code === 137) {
5187
+ if (result.stderr.includes("SESSION-DB-INTEGRITY-FAILED")) {
5188
+ const reason = result.stderr.includes("SESSION-DB-REPLICA-SIZE") ? "verification_timeout_during_separation" : "verification_timeout_before_separation";
5189
+ options.log(
5190
+ `SESSION-DB-VERIFY-TIMEOUT: verification did not finish within its ${SESSION_DB_VERIFY_TIMEOUT_MS}ms deadline; refusing to start with the restored opencode.db (${reason})`,
5191
+ "error"
5192
+ );
5193
+ reportRecord("verify", "session_db_boot_refused", reason, null, options);
5194
+ return { verifyFatal: true, verifyFatalReason: reason };
5195
+ }
4469
5196
  options.log(
4470
5197
  `SESSION-DB-VERIFY-TIMEOUT: verification did not finish within its ${SESSION_DB_VERIFY_TIMEOUT_MS}ms deadline; continuing with the restored opencode.db as-is, unverified`,
4471
5198
  "warn"
4472
5199
  );
4473
- return false;
5200
+ return { verifyFatal: false };
4474
5201
  }
4475
5202
  if (result.code === 34) {
4476
5203
  reportRecord(
@@ -4480,14 +5207,14 @@ async function verifySessionDb(options, configPath, env) {
4480
5207
  null,
4481
5208
  options
4482
5209
  );
4483
- return true;
5210
+ return { verifyFatal: true };
4484
5211
  }
4485
5212
  if (result.code === 33) {
4486
5213
  options.log(
4487
5214
  "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
4488
5215
  "warn"
4489
5216
  );
4490
- return false;
5217
+ return { verifyFatal: false };
4491
5218
  }
4492
5219
  if (result.code !== 0) {
4493
5220
  options.log(
@@ -4495,13 +5222,13 @@ async function verifySessionDb(options, configPath, env) {
4495
5222
  "warn"
4496
5223
  );
4497
5224
  }
4498
- return false;
5225
+ return { verifyFatal: false };
4499
5226
  }
4500
5227
  options.log(
4501
5228
  "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
4502
5229
  "debug"
4503
5230
  );
4504
- return false;
5231
+ return { verifyFatal: false };
4505
5232
  }
4506
5233
  function fileExists(path) {
4507
5234
  try {
@@ -4588,13 +5315,13 @@ async function restoreAndVerifySessionDb(options) {
4588
5315
  if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
4589
5316
  return { verifyFatal: false };
4590
5317
  }
4591
- return { verifyFatal: await verifySessionDb(options, configPath, env) };
5318
+ return verifySessionDb(options, configPath, env);
4592
5319
  }
4593
5320
 
4594
5321
  // src/lib/opencode/session-db-provenance.ts
4595
5322
  import { createRequire } from "node:module";
4596
5323
  import { mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "node:fs";
4597
- import { dirname as dirname4, join as join5 } from "node:path";
5324
+ import { dirname as dirname5, join as join6 } from "node:path";
4598
5325
  var require2 = createRequire(import.meta.url);
4599
5326
  function readSessionDbMigrationIds(dbPath) {
4600
5327
  let db;
@@ -4631,7 +5358,7 @@ function readSessionDbMigrationIds(dbPath) {
4631
5358
  }
4632
5359
  function sessionDbProvenanceStatePath(homeDir, env) {
4633
5360
  const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
4634
- return override || join5(homeDir, ".local", "state", "evident", "session-db-provenance.json");
5361
+ return override || join6(homeDir, ".local", "state", "evident", "session-db-provenance.json");
4635
5362
  }
4636
5363
  function loadSessionDbProvenanceState(path) {
4637
5364
  let value;
@@ -4660,7 +5387,7 @@ function loadSessionDbProvenanceState(path) {
4660
5387
  }
4661
5388
  function saveSessionDbProvenanceState(path, state) {
4662
5389
  try {
4663
- mkdirSync2(dirname4(path), { recursive: true });
5390
+ mkdirSync2(dirname5(path), { recursive: true });
4664
5391
  writeFileSync2(path, `${JSON.stringify(state, null, 2)}
4665
5392
  `, "utf8");
4666
5393
  } catch (error2) {
@@ -4751,7 +5478,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
4751
5478
  if (child.exitCode !== null || child.signalCode !== null) {
4752
5479
  return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
4753
5480
  }
4754
- return new Promise((resolve4, reject) => {
5481
+ return new Promise((resolve5, reject) => {
4755
5482
  let forced = false;
4756
5483
  let settled = false;
4757
5484
  const timer = setTimeout(() => {
@@ -4771,7 +5498,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
4771
5498
  settled = true;
4772
5499
  clearTimeout(timer);
4773
5500
  child.removeListener("exit", onExit);
4774
- resolve4(result);
5501
+ resolve5(result);
4775
5502
  };
4776
5503
  const fail = (error2) => {
4777
5504
  if (settled) return;
@@ -5247,7 +5974,7 @@ function selectSessionsToDelete(sessions, opts) {
5247
5974
  );
5248
5975
  }
5249
5976
  var DEFAULT_INTERVAL = "1h";
5250
- function resolve(flag, envValue, fallback) {
5977
+ function resolve2(flag, envValue, fallback) {
5251
5978
  return flag ?? envValue ?? fallback;
5252
5979
  }
5253
5980
  function parseMaxCount(input) {
@@ -5263,9 +5990,9 @@ function parseMaxCount(input) {
5263
5990
  }
5264
5991
  function resolveSessionCleanupConfig(flags, env = process.env) {
5265
5992
  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(
5993
+ const maxAgeRaw = resolve2(flags.maxAge, env.EVIDENT_SESSION_CLEANUP_MAX_AGE);
5994
+ const maxCountRaw = resolve2(flags.maxCount, env.EVIDENT_SESSION_CLEANUP_MAX_COUNT);
5995
+ const intervalRaw = resolve2(
5269
5996
  flags.interval,
5270
5997
  env.EVIDENT_SESSION_CLEANUP_INTERVAL,
5271
5998
  DEFAULT_INTERVAL
@@ -5305,10 +6032,10 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
5305
6032
 
5306
6033
  // src/lib/opencode/session-db-size.ts
5307
6034
  import { statSync as statSync4 } from "node:fs";
5308
- import { join as join6 } from "node:path";
6035
+ import { join as join7 } from "node:path";
5309
6036
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
5310
6037
  function statSessionDbBytes(homeDir) {
5311
- const dbPath = join6(homeDir, ".local", "share", "opencode", "opencode.db");
6038
+ const dbPath = join7(homeDir, ".local", "share", "opencode", "opencode.db");
5312
6039
  try {
5313
6040
  return statSync4(dbPath).size;
5314
6041
  } catch (err) {
@@ -5338,12 +6065,12 @@ function buildSessionStoreSizeWarning(input) {
5338
6065
  // src/lib/opencode/log-tail.ts
5339
6066
  import { statSync as statSync5 } from "node:fs";
5340
6067
  import { homedir as homedir5 } from "node:os";
5341
- import { join as join7 } from "node:path";
6068
+ import { join as join8 } from "node:path";
5342
6069
  import { open as open2, stat } from "node:fs/promises";
5343
6070
  var DEFAULT_POLL_INTERVAL_MS = 1e3;
5344
6071
  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");
6072
+ const dataDir = env.XDG_DATA_HOME || join8(homeDir, ".local", "share");
6073
+ return join8(dataDir, "opencode", "log", "opencode.log");
5347
6074
  }
5348
6075
  function isEnoent(error2) {
5349
6076
  return error2?.code === "ENOENT";
@@ -5498,8 +6225,8 @@ var StreamForwarder = class {
5498
6225
  pauseStream(sid, stream) {
5499
6226
  if (!stream.paused) {
5500
6227
  stream.paused = true;
5501
- stream.pausePromise = new Promise((resolve4) => {
5502
- stream.resume = resolve4;
6228
+ stream.pausePromise = new Promise((resolve5) => {
6229
+ stream.resume = resolve5;
5503
6230
  });
5504
6231
  }
5505
6232
  if (stream.pauseTimer !== null) clearTimeout(stream.pauseTimer);
@@ -5566,12 +6293,12 @@ var StreamForwarder = class {
5566
6293
  let endBody;
5567
6294
  if (has_body) {
5568
6295
  const chunks = [];
5569
- bodyPromise = new Promise((resolve4) => {
6296
+ bodyPromise = new Promise((resolve5) => {
5570
6297
  pushBody = (buf) => {
5571
6298
  chunks.push(buf);
5572
6299
  };
5573
6300
  endBody = () => {
5574
- resolve4(Buffer.concat(chunks));
6301
+ resolve5(Buffer.concat(chunks));
5575
6302
  };
5576
6303
  });
5577
6304
  }
@@ -5742,7 +6469,7 @@ function connectTunnel(options) {
5742
6469
  } = options;
5743
6470
  const tunnelUrl = getTunnelUrlConfig();
5744
6471
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
5745
- return new Promise((resolve4, reject) => {
6472
+ return new Promise((resolve5, reject) => {
5746
6473
  const ws = new WebSocket2(url, {
5747
6474
  headers: {
5748
6475
  Authorization: authHeader,
@@ -5851,7 +6578,7 @@ function connectTunnel(options) {
5851
6578
  forwarder.setBinaryFramesSupported(message.binary_frames === true);
5852
6579
  const connectedAgentId = message.agent_id ?? agentId;
5853
6580
  onConnected?.(connectedAgentId);
5854
- resolve4({
6581
+ resolve5({
5855
6582
  ws,
5856
6583
  close: () => ws.close(1e3, "CLI shutdown")
5857
6584
  });
@@ -6369,6 +7096,7 @@ function createSessionStoreHealth(options = {}) {
6369
7096
  const stalenessMs = Math.max(0, options.stalenessMs ?? SESSION_STORE_HEALTH_STALENESS_MS);
6370
7097
  let lastUnwritableAt = null;
6371
7098
  let lastWritableAt = null;
7099
+ const causes = /* @__PURE__ */ new Map();
6372
7100
  return {
6373
7101
  observeUnwritable: () => {
6374
7102
  lastUnwritableAt = now();
@@ -6380,6 +7108,23 @@ function createSessionStoreHealth(options = {}) {
6380
7108
  if (lastUnwritableAt === null) return false;
6381
7109
  if (lastWritableAt !== null && lastWritableAt >= lastUnwritableAt) return false;
6382
7110
  return now() - lastUnwritableAt < stalenessMs;
7111
+ },
7112
+ observeCause: (sessionId, cause) => {
7113
+ if (sessionId === null) return;
7114
+ causes.delete(sessionId);
7115
+ causes.set(sessionId, { cause, observedAt: now() });
7116
+ while (causes.size > 256) {
7117
+ const oldest = causes.keys().next().value;
7118
+ if (typeof oldest !== "string") break;
7119
+ causes.delete(oldest);
7120
+ }
7121
+ },
7122
+ readCause: (sessionId) => {
7123
+ const observed = causes.get(sessionId);
7124
+ if (!observed) return null;
7125
+ causes.delete(sessionId);
7126
+ if (now() - observed.observedAt >= stalenessMs) return null;
7127
+ return observed.cause;
6383
7128
  }
6384
7129
  };
6385
7130
  }
@@ -6388,12 +7133,12 @@ function createSessionStoreHealth(options = {}) {
6388
7133
  import { homedir as homedir6 } from "node:os";
6389
7134
 
6390
7135
  // src/lib/runner-file-sync.ts
6391
- import { join as join9 } from "node:path";
7136
+ import { join as join10 } from "node:path";
6392
7137
 
6393
7138
  // src/lib/file-push.ts
6394
7139
  import { randomUUID } from "node:crypto";
6395
7140
  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";
7141
+ import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute2, join as join9, relative as relative2, resolve as resolve3, sep as sep2 } from "node:path";
6397
7142
  var FILE_MODE = 384;
6398
7143
  var DIRECTORY_MODE = 448;
6399
7144
  async function writePushedFile(request2) {
@@ -6424,9 +7169,9 @@ async function writePushedFile(request2) {
6424
7169
  }
6425
7170
  try {
6426
7171
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
6427
- dirname5(candidate)
7172
+ dirname6(candidate)
6428
7173
  );
6429
- const realTarget = join8(existingAncestor, ...missingSegments, basename(candidate));
7174
+ const realTarget = join9(existingAncestor, ...missingSegments, basename2(candidate));
6430
7175
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
6431
7176
  if (allowedDirectory === null) {
6432
7177
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -6436,8 +7181,8 @@ async function writePushedFile(request2) {
6436
7181
  }
6437
7182
  if (missingSegments.length > 0) {
6438
7183
  await createMissingDirectories(existingAncestor, missingSegments);
6439
- const realParent = await realpath(dirname5(realTarget));
6440
- if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
7184
+ const realParent = await realpath(dirname6(realTarget));
7185
+ if (realParent !== dirname6(realTarget) || !contains(allowedDirectory, realTarget)) {
6441
7186
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
6442
7187
  path: realTarget,
6443
7188
  bytes,
@@ -6462,15 +7207,15 @@ function expandAndValidate(requestedPath, homeDir) {
6462
7207
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
6463
7208
  return null;
6464
7209
  }
6465
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
7210
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join9(homeDir, requestedPath.slice(2)) : requestedPath;
6466
7211
  if (expanded.split(/[/\\]/).includes("..")) {
6467
7212
  return null;
6468
7213
  }
6469
- if (!isAbsolute(expanded)) {
7214
+ if (!isAbsolute2(expanded)) {
6470
7215
  return null;
6471
7216
  }
6472
- const candidate = resolve2(expanded);
6473
- const name = basename(candidate);
7217
+ const candidate = resolve3(expanded);
7218
+ const name = basename2(candidate);
6474
7219
  return name === "" || name === "." || name === ".." ? null : candidate;
6475
7220
  }
6476
7221
  async function resolveNearestExistingAncestor(directory) {
@@ -6480,18 +7225,18 @@ async function resolveNearestExistingAncestor(directory) {
6480
7225
  try {
6481
7226
  return { existingAncestor: await realpath(current), missingSegments };
6482
7227
  } catch (err) {
6483
- const parent = dirname5(current);
7228
+ const parent = dirname6(current);
6484
7229
  if (err.code !== "ENOENT" || parent === current) {
6485
7230
  throw err;
6486
7231
  }
6487
- missingSegments.unshift(basename(current));
7232
+ missingSegments.unshift(basename2(current));
6488
7233
  current = parent;
6489
7234
  }
6490
7235
  }
6491
7236
  }
6492
7237
  async function findContainingAllowedDirectory(allowedDirectories, realTarget) {
6493
7238
  for (const directory of allowedDirectories) {
6494
- if (!isAbsolute(directory)) {
7239
+ if (!isAbsolute2(directory)) {
6495
7240
  log("warn", "file_push_allowed_directory_skipped", { directory, reason: "not_absolute" });
6496
7241
  continue;
6497
7242
  }
@@ -6529,19 +7274,19 @@ async function realpathCreatingIfMissing(directory) {
6529
7274
  }
6530
7275
  }
6531
7276
  function contains(realDirectory, realTarget) {
6532
- const rel = relative(realDirectory, realTarget);
6533
- return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
7277
+ const rel = relative2(realDirectory, realTarget);
7278
+ return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep2}`) && !isAbsolute2(rel);
6534
7279
  }
6535
7280
  async function createMissingDirectories(existingAncestor, missingSegments) {
6536
7281
  let current = existingAncestor;
6537
7282
  for (const segment of missingSegments) {
6538
- current = join8(current, segment);
7283
+ current = join9(current, segment);
6539
7284
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
6540
7285
  await chmod(current, DIRECTORY_MODE);
6541
7286
  }
6542
7287
  }
6543
7288
  async function writeAtomically(realTarget, content) {
6544
- const temporaryPath = join8(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
7289
+ const temporaryPath = join9(dirname6(realTarget), `.evident-push-${randomUUID()}.tmp`);
6545
7290
  let handle;
6546
7291
  try {
6547
7292
  handle = await open3(temporaryPath, "wx", FILE_MODE);
@@ -6671,12 +7416,12 @@ var NOT_APPLIED = {
6671
7416
  opencodeAuthApplied: false
6672
7417
  };
6673
7418
  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);
7419
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join10(homeDir, requestedPath.slice(2)) : requestedPath;
7420
+ return expanded === join10(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
6676
7421
  }
6677
7422
  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);
7423
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join10(homeDir, requestedPath.slice(2)) : requestedPath;
7424
+ return expanded === join10(homeDir, ...OPENCODE_AUTH_SEGMENTS);
6680
7425
  }
6681
7426
  async function applyOne(options, file) {
6682
7427
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -6820,6 +7565,11 @@ function messageIdOf(m) {
6820
7565
  const infoId = m.info?.id;
6821
7566
  return typeof infoId === "string" ? infoId : void 0;
6822
7567
  }
7568
+ function composeAbandonmentNote(finish, replyShape, outcome) {
7569
+ const base = `OpenCode ended this turn after a tool call (finish="${finish ?? "(unknown)"}") ` + (outcome === "without-answer" ? "without writing an answer, and confirmed it is no longer working on this conversation. Evident is reporting the run as failed so the failure and its reason are visible in failed runs." : "after writing only part of an answer, and confirmed it is no longer working on this conversation. Evident delivered what was written and is reporting the run as finished.");
7570
+ if (replyShape === null) return base;
7571
+ return `${base} ${replyShape.lastToolName === null ? "No tool call was recorded" : `The last tool call was "${replyShape.lastToolName}" with status "${replyShape.lastToolStatus ?? "(unknown)"}"`}; the reply's final part types were [${replyShape.partTypes.join(", ")}]${replyShape.droppedPartCount > 0 ? ` (${replyShape.droppedPartCount} earlier part types omitted)` : ""}.`;
7572
+ }
6823
7573
  function cleanImageMime(contentType) {
6824
7574
  if (!contentType) return null;
6825
7575
  const media = contentType.split(";")[0].trim().toLowerCase();
@@ -6840,6 +7590,10 @@ var SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS = 2e3;
6840
7590
  var SESSION_ERROR_STREAM_HEALTHY_MS = 5e3;
6841
7591
  var SESSION_ERROR_BUFFER_TTL_MS = SESSION_ERROR_STREAM_HEALTHY_MS;
6842
7592
  var MAX_BUFFERED_SESSION_ERRORS = 256;
7593
+ var SESSION_ERROR_RETENTION_TTL_MS = 6e4;
7594
+ var SESSION_STORE_UNWRITABLE_ERROR = "The runner session database could not be written, so this turn did not finish. Nothing was lost from the queue. Clear disk space and retry.";
7595
+ var SESSION_ERROR_RETAINED_DETAIL = "Runner-observed OpenCode session error: ";
7596
+ var SESSION_ERROR_OBSERVED_CAUSE = "Runner-observed OpenCode session-store cause: ";
6843
7597
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
6844
7598
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
6845
7599
  var DRIVER_SESSION_ENUMERATION_LIMIT = 20;
@@ -6962,12 +7716,15 @@ var ChannelDriver = class _ChannelDriver {
6962
7716
  /**
6963
7717
  * Per-conversation throttle state for the #183 recurrence warning (#1618
6964
7718
  * WI-4) — see `reportWedgedConversation`'s doc comment for why this exists.
6965
- * `firstWedgedAt` anchors `stuck_for_ms`; `lastWarnedAt` throttles both the
6966
- * log line and the `dispatch_wedged` signal to at most once per
6967
- * `wedgeWarningIntervalMs`; `consecutiveTicks` is reported in the log text
6968
- * so the operator sees magnitude, not repetition. Cleared the moment the
6969
- * conversation dispatches anything (a fresh wedge, if it recurs, is a new
6970
- * incident). Bounded FIFO, mirroring `supersededSessions`
7719
+ * `firstWedgedAt` anchors `stuck_for_ms`; `warnAnchorAt` is the throttle
7720
+ * anchor from first detection through the first warning, then from each
7721
+ * warning onward; `consecutiveTicks` is reported in the log text so the
7722
+ * operator sees magnitude, not repetition. Cleared the moment the
7723
+ * conversation dispatches anything, or when it drops off the pending list
7724
+ * (`runDrain`'s prune, the dominant real-world path — a wedge normally ends
7725
+ * with its message completing, not a fresh dispatch on the same
7726
+ * conversation) — either way a fresh wedge, if it recurs, is a new
7727
+ * incident. Bounded FIFO, mirroring `supersededSessions`
6971
7728
  * (`MAX_WEDGED_CONVERSATIONS`).
6972
7729
  */
6973
7730
  wedgeWarnings = /* @__PURE__ */ new Map();
@@ -6997,6 +7754,7 @@ var ChannelDriver = class _ChannelDriver {
6997
7754
  * with a short TTL so an unmatched session cannot retain an event indefinitely.
6998
7755
  */
6999
7756
  bufferedSessionErrors = /* @__PURE__ */ new Map();
7757
+ retainedSessionErrors = /* @__PURE__ */ new Map();
7000
7758
  /**
7001
7759
  * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
7002
7760
  * dispatched and are still in-flight. A message in this set is never
@@ -7029,10 +7787,18 @@ var ChannelDriver = class _ChannelDriver {
7029
7787
  * row is `processing` or `pending`; clear it only when absent from both lists.
7030
7788
  */
7031
7789
  untrackableAck = /* @__PURE__ */ new Map();
7790
+ /** Session binding used by the first untrackable-ack report, for retries. */
7791
+ untrackableAckSessionIds = /* @__PURE__ */ new Map();
7032
7792
  /** Messages whose untrackable-ack failure signal was emitted while fenced. */
7033
7793
  untrackableAckSignalled = /* @__PURE__ */ new Set();
7034
7794
  /** Messages whose first untrackable-ack fence warning was already logged. */
7035
7795
  untrackableAckWarned = /* @__PURE__ */ new Set();
7796
+ /**
7797
+ * Accepted V1 prompts whose assigned ids were not read back; these rows are
7798
+ * re-read instead of POSTed again. A null snapshot cannot safely exclude an older
7799
+ * identical-text turn, so it never produces a correlation.
7800
+ */
7801
+ awaitingCorrelation = /* @__PURE__ */ new Map();
7036
7802
  /** Pending rows seen in the current drain, used to retain terminal dispatch fences. */
7037
7803
  pendingMessageIds = /* @__PURE__ */ new Set();
7038
7804
  /**
@@ -7118,21 +7884,6 @@ var ChannelDriver = class _ChannelDriver {
7118
7884
  * re-signal on every one of the 15h of re-dispatch attempts #1110 made.
7119
7885
  */
7120
7886
  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
7887
  /**
7137
7888
  * Consecutive give-ups for a message whose OpenCode user row remains queued.
7138
7889
  * The session id scopes the count: a fresh session is a new attempt after a
@@ -7150,8 +7901,8 @@ var ChannelDriver = class _ChannelDriver {
7150
7901
  * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
7151
7902
  * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
7152
7903
  * 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).
7904
+ * re-entry) OR on a failed/unresolved dispatch (the accepted prompt is handed to
7905
+ * the terminal-report fence and is never sent again).
7155
7906
  */
7156
7907
  awaitingReadopt = /* @__PURE__ */ new Set();
7157
7908
  /**
@@ -7203,6 +7954,7 @@ var ChannelDriver = class _ChannelDriver {
7203
7954
  subagentInvocationPrefetches = /* @__PURE__ */ new Map();
7204
7955
  /** Serialises drains so a reconnect during a drain doesn't double-process. */
7205
7956
  draining = false;
7957
+ pendingProviderRegistryRepair = null;
7206
7958
  /**
7207
7959
  * Serialises runner-file syncs (#559) so the ~2s poll tick and a concurrent
7208
7960
  * drain ping don't download, write and ack the same file twice.
@@ -7388,8 +8140,33 @@ var ChannelDriver = class _ChannelDriver {
7388
8140
  async runDrain() {
7389
8141
  let dispatched = 0;
7390
8142
  try {
8143
+ const pendingRepair = this.pendingProviderRegistryRepair;
8144
+ if (pendingRepair && this.protectedSessionIds().size === 0) {
8145
+ try {
8146
+ await this.issueProviderRegistryReload(
8147
+ pendingRepair.conversationId,
8148
+ pendingRepair.messageId
8149
+ );
8150
+ } catch (err) {
8151
+ this.log({
8152
+ level: "error",
8153
+ message: `Provider registry repair adoption failed for message ${pendingRepair.messageId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`,
8154
+ conversation_id: pendingRepair.conversationId,
8155
+ message_id: pendingRepair.messageId
8156
+ });
8157
+ }
8158
+ return 0;
8159
+ }
7391
8160
  this.pendingMessageIds.clear();
7392
8161
  const conversations = await this.getPendingConversations();
8162
+ if (this.wedgeWarnings.size > 0) {
8163
+ const pendingConversationIds = new Set(conversations.map(({ id }) => id));
8164
+ for (const conversationId of this.wedgeWarnings.keys()) {
8165
+ if (!pendingConversationIds.has(conversationId)) {
8166
+ this.wedgeWarnings.delete(conversationId);
8167
+ }
8168
+ }
8169
+ }
7393
8170
  if (this.recycleRequestedFlag) {
7394
8171
  this.stop();
7395
8172
  }
@@ -7469,6 +8246,49 @@ var ChannelDriver = class _ChannelDriver {
7469
8246
  }
7470
8247
  return ids;
7471
8248
  }
8249
+ /** Find an accepted prompt's user row without adopting an older or concurrent turn. */
8250
+ findAwaitingCorrelationId(entry, sessionId, messages) {
8251
+ if (entry.sessionId !== sessionId || entry.knownUserIds === null) return null;
8252
+ const excludeIds = new Set(entry.knownUserIds);
8253
+ for (const inFlight of this.watchers.get(entry.sessionId)?.inFlight.values() ?? []) {
8254
+ excludeIds.add(inFlight.opencodeMessageId);
8255
+ }
8256
+ return findDispatchedUserMessageId(messages, entry.content, excludeIds);
8257
+ }
8258
+ /** Hand an exhausted accepted-but-untrackable prompt to the terminal-report fence. */
8259
+ async handoffUntrackableCorrelation(conversationId, messageId, sessionId, attempts, source) {
8260
+ this.awaitingCorrelation.delete(messageId);
8261
+ this.awaitingReadopt.delete(messageId);
8262
+ this.sessions.delete(conversationId);
8263
+ this.supersede(conversationId, sessionId);
8264
+ const dispatchDescription = source === "readopt" ? "re-dispatched message" : "message";
8265
+ 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.`;
8266
+ this.untrackableAck.set(messageId, errorMessage3);
8267
+ this.untrackableAckSessionIds.set(messageId, null);
8268
+ this.log({
8269
+ level: "error",
8270
+ message: errorMessage3,
8271
+ conversation_id: conversationId,
8272
+ message_id: messageId
8273
+ });
8274
+ const reportError = await this.reportUntrackableAck(
8275
+ conversationId,
8276
+ messageId,
8277
+ null,
8278
+ errorMessage3
8279
+ );
8280
+ if (reportError !== null) {
8281
+ this.log({
8282
+ level: "warn",
8283
+ message: `markFailed PATCH for message ${messageId.slice(0, 8)} (conversation ${conversationId.slice(0, 8)}) failed: ${reportError.message}`,
8284
+ conversation_id: conversationId,
8285
+ message_id: messageId
8286
+ });
8287
+ }
8288
+ if (source === "readopt") {
8289
+ void this.postSignal(conversationId, messageId, "readopt_orphan_unsent");
8290
+ }
8291
+ }
7472
8292
  /**
7473
8293
  * File-pull work, for `run.ts`'s idle accounting (#559).
7474
8294
  *
@@ -7648,6 +8468,56 @@ var ChannelDriver = class _ChannelDriver {
7648
8468
  }
7649
8469
  break;
7650
8470
  }
8471
+ const correlation = this.awaitingCorrelation.get(message.id);
8472
+ if (correlation) {
8473
+ correlation.attempts += 1;
8474
+ let correlatedId = null;
8475
+ if (correlation.knownUserIds !== null && correlation.sessionId === sessionId) {
8476
+ const polled = await this.pollSessionMessagesForRedrive(
8477
+ conv,
8478
+ message,
8479
+ correlation.sessionId
8480
+ );
8481
+ if (polled.ok)
8482
+ correlatedId = this.findAwaitingCorrelationId(correlation, sessionId, polled.messages);
8483
+ } else if (correlation.knownUserIds === null) {
8484
+ this.log({
8485
+ level: "debug",
8486
+ message: `Message ${message.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
8487
+ conversation_id: conv.id,
8488
+ message_id: message.id
8489
+ });
8490
+ }
8491
+ if (correlatedId !== null) {
8492
+ this.awaitingCorrelation.delete(message.id);
8493
+ this.dispatchNotStartedSignalled.delete(message.id);
8494
+ this.releasedOpencodeIds.delete(message.id);
8495
+ this.dispatched.add(message.id);
8496
+ this.registerInFlight(conv, correlation.sessionId, message, correlatedId);
8497
+ dispatched += 1;
8498
+ this.log({
8499
+ level: "info",
8500
+ message: `Matched accepted message ${message.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
8501
+ conversation_id: conv.id,
8502
+ message_id: message.id
8503
+ });
8504
+ void this.postSignal(conv.id, message.id, "ack_correlated", {
8505
+ opencode_message_id: correlatedId
8506
+ });
8507
+ continue;
8508
+ }
8509
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
8510
+ await this.handoffUntrackableCorrelation(
8511
+ conv.id,
8512
+ message.id,
8513
+ correlation.sessionId,
8514
+ correlation.attempts,
8515
+ "fresh"
8516
+ );
8517
+ break;
8518
+ }
8519
+ break;
8520
+ }
7651
8521
  const effectiveOpencodeMessageId = message.opencode_message_id ?? this.releasedOpencodeIds.get(message.id)?.opencodeMessageId ?? null;
7652
8522
  if (effectiveOpencodeMessageId) {
7653
8523
  const outcome = await this.resolveRedrive(
@@ -7669,6 +8539,7 @@ var ChannelDriver = class _ChannelDriver {
7669
8539
  model: message.opencode_model ?? void 0
7670
8540
  };
7671
8541
  let opencodeMessageId;
8542
+ let preDispatchUserIds = null;
7672
8543
  try {
7673
8544
  this.log({
7674
8545
  level: "info",
@@ -7698,7 +8569,20 @@ var ChannelDriver = class _ChannelDriver {
7698
8569
  message.content,
7699
8570
  options,
7700
8571
  sendAttachments,
7701
- this.openCodeClient
8572
+ this.openCodeClient,
8573
+ {
8574
+ onKnownUserIds: (ids) => {
8575
+ preDispatchUserIds = ids;
8576
+ },
8577
+ onKnownUserIdsError: (error2) => {
8578
+ this.log({
8579
+ level: "warn",
8580
+ message: `Pre-dispatch snapshot callback failed for message ${message.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
8581
+ conversation_id: conv.id,
8582
+ message_id: message.id
8583
+ });
8584
+ }
8585
+ }
7702
8586
  )
7703
8587
  );
7704
8588
  } catch (err) {
@@ -7706,6 +8590,7 @@ var ChannelDriver = class _ChannelDriver {
7706
8590
  if (this.isV2 && err instanceof OpenCodeV2PromptAckError) {
7707
8591
  const errorMessage4 = err instanceof Error ? err.message : String(err);
7708
8592
  this.untrackableAck.set(message.id, errorMessage4);
8593
+ this.untrackableAckSessionIds.set(message.id, null);
7709
8594
  this.log({
7710
8595
  level: "error",
7711
8596
  message: `V2 prompt dispatch for message ${message.id.slice(0, 8)} failed after a positive ack with no usable id: ${errorMessage4}`,
@@ -7781,39 +8666,21 @@ var ChannelDriver = class _ChannelDriver {
7781
8666
  if (this.isV2) {
7782
8667
  throw new Error("V2 prompt dispatch completed without an acknowledged message id");
7783
8668
  }
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.`;
8669
+ this.awaitingCorrelation.set(message.id, {
8670
+ sessionId,
8671
+ content: message.content,
8672
+ attempts: 0,
8673
+ knownUserIds: preDispatchUserIds
8674
+ });
7799
8675
  this.log({
7800
- level: "error",
7801
- message: errorMessage3,
8676
+ level: "warn",
8677
+ 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
8678
  conversation_id: conv.id,
7803
8679
  message_id: message.id
7804
8680
  });
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
- });
8681
+ this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7814
8682
  break;
7815
8683
  }
7816
- this.unconfirmedDispatchFailures.delete(message.id);
7817
8684
  this.dispatchNotStartedSignalled.delete(message.id);
7818
8685
  this.releasedOpencodeIds.delete(message.id);
7819
8686
  this.dispatched.add(message.id);
@@ -7836,14 +8703,13 @@ var ChannelDriver = class _ChannelDriver {
7836
8703
  * skip-because-already-`dispatched`, the exact signature of a message stuck
7837
8704
  * acknowledged-but-never-worked.
7838
8705
  *
7839
- * Unthrottled, this fired every ~6s drain tick for as long as a wedge lasted
7840
- * (52,843 occurrences observed in one incident) — burning the GLOBAL
7841
- * 30-events/60s `runner-activity-telemetry.ts` budget that was itself
7842
- * suppressing the diagnostics needed to debug the wedge. The `warn` log (and
7843
- * the `dispatch_wedged` signal once the wedge has persisted past the same
7844
- * interval) fire at most once per `wedgeWarningIntervalMs` per conversation,
7845
- * naming the consecutive-tick count so the operator sees magnitude rather
7846
- * than repetition.
8706
+ * The first detection starts the clock and emits a local `debug` entry. The
8707
+ * `warn` log and paired `dispatch_wedged` signal fire once the wedge has
8708
+ * lasted `wedgeWarningIntervalMs`, then at most once per interval, naming the
8709
+ * consecutive-tick count so the operator sees magnitude rather than repetition.
8710
+ * A watcher stall that the loop-liveness watchdog recovers is expected and is
8711
+ * already reported by its `watcher_recovered` signal, so it does not reach an
8712
+ * operator through this warning.
7847
8713
  *
7848
8714
  * Deliberately does NOT trigger a release: WI-1's `reconcileWatchers` runs
7849
8715
  * unconditionally on this same tick and is already recovering anything it
@@ -7855,36 +8721,41 @@ var ChannelDriver = class _ChannelDriver {
7855
8721
  const existing = this.wedgeWarnings.get(conv.id);
7856
8722
  const firstWedgedAt = existing?.firstWedgedAt ?? now;
7857
8723
  const consecutiveTicks = (existing?.consecutiveTicks ?? 0) + 1;
7858
- const dueForWarn = !existing || now - existing.lastWarnedAt >= this.wedgeWarningIntervalMs;
7859
- if (!dueForWarn) {
7860
- this.wedgeWarnings.delete(conv.id);
7861
- this.wedgeWarnings.set(conv.id, {
7862
- firstWedgedAt,
7863
- lastWarnedAt: existing.lastWarnedAt,
7864
- consecutiveTicks
8724
+ const warnAnchorAt = existing?.warnAnchorAt ?? now;
8725
+ const dueForWarn = now - warnAnchorAt >= this.wedgeWarningIntervalMs;
8726
+ if (!existing) {
8727
+ this.log({
8728
+ level: "debug",
8729
+ message: `Detected a possible wedge for conversation ${conv.id.slice(0, 8)} with ${messages.length} pending message(s); it will be reported only if it outlasts the watchdog's recovery.`,
8730
+ conversation_id: conv.id
8731
+ });
8732
+ }
8733
+ let untracked = 0;
8734
+ if (dueForWarn) {
8735
+ const stuckForMs = now - firstWedgedAt;
8736
+ untracked = this.countUntrackedIds(messages);
8737
+ this.log({
8738
+ level: "warn",
8739
+ message: `Conversation ${conv.id.slice(0, 8)} has ${messages.length} pending message(s) but ALL are already marked dispatched locally (in-flight set: ${this.dispatched.size}) \u2014 none sent to OpenCode for ${consecutiveTicks} consecutive tick(s) now (${Math.round(stuckForMs / 1e3)}s stuck). ` + (untracked > 0 ? `${untracked} of these id(s) are tracked by NO watcher \u2014 the dispatched/in-flight pairing invariant is violated for this conversation, which will NOT self-heal and needs a runner restart.` : `A watcher is tracking this work; the loop-liveness watchdog is already recovering it.`),
8740
+ conversation_id: conv.id
7865
8741
  });
7866
- return;
7867
8742
  }
7868
- const stuckForMs = now - firstWedgedAt;
7869
- const untracked = this.countUntrackedIds(messages);
7870
- this.log({
7871
- level: "warn",
7872
- message: `Conversation ${conv.id.slice(0, 8)} has ${messages.length} pending message(s) but ALL are already marked dispatched locally (in-flight set: ${this.dispatched.size}) \u2014 none sent to OpenCode for ${consecutiveTicks} consecutive tick(s) now (${stuckForMs}ms stuck). ` + (untracked > 0 ? `${untracked} of these id(s) are tracked by NO watcher \u2014 the dispatched/in-flight pairing invariant is violated for this conversation, which will NOT self-heal and needs a runner restart.` : `A watcher is tracking this work; the loop-liveness watchdog is already recovering it.`),
7873
- conversation_id: conv.id
7874
- });
7875
8743
  this.wedgeWarnings.delete(conv.id);
7876
- this.wedgeWarnings.set(conv.id, { firstWedgedAt, lastWarnedAt: now, consecutiveTicks });
8744
+ this.wedgeWarnings.set(conv.id, {
8745
+ firstWedgedAt,
8746
+ warnAnchorAt: dueForWarn ? now : warnAnchorAt,
8747
+ consecutiveTicks
8748
+ });
7877
8749
  while (this.wedgeWarnings.size > MAX_WEDGED_CONVERSATIONS) {
7878
8750
  const oldest = this.wedgeWarnings.keys().next().value;
7879
8751
  if (oldest === void 0) break;
7880
8752
  this.wedgeWarnings.delete(oldest);
7881
8753
  }
7882
- if (stuckForMs >= this.wedgeWarningIntervalMs) {
7883
- void this.postSignal(conv.id, messages[0].id, "dispatch_wedged", {
7884
- stuck_for_ms: stuckForMs,
7885
- untracked
7886
- });
7887
- }
8754
+ if (!dueForWarn) return;
8755
+ void this.postSignal(conv.id, messages[0].id, "dispatch_wedged", {
8756
+ stuck_for_ms: now - firstWedgedAt,
8757
+ untracked
8758
+ });
7888
8759
  }
7889
8760
  /**
7890
8761
  * How many of `messages`' ids are tracked by NO watcher's `inFlight` (#1618
@@ -8418,23 +9289,6 @@ var ChannelDriver = class _ChannelDriver {
8418
9289
  this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
8419
9290
  return 1;
8420
9291
  }
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
9292
  /** Record one queued give-up toward the never-started bound. */
8439
9293
  recordNeverStartedAttempt(messageId, sessionId) {
8440
9294
  const existing = this.neverStartedAttempts.get(messageId);
@@ -8746,6 +9600,7 @@ var ChannelDriver = class _ChannelDriver {
8746
9600
  b2LastDescendantCheckMs: 0,
8747
9601
  b2RootOngoingHeldLogged: false,
8748
9602
  b2AbandonedSignalled: false,
9603
+ b2AbandonedWithoutAnswer: false,
8749
9604
  ambiguousPinnedSinceMs: 0,
8750
9605
  ambiguousResolved: false,
8751
9606
  sessionStoreFailurePending: false,
@@ -8770,6 +9625,28 @@ var ChannelDriver = class _ChannelDriver {
8770
9625
  this.bufferedSessionErrors.delete(oldest);
8771
9626
  }
8772
9627
  }
9628
+ retainSessionError(event) {
9629
+ this.retainedSessionErrors.delete(event.sessionId);
9630
+ this.retainedSessionErrors.set(event.sessionId, {
9631
+ reason: event.reason,
9632
+ observedAt: this.now()
9633
+ });
9634
+ while (this.retainedSessionErrors.size > MAX_BUFFERED_SESSION_ERRORS) {
9635
+ const oldest = this.retainedSessionErrors.keys().next().value;
9636
+ if (typeof oldest !== "string") break;
9637
+ this.retainedSessionErrors.delete(oldest);
9638
+ }
9639
+ }
9640
+ readRetainedSessionError(sessionId) {
9641
+ const retained = this.retainedSessionErrors.get(sessionId);
9642
+ if (!retained) return null;
9643
+ if (this.now() - retained.observedAt >= SESSION_ERROR_RETENTION_TTL_MS) {
9644
+ this.retainedSessionErrors.delete(sessionId);
9645
+ return null;
9646
+ }
9647
+ this.retainedSessionErrors.delete(sessionId);
9648
+ return retained.reason;
9649
+ }
8773
9650
  /**
8774
9651
  * Build a fresh `SessionWatcher`. Seeds `lastTickAt`/`lastObservedTickAt`
8775
9652
  * EQUAL (#1618) so a watcher whose loop has not started ticking yet is never
@@ -8865,6 +9742,7 @@ var ChannelDriver = class _ChannelDriver {
8865
9742
  b2LastDescendantCheckMs: 0,
8866
9743
  b2RootOngoingHeldLogged: false,
8867
9744
  b2AbandonedSignalled: false,
9745
+ b2AbandonedWithoutAnswer: false,
8868
9746
  ambiguousPinnedSinceMs: 0,
8869
9747
  ambiguousResolved: false,
8870
9748
  sessionStoreFailurePending: false,
@@ -9095,9 +9973,10 @@ var ChannelDriver = class _ChannelDriver {
9095
9973
  return;
9096
9974
  }
9097
9975
  if ([...watcher.inFlight.values()].some((message) => message.started && !message.done)) {
9976
+ this.retainSessionError(event);
9098
9977
  this.log({
9099
- level: "debug",
9100
- message: `A turn is already running in session ${event.sessionId.slice(0, 8)} \u2014 deferring to transcript polling`,
9978
+ level: "warn",
9979
+ 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
9980
  conversation_id: watcher.conv.id
9102
9981
  });
9103
9982
  return;
@@ -9127,9 +10006,10 @@ var ChannelDriver = class _ChannelDriver {
9127
10006
  const messages = await this.getSessionMessages(event.sessionId);
9128
10007
  const state = messageRunState(messages, inFlight.opencodeMessageId);
9129
10008
  if (state !== "queued") {
10009
+ this.retainSessionError(event);
9130
10010
  this.log({
9131
- level: "debug",
9132
- message: `Session error for message ${inFlight.evidentMessageId.slice(0, 8)} observed state ${state}; leaving it to transcript polling`,
10011
+ level: "warn",
10012
+ 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
10013
  conversation_id: watcher.conv.id,
9134
10014
  message_id: inFlight.evidentMessageId
9135
10015
  });
@@ -9141,14 +10021,40 @@ var ChannelDriver = class _ChannelDriver {
9141
10021
  conversation_id: watcher.conv.id,
9142
10022
  message_id: inFlight.evidentMessageId
9143
10023
  });
9144
- await this.markFailed(
9145
- watcher.conv.id,
9146
- inFlight.evidentMessageId,
9147
- event.sessionId,
9148
- `OpenCode could not run this turn: ${event.reason}`
9149
- );
10024
+ const isMissingProvider = isProviderModelNotFoundError(event.reason);
10025
+ if (isMissingProvider) {
10026
+ const dispatchedModel = inFlight.message.opencode_model;
10027
+ const separator = dispatchedModel?.indexOf("/") ?? -1;
10028
+ const hasProviderAndModel = dispatchedModel !== null && separator > 0 && separator < dispatchedModel.length - 1;
10029
+ await this.markFailed(
10030
+ watcher.conv.id,
10031
+ inFlight.evidentMessageId,
10032
+ event.sessionId,
10033
+ `OpenCode could not run this turn: ${event.reason}`,
10034
+ void 0,
10035
+ {
10036
+ classification: {
10037
+ kind: "model_auth",
10038
+ providerId: hasProviderAndModel ? dispatchedModel.slice(0, separator) : null,
10039
+ modelId: hasProviderAndModel ? dispatchedModel.slice(separator + 1) : null,
10040
+ reason: "missing"
10041
+ },
10042
+ errorShape: { name: null, statusCode: null }
10043
+ }
10044
+ );
10045
+ } else {
10046
+ await this.markFailed(
10047
+ watcher.conv.id,
10048
+ inFlight.evidentMessageId,
10049
+ event.sessionId,
10050
+ `OpenCode could not run this turn: ${event.reason}`
10051
+ );
10052
+ }
9150
10053
  inFlight.done = true;
9151
10054
  this.removeInFlight(watcher, inFlight.evidentMessageId);
10055
+ if (isMissingProvider) {
10056
+ await this.repairProviderRegistry(watcher.conv.id, inFlight.evidentMessageId);
10057
+ }
9152
10058
  } catch (err) {
9153
10059
  if (err instanceof ChannelAuthError) {
9154
10060
  this.log({
@@ -9169,6 +10075,44 @@ var ChannelDriver = class _ChannelDriver {
9169
10075
  this.sessionErrorHandled.delete(inFlight.evidentMessageId);
9170
10076
  }
9171
10077
  }
10078
+ async repairProviderRegistry(conversationId, messageId) {
10079
+ if (this.isV2) {
10080
+ void this.postSignal(conversationId, messageId, "provider_registry_reload_unsupported");
10081
+ return;
10082
+ }
10083
+ const activeSessions = this.protectedSessionIds().size;
10084
+ if (activeSessions > 0 || this.draining) {
10085
+ this.pendingProviderRegistryRepair = { conversationId, messageId };
10086
+ void this.postSignal(conversationId, messageId, "provider_registry_repair_deferred", {
10087
+ active_sessions: activeSessions
10088
+ });
10089
+ this.log({
10090
+ level: "warn",
10091
+ message: `Provider registry repair deferred until a lifecycle-safe point; ${activeSessions} active session(s) protected`,
10092
+ conversation_id: conversationId,
10093
+ message_id: messageId
10094
+ });
10095
+ return;
10096
+ }
10097
+ await this.issueProviderRegistryReload(conversationId, messageId);
10098
+ }
10099
+ async issueProviderRegistryReload(conversationId, messageId) {
10100
+ this.pendingProviderRegistryRepair = null;
10101
+ const result = await disposeAndReloadProviderConfig(this.port, this.openCodeClient);
10102
+ if (result.outcome === "failed") {
10103
+ const failure = result.status === null ? `transport failure${result.detail ? `: ${result.detail}` : ""}` : `HTTP ${result.status}`;
10104
+ this.log({
10105
+ level: "warn",
10106
+ message: `Provider registry reload failed (${failure})`,
10107
+ conversation_id: conversationId,
10108
+ message_id: messageId
10109
+ });
10110
+ }
10111
+ const repairOutcome = result.outcome === "reloaded" ? "reloaded" : "reload_failed";
10112
+ void this.postSignal(conversationId, messageId, "provider_registry_reload_issued", {
10113
+ repair_outcome: repairOutcome
10114
+ });
10115
+ }
9172
10116
  /**
9173
10117
  * The per-session polling loop (WI-3). Once per tick it:
9174
10118
  * 1. polls `GET /session/:id/message` once and, per in-flight message,
@@ -9358,7 +10302,16 @@ var ChannelDriver = class _ChannelDriver {
9358
10302
  if (state === "failed") {
9359
10303
  this.anchorDeliveryDeadline(inFlight);
9360
10304
  if (!inFlight.done) {
9361
- const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
10305
+ const messageErrorText = messageError(messages, inFlight.opencodeMessageId);
10306
+ const retainedReason = this.readRetainedSessionError(sessionId);
10307
+ const retainedError = retainedReason === null || messageErrorText !== null && messageErrorText.includes(retainedReason) ? null : `${SESSION_ERROR_RETAINED_DETAIL}${retainedReason}`;
10308
+ const observedCause = this.sessionStoreHealth?.readCause(sessionId);
10309
+ const causeError = observedCause === null || observedCause === void 0 ? null : `${SESSION_ERROR_OBSERVED_CAUSE}${observedCause}`;
10310
+ const storeError = this.sessionStoreHealth?.isUnwritable() ? SESSION_STORE_UNWRITABLE_ERROR : null;
10311
+ const runnerDetails = [retainedError, causeError, storeError].filter(
10312
+ (detail) => detail !== null
10313
+ );
10314
+ const error2 = messageErrorText === null ? runnerDetails.length === 0 ? void 0 : runnerDetails.join(" ") : [messageErrorText, ...runnerDetails].join(" ");
9362
10315
  this.log({
9363
10316
  level: "error",
9364
10317
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
@@ -9444,10 +10397,33 @@ var ChannelDriver = class _ChannelDriver {
9444
10397
  inFlight.b2LastDescendantCheckMs = 0;
9445
10398
  inFlight.b2RootOngoingHeldLogged = false;
9446
10399
  inFlight.b2AbandonedSignalled = false;
10400
+ inFlight.b2AbandonedWithoutAnswer = false;
9447
10401
  }
9448
10402
  } else {
9449
10403
  if (inFlight.b2AbandonedSignalled) {
9450
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
10404
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
10405
+ const finish = reply?.info?.finish ?? reply?.finish;
10406
+ if (inFlight.b2AbandonedWithoutAnswer) {
10407
+ await this.settleMessageAbandonedWithoutAnswer(
10408
+ sessionId,
10409
+ watcher,
10410
+ inFlight,
10411
+ messages,
10412
+ finish
10413
+ );
10414
+ } else {
10415
+ await this.settleMessageDone(
10416
+ sessionId,
10417
+ watcher,
10418
+ inFlight,
10419
+ messages,
10420
+ composeAbandonmentNote(
10421
+ finish,
10422
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10423
+ "with-answer"
10424
+ )
10425
+ );
10426
+ }
9451
10427
  return;
9452
10428
  }
9453
10429
  if (inFlight.b2PinnedSinceMs === 0) inFlight.b2PinnedSinceMs = this.now();
@@ -9475,21 +10451,51 @@ var ChannelDriver = class _ChannelDriver {
9475
10451
  return;
9476
10452
  }
9477
10453
  inFlight.b2AbandonedSignalled = true;
10454
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
10455
+ const finish = reply?.info?.finish ?? reply?.finish;
10456
+ if (extractReplyTextForMessage(messages, inFlight.opencodeMessageId) === null) {
10457
+ inFlight.b2AbandonedWithoutAnswer = true;
10458
+ void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
10459
+ watched_for_ms: pinnedForMs,
10460
+ run_state: "failed",
10461
+ finish,
10462
+ ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
10463
+ ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
10464
+ opencode_message_id: inFlight.opencodeMessageId
10465
+ });
10466
+ await this.settleMessageAbandonedWithoutAnswer(
10467
+ sessionId,
10468
+ watcher,
10469
+ inFlight,
10470
+ messages,
10471
+ finish
10472
+ );
10473
+ return;
10474
+ }
9478
10475
  this.log({
9479
10476
  level: "warn",
9480
10477
  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
10478
  conversation_id: conv.id,
9482
10479
  message_id: id
9483
10480
  });
9484
- const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
9485
10481
  void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
9486
10482
  watched_for_ms: pinnedForMs,
9487
- finish: reply?.info?.finish ?? reply?.finish,
10483
+ finish,
9488
10484
  ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
9489
10485
  ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
9490
10486
  opencode_message_id: inFlight.opencodeMessageId
9491
10487
  });
9492
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
10488
+ await this.settleMessageDone(
10489
+ sessionId,
10490
+ watcher,
10491
+ inFlight,
10492
+ messages,
10493
+ composeAbandonmentNote(
10494
+ finish,
10495
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10496
+ "with-answer"
10497
+ )
10498
+ );
9493
10499
  return;
9494
10500
  }
9495
10501
  if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
@@ -9689,15 +10695,11 @@ var ChannelDriver = class _ChannelDriver {
9689
10695
  }
9690
10696
  }
9691
10697
  /**
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.
10698
+ * Settle a message whose run-state has resolved `'done'`. Normal completion and
10699
+ * confirmed b2 abandonment share the same delivery, usage, and terminal handling.
10700
+ * Normal completions omit the optional terminal diagnostic.
9699
10701
  */
9700
- async settleMessageDone(sessionId, watcher, inFlight, messages) {
10702
+ async settleMessageDone(sessionId, watcher, inFlight, messages, note) {
9701
10703
  const conv = watcher.conv;
9702
10704
  this.anchorDeliveryDeadline(inFlight);
9703
10705
  if (!inFlight.done) {
@@ -9724,7 +10726,8 @@ var ChannelDriver = class _ChannelDriver {
9724
10726
  title,
9725
10727
  usage,
9726
10728
  usageAgentName,
9727
- subagentInvocations
10729
+ subagentInvocations,
10730
+ note
9728
10731
  );
9729
10732
  } catch (err) {
9730
10733
  if (err instanceof ChannelAuthError) throw err;
@@ -9766,6 +10769,78 @@ var ChannelDriver = class _ChannelDriver {
9766
10769
  }
9767
10770
  this.removeInFlight(watcher, inFlight.evidentMessageId);
9768
10771
  }
10772
+ /** Report a confirmed b2 abandonment that produced no answer text. */
10773
+ async settleMessageAbandonedWithoutAnswer(sessionId, watcher, inFlight, messages, finish) {
10774
+ const conv = watcher.conv;
10775
+ const error2 = composeAbandonmentNote(
10776
+ finish,
10777
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10778
+ "without-answer"
10779
+ );
10780
+ this.anchorDeliveryDeadline(inFlight);
10781
+ this.log({
10782
+ level: "warn",
10783
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was abandoned without an answer \u2014 ${error2}`,
10784
+ conversation_id: conv.id,
10785
+ message_id: inFlight.evidentMessageId
10786
+ });
10787
+ const usage = messageUsage(messages, inFlight.opencodeMessageId);
10788
+ const usageAgentName = this.usageAgentName(messages ?? [], inFlight.opencodeMessageId);
10789
+ const subagentInvocations = await this.resolveSubagentInvocations(
10790
+ messages,
10791
+ inFlight.opencodeMessageId,
10792
+ inFlight.evidentMessageId
10793
+ );
10794
+ try {
10795
+ await this.markFailed(
10796
+ conv.id,
10797
+ inFlight.evidentMessageId,
10798
+ sessionId,
10799
+ error2,
10800
+ usage,
10801
+ void 0,
10802
+ usageAgentName,
10803
+ subagentInvocations
10804
+ );
10805
+ } catch (err) {
10806
+ if (err instanceof ChannelAuthError) throw err;
10807
+ if (err instanceof ChannelTerminalError) {
10808
+ this.log({
10809
+ level: "warn",
10810
+ 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}`,
10811
+ conversation_id: conv.id,
10812
+ message_id: inFlight.evidentMessageId
10813
+ });
10814
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10815
+ return;
10816
+ }
10817
+ if (this.now() >= inFlight.deadline) {
10818
+ this.log({
10819
+ level: "warn",
10820
+ 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)}`,
10821
+ conversation_id: conv.id,
10822
+ message_id: inFlight.evidentMessageId
10823
+ });
10824
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10825
+ return;
10826
+ }
10827
+ this.log({
10828
+ level: "warn",
10829
+ 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)}`,
10830
+ conversation_id: conv.id,
10831
+ message_id: inFlight.evidentMessageId
10832
+ });
10833
+ return;
10834
+ }
10835
+ inFlight.done = true;
10836
+ await this.reportSubagentAuthFailures(
10837
+ watcher.conv.id,
10838
+ inFlight.opencodeMessageId,
10839
+ inFlight.evidentMessageId,
10840
+ messages
10841
+ );
10842
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10843
+ }
9769
10844
  /**
9770
10845
  * Report a queued message as failed after the never-started bound. The accepted
9771
10846
  * OpenCode copy is not cancelled, so the local fence remains on unsuccessful
@@ -9828,7 +10903,7 @@ var ChannelDriver = class _ChannelDriver {
9828
10903
  */
9829
10904
  async settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight) {
9830
10905
  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.";
10906
+ const error2 = SESSION_STORE_UNWRITABLE_ERROR;
9832
10907
  this.log({
9833
10908
  level: "error",
9834
10909
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was not resolved as done: ${error2}`,
@@ -9919,11 +10994,22 @@ var ChannelDriver = class _ChannelDriver {
9919
10994
  }
9920
10995
  }
9921
10996
  }
9922
- if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0) {
10997
+ if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0 || this.awaitingCorrelation.size > 0) {
9923
10998
  const stillProcessing = new Set(rows.map((r) => r.id));
10999
+ for (const id of this.awaitingCorrelation.keys()) {
11000
+ if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
11001
+ this.awaitingCorrelation.delete(id);
11002
+ this.log({
11003
+ level: "debug",
11004
+ message: `Re-adopt: message ${id.slice(0, 8)} left processing and pending \u2014 cleared awaiting-correlation fence`,
11005
+ message_id: id
11006
+ });
11007
+ }
11008
+ }
9924
11009
  for (const id of [...this.untrackableAck.keys()]) {
9925
11010
  if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
9926
11011
  this.untrackableAck.delete(id);
11012
+ this.untrackableAckSessionIds.delete(id);
9927
11013
  this.untrackableAckSignalled.delete(id);
9928
11014
  this.untrackableAckWarned.delete(id);
9929
11015
  this.log({
@@ -9963,7 +11049,7 @@ var ChannelDriver = class _ChannelDriver {
9963
11049
  bySession.set(row.opencode_session_id, list);
9964
11050
  }
9965
11051
  for (const [sessionId, sessionRows] of bySession) {
9966
- let messages;
11052
+ let messages = null;
9967
11053
  try {
9968
11054
  if (this.isV2) {
9969
11055
  const snapshot = await this.getSessionMessages(sessionId);
@@ -9972,9 +11058,9 @@ var ChannelDriver = class _ChannelDriver {
9972
11058
  level: "warn",
9973
11059
  message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned an unreadable message snapshot \u2014 skipping this session this tick`
9974
11060
  });
9975
- continue;
11061
+ } else {
11062
+ messages = snapshot;
9976
11063
  }
9977
- messages = snapshot;
9978
11064
  } else {
9979
11065
  const polled = await pollSessionMessagesForRedrive(
9980
11066
  this.port,
@@ -9987,15 +11073,22 @@ var ChannelDriver = class _ChannelDriver {
9987
11073
  level: "warn",
9988
11074
  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
11075
  });
9990
- continue;
11076
+ } else {
11077
+ messages = polled.messages;
9991
11078
  }
9992
- messages = polled.messages;
9993
11079
  }
9994
11080
  } catch (err) {
9995
11081
  this.log({
9996
11082
  level: "warn",
9997
11083
  message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
9998
11084
  });
11085
+ }
11086
+ if (messages === null) {
11087
+ for (const row of sessionRows) {
11088
+ if (this.awaitingCorrelation.has(row.id)) {
11089
+ await this.advanceReadoptCorrelation(sessionId, row, null);
11090
+ }
11091
+ }
9999
11092
  continue;
10000
11093
  }
10001
11094
  const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
@@ -10005,6 +11098,59 @@ var ChannelDriver = class _ChannelDriver {
10005
11098
  }
10006
11099
  }
10007
11100
  }
11101
+ /** Advance a re-adopted accepted prompt through the shared correlation fence. */
11102
+ async advanceReadoptCorrelation(sessionId, row, messages) {
11103
+ const correlation = this.awaitingCorrelation.get(row.id);
11104
+ if (!correlation) return false;
11105
+ correlation.attempts += 1;
11106
+ const correlatedId = messages === null ? null : this.findAwaitingCorrelationId(correlation, sessionId, messages);
11107
+ if (correlatedId !== null) {
11108
+ this.awaitingCorrelation.delete(row.id);
11109
+ const conv = this.convForRow(sessionId, row);
11110
+ const message = this.queuedMessageForRow(row);
11111
+ const processedAtMs = this.processedAtMs(row);
11112
+ this.registerReadopted(conv, sessionId, message, correlatedId, processedAtMs, processedAtMs);
11113
+ this.dispatched.add(row.id);
11114
+ this.readopted.add(row.id);
11115
+ this.ensureWatcherRunning(sessionId);
11116
+ this.log({
11117
+ level: "info",
11118
+ message: `Re-adopt: matched accepted message ${row.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
11119
+ conversation_id: row.conversation_id,
11120
+ message_id: row.id
11121
+ });
11122
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
11123
+ void this.postSignal(row.conversation_id, row.id, "ack_correlated", {
11124
+ opencode_message_id: correlatedId
11125
+ });
11126
+ return true;
11127
+ }
11128
+ if (correlation.knownUserIds === null) {
11129
+ this.log({
11130
+ level: "debug",
11131
+ message: `Re-adopt: message ${row.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
11132
+ conversation_id: row.conversation_id,
11133
+ message_id: row.id
11134
+ });
11135
+ } else if (correlation.sessionId !== sessionId) {
11136
+ this.log({
11137
+ level: "warn",
11138
+ 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`,
11139
+ conversation_id: row.conversation_id,
11140
+ message_id: row.id
11141
+ });
11142
+ }
11143
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
11144
+ await this.handoffUntrackableCorrelation(
11145
+ row.conversation_id,
11146
+ row.id,
11147
+ correlation.sessionId,
11148
+ correlation.attempts,
11149
+ "readopt"
11150
+ );
11151
+ }
11152
+ return true;
11153
+ }
10008
11154
  /**
10009
11155
  * Re-adopt ONE `processing` row against the tick's session message snapshot
10010
11156
  * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
@@ -10012,8 +11158,10 @@ var ChannelDriver = class _ChannelDriver {
10012
11158
  * Branches on `messageRunState(messages, row.opencode_message_id)` — the
10013
11159
  * opencode-assigned user-message id persisted on the first `processing` PATCH
10014
11160
  * (#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`):
11161
+ * before the restart) is first offered to an awaiting-correlation fence recorded
11162
+ * by this process; without that fence, it has no id to correlate after restart
11163
+ * and is treated as an orphan and re-dispatched (at most once, see
11164
+ * `forceReadoptRun`):
10017
11165
  * - `done` → `markDone` now (guarded like the watcher's done branch);
10018
11166
  * - `failed` → `markFailed` with the surfaced error (issue #182), so an
10019
11167
  * errored turn is reported failed on restart, NOT re-dispatched —
@@ -10022,7 +11170,8 @@ var ChannelDriver = class _ChannelDriver {
10022
11170
  * re-dispatched instead (issue #1310, see the branch below);
10023
11171
  * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
10024
11172
  * tracking the stored id so the reply correlates by it;
10025
- * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
11173
+ * - `unknown`/null id → correlate through an awaiting fence when present;
11174
+ * otherwise re-dispatch (opencode assigns a fresh id) + attach a watcher.
10026
11175
  *
10027
11176
  * Only `ChannelAuthError` propagates.
10028
11177
  */
@@ -10036,6 +11185,7 @@ var ChannelDriver = class _ChannelDriver {
10036
11185
  });
10037
11186
  return;
10038
11187
  }
11188
+ if (await this.advanceReadoptCorrelation(sessionId, row, messages)) return;
10039
11189
  const ocId = row.opencode_message_id;
10040
11190
  const state = messageRunState(messages, ocId ?? "");
10041
11191
  if (state === "done") {
@@ -10102,12 +11252,48 @@ var ChannelDriver = class _ChannelDriver {
10102
11252
  await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
10103
11253
  this.dontRedispatch.delete(row.id);
10104
11254
  this.untrackableAck.delete(row.id);
11255
+ this.untrackableAckSessionIds.delete(row.id);
10105
11256
  this.untrackableAckSignalled.delete(row.id);
10106
11257
  this.untrackableAckWarned.delete(row.id);
10107
11258
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
10108
11259
  return;
10109
11260
  }
10110
- if (this.dontRedispatch.has(row.id) || this.untrackableAck.has(row.id)) {
11261
+ if (this.untrackableAck.has(row.id)) {
11262
+ if (this.untrackableAckSignalled.has(row.id)) {
11263
+ const firstFenceWarning = !this.untrackableAckWarned.has(row.id);
11264
+ this.untrackableAckWarned.add(row.id);
11265
+ this.log({
11266
+ level: firstFenceWarning ? "warn" : "debug",
11267
+ 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`,
11268
+ conversation_id: row.conversation_id,
11269
+ message_id: row.id
11270
+ });
11271
+ const errorMessage3 = this.untrackableAck.get(row.id);
11272
+ const reportError = await this.reportUntrackableAck(
11273
+ row.conversation_id,
11274
+ row.id,
11275
+ this.untrackableAckSessionIds.get(row.id) ?? null,
11276
+ errorMessage3
11277
+ );
11278
+ if (reportError !== null) {
11279
+ this.log({
11280
+ level: "warn",
11281
+ message: `Re-adopt: failed to report untrackable message ${row.id.slice(0, 8)} (will retry next drain): ${reportError.message}`,
11282
+ conversation_id: row.conversation_id,
11283
+ message_id: row.id
11284
+ });
11285
+ }
11286
+ } else {
11287
+ this.log({
11288
+ level: "debug",
11289
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
11290
+ conversation_id: row.conversation_id,
11291
+ message_id: row.id
11292
+ });
11293
+ }
11294
+ return;
11295
+ }
11296
+ if (this.dontRedispatch.has(row.id)) {
10111
11297
  this.log({
10112
11298
  level: "debug",
10113
11299
  message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
@@ -10285,6 +11471,7 @@ var ChannelDriver = class _ChannelDriver {
10285
11471
  }
10286
11472
  this.dontRedispatch.delete(row.id);
10287
11473
  this.untrackableAck.delete(row.id);
11474
+ this.untrackableAckSessionIds.delete(row.id);
10288
11475
  this.untrackableAckSignalled.delete(row.id);
10289
11476
  this.untrackableAckWarned.delete(row.id);
10290
11477
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
@@ -10304,8 +11491,9 @@ var ChannelDriver = class _ChannelDriver {
10304
11491
  * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
10305
11492
  * short-circuit while the row is latched; clear it on a successful dispatch (the
10306
11493
  * 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).
11494
+ * re-entry) or when the dispatch itself failed before acceptance. An accepted V1
11495
+ * dispatch whose id is not readable enters the bounded correlation fence instead
11496
+ * of retrying; exhaustion hands it to `untrackableAck`.
10309
11497
  *
10310
11498
  * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
10311
11499
  * `processed_at` (Invariant 1).
@@ -10368,6 +11556,7 @@ var ChannelDriver = class _ChannelDriver {
10368
11556
  );
10369
11557
  }
10370
11558
  let ocId;
11559
+ let preDispatchUserIds = null;
10371
11560
  try {
10372
11561
  ocId = this.isV2 ? await sendV2Prompt(this.openCodeClient, sessionId, row.content) : await this.dispatchLocked(
10373
11562
  sessionId,
@@ -10377,7 +11566,20 @@ var ChannelDriver = class _ChannelDriver {
10377
11566
  row.content,
10378
11567
  options,
10379
11568
  sendAttachments,
10380
- this.openCodeClient
11569
+ this.openCodeClient,
11570
+ {
11571
+ onKnownUserIds: (ids) => {
11572
+ preDispatchUserIds = ids;
11573
+ },
11574
+ onKnownUserIdsError: (error2) => {
11575
+ this.log({
11576
+ level: "warn",
11577
+ message: `Pre-dispatch snapshot callback failed for message ${row.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
11578
+ conversation_id: row.conversation_id,
11579
+ message_id: row.id
11580
+ });
11581
+ }
11582
+ }
10381
11583
  )
10382
11584
  );
10383
11585
  } catch (err) {
@@ -10410,6 +11612,7 @@ var ChannelDriver = class _ChannelDriver {
10410
11612
  }
10411
11613
  } else {
10412
11614
  this.untrackableAck.set(row.id, errorMessage3);
11615
+ this.untrackableAckSessionIds.set(row.id, sessionId);
10413
11616
  }
10414
11617
  this.log({
10415
11618
  level: "error",
@@ -10458,39 +11661,20 @@ var ChannelDriver = class _ChannelDriver {
10458
11661
  }
10459
11662
  if (ocId === null) {
10460
11663
  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
- }
11664
+ this.awaitingCorrelation.set(row.id, {
11665
+ sessionId,
11666
+ content: row.content,
11667
+ attempts: 0,
11668
+ knownUserIds: preDispatchUserIds
11669
+ });
10484
11670
  this.log({
10485
11671
  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`,
11672
+ 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
11673
  conversation_id: row.conversation_id,
10488
11674
  message_id: row.id
10489
11675
  });
10490
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
10491
11676
  return;
10492
11677
  }
10493
- this.unconfirmedDispatchFailures.delete(row.id);
10494
11678
  const processedAtMs = this.processedAtMs(row);
10495
11679
  this.registerReadopted(
10496
11680
  readoptConv,
@@ -10829,14 +12013,14 @@ var ChannelDriver = class _ChannelDriver {
10829
12013
  const fetchChildWithoutBlocking = async (sessionId) => {
10830
12014
  const pending = fetchChildMessages(sessionId);
10831
12015
  let timer;
10832
- const timeout = new Promise((resolve4) => {
12016
+ const timeout = new Promise((resolve5) => {
10833
12017
  timer = setTimeout(() => {
10834
12018
  this.log({
10835
12019
  level: "warn",
10836
12020
  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
12021
  message_id: messageId
10838
12022
  });
10839
- resolve4(null);
12023
+ resolve5(null);
10840
12024
  }, SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS);
10841
12025
  });
10842
12026
  try {
@@ -11396,7 +12580,7 @@ var ChannelDriver = class _ChannelDriver {
11396
12580
  * watcher retries next tick within the
11397
12581
  * deadline, Finding 4).
11398
12582
  */
11399
- async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations) {
12583
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations, error2) {
11400
12584
  const res = await this.fetchImpl(
11401
12585
  `${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
11402
12586
  {
@@ -11414,7 +12598,8 @@ var ChannelDriver = class _ChannelDriver {
11414
12598
  ...title ? { title } : {},
11415
12599
  ...usage ? usage : {},
11416
12600
  ...usageAgentName ? { usage_agent_name: usageAgentName } : {},
11417
- ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {}
12601
+ ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {},
12602
+ ...error2 ? { error: error2 } : {}
11418
12603
  })
11419
12604
  }
11420
12605
  );
@@ -11474,11 +12659,17 @@ var ChannelDriver = class _ChannelDriver {
11474
12659
  if (subagentInvocations && subagentInvocations.length > 0) {
11475
12660
  body.subagent_invocations = subagentInvocations;
11476
12661
  }
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;
12662
+ if (failure && failure.errorShape.name !== null) {
12663
+ body.failure_error_name = failure.errorShape.name;
12664
+ }
12665
+ if (failure && failure.errorShape.statusCode !== null) {
12666
+ body.failure_status_code = failure.errorShape.statusCode;
12667
+ }
12668
+ if (failure?.classification) {
12669
+ body.failure_kind = failure.classification.kind;
12670
+ body.failure_provider_id = failure.classification.providerId;
12671
+ body.failure_model_id = failure.classification.modelId;
12672
+ body.failure_reason = failure.classification.reason;
11482
12673
  }
11483
12674
  try {
11484
12675
  await this.callWithRetry(
@@ -11499,27 +12690,28 @@ var ChannelDriver = class _ChannelDriver {
11499
12690
  this.clearSubagentInvocationCaches(messageId);
11500
12691
  }
11501
12692
  /**
11502
- * Classify an errored turn as a model-auth failure (#736 P1-4), for `markFailed`.
12693
+ * Classify an errored turn and extract its bounded error shape for `markFailed`.
11503
12694
  *
11504
12695
  * `messageFailure` alone (structured OpenCode error → `model_auth`) covers
11505
12696
  * most cases; when it returns `null` on this ALREADY-FAILED turn, fall back
11506
12697
  * to the P1-2b zero-provider check — one extra loopback call to
11507
12698
  * `hasAnyConfiguredProvider`, only reached when the structured classifier
11508
12699
  * 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.
12700
+ * (`null`/indeterminate) leaves the classification `null`; the raw error shape
12701
+ * is still retained for diagnostics.
11511
12702
  */
11512
12703
  async classifyModelAuthFailure(messages, userMessageId) {
11513
- const classified = messageFailure(messages, userMessageId);
11514
- if (classified != null) return classified;
11515
12704
  const reply = findLastAssistantReplyFor(messages, userMessageId);
11516
- const hasProvider = await this.hasAnyConfiguredProvider();
11517
- return applyZeroProviderFallback(
12705
+ const errorShape = extractReplyErrorShape(reply);
12706
+ const classified = messageFailure(messages, userMessageId);
12707
+ if (classified != null) return { classification: classified, errorShape };
12708
+ const classification = applyZeroProviderFallback(
11518
12709
  classified,
11519
- hasProvider,
12710
+ await this.hasAnyConfiguredProvider(),
11520
12711
  reply?.info?.providerID ?? null,
11521
12712
  reply?.info?.modelID ?? null
11522
12713
  );
12714
+ return { classification, errorShape };
11523
12715
  }
11524
12716
  async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
11525
12717
  const providerId = failure.providerId ?? "(unknown)";
@@ -12077,7 +13269,7 @@ function commandError2(result) {
12077
13269
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
12078
13270
  }
12079
13271
  var runCommand2 = (command, args, opts) => {
12080
- return new Promise((resolve4) => {
13272
+ return new Promise((resolve5) => {
12081
13273
  let child;
12082
13274
  let stdout = "";
12083
13275
  let stderr = "";
@@ -12087,7 +13279,7 @@ var runCommand2 = (command, args, opts) => {
12087
13279
  if (settled) return;
12088
13280
  settled = true;
12089
13281
  if (timer.handle) clearTimeout(timer.handle);
12090
- resolve4(result);
13282
+ resolve5(result);
12091
13283
  };
12092
13284
  try {
12093
13285
  child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
@@ -12142,7 +13334,9 @@ async function installRunnerSecret({
12142
13334
  }) {
12143
13335
  const arn = env.RUNNER_SECRET_ARN?.trim();
12144
13336
  if (!arn) {
12145
- log3("runner secret is not configured; continuing without GitHub and MCP credentials");
13337
+ if (!env.GH_TOKEN) {
13338
+ log3("runner secret is not configured; continuing without GitHub and MCP credentials");
13339
+ }
12146
13340
  return false;
12147
13341
  }
12148
13342
  const result = await (commandRunner ?? runCommand2)(
@@ -12341,7 +13535,7 @@ async function configureGitHubAccess({ env, log: log3 }) {
12341
13535
  // src/lib/opencode/config-overlay.ts
12342
13536
  import { execFileSync as execFileSync2 } from "node:child_process";
12343
13537
  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";
13538
+ import { isAbsolute as isAbsolute3, join as join11, resolve as resolve4 } from "node:path";
12345
13539
  function isFile(filePath) {
12346
13540
  return existsSync2(filePath) && statSync6(filePath).isFile();
12347
13541
  }
@@ -12354,8 +13548,8 @@ function applyRunnerOpenCodeConfig({
12354
13548
  log3("runner OpenCode config is not configured; using the baked project config", "debug");
12355
13549
  return;
12356
13550
  }
12357
- const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
12358
- const target = isFile(join10(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
13551
+ const source = isAbsolute3(overlayPath) ? overlayPath : resolve4(cwd, overlayPath);
13552
+ const target = isFile(join11(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
12359
13553
  if (!isFile(source)) {
12360
13554
  log3(
12361
13555
  `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
@@ -12363,7 +13557,7 @@ function applyRunnerOpenCodeConfig({
12363
13557
  );
12364
13558
  return;
12365
13559
  }
12366
- copyFileSync(source, join10(cwd, target));
13560
+ copyFileSync(source, join11(cwd, target));
12367
13561
  try {
12368
13562
  execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
12369
13563
  stdio: "ignore"
@@ -12372,7 +13566,7 @@ function applyRunnerOpenCodeConfig({
12372
13566
  const detail = error2 instanceof Error ? error2.message : String(error2);
12373
13567
  log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
12374
13568
  }
12375
- log3(`Applied runner OpenCode config ${source} to ${join10(cwd, target)}`);
13569
+ log3(`Applied runner OpenCode config ${source} to ${join11(cwd, target)}`);
12376
13570
  }
12377
13571
 
12378
13572
  // src/lib/credential-sync.ts
@@ -12390,14 +13584,14 @@ function errorMessage2(error2) {
12390
13584
  return error2 instanceof Error ? error2.message : String(error2);
12391
13585
  }
12392
13586
  function waitForSettlement(promise, timeoutMs) {
12393
- return new Promise((resolve4) => {
13587
+ return new Promise((resolve5) => {
12394
13588
  let settled = false;
12395
13589
  const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
12396
13590
  const finish = (value) => {
12397
13591
  if (settled) return;
12398
13592
  settled = true;
12399
13593
  clearTimeout(timer);
12400
- resolve4(value);
13594
+ resolve5(value);
12401
13595
  };
12402
13596
  promise.then(
12403
13597
  () => finish(true),
@@ -12614,6 +13808,8 @@ function createCredentialSync({
12614
13808
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
12615
13809
  var MAX_DISCONNECT_REASON_LENGTH = 200;
12616
13810
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
13811
+ var CREDENTIAL_ADOPTION_PROBE_ATTEMPTS = 3;
13812
+ var CREDENTIAL_ADOPTION_PROBE_INTERVAL_MS = 1e3;
12617
13813
  var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
12618
13814
  var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
12619
13815
  var TELEMETRY_SHUTDOWN_TIMEOUT_MS = 5e3;
@@ -12648,8 +13844,8 @@ function resolveFileSyncDirectories(raw, homeDir) {
12648
13844
  if (trimmed === "") {
12649
13845
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
12650
13846
  }
12651
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join11(homeDir, trimmed.slice(2)) : trimmed;
12652
- if (!isAbsolute3(expanded)) {
13847
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join12(homeDir, trimmed.slice(2)) : trimmed;
13848
+ if (!isAbsolute4(expanded)) {
12653
13849
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
12654
13850
  }
12655
13851
  const normalized = resolvePath2(expanded);
@@ -12749,8 +13945,7 @@ function resolveMaxActiveSessions(options, env = process.env) {
12749
13945
  function meetsThreshold(state, level) {
12750
13946
  return LOG_LEVELS[level] >= LOG_LEVELS[state.logLevel];
12751
13947
  }
12752
- function log2(state, message, level = "info") {
12753
- if (!meetsThreshold(state, level)) return;
13948
+ function writeConsoleLog(state, message, level) {
12754
13949
  if (state.json) {
12755
13950
  console.log(
12756
13951
  JSON.stringify({
@@ -12759,10 +13954,18 @@ function log2(state, message, level = "info") {
12759
13954
  message
12760
13955
  })
12761
13956
  );
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}`);
13957
+ return;
12765
13958
  }
13959
+ const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
13960
+ console.log(`${prefix} ${message}`);
13961
+ }
13962
+ function log2(state, message, level = "info") {
13963
+ if (!meetsThreshold(state, level)) return;
13964
+ if (state.json || !state.interactive) writeConsoleLog(state, message, level);
13965
+ }
13966
+ function logLocalOnly(state, message, level = "info") {
13967
+ if (!meetsThreshold(state, level)) return;
13968
+ writeConsoleLog(state, message, level);
12766
13969
  }
12767
13970
  function logActivity(state, entry) {
12768
13971
  const level = entry.level ?? (entry.type === "error" ? "error" : "info");
@@ -12954,6 +14157,125 @@ async function handleAuthError(state, error2) {
12954
14157
  return { success: false };
12955
14158
  }
12956
14159
  }
14160
+ function logCredentialAdoptionActivity(state, activity, level) {
14161
+ const message = (() => {
14162
+ switch (activity.outcome) {
14163
+ case "deferred":
14164
+ return `CREDENTIAL-ADOPTION: deferred generation ${activity.generation} with ${activity.active_sessions} active session(s)`;
14165
+ case "reloaded_ready":
14166
+ return `CREDENTIAL-ADOPTION: adopted generation ${activity.generation} with ${activity.active_sessions} active session(s); readiness probe succeeded after ${activity.probe_attempts} attempt(s)`;
14167
+ case "reloaded_interrupted": {
14168
+ const response = activity.probe_status === null ? "without an HTTP response" : `with HTTP ${activity.probe_status} and ${activity.probe_body_empty ? "an empty" : "a non-empty"} body`;
14169
+ return `CREDENTIAL-ADOPTION: generation ${activity.generation} with ${activity.active_sessions} active session(s); replacement instance answered ${response} after ${activity.probe_attempts} probe attempt(s)`;
14170
+ }
14171
+ case "reload_failed":
14172
+ return `CREDENTIAL-ADOPTION: failed to adopt generation ${activity.generation} with ${activity.active_sessions} active session(s)`;
14173
+ case "skipped_v2":
14174
+ return `CREDENTIAL-ADOPTION: skipped generation ${activity.generation} with ${activity.active_sessions} active session(s) on OpenCode V2`;
14175
+ }
14176
+ })();
14177
+ logActivity(state, {
14178
+ type: level === "error" ? "error" : "info",
14179
+ level,
14180
+ ...level === "error" ? { error: message } : { message },
14181
+ metadata: {
14182
+ ...activity,
14183
+ opencode_version: state.opencodeVersion,
14184
+ port: state.port
14185
+ }
14186
+ });
14187
+ }
14188
+ async function adoptCredentialGeneration(state, generation, adoption, activeSessions, deferredForMs) {
14189
+ const reload = await disposeAndReloadProviderConfig(
14190
+ state.port,
14191
+ state.opencodeClient ?? void 0
14192
+ );
14193
+ if (reload.outcome === "failed") {
14194
+ logCredentialAdoptionActivity(
14195
+ state,
14196
+ {
14197
+ outcome: "reload_failed",
14198
+ generation,
14199
+ active_sessions: activeSessions,
14200
+ adoption,
14201
+ deferred_for_ms: deferredForMs,
14202
+ reload_status: reload.status,
14203
+ reload_detail: reload.detail,
14204
+ probe_attempts: null,
14205
+ probe_status: null,
14206
+ probe_body_empty: null,
14207
+ probe_detail: null
14208
+ },
14209
+ "error"
14210
+ );
14211
+ return;
14212
+ }
14213
+ if (reload.outcome === "skipped_v2") {
14214
+ logCredentialAdoptionActivity(
14215
+ state,
14216
+ {
14217
+ outcome: "skipped_v2",
14218
+ generation,
14219
+ active_sessions: activeSessions,
14220
+ adoption,
14221
+ deferred_for_ms: deferredForMs,
14222
+ reload_status: null,
14223
+ reload_detail: null,
14224
+ probe_attempts: null,
14225
+ probe_status: null,
14226
+ probe_body_empty: null,
14227
+ probe_detail: null
14228
+ },
14229
+ "debug"
14230
+ );
14231
+ return;
14232
+ }
14233
+ let lastProbe = null;
14234
+ let attempts = 0;
14235
+ for (attempts = 1; attempts <= CREDENTIAL_ADOPTION_PROBE_ATTEMPTS; attempts++) {
14236
+ lastProbe = await probeOpenCodeRequestReadiness(state.port, state.opencodeClient ?? void 0);
14237
+ if (lastProbe.ok) {
14238
+ logCredentialAdoptionActivity(
14239
+ state,
14240
+ {
14241
+ outcome: "reloaded_ready",
14242
+ generation,
14243
+ active_sessions: activeSessions,
14244
+ adoption,
14245
+ deferred_for_ms: deferredForMs,
14246
+ reload_status: null,
14247
+ reload_detail: null,
14248
+ probe_attempts: attempts,
14249
+ probe_status: lastProbe.status,
14250
+ probe_body_empty: lastProbe.bodyEmpty,
14251
+ probe_detail: lastProbe.detail
14252
+ },
14253
+ "warn"
14254
+ );
14255
+ return;
14256
+ }
14257
+ if (attempts < CREDENTIAL_ADOPTION_PROBE_ATTEMPTS) {
14258
+ await new Promise((resolve5) => setTimeout(resolve5, CREDENTIAL_ADOPTION_PROBE_INTERVAL_MS));
14259
+ }
14260
+ }
14261
+ logCredentialAdoptionActivity(
14262
+ state,
14263
+ {
14264
+ outcome: "reloaded_interrupted",
14265
+ generation,
14266
+ active_sessions: activeSessions,
14267
+ adoption,
14268
+ deferred_for_ms: deferredForMs,
14269
+ reload_status: null,
14270
+ reload_detail: null,
14271
+ probe_attempts: CREDENTIAL_ADOPTION_PROBE_ATTEMPTS,
14272
+ probe_status: lastProbe?.status ?? null,
14273
+ probe_body_empty: lastProbe?.bodyEmpty ?? null,
14274
+ probe_detail: lastProbe?.detail ?? null
14275
+ },
14276
+ "error"
14277
+ );
14278
+ }
12957
14279
  async function driveChannels(state, driver) {
12958
14280
  let idlePolls = 0;
12959
14281
  let idleMs = 0;
@@ -12963,6 +14285,15 @@ async function driveChannels(state, driver) {
12963
14285
  let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
12964
14286
  let lastSeenClaudeApplies = driver.fileSyncActivity().claudeCredentialApplies;
12965
14287
  let lastSeenOpencodeAuthApplies = driver.fileSyncActivity().opencodeAuthApplyCount;
14288
+ const credentialAdoption = {
14289
+ generation: 0,
14290
+ pendingGeneration: null,
14291
+ pendingSinceMs: null,
14292
+ deferredGeneration: null,
14293
+ adoption: 0
14294
+ };
14295
+ const stayAwakeHolds = () => state.stayAwakeUntilMs !== null && Date.now() < state.stayAwakeUntilMs;
14296
+ let stayAwakeSuppressionLogged = false;
12966
14297
  while (state.running) {
12967
14298
  const cycleStartedAtMs = performance.now();
12968
14299
  let idleThisCycle = false;
@@ -13004,12 +14335,30 @@ async function driveChannels(state, driver) {
13004
14335
  lastSeenOpencodeAuthApplies = opencodeAuthApplies;
13005
14336
  if (opencodeAuthApplied) state.openaiUsageRearm?.();
13006
14337
  if (claudeCredentialApplied || opencodeAuthApplied) {
13007
- void reloadProviderCache(state.port, state.opencodeClient ?? void 0).catch(
13008
- (error2) => logActivity(state, {
13009
- type: "error",
13010
- error: `OpenCode provider cache reload failed on port ${state.port}: ${error2 instanceof Error ? error2.message : String(error2)}`
13011
- })
13012
- );
14338
+ credentialAdoption.generation++;
14339
+ if (state.opencodeApiVersion === "v2") {
14340
+ logCredentialAdoptionActivity(
14341
+ state,
14342
+ {
14343
+ outcome: "skipped_v2",
14344
+ generation: credentialAdoption.generation,
14345
+ active_sessions: driver.protectedSessionIds().size,
14346
+ adoption: null,
14347
+ deferred_for_ms: null,
14348
+ reload_status: null,
14349
+ reload_detail: null,
14350
+ probe_attempts: null,
14351
+ probe_status: null,
14352
+ probe_body_empty: null,
14353
+ probe_detail: null
14354
+ },
14355
+ "debug"
14356
+ );
14357
+ } else {
14358
+ credentialAdoption.pendingGeneration = credentialAdoption.generation;
14359
+ credentialAdoption.pendingSinceMs = Date.now();
14360
+ credentialAdoption.deferredGeneration = null;
14361
+ }
13013
14362
  }
13014
14363
  if (opencodeAuthApplied && state.opencodeApiVersion === "v2") {
13015
14364
  void importOpenCodeV2CredentialsForState(state).catch(
@@ -13064,7 +14413,50 @@ async function driveChannels(state, driver) {
13064
14413
  }
13065
14414
  }
13066
14415
  }
13067
- await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
14416
+ if (credentialAdoption.pendingGeneration !== null) {
14417
+ const activeSessions = driver.protectedSessionIds();
14418
+ const generation = credentialAdoption.pendingGeneration;
14419
+ if (activeSessions.size > 0) {
14420
+ if (credentialAdoption.deferredGeneration !== generation) {
14421
+ credentialAdoption.deferredGeneration = generation;
14422
+ logCredentialAdoptionActivity(
14423
+ state,
14424
+ {
14425
+ outcome: "deferred",
14426
+ generation,
14427
+ active_sessions: activeSessions.size,
14428
+ adoption: null,
14429
+ deferred_for_ms: null,
14430
+ reload_status: null,
14431
+ reload_detail: null,
14432
+ probe_attempts: null,
14433
+ probe_status: null,
14434
+ probe_body_empty: null,
14435
+ probe_detail: null
14436
+ },
14437
+ "warn"
14438
+ );
14439
+ }
14440
+ } else {
14441
+ const deferredForMs = credentialAdoption.deferredGeneration === generation && credentialAdoption.pendingSinceMs !== null ? Math.max(0, Date.now() - credentialAdoption.pendingSinceMs) : null;
14442
+ credentialAdoption.pendingGeneration = null;
14443
+ credentialAdoption.pendingSinceMs = null;
14444
+ credentialAdoption.adoption++;
14445
+ void adoptCredentialGeneration(
14446
+ state,
14447
+ generation,
14448
+ credentialAdoption.adoption,
14449
+ activeSessions.size,
14450
+ deferredForMs
14451
+ ).catch(
14452
+ (error2) => logActivity(state, {
14453
+ type: "error",
14454
+ error: `CREDENTIAL-ADOPTION: adoption failed for generation ${generation} with ${activeSessions.size} active session(s) on port ${state.port}: ${error2 instanceof Error ? error2.message : String(error2)}`
14455
+ })
14456
+ );
14457
+ }
14458
+ }
14459
+ await new Promise((resolve5) => setTimeout(resolve5, CHANNEL_POLL_INTERVAL_MS));
13068
14460
  const cycleMs = performance.now() - cycleStartedAtMs;
13069
14461
  if (idleThisCycle) idleMs += cycleMs;
13070
14462
  if (unreachableThisCycle) unreachableMs += cycleMs;
@@ -13077,15 +14469,27 @@ async function driveChannels(state, driver) {
13077
14469
  if (state.interactive) displayStatus(state);
13078
14470
  break;
13079
14471
  }
13080
- if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3) {
14472
+ if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && !stayAwakeHolds()) {
13081
14473
  logActivity(state, { type: "info", message: "Idle timeout reached" });
13082
14474
  if (state.interactive) displayStatus(state);
13083
14475
  break;
13084
14476
  }
14477
+ if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && stayAwakeHolds()) {
14478
+ if (!stayAwakeSuppressionLogged) {
14479
+ stayAwakeSuppressionLogged = true;
14480
+ logActivity(state, {
14481
+ type: "info",
14482
+ message: "Idle, but staying awake until the requested minimum passes"
14483
+ });
14484
+ }
14485
+ }
13085
14486
  }
13086
14487
  }
13087
14488
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
13088
14489
  var SESSION_CLEANUP_MAX_DELETES_PER_SWEEP = 100;
14490
+ var SESSION_CLEANUP_MAX_ISOLATED_DELETE_FAILURES = 2;
14491
+ var SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES = 2;
14492
+ var MAX_SESSION_DELETE_BODY_LENGTH = 200;
13089
14493
  function shouldWarnForReclaimSkip(reason) {
13090
14494
  if (reason !== "sqlite-unavailable" && reason !== "disk-exhaustion") return false;
13091
14495
  const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
@@ -13096,7 +14500,7 @@ function shouldWarnForReclaimSkip(reason) {
13096
14500
  return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
13097
14501
  }
13098
14502
  function sessionDbPath() {
13099
- return join11(homedir7(), ".local", "share", "opencode", "opencode.db");
14503
+ return join12(homedir7(), ".local", "share", "opencode", "opencode.db");
13100
14504
  }
13101
14505
  function logSessionCleanupActivity(state, phase, entry) {
13102
14506
  logActivity(state, {
@@ -13173,8 +14577,12 @@ async function runSweep(state, driver, config) {
13173
14577
  protectedIds: protectedNow
13174
14578
  }
13175
14579
  );
13176
- const deleteCandidates = toDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
13177
- remaining = toDelete.length - deleteCandidates.length;
14580
+ const eligibleToDelete = toDelete.filter(
14581
+ (id) => (state.sessionDeleteFailures.get(id)?.isolatedFailures ?? 0) < SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES
14582
+ );
14583
+ const deleteCandidates = eligibleToDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
14584
+ remaining = eligibleToDelete.length - deleteCandidates.length;
14585
+ const answeredFailures = [];
13178
14586
  protectedNow = driver.protectedSessionIds();
13179
14587
  for (const id of deleteCandidates) {
13180
14588
  if (protectedNow.has(id)) {
@@ -13197,8 +14605,44 @@ async function runSweep(state, driver, config) {
13197
14605
  if (statusClass === 4) failed4xx++;
13198
14606
  else if (statusClass === 5) failed5xx++;
13199
14607
  else failedOtherStatus++;
14608
+ answeredFailures.push({
14609
+ id,
14610
+ status: outcome.status,
14611
+ ...outcome.body !== void 0 ? { body: outcome.body } : {}
14612
+ });
14613
+ }
14614
+ }
14615
+ const givenUpFailures = [];
14616
+ if (failed > SESSION_CLEANUP_MAX_ISOLATED_DELETE_FAILURES) {
14617
+ for (const { id } of answeredFailures) state.sessionDeleteFailures.delete(id);
14618
+ } else {
14619
+ for (const failure of answeredFailures) {
14620
+ const previous = state.sessionDeleteFailures.get(failure.id);
14621
+ const record = previous?.status === failure.status ? { status: failure.status, isolatedFailures: previous.isolatedFailures + 1 } : { status: failure.status, isolatedFailures: 1 };
14622
+ state.sessionDeleteFailures.set(failure.id, record);
14623
+ if (record.isolatedFailures >= SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES) {
14624
+ givenUpFailures.push(failure);
14625
+ }
13200
14626
  }
13201
14627
  }
14628
+ for (const failure of givenUpFailures) {
14629
+ const excerpt = redactDiagnosticText(failure.body ?? "(no response body)").slice(
14630
+ 0,
14631
+ MAX_SESSION_DELETE_BODY_LENGTH
14632
+ );
14633
+ logSessionCleanupActivity(state, "sweep", {
14634
+ type: "info",
14635
+ level: "warn",
14636
+ message: `Session cleanup: gave up on session ${failure.id} after ${SESSION_CLEANUP_GIVE_UP_AFTER_FAILURES} isolated delete failures (status ${failure.status}): ${excerpt}`,
14637
+ metadata: {
14638
+ outcome: "session-delete-given-up",
14639
+ session_id: failure.id,
14640
+ status: failure.status,
14641
+ body_excerpt: excerpt,
14642
+ given_up_count: givenUpFailures.length
14643
+ }
14644
+ });
14645
+ }
13202
14646
  const failedNote = failed > 0 ? `, failed ${failed}` : "";
13203
14647
  const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
13204
14648
  const remainingNote = remaining > 0 ? `, remaining ${remaining}` : "";
@@ -13219,21 +14663,31 @@ async function runSweep(state, driver, config) {
13219
14663
  sessions_remaining: remaining,
13220
14664
  ...enumerationReason !== null ? { session_enumeration_reason: enumerationReason } : {}
13221
14665
  };
14666
+ const protectedForReclaim = driver.protectedSessionIds();
13222
14667
  const reclaimResult = await reclaimSessionDbSpace({
13223
14668
  dbPath: sessionDbPath(),
13224
14669
  chunkPages: SESSION_DB_RECLAIM_CHUNK_PAGES,
13225
14670
  pageCeiling: SESSION_DB_RECLAIM_PAGE_CEILING,
13226
14671
  timeBudgetMs: SESSION_DB_RECLAIM_TIME_BUDGET_MS,
13227
- allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
14672
+ hasLiveTurn: protectedForReclaim.size > 0,
14673
+ hasProvenanceAnomaly: state.sessionDbProvenanceAnomaly
13228
14674
  });
13229
14675
  if (reclaimResult.ok) {
13230
14676
  state.sessionStoreHealth.observeWritable();
13231
14677
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
13232
14678
  const afterMib = (reclaimResult.afterBytes / 1024 / 1024).toFixed(1);
13233
14679
  const checkpointNote = reclaimResult.checkpoint.busy ? ` (on-disk file truncation deferred: checkpoint busy, ${reclaimResult.checkpoint.log} WAL frames pending)` : "";
14680
+ const loopElapsedMs = Math.max(
14681
+ 0,
14682
+ reclaimResult.elapsedMs - reclaimResult.checkpointElapsedMs
14683
+ );
14684
+ const reclaimIsSlow = reclaimResult.elapsedMs >= 4 * SESSION_DB_RECLAIM_TIME_BUDGET_MS;
14685
+ const perPageNote = reclaimResult.pagesReclaimed > 0 ? `, per-page ${(loopElapsedMs / reclaimResult.pagesReclaimed).toFixed(2)} ms/page` : "";
14686
+ 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
14687
  logSessionCleanupActivity(state, "sweep", {
13235
14688
  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,
14689
+ level: reclaimIsSlow ? "warn" : "info",
14690
+ 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
14691
  metadata: {
13238
14692
  outcome: "reclaimed",
13239
14693
  mode: reclaimResult.mode,
@@ -13246,6 +14700,7 @@ async function runSweep(state, driver, config) {
13246
14700
  freelist_after: reclaimResult.freelistAfter,
13247
14701
  pages_reclaimed: reclaimResult.pagesReclaimed,
13248
14702
  reclaim_elapsed_ms: reclaimResult.elapsedMs,
14703
+ checkpoint_elapsed_ms: reclaimResult.checkpointElapsedMs,
13249
14704
  ...sessionMetadata
13250
14705
  }
13251
14706
  });
@@ -13356,10 +14811,12 @@ function scheduleUsageReporting(state, params) {
13356
14811
  let phase = "dormant";
13357
14812
  let rearmRequested = false;
13358
14813
  const armProbe = () => {
14814
+ if (!state.running) return;
13359
14815
  phase = "probe-pending";
13360
14816
  params.setTimer(setTimeout(() => void tick(true), params.firstDelayMs()));
13361
14817
  };
13362
14818
  const scheduleNextTick = () => {
14819
+ if (!state.running) return;
13363
14820
  if (rearmRequested) {
13364
14821
  rearmRequested = false;
13365
14822
  armProbe();
@@ -13393,6 +14850,7 @@ function scheduleUsageReporting(state, params) {
13393
14850
  phase = "tick-in-flight";
13394
14851
  try {
13395
14852
  const usage = await params.fetchUsage();
14853
+ if (!state.running) return;
13396
14854
  const result = await params.report(usage);
13397
14855
  if (result.ok) {
13398
14856
  if (consecutiveFailures > 0) {
@@ -13531,6 +14989,7 @@ function scheduleResourceUsageReporting(state, options) {
13531
14989
  message: lowDiskDecision.warning
13532
14990
  });
13533
14991
  }
14992
+ if (!state.running) return;
13534
14993
  const result = await reportResourceUsage(state.agentId, state.authHeader, usage);
13535
14994
  if (result.ok) {
13536
14995
  if (consecutiveFailures > 0) {
@@ -13563,7 +15022,9 @@ function scheduleResourceUsageReporting(state, options) {
13563
15022
  message: `Resource usage reporting failed: ${message}${failureStreakSuffix(consecutiveFailures)}`
13564
15023
  });
13565
15024
  } finally {
13566
- state.resourceUsageTimer = setTimeout(() => void tick(), usageReportDelayMs());
15025
+ if (state.running) {
15026
+ state.resourceUsageTimer = setTimeout(() => void tick(), usageReportDelayMs());
15027
+ }
13567
15028
  }
13568
15029
  };
13569
15030
  state.resourceUsageTimer = setTimeout(() => void tick(), firstReportDelayMs());
@@ -13725,8 +15186,8 @@ async function beginGracefulShutdown(state, trigger) {
13725
15186
  return true;
13726
15187
  }
13727
15188
  );
13728
- const timedOut = new Promise((resolve4) => {
13729
- timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
15189
+ const timedOut = new Promise((resolve5) => {
15190
+ timer = setTimeout(() => resolve5(false), telemetryBudgetMs);
13730
15191
  });
13731
15192
  if (!await Promise.race([flushed, timedOut])) {
13732
15193
  log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
@@ -13766,6 +15227,8 @@ async function run(options) {
13766
15227
  port: options.port ?? 4096,
13767
15228
  conversationFilter: options.conversation ?? null,
13768
15229
  idleTimeout: options.idleTimeout ?? null,
15230
+ stayAwakeSeconds: null,
15231
+ stayAwakeUntilMs: null,
13769
15232
  json: options.json ?? false,
13770
15233
  interactive,
13771
15234
  logLevel,
@@ -13789,6 +15252,7 @@ async function run(options) {
13789
15252
  messageCount: 0,
13790
15253
  lastProxiedActivityAt: null,
13791
15254
  sessionCleanupTimers: [],
15255
+ sessionDeleteFailures: /* @__PURE__ */ new Map(),
13792
15256
  claudeUsageTimer: null,
13793
15257
  claudeUsageRearm: null,
13794
15258
  openaiUsageTimer: null,
@@ -13954,6 +15418,17 @@ async function run(options) {
13954
15418
  }
13955
15419
  spinner?.succeed(`Runner: ${validation.agent.name || state.agentId}`);
13956
15420
  state.agentName = validation.agent.name;
15421
+ const requestedStayAwakeSeconds = validation.agent?.wake_stay_awake_seconds;
15422
+ if (typeof requestedStayAwakeSeconds === "number" && Number.isFinite(requestedStayAwakeSeconds) && requestedStayAwakeSeconds > 0) {
15423
+ state.stayAwakeSeconds = requestedStayAwakeSeconds;
15424
+ } else if (requestedStayAwakeSeconds !== void 0 && requestedStayAwakeSeconds !== null) {
15425
+ state.stayAwakeSeconds = null;
15426
+ log2(
15427
+ state,
15428
+ `Ignoring invalid wake_stay_awake_seconds (type: ${typeof requestedStayAwakeSeconds})`,
15429
+ "warn"
15430
+ );
15431
+ }
13957
15432
  const microvmId = process.env.MICROVM_ID?.trim();
13958
15433
  if (microvmId) {
13959
15434
  const reported = await reportMicrovmId(state.agentId, state.authHeader, microvmId);
@@ -13969,29 +15444,73 @@ async function run(options) {
13969
15444
  } else {
13970
15445
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
13971
15446
  }
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
- }
15447
+ if (options.restoreRunnerCredentials || options.credentialSyncMarker) {
15448
+ const logCredentialVaultResolution = (message, level) => {
15449
+ log2(state, message, level);
15450
+ if (state.interactive && !state.json) {
15451
+ logActivity(state, { type: "info", level, message });
13982
15452
  }
13983
15453
  };
13984
- const githubTokenPopulated = await installRunnerSecret(credentialContext);
15454
+ const vaultResolution = await getRunnerCredentialVault(state.agentId, state.authHeader);
15455
+ if (vaultResolution.error) {
15456
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
15457
+ logCredentialVaultResolution(
15458
+ `CREDENTIAL-VAULT-LOOKUP-FAILED: ${vaultResolution.error}; credentials will be read from and written to this runner's own prefix this boot`,
15459
+ "warn"
15460
+ );
15461
+ } else if (vaultResolution.prefix !== null && vaultResolution.vaultId !== null && vaultResolution.source !== null) {
15462
+ process.env.CREDENTIAL_VAULT_PREFIX = vaultResolution.prefix;
15463
+ logCredentialVaultResolution(
15464
+ `CREDENTIAL-VAULT: source=${vaultResolution.source} vault=${vaultResolution.vaultId} prefix=${vaultResolution.prefix}`,
15465
+ "info"
15466
+ );
15467
+ } else {
15468
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
15469
+ logCredentialVaultResolution(
15470
+ "CREDENTIAL-VAULT-NONE: this runner is not linked to a credential vault; using its own prefix",
15471
+ "debug"
15472
+ );
15473
+ }
15474
+ }
15475
+ const credentialContext = {
15476
+ env: process.env,
15477
+ log: (message, level = "info") => {
15478
+ if (level === "error") {
15479
+ logActivity(state, { type: "error", error: message });
15480
+ } else {
15481
+ logActivity(state, { type: "info", level, message });
15482
+ }
15483
+ }
15484
+ };
15485
+ const githubTokenPopulated = await installRunnerSecret(credentialContext);
15486
+ if (options.restoreRunnerCredentials) {
13985
15487
  await restoreCredentialStores(credentialContext);
13986
- if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
13987
15488
  }
15489
+ if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
13988
15490
  state.credentialSync?.arm();
15491
+ const dependencyInstallDiagnosticReporter = createDependencyInstallDiagnosticReporter({
15492
+ context: {
15493
+ homeDir: homedir7(),
15494
+ cwd: process.cwd(),
15495
+ microvmId: process.env.MICROVM_ID?.trim(),
15496
+ euid: process.getuid?.(),
15497
+ egid: process.getgid?.()
15498
+ },
15499
+ emitEvent: (message) => logActivity(state, { type: "info", level: "warn", message }),
15500
+ emitLocal: (message) => logLocalOnly(state, message, "warn")
15501
+ });
13989
15502
  const openCodeActivityForwarder = createOpenCodeActivityForwarder(
13990
15503
  () => ({
13991
15504
  agentId: state.agentId,
13992
15505
  authHeader: state.authHeader
13993
15506
  }),
13994
- state.sessionStoreHealth.observeUnwritable
15507
+ ({ isDiskFailure, sessionId, cause }) => {
15508
+ if (isDiskFailure) state.sessionStoreHealth.observeUnwritable();
15509
+ if (cause !== null && sessionId !== null) {
15510
+ state.sessionStoreHealth.observeCause(sessionId, cause);
15511
+ }
15512
+ },
15513
+ dependencyInstallDiagnosticReporter
13995
15514
  );
13996
15515
  const openCodeLogTail = tailOpenCodeLogFile(
13997
15516
  resolveOpenCodeLogPath(homedir7(), process.env),
@@ -14002,22 +15521,27 @@ async function run(options) {
14002
15521
  openCodeLogTail.stop();
14003
15522
  };
14004
15523
  let sessionDbVerifyFatal = false;
15524
+ let sessionDbVerifyFatalReason;
14005
15525
  if (!options.restoreSessionDb) {
14006
- log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
15526
+ logActivity(state, {
15527
+ type: "info",
15528
+ level: "warn",
15529
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=flag-absent \u2014 Skipping session-DB restore: --restore-session-db was not passed"
15530
+ });
14007
15531
  } else {
14008
15532
  const health = await checkOpenCodeHealth(state.port);
14009
15533
  if (health.healthy) {
14010
- log2(
14011
- state,
14012
- "Skipping session-DB restore: OpenCode is already serving this database",
14013
- "debug"
14014
- );
15534
+ logActivity(state, {
15535
+ type: "info",
15536
+ level: "warn",
15537
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=opencode-serving \u2014 Skipping session-DB restore: OpenCode is already serving this database"
15538
+ });
14015
15539
  } 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
- );
15540
+ logActivity(state, {
15541
+ type: "info",
15542
+ level: "warn",
15543
+ 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"
15544
+ });
14021
15545
  } else {
14022
15546
  const result = await restoreAndVerifySessionDb({
14023
15547
  dbPath: sessionDbPath(),
@@ -14034,12 +15558,13 @@ async function run(options) {
14034
15558
  reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
14035
15559
  });
14036
15560
  sessionDbVerifyFatal = result.verifyFatal;
15561
+ sessionDbVerifyFatalReason = result.verifyFatalReason;
14037
15562
  }
14038
15563
  }
14039
15564
  reportSessionDbRecovery(state);
14040
15565
  if (sessionDbVerifyFatal) {
14041
15566
  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"
15567
+ 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
15568
  );
14044
15569
  }
14045
15570
  applyRunnerOpenCodeConfig({
@@ -14147,7 +15672,7 @@ async function run(options) {
14147
15672
  logActivity(state, { type: "info", level: "warn", message: versionWarning });
14148
15673
  }
14149
15674
  }
14150
- await reloadProviderCache(state.port, state.opencodeClient ?? void 0);
15675
+ await disposeAndReloadProviderConfig(state.port, state.opencodeClient ?? void 0);
14151
15676
  if (state.opencodeApiVersion === "v2" && state.opencodeClient !== null) {
14152
15677
  await importOpenCodeV2CredentialsForState(state);
14153
15678
  }
@@ -14318,6 +15843,13 @@ async function run(options) {
14318
15843
  type: "info",
14319
15844
  message: connectionMessage
14320
15845
  });
15846
+ if (!isReconnect && state.stayAwakeSeconds !== null && state.stayAwakeUntilMs === null) {
15847
+ state.stayAwakeUntilMs = Date.now() + state.stayAwakeSeconds * 1e3;
15848
+ logActivity(state, {
15849
+ type: "info",
15850
+ message: `Staying awake for at least ${state.stayAwakeSeconds}s as requested by the wake`
15851
+ });
15852
+ }
14321
15853
  if (options.tunnelReadyFile) {
14322
15854
  const marker = writeTunnelReadyMarker(options.tunnelReadyFile, agentId);
14323
15855
  if (marker.ok) {
@@ -14366,14 +15898,14 @@ async function run(options) {
14366
15898
  },
14367
15899
  onDisconnected: (code, reason, transition) => {
14368
15900
  state.connected = false;
14369
- const safeReason = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
15901
+ const safeReason2 = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
14370
15902
  logActivity(state, {
14371
15903
  type: "info",
14372
- message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason})`
15904
+ message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason2})`
14373
15905
  });
14374
15906
  emitAgentDisconnected(state.agentId, {
14375
15907
  code,
14376
- reason: safeReason,
15908
+ reason: safeReason2,
14377
15909
  connection_generation: transition.generation
14378
15910
  });
14379
15911
  if (state.interactive) displayStatus(state);
@@ -14452,6 +15984,7 @@ async function run(options) {
14452
15984
  if (error2.message === "Unauthorized") tunnelSpinner?.fail("Unauthorized");
14453
15985
  throw error2;
14454
15986
  }
15987
+ if (!state.running) return;
14455
15988
  scheduleSessionCleanup(state, channelDriver, options);
14456
15989
  state.claudeUsageRearm = scheduleClaudeUsageReporting(state, options);
14457
15990
  state.openaiUsageRearm = scheduleUsageReporting(state, {
@@ -14536,6 +16069,7 @@ program.command("login").description("Authenticate with Evident").option("--toke
14536
16069
  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
16070
  program.command("whoami").description("Show the currently logged in user").action(whoami);
14538
16071
  program.command("status").description("Check whether the configured credentials can reach Evident").option("--json", "Output in JSON format").action((options) => status({ json: options.json }));
16072
+ program.command("credential-vault-env").description("Print the shell assignment a runner image evals before starting the synchroniser").action(credentialVaultEnv);
14539
16073
  program.command("claude-usage").description("[spike] Show Claude subscription usage (requires a local `claude login`)").action(claudeUsage);
14540
16074
  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
16075
  "-a, --agent [id]",