@evident-ai/cli 3.5.2-dev.c2c9f26 → 3.5.2-dev.dc42196

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/index.ts
4
- import { createRequire as createRequire2 } from "module";
5
4
  import { Command } from "commander";
6
5
 
7
6
  // src/commands/login.ts
@@ -704,6 +703,31 @@ async function reportResourceUsage(agentId, authHeader, usage) {
704
703
  opencode_db_bytes: usage.opencodeDbBytes
705
704
  });
706
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
+ }
707
731
  async function getAgentInfo(agentId, authHeader) {
708
732
  const apiUrl = getApiUrlConfig();
709
733
  try {
@@ -878,6 +902,76 @@ async function status(options = {}) {
878
902
  process.exit(result.exitCode);
879
903
  }
880
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
+
881
975
  // src/lib/claude-usage.ts
882
976
  import { execFileSync } from "node:child_process";
883
977
  import { readFileSync } from "node:fs";
@@ -1063,13 +1157,83 @@ async function claudeUsage() {
1063
1157
 
1064
1158
  // src/commands/run.ts
1065
1159
  import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync7, writeFileSync as writeFileSync6 } from "node:fs";
1066
- import { homedir as homedir6 } from "node:os";
1160
+ import { homedir as homedir7 } from "node:os";
1067
1161
  import { isAbsolute as isAbsolute3, join as join11, parse, resolve as resolvePath2 } from "node:path";
1068
1162
  import chalk7 from "chalk";
1069
1163
 
1070
1164
  // ../../packages/types/src/agents/index.ts
1071
1165
  var MICROVM_MAX_LIFETIME_MS = 8 * 60 * 6e4;
1072
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
+
1073
1237
  // ../../packages/types/src/telemetry/index.ts
1074
1238
  var TelemetryEventTypes = {
1075
1239
  // Agent activity events (shown in web UI activity log)
@@ -1158,7 +1322,7 @@ import ora4 from "ora";
1158
1322
  import { select as select4 } from "@inquirer/prompts";
1159
1323
 
1160
1324
  // src/lib/telemetry.ts
1161
- var CLI_VERSION = (true ? "3.5.2-dev.c2c9f26" : void 0) ?? process.env.npm_package_version ?? "unknown";
1325
+ var CLI_VERSION = (true ? "3.5.2-dev.dc42196" : void 0) ?? process.env.npm_package_version ?? "unknown";
1162
1326
  function getCliVersion() {
1163
1327
  return CLI_VERSION;
1164
1328
  }
@@ -1172,6 +1336,23 @@ var authProvider = null;
1172
1336
  function setTelemetryAuthProvider(provider) {
1173
1337
  authProvider = provider;
1174
1338
  }
1339
+ var diagnosticsReporter = null;
1340
+ function setTelemetryDiagnosticsReporter(reporter) {
1341
+ diagnosticsReporter = reporter;
1342
+ }
1343
+ function reportTelemetryDiagnostic(message) {
1344
+ if (!diagnosticsReporter) {
1345
+ console.error(message);
1346
+ return;
1347
+ }
1348
+ try {
1349
+ diagnosticsReporter(message);
1350
+ } catch (error2) {
1351
+ console.error(
1352
+ `Telemetry diagnostics reporter failed: ${error2 instanceof Error ? error2.message : String(error2)}; original diagnostic: ${message}`
1353
+ );
1354
+ }
1355
+ }
1175
1356
  var FLUSH_FAILURE_LOG_INTERVAL_MS = 6e4;
1176
1357
  var lastFlushFailureLoggedAt = 0;
1177
1358
  var suppressedFlushFailureCount = 0;
@@ -1237,7 +1418,9 @@ async function flushEvents() {
1237
1418
  signal: controller.signal
1238
1419
  });
1239
1420
  if (!response.ok) {
1240
- console.error(`Telemetry flush failed: ${response.status}`);
1421
+ reportTelemetryDiagnostic(
1422
+ `Telemetry could not send events (HTTP ${response.status}); runner and OpenCode are unaffected`
1423
+ );
1241
1424
  }
1242
1425
  } finally {
1243
1426
  clearTimeout(timeout);
@@ -1247,7 +1430,9 @@ async function flushEvents() {
1247
1430
  if (now - lastFlushFailureLoggedAt >= FLUSH_FAILURE_LOG_INTERVAL_MS) {
1248
1431
  const message = error2 instanceof Error ? error2.message : String(error2);
1249
1432
  const suffix = suppressedFlushFailureCount > 0 ? ` (${suppressedFlushFailureCount} more suppressed in the last ${FLUSH_FAILURE_LOG_INTERVAL_MS / 1e3}s)` : "";
1250
- console.error(`Telemetry flush error: ${message}${suffix}`);
1433
+ reportTelemetryDiagnostic(
1434
+ `Telemetry flush failed: ${message}; runner and OpenCode are unaffected${suffix}`
1435
+ );
1251
1436
  lastFlushFailureLoggedAt = now;
1252
1437
  suppressedFlushFailureCount = 0;
1253
1438
  } else {
@@ -1298,9 +1483,323 @@ var EventTypes = {
1298
1483
  DEPRECATED_AGENT_KEY_ENV_USED: "cli.deprecated_agent_key_env_used"
1299
1484
  };
1300
1485
 
1486
+ // src/lib/opencode/session-db-reclaim.ts
1487
+ import { statSync as statSync2, statfsSync } from "node:fs";
1488
+ import { dirname as dirname2 } from "node:path";
1489
+
1490
+ // src/lib/diagnostic-redaction.ts
1491
+ import { homedir as homedir2 } from "node:os";
1492
+ function redactHomeDirectory(message) {
1493
+ const home = homedir2();
1494
+ if (!home || home === "/") {
1495
+ return message;
1496
+ }
1497
+ const pathSegmentCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-";
1498
+ let result = "";
1499
+ let searchFrom = 0;
1500
+ let homeIndex;
1501
+ while ((homeIndex = message.indexOf(home, searchFrom)) !== -1) {
1502
+ const homeEnd = homeIndex + home.length;
1503
+ const previousCharacter = message[homeIndex - 1];
1504
+ const nextCharacter = message[homeEnd];
1505
+ const atPathBoundary = (!previousCharacter || !pathSegmentCharacters.includes(previousCharacter)) && (!nextCharacter || !pathSegmentCharacters.includes(nextCharacter));
1506
+ result += message.slice(searchFrom, homeIndex);
1507
+ result += atPathBoundary ? "~" : home;
1508
+ searchFrom = homeEnd;
1509
+ }
1510
+ return result + message.slice(searchFrom);
1511
+ }
1512
+ function redactDiagnosticText(message) {
1513
+ const redacted = message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/(?<![A-Za-z0-9_-])sk-ant-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-ant-***").replace(/(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-proj-***").replace(/(?<![A-Za-z0-9_-])sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "sk-***").replace(/(?<![A-Za-z0-9_-])(gh[oprsu])_[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "$1_***").replace(/(?<![A-Za-z0-9_-])github_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_-])/g, "github_pat_***").replace(/https?:\/\/\S+/g, "<url>");
1514
+ return redactHomeDirectory(redacted);
1515
+ }
1516
+
1517
+ // src/lib/opencode/session-db-reclaim.ts
1518
+ var SESSION_DB_RECLAIM_CHUNK_PAGES = 2e3;
1519
+ var SESSION_DB_RECLAIM_TIME_BUDGET_MS = 500;
1520
+ var SESSION_DB_RECLAIM_PAGE_CEILING = 4e4;
1521
+ function errorMessage(error2) {
1522
+ if (!(error2 instanceof Error)) return String(error2);
1523
+ return error2.cause instanceof Error ? error2.cause.message : error2.message;
1524
+ }
1525
+ function errorMessages(error2) {
1526
+ const messages = [];
1527
+ const visited = /* @__PURE__ */ new Set();
1528
+ let current = error2;
1529
+ while (current !== null && current !== void 0 && !visited.has(current)) {
1530
+ visited.add(current);
1531
+ if (typeof current === "string") {
1532
+ messages.push(current);
1533
+ break;
1534
+ }
1535
+ if (current instanceof Error) {
1536
+ messages.push(current.name, current.message);
1537
+ current = current.cause;
1538
+ continue;
1539
+ }
1540
+ if (typeof current === "object") {
1541
+ const record = current;
1542
+ if (typeof record.code === "string") messages.push(record.code);
1543
+ if (typeof record.message === "string") messages.push(record.message);
1544
+ current = record.cause;
1545
+ continue;
1546
+ }
1547
+ break;
1548
+ }
1549
+ return messages;
1550
+ }
1551
+ function isSessionStoreWriteFailure(error2) {
1552
+ return errorMessages(error2).some(
1553
+ (message) => /(?:database or disk is full|disk is full|no space left on device|SQLITE_(?:FULL|IOERR)(?:\b|_))/i.test(
1554
+ message
1555
+ )
1556
+ );
1557
+ }
1558
+ 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;
1559
+ var EFFECT_SQL_CAUSE_TOKEN = /(?:^|cause:\s+)(effect\/sql\/SqlError\/[A-Za-z][A-Za-z0-9_]*)/i;
1560
+ var PARAMS_FIELD = /\bparams\s*(?:=|:)/i;
1561
+ function isCauseBearingText(text) {
1562
+ return /^(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text.trim()) || /(?:^|\s)cause:\s+(?:SQLiteError:\s+|effect\/sql\/SqlError\/)/i.test(text);
1563
+ }
1564
+ function structuredLogFields(line) {
1565
+ const fields = [];
1566
+ for (let index = 0; index < line.length; index++) {
1567
+ if (line[index] === '"') {
1568
+ index++;
1569
+ while (index < line.length) {
1570
+ if (line[index] === "\\") {
1571
+ index++;
1572
+ } else if (line[index] === '"') {
1573
+ break;
1574
+ }
1575
+ index++;
1576
+ }
1577
+ continue;
1578
+ }
1579
+ const atFieldBoundary = index === 0 || /\s/.test(line[index - 1]);
1580
+ const key = line.startsWith("error=", index) ? "error=" : line.startsWith("cause=", index) ? "cause=" : null;
1581
+ if (!atFieldBoundary || key === null) continue;
1582
+ const valueStart = index + key.length;
1583
+ if (line[valueStart] !== '"') {
1584
+ const valueEnd2 = line.indexOf(" ", valueStart);
1585
+ fields.push(line.slice(valueStart, valueEnd2 === -1 ? line.length : valueEnd2));
1586
+ index = valueEnd2 === -1 ? line.length : valueEnd2;
1587
+ continue;
1588
+ }
1589
+ let valueEnd = valueStart + 1;
1590
+ for (; valueEnd < line.length; valueEnd++) {
1591
+ if (line[valueEnd] === "\\") {
1592
+ valueEnd++;
1593
+ } else if (line[valueEnd] === '"') {
1594
+ break;
1595
+ }
1596
+ }
1597
+ fields.push(line.slice(valueStart + 1, valueEnd));
1598
+ index = valueEnd;
1599
+ }
1600
+ return fields;
1601
+ }
1602
+ function structuredCauseMessages(text) {
1603
+ const fields = structuredLogFields(text);
1604
+ if (fields.length > 0) return fields;
1605
+ return isCauseBearingText(text) ? [text] : [];
1606
+ }
1607
+ function causeTokenFromText(text) {
1608
+ const paramsIndex = text.search(PARAMS_FIELD);
1609
+ for (const pattern of [SQLITE_CAUSE_TOKEN, EFFECT_SQL_CAUSE_TOKEN]) {
1610
+ const match = pattern.exec(text);
1611
+ if (match?.[1] && (paramsIndex === -1 || match.index < paramsIndex)) {
1612
+ return match[1];
1613
+ }
1614
+ }
1615
+ return null;
1616
+ }
1617
+ function sessionStoreCauseToken(value) {
1618
+ for (const message of structuredCauseMessages(value)) {
1619
+ const token = causeTokenFromText(message);
1620
+ if (token !== null) return redactDiagnosticText(token).slice(0, 120);
1621
+ }
1622
+ return null;
1623
+ }
1624
+ function insufficientSpaceReason(dbPath, requiredBytes) {
1625
+ try {
1626
+ const fsStats = statfsSync(dirname2(dbPath));
1627
+ const availableBytes = fsStats.bavail * fsStats.bsize;
1628
+ if (availableBytes < requiredBytes) {
1629
+ return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
1630
+ }
1631
+ return null;
1632
+ } catch (err) {
1633
+ return `could not check free space (${err instanceof Error ? err.message : String(err)}); refusing to guess`;
1634
+ }
1635
+ }
1636
+ function readLogicalBytes(db) {
1637
+ const pageCount = db.prepare("PRAGMA page_count").get().page_count;
1638
+ const pageSize = db.prepare("PRAGMA page_size").get().page_size;
1639
+ return pageCount * pageSize;
1640
+ }
1641
+ function readFreelistCount(db) {
1642
+ return db.prepare("PRAGMA freelist_count").get().freelist_count;
1643
+ }
1644
+ function readCheckpointResult(db) {
1645
+ const row = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
1646
+ return { busy: row.busy !== 0, log: row.log, checkpointed: row.checkpointed };
1647
+ }
1648
+ async function probeReclaimAvailability(input) {
1649
+ const { dbPath, requiredBytes } = input;
1650
+ let sqlite;
1651
+ try {
1652
+ sqlite = await import("node:sqlite");
1653
+ } catch (err) {
1654
+ const detail = `Node ${process.version}: ${errorMessage(err)}`;
1655
+ console.warn(`[probeReclaimAvailability] node:sqlite unavailable for ${dbPath}: ` + detail);
1656
+ return { reason: "sqlite-unavailable", detail };
1657
+ }
1658
+ let autoVacuum = null;
1659
+ try {
1660
+ const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
1661
+ try {
1662
+ db.exec("PRAGMA busy_timeout=5000");
1663
+ autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1664
+ } finally {
1665
+ db.close();
1666
+ }
1667
+ } catch (err) {
1668
+ console.warn(
1669
+ `[probeReclaimAvailability] could not read auto_vacuum mode for ${dbPath}: ${err instanceof Error ? err.message : String(err)}`
1670
+ );
1671
+ }
1672
+ if (autoVacuum !== 0) return null;
1673
+ return insufficientSpaceReason(dbPath, requiredBytes) !== null ? { reason: "insufficient-disk-space" } : null;
1674
+ }
1675
+ async function reclaimSessionDbSpace(input) {
1676
+ const {
1677
+ dbPath,
1678
+ pageCeiling = SESSION_DB_RECLAIM_PAGE_CEILING,
1679
+ timeBudgetMs = SESSION_DB_RECLAIM_TIME_BUDGET_MS,
1680
+ chunkPages = SESSION_DB_RECLAIM_CHUNK_PAGES,
1681
+ now = () => performance.now(),
1682
+ allowFullVacuum = true
1683
+ } = input;
1684
+ let sqlite;
1685
+ try {
1686
+ sqlite = await import("node:sqlite");
1687
+ } catch (err) {
1688
+ const detail = `Node ${process.version}: ${errorMessage(err)}`;
1689
+ console.warn(
1690
+ `[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${detail}`
1691
+ );
1692
+ return { ok: false, skipped: "sqlite-unavailable", detail };
1693
+ }
1694
+ const { DatabaseSync } = sqlite;
1695
+ let db;
1696
+ try {
1697
+ db = new DatabaseSync(dbPath);
1698
+ db.exec("PRAGMA busy_timeout=5000");
1699
+ const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
1700
+ if (autoVacuum === 0) {
1701
+ if (!allowFullVacuum) {
1702
+ console.warn(
1703
+ `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: a session turn is live`
1704
+ );
1705
+ return {
1706
+ ok: false,
1707
+ skipped: "full-vacuum-blocked",
1708
+ detail: "a session turn is live"
1709
+ };
1710
+ }
1711
+ const fileBytesForGuard = statSync2(dbPath).size;
1712
+ const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
1713
+ if (skipReason !== null) {
1714
+ console.warn(
1715
+ `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: ${skipReason}`
1716
+ );
1717
+ return { ok: false, skipped: "insufficient-disk-space", detail: skipReason };
1718
+ }
1719
+ const beforeBytes = readLogicalBytes(db);
1720
+ const freelistBefore = readFreelistCount(db);
1721
+ const startedAt = now();
1722
+ db.exec("PRAGMA auto_vacuum=INCREMENTAL");
1723
+ db.exec("VACUUM");
1724
+ const afterBytes = readLogicalBytes(db);
1725
+ const freelistAfter = readFreelistCount(db);
1726
+ const checkpoint = readCheckpointResult(db);
1727
+ return {
1728
+ ok: true,
1729
+ mode: "convert",
1730
+ beforeBytes,
1731
+ afterBytes,
1732
+ freelistBefore,
1733
+ freelistAfter,
1734
+ pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1735
+ stoppedBy: "freelist-drained",
1736
+ elapsedMs: Math.max(0, now() - startedAt),
1737
+ checkpoint
1738
+ };
1739
+ }
1740
+ if (autoVacuum === 2) {
1741
+ const beforeBytes = readLogicalBytes(db);
1742
+ const freelistBefore = readFreelistCount(db);
1743
+ const startedAt = now();
1744
+ const pageLimit = Math.max(0, Math.trunc(pageCeiling));
1745
+ const chunkLimit = Math.max(1, Math.trunc(chunkPages));
1746
+ const budget = Math.max(0, timeBudgetMs);
1747
+ let pagesAttempted = 0;
1748
+ let freelistAfter = freelistBefore;
1749
+ let stoppedBy = "freelist-drained";
1750
+ while (freelistAfter > 0) {
1751
+ if (pagesAttempted >= pageLimit) {
1752
+ stoppedBy = "page-ceiling";
1753
+ break;
1754
+ }
1755
+ if (now() - startedAt >= budget) {
1756
+ stoppedBy = "time-budget";
1757
+ break;
1758
+ }
1759
+ const pages = Math.min(chunkLimit, pageLimit - pagesAttempted, freelistAfter);
1760
+ db.exec(`PRAGMA incremental_vacuum(${pages})`);
1761
+ pagesAttempted += pages;
1762
+ freelistAfter = readFreelistCount(db);
1763
+ }
1764
+ const afterBytes = readLogicalBytes(db);
1765
+ const checkpoint = readCheckpointResult(db);
1766
+ return {
1767
+ ok: true,
1768
+ mode: "incremental",
1769
+ beforeBytes,
1770
+ afterBytes,
1771
+ freelistBefore,
1772
+ freelistAfter,
1773
+ pagesReclaimed: Math.max(0, freelistBefore - freelistAfter),
1774
+ stoppedBy,
1775
+ elapsedMs: Math.max(0, now() - startedAt),
1776
+ checkpoint
1777
+ };
1778
+ }
1779
+ console.warn(
1780
+ `[reclaimSessionDbSpace] ${dbPath} has auto_vacuum=${autoVacuum} (neither NONE nor INCREMENTAL); nothing to reclaim`
1781
+ );
1782
+ return { ok: false, skipped: "auto-vacuum-not-applicable" };
1783
+ } catch (err) {
1784
+ console.error(`[reclaimSessionDbSpace] reclaim failed for ${dbPath}: ` + errorMessage(err));
1785
+ return {
1786
+ ok: false,
1787
+ skipped: isSessionStoreWriteFailure(err) ? "disk-exhaustion" : "reclaim-error",
1788
+ detail: errorMessage(err)
1789
+ };
1790
+ } finally {
1791
+ db?.close();
1792
+ }
1793
+ }
1794
+
1301
1795
  // src/lib/runner-activity-telemetry.ts
1302
- var FORWARDED_LEVELS = /* @__PURE__ */ new Set(["warn", "error"]);
1796
+ var FORWARDED_LEVELS_BY_SOURCE = {
1797
+ "cli.run": /* @__PURE__ */ new Set(["warn", "error"]),
1798
+ opencode: /* @__PURE__ */ new Set(["warn", "error"]),
1799
+ "cli.session-cleanup": /* @__PURE__ */ new Set(["info", "warn", "error"])
1800
+ };
1303
1801
  var SEVERITY_BY_LEVEL = {
1802
+ info: "info",
1304
1803
  warn: "warning",
1305
1804
  error: "error"
1306
1805
  };
@@ -1310,26 +1809,91 @@ function parseOpenCodeLogLine(line) {
1310
1809
  if (!levelMatch) return null;
1311
1810
  const level = levelMatch[1].toUpperCase();
1312
1811
  if (level !== "WARN" && level !== "ERROR") return null;
1313
- const sessionMatch = normalisedLine.match(/(?:^|\s)sessionID=(\S+)/);
1314
- return { level: level === "WARN" ? "warn" : "error", sessionID: sessionMatch?.[1] };
1812
+ const sessionMatch = normalisedLine.match(/(?:^|\s)(?:sessionID|session\.id)=(\S+)/);
1813
+ const messageIDMatch = normalisedLine.match(/(?:^|\s)messageID=(\S+)/);
1814
+ const messageMatch = normalisedLine.match(/(?:^|\s)message=(?:"((?:\\.|[^"])*)"|(\S+))/);
1815
+ const parsed = {
1816
+ level: level === "WARN" ? "warn" : "error",
1817
+ sessionID: sessionMatch?.[1],
1818
+ messageID: messageIDMatch?.[1]
1819
+ };
1820
+ const message = messageMatch?.[1] ?? messageMatch?.[2];
1821
+ if (message !== void 0) parsed.message = message;
1822
+ return parsed;
1315
1823
  }
1316
1824
  var MAX_LINE_BUFFER_BYTES = 16 * 1024;
1317
- function createOpenCodeActivityForwarder(getContext) {
1825
+ var REPEAT_SUPPRESSION_WINDOW_MS = 6e4;
1826
+ function createOpenCodeActivityForwarder(getContext, onSessionStoreWriteFailure) {
1318
1827
  let buffer = Buffer.alloc(0);
1319
- const flushLine = (line) => {
1320
- const parsed = parseOpenCodeLogLine(line);
1321
- if (!parsed) return;
1828
+ const repeatStates = /* @__PURE__ */ new Map();
1829
+ const forwardLine = (line, parsed, repeatsSuppressed = 0) => {
1830
+ const metadata = {
1831
+ ...parsed.sessionID ? { sessionID: parsed.sessionID } : {},
1832
+ ...parsed.messageID ? { messageID: parsed.messageID } : {},
1833
+ ...repeatsSuppressed > 0 ? { repeats_suppressed: repeatsSuppressed } : {}
1834
+ };
1322
1835
  forwardRunnerActivity(
1323
1836
  {
1324
1837
  level: parsed.level,
1325
1838
  error: line,
1326
- metadata: parsed.sessionID ? { sessionID: parsed.sessionID } : void 0,
1839
+ metadata: Object.keys(metadata).length > 0 ? metadata : void 0,
1327
1840
  source: "opencode"
1328
1841
  },
1329
1842
  getContext()
1330
1843
  );
1331
1844
  };
1332
- return (chunk) => {
1845
+ const expireRepeatStates = (now, currentKey) => {
1846
+ for (const [key, state] of repeatStates) {
1847
+ if (key === currentKey || now - state.lastForwardedAt < REPEAT_SUPPRESSION_WINDOW_MS) {
1848
+ continue;
1849
+ }
1850
+ if (state.repeatsSuppressed > 0) {
1851
+ forwardLine(state.line, state.parsed, state.repeatsSuppressed);
1852
+ }
1853
+ repeatStates.delete(key);
1854
+ }
1855
+ };
1856
+ const flushLine = (line) => {
1857
+ const parsed = parseOpenCodeLogLine(line);
1858
+ const isDiskFailure = isSessionStoreWriteFailure(line);
1859
+ const cause = sessionStoreCauseToken(line);
1860
+ if (onSessionStoreWriteFailure && (isDiskFailure || cause !== null)) {
1861
+ try {
1862
+ onSessionStoreWriteFailure({
1863
+ isDiskFailure,
1864
+ sessionId: parsed?.sessionID ?? null,
1865
+ cause
1866
+ });
1867
+ } catch (err) {
1868
+ reportTelemetryDiagnostic(
1869
+ `Runner activity telemetry could not record session-store write failure: ${err instanceof Error ? err.message : String(err)}`
1870
+ );
1871
+ }
1872
+ }
1873
+ if (!parsed) return;
1874
+ const now = Date.now();
1875
+ const key = parsed.message === void 0 ? void 0 : `${parsed.level}\0${parsed.message}\0${parsed.sessionID ?? ""}\0${parsed.messageID ?? ""}`;
1876
+ expireRepeatStates(now, key);
1877
+ if (key === void 0) {
1878
+ forwardLine(line, parsed);
1879
+ return;
1880
+ }
1881
+ const previous = repeatStates.get(key);
1882
+ if (!previous || now - previous.lastForwardedAt >= REPEAT_SUPPRESSION_WINDOW_MS) {
1883
+ forwardLine(line, parsed, previous?.repeatsSuppressed ?? 0);
1884
+ repeatStates.set(key, {
1885
+ line,
1886
+ parsed,
1887
+ lastForwardedAt: now,
1888
+ repeatsSuppressed: 0
1889
+ });
1890
+ return;
1891
+ }
1892
+ previous.line = line;
1893
+ previous.parsed = parsed;
1894
+ previous.repeatsSuppressed++;
1895
+ };
1896
+ const forward = ((chunk) => {
1333
1897
  buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, "utf-8")]);
1334
1898
  let newlineIndex;
1335
1899
  while ((newlineIndex = buffer.indexOf(10)) !== -1) {
@@ -1340,31 +1904,51 @@ function createOpenCodeActivityForwarder(getContext) {
1340
1904
  flushLine(buffer.toString("utf-8"));
1341
1905
  buffer = Buffer.alloc(0);
1342
1906
  }
1907
+ });
1908
+ forward.flush = () => {
1909
+ for (const state of repeatStates.values()) {
1910
+ if (state.repeatsSuppressed > 0) {
1911
+ forwardLine(state.line, state.parsed, state.repeatsSuppressed);
1912
+ }
1913
+ }
1914
+ repeatStates.clear();
1343
1915
  };
1916
+ return forward;
1344
1917
  }
1345
1918
  var MAX_MESSAGE_LENGTH = 500;
1346
1919
  var MAX_METADATA_VALUE_LENGTH = 200;
1347
1920
  var MAX_METADATA_ENTRIES = 20;
1348
- var TRUNCATION_MARKER = "\u2026";
1349
- function redact(message) {
1350
- return 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>");
1351
- }
1352
1921
  function truncate(message) {
1353
1922
  if (message.length <= MAX_MESSAGE_LENGTH) return message;
1354
- return message.slice(0, MAX_MESSAGE_LENGTH - TRUNCATION_MARKER.length) + TRUNCATION_MARKER;
1923
+ let dropped = message.length - MAX_MESSAGE_LENGTH;
1924
+ let marker = `\u2026${dropped} chars omitted\u2026`;
1925
+ let previousDropped = -1;
1926
+ while (dropped !== previousDropped) {
1927
+ previousDropped = dropped;
1928
+ const nextDropped = message.length - MAX_MESSAGE_LENGTH + marker.length;
1929
+ dropped = nextDropped;
1930
+ marker = `\u2026${dropped} chars omitted\u2026`;
1931
+ }
1932
+ const remaining = MAX_MESSAGE_LENGTH - marker.length;
1933
+ const headLength = Math.ceil(remaining * 0.6);
1934
+ const tailLength = remaining - headLength;
1935
+ return message.slice(0, headLength) + marker + message.slice(message.length - tailLength);
1355
1936
  }
1356
1937
  function sanitiseMetadata(metadata) {
1357
1938
  if (!metadata) return {};
1358
1939
  const entries = Object.entries(metadata).slice(0, MAX_METADATA_ENTRIES);
1359
1940
  if (Object.keys(metadata).length > entries.length) {
1360
- console.error(
1361
- `[runner-activity-telemetry] dropped ${Object.keys(metadata).length - entries.length} metadata entries`
1941
+ reportTelemetryDiagnostic(
1942
+ `Runner activity telemetry dropped ${Object.keys(metadata).length - entries.length} metadata entries; those entries were not sent`
1362
1943
  );
1363
1944
  }
1364
1945
  const sanitised = [];
1365
1946
  for (const [key, value] of entries) {
1366
1947
  if (typeof value === "string") {
1367
- sanitised.push([key, truncate(redact(value).slice(0, MAX_METADATA_VALUE_LENGTH))]);
1948
+ sanitised.push([
1949
+ key,
1950
+ truncate(redactDiagnosticText(value).slice(0, MAX_METADATA_VALUE_LENGTH))
1951
+ ]);
1368
1952
  } else if (value === null || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) {
1369
1953
  sanitised.push([key, value]);
1370
1954
  }
@@ -1382,8 +1966,8 @@ function admitUnderRateLimit(source, now) {
1382
1966
  }
1383
1967
  if (now - window.windowStartedAt >= RATE_LIMIT_WINDOW_MS) {
1384
1968
  if (window.windowDroppedCount > 0) {
1385
- console.error(
1386
- `[runner-activity-telemetry] rate cap reached: dropped ${window.windowDroppedCount} ${window.windowDroppedCount === 1 ? "entry" : "entries"} in the last ${RATE_LIMIT_WINDOW_MS / 1e3}s (cap ${RATE_LIMIT_MAX_EVENTS}/min) for source "${source}"`
1969
+ reportTelemetryDiagnostic(
1970
+ `Runner activity telemetry rate cap reached: dropped ${window.windowDroppedCount} ${window.windowDroppedCount === 1 ? "entry" : "entries"} in the last ${RATE_LIMIT_WINDOW_MS / 1e3}s (limit ${RATE_LIMIT_MAX_EVENTS}/min); those entries were not sent for source "${source}"`
1387
1971
  );
1388
1972
  }
1389
1973
  window.windowStartedAt = now;
@@ -1393,8 +1977,8 @@ function admitUnderRateLimit(source, now) {
1393
1977
  if (window.windowCount >= RATE_LIMIT_MAX_EVENTS) {
1394
1978
  window.windowDroppedCount++;
1395
1979
  if (window.windowDroppedCount === 1) {
1396
- console.error(
1397
- `[runner-activity-telemetry] rate cap reached (${RATE_LIMIT_MAX_EVENTS} per ${RATE_LIMIT_WINDOW_MS / 1e3}s) \u2014 dropping further entries this window for source "${source}"`
1980
+ reportTelemetryDiagnostic(
1981
+ `Runner activity telemetry rate cap reached (${RATE_LIMIT_MAX_EVENTS} entries per ${RATE_LIMIT_WINDOW_MS / 1e3}s limit; further entries were not sent for source "${source}"`
1398
1982
  );
1399
1983
  }
1400
1984
  return false;
@@ -1404,12 +1988,12 @@ function admitUnderRateLimit(source, now) {
1404
1988
  }
1405
1989
  function forwardRunnerActivity(entry, context) {
1406
1990
  try {
1407
- if (!FORWARDED_LEVELS.has(entry.level)) return;
1408
- if (!context.agentId || !context.authHeader) return;
1409
1991
  const source = entry.source ?? "cli.run";
1992
+ if (!FORWARDED_LEVELS_BY_SOURCE[source].has(entry.level)) return;
1993
+ if (!context.agentId || !context.authHeader) return;
1410
1994
  if (!admitUnderRateLimit(source, Date.now())) return;
1411
1995
  const rawMessage = entry.error ?? entry.message ?? "";
1412
- const message = truncate(redact(rawMessage));
1996
+ const message = truncate(redactDiagnosticText(rawMessage));
1413
1997
  logEvent(TelemetryEventTypes.RUNNER_ACTIVITY, {
1414
1998
  severity: SEVERITY_BY_LEVEL[entry.level],
1415
1999
  message,
@@ -1417,8 +2001,8 @@ function forwardRunnerActivity(entry, context) {
1417
2001
  agentId: context.agentId
1418
2002
  });
1419
2003
  } catch (err) {
1420
- console.error(
1421
- `[runner-activity-telemetry] failed to forward runner activity: ${err instanceof Error ? err.message : String(err)}`
2004
+ reportTelemetryDiagnostic(
2005
+ `Runner activity telemetry failed to forward runner activity: ${err instanceof Error ? err.message : String(err)}`
1422
2006
  );
1423
2007
  }
1424
2008
  }
@@ -1558,12 +2142,23 @@ function buildSessionDbRecoveryActivity(record) {
1558
2142
  metadata: withoutContractFields(record),
1559
2143
  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}`
1560
2144
  };
1561
- case "session_db_boot_refused":
1562
- return {
1563
- level,
1564
- metadata: withoutContractFields(record),
1565
- 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.`
1566
- };
2145
+ case "session_db_boot_refused": {
2146
+ const message = (() => {
2147
+ switch (record.reason) {
2148
+ case "local_discard_failed":
2149
+ 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.`;
2150
+ case "replica_separation_unproven":
2151
+ 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.`;
2152
+ case "verification_timeout_before_separation":
2153
+ 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.";
2154
+ case "verification_timeout_during_separation":
2155
+ 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.";
2156
+ default:
2157
+ 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.";
2158
+ }
2159
+ })();
2160
+ return { level, metadata: withoutContractFields(record), message };
2161
+ }
1567
2162
  case "schema_provenance_mismatch":
1568
2163
  return {
1569
2164
  level,
@@ -1708,8 +2303,46 @@ async function waitForOpenCode2Health(port, password, timeoutMs = 3e4) {
1708
2303
 
1709
2304
  // src/lib/http-timeout.ts
1710
2305
  var REQUEST_TIMEOUT_MS = 6e4;
2306
+ var RequestTimeoutError = class extends Error {
2307
+ constructor(operation, timeoutMs, elapsedMs) {
2308
+ super(`${operation} timed out after ${elapsedMs}ms (limit ${timeoutMs}ms)`);
2309
+ this.operation = operation;
2310
+ this.timeoutMs = timeoutMs;
2311
+ this.elapsedMs = elapsedMs;
2312
+ this.name = "TimeoutError";
2313
+ }
2314
+ };
2315
+ function describeRequestOperation(input, init) {
2316
+ const method = (init?.method ?? input.method ?? "GET").toUpperCase();
2317
+ const rawUrl = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
2318
+ let path;
2319
+ try {
2320
+ path = new URL(rawUrl).pathname;
2321
+ } catch (error2) {
2322
+ if (!(error2 instanceof TypeError)) throw error2;
2323
+ path = rawUrl.split(/[?#]/, 1)[0];
2324
+ }
2325
+ const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
2326
+ const prefixedIdentifier = /^[a-z]+_[A-Za-z0-9_-]{10,}$/;
2327
+ const maskedPath = path.split("/").map((segment) => uuid.test(segment) || prefixedIdentifier.test(segment) ? ":id" : segment).join("/");
2328
+ return `${method} ${maskedPath}`;
2329
+ }
1711
2330
  function withRequestTimeout(fetchImpl, timeoutMs) {
1712
- return ((input, init) => fetchImpl(input, { ...init, signal: AbortSignal.timeout(timeoutMs) }));
2331
+ return (async (input, init) => {
2332
+ const startedAt = Date.now();
2333
+ try {
2334
+ return await fetchImpl(input, { ...init, signal: AbortSignal.timeout(timeoutMs) });
2335
+ } catch (error2) {
2336
+ if (error2?.name === "TimeoutError") {
2337
+ throw new RequestTimeoutError(
2338
+ describeRequestOperation(input, init),
2339
+ timeoutMs,
2340
+ Date.now() - startedAt
2341
+ );
2342
+ }
2343
+ throw error2;
2344
+ }
2345
+ });
1713
2346
  }
1714
2347
 
1715
2348
  // src/lib/opencode/client.ts
@@ -1775,7 +2408,7 @@ async function getOpenCodeDirectory(port, client) {
1775
2408
  return null;
1776
2409
  }
1777
2410
  }
1778
- function roleOf(m) {
2411
+ function roleOf2(m) {
1779
2412
  if (!m || typeof m !== "object") return void 0;
1780
2413
  if (typeof m.role === "string") return m.role;
1781
2414
  const infoRole = m.info?.role;
@@ -1789,13 +2422,13 @@ function createdOf(m) {
1789
2422
  if (!m || typeof m !== "object") return void 0;
1790
2423
  return m.info?.time?.created ?? m.time?.created;
1791
2424
  }
1792
- function idOf(m) {
2425
+ function idOf2(m) {
1793
2426
  if (!m || typeof m !== "object") return void 0;
1794
2427
  if (typeof m.id === "string") return m.id;
1795
2428
  const infoId = m.info?.id;
1796
2429
  return typeof infoId === "string" ? infoId : void 0;
1797
2430
  }
1798
- function parentIdOf(m) {
2431
+ function parentIdOf2(m) {
1799
2432
  if (!m || typeof m !== "object") return void 0;
1800
2433
  if (typeof m.parentID === "string") return m.parentID;
1801
2434
  const infoParent = m.info?.parentID;
@@ -1811,8 +2444,12 @@ function errorOf(m) {
1811
2444
  if (!m || typeof m !== "object") return void 0;
1812
2445
  return m.info?.error ?? m.error;
1813
2446
  }
2447
+ function isTerminalStopAnswer(m) {
2448
+ return completedOf(m) != null && errorOf(m) == null && finishOf(m) === "stop";
2449
+ }
1814
2450
  function isAssistantInFlight(m) {
1815
2451
  if (completedOf(m) == null) return true;
2452
+ if (errorOf(m) != null) return false;
1816
2453
  return finishOf(m) === "tool-calls";
1817
2454
  }
1818
2455
  async function getSessionMessages(port, sessionId, client) {
@@ -1860,9 +2497,10 @@ async function pollSessionMessagesForRedrive(port, sessionId, client) {
1860
2497
  function isSessionActivelyGenerating(messages) {
1861
2498
  if (!messages || messages.length === 0) return false;
1862
2499
  const last = messages[messages.length - 1];
1863
- if (roleOf(last) !== "assistant") return false;
2500
+ if (roleOf2(last) !== "assistant") return false;
1864
2501
  return completedOf(last) == null;
1865
2502
  }
2503
+ var FULL_SESSION_ENUMERATION_MAX_SESSIONS = 3200;
1866
2504
  function sessionLastActivityMs(session) {
1867
2505
  const candidates = [
1868
2506
  session.time?.updated,
@@ -1877,30 +2515,60 @@ function sessionLastActivityMs(session) {
1877
2515
  }
1878
2516
  return null;
1879
2517
  }
1880
- async function listSessions(port, client) {
1881
- if (client?.version === "v2") return listV2Sessions(client);
2518
+ function sessionListPath(options) {
2519
+ const params = new URLSearchParams();
2520
+ if (options?.limit !== void 0) params.set("limit", String(options.limit));
2521
+ const query = params.toString();
2522
+ return query ? `/session?${query}` : "/session";
2523
+ }
2524
+ async function listSessionPage(port, client, options) {
2525
+ const path = sessionListPath(options);
1882
2526
  try {
1883
- const res = await requestWithClient(port, client, "/session");
2527
+ const res = await requestWithClient(port, client, path);
1884
2528
  if (!res.ok) return null;
1885
2529
  const body = await res.json();
1886
2530
  return Array.isArray(body) ? body : null;
1887
2531
  } catch (error2) {
1888
2532
  console.error(
1889
- `[listSessions] GET /session failed: ${error2 instanceof Error ? error2.message : String(error2)}`
2533
+ `[listSessionPage] GET ${path} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
1890
2534
  );
1891
2535
  return null;
1892
2536
  }
1893
2537
  }
2538
+ async function listSessions(port, client, options) {
2539
+ if (client?.version === "v2") return listV2Sessions(client);
2540
+ return listSessionPage(port, client, options);
2541
+ }
2542
+ async function listAllSessions(port, client) {
2543
+ if (client?.version === "v2") {
2544
+ const sessions2 = await listV2Sessions(client);
2545
+ return sessions2 === null ? { sessions: null, reason: "opencode-unreachable" } : { sessions: sessions2 };
2546
+ }
2547
+ const sessions = await listSessionPage(port, client, {
2548
+ limit: FULL_SESSION_ENUMERATION_MAX_SESSIONS
2549
+ });
2550
+ if (sessions === null) return { sessions: null, reason: "opencode-unreachable" };
2551
+ if (sessions.length >= FULL_SESSION_ENUMERATION_MAX_SESSIONS) {
2552
+ return { sessions: null, reason: "session-enumeration-limit-bound" };
2553
+ }
2554
+ return { sessions };
2555
+ }
1894
2556
  async function deleteSession(port, id, client) {
1895
2557
  if (client?.version === "v2") return deleteV2Session(client, id);
1896
2558
  try {
1897
- const res = await requestWithClient(port, client, `/session/${id}`, { method: "DELETE" });
1898
- return res.status >= 200 && res.status < 300;
2559
+ const res = await requestWithClient(
2560
+ port,
2561
+ client,
2562
+ `/session/${id}`,
2563
+ { method: "DELETE" },
2564
+ { allowStatuses: ALL_HTTP_STATUSES }
2565
+ );
2566
+ return { ok: res.status >= 200 && res.status < 300, status: res.status };
1899
2567
  } catch (error2) {
1900
2568
  console.error(
1901
2569
  `[deleteSession] DELETE /session/${id} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
1902
2570
  );
1903
- return false;
2571
+ return { ok: false, status: "threw" };
1904
2572
  }
1905
2573
  }
1906
2574
  async function sessionExists(port, id, client) {
@@ -2138,11 +2806,31 @@ function messageText(m) {
2138
2806
  if (!m || !Array.isArray(m.parts)) return "";
2139
2807
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
2140
2808
  }
2141
- async function sendPromptAsync(port, sessionId, content, options, attachments, client) {
2809
+ function findDispatchedUserMessageId(messages, content, excludeIds) {
2810
+ if (!messages || messages.length === 0) return null;
2811
+ let best = null;
2812
+ for (const message of messages) {
2813
+ if (roleOf2(message) !== "user") continue;
2814
+ const id = idOf2(message);
2815
+ if (typeof id !== "string" || excludeIds?.has(id)) continue;
2816
+ if (messageText(message) !== content) continue;
2817
+ const created = createdOf(message) ?? 0;
2818
+ if (best === null || created > best.created) best = { id, created };
2819
+ }
2820
+ return best?.id ?? null;
2821
+ }
2822
+ async function sendPromptAsync(port, sessionId, content, options, attachments, client, callbacks) {
2142
2823
  const before = await getSessionMessages(port, sessionId, client);
2143
2824
  const knownUserIds = new Set(
2144
- (before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
2825
+ (before ?? []).filter((m) => roleOf2(m) === "user").map((m) => idOf2(m)).filter((id) => typeof id === "string")
2145
2826
  );
2827
+ if (callbacks) {
2828
+ try {
2829
+ callbacks.onKnownUserIds(before === null ? null : knownUserIds);
2830
+ } catch (error2) {
2831
+ callbacks.onKnownUserIdsError(error2);
2832
+ }
2833
+ }
2146
2834
  const parts = [{ type: "text", text: content }];
2147
2835
  let pendingOutcomes = null;
2148
2836
  if (attachments && attachments.inputs.length > 0) {
@@ -2181,22 +2869,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2181
2869
  const READ_BACK_DELAY_MS = 150;
2182
2870
  for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
2183
2871
  const after = await getSessionMessages(port, sessionId, client);
2184
- if (after) {
2185
- let best = null;
2186
- for (const m of after) {
2187
- if (roleOf(m) !== "user") continue;
2188
- const id = idOf(m);
2189
- if (typeof id !== "string" || knownUserIds.has(id)) continue;
2190
- if (messageText(m) !== content) continue;
2191
- const created = createdOf(m) ?? 0;
2192
- if (best === null || created > best.created) {
2193
- best = { id, created };
2194
- }
2195
- }
2196
- if (best) {
2197
- if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
2198
- return best.id;
2199
- }
2872
+ const dispatchedUserMessageId = findDispatchedUserMessageId(after, content, knownUserIds);
2873
+ if (dispatchedUserMessageId !== null) {
2874
+ if (pendingOutcomes && attachments?.onOutcomes) attachments.onOutcomes(pendingOutcomes);
2875
+ return dispatchedUserMessageId;
2200
2876
  }
2201
2877
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2202
2878
  await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
@@ -2207,57 +2883,68 @@ async function sendPromptAsync(port, sessionId, content, options, attachments, c
2207
2883
  function findAssistantReplyAfter(messages, userMessageId) {
2208
2884
  if (!messages || messages.length === 0) return null;
2209
2885
  const byParent = messages.find(
2210
- (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
2886
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2211
2887
  );
2212
2888
  if (byParent) return byParent;
2213
- const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
2889
+ const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2214
2890
  if (userIndex === -1) return null;
2215
2891
  for (let i = userIndex + 1; i < messages.length; i++) {
2216
- if (roleOf(messages[i]) === "assistant") return messages[i];
2892
+ if (roleOf2(messages[i]) === "assistant") return messages[i];
2217
2893
  }
2218
2894
  return null;
2219
2895
  }
2220
2896
  function findLastAssistantReplyFor(messages, userMessageId) {
2221
2897
  if (!messages || messages.length === 0) return null;
2222
2898
  let lastCorrelated = null;
2223
- let lastNonErrored = null;
2224
2899
  for (let i = messages.length - 1; i >= 0; i--) {
2225
2900
  const m = messages[i];
2226
- if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
2227
- if (lastCorrelated === null) lastCorrelated = m;
2228
- if (errorOf(m) == null) {
2229
- lastNonErrored = m;
2230
- break;
2901
+ if (roleOf2(m) !== "assistant" || parentIdOf2(m) !== userMessageId) continue;
2902
+ lastCorrelated = m;
2903
+ break;
2904
+ }
2905
+ if (lastCorrelated) {
2906
+ if (errorOf(lastCorrelated) == null) return lastCorrelated;
2907
+ for (let i = messages.length - 1; i >= 0; i--) {
2908
+ const m = messages[i];
2909
+ if (roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId && isTerminalStopAnswer(m)) {
2910
+ return m;
2911
+ }
2231
2912
  }
2913
+ return lastCorrelated;
2232
2914
  }
2233
- if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
2234
- const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
2915
+ const userIndex = messages.findIndex((m) => idOf2(m) === userMessageId);
2235
2916
  if (userIndex === -1) return null;
2236
2917
  let last = null;
2237
- let lastOk = null;
2918
+ let lastAssistantIndex = -1;
2238
2919
  for (let i = userIndex + 1; i < messages.length; i++) {
2239
- const role = roleOf(messages[i]);
2920
+ const role = roleOf2(messages[i]);
2240
2921
  if (role === "user") break;
2241
2922
  if (role === "assistant") {
2242
2923
  last = messages[i];
2243
- if (errorOf(messages[i]) == null) lastOk = messages[i];
2924
+ lastAssistantIndex = i;
2244
2925
  }
2245
2926
  }
2246
- return lastOk ?? last;
2927
+ if (last === null || errorOf(last) == null) return last;
2928
+ for (let i = lastAssistantIndex; i > userIndex; i--) {
2929
+ const m = messages[i];
2930
+ if (roleOf2(m) === "user") break;
2931
+ if (roleOf2(m) === "assistant" && isTerminalStopAnswer(m)) return m;
2932
+ }
2933
+ return last;
2247
2934
  }
2248
2935
  function collectSubagentSessions(messages, userMessageId) {
2249
2936
  if (!messages || messages.length === 0) return [];
2250
2937
  const byParent = messages.filter(
2251
- (message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
2938
+ (message) => roleOf2(message) === "assistant" && parentIdOf2(message) === userMessageId
2252
2939
  );
2253
2940
  const assistants = byParent.length > 0 ? byParent : [];
2254
2941
  if (assistants.length === 0) {
2255
- const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
2942
+ const userIndex = messages.findIndex((message) => idOf2(message) === userMessageId);
2256
2943
  if (userIndex === -1) return [];
2257
2944
  for (let i = userIndex + 1; i < messages.length; i++) {
2258
2945
  const message = messages[i];
2259
- if (roleOf(message) === "user") break;
2260
- if (roleOf(message) === "assistant") assistants.push(message);
2946
+ if (roleOf2(message) === "user") break;
2947
+ if (roleOf2(message) === "assistant") assistants.push(message);
2261
2948
  }
2262
2949
  }
2263
2950
  const refs = [];
@@ -2297,7 +2984,7 @@ function collectTaskCalls(messages, userMessageId) {
2297
2984
  if (!messages || messages.length === 0) return [];
2298
2985
  const calls = [];
2299
2986
  for (const message of messages) {
2300
- if (roleOf(message) !== "assistant" || parentIdOf(message) !== userMessageId) continue;
2987
+ if (roleOf2(message) !== "assistant" || parentIdOf2(message) !== userMessageId) continue;
2301
2988
  for (const part of message.parts ?? []) {
2302
2989
  if (part.tool !== "task" || !part.callID || !part.state || part.state.status === "pending") {
2303
2990
  continue;
@@ -2327,7 +3014,7 @@ function attributeTaskCallUsage(messages, windows) {
2327
3014
  for (const window of eligibleWindows) assignments.set(window.callID, []);
2328
3015
  const unattributed = [];
2329
3016
  for (const message of messages ?? []) {
2330
- if (roleOf(message) !== "assistant") continue;
3017
+ if (roleOf2(message) !== "assistant") continue;
2331
3018
  const created = finiteNumber(createdOf(message));
2332
3019
  const matching = created === null ? [] : eligibleWindows.filter(
2333
3020
  (window) => window.timeStart <= created && (window.timeEnd === null || window.timeEnd === void 0 || created <= window.timeEnd)
@@ -2349,8 +3036,9 @@ function attributeTaskCallUsage(messages, windows) {
2349
3036
  }
2350
3037
  function sumAssistantUsage(messages) {
2351
3038
  if (!messages || messages.length === 0) return null;
2352
- const nonErrored = messages.filter((message) => errorOf(message) == null);
2353
- const selected = nonErrored.length > 0 ? nonErrored : messages;
3039
+ const hasTrailingError = errorOf(messages[messages.length - 1]) != null;
3040
+ const hasTerminalStopAnswer = messages.some(isTerminalStopAnswer);
3041
+ const selected = hasTrailingError && hasTerminalStopAnswer ? messages.filter((message) => errorOf(message) == null) : messages;
2354
3042
  let sawAnyUsage = false;
2355
3043
  let inputSum = 0;
2356
3044
  let outputSum = 0;
@@ -2404,13 +3092,11 @@ function sumAssistantUsage(messages) {
2404
3092
  function messageUsage(messages, userMessageId) {
2405
3093
  if (!messages || messages.length === 0) return null;
2406
3094
  const byParentAll = messages.filter(
2407
- (m) => roleOf(m) === "assistant" && parentIdOf(m) === userMessageId
3095
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) === userMessageId
2408
3096
  );
2409
- const byParentNonErrored = byParentAll.filter((m) => errorOf(m) == null);
2410
- const byParent = byParentNonErrored.length > 0 ? byParentNonErrored : byParentAll;
2411
3097
  let correlated;
2412
- if (byParent.length > 0) {
2413
- correlated = byParent;
3098
+ if (byParentAll.length > 0) {
3099
+ correlated = byParentAll;
2414
3100
  } else {
2415
3101
  const reply = findAssistantReplyAfter(messages, userMessageId);
2416
3102
  correlated = reply ? [reply] : [];
@@ -2420,7 +3106,7 @@ function messageUsage(messages, userMessageId) {
2420
3106
  }
2421
3107
  function messageRunState(messages, userMessageId) {
2422
3108
  if (!messages || messages.length === 0) return "unknown";
2423
- const hasUser = messages.some((m) => idOf(m) === userMessageId);
3109
+ const hasUser = messages.some((m) => idOf2(m) === userMessageId);
2424
3110
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2425
3111
  if (!hasUser) {
2426
3112
  if (!reply) return "unknown";
@@ -2456,14 +3142,38 @@ function messageError(messages, userMessageId) {
2456
3142
  const reply = findLastAssistantReplyFor(messages, userMessageId);
2457
3143
  const error2 = errorOf(reply);
2458
3144
  if (error2 == null) return null;
2459
- if (typeof error2 === "string") return error2;
2460
- if (typeof error2 === "object") {
3145
+ let rendered;
3146
+ if (typeof error2 === "string") {
3147
+ rendered = error2;
3148
+ } else if (typeof error2 === "object") {
2461
3149
  const e = error2;
2462
3150
  const dataMessage = e.data?.message;
2463
- if (typeof dataMessage === "string") return dataMessage;
2464
- if (typeof e.message === "string") return e.message;
3151
+ if (typeof dataMessage === "string") {
3152
+ rendered = dataMessage;
3153
+ } else if (typeof e.message === "string") {
3154
+ rendered = e.message;
3155
+ } else {
3156
+ rendered = "The agent run failed.";
3157
+ }
3158
+ } else {
3159
+ rendered = "The agent run failed.";
2465
3160
  }
2466
- return "The agent run failed.";
3161
+ const toolPart = Array.isArray(reply?.parts) ? reply.parts.find(
3162
+ (part) => part && typeof part === "object" && part.type === "tool" && part.state?.status === "error"
3163
+ ) : void 0;
3164
+ return typeof toolPart?.tool === "string" && toolPart.tool.length > 0 ? `${rendered} (tool "${toolPart.tool}")` : rendered;
3165
+ }
3166
+ function describeAbandonedReplyShape(messages, userMessageId) {
3167
+ const reply = findLastAssistantReplyFor(messages, userMessageId);
3168
+ if (!reply) return null;
3169
+ const parts = Array.isArray(reply.parts) ? reply.parts : [];
3170
+ const lastTool = [...parts].reverse().find((part) => part?.type === "tool");
3171
+ return {
3172
+ partTypes: parts.slice(-12).map((part) => typeof part?.type === "string" ? part.type : "(unknown)"),
3173
+ droppedPartCount: Math.max(0, parts.length - 12),
3174
+ lastToolName: typeof lastTool?.tool === "string" && lastTool.tool.length > 0 ? lastTool.tool : null,
3175
+ lastToolStatus: typeof lastTool?.state?.status === "string" && lastTool.state.status.length > 0 ? lastTool.state.status : null
3176
+ };
2467
3177
  }
2468
3178
  function isAbortedTerminalReply(messages, userMessageId) {
2469
3179
  const reply = findLastAssistantReplyFor(messages, userMessageId);
@@ -2512,7 +3222,7 @@ function findLatestSubagentAuthOutcome(messages, sinceMs) {
2512
3222
  if (!messages || messages.length === 0) return null;
2513
3223
  for (let i = messages.length - 1; i >= 0; i--) {
2514
3224
  const message = messages[i];
2515
- if (roleOf(message) !== "assistant") continue;
3225
+ if (roleOf2(message) !== "assistant") continue;
2516
3226
  const created = createdOf(message);
2517
3227
  if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
2518
3228
  const failure = classifyReplyAuthError(message);
@@ -2544,25 +3254,25 @@ function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProvi
2544
3254
  function hasRunningAssistantExcept(messages, exceptUserMessageId) {
2545
3255
  if (!messages || messages.length === 0) return false;
2546
3256
  return messages.some(
2547
- (m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
3257
+ (m) => roleOf2(m) === "assistant" && parentIdOf2(m) !== exceptUserMessageId && isAssistantInFlight(m)
2548
3258
  );
2549
3259
  }
2550
3260
  function hasLaterSiblingTurnStarted(messages, userMessageId, siblingUserMessageIds) {
2551
3261
  if (!messages || messages.length === 0) return false;
2552
- const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3262
+ const userIndex = messages.findIndex((message) => idOf2(message) === userMessageId);
2553
3263
  if (userIndex === -1) return false;
2554
3264
  let hasLaterUser = false;
2555
3265
  let hasStartedLaterUser = false;
2556
3266
  for (let i = userIndex + 1; i < messages.length; i++) {
2557
3267
  const message = messages[i];
2558
- if (roleOf(message) !== "user") continue;
3268
+ if (roleOf2(message) !== "user") continue;
2559
3269
  hasLaterUser = true;
2560
- const laterUserMessageId = idOf(message);
3270
+ const laterUserMessageId = idOf2(message);
2561
3271
  if (laterUserMessageId === void 0 || !siblingUserMessageIds.has(laterUserMessageId)) {
2562
3272
  return false;
2563
3273
  }
2564
3274
  if (messages.some(
2565
- (candidate) => roleOf(candidate) === "assistant" && parentIdOf(candidate) === laterUserMessageId
3275
+ (candidate) => roleOf2(candidate) === "assistant" && parentIdOf2(candidate) === laterUserMessageId
2566
3276
  )) {
2567
3277
  hasStartedLaterUser = true;
2568
3278
  }
@@ -3033,13 +3743,17 @@ async function listV2Sessions(client) {
3033
3743
  }
3034
3744
  async function deleteV2Session(client, sessionId) {
3035
3745
  try {
3036
- await client.request(`/api/session/${encodeURIComponent(sessionId)}`, { method: "DELETE" });
3037
- return true;
3746
+ const response = await client.request(
3747
+ `/api/session/${encodeURIComponent(sessionId)}`,
3748
+ { method: "DELETE" },
3749
+ { allowStatuses: Array.from({ length: 500 }, (_, index) => index + 100) }
3750
+ );
3751
+ return { ok: response.status >= 200 && response.status < 300, status: response.status };
3038
3752
  } catch (error2) {
3039
3753
  console.error(
3040
3754
  `[deleteV2Session] DELETE /api/session/${sessionId} failed: ${error2 instanceof Error ? error2.message : String(error2)}`
3041
3755
  );
3042
- return false;
3756
+ return { ok: false, status: "threw" };
3043
3757
  }
3044
3758
  }
3045
3759
  async function v2SessionExists(client, sessionId) {
@@ -3214,7 +3928,7 @@ import { join as join4 } from "node:path";
3214
3928
 
3215
3929
  // src/lib/openai-usage.ts
3216
3930
  import { readFileSync as readFileSync3 } from "node:fs";
3217
- import { homedir as homedir2 } from "node:os";
3931
+ import { homedir as homedir3 } from "node:os";
3218
3932
  import { join as join3 } from "node:path";
3219
3933
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3220
3934
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
@@ -3229,7 +3943,7 @@ function isLocalCredentialProblem2(err) {
3229
3943
  }
3230
3944
  function readOpenCodeChatGptCredentials() {
3231
3945
  try {
3232
- const raw = readFileSync3(join3(homedir2(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3946
+ const raw = readFileSync3(join3(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3233
3947
  let parsed;
3234
3948
  try {
3235
3949
  parsed = JSON.parse(raw);
@@ -3370,16 +4084,17 @@ function hasPrimaryHeaders(headers) {
3370
4084
  ].some((name) => headers.has(name));
3371
4085
  }
3372
4086
  async function getOpenAiUsage(port, client) {
4087
+ const openCodeVersion = client?.version ?? "v1";
3373
4088
  const credentials2 = readOpenCodeChatGptCredentials();
3374
4089
  if (!credentials2) {
3375
4090
  throw new OpenAiUsageError(
3376
- "No ChatGPT login found. Connect a ChatGPT account to this runner, or run `opencode auth login`.",
4091
+ openCodeVersion === "v2" ? "OpenAI usage reporting is not available on OpenCode V2 because V2 keeps a connected ChatGPT login in a store Evident cannot read." : "No ChatGPT login found. Connect a ChatGPT account to this runner, or run `opencode auth login`.",
3377
4092
  "no_credentials"
3378
4093
  );
3379
4094
  }
3380
4095
  if (credentials2.expiresAt < Date.now()) {
3381
4096
  throw new OpenAiUsageError(
3382
- "ChatGPT credentials have expired. Run `opencode auth login` to refresh them.",
4097
+ openCodeVersion === "v2" ? "The delivered ChatGPT login has expired. OpenCode V2 cannot be given a replacement by file; connect the account again from the runner's page in Evident." : "ChatGPT credentials have expired. Run `opencode auth login` to refresh them.",
3383
4098
  "credentials_expired"
3384
4099
  );
3385
4100
  }
@@ -3660,9 +4375,9 @@ async function importOpenCodeV2Credentials({
3660
4375
 
3661
4376
  // src/lib/opencode/session-db-boot.ts
3662
4377
  import { spawn as spawn2 } from "node:child_process";
3663
- import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
3664
- import { homedir as homedir3 } from "node:os";
3665
- import { dirname as dirname2, resolve as resolvePath } from "node:path";
4378
+ import { mkdirSync, statSync as statSync3, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
4379
+ import { homedir as homedir4 } from "node:os";
4380
+ import { dirname as dirname3, resolve as resolvePath } from "node:path";
3666
4381
 
3667
4382
  // src/lib/runner-synchroniser.ts
3668
4383
  import { spawn } from "node:child_process";
@@ -3742,9 +4457,12 @@ function runSynchroniser(args, opts) {
3742
4457
 
3743
4458
  // src/lib/opencode/session-db-boot.ts
3744
4459
  var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
3745
- var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
4460
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 36e4;
3746
4461
  var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
3747
- var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
4462
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 40;
4463
+ var SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS = 30;
4464
+ var SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS = 120;
4465
+ var SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS = 90;
3748
4466
  function commandError(result) {
3749
4467
  return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
3750
4468
  }
@@ -3787,7 +4505,7 @@ function clearMarker(options) {
3787
4505
  function markNoReplicate(options, message) {
3788
4506
  if (options.noReplicateMarker) {
3789
4507
  try {
3790
- mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
4508
+ mkdirSync(dirname3(options.noReplicateMarker), { recursive: true });
3791
4509
  writeFileSync(options.noReplicateMarker, "");
3792
4510
  } catch (error2) {
3793
4511
  options.log(
@@ -3912,7 +4630,7 @@ async function ensureLitestreamConfig(options, env) {
3912
4630
  return null;
3913
4631
  }
3914
4632
  try {
3915
- if (statSync2(configPath).size > 0) return configPath;
4633
+ if (statSync3(configPath).size > 0) return configPath;
3916
4634
  } catch (error2) {
3917
4635
  if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
3918
4636
  options.log(
@@ -3942,7 +4660,7 @@ async function ensureLitestreamConfig(options, env) {
3942
4660
  return null;
3943
4661
  }
3944
4662
  try {
3945
- mkdirSync(dirname2(configPath), { recursive: true });
4663
+ mkdirSync(dirname3(configPath), { recursive: true });
3946
4664
  writeFileSync(configPath, rendered.stdout);
3947
4665
  } catch (error2) {
3948
4666
  options.log(
@@ -4017,14 +4735,14 @@ async function restoreSessionDb(options, configPath, env) {
4017
4735
  case 0:
4018
4736
  return;
4019
4737
  case 31:
4020
- acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir3(), env));
4738
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir4(), env));
4021
4739
  options.log(
4022
4740
  "SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
4023
4741
  "warn"
4024
4742
  );
4025
4743
  return;
4026
4744
  case 32:
4027
- acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir3(), env));
4745
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir4(), env));
4028
4746
  discardSessionDbDebris(options);
4029
4747
  markNoReplicate(
4030
4748
  options,
@@ -4055,20 +4773,37 @@ async function verifySessionDb(options, configPath, env) {
4055
4773
  timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
4056
4774
  env: {
4057
4775
  ...env,
4058
- // The synchroniser reads this value in SECONDS. Keep this at 120, not
4059
- // 120_000, so the walkback gives up before the outer process bound.
4776
+ // The synchroniser reads all four values in SECONDS.
4060
4777
  EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
4061
4778
  SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
4779
+ ),
4780
+ EVIDENT_SESSION_DB_RESTORE_POINTS_TIMEOUT_SECONDS: String(
4781
+ SESSION_DB_VERIFY_RESTORE_POINTS_TIMEOUT_SECONDS
4782
+ ),
4783
+ EVIDENT_SESSION_DB_RESTORE_CANDIDATE_TIMEOUT_SECONDS: String(
4784
+ SESSION_DB_VERIFY_RESTORE_CANDIDATE_TIMEOUT_SECONDS
4785
+ ),
4786
+ EVIDENT_SESSION_DB_DISPOSAL_BUDGET_SECONDS: String(
4787
+ SESSION_DB_VERIFY_DISPOSAL_BUDGET_SECONDS
4062
4788
  )
4063
4789
  }
4064
4790
  });
4065
4791
  logSynchroniserDiagnostics(result, options);
4066
4792
  if (result.timedOut || result.code === 124 || result.code === 137) {
4793
+ if (result.stderr.includes("SESSION-DB-INTEGRITY-FAILED")) {
4794
+ const reason = result.stderr.includes("SESSION-DB-REPLICA-SIZE") ? "verification_timeout_during_separation" : "verification_timeout_before_separation";
4795
+ options.log(
4796
+ `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})`,
4797
+ "error"
4798
+ );
4799
+ reportRecord("verify", "session_db_boot_refused", reason, null, options);
4800
+ return { verifyFatal: true, verifyFatalReason: reason };
4801
+ }
4067
4802
  options.log(
4068
4803
  `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`,
4069
4804
  "warn"
4070
4805
  );
4071
- return false;
4806
+ return { verifyFatal: false };
4072
4807
  }
4073
4808
  if (result.code === 34) {
4074
4809
  reportRecord(
@@ -4078,14 +4813,14 @@ async function verifySessionDb(options, configPath, env) {
4078
4813
  null,
4079
4814
  options
4080
4815
  );
4081
- return true;
4816
+ return { verifyFatal: true };
4082
4817
  }
4083
4818
  if (result.code === 33) {
4084
4819
  options.log(
4085
4820
  "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
4086
4821
  "warn"
4087
4822
  );
4088
- return false;
4823
+ return { verifyFatal: false };
4089
4824
  }
4090
4825
  if (result.code !== 0) {
4091
4826
  options.log(
@@ -4093,17 +4828,17 @@ async function verifySessionDb(options, configPath, env) {
4093
4828
  "warn"
4094
4829
  );
4095
4830
  }
4096
- return false;
4831
+ return { verifyFatal: false };
4097
4832
  }
4098
4833
  options.log(
4099
4834
  "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
4100
4835
  "debug"
4101
4836
  );
4102
- return false;
4837
+ return { verifyFatal: false };
4103
4838
  }
4104
4839
  function fileExists(path) {
4105
4840
  try {
4106
- statSync2(path);
4841
+ statSync3(path);
4107
4842
  return true;
4108
4843
  } catch (error2) {
4109
4844
  if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
@@ -4113,7 +4848,7 @@ function fileExists(path) {
4113
4848
  async function restoreAndVerifySessionDb(options) {
4114
4849
  const env = options.env ?? process.env;
4115
4850
  clearMarker(options);
4116
- acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir3(), env));
4851
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir4(), env));
4117
4852
  const synchroniserEnv = await runSynchroniser(["env"], {
4118
4853
  timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
4119
4854
  env
@@ -4186,13 +4921,13 @@ async function restoreAndVerifySessionDb(options) {
4186
4921
  if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
4187
4922
  return { verifyFatal: false };
4188
4923
  }
4189
- return { verifyFatal: await verifySessionDb(options, configPath, env) };
4924
+ return verifySessionDb(options, configPath, env);
4190
4925
  }
4191
4926
 
4192
4927
  // src/lib/opencode/session-db-provenance.ts
4193
4928
  import { createRequire } from "node:module";
4194
4929
  import { mkdirSync as mkdirSync2, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "node:fs";
4195
- import { dirname as dirname3, join as join5 } from "node:path";
4930
+ import { dirname as dirname4, join as join5 } from "node:path";
4196
4931
  var require2 = createRequire(import.meta.url);
4197
4932
  function readSessionDbMigrationIds(dbPath) {
4198
4933
  let db;
@@ -4258,7 +4993,7 @@ function loadSessionDbProvenanceState(path) {
4258
4993
  }
4259
4994
  function saveSessionDbProvenanceState(path, state) {
4260
4995
  try {
4261
- mkdirSync2(dirname3(path), { recursive: true });
4996
+ mkdirSync2(dirname4(path), { recursive: true });
4262
4997
  writeFileSync2(path, `${JSON.stringify(state, null, 2)}
4263
4998
  `, "utf8");
4264
4999
  } catch (error2) {
@@ -4839,7 +5574,10 @@ function selectSessionsToDelete(sessions, opts) {
4839
5574
  toDelete.push(s.id);
4840
5575
  }
4841
5576
  }
4842
- return toDelete;
5577
+ const activityById = new Map(sessions.map((session) => [session.id, session.lastActivityMs]));
5578
+ return toDelete.sort(
5579
+ (a, b) => (activityById.get(a) ?? -Infinity) - (activityById.get(b) ?? -Infinity)
5580
+ );
4843
5581
  }
4844
5582
  var DEFAULT_INTERVAL = "1h";
4845
5583
  function resolve(flag, envValue, fallback) {
@@ -4899,13 +5637,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
4899
5637
  }
4900
5638
 
4901
5639
  // src/lib/opencode/session-db-size.ts
4902
- import { statSync as statSync3 } from "node:fs";
5640
+ import { statSync as statSync4 } from "node:fs";
4903
5641
  import { join as join6 } from "node:path";
4904
5642
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
4905
5643
  function statSessionDbBytes(homeDir) {
4906
5644
  const dbPath = join6(homeDir, ".local", "share", "opencode", "opencode.db");
4907
5645
  try {
4908
- return statSync3(dbPath).size;
5646
+ return statSync4(dbPath).size;
4909
5647
  } catch (err) {
4910
5648
  const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
4911
5649
  if (!isMissingFile) {
@@ -4931,12 +5669,12 @@ function buildSessionStoreSizeWarning(input) {
4931
5669
  }
4932
5670
 
4933
5671
  // src/lib/opencode/log-tail.ts
4934
- import { statSync as statSync4 } from "node:fs";
4935
- import { homedir as homedir4 } from "node:os";
5672
+ import { statSync as statSync5 } from "node:fs";
5673
+ import { homedir as homedir5 } from "node:os";
4936
5674
  import { join as join7 } from "node:path";
4937
5675
  import { open as open2, stat } from "node:fs/promises";
4938
5676
  var DEFAULT_POLL_INTERVAL_MS = 1e3;
4939
- function resolveOpenCodeLogPath(homeDir = homedir4(), env = process.env) {
5677
+ function resolveOpenCodeLogPath(homeDir = homedir5(), env = process.env) {
4940
5678
  const dataDir = env.XDG_DATA_HOME || join7(homeDir, ".local", "share");
4941
5679
  return join7(dataDir, "opencode", "log", "opencode.log");
4942
5680
  }
@@ -4954,7 +5692,7 @@ function tailOpenCodeLogFile(logPath, onChunk, opts = {}) {
4954
5692
  let inode = null;
4955
5693
  let baselineReady = true;
4956
5694
  try {
4957
- const initial = statSync4(logPath);
5695
+ const initial = statSync5(logPath);
4958
5696
  offset = initial.size;
4959
5697
  inode = initial.ino;
4960
5698
  } catch (error2) {
@@ -4971,165 +5709,46 @@ function tailOpenCodeLogFile(logPath, onChunk, opts = {}) {
4971
5709
  try {
4972
5710
  let current;
4973
5711
  try {
4974
- current = await stat(logPath);
4975
- } catch (error2) {
4976
- if (!isEnoent(error2)) reportFailure("stat", logPath, error2);
4977
- return;
4978
- }
4979
- if (!baselineReady) {
4980
- offset = current.size;
4981
- inode = current.ino;
4982
- baselineReady = true;
4983
- return;
4984
- }
4985
- if (inode !== null && current.ino !== inode || current.size < offset) {
4986
- offset = 0;
4987
- }
4988
- inode = current.ino;
4989
- if (current.size === offset) return;
4990
- const length = current.size - offset;
4991
- const fh = await open2(logPath, "r");
4992
- try {
4993
- const buf = Buffer.alloc(length);
4994
- const { bytesRead } = await fh.read(buf, 0, length, offset);
4995
- offset += bytesRead;
4996
- if (bytesRead > 0) onChunk(buf.subarray(0, bytesRead));
4997
- } finally {
4998
- await fh.close();
4999
- }
5000
- } catch (error2) {
5001
- if (!isEnoent(error2)) reportFailure("poll", logPath, error2);
5002
- } finally {
5003
- polling = false;
5004
- }
5005
- };
5006
- const interval = setInterval(() => void poll(), pollIntervalMs);
5007
- void poll();
5008
- return {
5009
- stop: () => {
5010
- stopped = true;
5011
- clearInterval(interval);
5012
- }
5013
- };
5014
- }
5015
-
5016
- // src/lib/opencode/session-db-reclaim.ts
5017
- import { statSync as statSync5, statfsSync } from "node:fs";
5018
- import { dirname as dirname4 } from "node:path";
5019
- function errorMessage(error2) {
5020
- if (!(error2 instanceof Error)) return String(error2);
5021
- return error2.cause instanceof Error ? error2.cause.message : error2.message;
5022
- }
5023
- function insufficientSpaceReason(dbPath, requiredBytes) {
5024
- try {
5025
- const fsStats = statfsSync(dirname4(dbPath));
5026
- const availableBytes = fsStats.bavail * fsStats.bsize;
5027
- if (availableBytes < requiredBytes) {
5028
- return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
5029
- }
5030
- return null;
5031
- } catch (err) {
5032
- return `could not check free space (${err instanceof Error ? err.message : String(err)}); refusing to guess`;
5033
- }
5034
- }
5035
- function readLogicalBytes(db) {
5036
- const pageCount = db.prepare("PRAGMA page_count").get().page_count;
5037
- const pageSize = db.prepare("PRAGMA page_size").get().page_size;
5038
- return pageCount * pageSize;
5039
- }
5040
- function readCheckpointResult(db) {
5041
- const row = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
5042
- return { busy: row.busy !== 0, log: row.log, checkpointed: row.checkpointed };
5043
- }
5044
- async function probeReclaimAvailability(input) {
5045
- const { dbPath, requiredBytes } = input;
5046
- let sqlite;
5047
- try {
5048
- sqlite = await import("node:sqlite");
5049
- } catch (err) {
5050
- const detail = `Node ${process.version}: ${errorMessage(err)}`;
5051
- console.warn(`[probeReclaimAvailability] node:sqlite unavailable for ${dbPath}: ` + detail);
5052
- return { reason: "sqlite-unavailable", detail };
5053
- }
5054
- let autoVacuum = null;
5055
- try {
5056
- const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
5057
- try {
5058
- db.exec("PRAGMA busy_timeout=5000");
5059
- autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
5060
- } finally {
5061
- db.close();
5062
- }
5063
- } catch (err) {
5064
- console.warn(
5065
- `[probeReclaimAvailability] could not read auto_vacuum mode for ${dbPath}: ${err instanceof Error ? err.message : String(err)}`
5066
- );
5067
- }
5068
- if (autoVacuum !== 0) return null;
5069
- return insufficientSpaceReason(dbPath, requiredBytes) !== null ? { reason: "insufficient-disk-space" } : null;
5070
- }
5071
- async function reclaimSessionDbSpace(input) {
5072
- const { dbPath, maxPages, allowFullVacuum = true } = input;
5073
- let sqlite;
5074
- try {
5075
- sqlite = await import("node:sqlite");
5076
- } catch (err) {
5077
- const detail = `Node ${process.version}: ${errorMessage(err)}`;
5078
- console.warn(
5079
- `[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${detail}`
5080
- );
5081
- return { ok: false, skipped: "sqlite-unavailable", detail };
5082
- }
5083
- const { DatabaseSync } = sqlite;
5084
- let db;
5085
- try {
5086
- db = new DatabaseSync(dbPath);
5087
- db.exec("PRAGMA busy_timeout=5000");
5088
- const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
5089
- if (autoVacuum === 0) {
5090
- if (!allowFullVacuum) {
5091
- console.warn(
5092
- `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: a session turn is live`
5093
- );
5094
- return { ok: false, skipped: "full-vacuum-blocked" };
5712
+ current = await stat(logPath);
5713
+ } catch (error2) {
5714
+ if (!isEnoent(error2)) reportFailure("stat", logPath, error2);
5715
+ return;
5095
5716
  }
5096
- const fileBytesForGuard = statSync5(dbPath).size;
5097
- const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
5098
- if (skipReason !== null) {
5099
- console.warn(
5100
- `[reclaimSessionDbSpace] skipping VACUUM conversion of ${dbPath}: ${skipReason}`
5101
- );
5102
- return { ok: false, skipped: "insufficient-disk-space" };
5717
+ if (!baselineReady) {
5718
+ offset = current.size;
5719
+ inode = current.ino;
5720
+ baselineReady = true;
5721
+ return;
5103
5722
  }
5104
- const beforeBytes = readLogicalBytes(db);
5105
- db.exec("PRAGMA auto_vacuum=INCREMENTAL");
5106
- db.exec("VACUUM");
5107
- const afterBytes = readLogicalBytes(db);
5108
- const checkpoint = readCheckpointResult(db);
5109
- return { ok: true, mode: "convert", beforeBytes, afterBytes, checkpoint };
5723
+ if (inode !== null && current.ino !== inode || current.size < offset) {
5724
+ offset = 0;
5725
+ }
5726
+ inode = current.ino;
5727
+ if (current.size === offset) return;
5728
+ const length = current.size - offset;
5729
+ const fh = await open2(logPath, "r");
5730
+ try {
5731
+ const buf = Buffer.alloc(length);
5732
+ const { bytesRead } = await fh.read(buf, 0, length, offset);
5733
+ offset += bytesRead;
5734
+ if (bytesRead > 0) onChunk(buf.subarray(0, bytesRead));
5735
+ } finally {
5736
+ await fh.close();
5737
+ }
5738
+ } catch (error2) {
5739
+ if (!isEnoent(error2)) reportFailure("poll", logPath, error2);
5740
+ } finally {
5741
+ polling = false;
5110
5742
  }
5111
- if (autoVacuum === 2) {
5112
- const beforeBytes = readLogicalBytes(db);
5113
- const bound = Math.max(0, Math.trunc(maxPages));
5114
- db.exec(`PRAGMA incremental_vacuum(${bound})`);
5115
- const afterBytes = readLogicalBytes(db);
5116
- const checkpoint = readCheckpointResult(db);
5117
- return { ok: true, mode: "incremental", beforeBytes, afterBytes, checkpoint };
5743
+ };
5744
+ const interval = setInterval(() => void poll(), pollIntervalMs);
5745
+ void poll();
5746
+ return {
5747
+ stop: () => {
5748
+ stopped = true;
5749
+ clearInterval(interval);
5118
5750
  }
5119
- console.warn(
5120
- `[reclaimSessionDbSpace] ${dbPath} has auto_vacuum=${autoVacuum} (neither NONE nor INCREMENTAL); nothing to reclaim`
5121
- );
5122
- return { ok: false, skipped: "auto-vacuum-not-applicable" };
5123
- } catch (err) {
5124
- console.error(`[reclaimSessionDbSpace] reclaim failed for ${dbPath}: ` + errorMessage(err));
5125
- return {
5126
- ok: false,
5127
- skipped: "reclaim-error",
5128
- detail: errorMessage(err)
5129
- };
5130
- } finally {
5131
- db?.close();
5132
- }
5751
+ };
5133
5752
  }
5134
5753
 
5135
5754
  // src/lib/tunnel/connection.ts
@@ -5361,6 +5980,8 @@ var StreamForwarder = class {
5361
5980
  if (done) break;
5362
5981
  const chunk = Buffer.from(value);
5363
5982
  for (let i = 0; i < chunk.length; i += MAX_FRAME_BYTES) {
5983
+ await stream.pausePromise;
5984
+ if (ac.signal.aborted) break;
5364
5985
  const slice = chunk.subarray(i, i + MAX_FRAME_BYTES);
5365
5986
  if (this.binaryFramesSupported) {
5366
5987
  if (this.ws.readyState === WebSocket.OPEN) {
@@ -5403,6 +6024,7 @@ function classifyUpgradeRejection(headers) {
5403
6024
  }
5404
6025
  var MAX_RECONNECT_DELAY = 3e4;
5405
6026
  var BASE_RECONNECT_DELAY = 500;
6027
+ var RELAY_SILENCE_TIMEOUT_MS = 9e4;
5406
6028
  function getReconnectDelay(attempt) {
5407
6029
  const exponentialDelay = BASE_RECONNECT_DELAY * Math.pow(2, attempt);
5408
6030
  const jitter = Math.random() * 1e3;
@@ -5474,10 +6096,27 @@ function connectTunnel(options) {
5474
6096
  ws.close();
5475
6097
  reject(new Error("Connection timeout"));
5476
6098
  }, 3e4);
6099
+ let relaySilenceTimeout;
6100
+ let relaySilenceTimedOut = false;
6101
+ const clearRelaySilenceTimeout = () => {
6102
+ if (relaySilenceTimeout === void 0) return;
6103
+ clearTimeout(relaySilenceTimeout);
6104
+ relaySilenceTimeout = void 0;
6105
+ };
6106
+ const armRelaySilenceTimeout = () => {
6107
+ clearRelaySilenceTimeout();
6108
+ relaySilenceTimeout = setTimeout(() => {
6109
+ relaySilenceTimeout = void 0;
6110
+ relaySilenceTimedOut = true;
6111
+ ws.terminate();
6112
+ }, RELAY_SILENCE_TIMEOUT_MS);
6113
+ relaySilenceTimeout.unref?.();
6114
+ };
5477
6115
  let upgradeRejection = null;
5478
6116
  let upgradeRejectionReason = null;
5479
6117
  ws.on("unexpected-response", (_req, res) => {
5480
6118
  clearTimeout(connectionTimeout);
6119
+ clearRelaySilenceTimeout();
5481
6120
  const reason = classifyUpgradeRejection(res.headers);
5482
6121
  upgradeRejectionReason = reason;
5483
6122
  const chunks = [];
@@ -5507,6 +6146,7 @@ function connectTunnel(options) {
5507
6146
  onInfo?.("WebSocket connection established");
5508
6147
  });
5509
6148
  ws.on("message", (data, isBinary) => {
6149
+ if (relaySilenceTimeout !== void 0) armRelaySilenceTimeout();
5510
6150
  if (isBinary) {
5511
6151
  try {
5512
6152
  const frame = decodeBinaryBodyFrame(toUint8Array(data));
@@ -5540,6 +6180,7 @@ function connectTunnel(options) {
5540
6180
  switch (message.type) {
5541
6181
  case "connected": {
5542
6182
  clearTimeout(connectionTimeout);
6183
+ armRelaySilenceTimeout();
5543
6184
  forwarder.setBinaryFramesSupported(message.binary_frames === true);
5544
6185
  const connectedAgentId = message.agent_id ?? agentId;
5545
6186
  onConnected?.(connectedAgentId);
@@ -5564,6 +6205,7 @@ function connectTunnel(options) {
5564
6205
  });
5565
6206
  ws.on("error", (error2) => {
5566
6207
  clearTimeout(connectionTimeout);
6208
+ clearRelaySilenceTimeout();
5567
6209
  const detail = upgradeRejection ?? describeSocketError(error2, url);
5568
6210
  if (upgradeRejectionReason === "do_code_updated") {
5569
6211
  onWarning?.("Relay redeployed \u2014 reconnecting");
@@ -5575,7 +6217,8 @@ function connectTunnel(options) {
5575
6217
  );
5576
6218
  });
5577
6219
  ws.on("close", (code, reason) => {
5578
- const reasonStr = reason.toString() || upgradeRejection || (code === 1006 ? "abnormal closure" : "No reason provided");
6220
+ clearRelaySilenceTimeout();
6221
+ const reasonStr = (relaySilenceTimedOut ? "no relay heartbeat for 90s \u2014 reconnecting" : void 0) || reason.toString() || upgradeRejection || (code === 1006 ? "abnormal closure" : "No reason provided");
5579
6222
  forwarder.abortAll();
5580
6223
  onDisconnected?.(code, reasonStr);
5581
6224
  });
@@ -5583,9 +6226,10 @@ function connectTunnel(options) {
5583
6226
  }
5584
6227
 
5585
6228
  // src/lib/tunnel/runner-connection.ts
5586
- var RunnerConnection = class {
6229
+ var RunnerConnection = class _RunnerConnection {
5587
6230
  opts;
5588
6231
  sleep;
6232
+ now;
5589
6233
  connection = null;
5590
6234
  resolvedAgentId;
5591
6235
  /** True while a (re)connect loop is in flight. */
@@ -5594,10 +6238,17 @@ var RunnerConnection = class {
5594
6238
  reconnectPromise = null;
5595
6239
  /** 1-based count of the current reconnect attempt streak. */
5596
6240
  reconnectAttempt = 0;
6241
+ generation = 0;
6242
+ disconnectedAtMs = null;
6243
+ static RELAY_RECONNECT_CLOSE_REASONS = /* @__PURE__ */ new Set([
6244
+ "No pong received",
6245
+ "Orphaned socket"
6246
+ ]);
5597
6247
  constructor(opts) {
5598
6248
  this.opts = opts;
5599
6249
  this.resolvedAgentId = opts.agentId;
5600
6250
  this.sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
6251
+ this.now = opts.now ?? Date.now;
5601
6252
  }
5602
6253
  get agentId() {
5603
6254
  return this.resolvedAgentId;
@@ -5633,14 +6284,24 @@ var RunnerConnection = class {
5633
6284
  port: this.opts.port,
5634
6285
  openCodePassword: this.opts.openCodePassword,
5635
6286
  onConnected: (agentId) => {
6287
+ const attempt = this.reconnectAttempt + 1;
5636
6288
  this.reconnectAttempt = 0;
5637
6289
  this.reconnecting = false;
5638
6290
  this.resolvedAgentId = agentId;
5639
- events.onConnected(agentId, isReconnect);
6291
+ this.generation++;
6292
+ const downtimeMs = this.disconnectedAtMs === null ? null : this.now() - this.disconnectedAtMs;
6293
+ this.disconnectedAtMs = null;
6294
+ events.onConnected(agentId, isReconnect, {
6295
+ generation: this.generation,
6296
+ attempt,
6297
+ downtimeMs
6298
+ });
5640
6299
  },
5641
6300
  onDisconnected: (code, reason) => {
5642
- events.onDisconnected(code, reason);
5643
- if (this.opts.isRunning() && code !== 1e3 && !this.reconnecting) {
6301
+ this.disconnectedAtMs = this.now();
6302
+ events.onDisconnected(code, reason, { generation: this.generation });
6303
+ const relayRequestedReconnect = code === 1e3 && _RunnerConnection.RELAY_RECONNECT_CLOSE_REASONS.has(reason);
6304
+ if (this.opts.isRunning() && (code !== 1e3 || relayRequestedReconnect) && !this.reconnecting) {
5644
6305
  this.reconnectPromise = this.connectWithRetry(true).catch((err) => {
5645
6306
  events.onError?.(`Reconnection failed: ${err.message}`);
5646
6307
  });
@@ -5979,8 +6640,103 @@ function createResourceUsageCollector(homeDir) {
5979
6640
  };
5980
6641
  }
5981
6642
 
6643
+ // src/lib/resource-usage-low-disk.ts
6644
+ var RUNNER_DISK_ALERT_ENTRY_FREE_BYTES = 4 * 1024 ** 3;
6645
+ var RUNNER_DISK_ALERT_ENTRY_FREE_FRACTION = 0.25;
6646
+ var RUNNER_DISK_ALERT_VERY_LOW_FREE_BYTES = 1 * 1024 ** 3;
6647
+ var RUNNER_DISK_ALERT_CRITICAL_FREE_BYTES = 256 * 1024 ** 2;
6648
+ var RUNNER_DISK_ALERT_RECOVERY_FREE_BYTES = 6 * 1024 ** 3;
6649
+ var RUNNER_DISK_ALERT_RECOVERY_FREE_FRACTION = 0.375;
6650
+ var LOCAL_DISK_WARNING_REASSERTION_INTERVAL_MS = 24 * 60 * 60 * 1e3;
6651
+ var BAND_RANK = {
6652
+ low: 1,
6653
+ very_low: 2,
6654
+ critical: 3
6655
+ };
6656
+ function bandFor(freeBytes) {
6657
+ if (freeBytes < RUNNER_DISK_ALERT_CRITICAL_FREE_BYTES) return "critical";
6658
+ if (freeBytes < RUNNER_DISK_ALERT_VERY_LOW_FREE_BYTES) return "very_low";
6659
+ return "low";
6660
+ }
6661
+ function formatBytes(bytes) {
6662
+ if (bytes === null) return "unavailable";
6663
+ const units = ["B", "KiB", "MiB", "GiB", "TiB"];
6664
+ const unitIndex = Math.min(Math.floor(Math.log2(Math.max(bytes, 1)) / 10), units.length - 1);
6665
+ const value = bytes / 1024 ** unitIndex;
6666
+ const digits = unitIndex === 0 || value >= 100 ? 0 : value >= 10 ? 1 : 2;
6667
+ return `${value.toFixed(digits)} ${units[unitIndex]}`;
6668
+ }
6669
+ function warningMessage(band, diskFreeBytes, diskTotalBytes, opencodeDbBytes) {
6670
+ return `Low disk warning (${band} severity): ${formatBytes(diskFreeBytes)} free of ${formatBytes(diskTotalBytes)}; opencode.db is ${formatBytes(opencodeDbBytes)}. Clear space on the machine running this runner before session history or new work can no longer be saved.`;
6671
+ }
6672
+ function evaluateLowDiskWarning(input) {
6673
+ const { diskFreeBytes, diskTotalBytes, opencodeDbBytes, nowMs, state } = input;
6674
+ if (diskFreeBytes === null || diskTotalBytes === null || diskTotalBytes <= 0 || diskFreeBytes >= diskTotalBytes) {
6675
+ return { warning: null, state };
6676
+ }
6677
+ const recovered = diskFreeBytes >= RUNNER_DISK_ALERT_RECOVERY_FREE_BYTES || diskFreeBytes >= diskTotalBytes * RUNNER_DISK_ALERT_RECOVERY_FREE_FRACTION;
6678
+ if (recovered) {
6679
+ return { warning: null, state: { worstBand: null, warnedAtMs: null } };
6680
+ }
6681
+ const low = diskFreeBytes < RUNNER_DISK_ALERT_ENTRY_FREE_BYTES && diskFreeBytes < diskTotalBytes * RUNNER_DISK_ALERT_ENTRY_FREE_FRACTION;
6682
+ if (!low) return { warning: null, state };
6683
+ const currentBand = bandFor(diskFreeBytes);
6684
+ const currentRank = BAND_RANK[currentBand];
6685
+ const previousRank = state.worstBand === null ? 0 : BAND_RANK[state.worstBand];
6686
+ const worse = currentRank > previousRank;
6687
+ const reassert = state.warnedAtMs !== null && nowMs - state.warnedAtMs >= LOCAL_DISK_WARNING_REASSERTION_INTERVAL_MS;
6688
+ const nextState = {
6689
+ worstBand: worse ? currentBand : state.worstBand ?? currentBand,
6690
+ warnedAtMs: worse || state.warnedAtMs === null || reassert ? nowMs : state.warnedAtMs
6691
+ };
6692
+ return {
6693
+ warning: worse || state.warnedAtMs === null || reassert ? warningMessage(nextState.worstBand, diskFreeBytes, diskTotalBytes, opencodeDbBytes) : null,
6694
+ state: nextState
6695
+ };
6696
+ }
6697
+
6698
+ // src/lib/opencode/session-store-health.ts
6699
+ var SESSION_STORE_HEALTH_STALENESS_MS = 5 * 6e4;
6700
+ function createSessionStoreHealth(options = {}) {
6701
+ const now = options.now ?? (() => Date.now());
6702
+ const stalenessMs = Math.max(0, options.stalenessMs ?? SESSION_STORE_HEALTH_STALENESS_MS);
6703
+ let lastUnwritableAt = null;
6704
+ let lastWritableAt = null;
6705
+ const causes = /* @__PURE__ */ new Map();
6706
+ return {
6707
+ observeUnwritable: () => {
6708
+ lastUnwritableAt = now();
6709
+ },
6710
+ observeWritable: () => {
6711
+ lastWritableAt = now();
6712
+ },
6713
+ isUnwritable: () => {
6714
+ if (lastUnwritableAt === null) return false;
6715
+ if (lastWritableAt !== null && lastWritableAt >= lastUnwritableAt) return false;
6716
+ return now() - lastUnwritableAt < stalenessMs;
6717
+ },
6718
+ observeCause: (sessionId, cause) => {
6719
+ if (sessionId === null) return;
6720
+ causes.delete(sessionId);
6721
+ causes.set(sessionId, { cause, observedAt: now() });
6722
+ while (causes.size > 256) {
6723
+ const oldest = causes.keys().next().value;
6724
+ if (typeof oldest !== "string") break;
6725
+ causes.delete(oldest);
6726
+ }
6727
+ },
6728
+ readCause: (sessionId) => {
6729
+ const observed = causes.get(sessionId);
6730
+ if (!observed) return null;
6731
+ causes.delete(sessionId);
6732
+ if (now() - observed.observedAt >= stalenessMs) return null;
6733
+ return observed.cause;
6734
+ }
6735
+ };
6736
+ }
6737
+
5982
6738
  // src/lib/channels/driver.ts
5983
- import { homedir as homedir5 } from "node:os";
6739
+ import { homedir as homedir6 } from "node:os";
5984
6740
 
5985
6741
  // src/lib/runner-file-sync.ts
5986
6742
  import { join as join9 } from "node:path";
@@ -6415,6 +7171,11 @@ function messageIdOf(m) {
6415
7171
  const infoId = m.info?.id;
6416
7172
  return typeof infoId === "string" ? infoId : void 0;
6417
7173
  }
7174
+ function composeAbandonmentNote(finish, replyShape, outcome) {
7175
+ 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.");
7176
+ if (replyShape === null) return base;
7177
+ 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)` : ""}.`;
7178
+ }
6418
7179
  function cleanImageMime(contentType) {
6419
7180
  if (!contentType) return null;
6420
7181
  const media = contentType.split(";")[0].trim().toLowerCase();
@@ -6435,8 +7196,13 @@ var SUBAGENT_TELEMETRY_FETCH_TIMEOUT_MS = 2e3;
6435
7196
  var SESSION_ERROR_STREAM_HEALTHY_MS = 5e3;
6436
7197
  var SESSION_ERROR_BUFFER_TTL_MS = SESSION_ERROR_STREAM_HEALTHY_MS;
6437
7198
  var MAX_BUFFERED_SESSION_ERRORS = 256;
7199
+ var SESSION_ERROR_RETENTION_TTL_MS = 6e4;
7200
+ 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.";
7201
+ var SESSION_ERROR_RETAINED_DETAIL = "The runner's session database could not be written, so this turn stopped part-way. Runner-observed session error: ";
7202
+ var SESSION_ERROR_OBSERVED_CAUSE = "Runner-observed OpenCode session-store cause: ";
6438
7203
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
6439
7204
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
7205
+ var DRIVER_SESSION_ENUMERATION_LIMIT = 20;
6440
7206
  var DEFAULT_STUCK_QUEUED_MS = 6e4;
6441
7207
  var HEARTBEAT_MS = 6e4;
6442
7208
  var ALIVE_WITHHELD_THRESHOLD_MS = 3 * HEARTBEAT_MS;
@@ -6498,6 +7264,7 @@ var ChannelDriver = class _ChannelDriver {
6498
7264
  maxActiveSessions;
6499
7265
  watcherStallMs;
6500
7266
  wedgeWarningIntervalMs;
7267
+ sessionStoreHealth;
6501
7268
  openCodeClient;
6502
7269
  /** Cache of conversationId → opencode sessionId. */
6503
7270
  sessions = /* @__PURE__ */ new Map();
@@ -6590,6 +7357,7 @@ var ChannelDriver = class _ChannelDriver {
6590
7357
  * with a short TTL so an unmatched session cannot retain an event indefinitely.
6591
7358
  */
6592
7359
  bufferedSessionErrors = /* @__PURE__ */ new Map();
7360
+ retainedSessionErrors = /* @__PURE__ */ new Map();
6593
7361
  /**
6594
7362
  * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
6595
7363
  * dispatched and are still in-flight. A message in this set is never
@@ -6622,10 +7390,18 @@ var ChannelDriver = class _ChannelDriver {
6622
7390
  * row is `processing` or `pending`; clear it only when absent from both lists.
6623
7391
  */
6624
7392
  untrackableAck = /* @__PURE__ */ new Map();
7393
+ /** Session binding used by the first untrackable-ack report, for retries. */
7394
+ untrackableAckSessionIds = /* @__PURE__ */ new Map();
6625
7395
  /** Messages whose untrackable-ack failure signal was emitted while fenced. */
6626
7396
  untrackableAckSignalled = /* @__PURE__ */ new Set();
6627
7397
  /** Messages whose first untrackable-ack fence warning was already logged. */
6628
7398
  untrackableAckWarned = /* @__PURE__ */ new Set();
7399
+ /**
7400
+ * Accepted V1 prompts whose assigned ids were not read back; these rows are
7401
+ * re-read instead of POSTed again. A null snapshot cannot safely exclude an older
7402
+ * identical-text turn, so it never produces a correlation.
7403
+ */
7404
+ awaitingCorrelation = /* @__PURE__ */ new Map();
6629
7405
  /** Pending rows seen in the current drain, used to retain terminal dispatch fences. */
6630
7406
  pendingMessageIds = /* @__PURE__ */ new Set();
6631
7407
  /**
@@ -6711,21 +7487,6 @@ var ChannelDriver = class _ChannelDriver {
6711
7487
  * re-signal on every one of the 15h of re-dispatch attempts #1110 made.
6712
7488
  */
6713
7489
  dispatchNotStartedSignalled = /* @__PURE__ */ new Map();
6714
- /**
6715
- * Consecutive-UNCONFIRMED-dispatch streak for a `pending` row with NO stored
6716
- * `opencode_message_id` yet — i.e. one that has never even reached the
6717
- * re-drive fence above. `sendPromptAsync`'s POST may 2xx, but its own
6718
- * read-back retries can never confirm the assigned id when the session's
6719
- * message list is PERMANENTLY unreadable (e.g. a corrupted local opencode
6720
- * SQLite DB, #1345/#1348's exact fault, just hit BEFORE the row is ever
6721
- * dispatched instead of after). Unlike an already-dispatched row, THIS row has
6722
- * no other safety net at all: the lifecycle cron only reclaims `status =
6723
- * 'processing'` rows, and a row stuck here never reaches `processing`. Keyed
6724
- * by message id, carrying `sessionId` so a session change (a fresh one bound
6725
- * after abandonment) starts a new streak rather than inheriting the old
6726
- * session's count — same shape as `redrivePollFailures` above.
6727
- */
6728
- unconfirmedDispatchFailures = /* @__PURE__ */ new Map();
6729
7490
  /**
6730
7491
  * Consecutive give-ups for a message whose OpenCode user row remains queued.
6731
7492
  * The session id scopes the count: a fresh session is a new attempt after a
@@ -6743,8 +7504,8 @@ var ChannelDriver = class _ChannelDriver {
6743
7504
  * short-circuits while it is present, so a null-id row is re-dispatched AT MOST
6744
7505
  * ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
6745
7506
  * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
6746
- * re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
6747
- * so the NEXT tick may retry exactly once more).
7507
+ * re-entry) OR on a failed/unresolved dispatch (the accepted prompt is handed to
7508
+ * the terminal-report fence and is never sent again).
6748
7509
  */
6749
7510
  awaitingReadopt = /* @__PURE__ */ new Set();
6750
7511
  /**
@@ -6863,10 +7624,11 @@ var ChannelDriver = class _ChannelDriver {
6863
7624
  this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
6864
7625
  this.now = config.now ?? (() => Date.now());
6865
7626
  this.fileSyncDirectories = config.fileSyncDirectories ?? [];
6866
- this.homeDir = config.homeDir ?? homedir5();
7627
+ this.homeDir = config.homeDir ?? homedir6();
6867
7628
  this.maxActiveSessions = config.maxActiveSessions;
6868
7629
  this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
6869
7630
  this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
7631
+ this.sessionStoreHealth = config.sessionStoreHealth;
6870
7632
  }
6871
7633
  get isV2() {
6872
7634
  return this.openCodeClient.version === "v2";
@@ -6882,7 +7644,9 @@ var ChannelDriver = class _ChannelDriver {
6882
7644
  return fetchSessionMessages(this.port, sessionId, this.openCodeClient);
6883
7645
  }
6884
7646
  async listSessions() {
6885
- return this.isV2 ? listV2Sessions(this.openCodeClient) : listSessions(this.port, this.openCodeClient);
7647
+ return this.isV2 ? listV2Sessions(this.openCodeClient) : listSessions(this.port, this.openCodeClient, {
7648
+ limit: DRIVER_SESSION_ENUMERATION_LIMIT
7649
+ });
6886
7650
  }
6887
7651
  async sessionExists(sessionId) {
6888
7652
  return this.isV2 ? v2SessionExists(this.openCodeClient, sessionId) : sessionExists(this.port, sessionId, this.openCodeClient);
@@ -7059,6 +7823,49 @@ var ChannelDriver = class _ChannelDriver {
7059
7823
  }
7060
7824
  return ids;
7061
7825
  }
7826
+ /** Find an accepted prompt's user row without adopting an older or concurrent turn. */
7827
+ findAwaitingCorrelationId(entry, sessionId, messages) {
7828
+ if (entry.sessionId !== sessionId || entry.knownUserIds === null) return null;
7829
+ const excludeIds = new Set(entry.knownUserIds);
7830
+ for (const inFlight of this.watchers.get(entry.sessionId)?.inFlight.values() ?? []) {
7831
+ excludeIds.add(inFlight.opencodeMessageId);
7832
+ }
7833
+ return findDispatchedUserMessageId(messages, entry.content, excludeIds);
7834
+ }
7835
+ /** Hand an exhausted accepted-but-untrackable prompt to the terminal-report fence. */
7836
+ async handoffUntrackableCorrelation(conversationId, messageId, sessionId, attempts, source) {
7837
+ this.awaitingCorrelation.delete(messageId);
7838
+ this.awaitingReadopt.delete(messageId);
7839
+ this.sessions.delete(conversationId);
7840
+ this.supersede(conversationId, sessionId);
7841
+ const dispatchDescription = source === "readopt" ? "re-dispatched message" : "message";
7842
+ 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.`;
7843
+ this.untrackableAck.set(messageId, errorMessage3);
7844
+ this.untrackableAckSessionIds.set(messageId, null);
7845
+ this.log({
7846
+ level: "error",
7847
+ message: errorMessage3,
7848
+ conversation_id: conversationId,
7849
+ message_id: messageId
7850
+ });
7851
+ const reportError = await this.reportUntrackableAck(
7852
+ conversationId,
7853
+ messageId,
7854
+ null,
7855
+ errorMessage3
7856
+ );
7857
+ if (reportError !== null) {
7858
+ this.log({
7859
+ level: "warn",
7860
+ message: `markFailed PATCH for message ${messageId.slice(0, 8)} (conversation ${conversationId.slice(0, 8)}) failed: ${reportError.message}`,
7861
+ conversation_id: conversationId,
7862
+ message_id: messageId
7863
+ });
7864
+ }
7865
+ if (source === "readopt") {
7866
+ void this.postSignal(conversationId, messageId, "readopt_orphan_unsent");
7867
+ }
7868
+ }
7062
7869
  /**
7063
7870
  * File-pull work, for `run.ts`'s idle accounting (#559).
7064
7871
  *
@@ -7238,6 +8045,56 @@ var ChannelDriver = class _ChannelDriver {
7238
8045
  }
7239
8046
  break;
7240
8047
  }
8048
+ const correlation = this.awaitingCorrelation.get(message.id);
8049
+ if (correlation) {
8050
+ correlation.attempts += 1;
8051
+ let correlatedId = null;
8052
+ if (correlation.knownUserIds !== null && correlation.sessionId === sessionId) {
8053
+ const polled = await this.pollSessionMessagesForRedrive(
8054
+ conv,
8055
+ message,
8056
+ correlation.sessionId
8057
+ );
8058
+ if (polled.ok)
8059
+ correlatedId = this.findAwaitingCorrelationId(correlation, sessionId, polled.messages);
8060
+ } else if (correlation.knownUserIds === null) {
8061
+ this.log({
8062
+ level: "debug",
8063
+ message: `Message ${message.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
8064
+ conversation_id: conv.id,
8065
+ message_id: message.id
8066
+ });
8067
+ }
8068
+ if (correlatedId !== null) {
8069
+ this.awaitingCorrelation.delete(message.id);
8070
+ this.dispatchNotStartedSignalled.delete(message.id);
8071
+ this.releasedOpencodeIds.delete(message.id);
8072
+ this.dispatched.add(message.id);
8073
+ this.registerInFlight(conv, correlation.sessionId, message, correlatedId);
8074
+ dispatched += 1;
8075
+ this.log({
8076
+ level: "info",
8077
+ message: `Matched accepted message ${message.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
8078
+ conversation_id: conv.id,
8079
+ message_id: message.id
8080
+ });
8081
+ void this.postSignal(conv.id, message.id, "ack_correlated", {
8082
+ opencode_message_id: correlatedId
8083
+ });
8084
+ continue;
8085
+ }
8086
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
8087
+ await this.handoffUntrackableCorrelation(
8088
+ conv.id,
8089
+ message.id,
8090
+ correlation.sessionId,
8091
+ correlation.attempts,
8092
+ "fresh"
8093
+ );
8094
+ break;
8095
+ }
8096
+ break;
8097
+ }
7241
8098
  const effectiveOpencodeMessageId = message.opencode_message_id ?? this.releasedOpencodeIds.get(message.id)?.opencodeMessageId ?? null;
7242
8099
  if (effectiveOpencodeMessageId) {
7243
8100
  const outcome = await this.resolveRedrive(
@@ -7259,6 +8116,7 @@ var ChannelDriver = class _ChannelDriver {
7259
8116
  model: message.opencode_model ?? void 0
7260
8117
  };
7261
8118
  let opencodeMessageId;
8119
+ let preDispatchUserIds = null;
7262
8120
  try {
7263
8121
  this.log({
7264
8122
  level: "info",
@@ -7288,7 +8146,20 @@ var ChannelDriver = class _ChannelDriver {
7288
8146
  message.content,
7289
8147
  options,
7290
8148
  sendAttachments,
7291
- this.openCodeClient
8149
+ this.openCodeClient,
8150
+ {
8151
+ onKnownUserIds: (ids) => {
8152
+ preDispatchUserIds = ids;
8153
+ },
8154
+ onKnownUserIdsError: (error2) => {
8155
+ this.log({
8156
+ level: "warn",
8157
+ message: `Pre-dispatch snapshot callback failed for message ${message.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
8158
+ conversation_id: conv.id,
8159
+ message_id: message.id
8160
+ });
8161
+ }
8162
+ }
7292
8163
  )
7293
8164
  );
7294
8165
  } catch (err) {
@@ -7296,6 +8167,7 @@ var ChannelDriver = class _ChannelDriver {
7296
8167
  if (this.isV2 && err instanceof OpenCodeV2PromptAckError) {
7297
8168
  const errorMessage4 = err instanceof Error ? err.message : String(err);
7298
8169
  this.untrackableAck.set(message.id, errorMessage4);
8170
+ this.untrackableAckSessionIds.set(message.id, null);
7299
8171
  this.log({
7300
8172
  level: "error",
7301
8173
  message: `V2 prompt dispatch for message ${message.id.slice(0, 8)} failed after a positive ack with no usable id: ${errorMessage4}`,
@@ -7371,39 +8243,21 @@ var ChannelDriver = class _ChannelDriver {
7371
8243
  if (this.isV2) {
7372
8244
  throw new Error("V2 prompt dispatch completed without an acknowledged message id");
7373
8245
  }
7374
- const streak = this.recordUnconfirmedDispatch(message.id, sessionId);
7375
- if (streak < MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
7376
- this.log({
7377
- level: "warn",
7378
- 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`,
7379
- conversation_id: conv.id,
7380
- message_id: message.id
7381
- });
7382
- this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7383
- continue;
7384
- }
7385
- this.unconfirmedDispatchFailures.delete(message.id);
7386
- this.sessions.delete(conv.id);
7387
- this.supersede(conv.id, sessionId);
7388
- 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.`;
8246
+ this.awaitingCorrelation.set(message.id, {
8247
+ sessionId,
8248
+ content: message.content,
8249
+ attempts: 0,
8250
+ knownUserIds: preDispatchUserIds
8251
+ });
7389
8252
  this.log({
7390
- level: "error",
7391
- message: errorMessage3,
8253
+ level: "warn",
8254
+ 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`,
7392
8255
  conversation_id: conv.id,
7393
8256
  message_id: message.id
7394
8257
  });
7395
- await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
7396
- this.log({
7397
- level: "warn",
7398
- 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)}`,
7399
- conversation_id: conv.id,
7400
- message_id: message.id
7401
- });
7402
- this.signalDispatchNotStarted(conv, message, "abandon_unreported");
7403
- });
8258
+ this.signalDispatchNotStarted(conv, message, "readback_unconfirmed");
7404
8259
  break;
7405
8260
  }
7406
- this.unconfirmedDispatchFailures.delete(message.id);
7407
8261
  this.dispatchNotStartedSignalled.delete(message.id);
7408
8262
  this.releasedOpencodeIds.delete(message.id);
7409
8263
  this.dispatched.add(message.id);
@@ -8008,23 +8862,6 @@ var ChannelDriver = class _ChannelDriver {
8008
8862
  this.redrivePollFailures.set(messageId, { sessionId, signature, count: 1 });
8009
8863
  return 1;
8010
8864
  }
8011
- /**
8012
- * Record one UNCONFIRMED-dispatch outcome (a `pending` row with no stored
8013
- * `opencode_message_id` whose `sendPromptAsync` returned `null`) toward the
8014
- * bound in `processConversation`'s dispatch loop, and return the resulting
8015
- * count. Mirrors `recordRedrivePollFailure`'s session-scoping: a session
8016
- * change starts a fresh streak at `1` rather than inheriting the old one's
8017
- * count, since a new session is a genuinely different attempt.
8018
- */
8019
- recordUnconfirmedDispatch(messageId, sessionId) {
8020
- const existing = this.unconfirmedDispatchFailures.get(messageId);
8021
- if (existing && existing.sessionId === sessionId) {
8022
- existing.count += 1;
8023
- return existing.count;
8024
- }
8025
- this.unconfirmedDispatchFailures.set(messageId, { sessionId, count: 1 });
8026
- return 1;
8027
- }
8028
8865
  /** Record one queued give-up toward the never-started bound. */
8029
8866
  recordNeverStartedAttempt(messageId, sessionId) {
8030
8867
  const existing = this.neverStartedAttempts.get(messageId);
@@ -8336,8 +9173,10 @@ var ChannelDriver = class _ChannelDriver {
8336
9173
  b2LastDescendantCheckMs: 0,
8337
9174
  b2RootOngoingHeldLogged: false,
8338
9175
  b2AbandonedSignalled: false,
9176
+ b2AbandonedWithoutAnswer: false,
8339
9177
  ambiguousPinnedSinceMs: 0,
8340
9178
  ambiguousResolved: false,
9179
+ sessionStoreFailurePending: false,
8341
9180
  neverStartedResolved: false
8342
9181
  });
8343
9182
  const buffered = this.bufferedSessionErrors.get(sessionId);
@@ -8359,6 +9198,28 @@ var ChannelDriver = class _ChannelDriver {
8359
9198
  this.bufferedSessionErrors.delete(oldest);
8360
9199
  }
8361
9200
  }
9201
+ retainSessionError(event) {
9202
+ this.retainedSessionErrors.delete(event.sessionId);
9203
+ this.retainedSessionErrors.set(event.sessionId, {
9204
+ reason: event.reason,
9205
+ observedAt: this.now()
9206
+ });
9207
+ while (this.retainedSessionErrors.size > MAX_BUFFERED_SESSION_ERRORS) {
9208
+ const oldest = this.retainedSessionErrors.keys().next().value;
9209
+ if (typeof oldest !== "string") break;
9210
+ this.retainedSessionErrors.delete(oldest);
9211
+ }
9212
+ }
9213
+ readRetainedSessionError(sessionId) {
9214
+ const retained = this.retainedSessionErrors.get(sessionId);
9215
+ if (!retained) return null;
9216
+ if (this.now() - retained.observedAt >= SESSION_ERROR_RETENTION_TTL_MS) {
9217
+ this.retainedSessionErrors.delete(sessionId);
9218
+ return null;
9219
+ }
9220
+ this.retainedSessionErrors.delete(sessionId);
9221
+ return retained.reason;
9222
+ }
8362
9223
  /**
8363
9224
  * Build a fresh `SessionWatcher`. Seeds `lastTickAt`/`lastObservedTickAt`
8364
9225
  * EQUAL (#1618) so a watcher whose loop has not started ticking yet is never
@@ -8454,8 +9315,10 @@ var ChannelDriver = class _ChannelDriver {
8454
9315
  b2LastDescendantCheckMs: 0,
8455
9316
  b2RootOngoingHeldLogged: false,
8456
9317
  b2AbandonedSignalled: false,
9318
+ b2AbandonedWithoutAnswer: false,
8457
9319
  ambiguousPinnedSinceMs: 0,
8458
9320
  ambiguousResolved: false,
9321
+ sessionStoreFailurePending: false,
8459
9322
  neverStartedResolved: false
8460
9323
  });
8461
9324
  }
@@ -8683,9 +9546,10 @@ var ChannelDriver = class _ChannelDriver {
8683
9546
  return;
8684
9547
  }
8685
9548
  if ([...watcher.inFlight.values()].some((message) => message.started && !message.done)) {
9549
+ this.retainSessionError(event);
8686
9550
  this.log({
8687
- level: "debug",
8688
- message: `A turn is already running in session ${event.sessionId.slice(0, 8)} \u2014 deferring to transcript polling`,
9551
+ level: "warn",
9552
+ 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}`,
8689
9553
  conversation_id: watcher.conv.id
8690
9554
  });
8691
9555
  return;
@@ -8715,9 +9579,10 @@ var ChannelDriver = class _ChannelDriver {
8715
9579
  const messages = await this.getSessionMessages(event.sessionId);
8716
9580
  const state = messageRunState(messages, inFlight.opencodeMessageId);
8717
9581
  if (state !== "queued") {
9582
+ this.retainSessionError(event);
8718
9583
  this.log({
8719
- level: "debug",
8720
- message: `Session error for message ${inFlight.evidentMessageId.slice(0, 8)} observed state ${state}; leaving it to transcript polling`,
9584
+ level: "warn",
9585
+ 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}`,
8721
9586
  conversation_id: watcher.conv.id,
8722
9587
  message_id: inFlight.evidentMessageId
8723
9588
  });
@@ -8869,6 +9734,10 @@ var ChannelDriver = class _ChannelDriver {
8869
9734
  if (state === "running" || state === "done" || state === "failed") {
8870
9735
  this.neverStartedAttempts.delete(id);
8871
9736
  }
9737
+ if (inFlight.sessionStoreFailurePending) {
9738
+ await this.settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight);
9739
+ return;
9740
+ }
8872
9741
  if (inFlight.neverStartedResolved) {
8873
9742
  if (state === "running" || state === "done" || state === "failed") {
8874
9743
  inFlight.neverStartedResolved = false;
@@ -8942,7 +9811,16 @@ var ChannelDriver = class _ChannelDriver {
8942
9811
  if (state === "failed") {
8943
9812
  this.anchorDeliveryDeadline(inFlight);
8944
9813
  if (!inFlight.done) {
8945
- const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
9814
+ const messageErrorText = messageError(messages, inFlight.opencodeMessageId);
9815
+ const retainedReason = this.readRetainedSessionError(sessionId);
9816
+ const retainedError = retainedReason === null ? null : `${SESSION_ERROR_RETAINED_DETAIL}${retainedReason}`;
9817
+ const observedCause = this.sessionStoreHealth?.readCause(sessionId);
9818
+ const causeError = observedCause === null || observedCause === void 0 ? null : `${SESSION_ERROR_OBSERVED_CAUSE}${observedCause}`;
9819
+ const storeError = this.sessionStoreHealth?.isUnwritable() ? SESSION_STORE_UNWRITABLE_ERROR : null;
9820
+ const runnerDetails = [retainedError, causeError, storeError].filter(
9821
+ (detail) => detail !== null
9822
+ );
9823
+ const error2 = messageErrorText === null ? runnerDetails.length === 0 ? void 0 : runnerDetails.join(" ") : [messageErrorText, ...runnerDetails].join(" ");
8946
9824
  this.log({
8947
9825
  level: "error",
8948
9826
  message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
@@ -9028,10 +9906,33 @@ var ChannelDriver = class _ChannelDriver {
9028
9906
  inFlight.b2LastDescendantCheckMs = 0;
9029
9907
  inFlight.b2RootOngoingHeldLogged = false;
9030
9908
  inFlight.b2AbandonedSignalled = false;
9909
+ inFlight.b2AbandonedWithoutAnswer = false;
9031
9910
  }
9032
9911
  } else {
9033
9912
  if (inFlight.b2AbandonedSignalled) {
9034
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
9913
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
9914
+ const finish = reply?.info?.finish ?? reply?.finish;
9915
+ if (inFlight.b2AbandonedWithoutAnswer) {
9916
+ await this.settleMessageAbandonedWithoutAnswer(
9917
+ sessionId,
9918
+ watcher,
9919
+ inFlight,
9920
+ messages,
9921
+ finish
9922
+ );
9923
+ } else {
9924
+ await this.settleMessageDone(
9925
+ sessionId,
9926
+ watcher,
9927
+ inFlight,
9928
+ messages,
9929
+ composeAbandonmentNote(
9930
+ finish,
9931
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
9932
+ "with-answer"
9933
+ )
9934
+ );
9935
+ }
9035
9936
  return;
9036
9937
  }
9037
9938
  if (inFlight.b2PinnedSinceMs === 0) inFlight.b2PinnedSinceMs = this.now();
@@ -9048,22 +9949,62 @@ var ChannelDriver = class _ChannelDriver {
9048
9949
  descendantOngoing,
9049
9950
  rootOngoing
9050
9951
  })) {
9952
+ if (this.sessionStoreHealth?.isUnwritable()) {
9953
+ inFlight.sessionStoreFailurePending = true;
9954
+ void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
9955
+ watched_for_ms: pinnedForMs,
9956
+ run_state: "failed",
9957
+ opencode_message_id: inFlight.opencodeMessageId
9958
+ });
9959
+ await this.settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight);
9960
+ return;
9961
+ }
9051
9962
  inFlight.b2AbandonedSignalled = true;
9963
+ const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
9964
+ const finish = reply?.info?.finish ?? reply?.finish;
9965
+ if (extractReplyTextForMessage(messages, inFlight.opencodeMessageId) === null) {
9966
+ inFlight.b2AbandonedWithoutAnswer = true;
9967
+ void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
9968
+ watched_for_ms: pinnedForMs,
9969
+ run_state: "failed",
9970
+ finish,
9971
+ ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
9972
+ ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
9973
+ opencode_message_id: inFlight.opencodeMessageId
9974
+ });
9975
+ await this.settleMessageAbandonedWithoutAnswer(
9976
+ sessionId,
9977
+ watcher,
9978
+ inFlight,
9979
+ messages,
9980
+ finish
9981
+ );
9982
+ return;
9983
+ }
9052
9984
  this.log({
9053
9985
  level: "warn",
9054
9986
  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`,
9055
9987
  conversation_id: conv.id,
9056
9988
  message_id: id
9057
9989
  });
9058
- const reply = findLastAssistantReplyFor(messages, inFlight.opencodeMessageId);
9059
9990
  void this.postSignal(conv.id, id, "b2_abandoned_resolved", {
9060
9991
  watched_for_ms: pinnedForMs,
9061
- finish: reply?.info?.finish ?? reply?.finish,
9992
+ finish,
9062
9993
  ...rootOngoing != null ? { root_ongoing: rootOngoing } : {},
9063
9994
  ...descendantOngoing != null ? { descendant_ongoing: descendantOngoing } : {},
9064
9995
  opencode_message_id: inFlight.opencodeMessageId
9065
9996
  });
9066
- await this.settleMessageDone(sessionId, watcher, inFlight, messages);
9997
+ await this.settleMessageDone(
9998
+ sessionId,
9999
+ watcher,
10000
+ inFlight,
10001
+ messages,
10002
+ composeAbandonmentNote(
10003
+ finish,
10004
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10005
+ "with-answer"
10006
+ )
10007
+ );
9067
10008
  return;
9068
10009
  }
9069
10010
  if (rootOngoing === true && !inFlight.b2RootOngoingHeldLogged) {
@@ -9106,6 +10047,16 @@ var ChannelDriver = class _ChannelDriver {
9106
10047
  maxPinnedMs: AMBIGUOUS_FINISH_MAX_PINNED_MS,
9107
10048
  sessionOngoing: ongoing
9108
10049
  })) {
10050
+ if (this.sessionStoreHealth?.isUnwritable()) {
10051
+ inFlight.sessionStoreFailurePending = true;
10052
+ void this.postSignal(conv.id, id, "ambiguous_finish_resolved", {
10053
+ watched_for_ms: pinnedForMs,
10054
+ run_state: "failed",
10055
+ opencode_message_id: inFlight.opencodeMessageId
10056
+ });
10057
+ await this.settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight);
10058
+ return;
10059
+ }
9109
10060
  inFlight.ambiguousResolved = true;
9110
10061
  this.log({
9111
10062
  level: "warn",
@@ -9253,15 +10204,11 @@ var ChannelDriver = class _ChannelDriver {
9253
10204
  }
9254
10205
  }
9255
10206
  /**
9256
- * Settle a message whose run-state has resolved `'done'` — extracted verbatim
9257
- * (pure refactor, no behavior change) from `serviceInFlightMessage`'s former
9258
- * inline `state === 'done'` branch body, so a SECOND caller (the #721
9259
- * b2-abandonment resolution) can reach the exact same completion behavior
9260
- * (delivery-deadline anchoring, title resolution, usage extraction, and
9261
- * `markDone`'s auth/terminal/transient-retry discipline) without duplicating it
9262
- * and risking the two copies silently drifting apart.
10207
+ * Settle a message whose run-state has resolved `'done'`. Normal completion and
10208
+ * confirmed b2 abandonment share the same delivery, usage, and terminal handling.
10209
+ * Normal completions omit the optional terminal diagnostic.
9263
10210
  */
9264
- async settleMessageDone(sessionId, watcher, inFlight, messages) {
10211
+ async settleMessageDone(sessionId, watcher, inFlight, messages, note) {
9265
10212
  const conv = watcher.conv;
9266
10213
  this.anchorDeliveryDeadline(inFlight);
9267
10214
  if (!inFlight.done) {
@@ -9288,7 +10235,8 @@ var ChannelDriver = class _ChannelDriver {
9288
10235
  title,
9289
10236
  usage,
9290
10237
  usageAgentName,
9291
- subagentInvocations
10238
+ subagentInvocations,
10239
+ note
9292
10240
  );
9293
10241
  } catch (err) {
9294
10242
  if (err instanceof ChannelAuthError) throw err;
@@ -9314,20 +10262,92 @@ var ChannelDriver = class _ChannelDriver {
9314
10262
  }
9315
10263
  this.log({
9316
10264
  level: "warn",
9317
- message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
10265
+ message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} done (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
10266
+ conversation_id: conv.id,
10267
+ message_id: inFlight.evidentMessageId
10268
+ });
10269
+ return;
10270
+ }
10271
+ inFlight.done = true;
10272
+ await this.reportSubagentAuthFailures(
10273
+ watcher.conv.id,
10274
+ inFlight.opencodeMessageId,
10275
+ inFlight.evidentMessageId,
10276
+ messages
10277
+ );
10278
+ }
10279
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10280
+ }
10281
+ /** Report a confirmed b2 abandonment that produced no answer text. */
10282
+ async settleMessageAbandonedWithoutAnswer(sessionId, watcher, inFlight, messages, finish) {
10283
+ const conv = watcher.conv;
10284
+ const error2 = composeAbandonmentNote(
10285
+ finish,
10286
+ describeAbandonedReplyShape(messages, inFlight.opencodeMessageId),
10287
+ "without-answer"
10288
+ );
10289
+ this.anchorDeliveryDeadline(inFlight);
10290
+ this.log({
10291
+ level: "warn",
10292
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was abandoned without an answer \u2014 ${error2}`,
10293
+ conversation_id: conv.id,
10294
+ message_id: inFlight.evidentMessageId
10295
+ });
10296
+ const usage = messageUsage(messages, inFlight.opencodeMessageId);
10297
+ const usageAgentName = this.usageAgentName(messages ?? [], inFlight.opencodeMessageId);
10298
+ const subagentInvocations = await this.resolveSubagentInvocations(
10299
+ messages,
10300
+ inFlight.opencodeMessageId,
10301
+ inFlight.evidentMessageId
10302
+ );
10303
+ try {
10304
+ await this.markFailed(
10305
+ conv.id,
10306
+ inFlight.evidentMessageId,
10307
+ sessionId,
10308
+ error2,
10309
+ usage,
10310
+ void 0,
10311
+ usageAgentName,
10312
+ subagentInvocations
10313
+ );
10314
+ } catch (err) {
10315
+ if (err instanceof ChannelAuthError) throw err;
10316
+ if (err instanceof ChannelTerminalError) {
10317
+ this.log({
10318
+ level: "warn",
10319
+ 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}`,
10320
+ conversation_id: conv.id,
10321
+ message_id: inFlight.evidentMessageId
10322
+ });
10323
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10324
+ return;
10325
+ }
10326
+ if (this.now() >= inFlight.deadline) {
10327
+ this.log({
10328
+ level: "warn",
10329
+ 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)}`,
9318
10330
  conversation_id: conv.id,
9319
10331
  message_id: inFlight.evidentMessageId
9320
10332
  });
10333
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
9321
10334
  return;
9322
10335
  }
9323
- inFlight.done = true;
9324
- await this.reportSubagentAuthFailures(
9325
- watcher.conv.id,
9326
- inFlight.opencodeMessageId,
9327
- inFlight.evidentMessageId,
9328
- messages
9329
- );
10336
+ this.log({
10337
+ level: "warn",
10338
+ 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)}`,
10339
+ conversation_id: conv.id,
10340
+ message_id: inFlight.evidentMessageId
10341
+ });
10342
+ return;
9330
10343
  }
10344
+ inFlight.done = true;
10345
+ await this.reportSubagentAuthFailures(
10346
+ watcher.conv.id,
10347
+ inFlight.opencodeMessageId,
10348
+ inFlight.evidentMessageId,
10349
+ messages
10350
+ );
9331
10351
  this.removeInFlight(watcher, inFlight.evidentMessageId);
9332
10352
  }
9333
10353
  /**
@@ -9385,6 +10405,66 @@ var ChannelDriver = class _ChannelDriver {
9385
10405
  this.neverStartedAttempts.delete(inFlight.evidentMessageId);
9386
10406
  this.removeInFlight(watcher, inFlight.evidentMessageId);
9387
10407
  }
10408
+ /**
10409
+ * Report a turn as failed when the session database was observed unwritable.
10410
+ * The queue row is retained as failed, while the OpenCode copy stays fenced so
10411
+ * a later drain cannot blindly prompt the same turn again.
10412
+ */
10413
+ async settleMessageSessionStoreUnwritable(sessionId, watcher, inFlight) {
10414
+ const conv = watcher.conv;
10415
+ const error2 = SESSION_STORE_UNWRITABLE_ERROR;
10416
+ this.log({
10417
+ level: "error",
10418
+ message: `Message ${inFlight.evidentMessageId.slice(0, 8)} was not resolved as done: ${error2}`,
10419
+ conversation_id: conv.id,
10420
+ message_id: inFlight.evidentMessageId
10421
+ });
10422
+ this.anchorDeliveryDeadline(inFlight);
10423
+ try {
10424
+ await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
10425
+ } catch (err) {
10426
+ if (err instanceof ChannelAuthError) throw err;
10427
+ if (err instanceof ChannelTerminalError) {
10428
+ this.log({
10429
+ level: "warn",
10430
+ message: `Failed to report session-store failure for message ${inFlight.evidentMessageId.slice(0, 8)} as failed (terminal HTTP ${err.status}) \u2014 keeping its OpenCode copy fenced: ${err.message}`,
10431
+ conversation_id: conv.id,
10432
+ message_id: inFlight.evidentMessageId
10433
+ });
10434
+ this.recordReleasedOpencodeId(
10435
+ inFlight.evidentMessageId,
10436
+ sessionId,
10437
+ inFlight.opencodeMessageId
10438
+ );
10439
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10440
+ return;
10441
+ }
10442
+ if (this.now() >= inFlight.deadline) {
10443
+ this.log({
10444
+ level: "warn",
10445
+ message: `Failed to report session-store failure for message ${inFlight.evidentMessageId.slice(0, 8)} as failed within the delivery window \u2014 keeping its OpenCode copy fenced: ${err instanceof Error ? err.message : String(err)}`,
10446
+ conversation_id: conv.id,
10447
+ message_id: inFlight.evidentMessageId
10448
+ });
10449
+ this.recordReleasedOpencodeId(
10450
+ inFlight.evidentMessageId,
10451
+ sessionId,
10452
+ inFlight.opencodeMessageId
10453
+ );
10454
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10455
+ return;
10456
+ }
10457
+ this.log({
10458
+ level: "warn",
10459
+ message: `Failed to report session-store failure for message ${inFlight.evidentMessageId.slice(0, 8)} as failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
10460
+ conversation_id: conv.id,
10461
+ message_id: inFlight.evidentMessageId
10462
+ });
10463
+ return;
10464
+ }
10465
+ inFlight.done = true;
10466
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
10467
+ }
9388
10468
  // Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
9389
10469
  /**
9390
10470
  * Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
@@ -9423,11 +10503,22 @@ var ChannelDriver = class _ChannelDriver {
9423
10503
  }
9424
10504
  }
9425
10505
  }
9426
- if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0) {
10506
+ if (this.untrackableAck.size > 0 || this.neverStartedAttempts.size > 0 || this.awaitingCorrelation.size > 0) {
9427
10507
  const stillProcessing = new Set(rows.map((r) => r.id));
10508
+ for (const id of this.awaitingCorrelation.keys()) {
10509
+ if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
10510
+ this.awaitingCorrelation.delete(id);
10511
+ this.log({
10512
+ level: "debug",
10513
+ message: `Re-adopt: message ${id.slice(0, 8)} left processing and pending \u2014 cleared awaiting-correlation fence`,
10514
+ message_id: id
10515
+ });
10516
+ }
10517
+ }
9428
10518
  for (const id of [...this.untrackableAck.keys()]) {
9429
10519
  if (!stillProcessing.has(id) && !this.pendingMessageIds.has(id)) {
9430
10520
  this.untrackableAck.delete(id);
10521
+ this.untrackableAckSessionIds.delete(id);
9431
10522
  this.untrackableAckSignalled.delete(id);
9432
10523
  this.untrackableAckWarned.delete(id);
9433
10524
  this.log({
@@ -9467,7 +10558,7 @@ var ChannelDriver = class _ChannelDriver {
9467
10558
  bySession.set(row.opencode_session_id, list);
9468
10559
  }
9469
10560
  for (const [sessionId, sessionRows] of bySession) {
9470
- let messages;
10561
+ let messages = null;
9471
10562
  try {
9472
10563
  if (this.isV2) {
9473
10564
  const snapshot = await this.getSessionMessages(sessionId);
@@ -9476,9 +10567,9 @@ var ChannelDriver = class _ChannelDriver {
9476
10567
  level: "warn",
9477
10568
  message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned an unreadable message snapshot \u2014 skipping this session this tick`
9478
10569
  });
9479
- continue;
10570
+ } else {
10571
+ messages = snapshot;
9480
10572
  }
9481
- messages = snapshot;
9482
10573
  } else {
9483
10574
  const polled = await pollSessionMessagesForRedrive(
9484
10575
  this.port,
@@ -9491,15 +10582,22 @@ var ChannelDriver = class _ChannelDriver {
9491
10582
  level: "warn",
9492
10583
  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`
9493
10584
  });
9494
- continue;
10585
+ } else {
10586
+ messages = polled.messages;
9495
10587
  }
9496
- messages = polled.messages;
9497
10588
  }
9498
10589
  } catch (err) {
9499
10590
  this.log({
9500
10591
  level: "warn",
9501
10592
  message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
9502
10593
  });
10594
+ }
10595
+ if (messages === null) {
10596
+ for (const row of sessionRows) {
10597
+ if (this.awaitingCorrelation.has(row.id)) {
10598
+ await this.advanceReadoptCorrelation(sessionId, row, null);
10599
+ }
10600
+ }
9503
10601
  continue;
9504
10602
  }
9505
10603
  const anyUntracked = sessionRows.some((row) => !this.isTracked(sessionId, row.id));
@@ -9509,6 +10607,59 @@ var ChannelDriver = class _ChannelDriver {
9509
10607
  }
9510
10608
  }
9511
10609
  }
10610
+ /** Advance a re-adopted accepted prompt through the shared correlation fence. */
10611
+ async advanceReadoptCorrelation(sessionId, row, messages) {
10612
+ const correlation = this.awaitingCorrelation.get(row.id);
10613
+ if (!correlation) return false;
10614
+ correlation.attempts += 1;
10615
+ const correlatedId = messages === null ? null : this.findAwaitingCorrelationId(correlation, sessionId, messages);
10616
+ if (correlatedId !== null) {
10617
+ this.awaitingCorrelation.delete(row.id);
10618
+ const conv = this.convForRow(sessionId, row);
10619
+ const message = this.queuedMessageForRow(row);
10620
+ const processedAtMs = this.processedAtMs(row);
10621
+ this.registerReadopted(conv, sessionId, message, correlatedId, processedAtMs, processedAtMs);
10622
+ this.dispatched.add(row.id);
10623
+ this.readopted.add(row.id);
10624
+ this.ensureWatcherRunning(sessionId);
10625
+ this.log({
10626
+ level: "info",
10627
+ message: `Re-adopt: matched accepted message ${row.id.slice(0, 8)} to OpenCode turn ${correlatedId.slice(0, 8)} without sending it again`,
10628
+ conversation_id: row.conversation_id,
10629
+ message_id: row.id
10630
+ });
10631
+ void this.postSignal(row.conversation_id, row.id, "readopt_redispatched");
10632
+ void this.postSignal(row.conversation_id, row.id, "ack_correlated", {
10633
+ opencode_message_id: correlatedId
10634
+ });
10635
+ return true;
10636
+ }
10637
+ if (correlation.knownUserIds === null) {
10638
+ this.log({
10639
+ level: "debug",
10640
+ message: `Re-adopt: message ${row.id.slice(0, 8)} cannot be correlated because its pre-dispatch session list was unreadable`,
10641
+ conversation_id: row.conversation_id,
10642
+ message_id: row.id
10643
+ });
10644
+ } else if (correlation.sessionId !== sessionId) {
10645
+ this.log({
10646
+ level: "warn",
10647
+ 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`,
10648
+ conversation_id: row.conversation_id,
10649
+ message_id: row.id
10650
+ });
10651
+ }
10652
+ if (correlation.attempts >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
10653
+ await this.handoffUntrackableCorrelation(
10654
+ row.conversation_id,
10655
+ row.id,
10656
+ correlation.sessionId,
10657
+ correlation.attempts,
10658
+ "readopt"
10659
+ );
10660
+ }
10661
+ return true;
10662
+ }
9512
10663
  /**
9513
10664
  * Re-adopt ONE `processing` row against the tick's session message snapshot
9514
10665
  * (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
@@ -9516,8 +10667,10 @@ var ChannelDriver = class _ChannelDriver {
9516
10667
  * Branches on `messageRunState(messages, row.opencode_message_id)` — the
9517
10668
  * opencode-assigned user-message id persisted on the first `processing` PATCH
9518
10669
  * (#218). A row with a NULL stored id (dispatched but the read-back never landed
9519
- * before the restart) has no id to correlate → treated as an orphan and
9520
- * re-dispatched (at most once, see `forceReadoptRun`):
10670
+ * before the restart) is first offered to an awaiting-correlation fence recorded
10671
+ * by this process; without that fence, it has no id to correlate after restart
10672
+ * and is treated as an orphan and re-dispatched (at most once, see
10673
+ * `forceReadoptRun`):
9521
10674
  * - `done` → `markDone` now (guarded like the watcher's done branch);
9522
10675
  * - `failed` → `markFailed` with the surfaced error (issue #182), so an
9523
10676
  * errored turn is reported failed on restart, NOT re-dispatched —
@@ -9526,7 +10679,8 @@ var ChannelDriver = class _ChannelDriver {
9526
10679
  * re-dispatched instead (issue #1310, see the branch below);
9527
10680
  * - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
9528
10681
  * tracking the stored id so the reply correlates by it;
9529
- * - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
10682
+ * - `unknown`/null id → correlate through an awaiting fence when present;
10683
+ * otherwise re-dispatch (opencode assigns a fresh id) + attach a watcher.
9530
10684
  *
9531
10685
  * Only `ChannelAuthError` propagates.
9532
10686
  */
@@ -9540,6 +10694,7 @@ var ChannelDriver = class _ChannelDriver {
9540
10694
  });
9541
10695
  return;
9542
10696
  }
10697
+ if (await this.advanceReadoptCorrelation(sessionId, row, messages)) return;
9543
10698
  const ocId = row.opencode_message_id;
9544
10699
  const state = messageRunState(messages, ocId ?? "");
9545
10700
  if (state === "done") {
@@ -9606,12 +10761,48 @@ var ChannelDriver = class _ChannelDriver {
9606
10761
  await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
9607
10762
  this.dontRedispatch.delete(row.id);
9608
10763
  this.untrackableAck.delete(row.id);
10764
+ this.untrackableAckSessionIds.delete(row.id);
9609
10765
  this.untrackableAckSignalled.delete(row.id);
9610
10766
  this.untrackableAckWarned.delete(row.id);
9611
10767
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
9612
10768
  return;
9613
10769
  }
9614
- if (this.dontRedispatch.has(row.id) || this.untrackableAck.has(row.id)) {
10770
+ if (this.untrackableAck.has(row.id)) {
10771
+ if (this.untrackableAckSignalled.has(row.id)) {
10772
+ const firstFenceWarning = !this.untrackableAckWarned.has(row.id);
10773
+ this.untrackableAckWarned.add(row.id);
10774
+ this.log({
10775
+ level: firstFenceWarning ? "warn" : "debug",
10776
+ 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`,
10777
+ conversation_id: row.conversation_id,
10778
+ message_id: row.id
10779
+ });
10780
+ const errorMessage3 = this.untrackableAck.get(row.id);
10781
+ const reportError = await this.reportUntrackableAck(
10782
+ row.conversation_id,
10783
+ row.id,
10784
+ this.untrackableAckSessionIds.get(row.id) ?? null,
10785
+ errorMessage3
10786
+ );
10787
+ if (reportError !== null) {
10788
+ this.log({
10789
+ level: "warn",
10790
+ message: `Re-adopt: failed to report untrackable message ${row.id.slice(0, 8)} (will retry next drain): ${reportError.message}`,
10791
+ conversation_id: row.conversation_id,
10792
+ message_id: row.id
10793
+ });
10794
+ }
10795
+ } else {
10796
+ this.log({
10797
+ level: "debug",
10798
+ message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
10799
+ conversation_id: row.conversation_id,
10800
+ message_id: row.id
10801
+ });
10802
+ }
10803
+ return;
10804
+ }
10805
+ if (this.dontRedispatch.has(row.id)) {
9615
10806
  this.log({
9616
10807
  level: "debug",
9617
10808
  message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
@@ -9789,6 +10980,7 @@ var ChannelDriver = class _ChannelDriver {
9789
10980
  }
9790
10981
  this.dontRedispatch.delete(row.id);
9791
10982
  this.untrackableAck.delete(row.id);
10983
+ this.untrackableAckSessionIds.delete(row.id);
9792
10984
  this.untrackableAckSignalled.delete(row.id);
9793
10985
  this.untrackableAckWarned.delete(row.id);
9794
10986
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
@@ -9808,8 +11000,9 @@ var ChannelDriver = class _ChannelDriver {
9808
11000
  * latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
9809
11001
  * short-circuit while the row is latched; clear it on a successful dispatch (the
9810
11002
  * row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
9811
- * re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
9812
- * may retry exactly once more).
11003
+ * re-entry) or when the dispatch itself failed before acceptance. An accepted V1
11004
+ * dispatch whose id is not readable enters the bounded correlation fence instead
11005
+ * of retrying; exhaustion hands it to `untrackableAck`.
9813
11006
  *
9814
11007
  * `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
9815
11008
  * `processed_at` (Invariant 1).
@@ -9872,6 +11065,7 @@ var ChannelDriver = class _ChannelDriver {
9872
11065
  );
9873
11066
  }
9874
11067
  let ocId;
11068
+ let preDispatchUserIds = null;
9875
11069
  try {
9876
11070
  ocId = this.isV2 ? await sendV2Prompt(this.openCodeClient, sessionId, row.content) : await this.dispatchLocked(
9877
11071
  sessionId,
@@ -9881,7 +11075,20 @@ var ChannelDriver = class _ChannelDriver {
9881
11075
  row.content,
9882
11076
  options,
9883
11077
  sendAttachments,
9884
- this.openCodeClient
11078
+ this.openCodeClient,
11079
+ {
11080
+ onKnownUserIds: (ids) => {
11081
+ preDispatchUserIds = ids;
11082
+ },
11083
+ onKnownUserIdsError: (error2) => {
11084
+ this.log({
11085
+ level: "warn",
11086
+ message: `Pre-dispatch snapshot callback failed for message ${row.id.slice(0, 8)}: ${error2 instanceof Error ? error2.message : String(error2)}`,
11087
+ conversation_id: row.conversation_id,
11088
+ message_id: row.id
11089
+ });
11090
+ }
11091
+ }
9885
11092
  )
9886
11093
  );
9887
11094
  } catch (err) {
@@ -9914,6 +11121,7 @@ var ChannelDriver = class _ChannelDriver {
9914
11121
  }
9915
11122
  } else {
9916
11123
  this.untrackableAck.set(row.id, errorMessage3);
11124
+ this.untrackableAckSessionIds.set(row.id, sessionId);
9917
11125
  }
9918
11126
  this.log({
9919
11127
  level: "error",
@@ -9962,39 +11170,20 @@ var ChannelDriver = class _ChannelDriver {
9962
11170
  }
9963
11171
  if (ocId === null) {
9964
11172
  this.awaitingReadopt.delete(row.id);
9965
- const streak = this.recordUnconfirmedDispatch(row.id, sessionId);
9966
- if (streak >= MAX_IDENTICAL_REDRIVE_POLL_FAILURES) {
9967
- this.unconfirmedDispatchFailures.delete(row.id);
9968
- this.sessions.delete(readoptConv.id);
9969
- this.supersede(readoptConv.id, sessionId);
9970
- 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.`;
9971
- this.log({
9972
- level: "error",
9973
- message: errorMessage3,
9974
- conversation_id: row.conversation_id,
9975
- message_id: row.id
9976
- });
9977
- await this.markFailed(row.conversation_id, row.id, null, errorMessage3).catch((markErr) => {
9978
- this.log({
9979
- level: "warn",
9980
- 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)}`,
9981
- conversation_id: row.conversation_id,
9982
- message_id: row.id
9983
- });
9984
- });
9985
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
9986
- return;
9987
- }
11173
+ this.awaitingCorrelation.set(row.id, {
11174
+ sessionId,
11175
+ content: row.content,
11176
+ attempts: 0,
11177
+ knownUserIds: preDispatchUserIds
11178
+ });
9988
11179
  this.log({
9989
11180
  level: "warn",
9990
- 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`,
11181
+ 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`,
9991
11182
  conversation_id: row.conversation_id,
9992
11183
  message_id: row.id
9993
11184
  });
9994
- void this.postSignal(row.conversation_id, row.id, "readopt_orphan_unsent");
9995
11185
  return;
9996
11186
  }
9997
- this.unconfirmedDispatchFailures.delete(row.id);
9998
11187
  const processedAtMs = this.processedAtMs(row);
9999
11188
  this.registerReadopted(
10000
11189
  readoptConv,
@@ -10900,7 +12089,7 @@ var ChannelDriver = class _ChannelDriver {
10900
12089
  * watcher retries next tick within the
10901
12090
  * deadline, Finding 4).
10902
12091
  */
10903
- async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations) {
12092
+ async markDone(conversationId, messageId, sessionId, opencodeMessageId, title, usage, usageAgentName, subagentInvocations, error2) {
10904
12093
  const res = await this.fetchImpl(
10905
12094
  `${this.apiUrl}/runners/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
10906
12095
  {
@@ -10918,7 +12107,8 @@ var ChannelDriver = class _ChannelDriver {
10918
12107
  ...title ? { title } : {},
10919
12108
  ...usage ? usage : {},
10920
12109
  ...usageAgentName ? { usage_agent_name: usageAgentName } : {},
10921
- ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {}
12110
+ ...subagentInvocations && subagentInvocations.length > 0 ? { subagent_invocations: subagentInvocations } : {},
12111
+ ...error2 ? { error: error2 } : {}
10922
12112
  })
10923
12113
  }
10924
12114
  );
@@ -12116,6 +13306,7 @@ function createCredentialSync({
12116
13306
 
12117
13307
  // src/commands/run.ts
12118
13308
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
13309
+ var MAX_DISCONNECT_REASON_LENGTH = 200;
12119
13310
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
12120
13311
  var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
12121
13312
  var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
@@ -12271,7 +13462,13 @@ function logActivity(state, entry) {
12271
13462
  const level = entry.level ?? (entry.type === "error" ? "error" : "info");
12272
13463
  if (!meetsThreshold(state, level)) return;
12273
13464
  forwardRunnerActivity(
12274
- { level, message: entry.message, error: entry.error, metadata: entry.metadata },
13465
+ {
13466
+ level,
13467
+ message: entry.message,
13468
+ error: entry.error,
13469
+ metadata: entry.metadata,
13470
+ ...entry.source ? { source: entry.source } : {}
13471
+ },
12275
13472
  { agentId: state.agentId, authHeader: state.authHeader }
12276
13473
  );
12277
13474
  const fullEntry = {
@@ -12293,7 +13490,7 @@ function logActivity(state, entry) {
12293
13490
  }
12294
13491
  function reportSessionDbRecovery(state) {
12295
13492
  try {
12296
- const report = drainSessionDbRecoveryReport({ homeDir: homedir6(), env: process.env });
13493
+ const report = drainSessionDbRecoveryReport({ homeDir: homedir7(), env: process.env });
12297
13494
  for (const record of report.records) {
12298
13495
  const activity = buildSessionDbRecoveryActivity(record);
12299
13496
  if (!activity) throw new Error("could not map session-DB recovery record");
@@ -12328,7 +13525,9 @@ function displayStatus(state) {
12328
13525
  const opencode = state.opencodeConnected ? chalk7.green(`opencode: :${state.port}`) : chalk7.red(`opencode: :${state.port} (down)`);
12329
13526
  const messages = state.messageCount > 0 ? chalk7.dim(` \xB7 ${state.messageCount} processed`) : "";
12330
13527
  const last = state.activityLog[state.activityLog.length - 1];
12331
- const detail = last ? chalk7.dim(` \xB7 ${last.type === "error" ? last.error ?? "" : last.message ?? ""}`) : "";
13528
+ const detail = last ? chalk7.dim(
13529
+ ` \xB7 ${last.timestamp.toISOString()} ${last.type === "error" ? last.error ?? "" : last.message ?? ""}`
13530
+ ) : "";
12332
13531
  const agent = state.agentName ?? state.agentId;
12333
13532
  console.log(
12334
13533
  `${chalk7.bold("Evident")} ${chalk7.dim(agent)} ${tunnel} ${opencode}${messages}${detail}`
@@ -12347,7 +13546,7 @@ function reportOpenCodeV2CredentialImport(state, result) {
12347
13546
  logActivity(state, {
12348
13547
  type: "info",
12349
13548
  level: "warn",
12350
- message: `OpenCode V2 saved login(s) for ${integrationIds} on this machine, but OpenCode V2 will not use them. See #2251 for importing existing logins.`
13549
+ message: `OpenCode V2 saved login(s) for ${integrationIds} on this machine, but OpenCode V2 will not use them. Connect the provider again from the runner's page in Evident; no shell on this machine is needed.`
12351
13550
  });
12352
13551
  }
12353
13552
  if (result.imported.length === 0 && result.unsupported.length === 0 && result.failed.length === 0) {
@@ -12372,7 +13571,7 @@ async function importOpenCodeV2CredentialsForState(state) {
12372
13571
  }
12373
13572
  const result = await importOpenCodeV2Credentials({
12374
13573
  request: client.request,
12375
- homeDir: homedir6(),
13574
+ homeDir: homedir7(),
12376
13575
  directory,
12377
13576
  guard: state.opencodeV2CredentialImportGuard,
12378
13577
  log: (message, level = "info") => logActivity(state, { type: "info", level, message })
@@ -12580,9 +13779,9 @@ async function driveChannels(state, driver) {
12580
13779
  }
12581
13780
  }
12582
13781
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
12583
- var SESSION_DB_RECLAIM_MAX_PAGES = 2e4;
13782
+ var SESSION_CLEANUP_MAX_DELETES_PER_SWEEP = 100;
12584
13783
  function shouldWarnForReclaimSkip(reason) {
12585
- if (reason !== "sqlite-unavailable") return false;
13784
+ if (reason !== "sqlite-unavailable" && reason !== "disk-exhaustion") return false;
12586
13785
  const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
12587
13786
  if (!version2) return false;
12588
13787
  const major = Number(version2[1]);
@@ -12591,7 +13790,14 @@ function shouldWarnForReclaimSkip(reason) {
12591
13790
  return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
12592
13791
  }
12593
13792
  function sessionDbPath() {
12594
- return join11(homedir6(), ".local", "share", "opencode", "opencode.db");
13793
+ return join11(homedir7(), ".local", "share", "opencode", "opencode.db");
13794
+ }
13795
+ function logSessionCleanupActivity(state, phase, entry) {
13796
+ logActivity(state, {
13797
+ ...entry,
13798
+ source: "cli.session-cleanup",
13799
+ metadata: { ...entry.metadata, phase }
13800
+ });
12595
13801
  }
12596
13802
  function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
12597
13803
  const record = {
@@ -12632,69 +13838,136 @@ function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBo
12632
13838
  async function runSweep(state, driver, config) {
12633
13839
  const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
12634
13840
  try {
12635
- const sessions = await listSessions(state.port, state.opencodeClient ?? void 0);
12636
- if (sessions === null) {
13841
+ const enumeration = await listAllSessions(state.port, state.opencodeClient ?? void 0);
13842
+ const sessions = enumeration.sessions;
13843
+ const enumerationReason = sessions === null ? enumeration.reason : null;
13844
+ if (enumerationReason !== null) {
12637
13845
  logActivity(state, {
12638
13846
  type: "info",
12639
- message: `Session cleanup: could not list sessions (opencode unreachable); skipping this sweep (${mode})`
13847
+ level: "warn",
13848
+ message: `Session cleanup: could not enumerate sessions (${enumerationReason}); skipping deletes and continuing with space reclaim (${mode})`
12640
13849
  });
12641
- return;
12642
13850
  }
12643
- const toDelete = selectSessionsToDelete(
12644
- sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
12645
- {
12646
- maxAgeMs: config.maxAgeMs,
12647
- maxCount: config.maxCount,
12648
- nowMs: Date.now(),
12649
- protectedIds: driver.protectedSessionIds()
12650
- }
12651
- );
12652
- const protectedNow = driver.protectedSessionIds();
12653
13851
  let deleted = 0;
12654
13852
  let failed = 0;
13853
+ let failed4xx = 0;
13854
+ let failed5xx = 0;
13855
+ let failedOtherStatus = 0;
13856
+ let failedThrew = 0;
12655
13857
  let skippedNewlyActive = 0;
12656
- for (const id of toDelete) {
12657
- if (protectedNow.has(id)) {
12658
- skippedNewlyActive++;
12659
- logActivity(state, {
12660
- type: "info",
12661
- message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
12662
- });
12663
- continue;
13858
+ let remaining = 0;
13859
+ let protectedNow = driver.protectedSessionIds();
13860
+ if (sessions !== null) {
13861
+ const toDelete = selectSessionsToDelete(
13862
+ sessions.map((s) => ({ id: s.id, lastActivityMs: sessionLastActivityMs(s) })),
13863
+ {
13864
+ maxAgeMs: config.maxAgeMs,
13865
+ maxCount: config.maxCount,
13866
+ nowMs: Date.now(),
13867
+ protectedIds: protectedNow
13868
+ }
13869
+ );
13870
+ const deleteCandidates = toDelete.slice(0, SESSION_CLEANUP_MAX_DELETES_PER_SWEEP);
13871
+ remaining = toDelete.length - deleteCandidates.length;
13872
+ protectedNow = driver.protectedSessionIds();
13873
+ for (const id of deleteCandidates) {
13874
+ if (protectedNow.has(id)) {
13875
+ skippedNewlyActive++;
13876
+ logActivity(state, {
13877
+ type: "info",
13878
+ message: `Session cleanup: skipping ${id} \u2014 became active/bound after selection (${mode})`
13879
+ });
13880
+ continue;
13881
+ }
13882
+ const outcome = await deleteSession(state.port, id, state.opencodeClient ?? void 0);
13883
+ if (outcome.ok) {
13884
+ deleted++;
13885
+ } else if (outcome.status === "threw") {
13886
+ failed++;
13887
+ failedThrew++;
13888
+ } else {
13889
+ failed++;
13890
+ const statusClass = Math.floor(outcome.status / 100);
13891
+ if (statusClass === 4) failed4xx++;
13892
+ else if (statusClass === 5) failed5xx++;
13893
+ else failedOtherStatus++;
13894
+ }
12664
13895
  }
12665
- if (await deleteSession(state.port, id, state.opencodeClient ?? void 0)) deleted++;
12666
- else failed++;
13896
+ const failedNote = failed > 0 ? `, failed ${failed}` : "";
13897
+ const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
13898
+ const remainingNote = remaining > 0 ? `, remaining ${remaining}` : "";
13899
+ logActivity(state, {
13900
+ type: "info",
13901
+ message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote}${remainingNote} (${mode})`
13902
+ });
12667
13903
  }
12668
- const failedNote = failed > 0 ? `, failed ${failed}` : "";
12669
- const skippedNote = skippedNewlyActive > 0 ? `, skipped ${skippedNewlyActive} newly-active` : "";
12670
- logActivity(state, {
12671
- type: "info",
12672
- message: `Session cleanup: inspected ${sessions.length}, deleted ${deleted}${failedNote}${skippedNote} (${mode})`
12673
- });
13904
+ const sessionMetadata = {
13905
+ sessions_inspected: sessions?.length ?? null,
13906
+ sessions_deleted: deleted,
13907
+ sessions_failed: failed,
13908
+ sessions_failed_4xx: failed4xx,
13909
+ sessions_failed_5xx: failed5xx,
13910
+ sessions_failed_other: failedOtherStatus,
13911
+ sessions_failed_threw: failedThrew,
13912
+ sessions_skipped_active: skippedNewlyActive,
13913
+ sessions_remaining: remaining,
13914
+ ...enumerationReason !== null ? { session_enumeration_reason: enumerationReason } : {}
13915
+ };
12674
13916
  const reclaimResult = await reclaimSessionDbSpace({
12675
13917
  dbPath: sessionDbPath(),
12676
- maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
13918
+ chunkPages: SESSION_DB_RECLAIM_CHUNK_PAGES,
13919
+ pageCeiling: SESSION_DB_RECLAIM_PAGE_CEILING,
13920
+ timeBudgetMs: SESSION_DB_RECLAIM_TIME_BUDGET_MS,
12677
13921
  allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
12678
13922
  });
12679
13923
  if (reclaimResult.ok) {
13924
+ state.sessionStoreHealth.observeWritable();
12680
13925
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
12681
13926
  const afterMib = (reclaimResult.afterBytes / 1024 / 1024).toFixed(1);
12682
13927
  const checkpointNote = reclaimResult.checkpoint.busy ? ` (on-disk file truncation deferred: checkpoint busy, ${reclaimResult.checkpoint.log} WAL frames pending)` : "";
12683
- logActivity(state, {
13928
+ logSessionCleanupActivity(state, "sweep", {
12684
13929
  type: "info",
12685
- message: `Session cleanup: reclaimed session-db space (${reclaimResult.mode}): ${beforeMib} MiB -> ${afterMib} MiB${checkpointNote}`
13930
+ message: `Session cleanup: reclaimed session-db space (${reclaimResult.mode}): ${beforeMib} MiB -> ${afterMib} MiB (stopped by ${reclaimResult.stoppedBy}; freelist ${reclaimResult.freelistBefore} -> ${reclaimResult.freelistAfter} pages)` + checkpointNote,
13931
+ metadata: {
13932
+ outcome: "reclaimed",
13933
+ mode: reclaimResult.mode,
13934
+ before_bytes: reclaimResult.beforeBytes,
13935
+ after_bytes: reclaimResult.afterBytes,
13936
+ checkpoint_busy: reclaimResult.checkpoint.busy,
13937
+ wal_frames_pending: reclaimResult.checkpoint.log,
13938
+ stopped_by: reclaimResult.stoppedBy,
13939
+ freelist_before: reclaimResult.freelistBefore,
13940
+ freelist_after: reclaimResult.freelistAfter,
13941
+ pages_reclaimed: reclaimResult.pagesReclaimed,
13942
+ reclaim_elapsed_ms: reclaimResult.elapsedMs,
13943
+ ...sessionMetadata
13944
+ }
12686
13945
  });
12687
13946
  } else {
12688
- logActivity(state, {
13947
+ if (reclaimResult.skipped === "disk-exhaustion") {
13948
+ state.sessionStoreHealth.observeUnwritable();
13949
+ }
13950
+ logSessionCleanupActivity(state, "sweep", {
12689
13951
  type: "info",
12690
- message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})` + (reclaimResult.detail ? `: ${reclaimResult.detail}` : "")
13952
+ level: reclaimResult.skipped === "reclaim-error" || reclaimResult.skipped === "disk-exhaustion" || reclaimResult.skipped === "insufficient-disk-space" ? "warn" : "info",
13953
+ message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})` + (reclaimResult.detail ? `: ${reclaimResult.detail}` : ""),
13954
+ metadata: {
13955
+ outcome: "reclaim-skipped",
13956
+ reason: reclaimResult.skipped,
13957
+ ...reclaimResult.detail !== void 0 ? { detail: reclaimResult.detail } : {},
13958
+ ...sessionMetadata
13959
+ }
12691
13960
  });
12692
13961
  }
12693
13962
  } catch (error2) {
12694
13963
  const message = error2 instanceof Error ? error2.message : String(error2);
12695
- logActivity(state, {
13964
+ logSessionCleanupActivity(state, "sweep", {
12696
13965
  type: "error",
12697
- error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`
13966
+ error: `Session cleanup sweep failed (non-fatal, ${mode}): ${message}`,
13967
+ metadata: {
13968
+ outcome: "sweep-failed",
13969
+ reason: "sweep-error"
13970
+ }
12698
13971
  });
12699
13972
  }
12700
13973
  }
@@ -12710,14 +13983,19 @@ function scheduleSessionCleanup(state, driver, options) {
12710
13983
  for (const warning2 of config.warnings) {
12711
13984
  logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
12712
13985
  }
12713
- const dbBytes = statSessionDbBytes(homedir6());
13986
+ const dbBytes = statSessionDbBytes(homedir7());
12714
13987
  void (async () => {
12715
13988
  const reclaimAvailability = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
12716
13989
  if (reclaimAvailability !== null) {
12717
- logActivity(state, {
13990
+ logSessionCleanupActivity(state, "startup", {
12718
13991
  type: "info",
12719
13992
  level: shouldWarnForReclaimSkip(reclaimAvailability.reason) ? "warn" : "info",
12720
- message: `Session cleanup: reclaim preflight skipped (${reclaimAvailability.reason})` + (reclaimAvailability.detail ? `: ${reclaimAvailability.detail}` : "")
13993
+ message: `Session cleanup: reclaim preflight skipped (${reclaimAvailability.reason})` + (reclaimAvailability.detail ? `: ${reclaimAvailability.detail}` : ""),
13994
+ metadata: {
13995
+ outcome: "preflight-skipped",
13996
+ reason: reclaimAvailability.reason,
13997
+ ...reclaimAvailability.detail !== void 0 ? { detail: reclaimAvailability.detail } : {}
13998
+ }
12721
13999
  });
12722
14000
  }
12723
14001
  const sizeWarning = buildSessionStoreSizeWarning({
@@ -12734,9 +14012,15 @@ function scheduleSessionCleanup(state, driver, options) {
12734
14012
  );
12735
14013
  });
12736
14014
  if (!config.enabled) return;
12737
- logActivity(state, {
14015
+ logSessionCleanupActivity(state, "startup", {
12738
14016
  type: "info",
12739
- message: `Session cleanup enabled (age=${config.maxAgeMs ?? "\u2014"}, count=${config.maxCount ?? "\u2014"}, interval=${config.intervalMs}ms)`
14017
+ message: `Session cleanup enabled (age=${config.maxAgeMs ?? "\u2014"}, count=${config.maxCount ?? "\u2014"}, interval=${config.intervalMs}ms)`,
14018
+ metadata: {
14019
+ outcome: "armed",
14020
+ interval_ms: config.intervalMs,
14021
+ max_age_ms: config.maxAgeMs ?? null,
14022
+ max_count: config.maxCount ?? null
14023
+ }
12740
14024
  });
12741
14025
  const interval = setInterval(() => void runSweep(state, driver, config), config.intervalMs);
12742
14026
  const firstSweep = setTimeout(
@@ -12915,7 +14199,7 @@ function scheduleResourceUsageReporting(state, options) {
12915
14199
  });
12916
14200
  return;
12917
14201
  }
12918
- const { collect, stop } = createResourceUsageCollector(homedir6());
14202
+ const { collect, stop } = createResourceUsageCollector(homedir7());
12919
14203
  state.stopResourceUsageSampling = stop;
12920
14204
  let consecutiveFailures = 0;
12921
14205
  const tick = async () => {
@@ -12928,6 +14212,19 @@ function scheduleResourceUsageReporting(state, options) {
12928
14212
  message: `Resource usage collection: ${warning2}`
12929
14213
  });
12930
14214
  }
14215
+ const lowDiskDecision = evaluateLowDiskWarning({
14216
+ ...usage,
14217
+ nowMs: Date.now(),
14218
+ state: state.lowDiskWarningState
14219
+ });
14220
+ state.lowDiskWarningState = lowDiskDecision.state;
14221
+ if (lowDiskDecision.warning !== null) {
14222
+ logActivity(state, {
14223
+ type: "info",
14224
+ level: "warn",
14225
+ message: lowDiskDecision.warning
14226
+ });
14227
+ }
12931
14228
  const result = await reportResourceUsage(state.agentId, state.authHeader, usage);
12932
14229
  if (result.ok) {
12933
14230
  if (consecutiveFailures > 0) {
@@ -13140,7 +14437,7 @@ async function run(options) {
13140
14437
  let fileSyncDirectories;
13141
14438
  try {
13142
14439
  logLevel = resolveLogLevel(options);
13143
- fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir6());
14440
+ fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir7());
13144
14441
  if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
13145
14442
  throw new Error(
13146
14443
  "--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
@@ -13174,6 +14471,7 @@ async function run(options) {
13174
14471
  opencodeClient: null,
13175
14472
  opencodeV2CredentialImportGuard: { inFlight: false },
13176
14473
  sessionDbProvenanceAnomaly: false,
14474
+ sessionStoreHealth: createSessionStoreHealth(),
13177
14475
  opencodeProcess: null,
13178
14476
  stopOpenCodeLogTail: null,
13179
14477
  litestreamProcess: null,
@@ -13191,10 +14489,12 @@ async function run(options) {
13191
14489
  openaiUsageRearm: null,
13192
14490
  resourceUsageTimer: null,
13193
14491
  stopResourceUsageSampling: null,
14492
+ lowDiskWarningState: { worstBand: null, warnedAtMs: null },
13194
14493
  credentialSync: null,
13195
14494
  authHeader: ""
13196
14495
  };
13197
14496
  setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
14497
+ setTelemetryDiagnosticsReporter((message) => log2(state, message, "warn"));
13198
14498
  if (options.credentialSyncMarker) {
13199
14499
  state.credentialSync = createCredentialSync({
13200
14500
  markerPath: options.credentialSyncMarker,
@@ -13363,6 +14663,34 @@ async function run(options) {
13363
14663
  } else {
13364
14664
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
13365
14665
  }
14666
+ if (options.restoreRunnerCredentials || options.credentialSyncMarker) {
14667
+ const logCredentialVaultResolution = (message, level) => {
14668
+ log2(state, message, level);
14669
+ if (state.interactive && !state.json) {
14670
+ logActivity(state, { type: "info", level, message });
14671
+ }
14672
+ };
14673
+ const vaultResolution = await getRunnerCredentialVault(state.agentId, state.authHeader);
14674
+ if (vaultResolution.error) {
14675
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
14676
+ logCredentialVaultResolution(
14677
+ `CREDENTIAL-VAULT-LOOKUP-FAILED: ${vaultResolution.error}; credentials will be read from and written to this runner's own prefix this boot`,
14678
+ "warn"
14679
+ );
14680
+ } else if (vaultResolution.prefix !== null && vaultResolution.vaultId !== null && vaultResolution.source !== null) {
14681
+ process.env.CREDENTIAL_VAULT_PREFIX = vaultResolution.prefix;
14682
+ logCredentialVaultResolution(
14683
+ `CREDENTIAL-VAULT: source=${vaultResolution.source} vault=${vaultResolution.vaultId} prefix=${vaultResolution.prefix}`,
14684
+ "info"
14685
+ );
14686
+ } else {
14687
+ delete process.env.CREDENTIAL_VAULT_PREFIX;
14688
+ logCredentialVaultResolution(
14689
+ "CREDENTIAL-VAULT-NONE: this runner is not linked to a credential vault; using its own prefix",
14690
+ "debug"
14691
+ );
14692
+ }
14693
+ }
13366
14694
  if (options.restoreRunnerCredentials) {
13367
14695
  log2(state, "Restoring runner credentials before starting OpenCode");
13368
14696
  const credentialContext = {
@@ -13380,30 +14708,48 @@ async function run(options) {
13380
14708
  if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
13381
14709
  }
13382
14710
  state.credentialSync?.arm();
13383
- state.stopOpenCodeLogTail = tailOpenCodeLogFile(
13384
- resolveOpenCodeLogPath(homedir6(), process.env),
13385
- createOpenCodeActivityForwarder(() => ({
14711
+ const openCodeActivityForwarder = createOpenCodeActivityForwarder(
14712
+ () => ({
13386
14713
  agentId: state.agentId,
13387
14714
  authHeader: state.authHeader
13388
- }))
13389
- ).stop;
14715
+ }),
14716
+ ({ isDiskFailure, sessionId, cause }) => {
14717
+ if (isDiskFailure) state.sessionStoreHealth.observeUnwritable();
14718
+ if (cause !== null && sessionId !== null) {
14719
+ state.sessionStoreHealth.observeCause(sessionId, cause);
14720
+ }
14721
+ }
14722
+ );
14723
+ const openCodeLogTail = tailOpenCodeLogFile(
14724
+ resolveOpenCodeLogPath(homedir7(), process.env),
14725
+ openCodeActivityForwarder
14726
+ );
14727
+ state.stopOpenCodeLogTail = () => {
14728
+ openCodeActivityForwarder.flush();
14729
+ openCodeLogTail.stop();
14730
+ };
13390
14731
  let sessionDbVerifyFatal = false;
14732
+ let sessionDbVerifyFatalReason;
13391
14733
  if (!options.restoreSessionDb) {
13392
- log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
14734
+ logActivity(state, {
14735
+ type: "info",
14736
+ level: "warn",
14737
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=flag-absent \u2014 Skipping session-DB restore: --restore-session-db was not passed"
14738
+ });
13393
14739
  } else {
13394
14740
  const health = await checkOpenCodeHealth(state.port);
13395
14741
  if (health.healthy) {
13396
- log2(
13397
- state,
13398
- "Skipping session-DB restore: OpenCode is already serving this database",
13399
- "debug"
13400
- );
14742
+ logActivity(state, {
14743
+ type: "info",
14744
+ level: "warn",
14745
+ message: "SESSION-DB-VERIFY-SKIPPED: reason=opencode-serving \u2014 Skipping session-DB restore: OpenCode is already serving this database"
14746
+ });
13401
14747
  } else if (health.authFailed) {
13402
- log2(
13403
- state,
13404
- "Skipping session-DB restore: OpenCode answered with an authentication challenge; assuming this database is in use",
13405
- "debug"
13406
- );
14748
+ logActivity(state, {
14749
+ type: "info",
14750
+ level: "warn",
14751
+ 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"
14752
+ });
13407
14753
  } else {
13408
14754
  const result = await restoreAndVerifySessionDb({
13409
14755
  dbPath: sessionDbPath(),
@@ -13420,12 +14766,13 @@ async function run(options) {
13420
14766
  reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
13421
14767
  });
13422
14768
  sessionDbVerifyFatal = result.verifyFatal;
14769
+ sessionDbVerifyFatalReason = result.verifyFatalReason;
13423
14770
  }
13424
14771
  }
13425
14772
  reportSessionDbRecovery(state);
13426
14773
  if (sessionDbVerifyFatal) {
13427
14774
  throw new Error(
13428
- "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
14775
+ 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`
13429
14776
  );
13430
14777
  }
13431
14778
  applyRunnerOpenCodeConfig({
@@ -13503,7 +14850,7 @@ async function run(options) {
13503
14850
  const provenance = checkSessionDbProvenance({
13504
14851
  dbPath: sessionDbPath(),
13505
14852
  currentVersion: state.opencodeVersion,
13506
- homeDir: homedir6(),
14853
+ homeDir: homedir7(),
13507
14854
  env: process.env
13508
14855
  });
13509
14856
  if (provenance.anomaly) {
@@ -13670,10 +15017,11 @@ async function run(options) {
13670
15017
  getAuthHeader: () => state.authHeader,
13671
15018
  conversationFilter: state.conversationFilter,
13672
15019
  stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
15020
+ sessionStoreHealth: state.sessionStoreHealth,
13673
15021
  // #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
13674
15022
  // REJECTED with `file_sync_disabled` on the ack, not silently ignored.
13675
15023
  fileSyncDirectories,
13676
- homeDir: homedir6(),
15024
+ homeDir: homedir7(),
13677
15025
  maxActiveSessions,
13678
15026
  log: (entry) => (
13679
15027
  // Thread the driver's real level straight through so `debug`/`warn`
@@ -13695,12 +15043,13 @@ async function run(options) {
13695
15043
  openCodePassword: state.opencodePassword,
13696
15044
  isRunning: () => state.running,
13697
15045
  events: {
13698
- onConnected: (agentId, isReconnect) => {
15046
+ onConnected: (agentId, isReconnect, transition) => {
13699
15047
  state.connected = true;
13700
15048
  state.agentId = agentId;
15049
+ const connectionMessage = isReconnect ? `Tunnel reconnected (runner: ${agentId}, generation: ${transition.generation}, attempt: ${transition.attempt}, offline for ${transition.downtimeMs}ms)` : `Tunnel connected (runner: ${agentId}, generation: ${transition.generation})`;
13701
15050
  logActivity(state, {
13702
15051
  type: "info",
13703
- message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (runner: ${agentId})`
15052
+ message: connectionMessage
13704
15053
  });
13705
15054
  if (options.tunnelReadyFile) {
13706
15055
  const marker = writeTunnelReadyMarker(options.tunnelReadyFile, agentId);
@@ -13721,7 +15070,12 @@ async function run(options) {
13721
15070
  version: state.opencodeVersion,
13722
15071
  major: state.opencodeApiVersion,
13723
15072
  connected: state.opencodeConnected
13724
- })
15073
+ }),
15074
+ connection_generation: transition.generation,
15075
+ ...isReconnect && transition.downtimeMs !== null ? {
15076
+ reconnect_attempt: transition.attempt,
15077
+ downtime_ms: transition.downtimeMs
15078
+ } : {}
13725
15079
  });
13726
15080
  if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
13727
15081
  if (state.interactive) displayStatus(state);
@@ -13743,13 +15097,18 @@ async function run(options) {
13743
15097
  if (state.interactive) displayStatus(state);
13744
15098
  });
13745
15099
  },
13746
- onDisconnected: (code, reason) => {
15100
+ onDisconnected: (code, reason, transition) => {
13747
15101
  state.connected = false;
15102
+ const safeReason2 = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
13748
15103
  logActivity(state, {
13749
15104
  type: "info",
13750
- message: `Tunnel disconnected (code: ${code}, reason: ${reason})`
15105
+ message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason2})`
15106
+ });
15107
+ emitAgentDisconnected(state.agentId, {
15108
+ code,
15109
+ reason: safeReason2,
15110
+ connection_generation: transition.generation
13751
15111
  });
13752
- emitAgentDisconnected(state.agentId, { code, reason });
13753
15112
  if (state.interactive) displayStatus(state);
13754
15113
  },
13755
15114
  onError: (error2) => {
@@ -13849,7 +15208,7 @@ async function run(options) {
13849
15208
  },
13850
15209
  report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
13851
15210
  isLocalCredentialProblem: isLocalCredentialProblem2,
13852
- forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
15211
+ forcedOnHint: state.opencodeApiVersion === "v2" ? "OpenCode V2 keeps its connected ChatGPT login in a store Evident cannot read" : "connect a ChatGPT account to this runner, or run `opencode auth login`",
13853
15212
  firstDelayMs: firstReportDelayMs,
13854
15213
  nextDelayMs: usageReportDelayMs,
13855
15214
  failureLogLevel: usageReportFailureLogLevel
@@ -13892,7 +15251,7 @@ async function run(options) {
13892
15251
  }
13893
15252
 
13894
15253
  // src/index.ts
13895
- var { version } = createRequire2(import.meta.url)("../package.json");
15254
+ var version = getCliVersion();
13896
15255
  var program = new Command();
13897
15256
  program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
13898
15257
  "--endpoint <url>",
@@ -13910,6 +15269,7 @@ program.command("login").description("Authenticate with Evident").option("--toke
13910
15269
  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 }));
13911
15270
  program.command("whoami").description("Show the currently logged in user").action(whoami);
13912
15271
  program.command("status").description("Check whether the configured credentials can reach Evident").option("--json", "Output in JSON format").action((options) => status({ json: options.json }));
15272
+ program.command("credential-vault-env").description("Print the shell assignment a runner image evals before starting the synchroniser").action(credentialVaultEnv);
13913
15273
  program.command("claude-usage").description("[spike] Show Claude subscription usage (requires a local `claude login`)").action(claudeUsage);
13914
15274
  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(
13915
15275
  "-a, --agent [id]",