@evident-ai/cli 3.4.1-dev.8b081ae → 3.4.1-dev.8c54b1f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/index.ts
4
- import { createRequire } from "module";
4
+ import { createRequire as createRequire2 } from "module";
5
5
  import { Command } from "commander";
6
6
 
7
7
  // src/commands/login.ts
@@ -11,8 +11,8 @@ import chalk2 from "chalk";
11
11
 
12
12
  // src/lib/config.ts
13
13
  import Conf from "conf";
14
- import { chmodSync, existsSync, statSync } from "fs";
15
- import { dirname } from "path";
14
+ import { chmodSync, existsSync, statSync } from "node:fs";
15
+ import { dirname } from "node:path";
16
16
  var PRODUCTION_API_URL = "https://api.production.evident.run/v1";
17
17
  var PRODUCTION_TUNNEL_URL = "wss://tunnel.production.evident.run";
18
18
  var defaults = {
@@ -371,14 +371,14 @@ function blank() {
371
371
  console.log();
372
372
  }
373
373
  function waitForEnter(prompt = "Press Enter to continue...") {
374
- return new Promise((resolve3) => {
374
+ return new Promise((resolve4) => {
375
375
  process.stdout.write(chalk.dim(prompt));
376
376
  const handler = () => {
377
377
  process.stdin.removeListener("data", handler);
378
378
  process.stdin.setRawMode?.(false);
379
379
  process.stdin.pause();
380
380
  console.log();
381
- resolve3();
381
+ resolve4();
382
382
  };
383
383
  if (process.stdin.isTTY) {
384
384
  process.stdin.setRawMode?.(true);
@@ -388,7 +388,7 @@ function waitForEnter(prompt = "Press Enter to continue...") {
388
388
  });
389
389
  }
390
390
  function sleep(ms) {
391
- return new Promise((resolve3) => setTimeout(resolve3, ms));
391
+ return new Promise((resolve4) => setTimeout(resolve4, ms));
392
392
  }
393
393
 
394
394
  // src/commands/login.ts
@@ -466,19 +466,19 @@ async function tokenLogin() {
466
466
  );
467
467
  blank();
468
468
  process.stdout.write("Paste token: ");
469
- const token = await new Promise((resolve3) => {
469
+ const token = await new Promise((resolve4) => {
470
470
  let data = "";
471
471
  process.stdin.setEncoding("utf8");
472
472
  process.stdin.on("data", (chunk) => {
473
473
  data += chunk;
474
474
  });
475
475
  process.stdin.on("end", () => {
476
- resolve3(data.trim());
476
+ resolve4(data.trim());
477
477
  });
478
478
  if (process.stdin.isTTY) {
479
479
  process.stdin.once("data", (chunk) => {
480
480
  process.stdin.pause();
481
- resolve3(chunk.toString().trim());
481
+ resolve4(chunk.toString().trim());
482
482
  });
483
483
  process.stdin.resume();
484
484
  }
@@ -623,6 +623,19 @@ function isInteractive(jsonOutput) {
623
623
  return true;
624
624
  }
625
625
 
626
+ // src/lib/subscription-usage-report.ts
627
+ function toReportedSubscription(collected) {
628
+ if (!collected) return null;
629
+ if (collected.ownerEmail === null && collected.planType === null && collected.organizationName === null) {
630
+ return null;
631
+ }
632
+ return {
633
+ owner_email: collected.ownerEmail,
634
+ plan_type: collected.planType,
635
+ organization_name: collected.organizationName
636
+ };
637
+ }
638
+
626
639
  // src/commands/agent-lookup.ts
627
640
  async function readErrorMessage(response) {
628
641
  const text = await response.text().catch(() => "");
@@ -722,14 +735,6 @@ function toReportedWindow(window) {
722
735
  if (!window) return null;
723
736
  return { utilization: window.utilization, resets_at: window.resetsAt };
724
737
  }
725
- function toReportedOwner(snapshot) {
726
- if (!snapshot.owner) return null;
727
- return {
728
- email: snapshot.owner.email,
729
- organization_name: snapshot.owner.organizationName,
730
- rate_limit_tier: snapshot.owner.rateLimitTier
731
- };
732
- }
733
738
  async function reportClaudeUsage(agentId, authHeader, snapshot) {
734
739
  try {
735
740
  const apiUrl = getApiUrlConfig();
@@ -739,7 +744,7 @@ async function reportClaudeUsage(agentId, authHeader, snapshot) {
739
744
  body: JSON.stringify({
740
745
  five_hour: toReportedWindow(snapshot.fiveHour),
741
746
  seven_day: toReportedWindow(snapshot.sevenDay),
742
- owner: toReportedOwner(snapshot)
747
+ subscription: toReportedSubscription(snapshot.subscription)
743
748
  }),
744
749
  signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
745
750
  });
@@ -773,7 +778,8 @@ async function reportOpenAiUsage(agentId, authHeader, snapshot) {
773
778
  primary: toReportedOpenAiWindow(snapshot.primary),
774
779
  secondary: toReportedOpenAiWindow(snapshot.secondary),
775
780
  has_credits: snapshot.hasCredits,
776
- credits_unlimited: snapshot.creditsUnlimited
781
+ credits_unlimited: snapshot.creditsUnlimited,
782
+ subscription: toReportedSubscription(snapshot.subscription)
777
783
  }),
778
784
  signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
779
785
  });
@@ -797,6 +803,7 @@ async function reportResourceUsage(agentId, authHeader, usage) {
797
803
  headers: { Authorization: authHeader, "Content-Type": "application/json" },
798
804
  body: JSON.stringify({
799
805
  cpu_percent: usage.cpuPercent,
806
+ cpu_peak_percent: usage.cpuPeakPercent,
800
807
  cpu_count: usage.cpuCount,
801
808
  memory_total_bytes: usage.memoryTotalBytes,
802
809
  memory_available_bytes: usage.memoryAvailableBytes,
@@ -1000,10 +1007,10 @@ async function status(options = {}) {
1000
1007
  }
1001
1008
 
1002
1009
  // src/lib/claude-usage.ts
1003
- import { execFileSync } from "child_process";
1004
- import { readFileSync } from "fs";
1005
- import { homedir } from "os";
1006
- import { join } from "path";
1010
+ import { execFileSync } from "node:child_process";
1011
+ import { readFileSync } from "node:fs";
1012
+ import { homedir } from "node:os";
1013
+ import { join } from "node:path";
1007
1014
  var CLAUDE_USAGE_URL = "https://api.anthropic.com/api/oauth/usage";
1008
1015
  var CLAUDE_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
1009
1016
  var CLAUDE_PROFILE_TIMEOUT_MS = 2e3;
@@ -1088,7 +1095,7 @@ function ownerLookupFailure(error2) {
1088
1095
  }
1089
1096
  async function getClaudeUsageOwner(accessToken) {
1090
1097
  if (cachedOwner?.accessToken === accessToken) {
1091
- return { owner: cachedOwner.owner, ownerLookupError: null };
1098
+ return { subscription: cachedOwner.owner, ownerLookupError: null };
1092
1099
  }
1093
1100
  try {
1094
1101
  const response = await fetch(CLAUDE_PROFILE_URL, {
@@ -1100,27 +1107,27 @@ async function getClaudeUsageOwner(accessToken) {
1100
1107
  signal: AbortSignal.timeout(CLAUDE_PROFILE_TIMEOUT_MS)
1101
1108
  });
1102
1109
  if (!response.ok) {
1103
- return { owner: null, ownerLookupError: `HTTP ${response.status}` };
1110
+ return { subscription: null, ownerLookupError: `HTTP ${response.status}` };
1104
1111
  }
1105
1112
  let body;
1106
1113
  try {
1107
1114
  body = await response.json();
1108
1115
  } catch (error2) {
1109
- return { owner: null, ownerLookupError: "malformed response" };
1116
+ return { subscription: null, ownerLookupError: "malformed response" };
1110
1117
  }
1111
1118
  const profile = body;
1112
1119
  if (typeof profile.account?.email !== "string" || !profile.account.email.trim()) {
1113
- return { owner: null, ownerLookupError: "malformed response" };
1120
+ return { subscription: null, ownerLookupError: "malformed response" };
1114
1121
  }
1115
- const owner = {
1116
- email: profile.account.email,
1122
+ const subscription = {
1123
+ ownerEmail: profile.account.email,
1117
1124
  organizationName: typeof profile.organization?.name === "string" && profile.organization.name.trim() ? profile.organization.name.trim() : null,
1118
- rateLimitTier: typeof profile.organization?.rate_limit_tier === "string" && profile.organization.rate_limit_tier.trim() ? profile.organization.rate_limit_tier.trim() : null
1125
+ planType: typeof profile.organization?.rate_limit_tier === "string" && profile.organization.rate_limit_tier.trim() ? profile.organization.rate_limit_tier.trim() : null
1119
1126
  };
1120
- cachedOwner = { accessToken, owner };
1121
- return { owner, ownerLookupError: null };
1127
+ cachedOwner = { accessToken, owner: subscription };
1128
+ return { subscription, ownerLookupError: null };
1122
1129
  } catch (error2) {
1123
- return { owner: null, ownerLookupError: ownerLookupFailure(error2) };
1130
+ return { subscription: null, ownerLookupError: ownerLookupFailure(error2) };
1124
1131
  }
1125
1132
  }
1126
1133
  async function getClaudeUsage() {
@@ -1149,11 +1156,11 @@ async function getClaudeUsage() {
1149
1156
  throw new ClaudeUsageError(`Claude usage request failed: HTTP ${res.status}`, "request_failed");
1150
1157
  }
1151
1158
  const body = await res.json();
1152
- const { owner, ownerLookupError } = await getClaudeUsageOwner(credentials2.accessToken);
1159
+ const { subscription, ownerLookupError } = await getClaudeUsageOwner(credentials2.accessToken);
1153
1160
  return {
1154
1161
  fiveHour: toWindow(body.five_hour),
1155
1162
  sevenDay: toWindow(body.seven_day),
1156
- owner,
1163
+ subscription,
1157
1164
  ownerLookupError
1158
1165
  };
1159
1166
  }
@@ -1183,9 +1190,10 @@ async function claudeUsage() {
1183
1190
  }
1184
1191
 
1185
1192
  // src/commands/run.ts
1186
- import { homedir as homedir4 } from "os";
1187
- import { isAbsolute as isAbsolute2, join as join7, parse, resolve as resolvePath } from "path";
1188
- import chalk6 from "chalk";
1193
+ import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync6 } from "node:fs";
1194
+ import { homedir as homedir6 } from "node:os";
1195
+ import { isAbsolute as isAbsolute3, join as join10, parse, resolve as resolvePath2 } from "node:path";
1196
+ import chalk7 from "chalk";
1189
1197
 
1190
1198
  // ../../packages/types/src/agents/index.ts
1191
1199
  var MICROVM_MAX_LIFETIME_MS = 8 * 60 * 6e4;
@@ -1206,6 +1214,7 @@ var TelemetryEventTypes = {
1206
1214
  // ../../packages/types/src/tunnel/index.ts
1207
1215
  var MAX_FRAME_BYTES = 256 * 1024;
1208
1216
  var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
1217
+ var TUNNEL_USAGE_REARM_PING_PATH = "/__evident/usage-rearm";
1209
1218
 
1210
1219
  // ../../packages/types/src/runner-files.ts
1211
1220
  var MAX_FILE_PUSH_BYTES = 64 * 1024;
@@ -1243,7 +1252,7 @@ function stripQuery(url) {
1243
1252
 
1244
1253
  // src/commands/run.ts
1245
1254
  import ora3 from "ora";
1246
- import { select as select3 } from "@inquirer/prompts";
1255
+ import { select as select4 } from "@inquirer/prompts";
1247
1256
 
1248
1257
  // src/lib/telemetry.ts
1249
1258
  var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
@@ -1416,12 +1425,50 @@ var SEVERITY_BY_LEVEL = {
1416
1425
  warn: "warning",
1417
1426
  error: "error"
1418
1427
  };
1428
+ function parseOpenCodeLogLine(line) {
1429
+ const normalisedLine = line.replace(/\r$/, "");
1430
+ const levelMatch = normalisedLine.match(/(?:^|\s)level=(\w+)/i);
1431
+ if (!levelMatch) return null;
1432
+ const level = levelMatch[1].toUpperCase();
1433
+ if (level !== "WARN" && level !== "ERROR") return null;
1434
+ const sessionMatch = normalisedLine.match(/(?:^|\s)sessionID=(\S+)/);
1435
+ return { level: level === "WARN" ? "warn" : "error", sessionID: sessionMatch?.[1] };
1436
+ }
1437
+ var MAX_LINE_BUFFER_BYTES = 16 * 1024;
1438
+ function createOpenCodeActivityForwarder(getContext) {
1439
+ let buffer = Buffer.alloc(0);
1440
+ const flushLine = (line) => {
1441
+ const parsed = parseOpenCodeLogLine(line);
1442
+ if (!parsed) return;
1443
+ forwardRunnerActivity(
1444
+ {
1445
+ level: parsed.level,
1446
+ error: line,
1447
+ metadata: parsed.sessionID ? { sessionID: parsed.sessionID } : void 0,
1448
+ source: "opencode"
1449
+ },
1450
+ getContext()
1451
+ );
1452
+ };
1453
+ return (chunk) => {
1454
+ buffer = Buffer.concat([buffer, Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, "utf-8")]);
1455
+ let newlineIndex;
1456
+ while ((newlineIndex = buffer.indexOf(10)) !== -1) {
1457
+ flushLine(buffer.subarray(0, newlineIndex).toString("utf-8").replace(/\r$/, ""));
1458
+ buffer = buffer.subarray(newlineIndex + 1);
1459
+ }
1460
+ if (buffer.length > MAX_LINE_BUFFER_BYTES) {
1461
+ flushLine(buffer.toString("utf-8"));
1462
+ buffer = Buffer.alloc(0);
1463
+ }
1464
+ };
1465
+ }
1419
1466
  var MAX_MESSAGE_LENGTH = 500;
1420
1467
  var MAX_METADATA_VALUE_LENGTH = 200;
1421
1468
  var MAX_METADATA_ENTRIES = 20;
1422
1469
  var TRUNCATION_MARKER = "\u2026";
1423
1470
  function redact(message) {
1424
- return message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/https?:\/\/\S+/g, "<url>");
1471
+ 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>");
1425
1472
  }
1426
1473
  function truncate(message) {
1427
1474
  if (message.length <= MAX_MESSAGE_LENGTH) return message;
@@ -1447,43 +1494,47 @@ function sanitiseMetadata(metadata) {
1447
1494
  }
1448
1495
  var RATE_LIMIT_WINDOW_MS = 6e4;
1449
1496
  var RATE_LIMIT_MAX_EVENTS = 30;
1450
- var windowStartedAt = 0;
1451
- var windowCount = 0;
1452
- var windowDroppedCount = 0;
1453
- function admitUnderRateLimit(now) {
1454
- if (now - windowStartedAt >= RATE_LIMIT_WINDOW_MS) {
1455
- if (windowDroppedCount > 0) {
1497
+ var rateWindows = /* @__PURE__ */ new Map();
1498
+ function admitUnderRateLimit(source, now) {
1499
+ let window = rateWindows.get(source);
1500
+ if (!window) {
1501
+ window = { windowStartedAt: 0, windowCount: 0, windowDroppedCount: 0 };
1502
+ rateWindows.set(source, window);
1503
+ }
1504
+ if (now - window.windowStartedAt >= RATE_LIMIT_WINDOW_MS) {
1505
+ if (window.windowDroppedCount > 0) {
1456
1506
  console.error(
1457
- `[runner-activity-telemetry] rate cap reached: dropped ${windowDroppedCount} ${windowDroppedCount === 1 ? "entry" : "entries"} in the last ${RATE_LIMIT_WINDOW_MS / 1e3}s (cap ${RATE_LIMIT_MAX_EVENTS}/min)`
1507
+ `[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}"`
1458
1508
  );
1459
1509
  }
1460
- windowStartedAt = now;
1461
- windowCount = 0;
1462
- windowDroppedCount = 0;
1510
+ window.windowStartedAt = now;
1511
+ window.windowCount = 0;
1512
+ window.windowDroppedCount = 0;
1463
1513
  }
1464
- if (windowCount >= RATE_LIMIT_MAX_EVENTS) {
1465
- windowDroppedCount++;
1466
- if (windowDroppedCount === 1) {
1514
+ if (window.windowCount >= RATE_LIMIT_MAX_EVENTS) {
1515
+ window.windowDroppedCount++;
1516
+ if (window.windowDroppedCount === 1) {
1467
1517
  console.error(
1468
- `[runner-activity-telemetry] rate cap reached (${RATE_LIMIT_MAX_EVENTS} per ${RATE_LIMIT_WINDOW_MS / 1e3}s) \u2014 dropping further entries this window`
1518
+ `[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}"`
1469
1519
  );
1470
1520
  }
1471
1521
  return false;
1472
1522
  }
1473
- windowCount++;
1523
+ window.windowCount++;
1474
1524
  return true;
1475
1525
  }
1476
1526
  function forwardRunnerActivity(entry, context) {
1477
1527
  try {
1478
1528
  if (!FORWARDED_LEVELS.has(entry.level)) return;
1479
1529
  if (!context.agentId || !context.authHeader) return;
1480
- if (!admitUnderRateLimit(Date.now())) return;
1530
+ const source = entry.source ?? "cli.run";
1531
+ if (!admitUnderRateLimit(source, Date.now())) return;
1481
1532
  const rawMessage = entry.error ?? entry.message ?? "";
1482
1533
  const message = truncate(redact(rawMessage));
1483
1534
  logEvent(TelemetryEventTypes.RUNNER_ACTIVITY, {
1484
1535
  severity: SEVERITY_BY_LEVEL[entry.level],
1485
1536
  message,
1486
- metadata: { ...sanitiseMetadata(entry.metadata), source: "cli.run" },
1537
+ metadata: { ...sanitiseMetadata(entry.metadata), source },
1487
1538
  agentId: context.agentId
1488
1539
  });
1489
1540
  } catch (err) {
@@ -1494,8 +1545,8 @@ function forwardRunnerActivity(entry, context) {
1494
1545
  }
1495
1546
 
1496
1547
  // src/lib/opencode/session-db-recovery-report.ts
1497
- import { readFileSync as readFileSync2, unlinkSync } from "fs";
1498
- import { join as join2 } from "path";
1548
+ import { readFileSync as readFileSync2, unlinkSync } from "node:fs";
1549
+ import { join as join2 } from "node:path";
1499
1550
  function sessionDbRecoveryReportPath(homeDir, env) {
1500
1551
  const override = env.EVIDENT_SESSION_DB_RECOVERY_REPORT?.trim();
1501
1552
  return override || join2(homeDir, ".local", "state", "evident", "session-db-recovery.jsonl");
@@ -1524,7 +1575,14 @@ function drainSessionDbRecoveryReport({
1524
1575
  skippedLines++;
1525
1576
  return [];
1526
1577
  }
1527
- return [value];
1578
+ return [
1579
+ {
1580
+ ...value,
1581
+ provenance_reason: value.provenance_reason ?? null,
1582
+ provenance_migration_delta: value.provenance_migration_delta ?? null,
1583
+ replication_suspended: value.replication_suspended ?? false
1584
+ }
1585
+ ];
1528
1586
  } catch (error2) {
1529
1587
  skippedLines++;
1530
1588
  console.error(
@@ -1549,12 +1607,39 @@ function buildSessionDbRecoveryActivity(record) {
1549
1607
  const level = record.severity === "warning" ? "warn" : record.severity === "error" ? "error" : null;
1550
1608
  if (!level) return null;
1551
1609
  const noVerifiedPoint = record.verified_restore_point ? ` The verified restore point is ${record.verified_restore_point}.` : " No verified restore point is known.";
1610
+ const replication = record.replication_suspended ? " This start is not backing up its new session history; restart after fixing the cause." : "";
1611
+ const giveupMessage = (() => {
1612
+ switch (record.reason) {
1613
+ case "restore_deadline_exceeded":
1614
+ return "The restore did not finish before this startup deadline. Earlier sessions are unavailable for this start; collect the SESSION-DB-RESTORE-TRUNCATED boot log before retrying.";
1615
+ case "restore_tool_unusable":
1616
+ case "classification_unrecognised":
1617
+ return "The restore or classification tool produced an unsupported result. Inspect the named boot marker or log and repair the runner image or bundle before restarting.";
1618
+ case "synchroniser_config_unevaluable":
1619
+ case "synchroniser_config_incomplete":
1620
+ return "The installed hook and synchroniser bundle could not agree on configuration. Inspect the named boot marker or log and repair the runner image or bundle before restarting.";
1621
+ case "synchroniser_config_unresolved":
1622
+ return "The synchroniser configuration could not be resolved. Inspect the preceding boot error and repair the identified runner image, bundle, or local environment before restarting.";
1623
+ case "litestream_config_unavailable":
1624
+ return "The local Litestream configuration could not be generated or written. Inspect the preceding boot error and repair the runner image, bundle, local environment, or write permissions before restarting.";
1625
+ case "classification_fatal":
1626
+ return "Session history could not be restored because backup setup could not be established. Inspect the named boot marker or log and repair the identified backup configuration or permissions.";
1627
+ default:
1628
+ return null;
1629
+ }
1630
+ })();
1631
+ if (giveupMessage)
1632
+ return {
1633
+ level,
1634
+ metadata: withoutContractFields(record),
1635
+ message: `${giveupMessage}${replication}`
1636
+ };
1552
1637
  switch (record.outcome) {
1553
1638
  case "fresh_session_db":
1554
1639
  return {
1555
1640
  level,
1556
1641
  metadata: withoutContractFields(record),
1557
- message: `This runner started with a fresh session database. Earlier sessions are unavailable, and queued conversation or session references from before this start may no longer resolve.${noVerifiedPoint} Review the runner's backup configuration before relying on restored session history.`
1642
+ message: `This runner started with a fresh session database. Earlier sessions are unavailable, and queued conversation or session references from before this start may no longer resolve.${noVerifiedPoint} Review the runner's backup configuration before relying on restored session history.${replication}`
1558
1643
  };
1559
1644
  case "restore_retried":
1560
1645
  return {
@@ -1592,7 +1677,7 @@ function buildSessionDbRecoveryActivity(record) {
1592
1677
  return {
1593
1678
  level,
1594
1679
  metadata: withoutContractFields(record),
1595
- message: "This runner started with a fresh session database because it could not verify where its session history is backed up. Nothing in the backup was changed; check the runner backup configuration and permissions."
1680
+ 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}`
1596
1681
  };
1597
1682
  case "session_db_boot_refused":
1598
1683
  return {
@@ -1600,6 +1685,12 @@ function buildSessionDbRecoveryActivity(record) {
1600
1685
  metadata: withoutContractFields(record),
1601
1686
  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.`
1602
1687
  };
1688
+ case "schema_provenance_mismatch":
1689
+ return {
1690
+ level,
1691
+ metadata: withoutContractFields(record),
1692
+ message: `Session database schema provenance mismatch for ${record.dbPath ?? record.db_path ?? "unknown"}: recorded version=${record.recorded_version ?? "unknown"}, current version=${record.current_version ?? "unknown"}, reason=${record.provenance_reason ?? "unknown"}, migration delta=${record.provenance_migration_delta ?? "unknown"}. Inspect the session database and runner backup before continuing.`
1693
+ };
1603
1694
  default:
1604
1695
  return null;
1605
1696
  }
@@ -1614,7 +1705,8 @@ var OUTCOMES = /* @__PURE__ */ new Set([
1614
1705
  "fresh_session_db",
1615
1706
  "history_rolled_back",
1616
1707
  "restore_misconfigured",
1617
- "session_db_boot_refused"
1708
+ "session_db_boot_refused",
1709
+ "schema_provenance_mismatch"
1618
1710
  ]);
1619
1711
  var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
1620
1712
  var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
@@ -1632,7 +1724,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
1632
1724
  function isSessionDbRecoveryRecord(value) {
1633
1725
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
1634
1726
  const record = value;
1635
- return record.v === 1 && record.event === "session_db_recovery" && typeof record.at === "string" && STAGES.has(record.stage) && OUTCOMES.has(record.outcome) && SEVERITIES.has(record.severity) && typeof record.reason === "string" && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
1727
+ return record.v === 1 && record.event === "session_db_recovery" && typeof record.at === "string" && STAGES.has(record.stage) && OUTCOMES.has(record.outcome) && SEVERITIES.has(record.severity) && typeof record.reason === "string" && (record.replication_suspended === void 0 || typeof record.replication_suspended === "boolean") && (record.provenance_reason === void 0 || record.provenance_reason === null || typeof record.provenance_reason === "string") && (record.provenance_migration_delta === void 0 || record.provenance_migration_delta === null || Number.isInteger(record.provenance_migration_delta)) && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
1636
1728
  (field) => record[field] === null || typeof record[field] === "string"
1637
1729
  );
1638
1730
  }
@@ -1661,11 +1753,667 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
1661
1753
  if (health.healthy) {
1662
1754
  return health;
1663
1755
  }
1664
- await new Promise((resolve3) => setTimeout(resolve3, 1e3));
1756
+ await new Promise((resolve4) => setTimeout(resolve4, 1e3));
1665
1757
  }
1666
1758
  return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
1667
1759
  }
1668
1760
 
1761
+ // src/lib/opencode/session-db-boot.ts
1762
+ import { spawn as spawn2 } from "node:child_process";
1763
+ import { mkdirSync, statSync as statSync2, unlinkSync as unlinkSync2, writeFileSync } from "node:fs";
1764
+ import { homedir as homedir2 } from "node:os";
1765
+ import { dirname as dirname2, resolve as resolvePath } from "node:path";
1766
+
1767
+ // src/lib/runner-synchroniser.ts
1768
+ import { spawn } from "node:child_process";
1769
+ function appendError(stderr, error2) {
1770
+ const message = error2 instanceof Error ? error2.message : String(error2);
1771
+ return stderr === "" ? message : `${stderr}
1772
+ ${message}`;
1773
+ }
1774
+ function runSynchroniser(args, opts) {
1775
+ return new Promise((resolve4) => {
1776
+ let child;
1777
+ let stdout = "";
1778
+ let stderr = "";
1779
+ let settled = false;
1780
+ const timer = {};
1781
+ let abortListener;
1782
+ let spawnListener;
1783
+ const finish = (result) => {
1784
+ if (settled) return;
1785
+ settled = true;
1786
+ if (timer.handle) clearTimeout(timer.handle);
1787
+ if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
1788
+ if (spawnListener) child.removeListener("spawn", spawnListener);
1789
+ resolve4(result);
1790
+ };
1791
+ try {
1792
+ child = spawn("runner-synchroniser", args, {
1793
+ env: opts.env ?? process.env,
1794
+ stdio: ["ignore", "pipe", "pipe"]
1795
+ });
1796
+ } catch (error2) {
1797
+ finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
1798
+ return;
1799
+ }
1800
+ child.stdout?.setEncoding("utf8");
1801
+ child.stdout?.on("data", (chunk) => {
1802
+ stdout += chunk;
1803
+ });
1804
+ child.stderr?.setEncoding("utf8");
1805
+ child.stderr?.on("data", (chunk) => {
1806
+ stderr += chunk;
1807
+ });
1808
+ child.once("error", (error2) => {
1809
+ finish({ code: null, stdout, stderr: appendError(stderr, error2), timedOut: false });
1810
+ });
1811
+ child.once("close", (code) => {
1812
+ finish({ code, stdout, stderr, timedOut: false });
1813
+ });
1814
+ if (opts.signal) {
1815
+ const killChild = () => {
1816
+ if (child.pid === void 0) {
1817
+ if (!spawnListener) {
1818
+ spawnListener = killChild;
1819
+ child.once("spawn", spawnListener);
1820
+ }
1821
+ return;
1822
+ }
1823
+ child.kill("SIGKILL");
1824
+ };
1825
+ abortListener = killChild;
1826
+ if (opts.signal.aborted) {
1827
+ abortListener();
1828
+ } else {
1829
+ opts.signal.addEventListener("abort", abortListener, { once: true });
1830
+ if (opts.signal.aborted) abortListener();
1831
+ }
1832
+ }
1833
+ timer.handle = setTimeout(
1834
+ () => {
1835
+ child.kill("SIGKILL");
1836
+ finish({ code: null, stdout, stderr, timedOut: true });
1837
+ },
1838
+ Math.max(0, opts.timeoutMs)
1839
+ );
1840
+ });
1841
+ }
1842
+
1843
+ // src/lib/opencode/session-db-boot.ts
1844
+ var SESSION_DB_RESTORE_TIMEOUT_MS = 3e5;
1845
+ var SESSION_DB_VERIFY_TIMEOUT_MS = 12e4;
1846
+ var SESSION_DB_SYNCHRONISER_TIMEOUT_MS = 12e4;
1847
+ var SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS = 120;
1848
+ function commandError(result) {
1849
+ return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
1850
+ }
1851
+ function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
1852
+ options.reportRecovery({
1853
+ v: 1,
1854
+ event: "session_db_recovery",
1855
+ at: (/* @__PURE__ */ new Date()).toISOString(),
1856
+ stage,
1857
+ outcome,
1858
+ severity: "error",
1859
+ reason,
1860
+ litestream_exit_code: litestreamExitCode,
1861
+ attempt: null,
1862
+ replica_objects: null,
1863
+ replica_bytes: null,
1864
+ quarantine_destination: null,
1865
+ quarantined_objects: null,
1866
+ quarantine_failed_objects: null,
1867
+ quarantined_bytes: null,
1868
+ verified_restore_point: null,
1869
+ restore_points_tried: null,
1870
+ provenance_reason: null,
1871
+ provenance_migration_delta: null,
1872
+ replication_suspended: stage === "restore"
1873
+ });
1874
+ }
1875
+ function clearMarker(options) {
1876
+ if (!options.noReplicateMarker) return;
1877
+ try {
1878
+ unlinkSync2(options.noReplicateMarker);
1879
+ } catch (error2) {
1880
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return;
1881
+ options.log(
1882
+ `Could not clear the previous session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1883
+ "warn"
1884
+ );
1885
+ }
1886
+ }
1887
+ function markNoReplicate(options, message) {
1888
+ if (options.noReplicateMarker) {
1889
+ try {
1890
+ mkdirSync(dirname2(options.noReplicateMarker), { recursive: true });
1891
+ writeFileSync(options.noReplicateMarker, "");
1892
+ } catch (error2) {
1893
+ options.log(
1894
+ `Could not write session-DB no-replicate marker ${options.noReplicateMarker}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1895
+ "error"
1896
+ );
1897
+ }
1898
+ }
1899
+ options.log(`SESSION-DB-NO-REPLICATE: ${message}`, "warn");
1900
+ }
1901
+ function discardSessionDbDebris(options) {
1902
+ for (const path of [options.dbPath, `${options.dbPath}-wal`, `${options.dbPath}-shm`]) {
1903
+ try {
1904
+ unlinkSync2(path);
1905
+ } catch (error2) {
1906
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") continue;
1907
+ options.log(
1908
+ `Could not remove session-DB debris ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`,
1909
+ "warn"
1910
+ );
1911
+ }
1912
+ }
1913
+ }
1914
+ function splitDiagnostics(text) {
1915
+ return text.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
1916
+ }
1917
+ function logSynchroniserDiagnostics(result, options) {
1918
+ for (const line of splitDiagnostics(result.stderr)) options.log(line, "warn");
1919
+ }
1920
+ function parseSingleQuotedAssignment(line) {
1921
+ const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(line);
1922
+ if (!match || !match[2].startsWith("'")) return null;
1923
+ const valueSource = match[2];
1924
+ let value = "";
1925
+ for (let index = 1; index < valueSource.length; index++) {
1926
+ const character = valueSource[index];
1927
+ if (character !== "'") {
1928
+ value += character;
1929
+ continue;
1930
+ }
1931
+ if (index === valueSource.length - 1) return [match[1], value];
1932
+ if (valueSource.slice(index + 1, index + 4) !== "\\''") return null;
1933
+ value += "'";
1934
+ index += 3;
1935
+ }
1936
+ return null;
1937
+ }
1938
+ function parseSynchroniserEnv(stdout) {
1939
+ const values = {};
1940
+ for (const line of stdout.split("\n")) {
1941
+ if (line.trim() === "") continue;
1942
+ const assignment = parseSingleQuotedAssignment(line);
1943
+ if (!assignment) return null;
1944
+ values[assignment[0]] = assignment[1];
1945
+ }
1946
+ return values;
1947
+ }
1948
+ function runCommand(command, args, options) {
1949
+ return new Promise((resolve4) => {
1950
+ let child;
1951
+ let stdout = "";
1952
+ let stderr = "";
1953
+ let settled = false;
1954
+ const finish = (result) => {
1955
+ if (settled) return;
1956
+ settled = true;
1957
+ if (timer) clearTimeout(timer);
1958
+ resolve4(result);
1959
+ };
1960
+ try {
1961
+ child = spawn2(command, args, {
1962
+ env: options.env,
1963
+ stdio: ["ignore", "pipe", "pipe"]
1964
+ });
1965
+ } catch (error2) {
1966
+ resolve4({
1967
+ code: null,
1968
+ stdout,
1969
+ stderr: error2 instanceof Error ? error2.message : String(error2),
1970
+ timedOut: false
1971
+ });
1972
+ return;
1973
+ }
1974
+ child.stdout?.setEncoding("utf8");
1975
+ child.stdout?.on("data", (chunk) => {
1976
+ stdout += chunk;
1977
+ });
1978
+ child.stderr?.setEncoding("utf8");
1979
+ child.stderr?.on("data", (chunk) => {
1980
+ stderr += chunk;
1981
+ });
1982
+ child.once("error", (error2) => {
1983
+ finish({
1984
+ code: null,
1985
+ stdout,
1986
+ stderr: stderr === "" ? error2.message : `${stderr}
1987
+ ${error2.message}`,
1988
+ timedOut: false
1989
+ });
1990
+ });
1991
+ child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
1992
+ const timer = setTimeout(
1993
+ () => {
1994
+ child.kill("SIGKILL");
1995
+ finish({ code: null, stdout, stderr, timedOut: true });
1996
+ },
1997
+ Math.max(0, options.timeoutMs)
1998
+ );
1999
+ });
2000
+ }
2001
+ async function ensureLitestreamConfig(options, env) {
2002
+ const configPath = options.litestreamConfig;
2003
+ if (!configPath) {
2004
+ markNoReplicate(options, "no Litestream configuration path was provided");
2005
+ reportRecord(
2006
+ "restore",
2007
+ "restore_misconfigured",
2008
+ "litestream_config_unavailable",
2009
+ null,
2010
+ options
2011
+ );
2012
+ return null;
2013
+ }
2014
+ try {
2015
+ if (statSync2(configPath).size > 0) return configPath;
2016
+ } catch (error2) {
2017
+ if (!(error2 instanceof Error && "code" in error2 && error2.code === "ENOENT")) {
2018
+ options.log(
2019
+ `Could not inspect ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
2020
+ "warn"
2021
+ );
2022
+ }
2023
+ }
2024
+ const rendered = await runSynchroniser(["litestream-config"], {
2025
+ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
2026
+ env
2027
+ });
2028
+ logSynchroniserDiagnostics(rendered, options);
2029
+ if (rendered.timedOut || rendered.code !== 0) {
2030
+ options.log(
2031
+ `Could not generate ${configPath}: runner-synchroniser litestream-config failed (${commandError(rendered)})`,
2032
+ "error"
2033
+ );
2034
+ markNoReplicate(options, `could not generate ${configPath}`);
2035
+ reportRecord(
2036
+ "restore",
2037
+ "restore_misconfigured",
2038
+ "litestream_config_unavailable",
2039
+ null,
2040
+ options
2041
+ );
2042
+ return null;
2043
+ }
2044
+ try {
2045
+ mkdirSync(dirname2(configPath), { recursive: true });
2046
+ writeFileSync(configPath, rendered.stdout);
2047
+ } catch (error2) {
2048
+ options.log(
2049
+ `Could not write ${configPath}: ${error2 instanceof Error ? error2.message : String(error2)}`,
2050
+ "error"
2051
+ );
2052
+ markNoReplicate(options, `could not generate ${configPath}`);
2053
+ reportRecord(
2054
+ "restore",
2055
+ "restore_misconfigured",
2056
+ "litestream_config_unavailable",
2057
+ null,
2058
+ options
2059
+ );
2060
+ return null;
2061
+ }
2062
+ const version2 = await runCommand("litestream", ["version"], {
2063
+ env,
2064
+ timeoutMs: 1e4
2065
+ });
2066
+ const litestreamVersion = version2.code === 0 ? version2.stdout.trim() || "unknown" : "unknown";
2067
+ const regionEmpty = !/^\s*region:\s*\S+/m.test(rendered.stdout);
2068
+ options.log(
2069
+ `litestream ${litestreamVersion}; AWS_REGION=${env.AWS_REGION ?? "<unset>"} AWS_DEFAULT_REGION=${env.AWS_DEFAULT_REGION ?? "<unset>"}; rendered litestream.yml region empty: ${regionEmpty ? "yes" : "no"}`
2070
+ );
2071
+ return configPath;
2072
+ }
2073
+ function restoreGiveUp(options, reason, message, litestreamExitCode, outcome = "fresh_session_db") {
2074
+ discardSessionDbDebris(options);
2075
+ markNoReplicate(options, message);
2076
+ reportRecord("restore", outcome, reason, litestreamExitCode, options);
2077
+ }
2078
+ async function restoreSessionDb(options, configPath, env) {
2079
+ const restored = await runCommand(
2080
+ "litestream",
2081
+ ["restore", "-config", configPath, "-if-db-not-exists", "-if-replica-exists", options.dbPath],
2082
+ { env, timeoutMs: SESSION_DB_RESTORE_TIMEOUT_MS }
2083
+ );
2084
+ for (const line of splitDiagnostics(restored.stderr)) options.log(line, "warn");
2085
+ if (restored.timedOut || restored.code === 124 || restored.code === 137) {
2086
+ restoreGiveUp(
2087
+ options,
2088
+ "restore_deadline_exceeded",
2089
+ `SESSION-DB-RESTORE-TRUNCATED: litestream restore did not finish within ${SESSION_DB_RESTORE_TIMEOUT_MS}ms; opencode starts with a fresh session DB and nothing is replicated this boot`,
2090
+ restored.code ?? 124
2091
+ );
2092
+ return;
2093
+ }
2094
+ if (restored.code === null || restored.code === 125 || restored.code === 126 || restored.code === 127) {
2095
+ options.log(`litestream restore could not run (${commandError(restored)})`, "error");
2096
+ restoreGiveUp(
2097
+ options,
2098
+ "restore_tool_unusable",
2099
+ `restore tool is broken (${commandError(restored)}); opencode starts with a fresh session DB and nothing is replicated this boot`,
2100
+ restored.code
2101
+ );
2102
+ return;
2103
+ }
2104
+ const classified = await runSynchroniser(
2105
+ [
2106
+ "session-db-classify",
2107
+ String(restored.code ?? 1),
2108
+ "1",
2109
+ "--on-unusable-replica=leave",
2110
+ "--fresh-db-fallback"
2111
+ ],
2112
+ { timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS, env }
2113
+ );
2114
+ logSynchroniserDiagnostics(classified, options);
2115
+ const classifyCode = classified.code;
2116
+ switch (classifyCode) {
2117
+ case 0:
2118
+ return;
2119
+ case 31:
2120
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2121
+ options.log(
2122
+ "SESSION-DB-REPLICA-UNUSABLE: booting with a fresh opencode.db and replicating into the existing prefix this boot",
2123
+ "warn"
2124
+ );
2125
+ return;
2126
+ case 32:
2127
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2128
+ discardSessionDbDebris(options);
2129
+ markNoReplicate(
2130
+ options,
2131
+ "session-db-classify asked for another restore attempt (32), but this boot has budget for only one; treating it as a give-up rather than retrying"
2132
+ );
2133
+ return;
2134
+ case 30:
2135
+ restoreGiveUp(
2136
+ options,
2137
+ "classification_fatal",
2138
+ "session-db-classify returned fatal (30); see the FATAL message above",
2139
+ restored.code,
2140
+ "restore_misconfigured"
2141
+ );
2142
+ return;
2143
+ default:
2144
+ restoreGiveUp(
2145
+ options,
2146
+ "classification_unrecognised",
2147
+ `session-db-classify exited ${classifyCode ?? "null"}, which is none of its documented answers`,
2148
+ restored.code
2149
+ );
2150
+ }
2151
+ }
2152
+ async function verifySessionDb(options, configPath, env) {
2153
+ if (!options.noReplicateMarker || !fileExists(options.noReplicateMarker)) {
2154
+ const result = await runSynchroniser(["session-db-verify", configPath], {
2155
+ timeoutMs: SESSION_DB_VERIFY_TIMEOUT_MS,
2156
+ env: {
2157
+ ...env,
2158
+ // The synchroniser reads this value in SECONDS. Keep this at 120, not
2159
+ // 120_000, so the walkback gives up before the outer process bound.
2160
+ EVIDENT_SESSION_DB_WALKBACK_BUDGET_SECONDS: String(
2161
+ SESSION_DB_VERIFY_WALKBACK_BUDGET_SECONDS
2162
+ )
2163
+ }
2164
+ });
2165
+ logSynchroniserDiagnostics(result, options);
2166
+ if (result.timedOut || result.code === 124 || result.code === 137) {
2167
+ options.log(
2168
+ `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`,
2169
+ "warn"
2170
+ );
2171
+ return false;
2172
+ }
2173
+ if (result.code === 34) {
2174
+ reportRecord(
2175
+ "verify",
2176
+ "session_db_boot_refused",
2177
+ result.stderr.includes("SESSION-DB-LOCAL-DISCARD-FAILED") ? "local_discard_failed" : "replica_separation_unproven",
2178
+ null,
2179
+ options
2180
+ );
2181
+ return true;
2182
+ }
2183
+ if (result.code === 33) {
2184
+ options.log(
2185
+ "SESSION-DB-INTEGRITY-EXHAUSTED: booting continues and litestream still replicates, starting an empty backup chain after the corrupt replica was separated.",
2186
+ "warn"
2187
+ );
2188
+ return false;
2189
+ }
2190
+ if (result.code !== 0) {
2191
+ options.log(
2192
+ `SESSION-DB-VERIFY-UNKNOWN: session-db-verify exited ${result.code ?? "null"}, which is none of its documented answers; continuing with the restored opencode.db as-is`,
2193
+ "warn"
2194
+ );
2195
+ }
2196
+ return false;
2197
+ }
2198
+ options.log(
2199
+ "skipping session-DB verification: this boot's session DB was not proven safe to replicate",
2200
+ "debug"
2201
+ );
2202
+ return false;
2203
+ }
2204
+ function fileExists(path) {
2205
+ try {
2206
+ statSync2(path);
2207
+ return true;
2208
+ } catch (error2) {
2209
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return false;
2210
+ return true;
2211
+ }
2212
+ }
2213
+ async function restoreAndVerifySessionDb(options) {
2214
+ const env = options.env ?? process.env;
2215
+ clearMarker(options);
2216
+ acknowledgeSessionDbRecoveryReport(sessionDbRecoveryReportPath(homedir2(), env));
2217
+ const synchroniserEnv = await runSynchroniser(["env"], {
2218
+ timeoutMs: SESSION_DB_SYNCHRONISER_TIMEOUT_MS,
2219
+ env
2220
+ });
2221
+ logSynchroniserDiagnostics(synchroniserEnv, options);
2222
+ if (synchroniserEnv.timedOut || synchroniserEnv.code !== 0) {
2223
+ options.log(
2224
+ `runner-synchroniser env could not resolve the session-DB configuration (${commandError(synchroniserEnv)})`,
2225
+ "error"
2226
+ );
2227
+ markNoReplicate(
2228
+ options,
2229
+ "could not resolve the runner-synchroniser configuration (see the ERROR above)"
2230
+ );
2231
+ reportRecord(
2232
+ "restore",
2233
+ "restore_misconfigured",
2234
+ "synchroniser_config_unresolved",
2235
+ null,
2236
+ options
2237
+ );
2238
+ return { verifyFatal: false };
2239
+ }
2240
+ const values = parseSynchroniserEnv(synchroniserEnv.stdout);
2241
+ if (!values) {
2242
+ markNoReplicate(
2243
+ options,
2244
+ "the runner-synchroniser configuration could not be evaluated; the installed bundle likely does not match this hook"
2245
+ );
2246
+ reportRecord(
2247
+ "restore",
2248
+ "restore_misconfigured",
2249
+ "synchroniser_config_unevaluable",
2250
+ null,
2251
+ options
2252
+ );
2253
+ return { verifyFatal: false };
2254
+ }
2255
+ const synchroniserDbPath = values.OPENCODE_DB_PATH;
2256
+ if (!synchroniserDbPath) {
2257
+ markNoReplicate(
2258
+ options,
2259
+ "run_synchroniser env did not define OPENCODE_DB_PATH; the installed runner-synchroniser build likely does not match this hook"
2260
+ );
2261
+ reportRecord(
2262
+ "restore",
2263
+ "restore_misconfigured",
2264
+ "synchroniser_config_incomplete",
2265
+ null,
2266
+ options
2267
+ );
2268
+ return { verifyFatal: false };
2269
+ }
2270
+ if (resolvePath(synchroniserDbPath) !== resolvePath(options.dbPath)) {
2271
+ options.log(
2272
+ `runner-synchroniser reported OPENCODE_DB_PATH=${synchroniserDbPath}, but OpenCode uses ${options.dbPath}; continuing with OpenCode's session-DB path`,
2273
+ "warn"
2274
+ );
2275
+ }
2276
+ if (!values.PERSISTENCE_BUCKET) {
2277
+ options.log(
2278
+ "SESSION-DB-PERSISTENCE-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; opencode starts with a fresh session DB and nothing is replicated.",
2279
+ "warn"
2280
+ );
2281
+ return { verifyFatal: false };
2282
+ }
2283
+ const configPath = await ensureLitestreamConfig(options, env);
2284
+ if (!configPath) return { verifyFatal: false };
2285
+ await restoreSessionDb(options, configPath, env);
2286
+ if (options.noReplicateMarker && fileExists(options.noReplicateMarker)) {
2287
+ return { verifyFatal: false };
2288
+ }
2289
+ return { verifyFatal: await verifySessionDb(options, configPath, env) };
2290
+ }
2291
+
2292
+ // src/lib/opencode/session-db-provenance.ts
2293
+ import { createRequire } from "node:module";
2294
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "node:fs";
2295
+ import { dirname as dirname3, join as join3 } from "node:path";
2296
+ var require2 = createRequire(import.meta.url);
2297
+ function readSessionDbMigrationIds(dbPath) {
2298
+ let db;
2299
+ try {
2300
+ const { DatabaseSync } = require2("node:sqlite");
2301
+ db = new DatabaseSync(dbPath, { readOnly: true });
2302
+ const columns = db.prepare("PRAGMA table_info(migration)").all();
2303
+ const hasExpectedShape = columns.length === 2 && columns.some(
2304
+ (column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
2305
+ ) && columns.some(
2306
+ (column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
2307
+ );
2308
+ if (!hasExpectedShape) {
2309
+ console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
2310
+ return null;
2311
+ }
2312
+ const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
2313
+ if (rows.some((row) => typeof row.id !== "string")) return null;
2314
+ return rows.map((row) => row.id);
2315
+ } catch (error2) {
2316
+ console.warn(
2317
+ `[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2318
+ );
2319
+ return null;
2320
+ } finally {
2321
+ try {
2322
+ db?.close();
2323
+ } catch (error2) {
2324
+ console.warn(
2325
+ `[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2326
+ );
2327
+ }
2328
+ }
2329
+ }
2330
+ function sessionDbProvenanceStatePath(homeDir, env) {
2331
+ const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
2332
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
2333
+ }
2334
+ function loadSessionDbProvenanceState(path) {
2335
+ let value;
2336
+ try {
2337
+ value = JSON.parse(readFileSync3(path, "utf8"));
2338
+ } catch (error2) {
2339
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
2340
+ console.error(
2341
+ `[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2342
+ );
2343
+ return {};
2344
+ }
2345
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
2346
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2347
+ return {};
2348
+ }
2349
+ const state = {};
2350
+ for (const [dbPath, record] of Object.entries(value)) {
2351
+ if (!isSessionDbProvenanceRecord(record)) {
2352
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2353
+ return {};
2354
+ }
2355
+ state[dbPath] = record;
2356
+ }
2357
+ return state;
2358
+ }
2359
+ function saveSessionDbProvenanceState(path, state) {
2360
+ try {
2361
+ mkdirSync2(dirname3(path), { recursive: true });
2362
+ writeFileSync2(path, `${JSON.stringify(state, null, 2)}
2363
+ `, "utf8");
2364
+ } catch (error2) {
2365
+ console.error(
2366
+ `[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2367
+ );
2368
+ }
2369
+ }
2370
+ function evaluateSessionDbProvenance(input) {
2371
+ const { currentVersion, currentIds, previous } = input;
2372
+ if (!previous) return { anomaly: false, reason: null };
2373
+ const current = new Set(currentIds);
2374
+ const prior = new Set(previous.migrationIds);
2375
+ for (const id of prior) {
2376
+ if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
2377
+ }
2378
+ if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
2379
+ return { anomaly: true, reason: "foreign-version-migrations" };
2380
+ }
2381
+ return { anomaly: false, reason: null };
2382
+ }
2383
+ function checkSessionDbProvenance(input) {
2384
+ const { dbPath, currentVersion, homeDir, env } = input;
2385
+ const path = sessionDbProvenanceStatePath(homeDir, env);
2386
+ const state = loadSessionDbProvenanceState(path);
2387
+ const previous = state[dbPath];
2388
+ const currentIds = readSessionDbMigrationIds(dbPath);
2389
+ if (currentIds === null) {
2390
+ return {
2391
+ anomaly: false,
2392
+ reason: null,
2393
+ recordedVersion: previous?.opencodeVersion ?? null,
2394
+ migrationDelta: null
2395
+ };
2396
+ }
2397
+ const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
2398
+ const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
2399
+ state[dbPath] = {
2400
+ opencodeVersion: currentVersion,
2401
+ migrationIds: currentIds,
2402
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2403
+ };
2404
+ saveSessionDbProvenanceState(path, state);
2405
+ return {
2406
+ ...decision,
2407
+ recordedVersion: previous?.opencodeVersion ?? null,
2408
+ migrationDelta
2409
+ };
2410
+ }
2411
+ function isSessionDbProvenanceRecord(value) {
2412
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
2413
+ const record = value;
2414
+ return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
2415
+ }
2416
+
1669
2417
  // src/lib/opencode/opencode-version-gate.ts
1670
2418
  var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
1671
2419
  function isQueueValidatedVersion(version2) {
@@ -1680,7 +2428,7 @@ function buildOpenCodeVersionWarning(version2) {
1680
2428
  }
1681
2429
 
1682
2430
  // src/lib/opencode/process.ts
1683
- import { execSync, spawn } from "child_process";
2431
+ import { execSync, spawn as spawn3 } from "child_process";
1684
2432
 
1685
2433
  // src/lib/process-stop.ts
1686
2434
  async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
@@ -1690,7 +2438,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
1690
2438
  if (child.exitCode !== null || child.signalCode !== null) {
1691
2439
  return { outcome: "exited", code: child.exitCode, signal: child.signalCode };
1692
2440
  }
1693
- return new Promise((resolve3, reject) => {
2441
+ return new Promise((resolve4, reject) => {
1694
2442
  let forced = false;
1695
2443
  let settled = false;
1696
2444
  const timer = setTimeout(() => {
@@ -1710,7 +2458,7 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
1710
2458
  settled = true;
1711
2459
  clearTimeout(timer);
1712
2460
  child.removeListener("exit", onExit);
1713
- resolve3(result);
2461
+ resolve4(result);
1714
2462
  };
1715
2463
  const fail = (error2) => {
1716
2464
  if (settled) return;
@@ -1738,6 +2486,17 @@ async function stopProcessAndWait(child, timeoutMs, sendTerm, sendKill) {
1738
2486
 
1739
2487
  // src/lib/opencode/process.ts
1740
2488
  var OPENCODE_PORT_RANGE = [4096, 4097, 4098, 4099, 4100];
2489
+ var VALID_OPENCODE_LOG_LEVELS = /* @__PURE__ */ new Set(["DEBUG", "INFO", "WARN", "ERROR"]);
2490
+ function resolveOpenCodeLogLevel(env) {
2491
+ const raw = env.OPENCODE_LOG_LEVEL;
2492
+ if (!raw) return "INFO";
2493
+ const upper = raw.toUpperCase();
2494
+ if (VALID_OPENCODE_LOG_LEVELS.has(upper)) return upper;
2495
+ console.warn(
2496
+ `startOpenCode: ignoring invalid OPENCODE_LOG_LEVEL "${raw}" (expected DEBUG|INFO|WARN|ERROR) \u2014 using INFO`
2497
+ );
2498
+ return "INFO";
2499
+ }
1741
2500
  function getProcessCwd(pid) {
1742
2501
  const platform = process.platform;
1743
2502
  try {
@@ -1786,14 +2545,14 @@ function findAvailablePort(startPort, maxAttempts = 10) {
1786
2545
  }
1787
2546
  return null;
1788
2547
  }
1789
- function findOpenCodeProcesses() {
2548
+ function findProcessesByPattern(pgrepPattern, psPattern) {
1790
2549
  const instances = [];
1791
2550
  try {
1792
2551
  const platform = process.platform;
1793
2552
  if (platform === "darwin" || platform === "linux") {
1794
2553
  let pids = [];
1795
2554
  try {
1796
- const pgrepOutput = execSync('pgrep -f "opencode serve|opencode-serve"', {
2555
+ const pgrepOutput = execSync(`pgrep -f "${pgrepPattern}"`, {
1797
2556
  encoding: "utf-8",
1798
2557
  stdio: ["pipe", "pipe", "pipe"]
1799
2558
  }).trim();
@@ -1802,7 +2561,7 @@ function findOpenCodeProcesses() {
1802
2561
  }
1803
2562
  } catch {
1804
2563
  try {
1805
- const psOutput = execSync('ps aux | grep -E "opencode (serve|--port)" | grep -v grep', {
2564
+ const psOutput = execSync(`ps aux | grep -E "${psPattern}" | grep -v grep`, {
1806
2565
  encoding: "utf-8",
1807
2566
  stdio: ["pipe", "pipe", "pipe"]
1808
2567
  }).trim();
@@ -1848,6 +2607,9 @@ function findOpenCodeProcesses() {
1848
2607
  }
1849
2608
  return instances;
1850
2609
  }
2610
+ function findOpenCodeProcesses() {
2611
+ return findProcessesByPattern("opencode serve|opencode-serve", "opencode (serve|--port)");
2612
+ }
1851
2613
  async function scanPortsForOpenCode() {
1852
2614
  const instances = [];
1853
2615
  const checks = OPENCODE_PORT_RANGE.map(async (port) => {
@@ -1892,18 +2654,27 @@ async function findHealthyOpenCodeInstances() {
1892
2654
  }
1893
2655
  return healthy;
1894
2656
  }
1895
- async function startOpenCode(port) {
2657
+ async function startOpenCode(port, options = {}) {
1896
2658
  let command = "opencode";
1897
- let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1"];
2659
+ const printLogs = options.inheritStdio ? ["--print-logs", "--log-level", resolveOpenCodeLogLevel(process.env)] : [];
2660
+ let args = ["serve", "--port", port.toString(), "--hostname", "127.0.0.1", ...printLogs];
1898
2661
  try {
1899
2662
  execSync("which opencode", { stdio: "ignore" });
1900
2663
  } catch {
1901
2664
  command = "npx";
1902
- args = ["opencode", "serve", "--port", port.toString(), "--hostname", "127.0.0.1"];
1903
- }
1904
- const child = spawn(command, args, {
2665
+ args = [
2666
+ "opencode",
2667
+ "serve",
2668
+ "--port",
2669
+ port.toString(),
2670
+ "--hostname",
2671
+ "127.0.0.1",
2672
+ ...printLogs
2673
+ ];
2674
+ }
2675
+ const child = spawn3(command, args, {
1905
2676
  detached: true,
1906
- stdio: "ignore",
2677
+ stdio: options.inheritStdio ? "inherit" : "ignore",
1907
2678
  cwd: process.cwd()
1908
2679
  });
1909
2680
  return child;
@@ -1942,6 +2713,19 @@ function isOpenCodeInstalled() {
1942
2713
  return false;
1943
2714
  }
1944
2715
  }
2716
+ function isOpenCode2Installed() {
2717
+ try {
2718
+ const platform = process.platform;
2719
+ if (platform === "win32") {
2720
+ execSync2("where opencode2", { stdio: "ignore" });
2721
+ } else {
2722
+ execSync2("which opencode2", { stdio: "ignore" });
2723
+ }
2724
+ return true;
2725
+ } catch {
2726
+ return false;
2727
+ }
2728
+ }
1945
2729
  async function promptOpenCodeInstall(interactive) {
1946
2730
  if (!interactive) {
1947
2731
  console.log(
@@ -1951,7 +2735,11 @@ async function promptOpenCodeInstall(interactive) {
1951
2735
  install_url: OPENCODE_INSTALL_URL,
1952
2736
  install_commands: {
1953
2737
  npm: "npm install -g opencode-ai",
1954
- curl: "curl -fsSL https://opencode.ai/install.sh | sh"
2738
+ curl: "curl -fsSL https://opencode.ai/install.sh | sh",
2739
+ v2: {
2740
+ npm: "npm install -g @opencode-ai/cli@beta",
2741
+ curl: "curl -fsSL https://opencode.ai/v2/install | bash"
2742
+ }
1955
2743
  }
1956
2744
  })
1957
2745
  );
@@ -2208,6 +2996,7 @@ async function createOpenCodeSession(port, directory) {
2208
2996
  return data.id;
2209
2997
  }
2210
2998
  async function getModelAttachmentCapability(port, model) {
2999
+ const { model: baseModel } = splitModelVariant(model);
2211
3000
  try {
2212
3001
  const res = await timedFetch(`${opencodeBase(port)}/config/providers`);
2213
3002
  if (!res.ok) {
@@ -2224,9 +3013,9 @@ async function getModelAttachmentCapability(port, model) {
2224
3013
  );
2225
3014
  return null;
2226
3015
  }
2227
- const slash = model ? model.indexOf("/") : -1;
2228
- const providerId = slash > 0 ? model.slice(0, slash) : void 0;
2229
- let modelId = slash > 0 ? model.slice(slash + 1) : void 0;
3016
+ const slash = baseModel ? baseModel.indexOf("/") : -1;
3017
+ const providerId = slash > 0 ? baseModel.slice(0, slash) : void 0;
3018
+ let modelId = slash > 0 ? baseModel.slice(slash + 1) : void 0;
2230
3019
  const defaults2 = body?.default && typeof body.default === "object" ? body.default : void 0;
2231
3020
  let provider = providerId ? providers.find((p) => p?.id === providerId) : void 0;
2232
3021
  if (!provider && !providerId) {
@@ -2306,6 +3095,29 @@ async function buildFileParts(attachments, capable) {
2306
3095
  }
2307
3096
  return { parts, outcomes, capabilityUnknown };
2308
3097
  }
3098
+ function splitModelVariant(raw) {
3099
+ const value = raw?.trim();
3100
+ if (!value) return {};
3101
+ const hashIndex = value.indexOf("#");
3102
+ if (hashIndex === -1) return { model: value };
3103
+ const model = value.slice(0, hashIndex).trim() || void 0;
3104
+ const variant = value.slice(hashIndex + 1).trim() || void 0;
3105
+ return { model, variant };
3106
+ }
3107
+ function applyModelOptions(body, options) {
3108
+ if (options?.agent) body.agent = options.agent;
3109
+ const { model, variant } = splitModelVariant(options?.model);
3110
+ if (model) {
3111
+ const slashIndex = model.indexOf("/");
3112
+ if (slashIndex !== -1) {
3113
+ body.model = {
3114
+ providerID: model.substring(0, slashIndex),
3115
+ modelID: model.substring(slashIndex + 1)
3116
+ };
3117
+ }
3118
+ }
3119
+ if (variant) body.variant = variant;
3120
+ }
2309
3121
  function messageText(m) {
2310
3122
  if (!m || !Array.isArray(m.parts)) return "";
2311
3123
  return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
@@ -2330,18 +3142,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2330
3142
  const body = {
2331
3143
  parts
2332
3144
  };
2333
- if (options?.agent) {
2334
- body.agent = options.agent;
2335
- }
2336
- if (options?.model) {
2337
- const slashIndex = options.model.indexOf("/");
2338
- if (slashIndex !== -1) {
2339
- body.model = {
2340
- providerID: options.model.substring(0, slashIndex),
2341
- modelID: options.model.substring(slashIndex + 1)
2342
- };
2343
- }
2344
- }
3145
+ applyModelOptions(body, options);
2345
3146
  const res = await timedFetch(`${opencodeBase(port)}/session/${sessionId}/prompt_async`, {
2346
3147
  method: "POST",
2347
3148
  headers: { "Content-Type": "application/json" },
@@ -2349,7 +3150,10 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2349
3150
  });
2350
3151
  if (res.status < 200 || res.status >= 300) {
2351
3152
  const text = await res.text().catch(() => "");
2352
- throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
3153
+ const { variant } = splitModelVariant(options?.model);
3154
+ throw new Error(
3155
+ `OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}${variant ? ` (variant: ${variant})` : ""}`
3156
+ );
2353
3157
  }
2354
3158
  const READ_BACK_ATTEMPTS = 5;
2355
3159
  const READ_BACK_DELAY_MS = 150;
@@ -2373,7 +3177,7 @@ async function sendPromptAsync(port, sessionId, content, options, attachments) {
2373
3177
  }
2374
3178
  }
2375
3179
  if (attempt < READ_BACK_ATTEMPTS - 1) {
2376
- await new Promise((resolve3) => setTimeout(resolve3, READ_BACK_DELAY_MS));
3180
+ await new Promise((resolve4) => setTimeout(resolve4, READ_BACK_DELAY_MS));
2377
3181
  }
2378
3182
  }
2379
3183
  return null;
@@ -2419,6 +3223,44 @@ function findLastAssistantReplyFor(messages, userMessageId) {
2419
3223
  }
2420
3224
  return lastOk ?? last;
2421
3225
  }
3226
+ function collectSubagentSessions(messages, userMessageId) {
3227
+ if (!messages || messages.length === 0) return [];
3228
+ const byParent = messages.filter(
3229
+ (message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
3230
+ );
3231
+ const assistants = byParent.length > 0 ? byParent : [];
3232
+ if (assistants.length === 0) {
3233
+ const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3234
+ if (userIndex === -1) return [];
3235
+ for (let i = userIndex + 1; i < messages.length; i++) {
3236
+ const message = messages[i];
3237
+ if (roleOf(message) === "user") break;
3238
+ if (roleOf(message) === "assistant") assistants.push(message);
3239
+ }
3240
+ }
3241
+ const refs = [];
3242
+ const seen = /* @__PURE__ */ new Set();
3243
+ for (const message of assistants) {
3244
+ const parts = Array.isArray(message.parts) ? message.parts : [];
3245
+ for (const part of parts) {
3246
+ if (!part || typeof part !== "object" || part.type !== "tool" || part.tool !== "task")
3247
+ continue;
3248
+ const state = part.state;
3249
+ if (!state || typeof state !== "object") continue;
3250
+ const metadata = state.metadata;
3251
+ if (!metadata || typeof metadata !== "object") continue;
3252
+ const sessionId = metadata.sessionId;
3253
+ if (typeof sessionId !== "string" || sessionId.length === 0 || seen.has(sessionId)) continue;
3254
+ seen.add(sessionId);
3255
+ const start = state.time?.start;
3256
+ refs.push({
3257
+ sessionId,
3258
+ startedAtMs: typeof start === "number" && Number.isFinite(start) ? start : null
3259
+ });
3260
+ }
3261
+ }
3262
+ return refs;
3263
+ }
2422
3264
  function messageUsage(messages, userMessageId) {
2423
3265
  if (!messages || messages.length === 0) return null;
2424
3266
  const byParentAll = messages.filter(
@@ -2547,8 +3389,7 @@ function isAbortedTerminalReply(messages, userMessageId) {
2547
3389
  }
2548
3390
  return false;
2549
3391
  }
2550
- function messageFailure(messages, userMessageId) {
2551
- const reply = findLastAssistantReplyFor(messages, userMessageId);
3392
+ function classifyReplyAuthError(reply) {
2552
3393
  const error2 = errorOf(reply);
2553
3394
  if (error2 == null || typeof error2 !== "object") return null;
2554
3395
  const e = error2;
@@ -2573,6 +3414,32 @@ function messageFailure(messages, userMessageId) {
2573
3414
  }
2574
3415
  return null;
2575
3416
  }
3417
+ function messageFailure(messages, userMessageId) {
3418
+ return classifyReplyAuthError(findLastAssistantReplyFor(messages, userMessageId));
3419
+ }
3420
+ function findLatestSubagentAuthOutcome(messages, sinceMs) {
3421
+ if (!messages || messages.length === 0) return null;
3422
+ for (let i = messages.length - 1; i >= 0; i--) {
3423
+ const message = messages[i];
3424
+ if (roleOf(message) !== "assistant") continue;
3425
+ const created = createdOf(message);
3426
+ if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
3427
+ const failure = classifyReplyAuthError(message);
3428
+ if (failure) {
3429
+ if (!failure.providerId) return null;
3430
+ return { providerId: failure.providerId, outcome: "failed", failure };
3431
+ }
3432
+ const providerId = message.info?.providerID;
3433
+ if (errorOf(message) == null && typeof providerId === "string" && providerId.length > 0) {
3434
+ return { providerId, outcome: "succeeded" };
3435
+ }
3436
+ return null;
3437
+ }
3438
+ return null;
3439
+ }
3440
+ function findSubagentAuthOutcome(messages, sinceMs) {
3441
+ return findLatestSubagentAuthOutcome(messages, sinceMs);
3442
+ }
2576
3443
  function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProviderId, replyModelId = null) {
2577
3444
  if (classified != null) return classified;
2578
3445
  if (hasConfiguredProvider !== false) return null;
@@ -2620,6 +3487,94 @@ async function hasAnyConfiguredProvider(port) {
2620
3487
  return null;
2621
3488
  }
2622
3489
  }
3490
+ function sessionErrorReason(error2) {
3491
+ const record = typeof error2 === "object" && error2 !== null ? error2 : null;
3492
+ const data = record?.data;
3493
+ const dataRecord = typeof data === "object" && data !== null ? data : null;
3494
+ const rawReason = typeof dataRecord?.message === "string" && dataRecord.message || typeof record?.message === "string" && record.message || typeof error2 === "string" && error2 || typeof record?.name === "string" && record.name || "OpenCode reported a session error with no details";
3495
+ const reason = rawReason.replace(/\s+/g, " ").trim().slice(0, 500);
3496
+ return reason || "OpenCode reported a session error with no details";
3497
+ }
3498
+ function parseSessionErrorFrame(data) {
3499
+ let parsed;
3500
+ try {
3501
+ parsed = JSON.parse(data);
3502
+ } catch (error2) {
3503
+ void error2;
3504
+ return null;
3505
+ }
3506
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
3507
+ const parsedRecord = parsed;
3508
+ const payload = parsedRecord.payload;
3509
+ const event = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : parsedRecord;
3510
+ if (event.type !== "session.error") return null;
3511
+ const properties = event.properties;
3512
+ if (properties === null || typeof properties !== "object" || Array.isArray(properties)) {
3513
+ return null;
3514
+ }
3515
+ const propertiesRecord = properties;
3516
+ const sessionId = propertiesRecord.sessionID;
3517
+ if (typeof sessionId !== "string" || sessionId.length === 0) return null;
3518
+ return {
3519
+ sessionId,
3520
+ reason: sessionErrorReason(propertiesRecord.error)
3521
+ };
3522
+ }
3523
+ async function readSessionErrorStream(port, options) {
3524
+ let reader = null;
3525
+ try {
3526
+ const response = await fetch(`${opencodeBase(port)}/event`, {
3527
+ headers: { accept: "text/event-stream" },
3528
+ signal: options.signal
3529
+ });
3530
+ if (!response.ok || !response.body) {
3531
+ return { reason: "unavailable", detail: `HTTP ${response.status}` };
3532
+ }
3533
+ reader = response.body.getReader();
3534
+ const decoder = new TextDecoder();
3535
+ let buffer = "";
3536
+ const processLine = (line) => {
3537
+ const trimmed = line.trimEnd();
3538
+ if (!trimmed.startsWith("data:")) return;
3539
+ const event = parseSessionErrorFrame(trimmed.slice("data:".length).replace(/^ /, ""));
3540
+ if (event) options.onSessionError(event);
3541
+ };
3542
+ while (true) {
3543
+ const { done, value } = await reader.read();
3544
+ if (done) return { reason: "ended" };
3545
+ buffer += decoder.decode(value, { stream: true });
3546
+ const lines = buffer.split("\n");
3547
+ buffer = lines.pop() ?? "";
3548
+ for (const line of lines) processLine(line);
3549
+ }
3550
+ } catch (err) {
3551
+ if (options.signal.aborted) return { reason: "aborted" };
3552
+ return {
3553
+ reason: "unavailable",
3554
+ detail: err instanceof Error ? err.message : String(err)
3555
+ };
3556
+ } finally {
3557
+ if (reader) void reader.cancel().catch(() => void 0);
3558
+ }
3559
+ }
3560
+ async function reloadProviderCache(port) {
3561
+ try {
3562
+ const res = await timedFetch(`${opencodeBase(port)}/config`, {
3563
+ method: "PATCH",
3564
+ headers: { "Content-Type": "application/json" },
3565
+ body: JSON.stringify({})
3566
+ });
3567
+ if (!res.ok) {
3568
+ console.error(
3569
+ `[reloadProviderCache] PATCH /config returned HTTP ${res.status} (port ${port})`
3570
+ );
3571
+ }
3572
+ } catch (err) {
3573
+ console.error(
3574
+ `[reloadProviderCache] PATCH /config failed (port ${port}): ${err instanceof Error ? err.message : String(err)}`
3575
+ );
3576
+ }
3577
+ }
2623
3578
 
2624
3579
  // src/lib/opencode/session-cleanup.ts
2625
3580
  var DURATION_UNIT_MS = {
@@ -2726,13 +3681,13 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
2726
3681
  }
2727
3682
 
2728
3683
  // src/lib/opencode/session-db-size.ts
2729
- import { statSync as statSync2 } from "fs";
2730
- import { join as join3 } from "path";
3684
+ import { statSync as statSync3 } from "node:fs";
3685
+ import { join as join4 } from "node:path";
2731
3686
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
2732
3687
  function statSessionDbBytes(homeDir) {
2733
- const dbPath = join3(homeDir, ".local", "share", "opencode", "opencode.db");
3688
+ const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
2734
3689
  try {
2735
- return statSync2(dbPath).size;
3690
+ return statSync3(dbPath).size;
2736
3691
  } catch (err) {
2737
3692
  const isMissingFile = err instanceof Error && "code" in err && err.code === "ENOENT";
2738
3693
  if (!isMissingFile) {
@@ -2757,12 +3712,99 @@ function buildSessionStoreSizeWarning(input) {
2757
3712
  return null;
2758
3713
  }
2759
3714
 
3715
+ // src/lib/opencode/log-tail.ts
3716
+ import { statSync as statSync4 } from "node:fs";
3717
+ import { homedir as homedir3 } from "node:os";
3718
+ import { join as join5 } from "node:path";
3719
+ import { open as open2, stat } from "node:fs/promises";
3720
+ var DEFAULT_POLL_INTERVAL_MS = 1e3;
3721
+ function resolveOpenCodeLogPath(homeDir = homedir3(), env = process.env) {
3722
+ const dataDir = env.XDG_DATA_HOME || join5(homeDir, ".local", "share");
3723
+ return join5(dataDir, "opencode", "log", "opencode.log");
3724
+ }
3725
+ function isEnoent(error2) {
3726
+ return error2?.code === "ENOENT";
3727
+ }
3728
+ function reportFailure(operation, logPath, error2) {
3729
+ console.error(
3730
+ `[opencode-log-tail] ${operation} failed for ${logPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
3731
+ );
3732
+ }
3733
+ function tailOpenCodeLogFile(logPath, onChunk, opts = {}) {
3734
+ const pollIntervalMs = opts.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
3735
+ let offset = 0;
3736
+ let inode = null;
3737
+ let baselineReady = true;
3738
+ try {
3739
+ const initial = statSync4(logPath);
3740
+ offset = initial.size;
3741
+ inode = initial.ino;
3742
+ } catch (error2) {
3743
+ if (!isEnoent(error2)) {
3744
+ reportFailure("initial stat", logPath, error2);
3745
+ baselineReady = false;
3746
+ }
3747
+ }
3748
+ let polling = false;
3749
+ let stopped = false;
3750
+ const poll = async () => {
3751
+ if (polling || stopped) return;
3752
+ polling = true;
3753
+ try {
3754
+ let current;
3755
+ try {
3756
+ current = await stat(logPath);
3757
+ } catch (error2) {
3758
+ if (!isEnoent(error2)) reportFailure("stat", logPath, error2);
3759
+ return;
3760
+ }
3761
+ if (!baselineReady) {
3762
+ offset = current.size;
3763
+ inode = current.ino;
3764
+ baselineReady = true;
3765
+ return;
3766
+ }
3767
+ if (inode !== null && current.ino !== inode || current.size < offset) {
3768
+ offset = 0;
3769
+ }
3770
+ inode = current.ino;
3771
+ if (current.size === offset) return;
3772
+ const length = current.size - offset;
3773
+ const fh = await open2(logPath, "r");
3774
+ try {
3775
+ const buf = Buffer.alloc(length);
3776
+ const { bytesRead } = await fh.read(buf, 0, length, offset);
3777
+ offset += bytesRead;
3778
+ if (bytesRead > 0) onChunk(buf.subarray(0, bytesRead));
3779
+ } finally {
3780
+ await fh.close();
3781
+ }
3782
+ } catch (error2) {
3783
+ if (!isEnoent(error2)) reportFailure("poll", logPath, error2);
3784
+ } finally {
3785
+ polling = false;
3786
+ }
3787
+ };
3788
+ const interval = setInterval(() => void poll(), pollIntervalMs);
3789
+ void poll();
3790
+ return {
3791
+ stop: () => {
3792
+ stopped = true;
3793
+ clearInterval(interval);
3794
+ }
3795
+ };
3796
+ }
3797
+
2760
3798
  // src/lib/opencode/session-db-reclaim.ts
2761
- import { statSync as statSync3, statfsSync } from "fs";
2762
- import { dirname as dirname2 } from "path";
3799
+ import { statSync as statSync5, statfsSync } from "node:fs";
3800
+ import { dirname as dirname4 } from "node:path";
3801
+ function errorMessage(error2) {
3802
+ if (!(error2 instanceof Error)) return String(error2);
3803
+ return error2.cause instanceof Error ? error2.cause.message : error2.message;
3804
+ }
2763
3805
  function insufficientSpaceReason(dbPath, requiredBytes) {
2764
3806
  try {
2765
- const fsStats = statfsSync(dirname2(dbPath));
3807
+ const fsStats = statfsSync(dirname4(dbPath));
2766
3808
  const availableBytes = fsStats.bavail * fsStats.bsize;
2767
3809
  if (availableBytes < requiredBytes) {
2768
3810
  return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
@@ -2785,17 +3827,17 @@ async function probeReclaimAvailability(input) {
2785
3827
  const { dbPath, requiredBytes } = input;
2786
3828
  let sqlite;
2787
3829
  try {
2788
- sqlite = await import("sqlite");
3830
+ sqlite = await import("node:sqlite");
2789
3831
  } catch (err) {
2790
- console.warn(
2791
- `[probeReclaimAvailability] node:sqlite unavailable for ${dbPath}: ${err instanceof Error ? err.message : String(err)}`
2792
- );
2793
- return "sqlite-unavailable";
3832
+ const detail = `Node ${process.version}: ${errorMessage(err)}`;
3833
+ console.warn(`[probeReclaimAvailability] node:sqlite unavailable for ${dbPath}: ` + detail);
3834
+ return { reason: "sqlite-unavailable", detail };
2794
3835
  }
2795
3836
  let autoVacuum = null;
2796
3837
  try {
2797
3838
  const db = new sqlite.DatabaseSync(dbPath, { readOnly: true });
2798
3839
  try {
3840
+ db.exec("PRAGMA busy_timeout=5000");
2799
3841
  autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
2800
3842
  } finally {
2801
3843
  db.close();
@@ -2806,23 +3848,25 @@ async function probeReclaimAvailability(input) {
2806
3848
  );
2807
3849
  }
2808
3850
  if (autoVacuum !== 0) return null;
2809
- return insufficientSpaceReason(dbPath, requiredBytes) !== null ? "insufficient-disk-space" : null;
3851
+ return insufficientSpaceReason(dbPath, requiredBytes) !== null ? { reason: "insufficient-disk-space" } : null;
2810
3852
  }
2811
3853
  async function reclaimSessionDbSpace(input) {
2812
3854
  const { dbPath, maxPages, allowFullVacuum = true } = input;
2813
3855
  let sqlite;
2814
3856
  try {
2815
- sqlite = await import("sqlite");
3857
+ sqlite = await import("node:sqlite");
2816
3858
  } catch (err) {
3859
+ const detail = `Node ${process.version}: ${errorMessage(err)}`;
2817
3860
  console.warn(
2818
- `[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${err instanceof Error ? err.message : String(err)}`
3861
+ `[reclaimSessionDbSpace] node:sqlite unavailable (need Node >=22.5, >=22.13 unflagged) for ${dbPath}: ${detail}`
2819
3862
  );
2820
- return { ok: false, skipped: "sqlite-unavailable" };
3863
+ return { ok: false, skipped: "sqlite-unavailable", detail };
2821
3864
  }
2822
3865
  const { DatabaseSync } = sqlite;
2823
3866
  let db;
2824
3867
  try {
2825
3868
  db = new DatabaseSync(dbPath);
3869
+ db.exec("PRAGMA busy_timeout=5000");
2826
3870
  const autoVacuum = db.prepare("PRAGMA auto_vacuum").get().auto_vacuum;
2827
3871
  if (autoVacuum === 0) {
2828
3872
  if (!allowFullVacuum) {
@@ -2831,7 +3875,7 @@ async function reclaimSessionDbSpace(input) {
2831
3875
  );
2832
3876
  return { ok: false, skipped: "full-vacuum-blocked" };
2833
3877
  }
2834
- const fileBytesForGuard = statSync3(dbPath).size;
3878
+ const fileBytesForGuard = statSync5(dbPath).size;
2835
3879
  const skipReason = insufficientSpaceReason(dbPath, fileBytesForGuard);
2836
3880
  if (skipReason !== null) {
2837
3881
  console.warn(
@@ -2859,10 +3903,12 @@ async function reclaimSessionDbSpace(input) {
2859
3903
  );
2860
3904
  return { ok: false, skipped: "auto-vacuum-not-applicable" };
2861
3905
  } catch (err) {
2862
- console.error(
2863
- `[reclaimSessionDbSpace] reclaim failed for ${dbPath}: ${err instanceof Error ? err.message : String(err)}`
2864
- );
2865
- return { ok: false, skipped: "reclaim-error" };
3906
+ console.error(`[reclaimSessionDbSpace] reclaim failed for ${dbPath}: ` + errorMessage(err));
3907
+ return {
3908
+ ok: false,
3909
+ skipped: "reclaim-error",
3910
+ detail: errorMessage(err)
3911
+ };
2866
3912
  } finally {
2867
3913
  db?.close();
2868
3914
  }
@@ -2903,7 +3949,6 @@ var StreamForwarder = class {
2903
3949
  handleFrame(frame) {
2904
3950
  switch (frame.type) {
2905
3951
  case "open":
2906
- this.callbacks.onOpen?.(frame.sid, frame.method, frame.path);
2907
3952
  void this.handleOpen(frame);
2908
3953
  break;
2909
3954
  case "req_data":
@@ -2939,12 +3984,21 @@ var StreamForwarder = class {
2939
3984
  const { sid, method, path, headers, has_body } = frame;
2940
3985
  const correlationId = headers?.[CORRELATION_ID_HEADER];
2941
3986
  const startedAt = Date.now();
3987
+ if (path !== TUNNEL_DRAIN_PING_PATH && path !== TUNNEL_USAGE_REARM_PING_PATH) {
3988
+ this.callbacks.onOpen?.(sid, method, path);
3989
+ }
2942
3990
  if (path === TUNNEL_DRAIN_PING_PATH) {
2943
3991
  this.callbacks.onDrainPing?.();
2944
3992
  this.send({ type: "head", sid, status: 204, headers: {} });
2945
3993
  this.send({ type: "res_end", sid });
2946
3994
  return;
2947
3995
  }
3996
+ if (path === TUNNEL_USAGE_REARM_PING_PATH) {
3997
+ this.callbacks.onUsageRearmPing?.();
3998
+ this.send({ type: "head", sid, status: 204, headers: {} });
3999
+ this.send({ type: "res_end", sid });
4000
+ return;
4001
+ }
2948
4002
  if (process.env.DEBUG) {
2949
4003
  log("debug", "agent_request", {
2950
4004
  correlation_id: correlationId,
@@ -2959,12 +4013,12 @@ var StreamForwarder = class {
2959
4013
  let endBody;
2960
4014
  if (has_body) {
2961
4015
  const chunks = [];
2962
- bodyPromise = new Promise((resolve3) => {
4016
+ bodyPromise = new Promise((resolve4) => {
2963
4017
  pushBody = (buf) => {
2964
4018
  chunks.push(buf);
2965
4019
  };
2966
4020
  endBody = () => {
2967
- resolve3(Buffer.concat(chunks));
4021
+ resolve4(Buffer.concat(chunks));
2968
4022
  };
2969
4023
  });
2970
4024
  }
@@ -3089,11 +4143,12 @@ function connectTunnel(options) {
3089
4143
  onResponse,
3090
4144
  onInfo,
3091
4145
  onWarning,
3092
- onDrainPing
4146
+ onDrainPing,
4147
+ onUsageRearmPing
3093
4148
  } = options;
3094
4149
  const tunnelUrl = getTunnelUrlConfig();
3095
4150
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
3096
- return new Promise((resolve3, reject) => {
4151
+ return new Promise((resolve4, reject) => {
3097
4152
  const ws = new WebSocket2(url, {
3098
4153
  headers: {
3099
4154
  Authorization: authHeader
@@ -3101,7 +4156,8 @@ function connectTunnel(options) {
3101
4156
  });
3102
4157
  const forwarder = new StreamForwarder(ws, port, {
3103
4158
  onHead: () => onResponse?.(),
3104
- onDrainPing: () => onDrainPing?.()
4159
+ onDrainPing: () => onDrainPing?.(),
4160
+ onUsageRearmPing: () => onUsageRearmPing?.()
3105
4161
  });
3106
4162
  const connectionTimeout = setTimeout(() => {
3107
4163
  ws.close();
@@ -3144,8 +4200,8 @@ function connectTunnel(options) {
3144
4200
  try {
3145
4201
  message = JSON.parse(data.toString());
3146
4202
  } catch (error2) {
3147
- const errorMessage = error2 instanceof Error ? error2.message : "Unknown error";
3148
- onError?.(`Failed to handle message: ${errorMessage}`);
4203
+ const errorMessage3 = error2 instanceof Error ? error2.message : "Unknown error";
4204
+ onError?.(`Failed to handle message: ${errorMessage3}`);
3149
4205
  return;
3150
4206
  }
3151
4207
  if (isStreamFrame(message)) {
@@ -3157,7 +4213,7 @@ function connectTunnel(options) {
3157
4213
  clearTimeout(connectionTimeout);
3158
4214
  const connectedAgentId = message.agent_id ?? agentId;
3159
4215
  onConnected?.(connectedAgentId);
3160
- resolve3({
4216
+ resolve4({
3161
4217
  ws,
3162
4218
  close: () => ws.close(1e3, "CLI shutdown")
3163
4219
  });
@@ -3262,6 +4318,7 @@ var RunnerConnection = class {
3262
4318
  onError: (error2) => events.onError?.(error2),
3263
4319
  onResponse: () => events.onResponse?.(),
3264
4320
  onDrainPing: () => events.onDrainPing?.(),
4321
+ onUsageRearmPing: () => events.onUsageRearmPing?.(),
3265
4322
  onInfo: (message) => events.onInfo?.(message),
3266
4323
  onWarning: (message) => events.onWarning?.(message)
3267
4324
  });
@@ -3288,10 +4345,10 @@ var RunnerConnection = class {
3288
4345
  };
3289
4346
 
3290
4347
  // src/lib/tunnel/ready-marker.ts
3291
- import { writeFileSync } from "fs";
4348
+ import { writeFileSync as writeFileSync3 } from "node:fs";
3292
4349
  function writeTunnelReadyMarker(path, agentId) {
3293
4350
  try {
3294
- writeFileSync(path, `${agentId}
4351
+ writeFileSync3(path, `${agentId}
3295
4352
  `);
3296
4353
  return { ok: true };
3297
4354
  } catch (error2) {
@@ -3300,9 +4357,9 @@ function writeTunnelReadyMarker(path, agentId) {
3300
4357
  }
3301
4358
 
3302
4359
  // src/lib/replication.ts
3303
- import { spawn as spawn2 } from "child_process";
4360
+ import { spawn as spawn4 } from "node:child_process";
3304
4361
  function startSessionDbReplication(configPath) {
3305
- return spawn2("litestream", ["replicate", "-config", configPath], {
4362
+ return spawn4("litestream", ["replicate", "-config", configPath], {
3306
4363
  stdio: "inherit"
3307
4364
  });
3308
4365
  }
@@ -3315,10 +4372,36 @@ async function stopSessionDbReplication(child, timeoutMs) {
3315
4372
  );
3316
4373
  }
3317
4374
 
4375
+ // src/lib/process-liveness.ts
4376
+ import { readFileSync as readFileSync4 } from "node:fs";
4377
+ function isProcessAlive(pid) {
4378
+ try {
4379
+ process.kill(pid, 0);
4380
+ } catch (error2) {
4381
+ const code = error2.code;
4382
+ if (code === "ESRCH") return false;
4383
+ if (code === "EPERM") return true;
4384
+ console.error(
4385
+ `[process-liveness] could not probe process ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`
4386
+ );
4387
+ return false;
4388
+ }
4389
+ if (process.platform !== "linux") return true;
4390
+ try {
4391
+ const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
4392
+ return !/^State:\s+Z(?:\s|$)/m.test(status2);
4393
+ } catch (error2) {
4394
+ console.error(
4395
+ `[process-liveness] could not inspect /proc/${pid}/status: ${error2 instanceof Error ? error2.message : String(error2)}`
4396
+ );
4397
+ return true;
4398
+ }
4399
+ }
4400
+
3318
4401
  // src/lib/openai-usage.ts
3319
- import { readFileSync as readFileSync3 } from "fs";
3320
- import { homedir as homedir2 } from "os";
3321
- import { join as join4 } from "path";
4402
+ import { readFileSync as readFileSync5 } from "node:fs";
4403
+ import { homedir as homedir4 } from "node:os";
4404
+ import { join as join6 } from "node:path";
3322
4405
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3323
4406
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3324
4407
  var OpenAiUsageError = class extends Error {
@@ -3332,7 +4415,7 @@ function isLocalCredentialProblem2(err) {
3332
4415
  }
3333
4416
  function readOpenCodeChatGptCredentials() {
3334
4417
  try {
3335
- const raw = readFileSync3(join4(homedir2(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4418
+ const raw = readFileSync5(join6(homedir4(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3336
4419
  let parsed;
3337
4420
  try {
3338
4421
  parsed = JSON.parse(raw);
@@ -3354,6 +4437,23 @@ function readOpenCodeChatGptCredentials() {
3354
4437
  return null;
3355
4438
  }
3356
4439
  }
4440
+ function parseChatGptIdentity(accessToken) {
4441
+ const segments = accessToken.split(".");
4442
+ if (segments.length !== 3) return null;
4443
+ let payload;
4444
+ try {
4445
+ const parsed = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
4446
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
4447
+ payload = parsed;
4448
+ } catch {
4449
+ return null;
4450
+ }
4451
+ const profile = payload["https://api.openai.com/profile"];
4452
+ const auth = payload["https://api.openai.com/auth"];
4453
+ const ownerEmail = typeof profile?.email === "string" ? profile.email.trim() || null : null;
4454
+ const planType = typeof auth?.chatgpt_plan_type === "string" ? auth.chatgpt_plan_type.trim() || null : null;
4455
+ return ownerEmail === null && planType === null ? null : { ownerEmail, planType, organizationName: null };
4456
+ }
3357
4457
  function toWindow2(headers, name) {
3358
4458
  const utilizationHeader = headers.get(`x-codex-${name}-used-percent`);
3359
4459
  const windowMinutesHeader = headers.get(`x-codex-${name}-window-minutes`);
@@ -3429,6 +4529,7 @@ async function getOpenAiUsage(port) {
3429
4529
  "credentials_expired"
3430
4530
  );
3431
4531
  }
4532
+ const subscription = parseChatGptIdentity(credentials2.accessToken);
3432
4533
  const models = await resolveProbeModels(port);
3433
4534
  if (models.length === 0) {
3434
4535
  throw new OpenAiUsageError("No supported OpenAI probe model is available.", "no_probe_model");
@@ -3461,7 +4562,7 @@ async function getOpenAiUsage(port) {
3461
4562
  "no_usable_window"
3462
4563
  );
3463
4564
  }
3464
- return usage;
4565
+ return { ...usage, subscription };
3465
4566
  }
3466
4567
  if (res.status === 401) {
3467
4568
  throw new OpenAiUsageError(
@@ -3576,8 +4677,8 @@ function resolveResourceUsageReportingEnabled(flagValue, env) {
3576
4677
  }
3577
4678
 
3578
4679
  // src/lib/resource-usage.ts
3579
- import { cpus, totalmem, freemem } from "os";
3580
- import { statfsSync as statfsSync2 } from "fs";
4680
+ import { cpus, totalmem, freemem } from "node:os";
4681
+ import { statfsSync as statfsSync2 } from "node:fs";
3581
4682
 
3582
4683
  // src/lib/ecs-task-metadata.ts
3583
4684
  var ECS_METADATA_TIMEOUT_MS = 2e3;
@@ -3662,58 +4763,97 @@ function readDisk(homeDir) {
3662
4763
  };
3663
4764
  }
3664
4765
  }
3665
- function createResourceUsageCollector(homeDir) {
3666
- let previous = readCpuSample();
3667
- return async () => {
4766
+ var CPU_PEAK_WINDOW_MS = 6e4;
4767
+ var CPU_PEAK_WINDOW_SAMPLE_COUNT = 4;
4768
+ var CPU_PEAK_SAMPLE_INTERVAL_MS = CPU_PEAK_WINDOW_MS / CPU_PEAK_WINDOW_SAMPLE_COUNT;
4769
+ function createCpuPeakSampler() {
4770
+ const sampleHistory = new Array(CPU_PEAK_WINDOW_SAMPLE_COUNT);
4771
+ sampleHistory[0] = readCpuSample();
4772
+ let nextSampleIndex = 1;
4773
+ let sampleCount = 1;
4774
+ let peak = null;
4775
+ const timer = setInterval(() => {
3668
4776
  const current = readCpuSample();
3669
- const hostCpuPercent = cpuPercentBetween(previous, current);
3670
- const hostCpuCount = cpus().length;
3671
- previous = current;
3672
- const disk = readDisk(homeDir);
3673
- const opencodeDbBytes = statSessionDbBytes(homeDir);
3674
- const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
3675
- const warnings = [];
3676
- if (disk.warning) warnings.push(disk.warning);
3677
- if (ecsWarning) warnings.push(ecsWarning);
3678
- let cpuPercent = hostCpuPercent;
3679
- let cpuCount = hostCpuCount;
3680
- let memoryTotalBytes = totalmem();
3681
- let memoryAvailableBytes = freemem();
3682
- if (limits !== null) {
3683
- cpuCount = limits.cpuCount;
3684
- memoryTotalBytes = limits.memoryTotalBytes;
3685
- memoryAvailableBytes = clamp(
3686
- limits.memoryTotalBytes - (totalmem() - freemem()),
3687
- 0,
3688
- limits.memoryTotalBytes
3689
- );
3690
- cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
4777
+ const sampleFromWindowAgo = sampleCount === CPU_PEAK_WINDOW_SAMPLE_COUNT ? sampleHistory[nextSampleIndex] : void 0;
4778
+ if (sampleFromWindowAgo !== void 0) {
4779
+ const percentage = cpuPercentBetween(sampleFromWindowAgo, current);
4780
+ if (percentage !== null) {
4781
+ peak = peak === null ? percentage : Math.max(peak, percentage);
4782
+ }
3691
4783
  }
3692
- return {
3693
- usage: {
3694
- cpuPercent,
3695
- cpuCount,
3696
- memoryTotalBytes,
3697
- memoryAvailableBytes,
3698
- diskTotalBytes: disk.totalBytes,
3699
- diskFreeBytes: disk.freeBytes,
3700
- opencodeDbBytes
3701
- },
3702
- warnings
3703
- };
4784
+ sampleHistory[nextSampleIndex] = current;
4785
+ nextSampleIndex = (nextSampleIndex + 1) % CPU_PEAK_WINDOW_SAMPLE_COUNT;
4786
+ sampleCount = Math.min(sampleCount + 1, CPU_PEAK_WINDOW_SAMPLE_COUNT);
4787
+ }, CPU_PEAK_SAMPLE_INTERVAL_MS);
4788
+ return {
4789
+ takeAndReset: () => {
4790
+ const currentPeak = peak;
4791
+ peak = null;
4792
+ return currentPeak;
4793
+ },
4794
+ stop: () => clearInterval(timer)
4795
+ };
4796
+ }
4797
+ function createResourceUsageCollector(homeDir) {
4798
+ let previous = readCpuSample();
4799
+ const cpuPeakSampler = createCpuPeakSampler();
4800
+ return {
4801
+ collect: async () => {
4802
+ const current = readCpuSample();
4803
+ const hostCpuPercent = cpuPercentBetween(previous, current);
4804
+ const hostCpuPeakPercent = cpuPeakSampler.takeAndReset();
4805
+ const hostCpuCount = cpus().length;
4806
+ previous = current;
4807
+ const disk = readDisk(homeDir);
4808
+ const opencodeDbBytes = statSessionDbBytes(homeDir);
4809
+ const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
4810
+ const warnings = [];
4811
+ if (disk.warning) warnings.push(disk.warning);
4812
+ if (ecsWarning) warnings.push(ecsWarning);
4813
+ let cpuPercent = hostCpuPercent;
4814
+ let cpuPeakPercent = hostCpuPeakPercent;
4815
+ let cpuCount = hostCpuCount;
4816
+ let memoryTotalBytes = totalmem();
4817
+ let memoryAvailableBytes = freemem();
4818
+ if (limits !== null) {
4819
+ cpuCount = limits.cpuCount;
4820
+ memoryTotalBytes = limits.memoryTotalBytes;
4821
+ memoryAvailableBytes = clamp(
4822
+ limits.memoryTotalBytes - (totalmem() - freemem()),
4823
+ 0,
4824
+ limits.memoryTotalBytes
4825
+ );
4826
+ cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
4827
+ cpuPeakPercent = hostCpuPeakPercent === null ? null : clamp(round2(hostCpuPeakPercent * hostCpuCount / limits.cpuCount), 0, 100);
4828
+ }
4829
+ return {
4830
+ usage: {
4831
+ cpuPercent,
4832
+ cpuPeakPercent,
4833
+ cpuCount,
4834
+ memoryTotalBytes,
4835
+ memoryAvailableBytes,
4836
+ diskTotalBytes: disk.totalBytes,
4837
+ diskFreeBytes: disk.freeBytes,
4838
+ opencodeDbBytes
4839
+ },
4840
+ warnings
4841
+ };
4842
+ },
4843
+ stop: cpuPeakSampler.stop
3704
4844
  };
3705
4845
  }
3706
4846
 
3707
4847
  // src/lib/channels/driver.ts
3708
- import { homedir as homedir3 } from "os";
4848
+ import { homedir as homedir5 } from "node:os";
3709
4849
 
3710
4850
  // src/lib/runner-file-sync.ts
3711
- import { join as join6 } from "path";
4851
+ import { join as join8 } from "node:path";
3712
4852
 
3713
4853
  // src/lib/file-push.ts
3714
- import { randomUUID } from "crypto";
3715
- import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
3716
- import { basename, dirname as dirname3, isAbsolute, join as join5, relative, resolve as resolve2, sep } from "path";
4854
+ import { randomUUID } from "node:crypto";
4855
+ import { chmod, mkdir, open as open3, realpath, rename, unlink } from "node:fs/promises";
4856
+ import { basename, dirname as dirname5, isAbsolute, join as join7, relative, resolve as resolve2, sep } from "node:path";
3717
4857
  var FILE_MODE = 384;
3718
4858
  var DIRECTORY_MODE = 448;
3719
4859
  async function writePushedFile(request) {
@@ -3744,9 +4884,9 @@ async function writePushedFile(request) {
3744
4884
  }
3745
4885
  try {
3746
4886
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
3747
- dirname3(candidate)
4887
+ dirname5(candidate)
3748
4888
  );
3749
- const realTarget = join5(existingAncestor, ...missingSegments, basename(candidate));
4889
+ const realTarget = join7(existingAncestor, ...missingSegments, basename(candidate));
3750
4890
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
3751
4891
  if (allowedDirectory === null) {
3752
4892
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -3756,8 +4896,8 @@ async function writePushedFile(request) {
3756
4896
  }
3757
4897
  if (missingSegments.length > 0) {
3758
4898
  await createMissingDirectories(existingAncestor, missingSegments);
3759
- const realParent = await realpath(dirname3(realTarget));
3760
- if (realParent !== dirname3(realTarget) || !contains(allowedDirectory, realTarget)) {
4899
+ const realParent = await realpath(dirname5(realTarget));
4900
+ if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
3761
4901
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
3762
4902
  path: realTarget,
3763
4903
  bytes,
@@ -3782,7 +4922,7 @@ function expandAndValidate(requestedPath, homeDir) {
3782
4922
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
3783
4923
  return null;
3784
4924
  }
3785
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join5(homeDir, requestedPath.slice(2)) : requestedPath;
4925
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
3786
4926
  if (expanded.split(/[/\\]/).includes("..")) {
3787
4927
  return null;
3788
4928
  }
@@ -3800,7 +4940,7 @@ async function resolveNearestExistingAncestor(directory) {
3800
4940
  try {
3801
4941
  return { existingAncestor: await realpath(current), missingSegments };
3802
4942
  } catch (err) {
3803
- const parent = dirname3(current);
4943
+ const parent = dirname5(current);
3804
4944
  if (err.code !== "ENOENT" || parent === current) {
3805
4945
  throw err;
3806
4946
  }
@@ -3855,16 +4995,16 @@ function contains(realDirectory, realTarget) {
3855
4995
  async function createMissingDirectories(existingAncestor, missingSegments) {
3856
4996
  let current = existingAncestor;
3857
4997
  for (const segment of missingSegments) {
3858
- current = join5(current, segment);
4998
+ current = join7(current, segment);
3859
4999
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
3860
5000
  await chmod(current, DIRECTORY_MODE);
3861
5001
  }
3862
5002
  }
3863
5003
  async function writeAtomically(realTarget, content) {
3864
- const temporaryPath = join5(dirname3(realTarget), `.evident-push-${randomUUID()}.tmp`);
5004
+ const temporaryPath = join7(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
3865
5005
  let handle;
3866
5006
  try {
3867
- handle = await open2(temporaryPath, "wx", FILE_MODE);
5007
+ handle = await open3(temporaryPath, "wx", FILE_MODE);
3868
5008
  await handle.writeFile(content);
3869
5009
  await handle.chmod(FILE_MODE);
3870
5010
  await handle.close();
@@ -3991,12 +5131,12 @@ var NOT_APPLIED = {
3991
5131
  opencodeAuthApplied: false
3992
5132
  };
3993
5133
  function isClaudeCredentialPath(requestedPath, homeDir) {
3994
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3995
- return expanded === join6(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
5134
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
5135
+ return expanded === join8(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
3996
5136
  }
3997
5137
  function isOpenCodeAuthPath(requestedPath, homeDir) {
3998
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
3999
- return expanded === join6(homeDir, ...OPENCODE_AUTH_SEGMENTS);
5138
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join8(homeDir, requestedPath.slice(2)) : requestedPath;
5139
+ return expanded === join8(homeDir, ...OPENCODE_AUTH_SEGMENTS);
4000
5140
  }
4001
5141
  async function applyOne(options, file) {
4002
5142
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -4156,6 +5296,9 @@ var DEFAULT_RETRY_POLICY = {
4156
5296
  baseDelayMs: 500,
4157
5297
  maxDelayMs: 3e4
4158
5298
  };
5299
+ var SESSION_ERROR_STREAM_HEALTHY_MS = 5e3;
5300
+ var SESSION_ERROR_BUFFER_TTL_MS = SESSION_ERROR_STREAM_HEALTHY_MS;
5301
+ var MAX_BUFFERED_SESSION_ERRORS = 256;
4159
5302
  var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
4160
5303
  var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
4161
5304
  var DEFAULT_STUCK_QUEUED_MS = 6e4;
@@ -4296,6 +5439,17 @@ var ChannelDriver = class _ChannelDriver {
4296
5439
  * message; it is removed once its in-flight set empties.
4297
5440
  */
4298
5441
  watchers = /* @__PURE__ */ new Map();
5442
+ sessionErrorStream = null;
5443
+ /**
5444
+ * Session-error failures currently being reported; entries are empty at rest
5445
+ * because each handoff deletes its id in `finally`.
5446
+ */
5447
+ sessionErrorHandled = /* @__PURE__ */ new Set();
5448
+ /**
5449
+ * Session errors that arrived before their dispatch was registered. Bounded FIFO
5450
+ * with a short TTL so an unmatched session cannot retain an event indefinitely.
5451
+ */
5452
+ bufferedSessionErrors = /* @__PURE__ */ new Map();
4299
5453
  /**
4300
5454
  * AUTHORITATIVE local dedup (WI-3): Evident message ids that have been
4301
5455
  * dispatched and are still in-flight. A message in this set is never
@@ -4523,6 +5677,7 @@ var ChannelDriver = class _ChannelDriver {
4523
5677
  * and stops opencode.
4524
5678
  */
4525
5679
  stopped = false;
5680
+ recycleRequestedFlag = false;
4526
5681
  constructor(config) {
4527
5682
  this.agentId = config.agentId;
4528
5683
  this.port = config.port;
@@ -4542,7 +5697,7 @@ var ChannelDriver = class _ChannelDriver {
4542
5697
  this.stuckQueuedMs = config.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
4543
5698
  this.now = config.now ?? (() => Date.now());
4544
5699
  this.fileSyncDirectories = config.fileSyncDirectories ?? [];
4545
- this.homeDir = config.homeDir ?? homedir3();
5700
+ this.homeDir = config.homeDir ?? homedir5();
4546
5701
  this.maxActiveSessions = config.maxActiveSessions;
4547
5702
  this.watcherStallMs = config.watcherStallMs ?? WATCHER_STALL_MS;
4548
5703
  this.wedgeWarningIntervalMs = config.wedgeWarningIntervalMs ?? WEDGE_WARNING_INTERVAL_MS;
@@ -4628,6 +5783,9 @@ var ChannelDriver = class _ChannelDriver {
4628
5783
  let dispatched = 0;
4629
5784
  try {
4630
5785
  const conversations = await this.getPendingConversations();
5786
+ if (this.recycleRequestedFlag) {
5787
+ this.stop();
5788
+ }
4631
5789
  if (conversations.length > 0) {
4632
5790
  const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
4633
5791
  this.log({
@@ -4755,6 +5913,16 @@ var ChannelDriver = class _ChannelDriver {
4755
5913
  */
4756
5914
  stop() {
4757
5915
  this.stopped = true;
5916
+ this.sessionErrorStream?.abort.abort();
5917
+ this.sessionErrorStream = null;
5918
+ }
5919
+ /**
5920
+ * The server clears this request when a new MicroVM identity is recorded, so a
5921
+ * same-VM tunnel reconnect does not consume it. This is a plain read rather
5922
+ * than a consume; `run.ts` guards the action once-only.
5923
+ */
5924
+ get recycleRequested() {
5925
+ return this.recycleRequestedFlag;
4758
5926
  }
4759
5927
  /**
4760
5928
  * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
@@ -4821,6 +5989,7 @@ var ChannelDriver = class _ChannelDriver {
4821
5989
  */
4822
5990
  async processConversation(conv) {
4823
5991
  const { sessionId, refusedSessionId, created: sessionCreated } = await this.ensureSession(conv);
5992
+ this.ensureSessionErrorStream();
4824
5993
  const messages = await this.getPendingMessages(conv.id);
4825
5994
  let dispatched = 0;
4826
5995
  let skippedAlreadyDispatched = 0;
@@ -4893,7 +6062,7 @@ var ChannelDriver = class _ChannelDriver {
4893
6062
  this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
4894
6063
  break;
4895
6064
  }
4896
- const errorMessage = err instanceof Error ? err.message : String(err);
6065
+ const errorMessage3 = err instanceof Error ? err.message : String(err);
4897
6066
  this.sessions.delete(conv.id);
4898
6067
  this.supersede(conv.id, sessionId);
4899
6068
  this.log({
@@ -4902,7 +6071,7 @@ var ChannelDriver = class _ChannelDriver {
4902
6071
  conversation_id: conv.id,
4903
6072
  message_id: message.id
4904
6073
  });
4905
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
6074
+ await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
4906
6075
  this.log({
4907
6076
  level: "warn",
4908
6077
  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)}`,
@@ -4913,7 +6082,7 @@ var ChannelDriver = class _ChannelDriver {
4913
6082
  });
4914
6083
  this.log({
4915
6084
  level: "error",
4916
- message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
6085
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage3}`,
4917
6086
  conversation_id: conv.id,
4918
6087
  message_id: message.id
4919
6088
  });
@@ -4934,14 +6103,14 @@ var ChannelDriver = class _ChannelDriver {
4934
6103
  this.unconfirmedDispatchFailures.delete(message.id);
4935
6104
  this.sessions.delete(conv.id);
4936
6105
  this.supersede(conv.id, sessionId);
4937
- const errorMessage = `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.`;
6106
+ 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.`;
4938
6107
  this.log({
4939
6108
  level: "error",
4940
- message: errorMessage,
6109
+ message: errorMessage3,
4941
6110
  conversation_id: conv.id,
4942
6111
  message_id: message.id
4943
6112
  });
4944
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
6113
+ await this.markFailed(conv.id, message.id, null, errorMessage3).catch((markErr) => {
4945
6114
  this.log({
4946
6115
  level: "warn",
4947
6116
  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)}`,
@@ -5275,6 +6444,9 @@ var ChannelDriver = class _ChannelDriver {
5275
6444
  });
5276
6445
  await this.markFailed(conv.id, message.id, sessionId, error2, usage, failure);
5277
6446
  }
6447
+ if (ocId !== null) {
6448
+ await this.reportSubagentAuthFailures(conv.id, ocId, message.id, messages);
6449
+ }
5278
6450
  } catch (err) {
5279
6451
  if (err instanceof ChannelAuthError) throw err;
5280
6452
  this.log({
@@ -5814,6 +6986,24 @@ var ChannelDriver = class _ChannelDriver {
5814
6986
  ambiguousPinnedSinceMs: 0,
5815
6987
  ambiguousResolved: false
5816
6988
  });
6989
+ const buffered = this.bufferedSessionErrors.get(sessionId);
6990
+ if (!buffered) return;
6991
+ this.bufferedSessionErrors.delete(sessionId);
6992
+ if (this.now() - buffered.receivedAt < SESSION_ERROR_BUFFER_TTL_MS) {
6993
+ this.handleSessionError(buffered.event);
6994
+ }
6995
+ }
6996
+ bufferSessionError(event) {
6997
+ this.bufferedSessionErrors.delete(event.sessionId);
6998
+ this.bufferedSessionErrors.set(event.sessionId, {
6999
+ event,
7000
+ receivedAt: this.now()
7001
+ });
7002
+ while (this.bufferedSessionErrors.size > MAX_BUFFERED_SESSION_ERRORS) {
7003
+ const oldest = this.bufferedSessionErrors.keys().next().value;
7004
+ if (typeof oldest !== "string") break;
7005
+ this.bufferedSessionErrors.delete(oldest);
7006
+ }
5817
7007
  }
5818
7008
  /**
5819
7009
  * Build a fresh `SessionWatcher`. Seeds `lastTickAt`/`lastObservedTickAt`
@@ -6018,6 +7208,7 @@ var ChannelDriver = class _ChannelDriver {
6018
7208
  ensureWatcherRunning(sessionId) {
6019
7209
  const watcher = this.watchers.get(sessionId);
6020
7210
  if (!watcher) return;
7211
+ this.ensureSessionErrorStream();
6021
7212
  if (watcher.loop) return;
6022
7213
  if (watcher.inFlight.size === 0) {
6023
7214
  this.watchers.delete(sessionId);
@@ -6033,6 +7224,154 @@ var ChannelDriver = class _ChannelDriver {
6033
7224
  });
6034
7225
  watcher.loop = loop;
6035
7226
  }
7227
+ ensureSessionErrorStream() {
7228
+ if (this.sessionErrorStream || this.stopped) return;
7229
+ const abort = new AbortController();
7230
+ const loop = this.runSessionErrorStream(abort.signal);
7231
+ this.sessionErrorStream = { abort, loop };
7232
+ }
7233
+ async runSessionErrorStream(signal) {
7234
+ let attempt = 0;
7235
+ let warned = false;
7236
+ while (!this.stopped && !signal.aborted) {
7237
+ const openedAt = this.now();
7238
+ try {
7239
+ const outcome = await readSessionErrorStream(this.port, {
7240
+ signal,
7241
+ onSessionError: (event) => this.handleSessionError(event)
7242
+ });
7243
+ if (outcome.reason === "aborted" || signal.aborted) return;
7244
+ const healthy = this.now() - openedAt >= SESSION_ERROR_STREAM_HEALTHY_MS;
7245
+ if (outcome.reason === "unavailable" || outcome.reason === "ended") {
7246
+ if (!healthy) {
7247
+ const detail = outcome.reason === "unavailable" ? outcome.detail : "stream ended";
7248
+ this.log({
7249
+ level: warned ? "debug" : "warn",
7250
+ message: `OpenCode session-error stream ${warned ? "still unavailable" : "unavailable"} (${detail}); transcript polling remains the evidence path`
7251
+ });
7252
+ warned = true;
7253
+ }
7254
+ }
7255
+ if (healthy) {
7256
+ if (warned) {
7257
+ this.log({
7258
+ level: "info",
7259
+ message: "OpenCode session-error stream reconnected; transcript polling remains the evidence path"
7260
+ });
7261
+ warned = false;
7262
+ }
7263
+ attempt = 0;
7264
+ } else {
7265
+ attempt += 1;
7266
+ }
7267
+ if (this.stopped || signal.aborted) return;
7268
+ await this.sleep(backoffDelay(healthy ? 0 : attempt - 1, this.retry));
7269
+ } catch (err) {
7270
+ if (this.stopped || signal.aborted) return;
7271
+ this.log({
7272
+ level: "error",
7273
+ message: `OpenCode session-error stream failed unexpectedly: ${err instanceof Error ? err.message : String(err)}`
7274
+ });
7275
+ const healthy = this.now() - openedAt >= SESSION_ERROR_STREAM_HEALTHY_MS;
7276
+ const delayAttempt = healthy ? 0 : attempt;
7277
+ attempt = healthy ? 0 : attempt + 1;
7278
+ try {
7279
+ await this.sleep(backoffDelay(delayAttempt, this.retry));
7280
+ } catch (sleepErr) {
7281
+ this.log({
7282
+ level: "error",
7283
+ message: `OpenCode session-error stream backoff failed unexpectedly: ${sleepErr instanceof Error ? sleepErr.message : String(sleepErr)}`
7284
+ });
7285
+ }
7286
+ }
7287
+ }
7288
+ }
7289
+ handleSessionError(event) {
7290
+ try {
7291
+ const watcher = this.watchers.get(event.sessionId);
7292
+ if (!watcher) {
7293
+ this.bufferSessionError(event);
7294
+ this.log({
7295
+ level: "debug",
7296
+ message: `Ignoring session error for unknown session ${event.sessionId.slice(0, 8)}`
7297
+ });
7298
+ return;
7299
+ }
7300
+ if ([...watcher.inFlight.values()].some((message) => message.started && !message.done)) {
7301
+ this.log({
7302
+ level: "debug",
7303
+ message: `A turn is already running in session ${event.sessionId.slice(0, 8)} \u2014 deferring to transcript polling`,
7304
+ conversation_id: watcher.conv.id
7305
+ });
7306
+ return;
7307
+ }
7308
+ const inFlight = [...watcher.inFlight.values()].filter((message) => !message.started && !message.done).sort((a, b) => a.dispatchedAt - b.dispatchedAt)[0];
7309
+ if (!inFlight) {
7310
+ this.bufferSessionError(event);
7311
+ this.log({
7312
+ level: "debug",
7313
+ message: `No queued in-flight turn to correlate with session error in ${event.sessionId.slice(0, 8)}`,
7314
+ conversation_id: watcher.conv.id
7315
+ });
7316
+ return;
7317
+ }
7318
+ if (this.sessionErrorHandled.has(inFlight.evidentMessageId)) return;
7319
+ this.sessionErrorHandled.add(inFlight.evidentMessageId);
7320
+ void this.failFromSessionError(watcher, event, inFlight);
7321
+ } catch (err) {
7322
+ this.log({
7323
+ level: "error",
7324
+ message: `Failed to handle OpenCode session error for ${event.sessionId.slice(0, 8)}: ${err instanceof Error ? err.message : String(err)}`
7325
+ });
7326
+ }
7327
+ }
7328
+ async failFromSessionError(watcher, event, inFlight) {
7329
+ try {
7330
+ const messages = await getSessionMessages(this.port, event.sessionId);
7331
+ const state = messageRunState(messages, inFlight.opencodeMessageId);
7332
+ if (state !== "queued") {
7333
+ this.log({
7334
+ level: "debug",
7335
+ message: `Session error for message ${inFlight.evidentMessageId.slice(0, 8)} observed state ${state}; leaving it to transcript polling`,
7336
+ conversation_id: watcher.conv.id,
7337
+ message_id: inFlight.evidentMessageId
7338
+ });
7339
+ return;
7340
+ }
7341
+ this.log({
7342
+ level: "error",
7343
+ message: `OpenCode could not run message ${inFlight.evidentMessageId.slice(0, 8)} in session ${event.sessionId.slice(0, 8)}: ${event.reason}`,
7344
+ conversation_id: watcher.conv.id,
7345
+ message_id: inFlight.evidentMessageId
7346
+ });
7347
+ await this.markFailed(
7348
+ watcher.conv.id,
7349
+ inFlight.evidentMessageId,
7350
+ event.sessionId,
7351
+ `OpenCode could not run this turn: ${event.reason}`
7352
+ );
7353
+ inFlight.done = true;
7354
+ this.removeInFlight(watcher, inFlight.evidentMessageId);
7355
+ } catch (err) {
7356
+ if (err instanceof ChannelAuthError) {
7357
+ this.log({
7358
+ level: "warn",
7359
+ message: `OpenCode session error could not mark message ${inFlight.evidentMessageId.slice(0, 8)} failed because authentication failed: ${err.message}; leaving it to transcript polling / the existing give-up path`,
7360
+ conversation_id: watcher.conv.id,
7361
+ message_id: inFlight.evidentMessageId
7362
+ });
7363
+ } else {
7364
+ this.log({
7365
+ level: "warn",
7366
+ message: `OpenCode session error could not mark message ${inFlight.evidentMessageId.slice(0, 8)} failed: ${err instanceof Error ? err.message : String(err)}; leaving it to transcript polling / the existing give-up path`,
7367
+ conversation_id: watcher.conv.id,
7368
+ message_id: inFlight.evidentMessageId
7369
+ });
7370
+ }
7371
+ } finally {
7372
+ this.sessionErrorHandled.delete(inFlight.evidentMessageId);
7373
+ }
7374
+ }
6036
7375
  /**
6037
7376
  * The per-session polling loop (WI-3). Once per tick it:
6038
7377
  * 1. polls `GET /session/:id/message` once and, per in-flight message,
@@ -6239,6 +7578,12 @@ var ChannelDriver = class _ChannelDriver {
6239
7578
  return;
6240
7579
  }
6241
7580
  inFlight.done = true;
7581
+ await this.reportSubagentAuthFailures(
7582
+ watcher.conv.id,
7583
+ inFlight.opencodeMessageId,
7584
+ inFlight.evidentMessageId,
7585
+ messages
7586
+ );
6242
7587
  }
6243
7588
  this.removeInFlight(watcher, inFlight.evidentMessageId);
6244
7589
  return;
@@ -6481,6 +7826,12 @@ var ChannelDriver = class _ChannelDriver {
6481
7826
  return;
6482
7827
  }
6483
7828
  inFlight.done = true;
7829
+ await this.reportSubagentAuthFailures(
7830
+ watcher.conv.id,
7831
+ inFlight.opencodeMessageId,
7832
+ inFlight.evidentMessageId,
7833
+ messages
7834
+ );
6484
7835
  }
6485
7836
  this.removeInFlight(watcher, inFlight.evidentMessageId);
6486
7837
  }
@@ -6651,6 +8002,7 @@ var ChannelDriver = class _ChannelDriver {
6651
8002
  });
6652
8003
  return;
6653
8004
  }
8005
+ await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
6654
8006
  this.dontRedispatch.delete(row.id);
6655
8007
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
6656
8008
  return;
@@ -6812,6 +8164,9 @@ var ChannelDriver = class _ChannelDriver {
6812
8164
  });
6813
8165
  return;
6814
8166
  }
8167
+ if (ocId !== null) {
8168
+ await this.reportSubagentAuthFailures(row.conversation_id, ocId, row.id, messages);
8169
+ }
6815
8170
  this.dontRedispatch.delete(row.id);
6816
8171
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
6817
8172
  }
@@ -6905,14 +8260,14 @@ var ChannelDriver = class _ChannelDriver {
6905
8260
  this.unconfirmedDispatchFailures.delete(row.id);
6906
8261
  this.sessions.delete(readoptConv.id);
6907
8262
  this.supersede(readoptConv.id, sessionId);
6908
- const errorMessage = `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.`;
8263
+ 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.`;
6909
8264
  this.log({
6910
8265
  level: "error",
6911
- message: errorMessage,
8266
+ message: errorMessage3,
6912
8267
  conversation_id: row.conversation_id,
6913
8268
  message_id: row.id
6914
8269
  });
6915
- await this.markFailed(row.conversation_id, row.id, null, errorMessage).catch((markErr) => {
8270
+ await this.markFailed(row.conversation_id, row.id, null, errorMessage3).catch((markErr) => {
6916
8271
  this.log({
6917
8272
  level: "warn",
6918
8273
  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)}`,
@@ -7527,6 +8882,7 @@ var ChannelDriver = class _ChannelDriver {
7527
8882
  throw new Error(`Failed to get pending conversations: HTTP ${res.status}`);
7528
8883
  }
7529
8884
  const data = await res.json();
8885
+ this.recycleRequestedFlag = data.recycle_requested === true;
7530
8886
  let conversations = data.conversations;
7531
8887
  if (this.conversationFilter) {
7532
8888
  conversations = conversations.filter((c) => c.id === this.conversationFilter);
@@ -7762,6 +9118,111 @@ var ChannelDriver = class _ChannelDriver {
7762
9118
  reply?.info?.modelID ?? null
7763
9119
  );
7764
9120
  }
9121
+ async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
9122
+ const providerId = failure.providerId ?? "(unknown)";
9123
+ try {
9124
+ const res = await this.fetchImpl(
9125
+ `${this.apiUrl}/runners/${this.agentId}/model-auth-failures`,
9126
+ {
9127
+ method: "POST",
9128
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
9129
+ body: JSON.stringify({
9130
+ provider_id: failure.providerId,
9131
+ model_id: failure.modelId,
9132
+ reason: failure.reason
9133
+ })
9134
+ }
9135
+ );
9136
+ if (!res.ok) {
9137
+ this.log({
9138
+ level: "warn",
9139
+ message: `Sub-agent model-auth report for provider ${providerId} (${failure.reason}) returned HTTP ${res.status} (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)})`,
9140
+ conversation_id: conversationId,
9141
+ message_id: messageId
9142
+ });
9143
+ }
9144
+ } catch (err) {
9145
+ this.log({
9146
+ level: "warn",
9147
+ message: `Sub-agent model-auth report for provider ${providerId} (${failure.reason}) failed with status network-error (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)}): ${err instanceof Error ? err.message : String(err)}`,
9148
+ conversation_id: conversationId,
9149
+ message_id: messageId
9150
+ });
9151
+ }
9152
+ }
9153
+ async clearSubagentModelAuthFailure(providerId, conversationId, messageId) {
9154
+ try {
9155
+ const res = await this.fetchImpl(
9156
+ `${this.apiUrl}/runners/${this.agentId}/model-auth-failures/${encodeURIComponent(providerId)}`,
9157
+ {
9158
+ method: "DELETE",
9159
+ headers: { Authorization: this.getAuthHeader() }
9160
+ }
9161
+ );
9162
+ if (!res.ok) {
9163
+ this.log({
9164
+ level: "warn",
9165
+ message: `Sub-agent model-auth clear for provider ${providerId} returned HTTP ${res.status} (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)})`,
9166
+ conversation_id: conversationId,
9167
+ message_id: messageId
9168
+ });
9169
+ }
9170
+ } catch (err) {
9171
+ this.log({
9172
+ level: "warn",
9173
+ message: `Sub-agent model-auth clear for provider ${providerId} failed with status network-error (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)}): ${err instanceof Error ? err.message : String(err)}`,
9174
+ conversation_id: conversationId,
9175
+ message_id: messageId
9176
+ });
9177
+ }
9178
+ }
9179
+ async reportSubagentAuthFailures(conversationId, opencodeMessageId, evidentMessageId, messages) {
9180
+ const refs = collectSubagentSessions(messages, opencodeMessageId);
9181
+ if (refs.length === 0) return;
9182
+ const failedProviders = /* @__PURE__ */ new Map();
9183
+ const succeededProviders = /* @__PURE__ */ new Set();
9184
+ for (const ref of refs) {
9185
+ try {
9186
+ const childMessages = await getSessionMessages(this.port, ref.sessionId);
9187
+ if (childMessages === null) {
9188
+ this.log({
9189
+ level: "debug",
9190
+ message: `Could not read sub-agent session ${ref.sessionId} while checking credential failures \u2014 skipping it`,
9191
+ conversation_id: conversationId,
9192
+ message_id: evidentMessageId
9193
+ });
9194
+ continue;
9195
+ }
9196
+ const outcome = findSubagentAuthOutcome(childMessages, ref.startedAtMs);
9197
+ if (!outcome) continue;
9198
+ if (outcome.outcome === "failed") {
9199
+ failedProviders.set(outcome.providerId, outcome.failure);
9200
+ } else {
9201
+ succeededProviders.add(outcome.providerId);
9202
+ }
9203
+ } catch (err) {
9204
+ this.log({
9205
+ level: "warn",
9206
+ message: `Failed to inspect sub-agent session ${ref.sessionId} for credential failures (conversation ${conversationId.slice(0, 8)}, message ${evidentMessageId.slice(0, 8)}): ${err instanceof Error ? err.message : String(err)}`,
9207
+ conversation_id: conversationId,
9208
+ message_id: evidentMessageId
9209
+ });
9210
+ }
9211
+ }
9212
+ for (const [providerId, failure] of failedProviders) {
9213
+ this.log({
9214
+ level: "warn",
9215
+ message: `Sub-agent turn failed on provider ${providerId} (${failure.reason}) \u2014 recording credential evidence`,
9216
+ conversation_id: conversationId,
9217
+ message_id: evidentMessageId
9218
+ });
9219
+ await this.recordSubagentModelAuthFailure(failure, conversationId, evidentMessageId);
9220
+ }
9221
+ for (const providerId of succeededProviders) {
9222
+ if (failedProviders.has(providerId)) continue;
9223
+ await this.clearSubagentModelAuthFailure(providerId, conversationId, evidentMessageId);
9224
+ }
9225
+ }
7765
9226
  /**
7766
9227
  * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
7767
9228
  * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
@@ -7915,6 +9376,13 @@ import chalk5 from "chalk";
7915
9376
  import ora2 from "ora";
7916
9377
  import { select as select2 } from "@inquirer/prompts";
7917
9378
  var INTERACTIVE_START_TIMEOUT_MS = 3e4;
9379
+ function checkNonInteractivePortConflict(port, isPortInUseFn) {
9380
+ if (isPortInUseFn(port)) {
9381
+ throw new Error(
9382
+ `Port ${port} is already in use by a non-OpenCode process. Free it or pass --port.`
9383
+ );
9384
+ }
9385
+ }
7918
9386
  async function ensureOpenCodeRunning(ctx) {
7919
9387
  const healthCheck = await checkOpenCodeHealth(ctx.port);
7920
9388
  if (healthCheck.healthy) {
@@ -7962,8 +9430,9 @@ async function ensureOpenCodeRunning(ctx) {
7962
9430
  }
7963
9431
  }
7964
9432
  if (!ctx.interactive) {
9433
+ checkNonInteractivePortConflict(ctx.port, isPortInUse);
7965
9434
  ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
7966
- const proc = await startOpenCode(ctx.port);
9435
+ const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
7967
9436
  const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs);
7968
9437
  if (!health.healthy) {
7969
9438
  return {
@@ -7981,66 +9450,721 @@ async function ensureOpenCodeRunning(ctx) {
7981
9450
  notReadyReason: null
7982
9451
  };
7983
9452
  }
7984
- let port = ctx.port;
7985
- if (isPortInUse(port)) {
7986
- console.log(chalk5.yellow(`
7987
- Port ${port} is already in use.`));
7988
- const alternativePort = findAvailablePort(port + 1);
7989
- if (alternativePort) {
7990
- const useAlternative = await select2({
7991
- message: `Use port ${alternativePort} instead?`,
7992
- choices: [
7993
- { name: `Yes, use port ${alternativePort}`, value: "yes" },
7994
- { name: "No, I will free the port manually", value: "no" }
7995
- ]
7996
- });
7997
- if (useAlternative === "yes") {
7998
- port = alternativePort;
7999
- } else {
8000
- throw new Error(`Port ${ctx.port} is in use`);
9453
+ let port = ctx.port;
9454
+ if (isPortInUse(port)) {
9455
+ console.log(chalk5.yellow(`
9456
+ Port ${port} is already in use.`));
9457
+ const alternativePort = findAvailablePort(port + 1);
9458
+ if (alternativePort) {
9459
+ const useAlternative = await select2({
9460
+ message: `Use port ${alternativePort} instead?`,
9461
+ choices: [
9462
+ { name: `Yes, use port ${alternativePort}`, value: "yes" },
9463
+ { name: "No, I will free the port manually", value: "no" }
9464
+ ]
9465
+ });
9466
+ if (useAlternative === "yes") {
9467
+ port = alternativePort;
9468
+ } else {
9469
+ throw new Error(`Port ${ctx.port} is in use`);
9470
+ }
9471
+ }
9472
+ }
9473
+ const action = await select2({
9474
+ message: "OpenCode is not running. What would you like to do?",
9475
+ choices: [
9476
+ {
9477
+ name: "Start OpenCode for me",
9478
+ value: "start",
9479
+ description: `Run 'opencode serve --port ${port}'`
9480
+ },
9481
+ {
9482
+ name: "Show me the command",
9483
+ value: "manual",
9484
+ description: "Display the command to run manually"
9485
+ },
9486
+ {
9487
+ name: "Continue without OpenCode",
9488
+ value: "continue",
9489
+ description: "Requests will fail until OpenCode starts"
9490
+ }
9491
+ ]
9492
+ });
9493
+ if (action === "manual") {
9494
+ blank();
9495
+ console.log(chalk5.bold("Run this command in another terminal:"));
9496
+ blank();
9497
+ console.log(` ${chalk5.cyan(`opencode serve --port ${port}`)}`);
9498
+ blank();
9499
+ throw new Error("Please start OpenCode manually");
9500
+ }
9501
+ if (action === "start") {
9502
+ const spinner = ora2("Starting OpenCode...").start();
9503
+ const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
9504
+ const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
9505
+ if (!health.healthy) {
9506
+ spinner.fail("Failed to start OpenCode");
9507
+ throw new Error("OpenCode failed to start");
9508
+ }
9509
+ spinner.stop();
9510
+ return { port, process: proc, version: health.version ?? null, notReadyReason: null };
9511
+ }
9512
+ return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
9513
+ }
9514
+
9515
+ // src/commands/ensure-opencode-v2.ts
9516
+ import chalk6 from "chalk";
9517
+ import { select as select3 } from "@inquirer/prompts";
9518
+ async function probeOpenCode2WithoutPassword(port) {
9519
+ try {
9520
+ const response = await fetch(`http://127.0.0.1:${port}/global/health`, {
9521
+ signal: AbortSignal.timeout(2e3)
9522
+ });
9523
+ if (response.status === 401) {
9524
+ return { healthy: false, authFailed: true, error: "HTTP 401" };
9525
+ }
9526
+ if (!response.ok) {
9527
+ return { healthy: false, error: `HTTP ${response.status}` };
9528
+ }
9529
+ return { healthy: true };
9530
+ } catch (error2) {
9531
+ return {
9532
+ healthy: false,
9533
+ error: error2 instanceof Error ? error2.message : "Unknown error"
9534
+ };
9535
+ }
9536
+ }
9537
+ function unknownPasswordError(port) {
9538
+ return new Error(
9539
+ `OpenCode V2 (opencode2) is already running on port ${port} with a password this runner doesn't know. Free the port or pass --port.`
9540
+ );
9541
+ }
9542
+ function v2SessionSupportIncompleteError() {
9543
+ return new Error(
9544
+ "OpenCode V2 (opencode2) session support is incomplete \u2014 see https://github.com/sroze/evident/issues/2075. Not starting a new opencode2 process."
9545
+ );
9546
+ }
9547
+ async function ensureOpenCode2Running(ctx) {
9548
+ const initialHealth = await probeOpenCode2WithoutPassword(ctx.port);
9549
+ if (initialHealth.authFailed) {
9550
+ throw unknownPasswordError(ctx.port);
9551
+ }
9552
+ if (initialHealth.healthy) {
9553
+ return {
9554
+ port: ctx.port,
9555
+ process: null,
9556
+ version: null,
9557
+ notReadyReason: null,
9558
+ password: null
9559
+ };
9560
+ }
9561
+ if (!isOpenCode2Installed()) {
9562
+ throw new Error(
9563
+ "OpenCode V2 (opencode2) is not installed. Install it with: npm install -g @opencode-ai/cli@beta"
9564
+ );
9565
+ }
9566
+ let port = ctx.port;
9567
+ if (!ctx.interactive) {
9568
+ checkNonInteractivePortConflict(port, isPortInUse);
9569
+ } else if (isPortInUse(port)) {
9570
+ console.log(chalk6.yellow(`
9571
+ Port ${port} is already in use.`));
9572
+ const alternativePort = findAvailablePort(port + 1);
9573
+ if (alternativePort) {
9574
+ const useAlternative = await select3({
9575
+ message: `Use port ${alternativePort} instead?`,
9576
+ choices: [
9577
+ { name: `Yes, use port ${alternativePort}`, value: "yes" },
9578
+ { name: "No, I will free the port manually", value: "no" }
9579
+ ]
9580
+ });
9581
+ if (useAlternative === "yes") {
9582
+ port = alternativePort;
9583
+ } else {
9584
+ throw new Error(`Port ${ctx.port} is in use`);
9585
+ }
9586
+ }
9587
+ }
9588
+ if (!ctx.interactive) {
9589
+ throw v2SessionSupportIncompleteError();
9590
+ }
9591
+ console.log(chalk6.yellow(`
9592
+ ${v2SessionSupportIncompleteError().message}`));
9593
+ const action = await select3({
9594
+ message: "OpenCode V2 is not running. What would you like to do?",
9595
+ choices: [
9596
+ {
9597
+ name: "Show me the command",
9598
+ value: "manual",
9599
+ description: "Display the command to run manually"
9600
+ },
9601
+ {
9602
+ name: "Continue without OpenCode V2",
9603
+ value: "continue",
9604
+ description: "Requests will fail until OpenCode V2 starts"
9605
+ }
9606
+ ]
9607
+ });
9608
+ if (action === "manual") {
9609
+ blank();
9610
+ console.log(chalk6.bold("Run this command in another terminal:"));
9611
+ blank();
9612
+ console.log(` ${chalk6.cyan(`opencode2 serve --port ${port}`)}`);
9613
+ blank();
9614
+ throw new Error("Please start OpenCode V2 manually");
9615
+ }
9616
+ return {
9617
+ port,
9618
+ process: null,
9619
+ version: null,
9620
+ notReadyReason: "you chose to continue without OpenCode V2",
9621
+ password: null
9622
+ };
9623
+ }
9624
+
9625
+ // src/lib/runner-credentials.ts
9626
+ import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "node:fs";
9627
+ import { spawn as spawn5 } from "node:child_process";
9628
+ var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
9629
+ var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
9630
+ var GITHUB_PROBE_TIMEOUT_MS = 1e4;
9631
+ var GIT_CONFIG_GLOBAL = "/tmp/gitconfig";
9632
+ var GIT_CREDENTIAL_HELPER = "/tmp/git-credential-helper.sh";
9633
+ function commandError2(result) {
9634
+ return result.stderr.trim() || `process exited with code ${result.code ?? "null"}`;
9635
+ }
9636
+ var runCommand2 = (command, args, opts) => {
9637
+ return new Promise((resolve4) => {
9638
+ let child;
9639
+ let stdout = "";
9640
+ let stderr = "";
9641
+ let settled = false;
9642
+ const timer = {};
9643
+ const finish = (result) => {
9644
+ if (settled) return;
9645
+ settled = true;
9646
+ if (timer.handle) clearTimeout(timer.handle);
9647
+ resolve4(result);
9648
+ };
9649
+ try {
9650
+ child = spawn5(command, args, { env: opts.env, stdio: ["ignore", "pipe", "pipe"] });
9651
+ } catch (error2) {
9652
+ finish({
9653
+ code: null,
9654
+ stdout,
9655
+ stderr: error2 instanceof Error ? error2.message : String(error2),
9656
+ timedOut: false
9657
+ });
9658
+ return;
9659
+ }
9660
+ child.stdout?.setEncoding("utf8");
9661
+ child.stdout?.on("data", (chunk) => {
9662
+ stdout += chunk;
9663
+ });
9664
+ child.stderr?.setEncoding("utf8");
9665
+ child.stderr?.on("data", (chunk) => {
9666
+ stderr += chunk;
9667
+ });
9668
+ child.once("error", (error2) => {
9669
+ finish({
9670
+ code: null,
9671
+ stdout,
9672
+ stderr: stderr === "" ? error2.message : `${stderr}
9673
+ ${error2.message}`,
9674
+ timedOut: false
9675
+ });
9676
+ });
9677
+ child.once("close", (code) => finish({ code, stdout, stderr, timedOut: false }));
9678
+ timer.handle = setTimeout(
9679
+ () => {
9680
+ child.kill("SIGKILL");
9681
+ finish({ code: null, stdout, stderr, timedOut: true });
9682
+ },
9683
+ Math.max(0, opts.timeoutMs)
9684
+ );
9685
+ });
9686
+ };
9687
+ function isEnvironmentObject(value) {
9688
+ return typeof value === "object" && value !== null && !Array.isArray(value);
9689
+ }
9690
+ function secretFailure(marker, detail, log3) {
9691
+ const message = `${marker}: ${detail}`;
9692
+ log3(message, "error");
9693
+ return new Error(message);
9694
+ }
9695
+ async function installRunnerSecret({
9696
+ env,
9697
+ log: log3,
9698
+ commandRunner
9699
+ }) {
9700
+ const arn = env.RUNNER_SECRET_ARN?.trim();
9701
+ if (!arn) {
9702
+ log3("runner secret is not configured; continuing without GitHub and MCP credentials");
9703
+ return false;
9704
+ }
9705
+ const result = await (commandRunner ?? runCommand2)(
9706
+ "aws",
9707
+ [
9708
+ "secretsmanager",
9709
+ "get-secret-value",
9710
+ "--secret-id",
9711
+ arn,
9712
+ "--query",
9713
+ "SecretString",
9714
+ "--output",
9715
+ "text"
9716
+ ],
9717
+ { env, timeoutMs: RUNNER_SECRET_FETCH_TIMEOUT_MS }
9718
+ );
9719
+ if (result.timedOut) {
9720
+ throw secretFailure(
9721
+ "CREDENTIAL-RESTORE-TIMEOUT",
9722
+ `runner-secret did not finish within ${RUNNER_SECRET_FETCH_TIMEOUT_MS}ms`,
9723
+ log3
9724
+ );
9725
+ }
9726
+ if (result.code !== 0) {
9727
+ throw secretFailure("RUNNER-SECRET-UNREADABLE", commandError2(result), log3);
9728
+ }
9729
+ let payload;
9730
+ try {
9731
+ payload = JSON.parse(result.stdout);
9732
+ } catch (error2) {
9733
+ log3(
9734
+ `RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object (${error2 instanceof Error ? error2.message : String(error2)})`,
9735
+ "warn"
9736
+ );
9737
+ return false;
9738
+ }
9739
+ if (!isEnvironmentObject(payload)) {
9740
+ log3("RUNNER-SECRET-UNPARSEABLE: secret value is not a JSON object", "warn");
9741
+ return false;
9742
+ }
9743
+ let populated = 0;
9744
+ let skipped = 0;
9745
+ let githubTokenPopulated = false;
9746
+ for (const [key, value] of Object.entries(payload)) {
9747
+ if (typeof value !== "string" || value.length === 0) continue;
9748
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
9749
+ log3(
9750
+ `RUNNER-SECRET-KEY-SKIPPED: ${JSON.stringify(key)} is not a valid environment variable name`,
9751
+ "warn"
9752
+ );
9753
+ skipped += 1;
9754
+ continue;
9755
+ }
9756
+ env[key] = value;
9757
+ populated += 1;
9758
+ if (key === "GH_TOKEN") githubTokenPopulated = true;
9759
+ }
9760
+ if (populated === 0) {
9761
+ log3(
9762
+ "RUNNER-SECRET-UNPOPULATED: populate the runner secret as documented in infrastructure/evident-runner/MICROVM.md",
9763
+ "warn"
9764
+ );
9765
+ } else {
9766
+ log3(
9767
+ `RUNNER-SECRET-OK: exported ${populated} secret values; skipped ${skipped} invalid environment variable names`
9768
+ );
9769
+ }
9770
+ return githubTokenPopulated;
9771
+ }
9772
+ function restoreFailure(operation, result, log3) {
9773
+ const message = `CREDENTIAL-RESTORE-FAILED: ${operation} failed: ${commandError2(result)}`;
9774
+ log3(message, "error");
9775
+ return new Error(message);
9776
+ }
9777
+ async function runCredentialRestore(args, operation, log3, synchroniserRunner) {
9778
+ const result = await synchroniserRunner(args, { timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS });
9779
+ if (result.timedOut) {
9780
+ log3(
9781
+ `CREDENTIAL-RESTORE-TIMEOUT: ${operation} did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
9782
+ "warn"
9783
+ );
9784
+ return result;
9785
+ }
9786
+ if (result.code !== 0) throw restoreFailure(operation, result, log3);
9787
+ return result;
9788
+ }
9789
+ async function restoreCredentialStores({
9790
+ env,
9791
+ log: log3,
9792
+ synchroniserRunner = runSynchroniser
9793
+ }) {
9794
+ await runCredentialRestore(["restore", "claude"], "restore-claude", log3, synchroniserRunner);
9795
+ await runCredentialRestore(["restore", "opencode"], "restore-opencode", log3, synchroniserRunner);
9796
+ const result = await synchroniserRunner(["model-auth-ready"], {
9797
+ timeoutMs: CREDENTIAL_RESTORE_TIMEOUT_MS
9798
+ });
9799
+ if (result.timedOut) {
9800
+ log3(
9801
+ `CREDENTIAL-RESTORE-TIMEOUT: model-auth-ready did not finish within ${CREDENTIAL_RESTORE_TIMEOUT_MS}ms; continuing without it`,
9802
+ "warn"
9803
+ );
9804
+ return;
9805
+ }
9806
+ switch (result.code) {
9807
+ case 0:
9808
+ return;
9809
+ case 10:
9810
+ log3(
9811
+ `no model credentials under s3://${env.LITESTREAM_BUCKET ?? ""}/${env.LITESTREAM_PREFIX ?? ""}/ (neither claude/credentials.json nor opencode/auth.json yielded valid JSON) and neither ANTHROPIC_API_KEY nor OPENAI_API_KEY is set. This VM boots and connects; a turn that needs a model provider fails until one is connected. See 'Seeding a credential store' in infrastructure/evident-runner/MICROVM.md.`,
9812
+ "warn"
9813
+ );
9814
+ return;
9815
+ default:
9816
+ log3("could not determine whether this VM has model credentials", "warn");
9817
+ }
9818
+ }
9819
+ var GIT_CREDENTIAL_HELPER_CONTENT = [
9820
+ "#!/usr/bin/env bash",
9821
+ '[ "$1" = get ] || exit 0',
9822
+ "echo username=x-access-token",
9823
+ 'echo "password=${GH_TOKEN}"',
9824
+ ""
9825
+ ].join("\n");
9826
+ async function probeGitHubAccess({ env, log: log3 }) {
9827
+ const auth = await runCommand2("gh", ["api", "user", "--jq", ".login"], {
9828
+ env,
9829
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9830
+ });
9831
+ if (auth.timedOut) {
9832
+ log3(`GITHUB-PROBE-TIMEOUT: auth probe exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`, "warn");
9833
+ return;
9834
+ }
9835
+ if (auth.code !== 0) {
9836
+ log3(`GITHUB-AUTH-REJECTED: ${commandError2(auth)}`, "warn");
9837
+ return;
9838
+ }
9839
+ log3(`GITHUB-AUTH-OK: ${auth.stdout.trim()}`, "debug");
9840
+ const remote = await runCommand2("git", ["-C", process.cwd(), "remote", "get-url", "origin"], {
9841
+ env,
9842
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9843
+ });
9844
+ if (remote.code !== 0 || remote.timedOut) return;
9845
+ const repo = remote.stdout.trim().replace(/^(?:https:\/\/github\.com\/|git@github\.com:)/, "").replace(/\.git$/, "");
9846
+ if (!repo) return;
9847
+ const repository = await runCommand2("gh", ["api", `repos/${repo}`, "--jq", ".full_name"], {
9848
+ env,
9849
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9850
+ });
9851
+ if (repository.timedOut) {
9852
+ log3(
9853
+ `GITHUB-PROBE-TIMEOUT: repository probe for ${repo} exceeded ${GITHUB_PROBE_TIMEOUT_MS}ms`,
9854
+ "warn"
9855
+ );
9856
+ } else if (repository.code !== 0) {
9857
+ log3(`GITHUB-REPO-INACCESSIBLE: ${repo}: ${commandError2(repository)}`, "warn");
9858
+ }
9859
+ }
9860
+ async function configureGitHubAccess({ env, log: log3 }) {
9861
+ if (!env.GH_TOKEN) {
9862
+ log3("GITHUB-CREDENTIALS-MISSING: GH_TOKEN is unavailable; see RUNNER-SECRET-* above", "warn");
9863
+ return;
9864
+ }
9865
+ try {
9866
+ env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
9867
+ writeFileSync4(GIT_CONFIG_GLOBAL, "");
9868
+ writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
9869
+ chmodSync2(GIT_CREDENTIAL_HELPER, 448);
9870
+ const config = [
9871
+ ["user.name", env.GIT_USER_NAME ?? "evident-bot"],
9872
+ ["user.email", env.GIT_USER_EMAIL ?? "evident-bot@users.noreply.github.com"],
9873
+ ["init.defaultBranch", "main"],
9874
+ ["credential.https://github.com.helper", GIT_CREDENTIAL_HELPER]
9875
+ ];
9876
+ for (const [key, value] of config) {
9877
+ const result = await runCommand2("git", ["config", "--global", key, value], {
9878
+ env,
9879
+ timeoutMs: GITHUB_PROBE_TIMEOUT_MS
9880
+ });
9881
+ if (result.timedOut || result.code !== 0) throw new Error(commandError2(result));
9882
+ }
9883
+ } catch (error2) {
9884
+ log3(
9885
+ `GITHUB-SETUP-FAILED: could not configure local git credentials; continuing without GitHub access (${error2 instanceof Error ? error2.message : String(error2)})`,
9886
+ "warn"
9887
+ );
9888
+ return;
9889
+ }
9890
+ void probeGitHubAccess({ env, log: log3 }).catch((error2) => {
9891
+ log3(
9892
+ `GITHUB-SETUP-FAILED: GitHub access probe failed: ${error2 instanceof Error ? error2.message : String(error2)}`,
9893
+ "warn"
9894
+ );
9895
+ });
9896
+ }
9897
+
9898
+ // src/lib/opencode/config-overlay.ts
9899
+ import { execFileSync as execFileSync2 } from "node:child_process";
9900
+ import { copyFileSync, existsSync as existsSync2, statSync as statSync6 } from "node:fs";
9901
+ import { isAbsolute as isAbsolute2, join as join9, resolve as resolve3 } from "node:path";
9902
+ function isFile(filePath) {
9903
+ return existsSync2(filePath) && statSync6(filePath).isFile();
9904
+ }
9905
+ function applyRunnerOpenCodeConfig({
9906
+ overlayPath,
9907
+ cwd = process.cwd(),
9908
+ log: log3
9909
+ }) {
9910
+ if (!overlayPath) {
9911
+ log3("runner OpenCode config is not configured; using the baked project config", "debug");
9912
+ return;
9913
+ }
9914
+ const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
9915
+ const target = isFile(join9(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
9916
+ if (!isFile(source)) {
9917
+ log3(
9918
+ `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
9919
+ "error"
9920
+ );
9921
+ return;
9922
+ }
9923
+ copyFileSync(source, join9(cwd, target));
9924
+ try {
9925
+ execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
9926
+ stdio: "ignore"
9927
+ });
9928
+ } catch (error2) {
9929
+ const detail = error2 instanceof Error ? error2.message : String(error2);
9930
+ log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
9931
+ }
9932
+ log3(`Applied runner OpenCode config ${source} to ${join9(cwd, target)}`);
9933
+ }
9934
+
9935
+ // src/lib/credential-sync.ts
9936
+ import { renameSync, writeFileSync as writeFileSync5 } from "node:fs";
9937
+ var CREDENTIAL_SYNC_TICK_TIMEOUT_MS = 3e4;
9938
+ var CREDENTIAL_FLUSH_DEADLINE_MS = 8e3;
9939
+ var CREDENTIAL_FLUSH_ABORT_GRACE_MS = 500;
9940
+ var DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS = 60;
9941
+ var STORES = ["claude", "opencode"];
9942
+ var MAX_FLUSH_PASSES = 2;
9943
+ function outcomesWith(outcome) {
9944
+ return { claude: outcome, opencode: outcome };
9945
+ }
9946
+ function errorMessage2(error2) {
9947
+ return error2 instanceof Error ? error2.message : String(error2);
9948
+ }
9949
+ function waitForSettlement(promise, timeoutMs) {
9950
+ return new Promise((resolve4) => {
9951
+ let settled = false;
9952
+ const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
9953
+ const finish = (value) => {
9954
+ if (settled) return;
9955
+ settled = true;
9956
+ clearTimeout(timer);
9957
+ resolve4(value);
9958
+ };
9959
+ promise.then(
9960
+ () => finish(true),
9961
+ () => finish(true)
9962
+ );
9963
+ });
9964
+ }
9965
+ function writeMarker(markerPath, outcomes, log3) {
9966
+ const body = `${STORES.map((store) => `${store}=${outcomes[store]}`).join("\n")}
9967
+ `;
9968
+ const temporaryPath = `${markerPath}.tmp`;
9969
+ try {
9970
+ writeFileSync5(temporaryPath, body, { mode: 384 });
9971
+ renameSync(temporaryPath, markerPath);
9972
+ } catch (error2) {
9973
+ log3(`CREDENTIAL-FLUSH-MARKER-UNWRITABLE: ${markerPath}: ${errorMessage2(error2)}`, "warn");
9974
+ }
9975
+ }
9976
+ function intervalSeconds(env, log3) {
9977
+ const raw = env.CREDS_SYNC_INTERVAL;
9978
+ if (raw === void 0 || /^[1-9][0-9]*$/.test(raw) && Number.isSafeInteger(Number(raw))) {
9979
+ return raw === void 0 ? DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS : Number(raw);
9980
+ }
9981
+ log3(
9982
+ `CREDENTIAL-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${raw}' is not a positive integer; using ${DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS}s`,
9983
+ "warn"
9984
+ );
9985
+ return DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS;
9986
+ }
9987
+ async function runFlushStore(store, env, synchroniserRunner, log3, deadlineAt) {
9988
+ const remainingMs = deadlineAt - Date.now();
9989
+ if (remainingMs <= 0) return { outcome: "timeout", orphaned: false };
9990
+ const controller = new AbortController();
9991
+ let result;
9992
+ let failed = false;
9993
+ const completion = Promise.resolve().then(
9994
+ () => synchroniserRunner(["sync-once", store], {
9995
+ timeoutMs: remainingMs,
9996
+ env,
9997
+ signal: controller.signal
9998
+ })
9999
+ ).then(
10000
+ (value) => {
10001
+ result = value;
10002
+ },
10003
+ (error2) => {
10004
+ failed = true;
10005
+ log3(`CREDENTIAL-FLUSH-ERROR: ${store}: ${errorMessage2(error2)}`, "warn");
10006
+ }
10007
+ );
10008
+ const abortTimer = setTimeout(() => controller.abort(), remainingMs);
10009
+ const settledBeforeDeadline = await waitForSettlement(completion, remainingMs);
10010
+ clearTimeout(abortTimer);
10011
+ if (!settledBeforeDeadline) {
10012
+ controller.abort();
10013
+ const settledAfterAbort = await waitForSettlement(completion, CREDENTIAL_FLUSH_ABORT_GRACE_MS);
10014
+ if (!settledAfterAbort) return { outcome: "timeout", orphaned: true };
10015
+ return { outcome: "timeout", orphaned: false };
10016
+ }
10017
+ if (failed || !result) return { outcome: "failed", orphaned: false };
10018
+ if (result.timedOut || Date.now() >= deadlineAt) {
10019
+ return { outcome: "timeout", orphaned: false };
10020
+ }
10021
+ return { outcome: result.code === 0 ? "ok" : "failed", orphaned: false };
10022
+ }
10023
+ function createCredentialSync({
10024
+ markerPath,
10025
+ env,
10026
+ log: log3,
10027
+ synchroniserRunner = runSynchroniser
10028
+ }) {
10029
+ const persistenceDisabled = !env.PERSISTENCE_BUCKET;
10030
+ let disabled = persistenceDisabled;
10031
+ let armed = false;
10032
+ let stopped = false;
10033
+ let timer;
10034
+ let inFlight;
10035
+ let activeTickAbort;
10036
+ let lastTickFailed;
10037
+ let flushPromise;
10038
+ const scheduleTick = (intervalMs, startTick2) => {
10039
+ if (stopped) return;
10040
+ timer = setTimeout(() => {
10041
+ timer = void 0;
10042
+ startTick2();
10043
+ }, intervalMs);
10044
+ };
10045
+ const startTick = (intervalMs) => {
10046
+ if (stopped) return;
10047
+ const controller = new AbortController();
10048
+ activeTickAbort = controller;
10049
+ const tick = (async () => {
10050
+ const outcomes = {
10051
+ claude: "failed",
10052
+ opencode: "failed"
10053
+ };
10054
+ for (const store of STORES) {
10055
+ if (controller.signal.aborted) break;
10056
+ try {
10057
+ const result = await synchroniserRunner(["sync-once", store], {
10058
+ timeoutMs: CREDENTIAL_SYNC_TICK_TIMEOUT_MS,
10059
+ env,
10060
+ signal: controller.signal
10061
+ });
10062
+ outcomes[store] = !result.timedOut && result.code === 0 ? "ok" : "failed";
10063
+ } catch (error2) {
10064
+ outcomes[store] = "failed";
10065
+ log3(`CREDENTIAL-SYNC-TICK-ERROR: ${store}: ${errorMessage2(error2)}`, "debug");
10066
+ }
10067
+ }
10068
+ const failed = STORES.some((store) => outcomes[store] === "failed");
10069
+ log3(
10070
+ `CREDENTIAL-SYNC-TICK: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
10071
+ "debug"
10072
+ );
10073
+ if (failed && lastTickFailed !== true) {
10074
+ log3(
10075
+ "CREDENTIAL-SYNC-FAILED: an interval credential sync failed; retrying on the next tick",
10076
+ "warn"
10077
+ );
10078
+ } else if (!failed && lastTickFailed === true) {
10079
+ log3("CREDENTIAL-SYNC-RECOVERED: interval credential sync succeeded again", "warn");
10080
+ }
10081
+ lastTickFailed = failed;
10082
+ })().finally(() => {
10083
+ if (activeTickAbort === controller) activeTickAbort = void 0;
10084
+ if (inFlight === tick) inFlight = void 0;
10085
+ scheduleTick(intervalMs, () => startTick(intervalMs));
10086
+ });
10087
+ inFlight = tick;
10088
+ };
10089
+ const performFlush = async () => {
10090
+ stopped = true;
10091
+ if (timer) {
10092
+ clearTimeout(timer);
10093
+ timer = void 0;
10094
+ }
10095
+ const deadlineAt = Date.now() + CREDENTIAL_FLUSH_DEADLINE_MS;
10096
+ if (inFlight) {
10097
+ const settled = await waitForSettlement(inFlight, CREDENTIAL_FLUSH_DEADLINE_MS);
10098
+ if (!settled) {
10099
+ activeTickAbort?.abort();
10100
+ const settledAfterAbort = await waitForSettlement(
10101
+ inFlight,
10102
+ CREDENTIAL_FLUSH_ABORT_GRACE_MS
10103
+ );
10104
+ if (!settledAfterAbort) {
10105
+ log3(
10106
+ "CREDENTIAL-FLUSH-ORPHANED-TICK: a sync-once child could not be proven settled; skipping the boundary flush rather than becoming a second writer",
10107
+ "warn"
10108
+ );
10109
+ return { outcomes: outcomesWith("timeout"), orphaned: true };
10110
+ }
8001
10111
  }
8002
10112
  }
8003
- }
8004
- const action = await select2({
8005
- message: "OpenCode is not running. What would you like to do?",
8006
- choices: [
8007
- {
8008
- name: "Start OpenCode for me",
8009
- value: "start",
8010
- description: `Run 'opencode serve --port ${port}'`
8011
- },
8012
- {
8013
- name: "Show me the command",
8014
- value: "manual",
8015
- description: "Display the command to run manually"
8016
- },
8017
- {
8018
- name: "Continue without OpenCode",
8019
- value: "continue",
8020
- description: "Requests will fail until OpenCode starts"
10113
+ if (disabled) return { outcomes: outcomesWith("skipped"), orphaned: false };
10114
+ const outcomes = outcomesWith("timeout");
10115
+ for (const store of STORES) {
10116
+ const result = await runFlushStore(store, env, synchroniserRunner, log3, deadlineAt);
10117
+ if (result.orphaned) {
10118
+ log3(
10119
+ "CREDENTIAL-FLUSH-ORPHANED-CHILD: a flush sync-once child could not be proven settled; refusing another flush pass rather than becoming a second writer",
10120
+ "warn"
10121
+ );
10122
+ return { outcomes: outcomesWith("timeout"), orphaned: true };
8021
10123
  }
8022
- ]
8023
- });
8024
- if (action === "manual") {
8025
- blank();
8026
- console.log(chalk5.bold("Run this command in another terminal:"));
8027
- blank();
8028
- console.log(` ${chalk5.cyan(`opencode serve --port ${port}`)}`);
8029
- blank();
8030
- throw new Error("Please start OpenCode manually");
8031
- }
8032
- if (action === "start") {
8033
- const spinner = ora2("Starting OpenCode...").start();
8034
- const proc = await startOpenCode(port);
8035
- const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS);
8036
- if (!health.healthy) {
8037
- spinner.fail("Failed to start OpenCode");
8038
- throw new Error("OpenCode failed to start");
10124
+ outcomes[store] = result.outcome;
8039
10125
  }
8040
- spinner.stop();
8041
- return { port, process: proc, version: health.version ?? null, notReadyReason: null };
8042
- }
8043
- return { port, process: null, version: null, notReadyReason: "you chose to continue without it" };
10126
+ return { outcomes, orphaned: false };
10127
+ };
10128
+ let flushPasses = 0;
10129
+ let lastFlush;
10130
+ return {
10131
+ arm() {
10132
+ if (stopped || armed) return;
10133
+ armed = true;
10134
+ if (persistenceDisabled) {
10135
+ disabled = true;
10136
+ log3(
10137
+ "CREDENTIAL-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot",
10138
+ "warn"
10139
+ );
10140
+ return;
10141
+ }
10142
+ disabled = false;
10143
+ const intervalMs = intervalSeconds(env, log3) * 1e3;
10144
+ scheduleTick(intervalMs, () => startTick(intervalMs));
10145
+ },
10146
+ async stopAndFlush(publish) {
10147
+ let result;
10148
+ const runningFlush = flushPromise;
10149
+ if (runningFlush) {
10150
+ result = await runningFlush;
10151
+ } else if (flushPasses >= MAX_FLUSH_PASSES || lastFlush?.orphaned) {
10152
+ result = lastFlush ?? { outcomes: outcomesWith("timeout"), orphaned: true };
10153
+ } else {
10154
+ flushPasses++;
10155
+ const currentFlush = performFlush();
10156
+ flushPromise = currentFlush;
10157
+ try {
10158
+ result = await currentFlush;
10159
+ lastFlush = result;
10160
+ } finally {
10161
+ if (flushPromise === currentFlush) flushPromise = void 0;
10162
+ }
10163
+ }
10164
+ if (publish) writeMarker(markerPath, result.outcomes, log3);
10165
+ return result.outcomes;
10166
+ }
10167
+ };
8044
10168
  }
8045
10169
 
8046
10170
  // src/commands/run.ts
@@ -8080,11 +10204,11 @@ function resolveFileSyncDirectories(raw, homeDir) {
8080
10204
  if (trimmed === "") {
8081
10205
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
8082
10206
  }
8083
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join7(homeDir, trimmed.slice(2)) : trimmed;
8084
- if (!isAbsolute2(expanded)) {
10207
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join10(homeDir, trimmed.slice(2)) : trimmed;
10208
+ if (!isAbsolute3(expanded)) {
8085
10209
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
8086
10210
  }
8087
- const normalized = resolvePath(expanded);
10211
+ const normalized = resolvePath2(expanded);
8088
10212
  if (parse(normalized).root === normalized) {
8089
10213
  throw new Error(
8090
10214
  `--enable-file-sync-to will not allow-list the filesystem root ("${entry}"); name the specific directory the credentials belong in (for example ~/.claude)`
@@ -8104,6 +10228,28 @@ function resolveFileSyncDirectories(raw, homeDir) {
8104
10228
  var DEFAULT_OPENCODE_START_TIMEOUT_SECONDS = 180;
8105
10229
  var MAX_OPENCODE_START_TIMEOUT_SECONDS = 3600;
8106
10230
  var OPENCODE_START_TIMEOUT_ENV = "EVIDENT_OPENCODE_START_TIMEOUT";
10231
+ var OPENCODE_VERSION_ENV = "EVIDENT_OPENCODE_VERSION";
10232
+ function resolveOpenCodeVersion(options, env = process.env) {
10233
+ let raw;
10234
+ let source;
10235
+ if (options.opencodeVersion !== void 0) {
10236
+ raw = options.opencodeVersion;
10237
+ source = "--opencode-version";
10238
+ } else if (env[OPENCODE_VERSION_ENV] !== void 0 && env[OPENCODE_VERSION_ENV] !== "") {
10239
+ raw = env[OPENCODE_VERSION_ENV];
10240
+ source = OPENCODE_VERSION_ENV;
10241
+ } else {
10242
+ return { version: "v1", warnings: [] };
10243
+ }
10244
+ const normalized = raw.trim().toLowerCase();
10245
+ if (normalized !== "v1" && normalized !== "v2") {
10246
+ return {
10247
+ version: "v1",
10248
+ warnings: [`Ignoring invalid ${source} "${raw}": expected v1 or v2; using the default v1`]
10249
+ };
10250
+ }
10251
+ return { version: normalized, warnings: [] };
10252
+ }
8107
10253
  function resolveOpenCodeStartTimeoutMs(options, env = process.env) {
8108
10254
  const defaultMs = DEFAULT_OPENCODE_START_TIMEOUT_SECONDS * 1e3;
8109
10255
  let raw;
@@ -8170,7 +10316,7 @@ function log2(state, message, level = "info") {
8170
10316
  })
8171
10317
  );
8172
10318
  } else if (!state.interactive) {
8173
- const prefix = level === "error" ? chalk6.red("\u2717") : level === "warn" ? chalk6.yellow("!") : level === "debug" ? chalk6.dim("\xB7") : chalk6.green("\u2022");
10319
+ const prefix = level === "error" ? chalk7.red("\u2717") : level === "warn" ? chalk7.yellow("!") : level === "debug" ? chalk7.dim("\xB7") : chalk7.green("\u2022");
8174
10320
  console.log(`${prefix} ${message}`);
8175
10321
  }
8176
10322
  }
@@ -8200,7 +10346,7 @@ function logActivity(state, entry) {
8200
10346
  }
8201
10347
  function reportSessionDbRecovery(state) {
8202
10348
  try {
8203
- const report = drainSessionDbRecoveryReport({ homeDir: homedir4(), env: process.env });
10349
+ const report = drainSessionDbRecoveryReport({ homeDir: homedir6(), env: process.env });
8204
10350
  for (const record of report.records) {
8205
10351
  const activity = buildSessionDbRecoveryActivity(record);
8206
10352
  if (!activity) throw new Error("could not map session-DB recovery record");
@@ -8218,21 +10364,31 @@ function reportSessionDbRecovery(state) {
8218
10364
  );
8219
10365
  }
8220
10366
  }
10367
+ function reportSessionDbRecoveryRecord(state, record) {
10368
+ const activity = buildSessionDbRecoveryActivity(record);
10369
+ if (!activity) throw new Error("could not map session-DB recovery record");
10370
+ logActivity(state, {
10371
+ type: activity.level === "error" ? "error" : "info",
10372
+ level: activity.level,
10373
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
10374
+ metadata: activity.metadata
10375
+ });
10376
+ }
8221
10377
  function displayStatus(state) {
8222
10378
  if (!state.interactive) return;
8223
10379
  const attempt = state.connection?.reconnectAttempt ?? 0;
8224
- const tunnel = state.connected ? chalk6.green("tunnel: connected") : attempt > 0 ? chalk6.yellow(`tunnel: reconnecting (#${attempt})`) : chalk6.yellow("tunnel: connecting");
8225
- const opencode = state.opencodeConnected ? chalk6.green(`opencode: :${state.port}`) : chalk6.red(`opencode: :${state.port} (down)`);
8226
- const messages = state.messageCount > 0 ? chalk6.dim(` \xB7 ${state.messageCount} processed`) : "";
10380
+ const tunnel = state.connected ? chalk7.green("tunnel: connected") : attempt > 0 ? chalk7.yellow(`tunnel: reconnecting (#${attempt})`) : chalk7.yellow("tunnel: connecting");
10381
+ const opencode = state.opencodeConnected ? chalk7.green(`opencode: :${state.port}`) : chalk7.red(`opencode: :${state.port} (down)`);
10382
+ const messages = state.messageCount > 0 ? chalk7.dim(` \xB7 ${state.messageCount} processed`) : "";
8227
10383
  const last = state.activityLog[state.activityLog.length - 1];
8228
- const detail = last ? chalk6.dim(` \xB7 ${last.type === "error" ? last.error ?? "" : last.message ?? ""}`) : "";
10384
+ const detail = last ? chalk7.dim(` \xB7 ${last.type === "error" ? last.error ?? "" : last.message ?? ""}`) : "";
8229
10385
  const agent = state.agentName ?? state.agentId;
8230
10386
  console.log(
8231
- `${chalk6.bold("Evident")} ${chalk6.dim(agent)} ${tunnel} ${opencode}${messages}${detail}`
10387
+ `${chalk7.bold("Evident")} ${chalk7.dim(agent)} ${tunnel} ${opencode}${messages}${detail}`
8232
10388
  );
8233
10389
  }
8234
10390
  async function promptForLogin(promptMessage, successMessage) {
8235
- const action = await select3({
10391
+ const action = await select4({
8236
10392
  message: promptMessage,
8237
10393
  choices: [
8238
10394
  {
@@ -8248,7 +10404,7 @@ async function promptForLogin(promptMessage, successMessage) {
8248
10404
  ]
8249
10405
  });
8250
10406
  if (action === "exit") {
8251
- console.log(chalk6.dim(`
10407
+ console.log(chalk7.dim(`
8252
10408
  You can log in later by running: ${getCliName()} login`));
8253
10409
  process.exit(0);
8254
10410
  }
@@ -8259,7 +10415,7 @@ You can log in later by running: ${getCliName()} login`));
8259
10415
  process.exit(1);
8260
10416
  }
8261
10417
  blank();
8262
- console.log(chalk6.green(successMessage));
10418
+ console.log(chalk7.green(successMessage));
8263
10419
  blank();
8264
10420
  return { token: credentials2.token, authType: "bearer", user: credentials2.user };
8265
10421
  }
@@ -8272,12 +10428,12 @@ async function handleAuthError(state, error2) {
8272
10428
  if (state.interactive) displayStatus(state);
8273
10429
  if (!state.interactive) {
8274
10430
  blank();
8275
- console.log(chalk6.red("Authentication expired"));
8276
- console.log(chalk6.dim("Your authentication token is no longer valid."));
10431
+ console.log(chalk7.red("Authentication expired"));
10432
+ console.log(chalk7.dim("Your authentication token is no longer valid."));
8277
10433
  blank();
8278
- console.log(chalk6.dim("To fix this:"));
8279
- console.log(chalk6.dim(` 1. Run '${getCliName()} login' to re-authenticate`));
8280
- console.log(chalk6.dim(" 2. Restart this command"));
10434
+ console.log(chalk7.dim("To fix this:"));
10435
+ console.log(chalk7.dim(` 1. Run '${getCliName()} login' to re-authenticate`));
10436
+ console.log(chalk7.dim(" 2. Restart this command"));
8281
10437
  blank();
8282
10438
  await cleanup(state);
8283
10439
  await shutdownTelemetry();
@@ -8285,7 +10441,7 @@ async function handleAuthError(state, error2) {
8285
10441
  return { success: false };
8286
10442
  }
8287
10443
  blank();
8288
- console.log(chalk6.yellow("Your authentication has expired."));
10444
+ console.log(chalk7.yellow("Your authentication has expired."));
8289
10445
  blank();
8290
10446
  try {
8291
10447
  const credentials2 = await promptForLogin(
@@ -8330,6 +10486,10 @@ async function driveChannels(state, driver) {
8330
10486
  consecutiveDrainFailures = 0;
8331
10487
  unreachableMs = 0;
8332
10488
  state.messageCount += processed;
10489
+ if (driver.recycleRequested) {
10490
+ await beginGracefulShutdown(state, "recycle");
10491
+ return;
10492
+ }
8333
10493
  const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
8334
10494
  lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
8335
10495
  const fileActivitySnapshot = driver.fileSyncActivity();
@@ -8345,6 +10505,14 @@ async function driveChannels(state, driver) {
8345
10505
  const opencodeAuthApplied = opencodeAuthApplies !== lastSeenOpencodeAuthApplies;
8346
10506
  lastSeenOpencodeAuthApplies = opencodeAuthApplies;
8347
10507
  if (opencodeAuthApplied) state.openaiUsageRearm?.();
10508
+ if (claudeCredentialApplied || opencodeAuthApplied) {
10509
+ void reloadProviderCache(state.port).catch(
10510
+ (error2) => logActivity(state, {
10511
+ type: "error",
10512
+ error: `OpenCode provider cache reload failed on port ${state.port}: ${error2 instanceof Error ? error2.message : String(error2)}`
10513
+ })
10514
+ );
10515
+ }
8348
10516
  if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity || fileActivity) {
8349
10517
  idlePolls = 0;
8350
10518
  idleMs = 0;
@@ -8372,8 +10540,8 @@ async function driveChannels(state, driver) {
8372
10540
  state.running = false;
8373
10541
  break;
8374
10542
  }
8375
- const errorMessage = error2 instanceof Error ? error2.message : String(error2);
8376
- logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
10543
+ const errorMessage3 = error2 instanceof Error ? error2.message : String(error2);
10544
+ logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage3}` });
8377
10545
  if (state.interactive) displayStatus(state);
8378
10546
  if (driver.hasInFlightWatchers()) {
8379
10547
  consecutiveDrainFailures = 0;
@@ -8390,7 +10558,7 @@ async function driveChannels(state, driver) {
8390
10558
  }
8391
10559
  }
8392
10560
  }
8393
- await new Promise((resolve3) => setTimeout(resolve3, CHANNEL_POLL_INTERVAL_MS));
10561
+ await new Promise((resolve4) => setTimeout(resolve4, CHANNEL_POLL_INTERVAL_MS));
8394
10562
  const cycleMs = performance.now() - cycleStartedAtMs;
8395
10563
  if (idleThisCycle) idleMs += cycleMs;
8396
10564
  if (unreachableThisCycle) unreachableMs += cycleMs;
@@ -8411,9 +10579,54 @@ async function driveChannels(state, driver) {
8411
10579
  }
8412
10580
  }
8413
10581
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
8414
- var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
10582
+ var SESSION_DB_RECLAIM_MAX_PAGES = 2e4;
10583
+ function shouldWarnForReclaimSkip(reason) {
10584
+ if (reason !== "sqlite-unavailable") return false;
10585
+ const version2 = /^v(\d+)\.(\d+)\.(\d+)$/.exec(process.version);
10586
+ if (!version2) return false;
10587
+ const major = Number(version2[1]);
10588
+ const minor = Number(version2[2]);
10589
+ const patch = Number(version2[3]);
10590
+ return major > 22 || major === 22 && (minor > 13 || minor === 13 && patch >= 0);
10591
+ }
8415
10592
  function sessionDbPath() {
8416
- return join7(homedir4(), ".local", "share", "opencode", "opencode.db");
10593
+ return join10(homedir6(), ".local", "share", "opencode", "opencode.db");
10594
+ }
10595
+ function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
10596
+ const record = {
10597
+ v: 1,
10598
+ event: "session_db_recovery",
10599
+ at: (/* @__PURE__ */ new Date()).toISOString(),
10600
+ stage: "verify",
10601
+ outcome: "schema_provenance_mismatch",
10602
+ severity: "error",
10603
+ reason: provenance.reason ?? "schema-provenance-mismatch",
10604
+ litestream_exit_code: null,
10605
+ attempt: null,
10606
+ replica_objects: null,
10607
+ replica_bytes: null,
10608
+ quarantine_destination: null,
10609
+ quarantined_objects: null,
10610
+ quarantine_failed_objects: null,
10611
+ quarantined_bytes: null,
10612
+ verified_restore_point: null,
10613
+ restore_points_tried: null,
10614
+ provenance_reason: provenance.reason,
10615
+ provenance_migration_delta: provenance.migrationDelta,
10616
+ replication_suspended: false,
10617
+ dbPath: sessionDbPath(),
10618
+ recorded_version: provenance.recordedVersion,
10619
+ current_version: currentVersion,
10620
+ provenance_pre_boot_migration_count: preBootMigrationCount
10621
+ };
10622
+ const activity = buildSessionDbRecoveryActivity(record);
10623
+ if (!activity) throw new Error("could not map session-DB provenance activity");
10624
+ logActivity(state, {
10625
+ type: activity.level === "error" ? "error" : "info",
10626
+ level: activity.level,
10627
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
10628
+ metadata: activity.metadata
10629
+ });
8417
10630
  }
8418
10631
  async function runSweep(state, driver, config) {
8419
10632
  const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
@@ -8460,7 +10673,7 @@ async function runSweep(state, driver, config) {
8460
10673
  const reclaimResult = await reclaimSessionDbSpace({
8461
10674
  dbPath: sessionDbPath(),
8462
10675
  maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
8463
- allowFullVacuum: protectedNow.size === 0
10676
+ allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
8464
10677
  });
8465
10678
  if (reclaimResult.ok) {
8466
10679
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
@@ -8473,7 +10686,7 @@ async function runSweep(state, driver, config) {
8473
10686
  } else {
8474
10687
  logActivity(state, {
8475
10688
  type: "info",
8476
- message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})`
10689
+ message: `Session cleanup: session-db space reclaim skipped (${reclaimResult.skipped})` + (reclaimResult.detail ? `: ${reclaimResult.detail}` : "")
8477
10690
  });
8478
10691
  }
8479
10692
  } catch (error2) {
@@ -8496,13 +10709,20 @@ function scheduleSessionCleanup(state, driver, options) {
8496
10709
  for (const warning2 of config.warnings) {
8497
10710
  logActivity(state, { type: "info", level: "warn", message: `Session cleanup: ${warning2}` });
8498
10711
  }
8499
- const dbBytes = statSessionDbBytes(homedir4());
10712
+ const dbBytes = statSessionDbBytes(homedir6());
8500
10713
  void (async () => {
8501
- const reclaimSkipReason = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
10714
+ const reclaimAvailability = dbBytes !== null && config.enabled ? await probeReclaimAvailability({ dbPath: sessionDbPath(), requiredBytes: dbBytes }) : null;
10715
+ if (reclaimAvailability !== null) {
10716
+ logActivity(state, {
10717
+ type: "info",
10718
+ level: shouldWarnForReclaimSkip(reclaimAvailability.reason) ? "warn" : "info",
10719
+ message: `Session cleanup: reclaim preflight skipped (${reclaimAvailability.reason})` + (reclaimAvailability.detail ? `: ${reclaimAvailability.detail}` : "")
10720
+ });
10721
+ }
8502
10722
  const sizeWarning = buildSessionStoreSizeWarning({
8503
10723
  dbBytes,
8504
10724
  cleanupEnabled: config.enabled,
8505
- reclaimSkipReason
10725
+ reclaimSkipReason: reclaimAvailability?.reason ?? null
8506
10726
  });
8507
10727
  if (sizeWarning !== null) {
8508
10728
  logActivity(state, { type: "info", level: "warn", message: sizeWarning });
@@ -8697,7 +10917,8 @@ function scheduleResourceUsageReporting(state, options) {
8697
10917
  });
8698
10918
  return;
8699
10919
  }
8700
- const collect = createResourceUsageCollector(homedir4());
10920
+ const { collect, stop } = createResourceUsageCollector(homedir6());
10921
+ state.stopResourceUsageSampling = stop;
8701
10922
  let consecutiveFailures = 0;
8702
10923
  const tick = async () => {
8703
10924
  try {
@@ -8793,6 +11014,8 @@ async function cleanup(state, opts = {}) {
8793
11014
  clearTimeout(timer);
8794
11015
  }
8795
11016
  state.sessionCleanupTimers = [];
11017
+ state.stopOpenCodeLogTail?.();
11018
+ state.stopOpenCodeLogTail = null;
8796
11019
  if (state.claudeUsageTimer) {
8797
11020
  clearTimeout(state.claudeUsageTimer);
8798
11021
  state.claudeUsageTimer = null;
@@ -8807,21 +11030,41 @@ async function cleanup(state, opts = {}) {
8807
11030
  clearTimeout(state.resourceUsageTimer);
8808
11031
  state.resourceUsageTimer = null;
8809
11032
  }
11033
+ state.stopResourceUsageSampling?.();
11034
+ state.stopResourceUsageSampling = null;
11035
+ const credentialSync = state.credentialSync;
11036
+ const flushCredentials = credentialSync ? async (phase, publish) => {
11037
+ await timeShutdownPhase(state, durations, phase, async () => {
11038
+ const outcomes = await credentialSync.stopAndFlush(publish);
11039
+ const level = Object.values(outcomes).every((outcome) => outcome === "ok") ? "info" : "warn";
11040
+ log2(
11041
+ state,
11042
+ `Credential flush: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
11043
+ level
11044
+ );
11045
+ });
11046
+ } : void 0;
11047
+ let drainSettled = true;
8810
11048
  if (opts.graceful && state.channelDriver) {
8811
11049
  state.channelDriver.stop();
11050
+ }
11051
+ if (flushCredentials) {
11052
+ await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
11053
+ }
11054
+ if (opts.graceful && state.channelDriver) {
8812
11055
  log2(state, "Draining in-flight channel work before shutdown...");
8813
11056
  if (state.interactive) {
8814
11057
  logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
8815
11058
  displayStatus(state);
8816
11059
  }
8817
11060
  const driver = state.channelDriver;
8818
- const settled = await timeShutdownPhase(
11061
+ drainSettled = await timeShutdownPhase(
8819
11062
  state,
8820
11063
  durations,
8821
11064
  "drain",
8822
11065
  () => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
8823
11066
  );
8824
- if (!settled) {
11067
+ if (!drainSettled) {
8825
11068
  logActivity(state, {
8826
11069
  type: "info",
8827
11070
  message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
@@ -8829,6 +11072,9 @@ async function cleanup(state, opts = {}) {
8829
11072
  if (state.interactive) displayStatus(state);
8830
11073
  }
8831
11074
  }
11075
+ if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
11076
+ await flushCredentials("credential_flush_final", true);
11077
+ }
8832
11078
  await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
8833
11079
  if (state.connection) {
8834
11080
  const connection = state.connection;
@@ -8864,13 +11110,56 @@ async function cleanup(state, opts = {}) {
8864
11110
  }
8865
11111
  return durations;
8866
11112
  }
11113
+ async function beginGracefulShutdown(state, trigger) {
11114
+ if (state.shuttingDown) return;
11115
+ state.shuttingDown = true;
11116
+ const shutdownStartedAt = Date.now();
11117
+ const shutdownMessage = trigger === "recycle" ? "Recycle requested \u2014 finishing in-flight work, then exiting" : "Shutting down...";
11118
+ if (state.interactive) {
11119
+ logActivity(state, { type: "info", message: shutdownMessage });
11120
+ displayStatus(state);
11121
+ } else {
11122
+ log2(state, shutdownMessage);
11123
+ }
11124
+ const durations = await cleanup(state, { graceful: true });
11125
+ const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
11126
+ await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
11127
+ let timer;
11128
+ const flushed = shutdownTelemetry().then(
11129
+ () => true,
11130
+ (error2) => {
11131
+ log2(
11132
+ state,
11133
+ `Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
11134
+ "warn"
11135
+ );
11136
+ return true;
11137
+ }
11138
+ );
11139
+ const timedOut = new Promise((resolve4) => {
11140
+ timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
11141
+ });
11142
+ if (!await Promise.race([flushed, timedOut])) {
11143
+ log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
11144
+ }
11145
+ clearTimeout(timer);
11146
+ });
11147
+ const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
11148
+ log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
11149
+ process.exit(0);
11150
+ }
8867
11151
  async function run(options) {
8868
11152
  const interactive = isInteractive(options.json);
8869
11153
  let logLevel;
8870
11154
  let fileSyncDirectories;
8871
11155
  try {
8872
11156
  logLevel = resolveLogLevel(options);
8873
- fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir4());
11157
+ fileSyncDirectories = resolveFileSyncDirectories(options.enableFileSyncTo, homedir6());
11158
+ if (options.restoreSessionDb && !options.sessionDbNoReplicateMarker) {
11159
+ throw new Error(
11160
+ "--restore-session-db requires --session-db-no-replicate-marker <path>; restore failures must block Litestream replication"
11161
+ );
11162
+ }
8874
11163
  } catch (error2) {
8875
11164
  const message = error2 instanceof Error ? error2.message : String(error2);
8876
11165
  if (options.json) {
@@ -8894,7 +11183,9 @@ async function run(options) {
8894
11183
  connected: false,
8895
11184
  opencodeConnected: false,
8896
11185
  opencodeVersion: null,
11186
+ sessionDbProvenanceAnomaly: false,
8897
11187
  opencodeProcess: null,
11188
+ stopOpenCodeLogTail: null,
8898
11189
  litestreamProcess: null,
8899
11190
  connection: null,
8900
11191
  channelDriver: null,
@@ -8909,9 +11200,24 @@ async function run(options) {
8909
11200
  openaiUsageTimer: null,
8910
11201
  openaiUsageRearm: null,
8911
11202
  resourceUsageTimer: null,
11203
+ stopResourceUsageSampling: null,
11204
+ credentialSync: null,
8912
11205
  authHeader: ""
8913
11206
  };
8914
11207
  setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
11208
+ if (options.credentialSyncMarker) {
11209
+ state.credentialSync = createCredentialSync({
11210
+ markerPath: options.credentialSyncMarker,
11211
+ env: process.env,
11212
+ log: (message, level = "info") => {
11213
+ if (level === "error") {
11214
+ logActivity(state, { type: "error", error: message });
11215
+ } else {
11216
+ logActivity(state, { type: "info", level, message });
11217
+ }
11218
+ }
11219
+ });
11220
+ }
8915
11221
  if (fileSyncDirectories.length > 0) {
8916
11222
  log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
8917
11223
  } else {
@@ -8937,43 +11243,7 @@ async function run(options) {
8937
11243
  "warn"
8938
11244
  );
8939
11245
  }
8940
- const handleSignal = async () => {
8941
- if (state.shuttingDown) return;
8942
- state.shuttingDown = true;
8943
- const shutdownStartedAt = Date.now();
8944
- if (state.interactive) {
8945
- logActivity(state, { type: "info", message: "Shutting down..." });
8946
- displayStatus(state);
8947
- } else {
8948
- log2(state, "Shutting down...");
8949
- }
8950
- const durations = await cleanup(state, { graceful: true });
8951
- const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
8952
- await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
8953
- let timer;
8954
- const flushed = shutdownTelemetry().then(
8955
- () => true,
8956
- (error2) => {
8957
- log2(
8958
- state,
8959
- `Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
8960
- "warn"
8961
- );
8962
- return true;
8963
- }
8964
- );
8965
- const timedOut = new Promise((resolve3) => {
8966
- timer = setTimeout(() => resolve3(false), telemetryBudgetMs);
8967
- });
8968
- if (!await Promise.race([flushed, timedOut])) {
8969
- log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
8970
- }
8971
- clearTimeout(timer);
8972
- });
8973
- const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
8974
- log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
8975
- process.exit(0);
8976
- };
11246
+ const handleSignal = () => beginGracefulShutdown(state, "signal");
8977
11247
  process.on("SIGINT", handleSignal);
8978
11248
  process.on("SIGTERM", handleSignal);
8979
11249
  try {
@@ -8983,15 +11253,15 @@ async function run(options) {
8983
11253
  printError("Authentication required");
8984
11254
  blank();
8985
11255
  console.log(
8986
- chalk6.dim("Set EVIDENT_RUNNER_KEY (or EVIDENT_AGENT_KEY) environment variable for CI")
11256
+ chalk7.dim("Set EVIDENT_RUNNER_KEY (or EVIDENT_AGENT_KEY) environment variable for CI")
8987
11257
  );
8988
- console.log(chalk6.dim("Or run `evident login` for interactive authentication"));
11258
+ console.log(chalk7.dim("Or run `evident login` for interactive authentication"));
8989
11259
  blank();
8990
11260
  process.exit(1);
8991
11261
  return;
8992
11262
  }
8993
11263
  blank();
8994
- console.log(chalk6.yellow("You are not logged in to Evident."));
11264
+ console.log(chalk7.yellow("You are not logged in to Evident."));
8995
11265
  blank();
8996
11266
  credentials2 = await promptForLogin(
8997
11267
  "Would you like to log in now?",
@@ -9041,7 +11311,7 @@ async function run(options) {
9041
11311
  );
9042
11312
  blank();
9043
11313
  console.log(
9044
- chalk6.dim(
11314
+ chalk7.dim(
9045
11315
  "Either provide --runner/--agent <id> or set EVIDENT_RUNNER_KEY/EVIDENT_AGENT_KEY"
9046
11316
  )
9047
11317
  );
@@ -9064,15 +11334,15 @@ async function run(options) {
9064
11334
  );
9065
11335
  if (interactive && !state.json) {
9066
11336
  blank();
9067
- console.log(chalk6.bold("Evident Run"));
9068
- console.log(chalk6.dim("-".repeat(40)));
11337
+ console.log(chalk7.bold("Evident Run"));
11338
+ console.log(chalk7.dim("-".repeat(40)));
9069
11339
  }
9070
11340
  const spinner = interactive && !state.json ? ora3("Validating runner...").start() : null;
9071
11341
  let validation = await getAgentInfo(state.agentId, state.authHeader);
9072
11342
  if (!validation.valid && validation.authFailed && interactive) {
9073
11343
  spinner?.fail("Authentication failed");
9074
11344
  blank();
9075
- console.log(chalk6.yellow("Your authentication token is invalid or expired."));
11345
+ console.log(chalk7.yellow("Your authentication token is invalid or expired."));
9076
11346
  blank();
9077
11347
  credentials2 = await promptForLogin(
9078
11348
  "Would you like to log in again?",
@@ -9103,27 +11373,140 @@ async function run(options) {
9103
11373
  } else {
9104
11374
  log2(state, "Not running in a MicroVM (MICROVM_ID unset) \u2014 nothing to report", "debug");
9105
11375
  }
11376
+ if (options.restoreRunnerCredentials) {
11377
+ log2(state, "Restoring runner credentials before starting OpenCode");
11378
+ const credentialContext = {
11379
+ env: process.env,
11380
+ log: (message, level = "info") => {
11381
+ if (level === "error") {
11382
+ logActivity(state, { type: "error", error: message });
11383
+ } else {
11384
+ logActivity(state, { type: "info", level, message });
11385
+ }
11386
+ }
11387
+ };
11388
+ const githubTokenPopulated = await installRunnerSecret(credentialContext);
11389
+ await restoreCredentialStores(credentialContext);
11390
+ if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
11391
+ }
11392
+ state.credentialSync?.arm();
11393
+ state.stopOpenCodeLogTail = tailOpenCodeLogFile(
11394
+ resolveOpenCodeLogPath(homedir6(), process.env),
11395
+ createOpenCodeActivityForwarder(() => ({
11396
+ agentId: state.agentId,
11397
+ authHeader: state.authHeader
11398
+ }))
11399
+ ).stop;
11400
+ let sessionDbVerifyFatal = false;
11401
+ if (!options.restoreSessionDb) {
11402
+ log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
11403
+ } else {
11404
+ const health = await checkOpenCodeHealth(state.port);
11405
+ if (health.healthy) {
11406
+ log2(
11407
+ state,
11408
+ "Skipping session-DB restore: OpenCode is already serving this database",
11409
+ "debug"
11410
+ );
11411
+ } else {
11412
+ const result = await restoreAndVerifySessionDb({
11413
+ dbPath: sessionDbPath(),
11414
+ litestreamConfig: options.litestreamConfig,
11415
+ noReplicateMarker: options.sessionDbNoReplicateMarker,
11416
+ env: process.env,
11417
+ log: (message, level = "info") => {
11418
+ if (level === "error") {
11419
+ logActivity(state, { type: "error", error: message });
11420
+ } else {
11421
+ logActivity(state, { type: "info", level, message });
11422
+ }
11423
+ },
11424
+ reportRecovery: (record) => reportSessionDbRecoveryRecord(state, record)
11425
+ });
11426
+ sessionDbVerifyFatal = result.verifyFatal;
11427
+ }
11428
+ }
9106
11429
  reportSessionDbRecovery(state);
11430
+ if (sessionDbVerifyFatal) {
11431
+ throw new Error(
11432
+ "SESSION-DB-INTEGRITY-EXHAUSTED: the corrupt session DB could not be proven separated from the active backup prefix or removed from disk"
11433
+ );
11434
+ }
11435
+ applyRunnerOpenCodeConfig({
11436
+ overlayPath: options.opencodeConfigOverlay,
11437
+ log: (message, level = "info") => {
11438
+ if (level === "error") {
11439
+ logActivity(state, { type: "error", error: message });
11440
+ } else {
11441
+ logActivity(state, { type: "info", level, message });
11442
+ }
11443
+ }
11444
+ });
9107
11445
  const { timeoutMs: opencodeStartTimeoutMs, warnings: opencodeStartTimeoutWarnings } = resolveOpenCodeStartTimeoutMs(options, process.env);
9108
11446
  for (const warning2 of opencodeStartTimeoutWarnings) {
9109
11447
  logActivity(state, { type: "info", level: "warn", message: warning2 });
9110
11448
  }
11449
+ const { version: opencodeVersion, warnings: opencodeVersionWarnings } = resolveOpenCodeVersion(
11450
+ options,
11451
+ process.env
11452
+ );
11453
+ for (const warning2 of opencodeVersionWarnings) {
11454
+ logActivity(state, { type: "info", level: "warn", message: warning2 });
11455
+ }
9111
11456
  const { value: maxActiveSessions, warnings: maxActiveSessionsWarnings } = resolveMaxActiveSessions(options, process.env);
9112
11457
  for (const warning2 of maxActiveSessionsWarnings) {
9113
11458
  logActivity(state, { type: "info", level: "warn", message: warning2 });
9114
11459
  }
11460
+ const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
9115
11461
  const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
9116
11462
  try {
9117
- const oc = await ensureOpenCodeRunning({
11463
+ const oc = opencodeVersion === "v2" ? await ensureOpenCode2Running({
11464
+ port: state.port,
11465
+ interactive: state.interactive,
11466
+ agentId: state.agentId,
11467
+ log: (message) => log2(state, message),
11468
+ startTimeoutMs: opencodeStartTimeoutMs,
11469
+ inheritStdio: Boolean(options.opencodePidFile)
11470
+ }) : await ensureOpenCodeRunning({
9118
11471
  port: state.port,
9119
11472
  interactive: state.interactive,
9120
11473
  agentId: state.agentId,
9121
11474
  log: (message) => log2(state, message),
9122
- startTimeoutMs: opencodeStartTimeoutMs
11475
+ startTimeoutMs: opencodeStartTimeoutMs,
11476
+ inheritStdio: Boolean(options.opencodePidFile)
9123
11477
  });
9124
11478
  state.port = oc.port;
9125
- state.opencodeProcess = oc.process;
11479
+ state.opencodeProcess = options.opencodePidFile ? null : oc.process;
9126
11480
  state.opencodeVersion = oc.version;
11481
+ if (options.opencodePidFile && oc.process?.pid !== void 0) {
11482
+ try {
11483
+ writeFileSync6(options.opencodePidFile, `${oc.process.pid}
11484
+ `, { mode: 384 });
11485
+ chmodSync3(options.opencodePidFile, 384);
11486
+ } catch (error2) {
11487
+ logActivity(state, {
11488
+ type: "error",
11489
+ error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
11490
+ });
11491
+ }
11492
+ }
11493
+ if (state.opencodeVersion !== null) {
11494
+ const provenance = checkSessionDbProvenance({
11495
+ dbPath: sessionDbPath(),
11496
+ currentVersion: state.opencodeVersion,
11497
+ homeDir: homedir6(),
11498
+ env: process.env
11499
+ });
11500
+ if (provenance.anomaly) {
11501
+ state.sessionDbProvenanceAnomaly = true;
11502
+ logSessionDbProvenanceMismatch(
11503
+ state,
11504
+ provenance,
11505
+ state.opencodeVersion,
11506
+ preBootMigrationIds?.length ?? null
11507
+ );
11508
+ }
11509
+ }
9127
11510
  state.opencodeConnected = oc.notReadyReason === null;
9128
11511
  const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
9129
11512
  ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
@@ -9146,10 +11529,10 @@ async function run(options) {
9146
11529
  if (state.interactive && !state.json) {
9147
11530
  logActivity(state, { type: "info", level: "warn", message: noProviderWarning });
9148
11531
  blank();
9149
- console.log(chalk6.yellow("\u26A0 No OpenCode model provider is configured."));
11532
+ console.log(chalk7.yellow("\u26A0 No OpenCode model provider is configured."));
9150
11533
  console.log(
9151
- chalk6.dim(
9152
- `Run ${chalk6.cyan("opencode auth login")} to set one up \u2014 messages will fail until then.`
11534
+ chalk7.dim(
11535
+ `Run ${chalk7.cyan("opencode auth login")} to set one up \u2014 messages will fail until then.`
9153
11536
  )
9154
11537
  );
9155
11538
  blank();
@@ -9160,7 +11543,75 @@ async function run(options) {
9160
11543
  ocSpinner?.fail(error2.message);
9161
11544
  throw error2;
9162
11545
  }
9163
- if (options.litestreamConfig) {
11546
+ if (options.litestreamPidFile) {
11547
+ if (options.sessionDbNoReplicateMarker && existsSync3(options.sessionDbNoReplicateMarker)) {
11548
+ log2(
11549
+ state,
11550
+ `Skipping Litestream replication because ${options.sessionDbNoReplicateMarker} marks this session database unsafe to replicate`
11551
+ );
11552
+ } else if (!options.litestreamConfig) {
11553
+ logActivity(state, {
11554
+ type: "info",
11555
+ level: "warn",
11556
+ message: "Skipping Litestream replication because no configuration file was provided"
11557
+ });
11558
+ } else {
11559
+ let existingPid;
11560
+ if (existsSync3(options.litestreamPidFile)) {
11561
+ try {
11562
+ const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
11563
+ const parsedPid = Number(rawPid);
11564
+ if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
11565
+ existingPid = parsedPid;
11566
+ }
11567
+ } catch (error2) {
11568
+ logActivity(state, {
11569
+ type: "info",
11570
+ level: "warn",
11571
+ message: `Could not read Litestream pid file ${options.litestreamPidFile}; checking for a new process: ${error2 instanceof Error ? error2.message : String(error2)}`
11572
+ });
11573
+ }
11574
+ }
11575
+ if (existingPid !== void 0 && isProcessAlive(existingPid)) {
11576
+ log2(state, `Litestream replication is already running with pid ${existingPid}`);
11577
+ } else {
11578
+ const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
11579
+ state.litestreamProcess = null;
11580
+ let failureHandled = false;
11581
+ const reportImageOwnedReplicationFailure = (message) => {
11582
+ if (failureHandled || state.shuttingDown || !state.running) return;
11583
+ failureHandled = true;
11584
+ logActivity(state, { type: "error", error: message });
11585
+ if (state.interactive) displayStatus(state);
11586
+ };
11587
+ litestreamProcess.on("exit", (code, signal) => {
11588
+ reportImageOwnedReplicationFailure(
11589
+ `Litestream replication stopped unexpectedly (code: ${code ?? "null"}, signal: ${signal ?? "none"})`
11590
+ );
11591
+ });
11592
+ litestreamProcess.on("error", (error2) => {
11593
+ reportImageOwnedReplicationFailure(
11594
+ `Litestream replication failed: ${error2 instanceof Error ? error2.message : String(error2)}`
11595
+ );
11596
+ });
11597
+ try {
11598
+ if (litestreamProcess.pid !== void 0) {
11599
+ writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
11600
+ `, {
11601
+ mode: 384
11602
+ });
11603
+ chmodSync3(options.litestreamPidFile, 384);
11604
+ }
11605
+ } catch (error2) {
11606
+ logActivity(state, {
11607
+ type: "error",
11608
+ error: `Failed to write Litestream pid file ${options.litestreamPidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
11609
+ });
11610
+ }
11611
+ log2(state, `Started litestream replication with config ${options.litestreamConfig}`);
11612
+ }
11613
+ }
11614
+ } else if (options.litestreamConfig) {
9164
11615
  const litestreamProcess = startSessionDbReplication(options.litestreamConfig);
9165
11616
  state.litestreamProcess = litestreamProcess;
9166
11617
  let failureHandled = false;
@@ -9205,7 +11656,7 @@ async function run(options) {
9205
11656
  // #559: the `--enable-file-sync-to` allow-list. Empty ⇒ pending files are
9206
11657
  // REJECTED with `file_sync_disabled` on the ack, not silently ignored.
9207
11658
  fileSyncDirectories,
9208
- homeDir: homedir4(),
11659
+ homeDir: homedir6(),
9209
11660
  maxActiveSessions,
9210
11661
  log: (entry) => (
9211
11662
  // Thread the driver's real level straight through so `debug`/`warn`
@@ -9331,6 +11782,18 @@ async function run(options) {
9331
11782
  if (state.interactive) displayStatus(state);
9332
11783
  });
9333
11784
  },
11785
+ // Both loops are rearmed because `rearm()` is idempotent for the
11786
+ // provider that did not just connect, and is a no-op when reporting is off.
11787
+ onUsageRearmPing: () => {
11788
+ if (!state.running) return;
11789
+ logActivity(state, {
11790
+ type: "info",
11791
+ level: "debug",
11792
+ message: "Usage rearm ping received"
11793
+ });
11794
+ state.claudeUsageRearm?.();
11795
+ state.openaiUsageRearm?.();
11796
+ },
9334
11797
  onInfo: (message) => logActivity(state, { type: "info", message })
9335
11798
  }
9336
11799
  });
@@ -9351,7 +11814,17 @@ async function run(options) {
9351
11814
  setTimer: (timer) => {
9352
11815
  state.openaiUsageTimer = timer;
9353
11816
  },
9354
- fetchUsage: () => getOpenAiUsage(state.port),
11817
+ fetchUsage: async () => {
11818
+ const usage = await getOpenAiUsage(state.port);
11819
+ if (usage.subscription === null) {
11820
+ logActivity(state, {
11821
+ type: "info",
11822
+ level: "debug",
11823
+ message: "OpenAI usage subscription could not be identified from the local credential"
11824
+ });
11825
+ }
11826
+ return usage;
11827
+ },
9355
11828
  report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
9356
11829
  isLocalCredentialProblem: isLocalCredentialProblem2,
9357
11830
  forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
@@ -9397,7 +11870,7 @@ async function run(options) {
9397
11870
  }
9398
11871
 
9399
11872
  // src/index.ts
9400
- var { version } = createRequire(import.meta.url)("../package.json");
11873
+ var { version } = createRequire2(import.meta.url)("../package.json");
9401
11874
  var program = new Command();
9402
11875
  program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
9403
11876
  "--endpoint <url>",
@@ -9425,6 +11898,9 @@ program.command("run").description("Connect to Evident and process messages").op
9425
11898
  ).option("-v, --verbose", "Alias for --log-level debug (ignored if --log-level is set)").option("-c, --conversation <id>", "Process only this specific conversation").option("--idle-timeout <seconds>", "Exit after N seconds idle").option(
9426
11899
  "--opencode-start-timeout <seconds>",
9427
11900
  "Seconds to wait for OpenCode to become healthy when the runner starts it (default: 180). Env: EVIDENT_OPENCODE_START_TIMEOUT"
11901
+ ).option(
11902
+ "--opencode-version <v1|v2>",
11903
+ "Which OpenCode major version to launch: v1 or v2 (default: v1). Env: EVIDENT_OPENCODE_VERSION"
9428
11904
  ).option("--json", "Output in JSON format").option(
9429
11905
  "--session-cleanup-max-age <duration>",
9430
11906
  "Delete OpenCode sessions idle longer than this (e.g. 7d, 24h). Enables cleanup. Env: EVIDENT_SESSION_CLEANUP_MAX_AGE"
@@ -9457,6 +11933,27 @@ program.command("run").description("Connect to Evident and process messages").op
9457
11933
  ).option(
9458
11934
  "--litestream-config <path>",
9459
11935
  "Replicate the OpenCode session database with `litestream replicate` using this config file (written by the runner image). Omit to disable replication."
11936
+ ).option(
11937
+ "--opencode-pid-file <path>",
11938
+ "Record the OpenCode pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
11939
+ ).option(
11940
+ "--litestream-pid-file <path>",
11941
+ "Record the Litestream pid here and leave the process running at shutdown for the runner image's lifecycle hooks to stop."
11942
+ ).option(
11943
+ "--session-db-no-replicate-marker <path>",
11944
+ "Read this marker as a gate before starting Litestream; the session-DB restore writes it when this boot's database is not safe to replicate."
11945
+ ).option(
11946
+ "--restore-session-db",
11947
+ "Restore and verify the OpenCode session database before starting OpenCode; requires --litestream-config for the replica and --session-db-no-replicate-marker for give-up records."
11948
+ ).option(
11949
+ "--restore-runner-credentials",
11950
+ "Restore the hosted runner secret and persisted credential stores before starting OpenCode."
11951
+ ).option(
11952
+ "--opencode-config-overlay <path>",
11953
+ "Apply this runner-provided OpenCode config before starting OpenCode."
11954
+ ).option(
11955
+ "--credential-sync-marker <path>",
11956
+ "Own the interval credential sync and write this marker once the shutdown flush has finished, so the runner image's lifecycle hooks can wait on it."
9460
11957
  ).action(
9461
11958
  (options) => {
9462
11959
  run({
@@ -9472,6 +11969,7 @@ program.command("run").description("Connect to Evident and process messages").op
9472
11969
  // Raw string — validation/precedence is single-sourced in run.ts's
9473
11970
  // resolveOpenCodeStartTimeoutMs (flag > EVIDENT_OPENCODE_START_TIMEOUT > 180s default).
9474
11971
  opencodeStartTimeout: options.opencodeStartTimeout,
11972
+ opencodeVersion: options.opencodeVersion,
9475
11973
  json: options.json,
9476
11974
  // Raw strings — the resolver in run.ts single-sources parsing (M1).
9477
11975
  sessionCleanupMaxAge: options.sessionCleanupMaxAge,
@@ -9489,7 +11987,14 @@ program.command("run").description("Connect to Evident and process messages").op
9489
11987
  // resolveFileSyncDirectories.
9490
11988
  enableFileSyncTo: options.enableFileSyncTo,
9491
11989
  tunnelReadyFile: options.tunnelReadyFile,
9492
- litestreamConfig: options.litestreamConfig
11990
+ litestreamConfig: options.litestreamConfig,
11991
+ opencodePidFile: options.opencodePidFile,
11992
+ litestreamPidFile: options.litestreamPidFile,
11993
+ sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
11994
+ restoreSessionDb: options.restoreSessionDb,
11995
+ restoreRunnerCredentials: options.restoreRunnerCredentials,
11996
+ opencodeConfigOverlay: options.opencodeConfigOverlay,
11997
+ credentialSyncMarker: options.credentialSyncMarker
9493
11998
  });
9494
11999
  }
9495
12000
  );